@@ -391,13 +391,14 @@ fn link_rlib<'a>(
391391 }
392392
393393 for ( raw_dylib_name, raw_dylib_imports) in
394- collate_raw_dylibs ( sess, & codegen_results. crate_info . used_libraries ) ?
394+ collate_raw_dylibs ( sess, codegen_results. crate_info . used_libraries . iter ( ) ) ?
395395 {
396396 let output_path = archive_builder_builder. create_dll_import_lib (
397397 sess,
398398 & raw_dylib_name,
399399 & raw_dylib_imports,
400400 tmpdir. as_ref ( ) ,
401+ true ,
401402 ) ;
402403
403404 ab. add_archive ( & output_path, Box :: new ( |_| false ) ) . unwrap_or_else ( |error| {
@@ -449,9 +450,9 @@ fn link_rlib<'a>(
449450/// then the CodegenResults value contains one NativeLib instance for each block. However, the
450451/// linker appears to expect only a single import library for each library used, so we need to
451452/// collate the symbols together by library name before generating the import libraries.
452- fn collate_raw_dylibs (
453- sess : & Session ,
454- used_libraries : & [ NativeLib ] ,
453+ fn collate_raw_dylibs < ' a , ' b > (
454+ sess : & ' a Session ,
455+ used_libraries : impl IntoIterator < Item = & ' b NativeLib > ,
455456) -> Result < Vec < ( String , Vec < DllImport > ) > , ErrorGuaranteed > {
456457 // Use index maps to preserve original order of imports and libraries.
457458 let mut dylib_table = FxIndexMap :: < String , FxIndexMap < Symbol , & DllImport > > :: default ( ) ;
@@ -2068,13 +2069,43 @@ fn linker_with_args<'a>(
20682069
20692070 // Link with the import library generated for any raw-dylib functions.
20702071 for ( raw_dylib_name, raw_dylib_imports) in
2071- collate_raw_dylibs ( sess, & codegen_results. crate_info . used_libraries ) ?
2072+ collate_raw_dylibs ( sess, codegen_results. crate_info . used_libraries . iter ( ) ) ?
2073+ {
2074+ cmd. add_object ( & archive_builder_builder. create_dll_import_lib (
2075+ sess,
2076+ & raw_dylib_name,
2077+ & raw_dylib_imports,
2078+ tmpdir,
2079+ true ,
2080+ ) ) ;
2081+ }
2082+ // As with add_upstream_native_libraries, we need to add the upstream raw-dylib symbols in case
2083+ // they are used within inlined functions or instantiated generic functions. We do this *after*
2084+ // handling the raw-dylib symbols in the current crate to make sure that those are chosen first
2085+ // by the linker.
2086+ let ( _, dependency_linkage) = codegen_results
2087+ . crate_info
2088+ . dependency_formats
2089+ . iter ( )
2090+ . find ( |( ty, _) | * ty == crate_type)
2091+ . expect ( "failed to find crate type in dependency format list" ) ;
2092+ let native_libraries_from_nonstatics = codegen_results
2093+ . crate_info
2094+ . native_libraries
2095+ . iter ( )
2096+ . filter_map ( |( cnum, libraries) | {
2097+ ( dependency_linkage[ cnum. as_usize ( ) - 1 ] != Linkage :: Static ) . then ( || libraries)
2098+ } )
2099+ . flatten ( ) ;
2100+ for ( raw_dylib_name, raw_dylib_imports) in
2101+ collate_raw_dylibs ( sess, native_libraries_from_nonstatics) ?
20722102 {
20732103 cmd. add_object ( & archive_builder_builder. create_dll_import_lib (
20742104 sess,
20752105 & raw_dylib_name,
20762106 & raw_dylib_imports,
20772107 tmpdir,
2108+ false ,
20782109 ) ) ;
20792110 }
20802111
0 commit comments