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