1 // fileread.cc -- read files for gold
3 // Copyright (C) 2006-2014 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.
31 #ifdef HAVE_SYS_MMAN_H
40 #include "filenames.h"
43 #include "parameters.h"
45 #include "dirsearch.h"
48 #include "descriptors.h"
49 #include "gold-threads.h"
52 // For systems without mmap support.
54 # define mmap gold_mmap
55 # define munmap gold_munmap
57 # define MAP_FAILED (reinterpret_cast<void*>(-1))
63 # define MAP_PRIVATE 0
67 # define ENOSYS EINVAL
71 gold_mmap(void *, size_t, int, int, int, off_t
)
78 gold_munmap(void *, size_t)
87 struct iovec
{ void* iov_base
; size_t iov_len
; };
89 readv(int, const iovec
*, int)
98 // Get the last modified time of an unopened file.
101 get_mtime(const char* filename
, Timespec
* mtime
)
103 struct stat file_stat
;
105 if (stat(filename
, &file_stat
) < 0)
107 #ifdef HAVE_STAT_ST_MTIM
108 mtime
->seconds
= file_stat
.st_mtim
.tv_sec
;
109 mtime
->nanoseconds
= file_stat
.st_mtim
.tv_nsec
;
111 mtime
->seconds
= file_stat
.st_mtime
;
112 mtime
->nanoseconds
= 0;
119 // A lock for the File_read static variables.
120 static Lock
* file_counts_lock
= NULL
;
121 static Initialize_lock
file_counts_initialize_lock(&file_counts_lock
);
123 // The File_read static variables.
124 unsigned long long File_read::total_mapped_bytes
;
125 unsigned long long File_read::current_mapped_bytes
;
126 unsigned long long File_read::maximum_mapped_bytes
;
128 // Class File_read::View.
130 File_read::View::~View()
132 gold_assert(!this->is_locked());
133 switch (this->data_ownership_
)
135 case DATA_ALLOCATED_ARRAY
:
136 free(const_cast<unsigned char*>(this->data_
));
139 if (::munmap(const_cast<unsigned char*>(this->data_
), this->size_
) != 0)
140 gold_warning(_("munmap failed: %s"), strerror(errno
));
141 if (!parameters
->options_valid() || parameters
->options().stats())
143 file_counts_initialize_lock
.initialize();
144 Hold_optional_lock
hl(file_counts_lock
);
145 File_read::current_mapped_bytes
-= this->size_
;
156 File_read::View::lock()
162 File_read::View::unlock()
164 gold_assert(this->lock_count_
> 0);
169 File_read::View::is_locked()
171 return this->lock_count_
> 0;
176 File_read::~File_read()
178 gold_assert(this->token_
.is_writable());
179 if (this->is_descriptor_opened_
)
181 release_descriptor(this->descriptor_
, true);
182 this->descriptor_
= -1;
183 this->is_descriptor_opened_
= false;
186 this->clear_views(CLEAR_VIEWS_ALL
);
192 File_read::open(const Task
* task
, const std::string
& name
)
194 gold_assert(this->token_
.is_writable()
195 && this->descriptor_
< 0
196 && !this->is_descriptor_opened_
197 && this->name_
.empty());
200 this->descriptor_
= open_descriptor(-1, this->name_
.c_str(),
203 if (this->descriptor_
>= 0)
205 this->is_descriptor_opened_
= true;
207 if (::fstat(this->descriptor_
, &s
) < 0)
208 gold_error(_("%s: fstat failed: %s"),
209 this->name_
.c_str(), strerror(errno
));
210 this->size_
= s
.st_size
;
211 gold_debug(DEBUG_FILES
, "Attempt to open %s succeeded",
212 this->name_
.c_str());
213 this->token_
.add_writer(task
);
216 return this->descriptor_
>= 0;
219 // Open the file with the contents in memory.
222 File_read::open(const Task
* task
, const std::string
& name
,
223 const unsigned char* contents
, off_t size
)
225 gold_assert(this->token_
.is_writable()
226 && this->descriptor_
< 0
227 && !this->is_descriptor_opened_
228 && this->name_
.empty());
230 this->whole_file_view_
= new View(0, size
, contents
, 0, false,
231 View::DATA_NOT_OWNED
);
232 this->add_view(this->whole_file_view_
);
234 this->token_
.add_writer(task
);
238 // Reopen a descriptor if necessary.
241 File_read::reopen_descriptor()
243 if (!this->is_descriptor_opened_
)
245 this->descriptor_
= open_descriptor(this->descriptor_
,
248 if (this->descriptor_
< 0)
249 gold_fatal(_("could not reopen file %s"), this->name_
.c_str());
250 this->is_descriptor_opened_
= true;
254 // Release the file. This is called when we are done with the file in
260 gold_assert(this->is_locked());
262 if (!parameters
->options_valid() || parameters
->options().stats())
264 file_counts_initialize_lock
.initialize();
265 Hold_optional_lock
hl(file_counts_lock
);
266 File_read::total_mapped_bytes
+= this->mapped_bytes_
;
267 File_read::current_mapped_bytes
+= this->mapped_bytes_
;
268 if (File_read::current_mapped_bytes
> File_read::maximum_mapped_bytes
)
269 File_read::maximum_mapped_bytes
= File_read::current_mapped_bytes
;
272 this->mapped_bytes_
= 0;
274 // Only clear views if there is only one attached object. Otherwise
275 // we waste time trying to clear cached archive views. Similarly
276 // for releasing the descriptor.
277 if (this->object_count_
<= 1)
279 this->clear_views(CLEAR_VIEWS_NORMAL
);
280 if (this->is_descriptor_opened_
)
282 release_descriptor(this->descriptor_
, false);
283 this->is_descriptor_opened_
= false;
287 this->released_
= true;
293 File_read::lock(const Task
* task
)
295 gold_assert(this->released_
);
296 this->token_
.add_writer(task
);
297 this->released_
= false;
303 File_read::unlock(const Task
* task
)
306 this->token_
.remove_writer(task
);
309 // Return whether the file is locked.
312 File_read::is_locked() const
314 if (!this->token_
.is_writable())
316 // The file is not locked, so it should have been released.
317 gold_assert(this->released_
);
321 // See if we have a view which covers the file starting at START for
322 // SIZE bytes. Return a pointer to the View if found, NULL if not.
323 // If BYTESHIFT is not -1U, the returned View must have the specified
324 // byte shift; otherwise, it may have any byte shift. If VSHIFTED is
325 // not NULL, this sets *VSHIFTED to a view which would have worked if
326 // not for the requested BYTESHIFT.
328 inline File_read::View
*
329 File_read::find_view(off_t start
, section_size_type size
,
330 unsigned int byteshift
, File_read::View
** vshifted
) const
332 gold_assert(start
<= this->size_
333 && (static_cast<unsigned long long>(size
)
334 <= static_cast<unsigned long long>(this->size_
- start
)));
336 if (vshifted
!= NULL
)
339 // If we have the whole file mmapped, and the alignment is right,
341 if (this->whole_file_view_
)
342 if (byteshift
== -1U || byteshift
== 0)
343 return this->whole_file_view_
;
345 off_t page
= File_read::page_offset(start
);
347 unsigned int bszero
= 0;
348 Views::const_iterator p
= this->views_
.upper_bound(std::make_pair(page
- 1,
351 while (p
!= this->views_
.end() && p
->first
.first
<= page
)
353 if (p
->second
->start() <= start
354 && (p
->second
->start() + static_cast<off_t
>(p
->second
->size())
355 >= start
+ static_cast<off_t
>(size
)))
357 if (byteshift
== -1U || byteshift
== p
->second
->byteshift())
359 p
->second
->set_accessed();
363 if (vshifted
!= NULL
&& *vshifted
== NULL
)
364 *vshifted
= p
->second
;
373 // Read SIZE bytes from the file starting at offset START. Read into
377 File_read::do_read(off_t start
, section_size_type size
, void* p
)
380 if (this->whole_file_view_
!= NULL
)
382 bytes
= this->size_
- start
;
383 if (static_cast<section_size_type
>(bytes
) >= size
)
385 memcpy(p
, this->whole_file_view_
->data() + start
, size
);
391 this->reopen_descriptor();
393 char *read_ptr
= static_cast<char *>(p
);
394 off_t read_pos
= start
;
395 size_t to_read
= size
;
398 bytes
= ::pread(this->descriptor_
, read_ptr
, to_read
, read_pos
);
400 gold_fatal(_("%s: pread failed: %s"),
401 this->filename().c_str(), strerror(errno
));
411 bytes
= size
- to_read
;
414 gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"),
415 this->filename().c_str(),
416 static_cast<long long>(bytes
),
417 static_cast<long long>(size
),
418 static_cast<long long>(start
));
421 // Read data from the file.
424 File_read::read(off_t start
, section_size_type size
, void* p
)
426 const File_read::View
* pv
= this->find_view(start
, size
, -1U, NULL
);
429 memcpy(p
, pv
->data() + (start
- pv
->start() + pv
->byteshift()), size
);
433 this->do_read(start
, size
, p
);
436 // Add a new view. There may already be an existing view at this
437 // offset. If there is, the new view will be larger, and should
438 // replace the old view.
441 File_read::add_view(File_read::View
* v
)
443 std::pair
<Views::iterator
, bool> ins
=
444 this->views_
.insert(std::make_pair(std::make_pair(v
->start(),
450 // There was an existing view at this offset. It must not be large
451 // enough. We can't delete it here, since something might be using
452 // it; we put it on a list to be deleted when the file is unlocked.
453 File_read::View
* vold
= ins
.first
->second
;
454 gold_assert(vold
->size() < v
->size());
455 if (vold
->should_cache())
460 this->saved_views_
.push_back(vold
);
462 ins
.first
->second
= v
;
465 // Make a new view with a specified byteshift, reading the data from
469 File_read::make_view(off_t start
, section_size_type size
,
470 unsigned int byteshift
, bool cache
)
472 gold_assert(size
> 0);
473 gold_assert(start
<= this->size_
474 && (static_cast<unsigned long long>(size
)
475 <= static_cast<unsigned long long>(this->size_
- start
)));
477 off_t poff
= File_read::page_offset(start
);
479 section_size_type psize
= File_read::pages(size
+ (start
- poff
));
481 if (poff
+ static_cast<off_t
>(psize
) >= this->size_
)
483 psize
= this->size_
- poff
;
484 gold_assert(psize
>= size
);
488 View::Data_ownership ownership
;
491 p
= malloc(psize
+ byteshift
);
494 memset(p
, 0, byteshift
);
495 this->do_read(poff
, psize
, static_cast<unsigned char*>(p
) + byteshift
);
496 ownership
= View::DATA_ALLOCATED_ARRAY
;
500 this->reopen_descriptor();
501 p
= ::mmap(NULL
, psize
, PROT_READ
, MAP_PRIVATE
, this->descriptor_
, poff
);
504 ownership
= View::DATA_MMAPPED
;
505 this->mapped_bytes_
+= psize
;
512 this->do_read(poff
, psize
, p
);
513 ownership
= View::DATA_ALLOCATED_ARRAY
;
517 const unsigned char* pbytes
= static_cast<const unsigned char*>(p
);
518 File_read::View
* v
= new File_read::View(poff
, psize
, pbytes
, byteshift
,
526 // Find a View or make a new one, shifted as required by the file
527 // offset OFFSET and ALIGNED.
530 File_read::find_or_make_view(off_t offset
, off_t start
,
531 section_size_type size
, bool aligned
, bool cache
)
533 // Check that start and end of the view are within the file.
534 if (start
> this->size_
535 || (static_cast<unsigned long long>(size
)
536 > static_cast<unsigned long long>(this->size_
- start
)))
537 gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds "
538 "size of file; the file may be corrupt"),
539 this->filename().c_str(),
540 static_cast<long long>(size
),
541 static_cast<long long>(start
));
543 unsigned int byteshift
;
548 unsigned int target_size
= (!parameters
->target_valid()
550 : parameters
->target().get_size());
551 byteshift
= offset
& ((target_size
/ 8) - 1);
553 // Set BYTESHIFT to the number of dummy bytes which must be
554 // inserted before the data in order for this data to be
557 byteshift
= (target_size
/ 8) - byteshift
;
560 // If --map-whole-files is set, make sure we have a
561 // whole file view. Options may not yet be ready, e.g.,
562 // when reading a version script. We then default to
563 // --no-map-whole-files.
564 if (this->whole_file_view_
== NULL
565 && parameters
->options_valid()
566 && parameters
->options().map_whole_files())
567 this->whole_file_view_
= this->make_view(0, this->size_
, 0, cache
);
569 // Try to find a View with the required BYTESHIFT.
570 File_read::View
* vshifted
;
571 File_read::View
* v
= this->find_view(offset
+ start
, size
,
572 aligned
? byteshift
: -1U,
581 // If VSHIFTED is not NULL, then it has the data we need, but with
582 // the wrong byteshift.
586 gold_assert(aligned
);
588 unsigned char* pbytes
;
589 pbytes
= static_cast<unsigned char*>(malloc(v
->size() + byteshift
));
592 memset(pbytes
, 0, byteshift
);
593 memcpy(pbytes
+ byteshift
, v
->data() + v
->byteshift(), v
->size());
595 File_read::View
* shifted_view
=
596 new File_read::View(v
->start(), v
->size(), pbytes
, byteshift
,
597 cache
, View::DATA_ALLOCATED_ARRAY
);
599 this->add_view(shifted_view
);
603 // Make a new view. If we don't need an aligned view, use a
604 // byteshift of 0, so that we can use mmap.
605 return this->make_view(offset
+ start
, size
,
606 aligned
? byteshift
: 0,
610 // Get a view into the file.
613 File_read::get_view(off_t offset
, off_t start
, section_size_type size
,
614 bool aligned
, bool cache
)
616 File_read::View
* pv
= this->find_or_make_view(offset
, start
, size
,
618 return pv
->data() + (offset
+ start
- pv
->start() + pv
->byteshift());
622 File_read::get_lasting_view(off_t offset
, off_t start
, section_size_type size
,
623 bool aligned
, bool cache
)
625 File_read::View
* pv
= this->find_or_make_view(offset
, start
, size
,
628 return new File_view(*this, pv
,
630 + (offset
+ start
- pv
->start() + pv
->byteshift())));
633 // Use readv to read COUNT entries from RM starting at START. BASE
634 // must be added to all file offsets in RM.
637 File_read::do_readv(off_t base
, const Read_multiple
& rm
, size_t start
,
640 unsigned char discard
[File_read::page_size
];
641 iovec iov
[File_read::max_readv_entries
* 2];
642 size_t iov_index
= 0;
644 off_t first_offset
= rm
[start
].file_offset
;
645 off_t last_offset
= first_offset
;
647 for (size_t i
= 0; i
< count
; ++i
)
649 const Read_multiple_entry
& i_entry(rm
[start
+ i
]);
651 if (i_entry
.file_offset
> last_offset
)
653 size_t skip
= i_entry
.file_offset
- last_offset
;
654 gold_assert(skip
<= sizeof discard
);
656 iov
[iov_index
].iov_base
= discard
;
657 iov
[iov_index
].iov_len
= skip
;
663 iov
[iov_index
].iov_base
= i_entry
.buffer
;
664 iov
[iov_index
].iov_len
= i_entry
.size
;
667 want
+= i_entry
.size
;
669 last_offset
= i_entry
.file_offset
+ i_entry
.size
;
672 this->reopen_descriptor();
674 gold_assert(iov_index
< sizeof iov
/ sizeof iov
[0]);
676 if (::lseek(this->descriptor_
, base
+ first_offset
, SEEK_SET
) < 0)
677 gold_fatal(_("%s: lseek failed: %s"),
678 this->filename().c_str(), strerror(errno
));
680 ssize_t got
= ::readv(this->descriptor_
, iov
, iov_index
);
683 gold_fatal(_("%s: readv failed: %s"),
684 this->filename().c_str(), strerror(errno
));
686 gold_fatal(_("%s: file too short: read only %zd of %zd bytes at %lld"),
687 this->filename().c_str(),
688 got
, want
, static_cast<long long>(base
+ first_offset
));
693 #if !defined(HAVE_READV)
694 #define GOLD_IOV_MAX 1
695 #elif defined(IOV_MAX)
696 #define GOLD_IOV_MAX IOV_MAX
698 #define GOLD_IOV_MAX (File_read::max_readv_entries * 2)
701 // Read several pieces of data from the file.
704 File_read::read_multiple(off_t base
, const Read_multiple
& rm
)
706 static size_t iov_max
= GOLD_IOV_MAX
;
707 size_t count
= rm
.size();
711 // Find up to MAX_READV_ENTRIES consecutive entries which are
712 // less than one page apart.
713 const Read_multiple_entry
& i_entry(rm
[i
]);
714 off_t i_off
= i_entry
.file_offset
;
715 off_t end_off
= i_off
+ i_entry
.size
;
717 for (j
= i
+ 1; j
< count
; ++j
)
719 if (j
- i
>= File_read::max_readv_entries
|| j
- i
>= iov_max
/ 2)
721 const Read_multiple_entry
& j_entry(rm
[j
]);
722 off_t j_off
= j_entry
.file_offset
;
723 gold_assert(j_off
>= end_off
);
724 off_t j_end_off
= j_off
+ j_entry
.size
;
725 if (j_end_off
- end_off
>= File_read::page_size
)
731 this->read(base
+ i_off
, i_entry
.size
, i_entry
.buffer
);
734 File_read::View
* view
= this->find_view(base
+ i_off
,
738 this->do_readv(base
, rm
, i
, j
- i
);
741 const unsigned char* v
= (view
->data()
742 + (base
+ i_off
- view
->start()
743 + view
->byteshift()));
744 for (size_t k
= i
; k
< j
; ++k
)
746 const Read_multiple_entry
& k_entry(rm
[k
]);
747 gold_assert((convert_to_section_size_type(k_entry
.file_offset
750 <= convert_to_section_size_type(end_off
752 memcpy(k_entry
.buffer
,
753 v
+ (k_entry
.file_offset
- i_off
),
763 // Mark all views as no longer cached.
766 File_read::clear_view_cache_marks()
768 // Just ignore this if there are multiple objects associated with
769 // the file. Otherwise we will wind up uncaching and freeing some
770 // views for other objects.
771 if (this->object_count_
> 1)
774 for (Views::iterator p
= this->views_
.begin();
775 p
!= this->views_
.end();
777 p
->second
->clear_cache();
778 for (Saved_views::iterator p
= this->saved_views_
.begin();
779 p
!= this->saved_views_
.end();
784 // Remove all the file views. For a file which has multiple
785 // associated objects (i.e., an archive), we keep accessed views
786 // around until next time, in the hopes that they will be useful for
790 File_read::clear_views(Clear_views_mode mode
)
792 bool keep_files_mapped
= (parameters
->options_valid()
793 && parameters
->options().keep_files_mapped());
794 Views::iterator p
= this->views_
.begin();
795 while (p
!= this->views_
.end())
798 if (p
->second
->is_locked() || p
->second
->is_permanent_view())
799 should_delete
= false;
800 else if (mode
== CLEAR_VIEWS_ALL
)
801 should_delete
= true;
802 else if ((p
->second
->should_cache()
803 || p
->second
== this->whole_file_view_
)
804 && keep_files_mapped
)
805 should_delete
= false;
806 else if (this->object_count_
> 1
807 && p
->second
->accessed()
808 && mode
!= CLEAR_VIEWS_ARCHIVE
)
809 should_delete
= false;
811 should_delete
= true;
815 if (p
->second
== this->whole_file_view_
)
816 this->whole_file_view_
= NULL
;
819 // map::erase invalidates only the iterator to the deleted
821 Views::iterator pe
= p
;
823 this->views_
.erase(pe
);
827 p
->second
->clear_accessed();
832 Saved_views::iterator q
= this->saved_views_
.begin();
833 while (q
!= this->saved_views_
.end())
835 if (!(*q
)->is_locked())
838 q
= this->saved_views_
.erase(q
);
842 gold_assert(mode
!= CLEAR_VIEWS_ALL
);
848 // Print statistical information to stderr. This is used for --stats.
851 File_read::print_stats()
853 fprintf(stderr
, _("%s: total bytes mapped for read: %llu\n"),
854 program_name
, File_read::total_mapped_bytes
);
855 fprintf(stderr
, _("%s: maximum bytes mapped for read at one time: %llu\n"),
856 program_name
, File_read::maximum_mapped_bytes
);
861 File_view::~File_view()
863 gold_assert(this->file_
.is_locked());
864 this->view_
->unlock();
869 // Create a file given just the filename.
871 Input_file::Input_file(const char* name
)
872 : found_name_(), file_(), is_in_sysroot_(false), format_(FORMAT_NONE
)
874 this->input_argument_
=
875 new Input_file_argument(name
, Input_file_argument::INPUT_FILE_TYPE_FILE
,
876 "", false, Position_dependent_options());
879 // Create a file for testing.
881 Input_file::Input_file(const Task
* task
, const char* name
,
882 const unsigned char* contents
, off_t size
)
885 this->input_argument_
=
886 new Input_file_argument(name
, Input_file_argument::INPUT_FILE_TYPE_FILE
,
887 "", false, Position_dependent_options());
888 bool ok
= this->file_
.open(task
, name
, contents
, size
);
892 // Return the position dependent options in force for this file.
894 const Position_dependent_options
&
895 Input_file::options() const
897 return this->input_argument_
->options();
900 // Return the name given by the user. For -lc this will return "c".
903 Input_file::name() const
905 return this->input_argument_
->name();
908 // Return whether this file is in a system directory.
911 Input_file::is_in_system_directory() const
913 if (this->is_in_sysroot())
915 return parameters
->options().is_in_system_directory(this->filename());
918 // Return whether we are only reading symbols.
921 Input_file::just_symbols() const
923 return this->input_argument_
->just_symbols();
926 // Return whether this is a file that we will search for in the list
930 Input_file::will_search_for() const
932 return (!IS_ABSOLUTE_PATH(this->input_argument_
->name())
933 && (this->input_argument_
->is_lib()
934 || this->input_argument_
->is_searched_file()
935 || this->input_argument_
->extra_search_path() != NULL
));
938 // Return the file last modification time. Calls gold_fatal if the stat
939 // system call failed.
942 File_read::get_mtime()
944 struct stat file_stat
;
945 this->reopen_descriptor();
947 if (fstat(this->descriptor_
, &file_stat
) < 0)
948 gold_fatal(_("%s: stat failed: %s"), this->name_
.c_str(),
950 #ifdef HAVE_STAT_ST_MTIM
951 return Timespec(file_stat
.st_mtim
.tv_sec
, file_stat
.st_mtim
.tv_nsec
);
953 return Timespec(file_stat
.st_mtime
, 0);
957 // Try to find a file in the extra search dirs. Returns true on success.
960 Input_file::try_extra_search_path(int* pindex
,
961 const Input_file_argument
* input_argument
,
962 std::string filename
, std::string
* found_name
,
965 if (input_argument
->extra_search_path() == NULL
)
968 std::string name
= input_argument
->extra_search_path();
969 if (!IS_DIR_SEPARATOR(name
[name
.length() - 1]))
973 struct stat dummy_stat
;
974 if (*pindex
> 0 || ::stat(name
.c_str(), &dummy_stat
) < 0)
977 *found_name
= filename
;
982 // Find the actual file.
983 // If the filename is not absolute, we assume it is in the current
984 // directory *except* when:
985 // A) input_argument_->is_lib() is true;
986 // B) input_argument_->is_searched_file() is true; or
987 // C) input_argument_->extra_search_path() is not empty.
988 // In each, we look in extra_search_path + library_path to find
989 // the file location, rather than the current directory.
992 Input_file::find_file(const Dirsearch
& dirpath
, int* pindex
,
993 const Input_file_argument
* input_argument
,
995 std::string
* found_name
, std::string
* namep
)
999 // Case 1: name is an absolute file, just try to open it
1000 // Case 2: name is relative but is_lib is false, is_searched_file is false,
1001 // and extra_search_path is empty
1002 if (IS_ABSOLUTE_PATH(input_argument
->name())
1003 || (!input_argument
->is_lib()
1004 && !input_argument
->is_searched_file()
1005 && input_argument
->extra_search_path() == NULL
))
1007 name
= input_argument
->name();
1012 // Case 3: is_lib is true or is_searched_file is true
1013 else if (input_argument
->is_lib()
1014 || input_argument
->is_searched_file())
1016 std::vector
<std::string
> names
;
1018 if (input_argument
->is_lib())
1020 std::string prefix
= "lib";
1021 prefix
+= input_argument
->name();
1022 if (parameters
->options().is_static()
1023 || !input_argument
->options().Bdynamic())
1024 names
.push_back(prefix
+ ".a");
1027 names
.push_back(prefix
+ ".so");
1028 names
.push_back(prefix
+ ".a");
1032 names
.push_back(input_argument
->name());
1034 for (std::vector
<std::string
>::const_iterator n
= names
.begin();
1037 if (Input_file::try_extra_search_path(pindex
, input_argument
, *n
,
1041 // It is not in the extra_search_path.
1042 name
= dirpath
.find(names
, is_in_sysroot
, pindex
, found_name
);
1045 gold_error(_("cannot find %s%s"),
1046 input_argument
->is_lib() ? "-l" : "",
1047 input_argument
->name());
1053 // Case 4: extra_search_path is not empty
1056 gold_assert(input_argument
->extra_search_path() != NULL
);
1058 if (try_extra_search_path(pindex
, input_argument
, input_argument
->name(),
1062 // extra_search_path failed, so check the normal search-path.
1063 int index
= *pindex
;
1066 name
= dirpath
.find(std::vector
<std::string
>(1, input_argument
->name()),
1067 is_in_sysroot
, &index
, found_name
);
1070 gold_error(_("cannot find %s"),
1071 input_argument
->name());
1075 *pindex
= index
+ 1;
1083 Input_file::open(const Dirsearch
& dirpath
, const Task
* task
, int* pindex
)
1086 if (!Input_file::find_file(dirpath
, pindex
, this->input_argument_
,
1087 &this->is_in_sysroot_
, &this->found_name_
, &name
))
1090 // Now that we've figured out where the file lives, try to open it.
1092 General_options::Object_format format
=
1093 this->input_argument_
->options().format_enum();
1095 if (format
== General_options::OBJECT_FORMAT_ELF
)
1097 ok
= this->file_
.open(task
, name
);
1098 this->format_
= FORMAT_ELF
;
1102 gold_assert(format
== General_options::OBJECT_FORMAT_BINARY
);
1103 ok
= this->open_binary(task
, name
);
1104 this->format_
= FORMAT_BINARY
;
1109 gold_error(_("cannot open %s: %s"),
1110 name
.c_str(), strerror(errno
));
1111 this->format_
= FORMAT_NONE
;
1118 // Open a file for --format binary.
1121 Input_file::open_binary(const Task
* task
, const std::string
& name
)
1123 // In order to open a binary file, we need machine code, size, and
1124 // endianness. We may not have a valid target at this point, in
1125 // which case we use the default target.
1126 parameters_force_valid_target();
1127 const Target
& target(parameters
->target());
1129 Binary_to_elf
binary_to_elf(target
.machine_code(),
1131 target
.is_big_endian(),
1133 if (!binary_to_elf
.convert(task
))
1135 return this->file_
.open(task
, name
, binary_to_elf
.converted_data_leak(),
1136 binary_to_elf
.converted_size());
1139 } // End namespace gold.