Skip to content

Commit c1dd585

Browse files
committed
[PAC][CodeGen] Fix handling of extern_weak symbols with signed GOT
Before executing auth instruction on a signed address from a GOT slot, we must ensure that the address is not zero since zero addresses are not signed to preserve zero-checks working as expected. A GOT slot might be filled with zero when we have an undefined weak symbol.
1 parent f0044c3 commit c1dd585

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,15 @@ void AArch64AsmPrinter::LowerLOADgotAUTH(const MachineInstr &MI) {
23002300
.addImm(0));
23012301

23022302
assert(GAMO.isGlobal());
2303+
MCSymbol *UndefWeakSym;
2304+
if (GAMO.getGlobal()->hasExternalWeakLinkage()) {
2305+
UndefWeakSym = createTempSymbol("undef_weak");
2306+
EmitToStreamer(
2307+
MCInstBuilder(AArch64::CBZX)
2308+
.addReg(DstReg)
2309+
.addExpr(MCSymbolRefExpr::create(UndefWeakSym, OutContext)));
2310+
}
2311+
23032312
assert(GAMO.getGlobal()->getValueType() != nullptr);
23042313
unsigned AuthOpcode = GAMO.getGlobal()->getValueType()->isFunctionTy()
23052314
? AArch64::AUTIA
@@ -2308,6 +2317,9 @@ void AArch64AsmPrinter::LowerLOADgotAUTH(const MachineInstr &MI) {
23082317
.addReg(DstReg)
23092318
.addReg(DstReg)
23102319
.addReg(AArch64::X16));
2320+
2321+
if (GAMO.getGlobal()->hasExternalWeakLinkage())
2322+
OutStreamer->emitLabel(UndefWeakSym);
23112323
}
23122324

23132325
const MCExpr *

llvm/lib/Target/AArch64/AArch64InstrInfo.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,7 @@ let Predicates = [HasPAuth] in {
19451945
def LOADgotAUTH : Pseudo<(outs GPR64common:$dst), (ins i64imm:$addr), []>,
19461946
Sched<[WriteI, ReadI]> {
19471947
let Defs = [X16];
1948-
let Size = 16;
1948+
let Size = 20;
19491949
}
19501950

19511951
// Load a signed global address from a special $auth_ptr$ stub slot.

llvm/test/CodeGen/AArch64/ptrauth-extern-weak.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ define ptr @foo() {
1919
; CHECK: adrp x16, :got_auth:var
2020
; CHECK-NEXT: add x16, x16, :got_auth_lo12:var
2121
; CHECK-NEXT: ldr x0, [x16]
22+
; CHECK-NEXT: cbz x0, .Lundef_weak0
2223
; CHECK-NEXT: autia x0, x16
24+
; CHECK-NEXT: .Lundef_weak0:
2325
; CHECK-NEXT: ret
2426
}
2527

@@ -33,7 +35,9 @@ define ptr @bar() {
3335
; CHECK: adrp x16, :got_auth:arr_var
3436
; CHECK-NEXT: add x16, x16, :got_auth_lo12:arr_var
3537
; CHECK-NEXT: ldr x8, [x16]
38+
; CHECK-NEXT: cbz x8, .Lundef_weak1
3639
; CHECK-NEXT: autda x8, x16
40+
; CHECK-NEXT: .Lundef_weak1:
3741
; CHECK-NEXT: add x0, x8, #20
3842
; CHECK-NEXT: ret
3943
}

0 commit comments

Comments
 (0)