RuntimeDyldMachO.h 6.2 KB

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