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