Skip to content

Commit facab69

Browse files
kovdan01c-rhodes
authored andcommitted
[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 2bf35f3)
1 parent f6de3be commit facab69

3 files changed

Lines changed: 80 additions & 8 deletions

File tree

llvm/lib/Target/TargetLoweringObjectFile.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/IR/DataLayout.h"
1818
#include "llvm/IR/DerivedTypes.h"
1919
#include "llvm/IR/Function.h"
20+
#include "llvm/IR/GlobalValue.h"
2021
#include "llvm/IR/GlobalVariable.h"
2122
#include "llvm/IR/Mangler.h"
2223
#include "llvm/IR/Module.h"
@@ -226,6 +227,20 @@ void TargetLoweringObjectFile::emitPseudoProbeDescMetadata(
226227
}
227228
}
228229

230+
static bool containsConstantPtrAuth(const Constant *C) {
231+
if (isa<ConstantPtrAuth>(C))
232+
return true;
233+
234+
if (isa<BlockAddress>(C) || isa<GlobalValue>(C))
235+
return false;
236+
237+
for (const Value *Op : C->operands())
238+
if (containsConstantPtrAuth(cast<Constant>(Op)))
239+
return true;
240+
241+
return false;
242+
}
243+
229244
/// getKindForGlobal - This is a top-level target-independent classifier for
230245
/// a global object. Given a global variable and information from the TM, this
231246
/// function classifies the global in a target independent manner. This function
@@ -327,6 +342,10 @@ SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO,
327342
}
328343

329344
} else {
345+
// The dynamic linker always needs to fix PtrAuth relocations up.
346+
if (containsConstantPtrAuth(C))
347+
return SectionKind::getReadOnlyWithRel();
348+
330349
// In static, ROPI and RWPI relocation models, the linker will resolve
331350
// all addresses, so the relocation entries will actually be constants by
332351
// the time the app starts up. However, we can't put this into a
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
; RUN: llc -mtriple aarch64-elf --relocation-model=static -mattr=+pauth < %s | FileCheck %s
2+
; RUN: llc -mtriple aarch64-elf --relocation-model=dynamic-no-pic -mattr=+pauth < %s | FileCheck %s
3+
4+
;; A constant value, use .rodata
5+
; CHECK: .section .rodata,"a",@progbits
6+
; CHECK: .globl Const
7+
; CHECK: Const:
8+
; CHECK: .xword 37
9+
10+
;; An AUTH reloc is needed, use .data.rel.ro
11+
; CHECK: .section .data.rel.ro,"aw",@progbits
12+
; CHECK: .globl PtrAuthExtern
13+
; CHECK: PtrAuthExtern:
14+
; CHECK: .xword ConstExtern@AUTH(da,0)
15+
16+
; CHECK-NOT: .section
17+
; CHECK: .globl PtrAuth
18+
; CHECK: PtrAuth:
19+
; CHECK: .xword Const@AUTH(da,0)
20+
21+
; CHECK-NOT: .section
22+
; CHECK: .globl PtrAuthExternNested1
23+
; CHECK: PtrAuthExternNested1:
24+
; CHECK: .xword ConstExtern@AUTH(da,0)
25+
26+
;; The address could be filled statically, use .rodata
27+
; CHECK: .section .rodata,"a",@progbits
28+
; CHECK: .globl PtrAuthExternNested2
29+
; CHECK: PtrAuthExternNested2:
30+
; CHECK: .xword PtrAuthExtern
31+
32+
;; An AUTH reloc is needed, use .data.rel.ro
33+
; CHECK: .section .data.rel.ro,"aw",@progbits
34+
; CHECK: .globl PtrAuthNested1
35+
; CHECK: PtrAuthNested1:
36+
; CHECK: .xword Const@AUTH(da,0)
37+
38+
;; The address could be filled statically, use .rodata
39+
; CHECK: .section .rodata,"a",@progbits
40+
; CHECK: .globl PtrAuthNested2
41+
; CHECK: PtrAuthNested2:
42+
; CHECK: .xword PtrAuth
43+
44+
@ConstExtern = external global i64
45+
@Const = constant i64 37
46+
47+
@PtrAuthExtern = constant ptr ptrauth (ptr @ConstExtern, i32 2)
48+
@PtrAuth = constant ptr ptrauth (ptr @Const, i32 2)
49+
50+
@PtrAuthExternNested1 = constant { ptr } { ptr ptrauth (ptr @ConstExtern, i32 2) }
51+
@PtrAuthExternNested2 = constant { ptr } { ptr @PtrAuthExtern }
52+
@PtrAuthNested1 = constant { ptr } { ptr ptrauth (ptr @Const, i32 2) }
53+
@PtrAuthNested2 = constant { ptr } { ptr @PtrAuth }

llvm/test/CodeGen/AArch64/ptrauth-irelative.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
; CHECK-NEXT: add x0, x0, :lo12:dsolocal
2222
; CHECK-NEXT: mov x1, #2
2323
; CHECK-NEXT: b __emupac_pacda
24-
; CHECK-NEXT: .section .rodata
24+
; CHECK-NEXT: .section .data.rel.ro
2525
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
2626
@dsolocalref = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 2, ptr null), align 8
2727

@@ -38,7 +38,7 @@
3838
; CHECK-NEXT: .reloc [[LABEL]], R_AARCH64_PATCHINST, ds
3939
; CHECK-NEXT: b __emupac_pacda
4040
; CHECK-NEXT: ret
41-
; CHECK-NEXT: .section .rodata
41+
; CHECK-NEXT: .section .data.rel.ro
4242
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
4343
@dsolocalrefds = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 2, ptr null, ptr @ds), align 8
4444

@@ -50,7 +50,7 @@
5050
; CHECK-NEXT: add x0, x0, :lo12:dsolocal+8
5151
; CHECK-NEXT: mov x1, #3
5252
; CHECK-NEXT: b __emupac_pacda
53-
; CHECK-NEXT: .section .rodata
53+
; CHECK-NEXT: .section .data.rel.ro
5454
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
5555
@dsolocalref8 = constant ptr ptrauth (ptr getelementptr (i8, ptr @dsolocal, i64 8), i32 2, i64 3, ptr null), align 8
5656

@@ -63,7 +63,7 @@
6363
; CHECK-NEXT: adrp x1, [[PLACE]]
6464
; CHECK-NEXT: add x1, x1, :lo12:[[PLACE]]
6565
; CHECK-NEXT: b __emupac_pacda
66-
; CHECK-NEXT: .section .rodata
66+
; CHECK-NEXT: .section .data.rel.ro
6767
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
6868
@disc = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 0, ptr @disc), align 8
6969

@@ -76,7 +76,7 @@
7676
; CHECK-NEXT: adrp x1, [[PLACE]]+65536
7777
; CHECK-NEXT: add x1, x1, :lo12:[[PLACE]]+65536
7878
; CHECK-NEXT: b __emupac_pacda
79-
; CHECK-NEXT: .section .rodata
79+
; CHECK-NEXT: .section .data.rel.ro
8080
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
8181
@disc65536 = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 65536, ptr @disc), align 8
8282

@@ -90,7 +90,7 @@
9090
; CHECK-NEXT: ldr x0, [x0, :got_lo12:global]
9191
; CHECK-NEXT: mov x1, #4
9292
; CHECK-NEXT: b __emupac_pacda
93-
; CHECK-NEXT: .section .rodata
93+
; CHECK-NEXT: .section .data.rel.ro
9494
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
9595
@globalref = constant ptr ptrauth (ptr @global, i32 2, i64 4, ptr null), align 8
9696

@@ -103,7 +103,7 @@
103103
; CHECK-NEXT: add x0, x0, #8
104104
; CHECK-NEXT: mov x1, #5
105105
; CHECK-NEXT: b __emupac_pacda
106-
; CHECK-NEXT: .section .rodata
106+
; CHECK-NEXT: .section .data.rel.ro
107107
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
108108
@globalref8 = constant ptr ptrauth (ptr getelementptr (i8, ptr @global, i64 8), i32 2, i64 5, ptr null), align 8
109109

@@ -118,7 +118,7 @@
118118
; CHECK-NEXT: add x0, x0, x16
119119
; CHECK-NEXT: mov x1, #5
120120
; CHECK-NEXT: b __emupac_pacda
121-
; CHECK-NEXT: .section .rodata
121+
; CHECK-NEXT: .section .data.rel.ro
122122
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
123123
@globalref16777216 = constant ptr ptrauth (ptr getelementptr (i8, ptr @global, i64 16777216), i32 2, i64 5, ptr null), align 8
124124

0 commit comments

Comments
 (0)