diff --git a/src/librustc_codegen_ssa/back/link.rs b/src/librustc_codegen_ssa/back/link.rs index dcce1d45298..cff84b11aa0 100644 --- a/src/librustc_codegen_ssa/back/link.rs +++ b/src/librustc_codegen_ssa/back/link.rs @@ -1253,20 +1253,10 @@ fn add_post_link_objects( /// Add arbitrary "pre-link" args defined by the target spec or from command line. /// FIXME: Determine where exactly these args need to be inserted. -fn add_pre_link_args( - cmd: &mut dyn Linker, - sess: &Session, - flavor: LinkerFlavor, - crate_type: CrateType, -) { +fn add_pre_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) { if let Some(args) = sess.target.target.options.pre_link_args.get(&flavor) { cmd.args(args); } - if let Some(args) = sess.target.target.options.pre_link_args_crt.get(&flavor) { - if sess.crt_static(Some(crate_type)) { - cmd.args(args); - } - } cmd.args(&sess.opts.debugging_opts.pre_link_args); } @@ -1502,7 +1492,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>( let crt_objects_fallback = crt_objects_fallback(sess, crate_type); // NO-OPT-OUT, OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT - add_pre_link_args(cmd, sess, flavor, crate_type); + add_pre_link_args(cmd, sess, flavor); // NO-OPT-OUT add_link_script(cmd, sess, tmpdir, crate_type); diff --git a/src/librustc_codegen_ssa/back/linker.rs b/src/librustc_codegen_ssa/back/linker.rs index 77cb31bf3d2..d9fed998c92 100644 --- a/src/librustc_codegen_ssa/back/linker.rs +++ b/src/librustc_codegen_ssa/back/linker.rs @@ -315,6 +315,21 @@ impl<'a> Linker for GccLinker<'a> { self.build_dylib(out_filename); } } + // VxWorks compiler driver introduced `--static-crt` flag specifically for rustc, + // it switches linking for libc and similar system libraries to static without using + // any `#[link]` attributes in the `libc` crate, see #72782 for details. + // FIXME: Switch to using `#[link]` attributes in the `libc` crate + // similarly to other targets. + if self.sess.target.target.target_os == "vxworks" + && matches!( + output_kind, + LinkOutputKind::StaticNoPicExe + | LinkOutputKind::StaticPicExe + | LinkOutputKind::StaticDylib + ) + { + self.cmd.arg("--static-crt"); + } } fn link_dylib(&mut self, lib: Symbol) { diff --git a/src/librustc_target/spec/mod.rs b/src/librustc_target/spec/mod.rs index c9558879a1c..f329c8c06cc 100644 --- a/src/librustc_target/spec/mod.rs +++ b/src/librustc_target/spec/mod.rs @@ -733,8 +733,7 @@ pub struct TargetOptions { pub lld_flavor: LldFlavor, /// Linker arguments that are passed *before* any user-defined libraries. - pub pre_link_args: LinkArgs, // ... unconditionally - pub pre_link_args_crt: LinkArgs, // ... when linking with a bundled crt + pub pre_link_args: LinkArgs, /// Objects to link before and after all other object code. pub pre_link_objects: CrtObjects, pub post_link_objects: CrtObjects, @@ -997,7 +996,6 @@ impl Default for TargetOptions { linker: option_env!("CFG_DEFAULT_LINKER").map(|s| s.to_string()), lld_flavor: LldFlavor::Ld, pre_link_args: LinkArgs::new(), - pre_link_args_crt: LinkArgs::new(), post_link_args: LinkArgs::new(), link_script: None, asm_args: Vec::new(), @@ -1397,7 +1395,6 @@ impl Target { key!(post_link_objects_fallback, link_objects); key!(crt_objects_fallback, crt_objects_fallback)?; key!(pre_link_args, link_args); - key!(pre_link_args_crt, link_args); key!(late_link_args, link_args); key!(late_link_args_dynamic, link_args); key!(late_link_args_static, link_args); @@ -1629,7 +1626,6 @@ impl ToJson for Target { target_option_val!(post_link_objects_fallback); target_option_val!(crt_objects_fallback); target_option_val!(link_args - pre_link_args); - target_option_val!(link_args - pre_link_args_crt); target_option_val!(link_args - late_link_args); target_option_val!(link_args - late_link_args_dynamic); target_option_val!(link_args - late_link_args_static); diff --git a/src/librustc_target/spec/tests/tests_impl.rs b/src/librustc_target/spec/tests/tests_impl.rs index 4cf186bdd7c..788d1d2c484 100644 --- a/src/librustc_target/spec/tests/tests_impl.rs +++ b/src/librustc_target/spec/tests/tests_impl.rs @@ -25,7 +25,6 @@ impl Target { ); for args in &[ &self.options.pre_link_args, - &self.options.pre_link_args_crt, &self.options.late_link_args, &self.options.late_link_args_dynamic, &self.options.late_link_args_static, diff --git a/src/librustc_target/spec/vxworks_base.rs b/src/librustc_target/spec/vxworks_base.rs index 1b25c51278d..777bb58d7db 100644 --- a/src/librustc_target/spec/vxworks_base.rs +++ b/src/librustc_target/spec/vxworks_base.rs @@ -1,8 +1,6 @@ use crate::spec::{LinkArgs, LinkerFlavor, TargetOptions}; pub fn opts() -> TargetOptions { - let mut args_crt = LinkArgs::new(); - args_crt.insert(LinkerFlavor::Gcc, vec!["--static-crt".to_string()]); let mut args = LinkArgs::new(); args.insert( LinkerFlavor::Gcc, @@ -29,7 +27,6 @@ pub fn opts() -> TargetOptions { pre_link_args: args, position_independent_executables: false, has_elf_tls: true, - pre_link_args_crt: args_crt, crt_static_default: true, crt_static_respected: true, crt_static_allows_dylibs: true,