authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-20 16:03:45-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:36-08:00
log694b129d8960a9c3548dccb2a8f0c82f44fbafa9
treec3008dc7568ab989d307c1265ed989c05cb080f3
parentee999d5a14c925b71d4b8c216bf1bd7c1b55e00f

wasm linker: fix data section in flush


2 files changed, 66 insertions(+), 36 deletions(-)

src/link/Wasm.zig+6-1
......@@ -327,6 +327,10 @@ pub const GlobalExport = extern struct {
327327pub const OutputFunctionIndex = enum(u32) {
328328 _,
329329
330 pub fn fromResolution(wasm: *const Wasm, resolution: FunctionImport.Resolution) ?OutputFunctionIndex {
331 return fromFunctionIndex(wasm, FunctionIndex.fromResolution(wasm, resolution) orelse return null);
332 }
333
330334 pub fn fromFunctionIndex(wasm: *const Wasm, index: FunctionIndex) OutputFunctionIndex {
331335 return @enumFromInt(wasm.function_imports.entries.len + @intFromEnum(index));
332336 }
......@@ -1487,7 +1491,8 @@ pub const DataSegment = extern struct {
14871491 }
14881492
14891493 pub fn isPassive(id: Id, wasm: *const Wasm) bool {
1490 if (wasm.base.comp.config.import_memory and !id.isBss(wasm)) return true;
1494 const comp = wasm.base.comp;
1495 if (comp.config.import_memory and !id.isBss(wasm)) return true;
14911496 return switch (unpack(id, wasm)) {
14921497 .__zig_error_names, .__zig_error_name_table => false,
14931498 .object => |i| i.ptr(wasm).flags.is_passive,
src/link/Wasm/Flush.zig+60-35
......@@ -21,10 +21,11 @@ const assert = std.debug.assert;
2121
2222/// Ordered list of data segments that will appear in the final binary.
2323/// When sorted, to-be-merged segments will be made adjacent.
24/// Values are offset relative to segment start.
24/// Values are virtual address.
2525data_segments: std.AutoArrayHashMapUnmanaged(Wasm.DataSegment.Id, u32) = .empty,
2626/// Each time a `data_segment` offset equals zero it indicates a new group, and
2727/// the next element in this array will contain the total merged segment size.
28/// Value is the virtual memory address of the end of the segment.
2829data_segment_groups: std.ArrayListUnmanaged(u32) = .empty,
2930
3031binary_bytes: std.ArrayListUnmanaged(u8) = .empty,
......@@ -71,6 +72,10 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
7172 };
7273 const is_obj = comp.config.output_mode == .Obj;
7374 const allow_undefined = is_obj or wasm.import_symbols;
75 //const undef_byte: u8 = switch (comp.root_mod.optimize_mode) {
76 // .Debug, .ReleaseSafe => 0xaa,
77 // .ReleaseFast, .ReleaseSmall => 0x00,
78 //};
7479
7580 if (comp.zcu) |zcu| {
7681 const ip: *const InternPool = &zcu.intern_pool; // No mutations allowed!
......@@ -304,45 +309,46 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
304309 }
305310
306311 const segment_ids = f.data_segments.keys();
307 const segment_offsets = f.data_segments.values();
312 const segment_vaddrs = f.data_segments.values();
308313 assert(f.data_segment_groups.items.len == 0);
314 const data_vaddr: u32 = @intCast(memory_ptr);
309315 {
310316 var seen_tls: enum { before, during, after } = .before;
311 var offset: u32 = 0;
312 for (segment_ids, segment_offsets, 0..) |segment_id, *segment_offset, i| {
317 var category: Wasm.DataSegment.Category = undefined;
318 for (segment_ids, segment_vaddrs, 0..) |segment_id, *segment_vaddr, i| {
313319 const alignment = segment_id.alignment(wasm);
314 memory_ptr = alignment.forward(memory_ptr);
320 category = segment_id.category(wasm);
321 const start_addr = alignment.forward(memory_ptr);
315322
316323 const want_new_segment = b: {
317324 if (is_obj) break :b false;
318325 switch (seen_tls) {
319 .before => if (segment_id.isTls(wasm)) {
320 virtual_addrs.tls_base = if (shared_memory) 0 else @intCast(memory_ptr);
326 .before => if (category == .tls) {
327 virtual_addrs.tls_base = if (shared_memory) 0 else @intCast(start_addr);
321328 virtual_addrs.tls_align = alignment;
322329 seen_tls = .during;
323 break :b true;
330 break :b f.data_segment_groups.items.len > 0;
324331 },
325 .during => if (!segment_id.isTls(wasm)) {
326 virtual_addrs.tls_size = @intCast(memory_ptr - virtual_addrs.tls_base.?);
332 .during => if (category != .tls) {
333 virtual_addrs.tls_size = @intCast(start_addr - virtual_addrs.tls_base.?);
327334 virtual_addrs.tls_align = virtual_addrs.tls_align.maxStrict(alignment);
328335 seen_tls = .after;
329336 break :b true;
330337 },
331338 .after => {},
332339 }
333 break :b i >= 1 and !wantSegmentMerge(wasm, segment_ids[i - 1], segment_id);
340 break :b i >= 1 and !wantSegmentMerge(wasm, segment_ids[i - 1], segment_id, category);
334341 };
335342 if (want_new_segment) {
336 if (offset > 0) try f.data_segment_groups.append(gpa, offset);
337 offset = 0;
343 log.debug("new segment at 0x{x} {} {s} {}", .{ start_addr, segment_id, segment_id.name(wasm), category });
344 try f.data_segment_groups.append(gpa, @intCast(memory_ptr));
338345 }
339346
340347 const size = segment_id.size(wasm);
341 segment_offset.* = offset;
342 offset += size;
343 memory_ptr += size;
348 segment_vaddr.* = @intCast(start_addr);
349 memory_ptr = start_addr + size;
344350 }
345 if (offset > 0) try f.data_segment_groups.append(gpa, offset);
351 if (category != .zero) try f.data_segment_groups.append(gpa, @intCast(memory_ptr));
346352 }
347353
348354 if (shared_memory and any_passive_inits) {
......@@ -588,7 +594,8 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
588594 try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len)));
589595 try binary_bytes.appendSlice(gpa, name);
590596 try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.function));
591 try leb.writeUleb128(binary_writer, @intFromEnum(exp.function_index));
597 const func_index = Wasm.OutputFunctionIndex.fromFunctionIndex(wasm, exp.function_index);
598 try leb.writeUleb128(binary_writer, @intFromEnum(func_index));
592599 }
593600 exports_len += wasm.function_exports.items.len;
594601
......@@ -620,9 +627,9 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
620627 }
621628 }
622629
623 if (Wasm.FunctionIndex.fromResolution(wasm, wasm.entry_resolution)) |entry_index| {
630 if (Wasm.OutputFunctionIndex.fromResolution(wasm, wasm.entry_resolution)) |func_index| {
624631 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
625 replaceVecSectionHeader(binary_bytes, header_offset, .start, @intFromEnum(entry_index));
632 replaceVecSectionHeader(binary_bytes, header_offset, .start, @intFromEnum(func_index));
626633 }
627634
628635 // element section (function table)
......@@ -720,28 +727,41 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
720727 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
721728
722729 var group_index: u32 = 0;
723 var offset: u32 = undefined;
724 for (segment_ids, segment_offsets) |segment_id, segment_offset| {
725 if (segment_id.isEmpty(wasm)) {
726 // It counted for virtual memory but it does not go into the binary.
727 continue;
730 var segment_offset: u32 = 0;
731 var group_start_addr: u32 = data_vaddr;
732 var group_end_addr = f.data_segment_groups.items[group_index];
733 for (segment_ids, segment_vaddrs) |segment_id, segment_vaddr| {
734 if (segment_vaddr >= group_end_addr) {
735 try binary_bytes.appendNTimes(gpa, 0, group_end_addr - group_start_addr - segment_offset);
736 group_index += 1;
737 if (group_index >= f.data_segment_groups.items.len) {
738 // All remaining segments are zero.
739 break;
740 }
741 group_start_addr = group_end_addr;
742 group_end_addr = f.data_segment_groups.items[group_index];
743 segment_offset = 0;
728744 }
729745 if (segment_offset == 0) {
730 const group_size = f.data_segment_groups.items[group_index];
731 group_index += 1;
732 offset = 0;
733
746 const group_size = group_end_addr - group_start_addr;
747 log.debug("emit data section group, {d} bytes", .{group_size});
734748 const flags: Object.DataSegmentFlags = if (segment_id.isPassive(wasm)) .passive else .active;
735749 try leb.writeUleb128(binary_writer, @intFromEnum(flags));
736 // when a segment is passive, it's initialized during runtime.
750 // Passive segments are initialized at runtime.
737751 if (flags != .passive) {
738752 try emitInit(binary_writer, .{ .i32_const = @as(i32, @bitCast(segment_offset)) });
739753 }
740754 try leb.writeUleb128(binary_writer, group_size);
741755 }
756 if (segment_id.isEmpty(wasm)) {
757 // It counted for virtual memory but it does not go into the binary.
758 continue;
759 }
742760
743 try binary_bytes.appendNTimes(gpa, 0, segment_offset - offset);
744 offset = segment_offset;
761 // Padding for alignment.
762 const needed_offset = segment_vaddr - group_start_addr;
763 try binary_bytes.appendNTimes(gpa, 0, needed_offset - segment_offset);
764 segment_offset = needed_offset;
745765
746766 const code_start = binary_bytes.items.len;
747767 append: {
......@@ -768,7 +788,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
768788 };
769789 try binary_bytes.appendSlice(gpa, code.slice(wasm));
770790 }
771 offset += @intCast(binary_bytes.items.len - code_start);
791 segment_offset += @intCast(binary_bytes.items.len - code_start);
772792 }
773793 assert(group_index == f.data_segment_groups.items.len);
774794
......@@ -1111,12 +1131,17 @@ fn splitSegmentName(name: []const u8) struct { []const u8, []const u8 } {
11111131 return .{ name[0..pivot], name[pivot..] };
11121132}
11131133
1114fn wantSegmentMerge(wasm: *const Wasm, a_id: Wasm.DataSegment.Id, b_id: Wasm.DataSegment.Id) bool {
1134fn wantSegmentMerge(
1135 wasm: *const Wasm,
1136 a_id: Wasm.DataSegment.Id,
1137 b_id: Wasm.DataSegment.Id,
1138 b_category: Wasm.DataSegment.Category,
1139) bool {
11151140 const a_category = a_id.category(wasm);
1116 const b_category = b_id.category(wasm);
11171141 if (a_category != b_category) return false;
11181142 if (a_category == .tls or b_category == .tls) return false;
11191143 if (a_id.isPassive(wasm) != b_id.isPassive(wasm)) return false;
1144 if (b_category == .zero) return true;
11201145 const a_name = a_id.name(wasm);
11211146 const b_name = b_id.name(wasm);
11221147 const a_prefix, _ = splitSegmentName(a_name);