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 {
625625 try wasm.resolved_symbols.put(wasm.base.allocator, location, {});
626626 assert(wasm.resolved_symbols.swapRemove(existing_loc));
627627 if (existing_sym.isUndefined()) {
628 assert(wasm.undefs.swapRemove(sym_name));
628 _ = wasm.undefs.swapRemove(sym_name);
629629 }
630630 }
631631}
......@@ -636,8 +636,7 @@ fn resolveSymbolsInArchives(wasm: *Wasm) !void {
636636 log.debug("Resolving symbols in archives", .{});
637637 var index: u32 = 0;
638638 undef_loop: while (index < wasm.undefs.count()) {
639 const undef_sym_loc = wasm.undefs.values()[index];
640 const sym_name = undef_sym_loc.getName(wasm);
639 const sym_name = wasm.undefs.keys()[index];
641640
642641 for (wasm.archives.items) |archive| {
643642 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
157157 };
158158
159159 var i: usize = 0;
160 while (i < sym_tab.len) {
161 const string = mem.sliceTo(sym_tab[i..], 0);
162 if (string.len == 0) {
163 i += 1;
164 continue;
165 }
166 i += string.len;
160 var pos: usize = 0;
161 while (i < num_symbols) : (i += 1) {
162 const string = mem.sliceTo(sym_tab[pos..], 0);
163 pos += string.len + 1;
164 if (string.len == 0) continue;
165
167166 const name = try allocator.dupe(u8, string);
168167 errdefer allocator.free(name);
169168 const gop = try archive.toc.getOrPut(allocator, name);
......@@ -172,7 +171,7 @@ fn parseTableOfContents(archive: *Archive, allocator: Allocator, reader: anytype
172171 } else {
173172 gop.value_ptr.* = .{};
174173 }
175 try gop.value_ptr.append(allocator, symbol_positions[gop.index]);
174 try gop.value_ptr.append(allocator, symbol_positions[i]);
176175 }
177176}
178177