@@ -896,7 +896,7 @@ fn link_args(cmd: &mut Linker,
896
896
// on other dylibs (e.g. other native deps).
897
897
add_local_native_libraries ( cmd, sess) ;
898
898
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 ) ;
900
900
901
901
// # Telling the linker what we're doing
902
902
@@ -1213,7 +1213,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker,
1213
1213
// generic function calls a native function, then the generic function must
1214
1214
// be instantiated in the target crate, meaning that the native symbol must
1215
1215
// 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 ) {
1217
1217
// Be sure to use a topological sorting of crates because there may be
1218
1218
// interdependencies between native libraries. When passing -nodefaultlibs,
1219
1219
// 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) {
1223
1223
// This passes RequireStatic, but the actual requirement doesn't matter,
1224
1224
// we're just getting an ordering of crate numbers, we're not worried about
1225
1225
// the paths.
1226
+ let formats = sess. dependency_formats . borrow ( ) ;
1227
+ let data = formats. get ( & crate_type) . unwrap ( ) ;
1228
+
1226
1229
let crates = sess. cstore . used_crates ( LinkagePreference :: RequireStatic ) ;
1227
1230
for ( cnum, _) in crates {
1228
1231
for lib in sess. cstore . native_libraries ( cnum) {
@@ -1232,8 +1235,15 @@ fn add_upstream_native_libraries(cmd: &mut Linker, sess: &Session) {
1232
1235
match lib. kind {
1233
1236
NativeLibraryKind :: NativeUnknown => cmd. link_dylib ( & lib. name . as_str ( ) ) ,
1234
1237
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
+ } ,
1237
1247
// ignore statically included native libraries here as we've
1238
1248
// already included them when we included the rust library
1239
1249
// previously
0 commit comments