From a996a75e06193d377c3071042ece1c3af5cfdc6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 12 Dec 2025 20:25:34 +0100 Subject: [PATCH] compiler: make all Zig-provided libraries use -ffunction-sections -fdata-sections We already did this for some of them; this just makes us consistent. Doing this gives the linker more flexibility to rearrange code/data, but more importantly, allows --gc-sections to get rid of all the unused code, which is a real concern for these libraries in particular. --- src/Compilation.zig | 8 ++++---- src/libs/libcxx.zig | 4 ++++ src/libs/libtsan.zig | 2 ++ src/libs/libunwind.zig | 3 ++- src/libs/mingw.zig | 1 + src/libs/musl.zig | 8 -------- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 1b17f81c7d..f308a60bd3 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -8084,8 +8084,8 @@ fn buildOutputFromZig( } pub const CrtFileOptions = struct { - function_sections: ?bool = null, - data_sections: ?bool = null, + function_sections: bool = true, + data_sections: bool = true, omit_frame_pointer: ?bool = null, unwind_tables: ?std.builtin.UnwindTables = null, pic: ?bool = null, @@ -8188,8 +8188,8 @@ pub fn build_crt_file( .root_name = root_name, .libc_installation = comp.libc_installation, .emit_bin = .yes_cache, - .function_sections = options.function_sections orelse false, - .data_sections = options.data_sections orelse false, + .function_sections = options.function_sections, + .data_sections = options.data_sections, .c_source_files = c_source_files, .verbose_cc = comp.verbose_cc, .verbose_link = comp.verbose_link, diff --git a/src/libs/libcxx.zig b/src/libs/libcxx.zig index bd8863991d..316ac6d815 100644 --- a/src/libs/libcxx.zig +++ b/src/libs/libcxx.zig @@ -265,6 +265,8 @@ pub fn buildLibCxx(comp: *Compilation, prog_node: std.Progress.Node) BuildError! .root_name = root_name, .libc_installation = comp.libc_installation, .emit_bin = .yes_cache, + .function_sections = true, + .data_sections = true, .c_source_files = c_source_files.items, .verbose_cc = comp.verbose_cc, .verbose_link = comp.verbose_link, @@ -459,6 +461,8 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr .root_name = root_name, .libc_installation = comp.libc_installation, .emit_bin = .yes_cache, + .function_sections = true, + .data_sections = true, .c_source_files = c_source_files.items, .verbose_cc = comp.verbose_cc, .verbose_link = comp.verbose_link, diff --git a/src/libs/libtsan.zig b/src/libs/libtsan.zig index 2f9574c473..c9dcf60953 100644 --- a/src/libs/libtsan.zig +++ b/src/libs/libtsan.zig @@ -288,6 +288,8 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo .root_name = root_name, .libc_installation = comp.libc_installation, .emit_bin = .yes_cache, + .function_sections = true, + .data_sections = true, .c_source_files = c_source_files.items, .verbose_cc = comp.verbose_cc, .verbose_link = comp.verbose_link, diff --git a/src/libs/libunwind.zig b/src/libs/libunwind.zig index 787a87f951..c1437d39bd 100644 --- a/src/libs/libunwind.zig +++ b/src/libs/libunwind.zig @@ -155,7 +155,8 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr .main_mod = null, .libc_installation = comp.libc_installation, .emit_bin = .yes_cache, - .function_sections = comp.function_sections, + .function_sections = true, + .data_sections = true, .c_source_files = &c_source_files, .verbose_cc = comp.verbose_cc, .verbose_link = comp.verbose_link, diff --git a/src/libs/mingw.zig b/src/libs/mingw.zig index 5c57f6d53e..da67f47661 100644 --- a/src/libs/mingw.zig +++ b/src/libs/mingw.zig @@ -56,6 +56,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre }, }; return comp.build_crt_file("crt2", .Obj, .@"mingw-w64 crt2.o", prog_node, &files, .{ + .function_sections = false, // https://codeberg.org/ziglang/zig/issues/30702 .unwind_tables = unwind_tables, }); }, diff --git a/src/libs/musl.zig b/src/libs/musl.zig index f198338195..744f1f28d2 100644 --- a/src/libs/musl.zig +++ b/src/libs/musl.zig @@ -43,8 +43,6 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro }, }; return comp.build_crt_file("crt1", .Obj, .@"musl crt1.o", prog_node, &files, .{ - .function_sections = true, - .data_sections = true, .omit_frame_pointer = true, .no_builtin = true, }); @@ -63,8 +61,6 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro }, }; return comp.build_crt_file("rcrt1", .Obj, .@"musl rcrt1.o", prog_node, &files, .{ - .function_sections = true, - .data_sections = true, .omit_frame_pointer = true, .pic = true, .no_builtin = true, @@ -84,8 +80,6 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro }, }; return comp.build_crt_file("Scrt1", .Obj, .@"musl Scrt1.o", prog_node, &files, .{ - .function_sections = true, - .data_sections = true, .omit_frame_pointer = true, .pic = true, .no_builtin = true, @@ -172,8 +166,6 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro }; } return comp.build_crt_file("c", .Lib, .@"musl libc.a", prog_node, c_source_files.items, .{ - .function_sections = true, - .data_sections = true, .omit_frame_pointer = true, .no_builtin = true, });