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/Bitcode/ReaderWriter.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/GVMaterializer.h"
17 #include "llvm/IR/Mangler.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Object/IRObjectFile.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/raw_ostream.h"
23 using namespace object;
25 IRObjectFile::IRObjectFile(std::unique_ptr<MemoryBuffer> Object,
26 std::error_code &EC, LLVMContext &Context)
27 : SymbolicFile(Binary::ID_IR, std::move(Object)) {
28 ErrorOr<Module *> MOrErr = getLazyBitcodeModule(Data.get(), Context);
29 if ((EC = MOrErr.getError()))
32 M.reset(MOrErr.get());
34 // If we have a DataLayout, setup a mangler.
35 const DataLayout *DL = M->getDataLayout();
39 Mang.reset(new Mangler(DL));
42 IRObjectFile::~IRObjectFile() { M->getMaterializer()->releaseBuffer(); }
44 static const GlobalValue &getGV(DataRefImpl &Symb) {
45 return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
48 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
49 if (I == M.alias_end())
51 const GlobalValue *GV = &*I;
52 return reinterpret_cast<uintptr_t>(GV) | 2;
55 static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
56 if (I == M.global_end())
57 return skipEmpty(M.alias_begin(), M);
58 const GlobalValue *GV = &*I;
59 return reinterpret_cast<uintptr_t>(GV) | 1;
62 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
64 return skipEmpty(M.global_begin(), M);
65 const GlobalValue *GV = &*I;
66 return reinterpret_cast<uintptr_t>(GV) | 0;
69 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
70 const GlobalValue *GV = &getGV(Symb);
71 const Module &M = *GV->getParent();
75 Module::const_iterator Iter(static_cast<const Function*>(GV));
77 Res = skipEmpty(Iter, M);
81 Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
83 Res = skipEmpty(Iter, M);
87 Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
89 Res = skipEmpty(Iter, M);
93 llvm_unreachable("Invalid symbol reference");
99 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
100 DataRefImpl Symb) const {
101 const GlobalValue &GV = getGV(Symb);
104 Mang->getNameWithPrefix(OS, &GV, false);
108 return object_error::success;
111 static bool isDeclaration(const GlobalValue &V) {
112 if (V.hasAvailableExternallyLinkage())
115 if (V.isMaterializable())
118 return V.isDeclaration();
121 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
122 const GlobalValue &GV = getGV(Symb);
124 uint32_t Res = BasicSymbolRef::SF_None;
125 if (isDeclaration(GV))
126 Res |= BasicSymbolRef::SF_Undefined;
127 if (GV.hasPrivateLinkage())
128 Res |= BasicSymbolRef::SF_FormatSpecific;
129 if (!GV.hasLocalLinkage())
130 Res |= BasicSymbolRef::SF_Global;
131 if (GV.hasCommonLinkage())
132 Res |= BasicSymbolRef::SF_Common;
133 if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
134 Res |= BasicSymbolRef::SF_Weak;
139 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
140 const GlobalValue &GV = getGV(Symb);
144 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
145 Module::const_iterator I = M->begin();
147 Ret.p = skipEmpty(I, *M);
148 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
151 basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
154 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
157 ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
158 std::unique_ptr<MemoryBuffer> Object, LLVMContext &Context) {
160 std::unique_ptr<IRObjectFile> Ret(
161 new IRObjectFile(std::move(Object), EC, Context));
164 return Ret.release();