1 // archive.cc -- archive support for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010 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 // The header of an entry in the archive. This is all readable text,
48 // padded with spaces where necesary. If the contents of an archive
49 // are all text file, the entire archive is readable.
51 struct Archive::Archive_header
55 // The file modification time.
57 // The user's UID in decimal.
59 // The user's GID in decimal.
61 // The file mode in octal.
63 // The file size in decimal.
65 // The final magic code.
69 // Class Archive static variables.
70 unsigned int Archive::total_archives
;
71 unsigned int Archive::total_members
;
72 unsigned int Archive::total_members_loaded
;
76 const char Archive::armag
[sarmag
] =
78 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
81 const char Archive::armagt
[sarmag
] =
83 '!', '<', 't', 'h', 'i', 'n', '>', '\n'
86 const char Archive::arfmag
[2] = { '`', '\n' };
88 Archive::Archive(const std::string
& name
, Input_file
* input_file
,
89 bool is_thin_archive
, Dirsearch
* dirpath
, Task
* task
)
90 : name_(name
), input_file_(input_file
), armap_(), armap_names_(),
91 extended_names_(), armap_checked_(), seen_offsets_(), members_(),
92 is_thin_archive_(is_thin_archive
), included_member_(false),
93 nested_archives_(), dirpath_(dirpath
), task_(task
), num_members_(0),
94 incremental_info_(NULL
)
97 parameters
->options().check_excluded_libs(input_file
->found_name());
100 // Set up the archive: read the symbol map and the extended name
106 // We need to ignore empty archives.
107 if (this->input_file_
->file().filesize() == sarmag
)
110 // The first member of the archive should be the symbol table.
111 std::string armap_name
;
112 section_size_type armap_size
=
113 convert_to_section_size_type(this->read_header(sarmag
, false,
116 if (armap_name
.empty())
118 this->read_armap(sarmag
+ sizeof(Archive_header
), armap_size
);
119 off
= sarmag
+ sizeof(Archive_header
) + armap_size
;
121 else if (!this->input_file_
->options().whole_archive())
122 gold_error(_("%s: no archive symbol table (run ranlib)"),
123 this->name().c_str());
125 // See if there is an extended name table. We cache these views
126 // because it is likely that we will want to read the following
127 // header in the add_symbols routine.
131 section_size_type extended_size
=
132 convert_to_section_size_type(this->read_header(off
, true, &xname
, NULL
));
135 const unsigned char* p
= this->get_view(off
+ sizeof(Archive_header
),
136 extended_size
, false, true);
137 const char* px
= reinterpret_cast<const char*>(p
);
138 this->extended_names_
.assign(px
, extended_size
);
140 bool preread_syms
= (parameters
->options().threads()
141 && parameters
->options().preread_archive_symbols());
142 #ifndef ENABLE_THREADS
143 preread_syms
= false;
145 if (parameters
->options().has_plugins())
146 preread_syms
= false;
149 this->read_all_symbols();
152 // Unlock any nested archives.
155 Archive::unlock_nested_archives()
157 for (Nested_archive_table::iterator p
= this->nested_archives_
.begin();
158 p
!= this->nested_archives_
.end();
161 p
->second
->unlock(this->task_
);
165 // Read the archive symbol map.
168 Archive::read_armap(off_t start
, section_size_type size
)
170 // To count the total number of archive members, we'll just count
171 // the number of times the file offset changes. Since most archives
172 // group the symbols in the armap by object, this ought to give us
173 // an accurate count.
174 off_t last_seen_offset
= -1;
176 // Read in the entire armap.
177 const unsigned char* p
= this->get_view(start
, size
, true, false);
179 // Numbers in the armap are always big-endian.
180 const elfcpp::Elf_Word
* pword
= reinterpret_cast<const elfcpp::Elf_Word
*>(p
);
181 unsigned int nsyms
= elfcpp::Swap
<32, true>::readval(pword
);
184 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
185 const char* pnames
= reinterpret_cast<const char*>(pword
+ nsyms
);
186 section_size_type names_size
=
187 reinterpret_cast<const char*>(p
) + size
- pnames
;
188 this->armap_names_
.assign(pnames
, names_size
);
190 this->armap_
.resize(nsyms
);
192 section_offset_type name_offset
= 0;
193 for (unsigned int i
= 0; i
< nsyms
; ++i
)
195 this->armap_
[i
].name_offset
= name_offset
;
196 this->armap_
[i
].file_offset
= elfcpp::Swap
<32, true>::readval(pword
);
197 name_offset
+= strlen(pnames
+ name_offset
) + 1;
199 if (this->armap_
[i
].file_offset
!= last_seen_offset
)
201 last_seen_offset
= this->armap_
[i
].file_offset
;
202 ++this->num_members_
;
206 if (static_cast<section_size_type
>(name_offset
) > names_size
)
207 gold_error(_("%s: bad archive symbol table names"),
208 this->name().c_str());
210 // This array keeps track of which symbols are for archive elements
211 // which we have already included in the link.
212 this->armap_checked_
.resize(nsyms
);
215 // Read the header of an archive member at OFF. Fail if something
216 // goes wrong. Return the size of the member. Set *PNAME to the name
220 Archive::read_header(off_t off
, bool cache
, std::string
* pname
,
223 const unsigned char* p
= this->get_view(off
, sizeof(Archive_header
), true,
225 const Archive_header
* hdr
= reinterpret_cast<const Archive_header
*>(p
);
226 return this->interpret_header(hdr
, off
, pname
, nested_off
);
229 // Interpret the header of HDR, the header of the archive member at
230 // file offset OFF. Fail if something goes wrong. Return the size of
231 // the member. Set *PNAME to the name of the member.
234 Archive::interpret_header(const Archive_header
* hdr
, off_t off
,
235 std::string
* pname
, off_t
* nested_off
) const
237 if (memcmp(hdr
->ar_fmag
, arfmag
, sizeof arfmag
) != 0)
239 gold_error(_("%s: malformed archive header at %zu"),
240 this->name().c_str(), static_cast<size_t>(off
));
241 return this->input_file_
->file().filesize() - off
;
244 const int size_string_size
= sizeof hdr
->ar_size
;
245 char size_string
[size_string_size
+ 1];
246 memcpy(size_string
, hdr
->ar_size
, size_string_size
);
247 char* ps
= size_string
+ size_string_size
;
248 while (ps
[-1] == ' ')
254 off_t member_size
= strtol(size_string
, &end
, 10);
257 || (member_size
== LONG_MAX
&& errno
== ERANGE
))
259 gold_error(_("%s: malformed archive header size at %zu"),
260 this->name().c_str(), static_cast<size_t>(off
));
261 return this->input_file_
->file().filesize() - off
;
264 if (hdr
->ar_name
[0] != '/')
266 const char* name_end
= strchr(hdr
->ar_name
, '/');
268 || name_end
- hdr
->ar_name
>= static_cast<int>(sizeof hdr
->ar_name
))
270 gold_error(_("%s: malformed archive header name at %zu"),
271 this->name().c_str(), static_cast<size_t>(off
));
272 return this->input_file_
->file().filesize() - off
;
274 pname
->assign(hdr
->ar_name
, name_end
- hdr
->ar_name
);
275 if (nested_off
!= NULL
)
278 else if (hdr
->ar_name
[1] == ' ')
280 // This is the symbol table.
284 else if (hdr
->ar_name
[1] == '/')
286 // This is the extended name table.
287 pname
->assign(1, '/');
292 long x
= strtol(hdr
->ar_name
+ 1, &end
, 10);
295 y
= strtol(end
+ 1, &end
, 10);
298 || (x
== LONG_MAX
&& errno
== ERANGE
)
299 || static_cast<size_t>(x
) >= this->extended_names_
.size())
301 gold_error(_("%s: bad extended name index at %zu"),
302 this->name().c_str(), static_cast<size_t>(off
));
303 return this->input_file_
->file().filesize() - off
;
306 const char* name
= this->extended_names_
.data() + x
;
307 const char* name_end
= strchr(name
, '\n');
308 if (static_cast<size_t>(name_end
- name
) > this->extended_names_
.size()
309 || name_end
[-1] != '/')
311 gold_error(_("%s: bad extended name entry at header %zu"),
312 this->name().c_str(), static_cast<size_t>(off
));
313 return this->input_file_
->file().filesize() - off
;
315 pname
->assign(name
, name_end
- 1 - name
);
316 if (nested_off
!= NULL
)
323 // An archive member iterator.
325 class Archive::const_iterator
328 // The header of an archive member. This is what this iterator
332 // The name of the member.
334 // The file offset of the member.
336 // The file offset of a nested archive member.
338 // The size of the member.
342 const_iterator(Archive
* archive
, off_t off
)
343 : archive_(archive
), off_(off
)
344 { this->read_next_header(); }
348 { return this->header_
; }
352 { return &this->header_
; }
357 if (this->off_
== this->archive_
->file().filesize())
359 this->off_
+= sizeof(Archive_header
);
360 if (!this->archive_
->is_thin_archive())
361 this->off_
+= this->header_
.size
;
362 if ((this->off_
& 1) != 0)
364 this->read_next_header();
371 const_iterator ret
= *this;
377 operator==(const const_iterator p
) const
378 { return this->off_
== p
->off
; }
381 operator!=(const const_iterator p
) const
382 { return this->off_
!= p
->off
; }
388 // The underlying archive.
390 // The current offset in the file.
392 // The current archive header.
396 // Read the next archive header.
399 Archive::const_iterator::read_next_header()
401 off_t filesize
= this->archive_
->file().filesize();
404 if (filesize
- this->off_
< static_cast<off_t
>(sizeof(Archive_header
)))
406 if (filesize
!= this->off_
)
408 gold_error(_("%s: short archive header at %zu"),
409 this->archive_
->filename().c_str(),
410 static_cast<size_t>(this->off_
));
411 this->off_
= filesize
;
413 this->header_
.off
= filesize
;
417 unsigned char buf
[sizeof(Archive_header
)];
418 this->archive_
->file().read(this->off_
, sizeof(Archive_header
), buf
);
420 const Archive_header
* hdr
= reinterpret_cast<const Archive_header
*>(buf
);
422 this->archive_
->interpret_header(hdr
, this->off_
, &this->header_
.name
,
423 &this->header_
.nested_off
);
424 this->header_
.off
= this->off_
;
426 // Skip special members.
427 if (!this->header_
.name
.empty() && this->header_
.name
!= "/")
430 this->off_
+= sizeof(Archive_header
) + this->header_
.size
;
431 if ((this->off_
& 1) != 0)
438 Archive::const_iterator
441 return Archive::const_iterator(this, sarmag
);
446 Archive::const_iterator
449 return Archive::const_iterator(this, this->input_file_
->file().filesize());
452 // Get the file and offset for an archive member, which may be an
453 // external member of a thin archive. Set *INPUT_FILE to the
454 // file containing the actual member, *MEMOFF to the offset
455 // within that file (0 if not a nested archive), and *MEMBER_NAME
456 // to the name of the archive member. Return TRUE on success.
459 Archive::get_file_and_offset(off_t off
, Input_file
** input_file
, off_t
* memoff
,
460 off_t
* memsize
, std::string
* member_name
)
464 *memsize
= this->read_header(off
, false, member_name
, &nested_off
);
466 *input_file
= this->input_file_
;
467 *memoff
= off
+ static_cast<off_t
>(sizeof(Archive_header
));
469 if (!this->is_thin_archive_
)
472 // Adjust a relative pathname so that it is relative
473 // to the directory containing the archive.
474 if (!IS_ABSOLUTE_PATH(member_name
->c_str()))
476 const char* arch_path
= this->filename().c_str();
477 const char* basename
= lbasename(arch_path
);
478 if (basename
> arch_path
)
479 member_name
->replace(0, 0,
480 this->filename().substr(0, basename
- arch_path
));
485 // This is a member of a nested archive. Open the containing
486 // archive if we don't already have it open, then do a recursive
487 // call to include the member from that archive.
489 Nested_archive_table::const_iterator p
=
490 this->nested_archives_
.find(*member_name
);
491 if (p
!= this->nested_archives_
.end())
495 Input_file_argument
* input_file_arg
=
496 new Input_file_argument(member_name
->c_str(),
497 Input_file_argument::INPUT_FILE_TYPE_FILE
,
498 "", false, parameters
->options());
499 *input_file
= new Input_file(input_file_arg
);
501 if (!(*input_file
)->open(*this->dirpath_
, this->task_
, &dummy
))
503 arch
= new Archive(*member_name
, *input_file
, false, this->dirpath_
,
506 std::pair
<Nested_archive_table::iterator
, bool> ins
=
507 this->nested_archives_
.insert(std::make_pair(*member_name
, arch
));
508 gold_assert(ins
.second
);
510 return arch
->get_file_and_offset(nested_off
, input_file
, memoff
,
511 memsize
, member_name
);
514 // This is an external member of a thin archive. Open the
515 // file as a regular relocatable object file.
516 Input_file_argument
* input_file_arg
=
517 new Input_file_argument(member_name
->c_str(),
518 Input_file_argument::INPUT_FILE_TYPE_FILE
,
519 "", false, this->input_file_
->options());
520 *input_file
= new Input_file(input_file_arg
);
522 if (!(*input_file
)->open(*this->dirpath_
, this->task_
, &dummy
))
526 *memsize
= (*input_file
)->file().filesize();
530 // Return an ELF object for the member at offset OFF. If
531 // PUNCONFIGURED is not NULL, then if the ELF object has an
532 // unsupported target type, set *PUNCONFIGURED to true and return
536 Archive::get_elf_object_for_member(off_t off
, bool* punconfigured
)
538 if (punconfigured
!= NULL
)
539 *punconfigured
= false;
541 Input_file
* input_file
;
544 std::string member_name
;
545 if (!this->get_file_and_offset(off
, &input_file
, &memoff
, &memsize
,
549 if (parameters
->options().has_plugins())
551 Object
* obj
= parameters
->options().plugins()->claim_file(input_file
,
556 // The input file was claimed by a plugin, and its symbols
557 // have been provided by the plugin.
562 const unsigned char* ehdr
;
564 if (!is_elf_object(input_file
, memoff
, &ehdr
, &read_size
))
566 gold_error(_("%s: member at %zu is not an ELF object"),
567 this->name().c_str(), static_cast<size_t>(off
));
571 Object
* obj
= make_elf_object((std::string(this->input_file_
->filename())
572 + "(" + member_name
+ ")"),
573 input_file
, memoff
, ehdr
, read_size
,
577 obj
->set_no_export(this->no_export());
581 // Read the symbols from all the archive members in the link.
584 Archive::read_all_symbols()
586 for (Archive::const_iterator p
= this->begin();
589 this->read_symbols(p
->off
);
592 // Read the symbols from an archive member in the link. OFF is the file
593 // offset of the member header.
596 Archive::read_symbols(off_t off
)
598 Object
* obj
= this->get_elf_object_for_member(off
, NULL
);
602 Read_symbols_data
* sd
= new Read_symbols_data
;
603 obj
->read_symbols(sd
);
604 Archive_member
member(obj
, sd
);
605 this->members_
[off
] = member
;
608 Archive::Should_include
609 Archive::should_include_member(Symbol_table
* symtab
, Layout
* layout
,
610 const char* sym_name
, Symbol
** symp
,
611 std::string
* why
, char** tmpbufp
,
614 // In an object file, and therefore in an archive map, an
615 // '@' in the name separates the symbol name from the
616 // version name. If there are two '@' characters, this is
617 // the default version.
618 char* tmpbuf
= *tmpbufp
;
619 const char* ver
= strchr(sym_name
, '@');
623 size_t symlen
= ver
- sym_name
;
624 if (symlen
+ 1 > *tmpbuflen
)
626 tmpbuf
= static_cast<char*>(xrealloc(tmpbuf
, symlen
+ 1));
628 *tmpbuflen
= symlen
+ 1;
630 memcpy(tmpbuf
, sym_name
, symlen
);
631 tmpbuf
[symlen
] = '\0';
642 Symbol
* sym
= symtab
->lookup(sym_name
, ver
);
646 || !sym
->is_undefined()
647 || sym
->binding() == elfcpp::STB_WEAK
))
648 sym
= symtab
->lookup(sym_name
, NULL
);
654 // Check whether the symbol was named in a -u option.
655 if (parameters
->options().is_undefined(sym_name
))
660 else if (layout
->script_options()->is_referenced(sym_name
))
662 size_t alc
= 100 + strlen(sym_name
);
663 char* buf
= new char[alc
];
664 snprintf(buf
, alc
, _("script or expression reference to %s"),
670 return Archive::SHOULD_INCLUDE_UNKNOWN
;
672 else if (!sym
->is_undefined())
673 return Archive::SHOULD_INCLUDE_NO
;
674 // PR 12001: Do not include an archive when the undefined
675 // symbol has actually been defined on the command line.
676 else if (layout
->script_options()->is_pending_assignment(sym_name
))
677 return Archive::SHOULD_INCLUDE_NO
;
678 else if (sym
->binding() == elfcpp::STB_WEAK
)
679 return Archive::SHOULD_INCLUDE_UNKNOWN
;
681 return Archive::SHOULD_INCLUDE_YES
;
684 // Select members from the archive and add them to the link. We walk
685 // through the elements in the archive map, and look each one up in
686 // the symbol table. If it exists as a strong undefined symbol, we
687 // pull in the corresponding element. We have to do this in a loop,
688 // since pulling in one element may create new undefined symbols which
689 // may be satisfied by other objects in the archive. Return true in
690 // the normal case, false if the first member we tried to add from
691 // this archive had an incompatible target.
694 Archive::add_symbols(Symbol_table
* symtab
, Layout
* layout
,
695 Input_objects
* input_objects
, Mapfile
* mapfile
)
697 ++Archive::total_archives
;
699 if (this->input_file_
->options().whole_archive())
700 return this->include_all_members(symtab
, layout
, input_objects
,
703 Archive::total_members
+= this->num_members_
;
705 input_objects
->archive_start(this);
707 const size_t armap_size
= this->armap_
.size();
709 // This is a quick optimization, since we usually see many symbols
710 // in a row with the same offset. last_seen_offset holds the last
711 // offset we saw that was present in the seen_offsets_ set.
712 off_t last_seen_offset
= -1;
714 // Track which symbols in the symbol table we've already found to be
718 size_t tmpbuflen
= 0;
719 bool added_new_object
;
722 added_new_object
= false;
723 for (size_t i
= 0; i
< armap_size
; ++i
)
725 if (this->armap_checked_
[i
])
727 if (this->armap_
[i
].file_offset
== last_seen_offset
)
729 this->armap_checked_
[i
] = true;
732 if (this->seen_offsets_
.find(this->armap_
[i
].file_offset
)
733 != this->seen_offsets_
.end())
735 this->armap_checked_
[i
] = true;
736 last_seen_offset
= this->armap_
[i
].file_offset
;
740 const char* sym_name
= (this->armap_names_
.data()
741 + this->armap_
[i
].name_offset
);
745 Archive::Should_include t
=
746 Archive::should_include_member(symtab
, layout
, sym_name
, &sym
,
747 &why
, &tmpbuf
, &tmpbuflen
);
749 if (t
== Archive::SHOULD_INCLUDE_NO
750 || t
== Archive::SHOULD_INCLUDE_YES
)
751 this->armap_checked_
[i
] = true;
753 if (t
!= Archive::SHOULD_INCLUDE_YES
)
756 // We want to include this object in the link.
757 last_seen_offset
= this->armap_
[i
].file_offset
;
758 this->seen_offsets_
.insert(last_seen_offset
);
760 if (!this->include_member(symtab
, layout
, input_objects
,
761 last_seen_offset
, mapfile
, sym
,
769 added_new_object
= true;
772 while (added_new_object
);
777 input_objects
->archive_stop(this);
782 // Include all the archive members in the link. This is for --whole-archive.
785 Archive::include_all_members(Symbol_table
* symtab
, Layout
* layout
,
786 Input_objects
* input_objects
, Mapfile
* mapfile
)
788 input_objects
->archive_start(this);
790 if (this->members_
.size() > 0)
792 std::map
<off_t
, Archive_member
>::const_iterator p
;
793 for (p
= this->members_
.begin();
794 p
!= this->members_
.end();
797 if (!this->include_member(symtab
, layout
, input_objects
, p
->first
,
798 mapfile
, NULL
, "--whole-archive"))
800 ++Archive::total_members
;
805 for (Archive::const_iterator p
= this->begin();
809 if (!this->include_member(symtab
, layout
, input_objects
, p
->off
,
810 mapfile
, NULL
, "--whole-archive"))
812 ++Archive::total_members
;
816 input_objects
->archive_stop(this);
821 // Return the number of members in the archive. This is only used for
825 Archive::count_members()
828 for (Archive::const_iterator p
= this->begin();
835 // Include an archive member in the link. OFF is the file offset of
836 // the member header. WHY is the reason we are including this member.
837 // Return true if we added the member or if we had an error, return
838 // false if this was the first member we tried to add from this
839 // archive and it had an incompatible format.
842 Archive::include_member(Symbol_table
* symtab
, Layout
* layout
,
843 Input_objects
* input_objects
, off_t off
,
844 Mapfile
* mapfile
, Symbol
* sym
, const char* why
)
846 ++Archive::total_members_loaded
;
848 std::map
<off_t
, Archive_member
>::const_iterator p
= this->members_
.find(off
);
849 if (p
!= this->members_
.end())
851 Object
* obj
= p
->second
.obj_
;
853 Read_symbols_data
* sd
= p
->second
.sd_
;
855 mapfile
->report_include_archive_member(obj
->name(), sym
, why
);
856 if (input_objects
->add_object(obj
))
858 obj
->layout(symtab
, layout
, sd
);
859 obj
->add_symbols(symtab
, sd
, layout
);
860 this->included_member_
= true;
866 // If this is the first object we are including from this archive,
867 // and we searched for this archive, most likely because it was
868 // found via a -l option, then if the target is incompatible we want
869 // to move on to the next archive found in the search path.
870 bool unconfigured
= false;
871 bool* punconfigured
= NULL
;
872 if (!this->included_member_
&& this->searched_for())
873 punconfigured
= &unconfigured
;
875 Object
* obj
= this->get_elf_object_for_member(off
, punconfigured
);
878 // Return false to search for another archive, true if we found
880 return unconfigured
? false : true;
884 mapfile
->report_include_archive_member(obj
->name(), sym
, why
);
886 Pluginobj
* pluginobj
= obj
->pluginobj();
887 if (pluginobj
!= NULL
)
889 pluginobj
->add_symbols(symtab
, NULL
, layout
);
890 this->included_member_
= true;
894 if (!input_objects
->add_object(obj
))
896 // If this is an external member of a thin archive, unlock the
898 if (obj
->offset() == 0)
899 obj
->unlock(this->task_
);
905 if (layout
->incremental_inputs() != NULL
)
906 layout
->incremental_inputs()->report_object(obj
, this);
907 Read_symbols_data sd
;
908 obj
->read_symbols(&sd
);
909 obj
->layout(symtab
, layout
, &sd
);
910 obj
->add_symbols(symtab
, &sd
, layout
);
913 // If this is an external member of a thin archive, unlock the file
914 // for the next task.
915 if (obj
->offset() == 0)
916 obj
->unlock(this->task_
);
918 this->included_member_
= true;
924 // Print statistical information to stderr. This is used for --stats.
927 Archive::print_stats()
929 fprintf(stderr
, _("%s: archive libraries: %u\n"),
930 program_name
, Archive::total_archives
);
931 fprintf(stderr
, _("%s: total archive members: %u\n"),
932 program_name
, Archive::total_members
);
933 fprintf(stderr
, _("%s: loaded archive members: %u\n"),
934 program_name
, Archive::total_members_loaded
);
937 // Add_archive_symbols methods.
939 Add_archive_symbols::~Add_archive_symbols()
941 if (this->this_blocker_
!= NULL
)
942 delete this->this_blocker_
;
943 // next_blocker_ is deleted by the task associated with the next
947 // Return whether we can add the archive symbols. We are blocked by
948 // this_blocker_. We block next_blocker_. We also lock the file.
951 Add_archive_symbols::is_runnable()
953 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
954 return this->this_blocker_
;
959 Add_archive_symbols::locks(Task_locker
* tl
)
961 tl
->add(this, this->next_blocker_
);
962 tl
->add(this, this->archive_
->token());
966 Add_archive_symbols::run(Workqueue
* workqueue
)
968 // For an incremental link, begin recording layout information.
969 Incremental_inputs
* incremental_inputs
= this->layout_
->incremental_inputs();
970 if (incremental_inputs
!= NULL
)
971 incremental_inputs
->report_archive_begin(this->archive_
);
973 bool added
= this->archive_
->add_symbols(this->symtab_
, this->layout_
,
974 this->input_objects_
,
976 this->archive_
->unlock_nested_archives();
978 this->archive_
->release();
979 this->archive_
->clear_uncached_views();
983 // This archive holds object files which are incompatible with
985 Read_symbols::incompatible_warning(this->input_argument_
,
986 this->archive_
->input_file());
987 Read_symbols::requeue(workqueue
, this->input_objects_
, this->symtab_
,
988 this->layout_
, this->dirpath_
, this->dirindex_
,
989 this->mapfile_
, this->input_argument_
,
990 this->input_group_
, this->next_blocker_
);
991 delete this->archive_
;
995 if (this->input_group_
!= NULL
)
996 this->input_group_
->add_archive(this->archive_
);
999 // For an incremental link, finish recording the layout information.
1000 Incremental_inputs
* incremental_inputs
= this->layout_
->incremental_inputs();
1001 if (incremental_inputs
!= NULL
)
1002 incremental_inputs
->report_archive_end(this->archive_
);
1004 // We no longer need to know about this archive.
1005 delete this->archive_
;
1006 this->archive_
= NULL
;
1010 // Class Lib_group static variables.
1011 unsigned int Lib_group::total_lib_groups
;
1012 unsigned int Lib_group::total_members
;
1013 unsigned int Lib_group::total_members_loaded
;
1015 Lib_group::Lib_group(const Input_file_lib
* lib
, Task
* task
)
1016 : lib_(lib
), task_(task
), members_()
1018 this->members_
.resize(lib
->size());
1021 // Select members from the lib group and add them to the link. We walk
1022 // through the the members, and check if each one up should be included.
1023 // If the object says it should be included, we do so. We have to do
1024 // this in a loop, since including one member may create new undefined
1025 // symbols which may be satisfied by other members.
1028 Lib_group::add_symbols(Symbol_table
* symtab
, Layout
* layout
,
1029 Input_objects
* input_objects
)
1031 ++Lib_group::total_lib_groups
;
1033 Lib_group::total_members
+= this->members_
.size();
1035 bool added_new_object
;
1038 added_new_object
= false;
1040 while (i
< this->members_
.size())
1042 const Archive_member
& member
= this->members_
[i
];
1043 Object
* obj
= member
.obj_
;
1046 // Skip files with no symbols. Plugin objects have
1047 // member.sd_ == NULL.
1049 && (member
.sd_
== NULL
|| member
.sd_
->symbol_names
!= NULL
))
1051 Archive::Should_include t
= obj
->should_include_member(symtab
,
1056 if (t
!= Archive::SHOULD_INCLUDE_YES
)
1062 this->include_member(symtab
, layout
, input_objects
, member
);
1064 added_new_object
= true;
1068 if (member
.sd_
!= NULL
)
1070 // The file must be locked in order to destroy the views
1071 // associated with it.
1072 gold_assert(obj
!= NULL
);
1073 obj
->lock(this->task_
);
1075 obj
->unlock(this->task_
);
1079 this->members_
[i
] = this->members_
.back();
1080 this->members_
.pop_back();
1083 while (added_new_object
);
1086 // Include a lib group member in the link.
1089 Lib_group::include_member(Symbol_table
* symtab
, Layout
* layout
,
1090 Input_objects
* input_objects
,
1091 const Archive_member
& member
)
1093 ++Lib_group::total_members_loaded
;
1095 Object
* obj
= member
.obj_
;
1096 gold_assert(obj
!= NULL
);
1098 Pluginobj
* pluginobj
= obj
->pluginobj();
1099 if (pluginobj
!= NULL
)
1101 pluginobj
->add_symbols(symtab
, NULL
, layout
);
1105 Read_symbols_data
* sd
= member
.sd_
;
1106 gold_assert(sd
!= NULL
);
1107 obj
->lock(this->task_
);
1108 if (input_objects
->add_object(obj
))
1110 // FIXME: Record incremental link info for --start-lib/--end-lib.
1111 if (layout
->incremental_inputs() != NULL
)
1112 layout
->incremental_inputs()->report_object(obj
, NULL
);
1113 obj
->layout(symtab
, layout
, sd
);
1114 obj
->add_symbols(symtab
, sd
, layout
);
1117 // Unlock the file for the next task.
1118 obj
->unlock(this->task_
);
1121 // Print statistical information to stderr. This is used for --stats.
1124 Lib_group::print_stats()
1126 fprintf(stderr
, _("%s: lib groups: %u\n"),
1127 program_name
, Lib_group::total_lib_groups
);
1128 fprintf(stderr
, _("%s: total lib groups members: %u\n"),
1129 program_name
, Lib_group::total_members
);
1130 fprintf(stderr
, _("%s: loaded lib groups members: %u\n"),
1131 program_name
, Lib_group::total_members_loaded
);
1135 Add_lib_group_symbols::is_runnable()
1137 if (this->readsyms_blocker_
!= NULL
&& this->readsyms_blocker_
->is_blocked())
1138 return this->readsyms_blocker_
;
1139 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1140 return this->this_blocker_
;
1145 Add_lib_group_symbols::locks(Task_locker
* tl
)
1147 tl
->add(this, this->next_blocker_
);
1151 Add_lib_group_symbols::run(Workqueue
*)
1153 this->lib_
->add_symbols(this->symtab_
, this->layout_
, this->input_objects_
);
1155 // FIXME: Record incremental link info for --start_lib/--end_lib.
1158 Add_lib_group_symbols::~Add_lib_group_symbols()
1160 if (this->this_blocker_
!= NULL
)
1161 delete this->this_blocker_
;
1162 // next_blocker_ is deleted by the task associated with the next
1166 } // End namespace gold.