rustc_codegen_ssa/back/
symbol_export.rs

1use std::collections::hash_map::Entry::*;
2
3use rustc_ast::expand::allocator::{ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE, global_fn_name};
4use rustc_data_structures::unord::UnordMap;
5use rustc_hir::def::DefKind;
6use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE, LocalDefId};
7use rustc_middle::bug;
8use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
9use rustc_middle::middle::exported_symbols::{
10    ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel, metadata_symbol_name,
11};
12use rustc_middle::query::LocalCrate;
13use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, Instance, SymbolName, Ty, TyCtxt};
14use rustc_middle::util::Providers;
15use rustc_session::config::{CrateType, OomStrategy};
16use rustc_symbol_mangling::mangle_internal_symbol;
17use rustc_target::callconv::Conv;
18use rustc_target::spec::{SanitizerSet, TlsModel};
19use tracing::debug;
20
21use crate::base::allocator_kind_for_codegen;
22
23fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
24    crates_export_threshold(tcx.crate_types())
25}
26
27fn crate_export_threshold(crate_type: CrateType) -> SymbolExportLevel {
28    match crate_type {
29        CrateType::Executable | CrateType::Staticlib | CrateType::ProcMacro | CrateType::Cdylib => {
30            SymbolExportLevel::C
31        }
32        CrateType::Rlib | CrateType::Dylib | CrateType::Sdylib => SymbolExportLevel::Rust,
33    }
34}
35
36pub fn crates_export_threshold(crate_types: &[CrateType]) -> SymbolExportLevel {
37    if crate_types
38        .iter()
39        .any(|&crate_type| crate_export_threshold(crate_type) == SymbolExportLevel::Rust)
40    {
41        SymbolExportLevel::Rust
42    } else {
43        SymbolExportLevel::C
44    }
45}
46
47fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<SymbolExportInfo> {
48    if !tcx.sess.opts.output_types.should_codegen() && !tcx.is_sdylib_interface_build() {
49        return Default::default();
50    }
51
52    // Check to see if this crate is a "special runtime crate". These
53    // crates, implementation details of the standard library, typically
54    // have a bunch of `pub extern` and `#[no_mangle]` functions as the
55    // ABI between them. We don't want their symbols to have a `C`
56    // export level, however, as they're just implementation details.
57    // Down below we'll hardwire all of the symbols to the `Rust` export
58    // level instead.
59    let special_runtime_crate =
60        tcx.is_panic_runtime(LOCAL_CRATE) || tcx.is_compiler_builtins(LOCAL_CRATE);
61
62    let mut reachable_non_generics: DefIdMap<_> = tcx
63        .reachable_set(())
64        .items()
65        .filter_map(|&def_id| {
66            // We want to ignore some FFI functions that are not exposed from
67            // this crate. Reachable FFI functions can be lumped into two
68            // categories:
69            //
70            // 1. Those that are included statically via a static library
71            // 2. Those included otherwise (e.g., dynamically or via a framework)
72            //
73            // Although our LLVM module is not literally emitting code for the
74            // statically included symbols, it's an export of our library which
75            // needs to be passed on to the linker and encoded in the metadata.
76            //
77            // As a result, if this id is an FFI item (foreign item) then we only
78            // let it through if it's included statically.
79            if let Some(parent_id) = tcx.opt_local_parent(def_id)
80                && let DefKind::ForeignMod = tcx.def_kind(parent_id)
81            {
82                let library = tcx.native_library(def_id)?;
83                return library.kind.is_statically_included().then_some(def_id);
84            }
85
86            // Only consider nodes that actually have exported symbols.
87            match tcx.def_kind(def_id) {
88                DefKind::Fn | DefKind::Static { .. } => {}
89                DefKind::AssocFn if tcx.impl_of_method(def_id.to_def_id()).is_some() => {}
90                _ => return None,
91            };
92
93            let generics = tcx.generics_of(def_id);
94            if generics.requires_monomorphization(tcx) {
95                return None;
96            }
97
98            if Instance::mono(tcx, def_id.into()).def.requires_inline(tcx) {
99                return None;
100            }
101
102            if tcx.cross_crate_inlinable(def_id) { None } else { Some(def_id) }
103        })
104        .map(|def_id| {
105            // We won't link right if this symbol is stripped during LTO.
106            let name = tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())).name;
107            let used = name == "rust_eh_personality";
108
109            let export_level = if special_runtime_crate {
110                SymbolExportLevel::Rust
111            } else {
112                symbol_export_level(tcx, def_id.to_def_id())
113            };
114            let codegen_attrs = tcx.codegen_fn_attrs(def_id.to_def_id());
115            debug!(
116                "EXPORTED SYMBOL (local): {} ({:?})",
117                tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())),
118                export_level
119            );
120            let info = SymbolExportInfo {
121                level: export_level,
122                kind: if tcx.is_static(def_id.to_def_id()) {
123                    if codegen_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
124                        SymbolExportKind::Tls
125                    } else {
126                        SymbolExportKind::Data
127                    }
128                } else {
129                    SymbolExportKind::Text
130                },
131                used: codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
132                    || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
133                    || used,
134            };
135            (def_id.to_def_id(), info)
136        })
137        .into();
138
139    if let Some(id) = tcx.proc_macro_decls_static(()) {
140        reachable_non_generics.insert(
141            id.to_def_id(),
142            SymbolExportInfo {
143                level: SymbolExportLevel::C,
144                kind: SymbolExportKind::Data,
145                used: false,
146            },
147        );
148    }
149
150    reachable_non_generics
151}
152
153fn is_reachable_non_generic_provider_local(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
154    let export_threshold = threshold(tcx);
155
156    if let Some(&info) = tcx.reachable_non_generics(LOCAL_CRATE).get(&def_id.to_def_id()) {
157        info.level.is_below_threshold(export_threshold)
158    } else {
159        false
160    }
161}
162
163fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
164    tcx.reachable_non_generics(def_id.krate).contains_key(&def_id)
165}
166
167fn exported_symbols_provider_local<'tcx>(
168    tcx: TyCtxt<'tcx>,
169    _: LocalCrate,
170) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
171    if !tcx.sess.opts.output_types.should_codegen() && !tcx.is_sdylib_interface_build() {
172        return &[];
173    }
174
175    // FIXME: Sorting this is unnecessary since we are sorting later anyway.
176    //        Can we skip the later sorting?
177    let sorted = tcx.with_stable_hashing_context(|hcx| {
178        tcx.reachable_non_generics(LOCAL_CRATE).to_sorted(&hcx, true)
179    });
180
181    let mut symbols: Vec<_> =
182        sorted.iter().map(|&(&def_id, &info)| (ExportedSymbol::NonGeneric(def_id), info)).collect();
183
184    // Export TLS shims
185    if !tcx.sess.target.dll_tls_export {
186        symbols.extend(sorted.iter().filter_map(|&(&def_id, &info)| {
187            tcx.needs_thread_local_shim(def_id).then(|| {
188                (
189                    ExportedSymbol::ThreadLocalShim(def_id),
190                    SymbolExportInfo {
191                        level: info.level,
192                        kind: SymbolExportKind::Text,
193                        used: info.used,
194                    },
195                )
196            })
197        }))
198    }
199
200    if tcx.entry_fn(()).is_some() {
201        let exported_symbol =
202            ExportedSymbol::NoDefId(SymbolName::new(tcx, tcx.sess.target.entry_name.as_ref()));
203
204        symbols.push((
205            exported_symbol,
206            SymbolExportInfo {
207                level: SymbolExportLevel::C,
208                kind: SymbolExportKind::Text,
209                used: false,
210            },
211        ));
212    }
213
214    // Mark allocator shim symbols as exported only if they were generated.
215    if allocator_kind_for_codegen(tcx).is_some() {
216        for symbol_name in ALLOCATOR_METHODS
217            .iter()
218            .map(|method| mangle_internal_symbol(tcx, global_fn_name(method.name).as_str()))
219            .chain([
220                mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
221                mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
222            ])
223        {
224            let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
225
226            symbols.push((
227                exported_symbol,
228                SymbolExportInfo {
229                    level: SymbolExportLevel::Rust,
230                    kind: SymbolExportKind::Text,
231                    used: false,
232                },
233            ));
234        }
235
236        let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(
237            tcx,
238            &mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
239        ));
240        symbols.push((
241            exported_symbol,
242            SymbolExportInfo {
243                level: SymbolExportLevel::Rust,
244                kind: SymbolExportKind::Data,
245                used: false,
246            },
247        ))
248    }
249
250    if tcx.sess.instrument_coverage() || tcx.sess.opts.cg.profile_generate.enabled() {
251        // These are weak symbols that point to the profile version and the
252        // profile name, which need to be treated as exported so LTO doesn't nix
253        // them.
254        const PROFILER_WEAK_SYMBOLS: [&str; 2] =
255            ["__llvm_profile_raw_version", "__llvm_profile_filename"];
256
257        symbols.extend(PROFILER_WEAK_SYMBOLS.iter().map(|sym| {
258            let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
259            (
260                exported_symbol,
261                SymbolExportInfo {
262                    level: SymbolExportLevel::C,
263                    kind: SymbolExportKind::Data,
264                    used: false,
265                },
266            )
267        }));
268    }
269
270    if tcx.sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::MEMORY) {
271        let mut msan_weak_symbols = Vec::new();
272
273        // Similar to profiling, preserve weak msan symbol during LTO.
274        if tcx.sess.opts.unstable_opts.sanitizer_recover.contains(SanitizerSet::MEMORY) {
275            msan_weak_symbols.push("__msan_keep_going");
276        }
277
278        if tcx.sess.opts.unstable_opts.sanitizer_memory_track_origins != 0 {
279            msan_weak_symbols.push("__msan_track_origins");
280        }
281
282        symbols.extend(msan_weak_symbols.into_iter().map(|sym| {
283            let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
284            (
285                exported_symbol,
286                SymbolExportInfo {
287                    level: SymbolExportLevel::C,
288                    kind: SymbolExportKind::Data,
289                    used: false,
290                },
291            )
292        }));
293    }
294
295    if tcx.crate_types().contains(&CrateType::Dylib)
296        || tcx.crate_types().contains(&CrateType::ProcMacro)
297    {
298        let symbol_name = metadata_symbol_name(tcx);
299        let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
300
301        symbols.push((
302            exported_symbol,
303            SymbolExportInfo {
304                level: SymbolExportLevel::C,
305                kind: SymbolExportKind::Data,
306                used: true,
307            },
308        ));
309    }
310
311    if tcx.local_crate_exports_generics() {
312        use rustc_middle::mir::mono::{Linkage, MonoItem, Visibility};
313        use rustc_middle::ty::InstanceKind;
314
315        // Normally, we require that shared monomorphizations are not hidden,
316        // because if we want to re-use a monomorphization from a Rust dylib, it
317        // needs to be exported.
318        // However, on platforms that don't allow for Rust dylibs, having
319        // external linkage is enough for monomorphization to be linked to.
320        let need_visibility = tcx.sess.target.dynamic_linking && !tcx.sess.target.only_cdylib;
321
322        let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;
323
324        // Do not export symbols that cannot be instantiated by downstream crates.
325        let reachable_set = tcx.reachable_set(());
326        let is_local_to_current_crate = |ty: Ty<'_>| {
327            let no_refs = ty.peel_refs();
328            let root_def_id = match no_refs.kind() {
329                ty::Closure(closure, _) => *closure,
330                ty::FnDef(def_id, _) => *def_id,
331                ty::Coroutine(def_id, _) => *def_id,
332                ty::CoroutineClosure(def_id, _) => *def_id,
333                ty::CoroutineWitness(def_id, _) => *def_id,
334                _ => return false,
335            };
336            let Some(root_def_id) = root_def_id.as_local() else {
337                return false;
338            };
339
340            let is_local = !reachable_set.contains(&root_def_id);
341            is_local
342        };
343
344        let is_instantiable_downstream =
345            |did: Option<DefId>, generic_args: GenericArgsRef<'tcx>| {
346                generic_args
347                    .types()
348                    .chain(did.into_iter().map(move |did| tcx.type_of(did).skip_binder()))
349                    .all(move |arg| {
350                        arg.walk().all(|ty| {
351                            ty.as_type().map_or(true, |ty| !is_local_to_current_crate(ty))
352                        })
353                    })
354            };
355
356        // The symbols created in this loop are sorted below it
357        #[allow(rustc::potential_query_instability)]
358        for (mono_item, data) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
359            if data.linkage != Linkage::External {
360                // We can only re-use things with external linkage, otherwise
361                // we'll get a linker error
362                continue;
363            }
364
365            if need_visibility && data.visibility == Visibility::Hidden {
366                // If we potentially share things from Rust dylibs, they must
367                // not be hidden
368                continue;
369            }
370
371            if !tcx.sess.opts.share_generics() {
372                if tcx.codegen_fn_attrs(mono_item.def_id()).inline
373                    == rustc_attr_data_structures::InlineAttr::Never
374                {
375                    // this is OK, we explicitly allow sharing inline(never) across crates even
376                    // without share-generics.
377                } else {
378                    continue;
379                }
380            }
381
382            match *mono_item {
383                MonoItem::Fn(Instance { def: InstanceKind::Item(def), args }) => {
384                    let has_generics = args.non_erasable_generics().next().is_some();
385
386                    let should_export =
387                        has_generics && is_instantiable_downstream(Some(def), &args);
388
389                    if should_export {
390                        let symbol = ExportedSymbol::Generic(def, args);
391                        symbols.push((
392                            symbol,
393                            SymbolExportInfo {
394                                level: SymbolExportLevel::Rust,
395                                kind: SymbolExportKind::Text,
396                                used: false,
397                            },
398                        ));
399                    }
400                }
401                MonoItem::Fn(Instance { def: InstanceKind::DropGlue(_, Some(ty)), args }) => {
402                    // A little sanity-check
403                    assert_eq!(args.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)));
404
405                    // Drop glue did is always going to be non-local outside of libcore, thus we don't need to check it's locality (which includes invoking `type_of` query).
406                    let should_export = match ty.kind() {
407                        ty::Adt(_, args) => is_instantiable_downstream(None, args),
408                        ty::Closure(_, args) => is_instantiable_downstream(None, args),
409                        _ => true,
410                    };
411
412                    if should_export {
413                        symbols.push((
414                            ExportedSymbol::DropGlue(ty),
415                            SymbolExportInfo {
416                                level: SymbolExportLevel::Rust,
417                                kind: SymbolExportKind::Text,
418                                used: false,
419                            },
420                        ));
421                    }
422                }
423                MonoItem::Fn(Instance {
424                    def: InstanceKind::AsyncDropGlueCtorShim(_, ty),
425                    args,
426                }) => {
427                    // A little sanity-check
428                    assert_eq!(args.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)));
429                    symbols.push((
430                        ExportedSymbol::AsyncDropGlueCtorShim(ty),
431                        SymbolExportInfo {
432                            level: SymbolExportLevel::Rust,
433                            kind: SymbolExportKind::Text,
434                            used: false,
435                        },
436                    ));
437                }
438                MonoItem::Fn(Instance { def: InstanceKind::AsyncDropGlue(def, ty), args: _ }) => {
439                    symbols.push((
440                        ExportedSymbol::AsyncDropGlue(def, ty),
441                        SymbolExportInfo {
442                            level: SymbolExportLevel::Rust,
443                            kind: SymbolExportKind::Text,
444                            used: false,
445                        },
446                    ));
447                }
448                _ => {
449                    // Any other symbols don't qualify for sharing
450                }
451            }
452        }
453    }
454
455    // Sort so we get a stable incr. comp. hash.
456    symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
457
458    tcx.arena.alloc_from_iter(symbols)
459}
460
461fn upstream_monomorphizations_provider(
462    tcx: TyCtxt<'_>,
463    (): (),
464) -> DefIdMap<UnordMap<GenericArgsRef<'_>, CrateNum>> {
465    let cnums = tcx.crates(());
466
467    let mut instances: DefIdMap<UnordMap<_, _>> = Default::default();
468
469    let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
470    let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();
471
472    for &cnum in cnums.iter() {
473        for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
474            let (def_id, args) = match *exported_symbol {
475                ExportedSymbol::Generic(def_id, args) => (def_id, args),
476                ExportedSymbol::DropGlue(ty) => {
477                    if let Some(drop_in_place_fn_def_id) = drop_in_place_fn_def_id {
478                        (drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
479                    } else {
480                        // `drop_in_place` in place does not exist, don't try
481                        // to use it.
482                        continue;
483                    }
484                }
485                ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
486                    if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
487                        (async_drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
488                    } else {
489                        continue;
490                    }
491                }
492                ExportedSymbol::AsyncDropGlue(def_id, ty) => (def_id, tcx.mk_args(&[ty.into()])),
493                ExportedSymbol::NonGeneric(..)
494                | ExportedSymbol::ThreadLocalShim(..)
495                | ExportedSymbol::NoDefId(..) => {
496                    // These are no monomorphizations
497                    continue;
498                }
499            };
500
501            let args_map = instances.entry(def_id).or_default();
502
503            match args_map.entry(args) {
504                Occupied(mut e) => {
505                    // If there are multiple monomorphizations available,
506                    // we select one deterministically.
507                    let other_cnum = *e.get();
508                    if tcx.stable_crate_id(other_cnum) > tcx.stable_crate_id(cnum) {
509                        e.insert(cnum);
510                    }
511                }
512                Vacant(e) => {
513                    e.insert(cnum);
514                }
515            }
516        }
517    }
518
519    instances
520}
521
522fn upstream_monomorphizations_for_provider(
523    tcx: TyCtxt<'_>,
524    def_id: DefId,
525) -> Option<&UnordMap<GenericArgsRef<'_>, CrateNum>> {
526    assert!(!def_id.is_local());
527    tcx.upstream_monomorphizations(()).get(&def_id)
528}
529
530fn upstream_drop_glue_for_provider<'tcx>(
531    tcx: TyCtxt<'tcx>,
532    args: GenericArgsRef<'tcx>,
533) -> Option<CrateNum> {
534    let def_id = tcx.lang_items().drop_in_place_fn()?;
535    tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned()
536}
537
538fn upstream_async_drop_glue_for_provider<'tcx>(
539    tcx: TyCtxt<'tcx>,
540    args: GenericArgsRef<'tcx>,
541) -> Option<CrateNum> {
542    let def_id = tcx.lang_items().async_drop_in_place_fn()?;
543    tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned()
544}
545
546fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
547    !tcx.reachable_set(()).contains(&def_id)
548}
549
550pub(crate) fn provide(providers: &mut Providers) {
551    providers.reachable_non_generics = reachable_non_generics_provider;
552    providers.is_reachable_non_generic = is_reachable_non_generic_provider_local;
553    providers.exported_symbols = exported_symbols_provider_local;
554    providers.upstream_monomorphizations = upstream_monomorphizations_provider;
555    providers.is_unreachable_local_definition = is_unreachable_local_definition_provider;
556    providers.upstream_drop_glue_for = upstream_drop_glue_for_provider;
557    providers.upstream_async_drop_glue_for = upstream_async_drop_glue_for_provider;
558    providers.wasm_import_module_map = wasm_import_module_map;
559    providers.extern_queries.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
560    providers.extern_queries.upstream_monomorphizations_for =
561        upstream_monomorphizations_for_provider;
562}
563
564fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel {
565    // We export anything that's not mangled at the "C" layer as it probably has
566    // to do with ABI concerns. We do not, however, apply such treatment to
567    // special symbols in the standard library for various plumbing between
568    // core/std/allocators/etc. For example symbols used to hook up allocation
569    // are not considered for export
570    let codegen_fn_attrs = tcx.codegen_fn_attrs(sym_def_id);
571    let is_extern = codegen_fn_attrs.contains_extern_indicator();
572    let std_internal =
573        codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
574
575    if is_extern && !std_internal {
576        let target = &tcx.sess.target.llvm_target;
577        // WebAssembly cannot export data symbols, so reduce their export level
578        if target.contains("emscripten") {
579            if let DefKind::Static { .. } = tcx.def_kind(sym_def_id) {
580                return SymbolExportLevel::Rust;
581            }
582        }
583
584        SymbolExportLevel::C
585    } else {
586        SymbolExportLevel::Rust
587    }
588}
589
590/// This is the symbol name of the given instance instantiated in a specific crate.
591pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
592    tcx: TyCtxt<'tcx>,
593    symbol: ExportedSymbol<'tcx>,
594    instantiating_crate: CrateNum,
595) -> String {
596    // If this is something instantiated in the local crate then we might
597    // already have cached the name as a query result.
598    if instantiating_crate == LOCAL_CRATE {
599        return symbol.symbol_name_for_local_instance(tcx).to_string();
600    }
601
602    // This is something instantiated in an upstream crate, so we have to use
603    // the slower (because uncached) version of computing the symbol name.
604    match symbol {
605        ExportedSymbol::NonGeneric(def_id) => {
606            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
607                tcx,
608                Instance::mono(tcx, def_id),
609                instantiating_crate,
610            )
611        }
612        ExportedSymbol::Generic(def_id, args) => {
613            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
614                tcx,
615                Instance::new_raw(def_id, args),
616                instantiating_crate,
617            )
618        }
619        ExportedSymbol::ThreadLocalShim(def_id) => {
620            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
621                tcx,
622                ty::Instance {
623                    def: ty::InstanceKind::ThreadLocalShim(def_id),
624                    args: ty::GenericArgs::empty(),
625                },
626                instantiating_crate,
627            )
628        }
629        ExportedSymbol::DropGlue(ty) => rustc_symbol_mangling::symbol_name_for_instance_in_crate(
630            tcx,
631            Instance::resolve_drop_in_place(tcx, ty),
632            instantiating_crate,
633        ),
634        ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
635            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
636                tcx,
637                Instance::resolve_async_drop_in_place(tcx, ty),
638                instantiating_crate,
639            )
640        }
641        ExportedSymbol::AsyncDropGlue(def_id, ty) => {
642            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
643                tcx,
644                Instance::resolve_async_drop_in_place_poll(tcx, def_id, ty),
645                instantiating_crate,
646            )
647        }
648        ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
649    }
650}
651
652fn calling_convention_for_symbol<'tcx>(
653    tcx: TyCtxt<'tcx>,
654    symbol: ExportedSymbol<'tcx>,
655) -> (Conv, &'tcx [rustc_target::callconv::ArgAbi<'tcx, Ty<'tcx>>]) {
656    let instance = match symbol {
657        ExportedSymbol::NonGeneric(def_id) | ExportedSymbol::Generic(def_id, _)
658            if tcx.is_static(def_id) =>
659        {
660            None
661        }
662        ExportedSymbol::NonGeneric(def_id) => Some(Instance::mono(tcx, def_id)),
663        ExportedSymbol::Generic(def_id, args) => Some(Instance::new_raw(def_id, args)),
664        // DropGlue always use the Rust calling convention and thus follow the target's default
665        // symbol decoration scheme.
666        ExportedSymbol::DropGlue(..) => None,
667        // AsyncDropGlueCtorShim always use the Rust calling convention and thus follow the
668        // target's default symbol decoration scheme.
669        ExportedSymbol::AsyncDropGlueCtorShim(..) => None,
670        ExportedSymbol::AsyncDropGlue(..) => None,
671        // NoDefId always follow the target's default symbol decoration scheme.
672        ExportedSymbol::NoDefId(..) => None,
673        // ThreadLocalShim always follow the target's default symbol decoration scheme.
674        ExportedSymbol::ThreadLocalShim(..) => None,
675    };
676
677    instance
678        .map(|i| {
679            tcx.fn_abi_of_instance(
680                ty::TypingEnv::fully_monomorphized().as_query_input((i, ty::List::empty())),
681            )
682            .unwrap_or_else(|_| bug!("fn_abi_of_instance({i:?}) failed"))
683        })
684        .map(|fnabi| (fnabi.conv, &fnabi.args[..]))
685        // FIXME(workingjubilee): why don't we know the convention here?
686        .unwrap_or((Conv::Rust, &[]))
687}
688
689/// This is the symbol name of the given instance as seen by the linker.
690///
691/// On 32-bit Windows symbols are decorated according to their calling conventions.
692pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>(
693    tcx: TyCtxt<'tcx>,
694    symbol: ExportedSymbol<'tcx>,
695    instantiating_crate: CrateNum,
696) -> String {
697    let mut undecorated = symbol_name_for_instance_in_crate(tcx, symbol, instantiating_crate);
698
699    // thread local will not be a function call,
700    // so it is safe to return before windows symbol decoration check.
701    if let Some(name) = maybe_emutls_symbol_name(tcx, symbol, &undecorated) {
702        return name;
703    }
704
705    let target = &tcx.sess.target;
706    if !target.is_like_windows {
707        // Mach-O has a global "_" suffix and `object` crate will handle it.
708        // ELF does not have any symbol decorations.
709        return undecorated;
710    }
711
712    let prefix = match &target.arch[..] {
713        "x86" => Some('_'),
714        "x86_64" => None,
715        "arm64ec" => Some('#'),
716        // Only x86/64 use symbol decorations.
717        _ => return undecorated,
718    };
719
720    let (conv, args) = calling_convention_for_symbol(tcx, symbol);
721
722    // Decorate symbols with prefixes, suffixes and total number of bytes of arguments.
723    // Reference: https://docs.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170
724    let (prefix, suffix) = match conv {
725        Conv::X86Fastcall => ("@", "@"),
726        Conv::X86Stdcall => ("_", "@"),
727        Conv::X86VectorCall => ("", "@@"),
728        _ => {
729            if let Some(prefix) = prefix {
730                undecorated.insert(0, prefix);
731            }
732            return undecorated;
733        }
734    };
735
736    let args_in_bytes: u64 = args
737        .iter()
738        .map(|abi| abi.layout.size.bytes().next_multiple_of(target.pointer_width as u64 / 8))
739        .sum();
740    format!("{prefix}{undecorated}{suffix}{args_in_bytes}")
741}
742
743pub(crate) fn exporting_symbol_name_for_instance_in_crate<'tcx>(
744    tcx: TyCtxt<'tcx>,
745    symbol: ExportedSymbol<'tcx>,
746    cnum: CrateNum,
747) -> String {
748    let undecorated = symbol_name_for_instance_in_crate(tcx, symbol, cnum);
749    maybe_emutls_symbol_name(tcx, symbol, &undecorated).unwrap_or(undecorated)
750}
751
752/// On amdhsa, `gpu-kernel` functions have an associated metadata object with a `.kd` suffix.
753/// Add it to the symbols list for all kernel functions, so that it is exported in the linked
754/// object.
755pub(crate) fn extend_exported_symbols<'tcx>(
756    symbols: &mut Vec<String>,
757    tcx: TyCtxt<'tcx>,
758    symbol: ExportedSymbol<'tcx>,
759    instantiating_crate: CrateNum,
760) {
761    let (conv, _) = calling_convention_for_symbol(tcx, symbol);
762
763    if conv != Conv::GpuKernel || tcx.sess.target.os != "amdhsa" {
764        return;
765    }
766
767    let undecorated = symbol_name_for_instance_in_crate(tcx, symbol, instantiating_crate);
768
769    // Add the symbol for the kernel descriptor (with .kd suffix)
770    symbols.push(format!("{undecorated}.kd"));
771}
772
773fn maybe_emutls_symbol_name<'tcx>(
774    tcx: TyCtxt<'tcx>,
775    symbol: ExportedSymbol<'tcx>,
776    undecorated: &str,
777) -> Option<String> {
778    if matches!(tcx.sess.tls_model(), TlsModel::Emulated)
779        && let ExportedSymbol::NonGeneric(def_id) = symbol
780        && tcx.is_thread_local_static(def_id)
781    {
782        // When using emutls, LLVM will add the `__emutls_v.` prefix to thread local symbols,
783        // and exported symbol name need to match this.
784        Some(format!("__emutls_v.{undecorated}"))
785    } else {
786        None
787    }
788}
789
790fn wasm_import_module_map(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<String> {
791    // Build up a map from DefId to a `NativeLib` structure, where
792    // `NativeLib` internally contains information about
793    // `#[link(wasm_import_module = "...")]` for example.
794    let native_libs = tcx.native_libraries(cnum);
795
796    let def_id_to_native_lib = native_libs
797        .iter()
798        .filter_map(|lib| lib.foreign_module.map(|id| (id, lib)))
799        .collect::<DefIdMap<_>>();
800
801    let mut ret = DefIdMap::default();
802    for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
803        let module = def_id_to_native_lib.get(def_id).and_then(|s| s.wasm_import_module());
804        let Some(module) = module else { continue };
805        ret.extend(lib.foreign_items.iter().map(|id| {
806            assert_eq!(id.krate, cnum);
807            (*id, module.to_string())
808        }));
809    }
810
811    ret
812}