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"
|
|
|
|
#include "RecordStreamer.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-07-04 02:59:23 +08: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"
|
2016-01-27 18:01:28 +08:00
|
|
|
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2015-05-16 06:19:42 +08:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.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/SourceMgr.h"
|
|
|
|
#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
|
|
|
|
2014-09-19 05:28:49 +08:00
|
|
|
ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
|
|
|
|
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))
|
|
|
|
return EC;
|
|
|
|
return MemoryBufferRef(SecContents, Obj.getFileName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return object_error::bitcode_section_not_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<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)
|
2016-04-07 06:14:09 +08:00
|
|
|
return errorToErrorCode(ObjFile.takeError());
|
2014-09-19 05:28:49 +08:00
|
|
|
return findBitcodeInObject(*ObjFile->get());
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return object_error::invalid_file_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-09-19 05:28:49 +08:00
|
|
|
ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
|
|
|
|
if (!BCOrErr)
|
2016-11-13 15:00:17 +08:00
|
|
|
return errorCodeToError(BCOrErr.getError());
|
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;
|
|
|
|
ErrorOr<MemoryBufferRef> BCOrErr =
|
|
|
|
IRObjectFile::findBitcodeInMemBuffer(MBRef);
|
|
|
|
if (!BCOrErr)
|
|
|
|
return errorCodeToError(BCOrErr.getError());
|
|
|
|
|
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);
|
|
|
|
}
|