From a6c2f73b6e0d24af5396355886b26bb23885c37e Mon Sep 17 00:00:00 2001 From: YI Date: Sun, 3 May 2020 12:36:12 +0800 Subject: [PATCH] add linking option strip move strip option to "Z" add more strip options, remove strip-debuginfo-if-disabled merge strip and debuginfo --- src/librustc_codegen_ssa/back/link.rs | 4 +- src/librustc_codegen_ssa/back/linker.rs | 83 +++++++++++++++---------- src/librustc_interface/tests.rs | 3 +- src/librustc_session/config.rs | 13 ++++ src/librustc_session/options.rs | 16 ++++- 5 files changed, 81 insertions(+), 38 deletions(-) diff --git a/src/librustc_codegen_ssa/back/link.rs b/src/librustc_codegen_ssa/back/link.rs index 7a0e1e2c63887..d8b38cf33707c 100644 --- a/src/librustc_codegen_ssa/back/link.rs +++ b/src/librustc_codegen_ssa/back/link.rs @@ -1500,8 +1500,8 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>( cmd.optimize(); // OBJECT-FILES-NO, AUDIT-ORDER - // Pass debuginfo flags down to the linker. - cmd.debuginfo(); + // Pass debuginfo and strip flags down to the linker. + cmd.debuginfo(sess.opts.debugging_opts.strip); // OBJECT-FILES-NO, AUDIT-ORDER // We want to prevent the compiler from accidentally leaking in any system libraries, diff --git a/src/librustc_codegen_ssa/back/linker.rs b/src/librustc_codegen_ssa/back/linker.rs index 7c89fdfbe1b87..535c4ff092f5f 100644 --- a/src/librustc_codegen_ssa/back/linker.rs +++ b/src/librustc_codegen_ssa/back/linker.rs @@ -14,7 +14,7 @@ use rustc_hir::def_id::{CrateNum, LOCAL_CRATE}; use rustc_middle::middle::dependency_format::Linkage; use rustc_middle::ty::TyCtxt; use rustc_serialize::{json, Encoder}; -use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel}; +use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip}; use rustc_session::Session; use rustc_span::symbol::Symbol; use rustc_target::spec::{LinkerFlavor, LldFlavor}; @@ -122,7 +122,7 @@ pub trait Linker { fn optimize(&mut self); fn pgo_gen(&mut self); fn control_flow_guard(&mut self); - fn debuginfo(&mut self); + fn debuginfo(&mut self, strip: Strip); fn no_default_libraries(&mut self); fn build_dylib(&mut self, out_filename: &Path); fn build_static_executable(&mut self); @@ -392,15 +392,16 @@ impl<'a> Linker for GccLinker<'a> { self.sess.warn("Windows Control Flow Guard is not supported by this linker."); } - fn debuginfo(&mut self) { - if let DebugInfo::None = self.sess.opts.debuginfo { - // If we are building without debuginfo enabled and we were called with - // `-Zstrip-debuginfo-if-disabled=yes`, tell the linker to strip any debuginfo - // found when linking to get rid of symbols from libstd. - if self.sess.opts.debugging_opts.strip_debuginfo_if_disabled { - self.linker_arg("-S"); + fn debuginfo(&mut self, strip: Strip) { + match strip { + Strip::None => {} + Strip::Debuginfo => { + self.linker_arg("--strip-debug"); } - }; + Strip::Symbols => { + self.linker_arg("--strip-all"); + } + } } fn no_default_libraries(&mut self) { @@ -686,29 +687,37 @@ impl<'a> Linker for MsvcLinker<'a> { self.cmd.arg("/guard:cf"); } - fn debuginfo(&mut self) { - // This will cause the Microsoft linker to generate a PDB file - // from the CodeView line tables in the object files. - self.cmd.arg("/DEBUG"); - - // This will cause the Microsoft linker to embed .natvis info into the PDB file - let natvis_dir_path = self.sess.sysroot.join("lib\\rustlib\\etc"); - if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) { - for entry in natvis_dir { - match entry { - Ok(entry) => { - let path = entry.path(); - if path.extension() == Some("natvis".as_ref()) { - let mut arg = OsString::from("/NATVIS:"); - arg.push(path); - self.cmd.arg(arg); + fn debuginfo(&mut self, strip: Strip) { + match strip { + Strip::None => { + // This will cause the Microsoft linker to generate a PDB file + // from the CodeView line tables in the object files. + self.cmd.arg("/DEBUG"); + + // This will cause the Microsoft linker to embed .natvis info into the PDB file + let natvis_dir_path = self.sess.sysroot.join("lib\\rustlib\\etc"); + if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) { + for entry in natvis_dir { + match entry { + Ok(entry) => { + let path = entry.path(); + if path.extension() == Some("natvis".as_ref()) { + let mut arg = OsString::from("/NATVIS:"); + arg.push(path); + self.cmd.arg(arg); + } + } + Err(err) => { + self.sess + .warn(&format!("error enumerating natvis directory: {}", err)); + } } } - Err(err) => { - self.sess.warn(&format!("error enumerating natvis directory: {}", err)); - } } } + Strip::Debuginfo | Strip::Symbols => { + self.cmd.arg("/DEBUG:NONE"); + } } } @@ -889,7 +898,7 @@ impl<'a> Linker for EmLinker<'a> { self.sess.warn("Windows Control Flow Guard is not supported by this linker."); } - fn debuginfo(&mut self) { + fn debuginfo(&mut self, _strip: Strip) { // Preserve names or generate source maps depending on debug info self.cmd.arg(match self.sess.opts.debuginfo { DebugInfo::None => "-g0", @@ -1081,7 +1090,17 @@ impl<'a> Linker for WasmLd<'a> { fn pgo_gen(&mut self) {} - fn debuginfo(&mut self) {} + fn debuginfo(&mut self, strip: Strip) { + match strip { + Strip::None => {} + Strip::Debuginfo => { + self.cmd.arg("--strip-debug"); + } + Strip::Symbols => { + self.cmd.arg("--strip-all"); + } + } + } fn control_flow_guard(&mut self) { self.sess.warn("Windows Control Flow Guard is not supported by this linker."); @@ -1184,7 +1203,7 @@ impl<'a> Linker for PtxLinker<'a> { self.cmd.arg("-L").arg(path); } - fn debuginfo(&mut self) { + fn debuginfo(&mut self, _strip: Strip) { self.cmd.arg("--debug"); } diff --git a/src/librustc_interface/tests.rs b/src/librustc_interface/tests.rs index 0a200426e38ea..dff78c3e053a4 100644 --- a/src/librustc_interface/tests.rs +++ b/src/librustc_interface/tests.rs @@ -3,6 +3,7 @@ use crate::interface::parse_cfgspecs; use rustc_data_structures::fx::FxHashSet; use rustc_errors::{emitter::HumanReadableErrorType, registry, ColorConfig}; use rustc_middle::middle::cstore; +use rustc_session::config::Strip; use rustc_session::config::{build_configuration, build_session_options, to_crate_config}; use rustc_session::config::{rustc_optgroups, ErrorOutputType, ExternLocation, Options, Passes}; use rustc_session::config::{CFGuard, ExternEntry, LinkerPluginLto, LtoCli, SwitchWithOptPath}; @@ -499,6 +500,7 @@ fn test_debugging_options_tracking_hash() { untracked!(self_profile, SwitchWithOptPath::Enabled(None)); untracked!(self_profile_events, Some(vec![String::new()])); untracked!(span_free_formats, true); + untracked!(strip, Strip::None); untracked!(terminal_width, Some(80)); untracked!(threads, 99); untracked!(time, true); @@ -562,7 +564,6 @@ fn test_debugging_options_tracking_hash() { tracked!(share_generics, Some(true)); tracked!(show_span, Some(String::from("abc"))); tracked!(src_hash_algorithm, Some(SourceFileHashAlgorithm::Sha1)); - tracked!(strip_debuginfo_if_disabled, true); tracked!(symbol_mangling_version, SymbolManglingVersion::V0); tracked!(teach, true); tracked!(thinlto, Some(true)); diff --git a/src/librustc_session/config.rs b/src/librustc_session/config.rs index 8a690621a6e8f..402ed054be460 100644 --- a/src/librustc_session/config.rs +++ b/src/librustc_session/config.rs @@ -69,6 +69,19 @@ impl FromStr for Sanitizer { } } +/// The different settings that the `-Z strip` flag can have. +#[derive(Clone, Copy, PartialEq, Hash, Debug)] +pub enum Strip { + /// Do not strip at all. + None, + + /// Strip debuginfo. + Debuginfo, + + /// Strip all symbols. + Symbols, +} + /// The different settings that the `-Z control_flow_guard` flag can have. #[derive(Clone, Copy, PartialEq, Hash, Debug)] pub enum CFGuard { diff --git a/src/librustc_session/options.rs b/src/librustc_session/options.rs index b03fc00d93db2..2c5ae1a259d41 100644 --- a/src/librustc_session/options.rs +++ b/src/librustc_session/options.rs @@ -253,6 +253,7 @@ macro_rules! options { pub const parse_sanitizer_list: &str = "comma separated list of sanitizers"; pub const parse_sanitizer_memory_track_origins: &str = "0, 1, or 2"; pub const parse_cfguard: &str = "either `disabled`, `nochecks`, or `checks`"; + pub const parse_strip: &str = "either `none`, `debuginfo`, or `symbols`"; pub const parse_linker_flavor: &str = ::rustc_target::spec::LinkerFlavor::one_of(); pub const parse_optimization_fuel: &str = "crate=integer"; pub const parse_unpretty: &str = "`string` or `string=string`"; @@ -491,6 +492,16 @@ macro_rules! options { } } + fn parse_strip(slot: &mut Strip, v: Option<&str>) -> bool { + match v { + Some("none") => *slot = Strip::None, + Some("debuginfo") => *slot = Strip::Debuginfo, + Some("symbols") => *slot = Strip::Symbols, + _ => return false, + } + true + } + fn parse_cfguard(slot: &mut CFGuard, v: Option<&str>) -> bool { match v { Some("disabled") => *slot = CFGuard::Disabled, @@ -960,9 +971,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, "exclude spans when debug-printing compiler state (default: no)"), src_hash_algorithm: Option = (None, parse_src_file_hash, [TRACKED], "hash algorithm of source files in debug info (`md5`, or `sha1`)"), - strip_debuginfo_if_disabled: bool = (false, parse_bool, [TRACKED], - "tell the linker to strip debuginfo when building without debuginfo enabled \ - (default: no)"), + strip: Strip = (Strip::None, parse_strip, [UNTRACKED], + "tell the linker which information to strip (`none` (default), `debuginfo` or `symbols`)"), symbol_mangling_version: SymbolManglingVersion = (SymbolManglingVersion::Legacy, parse_symbol_mangling_version, [TRACKED], "which mangling version to use for symbol names"),