From 6a9b179fafdbfa038fe42e710d8b027711c6f17d 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 15f0c450f55f8..515d3be2c4a49 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..d183d115a55e1 100644 --- a/tests/run-make/linker-warning/rmake.rs +++ b/tests/run-make/linker-warning/rmake.rs @@ -1,4 +1,5 @@ //@ ignore-cross-compile (need to run fake linker) +//@ ignore-backends: gcc use run_make_support::{Rustc, diff, regex, rustc};