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