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 Mang.reset(new Mangler());
42 const std::string &InlineAsm = M->getModuleInlineAsm();
43 if (InlineAsm.empty())
46 Triple TT(M->getTargetTriple());
48 const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
52 std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
56 std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str()));
60 std::unique_ptr<MCSubtargetInfo> STI(
61 T->createMCSubtargetInfo(TT.str(), "", ""));
65 std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
69 MCObjectFileInfo MOFI;
70 MCContext MCCtx(MAI.get(), MRI.get(), &MOFI);
71 MOFI.InitMCObjectFileInfo(TT, Reloc::Default, CodeModel::Default, MCCtx);
72 std::unique_ptr<RecordStreamer> Streamer(new RecordStreamer(MCCtx));
73 T->createNullTargetStreamer(*Streamer);
75 std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
77 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
78 std::unique_ptr<MCAsmParser> Parser(
79 createMCAsmParser(SrcMgr, MCCtx, *Streamer, *MAI));
81 MCTargetOptions MCOptions;
82 std::unique_ptr<MCTargetAsmParser> TAP(
83 T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
87 Parser->setTargetParser(*TAP);
88 if (Parser->Run(false))
91 for (auto &KV : *Streamer) {
92 StringRef Key = KV.first();
93 RecordStreamer::State Value = KV.second;
94 uint32_t Res = BasicSymbolRef::SF_None;
96 case RecordStreamer::NeverSeen:
97 llvm_unreachable("foo");
98 case RecordStreamer::DefinedGlobal:
99 Res |= BasicSymbolRef::SF_Global;
101 case RecordStreamer::Defined:
103 case RecordStreamer::Global:
104 case RecordStreamer::Used:
105 Res |= BasicSymbolRef::SF_Undefined;
106 Res |= BasicSymbolRef::SF_Global;
109 AsmSymbols.push_back(
110 std::make_pair<std::string, uint32_t>(Key, std::move(Res)));
114 IRObjectFile::~IRObjectFile() {
117 static GlobalValue *getGV(DataRefImpl &Symb) {
118 if ((Symb.p & 3) == 3)
121 return reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
124 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
125 if (I == M.alias_end())
127 const GlobalValue *GV = &*I;
128 return reinterpret_cast<uintptr_t>(GV) | 2;
131 static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
132 if (I == M.global_end())
133 return skipEmpty(M.alias_begin(), M);
134 const GlobalValue *GV = &*I;
135 return reinterpret_cast<uintptr_t>(GV) | 1;
138 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
140 return skipEmpty(M.global_begin(), M);
141 const GlobalValue *GV = &*I;
142 return reinterpret_cast<uintptr_t>(GV) | 0;
145 static unsigned getAsmSymIndex(DataRefImpl Symb) {
146 assert((Symb.p & uintptr_t(3)) == 3);
147 uintptr_t Index = Symb.p & ~uintptr_t(3);
152 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
153 const GlobalValue *GV = getGV(Symb);
156 switch (Symb.p & 3) {
158 Module::const_iterator Iter(static_cast<const Function*>(GV));
160 Res = skipEmpty(Iter, *M);
164 Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
166 Res = skipEmpty(Iter, *M);
170 Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
172 Res = skipEmpty(Iter, *M);
176 unsigned Index = getAsmSymIndex(Symb);
177 assert(Index < AsmSymbols.size());
179 Res = (Index << 2) | 3;
183 llvm_unreachable("unreachable case");
189 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
190 DataRefImpl Symb) const {
191 const GlobalValue *GV = getGV(Symb);
193 unsigned Index = getAsmSymIndex(Symb);
194 assert(Index <= AsmSymbols.size());
195 OS << AsmSymbols[Index].first;
196 return std::error_code();
199 if (GV->hasDLLImportStorageClass())
203 Mang->getNameWithPrefix(OS, GV, false);
207 return std::error_code();
210 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
211 const GlobalValue *GV = getGV(Symb);
214 unsigned Index = getAsmSymIndex(Symb);
215 assert(Index <= AsmSymbols.size());
216 return AsmSymbols[Index].second;
219 uint32_t Res = BasicSymbolRef::SF_None;
220 if (GV->isDeclarationForLinker())
221 Res |= BasicSymbolRef::SF_Undefined;
222 if (GV->hasPrivateLinkage())
223 Res |= BasicSymbolRef::SF_FormatSpecific;
224 if (!GV->hasLocalLinkage())
225 Res |= BasicSymbolRef::SF_Global;
226 if (GV->hasCommonLinkage())
227 Res |= BasicSymbolRef::SF_Common;
228 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage())
229 Res |= BasicSymbolRef::SF_Weak;
231 if (GV->getName().startswith("llvm."))
232 Res |= BasicSymbolRef::SF_FormatSpecific;
233 else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
234 if (Var->getSection() == StringRef("llvm.metadata"))
235 Res |= BasicSymbolRef::SF_FormatSpecific;
241 GlobalValue *IRObjectFile::getSymbolGV(DataRefImpl Symb) { return getGV(Symb); }
243 std::unique_ptr<Module> IRObjectFile::takeModule() { return std::move(M); }
245 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
246 Module::const_iterator I = M->begin();
248 Ret.p = skipEmpty(I, *M);
249 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
252 basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
254 uint64_t NumAsm = AsmSymbols.size();
257 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
260 ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
261 for (const SectionRef &Sec : Obj.sections()) {
263 if (std::error_code EC = Sec.getName(SecName))
265 if (SecName == ".llvmbc") {
266 StringRef SecContents;
267 if (std::error_code EC = Sec.getContents(SecContents))
269 return MemoryBufferRef(SecContents, Obj.getFileName());
273 return object_error::bitcode_section_not_found;
276 ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
277 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
279 case sys::fs::file_magic::bitcode:
281 case sys::fs::file_magic::elf_relocatable:
282 case sys::fs::file_magic::macho_object:
283 case sys::fs::file_magic::coff_object: {
284 ErrorOr<std::unique_ptr<ObjectFile>> ObjFile =
285 ObjectFile::createObjectFile(Object, Type);
287 return ObjFile.getError();
288 return findBitcodeInObject(*ObjFile->get());
291 return object_error::invalid_file_type;
295 ErrorOr<std::unique_ptr<IRObjectFile>>
296 llvm::object::IRObjectFile::create(MemoryBufferRef Object,
297 LLVMContext &Context) {
298 ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
300 return BCOrErr.getError();
302 std::unique_ptr<MemoryBuffer> Buff(
303 MemoryBuffer::getMemBuffer(BCOrErr.get(), false));
305 ErrorOr<std::unique_ptr<Module>> MOrErr =
306 getLazyBitcodeModule(std::move(Buff), Context, nullptr,
307 /*ShouldLazyLoadMetadata*/ true);
308 if (std::error_code EC = MOrErr.getError())
311 std::unique_ptr<Module> &M = MOrErr.get();
312 return llvm::make_unique<IRObjectFile>(Object, std::move(M));