Skip to content

Commit 3639412

Browse files
committed
Auto merge of #131634 - davidlattimore:lld-protected, r=<try>
Use protected visibility when building rustc rust-lang/compiler-team#782 I wasn't sure about having two commits in a PR, but I figured, at least initially it might make sense to discuss these commits together. Happy to squash, or move the second commit to a separate PR. I contemplated trying to enable protected visibility for more cases when LLD will be used other than just `-Zlinker-features=+lld`, but that would be more a complex change that probably still wouldn't cover all cases when LLD is used, so went with the simplest option of just checking if the linker-feature is enabled. r? lqd
2 parents c8b8378 + d1f7f68 commit 3639412

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

config.example.toml

+5
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,11 @@
781781
# - If building for a zkvm target, "compiler-builtins-mem" will be added.
782782
#std-features = ["panic_unwind"]
783783

784+
# Whether to use protected symbols when compiling rustc. Doing so produces a binary that starts
785+
# faster. Setting this to true when linking with GNU ld < 2.40 will likely result in link errors.
786+
# Defaults to true when using lld to link rustc.
787+
#use-protected-symbols = true
788+
784789
# =============================================================================
785790
# Options for specific targets
786791
#

src/bootstrap/src/core/build_steps/compile.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,10 @@ pub fn rustc_cargo(
10561056
cargo.rustflag("-l").rustflag("Enzyme-19");
10571057
}
10581058

1059+
if builder.build.config.rust_use_protected_symbols {
1060+
cargo.rustflag("-Zdefault-visibility=protected");
1061+
}
1062+
10591063
// We currently don't support cross-crate LTO in stage0. This also isn't hugely necessary
10601064
// and may just be a time sink.
10611065
if compiler.stage != 0 {

src/bootstrap/src/core/config/config.rs

+11
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ pub struct Config {
291291
pub rust_lto: RustcLto,
292292
pub rust_validate_mir_opts: Option<u32>,
293293
pub rust_std_features: BTreeSet<String>,
294+
pub rust_use_protected_symbols: bool,
294295
pub llvm_profile_use: Option<String>,
295296
pub llvm_profile_generate: bool,
296297
pub llvm_libunwind_default: Option<LlvmLibunwind>,
@@ -1166,6 +1167,7 @@ define_config! {
11661167
lto: Option<String> = "lto",
11671168
validate_mir_opts: Option<u32> = "validate-mir-opts",
11681169
std_features: Option<BTreeSet<String>> = "std-features",
1170+
use_protected_symbols: Option<bool> = "use-protected-symbols",
11691171
}
11701172
}
11711173

@@ -1223,6 +1225,7 @@ impl Config {
12231225
dist_include_mingw_linker: true,
12241226
dist_compression_profile: "fast".into(),
12251227
rustc_parallel: true,
1228+
rust_use_protected_symbols: false,
12261229

12271230
stdout_is_tty: std::io::stdout().is_terminal(),
12281231
stderr_is_tty: std::io::stderr().is_terminal(),
@@ -1725,6 +1728,7 @@ impl Config {
17251728
strip,
17261729
lld_mode,
17271730
std_features: std_features_toml,
1731+
use_protected_symbols,
17281732
} = rust;
17291733

17301734
is_user_configured_rust_channel = channel.is_some();
@@ -1769,6 +1773,12 @@ impl Config {
17691773
set(&mut config.lld_mode, lld_mode);
17701774
set(&mut config.llvm_bitcode_linker_enabled, llvm_bitcode_linker);
17711775

1776+
// Default to using protected symbols only when linking with LLD.
1777+
if config.lld_mode != LldMode::Unused {
1778+
config.rust_use_protected_symbols = true;
1779+
}
1780+
set(&mut config.rust_use_protected_symbols, use_protected_symbols);
1781+
17721782
config.rust_randomize_layout = randomize_layout.unwrap_or_default();
17731783
config.llvm_tools_enabled = llvm_tools.unwrap_or(true);
17741784
config.rustc_parallel =
@@ -3109,6 +3119,7 @@ fn check_incompatible_options_for_ci_rustc(
31093119
download_rustc: _,
31103120
validate_mir_opts: _,
31113121
frame_pointers: _,
3122+
use_protected_symbols: _,
31123123
} = ci_rust_config;
31133124

31143125
// There are two kinds of checks for CI rustc incompatible options:

0 commit comments

Comments
 (0)