authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-12 16:56:30-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:35-08:00
log968941b535052e2842f47dd9e7e45fa94f8f79c2
treeffc45babe98b66de6db25e90e026016931026ef0
parent264a63b000021b154fffa95e6e0565c3d402706b

wasm linker: finish the flush function

This branch is passing type checking now.

3 files changed, 127 insertions(+), 102 deletions(-)

lib/std/Target.zig+6
......@@ -1219,6 +1219,12 @@ pub const Cpu = struct {
12191219 } else true;
12201220 }
12211221
1222 pub fn count(set: Set) std.math.IntFittingRange(0, needed_bit_count) {
1223 var sum: usize = 0;
1224 for (set.ints) |x| sum += @popCount(x);
1225 return @intCast(sum);
1226 }
1227
12221228 pub fn isEnabled(set: Set, arch_feature_index: Index) bool {
12231229 const usize_index = arch_feature_index / @bitSizeOf(usize);
12241230 const bit_index: ShiftInt = @intCast(arch_feature_index % @bitSizeOf(usize));
src/link/Wasm.zig+18-5
......@@ -270,8 +270,16 @@ pub const FunctionIndex = enum(u32) {
270270 return &wasm.functions.keys()[@intFromEnum(index)];
271271 }
272272
273 pub fn toOutputFunctionIndex(index: FunctionIndex, wasm: *const Wasm) OutputFunctionIndex {
274 return @enumFromInt(wasm.function_imports.entries.len + @intFromEnum(index));
275 }
276
273277 pub fn fromIpNav(wasm: *const Wasm, nav_index: InternPool.Nav.Index) ?FunctionIndex {
274 const i = wasm.functions.getIndex(.fromIpNav(wasm, nav_index)) orelse return null;
278 return fromResolution(wasm, .fromIpNav(wasm, nav_index));
279 }
280
281 pub fn fromResolution(wasm: *const Wasm, resolution: FunctionImport.Resolution) ?FunctionIndex {
282 const i = wasm.functions.getIndex(resolution) orelse return null;
275283 return @enumFromInt(i);
276284 }
277285};
......@@ -295,11 +303,11 @@ pub const OutputFunctionIndex = enum(u32) {
295303 _,
296304};
297305
298/// Index into `globals`.
306/// Index into `Wasm.globals`.
299307pub const GlobalIndex = enum(u32) {
300308 _,
301309
302 fn key(index: GlobalIndex, f: *const Flush) *Wasm.GlobalImport.Resolution {
310 pub fn ptr(index: GlobalIndex, f: *const Flush) *Wasm.GlobalImport.Resolution {
303311 return &f.globals.items[@intFromEnum(index)];
304312 }
305313
......@@ -1089,7 +1097,7 @@ pub const DataSegment = extern struct {
10891097 /// The size in bytes of the data representing the segment within the section.
10901098 len: u32,
10911099
1092 fn slice(p: DataSegment.Payload, wasm: *const Wasm) []const u8 {
1100 pub fn slice(p: DataSegment.Payload, wasm: *const Wasm) []const u8 {
10931101 assert(p.off != p.len);
10941102 return wasm.string_bytes.items[p.off..][0..p.len];
10951103 }
......@@ -2365,6 +2373,7 @@ pub fn flushModule(
23652373 _ = tid;
23662374 const comp = wasm.base.comp;
23672375 const use_lld = build_options.have_llvm and comp.config.use_lld;
2376 const diags = &comp.link_diags;
23682377
23692378 if (wasm.llvm_object) |llvm_object| {
23702379 try wasm.base.emitLlvmObject(arena, llvm_object, prog_node);
......@@ -2392,7 +2401,11 @@ pub fn flushModule(
23922401 defer sub_prog_node.end();
23932402
23942403 wasm.flush_buffer.clear();
2395 return wasm.flush_buffer.finish(wasm, arena);
2404 return wasm.flush_buffer.finish(wasm, arena) catch |err| switch (err) {
2405 error.OutOfMemory => return error.OutOfMemory,
2406 error.LinkFailure => return error.LinkFailure,
2407 else => |e| return diags.fail("failed to flush wasm: {s}", .{@errorName(e)}),
2408 };
23962409}
23972410
23982411fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) !void {
src/link/Wasm/Flush.zig+103-97
......@@ -59,7 +59,7 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
5959 const gpa = comp.gpa;
6060 const import_memory = comp.config.import_memory;
6161 const export_memory = comp.config.export_memory;
62 const target = comp.root_mod.resolved_target.result;
62 const target = &comp.root_mod.resolved_target.result;
6363 const is_obj = comp.config.output_mode == .Obj;
6464 const allow_undefined = is_obj or wasm.import_symbols;
6565 const zcu = wasm.base.comp.zcu.?;
......@@ -523,7 +523,7 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
523523 try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len)));
524524 try binary_bytes.appendSlice(gpa, name);
525525 try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.function));
526 try leb.writeUleb128(binary_writer, @as(u32, @intCast(wasm.function_imports.entries.len + @intFromEnum(exp.function_index))));
526 try leb.writeUleb128(binary_writer, @intFromEnum(exp.function_index));
527527 }
528528 exports_len += wasm.function_exports.items.len;
529529
......@@ -543,7 +543,7 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
543543 try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len)));
544544 try binary_bytes.appendSlice(gpa, name);
545545 try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.global));
546 try leb.writeUleb128(binary_writer, @as(u32, @intCast(wasm.global_imports.entries.len + @intFromEnum(exp.global_index))));
546 try leb.writeUleb128(binary_writer, @intFromEnum(exp.global_index));
547547 }
548548 exports_len += wasm.global_exports.items.len;
549549
......@@ -555,38 +555,39 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
555555 }
556556 }
557557
558 if (Wasm.FunctionIndex.fromResolution(wasm.entry_resolution)) |entry_index| {
558 if (Wasm.FunctionIndex.fromResolution(wasm, wasm.entry_resolution)) |entry_index| {
559559 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
560560 replaceVecSectionHeader(binary_bytes, header_offset, .start, @intFromEnum(entry_index));
561561 }
562562
563563 // element section (function table)
564 if (wasm.function_table.count() > 0) {
565 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
564 if (f.indirect_function_table.count() > 0) {
565 @panic("TODO");
566 //const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
566567
567 const table_loc = wasm.globals.get(wasm.preloaded_strings.__indirect_function_table).?;
568 const table_sym = wasm.finalSymbolByLoc(table_loc);
568 //const table_loc = wasm.globals.get(wasm.preloaded_strings.__indirect_function_table).?;
569 //const table_sym = wasm.finalSymbolByLoc(table_loc);
569570
570 const flags: u32 = if (table_sym.index == 0) 0x0 else 0x02; // passive with implicit 0-index table or set table index manually
571 try leb.writeUleb128(binary_writer, flags);
572 if (flags == 0x02) {
573 try leb.writeUleb128(binary_writer, table_sym.index);
574 }
575 try emitInit(binary_writer, .{ .i32_const = 1 }); // We start at index 1, so unresolved function pointers are invalid
576 if (flags == 0x02) {
577 try leb.writeUleb128(binary_writer, @as(u8, 0)); // represents funcref
578 }
579 try leb.writeUleb128(binary_writer, @as(u32, @intCast(wasm.function_table.count())));
580 var symbol_it = wasm.function_table.keyIterator();
581 while (symbol_it.next()) |symbol_loc_ptr| {
582 const sym = wasm.finalSymbolByLoc(symbol_loc_ptr.*);
583 assert(sym.flags.alive);
584 assert(sym.index < wasm.functions.count() + wasm.imported_functions_count);
585 try leb.writeUleb128(binary_writer, sym.index);
586 }
571 //const flags: u32 = if (table_sym.index == 0) 0x0 else 0x02; // passive with implicit 0-index table or set table index manually
572 //try leb.writeUleb128(binary_writer, flags);
573 //if (flags == 0x02) {
574 // try leb.writeUleb128(binary_writer, table_sym.index);
575 //}
576 //try emitInit(binary_writer, .{ .i32_const = 1 }); // We start at index 1, so unresolved function pointers are invalid
577 //if (flags == 0x02) {
578 // try leb.writeUleb128(binary_writer, @as(u8, 0)); // represents funcref
579 //}
580 //try leb.writeUleb128(binary_writer, @as(u32, @intCast(f.indirect_function_table.count())));
581 //var symbol_it = f.indirect_function_table.keyIterator();
582 //while (symbol_it.next()) |symbol_loc_ptr| {
583 // const sym = wasm.finalSymbolByLoc(symbol_loc_ptr.*);
584 // assert(sym.flags.alive);
585 // assert(sym.index < wasm.functions.count() + wasm.imported_functions_count);
586 // try leb.writeUleb128(binary_writer, sym.index);
587 //}
587588
588 replaceVecSectionHeader(binary_bytes, header_offset, .element, 1);
589 section_index += 1;
589 //replaceVecSectionHeader(binary_bytes, header_offset, .element, 1);
590 //section_index += 1;
590591 }
591592
592593 // When the shared-memory option is enabled, we *must* emit the 'data count' section.
......@@ -600,7 +601,7 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
600601 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
601602 const start_offset = binary_bytes.items.len - 5; // minus 5 so start offset is 5 to include entry count
602603
603 for (wasm.functions.keys()) |resolution| switch (resolution.unpack()) {
604 for (wasm.functions.keys()) |resolution| switch (resolution.unpack(wasm)) {
604605 .unresolved => unreachable,
605606 .__wasm_apply_global_tls_relocs => @panic("TODO lower __wasm_apply_global_tls_relocs"),
606607 .__wasm_call_ctors => @panic("TODO lower __wasm_call_ctors"),
......@@ -614,10 +615,19 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
614615 //try leb.writeUleb128(binary_writer, atom.code.len);
615616 //try binary_bytes.appendSlice(gpa, atom.code.slice(wasm));
616617 },
617 .nav => |i| {
618 .nav_exe => |i| {
619 assert(!is_obj);
620 _ = i;
621 _ = start_offset;
622 @panic("TODO lower nav exe code and apply relocations");
623 //try leb.writeUleb128(binary_writer, atom.code.len);
624 //try binary_bytes.appendSlice(gpa, atom.code.slice(wasm));
625 },
626 .nav_obj => |i| {
627 assert(is_obj);
618628 _ = i;
619629 _ = start_offset;
620 @panic("TODO lower nav code and apply relocations");
630 @panic("TODO lower nav obj code and apply relocations");
621631 //try leb.writeUleb128(binary_writer, atom.code.len);
622632 //try binary_bytes.appendSlice(gpa, atom.code.slice(wasm));
623633 },
......@@ -636,7 +646,8 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
636646 var offset: u32 = undefined;
637647 for (segment_indexes, segment_offsets) |segment_index, segment_offset| {
638648 const segment = segment_index.ptr(wasm);
639 if (segment.size == 0) continue;
649 const segment_payload = segment.payload.slice(wasm);
650 if (segment_payload.len == 0) continue;
640651 if (!import_memory and isBss(wasm, segment.name)) {
641652 // It counted for virtual memory but it does not go into the binary.
642653 continue;
......@@ -657,8 +668,8 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
657668
658669 try binary_bytes.appendNTimes(gpa, 0, segment_offset - offset);
659670 offset = segment_offset;
660 try binary_bytes.appendSlice(gpa, segment.payload.slice(wasm));
661 offset += segment.payload.len;
671 try binary_bytes.appendSlice(gpa, segment_payload);
672 offset += @intCast(segment_payload.len);
662673 if (true) @panic("TODO apply data segment relocations");
663674 }
664675 assert(group_index == f.data_segment_groups.items.len);
......@@ -680,7 +691,7 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
680691 // try wasm.emitDataRelocations(binary_bytes, data_index, symbol_table);
681692 //}
682693 } else if (comp.config.debug_format != .strip) {
683 try wasm.emitNameSection(binary_bytes, arena);
694 try emitNameSection(wasm, binary_bytes, arena);
684695 }
685696
686697 if (comp.config.debug_format != .strip) {
......@@ -699,14 +710,14 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
699710 std.fmt.fmtSliceHexLower(id[8..10]),
700711 std.fmt.fmtSliceHexLower(id[10..]),
701712 });
702 try emitBuildIdSection(binary_bytes, &uuid);
713 try emitBuildIdSection(gpa, binary_bytes, &uuid);
703714 },
704715 .hexstring => |hs| {
705716 var buffer: [32 * 2]u8 = undefined;
706717 const str = std.fmt.bufPrint(&buffer, "{s}", .{
707718 std.fmt.fmtSliceHexLower(hs.toSlice()),
708719 }) catch unreachable;
709 try emitBuildIdSection(binary_bytes, str);
720 try emitBuildIdSection(gpa, binary_bytes, str);
710721 },
711722 else => |mode| {
712723 var err = try diags.addErrorWithNotes(0);
......@@ -717,9 +728,8 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
717728 var debug_bytes = std.ArrayList(u8).init(gpa);
718729 defer debug_bytes.deinit();
719730
720 try emitProducerSection(binary_bytes);
721 if (!target.cpu.features.isEmpty())
722 try emitFeaturesSection(binary_bytes, target.cpu.features);
731 try emitProducerSection(gpa, binary_bytes);
732 try emitFeaturesSection(gpa, binary_bytes, target);
723733 }
724734
725735 // Finally, write the entire binary into the file.
......@@ -729,6 +739,10 @@ pub fn finish(f: *Flush, wasm: *Wasm, arena: Allocator) !void {
729739}
730740
731741fn emitNameSection(wasm: *Wasm, binary_bytes: *std.ArrayListUnmanaged(u8), arena: Allocator) !void {
742 if (true) {
743 std.log.warn("TODO emit name section", .{});
744 return;
745 }
732746 const comp = wasm.base.comp;
733747 const gpa = comp.gpa;
734748 const import_memory = comp.config.import_memory;
......@@ -796,31 +810,15 @@ fn emitNameSection(wasm: *Wasm, binary_bytes: *std.ArrayListUnmanaged(u8), arena
796810 // Data segments are already ordered.
797811
798812 const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
799 const writer = binary_bytes.writer();
813 defer writeCustomSectionHeader(binary_bytes, header_offset);
814
815 const writer = binary_bytes.writer(gpa);
800816 try leb.writeUleb128(writer, @as(u32, @intCast("name".len)));
801817 try writer.writeAll("name");
802818
803819 try emitNameSubsection(wasm, binary_bytes, .function, funcs.keys(), funcs.values());
804820 try emitNameSubsection(wasm, binary_bytes, .global, globals.items(.index), globals.items(.name));
805821 try emitNameSubsection(wasm, binary_bytes, .data_segment, segments.items(.index), segments.items(.name));
806
807 try writeCustomSectionHeader(
808 binary_bytes.items,
809 header_offset,
810 @as(u32, @intCast(binary_bytes.items.len - header_offset - 6)),
811 );
812}
813
814fn writeCustomSectionHeader(buffer: []u8, offset: u32, size: u32) !void {
815 var buf: [1 + 5]u8 = undefined;
816 buf[0] = 0; // 0 = 'custom' section
817 leb.writeUnsignedFixed(5, buf[1..6], size);
818 buffer[offset..][0..buf.len].* = buf;
819}
820
821fn reserveCustomSectionHeader(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!u32 {
822 try bytes.appendNTimes(gpa, 0, section_header_size);
823 return @intCast(bytes.items.len - section_header_size);
824822}
825823
826824fn emitNameSubsection(
......@@ -856,35 +854,40 @@ fn emitNameSubsection(
856854fn emitFeaturesSection(
857855 gpa: Allocator,
858856 binary_bytes: *std.ArrayListUnmanaged(u8),
859 features: []const Wasm.Feature,
860) !void {
857 target: *const std.Target,
858) Allocator.Error!void {
859 const feature_count = target.cpu.features.count();
860 if (feature_count == 0) return;
861
861862 const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
863 defer writeCustomSectionHeader(binary_bytes, header_offset);
862864
863 const writer = binary_bytes.writer();
865 const writer = binary_bytes.writer(gpa);
864866 const target_features = "target_features";
865867 try leb.writeUleb128(writer, @as(u32, @intCast(target_features.len)));
866868 try writer.writeAll(target_features);
867869
868 try leb.writeUleb128(writer, @as(u32, @intCast(features.len)));
869 for (features) |feature| {
870 assert(feature.prefix != .invalid);
871 try leb.writeUleb128(writer, @tagName(feature.prefix)[0]);
872 const name = @tagName(feature.tag);
873 try leb.writeUleb128(writer, @as(u32, name.len));
870 try leb.writeUleb128(writer, @as(u32, @intCast(feature_count)));
871
872 var safety_count = feature_count;
873 for (target.cpu.arch.allFeaturesList(), 0..) |*feature, i| {
874 if (!std.Target.wasm.featureSetHas(target.cpu.features, @enumFromInt(i))) continue;
875 safety_count -= 1;
876
877 try leb.writeUleb128(writer, @as(u32, '+'));
878 // Depends on llvm_name for the hyphenated version that matches wasm tooling conventions.
879 const name = feature.llvm_name.?;
880 try leb.writeUleb128(writer, @as(u32, @intCast(name.len)));
874881 try writer.writeAll(name);
875882 }
876
877 try writeCustomSectionHeader(
878 binary_bytes.items,
879 header_offset,
880 @as(u32, @intCast(binary_bytes.items.len - header_offset - 6)),
881 );
883 assert(safety_count == 0);
882884}
883885
884886fn emitBuildIdSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8), build_id: []const u8) !void {
885887 const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
888 defer writeCustomSectionHeader(binary_bytes, header_offset);
886889
887 const writer = binary_bytes.writer();
890 const writer = binary_bytes.writer(gpa);
888891 const hdr_build_id = "build_id";
889892 try leb.writeUleb128(writer, @as(u32, @intCast(hdr_build_id.len)));
890893 try writer.writeAll(hdr_build_id);
......@@ -892,18 +895,13 @@ fn emitBuildIdSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8),
892895 try leb.writeUleb128(writer, @as(u32, 1));
893896 try leb.writeUleb128(writer, @as(u32, @intCast(build_id.len)));
894897 try writer.writeAll(build_id);
895
896 try writeCustomSectionHeader(
897 binary_bytes.items,
898 header_offset,
899 @as(u32, @intCast(binary_bytes.items.len - header_offset - 6)),
900 );
901898}
902899
903900fn emitProducerSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8)) !void {
904901 const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
902 defer writeCustomSectionHeader(binary_bytes, header_offset);
905903
906 const writer = binary_bytes.writer();
904 const writer = binary_bytes.writer(gpa);
907905 const producers = "producers";
908906 try leb.writeUleb128(writer, @as(u32, @intCast(producers.len)));
909907 try writer.writeAll(producers);
......@@ -947,12 +945,6 @@ fn emitProducerSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8)
947945 try writer.writeAll(build_options.version);
948946 }
949947 }
950
951 try writeCustomSectionHeader(
952 binary_bytes.items,
953 header_offset,
954 @as(u32, @intCast(binary_bytes.items.len - header_offset - 6)),
955 );
956948}
957949
958950///// For each relocatable section, emits a custom "relocation.<section_name>" section
......@@ -965,7 +957,7 @@ fn emitProducerSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8)
965957// const comp = wasm.base.comp;
966958// const gpa = comp.gpa;
967959// const code_index = wasm.code_section_index.unwrap() orelse return;
968// const writer = binary_bytes.writer();
960// const writer = binary_bytes.writer(gpa);
969961// const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
970962//
971963// // write custom section information
......@@ -1001,8 +993,7 @@ fn emitProducerSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8)
1001993// var buf: [5]u8 = undefined;
1002994// leb.writeUnsignedFixed(5, &buf, count);
1003995// try binary_bytes.insertSlice(reloc_start, &buf);
1004// const size: u32 = @intCast(binary_bytes.items.len - header_offset - 6);
1005// try writeCustomSectionHeader(binary_bytes.items, header_offset, size);
996// writeCustomSectionHeader(binary_bytes, header_offset);
1006997//}
1007998
1008999//fn emitDataRelocations(
......@@ -1013,7 +1004,7 @@ fn emitProducerSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8)
10131004//) !void {
10141005// const comp = wasm.base.comp;
10151006// const gpa = comp.gpa;
1016// const writer = binary_bytes.writer();
1007// const writer = binary_bytes.writer(gpa);
10171008// const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
10181009//
10191010// // write custom section information
......@@ -1052,8 +1043,7 @@ fn emitProducerSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8)
10521043// var buf: [5]u8 = undefined;
10531044// leb.writeUnsignedFixed(5, &buf, count);
10541045// try binary_bytes.insertSlice(reloc_start, &buf);
1055// const size = @as(u32, @intCast(binary_bytes.items.len - header_offset - 6));
1056// try writeCustomSectionHeader(binary_bytes.items, header_offset, size);
1046// writeCustomSectionHeader(binary_bytes, header_offset);
10571047//}
10581048
10591049fn isBss(wasm: *Wasm, optional_name: Wasm.OptionalString) bool {
......@@ -1104,6 +1094,21 @@ fn replaceVecSectionHeader(
11041094 bytes.replaceRangeAssumeCapacity(offset, section_header_reserve_size, fbw.getWritten());
11051095}
11061096
1097fn reserveCustomSectionHeader(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!u32 {
1098 try bytes.appendNTimes(gpa, 0, section_header_size);
1099 return @intCast(bytes.items.len - section_header_size);
1100}
1101
1102fn writeCustomSectionHeader(bytes: *std.ArrayListUnmanaged(u8), offset: u32) void {
1103 const size: u32 = @intCast(bytes.items.len - offset - section_header_size);
1104 var buf: [section_header_size]u8 = undefined;
1105 var fbw = std.io.fixedBufferStream(&buf);
1106 const w = fbw.writer();
1107 w.writeByte(0) catch unreachable; // 0 = 'custom' section
1108 leb.writeUleb128(w, size) catch unreachable;
1109 bytes.replaceRangeAssumeCapacity(offset, section_header_size, fbw.getWritten());
1110}
1111
11071112fn emitLimits(
11081113 gpa: Allocator,
11091114 binary_bytes: *std.ArrayListUnmanaged(u8),
......@@ -1171,7 +1176,7 @@ pub fn emitExpr(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanaged(u8), ex
11711176//) !void {
11721177// const gpa = wasm.base.comp.gpa;
11731178// const offset = try reserveCustomSectionHeader(gpa, binary_bytes);
1174// const writer = binary_bytes.writer();
1179// const writer = binary_bytes.writer(gpa);
11751180// // emit "linking" custom section name
11761181// const section_name = "linking";
11771182// try leb.writeUleb128(writer, section_name.len);
......@@ -1186,11 +1191,12 @@ pub fn emitExpr(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanaged(u8), ex
11861191// try wasm.emitSegmentInfo(binary_bytes);
11871192//
11881193// const size: u32 = @intCast(binary_bytes.items.len - offset - 6);
1189// try writeCustomSectionHeader(binary_bytes.items, offset, size);
1194// writeCustomSectionHeader(binary_bytes, offset, size);
11901195//}
11911196
11921197fn emitSegmentInfo(wasm: *Wasm, binary_bytes: *std.ArrayList(u8)) !void {
1193 const writer = binary_bytes.writer();
1198 const gpa = wasm.base.comp.gpa;
1199 const writer = binary_bytes.writer(gpa);
11941200 try leb.writeUleb128(writer, @intFromEnum(Wasm.SubsectionType.segment_info));
11951201 const segment_offset = binary_bytes.items.len;
11961202
......@@ -1370,7 +1376,7 @@ fn emitSegmentInfo(wasm: *Wasm, binary_bytes: *std.ArrayList(u8)) !void {
13701376// .TABLE_INDEX_I64,
13711377// .TABLE_INDEX_SLEB,
13721378// .TABLE_INDEX_SLEB64,
1373// => wasm.function_table.get(.{ .file = atom.file, .index = @enumFromInt(relocation.index) }) orelse 0,
1379// => wasm.indirect_function_table.get(.{ .file = atom.file, .index = @enumFromInt(relocation.index) }) orelse 0,
13741380//
13751381// .TYPE_INDEX_LEB => unreachable, // handled above
13761382// .GLOBAL_INDEX_I32, .GLOBAL_INDEX_LEB => if (symbol.flags.undefined)