authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-14 18:23:38-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:36-08:00
logb37ad5110cb66330211e930df1d1093963213c48
tree9927c40937719df863caf5eec90ccfab744c4c9e
parentc535422423abc0258b03693392bae86560a9cc79

wasm linker: always passive when importing memory

and detect passive inits from Zcu don't forget to intern function type for __wasm_init_memory make that function the start function if it is present don't skip emitting passive data segment data to the binary

2 files changed, 18 insertions(+), 7 deletions(-)

src/link/Wasm.zig+5-2
......@@ -1968,7 +1968,7 @@ pub const DataSegmentId = enum(u32) {
19681968
19691969 pub fn isPassive(id: DataSegmentId, wasm: *const Wasm) bool {
19701970 const comp = wasm.base.comp;
1971 if (comp.config.import_memory and !id.isBss(wasm)) return true;
1971 if (comp.config.import_memory) return true;
19721972 return switch (unpack(id, wasm)) {
19731973 .__zig_error_names,
19741974 .__zig_error_name_table,
......@@ -4614,7 +4614,10 @@ fn lowerZcuData(wasm: *Wasm, pt: Zcu.PerThread, ip_index: InternPool.Index) !Zcu
46144614 .off = .none,
46154615 .len = naive_code.len,
46164616 };
4617 } else naive_code;
4617 } else c: {
4618 wasm.any_passive_inits = wasm.any_passive_inits or wasm.base.comp.config.import_memory;
4619 break :c naive_code;
4620 };
46184621
46194622 return .{
46204623 .code = code,
src/link/Wasm/Flush.zig+13-5
......@@ -282,6 +282,8 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
282282 // function.
283283 if (wasm.any_passive_inits) {
284284 wasm.functions.putAssumeCapacity(.__wasm_init_memory, {});
285 const empty = try wasm.internValtypeList(&.{});
286 _ = try wasm.addFuncType(.{ .params = empty, .returns = empty });
285287 }
286288
287289 // When we have TLS GOT entries and shared memory is enabled,
......@@ -711,9 +713,10 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
711713 }
712714
713715 // start section
714 if (Wasm.OutputFunctionIndex.fromResolution(wasm, wasm.entry_resolution)) |func_index| {
715 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
716 replaceVecSectionHeader(binary_bytes, header_offset, .start, @intFromEnum(func_index));
716 if (wasm.functions.getIndex(.__wasm_init_memory)) |func_index| {
717 try emitStartSection(gpa, binary_bytes, .fromFunctionIndex(wasm, @enumFromInt(func_index)));
718 } else if (Wasm.OutputFunctionIndex.fromResolution(wasm, wasm.entry_resolution)) |func_index| {
719 try emitStartSection(gpa, binary_bytes, func_index);
717720 }
718721
719722 // element section
......@@ -846,10 +849,10 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
846849 group_end_addr = f.data_segment_groups.items[group_index].end_addr;
847850 segment_offset = 0;
848851 }
849 const flags: Object.DataSegmentFlags = if (segment_id.isPassive(wasm)) .passive else .active;
850852 if (segment_offset == 0) {
851853 const group_size = group_end_addr - group_start_addr;
852854 log.debug("emit data section group, {d} bytes", .{group_size});
855 const flags: Object.DataSegmentFlags = if (segment_id.isPassive(wasm)) .passive else .active;
853856 try leb.writeUleb128(binary_writer, @intFromEnum(flags));
854857 // Passive segments are initialized at runtime.
855858 if (flags != .passive) {
......@@ -857,7 +860,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
857860 }
858861 try leb.writeUleb128(binary_writer, group_size);
859862 }
860 if (flags == .passive or segment_id.isEmpty(wasm)) {
863 if (segment_id.isEmpty(wasm)) {
861864 // It counted for virtual memory but it does not go into the binary.
862865 continue;
863866 }
......@@ -1900,6 +1903,11 @@ fn emitInitMemoryFunction(
19001903 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end));
19011904}
19021905
1906fn emitStartSection(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8), i: Wasm.OutputFunctionIndex) !void {
1907 const header_offset = try reserveVecSectionHeader(gpa, bytes);
1908 replaceVecSectionHeader(bytes, header_offset, .start, @intFromEnum(i));
1909}
1910
19031911fn emitTagNameFunction(
19041912 wasm: *Wasm,
19051913 code: *std.ArrayListUnmanaged(u8),