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/Module.h" 17 #include "llvm/Object/IRObjectFile.h" 18 using namespace llvm; 19 using namespace object; 20 21 IRObjectFile::IRObjectFile(MemoryBuffer *Object, error_code &EC, 22 LLVMContext &Context, bool BufferOwned) 23 : SymbolicFile(Binary::ID_IR, Object, BufferOwned) { 24 ErrorOr<Module*> MOrErr = parseBitcodeFile(Object, Context); 25 if ((EC = MOrErr.getError())) 26 return; 27 28 M.reset(MOrErr.get()); 29 } 30 31 static const GlobalValue &getGV(DataRefImpl &Symb) { 32 return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3)); 33 } 34 35 static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) { 36 if (I == M.alias_end()) 37 return 3; 38 const GlobalValue *GV = &*I; 39 return reinterpret_cast<uintptr_t>(GV) | 2; 40 } 41 42 static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) { 43 if (I == M.global_end()) 44 return skipEmpty(M.alias_begin(), M); 45 const GlobalValue *GV = &*I; 46 return reinterpret_cast<uintptr_t>(GV) | 1; 47 } 48 49 static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) { 50 if (I == M.end()) 51 return skipEmpty(M.global_begin(), M); 52 const GlobalValue *GV = &*I; 53 return reinterpret_cast<uintptr_t>(GV) | 0; 54 } 55 56 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const { 57 const GlobalValue *GV = &getGV(Symb); 58 const Module &M = *GV->getParent(); 59 uintptr_t Res; 60 switch (Symb.p & 3) { 61 case 0: { 62 Module::const_iterator Iter(static_cast<const Function*>(GV)); 63 ++Iter; 64 Res = skipEmpty(Iter, M); 65 break; 66 } 67 case 1: { 68 Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV)); 69 ++Iter; 70 Res = skipEmpty(Iter, M); 71 break; 72 } 73 case 2: { 74 Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV)); 75 ++Iter; 76 Res = skipEmpty(Iter, M); 77 break; 78 } 79 case 3: 80 llvm_unreachable("Invalid symbol reference"); 81 } 82 83 Symb.p = Res; 84 } 85 86 error_code IRObjectFile::printSymbolName(raw_ostream &OS, 87 DataRefImpl Symb) const { 88 // FIXME: This should use the Mangler. 89 const GlobalValue &GV = getGV(Symb); 90 OS << GV.getName(); 91 return object_error::success; 92 } 93 94 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { 95 const GlobalValue &GV = getGV(Symb); 96 97 uint32_t Res = BasicSymbolRef::SF_None; 98 if (GV.isDeclaration() || GV.hasAvailableExternallyLinkage()) 99 Res |= BasicSymbolRef::SF_Undefined; 100 if (GV.hasPrivateLinkage() || GV.hasLinkerPrivateLinkage() || 101 GV.hasLinkerPrivateWeakLinkage()) 102 Res |= BasicSymbolRef::SF_FormatSpecific; 103 if (!GV.hasLocalLinkage()) 104 Res |= BasicSymbolRef::SF_Global; 105 if (GV.hasCommonLinkage()) 106 Res |= BasicSymbolRef::SF_Common; 107 if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage()) 108 Res |= BasicSymbolRef::SF_Weak; 109 110 return Res; 111 } 112 113 const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const { 114 const GlobalValue &GV = getGV(Symb); 115 return GV; 116 } 117 118 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const { 119 Module::const_iterator I = M->begin(); 120 DataRefImpl Ret; 121 Ret.p = skipEmpty(I, *M); 122 return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 123 } 124 125 basic_symbol_iterator IRObjectFile::symbol_end_impl() const { 126 DataRefImpl Ret; 127 Ret.p = 3; 128 return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 129 } 130 131 ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile( 132 MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) { 133 error_code EC; 134 OwningPtr<IRObjectFile> Ret( 135 new IRObjectFile(Object, EC, Context, BufferOwned)); 136 if (EC) 137 return EC; 138 return Ret.take(); 139 } 140