1 // archive.cc -- archive support for gold
3 // Copyright (C) 2006-2016 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.
29 #include "libiberty.h"
30 #include "filenames.h"
42 #include "incremental.h"
47 // Library_base methods.
49 // Determine whether a definition of SYM_NAME should cause an archive
50 // library member to be included in the link. Returns SHOULD_INCLUDE_YES
51 // if the symbol is referenced but not defined, SHOULD_INCLUDE_NO if the
52 // symbol is already defined, and SHOULD_INCLUDE_UNKNOWN if the symbol is
53 // neither referenced nor defined.
55 Library_base::Should_include
56 Library_base::should_include_member(Symbol_table
* symtab
, Layout
* layout
,
57 const char* sym_name
, Symbol
** symp
,
58 std::string
* why
, char** tmpbufp
,
61 // In an object file, and therefore in an archive map, an
62 // '@' in the name separates the symbol name from the
63 // version name. If there are two '@' characters, this is
64 // the default version.
65 char* tmpbuf
= *tmpbufp
;
66 const char* ver
= strchr(sym_name
, '@');
70 size_t symlen
= ver
- sym_name
;
71 if (symlen
+ 1 > *tmpbuflen
)
73 tmpbuf
= static_cast<char*>(xrealloc(tmpbuf
, symlen
+ 1));
75 *tmpbuflen
= symlen
+ 1;
77 memcpy(tmpbuf
, sym_name
, symlen
);
78 tmpbuf
[symlen
] = '\0';
89 Symbol
* sym
= symtab
->lookup(sym_name
, ver
);
93 || !sym
->is_undefined()
94 || sym
->binding() == elfcpp::STB_WEAK
))
95 sym
= symtab
->lookup(sym_name
, NULL
);
101 if (!sym
->is_undefined())
102 return Library_base::SHOULD_INCLUDE_NO
;
104 // PR 12001: Do not include an archive when the undefined
105 // symbol has actually been defined on the command line.
106 if (layout
->script_options()->is_pending_assignment(sym_name
))
107 return Library_base::SHOULD_INCLUDE_NO
;
109 // If the symbol is weak undefined, we still need to check
110 // for other reasons (like a -u option).
111 if (sym
->binding() != elfcpp::STB_WEAK
)
112 return Library_base::SHOULD_INCLUDE_YES
;
115 // Check whether the symbol was named in a -u option.
116 if (parameters
->options().is_undefined(sym_name
))
120 return Library_base::SHOULD_INCLUDE_YES
;
123 if (parameters
->options().is_export_dynamic_symbol(sym_name
))
125 *why
= "--export-dynamic-symbol ";
127 return Library_base::SHOULD_INCLUDE_YES
;
130 if (layout
->script_options()->is_referenced(sym_name
))
132 size_t alc
= 100 + strlen(sym_name
);
133 char* buf
= new char[alc
];
134 snprintf(buf
, alc
, _("script or expression reference to %s"),
138 return Library_base::SHOULD_INCLUDE_YES
;
141 if (!parameters
->options().relocatable())
143 const char* entry_sym
= parameters
->entry();
144 if (entry_sym
!= NULL
&& strcmp(sym_name
, entry_sym
) == 0)
146 *why
= "entry symbol ";
148 return Library_base::SHOULD_INCLUDE_YES
;
152 return Library_base::SHOULD_INCLUDE_UNKNOWN
;
155 // The header of an entry in the archive. This is all readable text,
156 // padded with spaces where necessary. If the contents of an archive
157 // are all text file, the entire archive is readable.
159 struct Archive::Archive_header
163 // The file modification time.
165 // The user's UID in decimal.
167 // The user's GID in decimal.
169 // The file mode in octal.
171 // The file size in decimal.
173 // The final magic code.
177 // Class Archive static variables.
178 unsigned int Archive::total_archives
;
179 unsigned int Archive::total_members
;
180 unsigned int Archive::total_members_loaded
;
184 const char Archive::armag
[sarmag
] =
186 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
189 const char Archive::armagt
[sarmag
] =
191 '!', '<', 't', 'h', 'i', 'n', '>', '\n'
194 const char Archive::arfmag
[2] = { '`', '\n' };
196 const char Archive::sym64name
[7] = { '/', 'S', 'Y', 'M', '6', '4', '/' };
198 Archive::Archive(const std::string
& name
, Input_file
* input_file
,
199 bool is_thin_archive
, Dirsearch
* dirpath
, Task
* task
)
200 : Library_base(task
), name_(name
), input_file_(input_file
), armap_(),
201 armap_names_(), extended_names_(), armap_checked_(), seen_offsets_(),
202 members_(), is_thin_archive_(is_thin_archive
), included_member_(false),
203 nested_archives_(), dirpath_(dirpath
), num_members_(0),
204 included_all_members_(false)
207 parameters
->options().check_excluded_libs(input_file
->found_name());
210 // Set up the archive: read the symbol map and the extended name
216 // We need to ignore empty archives.
217 if (this->input_file_
->file().filesize() == sarmag
)
220 // The first member of the archive should be the symbol table.
221 std::string armap_name
;
222 off_t header_size
= this->read_header(sarmag
, false, &armap_name
, NULL
);
223 if (header_size
== -1)
226 section_size_type armap_size
= convert_to_section_size_type(header_size
);
228 if (armap_name
.empty())
230 this->read_armap
<32>(sarmag
+ sizeof(Archive_header
), armap_size
);
231 off
= sarmag
+ sizeof(Archive_header
) + armap_size
;
233 else if (armap_name
== "/SYM64/")
235 this->read_armap
<64>(sarmag
+ sizeof(Archive_header
), armap_size
);
236 off
= sarmag
+ sizeof(Archive_header
) + armap_size
;
238 else if (!this->input_file_
->options().whole_archive())
239 gold_error(_("%s: no archive symbol table (run ranlib)"),
240 this->name().c_str());
242 // See if there is an extended name table. We cache these views
243 // because it is likely that we will want to read the following
244 // header in the add_symbols routine.
248 header_size
= this->read_header(off
, true, &xname
, NULL
);
249 if (header_size
== -1)
252 section_size_type extended_size
= convert_to_section_size_type(header_size
);
255 const unsigned char* p
= this->get_view(off
+ sizeof(Archive_header
),
256 extended_size
, false, true);
257 const char* px
= reinterpret_cast<const char*>(p
);
258 this->extended_names_
.assign(px
, extended_size
);
260 bool preread_syms
= (parameters
->options().threads()
261 && parameters
->options().preread_archive_symbols());
262 #ifndef ENABLE_THREADS
263 preread_syms
= false;
265 if (parameters
->options().has_plugins())
266 preread_syms
= false;
269 this->read_all_symbols();
272 // Unlock any nested archives.
275 Archive::unlock_nested_archives()
277 for (Nested_archive_table::iterator p
= this->nested_archives_
.begin();
278 p
!= this->nested_archives_
.end();
281 p
->second
->unlock(this->task_
);
285 // Read the archive symbol map.
287 template<int mapsize
>
289 Archive::read_armap(off_t start
, section_size_type size
)
291 // To count the total number of archive members, we'll just count
292 // the number of times the file offset changes. Since most archives
293 // group the symbols in the armap by object, this ought to give us
294 // an accurate count.
295 off_t last_seen_offset
= -1;
297 // Read in the entire armap.
298 const unsigned char* p
= this->get_view(start
, size
, true, false);
300 // Numbers in the armap are always big-endian.
301 typedef typename
elfcpp::Elf_types
<mapsize
>::Elf_Addr Entry_type
;
302 const Entry_type
* pword
= reinterpret_cast<const Entry_type
*>(p
);
303 unsigned long nsyms
= convert_types
<unsigned long, Entry_type
>(
304 elfcpp::Swap
<mapsize
, true>::readval(pword
));
307 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
308 const char* pnames
= reinterpret_cast<const char*>(pword
+ nsyms
);
309 section_size_type names_size
=
310 reinterpret_cast<const char*>(p
) + size
- pnames
;
311 this->armap_names_
.assign(pnames
, names_size
);
313 this->armap_
.resize(nsyms
);
315 section_offset_type name_offset
= 0;
316 for (unsigned long i
= 0; i
< nsyms
; ++i
)
318 this->armap_
[i
].name_offset
= name_offset
;
319 this->armap_
[i
].file_offset
= convert_types
<off_t
, Entry_type
>(
320 elfcpp::Swap
<mapsize
, true>::readval(pword
));
321 name_offset
+= strlen(pnames
+ name_offset
) + 1;
323 if (this->armap_
[i
].file_offset
!= last_seen_offset
)
325 last_seen_offset
= this->armap_
[i
].file_offset
;
326 ++this->num_members_
;
330 if (static_cast<section_size_type
>(name_offset
) > names_size
)
331 gold_error(_("%s: bad archive symbol table names"),
332 this->name().c_str());
334 // This array keeps track of which symbols are for archive elements
335 // which we have already included in the link.
336 this->armap_checked_
.resize(nsyms
);
339 // Read the header of an archive member at OFF. Fail if something
340 // goes wrong. Return the size of the member. Set *PNAME to the name
344 Archive::read_header(off_t off
, bool cache
, std::string
* pname
,
347 const unsigned char* p
= this->get_view(off
, sizeof(Archive_header
), true,
349 const Archive_header
* hdr
= reinterpret_cast<const Archive_header
*>(p
);
350 return this->interpret_header(hdr
, off
, pname
, nested_off
);
353 // Interpret the header of HDR, the header of the archive member at
354 // file offset OFF. Return the size of the member, or -1 if something
355 // has gone wrong. Set *PNAME to the name of the member.
358 Archive::interpret_header(const Archive_header
* hdr
, off_t off
,
359 std::string
* pname
, off_t
* nested_off
) const
361 if (memcmp(hdr
->ar_fmag
, arfmag
, sizeof arfmag
) != 0)
363 gold_error(_("%s: malformed archive header at %zu"),
364 this->name().c_str(), static_cast<size_t>(off
));
368 const int size_string_size
= sizeof hdr
->ar_size
;
369 char size_string
[size_string_size
+ 1];
370 memcpy(size_string
, hdr
->ar_size
, size_string_size
);
371 char* ps
= size_string
+ size_string_size
;
372 while (ps
[-1] == ' ')
378 off_t member_size
= strtol(size_string
, &end
, 10);
381 || (member_size
== LONG_MAX
&& errno
== ERANGE
))
383 gold_error(_("%s: malformed archive header size at %zu"),
384 this->name().c_str(), static_cast<size_t>(off
));
388 if (hdr
->ar_name
[0] != '/')
390 const char* name_end
= strchr(hdr
->ar_name
, '/');
392 || name_end
- hdr
->ar_name
>= static_cast<int>(sizeof hdr
->ar_name
))
394 gold_error(_("%s: malformed archive header name at %zu"),
395 this->name().c_str(), static_cast<size_t>(off
));
398 pname
->assign(hdr
->ar_name
, name_end
- hdr
->ar_name
);
399 if (nested_off
!= NULL
)
402 else if (hdr
->ar_name
[1] == ' ')
404 // This is the symbol table.
408 else if (memcmp(hdr
->ar_name
, sym64name
, sizeof sym64name
) == 0)
410 // This is the symbol table, 64-bit version.
411 pname
->assign(sym64name
, sizeof sym64name
);
413 else if (hdr
->ar_name
[1] == '/')
415 // This is the extended name table.
416 pname
->assign(1, '/');
421 long x
= strtol(hdr
->ar_name
+ 1, &end
, 10);
424 y
= strtol(end
+ 1, &end
, 10);
427 || (x
== LONG_MAX
&& errno
== ERANGE
)
428 || static_cast<size_t>(x
) >= this->extended_names_
.size())
430 gold_error(_("%s: bad extended name index at %zu"),
431 this->name().c_str(), static_cast<size_t>(off
));
435 const char* name
= this->extended_names_
.data() + x
;
436 const char* name_end
= strchr(name
, '\n');
437 if (static_cast<size_t>(name_end
- name
) > this->extended_names_
.size()
438 || name_end
[-1] != '/')
440 gold_error(_("%s: bad extended name entry at header %zu"),
441 this->name().c_str(), static_cast<size_t>(off
));
444 pname
->assign(name
, name_end
- 1 - name
);
445 if (nested_off
!= NULL
)
452 // An archive member iterator.
454 class Archive::const_iterator
457 // The header of an archive member. This is what this iterator
461 // The name of the member.
463 // The file offset of the member.
465 // The file offset of a nested archive member.
467 // The size of the member.
471 const_iterator(Archive
* archive
, off_t off
)
472 : archive_(archive
), off_(off
)
473 { this->read_next_header(); }
477 { return this->header_
; }
481 { return &this->header_
; }
486 if (this->off_
== this->archive_
->file().filesize())
488 this->off_
+= sizeof(Archive_header
);
489 if (!this->archive_
->is_thin_archive())
490 this->off_
+= this->header_
.size
;
491 if ((this->off_
& 1) != 0)
493 this->read_next_header();
500 const_iterator ret
= *this;
506 operator==(const const_iterator p
) const
507 { return this->off_
== p
->off
; }
510 operator!=(const const_iterator p
) const
511 { return this->off_
!= p
->off
; }
517 // The underlying archive.
519 // The current offset in the file.
521 // The current archive header.
525 // Read the next archive header.
528 Archive::const_iterator::read_next_header()
530 off_t filesize
= this->archive_
->file().filesize();
533 if (filesize
- this->off_
< static_cast<off_t
>(sizeof(Archive_header
)))
535 if (filesize
!= this->off_
)
537 gold_error(_("%s: short archive header at %zu"),
538 this->archive_
->filename().c_str(),
539 static_cast<size_t>(this->off_
));
540 this->off_
= filesize
;
542 this->header_
.off
= filesize
;
546 unsigned char buf
[sizeof(Archive_header
)];
547 this->archive_
->file().read(this->off_
, sizeof(Archive_header
), buf
);
549 const Archive_header
* hdr
= reinterpret_cast<const Archive_header
*>(buf
);
550 off_t size
= this->archive_
->interpret_header(hdr
, this->off_
,
552 &this->header_
.nested_off
);
555 this->header_
.off
= filesize
;
559 this->header_
.size
= size
;
560 this->header_
.off
= this->off_
;
562 // Skip special members.
563 if (!this->header_
.name
.empty()
564 && this->header_
.name
!= "/"
565 && this->header_
.name
!= "/SYM64/")
568 this->off_
+= sizeof(Archive_header
) + this->header_
.size
;
569 if ((this->off_
& 1) != 0)
576 Archive::const_iterator
579 return Archive::const_iterator(this, sarmag
);
584 Archive::const_iterator
587 return Archive::const_iterator(this, this->input_file_
->file().filesize());
590 // Get the file and offset for an archive member, which may be an
591 // external member of a thin archive. Set *INPUT_FILE to the
592 // file containing the actual member, *MEMOFF to the offset
593 // within that file (0 if not a nested archive), and *MEMBER_NAME
594 // to the name of the archive member. Return TRUE on success.
597 Archive::get_file_and_offset(off_t off
, Input_file
** input_file
, off_t
* memoff
,
598 off_t
* memsize
, std::string
* member_name
)
602 *memsize
= this->read_header(off
, false, member_name
, &nested_off
);
606 *input_file
= this->input_file_
;
607 *memoff
= off
+ static_cast<off_t
>(sizeof(Archive_header
));
609 if (!this->is_thin_archive_
)
612 // Adjust a relative pathname so that it is relative
613 // to the directory containing the archive.
614 if (!IS_ABSOLUTE_PATH(member_name
->c_str()))
616 const char* arch_path
= this->filename().c_str();
617 const char* basename
= lbasename(arch_path
);
618 if (basename
> arch_path
)
619 member_name
->replace(0, 0,
620 this->filename().substr(0, basename
- arch_path
));
625 // This is a member of a nested archive. Open the containing
626 // archive if we don't already have it open, then do a recursive
627 // call to include the member from that archive.
629 Nested_archive_table::const_iterator p
=
630 this->nested_archives_
.find(*member_name
);
631 if (p
!= this->nested_archives_
.end())
635 Input_file_argument
* input_file_arg
=
636 new Input_file_argument(member_name
->c_str(),
637 Input_file_argument::INPUT_FILE_TYPE_FILE
,
638 "", false, parameters
->options());
639 *input_file
= new Input_file(input_file_arg
);
641 if (!(*input_file
)->open(*this->dirpath_
, this->task_
, &dummy
))
643 arch
= new Archive(*member_name
, *input_file
, false, this->dirpath_
,
646 std::pair
<Nested_archive_table::iterator
, bool> ins
=
647 this->nested_archives_
.insert(std::make_pair(*member_name
, arch
));
648 gold_assert(ins
.second
);
650 return arch
->get_file_and_offset(nested_off
, input_file
, memoff
,
651 memsize
, member_name
);
654 // This is an external member of a thin archive. Open the
655 // file as a regular relocatable object file.
656 Input_file_argument
* input_file_arg
=
657 new Input_file_argument(member_name
->c_str(),
658 Input_file_argument::INPUT_FILE_TYPE_FILE
,
659 "", false, this->input_file_
->options());
660 *input_file
= new Input_file(input_file_arg
);
662 if (!(*input_file
)->open(*this->dirpath_
, this->task_
, &dummy
))
666 *memsize
= (*input_file
)->file().filesize();
670 // Return an ELF object for the member at offset OFF. If
671 // PUNCONFIGURED is not NULL, then if the ELF object has an
672 // unsupported target type, set *PUNCONFIGURED to true and return
676 Archive::get_elf_object_for_member(off_t off
, bool* punconfigured
)
678 if (punconfigured
!= NULL
)
679 *punconfigured
= false;
681 Input_file
* input_file
;
684 std::string member_name
;
685 if (!this->get_file_and_offset(off
, &input_file
, &memoff
, &memsize
,
689 const unsigned char* ehdr
;
692 bool is_elf_obj
= false;
694 if (is_elf_object(input_file
, memoff
, &ehdr
, &read_size
))
696 obj
= make_elf_object((std::string(this->input_file_
->filename())
697 + "(" + member_name
+ ")"),
698 input_file
, memoff
, ehdr
, read_size
,
703 if (parameters
->options().has_plugins())
706 = parameters
->options().plugins()->claim_file(input_file
,
710 if (plugin_obj
!= NULL
)
712 // The input file was claimed by a plugin, and its symbols
713 // have been provided by the plugin.
714 // Delete its elf object.
723 gold_error(_("%s: member at %zu is not an ELF object"),
724 this->name().c_str(), static_cast<size_t>(off
));
730 obj
->set_no_export(this->no_export());
734 // Read the symbols from all the archive members in the link.
737 Archive::read_all_symbols()
739 for (Archive::const_iterator p
= this->begin();
742 this->read_symbols(p
->off
);
745 // Read the symbols from an archive member in the link. OFF is the file
746 // offset of the member header.
749 Archive::read_symbols(off_t off
)
751 Object
* obj
= this->get_elf_object_for_member(off
, NULL
);
755 Read_symbols_data
* sd
= new Read_symbols_data
;
756 obj
->read_symbols(sd
);
757 Archive_member
member(obj
, sd
);
758 this->members_
[off
] = member
;
761 // Select members from the archive and add them to the link. We walk
762 // through the elements in the archive map, and look each one up in
763 // the symbol table. If it exists as a strong undefined symbol, we
764 // pull in the corresponding element. We have to do this in a loop,
765 // since pulling in one element may create new undefined symbols which
766 // may be satisfied by other objects in the archive. Return true in
767 // the normal case, false if the first member we tried to add from
768 // this archive had an incompatible target.
771 Archive::add_symbols(Symbol_table
* symtab
, Layout
* layout
,
772 Input_objects
* input_objects
, Mapfile
* mapfile
)
774 ++Archive::total_archives
;
776 if (this->input_file_
->options().whole_archive())
777 return this->include_all_members(symtab
, layout
, input_objects
,
780 Archive::total_members
+= this->num_members_
;
782 input_objects
->archive_start(this);
784 const size_t armap_size
= this->armap_
.size();
786 // This is a quick optimization, since we usually see many symbols
787 // in a row with the same offset. last_seen_offset holds the last
788 // offset we saw that was present in the seen_offsets_ set.
789 off_t last_seen_offset
= -1;
791 // Track which symbols in the symbol table we've already found to be
795 size_t tmpbuflen
= 0;
796 bool added_new_object
;
799 added_new_object
= false;
800 for (size_t i
= 0; i
< armap_size
; ++i
)
802 if (this->armap_checked_
[i
])
804 if (this->armap_
[i
].file_offset
== last_seen_offset
)
806 this->armap_checked_
[i
] = true;
809 if (this->seen_offsets_
.find(this->armap_
[i
].file_offset
)
810 != this->seen_offsets_
.end())
812 this->armap_checked_
[i
] = true;
813 last_seen_offset
= this->armap_
[i
].file_offset
;
817 const char* sym_name
= (this->armap_names_
.data()
818 + this->armap_
[i
].name_offset
);
822 Archive::Should_include t
=
823 Archive::should_include_member(symtab
, layout
, sym_name
, &sym
,
824 &why
, &tmpbuf
, &tmpbuflen
);
826 if (t
== Archive::SHOULD_INCLUDE_NO
827 || t
== Archive::SHOULD_INCLUDE_YES
)
828 this->armap_checked_
[i
] = true;
830 if (t
!= Archive::SHOULD_INCLUDE_YES
)
833 // We want to include this object in the link.
834 last_seen_offset
= this->armap_
[i
].file_offset
;
835 this->seen_offsets_
.insert(last_seen_offset
);
837 if (!this->include_member(symtab
, layout
, input_objects
,
838 last_seen_offset
, mapfile
, sym
,
846 added_new_object
= true;
849 while (added_new_object
);
854 input_objects
->archive_stop(this);
859 // Return whether the archive includes a member which defines the
863 Archive::defines_symbol(Symbol
* sym
) const
865 const char* symname
= sym
->name();
866 size_t symname_len
= strlen(symname
);
867 size_t armap_size
= this->armap_
.size();
868 for (size_t i
= 0; i
< armap_size
; ++i
)
870 if (this->armap_checked_
[i
])
872 const char* archive_symname
= (this->armap_names_
.data()
873 + this->armap_
[i
].name_offset
);
874 if (strncmp(archive_symname
, symname
, symname_len
) != 0)
876 char c
= archive_symname
[symname_len
];
877 if (c
== '\0' && sym
->version() == NULL
)
881 const char* ver
= archive_symname
+ symname_len
+ 1;
884 if (sym
->version() == NULL
)
888 if (sym
->version() != NULL
&& strcmp(sym
->version(), ver
) == 0)
895 // Include all the archive members in the link. This is for --whole-archive.
898 Archive::include_all_members(Symbol_table
* symtab
, Layout
* layout
,
899 Input_objects
* input_objects
, Mapfile
* mapfile
)
901 // Don't include the same archive twice. This can happen if
902 // --whole-archive is nested inside --start-group (PR gold/12163).
903 if (this->included_all_members_
)
906 this->included_all_members_
= true;
908 input_objects
->archive_start(this);
910 if (this->members_
.size() > 0)
912 std::map
<off_t
, Archive_member
>::const_iterator p
;
913 for (p
= this->members_
.begin();
914 p
!= this->members_
.end();
917 if (!this->include_member(symtab
, layout
, input_objects
, p
->first
,
918 mapfile
, NULL
, "--whole-archive"))
920 ++Archive::total_members
;
925 for (Archive::const_iterator p
= this->begin();
929 if (!this->include_member(symtab
, layout
, input_objects
, p
->off
,
930 mapfile
, NULL
, "--whole-archive"))
932 ++Archive::total_members
;
936 input_objects
->archive_stop(this);
941 // Return the number of members in the archive. This is only used for
945 Archive::count_members()
948 for (Archive::const_iterator p
= this->begin();
955 // RAII class to ensure we unlock the object if it's a member of a
956 // thin archive. We can't use Task_lock_obj in Archive::include_member
957 // because the object file is already locked when it's opened by
958 // get_elf_object_for_member.
960 class Thin_archive_object_unlocker
963 Thin_archive_object_unlocker(const Task
*task
, Object
* obj
)
964 : task_(task
), obj_(obj
)
967 ~Thin_archive_object_unlocker()
969 if (this->obj_
->offset() == 0)
970 this->obj_
->unlock(this->task_
);
974 Thin_archive_object_unlocker(const Thin_archive_object_unlocker
&);
975 Thin_archive_object_unlocker
& operator=(const Thin_archive_object_unlocker
&);
981 // Include an archive member in the link. OFF is the file offset of
982 // the member header. WHY is the reason we are including this member.
983 // Return true if we added the member or if we had an error, return
984 // false if this was the first member we tried to add from this
985 // archive and it had an incompatible format.
988 Archive::include_member(Symbol_table
* symtab
, Layout
* layout
,
989 Input_objects
* input_objects
, off_t off
,
990 Mapfile
* mapfile
, Symbol
* sym
, const char* why
)
992 ++Archive::total_members_loaded
;
994 std::map
<off_t
, Archive_member
>::const_iterator p
= this->members_
.find(off
);
995 if (p
!= this->members_
.end())
997 Object
* obj
= p
->second
.obj_
;
999 Read_symbols_data
* sd
= p
->second
.sd_
;
1000 if (mapfile
!= NULL
)
1001 mapfile
->report_include_archive_member(obj
->name(), sym
, why
);
1002 if (input_objects
->add_object(obj
))
1004 obj
->layout(symtab
, layout
, sd
);
1005 obj
->add_symbols(symtab
, sd
, layout
);
1006 this->included_member_
= true;
1012 // If this is the first object we are including from this archive,
1013 // and we searched for this archive, most likely because it was
1014 // found via a -l option, then if the target is incompatible we want
1015 // to move on to the next archive found in the search path.
1016 bool unconfigured
= false;
1017 bool* punconfigured
= NULL
;
1018 if (!this->included_member_
&& this->searched_for())
1019 punconfigured
= &unconfigured
;
1021 Object
* obj
= this->get_elf_object_for_member(off
, punconfigured
);
1024 // Return false to search for another archive, true if we found
1026 return unconfigured
? false : true;
1029 // If the object is an external member of a thin archive,
1030 // unlock it when we're done here.
1031 Thin_archive_object_unlocker
unlocker(this->task_
, obj
);
1033 if (mapfile
!= NULL
)
1034 mapfile
->report_include_archive_member(obj
->name(), sym
, why
);
1036 Pluginobj
* pluginobj
= obj
->pluginobj();
1037 if (pluginobj
!= NULL
)
1039 pluginobj
->add_symbols(symtab
, NULL
, layout
);
1040 this->included_member_
= true;
1044 if (!input_objects
->add_object(obj
))
1050 if (layout
->incremental_inputs() != NULL
)
1051 layout
->incremental_inputs()->report_object(obj
, 0, this, NULL
);
1054 Read_symbols_data sd
;
1055 obj
->read_symbols(&sd
);
1056 obj
->layout(symtab
, layout
, &sd
);
1057 obj
->add_symbols(symtab
, &sd
, layout
);
1060 this->included_member_
= true;
1064 // Iterate over all unused symbols, and call the visitor class V for each.
1067 Archive::do_for_all_unused_symbols(Symbol_visitor_base
* v
) const
1069 for (std::vector
<Armap_entry
>::const_iterator p
= this->armap_
.begin();
1070 p
!= this->armap_
.end();
1073 if (this->seen_offsets_
.find(p
->file_offset
)
1074 == this->seen_offsets_
.end())
1075 v
->visit(this->armap_names_
.data() + p
->name_offset
);
1079 // Print statistical information to stderr. This is used for --stats.
1082 Archive::print_stats()
1084 fprintf(stderr
, _("%s: archive libraries: %u\n"),
1085 program_name
, Archive::total_archives
);
1086 fprintf(stderr
, _("%s: total archive members: %u\n"),
1087 program_name
, Archive::total_members
);
1088 fprintf(stderr
, _("%s: loaded archive members: %u\n"),
1089 program_name
, Archive::total_members_loaded
);
1092 // Add_archive_symbols methods.
1094 Add_archive_symbols::~Add_archive_symbols()
1096 if (this->this_blocker_
!= NULL
)
1097 delete this->this_blocker_
;
1098 // next_blocker_ is deleted by the task associated with the next
1102 // Return whether we can add the archive symbols. We are blocked by
1103 // this_blocker_. We block next_blocker_. We also lock the file.
1106 Add_archive_symbols::is_runnable()
1108 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1109 return this->this_blocker_
;
1114 Add_archive_symbols::locks(Task_locker
* tl
)
1116 tl
->add(this, this->next_blocker_
);
1117 tl
->add(this, this->archive_
->token());
1121 Add_archive_symbols::run(Workqueue
* workqueue
)
1123 // For an incremental link, begin recording layout information.
1124 Incremental_inputs
* incremental_inputs
= this->layout_
->incremental_inputs();
1125 if (incremental_inputs
!= NULL
)
1127 unsigned int arg_serial
= this->input_argument_
->file().arg_serial();
1128 Script_info
* script_info
= this->input_argument_
->script_info();
1129 incremental_inputs
->report_archive_begin(this->archive_
, arg_serial
,
1133 bool added
= this->archive_
->add_symbols(this->symtab_
, this->layout_
,
1134 this->input_objects_
,
1136 this->archive_
->unlock_nested_archives();
1138 this->archive_
->release();
1139 this->archive_
->clear_uncached_views();
1143 // This archive holds object files which are incompatible with
1145 Read_symbols::incompatible_warning(this->input_argument_
,
1146 this->archive_
->input_file());
1147 Read_symbols::requeue(workqueue
, this->input_objects_
, this->symtab_
,
1148 this->layout_
, this->dirpath_
, this->dirindex_
,
1149 this->mapfile_
, this->input_argument_
,
1150 this->input_group_
, this->next_blocker_
);
1151 delete this->archive_
;
1155 if (this->input_group_
!= NULL
)
1156 this->input_group_
->add_archive(this->archive_
);
1159 // For an incremental link, finish recording the layout information.
1160 if (incremental_inputs
!= NULL
)
1161 incremental_inputs
->report_archive_end(this->archive_
);
1163 if (!parameters
->options().has_plugins()
1164 || this->archive_
->input_file()->options().whole_archive())
1166 // We no longer need to know about this archive.
1167 delete this->archive_
;
1171 // The plugin interface may want to rescan this archive.
1172 parameters
->options().plugins()->save_archive(this->archive_
);
1175 this->archive_
= NULL
;
1179 // Class Lib_group static variables.
1180 unsigned int Lib_group::total_lib_groups
;
1181 unsigned int Lib_group::total_members
;
1182 unsigned int Lib_group::total_members_loaded
;
1184 Lib_group::Lib_group(const Input_file_lib
* lib
, Task
* task
)
1185 : Library_base(task
), members_()
1187 this->members_
.resize(lib
->size());
1191 Lib_group::do_filename() const
1193 std::string
*filename
= new std::string("/group/");
1197 // Select members from the lib group and add them to the link. We walk
1198 // through the members, and check if each one up should be included.
1199 // If the object says it should be included, we do so. We have to do
1200 // this in a loop, since including one member may create new undefined
1201 // symbols which may be satisfied by other members.
1204 Lib_group::add_symbols(Symbol_table
* symtab
, Layout
* layout
,
1205 Input_objects
* input_objects
)
1207 ++Lib_group::total_lib_groups
;
1209 Lib_group::total_members
+= this->members_
.size();
1211 bool added_new_object
;
1214 added_new_object
= false;
1216 while (i
< this->members_
.size())
1218 const Archive_member
& member
= this->members_
[i
];
1219 Object
* obj
= member
.obj_
;
1222 // Skip files with no symbols. Plugin objects have
1223 // member.sd_ == NULL.
1225 && (member
.sd_
== NULL
|| member
.sd_
->symbol_names
!= NULL
))
1227 Archive::Should_include t
= obj
->should_include_member(symtab
,
1232 if (t
!= Archive::SHOULD_INCLUDE_YES
)
1238 this->include_member(symtab
, layout
, input_objects
, member
);
1240 added_new_object
= true;
1244 if (member
.sd_
!= NULL
)
1246 // The file must be locked in order to destroy the views
1247 // associated with it.
1248 gold_assert(obj
!= NULL
);
1249 obj
->lock(this->task_
);
1251 obj
->unlock(this->task_
);
1255 this->members_
[i
] = this->members_
.back();
1256 this->members_
.pop_back();
1259 while (added_new_object
);
1262 // Include a lib group member in the link.
1265 Lib_group::include_member(Symbol_table
* symtab
, Layout
* layout
,
1266 Input_objects
* input_objects
,
1267 const Archive_member
& member
)
1269 ++Lib_group::total_members_loaded
;
1271 Object
* obj
= member
.obj_
;
1272 gold_assert(obj
!= NULL
);
1274 Pluginobj
* pluginobj
= obj
->pluginobj();
1275 if (pluginobj
!= NULL
)
1277 pluginobj
->add_symbols(symtab
, NULL
, layout
);
1281 Read_symbols_data
* sd
= member
.sd_
;
1282 gold_assert(sd
!= NULL
);
1283 obj
->lock(this->task_
);
1284 if (input_objects
->add_object(obj
))
1286 if (layout
->incremental_inputs() != NULL
)
1287 layout
->incremental_inputs()->report_object(obj
, member
.arg_serial_
,
1289 obj
->layout(symtab
, layout
, sd
);
1290 obj
->add_symbols(symtab
, sd
, layout
);
1293 // Unlock the file for the next task.
1294 obj
->unlock(this->task_
);
1297 // Iterate over all unused symbols, and call the visitor class V for each.
1300 Lib_group::do_for_all_unused_symbols(Symbol_visitor_base
* v
) const
1302 // Files are removed from the members list when used, so all the
1303 // files remaining on the list are unused.
1304 for (std::vector
<Archive_member
>::const_iterator p
= this->members_
.begin();
1305 p
!= this->members_
.end();
1308 Object
* obj
= p
->obj_
;
1309 obj
->for_all_global_symbols(p
->sd_
, v
);
1313 // Print statistical information to stderr. This is used for --stats.
1316 Lib_group::print_stats()
1318 fprintf(stderr
, _("%s: lib groups: %u\n"),
1319 program_name
, Lib_group::total_lib_groups
);
1320 fprintf(stderr
, _("%s: total lib groups members: %u\n"),
1321 program_name
, Lib_group::total_members
);
1322 fprintf(stderr
, _("%s: loaded lib groups members: %u\n"),
1323 program_name
, Lib_group::total_members_loaded
);
1327 Add_lib_group_symbols::is_runnable()
1329 if (this->readsyms_blocker_
!= NULL
&& this->readsyms_blocker_
->is_blocked())
1330 return this->readsyms_blocker_
;
1331 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1332 return this->this_blocker_
;
1337 Add_lib_group_symbols::locks(Task_locker
* tl
)
1339 tl
->add(this, this->next_blocker_
);
1343 Add_lib_group_symbols::run(Workqueue
*)
1345 // For an incremental link, begin recording layout information.
1346 Incremental_inputs
* incremental_inputs
= this->layout_
->incremental_inputs();
1347 if (incremental_inputs
!= NULL
)
1348 incremental_inputs
->report_archive_begin(this->lib_
, 0, NULL
);
1350 this->lib_
->add_symbols(this->symtab_
, this->layout_
, this->input_objects_
);
1352 if (incremental_inputs
!= NULL
)
1353 incremental_inputs
->report_archive_end(this->lib_
);
1356 Add_lib_group_symbols::~Add_lib_group_symbols()
1358 if (this->this_blocker_
!= NULL
)
1359 delete this->this_blocker_
;
1360 // next_blocker_ is deleted by the task associated with the next
1364 } // End namespace gold.