From 44b322ce6410a0fab7c3cbdfc35bb1530a31a304 Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Thu, 15 Jun 2023 20:15:01 +0200 Subject: [PATCH] wasm-linker: correctly resolve undefined functions We now resolve undefined symbols during incremental-compilation where we discard the current symbol if we detect we found an existing symbol which is not the one currently being updated. The symbol will always be discarded in favor of the existing symbol in such a case. --- src/link/Wasm.zig | 13 ++++++++++++- test/behavior/bugs/529.zig | 1 - 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index f593f95c3d2a5eb61c0fddef08e52d669918c42d..60944ff981c1acbc2f66b029677db9f76ea33f23 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -1909,6 +1909,17 @@ pub fn addOrUpdateImport( global_gop.value_ptr.* = loc; try wasm.resolved_symbols.put(wasm.base.allocator, loc, {}); try wasm.undefs.putNoClobber(wasm.base.allocator, decl_name_index, loc); + } else if (global_gop.value_ptr.*.index != symbol_index) { + // We are not updating a symbol, but found an existing global + // symbol with the same name. This means we always favor the + // existing symbol, regardless whether it's defined or not. + // We can also skip storing the import as we will not output + // this symbol. + return wasm.discarded.put( + wasm.base.allocator, + .{ .file = null, .index = symbol_index }, + global_gop.value_ptr.*, + ); } if (type_index) |ty_index| { @@ -1924,8 +1935,8 @@ pub fn addOrUpdateImport( }; } } else { + // non-functions will not be imported from the runtime, but only resolved during link-time symbol.tag = .data; - return; // non-functions will not be imported from the runtime, but only resolved during link-time } } diff --git a/test/behavior/bugs/529.zig b/test/behavior/bugs/529.zig index a2e330055aec1723c2a83c3039f2308a2b9ec6e2..49a9b0a46d50d8ac205c163df375cc0ad9a8eea3 100644 --- a/test/behavior/bugs/529.zig +++ b/test/behavior/bugs/529.zig @@ -11,7 +11,6 @@ comptime { const builtin = @import("builtin"); test "issue 529 fixed" { - if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO -- 2.54.0