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 { |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 23 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 24 | class RuntimeDyldELF : public RuntimeDyldImpl { |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 25 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 26 | void resolveRelocation(const SectionEntry &Section, uint64_t Offset, |
| 27 | uint64_t Value, uint32_t Type, int64_t Addend, |
| 28 | uint64_t SymOffset = 0); |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 29 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 30 | void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset, |
| 31 | uint64_t Value, uint32_t Type, int64_t Addend, |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 32 | uint64_t SymOffset); |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 33 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 34 | void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset, |
| 35 | uint32_t Value, uint32_t Type, int32_t Addend); |
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 resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset, |
| 38 | uint64_t Value, uint32_t Type, int64_t Addend); |
Tim Northover | fa1b2f8 | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 39 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 40 | void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset, |
| 41 | uint32_t Value, uint32_t Type, int32_t Addend); |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 42 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 43 | void resolveMIPSRelocation(const SectionEntry &Section, uint64_t Offset, |
| 44 | uint32_t Value, uint32_t Type, int32_t Addend); |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 45 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 46 | void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset, |
| 47 | uint64_t Value, uint32_t Type, int64_t Addend); |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 48 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 49 | void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset, |
| 50 | uint64_t Value, uint32_t Type, int64_t Addend); |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 51 | |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 52 | unsigned getMaxStubSize() override { |
Tim Northover | e19bed7 | 2014-07-23 12:32:47 +0000 | [diff] [blame] | 53 | if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) |
Andrew Kaylor | 2ba21c5 | 2013-10-15 21:32:56 +0000 | [diff] [blame] | 54 | return 20; // movz; movk; movk; movk; br |
| 55 | if (Arch == Triple::arm || Arch == Triple::thumb) |
| 56 | return 8; // 32-bit instruction and 32-bit address |
| 57 | else if (Arch == Triple::mipsel || Arch == Triple::mips) |
| 58 | return 16; |
| 59 | else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) |
| 60 | return 44; |
| 61 | else if (Arch == Triple::x86_64) |
| 62 | return 6; // 2-byte jmp instruction + 32-bit relative address |
| 63 | else if (Arch == Triple::systemz) |
| 64 | return 16; |
| 65 | else |
| 66 | return 0; |
| 67 | } |
| 68 | |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 69 | unsigned getStubAlignment() override { |
Andrew Kaylor | 2ba21c5 | 2013-10-15 21:32:56 +0000 | [diff] [blame] | 70 | if (Arch == Triple::systemz) |
| 71 | return 8; |
| 72 | else |
| 73 | return 1; |
| 74 | } |
| 75 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 76 | void findPPC64TOCSection(const ObjectFile &Obj, |
| 77 | ObjSectionToIDMap &LocalSections, |
Ulrich Weigand | 8f1f87c | 2014-06-27 10:32:14 +0000 | [diff] [blame] | 78 | RelocationValueRef &Rel); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 79 | void findOPDEntrySection(const ObjectFile &Obj, |
| 80 | ObjSectionToIDMap &LocalSections, |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 81 | RelocationValueRef &Rel); |
| 82 | |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 83 | size_t getGOTEntrySize(); |
| 84 | |
Keno Fischer | 02628de | 2015-04-14 02:10:35 +0000 | [diff] [blame^] | 85 | SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; } |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 86 | |
Keno Fischer | 02628de | 2015-04-14 02:10:35 +0000 | [diff] [blame^] | 87 | // Allocate no GOT entries for use in the given section. |
| 88 | uint64_t allocateGOTEntries(unsigned SectionID, unsigned no); |
| 89 | |
| 90 | // Resolve the relvative address of GOTOffset in Section ID and place |
| 91 | // it at the given Offset |
| 92 | void resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset, |
| 93 | uint64_t GOTOffset); |
| 94 | |
| 95 | // For a GOT entry referenced from SectionID, compute a relocation entry |
| 96 | // that will place the final resolved value in the GOT slot |
| 97 | RelocationEntry computeGOTOffsetRE(unsigned SectionID, |
| 98 | uint64_t GOTOffset, |
| 99 | uint64_t SymbolOffset, |
| 100 | unsigned Type); |
| 101 | |
| 102 | // The tentative ID for the GOT section |
| 103 | unsigned GOTSectionID; |
| 104 | |
| 105 | // Records the current number of allocated slots in the GOT |
| 106 | // (This would be equivalent to GOTEntries.size() were it not for relocations |
| 107 | // that consume more than one slot) |
| 108 | unsigned CurrentGOTIndex; |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 109 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 110 | // When a module is loaded we save the SectionID of the EH frame section |
| 111 | // in a table until we receive a request to register all unregistered |
| 112 | // EH frame sections with the memory manager. |
| 113 | SmallVector<SID, 2> UnregisteredEHFrameSections; |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 114 | SmallVector<SID, 2> RegisteredEHFrameSections; |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 115 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 116 | public: |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 117 | RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr, |
| 118 | RuntimeDyld::SymbolResolver &Resolver); |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 119 | ~RuntimeDyldELF() override; |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 120 | |
| 121 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> |
| 122 | loadObject(const object::ObjectFile &O) override; |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 123 | |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 124 | void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override; |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 125 | relocation_iterator |
| 126 | processRelocationRef(unsigned SectionID, relocation_iterator RelI, |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 127 | const ObjectFile &Obj, |
| 128 | ObjSectionToIDMap &ObjSectionToID, |
Lang Hames | a5cd950 | 2014-11-27 05:40:13 +0000 | [diff] [blame] | 129 | StubMap &Stubs) override; |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 130 | bool isCompatibleFile(const object::ObjectFile &Obj) const override; |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 131 | void registerEHFrames() override; |
| 132 | void deregisterEHFrames() override; |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 133 | void finalizeLoad(const ObjectFile &Obj, |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 134 | ObjSectionToIDMap &SectionMap) override; |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } // end namespace llvm |
| 138 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 139 | #endif |