Skip to content

Commit 274f27a

Browse files
committed
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.
1 parent b9732ed commit 274f27a

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
507507
"system linker to link outputs with"),
508508
link_args: Option<Vec<String>> = (None, parse_opt_list,
509509
"extra arguments to pass to the linker (space separated)"),
510+
link_dead_code: bool = (false, parse_bool,
511+
"let the linker strip dead coded (turning it on can be used for code coverage)"),
510512
lto: bool = (false, parse_bool,
511513
"perform LLVM link-time optimizations"),
512514
target_cpu: Option<String> = (None, parse_opt_string,

src/librustc_trans/back/link.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,9 @@ fn link_args(cmd: &mut Linker,
976976

977977
// Try to strip as much out of the generated object by removing unused
978978
// sections if possible. See more comments in linker.rs
979-
cmd.gc_sections(dylib);
979+
if !sess.opts.cg.link_dead_code {
980+
cmd.gc_sections(dylib);
981+
}
980982

981983
let used_link_args = sess.cstore.used_link_args();
982984

src/test/run-make/codegen-options-parsing/Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ all:
2222
$(RUSTC) -C lto=foo dummy.rs 2>&1 | \
2323
grep 'codegen option `lto` takes no value'
2424
$(RUSTC) -C lto dummy.rs
25+
26+
# Should not link dead code...
27+
$(RUSTC) -Z print-link-args dummy.rs 2>&1 | \
28+
grep -e '--gc-sections\|-dead_strip\|/OPT:REF,ICF'
29+
# ... unless you specifically ask to keep it
30+
$(RUSTC) -Z print-link-args -C link-dead-code dummy.rs 2>&1 | \
31+
(! grep -e '--gc-sections\|-dead_strip\|/OPT:REF,ICF')

0 commit comments

Comments
 (0)