LLVM 19.0.0git
RuntimeDyldCOFFAArch64.h
Go to the documentation of this file.
1//===-- RuntimeDyldCOFFAArch64.h --- COFF/AArch64 specific code ---*- C++
2//-*-===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// COFF AArch64 support for MC-JIT runtime dynamic linker.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFAARCH64_H
15#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFAARCH64_H
16
17#include "../RuntimeDyldCOFF.h"
20#include "llvm/Object/COFF.h"
21#include "llvm/Support/Endian.h"
22
23#define DEBUG_TYPE "dyld"
24
25using namespace llvm::support::endian;
26
27namespace llvm {
28
29// This relocation type is used for handling long branch instruction
30// through the Stub.
31enum InternalRelocationType : unsigned {
33};
34
35static void add16(uint8_t *p, int16_t v) { write16le(p, read16le(p) + v); }
36static void or32le(void *P, int32_t V) { write32le(P, read32le(P) | V); }
37
38static void write32AArch64Imm(uint8_t *T, uint64_t imm, uint32_t rangeLimit) {
39 uint32_t orig = read32le(T);
40 orig &= ~(0xFFF << 10);
41 write32le(T, orig | ((imm & (0xFFF >> rangeLimit)) << 10));
42}
43
44static void write32AArch64Ldr(uint8_t *T, uint64_t imm) {
45 uint32_t orig = read32le(T);
46 uint32_t size = orig >> 30;
47 // 0x04000000 indicates SIMD/FP registers
48 // 0x00800000 indicates 128 bit
49 if ((orig & 0x04800000) == 0x04800000)
50 size += 4;
51 if ((imm & ((1 << size) - 1)) != 0)
52 assert(0 && "misaligned ldr/str offset");
53 write32AArch64Imm(T, imm >> size, size);
54}
55
56static void write32AArch64Addr(void *T, uint64_t s, uint64_t p, int shift) {
57 uint64_t Imm = (s >> shift) - (p >> shift);
58 uint32_t ImmLo = (Imm & 0x3) << 29;
59 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
60 uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
61 write32le(T, (read32le(T) & ~Mask) | ImmLo | ImmHi);
62}
63
65
66private:
67 // When a module is loaded we save the SectionID of the unwind
68 // sections in a table until we receive a request to register all
69 // unregisteredEH frame sections with the memory manager.
70 SmallVector<SID, 2> UnregisteredEHFrameSections;
71 SmallVector<SID, 2> RegisteredEHFrameSections;
72 uint64_t ImageBase;
73
74 // Fake an __ImageBase pointer by returning the section with the lowest adress
75 uint64_t getImageBase() {
76 if (!ImageBase) {
77 ImageBase = std::numeric_limits<uint64_t>::max();
78 for (const SectionEntry &Section : Sections)
79 // The Sections list may contain sections that weren't loaded for
80 // whatever reason: they may be debug sections, and ProcessAllSections
81 // is false, or they may be sections that contain 0 bytes. If the
82 // section isn't loaded, the load address will be 0, and it should not
83 // be included in the ImageBase calculation.
84 if (Section.getLoadAddress() != 0)
85 ImageBase = std::min(ImageBase, Section.getLoadAddress());
86 }
87 return ImageBase;
88 }
89
90public:
93 : RuntimeDyldCOFF(MM, Resolver, 8, COFF::IMAGE_REL_ARM64_ADDR64),
94 ImageBase(0) {}
95
96 Align getStubAlignment() override { return Align(8); }
97
98 unsigned getMaxStubSize() const override { return 20; }
99
100 std::tuple<uint64_t, uint64_t, uint64_t>
101 generateRelocationStub(unsigned SectionID, StringRef TargetName,
102 uint64_t Offset, uint64_t RelType, uint64_t Addend,
103 StubMap &Stubs) {
104 uintptr_t StubOffset;
105 SectionEntry &Section = Sections[SectionID];
106
107 RelocationValueRef OriginalRelValueRef;
108 OriginalRelValueRef.SectionID = SectionID;
109 OriginalRelValueRef.Offset = Offset;
110 OriginalRelValueRef.Addend = Addend;
111 OriginalRelValueRef.SymbolName = TargetName.data();
112
113 auto Stub = Stubs.find(OriginalRelValueRef);
114 if (Stub == Stubs.end()) {
115 LLVM_DEBUG(dbgs() << " Create a new stub function for "
116 << TargetName.data() << "\n");
117
118 StubOffset = Section.getStubOffset();
119 Stubs[OriginalRelValueRef] = StubOffset;
120 createStubFunction(Section.getAddressWithOffset(StubOffset));
121 Section.advanceStubOffset(getMaxStubSize());
122 } else {
123 LLVM_DEBUG(dbgs() << " Stub function found for " << TargetName.data()
124 << "\n");
125 StubOffset = Stub->second;
126 }
127
128 // Resolve original relocation to stub function.
129 const RelocationEntry RE(SectionID, Offset, RelType, Addend);
130 resolveRelocation(RE, Section.getLoadAddressWithOffset(StubOffset));
131
132 // adjust relocation info so resolution writes to the stub function
133 // Here an internal relocation type is used for resolving long branch via
134 // stub instruction.
135 Addend = 0;
136 Offset = StubOffset;
138
139 return std::make_tuple(Offset, RelType, Addend);
140 }
141
144 const object::ObjectFile &Obj,
145 ObjSectionToIDMap &ObjSectionToID,
146 StubMap &Stubs) override {
147
148 auto Symbol = RelI->getSymbol();
149 if (Symbol == Obj.symbol_end())
150 report_fatal_error("Unknown symbol in relocation");
151
152 Expected<StringRef> TargetNameOrErr = Symbol->getName();
153 if (!TargetNameOrErr)
154 return TargetNameOrErr.takeError();
155 StringRef TargetName = *TargetNameOrErr;
156
157 auto SectionOrErr = Symbol->getSection();
158 if (!SectionOrErr)
159 return SectionOrErr.takeError();
160 auto Section = *SectionOrErr;
161
162 uint64_t RelType = RelI->getType();
163 uint64_t Offset = RelI->getOffset();
164
165 // If there is no section, this must be an external reference.
166 bool IsExtern = Section == Obj.section_end();
167
168 // Determine the Addend used to adjust the relocation value.
169 uint64_t Addend = 0;
170 SectionEntry &AddendSection = Sections[SectionID];
171 uintptr_t ObjTarget = AddendSection.getObjAddress() + Offset;
172 uint8_t *Displacement = (uint8_t *)ObjTarget;
173
174 unsigned TargetSectionID = -1;
175 uint64_t TargetOffset = -1;
176
177 if (TargetName.starts_with(getImportSymbolPrefix())) {
178 TargetSectionID = SectionID;
179 TargetOffset = getDLLImportOffset(SectionID, Stubs, TargetName);
180 TargetName = StringRef();
181 IsExtern = false;
182 } else if (!IsExtern) {
183 if (auto TargetSectionIDOrErr = findOrEmitSection(
184 Obj, *Section, Section->isText(), ObjSectionToID))
185 TargetSectionID = *TargetSectionIDOrErr;
186 else
187 return TargetSectionIDOrErr.takeError();
188
189 TargetOffset = getSymbolOffset(*Symbol);
190 }
191
192 switch (RelType) {
197 Addend = read32le(Displacement);
198 break;
200 uint32_t orig = read32le(Displacement);
201 Addend = (orig & 0x03FFFFFF) << 2;
202
203 if (IsExtern)
204 std::tie(Offset, RelType, Addend) = generateRelocationStub(
205 SectionID, TargetName, Offset, RelType, Addend, Stubs);
206 break;
207 }
209 uint32_t orig = read32le(Displacement);
210 Addend = (orig & 0x00FFFFE0) >> 3;
211 break;
212 }
214 uint32_t orig = read32le(Displacement);
215 Addend = (orig & 0x000FFFE0) >> 3;
216 break;
217 }
220 uint32_t orig = read32le(Displacement);
221 Addend = ((orig >> 29) & 0x3) | ((orig >> 3) & 0x1FFFFC);
222 break;
223 }
226 uint32_t orig = read32le(Displacement);
227 Addend = ((orig >> 10) & 0xFFF);
228 break;
229 }
231 Addend = read64le(Displacement);
232 break;
233 }
234 default:
235 break;
236 }
237
238#if !defined(NDEBUG)
239 SmallString<32> RelTypeName;
240 RelI->getTypeName(RelTypeName);
241
242 LLVM_DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
243 << " RelType: " << RelTypeName << " TargetName: "
244 << TargetName << " Addend " << Addend << "\n");
245#endif
246
247 if (IsExtern) {
248 RelocationEntry RE(SectionID, Offset, RelType, Addend);
249 addRelocationForSymbol(RE, TargetName);
250 } else {
251 RelocationEntry RE(SectionID, Offset, RelType, TargetOffset + Addend);
252 addRelocationForSection(RE, TargetSectionID);
253 }
254 return ++RelI;
255 }
256
258 const auto Section = Sections[RE.SectionID];
259 uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
260 uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
261
262 switch (RE.RelType) {
263 default:
264 llvm_unreachable("unsupported relocation type");
266 // This relocation is ignored.
267 break;
268 }
270 // The page base of the target, for ADRP instruction.
271 Value += RE.Addend;
272 write32AArch64Addr(Target, Value, FinalAddress, 12);
273 break;
274 }
276 // The 12-bit relative displacement to the target, for instruction ADR
277 Value += RE.Addend;
278 write32AArch64Addr(Target, Value, FinalAddress, 0);
279 break;
280 }
282 // The 12-bit page offset of the target,
283 // for instructions ADD/ADDS (immediate) with zero shift.
284 Value += RE.Addend;
285 write32AArch64Imm(Target, Value & 0xFFF, 0);
286 break;
287 }
289 // The 12-bit page offset of the target,
290 // for instruction LDR (indexed, unsigned immediate).
291 Value += RE.Addend;
293 break;
294 }
296 // The 32-bit VA of the target.
297 uint32_t VA = Value + RE.Addend;
298 write32le(Target, VA);
299 break;
300 }
302 // The target's 32-bit RVA.
303 uint64_t RVA = Value + RE.Addend - getImageBase();
304 write32le(Target, RVA);
305 break;
306 }
308 // Encode the immadiate value for generated Stub instruction (MOVZ)
309 or32le(Target + 12, ((Value + RE.Addend) & 0xFFFF) << 5);
310 or32le(Target + 8, ((Value + RE.Addend) & 0xFFFF0000) >> 11);
311 or32le(Target + 4, ((Value + RE.Addend) & 0xFFFF00000000) >> 27);
312 or32le(Target + 0, ((Value + RE.Addend) & 0xFFFF000000000000) >> 43);
313 break;
314 }
316 // The 26-bit relative displacement to the target, for B and BL
317 // instructions.
318 uint64_t PCRelVal = Value + RE.Addend - FinalAddress;
319 assert(isInt<28>(PCRelVal) && "Branch target is out of range.");
320 write32le(Target, (read32le(Target) & ~(0x03FFFFFF)) |
321 (PCRelVal & 0x0FFFFFFC) >> 2);
322 break;
323 }
325 // The 19-bit offset to the relocation target,
326 // for conditional B instruction.
327 uint64_t PCRelVal = Value + RE.Addend - FinalAddress;
328 assert(isInt<21>(PCRelVal) && "Branch target is out of range.");
329 write32le(Target, (read32le(Target) & ~(0x00FFFFE0)) |
330 (PCRelVal & 0x001FFFFC) << 3);
331 break;
332 }
334 // The 14-bit offset to the relocation target,
335 // for instructions TBZ and TBNZ.
336 uint64_t PCRelVal = Value + RE.Addend - FinalAddress;
337 assert(isInt<16>(PCRelVal) && "Branch target is out of range.");
338 write32le(Target, (read32le(Target) & ~(0x000FFFE0)) |
339 (PCRelVal & 0x0000FFFC) << 3);
340 break;
341 }
343 // The 64-bit VA of the relocation target.
345 break;
346 }
348 // 16-bit section index of the section that contains the target.
349 assert(static_cast<uint32_t>(RE.SectionID) <= UINT16_MAX &&
350 "relocation overflow");
352 break;
353 }
355 // 32-bit offset of the target from the beginning of its section.
356 assert(static_cast<int64_t>(RE.Addend) <= INT32_MAX &&
357 "Relocation overflow");
358 assert(static_cast<int64_t>(RE.Addend) >= INT32_MIN &&
359 "Relocation underflow");
361 break;
362 }
364 // The 32-bit relative address from the byte following the relocation.
365 uint64_t Result = Value - FinalAddress - 4;
366 write32le(Target, Result + RE.Addend);
367 break;
368 }
369 }
370 }
371
372 void registerEHFrames() override {}
373};
374
375} // End namespace llvm
376
377#endif
#define LLVM_DEBUG(X)
Definition: Debug.h:101
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
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
RuntimeDyldCOFFAArch64(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)
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)
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
SectionEntry - represents a section emitted into memory by the dynamic linker.
uintptr_t getObjAddress() const
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
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
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_ARM64_SECREL
Definition: COFF.h:408
@ IMAGE_REL_ARM64_BRANCH19
Definition: COFF.h:415
@ IMAGE_REL_ARM64_REL32
Definition: COFF.h:417
@ IMAGE_REL_ARM64_ADDR32
Definition: COFF.h:401
@ IMAGE_REL_ARM64_SECTION
Definition: COFF.h:413
@ IMAGE_REL_ARM64_ABSOLUTE
Definition: COFF.h:400
@ IMAGE_REL_ARM64_PAGEOFFSET_12A
Definition: COFF.h:406
@ IMAGE_REL_ARM64_BRANCH14
Definition: COFF.h:416
@ IMAGE_REL_ARM64_BRANCH26
Definition: COFF.h:403
@ IMAGE_REL_ARM64_PAGEOFFSET_12L
Definition: COFF.h:407
@ IMAGE_REL_ARM64_ADDR32NB
Definition: COFF.h:402
@ IMAGE_REL_ARM64_PAGEBASE_REL21
Definition: COFF.h:404
@ IMAGE_REL_ARM64_REL21
Definition: COFF.h:405
@ IMAGE_REL_ARM64_ADDR64
Definition: COFF.h:414
uint64_t read64le(const void *P)
Definition: Endian.h:412
uint16_t read16le(const void *P)
Definition: Endian.h:406
void write64le(void *P, uint64_t V)
Definition: Endian.h:455
void write32le(void *P, uint32_t V)
Definition: Endian.h:452
void write16le(void *P, uint16_t V)
Definition: Endian.h:449
uint32_t read32le(const void *P)
Definition: Endian.h:409
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1689
@ INTERNAL_REL_ARM64_LONG_BRANCH26
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
static void write32AArch64Ldr(uint8_t *T, uint64_t imm)
static void or32le(void *P, int32_t V)
static void add16(uint8_t *p, int16_t v)
static void write32AArch64Imm(uint8_t *T, uint64_t imm, uint32_t rangeLimit)
static void write32AArch64Addr(void *T, uint64_t s, uint64_t p, int shift)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39