1f12b8282SRafael Espindola //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===// 2f12b8282SRafael Espindola // 3f12b8282SRafael Espindola // The LLVM Compiler Infrastructure 4f12b8282SRafael Espindola // 5f12b8282SRafael Espindola // This file is distributed under the University of Illinois Open Source 6f12b8282SRafael Espindola // License. See LICENSE.TXT for details. 7f12b8282SRafael Espindola // 8f12b8282SRafael Espindola //===----------------------------------------------------------------------===// 9f12b8282SRafael Espindola // 10f12b8282SRafael Espindola // Part of the IRObjectFile class implementation. 11f12b8282SRafael Espindola // 12f12b8282SRafael Espindola //===----------------------------------------------------------------------===// 13f12b8282SRafael Espindola 14f12b8282SRafael Espindola #include "llvm/Bitcode/ReaderWriter.h" 15f12b8282SRafael Espindola #include "llvm/IR/LLVMContext.h" 16a51f0f83SRafael Espindola #include "llvm/IR/Mangler.h" 17f12b8282SRafael Espindola #include "llvm/IR/Module.h" 18f12b8282SRafael Espindola #include "llvm/Object/IRObjectFile.h" 1923f04061SRafael Espindola #include "llvm/Support/raw_ostream.h" 20f12b8282SRafael Espindola using namespace llvm; 21f12b8282SRafael Espindola using namespace object; 22f12b8282SRafael Espindola 23*db4ed0bdSRafael Espindola IRObjectFile::IRObjectFile(MemoryBuffer *Object, std::error_code &EC, 24f12b8282SRafael Espindola LLVMContext &Context, bool BufferOwned) 25f12b8282SRafael Espindola : SymbolicFile(Binary::ID_IR, Object, BufferOwned) { 26f12b8282SRafael Espindola ErrorOr<Module*> MOrErr = parseBitcodeFile(Object, Context); 27f12b8282SRafael Espindola if ((EC = MOrErr.getError())) 28f12b8282SRafael Espindola return; 29f12b8282SRafael Espindola 30f12b8282SRafael Espindola M.reset(MOrErr.get()); 31a51f0f83SRafael Espindola 32a51f0f83SRafael Espindola // If we have a DataLayout, setup a mangler. 33a51f0f83SRafael Espindola const DataLayout *DL = M->getDataLayout(); 34a51f0f83SRafael Espindola if (!DL) 35a51f0f83SRafael Espindola return; 36a51f0f83SRafael Espindola 37a51f0f83SRafael Espindola Mang.reset(new Mangler(DL)); 38f12b8282SRafael Espindola } 39f12b8282SRafael Espindola 40f12b8282SRafael Espindola static const GlobalValue &getGV(DataRefImpl &Symb) { 41f12b8282SRafael Espindola return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3)); 42f12b8282SRafael Espindola } 43f12b8282SRafael Espindola 44f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) { 45f12b8282SRafael Espindola if (I == M.alias_end()) 46f12b8282SRafael Espindola return 3; 47f12b8282SRafael Espindola const GlobalValue *GV = &*I; 48f12b8282SRafael Espindola return reinterpret_cast<uintptr_t>(GV) | 2; 49f12b8282SRafael Espindola } 50f12b8282SRafael Espindola 51f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) { 52f12b8282SRafael Espindola if (I == M.global_end()) 53f12b8282SRafael Espindola return skipEmpty(M.alias_begin(), M); 54f12b8282SRafael Espindola const GlobalValue *GV = &*I; 55f12b8282SRafael Espindola return reinterpret_cast<uintptr_t>(GV) | 1; 56f12b8282SRafael Espindola } 57f12b8282SRafael Espindola 58f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) { 59f12b8282SRafael Espindola if (I == M.end()) 60f12b8282SRafael Espindola return skipEmpty(M.global_begin(), M); 61f12b8282SRafael Espindola const GlobalValue *GV = &*I; 62f12b8282SRafael Espindola return reinterpret_cast<uintptr_t>(GV) | 0; 63f12b8282SRafael Espindola } 64f12b8282SRafael Espindola 65f12b8282SRafael Espindola void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const { 66f12b8282SRafael Espindola const GlobalValue *GV = &getGV(Symb); 67f12b8282SRafael Espindola const Module &M = *GV->getParent(); 68f12b8282SRafael Espindola uintptr_t Res; 69f12b8282SRafael Espindola switch (Symb.p & 3) { 70f12b8282SRafael Espindola case 0: { 71f12b8282SRafael Espindola Module::const_iterator Iter(static_cast<const Function*>(GV)); 72f12b8282SRafael Espindola ++Iter; 73f12b8282SRafael Espindola Res = skipEmpty(Iter, M); 74f12b8282SRafael Espindola break; 75f12b8282SRafael Espindola } 76f12b8282SRafael Espindola case 1: { 77f12b8282SRafael Espindola Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV)); 78f12b8282SRafael Espindola ++Iter; 79f12b8282SRafael Espindola Res = skipEmpty(Iter, M); 80f12b8282SRafael Espindola break; 81f12b8282SRafael Espindola } 82f12b8282SRafael Espindola case 2: { 83f12b8282SRafael Espindola Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV)); 84f12b8282SRafael Espindola ++Iter; 85f12b8282SRafael Espindola Res = skipEmpty(Iter, M); 86f12b8282SRafael Espindola break; 87f12b8282SRafael Espindola } 88f12b8282SRafael Espindola case 3: 89f12b8282SRafael Espindola llvm_unreachable("Invalid symbol reference"); 90f12b8282SRafael Espindola } 91f12b8282SRafael Espindola 92f12b8282SRafael Espindola Symb.p = Res; 93f12b8282SRafael Espindola } 94f12b8282SRafael Espindola 95*db4ed0bdSRafael Espindola std::error_code IRObjectFile::printSymbolName(raw_ostream &OS, 96f12b8282SRafael Espindola DataRefImpl Symb) const { 97f12b8282SRafael Espindola const GlobalValue &GV = getGV(Symb); 98a51f0f83SRafael Espindola 99a51f0f83SRafael Espindola if (Mang) 100a51f0f83SRafael Espindola Mang->getNameWithPrefix(OS, &GV, false); 101a51f0f83SRafael Espindola else 102f12b8282SRafael Espindola OS << GV.getName(); 103a51f0f83SRafael Espindola 104f12b8282SRafael Espindola return object_error::success; 105f12b8282SRafael Espindola } 106f12b8282SRafael Espindola 107f12b8282SRafael Espindola uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { 108f12b8282SRafael Espindola const GlobalValue &GV = getGV(Symb); 109f12b8282SRafael Espindola 110f12b8282SRafael Espindola uint32_t Res = BasicSymbolRef::SF_None; 111f12b8282SRafael Espindola if (GV.isDeclaration() || GV.hasAvailableExternallyLinkage()) 112f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Undefined; 1132fb5bc33SRafael Espindola if (GV.hasPrivateLinkage()) 114f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_FormatSpecific; 115f12b8282SRafael Espindola if (!GV.hasLocalLinkage()) 116f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Global; 117f12b8282SRafael Espindola if (GV.hasCommonLinkage()) 118f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Common; 119f12b8282SRafael Espindola if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage()) 120f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Weak; 121f12b8282SRafael Espindola 122f12b8282SRafael Espindola return Res; 123f12b8282SRafael Espindola } 124f12b8282SRafael Espindola 125f12b8282SRafael Espindola const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const { 126f12b8282SRafael Espindola const GlobalValue &GV = getGV(Symb); 127f12b8282SRafael Espindola return GV; 128f12b8282SRafael Espindola } 129f12b8282SRafael Espindola 130f12b8282SRafael Espindola basic_symbol_iterator IRObjectFile::symbol_begin_impl() const { 131f12b8282SRafael Espindola Module::const_iterator I = M->begin(); 132f12b8282SRafael Espindola DataRefImpl Ret; 133f12b8282SRafael Espindola Ret.p = skipEmpty(I, *M); 134f12b8282SRafael Espindola return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 135f12b8282SRafael Espindola } 136f12b8282SRafael Espindola 137f12b8282SRafael Espindola basic_symbol_iterator IRObjectFile::symbol_end_impl() const { 138f12b8282SRafael Espindola DataRefImpl Ret; 139f12b8282SRafael Espindola Ret.p = 3; 140f12b8282SRafael Espindola return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 141f12b8282SRafael Espindola } 142f12b8282SRafael Espindola 143f12b8282SRafael Espindola ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile( 144f12b8282SRafael Espindola MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) { 145*db4ed0bdSRafael Espindola std::error_code EC; 14656440fd8SAhmed Charles std::unique_ptr<IRObjectFile> Ret( 147f12b8282SRafael Espindola new IRObjectFile(Object, EC, Context, BufferOwned)); 148f12b8282SRafael Espindola if (EC) 149f12b8282SRafael Espindola return EC; 15096c9d95fSAhmed Charles return Ret.release(); 151f12b8282SRafael Espindola } 152