Skip to content

Commit b824fc4

Browse files
committed
Add unstable -Zdefault-hidden-visibility cmdline flag for rustc.
The new flag has been described in the Major Change Proposal at rust-lang/compiler-team#656
1 parent 5facb42 commit b824fc4

File tree

10 files changed

+69
-8
lines changed

10 files changed

+69
-8
lines changed

compiler/rustc_codegen_gcc/src/allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn create_wrapper_function(
9090
.collect();
9191
let func = context.new_function(None, FunctionType::Exported, output.unwrap_or(void), &args, from_name, false);
9292

93-
if tcx.sess.target.options.default_hidden_visibility {
93+
if tcx.sess.default_hidden_visibility() {
9494
#[cfg(feature="master")]
9595
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
9696
}

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub(crate) unsafe fn codegen(
7676
// __rust_alloc_error_handler_should_panic
7777
let name = OomStrategy::SYMBOL;
7878
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
79-
if tcx.sess.target.default_hidden_visibility {
79+
if tcx.sess.default_hidden_visibility() {
8080
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
8181
}
8282
let val = tcx.sess.opts.unstable_opts.oom.should_panic();
@@ -85,7 +85,7 @@ pub(crate) unsafe fn codegen(
8585

8686
let name = NO_ALLOC_SHIM_IS_UNSTABLE;
8787
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
88-
if tcx.sess.target.default_hidden_visibility {
88+
if tcx.sess.default_hidden_visibility() {
8989
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
9090
}
9191
let llval = llvm::LLVMConstInt(i8, 0, False);
@@ -130,7 +130,7 @@ fn create_wrapper_function(
130130
None
131131
};
132132

133-
if tcx.sess.target.default_hidden_visibility {
133+
if tcx.sess.default_hidden_visibility() {
134134
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
135135
}
136136
if tcx.sess.must_emit_unwind_tables() {

compiler/rustc_codegen_llvm/src/declare.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
8484
fn_type: &'ll Type,
8585
) -> &'ll Value {
8686
// Declare C ABI functions with the visibility used by C by default.
87-
let visibility = if self.tcx.sess.target.default_hidden_visibility {
87+
let visibility = if self.tcx.sess.default_hidden_visibility() {
8888
llvm::Visibility::Hidden
8989
} else {
9090
llvm::Visibility::Default
@@ -107,7 +107,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
107107
unnamed: llvm::UnnamedAddr,
108108
fn_type: &'ll Type,
109109
) -> &'ll Value {
110-
let visibility = if self.tcx.sess.target.default_hidden_visibility {
110+
let visibility = if self.tcx.sess.default_hidden_visibility() {
111111
llvm::Visibility::Hidden
112112
} else {
113113
llvm::Visibility::Default

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ fn test_unstable_options_tracking_hash() {
749749
tracked!(cross_crate_inline_threshold, InliningThreshold::Always);
750750
tracked!(debug_info_for_profiling, true);
751751
tracked!(debug_macros, true);
752+
tracked!(default_hidden_visibility, None);
752753
tracked!(dep_info_omit_d_target, true);
753754
tracked!(dual_proc_macros, true);
754755
tracked!(dwarf_version, Some(5));

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ fn mono_item_visibility<'tcx>(
883883
}
884884

885885
fn default_visibility(tcx: TyCtxt<'_>, id: DefId, is_generic: bool) -> Visibility {
886-
if !tcx.sess.target.default_hidden_visibility {
886+
if !tcx.sess.default_hidden_visibility() {
887887
return Visibility::Default;
888888
}
889889

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,8 @@ options! {
15381538
"compress debug info sections (none, zlib, zstd, default: none)"),
15391539
deduplicate_diagnostics: bool = (true, parse_bool, [UNTRACKED],
15401540
"deduplicate identical diagnostics (default: yes)"),
1541+
default_hidden_visibility: Option<bool> = (None, parse_opt_bool, [TRACKED],
1542+
"overrides the `default_hidden_visibility` setting of the target"),
15411543
dep_info_omit_d_target: bool = (false, parse_bool, [TRACKED],
15421544
"in dep-info output, omit targets for tracking dependencies of the dep-info files \
15431545
themselves (default: no)"),

compiler/rustc_session/src/session.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,14 @@ impl Session {
971971
termize::dimensions().map_or(default_column_width, |(w, _)| w)
972972
}
973973
}
974+
975+
/// Whether the default visibility of symbols should be "hidden" rather than "default".
976+
pub fn default_hidden_visibility(&self) -> bool {
977+
self.opts
978+
.unstable_opts
979+
.default_hidden_visibility
980+
.unwrap_or(self.target.options.default_hidden_visibility)
981+
}
974982
}
975983

976984
// JUSTIFICATION: defn of the suggested wrapper fns

compiler/rustc_target/src/spec/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,11 @@ pub struct TargetOptions {
20882088
pub no_builtins: bool,
20892089

20902090
/// The default visibility for symbols in this target should be "hidden"
2091-
/// rather than "default"
2091+
/// rather than "default".
2092+
///
2093+
/// This value typically shouldn't be accessed directly, but through
2094+
/// the `rustc_session::Session::default_hidden_visibility` method, which
2095+
/// allows `rustc` users to override this setting using cmdline flags.
20922096
pub default_hidden_visibility: bool,
20932097

20942098
/// Whether a .debug_gdb_scripts section will be added to the output object file
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# `default-hidden-visibility`
2+
3+
The tracking issue for this feature is: https://github.com/rust-lang/compiler-team/issues/656
4+
5+
------------------------
6+
7+
This flag can be used to override the target's
8+
[`default_hidden_visibility`](https://doc.rust-lang.org/beta/nightly-rustc/rustc_target/spec/struct.TargetOptions.html#structfield.default_hidden_visibility)
9+
setting.
10+
Using `-Zdefault_hidden_visibility=yes` is roughly equivalent to Clang's
11+
[`-fvisibility=hidden`](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fvisibility)
12+
cmdline flag.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Verifies that `TargetOptions::default_hidden_visibility` is set when using the related cmdline
2+
// flag. This is a regression test for https://github.com/rust-lang/compiler-team/issues/656.
3+
// See also https://github.com/rust-lang/rust/issues/73295 and
4+
// https://github.com/rust-lang/rust/issues/37530.
5+
//
6+
// revisions:NONE YES NO
7+
//[YES] compile-flags: -Zdefault-hidden-visibility=yes
8+
//[NO] compile-flags: -Zdefault-hidden-visibility=no
9+
10+
// The test scenario is specifically about visibility of symbols in Rust static libraries.
11+
//
12+
// This mimics the relevant part from https://github.com/rust-lang/rust/issues/73295 which
13+
// says:
14+
//
15+
// > We build Rust code into these DSOs in the approved way, which is to aggregate a bunch of Rust
16+
// > libraries (rlibs) into a separate Rust ***staticlib*** for each of the DSOs. (For example,
17+
// > libbase_rust_deps.a and libservices_rust_deps.a). The final C++ linker links exactly one of
18+
// > these staticlibs together with the C++ .a and .o in the final construction of the DSO.
19+
#![crate_type = "staticlib"]
20+
21+
// The test scenario needs to use a public, but non-`#[no_mangle]` Rust symbol.
22+
//
23+
// We want to check the visibility of this symbol:
24+
//
25+
// FIXME: 1) confirm that this test file actually repros the problem at hand (I am not at all
26+
// confident that it does)
27+
// FIXME: 2) fix test expectations below ("internal" below seems unexpected for NONE scenario? no
28+
// idea what should be the expectation for YES scenario)
29+
//
30+
// NONE: @_ZN25default_hidden_visibility15exported_symbol17hc5deee4a42a30cf5E = internal constant
31+
// YES: @_ZN25default_hidden_visibility15exported_symbol17hc5deee4a42a30cf5E = internal constant
32+
// NO: @_ZN25default_hidden_visibility15exported_symbol17hc5deee4a42a30cf5E = internal constant
33+
#[used]
34+
pub static exported_symbol: [u8; 6] = *b"foobar";

0 commit comments

Comments
 (0)