Commit fbdcb2289b

Andrew Kelley <andrew@ziglang.org>
2025-01-12 02:50:17
wasm linker: don't pretend it's possible to export data symbols
1 parent 0dd0ebb
Changed files (3)
src
link
test
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),
+                    });
+                }
             }
         }
 
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 {
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