about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbjorn3 <bjorn3@users.noreply.github.com>2022-01-25 18:16:17 +0100
committerbjorn3 <bjorn3@users.noreply.github.com>2022-01-26 15:13:55 +0100
commit99941cd9d26c3ef2d53ecdb4f7563367d66ca3c1 (patch)
treefc6e9bfa178720283fa27aa8d18d664171d2cf98
parent5dc660b10647823842dfe3d8865b4802c7ab90f9 (diff)
downloadrust-99941cd9d26c3ef2d53ecdb4f7563367d66ca3c1.tar.gz
rust-99941cd9d26c3ef2d53ecdb4f7563367d66ca3c1.zip
Support -Zfunction-sections
This puts every function and data object in their own section. This
allows the linker to omit unused functions and data objects with
--gc-sections.

On linux this shrinks a hello world binary without optimizations
(neither sysroot nor binary) from 17MB to 13MB. It shrinks a hello world
binary with only sysroot optimizations from 14MB to 13MB. For comparison
cg_llvm produces a 3.5MB debug mode hello world binary with an optimized
sysroot. Cg_clif produces a 10MB debug mode hello world binary without
an optimized sysroot.
-rw-r--r--src/base.rs6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/base.rs b/src/base.rs
index 8b23e96066e..45e791a99d6 100644
--- a/src/base.rs
+++ b/src/base.rs
@@ -85,6 +85,12 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (Modul
         context.add_command_line_option("-fno-semantic-interposition");
         // NOTE: Rust relies on LLVM not doing TBAA (https://github.com/rust-lang/unsafe-code-guidelines/issues/292).
         context.add_command_line_option("-fno-strict-aliasing");
+
+        if tcx.sess.opts.debugging_opts.function_sections.unwrap_or(tcx.sess.target.function_sections) {
+            context.add_command_line_option("-ffunction-sections");
+            context.add_command_line_option("-fdata-sections");
+        }
+
         if env::var("CG_GCCJIT_DUMP_CODE").as_deref() == Ok("1") {
             context.set_dump_code_on_compile(true);
         }