Skip to content

Commit 49d812a

Browse files
committed
[PAC][CodeGen][ELF][AArch64] Support signed GOT
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
1 parent f53bfa3 commit 49d812a

21 files changed

+606
-36
lines changed

llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp

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

1414
#include "AArch64.h"
15+
#include "AArch64ExpandImm.h"
1516
#include "AArch64MCInstLower.h"
1617
#include "AArch64MachineFunctionInfo.h"
1718
#include "AArch64RegisterInfo.h"
@@ -160,6 +161,11 @@ class AArch64AsmPrinter : public AsmPrinter {
160161
// adrp-add followed by PAC sign)
161162
void LowerMOVaddrPAC(const MachineInstr &MI);
162163

164+
// Emit the sequence for LOADgotAUTH (load signed pointer from signed ELF GOT
165+
// and authenticate it with, if FPAC bit is not set, check+trap sequence after
166+
// authenticating)
167+
void LowerLOADgotAUTH(const MachineInstr &MI);
168+
163169
/// tblgen'erated driver function for lowering simple MI->MC
164170
/// pseudo instructions.
165171
bool lowerPseudoInstExpansion(const MachineInstr *MI, MCInst &Inst);
@@ -2168,6 +2174,10 @@ void AArch64AsmPrinter::LowerMOVaddrPAC(const MachineInstr &MI) {
21682174
};
21692175

21702176
const bool IsGOTLoad = MI.getOpcode() == AArch64::LOADgotPAC;
2177+
const bool IsELFSignedGOT = MI.getParent()
2178+
->getParent()
2179+
->getInfo<AArch64FunctionInfo>()
2180+
->hasELFSignedGOT();
21712181
MachineOperand GAOp = MI.getOperand(0);
21722182
const uint64_t KeyC = MI.getOperand(1).getImm();
21732183
assert(KeyC <= AArch64PACKey::LAST &&
@@ -2184,9 +2194,17 @@ void AArch64AsmPrinter::LowerMOVaddrPAC(const MachineInstr &MI) {
21842194
// Emit:
21852195
// target materialization:
21862196
// - via GOT:
2187-
// adrp x16, :got:target
2188-
// ldr x16, [x16, :got_lo12:target]
2189-
// add offset to x16 if offset != 0
2197+
// - unsigned GOT:
2198+
// adrp x16, :got:target
2199+
// ldr x16, [x16, :got_lo12:target]
2200+
// add offset to x16 if offset != 0
2201+
// - ELF signed GOT:
2202+
// adrp x17, :got:target
2203+
// add x17, x17, :got_auth_lo12:target
2204+
// ldr x16, [x17]
2205+
// aut{i|d}a x16, x17
2206+
// check+trap sequence (if no FPAC)
2207+
// add offset to x16 if offset != 0
21902208
//
21912209
// - direct:
21922210
// adrp x16, target
@@ -2229,13 +2247,79 @@ void AArch64AsmPrinter::LowerMOVaddrPAC(const MachineInstr &MI) {
22292247
MCInstLowering.lowerOperand(GAMOLo, GAMCLo);
22302248

22312249
EmitAndIncrement(
2232-
MCInstBuilder(AArch64::ADRP).addReg(AArch64::X16).addOperand(GAMCHi));
2250+
MCInstBuilder(AArch64::ADRP)
2251+
.addReg(IsGOTLoad && IsELFSignedGOT ? AArch64::X17 : AArch64::X16)
2252+
.addOperand(GAMCHi));
22332253

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

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

2618+
case AArch64::LOADgotAUTH:
2619+
LowerLOADgotAUTH(*MI);
2620+
return;
2621+
24872622
case AArch64::BRA:
24882623
case AArch64::BLRA:
24892624
emitPtrauthBranch(MI);

llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,6 @@ bool AArch64ExpandPseudo::expandMI(MachineBasicBlock &MBB,
12911291
MI.eraseFromParent();
12921292
return true;
12931293
}
1294-
12951294
case AArch64::LOADgot: {
12961295
MachineFunction *MF = MBB.getParent();
12971296
Register DstReg = MI.getOperand(0).getReg();

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)