LLVM 20.0.0git
aarch64.h
Go to the documentation of this file.
1//=== aarch64.h - Generic JITLink aarch64 edge kinds, utilities -*- 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// Generic utilities for graphs representing aarch64 objects.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_JITLINK_AARCH64_H
14#define LLVM_EXECUTIONENGINE_JITLINK_AARCH64_H
15
16#include "TableManager.h"
19
20namespace llvm {
21namespace jitlink {
22namespace aarch64 {
23
24/// Represents aarch64 fixups and other aarch64-specific edge kinds.
26
27 /// A plain 64-bit pointer value relocation.
28 ///
29 /// Fixup expression:
30 /// Fixup <- Target + Addend : uint64
31 ///
33
34 /// A plain 32-bit pointer value relocation.
35 ///
36 /// Fixup expression:
37 /// Fixup <- Target + Addend : uint32
38 ///
39 /// Errors:
40 /// - The target must reside in the low 32-bits of the address space,
41 /// otherwise an out-of-range error will be returned.
42 ///
44
45 /// A 64-bit delta.
46 ///
47 /// Delta from the fixup to the target.
48 ///
49 /// Fixup expression:
50 /// Fixup <- Target - Fixup + Addend : int64
51 ///
53
54 /// A 32-bit delta.
55 ///
56 /// Delta from the fixup to the target.
57 ///
58 /// Fixup expression:
59 /// Fixup <- Target - Fixup + Addend : int64
60 ///
61 /// Errors:
62 /// - The result of the fixup expression must fit into an int32, otherwise
63 /// an out-of-range error will be returned.
64 ///
66
67 /// A 64-bit negative delta.
68 ///
69 /// Delta from target back to the fixup.
70 ///
71 /// Fixup expression:
72 /// Fixup <- Fixup - Target + Addend : int64
73 ///
75
76 /// A 32-bit negative delta.
77 ///
78 /// Delta from the target back to the fixup.
79 ///
80 /// Fixup expression:
81 /// Fixup <- Fixup - Target + Addend : int32
82 ///
83 /// Errors:
84 /// - The result of the fixup expression must fit into an int32, otherwise
85 /// an out-of-range error will be returned.
87
88 /// A 26-bit PC-relative branch.
89 ///
90 /// Represents a PC-relative call or branch to a target within +/-128Mb. The
91 /// target must be 32-bit aligned.
92 ///
93 /// Fixup expression:
94 /// Fixup <- (Target - Fixup + Addend) >> 2 : int26
95 ///
96 /// Notes:
97 /// The '26' in the name refers to the number operand bits and follows the
98 /// naming convention used by the corresponding ELF and MachO relocations.
99 /// Since the low two bits must be zero (because of the 32-bit alignment of
100 /// the target) the operand is effectively a signed 28-bit number.
101 ///
102 ///
103 /// Errors:
104 /// - The result of the unshifted part of the fixup expression must be
105 /// 32-bit aligned otherwise an alignment error will be returned.
106 /// - The result of the fixup expression must fit into an int26 otherwise an
107 /// out-of-range error will be returned.
109
110 /// A 14-bit PC-relative test and branch.
111 ///
112 /// Represents a PC-relative test and branch to a target within +/-32Kb. The
113 /// target must be 32-bit aligned.
114 ///
115 /// Fixup expression:
116 /// Fixup <- (Target - Fixup + Addend) >> 2 : int14
117 ///
118 /// Notes:
119 /// The '14' in the name refers to the number operand bits and follows the
120 /// naming convention used by the corresponding ELF relocation.
121 /// Since the low two bits must be zero (because of the 32-bit alignment of
122 /// the target) the operand is effectively a signed 16-bit number.
123 ///
124 ///
125 /// Errors:
126 /// - The result of the unshifted part of the fixup expression must be
127 /// 32-bit aligned otherwise an alignment error will be returned.
128 /// - The result of the fixup expression must fit into an int14 otherwise an
129 /// out-of-range error will be returned.
131
132 /// A 19-bit PC-relative conditional branch.
133 ///
134 /// Represents a PC-relative conditional branch to a target within +/-1Mb. The
135 /// target must be 32-bit aligned.
136 ///
137 /// Fixup expression:
138 /// Fixup <- (Target - Fixup + Addend) >> 2 : int19
139 ///
140 /// Notes:
141 /// The '19' in the name refers to the number operand bits and follows the
142 /// naming convention used by the corresponding ELF relocation.
143 /// Since the low two bits must be zero (because of the 32-bit alignment of
144 /// the target) the operand is effectively a signed 21-bit number.
145 ///
146 ///
147 /// Errors:
148 /// - The result of the unshifted part of the fixup expression must be
149 /// 32-bit aligned otherwise an alignment error will be returned.
150 /// - The result of the fixup expression must fit into an int19 otherwise an
151 /// out-of-range error will be returned.
153
154 /// A 16-bit slice of the target address (which slice depends on the
155 /// instruction at the fixup location).
156 ///
157 /// Used to fix up MOVK/MOVN/MOVZ instructions.
158 ///
159 /// Fixup expression:
160 ///
161 /// Fixup <- (Target + Addend) >> Shift : uint16
162 ///
163 /// where Shift is encoded in the instruction at the fixup location.
164 ///
166
167 /// The signed 21-bit delta from the fixup to the target.
168 ///
169 /// Typically used to load a pointers at a PC-relative offset of +/- 1Mb. The
170 /// target must be 32-bit aligned.
171 ///
172 /// Fixup expression:
173 ///
174 /// Fixup <- (Target - Fixup + Addend) >> 2 : int19
175 ///
176 /// Notes:
177 /// The '19' in the name refers to the number operand bits and follows the
178 /// naming convention used by the corresponding ELF relocation.
179 /// Since the low two bits must be zero (because of the 32-bit alignment of
180 /// the target) the operand is effectively a signed 21-bit number.
181 ///
182 ///
183 /// Errors:
184 /// - The result of the unshifted part of the fixup expression must be
185 /// 32-bit aligned otherwise an alignment error will be returned.
186 /// - The result of the fixup expression must fit into an int19 or an
187 /// out-of-range error will be returned.
189
190 /// The signed 21-bit delta from the fixup to the target.
191 ///
192 /// Fixup expression:
193 ///
194 /// Fixup <- Target - Fixup + Addend : int21
195 ///
196 /// Notes:
197 /// For ADR fixups.
198 ///
199 /// Errors:
200 /// - The result of the fixup expression must fit into an int21 otherwise an
201 /// out-of-range error will be returned.
203
204 /// The signed 21-bit delta from the fixup page to the page containing the
205 /// target.
206 ///
207 /// Fixup expression:
208 ///
209 /// Fixup <- (((Target + Addend) & ~0xfff) - (Fixup & ~0xfff)) >> 12 : int21
210 ///
211 /// Notes:
212 /// For ADRP fixups.
213 ///
214 /// Errors:
215 /// - The result of the fixup expression must fit into an int21 otherwise an
216 /// out-of-range error will be returned.
218
219 /// The 12-bit (potentially shifted) offset of the target within its page.
220 ///
221 /// Typically used to fix up LDR immediates.
222 ///
223 /// Fixup expression:
224 ///
225 /// Fixup <- ((Target + Addend) >> Shift) & 0xfff : uint12
226 ///
227 /// where Shift is encoded in the size field of the instruction.
228 ///
229 /// Errors:
230 /// - The result of the unshifted part of the fixup expression must be
231 /// aligned otherwise an alignment error will be returned.
232 /// - The result of the fixup expression must fit into a uint12 otherwise an
233 /// out-of-range error will be returned.
235
236 /// The 15-bit offset of the GOT entry from the GOT table.
237 ///
238 /// Used for load/store instructions addressing a GOT entry.
239 ///
240 /// Fixup expression:
241 ///
242 /// Fixup <- ((Target + Addend - Page(GOT))) & 0x7fff) >> 3 : uint12
243 ///
244 /// Errors:
245 /// - The result of the unshifted part of the fixup expression must be
246 /// aligned otherwise an alignment error will be returned.
247 /// - The result of the fixup expression must fit into a uint12 otherwise an
248 /// out-of-range error will be returned.
250
251 /// A GOT entry getter/constructor, transformed to Page21 pointing at the GOT
252 /// entry for the original target.
253 ///
254 /// Indicates that this edge should be transformed into a Page21 targeting
255 /// the GOT entry for the edge's current target, maintaining the same addend.
256 /// A GOT entry for the target should be created if one does not already
257 /// exist.
258 ///
259 /// Edges of this kind are usually handled by a GOT builder pass inserted by
260 /// default.
261 ///
262 /// Fixup expression:
263 /// NONE
264 ///
265 /// Errors:
266 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
267 /// phase will result in an assert/unreachable during the fixup phase.
268 ///
270
271 /// A GOT entry getter/constructor, transformed to Pageoffset12 pointing at
272 /// the GOT entry for the original target.
273 ///
274 /// Indicates that this edge should be transformed into a PageOffset12
275 /// targeting the GOT entry for the edge's current target, maintaining the
276 /// same addend. A GOT entry for the target should be created if one does not
277 /// already exist.
278 ///
279 /// Edges of this kind are usually handled by a GOT builder pass inserted by
280 /// default.
281 ///
282 /// Fixup expression:
283 /// NONE
284 ///
285 /// Errors:
286 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
287 /// phase will result in an assert/unreachable during the fixup phase.
288 ///
290
291 /// A GOT entry getter/constructor, transformed to Pageoffset15 pointing at
292 /// the GOT entry for the original target.
293 ///
294 /// Indicates that this edge should be transformed into a GotPageOffset15
295 /// targeting the GOT entry for the edge's current target, maintaining the
296 /// same addend. A GOT entry for the target should be created if one does not
297 /// already exist.
298 ///
299 /// Fixup expression:
300 /// NONE
301 ///
302 /// Errors:
303 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
304 /// phase will result in an assert/unreachable during the fixup phase.
305 ///
307
308 /// A GOT entry getter/constructor, transformed to Delta32 pointing at the GOT
309 /// entry for the original target.
310 ///
311 /// Indicates that this edge should be transformed into a Delta32/ targeting
312 /// the GOT entry for the edge's current target, maintaining the same addend.
313 /// A GOT entry for the target should be created if one does not already
314 /// exist.
315 ///
316 /// Edges of this kind are usually handled by a GOT builder pass inserted by
317 /// default.
318 ///
319 /// Fixup expression:
320 /// NONE
321 ///
322 /// Errors:
323 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
324 /// phase will result in an assert/unreachable during the fixup phase.
325 ///
327
328 /// A TLVP entry getter/constructor, transformed to Page21.
329 ///
330 /// Indicates that this edge should be transformed into a Page21 targeting the
331 /// TLVP entry for the edge's current target. A TLVP entry for the target
332 /// should be created if one does not already exist.
333 ///
334 /// Fixup expression:
335 /// NONE
336 ///
337 /// Errors:
338 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
339 /// phase will result in an assert/unreachable during the fixup phase.
340 ///
342
343 /// A TLVP entry getter/constructor, transformed to PageOffset12.
344 ///
345 /// Indicates that this edge should be transformed into a PageOffset12
346 /// targeting the TLVP entry for the edge's current target. A TLVP entry for
347 /// the target should be created if one does not already exist.
348 ///
349 /// Fixup expression:
350 /// NONE
351 ///
352 /// Errors:
353 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
354 /// phase will result in an assert/unreachable during the fixup phase.
355 ///
357
358 /// A TLSDesc entry getter/constructor, transformed to Page21.
359 ///
360 /// Indicates that this edge should be transformed into a Page21 targeting the
361 /// TLSDesc entry for the edge's current target. A TLSDesc entry for the
362 /// target should be created if one does not already exist.
363 ///
364 /// Fixup expression:
365 /// NONE
366 ///
367 /// Errors:
368 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
369 /// phase will result in an assert/unreachable during the fixup phase.
370 ///
372
373 /// A TLSDesc entry getter/constructor, transformed to PageOffset12.
374 ///
375 /// Indicates that this edge should be transformed into a PageOffset12
376 /// targeting the TLSDesc entry for the edge's current target. A TLSDesc entry
377 /// for the target should be created if one does not already exist.
378 ///
379 /// Fixup expression:
380 /// NONE
381 ///
382 /// Errors:
383 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
384 /// phase will result in an assert/unreachable during the fixup phase.
385 ///
387};
388
389/// Returns a string name for the given aarch64 edge. For debugging purposes
390/// only
391const char *getEdgeKindName(Edge::Kind K);
392
393// Returns whether the Instr is LD/ST (imm12)
394inline bool isLoadStoreImm12(uint32_t Instr) {
395 constexpr uint32_t LoadStoreImm12Mask = 0x3b000000;
396 return (Instr & LoadStoreImm12Mask) == 0x39000000;
397}
398
399inline bool isTestAndBranchImm14(uint32_t Instr) {
400 constexpr uint32_t TestAndBranchImm14Mask = 0x7e000000;
401 return (Instr & TestAndBranchImm14Mask) == 0x36000000;
402}
403
404inline bool isCondBranchImm19(uint32_t Instr) {
405 constexpr uint32_t CondBranchImm19Mask = 0xfe000000;
406 return (Instr & CondBranchImm19Mask) == 0x54000000;
407}
408
409inline bool isCompAndBranchImm19(uint32_t Instr) {
410 constexpr uint32_t CompAndBranchImm19Mask = 0x7e000000;
411 return (Instr & CompAndBranchImm19Mask) == 0x34000000;
412}
413
414inline bool isADR(uint32_t Instr) {
415 constexpr uint32_t ADRMask = 0x9f000000;
416 return (Instr & ADRMask) == 0x10000000;
417}
418
419inline bool isLDRLiteral(uint32_t Instr) {
420 constexpr uint32_t LDRLitMask = 0x3b000000;
421 return (Instr & LDRLitMask) == 0x18000000;
422}
423
424// Returns the amount the address operand of LD/ST (imm12)
425// should be shifted right by.
426//
427// The shift value varies by the data size of LD/ST instruction.
428// For instance, LDH instructoin needs the address to be shifted
429// right by 1.
430inline unsigned getPageOffset12Shift(uint32_t Instr) {
431 constexpr uint32_t Vec128Mask = 0x04800000;
432
433 if (isLoadStoreImm12(Instr)) {
434 uint32_t ImplicitShift = Instr >> 30;
435 if (ImplicitShift == 0)
436 if ((Instr & Vec128Mask) == Vec128Mask)
437 ImplicitShift = 4;
438
439 return ImplicitShift;
440 }
441
442 return 0;
443}
444
445// Returns whether the Instr is MOVK/MOVZ (imm16) with a zero immediate field
446inline bool isMoveWideImm16(uint32_t Instr) {
447 constexpr uint32_t MoveWideImm16Mask = 0x5f9fffe0;
448 return (Instr & MoveWideImm16Mask) == 0x52800000;
449}
450
451// Returns the amount the address operand of MOVK/MOVZ (imm16)
452// should be shifted right by.
453//
454// The shift value is specfied in the assembly as LSL #<shift>.
455inline unsigned getMoveWide16Shift(uint32_t Instr) {
456 if (isMoveWideImm16(Instr)) {
457 uint32_t ImplicitShift = (Instr >> 21) & 0b11;
458 return ImplicitShift << 4;
459 }
460
461 return 0;
462}
463
464/// Apply fixup expression for edge to block content.
466 const Symbol *GOTSymbol) {
467 using namespace support;
468
469 char *BlockWorkingMem = B.getAlreadyMutableContent().data();
470 char *FixupPtr = BlockWorkingMem + E.getOffset();
471 orc::ExecutorAddr FixupAddress = B.getAddress() + E.getOffset();
472
473 switch (E.getKind()) {
474 case Pointer64: {
475 uint64_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
476 *(ulittle64_t *)FixupPtr = Value;
477 break;
478 }
479 case Pointer32: {
480 uint64_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
481 if (Value > std::numeric_limits<uint32_t>::max())
482 return makeTargetOutOfRangeError(G, B, E);
483 *(ulittle32_t *)FixupPtr = Value;
484 break;
485 }
486 case Delta32:
487 case Delta64:
488 case NegDelta32:
489 case NegDelta64: {
490 int64_t Value;
491 if (E.getKind() == Delta32 || E.getKind() == Delta64)
492 Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
493 else
494 Value = FixupAddress - E.getTarget().getAddress() + E.getAddend();
495
496 if (E.getKind() == Delta32 || E.getKind() == NegDelta32) {
497 if (Value < std::numeric_limits<int32_t>::min() ||
498 Value > std::numeric_limits<int32_t>::max())
499 return makeTargetOutOfRangeError(G, B, E);
500 *(little32_t *)FixupPtr = Value;
501 } else
502 *(little64_t *)FixupPtr = Value;
503 break;
504 }
505 case Branch26PCRel: {
506 assert((FixupAddress.getValue() & 0x3) == 0 &&
507 "Branch-inst is not 32-bit aligned");
508
509 int64_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
510
511 if (static_cast<uint64_t>(Value) & 0x3)
512 return make_error<JITLinkError>("BranchPCRel26 target is not 32-bit "
513 "aligned");
514
515 if (Value < -(1 << 27) || Value > ((1 << 27) - 1))
516 return makeTargetOutOfRangeError(G, B, E);
517
518 uint32_t RawInstr = *(little32_t *)FixupPtr;
519 assert((RawInstr & 0x7fffffff) == 0x14000000 &&
520 "RawInstr isn't a B or BR immediate instruction");
521 uint32_t Imm = (static_cast<uint32_t>(Value) & ((1 << 28) - 1)) >> 2;
522 uint32_t FixedInstr = RawInstr | Imm;
523 *(little32_t *)FixupPtr = FixedInstr;
524 break;
525 }
526 case MoveWide16: {
527 uint64_t TargetOffset =
528 (E.getTarget().getAddress() + E.getAddend()).getValue();
529
530 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
531 assert(isMoveWideImm16(RawInstr) &&
532 "RawInstr isn't a MOVK/MOVZ instruction");
533
534 unsigned ImmShift = getMoveWide16Shift(RawInstr);
535 uint32_t Imm = (TargetOffset >> ImmShift) & 0xffff;
536 uint32_t FixedInstr = RawInstr | (Imm << 5);
537 *(ulittle32_t *)FixupPtr = FixedInstr;
538 break;
539 }
540 case LDRLiteral19: {
541 assert((FixupAddress.getValue() & 0x3) == 0 && "LDR is not 32-bit aligned");
542 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
543 assert(isLDRLiteral(RawInstr) && "RawInstr is not an LDR Literal");
544 int64_t Delta = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
545 if (Delta & 0x3)
546 return make_error<JITLinkError>("LDR literal target is not 32-bit "
547 "aligned");
548 if (!isInt<21>(Delta))
549 return makeTargetOutOfRangeError(G, B, E);
550 uint32_t EncodedImm = ((static_cast<uint32_t>(Delta) >> 2) & 0x7ffff) << 5;
551 uint32_t FixedInstr = RawInstr | EncodedImm;
552 *(ulittle32_t *)FixupPtr = FixedInstr;
553 break;
554 }
555 case ADRLiteral21: {
556 assert((FixupAddress.getValue() & 0x3) == 0 && "ADR is not 32-bit aligned");
557 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
558 assert(isADR(RawInstr) && "RawInstr is not an ADR");
559 int64_t Delta = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
560 if (!isInt<21>(Delta))
561 return makeTargetOutOfRangeError(G, B, E);
562 auto UDelta = static_cast<uint32_t>(Delta);
563 uint32_t EncodedImmHi = ((UDelta >> 2) & 0x7ffff) << 5;
564 uint32_t EncodedImmLo = (UDelta & 0x3) << 29;
565 uint32_t FixedInstr = RawInstr | EncodedImmHi | EncodedImmLo;
566 *(ulittle32_t *)FixupPtr = FixedInstr;
567 break;
568 }
570 assert((FixupAddress.getValue() & 0x3) == 0 &&
571 "Test and branch is not 32-bit aligned");
572 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
573 assert(isTestAndBranchImm14(RawInstr) &&
574 "RawInstr is not a test and branch");
575 int64_t Delta = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
576 if (Delta & 0x3)
577 return make_error<JITLinkError>(
578 "Test and branch literal target is not 32-bit aligned");
579 if (!isInt<16>(Delta))
580 return makeTargetOutOfRangeError(G, B, E);
581 uint32_t EncodedImm = ((static_cast<uint32_t>(Delta) >> 2) & 0x3fff) << 5;
582 uint32_t FixedInstr = RawInstr | EncodedImm;
583 *(ulittle32_t *)FixupPtr = FixedInstr;
584 break;
585 }
586 case CondBranch19PCRel: {
587 assert((FixupAddress.getValue() & 0x3) == 0 &&
588 "Conditional branch is not 32-bit aligned");
589 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
590 assert((isCondBranchImm19(RawInstr) || isCompAndBranchImm19(RawInstr)) &&
591 "RawInstr is not a conditional branch");
592 int64_t Delta = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
593 if (Delta & 0x3)
594 return make_error<JITLinkError>(
595 "Conditional branch literal target is not 32-bit "
596 "aligned");
597 if (!isInt<21>(Delta))
598 return makeTargetOutOfRangeError(G, B, E);
599 uint32_t EncodedImm = ((static_cast<uint32_t>(Delta) >> 2) & 0x7ffff) << 5;
600 uint32_t FixedInstr = RawInstr | EncodedImm;
601 *(ulittle32_t *)FixupPtr = FixedInstr;
602 break;
603 }
604 case Page21: {
605 uint64_t TargetPage =
606 (E.getTarget().getAddress().getValue() + E.getAddend()) &
607 ~static_cast<uint64_t>(4096 - 1);
608 uint64_t PCPage =
609 FixupAddress.getValue() & ~static_cast<uint64_t>(4096 - 1);
610
611 int64_t PageDelta = TargetPage - PCPage;
612 if (!isInt<33>(PageDelta))
613 return makeTargetOutOfRangeError(G, B, E);
614
615 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
616 assert((RawInstr & 0xffffffe0) == 0x90000000 &&
617 "RawInstr isn't an ADRP instruction");
618 uint32_t ImmLo = (static_cast<uint64_t>(PageDelta) >> 12) & 0x3;
619 uint32_t ImmHi = (static_cast<uint64_t>(PageDelta) >> 14) & 0x7ffff;
620 uint32_t FixedInstr = RawInstr | (ImmLo << 29) | (ImmHi << 5);
621 *(ulittle32_t *)FixupPtr = FixedInstr;
622 break;
623 }
624 case PageOffset12: {
625 uint64_t TargetOffset =
626 (E.getTarget().getAddress() + E.getAddend()).getValue() & 0xfff;
627
628 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
629 unsigned ImmShift = getPageOffset12Shift(RawInstr);
630
631 if (TargetOffset & ((1 << ImmShift) - 1))
632 return make_error<JITLinkError>("PAGEOFF12 target is not aligned");
633
634 uint32_t EncodedImm = (TargetOffset >> ImmShift) << 10;
635 uint32_t FixedInstr = RawInstr | EncodedImm;
636 *(ulittle32_t *)FixupPtr = FixedInstr;
637 break;
638 }
639 case GotPageOffset15: {
640 assert(GOTSymbol && "No GOT section symbol");
641 uint64_t TargetOffset =
642 (E.getTarget().getAddress() + E.getAddend()).getValue() -
643 (GOTSymbol->getAddress().getValue() & ~static_cast<uint64_t>(4096 - 1));
644 if (TargetOffset > 0x7fff)
645 return make_error<JITLinkError>("PAGEOFF15 target is out of range");
646
647 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
648 const unsigned ImmShift = 3;
649 if (TargetOffset & ((1 << ImmShift) - 1))
650 return make_error<JITLinkError>("PAGEOFF15 target is not aligned");
651
652 uint32_t EncodedImm = (TargetOffset >> ImmShift) << 10;
653 uint32_t FixedInstr = RawInstr | EncodedImm;
654 *(ulittle32_t *)FixupPtr = FixedInstr;
655 break;
656 }
657 default:
658 return make_error<JITLinkError>(
659 "In graph " + G.getName() + ", section " + B.getSection().getName() +
660 " unsupported edge kind " + getEdgeKindName(E.getKind()));
661 }
662
663 return Error::success();
664}
665
666/// aarch64 pointer size.
667constexpr uint64_t PointerSize = 8;
668
669/// AArch64 null pointer content.
670extern const char NullPointerContent[PointerSize];
671
672/// AArch64 pointer jump stub content.
673///
674/// Contains the instruction sequence for an indirect jump via an in-memory
675/// pointer:
676/// ADRP x16, ptr@page21
677/// LDR x16, [x16, ptr@pageoff12]
678/// BR x16
679extern const char PointerJumpStubContent[12];
680
681/// Creates a new pointer block in the given section and returns an
682/// Anonymous symbol pointing to it.
683///
684/// If InitialTarget is given then an Pointer64 relocation will be added to the
685/// block pointing at InitialTarget.
686///
687/// The pointer block will have the following default values:
688/// alignment: 64-bit
689/// alignment-offset: 0
690/// address: highest allowable (~7U)
692 Symbol *InitialTarget = nullptr,
693 uint64_t InitialAddend = 0) {
694 auto &B = G.createContentBlock(PointerSection, NullPointerContent,
695 orc::ExecutorAddr(~uint64_t(7)), 8, 0);
696 if (InitialTarget)
697 B.addEdge(Pointer64, 0, *InitialTarget, InitialAddend);
698 return G.addAnonymousSymbol(B, 0, 8, false, false);
699}
700
701/// Create a jump stub block that jumps via the pointer at the given symbol.
702///
703/// The stub block will have the following default values:
704/// alignment: 32-bit
705/// alignment-offset: 0
706/// address: highest allowable: (~11U)
708 Symbol &PointerSymbol) {
709 auto &B = G.createContentBlock(StubSection, PointerJumpStubContent,
710 orc::ExecutorAddr(~uint64_t(11)), 4, 0);
711 B.addEdge(Page21, 0, PointerSymbol, 0);
712 B.addEdge(PageOffset12, 4, PointerSymbol, 0);
713 return B;
714}
715
716/// Create a jump stub that jumps via the pointer at the given symbol and
717/// an anonymous symbol pointing to it. Return the anonymous symbol.
718///
719/// The stub block will be created by createPointerJumpStubBlock.
721 Section &StubSection,
722 Symbol &PointerSymbol) {
723 return G.addAnonymousSymbol(
724 createPointerJumpStubBlock(G, StubSection, PointerSymbol), 0,
725 sizeof(PointerJumpStubContent), true, false);
726}
727
728/// Global Offset Table Builder.
729class GOTTableManager : public TableManager<GOTTableManager> {
730public:
731 static StringRef getSectionName() { return "$__GOT"; }
732
734 Edge::Kind KindToSet = Edge::Invalid;
735 const char *BlockWorkingMem = B->getContent().data();
736 const char *FixupPtr = BlockWorkingMem + E.getOffset();
737
738 switch (E.getKind()) {
741 KindToSet = aarch64::Page21;
742 break;
743 }
746 KindToSet = aarch64::PageOffset12;
747 uint32_t RawInstr = *(const support::ulittle32_t *)FixupPtr;
748 (void)RawInstr;
749 assert(E.getAddend() == 0 &&
750 "GOTPageOffset12/TLVPageOffset12 with non-zero addend");
751 assert((RawInstr & 0xfffffc00) == 0xf9400000 &&
752 "RawInstr isn't a 64-bit LDR immediate");
753 break;
754 }
756 KindToSet = aarch64::GotPageOffset15;
757 uint32_t RawInstr = *(const support::ulittle32_t *)FixupPtr;
758 (void)RawInstr;
759 assert(E.getAddend() == 0 && "GOTPageOffset15 with non-zero addend");
760 assert((RawInstr & 0xfffffc00) == 0xf9400000 &&
761 "RawInstr isn't a 64-bit LDR immediate");
762 break;
763 }
765 KindToSet = aarch64::Delta32;
766 break;
767 }
768 default:
769 return false;
770 }
771 assert(KindToSet != Edge::Invalid &&
772 "Fell through switch, but no new kind to set");
773 DEBUG_WITH_TYPE("jitlink", {
774 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
775 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
776 << formatv("{0:x}", E.getOffset()) << ")\n";
777 });
778 E.setKind(KindToSet);
779 E.setTarget(getEntryForTarget(G, E.getTarget()));
780 return true;
781 }
782
784 return createAnonymousPointer(G, getGOTSection(G), &Target);
785 }
786
787private:
788 Section &getGOTSection(LinkGraph &G) {
789 if (!GOTSection)
790 GOTSection = &G.createSection(getSectionName(),
792 return *GOTSection;
793 }
794
795 Section *GOTSection = nullptr;
796};
797
798/// Procedure Linkage Table Builder.
799class PLTTableManager : public TableManager<PLTTableManager> {
800public:
802
803 static StringRef getSectionName() { return "$__STUBS"; }
804
806 if (E.getKind() == aarch64::Branch26PCRel && !E.getTarget().isDefined()) {
807 DEBUG_WITH_TYPE("jitlink", {
808 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
809 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
810 << formatv("{0:x}", E.getOffset()) << ")\n";
811 });
812 E.setTarget(getEntryForTarget(G, E.getTarget()));
813 return true;
814 }
815 return false;
816 }
817
821 }
822
823public:
825 if (!StubsSection)
826 StubsSection = &G.createSection(getSectionName(),
828 return *StubsSection;
829 }
830
833};
834
835} // namespace aarch64
836} // namespace jitlink
837} // namespace llvm
838
839#endif // LLVM_EXECUTIONENGINE_JITLINK_AARCH64_H
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEBUG_WITH_TYPE(TYPE, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition: Debug.h:64
#define G(x, y, z)
Definition: MD5.cpp:56
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:337
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition: Value.h:74
Represents an address in the executor process.
uint64_t getValue() const
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163