Skip to content

Commit c642fa0

Browse files
authored
AArch64: Relax restriction on discriminator when PAuth ifunc used.
When a PAuth ifunc is being used, we can represent any discriminator that we want in the code and don't need to be restricted to 16 bits. For now we only need this capability for address discriminated ptrauth expressions, so keep the restriction in place for other discriminators. Reviewers: atrosinenko, fmayer Reviewed By: fmayer Pull Request: #170945
1 parent 390f17f commit c642fa0

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class AArch64AsmPrinter : public AsmPrinter {
220220
void LowerLOADgotAUTH(const MachineInstr &MI);
221221

222222
const MCExpr *emitPAuthRelocationAsIRelative(
223-
const MCExpr *Target, uint16_t Disc, AArch64PACKey::ID KeyID,
223+
const MCExpr *Target, uint64_t Disc, AArch64PACKey::ID KeyID,
224224
bool HasAddressDiversity, bool IsDSOLocal, const MCExpr *DSExpr);
225225

226226
/// tblgen'erated driver function for lowering simple MI->MC
@@ -2461,7 +2461,7 @@ static bool targetSupportsIRelativeRelocation(const Triple &TT) {
24612461
// ret
24622462
// .popsection
24632463
const MCExpr *AArch64AsmPrinter::emitPAuthRelocationAsIRelative(
2464-
const MCExpr *Target, uint16_t Disc, AArch64PACKey::ID KeyID,
2464+
const MCExpr *Target, uint64_t Disc, AArch64PACKey::ID KeyID,
24652465
bool HasAddressDiversity, bool IsDSOLocal, const MCExpr *DSExpr) {
24662466
const Triple &TT = TM.getTargetTriple();
24672467

@@ -2509,12 +2509,16 @@ const MCExpr *AArch64AsmPrinter::emitPAuthRelocationAsIRelative(
25092509
if (HasAddressDiversity) {
25102510
auto *PlacePlusDisc = MCBinaryExpr::createAdd(
25112511
MCSymbolRefExpr::create(Place, OutStreamer->getContext()),
2512-
MCConstantExpr::create(static_cast<int16_t>(Disc),
2513-
OutStreamer->getContext()),
2512+
MCConstantExpr::create(Disc, OutStreamer->getContext()),
25142513
OutStreamer->getContext());
25152514
emitAddress(*OutStreamer, AArch64::X1, PlacePlusDisc, /*IsDSOLocal=*/true,
25162515
*STI);
25172516
} else {
2517+
if (!isUInt<16>(Disc)) {
2518+
OutContext.reportError(SMLoc(), "AArch64 PAC Discriminator '" +
2519+
Twine(Disc) +
2520+
"' out of range [0, 0xFFFF]");
2521+
}
25182522
emitMOVZ(AArch64::X1, Disc, 0);
25192523
}
25202524

@@ -2593,18 +2597,19 @@ AArch64AsmPrinter::lowerConstantPtrAuth(const ConstantPtrAuth &CPA) {
25932597
}
25942598

25952599
uint64_t Disc = CPA.getDiscriminator()->getZExtValue();
2596-
if (!isUInt<16>(Disc)) {
2597-
CPA.getContext().emitError("AArch64 PAC Discriminator '" + Twine(Disc) +
2598-
"' out of range [0, 0xFFFF]");
2599-
Disc = 0;
2600-
}
26012600

26022601
// Check if we need to represent this with an IRELATIVE and emit it if so.
26032602
if (auto *IFuncSym = emitPAuthRelocationAsIRelative(
26042603
Sym, Disc, AArch64PACKey::ID(KeyID), CPA.hasAddressDiscriminator(),
26052604
BaseGVB && BaseGVB->isDSOLocal(), DSExpr))
26062605
return IFuncSym;
26072606

2607+
if (!isUInt<16>(Disc)) {
2608+
CPA.getContext().emitError("AArch64 PAC Discriminator '" + Twine(Disc) +
2609+
"' out of range [0, 0xFFFF]");
2610+
Disc = 0;
2611+
}
2612+
26082613
if (DSExpr)
26092614
report_fatal_error("deactivation symbols unsupported in constant "
26102615
"expressions on this target");

llvm/test/CodeGen/AArch64/ptrauth-irelative.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@
6767
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
6868
@disc = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 0, ptr @disc), align 8
6969

70+
; CHECK: disc65536:
71+
; CHECK-NEXT: [[PLACE:.*]]:
72+
; CHECK-NEXT: .section .text.startup
73+
; CHECK-NEXT: [[FUNC:.*]]:
74+
; CHECK-NEXT: adrp x0, dsolocal
75+
; CHECK-NEXT: add x0, x0, :lo12:dsolocal
76+
; CHECK-NEXT: adrp x1, [[PLACE]]+65536
77+
; CHECK-NEXT: add x1, x1, :lo12:[[PLACE]]+65536
78+
; CHECK-NEXT: b __emupac_pacda
79+
; CHECK-NEXT: .section .rodata
80+
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
81+
@disc65536 = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 65536, ptr @disc), align 8
82+
7083
@global = external global i8
7184

7285
; CHECK: globalref:

llvm/test/CodeGen/AArch64/ptrauth-reloc.ll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,20 @@
175175

176176
@g = external global i32
177177
@g.ref.ia.65536 = constant ptr ptrauth (ptr @g, i32 0, i64 65536)
178+
179+
;--- err-disc-elf.ll
180+
181+
; RUN: not llc < err-disc-elf.ll -mtriple aarch64-elf -mattr=+pauth 2>&1 \
182+
; RUN: | FileCheck %s --check-prefix=CHECK-ERR-DISC-ELF
183+
; RUN: not llc < err-disc-elf.ll -mtriple aarch64-elf -mattr=+pauth \
184+
; RUN: -global-isel -verify-machineinstrs -global-isel-abort=1 2>&1 \
185+
; RUN: | FileCheck %s --check-prefix=CHECK-ERR-DISC-ELF
186+
187+
@g = external global i32
188+
189+
@ds = external global i8
190+
; CHECK-ERR-DISC-ELF-NOT: error: AArch64 PAC Discriminator '65537' out of range [0, 0xFFFF]
191+
@g.ref.da.65537 = constant ptr ptrauth (ptr @g, i32 2, i64 65537, ptr @g.ref.da.65537, ptr @ds)
192+
193+
; CHECK-ERR-DISC-ELF: error: AArch64 PAC Discriminator '65538' out of range [0, 0xFFFF]
194+
@g.ref.da.65538 = constant ptr ptrauth (ptr @g, i32 2, i64 65538, ptr null, ptr @ds)

0 commit comments

Comments
 (0)