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