From 274f27a476d48a006f8e7142d4a239f63ea2d403 Mon Sep 17 00:00:00 2001 From: Johan Lorenzo Date: Tue, 2 Feb 2016 17:56:59 +0000 Subject: [PATCH] Add -C link-dead-code option r=alexcrichton 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 | 2 ++ src/librustc_trans/back/link.rs | 4 +++- src/test/run-make/codegen-options-parsing/Makefile | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index f6079217829..2091ebbf505 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -507,6 +507,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options, "system linker to link outputs with"), link_args: Option> = (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 = (None, parse_opt_string, diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs index 0f327d5c84c..d3131f209e8 100644 --- a/src/librustc_trans/back/link.rs +++ b/src/librustc_trans/back/link.rs @@ -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(); diff --git a/src/test/run-make/codegen-options-parsing/Makefile b/src/test/run-make/codegen-options-parsing/Makefile index e439b27a190..c1f9065e9d2 100644 --- a/src/test/run-make/codegen-options-parsing/Makefile +++ b/src/test/run-make/codegen-options-parsing/Makefile @@ -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')