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