RuntimeDyldMachO.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //===-- RuntimeDyldMachO.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. // MachO support for MC-JIT runtime dynamic linker.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
  13. #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
  14. #include "RuntimeDyldImpl.h"
  15. #include "llvm/Object/MachO.h"
  16. #include "llvm/Support/Format.h"
  17. #define DEBUG_TYPE "dyld"
  18. using namespace llvm;
  19. using namespace llvm::object;
  20. namespace llvm {
  21. class RuntimeDyldMachO : public RuntimeDyldImpl {
  22. protected:
  23. struct SectionOffsetPair {
  24. unsigned SectionID;
  25. uint64_t Offset;
  26. };
  27. struct EHFrameRelatedSections {
  28. EHFrameRelatedSections()
  29. : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
  30. TextSID(RTDYLD_INVALID_SECTION_ID),
  31. ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
  32. EHFrameRelatedSections(SID EH, SID T, SID Ex)
  33. : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
  34. SID EHFrameSID;
  35. SID TextSID;
  36. SID ExceptTabSID;
  37. };
  38. // When a module is loaded we save the SectionID of the EH frame section
  39. // in a table until we receive a request to register all unregistered
  40. // EH frame sections with the memory manager.
  41. SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;
  42. RuntimeDyldMachO(RuntimeDyld::MemoryManager &MemMgr,
  43. JITSymbolResolver &Resolver)
  44. : RuntimeDyldImpl(MemMgr, Resolver) {}
  45. /// This convenience method uses memcpy to extract a contiguous addend (the
  46. /// addend size and offset are taken from the corresponding fields of the RE).
  47. int64_t memcpyAddend(const RelocationEntry &RE) const;
  48. /// Given a relocation_iterator for a non-scattered relocation, construct a
  49. /// RelocationEntry and fill in the common fields. The 'Addend' field is *not*
  50. /// filled in, since immediate encodings are highly target/opcode specific.
  51. /// For targets/opcodes with simple, contiguous immediates (e.g. X86) the
  52. /// memcpyAddend method can be used to read the immediate.
  53. RelocationEntry getRelocationEntry(unsigned SectionID,
  54. const ObjectFile &BaseTObj,
  55. const relocation_iterator &RI) const {
  56. const MachOObjectFile &Obj =
  57. static_cast<const MachOObjectFile &>(BaseTObj);
  58. MachO::any_relocation_info RelInfo =
  59. Obj.getRelocation(RI->getRawDataRefImpl());
  60. bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
  61. unsigned Size = Obj.getAnyRelocationLength(RelInfo);
  62. uint64_t Offset = RI->getOffset();
  63. MachO::RelocationInfoType RelType =
  64. static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RelInfo));
  65. return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size);
  66. }
  67. /// Process a scattered vanilla relocation.
  68. Expected<relocation_iterator>
  69. processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI,
  70. const ObjectFile &BaseObjT,
  71. RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID,
  72. bool TargetIsLocalThumbFunc = false);
  73. /// Construct a RelocationValueRef representing the relocation target.
  74. /// For Symbols in known sections, this will return a RelocationValueRef
  75. /// representing a (SectionID, Offset) pair.
  76. /// For Symbols whose section is not known, this will return a
  77. /// (SymbolName, Offset) pair, where the Offset is taken from the instruction
  78. /// immediate (held in RE.Addend).
  79. /// In both cases the Addend field is *NOT* fixed up to be PC-relative. That
  80. /// should be done by the caller where appropriate by calling makePCRel on
  81. /// the RelocationValueRef.
  82. Expected<RelocationValueRef>
  83. getRelocationValueRef(const ObjectFile &BaseTObj,
  84. const relocation_iterator &RI,
  85. const RelocationEntry &RE,
  86. ObjSectionToIDMap &ObjSectionToID);
  87. /// Make the RelocationValueRef addend PC-relative.
  88. void makeValueAddendPCRel(RelocationValueRef &Value,
  89. const relocation_iterator &RI,
  90. unsigned OffsetToNextPC);
  91. /// Dump information about the relocation entry (RE) and resolved value.
  92. void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;
  93. // Return a section iterator for the section containing the given address.
  94. static section_iterator getSectionByAddress(const MachOObjectFile &Obj,
  95. uint64_t Addr);
  96. // Populate __pointers section.
  97. Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj,
  98. const SectionRef &PTSection,
  99. unsigned PTSectionID);
  100. public:
  101. /// Create a RuntimeDyldMachO instance for the given target architecture.
  102. static std::unique_ptr<RuntimeDyldMachO>
  103. create(Triple::ArchType Arch,
  104. RuntimeDyld::MemoryManager &MemMgr,
  105. JITSymbolResolver &Resolver);
  106. std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
  107. loadObject(const object::ObjectFile &O) override;
  108. SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
  109. bool isCompatibleFile(const object::ObjectFile &Obj) const override;
  110. };
  111. /// RuntimeDyldMachOTarget - Templated base class for generic MachO linker
  112. /// algorithms and data structures.
  113. ///
  114. /// Concrete, target specific sub-classes can be accessed via the impl()
  115. /// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously
  116. /// Recurring Template Idiom). Concrete subclasses for each target
  117. /// can be found in ./Targets.
  118. template <typename Impl>
  119. class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO {
  120. private:
  121. Impl &impl() { return static_cast<Impl &>(*this); }
  122. const Impl &impl() const { return static_cast<const Impl &>(*this); }
  123. unsigned char *processFDE(uint8_t *P, int64_t DeltaForText,
  124. int64_t DeltaForEH);
  125. public:
  126. RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr,
  127. JITSymbolResolver &Resolver)
  128. : RuntimeDyldMachO(MemMgr, Resolver) {}
  129. Error finalizeLoad(const ObjectFile &Obj,
  130. ObjSectionToIDMap &SectionMap) override;
  131. void registerEHFrames() override;
  132. };
  133. } // end namespace llvm
  134. #undef DEBUG_TYPE
  135. #endif