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 {
15611561 /// safely garbage-collected during the linking phase.
15621562 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
15641568 linker_allow_shlib_undefined: ?bool = null,
15651569
15661570 /// Permit read-only relocations in read-only segments. Disallowed by default.
......@@ -2705,6 +2709,9 @@ pub const LibExeObjStep = struct {
27052709 if (self.link_function_sections) {
27062710 try zig_args.append("-ffunction-sections");
27072711 }
2712 if (self.link_gc_sections) |x| {
2713 try zig_args.append(if (x) "--gc-sections" else "--no-gc-sections");
2714 }
27082715 if (self.linker_allow_shlib_undefined) |x| {
27092716 try zig_args.append(if (x) "-fallow-shlib-undefined" else "-fno-allow-shlib-undefined");
27102717 }
src/main.zig+7-1
......@@ -446,6 +446,8 @@ const usage_build_generic =
446446 \\ --compress-debug-sections=[e] Debug section compression settings
447447 \\ none No compression
448448 \\ 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
449451 \\ --subsystem [subsystem] (Windows) /SUBSYSTEM:<subsystem> to the linker
450452 \\ --stack [size] Override default stack size
451453 \\ --image-base [addr] Set base address for executable image
......@@ -463,7 +465,7 @@ const usage_build_generic =
463465 \\ -search_dylibs_first (Darwin) search `libx.dylib` in each dir in library search paths, then `libx.a`
464466 \\ -headerpad [value] (Darwin) set minimum space for future expansion of the load commands in hexadecimal notation
465467 \\ -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 symbols
468 \\ -dead_strip (Darwin) remove functions and data that are unreachable by the entry point or exported symbols
467469 \\ -dead_strip_dylibs (Darwin) remove dylibs that are unreachable by the entry point or exported symbols
468470 \\ --import-memory (WebAssembly) import memory from the environment
469471 \\ --import-table (WebAssembly) import function table from the host environment
......@@ -1314,6 +1316,10 @@ fn buildOutputType(
13141316 try linker_export_symbol_names.append(arg["--export=".len..]);
13151317 } else if (mem.eql(u8, arg, "-Bsymbolic")) {
13161318 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;
13171323 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
13181324 debug_compile_errors = true;
13191325 } else if (mem.eql(u8, arg, "--verbose-link")) {