1 //===-- RuntimeDyldELF.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 // ELF support for MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
15 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
17 #include "RuntimeDyldImpl.h"
18 #include "llvm/ADT/DenseMap.h"
24 class ELFObjectFileBase;
27 class RuntimeDyldELF : public RuntimeDyldImpl {
29 void resolveRelocation(const SectionEntry &Section, uint64_t Offset,
30 uint64_t Value, uint32_t Type, int64_t Addend,
31 uint64_t SymOffset = 0, SID SectionID = 0);
33 void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset,
34 uint64_t Value, uint32_t Type, int64_t Addend,
37 void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset,
38 uint32_t Value, uint32_t Type, int32_t Addend);
40 void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset,
41 uint64_t Value, uint32_t Type, int64_t Addend);
43 bool resolveAArch64ShortBranch(unsigned SectionID, relocation_iterator RelI,
44 const RelocationValueRef &Value);
46 void resolveAArch64Branch(unsigned SectionID, const RelocationValueRef &Value,
47 relocation_iterator RelI, StubMap &Stubs);
49 void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset,
50 uint32_t Value, uint32_t Type, int32_t Addend);
52 void resolvePPC32Relocation(const SectionEntry &Section, uint64_t Offset,
53 uint64_t Value, uint32_t Type, int64_t Addend);
55 void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset,
56 uint64_t Value, uint32_t Type, int64_t Addend);
58 void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset,
59 uint64_t Value, uint32_t Type, int64_t Addend);
61 void resolveBPFRelocation(const SectionEntry &Section, uint64_t Offset,
62 uint64_t Value, uint32_t Type, int64_t Addend);
64 unsigned getMaxStubSize() override {
65 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)
66 return 20; // movz; movk; movk; movk; br
67 if (Arch == Triple::arm || Arch == Triple::thumb)
68 return 8; // 32-bit instruction and 32-bit address
69 else if (IsMipsO32ABI)
71 else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
73 else if (Arch == Triple::x86_64)
74 return 6; // 2-byte jmp instruction + 32-bit relative address
75 else if (Arch == Triple::systemz)
81 unsigned getStubAlignment() override {
82 if (Arch == Triple::systemz)
88 void setMipsABI(const ObjectFile &Obj) override;
90 Error findPPC64TOCSection(const ELFObjectFileBase &Obj,
91 ObjSectionToIDMap &LocalSections,
92 RelocationValueRef &Rel);
93 Error findOPDEntrySection(const ELFObjectFileBase &Obj,
94 ObjSectionToIDMap &LocalSections,
95 RelocationValueRef &Rel);
97 size_t getGOTEntrySize() override;
100 SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
102 // Allocate no GOT entries for use in the given section.
103 uint64_t allocateGOTEntries(unsigned no);
105 // Find GOT entry corresponding to relocation or create new one.
106 uint64_t findOrAllocGOTEntry(const RelocationValueRef &Value,
107 unsigned GOTRelType);
109 // Resolve the relvative address of GOTOffset in Section ID and place
110 // it at the given Offset
111 void resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset,
112 uint64_t GOTOffset, uint32_t Type);
114 // For a GOT entry referenced from SectionID, compute a relocation entry
115 // that will place the final resolved value in the GOT slot
116 RelocationEntry computeGOTOffsetRE(uint64_t GOTOffset, uint64_t SymbolOffset,
119 // Compute the address in memory where we can find the placeholder
120 void *computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const;
122 // Split out common case for createing the RelocationEntry for when the relocation requires
123 // no particular advanced processing.
124 void processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value);
126 // Return matching *LO16 relocation (Mips specific)
127 uint32_t getMatchingLoRelocation(uint32_t RelType,
128 bool IsLocal = false) const;
130 // The tentative ID for the GOT section
131 unsigned GOTSectionID;
133 // Records the current number of allocated slots in the GOT
134 // (This would be equivalent to GOTEntries.size() were it not for relocations
135 // that consume more than one slot)
136 unsigned CurrentGOTIndex;
139 // A map from section to a GOT section that has entries for section's GOT
140 // relocations. (Mips64 specific)
141 DenseMap<SID, SID> SectionToGOTMap;
144 // A map to avoid duplicate got entries (Mips64 specific)
145 StringMap<uint64_t> GOTSymbolOffsets;
147 // *HI16 relocations will be added for resolving when we find matching
148 // *LO16 part. (Mips specific)
149 SmallVector<std::pair<RelocationValueRef, RelocationEntry>, 8> PendingRelocs;
151 // When a module is loaded we save the SectionID of the EH frame section
152 // in a table until we receive a request to register all unregistered
153 // EH frame sections with the memory manager.
154 SmallVector<SID, 2> UnregisteredEHFrameSections;
156 // Map between GOT relocation value and corresponding GOT offset
157 std::map<RelocationValueRef, uint64_t> GOTOffsetMap;
159 bool relocationNeedsGot(const RelocationRef &R) const override;
160 bool relocationNeedsStub(const RelocationRef &R) const override;
163 RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
164 JITSymbolResolver &Resolver);
165 ~RuntimeDyldELF() override;
167 static std::unique_ptr<RuntimeDyldELF>
168 create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr,
169 JITSymbolResolver &Resolver);
171 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
172 loadObject(const object::ObjectFile &O) override;
174 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
175 Expected<relocation_iterator>
176 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
177 const ObjectFile &Obj,
178 ObjSectionToIDMap &ObjSectionToID,
179 StubMap &Stubs) override;
180 bool isCompatibleFile(const object::ObjectFile &Obj) const override;
181 void registerEHFrames() override;
182 Error finalizeLoad(const ObjectFile &Obj,
183 ObjSectionToIDMap &SectionMap) override;
186 } // end namespace llvm