|
| 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. |
| 4 | +// |
| 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. |
| 10 | + |
| 11 | +use context::SharedCrateContext; |
| 12 | +use monomorphize::Instance; |
| 13 | +use symbol_map::SymbolMap; |
| 14 | +use util::nodemap::FxHashMap; |
| 15 | +use rustc::hir::def_id::{DefId, CrateNum, LOCAL_CRATE}; |
| 16 | +use rustc::session::config; |
| 17 | +use syntax::attr; |
| 18 | +use trans_item::TransItem; |
| 19 | + |
| 20 | +/// The SymbolExportLevel of a symbols specifies from which kinds of crates |
| 21 | +/// the symbol will be exported. `C` symbols will be exported from any |
| 22 | +/// kind of crate, including cdylibs which export very few things. |
| 23 | +/// `Rust` will only be exported if the crate produced is a Rust |
| 24 | +/// dylib. |
| 25 | +#[derive(Eq, PartialEq, Debug, Copy, Clone)] |
| 26 | +pub enum SymbolExportLevel { |
| 27 | + C, |
| 28 | + Rust, |
| 29 | +} |
| 30 | + |
| 31 | +/// The set of symbols exported from each crate in the crate graph. |
| 32 | +pub struct ExportedSymbols { |
| 33 | + exports: FxHashMap<CrateNum, Vec<(String, SymbolExportLevel)>>, |
| 34 | +} |
| 35 | + |
| 36 | +impl ExportedSymbols { |
| 37 | + |
| 38 | + pub fn empty() -> ExportedSymbols { |
| 39 | + ExportedSymbols { |
| 40 | + exports: FxHashMap(), |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + pub fn compute_from<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>, |
| 45 | + symbol_map: &SymbolMap<'tcx>) |
| 46 | + -> ExportedSymbols { |
| 47 | + let mut local_crate: Vec<_> = scx |
| 48 | + .exported_symbols() |
| 49 | + .iter() |
| 50 | + .map(|&node_id| { |
| 51 | + scx.tcx().map.local_def_id(node_id) |
| 52 | + }) |
| 53 | + .map(|def_id| { |
| 54 | + (symbol_for_def_id(scx, def_id, symbol_map), |
| 55 | + export_level(scx, def_id)) |
| 56 | + }) |
| 57 | + .collect(); |
| 58 | + |
| 59 | + if scx.sess().entry_fn.borrow().is_some() { |
| 60 | + local_crate.push(("main".to_string(), SymbolExportLevel::C)); |
| 61 | + } |
| 62 | + |
| 63 | + if let Some(id) = scx.sess().derive_registrar_fn.get() { |
| 64 | + let svh = &scx.link_meta().crate_hash; |
| 65 | + let def_id = scx.tcx().map.local_def_id(id); |
| 66 | + let idx = def_id.index; |
| 67 | + let registrar = scx.sess().generate_derive_registrar_symbol(svh, idx); |
| 68 | + local_crate.push((registrar, SymbolExportLevel::C)); |
| 69 | + } |
| 70 | + |
| 71 | + if scx.sess().crate_types.borrow().contains(&config::CrateTypeDylib) { |
| 72 | + local_crate.push((scx.metadata_symbol_name(), |
| 73 | + SymbolExportLevel::Rust)); |
| 74 | + } |
| 75 | + |
| 76 | + let mut exports = FxHashMap(); |
| 77 | + exports.insert(LOCAL_CRATE, local_crate); |
| 78 | + |
| 79 | + for cnum in scx.sess().cstore.crates() { |
| 80 | + debug_assert!(cnum != LOCAL_CRATE); |
| 81 | + |
| 82 | + if scx.sess().cstore.plugin_registrar_fn(cnum).is_some() || |
| 83 | + scx.sess().cstore.derive_registrar_fn(cnum).is_some() { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + let crate_exports = scx |
| 88 | + .sess() |
| 89 | + .cstore |
| 90 | + .exported_symbols(cnum) |
| 91 | + .iter() |
| 92 | + .map(|&def_id| { |
| 93 | + debug!("EXTERN-SYMBOL: {:?}", def_id); |
| 94 | + let name = Instance::mono(scx, def_id).symbol_name(scx); |
| 95 | + (name, export_level(scx, def_id)) |
| 96 | + }) |
| 97 | + .collect(); |
| 98 | + |
| 99 | + exports.insert(cnum, crate_exports); |
| 100 | + } |
| 101 | + |
| 102 | + return ExportedSymbols { |
| 103 | + exports: exports |
| 104 | + }; |
| 105 | + |
| 106 | + fn export_level(scx: &SharedCrateContext, |
| 107 | + sym_def_id: DefId) |
| 108 | + -> SymbolExportLevel { |
| 109 | + let attrs = scx.tcx().get_attrs(sym_def_id); |
| 110 | + if attr::contains_extern_indicator(scx.sess().diagnostic(), &attrs) { |
| 111 | + SymbolExportLevel::C |
| 112 | + } else { |
| 113 | + SymbolExportLevel::Rust |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + pub fn exported_symbols(&self, |
| 119 | + cnum: CrateNum) |
| 120 | + -> &[(String, SymbolExportLevel)] { |
| 121 | + match self.exports.get(&cnum) { |
| 122 | + Some(exports) => &exports[..], |
| 123 | + None => &[] |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + pub fn for_each_exported_symbol<F>(&self, |
| 128 | + cnum: CrateNum, |
| 129 | + export_threshold: SymbolExportLevel, |
| 130 | + mut f: F) |
| 131 | + where F: FnMut(&str, SymbolExportLevel) |
| 132 | + { |
| 133 | + for &(ref name, export_level) in self.exported_symbols(cnum) { |
| 134 | + if is_below_threshold(export_level, export_threshold) { |
| 135 | + f(&name[..], export_level) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +pub fn crate_export_threshold(crate_type: config::CrateType) |
| 142 | + -> SymbolExportLevel { |
| 143 | + match crate_type { |
| 144 | + config::CrateTypeExecutable | |
| 145 | + config::CrateTypeStaticlib | |
| 146 | + config::CrateTypeProcMacro | |
| 147 | + config::CrateTypeCdylib => SymbolExportLevel::C, |
| 148 | + config::CrateTypeRlib | |
| 149 | + config::CrateTypeMetadata | |
| 150 | + config::CrateTypeDylib => SymbolExportLevel::Rust, |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +pub fn crates_export_threshold(crate_types: &[config::CrateType]) |
| 155 | + -> SymbolExportLevel { |
| 156 | + if crate_types.iter().any(|&crate_type| { |
| 157 | + crate_export_threshold(crate_type) == SymbolExportLevel::Rust |
| 158 | + }) { |
| 159 | + SymbolExportLevel::Rust |
| 160 | + } else { |
| 161 | + SymbolExportLevel::C |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +pub fn is_below_threshold(level: SymbolExportLevel, |
| 166 | + threshold: SymbolExportLevel) |
| 167 | + -> bool { |
| 168 | + if threshold == SymbolExportLevel::Rust { |
| 169 | + // We export everything from Rust dylibs |
| 170 | + true |
| 171 | + } else { |
| 172 | + level == SymbolExportLevel::C |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +fn symbol_for_def_id<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>, |
| 177 | + def_id: DefId, |
| 178 | + symbol_map: &SymbolMap<'tcx>) |
| 179 | + -> String { |
| 180 | + // Just try to look things up in the symbol map. If nothing's there, we |
| 181 | + // recompute. |
| 182 | + if let Some(node_id) = scx.tcx().map.as_local_node_id(def_id) { |
| 183 | + if let Some(sym) = symbol_map.get(TransItem::Static(node_id)) { |
| 184 | + return sym.to_owned(); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + let instance = Instance::mono(scx, def_id); |
| 189 | + |
| 190 | + symbol_map.get(TransItem::Fn(instance)) |
| 191 | + .map(str::to_owned) |
| 192 | + .unwrap_or_else(|| instance.symbol_name(scx)) |
| 193 | +} |
0 commit comments