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::back::symbol_export;
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    // Sort so we get a stable incr. comp. hash.
221    symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
222
223    tcx.arena.alloc_from_iter(symbols)
224}
225
226fn exported_generic_symbols_provider_local<'tcx>(
227    tcx: TyCtxt<'tcx>,
228    _: LocalCrate,
229) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
230    if !tcx.sess.opts.output_types.should_codegen() && !tcx.is_sdylib_interface_build() {
231        return &[];
232    }
233
234    let mut symbols: Vec<_> = vec![];
235
236    if tcx.local_crate_exports_generics() {
237        use rustc_hir::attrs::Linkage;
238        use rustc_middle::mir::mono::{MonoItem, Visibility};
239        use rustc_middle::ty::InstanceKind;
240
241        // Normally, we require that shared monomorphizations are not hidden,
242        // because if we want to re-use a monomorphization from a Rust dylib, it
243        // needs to be exported.
244        // However, on platforms that don't allow for Rust dylibs, having
245        // external linkage is enough for monomorphization to be linked to.
246        let need_visibility = tcx.sess.target.dynamic_linking && !tcx.sess.target.only_cdylib;
247
248        let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;
249
250        // Do not export symbols that cannot be instantiated by downstream crates.
251        let reachable_set = tcx.reachable_set(());
252        let is_local_to_current_crate = |ty: Ty<'_>| {
253            let no_refs = ty.peel_refs();
254            let root_def_id = match no_refs.kind() {
255                ty::Closure(closure, _) => *closure,
256                ty::FnDef(def_id, _) => *def_id,
257                ty::Coroutine(def_id, _) => *def_id,
258                ty::CoroutineClosure(def_id, _) => *def_id,
259                ty::CoroutineWitness(def_id, _) => *def_id,
260                _ => return false,
261            };
262            let Some(root_def_id) = root_def_id.as_local() else {
263                return false;
264            };
265
266            let is_local = !reachable_set.contains(&root_def_id);
267            is_local
268        };
269
270        let is_instantiable_downstream =
271            |did: Option<DefId>, generic_args: GenericArgsRef<'tcx>| {
272                generic_args
273                    .types()
274                    .chain(did.into_iter().map(move |did| tcx.type_of(did).skip_binder()))
275                    .all(move |arg| {
276                        arg.walk().all(|ty| {
277                            ty.as_type().map_or(true, |ty| !is_local_to_current_crate(ty))
278                        })
279                    })
280            };
281
282        // The symbols created in this loop are sorted below it
283        #[allow(rustc::potential_query_instability)]
284        for (mono_item, data) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
285            if data.linkage != Linkage::External {
286                // We can only re-use things with external linkage, otherwise
287                // we'll get a linker error
288                continue;
289            }
290
291            if need_visibility && data.visibility == Visibility::Hidden {
292                // If we potentially share things from Rust dylibs, they must
293                // not be hidden
294                continue;
295            }
296
297            if !tcx.sess.opts.share_generics() {
298                if tcx.codegen_fn_attrs(mono_item.def_id()).inline
299                    == rustc_hir::attrs::InlineAttr::Never
300                {
301                    // this is OK, we explicitly allow sharing inline(never) across crates even
302                    // without share-generics.
303                } else {
304                    continue;
305                }
306            }
307
308            // Note: These all set rustc_std_internal_symbol to false as generic functions must not
309            // be marked with this attribute and we are only handling generic functions here.
310            match *mono_item {
311                MonoItem::Fn(Instance { def: InstanceKind::Item(def), args }) => {
312                    let has_generics = args.non_erasable_generics().next().is_some();
313
314                    let should_export =
315                        has_generics && is_instantiable_downstream(Some(def), &args);
316
317                    if should_export {
318                        let symbol = ExportedSymbol::Generic(def, args);
319                        symbols.push((
320                            symbol,
321                            SymbolExportInfo {
322                                level: SymbolExportLevel::Rust,
323                                kind: SymbolExportKind::Text,
324                                used: false,
325                                rustc_std_internal_symbol: false,
326                            },
327                        ));
328                    }
329                }
330                MonoItem::Fn(Instance { def: InstanceKind::DropGlue(_, Some(ty)), args }) => {
331                    // A little sanity-check
332                    assert_eq!(args.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)));
333
334                    // 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).
335                    let should_export = match ty.kind() {
336                        ty::Adt(_, args) => is_instantiable_downstream(None, args),
337                        ty::Closure(_, args) => is_instantiable_downstream(None, args),
338                        _ => true,
339                    };
340
341                    if should_export {
342                        symbols.push((
343                            ExportedSymbol::DropGlue(ty),
344                            SymbolExportInfo {
345                                level: SymbolExportLevel::Rust,
346                                kind: SymbolExportKind::Text,
347                                used: false,
348                                rustc_std_internal_symbol: false,
349                            },
350                        ));
351                    }
352                }
353                MonoItem::Fn(Instance {
354                    def: InstanceKind::AsyncDropGlueCtorShim(_, ty),
355                    args,
356                }) => {
357                    // A little sanity-check
358                    assert_eq!(args.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)));
359                    symbols.push((
360                        ExportedSymbol::AsyncDropGlueCtorShim(ty),
361                        SymbolExportInfo {
362                            level: SymbolExportLevel::Rust,
363                            kind: SymbolExportKind::Text,
364                            used: false,
365                            rustc_std_internal_symbol: false,
366                        },
367                    ));
368                }
369                MonoItem::Fn(Instance { def: InstanceKind::AsyncDropGlue(def, ty), args: _ }) => {
370                    symbols.push((
371                        ExportedSymbol::AsyncDropGlue(def, ty),
372                        SymbolExportInfo {
373                            level: SymbolExportLevel::Rust,
374                            kind: SymbolExportKind::Text,
375                            used: false,
376                            rustc_std_internal_symbol: false,
377                        },
378                    ));
379                }
380                _ => {
381                    // Any other symbols don't qualify for sharing
382                }
383            }
384        }
385    }
386
387    // Sort so we get a stable incr. comp. hash.
388    symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
389
390    tcx.arena.alloc_from_iter(symbols)
391}
392
393fn upstream_monomorphizations_provider(
394    tcx: TyCtxt<'_>,
395    (): (),
396) -> DefIdMap<UnordMap<GenericArgsRef<'_>, CrateNum>> {
397    let cnums = tcx.crates(());
398
399    let mut instances: DefIdMap<UnordMap<_, _>> = Default::default();
400
401    let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
402    let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();
403
404    for &cnum in cnums.iter() {
405        for (exported_symbol, _) in tcx.exported_generic_symbols(cnum).iter() {
406            let (def_id, args) = match *exported_symbol {
407                ExportedSymbol::Generic(def_id, args) => (def_id, args),
408                ExportedSymbol::DropGlue(ty) => {
409                    if let Some(drop_in_place_fn_def_id) = drop_in_place_fn_def_id {
410                        (drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
411                    } else {
412                        // `drop_in_place` in place does not exist, don't try
413                        // to use it.
414                        continue;
415                    }
416                }
417                ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
418                    if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
419                        (async_drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
420                    } else {
421                        continue;
422                    }
423                }
424                ExportedSymbol::AsyncDropGlue(def_id, ty) => (def_id, tcx.mk_args(&[ty.into()])),
425                ExportedSymbol::NonGeneric(..)
426                | ExportedSymbol::ThreadLocalShim(..)
427                | ExportedSymbol::NoDefId(..) => unreachable!("{exported_symbol:?}"),
428            };
429
430            let args_map = instances.entry(def_id).or_default();
431
432            match args_map.entry(args) {
433                Occupied(mut e) => {
434                    // If there are multiple monomorphizations available,
435                    // we select one deterministically.
436                    let other_cnum = *e.get();
437                    if tcx.stable_crate_id(other_cnum) > tcx.stable_crate_id(cnum) {
438                        e.insert(cnum);
439                    }
440                }
441                Vacant(e) => {
442                    e.insert(cnum);
443                }
444            }
445        }
446    }
447
448    instances
449}
450
451fn upstream_monomorphizations_for_provider(
452    tcx: TyCtxt<'_>,
453    def_id: DefId,
454) -> Option<&UnordMap<GenericArgsRef<'_>, CrateNum>> {
455    assert!(!def_id.is_local());
456    tcx.upstream_monomorphizations(()).get(&def_id)
457}
458
459fn upstream_drop_glue_for_provider<'tcx>(
460    tcx: TyCtxt<'tcx>,
461    args: GenericArgsRef<'tcx>,
462) -> Option<CrateNum> {
463    let def_id = tcx.lang_items().drop_in_place_fn()?;
464    tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned()
465}
466
467fn upstream_async_drop_glue_for_provider<'tcx>(
468    tcx: TyCtxt<'tcx>,
469    args: GenericArgsRef<'tcx>,
470) -> Option<CrateNum> {
471    let def_id = tcx.lang_items().async_drop_in_place_fn()?;
472    tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned()
473}
474
475fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
476    !tcx.reachable_set(()).contains(&def_id)
477}
478
479pub(crate) fn provide(providers: &mut Providers) {
480    providers.reachable_non_generics = reachable_non_generics_provider;
481    providers.is_reachable_non_generic = is_reachable_non_generic_provider_local;
482    providers.exported_non_generic_symbols = exported_non_generic_symbols_provider_local;
483    providers.exported_generic_symbols = exported_generic_symbols_provider_local;
484    providers.upstream_monomorphizations = upstream_monomorphizations_provider;
485    providers.is_unreachable_local_definition = is_unreachable_local_definition_provider;
486    providers.upstream_drop_glue_for = upstream_drop_glue_for_provider;
487    providers.upstream_async_drop_glue_for = upstream_async_drop_glue_for_provider;
488    providers.wasm_import_module_map = wasm_import_module_map;
489    providers.extern_queries.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
490    providers.extern_queries.upstream_monomorphizations_for =
491        upstream_monomorphizations_for_provider;
492}
493
494pub(crate) fn allocator_shim_symbols(
495    tcx: TyCtxt<'_>,
496) -> impl Iterator<Item = (String, SymbolExportKind)> {
497    ALLOCATOR_METHODS
498        .iter()
499        .map(move |method| mangle_internal_symbol(tcx, global_fn_name(method.name).as_str()))
500        .chain([
501            mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
502            mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
503            mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
504        ])
505        .map(move |symbol_name| {
506            let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
507
508            (
509                symbol_export::exporting_symbol_name_for_instance_in_crate(
510                    tcx,
511                    exported_symbol,
512                    LOCAL_CRATE,
513                ),
514                SymbolExportKind::Text,
515            )
516        })
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}