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_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H 15 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H 16 17 #include "RuntimeDyldImpl.h" 18 #include "llvm/ADT/DenseMap.h" 19 20 using namespace llvm; 21 22 namespace llvm { 23 namespace object { 24 class ELFObjectFileBase; 25 } 26 27 class RuntimeDyldELF : public RuntimeDyldImpl { 28 29 void resolveRelocation(const SectionEntry &Section, uint64_t Offset, 30 uint64_t Value, uint32_t Type, int64_t Addend, 31 uint64_t SymOffset = 0, SID SectionID = 0); 32 33 void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset, 34 uint64_t Value, uint32_t Type, int64_t Addend, 35 uint64_t SymOffset); 36 37 void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset, 38 uint32_t Value, uint32_t Type, int32_t Addend); 39 40 void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset, 41 uint64_t Value, uint32_t Type, int64_t Addend); 42 43 bool resolveAArch64ShortBranch(unsigned SectionID, relocation_iterator RelI, 44 const RelocationValueRef &Value); 45 46 void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset, 47 uint32_t Value, uint32_t Type, int32_t Addend); 48 49 void resolvePPC32Relocation(const SectionEntry &Section, uint64_t Offset, 50 uint64_t Value, uint32_t Type, int64_t Addend); 51 52 void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset, 53 uint64_t Value, uint32_t Type, int64_t Addend); 54 55 void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset, 56 uint64_t Value, uint32_t Type, int64_t Addend); 57 58 unsigned getMaxStubSize() override { 59 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) 60 return 20; // movz; movk; movk; movk; br 61 if (Arch == Triple::arm || Arch == Triple::thumb) 62 return 8; // 32-bit instruction and 32-bit address 63 else if (IsMipsO32ABI) 64 return 16; 65 else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) 66 return 44; 67 else if (Arch == Triple::x86_64) 68 return 6; // 2-byte jmp instruction + 32-bit relative address 69 else if (Arch == Triple::systemz) 70 return 16; 71 else 72 return 0; 73 } 74 75 unsigned getStubAlignment() override { 76 if (Arch == Triple::systemz) 77 return 8; 78 else 79 return 1; 80 } 81 82 void setMipsABI(const ObjectFile &Obj) override; 83 84 Error findPPC64TOCSection(const ELFObjectFileBase &Obj, 85 ObjSectionToIDMap &LocalSections, 86 RelocationValueRef &Rel); 87 Error findOPDEntrySection(const ELFObjectFileBase &Obj, 88 ObjSectionToIDMap &LocalSections, 89 RelocationValueRef &Rel); 90 protected: 91 size_t getGOTEntrySize(); 92 93 private: 94 SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; } 95 96 // Allocate no GOT entries for use in the given section. 97 uint64_t allocateGOTEntries(unsigned SectionID, unsigned no); 98 99 // Resolve the relvative address of GOTOffset in Section ID and place 100 // it at the given Offset 101 void resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset, 102 uint64_t GOTOffset); 103 104 // For a GOT entry referenced from SectionID, compute a relocation entry 105 // that will place the final resolved value in the GOT slot 106 RelocationEntry computeGOTOffsetRE(unsigned SectionID, 107 uint64_t GOTOffset, 108 uint64_t SymbolOffset, 109 unsigned Type); 110 111 // Compute the address in memory where we can find the placeholder 112 void *computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const; 113 114 // Split out common case for createing the RelocationEntry for when the relocation requires 115 // no particular advanced processing. 116 void processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value); 117 118 // Return matching *LO16 relocation (Mips specific) 119 uint32_t getMatchingLoRelocation(uint32_t RelType, 120 bool IsLocal = false) const; 121 122 // The tentative ID for the GOT section 123 unsigned GOTSectionID; 124 125 // Records the current number of allocated slots in the GOT 126 // (This would be equivalent to GOTEntries.size() were it not for relocations 127 // that consume more than one slot) 128 unsigned CurrentGOTIndex; 129 130 protected: 131 // A map from section to a GOT section that has entries for section's GOT 132 // relocations. (Mips64 specific) 133 DenseMap<SID, SID> SectionToGOTMap; 134 135 private: 136 // A map to avoid duplicate got entries (Mips64 specific) 137 StringMap<uint64_t> GOTSymbolOffsets; 138 139 // *HI16 relocations will be added for resolving when we find matching 140 // *LO16 part. (Mips specific) 141 SmallVector<std::pair<RelocationValueRef, RelocationEntry>, 8> PendingRelocs; 142 143 // When a module is loaded we save the SectionID of the EH frame section 144 // in a table until we receive a request to register all unregistered 145 // EH frame sections with the memory manager. 146 SmallVector<SID, 2> UnregisteredEHFrameSections; 147 SmallVector<SID, 2> RegisteredEHFrameSections; 148 149 bool relocationNeedsStub(const RelocationRef &R) const override; 150 151 public: 152 RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr, 153 JITSymbolResolver &Resolver); 154 ~RuntimeDyldELF() override; 155 156 static std::unique_ptr<RuntimeDyldELF> 157 create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, 158 JITSymbolResolver &Resolver); 159 160 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> 161 loadObject(const object::ObjectFile &O) override; 162 163 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override; 164 Expected<relocation_iterator> 165 processRelocationRef(unsigned SectionID, relocation_iterator RelI, 166 const ObjectFile &Obj, 167 ObjSectionToIDMap &ObjSectionToID, 168 StubMap &Stubs) override; 169 bool isCompatibleFile(const object::ObjectFile &Obj) const override; 170 void registerEHFrames() override; 171 void deregisterEHFrames() override; 172 Error finalizeLoad(const ObjectFile &Obj, 173 ObjSectionToIDMap &SectionMap) override; 174 }; 175 176 } // end namespace llvm 177 178 #endif 179