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