about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2016-02-11 11:06:31 -0800
committerAlex Crichton <alex@alexcrichton.com>2016-02-14 11:45:59 -0800
commitcc719d2d7d1967b92e38b1dec6d19f10c5b42891 (patch)
treebe6e36c82df4d0a9449bba55002136ef9c9044ca
parent12a68e6af31a32c52785298f036543caa96d24d3 (diff)
downloadrust-cc719d2d7d1967b92e38b1dec6d19f10c5b42891.tar.gz
rust-cc719d2d7d1967b92e38b1dec6d19f10c5b42891.zip
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.
-rw-r--r--src/librustc_trans/back/link.rs6
-rw-r--r--src/test/run-make/lto-no-link-whole-rlib/Makefile18
-rw-r--r--src/test/run-make/lto-no-link-whole-rlib/bar.c13
-rw-r--r--src/test/run-make/lto-no-link-whole-rlib/foo.c13
-rw-r--r--src/test/run-make/lto-no-link-whole-rlib/lib1.rs20
-rw-r--r--src/test/run-make/lto-no-link-whole-rlib/lib2.rs23
-rw-r--r--src/test/run-make/lto-no-link-whole-rlib/main.rs17
7 files changed, 109 insertions, 1 deletions
diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs
index 69a70cdf144..33734d615a6 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 00000000000..1d45cb413c5
--- /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 <LICENSE-APACHE or
+# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+# <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 00000000000..716d1abcf34
--- /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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 00000000000..1b36874581a
--- /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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 00000000000..0a87c8e4725
--- /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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 00000000000..6e3f382b3fd
--- /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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 00000000000..8417af63be9
--- /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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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);
+}