RuntimeDyldELF.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //===-- RuntimeDyldELF.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // ELF support for MC-JIT runtime dynamic linker.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
  13. #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
  14. #include "RuntimeDyldImpl.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. using namespace llvm;
  17. namespace llvm {
  18. namespace object {
  19. class ELFObjectFileBase;
  20. }
  21. class RuntimeDyldELF : public RuntimeDyldImpl {
  22. void resolveRelocation(const SectionEntry &Section, uint64_t Offset,
  23. uint64_t Value, uint32_t Type, int64_t Addend,
  24. uint64_t SymOffset = 0, SID SectionID = 0);
  25. void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset,
  26. uint64_t Value, uint32_t Type, int64_t Addend,
  27. uint64_t SymOffset);
  28. void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset,
  29. uint32_t Value, uint32_t Type, int32_t Addend);
  30. void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset,
  31. uint64_t Value, uint32_t Type, int64_t Addend);
  32. bool resolveAArch64ShortBranch(unsigned SectionID, relocation_iterator RelI,
  33. const RelocationValueRef &Value);
  34. void resolveAArch64Branch(unsigned SectionID, const RelocationValueRef &Value,
  35. relocation_iterator RelI, StubMap &Stubs);
  36. void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset,
  37. uint32_t Value, uint32_t Type, int32_t Addend);
  38. void resolvePPC32Relocation(const SectionEntry &Section, uint64_t Offset,
  39. uint64_t Value, uint32_t Type, int64_t Addend);
  40. void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset,
  41. uint64_t Value, uint32_t Type, int64_t Addend);
  42. void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset,
  43. uint64_t Value, uint32_t Type, int64_t Addend);
  44. void resolveBPFRelocation(const SectionEntry &Section, uint64_t Offset,
  45. uint64_t Value, uint32_t Type, int64_t Addend);
  46. unsigned getMaxStubSize() const override {
  47. if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)
  48. return 20; // movz; movk; movk; movk; br
  49. if (Arch == Triple::arm || Arch == Triple::thumb)
  50. return 8; // 32-bit instruction and 32-bit address
  51. else if (IsMipsO32ABI || IsMipsN32ABI)
  52. return 16;
  53. else if (IsMipsN64ABI)
  54. return 32;
  55. else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
  56. return 44;
  57. else if (Arch == Triple::x86_64)
  58. return 6; // 2-byte jmp instruction + 32-bit relative address
  59. else if (Arch == Triple::systemz)
  60. return 16;
  61. else
  62. return 0;
  63. }
  64. Align getStubAlignment() override {
  65. if (Arch == Triple::systemz)
  66. return Align(8);
  67. else
  68. return Align(1);
  69. }
  70. void setMipsABI(const ObjectFile &Obj) override;
  71. Error findPPC64TOCSection(const object::ELFObjectFileBase &Obj,
  72. ObjSectionToIDMap &LocalSections,
  73. RelocationValueRef &Rel);
  74. Error findOPDEntrySection(const object::ELFObjectFileBase &Obj,
  75. ObjSectionToIDMap &LocalSections,
  76. RelocationValueRef &Rel);
  77. protected:
  78. size_t getGOTEntrySize() override;
  79. private:
  80. SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
  81. // Allocate no GOT entries for use in the given section.
  82. uint64_t allocateGOTEntries(unsigned no);
  83. // Find GOT entry corresponding to relocation or create new one.
  84. uint64_t findOrAllocGOTEntry(const RelocationValueRef &Value,
  85. unsigned GOTRelType);
  86. // Resolve the relvative address of GOTOffset in Section ID and place
  87. // it at the given Offset
  88. void resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset,
  89. uint64_t GOTOffset, uint32_t Type);
  90. // For a GOT entry referenced from SectionID, compute a relocation entry
  91. // that will place the final resolved value in the GOT slot
  92. RelocationEntry computeGOTOffsetRE(uint64_t GOTOffset, uint64_t SymbolOffset,
  93. unsigned Type);
  94. // Compute the address in memory where we can find the placeholder
  95. void *computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const;
  96. // Split out common case for createing the RelocationEntry for when the relocation requires
  97. // no particular advanced processing.
  98. void processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value);
  99. // Return matching *LO16 relocation (Mips specific)
  100. uint32_t getMatchingLoRelocation(uint32_t RelType,
  101. bool IsLocal = false) const;
  102. // The tentative ID for the GOT section
  103. unsigned GOTSectionID;
  104. // Records the current number of allocated slots in the GOT
  105. // (This would be equivalent to GOTEntries.size() were it not for relocations
  106. // that consume more than one slot)
  107. unsigned CurrentGOTIndex;
  108. protected:
  109. // A map from section to a GOT section that has entries for section's GOT
  110. // relocations. (Mips64 specific)
  111. DenseMap<SID, SID> SectionToGOTMap;
  112. private:
  113. // A map to avoid duplicate got entries (Mips64 specific)
  114. StringMap<uint64_t> GOTSymbolOffsets;
  115. // *HI16 relocations will be added for resolving when we find matching
  116. // *LO16 part. (Mips specific)
  117. SmallVector<std::pair<RelocationValueRef, RelocationEntry>, 8> PendingRelocs;
  118. // When a module is loaded we save the SectionID of the EH frame section
  119. // in a table until we receive a request to register all unregistered
  120. // EH frame sections with the memory manager.
  121. SmallVector<SID, 2> UnregisteredEHFrameSections;
  122. // Map between GOT relocation value and corresponding GOT offset
  123. std::map<RelocationValueRef, uint64_t> GOTOffsetMap;
  124. /// The ID of the current IFunc stub section
  125. unsigned IFuncStubSectionID = 0;
  126. /// The current offset into the IFunc stub section
  127. uint64_t IFuncStubOffset = 0;
  128. /// A IFunc stub and its original symbol
  129. struct IFuncStub {
  130. /// The offset of this stub in the IFunc stub section
  131. uint64_t StubOffset;
  132. /// The symbol table entry of the original symbol
  133. SymbolTableEntry OriginalSymbol;
  134. };
  135. /// The IFunc stubs
  136. SmallVector<IFuncStub, 2> IFuncStubs;
  137. /// Create the code for the IFunc resolver at the given address. This code
  138. /// works together with the stubs created in createIFuncStub() to call the
  139. /// resolver function and then jump to the real function address.
  140. /// It must not be larger than 64B.
  141. void createIFuncResolver(uint8_t *Addr) const;
  142. /// Create the code for an IFunc stub for the IFunc that is defined in
  143. /// section IFuncSectionID at offset IFuncOffset. The IFunc resolver created
  144. /// by createIFuncResolver() is defined in the section IFuncStubSectionID at
  145. /// offset IFuncResolverOffset. The code should be written into the section
  146. /// with the id IFuncStubSectionID at the offset IFuncStubOffset.
  147. void createIFuncStub(unsigned IFuncStubSectionID,
  148. uint64_t IFuncResolverOffset, uint64_t IFuncStubOffset,
  149. unsigned IFuncSectionID, uint64_t IFuncOffset);
  150. /// Return the maximum size of a stub created by createIFuncStub()
  151. unsigned getMaxIFuncStubSize() const;
  152. void processNewSymbol(const SymbolRef &ObjSymbol,
  153. SymbolTableEntry &Entry) override;
  154. bool relocationNeedsGot(const RelocationRef &R) const override;
  155. bool relocationNeedsStub(const RelocationRef &R) const override;
  156. // Process a GOTTPOFF TLS relocation for x86-64
  157. // NOLINTNEXTLINE(readability-identifier-naming)
  158. void processX86_64GOTTPOFFRelocation(unsigned SectionID, uint64_t Offset,
  159. RelocationValueRef Value,
  160. int64_t Addend);
  161. // Process a TLSLD/TLSGD relocation for x86-64
  162. // NOLINTNEXTLINE(readability-identifier-naming)
  163. void processX86_64TLSRelocation(unsigned SectionID, uint64_t Offset,
  164. uint64_t RelType, RelocationValueRef Value,
  165. int64_t Addend,
  166. const RelocationRef &GetAddrRelocation);
  167. public:
  168. RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
  169. JITSymbolResolver &Resolver);
  170. ~RuntimeDyldELF() override;
  171. static std::unique_ptr<RuntimeDyldELF>
  172. create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr,
  173. JITSymbolResolver &Resolver);
  174. std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
  175. loadObject(const object::ObjectFile &O) override;
  176. void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
  177. Expected<relocation_iterator>
  178. processRelocationRef(unsigned SectionID, relocation_iterator RelI,
  179. const ObjectFile &Obj,
  180. ObjSectionToIDMap &ObjSectionToID,
  181. StubMap &Stubs) override;
  182. bool isCompatibleFile(const object::ObjectFile &Obj) const override;
  183. void registerEHFrames() override;
  184. Error finalizeLoad(const ObjectFile &Obj,
  185. ObjSectionToIDMap &SectionMap) override;
  186. };
  187. } // end namespace llvm
  188. #endif