authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-07-05 14:42:45+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-07-22 16:58:20+02:00
log7ec9a4f382fb73950e4b4c5f7e005c154f6ec294
tree3f07e8ea27bd98eaa630eb5d3787c771841ddea4
parent44ee42c6bc46b20b1dac1f0b3a44512a8ada9c93

cli: support --gc-sections and --no-gc-sections for Zig sources


2 files changed, 14 insertions(+), 1 deletions(-)

lib/std/build.zig+7
...@@ -1561,6 +1561,10 @@ pub const LibExeObjStep = struct {...@@ -1561,6 +1561,10 @@ pub const LibExeObjStep = struct {
1561 /// safely garbage-collected during the linking phase.1561 /// safely garbage-collected during the linking phase.
1562 link_function_sections: bool = false,1562 link_function_sections: bool = false,
15631563
1564 /// Remove functions and data that are unreachable by the entry point or
1565 /// exported symbols.
1566 link_gc_sections: ?bool = null,
1567
1564 linker_allow_shlib_undefined: ?bool = null,1568 linker_allow_shlib_undefined: ?bool = null,
15651569
1566 /// Permit read-only relocations in read-only segments. Disallowed by default.1570 /// Permit read-only relocations in read-only segments. Disallowed by default.
...@@ -2705,6 +2709,9 @@ pub const LibExeObjStep = struct {...@@ -2705,6 +2709,9 @@ pub const LibExeObjStep = struct {
2705 if (self.link_function_sections) {2709 if (self.link_function_sections) {
2706 try zig_args.append("-ffunction-sections");2710 try zig_args.append("-ffunction-sections");
2707 }2711 }
2712 if (self.link_gc_sections) |x| {
2713 try zig_args.append(if (x) "--gc-sections" else "--no-gc-sections");
2714 }
2708 if (self.linker_allow_shlib_undefined) |x| {2715 if (self.linker_allow_shlib_undefined) |x| {
2709 try zig_args.append(if (x) "-fallow-shlib-undefined" else "-fno-allow-shlib-undefined");2716 try zig_args.append(if (x) "-fallow-shlib-undefined" else "-fno-allow-shlib-undefined");
2710 }2717 }
src/main.zig+7-1
...@@ -446,6 +446,8 @@ const usage_build_generic =...@@ -446,6 +446,8 @@ const usage_build_generic =
446 \\ --compress-debug-sections=[e] Debug section compression settings446 \\ --compress-debug-sections=[e] Debug section compression settings
447 \\ none No compression447 \\ none No compression
448 \\ zlib Compression with deflate/inflate448 \\ zlib Compression with deflate/inflate
449 \\ --gc-sections Force removal of functions and data that are unreachable by the entry point or exported symbols
450 \\ --no-gc-sections Don't force removal of unreachable functions and data
449 \\ --subsystem [subsystem] (Windows) /SUBSYSTEM:<subsystem> to the linker451 \\ --subsystem [subsystem] (Windows) /SUBSYSTEM:<subsystem> to the linker
450 \\ --stack [size] Override default stack size452 \\ --stack [size] Override default stack size
451 \\ --image-base [addr] Set base address for executable image453 \\ --image-base [addr] Set base address for executable image
...@@ -463,7 +465,7 @@ const usage_build_generic =...@@ -463,7 +465,7 @@ const usage_build_generic =
463 \\ -search_dylibs_first (Darwin) search `libx.dylib` in each dir in library search paths, then `libx.a`465 \\ -search_dylibs_first (Darwin) search `libx.dylib` in each dir in library search paths, then `libx.a`
464 \\ -headerpad [value] (Darwin) set minimum space for future expansion of the load commands in hexadecimal notation466 \\ -headerpad [value] (Darwin) set minimum space for future expansion of the load commands in hexadecimal notation
465 \\ -headerpad_max_install_names (Darwin) set enough space as if all paths were MAXPATHLEN467 \\ -headerpad_max_install_names (Darwin) set enough space as if all paths were MAXPATHLEN
466 \\ -dead_strip (Darwin) remove function and data that are unreachable by the entry point of exported symbols468 \\ -dead_strip (Darwin) remove functions and data that are unreachable by the entry point or exported symbols
467 \\ -dead_strip_dylibs (Darwin) remove dylibs that are unreachable by the entry point or exported symbols469 \\ -dead_strip_dylibs (Darwin) remove dylibs that are unreachable by the entry point or exported symbols
468 \\ --import-memory (WebAssembly) import memory from the environment470 \\ --import-memory (WebAssembly) import memory from the environment
469 \\ --import-table (WebAssembly) import function table from the host environment471 \\ --import-table (WebAssembly) import function table from the host environment
...@@ -1314,6 +1316,10 @@ fn buildOutputType(...@@ -1314,6 +1316,10 @@ fn buildOutputType(
1314 try linker_export_symbol_names.append(arg["--export=".len..]);1316 try linker_export_symbol_names.append(arg["--export=".len..]);
1315 } else if (mem.eql(u8, arg, "-Bsymbolic")) {1317 } else if (mem.eql(u8, arg, "-Bsymbolic")) {
1316 linker_bind_global_refs_locally = true;1318 linker_bind_global_refs_locally = true;
1319 } else if (mem.eql(u8, arg, "--gc-sections")) {
1320 linker_gc_sections = true;
1321 } else if (mem.eql(u8, arg, "--no-gc-sections")) {
1322 linker_gc_sections = false;
1317 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {1323 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
1318 debug_compile_errors = true;1324 debug_compile_errors = true;
1319 } else if (mem.eql(u8, arg, "--verbose-link")) {1325 } else if (mem.eql(u8, arg, "--verbose-link")) {