@@ -17,6 +17,7 @@ use rustc_hir::lang_items::LangItem;
1717use rustc_hir:: { ItemId , Target } ;
1818use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrs ;
1919use rustc_middle:: middle:: debugger_visualizer:: { DebuggerVisualizerFile , DebuggerVisualizerType } ;
20+ use rustc_middle:: middle:: dependency_format:: Dependencies ;
2021use rustc_middle:: middle:: exported_symbols:: { self , SymbolExportKind } ;
2122use rustc_middle:: middle:: lang_items;
2223use rustc_middle:: mir:: BinOp ;
@@ -630,14 +631,30 @@ pub fn allocator_kind_for_codegen(tcx: TyCtxt<'_>) -> Option<AllocatorKind> {
630631 // If the crate doesn't have an `allocator_kind` set then there's definitely
631632 // no shim to generate. Otherwise we also check our dependency graph for all
632633 // our output crate types. If anything there looks like its a `Dynamic`
633- // linkage, then it's already got an allocator shim and we'll be using that
634- // one instead. If nothing exists then it's our job to generate the
635- // allocator!
636- let any_dynamic_crate = tcx. dependency_formats ( ( ) ) . iter ( ) . any ( |( _, list) | {
634+ // linkage for all crate types we may link as, then it's already got an
635+ // allocator shim and we'll be using that one instead. If nothing exists
636+ // then it's our job to generate the allocator! If crate types disagree
637+ // about whether an allocator shim is necessary or not, we generate one
638+ // and let needs_allocator_shim_for_linking decide at link time whether or
639+ // not to use it for any particular linker invocation.
640+ let all_crate_types_any_dynamic_crate = tcx. dependency_formats ( ( ) ) . iter ( ) . all ( |( _, list) | {
637641 use rustc_middle:: middle:: dependency_format:: Linkage ;
638642 list. iter ( ) . any ( |& linkage| linkage == Linkage :: Dynamic )
639643 } ) ;
640- if any_dynamic_crate { None } else { tcx. allocator_kind ( ( ) ) }
644+ if all_crate_types_any_dynamic_crate { None } else { tcx. allocator_kind ( ( ) ) }
645+ }
646+
647+ /// Decide if this particular crate type needs an allocator shim linked in.
648+ /// This may return true even when allocator_kind_for_codegen returns false. In
649+ /// this case no allocator shim shall be linked.
650+ pub ( crate ) fn needs_allocator_shim_for_linking (
651+ dependency_formats : & Dependencies ,
652+ crate_type : CrateType ,
653+ ) -> bool {
654+ use rustc_middle:: middle:: dependency_format:: Linkage ;
655+ let any_dynamic_crate =
656+ dependency_formats[ & crate_type] . iter ( ) . any ( |& linkage| linkage == Linkage :: Dynamic ) ;
657+ !any_dynamic_crate
641658}
642659
643660pub fn codegen_crate < B : ExtraBackendMethods > (
0 commit comments