From d1f7f689014f93f97fd434bcf55153cf91233443 Mon Sep 17 00:00:00 2001 From: David Lattimore Date: Sun, 27 Oct 2024 13:54:31 +1100 Subject: [PATCH] Use protected symbols when building rustc_driver --- config.example.toml | 5 +++++ src/bootstrap/src/core/build_steps/compile.rs | 4 ++++ src/bootstrap/src/core/config/config.rs | 11 +++++++++++ 3 files changed, 20 insertions(+) diff --git a/config.example.toml b/config.example.toml index d5b904ebf3d59..c4fe06e91f048 100644 --- a/config.example.toml +++ b/config.example.toml @@ -780,6 +780,11 @@ # - If building for a zkvm target, "compiler-builtins-mem" will be added. #std-features = ["panic_unwind"] +# Whether to use protected symbols when compiling rustc. Doing so produces a binary that starts +# faster. Setting this to true when linking with GNU ld < 2.40 will likely result in link errors. +# Defaults to true when using lld to link rustc. +#use-protected-symbols = true + # ============================================================================= # Options for specific targets # diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs index e13d4ccc61824..2b655012b9d63 100644 --- a/src/bootstrap/src/core/build_steps/compile.rs +++ b/src/bootstrap/src/core/build_steps/compile.rs @@ -1057,6 +1057,10 @@ pub fn rustc_cargo( cargo.rustflag("-l").rustflag("Enzyme-19"); } + if builder.build.config.rust_use_protected_symbols { + cargo.rustflag("-Zdefault-visibility=protected"); + } + // We currently don't support cross-crate LTO in stage0. This also isn't hugely necessary // and may just be a time sink. if compiler.stage != 0 { diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 139ca7eb52e7b..23099ad4dfe5d 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -291,6 +291,7 @@ pub struct Config { pub rust_lto: RustcLto, pub rust_validate_mir_opts: Option, pub rust_std_features: BTreeSet, + pub rust_use_protected_symbols: bool, pub llvm_profile_use: Option, pub llvm_profile_generate: bool, pub llvm_libunwind_default: Option, @@ -1166,6 +1167,7 @@ define_config! { lto: Option = "lto", validate_mir_opts: Option = "validate-mir-opts", std_features: Option> = "std-features", + use_protected_symbols: Option = "use-protected-symbols", } } @@ -1223,6 +1225,7 @@ impl Config { dist_include_mingw_linker: true, dist_compression_profile: "fast".into(), rustc_parallel: true, + rust_use_protected_symbols: false, stdout_is_tty: std::io::stdout().is_terminal(), stderr_is_tty: std::io::stderr().is_terminal(), @@ -1725,6 +1728,7 @@ impl Config { strip, lld_mode, std_features: std_features_toml, + use_protected_symbols, } = rust; is_user_configured_rust_channel = channel.is_some(); @@ -1769,6 +1773,12 @@ impl Config { set(&mut config.lld_mode, lld_mode); set(&mut config.llvm_bitcode_linker_enabled, llvm_bitcode_linker); + // Default to using protected symbols only when linking with LLD. + if config.lld_mode != LldMode::Unused { + config.rust_use_protected_symbols = true; + } + set(&mut config.rust_use_protected_symbols, use_protected_symbols); + config.rust_randomize_layout = randomize_layout.unwrap_or_default(); config.llvm_tools_enabled = llvm_tools.unwrap_or(true); config.rustc_parallel = @@ -3102,6 +3112,7 @@ fn check_incompatible_options_for_ci_rustc( download_rustc: _, validate_mir_opts: _, frame_pointers: _, + use_protected_symbols: _, } = ci_rust_config; // There are two kinds of checks for CI rustc incompatible options: