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