Skip to content

Commit 2f79054

Browse files
committed
auto merge of #13463 : alexcrichton/rust/c-linkage-oh-my, r=brson
Previously, upstream C libraries were linked in a nondeterministic fashion because they were collected through iter_crate_data() which is a nodeterministic traversal of a hash map. When upstream rlibs had interdependencies among their native libraries (such as libfoo depending on libc), then the ordering would occasionally be wrong, causing linkage to fail. This uses the topologically sorted list of libraries to collect native libraries, so if a native library depends on libc it just needs to make sure that the rust crate depends on liblibc.
2 parents 745a3ce + e6072fa commit 2f79054

File tree

7 files changed

+92
-5
lines changed

7 files changed

+92
-5
lines changed

src/librustc/back/link.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1487,15 +1487,24 @@ fn add_upstream_rust_crates(args: &mut Vec<~str>, sess: &Session,
14871487
// crate as well.
14881488
//
14891489
// The use case for this is a little subtle. In theory the native
1490-
// dependencies of a crate a purely an implementation detail of the crate
1490+
// dependencies of a crate are purely an implementation detail of the crate
14911491
// itself, but the problem arises with generic and inlined functions. If a
14921492
// generic function calls a native function, then the generic function must
14931493
// be instantiated in the target crate, meaning that the native symbol must
14941494
// also be resolved in the target crate.
14951495
fn add_upstream_native_libraries(args: &mut Vec<~str>, sess: &Session) {
1496-
let cstore = &sess.cstore;
1497-
cstore.iter_crate_data(|cnum, _| {
1498-
let libs = csearch::get_native_libraries(cstore, cnum);
1496+
// Be sure to use a topological sorting of crates becuase there may be
1497+
// interdependencies between native libraries. When passing -nodefaultlibs,
1498+
// for example, almost all native libraries depend on libc, so we have to
1499+
// make sure that's all the way at the right (liblibc is near the base of
1500+
// the dependency chain).
1501+
//
1502+
// This passes RequireStatic, but the actual requirement doesn't matter,
1503+
// we're just getting an ordering of crate numbers, we're not worried about
1504+
// the paths.
1505+
let crates = sess.cstore.get_used_crates(cstore::RequireStatic);
1506+
for (cnum, _) in crates.move_iter() {
1507+
let libs = csearch::get_native_libraries(&sess.cstore, cnum);
14991508
for &(kind, ref lib) in libs.iter() {
15001509
match kind {
15011510
cstore::NativeUnknown => args.push("-l" + *lib),
@@ -1508,5 +1517,5 @@ fn add_upstream_native_libraries(args: &mut Vec<~str>, sess: &Session) {
15081517
}
15091518
}
15101519
}
1511-
});
1520+
}
15121521
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-include ../tools.mk
2+
3+
# The rust crate foo will link to the native library foo, while the rust crate
4+
# bar will link to the native library bar. There is also a dependency between
5+
# the native library bar to the natibe library foo.
6+
#
7+
# This test ensures that the ordering of -lfoo and -lbar on the command line is
8+
# correct to complete the linkage. If passed as "-lfoo -lbar", then the 'foo'
9+
# library will be stripped out, and the linkage will fail.
10+
11+
all: $(call STATICLIB,foo) $(call STATICLIB,bar)
12+
$(RUSTC) foo.rs
13+
$(RUSTC) bar.rs
14+
$(RUSTC) main.rs -Z print-link-args
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void foo();
2+
3+
void bar() { foo(); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2014 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 foo;
14+
15+
#[link(name = "bar")]
16+
extern {
17+
fn bar();
18+
}
19+
20+
pub fn doit() {
21+
unsafe { bar(); }
22+
}
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2014 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")]
14+
extern {
15+
fn foo();
16+
}
17+
18+
pub fn doit() {
19+
unsafe { foo(); }
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 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 foo;
12+
extern crate bar;
13+
14+
fn main() {
15+
bar::doit();
16+
}

0 commit comments

Comments
 (0)