Skip to content

Commit 805ab05

Browse files
committed
[PAC][CodeGen][ELF][AArch64] Support signed GOT
This re-applies #96164 after revert in #102434. Support the following relocations and assembly operators: - `R_AARCH64_AUTH_ADR_GOT_PAGE` (`:got_auth:` for `adrp`) - `R_AARCH64_AUTH_LD64_GOT_LO12_NC` (`:got_auth_lo12:` for `ldr`) - `R_AARCH64_AUTH_GOT_ADD_LO12_NC` (`:got_auth_lo12:` for `add`) `LOADgotAUTH` pseudo-instruction is introduced which is later expanded to actual instruction sequence like the following. ``` adrp x16, :got_auth:sym add x16, x16, :got_auth_lo12:sym ldr x0, [x16] autia x0, x16 ``` If a resign is requested, like below, `LOADgotPAC` pseudo is used, and GOT load is lowered similarly to `LOADgotAUTH`. ``` @var = global i32 0 define ptr @resign_globalvar() { ret ptr ptrauth (ptr @var, i32 3, i64 43) } ``` If FPAC bit is not set and resign is requested, a check+trap sequence similar to one used for `AUT` pseudo is emitted. Both SelectionDAG and GlobalISel are suppported. For FastISel, we fall back to SelectionDAG. Tests starting with 'ptrauth-' have corresponding variants w/o this prefix. See also specification https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#appendix-signed-got tmo
1 parent 6e78aef commit 805ab05

20 files changed

+605
-35
lines changed

llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp

Lines changed: 142 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ class AArch64AsmPrinter : public AsmPrinter {
160160
// adrp-add followed by PAC sign)
161161
void LowerMOVaddrPAC(const MachineInstr &MI);
162162

163+
// Emit the sequence for LOADgotAUTH (load+auth pointer from signed ELF GOT)
164+
void LowerLOADgotAUTH(const MachineInstr &MI);
165+
163166
/// tblgen'erated driver function for lowering simple MI->MC
164167
/// pseudo instructions.
165168
bool lowerPseudoInstExpansion(const MachineInstr *MI, MCInst &Inst);
@@ -2168,6 +2171,10 @@ void AArch64AsmPrinter::LowerMOVaddrPAC(const MachineInstr &MI) {
21682171
};
21692172

21702173
const bool IsGOTLoad = MI.getOpcode() == AArch64::LOADgotPAC;
2174+
const bool IsELFSignedGOT = MI.getParent()
2175+
->getParent()
2176+
->getInfo<AArch64FunctionInfo>()
2177+
->hasELFSignedGOT();
21712178
MachineOperand GAOp = MI.getOperand(0);
21722179
const uint64_t KeyC = MI.getOperand(1).getImm();
21732180
assert(KeyC <= AArch64PACKey::LAST &&
@@ -2184,9 +2191,17 @@ void AArch64AsmPrinter::LowerMOVaddrPAC(const MachineInstr &MI) {
21842191
// Emit:
21852192
// target materialization:
21862193
// - via GOT:
2187-
// adrp x16, :got:target
2188-
// ldr x16, [x16, :got_lo12:target]
2189-
// add offset to x16 if offset != 0
2194+
// - unsigned GOT:
2195+
// adrp x16, :got:target
2196+
// ldr x16, [x16, :got_lo12:target]
2197+
// add offset to x16 if offset != 0
2198+
// - ELF signed GOT:
2199+
// adrp x17, :got:target
2200+
// add x17, x17, :got_auth_lo12:target
2201+
// ldr x16, [x17]
2202+
// aut{i|d}a x16, x17
2203+
// check+trap sequence (if no FPAC)
2204+
// add offset to x16 if offset != 0
21902205
//
21912206
// - direct:
21922207
// adrp x16, target
@@ -2229,13 +2244,81 @@ void AArch64AsmPrinter::LowerMOVaddrPAC(const MachineInstr &MI) {
22292244
MCInstLowering.lowerOperand(GAMOLo, GAMCLo);
22302245

22312246
EmitAndIncrement(
2232-
MCInstBuilder(AArch64::ADRP).addReg(AArch64::X16).addOperand(GAMCHi));
2247+
MCInstBuilder(AArch64::ADRP)
2248+
.addReg(IsGOTLoad && IsELFSignedGOT ? AArch64::X17 : AArch64::X16)
2249+
.addOperand(GAMCHi));
22332250

22342251
if (IsGOTLoad) {
2235-
EmitAndIncrement(MCInstBuilder(AArch64::LDRXui)
2236-
.addReg(AArch64::X16)
2237-
.addReg(AArch64::X16)
2238-
.addOperand(GAMCLo));
2252+
if (IsELFSignedGOT) {
2253+
EmitAndIncrement(MCInstBuilder(AArch64::ADDXri)
2254+
.addReg(AArch64::X17)
2255+
.addReg(AArch64::X17)
2256+
.addOperand(GAMCLo)
2257+
.addImm(0));
2258+
2259+
EmitAndIncrement(MCInstBuilder(AArch64::LDRXui)
2260+
.addReg(AArch64::X16)
2261+
.addReg(AArch64::X17)
2262+
.addImm(0));
2263+
2264+
assert(GAOp.isGlobal());
2265+
assert(GAOp.getGlobal()->getValueType() != nullptr);
2266+
unsigned AuthOpcode = GAOp.getGlobal()->getValueType()->isFunctionTy()
2267+
? AArch64::AUTIA
2268+
: AArch64::AUTDA;
2269+
2270+
EmitAndIncrement(MCInstBuilder(AuthOpcode)
2271+
.addReg(AArch64::X16)
2272+
.addReg(AArch64::X16)
2273+
.addReg(AArch64::X17));
2274+
2275+
// The logic in the following if statement is partially taken from
2276+
// emitPtrauthAuthResign.
2277+
if (!STI->hasFPAC()) {
2278+
auto AuthKey = (AuthOpcode == AArch64::AUTIA ? AArch64PACKey::IA
2279+
: AArch64PACKey::DA);
2280+
unsigned XPACOpc = getXPACOpcodeForKey(AuthKey);
2281+
MCSymbol *SuccessSym = createTempSymbol("auth_success_");
2282+
2283+
// XPAC has tied src/dst: use x17 as a temporary copy.
2284+
// mov x17, x16
2285+
EmitAndIncrement(MCInstBuilder(AArch64::ORRXrs)
2286+
.addReg(AArch64::X17)
2287+
.addReg(AArch64::XZR)
2288+
.addReg(AArch64::X16)
2289+
.addImm(0));
2290+
2291+
// xpaci x17
2292+
EmitAndIncrement(
2293+
MCInstBuilder(XPACOpc).addReg(AArch64::X17).addReg(AArch64::X17));
2294+
2295+
// cmp x16, x17
2296+
EmitAndIncrement(MCInstBuilder(AArch64::SUBSXrs)
2297+
.addReg(AArch64::XZR)
2298+
.addReg(AArch64::X16)
2299+
.addReg(AArch64::X17)
2300+
.addImm(0));
2301+
2302+
// b.eq Lsuccess
2303+
EmitAndIncrement(
2304+
MCInstBuilder(AArch64::Bcc)
2305+
.addImm(AArch64CC::EQ)
2306+
.addExpr(MCSymbolRefExpr::create(SuccessSym, OutContext)));
2307+
2308+
// Trapping sequences do a 'brk'.
2309+
// brk #<0xc470 + aut key>
2310+
EmitAndIncrement(MCInstBuilder(AArch64::BRK).addImm(0xc470 | AuthKey));
2311+
2312+
// If the auth check succeeds, we can continue.
2313+
// Lsuccess:
2314+
OutStreamer->emitLabel(SuccessSym);
2315+
}
2316+
} else {
2317+
EmitAndIncrement(MCInstBuilder(AArch64::LDRXui)
2318+
.addReg(AArch64::X16)
2319+
.addReg(AArch64::X16)
2320+
.addOperand(GAMCLo));
2321+
}
22392322
} else {
22402323
EmitAndIncrement(MCInstBuilder(AArch64::ADDXri)
22412324
.addReg(AArch64::X16)
@@ -2320,6 +2403,53 @@ void AArch64AsmPrinter::LowerMOVaddrPAC(const MachineInstr &MI) {
23202403
assert(STI->getInstrInfo()->getInstSizeInBytes(MI) >= InstsEmitted * 4);
23212404
}
23222405

2406+
void AArch64AsmPrinter::LowerLOADgotAUTH(const MachineInstr &MI) {
2407+
unsigned InstsEmitted = 0;
2408+
auto EmitAndIncrement = [this, &InstsEmitted](const MCInst &Inst) {
2409+
EmitToStreamer(*OutStreamer, Inst);
2410+
++InstsEmitted;
2411+
};
2412+
2413+
Register DstReg = MI.getOperand(0).getReg();
2414+
const MachineOperand &GAMO = MI.getOperand(1);
2415+
assert(GAMO.getOffset() == 0);
2416+
2417+
MachineOperand GAHiOp(GAMO);
2418+
MachineOperand GALoOp(GAMO);
2419+
GAHiOp.addTargetFlag(AArch64II::MO_PAGE);
2420+
GALoOp.addTargetFlag(AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
2421+
2422+
MCOperand GAMCHi, GAMCLo;
2423+
MCInstLowering.lowerOperand(GAHiOp, GAMCHi);
2424+
MCInstLowering.lowerOperand(GALoOp, GAMCLo);
2425+
2426+
EmitAndIncrement(
2427+
MCInstBuilder(AArch64::ADRP).addReg(AArch64::X16).addOperand(GAMCHi));
2428+
2429+
EmitAndIncrement(MCInstBuilder(AArch64::ADDXri)
2430+
.addReg(AArch64::X16)
2431+
.addReg(AArch64::X16)
2432+
.addOperand(GAMCLo)
2433+
.addImm(0));
2434+
2435+
EmitAndIncrement(MCInstBuilder(AArch64::LDRXui)
2436+
.addReg(DstReg)
2437+
.addReg(AArch64::X16)
2438+
.addImm(0));
2439+
2440+
assert(GAMO.isGlobal());
2441+
assert(GAMO.getGlobal()->getValueType() != nullptr);
2442+
unsigned AuthOpcode = GAMO.getGlobal()->getValueType()->isFunctionTy()
2443+
? AArch64::AUTIA
2444+
: AArch64::AUTDA;
2445+
EmitAndIncrement(MCInstBuilder(AuthOpcode)
2446+
.addReg(DstReg)
2447+
.addReg(DstReg)
2448+
.addReg(AArch64::X16));
2449+
2450+
assert(STI->getInstrInfo()->getInstSizeInBytes(MI) >= InstsEmitted * 4);
2451+
}
2452+
23232453
const MCExpr *
23242454
AArch64AsmPrinter::lowerBlockAddressConstant(const BlockAddress &BA) {
23252455
const MCExpr *BAE = AsmPrinter::lowerBlockAddressConstant(BA);
@@ -2484,6 +2614,10 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
24842614
LowerMOVaddrPAC(*MI);
24852615
return;
24862616

2617+
case AArch64::LOADgotAUTH:
2618+
LowerLOADgotAUTH(*MI);
2619+
return;
2620+
24872621
case AArch64::BRA:
24882622
case AArch64::BLRA:
24892623
emitPtrauthBranch(MI);

llvm/lib/Target/AArch64/AArch64FastISel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@ unsigned AArch64FastISel::materializeGV(const GlobalValue *GV) {
453453
if (!Subtarget->useSmallAddressing() && !Subtarget->isTargetMachO())
454454
return 0;
455455

456+
if (FuncInfo.MF->getInfo<AArch64FunctionInfo>()->hasELFSignedGOT())
457+
return 0;
458+
456459
unsigned OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
457460

458461
EVT DestEVT = TLI.getValueType(DL, GV->getType(), true);

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9357,6 +9357,11 @@ SDValue AArch64TargetLowering::getGOT(NodeTy *N, SelectionDAG &DAG,
93579357
SDValue GotAddr = getTargetNode(N, Ty, DAG, AArch64II::MO_GOT | Flags);
93589358
// FIXME: Once remat is capable of dealing with instructions with register
93599359
// operands, expand this into two nodes instead of using a wrapper node.
9360+
if (DAG.getMachineFunction()
9361+
.getInfo<AArch64FunctionInfo>()
9362+
->hasELFSignedGOT())
9363+
return SDValue(DAG.getMachineNode(AArch64::LOADgotAUTH, DL, Ty, GotAddr),
9364+
0);
93609365
return DAG.getNode(AArch64ISD::LOADgot, DL, Ty, GotAddr);
93619366
}
93629367

llvm/lib/Target/AArch64/AArch64InstrInfo.td

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,8 +1885,9 @@ let Predicates = [HasPAuth] in {
18851885
Sched<[WriteI, ReadI]> {
18861886
let isReMaterializable = 1;
18871887
let isCodeGenOnly = 1;
1888-
let Size = 40; // 12 fixed + 28 variable, for pointer offset, and discriminator
1889-
let Defs = [X16,X17];
1888+
let Size = 68; // 12 fixed + 56 variable, for pointer offset, discriminator and
1889+
// ELF signed GOT signed pointer authentication (if no FPAC)
1890+
let Defs = [X16,X17,NZCV];
18901891
}
18911892

18921893
// Load a signed global address from a special $auth_ptr$ stub slot.
@@ -1924,6 +1925,12 @@ let Predicates = [HasPAuth] in {
19241925
tcGPR64:$AddrDisc),
19251926
(AUTH_TCRETURN_BTI tcGPRx16x17:$dst, imm:$FPDiff, imm:$Key,
19261927
imm:$Disc, tcGPR64:$AddrDisc)>;
1928+
1929+
def LOADgotAUTH : Pseudo<(outs GPR64common:$dst), (ins i64imm:$addr), []>,
1930+
Sched<[WriteI, ReadI]> {
1931+
let Defs = [X16];
1932+
let Size = 16;
1933+
}
19271934
}
19281935

19291936
// v9.5-A pointer authentication extensions

llvm/lib/Target/AArch64/AArch64MCInstLower.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "AArch64MCInstLower.h"
15+
#include "AArch64MachineFunctionInfo.h"
1516
#include "MCTargetDesc/AArch64MCExpr.h"
1617
#include "Utils/AArch64BaseInfo.h"
1718
#include "llvm/CodeGen/AsmPrinter.h"
@@ -185,9 +186,12 @@ MCOperand AArch64MCInstLower::lowerSymbolOperandELF(const MachineOperand &MO,
185186
MCSymbol *Sym) const {
186187
uint32_t RefFlags = 0;
187188

188-
if (MO.getTargetFlags() & AArch64II::MO_GOT)
189-
RefFlags |= AArch64MCExpr::VK_GOT;
190-
else if (MO.getTargetFlags() & AArch64II::MO_TLS) {
189+
if (MO.getTargetFlags() & AArch64II::MO_GOT) {
190+
const MachineFunction *MF = MO.getParent()->getParent()->getParent();
191+
RefFlags |= (MF->getInfo<AArch64FunctionInfo>()->hasELFSignedGOT()
192+
? AArch64MCExpr::VK_GOT_AUTH
193+
: AArch64MCExpr::VK_GOT);
194+
} else if (MO.getTargetFlags() & AArch64II::MO_TLS) {
191195
TLSModel::Model Model;
192196
if (MO.isGlobal()) {
193197
const GlobalValue *GV = MO.getGlobal();

llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "AArch64MachineFunctionInfo.h"
1717
#include "AArch64InstrInfo.h"
1818
#include "AArch64Subtarget.h"
19+
#include "llvm/BinaryFormat/ELF.h"
1920
#include "llvm/IR/Constants.h"
2021
#include "llvm/IR/Metadata.h"
2122
#include "llvm/IR/Module.h"
@@ -72,6 +73,29 @@ static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) {
7273
return Key == "b_key";
7374
}
7475

76+
// Determine if we need to treat pointers in GOT as signed (as described in
77+
// https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#appendix-signed-got)
78+
// based on PAuth core info encoded as "aarch64-elf-pauthabi-platform" and
79+
// "aarch64-elf-pauthabi-version" module flags. Currently, only
80+
// AARCH64_PAUTH_PLATFORM_LLVM_LINUX platform supports signed GOT with
81+
// AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT bit in version value set.
82+
static bool hasELFSignedGOTHelper(const Function &F,
83+
const AArch64Subtarget *STI) {
84+
if (!Triple(STI->getTargetTriple()).isOSBinFormatELF())
85+
return false;
86+
const Module *M = F.getParent();
87+
const auto *PAP = mdconst::extract_or_null<ConstantInt>(
88+
M->getModuleFlag("aarch64-elf-pauthabi-platform"));
89+
if (!PAP || PAP->getZExtValue() != ELF::AARCH64_PAUTH_PLATFORM_LLVM_LINUX)
90+
return false;
91+
const auto *PAV = mdconst::extract_or_null<ConstantInt>(
92+
M->getModuleFlag("aarch64-elf-pauthabi-version"));
93+
if (!PAV)
94+
return false;
95+
return PAV->getZExtValue() &
96+
(1 << ELF::AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT);
97+
}
98+
7599
AArch64FunctionInfo::AArch64FunctionInfo(const Function &F,
76100
const AArch64Subtarget *STI) {
77101
// If we already know that the function doesn't have a redzone, set
@@ -80,6 +104,7 @@ AArch64FunctionInfo::AArch64FunctionInfo(const Function &F,
80104
HasRedZone = false;
81105
std::tie(SignReturnAddress, SignReturnAddressAll) = GetSignReturnAddress(F);
82106
SignWithBKey = ShouldSignWithBKey(F, *STI);
107+
HasELFSignedGOT = hasELFSignedGOTHelper(F, STI);
83108
// TODO: skip functions that have no instrumented allocas for optimization
84109
IsMTETagged = F.hasFnAttribute(Attribute::SanitizeMemTag);
85110

llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ class AArch64FunctionInfo final : public MachineFunctionInfo {
177177
/// SignWithBKey modifies the default PAC-RET mode to signing with the B key.
178178
bool SignWithBKey = false;
179179

180+
/// HasELFSignedGOT is true if the target binary format is ELF and the IR
181+
/// module containing the corresponding function has the following flags:
182+
/// - aarch64-elf-pauthabi-platform flag equal to
183+
/// AARCH64_PAUTH_PLATFORM_LLVM_LINUX;
184+
/// - aarch64-elf-pauthabi-version flag with
185+
/// AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT bit set.
186+
bool HasELFSignedGOT = false;
187+
180188
/// SigningInstrOffset captures the offset of the PAC-RET signing instruction
181189
/// within the prologue, so it can be re-used for authentication in the
182190
/// epilogue when using PC as a second salt (FEAT_PAuth_LR)
@@ -509,6 +517,8 @@ class AArch64FunctionInfo final : public MachineFunctionInfo {
509517

510518
bool shouldSignWithBKey() const { return SignWithBKey; }
511519

520+
bool hasELFSignedGOT() const { return HasELFSignedGOT; }
521+
512522
MCSymbol *getSigningInstrLabel() const { return SignInstrLabel; }
513523
void setSigningInstrLabel(MCSymbol *Label) { SignInstrLabel = Label; }
514524

0 commit comments

Comments
 (0)