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 using std::error_code; 23 24 IRObjectFile::IRObjectFile(MemoryBuffer *Object, error_code &EC, 25 LLVMContext &Context, bool BufferOwned) 26 : SymbolicFile(Binary::ID_IR, Object, BufferOwned) { 27 ErrorOr<Module*> MOrErr = parseBitcodeFile(Object, Context); 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 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 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { 109 const GlobalValue &GV = getGV(Symb); 110 111 uint32_t Res = BasicSymbolRef::SF_None; 112 if (GV.isDeclaration() || GV.hasAvailableExternallyLinkage()) 113 Res |= BasicSymbolRef::SF_Undefined; 114 if (GV.hasPrivateLinkage()) 115 Res |= BasicSymbolRef::SF_FormatSpecific; 116 if (!GV.hasLocalLinkage()) 117 Res |= BasicSymbolRef::SF_Global; 118 if (GV.hasCommonLinkage()) 119 Res |= BasicSymbolRef::SF_Common; 120 if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage()) 121 Res |= BasicSymbolRef::SF_Weak; 122 123 return Res; 124 } 125 126 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const { 127 const GlobalValue &GV = getGV(Symb); 128 return GV; 129 } 130 131 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const { 132 Module::const_iterator I = M->begin(); 133 DataRefImpl Ret; 134 Ret.p = skipEmpty(I, *M); 135 return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 136 } 137 138 basic_symbol_iterator IRObjectFile::symbol_end_impl() const { 139 DataRefImpl Ret; 140 Ret.p = 3; 141 return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 142 } 143 144 ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile( 145 MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) { 146 error_code EC; 147 std::unique_ptr<IRObjectFile> Ret( 148 new IRObjectFile(Object, EC, Context, BufferOwned)); 149 if (EC) 150 return EC; 151 return Ret.release(); 152 } 153