From e6cf7da586bd795b39c8dd035aea142b2014594f Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 27 Oct 2024 09:46:35 +0000 Subject: [PATCH] Use protected visibility for librustc_driver.so when possible This should make rustc faster to start by reducing the load on the dynamic linker. --- src/bootstrap/src/core/build_steps/compile.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs index 27bbc8bd8ff22..5a9dd3d249eff 100644 --- a/src/bootstrap/src/core/build_steps/compile.rs +++ b/src/bootstrap/src/core/build_steps/compile.rs @@ -1111,6 +1111,18 @@ pub fn rustc_cargo( cargo.rustflag("-Clink-args=-Wl,--icf=all"); } + // Use protected visibility for rustc_driver.so. This significantly reduces the + // amount of symbols the dynamic linker has to process at runtime as protected + // symbols defined in the same DSO can't be overwritten by other DSO's and thus + // can be directly referenced rather than going through the GOT. Only do this + // with lld however as ld.bfd versions older than 2.40 will give a linker error + // when using protected symbols with object files compiled by LLVM. See + // https://davidlattimore.github.io/posts/2024/08/27/rust-dylib-rabbit-holes.html + // for more details. + if builder.config.lld_mode.is_used() { + cargo.rustflag("-Zdefault-visibility=protected"); + } + if builder.config.rust_profile_use.is_some() && builder.config.rust_profile_generate.is_some() { panic!("Cannot use and generate PGO profiles at the same time"); }