Add -C link-dead-code option r=alexcrichton · rust-lang/rust@274f27a (original) (raw)

File tree

3 files changed

lines changed

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -507,6 +507,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
507 507 "system linker to link outputs with"),
508 508 link_args: Option<Vec<String>> = (None, parse_opt_list,
509 509 "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)"),
510 512 lto: bool = (false, parse_bool,
511 513 "perform LLVM link-time optimizations"),
512 514 target_cpu: Option<String> = (None, parse_opt_string,
Original file line number Diff line number Diff line change
@@ -976,7 +976,9 @@ fn link_args(cmd: &mut Linker,
976 976
977 977 // Try to strip as much out of the generated object by removing unused
978 978 // 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 +}
980 982
981 983 let used_link_args = sess.cstore.used_link_args();
982 984
Original file line number Diff line number Diff line change
@@ -22,3 +22,10 @@ all:
22 22 $(RUSTC) -C lto=foo dummy.rs 2>&1 | \
23 23 grep 'codegen option `lto` takes no value'
24 24 $(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\
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\