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/Mangler.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Object/IRObjectFile.h"
19 #include "llvm/Support/raw_ostream.h"
21 using namespace object;
23 IRObjectFile::IRObjectFile(MemoryBuffer *Object, std::error_code &EC,
24 LLVMContext &Context, bool BufferOwned)
25 : SymbolicFile(Binary::ID_IR, Object, BufferOwned) {
26 ErrorOr<Module *> MOrErr =
27 getLazyBitcodeModule(Object, Context, /*BufferOwned*/ false);
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 static const GlobalValue &getGV(DataRefImpl &Symb) {
42 return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
45 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
46 if (I == M.alias_end())
48 const GlobalValue *GV = &*I;
49 return reinterpret_cast<uintptr_t>(GV) | 2;
52 static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
53 if (I == M.global_end())
54 return skipEmpty(M.alias_begin(), M);
55 const GlobalValue *GV = &*I;
56 return reinterpret_cast<uintptr_t>(GV) | 1;
59 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
61 return skipEmpty(M.global_begin(), M);
62 const GlobalValue *GV = &*I;
63 return reinterpret_cast<uintptr_t>(GV) | 0;
66 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
67 const GlobalValue *GV = &getGV(Symb);
68 const Module &M = *GV->getParent();
72 Module::const_iterator Iter(static_cast<const Function*>(GV));
74 Res = skipEmpty(Iter, M);
78 Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
80 Res = skipEmpty(Iter, M);
84 Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
86 Res = skipEmpty(Iter, M);
90 llvm_unreachable("Invalid symbol reference");
96 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
97 DataRefImpl Symb) const {
98 const GlobalValue &GV = getGV(Symb);
101 Mang->getNameWithPrefix(OS, &GV, false);
105 return object_error::success;
108 static bool isDeclaration(const GlobalValue &V) {
109 if (V.hasAvailableExternallyLinkage())
112 if (V.isMaterializable())
115 return V.isDeclaration();
118 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
119 const GlobalValue &GV = getGV(Symb);
121 uint32_t Res = BasicSymbolRef::SF_None;
122 if (isDeclaration(GV))
123 Res |= BasicSymbolRef::SF_Undefined;
124 if (GV.hasPrivateLinkage())
125 Res |= BasicSymbolRef::SF_FormatSpecific;
126 if (!GV.hasLocalLinkage())
127 Res |= BasicSymbolRef::SF_Global;
128 if (GV.hasCommonLinkage())
129 Res |= BasicSymbolRef::SF_Common;
130 if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
131 Res |= BasicSymbolRef::SF_Weak;
136 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
137 const GlobalValue &GV = getGV(Symb);
141 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
142 Module::const_iterator I = M->begin();
144 Ret.p = skipEmpty(I, *M);
145 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
148 basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
151 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
154 ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
155 MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
157 std::unique_ptr<IRObjectFile> Ret(
158 new IRObjectFile(Object, EC, Context, BufferOwned));
161 return Ret.release();