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 => 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() {
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)
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(
168    tcx: TyCtxt<'_>,
169    _: LocalCrate,
170) -> &[(ExportedSymbol<'_>, SymbolExportInfo)] {
171    if !tcx.sess.opts.output_types.should_codegen() {
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        // The symbols created in this loop are sorted below it
325        #[allow(rustc::potential_query_instability)]
326        for (mono_item, data) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
327            if data.linkage != Linkage::External {
328                // We can only re-use things with external linkage, otherwise
329                // we'll get a linker error
330                continue;
331            }
332
333            if need_visibility && data.visibility == Visibility::Hidden {
334                // If we potentially share things from Rust dylibs, they must
335                // not be hidden
336                continue;
337            }
338
339            if !tcx.sess.opts.share_generics() {
340                if tcx.codegen_fn_attrs(mono_item.def_id()).inline
341                    == rustc_attr_parsing::InlineAttr::Never
342                {
343                    // this is OK, we explicitly allow sharing inline(never) across crates even
344                    // without share-generics.
345                } else {
346                    continue;
347                }
348            }
349
350            match *mono_item {
351                MonoItem::Fn(Instance { def: InstanceKind::Item(def), args }) => {
352                    if args.non_erasable_generics().next().is_some() {
353                        let symbol = ExportedSymbol::Generic(def, args);
354                        symbols.push((
355                            symbol,
356                            SymbolExportInfo {
357                                level: SymbolExportLevel::Rust,
358                                kind: SymbolExportKind::Text,
359                                used: false,
360                            },
361                        ));
362                    }
363                }
364                MonoItem::Fn(Instance { def: InstanceKind::DropGlue(_, Some(ty)), args }) => {
365                    // A little sanity-check
366                    assert_eq!(args.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)));
367                    symbols.push((
368                        ExportedSymbol::DropGlue(ty),
369                        SymbolExportInfo {
370                            level: SymbolExportLevel::Rust,
371                            kind: SymbolExportKind::Text,
372                            used: false,
373                        },
374                    ));
375                }
376                MonoItem::Fn(Instance {
377                    def: InstanceKind::AsyncDropGlueCtorShim(_, ty),
378                    args,
379                }) => {
380                    // A little sanity-check
381                    assert_eq!(args.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)));
382                    symbols.push((
383                        ExportedSymbol::AsyncDropGlueCtorShim(ty),
384                        SymbolExportInfo {
385                            level: SymbolExportLevel::Rust,
386                            kind: SymbolExportKind::Text,
387                            used: false,
388                        },
389                    ));
390                }
391                MonoItem::Fn(Instance { def: InstanceKind::AsyncDropGlue(def, ty), args: _ }) => {
392                    symbols.push((
393                        ExportedSymbol::AsyncDropGlue(def, ty),
394                        SymbolExportInfo {
395                            level: SymbolExportLevel::Rust,
396                            kind: SymbolExportKind::Text,
397                            used: false,
398                        },
399                    ));
400                }
401                _ => {
402                    // Any other symbols don't qualify for sharing
403                }
404            }
405        }
406    }
407
408    // Sort so we get a stable incr. comp. hash.
409    symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
410
411    tcx.arena.alloc_from_iter(symbols)
412}
413
414fn upstream_monomorphizations_provider(
415    tcx: TyCtxt<'_>,
416    (): (),
417) -> DefIdMap<UnordMap<GenericArgsRef<'_>, CrateNum>> {
418    let cnums = tcx.crates(());
419
420    let mut instances: DefIdMap<UnordMap<_, _>> = Default::default();
421
422    let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
423    let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();
424
425    for &cnum in cnums.iter() {
426        for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
427            let (def_id, args) = match *exported_symbol {
428                ExportedSymbol::Generic(def_id, args) => (def_id, args),
429                ExportedSymbol::DropGlue(ty) => {
430                    if let Some(drop_in_place_fn_def_id) = drop_in_place_fn_def_id {
431                        (drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
432                    } else {
433                        // `drop_in_place` in place does not exist, don't try
434                        // to use it.
435                        continue;
436                    }
437                }
438                ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
439                    if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
440                        (async_drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
441                    } else {
442                        continue;
443                    }
444                }
445                ExportedSymbol::AsyncDropGlue(def_id, ty) => (def_id, tcx.mk_args(&[ty.into()])),
446                ExportedSymbol::NonGeneric(..)
447                | ExportedSymbol::ThreadLocalShim(..)
448                | ExportedSymbol::NoDefId(..) => {
449                    // These are no monomorphizations
450                    continue;
451                }
452            };
453
454            let args_map = instances.entry(def_id).or_default();
455
456            match args_map.entry(args) {
457                Occupied(mut e) => {
458                    // If there are multiple monomorphizations available,
459                    // we select one deterministically.
460                    let other_cnum = *e.get();
461                    if tcx.stable_crate_id(other_cnum) > tcx.stable_crate_id(cnum) {
462                        e.insert(cnum);
463                    }
464                }
465                Vacant(e) => {
466                    e.insert(cnum);
467                }
468            }
469        }
470    }
471
472    instances
473}
474
475fn upstream_monomorphizations_for_provider(
476    tcx: TyCtxt<'_>,
477    def_id: DefId,
478) -> Option<&UnordMap<GenericArgsRef<'_>, CrateNum>> {
479    assert!(!def_id.is_local());
480    tcx.upstream_monomorphizations(()).get(&def_id)
481}
482
483fn upstream_drop_glue_for_provider<'tcx>(
484    tcx: TyCtxt<'tcx>,
485    args: GenericArgsRef<'tcx>,
486) -> Option<CrateNum> {
487    let def_id = tcx.lang_items().drop_in_place_fn()?;
488    tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned()
489}
490
491fn upstream_async_drop_glue_for_provider<'tcx>(
492    tcx: TyCtxt<'tcx>,
493    args: GenericArgsRef<'tcx>,
494) -> Option<CrateNum> {
495    let def_id = tcx.lang_items().async_drop_in_place_fn()?;
496    tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned()
497}
498
499fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
500    !tcx.reachable_set(()).contains(&def_id)
501}
502
503pub(crate) fn provide(providers: &mut Providers) {
504    providers.reachable_non_generics = reachable_non_generics_provider;
505    providers.is_reachable_non_generic = is_reachable_non_generic_provider_local;
506    providers.exported_symbols = exported_symbols_provider_local;
507    providers.upstream_monomorphizations = upstream_monomorphizations_provider;
508    providers.is_unreachable_local_definition = is_unreachable_local_definition_provider;
509    providers.upstream_drop_glue_for = upstream_drop_glue_for_provider;
510    providers.upstream_async_drop_glue_for = upstream_async_drop_glue_for_provider;
511    providers.wasm_import_module_map = wasm_import_module_map;
512    providers.extern_queries.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
513    providers.extern_queries.upstream_monomorphizations_for =
514        upstream_monomorphizations_for_provider;
515}
516
517fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel {
518    // We export anything that's not mangled at the "C" layer as it probably has
519    // to do with ABI concerns. We do not, however, apply such treatment to
520    // special symbols in the standard library for various plumbing between
521    // core/std/allocators/etc. For example symbols used to hook up allocation
522    // are not considered for export
523    let codegen_fn_attrs = tcx.codegen_fn_attrs(sym_def_id);
524    let is_extern = codegen_fn_attrs.contains_extern_indicator();
525    let std_internal =
526        codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
527
528    if is_extern && !std_internal {
529        let target = &tcx.sess.target.llvm_target;
530        // WebAssembly cannot export data symbols, so reduce their export level
531        if target.contains("emscripten") {
532            if let DefKind::Static { .. } = tcx.def_kind(sym_def_id) {
533                return SymbolExportLevel::Rust;
534            }
535        }
536
537        SymbolExportLevel::C
538    } else {
539        SymbolExportLevel::Rust
540    }
541}
542
543/// This is the symbol name of the given instance instantiated in a specific crate.
544pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
545    tcx: TyCtxt<'tcx>,
546    symbol: ExportedSymbol<'tcx>,
547    instantiating_crate: CrateNum,
548) -> String {
549    // If this is something instantiated in the local crate then we might
550    // already have cached the name as a query result.
551    if instantiating_crate == LOCAL_CRATE {
552        return symbol.symbol_name_for_local_instance(tcx).to_string();
553    }
554
555    // This is something instantiated in an upstream crate, so we have to use
556    // the slower (because uncached) version of computing the symbol name.
557    match symbol {
558        ExportedSymbol::NonGeneric(def_id) => {
559            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
560                tcx,
561                Instance::mono(tcx, def_id),
562                instantiating_crate,
563            )
564        }
565        ExportedSymbol::Generic(def_id, args) => {
566            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
567                tcx,
568                Instance::new(def_id, args),
569                instantiating_crate,
570            )
571        }
572        ExportedSymbol::ThreadLocalShim(def_id) => {
573            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
574                tcx,
575                ty::Instance {
576                    def: ty::InstanceKind::ThreadLocalShim(def_id),
577                    args: ty::GenericArgs::empty(),
578                },
579                instantiating_crate,
580            )
581        }
582        ExportedSymbol::DropGlue(ty) => rustc_symbol_mangling::symbol_name_for_instance_in_crate(
583            tcx,
584            Instance::resolve_drop_in_place(tcx, ty),
585            instantiating_crate,
586        ),
587        ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
588            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
589                tcx,
590                Instance::resolve_async_drop_in_place(tcx, ty),
591                instantiating_crate,
592            )
593        }
594        ExportedSymbol::AsyncDropGlue(def_id, ty) => {
595            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
596                tcx,
597                Instance::resolve_async_drop_in_place_poll(tcx, def_id, ty),
598                instantiating_crate,
599            )
600        }
601        ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
602    }
603}
604
605fn calling_convention_for_symbol<'tcx>(
606    tcx: TyCtxt<'tcx>,
607    symbol: ExportedSymbol<'tcx>,
608) -> (Conv, &'tcx [rustc_target::callconv::ArgAbi<'tcx, Ty<'tcx>>]) {
609    let instance = match symbol {
610        ExportedSymbol::NonGeneric(def_id) | ExportedSymbol::Generic(def_id, _)
611            if tcx.is_static(def_id) =>
612        {
613            None
614        }
615        ExportedSymbol::NonGeneric(def_id) => Some(Instance::mono(tcx, def_id)),
616        ExportedSymbol::Generic(def_id, args) => Some(Instance::new(def_id, args)),
617        // DropGlue always use the Rust calling convention and thus follow the target's default
618        // symbol decoration scheme.
619        ExportedSymbol::DropGlue(..) => None,
620        // AsyncDropGlueCtorShim always use the Rust calling convention and thus follow the
621        // target's default symbol decoration scheme.
622        ExportedSymbol::AsyncDropGlueCtorShim(..) => None,
623        ExportedSymbol::AsyncDropGlue(..) => None,
624        // NoDefId always follow the target's default symbol decoration scheme.
625        ExportedSymbol::NoDefId(..) => None,
626        // ThreadLocalShim always follow the target's default symbol decoration scheme.
627        ExportedSymbol::ThreadLocalShim(..) => None,
628    };
629
630    instance
631        .map(|i| {
632            tcx.fn_abi_of_instance(
633                ty::TypingEnv::fully_monomorphized().as_query_input((i, ty::List::empty())),
634            )
635            .unwrap_or_else(|_| bug!("fn_abi_of_instance({i:?}) failed"))
636        })
637        .map(|fnabi| (fnabi.conv, &fnabi.args[..]))
638        // FIXME(workingjubilee): why don't we know the convention here?
639        .unwrap_or((Conv::Rust, &[]))
640}
641
642/// This is the symbol name of the given instance as seen by the linker.
643///
644/// On 32-bit Windows symbols are decorated according to their calling conventions.
645pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>(
646    tcx: TyCtxt<'tcx>,
647    symbol: ExportedSymbol<'tcx>,
648    instantiating_crate: CrateNum,
649) -> String {
650    let mut undecorated = symbol_name_for_instance_in_crate(tcx, symbol, instantiating_crate);
651
652    // thread local will not be a function call,
653    // so it is safe to return before windows symbol decoration check.
654    if let Some(name) = maybe_emutls_symbol_name(tcx, symbol, &undecorated) {
655        return name;
656    }
657
658    let target = &tcx.sess.target;
659    if !target.is_like_windows {
660        // Mach-O has a global "_" suffix and `object` crate will handle it.
661        // ELF does not have any symbol decorations.
662        return undecorated;
663    }
664
665    let prefix = match &target.arch[..] {
666        "x86" => Some('_'),
667        "x86_64" => None,
668        "arm64ec" => Some('#'),
669        // Only x86/64 use symbol decorations.
670        _ => return undecorated,
671    };
672
673    let (conv, args) = calling_convention_for_symbol(tcx, symbol);
674
675    // Decorate symbols with prefixes, suffixes and total number of bytes of arguments.
676    // Reference: https://docs.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170
677    let (prefix, suffix) = match conv {
678        Conv::X86Fastcall => ("@", "@"),
679        Conv::X86Stdcall => ("_", "@"),
680        Conv::X86VectorCall => ("", "@@"),
681        _ => {
682            if let Some(prefix) = prefix {
683                undecorated.insert(0, prefix);
684            }
685            return undecorated;
686        }
687    };
688
689    let args_in_bytes: u64 = args
690        .iter()
691        .map(|abi| abi.layout.size.bytes().next_multiple_of(target.pointer_width as u64 / 8))
692        .sum();
693    format!("{prefix}{undecorated}{suffix}{args_in_bytes}")
694}
695
696pub(crate) fn exporting_symbol_name_for_instance_in_crate<'tcx>(
697    tcx: TyCtxt<'tcx>,
698    symbol: ExportedSymbol<'tcx>,
699    cnum: CrateNum,
700) -> String {
701    let undecorated = symbol_name_for_instance_in_crate(tcx, symbol, cnum);
702    maybe_emutls_symbol_name(tcx, symbol, &undecorated).unwrap_or(undecorated)
703}
704
705/// On amdhsa, `gpu-kernel` functions have an associated metadata object with a `.kd` suffix.
706/// Add it to the symbols list for all kernel functions, so that it is exported in the linked
707/// object.
708pub(crate) fn extend_exported_symbols<'tcx>(
709    symbols: &mut Vec<String>,
710    tcx: TyCtxt<'tcx>,
711    symbol: ExportedSymbol<'tcx>,
712    instantiating_crate: CrateNum,
713) {
714    let (conv, _) = calling_convention_for_symbol(tcx, symbol);
715
716    if conv != Conv::GpuKernel || tcx.sess.target.os != "amdhsa" {
717        return;
718    }
719
720    let undecorated = symbol_name_for_instance_in_crate(tcx, symbol, instantiating_crate);
721
722    // Add the symbol for the kernel descriptor (with .kd suffix)
723    symbols.push(format!("{undecorated}.kd"));
724}
725
726fn maybe_emutls_symbol_name<'tcx>(
727    tcx: TyCtxt<'tcx>,
728    symbol: ExportedSymbol<'tcx>,
729    undecorated: &str,
730) -> Option<String> {
731    if matches!(tcx.sess.tls_model(), TlsModel::Emulated)
732        && let ExportedSymbol::NonGeneric(def_id) = symbol
733        && tcx.is_thread_local_static(def_id)
734    {
735        // When using emutls, LLVM will add the `__emutls_v.` prefix to thread local symbols,
736        // and exported symbol name need to match this.
737        Some(format!("__emutls_v.{undecorated}"))
738    } else {
739        None
740    }
741}
742
743fn wasm_import_module_map(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<String> {
744    // Build up a map from DefId to a `NativeLib` structure, where
745    // `NativeLib` internally contains information about
746    // `#[link(wasm_import_module = "...")]` for example.
747    let native_libs = tcx.native_libraries(cnum);
748
749    let def_id_to_native_lib = native_libs
750        .iter()
751        .filter_map(|lib| lib.foreign_module.map(|id| (id, lib)))
752        .collect::<DefIdMap<_>>();
753
754    let mut ret = DefIdMap::default();
755    for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
756        let module = def_id_to_native_lib.get(def_id).and_then(|s| s.wasm_import_module());
757        let Some(module) = module else { continue };
758        ret.extend(lib.foreign_items.iter().map(|id| {
759            assert_eq!(id.krate, cnum);
760            (*id, module.to_string())
761        }));
762    }
763
764    ret
765}