1 //===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // MachO support for MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
15 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
17 #include "RuntimeDyldImpl.h"
18 #include "llvm/Object/MachO.h"
19 #include "llvm/Support/Format.h"
21 #define DEBUG_TYPE "dyld"
24 using namespace llvm::object;
27 class RuntimeDyldMachO : public RuntimeDyldImpl {
29 struct SectionOffsetPair {
34 struct EHFrameRelatedSections {
35 EHFrameRelatedSections()
36 : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
37 TextSID(RTDYLD_INVALID_SECTION_ID),
38 ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
40 EHFrameRelatedSections(SID EH, SID T, SID Ex)
41 : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
47 // When a module is loaded we save the SectionID of the EH frame section
48 // in a table until we receive a request to register all unregistered
49 // EH frame sections with the memory manager.
50 SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;
52 RuntimeDyldMachO(RuntimeDyld::MemoryManager &MemMgr,
53 RuntimeDyld::SymbolResolver &Resolver)
54 : RuntimeDyldImpl(MemMgr, Resolver) {}
56 /// This convenience method uses memcpy to extract a contiguous addend (the
57 /// addend size and offset are taken from the corresponding fields of the RE).
58 int64_t memcpyAddend(const RelocationEntry &RE) const;
60 /// Given a relocation_iterator for a non-scattered relocation, construct a
61 /// RelocationEntry and fill in the common fields. The 'Addend' field is *not*
62 /// filled in, since immediate encodings are highly target/opcode specific.
63 /// For targets/opcodes with simple, contiguous immediates (e.g. X86) the
64 /// memcpyAddend method can be used to read the immediate.
65 RelocationEntry getRelocationEntry(unsigned SectionID,
66 const ObjectFile &BaseTObj,
67 const relocation_iterator &RI) const {
68 const MachOObjectFile &Obj =
69 static_cast<const MachOObjectFile &>(BaseTObj);
70 MachO::any_relocation_info RelInfo =
71 Obj.getRelocation(RI->getRawDataRefImpl());
73 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
74 unsigned Size = Obj.getAnyRelocationLength(RelInfo);
76 RI->getOffset(Offset);
77 MachO::RelocationInfoType RelType =
78 static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RelInfo));
80 return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size);
83 /// Construct a RelocationValueRef representing the relocation target.
84 /// For Symbols in known sections, this will return a RelocationValueRef
85 /// representing a (SectionID, Offset) pair.
86 /// For Symbols whose section is not known, this will return a
87 /// (SymbolName, Offset) pair, where the Offset is taken from the instruction
88 /// immediate (held in RE.Addend).
89 /// In both cases the Addend field is *NOT* fixed up to be PC-relative. That
90 /// should be done by the caller where appropriate by calling makePCRel on
91 /// the RelocationValueRef.
92 RelocationValueRef getRelocationValueRef(const ObjectFile &BaseTObj,
93 const relocation_iterator &RI,
94 const RelocationEntry &RE,
95 ObjSectionToIDMap &ObjSectionToID);
97 /// Make the RelocationValueRef addend PC-relative.
98 void makeValueAddendPCRel(RelocationValueRef &Value,
99 const ObjectFile &BaseTObj,
100 const relocation_iterator &RI,
101 unsigned OffsetToNextPC);
103 /// Dump information about the relocation entry (RE) and resolved value.
104 void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;
106 // Return a section iterator for the section containing the given address.
107 static section_iterator getSectionByAddress(const MachOObjectFile &Obj,
111 // Populate __pointers section.
112 void populateIndirectSymbolPointersSection(const MachOObjectFile &Obj,
113 const SectionRef &PTSection,
114 unsigned PTSectionID);
118 /// Create a RuntimeDyldMachO instance for the given target architecture.
119 static std::unique_ptr<RuntimeDyldMachO>
120 create(Triple::ArchType Arch,
121 RuntimeDyld::MemoryManager &MemMgr,
122 RuntimeDyld::SymbolResolver &Resolver);
124 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
125 loadObject(const object::ObjectFile &O) override;
127 SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
129 bool isCompatibleFile(const object::ObjectFile &Obj) const override;
132 /// RuntimeDyldMachOTarget - Templated base class for generic MachO linker
133 /// algorithms and data structures.
135 /// Concrete, target specific sub-classes can be accessed via the impl()
136 /// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously
137 /// Recurring Template Idiom). Concrete subclasses for each target
138 /// can be found in ./Targets.
139 template <typename Impl>
140 class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO {
142 Impl &impl() { return static_cast<Impl &>(*this); }
143 const Impl &impl() const { return static_cast<const Impl &>(*this); }
145 unsigned char *processFDE(unsigned char *P, int64_t DeltaForText,
149 RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr,
150 RuntimeDyld::SymbolResolver &Resolver)
151 : RuntimeDyldMachO(MemMgr, Resolver) {}
153 void finalizeLoad(const ObjectFile &Obj,
154 ObjSectionToIDMap &SectionMap) override;
155 void registerEHFrames() override;
158 } // end namespace llvm