From 476202eec03a2196daab7f9998c556796cc42eca Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Fri, 16 Dec 2022 18:31:24 +0100 Subject: [PATCH] wasm-linker: Fix archive symbols parsing When parsing the table of contents containing the symbols and their positions we initially used the index within the map to retrieve the offset. However, during resizing of the underlaying array this would invalidate those indexes which meant incorrect offsets were being stored for symbols. We now use the current symbol index to also get the index into the symbol position instead. --- src/link/Wasm.zig | 5 ++--- src/link/Wasm/Archive.zig | 15 +++++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index e89422d0aeff4a8054b4167b871d2062c9409f27..d8aab58ef1b5a66a0bd9aa73e9e2f221e3d1af73 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -625,7 +625,7 @@ fn resolveSymbolsInObject(wasm: *Wasm, object_index: u16) !void { try wasm.resolved_symbols.put(wasm.base.allocator, location, {}); assert(wasm.resolved_symbols.swapRemove(existing_loc)); if (existing_sym.isUndefined()) { - assert(wasm.undefs.swapRemove(sym_name)); + _ = wasm.undefs.swapRemove(sym_name); } } } @@ -636,8 +636,7 @@ fn resolveSymbolsInArchives(wasm: *Wasm) !void { log.debug("Resolving symbols in archives", .{}); var index: u32 = 0; undef_loop: while (index < wasm.undefs.count()) { - const undef_sym_loc = wasm.undefs.values()[index]; - const sym_name = undef_sym_loc.getName(wasm); + const sym_name = wasm.undefs.keys()[index]; for (wasm.archives.items) |archive| { const offset = archive.toc.get(sym_name) orelse { diff --git a/src/link/Wasm/Archive.zig b/src/link/Wasm/Archive.zig index 2fa1e07915366509f6d2c5076b1211c84a4f1046..c4fb9b82917b83c0f0e958024d0f7250b2641946 100644 --- a/src/link/Wasm/Archive.zig +++ b/src/link/Wasm/Archive.zig @@ -157,13 +157,12 @@ fn parseTableOfContents(archive: *Archive, allocator: Allocator, reader: anytype }; var i: usize = 0; - while (i < sym_tab.len) { - const string = mem.sliceTo(sym_tab[i..], 0); - if (string.len == 0) { - i += 1; - continue; - } - i += string.len; + var pos: usize = 0; + while (i < num_symbols) : (i += 1) { + const string = mem.sliceTo(sym_tab[pos..], 0); + pos += string.len + 1; + if (string.len == 0) continue; + const name = try allocator.dupe(u8, string); errdefer allocator.free(name); const gop = try archive.toc.getOrPut(allocator, name); @@ -172,7 +171,7 @@ fn parseTableOfContents(archive: *Archive, allocator: Allocator, reader: anytype } else { gop.value_ptr.* = .{}; } - try gop.value_ptr.append(allocator, symbol_positions[gop.index]); + try gop.value_ptr.append(allocator, symbol_positions[i]); } } -- 2.54.0