LLVM 19.0.0git
RuntimeDyldCOFFX86_64.h
Go to the documentation of this file.
1//===-- RuntimeDyldCOFFX86_64.h --- COFF/X86_64 specific code ---*- 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// COFF x86_x64 support for MC-JIT runtime dynamic linker.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFF86_64_H
14#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFF86_64_H
15
16#include "../RuntimeDyldCOFF.h"
18#include "llvm/Object/COFF.h"
19
20#define DEBUG_TYPE "dyld"
21
22namespace llvm {
23
25
26private:
27 // When a module is loaded we save the SectionID of the unwind
28 // sections in a table until we receive a request to register all
29 // unregisteredEH frame sections with the memory manager.
30 SmallVector<SID, 2> UnregisteredEHFrameSections;
31 SmallVector<SID, 2> RegisteredEHFrameSections;
32 uint64_t ImageBase;
33
34 // Fake an __ImageBase pointer by returning the section with the lowest adress
35 uint64_t getImageBase() {
36 if (!ImageBase) {
37 ImageBase = std::numeric_limits<uint64_t>::max();
38 for (const SectionEntry &Section : Sections)
39 // The Sections list may contain sections that weren't loaded for
40 // whatever reason: they may be debug sections, and ProcessAllSections
41 // is false, or they may be sections that contain 0 bytes. If the
42 // section isn't loaded, the load address will be 0, and it should not
43 // be included in the ImageBase calculation.
44 if (Section.getLoadAddress() != 0)
45 ImageBase = std::min(ImageBase, Section.getLoadAddress());
46 }
47 return ImageBase;
48 }
49
50 void write32BitOffset(uint8_t *Target, int64_t Addend, uint64_t Delta) {
51 uint64_t Result = Addend + Delta;
52 assert(Result <= UINT32_MAX && "Relocation overflow");
53 writeBytesUnaligned(Result, Target, 4);
54 }
55
56public:
59 : RuntimeDyldCOFF(MM, Resolver, 8, COFF::IMAGE_REL_AMD64_ADDR64),
60 ImageBase(0) {}
61
62 Align getStubAlignment() override { return Align(1); }
63
64 // 2-byte jmp instruction + 32-bit relative address + 64-bit absolute jump
65 unsigned getMaxStubSize() const override { return 14; }
66
67 // The target location for the relocation is described by RE.SectionID and
68 // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
69 // SectionEntry has three members describing its location.
70 // SectionEntry::Address is the address at which the section has been loaded
71 // into memory in the current (host) process. SectionEntry::LoadAddress is
72 // the address that the section will have in the target process.
73 // SectionEntry::ObjAddress is the address of the bits for this section in the
74 // original emitted object image (also in the current address space).
75 //
76 // Relocations will be applied as if the section were loaded at
77 // SectionEntry::LoadAddress, but they will be applied at an address based
78 // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer
79 // to Target memory contents if they are required for value calculations.
80 //
81 // The Value parameter here is the load address of the symbol for the
82 // relocation to be applied. For relocations which refer to symbols in the
83 // current object Value will be the LoadAddress of the section in which
84 // the symbol resides (RE.Addend provides additional information about the
85 // symbol location). For external symbols, Value will be the address of the
86 // symbol in the target address space.
87 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
88 const SectionEntry &Section = Sections[RE.SectionID];
89 uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
90
91 switch (RE.RelType) {
92
99 uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
100 // Delta is the distance from the start of the reloc to the end of the
101 // instruction with the reloc.
103 Value -= FinalAddress + Delta;
104 uint64_t Result = Value + RE.Addend;
105 assert(((int64_t)Result <= INT32_MAX) && "Relocation overflow");
106 assert(((int64_t)Result >= INT32_MIN) && "Relocation underflow");
107 writeBytesUnaligned(Result, Target, 4);
108 break;
109 }
110
112 // ADDR32NB requires an offset less than 2GB from 'ImageBase'.
113 // The MemoryManager can make sure this is always true by forcing the
114 // memory layout to be: CodeSection < ReadOnlySection < ReadWriteSection.
115 const uint64_t ImageBase = getImageBase();
116 if (Value < ImageBase || ((Value - ImageBase) > UINT32_MAX))
117 report_fatal_error("IMAGE_REL_AMD64_ADDR32NB relocation requires an "
118 "ordered section layout");
119 else {
120 write32BitOffset(Target, RE.Addend, Value - ImageBase);
121 }
122 break;
123 }
124
127 break;
128 }
129
131 assert(static_cast<int64_t>(RE.Addend) <= INT32_MAX && "Relocation overflow");
132 assert(static_cast<int64_t>(RE.Addend) >= INT32_MIN && "Relocation underflow");
134 break;
135 }
136
138 assert(static_cast<int16_t>(RE.SectionID) <= INT16_MAX && "Relocation overflow");
139 assert(static_cast<int16_t>(RE.SectionID) >= INT16_MIN && "Relocation underflow");
141 break;
142 }
143
144 default:
145 llvm_unreachable("Relocation type not implemented yet!");
146 break;
147 }
148 }
149
150 std::tuple<uint64_t, uint64_t, uint64_t>
151 generateRelocationStub(unsigned SectionID, StringRef TargetName,
152 uint64_t Offset, uint64_t RelType, uint64_t Addend,
153 StubMap &Stubs) {
154 uintptr_t StubOffset;
155 SectionEntry &Section = Sections[SectionID];
156
157 RelocationValueRef OriginalRelValueRef;
158 OriginalRelValueRef.SectionID = SectionID;
159 OriginalRelValueRef.Offset = Offset;
160 OriginalRelValueRef.Addend = Addend;
161 OriginalRelValueRef.SymbolName = TargetName.data();
162
163 auto Stub = Stubs.find(OriginalRelValueRef);
164 if (Stub == Stubs.end()) {
165 LLVM_DEBUG(dbgs() << " Create a new stub function for "
166 << TargetName.data() << "\n");
167
168 StubOffset = Section.getStubOffset();
169 Stubs[OriginalRelValueRef] = StubOffset;
170 createStubFunction(Section.getAddressWithOffset(StubOffset));
171 Section.advanceStubOffset(getMaxStubSize());
172 } else {
173 LLVM_DEBUG(dbgs() << " Stub function found for " << TargetName.data()
174 << "\n");
175 StubOffset = Stub->second;
176 }
177
178 // FIXME: If RelType == COFF::IMAGE_REL_AMD64_ADDR32NB we should be able
179 // to ignore the __ImageBase requirement and just forward to the stub
180 // directly as an offset of this section:
181 // write32BitOffset(Section.getAddressWithOffset(Offset), 0, StubOffset);
182 // .xdata exception handler's aren't having this though.
183
184 // Resolve original relocation to stub function.
185 const RelocationEntry RE(SectionID, Offset, RelType, Addend);
186 resolveRelocation(RE, Section.getLoadAddressWithOffset(StubOffset));
187
188 // adjust relocation info so resolution writes to the stub function
189 Addend = 0;
190 Offset = StubOffset + 6;
192
193 return std::make_tuple(Offset, RelType, Addend);
194 }
195
197 processRelocationRef(unsigned SectionID,
199 const object::ObjectFile &Obj,
200 ObjSectionToIDMap &ObjSectionToID,
201 StubMap &Stubs) override {
202 // If possible, find the symbol referred to in the relocation,
203 // and the section that contains it.
204 object::symbol_iterator Symbol = RelI->getSymbol();
205 if (Symbol == Obj.symbol_end())
206 report_fatal_error("Unknown symbol in relocation");
207 auto SectionOrError = Symbol->getSection();
208 if (!SectionOrError)
209 return SectionOrError.takeError();
210 object::section_iterator SecI = *SectionOrError;
211 // If there is no section, this must be an external reference.
212 bool IsExtern = SecI == Obj.section_end();
213
214 // Determine the Addend used to adjust the relocation value.
215 uint64_t RelType = RelI->getType();
216 uint64_t Offset = RelI->getOffset();
217 uint64_t Addend = 0;
218 SectionEntry &Section = Sections[SectionID];
219 uintptr_t ObjTarget = Section.getObjAddress() + Offset;
220
221 Expected<StringRef> TargetNameOrErr = Symbol->getName();
222 if (!TargetNameOrErr)
223 return TargetNameOrErr.takeError();
224
225 StringRef TargetName = *TargetNameOrErr;
226 unsigned TargetSectionID = 0;
227 uint64_t TargetOffset = 0;
228
229 if (TargetName.starts_with(getImportSymbolPrefix())) {
230 assert(IsExtern && "DLLImport not marked extern?");
231 TargetSectionID = SectionID;
232 TargetOffset = getDLLImportOffset(SectionID, Stubs, TargetName);
233 TargetName = StringRef();
234 IsExtern = false;
235 } else if (!IsExtern) {
236 if (auto TargetSectionIDOrErr =
237 findOrEmitSection(Obj, *SecI, SecI->isText(), ObjSectionToID))
238 TargetSectionID = *TargetSectionIDOrErr;
239 else
240 return TargetSectionIDOrErr.takeError();
241 TargetOffset = getSymbolOffset(*Symbol);
242 }
243
244 switch (RelType) {
245
253 uint8_t *Displacement = (uint8_t *)ObjTarget;
254 Addend = readBytesUnaligned(Displacement, 4);
255
256 if (IsExtern)
257 std::tie(Offset, RelType, Addend) = generateRelocationStub(
258 SectionID, TargetName, Offset, RelType, Addend, Stubs);
259
260 break;
261 }
262
264 uint8_t *Displacement = (uint8_t *)ObjTarget;
265 Addend = readBytesUnaligned(Displacement, 8);
266 break;
267 }
268
269 default:
270 break;
271 }
272
273 LLVM_DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
274 << " RelType: " << RelType << " TargetName: "
275 << TargetName << " Addend " << Addend << "\n");
276
277 if (IsExtern) {
278 RelocationEntry RE(SectionID, Offset, RelType, Addend);
279 addRelocationForSymbol(RE, TargetName);
280 } else {
281 RelocationEntry RE(SectionID, Offset, RelType, TargetOffset + Addend);
282 addRelocationForSection(RE, TargetSectionID);
283 }
284
285 return ++RelI;
286 }
287
288 void registerEHFrames() override {
289 for (auto const &EHFrameSID : UnregisteredEHFrameSections) {
290 uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
291 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
292 size_t EHFrameSize = Sections[EHFrameSID].getSize();
293 MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
294 RegisteredEHFrameSections.push_back(EHFrameSID);
295 }
296 UnregisteredEHFrameSections.clear();
297 }
298
300 ObjSectionToIDMap &SectionMap) override {
301 // Look for and record the EH frame section IDs.
302 for (const auto &SectionPair : SectionMap) {
303 const object::SectionRef &Section = SectionPair.first;
304 Expected<StringRef> NameOrErr = Section.getName();
305 if (!NameOrErr)
306 return NameOrErr.takeError();
307
308 // Note unwind info is stored in .pdata but often points to .xdata
309 // with an IMAGE_REL_AMD64_ADDR32NB relocation. Using a memory manager
310 // that keeps sections ordered in relation to __ImageBase is necessary.
311 if ((*NameOrErr) == ".pdata")
312 UnregisteredEHFrameSections.push_back(SectionPair.second);
313 }
314 return Error::success();
315 }
316};
317
318} // end namespace llvm
319
320#undef DEBUG_TYPE
321
322#endif
#define LLVM_DEBUG(X)
Definition: Debug.h:101
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
Symbol resolution interface.
Definition: JITSymbol.h:371
RelocationEntry - used to represent relocations internally in the dynamic linker.
uint32_t RelType
RelType - relocation type.
uint64_t Offset
Offset - offset into the section.
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
unsigned SectionID
SectionID - the section this relocation points to.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2213
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override
A object file specific relocation resolver.
unsigned getMaxStubSize() const override
RuntimeDyldCOFFX86_64(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)
Error finalizeLoad(const object::ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
std::tuple< uint64_t, uint64_t, uint64_t > generateRelocationStub(unsigned SectionID, StringRef TargetName, uint64_t Offset, uint64_t RelType, uint64_t Addend, StubMap &Stubs)
Expected< object::relocation_iterator > processRelocationRef(unsigned SectionID, object::relocation_iterator RelI, const object::ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) override
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
uint64_t getSymbolOffset(const SymbolRef &Sym)
static constexpr StringRef getImportSymbolPrefix()
uint64_t getDLLImportOffset(unsigned SectionID, StubMap &Stubs, StringRef Name, bool SetSectionIDMinus1=false)
std::map< SectionRef, unsigned > ObjSectionToIDMap
std::map< RelocationValueRef, uintptr_t > StubMap
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
RuntimeDyld::MemoryManager & MemMgr
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size)=0
Register the EH frames with the runtime so that c++ exceptions work.
SectionEntry - represents a section emitted into memory by the dynamic linker.
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:257
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition: Value.h:74
This class is the base class for all object file types.
Definition: ObjectFile.h:229
virtual section_iterator section_end() const =0
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
bool isText() const
Whether this section contains instructions.
Definition: ObjectFile.h:549
virtual basic_symbol_iterator symbol_end() const =0
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ IMAGE_REL_AMD64_REL32
Definition: COFF.h:364
@ IMAGE_REL_AMD64_REL32_5
Definition: COFF.h:369
@ IMAGE_REL_AMD64_ADDR64
Definition: COFF.h:361
@ IMAGE_REL_AMD64_REL32_3
Definition: COFF.h:367
@ IMAGE_REL_AMD64_ADDR32NB
Definition: COFF.h:363
@ IMAGE_REL_AMD64_SECTION
Definition: COFF.h:370
@ IMAGE_REL_AMD64_REL32_2
Definition: COFF.h:366
@ IMAGE_REL_AMD64_REL32_1
Definition: COFF.h:365
@ IMAGE_REL_AMD64_SECREL
Definition: COFF.h:371
@ IMAGE_REL_AMD64_REL32_4
Definition: COFF.h:368
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39