From 9092424fc3eef6bb1dcb580b2ee22110d6ba52ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Thu, 22 Oct 2026 10:38:43 +0200 Subject: [PATCH] make -ffunction-sections and -fdata-sections resolved config options In preparation for giving them defaults that depend on the target and backend. --- build.zig | 8 ++++---- lib/compiler/Maker/Step/Compile.zig | 4 ++-- lib/std/Build/Configuration.zig | 7 ++++--- lib/std/Build/Serialize.zig | 4 ++-- lib/std/Build/Step/Compile.zig | 6 +++--- src/Compilation.zig | 22 ++++++++-------------- src/Compilation/Config.zig | 16 ++++++++++++++++ src/codegen/llvm.zig | 4 ++-- src/libs/libcxx.zig | 8 ++++---- src/libs/libtsan.zig | 4 ++-- src/libs/libunwind.zig | 4 ++-- src/main.zig | 20 ++++++++------------ test/tests.zig | 4 ++-- 13 files changed, 59 insertions(+), 52 deletions(-) diff --git a/build.zig b/build.zig index ffc7b37d58..7bec015a8e 100644 --- a/build.zig +++ b/build.zig @@ -931,7 +931,7 @@ fn addCompilerStep(b: *std.Build, options: AddCompilerModOptions) *std.Build.Ste exe.stack_size = stack_size; // Must match the condition in CMakeLists.txt. - const function_data_sections = switch (options.target.result.cpu.arch) { + const function_data_sections: ?bool = switch (options.target.result.cpu.arch) { .arm, .armeb, .thumb, @@ -942,11 +942,11 @@ fn addCompilerStep(b: *std.Build, options: AddCompilerModOptions) *std.Build.Ste .powerpc64, .powerpc64le, => true, - else => false, + else => null, }; - exe.link_function_sections = function_data_sections; - exe.link_data_sections = function_data_sections; + exe.function_sections = function_data_sections; + exe.data_sections = function_data_sections; return exe; } diff --git a/lib/compiler/Maker/Step/Compile.zig b/lib/compiler/Maker/Step/Compile.zig index 4fbe6e4679..8afb5ec881 100644 --- a/lib/compiler/Maker/Step/Compile.zig +++ b/lib/compiler/Maker/Step/Compile.zig @@ -630,8 +630,8 @@ fn lowerZigArgs( try addBool(gpa, zig_args, "--eh-frame-hdr", conf_comp.flags.link_eh_frame_hdr); try addBool(gpa, zig_args, "--emit-relocs", conf_comp.flags.link_emit_relocs); - try addBool(gpa, zig_args, "-ffunction-sections", conf_comp.flags.link_function_sections); - try addBool(gpa, zig_args, "-fdata-sections", conf_comp.flags.link_data_sections); + try addFlag(gpa, zig_args, "function-sections", conf_comp.flags4.function_sections.toBool()); + try addFlag(gpa, zig_args, "data-sections", conf_comp.flags4.data_sections.toBool()); if (conf_comp.flags2.link_gc_sections.toBool()) |x| try zig_args.append(gpa, if (x) "--gc-sections" else "--no-gc-sections"); diff --git a/lib/std/Build/Configuration.zig b/lib/std/Build/Configuration.zig index bb71c44ad6..48f7d764be 100644 --- a/lib/std/Build/Configuration.zig +++ b/lib/std/Build/Configuration.zig @@ -1000,8 +1000,6 @@ pub const Step = extern struct { shared_memory: bool, link_eh_frame_hdr: bool, link_emit_relocs: bool, - link_function_sections: bool, - link_data_sections: bool, linker_dynamicbase: bool, link_z_notext: bool, link_z_relro: bool, @@ -1012,6 +1010,7 @@ pub const Step = extern struct { force_load_objc: bool, discard_local_symbols: bool, mingw_unicode_entry_point: bool, + _: u2 = 0, }; pub const Flags2 = packed struct(u32) { @@ -1077,7 +1076,9 @@ pub const Step = extern struct { generated_llvm_ir: bool, generated_h: bool, incremental: DefaultingBool, - _: u7 = 0, + function_sections: DefaultingBool, + data_sections: DefaultingBool, + _: u3 = 0, }; pub fn isDynamicLibrary(compile: *const Compile) bool { diff --git a/lib/std/Build/Serialize.zig b/lib/std/Build/Serialize.zig index 3cf0ca9466..3a28c7caa5 100644 --- a/lib/std/Build/Serialize.zig +++ b/lib/std/Build/Serialize.zig @@ -132,8 +132,6 @@ pub fn write(b: *std.Build, wc: *Configuration.Wip, writer: *std.Io.Writer) !voi .shared_memory = c.shared_memory, .link_eh_frame_hdr = c.link_eh_frame_hdr, .link_emit_relocs = c.link_emit_relocs, - .link_function_sections = c.link_function_sections, - .link_data_sections = c.link_data_sections, .linker_dynamicbase = c.linker_dynamicbase, .link_z_notext = c.link_z_notext, .link_z_relro = c.link_z_relro, @@ -218,6 +216,8 @@ pub fn write(b: *std.Build, wc: *Configuration.Wip, writer: *std.Io.Writer) !voi .generated_llvm_ir = c.generated_llvm_ir != .none, .generated_h = c.generated_h != .none, .incremental = .init(c.incremental), + .function_sections = .init(c.function_sections), + .data_sections = .init(c.data_sections), }, .root_module = try s.addModule(c.root_module), .root_name = try wc.addString(c.name), diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 6d1c1ec4c4..12702dc393 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -109,11 +109,11 @@ link_emit_relocs: bool = false, /// Place every function in its own section so that unused ones may be /// safely garbage-collected during the linking phase. -link_function_sections: bool = false, +function_sections: ?bool = null, -/// Place every data in its own section so that unused ones may be +/// Place every datum in its own section so that unused ones may be /// safely garbage-collected during the linking phase. -link_data_sections: bool = false, +data_sections: ?bool = null, /// Remove functions and data that are unreachable by the entry point or /// exported symbols. diff --git a/src/Compilation.zig b/src/Compilation.zig index 913d1eeda6..f14c6e8e89 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -92,8 +92,6 @@ windows_libs_num_done: u32, version: ?std.SemanticVersion, libc_installation: ?*const LibCInstallation, skip_linker_dependencies: bool, -function_sections: bool, -data_sections: bool, link_eh_frame_hdr: bool, native_system_include_paths: []const []const u8, /// List of symbols forced as undefined in the symbol table @@ -1486,8 +1484,6 @@ pub const CreateOptions = struct { /// executable this field is ignored. want_compiler_rt: ?bool = null, want_ubsan_rt: ?bool = null, - function_sections: bool = false, - data_sections: bool = false, time_report: bool = false, stack_report: bool = false, link_eh_frame_hdr: bool = false, @@ -1945,8 +1941,8 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic, cache.hash.add(options.config.any_sanitize_thread); cache.hash.add(options.config.any_sanitize_c); cache.hash.add(options.config.any_fuzz); - cache.hash.add(options.function_sections); - cache.hash.add(options.data_sections); + cache.hash.add(options.config.function_sections); + cache.hash.add(options.config.data_sections); cache.hash.add(link_libc); cache.hash.add(options.config.link_libcpp); cache.hash.add(options.config.link_libunwind); @@ -2164,8 +2160,6 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic, .llvm_opt_bisect_limit = options.llvm_opt_bisect_limit, .skip_linker_dependencies = options.skip_linker_dependencies, .queued_jobs = .{}, - .function_sections = options.function_sections, - .data_sections = options.data_sections, .native_system_include_paths = options.native_system_include_paths, .force_undefined_symbols = options.force_undefined_symbols, .link_eh_frame_hdr = link_eh_frame_hdr, @@ -6727,8 +6721,8 @@ pub fn addCCArgs( try argv.append(if (mod.no_builtin) "-fno-builtin" else "-fbuiltin"); - try argv.append(if (comp.function_sections) "-ffunction-sections" else "-fno-function-sections"); - try argv.append(if (comp.data_sections) "-fdata-sections" else "-fno-data-sections"); + try argv.append(if (comp.config.function_sections) "-ffunction-sections" else "-fno-function-sections"); + try argv.append(if (comp.config.data_sections) "-fdata-sections" else "-fno-data-sections"); switch (mod.unwind_tables) { .none => { @@ -7350,6 +7344,8 @@ fn buildOutputFromZig( .any_error_tracing = false, .root_error_tracing = false, .lto = if (options.allow_lto) comp.config.lto else .none, + .function_sections = true, + .data_sections = true, }) catch |err| { comp.lockAndSetMiscFailure(misc_task_tag, "sub-compilation of {t} failed: failed to resolve compilation config: {t}", .{ misc_task_tag, err }); return error.AlreadyReported; @@ -7411,8 +7407,6 @@ fn buildOutputFromZig( .root_name = root_name, .libc_installation = comp.libc_installation, .emit_bin = .yes_cache, - .function_sections = true, - .data_sections = true, .verbose_cc = comp.verbose_cc, .verbose_link = comp.verbose_link, .clang_passthrough_mode = comp.clang_passthrough_mode, @@ -7490,6 +7484,8 @@ pub fn build_crt_file( .Lib => if (options.allow_lto) comp.config.lto else .none, .Obj, .Exe => .none, }, + .function_sections = options.function_sections, + .data_sections = options.data_sections, }) catch |err| { comp.lockAndSetMiscFailure(misc_task_tag, "sub-compilation of {t} failed: failed to resolve compilation config: {t}", .{ misc_task_tag, err }); return error.AlreadyReported; @@ -7541,8 +7537,6 @@ pub fn build_crt_file( .root_name = root_name, .libc_installation = comp.libc_installation, .emit_bin = .yes_cache, - .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/Compilation/Config.zig b/src/Compilation/Config.zig index 0294268834..0e63f60d4a 100644 --- a/src/Compilation/Config.zig +++ b/src/Compilation/Config.zig @@ -65,6 +65,8 @@ root_error_tracing: bool, dll_export_fns: bool, rdynamic: bool, san_cov_trace_pc_guard: bool, +function_sections: bool, +data_sections: bool, pub const CFrontend = enum { clang, aro }; @@ -119,6 +121,8 @@ pub const Options = struct { dll_export_fns: ?bool = null, rdynamic: ?bool = null, san_cov_trace_pc_guard: bool = false, + function_sections: ?bool = null, + data_sections: ?bool = null, }; pub const ResolveError = error{ @@ -556,6 +560,16 @@ pub fn resolve(options: Options) ResolveError!Config { }; }; + const function_sections = b: { + if (options.function_sections) |x| break :b x; + break :b false; + }; + + const data_sections = b: { + if (options.data_sections) |x| break :b x; + break :b false; + }; + return .{ .output_mode = options.output_mode, .have_zcu = options.have_zcu, @@ -590,6 +604,8 @@ pub fn resolve(options: Options) ResolveError!Config { .root_strip = root_strip, .dll_export_fns = dll_export_fns, .rdynamic = rdynamic, + .function_sections = function_sections, + .data_sections = data_sections, }; } diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 61c5eebf99..7d0cab07ac 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -623,8 +623,8 @@ pub const Object = struct { opt_level, reloc_mode, code_model, - comp.function_sections, - comp.data_sections, + comp.config.function_sections, + comp.config.data_sections, switch (target.abi.float()) { .hard => .Hard, .soft => .Soft, diff --git a/src/libs/libcxx.zig b/src/libs/libcxx.zig index 376887fd9e..4fd360c2bc 100644 --- a/src/libs/libcxx.zig +++ b/src/libs/libcxx.zig @@ -147,6 +147,8 @@ pub fn buildLibCxx(comp: *Compilation, prog_node: std.Progress.Node) BuildError! .link_libc = true, .lto = comp.config.lto, .any_sanitize_thread = comp.config.any_sanitize_thread, + .function_sections = true, + .data_sections = true, }) catch |err| { comp.lockAndSetMiscFailure( .libcxx, @@ -255,8 +257,6 @@ 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, @@ -328,6 +328,8 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr .any_unwind_tables = unwind_tables != .none, .lto = comp.config.lto, .any_sanitize_thread = comp.config.any_sanitize_thread, + .function_sections = true, + .data_sections = true, }) catch |err| { comp.lockAndSetMiscFailure( .libcxxabi, @@ -436,8 +438,6 @@ 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 bc8cacd84c..2201a28649 100644 --- a/src/libs/libtsan.zig +++ b/src/libs/libtsan.zig @@ -69,6 +69,8 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo .any_unwind_tables = unwind_tables != .none, // LLVM disables LTO for its libtsan. .lto = .none, + .function_sections = true, + .data_sections = true, }) catch |err| { comp.lockAndSetMiscFailure( .libtsan, @@ -290,8 +292,6 @@ 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 d8d3456d15..76bbbc47c1 100644 --- a/src/libs/libunwind.zig +++ b/src/libs/libunwind.zig @@ -42,6 +42,8 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr .link_libc = true, .any_unwind_tables = unwind_tables != .none, .lto = comp.config.lto, + .function_sections = true, + .data_sections = true, }) catch |err| { comp.lockAndSetMiscFailure( .libunwind, @@ -152,8 +154,6 @@ 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 = true, - .data_sections = true, .c_source_files = &c_source_files, .verbose_cc = comp.verbose_cc, .verbose_link = comp.verbose_link, diff --git a/src/main.zig b/src/main.zig index 8e044e0751..7df79f83f2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -921,8 +921,6 @@ fn buildOutputType( var version: std.SemanticVersion = .{ .major = 0, .minor = 0, .patch = 0 }; var have_version = false; var compatibility_version: ?std.SemanticVersion = null; - var function_sections = false; - var data_sections = false; var listen: Listen = .none; var debug_compile_errors = false; var debug_incremental = false; @@ -1684,13 +1682,13 @@ fn buildOutputType( } else if (mem.eql(u8, arg, "-fno-single-threaded")) { mod_opts.single_threaded = false; } else if (mem.eql(u8, arg, "-ffunction-sections")) { - function_sections = true; + create_module.opts.function_sections = true; } else if (mem.eql(u8, arg, "-fno-function-sections")) { - function_sections = false; + create_module.opts.function_sections = false; } else if (mem.eql(u8, arg, "-fdata-sections")) { - data_sections = true; + create_module.opts.data_sections = true; } else if (mem.eql(u8, arg, "-fno-data-sections")) { - data_sections = false; + create_module.opts.data_sections = false; } else if (mem.eql(u8, arg, "-fbuiltin")) { mod_opts.no_builtin = false; } else if (mem.eql(u8, arg, "-fno-builtin")) { @@ -2137,10 +2135,10 @@ fn buildOutputType( .no_red_zone => mod_opts.red_zone = false, .omit_frame_pointer => mod_opts.omit_frame_pointer = true, .no_omit_frame_pointer => mod_opts.omit_frame_pointer = false, - .function_sections => function_sections = true, - .no_function_sections => function_sections = false, - .data_sections => data_sections = true, - .no_data_sections => data_sections = false, + .function_sections => create_module.opts.function_sections = true, + .no_function_sections => create_module.opts.function_sections = false, + .data_sections => create_module.opts.data_sections = true, + .no_data_sections => create_module.opts.data_sections = false, .builtin => mod_opts.no_builtin = false, .no_builtin => mod_opts.no_builtin = true, .color_diagnostics => color = .on, @@ -3722,8 +3720,6 @@ fn buildOutputType( .force_undefined_symbols = force_undefined_symbols, .stack_size = stack_size, .image_base = image_base, - .function_sections = function_sections, - .data_sections = data_sections, .clang_passthrough_mode = clang_passthrough_mode, .clang_preprocessor_mode = clang_preprocessor_mode, .version = optional_version, diff --git a/test/tests.zig b/test/tests.zig index d966928ae3..8f27734071 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -2696,8 +2696,8 @@ fn addOneModuleTest( if (options.build_options) |build_options| { these_tests.root_module.addOptions("build_options", build_options); } - if (test_target.function_sections) |fs| these_tests.link_function_sections = fs; - if (test_target.data_sections) |ds| these_tests.link_data_sections = ds; + if (test_target.function_sections) |fs| these_tests.function_sections = fs; + if (test_target.data_sections) |ds| these_tests.data_sections = ds; const single_threaded_suffix = if (test_target.single_threaded == true) "-single" else ""; const backend_suffix = if (test_target.use_llvm == true) "-llvm"