From 4613f38cc5a7d50ab76bf93f1f3b76fdf0d89764 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 25 Jul 2026 15:06:31 -0400 Subject: [PATCH] Send -fno-lto when linker plugin LTO is not requested to avoid having GCC do LTO when using rustc_codegen_gcc --- compiler/rustc_codegen_ssa/src/back/linker.rs | 9 ++++++++- tests/run-make/fno-lto/main.rs | 1 + tests/run-make/fno-lto/rmake.rs | 17 +++++++++++++++++ tests/run-make/linker-warning/rmake.rs | 1 + 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/run-make/fno-lto/main.rs create mode 100644 tests/run-make/fno-lto/rmake.rs diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index e08a825aa7019..ffb9130ed1a08 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -900,7 +900,14 @@ impl<'a> Linker for GccLinker<'a> { fn linker_plugin_lto(&mut self) { match self.sess.opts.cg.linker_plugin_lto { LinkerPluginLto::Disabled => { - // Nothing to do + // GCC, when used as a linker, will perform LTO even without -flto when it sees + // object files with the GCC IR (which can happen when using rustc_codegen_gcc). + // Since the std is compiled with -Cembed-bitcode=yes, any program using the std + // will have the linker do LTO. + // So, we disable this with -fno-lto when linker plugin LTO is not requested. + if self.is_cc() && self.codegen_backend == "gcc" { + self.cc_arg("-fno-lto"); + } } LinkerPluginLto::LinkerPluginAuto => { self.push_linker_plugin_lto_args(None); diff --git a/tests/run-make/fno-lto/main.rs b/tests/run-make/fno-lto/main.rs new file mode 100644 index 0000000000000..f328e4d9d04c3 --- /dev/null +++ b/tests/run-make/fno-lto/main.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/tests/run-make/fno-lto/rmake.rs b/tests/run-make/fno-lto/rmake.rs new file mode 100644 index 0000000000000..db6fe7e6797f5 --- /dev/null +++ b/tests/run-make/fno-lto/rmake.rs @@ -0,0 +1,17 @@ +//@ needs-target-std +//@ ignore-msvc +//@ ignore-wasm +//@ needs-backends: gcc + +use run_make_support::rustc; + +fn main() { + rustc().input("main.rs").print("link-args").run_unchecked().assert_stdout_contains("-fno-lto"); + + rustc() + .input("main.rs") + .arg("-Clinker-plugin-lto") + .print("link-args") + .run_unchecked() + .assert_stdout_not_contains("-fno-lto"); +} diff --git a/tests/run-make/linker-warning/rmake.rs b/tests/run-make/linker-warning/rmake.rs index b25d892507907..f1a561e1291a5 100644 --- a/tests/run-make/linker-warning/rmake.rs +++ b/tests/run-make/linker-warning/rmake.rs @@ -83,6 +83,7 @@ fn main() { .normalize("libpanic_abort", "libpanic_unwind") .normalize(r#""[^"]*\/symbols.o""#, "\"/symbols.o\"") .normalize(r#""[^"]*\/raw-dylibs""#, "\"/raw-dylibs\"") + .normalize("\"-fno-lto\" ", "") .run(); }