1 // fileread.cc -- read files for gold
3 // Copyright (C) 2006-2023 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
;
127 std::vector
<std::string
> File_read::files_read
;
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
);
215 file_counts_initialize_lock
.initialize();
216 Hold_optional_lock
hl(file_counts_lock
);
217 record_file_read(this->name_
);
220 return this->descriptor_
>= 0;
223 // Open the file with the contents in memory.
226 File_read::open(const Task
* task
, const std::string
& name
,
227 const unsigned char* contents
, off_t size
)
229 gold_assert(this->token_
.is_writable()
230 && this->descriptor_
< 0
231 && !this->is_descriptor_opened_
232 && this->name_
.empty());
234 this->whole_file_view_
= new View(0, size
, contents
, 0, false,
235 View::DATA_NOT_OWNED
);
236 this->add_view(this->whole_file_view_
);
238 this->token_
.add_writer(task
);
242 // Reopen a descriptor if necessary.
245 File_read::reopen_descriptor()
247 if (!this->is_descriptor_opened_
)
249 this->descriptor_
= open_descriptor(this->descriptor_
,
252 if (this->descriptor_
< 0)
253 gold_fatal(_("could not reopen file %s"), this->name_
.c_str());
254 this->is_descriptor_opened_
= true;
258 // Release the file. This is called when we are done with the file in
264 gold_assert(this->is_locked());
266 if (!parameters
->options_valid() || parameters
->options().stats())
268 file_counts_initialize_lock
.initialize();
269 Hold_optional_lock
hl(file_counts_lock
);
270 File_read::total_mapped_bytes
+= this->mapped_bytes_
;
271 File_read::current_mapped_bytes
+= this->mapped_bytes_
;
272 if (File_read::current_mapped_bytes
> File_read::maximum_mapped_bytes
)
273 File_read::maximum_mapped_bytes
= File_read::current_mapped_bytes
;
276 this->mapped_bytes_
= 0;
278 // Only clear views if there is only one attached object. Otherwise
279 // we waste time trying to clear cached archive views. Similarly
280 // for releasing the descriptor.
281 if (this->object_count_
<= 1)
283 this->clear_views(CLEAR_VIEWS_NORMAL
);
284 if (this->is_descriptor_opened_
)
286 release_descriptor(this->descriptor_
, false);
287 this->is_descriptor_opened_
= false;
291 this->released_
= true;
297 File_read::lock(const Task
* task
)
299 gold_assert(this->released_
);
300 gold_debug(DEBUG_FILES
, "Locking file \"%s\"", this->name_
.c_str());
301 this->token_
.add_writer(task
);
302 this->released_
= false;
308 File_read::unlock(const Task
* task
)
310 gold_debug(DEBUG_FILES
, "Unlocking file \"%s\"", this->name_
.c_str());
312 this->token_
.remove_writer(task
);
315 // Return whether the file is locked.
318 File_read::is_locked() const
320 if (!this->token_
.is_writable())
322 // The file is not locked, so it should have been released.
323 gold_assert(this->released_
);
327 // See if we have a view which covers the file starting at START for
328 // SIZE bytes. Return a pointer to the View if found, NULL if not.
329 // If BYTESHIFT is not -1U, the returned View must have the specified
330 // byte shift; otherwise, it may have any byte shift. If VSHIFTED is
331 // not NULL, this sets *VSHIFTED to a view which would have worked if
332 // not for the requested BYTESHIFT.
334 inline File_read::View
*
335 File_read::find_view(off_t start
, section_size_type size
,
336 unsigned int byteshift
, File_read::View
** vshifted
) const
338 gold_assert(start
<= this->size_
339 && (static_cast<unsigned long long>(size
)
340 <= static_cast<unsigned long long>(this->size_
- start
)));
342 if (vshifted
!= NULL
)
345 // If we have the whole file mmapped, and the alignment is right,
347 if (this->whole_file_view_
)
348 if (byteshift
== -1U || byteshift
== 0)
349 return this->whole_file_view_
;
351 off_t page
= File_read::page_offset(start
);
353 unsigned int bszero
= 0;
354 Views::const_iterator p
= this->views_
.upper_bound(std::make_pair(page
- 1,
357 while (p
!= this->views_
.end() && p
->first
.first
<= page
)
359 if (p
->second
->start() <= start
360 && (p
->second
->start() + static_cast<off_t
>(p
->second
->size())
361 >= start
+ static_cast<off_t
>(size
)))
363 if (byteshift
== -1U || byteshift
== p
->second
->byteshift())
365 p
->second
->set_accessed();
369 if (vshifted
!= NULL
&& *vshifted
== NULL
)
370 *vshifted
= p
->second
;
379 // Read SIZE bytes from the file starting at offset START. Read into
383 File_read::do_read(off_t start
, section_size_type size
, void* p
)
386 if (this->whole_file_view_
!= NULL
)
388 // See PR 23765 for an example of a testcase that triggers this error.
389 if (((ssize_t
) start
) < 0)
390 gold_fatal(_("%s: read failed, starting offset (%#llx) less than zero"),
391 this->filename().c_str(),
392 static_cast<long long>(start
));
394 bytes
= this->size_
- start
;
395 if (static_cast<section_size_type
>(bytes
) >= size
)
397 memcpy(p
, this->whole_file_view_
->data() + start
, size
);
403 this->reopen_descriptor();
405 char *read_ptr
= static_cast<char *>(p
);
406 off_t read_pos
= start
;
407 size_t to_read
= size
;
410 bytes
= ::pread(this->descriptor_
, read_ptr
, to_read
, read_pos
);
412 gold_fatal(_("%s: pread failed: %s"),
413 this->filename().c_str(), strerror(errno
));
423 bytes
= size
- to_read
;
426 gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"),
427 this->filename().c_str(),
428 static_cast<long long>(bytes
),
429 static_cast<long long>(size
),
430 static_cast<long long>(start
));
433 // Read data from the file.
436 File_read::read(off_t start
, section_size_type size
, void* p
)
438 const File_read::View
* pv
= this->find_view(start
, size
, -1U, NULL
);
441 memcpy(p
, pv
->data() + (start
- pv
->start() + pv
->byteshift()), size
);
445 this->do_read(start
, size
, p
);
448 // Add a new view. There may already be an existing view at this
449 // offset. If there is, the new view will be larger, and should
450 // replace the old view.
453 File_read::add_view(File_read::View
* v
)
455 std::pair
<Views::iterator
, bool> ins
=
456 this->views_
.insert(std::make_pair(std::make_pair(v
->start(),
462 // There was an existing view at this offset. It must not be large
463 // enough. We can't delete it here, since something might be using
464 // it; we put it on a list to be deleted when the file is unlocked.
465 File_read::View
* vold
= ins
.first
->second
;
466 gold_assert(vold
->size() < v
->size());
467 if (vold
->should_cache())
472 this->saved_views_
.push_back(vold
);
474 ins
.first
->second
= v
;
477 // Make a new view with a specified byteshift, reading the data from
481 File_read::make_view(off_t start
, section_size_type size
,
482 unsigned int byteshift
, bool cache
)
484 gold_assert(size
> 0);
485 gold_assert(start
<= this->size_
486 && (static_cast<unsigned long long>(size
)
487 <= static_cast<unsigned long long>(this->size_
- start
)));
489 off_t poff
= File_read::page_offset(start
);
491 section_size_type psize
= File_read::pages(size
+ (start
- poff
));
493 if (poff
+ static_cast<off_t
>(psize
) >= this->size_
)
495 psize
= this->size_
- poff
;
496 gold_assert(psize
>= size
);
500 View::Data_ownership ownership
;
503 p
= malloc(psize
+ byteshift
);
506 memset(p
, 0, byteshift
);
507 this->do_read(poff
, psize
, static_cast<unsigned char*>(p
) + byteshift
);
508 ownership
= View::DATA_ALLOCATED_ARRAY
;
512 this->reopen_descriptor();
513 p
= ::mmap(NULL
, psize
, PROT_READ
, MAP_PRIVATE
, this->descriptor_
, poff
);
516 ownership
= View::DATA_MMAPPED
;
517 this->mapped_bytes_
+= psize
;
524 this->do_read(poff
, psize
, p
);
525 ownership
= View::DATA_ALLOCATED_ARRAY
;
529 const unsigned char* pbytes
= static_cast<const unsigned char*>(p
);
530 File_read::View
* v
= new File_read::View(poff
, psize
, pbytes
, byteshift
,
538 // Find a View or make a new one, shifted as required by the file
539 // offset OFFSET and ALIGNED.
542 File_read::find_or_make_view(off_t offset
, off_t start
,
543 section_size_type size
, bool aligned
, bool cache
)
545 // Check that start and end of the view are within the file.
546 if (start
> this->size_
547 || (static_cast<unsigned long long>(size
)
548 > static_cast<unsigned long long>(this->size_
- start
)))
549 gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds "
550 "size of file; the file may be corrupt"),
551 this->filename().c_str(),
552 static_cast<long long>(size
),
553 static_cast<long long>(start
));
555 unsigned int byteshift
;
560 unsigned int target_size
= (!parameters
->target_valid()
562 : parameters
->target().get_size());
563 byteshift
= offset
& ((target_size
/ 8) - 1);
565 // Set BYTESHIFT to the number of dummy bytes which must be
566 // inserted before the data in order for this data to be
569 byteshift
= (target_size
/ 8) - byteshift
;
572 // If --map-whole-files is set, make sure we have a
573 // whole file view. Options may not yet be ready, e.g.,
574 // when reading a version script. We then default to
575 // --no-map-whole-files.
576 if (this->whole_file_view_
== NULL
577 && parameters
->options_valid()
578 && parameters
->options().map_whole_files())
579 this->whole_file_view_
= this->make_view(0, this->size_
, 0, cache
);
581 // Try to find a View with the required BYTESHIFT.
582 File_read::View
* vshifted
;
583 File_read::View
* v
= this->find_view(offset
+ start
, size
,
584 aligned
? byteshift
: -1U,
593 // If VSHIFTED is not NULL, then it has the data we need, but with
594 // the wrong byteshift.
598 gold_assert(aligned
);
600 unsigned char* pbytes
;
601 pbytes
= static_cast<unsigned char*>(malloc(v
->size() + byteshift
));
604 memset(pbytes
, 0, byteshift
);
605 memcpy(pbytes
+ byteshift
, v
->data() + v
->byteshift(), v
->size());
607 File_read::View
* shifted_view
=
608 new File_read::View(v
->start(), v
->size(), pbytes
, byteshift
,
609 cache
, View::DATA_ALLOCATED_ARRAY
);
611 this->add_view(shifted_view
);
615 // Make a new view. If we don't need an aligned view, use a
616 // byteshift of 0, so that we can use mmap.
617 return this->make_view(offset
+ start
, size
,
618 aligned
? byteshift
: 0,
622 // Get a view into the file.
625 File_read::get_view(off_t offset
, off_t start
, section_size_type size
,
626 bool aligned
, bool cache
)
628 File_read::View
* pv
= this->find_or_make_view(offset
, start
, size
,
630 return pv
->data() + (offset
+ start
- pv
->start() + pv
->byteshift());
634 File_read::get_lasting_view(off_t offset
, off_t start
, section_size_type size
,
635 bool aligned
, bool cache
)
637 File_read::View
* pv
= this->find_or_make_view(offset
, start
, size
,
640 return new File_view(*this, pv
,
642 + (offset
+ start
- pv
->start() + pv
->byteshift())));
645 // Use readv to read COUNT entries from RM starting at START. BASE
646 // must be added to all file offsets in RM.
649 File_read::do_readv(off_t base
, const Read_multiple
& rm
, size_t start
,
652 unsigned char discard
[File_read::page_size
];
653 iovec iov
[File_read::max_readv_entries
* 2];
654 size_t iov_index
= 0;
656 off_t first_offset
= rm
[start
].file_offset
;
657 off_t last_offset
= first_offset
;
659 for (size_t i
= 0; i
< count
; ++i
)
661 const Read_multiple_entry
& i_entry(rm
[start
+ i
]);
663 if (i_entry
.file_offset
> last_offset
)
665 size_t skip
= i_entry
.file_offset
- last_offset
;
666 gold_assert(skip
<= sizeof discard
);
668 iov
[iov_index
].iov_base
= discard
;
669 iov
[iov_index
].iov_len
= skip
;
675 iov
[iov_index
].iov_base
= i_entry
.buffer
;
676 iov
[iov_index
].iov_len
= i_entry
.size
;
679 want
+= i_entry
.size
;
681 last_offset
= i_entry
.file_offset
+ i_entry
.size
;
684 this->reopen_descriptor();
686 gold_assert(iov_index
< sizeof iov
/ sizeof iov
[0]);
688 if (::lseek(this->descriptor_
, base
+ first_offset
, SEEK_SET
) < 0)
689 gold_fatal(_("%s: lseek failed: %s"),
690 this->filename().c_str(), strerror(errno
));
692 ssize_t got
= ::readv(this->descriptor_
, iov
, iov_index
);
695 gold_fatal(_("%s: readv failed: %s"),
696 this->filename().c_str(), strerror(errno
));
698 gold_fatal(_("%s: file too short: read only %zd of %zd bytes at %lld"),
699 this->filename().c_str(),
700 got
, want
, static_cast<long long>(base
+ first_offset
));
705 #if !defined(HAVE_READV)
706 #define GOLD_IOV_MAX 1
707 #elif defined(IOV_MAX)
708 #define GOLD_IOV_MAX IOV_MAX
710 #define GOLD_IOV_MAX (File_read::max_readv_entries * 2)
713 // Read several pieces of data from the file.
716 File_read::read_multiple(off_t base
, const Read_multiple
& rm
)
718 static size_t iov_max
= GOLD_IOV_MAX
;
719 size_t count
= rm
.size();
723 // Find up to MAX_READV_ENTRIES consecutive entries which are
724 // less than one page apart.
725 const Read_multiple_entry
& i_entry(rm
[i
]);
726 off_t i_off
= i_entry
.file_offset
;
727 off_t end_off
= i_off
+ i_entry
.size
;
729 for (j
= i
+ 1; j
< count
; ++j
)
731 if (j
- i
>= File_read::max_readv_entries
|| j
- i
>= iov_max
/ 2)
733 const Read_multiple_entry
& j_entry(rm
[j
]);
734 off_t j_off
= j_entry
.file_offset
;
735 gold_assert(j_off
>= end_off
);
736 off_t j_end_off
= j_off
+ j_entry
.size
;
737 if (j_end_off
- end_off
>= File_read::page_size
)
743 this->read(base
+ i_off
, i_entry
.size
, i_entry
.buffer
);
746 File_read::View
* view
= this->find_view(base
+ i_off
,
750 this->do_readv(base
, rm
, i
, j
- i
);
753 const unsigned char* v
= (view
->data()
754 + (base
+ i_off
- view
->start()
755 + view
->byteshift()));
756 for (size_t k
= i
; k
< j
; ++k
)
758 const Read_multiple_entry
& k_entry(rm
[k
]);
759 gold_assert((convert_to_section_size_type(k_entry
.file_offset
762 <= convert_to_section_size_type(end_off
764 memcpy(k_entry
.buffer
,
765 v
+ (k_entry
.file_offset
- i_off
),
775 // Mark all views as no longer cached.
778 File_read::clear_view_cache_marks()
780 // Just ignore this if there are multiple objects associated with
781 // the file. Otherwise we will wind up uncaching and freeing some
782 // views for other objects.
783 if (this->object_count_
> 1)
786 for (Views::iterator p
= this->views_
.begin();
787 p
!= this->views_
.end();
789 p
->second
->clear_cache();
790 for (Saved_views::iterator p
= this->saved_views_
.begin();
791 p
!= this->saved_views_
.end();
796 // Remove all the file views. For a file which has multiple
797 // associated objects (i.e., an archive), we keep accessed views
798 // around until next time, in the hopes that they will be useful for
802 File_read::clear_views(Clear_views_mode mode
)
804 bool keep_files_mapped
= (parameters
->options_valid()
805 && parameters
->options().keep_files_mapped());
806 Views::iterator p
= this->views_
.begin();
807 while (p
!= this->views_
.end())
810 if (p
->second
->is_locked() || p
->second
->is_permanent_view())
811 should_delete
= false;
812 else if (mode
== CLEAR_VIEWS_ALL
)
813 should_delete
= true;
814 else if ((p
->second
->should_cache()
815 || p
->second
== this->whole_file_view_
)
816 && keep_files_mapped
)
817 should_delete
= false;
818 else if (this->object_count_
> 1
819 && p
->second
->accessed()
820 && mode
!= CLEAR_VIEWS_ARCHIVE
)
821 should_delete
= false;
823 should_delete
= true;
827 if (p
->second
== this->whole_file_view_
)
828 this->whole_file_view_
= NULL
;
831 // map::erase invalidates only the iterator to the deleted
833 Views::iterator pe
= p
;
835 this->views_
.erase(pe
);
839 p
->second
->clear_accessed();
844 Saved_views::iterator q
= this->saved_views_
.begin();
845 while (q
!= this->saved_views_
.end())
847 if (!(*q
)->is_locked())
850 q
= this->saved_views_
.erase(q
);
854 gold_assert(mode
!= CLEAR_VIEWS_ALL
);
860 // Print statistical information to stderr. This is used for --stats.
863 File_read::print_stats()
865 fprintf(stderr
, _("%s: total bytes mapped for read: %llu\n"),
866 program_name
, File_read::total_mapped_bytes
);
867 fprintf(stderr
, _("%s: maximum bytes mapped for read at one time: %llu\n"),
868 program_name
, File_read::maximum_mapped_bytes
);
873 File_view::~File_view()
875 gold_assert(this->file_
.is_locked());
876 this->view_
->unlock();
881 // Create a file given just the filename.
883 Input_file::Input_file(const char* name
)
884 : found_name_(), file_(), is_in_sysroot_(false), format_(FORMAT_NONE
)
886 this->input_argument_
=
887 new Input_file_argument(name
, Input_file_argument::INPUT_FILE_TYPE_FILE
,
888 "", false, Position_dependent_options());
891 // Create a file for testing.
893 Input_file::Input_file(const Task
* task
, const char* name
,
894 const unsigned char* contents
, off_t size
)
897 this->input_argument_
=
898 new Input_file_argument(name
, Input_file_argument::INPUT_FILE_TYPE_FILE
,
899 "", false, Position_dependent_options());
900 bool ok
= this->file_
.open(task
, name
, contents
, size
);
904 // Return the position dependent options in force for this file.
906 const Position_dependent_options
&
907 Input_file::options() const
909 return this->input_argument_
->options();
912 // Return the name given by the user. For -lc this will return "c".
915 Input_file::name() const
917 return this->input_argument_
->name();
920 // Return whether this file is in a system directory.
923 Input_file::is_in_system_directory() const
925 if (this->is_in_sysroot())
927 return parameters
->options().is_in_system_directory(this->filename());
930 // Return whether we are only reading symbols.
933 Input_file::just_symbols() const
935 return this->input_argument_
->just_symbols();
938 // Return whether this is a file that we will search for in the list
942 Input_file::will_search_for() const
944 return (!IS_ABSOLUTE_PATH(this->input_argument_
->name())
945 && (this->input_argument_
->is_lib()
946 || this->input_argument_
->is_searched_file()
947 || this->input_argument_
->extra_search_path() != NULL
));
950 // Return the file last modification time. Calls gold_fatal if the stat
951 // system call failed.
954 File_read::get_mtime()
956 struct stat file_stat
;
957 this->reopen_descriptor();
959 if (fstat(this->descriptor_
, &file_stat
) < 0)
960 gold_fatal(_("%s: stat failed: %s"), this->name_
.c_str(),
962 #ifdef HAVE_STAT_ST_MTIM
963 return Timespec(file_stat
.st_mtim
.tv_sec
, file_stat
.st_mtim
.tv_nsec
);
965 return Timespec(file_stat
.st_mtime
, 0);
969 // Try to find a file in the extra search dirs. Returns true on success.
972 Input_file::try_extra_search_path(int* pindex
,
973 const Input_file_argument
* input_argument
,
974 std::string filename
, std::string
* found_name
,
977 if (input_argument
->extra_search_path() == NULL
)
980 std::string name
= input_argument
->extra_search_path();
981 if (!IS_DIR_SEPARATOR(name
[name
.length() - 1]))
985 struct stat dummy_stat
;
986 if (*pindex
> 0 || ::stat(name
.c_str(), &dummy_stat
) < 0)
989 *found_name
= filename
;
994 // Find the actual file.
995 // If the filename is not absolute, we assume it is in the current
996 // directory *except* when:
997 // A) input_argument_->is_lib() is true;
998 // B) input_argument_->is_searched_file() is true; or
999 // C) input_argument_->extra_search_path() is not empty.
1000 // In each, we look in extra_search_path + library_path to find
1001 // the file location, rather than the current directory.
1004 Input_file::find_file(const Dirsearch
& dirpath
, int* pindex
,
1005 const Input_file_argument
* input_argument
,
1006 bool* is_in_sysroot
,
1007 std::string
* found_name
, std::string
* namep
)
1011 // Case 1: name is an absolute file, just try to open it
1012 // Case 2: name is relative but is_lib is false, is_searched_file is false,
1013 // and extra_search_path is empty
1014 if (IS_ABSOLUTE_PATH(input_argument
->name())
1015 || (!input_argument
->is_lib()
1016 && !input_argument
->is_searched_file()
1017 && input_argument
->extra_search_path() == NULL
))
1019 name
= input_argument
->name();
1024 // Case 3: is_lib is true or is_searched_file is true
1025 else if (input_argument
->is_lib()
1026 || input_argument
->is_searched_file())
1028 std::vector
<std::string
> names
;
1030 if (input_argument
->is_lib())
1032 std::string prefix
= "lib";
1033 prefix
+= input_argument
->name();
1034 if (parameters
->options().is_static()
1035 || !input_argument
->options().Bdynamic())
1036 names
.push_back(prefix
+ ".a");
1039 names
.push_back(prefix
+ ".so");
1040 names
.push_back(prefix
+ ".a");
1044 names
.push_back(input_argument
->name());
1046 for (std::vector
<std::string
>::const_iterator n
= names
.begin();
1049 if (Input_file::try_extra_search_path(pindex
, input_argument
, *n
,
1053 // It is not in the extra_search_path.
1054 name
= dirpath
.find(names
, is_in_sysroot
, pindex
, found_name
);
1057 gold_error(_("cannot find %s%s"),
1058 input_argument
->is_lib() ? "-l" : "",
1059 input_argument
->name());
1065 // Case 4: extra_search_path is not empty
1068 gold_assert(input_argument
->extra_search_path() != NULL
);
1070 if (try_extra_search_path(pindex
, input_argument
, input_argument
->name(),
1074 // extra_search_path failed, so check the normal search-path.
1075 int index
= *pindex
;
1078 name
= dirpath
.find(std::vector
<std::string
>(1, input_argument
->name()),
1079 is_in_sysroot
, &index
, found_name
);
1082 gold_error(_("cannot find %s"),
1083 input_argument
->name());
1087 *pindex
= index
+ 1;
1095 Input_file::open(const Dirsearch
& dirpath
, const Task
* task
, int* pindex
)
1098 if (!Input_file::find_file(dirpath
, pindex
, this->input_argument_
,
1099 &this->is_in_sysroot_
, &this->found_name_
, &name
))
1102 // Now that we've figured out where the file lives, try to open it.
1104 General_options::Object_format format
=
1105 this->input_argument_
->options().format_enum();
1107 if (format
== General_options::OBJECT_FORMAT_ELF
)
1109 ok
= this->file_
.open(task
, name
);
1110 this->format_
= FORMAT_ELF
;
1114 gold_assert(format
== General_options::OBJECT_FORMAT_BINARY
);
1115 ok
= this->open_binary(task
, name
);
1116 this->format_
= FORMAT_BINARY
;
1121 gold_error(_("cannot open %s: %s"),
1122 name
.c_str(), strerror(errno
));
1123 this->format_
= FORMAT_NONE
;
1130 // Open a file for --format binary.
1133 Input_file::open_binary(const Task
* task
, const std::string
& name
)
1135 // In order to open a binary file, we need machine code, size, and
1136 // endianness. We may not have a valid target at this point, in
1137 // which case we use the default target.
1138 parameters_force_valid_target();
1139 const Target
& target(parameters
->target());
1141 Binary_to_elf
binary_to_elf(target
.machine_code(),
1143 target
.is_big_endian(),
1145 if (!binary_to_elf
.convert(task
))
1147 return this->file_
.open(task
, name
, binary_to_elf
.converted_data_leak(),
1148 binary_to_elf
.converted_size());
1152 File_read::record_file_read(const std::string
& name
)
1154 File_read::files_read
.push_back(name
);
1158 File_read::write_dependency_file(const char* dependency_file_name
,
1159 const char* output_file_name
)
1161 FILE *depfile
= fopen(dependency_file_name
, "w");
1163 fprintf(depfile
, "%s:", output_file_name
);
1164 for (std::vector
<std::string
>::const_iterator it
= files_read
.begin();
1165 it
!= files_read
.end();
1167 fprintf(depfile
, " \\\n %s", it
->c_str());
1168 fprintf(depfile
, "\n");
1170 for (std::vector
<std::string
>::const_iterator it
= files_read
.begin();
1171 it
!= files_read
.end();
1173 fprintf(depfile
, "\n%s:\n", it
->c_str());
1178 } // End namespace gold.