2014-02-21 20:10:59 +00:00
|
|
|
//===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the IRObjectFile class implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-07-04 22:44:18 +00:00
|
|
|
#include "llvm/Object/IRObjectFile.h"
|
|
|
|
#include "RecordStreamer.h"
|
2014-02-21 20:10:59 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2014-06-23 21:53:12 +00:00
|
|
|
#include "llvm/IR/GVMaterializer.h"
|
2015-01-14 11:23:27 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2014-02-28 02:17:23 +00:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2014-02-21 20:10:59 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-07-03 18:59:23 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
|
|
|
#include "llvm/MC/MCParser/MCAsmParser.h"
|
2015-01-14 11:23:27 +00:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/MC/MCTargetAsmParser.h"
|
2014-09-18 21:28:49 +00:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2014-06-24 13:56:32 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-07-03 18:59:23 +00:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2014-02-21 20:21:55 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-02-21 20:10:59 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
2014-08-19 18:44:46 +00:00
|
|
|
IRObjectFile::IRObjectFile(MemoryBufferRef Object, std::unique_ptr<Module> Mod)
|
|
|
|
: SymbolicFile(Binary::ID_IR, Object), M(std::move(Mod)) {
|
2014-02-28 02:17:23 +00:00
|
|
|
// If we have a DataLayout, setup a mangler.
|
|
|
|
const DataLayout *DL = M->getDataLayout();
|
|
|
|
if (!DL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Mang.reset(new Mangler(DL));
|
2014-07-03 18:59:23 +00:00
|
|
|
|
|
|
|
const std::string &InlineAsm = M->getModuleInlineAsm();
|
|
|
|
if (InlineAsm.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
StringRef Triple = M->getTargetTriple();
|
|
|
|
std::string Err;
|
|
|
|
const Target *T = TargetRegistry::lookupTarget(Triple, Err);
|
|
|
|
if (!T)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(Triple));
|
|
|
|
if (!MRI)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, Triple));
|
|
|
|
if (!MAI)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unique_ptr<MCSubtargetInfo> STI(
|
|
|
|
T->createMCSubtargetInfo(Triple, "", ""));
|
|
|
|
if (!STI)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
|
|
|
|
if (!MCII)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MCObjectFileInfo MOFI;
|
|
|
|
MCContext MCCtx(MAI.get(), MRI.get(), &MOFI);
|
|
|
|
MOFI.InitMCObjectFileInfo(Triple, Reloc::Default, CodeModel::Default, MCCtx);
|
|
|
|
std::unique_ptr<RecordStreamer> Streamer(new RecordStreamer(MCCtx));
|
|
|
|
|
|
|
|
std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
|
|
|
|
SourceMgr SrcMgr;
|
2014-08-21 20:44:56 +00:00
|
|
|
SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
|
2014-07-03 18:59:23 +00:00
|
|
|
std::unique_ptr<MCAsmParser> Parser(
|
|
|
|
createMCAsmParser(SrcMgr, MCCtx, *Streamer, *MAI));
|
|
|
|
|
|
|
|
MCTargetOptions MCOptions;
|
|
|
|
std::unique_ptr<MCTargetAsmParser> TAP(
|
|
|
|
T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
|
|
|
|
if (!TAP)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Parser->setTargetParser(*TAP);
|
|
|
|
if (Parser->Run(false))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto &KV : *Streamer) {
|
|
|
|
StringRef Key = KV.first();
|
|
|
|
RecordStreamer::State Value = KV.second;
|
|
|
|
uint32_t Res = BasicSymbolRef::SF_None;
|
|
|
|
switch (Value) {
|
|
|
|
case RecordStreamer::NeverSeen:
|
|
|
|
llvm_unreachable("foo");
|
|
|
|
case RecordStreamer::DefinedGlobal:
|
|
|
|
Res |= BasicSymbolRef::SF_Global;
|
|
|
|
break;
|
|
|
|
case RecordStreamer::Defined:
|
|
|
|
break;
|
|
|
|
case RecordStreamer::Global:
|
|
|
|
case RecordStreamer::Used:
|
|
|
|
Res |= BasicSymbolRef::SF_Undefined;
|
|
|
|
Res |= BasicSymbolRef::SF_Global;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
AsmSymbols.push_back(
|
|
|
|
std::make_pair<std::string, uint32_t>(Key, std::move(Res)));
|
|
|
|
}
|
2014-02-21 20:10:59 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 18:40:36 +00:00
|
|
|
IRObjectFile::~IRObjectFile() {
|
|
|
|
}
|
2014-06-23 21:53:12 +00:00
|
|
|
|
2014-12-09 16:13:59 +00:00
|
|
|
static GlobalValue *getGV(DataRefImpl &Symb) {
|
2014-07-03 18:59:23 +00:00
|
|
|
if ((Symb.p & 3) == 3)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
|
2014-02-21 20:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
|
|
|
|
if (I == M.alias_end())
|
|
|
|
return 3;
|
|
|
|
const GlobalValue *GV = &*I;
|
|
|
|
return reinterpret_cast<uintptr_t>(GV) | 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
|
|
|
|
if (I == M.global_end())
|
|
|
|
return skipEmpty(M.alias_begin(), M);
|
|
|
|
const GlobalValue *GV = &*I;
|
|
|
|
return reinterpret_cast<uintptr_t>(GV) | 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
|
|
|
|
if (I == M.end())
|
|
|
|
return skipEmpty(M.global_begin(), M);
|
|
|
|
const GlobalValue *GV = &*I;
|
|
|
|
return reinterpret_cast<uintptr_t>(GV) | 0;
|
|
|
|
}
|
|
|
|
|
2014-07-03 18:59:23 +00:00
|
|
|
static unsigned getAsmSymIndex(DataRefImpl Symb) {
|
|
|
|
assert((Symb.p & uintptr_t(3)) == 3);
|
|
|
|
uintptr_t Index = Symb.p & ~uintptr_t(3);
|
|
|
|
Index >>= 2;
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2014-02-21 20:10:59 +00:00
|
|
|
void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
|
2014-07-03 18:59:23 +00:00
|
|
|
const GlobalValue *GV = getGV(Symb);
|
2014-02-21 20:10:59 +00:00
|
|
|
uintptr_t Res;
|
2014-07-03 18:59:23 +00:00
|
|
|
|
2014-02-21 20:10:59 +00:00
|
|
|
switch (Symb.p & 3) {
|
|
|
|
case 0: {
|
|
|
|
Module::const_iterator Iter(static_cast<const Function*>(GV));
|
|
|
|
++Iter;
|
2014-07-03 18:59:23 +00:00
|
|
|
Res = skipEmpty(Iter, *M);
|
2014-02-21 20:10:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: {
|
|
|
|
Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
|
|
|
|
++Iter;
|
2014-07-03 18:59:23 +00:00
|
|
|
Res = skipEmpty(Iter, *M);
|
2014-02-21 20:10:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
|
|
|
|
++Iter;
|
2014-07-03 18:59:23 +00:00
|
|
|
Res = skipEmpty(Iter, *M);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
|
|
|
unsigned Index = getAsmSymIndex(Symb);
|
|
|
|
assert(Index < AsmSymbols.size());
|
|
|
|
++Index;
|
|
|
|
Res = (Index << 2) | 3;
|
2014-02-21 20:10:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-12-15 18:48:43 +00:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unreachable case");
|
2014-02-21 20:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Symb.p = Res;
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:24:39 +00:00
|
|
|
std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
|
|
|
|
DataRefImpl Symb) const {
|
2014-07-03 18:59:23 +00:00
|
|
|
const GlobalValue *GV = getGV(Symb);
|
|
|
|
if (!GV) {
|
|
|
|
unsigned Index = getAsmSymIndex(Symb);
|
|
|
|
assert(Index <= AsmSymbols.size());
|
|
|
|
OS << AsmSymbols[Index].first;
|
|
|
|
return object_error::success;;
|
|
|
|
}
|
2014-02-28 02:17:23 +00:00
|
|
|
|
|
|
|
if (Mang)
|
2014-07-03 18:59:23 +00:00
|
|
|
Mang->getNameWithPrefix(OS, GV, false);
|
2014-02-28 02:17:23 +00:00
|
|
|
else
|
2014-07-03 18:59:23 +00:00
|
|
|
OS << GV->getName();
|
2014-02-28 02:17:23 +00:00
|
|
|
|
2014-02-21 20:10:59 +00:00
|
|
|
return object_error::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
|
2014-07-03 18:59:23 +00:00
|
|
|
const GlobalValue *GV = getGV(Symb);
|
|
|
|
|
|
|
|
if (!GV) {
|
|
|
|
unsigned Index = getAsmSymIndex(Symb);
|
|
|
|
assert(Index <= AsmSymbols.size());
|
|
|
|
return AsmSymbols[Index].second;
|
|
|
|
}
|
2014-02-21 20:10:59 +00:00
|
|
|
|
|
|
|
uint32_t Res = BasicSymbolRef::SF_None;
|
2014-10-24 18:13:04 +00:00
|
|
|
if (GV->isDeclarationForLinker())
|
2014-02-21 20:10:59 +00:00
|
|
|
Res |= BasicSymbolRef::SF_Undefined;
|
2014-07-03 18:59:23 +00:00
|
|
|
if (GV->hasPrivateLinkage())
|
2014-02-21 20:10:59 +00:00
|
|
|
Res |= BasicSymbolRef::SF_FormatSpecific;
|
2014-07-03 18:59:23 +00:00
|
|
|
if (!GV->hasLocalLinkage())
|
2014-02-21 20:10:59 +00:00
|
|
|
Res |= BasicSymbolRef::SF_Global;
|
2014-07-03 18:59:23 +00:00
|
|
|
if (GV->hasCommonLinkage())
|
2014-02-21 20:10:59 +00:00
|
|
|
Res |= BasicSymbolRef::SF_Common;
|
2014-07-03 18:59:23 +00:00
|
|
|
if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage())
|
2014-02-21 20:10:59 +00:00
|
|
|
Res |= BasicSymbolRef::SF_Weak;
|
|
|
|
|
2014-07-04 19:08:22 +00:00
|
|
|
if (GV->getName().startswith("llvm."))
|
|
|
|
Res |= BasicSymbolRef::SF_FormatSpecific;
|
|
|
|
else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
|
2014-07-04 15:03:17 +00:00
|
|
|
if (Var->getSection() == StringRef("llvm.metadata"))
|
|
|
|
Res |= BasicSymbolRef::SF_FormatSpecific;
|
|
|
|
}
|
|
|
|
|
2014-02-21 20:10:59 +00:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2014-12-09 16:13:59 +00:00
|
|
|
GlobalValue *IRObjectFile::getSymbolGV(DataRefImpl Symb) { return getGV(Symb); }
|
2014-02-21 20:10:59 +00:00
|
|
|
|
2014-12-09 16:18:11 +00:00
|
|
|
std::unique_ptr<Module> IRObjectFile::takeModule() { return std::move(M); }
|
|
|
|
|
2014-02-21 20:10:59 +00:00
|
|
|
basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
|
|
|
|
Module::const_iterator I = M->begin();
|
|
|
|
DataRefImpl Ret;
|
|
|
|
Ret.p = skipEmpty(I, *M);
|
|
|
|
return basic_symbol_iterator(BasicSymbolRef(Ret, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
|
|
|
|
DataRefImpl Ret;
|
2014-07-03 18:59:23 +00:00
|
|
|
uint64_t NumAsm = AsmSymbols.size();
|
|
|
|
NumAsm <<= 2;
|
|
|
|
Ret.p = 3 | NumAsm;
|
2014-02-21 20:10:59 +00:00
|
|
|
return basic_symbol_iterator(BasicSymbolRef(Ret, this));
|
|
|
|
}
|
|
|
|
|
2014-09-18 21:28:49 +00:00
|
|
|
ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
|
|
|
|
for (const SectionRef &Sec : Obj.sections()) {
|
|
|
|
StringRef SecName;
|
|
|
|
if (std::error_code EC = Sec.getName(SecName))
|
|
|
|
return EC;
|
|
|
|
if (SecName == ".llvmbc") {
|
|
|
|
StringRef SecContents;
|
|
|
|
if (std::error_code EC = Sec.getContents(SecContents))
|
|
|
|
return EC;
|
|
|
|
return MemoryBufferRef(SecContents, Obj.getFileName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return object_error::bitcode_section_not_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
|
|
|
|
sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
|
|
|
|
switch (Type) {
|
|
|
|
case sys::fs::file_magic::bitcode:
|
|
|
|
return Object;
|
|
|
|
case sys::fs::file_magic::elf_relocatable:
|
|
|
|
case sys::fs::file_magic::macho_object:
|
|
|
|
case sys::fs::file_magic::coff_object: {
|
|
|
|
ErrorOr<std::unique_ptr<ObjectFile>> ObjFile =
|
|
|
|
ObjectFile::createObjectFile(Object, Type);
|
|
|
|
if (!ObjFile)
|
|
|
|
return ObjFile.getError();
|
|
|
|
return findBitcodeInObject(*ObjFile->get());
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return object_error::invalid_file_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 17:59:23 +00:00
|
|
|
ErrorOr<std::unique_ptr<IRObjectFile>>
|
2014-12-09 20:36:13 +00:00
|
|
|
llvm::object::IRObjectFile::create(MemoryBufferRef Object,
|
|
|
|
LLVMContext &Context) {
|
2014-09-18 21:28:49 +00:00
|
|
|
ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
|
|
|
|
if (!BCOrErr)
|
|
|
|
return BCOrErr.getError();
|
2014-08-19 18:44:46 +00:00
|
|
|
|
2014-09-18 21:28:49 +00:00
|
|
|
std::unique_ptr<MemoryBuffer> Buff(
|
|
|
|
MemoryBuffer::getMemBuffer(BCOrErr.get(), false));
|
2014-08-19 18:44:46 +00:00
|
|
|
|
2014-09-03 17:31:46 +00:00
|
|
|
ErrorOr<Module *> MOrErr = getLazyBitcodeModule(std::move(Buff), Context);
|
2014-07-04 18:40:36 +00:00
|
|
|
if (std::error_code EC = MOrErr.getError())
|
2014-02-21 20:10:59 +00:00
|
|
|
return EC;
|
2014-07-04 18:40:36 +00:00
|
|
|
|
|
|
|
std::unique_ptr<Module> M(MOrErr.get());
|
2014-09-03 17:59:23 +00:00
|
|
|
return llvm::make_unique<IRObjectFile>(Object, std::move(M));
|
2014-02-21 20:10:59 +00:00
|
|
|
}
|