]> git.lizzy.rs Git - rust.git/commitdiff
Add -C link-dead-code option r=alexcrichton
authorJohan Lorenzo <jlorenzo@mozilla.com>
Tue, 2 Feb 2016 17:56:59 +0000 (17:56 +0000)
committerJohan Lorenzo <jlorenzo@mozilla.com>
Thu, 11 Feb 2016 10:14:32 +0000 (11:14 +0100)
Turning gc-sections off improves code coverage based for tools which
use DWARF debugging information (like kcov). Otherwise dead code is
stripped and kcov returns a coverage percentage that doesn't reflect
reality.

src/librustc/session/config.rs
src/librustc_trans/back/link.rs
src/test/run-make/codegen-options-parsing/Makefile

index f60792178291a976d01812494445dc5587afad05..2091ebbf505f6acaa244609e4b62a244efef884e 100644 (file)
@@ -507,6 +507,8 @@ fn parse_passes(slot: &mut Passes, v: Option<&str>) -> bool {
         "system linker to link outputs with"),
     link_args: Option<Vec<String>> = (None, parse_opt_list,
         "extra arguments to pass to the linker (space separated)"),
+    link_dead_code: bool = (false, parse_bool,
+        "let the linker strip dead coded (turning it on can be used for code coverage)"),
     lto: bool = (false, parse_bool,
         "perform LLVM link-time optimizations"),
     target_cpu: Option<String> = (None, parse_opt_string,
index 0f327d5c84cfd80f4f889a84c547079276f64372..d3131f209e8a75899e299cf635c134bfa5dbdde8 100644 (file)
@@ -976,7 +976,9 @@ fn link_args(cmd: &mut Linker,
 
     // Try to strip as much out of the generated object by removing unused
     // sections if possible. See more comments in linker.rs
-    cmd.gc_sections(dylib);
+    if !sess.opts.cg.link_dead_code {
+        cmd.gc_sections(dylib);
+    }
 
     let used_link_args = sess.cstore.used_link_args();
 
index e439b27a19061d350405e9db2ece0d503640accb..c1f9065e9d2d621d37bbf8574f0b7725da3ec218 100644 (file)
@@ -22,3 +22,10 @@ all:
        $(RUSTC) -C lto=foo dummy.rs 2>&1 | \
                grep 'codegen option `lto` takes no value'
        $(RUSTC) -C lto dummy.rs
+
+       # Should not link dead code...
+       $(RUSTC) -Z print-link-args dummy.rs 2>&1 | \
+               grep -e '--gc-sections\|-dead_strip\|/OPT:REF,ICF'
+       # ... unless you specifically ask to keep it
+       $(RUSTC) -Z print-link-args -C link-dead-code dummy.rs 2>&1 | \
+               (! grep -e '--gc-sections\|-dead_strip\|/OPT:REF,ICF')