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 | |
13 | #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H |
14 | #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H |
15 | |
16 | #include "RuntimeDyldImpl.h" |
17 | #include "llvm/Object/MachO.h" |
18 | #include "llvm/Support/Format.h" |
19 | |
20 | namespace llvm { |
21 | class RuntimeDyldMachO : public RuntimeDyldImpl { |
22 | protected: |
23 | struct SectionOffsetPair { |
24 | unsigned SectionID; |
25 | uint64_t Offset; |
26 | }; |
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 | |
34 | EHFrameRelatedSections(SID EH, SID T, SID Ex) |
35 | : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {} |
36 | SID EHFrameSID; |
37 | SID TextSID; |
38 | SID ExceptTabSID; |
39 | }; |
40 | |
41 | // When a module is loaded we save the SectionID of the EH frame section |
42 | // in a table until we receive a request to register all unregistered |
43 | // EH frame sections with the memory manager. |
44 | SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections; |
45 | |
46 | RuntimeDyldMachO(RuntimeDyld::MemoryManager &MemMgr, |
47 | JITSymbolResolver &Resolver) |
48 | : RuntimeDyldImpl(MemMgr, Resolver) {} |
49 | |
50 | /// This convenience method uses memcpy to extract a contiguous addend (the |
51 | /// addend size and offset are taken from the corresponding fields of the RE). |
52 | int64_t memcpyAddend(const RelocationEntry &RE) const; |
53 | |
54 | /// Given a relocation_iterator for a non-scattered relocation, construct a |
55 | /// RelocationEntry and fill in the common fields. The 'Addend' field is *not* |
56 | /// filled in, since immediate encodings are highly target/opcode specific. |
57 | /// For targets/opcodes with simple, contiguous immediates (e.g. X86) the |
58 | /// memcpyAddend method can be used to read the immediate. |
59 | RelocationEntry getRelocationEntry(unsigned SectionID, |
60 | const ObjectFile &BaseTObj, |
61 | const relocation_iterator &RI) const { |
62 | const MachOObjectFile &Obj = |
63 | static_cast<const MachOObjectFile &>(BaseTObj); |
64 | MachO::any_relocation_info RelInfo = |
65 | Obj.getRelocation(Rel: RI->getRawDataRefImpl()); |
66 | |
67 | bool IsPCRel = Obj.getAnyRelocationPCRel(RE: RelInfo); |
68 | unsigned Size = Obj.getAnyRelocationLength(RE: RelInfo); |
69 | uint64_t Offset = RI->getOffset(); |
70 | MachO::RelocationInfoType RelType = |
71 | static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RE: RelInfo)); |
72 | |
73 | return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size); |
74 | } |
75 | |
76 | /// Process a scattered vanilla relocation. |
77 | Expected<relocation_iterator> |
78 | processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI, |
79 | const ObjectFile &BaseObjT, |
80 | RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID, |
81 | bool TargetIsLocalThumbFunc = false); |
82 | |
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 | Expected<RelocationValueRef> |
93 | getRelocationValueRef(const ObjectFile &BaseTObj, |
94 | const relocation_iterator &RI, |
95 | const RelocationEntry &RE, |
96 | ObjSectionToIDMap &ObjSectionToID); |
97 | |
98 | /// Make the RelocationValueRef addend PC-relative. |
99 | void makeValueAddendPCRel(RelocationValueRef &Value, |
100 | const relocation_iterator &RI, |
101 | unsigned OffsetToNextPC); |
102 | |
103 | /// Dump information about the relocation entry (RE) and resolved value. |
104 | void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const; |
105 | |
106 | // Return a section iterator for the section containing the given address. |
107 | static section_iterator getSectionByAddress(const MachOObjectFile &Obj, |
108 | uint64_t Addr); |
109 | |
110 | |
111 | // Populate __pointers section. |
112 | Error (const MachOObjectFile &Obj, |
113 | const SectionRef &PTSection, |
114 | unsigned PTSectionID); |
115 | |
116 | public: |
117 | |
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 | JITSymbolResolver &Resolver); |
123 | |
124 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> |
125 | loadObject(const object::ObjectFile &O) override; |
126 | |
127 | SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; } |
128 | |
129 | bool isCompatibleFile(const object::ObjectFile &Obj) const override; |
130 | }; |
131 | |
132 | /// RuntimeDyldMachOTarget - Templated base class for generic MachO linker |
133 | /// algorithms and data structures. |
134 | /// |
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 { |
141 | private: |
142 | Impl &impl() { return static_cast<Impl &>(*this); } |
143 | const Impl &impl() const { return static_cast<const Impl &>(*this); } |
144 | |
145 | unsigned char *processFDE(uint8_t *P, int64_t DeltaForText, |
146 | int64_t DeltaForEH); |
147 | |
148 | public: |
149 | RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr, |
150 | JITSymbolResolver &Resolver) |
151 | : RuntimeDyldMachO(MemMgr, Resolver) {} |
152 | |
153 | Error finalizeLoad(const ObjectFile &Obj, |
154 | ObjSectionToIDMap &SectionMap) override; |
155 | void registerEHFrames() override; |
156 | }; |
157 | |
158 | } // end namespace llvm |
159 | |
160 | #endif // LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H |
161 | |