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