1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc_data_structures::sync::Lrc;
15 use monomorphize::Instance;
16 use rustc::hir::def_id::CrateNum;
17 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
18 use rustc::middle::exported_symbols::SymbolExportLevel;
19 use rustc::session::config;
20 use rustc::ty::TyCtxt;
21 use rustc::ty::maps::Providers;
22 use rustc::util::nodemap::FxHashMap;
23 use rustc_allocator::ALLOCATOR_METHODS;
24 use rustc_back::LinkerFlavor;
27 pub type ExportedSymbols = FxHashMap<
29 Arc<Vec<(String, Option<DefId>, SymbolExportLevel)>>,
32 pub fn threshold(tcx: TyCtxt) -> SymbolExportLevel {
33 crates_export_threshold(&tcx.sess.crate_types.borrow())
36 pub fn metadata_symbol_name(tcx: TyCtxt) -> String {
37 format!("rust_metadata_{}_{}",
38 tcx.crate_name(LOCAL_CRATE),
39 tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex())
42 fn crate_export_threshold(crate_type: config::CrateType) -> SymbolExportLevel {
44 config::CrateTypeExecutable |
45 config::CrateTypeStaticlib |
46 config::CrateTypeProcMacro |
47 config::CrateTypeCdylib => SymbolExportLevel::C,
48 config::CrateTypeRlib |
49 config::CrateTypeDylib => SymbolExportLevel::Rust,
53 pub fn crates_export_threshold(crate_types: &[config::CrateType])
54 -> SymbolExportLevel {
55 if crate_types.iter().any(|&crate_type| {
56 crate_export_threshold(crate_type) == SymbolExportLevel::Rust
58 SymbolExportLevel::Rust
64 pub fn provide(providers: &mut Providers) {
65 providers.exported_symbol_ids = |tcx, cnum| {
66 let export_threshold = threshold(tcx);
67 Lrc::new(tcx.exported_symbols(cnum)
69 .filter_map(|&(_, id, level)| {
71 if level.is_below_threshold(export_threshold) {
81 providers.is_exported_symbol = |tcx, id| {
82 tcx.exported_symbol_ids(id.krate).contains(&id)
85 providers.exported_symbols = |tcx, cnum| {
86 assert_eq!(cnum, LOCAL_CRATE);
87 let local_exported_symbols = base::find_exported_symbols(tcx);
89 let mut local_crate: Vec<_> = local_exported_symbols
92 tcx.hir.local_def_id(node_id)
95 let name = tcx.symbol_name(Instance::mono(tcx, def_id));
96 let export_level = export_level(tcx, def_id);
97 debug!("EXPORTED SYMBOL (local): {} ({:?})", name, export_level);
98 (str::to_owned(&name), Some(def_id), export_level)
102 if let Some(_) = *tcx.sess.entry_fn.borrow() {
103 local_crate.push(("main".to_string(),
105 SymbolExportLevel::C));
108 if tcx.sess.allocator_kind.get().is_some() {
109 for method in ALLOCATOR_METHODS {
110 local_crate.push((format!("__rust_{}", method.name),
112 SymbolExportLevel::Rust));
116 if let Some(id) = tcx.sess.derive_registrar_fn.get() {
117 let def_id = tcx.hir.local_def_id(id);
118 let disambiguator = tcx.sess.local_crate_disambiguator();
119 let registrar = tcx.sess.generate_derive_registrar_symbol(disambiguator);
120 local_crate.push((registrar, Some(def_id), SymbolExportLevel::C));
123 if tcx.sess.crate_types.borrow().contains(&config::CrateTypeDylib) {
124 local_crate.push((metadata_symbol_name(tcx),
126 SymbolExportLevel::Rust));
129 // Sort so we get a stable incr. comp. hash.
130 local_crate.sort_unstable_by(|&(ref name1, ..), &(ref name2, ..)| {
134 Arc::new(local_crate)
137 providers.symbol_export_level = export_level;
140 pub fn provide_extern(providers: &mut Providers) {
141 providers.exported_symbols = |tcx, cnum| {
142 // If this crate is a plugin and/or a custom derive crate, then
143 // we're not even going to link those in so we skip those crates.
144 if tcx.plugin_registrar_fn(cnum).is_some() ||
145 tcx.derive_registrar_fn(cnum).is_some() {
146 return Arc::new(Vec::new())
149 // Check to see if this crate is a "special runtime crate". These
150 // crates, implementation details of the standard library, typically
151 // have a bunch of `pub extern` and `#[no_mangle]` functions as the
152 // ABI between them. We don't want their symbols to have a `C`
153 // export level, however, as they're just implementation details.
154 // Down below we'll hardwire all of the symbols to the `Rust` export
156 let special_runtime_crate =
157 tcx.is_panic_runtime(cnum) || tcx.is_compiler_builtins(cnum);
159 // Dealing with compiler-builtins and wasm right now is super janky.
160 // There's no linker! As a result we need all of the compiler-builtins
161 // exported symbols to make their way through all the way to the end of
162 // compilation. We want to make sure that LLVM doesn't remove them as
163 // well because we may or may not need them in the final output
164 // artifact. For now just force them to always get exported at the C
165 // layer, and we'll worry about gc'ing them later.
166 let compiler_builtins_and_binaryen =
167 tcx.is_compiler_builtins(cnum) &&
168 tcx.sess.linker_flavor() == LinkerFlavor::Binaryen;
170 let mut crate_exports: Vec<_> = tcx
171 .exported_symbol_ids(cnum)
174 let name = tcx.symbol_name(Instance::mono(tcx, def_id));
175 let export_level = if compiler_builtins_and_binaryen &&
176 tcx.contains_extern_indicator(def_id) {
178 } else if special_runtime_crate {
179 // We can probably do better here by just ensuring that
180 // it has hidden visibility rather than public
181 // visibility, as this is primarily here to ensure it's
182 // not stripped during LTO.
184 // In general though we won't link right if these
185 // symbols are stripped, and LTO currently strips them.
186 if &*name == "rust_eh_personality" ||
187 &*name == "rust_eh_register_frames" ||
188 &*name == "rust_eh_unregister_frames" {
191 SymbolExportLevel::Rust
194 export_level(tcx, def_id)
196 debug!("EXPORTED SYMBOL (re-export): {} ({:?})", name, export_level);
197 (str::to_owned(&name), Some(def_id), export_level)
201 // Sort so we get a stable incr. comp. hash.
202 crate_exports.sort_unstable_by(|&(ref name1, ..), &(ref name2, ..)| {
206 Arc::new(crate_exports)
208 providers.symbol_export_level = export_level;
211 fn export_level(tcx: TyCtxt, sym_def_id: DefId) -> SymbolExportLevel {
212 // We export anything that's not mangled at the "C" layer as it probably has
213 // to do with ABI concerns. We do not, however, apply such treatment to
214 // special symbols in the standard library for various plumbing between
215 // core/std/allocators/etc. For example symbols used to hook up allocation
216 // are not considered for export
217 let is_extern = tcx.contains_extern_indicator(sym_def_id);
218 let std_internal = attr::contains_name(&tcx.get_attrs(sym_def_id),
219 "rustc_std_internal_symbol");
220 if is_extern && !std_internal {
223 SymbolExportLevel::Rust