authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-11-14 19:58:52+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-11-28 15:47:07+01:00
logc986c6c90a5746cb671177e486a77bd78a5946b0
treefe0f223b4b9fae0af5c1ce446a7f88273c3b5695
parent589aef153709a3c1e0b1ee8af4bb4d710d46b792
signaturelock-open Commit is signed but in an unrecognized format.

wasm-linker: do not merge unreferenced symbols

When a symbol is unreferenced and therefore garbage-collected, we do not merge its specific section into the final binary.

1 files changed, 47 insertions(+), 18 deletions(-)

src/link/Wasm.zig+47-18
......@@ -1981,10 +1981,16 @@ pub fn addTableFunction(wasm: *Wasm, symbol_index: u32) !void {
19811981/// Starts at offset 1, where the value `0` represents an unresolved function pointer
19821982/// or null-pointer
19831983fn mapFunctionTable(wasm: *Wasm) void {
1984 var it = wasm.function_table.valueIterator();
1984 var it = wasm.function_table.iterator();
19851985 var index: u32 = 1;
1986 while (it.next()) |value_ptr| : (index += 1) {
1987 value_ptr.* = index;
1986 while (it.next()) |entry| {
1987 const symbol = entry.key_ptr.*.getSymbol(wasm);
1988 if (symbol.isAlive()) {
1989 entry.value_ptr.* = index;
1990 index += 1;
1991 } else {
1992 wasm.function_table.removeByPtr(entry.key_ptr);
1993 }
19881994 }
19891995
19901996 if (wasm.base.options.import_table or wasm.base.options.output_mode == .Obj) {
......@@ -2242,14 +2248,23 @@ fn allocateAtoms(wasm: *Wasm) !void {
22422248 while (true) {
22432249 const atom = wasm.getAtomPtr(atom_index);
22442250 const symbol_loc = atom.symbolLoc();
2245 if (wasm.code_section_index) |index| {
2246 if (index == entry.key_ptr.*) {
2247 if (!wasm.resolved_symbols.contains(symbol_loc)) {
2248 // only allocate resolved function body's.
2249 atom_index = atom.prev orelse break;
2250 continue;
2251 }
2251 const sym = symbol_loc.getSymbol(wasm);
2252 if (sym.isDead()) {
2253 // Dead symbols must be unlinked from the linked-list to prevent them
2254 // from being emit into the binary.
2255 if (atom.prev) |prev_index| {
2256 const prev = wasm.getAtomPtr(prev_index);
2257 prev.next = atom.next;
22522258 }
2259 atom_index = atom.next orelse {
2260 atom.prev = null;
2261 break;
2262 };
2263 const next = wasm.getAtomPtr(atom_index);
2264 next.prev = atom.prev;
2265 atom.prev = null;
2266 atom.next = null;
2267 continue;
22532268 }
22542269 offset = @intCast(atom.alignment.forward(offset));
22552270 atom.offset = offset;
......@@ -2358,11 +2373,17 @@ fn setupInitFunctions(wasm: *Wasm) !void {
23582373 .file = @as(u16, @intCast(file_index)),
23592374 .priority = init_func.priority,
23602375 });
2376 try wasm.mark(.{ .index = init_func.symbol_index, .file = @intCast(file_index) });
23612377 }
23622378 }
23632379
23642380 // sort the initfunctions based on their priority
23652381 mem.sort(InitFuncLoc, wasm.init_funcs.items, {}, InitFuncLoc.lessThan);
2382
2383 if (wasm.init_funcs.items.len > 0) {
2384 const loc = wasm.findGlobalSymbol("__wasm_call_ctors").?;
2385 try wasm.mark(loc);
2386 }
23662387}
23672388
23682389/// Generates an atom containing the global error set' size.
......@@ -2463,6 +2484,9 @@ fn createSyntheticFunction(
24632484 const loc = wasm.findGlobalSymbol(symbol_name) orelse
24642485 try wasm.createSyntheticSymbol(symbol_name, .function);
24652486 const symbol = loc.getSymbol(wasm);
2487 if (symbol.isDead()) {
2488 return;
2489 }
24662490 const ty_index = try wasm.putOrGetFuncType(func_ty);
24672491 // create function with above type
24682492 const func_index = wasm.imported_functions_count + @as(u32, @intCast(wasm.functions.count()));
......@@ -2628,10 +2652,10 @@ fn setupImports(wasm: *Wasm) !void {
26282652 }
26292653
26302654 const symbol = symbol_loc.getSymbol(wasm);
2631 if (std.mem.eql(u8, symbol_loc.getName(wasm), "__indirect_function_table")) {
2632 continue;
2633 }
2634 if (!symbol.requiresImport()) {
2655 if (symbol.isDead() or
2656 !symbol.requiresImport() or
2657 std.mem.eql(u8, symbol_loc.getName(wasm), "__indirect_function_table"))
2658 {
26352659 continue;
26362660 }
26372661
......@@ -2697,7 +2721,11 @@ fn mergeSections(wasm: *Wasm) !void {
26972721
26982722 const object = &wasm.objects.items[sym_loc.file.?];
26992723 const symbol = &object.symtable[sym_loc.index];
2700 if (symbol.isUndefined() or (symbol.tag != .function and symbol.tag != .global and symbol.tag != .table)) {
2724
2725 if (symbol.isDead() or
2726 symbol.isUndefined() or
2727 (symbol.tag != .function and symbol.tag != .global and symbol.tag != .table))
2728 {
27012729 // Skip undefined symbols as they go in the `import` section
27022730 // Also skip symbols that do not need to have a section merged.
27032731 continue;
......@@ -2753,8 +2781,8 @@ fn mergeTypes(wasm: *Wasm) !void {
27532781 }
27542782 const object = wasm.objects.items[sym_loc.file.?];
27552783 const symbol = object.symtable[sym_loc.index];
2756 if (symbol.tag != .function) {
2757 // Only functions have types
2784 if (symbol.tag != .function or symbol.isDead()) {
2785 // Only functions have types. Only retrieve the type of referenced functions.
27582786 continue;
27592787 }
27602788
......@@ -3823,7 +3851,8 @@ fn writeToFile(
38233851 try leb.writeULEB128(binary_writer, @as(u32, @intCast(wasm.function_table.count())));
38243852 var symbol_it = wasm.function_table.keyIterator();
38253853 while (symbol_it.next()) |symbol_loc_ptr| {
3826 try leb.writeULEB128(binary_writer, symbol_loc_ptr.*.getSymbol(wasm).index);
3854 const sym = symbol_loc_ptr.*.getSymbol(wasm);
3855 try leb.writeULEB128(binary_writer, sym.index);
38273856 }
38283857
38293858 try writeVecSectionHeader(