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