Skip to content

Commit ba8683f

Browse files
committed
[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.
1 parent 4fccdd5 commit ba8683f

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
@@ -256,7 +256,7 @@ class MachOLinkGraphBuilder_arm64 : public MachOLinkGraphBuilder {
256256
// If this is an Addend relocation then process it and move to the
257257
// paired reloc.
258258

259-
Addend = RI.r_symbolnum;
259+
Addend = SignExtend64(RI.r_symbolnum, 24);
260260

261261
if (RelItr == RelEnd)
262262
return make_error<JITLinkError>("Unpaired Addend reloc at " +
@@ -340,6 +340,11 @@ class MachOLinkGraphBuilder_arm64 : public MachOLinkGraphBuilder {
340340
TargetSymbol = TargetSymbolOrErr->GraphSymbol;
341341
else
342342
return TargetSymbolOrErr.takeError();
343+
uint32_t Instr = *(const ulittle32_t *)FixupContent;
344+
uint32_t EncodedAddend = (Instr & 0x003FFC00) >> 10;
345+
if (EncodedAddend != 0)
346+
return make_error<JITLinkError>("GOTPAGEOFF12 target has non-zero "
347+
"encoded addend");
343348
break;
344349
}
345350
case GOTPageOffset12: {
@@ -524,23 +529,17 @@ class MachOJITLinker_arm64 : public JITLinker<MachOJITLinker_arm64> {
524529
}
525530

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

545544
return 0;
546545
}

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)