From cc719d2d7d1967b92e38b1dec6d19f10c5b42891 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 11 Feb 2016 11:06:31 -0800 Subject: [PATCH] trans: Don't link whole rlibs to executables Back in 9bc8e6d14 the linking of rlibs changed to using the `link_whole_rlib` function. This change, however was only intended to affect dylibs, not executables. For executables we don't actually want to link entire rlibs because we want the linker to strip out as much as possible. This commit adds a conditional to this logic to only link entire rlibs if we're creating a dylib, and otherwise an executable just links an rlib as usual. A test is included which will fail to link if this behavior is reverted. --- src/librustc_trans/back/link.rs | 6 ++++- .../run-make/lto-no-link-whole-rlib/Makefile | 18 +++++++++++++++ .../run-make/lto-no-link-whole-rlib/bar.c | 13 +++++++++++ .../run-make/lto-no-link-whole-rlib/foo.c | 13 +++++++++++ .../run-make/lto-no-link-whole-rlib/lib1.rs | 20 ++++++++++++++++ .../run-make/lto-no-link-whole-rlib/lib2.rs | 23 +++++++++++++++++++ .../run-make/lto-no-link-whole-rlib/main.rs | 17 ++++++++++++++ 7 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/test/run-make/lto-no-link-whole-rlib/Makefile create mode 100644 src/test/run-make/lto-no-link-whole-rlib/bar.c create mode 100644 src/test/run-make/lto-no-link-whole-rlib/foo.c create mode 100644 src/test/run-make/lto-no-link-whole-rlib/lib1.rs create mode 100644 src/test/run-make/lto-no-link-whole-rlib/lib2.rs create mode 100644 src/test/run-make/lto-no-link-whole-rlib/main.rs diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs index 69a70cdf144b..33734d615a62 100644 --- a/src/librustc_trans/back/link.rs +++ b/src/librustc_trans/back/link.rs @@ -1253,7 +1253,11 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session, if any_objects { archive.build(); - cmd.link_whole_rlib(&fix_windows_verbatim_for_gcc(&dst)); + if dylib { + cmd.link_whole_rlib(&fix_windows_verbatim_for_gcc(&dst)); + } else { + cmd.link_rlib(&fix_windows_verbatim_for_gcc(&dst)); + } } }); } diff --git a/src/test/run-make/lto-no-link-whole-rlib/Makefile b/src/test/run-make/lto-no-link-whole-rlib/Makefile new file mode 100644 index 000000000000..1d45cb413c57 --- /dev/null +++ b/src/test/run-make/lto-no-link-whole-rlib/Makefile @@ -0,0 +1,18 @@ +# Copyright 2016 The Rust Project Developers. See the COPYRIGHT +# file at the top-level directory of this distribution and at +# http://rust-lang.org/COPYRIGHT. +# +# Licensed under the Apache License, Version 2.0 or the MIT license +# , at your +# option. This file may not be copied, modified, or distributed +# except according to those terms. + +-include ../tools.mk + +all: $(call NATIVE_STATICLIB,foo) $(call NATIVE_STATICLIB,bar) + $(RUSTC) lib1.rs + $(RUSTC) lib2.rs + $(RUSTC) main.rs -Clto + $(call RUN,main) + diff --git a/src/test/run-make/lto-no-link-whole-rlib/bar.c b/src/test/run-make/lto-no-link-whole-rlib/bar.c new file mode 100644 index 000000000000..716d1abcf347 --- /dev/null +++ b/src/test/run-make/lto-no-link-whole-rlib/bar.c @@ -0,0 +1,13 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +int foo() { + return 2; +} diff --git a/src/test/run-make/lto-no-link-whole-rlib/foo.c b/src/test/run-make/lto-no-link-whole-rlib/foo.c new file mode 100644 index 000000000000..1b36874581a9 --- /dev/null +++ b/src/test/run-make/lto-no-link-whole-rlib/foo.c @@ -0,0 +1,13 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +int foo() { + return 1; +} diff --git a/src/test/run-make/lto-no-link-whole-rlib/lib1.rs b/src/test/run-make/lto-no-link-whole-rlib/lib1.rs new file mode 100644 index 000000000000..0a87c8e47255 --- /dev/null +++ b/src/test/run-make/lto-no-link-whole-rlib/lib1.rs @@ -0,0 +1,20 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_type = "rlib"] + +#[link(name = "foo", kind = "static")] +extern { + fn foo() -> i32; +} + +pub fn foo1() -> i32 { + unsafe { foo() } +} diff --git a/src/test/run-make/lto-no-link-whole-rlib/lib2.rs b/src/test/run-make/lto-no-link-whole-rlib/lib2.rs new file mode 100644 index 000000000000..6e3f382b3fd9 --- /dev/null +++ b/src/test/run-make/lto-no-link-whole-rlib/lib2.rs @@ -0,0 +1,23 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_type = "rlib"] + +extern crate lib1; + +#[link(name = "bar", kind = "static")] +extern { + fn foo() -> i32; +} + +pub fn foo2() -> i32 { + unsafe { foo() } +} + diff --git a/src/test/run-make/lto-no-link-whole-rlib/main.rs b/src/test/run-make/lto-no-link-whole-rlib/main.rs new file mode 100644 index 000000000000..8417af63be9d --- /dev/null +++ b/src/test/run-make/lto-no-link-whole-rlib/main.rs @@ -0,0 +1,17 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +extern crate lib1; +extern crate lib2; + +fn main() { + assert_eq!(lib1::foo1(), 2); + assert_eq!(lib2::foo2(), 2); +}