Skip to content

Commit cc719d2

Browse files
committed
trans: Don't link whole rlibs to executables
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.
1 parent 12a68e6 commit cc719d2

File tree

7 files changed

+109
-1
lines changed

7 files changed

+109
-1
lines changed

src/librustc_trans/back/link.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,11 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
12531253

12541254
if any_objects {
12551255
archive.build();
1256-
cmd.link_whole_rlib(&fix_windows_verbatim_for_gcc(&dst));
1256+
if dylib {
1257+
cmd.link_whole_rlib(&fix_windows_verbatim_for_gcc(&dst));
1258+
} else {
1259+
cmd.link_rlib(&fix_windows_verbatim_for_gcc(&dst));
1260+
}
12571261
}
12581262
});
12591263
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
# file at the top-level directory of this distribution and at
3+
# http://rust-lang.org/COPYRIGHT.
4+
#
5+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
# option. This file may not be copied, modified, or distributed
9+
# except according to those terms.
10+
11+
-include ../tools.mk
12+
13+
all: $(call NATIVE_STATICLIB,foo) $(call NATIVE_STATICLIB,bar)
14+
$(RUSTC) lib1.rs
15+
$(RUSTC) lib2.rs
16+
$(RUSTC) main.rs -Clto
17+
$(call RUN,main)
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
int foo() {
12+
return 2;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
int foo() {
12+
return 1;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
13+
#[link(name = "foo", kind = "static")]
14+
extern {
15+
fn foo() -> i32;
16+
}
17+
18+
pub fn foo1() -> i32 {
19+
unsafe { foo() }
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
13+
extern crate lib1;
14+
15+
#[link(name = "bar", kind = "static")]
16+
extern {
17+
fn foo() -> i32;
18+
}
19+
20+
pub fn foo2() -> i32 {
21+
unsafe { foo() }
22+
}
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
extern crate lib1;
12+
extern crate lib2;
13+
14+
fn main() {
15+
assert_eq!(lib1::foo1(), 2);
16+
assert_eq!(lib2::foo2(), 2);
17+
}

0 commit comments

Comments
 (0)