https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/176052
Backport 2bf35f32330d6a0cada403559cd84e659224515a Requested by: @kovdan01 >From 43b2ef7d4ae461ff004a6f6280c012c8111d63ad Mon Sep 17 00:00:00 2001 From: Daniil Kovalev <[email protected]> Date: Thu, 15 Jan 2026 00:58:29 +0300 Subject: [PATCH] [PAC][ELF] Place AUTH constants in .data.rel.ro for position dependent code (#150418) For AArch64 target machine, effective relocation model on Windows and Darwin is always PIC, while for ELF targets Static is used when DynamicNoPIC is requested (see `getEffectiveRelocModel` in AArch64TargetMachine.cpp). This resulted in using .rodata section for AUTH constants, which is wrong since these are filled with AUTH dynamic relocs and require the section to be writeable during dynamic relocation resolving. This patch adds a check ensuring if the constant itself or one of the nested constants are AUTH ones. If so, use .data.rel.ro section. (cherry picked from commit 2bf35f32330d6a0cada403559cd84e659224515a) --- llvm/lib/Target/TargetLoweringObjectFile.cpp | 19 +++++++ .../CodeGen/AArch64/ptrauth-global-no-pic.ll | 53 +++++++++++++++++++ .../test/CodeGen/AArch64/ptrauth-irelative.ll | 16 +++--- 3 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll diff --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp index 343bcce80e3a1..ae31cd90b37ab 100644 --- a/llvm/lib/Target/TargetLoweringObjectFile.cpp +++ b/llvm/lib/Target/TargetLoweringObjectFile.cpp @@ -17,6 +17,7 @@ #include "llvm/IR/DataLayout.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" +#include "llvm/IR/GlobalValue.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" @@ -226,6 +227,20 @@ void TargetLoweringObjectFile::emitPseudoProbeDescMetadata( } } +static bool containsConstantPtrAuth(const Constant *C) { + if (isa<ConstantPtrAuth>(C)) + return true; + + if (isa<BlockAddress>(C) || isa<GlobalValue>(C)) + return false; + + for (const Value *Op : C->operands()) + if (containsConstantPtrAuth(cast<Constant>(Op))) + return true; + + return false; +} + /// getKindForGlobal - This is a top-level target-independent classifier for /// a global object. Given a global variable and information from the TM, this /// function classifies the global in a target independent manner. This function @@ -327,6 +342,10 @@ SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO, } } else { + // The dynamic linker always needs to fix PtrAuth relocations up. + if (containsConstantPtrAuth(C)) + return SectionKind::getReadOnlyWithRel(); + // In static, ROPI and RWPI relocation models, the linker will resolve // all addresses, so the relocation entries will actually be constants by // the time the app starts up. However, we can't put this into a diff --git a/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll b/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll new file mode 100644 index 0000000000000..77250a25f3f7c --- /dev/null +++ b/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll @@ -0,0 +1,53 @@ +; RUN: llc -mtriple aarch64-elf --relocation-model=static -mattr=+pauth < %s | FileCheck %s +; RUN: llc -mtriple aarch64-elf --relocation-model=dynamic-no-pic -mattr=+pauth < %s | FileCheck %s + +;; A constant value, use .rodata +; CHECK: .section .rodata,"a",@progbits +; CHECK: .globl Const +; CHECK: Const: +; CHECK: .xword 37 + +;; An AUTH reloc is needed, use .data.rel.ro +; CHECK: .section .data.rel.ro,"aw",@progbits +; CHECK: .globl PtrAuthExtern +; CHECK: PtrAuthExtern: +; CHECK: .xword ConstExtern@AUTH(da,0) + +; CHECK-NOT: .section +; CHECK: .globl PtrAuth +; CHECK: PtrAuth: +; CHECK: .xword Const@AUTH(da,0) + +; CHECK-NOT: .section +; CHECK: .globl PtrAuthExternNested1 +; CHECK: PtrAuthExternNested1: +; CHECK: .xword ConstExtern@AUTH(da,0) + +;; The address could be filled statically, use .rodata +; CHECK: .section .rodata,"a",@progbits +; CHECK: .globl PtrAuthExternNested2 +; CHECK: PtrAuthExternNested2: +; CHECK: .xword PtrAuthExtern + +;; An AUTH reloc is needed, use .data.rel.ro +; CHECK: .section .data.rel.ro,"aw",@progbits +; CHECK: .globl PtrAuthNested1 +; CHECK: PtrAuthNested1: +; CHECK: .xword Const@AUTH(da,0) + +;; The address could be filled statically, use .rodata +; CHECK: .section .rodata,"a",@progbits +; CHECK: .globl PtrAuthNested2 +; CHECK: PtrAuthNested2: +; CHECK: .xword PtrAuth + +@ConstExtern = external global i64 +@Const = constant i64 37 + +@PtrAuthExtern = constant ptr ptrauth (ptr @ConstExtern, i32 2) +@PtrAuth = constant ptr ptrauth (ptr @Const, i32 2) + +@PtrAuthExternNested1 = constant { ptr } { ptr ptrauth (ptr @ConstExtern, i32 2) } +@PtrAuthExternNested2 = constant { ptr } { ptr @PtrAuthExtern } +@PtrAuthNested1 = constant { ptr } { ptr ptrauth (ptr @Const, i32 2) } +@PtrAuthNested2 = constant { ptr } { ptr @PtrAuth } diff --git a/llvm/test/CodeGen/AArch64/ptrauth-irelative.ll b/llvm/test/CodeGen/AArch64/ptrauth-irelative.ll index 1a367bf0d6046..1e0af68dd7ab8 100644 --- a/llvm/test/CodeGen/AArch64/ptrauth-irelative.ll +++ b/llvm/test/CodeGen/AArch64/ptrauth-irelative.ll @@ -21,7 +21,7 @@ ; CHECK-NEXT: add x0, x0, :lo12:dsolocal ; CHECK-NEXT: mov x1, #2 ; CHECK-NEXT: b __emupac_pacda -; CHECK-NEXT: .section .rodata +; CHECK-NEXT: .section .data.rel.ro ; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT @dsolocalref = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 2, ptr null), align 8 @@ -38,7 +38,7 @@ ; CHECK-NEXT: .reloc [[LABEL]], R_AARCH64_PATCHINST, ds ; CHECK-NEXT: b __emupac_pacda ; CHECK-NEXT: ret -; CHECK-NEXT: .section .rodata +; CHECK-NEXT: .section .data.rel.ro ; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT @dsolocalrefds = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 2, ptr null, ptr @ds), align 8 @@ -50,7 +50,7 @@ ; CHECK-NEXT: add x0, x0, :lo12:dsolocal+8 ; CHECK-NEXT: mov x1, #3 ; CHECK-NEXT: b __emupac_pacda -; CHECK-NEXT: .section .rodata +; CHECK-NEXT: .section .data.rel.ro ; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT @dsolocalref8 = constant ptr ptrauth (ptr getelementptr (i8, ptr @dsolocal, i64 8), i32 2, i64 3, ptr null), align 8 @@ -63,7 +63,7 @@ ; CHECK-NEXT: adrp x1, [[PLACE]] ; CHECK-NEXT: add x1, x1, :lo12:[[PLACE]] ; CHECK-NEXT: b __emupac_pacda -; CHECK-NEXT: .section .rodata +; CHECK-NEXT: .section .data.rel.ro ; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT @disc = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 0, ptr @disc), align 8 @@ -76,7 +76,7 @@ ; CHECK-NEXT: adrp x1, [[PLACE]]+65536 ; CHECK-NEXT: add x1, x1, :lo12:[[PLACE]]+65536 ; CHECK-NEXT: b __emupac_pacda -; CHECK-NEXT: .section .rodata +; CHECK-NEXT: .section .data.rel.ro ; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT @disc65536 = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 65536, ptr @disc), align 8 @@ -90,7 +90,7 @@ ; CHECK-NEXT: ldr x0, [x0, :got_lo12:global] ; CHECK-NEXT: mov x1, #4 ; CHECK-NEXT: b __emupac_pacda -; CHECK-NEXT: .section .rodata +; CHECK-NEXT: .section .data.rel.ro ; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT @globalref = constant ptr ptrauth (ptr @global, i32 2, i64 4, ptr null), align 8 @@ -103,7 +103,7 @@ ; CHECK-NEXT: add x0, x0, #8 ; CHECK-NEXT: mov x1, #5 ; CHECK-NEXT: b __emupac_pacda -; CHECK-NEXT: .section .rodata +; CHECK-NEXT: .section .data.rel.ro ; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT @globalref8 = constant ptr ptrauth (ptr getelementptr (i8, ptr @global, i64 8), i32 2, i64 5, ptr null), align 8 @@ -118,7 +118,7 @@ ; CHECK-NEXT: add x0, x0, x16 ; CHECK-NEXT: mov x1, #5 ; CHECK-NEXT: b __emupac_pacda -; CHECK-NEXT: .section .rodata +; CHECK-NEXT: .section .data.rel.ro ; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT @globalref16777216 = constant ptr ptrauth (ptr getelementptr (i8, ptr @global, i64 16777216), i32 2, i64 5, ptr null), align 8 _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
