authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-01-23 01:38:20+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-23 23:22:38+00:00
logef4d7f01a5c26d8ed6fa8236d1b64e4091a51be3
tree80da7b2ae3a66a5e394d31ccd62421529a14929b
parentb46a40ff1db6c4d467925a9a0d72436cdb3a6a74
signaturelock-open Commit is signed but in an unrecognized format.

compiler: Fix computation of Compilation.Config.any_unwind_tables.

This moves the default value logic to Package.Module.create() instead and makes it so that Compilation.Config.any_unwind_tables is computed similarly to any_sanitize_thread, any_fuzz, etc. It turns out that for any_unwind_tables, we only actually care if unwind tables are enabled at all, not at what level.

7 files changed, 36 insertions(+), 43 deletions(-)

src/Compilation.zig+4-5
......@@ -1274,15 +1274,12 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
12741274 // The "any" values provided by resolved config only account for
12751275 // explicitly-provided settings. We now make them additionally account
12761276 // for default setting resolution.
1277 const any_unwind_tables = switch (options.config.any_unwind_tables) {
1278 .none => options.root_mod.unwind_tables,
1279 .sync, .@"async" => |uwt| uwt,
1280 };
1277 const any_unwind_tables = options.config.any_unwind_tables or options.root_mod.unwind_tables != .none;
12811278 const any_non_single_threaded = options.config.any_non_single_threaded or !options.root_mod.single_threaded;
12821279 const any_sanitize_thread = options.config.any_sanitize_thread or options.root_mod.sanitize_thread;
12831280 const any_fuzz = options.config.any_fuzz or options.root_mod.fuzz;
12841281
1285 const link_eh_frame_hdr = options.link_eh_frame_hdr or any_unwind_tables != .none;
1282 const link_eh_frame_hdr = options.link_eh_frame_hdr or any_unwind_tables;
12861283 const build_id = options.build_id orelse .none;
12871284
12881285 const link_libc = options.config.link_libc;
......@@ -6459,6 +6456,7 @@ fn buildOutputFromZig(
64596456 .root_optimize_mode = optimize_mode,
64606457 .root_strip = strip,
64616458 .link_libc = comp.config.link_libc,
6459 .any_unwind_tables = comp.root_mod.unwind_tables != .none,
64626460 });
64636461
64646462 const root_mod = try Package.Module.create(arena, .{
......@@ -6595,6 +6593,7 @@ pub fn build_crt_file(
65956593 .root_optimize_mode = comp.compilerRtOptMode(),
65966594 .root_strip = comp.compilerRtStrip(),
65976595 .link_libc = false,
6596 .any_unwind_tables = options.unwind_tables != .none,
65986597 .lto = switch (output_mode) {
65996598 .Lib => comp.config.lto,
66006599 .Obj, .Exe => .none,
src/Compilation/Config.zig+6-13
......@@ -12,14 +12,13 @@ link_libunwind: bool,
1212/// True if and only if the c_source_files field will have nonzero length when
1313/// calling Compilation.create.
1414any_c_source_files: bool,
15/// This is not `.none` if any `Module` has `unwind_tables` set explicitly to a
15/// This is `true` if any `Module` has `unwind_tables` set explicitly to a
1616/// value other than `.none`. Until `Compilation.create()` is called, it is
17/// possible for this to be `.none` while in fact all `Module` instances have
17/// possible for this to be `false` while in fact all `Module` instances have
1818/// `unwind_tables != .none` due to the default. After `Compilation.create()` is
1919/// called, this will also take into account the default setting, making this
20/// value `.sync` or `.@"async"` if and only if any `Module` has
21/// `unwind_tables != .none`.
22any_unwind_tables: std.builtin.UnwindTables,
20/// value `true` if and only if any `Module` has `unwind_tables != .none`.
21any_unwind_tables: bool,
2322/// This is true if any Module has single_threaded set explicitly to false. Until
2423/// Compilation.create is called, it is possible for this to be false while in
2524/// fact all Module instances have single_threaded=false due to the default
......@@ -88,7 +87,7 @@ pub const Options = struct {
8887 any_non_single_threaded: bool = false,
8988 any_sanitize_thread: bool = false,
9089 any_fuzz: bool = false,
91 any_unwind_tables: std.builtin.UnwindTables = .none,
90 any_unwind_tables: bool = false,
9291 any_dyn_libs: bool = false,
9392 any_c_source_files: bool = false,
9493 any_non_stripped: bool = false,
......@@ -359,12 +358,6 @@ pub fn resolve(options: Options) ResolveError!Config {
359358 break :b false;
360359 };
361360
362 const any_unwind_tables = b: {
363 if (options.any_unwind_tables != .none) break :b options.any_unwind_tables;
364
365 break :b target_util.needUnwindTables(target, link_libunwind, options.any_sanitize_thread);
366 };
367
368361 const link_mode = b: {
369362 const explicitly_exe_or_dyn_lib = switch (options.output_mode) {
370363 .Obj => false,
......@@ -496,7 +489,7 @@ pub fn resolve(options: Options) ResolveError!Config {
496489 .link_libc = link_libc,
497490 .link_libcpp = link_libcpp,
498491 .link_libunwind = link_libunwind,
499 .any_unwind_tables = any_unwind_tables,
492 .any_unwind_tables = options.any_unwind_tables,
500493 .any_c_source_files = options.any_c_source_files,
501494 .any_non_single_threaded = options.any_non_single_threaded,
502495 .any_error_tracing = any_error_tracing,
src/Package/Module.zig+12-4
......@@ -112,7 +112,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
112112 if (options.inherited.sanitize_thread == true) assert(options.global.any_sanitize_thread);
113113 if (options.inherited.fuzz == true) assert(options.global.any_fuzz);
114114 if (options.inherited.single_threaded == false) assert(options.global.any_non_single_threaded);
115 if (options.inherited.unwind_tables) |uwt| if (uwt != .none) assert(options.global.any_unwind_tables != .none);
115 if (options.inherited.unwind_tables) |uwt| if (uwt != .none) assert(options.global.any_unwind_tables);
116116 if (options.inherited.error_tracing == true) assert(options.global.any_error_tracing);
117117
118118 const resolved_target = options.inherited.resolved_target orelse options.parent.?.resolved_target;
......@@ -121,9 +121,6 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
121121 const optimize_mode = options.inherited.optimize_mode orelse
122122 if (options.parent) |p| p.optimize_mode else .Debug;
123123
124 const unwind_tables = options.inherited.unwind_tables orelse
125 if (options.parent) |p| p.unwind_tables else options.global.any_unwind_tables;
126
127124 const strip = b: {
128125 if (options.inherited.strip) |x| break :b x;
129126 if (options.parent) |p| break :b p.strip;
......@@ -220,6 +217,17 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
220217 break :b false;
221218 };
222219
220 const unwind_tables = b: {
221 if (options.inherited.unwind_tables) |x| break :b x;
222 if (options.parent) |p| break :b p.unwind_tables;
223
224 break :b target_util.defaultUnwindTables(
225 target,
226 options.global.link_libunwind,
227 sanitize_thread or options.global.any_sanitize_thread,
228 );
229 };
230
223231 const fuzz = b: {
224232 if (options.inherited.fuzz) |x| break :b x;
225233 if (options.parent) |p| break :b p.fuzz;
src/libcxx.zig+6-6
......@@ -397,6 +397,10 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
397397
398398 const optimize_mode = comp.compilerRtOptMode();
399399 const strip = comp.compilerRtStrip();
400 // See the `-fno-exceptions` logic for WASI.
401 // The old 32-bit x86 variant of SEH doesn't use tables.
402 const unwind_tables: std.builtin.UnwindTables =
403 if (target.os.tag == .wasi or (target.cpu.arch == .x86 and target.os.tag == .windows)) .none else .@"async";
400404
401405 const config = Compilation.Config.resolve(.{
402406 .output_mode = output_mode,
......@@ -408,6 +412,7 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
408412 .root_optimize_mode = optimize_mode,
409413 .root_strip = strip,
410414 .link_libc = true,
415 .any_unwind_tables = unwind_tables != .none,
411416 .lto = comp.config.lto,
412417 .any_sanitize_thread = comp.config.any_sanitize_thread,
413418 }) catch |err| {
......@@ -438,12 +443,7 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
438443 .valgrind = false,
439444 .optimize_mode = optimize_mode,
440445 .structured_cfg = comp.root_mod.structured_cfg,
441 // See the `-fno-exceptions` logic for WASI.
442 // The old 32-bit x86 variant of SEH doesn't use tables.
443 .unwind_tables = if (target.os.tag == .wasi or (target.cpu.arch == .x86 and target.os.tag == .windows))
444 .none
445 else
446 .@"async",
446 .unwind_tables = unwind_tables,
447447 .pic = comp.root_mod.pic,
448448 },
449449 .global = config,
src/libunwind.zig+5-2
......@@ -27,6 +27,9 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
2727 const arena = arena_allocator.allocator();
2828
2929 const output_mode = .Lib;
30 const target = comp.root_mod.resolved_target.result;
31 const unwind_tables: std.builtin.UnwindTables =
32 if (target.cpu.arch == .x86 and target.os.tag == .windows) .none else .@"async";
3033 const config = Compilation.Config.resolve(.{
3134 .output_mode = .Lib,
3235 .resolved_target = comp.root_mod.resolved_target,
......@@ -36,6 +39,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
3639 .root_optimize_mode = comp.compilerRtOptMode(),
3740 .root_strip = comp.compilerRtStrip(),
3841 .link_libc = true,
42 .any_unwind_tables = unwind_tables != .none,
3943 .lto = comp.config.lto,
4044 }) catch |err| {
4145 comp.setMiscFailure(
......@@ -45,7 +49,6 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
4549 );
4650 return error.SubCompilationFailed;
4751 };
48 const target = comp.root_mod.resolved_target.result;
4952 const root_mod = Module.create(arena, .{
5053 .global_cache_directory = comp.global_cache_directory,
5154 .paths = .{
......@@ -65,7 +68,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
6568 .sanitize_thread = false,
6669 // necessary so that libunwind can unwind through its own stack frames
6770 // The old 32-bit x86 variant of SEH doesn't use tables.
68 .unwind_tables = if (target.cpu.arch == .x86 and target.os.tag == .windows) .none else .@"async",
71 .unwind_tables = unwind_tables,
6972 .pic = if (target_util.supports_fpic(target)) true else null,
7073 .optimize_mode = comp.compilerRtOptMode(),
7174 },
src/main.zig+2-12
......@@ -2849,12 +2849,7 @@ fn buildOutputType(
28492849 create_module.opts.any_fuzz = true;
28502850 if (mod_opts.unwind_tables) |uwt| switch (uwt) {
28512851 .none => {},
2852 .sync => if (create_module.opts.any_unwind_tables == .none) {
2853 create_module.opts.any_unwind_tables = .sync;
2854 },
2855 .@"async" => {
2856 create_module.opts.any_unwind_tables = .@"async";
2857 },
2852 .sync, .@"async" => create_module.opts.any_unwind_tables = true,
28582853 };
28592854 if (mod_opts.strip == false)
28602855 create_module.opts.any_non_stripped = true;
......@@ -7566,12 +7561,7 @@ fn handleModArg(
75667561 create_module.opts.any_fuzz = true;
75677562 if (mod_opts.unwind_tables) |uwt| switch (uwt) {
75687563 .none => {},
7569 .sync => if (create_module.opts.any_unwind_tables == .none) {
7570 create_module.opts.any_unwind_tables = .sync;
7571 },
7572 .@"async" => {
7573 create_module.opts.any_unwind_tables = .@"async";
7574 },
7564 .sync, .@"async" => create_module.opts.any_unwind_tables = true,
75757565 };
75767566 if (mod_opts.strip == false)
75777567 create_module.opts.any_non_stripped = true;
src/target.zig+1-1
......@@ -407,7 +407,7 @@ pub fn clangSupportsNoImplicitFloatArg(target: std.Target) bool {
407407 };
408408}
409409
410pub fn needUnwindTables(target: std.Target, libunwind: bool, libtsan: bool) std.builtin.UnwindTables {
410pub fn defaultUnwindTables(target: std.Target, libunwind: bool, libtsan: bool) std.builtin.UnwindTables {
411411 if (target.os.tag == .windows) {
412412 // The old 32-bit x86 variant of SEH doesn't use tables.
413413 return if (target.cpu.arch != .x86) .@"async" else .none;