Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // fileread.cc -- read files for gold |
2 | ||
0f7c0701 | 3 | // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
bae7f79e ILT |
23 | #include "gold.h" |
24 | ||
bae7f79e ILT |
25 | #include <cstring> |
26 | #include <cerrno> | |
27 | #include <fcntl.h> | |
28 | #include <unistd.h> | |
d1038c21 | 29 | #include <sys/mman.h> |
cb295612 | 30 | #include <sys/uio.h> |
3d857b98 | 31 | #include <sys/stat.h> |
51dee2fe | 32 | #include "filenames.h" |
bae7f79e | 33 | |
2285a610 | 34 | #include "debug.h" |
bc644c6c | 35 | #include "parameters.h" |
bae7f79e ILT |
36 | #include "options.h" |
37 | #include "dirsearch.h" | |
bc644c6c ILT |
38 | #include "target.h" |
39 | #include "binary.h" | |
2a00e4fb | 40 | #include "descriptors.h" |
bae7f79e ILT |
41 | #include "fileread.h" |
42 | ||
d9a893b8 ILT |
43 | #ifndef HAVE_READV |
44 | struct iovec { void* iov_base; size_t iov_len }; | |
45 | ssize_t | |
46 | readv(int, const iovec*, int) | |
47 | { | |
48 | gold_unreachable(); | |
49 | } | |
50 | #endif | |
51 | ||
bae7f79e ILT |
52 | namespace gold |
53 | { | |
54 | ||
55 | // Class File_read::View. | |
56 | ||
57 | File_read::View::~View() | |
58 | { | |
a3ad94ed | 59 | gold_assert(!this->is_locked()); |
2c849493 | 60 | switch (this->data_ownership_) |
d1038c21 | 61 | { |
2c849493 ILT |
62 | case DATA_ALLOCATED_ARRAY: |
63 | delete[] this->data_; | |
64 | break; | |
65 | case DATA_MMAPPED: | |
d1038c21 | 66 | if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0) |
75f2446e | 67 | gold_warning(_("munmap failed: %s"), strerror(errno)); |
e44fcf3b | 68 | File_read::current_mapped_bytes -= this->size_; |
2c849493 ILT |
69 | break; |
70 | case DATA_NOT_OWNED: | |
71 | break; | |
72 | default: | |
73 | gold_unreachable(); | |
d1038c21 | 74 | } |
bae7f79e ILT |
75 | } |
76 | ||
77 | void | |
78 | File_read::View::lock() | |
79 | { | |
80 | ++this->lock_count_; | |
81 | } | |
82 | ||
83 | void | |
84 | File_read::View::unlock() | |
85 | { | |
a3ad94ed | 86 | gold_assert(this->lock_count_ > 0); |
bae7f79e ILT |
87 | --this->lock_count_; |
88 | } | |
89 | ||
90 | bool | |
91 | File_read::View::is_locked() | |
92 | { | |
93 | return this->lock_count_ > 0; | |
94 | } | |
95 | ||
96 | // Class File_read. | |
97 | ||
e44fcf3b ILT |
98 | // The File_read static variables. |
99 | unsigned long long File_read::total_mapped_bytes; | |
100 | unsigned long long File_read::current_mapped_bytes; | |
101 | unsigned long long File_read::maximum_mapped_bytes; | |
102 | ||
bae7f79e ILT |
103 | File_read::~File_read() |
104 | { | |
17a1d0a9 | 105 | gold_assert(this->token_.is_writable()); |
2a00e4fb | 106 | if (this->is_descriptor_opened_) |
bae7f79e | 107 | { |
2a00e4fb | 108 | release_descriptor(this->descriptor_, true); |
bae7f79e | 109 | this->descriptor_ = -1; |
2a00e4fb | 110 | this->is_descriptor_opened_ = false; |
bae7f79e ILT |
111 | } |
112 | this->name_.clear(); | |
113 | this->clear_views(true); | |
2c849493 ILT |
114 | if (this->whole_file_view_) |
115 | delete this->whole_file_view_; | |
bae7f79e ILT |
116 | } |
117 | ||
5a6f7e2d ILT |
118 | // Open the file. |
119 | ||
bae7f79e | 120 | bool |
17a1d0a9 | 121 | File_read::open(const Task* task, const std::string& name) |
bae7f79e | 122 | { |
17a1d0a9 | 123 | gold_assert(this->token_.is_writable() |
a3ad94ed | 124 | && this->descriptor_ < 0 |
2a00e4fb | 125 | && !this->is_descriptor_opened_ |
a3ad94ed | 126 | && this->name_.empty()); |
bae7f79e | 127 | this->name_ = name; |
82dcae9d | 128 | |
2a00e4fb ILT |
129 | this->descriptor_ = open_descriptor(-1, this->name_.c_str(), |
130 | O_RDONLY); | |
82dcae9d ILT |
131 | |
132 | if (this->descriptor_ >= 0) | |
133 | { | |
2a00e4fb | 134 | this->is_descriptor_opened_ = true; |
82dcae9d ILT |
135 | struct stat s; |
136 | if (::fstat(this->descriptor_, &s) < 0) | |
75f2446e ILT |
137 | gold_error(_("%s: fstat failed: %s"), |
138 | this->name_.c_str(), strerror(errno)); | |
82dcae9d | 139 | this->size_ = s.st_size; |
2285a610 ILT |
140 | gold_debug(DEBUG_FILES, "Attempt to open %s succeeded", |
141 | this->name_.c_str()); | |
82dcae9d | 142 | |
2c849493 ILT |
143 | // Options may not yet be ready e.g. when reading a version |
144 | // script. We then default to --no-keep-files-mapped. | |
145 | if (parameters->options_valid() | |
146 | && parameters->options().keep_files_mapped()) | |
147 | { | |
148 | const unsigned char* contents = static_cast<const unsigned char*>( | |
149 | ::mmap(NULL, this->size_, PROT_READ, MAP_PRIVATE, | |
150 | this->descriptor_, 0)); | |
151 | if (contents == MAP_FAILED) | |
152 | gold_fatal(_("%s: mmap failed: %s"), this->filename().c_str(), | |
153 | strerror(errno)); | |
154 | this->whole_file_view_ = new View(0, this->size_, contents, 0, false, | |
155 | View::DATA_MMAPPED); | |
156 | this->mapped_bytes_ += this->size_; | |
157 | } | |
158 | ||
1e52a9c1 CS |
159 | this->token_.add_writer(task); |
160 | } | |
82dcae9d | 161 | |
bae7f79e ILT |
162 | return this->descriptor_ >= 0; |
163 | } | |
164 | ||
bc644c6c | 165 | // Open the file with the contents in memory. |
5a6f7e2d ILT |
166 | |
167 | bool | |
17a1d0a9 ILT |
168 | File_read::open(const Task* task, const std::string& name, |
169 | const unsigned char* contents, off_t size) | |
bae7f79e | 170 | { |
17a1d0a9 | 171 | gold_assert(this->token_.is_writable() |
5a6f7e2d | 172 | && this->descriptor_ < 0 |
2a00e4fb | 173 | && !this->is_descriptor_opened_ |
5a6f7e2d ILT |
174 | && this->name_.empty()); |
175 | this->name_ = name; | |
2c849493 ILT |
176 | this->whole_file_view_ = new View(0, size, contents, 0, false, |
177 | View::DATA_NOT_OWNED); | |
82dcae9d | 178 | this->size_ = size; |
17a1d0a9 | 179 | this->token_.add_writer(task); |
5a6f7e2d | 180 | return true; |
bae7f79e ILT |
181 | } |
182 | ||
2a00e4fb ILT |
183 | // Reopen a descriptor if necessary. |
184 | ||
185 | void | |
186 | File_read::reopen_descriptor() | |
187 | { | |
188 | if (!this->is_descriptor_opened_) | |
189 | { | |
190 | this->descriptor_ = open_descriptor(this->descriptor_, | |
191 | this->name_.c_str(), | |
192 | O_RDONLY); | |
193 | if (this->descriptor_ < 0) | |
194 | gold_fatal(_("could not reopen file %s"), this->name_.c_str()); | |
195 | this->is_descriptor_opened_ = true; | |
196 | } | |
197 | } | |
198 | ||
17a1d0a9 ILT |
199 | // Release the file. This is called when we are done with the file in |
200 | // a Task. | |
201 | ||
bae7f79e | 202 | void |
17a1d0a9 | 203 | File_read::release() |
bae7f79e | 204 | { |
17a1d0a9 ILT |
205 | gold_assert(this->is_locked()); |
206 | ||
207 | File_read::total_mapped_bytes += this->mapped_bytes_; | |
208 | File_read::current_mapped_bytes += this->mapped_bytes_; | |
209 | this->mapped_bytes_ = 0; | |
210 | if (File_read::current_mapped_bytes > File_read::maximum_mapped_bytes) | |
211 | File_read::maximum_mapped_bytes = File_read::current_mapped_bytes; | |
212 | ||
39d0cb0e | 213 | // Only clear views if there is only one attached object. Otherwise |
2a00e4fb ILT |
214 | // we waste time trying to clear cached archive views. Similarly |
215 | // for releasing the descriptor. | |
39d0cb0e | 216 | if (this->object_count_ <= 1) |
2a00e4fb ILT |
217 | { |
218 | this->clear_views(false); | |
219 | if (this->is_descriptor_opened_) | |
220 | { | |
221 | release_descriptor(this->descriptor_, false); | |
222 | this->is_descriptor_opened_ = false; | |
223 | } | |
224 | } | |
17a1d0a9 ILT |
225 | |
226 | this->released_ = true; | |
bae7f79e ILT |
227 | } |
228 | ||
17a1d0a9 ILT |
229 | // Lock the file. |
230 | ||
bae7f79e | 231 | void |
17a1d0a9 | 232 | File_read::lock(const Task* task) |
bae7f79e | 233 | { |
17a1d0a9 ILT |
234 | gold_assert(this->released_); |
235 | this->token_.add_writer(task); | |
236 | this->released_ = false; | |
237 | } | |
e44fcf3b | 238 | |
17a1d0a9 ILT |
239 | // Unlock the file. |
240 | ||
241 | void | |
242 | File_read::unlock(const Task* task) | |
243 | { | |
244 | this->release(); | |
245 | this->token_.remove_writer(task); | |
bae7f79e ILT |
246 | } |
247 | ||
17a1d0a9 ILT |
248 | // Return whether the file is locked. |
249 | ||
bae7f79e | 250 | bool |
7004837e | 251 | File_read::is_locked() const |
bae7f79e | 252 | { |
17a1d0a9 ILT |
253 | if (!this->token_.is_writable()) |
254 | return true; | |
255 | // The file is not locked, so it should have been released. | |
256 | gold_assert(this->released_); | |
257 | return false; | |
bae7f79e ILT |
258 | } |
259 | ||
260 | // See if we have a view which covers the file starting at START for | |
261 | // SIZE bytes. Return a pointer to the View if found, NULL if not. | |
39d0cb0e ILT |
262 | // If BYTESHIFT is not -1U, the returned View must have the specified |
263 | // byte shift; otherwise, it may have any byte shift. If VSHIFTED is | |
264 | // not NULL, this sets *VSHIFTED to a view which would have worked if | |
265 | // not for the requested BYTESHIFT. | |
bae7f79e | 266 | |
ead1e424 | 267 | inline File_read::View* |
39d0cb0e ILT |
268 | File_read::find_view(off_t start, section_size_type size, |
269 | unsigned int byteshift, File_read::View** vshifted) const | |
bae7f79e | 270 | { |
39d0cb0e ILT |
271 | if (vshifted != NULL) |
272 | *vshifted = NULL; | |
273 | ||
2c849493 ILT |
274 | // If we have the whole file mmapped, and the alignment is right, |
275 | // we can return it. | |
276 | if (this->whole_file_view_) | |
277 | if (byteshift == -1U || byteshift == 0) | |
278 | return this->whole_file_view_; | |
279 | ||
ead1e424 | 280 | off_t page = File_read::page_offset(start); |
cb295612 | 281 | |
39d0cb0e ILT |
282 | unsigned int bszero = 0; |
283 | Views::const_iterator p = this->views_.upper_bound(std::make_pair(page - 1, | |
284 | bszero)); | |
285 | ||
286 | while (p != this->views_.end() && p->first.first <= page) | |
cb295612 | 287 | { |
39d0cb0e ILT |
288 | if (p->second->start() <= start |
289 | && (p->second->start() + static_cast<off_t>(p->second->size()) | |
290 | >= start + static_cast<off_t>(size))) | |
291 | { | |
292 | if (byteshift == -1U || byteshift == p->second->byteshift()) | |
293 | { | |
294 | p->second->set_accessed(); | |
295 | return p->second; | |
296 | } | |
cb295612 | 297 | |
39d0cb0e ILT |
298 | if (vshifted != NULL && *vshifted == NULL) |
299 | *vshifted = p->second; | |
300 | } | |
cb295612 | 301 | |
39d0cb0e ILT |
302 | ++p; |
303 | } | |
cb295612 | 304 | |
39d0cb0e | 305 | return NULL; |
bae7f79e ILT |
306 | } |
307 | ||
82dcae9d | 308 | // Read SIZE bytes from the file starting at offset START. Read into |
9eb9fa57 | 309 | // the buffer at P. |
bae7f79e | 310 | |
9eb9fa57 | 311 | void |
2a00e4fb | 312 | File_read::do_read(off_t start, section_size_type size, void* p) |
bae7f79e | 313 | { |
8cce6718 | 314 | ssize_t bytes; |
2c849493 | 315 | if (this->whole_file_view_ != NULL) |
bae7f79e | 316 | { |
9eb9fa57 | 317 | bytes = this->size_ - start; |
8cce6718 | 318 | if (static_cast<section_size_type>(bytes) >= size) |
9eb9fa57 | 319 | { |
2c849493 | 320 | memcpy(p, this->whole_file_view_->data() + start, size); |
9eb9fa57 ILT |
321 | return; |
322 | } | |
bae7f79e | 323 | } |
9eb9fa57 | 324 | else |
82dcae9d | 325 | { |
2a00e4fb | 326 | this->reopen_descriptor(); |
8cce6718 ILT |
327 | bytes = ::pread(this->descriptor_, p, size, start); |
328 | if (static_cast<section_size_type>(bytes) == size) | |
329 | return; | |
330 | ||
331 | if (bytes < 0) | |
9eb9fa57 | 332 | { |
75f2446e ILT |
333 | gold_fatal(_("%s: pread failed: %s"), |
334 | this->filename().c_str(), strerror(errno)); | |
335 | return; | |
9eb9fa57 | 336 | } |
bae7f79e | 337 | } |
9eb9fa57 | 338 | |
75f2446e ILT |
339 | gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"), |
340 | this->filename().c_str(), | |
341 | static_cast<long long>(bytes), | |
342 | static_cast<long long>(size), | |
343 | static_cast<long long>(start)); | |
bae7f79e ILT |
344 | } |
345 | ||
ba45d247 ILT |
346 | // Read data from the file. |
347 | ||
bae7f79e | 348 | void |
2a00e4fb | 349 | File_read::read(off_t start, section_size_type size, void* p) |
ba45d247 | 350 | { |
39d0cb0e | 351 | const File_read::View* pv = this->find_view(start, size, -1U, NULL); |
ba45d247 ILT |
352 | if (pv != NULL) |
353 | { | |
39d0cb0e | 354 | memcpy(p, pv->data() + (start - pv->start() + pv->byteshift()), size); |
ba45d247 ILT |
355 | return; |
356 | } | |
357 | ||
9eb9fa57 | 358 | this->do_read(start, size, p); |
bae7f79e ILT |
359 | } |
360 | ||
39d0cb0e ILT |
361 | // Add a new view. There may already be an existing view at this |
362 | // offset. If there is, the new view will be larger, and should | |
363 | // replace the old view. | |
bae7f79e | 364 | |
39d0cb0e ILT |
365 | void |
366 | File_read::add_view(File_read::View* v) | |
bae7f79e | 367 | { |
39d0cb0e ILT |
368 | std::pair<Views::iterator, bool> ins = |
369 | this->views_.insert(std::make_pair(std::make_pair(v->start(), | |
370 | v->byteshift()), | |
371 | v)); | |
372 | if (ins.second) | |
373 | return; | |
374 | ||
375 | // There was an existing view at this offset. It must not be large | |
376 | // enough. We can't delete it here, since something might be using | |
377 | // it; we put it on a list to be deleted when the file is unlocked. | |
378 | File_read::View* vold = ins.first->second; | |
379 | gold_assert(vold->size() < v->size()); | |
380 | if (vold->should_cache()) | |
cb295612 | 381 | { |
39d0cb0e ILT |
382 | v->set_cache(); |
383 | vold->clear_cache(); | |
cb295612 | 384 | } |
39d0cb0e | 385 | this->saved_views_.push_back(vold); |
cb295612 | 386 | |
39d0cb0e ILT |
387 | ins.first->second = v; |
388 | } | |
ead1e424 | 389 | |
39d0cb0e ILT |
390 | // Make a new view with a specified byteshift, reading the data from |
391 | // the file. | |
ead1e424 | 392 | |
39d0cb0e ILT |
393 | File_read::View* |
394 | File_read::make_view(off_t start, section_size_type size, | |
395 | unsigned int byteshift, bool cache) | |
396 | { | |
3f2e6a2d CC |
397 | gold_assert(size > 0); |
398 | ||
a9caad02 | 399 | // Check that start and end of the view are within the file. |
de31bda5 ILT |
400 | if (start > this->size_ |
401 | || (static_cast<unsigned long long>(size) | |
402 | > static_cast<unsigned long long>(this->size_ - start))) | |
a9caad02 CC |
403 | gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds " |
404 | "size of file; the file may be corrupt"), | |
405 | this->filename().c_str(), | |
406 | static_cast<long long>(size), | |
407 | static_cast<long long>(start)); | |
408 | ||
39d0cb0e | 409 | off_t poff = File_read::page_offset(start); |
ead1e424 | 410 | |
fe8718a4 | 411 | section_size_type psize = File_read::pages(size + (start - poff)); |
bae7f79e | 412 | |
8d32f935 | 413 | if (poff + static_cast<off_t>(psize) >= this->size_) |
82dcae9d ILT |
414 | { |
415 | psize = this->size_ - poff; | |
fe8718a4 | 416 | gold_assert(psize >= size); |
82dcae9d | 417 | } |
ead1e424 | 418 | |
39d0cb0e | 419 | File_read::View* v; |
2c849493 | 420 | if (this->whole_file_view_ != NULL || byteshift != 0) |
d1038c21 | 421 | { |
39d0cb0e ILT |
422 | unsigned char* p = new unsigned char[psize + byteshift]; |
423 | memset(p, 0, byteshift); | |
424 | this->do_read(poff, psize, p + byteshift); | |
2c849493 ILT |
425 | v = new File_read::View(poff, psize, p, byteshift, cache, |
426 | View::DATA_ALLOCATED_ARRAY); | |
d1038c21 ILT |
427 | } |
428 | else | |
429 | { | |
2a00e4fb | 430 | this->reopen_descriptor(); |
cb295612 | 431 | void* p = ::mmap(NULL, psize, PROT_READ, MAP_PRIVATE, |
d1038c21 ILT |
432 | this->descriptor_, poff); |
433 | if (p == MAP_FAILED) | |
75f2446e ILT |
434 | gold_fatal(_("%s: mmap offset %lld size %lld failed: %s"), |
435 | this->filename().c_str(), | |
436 | static_cast<long long>(poff), | |
437 | static_cast<long long>(psize), | |
438 | strerror(errno)); | |
d1038c21 | 439 | |
e44fcf3b ILT |
440 | this->mapped_bytes_ += psize; |
441 | ||
d1038c21 | 442 | const unsigned char* pbytes = static_cast<const unsigned char*>(p); |
2c849493 ILT |
443 | v = new File_read::View(poff, psize, pbytes, 0, cache, |
444 | View::DATA_MMAPPED); | |
d1038c21 | 445 | } |
ead1e424 | 446 | |
39d0cb0e ILT |
447 | this->add_view(v); |
448 | ||
82dcae9d | 449 | return v; |
bae7f79e ILT |
450 | } |
451 | ||
39d0cb0e ILT |
452 | // Find a View or make a new one, shifted as required by the file |
453 | // offset OFFSET and ALIGNED. | |
454 | ||
455 | File_read::View* | |
456 | File_read::find_or_make_view(off_t offset, off_t start, | |
457 | section_size_type size, bool aligned, bool cache) | |
458 | { | |
459 | unsigned int byteshift; | |
460 | if (offset == 0) | |
461 | byteshift = 0; | |
462 | else | |
463 | { | |
464 | unsigned int target_size = (!parameters->target_valid() | |
465 | ? 64 | |
466 | : parameters->target().get_size()); | |
467 | byteshift = offset & ((target_size / 8) - 1); | |
468 | ||
469 | // Set BYTESHIFT to the number of dummy bytes which must be | |
470 | // inserted before the data in order for this data to be | |
471 | // aligned. | |
472 | if (byteshift != 0) | |
473 | byteshift = (target_size / 8) - byteshift; | |
474 | } | |
475 | ||
476 | // Try to find a View with the required BYTESHIFT. | |
477 | File_read::View* vshifted; | |
478 | File_read::View* v = this->find_view(offset + start, size, | |
479 | aligned ? byteshift : -1U, | |
480 | &vshifted); | |
481 | if (v != NULL) | |
482 | { | |
483 | if (cache) | |
484 | v->set_cache(); | |
485 | return v; | |
486 | } | |
487 | ||
488 | // If VSHIFTED is not NULL, then it has the data we need, but with | |
489 | // the wrong byteshift. | |
490 | v = vshifted; | |
491 | if (v != NULL) | |
492 | { | |
493 | gold_assert(aligned); | |
494 | ||
495 | unsigned char* pbytes = new unsigned char[v->size() + byteshift]; | |
496 | memset(pbytes, 0, byteshift); | |
497 | memcpy(pbytes + byteshift, v->data() + v->byteshift(), v->size()); | |
498 | ||
2c849493 ILT |
499 | File_read::View* shifted_view = |
500 | new File_read::View(v->start(), v->size(), pbytes, byteshift, | |
501 | cache, View::DATA_ALLOCATED_ARRAY); | |
39d0cb0e ILT |
502 | |
503 | this->add_view(shifted_view); | |
504 | return shifted_view; | |
505 | } | |
506 | ||
507 | // Make a new view. If we don't need an aligned view, use a | |
508 | // byteshift of 0, so that we can use mmap. | |
509 | return this->make_view(offset + start, size, | |
510 | aligned ? byteshift : 0, | |
511 | cache); | |
512 | } | |
513 | ||
17a1d0a9 | 514 | // Get a view into the file. |
bae7f79e ILT |
515 | |
516 | const unsigned char* | |
39d0cb0e ILT |
517 | File_read::get_view(off_t offset, off_t start, section_size_type size, |
518 | bool aligned, bool cache) | |
ba45d247 | 519 | { |
39d0cb0e ILT |
520 | File_read::View* pv = this->find_or_make_view(offset, start, size, |
521 | aligned, cache); | |
522 | return pv->data() + (offset + start - pv->start() + pv->byteshift()); | |
bae7f79e ILT |
523 | } |
524 | ||
525 | File_view* | |
39d0cb0e ILT |
526 | File_read::get_lasting_view(off_t offset, off_t start, section_size_type size, |
527 | bool aligned, bool cache) | |
bae7f79e | 528 | { |
39d0cb0e ILT |
529 | File_read::View* pv = this->find_or_make_view(offset, start, size, |
530 | aligned, cache); | |
bae7f79e | 531 | pv->lock(); |
39d0cb0e ILT |
532 | return new File_view(*this, pv, |
533 | (pv->data() | |
534 | + (offset + start - pv->start() + pv->byteshift()))); | |
bae7f79e ILT |
535 | } |
536 | ||
cb295612 ILT |
537 | // Use readv to read COUNT entries from RM starting at START. BASE |
538 | // must be added to all file offsets in RM. | |
539 | ||
540 | void | |
541 | File_read::do_readv(off_t base, const Read_multiple& rm, size_t start, | |
542 | size_t count) | |
543 | { | |
544 | unsigned char discard[File_read::page_size]; | |
545 | iovec iov[File_read::max_readv_entries * 2]; | |
546 | size_t iov_index = 0; | |
547 | ||
548 | off_t first_offset = rm[start].file_offset; | |
549 | off_t last_offset = first_offset; | |
550 | ssize_t want = 0; | |
551 | for (size_t i = 0; i < count; ++i) | |
552 | { | |
553 | const Read_multiple_entry& i_entry(rm[start + i]); | |
554 | ||
555 | if (i_entry.file_offset > last_offset) | |
556 | { | |
557 | size_t skip = i_entry.file_offset - last_offset; | |
558 | gold_assert(skip <= sizeof discard); | |
559 | ||
560 | iov[iov_index].iov_base = discard; | |
561 | iov[iov_index].iov_len = skip; | |
562 | ++iov_index; | |
563 | ||
564 | want += skip; | |
565 | } | |
566 | ||
567 | iov[iov_index].iov_base = i_entry.buffer; | |
568 | iov[iov_index].iov_len = i_entry.size; | |
569 | ++iov_index; | |
570 | ||
571 | want += i_entry.size; | |
572 | ||
573 | last_offset = i_entry.file_offset + i_entry.size; | |
574 | } | |
575 | ||
2a00e4fb ILT |
576 | this->reopen_descriptor(); |
577 | ||
cb295612 ILT |
578 | gold_assert(iov_index < sizeof iov / sizeof iov[0]); |
579 | ||
580 | if (::lseek(this->descriptor_, base + first_offset, SEEK_SET) < 0) | |
581 | gold_fatal(_("%s: lseek failed: %s"), | |
582 | this->filename().c_str(), strerror(errno)); | |
583 | ||
584 | ssize_t got = ::readv(this->descriptor_, iov, iov_index); | |
585 | ||
586 | if (got < 0) | |
587 | gold_fatal(_("%s: readv failed: %s"), | |
588 | this->filename().c_str(), strerror(errno)); | |
589 | if (got != want) | |
590 | gold_fatal(_("%s: file too short: read only %zd of %zd bytes at %lld"), | |
591 | this->filename().c_str(), | |
592 | got, want, static_cast<long long>(base + first_offset)); | |
593 | } | |
594 | ||
595 | // Read several pieces of data from the file. | |
596 | ||
597 | void | |
598 | File_read::read_multiple(off_t base, const Read_multiple& rm) | |
599 | { | |
600 | size_t count = rm.size(); | |
601 | size_t i = 0; | |
602 | while (i < count) | |
603 | { | |
604 | // Find up to MAX_READV_ENTRIES consecutive entries which are | |
605 | // less than one page apart. | |
606 | const Read_multiple_entry& i_entry(rm[i]); | |
607 | off_t i_off = i_entry.file_offset; | |
608 | off_t end_off = i_off + i_entry.size; | |
609 | size_t j; | |
610 | for (j = i + 1; j < count; ++j) | |
611 | { | |
612 | if (j - i >= File_read::max_readv_entries) | |
613 | break; | |
614 | const Read_multiple_entry& j_entry(rm[j]); | |
615 | off_t j_off = j_entry.file_offset; | |
616 | gold_assert(j_off >= end_off); | |
617 | off_t j_end_off = j_off + j_entry.size; | |
618 | if (j_end_off - end_off >= File_read::page_size) | |
619 | break; | |
620 | end_off = j_end_off; | |
621 | } | |
622 | ||
623 | if (j == i + 1) | |
624 | this->read(base + i_off, i_entry.size, i_entry.buffer); | |
625 | else | |
626 | { | |
627 | File_read::View* view = this->find_view(base + i_off, | |
39d0cb0e ILT |
628 | end_off - i_off, |
629 | -1U, NULL); | |
cb295612 ILT |
630 | if (view == NULL) |
631 | this->do_readv(base, rm, i, j - i); | |
632 | else | |
633 | { | |
634 | const unsigned char* v = (view->data() | |
39d0cb0e ILT |
635 | + (base + i_off - view->start() |
636 | + view->byteshift())); | |
cb295612 ILT |
637 | for (size_t k = i; k < j; ++k) |
638 | { | |
639 | const Read_multiple_entry& k_entry(rm[k]); | |
be2f3dec ILT |
640 | gold_assert((convert_to_section_size_type(k_entry.file_offset |
641 | - i_off) | |
642 | + k_entry.size) | |
643 | <= convert_to_section_size_type(end_off | |
644 | - i_off)); | |
cb295612 ILT |
645 | memcpy(k_entry.buffer, |
646 | v + (k_entry.file_offset - i_off), | |
647 | k_entry.size); | |
648 | } | |
649 | } | |
650 | } | |
651 | ||
652 | i = j; | |
653 | } | |
654 | } | |
655 | ||
656 | // Mark all views as no longer cached. | |
657 | ||
658 | void | |
659 | File_read::clear_view_cache_marks() | |
660 | { | |
661 | // Just ignore this if there are multiple objects associated with | |
662 | // the file. Otherwise we will wind up uncaching and freeing some | |
663 | // views for other objects. | |
664 | if (this->object_count_ > 1) | |
665 | return; | |
666 | ||
667 | for (Views::iterator p = this->views_.begin(); | |
668 | p != this->views_.end(); | |
669 | ++p) | |
670 | p->second->clear_cache(); | |
671 | for (Saved_views::iterator p = this->saved_views_.begin(); | |
672 | p != this->saved_views_.end(); | |
673 | ++p) | |
674 | (*p)->clear_cache(); | |
675 | } | |
676 | ||
677 | // Remove all the file views. For a file which has multiple | |
678 | // associated objects (i.e., an archive), we keep accessed views | |
679 | // around until next time, in the hopes that they will be useful for | |
680 | // the next object. | |
bae7f79e ILT |
681 | |
682 | void | |
683 | File_read::clear_views(bool destroying) | |
684 | { | |
fcf29b24 ILT |
685 | Views::iterator p = this->views_.begin(); |
686 | while (p != this->views_.end()) | |
bae7f79e | 687 | { |
cb295612 ILT |
688 | bool should_delete; |
689 | if (p->second->is_locked()) | |
690 | should_delete = false; | |
691 | else if (destroying) | |
692 | should_delete = true; | |
693 | else if (p->second->should_cache()) | |
694 | should_delete = false; | |
695 | else if (this->object_count_ > 1 && p->second->accessed()) | |
696 | should_delete = false; | |
697 | else | |
698 | should_delete = true; | |
699 | ||
700 | if (should_delete) | |
fcf29b24 ILT |
701 | { |
702 | delete p->second; | |
703 | ||
704 | // map::erase invalidates only the iterator to the deleted | |
705 | // element. | |
706 | Views::iterator pe = p; | |
707 | ++p; | |
708 | this->views_.erase(pe); | |
709 | } | |
ead1e424 | 710 | else |
bae7f79e | 711 | { |
a3ad94ed | 712 | gold_assert(!destroying); |
cb295612 | 713 | p->second->clear_accessed(); |
fcf29b24 | 714 | ++p; |
bae7f79e | 715 | } |
ead1e424 | 716 | } |
ead1e424 | 717 | |
fcf29b24 ILT |
718 | Saved_views::iterator q = this->saved_views_.begin(); |
719 | while (q != this->saved_views_.end()) | |
ead1e424 | 720 | { |
cb295612 | 721 | if (!(*q)->is_locked()) |
bae7f79e | 722 | { |
fcf29b24 ILT |
723 | delete *q; |
724 | q = this->saved_views_.erase(q); | |
ead1e424 ILT |
725 | } |
726 | else | |
727 | { | |
a3ad94ed | 728 | gold_assert(!destroying); |
fcf29b24 | 729 | ++q; |
bae7f79e ILT |
730 | } |
731 | } | |
732 | } | |
733 | ||
e44fcf3b ILT |
734 | // Print statistical information to stderr. This is used for --stats. |
735 | ||
736 | void | |
737 | File_read::print_stats() | |
738 | { | |
739 | fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"), | |
740 | program_name, File_read::total_mapped_bytes); | |
741 | fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"), | |
742 | program_name, File_read::maximum_mapped_bytes); | |
743 | } | |
744 | ||
bae7f79e ILT |
745 | // Class File_view. |
746 | ||
747 | File_view::~File_view() | |
748 | { | |
a3ad94ed | 749 | gold_assert(this->file_.is_locked()); |
bae7f79e ILT |
750 | this->view_->unlock(); |
751 | } | |
752 | ||
753 | // Class Input_file. | |
754 | ||
5a6f7e2d ILT |
755 | // Create a file for testing. |
756 | ||
2ea97941 | 757 | Input_file::Input_file(const Task* task, const char* name, |
17a1d0a9 | 758 | const unsigned char* contents, off_t size) |
5a6f7e2d ILT |
759 | : file_() |
760 | { | |
761 | this->input_argument_ = | |
2ea97941 | 762 | new Input_file_argument(name, Input_file_argument::INPUT_FILE_TYPE_FILE, |
ae3b5189 | 763 | "", false, Position_dependent_options()); |
2ea97941 | 764 | bool ok = this->file_.open(task, name, contents, size); |
5a6f7e2d ILT |
765 | gold_assert(ok); |
766 | } | |
767 | ||
14144f39 ILT |
768 | // Return the position dependent options in force for this file. |
769 | ||
770 | const Position_dependent_options& | |
771 | Input_file::options() const | |
772 | { | |
773 | return this->input_argument_->options(); | |
774 | } | |
775 | ||
776 | // Return the name given by the user. For -lc this will return "c". | |
777 | ||
778 | const char* | |
779 | Input_file::name() const | |
88dd47ac ILT |
780 | { |
781 | return this->input_argument_->name(); | |
782 | } | |
783 | ||
fd9d194f ILT |
784 | // Return whether this file is in a system directory. |
785 | ||
786 | bool | |
787 | Input_file::is_in_system_directory() const | |
788 | { | |
789 | if (this->is_in_sysroot()) | |
790 | return true; | |
791 | return parameters->options().is_in_system_directory(this->filename()); | |
792 | } | |
793 | ||
88dd47ac ILT |
794 | // Return whether we are only reading symbols. |
795 | ||
796 | bool | |
797 | Input_file::just_symbols() const | |
798 | { | |
799 | return this->input_argument_->just_symbols(); | |
800 | } | |
14144f39 | 801 | |
15f8229b ILT |
802 | // Return whether this is a file that we will search for in the list |
803 | // of directories. | |
804 | ||
805 | bool | |
806 | Input_file::will_search_for() const | |
807 | { | |
808 | return (!IS_ABSOLUTE_PATH(this->input_argument_->name()) | |
809 | && (this->input_argument_->is_lib() | |
ae3b5189 | 810 | || this->input_argument_->is_searched_file() |
15f8229b ILT |
811 | || this->input_argument_->extra_search_path() != NULL)); |
812 | } | |
813 | ||
98fa85cb ILT |
814 | // Return the file last modification time. Calls gold_fatal if the stat |
815 | // system call failed. | |
816 | ||
817 | Timespec | |
818 | File_read::get_mtime() | |
819 | { | |
820 | struct stat file_stat; | |
821 | this->reopen_descriptor(); | |
822 | ||
823 | if (fstat(this->descriptor_, &file_stat) < 0) | |
824 | gold_fatal(_("%s: stat failed: %s"), this->name_.c_str(), | |
825 | strerror(errno)); | |
5d329b7d ILT |
826 | #ifdef HAVE_STAT_ST_MTIM |
827 | return Timespec(file_stat.st_mtim.tv_sec, file_stat.st_mtim.tv_nsec); | |
828 | #else | |
98fa85cb | 829 | return Timespec(file_stat.st_mtime, 0); |
5d329b7d | 830 | #endif |
98fa85cb ILT |
831 | } |
832 | ||
5a6f7e2d ILT |
833 | // Open the file. |
834 | ||
51dee2fe ILT |
835 | // If the filename is not absolute, we assume it is in the current |
836 | // directory *except* when: | |
ae3b5189 CD |
837 | // A) input_argument_->is_lib() is true; |
838 | // B) input_argument_->is_searched_file() is true; or | |
839 | // C) input_argument_->extra_search_path() is not empty. | |
840 | // In each, we look in extra_search_path + library_path to find | |
51dee2fe ILT |
841 | // the file location, rather than the current directory. |
842 | ||
75f2446e | 843 | bool |
15f8229b | 844 | Input_file::open(const Dirsearch& dirpath, const Task* task, int *pindex) |
bae7f79e | 845 | { |
2ea97941 | 846 | std::string name; |
51dee2fe ILT |
847 | |
848 | // Case 1: name is an absolute file, just try to open it | |
ae3b5189 CD |
849 | // Case 2: name is relative but is_lib is false, is_searched_file is false, |
850 | // and extra_search_path is empty | |
15f8229b | 851 | if (IS_ABSOLUTE_PATH(this->input_argument_->name()) |
51dee2fe | 852 | || (!this->input_argument_->is_lib() |
ae3b5189 | 853 | && !this->input_argument_->is_searched_file() |
51dee2fe | 854 | && this->input_argument_->extra_search_path() == NULL)) |
e2aacd2c | 855 | { |
2ea97941 ILT |
856 | name = this->input_argument_->name(); |
857 | this->found_name_ = name; | |
e2aacd2c | 858 | } |
ae3b5189 CD |
859 | // Case 3: is_lib is true or is_searched_file is true |
860 | else if (this->input_argument_->is_lib() | |
861 | || this->input_argument_->is_searched_file()) | |
bae7f79e | 862 | { |
51dee2fe ILT |
863 | // We don't yet support extra_search_path with -l. |
864 | gold_assert(this->input_argument_->extra_search_path() == NULL); | |
ae3b5189 CD |
865 | std::string n1, n2; |
866 | if (this->input_argument_->is_lib()) | |
f6ce93d6 | 867 | { |
ae3b5189 CD |
868 | n1 = "lib"; |
869 | n1 += this->input_argument_->name(); | |
870 | if (parameters->options().is_static() | |
871 | || !this->input_argument_->options().Bdynamic()) | |
872 | n1 += ".a"; | |
873 | else | |
874 | { | |
875 | n2 = n1 + ".a"; | |
876 | n1 += ".so"; | |
877 | } | |
f6ce93d6 | 878 | } |
ae3b5189 CD |
879 | else |
880 | n1 = this->input_argument_->name(); | |
2ea97941 ILT |
881 | name = dirpath.find(n1, n2, &this->is_in_sysroot_, pindex); |
882 | if (name.empty()) | |
bae7f79e | 883 | { |
ae3b5189 CD |
884 | gold_error(_("cannot find %s%s"), |
885 | this->input_argument_->is_lib() ? "-l" : "", | |
75f2446e ILT |
886 | this->input_argument_->name()); |
887 | return false; | |
bae7f79e | 888 | } |
2ea97941 | 889 | if (n2.empty() || name[name.length() - 1] == 'o') |
e2aacd2c ILT |
890 | this->found_name_ = n1; |
891 | else | |
892 | this->found_name_ = n2; | |
bae7f79e | 893 | } |
51dee2fe ILT |
894 | // Case 4: extra_search_path is not empty |
895 | else | |
896 | { | |
897 | gold_assert(this->input_argument_->extra_search_path() != NULL); | |
898 | ||
899 | // First, check extra_search_path. | |
2ea97941 ILT |
900 | name = this->input_argument_->extra_search_path(); |
901 | if (!IS_DIR_SEPARATOR (name[name.length() - 1])) | |
902 | name += '/'; | |
903 | name += this->input_argument_->name(); | |
51dee2fe | 904 | struct stat dummy_stat; |
2ea97941 | 905 | if (*pindex > 0 || ::stat(name.c_str(), &dummy_stat) < 0) |
51dee2fe ILT |
906 | { |
907 | // extra_search_path failed, so check the normal search-path. | |
15f8229b ILT |
908 | int index = *pindex; |
909 | if (index > 0) | |
910 | --index; | |
2ea97941 ILT |
911 | name = dirpath.find(this->input_argument_->name(), "", |
912 | &this->is_in_sysroot_, &index); | |
913 | if (name.empty()) | |
51dee2fe | 914 | { |
a0c4fb0a | 915 | gold_error(_("cannot find %s"), |
75f2446e ILT |
916 | this->input_argument_->name()); |
917 | return false; | |
51dee2fe | 918 | } |
15f8229b | 919 | *pindex = index + 1; |
51dee2fe | 920 | } |
e2aacd2c | 921 | this->found_name_ = this->input_argument_->name(); |
51dee2fe ILT |
922 | } |
923 | ||
924 | // Now that we've figured out where the file lives, try to open it. | |
bc644c6c ILT |
925 | |
926 | General_options::Object_format format = | |
7cc619c3 | 927 | this->input_argument_->options().format_enum(); |
bc644c6c ILT |
928 | bool ok; |
929 | if (format == General_options::OBJECT_FORMAT_ELF) | |
2ea97941 | 930 | ok = this->file_.open(task, name); |
bc644c6c ILT |
931 | else |
932 | { | |
933 | gold_assert(format == General_options::OBJECT_FORMAT_BINARY); | |
2ea97941 | 934 | ok = this->open_binary(task, name); |
bc644c6c ILT |
935 | } |
936 | ||
937 | if (!ok) | |
bae7f79e | 938 | { |
a0c4fb0a | 939 | gold_error(_("cannot open %s: %s"), |
2ea97941 | 940 | name.c_str(), strerror(errno)); |
75f2446e | 941 | return false; |
bae7f79e | 942 | } |
75f2446e ILT |
943 | |
944 | return true; | |
bae7f79e ILT |
945 | } |
946 | ||
bc644c6c ILT |
947 | // Open a file for --format binary. |
948 | ||
949 | bool | |
2ea97941 | 950 | Input_file::open_binary(const Task* task, const std::string& name) |
bc644c6c ILT |
951 | { |
952 | // In order to open a binary file, we need machine code, size, and | |
0daa6f62 ILT |
953 | // endianness. We may not have a valid target at this point, in |
954 | // which case we use the default target. | |
029ba973 ILT |
955 | parameters_force_valid_target(); |
956 | const Target& target(parameters->target()); | |
bc644c6c | 957 | |
029ba973 ILT |
958 | Binary_to_elf binary_to_elf(target.machine_code(), |
959 | target.get_size(), | |
960 | target.is_big_endian(), | |
2ea97941 | 961 | name); |
bc644c6c ILT |
962 | if (!binary_to_elf.convert(task)) |
963 | return false; | |
2ea97941 | 964 | return this->file_.open(task, name, binary_to_elf.converted_data_leak(), |
bc644c6c ILT |
965 | binary_to_elf.converted_size()); |
966 | } | |
967 | ||
bae7f79e | 968 | } // End namespace gold. |