authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-09 20:55:39-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:36-08:00
log788b7f8f115e8510176ca7fbfdc04fb48d69ecb1
tree91208c95559c3d9ddb00ea97c6a7a64ca70292c2
parent1c4b4fb51604f388bb4355e566aac4ea9fda8960

wasm linker: don't call init functions unless object included


3 files changed, 14 insertions(+), 5 deletions(-)

src/link/Wasm.zig+10-2
......@@ -117,7 +117,7 @@ object_memories: std.ArrayListUnmanaged(ObjectMemory) = .empty,
117117object_relocations: std.MultiArrayList(ObjectRelocation) = .empty,
118118
119119/// List of initialization functions. These must be called in order of priority
120/// by the (synthetic) __wasm_call_ctors function.
120/// by the (synthetic) `__wasm_call_ctors` function.
121121object_init_funcs: std.ArrayListUnmanaged(InitFunc) = .empty,
122122
123123/// The data section of an object has many segments. Each segment corresponds
......@@ -3308,7 +3308,10 @@ pub fn prelink(wasm: *Wasm, prog_node: std.Progress.Node) link.File.FlushError!v
33083308 }
33093309 // Also treat init functions as roots.
33103310 for (wasm.object_init_funcs.items) |init_func| {
3311 try markFunction(wasm, init_func.function_index);
3311 const func = init_func.function_index.ptr(wasm);
3312 if (func.object_index.ptr(wasm).is_included) {
3313 try markFunction(wasm, init_func.function_index);
3314 }
33123315 }
33133316 wasm.functions_end_prelink = @intCast(wasm.functions.entries.len);
33143317 wasm.function_exports_len = @intCast(wasm.function_exports.items.len);
......@@ -3383,6 +3386,7 @@ fn markFunction(wasm: *Wasm, i: ObjectFunctionIndex) link.File.FlushError!void {
33833386 const rdynamic = comp.config.rdynamic;
33843387 const is_obj = comp.config.output_mode == .Obj;
33853388 const function = i.ptr(wasm);
3389 markObject(wasm, function.object_index);
33863390
33873391 if (!is_obj and function.flags.isExported(rdynamic)) try wasm.function_exports.append(gpa, .{
33883392 .name = function.name.unwrap().?,
......@@ -3392,6 +3396,10 @@ fn markFunction(wasm: *Wasm, i: ObjectFunctionIndex) link.File.FlushError!void {
33923396 try wasm.markRelocations(function.relocations(wasm));
33933397}
33943398
3399fn markObject(wasm: *Wasm, i: ObjectIndex) void {
3400 i.ptr(wasm).is_included = true;
3401}
3402
33953403/// Recursively mark alive everything referenced by the global.
33963404fn markGlobalImport(
33973405 wasm: *Wasm,
src/link/Wasm/Flush.zig+2-3
......@@ -188,8 +188,6 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
188188 for (wasm.object_indirect_function_set.keys()) |object_function_index|
189189 f.indirect_function_table.putAssumeCapacity(.fromObjectFunction(wasm, object_function_index), {});
190190
191 // TODO only include init functions for objects with must_link=true or
192 // which have any alive functions inside them.
193191 if (wasm.object_init_funcs.items.len > 0) {
194192 // Zig has no constructors so these are only for object file inputs.
195193 mem.sortUnstable(Wasm.InitFunc, wasm.object_init_funcs.items, {}, Wasm.InitFunc.lessThan);
......@@ -692,7 +690,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
692690 }
693691
694692 // When the shared-memory option is enabled, we *must* emit the 'data count' section.
695 if (f.data_segment_groups.items.len > 0 and shared_memory) {
693 {
696694 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
697695 replaceVecSectionHeader(binary_bytes, header_offset, .data_count, @intCast(f.data_segment_groups.items.len));
698696 }
......@@ -1644,6 +1642,7 @@ fn emitCallCtorsFunction(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanage
16441642
16451643 for (wasm.object_init_funcs.items) |init_func| {
16461644 const func = init_func.function_index.ptr(wasm);
1645 if (!func.object_index.ptr(wasm).is_included) continue;
16471646 const ty = func.type_index.ptr(wasm);
16481647 const n_returns = ty.returns.slice(wasm).len;
16491648
src/link/Wasm/Object.zig+2
......@@ -48,6 +48,7 @@ code_section_index: ?Wasm.ObjectSectionIndex,
4848global_section_index: ?Wasm.ObjectSectionIndex,
4949/// Guaranteed to be non-null when data segments has nonzero length.
5050data_section_index: ?Wasm.ObjectSectionIndex,
51is_included: bool,
5152
5253pub const RelativeSlice = struct {
5354 off: u32,
......@@ -1414,6 +1415,7 @@ pub fn parse(
14141415 .code_section_index = code_section_index,
14151416 .global_section_index = global_section_index,
14161417 .data_section_index = data_section_index,
1418 .is_included = must_link,
14171419 };
14181420}
14191421