]> git.lizzy.rs Git - rust.git/commitdiff
add linking option strip
authorYI <uuuuuu@protonmail.com>
Sun, 3 May 2020 04:36:12 +0000 (12:36 +0800)
committerYI <uuuuuu@protonmail.com>
Sun, 10 May 2020 08:44:46 +0000 (16:44 +0800)
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
src/librustc_codegen_ssa/back/linker.rs
src/librustc_interface/tests.rs
src/librustc_session/config.rs
src/librustc_session/options.rs

index 7a0e1e2c63887f63e901bb423f099e473d5a28a6..d8b38cf33707c27aa461e166eee88948806c7fca 100644 (file)
@@ -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,
index 7c89fdfbe1b879b26e529ad4df0bfa1b6bb12db5..535c4ff092f5f3f79a6b0e371f7d8856aac99756 100644 (file)
@@ -14,7 +14,7 @@
 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 @@ fn control_flow_guard(&mut self) {
         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 @@ fn control_flow_guard(&mut self) {
         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 @@ fn control_flow_guard(&mut self) {
         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 @@ fn optimize(&mut self) {
 
     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 @@ fn include_path(&mut self, path: &Path) {
         self.cmd.arg("-L").arg(path);
     }
 
-    fn debuginfo(&mut self) {
+    fn debuginfo(&mut self, _strip: Strip) {
         self.cmd.arg("--debug");
     }
 
index 0a200426e38ea88d8d0ed8c2712ade7899d24984..dff78c3e053a45e07a875c16b12a3d580868fff0 100644 (file)
@@ -3,6 +3,7 @@
 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 @@ macro_rules! untracked {
     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 @@ macro_rules! tracked {
     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));
index 8a690621a6e8f044df921bca724fe27d574f15ba..402ed054be460c7b357626beebcbc96e532605f6 100644 (file)
@@ -69,6 +69,19 @@ fn from_str(s: &str) -> Result<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 {
index b03fc00d93db28a879e475cbce5faf55858aa0a0..2c5ae1a259d414e8899b569888fee34c4d9eff35 100644 (file)
@@ -253,6 +253,7 @@ mod $mod_desc {
         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 @@ fn parse_sanitizer_memory_track_origins(slot: &mut usize, v: Option<&str>) -> bo
             }
         }
 
+        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 @@ fn parse_src_file_hash(slot: &mut Option<SourceFileHashAlgorithm>, v: Option<&st
         "exclude spans when debug-printing compiler state (default: no)"),
     src_hash_algorithm: Option<SourceFileHashAlgorithm> = (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"),