RuntimeDyldELF.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. unsigned getStubAlignment() override {
  65. if (Arch == Triple::systemz)
  66. return 8;
  67. else
  68. return 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. bool relocationNeedsGot(const RelocationRef &R) const override;
  125. bool relocationNeedsStub(const RelocationRef &R) const override;
  126. // Process a GOTTPOFF TLS relocation for x86-64
  127. // NOLINTNEXTLINE(readability-identifier-naming)
  128. void processX86_64GOTTPOFFRelocation(unsigned SectionID, uint64_t Offset,
  129. RelocationValueRef Value,
  130. int64_t Addend);
  131. // Process a TLSLD/TLSGD relocation for x86-64
  132. // NOLINTNEXTLINE(readability-identifier-naming)
  133. void processX86_64TLSRelocation(unsigned SectionID, uint64_t Offset,
  134. uint64_t RelType, RelocationValueRef Value,
  135. int64_t Addend,
  136. const RelocationRef &GetAddrRelocation);
  137. public:
  138. RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
  139. JITSymbolResolver &Resolver);
  140. ~RuntimeDyldELF() override;
  141. static std::unique_ptr<RuntimeDyldELF>
  142. create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr,
  143. JITSymbolResolver &Resolver);
  144. std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
  145. loadObject(const object::ObjectFile &O) override;
  146. void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
  147. Expected<relocation_iterator>
  148. processRelocationRef(unsigned SectionID, relocation_iterator RelI,
  149. const ObjectFile &Obj,
  150. ObjSectionToIDMap &ObjSectionToID,
  151. StubMap &Stubs) override;
  152. bool isCompatibleFile(const object::ObjectFile &Obj) const override;
  153. void registerEHFrames() override;
  154. Error finalizeLoad(const ObjectFile &Obj,
  155. ObjSectionToIDMap &SectionMap) override;
  156. };
  157. } // end namespace llvm
  158. #endif