#include "ArchiveInternals.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Module.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/System/Process.h"
#include <memory>
#include <cstring>
using namespace llvm;
unsigned
ArchiveMember::getMemberSize() const {
unsigned result = info.fileSize + sizeof(ArchiveMemberHeader);
if (hasLongFilename())
result += path.str().length() + 1;
if (result % 2 != 0 )
result++;
return result;
}
ArchiveMember::ArchiveMember()
: 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();
}
ArchiveMember::ArchiveMember(Archive* PAR)
: parent(PAR), path(), flags(0), data(0)
{
}
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;
if (path.str() == ARFILE_SVR4_SYMTAB_NAME)
flags |= SVR4SymbolTableFlag;
else
flags &= ~SVR4SymbolTableFlag;
if (path.str() == ARFILE_BSD4_SYMTAB_NAME)
flags |= BSD4SymbolTableFlag;
else
flags &= ~BSD4SymbolTableFlag;
if (path.str() == ARFILE_LLVM_SYMTAB_NAME)
flags |= LLVMSymbolTableFlag;
else
flags &= ~LLVMSymbolTableFlag;
if (path.str() == ARFILE_STRTAB_NAME)
flags |= StringTableFlag;
else
flags &= ~StringTableFlag;
bool hasSlash = path.str().find('/') != std::string::npos;
if (hasSlash)
flags |= HasPathFlag;
else
flags &= ~HasPathFlag;
if (hasSlash || path.str().length() > 15)
flags |= HasLongFilenameFlag;
else
flags &= ~HasLongFilenameFlag;
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;
}
switch (sys::IdentifyFileType(signature,4)) {
case sys::Bitcode_FileType:
flags |= BitcodeFlag;
break;
default:
flags &= ~BitcodeFlag;
break;
}
return false;
}
Archive::Archive(const sys::Path& filename, LLVMContext& C)
: archPath(filename), members(), mapfile(0), base(0), symTab(), strtab(),
symTabSize(0), firstFileOffset(0), modules(), foreignST(0), Context(C) {
}
bool
Archive::mapToMemory(std::string* ErrMsg) {
mapfile = MemoryBuffer::getFile(archPath.c_str(), ErrMsg);
if (mapfile == 0)
return true;
base = mapfile->getBufferStart();
return false;
}
void Archive::cleanUpMemory() {
delete mapfile;
mapfile = 0;
base = 0;
symTab.clear();
symTabSize = 0;
firstFileOffset = 0;
if (foreignST) {
delete foreignST;
foreignST = 0;
}
for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) {
delete I->second.first;
delete I->second.second;
}
}
Archive::~Archive() {
cleanUpMemory();
}
static void getSymbols(Module*M, std::vector<std::string>& symbols) {
for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
if (!GI->isDeclaration() && !GI->hasLocalLinkage())
if (!GI->getName().empty())
symbols.push_back(GI->getName());
for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
if (!FI->isDeclaration() && !FI->hasLocalLinkage())
if (!FI->getName().empty())
symbols.push_back(FI->getName());
for (Module::alias_iterator AI = M->alias_begin(), AE = M->alias_end();
AI != AE; ++AI) {
if (AI->hasName())
symbols.push_back(AI->getName());
}
}
bool llvm::GetBitcodeSymbols(const sys::Path& fName,
LLVMContext& Context,
std::vector<std::string>& symbols,
std::string* ErrMsg) {
std::auto_ptr<MemoryBuffer> Buffer(
MemoryBuffer::getFileOrSTDIN(fName.c_str()));
if (!Buffer.get()) {
if (ErrMsg) *ErrMsg = "Could not open file '" + fName.str() + "'";
return true;
}
Module *M = ParseBitcodeFile(Buffer.get(), Context, ErrMsg);
if (!M)
return true;
getSymbols(M, symbols);
delete M;
return true;
}
Module*
llvm::GetBitcodeSymbols(const unsigned char *BufPtr, unsigned Length,
const std::string& ModuleID,
LLVMContext& Context,
std::vector<std::string>& symbols,
std::string* ErrMsg) {
std::auto_ptr<MemoryBuffer> Buffer(
MemoryBuffer::getNewMemBuffer(Length, ModuleID.c_str()));
memcpy((char*)Buffer->getBufferStart(), BufPtr, Length);
Module *M = ParseBitcodeFile(Buffer.get(), Context, ErrMsg);
if (!M)
return 0;
getSymbols(M, symbols);
return M;
}