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