@@ -849,26 +849,34 @@ impl CrateInfo {
849849
850850 // Handle circular dependencies in the standard library.
851851 // See comment before `add_linked_symbol_object` function for the details.
852- let missing_weak_lang_items: FxHashSet < & Symbol > = info
853- . used_crates
854- . iter ( )
855- . flat_map ( |cnum| {
856- tcx. missing_lang_items ( * cnum)
857- . iter ( )
858- . filter ( |l| lang_items:: required ( tcx, * * l) )
859- . filter_map ( |item| WEAK_ITEMS_SYMBOLS . get ( item) )
860- } )
861- . collect ( ) ;
862- info. linked_symbols
863- . iter_mut ( )
864- . filter ( |( crate_type, _) | !matches ! ( crate_type, CrateType :: Rlib | CrateType :: Staticlib ) )
865- . for_each ( |( _, linked_symbols) | {
866- linked_symbols. extend (
867- missing_weak_lang_items
852+ // With msvc-like linkers it's both unnecessary (they support circular dependencies),
853+ // and causes linking issues (when weak lang item symbols are "privatized" by LTO).
854+ let target = & tcx. sess . target ;
855+ if !target. is_like_msvc {
856+ let missing_weak_lang_items: FxHashSet < & Symbol > = info
857+ . used_crates
858+ . iter ( )
859+ . flat_map ( |cnum| {
860+ tcx. missing_lang_items ( * cnum)
868861 . iter ( )
869- . map ( |item| ( item. to_string ( ) , SymbolExportKind :: Text ) ) ,
870- )
871- } ) ;
862+ . filter ( |l| lang_items:: required ( tcx, * * l) )
863+ . filter_map ( |item| WEAK_ITEMS_SYMBOLS . get ( item) )
864+ } )
865+ . collect ( ) ;
866+ let prefix = if target. is_like_windows && target. arch == "x86" { "_" } else { "" } ;
867+ info. linked_symbols
868+ . iter_mut ( )
869+ . filter ( |( crate_type, _) | {
870+ !matches ! ( crate_type, CrateType :: Rlib | CrateType :: Staticlib )
871+ } )
872+ . for_each ( |( _, linked_symbols) | {
873+ linked_symbols. extend (
874+ missing_weak_lang_items
875+ . iter ( )
876+ . map ( |item| ( format ! ( "{prefix}{item}" ) , SymbolExportKind :: Text ) ) ,
877+ )
878+ } ) ;
879+ }
872880
873881 let embed_visualizers = tcx. sess . crate_types ( ) . iter ( ) . any ( |& crate_type| match crate_type {
874882 CrateType :: Executable | CrateType :: Dylib | CrateType :: Cdylib => {
@@ -888,7 +896,7 @@ impl CrateInfo {
888896 }
889897 } ) ;
890898
891- if tcx . sess . target . is_like_msvc && embed_visualizers {
899+ if target. is_like_msvc && embed_visualizers {
892900 info. natvis_debugger_visualizers =
893901 collect_debugger_visualizers_transitive ( tcx, DebuggerVisualizerType :: Natvis ) ;
894902 }
0 commit comments