LLVM 20.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 resolvePPC32Relocation(const SectionEntry &Section, uint64_t Offset,
50 uint64_t Value, uint32_t Type, int64_t Addend);
51
52 void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset,
53 uint64_t Value, uint32_t Type, int64_t Addend);
54
55 void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset,
56 uint64_t Value, uint32_t Type, int64_t Addend);
57
58 void resolveBPFRelocation(const SectionEntry &Section, uint64_t Offset,
59 uint64_t Value, uint32_t Type, int64_t Addend);
60
61 unsigned getMaxStubSize() const override {
63 return 20; // movz; movk; movk; movk; br
65 return 8; // 32-bit instruction and 32-bit address
66 else if (IsMipsO32ABI || IsMipsN32ABI)
67 return 16;
68 else if (IsMipsN64ABI)
69 return 32;
70 else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
71 return 44;
72 else if (Arch == Triple::x86_64)
73 return 6; // 2-byte jmp instruction + 32-bit relative address
74 else if (Arch == Triple::systemz)
75 return 16;
76 else
77 return 0;
78 }
79
80 Align getStubAlignment() override {
81 if (Arch == Triple::systemz)
82 return Align(8);
83 else
84 return Align(1);
85 }
86
87 void setMipsABI(const ObjectFile &Obj) override;
88
89 Error findPPC64TOCSection(const object::ELFObjectFileBase &Obj,
90 ObjSectionToIDMap &LocalSections,
92 Error findOPDEntrySection(const object::ELFObjectFileBase &Obj,
93 ObjSectionToIDMap &LocalSections,
95
96protected:
97 size_t getGOTEntrySize() override;
98
99private:
100 SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
101
102 // Allocate no GOT entries for use in the given section.
103 uint64_t allocateGOTEntries(unsigned no);
104
105 // Find GOT entry corresponding to relocation or create new one.
106 uint64_t findOrAllocGOTEntry(const RelocationValueRef &Value,
107 unsigned GOTRelType);
108
109 // Resolve the relative 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);
113
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,
117 unsigned Type);
118
119 // Compute the address in memory where we can find the placeholder
120 void *computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const;
121
122 // Split out common case for creating the RelocationEntry for when the
123 // relocation requires no particular advanced processing.
124 void processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value);
125
126 // Return matching *LO16 relocation (Mips specific)
127 uint32_t getMatchingLoRelocation(uint32_t RelType,
128 bool IsLocal = false) const;
129
130 // The tentative ID for the GOT section
131 unsigned GOTSectionID;
132
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;
137
138protected:
139 // A map from section to a GOT section that has entries for section's GOT
140 // relocations. (Mips64 specific)
142
143private:
144 // A map to avoid duplicate got entries (Mips64 specific)
145 StringMap<uint64_t> GOTSymbolOffsets;
146
147 // *HI16 relocations will be added for resolving when we find matching
148 // *LO16 part. (Mips specific)
150
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;
155
156 // Map between GOT relocation value and corresponding GOT offset
157 std::map<RelocationValueRef, uint64_t> GOTOffsetMap;
158
159 /// The ID of the current IFunc stub section
160 unsigned IFuncStubSectionID = 0;
161 /// The current offset into the IFunc stub section
162 uint64_t IFuncStubOffset = 0;
163
164 /// A IFunc stub and its original symbol
165 struct IFuncStub {
166 /// The offset of this stub in the IFunc stub section
167 uint64_t StubOffset;
168 /// The symbol table entry of the original symbol
169 SymbolTableEntry OriginalSymbol;
170 };
171
172 /// The IFunc stubs
173 SmallVector<IFuncStub, 2> IFuncStubs;
174
175 /// Create the code for the IFunc resolver at the given address. This code
176 /// works together with the stubs created in createIFuncStub() to call the
177 /// resolver function and then jump to the real function address.
178 /// It must not be larger than 64B.
179 void createIFuncResolver(uint8_t *Addr) const;
180 /// Create the code for an IFunc stub for the IFunc that is defined in
181 /// section IFuncSectionID at offset IFuncOffset. The IFunc resolver created
182 /// by createIFuncResolver() is defined in the section IFuncStubSectionID at
183 /// offset IFuncResolverOffset. The code should be written into the section
184 /// with the id IFuncStubSectionID at the offset IFuncStubOffset.
185 void createIFuncStub(unsigned IFuncStubSectionID,
186 uint64_t IFuncResolverOffset, uint64_t IFuncStubOffset,
187 unsigned IFuncSectionID, uint64_t IFuncOffset);
188 /// Return the maximum size of a stub created by createIFuncStub()
189 unsigned getMaxIFuncStubSize() const;
190
191 void processNewSymbol(const SymbolRef &ObjSymbol,
192 SymbolTableEntry &Entry) override;
193 bool relocationNeedsGot(const RelocationRef &R) const override;
194 bool relocationNeedsStub(const RelocationRef &R) const override;
195
196 // Process a GOTTPOFF TLS relocation for x86-64
197 // NOLINTNEXTLINE(readability-identifier-naming)
198 void processX86_64GOTTPOFFRelocation(unsigned SectionID, uint64_t Offset,
200 int64_t Addend);
201 // Process a TLSLD/TLSGD relocation for x86-64
202 // NOLINTNEXTLINE(readability-identifier-naming)
203 void processX86_64TLSRelocation(unsigned SectionID, uint64_t Offset,
205 int64_t Addend,
206 const RelocationRef &GetAddrRelocation);
207
208public:
211 ~RuntimeDyldELF() override;
212
213 static std::unique_ptr<RuntimeDyldELF>
216
217 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
218 loadObject(const object::ObjectFile &O) override;
219
220 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
222 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
223 const ObjectFile &Obj,
224 ObjSectionToIDMap &ObjSectionToID,
225 StubMap &Stubs) override;
226 bool isCompatibleFile(const object::ObjectFile &Obj) const override;
227 void registerEHFrames() override;
228 Error finalizeLoad(const ObjectFile &Obj,
229 ObjSectionToIDMap &SectionMap) override;
230};
231
232} // end namespace llvm
233
234#endif // LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
This file defines the DenseMap class.
uint64_t Addr
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
Symbol resolution interface.
Definition: JITSymbol.h:371
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:2212
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
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
std::map< RelocationValueRef, uintptr_t > StubMap
RuntimeDyld::MemoryManager & MemMgr
Triple::ArchType Arch
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.
Definition: SmallVector.h:1209
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
Symbol info for RuntimeDyld.
@ aarch64_be
Definition: Triple.h:52
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
This class is the base class for all object file types.
Definition: ObjectFile.h:229
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:168
uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, uint64_t S, uint64_t LocData)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39