Skip to content

Commit 3ae80f2

Browse files
lhamescompnerd
authored andcommitted
[JITLink][MachO][AArch64] More PAGEOFF12 relocation fixes.
Correctly sign extend the addend, and fix implicit shift operand decoding (it incorrectly returned 0 for some cases), and check that the initial encoded immediate is 0. (cherry picked from commit ba8683f)
1 parent b8219ea commit 3ae80f2

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class MachOLinkGraphBuilder_arm64 : public MachOLinkGraphBuilder {
262262
// If this is an Addend relocation then process it and move to the
263263
// paired reloc.
264264

265-
Addend = RI.r_symbolnum;
265+
Addend = SignExtend64(RI.r_symbolnum, 24);
266266

267267
if (RelItr == RelEnd)
268268
return make_error<JITLinkError>("Unpaired Addend reloc at " +
@@ -345,6 +345,11 @@ class MachOLinkGraphBuilder_arm64 : public MachOLinkGraphBuilder {
345345
TargetSymbol = TargetSymbolOrErr->GraphSymbol;
346346
else
347347
return TargetSymbolOrErr.takeError();
348+
uint32_t Instr = *(const ulittle32_t *)FixupContent;
349+
uint32_t EncodedAddend = (Instr & 0x003FFC00) >> 10;
350+
if (EncodedAddend != 0)
351+
return make_error<JITLinkError>("GOTPAGEOFF12 target has non-zero "
352+
"encoded addend");
348353
break;
349354
}
350355
case GOTPageOffset12: {
@@ -528,23 +533,17 @@ class MachOJITLinker_arm64 : public JITLinker<MachOJITLinker_arm64> {
528533
}
529534

530535
static unsigned getPageOffset12Shift(uint32_t Instr) {
531-
constexpr uint32_t LDRLiteralMask = 0x3ffffc00;
532-
533-
// Check for a GPR LDR immediate with a zero embedded literal.
534-
// If found, the top two bits contain the shift.
535-
if ((Instr & LDRLiteralMask) == 0x39400000)
536-
return Instr >> 30;
537-
538-
// Check for a Neon LDR immediate of size 64-bit or less with a zero
539-
// embedded literal. If found, the top two bits contain the shift.
540-
if ((Instr & LDRLiteralMask) == 0x3d400000)
541-
return Instr >> 30;
542-
543-
// Check for a Neon LDR immediate of size 128-bit with a zero embedded
544-
// literal.
545-
constexpr uint32_t SizeBitsMask = 0xc0000000;
546-
if ((Instr & (LDRLiteralMask | SizeBitsMask)) == 0x3dc00000)
547-
return 4;
536+
constexpr uint32_t LoadStoreImm12Mask = 0x3b000000;
537+
constexpr uint32_t Vec128Mask = 0x04800000;
538+
539+
if ((Instr & LoadStoreImm12Mask) == 0x39000000) {
540+
uint32_t ImplicitShift = Instr >> 30;
541+
if (ImplicitShift == 0)
542+
if ((Instr & Vec128Mask) == Vec128Mask)
543+
ImplicitShift = 4;
544+
545+
return ImplicitShift;
546+
}
548547

549548
return 0;
550549
}

llvm/test/ExecutionEngine/JITLink/AArch64/MachO_Arm64_relocations.s

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ test_gotpageoff12:
6161
# jitlink-check: decode_operand(test_page21, 1) = ((named_data + 256) - test_page21)[32:12]
6262
# jitlink-check: decode_operand(test_pageoff12add, 2) = (named_data + 256)[11:0]
6363
# jitlink-check: decode_operand(test_pageoff12gpr8, 2) = (named_data + 256)[11:0]
64+
# jitlink-cherk: decode_operand(test_pageoff12gpr8s, 2) = (named_data + 256)[11:0]
6465
# jitlink-check: decode_operand(test_pageoff12gpr16, 2) = (named_data + 256)[11:1]
66+
# jitlink-check: decode_operand(test_pageoff12gpr16s, 2) = (named_data + 256)[11:1]
6567
# jitlink-check: decode_operand(test_pageoff12gpr32, 2) = (named_data + 256)[11:2]
6668
# jitlink-check: decode_operand(test_pageoff12gpr64, 2) = (named_data + 256)[11:3]
6769
# jitlink-check: decode_operand(test_pageoff12neon8, 2) = (named_data + 256)[11:0]
@@ -82,10 +84,18 @@ test_pageoff12add:
8284
test_pageoff12gpr8:
8385
ldrb w0, [x0, named_data@PAGEOFF + 256]
8486

87+
.globl test_pageoff12gpr8s
88+
test_pageoff12gpr8s:
89+
ldrsb w0, [x0, named_data@PAGEOFF + 256]
90+
8591
.globl test_pageoff12gpr16
8692
test_pageoff12gpr16:
8793
ldrh w0, [x0, named_data@PAGEOFF + 256]
8894

95+
.globl test_pageoff12gpr16s
96+
test_pageoff12gpr16s:
97+
ldrsh w0, [x0, named_data@PAGEOFF + 256]
98+
8999
.globl test_pageoff12gpr32
90100
test_pageoff12gpr32:
91101
ldr w0, [x0, named_data@PAGEOFF + 256]

0 commit comments

Comments
 (0)