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 | |
| 14 | #ifndef LLVM_RUNTIME_DYLD_ELF_H |
| 15 | #define LLVM_RUNTIME_DYLD_ELF_H |
| 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 | |
| 24 | namespace { |
| 25 | // Helper for extensive error checking in debug builds. |
| 26 | error_code Check(error_code Err) { |
| 27 | if (Err) { |
| 28 | report_fatal_error(Err.message()); |
| 29 | } |
| 30 | return Err; |
| 31 | } |
| 32 | } // end anonymous namespace |
| 33 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 34 | class RuntimeDyldELF : public RuntimeDyldImpl { |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 35 | void resolveRelocation(const SectionEntry &Section, |
| 36 | uint64_t Offset, |
| 37 | uint64_t Value, |
| 38 | uint32_t Type, |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 39 | int64_t Addend, |
| 40 | uint64_t SymOffset=0); |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 41 | |
Andrew Kaylor | fb05a50 | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 42 | void resolveX86_64Relocation(const SectionEntry &Section, |
| 43 | uint64_t Offset, |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 44 | uint64_t Value, |
| 45 | uint32_t Type, |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 46 | int64_t Addend, |
| 47 | uint64_t SymOffset); |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 48 | |
Andrew Kaylor | fb05a50 | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 49 | void resolveX86Relocation(const SectionEntry &Section, |
| 50 | uint64_t Offset, |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 51 | uint32_t Value, |
| 52 | uint32_t Type, |
| 53 | int32_t Addend); |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 54 | |
Tim Northover | fa1b2f8 | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 55 | void resolveAArch64Relocation(const SectionEntry &Section, |
| 56 | uint64_t Offset, |
| 57 | uint64_t Value, |
| 58 | uint32_t Type, |
| 59 | int64_t Addend); |
| 60 | |
Andrew Kaylor | fb05a50 | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 61 | void resolveARMRelocation(const SectionEntry &Section, |
| 62 | uint64_t Offset, |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 63 | uint32_t Value, |
| 64 | uint32_t Type, |
| 65 | int32_t Addend); |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 66 | |
Andrew Kaylor | fb05a50 | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 67 | void resolveMIPSRelocation(const SectionEntry &Section, |
| 68 | uint64_t Offset, |
Akira Hatanaka | 11dfbe1 | 2012-08-20 17:53:24 +0000 | [diff] [blame] | 69 | uint32_t Value, |
| 70 | uint32_t Type, |
| 71 | int32_t Addend); |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 72 | |
Andrew Kaylor | fb05a50 | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 73 | void resolvePPC64Relocation(const SectionEntry &Section, |
| 74 | uint64_t Offset, |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 75 | uint64_t Value, |
| 76 | uint32_t Type, |
| 77 | int64_t Addend); |
| 78 | |
Richard Sandiford | ca04408 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 79 | void resolveSystemZRelocation(const SectionEntry &Section, |
| 80 | uint64_t Offset, |
| 81 | uint64_t Value, |
| 82 | uint32_t Type, |
| 83 | int64_t Addend); |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 84 | |
Andrew Kaylor | 2ba21c5 | 2013-10-15 21:32:56 +0000 | [diff] [blame] | 85 | unsigned getMaxStubSize() { |
| 86 | if (Arch == Triple::aarch64) |
| 87 | return 20; // movz; movk; movk; movk; br |
| 88 | if (Arch == Triple::arm || Arch == Triple::thumb) |
| 89 | return 8; // 32-bit instruction and 32-bit address |
| 90 | else if (Arch == Triple::mipsel || Arch == Triple::mips) |
| 91 | return 16; |
| 92 | else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) |
| 93 | return 44; |
| 94 | else if (Arch == Triple::x86_64) |
| 95 | return 6; // 2-byte jmp instruction + 32-bit relative address |
| 96 | else if (Arch == Triple::systemz) |
| 97 | return 16; |
| 98 | else |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | unsigned getStubAlignment() { |
| 103 | if (Arch == Triple::systemz) |
| 104 | return 8; |
| 105 | else |
| 106 | return 1; |
| 107 | } |
| 108 | |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 109 | uint64_t findPPC64TOC() const; |
| 110 | void findOPDEntrySection(ObjectImage &Obj, |
| 111 | ObjSectionToIDMap &LocalSections, |
| 112 | RelocationValueRef &Rel); |
| 113 | |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 114 | uint64_t findGOTEntry(uint64_t LoadAddr, uint64_t Offset); |
| 115 | size_t getGOTEntrySize(); |
| 116 | |
| 117 | virtual void updateGOTEntries(StringRef Name, uint64_t Addr); |
| 118 | |
Andrew Kaylor | 480dcb3 | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 119 | // Relocation entries for symbols whose position-independant offset is |
| 120 | // updated in a global offset table. |
Andrew Kaylor | 480dcb3 | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 121 | typedef SmallVector<RelocationValueRef, 2> GOTRelocations; |
| 122 | GOTRelocations GOTEntries; // List of entries requiring finalization. |
| 123 | SmallVector<std::pair<SID, GOTRelocations>, 8> GOTs; // Allocated tables. |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 124 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 125 | // When a module is loaded we save the SectionID of the EH frame section |
| 126 | // in a table until we receive a request to register all unregistered |
| 127 | // EH frame sections with the memory manager. |
| 128 | SmallVector<SID, 2> UnregisteredEHFrameSections; |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame^] | 129 | SmallVector<SID, 2> RegisteredEHFrameSections; |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 130 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 131 | public: |
Andrew Kaylor | 480dcb3 | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 132 | RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 133 | {} |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 134 | |
Rafael Espindola | 2b06530 | 2013-04-29 22:06:33 +0000 | [diff] [blame] | 135 | virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value); |
| 136 | virtual void processRelocationRef(unsigned SectionID, |
| 137 | RelocationRef RelI, |
| 138 | ObjectImage &Obj, |
| 139 | ObjSectionToIDMap &ObjSectionToID, |
| 140 | const SymbolTableMap &Symbols, |
| 141 | StubMap &Stubs); |
| 142 | virtual bool isCompatibleFormat(const ObjectBuffer *Buffer) const; |
| 143 | virtual ObjectImage *createObjectImage(ObjectBuffer *InputBuffer); |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 144 | virtual void registerEHFrames(); |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame^] | 145 | virtual void deregisterEHFrames(); |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 146 | virtual void finalizeLoad(ObjSectionToIDMap &SectionMap); |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 147 | virtual ~RuntimeDyldELF(); |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | } // end namespace llvm |
| 151 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 152 | #endif |