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