]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/back/symbol_export.rs
Auto merge of #74576 - myfreeweb:freebsd-sanitizers, r=oli-obk
[rust.git] / src / librustc_codegen_ssa / 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::fingerprint::Fingerprint;
5 use rustc_data_structures::fx::FxHashMap;
6 use rustc_hir as hir;
7 use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
8 use rustc_hir::Node;
9 use rustc_index::vec::IndexVec;
10 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
11 use rustc_middle::middle::exported_symbols::{
12     metadata_symbol_name, ExportedSymbol, SymbolExportLevel,
13 };
14 use rustc_middle::ty::query::Providers;
15 use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
16 use rustc_middle::ty::Instance;
17 use rustc_middle::ty::{SymbolName, TyCtxt};
18 use rustc_session::config::{CrateType, SanitizerSet};
19
20 pub fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
21     crates_export_threshold(&tcx.sess.crate_types())
22 }
23
24 fn crate_export_threshold(crate_type: CrateType) -> SymbolExportLevel {
25     match crate_type {
26         CrateType::Executable | CrateType::Staticlib | CrateType::ProcMacro | CrateType::Cdylib => {
27             SymbolExportLevel::C
28         }
29         CrateType::Rlib | CrateType::Dylib => SymbolExportLevel::Rust,
30     }
31 }
32
33 pub fn crates_export_threshold(crate_types: &[CrateType]) -> SymbolExportLevel {
34     if crate_types
35         .iter()
36         .any(|&crate_type| crate_export_threshold(crate_type) == SymbolExportLevel::Rust)
37     {
38         SymbolExportLevel::Rust
39     } else {
40         SymbolExportLevel::C
41     }
42 }
43
44 fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<SymbolExportLevel> {
45     assert_eq!(cnum, LOCAL_CRATE);
46
47     if !tcx.sess.opts.output_types.should_codegen() {
48         return Default::default();
49     }
50
51     // Check to see if this crate is a "special runtime crate". These
52     // crates, implementation details of the standard library, typically
53     // have a bunch of `pub extern` and `#[no_mangle]` functions as the
54     // ABI between them. We don't want their symbols to have a `C`
55     // export level, however, as they're just implementation details.
56     // Down below we'll hardwire all of the symbols to the `Rust` export
57     // level instead.
58     let special_runtime_crate =
59         tcx.is_panic_runtime(LOCAL_CRATE) || tcx.is_compiler_builtins(LOCAL_CRATE);
60
61     let mut reachable_non_generics: DefIdMap<_> = tcx
62         .reachable_set(LOCAL_CRATE)
63         .iter()
64         .filter_map(|&hir_id| {
65             // We want to ignore some FFI functions that are not exposed from
66             // this crate. Reachable FFI functions can be lumped into two
67             // categories:
68             //
69             // 1. Those that are included statically via a static library
70             // 2. Those included otherwise (e.g., dynamically or via a framework)
71             //
72             // Although our LLVM module is not literally emitting code for the
73             // statically included symbols, it's an export of our library which
74             // needs to be passed on to the linker and encoded in the metadata.
75             //
76             // As a result, if this id is an FFI item (foreign item) then we only
77             // let it through if it's included statically.
78             match tcx.hir().get(hir_id) {
79                 Node::ForeignItem(..) => {
80                     let def_id = tcx.hir().local_def_id(hir_id);
81                     tcx.is_statically_included_foreign_item(def_id).then_some(def_id)
82                 }
83
84                 // Only consider nodes that actually have exported symbols.
85                 Node::Item(&hir::Item {
86                     kind: hir::ItemKind::Static(..) | hir::ItemKind::Fn(..),
87                     ..
88                 })
89                 | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) => {
90                     let def_id = tcx.hir().local_def_id(hir_id);
91                     let generics = tcx.generics_of(def_id);
92                     if !generics.requires_monomorphization(tcx)
93                         // Functions marked with #[inline] are codegened with "internal"
94                         // linkage and are not exported unless marked with an extern
95                         // inidicator
96                         && (!Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
97                             || tcx.codegen_fn_attrs(def_id.to_def_id()).contains_extern_indicator())
98                     {
99                         Some(def_id)
100                     } else {
101                         None
102                     }
103                 }
104
105                 _ => None,
106             }
107         })
108         .map(|def_id| {
109             let export_level = if special_runtime_crate {
110                 let name = tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())).name;
111                 // We can probably do better here by just ensuring that
112                 // it has hidden visibility rather than public
113                 // visibility, as this is primarily here to ensure it's
114                 // not stripped during LTO.
115                 //
116                 // In general though we won't link right if these
117                 // symbols are stripped, and LTO currently strips them.
118                 match name {
119                     "rust_eh_personality"
120                     | "rust_eh_register_frames"
121                     | "rust_eh_unregister_frames" =>
122                         SymbolExportLevel::C,
123                     _ => SymbolExportLevel::Rust,
124                 }
125             } else {
126                 symbol_export_level(tcx, def_id.to_def_id())
127             };
128             debug!(
129                 "EXPORTED SYMBOL (local): {} ({:?})",
130                 tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())),
131                 export_level
132             );
133             (def_id.to_def_id(), export_level)
134         })
135         .collect();
136
137     if let Some(id) = tcx.proc_macro_decls_static(LOCAL_CRATE) {
138         reachable_non_generics.insert(id, SymbolExportLevel::C);
139     }
140
141     if let Some(id) = tcx.plugin_registrar_fn(LOCAL_CRATE) {
142         reachable_non_generics.insert(id, SymbolExportLevel::C);
143     }
144
145     reachable_non_generics
146 }
147
148 fn is_reachable_non_generic_provider_local(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
149     let export_threshold = threshold(tcx);
150
151     if let Some(&level) = tcx.reachable_non_generics(def_id.krate).get(&def_id) {
152         level.is_below_threshold(export_threshold)
153     } else {
154         false
155     }
156 }
157
158 fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
159     tcx.reachable_non_generics(def_id.krate).contains_key(&def_id)
160 }
161
162 fn exported_symbols_provider_local(
163     tcx: TyCtxt<'tcx>,
164     cnum: CrateNum,
165 ) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
166     assert_eq!(cnum, LOCAL_CRATE);
167
168     if !tcx.sess.opts.output_types.should_codegen() {
169         return &[];
170     }
171
172     let mut symbols: Vec<_> = tcx
173         .reachable_non_generics(LOCAL_CRATE)
174         .iter()
175         .map(|(&def_id, &level)| (ExportedSymbol::NonGeneric(def_id), level))
176         .collect();
177
178     if tcx.entry_fn(LOCAL_CRATE).is_some() {
179         let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, "main"));
180
181         symbols.push((exported_symbol, SymbolExportLevel::C));
182     }
183
184     if tcx.allocator_kind().is_some() {
185         for method in ALLOCATOR_METHODS {
186             let symbol_name = format!("__rust_{}", method.name);
187             let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
188
189             symbols.push((exported_symbol, SymbolExportLevel::Rust));
190         }
191     }
192
193     if tcx.sess.opts.debugging_opts.instrument_coverage
194         || tcx.sess.opts.cg.profile_generate.enabled()
195     {
196         // These are weak symbols that point to the profile version and the
197         // profile name, which need to be treated as exported so LTO doesn't nix
198         // them.
199         const PROFILER_WEAK_SYMBOLS: [&str; 2] =
200             ["__llvm_profile_raw_version", "__llvm_profile_filename"];
201
202         symbols.extend(PROFILER_WEAK_SYMBOLS.iter().map(|sym| {
203             let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
204             (exported_symbol, SymbolExportLevel::C)
205         }));
206     }
207
208     if tcx.sess.opts.debugging_opts.sanitizer.contains(SanitizerSet::MEMORY) {
209         // Similar to profiling, preserve weak msan symbol during LTO.
210         const MSAN_WEAK_SYMBOLS: [&str; 2] = ["__msan_track_origins", "__msan_keep_going"];
211
212         symbols.extend(MSAN_WEAK_SYMBOLS.iter().map(|sym| {
213             let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
214             (exported_symbol, SymbolExportLevel::C)
215         }));
216     }
217
218     if tcx.sess.crate_types().contains(&CrateType::Dylib) {
219         let symbol_name = metadata_symbol_name(tcx);
220         let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
221
222         symbols.push((exported_symbol, SymbolExportLevel::Rust));
223     }
224
225     if tcx.sess.opts.share_generics() && tcx.local_crate_exports_generics() {
226         use rustc_middle::mir::mono::{Linkage, MonoItem, Visibility};
227         use rustc_middle::ty::InstanceDef;
228
229         // Normally, we require that shared monomorphizations are not hidden,
230         // because if we want to re-use a monomorphization from a Rust dylib, it
231         // needs to be exported.
232         // However, on platforms that don't allow for Rust dylibs, having
233         // external linkage is enough for monomorphization to be linked to.
234         let need_visibility = tcx.sess.target.target.options.dynamic_linking
235             && !tcx.sess.target.target.options.only_cdylib;
236
237         let (_, cgus) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
238
239         for (mono_item, &(linkage, visibility)) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
240             if linkage != Linkage::External {
241                 // We can only re-use things with external linkage, otherwise
242                 // we'll get a linker error
243                 continue;
244             }
245
246             if need_visibility && visibility == Visibility::Hidden {
247                 // If we potentially share things from Rust dylibs, they must
248                 // not be hidden
249                 continue;
250             }
251
252             match *mono_item {
253                 MonoItem::Fn(Instance { def: InstanceDef::Item(def), substs }) => {
254                     if substs.non_erasable_generics().next().is_some() {
255                         let symbol = ExportedSymbol::Generic(def.did, substs);
256                         symbols.push((symbol, SymbolExportLevel::Rust));
257                     }
258                 }
259                 MonoItem::Fn(Instance { def: InstanceDef::DropGlue(_, Some(ty)), substs }) => {
260                     // A little sanity-check
261                     debug_assert_eq!(
262                         substs.non_erasable_generics().next(),
263                         Some(GenericArgKind::Type(ty))
264                     );
265                     symbols.push((ExportedSymbol::DropGlue(ty), SymbolExportLevel::Rust));
266                 }
267                 _ => {
268                     // Any other symbols don't qualify for sharing
269                 }
270             }
271         }
272     }
273
274     // Sort so we get a stable incr. comp. hash.
275     symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
276
277     tcx.arena.alloc_from_iter(symbols)
278 }
279
280 fn upstream_monomorphizations_provider(
281     tcx: TyCtxt<'_>,
282     cnum: CrateNum,
283 ) -> DefIdMap<FxHashMap<SubstsRef<'_>, CrateNum>> {
284     debug_assert!(cnum == LOCAL_CRATE);
285
286     let cnums = tcx.all_crate_nums(LOCAL_CRATE);
287
288     let mut instances: DefIdMap<FxHashMap<_, _>> = Default::default();
289
290     let cnum_stable_ids: IndexVec<CrateNum, Fingerprint> = {
291         let mut cnum_stable_ids = IndexVec::from_elem_n(Fingerprint::ZERO, cnums.len() + 1);
292
293         for &cnum in cnums.iter() {
294             cnum_stable_ids[cnum] =
295                 tcx.def_path_hash(DefId { krate: cnum, index: CRATE_DEF_INDEX }).0;
296         }
297
298         cnum_stable_ids
299     };
300
301     let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
302
303     for &cnum in cnums.iter() {
304         for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
305             let (def_id, substs) = match *exported_symbol {
306                 ExportedSymbol::Generic(def_id, substs) => (def_id, substs),
307                 ExportedSymbol::DropGlue(ty) => {
308                     if let Some(drop_in_place_fn_def_id) = drop_in_place_fn_def_id {
309                         (drop_in_place_fn_def_id, tcx.intern_substs(&[ty.into()]))
310                     } else {
311                         // `drop_in_place` in place does not exist, don't try
312                         // to use it.
313                         continue;
314                     }
315                 }
316                 ExportedSymbol::NonGeneric(..) | ExportedSymbol::NoDefId(..) => {
317                     // These are no monomorphizations
318                     continue;
319                 }
320             };
321
322             let substs_map = instances.entry(def_id).or_default();
323
324             match substs_map.entry(substs) {
325                 Occupied(mut e) => {
326                     // If there are multiple monomorphizations available,
327                     // we select one deterministically.
328                     let other_cnum = *e.get();
329                     if cnum_stable_ids[other_cnum] > cnum_stable_ids[cnum] {
330                         e.insert(cnum);
331                     }
332                 }
333                 Vacant(e) => {
334                     e.insert(cnum);
335                 }
336             }
337         }
338     }
339
340     instances
341 }
342
343 fn upstream_monomorphizations_for_provider(
344     tcx: TyCtxt<'_>,
345     def_id: DefId,
346 ) -> Option<&FxHashMap<SubstsRef<'_>, CrateNum>> {
347     debug_assert!(!def_id.is_local());
348     tcx.upstream_monomorphizations(LOCAL_CRATE).get(&def_id)
349 }
350
351 fn upstream_drop_glue_for_provider<'tcx>(
352     tcx: TyCtxt<'tcx>,
353     substs: SubstsRef<'tcx>,
354 ) -> Option<CrateNum> {
355     if let Some(def_id) = tcx.lang_items().drop_in_place_fn() {
356         tcx.upstream_monomorphizations_for(def_id).and_then(|monos| monos.get(&substs).cloned())
357     } else {
358         None
359     }
360 }
361
362 fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
363     if let Some(def_id) = def_id.as_local() {
364         !tcx.reachable_set(LOCAL_CRATE).contains(&tcx.hir().local_def_id_to_hir_id(def_id))
365     } else {
366         bug!("is_unreachable_local_definition called with non-local DefId: {:?}", def_id)
367     }
368 }
369
370 pub fn provide(providers: &mut Providers) {
371     providers.reachable_non_generics = reachable_non_generics_provider;
372     providers.is_reachable_non_generic = is_reachable_non_generic_provider_local;
373     providers.exported_symbols = exported_symbols_provider_local;
374     providers.upstream_monomorphizations = upstream_monomorphizations_provider;
375     providers.is_unreachable_local_definition = is_unreachable_local_definition_provider;
376     providers.upstream_drop_glue_for = upstream_drop_glue_for_provider;
377 }
378
379 pub fn provide_extern(providers: &mut Providers) {
380     providers.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
381     providers.upstream_monomorphizations_for = upstream_monomorphizations_for_provider;
382 }
383
384 fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel {
385     // We export anything that's not mangled at the "C" layer as it probably has
386     // to do with ABI concerns. We do not, however, apply such treatment to
387     // special symbols in the standard library for various plumbing between
388     // core/std/allocators/etc. For example symbols used to hook up allocation
389     // are not considered for export
390     let codegen_fn_attrs = tcx.codegen_fn_attrs(sym_def_id);
391     let is_extern = codegen_fn_attrs.contains_extern_indicator();
392     let std_internal =
393         codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
394
395     if is_extern && !std_internal {
396         let target = &tcx.sess.target.target.llvm_target;
397         // WebAssembly cannot export data symbols, so reduce their export level
398         if target.contains("emscripten") {
399             if let Some(Node::Item(&hir::Item { kind: hir::ItemKind::Static(..), .. })) =
400                 tcx.hir().get_if_local(sym_def_id)
401             {
402                 return SymbolExportLevel::Rust;
403             }
404         }
405
406         SymbolExportLevel::C
407     } else {
408         SymbolExportLevel::Rust
409     }
410 }
411
412 /// This is the symbol name of the given instance instantiated in a specific crate.
413 pub fn symbol_name_for_instance_in_crate<'tcx>(
414     tcx: TyCtxt<'tcx>,
415     symbol: ExportedSymbol<'tcx>,
416     instantiating_crate: CrateNum,
417 ) -> String {
418     // If this is something instantiated in the local crate then we might
419     // already have cached the name as a query result.
420     if instantiating_crate == LOCAL_CRATE {
421         return symbol.symbol_name_for_local_instance(tcx).to_string();
422     }
423
424     // This is something instantiated in an upstream crate, so we have to use
425     // the slower (because uncached) version of computing the symbol name.
426     match symbol {
427         ExportedSymbol::NonGeneric(def_id) => {
428             rustc_symbol_mangling::symbol_name_for_instance_in_crate(
429                 tcx,
430                 Instance::mono(tcx, def_id),
431                 instantiating_crate,
432             )
433         }
434         ExportedSymbol::Generic(def_id, substs) => {
435             rustc_symbol_mangling::symbol_name_for_instance_in_crate(
436                 tcx,
437                 Instance::new(def_id, substs),
438                 instantiating_crate,
439             )
440         }
441         ExportedSymbol::DropGlue(ty) => rustc_symbol_mangling::symbol_name_for_instance_in_crate(
442             tcx,
443             Instance::resolve_drop_in_place(tcx, ty),
444             instantiating_crate,
445         ),
446         ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
447     }
448 }