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/raw_ostream.h"
22 using namespace object;
24 IRObjectFile::IRObjectFile(MemoryBuffer *Object, std::error_code &EC,
26 : SymbolicFile(Binary::ID_IR, Object) {
27 ErrorOr<Module *> MOrErr = getLazyBitcodeModule(Object, Context);
28 if ((EC = MOrErr.getError()))
31 M.reset(MOrErr.get());
33 // If we have a DataLayout, setup a mangler.
34 const DataLayout *DL = M->getDataLayout();
38 Mang.reset(new Mangler(DL));
41 IRObjectFile::~IRObjectFile() { M->getMaterializer()->releaseBuffer(); }
43 static const GlobalValue &getGV(DataRefImpl &Symb) {
44 return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
47 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
48 if (I == M.alias_end())
50 const GlobalValue *GV = &*I;
51 return reinterpret_cast<uintptr_t>(GV) | 2;
54 static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
55 if (I == M.global_end())
56 return skipEmpty(M.alias_begin(), M);
57 const GlobalValue *GV = &*I;
58 return reinterpret_cast<uintptr_t>(GV) | 1;
61 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
63 return skipEmpty(M.global_begin(), M);
64 const GlobalValue *GV = &*I;
65 return reinterpret_cast<uintptr_t>(GV) | 0;
68 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
69 const GlobalValue *GV = &getGV(Symb);
70 const Module &M = *GV->getParent();
74 Module::const_iterator Iter(static_cast<const Function*>(GV));
76 Res = skipEmpty(Iter, M);
80 Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
82 Res = skipEmpty(Iter, M);
86 Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
88 Res = skipEmpty(Iter, M);
92 llvm_unreachable("Invalid symbol reference");
98 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
99 DataRefImpl Symb) const {
100 const GlobalValue &GV = getGV(Symb);
103 Mang->getNameWithPrefix(OS, &GV, false);
107 return object_error::success;
110 static bool isDeclaration(const GlobalValue &V) {
111 if (V.hasAvailableExternallyLinkage())
114 if (V.isMaterializable())
117 return V.isDeclaration();
120 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
121 const GlobalValue &GV = getGV(Symb);
123 uint32_t Res = BasicSymbolRef::SF_None;
124 if (isDeclaration(GV))
125 Res |= BasicSymbolRef::SF_Undefined;
126 if (GV.hasPrivateLinkage())
127 Res |= BasicSymbolRef::SF_FormatSpecific;
128 if (!GV.hasLocalLinkage())
129 Res |= BasicSymbolRef::SF_Global;
130 if (GV.hasCommonLinkage())
131 Res |= BasicSymbolRef::SF_Common;
132 if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
133 Res |= BasicSymbolRef::SF_Weak;
138 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
139 const GlobalValue &GV = getGV(Symb);
143 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
144 Module::const_iterator I = M->begin();
146 Ret.p = skipEmpty(I, *M);
147 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
150 basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
153 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
156 ErrorOr<SymbolicFile *>
157 llvm::object::SymbolicFile::createIRObjectFile(MemoryBuffer *Object,
158 LLVMContext &Context) {
160 std::unique_ptr<IRObjectFile> Ret(new IRObjectFile(Object, EC, Context));
163 return Ret.release();