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 RuntimeDyldELF : public RuntimeDyldImpl {
26 void resolveRelocation(const SectionEntry &Section, uint64_t Offset,
27 uint64_t Value, uint32_t Type, int64_t Addend,
28 uint64_t SymOffset = 0, SID SectionID = 0);
30 void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset,
31 uint64_t Value, uint32_t Type, int64_t Addend,
34 void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset,
35 uint32_t Value, uint32_t Type, int32_t Addend);
37 void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset,
38 uint64_t Value, uint32_t Type, int64_t Addend);
40 void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset,
41 uint32_t Value, uint32_t Type, int32_t Addend);
43 void resolveMIPSRelocation(const SectionEntry &Section, uint64_t Offset,
44 uint32_t Value, uint32_t Type, int32_t Addend);
46 void resolvePPC32Relocation(const SectionEntry &Section, uint64_t Offset,
47 uint64_t Value, uint32_t Type, int64_t Addend);
49 void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset,
50 uint64_t Value, uint32_t Type, int64_t Addend);
52 void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset,
53 uint64_t Value, uint32_t Type, int64_t Addend);
55 void resolveMIPS64Relocation(const SectionEntry &Section, uint64_t Offset,
56 uint64_t Value, uint32_t Type, int64_t Addend,
57 uint64_t SymOffset, SID SectionID);
59 int64_t evaluateMIPS64Relocation(const SectionEntry &Section,
60 uint64_t Offset, uint64_t Value,
61 uint32_t Type, int64_t Addend,
62 uint64_t SymOffset, SID SectionID);
64 void applyMIPS64Relocation(uint8_t *TargetPtr, int64_t CalculatedValue,
67 unsigned getMaxStubSize() override {
68 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)
69 return 20; // movz; movk; movk; movk; br
70 if (Arch == Triple::arm || Arch == Triple::thumb)
71 return 8; // 32-bit instruction and 32-bit address
72 else if (IsMipsO32ABI)
74 else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
76 else if (Arch == Triple::x86_64)
77 return 6; // 2-byte jmp instruction + 32-bit relative address
78 else if (Arch == Triple::systemz)
84 unsigned getStubAlignment() override {
85 if (Arch == Triple::systemz)
91 void setMipsABI(const ObjectFile &Obj) override;
93 void findPPC64TOCSection(const ELFObjectFileBase &Obj,
94 ObjSectionToIDMap &LocalSections,
95 RelocationValueRef &Rel);
96 void findOPDEntrySection(const ELFObjectFileBase &Obj,
97 ObjSectionToIDMap &LocalSections,
98 RelocationValueRef &Rel);
100 size_t getGOTEntrySize();
102 SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
104 // Allocate no GOT entries for use in the given section.
105 uint64_t allocateGOTEntries(unsigned SectionID, unsigned no);
107 // Resolve the relvative address of GOTOffset in Section ID and place
108 // it at the given Offset
109 void resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset,
112 // For a GOT entry referenced from SectionID, compute a relocation entry
113 // that will place the final resolved value in the GOT slot
114 RelocationEntry computeGOTOffsetRE(unsigned SectionID,
116 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;
138 // A map from section to a GOT section that has entries for section's GOT
139 // relocations. (Mips64 specific)
140 DenseMap<SID, SID> SectionToGOTMap;
142 // A map to avoid duplicate got entries (Mips64 specific)
143 StringMap<uint64_t> GOTSymbolOffsets;
145 // *HI16 relocations will be added for resolving when we find matching
146 // *LO16 part. (Mips specific)
147 SmallVector<std::pair<RelocationValueRef, RelocationEntry>, 8> PendingRelocs;
149 // When a module is loaded we save the SectionID of the EH frame section
150 // in a table until we receive a request to register all unregistered
151 // EH frame sections with the memory manager.
152 SmallVector<SID, 2> UnregisteredEHFrameSections;
153 SmallVector<SID, 2> RegisteredEHFrameSections;
155 bool relocationNeedsStub(const RelocationRef &R) const override;
158 RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
159 RuntimeDyld::SymbolResolver &Resolver);
160 ~RuntimeDyldELF() override;
162 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
163 loadObject(const object::ObjectFile &O) override;
165 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
167 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
168 const ObjectFile &Obj,
169 ObjSectionToIDMap &ObjSectionToID,
170 StubMap &Stubs) override;
171 bool isCompatibleFile(const object::ObjectFile &Obj) const override;
172 void registerEHFrames() override;
173 void deregisterEHFrames() override;
174 void finalizeLoad(const ObjectFile &Obj,
175 ObjSectionToIDMap &SectionMap) override;
178 } // end namespace llvm