Skip to content

Commit aa857eb

Browse files
committed
Auto merge of #100537 - petrochenkov:piccheck, r=oli-obk
rustc_target: Add some more target spec sanity checking
2 parents 3892b70 + f7eb7ef commit aa857eb

14 files changed

+85
-54
lines changed

compiler/rustc_target/src/spec/aarch64_nintendo_switch_freestanding.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub fn target() -> Target {
1818
panic_strategy: PanicStrategy::Abort,
1919
position_independent_executables: true,
2020
dynamic_linking: true,
21-
executables: true,
2221
relro_level: RelroLevel::Off,
2322
..Default::default()
2423
},

compiler/rustc_target/src/spec/android_base.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub fn opts() -> TargetOptions {
44
let mut base = super::linux_base::opts();
55
base.os = "android".into();
66
base.default_dwarf_version = 2;
7-
base.position_independent_executables = true;
87
base.has_thread_local = false;
98
// This is for backward compatibility, see https://github.com/rust-lang/rust/issues/49867
109
// for context. (At that time, there was no `-C force-unwind-tables`, so the only solution

compiler/rustc_target/src/spec/apple_base.rs

+19-22
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
use std::{borrow::Cow, env};
22

3-
use crate::spec::{cvs, DebuginfoKind, FramePointer, SplitDebuginfo, TargetOptions};
3+
use crate::spec::{cvs, DebuginfoKind, FramePointer, SplitDebuginfo, StaticCow, TargetOptions};
44
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor};
55

66
fn pre_link_args(os: &'static str, arch: &'static str, abi: &'static str) -> LinkArgs {
7-
let mut args = LinkArgs::new();
8-
9-
let platform_name = match abi {
10-
"sim" => format!("{}-simulator", os),
11-
"macabi" => "mac-catalyst".to_string(),
12-
_ => os.to_string(),
7+
let platform_name: StaticCow<str> = match abi {
8+
"sim" => format!("{}-simulator", os).into(),
9+
"macabi" => "mac-catalyst".into(),
10+
_ => os.into(),
1311
};
1412

15-
let platform_version = match os.as_ref() {
13+
let platform_version: StaticCow<str> = match os.as_ref() {
1614
"ios" => ios_lld_platform_version(),
1715
"tvos" => tvos_lld_platform_version(),
1816
"watchos" => watchos_lld_platform_version(),
1917
"macos" => macos_lld_platform_version(arch),
2018
_ => unreachable!(),
21-
};
22-
23-
if abi != "macabi" {
24-
args.insert(LinkerFlavor::Gcc, vec!["-arch".into(), arch.into()]);
2519
}
20+
.into();
2621

27-
args.insert(
22+
let mut args = TargetOptions::link_args(
2823
LinkerFlavor::Lld(LldFlavor::Ld64),
29-
vec![
30-
"-arch".into(),
31-
arch.into(),
32-
"-platform_version".into(),
33-
platform_name.into(),
34-
platform_version.clone().into(),
35-
platform_version.into(),
36-
],
24+
&["-arch", arch, "-platform_version"],
3725
);
26+
// Manually add owned args unsupported by link arg building helpers.
27+
args.entry(LinkerFlavor::Lld(LldFlavor::Ld64)).or_default().extend([
28+
platform_name,
29+
platform_version.clone(),
30+
platform_version,
31+
]);
32+
if abi != "macabi" {
33+
super::add_link_args(&mut args, LinkerFlavor::Gcc, &["-arch", arch]);
34+
}
3835

3936
args
4037
}
@@ -127,7 +124,7 @@ pub fn macos_llvm_target(arch: &str) -> String {
127124
format!("{}-apple-macosx{}.{}.0", arch, major, minor)
128125
}
129126

130-
pub fn macos_link_env_remove() -> Vec<Cow<'static, str>> {
127+
pub fn macos_link_env_remove() -> Vec<StaticCow<str>> {
131128
let mut env_remove = Vec::with_capacity(2);
132129
// Remove the `SDKROOT` environment variable if it's clearly set for the wrong platform, which
133130
// may occur when we're linking a custom build script while targeting iOS for example.

compiler/rustc_target/src/spec/avr_gnu_base.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::spec::{LinkerFlavor, Target, TargetOptions};
1+
use crate::spec::{LinkerFlavor, RelocModel, Target, TargetOptions};
22

33
/// A base target for AVR devices using the GNU toolchain.
44
///
@@ -21,6 +21,7 @@ pub fn target(target_cpu: &'static str, mmcu: &'static str) -> Target {
2121
late_link_args: TargetOptions::link_args(LinkerFlavor::Gcc, &["-lgcc"]),
2222
max_atomic_width: Some(0),
2323
atomic_cas: false,
24+
relocation_model: RelocModel::Static,
2425
..TargetOptions::default()
2526
},
2627
}

compiler/rustc_target/src/spec/hexagon_unknown_linux_musl.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub fn target() -> Target {
1010
base.crt_static_default = false;
1111
base.has_rpath = true;
1212
base.linker_is_gnu = false;
13-
base.dynamic_linking = true;
1413

1514
base.c_enum_min_bits = 8;
1615

compiler/rustc_target/src/spec/l4re_base.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::spec::{cvs, LinkerFlavor, PanicStrategy, TargetOptions};
1+
use crate::spec::{cvs, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions};
22

33
pub fn opts() -> TargetOptions {
44
TargetOptions {
@@ -9,6 +9,7 @@ pub fn opts() -> TargetOptions {
99
linker: Some("l4-bender".into()),
1010
linker_is_gnu: false,
1111
families: cvs!["unix"],
12+
relocation_model: RelocModel::Static,
1213
..Default::default()
1314
}
1415
}

compiler/rustc_target/src/spec/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -837,15 +837,15 @@ impl fmt::Display for StackProtector {
837837
}
838838

839839
macro_rules! supported_targets {
840-
( $(($( $triple:literal, )+ $module:ident ),)+ ) => {
840+
( $(($triple:literal, $module:ident ),)+ ) => {
841841
$(mod $module;)+
842842

843843
/// List of supported targets
844-
pub const TARGETS: &[&str] = &[$($($triple),+),+];
844+
pub const TARGETS: &[&str] = &[$($triple),+];
845845

846846
fn load_builtin(target: &str) -> Option<Target> {
847847
let mut t = match target {
848-
$( $($triple)|+ => $module::target(), )+
848+
$( $triple => $module::target(), )+
849849
_ => return None,
850850
};
851851
t.is_builtin = true;
@@ -861,7 +861,7 @@ macro_rules! supported_targets {
861861
$(
862862
#[test] // `#[test]`
863863
fn $module() {
864-
tests_impl::test_target(super::$module::target());
864+
tests_impl::test_target(super::$module::target(), $triple);
865865
}
866866
)+
867867
}
@@ -1528,7 +1528,7 @@ fn add_link_args(link_args: &mut LinkArgs, flavor: LinkerFlavor, args: &[&'stati
15281528
match flavor {
15291529
LinkerFlavor::Ld => insert(LinkerFlavor::Lld(LldFlavor::Ld)),
15301530
LinkerFlavor::Msvc => insert(LinkerFlavor::Lld(LldFlavor::Link)),
1531-
LinkerFlavor::Lld(LldFlavor::Wasm) => {}
1531+
LinkerFlavor::Lld(LldFlavor::Ld64) | LinkerFlavor::Lld(LldFlavor::Wasm) => {}
15321532
LinkerFlavor::Lld(lld_flavor) => {
15331533
panic!("add_link_args: use non-LLD flavor for {:?}", lld_flavor)
15341534
}

compiler/rustc_target/src/spec/powerpc_unknown_freebsd.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::abi::Endian;
2-
use crate::spec::{LinkerFlavor, RelocModel, Target, TargetOptions};
2+
use crate::spec::{LinkerFlavor, Target, TargetOptions};
33

44
pub fn target() -> Target {
55
let mut base = super::freebsd_base::opts();
@@ -15,7 +15,6 @@ pub fn target() -> Target {
1515
options: TargetOptions {
1616
endian: Endian::Big,
1717
features: "+secure-plt".into(),
18-
relocation_model: RelocModel::Pic,
1918
mcount: "_mcount".into(),
2019
..base
2120
},

compiler/rustc_target/src/spec/tests/tests_impl.rs

+49-10
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ use super::super::*;
22
use std::assert_matches::assert_matches;
33

44
// Test target self-consistency and JSON encoding/decoding roundtrip.
5-
pub(super) fn test_target(target: Target) {
6-
target.check_consistency();
5+
pub(super) fn test_target(target: Target, triple: &str) {
6+
target.check_consistency(triple);
77
assert_eq!(Target::from_json(target.to_json()).map(|(j, _)| j), Ok(target));
88
}
99

1010
impl Target {
11-
fn check_consistency(&self) {
11+
fn check_consistency(&self, triple: &str) {
1212
assert_eq!(self.is_like_osx, self.vendor == "apple");
1313
assert_eq!(self.is_like_solaris, self.os == "solaris" || self.os == "illumos");
1414
assert_eq!(self.is_like_windows, self.os == "windows" || self.os == "uefi");
1515
assert_eq!(self.is_like_wasm, self.arch == "wasm32" || self.arch == "wasm64");
16-
assert!(self.is_like_windows || !self.is_like_msvc);
16+
if self.is_like_msvc {
17+
assert!(self.is_like_windows);
18+
}
1719

1820
// Check that default linker flavor and lld flavor are compatible
1921
// with some other key properties.
@@ -94,8 +96,9 @@ impl Target {
9496
check_noncc(LinkerFlavor::Ld);
9597
check_noncc(LinkerFlavor::Lld(LldFlavor::Ld));
9698
}
99+
LldFlavor::Ld64 => check_noncc(LinkerFlavor::Lld(LldFlavor::Ld64)),
97100
LldFlavor::Wasm => check_noncc(LinkerFlavor::Lld(LldFlavor::Wasm)),
98-
LldFlavor::Ld64 | LldFlavor::Link => {}
101+
LldFlavor::Link => {}
99102
},
100103
_ => {}
101104
}
@@ -109,20 +112,56 @@ impl Target {
109112
);
110113
}
111114

112-
assert!(
113-
(self.pre_link_objects_self_contained.is_empty()
114-
&& self.post_link_objects_self_contained.is_empty())
115-
|| self.link_self_contained != LinkSelfContainedDefault::False
116-
);
115+
if self.link_self_contained == LinkSelfContainedDefault::False {
116+
assert!(
117+
self.pre_link_objects_self_contained.is_empty()
118+
&& self.post_link_objects_self_contained.is_empty()
119+
);
120+
}
117121

118122
// If your target really needs to deviate from the rules below,
119123
// except it and document the reasons.
120124
// Keep the default "unknown" vendor instead.
121125
assert_ne!(self.vendor, "");
126+
assert_ne!(self.os, "");
122127
if !self.can_use_os_unknown() {
123128
// Keep the default "none" for bare metal targets instead.
124129
assert_ne!(self.os, "unknown");
125130
}
131+
132+
// Check dynamic linking stuff
133+
// BPF: when targeting user space vms (like rbpf), those can load dynamic libraries.
134+
if self.os == "none" && self.arch != "bpf" {
135+
assert!(!self.dynamic_linking);
136+
}
137+
if self.only_cdylib
138+
|| self.crt_static_allows_dylibs
139+
|| !self.late_link_args_dynamic.is_empty()
140+
{
141+
assert!(self.dynamic_linking);
142+
}
143+
// Apparently PIC was slow on wasm at some point, see comments in wasm_base.rs
144+
if self.dynamic_linking && !(self.is_like_wasm && self.os != "emscripten") {
145+
assert_eq!(self.relocation_model, RelocModel::Pic);
146+
}
147+
// PIEs are supported but not enabled by default with linuxkernel target.
148+
if self.position_independent_executables && !triple.ends_with("-linuxkernel") {
149+
assert_eq!(self.relocation_model, RelocModel::Pic);
150+
}
151+
if self.relocation_model == RelocModel::Pic {
152+
assert!(self.dynamic_linking || self.position_independent_executables);
153+
}
154+
if self.static_position_independent_executables {
155+
assert!(self.position_independent_executables);
156+
}
157+
if self.position_independent_executables {
158+
assert!(self.executables);
159+
}
160+
161+
// Check crt static stuff
162+
if self.crt_static_default || self.crt_static_allows_dylibs {
163+
assert!(self.crt_static_respected);
164+
}
126165
}
127166

128167
// Add your target to the whitelist if it has `std` library

compiler/rustc_target/src/spec/uefi_msvc_base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
// the timer-interrupt. Device-drivers are required to use polling-based models. Furthermore, all
1010
// code runs in the same environment, no process separation is supported.
1111

12-
use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, StackProbeType, TargetOptions};
12+
use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy};
13+
use crate::spec::{RelocModel, StackProbeType, TargetOptions};
1314

1415
pub fn opts() -> TargetOptions {
1516
let mut base = super::msvc_base::opts();
@@ -46,6 +47,7 @@ pub fn opts() -> TargetOptions {
4647
stack_probes: StackProbeType::Call,
4748
singlethread: true,
4849
linker: Some("rust-lld".into()),
50+
relocation_model: RelocModel::Static,
4951
..base
5052
}
5153
}

compiler/rustc_target/src/spec/x86_64_unknown_l4re_uclibc.rs

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ pub fn target() -> Target {
44
let mut base = super::l4re_base::opts();
55
base.cpu = "x86-64".into();
66
base.max_atomic_width = Some(64);
7-
base.crt_static_allows_dylibs = false;
8-
base.dynamic_linking = false;
97
base.panic_strategy = PanicStrategy::Abort;
108

119
Target {

compiler/rustc_target/src/spec/x86_64_unknown_none.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
// `target-cpu` compiler flags to opt-in more hardware-specific
55
// features.
66

7-
use super::{
8-
CodeModel, LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, RelroLevel, StackProbeType,
9-
Target, TargetOptions,
10-
};
7+
use super::{CodeModel, LinkerFlavor, LldFlavor, PanicStrategy};
8+
use super::{RelroLevel, StackProbeType, Target, TargetOptions};
119

1210
pub fn target() -> Target {
1311
let opts = TargetOptions {
@@ -18,7 +16,6 @@ pub fn target() -> Target {
1816
position_independent_executables: true,
1917
static_position_independent_executables: true,
2018
relro_level: RelroLevel::Full,
21-
relocation_model: RelocModel::Pic,
2219
linker_flavor: LinkerFlavor::Lld(LldFlavor::Ld),
2320
linker: Some("rust-lld".into()),
2421
features:

src/test/codegen/abi-efiapi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ trait Freeze { }
2424
#[lang="copy"]
2525
trait Copy { }
2626

27-
//x86_64: define win64cc void @has_efiapi
27+
//x86_64: define dso_local win64cc void @has_efiapi
2828
//i686: define void @has_efiapi
2929
//aarch64: define dso_local void @has_efiapi
3030
//arm: define dso_local void @has_efiapi

src/test/codegen/avr/avr-func-addrspace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn update_bar_value() {
7777
}
7878
}
7979

80-
// CHECK: define void @test(){{.+}}addrspace(1)
80+
// CHECK: define dso_local void @test(){{.+}}addrspace(1)
8181
#[no_mangle]
8282
pub extern "C" fn test() {
8383
let mut buf = 7;

0 commit comments

Comments
 (0)