13#ifndef LLVM_EXECUTIONENGINE_JITLINK_AARCH64_H
14#define LLVM_EXECUTIONENGINE_JITLINK_AARCH64_H
425 constexpr uint32_t LoadStoreImm12Mask = 0x3b000000;
426 return (Instr & LoadStoreImm12Mask) == 0x39000000;
430 constexpr uint32_t TestAndBranchImm14Mask = 0x7e000000;
431 return (Instr & TestAndBranchImm14Mask) == 0x36000000;
435 constexpr uint32_t CondBranchImm19Mask = 0xfe000000;
436 return (Instr & CondBranchImm19Mask) == 0x54000000;
440 constexpr uint32_t CompAndBranchImm19Mask = 0x7e000000;
441 return (Instr & CompAndBranchImm19Mask) == 0x34000000;
445 constexpr uint32_t ADRMask = 0x9f000000;
446 return (Instr & ADRMask) == 0x10000000;
450 constexpr uint32_t LDRLitMask = 0x3b000000;
451 return (Instr & LDRLitMask) == 0x18000000;
461 constexpr uint32_t Vec128Mask = 0x04800000;
464 uint32_t ImplicitShift = Instr >> 30;
465 if (ImplicitShift == 0)
466 if ((Instr & Vec128Mask) == Vec128Mask)
469 return ImplicitShift;
477 constexpr uint32_t MoveWideImm16Mask = 0x5f9fffe0;
478 return (Instr & MoveWideImm16Mask) == 0x52800000;
487 uint32_t ImplicitShift = (Instr >> 21) & 0b11;
488 return ImplicitShift << 4;
496 const Symbol *GOTSymbol) {
497 using namespace support;
499 char *BlockWorkingMem =
B.getAlreadyMutableContent().data();
500 char *FixupPtr = BlockWorkingMem +
E.getOffset();
503 switch (
E.getKind()) {
505 uint64_t Value =
E.getTarget().getAddress().getValue() +
E.getAddend();
506 *(ulittle64_t *)FixupPtr =
Value;
510 uint64_t Value =
E.getTarget().getAddress().getValue() +
E.getAddend();
511 if (
Value > std::numeric_limits<uint32_t>::max())
513 *(ulittle32_t *)FixupPtr =
Value;
522 Value =
E.getTarget().getAddress() - FixupAddress +
E.getAddend();
524 Value = FixupAddress -
E.getTarget().getAddress() +
E.getAddend();
527 if (
Value < std::numeric_limits<int32_t>::min() ||
528 Value > std::numeric_limits<int32_t>::max())
530 *(little32_t *)FixupPtr =
Value;
532 *(little64_t *)FixupPtr =
Value;
537 "Branch-inst is not 32-bit aligned");
539 int64_t
Value =
E.getTarget().getAddress() - FixupAddress +
E.getAddend();
542 return make_error<JITLinkError>(
"BranchPCRel26 target is not 32-bit "
545 if (
Value < -(1 << 27) ||
Value > ((1 << 27) - 1))
548 uint32_t RawInstr = *(little32_t *)FixupPtr;
549 assert((RawInstr & 0x7fffffff) == 0x14000000 &&
550 "RawInstr isn't a B or BR immediate instruction");
552 uint32_t FixedInstr = RawInstr | Imm;
553 *(little32_t *)FixupPtr = FixedInstr;
558 (
E.getTarget().getAddress() +
E.getAddend()).getValue();
560 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
562 "RawInstr isn't a MOVK/MOVZ instruction");
565 uint32_t Imm = (TargetOffset >> ImmShift) & 0xffff;
566 uint32_t FixedInstr = RawInstr | (Imm << 5);
567 *(ulittle32_t *)FixupPtr = FixedInstr;
571 assert((FixupAddress.
getValue() & 0x3) == 0 &&
"LDR is not 32-bit aligned");
572 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
574 int64_t Delta =
E.getTarget().getAddress() +
E.getAddend() - FixupAddress;
576 return make_error<JITLinkError>(
"LDR literal target is not 32-bit "
578 if (!isInt<21>(Delta))
581 uint32_t FixedInstr = RawInstr | EncodedImm;
582 *(ulittle32_t *)FixupPtr = FixedInstr;
586 assert((FixupAddress.
getValue() & 0x3) == 0 &&
"ADR is not 32-bit aligned");
587 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
588 assert(
isADR(RawInstr) &&
"RawInstr is not an ADR");
589 int64_t Delta =
E.getTarget().getAddress() +
E.getAddend() - FixupAddress;
590 if (!isInt<21>(Delta))
592 auto UDelta =
static_cast<uint32_t>(Delta);
593 uint32_t EncodedImmHi = ((UDelta >> 2) & 0x7ffff) << 5;
594 uint32_t EncodedImmLo = (UDelta & 0x3) << 29;
595 uint32_t FixedInstr = RawInstr | EncodedImmHi | EncodedImmLo;
596 *(ulittle32_t *)FixupPtr = FixedInstr;
601 "Test and branch is not 32-bit aligned");
602 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
604 "RawInstr is not a test and branch");
605 int64_t Delta =
E.getTarget().getAddress() +
E.getAddend() - FixupAddress;
607 return make_error<JITLinkError>(
608 "Test and branch literal target is not 32-bit aligned");
609 if (!isInt<16>(Delta))
612 uint32_t FixedInstr = RawInstr | EncodedImm;
613 *(ulittle32_t *)FixupPtr = FixedInstr;
618 "Conditional branch is not 32-bit aligned");
619 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
621 "RawInstr is not a conditional branch");
622 int64_t Delta =
E.getTarget().getAddress() +
E.getAddend() - FixupAddress;
624 return make_error<JITLinkError>(
625 "Conditional branch literal target is not 32-bit "
627 if (!isInt<21>(Delta))
630 uint32_t FixedInstr = RawInstr | EncodedImm;
631 *(ulittle32_t *)FixupPtr = FixedInstr;
636 (
E.getTarget().getAddress().getValue() +
E.getAddend()) &
639 FixupAddress.
getValue() & ~static_cast<uint64_t>(4096 - 1);
641 int64_t PageDelta = TargetPage - PCPage;
642 if (!isInt<33>(PageDelta))
645 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
646 assert((RawInstr & 0xffffffe0) == 0x90000000 &&
647 "RawInstr isn't an ADRP instruction");
650 uint32_t FixedInstr = RawInstr | (ImmLo << 29) | (ImmHi << 5);
651 *(ulittle32_t *)FixupPtr = FixedInstr;
656 (
E.getTarget().getAddress() +
E.getAddend()).getValue() & 0xfff;
658 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
661 if (TargetOffset & ((1 << ImmShift) - 1))
662 return make_error<JITLinkError>(
"PAGEOFF12 target is not aligned");
664 uint32_t EncodedImm = (TargetOffset >> ImmShift) << 10;
665 uint32_t FixedInstr = RawInstr | EncodedImm;
666 *(ulittle32_t *)FixupPtr = FixedInstr;
670 assert(GOTSymbol &&
"No GOT section symbol");
672 (
E.getTarget().getAddress() +
E.getAddend()).getValue() -
674 if (TargetOffset > 0x7fff)
675 return make_error<JITLinkError>(
"PAGEOFF15 target is out of range");
677 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
678 const unsigned ImmShift = 3;
679 if (TargetOffset & ((1 << ImmShift) - 1))
680 return make_error<JITLinkError>(
"PAGEOFF15 target is not aligned");
682 uint32_t EncodedImm = (TargetOffset >> ImmShift) << 10;
683 uint32_t FixedInstr = RawInstr | EncodedImm;
684 *(ulittle32_t *)FixupPtr = FixedInstr;
688 return make_error<JITLinkError>(
689 "In graph " +
G.getName() +
", section " +
B.getSection().getName() +
722 Symbol *InitialTarget =
nullptr,
727 B.addEdge(
Pointer64, 0, *InitialTarget, InitialAddend);
728 return G.addAnonymousSymbol(
B, 0, 8,
false,
false);
741 B.addEdge(
Page21, 0, PointerSymbol, 0);
753 return G.addAnonymousSymbol(
779 return G.addAnonymousSymbol(
791 const char *BlockWorkingMem =
B->getContent().data();
792 const char *FixupPtr = BlockWorkingMem +
E.getOffset();
794 switch (
E.getKind()) {
806 "GOTPageOffset12/TLVPageOffset12 with non-zero addend");
807 assert((RawInstr & 0xfffffc00) == 0xf9400000 &&
808 "RawInstr isn't a 64-bit LDR immediate");
815 assert(
E.getAddend() == 0 &&
"GOTPageOffset15 with non-zero addend");
816 assert((RawInstr & 0xfffffc00) == 0xf9400000 &&
817 "RawInstr isn't a 64-bit LDR immediate");
828 "Fell through switch, but no new kind to set");
830 dbgs() <<
" Fixing " <<
G.getEdgeKindName(
E.getKind()) <<
" edge at "
831 <<
B->getFixupAddress(
E) <<
" (" <<
B->getAddress() <<
" + "
832 <<
formatv(
"{0:x}",
E.getOffset()) <<
")\n";
834 E.setKind(KindToSet);
864 dbgs() <<
" Fixing " <<
G.getEdgeKindName(
E.getKind()) <<
" edge at "
865 <<
B->getFixupAddress(
E) <<
" (" <<
B->getAddress() <<
" + "
866 <<
formatv(
"{0:x}",
E.getOffset()) <<
")\n";
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,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
StringRef - Represent a constant reference to a string, i.e.
Target - Wrapper for Target specific information.
LLVM Value Representation.
An Addressable with content and edges.
Represents fixups and constraints in the LinkGraph.
Represents an object file section.
orc::ExecutorAddr getAddress() const
Returns the address of this symbol.
A CRTP base for tables that are built on demand, e.g.
Symbol & getEntryForTarget(LinkGraph &G, Symbol &Target)
Return the constructed entry.
Global Offset Table Builder.
Symbol & createEntry(LinkGraph &G, Symbol &Target)
bool visitEdge(LinkGraph &G, Block *B, Edge &E)
static StringRef getSectionName()
Procedure Linkage Table Builder.
static StringRef getSectionName()
bool visitEdge(LinkGraph &G, Block *B, Edge &E)
PLTTableManager(GOTTableManager &GOT)
Symbol & createEntry(LinkGraph &G, Symbol &Target)
Section & getStubsSection(LinkGraph &G)
Represents an address in the executor process.
uint64_t getValue() const
const char NullPointerContent[PointerSize]
AArch64 null pointer content.
bool isLDRLiteral(uint32_t Instr)
bool isADR(uint32_t Instr)
bool isCondBranchImm19(uint32_t Instr)
const char ReentryTrampolineContent[8]
AArch64 reentry trampoline.
constexpr uint64_t PointerSize
aarch64 pointer size.
Error applyFixup(LinkGraph &G, Block &B, const Edge &E, const Symbol *GOTSymbol)
Apply fixup expression for edge to block content.
Block & createReentryTrampolineBlock(LinkGraph &G, Section &TrampolineSection, Symbol &ReentrySymbol)
Create a block of N reentry trampolines.
bool isMoveWideImm16(uint32_t Instr)
bool isCompAndBranchImm19(uint32_t Instr)
const char PointerJumpStubContent[12]
bool isLoadStoreImm12(uint32_t Instr)
Error createEmptyPointerSigningFunction(LinkGraph &G)
Creates a pointer signing function section, block, and symbol to reserve space for a signing function...
Error lowerPointer64AuthEdgesToSigningFunction(LinkGraph &G)
Given a LinkGraph containing Pointer64Authenticated edges, transform those edges to Pointer64 and add...
const char * getEdgeKindName(Edge::Kind K)
Returns a string name for the given aarch64 edge.
Block & createPointerJumpStubBlock(LinkGraph &G, Section &StubSection, Symbol &PointerSymbol)
Create a jump stub block that jumps via the pointer at the given symbol.
Symbol & createAnonymousPointer(LinkGraph &G, Section &PointerSection, Symbol *InitialTarget=nullptr, uint64_t InitialAddend=0)
Creates a new pointer block in the given section and returns an Anonymous symbol pointing to it.
const char * getPointerSigningFunctionSectionName()
Returns the name of the pointer signing function section.
Symbol & createAnonymousReentryTrampoline(LinkGraph &G, Section &TrampolineSection, Symbol &ReentrySymbol)
EdgeKind_aarch64
Represents aarch64 fixups and other aarch64-specific edge kinds.
@ LDRLiteral19
The signed 21-bit delta from the fixup to the target.
@ Pointer64Authenticated
An arm64e authenticated pointer relocation.
@ RequestGOTAndTransformToPageOffset15
A GOT entry getter/constructor, transformed to Pageoffset15 pointing at the GOT entry for the origina...
@ CondBranch19PCRel
A 19-bit PC-relative conditional branch.
@ RequestTLVPAndTransformToPageOffset12
A TLVP entry getter/constructor, transformed to PageOffset12.
@ RequestTLSDescEntryAndTransformToPageOffset12
A TLSDesc entry getter/constructor, transformed to PageOffset12.
@ Page21
The signed 21-bit delta from the fixup page to the page containing the target.
@ Branch26PCRel
A 26-bit PC-relative branch.
@ Pointer64
A plain 64-bit pointer value relocation.
@ Pointer32
A plain 32-bit pointer value relocation.
@ RequestTLVPAndTransformToPage21
A TLVP entry getter/constructor, transformed to Page21.
@ GotPageOffset15
The 15-bit offset of the GOT entry from the GOT table.
@ MoveWide16
A 16-bit slice of the target address (which slice depends on the instruction at the fixup location).
@ TestAndBranch14PCRel
A 14-bit PC-relative test and branch.
@ RequestGOTAndTransformToPage21
A GOT entry getter/constructor, transformed to Page21 pointing at the GOT entry for the original targ...
@ ADRLiteral21
The signed 21-bit delta from the fixup to the target.
@ RequestGOTAndTransformToPageOffset12
A GOT entry getter/constructor, transformed to Pageoffset12 pointing at the GOT entry for the origina...
@ NegDelta32
A 32-bit negative delta.
@ NegDelta64
A 64-bit negative delta.
@ RequestGOTAndTransformToDelta32
A GOT entry getter/constructor, transformed to Delta32 pointing at the GOT entry for the original tar...
@ PageOffset12
The 12-bit (potentially shifted) offset of the target within its page.
@ RequestTLSDescEntryAndTransformToPage21
A TLSDesc entry getter/constructor, transformed to Page21.
bool isTestAndBranchImm14(uint32_t Instr)
unsigned getPageOffset12Shift(uint32_t Instr)
unsigned getMoveWide16Shift(uint32_t Instr)
Symbol & createAnonymousPointerJumpStub(LinkGraph &G, Section &StubSection, Symbol &PointerSymbol)
Create a jump stub that jumps via the pointer at the given symbol and an anonymous symbol pointing to...
Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B, const Edge &E)
Create an out of range error for the given edge in the given block.
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.