@@ -896,7 +896,7 @@ fn link_args(cmd: &mut Linker,
896896 // on other dylibs (e.g. other native deps).
897897 add_local_native_libraries ( cmd, sess) ;
898898 add_upstream_rust_crates ( cmd, sess, crate_type, tmpdir) ;
899- add_upstream_native_libraries ( cmd, sess) ;
899+ add_upstream_native_libraries ( cmd, sess, crate_type ) ;
900900
901901 // # Telling the linker what we're doing
902902
@@ -1213,7 +1213,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker,
12131213// generic function calls a native function, then the generic function must
12141214// be instantiated in the target crate, meaning that the native symbol must
12151215// also be resolved in the target crate.
1216- fn add_upstream_native_libraries ( cmd : & mut Linker , sess : & Session ) {
1216+ fn add_upstream_native_libraries ( cmd : & mut Linker , sess : & Session , crate_type : config :: CrateType ) {
12171217 // Be sure to use a topological sorting of crates because there may be
12181218 // interdependencies between native libraries. When passing -nodefaultlibs,
12191219 // for example, almost all native libraries depend on libc, so we have to
@@ -1223,6 +1223,9 @@ fn add_upstream_native_libraries(cmd: &mut Linker, sess: &Session) {
12231223 // This passes RequireStatic, but the actual requirement doesn't matter,
12241224 // we're just getting an ordering of crate numbers, we're not worried about
12251225 // the paths.
1226+ let formats = sess. dependency_formats . borrow ( ) ;
1227+ let data = formats. get ( & crate_type) . unwrap ( ) ;
1228+
12261229 let crates = sess. cstore . used_crates ( LinkagePreference :: RequireStatic ) ;
12271230 for ( cnum, _) in crates {
12281231 for lib in sess. cstore . native_libraries ( cnum) {
@@ -1232,8 +1235,15 @@ fn add_upstream_native_libraries(cmd: &mut Linker, sess: &Session) {
12321235 match lib. kind {
12331236 NativeLibraryKind :: NativeUnknown => cmd. link_dylib ( & lib. name . as_str ( ) ) ,
12341237 NativeLibraryKind :: NativeFramework => cmd. link_framework ( & lib. name . as_str ( ) ) ,
1235- NativeLibraryKind :: NativeStaticNobundle => cmd. link_staticlib ( & lib. name . as_str ( ) ) ,
1236-
1238+ NativeLibraryKind :: NativeStaticNobundle => {
1239+ // Link "static-nobundle" native libs only if the crate they originate from
1240+ // is being linked statically to the current crate. If it's linked dynamically
1241+ // or is an rlib already included via some other dylib crate, the symbols from
1242+ // native libs will have already been included in that dylib.
1243+ if data[ cnum. as_usize ( ) - 1 ] == Linkage :: Static {
1244+ cmd. link_staticlib ( & lib. name . as_str ( ) )
1245+ }
1246+ } ,
12371247 // ignore statically included native libraries here as we've
12381248 // already included them when we included the rust library
12391249 // previously
0 commit comments