Skip to content

Commit a2cc5d6

Browse files
committed
rustc: Add an option to default hidden visibility
This commit adds a new option to target specifictions to specify that symbols should be "hidden" visibility by default in LLVM. While there are no existing targets that take advantage of this the `wasm32-unknown-unknown` target will soon start to use this visibility. The LLD linker currently interprets `hidden` as "don't export this from the wasm module" which is what we want for 90% of our functions. While the LLD linker does have a "export this symbol" argument which is what we use for other linkers, it was also somewhat easier to do this change instead which'll involve less arguments flying around. Additionally there's no need for non-`hidden` visibility for most of our symbols! This change should not immediately impact the wasm targets as-is, but rather this is laying the foundations for soon integrating LLD as a linker for wasm code.
1 parent 70f7d58 commit a2cc5d6

File tree

9 files changed

+41
-3
lines changed

9 files changed

+41
-3
lines changed

src/libpanic_abort/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
#![feature(libc)]
2828
#![feature(panic_runtime)]
2929
#![feature(staged_api)]
30+
#![feature(rustc_attrs)]
3031

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

src/librustc/dep_graph/dep_node.rs

+3
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,9 @@ define_dep_nodes!( <'tcx>
639639
[] TargetFeaturesEnabled(DefId),
640640

641641
[] InstanceDefSizeEstimate { instance_def: InstanceDef<'tcx> },
642+
643+
[] GetSymbolExportLevel(DefId),
644+
642645
);
643646

644647
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

src/librustc/ty/maps/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ define_maps! { <'tcx>
343343
-> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>),
344344
[] fn export_name: ExportName(DefId) -> Option<Symbol>,
345345
[] fn contains_extern_indicator: ContainsExternIndicator(DefId) -> bool,
346+
[] fn symbol_export_level: GetSymbolExportLevel(DefId) -> SymbolExportLevel,
346347
[] fn is_translated_function: IsTranslatedFunction(DefId) -> bool,
347348
[] fn codegen_unit: CodegenUnit(InternedString) -> Arc<CodegenUnit<'tcx>>,
348349
[] fn compile_codegen_unit: CompileCodegenUnit(InternedString) -> Stats,

src/librustc/ty/maps/plumbing.rs

+2
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,8 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
921921

922922
DepKind::TargetFeaturesWhitelist => { force!(target_features_whitelist, LOCAL_CRATE); }
923923
DepKind::TargetFeaturesEnabled => { force!(target_features_enabled, def_id!()); }
924+
925+
DepKind::GetSymbolExportLevel => { force!(symbol_export_level, def_id!()); }
924926
}
925927

926928
true

src/librustc_back/target/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ pub struct TargetOptions {
468468

469469
/// The codegen backend to use for this target, typically "llvm"
470470
pub codegen_backend: String,
471+
472+
/// The default visibility for symbols in this target should be "hidden"
473+
/// rather than "default"
474+
pub default_hidden_visibility: bool,
471475
}
472476

473477
impl Default for TargetOptions {
@@ -538,6 +542,7 @@ impl Default for TargetOptions {
538542
no_builtins: false,
539543
i128_lowering: false,
540544
codegen_backend: "llvm".to_string(),
545+
default_hidden_visibility: false,
541546
}
542547
}
543548
}
@@ -785,6 +790,7 @@ impl Target {
785790
key!(singlethread, bool);
786791
key!(no_builtins, bool);
787792
key!(codegen_backend);
793+
key!(default_hidden_visibility, bool);
788794

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

986993
if default.abi_blacklist != self.options.abi_blacklist {
987994
d.insert("abi-blacklist".to_string(), self.options.abi_blacklist.iter()

src/librustc_back/target/wasm32_unknown_unknown.rs

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pub fn target() -> Result<Target, String> {
8383
// performing LTO with compiler-builtins.
8484
no_builtins: true,
8585

86+
// no dynamic linking, no need for default visibility!
87+
default_hidden_visibility: true,
88+
8689
.. Default::default()
8790
};
8891
Ok(Target {

src/librustc_mir/monomorphize/partitioning.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ use rustc::dep_graph::WorkProductId;
107107
use rustc::hir::def_id::DefId;
108108
use rustc::hir::map::DefPathData;
109109
use rustc::mir::mono::{Linkage, Visibility};
110+
use rustc::middle::exported_symbols::SymbolExportLevel;
110111
use rustc::ty::{self, TyCtxt, InstanceDef};
111112
use rustc::ty::item_path::characteristic_def_id_of_type;
112113
use rustc::util::nodemap::{FxHashMap, FxHashSet};
@@ -322,7 +323,16 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
322323
.or_insert_with(make_codegen_unit);
323324

324325
let mut can_be_internalized = true;
325-
let (linkage, visibility) = match trans_item.explicit_linkage(tcx) {
326+
let default_visibility = |id: DefId| {
327+
if tcx.sess.target.target.options.default_hidden_visibility &&
328+
tcx.symbol_export_level(id) != SymbolExportLevel::C
329+
{
330+
Visibility::Hidden
331+
} else {
332+
Visibility::Default
333+
}
334+
};
335+
let (linkage, mut visibility) = match trans_item.explicit_linkage(tcx) {
326336
Some(explicit_linkage) => (explicit_linkage, Visibility::Default),
327337
None => {
328338
match trans_item {
@@ -352,7 +362,8 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
352362
Visibility::Hidden
353363
} else if def_id.is_local() {
354364
if tcx.is_exported_symbol(def_id) {
355-
Visibility::Default
365+
can_be_internalized = false;
366+
default_visibility(def_id)
356367
} else {
357368
Visibility::Hidden
358369
}
@@ -375,7 +386,8 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
375386
MonoItem::GlobalAsm(node_id) => {
376387
let def_id = tcx.hir.local_def_id(node_id);
377388
let visibility = if tcx.is_exported_symbol(def_id) {
378-
Visibility::Default
389+
can_be_internalized = false;
390+
default_visibility(def_id)
379391
} else {
380392
Visibility::Hidden
381393
};

src/librustc_trans/allocator.rs

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ pub(crate) unsafe fn trans(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind)
8686
name.as_ptr(),
8787
ty);
8888

89+
if tcx.sess.target.target.options.default_hidden_visibility {
90+
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
91+
}
92+
8993
let callee = CString::new(kind.fn_name(method.name)).unwrap();
9094
let callee = llvm::LLVMRustGetOrInsertFunction(llmod,
9195
callee.as_ptr(),

src/librustc_trans/back/symbol_export.rs

+3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ pub fn provide(providers: &mut Providers) {
133133

134134
Arc::new(local_crate)
135135
};
136+
137+
providers.symbol_export_level = export_level;
136138
}
137139

138140
pub fn provide_extern(providers: &mut Providers) {
@@ -203,6 +205,7 @@ pub fn provide_extern(providers: &mut Providers) {
203205

204206
Arc::new(crate_exports)
205207
};
208+
providers.symbol_export_level = export_level;
206209
}
207210

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

0 commit comments

Comments
 (0)