Skip to content

Commit 8627858

Browse files
committed
test/link: add test for extern resolution
Adds a linker tests to verify extern/undefined symbols representing non-functions are being resolved correctly.
1 parent 4f72ac2 commit 8627858

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

src/arch/wasm/CodeGen.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2355,7 +2355,7 @@ fn lowerDeclRefValue(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index)
23552355

23562356
const module = self.bin_file.base.options.module.?;
23572357
const decl = module.declPtr(decl_index);
2358-
if (!decl.ty.hasRuntimeBitsIgnoreComptime()) {
2358+
if (decl.ty.zigTypeTag() != .Fn and !decl.ty.hasRuntimeBitsIgnoreComptime()) {
23592359
return WValue{ .imm32 = 0xaaaaaaaa };
23602360
}
23612361

test/link.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ fn addWasmCases(cases: *tests.StandaloneContext) void {
5252
.build_modes = true,
5353
.requires_stage2 = true,
5454
});
55+
56+
cases.addBuildFile("test/link/wasm/extern/build.zig", .{
57+
.build_modes = true,
58+
.requires_stage2 = true,
59+
.use_emulation = true,
60+
});
5561
}
5662

5763
fn addMachOCases(cases: *tests.StandaloneContext) void {

test/link/wasm/extern/build.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.build.Builder) void {
4+
const mode = b.standardReleaseOptions();
5+
const exe = b.addExecutable("extern", "main.zig");
6+
exe.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .wasi });
7+
exe.setBuildMode(mode);
8+
exe.addCSourceFile("foo.c", &.{});
9+
exe.use_llvm = false;
10+
exe.use_lld = false;
11+
12+
const run = exe.runEmulatable();
13+
run.expectStdOutEqual("Result: 30");
14+
15+
const test_step = b.step("test", "Run linker test");
16+
test_step.dependOn(&run.step);
17+
}

test/link/wasm/extern/foo.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int foo = 30;

test/link/wasm/extern/main.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const std = @import("std");
2+
3+
extern const foo: u32;
4+
5+
pub fn main() void {
6+
const std_out = std.io.getStdOut();
7+
std_out.writer().print("Result: {d}", .{foo}) catch {};
8+
}

0 commit comments

Comments
 (0)