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