#include <cstring>
#include <climits>
#include <vector>
+#include "libiberty.h"
+#include "filenames.h"
#include "elfcpp.h"
#include "options.h"
'!', '<', 'a', 'r', 'c', 'h', '>', '\n'
};
+const char Archive::armagt[sarmag] =
+{
+ '!', '<', 't', 'h', 'i', 'n', '>', '\n'
+};
+
const char Archive::arfmag[2] = { '`', '\n' };
// Set up the archive: read the symbol map and the extended name
// table.
void
-Archive::setup(Task* task)
+Archive::setup()
{
// We need to ignore empty archives.
if (this->input_file_->file().filesize() == sarmag)
- {
- this->input_file_->file().unlock(task);
- return;
- }
+ return;
// The first member of the archive should be the symbol table.
std::string armap_name;
section_size_type armap_size =
convert_to_section_size_type(this->read_header(sarmag, false,
- &armap_name));
+ &armap_name, NULL));
off_t off = sarmag;
if (armap_name.empty())
{
++off;
std::string xname;
section_size_type extended_size =
- convert_to_section_size_type(this->read_header(off, true, &xname));
+ convert_to_section_size_type(this->read_header(off, true, &xname, NULL));
if (xname == "/")
{
const unsigned char* p = this->get_view(off + sizeof(Archive_header),
const char* px = reinterpret_cast<const char*>(p);
this->extended_names_.assign(px, extended_size);
}
+}
+
+// Unlock any nested archives.
- // Opening the file locked it. Unlock it now.
- this->input_file_->file().unlock(task);
+void
+Archive::unlock_nested_archives()
+{
+ for (Nested_archive_table::iterator p = this->nested_archives_.begin();
+ p != this->nested_archives_.end();
+ ++p)
+ {
+ p->second->unlock(this->task_);
+ }
}
// Read the archive symbol map.
// of the member.
off_t
-Archive::read_header(off_t off, bool cache, std::string* pname)
+Archive::read_header(off_t off, bool cache, std::string* pname,
+ off_t* nested_off)
{
const unsigned char* p = this->get_view(off, sizeof(Archive_header), cache);
const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
- return this->interpret_header(hdr, off, pname);
+ return this->interpret_header(hdr, off, pname, nested_off);
}
// Interpret the header of HDR, the header of the archive member at
off_t
Archive::interpret_header(const Archive_header* hdr, off_t off,
- std::string* pname)
+ std::string* pname, off_t* nested_off)
{
if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
{
return this->input_file_->file().filesize() - off;
}
pname->assign(hdr->ar_name, name_end - hdr->ar_name);
+ if (nested_off != NULL)
+ *nested_off = 0;
}
else if (hdr->ar_name[1] == ' ')
{
{
errno = 0;
long x = strtol(hdr->ar_name + 1, &end, 10);
+ long y = 0;
+ if (*end == ':')
+ y = strtol(end + 1, &end, 10);
if (*end != ' '
|| x < 0
|| (x == LONG_MAX && errno == ERANGE)
}
const char* name = this->extended_names_.data() + x;
- const char* name_end = strchr(name, '/');
+ const char* name_end = strchr(name, '\n');
if (static_cast<size_t>(name_end - name) > this->extended_names_.size()
- || name_end[1] != '\n')
+ || name_end[-1] != '/')
{
gold_error(_("%s: bad extended name entry at header %zu"),
this->name().c_str(), static_cast<size_t>(off));
return this->input_file_->file().filesize() - off;
}
- pname->assign(name, name_end - name);
+ pname->assign(name, name_end - 1 - name);
+ if (nested_off != NULL)
+ *nested_off = y;
}
return member_size;
const Archive_header* hdr =
reinterpret_cast<const Archive_header*>(hdr_buf);
std::string name;
- off_t size = this->interpret_header(hdr, off, &name);
+ off_t size = this->interpret_header(hdr, off, &name, NULL);
if (name.empty())
{
// Symbol table.
else
this->include_member(symtab, layout, input_objects, off);
- off += sizeof(Archive_header) + size;
+ off += sizeof(Archive_header);
+ if (!this->is_thin_archive_)
+ off += size;
if ((off & 1) != 0)
++off;
}
Input_objects* input_objects, off_t off)
{
std::string n;
- this->read_header(off, false, &n);
+ off_t nested_off;
+ this->read_header(off, false, &n, &nested_off);
- const off_t memoff = off + static_cast<off_t>(sizeof(Archive_header));
+ Input_file* input_file;
+ off_t memoff;
+
+ if (!this->is_thin_archive_)
+ {
+ input_file = this->input_file_;
+ memoff = off + static_cast<off_t>(sizeof(Archive_header));
+ }
+ else
+ {
+ // Adjust a relative pathname so that it is relative
+ // to the directory containing the archive.
+ if (!IS_ABSOLUTE_PATH(n.c_str()))
+ {
+ const char *arch_path = this->name().c_str();
+ const char *basename = lbasename(arch_path);
+ if (basename > arch_path)
+ n.replace(0, 0, this->name().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(n);
+ if (p != this->nested_archives_.end())
+ arch = p->second;
+ else
+ {
+ Input_file_argument* input_file_arg =
+ new Input_file_argument(n.c_str(), false, "", false,
+ parameters->options());
+ input_file = new Input_file(input_file_arg);
+ if (!input_file->open(parameters->options(), *this->dirpath_,
+ this->task_))
+ return;
+ arch = new Archive(n, input_file, false, this->dirpath_,
+ this->task_);
+ arch->setup();
+ std::pair<Nested_archive_table::iterator, bool> ins =
+ this->nested_archives_.insert(std::make_pair(n, arch));
+ gold_assert(ins.second);
+ }
+ arch->include_member(symtab, layout, input_objects, nested_off);
+ return;
+ }
+ // 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(n.c_str(), false, "", false,
+ this->input_file_->options());
+ input_file = new Input_file(input_file_arg);
+ if (!input_file->open(parameters->options(), *this->dirpath_,
+ this->task_))
+ {
+ return;
+ }
+ memoff = 0;
+ }
// Read enough of the file to pick up the entire ELF header.
unsigned char ehdr_buf[elfcpp::Elf_sizes<64>::ehdr_size];
- off_t filesize = this->input_file_->file().filesize();
+ off_t filesize = input_file->file().filesize();
int read_size = elfcpp::Elf_sizes<64>::ehdr_size;
if (filesize - memoff < read_size)
read_size = filesize - memoff;
return;
}
- this->input_file_->file().read(memoff, read_size, ehdr_buf);
+ input_file->file().read(memoff, read_size, ehdr_buf);
static unsigned char elfmagic[4] =
{
Object* obj = make_elf_object((std::string(this->input_file_->filename())
+ "(" + n + ")"),
- this->input_file_, memoff, ehdr_buf,
+ input_file, memoff, ehdr_buf,
read_size);
if (input_objects->add_object(obj))
delete obj;
}
+ if (this->is_thin_archive_)
+ {
+ // Opening the file locked it. Unlock it now.
+ input_file->file().unlock(this->task_);
+ }
}
// Add_archive_symbols methods.
this->archive_->add_symbols(this->symtab_, this->layout_,
this->input_objects_);
+ this->archive_->unlock_nested_archives();
+
this->archive_->release();
if (this->input_group_ != NULL)
class Archive
{
public:
- Archive(const std::string& name, Input_file* input_file)
+ Archive(const std::string& name, Input_file* input_file,
+ bool is_thin_archive, Dirsearch* dirpath, Task* task)
: name_(name), input_file_(input_file), armap_(), armap_names_(),
- extended_names_(), armap_checked_(), seen_offsets_()
+ extended_names_(), armap_checked_(), seen_offsets_(),
+ is_thin_archive_(is_thin_archive), dirpath_(dirpath), task_(task)
{ }
// The length of the magic string at the start of an archive.
// The magic string at the start of an archive.
static const char armag[sarmag];
+ static const char armagt[sarmag];
// The string expected at the end of an archive member header.
static const char arfmag[2];
// Set up the archive: read the symbol map.
void
- setup(Task*);
+ setup();
// Get a reference to the underlying file.
File_read&
release()
{ this->input_file_->file().release(); }
+ // Unlock any nested archives.
+ void
+ unlock_nested_archives();
+
// Select members from the archive as needed and add them to the
// link.
void
// the file view. Return the size of the member, and set *PNAME to
// the name.
off_t
- read_header(off_t off, bool cache, std::string* pname);
+ read_header(off_t off, bool cache, std::string* pname, off_t* nested_off);
// Interpret an archive header HDR at OFF. Return the size of the
// member, and set *PNAME to the name.
off_t
- interpret_header(const Archive_header* hdr, off_t off, std::string* pname);
+ interpret_header(const Archive_header* hdr, off_t off, std::string* pname,
+ off_t* nested_off);
// Include all the archive members in the link.
void
{ return static_cast<size_t>(val); }
};
+ // For keeping track of open nested archives in a thin archive file.
+ typedef Unordered_map<std::string, Archive*> Nested_archive_table;
+
// Name of object as printed to user.
std::string name_;
// For reading the file.
std::vector<bool> armap_checked_;
// Track which elements have been included by offset.
Unordered_set<off_t, Seen_hash> seen_offsets_;
+ // True if this is a thin archive.
+ const bool is_thin_archive_;
+ // Table of nested archives, indexed by filename.
+ Nested_archive_table nested_archives_;
+ // The directory search path.
+ Dirsearch* dirpath_;
+ // The task reading this archive.
+ Task *task_;
};
// This class is used to read an archive and pick out the desired