Commit f9b3e8c762

Luuk de Gram <luuk@degram.dev>
2023-01-01 17:13:12
test/link: add test case for exporting data syms
1 parent e475ddb
Changed files (3)
test
link
wasm
export-data
test/link/wasm/export-data/build.zig
@@ -0,0 +1,41 @@
+const std = @import("std");
+const Builder = std.build.Builder;
+
+pub fn build(b: *Builder) void {
+    const mode = b.standardReleaseOptions();
+
+    const test_step = b.step("test", "Test");
+    test_step.dependOn(b.getInstallStep());
+
+    const lib = b.addSharedLibrary("lib", "lib.zig", .unversioned);
+    lib.setBuildMode(mode);
+    lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
+    lib.use_lld = false;
+    lib.export_symbol_names = &.{ "foo", "bar" };
+    lib.global_base = 0; // put data section at address 0 to make data symbols easier to parse
+
+    const check_lib = lib.checkObject(.wasm);
+
+    check_lib.checkStart("Section global");
+    check_lib.checkNext("entries 3");
+    check_lib.checkNext("type i32"); // stack pointer so skip other fields
+    check_lib.checkNext("type i32");
+    check_lib.checkNext("mutable false");
+    check_lib.checkNext("i32.const {foo_address}");
+    check_lib.checkNext("type i32");
+    check_lib.checkNext("mutable false");
+    check_lib.checkNext("i32.const {bar_address}");
+    check_lib.checkComputeCompare("foo_address", .{ .op = .eq, .value = .{ .literal = 0x0c } });
+    check_lib.checkComputeCompare("bar_address", .{ .op = .eq, .value = .{ .literal = 0x10 } });
+
+    check_lib.checkStart("Section export");
+    check_lib.checkNext("entries 3");
+    check_lib.checkNext("name foo");
+    check_lib.checkNext("kind global");
+    check_lib.checkNext("index 1");
+    check_lib.checkNext("name bar");
+    check_lib.checkNext("kind global");
+    check_lib.checkNext("index 2");
+
+    test_step.dependOn(&check_lib.step);
+}
test/link/wasm/export-data/lib.zig
@@ -0,0 +1,2 @@
+export const foo: u32 = 0xbbbbbbbb;
+export const bar: u32 = 0xbbbbbbbb;
test/link.zig
@@ -47,6 +47,10 @@ fn addWasmCases(cases: *tests.StandaloneContext) void {
         .requires_stage2 = true,
     });
 
+    cases.addBuildFile("test/link/wasm/export-data/build.zig", .{
+        .build_modes = true,
+    });
+
     cases.addBuildFile("test/link/wasm/extern/build.zig", .{
         .build_modes = true,
         .requires_stage2 = true,