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