1 // fileread.cc -- read files for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
37 #include "filenames.h"
40 #include "parameters.h"
42 #include "dirsearch.h"
45 #include "descriptors.h"
46 #include "gold-threads.h"
50 struct iovec
{ void* iov_base
; size_t iov_len
; };
52 readv(int, const iovec
*, int)
61 // Get the last modified time of an unopened file.
64 get_mtime(const char* filename
, Timespec
* mtime
)
66 struct stat file_stat
;
68 if (stat(filename
, &file_stat
) < 0)
70 #ifdef HAVE_STAT_ST_MTIM
71 mtime
->seconds
= file_stat
.st_mtim
.tv_sec
;
72 mtime
->nanoseconds
= file_stat
.st_mtim
.tv_nsec
;
74 mtime
->seconds
= file_stat
.st_mtime
;
75 mtime
->nanoseconds
= 0;
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
);
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
;
91 // Class File_read::View.
93 File_read::View::~View()
95 gold_assert(!this->is_locked());
96 switch (this->data_ownership_
)
98 case DATA_ALLOCATED_ARRAY
:
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())
106 file_counts_initialize_lock
.initialize();
107 Hold_optional_lock
hl(file_counts_lock
);
108 File_read::current_mapped_bytes
-= this->size_
;
119 File_read::View::lock()
125 File_read::View::unlock()
127 gold_assert(this->lock_count_
> 0);
132 File_read::View::is_locked()
134 return this->lock_count_
> 0;
139 File_read::~File_read()
141 gold_assert(this->token_
.is_writable());
142 if (this->is_descriptor_opened_
)
144 release_descriptor(this->descriptor_
, true);
145 this->descriptor_
= -1;
146 this->is_descriptor_opened_
= false;
149 this->clear_views(CLEAR_VIEWS_ALL
);
155 File_read::open(const Task
* task
, const std::string
& name
)
157 gold_assert(this->token_
.is_writable()
158 && this->descriptor_
< 0
159 && !this->is_descriptor_opened_
160 && this->name_
.empty());
163 this->descriptor_
= open_descriptor(-1, this->name_
.c_str(),
166 if (this->descriptor_
>= 0)
168 this->is_descriptor_opened_
= true;
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
);
179 return this->descriptor_
>= 0;
182 // Open the file with the contents in memory.
185 File_read::open(const Task
* task
, const std::string
& name
,
186 const unsigned char* contents
, off_t size
)
188 gold_assert(this->token_
.is_writable()
189 && this->descriptor_
< 0
190 && !this->is_descriptor_opened_
191 && this->name_
.empty());
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_
);
197 this->token_
.add_writer(task
);
201 // Reopen a descriptor if necessary.
204 File_read::reopen_descriptor()
206 if (!this->is_descriptor_opened_
)
208 this->descriptor_
= open_descriptor(this->descriptor_
,
211 if (this->descriptor_
< 0)
212 gold_fatal(_("could not reopen file %s"), this->name_
.c_str());
213 this->is_descriptor_opened_
= true;
217 // Release the file. This is called when we are done with the file in
223 gold_assert(this->is_locked());
225 if (!parameters
->options_valid() || parameters
->options().stats())
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
;
235 this->mapped_bytes_
= 0;
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)
242 this->clear_views(CLEAR_VIEWS_NORMAL
);
243 if (this->is_descriptor_opened_
)
245 release_descriptor(this->descriptor_
, false);
246 this->is_descriptor_opened_
= false;
250 this->released_
= true;
256 File_read::lock(const Task
* task
)
258 gold_assert(this->released_
);
259 this->token_
.add_writer(task
);
260 this->released_
= false;
266 File_read::unlock(const Task
* task
)
269 this->token_
.remove_writer(task
);
272 // Return whether the file is locked.
275 File_read::is_locked() const
277 if (!this->token_
.is_writable())
279 // The file is not locked, so it should have been released.
280 gold_assert(this->released_
);
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.
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
295 if (vshifted
!= NULL
)
298 // If we have the whole file mmapped, and the alignment is right,
300 if (this->whole_file_view_
)
301 if (byteshift
== -1U || byteshift
== 0)
302 return this->whole_file_view_
;
304 off_t page
= File_read::page_offset(start
);
306 unsigned int bszero
= 0;
307 Views::const_iterator p
= this->views_
.upper_bound(std::make_pair(page
- 1,
310 while (p
!= this->views_
.end() && p
->first
.first
<= page
)
312 if (p
->second
->start() <= start
313 && (p
->second
->start() + static_cast<off_t
>(p
->second
->size())
314 >= start
+ static_cast<off_t
>(size
)))
316 if (byteshift
== -1U || byteshift
== p
->second
->byteshift())
318 p
->second
->set_accessed();
322 if (vshifted
!= NULL
&& *vshifted
== NULL
)
323 *vshifted
= p
->second
;
332 // Read SIZE bytes from the file starting at offset START. Read into
336 File_read::do_read(off_t start
, section_size_type size
, void* p
)
339 if (this->whole_file_view_
!= NULL
)
341 bytes
= this->size_
- start
;
342 if (static_cast<section_size_type
>(bytes
) >= size
)
344 memcpy(p
, this->whole_file_view_
->data() + start
, size
);
350 this->reopen_descriptor();
351 bytes
= ::pread(this->descriptor_
, p
, size
, start
);
352 if (static_cast<section_size_type
>(bytes
) == size
)
357 gold_fatal(_("%s: pread failed: %s"),
358 this->filename().c_str(), strerror(errno
));
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
));
370 // Read data from the file.
373 File_read::read(off_t start
, section_size_type size
, void* p
)
375 const File_read::View
* pv
= this->find_view(start
, size
, -1U, NULL
);
378 memcpy(p
, pv
->data() + (start
- pv
->start() + pv
->byteshift()), size
);
382 this->do_read(start
, size
, p
);
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.
390 File_read::add_view(File_read::View
* v
)
392 std::pair
<Views::iterator
, bool> ins
=
393 this->views_
.insert(std::make_pair(std::make_pair(v
->start(),
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())
409 this->saved_views_
.push_back(vold
);
411 ins
.first
->second
= v
;
414 // Make a new view with a specified byteshift, reading the data from
418 File_read::make_view(off_t start
, section_size_type size
,
419 unsigned int byteshift
, bool cache
)
421 gold_assert(size
> 0);
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
));
433 off_t poff
= File_read::page_offset(start
);
435 section_size_type psize
= File_read::pages(size
+ (start
- poff
));
437 if (poff
+ static_cast<off_t
>(psize
) >= this->size_
)
439 psize
= this->size_
- poff
;
440 gold_assert(psize
>= size
);
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
);
454 this->reopen_descriptor();
455 void* p
= ::mmap(NULL
, psize
, PROT_READ
, MAP_PRIVATE
,
456 this->descriptor_
, poff
);
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
),
464 this->mapped_bytes_
+= psize
;
466 const unsigned char* pbytes
= static_cast<const unsigned char*>(p
);
467 v
= new File_read::View(poff
, psize
, pbytes
, 0, cache
,
476 // Find a View or make a new one, shifted as required by the file
477 // offset OFFSET and ALIGNED.
480 File_read::find_or_make_view(off_t offset
, off_t start
,
481 section_size_type size
, bool aligned
, bool cache
)
483 unsigned int byteshift
;
488 unsigned int target_size
= (!parameters
->target_valid()
490 : parameters
->target().get_size());
491 byteshift
= offset
& ((target_size
/ 8) - 1);
493 // Set BYTESHIFT to the number of dummy bytes which must be
494 // inserted before the data in order for this data to be
497 byteshift
= (target_size
/ 8) - byteshift
;
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
);
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,
521 // If VSHIFTED is not NULL, then it has the data we need, but with
522 // the wrong byteshift.
526 gold_assert(aligned
);
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());
532 File_read::View
* shifted_view
=
533 new File_read::View(v
->start(), v
->size(), pbytes
, byteshift
,
534 cache
, View::DATA_ALLOCATED_ARRAY
);
536 this->add_view(shifted_view
);
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,
547 // Get a view into the file.
550 File_read::get_view(off_t offset
, off_t start
, section_size_type size
,
551 bool aligned
, bool cache
)
553 File_read::View
* pv
= this->find_or_make_view(offset
, start
, size
,
555 return pv
->data() + (offset
+ start
- pv
->start() + pv
->byteshift());
559 File_read::get_lasting_view(off_t offset
, off_t start
, section_size_type size
,
560 bool aligned
, bool cache
)
562 File_read::View
* pv
= this->find_or_make_view(offset
, start
, size
,
565 return new File_view(*this, pv
,
567 + (offset
+ start
- pv
->start() + pv
->byteshift())));
570 // Use readv to read COUNT entries from RM starting at START. BASE
571 // must be added to all file offsets in RM.
574 File_read::do_readv(off_t base
, const Read_multiple
& rm
, size_t start
,
577 unsigned char discard
[File_read::page_size
];
578 iovec iov
[File_read::max_readv_entries
* 2];
579 size_t iov_index
= 0;
581 off_t first_offset
= rm
[start
].file_offset
;
582 off_t last_offset
= first_offset
;
584 for (size_t i
= 0; i
< count
; ++i
)
586 const Read_multiple_entry
& i_entry(rm
[start
+ i
]);
588 if (i_entry
.file_offset
> last_offset
)
590 size_t skip
= i_entry
.file_offset
- last_offset
;
591 gold_assert(skip
<= sizeof discard
);
593 iov
[iov_index
].iov_base
= discard
;
594 iov
[iov_index
].iov_len
= skip
;
600 iov
[iov_index
].iov_base
= i_entry
.buffer
;
601 iov
[iov_index
].iov_len
= i_entry
.size
;
604 want
+= i_entry
.size
;
606 last_offset
= i_entry
.file_offset
+ i_entry
.size
;
609 this->reopen_descriptor();
611 gold_assert(iov_index
< sizeof iov
/ sizeof iov
[0]);
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
));
617 ssize_t got
= ::readv(this->descriptor_
, iov
, iov_index
);
620 gold_fatal(_("%s: readv failed: %s"),
621 this->filename().c_str(), strerror(errno
));
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
));
630 #if !defined(HAVE_READV)
631 #define GOLD_IOV_MAX 1
632 #elif defined(IOV_MAX)
633 #define GOLD_IOV_MAX IOV_MAX
635 #define GOLD_IOV_MAX (File_read::max_readv_entries * 2)
638 // Read several pieces of data from the file.
641 File_read::read_multiple(off_t base
, const Read_multiple
& rm
)
643 static size_t iov_max
= GOLD_IOV_MAX
;
644 size_t count
= rm
.size();
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
;
654 for (j
= i
+ 1; j
< count
; ++j
)
656 if (j
- i
>= File_read::max_readv_entries
|| j
- i
>= iov_max
/ 2)
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
)
668 this->read(base
+ i_off
, i_entry
.size
, i_entry
.buffer
);
671 File_read::View
* view
= this->find_view(base
+ i_off
,
675 this->do_readv(base
, rm
, i
, j
- i
);
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
)
683 const Read_multiple_entry
& k_entry(rm
[k
]);
684 gold_assert((convert_to_section_size_type(k_entry
.file_offset
687 <= convert_to_section_size_type(end_off
689 memcpy(k_entry
.buffer
,
690 v
+ (k_entry
.file_offset
- i_off
),
700 // Mark all views as no longer cached.
703 File_read::clear_view_cache_marks()
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)
711 for (Views::iterator p
= this->views_
.begin();
712 p
!= this->views_
.end();
714 p
->second
->clear_cache();
715 for (Saved_views::iterator p
= this->saved_views_
.begin();
716 p
!= this->saved_views_
.end();
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
727 File_read::clear_views(Clear_views_mode mode
)
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())
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;
748 should_delete
= true;
752 if (p
->second
== this->whole_file_view_
)
753 this->whole_file_view_
= NULL
;
756 // map::erase invalidates only the iterator to the deleted
758 Views::iterator pe
= p
;
760 this->views_
.erase(pe
);
764 p
->second
->clear_accessed();
769 Saved_views::iterator q
= this->saved_views_
.begin();
770 while (q
!= this->saved_views_
.end())
772 if (!(*q
)->is_locked())
775 q
= this->saved_views_
.erase(q
);
779 gold_assert(mode
!= CLEAR_VIEWS_ALL
);
785 // Print statistical information to stderr. This is used for --stats.
788 File_read::print_stats()
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
);
798 File_view::~File_view()
800 gold_assert(this->file_
.is_locked());
801 this->view_
->unlock();
806 // Create a file for testing.
808 Input_file::Input_file(const Task
* task
, const char* name
,
809 const unsigned char* contents
, off_t size
)
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
);
819 // Return the position dependent options in force for this file.
821 const Position_dependent_options
&
822 Input_file::options() const
824 return this->input_argument_
->options();
827 // Return the name given by the user. For -lc this will return "c".
830 Input_file::name() const
832 return this->input_argument_
->name();
835 // Return whether this file is in a system directory.
838 Input_file::is_in_system_directory() const
840 if (this->is_in_sysroot())
842 return parameters
->options().is_in_system_directory(this->filename());
845 // Return whether we are only reading symbols.
848 Input_file::just_symbols() const
850 return this->input_argument_
->just_symbols();
853 // Return whether this is a file that we will search for in the list
857 Input_file::will_search_for() const
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
));
865 // Return the file last modification time. Calls gold_fatal if the stat
866 // system call failed.
869 File_read::get_mtime()
871 struct stat file_stat
;
872 this->reopen_descriptor();
874 if (fstat(this->descriptor_
, &file_stat
) < 0)
875 gold_fatal(_("%s: stat failed: %s"), this->name_
.c_str(),
877 #ifdef HAVE_STAT_ST_MTIM
878 return Timespec(file_stat
.st_mtim
.tv_sec
, file_stat
.st_mtim
.tv_nsec
);
880 return Timespec(file_stat
.st_mtime
, 0);
884 // Try to find a file in the extra search dirs. Returns true on success.
887 Input_file::try_extra_search_path(int* pindex
,
888 const Input_file_argument
* input_argument
,
889 std::string filename
, std::string
* found_name
,
892 if (input_argument
->extra_search_path() == NULL
)
895 std::string name
= input_argument
->extra_search_path();
896 if (!IS_DIR_SEPARATOR(name
[name
.length() - 1]))
900 struct stat dummy_stat
;
901 if (*pindex
> 0 || ::stat(name
.c_str(), &dummy_stat
) < 0)
904 *found_name
= filename
;
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.
919 Input_file::find_file(const Dirsearch
& dirpath
, int* pindex
,
920 const Input_file_argument
* input_argument
,
922 std::string
* found_name
, std::string
* namep
)
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
))
934 name
= input_argument
->name();
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())
944 if (input_argument
->is_lib())
947 n1
+= input_argument
->name();
948 if (parameters
->options().is_static()
949 || !input_argument
->options().Bdynamic())
958 n1
= input_argument
->name();
960 if (Input_file::try_extra_search_path(pindex
, input_argument
, n1
,
964 if (!n2
.empty() && Input_file::try_extra_search_path(pindex
,
969 // It is not in the extra_search_path.
970 name
= dirpath
.find(n1
, n2
, is_in_sysroot
, pindex
);
973 gold_error(_("cannot find %s%s"),
974 input_argument
->is_lib() ? "-l" : "",
975 input_argument
->name());
978 if (n2
.empty() || name
[name
.length() - 1] == 'o')
985 // Case 4: extra_search_path is not empty
988 gold_assert(input_argument
->extra_search_path() != NULL
);
990 if (try_extra_search_path(pindex
, input_argument
, input_argument
->name(),
994 // extra_search_path failed, so check the normal search-path.
998 name
= dirpath
.find(input_argument
->name(), "",
999 is_in_sysroot
, &index
);
1002 gold_error(_("cannot find %s"),
1003 input_argument
->name());
1006 *found_name
= input_argument
->name();
1008 *pindex
= index
+ 1;
1016 Input_file::open(const Dirsearch
& dirpath
, const Task
* task
, int* pindex
)
1019 if (!Input_file::find_file(dirpath
, pindex
, this->input_argument_
,
1020 &this->is_in_sysroot_
, &this->found_name_
, &name
))
1023 // Now that we've figured out where the file lives, try to open it.
1025 General_options::Object_format format
=
1026 this->input_argument_
->options().format_enum();
1028 if (format
== General_options::OBJECT_FORMAT_ELF
)
1030 ok
= this->file_
.open(task
, name
);
1031 this->format_
= FORMAT_ELF
;
1035 gold_assert(format
== General_options::OBJECT_FORMAT_BINARY
);
1036 ok
= this->open_binary(task
, name
);
1037 this->format_
= FORMAT_BINARY
;
1042 gold_error(_("cannot open %s: %s"),
1043 name
.c_str(), strerror(errno
));
1044 this->format_
= FORMAT_NONE
;
1051 // Open a file for --format binary.
1054 Input_file::open_binary(const Task
* task
, const std::string
& name
)
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());
1062 Binary_to_elf
binary_to_elf(target
.machine_code(),
1064 target
.is_big_endian(),
1066 if (!binary_to_elf
.convert(task
))
1068 return this->file_
.open(task
, name
, binary_to_elf
.converted_data_leak(),
1069 binary_to_elf
.converted_size());
1072 } // End namespace gold.