2004-11-14 21:58:33 +00:00
|
|
|
//===-- ArchiveReader.cpp - Read LLVM archive files -------------*- C++ -*-===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2005-04-21 21:13:18 +00:00
|
|
|
// This file was developed by Reid Spencer and is distributed under the
|
2004-11-14 21:58:33 +00:00
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-04-19 21:45:34 +00:00
|
|
|
//
|
2004-11-14 21:58:33 +00:00
|
|
|
// Builds up standard unix archive files (.a) containing LLVM bytecode.
|
2003-04-19 21:45:34 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-11-14 21:58:33 +00:00
|
|
|
#include "ArchiveInternals.h"
|
2003-04-19 21:45:34 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2004-11-14 21:58:33 +00:00
|
|
|
|
2004-01-10 19:00:15 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-11-14 21:58:33 +00:00
|
|
|
/// Read a variable-bit-rate encoded unsigned integer
|
|
|
|
inline unsigned readInteger(const char*&At, const char*End) {
|
|
|
|
unsigned Shift = 0;
|
|
|
|
unsigned Result = 0;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-11-14 21:58:33 +00:00
|
|
|
do {
|
2005-04-21 21:13:18 +00:00
|
|
|
if (At == End)
|
2004-11-14 21:58:33 +00:00
|
|
|
throw std::string("Ran out of data reading vbr_uint!");
|
|
|
|
Result |= (unsigned)((*At++) & 0x7F) << Shift;
|
|
|
|
Shift += 7;
|
|
|
|
} while (At[-1] & 0x80);
|
|
|
|
return Result;
|
2003-04-19 21:45:34 +00:00
|
|
|
}
|
|
|
|
|
2004-11-14 21:58:33 +00:00
|
|
|
// Completely parse the Archive's symbol table and populate symTab member var.
|
|
|
|
void
|
|
|
|
Archive::parseSymbolTable(const void* data, unsigned size) {
|
|
|
|
const char* At = (const char*) data;
|
|
|
|
const char* End = At + size;
|
|
|
|
while (At < End) {
|
|
|
|
unsigned offset = readInteger(At, End);
|
|
|
|
unsigned length = readInteger(At, End);
|
|
|
|
if (At + length > End)
|
|
|
|
throw std::string("malformed symbol table");
|
|
|
|
// we don't care if it can't be inserted (duplicate entry)
|
2004-11-20 07:29:40 +00:00
|
|
|
symTab.insert(std::make_pair(std::string(At, length), offset));
|
2004-11-14 21:58:33 +00:00
|
|
|
At += length;
|
|
|
|
}
|
|
|
|
symTabSize = size;
|
2003-11-16 23:08:48 +00:00
|
|
|
}
|
2003-04-19 21:45:34 +00:00
|
|
|
|
2004-11-14 21:58:33 +00:00
|
|
|
// This member parses an ArchiveMemberHeader that is presumed to be pointed to
|
|
|
|
// by At. The At pointer is updated to the byte just after the header, which
|
2005-04-21 21:13:18 +00:00
|
|
|
// can be variable in size.
|
2004-11-14 21:58:33 +00:00
|
|
|
ArchiveMember*
|
|
|
|
Archive::parseMemberHeader(const char*& At, const char* End) {
|
|
|
|
assert(At + sizeof(ArchiveMemberHeader) < End && "Not enough data");
|
|
|
|
|
|
|
|
// Cast archive member header
|
|
|
|
ArchiveMemberHeader* Hdr = (ArchiveMemberHeader*)At;
|
|
|
|
At += sizeof(ArchiveMemberHeader);
|
|
|
|
|
|
|
|
// Instantiate the ArchiveMember to be filled
|
|
|
|
ArchiveMember* member = new ArchiveMember(this);
|
|
|
|
|
2005-04-21 21:13:18 +00:00
|
|
|
// Extract the size and determine if the file is
|
2004-11-14 21:58:33 +00:00
|
|
|
// compressed or not (negative length).
|
|
|
|
int flags = 0;
|
|
|
|
int MemberSize = atoi(Hdr->size);
|
|
|
|
if (MemberSize < 0) {
|
|
|
|
flags |= ArchiveMember::CompressedFlag;
|
|
|
|
MemberSize = -MemberSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the size of the member for sanity
|
|
|
|
if (At + MemberSize > End)
|
|
|
|
throw std::string("invalid member length in archive file");
|
|
|
|
|
|
|
|
// Check the member signature
|
|
|
|
if (!Hdr->checkSignature())
|
|
|
|
throw std::string("invalid file member signature");
|
|
|
|
|
|
|
|
// Convert and check the member name
|
2005-04-21 21:13:18 +00:00
|
|
|
// The empty name ( '/' and 15 blanks) is for a foreign (non-LLVM) symbol
|
|
|
|
// table. The special name "//" and 14 blanks is for a string table, used
|
2004-11-14 21:58:33 +00:00
|
|
|
// for long file names. This library doesn't generate either of those but
|
2005-04-21 21:13:18 +00:00
|
|
|
// it will accept them. If the name starts with #1/ and the remainder is
|
|
|
|
// digits, then those digits specify the length of the name that is
|
|
|
|
// stored immediately following the header. The special name
|
|
|
|
// __LLVM_SYM_TAB__ identifies the symbol table for LLVM bytecode.
|
|
|
|
// Anything else is a regular, short filename that is terminated with
|
2004-11-14 21:58:33 +00:00
|
|
|
// a '/' and blanks.
|
|
|
|
|
|
|
|
std::string pathname;
|
|
|
|
switch (Hdr->name[0]) {
|
|
|
|
case '#':
|
|
|
|
if (Hdr->name[1] == '1' && Hdr->name[2] == '/') {
|
|
|
|
if (isdigit(Hdr->name[3])) {
|
|
|
|
unsigned len = atoi(&Hdr->name[3]);
|
2004-11-20 07:29:40 +00:00
|
|
|
pathname.assign(At, len);
|
2004-11-17 16:13:11 +00:00
|
|
|
At += len;
|
|
|
|
MemberSize -= len;
|
2004-11-14 21:58:33 +00:00
|
|
|
flags |= ArchiveMember::HasLongFilenameFlag;
|
|
|
|
} else
|
|
|
|
throw std::string("invalid long filename");
|
2005-04-21 21:13:18 +00:00
|
|
|
} else if (Hdr->name[1] == '_' &&
|
2004-11-20 07:29:40 +00:00
|
|
|
(0 == memcmp(Hdr->name, ARFILE_LLVM_SYMTAB_NAME, 16))) {
|
2004-11-14 21:58:33 +00:00
|
|
|
// The member is using a long file name (>15 chars) format.
|
|
|
|
// This format is standard for 4.4BSD and Mac OSX operating
|
|
|
|
// systems. LLVM uses it similarly. In this format, the
|
|
|
|
// remainder of the name field (after #1/) specifies the
|
|
|
|
// length of the file name which occupy the first bytes of
|
|
|
|
// the member's data. The pathname already has the #1/ stripped.
|
|
|
|
pathname.assign(ARFILE_LLVM_SYMTAB_NAME);
|
|
|
|
flags |= ArchiveMember::LLVMSymbolTableFlag;
|
|
|
|
}
|
2003-04-19 21:45:34 +00:00
|
|
|
break;
|
2004-11-14 21:58:33 +00:00
|
|
|
case '/':
|
|
|
|
if (Hdr->name[1]== '/') {
|
2004-11-20 07:29:40 +00:00
|
|
|
if (0 == memcmp(Hdr->name, ARFILE_STRTAB_NAME, 16)) {
|
2004-11-14 21:58:33 +00:00
|
|
|
pathname.assign(ARFILE_STRTAB_NAME);
|
|
|
|
flags |= ArchiveMember::StringTableFlag;
|
|
|
|
} else {
|
|
|
|
throw std::string("invalid string table name");
|
|
|
|
}
|
|
|
|
} else if (Hdr->name[1] == ' ') {
|
2004-11-20 07:29:40 +00:00
|
|
|
if (0 == memcmp(Hdr->name, ARFILE_SVR4_SYMTAB_NAME, 16)) {
|
|
|
|
pathname.assign(ARFILE_SVR4_SYMTAB_NAME);
|
|
|
|
flags |= ArchiveMember::SVR4SymbolTableFlag;
|
2004-11-14 21:58:33 +00:00
|
|
|
} else {
|
2004-11-20 07:29:40 +00:00
|
|
|
throw std::string("invalid SVR4 symbol table name");
|
2004-11-14 21:58:33 +00:00
|
|
|
}
|
|
|
|
} else if (isdigit(Hdr->name[1])) {
|
|
|
|
unsigned index = atoi(&Hdr->name[1]);
|
|
|
|
if (index < strtab.length()) {
|
|
|
|
const char* namep = strtab.c_str() + index;
|
|
|
|
const char* endp = strtab.c_str() + strtab.length();
|
|
|
|
const char* p = namep;
|
|
|
|
const char* last_p = p;
|
|
|
|
while (p < endp) {
|
|
|
|
if (*p == '\n' && *last_p == '/') {
|
2004-11-20 07:29:40 +00:00
|
|
|
pathname.assign(namep, last_p - namep);
|
2004-11-14 21:58:33 +00:00
|
|
|
flags |= ArchiveMember::HasLongFilenameFlag;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
last_p = p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (p >= endp)
|
|
|
|
throw std::string("missing name termiantor in string table");
|
|
|
|
} else {
|
|
|
|
throw std::string("name index beyond string table");
|
|
|
|
}
|
|
|
|
}
|
2003-11-16 23:08:48 +00:00
|
|
|
break;
|
2004-11-20 07:29:40 +00:00
|
|
|
case '_':
|
2005-04-21 21:13:18 +00:00
|
|
|
if (Hdr->name[1] == '_' &&
|
2004-11-20 07:29:40 +00:00
|
|
|
(0 == memcmp(Hdr->name, ARFILE_BSD4_SYMTAB_NAME, 16))) {
|
|
|
|
pathname.assign(ARFILE_BSD4_SYMTAB_NAME);
|
|
|
|
flags |= ArchiveMember::BSD4SymbolTableFlag;
|
2004-11-23 22:35:39 +00:00
|
|
|
break;
|
2004-11-20 07:29:40 +00:00
|
|
|
}
|
2004-11-23 22:35:39 +00:00
|
|
|
/* FALL THROUGH */
|
2004-11-14 21:58:33 +00:00
|
|
|
|
2003-11-16 23:08:48 +00:00
|
|
|
default:
|
2004-11-20 07:29:40 +00:00
|
|
|
char* slash = (char*) memchr(Hdr->name, '/', 16);
|
2004-11-14 21:58:33 +00:00
|
|
|
if (slash == 0)
|
2004-11-17 16:13:11 +00:00
|
|
|
slash = Hdr->name + 16;
|
2004-11-20 07:29:40 +00:00
|
|
|
pathname.assign(Hdr->name, slash - Hdr->name);
|
2004-11-14 21:58:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-04-19 21:45:34 +00:00
|
|
|
|
2004-11-14 21:58:33 +00:00
|
|
|
// Determine if this is a bytecode file
|
2004-11-20 07:29:40 +00:00
|
|
|
switch (sys::IdentifyFileType(At, 4)) {
|
2004-11-14 21:58:33 +00:00
|
|
|
case sys::BytecodeFileType:
|
|
|
|
flags |= ArchiveMember::BytecodeFlag;
|
|
|
|
break;
|
|
|
|
case sys::CompressedBytecodeFileType:
|
|
|
|
flags |= ArchiveMember::CompressedBytecodeFlag;
|
|
|
|
flags &= ~ArchiveMember::CompressedFlag;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
flags &= ~(ArchiveMember::BytecodeFlag|
|
|
|
|
ArchiveMember::CompressedBytecodeFlag);
|
|
|
|
break;
|
2003-04-19 21:45:34 +00:00
|
|
|
}
|
|
|
|
|
2004-11-14 21:58:33 +00:00
|
|
|
// Fill in fields of the ArchiveMember
|
|
|
|
member->next = 0;
|
|
|
|
member->prev = 0;
|
|
|
|
member->parent = this;
|
2005-07-07 23:21:43 +00:00
|
|
|
member->path.set(pathname);
|
2004-11-14 21:58:33 +00:00
|
|
|
member->info.fileSize = MemberSize;
|
|
|
|
member->info.modTime.fromEpochTime(atoi(Hdr->date));
|
2004-12-29 01:20:24 +00:00
|
|
|
unsigned int mode;
|
|
|
|
sscanf(Hdr->mode, "%o", &mode);
|
|
|
|
member->info.mode = mode;
|
2004-11-14 21:58:33 +00:00
|
|
|
member->info.user = atoi(Hdr->uid);
|
|
|
|
member->info.group = atoi(Hdr->gid);
|
|
|
|
member->flags = flags;
|
|
|
|
member->data = At;
|
|
|
|
|
|
|
|
return member;
|
2003-04-19 21:45:34 +00:00
|
|
|
}
|
|
|
|
|
2004-11-14 21:58:33 +00:00
|
|
|
void
|
|
|
|
Archive::checkSignature() {
|
|
|
|
// Check the magic string at file's header
|
2004-11-20 07:29:40 +00:00
|
|
|
if (mapfile->size() < 8 || memcmp(base, ARFILE_MAGIC, 8))
|
2004-11-14 21:58:33 +00:00
|
|
|
throw std::string("invalid signature for an archive file");
|
|
|
|
}
|
2003-04-19 21:45:34 +00:00
|
|
|
|
2005-04-21 21:13:18 +00:00
|
|
|
// This function loads the entire archive and fully populates its ilist with
|
2004-11-14 21:58:33 +00:00
|
|
|
// the members of the archive file. This is typically used in preparation for
|
|
|
|
// editing the contents of the archive.
|
|
|
|
void
|
|
|
|
Archive::loadArchive() {
|
|
|
|
|
|
|
|
// Set up parsing
|
|
|
|
members.clear();
|
|
|
|
symTab.clear();
|
|
|
|
const char *At = base;
|
|
|
|
const char *End = base + mapfile->size();
|
|
|
|
|
|
|
|
checkSignature();
|
|
|
|
At += 8; // Skip the magic string.
|
|
|
|
|
|
|
|
bool seenSymbolTable = false;
|
|
|
|
bool foundFirstFile = false;
|
|
|
|
while (At < End) {
|
2005-04-21 21:13:18 +00:00
|
|
|
// parse the member header
|
2004-11-14 21:58:33 +00:00
|
|
|
const char* Save = At;
|
|
|
|
ArchiveMember* mbr = parseMemberHeader(At, End);
|
|
|
|
|
|
|
|
// check if this is the foreign symbol table
|
2004-11-20 07:29:40 +00:00
|
|
|
if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) {
|
2004-11-16 06:47:19 +00:00
|
|
|
// We just save this but don't do anything special
|
|
|
|
// with it. It doesn't count as the "first file".
|
2004-11-20 07:29:40 +00:00
|
|
|
if (foreignST) {
|
|
|
|
// What? Multiple foreign symbol tables? Just chuck it
|
|
|
|
// and retain the last one found.
|
|
|
|
delete foreignST;
|
|
|
|
}
|
2004-11-16 06:47:19 +00:00
|
|
|
foreignST = mbr;
|
2004-11-14 21:58:33 +00:00
|
|
|
At += mbr->getSize();
|
2004-11-19 17:08:00 +00:00
|
|
|
if ((intptr_t(At) & 1) == 1)
|
2004-11-14 21:58:33 +00:00
|
|
|
At++;
|
|
|
|
} else if (mbr->isStringTable()) {
|
2004-11-16 06:47:19 +00:00
|
|
|
// Simply suck the entire string table into a string
|
|
|
|
// variable. This will be used to get the names of the
|
|
|
|
// members that use the "/ddd" format for their names
|
|
|
|
// (SVR4 style long names).
|
2004-11-20 07:29:40 +00:00
|
|
|
strtab.assign(At, mbr->getSize());
|
2004-11-14 21:58:33 +00:00
|
|
|
At += mbr->getSize();
|
2004-11-19 17:08:00 +00:00
|
|
|
if ((intptr_t(At) & 1) == 1)
|
2004-11-14 21:58:33 +00:00
|
|
|
At++;
|
|
|
|
delete mbr;
|
2005-04-21 21:13:18 +00:00
|
|
|
} else if (mbr->isLLVMSymbolTable()) {
|
2004-11-16 06:47:19 +00:00
|
|
|
// This is the LLVM symbol table for the archive. If we've seen it
|
|
|
|
// already, its an error. Otherwise, parse the symbol table and move on.
|
2004-11-14 21:58:33 +00:00
|
|
|
if (seenSymbolTable)
|
|
|
|
throw std::string("invalid archive: multiple symbol tables");
|
2004-11-20 07:29:40 +00:00
|
|
|
parseSymbolTable(mbr->getData(), mbr->getSize());
|
2004-11-14 21:58:33 +00:00
|
|
|
seenSymbolTable = true;
|
|
|
|
At += mbr->getSize();
|
2004-11-19 17:08:00 +00:00
|
|
|
if ((intptr_t(At) & 1) == 1)
|
2004-11-14 21:58:33 +00:00
|
|
|
At++;
|
2004-11-16 06:47:19 +00:00
|
|
|
delete mbr; // We don't need this member in the list of members.
|
2004-11-14 21:58:33 +00:00
|
|
|
} else {
|
2004-11-16 06:47:19 +00:00
|
|
|
// This is just a regular file. If its the first one, save its offset.
|
|
|
|
// Otherwise just push it on the list and move on to the next file.
|
2004-11-14 21:58:33 +00:00
|
|
|
if (!foundFirstFile) {
|
|
|
|
firstFileOffset = Save - base;
|
|
|
|
foundFirstFile = true;
|
|
|
|
}
|
|
|
|
members.push_back(mbr);
|
|
|
|
At += mbr->getSize();
|
2004-11-19 17:08:00 +00:00
|
|
|
if ((intptr_t(At) & 1) == 1)
|
2004-11-14 21:58:33 +00:00
|
|
|
At++;
|
|
|
|
}
|
2004-05-28 00:24:41 +00:00
|
|
|
}
|
2004-11-14 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open and completely load the archive file.
|
|
|
|
Archive*
|
2004-12-13 02:59:03 +00:00
|
|
|
Archive::OpenAndLoad(const sys::Path& file, std::string* ErrorMessage) {
|
|
|
|
try {
|
2004-12-13 03:22:31 +00:00
|
|
|
std::auto_ptr<Archive> result ( new Archive(file, true));
|
2004-12-13 02:59:03 +00:00
|
|
|
result->loadArchive();
|
2004-12-13 03:22:31 +00:00
|
|
|
return result.release();
|
2004-12-13 02:59:03 +00:00
|
|
|
} catch (const std::string& msg) {
|
|
|
|
if (ErrorMessage) {
|
|
|
|
*ErrorMessage = msg;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-14 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get all the bytecode modules from the archive
|
|
|
|
bool
|
|
|
|
Archive::getAllModules(std::vector<Module*>& Modules, std::string* ErrMessage) {
|
|
|
|
|
|
|
|
for (iterator I=begin(), E=end(); I != E; ++I) {
|
|
|
|
if (I->isBytecode() || I->isCompressedBytecode()) {
|
2005-04-21 21:13:18 +00:00
|
|
|
std::string FullMemberName = archPath.toString() +
|
2004-12-11 00:14:15 +00:00
|
|
|
"(" + I->getPath().toString() + ")";
|
2005-04-21 21:13:18 +00:00
|
|
|
Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
|
2004-11-17 18:25:21 +00:00
|
|
|
I->getSize(), FullMemberName, ErrMessage);
|
2004-11-14 21:58:33 +00:00
|
|
|
if (!M)
|
|
|
|
return true;
|
2003-04-19 21:45:34 +00:00
|
|
|
|
2004-11-14 21:58:33 +00:00
|
|
|
Modules.push_back(M);
|
2003-04-19 21:45:34 +00:00
|
|
|
}
|
2004-11-14 21:58:33 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load just the symbol table from the archive file
|
|
|
|
void
|
|
|
|
Archive::loadSymbolTable() {
|
|
|
|
|
|
|
|
// Set up parsing
|
|
|
|
members.clear();
|
|
|
|
symTab.clear();
|
|
|
|
const char *At = base;
|
|
|
|
const char *End = base + mapfile->size();
|
|
|
|
|
|
|
|
// Make sure we're dealing with an archive
|
|
|
|
checkSignature();
|
|
|
|
|
|
|
|
At += 8; // Skip signature
|
|
|
|
|
|
|
|
// Parse the first file member header
|
|
|
|
const char* FirstFile = At;
|
|
|
|
ArchiveMember* mbr = parseMemberHeader(At, End);
|
|
|
|
|
2004-11-20 07:29:40 +00:00
|
|
|
if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) {
|
2004-11-14 21:58:33 +00:00
|
|
|
// Skip the foreign symbol table, we don't do anything with it
|
|
|
|
At += mbr->getSize();
|
2004-11-19 17:08:00 +00:00
|
|
|
if ((intptr_t(At) & 1) == 1)
|
2004-11-15 01:40:20 +00:00
|
|
|
At++;
|
2004-11-14 21:58:33 +00:00
|
|
|
delete mbr;
|
|
|
|
|
2004-11-15 01:40:20 +00:00
|
|
|
// Read the next one
|
|
|
|
FirstFile = At;
|
2004-11-20 07:29:40 +00:00
|
|
|
mbr = parseMemberHeader(At, End);
|
2004-11-15 01:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mbr->isStringTable()) {
|
|
|
|
// Process the string table entry
|
2004-11-20 07:29:40 +00:00
|
|
|
strtab.assign((const char*)mbr->getData(), mbr->getSize());
|
2004-11-15 01:40:20 +00:00
|
|
|
At += mbr->getSize();
|
2004-11-19 17:08:00 +00:00
|
|
|
if ((intptr_t(At) & 1) == 1)
|
2004-11-15 01:40:20 +00:00
|
|
|
At++;
|
|
|
|
delete mbr;
|
|
|
|
// Get the next one
|
2004-11-14 21:58:33 +00:00
|
|
|
FirstFile = At;
|
2004-11-20 07:29:40 +00:00
|
|
|
mbr = parseMemberHeader(At, End);
|
2004-11-14 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// See if its the symbol table
|
|
|
|
if (mbr->isLLVMSymbolTable()) {
|
2004-11-20 07:29:40 +00:00
|
|
|
parseSymbolTable(mbr->getData(), mbr->getSize());
|
2004-11-28 03:13:02 +00:00
|
|
|
At += mbr->getSize();
|
2004-11-19 17:08:00 +00:00
|
|
|
if ((intptr_t(At) & 1) == 1)
|
2004-11-28 03:13:02 +00:00
|
|
|
At++;
|
|
|
|
FirstFile = At;
|
2004-11-14 21:58:33 +00:00
|
|
|
} else {
|
|
|
|
// There's no symbol table in the file. We have to rebuild it from scratch
|
2005-04-21 21:13:18 +00:00
|
|
|
// because the intent of this method is to get the symbol table loaded so
|
|
|
|
// it can be searched efficiently.
|
2004-11-14 21:58:33 +00:00
|
|
|
// Add the member to the members list
|
|
|
|
members.push_back(mbr);
|
|
|
|
}
|
|
|
|
|
|
|
|
firstFileOffset = FirstFile - base;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the archive and load just the symbol tables
|
|
|
|
Archive*
|
2004-12-13 02:59:03 +00:00
|
|
|
Archive::OpenAndLoadSymbols(const sys::Path& file, std::string* ErrorMessage) {
|
|
|
|
try {
|
2004-12-13 03:22:31 +00:00
|
|
|
std::auto_ptr<Archive> result ( new Archive(file, true) );
|
2004-12-13 02:59:03 +00:00
|
|
|
result->loadSymbolTable();
|
2004-12-13 03:22:31 +00:00
|
|
|
return result.release();
|
2004-12-13 02:59:03 +00:00
|
|
|
} catch (const std::string& msg) {
|
|
|
|
if (ErrorMessage) {
|
|
|
|
*ErrorMessage = msg;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-14 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look up one symbol in the symbol table and return a ModuleProvider for the
|
|
|
|
// module that defines that symbol.
|
2005-04-21 21:13:18 +00:00
|
|
|
ModuleProvider*
|
2004-11-14 21:58:33 +00:00
|
|
|
Archive::findModuleDefiningSymbol(const std::string& symbol) {
|
|
|
|
SymTabType::iterator SI = symTab.find(symbol);
|
|
|
|
if (SI == symTab.end())
|
|
|
|
return 0;
|
|
|
|
|
2005-04-21 21:13:18 +00:00
|
|
|
// The symbol table was previously constructed assuming that the members were
|
2004-11-14 21:58:33 +00:00
|
|
|
// written without the symbol table header. Because VBR encoding is used, the
|
|
|
|
// values could not be adjusted to account for the offset of the symbol table
|
|
|
|
// because that could affect the size of the symbol table due to VBR encoding.
|
2005-04-21 21:13:18 +00:00
|
|
|
// We now have to account for this by adjusting the offset by the size of the
|
2004-11-14 21:58:33 +00:00
|
|
|
// symbol table and its header.
|
2005-04-21 21:13:18 +00:00
|
|
|
unsigned fileOffset =
|
2004-11-14 21:58:33 +00:00
|
|
|
SI->second + // offset in symbol-table-less file
|
|
|
|
firstFileOffset; // add offset to first "real" file in archive
|
|
|
|
|
|
|
|
// See if the module is already loaded
|
|
|
|
ModuleMap::iterator MI = modules.find(fileOffset);
|
|
|
|
if (MI != modules.end())
|
|
|
|
return MI->second.first;
|
|
|
|
|
|
|
|
// Module hasn't been loaded yet, we need to load it
|
|
|
|
const char* modptr = base + fileOffset;
|
|
|
|
ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size());
|
|
|
|
|
|
|
|
// Now, load the bytecode module to get the ModuleProvider
|
2005-04-21 21:13:18 +00:00
|
|
|
std::string FullMemberName = archPath.toString() + "(" +
|
2004-12-11 00:14:15 +00:00
|
|
|
mbr->getPath().toString() + ")";
|
2004-11-14 21:58:33 +00:00
|
|
|
ModuleProvider* mp = getBytecodeBufferModuleProvider(
|
2005-04-21 21:13:18 +00:00
|
|
|
(const unsigned char*) mbr->getData(), mbr->getSize(),
|
2004-11-17 18:25:21 +00:00
|
|
|
FullMemberName, 0);
|
2004-11-14 21:58:33 +00:00
|
|
|
|
2004-11-20 07:29:40 +00:00
|
|
|
modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr)));
|
2004-11-14 21:58:33 +00:00
|
|
|
|
|
|
|
return mp;
|
|
|
|
}
|
|
|
|
|
2005-04-21 21:13:18 +00:00
|
|
|
// Look up multiple symbols in the symbol table and return a set of
|
2004-11-14 21:58:33 +00:00
|
|
|
// ModuleProviders that define those symbols.
|
|
|
|
void
|
2004-11-19 03:18:22 +00:00
|
|
|
Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
|
2004-11-15 01:20:11 +00:00
|
|
|
std::set<ModuleProvider*>& result)
|
2004-11-14 21:58:33 +00:00
|
|
|
{
|
2004-11-15 01:20:11 +00:00
|
|
|
assert(mapfile && base && "Can't findModulesDefiningSymbols on new archive");
|
|
|
|
if (symTab.empty()) {
|
|
|
|
// We don't have a symbol table, so we must build it now but lets also
|
|
|
|
// make sure that we populate the modules table as we do this to ensure
|
|
|
|
// that we don't load them twice when findModuleDefiningSymbol is called
|
|
|
|
// below.
|
|
|
|
|
|
|
|
// Get a pointer to the first file
|
|
|
|
const char* At = ((const char*)base) + firstFileOffset;
|
|
|
|
const char* End = ((const char*)base) + mapfile->size();
|
|
|
|
|
|
|
|
while ( At < End) {
|
|
|
|
// Compute the offset to be put in the symbol table
|
|
|
|
unsigned offset = At - base - firstFileOffset;
|
|
|
|
|
|
|
|
// Parse the file's header
|
|
|
|
ArchiveMember* mbr = parseMemberHeader(At, End);
|
|
|
|
|
|
|
|
// If it contains symbols
|
|
|
|
if (mbr->isBytecode() || mbr->isCompressedBytecode()) {
|
2005-04-21 21:13:18 +00:00
|
|
|
// Get the symbols
|
2004-11-15 01:20:11 +00:00
|
|
|
std::vector<std::string> symbols;
|
2005-04-21 21:13:18 +00:00
|
|
|
std::string FullMemberName = archPath.toString() + "(" +
|
2004-12-11 00:14:15 +00:00
|
|
|
mbr->getPath().toString() + ")";
|
2004-11-15 01:20:11 +00:00
|
|
|
ModuleProvider* MP = GetBytecodeSymbols((const unsigned char*)At,
|
2004-11-17 18:25:21 +00:00
|
|
|
mbr->getSize(), FullMemberName, symbols);
|
2004-11-15 01:20:11 +00:00
|
|
|
|
|
|
|
if (MP) {
|
|
|
|
// Insert the module's symbols into the symbol table
|
2005-04-21 21:13:18 +00:00
|
|
|
for (std::vector<std::string>::iterator I = symbols.begin(),
|
2004-11-15 01:20:11 +00:00
|
|
|
E=symbols.end(); I != E; ++I ) {
|
2004-11-20 07:29:40 +00:00
|
|
|
symTab.insert(std::make_pair(*I, offset));
|
2004-11-15 01:20:11 +00:00
|
|
|
}
|
|
|
|
// Insert the ModuleProvider and the ArchiveMember into the table of
|
|
|
|
// modules.
|
2004-11-20 07:29:40 +00:00
|
|
|
modules.insert(std::make_pair(offset, std::make_pair(MP, mbr)));
|
2004-11-15 01:20:11 +00:00
|
|
|
} else {
|
|
|
|
throw std::string("Can't parse bytecode member: ") +
|
2004-12-11 00:14:15 +00:00
|
|
|
mbr->getPath().toString();
|
2004-11-15 01:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
2004-11-15 01:40:20 +00:00
|
|
|
|
|
|
|
// Go to the next file location
|
|
|
|
At += mbr->getSize();
|
2004-11-19 17:08:00 +00:00
|
|
|
if ((intptr_t(At) & 1) == 1)
|
2004-11-15 01:40:20 +00:00
|
|
|
At++;
|
2004-11-15 01:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-21 21:13:18 +00:00
|
|
|
// At this point we have a valid symbol table (one way or another) so we
|
2004-11-15 01:20:11 +00:00
|
|
|
// just use it to quickly find the symbols requested.
|
|
|
|
|
2005-04-21 21:13:18 +00:00
|
|
|
for (std::set<std::string>::iterator I=symbols.begin(),
|
2004-11-19 03:18:22 +00:00
|
|
|
E=symbols.end(); I != E;) {
|
|
|
|
// See if this symbol exists
|
2004-11-14 21:58:33 +00:00
|
|
|
ModuleProvider* mp = findModuleDefiningSymbol(*I);
|
|
|
|
if (mp) {
|
2004-11-19 03:18:22 +00:00
|
|
|
// The symbol exists, insert the ModuleProvider into our result,
|
|
|
|
// duplicates wil be ignored
|
2004-11-15 01:20:11 +00:00
|
|
|
result.insert(mp);
|
2004-11-19 03:18:22 +00:00
|
|
|
|
2005-04-21 21:13:18 +00:00
|
|
|
// Remove the symbol now that its been resolved, being careful to
|
2004-11-19 03:44:10 +00:00
|
|
|
// post-increment the iterator.
|
|
|
|
symbols.erase(I++);
|
2004-11-19 03:18:22 +00:00
|
|
|
} else {
|
|
|
|
++I;
|
2004-11-14 21:58:33 +00:00
|
|
|
}
|
|
|
|
}
|
2003-04-19 21:45:34 +00:00
|
|
|
}
|
2005-02-26 22:00:32 +00:00
|
|
|
|
2005-09-23 06:22:58 +00:00
|
|
|
bool Archive::isBytecodeArchive() {
|
|
|
|
// Make sure the symTab has been loaded. In most cases this should have been
|
|
|
|
// done when the archive was constructed, but still, this is just in case.
|
|
|
|
if (!symTab.size())
|
2005-02-26 22:00:32 +00:00
|
|
|
loadSymbolTable();
|
|
|
|
|
2005-09-23 06:22:58 +00:00
|
|
|
// Now that we know it's been loaded, return true
|
|
|
|
// if it has a size
|
|
|
|
if (symTab.size()) return true;
|
2005-02-26 22:00:32 +00:00
|
|
|
|
|
|
|
//We still can't be sure it isn't a bytecode archive
|
|
|
|
loadArchive();
|
|
|
|
|
|
|
|
std::vector<Module *> Modules;
|
|
|
|
std::string ErrorMessage;
|
|
|
|
|
2005-09-23 06:22:58 +00:00
|
|
|
// Scan the archive, trying to load a bytecode member. We only load one to
|
|
|
|
// see if this works.
|
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I) {
|
|
|
|
if (!I->isBytecode() && !I->isCompressedBytecode())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string FullMemberName =
|
|
|
|
archPath.toString() + "(" + I->getPath().toString() + ")";
|
|
|
|
Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
|
|
|
|
I->getSize(), FullMemberName);
|
|
|
|
if (!M)
|
|
|
|
return false; // Couldn't parse bytecode, not a bytecode archive.
|
|
|
|
delete M;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2005-02-26 22:00:32 +00:00
|
|
|
}
|