Skip to content

Commit 4301abb

Browse files
committed
Align module flag behaviour with upstream LLVM
This patch brings Rust on par with: llvm/llvm-project#204226
1 parent 9545275 commit 4301abb

4 files changed

Lines changed: 85 additions & 51 deletions

File tree

compiler/rustc_codegen_llvm/src/base.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_middle::mono::Visibility;
2525
use rustc_middle::ty::TyCtxt;
2626
use rustc_session::config::{DebugInfo, Offload};
2727
use rustc_span::Symbol;
28-
use rustc_target::spec::SanitizerSet;
28+
use rustc_target::spec::{Arch, BinaryFormat, Os, SanitizerSet};
2929

3030
use super::ModuleLlvm;
3131
use crate::attributes;
@@ -152,21 +152,28 @@ pub(crate) fn compile_codegen_unit(
152152
cx.add_objc_module_flags();
153153
}
154154

155-
if cx.sess().pointer_authentication() {
156-
let cfg = cx.sess().pointer_auth_config.as_ref().unwrap();
157-
158-
let aarch64_elf_pauthabi_version =
159-
cfg.calculate_pauth_abi_version(&cx.sess().target);
160-
if aarch64_elf_pauthabi_version != 0 {
155+
if cx.sess().target.arch == Arch::AArch64 {
156+
if cx.sess().target.binary_format == BinaryFormat::Elf {
157+
let elf_got_flag = if let Some(cfg) = cx.sess().pointer_auth_config.as_ref() {
158+
cfg.elf_got as u32
159+
} else {
160+
0
161+
};
162+
cx.add_ptrauth_elf_got_flag(elf_got_flag);
163+
}
164+
if cx.sess().target.options.os == Os::Linux {
165+
let sign_personality_flag =
166+
if cx.sess().pointer_authentication_functions() { 1 } else { 0 };
167+
let aarch64_elf_pauthabi_version =
168+
if let Some(cfg) = cx.sess().pointer_auth_config.as_ref() {
169+
cfg.calculate_pauth_abi_version(&cx.sess().target)
170+
} else {
171+
0
172+
};
161173
cx.add_ptrauth_pauthabi_version_and_platform_flags(
162174
aarch64_elf_pauthabi_version,
163175
);
164-
}
165-
if cfg.elf_got {
166-
cx.add_ptrauth_elf_got_flag();
167-
}
168-
if cx.sess().pointer_authentication_functions() {
169-
cx.add_ptrauth_sign_personality_flag();
176+
cx.add_ptrauth_sign_personality_flag(sign_personality_flag);
170177
}
171178
}
172179

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,21 +727,21 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
727727
}
728728
}
729729

730-
pub(crate) fn add_ptrauth_elf_got_flag(&self) {
730+
pub(crate) fn add_ptrauth_elf_got_flag(&self, elf_got_flag: u32) {
731731
llvm::add_module_flag_u32(
732732
self.llmod,
733733
llvm::ModuleFlagMergeBehavior::Error,
734734
"ptrauth-elf-got",
735-
1,
735+
elf_got_flag,
736736
);
737737
}
738738

739-
pub(crate) fn add_ptrauth_sign_personality_flag(&self) {
739+
pub(crate) fn add_ptrauth_sign_personality_flag(&self, sign_personality_flag: u32) {
740740
llvm::add_module_flag_u32(
741741
self.llmod,
742742
llvm::ModuleFlagMergeBehavior::Error,
743743
"ptrauth-sign-personality",
744-
1,
744+
sign_personality_flag,
745745
);
746746
}
747747

compiler/rustc_session/src/session.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ impl PointerAuthConfig {
188188
};
189189
}
190190
pub fn calculate_pauth_abi_version(&self, target: &Target) -> u32 {
191-
assert!(target.cfg_abi == CfgAbi::Pauthtest);
191+
if target.cfg_abi != CfgAbi::Pauthtest {
192+
return 0;
193+
}
192194
// Bit positions of version flags for AARCH64_PAUTH_PLATFORM_LLVM_LINUX.
193195
// NOTE: The enum values must stay in sync with clang, see:
194196
// <llvm_root>/llvm/include/llvm/BinaryFormat/ELF.h
Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
//@ add-minicore
12
// ignore-tidy-linelength
23
//@ only-pauthtest
3-
//@ revisions: DEFAULT ALL DISABLE_JUMP DISABLE_AUTH_TRAPS DISABLE_CALLS DISABLE_INDIRCT_GOTOS DISABLE_RETURNS DISABLE_INTRINSICS DISABLE_TYPEINFO DISABLE_VT_PTR_ADDR DISABLE_VT_PTR_TYPE NONE
4+
//@ revisions: DEFAULT ALL DISABLE_JUMP DISABLE_AUTH_TRAPS DISABLE_CALLS DISABLE_INDIRECT_GOTOS DISABLE_RETURNS DISABLE_INTRINSICS DISABLE_TYPEINFO DISABLE_VT_PTR_ADDR DISABLE_VT_PTR_TYPE NONE AARCH64_MUSL
5+
6+
#![feature(no_core, lang_items)]
7+
#![no_std]
8+
#![no_core]
9+
#![crate_type = "lib"]
10+
11+
extern crate minicore;
12+
use minicore::ptr;
413

514
//@[DEFAULT] needs-llvm-components: aarch64
615
//@[DEFAULT] compile-flags: --target=aarch64-unknown-linux-pauthtest
@@ -12,8 +21,8 @@
1221
//@[DISABLE_AUTH_TRAPS] compile-flags: --target=aarch64-unknown-linux-pauthtest -Zpointer-authentication=-auth-traps
1322
//@[DISABLE_CALLS] needs-llvm-components: aarch64
1423
//@[DISABLE_CALLS] compile-flags: --target=aarch64-unknown-linux-pauthtest -Zpointer-authentication=-calls
15-
//@[DISABLE_INDIRCT_GOTOS] needs-llvm-components: aarch64
16-
//@[DISABLE_INDIRCT_GOTOS] compile-flags: --target=aarch64-unknown-linux-pauthtest -Zpointer-authentication=-indirect-gotos
24+
//@[DISABLE_INDIRECT_GOTOS] needs-llvm-components: aarch64
25+
//@[DISABLE_INDIRECT_GOTOS] compile-flags: --target=aarch64-unknown-linux-pauthtest -Zpointer-authentication=-indirect-gotos
1726
//@[DISABLE_RETURNS] needs-llvm-components: aarch64
1827
//@[DISABLE_RETURNS] compile-flags: --target=aarch64-unknown-linux-pauthtest -Zpointer-authentication=-return-addresses
1928
//@[DISABLE_INTRINSICS] needs-llvm-components: aarch64
@@ -25,94 +34,110 @@
2534
//@[DISABLE_VT_PTR_TYPE] needs-llvm-components: aarch64
2635
//@[DISABLE_VT_PTR_TYPE] compile-flags: --target=aarch64-unknown-linux-pauthtest -Zpointer-authentication=-vt-ptr-type-discrimination
2736
//@[NONE] needs-llvm-components: aarch64
28-
//@[NONE] compile-flags: --target=aarch64-unknown-linux-pauthtest -Zpointer-authentication=-aarch64-jump-table-hardening,-auth-traps,-calls,-indirect-gotos,-return-addresses,-init-fini,-init-fini-address-discrimination,-intrinsics,-typeinfo-vt-ptr-discrimination,-vt-ptr-addr-discrimination,-vt-ptr-type-discrimination
37+
//@[NONE] compile-flags: --target=aarch64-unknown-linux-pauthtest -Zpointer-authentication=-aarch64-jump-table-hardening,-auth-traps,-calls,-indirect-gotos,-return-addresses,-init-fini,-init-fini-address-discrimination,-intrinsics,-typeinfo-vt-ptr-discrimination,-vt-ptr-addr-discrimination,-vt-ptr-type-discrimination
38+
//@[AARCH64_MUSL] needs-llvm-components: aarch64
39+
//@[AARCH64_MUSL] compile-flags: --target=aarch64-unknown-linux-musl
2940

30-
// CHECK: define {{.*}} @main{{.*}} [[ATTR_MAIN:#[0-9]+]]
31-
fn main() {}
41+
// CHECK: define {{.*}} @{{.*}}test{{.*}} [[ATTR_MAIN:#[0-9]+]]
42+
#[inline(never)]
43+
pub fn test(a: i32) -> i32 {
44+
a + 4
45+
}
3246
// DEFAULT: attributes [[ATTR_MAIN]] = { {{.*}}"aarch64-jump-table-hardening"
3347
// DEFAULT-SAME: "ptrauth-auth-traps"
3448
// DEFAULT-SAME: "ptrauth-calls"
3549
// DEFAULT-SAME: "ptrauth-indirect-gotos"
3650
// DEFAULT-SAME: "ptrauth-returns"
37-
// DEFAULT: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
38-
// DEFAULT-NEXT: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1791}
51+
// DEFAULT: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
52+
// DEFAULT-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 1791}
3953

4054
// DISABLE_JUMP-NOT: aarch64-jump-table-hardening
4155
// DISABLE_JUMP: attributes [[ATTR_MAIN]] = { {{.*}}"ptrauth-auth-traps"
4256
// DISABLE_JUMP-SAME: "ptrauth-calls"
4357
// DISABLE_JUMP-SAME: "ptrauth-indirect-gotos"
4458
// DISABLE_JUMP-SAME: "ptrauth-returns"
45-
// DISABLE_JUMP: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
46-
// DISABLE_JUMP-NEXT: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1791}
59+
// DISABLE_JUMP: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
60+
// DISABLE_JUMP-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 1791}
4761

4862
// DISABLE_AUTH_TRAPS-NOT: ptrauth-auth-traps
4963
// DISABLE_AUTH_TRAPS: attributes [[ATTR_MAIN]] = { {{.*}}"aarch64-jump-table-hardening"
5064
// DISABLE_AUTH_TRAPS-SAME: "ptrauth-calls"
5165
// DISABLE_AUTH_TRAPS-SAME: "ptrauth-indirect-gotos"
5266
// DISABLE_AUTH_TRAPS-SAME: "ptrauth-returns"
53-
// DISABLE_AUTH_TRAPS: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
54-
// DISABLE_AUTH_TRAPS-NEXT !{i32 1, !"aarch64-elf-pauthabi-version", i32 1783}
67+
// DISABLE_AUTH_TRAPS: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
68+
// DISABLE_AUTH_TRAPS-NEXT !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 1783}
5569

5670
// DISABLE_CALLS-NOT: ptrauth-calls
5771
// DISABLE_CALLS: attributes [[ATTR_MAIN]] = { {{.*}}"aarch64-jump-table-hardening"
5872
// DISABLE_CALLS-SAME: "ptrauth-auth-traps"
5973
// DISABLE_CALLS-SAME: "ptrauth-indirect-gotos"
6074
// DISABLE_CALLS-SAME: "ptrauth-returns"
61-
// DISABLE_CALLS: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
62-
// DISABLE_CALLS-SAME-NEXT: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1789}
75+
// DISABLE_CALLS: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
76+
// DISABLE_CALLS-SAME-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 1789}
6377

64-
// DISABLE_INDIRCT_GOTOS-NOT: ptrauth-indirect-gotos
65-
// DISABLE_INDIRCT_GOTOS: attributes [[ATTR_MAIN]] = { {{.*}}"aarch64-jump-table-hardening"
66-
// DISABLE_INDIRCT_GOTOS-SAME: "ptrauth-auth-traps"
67-
// DISABLE_INDIRCT_GOTOS-SAME: "ptrauth-calls"
68-
// DISABLE_INDIRCT_GOTOS-SAME: "ptrauth-returns"
69-
// DISABLE_INDIRCT_GOTOS: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1279}
70-
// DISABLE_INDIRCT_GOTOS-NEXT: !{i32 1, !"ptrauth-sign-personality", i32 1}
78+
// DISABLE_INDIRECT_GOTOS-NOT: ptrauth-indirect-gotos
79+
// DISABLE_INDIRECT_GOTOS: attributes [[ATTR_MAIN]] = { {{.*}}"aarch64-jump-table-hardening"
80+
// DISABLE_INDIRECT_GOTOS-SAME: "ptrauth-auth-traps"
81+
// DISABLE_INDIRECT_GOTOS-SAME: "ptrauth-calls"
82+
// DISABLE_INDIRECT_GOTOS-SAME: "ptrauth-returns"
83+
// DISABLE_INDIRECT_GOTOS: !{i32 [[#]], !"ptrauth-elf-got", i32 0}
84+
// DISABLE_INDIRECT_GOTOS-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
85+
// DISABLE_INDIRECT_GOTOS-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 1279}
86+
// DISABLE_INDIRECT_GOTOS-NEXT: !{i32 [[#]], !"ptrauth-sign-personality", i32 1}
7187

7288
// DISABLE_RETURNS-NOT: ptrauth-returns
7389
// DISABLE_RETURNS: attributes [[ATTR_MAIN]] = { {{.*}}"aarch64-jump-table-hardening"
7490
// DISABLE_RETURNS-SAME: "ptrauth-auth-traps"
7591
// DISABLE_RETURNS-SAME: "ptrauth-calls"
7692
// DISABLE_RETURNS-SAME: "ptrauth-indirect-gotos"
77-
// DISABLE_RETURNS: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
78-
// DISABLE_RETURNS-NEXT: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1787}
93+
// DISABLE_RETURNS: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
94+
// DISABLE_RETURNS-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 1787}
7995

8096
// DISABLE_INTRINSICS: attributes [[ATTR_MAIN]] = { {{.*}}"aarch64-jump-table-hardening"
8197
// DISABLE_INTRINSICS-SAME: "ptrauth-auth-traps"
8298
// DISABLE_INTRINSICS-SAME: "ptrauth-calls"
8399
// DISABLE_INTRINSICS-SAME: "ptrauth-indirect-gotos"
84100
// DISABLE_INTRINSICS-SAME: "ptrauth-returns"
85-
// DISABLE_INTRINSICS: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
86-
// DISABLE_INTRINSICS-NEXT: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1790}
101+
// DISABLE_INTRINSICS: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
102+
// DISABLE_INTRINSICS-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 1790}
87103

88104
// DISABLE_TYPEINFO: attributes [[ATTR_MAIN]] = { {{.*}}"aarch64-jump-table-hardening"
89105
// DISABLE_TYPEINFO-SAME: "ptrauth-auth-traps"
90106
// DISABLE_TYPEINFO-SAME: "ptrauth-calls"
91107
// DISABLE_TYPEINFO-SAME: "ptrauth-indirect-gotos"
92108
// DISABLE_TYPEINFO-SAME: "ptrauth-returns"
93-
// DISABLE_TYPEINFO: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
94-
// DISABLE_TYPEINFO-NEXT: !{i32 1, !"aarch64-elf-pauthabi-version", i32 767}
109+
// DISABLE_TYPEINFO: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
110+
// DISABLE_TYPEINFO-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 767}
95111

96112
// DISABLE_VT_PTR_ADDR: attributes [[ATTR_MAIN]] = { {{.*}}"aarch64-jump-table-hardening"
97113
// DISABLE_VT_PTR_ADDR-SAME: "ptrauth-auth-traps"
98114
// DISABLE_VT_PTR_ADDR-SAME: "ptrauth-calls"
99115
// DISABLE_VT_PTR_ADDR-SAME: "ptrauth-indirect-gotos"
100116
// DISABLE_VT_PTR_ADDR-SAME: "ptrauth-returns"
101-
// DISABLE_VT_PTR_ADDR: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
102-
// DISABLE_VT_PTR_ADDR-NEXT: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1775}
117+
// DISABLE_VT_PTR_ADDR: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
118+
// DISABLE_VT_PTR_ADDR-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 1775}
103119

104120
// DISABLE_VT_PTR_TYPE: attributes [[ATTR_MAIN]] = { {{.*}}"aarch64-jump-table-hardening"
105121
// DISABLE_VT_PTR_TYPE-SAME: "ptrauth-auth-traps"
106122
// DISABLE_VT_PTR_TYPE-SAME: "ptrauth-calls"
107123
// DISABLE_VT_PTR_TYPE-SAME: "ptrauth-indirect-gotos"
108124
// DISABLE_VT_PTR_TYPE-SAME: "ptrauth-returns"
109-
// DISABLE_VT_PTR_TYPE: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
110-
// DISABLE_VT_PTR_TYPE-NEXT: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1759}
125+
// DISABLE_VT_PTR_TYPE: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
126+
// DISABLE_VT_PTR_TYPE-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 1759}
111127

112128
// NONE-NOT: ptrauth-returns
113129
// NONE-NOT: aarch64-jump-table-hardening
114130
// NONE-NOT: ptrauth-auth-traps
115131
// NONE-NOT: ptrauth-calls
116132
// NONE-NOT: ptrauth-indirect-gotos
117-
// NONE-NOT: aarch64-elf-pauthabi-platform
118-
// NONE-NOT: aarch64-elf-pauthabi-version
133+
// The following flags are always emitted. When all ptrauth options are disabled,
134+
// their values should be 0.
135+
// NONE: !{i32 [[#]], !"ptrauth-elf-got", i32 0}
136+
// NONE-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
137+
// NONE-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 0}
138+
// NONE-NEXT: !{i32 [[#]], !"ptrauth-sign-personality", i32 0}
139+
140+
// AARCH64_MUSL: !{i32 [[#]], !"ptrauth-elf-got", i32 0}
141+
// AARCH64_MUSL-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-platform", i32 268435458}
142+
// AARCH64_MUSL-NEXT: !{i32 [[#]], !"aarch64-elf-pauthabi-version", i32 0}
143+
// AARCH64_MUSL-NEXT: !{i32 [[#]], !"ptrauth-sign-personality", i32 0}

0 commit comments

Comments
 (0)