Skip to content

rustc: Add an option to default hidden visibility #47889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/libpanic_abort/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
#![feature(libc)]
#![feature(panic_runtime)]
#![feature(staged_api)]
#![feature(rustc_attrs)]

// Rust's "try" function, but if we're aborting on panics we just call the
// function as there's nothing else we need to do here.
#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rust_maybe_catch_panic(f: fn(*mut u8),
data: *mut u8,
_data_ptr: *mut usize,
Expand All @@ -50,6 +52,7 @@ pub unsafe extern fn __rust_maybe_catch_panic(f: fn(*mut u8),
// will kill us with an illegal instruction, which will do a good enough job for
// now hopefully.
#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rust_start_panic(_data: usize, _vtable: usize) -> u32 {
abort();

Expand Down
3 changes: 3 additions & 0 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,9 @@ define_dep_nodes!( <'tcx>
[] TargetFeaturesEnabled(DefId),

[] InstanceDefSizeEstimate { instance_def: InstanceDef<'tcx> },

[] GetSymbolExportLevel(DefId),

);

trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ty/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ define_maps! { <'tcx>
-> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>),
[] fn export_name: ExportName(DefId) -> Option<Symbol>,
[] fn contains_extern_indicator: ContainsExternIndicator(DefId) -> bool,
[] fn symbol_export_level: GetSymbolExportLevel(DefId) -> SymbolExportLevel,
[] fn is_translated_function: IsTranslatedFunction(DefId) -> bool,
[] fn codegen_unit: CodegenUnit(InternedString) -> Arc<CodegenUnit<'tcx>>,
[] fn compile_codegen_unit: CompileCodegenUnit(InternedString) -> Stats,
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/ty/maps/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,8 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,

DepKind::TargetFeaturesWhitelist => { force!(target_features_whitelist, LOCAL_CRATE); }
DepKind::TargetFeaturesEnabled => { force!(target_features_enabled, def_id!()); }

DepKind::GetSymbolExportLevel => { force!(symbol_export_level, def_id!()); }
}

true
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_back/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@ pub struct TargetOptions {

/// The codegen backend to use for this target, typically "llvm"
pub codegen_backend: String,

/// The default visibility for symbols in this target should be "hidden"
/// rather than "default"
pub default_hidden_visibility: bool,
}

impl Default for TargetOptions {
Expand Down Expand Up @@ -538,6 +542,7 @@ impl Default for TargetOptions {
no_builtins: false,
i128_lowering: false,
codegen_backend: "llvm".to_string(),
default_hidden_visibility: false,
}
}
}
Expand Down Expand Up @@ -785,6 +790,7 @@ impl Target {
key!(singlethread, bool);
key!(no_builtins, bool);
key!(codegen_backend);
key!(default_hidden_visibility, bool);

if let Some(array) = obj.find("abi-blacklist").and_then(Json::as_array) {
for name in array.iter().filter_map(|abi| abi.as_string()) {
Expand Down Expand Up @@ -982,6 +988,7 @@ impl ToJson for Target {
target_option_val!(singlethread);
target_option_val!(no_builtins);
target_option_val!(codegen_backend);
target_option_val!(default_hidden_visibility);

if default.abi_blacklist != self.options.abi_blacklist {
d.insert("abi-blacklist".to_string(), self.options.abi_blacklist.iter()
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_back/target/wasm32_unknown_unknown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ pub fn target() -> Result<Target, String> {
// performing LTO with compiler-builtins.
no_builtins: true,

// no dynamic linking, no need for default visibility!
default_hidden_visibility: true,

.. Default::default()
};
Ok(Target {
Expand Down
18 changes: 15 additions & 3 deletions src/librustc_mir/monomorphize/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ use rustc::dep_graph::WorkProductId;
use rustc::hir::def_id::DefId;
use rustc::hir::map::DefPathData;
use rustc::mir::mono::{Linkage, Visibility};
use rustc::middle::exported_symbols::SymbolExportLevel;
use rustc::ty::{self, TyCtxt, InstanceDef};
use rustc::ty::item_path::characteristic_def_id_of_type;
use rustc::util::nodemap::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -322,7 +323,16 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
.or_insert_with(make_codegen_unit);

let mut can_be_internalized = true;
let (linkage, visibility) = match trans_item.explicit_linkage(tcx) {
let default_visibility = |id: DefId| {
if tcx.sess.target.target.options.default_hidden_visibility &&
tcx.symbol_export_level(id) != SymbolExportLevel::C
{
Visibility::Hidden
} else {
Visibility::Default
}
};
let (linkage, mut visibility) = match trans_item.explicit_linkage(tcx) {
Some(explicit_linkage) => (explicit_linkage, Visibility::Default),
None => {
match trans_item {
Expand Down Expand Up @@ -352,7 +362,8 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
Visibility::Hidden
} else if def_id.is_local() {
if tcx.is_exported_symbol(def_id) {
Visibility::Default
can_be_internalized = false;
default_visibility(def_id)
} else {
Visibility::Hidden
}
Expand All @@ -375,7 +386,8 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
MonoItem::GlobalAsm(node_id) => {
let def_id = tcx.hir.local_def_id(node_id);
let visibility = if tcx.is_exported_symbol(def_id) {
Visibility::Default
can_be_internalized = false;
default_visibility(def_id)
} else {
Visibility::Hidden
};
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_trans/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ pub(crate) unsafe fn trans(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind)
name.as_ptr(),
ty);

if tcx.sess.target.target.options.default_hidden_visibility {
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
}

let callee = CString::new(kind.fn_name(method.name)).unwrap();
let callee = llvm::LLVMRustGetOrInsertFunction(llmod,
callee.as_ptr(),
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_trans/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub fn provide(providers: &mut Providers) {

Arc::new(local_crate)
};

providers.symbol_export_level = export_level;
}

pub fn provide_extern(providers: &mut Providers) {
Expand Down Expand Up @@ -203,6 +205,7 @@ pub fn provide_extern(providers: &mut Providers) {

Arc::new(crate_exports)
};
providers.symbol_export_level = export_level;
}

fn export_level(tcx: TyCtxt, sym_def_id: DefId) -> SymbolExportLevel {
Expand Down