[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.
This commit is contained in:
Daniil Kovalev 2026-01-15 00:58:29 +03:00 committed by GitHub
parent aac296b87e
commit 2bf35f3233
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 80 additions and 8 deletions

View File

@ -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

View File

@ -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 }

View File

@ -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