+// 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);
+ off_t size = this->archive_->interpret_header(hdr, this->off_,
+ &this->header_.name,
+ &this->header_.nested_off);
+ if (size == -1)
+ {
+ this->header_.off = filesize;
+ return;
+ }
+
+ this->header_.size = size;
+ this->header_.off = this->off_;
+
+ // Skip special members.
+ if (!this->header_.name.empty()
+ && this->header_.name != "/"
+ && this->header_.name != "/SYM64/")
+ 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);
+ if (*memsize == -1)
+ return false;
+
+ *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;
+
+ const unsigned char* ehdr;
+ int read_size;
+ Object *obj = NULL;
+ bool is_elf_obj = false;
+ bool unclaimed = false;
+
+ if (is_elf_object(input_file, memoff, &ehdr, &read_size))
+ {
+ obj = make_elf_object((std::string(this->input_file_->filename())
+ + "(" + member_name + ")"),
+ input_file, memoff, ehdr, read_size,
+ punconfigured);
+ is_elf_obj = true;
+ }
+
+ if (parameters->options().has_plugins())
+ {
+ Object* plugin_obj
+ = parameters->options().plugins()->claim_file(input_file,
+ memoff,
+ memsize,
+ obj);
+ if (plugin_obj != NULL)
+ {
+ // The input file was claimed by a plugin, and its symbols
+ // have been provided by the plugin.
+ // Delete its elf object.
+ if (obj != NULL)
+ delete obj;
+ return plugin_obj;
+ }
+
+ unclaimed = true;
+ }
+
+ if (!is_elf_obj)
+ {
+ if (unclaimed)
+ gold_error(_("%s: plugin failed to claim member %s at %zu"),
+ this->name().c_str(), member_name.c_str(),
+ static_cast<size_t>(off));
+ else
+ gold_error(_("%s: member %s at %zu is not an ELF object"),
+ this->name().c_str(), member_name.c_str(),
+ static_cast<size_t>(off));
+ return NULL;
+ }
+
+ 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;
+}
+