Commit 7ec9a4f382

Jakub Konka <kubkon@jakubkonka.com>
2022-07-05 14:42:45
cli: support --gc-sections and --no-gc-sections for Zig sources
1 parent 44ee42c
Changed files (2)
lib
src
lib/std/build.zig
@@ -1561,6 +1561,10 @@ pub const LibExeObjStep = struct {
     /// safely garbage-collected during the linking phase.
     link_function_sections: bool = false,
 
+    /// Remove functions and data that are unreachable by the entry point or
+    /// exported symbols.
+    link_gc_sections: ?bool = null,
+
     linker_allow_shlib_undefined: ?bool = null,
 
     /// Permit read-only relocations in read-only segments. Disallowed by default.
@@ -2705,6 +2709,9 @@ pub const LibExeObjStep = struct {
         if (self.link_function_sections) {
             try zig_args.append("-ffunction-sections");
         }
+        if (self.link_gc_sections) |x| {
+            try zig_args.append(if (x) "--gc-sections" else "--no-gc-sections");
+        }
         if (self.linker_allow_shlib_undefined) |x| {
             try zig_args.append(if (x) "-fallow-shlib-undefined" else "-fno-allow-shlib-undefined");
         }
src/main.zig
@@ -446,6 +446,8 @@ const usage_build_generic =
     \\  --compress-debug-sections=[e]  Debug section compression settings
     \\      none                       No compression
     \\      zlib                       Compression with deflate/inflate
+    \\  --gc-sections                  Force removal of functions and data that are unreachable by the entry point or exported symbols
+    \\  --no-gc-sections               Don't force removal of unreachable functions and data
     \\  --subsystem [subsystem]        (Windows) /SUBSYSTEM:<subsystem> to the linker
     \\  --stack [size]                 Override default stack size
     \\  --image-base [addr]            Set base address for executable image
@@ -463,7 +465,7 @@ const usage_build_generic =
     \\  -search_dylibs_first           (Darwin) search `libx.dylib` in each dir in library search paths, then `libx.a`
     \\  -headerpad [value]             (Darwin) set minimum space for future expansion of the load commands in hexadecimal notation
     \\  -headerpad_max_install_names   (Darwin) set enough space as if all paths were MAXPATHLEN
-    \\  -dead_strip                    (Darwin) remove function and data that are unreachable by the entry point of exported symbols
+    \\  -dead_strip                    (Darwin) remove functions and data that are unreachable by the entry point or exported symbols
     \\  -dead_strip_dylibs             (Darwin) remove dylibs that are unreachable by the entry point or exported symbols
     \\  --import-memory                (WebAssembly) import memory from the environment
     \\  --import-table                 (WebAssembly) import function table from the host environment
@@ -1314,6 +1316,10 @@ fn buildOutputType(
                         try linker_export_symbol_names.append(arg["--export=".len..]);
                     } else if (mem.eql(u8, arg, "-Bsymbolic")) {
                         linker_bind_global_refs_locally = true;
+                    } else if (mem.eql(u8, arg, "--gc-sections")) {
+                        linker_gc_sections = true;
+                    } else if (mem.eql(u8, arg, "--no-gc-sections")) {
+                        linker_gc_sections = false;
                     } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
                         debug_compile_errors = true;
                     } else if (mem.eql(u8, arg, "--verbose-link")) {