authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-12-16 18:31:24+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-12-16 18:31:24+01:00
log476202eec03a2196daab7f9998c556796cc42eca
tree9a637647a15920ea59a6e07ea49494f96b33eeb0
parentae106db8897ce3afd38dbfc1c2c811cd2f2de357
signature Commit is signed but in an unrecognized format.

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.

2 files changed, 9 insertions(+), 11 deletions(-)

src/link/Wasm.zig+2-3
...@@ -625,7 +625,7 @@ fn resolveSymbolsInObject(wasm: *Wasm, object_index: u16) !void {...@@ -625,7 +625,7 @@ fn resolveSymbolsInObject(wasm: *Wasm, object_index: u16) !void {
625 try wasm.resolved_symbols.put(wasm.base.allocator, location, {});625 try wasm.resolved_symbols.put(wasm.base.allocator, location, {});
626 assert(wasm.resolved_symbols.swapRemove(existing_loc));626 assert(wasm.resolved_symbols.swapRemove(existing_loc));
627 if (existing_sym.isUndefined()) {627 if (existing_sym.isUndefined()) {
628 assert(wasm.undefs.swapRemove(sym_name));628 _ = wasm.undefs.swapRemove(sym_name);
629 }629 }
630 }630 }
631}631}
...@@ -636,8 +636,7 @@ fn resolveSymbolsInArchives(wasm: *Wasm) !void {...@@ -636,8 +636,7 @@ fn resolveSymbolsInArchives(wasm: *Wasm) !void {
636 log.debug("Resolving symbols in archives", .{});636 log.debug("Resolving symbols in archives", .{});
637 var index: u32 = 0;637 var index: u32 = 0;
638 undef_loop: while (index < wasm.undefs.count()) {638 undef_loop: while (index < wasm.undefs.count()) {
639 const undef_sym_loc = wasm.undefs.values()[index];639 const sym_name = wasm.undefs.keys()[index];
640 const sym_name = undef_sym_loc.getName(wasm);
641640
642 for (wasm.archives.items) |archive| {641 for (wasm.archives.items) |archive| {
643 const offset = archive.toc.get(sym_name) orelse {642 const offset = archive.toc.get(sym_name) orelse {
src/link/Wasm/Archive.zig+7-8
...@@ -157,13 +157,12 @@ fn parseTableOfContents(archive: *Archive, allocator: Allocator, reader: anytype...@@ -157,13 +157,12 @@ fn parseTableOfContents(archive: *Archive, allocator: Allocator, reader: anytype
157 };157 };
158158
159 var i: usize = 0;159 var i: usize = 0;
160 while (i < sym_tab.len) {160 var pos: usize = 0;
161 const string = mem.sliceTo(sym_tab[i..], 0);161 while (i < num_symbols) : (i += 1) {
162 if (string.len == 0) {162 const string = mem.sliceTo(sym_tab[pos..], 0);
163 i += 1;163 pos += string.len + 1;
164 continue;164 if (string.len == 0) continue;
165 }165
166 i += string.len;
167 const name = try allocator.dupe(u8, string);166 const name = try allocator.dupe(u8, string);
168 errdefer allocator.free(name);167 errdefer allocator.free(name);
169 const gop = try archive.toc.getOrPut(allocator, name);168 const gop = try archive.toc.getOrPut(allocator, name);
...@@ -172,7 +171,7 @@ fn parseTableOfContents(archive: *Archive, allocator: Allocator, reader: anytype...@@ -172,7 +171,7 @@ fn parseTableOfContents(archive: *Archive, allocator: Allocator, reader: anytype
172 } else {171 } else {
173 gop.value_ptr.* = .{};172 gop.value_ptr.* = .{};
174 }173 }
175 try gop.value_ptr.append(allocator, symbol_positions[gop.index]);174 try gop.value_ptr.append(allocator, symbol_positions[i]);
176 }175 }
177}176}
178177