rustc_codegen_ssa/back/
symbol_export.rs

1use std::collections::hash_map::Entry::*;
2
3use rustc_abi::{CanonAbi, X86Call};
4use rustc_ast::expand::allocator::{ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE, global_fn_name};
5use rustc_data_structures::unord::UnordMap;
6use rustc_hir::def::DefKind;
7use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE, LocalDefId};
8use rustc_middle::bug;
9use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
10use rustc_middle::middle::exported_symbols::{
11    ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
12};
13use rustc_middle::query::LocalCrate;
14use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, Instance, SymbolName, Ty, TyCtxt};
15use rustc_middle::util::Providers;
16use rustc_session::config::{CrateType, OomStrategy};
17use rustc_symbol_mangling::mangle_internal_symbol;
18use rustc_target::spec::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_assoc(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                rustc_std_internal_symbol: codegen_attrs
135                    .flags
136                    .contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL),
137            };
138            (def_id.to_def_id(), info)
139        })
140        .into();
141
142    if let Some(id) = tcx.proc_macro_decls_static(()) {
143        reachable_non_generics.insert(
144            id.to_def_id(),
145            SymbolExportInfo {
146                level: SymbolExportLevel::C,
147                kind: SymbolExportKind::Data,
148                used: false,
149                rustc_std_internal_symbol: false,
150            },
151        );
152    }
153
154    reachable_non_generics
155}
156
157fn is_reachable_non_generic_provider_local(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
158    let export_threshold = threshold(tcx);
159
160    if let Some(&info) = tcx.reachable_non_generics(LOCAL_CRATE).get(&def_id.to_def_id()) {
161        info.level.is_below_threshold(export_threshold)
162    } else {
163        false
164    }
165}
166
167fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
168    tcx.reachable_non_generics(def_id.krate).contains_key(&def_id)
169}
170
171fn exported_non_generic_symbols_provider_local<'tcx>(
172    tcx: TyCtxt<'tcx>,
173    _: LocalCrate,
174) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
175    if !tcx.sess.opts.output_types.should_codegen() && !tcx.is_sdylib_interface_build() {
176        return &[];
177    }
178
179    // FIXME: Sorting this is unnecessary since we are sorting later anyway.
180    //        Can we skip the later sorting?
181    let sorted = tcx.with_stable_hashing_context(|hcx| {
182        tcx.reachable_non_generics(LOCAL_CRATE).to_sorted(&hcx, true)
183    });
184
185    let mut symbols: Vec<_> =
186        sorted.iter().map(|&(&def_id, &info)| (ExportedSymbol::NonGeneric(def_id), info)).collect();
187
188    // Export TLS shims
189    if !tcx.sess.target.dll_tls_export {
190        symbols.extend(sorted.iter().filter_map(|&(&def_id, &info)| {
191            tcx.needs_thread_local_shim(def_id).then(|| {
192                (
193                    ExportedSymbol::ThreadLocalShim(def_id),
194                    SymbolExportInfo {
195                        level: info.level,
196                        kind: SymbolExportKind::Text,
197                        used: info.used,
198                        rustc_std_internal_symbol: info.rustc_std_internal_symbol,
199                    },
200                )
201            })
202        }))
203    }
204
205    if tcx.entry_fn(()).is_some() {
206        let exported_symbol =
207            ExportedSymbol::NoDefId(SymbolName::new(tcx, tcx.sess.target.entry_name.as_ref()));
208
209        symbols.push((
210            exported_symbol,
211            SymbolExportInfo {
212                level: SymbolExportLevel::C,
213                kind: SymbolExportKind::Text,
214                used: false,
215                rustc_std_internal_symbol: false,
216            },
217        ));
218    }
219
220    // Mark allocator shim symbols as exported only if they were generated.
221    if allocator_kind_for_codegen(tcx).is_some() {
222        for symbol_name in ALLOCATOR_METHODS
223            .iter()
224            .map(|method| mangle_internal_symbol(tcx, global_fn_name(method.name).as_str()))
225            .chain([
226                mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
227                mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
228                mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
229            ])
230        {
231            let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
232
233            symbols.push((
234                exported_symbol,
235                SymbolExportInfo {
236                    level: SymbolExportLevel::Rust,
237                    kind: SymbolExportKind::Text,
238                    used: false,
239                    rustc_std_internal_symbol: true,
240                },
241            ));
242        }
243    }
244
245    // Sort so we get a stable incr. comp. hash.
246    symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
247
248    tcx.arena.alloc_from_iter(symbols)
249}
250
251fn exported_generic_symbols_provider_local<'tcx>(
252    tcx: TyCtxt<'tcx>,
253    _: LocalCrate,
254) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
255    if !tcx.sess.opts.output_types.should_codegen() && !tcx.is_sdylib_interface_build() {
256        return &[];
257    }
258
259    let mut symbols: Vec<_> = vec![];
260
261    if tcx.local_crate_exports_generics() {
262        use rustc_hir::attrs::Linkage;
263        use rustc_middle::mir::mono::{MonoItem, Visibility};
264        use rustc_middle::ty::InstanceKind;
265
266        // Normally, we require that shared monomorphizations are not hidden,
267        // because if we want to re-use a monomorphization from a Rust dylib, it
268        // needs to be exported.
269        // However, on platforms that don't allow for Rust dylibs, having
270        // external linkage is enough for monomorphization to be linked to.
271        let need_visibility = tcx.sess.target.dynamic_linking && !tcx.sess.target.only_cdylib;
272
273        let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;
274
275        // Do not export symbols that cannot be instantiated by downstream crates.
276        let reachable_set = tcx.reachable_set(());
277        let is_local_to_current_crate = |ty: Ty<'_>| {
278            let no_refs = ty.peel_refs();
279            let root_def_id = match no_refs.kind() {
280                ty::Closure(closure, _) => *closure,
281                ty::FnDef(def_id, _) => *def_id,
282                ty::Coroutine(def_id, _) => *def_id,
283                ty::CoroutineClosure(def_id, _) => *def_id,
284                ty::CoroutineWitness(def_id, _) => *def_id,
285                _ => return false,
286            };
287            let Some(root_def_id) = root_def_id.as_local() else {
288                return false;
289            };
290
291            let is_local = !reachable_set.contains(&root_def_id);
292            is_local
293        };
294
295        let is_instantiable_downstream =
296            |did: Option<DefId>, generic_args: GenericArgsRef<'tcx>| {
297                generic_args
298                    .types()
299                    .chain(did.into_iter().map(move |did| tcx.type_of(did).skip_binder()))
300                    .all(move |arg| {
301                        arg.walk().all(|ty| {
302                            ty.as_type().map_or(true, |ty| !is_local_to_current_crate(ty))
303                        })
304                    })
305            };
306
307        // The symbols created in this loop are sorted below it
308        #[allow(rustc::potential_query_instability)]
309        for (mono_item, data) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
310            if data.linkage != Linkage::External {
311                // We can only re-use things with external linkage, otherwise
312                // we'll get a linker error
313                continue;
314            }
315
316            if need_visibility && data.visibility == Visibility::Hidden {
317                // If we potentially share things from Rust dylibs, they must
318                // not be hidden
319                continue;
320            }
321
322            if !tcx.sess.opts.share_generics() {
323                if tcx.codegen_fn_attrs(mono_item.def_id()).inline
324                    == rustc_hir::attrs::InlineAttr::Never
325                {
326                    // this is OK, we explicitly allow sharing inline(never) across crates even
327                    // without share-generics.
328                } else {
329                    continue;
330                }
331            }
332
333            // Note: These all set rustc_std_internal_symbol to false as generic functions must not
334            // be marked with this attribute and we are only handling generic functions here.
335            match *mono_item {
336                MonoItem::Fn(Instance { def: InstanceKind::Item(def), args }) => {
337                    let has_generics = args.non_erasable_generics().next().is_some();
338
339                    let should_export =
340                        has_generics && is_instantiable_downstream(Some(def), &args);
341
342                    if should_export {
343                        let symbol = ExportedSymbol::Generic(def, args);
344                        symbols.push((
345                            symbol,
346                            SymbolExportInfo {
347                                level: SymbolExportLevel::Rust,
348                                kind: SymbolExportKind::Text,
349                                used: false,
350                                rustc_std_internal_symbol: false,
351                            },
352                        ));
353                    }
354                }
355                MonoItem::Fn(Instance { def: InstanceKind::DropGlue(_, Some(ty)), args }) => {
356                    // A little sanity-check
357                    assert_eq!(args.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)));
358
359                    // 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).
360                    let should_export = match ty.kind() {
361                        ty::Adt(_, args) => is_instantiable_downstream(None, args),
362                        ty::Closure(_, args) => is_instantiable_downstream(None, args),
363                        _ => true,
364                    };
365
366                    if should_export {
367                        symbols.push((
368                            ExportedSymbol::DropGlue(ty),
369                            SymbolExportInfo {
370                                level: SymbolExportLevel::Rust,
371                                kind: SymbolExportKind::Text,
372                                used: false,
373                                rustc_std_internal_symbol: false,
374                            },
375                        ));
376                    }
377                }
378                MonoItem::Fn(Instance {
379                    def: InstanceKind::AsyncDropGlueCtorShim(_, ty),
380                    args,
381                }) => {
382                    // A little sanity-check
383                    assert_eq!(args.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)));
384                    symbols.push((
385                        ExportedSymbol::AsyncDropGlueCtorShim(ty),
386                        SymbolExportInfo {
387                            level: SymbolExportLevel::Rust,
388                            kind: SymbolExportKind::Text,
389                            used: false,
390                            rustc_std_internal_symbol: false,
391                        },
392                    ));
393                }
394                MonoItem::Fn(Instance { def: InstanceKind::AsyncDropGlue(def, ty), args: _ }) => {
395                    symbols.push((
396                        ExportedSymbol::AsyncDropGlue(def, ty),
397                        SymbolExportInfo {
398                            level: SymbolExportLevel::Rust,
399                            kind: SymbolExportKind::Text,
400                            used: false,
401                            rustc_std_internal_symbol: false,
402                        },
403                    ));
404                }
405                _ => {
406                    // Any other symbols don't qualify for sharing
407                }
408            }
409        }
410    }
411
412    // Sort so we get a stable incr. comp. hash.
413    symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
414
415    tcx.arena.alloc_from_iter(symbols)
416}
417
418fn upstream_monomorphizations_provider(
419    tcx: TyCtxt<'_>,
420    (): (),
421) -> DefIdMap<UnordMap<GenericArgsRef<'_>, CrateNum>> {
422    let cnums = tcx.crates(());
423
424    let mut instances: DefIdMap<UnordMap<_, _>> = Default::default();
425
426    let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
427    let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();
428
429    for &cnum in cnums.iter() {
430        for (exported_symbol, _) in tcx.exported_generic_symbols(cnum).iter() {
431            let (def_id, args) = match *exported_symbol {
432                ExportedSymbol::Generic(def_id, args) => (def_id, args),
433                ExportedSymbol::DropGlue(ty) => {
434                    if let Some(drop_in_place_fn_def_id) = drop_in_place_fn_def_id {
435                        (drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
436                    } else {
437                        // `drop_in_place` in place does not exist, don't try
438                        // to use it.
439                        continue;
440                    }
441                }
442                ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
443                    if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
444                        (async_drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
445                    } else {
446                        continue;
447                    }
448                }
449                ExportedSymbol::AsyncDropGlue(def_id, ty) => (def_id, tcx.mk_args(&[ty.into()])),
450                ExportedSymbol::NonGeneric(..)
451                | ExportedSymbol::ThreadLocalShim(..)
452                | ExportedSymbol::NoDefId(..) => unreachable!("{exported_symbol:?}"),
453            };
454
455            let args_map = instances.entry(def_id).or_default();
456
457            match args_map.entry(args) {
458                Occupied(mut e) => {
459                    // If there are multiple monomorphizations available,
460                    // we select one deterministically.
461                    let other_cnum = *e.get();
462                    if tcx.stable_crate_id(other_cnum) > tcx.stable_crate_id(cnum) {
463                        e.insert(cnum);
464                    }
465                }
466                Vacant(e) => {
467                    e.insert(cnum);
468                }
469            }
470        }
471    }
472
473    instances
474}
475
476fn upstream_monomorphizations_for_provider(
477    tcx: TyCtxt<'_>,
478    def_id: DefId,
479) -> Option<&UnordMap<GenericArgsRef<'_>, CrateNum>> {
480    assert!(!def_id.is_local());
481    tcx.upstream_monomorphizations(()).get(&def_id)
482}
483
484fn upstream_drop_glue_for_provider<'tcx>(
485    tcx: TyCtxt<'tcx>,
486    args: GenericArgsRef<'tcx>,
487) -> Option<CrateNum> {
488    let def_id = tcx.lang_items().drop_in_place_fn()?;
489    tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned()
490}
491
492fn upstream_async_drop_glue_for_provider<'tcx>(
493    tcx: TyCtxt<'tcx>,
494    args: GenericArgsRef<'tcx>,
495) -> Option<CrateNum> {
496    let def_id = tcx.lang_items().async_drop_in_place_fn()?;
497    tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned()
498}
499
500fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
501    !tcx.reachable_set(()).contains(&def_id)
502}
503
504pub(crate) fn provide(providers: &mut Providers) {
505    providers.reachable_non_generics = reachable_non_generics_provider;
506    providers.is_reachable_non_generic = is_reachable_non_generic_provider_local;
507    providers.exported_non_generic_symbols = exported_non_generic_symbols_provider_local;
508    providers.exported_generic_symbols = exported_generic_symbols_provider_local;
509    providers.upstream_monomorphizations = upstream_monomorphizations_provider;
510    providers.is_unreachable_local_definition = is_unreachable_local_definition_provider;
511    providers.upstream_drop_glue_for = upstream_drop_glue_for_provider;
512    providers.upstream_async_drop_glue_for = upstream_async_drop_glue_for_provider;
513    providers.wasm_import_module_map = wasm_import_module_map;
514    providers.extern_queries.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
515    providers.extern_queries.upstream_monomorphizations_for =
516        upstream_monomorphizations_for_provider;
517}
518
519fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel {
520    // We export anything that's not mangled at the "C" layer as it probably has
521    // to do with ABI concerns. We do not, however, apply such treatment to
522    // special symbols in the standard library for various plumbing between
523    // core/std/allocators/etc. For example symbols used to hook up allocation
524    // are not considered for export
525    let codegen_fn_attrs = tcx.codegen_fn_attrs(sym_def_id);
526    let is_extern = codegen_fn_attrs.contains_extern_indicator();
527    let std_internal =
528        codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
529
530    if is_extern && !std_internal {
531        let target = &tcx.sess.target.llvm_target;
532        // WebAssembly cannot export data symbols, so reduce their export level
533        if target.contains("emscripten") {
534            if let DefKind::Static { .. } = tcx.def_kind(sym_def_id) {
535                return SymbolExportLevel::Rust;
536            }
537        }
538
539        SymbolExportLevel::C
540    } else {
541        SymbolExportLevel::Rust
542    }
543}
544
545/// This is the symbol name of the given instance instantiated in a specific crate.
546pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
547    tcx: TyCtxt<'tcx>,
548    symbol: ExportedSymbol<'tcx>,
549    instantiating_crate: CrateNum,
550) -> String {
551    // If this is something instantiated in the local crate then we might
552    // already have cached the name as a query result.
553    if instantiating_crate == LOCAL_CRATE {
554        return symbol.symbol_name_for_local_instance(tcx).to_string();
555    }
556
557    // This is something instantiated in an upstream crate, so we have to use
558    // the slower (because uncached) version of computing the symbol name.
559    match symbol {
560        ExportedSymbol::NonGeneric(def_id) => {
561            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
562                tcx,
563                Instance::mono(tcx, def_id),
564                instantiating_crate,
565            )
566        }
567        ExportedSymbol::Generic(def_id, args) => {
568            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
569                tcx,
570                Instance::new_raw(def_id, args),
571                instantiating_crate,
572            )
573        }
574        ExportedSymbol::ThreadLocalShim(def_id) => {
575            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
576                tcx,
577                ty::Instance {
578                    def: ty::InstanceKind::ThreadLocalShim(def_id),
579                    args: ty::GenericArgs::empty(),
580                },
581                instantiating_crate,
582            )
583        }
584        ExportedSymbol::DropGlue(ty) => rustc_symbol_mangling::symbol_name_for_instance_in_crate(
585            tcx,
586            Instance::resolve_drop_in_place(tcx, ty),
587            instantiating_crate,
588        ),
589        ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
590            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
591                tcx,
592                Instance::resolve_async_drop_in_place(tcx, ty),
593                instantiating_crate,
594            )
595        }
596        ExportedSymbol::AsyncDropGlue(def_id, ty) => {
597            rustc_symbol_mangling::symbol_name_for_instance_in_crate(
598                tcx,
599                Instance::resolve_async_drop_in_place_poll(tcx, def_id, ty),
600                instantiating_crate,
601            )
602        }
603        ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
604    }
605}
606
607fn calling_convention_for_symbol<'tcx>(
608    tcx: TyCtxt<'tcx>,
609    symbol: ExportedSymbol<'tcx>,
610) -> (CanonAbi, &'tcx [rustc_target::callconv::ArgAbi<'tcx, Ty<'tcx>>]) {
611    let instance = match symbol {
612        ExportedSymbol::NonGeneric(def_id) | ExportedSymbol::Generic(def_id, _)
613            if tcx.is_static(def_id) =>
614        {
615            None
616        }
617        ExportedSymbol::NonGeneric(def_id) => Some(Instance::mono(tcx, def_id)),
618        ExportedSymbol::Generic(def_id, args) => Some(Instance::new_raw(def_id, args)),
619        // DropGlue always use the Rust calling convention and thus follow the target's default
620        // symbol decoration scheme.
621        ExportedSymbol::DropGlue(..) => None,
622        // AsyncDropGlueCtorShim always use the Rust calling convention and thus follow the
623        // target's default symbol decoration scheme.
624        ExportedSymbol::AsyncDropGlueCtorShim(..) => None,
625        ExportedSymbol::AsyncDropGlue(..) => None,
626        // NoDefId always follow the target's default symbol decoration scheme.
627        ExportedSymbol::NoDefId(..) => None,
628        // ThreadLocalShim always follow the target's default symbol decoration scheme.
629        ExportedSymbol::ThreadLocalShim(..) => None,
630    };
631
632    instance
633        .map(|i| {
634            tcx.fn_abi_of_instance(
635                ty::TypingEnv::fully_monomorphized().as_query_input((i, ty::List::empty())),
636            )
637            .unwrap_or_else(|_| bug!("fn_abi_of_instance({i:?}) failed"))
638        })
639        .map(|fnabi| (fnabi.conv, &fnabi.args[..]))
640        // FIXME(workingjubilee): why don't we know the convention here?
641        .unwrap_or((CanonAbi::Rust, &[]))
642}
643
644/// This is the symbol name of the given instance as seen by the linker.
645///
646/// On 32-bit Windows symbols are decorated according to their calling conventions.
647pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>(
648    tcx: TyCtxt<'tcx>,
649    symbol: ExportedSymbol<'tcx>,
650    export_kind: SymbolExportKind,
651    instantiating_crate: CrateNum,
652) -> String {
653    let mut undecorated = symbol_name_for_instance_in_crate(tcx, symbol, instantiating_crate);
654
655    // thread local will not be a function call,
656    // so it is safe to return before windows symbol decoration check.
657    if let Some(name) = maybe_emutls_symbol_name(tcx, symbol, &undecorated) {
658        return name;
659    }
660
661    let target = &tcx.sess.target;
662    if !target.is_like_windows {
663        // Mach-O has a global "_" suffix and `object` crate will handle it.
664        // ELF does not have any symbol decorations.
665        return undecorated;
666    }
667
668    let prefix = match &target.arch[..] {
669        "x86" => Some('_'),
670        "x86_64" => None,
671        // Only functions are decorated for arm64ec.
672        "arm64ec" if export_kind == SymbolExportKind::Text => Some('#'),
673        // Only x86/64 and arm64ec use symbol decorations.
674        _ => return undecorated,
675    };
676
677    let (callconv, args) = calling_convention_for_symbol(tcx, symbol);
678
679    // Decorate symbols with prefixes, suffixes and total number of bytes of arguments.
680    // Reference: https://docs.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170
681    let (prefix, suffix) = match callconv {
682        CanonAbi::X86(X86Call::Fastcall) => ("@", "@"),
683        CanonAbi::X86(X86Call::Stdcall) => ("_", "@"),
684        CanonAbi::X86(X86Call::Vectorcall) => ("", "@@"),
685        _ => {
686            if let Some(prefix) = prefix {
687                undecorated.insert(0, prefix);
688            }
689            return undecorated;
690        }
691    };
692
693    let args_in_bytes: u64 = args
694        .iter()
695        .map(|abi| abi.layout.size.bytes().next_multiple_of(target.pointer_width as u64 / 8))
696        .sum();
697    format!("{prefix}{undecorated}{suffix}{args_in_bytes}")
698}
699
700pub(crate) fn exporting_symbol_name_for_instance_in_crate<'tcx>(
701    tcx: TyCtxt<'tcx>,
702    symbol: ExportedSymbol<'tcx>,
703    cnum: CrateNum,
704) -> String {
705    let undecorated = symbol_name_for_instance_in_crate(tcx, symbol, cnum);
706    maybe_emutls_symbol_name(tcx, symbol, &undecorated).unwrap_or(undecorated)
707}
708
709/// On amdhsa, `gpu-kernel` functions have an associated metadata object with a `.kd` suffix.
710/// Add it to the symbols list for all kernel functions, so that it is exported in the linked
711/// object.
712pub(crate) fn extend_exported_symbols<'tcx>(
713    symbols: &mut Vec<(String, SymbolExportKind)>,
714    tcx: TyCtxt<'tcx>,
715    symbol: ExportedSymbol<'tcx>,
716    instantiating_crate: CrateNum,
717) {
718    let (callconv, _) = calling_convention_for_symbol(tcx, symbol);
719
720    if callconv != CanonAbi::GpuKernel || tcx.sess.target.os != "amdhsa" {
721        return;
722    }
723
724    let undecorated = symbol_name_for_instance_in_crate(tcx, symbol, instantiating_crate);
725
726    // Add the symbol for the kernel descriptor (with .kd suffix)
727    // Per https://llvm.org/docs/AMDGPUUsage.html#symbols these will always be `STT_OBJECT` so
728    // export as data.
729    symbols.push((format!("{undecorated}.kd"), SymbolExportKind::Data));
730}
731
732fn maybe_emutls_symbol_name<'tcx>(
733    tcx: TyCtxt<'tcx>,
734    symbol: ExportedSymbol<'tcx>,
735    undecorated: &str,
736) -> Option<String> {
737    if matches!(tcx.sess.tls_model(), TlsModel::Emulated)
738        && let ExportedSymbol::NonGeneric(def_id) = symbol
739        && tcx.is_thread_local_static(def_id)
740    {
741        // When using emutls, LLVM will add the `__emutls_v.` prefix to thread local symbols,
742        // and exported symbol name need to match this.
743        Some(format!("__emutls_v.{undecorated}"))
744    } else {
745        None
746    }
747}
748
749fn wasm_import_module_map(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<String> {
750    // Build up a map from DefId to a `NativeLib` structure, where
751    // `NativeLib` internally contains information about
752    // `#[link(wasm_import_module = "...")]` for example.
753    let native_libs = tcx.native_libraries(cnum);
754
755    let def_id_to_native_lib = native_libs
756        .iter()
757        .filter_map(|lib| lib.foreign_module.map(|id| (id, lib)))
758        .collect::<DefIdMap<_>>();
759
760    let mut ret = DefIdMap::default();
761    for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
762        let module = def_id_to_native_lib.get(def_id).and_then(|s| s.wasm_import_module());
763        let Some(module) = module else { continue };
764        ret.extend(lib.foreign_items.iter().map(|id| {
765            assert_eq!(id.krate, cnum);
766            (*id, module.to_string())
767        }));
768    }
769
770    ret
771}