| Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyldELF.h - Run-time dynamic linker for MC-JIT ---*- 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 | // ELF support for MC-JIT runtime dynamic linker. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 14 | #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H |
| 15 | #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H |
| Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 16 | |
| 17 | #include "RuntimeDyldImpl.h" |
| Andrew Kaylor | 480dcb3 | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
| Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
| Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 22 | namespace llvm { |
| Reid Kleckner | dafc5d7 | 2016-07-06 16:56:42 +0000 | [diff] [blame] | 23 | namespace object { |
| 24 | class ELFObjectFileBase; |
| 25 | } |
| Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 26 | |
| Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 27 | class RuntimeDyldELF : public RuntimeDyldImpl { |
| Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 28 | |
| Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 29 | void resolveRelocation(const SectionEntry &Section, uint64_t Offset, |
| 30 | uint64_t Value, uint32_t Type, int64_t Addend, |
| Petar Jovanovic | 9720283 | 2015-05-28 13:48:41 +0000 | [diff] [blame] | 31 | uint64_t SymOffset = 0, SID SectionID = 0); |
| Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 32 | |
| Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 33 | void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset, |
| 34 | uint64_t Value, uint32_t Type, int64_t Addend, |
| Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 35 | uint64_t SymOffset); |
| Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 36 | |
| Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 37 | void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset, |
| 38 | uint32_t Value, uint32_t Type, int32_t Addend); |
| Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 39 | |
| Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 40 | void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset, |
| 41 | uint64_t Value, uint32_t Type, int64_t Addend); |
| Tim Northover | fa1b2f8 | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 42 | |
| Eugene Leviant | be2d68f | 2017-01-09 09:56:31 +0000 | [diff] [blame] | 43 | bool resolveAArch64ShortBranch(unsigned SectionID, relocation_iterator RelI, |
| 44 | const RelocationValueRef &Value); |
| 45 | |
| Eugene Leviant | 3e582c8 | 2017-02-06 15:31:28 +0000 | [diff] [blame] | 46 | void resolveAArch64Branch(unsigned SectionID, const RelocationValueRef &Value, |
| 47 | relocation_iterator RelI, StubMap &Stubs); |
| 48 | |
| Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 49 | void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset, |
| 50 | uint32_t Value, uint32_t Type, int32_t Addend); |
| Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 51 | |
| Hal Finkel | 23cdeee | 2015-08-04 15:29:00 +0000 | [diff] [blame] | 52 | void resolvePPC32Relocation(const SectionEntry &Section, uint64_t Offset, |
| 53 | uint64_t Value, uint32_t Type, int64_t Addend); |
| 54 | |
| Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 55 | void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset, |
| 56 | uint64_t Value, uint32_t Type, int64_t Addend); |
| Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 57 | |
| Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 58 | void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset, |
| 59 | uint64_t Value, uint32_t Type, int64_t Addend); |
| Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 60 | |
| Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 61 | unsigned getMaxStubSize() override { |
| Tim Northover | e19bed7 | 2014-07-23 12:32:47 +0000 | [diff] [blame] | 62 | if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) |
| Andrew Kaylor | 2ba21c5 | 2013-10-15 21:32:56 +0000 | [diff] [blame] | 63 | return 20; // movz; movk; movk; movk; br |
| 64 | if (Arch == Triple::arm || Arch == Triple::thumb) |
| 65 | return 8; // 32-bit instruction and 32-bit address |
| Petar Jovanovic | 9720283 | 2015-05-28 13:48:41 +0000 | [diff] [blame] | 66 | else if (IsMipsO32ABI) |
| Andrew Kaylor | 2ba21c5 | 2013-10-15 21:32:56 +0000 | [diff] [blame] | 67 | return 16; |
| 68 | else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) |
| 69 | return 44; |
| 70 | else if (Arch == Triple::x86_64) |
| 71 | return 6; // 2-byte jmp instruction + 32-bit relative address |
| 72 | else if (Arch == Triple::systemz) |
| 73 | return 16; |
| 74 | else |
| 75 | return 0; |
| 76 | } |
| 77 | |
| Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 78 | unsigned getStubAlignment() override { |
| Andrew Kaylor | 2ba21c5 | 2013-10-15 21:32:56 +0000 | [diff] [blame] | 79 | if (Arch == Triple::systemz) |
| 80 | return 8; |
| 81 | else |
| 82 | return 1; |
| 83 | } |
| 84 | |
| Petar Jovanovic | 9720283 | 2015-05-28 13:48:41 +0000 | [diff] [blame] | 85 | void setMipsABI(const ObjectFile &Obj) override; |
| 86 | |
| Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 87 | Error findPPC64TOCSection(const ELFObjectFileBase &Obj, |
| 88 | ObjSectionToIDMap &LocalSections, |
| 89 | RelocationValueRef &Rel); |
| 90 | Error findOPDEntrySection(const ELFObjectFileBase &Obj, |
| 91 | ObjSectionToIDMap &LocalSections, |
| 92 | RelocationValueRef &Rel); |
| Simon Dardis | c97cfb6 | 2016-12-13 11:39:18 +0000 | [diff] [blame] | 93 | protected: |
| Eugene Leviant | 3e582c8 | 2017-02-06 15:31:28 +0000 | [diff] [blame] | 94 | size_t getGOTEntrySize() override; |
| Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 95 | |
| Simon Dardis | c97cfb6 | 2016-12-13 11:39:18 +0000 | [diff] [blame] | 96 | private: |
| Keno Fischer | 02628de | 2015-04-14 02:10:35 +0000 | [diff] [blame] | 97 | SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; } |
| Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 98 | |
| Keno Fischer | 02628de | 2015-04-14 02:10:35 +0000 | [diff] [blame] | 99 | // Allocate no GOT entries for use in the given section. |
| Eugene Leviant | 3e582c8 | 2017-02-06 15:31:28 +0000 | [diff] [blame] | 100 | uint64_t allocateGOTEntries(unsigned no); |
| 101 | |
| 102 | // Find GOT entry corresponding to relocation or create new one. |
| 103 | uint64_t findOrAllocGOTEntry(const RelocationValueRef &Value, |
| 104 | unsigned GOTRelType); |
| Keno Fischer | 02628de | 2015-04-14 02:10:35 +0000 | [diff] [blame] | 105 | |
| 106 | // Resolve the relvative address of GOTOffset in Section ID and place |
| 107 | // it at the given Offset |
| 108 | void resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset, |
| Eugene Leviant | 3e582c8 | 2017-02-06 15:31:28 +0000 | [diff] [blame] | 109 | uint64_t GOTOffset, uint32_t Type); |
| Keno Fischer | 02628de | 2015-04-14 02:10:35 +0000 | [diff] [blame] | 110 | |
| 111 | // For a GOT entry referenced from SectionID, compute a relocation entry |
| 112 | // that will place the final resolved value in the GOT slot |
| Eugene Leviant | 3e582c8 | 2017-02-06 15:31:28 +0000 | [diff] [blame] | 113 | RelocationEntry computeGOTOffsetRE(uint64_t GOTOffset, uint64_t SymbolOffset, |
| Keno Fischer | 02628de | 2015-04-14 02:10:35 +0000 | [diff] [blame] | 114 | unsigned Type); |
| 115 | |
| Keno Fischer | e6892c8 | 2015-05-01 20:21:45 +0000 | [diff] [blame] | 116 | // Compute the address in memory where we can find the placeholder |
| 117 | void *computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const; |
| 118 | |
| 119 | // Split out common case for createing the RelocationEntry for when the relocation requires |
| 120 | // no particular advanced processing. |
| 121 | void processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value); |
| 122 | |
| Petar Jovanovic | d22164d | 2015-08-13 15:12:49 +0000 | [diff] [blame] | 123 | // Return matching *LO16 relocation (Mips specific) |
| 124 | uint32_t getMatchingLoRelocation(uint32_t RelType, |
| 125 | bool IsLocal = false) const; |
| 126 | |
| Keno Fischer | 02628de | 2015-04-14 02:10:35 +0000 | [diff] [blame] | 127 | // The tentative ID for the GOT section |
| 128 | unsigned GOTSectionID; |
| 129 | |
| 130 | // Records the current number of allocated slots in the GOT |
| 131 | // (This would be equivalent to GOTEntries.size() were it not for relocations |
| 132 | // that consume more than one slot) |
| 133 | unsigned CurrentGOTIndex; |
| Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 134 | |
| Simon Dardis | c97cfb6 | 2016-12-13 11:39:18 +0000 | [diff] [blame] | 135 | protected: |
| Petar Jovanovic | 9720283 | 2015-05-28 13:48:41 +0000 | [diff] [blame] | 136 | // A map from section to a GOT section that has entries for section's GOT |
| 137 | // relocations. (Mips64 specific) |
| 138 | DenseMap<SID, SID> SectionToGOTMap; |
| 139 | |
| Simon Dardis | c97cfb6 | 2016-12-13 11:39:18 +0000 | [diff] [blame] | 140 | private: |
| Petar Jovanovic | 9720283 | 2015-05-28 13:48:41 +0000 | [diff] [blame] | 141 | // A map to avoid duplicate got entries (Mips64 specific) |
| 142 | StringMap<uint64_t> GOTSymbolOffsets; |
| 143 | |
| Petar Jovanovic | d22164d | 2015-08-13 15:12:49 +0000 | [diff] [blame] | 144 | // *HI16 relocations will be added for resolving when we find matching |
| 145 | // *LO16 part. (Mips specific) |
| 146 | SmallVector<std::pair<RelocationValueRef, RelocationEntry>, 8> PendingRelocs; |
| 147 | |
| Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 148 | // When a module is loaded we save the SectionID of the EH frame section |
| 149 | // in a table until we receive a request to register all unregistered |
| 150 | // EH frame sections with the memory manager. |
| 151 | SmallVector<SID, 2> UnregisteredEHFrameSections; |
| Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 152 | SmallVector<SID, 2> RegisteredEHFrameSections; |
| Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 153 | |
| Eugene Leviant | 3e582c8 | 2017-02-06 15:31:28 +0000 | [diff] [blame] | 154 | // Map between GOT relocation value and corresponding GOT offset |
| 155 | std::map<RelocationValueRef, uint64_t> GOTOffsetMap; |
| 156 | |
| 157 | bool relocationNeedsGot(const RelocationRef &R) const override; |
| Sanjoy Das | d5658b0 | 2015-11-23 21:47:51 +0000 | [diff] [blame] | 158 | bool relocationNeedsStub(const RelocationRef &R) const override; |
| 159 | |
| Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 160 | public: |
| Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 161 | RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr, |
| Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 162 | JITSymbolResolver &Resolver); |
| Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 163 | ~RuntimeDyldELF() override; |
| Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 164 | |
| Simon Dardis | c97cfb6 | 2016-12-13 11:39:18 +0000 | [diff] [blame] | 165 | static std::unique_ptr<RuntimeDyldELF> |
| 166 | create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, |
| 167 | JITSymbolResolver &Resolver); |
| 168 | |
| Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 169 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> |
| 170 | loadObject(const object::ObjectFile &O) override; |
| Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 171 | |
| Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 172 | void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override; |
| Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 173 | Expected<relocation_iterator> |
| Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 174 | processRelocationRef(unsigned SectionID, relocation_iterator RelI, |
| Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 175 | const ObjectFile &Obj, |
| 176 | ObjSectionToIDMap &ObjSectionToID, |
| Lang Hames | a5cd950 | 2014-11-27 05:40:13 +0000 | [diff] [blame] | 177 | StubMap &Stubs) override; |
| Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 178 | bool isCompatibleFile(const object::ObjectFile &Obj) const override; |
| Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 179 | void registerEHFrames() override; |
| 180 | void deregisterEHFrames() override; |
| Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 181 | Error finalizeLoad(const ObjectFile &Obj, |
| 182 | ObjSectionToIDMap &SectionMap) override; |
| Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | } // end namespace llvm |
| 186 | |
| Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 187 | #endif |