1 // archive.cc -- archive support for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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 // Check whether the symbol was named in a -u option.
102 if (parameters->options().is_undefined(sym_name))
107 else if (layout->script_options()->is_referenced(sym_name))
109 size_t alc = 100 + strlen(sym_name);
110 char* buf = new char[alc];
111 snprintf(buf, alc, _("script or expression reference to %s"),
116 else if (strcmp(sym_name, parameters->entry()) == 0)
118 *why = "entry symbol ";
122 return Library_base::SHOULD_INCLUDE_UNKNOWN;
124 else if (!sym->is_undefined())
125 return Library_base::SHOULD_INCLUDE_NO;
126 // PR 12001: Do not include an archive when the undefined
127 // symbol has actually been defined on the command line.
128 else if (layout->script_options()->is_pending_assignment(sym_name))
129 return Library_base::SHOULD_INCLUDE_NO;
130 else if (sym->binding() == elfcpp::STB_WEAK)
131 return Library_base::SHOULD_INCLUDE_UNKNOWN;
133 return Library_base::SHOULD_INCLUDE_YES;
136 // The header of an entry in the archive. This is all readable text,
137 // padded with spaces where necessary. If the contents of an archive
138 // are all text file, the entire archive is readable.
140 struct Archive::Archive_header
144 // The file modification time.
146 // The user's UID in decimal.
148 // The user's GID in decimal.
150 // The file mode in octal.
152 // The file size in decimal.
154 // The final magic code.
158 // Class Archive static variables.
159 unsigned int Archive::total_archives;
160 unsigned int Archive::total_members;
161 unsigned int Archive::total_members_loaded;
165 const char Archive::armag[sarmag] =
167 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
170 const char Archive::armagt[sarmag] =
172 '!', '<', 't', 'h', 'i', 'n', '>', '\n'
175 const char Archive::arfmag[2] = { '`', '\n' };
177 Archive::Archive(const std::string& name, Input_file* input_file,
178 bool is_thin_archive, Dirsearch* dirpath, Task* task)
179 : Library_base(task), name_(name), input_file_(input_file), armap_(),
180 armap_names_(), extended_names_(), armap_checked_(), seen_offsets_(),
181 members_(), is_thin_archive_(is_thin_archive), included_member_(false),
182 nested_archives_(), dirpath_(dirpath), num_members_(0),
183 included_all_members_(false)
186 parameters->options().check_excluded_libs(input_file->found_name());
189 // Set up the archive: read the symbol map and the extended name
195 // We need to ignore empty archives.
196 if (this->input_file_->file().filesize() == sarmag)
199 // The first member of the archive should be the symbol table.
200 std::string armap_name;
201 off_t header_size = this->read_header(sarmag, false, &armap_name, NULL);
202 if (header_size == -1)
205 section_size_type armap_size = convert_to_section_size_type(header_size);
207 if (armap_name.empty())
209 this->read_armap(sarmag + sizeof(Archive_header), armap_size);
210 off = sarmag + sizeof(Archive_header) + armap_size;
212 else if (!this->input_file_->options().whole_archive())
213 gold_error(_("%s: no archive symbol table (run ranlib)"),
214 this->name().c_str());
216 // See if there is an extended name table. We cache these views
217 // because it is likely that we will want to read the following
218 // header in the add_symbols routine.
222 header_size = this->read_header(off, true, &xname, NULL);
223 if (header_size == -1)
226 section_size_type extended_size = convert_to_section_size_type(header_size);
229 const unsigned char* p = this->get_view(off + sizeof(Archive_header),
230 extended_size, false, true);
231 const char* px = reinterpret_cast<const char*>(p);
232 this->extended_names_.assign(px, extended_size);
234 bool preread_syms = (parameters->options().threads()
235 && parameters->options().preread_archive_symbols());
236 #ifndef ENABLE_THREADS
237 preread_syms = false;
239 if (parameters->options().has_plugins())
240 preread_syms = false;
243 this->read_all_symbols();
246 // Unlock any nested archives.
249 Archive::unlock_nested_archives()
251 for (Nested_archive_table::iterator p = this->nested_archives_.begin();
252 p != this->nested_archives_.end();
255 p->second->unlock(this->task_);
259 // Read the archive symbol map.
262 Archive::read_armap(off_t start, section_size_type size)
264 // To count the total number of archive members, we'll just count
265 // the number of times the file offset changes. Since most archives
266 // group the symbols in the armap by object, this ought to give us
267 // an accurate count.
268 off_t last_seen_offset = -1;
270 // Read in the entire armap.
271 const unsigned char* p = this->get_view(start, size, true, false);
273 // Numbers in the armap are always big-endian.
274 const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p);
275 unsigned int nsyms = elfcpp::Swap<32, true>::readval(pword);
278 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
279 const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
280 section_size_type names_size =
281 reinterpret_cast<const char*>(p) + size - pnames;
282 this->armap_names_.assign(pnames, names_size);
284 this->armap_.resize(nsyms);
286 section_offset_type name_offset = 0;
287 for (unsigned int i = 0; i < nsyms; ++i)
289 this->armap_[i].name_offset = name_offset;
290 this->armap_[i].file_offset = elfcpp::Swap<32, true>::readval(pword);
291 name_offset += strlen(pnames + name_offset) + 1;
293 if (this->armap_[i].file_offset != last_seen_offset)
295 last_seen_offset = this->armap_[i].file_offset;
296 ++this->num_members_;
300 if (static_cast<section_size_type>(name_offset) > names_size)
301 gold_error(_("%s: bad archive symbol table names"),
302 this->name().c_str());
304 // This array keeps track of which symbols are for archive elements
305 // which we have already included in the link.
306 this->armap_checked_.resize(nsyms);
309 // Read the header of an archive member at OFF. Fail if something
310 // goes wrong. Return the size of the member. Set *PNAME to the name
314 Archive::read_header(off_t off, bool cache, std::string* pname,
317 const unsigned char* p = this->get_view(off, sizeof(Archive_header), true,
319 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
320 return this->interpret_header(hdr, off, pname, nested_off);
323 // Interpret the header of HDR, the header of the archive member at
324 // file offset OFF. Return the size of the member, or -1 if something
325 // has gone wrong. Set *PNAME to the name of the member.
328 Archive::interpret_header(const Archive_header* hdr, off_t off,
329 std::string* pname, off_t* nested_off) const
331 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
333 gold_error(_("%s: malformed archive header at %zu"),
334 this->name().c_str(), static_cast<size_t>(off));
338 const int size_string_size = sizeof hdr->ar_size;
339 char size_string[size_string_size + 1];
340 memcpy(size_string, hdr->ar_size, size_string_size);
341 char* ps = size_string + size_string_size;
342 while (ps[-1] == ' ')
348 off_t member_size = strtol(size_string, &end, 10);
351 || (member_size == LONG_MAX && errno == ERANGE))
353 gold_error(_("%s: malformed archive header size at %zu"),
354 this->name().c_str(), static_cast<size_t>(off));
358 if (hdr->ar_name[0] != '/')
360 const char* name_end = strchr(hdr->ar_name, '/');
362 || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name))
364 gold_error(_("%s: malformed archive header name at %zu"),
365 this->name().c_str(), static_cast<size_t>(off));
368 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
369 if (nested_off != NULL)
372 else if (hdr->ar_name[1] == ' ')
374 // This is the symbol table.
378 else if (hdr->ar_name[1] == '/')
380 // This is the extended name table.
381 pname->assign(1, '/');
386 long x = strtol(hdr->ar_name + 1, &end, 10);
389 y = strtol(end + 1, &end, 10);
392 || (x == LONG_MAX && errno == ERANGE)
393 || static_cast<size_t>(x) >= this->extended_names_.size())
395 gold_error(_("%s: bad extended name index at %zu"),
396 this->name().c_str(), static_cast<size_t>(off));
400 const char* name = this->extended_names_.data() + x;
401 const char* name_end = strchr(name, '\n');
402 if (static_cast<size_t>(name_end - name) > this->extended_names_.size()
403 || name_end[-1] != '/')
405 gold_error(_("%s: bad extended name entry at header %zu"),
406 this->name().c_str(), static_cast<size_t>(off));
409 pname->assign(name, name_end - 1 - name);
410 if (nested_off != NULL)
417 // An archive member iterator.
419 class Archive::const_iterator
422 // The header of an archive member. This is what this iterator
426 // The name of the member.
428 // The file offset of the member.
430 // The file offset of a nested archive member.
432 // The size of the member.
436 const_iterator(Archive* archive, off_t off)
437 : archive_(archive), off_(off)
438 { this->read_next_header(); }
442 { return this->header_; }
446 { return &this->header_; }
451 if (this->off_ == this->archive_->file().filesize())
453 this->off_ += sizeof(Archive_header);
454 if (!this->archive_->is_thin_archive())
455 this->off_ += this->header_.size;
456 if ((this->off_ & 1) != 0)
458 this->read_next_header();
465 const_iterator ret = *this;
471 operator==(const const_iterator p) const
472 { return this->off_ == p->off; }
475 operator!=(const const_iterator p) const
476 { return this->off_ != p->off; }
482 // The underlying archive.
484 // The current offset in the file.
486 // The current archive header.
490 // Read the next archive header.
493 Archive::const_iterator::read_next_header()
495 off_t filesize = this->archive_->file().filesize();
498 if (filesize - this->off_ < static_cast<off_t>(sizeof(Archive_header)))
500 if (filesize != this->off_)
502 gold_error(_("%s: short archive header at %zu"),
503 this->archive_->filename().c_str(),
504 static_cast<size_t>(this->off_));
505 this->off_ = filesize;
507 this->header_.off = filesize;
511 unsigned char buf[sizeof(Archive_header)];
512 this->archive_->file().read(this->off_, sizeof(Archive_header), buf);
514 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(buf);
515 off_t size = this->archive_->interpret_header(hdr, this->off_,
517 &this->header_.nested_off);
520 this->header_.off = filesize;
524 this->header_.size = size;
525 this->header_.off = this->off_;
527 // Skip special members.
528 if (!this->header_.name.empty() && this->header_.name != "/")
531 this->off_ += sizeof(Archive_header) + this->header_.size;
532 if ((this->off_ & 1) != 0)
539 Archive::const_iterator
542 return Archive::const_iterator(this, sarmag);
547 Archive::const_iterator
550 return Archive::const_iterator(this, this->input_file_->file().filesize());
553 // Get the file and offset for an archive member, which may be an
554 // external member of a thin archive. Set *INPUT_FILE to the
555 // file containing the actual member, *MEMOFF to the offset
556 // within that file (0 if not a nested archive), and *MEMBER_NAME
557 // to the name of the archive member. Return TRUE on success.
560 Archive::get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
561 off_t* memsize, std::string* member_name)
565 *memsize = this->read_header(off, false, member_name, &nested_off);
569 *input_file = this->input_file_;
570 *memoff = off + static_cast<off_t>(sizeof(Archive_header));
572 if (!this->is_thin_archive_)
575 // Adjust a relative pathname so that it is relative
576 // to the directory containing the archive.
577 if (!IS_ABSOLUTE_PATH(member_name->c_str()))
579 const char* arch_path = this->filename().c_str();
580 const char* basename = lbasename(arch_path);
581 if (basename > arch_path)
582 member_name->replace(0, 0,
583 this->filename().substr(0, basename - arch_path));
588 // This is a member of a nested archive. Open the containing
589 // archive if we don't already have it open, then do a recursive
590 // call to include the member from that archive.
592 Nested_archive_table::const_iterator p =
593 this->nested_archives_.find(*member_name);
594 if (p != this->nested_archives_.end())
598 Input_file_argument* input_file_arg =
599 new Input_file_argument(member_name->c_str(),
600 Input_file_argument::INPUT_FILE_TYPE_FILE,
601 "", false, parameters->options());
602 *input_file = new Input_file(input_file_arg);
604 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
606 arch = new Archive(*member_name, *input_file, false, this->dirpath_,
609 std::pair<Nested_archive_table::iterator, bool> ins =
610 this->nested_archives_.insert(std::make_pair(*member_name, arch));
611 gold_assert(ins.second);
613 return arch->get_file_and_offset(nested_off, input_file, memoff,
614 memsize, member_name);
617 // This is an external member of a thin archive. Open the
618 // file as a regular relocatable object file.
619 Input_file_argument* input_file_arg =
620 new Input_file_argument(member_name->c_str(),
621 Input_file_argument::INPUT_FILE_TYPE_FILE,
622 "", false, this->input_file_->options());
623 *input_file = new Input_file(input_file_arg);
625 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
629 *memsize = (*input_file)->file().filesize();
633 // Return an ELF object for the member at offset OFF. If
634 // PUNCONFIGURED is not NULL, then if the ELF object has an
635 // unsupported target type, set *PUNCONFIGURED to true and return
639 Archive::get_elf_object_for_member(off_t off, bool* punconfigured)
641 if (punconfigured != NULL)
642 *punconfigured = false;
644 Input_file* input_file;
647 std::string member_name;
648 if (!this->get_file_and_offset(off, &input_file, &memoff, &memsize,
652 if (parameters->options().has_plugins())
654 Object* obj = parameters->options().plugins()->claim_file(input_file,
660 // The input file was claimed by a plugin, and its symbols
661 // have been provided by the plugin.
666 const unsigned char* ehdr;
668 if (!is_elf_object(input_file, memoff, &ehdr, &read_size))
670 gold_error(_("%s: member at %zu is not an ELF object"),
671 this->name().c_str(), static_cast<size_t>(off));
675 Object* obj = make_elf_object((std::string(this->input_file_->filename())
676 + "(" + member_name + ")"),
677 input_file, memoff, ehdr, read_size,
681 obj->set_no_export(this->no_export());
685 // Read the symbols from all the archive members in the link.
688 Archive::read_all_symbols()
690 for (Archive::const_iterator p = this->begin();
693 this->read_symbols(p->off);
696 // Read the symbols from an archive member in the link. OFF is the file
697 // offset of the member header.
700 Archive::read_symbols(off_t off)
702 Object* obj = this->get_elf_object_for_member(off, NULL);
706 Read_symbols_data* sd = new Read_symbols_data;
707 obj->read_symbols(sd);
708 Archive_member member(obj, sd);
709 this->members_[off] = member;
712 // Select members from the archive and add them to the link. We walk
713 // through the elements in the archive map, and look each one up in
714 // the symbol table. If it exists as a strong undefined symbol, we
715 // pull in the corresponding element. We have to do this in a loop,
716 // since pulling in one element may create new undefined symbols which
717 // may be satisfied by other objects in the archive. Return true in
718 // the normal case, false if the first member we tried to add from
719 // this archive had an incompatible target.
722 Archive::add_symbols(Symbol_table* symtab, Layout* layout,
723 Input_objects* input_objects, Mapfile* mapfile)
725 ++Archive::total_archives;
727 if (this->input_file_->options().whole_archive())
728 return this->include_all_members(symtab, layout, input_objects,
731 Archive::total_members += this->num_members_;
733 input_objects->archive_start(this);
735 const size_t armap_size = this->armap_.size();
737 // This is a quick optimization, since we usually see many symbols
738 // in a row with the same offset. last_seen_offset holds the last
739 // offset we saw that was present in the seen_offsets_ set.
740 off_t last_seen_offset = -1;
742 // Track which symbols in the symbol table we've already found to be
746 size_t tmpbuflen = 0;
747 bool added_new_object;
750 added_new_object = false;
751 for (size_t i = 0; i < armap_size; ++i)
753 if (this->armap_checked_[i])
755 if (this->armap_[i].file_offset == last_seen_offset)
757 this->armap_checked_[i] = true;
760 if (this->seen_offsets_.find(this->armap_[i].file_offset)
761 != this->seen_offsets_.end())
763 this->armap_checked_[i] = true;
764 last_seen_offset = this->armap_[i].file_offset;
768 const char* sym_name = (this->armap_names_.data()
769 + this->armap_[i].name_offset);
773 Archive::Should_include t =
774 Archive::should_include_member(symtab, layout, sym_name, &sym,
775 &why, &tmpbuf, &tmpbuflen);
777 if (t == Archive::SHOULD_INCLUDE_NO
778 || t == Archive::SHOULD_INCLUDE_YES)
779 this->armap_checked_[i] = true;
781 if (t != Archive::SHOULD_INCLUDE_YES)
784 // We want to include this object in the link.
785 last_seen_offset = this->armap_[i].file_offset;
786 this->seen_offsets_.insert(last_seen_offset);
788 if (!this->include_member(symtab, layout, input_objects,
789 last_seen_offset, mapfile, sym,
797 added_new_object = true;
800 while (added_new_object);
805 input_objects->archive_stop(this);
810 // Return whether the archive includes a member which defines the
814 Archive::defines_symbol(Symbol* sym) const
816 const char* symname = sym->name();
817 size_t symname_len = strlen(symname);
818 size_t armap_size = this->armap_.size();
819 for (size_t i = 0; i < armap_size; ++i)
821 if (this->armap_checked_[i])
823 const char* archive_symname = (this->armap_names_.data()
824 + this->armap_[i].name_offset);
825 if (strncmp(archive_symname, symname, symname_len) != 0)
827 char c = archive_symname[symname_len];
828 if (c == '\0' && sym->version() == NULL)
832 const char* ver = archive_symname + symname_len + 1;
835 if (sym->version() == NULL)
839 if (sym->version() != NULL && strcmp(sym->version(), ver) == 0)
846 // Include all the archive members in the link. This is for --whole-archive.
849 Archive::include_all_members(Symbol_table* symtab, Layout* layout,
850 Input_objects* input_objects, Mapfile* mapfile)
852 // Don't include the same archive twice. This can happen if
853 // --whole-archive is nested inside --start-group (PR gold/12163).
854 if (this->included_all_members_)
857 this->included_all_members_ = true;
859 input_objects->archive_start(this);
861 if (this->members_.size() > 0)
863 std::map<off_t, Archive_member>::const_iterator p;
864 for (p = this->members_.begin();
865 p != this->members_.end();
868 if (!this->include_member(symtab, layout, input_objects, p->first,
869 mapfile, NULL, "--whole-archive"))
871 ++Archive::total_members;
876 for (Archive::const_iterator p = this->begin();
880 if (!this->include_member(symtab, layout, input_objects, p->off,
881 mapfile, NULL, "--whole-archive"))
883 ++Archive::total_members;
887 input_objects->archive_stop(this);
892 // Return the number of members in the archive. This is only used for
896 Archive::count_members()
899 for (Archive::const_iterator p = this->begin();
906 // Include an archive member in the link. OFF is the file offset of
907 // the member header. WHY is the reason we are including this member.
908 // Return true if we added the member or if we had an error, return
909 // false if this was the first member we tried to add from this
910 // archive and it had an incompatible format.
913 Archive::include_member(Symbol_table* symtab, Layout* layout,
914 Input_objects* input_objects, off_t off,
915 Mapfile* mapfile, Symbol* sym, const char* why)
917 ++Archive::total_members_loaded;
919 std::map<off_t, Archive_member>::const_iterator p = this->members_.find(off);
920 if (p != this->members_.end())
922 Object* obj = p->second.obj_;
924 Read_symbols_data* sd = p->second.sd_;
926 mapfile->report_include_archive_member(obj->name(), sym, why);
927 if (input_objects->add_object(obj))
929 obj->layout(symtab, layout, sd);
930 obj->add_symbols(symtab, sd, layout);
931 this->included_member_ = true;
937 // If this is the first object we are including from this archive,
938 // and we searched for this archive, most likely because it was
939 // found via a -l option, then if the target is incompatible we want
940 // to move on to the next archive found in the search path.
941 bool unconfigured = false;
942 bool* punconfigured = NULL;
943 if (!this->included_member_ && this->searched_for())
944 punconfigured = &unconfigured;
946 Object* obj = this->get_elf_object_for_member(off, punconfigured);
949 // Return false to search for another archive, true if we found
951 return unconfigured ? false : true;
955 mapfile->report_include_archive_member(obj->name(), sym, why);
957 Pluginobj* pluginobj = obj->pluginobj();
958 if (pluginobj != NULL)
960 pluginobj->add_symbols(symtab, NULL, layout);
961 this->included_member_ = true;
965 if (!input_objects->add_object(obj))
967 // If this is an external member of a thin archive, unlock the
969 if (obj->offset() == 0)
970 obj->unlock(this->task_);
976 if (layout->incremental_inputs() != NULL)
977 layout->incremental_inputs()->report_object(obj, 0, this, NULL);
978 Read_symbols_data sd;
979 obj->read_symbols(&sd);
980 obj->layout(symtab, layout, &sd);
981 obj->add_symbols(symtab, &sd, layout);
984 // If this is an external member of a thin archive, unlock the file
985 // for the next task.
986 if (obj->offset() == 0)
987 obj->unlock(this->task_);
989 this->included_member_ = true;
995 // Iterate over all unused symbols, and call the visitor class V for each.
998 Archive::do_for_all_unused_symbols(Symbol_visitor_base* v) const
1000 for (std::vector<Armap_entry>::const_iterator p = this->armap_.begin();
1001 p != this->armap_.end();
1004 if (this->seen_offsets_.find(p->file_offset)
1005 == this->seen_offsets_.end())
1006 v->visit(this->armap_names_.data() + p->name_offset);
1010 // Print statistical information to stderr. This is used for --stats.
1013 Archive::print_stats()
1015 fprintf(stderr, _("%s: archive libraries: %u\n"),
1016 program_name, Archive::total_archives);
1017 fprintf(stderr, _("%s: total archive members: %u\n"),
1018 program_name, Archive::total_members);
1019 fprintf(stderr, _("%s: loaded archive members: %u\n"),
1020 program_name, Archive::total_members_loaded);
1023 // Add_archive_symbols methods.
1025 Add_archive_symbols::~Add_archive_symbols()
1027 if (this->this_blocker_ != NULL)
1028 delete this->this_blocker_;
1029 // next_blocker_ is deleted by the task associated with the next
1033 // Return whether we can add the archive symbols. We are blocked by
1034 // this_blocker_. We block next_blocker_. We also lock the file.
1037 Add_archive_symbols::is_runnable()
1039 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1040 return this->this_blocker_;
1045 Add_archive_symbols::locks(Task_locker* tl)
1047 tl->add(this, this->next_blocker_);
1048 tl->add(this, this->archive_->token());
1052 Add_archive_symbols::run(Workqueue* workqueue)
1054 // For an incremental link, begin recording layout information.
1055 Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs();
1056 if (incremental_inputs != NULL)
1058 unsigned int arg_serial = this->input_argument_->file().arg_serial();
1059 Script_info* script_info = this->input_argument_->script_info();
1060 incremental_inputs->report_archive_begin(this->archive_, arg_serial,
1064 bool added = this->archive_->add_symbols(this->symtab_, this->layout_,
1065 this->input_objects_,
1067 this->archive_->unlock_nested_archives();
1069 this->archive_->release();
1070 this->archive_->clear_uncached_views();
1074 // This archive holds object files which are incompatible with
1076 Read_symbols::incompatible_warning(this->input_argument_,
1077 this->archive_->input_file());
1078 Read_symbols::requeue(workqueue, this->input_objects_, this->symtab_,
1079 this->layout_, this->dirpath_, this->dirindex_,
1080 this->mapfile_, this->input_argument_,
1081 this->input_group_, this->next_blocker_);
1082 delete this->archive_;
1086 if (this->input_group_ != NULL)
1087 this->input_group_->add_archive(this->archive_);
1090 // For an incremental link, finish recording the layout information.
1091 if (incremental_inputs != NULL)
1092 incremental_inputs->report_archive_end(this->archive_);
1094 if (!parameters->options().has_plugins()
1095 || this->archive_->input_file()->options().whole_archive())
1097 // We no longer need to know about this archive.
1098 delete this->archive_;
1102 // The plugin interface may want to rescan this archive.
1103 parameters->options().plugins()->save_archive(this->archive_);
1106 this->archive_ = NULL;
1110 // Class Lib_group static variables.
1111 unsigned int Lib_group::total_lib_groups;
1112 unsigned int Lib_group::total_members;
1113 unsigned int Lib_group::total_members_loaded;
1115 Lib_group::Lib_group(const Input_file_lib* lib, Task* task)
1116 : Library_base(task), lib_(lib), members_()
1118 this->members_.resize(lib->size());
1122 Lib_group::do_filename() const
1124 std::string *filename = new std::string("/group/");
1128 // Select members from the lib group and add them to the link. We walk
1129 // through the members, and check if each one up should be included.
1130 // If the object says it should be included, we do so. We have to do
1131 // this in a loop, since including one member may create new undefined
1132 // symbols which may be satisfied by other members.
1135 Lib_group::add_symbols(Symbol_table* symtab, Layout* layout,
1136 Input_objects* input_objects)
1138 ++Lib_group::total_lib_groups;
1140 Lib_group::total_members += this->members_.size();
1142 bool added_new_object;
1145 added_new_object = false;
1147 while (i < this->members_.size())
1149 const Archive_member& member = this->members_[i];
1150 Object* obj = member.obj_;
1153 // Skip files with no symbols. Plugin objects have
1154 // member.sd_ == NULL.
1156 && (member.sd_ == NULL || member.sd_->symbol_names != NULL))
1158 Archive::Should_include t = obj->should_include_member(symtab,
1163 if (t != Archive::SHOULD_INCLUDE_YES)
1169 this->include_member(symtab, layout, input_objects, member);
1171 added_new_object = true;
1175 if (member.sd_ != NULL)
1177 // The file must be locked in order to destroy the views
1178 // associated with it.
1179 gold_assert(obj != NULL);
1180 obj->lock(this->task_);
1182 obj->unlock(this->task_);
1186 this->members_[i] = this->members_.back();
1187 this->members_.pop_back();
1190 while (added_new_object);
1193 // Include a lib group member in the link.
1196 Lib_group::include_member(Symbol_table* symtab, Layout* layout,
1197 Input_objects* input_objects,
1198 const Archive_member& member)
1200 ++Lib_group::total_members_loaded;
1202 Object* obj = member.obj_;
1203 gold_assert(obj != NULL);
1205 Pluginobj* pluginobj = obj->pluginobj();
1206 if (pluginobj != NULL)
1208 pluginobj->add_symbols(symtab, NULL, layout);
1212 Read_symbols_data* sd = member.sd_;
1213 gold_assert(sd != NULL);
1214 obj->lock(this->task_);
1215 if (input_objects->add_object(obj))
1217 if (layout->incremental_inputs() != NULL)
1218 layout->incremental_inputs()->report_object(obj, member.arg_serial_,
1220 obj->layout(symtab, layout, sd);
1221 obj->add_symbols(symtab, sd, layout);
1224 // Unlock the file for the next task.
1225 obj->unlock(this->task_);
1228 // Iterate over all unused symbols, and call the visitor class V for each.
1231 Lib_group::do_for_all_unused_symbols(Symbol_visitor_base* v) const
1233 // Files are removed from the members list when used, so all the
1234 // files remaining on the list are unused.
1235 for (std::vector<Archive_member>::const_iterator p = this->members_.begin();
1236 p != this->members_.end();
1239 Object* obj = p->obj_;
1240 obj->for_all_global_symbols(p->sd_, v);
1244 // Print statistical information to stderr. This is used for --stats.
1247 Lib_group::print_stats()
1249 fprintf(stderr, _("%s: lib groups: %u\n"),
1250 program_name, Lib_group::total_lib_groups);
1251 fprintf(stderr, _("%s: total lib groups members: %u\n"),
1252 program_name, Lib_group::total_members);
1253 fprintf(stderr, _("%s: loaded lib groups members: %u\n"),
1254 program_name, Lib_group::total_members_loaded);
1258 Add_lib_group_symbols::is_runnable()
1260 if (this->readsyms_blocker_ != NULL && this->readsyms_blocker_->is_blocked())
1261 return this->readsyms_blocker_;
1262 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1263 return this->this_blocker_;
1268 Add_lib_group_symbols::locks(Task_locker* tl)
1270 tl->add(this, this->next_blocker_);
1274 Add_lib_group_symbols::run(Workqueue*)
1276 // For an incremental link, begin recording layout information.
1277 Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs();
1278 if (incremental_inputs != NULL)
1279 incremental_inputs->report_archive_begin(this->lib_, 0, NULL);
1281 this->lib_->add_symbols(this->symtab_, this->layout_, this->input_objects_);
1283 if (incremental_inputs != NULL)
1284 incremental_inputs->report_archive_end(this->lib_);
1287 Add_lib_group_symbols::~Add_lib_group_symbols()
1289 if (this->this_blocker_ != NULL)
1290 delete this->this_blocker_;
1291 // next_blocker_ is deleted by the task associated with the next
1295 } // End namespace gold.