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.
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;
25 pub type ExportedSymbols = FxHashMap<
27 Arc<Vec<(String, Option<DefId>, SymbolExportLevel)>>,
30 pub fn threshold(tcx: TyCtxt) -> SymbolExportLevel {
31 crates_export_threshold(&tcx.sess.crate_types.borrow())
34 pub fn metadata_symbol_name(tcx: TyCtxt) -> String {
35 format!("rust_metadata_{}_{}",
36 tcx.crate_name(LOCAL_CRATE),
37 tcx.crate_disambiguator(LOCAL_CRATE).to_hex())
40 fn crate_export_threshold(crate_type: config::CrateType) -> SymbolExportLevel {
42 config::CrateTypeExecutable |
43 config::CrateTypeStaticlib |
44 config::CrateTypeProcMacro |
45 config::CrateTypeCdylib => SymbolExportLevel::C,
46 config::CrateTypeRlib |
47 config::CrateTypeDylib => SymbolExportLevel::Rust,
51 pub fn crates_export_threshold(crate_types: &[config::CrateType])
52 -> SymbolExportLevel {
53 if crate_types.iter().any(|&crate_type| {
54 crate_export_threshold(crate_type) == SymbolExportLevel::Rust
56 SymbolExportLevel::Rust
62 pub fn provide_local(providers: &mut Providers) {
63 providers.exported_symbol_ids = |tcx, cnum| {
64 let export_threshold = threshold(tcx);
65 Rc::new(tcx.exported_symbols(cnum)
67 .filter_map(|&(_, id, level)| {
69 if level.is_below_threshold(export_threshold) {
79 providers.is_exported_symbol = |tcx, id| {
80 tcx.exported_symbol_ids(id.krate).contains(&id)
83 providers.exported_symbols = |tcx, cnum| {
84 assert_eq!(cnum, LOCAL_CRATE);
85 let local_exported_symbols = base::find_exported_symbols(tcx);
87 let mut local_crate: Vec<_> = local_exported_symbols
90 tcx.hir.local_def_id(node_id)
93 let name = tcx.symbol_name(Instance::mono(tcx, def_id));
94 let export_level = export_level(tcx, def_id);
95 debug!("EXPORTED SYMBOL (local): {} ({:?})", name, export_level);
96 (str::to_owned(&name), Some(def_id), export_level)
100 if let Some(_) = *tcx.sess.entry_fn.borrow() {
101 local_crate.push(("main".to_string(),
103 SymbolExportLevel::C));
106 if tcx.sess.allocator_kind.get().is_some() {
107 for method in ALLOCATOR_METHODS {
108 local_crate.push((format!("__rust_{}", method.name),
110 SymbolExportLevel::Rust));
114 if let Some(id) = tcx.sess.derive_registrar_fn.get() {
115 let def_id = tcx.hir.local_def_id(id);
116 let idx = def_id.index;
117 let disambiguator = tcx.sess.local_crate_disambiguator();
118 let registrar = tcx.sess.generate_derive_registrar_symbol(disambiguator, idx);
119 local_crate.push((registrar, Some(def_id), SymbolExportLevel::C));
122 if tcx.sess.crate_types.borrow().contains(&config::CrateTypeDylib) {
123 local_crate.push((metadata_symbol_name(tcx),
125 SymbolExportLevel::Rust));
127 Arc::new(local_crate)
131 pub fn provide_extern(providers: &mut Providers) {
132 providers.exported_symbols = |tcx, cnum| {
133 // If this crate is a plugin and/or a custom derive crate, then
134 // we're not even going to link those in so we skip those crates.
135 if tcx.plugin_registrar_fn(cnum).is_some() ||
136 tcx.derive_registrar_fn(cnum).is_some() {
137 return Arc::new(Vec::new())
140 // Check to see if this crate is a "special runtime crate". These
141 // crates, implementation details of the standard library, typically
142 // have a bunch of `pub extern` and `#[no_mangle]` functions as the
143 // ABI between them. We don't want their symbols to have a `C`
144 // export level, however, as they're just implementation details.
145 // Down below we'll hardwire all of the symbols to the `Rust` export
147 let special_runtime_crate =
148 tcx.is_panic_runtime(cnum) || tcx.is_compiler_builtins(cnum);
150 let crate_exports = tcx
151 .exported_symbol_ids(cnum)
154 let name = tcx.symbol_name(Instance::mono(tcx, def_id));
155 let export_level = if special_runtime_crate {
156 // We can probably do better here by just ensuring that
157 // it has hidden visibility rather than public
158 // visibility, as this is primarily here to ensure it's
159 // not stripped during LTO.
161 // In general though we won't link right if these
162 // symbols are stripped, and LTO currently strips them.
163 if &*name == "rust_eh_personality" ||
164 &*name == "rust_eh_register_frames" ||
165 &*name == "rust_eh_unregister_frames" {
168 SymbolExportLevel::Rust
171 export_level(tcx, def_id)
173 debug!("EXPORTED SYMBOL (re-export): {} ({:?})", name, export_level);
174 (str::to_owned(&name), Some(def_id), export_level)
178 Arc::new(crate_exports)
182 fn export_level(tcx: TyCtxt, sym_def_id: DefId) -> SymbolExportLevel {
183 if tcx.contains_extern_indicator(sym_def_id) {
186 SymbolExportLevel::Rust