2014-02-22 04:10:59 +08: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-05 06:44:18 +08:00
|
|
|
#include "llvm/Object/IRObjectFile.h"
|
2015-03-02 05:28:53 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Magic.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2014-06-24 05:53:12 +08:00
|
|
|
#include "llvm/IR/GVMaterializer.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2014-02-28 10:17:23 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2014-02-22 04:10:59 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-09-19 05:28:49 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2014-06-24 21:56:32 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-07-04 02:59:23 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2014-02-22 04:21:55 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-02-22 04:10:59 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
2016-12-14 04:20:17 +08:00
|
|
|
IRObjectFile::IRObjectFile(MemoryBufferRef Object,
|
|
|
|
std::vector<std::unique_ptr<Module>> Mods)
|
|
|
|
: SymbolicFile(Binary::ID_IR, Object), Mods(std::move(Mods)) {
|
|
|
|
for (auto &M : this->Mods)
|
|
|
|
SymTab.addModule(M.get());
|
2016-04-22 12:28:05 +08:00
|
|
|
}
|
|
|
|
|
2016-12-01 14:51:47 +08:00
|
|
|
IRObjectFile::~IRObjectFile() {}
|
2014-07-04 02:59:23 +08:00
|
|
|
|
2016-12-01 14:51:47 +08:00
|
|
|
static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb) {
|
|
|
|
return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p);
|
2014-02-22 04:10:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
|
2016-12-01 14:51:47 +08:00
|
|
|
Symb.p += sizeof(ModuleSymbolTable::Symbol);
|
2014-02-22 04:10:59 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
|
|
|
|
DataRefImpl Symb) const {
|
2016-12-01 14:51:47 +08:00
|
|
|
SymTab.printSymbolName(OS, getSym(Symb));
|
2015-06-09 23:20:42 +08:00
|
|
|
return std::error_code();
|
2014-02-22 04:10:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
|
2016-12-01 14:51:47 +08:00
|
|
|
return SymTab.getSymbolFlags(getSym(Symb));
|
2014-02-22 04:10:59 +08:00
|
|
|
}
|
|
|
|
|
2016-11-22 11:38:40 +08:00
|
|
|
basic_symbol_iterator IRObjectFile::symbol_begin() const {
|
2014-02-22 04:10:59 +08:00
|
|
|
DataRefImpl Ret;
|
2016-12-01 14:51:47 +08:00
|
|
|
Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data());
|
2014-02-22 04:10:59 +08:00
|
|
|
return basic_symbol_iterator(BasicSymbolRef(Ret, this));
|
|
|
|
}
|
|
|
|
|
2016-11-22 11:38:40 +08:00
|
|
|
basic_symbol_iterator IRObjectFile::symbol_end() const {
|
2014-02-22 04:10:59 +08:00
|
|
|
DataRefImpl Ret;
|
2016-12-01 14:51:47 +08:00
|
|
|
Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data() +
|
|
|
|
SymTab.symbols().size());
|
2014-02-22 04:10:59 +08:00
|
|
|
return basic_symbol_iterator(BasicSymbolRef(Ret, this));
|
|
|
|
}
|
|
|
|
|
2016-12-14 04:20:17 +08:00
|
|
|
StringRef IRObjectFile::getTargetTriple() const {
|
|
|
|
// Each module must have the same target triple, so we arbitrarily access the
|
|
|
|
// first one.
|
|
|
|
return Mods[0]->getTargetTriple();
|
|
|
|
}
|
2016-11-24 09:13:09 +08:00
|
|
|
|
2017-10-12 02:07:18 +08:00
|
|
|
Expected<MemoryBufferRef>
|
|
|
|
IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
|
2014-09-19 05:28:49 +08:00
|
|
|
for (const SectionRef &Sec : Obj.sections()) {
|
2016-03-01 03:40:10 +08:00
|
|
|
if (Sec.isBitcode()) {
|
2014-09-19 05:28:49 +08:00
|
|
|
StringRef SecContents;
|
|
|
|
if (std::error_code EC = Sec.getContents(SecContents))
|
2017-10-12 02:07:18 +08:00
|
|
|
return errorCodeToError(EC);
|
2014-09-19 05:28:49 +08:00
|
|
|
return MemoryBufferRef(SecContents, Obj.getFileName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:07:18 +08:00
|
|
|
return errorCodeToError(object_error::bitcode_section_not_found);
|
2014-09-19 05:28:49 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 02:07:18 +08:00
|
|
|
Expected<MemoryBufferRef>
|
|
|
|
IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
|
2017-06-07 11:48:56 +08:00
|
|
|
file_magic Type = identify_magic(Object.getBuffer());
|
2014-09-19 05:28:49 +08:00
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
case file_magic::bitcode:
|
2014-09-19 05:28:49 +08:00
|
|
|
return Object;
|
2017-06-07 11:48:56 +08:00
|
|
|
case file_magic::elf_relocatable:
|
|
|
|
case file_magic::macho_object:
|
|
|
|
case file_magic::coff_object: {
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<std::unique_ptr<ObjectFile>> ObjFile =
|
2014-09-19 05:28:49 +08:00
|
|
|
ObjectFile::createObjectFile(Object, Type);
|
|
|
|
if (!ObjFile)
|
2017-10-12 02:07:18 +08:00
|
|
|
return ObjFile.takeError();
|
2014-09-19 05:28:49 +08:00
|
|
|
return findBitcodeInObject(*ObjFile->get());
|
|
|
|
}
|
|
|
|
default:
|
2017-10-12 02:07:18 +08:00
|
|
|
return errorCodeToError(object_error::invalid_file_type);
|
2014-09-19 05:28:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<IRObjectFile>>
|
2016-12-14 04:20:17 +08:00
|
|
|
IRObjectFile::create(MemoryBufferRef Object, LLVMContext &Context) {
|
2017-10-12 02:07:18 +08:00
|
|
|
Expected<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
|
2014-09-19 05:28:49 +08:00
|
|
|
if (!BCOrErr)
|
2017-10-12 02:07:18 +08:00
|
|
|
return BCOrErr.takeError();
|
2014-08-20 02:44:46 +08:00
|
|
|
|
2016-12-14 04:20:17 +08:00
|
|
|
Expected<std::vector<BitcodeModule>> BMsOrErr =
|
|
|
|
getBitcodeModuleList(*BCOrErr);
|
|
|
|
if (!BMsOrErr)
|
|
|
|
return BMsOrErr.takeError();
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<Module>> Mods;
|
|
|
|
for (auto BM : *BMsOrErr) {
|
|
|
|
Expected<std::unique_ptr<Module>> MOrErr =
|
2016-12-17 05:25:01 +08:00
|
|
|
BM.getLazyModule(Context, /*ShouldLazyLoadMetadata*/ true,
|
|
|
|
/*IsImporting*/ false);
|
2016-12-14 04:20:17 +08:00
|
|
|
if (!MOrErr)
|
|
|
|
return MOrErr.takeError();
|
|
|
|
|
|
|
|
Mods.push_back(std::move(*MOrErr));
|
|
|
|
}
|
2014-07-05 02:40:36 +08:00
|
|
|
|
2016-12-14 04:10:22 +08:00
|
|
|
return std::unique_ptr<IRObjectFile>(
|
2016-12-14 04:20:17 +08:00
|
|
|
new IRObjectFile(*BCOrErr, std::move(Mods)));
|
2014-02-22 04:10:59 +08:00
|
|
|
}
|
2017-06-08 09:26:14 +08:00
|
|
|
|
|
|
|
Expected<IRSymtabFile> object::readIRSymtab(MemoryBufferRef MBRef) {
|
|
|
|
IRSymtabFile F;
|
2017-10-12 02:07:18 +08:00
|
|
|
Expected<MemoryBufferRef> BCOrErr =
|
2017-06-08 09:26:14 +08:00
|
|
|
IRObjectFile::findBitcodeInMemBuffer(MBRef);
|
|
|
|
if (!BCOrErr)
|
2017-10-12 02:07:18 +08:00
|
|
|
return BCOrErr.takeError();
|
2017-06-08 09:26:14 +08:00
|
|
|
|
2017-06-09 06:00:24 +08:00
|
|
|
Expected<BitcodeFileContents> BFCOrErr = getBitcodeFileContents(*BCOrErr);
|
|
|
|
if (!BFCOrErr)
|
|
|
|
return BFCOrErr.takeError();
|
2017-06-08 09:26:14 +08:00
|
|
|
|
2017-06-09 06:00:24 +08:00
|
|
|
Expected<irsymtab::FileContents> FCOrErr = irsymtab::readBitcode(*BFCOrErr);
|
2017-06-08 09:26:14 +08:00
|
|
|
if (!FCOrErr)
|
|
|
|
return FCOrErr.takeError();
|
|
|
|
|
2017-06-09 06:00:24 +08:00
|
|
|
F.Mods = std::move(BFCOrErr->Mods);
|
2017-06-08 09:26:14 +08:00
|
|
|
F.Symtab = std::move(FCOrErr->Symtab);
|
|
|
|
F.Strtab = std::move(FCOrErr->Strtab);
|
|
|
|
F.TheReader = std::move(FCOrErr->TheReader);
|
|
|
|
return std::move(F);
|
|
|
|
}
|