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