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"
45 // The header of an entry in the archive. This is all readable text,
46 // padded with spaces where necesary. If the contents of an archive
47 // are all text file, the entire archive is readable.
49 struct Archive::Archive_header
53 // The file modification time.
55 // The user's UID in decimal.
57 // The user's GID in decimal.
59 // The file mode in octal.
61 // The file size in decimal.
63 // The final magic code.
67 // Class Archive static variables.
68 unsigned int Archive::total_archives;
69 unsigned int Archive::total_members;
70 unsigned int Archive::total_members_loaded;
74 const char Archive::armag[sarmag] =
76 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
79 const char Archive::armagt[sarmag] =
81 '!', '<', 't', 'h', 'i', 'n', '>', '\n'
84 const char Archive::arfmag[2] = { '`', '\n' };
86 Archive::Archive(const std::string& name, Input_file* input_file,
87 bool is_thin_archive, Dirsearch* dirpath, Task* task)
88 : name_(name), input_file_(input_file), armap_(), armap_names_(),
89 extended_names_(), armap_checked_(), seen_offsets_(), members_(),
90 is_thin_archive_(is_thin_archive), included_member_(false),
91 nested_archives_(), dirpath_(dirpath), task_(task), num_members_(0)
94 parameters->options().check_excluded_libs(input_file->found_name());
97 // Set up the archive: read the symbol map and the extended name
103 // We need to ignore empty archives.
104 if (this->input_file_->file().filesize() == sarmag)
107 // The first member of the archive should be the symbol table.
108 std::string armap_name;
109 section_size_type armap_size =
110 convert_to_section_size_type(this->read_header(sarmag, false,
113 if (armap_name.empty())
115 this->read_armap(sarmag + sizeof(Archive_header), armap_size);
116 off = sarmag + sizeof(Archive_header) + armap_size;
118 else if (!this->input_file_->options().whole_archive())
119 gold_error(_("%s: no archive symbol table (run ranlib)"),
120 this->name().c_str());
122 // See if there is an extended name table. We cache these views
123 // because it is likely that we will want to read the following
124 // header in the add_symbols routine.
128 section_size_type extended_size =
129 convert_to_section_size_type(this->read_header(off, true, &xname, NULL));
132 const unsigned char* p = this->get_view(off + sizeof(Archive_header),
133 extended_size, false, true);
134 const char* px = reinterpret_cast<const char*>(p);
135 this->extended_names_.assign(px, extended_size);
137 bool preread_syms = (parameters->options().threads()
138 && parameters->options().preread_archive_symbols());
139 #ifndef ENABLE_THREADS
140 preread_syms = false;
142 if (parameters->options().has_plugins())
143 preread_syms = false;
146 this->read_all_symbols();
149 // Unlock any nested archives.
152 Archive::unlock_nested_archives()
154 for (Nested_archive_table::iterator p = this->nested_archives_.begin();
155 p != this->nested_archives_.end();
158 p->second->unlock(this->task_);
162 // Read the archive symbol map.
165 Archive::read_armap(off_t start, section_size_type size)
167 // To count the total number of archive members, we'll just count
168 // the number of times the file offset changes. Since most archives
169 // group the symbols in the armap by object, this ought to give us
170 // an accurate count.
171 off_t last_seen_offset = -1;
173 // Read in the entire armap.
174 const unsigned char* p = this->get_view(start, size, true, false);
176 // Numbers in the armap are always big-endian.
177 const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p);
178 unsigned int nsyms = elfcpp::Swap<32, true>::readval(pword);
181 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
182 const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
183 section_size_type names_size =
184 reinterpret_cast<const char*>(p) + size - pnames;
185 this->armap_names_.assign(pnames, names_size);
187 this->armap_.resize(nsyms);
189 section_offset_type name_offset = 0;
190 for (unsigned int i = 0; i < nsyms; ++i)
192 this->armap_[i].name_offset = name_offset;
193 this->armap_[i].file_offset = elfcpp::Swap<32, true>::readval(pword);
194 name_offset += strlen(pnames + name_offset) + 1;
196 if (this->armap_[i].file_offset != last_seen_offset)
198 last_seen_offset = this->armap_[i].file_offset;
199 ++this->num_members_;
203 if (static_cast<section_size_type>(name_offset) > names_size)
204 gold_error(_("%s: bad archive symbol table names"),
205 this->name().c_str());
207 // This array keeps track of which symbols are for archive elements
208 // which we have already included in the link.
209 this->armap_checked_.resize(nsyms);
212 // Read the header of an archive member at OFF. Fail if something
213 // goes wrong. Return the size of the member. Set *PNAME to the name
217 Archive::read_header(off_t off, bool cache, std::string* pname,
220 const unsigned char* p = this->get_view(off, sizeof(Archive_header), true,
222 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
223 return this->interpret_header(hdr, off, pname, nested_off);
226 // Interpret the header of HDR, the header of the archive member at
227 // file offset OFF. Fail if something goes wrong. Return the size of
228 // the member. Set *PNAME to the name of the member.
231 Archive::interpret_header(const Archive_header* hdr, off_t off,
232 std::string* pname, off_t* nested_off) const
234 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
236 gold_error(_("%s: malformed archive header at %zu"),
237 this->name().c_str(), static_cast<size_t>(off));
238 return this->input_file_->file().filesize() - off;
241 const int size_string_size = sizeof hdr->ar_size;
242 char size_string[size_string_size + 1];
243 memcpy(size_string, hdr->ar_size, size_string_size);
244 char* ps = size_string + size_string_size;
245 while (ps[-1] == ' ')
251 off_t member_size = strtol(size_string, &end, 10);
254 || (member_size == LONG_MAX && errno == ERANGE))
256 gold_error(_("%s: malformed archive header size at %zu"),
257 this->name().c_str(), static_cast<size_t>(off));
258 return this->input_file_->file().filesize() - off;
261 if (hdr->ar_name[0] != '/')
263 const char* name_end = strchr(hdr->ar_name, '/');
265 || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name))
267 gold_error(_("%s: malformed archive header name at %zu"),
268 this->name().c_str(), static_cast<size_t>(off));
269 return this->input_file_->file().filesize() - off;
271 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
272 if (nested_off != NULL)
275 else if (hdr->ar_name[1] == ' ')
277 // This is the symbol table.
281 else if (hdr->ar_name[1] == '/')
283 // This is the extended name table.
284 pname->assign(1, '/');
289 long x = strtol(hdr->ar_name + 1, &end, 10);
292 y = strtol(end + 1, &end, 10);
295 || (x == LONG_MAX && errno == ERANGE)
296 || static_cast<size_t>(x) >= this->extended_names_.size())
298 gold_error(_("%s: bad extended name index at %zu"),
299 this->name().c_str(), static_cast<size_t>(off));
300 return this->input_file_->file().filesize() - off;
303 const char* name = this->extended_names_.data() + x;
304 const char* name_end = strchr(name, '\n');
305 if (static_cast<size_t>(name_end - name) > this->extended_names_.size()
306 || name_end[-1] != '/')
308 gold_error(_("%s: bad extended name entry at header %zu"),
309 this->name().c_str(), static_cast<size_t>(off));
310 return this->input_file_->file().filesize() - off;
312 pname->assign(name, name_end - 1 - name);
313 if (nested_off != NULL)
320 // An archive member iterator.
322 class Archive::const_iterator
325 // The header of an archive member. This is what this iterator
329 // The name of the member.
331 // The file offset of the member.
333 // The file offset of a nested archive member.
335 // The size of the member.
339 const_iterator(Archive* archive, off_t off)
340 : archive_(archive), off_(off)
341 { this->read_next_header(); }
345 { return this->header_; }
349 { return &this->header_; }
354 if (this->off_ == this->archive_->file().filesize())
356 this->off_ += sizeof(Archive_header);
357 if (!this->archive_->is_thin_archive())
358 this->off_ += this->header_.size;
359 if ((this->off_ & 1) != 0)
361 this->read_next_header();
368 const_iterator ret = *this;
374 operator==(const const_iterator p) const
375 { return this->off_ == p->off; }
378 operator!=(const const_iterator p) const
379 { return this->off_ != p->off; }
385 // The underlying archive.
387 // The current offset in the file.
389 // The current archive header.
393 // Read the next archive header.
396 Archive::const_iterator::read_next_header()
398 off_t filesize = this->archive_->file().filesize();
401 if (filesize - this->off_ < static_cast<off_t>(sizeof(Archive_header)))
403 if (filesize != this->off_)
405 gold_error(_("%s: short archive header at %zu"),
406 this->archive_->filename().c_str(),
407 static_cast<size_t>(this->off_));
408 this->off_ = filesize;
410 this->header_.off = filesize;
414 unsigned char buf[sizeof(Archive_header)];
415 this->archive_->file().read(this->off_, sizeof(Archive_header), buf);
417 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(buf);
419 this->archive_->interpret_header(hdr, this->off_, &this->header_.name,
420 &this->header_.nested_off);
421 this->header_.off = this->off_;
423 // Skip special members.
424 if (!this->header_.name.empty() && this->header_.name != "/")
427 this->off_ += sizeof(Archive_header) + this->header_.size;
428 if ((this->off_ & 1) != 0)
435 Archive::const_iterator
438 return Archive::const_iterator(this, sarmag);
443 Archive::const_iterator
446 return Archive::const_iterator(this, this->input_file_->file().filesize());
449 // Get the file and offset for an archive member, which may be an
450 // external member of a thin archive. Set *INPUT_FILE to the
451 // file containing the actual member, *MEMOFF to the offset
452 // within that file (0 if not a nested archive), and *MEMBER_NAME
453 // to the name of the archive member. Return TRUE on success.
456 Archive::get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
457 off_t* memsize, std::string* member_name)
461 *memsize = this->read_header(off, false, member_name, &nested_off);
463 *input_file = this->input_file_;
464 *memoff = off + static_cast<off_t>(sizeof(Archive_header));
466 if (!this->is_thin_archive_)
469 // Adjust a relative pathname so that it is relative
470 // to the directory containing the archive.
471 if (!IS_ABSOLUTE_PATH(member_name->c_str()))
473 const char* arch_path = this->filename().c_str();
474 const char* basename = lbasename(arch_path);
475 if (basename > arch_path)
476 member_name->replace(0, 0,
477 this->filename().substr(0, basename - arch_path));
482 // This is a member of a nested archive. Open the containing
483 // archive if we don't already have it open, then do a recursive
484 // call to include the member from that archive.
486 Nested_archive_table::const_iterator p =
487 this->nested_archives_.find(*member_name);
488 if (p != this->nested_archives_.end())
492 Input_file_argument* input_file_arg =
493 new Input_file_argument(member_name->c_str(),
494 Input_file_argument::INPUT_FILE_TYPE_FILE,
495 "", false, parameters->options());
496 *input_file = new Input_file(input_file_arg);
498 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
500 arch = new Archive(*member_name, *input_file, false, this->dirpath_,
503 std::pair<Nested_archive_table::iterator, bool> ins =
504 this->nested_archives_.insert(std::make_pair(*member_name, arch));
505 gold_assert(ins.second);
507 return arch->get_file_and_offset(nested_off, input_file, memoff,
508 memsize, member_name);
511 // This is an external member of a thin archive. Open the
512 // file as a regular relocatable object file.
513 Input_file_argument* input_file_arg =
514 new Input_file_argument(member_name->c_str(),
515 Input_file_argument::INPUT_FILE_TYPE_FILE,
516 "", false, this->input_file_->options());
517 *input_file = new Input_file(input_file_arg);
519 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
523 *memsize = (*input_file)->file().filesize();
527 // Return an ELF object for the member at offset OFF. If the ELF
528 // object has an unsupported target type, set *PUNCONFIGURED to true
532 Archive::get_elf_object_for_member(off_t off, bool* punconfigured)
534 *punconfigured = false;
536 Input_file* input_file;
539 std::string member_name;
540 if (!this->get_file_and_offset(off, &input_file, &memoff, &memsize,
544 if (parameters->options().has_plugins())
546 Object* obj = parameters->options().plugins()->claim_file(input_file,
551 // The input file was claimed by a plugin, and its symbols
552 // have been provided by the plugin.
557 const unsigned char* ehdr;
559 if (!is_elf_object(input_file, memoff, &ehdr, &read_size))
561 gold_error(_("%s: member at %zu is not an ELF object"),
562 this->name().c_str(), static_cast<size_t>(off));
566 Object *obj = make_elf_object((std::string(this->input_file_->filename())
567 + "(" + member_name + ")"),
568 input_file, memoff, ehdr, read_size,
572 obj->set_no_export(this->no_export());
576 // Read the symbols from all the archive members in the link.
579 Archive::read_all_symbols()
581 for (Archive::const_iterator p = this->begin();
584 this->read_symbols(p->off);
587 // Read the symbols from an archive member in the link. OFF is the file
588 // offset of the member header.
591 Archive::read_symbols(off_t off)
594 Object* obj = this->get_elf_object_for_member(off, &dummy);
599 Read_symbols_data* sd = new Read_symbols_data;
600 obj->read_symbols(sd);
601 Archive_member member(obj, sd);
602 this->members_[off] = member;
605 // Select members from the archive and add them to the link. We walk
606 // through the elements in the archive map, and look each one up in
607 // the symbol table. If it exists as a strong undefined symbol, we
608 // pull in the corresponding element. We have to do this in a loop,
609 // since pulling in one element may create new undefined symbols which
610 // may be satisfied by other objects in the archive. Return true in
611 // the normal case, false if the first member we tried to add from
612 // this archive had an incompatible target.
615 Archive::add_symbols(Symbol_table* symtab, Layout* layout,
616 Input_objects* input_objects, Mapfile* mapfile)
618 ++Archive::total_archives;
620 if (this->input_file_->options().whole_archive())
621 return this->include_all_members(symtab, layout, input_objects,
624 Archive::total_members += this->num_members_;
626 input_objects->archive_start(this);
628 const size_t armap_size = this->armap_.size();
630 // This is a quick optimization, since we usually see many symbols
631 // in a row with the same offset. last_seen_offset holds the last
632 // offset we saw that was present in the seen_offsets_ set.
633 off_t last_seen_offset = -1;
635 // Track which symbols in the symbol table we've already found to be
639 size_t tmpbuflen = 0;
640 bool added_new_object;
643 added_new_object = false;
644 for (size_t i = 0; i < armap_size; ++i)
646 if (this->armap_checked_[i])
648 if (this->armap_[i].file_offset == last_seen_offset)
650 this->armap_checked_[i] = true;
653 if (this->seen_offsets_.find(this->armap_[i].file_offset)
654 != this->seen_offsets_.end())
656 this->armap_checked_[i] = true;
657 last_seen_offset = this->armap_[i].file_offset;
661 const char* sym_name = (this->armap_names_.data()
662 + this->armap_[i].name_offset);
664 // In an object file, and therefore in an archive map, an
665 // '@' in the name separates the symbol name from the
666 // version name. If there are two '@' characters, this is
667 // the default version.
668 const char* ver = strchr(sym_name, '@');
672 size_t symlen = ver - sym_name;
673 if (symlen + 1 > tmpbuflen)
675 tmpbuf = static_cast<char*>(realloc(tmpbuf, symlen + 1));
676 tmpbuflen = symlen + 1;
678 memcpy(tmpbuf, sym_name, symlen);
679 tmpbuf[symlen] = '\0';
690 Symbol* sym = symtab->lookup(sym_name, ver);
694 || !sym->is_undefined()
695 || sym->binding() == elfcpp::STB_WEAK))
696 sym = symtab->lookup(sym_name, NULL);
700 // Check whether the symbol was named in a -u option.
701 if (!parameters->options().is_undefined(sym_name))
704 else if (!sym->is_undefined())
706 this->armap_checked_[i] = true;
709 else if (sym->binding() == elfcpp::STB_WEAK)
712 // We want to include this object in the link.
713 last_seen_offset = this->armap_[i].file_offset;
714 this->seen_offsets_.insert(last_seen_offset);
715 this->armap_checked_[i] = true;
723 if (!this->include_member(symtab, layout, input_objects,
724 last_seen_offset, mapfile, sym,
732 added_new_object = true;
735 while (added_new_object);
740 input_objects->archive_stop(this);
745 // Include all the archive members in the link. This is for --whole-archive.
748 Archive::include_all_members(Symbol_table* symtab, Layout* layout,
749 Input_objects* input_objects, Mapfile* mapfile)
751 input_objects->archive_start(this);
753 if (this->members_.size() > 0)
755 std::map<off_t, Archive_member>::const_iterator p;
756 for (p = this->members_.begin();
757 p != this->members_.end();
760 if (!this->include_member(symtab, layout, input_objects, p->first,
761 mapfile, NULL, "--whole-archive"))
763 ++Archive::total_members;
768 for (Archive::const_iterator p = this->begin();
772 if (!this->include_member(symtab, layout, input_objects, p->off,
773 mapfile, NULL, "--whole-archive"))
775 ++Archive::total_members;
779 input_objects->archive_stop(this);
784 // Return the number of members in the archive. This is only used for
788 Archive::count_members()
791 for (Archive::const_iterator p = this->begin();
798 // Include an archive member in the link. OFF is the file offset of
799 // the member header. WHY is the reason we are including this member.
800 // Return true if we added the member or if we had an error, return
801 // false if this was the first member we tried to add from this
802 // archive and it had an incompatible format.
805 Archive::include_member(Symbol_table* symtab, Layout* layout,
806 Input_objects* input_objects, off_t off,
807 Mapfile* mapfile, Symbol* sym, const char* why)
809 ++Archive::total_members_loaded;
811 std::map<off_t, Archive_member>::const_iterator p = this->members_.find(off);
812 if (p != this->members_.end())
814 Object *obj = p->second.obj_;
816 Read_symbols_data *sd = p->second.sd_;
818 mapfile->report_include_archive_member(obj->name(), sym, why);
819 if (input_objects->add_object(obj))
821 obj->layout(symtab, layout, sd);
822 obj->add_symbols(symtab, sd, layout);
823 this->included_member_ = true;
830 Object* obj = this->get_elf_object_for_member(off, &unconfigured);
832 if (!this->included_member_
833 && this->searched_for()
846 mapfile->report_include_archive_member(obj->name(), sym, why);
848 Pluginobj* pluginobj = obj->pluginobj();
849 if (pluginobj != NULL)
851 pluginobj->add_symbols(symtab, NULL, layout);
852 this->included_member_ = true;
856 if (!input_objects->add_object(obj))
860 Read_symbols_data sd;
861 obj->read_symbols(&sd);
862 obj->layout(symtab, layout, &sd);
863 obj->add_symbols(symtab, &sd, layout);
865 // If this is an external member of a thin archive, unlock the file
866 // for the next task.
867 if (obj->offset() == 0)
868 obj->unlock(this->task_);
870 this->included_member_ = true;
876 // Print statistical information to stderr. This is used for --stats.
879 Archive::print_stats()
881 fprintf(stderr, _("%s: archive libraries: %u\n"),
882 program_name, Archive::total_archives);
883 fprintf(stderr, _("%s: total archive members: %u\n"),
884 program_name, Archive::total_members);
885 fprintf(stderr, _("%s: loaded archive members: %u\n"),
886 program_name, Archive::total_members_loaded);
889 // Add_archive_symbols methods.
891 Add_archive_symbols::~Add_archive_symbols()
893 if (this->this_blocker_ != NULL)
894 delete this->this_blocker_;
895 // next_blocker_ is deleted by the task associated with the next
899 // Return whether we can add the archive symbols. We are blocked by
900 // this_blocker_. We block next_blocker_. We also lock the file.
903 Add_archive_symbols::is_runnable()
905 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
906 return this->this_blocker_;
911 Add_archive_symbols::locks(Task_locker* tl)
913 tl->add(this, this->next_blocker_);
914 tl->add(this, this->archive_->token());
918 Add_archive_symbols::run(Workqueue* workqueue)
920 bool added = this->archive_->add_symbols(this->symtab_, this->layout_,
921 this->input_objects_,
923 this->archive_->unlock_nested_archives();
925 this->archive_->release();
926 this->archive_->clear_uncached_views();
930 // This archive holds object files which are incompatible with
932 Read_symbols::incompatible_warning(this->input_argument_,
933 this->archive_->input_file());
934 Read_symbols::requeue(workqueue, this->input_objects_, this->symtab_,
935 this->layout_, this->dirpath_, this->dirindex_,
936 this->mapfile_, this->input_argument_,
937 this->input_group_, this->next_blocker_);
938 delete this->archive_;
942 if (this->input_group_ != NULL)
943 this->input_group_->add_archive(this->archive_);
946 // We no longer need to know about this archive.
947 delete this->archive_;
948 this->archive_ = NULL;
952 } // End namespace gold.