LLVM 24.0.0git
RuntimeDyldELF.h
Go to the documentation of this file.
1//===-- RuntimeDyldELF.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// ELF support for MC-JIT runtime dynamic linker.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
14#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
15
16#include "RuntimeDyldImpl.h"
17#include "llvm/ADT/DenseMap.h"
18
19namespace llvm {
20namespace object {
22}
23
25
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);
29
30 void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset,
31 uint64_t Value, uint32_t Type, int64_t Addend,
32 uint64_t SymOffset);
33
34 void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset,
35 uint32_t Value, uint32_t Type, int32_t Addend);
36
37 void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset,
38 uint64_t Value, uint32_t Type, int64_t Addend);
39
40 bool resolveAArch64ShortBranch(unsigned SectionID, relocation_iterator RelI,
42
43 void resolveAArch64Branch(unsigned SectionID, const RelocationValueRef &Value,
44 relocation_iterator RelI, StubMap &Stubs);
45
46 void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset,
47 uint32_t Value, uint32_t Type, int32_t Addend);
48
49 void resolveLoongArch64Relocation(const SectionEntry &Section,
50 uint64_t Offset, uint64_t Value,
51 uint32_t Type, int64_t Addend);
52
53 bool resolveLoongArch64ShortBranch(unsigned SectionID,
56
57 void resolveLoongArch64Branch(unsigned SectionID,
59 relocation_iterator RelI, StubMap &Stubs);
60
61 void resolvePPC32Relocation(const SectionEntry &Section, uint64_t Offset,
62 uint64_t Value, uint32_t Type, int64_t Addend);
63
64 void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset,
65 uint64_t Value, uint32_t Type, int64_t Addend);
66
67 void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset,
68 uint64_t Value, uint32_t Type, int64_t Addend);
69
70 void resolveBPFRelocation(const SectionEntry &Section, uint64_t Offset,
71 uint64_t Value, uint32_t Type, int64_t Addend);
72
73 void resolveRISCVRelocation(const SectionEntry &Section, uint64_t Offset,
74 uint64_t Value, uint32_t Type, int64_t Addend,
75 SID SectionID);
76
77 unsigned getMaxStubSize() const override {
79 return 20; // movz; movk; movk; movk; br
81 return 8; // 32-bit instruction and 32-bit address
82 else if (IsMipsO32ABI || IsMipsN32ABI)
83 return 16;
84 else if (IsMipsN64ABI)
85 return 32;
87 return 20; // lu12i.w; ori; lu32i.d; lu52i.d; jr
88 else if (Arch == Triple::ppc)
89 return 16; // lis; ori; mtctr; bctr
90 else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
91 return 44;
92 else if (Arch == Triple::x86_64)
93 return 6; // 2-byte jmp instruction + 32-bit relative address
94 else if (Arch == Triple::systemz)
95 return 16;
96 else
97 return 0;
98 }
99
100 Align getStubAlignment() override {
101 if (Arch == Triple::systemz)
102 return Align(8);
103 else
104 return Align(1);
105 }
106
107 void setMipsABI(const ObjectFile &Obj) override;
108
109 Error findPPC64TOCSection(const object::ELFObjectFileBase &Obj,
110 ObjSectionToIDMap &LocalSections,
111 RelocationValueRef &Rel);
112 Error findOPDEntrySection(const object::ELFObjectFileBase &Obj,
113 ObjSectionToIDMap &LocalSections,
114 RelocationValueRef &Rel);
115
116protected:
117 size_t getGOTEntrySize() override;
118
119private:
120 SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
121
122 // Allocate no GOT entries for use in the given section.
123 uint64_t allocateGOTEntries(unsigned no);
124
125 // Find GOT entry corresponding to relocation or create new one.
126 uint64_t findOrAllocGOTEntry(const RelocationValueRef &Value,
127 unsigned GOTRelType);
128
129 // Resolve the relative address of GOTOffset in Section ID and place
130 // it at the given Offset
131 void resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset,
132 uint64_t GOTOffset, uint32_t Type);
133
134 // For a GOT entry referenced from SectionID, compute a relocation entry
135 // that will place the final resolved value in the GOT slot
136 RelocationEntry computeGOTOffsetRE(uint64_t GOTOffset, uint64_t SymbolOffset,
137 unsigned Type);
138
139 // Compute the address in memory where we can find the placeholder
140 void *computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const;
141
142 // Split out common case for creating the RelocationEntry for when the
143 // relocation requires no particular advanced processing.
144 void processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value);
145
146 // Return matching *LO16 relocation (Mips specific)
147 uint32_t getMatchingLoRelocation(uint32_t RelType,
148 bool IsLocal = false) const;
149
150 // The tentative ID for the GOT section
151 unsigned GOTSectionID;
152
153 // Records the current number of allocated slots in the GOT
154 // (This would be equivalent to GOTEntries.size() were it not for relocations
155 // that consume more than one slot)
156 unsigned CurrentGOTIndex;
157
158protected:
159 // A map from section to a GOT section that has entries for section's GOT
160 // relocations. (Mips64 specific)
162
163private:
164 // A map to avoid duplicate got entries (Mips64 specific)
165 StringMap<uint64_t> GOTSymbolOffsets;
166
167 // *HI16 relocations will be added for resolving when we find matching
168 // *LO16 part. (Mips specific)
169 //
170 // *HI20 relocations will be added for resolving when we find matching
171 // *LO12 part. (RISC-V specific)
173
174 // When a module is loaded we save the SectionID of the EH frame section
175 // in a table until we receive a request to register all unregistered
176 // EH frame sections with the memory manager.
177 SmallVector<SID, 2> UnregisteredEHFrameSections;
178
179 // Map between GOT relocation value and corresponding GOT offset
180 std::map<RelocationValueRef, uint64_t> GOTOffsetMap;
181
182 /// The ID of the current IFunc stub section
183 unsigned IFuncStubSectionID = 0;
184 /// The current offset into the IFunc stub section
185 uint64_t IFuncStubOffset = 0;
186
187 /// A IFunc stub and its original symbol
188 struct IFuncStub {
189 /// The offset of this stub in the IFunc stub section
190 uint64_t StubOffset;
191 /// The symbol table entry of the original symbol
192 SymbolTableEntry OriginalSymbol;
193 };
194
195 /// The IFunc stubs
196 SmallVector<IFuncStub, 2> IFuncStubs;
197
198 /// Create the code for the IFunc resolver at the given address. This code
199 /// works together with the stubs created in createIFuncStub() to call the
200 /// resolver function and then jump to the real function address.
201 /// It must not be larger than 64B.
202 void createIFuncResolver(uint8_t *Addr) const;
203 /// Create the code for an IFunc stub for the IFunc that is defined in
204 /// section IFuncSectionID at offset IFuncOffset. The IFunc resolver created
205 /// by createIFuncResolver() is defined in the section IFuncStubSectionID at
206 /// offset IFuncResolverOffset. The code should be written into the section
207 /// with the id IFuncStubSectionID at the offset IFuncStubOffset.
208 void createIFuncStub(unsigned IFuncStubSectionID,
209 uint64_t IFuncResolverOffset, uint64_t IFuncStubOffset,
210 unsigned IFuncSectionID, uint64_t IFuncOffset);
211 /// Return the maximum size of a stub created by createIFuncStub()
212 unsigned getMaxIFuncStubSize() const;
213
214 void processNewSymbol(const SymbolRef &ObjSymbol,
215 SymbolTableEntry &Entry) override;
216 bool relocationNeedsGot(const RelocationRef &R) const override;
217 bool relocationNeedsStub(const RelocationRef &R) const override;
218
219 // Process a GOTTPOFF TLS relocation for x86-64
220 // NOLINTNEXTLINE(readability-identifier-naming)
221 void processX86_64GOTTPOFFRelocation(unsigned SectionID, uint64_t Offset,
223 int64_t Addend);
224 // Process a TLSLD/TLSGD relocation for x86-64
225 // NOLINTNEXTLINE(readability-identifier-naming)
226 void processX86_64TLSRelocation(unsigned SectionID, uint64_t Offset,
227 uint64_t RelType, RelocationValueRef Value,
228 int64_t Addend,
229 const RelocationRef &GetAddrRelocation);
230
231public:
234 ~RuntimeDyldELF() override;
235
236 static std::unique_ptr<RuntimeDyldELF>
239
240 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
241 loadObject(const object::ObjectFile &O) override;
242
243 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
245 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
246 const ObjectFile &Obj,
247 ObjSectionToIDMap &ObjSectionToID,
248 StubMap &Stubs) override;
249 bool isCompatibleFile(const object::ObjectFile &Obj) const override;
250 void registerEHFrames() override;
251 Error finalizeLoad(const ObjectFile &Obj,
252 ObjSectionToIDMap &SectionMap) override;
253};
254
255} // end namespace llvm
256
257#endif // LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
This file defines the DenseMap class.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Symbol resolution interface.
Definition JITSymbol.h:373
RelocationEntry - used to represent relocations internally in the dynamic linker.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition Record.h:2255
void registerEHFrames() override
size_t getGOTEntrySize() override
~RuntimeDyldELF() override
static std::unique_ptr< RuntimeDyldELF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Error finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
DenseMap< SID, SID > SectionToGOTMap
bool isCompatibleFile(const object::ObjectFile &Obj) const override
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &O) override
RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Expected< relocation_iterator > processRelocationRef(unsigned SectionID, relocation_iterator RelI, const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) override
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
std::map< SectionRef, unsigned > ObjSectionToIDMap
virtual Align getStubAlignment()=0
RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
std::map< RelocationValueRef, uintptr_t > StubMap
JITSymbolResolver & Resolver
RuntimeDyld::MemoryManager & MemMgr
Triple::ArchType Arch
virtual unsigned getMaxStubSize() const =0
SectionEntry - represents a section emitted into memory by the dynamic linker.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:129
Symbol info for RuntimeDyld.
@ loongarch64
Definition Triple.h:66
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
This class is the base class for all object file types.
Definition ObjectFile.h:231
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition ObjectFile.h:54
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition ObjectFile.h:170
content_iterator< RelocationRef > relocation_iterator
Definition ObjectFile.h:79
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39