1 //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Part of the IRObjectFile class implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/IRObjectFile.h"
15 #include "RecordStreamer.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
18 #include "llvm/IR/GVMaterializer.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCObjectFileInfo.h"
26 #include "llvm/MC/MCParser/MCAsmParser.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCSubtargetInfo.h"
29 #include "llvm/MC/MCTargetAsmParser.h"
30 #include "llvm/Object/ObjectFile.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/raw_ostream.h"
36 using namespace object;
38 IRObjectFile::IRObjectFile(MemoryBufferRef Object, std::unique_ptr<Module> Mod)
39 : SymbolicFile(Binary::ID_IR, Object), M(std::move(Mod)) {
40 // Setup a mangler with the DataLayout.
41 const DataLayout &DL = M->getDataLayout();
42 Mang.reset(new Mangler(&DL));
44 const std::string &InlineAsm = M->getModuleInlineAsm();
45 if (InlineAsm.empty())
48 Triple TT(M->getTargetTriple());
50 const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
54 std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
58 std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str()));
62 std::unique_ptr<MCSubtargetInfo> STI(
63 T->createMCSubtargetInfo(TT.str(), "", ""));
67 std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
71 MCObjectFileInfo MOFI;
72 MCContext MCCtx(MAI.get(), MRI.get(), &MOFI);
73 MOFI.InitMCObjectFileInfo(TT, Reloc::Default, CodeModel::Default, MCCtx);
74 std::unique_ptr<RecordStreamer> Streamer(new RecordStreamer(MCCtx));
75 T->createNullTargetStreamer(*Streamer);
77 std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
79 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
80 std::unique_ptr<MCAsmParser> Parser(
81 createMCAsmParser(SrcMgr, MCCtx, *Streamer, *MAI));
83 MCTargetOptions MCOptions;
84 std::unique_ptr<MCTargetAsmParser> TAP(
85 T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
89 Parser->setTargetParser(*TAP);
90 if (Parser->Run(false))
93 for (auto &KV : *Streamer) {
94 StringRef Key = KV.first();
95 RecordStreamer::State Value = KV.second;
96 uint32_t Res = BasicSymbolRef::SF_None;
98 case RecordStreamer::NeverSeen:
99 llvm_unreachable("foo");
100 case RecordStreamer::DefinedGlobal:
101 Res |= BasicSymbolRef::SF_Global;
103 case RecordStreamer::Defined:
105 case RecordStreamer::Global:
106 case RecordStreamer::Used:
107 Res |= BasicSymbolRef::SF_Undefined;
108 Res |= BasicSymbolRef::SF_Global;
111 AsmSymbols.push_back(
112 std::make_pair<std::string, uint32_t>(Key, std::move(Res)));
116 IRObjectFile::~IRObjectFile() {
119 static GlobalValue *getGV(DataRefImpl &Symb) {
120 if ((Symb.p & 3) == 3)
123 return reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
126 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
127 if (I == M.alias_end())
129 const GlobalValue *GV = &*I;
130 return reinterpret_cast<uintptr_t>(GV) | 2;
133 static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
134 if (I == M.global_end())
135 return skipEmpty(M.alias_begin(), M);
136 const GlobalValue *GV = &*I;
137 return reinterpret_cast<uintptr_t>(GV) | 1;
140 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
142 return skipEmpty(M.global_begin(), M);
143 const GlobalValue *GV = &*I;
144 return reinterpret_cast<uintptr_t>(GV) | 0;
147 static unsigned getAsmSymIndex(DataRefImpl Symb) {
148 assert((Symb.p & uintptr_t(3)) == 3);
149 uintptr_t Index = Symb.p & ~uintptr_t(3);
154 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
155 const GlobalValue *GV = getGV(Symb);
158 switch (Symb.p & 3) {
160 Module::const_iterator Iter(static_cast<const Function*>(GV));
162 Res = skipEmpty(Iter, *M);
166 Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
168 Res = skipEmpty(Iter, *M);
172 Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
174 Res = skipEmpty(Iter, *M);
178 unsigned Index = getAsmSymIndex(Symb);
179 assert(Index < AsmSymbols.size());
181 Res = (Index << 2) | 3;
185 llvm_unreachable("unreachable case");
191 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
192 DataRefImpl Symb) const {
193 const GlobalValue *GV = getGV(Symb);
195 unsigned Index = getAsmSymIndex(Symb);
196 assert(Index <= AsmSymbols.size());
197 OS << AsmSymbols[Index].first;
198 return std::error_code();
201 if (GV->hasDLLImportStorageClass())
205 Mang->getNameWithPrefix(OS, GV, false);
209 return std::error_code();
212 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
213 const GlobalValue *GV = getGV(Symb);
216 unsigned Index = getAsmSymIndex(Symb);
217 assert(Index <= AsmSymbols.size());
218 return AsmSymbols[Index].second;
221 uint32_t Res = BasicSymbolRef::SF_None;
222 if (GV->isDeclarationForLinker())
223 Res |= BasicSymbolRef::SF_Undefined;
224 if (GV->hasPrivateLinkage())
225 Res |= BasicSymbolRef::SF_FormatSpecific;
226 if (!GV->hasLocalLinkage())
227 Res |= BasicSymbolRef::SF_Global;
228 if (GV->hasCommonLinkage())
229 Res |= BasicSymbolRef::SF_Common;
230 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage())
231 Res |= BasicSymbolRef::SF_Weak;
233 if (GV->getName().startswith("llvm."))
234 Res |= BasicSymbolRef::SF_FormatSpecific;
235 else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
236 if (Var->getSection() == StringRef("llvm.metadata"))
237 Res |= BasicSymbolRef::SF_FormatSpecific;
243 GlobalValue *IRObjectFile::getSymbolGV(DataRefImpl Symb) { return getGV(Symb); }
245 std::unique_ptr<Module> IRObjectFile::takeModule() { return std::move(M); }
247 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
248 Module::const_iterator I = M->begin();
250 Ret.p = skipEmpty(I, *M);
251 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
254 basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
256 uint64_t NumAsm = AsmSymbols.size();
259 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
262 ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
263 for (const SectionRef &Sec : Obj.sections()) {
265 if (std::error_code EC = Sec.getName(SecName))
267 if (SecName == ".llvmbc") {
268 StringRef SecContents;
269 if (std::error_code EC = Sec.getContents(SecContents))
271 return MemoryBufferRef(SecContents, Obj.getFileName());
275 return object_error::bitcode_section_not_found;
278 ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
279 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
281 case sys::fs::file_magic::bitcode:
283 case sys::fs::file_magic::elf_relocatable:
284 case sys::fs::file_magic::macho_object:
285 case sys::fs::file_magic::coff_object: {
286 ErrorOr<std::unique_ptr<ObjectFile>> ObjFile =
287 ObjectFile::createObjectFile(Object, Type);
289 return ObjFile.getError();
290 return findBitcodeInObject(*ObjFile->get());
293 return object_error::invalid_file_type;
297 ErrorOr<std::unique_ptr<IRObjectFile>>
298 llvm::object::IRObjectFile::create(MemoryBufferRef Object,
299 LLVMContext &Context) {
300 ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
302 return BCOrErr.getError();
304 std::unique_ptr<MemoryBuffer> Buff(
305 MemoryBuffer::getMemBuffer(BCOrErr.get(), false));
307 ErrorOr<std::unique_ptr<Module>> MOrErr =
308 getLazyBitcodeModule(std::move(Buff), Context, nullptr,
309 /*ShouldLazyLoadMetadata*/ true);
310 if (std::error_code EC = MOrErr.getError())
313 std::unique_ptr<Module> &M = MOrErr.get();
314 return llvm::make_unique<IRObjectFile>(Object, std::move(M));