1 // fileread.cc -- read files for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
32 #ifdef HAVE_SYS_MMAN_H
41 #include "filenames.h"
44 #include "parameters.h"
46 #include "dirsearch.h"
49 #include "descriptors.h"
50 #include "gold-threads.h"
53 // For systems without mmap support.
55 # define mmap gold_mmap
56 # define munmap gold_munmap
58 # define MAP_FAILED (reinterpret_cast<void*>(-1))
64 # define MAP_PRIVATE 0
68 # define ENOSYS EINVAL
72 gold_mmap(void *, size_t, int, int, int, off_t
)
79 gold_munmap(void *, size_t)
88 struct iovec
{ void* iov_base
; size_t iov_len
; };
90 readv(int, const iovec
*, int)
99 // Get the last modified time of an unopened file.
102 get_mtime(const char* filename
, Timespec
* mtime
)
104 struct stat file_stat
;
106 if (stat(filename
, &file_stat
) < 0)
108 #ifdef HAVE_STAT_ST_MTIM
109 mtime
->seconds
= file_stat
.st_mtim
.tv_sec
;
110 mtime
->nanoseconds
= file_stat
.st_mtim
.tv_nsec
;
112 mtime
->seconds
= file_stat
.st_mtime
;
113 mtime
->nanoseconds
= 0;
120 // A lock for the File_read static variables.
121 static Lock
* file_counts_lock
= NULL
;
122 static Initialize_lock
file_counts_initialize_lock(&file_counts_lock
);
124 // The File_read static variables.
125 unsigned long long File_read::total_mapped_bytes
;
126 unsigned long long File_read::current_mapped_bytes
;
127 unsigned long long File_read::maximum_mapped_bytes
;
129 // Class File_read::View.
131 File_read::View::~View()
133 gold_assert(!this->is_locked());
134 switch (this->data_ownership_
)
136 case DATA_ALLOCATED_ARRAY
:
137 free(const_cast<unsigned char*>(this->data_
));
140 if (::munmap(const_cast<unsigned char*>(this->data_
), this->size_
) != 0)
141 gold_warning(_("munmap failed: %s"), strerror(errno
));
142 if (!parameters
->options_valid() || parameters
->options().stats())
144 file_counts_initialize_lock
.initialize();
145 Hold_optional_lock
hl(file_counts_lock
);
146 File_read::current_mapped_bytes
-= this->size_
;
157 File_read::View::lock()
163 File_read::View::unlock()
165 gold_assert(this->lock_count_
> 0);
170 File_read::View::is_locked()
172 return this->lock_count_
> 0;
177 File_read::~File_read()
179 gold_assert(this->token_
.is_writable());
180 if (this->is_descriptor_opened_
)
182 release_descriptor(this->descriptor_
, true);
183 this->descriptor_
= -1;
184 this->is_descriptor_opened_
= false;
187 this->clear_views(CLEAR_VIEWS_ALL
);
193 File_read::open(const Task
* task
, const std::string
& name
)
195 gold_assert(this->token_
.is_writable()
196 && this->descriptor_
< 0
197 && !this->is_descriptor_opened_
198 && this->name_
.empty());
201 this->descriptor_
= open_descriptor(-1, this->name_
.c_str(),
204 if (this->descriptor_
>= 0)
206 this->is_descriptor_opened_
= true;
208 if (::fstat(this->descriptor_
, &s
) < 0)
209 gold_error(_("%s: fstat failed: %s"),
210 this->name_
.c_str(), strerror(errno
));
211 this->size_
= s
.st_size
;
212 gold_debug(DEBUG_FILES
, "Attempt to open %s succeeded",
213 this->name_
.c_str());
214 this->token_
.add_writer(task
);
217 return this->descriptor_
>= 0;
220 // Open the file with the contents in memory.
223 File_read::open(const Task
* task
, const std::string
& name
,
224 const unsigned char* contents
, off_t size
)
226 gold_assert(this->token_
.is_writable()
227 && this->descriptor_
< 0
228 && !this->is_descriptor_opened_
229 && this->name_
.empty());
231 this->whole_file_view_
= new View(0, size
, contents
, 0, false,
232 View::DATA_NOT_OWNED
);
233 this->add_view(this->whole_file_view_
);
235 this->token_
.add_writer(task
);
239 // Reopen a descriptor if necessary.
242 File_read::reopen_descriptor()
244 if (!this->is_descriptor_opened_
)
246 this->descriptor_
= open_descriptor(this->descriptor_
,
249 if (this->descriptor_
< 0)
250 gold_fatal(_("could not reopen file %s"), this->name_
.c_str());
251 this->is_descriptor_opened_
= true;
255 // Release the file. This is called when we are done with the file in
261 gold_assert(this->is_locked());
263 if (!parameters
->options_valid() || parameters
->options().stats())
265 file_counts_initialize_lock
.initialize();
266 Hold_optional_lock
hl(file_counts_lock
);
267 File_read::total_mapped_bytes
+= this->mapped_bytes_
;
268 File_read::current_mapped_bytes
+= this->mapped_bytes_
;
269 if (File_read::current_mapped_bytes
> File_read::maximum_mapped_bytes
)
270 File_read::maximum_mapped_bytes
= File_read::current_mapped_bytes
;
273 this->mapped_bytes_
= 0;
275 // Only clear views if there is only one attached object. Otherwise
276 // we waste time trying to clear cached archive views. Similarly
277 // for releasing the descriptor.
278 if (this->object_count_
<= 1)
280 this->clear_views(CLEAR_VIEWS_NORMAL
);
281 if (this->is_descriptor_opened_
)
283 release_descriptor(this->descriptor_
, false);
284 this->is_descriptor_opened_
= false;
288 this->released_
= true;
294 File_read::lock(const Task
* task
)
296 gold_assert(this->released_
);
297 this->token_
.add_writer(task
);
298 this->released_
= false;
304 File_read::unlock(const Task
* task
)
307 this->token_
.remove_writer(task
);
310 // Return whether the file is locked.
313 File_read::is_locked() const
315 if (!this->token_
.is_writable())
317 // The file is not locked, so it should have been released.
318 gold_assert(this->released_
);
322 // See if we have a view which covers the file starting at START for
323 // SIZE bytes. Return a pointer to the View if found, NULL if not.
324 // If BYTESHIFT is not -1U, the returned View must have the specified
325 // byte shift; otherwise, it may have any byte shift. If VSHIFTED is
326 // not NULL, this sets *VSHIFTED to a view which would have worked if
327 // not for the requested BYTESHIFT.
329 inline File_read::View
*
330 File_read::find_view(off_t start
, section_size_type size
,
331 unsigned int byteshift
, File_read::View
** vshifted
) const
333 gold_assert(start
<= this->size_
334 && (static_cast<unsigned long long>(size
)
335 <= static_cast<unsigned long long>(this->size_
- start
)));
337 if (vshifted
!= NULL
)
340 // If we have the whole file mmapped, and the alignment is right,
342 if (this->whole_file_view_
)
343 if (byteshift
== -1U || byteshift
== 0)
344 return this->whole_file_view_
;
346 off_t page
= File_read::page_offset(start
);
348 unsigned int bszero
= 0;
349 Views::const_iterator p
= this->views_
.upper_bound(std::make_pair(page
- 1,
352 while (p
!= this->views_
.end() && p
->first
.first
<= page
)
354 if (p
->second
->start() <= start
355 && (p
->second
->start() + static_cast<off_t
>(p
->second
->size())
356 >= start
+ static_cast<off_t
>(size
)))
358 if (byteshift
== -1U || byteshift
== p
->second
->byteshift())
360 p
->second
->set_accessed();
364 if (vshifted
!= NULL
&& *vshifted
== NULL
)
365 *vshifted
= p
->second
;
374 // Read SIZE bytes from the file starting at offset START. Read into
378 File_read::do_read(off_t start
, section_size_type size
, void* p
)
381 if (this->whole_file_view_
!= NULL
)
383 bytes
= this->size_
- start
;
384 if (static_cast<section_size_type
>(bytes
) >= size
)
386 memcpy(p
, this->whole_file_view_
->data() + start
, size
);
392 this->reopen_descriptor();
394 char *read_ptr
= static_cast<char *>(p
);
395 off_t read_pos
= start
;
396 size_t to_read
= size
;
399 bytes
= ::pread(this->descriptor_
, read_ptr
, to_read
, read_pos
);
401 gold_fatal(_("%s: pread failed: %s"),
402 this->filename().c_str(), strerror(errno
));
412 bytes
= size
- to_read
;
415 gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"),
416 this->filename().c_str(),
417 static_cast<long long>(bytes
),
418 static_cast<long long>(size
),
419 static_cast<long long>(start
));
422 // Read data from the file.
425 File_read::read(off_t start
, section_size_type size
, void* p
)
427 const File_read::View
* pv
= this->find_view(start
, size
, -1U, NULL
);
430 memcpy(p
, pv
->data() + (start
- pv
->start() + pv
->byteshift()), size
);
434 this->do_read(start
, size
, p
);
437 // Add a new view. There may already be an existing view at this
438 // offset. If there is, the new view will be larger, and should
439 // replace the old view.
442 File_read::add_view(File_read::View
* v
)
444 std::pair
<Views::iterator
, bool> ins
=
445 this->views_
.insert(std::make_pair(std::make_pair(v
->start(),
451 // There was an existing view at this offset. It must not be large
452 // enough. We can't delete it here, since something might be using
453 // it; we put it on a list to be deleted when the file is unlocked.
454 File_read::View
* vold
= ins
.first
->second
;
455 gold_assert(vold
->size() < v
->size());
456 if (vold
->should_cache())
461 this->saved_views_
.push_back(vold
);
463 ins
.first
->second
= v
;
466 // Make a new view with a specified byteshift, reading the data from
470 File_read::make_view(off_t start
, section_size_type size
,
471 unsigned int byteshift
, bool cache
)
473 gold_assert(size
> 0);
474 gold_assert(start
<= this->size_
475 && (static_cast<unsigned long long>(size
)
476 <= static_cast<unsigned long long>(this->size_
- start
)));
478 off_t poff
= File_read::page_offset(start
);
480 section_size_type psize
= File_read::pages(size
+ (start
- poff
));
482 if (poff
+ static_cast<off_t
>(psize
) >= this->size_
)
484 psize
= this->size_
- poff
;
485 gold_assert(psize
>= size
);
489 View::Data_ownership ownership
;
492 p
= malloc(psize
+ byteshift
);
495 memset(p
, 0, byteshift
);
496 this->do_read(poff
, psize
, static_cast<unsigned char*>(p
) + byteshift
);
497 ownership
= View::DATA_ALLOCATED_ARRAY
;
501 this->reopen_descriptor();
502 p
= ::mmap(NULL
, psize
, PROT_READ
, MAP_PRIVATE
, this->descriptor_
, poff
);
505 ownership
= View::DATA_MMAPPED
;
506 this->mapped_bytes_
+= psize
;
513 this->do_read(poff
, psize
, p
);
514 ownership
= View::DATA_ALLOCATED_ARRAY
;
518 const unsigned char* pbytes
= static_cast<const unsigned char*>(p
);
519 File_read::View
* v
= new File_read::View(poff
, psize
, pbytes
, byteshift
,
527 // Find a View or make a new one, shifted as required by the file
528 // offset OFFSET and ALIGNED.
531 File_read::find_or_make_view(off_t offset
, off_t start
,
532 section_size_type size
, bool aligned
, bool cache
)
534 // Check that start and end of the view are within the file.
535 if (start
> this->size_
536 || (static_cast<unsigned long long>(size
)
537 > static_cast<unsigned long long>(this->size_
- start
)))
538 gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds "
539 "size of file; the file may be corrupt"),
540 this->filename().c_str(),
541 static_cast<long long>(size
),
542 static_cast<long long>(start
));
544 unsigned int byteshift
;
549 unsigned int target_size
= (!parameters
->target_valid()
551 : parameters
->target().get_size());
552 byteshift
= offset
& ((target_size
/ 8) - 1);
554 // Set BYTESHIFT to the number of dummy bytes which must be
555 // inserted before the data in order for this data to be
558 byteshift
= (target_size
/ 8) - byteshift
;
561 // If --map-whole-files is set, make sure we have a
562 // whole file view. Options may not yet be ready, e.g.,
563 // when reading a version script. We then default to
564 // --no-map-whole-files.
565 if (this->whole_file_view_
== NULL
566 && parameters
->options_valid()
567 && parameters
->options().map_whole_files())
568 this->whole_file_view_
= this->make_view(0, this->size_
, 0, cache
);
570 // Try to find a View with the required BYTESHIFT.
571 File_read::View
* vshifted
;
572 File_read::View
* v
= this->find_view(offset
+ start
, size
,
573 aligned
? byteshift
: -1U,
582 // If VSHIFTED is not NULL, then it has the data we need, but with
583 // the wrong byteshift.
587 gold_assert(aligned
);
589 unsigned char* pbytes
;
590 pbytes
= static_cast<unsigned char*>(malloc(v
->size() + byteshift
));
593 memset(pbytes
, 0, byteshift
);
594 memcpy(pbytes
+ byteshift
, v
->data() + v
->byteshift(), v
->size());
596 File_read::View
* shifted_view
=
597 new File_read::View(v
->start(), v
->size(), pbytes
, byteshift
,
598 cache
, View::DATA_ALLOCATED_ARRAY
);
600 this->add_view(shifted_view
);
604 // Make a new view. If we don't need an aligned view, use a
605 // byteshift of 0, so that we can use mmap.
606 return this->make_view(offset
+ start
, size
,
607 aligned
? byteshift
: 0,
611 // Get a view into the file.
614 File_read::get_view(off_t offset
, off_t start
, section_size_type size
,
615 bool aligned
, bool cache
)
617 File_read::View
* pv
= this->find_or_make_view(offset
, start
, size
,
619 return pv
->data() + (offset
+ start
- pv
->start() + pv
->byteshift());
623 File_read::get_lasting_view(off_t offset
, off_t start
, section_size_type size
,
624 bool aligned
, bool cache
)
626 File_read::View
* pv
= this->find_or_make_view(offset
, start
, size
,
629 return new File_view(*this, pv
,
631 + (offset
+ start
- pv
->start() + pv
->byteshift())));
634 // Use readv to read COUNT entries from RM starting at START. BASE
635 // must be added to all file offsets in RM.
638 File_read::do_readv(off_t base
, const Read_multiple
& rm
, size_t start
,
641 unsigned char discard
[File_read::page_size
];
642 iovec iov
[File_read::max_readv_entries
* 2];
643 size_t iov_index
= 0;
645 off_t first_offset
= rm
[start
].file_offset
;
646 off_t last_offset
= first_offset
;
648 for (size_t i
= 0; i
< count
; ++i
)
650 const Read_multiple_entry
& i_entry(rm
[start
+ i
]);
652 if (i_entry
.file_offset
> last_offset
)
654 size_t skip
= i_entry
.file_offset
- last_offset
;
655 gold_assert(skip
<= sizeof discard
);
657 iov
[iov_index
].iov_base
= discard
;
658 iov
[iov_index
].iov_len
= skip
;
664 iov
[iov_index
].iov_base
= i_entry
.buffer
;
665 iov
[iov_index
].iov_len
= i_entry
.size
;
668 want
+= i_entry
.size
;
670 last_offset
= i_entry
.file_offset
+ i_entry
.size
;
673 this->reopen_descriptor();
675 gold_assert(iov_index
< sizeof iov
/ sizeof iov
[0]);
677 if (::lseek(this->descriptor_
, base
+ first_offset
, SEEK_SET
) < 0)
678 gold_fatal(_("%s: lseek failed: %s"),
679 this->filename().c_str(), strerror(errno
));
681 ssize_t got
= ::readv(this->descriptor_
, iov
, iov_index
);
684 gold_fatal(_("%s: readv failed: %s"),
685 this->filename().c_str(), strerror(errno
));
687 gold_fatal(_("%s: file too short: read only %zd of %zd bytes at %lld"),
688 this->filename().c_str(),
689 got
, want
, static_cast<long long>(base
+ first_offset
));
694 #if !defined(HAVE_READV)
695 #define GOLD_IOV_MAX 1
696 #elif defined(IOV_MAX)
697 #define GOLD_IOV_MAX IOV_MAX
699 #define GOLD_IOV_MAX (File_read::max_readv_entries * 2)
702 // Read several pieces of data from the file.
705 File_read::read_multiple(off_t base
, const Read_multiple
& rm
)
707 static size_t iov_max
= GOLD_IOV_MAX
;
708 size_t count
= rm
.size();
712 // Find up to MAX_READV_ENTRIES consecutive entries which are
713 // less than one page apart.
714 const Read_multiple_entry
& i_entry(rm
[i
]);
715 off_t i_off
= i_entry
.file_offset
;
716 off_t end_off
= i_off
+ i_entry
.size
;
718 for (j
= i
+ 1; j
< count
; ++j
)
720 if (j
- i
>= File_read::max_readv_entries
|| j
- i
>= iov_max
/ 2)
722 const Read_multiple_entry
& j_entry(rm
[j
]);
723 off_t j_off
= j_entry
.file_offset
;
724 gold_assert(j_off
>= end_off
);
725 off_t j_end_off
= j_off
+ j_entry
.size
;
726 if (j_end_off
- end_off
>= File_read::page_size
)
732 this->read(base
+ i_off
, i_entry
.size
, i_entry
.buffer
);
735 File_read::View
* view
= this->find_view(base
+ i_off
,
739 this->do_readv(base
, rm
, i
, j
- i
);
742 const unsigned char* v
= (view
->data()
743 + (base
+ i_off
- view
->start()
744 + view
->byteshift()));
745 for (size_t k
= i
; k
< j
; ++k
)
747 const Read_multiple_entry
& k_entry(rm
[k
]);
748 gold_assert((convert_to_section_size_type(k_entry
.file_offset
751 <= convert_to_section_size_type(end_off
753 memcpy(k_entry
.buffer
,
754 v
+ (k_entry
.file_offset
- i_off
),
764 // Mark all views as no longer cached.
767 File_read::clear_view_cache_marks()
769 // Just ignore this if there are multiple objects associated with
770 // the file. Otherwise we will wind up uncaching and freeing some
771 // views for other objects.
772 if (this->object_count_
> 1)
775 for (Views::iterator p
= this->views_
.begin();
776 p
!= this->views_
.end();
778 p
->second
->clear_cache();
779 for (Saved_views::iterator p
= this->saved_views_
.begin();
780 p
!= this->saved_views_
.end();
785 // Remove all the file views. For a file which has multiple
786 // associated objects (i.e., an archive), we keep accessed views
787 // around until next time, in the hopes that they will be useful for
791 File_read::clear_views(Clear_views_mode mode
)
793 bool keep_files_mapped
= (parameters
->options_valid()
794 && parameters
->options().keep_files_mapped());
795 Views::iterator p
= this->views_
.begin();
796 while (p
!= this->views_
.end())
799 if (p
->second
->is_locked() || p
->second
->is_permanent_view())
800 should_delete
= false;
801 else if (mode
== CLEAR_VIEWS_ALL
)
802 should_delete
= true;
803 else if ((p
->second
->should_cache()
804 || p
->second
== this->whole_file_view_
)
805 && keep_files_mapped
)
806 should_delete
= false;
807 else if (this->object_count_
> 1
808 && p
->second
->accessed()
809 && mode
!= CLEAR_VIEWS_ARCHIVE
)
810 should_delete
= false;
812 should_delete
= true;
816 if (p
->second
== this->whole_file_view_
)
817 this->whole_file_view_
= NULL
;
820 // map::erase invalidates only the iterator to the deleted
822 Views::iterator pe
= p
;
824 this->views_
.erase(pe
);
828 p
->second
->clear_accessed();
833 Saved_views::iterator q
= this->saved_views_
.begin();
834 while (q
!= this->saved_views_
.end())
836 if (!(*q
)->is_locked())
839 q
= this->saved_views_
.erase(q
);
843 gold_assert(mode
!= CLEAR_VIEWS_ALL
);
849 // Print statistical information to stderr. This is used for --stats.
852 File_read::print_stats()
854 fprintf(stderr
, _("%s: total bytes mapped for read: %llu\n"),
855 program_name
, File_read::total_mapped_bytes
);
856 fprintf(stderr
, _("%s: maximum bytes mapped for read at one time: %llu\n"),
857 program_name
, File_read::maximum_mapped_bytes
);
862 File_view::~File_view()
864 gold_assert(this->file_
.is_locked());
865 this->view_
->unlock();
870 // Create a file given just the filename.
872 Input_file::Input_file(const char* name
)
873 : found_name_(), file_(), is_in_sysroot_(false), format_(FORMAT_NONE
)
875 this->input_argument_
=
876 new Input_file_argument(name
, Input_file_argument::INPUT_FILE_TYPE_FILE
,
877 "", false, Position_dependent_options());
880 // Create a file for testing.
882 Input_file::Input_file(const Task
* task
, const char* name
,
883 const unsigned char* contents
, off_t size
)
886 this->input_argument_
=
887 new Input_file_argument(name
, Input_file_argument::INPUT_FILE_TYPE_FILE
,
888 "", false, Position_dependent_options());
889 bool ok
= this->file_
.open(task
, name
, contents
, size
);
893 // Return the position dependent options in force for this file.
895 const Position_dependent_options
&
896 Input_file::options() const
898 return this->input_argument_
->options();
901 // Return the name given by the user. For -lc this will return "c".
904 Input_file::name() const
906 return this->input_argument_
->name();
909 // Return whether this file is in a system directory.
912 Input_file::is_in_system_directory() const
914 if (this->is_in_sysroot())
916 return parameters
->options().is_in_system_directory(this->filename());
919 // Return whether we are only reading symbols.
922 Input_file::just_symbols() const
924 return this->input_argument_
->just_symbols();
927 // Return whether this is a file that we will search for in the list
931 Input_file::will_search_for() const
933 return (!IS_ABSOLUTE_PATH(this->input_argument_
->name())
934 && (this->input_argument_
->is_lib()
935 || this->input_argument_
->is_searched_file()
936 || this->input_argument_
->extra_search_path() != NULL
));
939 // Return the file last modification time. Calls gold_fatal if the stat
940 // system call failed.
943 File_read::get_mtime()
945 struct stat file_stat
;
946 this->reopen_descriptor();
948 if (fstat(this->descriptor_
, &file_stat
) < 0)
949 gold_fatal(_("%s: stat failed: %s"), this->name_
.c_str(),
951 #ifdef HAVE_STAT_ST_MTIM
952 return Timespec(file_stat
.st_mtim
.tv_sec
, file_stat
.st_mtim
.tv_nsec
);
954 return Timespec(file_stat
.st_mtime
, 0);
958 // Try to find a file in the extra search dirs. Returns true on success.
961 Input_file::try_extra_search_path(int* pindex
,
962 const Input_file_argument
* input_argument
,
963 std::string filename
, std::string
* found_name
,
966 if (input_argument
->extra_search_path() == NULL
)
969 std::string name
= input_argument
->extra_search_path();
970 if (!IS_DIR_SEPARATOR(name
[name
.length() - 1]))
974 struct stat dummy_stat
;
975 if (*pindex
> 0 || ::stat(name
.c_str(), &dummy_stat
) < 0)
978 *found_name
= filename
;
983 // Find the actual file.
984 // If the filename is not absolute, we assume it is in the current
985 // directory *except* when:
986 // A) input_argument_->is_lib() is true;
987 // B) input_argument_->is_searched_file() is true; or
988 // C) input_argument_->extra_search_path() is not empty.
989 // In each, we look in extra_search_path + library_path to find
990 // the file location, rather than the current directory.
993 Input_file::find_file(const Dirsearch
& dirpath
, int* pindex
,
994 const Input_file_argument
* input_argument
,
996 std::string
* found_name
, std::string
* namep
)
1000 // Case 1: name is an absolute file, just try to open it
1001 // Case 2: name is relative but is_lib is false, is_searched_file is false,
1002 // and extra_search_path is empty
1003 if (IS_ABSOLUTE_PATH(input_argument
->name())
1004 || (!input_argument
->is_lib()
1005 && !input_argument
->is_searched_file()
1006 && input_argument
->extra_search_path() == NULL
))
1008 name
= input_argument
->name();
1013 // Case 3: is_lib is true or is_searched_file is true
1014 else if (input_argument
->is_lib()
1015 || input_argument
->is_searched_file())
1017 std::vector
<std::string
> names
;
1019 if (input_argument
->is_lib())
1021 std::string prefix
= "lib";
1022 prefix
+= input_argument
->name();
1023 if (parameters
->options().is_static()
1024 || !input_argument
->options().Bdynamic())
1025 names
.push_back(prefix
+ ".a");
1028 names
.push_back(prefix
+ ".so");
1029 names
.push_back(prefix
+ ".a");
1033 names
.push_back(input_argument
->name());
1035 for (std::vector
<std::string
>::const_iterator n
= names
.begin();
1038 if (Input_file::try_extra_search_path(pindex
, input_argument
, *n
,
1042 // It is not in the extra_search_path.
1043 name
= dirpath
.find(names
, is_in_sysroot
, pindex
, found_name
);
1046 gold_error(_("cannot find %s%s"),
1047 input_argument
->is_lib() ? "-l" : "",
1048 input_argument
->name());
1054 // Case 4: extra_search_path is not empty
1057 gold_assert(input_argument
->extra_search_path() != NULL
);
1059 if (try_extra_search_path(pindex
, input_argument
, input_argument
->name(),
1063 // extra_search_path failed, so check the normal search-path.
1064 int index
= *pindex
;
1067 name
= dirpath
.find(std::vector
<std::string
>(1, input_argument
->name()),
1068 is_in_sysroot
, &index
, found_name
);
1071 gold_error(_("cannot find %s"),
1072 input_argument
->name());
1076 *pindex
= index
+ 1;
1084 Input_file::open(const Dirsearch
& dirpath
, const Task
* task
, int* pindex
)
1087 if (!Input_file::find_file(dirpath
, pindex
, this->input_argument_
,
1088 &this->is_in_sysroot_
, &this->found_name_
, &name
))
1091 // Now that we've figured out where the file lives, try to open it.
1093 General_options::Object_format format
=
1094 this->input_argument_
->options().format_enum();
1096 if (format
== General_options::OBJECT_FORMAT_ELF
)
1098 ok
= this->file_
.open(task
, name
);
1099 this->format_
= FORMAT_ELF
;
1103 gold_assert(format
== General_options::OBJECT_FORMAT_BINARY
);
1104 ok
= this->open_binary(task
, name
);
1105 this->format_
= FORMAT_BINARY
;
1110 gold_error(_("cannot open %s: %s"),
1111 name
.c_str(), strerror(errno
));
1112 this->format_
= FORMAT_NONE
;
1119 // Open a file for --format binary.
1122 Input_file::open_binary(const Task
* task
, const std::string
& name
)
1124 // In order to open a binary file, we need machine code, size, and
1125 // endianness. We may not have a valid target at this point, in
1126 // which case we use the default target.
1127 parameters_force_valid_target();
1128 const Target
& target(parameters
->target());
1130 Binary_to_elf
binary_to_elf(target
.machine_code(),
1132 target
.is_big_endian(),
1134 if (!binary_to_elf
.convert(task
))
1136 return this->file_
.open(task
, name
, binary_to_elf
.converted_data_leak(),
1137 binary_to_elf
.converted_size());
1140 } // End namespace gold.