rustc_codegen_ssa/back/
symbol_export.rs

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