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