+// An archive member iterator.
+
+class Archive::const_iterator
+{
+ public:
+ // The header of an archive member. This is what this iterator
+ // points to.
+ struct Header
+ {
+ // The name of the member.
+ std::string name;
+ // The file offset of the member.
+ off_t off;
+ // The file offset of a nested archive member.
+ off_t nested_off;
+ // The size of the member.
+ off_t size;
+ };
+
+ const_iterator(Archive* archive, off_t off)
+ : archive_(archive), off_(off)
+ { this->read_next_header(); }
+
+ const Header&
+ operator*() const
+ { return this->header_; }
+
+ const Header*
+ operator->() const
+ { return &this->header_; }
+
+ const_iterator&
+ operator++()
+ {
+ if (this->off_ == this->archive_->file().filesize())
+ return *this;
+ this->off_ += sizeof(Archive_header);
+ if (!this->archive_->is_thin_archive())
+ this->off_ += this->header_.size;
+ if ((this->off_ & 1) != 0)
+ ++this->off_;
+ this->read_next_header();
+ return *this;
+ }
+
+ const_iterator
+ operator++(int)
+ {
+ const_iterator ret = *this;
+ ++*this;
+ return ret;
+ }
+
+ bool
+ operator==(const const_iterator p) const
+ { return this->off_ == p->off; }
+
+ bool
+ operator!=(const const_iterator p) const
+ { return this->off_ != p->off; }
+
+ private:
+ void
+ read_next_header();
+
+ // The underlying archive.
+ Archive* archive_;
+ // The current offset in the file.
+ off_t off_;
+ // The current archive header.
+ Header header_;
+};
+
+// Read the next archive header.
+
+void
+Archive::const_iterator::read_next_header()
+{
+ off_t filesize = this->archive_->file().filesize();
+ while (true)
+ {
+ if (filesize - this->off_ < static_cast<off_t>(sizeof(Archive_header)))
+ {
+ if (filesize != this->off_)
+ {
+ gold_error(_("%s: short archive header at %zu"),
+ this->archive_->filename().c_str(),
+ static_cast<size_t>(this->off_));
+ this->off_ = filesize;
+ }
+ this->header_.off = filesize;
+ return;
+ }
+
+ unsigned char buf[sizeof(Archive_header)];
+ this->archive_->file().read(this->off_, sizeof(Archive_header), buf);
+
+ const Archive_header* hdr = reinterpret_cast<const Archive_header*>(buf);
+ this->header_.size =
+ this->archive_->interpret_header(hdr, this->off_, &this->header_.name,
+ &this->header_.nested_off);
+ this->header_.off = this->off_;
+
+ // Skip special members.
+ if (!this->header_.name.empty() && this->header_.name != "/")
+ return;
+
+ this->off_ += sizeof(Archive_header) + this->header_.size;
+ if ((this->off_ & 1) != 0)
+ ++this->off_;
+ }
+}
+
+// Initial iterator.
+
+Archive::const_iterator
+Archive::begin()
+{
+ return Archive::const_iterator(this, sarmag);
+}
+
+// Final iterator.
+
+Archive::const_iterator
+Archive::end()
+{
+ return Archive::const_iterator(this, this->input_file_->file().filesize());
+}
+
+// Get the file and offset for an archive member, which may be an
+// external member of a thin archive. Set *INPUT_FILE to the
+// file containing the actual member, *MEMOFF to the offset
+// within that file (0 if not a nested archive), and *MEMBER_NAME
+// to the name of the archive member. Return TRUE on success.
+
+bool
+Archive::get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
+ off_t* memsize, std::string* member_name)
+{
+ off_t nested_off;
+
+ *memsize = this->read_header(off, false, member_name, &nested_off);
+
+ *input_file = this->input_file_;
+ *memoff = off + static_cast<off_t>(sizeof(Archive_header));
+
+ if (!this->is_thin_archive_)
+ return true;
+
+ // Adjust a relative pathname so that it is relative
+ // to the directory containing the archive.
+ if (!IS_ABSOLUTE_PATH(member_name->c_str()))
+ {
+ const char* arch_path = this->filename().c_str();
+ const char* basename = lbasename(arch_path);
+ if (basename > arch_path)
+ member_name->replace(0, 0,
+ this->filename().substr(0, basename - arch_path));
+ }
+
+ if (nested_off > 0)
+ {
+ // This is a member of a nested archive. Open the containing
+ // archive if we don't already have it open, then do a recursive
+ // call to include the member from that archive.
+ Archive* arch;
+ Nested_archive_table::const_iterator p =
+ this->nested_archives_.find(*member_name);
+ if (p != this->nested_archives_.end())
+ arch = p->second;
+ else
+ {
+ Input_file_argument* input_file_arg =
+ new Input_file_argument(member_name->c_str(),
+ Input_file_argument::INPUT_FILE_TYPE_FILE,
+ "", false, parameters->options());
+ *input_file = new Input_file(input_file_arg);
+ int dummy = 0;
+ if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
+ return false;
+ arch = new Archive(*member_name, *input_file, false, this->dirpath_,
+ this->task_);
+ arch->setup();
+ std::pair<Nested_archive_table::iterator, bool> ins =
+ this->nested_archives_.insert(std::make_pair(*member_name, arch));
+ gold_assert(ins.second);
+ }
+ return arch->get_file_and_offset(nested_off, input_file, memoff,
+ memsize, member_name);
+ }
+
+ // This is an external member of a thin archive. Open the
+ // file as a regular relocatable object file.
+ Input_file_argument* input_file_arg =
+ new Input_file_argument(member_name->c_str(),
+ Input_file_argument::INPUT_FILE_TYPE_FILE,
+ "", false, this->input_file_->options());
+ *input_file = new Input_file(input_file_arg);
+ int dummy = 0;
+ if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
+ return false;
+
+ *memoff = 0;
+ *memsize = (*input_file)->file().filesize();
+ return true;
+}
+
+// Return an ELF object for the member at offset OFF. If
+// PUNCONFIGURED is not NULL, then if the ELF object has an
+// unsupported target type, set *PUNCONFIGURED to true and return
+// NULL.
+
+Object*
+Archive::get_elf_object_for_member(off_t off, bool* punconfigured)
+{
+ if (punconfigured != NULL)
+ *punconfigured = false;
+
+ Input_file* input_file;
+ off_t memoff;
+ off_t memsize;
+ std::string member_name;
+ if (!this->get_file_and_offset(off, &input_file, &memoff, &memsize,
+ &member_name))
+ return NULL;
+
+ if (parameters->options().has_plugins())
+ {
+ Object* obj = parameters->options().plugins()->claim_file(input_file,
+ memoff,
+ memsize);
+ if (obj != NULL)
+ {
+ // The input file was claimed by a plugin, and its symbols
+ // have been provided by the plugin.
+ return obj;
+ }
+ }
+
+ const unsigned char* ehdr;
+ int read_size;
+ if (!is_elf_object(input_file, memoff, &ehdr, &read_size))
+ {
+ gold_error(_("%s: member at %zu is not an ELF object"),
+ this->name().c_str(), static_cast<size_t>(off));
+ return NULL;
+ }
+
+ Object* obj = make_elf_object((std::string(this->input_file_->filename())
+ + "(" + member_name + ")"),
+ input_file, memoff, ehdr, read_size,
+ punconfigured);
+ if (obj == NULL)
+ return NULL;
+ obj->set_no_export(this->no_export());
+ return obj;
+}
+
+// Read the symbols from all the archive members in the link.
+
+void
+Archive::read_all_symbols()
+{
+ for (Archive::const_iterator p = this->begin();
+ p != this->end();
+ ++p)
+ this->read_symbols(p->off);
+}
+
+// Read the symbols from an archive member in the link. OFF is the file
+// offset of the member header.
+
+void
+Archive::read_symbols(off_t off)
+{
+ Object* obj = this->get_elf_object_for_member(off, NULL);
+ if (obj == NULL)
+ return;
+
+ Read_symbols_data* sd = new Read_symbols_data;
+ obj->read_symbols(sd);
+ Archive_member member(obj, sd);
+ this->members_[off] = member;
+}
+
+Archive::Should_include
+Archive::should_include_member(Symbol_table* symtab, Layout* layout,
+ const char* sym_name, Symbol** symp,
+ std::string* why, char** tmpbufp,
+ size_t* tmpbuflen)
+{
+ // In an object file, and therefore in an archive map, an
+ // '@' in the name separates the symbol name from the
+ // version name. If there are two '@' characters, this is
+ // the default version.
+ char* tmpbuf = *tmpbufp;
+ const char* ver = strchr(sym_name, '@');
+ bool def = false;
+ if (ver != NULL)
+ {
+ size_t symlen = ver - sym_name;
+ if (symlen + 1 > *tmpbuflen)
+ {
+ tmpbuf = static_cast<char*>(xrealloc(tmpbuf, symlen + 1));
+ *tmpbufp = tmpbuf;
+ *tmpbuflen = symlen + 1;
+ }
+ memcpy(tmpbuf, sym_name, symlen);
+ tmpbuf[symlen] = '\0';
+ sym_name = tmpbuf;
+
+ ++ver;
+ if (*ver == '@')
+ {
+ ++ver;
+ def = true;
+ }
+ }
+
+ Symbol* sym = symtab->lookup(sym_name, ver);
+ if (def
+ && ver != NULL
+ && (sym == NULL
+ || !sym->is_undefined()
+ || sym->binding() == elfcpp::STB_WEAK))
+ sym = symtab->lookup(sym_name, NULL);
+
+ *symp = sym;
+
+ if (sym == NULL)
+ {
+ // Check whether the symbol was named in a -u option.
+ if (parameters->options().is_undefined(sym_name))
+ {
+ *why = "-u ";
+ *why += sym_name;
+ }
+ else if (layout->script_options()->is_referenced(sym_name))
+ {
+ size_t alc = 100 + strlen(sym_name);
+ char* buf = new char[alc];
+ snprintf(buf, alc, _("script or expression reference to %s"),
+ sym_name);
+ *why = buf;
+ delete[] buf;
+ }
+ else
+ return Archive::SHOULD_INCLUDE_UNKNOWN;
+ }
+ else if (!sym->is_undefined())
+ return Archive::SHOULD_INCLUDE_NO;
+ // PR 12001: Do not include an archive when the undefined
+ // symbol has actually been defined on the command line.
+ else if (layout->script_options()->is_pending_assignment(sym_name))
+ return Archive::SHOULD_INCLUDE_NO;
+ else if (sym->binding() == elfcpp::STB_WEAK)
+ return Archive::SHOULD_INCLUDE_UNKNOWN;
+
+ return Archive::SHOULD_INCLUDE_YES;
+}
+