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