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