aboutsummaryrefslogtreecommitdiff
path: root/lib/Bytecode
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-05-06 19:50:53 +0000
committerChris Lattner <sabre@nondot.org>2007-05-06 19:50:53 +0000
commit38a5f7488202a9594b01dbca143a730184e1a3ad (patch)
tree09565cfe9e4ffc66b21728f85c2f3c24fc7eed9d /lib/Bytecode
parent6593380c0ed340b16c32a6580d16cc458b44f321 (diff)
move this to lib/Archive
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36888 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Bytecode')
-rw-r--r--lib/Bytecode/Archive/Archive.cpp271
-rw-r--r--lib/Bytecode/Archive/ArchiveInternals.h83
-rw-r--r--lib/Bytecode/Archive/ArchiveReader.cpp633
-rw-r--r--lib/Bytecode/Archive/ArchiveWriter.cpp482
-rw-r--r--lib/Bytecode/Archive/Makefile17
5 files changed, 0 insertions, 1486 deletions
diff --git a/lib/Bytecode/Archive/Archive.cpp b/lib/Bytecode/Archive/Archive.cpp
deleted file mode 100644
index 6fabc5c862..0000000000
--- a/lib/Bytecode/Archive/Archive.cpp
+++ /dev/null
@@ -1,271 +0,0 @@
-//===-- Archive.cpp - Generic LLVM archive functions ------------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file was developed by Reid Spencer and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file contains the implementation of the Archive and ArchiveMember
-// classes that is common to both reading and writing archives..
-//
-//===----------------------------------------------------------------------===//
-
-#include "ArchiveInternals.h"
-#include "llvm/Bitcode/ReaderWriter.h"
-#include "llvm/ModuleProvider.h"
-#include "llvm/Module.h"
-#include "llvm/Support/MemoryBuffer.h"
-#include "llvm/System/Process.h"
-using namespace llvm;
-
-// getMemberSize - compute the actual physical size of the file member as seen
-// on disk. This isn't the size of member's payload. Use getSize() for that.
-unsigned
-ArchiveMember::getMemberSize() const {
- // Basically its the file size plus the header size
- unsigned result = info.fileSize + sizeof(ArchiveMemberHeader);
-
- // If it has a long filename, include the name length
- if (hasLongFilename())
- result += path.toString().length() + 1;
-
- // If its now odd lengthed, include the padding byte
- if (result % 2 != 0 )
- result++;
-
- return result;
-}
-
-// This default constructor is only use by the ilist when it creates its
-// sentry node. We give it specific static values to make it stand out a bit.
-ArchiveMember::ArchiveMember()
- : next(0), prev(0), parent(0), path("--invalid--"), flags(0), data(0)
-{
- info.user = sys::Process::GetCurrentUserId();
- info.group = sys::Process::GetCurrentGroupId();
- info.mode = 0777;
- info.fileSize = 0;
- info.modTime = sys::TimeValue::now();
-}
-
-// This is the constructor that the Archive class uses when it is building or
-// reading an archive. It just defaults a few things and ensures the parent is
-// set for the iplist. The Archive class fills in the ArchiveMember's data.
-// This is required because correctly setting the data may depend on other
-// things in the Archive.
-ArchiveMember::ArchiveMember(Archive* PAR)
- : next(0), prev(0), parent(PAR), path(), flags(0), data(0)
-{
-}
-
-// This method allows an ArchiveMember to be replaced with the data for a
-// different file, presumably as an update to the member. It also makes sure
-// the flags are reset correctly.
-bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) {
- if (!newFile.exists()) {
- if (ErrMsg)
- *ErrMsg = "Can not replace an archive member with a non-existent file";
- return true;
- }
-
- data = 0;
- path = newFile;
-
- // SVR4 symbol tables have an empty name
- if (path.toString() == ARFILE_SVR4_SYMTAB_NAME)
- flags |= SVR4SymbolTableFlag;
- else
- flags &= ~SVR4SymbolTableFlag;
-
- // BSD4.4 symbol tables have a special name
- if (path.toString() == ARFILE_BSD4_SYMTAB_NAME)
- flags |= BSD4SymbolTableFlag;
- else
- flags &= ~BSD4SymbolTableFlag;
-
- // LLVM symbol tables have a very specific name
- if (path.toString() == ARFILE_LLVM_SYMTAB_NAME)
- flags |= LLVMSymbolTableFlag;
- else
- flags &= ~LLVMSymbolTableFlag;
-
- // String table name
- if (path.toString() == ARFILE_STRTAB_NAME)
- flags |= StringTableFlag;
- else
- flags &= ~StringTableFlag;
-
- // If it has a slash then it has a path
- bool hasSlash = path.toString().find('/') != std::string::npos;
- if (hasSlash)
- flags |= HasPathFlag;
- else
- flags &= ~HasPathFlag;
-
- // If it has a slash or its over 15 chars then its a long filename format
- if (hasSlash || path.toString().length() > 15)
- flags |= HasLongFilenameFlag;
- else
- flags &= ~HasLongFilenameFlag;
-
- // Get the signature and status info
- const char* signature = (const char*) data;
- std::string magic;
- if (!signature) {
- path.getMagicNumber(magic,4);
- signature = magic.c_str();
- std::string err;
- const sys::FileStatus *FSinfo = path.getFileStatus(false, ErrMsg);
- if (FSinfo)
- info = *FSinfo;
- else
- return true;
- }
-
- // Determine what kind of file it is
- switch (sys::IdentifyFileType(signature,4)) {
- case sys::Bytecode_FileType:
- flags |= BytecodeFlag;
- break;
- case sys::CompressedBytecode_FileType:
- flags |= CompressedBytecodeFlag;
- flags &= ~CompressedFlag;
- break;
- default:
- flags &= ~(BytecodeFlag|CompressedBytecodeFlag);
- break;
- }
- return false;
-}
-
-// Archive constructor - this is the only constructor that gets used for the
-// Archive class. Everything else (default,copy) is deprecated. This just
-// initializes and maps the file into memory, if requested.
-Archive::Archive(const sys::Path& filename)
- : archPath(filename), members(), mapfile(0), base(0), symTab(), strtab(),
- symTabSize(0), firstFileOffset(0), modules(), foreignST(0) {
-}
-
-bool
-Archive::mapToMemory(std::string* ErrMsg)
-{
- mapfile = new sys::MappedFile();
- if (mapfile->open(archPath, sys::MappedFile::READ_ACCESS, ErrMsg))
- return true;
- if (!(base = (char*) mapfile->map(ErrMsg)))
- return true;
- return false;
-}
-
-void Archive::cleanUpMemory() {
- // Shutdown the file mapping
- if (mapfile) {
- mapfile->close();
- delete mapfile;
-
- mapfile = 0;
- base = 0;
- }
-
- // Forget the entire symbol table
- symTab.clear();
- symTabSize = 0;
-
- firstFileOffset = 0;
-
- // Free the foreign symbol table member
- if (foreignST) {
- delete foreignST;
- foreignST = 0;
- }
-
- // Delete any ModuleProviders and ArchiveMember's we've allocated as a result
- // of symbol table searches.
- for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) {
- delete I->second.first;
- delete I->second.second;
- }
-}
-
-// Archive destructor - just clean up memory
-Archive::~Archive() {
- cleanUpMemory();
-}
-
-
-
-static void getSymbols(Module*M, std::vector<std::string>& symbols) {
- // Loop over global variables
- for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
- if (!GI->isDeclaration() && !GI->hasInternalLinkage())
- if (!GI->getName().empty())
- symbols.push_back(GI->getName());
-
- // Loop over functions.
- for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
- if (!FI->isDeclaration() && !FI->hasInternalLinkage())
- if (!FI->getName().empty())
- symbols.push_back(FI->getName());
-}
-
-// Get just the externally visible defined symbols from the bytecode
-bool llvm::GetBytecodeSymbols(const sys::Path& fName,
- std::vector<std::string>& symbols,
- std::string* ErrMsg) {
- std::auto_ptr<MemoryBuffer> Buffer(
- MemoryBuffer::getFileOrSTDIN(&fName.toString()[0],
- fName.toString().size()));
- if (!Buffer.get()) {
- if (ErrMsg) *ErrMsg = "Could not open file '" + fName.toString() + "'";
- return true;
- }
-
- ModuleProvider *MP = getBitcodeModuleProvider(Buffer.get(), ErrMsg);
- if (!MP)
- return true;
-
- // Get the module from the provider
- Module* M = MP->materializeModule();
- if (M == 0) {
- delete MP;
- return true;
- }
-
- // Get the symbols
- getSymbols(M, symbols);
-
- // Done with the module.
- delete MP;
- return true;
-}
-
-ModuleProvider*
-llvm::GetBytecodeSymbols(const unsigned char *BufPtr, unsigned Length,
- const std::string& ModuleID,
- std::vector<std::string>& symbols,
- std::string* ErrMsg) {
- // Get the module provider
- MemoryBuffer *Buffer =MemoryBuffer::getNewMemBuffer(Length, ModuleID.c_str());
- memcpy((char*)Buffer->getBufferStart(), BufPtr, Length);
-
- ModuleProvider *MP = getBitcodeModuleProvider(Buffer, ErrMsg);
- if (!MP)
- return 0;
-
- // Get the module from the provider
- Module* M = MP->materializeModule();
- if (M == 0) {
- delete MP;
- return 0;
- }
-
- // Get the symbols
- getSymbols(M, symbols);
-
- // Done with the module. Note that ModuleProvider will delete the
- // Module when it is deleted. Also note that its the caller's responsibility
- // to delete the ModuleProvider.
- return MP;
-}
diff --git a/lib/Bytecode/Archive/ArchiveInternals.h b/lib/Bytecode/Archive/ArchiveInternals.h
deleted file mode 100644
index 4642f7a30a..0000000000
--- a/lib/Bytecode/Archive/ArchiveInternals.h
+++ /dev/null
@@ -1,83 +0,0 @@
-//===-- lib/Bytecode/ArchiveInternals.h -------------------------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file was developed by Reid Spencer and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// Internal implementation header for LLVM Archive files.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LIB_BYTECODE_ARCHIVEINTERNALS_H
-#define LIB_BYTECODE_ARCHIVEINTERNALS_H
-
-#include "llvm/Bitcode/Archive.h"
-#include "llvm/System/TimeValue.h"
-#include "llvm/ADT/StringExtras.h"
-
-#define ARFILE_MAGIC "!<arch>\n" ///< magic string
-#define ARFILE_MAGIC_LEN (sizeof(ARFILE_MAGIC)-1) ///< length of magic string
-#define ARFILE_SVR4_SYMTAB_NAME "/ " ///< SVR4 symtab entry name
-#define ARFILE_LLVM_SYMTAB_NAME "#_LLVM_SYM_TAB_#" ///< LLVM symtab entry name
-#define ARFILE_BSD4_SYMTAB_NAME "__.SYMDEF SORTED" ///< BSD4 symtab entry name
-#define ARFILE_STRTAB_NAME "// " ///< Name of string table
-#define ARFILE_PAD "\n" ///< inter-file align padding
-#define ARFILE_MEMBER_MAGIC "`\n" ///< fmag field magic #
-
-namespace llvm {
-
- /// The ArchiveMemberHeader structure is used internally for bytecode
- /// archives.
- /// The header precedes each file member in the archive. This structure is
- /// defined using character arrays for direct and correct interpretation
- /// regardless of the endianess of the machine that produced it.
- /// @brief Archive File Member Header
- class ArchiveMemberHeader {
- /// @name Data
- /// @{
- public:
- char name[16]; ///< Name of the file member.
- char date[12]; ///< File date, decimal seconds since Epoch
- char uid[6]; ///< user id in ASCII decimal
- char gid[6]; ///< group id in ASCII decimal
- char mode[8]; ///< file mode in ASCII octal
- char size[10]; ///< file size in ASCII decimal
- char fmag[2]; ///< Always contains ARFILE_MAGIC_TERMINATOR
-
- /// @}
- /// @name Methods
- /// @{
- public:
- void init() {
- memset(name,' ',16);
- memset(date,' ',12);
- memset(uid,' ',6);
- memset(gid,' ',6);
- memset(mode,' ',8);
- memset(size,' ',10);
- fmag[0] = '`';
- fmag[1] = '\n';
- }
-
- bool checkSignature() {
- return 0 == memcmp(fmag, ARFILE_MEMBER_MAGIC,2);
- }
- };
-
- // Get just the externally visible defined symbols from the bytecode
- bool GetBytecodeSymbols(const sys::Path& fName,
- std::vector<std::string>& symbols,
- std::string* ErrMsg);
-
- ModuleProvider* GetBytecodeSymbols(const unsigned char*Buffer,unsigned Length,
- const std::string& ModuleID,
- std::vector<std::string>& symbols,
- std::string* ErrMsg);
-}
-
-#endif
-
-// vim: sw=2 ai
diff --git a/lib/Bytecode/Archive/ArchiveReader.cpp b/lib/Bytecode/Archive/ArchiveReader.cpp
deleted file mode 100644
index c38389e2d7..0000000000
--- a/lib/Bytecode/Archive/ArchiveReader.cpp
+++ /dev/null
@@ -1,633 +0,0 @@
-//===-- ArchiveReader.cpp - Read LLVM archive files -------------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file was developed by Reid Spencer and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// Builds up standard unix archive files (.a) containing LLVM bytecode.
-//
-//===----------------------------------------------------------------------===//
-
-#include "ArchiveInternals.h"
-#include "llvm/Bitcode/ReaderWriter.h"
-#include "llvm/Support/MemoryBuffer.h"
-#include "llvm/Module.h"
-#include <memory>
-using namespace llvm;
-
-/// Read a variable-bit-rate encoded unsigned integer
-inline unsigned readInteger(const char*&At, const char*End){
- unsigned Shift = 0;
- unsigned Result = 0;
-
- do {
- if (At == End)
- return Result;
- Result |= (unsigned)((*At++) & 0x7F) << Shift;
- Shift += 7;
- } while (At[-1] & 0x80);
- return Result;
-}
-
-// Completely parse the Archive's symbol table and populate symTab member var.
-bool
-Archive::parseSymbolTable(const void* data, unsigned size, std::string* error) {
- const char* At = (const char*) data;
- const char* End = At + size;
- while (At < End) {
- unsigned offset = readInteger(At, End);
- if (At == End) {
- if (error)
- *error = "Ran out of data reading vbr_uint for symtab offset!";
- return false;
- }
- unsigned length = readInteger(At, End);
- if (At == End) {
- if (error)
- *error = "Ran out of data reading vbr_uint for symtab length!";
- return false;
- }
- if (At + length > End) {
- if (error)
- *error = "Malformed symbol table: length not consistent with size";
- return false;
- }
- // we don't care if it can't be inserted (duplicate entry)
- symTab.insert(std::make_pair(std::string(At, length), offset));
- At += length;
- }
- symTabSize = size;
- return true;
-}
-
-// 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
-// can be variable in size.
-ArchiveMember*
-Archive::parseMemberHeader(const char*& At, const char* End, std::string* error)
-{
- if (At + sizeof(ArchiveMemberHeader) >= End) {
- if (error)
- *error = "Unexpected end of file";
- return 0;
- }
-
- // Cast archive member header
- ArchiveMemberHeader* Hdr = (ArchiveMemberHeader*)At;
- At += sizeof(ArchiveMemberHeader);
-
- // Extract the size and determine if the file is
- // 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) {
- if (error)
- *error = "invalid member length in archive file";
- return 0;
- }
-
- // Check the member signature
- if (!Hdr->checkSignature()) {
- if (error)
- *error = "invalid file member signature";
- return 0;
- }
-
- // Convert and check the member name
- // 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
- // for long file names. This library doesn't generate either of those but
- // 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
- // 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]);
- pathname.assign(At, len);
- At += len;
- MemberSize -= len;
- flags |= ArchiveMember::HasLongFilenameFlag;
- } else {
- if (error)
- *error = "invalid long filename";
- return 0;
- }
- } else if (Hdr->name[1] == '_' &&
- (0 == memcmp(Hdr->name, ARFILE_LLVM_SYMTAB_NAME, 16))) {
- // 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;
- }
- break;
- case '/':
- if (Hdr->name[1]== '/') {
- if (0 == memcmp(Hdr->name, ARFILE_STRTAB_NAME, 16)) {
- pathname.assign(ARFILE_STRTAB_NAME);
- flags |= ArchiveMember::StringTableFlag;
- } else {
- if (error)
- *error = "invalid string table name";
- return 0;
- }
- } else if (Hdr->name[1] == ' ') {
- if (0 == memcmp(Hdr->name, ARFILE_SVR4_SYMTAB_NAME, 16)) {
- pathname.assign(ARFILE_SVR4_SYMTAB_NAME);
- flags |= ArchiveMember::SVR4SymbolTableFlag;
- } else {
- if (error)
- *error = "invalid SVR4 symbol table name";
- return 0;
- }
- } 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 == '/') {
- pathname.assign(namep, last_p - namep);
- flags |= ArchiveMember::HasLongFilenameFlag;
- break;
- }
- last_p = p;
- p++;
- }
- if (p >= endp) {
- if (error)
- *error = "missing name termiantor in string table";
- return 0;
- }
- } else {
- if (error)
- *error = "name index beyond string table";
- return 0;
- }
- }
- break;
- case '_':
- if (Hdr->name[1] == '_' &&
- (0 == memcmp(Hdr->name, ARFILE_BSD4_SYMTAB_NAME, 16))) {
- pathname.assign(ARFILE_BSD4_SYMTAB_NAME);
- flags |= ArchiveMember::BSD4SymbolTableFlag;
- break;
- }
- /* FALL THROUGH */
-
- default:
- char* slash = (char*) memchr(Hdr->name, '/', 16);
- if (slash == 0)
- slash = Hdr->name + 16;
- pathname.assign(Hdr->name, slash - Hdr->name);
- break;
- }
-
- // Determine if this is a bytecode file
- switch (sys::IdentifyFileType(At, 4)) {
- case sys::Bitcode_FileType:
- case sys::Bytecode_FileType:
- flags |= ArchiveMember::BytecodeFlag;
- break;
- case sys::CompressedBytecode_FileType:
- flags |= ArchiveMember::CompressedBytecodeFlag;
- flags &= ~ArchiveMember::CompressedFlag;
- break;
- default:
- flags &= ~(ArchiveMember::BytecodeFlag|
- ArchiveMember::CompressedBytecodeFlag);
- break;
- }
-
- // Instantiate the ArchiveMember to be filled
- ArchiveMember* member = new ArchiveMember(this);
-
- // Fill in fields of the ArchiveMember
- member->next = 0;
- member->prev = 0;
- member->parent = this;
- member->path.set(pathname);
- member->info.fileSize = MemberSize;
- member->info.modTime.fromEpochTime(atoi(Hdr->date));
- unsigned int mode;
- sscanf(Hdr->mode, "%o", &mode);
- member->info.mode = mode;
- member->info.user = atoi(Hdr->uid);
- member->info.group = atoi(Hdr->gid);
- member->flags = flags;
- member->data = At;
-
- return member;
-}
-
-bool
-Archive::checkSignature(std::string* error) {
- // Check the magic string at file's header
- if (mapfile->size() < 8 || memcmp(base, ARFILE_MAGIC, 8)) {
- if (error)
- *error = "invalid signature for an archive file";
- return false;
- }
- return true;
-}
-
-// This function loads the entire archive and fully populates its ilist with
-// the members of the archive file. This is typically used in preparation for
-// editing the contents of the archive.
-bool
-Archive::loadArchive(std::string* error) {
-
- // Set up parsing
- members.clear();
- symTab.clear();
- const char *At = base;
- const char *End = base + mapfile->size();
-
- if (!checkSignature(error))
- return false;
-
- At += 8; // Skip the magic string.
-
- bool seenSymbolTable = false;
- bool foundFirstFile = false;
- while (At < End) {
- // parse the member header
- const char* Save = At;
- ArchiveMember* mbr = parseMemberHeader(At, End, error);
- if (!mbr)
- return false;
-
- // check if this is the foreign symbol table
- if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) {
- // We just save this but don't do anything special
- // with it. It doesn't count as the "first file".
- if (foreignST) {
- // What? Multiple foreign symbol tables? Just chuck it
- // and retain the last one found.
- delete foreignST;
- }
- foreignST = mbr;
- At += mbr->getSize();
- if ((intptr_t(At) & 1) == 1)
- At++;
- } else if (mbr->isStringTable()) {
- // 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).
- strtab.assign(At, mbr->getSize());
- At += mbr->getSize();
- if ((intptr_t(At) & 1) == 1)
- At++;
- delete mbr;
- } else if (mbr->isLLVMSymbolTable()) {
- // 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.
- if (seenSymbolTable) {
- if (error)
- *error = "invalid archive: multiple symbol tables";
- return false;
- }
- if (!parseSymbolTable(mbr->getData(), mbr->getSize(), error))
- return false;
- seenSymbolTable = true;
- At += mbr->getSize();
- if ((intptr_t(At) & 1) == 1)
- At++;
- delete mbr; // We don't need this member in the list of members.
- } else {
- // 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.
- if (!foundFirstFile) {
- firstFileOffset = Save - base;
- foundFirstFile = true;
- }
- members.push_back(mbr);
- At += mbr->getSize();
- if ((intptr_t(At) & 1) == 1)
- At++;
- }
- }
- return true;
-}
-
-// Open and completely load the archive file.
-Archive*
-Archive::OpenAndLoad(const sys::Path& file, std::string* ErrorMessage)
-{
- std::auto_ptr<Archive> result ( new Archive(file));
- if (result->mapToMemory(ErrorMessage))
- return 0;
- if (!result->loadArchive(ErrorMessage))
- return 0;
- return result.release();
-}
-
-// 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()) {
- std::string FullMemberName = archPath.toString() +
- "(" + I->getPath().toString() + ")";
- MemoryBuffer *Buffer =
- MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
- memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
-
- Module *M = ParseBitcodeFile(Buffer, ErrMessage);
- delete Buffer;
- if (!M)
- return true;
-
- Modules.push_back(M);
- }
- }
- return false;
-}
-
-// Load just the symbol table from the archive file
-bool
-Archive::loadSymbolTable(std::string* ErrorMsg) {
-
- // 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
- if (!checkSignature(ErrorMsg))
- return false;
-
- At += 8; // Skip signature
-
- // Parse the first file member header
- const char* FirstFile = At;
- ArchiveMember* mbr = parseMemberHeader(At, End, ErrorMsg);
- if (!mbr)
- return false;
-
- if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) {
- // Skip the foreign symbol table, we don't do anything with it
- At += mbr->getSize();
- if ((intptr_t(At) & 1) == 1)
- At++;
- delete mbr;
-
- // Read the next one
- FirstFile = At;
- mbr = parseMemberHeader(At, End, ErrorMsg);
- if (!mbr) {
- delete mbr;
- return false;
- }
- }
-
- if (mbr->isStringTable()) {
- // Process the string table entry
- strtab.assign((const char*)mbr->getData(), mbr->getSize());
- At += mbr->getSize();
- if ((intptr_t(At) & 1) == 1)
- At++;
- delete mbr;
- // Get the next one
- FirstFile = At;
- mbr = parseMemberHeader(At, End, ErrorMsg);
- if (!mbr) {
- delete mbr;
- return false;
- }
- }
-
- // See if its the symbol table
- if (mbr->isLLVMSymbolTable()) {
- if (!parseSymbolTable(mbr->getData(), mbr->getSize(), ErrorMsg)) {
- delete mbr;
- return false;
- }
-
- At += mbr->getSize();
- if ((intptr_t(At) & 1) == 1)
- At++;
- delete mbr;
- // Can't be any more symtab headers so just advance
- FirstFile = At;
- } else {
- // There's no symbol table in the file. We have to rebuild it from scratch
- // because the intent of this method is to get the symbol table loaded so
- // it can be searched efficiently.
- // Add the member to the members list
- members.push_back(mbr);
- }
-
- firstFileOffset = FirstFile - base;
- return true;
-}
-
-// Open the archive and load just the symbol tables
-Archive*
-Archive::OpenAndLoadSymbols(const sys::Path& file, std::string* ErrorMessage) {
- std::auto_ptr<Archive> result ( new Archive(file) );
- if (result->mapToMemory(ErrorMessage))
- return 0;
- if (!result->loadSymbolTable(ErrorMessage))
- return 0;
- return result.release();
-}
-
-// Look up one symbol in the symbol table and return a ModuleProvider for the
-// module that defines that symbol.
-ModuleProvider*
-Archive::findModuleDefiningSymbol(const std::string& symbol,
- std::string* ErrMsg) {
- SymTabType::iterator SI = symTab.find(symbol);
- if (SI == symTab.end())
- return 0;
-
- // The symbol table was previously constructed assuming that the members were
- // 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.
- // We now have to account for this by adjusting the offset by the size of the
- // symbol table and its header.
- unsigned fileOffset =
- 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(),ErrMsg);
- if (!mbr)
- return 0;
-
- // Now, load the bytecode module to get the ModuleProvider
- std::string FullMemberName = archPath.toString() + "(" +
- mbr->getPath().toString() + ")";
- MemoryBuffer *Buffer =MemoryBuffer::getNewMemBuffer(mbr->getSize(),
- FullMemberName.c_str());
- memcpy((char*)Buffer->getBufferStart(), mbr->getData(), mbr->getSize());
-
- ModuleProvider *mp = getBitcodeModuleProvider(Buffer, ErrMsg);
- if (!mp)
- return 0;
-
- modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr)));
-
- return mp;
-}
-
-// Look up multiple symbols in the symbol table and return a set of
-// ModuleProviders that define those symbols.
-bool
-Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
- std::set<ModuleProvider*>& result,
- std::string* error) {
- if (!mapfile || !base) {
- if (error)
- *error = "Empty archive invalid for finding modules defining symbols";
- return false;
- }
-
- 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, error);
- if (!mbr)
- return false;
-
- // If it contains symbols
- if (mbr->isBytecode() || mbr->isCompressedBytecode()) {
- // Get the symbols
- std::vector<std::string> symbols;
- std::string FullMemberName = archPath.toString() + "(" +
- mbr->getPath().toString() + ")";
- ModuleProvider* MP =
- GetBytecodeSymbols((const unsigned char*)At, mbr->getSize(),
- FullMemberName, symbols, error);
-
- if (MP) {
- // Insert the module's symbols into the symbol table
- for (std::vector<std::string>::iterator I = symbols.begin(),
- E=symbols.end(); I != E; ++I ) {
- symTab.insert(std::make_pair(*I, offset));
- }
- // Insert the ModuleProvider and the ArchiveMember into the table of
- // modules.
- modules.insert(std::make_pair(offset, std::make_pair(MP, mbr)));
- } else {
- if (error)
- *error = "Can't parse bytecode member: " +
- mbr->getPath().toString() + ": " + *error;
- delete mbr;
- return false;
- }
- }
-
- // Go to the next file location
- At += mbr->getSize();
- if ((intptr_t(At) & 1) == 1)
- At++;
- }
- }
-
- // At this point we have a valid symbol table (one way or another) so we
- // just use it to quickly find the symbols requested.
-
- for (std::set<std::string>::iterator I=symbols.begin(),
- E=symbols.end(); I != E;) {
- // See if this symbol exists
- ModuleProvider* mp = findModuleDefiningSymbol(*I,error);
- if (mp) {
- // The symbol exists, insert the ModuleProvider into our result,
- // duplicates wil be ignored
- result.insert(mp);
-
- // Remove the symbol now that its been resolved, being careful to
- // post-increment the iterator.
- symbols.erase(I++);
- } else {
- ++I;
- }
- }
- return true;
-}
-
-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())
- if (!loadSymbolTable(0))
- return false;
-
- // Now that we know it's been loaded, return true
- // if it has a size
- if (symTab.size()) return true;
-
- //We still can't be sure it isn't a bytecode archive
- if (!loadArchive(0))
- return false;
-
- std::vector<Module *> Modules;
- std::string ErrorMessage;
-
- // 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() + ")";
-
- MemoryBuffer *Buffer =
- MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
- memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
- M