From fbdcb2289b3fed28792474579625a977feca0ed1 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 11 Jan 2025 17:50:17 -0800 Subject: [PATCH] wasm linker: don't pretend it's possible to export data symbols --- src/link/Wasm/Flush.zig | 13 +++++++------ test/behavior.zig | 13 ++++++++++--- test/behavior/export_builtin.zig | 16 +++++++++++++++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/link/Wasm/Flush.zig b/src/link/Wasm/Flush.zig index 91c8adb98e..8b1797aafc 100644 --- a/src/link/Wasm/Flush.zig +++ b/src/link/Wasm/Flush.zig @@ -154,14 +154,15 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void { if (nav_export.name.toOptional() == entry_name) wasm.entry_resolution = .fromIpNav(wasm, nav_export.nav_index); } else { - try wasm.global_exports.append(gpa, .{ - .name = nav_export.name, - .global_index = Wasm.GlobalIndex.fromIpNav(wasm, nav_export.nav_index).?, - }); + // This is a data export because Zcu currently has no way to + // export wasm globals. _ = f.missing_exports.swapRemove(nav_export.name); _ = f.data_imports.swapRemove(nav_export.name); - // `f.global_imports` is ignored because Zcu has no way to - // export wasm globals. + if (!is_obj) { + diags.addError("unable to export data symbol '{s}'; not emitting a relocatable", .{ + nav_export.name.slice(wasm), + }); + } } } diff --git a/test/behavior.zig b/test/behavior.zig index f5ad19a921..e0e07b24cd 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -31,8 +31,6 @@ test { _ = @import("behavior/error.zig"); _ = @import("behavior/eval.zig"); _ = @import("behavior/export_builtin.zig"); - _ = @import("behavior/export_self_referential_type_info.zig"); - _ = @import("behavior/extern.zig"); _ = @import("behavior/field_parent_ptr.zig"); _ = @import("behavior/floatop.zig"); _ = @import("behavior/fn.zig"); @@ -45,7 +43,6 @@ test { _ = @import("behavior/hasfield.zig"); _ = @import("behavior/if.zig"); _ = @import("behavior/import.zig"); - _ = @import("behavior/import_c_keywords.zig"); _ = @import("behavior/incomplete_struct_param_tld.zig"); _ = @import("behavior/inline_switch.zig"); _ = @import("behavior/int128.zig"); @@ -127,6 +124,16 @@ test { { _ = @import("behavior/export_keyword.zig"); } + + if (!builtin.cpu.arch.isWasm()) { + // Due to lack of import/export of global support + // (https://github.com/ziglang/zig/issues/4866), these tests correctly + // cause linker errors, since a data symbol cannot be exported when + // building an executable. + _ = @import("behavior/export_self_referential_type_info.zig"); + _ = @import("behavior/extern.zig"); + _ = @import("behavior/import_c_keywords.zig"); + } } // This bug only repros in the root file diff --git a/test/behavior/export_builtin.zig b/test/behavior/export_builtin.zig index 6ca10376a1..bd53a1df93 100644 --- a/test/behavior/export_builtin.zig +++ b/test/behavior/export_builtin.zig @@ -6,6 +6,11 @@ test "exporting enum value" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.cpu.arch.isWasm()) { + // https://github.com/ziglang/zig/issues/4866 + return error.SkipZigTest; + } + const S = struct { const E = enum(c_int) { one, two }; const e: E = .two; @@ -33,6 +38,11 @@ test "exporting using namespace access" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.cpu.arch.isWasm()) { + // https://github.com/ziglang/zig/issues/4866 + return error.SkipZigTest; + } + const S = struct { const Inner = struct { const x: u32 = 5; @@ -46,7 +56,6 @@ test "exporting using namespace access" { } test "exporting comptime-known value" { - if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_x86_64 and (builtin.target.ofmt != .elf and @@ -56,6 +65,11 @@ test "exporting comptime-known value" { if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + if (builtin.cpu.arch.isWasm()) { + // https://github.com/ziglang/zig/issues/4866 + return error.SkipZigTest; + } + const x: u32 = 10; @export(&x, .{ .name = "exporting_comptime_known_value_foo" }); const S = struct {