1 //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Part of the IRObjectFile class implementation. 11 // 12 //===----------------------------------------------------------------------===// 13 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" 20 using namespace llvm; 21 using namespace object; 22 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())) 29 return; 30 31 M.reset(MOrErr.get()); 32 33 // If we have a DataLayout, setup a mangler. 34 const DataLayout *DL = M->getDataLayout(); 35 if (!DL) 36 return; 37 38 Mang.reset(new Mangler(DL)); 39 } 40 41 static const GlobalValue &getGV(DataRefImpl &Symb) { 42 return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3)); 43 } 44 45 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) { 46 if (I == M.alias_end()) 47 return 3; 48 const GlobalValue *GV = &*I; 49 return reinterpret_cast<uintptr_t>(GV) | 2; 50 } 51 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; 57 } 58 59 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) { 60 if (I == M.end()) 61 return skipEmpty(M.global_begin(), M); 62 const GlobalValue *GV = &*I; 63 return reinterpret_cast<uintptr_t>(GV) | 0; 64 } 65 66 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const { 67 const GlobalValue *GV = &getGV(Symb); 68 const Module &M = *GV->getParent(); 69 uintptr_t Res; 70 switch (Symb.p & 3) { 71 case 0: { 72 Module::const_iterator Iter(static_cast<const Function*>(GV)); 73 ++Iter; 74 Res = skipEmpty(Iter, M); 75 break; 76 } 77 case 1: { 78 Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV)); 79 ++Iter; 80 Res = skipEmpty(Iter, M); 81 break; 82 } 83 case 2: { 84 Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV)); 85 ++Iter; 86 Res = skipEmpty(Iter, M); 87 break; 88 } 89 case 3: 90 llvm_unreachable("Invalid symbol reference"); 91 } 92 93 Symb.p = Res; 94 } 95 96 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS, 97 DataRefImpl Symb) const { 98 const GlobalValue &GV = getGV(Symb); 99 100 if (Mang) 101 Mang->getNameWithPrefix(OS, &GV, false); 102 else 103 OS << GV.getName(); 104 105 return object_error::success; 106 } 107 108 static bool isDeclaration(const GlobalValue &V) { 109 if (V.hasAvailableExternallyLinkage()) 110 return true; 111 112 if (V.isMaterializable()) 113 return false; 114 115 return V.isDeclaration(); 116 } 117 118 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { 119 const GlobalValue &GV = getGV(Symb); 120 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; 132 133 return Res; 134 } 135 136 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const { 137 const GlobalValue &GV = getGV(Symb); 138 return GV; 139 } 140 141 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const { 142 Module::const_iterator I = M->begin(); 143 DataRefImpl Ret; 144 Ret.p = skipEmpty(I, *M); 145 return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 146 } 147 148 basic_symbol_iterator IRObjectFile::symbol_end_impl() const { 149 DataRefImpl Ret; 150 Ret.p = 3; 151 return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 152 } 153 154 ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile( 155 MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) { 156 std::error_code EC; 157 std::unique_ptr<IRObjectFile> Ret( 158 new IRObjectFile(Object, EC, Context, BufferOwned)); 159 if (EC) 160 return EC; 161 return Ret.release(); 162 } 163