authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-03-08 06:42:23+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-03-18 16:47:12+01:00
logd0fb1ef9625b55cc71b41438265f596a00d63749
tree2b9337b32ae6dbf6716670d08fcbecf03ab8b1ca
parentbddf138e7285eefb86ef880e1200a929193da40b
signaturelock-open Commit is signed but in an unrecognized format.

wasm-link: update bss linker test

Updates the linker test to verify the various cases where we must store the data in the bss segment.

2 files changed, 79 insertions(+), 34 deletions(-)

test/link/wasm/bss/build.zig+74-34
...@@ -6,38 +6,78 @@ pub fn build(b: *std.Build) void {...@@ -6,38 +6,78 @@ pub fn build(b: *std.Build) void {
6 const test_step = b.step("test", "Test");6 const test_step = b.step("test", "Test");
7 b.default_step = test_step;7 b.default_step = test_step;
88
9 const lib = b.addSharedLibrary(.{9 add(b, test_step, .Debug, true);
10 .name = "lib",10 add(b, test_step, .ReleaseFast, false);
11 .root_source_file = .{ .path = "lib.zig" },11 add(b, test_step, .ReleaseSmall, false);
12 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },12 add(b, test_step, .ReleaseSafe, true);
13 .optimize = .Debug,13}
14 });14
15 lib.use_llvm = false;15fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.OptimizeMode, is_safe: bool) void {
16 lib.use_lld = false;16 {
17 lib.strip = false;17 const lib = b.addSharedLibrary(.{
18 // to make sure the bss segment is emitted, we must import memory18 .name = "lib",
19 lib.import_memory = true;19 .root_source_file = .{ .path = "lib.zig" },
20 lib.install();20 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
2121 .optimize = optimize_mode,
22 const check_lib = lib.checkObject();22 });
2323 lib.use_llvm = false;
24 // since we import memory, make sure it exists with the correct naming24 lib.use_lld = false;
25 check_lib.checkStart("Section import");25 lib.strip = false;
26 check_lib.checkNext("entries 1");26 // to make sure the bss segment is emitted, we must import memory
27 check_lib.checkNext("module env"); // default module name is "env"27 lib.import_memory = true;
28 check_lib.checkNext("name memory"); // as per linker specification28
2929 const check_lib = lib.checkObject();
30 // since we are importing memory, ensure it's not exported30
31 check_lib.checkNotPresent("Section export");31 // since we import memory, make sure it exists with the correct naming
3232 check_lib.checkStart("Section import");
33 // validate the name of the stack pointer33 check_lib.checkNext("entries 1");
34 check_lib.checkStart("Section custom");34 check_lib.checkNext("module env"); // default module name is "env"
35 check_lib.checkNext("type data_segment");35 check_lib.checkNext("name memory"); // as per linker specification
36 check_lib.checkNext("names 2");36
37 check_lib.checkNext("index 0");37 // since we are importing memory, ensure it's not exported
38 check_lib.checkNext("name .rodata");38 check_lib.checkNotPresent("Section export");
39 check_lib.checkNext("index 1"); // bss section always last39
40 check_lib.checkNext("name .bss");40 // validate the name of the stack pointer
4141 check_lib.checkStart("Section custom");
42 test_step.dependOn(&check_lib.step);42 check_lib.checkNext("type data_segment");
43 check_lib.checkNext("names 2");
44 check_lib.checkNext("index 0");
45 check_lib.checkNext("name .rodata");
46 // for safe optimization modes `undefined` is stored in data instead of bss.
47 if (is_safe) {
48 check_lib.checkNext("index 1");
49 check_lib.checkNext("name .data");
50 check_lib.checkNotPresent("name .bss");
51 } else {
52 check_lib.checkNext("index 1"); // bss section always last
53 check_lib.checkNext("name .bss");
54 }
55 test_step.dependOn(&check_lib.step);
56 }
57
58 // verify zero'd declaration is stored in bss for all optimization modes.
59 {
60 const lib = b.addSharedLibrary(.{
61 .name = "lib",
62 .root_source_file = .{ .path = "lib2.zig" },
63 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
64 .optimize = optimize_mode,
65 });
66 lib.use_llvm = false;
67 lib.use_lld = false;
68 lib.strip = false;
69 // to make sure the bss segment is emitted, we must import memory
70 lib.import_memory = true;
71
72 const check_lib = lib.checkObject();
73 check_lib.checkStart("Section custom");
74 check_lib.checkNext("type data_segment");
75 check_lib.checkNext("names 2");
76 check_lib.checkNext("index 0");
77 check_lib.checkNext("name .rodata");
78 check_lib.checkNext("index 1");
79 check_lib.checkNext("name .bss");
80
81 test_step.dependOn(&check_lib.step);
82 }
43}83}
test/link/wasm/bss/lib2.zig created+5
...@@ -0,0 +1,5 @@
1pub var bss: u32 = 0;
2
3export fn foo() void {
4 _ = bss;
5}