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