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