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