Skip to content

Commit c553de8

Browse files
committed
Send -fno-lto when linker plugin LTO is not requested to avoid having GCC do LTO when using rustc_codegen_gcc
1 parent da86f4d commit c553de8

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,14 @@ impl<'a> Linker for GccLinker<'a> {
902902
fn linker_plugin_lto(&mut self) {
903903
match self.sess.opts.cg.linker_plugin_lto {
904904
LinkerPluginLto::Disabled => {
905-
// Nothing to do
905+
// GCC, when used as a linker, will perform LTO even without -flto when it sees
906+
// object files with the GCC IR (which can happen when using rustc_codegen_gcc).
907+
// Since the std is compiled with -Cembed-bitcode=yes, any program using the std
908+
// will have the linker do LTO.
909+
// So, we disable this with -fno-lto when linker plugin LTO is not requested.
910+
if self.is_cc() {
911+
self.cc_arg("-fno-lto");
912+
}
906913
}
907914
LinkerPluginLto::LinkerPluginAuto => {
908915
self.push_linker_plugin_lto_args(None);

tests/run-make/fno-lto/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}

tests/run-make/fno-lto/rmake.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ needs-target-std
2+
//@ ignore-msvc
3+
//@ ignore-wasm
4+
5+
use run_make_support::rustc;
6+
7+
fn main() {
8+
rustc().input("main.rs").print("link-args").run_unchecked().assert_stdout_contains("-fno-lto");
9+
10+
rustc()
11+
.input("main.rs")
12+
.arg("-Clinker-plugin-lto")
13+
.print("link-args")
14+
.run_unchecked()
15+
.assert_stdout_not_contains("-fno-lto");
16+
}

tests/run-make/linker-warning/short-error.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error: linking with `./fake-linker` failed: exit status: 1
22
|
3-
= note: "./fake-linker" "-m64" "/symbols.o" "<2 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "/build-root/test/run-make/linker-warning/rmake_out/{libfoo,libbar}.rlib" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,libcfg_if-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,liblibc-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/raw-dylibs" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/build-root/test/run-make/linker-warning/rmake_out" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "main" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "run_make_error"
3+
= note: "./fake-linker" "-m64" "/symbols.o" "<2 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "/build-root/test/run-make/linker-warning/rmake_out/{libfoo,libbar}.rlib" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,libcfg_if-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,liblibc-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/raw-dylibs" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-fno-lto" "-L" "/build-root/test/run-make/linker-warning/rmake_out" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "main" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "run_make_error"
44
= note: some arguments are omitted. use `--verbose` to show all linker arguments
55
= note: error: baz
66

0 commit comments

Comments
 (0)