Skip to content

Commit

Permalink
trans: Don't link whole rlibs to executables
Browse files Browse the repository at this point in the history
Back in 9bc8e6d 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.
  • Loading branch information
alexcrichton committed Feb 14, 2016
1 parent 12a68e6 commit cc719d2
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/librustc_trans/back/link.rs
Expand Up @@ -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));
}
}
});
}
Expand Down
18 changes: 18 additions & 0 deletions 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)

13 changes: 13 additions & 0 deletions 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;
}
13 changes: 13 additions & 0 deletions 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;
}
20 changes: 20 additions & 0 deletions 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() }
}
23 changes: 23 additions & 0 deletions 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() }
}

17 changes: 17 additions & 0 deletions 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);
}

0 comments on commit cc719d2

Please sign in to comment.