RuntimeDyldELF.h 6.6 KB

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