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" 16c3f9b5a5SRafael Espindola #include "llvm/IR/GVMaterializer.h" 17a51f0f83SRafael Espindola #include "llvm/IR/Mangler.h" 18f12b8282SRafael Espindola #include "llvm/IR/Module.h" 19f12b8282SRafael Espindola #include "llvm/Object/IRObjectFile.h" 20*2e60ca96SRafael Espindola #include "llvm/Support/MemoryBuffer.h" 2123f04061SRafael Espindola #include "llvm/Support/raw_ostream.h" 22f12b8282SRafael Espindola using namespace llvm; 23f12b8282SRafael Espindola using namespace object; 24f12b8282SRafael Espindola 25*2e60ca96SRafael Espindola IRObjectFile::IRObjectFile(std::unique_ptr<MemoryBuffer> Object, 26*2e60ca96SRafael Espindola std::error_code &EC, LLVMContext &Context) 27*2e60ca96SRafael Espindola : SymbolicFile(Binary::ID_IR, std::move(Object)) { 28*2e60ca96SRafael Espindola ErrorOr<Module *> MOrErr = getLazyBitcodeModule(Data.get(), Context); 29f12b8282SRafael Espindola if ((EC = MOrErr.getError())) 30f12b8282SRafael Espindola return; 31f12b8282SRafael Espindola 32f12b8282SRafael Espindola M.reset(MOrErr.get()); 33a51f0f83SRafael Espindola 34a51f0f83SRafael Espindola // If we have a DataLayout, setup a mangler. 35a51f0f83SRafael Espindola const DataLayout *DL = M->getDataLayout(); 36a51f0f83SRafael Espindola if (!DL) 37a51f0f83SRafael Espindola return; 38a51f0f83SRafael Espindola 39a51f0f83SRafael Espindola Mang.reset(new Mangler(DL)); 40f12b8282SRafael Espindola } 41f12b8282SRafael Espindola 42c3f9b5a5SRafael Espindola IRObjectFile::~IRObjectFile() { M->getMaterializer()->releaseBuffer(); } 43c3f9b5a5SRafael Espindola 44f12b8282SRafael Espindola static const GlobalValue &getGV(DataRefImpl &Symb) { 45f12b8282SRafael Espindola return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3)); 46f12b8282SRafael Espindola } 47f12b8282SRafael Espindola 48f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) { 49f12b8282SRafael Espindola if (I == M.alias_end()) 50f12b8282SRafael Espindola return 3; 51f12b8282SRafael Espindola const GlobalValue *GV = &*I; 52f12b8282SRafael Espindola return reinterpret_cast<uintptr_t>(GV) | 2; 53f12b8282SRafael Espindola } 54f12b8282SRafael Espindola 55f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) { 56f12b8282SRafael Espindola if (I == M.global_end()) 57f12b8282SRafael Espindola return skipEmpty(M.alias_begin(), M); 58f12b8282SRafael Espindola const GlobalValue *GV = &*I; 59f12b8282SRafael Espindola return reinterpret_cast<uintptr_t>(GV) | 1; 60f12b8282SRafael Espindola } 61f12b8282SRafael Espindola 62f12b8282SRafael Espindola static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) { 63f12b8282SRafael Espindola if (I == M.end()) 64f12b8282SRafael Espindola return skipEmpty(M.global_begin(), M); 65f12b8282SRafael Espindola const GlobalValue *GV = &*I; 66f12b8282SRafael Espindola return reinterpret_cast<uintptr_t>(GV) | 0; 67f12b8282SRafael Espindola } 68f12b8282SRafael Espindola 69f12b8282SRafael Espindola void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const { 70f12b8282SRafael Espindola const GlobalValue *GV = &getGV(Symb); 71f12b8282SRafael Espindola const Module &M = *GV->getParent(); 72f12b8282SRafael Espindola uintptr_t Res; 73f12b8282SRafael Espindola switch (Symb.p & 3) { 74f12b8282SRafael Espindola case 0: { 75f12b8282SRafael Espindola Module::const_iterator Iter(static_cast<const Function*>(GV)); 76f12b8282SRafael Espindola ++Iter; 77f12b8282SRafael Espindola Res = skipEmpty(Iter, M); 78f12b8282SRafael Espindola break; 79f12b8282SRafael Espindola } 80f12b8282SRafael Espindola case 1: { 81f12b8282SRafael Espindola Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV)); 82f12b8282SRafael Espindola ++Iter; 83f12b8282SRafael Espindola Res = skipEmpty(Iter, M); 84f12b8282SRafael Espindola break; 85f12b8282SRafael Espindola } 86f12b8282SRafael Espindola case 2: { 87f12b8282SRafael Espindola Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV)); 88f12b8282SRafael Espindola ++Iter; 89f12b8282SRafael Espindola Res = skipEmpty(Iter, M); 90f12b8282SRafael Espindola break; 91f12b8282SRafael Espindola } 92f12b8282SRafael Espindola case 3: 93f12b8282SRafael Espindola llvm_unreachable("Invalid symbol reference"); 94f12b8282SRafael Espindola } 95f12b8282SRafael Espindola 96f12b8282SRafael Espindola Symb.p = Res; 97f12b8282SRafael Espindola } 98f12b8282SRafael Espindola 99db4ed0bdSRafael Espindola std::error_code IRObjectFile::printSymbolName(raw_ostream &OS, 100f12b8282SRafael Espindola DataRefImpl Symb) const { 101f12b8282SRafael Espindola const GlobalValue &GV = getGV(Symb); 102a51f0f83SRafael Espindola 103a51f0f83SRafael Espindola if (Mang) 104a51f0f83SRafael Espindola Mang->getNameWithPrefix(OS, &GV, false); 105a51f0f83SRafael Espindola else 106f12b8282SRafael Espindola OS << GV.getName(); 107a51f0f83SRafael Espindola 108f12b8282SRafael Espindola return object_error::success; 109f12b8282SRafael Espindola } 110f12b8282SRafael Espindola 1118af5cb2cSRafael Espindola static bool isDeclaration(const GlobalValue &V) { 1128af5cb2cSRafael Espindola if (V.hasAvailableExternallyLinkage()) 1138af5cb2cSRafael Espindola return true; 1148af5cb2cSRafael Espindola 1158af5cb2cSRafael Espindola if (V.isMaterializable()) 1168af5cb2cSRafael Espindola return false; 1178af5cb2cSRafael Espindola 1188af5cb2cSRafael Espindola return V.isDeclaration(); 1198af5cb2cSRafael Espindola } 1208af5cb2cSRafael Espindola 121f12b8282SRafael Espindola uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { 122f12b8282SRafael Espindola const GlobalValue &GV = getGV(Symb); 123f12b8282SRafael Espindola 124f12b8282SRafael Espindola uint32_t Res = BasicSymbolRef::SF_None; 1258af5cb2cSRafael Espindola if (isDeclaration(GV)) 126f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Undefined; 1272fb5bc33SRafael Espindola if (GV.hasPrivateLinkage()) 128f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_FormatSpecific; 129f12b8282SRafael Espindola if (!GV.hasLocalLinkage()) 130f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Global; 131f12b8282SRafael Espindola if (GV.hasCommonLinkage()) 132f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Common; 133f12b8282SRafael Espindola if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage()) 134f12b8282SRafael Espindola Res |= BasicSymbolRef::SF_Weak; 135f12b8282SRafael Espindola 136f12b8282SRafael Espindola return Res; 137f12b8282SRafael Espindola } 138f12b8282SRafael Espindola 139f12b8282SRafael Espindola const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const { 140f12b8282SRafael Espindola const GlobalValue &GV = getGV(Symb); 141f12b8282SRafael Espindola return GV; 142f12b8282SRafael Espindola } 143f12b8282SRafael Espindola 144f12b8282SRafael Espindola basic_symbol_iterator IRObjectFile::symbol_begin_impl() const { 145f12b8282SRafael Espindola Module::const_iterator I = M->begin(); 146f12b8282SRafael Espindola DataRefImpl Ret; 147f12b8282SRafael Espindola Ret.p = skipEmpty(I, *M); 148f12b8282SRafael Espindola return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 149f12b8282SRafael Espindola } 150f12b8282SRafael Espindola 151f12b8282SRafael Espindola basic_symbol_iterator IRObjectFile::symbol_end_impl() const { 152f12b8282SRafael Espindola DataRefImpl Ret; 153f12b8282SRafael Espindola Ret.p = 3; 154f12b8282SRafael Espindola return basic_symbol_iterator(BasicSymbolRef(Ret, this)); 155f12b8282SRafael Espindola } 156f12b8282SRafael Espindola 157*2e60ca96SRafael Espindola ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile( 158*2e60ca96SRafael Espindola std::unique_ptr<MemoryBuffer> Object, LLVMContext &Context) { 159db4ed0bdSRafael Espindola std::error_code EC; 160*2e60ca96SRafael Espindola std::unique_ptr<IRObjectFile> Ret( 161*2e60ca96SRafael Espindola new IRObjectFile(std::move(Object), EC, Context)); 162f12b8282SRafael Espindola if (EC) 163f12b8282SRafael Espindola return EC; 16496c9d95fSAhmed Charles return Ret.release(); 165f12b8282SRafael Espindola } 166