22#define DEBUG_TYPE "jitlink"
55 return SignExtend64<22>(Imm11H << 12 | Imm11L << 1);
69 return HalfWords{S | Imm10, J1 | J2 | Imm11};
83 return SignExtend64<25>(S << 14 | I1 | I2 | Imm10 << 12 | Imm11 << 1);
92 return (
Value >> 2) & 0x00ffffff;
101 return SignExtend64<26>((
Value & 0x00ffffff) << 2);
114 return HalfWords{Imm1 << 10 | Imm4, Imm3 << 12 | Imm8};
127 uint32_t Imm16 = Imm4 << 12 | Imm1 << 11 | Imm3 << 8 | Imm8;
128 assert(Imm16 <= 0xffff &&
"Decoded value out-of-range");
158 return (Imm4 << 16) | Imm12;
169 return (Imm4 << 12) | Imm12;
197struct WritableThumbRelocation {
199 WritableThumbRelocation(
char *FixupPtr)
200 :
Hi{*reinterpret_cast<support::ulittle16_t *>(FixupPtr)},
201 Lo{*reinterpret_cast<support::ulittle16_t *>(FixupPtr + 2)} {}
207struct ThumbRelocation {
209 ThumbRelocation(
const char *FixupPtr)
214 ThumbRelocation(WritableThumbRelocation &Writable)
215 :
Hi{Writable.
Hi},
Lo(Writable.
Lo) {}
221struct WritableArmRelocation {
222 WritableArmRelocation(
char *FixupPtr)
228struct ArmRelocation {
229 ArmRelocation(
const char *FixupPtr)
232 ArmRelocation(WritableArmRelocation &Writable) :
Wd{Writable.
Wd} {}
237Error makeUnexpectedOpcodeError(
const LinkGraph &
G,
const ThumbRelocation &R,
239 return make_error<JITLinkError>(
240 formatv(
"Invalid opcode [ {0:x4}, {1:x4} ] for relocation: {2}",
242 G.getEdgeKindName(Kind)));
245Error makeUnexpectedOpcodeError(
const LinkGraph &
G,
const ArmRelocation &R,
247 return make_error<JITLinkError>(
248 formatv(
"Invalid opcode {0:x8} for relocation: {1}",
249 static_cast<uint32_t>(
R.Wd),
G.getEdgeKindName(Kind)));
252template <EdgeKind_aarch32 K>
constexpr bool isArm() {
255template <EdgeKind_aarch32 K>
constexpr bool isThumb() {
259template <EdgeKind_aarch32 K>
static bool checkOpcodeArm(
uint32_t Wd) {
260 return (
Wd & FixupInfo<K>::OpcodeMask) == FixupInfo<K>::Opcode;
263template <EdgeKind_aarch32 K>
265 return (
Hi & FixupInfo<K>::OpcodeMask.
Hi) == FixupInfo<K>::Opcode.Hi &&
266 (
Lo & FixupInfo<K>::OpcodeMask.Lo) == FixupInfo<K>::Opcode.
Lo;
269class FixupInfoTable {
274 populateEntries<FirstArmRelocation, LastArmRelocation>();
275 populateEntries<FirstThumbRelocation, LastThumbRelocation>();
279 assert(K < Data.size() &&
"Index out of bounds");
280 return Data.at(K).get();
284 template <EdgeKind_aarch32 K, EdgeKind_aarch32 LastK>
void populateEntries() {
285 assert(K < Data.size() &&
"Index out of range");
286 assert(Data.at(K) ==
nullptr &&
"Initialized entries are immutable");
287 Data[
K] = initEntry<K>();
288 if constexpr (
K < LastK) {
290 populateEntries<Next, LastK>();
294 template <EdgeKind_aarch32 K>
295 static std::unique_ptr<FixupInfoBase> initEntry() {
296 auto Entry = std::make_unique<FixupInfo<K>>();
297 static_assert(isArm<K>() != isThumb<K>(),
"Classes are mutually exclusive");
298 if constexpr (isArm<K>())
299 Entry->checkOpcode = checkOpcodeArm<K>;
300 if constexpr (isThumb<K>())
301 Entry->checkOpcode = checkOpcodeThumb<K>;
306 std::array<std::unique_ptr<FixupInfoBase>, Items> Data;
309ManagedStatic<FixupInfoTable> DynFixupInfos;
316 "Edge kind must be Arm relocation");
319 assert(
Info.checkOpcode &&
"Opcode check is mandatory for Arm edges");
320 if (!
Info.checkOpcode(R.Wd))
321 return makeUnexpectedOpcodeError(
G, R, Kind);
329 "Edge kind must be Thumb relocation");
332 assert(
Info.checkOpcode &&
"Opcode check is mandatory for Thumb edges");
333 if (!
Info.checkOpcode(R.Hi, R.Lo))
334 return makeUnexpectedOpcodeError(
G, R, Kind);
340 return DynFixupInfos->getEntry(K);
343template <EdgeKind_aarch32 Kind>
350template <EdgeKind_aarch32 Kind>
356template <EdgeKind_aarch32 Kind>
360 "Value bits exceed bit range of given mask");
361 R.Hi = (R.Hi & ~Mask.Hi) |
Reg.Hi;
362 R.Lo = (R.Lo & ~Mask.Lo) |
Reg.Lo;
365template <EdgeKind_aarch32 Kind>
368 assert((Mask &
Reg) ==
Reg &&
"Value bits exceed bit range of given mask");
369 R.Wd = (R.Wd & ~Mask) |
Reg;
372template <EdgeKind_aarch32 Kind>
375 assert((Mask.Hi & Imm.Hi) == Imm.Hi && (Mask.Lo & Imm.Lo) == Imm.Lo &&
376 "Value bits exceed bit range of given mask");
377 R.Hi = (R.Hi & ~Mask.Hi) | Imm.Hi;
378 R.Lo = (R.Lo & ~Mask.Lo) | Imm.Lo;
381template <EdgeKind_aarch32 Kind>
384 assert((Mask & Imm) == Imm &&
"Value bits exceed bit range of given mask");
385 R.Wd = (R.Wd & ~Mask) | Imm;
391 const char *BlockWorkingMem =
B.getContent().data();
392 const char *FixupPtr = BlockWorkingMem +
Offset;
402 return make_error<JITLinkError>(
403 "In graph " +
G.getName() +
", section " +
B.getSection().getName() +
404 " can not read implicit addend for aarch32 edge kind " +
405 G.getEdgeKindName(Kind));
411 ArmRelocation R(
B.getContent().data() +
Offset);
413 return std::move(Err);
425 return make_error<JITLinkError>(
426 "In graph " +
G.getName() +
", section " +
B.getSection().getName() +
427 " can not read implicit addend for aarch32 edge kind " +
428 G.getEdgeKindName(Kind));
434 ThumbRelocation R(
B.getContent().data() +
Offset);
436 return std::move(Err);
456 return make_error<JITLinkError>(
457 "In graph " +
G.getName() +
", section " +
B.getSection().getName() +
458 " can not read implicit addend for aarch32 edge kind " +
459 G.getEdgeKindName(Kind));
464 using namespace support;
466 char *BlockWorkingMem =
B.getAlreadyMutableContent().data();
467 char *FixupPtr = BlockWorkingMem +
E.getOffset();
470 uint64_t FixupAddress = (
B.getAddress() +
E.getOffset()).getValue();
471 int64_t Addend =
E.getAddend();
472 Symbol &TargetSymbol =
E.getTarget();
479 int64_t
Value = TargetAddress - FixupAddress + Addend;
480 if (!isInt<32>(
Value))
489 int64_t
Value = TargetAddress + Addend;
490 if (!isUInt<32>(
Value))
499 int64_t
Value = TargetAddress - FixupAddress + Addend;
500 if (!isInt<31>(
Value))
514 return make_error<JITLinkError>(
515 "In graph " +
G.getName() +
", section " +
B.getSection().getName() +
516 " encountered unfixable aarch32 edge kind " +
517 G.getEdgeKindName(
E.getKind()));
522 WritableArmRelocation R(
B.getAlreadyMutableContent().data() +
E.getOffset());
527 uint64_t FixupAddress = (
B.getAddress() +
E.getOffset()).getValue();
528 int64_t Addend =
E.getAddend();
529 Symbol &TargetSymbol =
E.getTarget();
535 return make_error<JITLinkError>(
"Branch relocation needs interworking "
536 "stub when bridging to Thumb: " +
539 int64_t
Value = TargetAddress - FixupAddress + Addend;
541 if (!isInt<26>(
Value))
550 return make_error<JITLinkError>(
"Relocation expects an unconditional "
551 "BL/BLX branch instruction: " +
554 int64_t
Value = TargetAddress - FixupAddress + Addend;
560 if (TargetIsThumb != InstrIsBlx) {
571 if (!isInt<26>(
Value))
588 return make_error<JITLinkError>(
589 "In graph " +
G.getName() +
", section " +
B.getSection().getName() +
590 " encountered unfixable aarch32 edge kind " +
591 G.getEdgeKindName(
E.getKind()));
597 WritableThumbRelocation R(
B.getAlreadyMutableContent().data() +
603 uint64_t FixupAddress = (
B.getAddress() +
E.getOffset()).getValue();
604 int64_t Addend =
E.getAddend();
605 Symbol &TargetSymbol =
E.getTarget();
611 return make_error<JITLinkError>(
"Branch relocation needs interworking "
612 "stub when bridging to ARM: " +
615 int64_t
Value = TargetAddress - FixupAddress + Addend;
617 if (!isInt<25>(
Value))
621 if (!isInt<22>(
Value))
630 int64_t
Value = TargetAddress - FixupAddress + Addend;
636 if (TargetIsArm != InstrIsBlx) {
650 if (!isInt<25>(
Value))
654 if (!isInt<22>(
Value))
661 "Opcode BLX implies H bit is clear (avoid UB in BLX T2)");
676 uint16_t Value = ((TargetAddress + Addend - FixupAddress) & 0xffff);
681 uint16_t Value = (((TargetAddress + Addend - FixupAddress) >> 16) & 0xffff);
687 return make_error<JITLinkError>(
688 "In graph " +
G.getName() +
", section " +
B.getSection().getName() +
689 " encountered unfixable aarch32 edge kind " +
690 G.getEdgeKindName(
E.getKind()));
702template <
size_t Size>
705 static_assert(
Size == 4,
"Pointers are 32-bit");
715 constexpr int64_t GOTEntryAddend = 0;
717 return G.addAnonymousSymbol(
B, 0,
B.getSize(),
false,
false);
722 switch (
E.getKind()) {
731 <<
" edge at " <<
B->getFixupAddress(
E) <<
" ("
732 <<
B->getAddress() <<
" + "
733 <<
formatv(
"{0:x}",
E.getOffset()) <<
") into "
734 <<
G.getEdgeKindName(KindToSet) <<
"\n");
735 E.setKind(KindToSet);
743 0x04, 0xf0, 0x1f, 0xe5,
744 0x00, 0x00, 0x00, 0x00,
748 0x00, 0xc0, 0x00, 0xe3,
749 0x00, 0xc0, 0x40, 0xe3,
750 0x1c, 0xff, 0x2f, 0xe1
754 0x40, 0xf2, 0x00, 0x0c,
755 0xc0, 0xf2, 0x00, 0x0c,
760template <
size_t Size>
778 [[maybe_unused]]
const char *StubPtr =
B.getContent().data();
780 assert(checkRegister<Thumb_MovwAbsNC>(StubPtr, Reg12) &&
781 checkRegister<Thumb_MovtAbs>(StubPtr + 4, Reg12) &&
782 "Linker generated stubs may only corrupt register r12 (IP)");
791 [[maybe_unused]]
const char *StubPtr =
B.getContent().data();
793 assert(checkRegister<Arm_MovwAbsNC>(StubPtr, Reg12) &&
794 checkRegister<Arm_MovtAbs>(StubPtr + 4, Reg12) &&
795 "Linker generated stubs may only corrupt register r12 (IP)");
803 if (!
Target.isDefined()) {
804 switch (
E.getKind()) {
818 switch (
E.getKind()) {
820 return TargetIsThumb;
822 return !TargetIsThumb;
838 if (Thumb && !Slot.ThumbEntry) {
840 &
G.addAnonymousSymbol(*Slot.B, ThumbEntrypointOffset, 4,
true,
false);
843 if (!Thumb && !Slot.ArmEntry)
845 &
G.addAnonymousSymbol(*Slot.B, ArmEntrypointOffset, 8,
true,
false);
846 return Thumb ? Slot.ThumbEntry : Slot.ArmEntry;
854 assert(
Target.hasName() &&
"Edge cannot point to anonymous target");
863 << StubsSection->
getName() <<
"\n";
872 Symbol *StubEntrypoint = getOrCreateSlotEntrypoint(
G, *Slot, UseThumb);
875 dbgs() <<
" Using " << (UseThumb ?
"Thumb" :
"Arm") <<
" entrypoint "
876 << *StubEntrypoint <<
" in "
880 E.setTarget(*StubEntrypoint);
891 LLVM_DEBUG(
dbgs() <<
" Preparing " << (MakeThumb ?
"Thumb" :
"Arm")
892 <<
" stub for " <<
G.getEdgeKindName(
E.getKind())
893 <<
" edge at " <<
B->getFixupAddress(
E) <<
" ("
894 <<
B->getAddress() <<
" + "
895 <<
formatv(
"{0:x}",
E.getOffset()) <<
")\n");
898 assert(
Target.hasName() &&
"Edge cannot point to anonymous target");
907 StubSymbol = &
G.addAnonymousSymbol(
B, 0,
B.getSize(),
true,
false);
912 dbgs() <<
" Created " << (MakeThumb ?
"Thumb" :
"Arm") <<
" entry for "
914 << *StubSymbol <<
"\n";
919 "Instruction set states of stub and relocation site should be equal");
921 dbgs() <<
" Using " << (MakeThumb ?
"Thumb" :
"Arm") <<
" entry "
922 << *StubSymbol <<
" in "
926 E.setTarget(*StubSymbol);
931#define KIND_NAME_CASE(K) \
958#define CPUARCH_NAME_CASE(K) \
962 using namespace ARMBuildAttrs;
986#undef CPUARCH_NAME_CASE
static bool isThumb(const MCSubtargetInfo &STI)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
#define LLVM_LIKELY(EXPR)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
#define CPUARCH_NAME_CASE(K)
support::ulittle16_t & Lo
support::ulittle32_t & Wd
#define KIND_NAME_CASE(K)
support::ulittle16_t & Hi
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
StringRef - Represent a constant reference to a string, i.e.
Target - Wrapper for Target specific information.
const char * getName() const
getName - Get the target name.
LLVM Value Representation.
An Addressable with content and edges.
Section & getSection() const
Return the parent section for this block.
Represents fixups and constraints in the LinkGraph.
Represents an object file section.
StringRef getName() const
Returns the name of this section.
TargetFlagsType getTargetFlags() const
Get the target flags of this Symbol.
void setTargetFlags(TargetFlagsType Flags)
Set the target flags for this Symbol.
orc::ExecutorAddr getAddress() const
Returns the address of this symbol.
Block & getBlock()
Return the Block for this Symbol (Symbol must be defined).
Symbol & getEntryForTarget(LinkGraph &G, Symbol &Target)
Return the constructed entry.
bool visitEdge(LinkGraph &G, Block *B, Edge &E)
static StringRef getSectionName()
Symbol & createEntry(LinkGraph &G, Symbol &Target)
static StringRef getSectionName()
Name of the object file section that will contain all our stubs.
bool visitEdge(LinkGraph &G, Block *B, Edge &E)
Implements link-graph traversal via visitExistingEdges()
static StringRef getSectionName()
Name of the object file section that will contain all our stubs.
bool visitEdge(LinkGraph &G, Block *B, Edge &E)
Implements link-graph traversal via visitExistingEdges().
Represents an address in the executor process.
uint64_t getValue() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
EdgeKind_aarch32
JITLink-internal AArch32 fixup kinds.
@ Data_RequestGOTAndTransformToDelta32
Create GOT entry and store offset.
@ Arm_MovtAbs
Write immediate value to the top halfword of the destination register.
@ Data_PRel31
Relative 31-bit value relocation that preserves the most-significant bit.
@ Data_Pointer32
Absolute 32-bit value relocation.
@ FirstThumbRelocation
Relocations of class Thumb16 and Thumb32 (covers Thumb instruction subset)
@ Arm_MovwAbsNC
Write immediate value to the lower halfword of the destination register.
@ Arm_Call
Write immediate value for unconditional PC-relative branch with link.
@ Thumb_MovtPrel
Write PC-relative immediate value to the top halfword of the destination register.
@ Thumb_Jump24
Write immediate value for PC-relative branch without link.
@ Arm_Jump24
Write immediate value for conditional PC-relative branch without link.
@ Thumb_MovwAbsNC
Write immediate value to the lower halfword of the destination register.
@ FirstArmRelocation
Relocations of class Arm (covers fixed-width 4-byte instruction subset)
@ Data_Delta32
Relative 32-bit value relocation.
@ Thumb_Call
Write immediate value for unconditional PC-relative branch with link.
@ Thumb_MovtAbs
Write immediate value to the top halfword of the destination register.
@ Thumb_MovwPrelNC
Write PC-relative immediate value to the lower halfword of the destination register.
static Block & createStubThumbv7(LinkGraph &G, Section &S, Symbol &Target)
Error applyFixupThumb(LinkGraph &G, Block &B, const Edge &E, const ArmConfig &ArmCfg)
Helper function to apply the fixup for Thumb-class relocations.
int64_t decodeImmBT4BlT1BlxT2(uint32_t Hi, uint32_t Lo)
Decode 22-bit immediate value for branch instructions without J1J2 range extension (formats B T4,...
static bool needsStub(const Edge &E)
HalfWords encodeRegMovtT1MovwT3(int64_t Value)
Encode register ID for instruction formats MOVT T1 and MOVW T3.
Error applyFixupData(LinkGraph &G, Block &B, const Edge &E)
Helper function to apply the fixup for Data-class relocations.
uint32_t encodeRegMovtA1MovwA2(int64_t Value)
Encode register ID for instruction formats MOVT A1 and MOVW A2.
Expected< int64_t > readAddendData(LinkGraph &G, Block &B, Edge::OffsetT Offset, Edge::Kind Kind)
Helper function to read the initial addend for Data-class relocations.
HalfWords encodeImmBT4BlT1BlxT2(int64_t Value)
Encode 22-bit immediate value for branch instructions without J1J2 range extension (formats B T4,...
int64_t decodeRegMovtT1MovwT3(uint32_t Hi, uint32_t Lo)
Decode register ID from instruction formats MOVT T1 and MOVW T3.
const char * getCPUArchName(ARMBuildAttrs::CPUArch K)
Human-readable name for a given CPU architecture kind.
uint16_t decodeImmMovtT1MovwT3(uint32_t Hi, uint32_t Lo)
Decode 16-bit immediate value from move instruction formats MOVT T1 and MOVW T3.
uint32_t encodeImmBA1BlA1BlxA2(int64_t Value)
Encode 26-bit immediate value for branch instructions (formats B A1, BL A1 and BLX A2).
uint32_t encodeImmMovtA1MovwA2(uint16_t Value)
Encode 16-bit immediate value for move instruction formats MOVT A1 and MOVW A2.
const uint8_t ArmThumbv5LdrPc[]
Error applyFixupArm(LinkGraph &G, Block &B, const Edge &E)
Helper function to apply the fixup for Arm-class relocations.
HalfWords encodeImmMovtT1MovwT3(uint16_t Value)
Encode 16-bit immediate value for move instruction formats MOVT T1 and MOVW T3.
const char * getEdgeKindName(Edge::Kind K)
Get a human-readable name for the given AArch32 edge kind.
int64_t decodeRegMovtA1MovwA2(uint64_t Value)
Decode register ID for instruction formats MOVT A1 and MOVW A2.
bool hasTargetFlags(Symbol &Sym, TargetFlagsType Flags)
Check whether the given target flags are set for this Symbol.
static Block & allocStub(LinkGraph &G, Section &S, const uint8_t(&Code)[Size])
Create a new node in the link-graph for the given stub template.
const uint8_t GOTEntryInit[]
uint16_t decodeImmMovtA1MovwA2(uint64_t Value)
Decode 16-bit immediate value for move instruction formats MOVT A1 and MOVW A2.
const uint8_t Thumbv7ABS[]
HalfWords encodeImmBT4BlT1BlxT2_J1J2(int64_t Value)
Encode 25-bit immediate value for branch instructions with J1J2 range extension (formats B T4,...
static Error checkOpcode(LinkGraph &G, const ArmRelocation &R, Edge::Kind Kind)
int64_t decodeImmBA1BlA1BlxA2(int64_t Value)
Decode 26-bit immediate value for branch instructions (formats B A1, BL A1 and BLX A2).
Expected< int64_t > readAddendThumb(LinkGraph &G, Block &B, Edge::OffsetT Offset, Edge::Kind Kind, const ArmConfig &ArmCfg)
Helper function to read the initial addend for Thumb-class relocations.
void writeRegister(WritableThumbRelocation &R, HalfWords Reg)
bool checkRegister(const ThumbRelocation &R, HalfWords Reg)
int64_t decodeImmBT4BlT1BlxT2_J1J2(uint32_t Hi, uint32_t Lo)
Decode 25-bit immediate value for branch instructions with J1J2 range extension (formats B T4,...
static Block & allocPointer(LinkGraph &G, Section &S, const uint8_t(&Content)[Size])
Create a new node in the link-graph for the given pointer value.
void writeImmediate(WritableThumbRelocation &R, HalfWords Imm)
static Block & createStubArmv7(LinkGraph &G, Section &S, Symbol &Target)
Expected< int64_t > readAddendArm(LinkGraph &G, Block &B, Edge::OffsetT Offset, Edge::Kind Kind)
Helper function to read the initial addend for Arm-class relocations.
static Block & createStubPrev7(LinkGraph &G, Section &S, Symbol &Target)
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.
const char * getGenericEdgeKindName(Edge::Kind K)
Returns the string name of the given generic edge kind, or "unknown" otherwise.
uint8_t TargetFlagsType
Holds target-specific properties for a symbol.
uint32_t read32(const void *P, endianness E)
void write32le(void *P, uint32_t V)
void write32be(void *P, uint32_t V)
uint32_t read32be(const void *P)
uint32_t read32le(const void *P)
detail::packed_endian_specific_integral< uint16_t, llvm::endianness::little, unaligned > ulittle16_t
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
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.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
JITLink sub-arch configuration for Arm CPU models.
FixupInfo checks for Arm edge kinds work on 32-bit words.
FixupInfo base class is required for dynamic lookups.
static const FixupInfoBase * getDynFixupInfo(Edge::Kind K)
FixupInfo check for Thumb32 edge kinds work on a pair of 16-bit halfwords.
Collection of named constants per fixup kind.
Immutable pair of halfwords, Hi and Lo, with overflow check.