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