rustc_codegen_ssa/back/
symbol_export.rs

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