diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-01-16 04:20:41 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-16 04:20:41 -0500 |
| commit | d4fe4698d9ff865ed1dc7e0163f2d5fcbe2b45a6 (patch) | |
| tree | 160d596e8ab0ab9568dac3f026c2ce42ad1c935e /src/link | |
| parent | 77273103a8f9895ceab28287dffcf4d4c6fcb91b (diff) | |
| parent | eda8b6e137a10f398cd292b533e924960f7fc409 (diff) | |
| download | zig-d4fe4698d9ff865ed1dc7e0163f2d5fcbe2b45a6.tar.gz zig-d4fe4698d9ff865ed1dc7e0163f2d5fcbe2b45a6.zip | |
Merge pull request #22220 from ziglang/wasm-linker
wasm linker: aggressive rewrite towards Data-Oriented Design
Diffstat (limited to 'src/link')
| -rw-r--r-- | src/link/C.zig | 27 | ||||
| -rw-r--r-- | src/link/Coff.zig | 211 | ||||
| -rw-r--r-- | src/link/Dwarf.zig | 50 | ||||
| -rw-r--r-- | src/link/Elf.zig | 193 | ||||
| -rw-r--r-- | src/link/Elf/Atom.zig | 24 | ||||
| -rw-r--r-- | src/link/Elf/AtomList.zig | 5 | ||||
| -rw-r--r-- | src/link/Elf/Object.zig | 10 | ||||
| -rw-r--r-- | src/link/Elf/ZigObject.zig | 132 | ||||
| -rw-r--r-- | src/link/Elf/eh_frame.zig | 2 | ||||
| -rw-r--r-- | src/link/Elf/relocatable.zig | 12 | ||||
| -rw-r--r-- | src/link/MachO.zig | 181 | ||||
| -rw-r--r-- | src/link/MachO/Atom.zig | 14 | ||||
| -rw-r--r-- | src/link/MachO/InternalObject.zig | 16 | ||||
| -rw-r--r-- | src/link/MachO/Object.zig | 66 | ||||
| -rw-r--r-- | src/link/MachO/ZigObject.zig | 153 | ||||
| -rw-r--r-- | src/link/MachO/relocatable.zig | 110 | ||||
| -rw-r--r-- | src/link/NvPtx.zig | 16 | ||||
| -rw-r--r-- | src/link/Plan9.zig | 186 | ||||
| -rw-r--r-- | src/link/SpirV.zig | 44 | ||||
| -rw-r--r-- | src/link/Wasm.zig | 7363 | ||||
| -rw-r--r-- | src/link/Wasm/Archive.zig | 17 | ||||
| -rw-r--r-- | src/link/Wasm/Flush.zig | 1975 | ||||
| -rw-r--r-- | src/link/Wasm/Object.zig | 2153 | ||||
| -rw-r--r-- | src/link/Wasm/Symbol.zig | 210 | ||||
| -rw-r--r-- | src/link/Wasm/ZigObject.zig | 1229 |
25 files changed, 7883 insertions, 6516 deletions
diff --git a/src/link/C.zig b/src/link/C.zig index 4df5b824bd..6c7b7c8975 100644 --- a/src/link/C.zig +++ b/src/link/C.zig @@ -175,21 +175,13 @@ pub fn deinit(self: *C) void { self.lazy_code_buf.deinit(gpa); } -pub fn freeDecl(self: *C, decl_index: InternPool.DeclIndex) void { - const gpa = self.base.comp.gpa; - if (self.decl_table.fetchSwapRemove(decl_index)) |kv| { - var decl_block = kv.value; - decl_block.deinit(gpa); - } -} - pub fn updateFunc( self: *C, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness, -) !void { +) link.File.UpdateNavError!void { const zcu = pt.zcu; const gpa = zcu.gpa; const func = zcu.funcInfo(func_index); @@ -313,7 +305,7 @@ fn updateUav(self: *C, pt: Zcu.PerThread, i: usize) !void { }; } -pub fn updateNav(self: *C, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) !void { +pub fn updateNav(self: *C, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) link.File.UpdateNavError!void { const tracy = trace(@src()); defer tracy.end(); @@ -390,7 +382,7 @@ pub fn updateLineNumber(self: *C, pt: Zcu.PerThread, ti_id: InternPool.TrackedIn _ = ti_id; } -pub fn flush(self: *C, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) !void { +pub fn flush(self: *C, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { return self.flushModule(arena, tid, prog_node); } @@ -409,7 +401,7 @@ fn abiDefines(self: *C, target: std.Target) !std.ArrayList(u8) { return defines; } -pub fn flushModule(self: *C, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) !void { +pub fn flushModule(self: *C, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { _ = arena; // Has the same lifetime as the call to Compilation.update. const tracy = trace(@src()); @@ -419,6 +411,7 @@ pub fn flushModule(self: *C, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: defer sub_prog_node.end(); const comp = self.base.comp; + const diags = &comp.link_diags; const gpa = comp.gpa; const zcu = self.base.comp.zcu.?; const ip = &zcu.intern_pool; @@ -476,7 +469,7 @@ pub fn flushModule(self: *C, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: defer export_names.deinit(gpa); try export_names.ensureTotalCapacity(gpa, @intCast(zcu.single_exports.count())); for (zcu.single_exports.values()) |export_index| { - export_names.putAssumeCapacity(zcu.all_exports.items[export_index].opts.name, {}); + export_names.putAssumeCapacity(export_index.ptr(zcu).opts.name, {}); } for (zcu.multi_exports.values()) |info| { try export_names.ensureUnusedCapacity(gpa, info.len); @@ -554,8 +547,10 @@ pub fn flushModule(self: *C, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: }, self.getString(av_block.code)); const file = self.base.file.?; - try file.setEndPos(f.file_size); - try file.pwritevAll(f.all_buffers.items, 0); + file.setEndPos(f.file_size) catch |err| return diags.fail("failed to allocate file: {s}", .{@errorName(err)}); + file.pwritevAll(f.all_buffers.items, 0) catch |err| return diags.fail("failed to write to '{'}': {s}", .{ + self.base.emit, @errorName(err), + }); } const Flush = struct { @@ -845,7 +840,7 @@ pub fn updateExports( self: *C, pt: Zcu.PerThread, exported: Zcu.Exported, - export_indices: []const u32, + export_indices: []const Zcu.Export.Index, ) !void { const zcu = pt.zcu; const gpa = zcu.gpa; diff --git a/src/link/Coff.zig b/src/link/Coff.zig index 64df9b82f8..9bd3bd76e9 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -408,7 +408,7 @@ pub fn createEmpty( max_file_offset = header.pointer_to_raw_data + header.size_of_raw_data; } } - try coff.base.file.?.pwriteAll(&[_]u8{0}, max_file_offset); + try coff.pwriteAll(&[_]u8{0}, max_file_offset); } return coff; @@ -858,7 +858,7 @@ fn writeAtom(coff: *Coff, atom_index: Atom.Index, code: []u8) !void { } coff.resolveRelocs(atom_index, relocs.items, code, coff.image_base); - try coff.base.file.?.pwriteAll(code, file_offset); + try coff.pwriteAll(code, file_offset); // Now we can mark the relocs as resolved. while (relocs.popOrNull()) |reloc| { @@ -891,7 +891,7 @@ fn writeOffsetTableEntry(coff: *Coff, index: usize) !void { const sect_id = coff.got_section_index.?; if (coff.got_table_count_dirty) { - const needed_size = @as(u32, @intCast(coff.got_table.entries.items.len * coff.ptr_width.size())); + const needed_size: u32 = @intCast(coff.got_table.entries.items.len * coff.ptr_width.size()); try coff.growSection(sect_id, needed_size); coff.got_table_count_dirty = false; } @@ -908,7 +908,7 @@ fn writeOffsetTableEntry(coff: *Coff, index: usize) !void { switch (coff.ptr_width) { .p32 => { var buf: [4]u8 = undefined; - mem.writeInt(u32, &buf, @as(u32, @intCast(entry_value + coff.image_base)), .little); + mem.writeInt(u32, &buf, @intCast(entry_value + coff.image_base), .little); try coff.base.file.?.pwriteAll(&buf, file_offset); }, .p64 => { @@ -1093,7 +1093,13 @@ fn freeAtom(coff: *Coff, atom_index: Atom.Index) void { coff.getAtomPtr(atom_index).sym_index = 0; } -pub fn updateFunc(coff: *Coff, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { +pub fn updateFunc( + coff: *Coff, + pt: Zcu.PerThread, + func_index: InternPool.Index, + air: Air, + liveness: Liveness, +) link.File.UpdateNavError!void { if (build_options.skip_non_native and builtin.object_format != .coff) { @panic("Attempted to compile for object format that was disabled by build configuration"); } @@ -1106,34 +1112,41 @@ pub fn updateFunc(coff: *Coff, pt: Zcu.PerThread, func_index: InternPool.Index, const zcu = pt.zcu; const gpa = zcu.gpa; const func = zcu.funcInfo(func_index); + const nav_index = func.owner_nav; - const atom_index = try coff.getOrCreateAtomForNav(func.owner_nav); + const atom_index = try coff.getOrCreateAtomForNav(nav_index); coff.freeRelocations(atom_index); coff.navs.getPtr(func.owner_nav).?.section = coff.text_section_index.?; - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); - const res = try codegen.generateFunction( + codegen.generateFunction( &coff.base, pt, - zcu.navSrcLoc(func.owner_nav), + zcu.navSrcLoc(nav_index), func_index, air, liveness, &code_buffer, .none, - ); - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| { - try zcu.failed_codegen.put(zcu.gpa, func.owner_nav, em); - return; + ) catch |err| switch (err) { + error.CodegenFail => return error.CodegenFail, + error.OutOfMemory => return error.OutOfMemory, + error.Overflow => |e| { + try zcu.failed_codegen.putNoClobber(gpa, nav_index, try Zcu.ErrorMsg.create( + gpa, + zcu.navSrcLoc(nav_index), + "unable to codegen: {s}", + .{@errorName(e)}, + )); + try zcu.retryable_failures.append(zcu.gpa, AnalUnit.wrap(.{ .func = func_index })); + return error.CodegenFail; }, }; - try coff.updateNavCode(pt, func.owner_nav, code, .FUNCTION); + try coff.updateNavCode(pt, nav_index, code_buffer.items, .FUNCTION); // Exports will be updated by `Zcu.processExports` after the update. } @@ -1154,24 +1167,21 @@ fn lowerConst( ) !LowerConstResult { const gpa = coff.base.comp.gpa; - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); const atom_index = try coff.createAtom(); const sym = coff.getAtom(atom_index).getSymbolPtr(coff); try coff.setSymbolName(sym, name); sym.section_number = @as(coff_util.SectionNumber, @enumFromInt(sect_id + 1)); - const res = try codegen.generateSymbol(&coff.base, pt, src_loc, val, &code_buffer, .{ + try codegen.generateSymbol(&coff.base, pt, src_loc, val, &code_buffer, .{ .atom_index = coff.getAtom(atom_index).getSymbolIndex().?, }); - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| return .{ .fail = em }, - }; + const code = code_buffer.items; const atom = coff.getAtomPtr(atom_index); - atom.size = @as(u32, @intCast(code.len)); + atom.size = @intCast(code.len); atom.getSymbolPtr(coff).value = try coff.allocateAtom( atom_index, atom.size, @@ -1227,10 +1237,10 @@ pub fn updateNav( coff.navs.getPtr(nav_index).?.section = coff.getNavOutputSection(nav_index); - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); - const res = try codegen.generateSymbol( + try codegen.generateSymbol( &coff.base, pt, zcu.navSrcLoc(nav_index), @@ -1238,15 +1248,8 @@ pub fn updateNav( &code_buffer, .{ .atom_index = atom.getSymbolIndex().? }, ); - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| { - try zcu.failed_codegen.put(gpa, nav_index, em); - return; - }, - }; - try coff.updateNavCode(pt, nav_index, code, .NULL); + try coff.updateNavCode(pt, nav_index, code_buffer.items, .NULL); } // Exports will be updated by `Zcu.processExports` after the update. @@ -1260,11 +1263,12 @@ fn updateLazySymbolAtom( section_index: u16, ) !void { const zcu = pt.zcu; - const gpa = zcu.gpa; + const comp = coff.base.comp; + const gpa = comp.gpa; var required_alignment: InternPool.Alignment = .none; - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); const name = try allocPrint(gpa, "__lazy_{s}_{}", .{ @tagName(sym.kind), @@ -1276,7 +1280,7 @@ fn updateLazySymbolAtom( const local_sym_index = atom.getSymbolIndex().?; const src = Type.fromInterned(sym.ty).srcLocOrNull(zcu) orelse Zcu.LazySrcLoc.unneeded; - const res = try codegen.generateLazySymbol( + try codegen.generateLazySymbol( &coff.base, pt, src, @@ -1286,13 +1290,7 @@ fn updateLazySymbolAtom( .none, .{ .atom_index = local_sym_index }, ); - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| { - log.err("{s}", .{em.msg}); - return error.CodegenFail; - }, - }; + const code = code_buffer.items; const code_len: u32 = @intCast(code.len); const symbol = atom.getSymbolPtr(coff); @@ -1387,7 +1385,7 @@ fn updateNavCode( nav_index: InternPool.Nav.Index, code: []u8, complex_type: coff_util.ComplexType, -) !void { +) link.File.UpdateNavError!void { const zcu = pt.zcu; const ip = &zcu.intern_pool; const nav = ip.getNav(nav_index); @@ -1405,18 +1403,21 @@ fn updateNavCode( const atom = coff.getAtom(atom_index); const sym_index = atom.getSymbolIndex().?; const sect_index = nav_metadata.section; - const code_len = @as(u32, @intCast(code.len)); + const code_len: u32 = @intCast(code.len); if (atom.size != 0) { const sym = atom.getSymbolPtr(coff); try coff.setSymbolName(sym, nav.fqn.toSlice(ip)); - sym.section_number = @as(coff_util.SectionNumber, @enumFromInt(sect_index + 1)); + sym.section_number = @enumFromInt(sect_index + 1); sym.type = .{ .complex_type = complex_type, .base_type = .NULL }; const capacity = atom.capacity(coff); const need_realloc = code.len > capacity or !required_alignment.check(sym.value); if (need_realloc) { - const vaddr = try coff.growAtom(atom_index, code_len, @intCast(required_alignment.toByteUnits() orelse 0)); + const vaddr = coff.growAtom(atom_index, code_len, @intCast(required_alignment.toByteUnits() orelse 0)) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| return coff.base.cgFail(nav_index, "failed to grow atom: {s}", .{@errorName(e)}), + }; log.debug("growing {} from 0x{x} to 0x{x}", .{ nav.fqn.fmt(ip), sym.value, vaddr }); log.debug(" (required alignment 0x{x}", .{required_alignment}); @@ -1424,7 +1425,10 @@ fn updateNavCode( sym.value = vaddr; log.debug(" (updating GOT entry)", .{}); const got_entry_index = coff.got_table.lookup.get(.{ .sym_index = sym_index }).?; - try coff.writeOffsetTableEntry(got_entry_index); + coff.writeOffsetTableEntry(got_entry_index) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| return coff.base.cgFail(nav_index, "failed to write offset table entry: {s}", .{@errorName(e)}), + }; coff.markRelocsDirtyByTarget(.{ .sym_index = sym_index }); } } else if (code_len < atom.size) { @@ -1434,26 +1438,34 @@ fn updateNavCode( } else { const sym = atom.getSymbolPtr(coff); try coff.setSymbolName(sym, nav.fqn.toSlice(ip)); - sym.section_number = @as(coff_util.SectionNumber, @enumFromInt(sect_index + 1)); + sym.section_number = @enumFromInt(sect_index + 1); sym.type = .{ .complex_type = complex_type, .base_type = .NULL }; - const vaddr = try coff.allocateAtom(atom_index, code_len, @intCast(required_alignment.toByteUnits() orelse 0)); + const vaddr = coff.allocateAtom(atom_index, code_len, @intCast(required_alignment.toByteUnits() orelse 0)) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| return coff.base.cgFail(nav_index, "failed to allocate atom: {s}", .{@errorName(e)}), + }; errdefer coff.freeAtom(atom_index); log.debug("allocated atom for {} at 0x{x}", .{ nav.fqn.fmt(ip), vaddr }); coff.getAtomPtr(atom_index).size = code_len; sym.value = vaddr; - try coff.addGotEntry(.{ .sym_index = sym_index }); + coff.addGotEntry(.{ .sym_index = sym_index }) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| return coff.base.cgFail(nav_index, "failed to add GOT entry: {s}", .{@errorName(e)}), + }; } - try coff.writeAtom(atom_index, code); + coff.writeAtom(atom_index, code) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| return coff.base.cgFail(nav_index, "failed to write atom: {s}", .{@errorName(e)}), + }; } pub fn freeNav(coff: *Coff, nav_index: InternPool.NavIndex) void { if (coff.llvm_object) |llvm_object| return llvm_object.freeNav(nav_index); const gpa = coff.base.comp.gpa; - log.debug("freeDecl 0x{x}", .{nav_index}); if (coff.decls.fetchOrderedRemove(nav_index)) |const_kv| { var kv = const_kv; @@ -1466,7 +1478,7 @@ pub fn updateExports( coff: *Coff, pt: Zcu.PerThread, exported: Zcu.Exported, - export_indices: []const u32, + export_indices: []const Zcu.Export.Index, ) link.File.UpdateExportsError!void { if (build_options.skip_non_native and builtin.object_format != .coff) { @panic("Attempted to compile for object format that was disabled by build configuration"); @@ -1481,7 +1493,7 @@ pub fn updateExports( // Even in the case of LLVM, we need to notice certain exported symbols in order to // detect the default subsystem. for (export_indices) |export_idx| { - const exp = zcu.all_exports.items[export_idx]; + const exp = export_idx.ptr(zcu); const exported_nav_index = switch (exp.exported) { .nav => |nav| nav, .uav => continue, @@ -1524,7 +1536,7 @@ pub fn updateExports( break :blk coff.navs.getPtr(nav).?; }, .uav => |uav| coff.uavs.getPtr(uav) orelse blk: { - const first_exp = zcu.all_exports.items[export_indices[0]]; + const first_exp = export_indices[0].ptr(zcu); const res = try coff.lowerUav(pt, uav, .none, first_exp.src); switch (res) { .mcv => {}, @@ -1543,7 +1555,7 @@ pub fn updateExports( const atom = coff.getAtom(atom_index); for (export_indices) |export_idx| { - const exp = zcu.all_exports.items[export_idx]; + const exp = export_idx.ptr(zcu); log.debug("adding new export '{}'", .{exp.opts.name.fmt(&zcu.intern_pool)}); if (exp.opts.section.toSlice(&zcu.intern_pool)) |section_name| { @@ -1671,12 +1683,17 @@ fn resolveGlobalSymbol(coff: *Coff, current: SymbolWithLoc) !void { pub fn flush(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { const comp = coff.base.comp; const use_lld = build_options.have_llvm and comp.config.use_lld; + const diags = &comp.link_diags; if (use_lld) { - return coff.linkWithLLD(arena, tid, prog_node); + return coff.linkWithLLD(arena, tid, prog_node) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to link with LLD: {s}", .{@errorName(e)}), + }; } switch (comp.config.output_mode) { .Exe, .Obj => return coff.flushModule(arena, tid, prog_node), - .Lib => return error.TODOImplementWritingLibFiles, + .Lib => return diags.fail("writing lib files not yet implemented for COFF", .{}), } } @@ -2207,12 +2224,16 @@ fn findLib(arena: Allocator, name: []const u8, lib_directories: []const Director return null; } -pub fn flushModule(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { +pub fn flushModule( + coff: *Coff, + arena: Allocator, + tid: Zcu.PerThread.Id, + prog_node: std.Progress.Node, +) link.File.FlushError!void { const tracy = trace(@src()); defer tracy.end(); const comp = coff.base.comp; - const gpa = comp.gpa; const diags = &comp.link_diags; if (coff.llvm_object) |llvm_object| { @@ -2223,8 +2244,22 @@ pub fn flushModule(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_no const sub_prog_node = prog_node.start("COFF Flush", 0); defer sub_prog_node.end(); + return flushModuleInner(coff, arena, tid) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("COFF flush failed: {s}", .{@errorName(e)}), + }; +} + +fn flushModuleInner(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id) !void { + _ = arena; + + const comp = coff.base.comp; + const gpa = comp.gpa; + const diags = &comp.link_diags; + const pt: Zcu.PerThread = .activate( - comp.zcu orelse return error.LinkingWithoutZigSourceUnimplemented, + comp.zcu orelse return diags.fail("linking without zig source is not yet implemented", .{}), tid, ); defer pt.deactivate(); @@ -2232,24 +2267,18 @@ pub fn flushModule(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_no if (coff.lazy_syms.getPtr(.anyerror_type)) |metadata| { // Most lazy symbols can be updated on first use, but // anyerror needs to wait for everything to be flushed. - if (metadata.text_state != .unused) coff.updateLazySymbolAtom( + if (metadata.text_state != .unused) try coff.updateLazySymbolAtom( pt, .{ .kind = .code, .ty = .anyerror_type }, metadata.text_atom, coff.text_section_index.?, - ) catch |err| return switch (err) { - error.CodegenFail => error.FlushFailure, - else => |e| e, - }; - if (metadata.rdata_state != .unused) coff.updateLazySymbolAtom( + ); + if (metadata.rdata_state != .unused) try coff.updateLazySymbolAtom( pt, .{ .kind = .const_data, .ty = .anyerror_type }, metadata.rdata_atom, coff.rdata_section_index.?, - ) catch |err| return switch (err) { - error.CodegenFail => error.FlushFailure, - else => |e| e, - }; + ); } for (coff.lazy_syms.values()) |*metadata| { if (metadata.text_state != .unused) metadata.text_state = .flushed; @@ -2594,7 +2623,7 @@ fn writeBaseRelocations(coff: *Coff) !void { const needed_size = @as(u32, @intCast(buffer.items.len)); try coff.growSection(coff.reloc_section_index.?, needed_size); - try coff.base.file.?.pwriteAll(buffer.items, header.pointer_to_raw_data); + try coff.pwriteAll(buffer.items, header.pointer_to_raw_data); coff.data_directories[@intFromEnum(coff_util.DirectoryEntry.BASERELOC)] = .{ .virtual_address = header.virtual_address, @@ -2727,7 +2756,7 @@ fn writeImportTables(coff: *Coff) !void { assert(dll_names_offset == needed_size); - try coff.base.file.?.pwriteAll(buffer.items, header.pointer_to_raw_data); + try coff.pwriteAll(buffer.items, header.pointer_to_raw_data); coff.data_directories[@intFromEnum(coff_util.DirectoryEntry.IMPORT)] = .{ .virtual_address = header.virtual_address + iat_size, @@ -2744,17 +2773,19 @@ fn writeImportTables(coff: *Coff) !void { fn writeStrtab(coff: *Coff) !void { if (coff.strtab_offset == null) return; + const comp = coff.base.comp; + const gpa = comp.gpa; + const diags = &comp.link_diags; const allocated_size = coff.allocatedSize(coff.strtab_offset.?); - const needed_size = @as(u32, @intCast(coff.strtab.buffer.items.len)); + const needed_size: u32 = @intCast(coff.strtab.buffer.items.len); if (needed_size > allocated_size) { coff.strtab_offset = null; - coff.strtab_offset = @as(u32, @intCast(coff.findFreeSpace(needed_size, @alignOf(u32)))); + coff.strtab_offset = @intCast(coff.findFreeSpace(needed_size, @alignOf(u32))); } log.debug("writing strtab from 0x{x} to 0x{x}", .{ coff.strtab_offset.?, coff.strtab_offset.? + needed_size }); - const gpa = coff.base.comp.gpa; var buffer = std.ArrayList(u8).init(gpa); defer buffer.deinit(); try buffer.ensureTotalCapacityPrecise(needed_size); @@ -2763,17 +2794,19 @@ fn writeStrtab(coff: *Coff) !void { // we write the length of the strtab to a temporary buffer that goes to file. mem.writeInt(u32, buffer.items[0..4], @as(u32, @intCast(coff.strtab.buffer.items.len)), .little); - try coff.base.file.?.pwriteAll(buffer.items, coff.strtab_offset.?); + coff.pwriteAll(buffer.items, coff.strtab_offset.?) catch |err| { + return diags.fail("failed to write: {s}", .{@errorName(err)}); + }; } fn writeSectionHeaders(coff: *Coff) !void { const offset = coff.getSectionHeadersOffset(); - try coff.base.file.?.pwriteAll(mem.sliceAsBytes(coff.sections.items(.header)), offset); + try coff.pwriteAll(mem.sliceAsBytes(coff.sections.items(.header)), offset); } fn writeDataDirectoriesHeaders(coff: *Coff) !void { const offset = coff.getDataDirectoryHeadersOffset(); - try coff.base.file.?.pwriteAll(mem.sliceAsBytes(&coff.data_directories), offset); + try coff.pwriteAll(mem.sliceAsBytes(&coff.data_directories), offset); } fn writeHeader(coff: *Coff) !void { @@ -2913,7 +2946,7 @@ fn writeHeader(coff: *Coff) !void { }, } - try coff.base.file.?.pwriteAll(buffer.items, 0); + try coff.pwriteAll(buffer.items, 0); } pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { @@ -3710,6 +3743,14 @@ const ImportTable = struct { const ImportIndex = u32; }; +fn pwriteAll(coff: *Coff, bytes: []const u8, offset: u64) error{LinkFailure}!void { + const comp = coff.base.comp; + const diags = &comp.link_diags; + coff.base.file.?.pwriteAll(bytes, offset) catch |err| { + return diags.fail("failed to write: {s}", .{@errorName(err)}); + }; +} + const Coff = @This(); const std = @import("std"); diff --git a/src/link/Dwarf.zig b/src/link/Dwarf.zig index 2aa04a5efd..f76c483853 100644 --- a/src/link/Dwarf.zig +++ b/src/link/Dwarf.zig @@ -21,7 +21,6 @@ debug_rnglists: DebugRngLists, debug_str: StringSection, pub const UpdateError = error{ - CodegenFail, ReinterpretDeclRef, Unimplemented, OutOfMemory, @@ -451,7 +450,6 @@ pub const Section = struct { const zo = elf_file.zigObjectPtr().?; const atom = zo.symbol(sec.index).atom(elf_file).?; if (atom.prevAtom(elf_file)) |_| { - // FIXME:JK trimming/shrinking has to be reworked on ZigObject/Elf level atom.value += len; } else { const shdr = &elf_file.sections.items(.shdr)[atom.output_section_index]; @@ -600,12 +598,13 @@ const Unit = struct { fn move(unit: *Unit, sec: *Section, dwarf: *Dwarf, new_off: u32) UpdateError!void { if (unit.off == new_off) return; - if (try dwarf.getFile().?.copyRangeAll( + const n = try dwarf.getFile().?.copyRangeAll( sec.off(dwarf) + unit.off, dwarf.getFile().?, sec.off(dwarf) + new_off, unit.len, - ) != unit.len) return error.InputOutput; + ); + if (n != unit.len) return error.InputOutput; unit.off = new_off; } @@ -1891,19 +1890,16 @@ pub const WipNav = struct { const bytes = if (ty.hasRuntimeBits(wip_nav.pt.zcu)) ty.abiSize(wip_nav.pt.zcu) else 0; try uleb128(diw, bytes); if (bytes == 0) return; - var dim = wip_nav.debug_info.toManaged(wip_nav.dwarf.gpa); - defer wip_nav.debug_info = dim.moveToUnmanaged(); - switch (try codegen.generateSymbol( + const old_len = wip_nav.debug_info.items.len; + try codegen.generateSymbol( wip_nav.dwarf.bin_file, wip_nav.pt, src_loc, val, - &dim, + &wip_nav.debug_info, .{ .debug_output = .{ .dwarf = wip_nav } }, - )) { - .ok => assert(dim.items.len == wip_nav.debug_info.items.len + bytes), - .fail => unreachable, - } + ); + assert(old_len + bytes == wip_nav.debug_info.items.len); } const AbbrevCodeForForm = struct { @@ -2278,7 +2274,7 @@ pub fn deinit(dwarf: *Dwarf) void { dwarf.* = undefined; } -fn getUnit(dwarf: *Dwarf, mod: *Module) UpdateError!Unit.Index { +fn getUnit(dwarf: *Dwarf, mod: *Module) !Unit.Index { const mod_gop = try dwarf.mods.getOrPut(dwarf.gpa, mod); const unit: Unit.Index = @enumFromInt(mod_gop.index); if (!mod_gop.found_existing) { @@ -2338,7 +2334,24 @@ fn getModInfo(dwarf: *Dwarf, unit: Unit.Index) *ModInfo { return &dwarf.mods.values()[@intFromEnum(unit)]; } -pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index, sym_index: u32) UpdateError!?WipNav { +pub fn initWipNav( + dwarf: *Dwarf, + pt: Zcu.PerThread, + nav_index: InternPool.Nav.Index, + sym_index: u32, +) error{ OutOfMemory, CodegenFail }!?WipNav { + return initWipNavInner(dwarf, pt, nav_index, sym_index) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| return pt.zcu.codegenFail(nav_index, "failed to init dwarf: {s}", .{@errorName(e)}), + }; +} + +fn initWipNavInner( + dwarf: *Dwarf, + pt: Zcu.PerThread, + nav_index: InternPool.Nav.Index, + sym_index: u32, +) !?WipNav { const zcu = pt.zcu; const ip = &zcu.intern_pool; @@ -2667,7 +2680,14 @@ pub fn finishWipNav( try wip_nav.updateLazy(zcu.navSrcLoc(nav_index)); } -pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) UpdateError!void { +pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) error{ OutOfMemory, CodegenFail }!void { + return updateComptimeNavInner(dwarf, pt, nav_index) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| return pt.zcu.codegenFail(nav_index, "failed to update dwarf: {s}", .{@errorName(e)}), + }; +} + +fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) !void { const zcu = pt.zcu; const ip = &zcu.intern_pool; const nav_src_loc = zcu.navSrcLoc(nav_index); diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 716a1ee59c..ea2fa56a5d 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -795,9 +795,15 @@ pub fn loadInput(self: *Elf, input: link.Input) !void { } pub fn flush(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { - const use_lld = build_options.have_llvm and self.base.comp.config.use_lld; + const comp = self.base.comp; + const use_lld = build_options.have_llvm and comp.config.use_lld; + const diags = &comp.link_diags; if (use_lld) { - return self.linkWithLLD(arena, tid, prog_node); + return self.linkWithLLD(arena, tid, prog_node) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to link with LLD: {s}", .{@errorName(e)}), + }; } try self.flushModule(arena, tid, prog_node); } @@ -807,7 +813,6 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod defer tracy.end(); const comp = self.base.comp; - const gpa = comp.gpa; const diags = &comp.link_diags; if (self.llvm_object) |llvm_object| { @@ -821,6 +826,18 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod const sub_prog_node = prog_node.start("ELF Flush", 0); defer sub_prog_node.end(); + return flushModuleInner(self, arena, tid) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("ELF flush failed: {s}", .{@errorName(e)}), + }; +} + +fn flushModuleInner(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id) !void { + const comp = self.base.comp; + const gpa = comp.gpa; + const diags = &comp.link_diags; + const module_obj_path: ?Path = if (self.base.zcu_object_sub_path) |path| .{ .root_dir = self.base.emit.root_dir, .sub_path = if (fs.path.dirname(self.base.emit.sub_path)) |dirname| @@ -842,12 +859,12 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod .Exe => {}, } - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; // If we haven't already, create a linker-generated input file comprising of // linker-defined synthetic symbols only such as `_DYNAMIC`, etc. if (self.linker_defined_index == null) { - const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); + const index: File.Index = @intCast(try self.files.addOne(gpa)); self.files.set(index, .{ .linker_defined = .{ .index = index } }); self.linker_defined_index = index; const object = self.linkerDefinedPtr().?; @@ -878,7 +895,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod } self.checkDuplicates() catch |err| switch (err) { - error.HasDuplicates => return error.FlushFailure, + error.HasDuplicates => return error.LinkFailure, else => |e| return e, }; @@ -956,14 +973,14 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod error.RelocFailure, error.RelaxFailure => has_reloc_errors = true, error.UnsupportedCpuArch => { try self.reportUnsupportedCpuArch(); - return error.FlushFailure; + return error.LinkFailure; }, else => |e| return e, }; - try self.base.file.?.pwriteAll(code, file_offset); + try self.pwriteAll(code, file_offset); } - if (has_reloc_errors) return error.FlushFailure; + if (has_reloc_errors) return error.LinkFailure; } try self.writePhdrTable(); @@ -972,10 +989,10 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod try self.writeMergeSections(); self.writeSyntheticSections() catch |err| switch (err) { - error.RelocFailure => return error.FlushFailure, + error.RelocFailure => return error.LinkFailure, error.UnsupportedCpuArch => { try self.reportUnsupportedCpuArch(); - return error.FlushFailure; + return error.LinkFailure; }, else => |e| return e, }; @@ -989,7 +1006,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod try self.writeElfHeader(); } - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; } fn dumpArgvInit(self: *Elf, arena: Allocator) !void { @@ -1389,7 +1406,7 @@ fn scanRelocs(self: *Elf) !void { error.RelaxFailure => unreachable, error.UnsupportedCpuArch => { try self.reportUnsupportedCpuArch(); - return error.FlushFailure; + return error.LinkFailure; }, error.RelocFailure => has_reloc_errors = true, else => |e| return e, @@ -1400,7 +1417,7 @@ fn scanRelocs(self: *Elf) !void { error.RelaxFailure => unreachable, error.UnsupportedCpuArch => { try self.reportUnsupportedCpuArch(); - return error.FlushFailure; + return error.LinkFailure; }, error.RelocFailure => has_reloc_errors = true, else => |e| return e, @@ -1409,7 +1426,7 @@ fn scanRelocs(self: *Elf) !void { try self.reportUndefinedSymbols(&undefs); - if (has_reloc_errors) return error.FlushFailure; + if (has_reloc_errors) return error.LinkFailure; if (self.zigObjectPtr()) |zo| { try zo.asFile().createSymbolIndirection(self); @@ -2117,7 +2134,7 @@ pub fn writeShdrTable(self: *Elf) !void { mem.byteSwapAllFields(elf.Elf32_Shdr, shdr); } } - try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), self.shdr_table_offset.?); + try self.pwriteAll(mem.sliceAsBytes(buf), self.shdr_table_offset.?); }, .p64 => { const buf = try gpa.alloc(elf.Elf64_Shdr, self.sections.items(.shdr).len); @@ -2130,7 +2147,7 @@ pub fn writeShdrTable(self: *Elf) !void { mem.byteSwapAllFields(elf.Elf64_Shdr, shdr); } } - try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), self.shdr_table_offset.?); + try self.pwriteAll(mem.sliceAsBytes(buf), self.shdr_table_offset.?); }, } } @@ -2157,7 +2174,7 @@ fn writePhdrTable(self: *Elf) !void { mem.byteSwapAllFields(elf.Elf32_Phdr, phdr); } } - try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), phdr_table.p_offset); + try self.pwriteAll(mem.sliceAsBytes(buf), phdr_table.p_offset); }, .p64 => { const buf = try gpa.alloc(elf.Elf64_Phdr, self.phdrs.items.len); @@ -2169,7 +2186,7 @@ fn writePhdrTable(self: *Elf) !void { mem.byteSwapAllFields(elf.Elf64_Phdr, phdr); } } - try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), phdr_table.p_offset); + try self.pwriteAll(mem.sliceAsBytes(buf), phdr_table.p_offset); }, } } @@ -2319,7 +2336,7 @@ pub fn writeElfHeader(self: *Elf) !void { assert(index == e_ehsize); - try self.base.file.?.pwriteAll(hdr_buf[0..index], 0); + try self.pwriteAll(hdr_buf[0..index], 0); } pub fn freeNav(self: *Elf, nav: InternPool.Nav.Index) void { @@ -2327,7 +2344,13 @@ pub fn freeNav(self: *Elf, nav: InternPool.Nav.Index) void { return self.zigObjectPtr().?.freeNav(self, nav); } -pub fn updateFunc(self: *Elf, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { +pub fn updateFunc( + self: *Elf, + pt: Zcu.PerThread, + func_index: InternPool.Index, + air: Air, + liveness: Liveness, +) link.File.UpdateNavError!void { if (build_options.skip_non_native and builtin.object_format != .elf) { @panic("Attempted to compile for object format that was disabled by build configuration"); } @@ -2351,19 +2374,32 @@ pub fn updateContainerType( self: *Elf, pt: Zcu.PerThread, ty: InternPool.Index, -) link.File.UpdateNavError!void { +) link.File.UpdateContainerTypeError!void { if (build_options.skip_non_native and builtin.object_format != .elf) { @panic("Attempted to compile for object format that was disabled by build configuration"); } if (self.llvm_object) |_| return; - return self.zigObjectPtr().?.updateContainerType(pt, ty); + const zcu = pt.zcu; + const gpa = zcu.gpa; + return self.zigObjectPtr().?.updateContainerType(pt, ty) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| { + try zcu.failed_types.putNoClobber(gpa, ty, try Zcu.ErrorMsg.create( + gpa, + zcu.typeSrcLoc(ty), + "failed to update container type: {s}", + .{@errorName(e)}, + )); + return error.TypeFailureReported; + }, + }; } pub fn updateExports( self: *Elf, pt: Zcu.PerThread, exported: Zcu.Exported, - export_indices: []const u32, + export_indices: []const Zcu.Export.Index, ) link.File.UpdateExportsError!void { if (build_options.skip_non_native and builtin.object_format != .elf) { @panic("Attempted to compile for object format that was disabled by build configuration"); @@ -2441,7 +2477,7 @@ pub fn resolveMergeSections(self: *Elf) !void { }; } - if (has_errors) return error.FlushFailure; + if (has_errors) return error.LinkFailure; for (self.objects.items) |index| { const object = self.file(index).?.object; @@ -2491,8 +2527,8 @@ pub fn writeMergeSections(self: *Elf) !void { for (self.merge_sections.items) |*msec| { const shdr = self.sections.items(.shdr)[msec.output_section_index]; - const fileoff = math.cast(usize, msec.value + shdr.sh_offset) orelse return error.Overflow; - const size = math.cast(usize, msec.size) orelse return error.Overflow; + const fileoff = try self.cast(usize, msec.value + shdr.sh_offset); + const size = try self.cast(usize, msec.size); try buffer.ensureTotalCapacity(size); buffer.appendNTimesAssumeCapacity(0, size); @@ -2500,11 +2536,11 @@ pub fn writeMergeSections(self: *Elf) !void { const msub = msec.mergeSubsection(msub_index); assert(msub.alive); const string = msub.getString(self); - const off = math.cast(usize, msub.value) orelse return error.Overflow; + const off = try self.cast(usize, msub.value); @memcpy(buffer.items[off..][0..string.len], string); } - try self.base.file.?.pwriteAll(buffer.items, fileoff); + try self.pwriteAll(buffer.items, fileoff); buffer.clearRetainingCapacity(); } } @@ -3121,9 +3157,6 @@ pub fn sortShdrs( fileLookup(files, ref.file, zig_object_ptr).?.atom(ref.index).?.output_section_index = atom_list.output_section_index; } if (shdr.sh_type == elf.SHT_RELA) { - // FIXME:JK we should spin up .symtab potentially earlier, or set all non-dynamic RELA sections - // to point at symtab - // shdr.sh_link = backlinks[shdr.sh_link]; shdr.sh_link = section_indexes.symtab.?; shdr.sh_info = backlinks[shdr.sh_info]; } @@ -3211,7 +3244,7 @@ fn updateSectionSizes(self: *Elf) !void { atom_list.dirty = false; } - // FIXME:JK this will hopefully not be needed once we create a link from Atom/Thunk to AtomList. + // This might not be needed if there was a link from Atom/Thunk to AtomList. for (self.thunks.items) |*th| { th.value += slice.items(.atom_list_2)[th.output_section_index].value; } @@ -3297,7 +3330,6 @@ fn updateSectionSizes(self: *Elf) !void { self.updateShStrtabSize(); } -// FIXME:JK this is very much obsolete, remove! pub fn updateShStrtabSize(self: *Elf) void { if (self.section_indexes.shstrtab) |index| { self.sections.items(.shdr)[index].sh_size = self.shstrtab.items.len; @@ -3362,7 +3394,7 @@ fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void { // TODO verify `getMaxNumberOfPhdrs()` is accurate and convert this into no-op var err = try diags.addErrorWithNotes(1); try err.addMsg("fatal linker error: not enough space reserved for EHDR and PHDR table", .{}); - try err.addNote("required 0x{x}, available 0x{x}", .{ needed_size, available_space }); + err.addNote("required 0x{x}, available 0x{x}", .{ needed_size, available_space }); } phdr_table_load.p_filesz = needed_size + ehsize; @@ -3658,7 +3690,7 @@ fn writeAtoms(self: *Elf) !void { atom_list.write(&buffer, &undefs, self) catch |err| switch (err) { error.UnsupportedCpuArch => { try self.reportUnsupportedCpuArch(); - return error.FlushFailure; + return error.LinkFailure; }, error.RelocFailure, error.RelaxFailure => has_reloc_errors = true, else => |e| return e, @@ -3666,7 +3698,7 @@ fn writeAtoms(self: *Elf) !void { } try self.reportUndefinedSymbols(&undefs); - if (has_reloc_errors) return error.FlushFailure; + if (has_reloc_errors) return error.LinkFailure; if (self.requiresThunks()) { for (self.thunks.items) |th| { @@ -3676,7 +3708,7 @@ fn writeAtoms(self: *Elf) !void { const offset = @as(u64, @intCast(th.value)) + shdr.sh_offset; try th.write(self, buffer.writer()); assert(buffer.items.len == thunk_size); - try self.base.file.?.pwriteAll(buffer.items, offset); + try self.pwriteAll(buffer.items, offset); buffer.clearRetainingCapacity(); } } @@ -3784,12 +3816,12 @@ fn writeSyntheticSections(self: *Elf) !void { const contents = buffer[0 .. interp.len + 1]; const shdr = slice.items(.shdr)[shndx]; assert(shdr.sh_size == contents.len); - try self.base.file.?.pwriteAll(contents, shdr.sh_offset); + try self.pwriteAll(contents, shdr.sh_offset); } if (self.section_indexes.hash) |shndx| { const shdr = slice.items(.shdr)[shndx]; - try self.base.file.?.pwriteAll(self.hash.buffer.items, shdr.sh_offset); + try self.pwriteAll(self.hash.buffer.items, shdr.sh_offset); } if (self.section_indexes.gnu_hash) |shndx| { @@ -3797,12 +3829,12 @@ fn writeSyntheticSections(self: *Elf) !void { var buffer = try std.ArrayList(u8).initCapacity(gpa, self.gnu_hash.size()); defer buffer.deinit(); try self.gnu_hash.write(self, buffer.writer()); - try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); + try self.pwriteAll(buffer.items, shdr.sh_offset); } if (self.section_indexes.versym) |shndx| { const shdr = slice.items(.shdr)[shndx]; - try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.versym.items), shdr.sh_offset); + try self.pwriteAll(mem.sliceAsBytes(self.versym.items), shdr.sh_offset); } if (self.section_indexes.verneed) |shndx| { @@ -3810,7 +3842,7 @@ fn writeSyntheticSections(self: *Elf) !void { var buffer = try std.ArrayList(u8).initCapacity(gpa, self.verneed.size()); defer buffer.deinit(); try self.verneed.write(buffer.writer()); - try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); + try self.pwriteAll(buffer.items, shdr.sh_offset); } if (self.section_indexes.dynamic) |shndx| { @@ -3818,7 +3850,7 @@ fn writeSyntheticSections(self: *Elf) !void { var buffer = try std.ArrayList(u8).initCapacity(gpa, self.dynamic.size(self)); defer buffer.deinit(); try self.dynamic.write(self, buffer.writer()); - try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); + try self.pwriteAll(buffer.items, shdr.sh_offset); } if (self.section_indexes.dynsymtab) |shndx| { @@ -3826,12 +3858,12 @@ fn writeSyntheticSections(self: *Elf) !void { var buffer = try std.ArrayList(u8).initCapacity(gpa, self.dynsym.size()); defer buffer.deinit(); try self.dynsym.write(self, buffer.writer()); - try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); + try self.pwriteAll(buffer.items, shdr.sh_offset); } if (self.section_indexes.dynstrtab) |shndx| { const shdr = slice.items(.shdr)[shndx]; - try self.base.file.?.pwriteAll(self.dynstrtab.items, shdr.sh_offset); + try self.pwriteAll(self.dynstrtab.items, shdr.sh_offset); } if (self.section_indexes.eh_frame) |shndx| { @@ -3841,21 +3873,21 @@ fn writeSyntheticSections(self: *Elf) !void { break :existing_size sym.atom(self).?.size; }; const shdr = slice.items(.shdr)[shndx]; - const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow; + const sh_size = try self.cast(usize, shdr.sh_size); var buffer = try std.ArrayList(u8).initCapacity(gpa, @intCast(sh_size - existing_size)); defer buffer.deinit(); try eh_frame.writeEhFrame(self, buffer.writer()); assert(buffer.items.len == sh_size - existing_size); - try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset + existing_size); + try self.pwriteAll(buffer.items, shdr.sh_offset + existing_size); } if (self.section_indexes.eh_frame_hdr) |shndx| { const shdr = slice.items(.shdr)[shndx]; - const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow; + const sh_size = try self.cast(usize, shdr.sh_size); var buffer = try std.ArrayList(u8).initCapacity(gpa, sh_size); defer buffer.deinit(); try eh_frame.writeEhFrameHdr(self, buffer.writer()); - try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); + try self.pwriteAll(buffer.items, shdr.sh_offset); } if (self.section_indexes.got) |index| { @@ -3863,7 +3895,7 @@ fn writeSyntheticSections(self: *Elf) !void { var buffer = try std.ArrayList(u8).initCapacity(gpa, self.got.size(self)); defer buffer.deinit(); try self.got.write(self, buffer.writer()); - try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); + try self.pwriteAll(buffer.items, shdr.sh_offset); } if (self.section_indexes.rela_dyn) |shndx| { @@ -3871,7 +3903,7 @@ fn writeSyntheticSections(self: *Elf) !void { try self.got.addRela(self); try self.copy_rel.addRela(self); self.sortRelaDyn(); - try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.rela_dyn.items), shdr.sh_offset); + try self.pwriteAll(mem.sliceAsBytes(self.rela_dyn.items), shdr.sh_offset); } if (self.section_indexes.plt) |shndx| { @@ -3879,7 +3911,7 @@ fn writeSyntheticSections(self: *Elf) !void { var buffer = try std.ArrayList(u8).initCapacity(gpa, self.plt.size(self)); defer buffer.deinit(); try self.plt.write(self, buffer.writer()); - try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); + try self.pwriteAll(buffer.items, shdr.sh_offset); } if (self.section_indexes.got_plt) |shndx| { @@ -3887,7 +3919,7 @@ fn writeSyntheticSections(self: *Elf) !void { var buffer = try std.ArrayList(u8).initCapacity(gpa, self.got_plt.size(self)); defer buffer.deinit(); try self.got_plt.write(self, buffer.writer()); - try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); + try self.pwriteAll(buffer.items, shdr.sh_offset); } if (self.section_indexes.plt_got) |shndx| { @@ -3895,25 +3927,24 @@ fn writeSyntheticSections(self: *Elf) !void { var buffer = try std.ArrayList(u8).initCapacity(gpa, self.plt_got.size(self)); defer buffer.deinit(); try self.plt_got.write(self, buffer.writer()); - try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); + try self.pwriteAll(buffer.items, shdr.sh_offset); } if (self.section_indexes.rela_plt) |shndx| { const shdr = slice.items(.shdr)[shndx]; try self.plt.addRela(self); - try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.rela_plt.items), shdr.sh_offset); + try self.pwriteAll(mem.sliceAsBytes(self.rela_plt.items), shdr.sh_offset); } try self.writeSymtab(); try self.writeShStrtab(); } -// FIXME:JK again, why is this needed? pub fn writeShStrtab(self: *Elf) !void { if (self.section_indexes.shstrtab) |index| { const shdr = self.sections.items(.shdr)[index]; log.debug("writing .shstrtab from 0x{x} to 0x{x}", .{ shdr.sh_offset, shdr.sh_offset + shdr.sh_size }); - try self.base.file.?.pwriteAll(self.shstrtab.items, shdr.sh_offset); + try self.pwriteAll(self.shstrtab.items, shdr.sh_offset); } } @@ -3928,7 +3959,7 @@ pub fn writeSymtab(self: *Elf) !void { .p32 => @sizeOf(elf.Elf32_Sym), .p64 => @sizeOf(elf.Elf64_Sym), }; - const nsyms = math.cast(usize, @divExact(symtab_shdr.sh_size, sym_size)) orelse return error.Overflow; + const nsyms = try self.cast(usize, @divExact(symtab_shdr.sh_size, sym_size)); log.debug("writing {d} symbols in .symtab from 0x{x} to 0x{x}", .{ nsyms, @@ -3941,7 +3972,7 @@ pub fn writeSymtab(self: *Elf) !void { }); try self.symtab.resize(gpa, nsyms); - const needed_strtab_size = math.cast(usize, strtab_shdr.sh_size - 1) orelse return error.Overflow; + const needed_strtab_size = try self.cast(usize, strtab_shdr.sh_size - 1); // TODO we could resize instead and in ZigObject/Object always access as slice self.strtab.clearRetainingCapacity(); self.strtab.appendAssumeCapacity(0); @@ -4010,17 +4041,17 @@ pub fn writeSymtab(self: *Elf) !void { }; if (foreign_endian) mem.byteSwapAllFields(elf.Elf32_Sym, out); } - try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), symtab_shdr.sh_offset); + try self.pwriteAll(mem.sliceAsBytes(buf), symtab_shdr.sh_offset); }, .p64 => { if (foreign_endian) { for (self.symtab.items) |*sym| mem.byteSwapAllFields(elf.Elf64_Sym, sym); } - try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.symtab.items), symtab_shdr.sh_offset); + try self.pwriteAll(mem.sliceAsBytes(self.symtab.items), symtab_shdr.sh_offset); }, } - try self.base.file.?.pwriteAll(self.strtab.items, strtab_shdr.sh_offset); + try self.pwriteAll(self.strtab.items, strtab_shdr.sh_offset); } /// Always 4 or 8 depending on whether this is 32-bit ELF or 64-bit ELF. @@ -4514,12 +4545,12 @@ fn reportUndefinedSymbols(self: *Elf, undefs: anytype) !void { for (refs.items[0..nrefs]) |ref| { const atom_ptr = self.atom(ref).?; const file_ptr = atom_ptr.file(self).?; - try err.addNote("referenced by {s}:{s}", .{ file_ptr.fmtPath(), atom_ptr.name(self) }); + err.addNote("referenced by {s}:{s}", .{ file_ptr.fmtPath(), atom_ptr.name(self) }); } if (refs.items.len > max_notes) { const remaining = refs.items.len - max_notes; - try err.addNote("referenced {d} more times", .{remaining}); + err.addNote("referenced {d} more times", .{remaining}); } } } @@ -4536,17 +4567,17 @@ fn reportDuplicates(self: *Elf, dupes: anytype) error{ HasDuplicates, OutOfMemor var err = try diags.addErrorWithNotes(nnotes + 1); try err.addMsg("duplicate symbol definition: {s}", .{sym.name(self)}); - try err.addNote("defined by {}", .{sym.file(self).?.fmtPath()}); + err.addNote("defined by {}", .{sym.file(self).?.fmtPath()}); var inote: usize = 0; while (inote < @min(notes.items.len, max_notes)) : (inote += 1) { const file_ptr = self.file(notes.items[inote]).?; - try err.addNote("defined by {}", .{file_ptr.fmtPath()}); + err.addNote("defined by {}", .{file_ptr.fmtPath()}); } if (notes.items.len > max_notes) { const remaining = notes.items.len - max_notes; - try err.addNote("defined {d} more times", .{remaining}); + err.addNote("defined {d} more times", .{remaining}); } } @@ -4570,7 +4601,7 @@ pub fn addFileError( const diags = &self.base.comp.link_diags; var err = try diags.addErrorWithNotes(1); try err.addMsg(format, args); - try err.addNote("while parsing {}", .{self.file(file_index).?.fmtPath()}); + err.addNote("while parsing {}", .{self.file(file_index).?.fmtPath()}); } pub fn failFile( @@ -5184,6 +5215,30 @@ pub fn stringTableLookup(strtab: []const u8, off: u32) [:0]const u8 { return slice[0..mem.indexOfScalar(u8, slice, 0).? :0]; } +pub fn pwriteAll(elf_file: *Elf, bytes: []const u8, offset: u64) error{LinkFailure}!void { + const comp = elf_file.base.comp; + const diags = &comp.link_diags; + elf_file.base.file.?.pwriteAll(bytes, offset) catch |err| { + return diags.fail("failed to write: {s}", .{@errorName(err)}); + }; +} + +pub fn setEndPos(elf_file: *Elf, length: u64) error{LinkFailure}!void { + const comp = elf_file.base.comp; + const diags = &comp.link_diags; + elf_file.base.file.?.setEndPos(length) catch |err| { + return diags.fail("failed to set file end pos: {s}", .{@errorName(err)}); + }; +} + +pub fn cast(elf_file: *Elf, comptime T: type, x: anytype) error{LinkFailure}!T { + return std.math.cast(T, x) orelse { + const comp = elf_file.base.comp; + const diags = &comp.link_diags; + return diags.fail("encountered {d}, overflowing {d}-bit value", .{ x, @bitSizeOf(T) }); + }; +} + const std = @import("std"); const build_options = @import("build_options"); const builtin = @import("builtin"); diff --git a/src/link/Elf/Atom.zig b/src/link/Elf/Atom.zig index d34dd6bb63..f0eb0dce3f 100644 --- a/src/link/Elf/Atom.zig +++ b/src/link/Elf/Atom.zig @@ -523,7 +523,7 @@ fn reportUnhandledRelocError(self: Atom, rel: elf.Elf64_Rela, elf_file: *Elf) Re relocation.fmtRelocType(rel.r_type(), elf_file.getTarget().cpu.arch), rel.r_offset, }); - try err.addNote("in {}:{s}", .{ self.file(elf_file).?.fmtPath(), self.name(elf_file) }); + err.addNote("in {}:{s}", .{ self.file(elf_file).?.fmtPath(), self.name(elf_file) }); return error.RelocFailure; } @@ -539,7 +539,7 @@ fn reportTextRelocError( rel.r_offset, symbol.name(elf_file), }); - try err.addNote("in {}:{s}", .{ self.file(elf_file).?.fmtPath(), self.name(elf_file) }); + err.addNote("in {}:{s}", .{ self.file(elf_file).?.fmtPath(), self.name(elf_file) }); return error.RelocFailure; } @@ -555,8 +555,8 @@ fn reportPicError( rel.r_offset, symbol.name(elf_file), }); - try err.addNote("in {}:{s}", .{ self.file(elf_file).?.fmtPath(), self.name(elf_file) }); - try err.addNote("recompile with -fPIC", .{}); + err.addNote("in {}:{s}", .{ self.file(elf_file).?.fmtPath(), self.name(elf_file) }); + err.addNote("recompile with -fPIC", .{}); return error.RelocFailure; } @@ -572,8 +572,8 @@ fn reportNoPicError( rel.r_offset, symbol.name(elf_file), }); - try err.addNote("in {}:{s}", .{ self.file(elf_file).?.fmtPath(), self.name(elf_file) }); - try err.addNote("recompile with -fno-PIC", .{}); + err.addNote("in {}:{s}", .{ self.file(elf_file).?.fmtPath(), self.name(elf_file) }); + err.addNote("recompile with -fno-PIC", .{}); return error.RelocFailure; } @@ -1187,7 +1187,7 @@ const x86_64 = struct { x86_64.relaxGotPcTlsDesc(code[r_offset - 3 ..]) catch { var err = try diags.addErrorWithNotes(1); try err.addMsg("could not relax {s}", .{@tagName(r_type)}); - try err.addNote("in {}:{s} at offset 0x{x}", .{ + err.addNote("in {}:{s} at offset 0x{x}", .{ atom.file(elf_file).?.fmtPath(), atom.name(elf_file), rel.r_offset, @@ -1332,7 +1332,7 @@ const x86_64 = struct { relocation.fmtRelocType(rels[0].r_type(), .x86_64), relocation.fmtRelocType(rels[1].r_type(), .x86_64), }); - try err.addNote("in {}:{s} at offset 0x{x}", .{ + err.addNote("in {}:{s} at offset 0x{x}", .{ self.file(elf_file).?.fmtPath(), self.name(elf_file), rels[0].r_offset, @@ -1388,7 +1388,7 @@ const x86_64 = struct { relocation.fmtRelocType(rels[0].r_type(), .x86_64), relocation.fmtRelocType(rels[1].r_type(), .x86_64), }); - try err.addNote("in {}:{s} at offset 0x{x}", .{ + err.addNote("in {}:{s} at offset 0x{x}", .{ self.file(elf_file).?.fmtPath(), self.name(elf_file), rels[0].r_offset, @@ -1485,7 +1485,7 @@ const x86_64 = struct { relocation.fmtRelocType(rels[0].r_type(), .x86_64), relocation.fmtRelocType(rels[1].r_type(), .x86_64), }); - try err.addNote("in {}:{s} at offset 0x{x}", .{ + err.addNote("in {}:{s} at offset 0x{x}", .{ self.file(elf_file).?.fmtPath(), self.name(elf_file), rels[0].r_offset, @@ -1672,7 +1672,7 @@ const aarch64 = struct { // TODO: relax var err = try diags.addErrorWithNotes(1); try err.addMsg("TODO: relax ADR_GOT_PAGE", .{}); - try err.addNote("in {}:{s} at offset 0x{x}", .{ + err.addNote("in {}:{s} at offset 0x{x}", .{ atom.file(elf_file).?.fmtPath(), atom.name(elf_file), r_offset, @@ -1959,7 +1959,7 @@ const riscv = struct { // TODO: implement searching forward var err = try diags.addErrorWithNotes(1); try err.addMsg("TODO: find HI20 paired reloc scanning forward", .{}); - try err.addNote("in {}:{s} at offset 0x{x}", .{ + err.addNote("in {}:{s} at offset 0x{x}", .{ atom.file(elf_file).?.fmtPath(), atom.name(elf_file), rel.r_offset, diff --git a/src/link/Elf/AtomList.zig b/src/link/Elf/AtomList.zig index bab4726f24..f8d57d04a1 100644 --- a/src/link/Elf/AtomList.zig +++ b/src/link/Elf/AtomList.zig @@ -58,7 +58,7 @@ pub fn allocate(list: *AtomList, elf_file: *Elf) !void { if (expand_section) last_atom_ref.* = list.lastAtom(elf_file).ref(); shdr.sh_addralign = @max(shdr.sh_addralign, list.alignment.toByteUnits().?); - // FIXME:JK this currently ignores Thunks as valid chunks. + // This currently ignores Thunks as valid chunks. { var idx: usize = 0; while (idx < list.atoms.keys().len) : (idx += 1) { @@ -78,7 +78,8 @@ pub fn allocate(list: *AtomList, elf_file: *Elf) !void { placement_atom.next_atom_ref = list.firstAtom(elf_file).ref(); } - // FIXME:JK if we had a link from Atom to parent AtomList we would not need to update Atom's value or osec index + // If we had a link from Atom to parent AtomList we would not need to + // update Atom's value or osec index. for (list.atoms.keys()) |ref| { const atom_ptr = elf_file.atom(ref).?; atom_ptr.output_section_index = list.output_section_index; diff --git a/src/link/Elf/Object.zig b/src/link/Elf/Object.zig index 65a62ff1a6..d6076a5558 100644 --- a/src/link/Elf/Object.zig +++ b/src/link/Elf/Object.zig @@ -797,7 +797,7 @@ pub fn initInputMergeSections(self: *Object, elf_file: *Elf) !void { if (!isNull(data[end .. end + sh_entsize])) { var err = try diags.addErrorWithNotes(1); try err.addMsg("string not null terminated", .{}); - try err.addNote("in {}:{s}", .{ self.fmtPath(), atom_ptr.name(elf_file) }); + err.addNote("in {}:{s}", .{ self.fmtPath(), atom_ptr.name(elf_file) }); return error.LinkFailure; } end += sh_entsize; @@ -812,7 +812,7 @@ pub fn initInputMergeSections(self: *Object, elf_file: *Elf) !void { if (shdr.sh_size % sh_entsize != 0) { var err = try diags.addErrorWithNotes(1); try err.addMsg("size not a multiple of sh_entsize", .{}); - try err.addNote("in {}:{s}", .{ self.fmtPath(), atom_ptr.name(elf_file) }); + err.addNote("in {}:{s}", .{ self.fmtPath(), atom_ptr.name(elf_file) }); return error.LinkFailure; } @@ -889,8 +889,8 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) error{ const res = imsec.findSubsection(@intCast(esym.st_value)) orelse { var err = try diags.addErrorWithNotes(2); try err.addMsg("invalid symbol value: {x}", .{esym.st_value}); - try err.addNote("for symbol {s}", .{sym.name(elf_file)}); - try err.addNote("in {}", .{self.fmtPath()}); + err.addNote("for symbol {s}", .{sym.name(elf_file)}); + err.addNote("in {}", .{self.fmtPath()}); return error.LinkFailure; }; @@ -915,7 +915,7 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) error{ const res = imsec.findSubsection(@intCast(@as(i64, @intCast(esym.st_value)) + rel.r_addend)) orelse { var err = try diags.addErrorWithNotes(1); try err.addMsg("invalid relocation at offset 0x{x}", .{rel.r_offset}); - try err.addNote("in {}:{s}", .{ self.fmtPath(), atom_ptr.name(elf_file) }); + err.addNote("in {}:{s}", .{ self.fmtPath(), atom_ptr.name(elf_file) }); return error.LinkFailure; }; diff --git a/src/link/Elf/ZigObject.zig b/src/link/Elf/ZigObject.zig index 73ea628864..aac29d8a4f 100644 --- a/src/link/Elf/ZigObject.zig +++ b/src/link/Elf/ZigObject.zig @@ -278,8 +278,8 @@ pub fn flush(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !void { .{ .kind = .code, .ty = .anyerror_type }, metadata.text_symbol_index, ) catch |err| return switch (err) { - error.CodegenFail => error.FlushFailure, - else => |e| e, + error.CodegenFail => error.LinkFailure, + else => |e| return e, }; if (metadata.rodata_state != .unused) self.updateLazySymbol( elf_file, @@ -287,8 +287,8 @@ pub fn flush(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !void { .{ .kind = .const_data, .ty = .anyerror_type }, metadata.rodata_symbol_index, ) catch |err| return switch (err) { - error.CodegenFail => error.FlushFailure, - else => |e| e, + error.CodegenFail => error.LinkFailure, + else => |e| return e, }; } for (self.lazy_syms.values()) |*metadata| { @@ -933,6 +933,7 @@ pub fn getNavVAddr( const this_sym = self.symbol(this_sym_index); const vaddr = this_sym.address(.{}, elf_file); switch (reloc_info.parent) { + .none => unreachable, .atom_index => |atom_index| { const parent_atom = self.symbol(atom_index).atom(elf_file).?; const r_type = relocation.encode(.abs, elf_file.getTarget().cpu.arch); @@ -965,6 +966,7 @@ pub fn getUavVAddr( const sym = self.symbol(sym_index); const vaddr = sym.address(.{}, elf_file); switch (reloc_info.parent) { + .none => unreachable, .atom_index => |atom_index| { const parent_atom = self.symbol(atom_index).atom(elf_file).?; const r_type = relocation.encode(.abs, elf_file.getTarget().cpu.arch); @@ -1261,7 +1263,7 @@ fn updateNavCode( shdr_index: u32, code: []const u8, stt_bits: u8, -) !void { +) link.File.UpdateNavError!void { const zcu = pt.zcu; const gpa = zcu.gpa; const ip = &zcu.intern_pool; @@ -1298,7 +1300,9 @@ fn updateNavCode( const capacity = atom_ptr.capacity(elf_file); const need_realloc = code.len > capacity or !required_alignment.check(@intCast(atom_ptr.value)); if (need_realloc) { - try self.allocateAtom(atom_ptr, true, elf_file); + self.allocateAtom(atom_ptr, true, elf_file) catch |err| + return elf_file.base.cgFail(nav_index, "failed to allocate atom: {s}", .{@errorName(err)}); + log.debug("growing {} from 0x{x} to 0x{x}", .{ nav.fqn.fmt(ip), old_vaddr, atom_ptr.value }); if (old_vaddr != atom_ptr.value) { sym.value = 0; @@ -1308,7 +1312,9 @@ fn updateNavCode( // TODO shrink section size } } else { - try self.allocateAtom(atom_ptr, true, elf_file); + self.allocateAtom(atom_ptr, true, elf_file) catch |err| + return elf_file.base.cgFail(nav_index, "failed to allocate atom: {s}", .{@errorName(err)}); + errdefer self.freeNavMetadata(elf_file, sym_index); sym.value = 0; esym.st_value = 0; @@ -1333,14 +1339,15 @@ fn updateNavCode( else => |errno| log.warn("process_vm_writev failure: {s}", .{@tagName(errno)}), } }, - else => return error.HotSwapUnavailableOnHostOperatingSystem, + else => return elf_file.base.cgFail(nav_index, "ELF hot swap unavailable on host operating system '{s}'", .{@tagName(builtin.os.tag)}), } } const shdr = elf_file.sections.items(.shdr)[shdr_index]; if (shdr.sh_type != elf.SHT_NOBITS) { const file_offset = atom_ptr.offset(elf_file); - try elf_file.base.file.?.pwriteAll(code, file_offset); + elf_file.base.file.?.pwriteAll(code, file_offset) catch |err| + return elf_file.base.cgFail(nav_index, "failed to write to output file: {s}", .{@errorName(err)}); log.debug("writing {} from 0x{x} to 0x{x}", .{ nav.fqn.fmt(ip), file_offset, file_offset + code.len }); } } @@ -1353,7 +1360,7 @@ fn updateTlv( sym_index: Symbol.Index, shndx: u32, code: []const u8, -) !void { +) link.File.UpdateNavError!void { const zcu = pt.zcu; const ip = &zcu.intern_pool; const gpa = zcu.gpa; @@ -1383,7 +1390,8 @@ fn updateTlv( const gop = try self.tls_variables.getOrPut(gpa, atom_ptr.atom_index); assert(!gop.found_existing); // TODO incremental updates - try self.allocateAtom(atom_ptr, true, elf_file); + self.allocateAtom(atom_ptr, true, elf_file) catch |err| + return elf_file.base.cgFail(nav_index, "failed to allocate atom: {s}", .{@errorName(err)}); sym.value = 0; esym.st_value = 0; @@ -1392,7 +1400,8 @@ fn updateTlv( const shdr = elf_file.sections.items(.shdr)[shndx]; if (shdr.sh_type != elf.SHT_NOBITS) { const file_offset = atom_ptr.offset(elf_file); - try elf_file.base.file.?.pwriteAll(code, file_offset); + elf_file.base.file.?.pwriteAll(code, file_offset) catch |err| + return elf_file.base.cgFail(nav_index, "failed to write to output file: {s}", .{@errorName(err)}); log.debug("writing TLV {s} from 0x{x} to 0x{x}", .{ atom_ptr.name(elf_file), file_offset, @@ -1408,7 +1417,7 @@ pub fn updateFunc( func_index: InternPool.Index, air: Air, liveness: Liveness, -) !void { +) link.File.UpdateNavError!void { const tracy = trace(@src()); defer tracy.end(); @@ -1422,13 +1431,13 @@ pub fn updateFunc( const sym_index = try self.getOrCreateMetadataForNav(zcu, func.owner_nav); self.atom(self.symbol(sym_index).ref.index).?.freeRelocs(self); - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); var debug_wip_nav = if (self.dwarf) |*dwarf| try dwarf.initWipNav(pt, func.owner_nav, sym_index) else null; defer if (debug_wip_nav) |*wip_nav| wip_nav.deinit(); - const res = try codegen.generateFunction( + try codegen.generateFunction( &elf_file.base, pt, zcu.navSrcLoc(func.owner_nav), @@ -1438,14 +1447,7 @@ pub fn updateFunc( &code_buffer, if (debug_wip_nav) |*dn| .{ .dwarf = dn } else .none, ); - - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| { - try zcu.failed_codegen.put(gpa, func.owner_nav, em); - return; - }, - }; + const code = code_buffer.items; const shndx = try self.getNavShdrIndex(elf_file, zcu, func.owner_nav, sym_index, code); log.debug("setting shdr({x},{s}) for {}", .{ @@ -1463,7 +1465,8 @@ pub fn updateFunc( break :blk .{ atom_ptr.value, atom_ptr.alignment }; }; - if (debug_wip_nav) |*wip_nav| try self.dwarf.?.finishWipNavFunc(pt, func.owner_nav, code.len, wip_nav); + if (debug_wip_nav) |*wip_nav| self.dwarf.?.finishWipNavFunc(pt, func.owner_nav, code.len, wip_nav) catch |err| + return elf_file.base.cgFail(func.owner_nav, "failed to finish dwarf function: {s}", .{@errorName(err)}); // Exports will be updated by `Zcu.processExports` after the update. @@ -1511,7 +1514,8 @@ pub fn updateFunc( target_sym.flags.has_trampoline = true; } const target_sym = self.symbol(sym_index); - try writeTrampoline(self.symbol(target_sym.extra(elf_file).trampoline).*, target_sym.*, elf_file); + writeTrampoline(self.symbol(target_sym.extra(elf_file).trampoline).*, target_sym.*, elf_file) catch |err| + return elf_file.base.cgFail(func.owner_nav, "failed to write trampoline: {s}", .{@errorName(err)}); } } @@ -1547,7 +1551,11 @@ pub fn updateNav( if (self.dwarf) |*dwarf| dwarf: { var debug_wip_nav = try dwarf.initWipNav(pt, nav_index, sym_index) orelse break :dwarf; defer debug_wip_nav.deinit(); - try dwarf.finishWipNav(pt, nav_index, &debug_wip_nav); + dwarf.finishWipNav(pt, nav_index, &debug_wip_nav) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.Overflow => return error.Overflow, + else => |e| return elf_file.base.cgFail(nav_index, "failed to finish dwarf nav: {s}", .{@errorName(e)}), + }; } return; }, @@ -1558,13 +1566,13 @@ pub fn updateNav( const sym_index = try self.getOrCreateMetadataForNav(zcu, nav_index); self.symbol(sym_index).atom(elf_file).?.freeRelocs(self); - var code_buffer = std.ArrayList(u8).init(zcu.gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(zcu.gpa); var debug_wip_nav = if (self.dwarf) |*dwarf| try dwarf.initWipNav(pt, nav_index, sym_index) else null; defer if (debug_wip_nav) |*wip_nav| wip_nav.deinit(); - const res = try codegen.generateSymbol( + try codegen.generateSymbol( &elf_file.base, pt, zcu.navSrcLoc(nav_index), @@ -1572,14 +1580,7 @@ pub fn updateNav( &code_buffer, .{ .atom_index = sym_index }, ); - - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| { - try zcu.failed_codegen.put(zcu.gpa, nav_index, em); - return; - }, - }; + const code = code_buffer.items; const shndx = try self.getNavShdrIndex(elf_file, zcu, nav_index, sym_index, code); log.debug("setting shdr({x},{s}) for {}", .{ @@ -1592,7 +1593,11 @@ pub fn updateNav( else try self.updateNavCode(elf_file, pt, nav_index, sym_index, shndx, code, elf.STT_OBJECT); - if (debug_wip_nav) |*wip_nav| try self.dwarf.?.finishWipNav(pt, nav_index, wip_nav); + if (debug_wip_nav) |*wip_nav| self.dwarf.?.finishWipNav(pt, nav_index, wip_nav) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.Overflow => return error.Overflow, + else => |e| return elf_file.base.cgFail(nav_index, "failed to finish dwarf nav: {s}", .{@errorName(e)}), + }; } else if (self.dwarf) |*dwarf| try dwarf.updateComptimeNav(pt, nav_index); // Exports will be updated by `Zcu.processExports` after the update. @@ -1602,7 +1607,7 @@ pub fn updateContainerType( self: *ZigObject, pt: Zcu.PerThread, ty: InternPool.Index, -) link.File.UpdateNavError!void { +) !void { const tracy = trace(@src()); defer tracy.end(); @@ -1620,8 +1625,8 @@ fn updateLazySymbol( const gpa = zcu.gpa; var required_alignment: InternPool.Alignment = .none; - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); const name_str_index = blk: { const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{ @@ -1633,7 +1638,7 @@ fn updateLazySymbol( }; const src = Type.fromInterned(sym.ty).srcLocOrNull(zcu) orelse Zcu.LazySrcLoc.unneeded; - const res = try codegen.generateLazySymbol( + try codegen.generateLazySymbol( &elf_file.base, pt, src, @@ -1643,13 +1648,7 @@ fn updateLazySymbol( .none, .{ .atom_index = symbol_index }, ); - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| { - log.err("{s}", .{em.msg}); - return error.CodegenFail; - }, - }; + const code = code_buffer.items; const output_section_index = switch (sym.kind) { .code => if (self.text_index) |sym_index| @@ -1696,7 +1695,7 @@ fn updateLazySymbol( local_sym.value = 0; local_esym.st_value = 0; - try elf_file.base.file.?.pwriteAll(code, atom_ptr.offset(elf_file)); + try elf_file.pwriteAll(code, atom_ptr.offset(elf_file)); } const LowerConstResult = union(enum) { @@ -1716,13 +1715,13 @@ fn lowerConst( ) !LowerConstResult { const gpa = pt.zcu.gpa; - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); const name_off = try self.addString(gpa, name); const sym_index = try self.newSymbolWithAtom(gpa, name_off); - const res = try codegen.generateSymbol( + try codegen.generateSymbol( &elf_file.base, pt, src_loc, @@ -1730,10 +1729,7 @@ fn lowerConst( &code_buffer, .{ .atom_index = sym_index }, ); - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| return .{ .fail = em }, - }; + const code = code_buffer.items; const local_sym = self.symbol(sym_index); const local_esym = &self.symtab.items(.elf_sym)[local_sym.esym_index]; @@ -1748,7 +1744,7 @@ fn lowerConst( try self.allocateAtom(atom_ptr, true, elf_file); errdefer self.freeNavMetadata(elf_file, sym_index); - try elf_file.base.file.?.pwriteAll(code, atom_ptr.offset(elf_file)); + try elf_file.pwriteAll(code, atom_ptr.offset(elf_file)); return .{ .ok = sym_index }; } @@ -1758,7 +1754,7 @@ pub fn updateExports( elf_file: *Elf, pt: Zcu.PerThread, exported: Zcu.Exported, - export_indices: []const u32, + export_indices: []const Zcu.Export.Index, ) link.File.UpdateExportsError!void { const tracy = trace(@src()); defer tracy.end(); @@ -1771,7 +1767,7 @@ pub fn updateExports( break :blk self.navs.getPtr(nav).?; }, .uav => |uav| self.uavs.getPtr(uav) orelse blk: { - const first_exp = zcu.all_exports.items[export_indices[0]]; + const first_exp = export_indices[0].ptr(zcu); const res = try self.lowerUav(elf_file, pt, uav, .none, first_exp.src); switch (res) { .mcv => {}, @@ -1792,7 +1788,7 @@ pub fn updateExports( const esym_shndx = self.symtab.items(.shndx)[esym_index]; for (export_indices) |export_idx| { - const exp = zcu.all_exports.items[export_idx]; + const exp = export_idx.ptr(zcu); if (exp.opts.section.unwrap()) |section_name| { if (!section_name.eqlSlice(".text", &zcu.intern_pool)) { try zcu.failed_exports.ensureUnusedCapacity(zcu.gpa, 1); @@ -1849,7 +1845,13 @@ pub fn updateExports( pub fn updateLineNumber(self: *ZigObject, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) !void { if (self.dwarf) |*dwarf| { - try dwarf.updateLineNumber(pt.zcu, ti_id); + const comp = dwarf.bin_file.comp; + const diags = &comp.link_diags; + dwarf.updateLineNumber(pt.zcu, ti_id) catch |err| switch (err) { + error.Overflow => return error.Overflow, + error.OutOfMemory => return error.OutOfMemory, + else => |e| return diags.fail("failed to update dwarf line numbers: {s}", .{@errorName(e)}), + }; } } @@ -1935,8 +1937,8 @@ pub fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, requires_padding: bool, e const shdr = &slice.items(.shdr)[atom_ptr.output_section_index]; const last_atom_ref = &slice.items(.last_atom)[atom_ptr.output_section_index]; - // FIXME:JK this only works if this atom is the only atom in the output section - // In every other case, we need to redo the prev/next links + // This only works if this atom is the only atom in the output section. In + // every other case, we need to redo the prev/next links. if (last_atom_ref.eql(atom_ptr.ref())) last_atom_ref.* = .{}; const alloc_res = try elf_file.allocateChunk(.{ diff --git a/src/link/Elf/eh_frame.zig b/src/link/Elf/eh_frame.zig index 4df05d49d3..bf46fb0262 100644 --- a/src/link/Elf/eh_frame.zig +++ b/src/link/Elf/eh_frame.zig @@ -611,7 +611,7 @@ fn reportInvalidReloc(rec: anytype, elf_file: *Elf, rel: elf.Elf64_Rela) !void { relocation.fmtRelocType(rel.r_type(), elf_file.getTarget().cpu.arch), rel.r_offset, }); - try err.addNote("in {}:.eh_frame", .{elf_file.file(rec.file_index).?.fmtPath()}); + err.addNote("in {}:.eh_frame", .{elf_file.file(rec.file_index).?.fmtPath()}); return error.RelocFailure; } diff --git a/src/link/Elf/relocatable.zig b/src/link/Elf/relocatable.zig index 3035c33790..e8f9414da6 100644 --- a/src/link/Elf/relocatable.zig +++ b/src/link/Elf/relocatable.zig @@ -1,8 +1,8 @@ -pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation) link.File.FlushError!void { +pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation) !void { const gpa = comp.gpa; const diags = &comp.link_diags; - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; // First, we flush relocatable object file generated with our backends. if (elf_file.zigObjectPtr()) |zig_object| { @@ -127,13 +127,13 @@ pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation) link.File.FlushError!v try elf_file.base.file.?.setEndPos(total_size); try elf_file.base.file.?.pwriteAll(buffer.items, 0); - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; } -pub fn flushObject(elf_file: *Elf, comp: *Compilation) link.File.FlushError!void { +pub fn flushObject(elf_file: *Elf, comp: *Compilation) !void { const diags = &comp.link_diags; - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; // Now, we are ready to resolve the symbols across all input files. // We will first resolve the files in the ZigObject, next in the parsed @@ -179,7 +179,7 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation) link.File.FlushError!void try elf_file.writeShdrTable(); try elf_file.writeElfHeader(); - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; } fn claimUnresolved(elf_file: *Elf) void { diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 3a3710aed8..e36fd4e80a 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -434,7 +434,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n // libc/libSystem dep self.resolveLibSystem(arena, comp, &system_libs) catch |err| switch (err) { error.MissingLibSystem => {}, // already reported - else => |e| return e, // TODO: convert into an error + else => |e| return diags.fail("failed to resolve libSystem: {s}", .{@errorName(e)}), }; for (comp.link_inputs) |link_input| switch (link_input) { @@ -481,7 +481,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n } }; - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; { const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); @@ -494,14 +494,17 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n try self.resolveSymbols(); try self.convertTentativeDefsAndResolveSpecialSymbols(); - try self.dedupLiterals(); + self.dedupLiterals() catch |err| switch (err) { + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to deduplicate literals: {s}", .{@errorName(e)}), + }; if (self.base.gc_sections) { try dead_strip.gcAtoms(self); } self.checkDuplicates() catch |err| switch (err) { - error.HasDuplicates => return error.FlushFailure, + error.HasDuplicates => return error.LinkFailure, else => |e| return diags.fail("failed to check for duplicate symbol definitions: {s}", .{@errorName(e)}), }; @@ -516,7 +519,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n self.claimUnresolved(); self.scanRelocs() catch |err| switch (err) { - error.HasUndefinedSymbols => return error.FlushFailure, + error.HasUndefinedSymbols => return error.LinkFailure, else => |e| return diags.fail("failed to scan relocations: {s}", .{@errorName(e)}), }; @@ -529,7 +532,10 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n try self.generateUnwindInfo(); try self.initSegments(); - try self.allocateSections(); + self.allocateSections() catch |err| switch (err) { + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to allocate sections: {s}", .{@errorName(e)}), + }; self.allocateSegments(); self.allocateSyntheticSymbols(); @@ -543,7 +549,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n if (self.getZigObject()) |zo| { zo.resolveRelocs(self) catch |err| switch (err) { - error.ResolveFailed => return error.FlushFailure, + error.ResolveFailed => return error.LinkFailure, else => |e| return e, }; } @@ -551,7 +557,11 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n try self.writeSectionsToFile(); try self.allocateLinkeditSegment(); - try self.writeLinkeditSectionsToFile(); + self.writeLinkeditSectionsToFile() catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to write linkedit sections to file: {s}", .{@errorName(e)}), + }; var codesig: ?CodeSignature = if (self.requiresCodeSig()) blk: { // Preallocate space for the code signature. @@ -561,7 +571,8 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n // where the code signature goes into. var codesig = CodeSignature.init(self.getPageSize()); codesig.code_directory.ident = fs.path.basename(self.base.emit.sub_path); - if (self.entitlements) |path| try codesig.addEntitlements(gpa, path); + if (self.entitlements) |path| codesig.addEntitlements(gpa, path) catch |err| + return diags.fail("failed to add entitlements from {s}: {s}", .{ path, @errorName(err) }); try self.writeCodeSignaturePadding(&codesig); break :blk codesig; } else null; @@ -573,15 +584,34 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n self.getPageSize(), ); - const ncmds, const sizeofcmds, const uuid_cmd_offset = try self.writeLoadCommands(); + const ncmds, const sizeofcmds, const uuid_cmd_offset = self.writeLoadCommands() catch |err| switch (err) { + error.NoSpaceLeft => unreachable, + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + }; try self.writeHeader(ncmds, sizeofcmds); - try self.writeUuid(uuid_cmd_offset, self.requiresCodeSig()); - if (self.getDebugSymbols()) |dsym| try dsym.flushModule(self); + self.writeUuid(uuid_cmd_offset, self.requiresCodeSig()) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to calculate and write uuid: {s}", .{@errorName(e)}), + }; + if (self.getDebugSymbols()) |dsym| dsym.flushModule(self) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| return diags.fail("failed to get debug symbols: {s}", .{@errorName(e)}), + }; + // Code signing always comes last. if (codesig) |*csig| { - try self.writeCodeSignature(csig); // code signing always comes last + self.writeCodeSignature(csig) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to write code signature: {s}", .{@errorName(e)}), + }; const emit = self.base.emit; - try invalidateKernelCache(emit.root_dir.handle, emit.sub_path); + invalidateKernelCache(emit.root_dir.handle, emit.sub_path) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| return diags.fail("failed to invalidate kernel cache: {s}", .{@errorName(e)}), + }; } } @@ -1545,21 +1575,21 @@ fn reportUndefs(self: *MachO) !void { try err.addMsg("undefined symbol: {s}", .{undef_sym.getName(self)}); switch (notes) { - .force_undefined => try err.addNote("referenced with linker flag -u", .{}), - .entry => try err.addNote("referenced with linker flag -e", .{}), - .dyld_stub_binder, .objc_msgsend => try err.addNote("referenced implicitly", .{}), + .force_undefined => err.addNote("referenced with linker flag -u", .{}), + .entry => err.addNote("referenced with linker flag -e", .{}), + .dyld_stub_binder, .objc_msgsend => err.addNote("referenced implicitly", .{}), .refs => |refs| { var inote: usize = 0; while (inote < @min(refs.items.len, max_notes)) : (inote += 1) { const ref = refs.items[inote]; const file = self.getFile(ref.file).?; const atom = ref.getAtom(self).?; - try err.addNote("referenced by {}:{s}", .{ file.fmtPath(), atom.getName(self) }); + err.addNote("referenced by {}:{s}", .{ file.fmtPath(), atom.getName(self) }); } if (refs.items.len > max_notes) { const remaining = refs.items.len - max_notes; - try err.addNote("referenced {d} more times", .{remaining}); + err.addNote("referenced {d} more times", .{remaining}); } }, } @@ -2171,7 +2201,7 @@ fn allocateSections(self: *MachO) !void { fileoff = mem.alignForward(u32, fileoff, page_size); } - const alignment = try math.powi(u32, 2, header.@"align"); + const alignment = try self.alignPow(header.@"align"); vmaddr = mem.alignForward(u64, vmaddr, alignment); header.addr = vmaddr; @@ -2327,7 +2357,7 @@ fn allocateLinkeditSegment(self: *MachO) !void { seg.vmaddr = mem.alignForward(u64, vmaddr, page_size); seg.fileoff = mem.alignForward(u64, fileoff, page_size); - var off = math.cast(u32, seg.fileoff) orelse return error.Overflow; + var off = try self.cast(u32, seg.fileoff); // DYLD_INFO_ONLY { const cmd = &self.dyld_info_cmd; @@ -2392,7 +2422,7 @@ fn resizeSections(self: *MachO) !void { if (header.isZerofill()) continue; if (self.isZigSection(@intCast(n_sect))) continue; // TODO this is horrible const cpu_arch = self.getTarget().cpu.arch; - const size = math.cast(usize, header.size) orelse return error.Overflow; + const size = try self.cast(usize, header.size); try out.resize(self.base.comp.gpa, size); const padding_byte: u8 = if (header.isCode() and cpu_arch == .x86_64) 0xcc else 0; @memset(out.items, padding_byte); @@ -2489,7 +2519,7 @@ fn writeThunkWorker(self: *MachO, thunk: Thunk) void { const doWork = struct { fn doWork(th: Thunk, buffer: []u8, macho_file: *MachO) !void { - const off = math.cast(usize, th.value) orelse return error.Overflow; + const off = try macho_file.cast(usize, th.value); const size = th.size(); var stream = std.io.fixedBufferStream(buffer[off..][0..size]); try th.write(macho_file, stream.writer()); @@ -2601,7 +2631,7 @@ fn writeSectionsToFile(self: *MachO) !void { const slice = self.sections.slice(); for (slice.items(.header), slice.items(.out)) |header, out| { - try self.base.file.?.pwriteAll(out.items, header.offset); + try self.pwriteAll(out.items, header.offset); } } @@ -2644,7 +2674,7 @@ fn writeDyldInfo(self: *MachO) !void { try self.lazy_bind_section.write(writer); try stream.seekTo(cmd.export_off - base_off); try self.export_trie.write(writer); - try self.base.file.?.pwriteAll(buffer, cmd.rebase_off); + try self.pwriteAll(buffer, cmd.rebase_off); } pub fn writeDataInCode(self: *MachO) !void { @@ -2655,7 +2685,7 @@ pub fn writeDataInCode(self: *MachO) !void { var buffer = try std.ArrayList(u8).initCapacity(gpa, self.data_in_code.size()); defer buffer.deinit(); try self.data_in_code.write(self, buffer.writer()); - try self.base.file.?.pwriteAll(buffer.items, cmd.dataoff); + try self.pwriteAll(buffer.items, cmd.dataoff); } fn writeIndsymtab(self: *MachO) !void { @@ -2667,15 +2697,15 @@ fn writeIndsymtab(self: *MachO) !void { var buffer = try std.ArrayList(u8).initCapacity(gpa, needed_size); defer buffer.deinit(); try self.indsymtab.write(self, buffer.writer()); - try self.base.file.?.pwriteAll(buffer.items, cmd.indirectsymoff); + try self.pwriteAll(buffer.items, cmd.indirectsymoff); } pub fn writeSymtabToFile(self: *MachO) !void { const tracy = trace(@src()); defer tracy.end(); const cmd = self.symtab_cmd; - try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.symtab.items), cmd.symoff); - try self.base.file.?.pwriteAll(self.strtab.items, cmd.stroff); + try self.pwriteAll(mem.sliceAsBytes(self.symtab.items), cmd.symoff); + try self.pwriteAll(self.strtab.items, cmd.stroff); } fn writeUnwindInfo(self: *MachO) !void { @@ -2686,20 +2716,20 @@ fn writeUnwindInfo(self: *MachO) !void { if (self.eh_frame_sect_index) |index| { const header = self.sections.items(.header)[index]; - const size = math.cast(usize, header.size) orelse return error.Overflow; + const size = try self.cast(usize, header.size); const buffer = try gpa.alloc(u8, size); defer gpa.free(buffer); eh_frame.write(self, buffer); - try self.base.file.?.pwriteAll(buffer, header.offset); + try self.pwriteAll(buffer, header.offset); } if (self.unwind_info_sect_index) |index| { const header = self.sections.items(.header)[index]; - const size = math.cast(usize, header.size) orelse return error.Overflow; + const size = try self.cast(usize, header.size); const buffer = try gpa.alloc(u8, size); defer gpa.free(buffer); try self.unwind_info.write(self, buffer); - try self.base.file.?.pwriteAll(buffer, header.offset); + try self.pwriteAll(buffer, header.offset); } } @@ -2890,7 +2920,7 @@ fn writeLoadCommands(self: *MachO) !struct { usize, usize, u64 } { assert(stream.pos == needed_size); - try self.base.file.?.pwriteAll(buffer, @sizeOf(macho.mach_header_64)); + try self.pwriteAll(buffer, @sizeOf(macho.mach_header_64)); return .{ ncmds, buffer.len, uuid_cmd_offset }; } @@ -2944,7 +2974,7 @@ fn writeHeader(self: *MachO, ncmds: usize, sizeofcmds: usize) !void { log.debug("writing Mach-O header {}", .{header}); - try self.base.file.?.pwriteAll(mem.asBytes(&header), 0); + try self.pwriteAll(mem.asBytes(&header), 0); } fn writeUuid(self: *MachO, uuid_cmd_offset: u64, has_codesig: bool) !void { @@ -2954,7 +2984,7 @@ fn writeUuid(self: *MachO, uuid_cmd_offset: u64, has_codesig: bool) !void { } else self.codesig_cmd.dataoff; try calcUuid(self.base.comp, self.base.file.?, file_size, &self.uuid_cmd.uuid); const offset = uuid_cmd_offset + @sizeOf(macho.load_command); - try self.base.file.?.pwriteAll(&self.uuid_cmd.uuid, offset); + try self.pwriteAll(&self.uuid_cmd.uuid, offset); } pub fn writeCodeSignaturePadding(self: *MachO, code_sig: *CodeSignature) !void { @@ -2968,7 +2998,7 @@ pub fn writeCodeSignaturePadding(self: *MachO, code_sig: *CodeSignature) !void { log.debug("writing code signature padding from 0x{x} to 0x{x}", .{ offset, offset + needed_size }); // Pad out the space. We need to do this to calculate valid hashes for everything in the file // except for code signature data. - try self.base.file.?.pwriteAll(&[_]u8{0}, offset + needed_size - 1); + try self.pwriteAll(&[_]u8{0}, offset + needed_size - 1); self.codesig_cmd.dataoff = @as(u32, @intCast(offset)); self.codesig_cmd.datasize = @as(u32, @intCast(needed_size)); @@ -2995,10 +3025,16 @@ pub fn writeCodeSignature(self: *MachO, code_sig: *CodeSignature) !void { offset + buffer.items.len, }); - try self.base.file.?.pwriteAll(buffer.items, offset); + try self.pwriteAll(buffer.items, offset); } -pub fn updateFunc(self: *MachO, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { +pub fn updateFunc( + self: *MachO, + pt: Zcu.PerThread, + func_index: InternPool.Index, + air: Air, + liveness: Liveness, +) link.File.UpdateNavError!void { if (build_options.skip_non_native and builtin.object_format != .macho) { @panic("Attempted to compile for object format that was disabled by build configuration"); } @@ -3006,7 +3042,7 @@ pub fn updateFunc(self: *MachO, pt: Zcu.PerThread, func_index: InternPool.Index, return self.getZigObject().?.updateFunc(self, pt, func_index, air, liveness); } -pub fn updateNav(self: *MachO, pt: Zcu.PerThread, nav: InternPool.Nav.Index) !void { +pub fn updateNav(self: *MachO, pt: Zcu.PerThread, nav: InternPool.Nav.Index) link.File.UpdateNavError!void { if (build_options.skip_non_native and builtin.object_format != .macho) { @panic("Attempted to compile for object format that was disabled by build configuration"); } @@ -3023,7 +3059,7 @@ pub fn updateExports( self: *MachO, pt: Zcu.PerThread, exported: Zcu.Exported, - export_indices: []const u32, + export_indices: []const Zcu.Export.Index, ) link.File.UpdateExportsError!void { if (build_options.skip_non_native and builtin.object_format != .macho) { @panic("Attempted to compile for object format that was disabled by build configuration"); @@ -3199,7 +3235,7 @@ fn copyRangeAllZeroOut(self: *MachO, old_offset: u64, new_offset: u64, size: u64 const gpa = self.base.comp.gpa; try self.copyRangeAll(old_offset, new_offset, size); const size_u = math.cast(usize, size) orelse return error.Overflow; - const zeroes = try gpa.alloc(u8, size_u); + const zeroes = try gpa.alloc(u8, size_u); // TODO no need to allocate here. defer gpa.free(zeroes); @memset(zeroes, 0); try self.base.file.?.pwriteAll(zeroes, old_offset); @@ -3306,10 +3342,9 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void { const allocSect = struct { fn allocSect(macho_file: *MachO, sect_id: u8, size: u64) !void { const sect = &macho_file.sections.items(.header)[sect_id]; - const alignment = try math.powi(u32, 2, sect.@"align"); + const alignment = try macho_file.alignPow(sect.@"align"); if (!sect.isZerofill()) { - sect.offset = math.cast(u32, try macho_file.findFreeSpace(size, alignment)) orelse - return error.Overflow; + sect.offset = try macho_file.cast(u32, try macho_file.findFreeSpace(size, alignment)); } sect.addr = macho_file.findFreeSpaceVirtual(size, alignment); sect.size = size; @@ -3441,8 +3476,8 @@ fn growSectionNonRelocatable(self: *MachO, sect_index: u8, needed_size: u64) !vo seg_id, seg.segName(), }); - try err.addNote("TODO: emit relocations to memory locations in self-hosted backends", .{}); - try err.addNote("as a workaround, try increasing pre-allocated virtual memory of each segment", .{}); + err.addNote("TODO: emit relocations to memory locations in self-hosted backends", .{}); + err.addNote("as a workaround, try increasing pre-allocated virtual memory of each segment", .{}); } seg.vmsize = needed_size; @@ -3744,7 +3779,7 @@ pub fn reportParseError2( const diags = &self.base.comp.link_diags; var err = try diags.addErrorWithNotes(1); try err.addMsg(format, args); - try err.addNote("while parsing {}", .{self.getFile(file_index).?.fmtPath()}); + err.addNote("while parsing {}", .{self.getFile(file_index).?.fmtPath()}); } fn reportMissingDependencyError( @@ -3758,10 +3793,10 @@ fn reportMissingDependencyError( const diags = &self.base.comp.link_diags; var err = try diags.addErrorWithNotes(2 + checked_paths.len); try err.addMsg(format, args); - try err.addNote("while resolving {s}", .{path}); - try err.addNote("a dependency of {}", .{self.getFile(parent).?.fmtPath()}); + err.addNote("while resolving {s}", .{path}); + err.addNote("a dependency of {}", .{self.getFile(parent).?.fmtPath()}); for (checked_paths) |p| { - try err.addNote("tried {s}", .{p}); + err.addNote("tried {s}", .{p}); } } @@ -3775,8 +3810,8 @@ fn reportDependencyError( const diags = &self.base.comp.link_diags; var err = try diags.addErrorWithNotes(2); try err.addMsg(format, args); - try err.addNote("while parsing {s}", .{path}); - try err.addNote("a dependency of {}", .{self.getFile(parent).?.fmtPath()}); + err.addNote("while parsing {s}", .{path}); + err.addNote("a dependency of {}", .{self.getFile(parent).?.fmtPath()}); } fn reportDuplicates(self: *MachO) error{ HasDuplicates, OutOfMemory }!void { @@ -3806,17 +3841,17 @@ fn reportDuplicates(self: *MachO) error{ HasDuplicates, OutOfMemory }!void { var err = try diags.addErrorWithNotes(nnotes + 1); try err.addMsg("duplicate symbol definition: {s}", .{sym.getName(self)}); - try err.addNote("defined by {}", .{sym.getFile(self).?.fmtPath()}); + err.addNote("defined by {}", .{sym.getFile(self).?.fmtPath()}); var inote: usize = 0; while (inote < @min(notes.items.len, max_notes)) : (inote += 1) { const file = self.getFile(notes.items[inote]).?; - try err.addNote("defined by {}", .{file.fmtPath()}); + err.addNote("defined by {}", .{file.fmtPath()}); } if (notes.items.len > max_notes) { const remaining = notes.items.len - max_notes; - try err.addNote("defined {d} more times", .{remaining}); + err.addNote("defined {d} more times", .{remaining}); } } return error.HasDuplicates; @@ -5310,6 +5345,40 @@ fn isReachable(atom: *const Atom, rel: Relocation, macho_file: *MachO) bool { return true; } +pub fn pwriteAll(macho_file: *MachO, bytes: []const u8, offset: u64) error{LinkFailure}!void { + const comp = macho_file.base.comp; + const diags = &comp.link_diags; + macho_file.base.file.?.pwriteAll(bytes, offset) catch |err| { + return diags.fail("failed to write: {s}", .{@errorName(err)}); + }; +} + +pub fn setEndPos(macho_file: *MachO, length: u64) error{LinkFailure}!void { + const comp = macho_file.base.comp; + const diags = &comp.link_diags; + macho_file.base.file.?.setEndPos(length) catch |err| { + return diags.fail("failed to set file end pos: {s}", .{@errorName(err)}); + }; +} + +pub fn cast(macho_file: *MachO, comptime T: type, x: anytype) error{LinkFailure}!T { + return std.math.cast(T, x) orelse { + const comp = macho_file.base.comp; + const diags = &comp.link_diags; + return diags.fail("encountered {d}, overflowing {d}-bit value", .{ x, @bitSizeOf(T) }); + }; +} + +pub fn alignPow(macho_file: *MachO, x: u32) error{LinkFailure}!u32 { + const result, const ov = @shlWithOverflow(@as(u32, 1), try cast(macho_file, u5, x)); + if (ov != 0) { + const comp = macho_file.base.comp; + const diags = &comp.link_diags; + return diags.fail("alignment overflow", .{}); + } + return result; +} + /// Branch instruction has 26 bits immediate but is 4 byte aligned. const jump_bits = @bitSizeOf(i28); const max_distance = (1 << (jump_bits - 1)); diff --git a/src/link/MachO/Atom.zig b/src/link/MachO/Atom.zig index f8bf9c37e7..ed554ffb35 100644 --- a/src/link/MachO/Atom.zig +++ b/src/link/MachO/Atom.zig @@ -909,8 +909,8 @@ const x86_64 = struct { rel.offset, rel.fmtPretty(.x86_64), }); - try err.addNote("expected .mov instruction but found .{s}", .{@tagName(x)}); - try err.addNote("while parsing {}", .{self.getFile(macho_file).fmtPath()}); + err.addNote("expected .mov instruction but found .{s}", .{@tagName(x)}); + err.addNote("while parsing {}", .{self.getFile(macho_file).fmtPath()}); return error.RelaxFailUnexpectedInstruction; }, } @@ -971,7 +971,7 @@ pub fn calcNumRelocs(self: Atom, macho_file: *MachO) u32 { } } -pub fn writeRelocs(self: Atom, macho_file: *MachO, code: []u8, buffer: []macho.relocation_info) !void { +pub fn writeRelocs(self: Atom, macho_file: *MachO, code: []u8, buffer: []macho.relocation_info) error{ LinkFailure, OutOfMemory }!void { const tracy = trace(@src()); defer tracy.end(); @@ -983,15 +983,15 @@ pub fn writeRelocs(self: Atom, macho_file: *MachO, code: []u8, buffer: []macho.r var i: usize = 0; for (relocs) |rel| { defer i += 1; - const rel_offset = math.cast(usize, rel.offset - self.off) orelse return error.Overflow; - const r_address: i32 = math.cast(i32, self.value + rel_offset) orelse return error.Overflow; + const rel_offset = try macho_file.cast(usize, rel.offset - self.off); + const r_address: i32 = try macho_file.cast(i32, self.value + rel_offset); assert(r_address >= 0); const r_symbolnum = r_symbolnum: { const r_symbolnum: u32 = switch (rel.tag) { .local => rel.getTargetAtom(self, macho_file).out_n_sect + 1, .@"extern" => rel.getTargetSymbol(self, macho_file).getOutputSymtabIndex(macho_file).?, }; - break :r_symbolnum math.cast(u24, r_symbolnum) orelse return error.Overflow; + break :r_symbolnum try macho_file.cast(u24, r_symbolnum); }; const r_extern = rel.tag == .@"extern"; var addend = rel.addend + rel.getRelocAddend(cpu_arch); @@ -1027,7 +1027,7 @@ pub fn writeRelocs(self: Atom, macho_file: *MachO, code: []u8, buffer: []macho.r } else if (addend > 0) { buffer[i] = .{ .r_address = r_address, - .r_symbolnum = @bitCast(math.cast(i24, addend) orelse return error.Overflow), + .r_symbolnum = @bitCast(try macho_file.cast(i24, addend)), .r_pcrel = 0, .r_length = 2, .r_extern = 0, diff --git a/src/link/MachO/InternalObject.zig b/src/link/MachO/InternalObject.zig index f41b1aa7ef..2eb9837833 100644 --- a/src/link/MachO/InternalObject.zig +++ b/src/link/MachO/InternalObject.zig @@ -414,10 +414,11 @@ pub fn resolveLiterals(self: *InternalObject, lp: *MachO.LiteralPool, macho_file const rel = relocs[0]; assert(rel.tag == .@"extern"); const target = rel.getTargetSymbol(atom.*, macho_file).getAtom(macho_file).?; - const target_size = std.math.cast(usize, target.size) orelse return error.Overflow; + const target_size = try macho_file.cast(usize, target.size); try buffer.ensureUnusedCapacity(target_size); buffer.resize(target_size) catch unreachable; - @memcpy(buffer.items, try self.getSectionData(target.n_sect)); + const section_data = try self.getSectionData(target.n_sect, macho_file); + @memcpy(buffer.items, section_data); const res = try lp.insert(gpa, header.type(), buffer.items); buffer.clearRetainingCapacity(); if (!res.found_existing) { @@ -607,10 +608,11 @@ pub fn writeAtoms(self: *InternalObject, macho_file: *MachO) !void { if (!atom.isAlive()) continue; const sect = atom.getInputSection(macho_file); if (sect.isZerofill()) continue; - const off = std.math.cast(usize, atom.value) orelse return error.Overflow; - const size = std.math.cast(usize, atom.size) orelse return error.Overflow; + const off = try macho_file.cast(usize, atom.value); + const size = try macho_file.cast(usize, atom.size); const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items[off..][0..size]; - @memcpy(buffer, try self.getSectionData(atom.n_sect)); + const section_data = try self.getSectionData(atom.n_sect, macho_file); + @memcpy(buffer, section_data); try atom.resolveRelocs(macho_file, buffer); } } @@ -644,13 +646,13 @@ fn addSection(self: *InternalObject, allocator: Allocator, segname: []const u8, return n_sect; } -fn getSectionData(self: *const InternalObject, index: u32) error{Overflow}![]const u8 { +fn getSectionData(self: *const InternalObject, index: u32, macho_file: *MachO) error{LinkFailure}![]const u8 { const slice = self.sections.slice(); assert(index < slice.items(.header).len); const sect = slice.items(.header)[index]; const extra = slice.items(.extra)[index]; if (extra.is_objc_methname) { - const size = std.math.cast(usize, sect.size) orelse return error.Overflow; + const size = try macho_file.cast(usize, sect.size); return self.objc_methnames.items[sect.offset..][0..size]; } else if (extra.is_objc_selref) return &self.objc_selrefs diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig index 349ee99ca4..000f374035 100644 --- a/src/link/MachO/Object.zig +++ b/src/link/MachO/Object.zig @@ -582,7 +582,7 @@ fn initPointerLiterals(self: *Object, allocator: Allocator, macho_file: *MachO) ); return error.MalformedObject; } - const num_ptrs = math.cast(usize, @divExact(sect.size, rec_size)) orelse return error.Overflow; + const num_ptrs = try macho_file.cast(usize, @divExact(sect.size, rec_size)); for (0..num_ptrs) |i| { const pos: u32 = @as(u32, @intCast(i)) * rec_size; @@ -650,8 +650,8 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO for (subs.items) |sub| { const atom = self.getAtom(sub.atom).?; - const atom_off = math.cast(usize, atom.off) orelse return error.Overflow; - const atom_size = math.cast(usize, atom.size) orelse return error.Overflow; + const atom_off = try macho_file.cast(usize, atom.off); + const atom_size = try macho_file.cast(usize, atom.size); const atom_data = data[atom_off..][0..atom_size]; const res = try lp.insert(gpa, header.type(), atom_data); if (!res.found_existing) { @@ -674,8 +674,8 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO .local => rel.getTargetAtom(atom.*, macho_file), .@"extern" => rel.getTargetSymbol(atom.*, macho_file).getAtom(macho_file).?, }; - const addend = math.cast(u32, rel.addend) orelse return error.Overflow; - const target_size = math.cast(usize, target.size) orelse return error.Overflow; + const addend = try macho_file.cast(u32, rel.addend); + const target_size = try macho_file.cast(usize, target.size); try buffer.ensureUnusedCapacity(target_size); buffer.resize(target_size) catch unreachable; const gop = try sections_data.getOrPut(target.n_sect); @@ -683,7 +683,7 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO gop.value_ptr.* = try self.readSectionData(gpa, file, @intCast(target.n_sect)); } const data = gop.value_ptr.*; - const target_off = math.cast(usize, target.off) orelse return error.Overflow; + const target_off = try macho_file.cast(usize, target.off); @memcpy(buffer.items, data[target_off..][0..target_size]); const res = try lp.insert(gpa, header.type(), buffer.items[addend..]); buffer.clearRetainingCapacity(); @@ -1033,7 +1033,7 @@ fn initEhFrameRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fi const sect = slice.items(.header)[sect_id]; const relocs = slice.items(.relocs)[sect_id]; - const size = math.cast(usize, sect.size) orelse return error.Overflow; + const size = try macho_file.cast(usize, sect.size); try self.eh_frame_data.resize(allocator, size); const amt = try file.preadAll(self.eh_frame_data.items, sect.offset + self.offset); if (amt != self.eh_frame_data.items.len) return error.InputOutput; @@ -1696,7 +1696,7 @@ pub fn updateArSize(self: *Object, macho_file: *MachO) !void { pub fn writeAr(self: Object, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void { // Header - const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow; + const size = try macho_file.cast(usize, self.output_ar_state.size); const basename = std.fs.path.basename(self.path.sub_path); try Archive.writeHeader(basename, size, ar_format, writer); // Data @@ -1826,7 +1826,7 @@ pub fn writeAtoms(self: *Object, macho_file: *MachO) !void { for (headers, 0..) |header, n_sect| { if (header.isZerofill()) continue; - const size = math.cast(usize, header.size) orelse return error.Overflow; + const size = try macho_file.cast(usize, header.size); const data = try gpa.alloc(u8, size); const amt = try file.preadAll(data, header.offset + self.offset); if (amt != data.len) return error.InputOutput; @@ -1837,9 +1837,9 @@ pub fn writeAtoms(self: *Object, macho_file: *MachO) !void { if (!atom.isAlive()) continue; const sect = atom.getInputSection(macho_file); if (sect.isZerofill()) continue; - const value = math.cast(usize, atom.value) orelse return error.Overflow; - const off = math.cast(usize, atom.off) orelse return error.Overflow; - const size = math.cast(usize, atom.size) orelse return error.Overflow; + const value = try macho_file.cast(usize, atom.value); + const off = try macho_file.cast(usize, atom.off); + const size = try macho_file.cast(usize, atom.size); const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items; const data = sections_data[atom.n_sect]; @memcpy(buffer[value..][0..size], data[off..][0..size]); @@ -1865,7 +1865,7 @@ pub fn writeAtomsRelocatable(self: *Object, macho_file: *MachO) !void { for (headers, 0..) |header, n_sect| { if (header.isZerofill()) continue; - const size = math.cast(usize, header.size) orelse return error.Overflow; + const size = try macho_file.cast(usize, header.size); const data = try gpa.alloc(u8, size); const amt = try file.preadAll(data, header.offset + self.offset); if (amt != data.len) return error.InputOutput; @@ -1876,9 +1876,9 @@ pub fn writeAtomsRelocatable(self: *Object, macho_file: *MachO) !void { if (!atom.isAlive()) continue; const sect = atom.getInputSection(macho_file); if (sect.isZerofill()) continue; - const value = math.cast(usize, atom.value) orelse return error.Overflow; - const off = math.cast(usize, atom.off) orelse return error.Overflow; - const size = math.cast(usize, atom.size) orelse return error.Overflow; + const value = try macho_file.cast(usize, atom.value); + const off = try macho_file.cast(usize, atom.off); + const size = try macho_file.cast(usize, atom.size); const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items; const data = sections_data[atom.n_sect]; @memcpy(buffer[value..][0..size], data[off..][0..size]); @@ -1909,29 +1909,27 @@ pub fn calcCompactUnwindSizeRelocatable(self: *Object, macho_file: *MachO) void } } +fn addReloc(offset: u32, arch: std.Target.Cpu.Arch) !macho.relocation_info { + return .{ + .r_address = std.math.cast(i32, offset) orelse return error.Overflow, + .r_symbolnum = 0, + .r_pcrel = 0, + .r_length = 3, + .r_extern = 0, + .r_type = switch (arch) { + .aarch64 => @intFromEnum(macho.reloc_type_arm64.ARM64_RELOC_UNSIGNED), + .x86_64 => @intFromEnum(macho.reloc_type_x86_64.X86_64_RELOC_UNSIGNED), + else => unreachable, + }, + }; +} + pub fn writeCompactUnwindRelocatable(self: *Object, macho_file: *MachO) !void { const tracy = trace(@src()); defer tracy.end(); const cpu_arch = macho_file.getTarget().cpu.arch; - const addReloc = struct { - fn addReloc(offset: u32, arch: std.Target.Cpu.Arch) !macho.relocation_info { - return .{ - .r_address = math.cast(i32, offset) orelse return error.Overflow, - .r_symbolnum = 0, - .r_pcrel = 0, - .r_length = 3, - .r_extern = 0, - .r_type = switch (arch) { - .aarch64 => @intFromEnum(macho.reloc_type_arm64.ARM64_RELOC_UNSIGNED), - .x86_64 => @intFromEnum(macho.reloc_type_x86_64.X86_64_RELOC_UNSIGNED), - else => unreachable, - }, - }; - } - }.addReloc; - const nsect = macho_file.unwind_info_sect_index.?; const buffer = macho_file.sections.items(.out)[nsect].items; const relocs = macho_file.sections.items(.relocs)[nsect].items; @@ -1967,7 +1965,7 @@ pub fn writeCompactUnwindRelocatable(self: *Object, macho_file: *MachO) !void { // Personality function if (rec.getPersonality(macho_file)) |sym| { - const r_symbolnum = math.cast(u24, sym.getOutputSymtabIndex(macho_file).?) orelse return error.Overflow; + const r_symbolnum = try macho_file.cast(u24, sym.getOutputSymtabIndex(macho_file).?); var reloc = try addReloc(offset + 16, cpu_arch); reloc.r_symbolnum = r_symbolnum; reloc.r_extern = 1; diff --git a/src/link/MachO/ZigObject.zig b/src/link/MachO/ZigObject.zig index fb5a1255ca..5f2e1291c0 100644 --- a/src/link/MachO/ZigObject.zig +++ b/src/link/MachO/ZigObject.zig @@ -290,12 +290,15 @@ pub fn dedupLiterals(self: *ZigObject, lp: MachO.LiteralPool, macho_file: *MachO /// We need this so that we can write to an archive. /// TODO implement writing ZigObject data directly to a buffer instead. pub fn readFileContents(self: *ZigObject, macho_file: *MachO) !void { + const diags = &macho_file.base.comp.link_diags; // Size of the output object file is always the offset + size of the strtab const size = macho_file.symtab_cmd.stroff + macho_file.symtab_cmd.strsize; const gpa = macho_file.base.comp.gpa; try self.data.resize(gpa, size); - const amt = try macho_file.base.file.?.preadAll(self.data.items, 0); - if (amt != size) return error.InputOutput; + const amt = macho_file.base.file.?.preadAll(self.data.items, 0) catch |err| + return diags.fail("failed to read output file: {s}", .{@errorName(err)}); + if (amt != size) + return diags.fail("unexpected EOF reading from output file", .{}); } pub fn updateArSymtab(self: ZigObject, ar_symtab: *Archive.ArSymtab, macho_file: *MachO) error{OutOfMemory}!void { @@ -376,7 +379,7 @@ pub fn resolveRelocs(self: *ZigObject, macho_file: *MachO) !void { if (atom.getRelocs(macho_file).len == 0) continue; // TODO: we will resolve and write ZigObject's TLS data twice: // once here, and once in writeAtoms - const atom_size = std.math.cast(usize, atom.size) orelse return error.Overflow; + const atom_size = try macho_file.cast(usize, atom.size); const code = try gpa.alloc(u8, atom_size); defer gpa.free(code); self.getAtomData(macho_file, atom.*, code) catch |err| { @@ -400,7 +403,7 @@ pub fn resolveRelocs(self: *ZigObject, macho_file: *MachO) !void { has_error = true; continue; }; - try macho_file.base.file.?.pwriteAll(code, file_offset); + try macho_file.pwriteAll(code, file_offset); } if (has_error) return error.ResolveFailed; @@ -419,7 +422,7 @@ pub fn calcNumRelocs(self: *ZigObject, macho_file: *MachO) void { } } -pub fn writeRelocs(self: *ZigObject, macho_file: *MachO) !void { +pub fn writeRelocs(self: *ZigObject, macho_file: *MachO) error{ LinkFailure, OutOfMemory }!void { const gpa = macho_file.base.comp.gpa; const diags = &macho_file.base.comp.link_diags; @@ -432,14 +435,14 @@ pub fn writeRelocs(self: *ZigObject, macho_file: *MachO) !void { if (!macho_file.isZigSection(atom.out_n_sect) and !macho_file.isDebugSection(atom.out_n_sect)) continue; if (atom.getRelocs(macho_file).len == 0) continue; const extra = atom.getExtra(macho_file); - const atom_size = std.math.cast(usize, atom.size) orelse return error.Overflow; + const atom_size = try macho_file.cast(usize, atom.size); const code = try gpa.alloc(u8, atom_size); defer gpa.free(code); self.getAtomData(macho_file, atom.*, code) catch |err| return diags.fail("failed to fetch code for '{s}': {s}", .{ atom.getName(macho_file), @errorName(err) }); const file_offset = header.offset + atom.value; try atom.writeRelocs(macho_file, code, relocs[extra.rel_out_index..][0..extra.rel_out_count]); - try macho_file.base.file.?.pwriteAll(code, file_offset); + try macho_file.pwriteAll(code, file_offset); } } @@ -457,8 +460,8 @@ pub fn writeAtomsRelocatable(self: *ZigObject, macho_file: *MachO) !void { if (sect.isZerofill()) continue; if (macho_file.isZigSection(atom.out_n_sect)) continue; if (atom.getRelocs(macho_file).len == 0) continue; - const off = std.math.cast(usize, atom.value) orelse return error.Overflow; - const size = std.math.cast(usize, atom.size) orelse return error.Overflow; + const off = try macho_file.cast(usize, atom.value); + const size = try macho_file.cast(usize, atom.size); const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items; try self.getAtomData(macho_file, atom.*, buffer[off..][0..size]); const relocs = macho_file.sections.items(.relocs)[atom.out_n_sect].items; @@ -480,8 +483,8 @@ pub fn writeAtoms(self: *ZigObject, macho_file: *MachO) !void { const sect = atom.getInputSection(macho_file); if (sect.isZerofill()) continue; if (macho_file.isZigSection(atom.out_n_sect)) continue; - const off = std.math.cast(usize, atom.value) orelse return error.Overflow; - const size = std.math.cast(usize, atom.size) orelse return error.Overflow; + const off = try macho_file.cast(usize, atom.value); + const size = try macho_file.cast(usize, atom.size); const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items; try self.getAtomData(macho_file, atom.*, buffer[off..][0..size]); try atom.resolveRelocs(macho_file, buffer[off..][0..size]); @@ -546,7 +549,9 @@ pub fn getInputSection(self: ZigObject, atom: Atom, macho_file: *MachO) macho.se return sect; } -pub fn flushModule(self: *ZigObject, macho_file: *MachO, tid: Zcu.PerThread.Id) !void { +pub fn flushModule(self: *ZigObject, macho_file: *MachO, tid: Zcu.PerThread.Id) link.File.FlushError!void { + const diags = &macho_file.base.comp.link_diags; + // Handle any lazy symbols that were emitted by incremental compilation. if (self.lazy_syms.getPtr(.anyerror_type)) |metadata| { const pt: Zcu.PerThread = .activate(macho_file.base.comp.zcu.?, tid); @@ -559,18 +564,20 @@ pub fn flushModule(self: *ZigObject, macho_file: *MachO, tid: Zcu.PerThread.Id) pt, .{ .kind = .code, .ty = .anyerror_type }, metadata.text_symbol_index, - ) catch |err| return switch (err) { - error.CodegenFail => error.FlushFailure, - else => |e| e, + ) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to update lazy symbol: {s}", .{@errorName(e)}), }; if (metadata.const_state != .unused) self.updateLazySymbol( macho_file, pt, .{ .kind = .const_data, .ty = .anyerror_type }, metadata.const_symbol_index, - ) catch |err| return switch (err) { - error.CodegenFail => error.FlushFailure, - else => |e| e, + ) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to update lazy symbol: {s}", .{@errorName(e)}), }; } for (self.lazy_syms.values()) |*metadata| { @@ -581,7 +588,10 @@ pub fn flushModule(self: *ZigObject, macho_file: *MachO, tid: Zcu.PerThread.Id) if (self.dwarf) |*dwarf| { const pt: Zcu.PerThread = .activate(macho_file.base.comp.zcu.?, tid); defer pt.deactivate(); - try dwarf.flushModule(pt); + dwarf.flushModule(pt) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| return diags.fail("failed to flush dwarf module: {s}", .{@errorName(e)}), + }; self.debug_abbrev_dirty = false; self.debug_aranges_dirty = false; @@ -616,6 +626,7 @@ pub fn getNavVAddr( const sym = self.symbols.items[sym_index]; const vaddr = sym.getAddress(.{}, macho_file); switch (reloc_info.parent) { + .none => unreachable, .atom_index => |atom_index| { const parent_atom = self.symbols.items[atom_index].getAtom(macho_file).?; try parent_atom.addReloc(macho_file, .{ @@ -655,6 +666,7 @@ pub fn getUavVAddr( const sym = self.symbols.items[sym_index]; const vaddr = sym.getAddress(.{}, macho_file); switch (reloc_info.parent) { + .none => unreachable, .atom_index => |atom_index| { const parent_atom = self.symbols.items[atom_index].getAtom(macho_file).?; try parent_atom.addReloc(macho_file, .{ @@ -766,7 +778,7 @@ pub fn updateFunc( func_index: InternPool.Index, air: Air, liveness: Liveness, -) !void { +) link.File.UpdateNavError!void { const tracy = trace(@src()); defer tracy.end(); @@ -777,13 +789,13 @@ pub fn updateFunc( const sym_index = try self.getOrCreateMetadataForNav(macho_file, func.owner_nav); self.symbols.items[sym_index].getAtom(macho_file).?.freeRelocs(macho_file); - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); var debug_wip_nav = if (self.dwarf) |*dwarf| try dwarf.initWipNav(pt, func.owner_nav, sym_index) else null; defer if (debug_wip_nav) |*wip_nav| wip_nav.deinit(); - const res = try codegen.generateFunction( + try codegen.generateFunction( &macho_file.base, pt, zcu.navSrcLoc(func.owner_nav), @@ -793,14 +805,7 @@ pub fn updateFunc( &code_buffer, if (debug_wip_nav) |*wip_nav| .{ .dwarf = wip_nav } else .none, ); - - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| { - try zcu.failed_codegen.put(gpa, func.owner_nav, em); - return; - }, - }; + const code = code_buffer.items; const sect_index = try self.getNavOutputSection(macho_file, zcu, func.owner_nav, code); const old_rva, const old_alignment = blk: { @@ -813,7 +818,8 @@ pub fn updateFunc( break :blk .{ atom.value, atom.alignment }; }; - if (debug_wip_nav) |*wip_nav| try self.dwarf.?.finishWipNavFunc(pt, func.owner_nav, code.len, wip_nav); + if (debug_wip_nav) |*wip_nav| self.dwarf.?.finishWipNavFunc(pt, func.owner_nav, code.len, wip_nav) catch |err| + return macho_file.base.cgFail(func.owner_nav, "falied to finish dwarf function: {s}", .{@errorName(err)}); // Exports will be updated by `Zcu.processExports` after the update. if (old_rva != new_rva and old_rva > 0) { @@ -850,7 +856,8 @@ pub fn updateFunc( } const target_sym = self.symbols.items[sym_index]; const source_sym = self.symbols.items[target_sym.getExtra(macho_file).trampoline]; - try writeTrampoline(source_sym, target_sym, macho_file); + writeTrampoline(source_sym, target_sym, macho_file) catch |err| + return macho_file.base.cgFail(func.owner_nav, "failed to write trampoline: {s}", .{@errorName(err)}); } } @@ -883,7 +890,11 @@ pub fn updateNav( if (self.dwarf) |*dwarf| dwarf: { var debug_wip_nav = try dwarf.initWipNav(pt, nav_index, sym_index) orelse break :dwarf; defer debug_wip_nav.deinit(); - try dwarf.finishWipNav(pt, nav_index, &debug_wip_nav); + dwarf.finishWipNav(pt, nav_index, &debug_wip_nav) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.Overflow => return error.Overflow, + else => |e| return macho_file.base.cgFail(nav_index, "failed to finish dwarf nav: {s}", .{@errorName(e)}), + }; } return; }, @@ -894,13 +905,13 @@ pub fn updateNav( const sym_index = try self.getOrCreateMetadataForNav(macho_file, nav_index); self.symbols.items[sym_index].getAtom(macho_file).?.freeRelocs(macho_file); - var code_buffer = std.ArrayList(u8).init(zcu.gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(zcu.gpa); var debug_wip_nav = if (self.dwarf) |*dwarf| try dwarf.initWipNav(pt, nav_index, sym_index) else null; defer if (debug_wip_nav) |*wip_nav| wip_nav.deinit(); - const res = try codegen.generateSymbol( + try codegen.generateSymbol( &macho_file.base, pt, zcu.navSrcLoc(nav_index), @@ -908,21 +919,19 @@ pub fn updateNav( &code_buffer, .{ .atom_index = sym_index }, ); + const code = code_buffer.items; - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| { - try zcu.failed_codegen.put(zcu.gpa, nav_index, em); - return; - }, - }; const sect_index = try self.getNavOutputSection(macho_file, zcu, nav_index, code); if (isThreadlocal(macho_file, nav_index)) try self.updateTlv(macho_file, pt, nav_index, sym_index, sect_index, code) else try self.updateNavCode(macho_file, pt, nav_index, sym_index, sect_index, code); - if (debug_wip_nav) |*wip_nav| try self.dwarf.?.finishWipNav(pt, nav_index, wip_nav); + if (debug_wip_nav) |*wip_nav| self.dwarf.?.finishWipNav(pt, nav_index, wip_nav) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.Overflow => return error.Overflow, + else => |e| return macho_file.base.cgFail(nav_index, "failed to finish dwarf nav: {s}", .{@errorName(e)}), + }; } else if (self.dwarf) |*dwarf| try dwarf.updateComptimeNav(pt, nav_index); // Exports will be updated by `Zcu.processExports` after the update. @@ -936,7 +945,7 @@ fn updateNavCode( sym_index: Symbol.Index, sect_index: u8, code: []const u8, -) !void { +) link.File.UpdateNavError!void { const zcu = pt.zcu; const gpa = zcu.gpa; const ip = &zcu.intern_pool; @@ -978,7 +987,8 @@ fn updateNavCode( const need_realloc = code.len > capacity or !required_alignment.check(atom.value); if (need_realloc) { - try atom.grow(macho_file); + atom.grow(macho_file) catch |err| + return macho_file.base.cgFail(nav_index, "failed to grow atom: {s}", .{@errorName(err)}); log.debug("growing {} from 0x{x} to 0x{x}", .{ nav.fqn.fmt(ip), old_vaddr, atom.value }); if (old_vaddr != atom.value) { sym.value = 0; @@ -991,7 +1001,8 @@ fn updateNavCode( sect.size = needed_size; } } else { - try atom.allocate(macho_file); + atom.allocate(macho_file) catch |err| + return macho_file.base.cgFail(nav_index, "failed to allocate atom: {s}", .{@errorName(err)}); errdefer self.freeNavMetadata(macho_file, sym_index); sym.value = 0; @@ -1000,7 +1011,8 @@ fn updateNavCode( if (!sect.isZerofill()) { const file_offset = sect.offset + atom.value; - try macho_file.base.file.?.pwriteAll(code, file_offset); + macho_file.base.file.?.pwriteAll(code, file_offset) catch |err| + return macho_file.base.cgFail(nav_index, "failed to write output file: {s}", .{@errorName(err)}); } } @@ -1198,13 +1210,13 @@ fn lowerConst( ) !LowerConstResult { const gpa = macho_file.base.comp.gpa; - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); const name_str = try self.addString(gpa, name); const sym_index = try self.newSymbolWithAtom(gpa, name_str, macho_file); - const res = try codegen.generateSymbol( + try codegen.generateSymbol( &macho_file.base, pt, src_loc, @@ -1212,10 +1224,7 @@ fn lowerConst( &code_buffer, .{ .atom_index = sym_index }, ); - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| return .{ .fail = em }, - }; + const code = code_buffer.items; const sym = &self.symbols.items[sym_index]; sym.out_n_sect = output_section_index; @@ -1236,7 +1245,7 @@ fn lowerConst( const sect = macho_file.sections.items(.header)[output_section_index]; const file_offset = sect.offset + atom.value; - try macho_file.base.file.?.pwriteAll(code, file_offset); + try macho_file.pwriteAll(code, file_offset); return .{ .ok = sym_index }; } @@ -1246,7 +1255,7 @@ pub fn updateExports( macho_file: *MachO, pt: Zcu.PerThread, exported: Zcu.Exported, - export_indices: []const u32, + export_indices: []const Zcu.Export.Index, ) link.File.UpdateExportsError!void { const tracy = trace(@src()); defer tracy.end(); @@ -1259,7 +1268,7 @@ pub fn updateExports( break :blk self.navs.getPtr(nav).?; }, .uav => |uav| self.uavs.getPtr(uav) orelse blk: { - const first_exp = zcu.all_exports.items[export_indices[0]]; + const first_exp = export_indices[0].ptr(zcu); const res = try self.lowerUav(macho_file, pt, uav, .none, first_exp.src); switch (res) { .mcv => {}, @@ -1279,7 +1288,7 @@ pub fn updateExports( const nlist = self.symtab.items(.nlist)[nlist_idx]; for (export_indices) |export_idx| { - const exp = zcu.all_exports.items[export_idx]; + const exp = export_idx.ptr(zcu); if (exp.opts.section.unwrap()) |section_name| { if (!section_name.eqlSlice("__text", &zcu.intern_pool)) { try zcu.failed_exports.ensureUnusedCapacity(zcu.gpa, 1); @@ -1352,8 +1361,8 @@ fn updateLazySymbol( const gpa = zcu.gpa; var required_alignment: Atom.Alignment = .none; - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); const name_str = blk: { const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{ @@ -1365,7 +1374,7 @@ fn updateLazySymbol( }; const src = Type.fromInterned(lazy_sym.ty).srcLocOrNull(zcu) orelse Zcu.LazySrcLoc.unneeded; - const res = try codegen.generateLazySymbol( + try codegen.generateLazySymbol( &macho_file.base, pt, src, @@ -1375,13 +1384,7 @@ fn updateLazySymbol( .none, .{ .atom_index = symbol_index }, ); - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| { - log.err("{s}", .{em.msg}); - return error.CodegenFail; - }, - }; + const code = code_buffer.items; const output_section_index = switch (lazy_sym.kind) { .code => macho_file.zig_text_sect_index.?, @@ -1412,12 +1415,18 @@ fn updateLazySymbol( const sect = macho_file.sections.items(.header)[output_section_index]; const file_offset = sect.offset + atom.value; - try macho_file.base.file.?.pwriteAll(code, file_offset); + try macho_file.pwriteAll(code, file_offset); } pub fn updateLineNumber(self: *ZigObject, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) !void { if (self.dwarf) |*dwarf| { - try dwarf.updateLineNumber(pt.zcu, ti_id); + const comp = dwarf.bin_file.comp; + const diags = &comp.link_diags; + dwarf.updateLineNumber(pt.zcu, ti_id) catch |err| switch (err) { + error.Overflow => return error.Overflow, + error.OutOfMemory => return error.OutOfMemory, + else => |e| return diags.fail("failed to update dwarf line numbers: {s}", .{@errorName(e)}), + }; } } diff --git a/src/link/MachO/relocatable.zig b/src/link/MachO/relocatable.zig index 497969ab90..d090a2c9ad 100644 --- a/src/link/MachO/relocatable.zig +++ b/src/link/MachO/relocatable.zig @@ -18,13 +18,15 @@ pub fn flushObject(macho_file: *MachO, comp: *Compilation, module_obj_path: ?Pat // Instead of invoking a full-blown `-r` mode on the input which sadly will strip all // debug info segments/sections (this is apparently by design by Apple), we copy // the *only* input file over. - // TODO: in the future, when we implement `dsymutil` alternative directly in the Zig - // compiler, investigate if we can get rid of this `if` prong here. const path = positionals.items[0].path().?; - const in_file = try path.root_dir.handle.openFile(path.sub_path, .{}); - const stat = try in_file.stat(); - const amt = try in_file.copyRangeAll(0, macho_file.base.file.?, 0, stat.size); - if (amt != stat.size) return error.InputOutput; // TODO: report an actual user error + const in_file = path.root_dir.handle.openFile(path.sub_path, .{}) catch |err| + return diags.fail("failed to open {}: {s}", .{ path, @errorName(err) }); + const stat = in_file.stat() catch |err| + return diags.fail("failed to stat {}: {s}", .{ path, @errorName(err) }); + const amt = in_file.copyRangeAll(0, macho_file.base.file.?, 0, stat.size) catch |err| + return diags.fail("failed to copy range of file {}: {s}", .{ path, @errorName(err) }); + if (amt != stat.size) + return diags.fail("unexpected short write in copy range of file {}", .{path}); return; } @@ -33,14 +35,18 @@ pub fn flushObject(macho_file: *MachO, comp: *Compilation, module_obj_path: ?Pat diags.addParseError(link_input.path().?, "failed to read input file: {s}", .{@errorName(err)}); } - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; try macho_file.parseInputFiles(); - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; try macho_file.resolveSymbols(); - try macho_file.dedupLiterals(); + macho_file.dedupLiterals() catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to update ar size: {s}", .{@errorName(e)}), + }; markExports(macho_file); claimUnresolved(macho_file); try initOutputSections(macho_file); @@ -49,7 +55,10 @@ pub fn flushObject(macho_file: *MachO, comp: *Compilation, module_obj_path: ?Pat try calcSectionSizes(macho_file); try createSegment(macho_file); - try allocateSections(macho_file); + allocateSections(macho_file) catch |err| switch (err) { + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to allocate sections: {s}", .{@errorName(e)}), + }; allocateSegment(macho_file); if (build_options.enable_logging) { @@ -93,11 +102,11 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? diags.addParseError(link_input.path().?, "failed to read input file: {s}", .{@errorName(err)}); } - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; try parseInputFilesAr(macho_file); - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; // First, we flush relocatable object file generated with our backends. if (macho_file.getZigObject()) |zo| { @@ -108,7 +117,8 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? try macho_file.addAtomsToSections(); try calcSectionSizes(macho_file); try createSegment(macho_file); - try allocateSections(macho_file); + allocateSections(macho_file) catch |err| + return diags.fail("failed to allocate sections: {s}", .{@errorName(err)}); allocateSegment(macho_file); if (build_options.enable_logging) { @@ -126,8 +136,6 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? const ncmds, const sizeofcmds = try writeLoadCommands(macho_file); try writeHeader(macho_file, ncmds, sizeofcmds); - // TODO we can avoid reading in the file contents we just wrote if we give the linker - // ability to write directly to a buffer. try zo.readFileContents(macho_file); } @@ -152,7 +160,8 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? // Update sizes of contributing objects for (files.items) |index| { - try macho_file.getFile(index).?.updateArSize(macho_file); + macho_file.getFile(index).?.updateArSize(macho_file) catch |err| + return diags.fail("failed to update ar size: {s}", .{@errorName(err)}); } // Update file offsets of contributing objects @@ -171,7 +180,7 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? state.file_off = pos; pos += @sizeOf(Archive.ar_hdr); pos += mem.alignForward(usize, zo.basename.len + 1, ptr_width); - pos += math.cast(usize, state.size) orelse return error.Overflow; + pos += try macho_file.cast(usize, state.size); }, .object => |o| { const state = &o.output_ar_state; @@ -179,7 +188,7 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? state.file_off = pos; pos += @sizeOf(Archive.ar_hdr); pos += mem.alignForward(usize, o.path.basename().len + 1, ptr_width); - pos += math.cast(usize, state.size) orelse return error.Overflow; + pos += try macho_file.cast(usize, state.size); }, else => unreachable, } @@ -201,7 +210,10 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? try writer.writeAll(Archive.ARMAG); // Write symtab - try ar_symtab.write(format, macho_file, writer); + ar_symtab.write(format, macho_file, writer) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => |e| return diags.fail("failed to write archive symbol table: {s}", .{@errorName(e)}), + }; // Write object files for (files.items) |index| { @@ -210,15 +222,16 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? if (padding > 0) { try writer.writeByteNTimes(0, padding); } - try macho_file.getFile(index).?.writeAr(format, macho_file, writer); + macho_file.getFile(index).?.writeAr(format, macho_file, writer) catch |err| + return diags.fail("failed to write archive: {s}", .{@errorName(err)}); } assert(buffer.items.len == total_size); - try macho_file.base.file.?.setEndPos(total_size); - try macho_file.base.file.?.pwriteAll(buffer.items, 0); + try macho_file.setEndPos(total_size); + try macho_file.pwriteAll(buffer.items, 0); - if (diags.hasErrors()) return error.FlushFailure; + if (diags.hasErrors()) return error.LinkFailure; } fn parseInputFilesAr(macho_file: *MachO) !void { @@ -452,11 +465,10 @@ fn allocateSections(macho_file: *MachO) !void { for (slice.items(.header)) |*header| { const needed_size = header.size; header.size = 0; - const alignment = try math.powi(u32, 2, header.@"align"); + const alignment = try macho_file.alignPow(header.@"align"); if (!header.isZerofill()) { if (needed_size > macho_file.allocatedSize(header.offset)) { - header.offset = math.cast(u32, try macho_file.findFreeSpace(needed_size, alignment)) orelse - return error.Overflow; + header.offset = try macho_file.cast(u32, try macho_file.findFreeSpace(needed_size, alignment)); } } if (needed_size > macho_file.allocatedSizeVirtual(header.addr)) { @@ -572,7 +584,7 @@ fn sortRelocs(macho_file: *MachO) void { } } -fn writeSections(macho_file: *MachO) !void { +fn writeSections(macho_file: *MachO) link.File.FlushError!void { const tracy = trace(@src()); defer tracy.end(); @@ -583,7 +595,7 @@ fn writeSections(macho_file: *MachO) !void { for (slice.items(.header), slice.items(.out), slice.items(.relocs), 0..) |header, *out, *relocs, n_sect| { if (header.isZerofill()) continue; if (!macho_file.isZigSection(@intCast(n_sect))) { // TODO this is wrong; what about debug sections? - const size = math.cast(usize, header.size) orelse return error.Overflow; + const size = try macho_file.cast(usize, header.size); try out.resize(gpa, size); const padding_byte: u8 = if (header.isCode() and cpu_arch == .x86_64) 0xcc else 0; @memset(out.items, padding_byte); @@ -662,16 +674,16 @@ fn writeSectionsToFile(macho_file: *MachO) !void { const slice = macho_file.sections.slice(); for (slice.items(.header), slice.items(.out), slice.items(.relocs)) |header, out, relocs| { - try macho_file.base.file.?.pwriteAll(out.items, header.offset); - try macho_file.base.file.?.pwriteAll(mem.sliceAsBytes(relocs.items), header.reloff); + try macho_file.pwriteAll(out.items, header.offset); + try macho_file.pwriteAll(mem.sliceAsBytes(relocs.items), header.reloff); } try macho_file.writeDataInCode(); - try macho_file.base.file.?.pwriteAll(mem.sliceAsBytes(macho_file.symtab.items), macho_file.symtab_cmd.symoff); - try macho_file.base.file.?.pwriteAll(macho_file.strtab.items, macho_file.symtab_cmd.stroff); + try macho_file.pwriteAll(mem.sliceAsBytes(macho_file.symtab.items), macho_file.symtab_cmd.symoff); + try macho_file.pwriteAll(macho_file.strtab.items, macho_file.symtab_cmd.stroff); } -fn writeLoadCommands(macho_file: *MachO) !struct { usize, usize } { +fn writeLoadCommands(macho_file: *MachO) error{ LinkFailure, OutOfMemory }!struct { usize, usize } { const gpa = macho_file.base.comp.gpa; const needed_size = load_commands.calcLoadCommandsSizeObject(macho_file); const buffer = try gpa.alloc(u8, needed_size); @@ -686,31 +698,45 @@ fn writeLoadCommands(macho_file: *MachO) !struct { usize, usize } { { assert(macho_file.segments.items.len == 1); const seg = macho_file.segments.items[0]; - try writer.writeStruct(seg); + writer.writeStruct(seg) catch |err| switch (err) { + error.NoSpaceLeft => unreachable, + }; for (macho_file.sections.items(.header)) |header| { - try writer.writeStruct(header); + writer.writeStruct(header) catch |err| switch (err) { + error.NoSpaceLeft => unreachable, + }; } ncmds += 1; } - try writer.writeStruct(macho_file.data_in_code_cmd); + writer.writeStruct(macho_file.data_in_code_cmd) catch |err| switch (err) { + error.NoSpaceLeft => unreachable, + }; ncmds += 1; - try writer.writeStruct(macho_file.symtab_cmd); + writer.writeStruct(macho_file.symtab_cmd) catch |err| switch (err) { + error.NoSpaceLeft => unreachable, + }; ncmds += 1; - try writer.writeStruct(macho_file.dysymtab_cmd); + writer.writeStruct(macho_file.dysymtab_cmd) catch |err| switch (err) { + error.NoSpaceLeft => unreachable, + }; ncmds += 1; if (macho_file.platform.isBuildVersionCompatible()) { - try load_commands.writeBuildVersionLC(macho_file.platform, macho_file.sdk_version, writer); + load_commands.writeBuildVersionLC(macho_file.platform, macho_file.sdk_version, writer) catch |err| switch (err) { + error.NoSpaceLeft => unreachable, + }; ncmds += 1; } else { - try load_commands.writeVersionMinLC(macho_file.platform, macho_file.sdk_version, writer); + load_commands.writeVersionMinLC(macho_file.platform, macho_file.sdk_version, writer) catch |err| switch (err) { + error.NoSpaceLeft => unreachable, + }; ncmds += 1; } assert(stream.pos == needed_size); - try macho_file.base.file.?.pwriteAll(buffer, @sizeOf(macho.mach_header_64)); + try macho_file.pwriteAll(buffer, @sizeOf(macho.mach_header_64)); return .{ ncmds, buffer.len }; } @@ -742,7 +768,7 @@ fn writeHeader(macho_file: *MachO, ncmds: usize, sizeofcmds: usize) !void { header.ncmds = @intCast(ncmds); header.sizeofcmds = @intCast(sizeofcmds); - try macho_file.base.file.?.pwriteAll(mem.asBytes(&header), 0); + try macho_file.pwriteAll(mem.asBytes(&header), 0); } const std = @import("std"); diff --git a/src/link/NvPtx.zig b/src/link/NvPtx.zig index 199b13a6c6..ab82bb9de8 100644 --- a/src/link/NvPtx.zig +++ b/src/link/NvPtx.zig @@ -82,11 +82,17 @@ pub fn deinit(self: *NvPtx) void { self.llvm_object.deinit(); } -pub fn updateFunc(self: *NvPtx, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { +pub fn updateFunc( + self: *NvPtx, + pt: Zcu.PerThread, + func_index: InternPool.Index, + air: Air, + liveness: Liveness, +) link.File.UpdateNavError!void { try self.llvm_object.updateFunc(pt, func_index, air, liveness); } -pub fn updateNav(self: *NvPtx, pt: Zcu.PerThread, nav: InternPool.Nav.Index) !void { +pub fn updateNav(self: *NvPtx, pt: Zcu.PerThread, nav: InternPool.Nav.Index) link.File.UpdateNavError!void { return self.llvm_object.updateNav(pt, nav); } @@ -94,7 +100,7 @@ pub fn updateExports( self: *NvPtx, pt: Zcu.PerThread, exported: Zcu.Exported, - export_indices: []const u32, + export_indices: []const Zcu.Export.Index, ) !void { if (build_options.skip_non_native and builtin.object_format != .nvptx) @panic("Attempted to compile for object format that was disabled by build configuration"); @@ -102,10 +108,6 @@ pub fn updateExports( return self.llvm_object.updateExports(pt, exported, export_indices); } -pub fn freeDecl(self: *NvPtx, decl_index: InternPool.DeclIndex) void { - return self.llvm_object.freeDecl(decl_index); -} - pub fn flush(self: *NvPtx, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { return self.flushModule(arena, tid, prog_node); } diff --git a/src/link/Plan9.zig b/src/link/Plan9.zig index 31aac2486e..15c89693ff 100644 --- a/src/link/Plan9.zig +++ b/src/link/Plan9.zig @@ -60,7 +60,7 @@ fn_nav_table: std.AutoArrayHashMapUnmanaged( data_nav_table: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, []u8) = .empty, /// When `updateExports` is called, we store the export indices here, to be used /// during flush. -nav_exports: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, []u32) = .empty, +nav_exports: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, []Zcu.Export.Index) = .empty, lazy_syms: LazySymbolTable = .{}, @@ -345,6 +345,7 @@ fn putFn(self: *Plan9, nav_index: InternPool.Nav.Index, out: FnNavOutput) !void try a.writer().writeInt(u16, 1, .big); // getting the full file path + // TODO don't call getcwd here, that is inappropriate var buf: [std.fs.max_path_bytes]u8 = undefined; const full_path = try std.fs.path.join(arena, &.{ file.mod.root.root_dir.path orelse try std.posix.getcwd(&buf), @@ -385,7 +386,13 @@ fn addPathComponents(self: *Plan9, path: []const u8, a: *std.ArrayList(u8)) !voi } } -pub fn updateFunc(self: *Plan9, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { +pub fn updateFunc( + self: *Plan9, + pt: Zcu.PerThread, + func_index: InternPool.Index, + air: Air, + liveness: Liveness, +) link.File.UpdateNavError!void { if (build_options.skip_non_native and builtin.object_format != .plan9) { @panic("Attempted to compile for object format that was disabled by build configuration"); } @@ -397,8 +404,8 @@ pub fn updateFunc(self: *Plan9, pt: Zcu.PerThread, func_index: InternPool.Index, const atom_idx = try self.seeNav(pt, func.owner_nav); - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); var dbg_info_output: DebugInfoOutput = .{ .dbg_line = std.ArrayList(u8).init(gpa), .start_line = null, @@ -409,7 +416,7 @@ pub fn updateFunc(self: *Plan9, pt: Zcu.PerThread, func_index: InternPool.Index, }; defer dbg_info_output.dbg_line.deinit(); - const res = try codegen.generateFunction( + try codegen.generateFunction( &self.base, pt, zcu.navSrcLoc(func.owner_nav), @@ -419,10 +426,7 @@ pub fn updateFunc(self: *Plan9, pt: Zcu.PerThread, func_index: InternPool.Index, &code_buffer, .{ .plan9 = &dbg_info_output }, ); - const code = switch (res) { - .ok => try code_buffer.toOwnedSlice(), - .fail => |em| return zcu.failed_codegen.put(gpa, func.owner_nav, em), - }; + const code = try code_buffer.toOwnedSlice(gpa); self.getAtomPtr(atom_idx).code = .{ .code_ptr = null, .other = .{ .nav_index = func.owner_nav }, @@ -433,11 +437,13 @@ pub fn updateFunc(self: *Plan9, pt: Zcu.PerThread, func_index: InternPool.Index, .start_line = dbg_info_output.start_line.?, .end_line = dbg_info_output.end_line, }; - try self.putFn(func.owner_nav, out); + // The awkward error handling here is due to putFn calling `std.posix.getcwd` which it should not do. + self.putFn(func.owner_nav, out) catch |err| + return zcu.codegenFail(func.owner_nav, "failed to put fn: {s}", .{@errorName(err)}); return self.updateFinish(pt, func.owner_nav); } -pub fn updateNav(self: *Plan9, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) !void { +pub fn updateNav(self: *Plan9, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) link.File.UpdateNavError!void { const zcu = pt.zcu; const gpa = zcu.gpa; const ip = &zcu.intern_pool; @@ -456,10 +462,10 @@ pub fn updateNav(self: *Plan9, pt: Zcu.PerThread, nav_index: InternPool.Nav.Inde if (nav_init.typeOf(zcu).hasRuntimeBits(zcu)) { const atom_idx = try self.seeNav(pt, nav_index); - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); // TODO we need the symbol index for symbol in the table of locals for the containing atom - const res = try codegen.generateSymbol( + try codegen.generateSymbol( &self.base, pt, zcu.navSrcLoc(nav_index), @@ -467,10 +473,7 @@ pub fn updateNav(self: *Plan9, pt: Zcu.PerThread, nav_index: InternPool.Nav.Inde &code_buffer, .{ .atom_index = @intCast(atom_idx) }, ); - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| return zcu.failed_codegen.put(gpa, nav_index, em), - }; + const code = code_buffer.items; try self.data_nav_table.ensureUnusedCapacity(gpa, 1); const duped_code = try gpa.dupe(u8, code); self.getAtomPtr(self.navs.get(nav_index).?.index).code = .{ .code_ptr = null, .other = .{ .nav_index = nav_index } }; @@ -529,16 +532,21 @@ fn allocateGotIndex(self: *Plan9) usize { } } -pub fn flush(self: *Plan9, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { +pub fn flush( + self: *Plan9, + arena: Allocator, + tid: Zcu.PerThread.Id, + prog_node: std.Progress.Node, +) link.File.FlushError!void { const comp = self.base.comp; + const diags = &comp.link_diags; const use_lld = build_options.have_llvm and comp.config.use_lld; assert(!use_lld); switch (link.File.effectiveOutputMode(use_lld, comp.config.output_mode)) { .Exe => {}, - // plan9 object files are totally different - .Obj => return error.TODOImplementPlan9Objs, - .Lib => return error.TODOImplementWritingLibFiles, + .Obj => return diags.fail("writing plan9 object files unimplemented", .{}), + .Lib => return diags.fail("writing plan9 lib files unimplemented", .{}), } return self.flushModule(arena, tid, prog_node); } @@ -583,7 +591,13 @@ fn atomCount(self: *Plan9) usize { return data_nav_count + fn_nav_count + lazy_atom_count + extern_atom_count + uav_atom_count; } -pub fn flushModule(self: *Plan9, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { +pub fn flushModule( + self: *Plan9, + arena: Allocator, + /// TODO: stop using this + tid: Zcu.PerThread.Id, + prog_node: std.Progress.Node, +) link.File.FlushError!void { if (build_options.skip_non_native and builtin.object_format != .plan9) { @panic("Attempted to compile for object format that was disabled by build configuration"); } @@ -594,6 +608,7 @@ pub fn flushModule(self: *Plan9, arena: Allocator, tid: Zcu.PerThread.Id, prog_n _ = arena; // Has the same lifetime as the call to Compilation.update. const comp = self.base.comp; + const diags = &comp.link_diags; const gpa = comp.gpa; const target = comp.root_mod.resolved_target.result; @@ -605,7 +620,7 @@ pub fn flushModule(self: *Plan9, arena: Allocator, tid: Zcu.PerThread.Id, prog_n defer assert(self.hdr.entry != 0x0); const pt: Zcu.PerThread = .activate( - self.base.comp.zcu orelse return error.LinkingWithoutZigSourceUnimplemented, + self.base.comp.zcu orelse return diags.fail("linking without zig source unimplemented", .{}), tid, ); defer pt.deactivate(); @@ -614,22 +629,16 @@ pub fn flushModule(self: *Plan9, arena: Allocator, tid: Zcu.PerThread.Id, prog_n if (self.lazy_syms.getPtr(.none)) |metadata| { // Most lazy symbols can be updated on first use, but // anyerror needs to wait for everything to be flushed. - if (metadata.text_state != .unused) self.updateLazySymbolAtom( + if (metadata.text_state != .unused) try self.updateLazySymbolAtom( pt, .{ .kind = .code, .ty = .anyerror_type }, metadata.text_atom, - ) catch |err| return switch (err) { - error.CodegenFail => error.FlushFailure, - else => |e| e, - }; - if (metadata.rodata_state != .unused) self.updateLazySymbolAtom( + ); + if (metadata.rodata_state != .unused) try self.updateLazySymbolAtom( pt, .{ .kind = .const_data, .ty = .anyerror_type }, metadata.rodata_atom, - ) catch |err| return switch (err) { - error.CodegenFail => error.FlushFailure, - else => |e| e, - }; + ); } for (self.lazy_syms.values()) |*metadata| { if (metadata.text_state != .unused) metadata.text_state = .flushed; @@ -902,30 +911,29 @@ pub fn flushModule(self: *Plan9, arena: Allocator, tid: Zcu.PerThread.Id, prog_n } } } - // write it all! - try file.pwritevAll(iovecs, 0); + file.pwritevAll(iovecs, 0) catch |err| return diags.fail("failed to write file: {s}", .{@errorName(err)}); } fn addNavExports( self: *Plan9, - mod: *Zcu, + zcu: *Zcu, nav_index: InternPool.Nav.Index, - export_indices: []const u32, + export_indices: []const Zcu.Export.Index, ) !void { const gpa = self.base.comp.gpa; const metadata = self.navs.getPtr(nav_index).?; const atom = self.getAtom(metadata.index); for (export_indices) |export_idx| { - const exp = mod.all_exports.items[export_idx]; - const exp_name = exp.opts.name.toSlice(&mod.intern_pool); + const exp = export_idx.ptr(zcu); + const exp_name = exp.opts.name.toSlice(&zcu.intern_pool); // plan9 does not support custom sections if (exp.opts.section.unwrap()) |section_name| { - if (!section_name.eqlSlice(".text", &mod.intern_pool) and - !section_name.eqlSlice(".data", &mod.intern_pool)) + if (!section_name.eqlSlice(".text", &zcu.intern_pool) and + !section_name.eqlSlice(".data", &zcu.intern_pool)) { - try mod.failed_exports.put(mod.gpa, export_idx, try Zcu.ErrorMsg.create( + try zcu.failed_exports.put(zcu.gpa, export_idx, try Zcu.ErrorMsg.create( gpa, - mod.navSrcLoc(nav_index), + zcu.navSrcLoc(nav_index), "plan9 does not support extra sections", .{}, )); @@ -947,50 +955,6 @@ fn addNavExports( } } -pub fn freeDecl(self: *Plan9, decl_index: InternPool.DeclIndex) void { - const gpa = self.base.comp.gpa; - // TODO audit the lifetimes of decls table entries. It's possible to get - // freeDecl without any updateDecl in between. - const zcu = self.base.comp.zcu.?; - const decl = zcu.declPtr(decl_index); - const is_fn = decl.val.isFuncBody(zcu); - if (is_fn) { - const symidx_and_submap = self.fn_decl_table.get(decl.getFileScope(zcu)).?; - var submap = symidx_and_submap.functions; - if (submap.fetchSwapRemove(decl_index)) |removed_entry| { - gpa.free(removed_entry.value.code); - gpa.free(removed_entry.value.lineinfo); - } - if (submap.count() == 0) { - self.syms.items[symidx_and_submap.sym_index] = aout.Sym.undefined_symbol; - self.syms_index_free_list.append(gpa, symidx_and_submap.sym_index) catch {}; - submap.deinit(gpa); - } - } else { - if (self.data_decl_table.fetchSwapRemove(decl_index)) |removed_entry| { - gpa.free(removed_entry.value); - } - } - if (self.decls.fetchRemove(decl_index)) |const_kv| { - var kv = const_kv; - const atom = self.getAtom(kv.value.index); - if (atom.got_index) |i| { - // TODO: if this catch {} is triggered, an assertion in flushModule will be triggered, because got_index_free_list will have the wrong length - self.got_index_free_list.append(gpa, i) catch {}; - } - if (atom.sym_index) |i| { - self.syms_index_free_list.append(gpa, i) catch {}; - self.syms.items[i] = aout.Sym.undefined_symbol; - } - kv.value.exports.deinit(gpa); - } - { - const atom_index = self.decls.get(decl_index).?.index; - const relocs = self.relocs.getPtr(atom_index) orelse return; - relocs.clearAndFree(gpa); - assert(self.relocs.remove(atom_index)); - } -} fn createAtom(self: *Plan9) !Atom.Index { const gpa = self.base.comp.gpa; const index = @as(Atom.Index, @intCast(self.atoms.items.len)); @@ -1043,7 +1007,7 @@ pub fn updateExports( self: *Plan9, pt: Zcu.PerThread, exported: Zcu.Exported, - export_indices: []const u32, + export_indices: []const Zcu.Export.Index, ) !void { const gpa = self.base.comp.gpa; switch (exported) { @@ -1054,7 +1018,7 @@ pub fn updateExports( gpa.free(kv.value); } try self.nav_exports.ensureUnusedCapacity(gpa, 1); - const duped_indices = try gpa.dupe(u32, export_indices); + const duped_indices = try gpa.dupe(Zcu.Export.Index, export_indices); self.nav_exports.putAssumeCapacityNoClobber(nav, duped_indices); }, } @@ -1085,12 +1049,19 @@ pub fn getOrCreateAtomForLazySymbol(self: *Plan9, pt: Zcu.PerThread, lazy_sym: F return atom; } -fn updateLazySymbolAtom(self: *Plan9, pt: Zcu.PerThread, sym: File.LazySymbol, atom_index: Atom.Index) !void { +fn updateLazySymbolAtom( + self: *Plan9, + pt: Zcu.PerThread, + sym: File.LazySymbol, + atom_index: Atom.Index, +) error{ LinkFailure, OutOfMemory }!void { const gpa = pt.zcu.gpa; + const comp = self.base.comp; + const diags = &comp.link_diags; var required_alignment: InternPool.Alignment = .none; - var code_buffer = std.ArrayList(u8).init(gpa); - defer code_buffer.deinit(); + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); // create the symbol for the name const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{ @@ -1107,7 +1078,7 @@ fn updateLazySymbolAtom(self: *Plan9, pt: Zcu.PerThread, sym: File.LazySymbol, a // generate the code const src = Type.fromInterned(sym.ty).srcLocOrNull(pt.zcu) orelse Zcu.LazySrcLoc.unneeded; - const res = try codegen.generateLazySymbol( + codegen.generateLazySymbol( &self.base, pt, src, @@ -1116,14 +1087,12 @@ fn updateLazySymbolAtom(self: *Plan9, pt: Zcu.PerThread, sym: File.LazySymbol, a &code_buffer, .none, .{ .atom_index = @intCast(atom_index) }, - ); - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| { - log.err("{s}", .{em.msg}); - return error.CodegenFail; - }, + ) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.CodegenFail => return error.LinkFailure, + error.Overflow => return diags.fail("codegen failure: encountered number too big for compiler", .{}), }; + const code = code_buffer.items; // duped_code is freed when the atom is freed const duped_code = try gpa.dupe(u8, code); errdefer gpa.free(duped_code); @@ -1283,7 +1252,7 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { try self.writeSym(writer, sym); if (self.nav_exports.get(nav_index)) |export_indices| { for (export_indices) |export_idx| { - const exp = zcu.all_exports.items[export_idx]; + const exp = export_idx.ptr(zcu); if (nav_metadata.getExport(self, exp.opts.name.toSlice(ip))) |exp_i| { try self.writeSym(writer, self.syms.items[exp_i]); } @@ -1322,7 +1291,7 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { try self.writeSym(writer, sym); if (self.nav_exports.get(nav_index)) |export_indices| { for (export_indices) |export_idx| { - const exp = zcu.all_exports.items[export_idx]; + const exp = export_idx.ptr(zcu); if (nav_metadata.getExport(self, exp.opts.name.toSlice(ip))) |exp_i| { const s = self.syms.items[exp_i]; if (mem.eql(u8, s.name, "_start")) @@ -1432,19 +1401,16 @@ pub fn lowerUav( const got_index = self.allocateGotIndex(); gop.value_ptr.* = index; // we need to free name latex - var code_buffer = std.ArrayList(u8).init(gpa); - const res = try codegen.generateSymbol(&self.base, pt, src_loc, val, &code_buffer, .{ .atom_index = index }); - const code = switch (res) { - .ok => code_buffer.items, - .fail => |em| return .{ .fail = em }, - }; + var code_buffer: std.ArrayListUnmanaged(u8) = .empty; + defer code_buffer.deinit(gpa); + try codegen.generateSymbol(&self.base, pt, src_loc, val, &code_buffer, .{ .atom_index = index }); const atom_ptr = self.getAtomPtr(index); atom_ptr.* = .{ .type = .d, .offset = undefined, .sym_index = null, .got_index = got_index, - .code = Atom.CodePtr.fromSlice(code), + .code = Atom.CodePtr.fromSlice(try code_buffer.toOwnedSlice(gpa)), }; _ = try atom_ptr.getOrCreateSymbolTableEntry(self); self.syms.items[atom_ptr.sym_index.?] = .{ diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index b1b8945963..a5b9615c5e 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -122,7 +122,13 @@ pub fn deinit(self: *SpirV) void { self.object.deinit(); } -pub fn updateFunc(self: *SpirV, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { +pub fn updateFunc( + self: *SpirV, + pt: Zcu.PerThread, + func_index: InternPool.Index, + air: Air, + liveness: Liveness, +) link.File.UpdateNavError!void { if (build_options.skip_non_native) { @panic("Attempted to compile for architecture that was disabled by build configuration"); } @@ -134,7 +140,7 @@ pub fn updateFunc(self: *SpirV, pt: Zcu.PerThread, func_index: InternPool.Index, try self.object.updateFunc(pt, func_index, air, liveness); } -pub fn updateNav(self: *SpirV, pt: Zcu.PerThread, nav: InternPool.Nav.Index) !void { +pub fn updateNav(self: *SpirV, pt: Zcu.PerThread, nav: InternPool.Nav.Index) link.File.UpdateNavError!void { if (build_options.skip_non_native) { @panic("Attempted to compile for architecture that was disabled by build configuration"); } @@ -149,7 +155,7 @@ pub fn updateExports( self: *SpirV, pt: Zcu.PerThread, exported: Zcu.Exported, - export_indices: []const u32, + export_indices: []const Zcu.Export.Index, ) !void { const zcu = pt.zcu; const ip = &zcu.intern_pool; @@ -184,7 +190,7 @@ pub fn updateExports( }; for (export_indices) |export_idx| { - const exp = zcu.all_exports.items[export_idx]; + const exp = export_idx.ptr(zcu); try self.object.spv.declareEntryPoint( spv_decl_index, exp.opts.name.toSlice(ip), @@ -196,16 +202,21 @@ pub fn updateExports( // TODO: Export regular functions, variables, etc using Linkage attributes. } -pub fn freeDecl(self: *SpirV, decl_index: InternPool.DeclIndex) void { - _ = self; - _ = decl_index; -} - pub fn flush(self: *SpirV, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { return self.flushModule(arena, tid, prog_node); } -pub fn flushModule(self: *SpirV, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { +pub fn flushModule( + self: *SpirV, + arena: Allocator, + tid: Zcu.PerThread.Id, + prog_node: std.Progress.Node, +) link.File.FlushError!void { + // The goal is to never use this because it's only needed if we need to + // write to InternPool, but flushModule is too late to be writing to the + // InternPool. + _ = tid; + if (build_options.skip_non_native) { @panic("Attempted to compile for architecture that was disabled by build configuration"); } @@ -216,12 +227,11 @@ pub fn flushModule(self: *SpirV, arena: Allocator, tid: Zcu.PerThread.Id, prog_n const sub_prog_node = prog_node.start("Flush Module", 0); defer sub_prog_node.end(); - const spv = &self.object.spv; - const comp = self.base.comp; + const spv = &self.object.spv; + const diags = &comp.link_diags; const gpa = comp.gpa; const target = comp.getTarget(); - _ = tid; try writeCapabilities(spv, target); try writeMemoryModel(spv, target); @@ -264,13 +274,11 @@ pub fn flushModule(self: *SpirV, arena: Allocator, tid: Zcu.PerThread.Id, prog_n const linked_module = self.linkModule(arena, module, sub_prog_node) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, - else => |other| { - log.err("error while linking: {s}", .{@errorName(other)}); - return error.FlushFailure; - }, + else => |other| return diags.fail("error while linking: {s}", .{@errorName(other)}), }; - try self.base.file.?.writeAll(std.mem.sliceAsBytes(linked_module)); + self.base.file.?.writeAll(std.mem.sliceAsBytes(linked_module)) catch |err| + return diags.fail("failed to write: {s}", .{@errorName(err)}); } fn linkModule(self: *SpirV, a: Allocator, module: []Word, progress: std.Progress.Node) ![]Word { diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 7bb1d8c476..cf1a560fb3 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -1,45 +1,67 @@ +//! The overall strategy here is to load all the object file data into memory +//! as inputs are parsed. During `prelink`, as much linking as possible is +//! performed without any knowledge of functions and globals provided by the +//! Zcu. If there is no Zcu, effectively all linking is done in `prelink`. +//! +//! `updateFunc`, `updateNav`, `updateExports`, and `deleteExport` are handled +//! by merely tracking references to the relevant functions and globals. All +//! the linking logic between objects and Zcu happens in `flush`. Many +//! components of the final output are computed on-the-fly at this time rather +//! than being precomputed and stored separately. + const Wasm = @This(); -const build_options = @import("build_options"); +const Archive = @import("Wasm/Archive.zig"); +const Object = @import("Wasm/Object.zig"); +pub const Flush = @import("Wasm/Flush.zig"); const builtin = @import("builtin"); const native_endian = builtin.cpu.arch.endian(); +const build_options = @import("build_options"); + const std = @import("std"); const Allocator = std.mem.Allocator; const Cache = std.Build.Cache; const Path = Cache.Path; const assert = std.debug.assert; const fs = std.fs; -const gc_log = std.log.scoped(.gc); const leb = std.leb; const log = std.log.scoped(.link); const mem = std.mem; const Air = @import("../Air.zig"); -const Archive = @import("Wasm/Archive.zig"); +const Mir = @import("../arch/wasm/Mir.zig"); const CodeGen = @import("../arch/wasm/CodeGen.zig"); +const abi = @import("../arch/wasm/abi.zig"); const Compilation = @import("../Compilation.zig"); const Dwarf = @import("Dwarf.zig"); const InternPool = @import("../InternPool.zig"); const Liveness = @import("../Liveness.zig"); const LlvmObject = @import("../codegen/llvm.zig").Object; -const Object = @import("Wasm/Object.zig"); -const Symbol = @import("Wasm/Symbol.zig"); -const Type = @import("../Type.zig"); -const Value = @import("../Value.zig"); const Zcu = @import("../Zcu.zig"); -const ZigObject = @import("Wasm/ZigObject.zig"); const codegen = @import("../codegen.zig"); const dev = @import("../dev.zig"); const link = @import("../link.zig"); const lldMain = @import("../main.zig").lldMain; const trace = @import("../tracy.zig").trace; const wasi_libc = @import("../wasi_libc.zig"); +const Value = @import("../Value.zig"); base: link.File, /// Null-terminated strings, indexes have type String and string_table provides /// lookup. +/// +/// There are a couple of sites that add things here without adding +/// corresponding string_table entries. For such cases, when implementing +/// serialization/deserialization, they should be adjusted to prefix that data +/// with a null byte so that deserialization does not attempt to create +/// string_table entries for them. Alternately those sites could be moved to +/// use a different byte array for this purpose. string_bytes: std.ArrayListUnmanaged(u8), +/// Sometimes we have logic that wants to borrow string bytes to store +/// arbitrary things in there. In this case it is not allowed to intern new +/// strings during this time. This safety lock is used to detect misuses. +string_bytes_lock: std.debug.SafetyLock = .{}, /// Omitted when serializing linker state. string_table: String.Table, /// Symbol name of the entry function to export @@ -62,2525 +84,3230 @@ export_table: bool, name: []const u8, /// If this is not null, an object file is created by LLVM and linked with LLD afterwards. llvm_object: ?LlvmObject.Ptr = null, -zig_object: ?*ZigObject, /// List of relocatable files to be linked into the final binary. objects: std.ArrayListUnmanaged(Object) = .{}, + +func_types: std.AutoArrayHashMapUnmanaged(FunctionType, void) = .empty, +/// Provides a mapping of both imports and provided functions to symbol name. +/// Local functions may be unnamed. +/// Key is symbol name, however the `FunctionImport` may have an name override for the import name. +object_function_imports: std.AutoArrayHashMapUnmanaged(String, FunctionImport) = .empty, +/// All functions for all objects. +object_functions: std.ArrayListUnmanaged(ObjectFunction) = .empty, + +/// Provides a mapping of both imports and provided globals to symbol name. +/// Local globals may be unnamed. +object_global_imports: std.AutoArrayHashMapUnmanaged(String, GlobalImport) = .empty, +/// All globals for all objects. +object_globals: std.ArrayListUnmanaged(ObjectGlobal) = .empty, + +/// All table imports for all objects. +object_table_imports: std.AutoArrayHashMapUnmanaged(String, TableImport) = .empty, +/// All parsed table sections for all objects. +object_tables: std.ArrayListUnmanaged(Table) = .empty, + +/// All memory imports for all objects. +object_memory_imports: std.AutoArrayHashMapUnmanaged(String, MemoryImport) = .empty, +/// All parsed memory sections for all objects. +object_memories: std.ArrayListUnmanaged(ObjectMemory) = .empty, + +/// All relocations from all objects concatenated. `relocs_start` marks the end +/// point of object relocations and start point of Zcu relocations. +object_relocations: std.MultiArrayList(ObjectRelocation) = .empty, + +/// List of initialization functions. These must be called in order of priority +/// by the (synthetic) `__wasm_call_ctors` function. +object_init_funcs: std.ArrayListUnmanaged(InitFunc) = .empty, + +/// The data section of an object has many segments. Each segment corresponds +/// logically to an object file's .data section, or .rodata section. In +/// the case of `-fdata-sections` there will be one segment per data symbol. +object_data_segments: std.ArrayListUnmanaged(ObjectDataSegment) = .empty, +/// Each segment has many data symbols, which correspond logically to global +/// constants. +object_datas: std.ArrayListUnmanaged(ObjectData) = .empty, +object_data_imports: std.AutoArrayHashMapUnmanaged(String, ObjectDataImport) = .empty, +/// Non-synthetic section that can essentially be mem-cpy'd into place after performing relocations. +object_custom_segments: std.AutoArrayHashMapUnmanaged(ObjectSectionIndex, CustomSegment) = .empty, + +/// All comdat information for all objects. +object_comdats: std.ArrayListUnmanaged(Comdat) = .empty, +/// A table that maps the relocations to be performed where the key represents +/// the section (across all objects) that the slice of relocations applies to. +object_relocations_table: std.AutoArrayHashMapUnmanaged(ObjectSectionIndex, ObjectRelocation.Slice) = .empty, +/// Incremented across all objects in order to enable calculation of `ObjectSectionIndex` values. +object_total_sections: u32 = 0, +/// All comdat symbols from all objects concatenated. +object_comdat_symbols: std.MultiArrayList(Comdat.Symbol) = .empty, + +/// Relocations to be emitted into an object file. Remains empty when not +/// emitting an object file. +out_relocs: std.MultiArrayList(OutReloc) = .empty, +/// List of locations within `string_bytes` that must be patched with the virtual +/// memory address of a Uav during `flush`. +/// When emitting an object file, `out_relocs` is used instead. +uav_fixups: std.ArrayListUnmanaged(UavFixup) = .empty, +/// List of locations within `string_bytes` that must be patched with the virtual +/// memory address of a Nav during `flush`. +/// When emitting an object file, `out_relocs` is used instead. +/// No functions here only global variables. +nav_fixups: std.ArrayListUnmanaged(NavFixup) = .empty, +/// When a nav reference is a function pointer, this tracks the required function +/// table entry index that needs to overwrite the code in the final output. +func_table_fixups: std.ArrayListUnmanaged(FuncTableFixup) = .empty, +/// Symbols to be emitted into an object file. Remains empty when not emitting +/// an object file. +symbol_table: std.AutoArrayHashMapUnmanaged(String, void) = .empty, + /// When importing objects from the host environment, a name must be supplied. -/// LLVM uses "env" by default when none is given. This would be a good default for Zig -/// to support existing code. -/// TODO: Allow setting this through a flag? -host_name: String, -/// List of symbols generated by the linker. -synthetic_symbols: std.ArrayListUnmanaged(Symbol) = .empty, -/// Maps atoms to their segment index -atoms: std.AutoHashMapUnmanaged(Segment.Index, Atom.Index) = .empty, -/// List of all atoms. -managed_atoms: std.ArrayListUnmanaged(Atom) = .empty, - -/// The count of imported functions. This number will be appended -/// to the function indexes as their index starts at the lowest non-extern function. -imported_functions_count: u32 = 0, -/// The count of imported wasm globals. This number will be appended -/// to the global indexes when sections are merged. -imported_globals_count: u32 = 0, -/// The count of imported tables. This number will be appended -/// to the table indexes when sections are merged. -imported_tables_count: u32 = 0, -/// Map of symbol locations, represented by its `Import` -imports: std.AutoHashMapUnmanaged(SymbolLoc, Import) = .empty, -/// Represents non-synthetic section entries. -/// Used for code, data and custom sections. -segments: std.ArrayListUnmanaged(Segment) = .empty, -/// Maps a data segment key (such as .rodata) to the index into `segments`. -data_segments: std.StringArrayHashMapUnmanaged(Segment.Index) = .empty, -/// A table of `NamedSegment` which provide meta data -/// about a data symbol such as its name where the key is -/// the segment index, which can be found from `data_segments` -segment_info: std.AutoArrayHashMapUnmanaged(Segment.Index, NamedSegment) = .empty, - -// Output sections -/// Output type section -func_types: std.ArrayListUnmanaged(std.wasm.Type) = .empty, -/// Output function section where the key is the original -/// function index and the value is function. -/// This allows us to map multiple symbols to the same function. -functions: std.AutoArrayHashMapUnmanaged( - struct { - /// `none` in the case of synthetic sections. - file: OptionalObjectId, - index: u32, - }, - struct { - func: std.wasm.Func, - sym_index: Symbol.Index, - }, -) = .{}, -/// Output global section -wasm_globals: std.ArrayListUnmanaged(std.wasm.Global) = .empty, +/// LLVM uses "env" by default when none is given. +/// This value is passed to object files since wasm tooling conventions provides +/// no way to specify the module name in the symbol table. +object_host_name: OptionalString, + /// Memory section memories: std.wasm.Memory = .{ .limits = .{ .min = 0, - .max = undefined, - .flags = 0, + .max = 0, + .flags = .{ .has_max = false, .is_shared = false }, } }, -/// Output table section -tables: std.ArrayListUnmanaged(std.wasm.Table) = .empty, -/// Output export section -exports: std.ArrayListUnmanaged(Export) = .empty, -/// List of initialization functions. These must be called in order of priority -/// by the (synthetic) __wasm_call_ctors function. -init_funcs: std.ArrayListUnmanaged(InitFuncLoc) = .empty, -/// Index to a function defining the entry of the wasm file -entry: ?u32 = null, - -/// Indirect function table, used to call function pointers -/// When this is non-zero, we must emit a table entry, -/// as well as an 'elements' section. -/// -/// Note: Key is symbol location, value represents the index into the table -function_table: std.AutoHashMapUnmanaged(SymbolLoc, u32) = .empty, - -/// All archive files that are lazy loaded. -/// e.g. when an undefined symbol references a symbol from the archive. -/// None of this data is serialized to disk because it is trivially reloaded -/// from unchanged archive files on the next start of the compiler process, -/// or if those files have changed, the prelink phase needs to be restarted. -lazy_archives: std.ArrayListUnmanaged(LazyArchive) = .empty, - -/// A map of global names to their symbol location -globals: std.AutoArrayHashMapUnmanaged(String, SymbolLoc) = .empty, -/// The list of GOT symbols and their location -got_symbols: std.ArrayListUnmanaged(SymbolLoc) = .empty, -/// Maps discarded symbols and their positions to the location of the symbol -/// it was resolved to -discarded: std.AutoHashMapUnmanaged(SymbolLoc, SymbolLoc) = .empty, -/// List of all symbol locations which have been resolved by the linker and will be emit -/// into the final binary. -resolved_symbols: std.AutoArrayHashMapUnmanaged(SymbolLoc, void) = .empty, -/// Symbols that remain undefined after symbol resolution. -undefs: std.AutoArrayHashMapUnmanaged(String, SymbolLoc) = .empty, -/// Maps a symbol's location to an atom. This can be used to find meta -/// data of a symbol, such as its size, or its offset to perform a relocation. -/// Undefined (and synthetic) symbols do not have an Atom and therefore cannot be mapped. -symbol_atom: std.AutoHashMapUnmanaged(SymbolLoc, Atom.Index) = .empty, /// `--verbose-link` output. /// Initialized on creation, appended to as inputs are added, printed during `flush`. /// String data is allocated into Compilation arena. dump_argv_list: std.ArrayListUnmanaged([]const u8), -/// Represents the index into `segments` where the 'code' section lives. -code_section_index: Segment.OptionalIndex = .none, -custom_sections: CustomSections, preloaded_strings: PreloadedStrings, -/// Type reflection is used on the field names to autopopulate each field -/// during initialization. -const PreloadedStrings = struct { - __heap_base: String, - __heap_end: String, - __indirect_function_table: String, - __linear_memory: String, - __stack_pointer: String, - __tls_align: String, - __tls_base: String, - __tls_size: String, - __wasm_apply_global_tls_relocs: String, - __wasm_call_ctors: String, - __wasm_init_memory: String, - __wasm_init_memory_flag: String, - __wasm_init_tls: String, - __zig_err_name_table: String, - __zig_err_names: String, - __zig_errors_len: String, - _initialize: String, - _start: String, - memory: String, +/// This field is used when emitting an object; `navs_exe` used otherwise. +/// Does not include externs since that data lives elsewhere. +navs_obj: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, ZcuDataObj) = .empty, +/// This field is unused when emitting an object; `navs_obj` used otherwise. +/// Does not include externs since that data lives elsewhere. +navs_exe: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, ZcuDataExe) = .empty, +/// Tracks all InternPool values referenced by codegen. Needed for outputting +/// the data segment. This one does not track ref count because object files +/// require using max LEB encoding for these references anyway. +uavs_obj: std.AutoArrayHashMapUnmanaged(InternPool.Index, ZcuDataObj) = .empty, +/// Tracks ref count to optimize LEB encodings for UAV references. +uavs_exe: std.AutoArrayHashMapUnmanaged(InternPool.Index, ZcuDataExe) = .empty, +/// Sparse table of uavs that need to be emitted with greater alignment than +/// the default for the type. +overaligned_uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment) = .empty, +/// When the key is an enum type, this represents a `@tagName` function. +zcu_funcs: std.AutoArrayHashMapUnmanaged(InternPool.Index, ZcuFunc) = .empty, +nav_exports: std.AutoArrayHashMapUnmanaged(NavExport, Zcu.Export.Index) = .empty, +uav_exports: std.AutoArrayHashMapUnmanaged(UavExport, Zcu.Export.Index) = .empty, +imports: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, void) = .empty, + +dwarf: ?Dwarf = null, + +flush_buffer: Flush = .{}, + +/// Empty until `prelink`. There it is populated based on object files. +/// Next, it is copied into `Flush.missing_exports` just before `flush` +/// and that data is used during `flush`. +missing_exports: std.AutoArrayHashMapUnmanaged(String, void) = .empty, +entry_resolution: FunctionImport.Resolution = .unresolved, + +/// Empty when outputting an object. +function_exports: std.AutoArrayHashMapUnmanaged(String, FunctionIndex) = .empty, +hidden_function_exports: std.AutoArrayHashMapUnmanaged(String, FunctionIndex) = .empty, +global_exports: std.ArrayListUnmanaged(GlobalExport) = .empty, +/// Tracks the value at the end of prelink. +global_exports_len: u32 = 0, + +/// Ordered list of non-import functions that will appear in the final binary. +/// Empty until prelink. +functions: std.AutoArrayHashMapUnmanaged(FunctionImport.Resolution, void) = .empty, +/// Tracks the value at the end of prelink, at which point `functions` +/// contains only object file functions, and nothing from the Zcu yet. +functions_end_prelink: u32 = 0, + +function_imports_len_prelink: u32 = 0, +data_imports_len_prelink: u32 = 0, +/// At the end of prelink, this is populated with needed functions from +/// objects. +/// +/// During the Zcu phase, entries are not deleted from this table +/// because doing so would be irreversible when a `deleteExport` call is +/// handled. However, entries are added during the Zcu phase when extern +/// functions are passed to `updateNav`. +/// +/// `flush` gets a copy of this table, and then Zcu exports are applied to +/// remove elements from the table, and the remainder are either undefined +/// symbol errors, or import section entries depending on the output mode. +function_imports: std.AutoArrayHashMapUnmanaged(String, FunctionImportId) = .empty, + +/// At the end of prelink, this is populated with data symbols needed by +/// objects. +/// +/// During the Zcu phase, entries are not deleted from this table +/// because doing so would be irreversible when a `deleteExport` call is +/// handled. However, entries are added during the Zcu phase when extern +/// functions are passed to `updateNav`. +/// +/// `flush` gets a copy of this table, and then Zcu exports are applied to +/// remove elements from the table, and the remainder are either undefined +/// symbol errors, or symbol table entries depending on the output mode. +data_imports: std.AutoArrayHashMapUnmanaged(String, DataImportId) = .empty, +/// Set of data symbols that will appear in the final binary. Used to populate +/// `Flush.data_segments` before sorting. +data_segments: std.AutoArrayHashMapUnmanaged(DataSegmentId, void) = .empty, + +/// Ordered list of non-import globals that will appear in the final binary. +/// Empty until prelink. +globals: std.AutoArrayHashMapUnmanaged(GlobalImport.Resolution, void) = .empty, +/// Tracks the value at the end of prelink, at which point `globals` +/// contains only object file globals, and nothing from the Zcu yet. +globals_end_prelink: u32 = 0, +global_imports: std.AutoArrayHashMapUnmanaged(String, GlobalImportId) = .empty, + +/// Ordered list of non-import tables that will appear in the final binary. +/// Empty until prelink. +tables: std.AutoArrayHashMapUnmanaged(TableImport.Resolution, void) = .empty, +table_imports: std.AutoArrayHashMapUnmanaged(String, TableImport.Index) = .empty, + +/// All functions that have had their address taken and therefore might be +/// called via a `call_indirect` function. +zcu_indirect_function_set: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, void) = .empty, +object_indirect_function_import_set: std.AutoArrayHashMapUnmanaged(String, void) = .empty, +object_indirect_function_set: std.AutoArrayHashMapUnmanaged(ObjectFunctionIndex, void) = .empty, + +error_name_table_ref_count: u32 = 0, +tag_name_table_ref_count: u32 = 0, + +/// Set to true if any `GLOBAL_INDEX` relocation is encountered with +/// `SymbolFlags.tls` set to true. This is for objects only; final +/// value must be this OR'd with the same logic for zig functions +/// (set to true if any threadlocal global is used). +any_tls_relocs: bool = false, +any_passive_inits: bool = false, + +/// All MIR instructions for all Zcu functions. +mir_instructions: std.MultiArrayList(Mir.Inst) = .{}, +/// Corresponds to `mir_instructions`. +mir_extra: std.ArrayListUnmanaged(u32) = .empty, +/// All local types for all Zcu functions. +all_zcu_locals: std.ArrayListUnmanaged(std.wasm.Valtype) = .empty, + +params_scratch: std.ArrayListUnmanaged(std.wasm.Valtype) = .empty, +returns_scratch: std.ArrayListUnmanaged(std.wasm.Valtype) = .empty, + +/// All Zcu error names in order, null-terminated, concatenated. No need to +/// serialize; trivially reconstructed. +error_name_bytes: std.ArrayListUnmanaged(u8) = .empty, +/// For each Zcu error, in order, offset into `error_name_bytes` where the name +/// is stored. No need to serialize; trivially reconstructed. +error_name_offs: std.ArrayListUnmanaged(u32) = .empty, + +tag_name_bytes: std.ArrayListUnmanaged(u8) = .empty, +tag_name_offs: std.ArrayListUnmanaged(u32) = .empty, + +pub const TagNameOff = extern struct { + off: u32, + len: u32, }; -/// Type reflection is used on the field names to autopopulate each inner `name` field. -const CustomSections = struct { - @".debug_info": CustomSection, - @".debug_pubtypes": CustomSection, - @".debug_abbrev": CustomSection, - @".debug_line": CustomSection, - @".debug_str": CustomSection, - @".debug_pubnames": CustomSection, - @".debug_loc": CustomSection, - @".debug_ranges": CustomSection, +/// Index into `Wasm.zcu_indirect_function_set`. +pub const ZcuIndirectFunctionSetIndex = enum(u32) { + _, }; -const CustomSection = struct { - name: String, - index: Segment.OptionalIndex, +pub const UavFixup = extern struct { + uavs_exe_index: UavsExeIndex, + /// Index into `string_bytes`. + offset: u32, + addend: u32, }; -/// Index into string_bytes -pub const String = enum(u32) { - _, +pub const NavFixup = extern struct { + navs_exe_index: NavsExeIndex, + /// Index into `string_bytes`. + offset: u32, + addend: u32, +}; - const Table = std.HashMapUnmanaged(String, void, TableContext, std.hash_map.default_max_load_percentage); +pub const FuncTableFixup = extern struct { + table_index: ZcuIndirectFunctionSetIndex, + /// Index into `string_bytes`. + offset: u32, +}; - const TableContext = struct { - bytes: []const u8, +/// Index into `objects`. +pub const ObjectIndex = enum(u32) { + _, - pub fn eql(_: @This(), a: String, b: String) bool { - return a == b; - } + pub fn ptr(index: ObjectIndex, wasm: *const Wasm) *Object { + return &wasm.objects.items[@intFromEnum(index)]; + } +}; - pub fn hash(ctx: @This(), key: String) u64 { - return std.hash_map.hashString(mem.sliceTo(ctx.bytes[@intFromEnum(key)..], 0)); - } - }; +/// Index into `Wasm.functions`. +pub const FunctionIndex = enum(u32) { + _, - const TableIndexAdapter = struct { - bytes: []const u8, + pub fn ptr(index: FunctionIndex, wasm: *const Wasm) *FunctionImport.Resolution { + return &wasm.functions.keys()[@intFromEnum(index)]; + } - pub fn eql(ctx: @This(), a: []const u8, b: String) bool { - return mem.eql(u8, a, mem.sliceTo(ctx.bytes[@intFromEnum(b)..], 0)); - } + pub fn fromIpNav(wasm: *const Wasm, nav_index: InternPool.Nav.Index) ?FunctionIndex { + return fromResolution(wasm, .fromIpNav(wasm, nav_index)); + } - pub fn hash(_: @This(), adapted_key: []const u8) u64 { - assert(mem.indexOfScalar(u8, adapted_key, 0) == null); - return std.hash_map.hashString(adapted_key); + pub fn fromTagNameType(wasm: *const Wasm, tag_type: InternPool.Index) ?FunctionIndex { + const zcu_func: ZcuFunc.Index = @enumFromInt(wasm.zcu_funcs.getIndex(tag_type) orelse return null); + return fromResolution(wasm, .pack(wasm, .{ .zcu_func = zcu_func })); + } + + pub fn fromSymbolName(wasm: *const Wasm, name: String) ?FunctionIndex { + if (wasm.object_function_imports.getPtr(name)) |import| { + return fromResolution(wasm, import.resolution); } - }; + if (wasm.function_exports.get(name)) |index| return index; + if (wasm.hidden_function_exports.get(name)) |index| return index; + return null; + } - pub fn toOptional(i: String) OptionalString { - const result: OptionalString = @enumFromInt(@intFromEnum(i)); - assert(result != .none); - return result; + pub fn fromResolution(wasm: *const Wasm, resolution: FunctionImport.Resolution) ?FunctionIndex { + const i = wasm.functions.getIndex(resolution) orelse return null; + return @enumFromInt(i); } }; -pub const OptionalString = enum(u32) { - none = std.math.maxInt(u32), +pub const GlobalExport = extern struct { + name: String, + global_index: GlobalIndex, +}; + +/// 0. Index into `Flush.function_imports` +/// 1. Index into `functions`. +/// +/// Note that function_imports indexes are subject to swap removals during +/// `flush`. +pub const OutputFunctionIndex = enum(u32) { _, - pub fn unwrap(i: OptionalString) ?String { - if (i == .none) return null; - return @enumFromInt(@intFromEnum(i)); + pub fn fromResolution(wasm: *const Wasm, resolution: FunctionImport.Resolution) ?OutputFunctionIndex { + return fromFunctionIndex(wasm, FunctionIndex.fromResolution(wasm, resolution) orelse return null); + } + + pub fn fromFunctionIndex(wasm: *const Wasm, index: FunctionIndex) OutputFunctionIndex { + return @enumFromInt(wasm.flush_buffer.function_imports.entries.len + @intFromEnum(index)); + } + + pub fn fromObjectFunction(wasm: *const Wasm, index: ObjectFunctionIndex) OutputFunctionIndex { + return fromResolution(wasm, .fromObjectFunction(wasm, index)).?; + } + + pub fn fromObjectFunctionHandlingWeak(wasm: *const Wasm, index: ObjectFunctionIndex) OutputFunctionIndex { + const ptr = index.ptr(wasm); + if (ptr.flags.binding == .weak) { + const name = ptr.name.unwrap().?; + const import = wasm.object_function_imports.getPtr(name).?; + assert(import.resolution != .unresolved); + return fromResolution(wasm, import.resolution).?; + } + return fromResolution(wasm, .fromObjectFunction(wasm, index)).?; + } + + pub fn fromIpIndex(wasm: *const Wasm, ip_index: InternPool.Index) OutputFunctionIndex { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + return switch (ip.indexToKey(ip_index)) { + .@"extern" => |ext| { + const name = wasm.getExistingString(ext.name.toSlice(ip)).?; + return fromSymbolName(wasm, name); + }, + else => fromResolution(wasm, .fromIpIndex(wasm, ip_index)).?, + }; + } + + pub fn fromIpNav(wasm: *const Wasm, nav_index: InternPool.Nav.Index) OutputFunctionIndex { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const nav = ip.getNav(nav_index); + return fromIpIndex(wasm, nav.status.fully_resolved.val); + } + + pub fn fromTagNameType(wasm: *const Wasm, tag_type: InternPool.Index) OutputFunctionIndex { + return fromFunctionIndex(wasm, FunctionIndex.fromTagNameType(wasm, tag_type).?); + } + + pub fn fromSymbolName(wasm: *const Wasm, name: String) OutputFunctionIndex { + if (wasm.flush_buffer.function_imports.getIndex(name)) |i| return @enumFromInt(i); + return fromFunctionIndex(wasm, FunctionIndex.fromSymbolName(wasm, name).?); } }; -/// Index into objects array or the zig object. -pub const ObjectId = enum(u16) { - zig_object = std.math.maxInt(u16) - 1, +/// Index into `Wasm.globals`. +pub const GlobalIndex = enum(u32) { _, - pub fn toOptional(i: ObjectId) OptionalObjectId { - const result: OptionalObjectId = @enumFromInt(@intFromEnum(i)); - assert(result != .none); - return result; + /// This is only accurate when not emitting an object and there is a Zcu. + pub const stack_pointer: GlobalIndex = @enumFromInt(0); + + /// Same as `stack_pointer` but with a safety assertion. + pub fn stackPointer(wasm: *const Wasm) ObjectGlobal.Index { + const comp = wasm.base.comp; + assert(comp.config.output_mode != .Obj); + assert(comp.zcu != null); + return .stack_pointer; + } + + pub fn ptr(index: GlobalIndex, f: *const Flush) *Wasm.GlobalImport.Resolution { + return &f.globals.items[@intFromEnum(index)]; + } + + pub fn fromIpNav(wasm: *const Wasm, nav_index: InternPool.Nav.Index) ?GlobalIndex { + const i = wasm.globals.getIndex(.fromIpNav(wasm, nav_index)) orelse return null; + return @enumFromInt(i); + } + + pub fn fromObjectGlobal(wasm: *const Wasm, i: ObjectGlobalIndex) GlobalIndex { + return @enumFromInt(wasm.globals.getIndex(.fromObjectGlobal(wasm, i)).?); + } + + pub fn fromObjectGlobalHandlingWeak(wasm: *const Wasm, index: ObjectGlobalIndex) GlobalIndex { + const global = index.ptr(wasm); + return if (global.flags.binding == .weak) + fromSymbolName(wasm, global.name.unwrap().?) + else + fromObjectGlobal(wasm, index); + } + + pub fn fromSymbolName(wasm: *const Wasm, name: String) GlobalIndex { + const import = wasm.object_global_imports.getPtr(name).?; + return @enumFromInt(wasm.globals.getIndex(import.resolution).?); } }; -/// Optional index into objects array or the zig object. -pub const OptionalObjectId = enum(u16) { - zig_object = std.math.maxInt(u16) - 1, - none = std.math.maxInt(u16), +/// Index into `tables`. +pub const TableIndex = enum(u32) { _, - pub fn unwrap(i: OptionalObjectId) ?ObjectId { - if (i == .none) return null; - return @enumFromInt(@intFromEnum(i)); + pub fn ptr(index: TableIndex, f: *const Flush) *Wasm.TableImport.Resolution { + return &f.tables.items[@intFromEnum(index)]; } -}; -/// None of this data is serialized since it can be re-loaded from disk, or if -/// it has been changed, the data must be discarded. -const LazyArchive = struct { - path: Path, - file_contents: []const u8, - archive: Archive, + pub fn fromObjectTable(wasm: *const Wasm, i: ObjectTableIndex) TableIndex { + return @enumFromInt(wasm.tables.getIndex(.fromObjectTable(i)).?); + } - fn deinit(la: *LazyArchive, gpa: Allocator) void { - la.archive.deinit(gpa); - gpa.free(la.path.sub_path); - gpa.free(la.file_contents); - la.* = undefined; + pub fn fromSymbolName(wasm: *const Wasm, name: String) TableIndex { + const import = wasm.object_table_imports.getPtr(name).?; + return @enumFromInt(wasm.tables.getIndex(import.resolution).?); } }; -pub const Segment = struct { - alignment: Alignment, - size: u32, - offset: u32, - flags: u32, +/// The first N indexes correspond to input objects (`objects`) array. +/// After that, the indexes correspond to the `source_locations` array, +/// representing a location in a Zig source file that can be pinpointed +/// precisely via AST node and token. +pub const SourceLocation = enum(u32) { + /// From the Zig compilation unit but no precise source location. + zig_object_nofile = std.math.maxInt(u32) - 1, + none = std.math.maxInt(u32), + _, - const Index = enum(u32) { + /// Index into `source_locations`. + pub const Index = enum(u32) { _, - - pub fn toOptional(i: Index) OptionalIndex { - const result: OptionalIndex = @enumFromInt(@intFromEnum(i)); - assert(result != .none); - return result; - } }; - const OptionalIndex = enum(u32) { - none = std.math.maxInt(u32), - _, - - pub fn unwrap(i: OptionalIndex) ?Index { - if (i == .none) return null; - return @enumFromInt(@intFromEnum(i)); - } + pub const Unpacked = union(enum) { + none, + zig_object_nofile, + object_index: ObjectIndex, + source_location_index: Index, }; - pub const Flag = enum(u32) { - WASM_DATA_SEGMENT_IS_PASSIVE = 0x01, - WASM_DATA_SEGMENT_HAS_MEMINDEX = 0x02, - }; + pub fn pack(unpacked: Unpacked, wasm: *const Wasm) SourceLocation { + _ = wasm; + return switch (unpacked) { + .zig_object_nofile => .zig_object_nofile, + .none => .none, + .object_index => |object_index| @enumFromInt(@intFromEnum(object_index)), + .source_location_index => @panic("TODO"), + }; + } + + pub fn unpack(sl: SourceLocation, wasm: *const Wasm) Unpacked { + return switch (sl) { + .zig_object_nofile => .zig_object_nofile, + .none => .none, + _ => { + const i = @intFromEnum(sl); + if (i < wasm.objects.items.len) return .{ .object_index = @enumFromInt(i) }; + const sl_index = i - wasm.objects.items.len; + _ = sl_index; + @panic("TODO"); + }, + }; + } - pub fn isPassive(segment: Segment) bool { - return segment.flags & @intFromEnum(Flag.WASM_DATA_SEGMENT_IS_PASSIVE) != 0; + pub fn fromObject(object_index: ObjectIndex, wasm: *const Wasm) SourceLocation { + return pack(.{ .object_index = object_index }, wasm); } - /// For a given segment, determines if it needs passive initialization - fn needsPassiveInitialization(segment: Segment, import_mem: bool, name: []const u8) bool { - if (import_mem and !std.mem.eql(u8, name, ".bss")) { - return true; + pub fn addError(sl: SourceLocation, wasm: *Wasm, comptime f: []const u8, args: anytype) void { + const diags = &wasm.base.comp.link_diags; + switch (sl.unpack(wasm)) { + .none => unreachable, + .zig_object_nofile => diags.addError("zig compilation unit: " ++ f, args), + .object_index => |i| diags.addError("{}: " ++ f, .{i.ptr(wasm).path} ++ args), + .source_location_index => @panic("TODO"), } - return segment.isPassive(); } -}; -pub const SymbolLoc = struct { - /// The index of the symbol within the specified file - index: Symbol.Index, - /// The index of the object file where the symbol resides. - file: OptionalObjectId, -}; + pub fn addNote( + sl: SourceLocation, + err: *link.Diags.ErrorWithNotes, + comptime f: []const u8, + args: anytype, + ) void { + err.addNote(f, args); + const err_msg = &err.diags.msgs.items[err.index]; + err_msg.notes[err.note_slot - 1].source_location = .{ .wasm = sl }; + } -/// From a given location, returns the corresponding symbol in the wasm binary -pub fn symbolLocSymbol(wasm: *const Wasm, loc: SymbolLoc) *Symbol { - if (wasm.discarded.get(loc)) |new_loc| { - return symbolLocSymbol(wasm, new_loc); + pub fn fail(sl: SourceLocation, diags: *link.Diags, comptime format: []const u8, args: anytype) error{LinkFailure} { + return diags.failSourceLocation(.{ .wasm = sl }, format, args); } - return switch (loc.file) { - .none => &wasm.synthetic_symbols.items[@intFromEnum(loc.index)], - .zig_object => wasm.zig_object.?.symbol(loc.index), - _ => &wasm.objects.items[@intFromEnum(loc.file)].symtable[@intFromEnum(loc.index)], + + pub fn string( + sl: SourceLocation, + msg: []const u8, + bundle: *std.zig.ErrorBundle.Wip, + wasm: *const Wasm, + ) Allocator.Error!std.zig.ErrorBundle.String { + return switch (sl.unpack(wasm)) { + .none => try bundle.addString(msg), + .zig_object_nofile => try bundle.printString("zig compilation unit: {s}", .{msg}), + .object_index => |i| { + const obj = i.ptr(wasm); + return if (obj.archive_member_name.slice(wasm)) |obj_name| + try bundle.printString("{} ({s}): {s}", .{ obj.path, std.fs.path.basename(obj_name), msg }) + else + try bundle.printString("{}: {s}", .{ obj.path, msg }); + }, + .source_location_index => @panic("TODO"), + }; + } +}; + +/// The lower bits of this ABI-match the flags here: +/// https://github.com/WebAssembly/tool-conventions/blob/df8d737539eb8a8f446ba5eab9dc670c40dfb81e/Linking.md#symbol-table-subsection +/// The upper bits are used for nefarious purposes. +pub const SymbolFlags = packed struct(u32) { + binding: Binding = .strong, + /// Indicating that this is a hidden symbol. Hidden symbols are not to be + /// exported when performing the final link, but may be linked to other + /// modules. + visibility_hidden: bool = false, + padding0: u1 = 0, + /// For non-data symbols, this must match whether the symbol is an import + /// or is defined; for data symbols, determines whether a segment is + /// specified. + undefined: bool = false, + /// The symbol is intended to be exported from the wasm module to the host + /// environment. This differs from the visibility flags in that it affects + /// static linking. + exported: bool = false, + /// The symbol uses an explicit symbol name, rather than reusing the name + /// from a wasm import. This allows it to remap imports from foreign + /// WebAssembly modules into local symbols with different names. + explicit_name: bool = false, + /// The symbol is intended to be included in the linker output, regardless + /// of whether it is used by the program. Same meaning as `retain`. + no_strip: bool = false, + /// The symbol resides in thread local storage. + tls: bool = false, + /// The symbol represents an absolute address. This means its offset is + /// relative to the start of the wasm memory as opposed to being relative + /// to a data segment. + absolute: bool = false, + + // Above here matches the tooling conventions ABI. + + padding1: u13 = 0, + /// Zig-specific. Dead things are allowed to be garbage collected. + alive: bool = false, + /// Zig-specific. This symbol comes from an object that must be included in + /// the final link. + must_link: bool = false, + /// Zig-specific. + global_type: GlobalType4 = .zero, + /// Zig-specific. + limits_has_max: bool = false, + /// Zig-specific. + limits_is_shared: bool = false, + /// Zig-specific. + ref_type: RefType1 = .funcref, + + pub const Binding = enum(u2) { + strong = 0, + /// Indicating that this is a weak symbol. When linking multiple modules + /// defining the same symbol, all weak definitions are discarded if any + /// strong definitions exist; then if multiple weak definitions exist all + /// but one (unspecified) are discarded; and finally it is an error if more + /// than one definition remains. + weak = 1, + /// Indicating that this is a local symbol. Local symbols are not to be + /// exported, or linked to other modules/sections. The names of all + /// non-local symbols must be unique, but the names of local symbols + /// are not considered for uniqueness. A local function or global + /// symbol cannot reference an import. + local = 2, }; -} -/// From a given location, returns the name of the symbol. -pub fn symbolLocName(wasm: *const Wasm, loc: SymbolLoc) [:0]const u8 { - return wasm.stringSlice(wasm.symbolLocSymbol(loc).name); -} + pub fn initZigSpecific(flags: *SymbolFlags, must_link: bool, no_strip: bool) void { + flags.no_strip = no_strip; + flags.alive = false; + flags.must_link = must_link; + flags.global_type = .zero; + flags.limits_has_max = false; + flags.limits_is_shared = false; + flags.ref_type = .funcref; + } -/// From a given symbol location, returns the final location. -/// e.g. when a symbol was resolved and replaced by the symbol -/// in a different file, this will return said location. -/// If the symbol wasn't replaced by another, this will return -/// the given location itwasm. -pub fn symbolLocFinalLoc(wasm: *const Wasm, loc: SymbolLoc) SymbolLoc { - if (wasm.discarded.get(loc)) |new_loc| { - return symbolLocFinalLoc(wasm, new_loc); + pub fn isIncluded(flags: SymbolFlags, is_dynamic: bool) bool { + return flags.exported or + (is_dynamic and !flags.visibility_hidden) or + (flags.no_strip and flags.must_link); } - return loc; -} -// Contains the location of the function symbol, as well as -/// the priority itself of the initialization function. -pub const InitFuncLoc = struct { - /// object file index in the list of objects. - /// Unlike `SymbolLoc` this cannot be `null` as we never define - /// our own ctors. - file: ObjectId, - /// Symbol index within the corresponding object file. - index: Symbol.Index, - /// The priority in which the constructor must be called. - priority: u32, + pub fn isExported(flags: SymbolFlags, is_dynamic: bool) bool { + if (flags.undefined or flags.binding == .local) return false; + if (is_dynamic and !flags.visibility_hidden) return true; + return flags.exported; + } - /// From a given `InitFuncLoc` returns the corresponding function symbol - fn getSymbol(loc: InitFuncLoc, wasm: *const Wasm) *Symbol { - return wasm.symbolLocSymbol(getSymbolLoc(loc)); + /// Returns the name as how it will be output into the final object + /// file or binary. When `merge` is true, this will return the + /// short name. i.e. ".rodata". When false, it returns the entire name instead. + pub fn outputName(flags: SymbolFlags, name: []const u8, merge: bool) []const u8 { + if (flags.tls) return ".tdata"; + if (!merge) return name; + if (mem.startsWith(u8, name, ".rodata.")) return ".rodata"; + if (mem.startsWith(u8, name, ".text.")) return ".text"; + if (mem.startsWith(u8, name, ".data.")) return ".data"; + if (mem.startsWith(u8, name, ".bss.")) return ".bss"; + return name; + } + + /// Masks off the Zig-specific stuff. + pub fn toAbiInteger(flags: SymbolFlags) u32 { + var copy = flags; + copy.initZigSpecific(false, false); + return @bitCast(copy); } +}; + +pub const GlobalType4 = packed struct(u4) { + valtype: Valtype3, + mutable: bool, + + pub const zero: GlobalType4 = @bitCast(@as(u4, 0)); - /// Turns the given `InitFuncLoc` into a `SymbolLoc` - fn getSymbolLoc(loc: InitFuncLoc) SymbolLoc { + pub fn to(gt: GlobalType4) ObjectGlobal.Type { return .{ - .file = loc.file.toOptional(), - .index = loc.index, + .valtype = gt.valtype.to(), + .mutable = gt.mutable, }; } +}; - /// Returns true when `lhs` has a higher priority (e.i. value closer to 0) than `rhs`. - fn lessThan(ctx: void, lhs: InitFuncLoc, rhs: InitFuncLoc) bool { - _ = ctx; - return lhs.priority < rhs.priority; +pub const Valtype3 = enum(u3) { + i32, + i64, + f32, + f64, + v128, + + pub fn from(v: std.wasm.Valtype) Valtype3 { + return switch (v) { + .i32 => .i32, + .i64 => .i64, + .f32 => .f32, + .f64 => .f64, + .v128 => .v128, + }; + } + + pub fn to(v: Valtype3) std.wasm.Valtype { + return switch (v) { + .i32 => .i32, + .i64 => .i64, + .f32 => .f32, + .f64 => .f64, + .v128 => .v128, + }; } }; -pub fn open( - arena: Allocator, - comp: *Compilation, - emit: Path, - options: link.File.OpenOptions, -) !*Wasm { - // TODO: restore saved linker state, don't truncate the file, and - // participate in incremental compilation. - return createEmpty(arena, comp, emit, options); -} +/// Index into `Wasm.navs_obj`. +pub const NavsObjIndex = enum(u32) { + _, -pub fn createEmpty( - arena: Allocator, - comp: *Compilation, - emit: Path, - options: link.File.OpenOptions, -) !*Wasm { - const gpa = comp.gpa; - const target = comp.root_mod.resolved_target.result; - assert(target.ofmt == .wasm); + pub fn key(i: @This(), wasm: *const Wasm) *InternPool.Nav.Index { + return &wasm.navs_obj.keys()[@intFromEnum(i)]; + } - const use_lld = build_options.have_llvm and comp.config.use_lld; - const use_llvm = comp.config.use_llvm; - const output_mode = comp.config.output_mode; - const shared_memory = comp.config.shared_memory; - const wasi_exec_model = comp.config.wasi_exec_model; + pub fn value(i: @This(), wasm: *const Wasm) *ZcuDataObj { + return &wasm.navs_obj.values()[@intFromEnum(i)]; + } - // If using LLD to link, this code should produce an object file so that it - // can be passed to LLD. - // If using LLVM to generate the object file for the zig compilation unit, - // we need a place to put the object file so that it can be subsequently - // handled. - const zcu_object_sub_path = if (!use_lld and !use_llvm) - null - else - try std.fmt.allocPrint(arena, "{s}.o", .{emit.sub_path}); + pub fn name(i: @This(), wasm: *const Wasm) [:0]const u8 { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const nav = ip.getNav(i.key(wasm).*); + return nav.fqn.toSlice(ip); + } +}; - const wasm = try arena.create(Wasm); - wasm.* = .{ - .base = .{ - .tag = .wasm, - .comp = comp, - .emit = emit, - .zcu_object_sub_path = zcu_object_sub_path, - .gc_sections = options.gc_sections orelse (output_mode != .Obj), - .print_gc_sections = options.print_gc_sections, - .stack_size = options.stack_size orelse switch (target.os.tag) { - .freestanding => 1 * 1024 * 1024, // 1 MiB - else => 16 * 1024 * 1024, // 16 MiB - }, - .allow_shlib_undefined = options.allow_shlib_undefined orelse false, - .file = null, - .disable_lld_caching = options.disable_lld_caching, - .build_id = options.build_id, - }, - .name = undefined, - .string_table = .empty, - .string_bytes = .empty, - .import_table = options.import_table, - .export_table = options.export_table, - .import_symbols = options.import_symbols, - .export_symbol_names = options.export_symbol_names, - .global_base = options.global_base, - .initial_memory = options.initial_memory, - .max_memory = options.max_memory, +/// Index into `Wasm.navs_exe`. +pub const NavsExeIndex = enum(u32) { + _, - .entry_name = undefined, - .zig_object = null, - .dump_argv_list = .empty, - .host_name = undefined, - .custom_sections = undefined, - .preloaded_strings = undefined, - }; - if (use_llvm and comp.config.have_zcu) { - wasm.llvm_object = try LlvmObject.create(arena, comp); + pub fn key(i: @This(), wasm: *const Wasm) *InternPool.Nav.Index { + return &wasm.navs_exe.keys()[@intFromEnum(i)]; } - errdefer wasm.base.destroy(); - wasm.host_name = try wasm.internString("env"); + pub fn value(i: @This(), wasm: *const Wasm) *ZcuDataExe { + return &wasm.navs_exe.values()[@intFromEnum(i)]; + } - inline for (@typeInfo(CustomSections).@"struct".fields) |field| { - @field(wasm.custom_sections, field.name) = .{ - .index = .none, - .name = try wasm.internString(field.name), - }; + pub fn name(i: @This(), wasm: *const Wasm) [:0]const u8 { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const nav = ip.getNav(i.key(wasm).*); + return nav.fqn.toSlice(ip); } +}; - inline for (@typeInfo(PreloadedStrings).@"struct".fields) |field| { - @field(wasm.preloaded_strings, field.name) = try wasm.internString(field.name); +/// Index into `Wasm.uavs_obj`. +pub const UavsObjIndex = enum(u32) { + _, + + pub fn key(i: @This(), wasm: *const Wasm) *InternPool.Index { + return &wasm.uavs_obj.keys()[@intFromEnum(i)]; } - wasm.entry_name = switch (options.entry) { - .disabled => .none, - .default => if (output_mode != .Exe) .none else defaultEntrySymbolName(&wasm.preloaded_strings, wasi_exec_model).toOptional(), - .enabled => defaultEntrySymbolName(&wasm.preloaded_strings, wasi_exec_model).toOptional(), - .named => |name| (try wasm.internString(name)).toOptional(), - }; + pub fn value(i: @This(), wasm: *const Wasm) *ZcuDataObj { + return &wasm.uavs_obj.values()[@intFromEnum(i)]; + } +}; - if (use_lld and (use_llvm or !comp.config.have_zcu)) { - // LLVM emits the object file (if any); LLD links it into the final product. - return wasm; +/// Index into `Wasm.uavs_exe`. +pub const UavsExeIndex = enum(u32) { + _, + + pub fn key(i: @This(), wasm: *const Wasm) *InternPool.Index { + return &wasm.uavs_exe.keys()[@intFromEnum(i)]; } - // What path should this Wasm linker code output to? - // If using LLD to link, this code should produce an object file so that it - // can be passed to LLD. - const sub_path = if (use_lld) zcu_object_sub_path.? else emit.sub_path; + pub fn value(i: @This(), wasm: *const Wasm) *ZcuDataExe { + return &wasm.uavs_exe.values()[@intFromEnum(i)]; + } +}; - wasm.base.file = try emit.root_dir.handle.createFile(sub_path, .{ - .truncate = true, - .read = true, - .mode = if (fs.has_executable_bit) - if (target.os.tag == .wasi and output_mode == .Exe) - fs.File.default_mode | 0b001_000_000 - else - fs.File.default_mode - else - 0, - }); - wasm.name = sub_path; +/// Used when emitting a relocatable object. +pub const ZcuDataObj = extern struct { + code: DataPayload, + relocs: OutReloc.Slice, +}; - // create stack pointer symbol - { - const loc = try wasm.createSyntheticSymbol(wasm.preloaded_strings.__stack_pointer, .global); - const symbol = wasm.symbolLocSymbol(loc); - // For object files we will import the stack pointer symbol - if (output_mode == .Obj) { - symbol.setUndefined(true); - symbol.index = @intCast(wasm.imported_globals_count); - wasm.imported_globals_count += 1; - try wasm.imports.putNoClobber(gpa, loc, .{ - .module_name = wasm.host_name, - .name = symbol.name, - .kind = .{ .global = .{ .valtype = .i32, .mutable = true } }, - }); - } else { - symbol.index = @intCast(wasm.imported_globals_count + wasm.wasm_globals.items.len); - symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); - const global = try wasm.wasm_globals.addOne(gpa); - global.* = .{ - .global_type = .{ - .valtype = .i32, - .mutable = true, - }, - .init = .{ .i32_const = 0 }, - }; - } +/// Used when not emitting a relocatable object. +pub const ZcuDataExe = extern struct { + code: DataPayload, + /// Tracks how many references there are for the purposes of sorting data segments. + count: u32, +}; + +/// An abstraction for calling `lowerZcuData` repeatedly until all data entries +/// are populated. +const ZcuDataStarts = struct { + uavs_i: u32, + + fn init(wasm: *const Wasm) ZcuDataStarts { + const comp = wasm.base.comp; + const is_obj = comp.config.output_mode == .Obj; + return if (is_obj) initObj(wasm) else initExe(wasm); } - // create indirect function pointer symbol - { - const loc = try wasm.createSyntheticSymbol(wasm.preloaded_strings.__indirect_function_table, .table); - const symbol = wasm.symbolLocSymbol(loc); - const table: std.wasm.Table = .{ - .limits = .{ .flags = 0, .min = 0, .max = undefined }, // will be overwritten during `mapFunctionTable` - .reftype = .funcref, + fn initObj(wasm: *const Wasm) ZcuDataStarts { + return .{ + .uavs_i = @intCast(wasm.uavs_obj.entries.len), }; - if (output_mode == .Obj or options.import_table) { - symbol.setUndefined(true); - symbol.index = @intCast(wasm.imported_tables_count); - wasm.imported_tables_count += 1; - try wasm.imports.put(gpa, loc, .{ - .module_name = wasm.host_name, - .name = symbol.name, - .kind = .{ .table = table }, - }); - } else { - symbol.index = @as(u32, @intCast(wasm.imported_tables_count + wasm.tables.items.len)); - try wasm.tables.append(gpa, table); - if (wasm.export_table) { - symbol.setFlag(.WASM_SYM_EXPORTED); - } else { - symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); - } + } + + fn initExe(wasm: *const Wasm) ZcuDataStarts { + return .{ + .uavs_i = @intCast(wasm.uavs_exe.entries.len), + }; + } + + fn finish(zds: ZcuDataStarts, wasm: *Wasm, pt: Zcu.PerThread) !void { + const comp = wasm.base.comp; + const is_obj = comp.config.output_mode == .Obj; + return if (is_obj) finishObj(zds, wasm, pt) else finishExe(zds, wasm, pt); + } + + fn finishObj(zds: ZcuDataStarts, wasm: *Wasm, pt: Zcu.PerThread) !void { + var uavs_i = zds.uavs_i; + while (uavs_i < wasm.uavs_obj.entries.len) : (uavs_i += 1) { + // Call to `lowerZcuData` here possibly creates more entries in these tables. + wasm.uavs_obj.values()[uavs_i] = try lowerZcuData(wasm, pt, wasm.uavs_obj.keys()[uavs_i]); } } - // create __wasm_call_ctors - { - const loc = try wasm.createSyntheticSymbol(wasm.preloaded_strings.__wasm_call_ctors, .function); - const symbol = wasm.symbolLocSymbol(loc); - symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); - // we do not know the function index until after we merged all sections. - // Therefore we set `symbol.index` and create its corresponding references - // at the end during `initializeCallCtorsFunction`. + fn finishExe(zds: ZcuDataStarts, wasm: *Wasm, pt: Zcu.PerThread) !void { + var uavs_i = zds.uavs_i; + while (uavs_i < wasm.uavs_exe.entries.len) : (uavs_i += 1) { + // Call to `lowerZcuData` here possibly creates more entries in these tables. + const zcu_data = try lowerZcuData(wasm, pt, wasm.uavs_exe.keys()[uavs_i]); + wasm.uavs_exe.values()[uavs_i].code = zcu_data.code; + } } +}; - // shared-memory symbols for TLS support - if (shared_memory) { - { - const loc = try wasm.createSyntheticSymbol(wasm.preloaded_strings.__tls_base, .global); - const symbol = wasm.symbolLocSymbol(loc); - symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); - symbol.index = @intCast(wasm.imported_globals_count + wasm.wasm_globals.items.len); - symbol.mark(); - try wasm.wasm_globals.append(gpa, .{ - .global_type = .{ .valtype = .i32, .mutable = true }, - .init = .{ .i32_const = undefined }, - }); +pub const ZcuFunc = union { + function: CodeGen.Function, + tag_name: TagName, + + pub const TagName = extern struct { + symbol_name: String, + type_index: FunctionType.Index, + /// Index into `Wasm.tag_name_offs`. + table_index: u32, + }; + + /// Index into `Wasm.zcu_funcs`. + /// Note that swapRemove is sometimes performed on `zcu_funcs`. + pub const Index = enum(u32) { + _, + + pub fn key(i: @This(), wasm: *const Wasm) *InternPool.Index { + return &wasm.zcu_funcs.keys()[@intFromEnum(i)]; } - { - const loc = try wasm.createSyntheticSymbol(wasm.preloaded_strings.__tls_size, .global); - const symbol = wasm.symbolLocSymbol(loc); - symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); - symbol.index = @intCast(wasm.imported_globals_count + wasm.wasm_globals.items.len); - symbol.mark(); - try wasm.wasm_globals.append(gpa, .{ - .global_type = .{ .valtype = .i32, .mutable = false }, - .init = .{ .i32_const = undefined }, - }); + + pub fn value(i: @This(), wasm: *const Wasm) *ZcuFunc { + return &wasm.zcu_funcs.values()[@intFromEnum(i)]; } - { - const loc = try wasm.createSyntheticSymbol(wasm.preloaded_strings.__tls_align, .global); - const symbol = wasm.symbolLocSymbol(loc); - symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); - symbol.index = @intCast(wasm.imported_globals_count + wasm.wasm_globals.items.len); - symbol.mark(); - try wasm.wasm_globals.append(gpa, .{ - .global_type = .{ .valtype = .i32, .mutable = false }, - .init = .{ .i32_const = undefined }, - }); + + pub fn name(i: @This(), wasm: *const Wasm) [:0]const u8 { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const ip_index = i.key(wasm).*; + switch (ip.indexToKey(ip_index)) { + .func => |func| { + const nav = ip.getNav(func.owner_nav); + return nav.fqn.toSlice(ip); + }, + .enum_type => { + return i.value(wasm).tag_name.symbol_name.slice(wasm); + }, + else => unreachable, + } } - { - const loc = try wasm.createSyntheticSymbol(wasm.preloaded_strings.__wasm_init_tls, .function); - const symbol = wasm.symbolLocSymbol(loc); - symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); + + pub fn typeIndex(i: @This(), wasm: *Wasm) FunctionType.Index { + const comp = wasm.base.comp; + const zcu = comp.zcu.?; + const target = &comp.root_mod.resolved_target.result; + const ip = &zcu.intern_pool; + switch (ip.indexToKey(i.key(wasm).*)) { + .func => |func| { + const fn_ty = zcu.navValue(func.owner_nav).typeOf(zcu); + const fn_info = zcu.typeToFunc(fn_ty).?; + return wasm.getExistingFunctionType(fn_info.cc, fn_info.param_types.get(ip), .fromInterned(fn_info.return_type), target).?; + }, + .enum_type => { + return i.value(wasm).tag_name.type_index; + }, + else => unreachable, + } } - } + }; +}; + +pub const NavExport = extern struct { + name: String, + nav_index: InternPool.Nav.Index, +}; + +pub const UavExport = extern struct { + name: String, + uav_index: InternPool.Index, +}; + +pub const FunctionImport = extern struct { + flags: SymbolFlags, + module_name: OptionalString, + /// May be different than the key which is a symbol name. + name: String, + source_location: SourceLocation, + resolution: Resolution, + type: FunctionType.Index, + + /// Represents a synthetic function, a function from an object, or a + /// function from the Zcu. + pub const Resolution = enum(u32) { + unresolved, + __wasm_apply_global_tls_relocs, + __wasm_call_ctors, + __wasm_init_memory, + __wasm_init_tls, + // Next, index into `object_functions`. + // Next, index into `zcu_funcs`. + _, + + const first_object_function = @intFromEnum(Resolution.__wasm_init_tls) + 1; + + pub const Unpacked = union(enum) { + unresolved, + __wasm_apply_global_tls_relocs, + __wasm_call_ctors, + __wasm_init_memory, + __wasm_init_tls, + object_function: ObjectFunctionIndex, + zcu_func: ZcuFunc.Index, + }; - if (comp.zcu) |zcu| { - if (!use_llvm) { - const zig_object = try arena.create(ZigObject); - wasm.zig_object = zig_object; - zig_object.* = .{ - .path = .{ - .root_dir = std.Build.Cache.Directory.cwd(), - .sub_path = try std.fmt.allocPrint(gpa, "{s}.o", .{fs.path.stem(zcu.main_mod.root_src_path)}), + pub fn unpack(r: Resolution, wasm: *const Wasm) Unpacked { + return switch (r) { + .unresolved => .unresolved, + .__wasm_apply_global_tls_relocs => .__wasm_apply_global_tls_relocs, + .__wasm_call_ctors => .__wasm_call_ctors, + .__wasm_init_memory => .__wasm_init_memory, + .__wasm_init_tls => .__wasm_init_tls, + _ => { + const object_function_index = @intFromEnum(r) - first_object_function; + + const zcu_func_index = if (object_function_index < wasm.object_functions.items.len) + return .{ .object_function = @enumFromInt(object_function_index) } + else + object_function_index - wasm.object_functions.items.len; + + return .{ .zcu_func = @enumFromInt(zcu_func_index) }; }, - .stack_pointer_sym = .null, }; - try zig_object.init(wasm); } - } - return wasm; -} + pub fn pack(wasm: *const Wasm, unpacked: Unpacked) Resolution { + return switch (unpacked) { + .unresolved => .unresolved, + .__wasm_apply_global_tls_relocs => .__wasm_apply_global_tls_relocs, + .__wasm_call_ctors => .__wasm_call_ctors, + .__wasm_init_memory => .__wasm_init_memory, + .__wasm_init_tls => .__wasm_init_tls, + .object_function => |i| @enumFromInt(first_object_function + @intFromEnum(i)), + .zcu_func => |i| @enumFromInt(first_object_function + wasm.object_functions.items.len + @intFromEnum(i)), + }; + } -pub fn getTypeIndex(wasm: *const Wasm, func_type: std.wasm.Type) ?u32 { - var index: u32 = 0; - while (index < wasm.func_types.items.len) : (index += 1) { - if (wasm.func_types.items[index].eql(func_type)) return index; - } - return null; -} + pub fn fromIpNav(wasm: *const Wasm, nav_index: InternPool.Nav.Index) Resolution { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + return fromIpIndex(wasm, ip.getNav(nav_index).status.fully_resolved.val); + } -/// Either creates a new import, or updates one if existing. -/// When `type_index` is non-null, we assume an external function. -/// In all other cases, a data-symbol will be created instead. -pub fn addOrUpdateImport( - wasm: *Wasm, - /// Name of the import - name: []const u8, - /// Symbol index that is external - symbol_index: Symbol.Index, - /// Optional library name (i.e. `extern "c" fn foo() void` - lib_name: ?[:0]const u8, - /// The index of the type that represents the function signature - /// when the extern is a function. When this is null, a data-symbol - /// is asserted instead. - type_index: ?u32, -) !void { - return wasm.zig_object.?.addOrUpdateImport(wasm, name, symbol_index, lib_name, type_index); -} + pub fn fromZcuFunc(wasm: *const Wasm, i: ZcuFunc.Index) Resolution { + return pack(wasm, .{ .zcu_func = i }); + } -/// For a given name, creates a new global synthetic symbol. -/// Leaves index undefined and the default flags (0). -fn createSyntheticSymbol(wasm: *Wasm, name: String, tag: Symbol.Tag) !SymbolLoc { - return wasm.createSyntheticSymbolOffset(name, tag); -} + pub fn fromIpIndex(wasm: *const Wasm, ip_index: InternPool.Index) Resolution { + return fromZcuFunc(wasm, @enumFromInt(wasm.zcu_funcs.getIndex(ip_index).?)); + } -fn createSyntheticSymbolOffset(wasm: *Wasm, name_offset: String, tag: Symbol.Tag) !SymbolLoc { - const sym_index: Symbol.Index = @enumFromInt(wasm.synthetic_symbols.items.len); - const loc: SymbolLoc = .{ .index = sym_index, .file = .none }; - const gpa = wasm.base.comp.gpa; - try wasm.synthetic_symbols.append(gpa, .{ - .name = name_offset, - .flags = 0, - .tag = tag, - .index = undefined, - .virtual_address = undefined, - }); - try wasm.resolved_symbols.putNoClobber(gpa, loc, {}); - try wasm.globals.put(gpa, name_offset, loc); - return loc; -} + pub fn fromObjectFunction(wasm: *const Wasm, object_function: ObjectFunctionIndex) Resolution { + return pack(wasm, .{ .object_function = object_function }); + } -fn openParseObjectReportingFailure(wasm: *Wasm, path: Path) void { - const diags = &wasm.base.comp.link_diags; - const obj = link.openObject(path, false, false) catch |err| { - switch (diags.failParse(path, "failed to open object: {s}", .{@errorName(err)})) { - error.LinkFailure => return, + pub fn isNavOrUnresolved(r: Resolution, wasm: *const Wasm) bool { + return switch (r.unpack(wasm)) { + .unresolved, .zcu_func => true, + else => false, + }; } - }; - wasm.parseObject(obj) catch |err| { - switch (diags.failParse(path, "failed to parse object: {s}", .{@errorName(err)})) { - error.LinkFailure => return, + + pub fn typeIndex(r: Resolution, wasm: *Wasm) FunctionType.Index { + return switch (unpack(r, wasm)) { + .unresolved => unreachable, + .__wasm_apply_global_tls_relocs, + .__wasm_call_ctors, + .__wasm_init_memory, + => getExistingFuncType2(wasm, &.{}, &.{}), + .__wasm_init_tls => getExistingFuncType2(wasm, &.{.i32}, &.{}), + .object_function => |i| i.ptr(wasm).type_index, + .zcu_func => |i| i.typeIndex(wasm), + }; + } + + pub fn name(r: Resolution, wasm: *const Wasm) ?[]const u8 { + return switch (unpack(r, wasm)) { + .unresolved => unreachable, + .__wasm_apply_global_tls_relocs => @tagName(Unpacked.__wasm_apply_global_tls_relocs), + .__wasm_call_ctors => @tagName(Unpacked.__wasm_call_ctors), + .__wasm_init_memory => @tagName(Unpacked.__wasm_init_memory), + .__wasm_init_tls => @tagName(Unpacked.__wasm_init_tls), + .object_function => |i| i.ptr(wasm).name.slice(wasm), + .zcu_func => |i| i.name(wasm), + }; } }; -} -fn parseObject(wasm: *Wasm, obj: link.Input.Object) !void { - defer obj.file.close(); - const gpa = wasm.base.comp.gpa; - try wasm.objects.ensureUnusedCapacity(gpa, 1); - const stat = try obj.file.stat(); - const size = std.math.cast(usize, stat.size) orelse return error.FileTooBig; + /// Index into `object_function_imports`. + pub const Index = enum(u32) { + _, - const file_contents = try gpa.alloc(u8, size); - defer gpa.free(file_contents); + pub fn key(index: Index, wasm: *const Wasm) *String { + return &wasm.object_function_imports.keys()[@intFromEnum(index)]; + } - const n = try obj.file.preadAll(file_contents, 0); - if (n != file_contents.len) return error.UnexpectedEndOfFile; + pub fn value(index: Index, wasm: *const Wasm) *FunctionImport { + return &wasm.object_function_imports.values()[@intFromEnum(index)]; + } - wasm.objects.appendAssumeCapacity(try Object.create(wasm, file_contents, obj.path, null)); -} + pub fn symbolName(index: Index, wasm: *const Wasm) String { + return index.key(wasm).*; + } -/// Creates a new empty `Atom` and returns its `Atom.Index` -pub fn createAtom(wasm: *Wasm, sym_index: Symbol.Index, object_index: OptionalObjectId) !Atom.Index { - const gpa = wasm.base.comp.gpa; - const index: Atom.Index = @enumFromInt(wasm.managed_atoms.items.len); - const atom = try wasm.managed_atoms.addOne(gpa); - atom.* = .{ - .file = object_index, - .sym_index = sym_index, - }; - try wasm.symbol_atom.putNoClobber(gpa, atom.symbolLoc(), index); + pub fn importName(index: Index, wasm: *const Wasm) String { + return index.value(wasm).name; + } - return index; -} + pub fn moduleName(index: Index, wasm: *const Wasm) OptionalString { + return index.value(wasm).module_name; + } -pub fn getAtom(wasm: *const Wasm, index: Atom.Index) Atom { - return wasm.managed_atoms.items[@intFromEnum(index)]; -} + pub fn functionType(index: Index, wasm: *const Wasm) FunctionType.Index { + return value(index, wasm).type; + } + }; +}; -pub fn getAtomPtr(wasm: *Wasm, index: Atom.Index) *Atom { - return &wasm.managed_atoms.items[@intFromEnum(index)]; -} +pub const ObjectFunction = extern struct { + flags: SymbolFlags, + /// `none` if this function has no symbol describing it. + name: OptionalString, + type_index: FunctionType.Index, + code: Code, + /// The offset within the code section where the data starts. + offset: u32, + /// The object file whose code section contains this function. + object_index: ObjectIndex, -fn parseArchive(wasm: *Wasm, obj: link.Input.Object) !void { - const gpa = wasm.base.comp.gpa; + pub const Code = DataPayload; - defer obj.file.close(); + pub fn relocations(of: *const ObjectFunction, wasm: *const Wasm) ObjectRelocation.IterableSlice { + const code_section_index = of.object_index.ptr(wasm).code_section_index.?; + const relocs = wasm.object_relocations_table.get(code_section_index) orelse return .empty; + return .init(relocs, of.offset, of.code.len, wasm); + } +}; - const stat = try obj.file.stat(); - const size = std.math.cast(usize, stat.size) orelse return error.FileTooBig; +pub const GlobalImport = extern struct { + flags: SymbolFlags, + module_name: OptionalString, + /// May be different than the key which is a symbol name. + name: String, + source_location: SourceLocation, + resolution: Resolution, + + /// Represents a synthetic global, a global from an object, or a global + /// from the Zcu. + pub const Resolution = enum(u32) { + unresolved, + __heap_base, + __heap_end, + __stack_pointer, + __tls_align, + __tls_base, + __tls_size, + // Next, index into `object_globals`. + // Next, index into `navs_obj` or `navs_exe` depending on whether emitting an object. + _, - const file_contents = try gpa.alloc(u8, size); - var keep_file_contents = false; - defer if (!keep_file_contents) gpa.free(file_contents); + const first_object_global = @intFromEnum(Resolution.__tls_size) + 1; + + pub const Unpacked = union(enum) { + unresolved, + __heap_base, + __heap_end, + __stack_pointer, + __tls_align, + __tls_base, + __tls_size, + object_global: ObjectGlobalIndex, + nav_exe: NavsExeIndex, + nav_obj: NavsObjIndex, + }; - const n = try obj.file.preadAll(file_contents, 0); - if (n != file_contents.len) return error.UnexpectedEndOfFile; + pub fn unpack(r: Resolution, wasm: *const Wasm) Unpacked { + return switch (r) { + .unresolved => .unresolved, + .__heap_base => .__heap_base, + .__heap_end => .__heap_end, + .__stack_pointer => .__stack_pointer, + .__tls_align => .__tls_align, + .__tls_base => .__tls_base, + .__tls_size => .__tls_size, + _ => { + const i: u32 = @intFromEnum(r); + const object_global_index = i - first_object_global; + if (object_global_index < wasm.object_globals.items.len) + return .{ .object_global = @enumFromInt(object_global_index) }; + const comp = wasm.base.comp; + const is_obj = comp.config.output_mode == .Obj; + const nav_index = object_global_index - wasm.object_globals.items.len; + return if (is_obj) .{ + .nav_obj = @enumFromInt(nav_index), + } else .{ + .nav_exe = @enumFromInt(nav_index), + }; + }, + }; + } - var archive = try Archive.parse(gpa, file_contents); + pub fn pack(wasm: *const Wasm, unpacked: Unpacked) Resolution { + return switch (unpacked) { + .unresolved => .unresolved, + .__heap_base => .__heap_base, + .__heap_end => .__heap_end, + .__stack_pointer => .__stack_pointer, + .__tls_align => .__tls_align, + .__tls_base => .__tls_base, + .__tls_size => .__tls_size, + .object_global => |i| @enumFromInt(first_object_global + @intFromEnum(i)), + .nav_obj => |i| @enumFromInt(first_object_global + wasm.object_globals.items.len + @intFromEnum(i)), + .nav_exe => |i| @enumFromInt(first_object_global + wasm.object_globals.items.len + @intFromEnum(i)), + }; + } - if (!obj.must_link) { - errdefer archive.deinit(gpa); - try wasm.lazy_archives.append(gpa, .{ - .path = .{ - .root_dir = obj.path.root_dir, - .sub_path = try gpa.dupe(u8, obj.path.sub_path), - }, - .file_contents = file_contents, - .archive = archive, - }); - keep_file_contents = true; - return; - } + pub fn fromIpNav(wasm: *const Wasm, ip_nav: InternPool.Nav.Index) Resolution { + const comp = wasm.base.comp; + const is_obj = comp.config.output_mode == .Obj; + return pack(wasm, if (is_obj) .{ + .nav_obj = @enumFromInt(wasm.navs_obj.getIndex(ip_nav).?), + } else .{ + .nav_exe = @enumFromInt(wasm.navs_exe.getIndex(ip_nav).?), + }); + } - defer archive.deinit(gpa); + pub fn fromObjectGlobal(wasm: *const Wasm, object_global: ObjectGlobalIndex) Resolution { + return pack(wasm, .{ .object_global = object_global }); + } - // In this case we must force link all embedded object files within the archive - // We loop over all symbols, and then group them by offset as the offset - // notates where the object file starts. - var offsets = std.AutoArrayHashMap(u32, void).init(gpa); - defer offsets.deinit(); - for (archive.toc.values()) |symbol_offsets| { - for (symbol_offsets.items) |sym_offset| { - try offsets.put(sym_offset, {}); + pub fn name(r: Resolution, wasm: *const Wasm) ?[]const u8 { + return switch (unpack(r, wasm)) { + .unresolved => unreachable, + .__heap_base => @tagName(Unpacked.__heap_base), + .__heap_end => @tagName(Unpacked.__heap_end), + .__stack_pointer => @tagName(Unpacked.__stack_pointer), + .__tls_align => @tagName(Unpacked.__tls_align), + .__tls_base => @tagName(Unpacked.__tls_base), + .__tls_size => @tagName(Unpacked.__tls_size), + .object_global => |i| i.name(wasm).slice(wasm), + .nav_obj => |i| i.name(wasm), + .nav_exe => |i| i.name(wasm), + }; } - } + }; - for (offsets.keys()) |file_offset| { - const object = try archive.parseObject(wasm, file_contents[file_offset..], obj.path); - try wasm.objects.append(gpa, object); - } -} + /// Index into `Wasm.object_global_imports`. + pub const Index = enum(u32) { + _, -fn requiresTLSReloc(wasm: *const Wasm) bool { - for (wasm.got_symbols.items) |loc| { - if (wasm.symbolLocSymbol(loc).isTLS()) { - return true; + pub fn key(index: Index, wasm: *const Wasm) *String { + return &wasm.object_global_imports.keys()[@intFromEnum(index)]; } - } - return false; -} -fn objectPath(wasm: *const Wasm, object_id: ObjectId) Path { - const obj = wasm.objectById(object_id) orelse return wasm.zig_object.?.path; - return obj.path; -} + pub fn value(index: Index, wasm: *const Wasm) *GlobalImport { + return &wasm.object_global_imports.values()[@intFromEnum(index)]; + } -fn objectSymbols(wasm: *const Wasm, object_id: ObjectId) []const Symbol { - const obj = wasm.objectById(object_id) orelse return wasm.zig_object.?.symbols.items; - return obj.symtable; -} + pub fn symbolName(index: Index, wasm: *const Wasm) String { + return index.key(wasm).*; + } -fn objectSymbol(wasm: *const Wasm, object_id: ObjectId, index: Symbol.Index) *Symbol { - const obj = wasm.objectById(object_id) orelse return &wasm.zig_object.?.symbols.items[@intFromEnum(index)]; - return &obj.symtable[@intFromEnum(index)]; -} + pub fn importName(index: Index, wasm: *const Wasm) String { + return index.value(wasm).name; + } -fn objectFunction(wasm: *const Wasm, object_id: ObjectId, sym_index: Symbol.Index) std.wasm.Func { - const obj = wasm.objectById(object_id) orelse { - const zo = wasm.zig_object.?; - const sym = zo.symbols.items[@intFromEnum(sym_index)]; - return zo.functions.items[sym.index]; + pub fn moduleName(index: Index, wasm: *const Wasm) OptionalString { + return index.value(wasm).module_name; + } + + pub fn globalType(index: Index, wasm: *const Wasm) ObjectGlobal.Type { + return value(index, wasm).type(); + } }; - const sym = obj.symtable[@intFromEnum(sym_index)]; - return obj.functions[sym.index - obj.imported_functions_count]; -} -fn objectImportedFunctions(wasm: *const Wasm, object_id: ObjectId) u32 { - const obj = wasm.objectById(object_id) orelse return wasm.zig_object.?.imported_functions_count; - return obj.imported_functions_count; -} + pub fn @"type"(gi: *const GlobalImport) ObjectGlobal.Type { + return gi.flags.global_type.to(); + } +}; -fn objectGlobals(wasm: *const Wasm, object_id: ObjectId) []const std.wasm.Global { - const obj = wasm.objectById(object_id) orelse return wasm.zig_object.?.globals.items; - return obj.globals; -} +pub const ObjectGlobal = extern struct { + /// `none` if this function has no symbol describing it. + name: OptionalString, + flags: SymbolFlags, + expr: Expr, + /// The object file whose global section contains this global. + object_index: ObjectIndex, + offset: u32, + size: u32, -fn objectFuncTypes(wasm: *const Wasm, object_id: ObjectId) []const std.wasm.Type { - const obj = wasm.objectById(object_id) orelse return wasm.zig_object.?.func_types.items; - return obj.func_types; -} + pub fn @"type"(og: *const ObjectGlobal) Type { + return og.flags.global_type.to(); + } -fn objectSegmentInfo(wasm: *const Wasm, object_id: ObjectId) []const NamedSegment { - const obj = wasm.objectById(object_id) orelse return wasm.zig_object.?.segment_info.items; - return obj.segment_info; -} + pub const Type = struct { + valtype: std.wasm.Valtype, + mutable: bool, + }; -/// For a given symbol index, find its corresponding import. -/// Asserts import exists. -fn objectImport(wasm: *const Wasm, object_id: ObjectId, symbol_index: Symbol.Index) Import { - const obj = wasm.objectById(object_id) orelse return wasm.zig_object.?.imports.get(symbol_index).?; - return obj.findImport(obj.symtable[@intFromEnum(symbol_index)]); -} + pub fn relocations(og: *const ObjectGlobal, wasm: *const Wasm) ObjectRelocation.IterableSlice { + const global_section_index = og.object_index.ptr(wasm).global_section_index.?; + const relocs = wasm.object_relocations_table.get(global_section_index) orelse return .empty; + return .init(relocs, og.offset, og.size, wasm); + } +}; -/// Returns the object element pointer, or null if it is the ZigObject. -fn objectById(wasm: *const Wasm, object_id: ObjectId) ?*Object { - if (object_id == .zig_object) return null; - return &wasm.objects.items[@intFromEnum(object_id)]; -} +pub const RefType1 = enum(u1) { + funcref, + externref, -fn resolveSymbolsInObject(wasm: *Wasm, object_id: ObjectId) !void { - const gpa = wasm.base.comp.gpa; - const diags = &wasm.base.comp.link_diags; - const obj_path = objectPath(wasm, object_id); - log.debug("Resolving symbols in object: '{'}'", .{obj_path}); - const symbols = objectSymbols(wasm, object_id); - - for (symbols, 0..) |symbol, i| { - const sym_index: Symbol.Index = @enumFromInt(i); - const location: SymbolLoc = .{ - .file = object_id.toOptional(), - .index = sym_index, + pub fn from(rt: std.wasm.RefType) RefType1 { + return switch (rt) { + .funcref => .funcref, + .externref => .externref, }; - if (symbol.name == wasm.preloaded_strings.__indirect_function_table) continue; + } - if (symbol.isLocal()) { - if (symbol.isUndefined()) { - diags.addParseError(obj_path, "local symbol '{s}' references import", .{ - wasm.stringSlice(symbol.name), - }); - } - try wasm.resolved_symbols.putNoClobber(gpa, location, {}); - continue; - } + pub fn to(rt: RefType1) std.wasm.RefType { + return switch (rt) { + .funcref => .funcref, + .externref => .externref, + }; + } +}; - const maybe_existing = try wasm.globals.getOrPut(gpa, symbol.name); - if (!maybe_existing.found_existing) { - maybe_existing.value_ptr.* = location; - try wasm.resolved_symbols.putNoClobber(gpa, location, {}); +pub const TableImport = extern struct { + flags: SymbolFlags, + module_name: String, + /// May be different than the key which is a symbol name. + name: String, + source_location: SourceLocation, + resolution: Resolution, + limits_min: u32, + limits_max: u32, + + /// Represents a synthetic table, or a table from an object. + pub const Resolution = enum(u32) { + unresolved, + __indirect_function_table, + // Next, index into `object_tables`. + _, - if (symbol.isUndefined()) { - try wasm.undefs.putNoClobber(gpa, symbol.name, location); - } - continue; - } + const first_object_table = @intFromEnum(Resolution.__indirect_function_table) + 1; - const existing_loc = maybe_existing.value_ptr.*; - const existing_sym: *Symbol = wasm.symbolLocSymbol(existing_loc); - const existing_file_path: Path = if (existing_loc.file.unwrap()) |id| objectPath(wasm, id) else .{ - .root_dir = std.Build.Cache.Directory.cwd(), - .sub_path = wasm.name, + pub const Unpacked = union(enum) { + unresolved, + __indirect_function_table, + object_table: ObjectTableIndex, }; - if (!existing_sym.isUndefined()) outer: { - if (!symbol.isUndefined()) inner: { - if (symbol.isWeak()) { - break :inner; // ignore the new symbol (discard it) - } - if (existing_sym.isWeak()) { - break :outer; // existing is weak, while new one isn't. Replace it. - } - // both are defined and weak, we have a symbol collision. - var err = try diags.addErrorWithNotes(2); - try err.addMsg("symbol '{s}' defined multiple times", .{wasm.stringSlice(symbol.name)}); - try err.addNote("first definition in '{'}'", .{existing_file_path}); - try err.addNote("next definition in '{'}'", .{obj_path}); - } + pub fn unpack(r: Resolution) Unpacked { + return switch (r) { + .unresolved => .unresolved, + .__indirect_function_table => .__indirect_function_table, + _ => .{ .object_table = @enumFromInt(@intFromEnum(r) - first_object_table) }, + }; + } - try wasm.discarded.put(gpa, location, existing_loc); - continue; // Do not overwrite defined symbols with undefined symbols + fn pack(unpacked: Unpacked) Resolution { + return switch (unpacked) { + .unresolved => .unresolved, + .__indirect_function_table => .__indirect_function_table, + .object_table => |i| @enumFromInt(first_object_table + @intFromEnum(i)), + }; } - if (symbol.tag != existing_sym.tag) { - var err = try diags.addErrorWithNotes(2); - try err.addMsg("symbol '{s}' mismatching types '{s}' and '{s}'", .{ - wasm.stringSlice(symbol.name), @tagName(symbol.tag), @tagName(existing_sym.tag), - }); - try err.addNote("first definition in '{'}'", .{existing_file_path}); - try err.addNote("next definition in '{'}'", .{obj_path}); + fn fromObjectTable(object_table: ObjectTableIndex) Resolution { + return pack(.{ .object_table = object_table }); } - if (existing_sym.isUndefined() and symbol.isUndefined()) { - // only verify module/import name for function symbols - if (symbol.tag == .function) { - const existing_name = if (existing_loc.file.unwrap()) |existing_obj_id| - objectImport(wasm, existing_obj_id, existing_loc.index).module_name - else - wasm.imports.get(existing_loc).?.module_name; - - const module_name = objectImport(wasm, object_id, sym_index).module_name; - if (existing_name != module_name) { - var err = try diags.addErrorWithNotes(2); - try err.addMsg("symbol '{s}' module name mismatch. Expected '{s}', but found '{s}'", .{ - wasm.stringSlice(symbol.name), - wasm.stringSlice(existing_name), - wasm.stringSlice(module_name), - }); - try err.addNote("first definition in '{'}'", .{existing_file_path}); - try err.addNote("next definition in '{'}'", .{obj_path}); - } - } + pub fn refType(r: Resolution, wasm: *const Wasm) std.wasm.RefType { + return switch (unpack(r)) { + .unresolved => unreachable, + .__indirect_function_table => .funcref, + .object_table => |i| i.ptr(wasm).flags.ref_type.to(), + }; + } - // both undefined so skip overwriting existing symbol and discard the new symbol - try wasm.discarded.put(gpa, location, existing_loc); - continue; + pub fn limits(r: Resolution, wasm: *const Wasm) std.wasm.Limits { + return switch (unpack(r)) { + .unresolved => unreachable, + .__indirect_function_table => .{ + .flags = .{ .has_max = true, .is_shared = false }, + .min = @intCast(wasm.flush_buffer.indirect_function_table.entries.len + 1), + .max = @intCast(wasm.flush_buffer.indirect_function_table.entries.len + 1), + }, + .object_table => |i| i.ptr(wasm).limits(), + }; } + }; - if (existing_sym.tag == .global) { - const existing_ty = wasm.getGlobalType(existing_loc); - const new_ty = wasm.getGlobalType(location); - if (existing_ty.mutable != new_ty.mutable or existing_ty.valtype != new_ty.valtype) { - var err = try diags.addErrorWithNotes(2); - try err.addMsg("symbol '{s}' mismatching global types", .{wasm.stringSlice(symbol.name)}); - try err.addNote("first definition in '{'}'", .{existing_file_path}); - try err.addNote("next definition in '{'}'", .{obj_path}); - } + /// Index into `object_table_imports`. + pub const Index = enum(u32) { + _, + + pub fn key(index: Index, wasm: *const Wasm) *String { + return &wasm.object_table_imports.keys()[@intFromEnum(index)]; } - if (existing_sym.tag == .function) { - const existing_ty = wasm.getFunctionSignature(existing_loc); - const new_ty = wasm.getFunctionSignature(location); - if (!existing_ty.eql(new_ty)) { - var err = try diags.addErrorWithNotes(3); - try err.addMsg("symbol '{s}' mismatching function signatures.", .{wasm.stringSlice(symbol.name)}); - try err.addNote("expected signature {}, but found signature {}", .{ existing_ty, new_ty }); - try err.addNote("first definition in '{'}'", .{existing_file_path}); - try err.addNote("next definition in '{'}'", .{obj_path}); - } + pub fn value(index: Index, wasm: *const Wasm) *TableImport { + return &wasm.object_table_imports.values()[@intFromEnum(index)]; } - // when both symbols are weak, we skip overwriting unless the existing - // symbol is weak and the new one isn't, in which case we *do* overwrite it. - if (existing_sym.isWeak() and symbol.isWeak()) blk: { - if (existing_sym.isUndefined() and !symbol.isUndefined()) break :blk; - try wasm.discarded.put(gpa, location, existing_loc); - continue; + pub fn name(index: Index, wasm: *const Wasm) String { + return index.key(wasm).*; } - // simply overwrite with the new symbol - log.debug("Overwriting symbol '{s}'", .{wasm.stringSlice(symbol.name)}); - log.debug(" old definition in '{'}'", .{existing_file_path}); - log.debug(" new definition in '{'}'", .{obj_path}); - try wasm.discarded.putNoClobber(gpa, existing_loc, location); - maybe_existing.value_ptr.* = location; - try wasm.globals.put(gpa, symbol.name, location); - try wasm.resolved_symbols.put(gpa, location, {}); - assert(wasm.resolved_symbols.swapRemove(existing_loc)); - if (existing_sym.isUndefined()) { - _ = wasm.undefs.swapRemove(symbol.name); + pub fn moduleName(index: Index, wasm: *const Wasm) OptionalString { + return index.value(wasm).module_name; } + }; + + pub fn limits(ti: *const TableImport) std.wasm.Limits { + return .{ + .flags = .{ + .has_max = ti.flags.limits_has_max, + .is_shared = ti.flags.limits_is_shared, + }, + .min = ti.limits_min, + .max = ti.limits_max, + }; } -} +}; -fn resolveSymbolsInArchives(wasm: *Wasm) !void { - if (wasm.lazy_archives.items.len == 0) return; - const gpa = wasm.base.comp.gpa; - const diags = &wasm.base.comp.link_diags; +pub const Table = extern struct { + module_name: OptionalString, + name: OptionalString, + flags: SymbolFlags, + limits_min: u32, + limits_max: u32, - log.debug("Resolving symbols in lazy_archives", .{}); - var index: u32 = 0; - undef_loop: while (index < wasm.undefs.count()) { - const sym_name_index = wasm.undefs.keys()[index]; + pub fn limits(t: *const Table) std.wasm.Limits { + return .{ + .flags = .{ + .has_max = t.flags.limits_has_max, + .is_shared = t.flags.limits_is_shared, + }, + .min = t.limits_min, + .max = t.limits_max, + }; + } +}; - for (wasm.lazy_archives.items) |lazy_archive| { - const sym_name = wasm.stringSlice(sym_name_index); - log.debug("Detected symbol '{s}' in archive '{'}', parsing objects..", .{ - sym_name, lazy_archive.path, - }); - const offset = lazy_archive.archive.toc.get(sym_name) orelse continue; // symbol does not exist in this archive - - // Symbol is found in unparsed object file within current archive. - // Parse object and and resolve symbols again before we check remaining - // undefined symbols. - const file_contents = lazy_archive.file_contents[offset.items[0]..]; - const object = lazy_archive.archive.parseObject(wasm, file_contents, lazy_archive.path) catch |err| { - // TODO this fails to include information to identify which object failed - return diags.failParse(lazy_archive.path, "failed to parse object in archive: {s}", .{@errorName(err)}); - }; - try wasm.objects.append(gpa, object); - try wasm.resolveSymbolsInObject(@enumFromInt(wasm.objects.items.len - 1)); +/// Uniquely identifies a section across all objects. By subtracting +/// `Object.local_section_index_base` from this one, the Object section index +/// is obtained. +pub const ObjectSectionIndex = enum(u32) { + _, +}; - // continue loop for any remaining undefined symbols that still exist - // after resolving last object file - continue :undef_loop; - } - index += 1; +/// Index into `object_tables`. +pub const ObjectTableIndex = enum(u32) { + _, + + pub fn ptr(index: ObjectTableIndex, wasm: *const Wasm) *Table { + return &wasm.object_tables.items[@intFromEnum(index)]; } -} -/// Writes an unsigned 32-bit integer as a LEB128-encoded 'i32.const' value. -fn writeI32Const(writer: anytype, val: u32) !void { - try writer.writeByte(std.wasm.opcode(.i32_const)); - try leb.writeIleb128(writer, @as(i32, @bitCast(val))); -} + pub fn chaseWeak(i: ObjectTableIndex, wasm: *const Wasm) ObjectTableIndex { + const table = ptr(i, wasm); + if (table.flags.binding != .weak) return i; + const name = table.name.unwrap().?; + const import = wasm.object_table_imports.getPtr(name).?; + assert(import.resolution != .unresolved); // otherwise it should resolve to this one. + return import.resolution.unpack().object_table; + } +}; -fn setupInitMemoryFunction(wasm: *Wasm) !void { - const comp = wasm.base.comp; - const gpa = comp.gpa; - const shared_memory = comp.config.shared_memory; - const import_memory = comp.config.import_memory; +/// Index into `Wasm.object_globals`. +pub const ObjectGlobalIndex = enum(u32) { + _, - // Passive segments are used to avoid memory being reinitialized on each - // thread's instantiation. These passive segments are initialized and - // dropped in __wasm_init_memory, which is registered as the start function - // We also initialize bss segments (using memory.fill) as part of this - // function. - if (!wasm.hasPassiveInitializationSegments()) { - return; + pub fn ptr(index: ObjectGlobalIndex, wasm: *const Wasm) *ObjectGlobal { + return &wasm.object_globals.items[@intFromEnum(index)]; } - const sym_loc = try wasm.createSyntheticSymbol(wasm.preloaded_strings.__wasm_init_memory, .function); - wasm.symbolLocSymbol(sym_loc).mark(); - - const flag_address: u32 = if (shared_memory) address: { - // when we have passive initialization segments and shared memory - // `setupMemory` will create this symbol and set its virtual address. - const loc = wasm.globals.get(wasm.preloaded_strings.__wasm_init_memory_flag).?; - break :address wasm.symbolLocSymbol(loc).virtual_address; - } else 0; - - var function_body = std.ArrayList(u8).init(gpa); - defer function_body.deinit(); - const writer = function_body.writer(); - - // we have 0 locals - try leb.writeUleb128(writer, @as(u32, 0)); - - if (shared_memory) { - // destination blocks - // based on values we jump to corresponding label - try writer.writeByte(std.wasm.opcode(.block)); // $drop - try writer.writeByte(std.wasm.block_empty); // block type - - try writer.writeByte(std.wasm.opcode(.block)); // $wait - try writer.writeByte(std.wasm.block_empty); // block type - - try writer.writeByte(std.wasm.opcode(.block)); // $init - try writer.writeByte(std.wasm.block_empty); // block type - - // atomically check - try writeI32Const(writer, flag_address); - try writeI32Const(writer, 0); - try writeI32Const(writer, 1); - try writer.writeByte(std.wasm.opcode(.atomics_prefix)); - try leb.writeUleb128(writer, std.wasm.atomicsOpcode(.i32_atomic_rmw_cmpxchg)); - try leb.writeUleb128(writer, @as(u32, 2)); // alignment - try leb.writeUleb128(writer, @as(u32, 0)); // offset - - // based on the value from the atomic check, jump to the label. - try writer.writeByte(std.wasm.opcode(.br_table)); - try leb.writeUleb128(writer, @as(u32, 2)); // length of the table (we have 3 blocks but because of the mandatory default the length is 2). - try leb.writeUleb128(writer, @as(u32, 0)); // $init - try leb.writeUleb128(writer, @as(u32, 1)); // $wait - try leb.writeUleb128(writer, @as(u32, 2)); // $drop - try writer.writeByte(std.wasm.opcode(.end)); - } - - for (wasm.data_segments.keys(), wasm.data_segments.values(), 0..) |key, value, segment_index_usize| { - const segment_index: u32 = @intCast(segment_index_usize); - const segment = wasm.segmentPtr(value); - if (segment.needsPassiveInitialization(import_memory, key)) { - // For passive BSS segments we can simple issue a memory.fill(0). - // For non-BSS segments we do a memory.init. Both these - // instructions take as their first argument the destination - // address. - try writeI32Const(writer, segment.offset); - - if (shared_memory and std.mem.eql(u8, key, ".tdata")) { - // When we initialize the TLS segment we also set the `__tls_base` - // global. This allows the runtime to use this static copy of the - // TLS data for the first/main thread. - try writeI32Const(writer, segment.offset); - try writer.writeByte(std.wasm.opcode(.global_set)); - const loc = wasm.globals.get(wasm.preloaded_strings.__tls_base).?; - try leb.writeUleb128(writer, wasm.symbolLocSymbol(loc).index); - } - try writeI32Const(writer, 0); - try writeI32Const(writer, segment.size); - try writer.writeByte(std.wasm.opcode(.misc_prefix)); - if (std.mem.eql(u8, key, ".bss")) { - // fill bss segment with zeroes - try leb.writeUleb128(writer, std.wasm.miscOpcode(.memory_fill)); - } else { - // initialize the segment - try leb.writeUleb128(writer, std.wasm.miscOpcode(.memory_init)); - try leb.writeUleb128(writer, segment_index); - } - try writer.writeByte(0); // memory index immediate - } - } - - if (shared_memory) { - // we set the init memory flag to value '2' - try writeI32Const(writer, flag_address); - try writeI32Const(writer, 2); - try writer.writeByte(std.wasm.opcode(.atomics_prefix)); - try leb.writeUleb128(writer, std.wasm.atomicsOpcode(.i32_atomic_store)); - try leb.writeUleb128(writer, @as(u32, 2)); // alignment - try leb.writeUleb128(writer, @as(u32, 0)); // offset - - // notify any waiters for segment initialization completion - try writeI32Const(writer, flag_address); - try writer.writeByte(std.wasm.opcode(.i32_const)); - try leb.writeIleb128(writer, @as(i32, -1)); // number of waiters - try writer.writeByte(std.wasm.opcode(.atomics_prefix)); - try leb.writeUleb128(writer, std.wasm.atomicsOpcode(.memory_atomic_notify)); - try leb.writeUleb128(writer, @as(u32, 2)); // alignment - try leb.writeUleb128(writer, @as(u32, 0)); // offset - try writer.writeByte(std.wasm.opcode(.drop)); - - // branch and drop segments - try writer.writeByte(std.wasm.opcode(.br)); - try leb.writeUleb128(writer, @as(u32, 1)); - - // wait for thread to initialize memory segments - try writer.writeByte(std.wasm.opcode(.end)); // end $wait - try writeI32Const(writer, flag_address); - try writeI32Const(writer, 1); // expected flag value - try writer.writeByte(std.wasm.opcode(.i64_const)); - try leb.writeIleb128(writer, @as(i64, -1)); // timeout - try writer.writeByte(std.wasm.opcode(.atomics_prefix)); - try leb.writeUleb128(writer, std.wasm.atomicsOpcode(.memory_atomic_wait32)); - try leb.writeUleb128(writer, @as(u32, 2)); // alignment - try leb.writeUleb128(writer, @as(u32, 0)); // offset - try writer.writeByte(std.wasm.opcode(.drop)); - - try writer.writeByte(std.wasm.opcode(.end)); // end $drop - } - - for (wasm.data_segments.keys(), wasm.data_segments.values(), 0..) |name, value, segment_index_usize| { - const segment_index: u32 = @intCast(segment_index_usize); - const segment = wasm.segmentPtr(value); - if (segment.needsPassiveInitialization(import_memory, name) and - !std.mem.eql(u8, name, ".bss")) - { - // The TLS region should not be dropped since its is needed - // during the initialization of each thread (__wasm_init_tls). - if (shared_memory and std.mem.eql(u8, name, ".tdata")) { - continue; - } + pub fn name(index: ObjectGlobalIndex, wasm: *const Wasm) OptionalString { + return index.ptr(wasm).name; + } + + pub fn chaseWeak(i: ObjectGlobalIndex, wasm: *const Wasm) ObjectGlobalIndex { + const global = ptr(i, wasm); + if (global.flags.binding != .weak) return i; + const import_name = global.name.unwrap().?; + const import = wasm.object_global_imports.getPtr(import_name).?; + assert(import.resolution != .unresolved); // otherwise it should resolve to this one. + return import.resolution.unpack(wasm).object_global; + } +}; + +pub const ObjectMemory = extern struct { + flags: SymbolFlags, + name: OptionalString, + limits_min: u32, + limits_max: u32, + + /// Index into `Wasm.object_memories`. + pub const Index = enum(u32) { + _, - try writer.writeByte(std.wasm.opcode(.misc_prefix)); - try leb.writeUleb128(writer, std.wasm.miscOpcode(.data_drop)); - try leb.writeUleb128(writer, segment_index); + pub fn ptr(index: Index, wasm: *const Wasm) *ObjectMemory { + return &wasm.object_memories.items[@intFromEnum(index)]; } + }; + + pub fn limits(om: *const ObjectMemory) std.wasm.Limits { + return .{ + .flags = .{ + .has_max = om.limits_has_max, + .is_shared = om.limits_is_shared, + }, + .min = om.limits_min, + .max = om.limits_max, + }; } +}; - // End of the function body - try writer.writeByte(std.wasm.opcode(.end)); +/// Index into `Wasm.object_functions`. +pub const ObjectFunctionIndex = enum(u32) { + _, - try wasm.createSyntheticFunction( - wasm.preloaded_strings.__wasm_init_memory, - std.wasm.Type{ .params = &.{}, .returns = &.{} }, - &function_body, - ); -} + pub fn ptr(index: ObjectFunctionIndex, wasm: *const Wasm) *ObjectFunction { + return &wasm.object_functions.items[@intFromEnum(index)]; + } -/// Constructs a synthetic function that performs runtime relocations for -/// TLS symbols. This function is called by `__wasm_init_tls`. -fn setupTLSRelocationsFunction(wasm: *Wasm) !void { - const comp = wasm.base.comp; - const gpa = comp.gpa; - const shared_memory = comp.config.shared_memory; + pub fn toOptional(i: ObjectFunctionIndex) OptionalObjectFunctionIndex { + const result: OptionalObjectFunctionIndex = @enumFromInt(@intFromEnum(i)); + assert(result != .none); + return result; + } - // When we have TLS GOT entries and shared memory is enabled, - // we must perform runtime relocations or else we don't create the function. - if (!shared_memory or !wasm.requiresTLSReloc()) { - return; + pub fn chaseWeak(i: ObjectFunctionIndex, wasm: *const Wasm) ObjectFunctionIndex { + const func = ptr(i, wasm); + if (func.flags.binding != .weak) return i; + const name = func.name.unwrap().?; + const import = wasm.object_function_imports.getPtr(name).?; + assert(import.resolution != .unresolved); // otherwise it should resolve to this one. + return import.resolution.unpack(wasm).object_function; } +}; - const loc = try wasm.createSyntheticSymbol(wasm.preloaded_strings.__wasm_apply_global_tls_relocs, .function); - wasm.symbolLocSymbol(loc).mark(); - var function_body = std.ArrayList(u8).init(gpa); - defer function_body.deinit(); - const writer = function_body.writer(); - - // locals (we have none) - try writer.writeByte(0); - for (wasm.got_symbols.items, 0..) |got_loc, got_index| { - const sym: *Symbol = wasm.symbolLocSymbol(got_loc); - if (!sym.isTLS()) continue; // only relocate TLS symbols - if (sym.tag == .data and sym.isDefined()) { - // get __tls_base - try writer.writeByte(std.wasm.opcode(.global_get)); - try leb.writeUleb128(writer, wasm.symbolLocSymbol(wasm.globals.get(wasm.preloaded_strings.__tls_base).?).index); - - // add the virtual address of the symbol - try writer.writeByte(std.wasm.opcode(.i32_const)); - try leb.writeUleb128(writer, sym.virtual_address); - } else if (sym.tag == .function) { - @panic("TODO: relocate GOT entry of function"); - } else continue; - - try writer.writeByte(std.wasm.opcode(.i32_add)); - try writer.writeByte(std.wasm.opcode(.global_set)); - try leb.writeUleb128(writer, wasm.imported_globals_count + @as(u32, @intCast(wasm.wasm_globals.items.len + got_index))); - } - try writer.writeByte(std.wasm.opcode(.end)); - - try wasm.createSyntheticFunction( - wasm.preloaded_strings.__wasm_apply_global_tls_relocs, - std.wasm.Type{ .params = &.{}, .returns = &.{} }, - &function_body, - ); -} +/// Index into `object_functions`, or null. +pub const OptionalObjectFunctionIndex = enum(u32) { + none = std.math.maxInt(u32), + _, -fn validateFeatures( - wasm: *const Wasm, - to_emit: *[@typeInfo(Feature.Tag).@"enum".fields.len]bool, - emit_features_count: *u32, -) !void { - const comp = wasm.base.comp; - const diags = &wasm.base.comp.link_diags; - const target = comp.root_mod.resolved_target.result; - const shared_memory = comp.config.shared_memory; - const cpu_features = target.cpu.features; - const infer = cpu_features.isEmpty(); // when the user did not define any features, we infer them from linked objects. - const known_features_count = @typeInfo(Feature.Tag).@"enum".fields.len; - - var allowed = [_]bool{false} ** known_features_count; - var used = [_]u17{0} ** known_features_count; - var disallowed = [_]u17{0} ** known_features_count; - var required = [_]u17{0} ** known_features_count; - - // when false, we fail linking. We only verify this after a loop to catch all invalid features. - var valid_feature_set = true; - // will be set to true when there's any TLS segment found in any of the object files - var has_tls = false; - - // When the user has given an explicit list of features to enable, - // we extract them and insert each into the 'allowed' list. - if (!infer) { - inline for (@typeInfo(std.Target.wasm.Feature).@"enum".fields) |feature_field| { - if (cpu_features.isEnabled(feature_field.value)) { - allowed[feature_field.value] = true; - emit_features_count.* += 1; - } + pub fn unwrap(i: OptionalObjectFunctionIndex) ?ObjectFunctionIndex { + if (i == .none) return null; + return @enumFromInt(@intFromEnum(i)); + } +}; + +pub const ObjectDataSegment = extern struct { + /// `none` if segment info custom subsection is missing. + name: OptionalString, + flags: Flags, + payload: DataPayload, + offset: u32, + object_index: ObjectIndex, + + pub const Flags = packed struct(u32) { + alive: bool = false, + is_passive: bool = false, + alignment: Alignment = .none, + /// Signals that the segment contains only null terminated strings allowing + /// the linker to perform merging. + strings: bool = false, + /// The segment contains thread-local data. This means that a unique copy + /// of this segment will be created for each thread. + tls: bool = false, + /// If the object file is included in the final link, the segment should be + /// retained in the final output regardless of whether it is used by the + /// program. + retain: bool = false, + + _: u21 = 0, + }; + + /// Index into `Wasm.object_data_segments`. + pub const Index = enum(u32) { + _, + + pub fn ptr(i: Index, wasm: *const Wasm) *ObjectDataSegment { + return &wasm.object_data_segments.items[@intFromEnum(i)]; } + }; + + pub fn relocations(ods: *const ObjectDataSegment, wasm: *const Wasm) ObjectRelocation.IterableSlice { + const data_section_index = ods.object_index.ptr(wasm).data_section_index.?; + const relocs = wasm.object_relocations_table.get(data_section_index) orelse return .empty; + return .init(relocs, ods.offset, ods.payload.len, wasm); } +}; - // extract all the used, disallowed and required features from each - // linked object file so we can test them. - for (wasm.objects.items, 0..) |*object, file_index| { - for (object.features) |feature| { - const value = (@as(u16, @intCast(file_index)) << 1) | 1; - switch (feature.prefix) { - .used => { - used[@intFromEnum(feature.tag)] = value; - }, - .disallowed => { - disallowed[@intFromEnum(feature.tag)] = value; - }, - .required => { - required[@intFromEnum(feature.tag)] = value; - used[@intFromEnum(feature.tag)] = value; - }, - } +/// A local or exported global const from an object file. +pub const ObjectData = extern struct { + segment: ObjectDataSegment.Index, + /// Index into the object segment payload. Must be <= the segment's size. + offset: u32, + /// May be zero. `offset + size` must be <= the segment's size. + size: u32, + name: String, + flags: SymbolFlags, + + /// Index into `Wasm.object_datas`. + pub const Index = enum(u32) { + _, + + pub fn ptr(i: Index, wasm: *const Wasm) *ObjectData { + return &wasm.object_datas.items[@intFromEnum(i)]; } + }; +}; - for (object.segment_info) |segment| { - if (segment.isTLS()) { - has_tls = true; - } +pub const ObjectDataImport = extern struct { + resolution: Resolution, + flags: SymbolFlags, + source_location: SourceLocation, + + pub const Resolution = enum(u32) { + unresolved, + __zig_error_names, + __zig_error_name_table, + __heap_base, + __heap_end, + /// Next, an `ObjectData.Index`. + /// Next, index into `uavs_obj` or `uavs_exe` depending on whether emitting an object. + /// Next, index into `navs_obj` or `navs_exe` depending on whether emitting an object. + _, + + const first_object = @intFromEnum(Resolution.__heap_end) + 1; + + pub const Unpacked = union(enum) { + unresolved, + __zig_error_names, + __zig_error_name_table, + __heap_base, + __heap_end, + object: ObjectData.Index, + uav_exe: UavsExeIndex, + uav_obj: UavsObjIndex, + nav_exe: NavsExeIndex, + nav_obj: NavsObjIndex, + }; + + pub fn unpack(r: Resolution, wasm: *const Wasm) Unpacked { + return switch (r) { + .unresolved => .unresolved, + .__zig_error_names => .__zig_error_names, + .__zig_error_name_table => .__zig_error_name_table, + .__heap_base => .__heap_base, + .__heap_end => .__heap_end, + _ => { + const object_index = @intFromEnum(r) - first_object; + + const uav_index = if (object_index < wasm.object_datas.items.len) + return .{ .object = @enumFromInt(object_index) } + else + object_index - wasm.object_datas.items.len; + + const comp = wasm.base.comp; + const is_obj = comp.config.output_mode == .Obj; + if (is_obj) { + const nav_index = if (uav_index < wasm.uavs_obj.entries.len) + return .{ .uav_obj = @enumFromInt(uav_index) } + else + uav_index - wasm.uavs_obj.entries.len; + + return .{ .nav_obj = @enumFromInt(nav_index) }; + } else { + const nav_index = if (uav_index < wasm.uavs_exe.entries.len) + return .{ .uav_exe = @enumFromInt(uav_index) } + else + uav_index - wasm.uavs_exe.entries.len; + + return .{ .nav_exe = @enumFromInt(nav_index) }; + } + }, + }; } - } - // when we infer the features, we allow each feature found in the 'used' set - // and insert it into the 'allowed' set. When features are not inferred, - // we validate that a used feature is allowed. - for (used, 0..) |used_set, used_index| { - const is_enabled = @as(u1, @truncate(used_set)) != 0; - if (infer) { - allowed[used_index] = is_enabled; - emit_features_count.* += @intFromBool(is_enabled); - } else if (is_enabled and !allowed[used_index]) { - diags.addParseError( - wasm.objects.items[used_set >> 1].path, - "feature '{}' not allowed, but used by linked object", - .{@as(Feature.Tag, @enumFromInt(used_index))}, - ); - valid_feature_set = false; + pub fn pack(wasm: *const Wasm, unpacked: Unpacked) Resolution { + return switch (unpacked) { + .unresolved => .unresolved, + .__zig_error_names => .__zig_error_names, + .__zig_error_name_table => .__zig_error_name_table, + .__heap_base => .__heap_base, + .__heap_end => .__heap_end, + .object => |i| @enumFromInt(first_object + @intFromEnum(i)), + inline .uav_exe, .uav_obj => |i| @enumFromInt(first_object + wasm.object_datas.items.len + @intFromEnum(i)), + .nav_exe => |i| @enumFromInt(first_object + wasm.object_datas.items.len + wasm.uavs_exe.entries.len + @intFromEnum(i)), + .nav_obj => |i| @enumFromInt(first_object + wasm.object_datas.items.len + wasm.uavs_obj.entries.len + @intFromEnum(i)), + }; } - } - if (!valid_feature_set) { - return error.FlushFailure; - } + pub fn fromObjectDataIndex(wasm: *const Wasm, object_data_index: ObjectData.Index) Resolution { + return pack(wasm, .{ .object = object_data_index }); + } - if (shared_memory) { - const disallowed_feature = disallowed[@intFromEnum(Feature.Tag.shared_mem)]; - if (@as(u1, @truncate(disallowed_feature)) != 0) { - diags.addParseError( - wasm.objects.items[disallowed_feature >> 1].path, - "shared-memory is disallowed because it wasn't compiled with 'atomics' and 'bulk-memory' features enabled", - .{}, - ); - valid_feature_set = false; + pub fn objectDataSegment(r: Resolution, wasm: *const Wasm) ?ObjectDataSegment.Index { + return switch (unpack(r, wasm)) { + .unresolved => unreachable, + .object => |i| i.ptr(wasm).segment, + .__zig_error_names, + .__zig_error_name_table, + .__heap_base, + .__heap_end, + .uav_exe, + .uav_obj, + .nav_exe, + .nav_obj, + => null, + }; } - for ([_]Feature.Tag{ .atomics, .bulk_memory }) |feature| { - if (!allowed[@intFromEnum(feature)]) { - var err = try diags.addErrorWithNotes(0); - try err.addMsg("feature '{}' is not used but is required for shared-memory", .{feature}); - } + pub fn dataLoc(r: Resolution, wasm: *const Wasm) DataLoc { + return switch (unpack(r, wasm)) { + .unresolved => unreachable, + .object => |i| { + const ptr = i.ptr(wasm); + return .{ + .segment = .fromObjectDataSegment(wasm, ptr.segment), + .offset = ptr.offset, + }; + }, + .__zig_error_names => .{ .segment = .__zig_error_names, .offset = 0 }, + .__zig_error_name_table => .{ .segment = .__zig_error_name_table, .offset = 0 }, + .__heap_base => .{ .segment = .__heap_base, .offset = 0 }, + .__heap_end => .{ .segment = .__heap_end, .offset = 0 }, + .uav_exe => @panic("TODO"), + .uav_obj => @panic("TODO"), + .nav_exe => @panic("TODO"), + .nav_obj => @panic("TODO"), + }; } - } + }; - if (has_tls) { - for ([_]Feature.Tag{ .atomics, .bulk_memory }) |feature| { - if (!allowed[@intFromEnum(feature)]) { - var err = try diags.addErrorWithNotes(0); - try err.addMsg("feature '{}' is not used but is required for thread-local storage", .{feature}); - } + /// Points into `Wasm.object_data_imports`. + pub const Index = enum(u32) { + _, + + pub fn value(i: @This(), wasm: *const Wasm) *ObjectDataImport { + return &wasm.object_data_imports.values()[@intFromEnum(i)]; } - } - // For each linked object, validate the required and disallowed features - for (wasm.objects.items) |*object| { - var object_used_features = [_]bool{false} ** known_features_count; - for (object.features) |feature| { - if (feature.prefix == .disallowed) continue; // already defined in 'disallowed' set. - // from here a feature is always used - const disallowed_feature = disallowed[@intFromEnum(feature.tag)]; - if (@as(u1, @truncate(disallowed_feature)) != 0) { - var err = try diags.addErrorWithNotes(2); - try err.addMsg("feature '{}' is disallowed, but used by linked object", .{feature.tag}); - try err.addNote("disallowed by '{'}'", .{wasm.objects.items[disallowed_feature >> 1].path}); - try err.addNote("used in '{'}'", .{object.path}); - valid_feature_set = false; - } - object_used_features[@intFromEnum(feature.tag)] = true; + pub fn fromSymbolName(wasm: *const Wasm, name: String) ?Index { + return @enumFromInt(wasm.object_data_imports.getIndex(name) orelse return null); } + }; +}; - // validate the linked object file has each required feature - for (required, 0..) |required_feature, feature_index| { - const is_required = @as(u1, @truncate(required_feature)) != 0; - if (is_required and !object_used_features[feature_index]) { - var err = try diags.addErrorWithNotes(2); - try err.addMsg("feature '{}' is required but not used in linked object", .{@as(Feature.Tag, @enumFromInt(feature_index))}); - try err.addNote("required by '{'}'", .{wasm.objects.items[required_feature >> 1].path}); - try err.addNote("missing in '{'}'", .{object.path}); - valid_feature_set = false; - } +pub const DataPayload = extern struct { + off: Off, + /// The size in bytes of the data representing the segment within the section. + len: u32, + + pub const Off = enum(u32) { + /// The payload is all zeroes (bss section). + none = std.math.maxInt(u32), + /// Points into string_bytes. No corresponding string_table entry. + _, + + pub fn unwrap(off: Off) ?u32 { + return if (off == .none) null else @intFromEnum(off); } + }; + + pub fn slice(p: DataPayload, wasm: *const Wasm) []const u8 { + return wasm.string_bytes.items[p.off.unwrap().?..][0..p.len]; } +}; + +/// A reference to a local or exported global const. +pub const DataSegmentId = enum(u32) { + __zig_error_names, + __zig_error_name_table, + /// All name string bytes for all `@tagName` implementations, concatenated together. + __zig_tag_names, + /// All tag name slices for all `@tagName` implementations, concatenated together. + __zig_tag_name_table, + /// This and `__heap_end` are better retrieved via a global, but there is + /// some suboptimal code out there (wasi libc) that additionally needs them + /// as data symbols. + __heap_base, + __heap_end, + /// First, an `ObjectDataSegment.Index`. + /// Next, index into `uavs_obj` or `uavs_exe` depending on whether emitting an object. + /// Next, index into `navs_obj` or `navs_exe` depending on whether emitting an object. + _, + + const first_object = @intFromEnum(DataSegmentId.__heap_end) + 1; + + pub const Category = enum { + /// Thread-local variables. + tls, + /// Data that is not zero initialized and not threadlocal. + data, + /// Zero-initialized. Does not require corresponding bytes in the + /// output file. + zero, + }; - if (!valid_feature_set) { - return error.FlushFailure; + pub const Unpacked = union(enum) { + __zig_error_names, + __zig_error_name_table, + __zig_tag_names, + __zig_tag_name_table, + __heap_base, + __heap_end, + object: ObjectDataSegment.Index, + uav_exe: UavsExeIndex, + uav_obj: UavsObjIndex, + nav_exe: NavsExeIndex, + nav_obj: NavsObjIndex, + }; + + pub fn pack(wasm: *const Wasm, unpacked: Unpacked) DataSegmentId { + return switch (unpacked) { + .__zig_error_names => .__zig_error_names, + .__zig_error_name_table => .__zig_error_name_table, + .__zig_tag_names => .__zig_tag_names, + .__zig_tag_name_table => .__zig_tag_name_table, + .__heap_base => .__heap_base, + .__heap_end => .__heap_end, + .object => |i| @enumFromInt(first_object + @intFromEnum(i)), + inline .uav_exe, .uav_obj => |i| @enumFromInt(first_object + wasm.object_data_segments.items.len + @intFromEnum(i)), + .nav_exe => |i| @enumFromInt(first_object + wasm.object_data_segments.items.len + wasm.uavs_exe.entries.len + @intFromEnum(i)), + .nav_obj => |i| @enumFromInt(first_object + wasm.object_data_segments.items.len + wasm.uavs_obj.entries.len + @intFromEnum(i)), + }; } - to_emit.* = allowed; -} + pub fn unpack(id: DataSegmentId, wasm: *const Wasm) Unpacked { + return switch (id) { + .__zig_error_names => .__zig_error_names, + .__zig_error_name_table => .__zig_error_name_table, + .__zig_tag_names => .__zig_tag_names, + .__zig_tag_name_table => .__zig_tag_name_table, + .__heap_base => .__heap_base, + .__heap_end => .__heap_end, + _ => { + const object_index = @intFromEnum(id) - first_object; -/// Creates synthetic linker-symbols, but only if they are being referenced from -/// any object file. For instance, the `__heap_base` symbol will only be created, -/// if one or multiple undefined references exist. When none exist, the symbol will -/// not be created, ensuring we don't unnecessarily emit unreferenced symbols. -fn resolveLazySymbols(wasm: *Wasm) !void { - const comp = wasm.base.comp; - const gpa = comp.gpa; - const shared_memory = comp.config.shared_memory; + const uav_index = if (object_index < wasm.object_data_segments.items.len) + return .{ .object = @enumFromInt(object_index) } + else + object_index - wasm.object_data_segments.items.len; - if (wasm.getExistingString("__heap_base")) |name_offset| { - if (wasm.undefs.fetchSwapRemove(name_offset)) |kv| { - const loc = try wasm.createSyntheticSymbolOffset(name_offset, .data); - try wasm.discarded.putNoClobber(gpa, kv.value, loc); - _ = wasm.resolved_symbols.swapRemove(loc); // we don't want to emit this symbol, only use it for relocations. - } + const comp = wasm.base.comp; + const is_obj = comp.config.output_mode == .Obj; + if (is_obj) { + const nav_index = if (uav_index < wasm.uavs_obj.entries.len) + return .{ .uav_obj = @enumFromInt(uav_index) } + else + uav_index - wasm.uavs_obj.entries.len; + + return .{ .nav_obj = @enumFromInt(nav_index) }; + } else { + const nav_index = if (uav_index < wasm.uavs_exe.entries.len) + return .{ .uav_exe = @enumFromInt(uav_index) } + else + uav_index - wasm.uavs_exe.entries.len; + + return .{ .nav_exe = @enumFromInt(nav_index) }; + } + }, + }; } - if (wasm.getExistingString("__heap_end")) |name_offset| { - if (wasm.undefs.fetchSwapRemove(name_offset)) |kv| { - const loc = try wasm.createSyntheticSymbolOffset(name_offset, .data); - try wasm.discarded.putNoClobber(gpa, kv.value, loc); - _ = wasm.resolved_symbols.swapRemove(loc); - } + pub fn fromNav(wasm: *const Wasm, nav_index: InternPool.Nav.Index) DataSegmentId { + const comp = wasm.base.comp; + const is_obj = comp.config.output_mode == .Obj; + return pack(wasm, if (is_obj) .{ + .nav_obj = @enumFromInt(wasm.navs_obj.getIndex(nav_index).?), + } else .{ + .nav_exe = @enumFromInt(wasm.navs_exe.getIndex(nav_index).?), + }); } - if (!shared_memory) { - if (wasm.getExistingString("__tls_base")) |name_offset| { - if (wasm.undefs.fetchSwapRemove(name_offset)) |kv| { - const loc = try wasm.createSyntheticSymbolOffset(name_offset, .global); - try wasm.discarded.putNoClobber(gpa, kv.value, loc); - _ = wasm.resolved_symbols.swapRemove(kv.value); - const symbol = wasm.symbolLocSymbol(loc); - symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); - symbol.index = @intCast(wasm.imported_globals_count + wasm.wasm_globals.items.len); - try wasm.wasm_globals.append(gpa, .{ - .global_type = .{ .valtype = .i32, .mutable = true }, - .init = .{ .i32_const = undefined }, - }); - } - } + pub fn fromObjectDataSegment(wasm: *const Wasm, object_data_segment: ObjectDataSegment.Index) DataSegmentId { + return pack(wasm, .{ .object = object_data_segment }); } -} -pub fn findGlobalSymbol(wasm: *const Wasm, name: []const u8) ?SymbolLoc { - const name_index = wasm.getExistingString(name) orelse return null; - return wasm.globals.get(name_index); -} + pub fn category(id: DataSegmentId, wasm: *const Wasm) Category { + return switch (unpack(id, wasm)) { + .__zig_error_names, + .__zig_error_name_table, + .__zig_tag_names, + .__zig_tag_name_table, + .__heap_base, + .__heap_end, + => .data, -fn checkUndefinedSymbols(wasm: *const Wasm) !void { - const comp = wasm.base.comp; - const diags = &wasm.base.comp.link_diags; - if (comp.config.output_mode == .Obj) return; - if (wasm.import_symbols) return; - - var found_undefined_symbols = false; - for (wasm.undefs.values()) |undef| { - const symbol = wasm.symbolLocSymbol(undef); - if (symbol.tag == .data) { - found_undefined_symbols = true; - const symbol_name = wasm.symbolLocName(undef); - switch (undef.file) { - .zig_object => { - // TODO: instead of saying the zig compilation unit, attach an actual source location - // to this diagnostic - diags.addError("unresolved symbol in Zig compilation unit: {s}", .{symbol_name}); - }, - .none => { - diags.addError("internal linker bug: unresolved synthetic symbol: {s}", .{symbol_name}); - }, - _ => { - const path = wasm.objects.items[@intFromEnum(undef.file)].path; - diags.addParseError(path, "unresolved symbol: {s}", .{symbol_name}); - }, - } - } - } - if (found_undefined_symbols) { - return error.LinkFailure; + .object => |i| { + const ptr = i.ptr(wasm); + if (ptr.flags.tls) return .tls; + if (wasm.isBss(ptr.name)) return .zero; + return .data; + }, + inline .uav_exe, .uav_obj => |i| if (i.value(wasm).code.off == .none) .zero else .data, + inline .nav_exe, .nav_obj => |i| { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const nav = ip.getNav(i.key(wasm).*); + if (nav.isThreadlocal(ip)) return .tls; + const code = i.value(wasm).code; + return if (code.off == .none) .zero else .data; + }, + }; } -} -pub fn deinit(wasm: *Wasm) void { - const gpa = wasm.base.comp.gpa; - if (wasm.llvm_object) |llvm_object| llvm_object.deinit(); + pub fn isTls(id: DataSegmentId, wasm: *const Wasm) bool { + return switch (unpack(id, wasm)) { + .__zig_error_names, + .__zig_error_name_table, + .__zig_tag_names, + .__zig_tag_name_table, + .__heap_base, + .__heap_end, + => false, + + .object => |i| i.ptr(wasm).flags.tls, + .uav_exe, .uav_obj => false, + inline .nav_exe, .nav_obj => |i| { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const nav = ip.getNav(i.key(wasm).*); + return nav.isThreadlocal(ip); + }, + }; + } - for (wasm.func_types.items) |*func_type| { - func_type.deinit(gpa); + pub fn isBss(id: DataSegmentId, wasm: *const Wasm) bool { + return id.category(wasm) == .zero; + } + + pub fn name(id: DataSegmentId, wasm: *const Wasm) []const u8 { + return switch (unpack(id, wasm)) { + .__zig_error_names, + .__zig_error_name_table, + .__zig_tag_names, + .__zig_tag_name_table, + .uav_exe, + .uav_obj, + .__heap_base, + .__heap_end, + => ".data", + + .object => |i| i.ptr(wasm).name.unwrap().?.slice(wasm), + inline .nav_exe, .nav_obj => |i| { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const nav = ip.getNav(i.key(wasm).*); + return nav.getLinkSection().toSlice(ip) orelse switch (category(id, wasm)) { + .tls => ".tdata", + .data => ".data", + .zero => ".bss", + }; + }, + }; } - for (wasm.segment_info.values()) |segment_info| { - gpa.free(segment_info.name); + + pub fn alignment(id: DataSegmentId, wasm: *const Wasm) Alignment { + return switch (unpack(id, wasm)) { + .__zig_error_names, .__zig_tag_names => .@"1", + .__zig_error_name_table, .__zig_tag_name_table, .__heap_base, .__heap_end => wasm.pointerAlignment(), + .object => |i| i.ptr(wasm).flags.alignment, + inline .uav_exe, .uav_obj => |i| { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const ip_index = i.key(wasm).*; + if (wasm.overaligned_uavs.get(ip_index)) |a| return a; + const ty: Zcu.Type = .fromInterned(ip.typeOf(ip_index)); + const result = ty.abiAlignment(zcu); + assert(result != .none); + return result; + }, + inline .nav_exe, .nav_obj => |i| { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const nav = ip.getNav(i.key(wasm).*); + const explicit = nav.getAlignment(); + if (explicit != .none) return explicit; + const ty: Zcu.Type = .fromInterned(nav.typeOf(ip)); + const result = ty.abiAlignment(zcu); + assert(result != .none); + return result; + }, + }; } - if (wasm.zig_object) |zig_obj| { - zig_obj.deinit(wasm); + + pub fn refCount(id: DataSegmentId, wasm: *const Wasm) u32 { + return switch (unpack(id, wasm)) { + .__zig_error_names => @intCast(wasm.error_name_offs.items.len), + .__zig_error_name_table => wasm.error_name_table_ref_count, + .__zig_tag_names => @intCast(wasm.tag_name_offs.items.len), + .__zig_tag_name_table => wasm.tag_name_table_ref_count, + .object, .uav_obj, .nav_obj, .__heap_base, .__heap_end => 0, + inline .uav_exe, .nav_exe => |i| i.value(wasm).count, + }; } - for (wasm.objects.items) |*object| { - object.deinit(gpa); + + pub fn isPassive(id: DataSegmentId, wasm: *const Wasm) bool { + const comp = wasm.base.comp; + if (comp.config.import_memory) return true; + return switch (unpack(id, wasm)) { + .__zig_error_names, + .__zig_error_name_table, + .__zig_tag_names, + .__zig_tag_name_table, + .__heap_base, + .__heap_end, + => false, + + .object => |i| i.ptr(wasm).flags.is_passive, + .uav_exe, .uav_obj, .nav_exe, .nav_obj => false, + }; } - for (wasm.lazy_archives.items) |*lazy_archive| lazy_archive.deinit(gpa); - wasm.lazy_archives.deinit(gpa); + pub fn isEmpty(id: DataSegmentId, wasm: *const Wasm) bool { + return switch (unpack(id, wasm)) { + .__zig_error_names, + .__zig_error_name_table, + .__zig_tag_names, + .__zig_tag_name_table, + .__heap_base, + .__heap_end, + => false, - if (wasm.globals.get(wasm.preloaded_strings.__wasm_init_tls)) |loc| { - const atom = wasm.symbol_atom.get(loc).?; - wasm.getAtomPtr(atom).deinit(gpa); + .object => |i| i.ptr(wasm).payload.off == .none, + inline .uav_exe, .uav_obj, .nav_exe, .nav_obj => |i| i.value(wasm).code.off == .none, + }; } - wasm.synthetic_symbols.deinit(gpa); - wasm.globals.deinit(gpa); - wasm.resolved_symbols.deinit(gpa); - wasm.undefs.deinit(gpa); - wasm.discarded.deinit(gpa); - wasm.symbol_atom.deinit(gpa); - wasm.atoms.deinit(gpa); - wasm.managed_atoms.deinit(gpa); - wasm.segments.deinit(gpa); - wasm.data_segments.deinit(gpa); - wasm.segment_info.deinit(gpa); - wasm.objects.deinit(gpa); - - // free output sections - wasm.imports.deinit(gpa); - wasm.func_types.deinit(gpa); - wasm.functions.deinit(gpa); - wasm.wasm_globals.deinit(gpa); - wasm.function_table.deinit(gpa); - wasm.tables.deinit(gpa); - wasm.init_funcs.deinit(gpa); - wasm.exports.deinit(gpa); + pub fn size(id: DataSegmentId, wasm: *const Wasm) u32 { + return switch (unpack(id, wasm)) { + .__zig_error_names => @intCast(wasm.error_name_bytes.items.len), + .__zig_error_name_table => { + const comp = wasm.base.comp; + const zcu = comp.zcu.?; + const errors_len = wasm.error_name_offs.items.len; + const elem_size = Zcu.Type.slice_const_u8_sentinel_0.abiSize(zcu); + return @intCast(errors_len * elem_size); + }, + .__zig_tag_names => @intCast(wasm.tag_name_bytes.items.len), + .__zig_tag_name_table => { + const comp = wasm.base.comp; + const zcu = comp.zcu.?; + const table_len = wasm.tag_name_offs.items.len; + const elem_size = Zcu.Type.slice_const_u8_sentinel_0.abiSize(zcu); + return @intCast(table_len * elem_size); + }, + .__heap_base, .__heap_end => wasm.pointerSize(), + .object => |i| i.ptr(wasm).payload.len, + inline .uav_exe, .uav_obj, .nav_exe, .nav_obj => |i| i.value(wasm).code.len, + }; + } +}; - wasm.string_bytes.deinit(gpa); - wasm.string_table.deinit(gpa); - wasm.dump_argv_list.deinit(gpa); -} +pub const DataLoc = struct { + segment: Wasm.DataSegmentId, + offset: u32, -pub fn updateFunc(wasm: *Wasm, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { - if (build_options.skip_non_native and builtin.object_format != .wasm) { - @panic("Attempted to compile for object format that was disabled by build configuration"); + pub fn fromObjectDataIndex(wasm: *const Wasm, i: Wasm.ObjectData.Index) DataLoc { + const ptr = i.ptr(wasm); + return .{ + .segment = .fromObjectDataSegment(wasm, ptr.segment), + .offset = ptr.offset, + }; } - if (wasm.llvm_object) |llvm_object| return llvm_object.updateFunc(pt, func_index, air, liveness); - try wasm.zig_object.?.updateFunc(wasm, pt, func_index, air, liveness); -} -// Generate code for the "Nav", storing it in memory to be later written to -// the file on flush(). -pub fn updateNav(wasm: *Wasm, pt: Zcu.PerThread, nav: InternPool.Nav.Index) !void { - if (build_options.skip_non_native and builtin.object_format != .wasm) { - @panic("Attempted to compile for object format that was disabled by build configuration"); + pub fn fromDataImportId(wasm: *const Wasm, id: Wasm.DataImportId) DataLoc { + return switch (id.unpack(wasm)) { + .object_data_import => |i| .fromObjectDataImportIndex(wasm, i), + .zcu_import => |i| .fromZcuImport(wasm, i), + }; } - if (wasm.llvm_object) |llvm_object| return llvm_object.updateNav(pt, nav); - try wasm.zig_object.?.updateNav(wasm, pt, nav); -} -pub fn updateLineNumber(wasm: *Wasm, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) !void { - if (wasm.llvm_object) |_| return; - try wasm.zig_object.?.updateLineNumber(pt, ti_id); -} - -/// From a given symbol location, returns its `wasm.GlobalType`. -/// Asserts the Symbol represents a global. -fn getGlobalType(wasm: *const Wasm, loc: SymbolLoc) std.wasm.GlobalType { - const symbol = wasm.symbolLocSymbol(loc); - assert(symbol.tag == .global); - const is_undefined = symbol.isUndefined(); - switch (loc.file) { - .zig_object => { - const zo = wasm.zig_object.?; - return if (is_undefined) - zo.imports.get(loc.index).?.kind.global - else - zo.globals.items[symbol.index - zo.imported_globals_count].global_type; - }, - .none => { - return if (is_undefined) - wasm.imports.get(loc).?.kind.global - else - wasm.wasm_globals.items[symbol.index].global_type; - }, - _ => { - const obj = &wasm.objects.items[@intFromEnum(loc.file)]; - return if (is_undefined) - obj.findImport(obj.symtable[@intFromEnum(loc.index)]).kind.global - else - obj.globals[symbol.index - obj.imported_globals_count].global_type; - }, + pub fn fromObjectDataImportIndex(wasm: *const Wasm, i: Wasm.ObjectDataImport.Index) DataLoc { + return i.value(wasm).resolution.dataLoc(wasm); } -} -/// From a given symbol location, returns its `wasm.Type`. -/// Asserts the Symbol represents a function. -fn getFunctionSignature(wasm: *const Wasm, loc: SymbolLoc) std.wasm.Type { - const symbol = wasm.symbolLocSymbol(loc); - assert(symbol.tag == .function); - const is_undefined = symbol.isUndefined(); - switch (loc.file) { - .zig_object => { - const zo = wasm.zig_object.?; - if (is_undefined) { - const type_index = zo.imports.get(loc.index).?.kind.function; - return zo.func_types.items[type_index]; - } - const sym = zo.symbols.items[@intFromEnum(loc.index)]; - const type_index = zo.functions.items[sym.index].type_index; - return zo.func_types.items[type_index]; - }, - .none => { - if (is_undefined) { - const type_index = wasm.imports.get(loc).?.kind.function; - return wasm.func_types.items[type_index]; - } - return wasm.func_types.items[ - wasm.functions.get(.{ - .file = .none, - .index = symbol.index, - }).?.func.type_index - ]; - }, - _ => { - const obj = &wasm.objects.items[@intFromEnum(loc.file)]; - if (is_undefined) { - const type_index = obj.findImport(obj.symtable[@intFromEnum(loc.index)]).kind.function; - return obj.func_types[type_index]; - } - const sym = obj.symtable[@intFromEnum(loc.index)]; - const type_index = obj.functions[sym.index - obj.imported_functions_count].type_index; - return obj.func_types[type_index]; - }, + pub fn fromZcuImport(wasm: *const Wasm, zcu_import: ZcuImportIndex) DataLoc { + const nav_index = zcu_import.ptr(wasm).*; + return .{ + .segment = .fromNav(wasm, nav_index), + .offset = 0, + }; } -} +}; -/// Returns the symbol index from a symbol of which its flag is set global, -/// such as an exported or imported symbol. -/// If the symbol does not yet exist, creates a new one symbol instead -/// and then returns the index to it. -pub fn getGlobalSymbol(wasm: *Wasm, name: []const u8, lib_name: ?[]const u8) !Symbol.Index { - _ = lib_name; - const name_index = try wasm.internString(name); - return wasm.zig_object.?.getGlobalSymbol(wasm.base.comp.gpa, name_index); -} +/// Index into `Wasm.uavs`. +pub const UavIndex = enum(u32) { + _, +}; -/// For a given `Nav`, find the given symbol index's atom, and create a relocation for the type. -/// Returns the given pointer address -pub fn getNavVAddr( - wasm: *Wasm, - pt: Zcu.PerThread, - nav: InternPool.Nav.Index, - reloc_info: link.File.RelocInfo, -) !u64 { - return wasm.zig_object.?.getNavVAddr(wasm, pt, nav, reloc_info); -} +pub const CustomSegment = extern struct { + payload: Payload, + flags: SymbolFlags, + section_name: String, -pub fn lowerUav( - wasm: *Wasm, - pt: Zcu.PerThread, - uav: InternPool.Index, - explicit_alignment: Alignment, - src_loc: Zcu.LazySrcLoc, -) !codegen.GenResult { - return wasm.zig_object.?.lowerUav(wasm, pt, uav, explicit_alignment, src_loc); -} + pub const Payload = DataPayload; +}; -pub fn getUavVAddr(wasm: *Wasm, uav: InternPool.Index, reloc_info: link.File.RelocInfo) !u64 { - return wasm.zig_object.?.getUavVAddr(wasm, uav, reloc_info); -} +/// An index into string_bytes where a wasm expression is found. +pub const Expr = enum(u32) { + _, -pub fn deleteExport( - wasm: *Wasm, - exported: Zcu.Exported, - name: InternPool.NullTerminatedString, -) void { - if (wasm.llvm_object) |_| return; - return wasm.zig_object.?.deleteExport(wasm, exported, name); -} + pub const end = @intFromEnum(std.wasm.Opcode.end); -pub fn updateExports( - wasm: *Wasm, - pt: Zcu.PerThread, - exported: Zcu.Exported, - export_indices: []const u32, -) !void { - if (build_options.skip_non_native and builtin.object_format != .wasm) { - @panic("Attempted to compile for object format that was disabled by build configuration"); + pub fn slice(index: Expr, wasm: *const Wasm) [:end]const u8 { + const start_slice = wasm.string_bytes.items[@intFromEnum(index)..]; + const end_pos = Object.exprEndPos(start_slice, 0) catch |err| switch (err) { + error.InvalidInitOpcode => unreachable, + }; + return start_slice[0..end_pos :end]; } - if (wasm.llvm_object) |llvm_object| return llvm_object.updateExports(pt, exported, export_indices); - return wasm.zig_object.?.updateExports(wasm, pt, exported, export_indices); -} +}; -pub fn freeDecl(wasm: *Wasm, decl_index: InternPool.DeclIndex) void { - if (wasm.llvm_object) |llvm_object| return llvm_object.freeDecl(decl_index); - return wasm.zig_object.?.freeDecl(wasm, decl_index); -} +pub const FunctionType = extern struct { + params: ValtypeList, + returns: ValtypeList, -/// Assigns indexes to all indirect functions. -/// Starts at offset 1, where the value `0` represents an unresolved function pointer -/// or null-pointer -fn mapFunctionTable(wasm: *Wasm) void { - var it = wasm.function_table.iterator(); - var index: u32 = 1; - while (it.next()) |entry| { - const symbol = wasm.symbolLocSymbol(entry.key_ptr.*); - if (symbol.isAlive()) { - entry.value_ptr.* = index; - index += 1; - } else { - wasm.function_table.removeByPtr(entry.key_ptr); + /// Index into func_types + pub const Index = enum(u32) { + _, + + pub fn ptr(i: Index, wasm: *const Wasm) *FunctionType { + return &wasm.func_types.keys()[@intFromEnum(i)]; } - } - if (wasm.import_table or wasm.base.comp.config.output_mode == .Obj) { - const sym_loc = wasm.globals.get(wasm.preloaded_strings.__indirect_function_table).?; - const import = wasm.imports.getPtr(sym_loc).?; - import.kind.table.limits.min = index - 1; // we start at index 1. - } else if (index > 1) { - log.debug("Appending indirect function table", .{}); - const sym_loc = wasm.globals.get(wasm.preloaded_strings.__indirect_function_table).?; - const symbol = wasm.symbolLocSymbol(sym_loc); - const table = &wasm.tables.items[symbol.index - wasm.imported_tables_count]; - table.limits = .{ .min = index, .max = index, .flags = 0x1 }; + pub fn fmt(i: Index, wasm: *const Wasm) Formatter { + return i.ptr(wasm).fmt(wasm); + } + }; + + pub const format = @compileError("can't format without *Wasm reference"); + + pub fn eql(a: FunctionType, b: FunctionType) bool { + return a.params == b.params and a.returns == b.returns; } -} -/// From a given index, append the given `Atom` at the back of the linked list. -/// Simply inserts it into the map of atoms when it doesn't exist yet. -pub fn appendAtomAtIndex(wasm: *Wasm, index: Segment.Index, atom_index: Atom.Index) !void { - const gpa = wasm.base.comp.gpa; - const atom = wasm.getAtomPtr(atom_index); - if (wasm.atoms.getPtr(index)) |last_index_ptr| { - atom.prev = last_index_ptr.*; - last_index_ptr.* = atom_index; - } else { - try wasm.atoms.putNoClobber(gpa, index, atom_index); + pub fn fmt(ft: FunctionType, wasm: *const Wasm) Formatter { + return .{ .wasm = wasm, .ft = ft }; } -} -fn allocateAtoms(wasm: *Wasm) !void { - // first sort the data segments - try sortDataSegments(wasm); - - var it = wasm.atoms.iterator(); - while (it.next()) |entry| { - const segment = wasm.segmentPtr(entry.key_ptr.*); - var atom_index = entry.value_ptr.*; - if (entry.key_ptr.toOptional() == wasm.code_section_index) { - // Code section is allocated upon writing as they are required to be ordered - // to synchronise with the function section. - continue; - } - var offset: u32 = 0; - while (true) { - const atom = wasm.getAtomPtr(atom_index); - const symbol_loc = atom.symbolLoc(); - // Ensure we get the original symbol, so we verify the correct symbol on whether - // it is dead or not and ensure an atom is removed when dead. - // This is required as we may have parsed aliases into atoms. - const sym = switch (symbol_loc.file) { - .zig_object => wasm.zig_object.?.symbols.items[@intFromEnum(symbol_loc.index)], - .none => wasm.synthetic_symbols.items[@intFromEnum(symbol_loc.index)], - _ => wasm.objects.items[@intFromEnum(symbol_loc.file)].symtable[@intFromEnum(symbol_loc.index)], - }; + const Formatter = struct { + wasm: *const Wasm, + ft: FunctionType, + + pub fn format( + self: Formatter, + comptime format_string: []const u8, + options: std.fmt.FormatOptions, + writer: anytype, + ) !void { + if (format_string.len != 0) std.fmt.invalidFmtError(format_string, self); + _ = options; + const params = self.ft.params.slice(self.wasm); + const returns = self.ft.returns.slice(self.wasm); - // Dead symbols must be unlinked from the linked-list to prevent them - // from being emit into the binary. - if (sym.isDead()) { - if (entry.value_ptr.* == atom_index and atom.prev != .null) { - // When the atom is dead and is also the first atom retrieved from wasm.atoms(index) we update - // the entry to point it to the previous atom to ensure we do not start with a dead symbol that - // was removed and therefore do not emit any code at all. - entry.value_ptr.* = atom.prev; + try writer.writeByte('('); + for (params, 0..) |param, i| { + try writer.print("{s}", .{@tagName(param)}); + if (i + 1 != params.len) { + try writer.writeAll(", "); + } + } + try writer.writeAll(") -> "); + if (returns.len == 0) { + try writer.writeAll("nil"); + } else { + for (returns, 0..) |return_ty, i| { + try writer.print("{s}", .{@tagName(return_ty)}); + if (i + 1 != returns.len) { + try writer.writeAll(", "); + } } - if (atom.prev == .null) break; - atom_index = atom.prev; - atom.prev = .null; - continue; } - offset = @intCast(atom.alignment.forward(offset)); - atom.offset = offset; - log.debug("Atom '{s}' allocated from 0x{x:0>8} to 0x{x:0>8} size={d}", .{ - wasm.symbolLocName(symbol_loc), - offset, - offset + atom.size, - atom.size, - }); - offset += atom.size; - if (atom.prev == .null) break; - atom_index = atom.prev; } - segment.size = @intCast(segment.alignment.forward(offset)); - } -} + }; +}; -/// For each data symbol, sets the virtual address. -fn allocateVirtualAddresses(wasm: *Wasm) void { - for (wasm.resolved_symbols.keys()) |loc| { - const symbol = wasm.symbolLocSymbol(loc); - if (symbol.tag != .data or symbol.isDead()) { - // Only data symbols have virtual addresses. - // Dead symbols do not get allocated, so we don't need to set their virtual address either. - continue; - } - const atom_index = wasm.symbol_atom.get(loc) orelse { - // synthetic symbol that does not contain an atom - continue; - }; +/// Represents a function entry, holding the index to its type +pub const Func = extern struct { + type_index: FunctionType.Index, +}; - const atom = wasm.getAtom(atom_index); - const merge_segment = wasm.base.comp.config.output_mode != .Obj; - const segment_info = switch (atom.file) { - .zig_object => wasm.zig_object.?.segment_info.items, - .none => wasm.segment_info.values(), - _ => wasm.objects.items[@intFromEnum(atom.file)].segment_info, - }; - const segment_name = segment_info[symbol.index].outputName(merge_segment); - const segment_index = wasm.data_segments.get(segment_name).?; - const segment = wasm.segmentPtr(segment_index); - - // TLS symbols have their virtual address set relative to their own TLS segment, - // rather than the entire Data section. - if (symbol.hasFlag(.WASM_SYM_TLS)) { - symbol.virtual_address = atom.offset; - } else { - symbol.virtual_address = atom.offset + segment.offset; - } - } -} +/// Type reflection is used on the field names to autopopulate each field +/// during initialization. +const PreloadedStrings = struct { + __heap_base: String, + __heap_end: String, + __indirect_function_table: String, + __linear_memory: String, + __stack_pointer: String, + __tls_align: String, + __tls_base: String, + __tls_size: String, + __wasm_apply_global_tls_relocs: String, + __wasm_call_ctors: String, + __wasm_init_memory: String, + __wasm_init_memory_flag: String, + __wasm_init_tls: String, + __zig_error_names: String, + __zig_error_name_table: String, + __zig_errors_len: String, + _initialize: String, + _start: String, + memory: String, +}; -fn sortDataSegments(wasm: *Wasm) !void { - const gpa = wasm.base.comp.gpa; - var new_mapping: std.StringArrayHashMapUnmanaged(Segment.Index) = .empty; - try new_mapping.ensureUnusedCapacity(gpa, wasm.data_segments.count()); - errdefer new_mapping.deinit(gpa); +/// Index into string_bytes +pub const String = enum(u32) { + _, + + const Table = std.HashMapUnmanaged(String, void, TableContext, std.hash_map.default_max_load_percentage); - const keys = try gpa.dupe([]const u8, wasm.data_segments.keys()); - defer gpa.free(keys); + const TableContext = struct { + bytes: []const u8, - const SortContext = struct { - fn sort(_: void, lhs: []const u8, rhs: []const u8) bool { - return order(lhs) < order(rhs); + pub fn eql(_: @This(), a: String, b: String) bool { + return a == b; } - fn order(name: []const u8) u8 { - if (mem.startsWith(u8, name, ".rodata")) return 0; - if (mem.startsWith(u8, name, ".data")) return 1; - if (mem.startsWith(u8, name, ".text")) return 2; - return 3; + pub fn hash(ctx: @This(), key: String) u64 { + return std.hash_map.hashString(mem.sliceTo(ctx.bytes[@intFromEnum(key)..], 0)); } }; - mem.sort([]const u8, keys, {}, SortContext.sort); - for (keys) |key| { - const segment_index = wasm.data_segments.get(key).?; - new_mapping.putAssumeCapacity(key, segment_index); - } - wasm.data_segments.deinit(gpa); - wasm.data_segments = new_mapping; -} + const TableIndexAdapter = struct { + bytes: []const u8, -/// Obtains all initfuncs from each object file, verifies its function signature, -/// and then appends it to our final `init_funcs` list. -/// After all functions have been inserted, the functions will be ordered based -/// on their priority. -/// NOTE: This function must be called before we merged any other section. -/// This is because all init funcs in the object files contain references to the -/// original functions and their types. We need to know the type to verify it doesn't -/// contain any parameters. -fn setupInitFunctions(wasm: *Wasm) !void { - const gpa = wasm.base.comp.gpa; - const diags = &wasm.base.comp.link_diags; - // There's no constructors for Zig so we can simply search through linked object files only. - for (wasm.objects.items, 0..) |*object, object_index| { - try wasm.init_funcs.ensureUnusedCapacity(gpa, object.init_funcs.len); - for (object.init_funcs) |init_func| { - const symbol = object.symtable[init_func.symbol_index]; - const ty: std.wasm.Type = if (symbol.isUndefined()) ty: { - const imp: Import = object.findImport(symbol); - break :ty object.func_types[imp.kind.function]; - } else ty: { - const func_index = symbol.index - object.imported_functions_count; - const func = object.functions[func_index]; - break :ty object.func_types[func.type_index]; - }; - if (ty.params.len != 0) { - var err = try diags.addErrorWithNotes(0); - try err.addMsg("constructor functions cannot take arguments: '{s}'", .{wasm.stringSlice(symbol.name)}); - } - log.debug("appended init func '{s}'\n", .{wasm.stringSlice(symbol.name)}); - wasm.init_funcs.appendAssumeCapacity(.{ - .index = @enumFromInt(init_func.symbol_index), - .file = @enumFromInt(object_index), - .priority = init_func.priority, - }); - try wasm.mark(.{ - .index = @enumFromInt(init_func.symbol_index), - .file = @enumFromInt(object_index), - }); + pub fn eql(ctx: @This(), a: []const u8, b: String) bool { + return mem.eql(u8, a, mem.sliceTo(ctx.bytes[@intFromEnum(b)..], 0)); } - } - // sort the initfunctions based on their priority - mem.sort(InitFuncLoc, wasm.init_funcs.items, {}, InitFuncLoc.lessThan); + pub fn hash(_: @This(), adapted_key: []const u8) u64 { + assert(mem.indexOfScalar(u8, adapted_key, 0) == null); + return std.hash_map.hashString(adapted_key); + } + }; - if (wasm.init_funcs.items.len > 0) { - const loc = wasm.globals.get(wasm.preloaded_strings.__wasm_call_ctors).?; - try wasm.mark(loc); + pub fn slice(index: String, wasm: *const Wasm) [:0]const u8 { + const start_slice = wasm.string_bytes.items[@intFromEnum(index)..]; + return start_slice[0..mem.indexOfScalar(u8, start_slice, 0).? :0]; } -} -/// Creates a function body for the `__wasm_call_ctors` symbol. -/// Loops over all constructors found in `init_funcs` and calls them -/// respectively based on their priority which was sorted by `setupInitFunctions`. -/// NOTE: This function must be called after we merged all sections to ensure the -/// references to the function stored in the symbol have been finalized so we end -/// up calling the resolved function. -fn initializeCallCtorsFunction(wasm: *Wasm) !void { - const gpa = wasm.base.comp.gpa; - // No code to emit, so also no ctors to call - if (wasm.code_section_index == .none) { - // Make sure to remove it from the resolved symbols so we do not emit - // it within any section. TODO: Remove this once we implement garbage collection. - const loc = wasm.globals.get(wasm.preloaded_strings.__wasm_call_ctors).?; - assert(wasm.resolved_symbols.swapRemove(loc)); - return; + pub fn toOptional(i: String) OptionalString { + const result: OptionalString = @enumFromInt(@intFromEnum(i)); + assert(result != .none); + return result; } +}; - var function_body = std.ArrayList(u8).init(gpa); - defer function_body.deinit(); - const writer = function_body.writer(); +pub const OptionalString = enum(u32) { + none = std.math.maxInt(u32), + _, - // Create the function body - { - // Write locals count (we have none) - try leb.writeUleb128(writer, @as(u32, 0)); + pub fn unwrap(i: OptionalString) ?String { + if (i == .none) return null; + return @enumFromInt(@intFromEnum(i)); + } - // call constructors - for (wasm.init_funcs.items) |init_func_loc| { - const symbol = init_func_loc.getSymbol(wasm); - const func = wasm.functions.values()[symbol.index - wasm.imported_functions_count].func; - const ty = wasm.func_types.items[func.type_index]; + pub fn slice(index: OptionalString, wasm: *const Wasm) ?[:0]const u8 { + return (index.unwrap() orelse return null).slice(wasm); + } +}; - // Call function by its function index - try writer.writeByte(std.wasm.opcode(.call)); - try leb.writeUleb128(writer, symbol.index); +/// Stored identically to `String`. The bytes are reinterpreted as +/// `std.wasm.Valtype` elements. +pub const ValtypeList = enum(u32) { + _, - // drop all returned values from the stack as __wasm_call_ctors has no return value - for (ty.returns) |_| { - try writer.writeByte(std.wasm.opcode(.drop)); - } - } + pub fn fromString(s: String) ValtypeList { + return @enumFromInt(@intFromEnum(s)); + } - // End function body - try writer.writeByte(std.wasm.opcode(.end)); + pub fn slice(index: ValtypeList, wasm: *const Wasm) []const std.wasm.Valtype { + return @ptrCast(String.slice(@enumFromInt(@intFromEnum(index)), wasm)); } +}; - try wasm.createSyntheticFunction( - wasm.preloaded_strings.__wasm_call_ctors, - std.wasm.Type{ .params = &.{}, .returns = &.{} }, - &function_body, - ); -} +/// Index into `Wasm.imports`. +pub const ZcuImportIndex = enum(u32) { + _, -fn createSyntheticFunction( - wasm: *Wasm, - symbol_name: String, - func_ty: std.wasm.Type, - function_body: *std.ArrayList(u8), -) !void { - const gpa = wasm.base.comp.gpa; - const loc = wasm.globals.get(symbol_name).?; - const symbol = wasm.symbolLocSymbol(loc); - if (symbol.isDead()) { - return; + pub fn ptr(index: ZcuImportIndex, wasm: *const Wasm) *InternPool.Nav.Index { + return &wasm.imports.keys()[@intFromEnum(index)]; } - const ty_index = try wasm.putOrGetFuncType(func_ty); - // create function with above type - const func_index = wasm.imported_functions_count + @as(u32, @intCast(wasm.functions.count())); - try wasm.functions.putNoClobber( - gpa, - .{ .file = .none, .index = func_index }, - .{ .func = .{ .type_index = ty_index }, .sym_index = loc.index }, - ); - symbol.index = func_index; - - // create the atom that will be output into the final binary - const atom_index = try wasm.createAtom(loc.index, .none); - const atom = wasm.getAtomPtr(atom_index); - atom.size = @intCast(function_body.items.len); - atom.code = function_body.moveToUnmanaged(); - try wasm.appendAtomAtIndex(wasm.code_section_index.unwrap().?, atom_index); -} -/// Unlike `createSyntheticFunction` this function is to be called by -/// the codegeneration backend. This will not allocate the created Atom yet. -/// Returns the index of the symbol. -pub fn createFunction( - wasm: *Wasm, - symbol_name: []const u8, - func_ty: std.wasm.Type, - function_body: *std.ArrayList(u8), - relocations: *std.ArrayList(Relocation), -) !Symbol.Index { - return wasm.zig_object.?.createFunction(wasm, symbol_name, func_ty, function_body, relocations); -} - -/// If required, sets the function index in the `start` section. -fn setupStartSection(wasm: *Wasm) !void { - if (wasm.globals.get(wasm.preloaded_strings.__wasm_init_memory)) |loc| { - wasm.entry = wasm.symbolLocSymbol(loc).index; + pub fn importName(index: ZcuImportIndex, wasm: *const Wasm) String { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const nav_index = index.ptr(wasm).*; + const ext = ip.getNav(nav_index).getResolvedExtern(ip).?; + const name_slice = ext.name.toSlice(ip); + return wasm.getExistingString(name_slice).?; } -} -fn initializeTLSFunction(wasm: *Wasm) !void { - const comp = wasm.base.comp; - const gpa = comp.gpa; - const shared_memory = comp.config.shared_memory; + pub fn moduleName(index: ZcuImportIndex, wasm: *const Wasm) OptionalString { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const nav_index = index.ptr(wasm).*; + const ext = ip.getNav(nav_index).getResolvedExtern(ip).?; + const lib_name = ext.lib_name.toSlice(ip) orelse return .none; + return wasm.getExistingString(lib_name).?.toOptional(); + } - if (!shared_memory) return; + pub fn functionType(index: ZcuImportIndex, wasm: *Wasm) FunctionType.Index { + const comp = wasm.base.comp; + const target = &comp.root_mod.resolved_target.result; + const zcu = comp.zcu.?; + const ip = &zcu.intern_pool; + const nav_index = index.ptr(wasm).*; + const ext = ip.getNav(nav_index).getResolvedExtern(ip).?; + const fn_info = zcu.typeToFunc(.fromInterned(ext.ty)).?; + return getExistingFunctionType(wasm, fn_info.cc, fn_info.param_types.get(ip), .fromInterned(fn_info.return_type), target).?; + } - // ensure function is marked as we must emit it - wasm.symbolLocSymbol(wasm.globals.get(wasm.preloaded_strings.__wasm_init_tls).?).mark(); + pub fn globalType(index: ZcuImportIndex, wasm: *const Wasm) ObjectGlobal.Type { + _ = index; + _ = wasm; + unreachable; // Zig has no way to create Wasm globals yet. + } +}; - var function_body = std.ArrayList(u8).init(gpa); - defer function_body.deinit(); - const writer = function_body.writer(); +/// 0. Index into `Wasm.object_function_imports`. +/// 1. Index into `Wasm.imports`. +pub const FunctionImportId = enum(u32) { + _, - // locals - try writer.writeByte(0); + pub const Unpacked = union(enum) { + object_function_import: FunctionImport.Index, + zcu_import: ZcuImportIndex, + }; - // If there's a TLS segment, initialize it during runtime using the bulk-memory feature - if (wasm.data_segments.getIndex(".tdata")) |data_index| { - const segment_index = wasm.data_segments.entries.items(.value)[data_index]; - const segment = wasm.segmentPtr(segment_index); + pub fn pack(unpacked: Unpacked, wasm: *const Wasm) FunctionImportId { + return switch (unpacked) { + .object_function_import => |i| @enumFromInt(@intFromEnum(i)), + .zcu_import => |i| @enumFromInt(@intFromEnum(i) + wasm.object_function_imports.entries.len), + }; + } - const param_local: u32 = 0; + pub fn unpack(id: FunctionImportId, wasm: *const Wasm) Unpacked { + const i = @intFromEnum(id); + if (i < wasm.object_function_imports.entries.len) return .{ .object_function_import = @enumFromInt(i) }; + const zcu_import_i = i - wasm.object_function_imports.entries.len; + return .{ .zcu_import = @enumFromInt(zcu_import_i) }; + } - try writer.writeByte(std.wasm.opcode(.local_get)); - try leb.writeUleb128(writer, param_local); + pub fn fromObject(function_import_index: FunctionImport.Index, wasm: *const Wasm) FunctionImportId { + return pack(.{ .object_function_import = function_import_index }, wasm); + } - const tls_base_loc = wasm.globals.get(wasm.preloaded_strings.__tls_base).?; - try writer.writeByte(std.wasm.opcode(.global_set)); - try leb.writeUleb128(writer, wasm.symbolLocSymbol(tls_base_loc).index); + pub fn fromZcuImport(zcu_import: ZcuImportIndex, wasm: *const Wasm) FunctionImportId { + return pack(.{ .zcu_import = zcu_import }, wasm); + } - // load stack values for the bulk-memory operation - { - try writer.writeByte(std.wasm.opcode(.local_get)); - try leb.writeUleb128(writer, param_local); + /// This function is allowed O(N) lookup because it is only called during + /// diagnostic generation. + pub fn sourceLocation(id: FunctionImportId, wasm: *const Wasm) SourceLocation { + switch (id.unpack(wasm)) { + .object_function_import => |obj_func_index| { + // TODO binary search + for (wasm.objects.items, 0..) |o, i| { + if (o.function_imports.off <= @intFromEnum(obj_func_index) and + o.function_imports.off + o.function_imports.len > @intFromEnum(obj_func_index)) + { + return .pack(.{ .object_index = @enumFromInt(i) }, wasm); + } + } else unreachable; + }, + .zcu_import => return .zig_object_nofile, // TODO give a better source location + } + } - try writer.writeByte(std.wasm.opcode(.i32_const)); - try leb.writeUleb128(writer, @as(u32, 0)); //segment offset + pub fn importName(id: FunctionImportId, wasm: *const Wasm) String { + return switch (unpack(id, wasm)) { + inline .object_function_import, .zcu_import => |i| i.importName(wasm), + }; + } - try writer.writeByte(std.wasm.opcode(.i32_const)); - try leb.writeUleb128(writer, @as(u32, segment.size)); //segment offset - } + pub fn moduleName(id: FunctionImportId, wasm: *const Wasm) OptionalString { + return switch (unpack(id, wasm)) { + inline .object_function_import, .zcu_import => |i| i.moduleName(wasm), + }; + } - // perform the bulk-memory operation to initialize the data segment - try writer.writeByte(std.wasm.opcode(.misc_prefix)); - try leb.writeUleb128(writer, std.wasm.miscOpcode(.memory_init)); - // segment immediate - try leb.writeUleb128(writer, @as(u32, @intCast(data_index))); - // memory index immediate (always 0) - try leb.writeUleb128(writer, @as(u32, 0)); + pub fn functionType(id: FunctionImportId, wasm: *Wasm) FunctionType.Index { + return switch (unpack(id, wasm)) { + inline .object_function_import, .zcu_import => |i| i.functionType(wasm), + }; } - // If we have to perform any TLS relocations, call the corresponding function - // which performs all runtime TLS relocations. This is a synthetic function, - // generated by the linker. - if (wasm.globals.get(wasm.preloaded_strings.__wasm_apply_global_tls_relocs)) |loc| { - try writer.writeByte(std.wasm.opcode(.call)); - try leb.writeUleb128(writer, wasm.symbolLocSymbol(loc).index); - wasm.symbolLocSymbol(loc).mark(); + /// Asserts not emitting an object, and `Wasm.import_symbols` is false. + pub fn undefinedAllowed(id: FunctionImportId, wasm: *const Wasm) bool { + assert(!wasm.import_symbols); + assert(wasm.base.comp.config.output_mode != .Obj); + return switch (unpack(id, wasm)) { + .object_function_import => |i| { + const import = i.value(wasm); + return import.flags.binding == .strong and import.module_name != .none; + }, + .zcu_import => |i| { + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const ext = ip.getNav(i.ptr(wasm).*).getResolvedExtern(ip).?; + return !ext.is_weak_linkage and ext.lib_name != .none; + }, + }; } +}; - try writer.writeByte(std.wasm.opcode(.end)); +/// 0. Index into `object_global_imports`. +/// 1. Index into `imports`. +pub const GlobalImportId = enum(u32) { + _, - try wasm.createSyntheticFunction( - wasm.preloaded_strings.__wasm_init_tls, - std.wasm.Type{ .params = &.{.i32}, .returns = &.{} }, - &function_body, - ); -} + pub const Unpacked = union(enum) { + object_global_import: GlobalImport.Index, + zcu_import: ZcuImportIndex, + }; -fn setupImports(wasm: *Wasm) !void { - const gpa = wasm.base.comp.gpa; - log.debug("Merging imports", .{}); - for (wasm.resolved_symbols.keys()) |symbol_loc| { - const object_id = symbol_loc.file.unwrap() orelse { - // Synthetic symbols will already exist in the `import` section - continue; + pub fn pack(unpacked: Unpacked, wasm: *const Wasm) GlobalImportId { + return switch (unpacked) { + .object_global_import => |i| @enumFromInt(@intFromEnum(i)), + .zcu_import => |i| @enumFromInt(@intFromEnum(i) + wasm.object_global_imports.entries.len), }; + } - const symbol = wasm.symbolLocSymbol(symbol_loc); - if (symbol.isDead()) continue; - if (!symbol.requiresImport()) continue; - if (symbol.name == wasm.preloaded_strings.__indirect_function_table) continue; + pub fn unpack(id: GlobalImportId, wasm: *const Wasm) Unpacked { + const i = @intFromEnum(id); + if (i < wasm.object_global_imports.entries.len) return .{ .object_global_import = @enumFromInt(i) }; + const zcu_import_i = i - wasm.object_global_imports.entries.len; + return .{ .zcu_import = @enumFromInt(zcu_import_i) }; + } - log.debug("Symbol '{s}' will be imported from the host", .{wasm.stringSlice(symbol.name)}); - const import = objectImport(wasm, object_id, symbol_loc.index); + pub fn fromObject(object_global_import: GlobalImport.Index, wasm: *const Wasm) GlobalImportId { + return pack(.{ .object_global_import = object_global_import }, wasm); + } - // We copy the import to a new import to ensure the names contain references - // to the internal string table, rather than of the object file. - const new_imp: Import = .{ - .module_name = import.module_name, - .name = import.name, - .kind = import.kind, - }; - // TODO: De-duplicate imports when they contain the same names and type - try wasm.imports.putNoClobber(gpa, symbol_loc, new_imp); - } - - // Assign all indexes of the imports to their representing symbols - var function_index: u32 = 0; - var global_index: u32 = 0; - var table_index: u32 = 0; - var it = wasm.imports.iterator(); - while (it.next()) |entry| { - const symbol = wasm.symbolLocSymbol(entry.key_ptr.*); - const import: Import = entry.value_ptr.*; - switch (import.kind) { - .function => { - symbol.index = function_index; - function_index += 1; - }, - .global => { - symbol.index = global_index; - global_index += 1; - }, - .table => { - symbol.index = table_index; - table_index += 1; + /// This function is allowed O(N) lookup because it is only called during + /// diagnostic generation. + pub fn sourceLocation(id: GlobalImportId, wasm: *const Wasm) SourceLocation { + switch (id.unpack(wasm)) { + .object_global_import => |obj_global_index| { + // TODO binary search + for (wasm.objects.items, 0..) |o, i| { + if (o.global_imports.off <= @intFromEnum(obj_global_index) and + o.global_imports.off + o.global_imports.len > @intFromEnum(obj_global_index)) + { + return .pack(.{ .object_index = @enumFromInt(i) }, wasm); + } + } else unreachable; }, - else => unreachable, + .zcu_import => return .zig_object_nofile, // TODO give a better source location } } - wasm.imported_functions_count = function_index; - wasm.imported_globals_count = global_index; - wasm.imported_tables_count = table_index; - log.debug("Merged ({d}) functions, ({d}) globals, and ({d}) tables into import section", .{ - function_index, - global_index, - table_index, - }); -} + pub fn importName(id: GlobalImportId, wasm: *const Wasm) String { + return switch (unpack(id, wasm)) { + inline .object_global_import, .zcu_import => |i| i.importName(wasm), + }; + } -/// Takes the global, function and table section from each linked object file -/// and merges it into a single section for each. -fn mergeSections(wasm: *Wasm) !void { - const gpa = wasm.base.comp.gpa; + pub fn moduleName(id: GlobalImportId, wasm: *const Wasm) OptionalString { + return switch (unpack(id, wasm)) { + inline .object_global_import, .zcu_import => |i| i.moduleName(wasm), + }; + } + + pub fn globalType(id: GlobalImportId, wasm: *Wasm) ObjectGlobal.Type { + return switch (unpack(id, wasm)) { + inline .object_global_import, .zcu_import => |i| i.globalType(wasm), + }; + } +}; + +/// 0. Index into `Wasm.object_data_imports`. +/// 1. Index into `Wasm.imports`. +pub const DataImportId = enum(u32) { + _, - var removed_duplicates = std.ArrayList(SymbolLoc).init(gpa); - defer removed_duplicates.deinit(); + pub const Unpacked = union(enum) { + object_data_import: ObjectDataImport.Index, + zcu_import: ZcuImportIndex, + }; - for (wasm.resolved_symbols.keys()) |sym_loc| { - const object_id = sym_loc.file.unwrap() orelse { - // Synthetic symbols already live in the corresponding sections. - continue; + pub fn pack(unpacked: Unpacked, wasm: *const Wasm) DataImportId { + return switch (unpacked) { + .object_data_import => |i| @enumFromInt(@intFromEnum(i)), + .zcu_import => |i| @enumFromInt(@intFromEnum(i) + wasm.object_data_imports.entries.len), }; + } - const symbol = objectSymbol(wasm, object_id, sym_loc.index); - if (symbol.isDead() or symbol.isUndefined()) { - // Skip undefined symbols as they go in the `import` section - continue; - } - - switch (symbol.tag) { - .function => { - const gop = try wasm.functions.getOrPut( - gpa, - .{ .file = sym_loc.file, .index = symbol.index }, - ); - if (gop.found_existing) { - // We found an alias to the same function, discard this symbol in favor of - // the original symbol and point the discard function to it. This ensures - // we only emit a single function, instead of duplicates. - // we favor keeping the global over a local. - const original_loc: SymbolLoc = .{ .file = gop.key_ptr.file, .index = gop.value_ptr.sym_index }; - const original_sym = wasm.symbolLocSymbol(original_loc); - if (original_sym.isLocal() and symbol.isGlobal()) { - original_sym.unmark(); - try wasm.discarded.put(gpa, original_loc, sym_loc); - try removed_duplicates.append(original_loc); - } else { - symbol.unmark(); - try wasm.discarded.putNoClobber(gpa, sym_loc, original_loc); - try removed_duplicates.append(sym_loc); - continue; + pub fn unpack(id: DataImportId, wasm: *const Wasm) Unpacked { + const i = @intFromEnum(id); + if (i < wasm.object_data_imports.entries.len) return .{ .object_data_import = @enumFromInt(i) }; + const zcu_import_i = i - wasm.object_data_imports.entries.len; + return .{ .zcu_import = @enumFromInt(zcu_import_i) }; + } + + pub fn fromZcuImport(zcu_import: ZcuImportIndex, wasm: *const Wasm) DataImportId { + return pack(.{ .zcu_import = zcu_import }, wasm); + } + + pub fn fromObject(object_data_import: ObjectDataImport.Index, wasm: *const Wasm) DataImportId { + return pack(.{ .object_data_import = object_data_import }, wasm); + } + + pub fn sourceLocation(id: DataImportId, wasm: *const Wasm) SourceLocation { + switch (id.unpack(wasm)) { + .object_data_import => |obj_data_index| { + // TODO binary search + for (wasm.objects.items, 0..) |o, i| { + if (o.data_imports.off <= @intFromEnum(obj_data_index) and + o.data_imports.off + o.data_imports.len > @intFromEnum(obj_data_index)) + { + return .pack(.{ .object_index = @enumFromInt(i) }, wasm); } - } - gop.value_ptr.* = .{ - .func = objectFunction(wasm, object_id, sym_loc.index), - .sym_index = sym_loc.index, - }; - symbol.index = @as(u32, @intCast(gop.index)) + wasm.imported_functions_count; + } else unreachable; }, - .global => { - const index = symbol.index - objectImportedFunctions(wasm, object_id); - const original_global = objectGlobals(wasm, object_id)[index]; - symbol.index = @as(u32, @intCast(wasm.wasm_globals.items.len)) + wasm.imported_globals_count; - try wasm.wasm_globals.append(gpa, original_global); - }, - .table => { - const index = symbol.index - objectImportedFunctions(wasm, object_id); - // assert it's a regular relocatable object file as `ZigObject` will never - // contain a table. - const original_table = wasm.objectById(object_id).?.tables[index]; - symbol.index = @as(u32, @intCast(wasm.tables.items.len)) + wasm.imported_tables_count; - try wasm.tables.append(gpa, original_table); - }, - .dead, .undefined => unreachable, - else => {}, + .zcu_import => return .zig_object_nofile, // TODO give a better source location } } +}; - // For any removed duplicates, remove them from the resolved symbols list - for (removed_duplicates.items) |sym_loc| { - assert(wasm.resolved_symbols.swapRemove(sym_loc)); - gc_log.debug("Removed duplicate for function '{s}'", .{wasm.symbolLocName(sym_loc)}); +/// Index into `Wasm.symbol_table`. +pub const SymbolTableIndex = enum(u32) { + _, + + pub fn key(i: @This(), wasm: *const Wasm) *String { + return &wasm.symbol_table.keys()[@intFromEnum(i)]; } +}; - log.debug("Merged ({d}) functions", .{wasm.functions.count()}); - log.debug("Merged ({d}) globals", .{wasm.wasm_globals.items.len}); - log.debug("Merged ({d}) tables", .{wasm.tables.items.len}); -} +pub const OutReloc = struct { + tag: Object.RelocationType, + offset: u32, + pointee: Pointee, + addend: i32, -/// Merges function types of all object files into the final -/// 'types' section, while assigning the type index to the representing -/// section (import, export, function). -fn mergeTypes(wasm: *Wasm) !void { - const gpa = wasm.base.comp.gpa; - // A map to track which functions have already had their - // type inserted. If we do this for the same function multiple times, - // it will be overwritten with the incorrect type. - var dirty = std.AutoHashMap(u32, void).init(gpa); - try dirty.ensureUnusedCapacity(@as(u32, @intCast(wasm.functions.count()))); - defer dirty.deinit(); - - for (wasm.resolved_symbols.keys()) |sym_loc| { - const object_id = sym_loc.file.unwrap() orelse { - // zig code-generated symbols are already present in final type section - continue; - }; + pub const Pointee = union { + symbol_index: SymbolTableIndex, + type_index: FunctionType.Index, + }; - const symbol = objectSymbol(wasm, object_id, sym_loc.index); - if (symbol.tag != .function or symbol.isDead()) { - // Only functions have types. Only retrieve the type of referenced functions. - continue; + pub const Slice = extern struct { + /// Index into `out_relocs`. + off: u32, + len: u32, + + pub fn slice(s: Slice, wasm: *const Wasm) []OutReloc { + return wasm.relocations.items[s.off..][0..s.len]; } + }; +}; - if (symbol.isUndefined()) { - log.debug("Adding type from extern function '{s}'", .{wasm.symbolLocName(sym_loc)}); - const import: *Import = wasm.imports.getPtr(sym_loc) orelse continue; - const original_type = objectFuncTypes(wasm, object_id)[import.kind.function]; - import.kind.function = try wasm.putOrGetFuncType(original_type); - } else if (!dirty.contains(symbol.index)) { - log.debug("Adding type from function '{s}'", .{wasm.symbolLocName(sym_loc)}); - const func = &wasm.functions.values()[symbol.index - wasm.imported_functions_count].func; - func.type_index = try wasm.putOrGetFuncType(objectFuncTypes(wasm, object_id)[func.type_index]); - dirty.putAssumeCapacityNoClobber(symbol.index, {}); +pub const ObjectRelocation = struct { + tag: Tag, + /// Offset of the value to rewrite relative to the relevant section's contents. + /// When `offset` is zero, its position is immediately after the id and size of the section. + offset: u32, + pointee: Pointee, + /// Populated only for `memory_addr_*`, `function_offset_i32` and `section_offset_i32`. + addend: i32, + + pub const Tag = enum(u8) { + // These use `Pointee.function`. + function_index_i32, + function_index_leb, + function_offset_i32, + function_offset_i64, + table_index_i32, + table_index_i64, + table_index_rel_sleb, + table_index_rel_sleb64, + table_index_sleb, + table_index_sleb64, + // These use `Pointee.symbol_name`. + function_import_index_i32, + function_import_index_leb, + function_import_offset_i32, + function_import_offset_i64, + table_import_index_i32, + table_import_index_i64, + table_import_index_rel_sleb, + table_import_index_rel_sleb64, + table_import_index_sleb, + table_import_index_sleb64, + // These use `Pointee.global`. + global_index_i32, + global_index_leb, + // These use `Pointee.symbol_name`. + global_import_index_i32, + global_import_index_leb, + // These use `Pointee.data`. + memory_addr_i32, + memory_addr_i64, + memory_addr_leb, + memory_addr_leb64, + memory_addr_locrel_i32, + memory_addr_rel_sleb, + memory_addr_rel_sleb64, + memory_addr_sleb, + memory_addr_sleb64, + memory_addr_tls_sleb, + memory_addr_tls_sleb64, + // These use `Pointee.symbol_name`. + memory_addr_import_i32, + memory_addr_import_i64, + memory_addr_import_leb, + memory_addr_import_leb64, + memory_addr_import_locrel_i32, + memory_addr_import_rel_sleb, + memory_addr_import_rel_sleb64, + memory_addr_import_sleb, + memory_addr_import_sleb64, + memory_addr_import_tls_sleb, + memory_addr_import_tls_sleb64, + /// Uses `Pointee.section`. + section_offset_i32, + /// Uses `Pointee.table`. + table_number_leb, + /// Uses `Pointee.symbol_name`. + table_import_number_leb, + /// Uses `Pointee.type_index`. + type_index_leb, + + pub fn fromType(t: Object.RelocationType) Tag { + return switch (t) { + .event_index_leb => unreachable, + .function_index_i32 => .function_index_i32, + .function_index_leb => .function_index_leb, + .function_offset_i32 => .function_offset_i32, + .function_offset_i64 => .function_offset_i64, + .global_index_i32 => .global_index_i32, + .global_index_leb => .global_index_leb, + .memory_addr_i32 => .memory_addr_i32, + .memory_addr_i64 => .memory_addr_i64, + .memory_addr_leb => .memory_addr_leb, + .memory_addr_leb64 => .memory_addr_leb64, + .memory_addr_locrel_i32 => .memory_addr_locrel_i32, + .memory_addr_rel_sleb => .memory_addr_rel_sleb, + .memory_addr_rel_sleb64 => .memory_addr_rel_sleb64, + .memory_addr_sleb => .memory_addr_sleb, + .memory_addr_sleb64 => .memory_addr_sleb64, + .memory_addr_tls_sleb => .memory_addr_tls_sleb, + .memory_addr_tls_sleb64 => .memory_addr_tls_sleb64, + .section_offset_i32 => .section_offset_i32, + .table_index_i32 => .table_index_i32, + .table_index_i64 => .table_index_i64, + .table_index_rel_sleb => .table_index_rel_sleb, + .table_index_rel_sleb64 => .table_index_rel_sleb64, + .table_index_sleb => .table_index_sleb, + .table_index_sleb64 => .table_index_sleb64, + .table_number_leb => .table_number_leb, + .type_index_leb => .type_index_leb, + }; } - } - log.debug("Completed merging and deduplicating types. Total count: ({d})", .{wasm.func_types.items.len}); -} -fn checkExportNames(wasm: *Wasm) !void { - const force_exp_names = wasm.export_symbol_names; - const diags = &wasm.base.comp.link_diags; - if (force_exp_names.len > 0) { - var failed_exports = false; - - for (force_exp_names) |exp_name| { - const exp_name_interned = try wasm.internString(exp_name); - const loc = wasm.globals.get(exp_name_interned) orelse { - var err = try diags.addErrorWithNotes(0); - try err.addMsg("could not export '{s}', symbol not found", .{exp_name}); - failed_exports = true; - continue; + pub fn fromTypeImport(t: Object.RelocationType) Tag { + return switch (t) { + .event_index_leb => unreachable, + .function_index_i32 => .function_import_index_i32, + .function_index_leb => .function_import_index_leb, + .function_offset_i32 => .function_import_offset_i32, + .function_offset_i64 => .function_import_offset_i64, + .global_index_i32 => .global_import_index_i32, + .global_index_leb => .global_import_index_leb, + .memory_addr_i32 => .memory_addr_import_i32, + .memory_addr_i64 => .memory_addr_import_i64, + .memory_addr_leb => .memory_addr_import_leb, + .memory_addr_leb64 => .memory_addr_import_leb64, + .memory_addr_locrel_i32 => .memory_addr_import_locrel_i32, + .memory_addr_rel_sleb => .memory_addr_import_rel_sleb, + .memory_addr_rel_sleb64 => .memory_addr_import_rel_sleb64, + .memory_addr_sleb => .memory_addr_import_sleb, + .memory_addr_sleb64 => .memory_addr_import_sleb64, + .memory_addr_tls_sleb => .memory_addr_import_tls_sleb, + .memory_addr_tls_sleb64 => .memory_addr_import_tls_sleb64, + .section_offset_i32 => unreachable, + .table_index_i32 => .table_import_index_i32, + .table_index_i64 => .table_import_index_i64, + .table_index_rel_sleb => .table_import_index_rel_sleb, + .table_index_rel_sleb64 => .table_import_index_rel_sleb64, + .table_index_sleb => .table_import_index_sleb, + .table_index_sleb64 => .table_import_index_sleb64, + .table_number_leb => .table_import_number_leb, + .type_index_leb => unreachable, }; + } + }; - const symbol = wasm.symbolLocSymbol(loc); - symbol.setFlag(.WASM_SYM_EXPORTED); + pub const Pointee = union { + symbol_name: String, + data: ObjectData.Index, + type_index: FunctionType.Index, + section: ObjectSectionIndex, + function: ObjectFunctionIndex, + global: ObjectGlobalIndex, + table: ObjectTableIndex, + }; + + pub const Slice = extern struct { + /// Index into `relocations`. + off: u32, + len: u32, + + const empty: Slice = .{ .off = 0, .len = 0 }; + + pub fn tags(s: Slice, wasm: *const Wasm) []const ObjectRelocation.Tag { + return wasm.object_relocations.items(.tag)[s.off..][0..s.len]; } - if (failed_exports) { - return error.FlushFailure; + pub fn offsets(s: Slice, wasm: *const Wasm) []const u32 { + return wasm.object_relocations.items(.offset)[s.off..][0..s.len]; } - } -} -fn setupExports(wasm: *Wasm) !void { - const comp = wasm.base.comp; - const gpa = comp.gpa; - if (comp.config.output_mode == .Obj) return; - log.debug("Building exports from symbols", .{}); - - for (wasm.resolved_symbols.keys()) |sym_loc| { - const symbol = wasm.symbolLocSymbol(sym_loc); - if (!symbol.isExported(comp.config.rdynamic)) continue; - - const exp: Export = if (symbol.tag == .data) exp: { - const global_index = @as(u32, @intCast(wasm.imported_globals_count + wasm.wasm_globals.items.len)); - try wasm.wasm_globals.append(gpa, .{ - .global_type = .{ .valtype = .i32, .mutable = false }, - .init = .{ .i32_const = @as(i32, @intCast(symbol.virtual_address)) }, - }); - break :exp .{ - .name = symbol.name, - .kind = .global, - .index = global_index, + pub fn pointees(s: Slice, wasm: *const Wasm) []const Pointee { + return wasm.object_relocations.items(.pointee)[s.off..][0..s.len]; + } + + pub fn addends(s: Slice, wasm: *const Wasm) []const i32 { + return wasm.object_relocations.items(.addend)[s.off..][0..s.len]; + } + }; + + pub const IterableSlice = struct { + slice: Slice, + /// Offset at which point to stop iterating. + end: u32, + + const empty: IterableSlice = .{ .slice = .empty, .end = 0 }; + + fn init(relocs: Slice, offset: u32, size: u32, wasm: *const Wasm) IterableSlice { + const offsets = relocs.offsets(wasm); + const start = std.sort.lowerBound(u32, offsets, offset, order); + return .{ + .slice = .{ + .off = @intCast(relocs.off + start), + .len = @intCast(relocs.len - start), + }, + .end = offset + size, }; - } else .{ - .name = symbol.name, - .kind = symbol.tag.externalType(), - .index = symbol.index, + } + + fn order(lhs: u32, rhs: u32) std.math.Order { + return std.math.order(lhs, rhs); + } + }; +}; + +pub const MemoryImport = extern struct { + module_name: String, + limits_min: u32, + limits_max: u32, + source_location: SourceLocation, + limits_has_max: bool, + limits_is_shared: bool, + padding: [2]u8 = .{ 0, 0 }, + + pub fn limits(mi: *const MemoryImport) std.wasm.Limits { + return .{ + .flags = .{ + .has_max = mi.limits_has_max, + .is_shared = mi.limits_is_shared, + }, + .min = mi.limits_min, + .max = mi.limits_max, }; - log.debug("Exporting symbol '{s}' as '{s}' at index: ({d})", .{ - wasm.stringSlice(symbol.name), - wasm.stringSlice(exp.name), - exp.index, - }); - try wasm.exports.append(gpa, exp); } +}; - log.debug("Completed building exports. Total count: ({d})", .{wasm.exports.items.len}); -} +pub const Alignment = InternPool.Alignment; -fn setupStart(wasm: *Wasm) !void { - const comp = wasm.base.comp; - const diags = &wasm.base.comp.link_diags; - // do not export entry point if user set none or no default was set. - const entry_name = wasm.entry_name.unwrap() orelse return; - - const symbol_loc = wasm.globals.get(entry_name) orelse { - var err = try diags.addErrorWithNotes(1); - try err.addMsg("entry symbol '{s}' missing", .{wasm.stringSlice(entry_name)}); - try err.addNote("'-fno-entry' suppresses this error", .{}); - return error.LinkFailure; +pub const InitFunc = extern struct { + priority: u32, + function_index: ObjectFunctionIndex, + + pub fn lessThan(ctx: void, lhs: InitFunc, rhs: InitFunc) bool { + _ = ctx; + if (lhs.priority == rhs.priority) { + return @intFromEnum(lhs.function_index) < @intFromEnum(rhs.function_index); + } else { + return lhs.priority < rhs.priority; + } + } +}; + +pub const Comdat = struct { + name: String, + /// Must be zero, no flags are currently defined by the tool-convention. + flags: u32, + symbols: Comdat.Symbol.Slice, + + pub const Symbol = struct { + kind: Comdat.Symbol.Type, + /// Index of the data segment/function/global/event/table within a WASM module. + /// The object must not be an import. + index: u32, + + pub const Slice = struct { + /// Index into Wasm object_comdat_symbols + off: u32, + len: u32, + }; + + pub const Type = enum(u8) { + data = 0, + function = 1, + global = 2, + event = 3, + table = 4, + section = 5, + }; }; +}; - const symbol = wasm.symbolLocSymbol(symbol_loc); - if (symbol.tag != .function) - return diags.fail("entry symbol '{s}' is not a function", .{wasm.stringSlice(entry_name)}); +/// Stored as a u8 so it can reuse the string table mechanism. +pub const Feature = packed struct(u8) { + prefix: Prefix, + /// Type of the feature, must be unique in the sequence of features. + tag: Tag, - // Ensure the symbol is exported so host environment can access it - if (comp.config.output_mode != .Obj) { - symbol.setFlag(.WASM_SYM_EXPORTED); - } -} + pub const sentinel: Feature = @bitCast(@as(u8, 0)); -/// Sets up the memory section of the wasm module, as well as the stack. -fn setupMemory(wasm: *Wasm) !void { - const comp = wasm.base.comp; - const diags = &wasm.base.comp.link_diags; - const shared_memory = comp.config.shared_memory; - log.debug("Setting up memory layout", .{}); - const page_size = std.wasm.page_size; // 64kb - const stack_alignment: Alignment = .@"16"; // wasm's stack alignment as specified by tool-convention - const heap_alignment: Alignment = .@"16"; // wasm's heap alignment as specified by tool-convention - - // Always place the stack at the start by default - // unless the user specified the global-base flag - var place_stack_first = true; - var memory_ptr: u64 = if (wasm.global_base) |base| blk: { - place_stack_first = false; - break :blk base; - } else 0; + /// Stored identically to `String`. The bytes are reinterpreted as `Feature` + /// elements. Elements must be sorted before string-interning. + pub const Set = enum(u32) { + _, - const is_obj = comp.config.output_mode == .Obj; + pub fn fromString(s: String) Set { + return @enumFromInt(@intFromEnum(s)); + } - const stack_ptr = if (wasm.globals.get(wasm.preloaded_strings.__stack_pointer)) |loc| index: { - const sym = wasm.symbolLocSymbol(loc); - break :index sym.index - wasm.imported_globals_count; - } else null; + pub fn string(s: Set) String { + return @enumFromInt(@intFromEnum(s)); + } - if (place_stack_first and !is_obj) { - memory_ptr = stack_alignment.forward(memory_ptr); - memory_ptr += wasm.base.stack_size; - // We always put the stack pointer global at index 0 - if (stack_ptr) |index| { - wasm.wasm_globals.items[index].init.i32_const = @as(i32, @bitCast(@as(u32, @intCast(memory_ptr)))); + pub fn slice(s: Set, wasm: *const Wasm) [:sentinel]const Feature { + return @ptrCast(string(s).slice(wasm)); } - } + }; - var offset: u32 = @as(u32, @intCast(memory_ptr)); - var data_seg_it = wasm.data_segments.iterator(); - while (data_seg_it.next()) |entry| { - const segment = wasm.segmentPtr(entry.value_ptr.*); - memory_ptr = segment.alignment.forward(memory_ptr); + /// Unlike `std.Target.wasm.Feature` this also contains linker-features such as shared-mem. + /// Additionally the name uses convention matching the wasm binary format. + pub const Tag = enum(u6) { + atomics, + @"bulk-memory", + @"exception-handling", + @"extended-const", + @"half-precision", + multimemory, + multivalue, + @"mutable-globals", + @"nontrapping-fptoint", + @"reference-types", + @"relaxed-simd", + @"sign-ext", + simd128, + @"tail-call", + @"shared-mem", - // set TLS-related symbols - if (mem.eql(u8, entry.key_ptr.*, ".tdata")) { - if (wasm.globals.get(wasm.preloaded_strings.__tls_size)) |loc| { - const sym = wasm.symbolLocSymbol(loc); - wasm.wasm_globals.items[sym.index - wasm.imported_globals_count].init.i32_const = @intCast(segment.size); - } - if (wasm.globals.get(wasm.preloaded_strings.__tls_align)) |loc| { - const sym = wasm.symbolLocSymbol(loc); - wasm.wasm_globals.items[sym.index - wasm.imported_globals_count].init.i32_const = @intCast(segment.alignment.toByteUnits().?); - } - if (wasm.globals.get(wasm.preloaded_strings.__tls_base)) |loc| { - const sym = wasm.symbolLocSymbol(loc); - wasm.wasm_globals.items[sym.index - wasm.imported_globals_count].init.i32_const = if (shared_memory) - @as(i32, 0) - else - @as(i32, @intCast(memory_ptr)); - } + pub fn fromCpuFeature(feature: std.Target.wasm.Feature) Tag { + return @enumFromInt(@intFromEnum(feature)); } - memory_ptr += segment.size; - segment.offset = offset; - offset += segment.size; + pub fn toCpuFeature(tag: Tag) ?std.Target.wasm.Feature { + return if (@intFromEnum(tag) < @typeInfo(std.Target.wasm.Feature).@"enum".fields.len) + @enumFromInt(@intFromEnum(tag)) + else + null; + } + + pub const format = @compileError("use @tagName instead"); + }; + + /// Provides information about the usage of the feature. + pub const Prefix = enum(u2) { + /// Reserved so that a 0-byte Feature is invalid and therefore can be a sentinel. + invalid, + /// Object uses this feature, and the link fails if feature is not in + /// the allowed set. + @"+", + /// Object does not use this feature, and the link fails if this + /// feature is in the allowed set. + @"-", + /// Object uses this feature, and the link fails if this feature is not + /// in the allowed set, or if any object does not use this feature. + @"=", + }; + + pub fn format(feature: Feature, comptime fmt: []const u8, opt: std.fmt.FormatOptions, writer: anytype) !void { + _ = opt; + _ = fmt; + try writer.print("{s} {s}", .{ @tagName(feature.prefix), @tagName(feature.tag) }); } - // create the memory init flag which is used by the init memory function - if (shared_memory and wasm.hasPassiveInitializationSegments()) { - // align to pointer size - memory_ptr = mem.alignForward(u64, memory_ptr, 4); - const loc = try wasm.createSyntheticSymbol(wasm.preloaded_strings.__wasm_init_memory_flag, .data); - const sym = wasm.symbolLocSymbol(loc); - sym.mark(); - sym.virtual_address = @as(u32, @intCast(memory_ptr)); - memory_ptr += 4; + pub fn lessThan(_: void, a: Feature, b: Feature) bool { + assert(a != b); + const a_int: u8 = @bitCast(a); + const b_int: u8 = @bitCast(b); + return a_int < b_int; } +}; - if (!place_stack_first and !is_obj) { - memory_ptr = stack_alignment.forward(memory_ptr); - memory_ptr += wasm.base.stack_size; - if (stack_ptr) |index| { - wasm.wasm_globals.items[index].init.i32_const = @as(i32, @bitCast(@as(u32, @intCast(memory_ptr)))); - } +pub fn open( + arena: Allocator, + comp: *Compilation, + emit: Path, + options: link.File.OpenOptions, +) !*Wasm { + // TODO: restore saved linker state, don't truncate the file, and + // participate in incremental compilation. + return createEmpty(arena, comp, emit, options); +} + +pub fn createEmpty( + arena: Allocator, + comp: *Compilation, + emit: Path, + options: link.File.OpenOptions, +) !*Wasm { + const target = comp.root_mod.resolved_target.result; + assert(target.ofmt == .wasm); + + const use_lld = build_options.have_llvm and comp.config.use_lld; + const use_llvm = comp.config.use_llvm; + const output_mode = comp.config.output_mode; + const wasi_exec_model = comp.config.wasi_exec_model; + + // If using LLD to link, this code should produce an object file so that it + // can be passed to LLD. + // If using LLVM to generate the object file for the zig compilation unit, + // we need a place to put the object file so that it can be subsequently + // handled. + const zcu_object_sub_path = if (!use_lld and !use_llvm) + null + else + try std.fmt.allocPrint(arena, "{s}.o", .{emit.sub_path}); + + const wasm = try arena.create(Wasm); + wasm.* = .{ + .base = .{ + .tag = .wasm, + .comp = comp, + .emit = emit, + .zcu_object_sub_path = zcu_object_sub_path, + // Garbage collection is so crucial to WebAssembly that we design + // the linker around the assumption that it will be on in the vast + // majority of cases, and therefore express "no garbage collection" + // in terms of setting the no_strip and must_link flags on all + // symbols. + .gc_sections = options.gc_sections orelse (output_mode != .Obj), + .print_gc_sections = options.print_gc_sections, + .stack_size = options.stack_size orelse switch (target.os.tag) { + .freestanding => 1 * 1024 * 1024, // 1 MiB + else => 16 * 1024 * 1024, // 16 MiB + }, + .allow_shlib_undefined = options.allow_shlib_undefined orelse false, + .file = null, + .disable_lld_caching = options.disable_lld_caching, + .build_id = options.build_id, + }, + .name = undefined, + .string_table = .empty, + .string_bytes = .empty, + .import_table = options.import_table, + .export_table = options.export_table, + .import_symbols = options.import_symbols, + .export_symbol_names = options.export_symbol_names, + .global_base = options.global_base, + .initial_memory = options.initial_memory, + .max_memory = options.max_memory, + + .entry_name = undefined, + .dump_argv_list = .empty, + .object_host_name = .none, + .preloaded_strings = undefined, + }; + if (use_llvm and comp.config.have_zcu) { + wasm.llvm_object = try LlvmObject.create(arena, comp); } + errdefer wasm.base.destroy(); + + if (options.object_host_name) |name| wasm.object_host_name = (try wasm.internString(name)).toOptional(); + + inline for (@typeInfo(PreloadedStrings).@"struct".fields) |field| { + @field(wasm.preloaded_strings, field.name) = try wasm.internString(field.name); + } + + wasm.entry_name = switch (options.entry) { + .disabled => .none, + .default => if (output_mode != .Exe) .none else defaultEntrySymbolName(&wasm.preloaded_strings, wasi_exec_model).toOptional(), + .enabled => defaultEntrySymbolName(&wasm.preloaded_strings, wasi_exec_model).toOptional(), + .named => |name| (try wasm.internString(name)).toOptional(), + }; - // One of the linked object files has a reference to the __heap_base symbol. - // We must set its virtual address so it can be used in relocations. - if (wasm.globals.get(wasm.preloaded_strings.__heap_base)) |loc| { - const symbol = wasm.symbolLocSymbol(loc); - symbol.virtual_address = @intCast(heap_alignment.forward(memory_ptr)); + if (use_lld and (use_llvm or !comp.config.have_zcu)) { + // LLVM emits the object file (if any); LLD links it into the final product. + return wasm; } - // Setup the max amount of pages - // For now we only support wasm32 by setting the maximum allowed memory size 2^32-1 - const max_memory_allowed: u64 = (1 << 32) - 1; + // What path should this Wasm linker code output to? + // If using LLD to link, this code should produce an object file so that it + // can be passed to LLD. + const sub_path = if (use_lld) zcu_object_sub_path.? else emit.sub_path; - if (wasm.initial_memory) |initial_memory| { - if (!std.mem.isAlignedGeneric(u64, initial_memory, page_size)) { - var err = try diags.addErrorWithNotes(0); - try err.addMsg("Initial memory must be {d}-byte aligned", .{page_size}); + wasm.base.file = try emit.root_dir.handle.createFile(sub_path, .{ + .truncate = true, + .read = true, + .mode = if (fs.has_executable_bit) + if (target.os.tag == .wasi and output_mode == .Exe) + fs.File.default_mode | 0b001_000_000 + else + fs.File.default_mode + else + 0, + }); + wasm.name = sub_path; + + return wasm; +} + +fn openParseObjectReportingFailure(wasm: *Wasm, path: Path) void { + const diags = &wasm.base.comp.link_diags; + const obj = link.openObject(path, false, false) catch |err| { + switch (diags.failParse(path, "failed to open object: {s}", .{@errorName(err)})) { + error.LinkFailure => return, } - if (memory_ptr > initial_memory) { - var err = try diags.addErrorWithNotes(0); - try err.addMsg("Initial memory too small, must be at least {d} bytes", .{memory_ptr}); + }; + wasm.parseObject(obj) catch |err| { + switch (diags.failParse(path, "failed to parse object: {s}", .{@errorName(err)})) { + error.LinkFailure => return, } - if (initial_memory > max_memory_allowed) { - var err = try diags.addErrorWithNotes(0); - try err.addMsg("Initial memory exceeds maximum memory {d}", .{max_memory_allowed}); + }; +} + +fn parseObject(wasm: *Wasm, obj: link.Input.Object) !void { + log.debug("parseObject {}", .{obj.path}); + const gpa = wasm.base.comp.gpa; + const gc_sections = wasm.base.gc_sections; + + defer obj.file.close(); + + try wasm.objects.ensureUnusedCapacity(gpa, 1); + const stat = try obj.file.stat(); + const size = std.math.cast(usize, stat.size) orelse return error.FileTooBig; + + const file_contents = try gpa.alloc(u8, size); + defer gpa.free(file_contents); + + const n = try obj.file.preadAll(file_contents, 0); + if (n != file_contents.len) return error.UnexpectedEndOfFile; + + var ss: Object.ScratchSpace = .{}; + defer ss.deinit(gpa); + + const object = try Object.parse(wasm, file_contents, obj.path, null, wasm.object_host_name, &ss, obj.must_link, gc_sections); + wasm.objects.appendAssumeCapacity(object); +} + +fn parseArchive(wasm: *Wasm, obj: link.Input.Object) !void { + log.debug("parseArchive {}", .{obj.path}); + const gpa = wasm.base.comp.gpa; + const gc_sections = wasm.base.gc_sections; + + defer obj.file.close(); + + const stat = try obj.file.stat(); + const size = std.math.cast(usize, stat.size) orelse return error.FileTooBig; + + const file_contents = try gpa.alloc(u8, size); + defer gpa.free(file_contents); + + const n = try obj.file.preadAll(file_contents, 0); + if (n != file_contents.len) return error.UnexpectedEndOfFile; + + var archive = try Archive.parse(gpa, file_contents); + defer archive.deinit(gpa); + + // In this case we must force link all embedded object files within the archive + // We loop over all symbols, and then group them by offset as the offset + // notates where the object file starts. + var offsets = std.AutoArrayHashMap(u32, void).init(gpa); + defer offsets.deinit(); + for (archive.toc.values()) |symbol_offsets| { + for (symbol_offsets.items) |sym_offset| { + try offsets.put(sym_offset, {}); } - memory_ptr = initial_memory; } - memory_ptr = mem.alignForward(u64, memory_ptr, std.wasm.page_size); - // In case we do not import memory, but define it ourselves, - // set the minimum amount of pages on the memory section. - wasm.memories.limits.min = @as(u32, @intCast(memory_ptr / page_size)); - log.debug("Total memory pages: {d}", .{wasm.memories.limits.min}); - if (wasm.globals.get(wasm.preloaded_strings.__heap_end)) |loc| { - const symbol = wasm.symbolLocSymbol(loc); - symbol.virtual_address = @as(u32, @intCast(memory_ptr)); + var ss: Object.ScratchSpace = .{}; + defer ss.deinit(gpa); + + try wasm.objects.ensureUnusedCapacity(gpa, offsets.count()); + for (offsets.keys()) |file_offset| { + const object = try archive.parseObject(wasm, file_contents, file_offset, obj.path, wasm.object_host_name, &ss, obj.must_link, gc_sections); + wasm.objects.appendAssumeCapacity(object); } +} - if (wasm.max_memory) |max_memory| { - if (!std.mem.isAlignedGeneric(u64, max_memory, page_size)) { - var err = try diags.addErrorWithNotes(0); - try err.addMsg("Maximum memory must be {d}-byte aligned", .{page_size}); - } - if (memory_ptr > max_memory) { - var err = try diags.addErrorWithNotes(0); - try err.addMsg("Maximum memory too small, must be at least {d} bytes", .{memory_ptr}); - } - if (max_memory > max_memory_allowed) { - var err = try diags.addErrorWithNotes(0); - try err.addMsg("Maximum memory exceeds maximum amount {d}", .{max_memory_allowed}); - } - wasm.memories.limits.max = @as(u32, @intCast(max_memory / page_size)); - wasm.memories.limits.setFlag(.WASM_LIMITS_FLAG_HAS_MAX); - if (shared_memory) { - wasm.memories.limits.setFlag(.WASM_LIMITS_FLAG_IS_SHARED); - } - log.debug("Maximum memory pages: {?d}", .{wasm.memories.limits.max}); +pub fn deinit(wasm: *Wasm) void { + const gpa = wasm.base.comp.gpa; + if (wasm.llvm_object) |llvm_object| llvm_object.deinit(); + + wasm.navs_exe.deinit(gpa); + wasm.navs_obj.deinit(gpa); + wasm.uavs_exe.deinit(gpa); + wasm.uavs_obj.deinit(gpa); + wasm.overaligned_uavs.deinit(gpa); + wasm.zcu_funcs.deinit(gpa); + wasm.nav_exports.deinit(gpa); + wasm.uav_exports.deinit(gpa); + wasm.imports.deinit(gpa); + + wasm.flush_buffer.deinit(gpa); + + wasm.mir_instructions.deinit(gpa); + wasm.mir_extra.deinit(gpa); + wasm.all_zcu_locals.deinit(gpa); + + if (wasm.dwarf) |*dwarf| dwarf.deinit(); + + wasm.object_function_imports.deinit(gpa); + wasm.object_functions.deinit(gpa); + wasm.object_global_imports.deinit(gpa); + wasm.object_globals.deinit(gpa); + wasm.object_table_imports.deinit(gpa); + wasm.object_tables.deinit(gpa); + wasm.object_memory_imports.deinit(gpa); + wasm.object_memories.deinit(gpa); + wasm.object_relocations.deinit(gpa); + wasm.object_data_imports.deinit(gpa); + wasm.object_data_segments.deinit(gpa); + wasm.object_datas.deinit(gpa); + wasm.object_custom_segments.deinit(gpa); + wasm.object_init_funcs.deinit(gpa); + wasm.object_comdats.deinit(gpa); + wasm.object_relocations_table.deinit(gpa); + wasm.object_comdat_symbols.deinit(gpa); + wasm.objects.deinit(gpa); + + wasm.func_types.deinit(gpa); + wasm.function_exports.deinit(gpa); + wasm.hidden_function_exports.deinit(gpa); + wasm.function_imports.deinit(gpa); + wasm.functions.deinit(gpa); + wasm.globals.deinit(gpa); + wasm.global_exports.deinit(gpa); + wasm.global_imports.deinit(gpa); + wasm.table_imports.deinit(gpa); + wasm.tables.deinit(gpa); + wasm.data_imports.deinit(gpa); + wasm.data_segments.deinit(gpa); + wasm.symbol_table.deinit(gpa); + wasm.out_relocs.deinit(gpa); + wasm.uav_fixups.deinit(gpa); + wasm.nav_fixups.deinit(gpa); + wasm.func_table_fixups.deinit(gpa); + + wasm.zcu_indirect_function_set.deinit(gpa); + wasm.object_indirect_function_import_set.deinit(gpa); + wasm.object_indirect_function_set.deinit(gpa); + + wasm.string_bytes.deinit(gpa); + wasm.string_table.deinit(gpa); + wasm.dump_argv_list.deinit(gpa); + + wasm.params_scratch.deinit(gpa); + wasm.returns_scratch.deinit(gpa); + + wasm.error_name_bytes.deinit(gpa); + wasm.error_name_offs.deinit(gpa); + wasm.tag_name_bytes.deinit(gpa); + wasm.tag_name_offs.deinit(gpa); + + wasm.missing_exports.deinit(gpa); +} + +pub fn updateFunc(wasm: *Wasm, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { + if (build_options.skip_non_native and builtin.object_format != .wasm) { + @panic("Attempted to compile for object format that was disabled by build configuration"); } + if (wasm.llvm_object) |llvm_object| return llvm_object.updateFunc(pt, func_index, air, liveness); + + dev.check(.wasm_backend); + + const zcu = pt.zcu; + const gpa = zcu.gpa; + try wasm.functions.ensureUnusedCapacity(gpa, 1); + try wasm.zcu_funcs.ensureUnusedCapacity(gpa, 1); + + const ip = &zcu.intern_pool; + const owner_nav = zcu.funcInfo(func_index).owner_nav; + log.debug("updateFunc {}", .{ip.getNav(owner_nav).fqn.fmt(ip)}); + + const zds: ZcuDataStarts = .init(wasm); + + // This converts AIR to MIR but does not yet lower to wasm code. + // That lowering happens during `flush`, after garbage collection, which + // can affect function and global indexes, which affects the LEB integer + // encoding, which affects the output binary size. + const function = try CodeGen.function(wasm, pt, func_index, air, liveness); + wasm.zcu_funcs.putAssumeCapacity(func_index, .{ .function = function }); + wasm.functions.putAssumeCapacity(.pack(wasm, .{ .zcu_func = @enumFromInt(wasm.zcu_funcs.entries.len - 1) }), {}); + + try zds.finish(wasm, pt); } -/// From a given object's index and the index of the segment, returns the corresponding -/// index of the segment within the final data section. When the segment does not yet -/// exist, a new one will be initialized and appended. The new index will be returned in that case. -pub fn getMatchingSegment(wasm: *Wasm, object_id: ObjectId, symbol_index: Symbol.Index) !Segment.Index { +// Generate code for the "Nav", storing it in memory to be later written to +// the file on flush(). +pub fn updateNav(wasm: *Wasm, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) !void { + if (build_options.skip_non_native and builtin.object_format != .wasm) { + @panic("Attempted to compile for object format that was disabled by build configuration"); + } + if (wasm.llvm_object) |llvm_object| return llvm_object.updateNav(pt, nav_index); + const zcu = pt.zcu; + const ip = &zcu.intern_pool; + const nav = ip.getNav(nav_index); const comp = wasm.base.comp; const gpa = comp.gpa; - const diags = &wasm.base.comp.link_diags; - const symbol = objectSymbols(wasm, object_id)[@intFromEnum(symbol_index)]; - const index: Segment.Index = @enumFromInt(wasm.segments.items.len); - const shared_memory = comp.config.shared_memory; + const is_obj = comp.config.output_mode == .Obj; + const target = &comp.root_mod.resolved_target.result; - switch (symbol.tag) { - .data => { - const segment_info = objectSegmentInfo(wasm, object_id)[symbol.index]; - const merge_segment = comp.config.output_mode != .Obj; - const result = try wasm.data_segments.getOrPut(gpa, segment_info.outputName(merge_segment)); - if (!result.found_existing) { - result.value_ptr.* = index; - var flags: u32 = 0; - if (shared_memory) { - flags |= @intFromEnum(Segment.Flag.WASM_DATA_SEGMENT_IS_PASSIVE); - } - try wasm.segments.append(gpa, .{ - .alignment = .@"1", - .size = 0, - .offset = 0, - .flags = flags, - }); - try wasm.segment_info.putNoClobber(gpa, index, .{ - .name = try gpa.dupe(u8, segment_info.name), - .alignment = segment_info.alignment, - .flags = segment_info.flags, - }); - return index; - } else return result.value_ptr.*; - }, - .function => return wasm.code_section_index.unwrap() orelse blk: { - wasm.code_section_index = index.toOptional(); - try wasm.appendDummySegment(); - break :blk index; - }, - .section => { - const section_name = wasm.objectSymbol(object_id, symbol_index).name; - - inline for (@typeInfo(CustomSections).@"struct".fields) |field| { - if (@field(wasm.custom_sections, field.name).name == section_name) { - const field_ptr = &@field(wasm.custom_sections, field.name).index; - return field_ptr.unwrap() orelse { - field_ptr.* = index.toOptional(); - try wasm.appendDummySegment(); - return index; - }; - } + const nav_init, const chased_nav_index = switch (ip.indexToKey(nav.status.fully_resolved.val)) { + .func => return, // global const which is a function alias + .@"extern" => |ext| { + if (is_obj) { + assert(!wasm.navs_obj.contains(ext.owner_nav)); + } else { + assert(!wasm.navs_exe.contains(ext.owner_nav)); + } + const name = try wasm.internString(ext.name.toSlice(ip)); + if (ext.lib_name.toSlice(ip)) |ext_name| _ = try wasm.internString(ext_name); + try wasm.imports.ensureUnusedCapacity(gpa, 1); + try wasm.function_imports.ensureUnusedCapacity(gpa, 1); + try wasm.data_imports.ensureUnusedCapacity(gpa, 1); + const zcu_import = wasm.addZcuImportReserved(ext.owner_nav); + if (ip.isFunctionType(nav.typeOf(ip))) { + wasm.function_imports.putAssumeCapacity(name, .fromZcuImport(zcu_import, wasm)); + // Ensure there is a corresponding function type table entry. + const fn_info = zcu.typeToFunc(.fromInterned(ext.ty)).?; + _ = try internFunctionType(wasm, fn_info.cc, fn_info.param_types.get(ip), .fromInterned(fn_info.return_type), target); } else { - return diags.failParse(objectPath(wasm, object_id), "unknown section: {s}", .{ - wasm.stringSlice(section_name), - }); + wasm.data_imports.putAssumeCapacity(name, .fromZcuImport(zcu_import, wasm)); } + return; }, - else => unreachable, + .variable => |variable| .{ variable.init, variable.owner_nav }, + else => .{ nav.status.fully_resolved.val, nav_index }, + }; + //log.debug("updateNav {} {d}", .{ nav.fqn.fmt(ip), chased_nav_index }); + assert(!wasm.imports.contains(chased_nav_index)); + + if (nav_init != .none and !Value.fromInterned(nav_init).typeOf(zcu).hasRuntimeBits(zcu)) { + if (is_obj) { + assert(!wasm.navs_obj.contains(chased_nav_index)); + } else { + assert(!wasm.navs_exe.contains(chased_nav_index)); + } + return; + } + + if (is_obj) { + const zcu_data_starts: ZcuDataStarts = .initObj(wasm); + const navs_i = try refNavObj(wasm, chased_nav_index); + const zcu_data = try lowerZcuData(wasm, pt, nav_init); + navs_i.value(wasm).* = zcu_data; + try zcu_data_starts.finishObj(wasm, pt); + } else { + const zcu_data_starts: ZcuDataStarts = .initExe(wasm); + const navs_i = try refNavExe(wasm, chased_nav_index); + const zcu_data = try lowerZcuData(wasm, pt, nav_init); + navs_i.value(wasm).code = zcu_data.code; + try zcu_data_starts.finishExe(wasm, pt); } } -/// Appends a new segment with default field values -fn appendDummySegment(wasm: *Wasm) !void { - const gpa = wasm.base.comp.gpa; - try wasm.segments.append(gpa, .{ - .alignment = .@"1", - .size = 0, - .offset = 0, - .flags = 0, - }); +pub fn updateLineNumber(wasm: *Wasm, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) !void { + const comp = wasm.base.comp; + const diags = &comp.link_diags; + if (wasm.dwarf) |*dw| { + dw.updateLineNumber(pt.zcu, ti_id) catch |err| switch (err) { + error.Overflow => return error.Overflow, + error.OutOfMemory => return error.OutOfMemory, + else => |e| return diags.fail("failed to update dwarf line numbers: {s}", .{@errorName(e)}), + }; + } +} + +pub fn deleteExport( + wasm: *Wasm, + exported: Zcu.Exported, + name: InternPool.NullTerminatedString, +) void { + if (wasm.llvm_object != null) return; + + const zcu = wasm.base.comp.zcu.?; + const ip = &zcu.intern_pool; + const name_slice = name.toSlice(ip); + const export_name = wasm.getExistingString(name_slice).?; + switch (exported) { + .nav => |nav_index| { + log.debug("deleteExport '{s}' nav={d}", .{ name_slice, @intFromEnum(nav_index) }); + assert(wasm.nav_exports.swapRemove(.{ .nav_index = nav_index, .name = export_name })); + }, + .uav => |uav_index| assert(wasm.uav_exports.swapRemove(.{ .uav_index = uav_index, .name = export_name })), + } +} + +pub fn updateExports( + wasm: *Wasm, + pt: Zcu.PerThread, + exported: Zcu.Exported, + export_indices: []const Zcu.Export.Index, +) !void { + if (build_options.skip_non_native and builtin.object_format != .wasm) { + @panic("Attempted to compile for object format that was disabled by build configuration"); + } + if (wasm.llvm_object) |llvm_object| return llvm_object.updateExports(pt, exported, export_indices); + + const zcu = pt.zcu; + const gpa = zcu.gpa; + const ip = &zcu.intern_pool; + for (export_indices) |export_idx| { + const exp = export_idx.ptr(zcu); + const name_slice = exp.opts.name.toSlice(ip); + const name = try wasm.internString(name_slice); + switch (exported) { + .nav => |nav_index| { + log.debug("updateExports '{s}' nav={d}", .{ name_slice, @intFromEnum(nav_index) }); + try wasm.nav_exports.put(gpa, .{ .nav_index = nav_index, .name = name }, export_idx); + }, + .uav => |uav_index| try wasm.uav_exports.put(gpa, .{ .uav_index = uav_index, .name = name }, export_idx), + } + } } pub fn loadInput(wasm: *Wasm, input: link.Input) !void { @@ -2596,7 +3323,9 @@ pub fn loadInput(wasm: *Wasm, input: link.Input) !void { .res => unreachable, .dso_exact => unreachable, .dso => unreachable, - .object, .archive => |obj| try argv.append(gpa, try obj.path.toString(comp.arena)), + .object, .archive => |obj| { + try argv.append(gpa, try obj.path.toString(comp.arena)); + }, } } @@ -2612,791 +3341,472 @@ pub fn loadInput(wasm: *Wasm, input: link.Input) !void { pub fn flush(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { const comp = wasm.base.comp; const use_lld = build_options.have_llvm and comp.config.use_lld; + const diags = &comp.link_diags; if (use_lld) { - return wasm.linkWithLLD(arena, tid, prog_node); + return wasm.linkWithLLD(arena, tid, prog_node) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to link with LLD: {s}", .{@errorName(e)}), + }; } return wasm.flushModule(arena, tid, prog_node); } -pub fn flushModule(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { +pub fn prelink(wasm: *Wasm, prog_node: std.Progress.Node) link.File.FlushError!void { const tracy = trace(@src()); defer tracy.end(); - const comp = wasm.base.comp; - const diags = &comp.link_diags; - if (wasm.llvm_object) |llvm_object| { - try wasm.base.emitLlvmObject(arena, llvm_object, prog_node); - const use_lld = build_options.have_llvm and comp.config.use_lld; - if (use_lld) return; - } - - if (comp.verbose_link) Compilation.dump_argv(wasm.dump_argv_list.items); - - const sub_prog_node = prog_node.start("Wasm Flush", 0); + const sub_prog_node = prog_node.start("Wasm Prelink", 0); defer sub_prog_node.end(); - const module_obj_path: ?Path = if (wasm.base.zcu_object_sub_path) |path| .{ - .root_dir = wasm.base.emit.root_dir, - .sub_path = if (fs.path.dirname(wasm.base.emit.sub_path)) |dirname| - try fs.path.join(arena, &.{ dirname, path }) - else - path, - } else null; - - if (wasm.zig_object) |zig_object| try zig_object.flushModule(wasm, tid); - - if (module_obj_path) |path| openParseObjectReportingFailure(wasm, path); - - if (wasm.zig_object != null) { - try wasm.resolveSymbolsInObject(.zig_object); - } - if (diags.hasErrors()) return error.FlushFailure; - for (0..wasm.objects.items.len) |object_index| { - try wasm.resolveSymbolsInObject(@enumFromInt(object_index)); - } - if (diags.hasErrors()) return error.FlushFailure; - - var emit_features_count: u32 = 0; - var enabled_features: [@typeInfo(Feature.Tag).@"enum".fields.len]bool = undefined; - try wasm.validateFeatures(&enabled_features, &emit_features_count); - try wasm.resolveSymbolsInArchives(); - if (diags.hasErrors()) return error.FlushFailure; - try wasm.resolveLazySymbols(); - try wasm.checkUndefinedSymbols(); - try wasm.checkExportNames(); - - try wasm.setupInitFunctions(); - if (diags.hasErrors()) return error.FlushFailure; - try wasm.setupStart(); - - try wasm.markReferences(); - try wasm.setupImports(); - try wasm.mergeSections(); - try wasm.mergeTypes(); - try wasm.allocateAtoms(); - try wasm.setupMemory(); - if (diags.hasErrors()) return error.FlushFailure; - wasm.allocateVirtualAddresses(); - wasm.mapFunctionTable(); - try wasm.initializeCallCtorsFunction(); - try wasm.setupInitMemoryFunction(); - try wasm.setupTLSRelocationsFunction(); - try wasm.initializeTLSFunction(); - try wasm.setupStartSection(); - try wasm.setupExports(); - try wasm.writeToFile(enabled_features, emit_features_count, arena); - if (diags.hasErrors()) return error.FlushFailure; -} - -/// Writes the WebAssembly in-memory module to the file -fn writeToFile( - wasm: *Wasm, - enabled_features: [@typeInfo(Feature.Tag).@"enum".fields.len]bool, - feature_count: u32, - arena: Allocator, -) !void { const comp = wasm.base.comp; - const diags = &comp.link_diags; const gpa = comp.gpa; - const use_llvm = comp.config.use_llvm; - const use_lld = build_options.have_llvm and comp.config.use_lld; - const shared_memory = comp.config.shared_memory; - const import_memory = comp.config.import_memory; - const export_memory = comp.config.export_memory; + const rdynamic = comp.config.rdynamic; + const is_obj = comp.config.output_mode == .Obj; - // Size of each section header - const header_size = 5 + 1; - // The amount of sections that will be written - var section_count: u32 = 0; - // Index of the code section. Used to tell relocation table where the section lives. - var code_section_index: ?u32 = null; - // Index of the data section. Used to tell relocation table where the section lives. - var data_section_index: ?u32 = null; - const is_obj = comp.config.output_mode == .Obj or (!use_llvm and use_lld); - - var binary_bytes = std.ArrayList(u8).init(gpa); - defer binary_bytes.deinit(); - const binary_writer = binary_bytes.writer(); - - // We write the magic bytes at the end so they will only be written - // if everything succeeded as expected. So populate with 0's for now. - try binary_writer.writeAll(&[_]u8{0} ** 8); - // (Re)set file pointer to 0 - try wasm.base.file.?.setEndPos(0); - try wasm.base.file.?.seekTo(0); - - // Type section - if (wasm.func_types.items.len != 0) { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - log.debug("Writing type section. Count: ({d})", .{wasm.func_types.items.len}); - for (wasm.func_types.items) |func_type| { - try leb.writeUleb128(binary_writer, std.wasm.function_type); - try leb.writeUleb128(binary_writer, @as(u32, @intCast(func_type.params.len))); - for (func_type.params) |param_ty| { - try leb.writeUleb128(binary_writer, std.wasm.valtype(param_ty)); + assert(wasm.missing_exports.entries.len == 0); + for (wasm.export_symbol_names) |exp_name| { + const exp_name_interned = try wasm.internString(exp_name); + if (wasm.object_function_imports.getPtr(exp_name_interned)) |import| { + if (import.resolution != .unresolved) { + import.flags.exported = true; + continue; } - try leb.writeUleb128(binary_writer, @as(u32, @intCast(func_type.returns.len))); - for (func_type.returns) |ret_ty| { - try leb.writeUleb128(binary_writer, std.wasm.valtype(ret_ty)); + } + if (wasm.object_global_imports.getPtr(exp_name_interned)) |import| { + if (import.resolution != .unresolved) { + import.flags.exported = true; + continue; } } - - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .type, - @intCast(binary_bytes.items.len - header_offset - header_size), - @intCast(wasm.func_types.items.len), - ); - section_count += 1; + if (wasm.object_table_imports.getPtr(exp_name_interned)) |import| { + if (import.resolution != .unresolved) { + import.flags.exported = true; + continue; + } + } + try wasm.missing_exports.put(gpa, exp_name_interned, {}); } - // Import section - if (wasm.imports.count() != 0 or import_memory) { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - - var it = wasm.imports.iterator(); - while (it.next()) |entry| { - assert(wasm.symbolLocSymbol(entry.key_ptr.*).isUndefined()); - const import = entry.value_ptr.*; - try wasm.emitImport(binary_writer, import); + if (wasm.entry_name.unwrap()) |entry_name| { + if (wasm.object_function_imports.getPtr(entry_name)) |import| { + if (import.resolution != .unresolved) { + import.flags.exported = true; + wasm.entry_resolution = import.resolution; + } } + } - if (import_memory) { - const mem_imp: Import = .{ - .module_name = wasm.host_name, - .name = if (is_obj) wasm.preloaded_strings.__linear_memory else wasm.preloaded_strings.memory, - .kind = .{ .memory = wasm.memories.limits }, - }; - try wasm.emitImport(binary_writer, mem_imp); + if (comp.zcu != null) { + // Zig always depends on a stack pointer global. + // If emitting an object, it's an import. Otherwise, the linker synthesizes it. + if (is_obj) { + @panic("TODO"); + } else { + try wasm.globals.put(gpa, .__stack_pointer, {}); + assert(wasm.globals.entries.len - 1 == @intFromEnum(GlobalIndex.stack_pointer)); } - - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .import, - @intCast(binary_bytes.items.len - header_offset - header_size), - @intCast(wasm.imports.count() + @intFromBool(import_memory)), - ); - section_count += 1; } - // Function section - if (wasm.functions.count() != 0) { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - for (wasm.functions.values()) |function| { - try leb.writeUleb128(binary_writer, function.func.type_index); + // These loops do both recursive marking of alive symbols well as checking for undefined symbols. + // At the end, output functions and globals will be populated. + for (wasm.object_function_imports.keys(), wasm.object_function_imports.values(), 0..) |name, *import, i| { + if (import.flags.isIncluded(rdynamic)) { + try markFunctionImport(wasm, name, import, @enumFromInt(i)); } - - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .function, - @intCast(binary_bytes.items.len - header_offset - header_size), - @intCast(wasm.functions.count()), - ); - section_count += 1; } - - // Table section - if (wasm.tables.items.len > 0) { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - - for (wasm.tables.items) |table| { - try leb.writeUleb128(binary_writer, std.wasm.reftype(table.reftype)); - try emitLimits(binary_writer, table.limits); + // Also treat init functions as roots. + for (wasm.object_init_funcs.items) |init_func| { + const func = init_func.function_index.ptr(wasm); + if (func.object_index.ptr(wasm).is_included) { + try markFunction(wasm, init_func.function_index, false); } - - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .table, - @intCast(binary_bytes.items.len - header_offset - header_size), - @intCast(wasm.tables.items.len), - ); - section_count += 1; } + wasm.functions_end_prelink = @intCast(wasm.functions.entries.len); - // Memory section - if (!import_memory) { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - - try emitLimits(binary_writer, wasm.memories.limits); - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .memory, - @intCast(binary_bytes.items.len - header_offset - header_size), - 1, // wasm currently only supports 1 linear memory segment - ); - section_count += 1; + for (wasm.object_global_imports.keys(), wasm.object_global_imports.values(), 0..) |name, *import, i| { + if (import.flags.isIncluded(rdynamic)) { + try markGlobalImport(wasm, name, import, @enumFromInt(i)); + } } + wasm.globals_end_prelink = @intCast(wasm.globals.entries.len); + wasm.global_exports_len = @intCast(wasm.global_exports.items.len); - // Global section (used to emit stack pointer) - if (wasm.wasm_globals.items.len > 0) { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - - for (wasm.wasm_globals.items) |global| { - try binary_writer.writeByte(std.wasm.valtype(global.global_type.valtype)); - try binary_writer.writeByte(@intFromBool(global.global_type.mutable)); - try emitInit(binary_writer, global.init); + for (wasm.object_table_imports.keys(), wasm.object_table_imports.values(), 0..) |name, *import, i| { + if (import.flags.isIncluded(rdynamic)) { + try markTableImport(wasm, name, import, @enumFromInt(i)); } - - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .global, - @intCast(binary_bytes.items.len - header_offset - header_size), - @intCast(wasm.wasm_globals.items.len), - ); - section_count += 1; } - // Export section - if (wasm.exports.items.len != 0 or export_memory) { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - - for (wasm.exports.items) |exp| { - const name = wasm.stringSlice(exp.name); - try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); - try binary_writer.writeAll(name); - try leb.writeUleb128(binary_writer, @intFromEnum(exp.kind)); - try leb.writeUleb128(binary_writer, exp.index); + for (wasm.object_data_imports.keys(), wasm.object_data_imports.values(), 0..) |name, *import, i| { + if (import.flags.isIncluded(rdynamic)) { + try markDataImport(wasm, name, import, @enumFromInt(i)); } - - if (export_memory) { - try leb.writeUleb128(binary_writer, @as(u32, @intCast("memory".len))); - try binary_writer.writeAll("memory"); - try binary_writer.writeByte(std.wasm.externalKind(.memory)); - try leb.writeUleb128(binary_writer, @as(u32, 0)); - } - - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .@"export", - @intCast(binary_bytes.items.len - header_offset - header_size), - @intCast(wasm.exports.items.len + @intFromBool(export_memory)), - ); - section_count += 1; } - if (wasm.entry) |entry_index| { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .start, - @intCast(binary_bytes.items.len - header_offset - header_size), - entry_index, - ); + // This is a wild ass guess at how to merge memories, haven't checked yet + // what the proper way to do this is. + for (wasm.object_memory_imports.values()) |*memory_import| { + wasm.memories.limits.min = @min(wasm.memories.limits.min, memory_import.limits_min); + wasm.memories.limits.max = @max(wasm.memories.limits.max, memory_import.limits_max); + wasm.memories.limits.flags.has_max = wasm.memories.limits.flags.has_max or memory_import.limits_has_max; } - // element section (function table) - if (wasm.function_table.count() > 0) { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - - const table_loc = wasm.globals.get(wasm.preloaded_strings.__indirect_function_table).?; - const table_sym = wasm.symbolLocSymbol(table_loc); - - const flags: u32 = if (table_sym.index == 0) 0x0 else 0x02; // passive with implicit 0-index table or set table index manually - try leb.writeUleb128(binary_writer, flags); - if (flags == 0x02) { - try leb.writeUleb128(binary_writer, table_sym.index); - } - try emitInit(binary_writer, .{ .i32_const = 1 }); // We start at index 1, so unresolved function pointers are invalid - if (flags == 0x02) { - try leb.writeUleb128(binary_writer, @as(u8, 0)); // represents funcref - } - try leb.writeUleb128(binary_writer, @as(u32, @intCast(wasm.function_table.count()))); - var symbol_it = wasm.function_table.keyIterator(); - while (symbol_it.next()) |symbol_loc_ptr| { - const sym = wasm.symbolLocSymbol(symbol_loc_ptr.*); - std.debug.assert(sym.isAlive()); - std.debug.assert(sym.index < wasm.functions.count() + wasm.imported_functions_count); - try leb.writeUleb128(binary_writer, sym.index); - } - - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .element, - @intCast(binary_bytes.items.len - header_offset - header_size), - 1, - ); - section_count += 1; - } - - // When the shared-memory option is enabled, we *must* emit the 'data count' section. - const data_segments_count = wasm.data_segments.count() - @intFromBool(wasm.data_segments.contains(".bss") and !import_memory); - if (data_segments_count != 0 and shared_memory) { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .data_count, - @intCast(binary_bytes.items.len - header_offset - header_size), - @intCast(data_segments_count), - ); - } + wasm.function_imports_len_prelink = @intCast(wasm.function_imports.entries.len); + wasm.data_imports_len_prelink = @intCast(wasm.data_imports.entries.len); +} - // Code section - if (wasm.code_section_index != .none) { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - const start_offset = binary_bytes.items.len - 5; // minus 5 so start offset is 5 to include entry count +pub fn markFunctionImport( + wasm: *Wasm, + name: String, + import: *FunctionImport, + func_index: FunctionImport.Index, +) link.File.FlushError!void { + if (import.flags.alive) return; + import.flags.alive = true; - var func_it = wasm.functions.iterator(); - while (func_it.next()) |entry| { - const sym_loc: SymbolLoc = .{ .index = entry.value_ptr.sym_index, .file = entry.key_ptr.file }; - const atom_index = wasm.symbol_atom.get(sym_loc).?; - const atom = wasm.getAtomPtr(atom_index); + const comp = wasm.base.comp; + const gpa = comp.gpa; - if (!is_obj) { - atom.resolveRelocs(wasm); - } - atom.offset = @intCast(binary_bytes.items.len - start_offset); - try leb.writeUleb128(binary_writer, atom.size); - try binary_writer.writeAll(atom.code.items); + try wasm.functions.ensureUnusedCapacity(gpa, 1); + + if (import.resolution == .unresolved) { + if (name == wasm.preloaded_strings.__wasm_init_memory) { + try wasm.resolveFunctionSynthetic(import, .__wasm_init_memory, &.{}, &.{}); + } else if (name == wasm.preloaded_strings.__wasm_apply_global_tls_relocs) { + try wasm.resolveFunctionSynthetic(import, .__wasm_apply_global_tls_relocs, &.{}, &.{}); + } else if (name == wasm.preloaded_strings.__wasm_call_ctors) { + try wasm.resolveFunctionSynthetic(import, .__wasm_call_ctors, &.{}, &.{}); + } else if (name == wasm.preloaded_strings.__wasm_init_tls) { + try wasm.resolveFunctionSynthetic(import, .__wasm_init_tls, &.{.i32}, &.{}); + } else { + try wasm.function_imports.put(gpa, name, .fromObject(func_index, wasm)); } + } else { + try markFunction(wasm, import.resolution.unpack(wasm).object_function, import.flags.exported); + } +} - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .code, - @intCast(binary_bytes.items.len - header_offset - header_size), - @intCast(wasm.functions.count()), - ); - code_section_index = section_count; - section_count += 1; - } - - // Data section - if (data_segments_count != 0) { - const header_offset = try reserveVecSectionHeader(&binary_bytes); - - var it = wasm.data_segments.iterator(); - var segment_count: u32 = 0; - while (it.next()) |entry| { - // do not output 'bss' section unless we import memory and therefore - // want to guarantee the data is zero initialized - if (!import_memory and std.mem.eql(u8, entry.key_ptr.*, ".bss")) continue; - const segment_index = entry.value_ptr.*; - const segment = wasm.segmentPtr(segment_index); - if (segment.size == 0) continue; // do not emit empty segments - segment_count += 1; - var atom_index = wasm.atoms.get(segment_index).?; - - try leb.writeUleb128(binary_writer, segment.flags); - if (segment.flags & @intFromEnum(Wasm.Segment.Flag.WASM_DATA_SEGMENT_HAS_MEMINDEX) != 0) { - try leb.writeUleb128(binary_writer, @as(u32, 0)); // memory is always index 0 as we only have 1 memory entry - } - // when a segment is passive, it's initialized during runtime. - if (!segment.isPassive()) { - try emitInit(binary_writer, .{ .i32_const = @as(i32, @bitCast(segment.offset)) }); - } - // offset into data section - try leb.writeUleb128(binary_writer, segment.size); - - // fill in the offset table and the data segments - var current_offset: u32 = 0; - while (true) { - const atom = wasm.getAtomPtr(atom_index); - if (!is_obj) { - atom.resolveRelocs(wasm); - } +/// Recursively mark alive everything referenced by the function. +fn markFunction(wasm: *Wasm, i: ObjectFunctionIndex, override_export: bool) link.File.FlushError!void { + const comp = wasm.base.comp; + const gpa = comp.gpa; + const gop = try wasm.functions.getOrPut(gpa, .fromObjectFunction(wasm, i)); + if (gop.found_existing) return; - // Pad with zeroes to ensure all segments are aligned - if (current_offset != atom.offset) { - const diff = atom.offset - current_offset; - try binary_writer.writeByteNTimes(0, diff); - current_offset += diff; - } - assert(current_offset == atom.offset); - assert(atom.code.items.len == atom.size); - try binary_writer.writeAll(atom.code.items); + const rdynamic = comp.config.rdynamic; + const is_obj = comp.config.output_mode == .Obj; + const function = i.ptr(wasm); + markObject(wasm, function.object_index); - current_offset += atom.size; - if (atom.prev != .null) { - atom_index = atom.prev; - } else { - // also pad with zeroes when last atom to ensure - // segments are aligned. - if (current_offset != segment.size) { - try binary_writer.writeByteNTimes(0, segment.size - current_offset); - current_offset += segment.size - current_offset; - } - break; - } - } - assert(current_offset == segment.size); + if (!is_obj and (override_export or function.flags.isExported(rdynamic))) { + const symbol_name = function.name.unwrap().?; + if (!override_export and function.flags.visibility_hidden) { + try wasm.hidden_function_exports.put(gpa, symbol_name, @enumFromInt(gop.index)); + } else { + try wasm.function_exports.put(gpa, symbol_name, @enumFromInt(gop.index)); } - - try writeVecSectionHeader( - binary_bytes.items, - header_offset, - .data, - @intCast(binary_bytes.items.len - header_offset - header_size), - @intCast(segment_count), - ); - data_section_index = section_count; - section_count += 1; } - if (is_obj) { - // relocations need to point to the index of a symbol in the final symbol table. To save memory, - // we never store all symbols in a single table, but store a location reference instead. - // This means that for a relocatable object file, we need to generate one and provide it to the relocation sections. - var symbol_table = std.AutoArrayHashMap(SymbolLoc, u32).init(arena); - try wasm.emitLinkSection(&binary_bytes, &symbol_table); - if (code_section_index) |code_index| { - try wasm.emitCodeRelocations(&binary_bytes, code_index, symbol_table); - } - if (data_section_index) |data_index| { - try wasm.emitDataRelocations(&binary_bytes, data_index, symbol_table); - } - } else if (comp.config.debug_format != .strip) { - try wasm.emitNameSection(&binary_bytes, arena); - } - - if (comp.config.debug_format != .strip) { - // The build id must be computed on the main sections only, - // so we have to do it now, before the debug sections. - switch (wasm.base.build_id) { - .none => {}, - .fast => { - var id: [16]u8 = undefined; - std.crypto.hash.sha3.TurboShake128(null).hash(binary_bytes.items, &id, .{}); - var uuid: [36]u8 = undefined; - _ = try std.fmt.bufPrint(&uuid, "{s}-{s}-{s}-{s}-{s}", .{ - std.fmt.fmtSliceHexLower(id[0..4]), - std.fmt.fmtSliceHexLower(id[4..6]), - std.fmt.fmtSliceHexLower(id[6..8]), - std.fmt.fmtSliceHexLower(id[8..10]), - std.fmt.fmtSliceHexLower(id[10..]), - }); - try emitBuildIdSection(&binary_bytes, &uuid); - }, - .hexstring => |hs| { - var buffer: [32 * 2]u8 = undefined; - const str = std.fmt.bufPrint(&buffer, "{s}", .{ - std.fmt.fmtSliceHexLower(hs.toSlice()), - }) catch unreachable; - try emitBuildIdSection(&binary_bytes, str); - }, - else => |mode| { - var err = try diags.addErrorWithNotes(0); - try err.addMsg("build-id '{s}' is not supported for WebAssembly", .{@tagName(mode)}); - }, - } + try wasm.markRelocations(function.relocations(wasm)); +} - var debug_bytes = std.ArrayList(u8).init(gpa); - defer debug_bytes.deinit(); +fn markObject(wasm: *Wasm, i: ObjectIndex) void { + i.ptr(wasm).is_included = true; +} - inline for (@typeInfo(CustomSections).@"struct".fields) |field| { - if (@field(wasm.custom_sections, field.name).index.unwrap()) |index| { - var atom = wasm.getAtomPtr(wasm.atoms.get(index).?); - while (true) { - atom.resolveRelocs(wasm); - try debug_bytes.appendSlice(atom.code.items); - if (atom.prev == .null) break; - atom = wasm.getAtomPtr(atom.prev); - } - try emitDebugSection(&binary_bytes, debug_bytes.items, field.name); - debug_bytes.clearRetainingCapacity(); - } - } +/// Recursively mark alive everything referenced by the global. +fn markGlobalImport( + wasm: *Wasm, + name: String, + import: *GlobalImport, + global_index: GlobalImport.Index, +) link.File.FlushError!void { + if (import.flags.alive) return; + import.flags.alive = true; - try emitProducerSection(&binary_bytes); - if (feature_count > 0) { - try emitFeaturesSection(&binary_bytes, &enabled_features, feature_count); - } - } + const comp = wasm.base.comp; + const gpa = comp.gpa; - // Only when writing all sections executed properly we write the magic - // bytes. This allows us to easily detect what went wrong while generating - // the final binary. - { - const src = std.wasm.magic ++ std.wasm.version; - binary_bytes.items[0..src.len].* = src; + try wasm.globals.ensureUnusedCapacity(gpa, 1); + + if (import.resolution == .unresolved) { + if (name == wasm.preloaded_strings.__heap_base) { + import.resolution = .__heap_base; + wasm.globals.putAssumeCapacity(.__heap_base, {}); + } else if (name == wasm.preloaded_strings.__heap_end) { + import.resolution = .__heap_end; + wasm.globals.putAssumeCapacity(.__heap_end, {}); + } else if (name == wasm.preloaded_strings.__stack_pointer) { + import.resolution = .__stack_pointer; + wasm.globals.putAssumeCapacity(.__stack_pointer, {}); + } else if (name == wasm.preloaded_strings.__tls_align) { + import.resolution = .__tls_align; + wasm.globals.putAssumeCapacity(.__tls_align, {}); + } else if (name == wasm.preloaded_strings.__tls_base) { + import.resolution = .__tls_base; + wasm.globals.putAssumeCapacity(.__tls_base, {}); + } else if (name == wasm.preloaded_strings.__tls_size) { + import.resolution = .__tls_size; + wasm.globals.putAssumeCapacity(.__tls_size, {}); + } else { + try wasm.global_imports.put(gpa, name, .fromObject(global_index, wasm)); + } + } else { + try markGlobal(wasm, import.resolution.unpack(wasm).object_global, import.flags.exported); } - - // finally, write the entire binary into the file. - var iovec = [_]std.posix.iovec_const{.{ - .base = binary_bytes.items.ptr, - .len = binary_bytes.items.len, - }}; - try wasm.base.file.?.writevAll(&iovec); } -fn emitDebugSection(binary_bytes: *std.ArrayList(u8), data: []const u8, name: []const u8) !void { - if (data.len == 0) return; - const header_offset = try reserveCustomSectionHeader(binary_bytes); - const writer = binary_bytes.writer(); - try leb.writeUleb128(writer, @as(u32, @intCast(name.len))); - try writer.writeAll(name); - - const start = binary_bytes.items.len - header_offset; - log.debug("Emit debug section: '{s}' start=0x{x:0>8} end=0x{x:0>8}", .{ name, start, start + data.len }); - try writer.writeAll(data); - - try writeCustomSectionHeader( - binary_bytes.items, - header_offset, - @as(u32, @intCast(binary_bytes.items.len - header_offset - 6)), - ); -} - -fn emitProducerSection(binary_bytes: *std.ArrayList(u8)) !void { - const header_offset = try reserveCustomSectionHeader(binary_bytes); +fn markGlobal(wasm: *Wasm, i: ObjectGlobalIndex, override_export: bool) link.File.FlushError!void { + const comp = wasm.base.comp; + const gpa = comp.gpa; + const gop = try wasm.globals.getOrPut(gpa, .fromObjectGlobal(wasm, i)); + if (gop.found_existing) return; - const writer = binary_bytes.writer(); - const producers = "producers"; - try leb.writeUleb128(writer, @as(u32, @intCast(producers.len))); - try writer.writeAll(producers); + const rdynamic = comp.config.rdynamic; + const is_obj = comp.config.output_mode == .Obj; + const global = i.ptr(wasm); - try leb.writeUleb128(writer, @as(u32, 2)); // 2 fields: Language + processed-by + if (!is_obj and (override_export or global.flags.isExported(rdynamic))) try wasm.global_exports.append(gpa, .{ + .name = global.name.unwrap().?, + .global_index = @enumFromInt(gop.index), + }); - // used for the Zig version - var version_buf: [100]u8 = undefined; - const version = try std.fmt.bufPrint(&version_buf, "{}", .{build_options.semver}); + try wasm.markRelocations(global.relocations(wasm)); +} - // language field - { - const language = "language"; - try leb.writeUleb128(writer, @as(u32, @intCast(language.len))); - try writer.writeAll(language); +fn markTableImport( + wasm: *Wasm, + name: String, + import: *TableImport, + table_index: TableImport.Index, +) link.File.FlushError!void { + if (import.flags.alive) return; + import.flags.alive = true; - // field_value_count (TODO: Parse object files for producer sections to detect their language) - try leb.writeUleb128(writer, @as(u32, 1)); + const comp = wasm.base.comp; + const gpa = comp.gpa; - // versioned name - { - try leb.writeUleb128(writer, @as(u32, 3)); // len of "Zig" - try writer.writeAll("Zig"); + try wasm.tables.ensureUnusedCapacity(gpa, 1); - try leb.writeUleb128(writer, @as(u32, @intCast(version.len))); - try writer.writeAll(version); + if (import.resolution == .unresolved) { + if (name == wasm.preloaded_strings.__indirect_function_table) { + import.resolution = .__indirect_function_table; + wasm.tables.putAssumeCapacity(.__indirect_function_table, {}); + } else { + try wasm.table_imports.put(gpa, name, table_index); } + } else { + wasm.tables.putAssumeCapacity(import.resolution, {}); + // Tables have no relocations. } +} - // processed-by field - { - const processed_by = "processed-by"; - try leb.writeUleb128(writer, @as(u32, @intCast(processed_by.len))); - try writer.writeAll(processed_by); - - // field_value_count (TODO: Parse object files for producer sections to detect other used tools) - try leb.writeUleb128(writer, @as(u32, 1)); - - // versioned name - { - try leb.writeUleb128(writer, @as(u32, 3)); // len of "Zig" - try writer.writeAll("Zig"); +fn markDataSegment(wasm: *Wasm, segment_index: ObjectDataSegment.Index) link.File.FlushError!void { + const comp = wasm.base.comp; + const segment = segment_index.ptr(wasm); + if (segment.flags.alive) return; + segment.flags.alive = true; - try leb.writeUleb128(writer, @as(u32, @intCast(version.len))); - try writer.writeAll(version); - } - } + wasm.any_passive_inits = wasm.any_passive_inits or segment.flags.is_passive or + (comp.config.import_memory and !wasm.isBss(segment.name)); - try writeCustomSectionHeader( - binary_bytes.items, - header_offset, - @as(u32, @intCast(binary_bytes.items.len - header_offset - 6)), - ); + try wasm.data_segments.put(comp.gpa, .pack(wasm, .{ .object = segment_index }), {}); + try wasm.markRelocations(segment.relocations(wasm)); } -fn emitBuildIdSection(binary_bytes: *std.ArrayList(u8), build_id: []const u8) !void { - const header_offset = try reserveCustomSectionHeader(binary_bytes); - - const writer = binary_bytes.writer(); - const hdr_build_id = "build_id"; - try leb.writeUleb128(writer, @as(u32, @intCast(hdr_build_id.len))); - try writer.writeAll(hdr_build_id); +pub fn markDataImport( + wasm: *Wasm, + name: String, + import: *ObjectDataImport, + data_index: ObjectDataImport.Index, +) link.File.FlushError!void { + if (import.flags.alive) return; + import.flags.alive = true; - try leb.writeUleb128(writer, @as(u32, 1)); - try leb.writeUleb128(writer, @as(u32, @intCast(build_id.len))); - try writer.writeAll(build_id); + const comp = wasm.base.comp; + const gpa = comp.gpa; - try writeCustomSectionHeader( - binary_bytes.items, - header_offset, - @as(u32, @intCast(binary_bytes.items.len - header_offset - 6)), - ); + if (import.resolution == .unresolved) { + if (name == wasm.preloaded_strings.__heap_base) { + import.resolution = .__heap_base; + wasm.data_segments.putAssumeCapacity(.__heap_base, {}); + } else if (name == wasm.preloaded_strings.__heap_end) { + import.resolution = .__heap_end; + wasm.data_segments.putAssumeCapacity(.__heap_end, {}); + } else { + try wasm.data_imports.put(gpa, name, .fromObject(data_index, wasm)); + } + } else if (import.resolution.objectDataSegment(wasm)) |segment_index| { + try markDataSegment(wasm, segment_index); + } } -fn emitFeaturesSection(binary_bytes: *std.ArrayList(u8), enabled_features: []const bool, features_count: u32) !void { - const header_offset = try reserveCustomSectionHeader(binary_bytes); +fn markRelocations(wasm: *Wasm, relocs: ObjectRelocation.IterableSlice) link.File.FlushError!void { + const gpa = wasm.base.comp.gpa; + for (relocs.slice.tags(wasm), relocs.slice.pointees(wasm), relocs.slice.offsets(wasm)) |tag, pointee, offset| { + if (offset >= relocs.end) break; + switch (tag) { + .function_import_index_leb, + .function_import_index_i32, + .function_import_offset_i32, + .function_import_offset_i64, + => { + const name = pointee.symbol_name; + const i: FunctionImport.Index = @enumFromInt(wasm.object_function_imports.getIndex(name).?); + try markFunctionImport(wasm, name, i.value(wasm), i); + }, + .table_import_index_sleb, + .table_import_index_i32, + .table_import_index_sleb64, + .table_import_index_i64, + .table_import_index_rel_sleb, + .table_import_index_rel_sleb64, + => { + const name = pointee.symbol_name; + try wasm.object_indirect_function_import_set.put(gpa, name, {}); + const i: FunctionImport.Index = @enumFromInt(wasm.object_function_imports.getIndex(name).?); + try markFunctionImport(wasm, name, i.value(wasm), i); + }, + .global_import_index_leb, .global_import_index_i32 => { + const name = pointee.symbol_name; + const i: GlobalImport.Index = @enumFromInt(wasm.object_global_imports.getIndex(name).?); + try markGlobalImport(wasm, name, i.value(wasm), i); + }, + .table_import_number_leb => { + const name = pointee.symbol_name; + const i: TableImport.Index = @enumFromInt(wasm.object_table_imports.getIndex(name).?); + try markTableImport(wasm, name, i.value(wasm), i); + }, + .memory_addr_import_leb, + .memory_addr_import_sleb, + .memory_addr_import_i32, + .memory_addr_import_rel_sleb, + .memory_addr_import_leb64, + .memory_addr_import_sleb64, + .memory_addr_import_i64, + .memory_addr_import_rel_sleb64, + .memory_addr_import_tls_sleb, + .memory_addr_import_locrel_i32, + .memory_addr_import_tls_sleb64, + => { + const name = pointee.symbol_name; + const i = ObjectDataImport.Index.fromSymbolName(wasm, name).?; + try markDataImport(wasm, name, i.value(wasm), i); + }, - const writer = binary_bytes.writer(); - const target_features = "target_features"; - try leb.writeUleb128(writer, @as(u32, @intCast(target_features.len))); - try writer.writeAll(target_features); + .function_index_leb, + .function_index_i32, + .function_offset_i32, + .function_offset_i64, + => try markFunction(wasm, pointee.function.chaseWeak(wasm), false), + .table_index_sleb, + .table_index_i32, + .table_index_sleb64, + .table_index_i64, + .table_index_rel_sleb, + .table_index_rel_sleb64, + => { + const function = pointee.function; + try wasm.object_indirect_function_set.put(gpa, function, {}); + try markFunction(wasm, function.chaseWeak(wasm), false); + }, + .global_index_leb, + .global_index_i32, + => try markGlobal(wasm, pointee.global.chaseWeak(wasm), false), + .table_number_leb, + => try markTable(wasm, pointee.table.chaseWeak(wasm)), + + .section_offset_i32 => { + log.warn("TODO: ensure section {d} is included in output", .{pointee.section}); + }, - try leb.writeUleb128(writer, features_count); - for (enabled_features, 0..) |enabled, feature_index| { - if (enabled) { - const feature: Feature = .{ .prefix = .used, .tag = @as(Feature.Tag, @enumFromInt(feature_index)) }; - try leb.writeUleb128(writer, @intFromEnum(feature.prefix)); - var buf: [100]u8 = undefined; - const string = try std.fmt.bufPrint(&buf, "{}", .{feature.tag}); - try leb.writeUleb128(writer, @as(u32, @intCast(string.len))); - try writer.writeAll(string); + .memory_addr_leb, + .memory_addr_sleb, + .memory_addr_i32, + .memory_addr_rel_sleb, + .memory_addr_leb64, + .memory_addr_sleb64, + .memory_addr_i64, + .memory_addr_rel_sleb64, + .memory_addr_tls_sleb, + .memory_addr_locrel_i32, + .memory_addr_tls_sleb64, + => try markDataSegment(wasm, pointee.data.ptr(wasm).segment), + + .type_index_leb => continue, } } +} - try writeCustomSectionHeader( - binary_bytes.items, - header_offset, - @as(u32, @intCast(binary_bytes.items.len - header_offset - 6)), - ); +fn markTable(wasm: *Wasm, i: ObjectTableIndex) link.File.FlushError!void { + try wasm.tables.put(wasm.base.comp.gpa, .fromObjectTable(i), {}); } -fn emitNameSection(wasm: *Wasm, binary_bytes: *std.ArrayList(u8), arena: std.mem.Allocator) !void { +pub fn flushModule( + wasm: *Wasm, + arena: Allocator, + tid: Zcu.PerThread.Id, + prog_node: std.Progress.Node, +) link.File.FlushError!void { + // The goal is to never use this because it's only needed if we need to + // write to InternPool, but flushModule is too late to be writing to the + // InternPool. + _ = tid; const comp = wasm.base.comp; - const import_memory = comp.config.import_memory; - const Name = struct { - index: u32, - name: []const u8, - - fn lessThan(context: void, lhs: @This(), rhs: @This()) bool { - _ = context; - return lhs.index < rhs.index; - } - }; + const use_lld = build_options.have_llvm and comp.config.use_lld; + const diags = &comp.link_diags; + const gpa = comp.gpa; - // we must de-duplicate symbols that point to the same function - var funcs = std.AutoArrayHashMap(u32, Name).init(arena); - try funcs.ensureUnusedCapacity(wasm.functions.count() + wasm.imported_functions_count); - var globals = try std.ArrayList(Name).initCapacity(arena, wasm.wasm_globals.items.len + wasm.imported_globals_count); - var segments = try std.ArrayList(Name).initCapacity(arena, wasm.data_segments.count()); - - for (wasm.resolved_symbols.keys()) |sym_loc| { - const symbol = wasm.symbolLocSymbol(sym_loc).*; - if (symbol.isDead()) { - continue; - } - const name = wasm.symbolLocName(sym_loc); - switch (symbol.tag) { - .function => { - const gop = funcs.getOrPutAssumeCapacity(symbol.index); - if (!gop.found_existing) { - gop.value_ptr.* = .{ .index = symbol.index, .name = name }; - } - }, - .global => globals.appendAssumeCapacity(.{ .index = symbol.index, .name = name }), - else => {}, - } - } - // data segments are already 'ordered' - var data_segment_index: u32 = 0; - for (wasm.data_segments.keys()) |key| { - // bss section is not emitted when this condition holds true, so we also - // do not output a name for it. - if (!import_memory and std.mem.eql(u8, key, ".bss")) continue; - segments.appendAssumeCapacity(.{ .index = data_segment_index, .name = key }); - data_segment_index += 1; + if (wasm.llvm_object) |llvm_object| { + try wasm.base.emitLlvmObject(arena, llvm_object, prog_node); + if (use_lld) return; } - mem.sort(Name, funcs.values(), {}, Name.lessThan); - mem.sort(Name, globals.items, {}, Name.lessThan); + if (comp.verbose_link) Compilation.dump_argv(wasm.dump_argv_list.items); - const header_offset = try reserveCustomSectionHeader(binary_bytes); - const writer = binary_bytes.writer(); - try leb.writeUleb128(writer, @as(u32, @intCast("name".len))); - try writer.writeAll("name"); + if (wasm.base.zcu_object_sub_path) |path| { + const module_obj_path: Path = .{ + .root_dir = wasm.base.emit.root_dir, + .sub_path = if (fs.path.dirname(wasm.base.emit.sub_path)) |dirname| + try fs.path.join(arena, &.{ dirname, path }) + else + path, + }; + openParseObjectReportingFailure(wasm, module_obj_path); + try prelink(wasm, prog_node); + } - try wasm.emitNameSubsection(.function, funcs.values(), writer); - try wasm.emitNameSubsection(.global, globals.items, writer); - try wasm.emitNameSubsection(.data_segment, segments.items, writer); + const tracy = trace(@src()); + defer tracy.end(); - try writeCustomSectionHeader( - binary_bytes.items, - header_offset, - @as(u32, @intCast(binary_bytes.items.len - header_offset - 6)), - ); -} + const sub_prog_node = prog_node.start("Wasm Flush", 0); + defer sub_prog_node.end(); -fn emitNameSubsection(wasm: *Wasm, section_id: std.wasm.NameSubsection, names: anytype, writer: anytype) !void { - const gpa = wasm.base.comp.gpa; + const functions_end_zcu: u32 = @intCast(wasm.functions.entries.len); + defer wasm.functions.shrinkRetainingCapacity(functions_end_zcu); - // We must emit subsection size, so first write to a temporary list - var section_list = std.ArrayList(u8).init(gpa); - defer section_list.deinit(); - const sub_writer = section_list.writer(); + const globals_end_zcu: u32 = @intCast(wasm.globals.entries.len); + defer wasm.globals.shrinkRetainingCapacity(globals_end_zcu); - try leb.writeUleb128(sub_writer, @as(u32, @intCast(names.len))); - for (names) |name| { - log.debug("Emit symbol '{s}' type({s})", .{ name.name, @tagName(section_id) }); - try leb.writeUleb128(sub_writer, name.index); - try leb.writeUleb128(sub_writer, @as(u32, @intCast(name.name.len))); - try sub_writer.writeAll(name.name); - } + const function_exports_end_zcu: u32 = @intCast(wasm.function_exports.entries.len); + defer wasm.function_exports.shrinkRetainingCapacity(function_exports_end_zcu); - // From now, write to the actual writer - try leb.writeUleb128(writer, @intFromEnum(section_id)); - try leb.writeUleb128(writer, @as(u32, @intCast(section_list.items.len))); - try writer.writeAll(section_list.items); -} + const hidden_function_exports_end_zcu: u32 = @intCast(wasm.hidden_function_exports.entries.len); + defer wasm.hidden_function_exports.shrinkRetainingCapacity(hidden_function_exports_end_zcu); -fn emitLimits(writer: anytype, limits: std.wasm.Limits) !void { - try writer.writeByte(limits.flags); - try leb.writeUleb128(writer, limits.min); - if (limits.hasFlag(.WASM_LIMITS_FLAG_HAS_MAX)) { - try leb.writeUleb128(writer, limits.max); - } -} + wasm.flush_buffer.clear(); + try wasm.flush_buffer.missing_exports.reinit(gpa, wasm.missing_exports.keys(), &.{}); + try wasm.flush_buffer.function_imports.reinit(gpa, wasm.function_imports.keys(), wasm.function_imports.values()); + try wasm.flush_buffer.global_imports.reinit(gpa, wasm.global_imports.keys(), wasm.global_imports.values()); + try wasm.flush_buffer.data_imports.reinit(gpa, wasm.data_imports.keys(), wasm.data_imports.values()); -fn emitInit(writer: anytype, init_expr: std.wasm.InitExpression) !void { - switch (init_expr) { - .i32_const => |val| { - try writer.writeByte(std.wasm.opcode(.i32_const)); - try leb.writeIleb128(writer, val); - }, - .i64_const => |val| { - try writer.writeByte(std.wasm.opcode(.i64_const)); - try leb.writeIleb128(writer, val); - }, - .f32_const => |val| { - try writer.writeByte(std.wasm.opcode(.f32_const)); - try writer.writeInt(u32, @bitCast(val), .little); - }, - .f64_const => |val| { - try writer.writeByte(std.wasm.opcode(.f64_const)); - try writer.writeInt(u64, @bitCast(val), .little); - }, - .global_get => |val| { - try writer.writeByte(std.wasm.opcode(.global_get)); - try leb.writeUleb128(writer, val); - }, - } - try writer.writeByte(std.wasm.opcode(.end)); -} - -fn emitImport(wasm: *Wasm, writer: anytype, import: Import) !void { - const module_name = wasm.stringSlice(import.module_name); - try leb.writeUleb128(writer, @as(u32, @intCast(module_name.len))); - try writer.writeAll(module_name); - - const name = wasm.stringSlice(import.name); - try leb.writeUleb128(writer, @as(u32, @intCast(name.len))); - try writer.writeAll(name); - - try writer.writeByte(@intFromEnum(import.kind)); - switch (import.kind) { - .function => |type_index| try leb.writeUleb128(writer, type_index), - .global => |global_type| { - try leb.writeUleb128(writer, std.wasm.valtype(global_type.valtype)); - try writer.writeByte(@intFromBool(global_type.mutable)); - }, - .table => |table| { - try leb.writeUleb128(writer, std.wasm.reftype(table.reftype)); - try emitLimits(writer, table.limits); - }, - .memory => |limits| { - try emitLimits(writer, limits); - }, - } + return wasm.flush_buffer.finish(wasm) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to flush wasm: {s}", .{@errorName(e)}), + }; } fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) !void { @@ -3406,6 +3816,7 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: defer tracy.end(); const comp = wasm.base.comp; + const diags = &comp.link_diags; const shared_memory = comp.config.shared_memory; const export_memory = comp.config.export_memory; const import_memory = comp.config.import_memory; @@ -3459,7 +3870,7 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: } try man.addOptionalFile(module_obj_path); try man.addOptionalFilePath(compiler_rt_path); - man.hash.addOptionalBytes(wasm.optionalStringSlice(wasm.entry_name)); + man.hash.addOptionalBytes(wasm.entry_name.slice(wasm)); man.hash.add(wasm.base.stack_size); man.hash.add(wasm.base.build_id); man.hash.add(import_memory); @@ -3608,7 +4019,7 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: try argv.append("--export-dynamic"); } - if (wasm.optionalStringSlice(wasm.entry_name)) |entry_name| { + if (wasm.entry_name.slice(wasm)) |entry_name| { try argv.appendSlice(&.{ "--entry", entry_name }); } else { try argv.append("--no-entry"); @@ -3750,14 +4161,12 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: switch (term) { .Exited => |code| { if (code != 0) { - const diags = &comp.link_diags; diags.lockAndParseLldStderr(linker_command, stderr); - return error.LLDReportedFailure; + return error.LinkFailure; } }, else => { - log.err("{s} terminated with stderr:\n{s}", .{ argv.items[0], stderr }); - return error.LLDCrashed; + return diags.fail("{s} terminated with stderr:\n{s}", .{ argv.items[0], stderr }); }, } @@ -3771,7 +4180,7 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: if (comp.clang_passthrough_mode) { std.process.exit(exit_code); } else { - return error.LLDReportedFailure; + return diags.fail("{s} returned exit code {d}:\n{s}", .{ argv.items[0], exit_code }); } } } @@ -3811,969 +4220,507 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: } } -fn reserveVecSectionHeader(bytes: *std.ArrayList(u8)) !u32 { - // section id + fixed leb contents size + fixed leb vector length - const header_size = 1 + 5 + 5; - const offset = @as(u32, @intCast(bytes.items.len)); - try bytes.appendSlice(&[_]u8{0} ** header_size); - return offset; +fn defaultEntrySymbolName( + preloaded_strings: *const PreloadedStrings, + wasi_exec_model: std.builtin.WasiExecModel, +) String { + return switch (wasi_exec_model) { + .reactor => preloaded_strings._initialize, + .command => preloaded_strings._start, + }; } -fn reserveCustomSectionHeader(bytes: *std.ArrayList(u8)) !u32 { - // unlike regular section, we don't emit the count - const header_size = 1 + 5; - const offset = @as(u32, @intCast(bytes.items.len)); - try bytes.appendSlice(&[_]u8{0} ** header_size); - return offset; +pub fn internOptionalString(wasm: *Wasm, optional_bytes: ?[]const u8) Allocator.Error!OptionalString { + const bytes = optional_bytes orelse return .none; + const string = try internString(wasm, bytes); + return string.toOptional(); } -fn writeVecSectionHeader(buffer: []u8, offset: u32, section: std.wasm.Section, size: u32, items: u32) !void { - var buf: [1 + 5 + 5]u8 = undefined; - buf[0] = @intFromEnum(section); - leb.writeUnsignedFixed(5, buf[1..6], size); - leb.writeUnsignedFixed(5, buf[6..], items); - buffer[offset..][0..buf.len].* = buf; -} +pub fn internString(wasm: *Wasm, bytes: []const u8) Allocator.Error!String { + assert(mem.indexOfScalar(u8, bytes, 0) == null); + wasm.string_bytes_lock.lock(); + defer wasm.string_bytes_lock.unlock(); + const gpa = wasm.base.comp.gpa; + const gop = try wasm.string_table.getOrPutContextAdapted( + gpa, + @as([]const u8, bytes), + @as(String.TableIndexAdapter, .{ .bytes = wasm.string_bytes.items }), + @as(String.TableContext, .{ .bytes = wasm.string_bytes.items }), + ); + if (gop.found_existing) return gop.key_ptr.*; -fn writeCustomSectionHeader(buffer: []u8, offset: u32, size: u32) !void { - var buf: [1 + 5]u8 = undefined; - buf[0] = 0; // 0 = 'custom' section - leb.writeUnsignedFixed(5, buf[1..6], size); - buffer[offset..][0..buf.len].* = buf; -} + try wasm.string_bytes.ensureUnusedCapacity(gpa, bytes.len + 1); + const new_off: String = @enumFromInt(wasm.string_bytes.items.len); -fn emitLinkSection(wasm: *Wasm, binary_bytes: *std.ArrayList(u8), symbol_table: *std.AutoArrayHashMap(SymbolLoc, u32)) !void { - const offset = try reserveCustomSectionHeader(binary_bytes); - const writer = binary_bytes.writer(); - // emit "linking" custom section name - const section_name = "linking"; - try leb.writeUleb128(writer, section_name.len); - try writer.writeAll(section_name); + wasm.string_bytes.appendSliceAssumeCapacity(bytes); + wasm.string_bytes.appendAssumeCapacity(0); - // meta data version, which is currently '2' - try leb.writeUleb128(writer, @as(u32, 2)); + gop.key_ptr.* = new_off; - // For each subsection type (found in Subsection) we can emit a section. - // Currently, we only support emitting segment info and the symbol table. - try wasm.emitSymbolTable(binary_bytes, symbol_table); - try wasm.emitSegmentInfo(binary_bytes); + return new_off; +} - const size: u32 = @intCast(binary_bytes.items.len - offset - 6); - try writeCustomSectionHeader(binary_bytes.items, offset, size); +// TODO implement instead by appending to string_bytes +pub fn internStringFmt(wasm: *Wasm, comptime format: []const u8, args: anytype) Allocator.Error!String { + var buffer: [32]u8 = undefined; + const slice = std.fmt.bufPrint(&buffer, format, args) catch unreachable; + return internString(wasm, slice); } -fn emitSymbolTable(wasm: *Wasm, binary_bytes: *std.ArrayList(u8), symbol_table: *std.AutoArrayHashMap(SymbolLoc, u32)) !void { - const writer = binary_bytes.writer(); - - try leb.writeUleb128(writer, @intFromEnum(SubsectionType.WASM_SYMBOL_TABLE)); - const table_offset = binary_bytes.items.len; - - var symbol_count: u32 = 0; - for (wasm.resolved_symbols.keys()) |sym_loc| { - const symbol = wasm.symbolLocSymbol(sym_loc).*; - if (symbol.tag == .dead) continue; // Do not emit dead symbols - try symbol_table.putNoClobber(sym_loc, symbol_count); - symbol_count += 1; - log.debug("Emit symbol: {}", .{symbol}); - try leb.writeUleb128(writer, @intFromEnum(symbol.tag)); - try leb.writeUleb128(writer, symbol.flags); - - const sym_name = wasm.symbolLocName(sym_loc); - switch (symbol.tag) { - .data => { - try leb.writeUleb128(writer, @as(u32, @intCast(sym_name.len))); - try writer.writeAll(sym_name); - - if (symbol.isDefined()) { - try leb.writeUleb128(writer, symbol.index); - const atom_index = wasm.symbol_atom.get(sym_loc).?; - const atom = wasm.getAtom(atom_index); - try leb.writeUleb128(writer, @as(u32, atom.offset)); - try leb.writeUleb128(writer, @as(u32, atom.size)); - } - }, - .section => { - try leb.writeUleb128(writer, symbol.index); - }, - else => { - try leb.writeUleb128(writer, symbol.index); - if (symbol.isDefined()) { - try leb.writeUleb128(writer, @as(u32, @intCast(sym_name.len))); - try writer.writeAll(sym_name); - } - }, - } - } +pub fn getExistingString(wasm: *const Wasm, bytes: []const u8) ?String { + assert(mem.indexOfScalar(u8, bytes, 0) == null); + return wasm.string_table.getKeyAdapted(bytes, @as(String.TableIndexAdapter, .{ + .bytes = wasm.string_bytes.items, + })); +} - var buf: [10]u8 = undefined; - leb.writeUnsignedFixed(5, buf[0..5], @intCast(binary_bytes.items.len - table_offset + 5)); - leb.writeUnsignedFixed(5, buf[5..], symbol_count); - try binary_bytes.insertSlice(table_offset, &buf); +pub fn internValtypeList(wasm: *Wasm, valtype_list: []const std.wasm.Valtype) Allocator.Error!ValtypeList { + return .fromString(try internString(wasm, @ptrCast(valtype_list))); } -fn emitSegmentInfo(wasm: *Wasm, binary_bytes: *std.ArrayList(u8)) !void { - const writer = binary_bytes.writer(); - try leb.writeUleb128(writer, @intFromEnum(SubsectionType.WASM_SEGMENT_INFO)); - const segment_offset = binary_bytes.items.len; - - try leb.writeUleb128(writer, @as(u32, @intCast(wasm.segment_info.count()))); - for (wasm.segment_info.values()) |segment_info| { - log.debug("Emit segment: {s} align({d}) flags({b})", .{ - segment_info.name, - segment_info.alignment, - segment_info.flags, - }); - try leb.writeUleb128(writer, @as(u32, @intCast(segment_info.name.len))); - try writer.writeAll(segment_info.name); - try leb.writeUleb128(writer, segment_info.alignment.toLog2Units()); - try leb.writeUleb128(writer, segment_info.flags); - } +pub fn getExistingValtypeList(wasm: *const Wasm, valtype_list: []const std.wasm.Valtype) ?ValtypeList { + return .fromString(getExistingString(wasm, @ptrCast(valtype_list)) orelse return null); +} - var buf: [5]u8 = undefined; - leb.writeUnsignedFixed(5, &buf, @as(u32, @intCast(binary_bytes.items.len - segment_offset))); - try binary_bytes.insertSlice(segment_offset, &buf); +pub fn addFuncType(wasm: *Wasm, ft: FunctionType) Allocator.Error!FunctionType.Index { + const gpa = wasm.base.comp.gpa; + const gop = try wasm.func_types.getOrPut(gpa, ft); + return @enumFromInt(gop.index); } -pub fn getUleb128Size(uint_value: anytype) u32 { - const T = @TypeOf(uint_value); - const U = if (@typeInfo(T).int.bits < 8) u8 else T; - var value = @as(U, @intCast(uint_value)); +pub fn getExistingFuncType(wasm: *const Wasm, ft: FunctionType) ?FunctionType.Index { + const index = wasm.func_types.getIndex(ft) orelse return null; + return @enumFromInt(index); +} - var size: u32 = 0; - while (value != 0) : (size += 1) { - value >>= 7; - } - return size; +pub fn getExistingFuncType2(wasm: *const Wasm, params: []const std.wasm.Valtype, returns: []const std.wasm.Valtype) FunctionType.Index { + return getExistingFuncType(wasm, .{ + .params = getExistingValtypeList(wasm, params).?, + .returns = getExistingValtypeList(wasm, returns).?, + }).?; } -/// For each relocatable section, emits a custom "relocation.<section_name>" section -fn emitCodeRelocations( +pub fn internFunctionType( wasm: *Wasm, - binary_bytes: *std.ArrayList(u8), - section_index: u32, - symbol_table: std.AutoArrayHashMap(SymbolLoc, u32), -) !void { - const code_index = wasm.code_section_index.unwrap() orelse return; - const writer = binary_bytes.writer(); - const header_offset = try reserveCustomSectionHeader(binary_bytes); - - // write custom section information - const name = "reloc.CODE"; - try leb.writeUleb128(writer, @as(u32, @intCast(name.len))); - try writer.writeAll(name); - try leb.writeUleb128(writer, section_index); - const reloc_start = binary_bytes.items.len; - - var count: u32 = 0; - var atom: *Atom = wasm.getAtomPtr(wasm.atoms.get(code_index).?); - // for each atom, we calculate the uleb size and append that - var size_offset: u32 = 5; // account for code section size leb128 - while (true) { - size_offset += getUleb128Size(atom.size); - for (atom.relocs.items) |relocation| { - count += 1; - const sym_loc: SymbolLoc = .{ .file = atom.file, .index = @enumFromInt(relocation.index) }; - const symbol_index = symbol_table.get(sym_loc).?; - try leb.writeUleb128(writer, @intFromEnum(relocation.relocation_type)); - const offset = atom.offset + relocation.offset + size_offset; - try leb.writeUleb128(writer, offset); - try leb.writeUleb128(writer, symbol_index); - if (relocation.relocation_type.addendIsPresent()) { - try leb.writeIleb128(writer, relocation.addend); - } - log.debug("Emit relocation: {}", .{relocation}); - } - if (atom.prev == .null) break; - atom = wasm.getAtomPtr(atom.prev); - } - if (count == 0) return; - var buf: [5]u8 = undefined; - leb.writeUnsignedFixed(5, &buf, count); - try binary_bytes.insertSlice(reloc_start, &buf); - const size: u32 = @intCast(binary_bytes.items.len - header_offset - 6); - try writeCustomSectionHeader(binary_bytes.items, header_offset, size); + cc: std.builtin.CallingConvention, + params: []const InternPool.Index, + return_type: Zcu.Type, + target: *const std.Target, +) Allocator.Error!FunctionType.Index { + try convertZcuFnType(wasm.base.comp, cc, params, return_type, target, &wasm.params_scratch, &wasm.returns_scratch); + return wasm.addFuncType(.{ + .params = try wasm.internValtypeList(wasm.params_scratch.items), + .returns = try wasm.internValtypeList(wasm.returns_scratch.items), + }); } -fn emitDataRelocations( +pub fn getExistingFunctionType( wasm: *Wasm, - binary_bytes: *std.ArrayList(u8), - section_index: u32, - symbol_table: std.AutoArrayHashMap(SymbolLoc, u32), -) !void { - if (wasm.data_segments.count() == 0) return; - const writer = binary_bytes.writer(); - const header_offset = try reserveCustomSectionHeader(binary_bytes); - - // write custom section information - const name = "reloc.DATA"; - try leb.writeUleb128(writer, @as(u32, @intCast(name.len))); - try writer.writeAll(name); - try leb.writeUleb128(writer, section_index); - const reloc_start = binary_bytes.items.len; - - var count: u32 = 0; - // for each atom, we calculate the uleb size and append that - var size_offset: u32 = 5; // account for code section size leb128 - for (wasm.data_segments.values()) |segment_index| { - var atom: *Atom = wasm.getAtomPtr(wasm.atoms.get(segment_index).?); - while (true) { - size_offset += getUleb128Size(atom.size); - for (atom.relocs.items) |relocation| { - count += 1; - const sym_loc: SymbolLoc = .{ .file = atom.file, .index = @enumFromInt(relocation.index) }; - const symbol_index = symbol_table.get(sym_loc).?; - try leb.writeUleb128(writer, @intFromEnum(relocation.relocation_type)); - const offset = atom.offset + relocation.offset + size_offset; - try leb.writeUleb128(writer, offset); - try leb.writeUleb128(writer, symbol_index); - if (relocation.relocation_type.addendIsPresent()) { - try leb.writeIleb128(writer, relocation.addend); - } - log.debug("Emit relocation: {}", .{relocation}); - } - if (atom.prev == .null) break; - atom = wasm.getAtomPtr(atom.prev); - } - } - if (count == 0) return; - - var buf: [5]u8 = undefined; - leb.writeUnsignedFixed(5, &buf, count); - try binary_bytes.insertSlice(reloc_start, &buf); - const size = @as(u32, @intCast(binary_bytes.items.len - header_offset - 6)); - try writeCustomSectionHeader(binary_bytes.items, header_offset, size); + cc: std.builtin.CallingConvention, + params: []const InternPool.Index, + return_type: Zcu.Type, + target: *const std.Target, +) ?FunctionType.Index { + convertZcuFnType(wasm.base.comp, cc, params, return_type, target, &wasm.params_scratch, &wasm.returns_scratch) catch |err| switch (err) { + error.OutOfMemory => return null, + }; + return wasm.getExistingFuncType(.{ + .params = wasm.getExistingValtypeList(wasm.params_scratch.items) orelse return null, + .returns = wasm.getExistingValtypeList(wasm.returns_scratch.items) orelse return null, + }); } -fn hasPassiveInitializationSegments(wasm: *const Wasm) bool { - const comp = wasm.base.comp; - const import_memory = comp.config.import_memory; - - var it = wasm.data_segments.iterator(); - while (it.next()) |entry| { - const segment = wasm.segmentPtr(entry.value_ptr.*); - if (segment.needsPassiveInitialization(import_memory, entry.key_ptr.*)) { - return true; - } - } - return false; +pub fn addExpr(wasm: *Wasm, bytes: []const u8) Allocator.Error!Expr { + const gpa = wasm.base.comp.gpa; + // We can't use string table deduplication here since these expressions can + // have null bytes in them however it may be interesting to explore since + // it is likely for globals to share initialization values. Then again + // there may not be very many globals in total. + try wasm.string_bytes.appendSlice(gpa, bytes); + return @enumFromInt(wasm.string_bytes.items.len - bytes.len); } -/// Searches for a matching function signature. When no matching signature is found, -/// a new entry will be made. The value returned is the index of the type within `wasm.func_types`. -pub fn putOrGetFuncType(wasm: *Wasm, func_type: std.wasm.Type) !u32 { - if (wasm.getTypeIndex(func_type)) |index| { - return index; - } - - // functype does not exist. +pub fn addRelocatableDataPayload(wasm: *Wasm, bytes: []const u8) Allocator.Error!DataPayload { const gpa = wasm.base.comp.gpa; - const index: u32 = @intCast(wasm.func_types.items.len); - const params = try gpa.dupe(std.wasm.Valtype, func_type.params); - errdefer gpa.free(params); - const returns = try gpa.dupe(std.wasm.Valtype, func_type.returns); - errdefer gpa.free(returns); - try wasm.func_types.append(gpa, .{ - .params = params, - .returns = returns, - }); - return index; + try wasm.string_bytes.appendSlice(gpa, bytes); + return .{ + .off = @enumFromInt(wasm.string_bytes.items.len - bytes.len), + .len = @intCast(bytes.len), + }; } -/// For the given `nav`, stores the corresponding type representing the function signature. -/// Asserts declaration has an associated `Atom`. -/// Returns the index into the list of types. -pub fn storeNavType(wasm: *Wasm, nav: InternPool.Nav.Index, func_type: std.wasm.Type) !u32 { - return wasm.zig_object.?.storeDeclType(wasm.base.comp.gpa, nav, func_type); +pub fn uavSymbolIndex(wasm: *Wasm, ip_index: InternPool.Index) Allocator.Error!SymbolTableIndex { + const comp = wasm.base.comp; + assert(comp.config.output_mode == .Obj); + const gpa = comp.gpa; + const name = try wasm.internStringFmt("__anon_{d}", .{@intFromEnum(ip_index)}); + const gop = try wasm.symbol_table.getOrPut(gpa, name); + gop.value_ptr.* = {}; + return @enumFromInt(gop.index); } -/// Returns the symbol index of the error name table. -/// -/// When the symbol does not yet exist, it will create a new one instead. -pub fn getErrorTableSymbol(wasm: *Wasm, pt: Zcu.PerThread) !u32 { - const sym_index = try wasm.zig_object.?.getErrorTableSymbol(wasm, pt); - return @intFromEnum(sym_index); +pub fn navSymbolIndex(wasm: *Wasm, nav_index: InternPool.Nav.Index) Allocator.Error!SymbolTableIndex { + const comp = wasm.base.comp; + assert(comp.config.output_mode == .Obj); + const zcu = comp.zcu.?; + const ip = &zcu.intern_pool; + const gpa = comp.gpa; + const nav = ip.getNav(nav_index); + const name = try wasm.internString(nav.fqn.toSlice(ip)); + const gop = try wasm.symbol_table.getOrPut(gpa, name); + gop.value_ptr.* = {}; + return @enumFromInt(gop.index); } -/// For a given `InternPool.DeclIndex` returns its corresponding `Atom.Index`. -/// When the index was not found, a new `Atom` will be created, and its index will be returned. -/// The newly created Atom is empty with default fields as specified by `Atom.empty`. -pub fn getOrCreateAtomForNav(wasm: *Wasm, pt: Zcu.PerThread, nav: InternPool.Nav.Index) !Atom.Index { - return wasm.zig_object.?.getOrCreateAtomForNav(wasm, pt, nav); +pub fn errorNameTableSymbolIndex(wasm: *Wasm) Allocator.Error!SymbolTableIndex { + const comp = wasm.base.comp; + assert(comp.config.output_mode == .Obj); + const gpa = comp.gpa; + const gop = try wasm.symbol_table.getOrPut(gpa, wasm.preloaded_strings.__zig_error_name_table); + gop.value_ptr.* = {}; + return @enumFromInt(gop.index); } -/// Verifies all resolved symbols and checks whether itself needs to be marked alive, -/// as well as any of its references. -fn markReferences(wasm: *Wasm) !void { - const tracy = trace(@src()); - defer tracy.end(); - - const do_garbage_collect = wasm.base.gc_sections; +pub fn stackPointerSymbolIndex(wasm: *Wasm) Allocator.Error!SymbolTableIndex { const comp = wasm.base.comp; - - for (wasm.resolved_symbols.keys()) |sym_loc| { - const sym = wasm.symbolLocSymbol(sym_loc); - if (sym.isExported(comp.config.rdynamic) or sym.isNoStrip() or !do_garbage_collect) { - try wasm.mark(sym_loc); - continue; - } - - // Debug sections may require to be parsed and marked when it contains - // relocations to alive symbols. - if (sym.tag == .section and comp.config.debug_format != .strip) { - const object_id = sym_loc.file.unwrap() orelse continue; // Incremental debug info is done independently - _ = try wasm.parseSymbolIntoAtom(object_id, sym_loc.index); - sym.mark(); - } - } + assert(comp.config.output_mode == .Obj); + const gpa = comp.gpa; + const gop = try wasm.symbol_table.getOrPut(gpa, wasm.preloaded_strings.__stack_pointer); + gop.value_ptr.* = {}; + return @enumFromInt(gop.index); } -/// Marks a symbol as 'alive' recursively so itself and any references it contains to -/// other symbols will not be omit from the binary. -fn mark(wasm: *Wasm, loc: SymbolLoc) !void { - const symbol = wasm.symbolLocSymbol(loc); - if (symbol.isAlive()) { - // Symbol is already marked alive, including its references. - // This means we can skip it so we don't end up marking the same symbols - // multiple times. - return; - } - symbol.mark(); - gc_log.debug("Marked symbol '{s}'", .{wasm.symbolLocName(loc)}); - if (symbol.isUndefined()) { - // undefined symbols do not have an associated `Atom` and therefore also - // do not contain relocations. - return; - } - - const atom_index = if (loc.file.unwrap()) |object_id| - try wasm.parseSymbolIntoAtom(object_id, loc.index) - else - wasm.symbol_atom.get(loc) orelse return; - - const atom = wasm.getAtom(atom_index); - for (atom.relocs.items) |reloc| { - const target_loc: SymbolLoc = .{ .index = @enumFromInt(reloc.index), .file = loc.file }; - try wasm.mark(wasm.symbolLocFinalLoc(target_loc)); - } +pub fn tagNameSymbolIndex(wasm: *Wasm, ip_index: InternPool.Index) Allocator.Error!SymbolTableIndex { + const comp = wasm.base.comp; + assert(comp.config.output_mode == .Obj); + const gpa = comp.gpa; + const name = try wasm.internStringFmt("__zig_tag_name_{d}", .{@intFromEnum(ip_index)}); + const gop = try wasm.symbol_table.getOrPut(gpa, name); + gop.value_ptr.* = {}; + return @enumFromInt(gop.index); } -fn defaultEntrySymbolName( - preloaded_strings: *const PreloadedStrings, - wasi_exec_model: std.builtin.WasiExecModel, -) String { - return switch (wasi_exec_model) { - .reactor => preloaded_strings._initialize, - .command => preloaded_strings._start, - }; +pub fn symbolNameIndex(wasm: *Wasm, name: String) Allocator.Error!SymbolTableIndex { + const comp = wasm.base.comp; + assert(comp.config.output_mode == .Obj); + const gpa = comp.gpa; + const gop = try wasm.symbol_table.getOrPut(gpa, name); + gop.value_ptr.* = {}; + return @enumFromInt(gop.index); } -pub const Atom = struct { - /// Represents the index of the file this atom was generated from. - /// This is `none` when the atom was generated by a synthetic linker symbol. - file: OptionalObjectId, - /// symbol index of the symbol representing this atom - sym_index: Symbol.Index, - /// Size of the atom, used to calculate section sizes in the final binary - size: u32 = 0, - /// List of relocations belonging to this atom - relocs: std.ArrayListUnmanaged(Relocation) = .empty, - /// Contains the binary data of an atom, which can be non-relocated - code: std.ArrayListUnmanaged(u8) = .empty, - /// For code this is 1, for data this is set to the highest value of all segments - alignment: Wasm.Alignment = .@"1", - /// Offset into the section where the atom lives, this already accounts - /// for alignment. - offset: u32 = 0, - /// The original offset within the object file. This value is subtracted from - /// relocation offsets to determine where in the `data` to rewrite the value - original_offset: u32 = 0, - /// Previous atom in relation to this atom. - /// is null when this atom is the first in its order - prev: Atom.Index = .null, - /// Contains atoms local to a decl, all managed by this `Atom`. - /// When the parent atom is being freed, it will also do so for all local atoms. - locals: std.ArrayListUnmanaged(Atom.Index) = .empty, - - /// Represents the index of an Atom where `null` is considered - /// an invalid atom. - pub const Index = enum(u32) { - null = std.math.maxInt(u32), - _, - }; - - /// Frees all resources owned by this `Atom`. - pub fn deinit(atom: *Atom, gpa: std.mem.Allocator) void { - atom.relocs.deinit(gpa); - atom.code.deinit(gpa); - atom.locals.deinit(gpa); - atom.* = undefined; - } - - /// Sets the length of relocations and code to '0', - /// effectively resetting them and allowing them to be re-populated. - pub fn clear(atom: *Atom) void { - atom.relocs.clearRetainingCapacity(); - atom.code.clearRetainingCapacity(); - } - - pub fn format(atom: Atom, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { - _ = fmt; - _ = options; - try writer.print("Atom{{ .sym_index = {d}, .alignment = {d}, .size = {d}, .offset = 0x{x:0>8} }}", .{ - @intFromEnum(atom.sym_index), - atom.alignment, - atom.size, - atom.offset, - }); - } +pub fn refUavObj(wasm: *Wasm, ip_index: InternPool.Index, orig_ptr_ty: InternPool.Index) !UavsObjIndex { + const comp = wasm.base.comp; + const zcu = comp.zcu.?; + const ip = &zcu.intern_pool; + const gpa = comp.gpa; + assert(comp.config.output_mode == .Obj); - /// Returns the location of the symbol that represents this `Atom` - pub fn symbolLoc(atom: Atom) Wasm.SymbolLoc { - return .{ - .file = atom.file, - .index = atom.sym_index, - }; + if (orig_ptr_ty != .none) { + const abi_alignment = Zcu.Type.fromInterned(ip.typeOf(ip_index)).abiAlignment(zcu); + const explicit_alignment = ip.indexToKey(orig_ptr_ty).ptr_type.flags.alignment; + if (explicit_alignment.compare(.gt, abi_alignment)) { + const gop = try wasm.overaligned_uavs.getOrPut(gpa, ip_index); + gop.value_ptr.* = if (gop.found_existing) gop.value_ptr.maxStrict(explicit_alignment) else explicit_alignment; + } } - /// Resolves the relocations within the atom, writing the new value - /// at the calculated offset. - pub fn resolveRelocs(atom: *Atom, wasm: *const Wasm) void { - if (atom.relocs.items.len == 0) return; - const symbol_name = wasm.symbolLocName(atom.symbolLoc()); - log.debug("Resolving relocs in atom '{s}' count({d})", .{ - symbol_name, - atom.relocs.items.len, - }); + const gop = try wasm.uavs_obj.getOrPut(gpa, ip_index); + if (!gop.found_existing) gop.value_ptr.* = .{ + // Lowering the value is delayed to avoid recursion. + .code = undefined, + .relocs = undefined, + }; + return @enumFromInt(gop.index); +} - for (atom.relocs.items) |reloc| { - const value = atom.relocationValue(reloc, wasm); - log.debug("Relocating '{s}' referenced in '{s}' offset=0x{x:0>8} value={d}", .{ - wasm.symbolLocName(.{ - .file = atom.file, - .index = @enumFromInt(reloc.index), - }), - symbol_name, - reloc.offset, - value, - }); +pub fn refUavExe(wasm: *Wasm, ip_index: InternPool.Index, orig_ptr_ty: InternPool.Index) !UavsExeIndex { + const comp = wasm.base.comp; + const zcu = comp.zcu.?; + const ip = &zcu.intern_pool; + const gpa = comp.gpa; + assert(comp.config.output_mode != .Obj); - switch (reloc.relocation_type) { - .R_WASM_TABLE_INDEX_I32, - .R_WASM_FUNCTION_OFFSET_I32, - .R_WASM_GLOBAL_INDEX_I32, - .R_WASM_MEMORY_ADDR_I32, - .R_WASM_SECTION_OFFSET_I32, - => std.mem.writeInt(u32, atom.code.items[reloc.offset - atom.original_offset ..][0..4], @as(u32, @truncate(value)), .little), - .R_WASM_TABLE_INDEX_I64, - .R_WASM_MEMORY_ADDR_I64, - => std.mem.writeInt(u64, atom.code.items[reloc.offset - atom.original_offset ..][0..8], value, .little), - .R_WASM_GLOBAL_INDEX_LEB, - .R_WASM_EVENT_INDEX_LEB, - .R_WASM_FUNCTION_INDEX_LEB, - .R_WASM_MEMORY_ADDR_LEB, - .R_WASM_MEMORY_ADDR_SLEB, - .R_WASM_TABLE_INDEX_SLEB, - .R_WASM_TABLE_NUMBER_LEB, - .R_WASM_TYPE_INDEX_LEB, - .R_WASM_MEMORY_ADDR_TLS_SLEB, - => leb.writeUnsignedFixed(5, atom.code.items[reloc.offset - atom.original_offset ..][0..5], @as(u32, @truncate(value))), - .R_WASM_MEMORY_ADDR_LEB64, - .R_WASM_MEMORY_ADDR_SLEB64, - .R_WASM_TABLE_INDEX_SLEB64, - .R_WASM_MEMORY_ADDR_TLS_SLEB64, - => leb.writeUnsignedFixed(10, atom.code.items[reloc.offset - atom.original_offset ..][0..10], value), - } + if (orig_ptr_ty != .none) { + const abi_alignment = Zcu.Type.fromInterned(ip.typeOf(ip_index)).abiAlignment(zcu); + const explicit_alignment = ip.indexToKey(orig_ptr_ty).ptr_type.flags.alignment; + if (explicit_alignment.compare(.gt, abi_alignment)) { + const gop = try wasm.overaligned_uavs.getOrPut(gpa, ip_index); + gop.value_ptr.* = if (gop.found_existing) gop.value_ptr.maxStrict(explicit_alignment) else explicit_alignment; } } - /// From a given `relocation` will return the new value to be written. - /// All values will be represented as a `u64` as all values can fit within it. - /// The final value must be casted to the correct size. - fn relocationValue(atom: Atom, relocation: Relocation, wasm: *const Wasm) u64 { - const target_loc = wasm.symbolLocFinalLoc(.{ - .file = atom.file, - .index = @enumFromInt(relocation.index), - }); - const symbol = wasm.symbolLocSymbol(target_loc); - if (relocation.relocation_type != .R_WASM_TYPE_INDEX_LEB and - symbol.tag != .section and - symbol.isDead()) - { - const val = atom.tombstone(wasm) orelse relocation.addend; - return @bitCast(val); - } - switch (relocation.relocation_type) { - .R_WASM_FUNCTION_INDEX_LEB => return symbol.index, - .R_WASM_TABLE_NUMBER_LEB => return symbol.index, - .R_WASM_TABLE_INDEX_I32, - .R_WASM_TABLE_INDEX_I64, - .R_WASM_TABLE_INDEX_SLEB, - .R_WASM_TABLE_INDEX_SLEB64, - => return wasm.function_table.get(.{ .file = atom.file, .index = @enumFromInt(relocation.index) }) orelse 0, - .R_WASM_TYPE_INDEX_LEB => { - const object_id = atom.file.unwrap() orelse return relocation.index; - const original_type = objectFuncTypes(wasm, object_id)[relocation.index]; - return wasm.getTypeIndex(original_type).?; - }, - .R_WASM_GLOBAL_INDEX_I32, - .R_WASM_GLOBAL_INDEX_LEB, - => return symbol.index, - .R_WASM_MEMORY_ADDR_I32, - .R_WASM_MEMORY_ADDR_I64, - .R_WASM_MEMORY_ADDR_LEB, - .R_WASM_MEMORY_ADDR_LEB64, - .R_WASM_MEMORY_ADDR_SLEB, - .R_WASM_MEMORY_ADDR_SLEB64, - => { - std.debug.assert(symbol.tag == .data); - if (symbol.isUndefined()) { - return 0; - } - const va: i33 = @intCast(symbol.virtual_address); - return @intCast(va + relocation.addend); - }, - .R_WASM_EVENT_INDEX_LEB => return symbol.index, - .R_WASM_SECTION_OFFSET_I32 => { - const target_atom_index = wasm.symbol_atom.get(target_loc).?; - const target_atom = wasm.getAtom(target_atom_index); - const rel_value: i33 = @intCast(target_atom.offset); - return @intCast(rel_value + relocation.addend); - }, - .R_WASM_FUNCTION_OFFSET_I32 => { - if (symbol.isUndefined()) { - const val = atom.tombstone(wasm) orelse relocation.addend; - return @bitCast(val); - } - const target_atom_index = wasm.symbol_atom.get(target_loc).?; - const target_atom = wasm.getAtom(target_atom_index); - const rel_value: i33 = @intCast(target_atom.offset); - return @intCast(rel_value + relocation.addend); - }, - .R_WASM_MEMORY_ADDR_TLS_SLEB, - .R_WASM_MEMORY_ADDR_TLS_SLEB64, - => { - const va: i33 = @intCast(symbol.virtual_address); - return @intCast(va + relocation.addend); - }, - } - } - - // For a given `Atom` returns whether it has a tombstone value or not. - /// This defines whether we want a specific value when a section is dead. - fn tombstone(atom: Atom, wasm: *const Wasm) ?i64 { - const atom_name = wasm.symbolLocSymbol(atom.symbolLoc()).name; - if (atom_name == wasm.custom_sections.@".debug_ranges".name or - atom_name == wasm.custom_sections.@".debug_loc".name) - { - return -2; - } else if (std.mem.startsWith(u8, wasm.stringSlice(atom_name), ".debug_")) { - return -1; - } else { - return null; - } + const gop = try wasm.uavs_exe.getOrPut(gpa, ip_index); + if (gop.found_existing) { + gop.value_ptr.count += 1; + } else { + gop.value_ptr.* = .{ + // Lowering the value is delayed to avoid recursion. + .code = undefined, + .count = 1, + }; } -}; + return @enumFromInt(gop.index); +} -pub const Relocation = struct { - /// Represents the type of the `Relocation` - relocation_type: RelocationType, - /// Offset of the value to rewrite relative to the relevant section's contents. - /// When `offset` is zero, its position is immediately after the id and size of the section. - offset: u32, - /// The index of the symbol used. - /// When the type is `R_WASM_TYPE_INDEX_LEB`, it represents the index of the type. - index: u32, - /// Addend to add to the address. - /// This field is only non-zero for `R_WASM_MEMORY_ADDR_*`, `R_WASM_FUNCTION_OFFSET_I32` and `R_WASM_SECTION_OFFSET_I32`. - addend: i32 = 0, - - /// All possible relocation types currently existing. - /// This enum is exhaustive as the spec is WIP and new types - /// can be added which means that a generated binary will be invalid, - /// so instead we will show an error in such cases. - pub const RelocationType = enum(u8) { - R_WASM_FUNCTION_INDEX_LEB = 0, - R_WASM_TABLE_INDEX_SLEB = 1, - R_WASM_TABLE_INDEX_I32 = 2, - R_WASM_MEMORY_ADDR_LEB = 3, - R_WASM_MEMORY_ADDR_SLEB = 4, - R_WASM_MEMORY_ADDR_I32 = 5, - R_WASM_TYPE_INDEX_LEB = 6, - R_WASM_GLOBAL_INDEX_LEB = 7, - R_WASM_FUNCTION_OFFSET_I32 = 8, - R_WASM_SECTION_OFFSET_I32 = 9, - R_WASM_EVENT_INDEX_LEB = 10, - R_WASM_GLOBAL_INDEX_I32 = 13, - R_WASM_MEMORY_ADDR_LEB64 = 14, - R_WASM_MEMORY_ADDR_SLEB64 = 15, - R_WASM_MEMORY_ADDR_I64 = 16, - R_WASM_TABLE_INDEX_SLEB64 = 18, - R_WASM_TABLE_INDEX_I64 = 19, - R_WASM_TABLE_NUMBER_LEB = 20, - R_WASM_MEMORY_ADDR_TLS_SLEB = 21, - R_WASM_MEMORY_ADDR_TLS_SLEB64 = 25, - - /// Returns true for relocation types where the `addend` field is present. - pub fn addendIsPresent(self: RelocationType) bool { - return switch (self) { - .R_WASM_MEMORY_ADDR_LEB, - .R_WASM_MEMORY_ADDR_SLEB, - .R_WASM_MEMORY_ADDR_I32, - .R_WASM_MEMORY_ADDR_LEB64, - .R_WASM_MEMORY_ADDR_SLEB64, - .R_WASM_MEMORY_ADDR_I64, - .R_WASM_MEMORY_ADDR_TLS_SLEB, - .R_WASM_MEMORY_ADDR_TLS_SLEB64, - .R_WASM_FUNCTION_OFFSET_I32, - .R_WASM_SECTION_OFFSET_I32, - => true, - else => false, - }; - } +pub fn refNavObj(wasm: *Wasm, nav_index: InternPool.Nav.Index) !NavsObjIndex { + const comp = wasm.base.comp; + const gpa = comp.gpa; + assert(comp.config.output_mode != .Obj); + const gop = try wasm.navs_obj.getOrPut(gpa, nav_index); + if (!gop.found_existing) gop.value_ptr.* = .{ + // Lowering the value is delayed to avoid recursion. + .code = undefined, + .relocs = undefined, }; + return @enumFromInt(gop.index); +} - /// Verifies the relocation type of a given `Relocation` and returns - /// true when the relocation references a function call or address to a function. - pub fn isFunction(self: Relocation) bool { - return switch (self.relocation_type) { - .R_WASM_FUNCTION_INDEX_LEB, - .R_WASM_TABLE_INDEX_SLEB, - => true, - else => false, +pub fn refNavExe(wasm: *Wasm, nav_index: InternPool.Nav.Index) !NavsExeIndex { + const comp = wasm.base.comp; + const gpa = comp.gpa; + assert(comp.config.output_mode != .Obj); + const gop = try wasm.navs_exe.getOrPut(gpa, nav_index); + if (gop.found_existing) { + gop.value_ptr.count += 1; + } else { + gop.value_ptr.* = .{ + // Lowering the value is delayed to avoid recursion. + .code = undefined, + .count = 0, }; } + return @enumFromInt(gop.index); +} - pub fn format(self: Relocation, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { - _ = fmt; - _ = options; - try writer.print("{s} offset=0x{x:0>6} symbol={d}", .{ - @tagName(self.relocation_type), - self.offset, - self.index, - }); - } -}; - -/// Unlike the `Import` object defined by the wasm spec, and existing -/// in the std.wasm namespace, this construct saves the 'module name' and 'name' -/// of the import using offsets into a string table, rather than the slices itself. -/// This saves us (potentially) 24 bytes per import on 64bit machines. -pub const Import = struct { - module_name: String, - name: String, - kind: std.wasm.Import.Kind, -}; - -/// Unlike the `Export` object defined by the wasm spec, and existing -/// in the std.wasm namespace, this construct saves the 'name' -/// of the export using offsets into a string table, rather than the slice itself. -/// This saves us (potentially) 12 bytes per export on 64bit machines. -pub const Export = struct { - name: String, - index: u32, - kind: std.wasm.ExternalKind, -}; - -pub const SubsectionType = enum(u8) { - WASM_SEGMENT_INFO = 5, - WASM_INIT_FUNCS = 6, - WASM_COMDAT_INFO = 7, - WASM_SYMBOL_TABLE = 8, -}; - -pub const Alignment = @import("../InternPool.zig").Alignment; - -pub const NamedSegment = struct { - /// Segment's name, encoded as UTF-8 bytes. - name: []const u8, - /// The required alignment of the segment, encoded as a power of 2 - alignment: Alignment, - /// Bitfield containing flags for a segment - flags: u32, - - pub fn isTLS(segment: NamedSegment) bool { - return segment.flags & @intFromEnum(Flags.WASM_SEG_FLAG_TLS) != 0; - } - - /// Returns the name as how it will be output into the final object - /// file or binary. When `merge_segments` is true, this will return the - /// short name. i.e. ".rodata". When false, it returns the entire name instead. - pub fn outputName(segment: NamedSegment, merge_segments: bool) []const u8 { - if (segment.isTLS()) { - return ".tdata"; - } else if (!merge_segments) { - return segment.name; - } else if (std.mem.startsWith(u8, segment.name, ".rodata.")) { - return ".rodata"; - } else if (std.mem.startsWith(u8, segment.name, ".text.")) { - return ".text"; - } else if (std.mem.startsWith(u8, segment.name, ".data.")) { - return ".data"; - } else if (std.mem.startsWith(u8, segment.name, ".bss.")) { - return ".bss"; - } - return segment.name; - } - - pub const Flags = enum(u32) { - WASM_SEG_FLAG_STRINGS = 0x1, - WASM_SEG_FLAG_TLS = 0x2, - }; -}; - -pub const InitFunc = struct { - /// Priority of the init function - priority: u32, - /// The symbol index of init function (not the function index). - symbol_index: u32, -}; - -pub const Comdat = struct { - name: []const u8, - /// Must be zero, no flags are currently defined by the tool-convention. - flags: u32, - symbols: []const ComdatSym, -}; - -pub const ComdatSym = struct { - kind: @This().Type, - /// Index of the data segment/function/global/event/table within a WASM module. - /// The object must not be an import. - index: u32, - - pub const Type = enum(u8) { - WASM_COMDAT_DATA = 0, - WASM_COMDAT_FUNCTION = 1, - WASM_COMDAT_GLOBAL = 2, - WASM_COMDAT_EVENT = 3, - WASM_COMDAT_TABLE = 4, - WASM_COMDAT_SECTION = 5, - }; -}; - -pub const Feature = struct { - /// Provides information about the usage of the feature. - /// - '0x2b' (+): Object uses this feature, and the link fails if feature is not in the allowed set. - /// - '0x2d' (-): Object does not use this feature, and the link fails if this feature is in the allowed set. - /// - '0x3d' (=): Object uses this feature, and the link fails if this feature is not in the allowed set, - /// or if any object does not use this feature. - prefix: Prefix, - /// Type of the feature, must be unique in the sequence of features. - tag: Tag, - - /// Unlike `std.Target.wasm.Feature` this also contains linker-features such as shared-mem - pub const Tag = enum { - atomics, - bulk_memory, - exception_handling, - extended_const, - half_precision, - multimemory, - multivalue, - mutable_globals, - nontrapping_fptoint, - reference_types, - relaxed_simd, - sign_ext, - simd128, - tail_call, - shared_mem, - - /// From a given cpu feature, returns its linker feature - pub fn fromCpuFeature(feature: std.Target.wasm.Feature) Tag { - return @as(Tag, @enumFromInt(@intFromEnum(feature))); - } - - pub fn format(tag: Tag, comptime fmt: []const u8, opt: std.fmt.FormatOptions, writer: anytype) !void { - _ = fmt; - _ = opt; - try writer.writeAll(switch (tag) { - .atomics => "atomics", - .bulk_memory => "bulk-memory", - .exception_handling => "exception-handling", - .extended_const => "extended-const", - .half_precision => "half-precision", - .multimemory => "multimemory", - .multivalue => "multivalue", - .mutable_globals => "mutable-globals", - .nontrapping_fptoint => "nontrapping-fptoint", - .reference_types => "reference-types", - .relaxed_simd => "relaxed-simd", - .sign_ext => "sign-ext", - .simd128 => "simd128", - .tail_call => "tail-call", - .shared_mem => "shared-mem", - }); - } - }; - - pub const Prefix = enum(u8) { - used = '+', - disallowed = '-', - required = '=', - }; - - pub fn format(feature: Feature, comptime fmt: []const u8, opt: std.fmt.FormatOptions, writer: anytype) !void { - _ = opt; - _ = fmt; - try writer.print("{c} {}", .{ feature.prefix, feature.tag }); - } -}; +/// Asserts it is called after `Flush.data_segments` is fully populated and sorted. +pub fn uavAddr(wasm: *Wasm, uav_index: UavsExeIndex) u32 { + assert(wasm.flush_buffer.memory_layout_finished); + const comp = wasm.base.comp; + assert(comp.config.output_mode != .Obj); + const ds_id: DataSegmentId = .pack(wasm, .{ .uav_exe = uav_index }); + return wasm.flush_buffer.data_segments.get(ds_id).?; +} -pub const known_features = std.StaticStringMap(Feature.Tag).initComptime(.{ - .{ "atomics", .atomics }, - .{ "bulk-memory", .bulk_memory }, - .{ "exception-handling", .exception_handling }, - .{ "extended-const", .extended_const }, - .{ "half-precision", .half_precision }, - .{ "multimemory", .multimemory }, - .{ "multivalue", .multivalue }, - .{ "mutable-globals", .mutable_globals }, - .{ "nontrapping-fptoint", .nontrapping_fptoint }, - .{ "reference-types", .reference_types }, - .{ "relaxed-simd", .relaxed_simd }, - .{ "sign-ext", .sign_ext }, - .{ "simd128", .simd128 }, - .{ "tail-call", .tail_call }, - .{ "shared-mem", .shared_mem }, -}); - -/// Parses an object file into atoms, for code and data sections -fn parseSymbolIntoAtom(wasm: *Wasm, object_id: ObjectId, symbol_index: Symbol.Index) !Atom.Index { - const object = wasm.objectById(object_id) orelse - return wasm.zig_object.?.parseSymbolIntoAtom(wasm, symbol_index); +/// Asserts it is called after `Flush.data_segments` is fully populated and sorted. +pub fn navAddr(wasm: *Wasm, nav_index: InternPool.Nav.Index) u32 { + assert(wasm.flush_buffer.memory_layout_finished); const comp = wasm.base.comp; - const gpa = comp.gpa; - const symbol = &object.symtable[@intFromEnum(symbol_index)]; - const relocatable_data: Object.RelocatableData = switch (symbol.tag) { - .function => object.relocatable_data.get(.code).?[symbol.index - object.imported_functions_count], - .data => object.relocatable_data.get(.data).?[symbol.index], - .section => blk: { - const data = object.relocatable_data.get(.custom).?; - for (data) |dat| { - if (dat.section_index == symbol.index) { - break :blk dat; + assert(comp.config.output_mode != .Obj); + if (wasm.navs_exe.getIndex(nav_index)) |i| { + const navs_exe_index: NavsExeIndex = @enumFromInt(i); + log.debug("navAddr {s} {}", .{ navs_exe_index.name(wasm), nav_index }); + const ds_id: DataSegmentId = .pack(wasm, .{ .nav_exe = navs_exe_index }); + return wasm.flush_buffer.data_segments.get(ds_id).?; + } + const zcu = comp.zcu.?; + const ip = &zcu.intern_pool; + const nav = ip.getNav(nav_index); + if (nav.getResolvedExtern(ip)) |ext| { + if (wasm.getExistingString(ext.name.toSlice(ip))) |symbol_name| { + if (wasm.object_data_imports.getPtr(symbol_name)) |import| { + switch (import.resolution.unpack(wasm)) { + .unresolved => unreachable, + .object => |object_data_index| { + const object_data = object_data_index.ptr(wasm); + const ds_id: DataSegmentId = .fromObjectDataSegment(wasm, object_data.segment); + const segment_base_addr = wasm.flush_buffer.data_segments.get(ds_id).?; + return segment_base_addr + object_data.offset; + }, + .__zig_error_names => @panic("TODO"), + .__zig_error_name_table => @panic("TODO"), + .__heap_base => @panic("TODO"), + .__heap_end => @panic("TODO"), + .uav_exe => @panic("TODO"), + .uav_obj => @panic("TODO"), + .nav_exe => @panic("TODO"), + .nav_obj => @panic("TODO"), } } - unreachable; - }, - else => unreachable, - }; - const final_index = try wasm.getMatchingSegment(object_id, symbol_index); - const atom_index = try wasm.createAtom(symbol_index, object_id.toOptional()); - try wasm.appendAtomAtIndex(final_index, atom_index); - - const atom = wasm.getAtomPtr(atom_index); - atom.size = relocatable_data.size; - atom.alignment = relocatable_data.getAlignment(object); - atom.code = std.ArrayListUnmanaged(u8).fromOwnedSlice(relocatable_data.data[0..relocatable_data.size]); - atom.original_offset = relocatable_data.offset; - - const segment = wasm.segmentPtr(final_index); - if (relocatable_data.type == .data) { //code section and custom sections are 1-byte aligned - segment.alignment = segment.alignment.max(atom.alignment); - } - - if (object.relocations.get(relocatable_data.section_index)) |relocations| { - const start = searchRelocStart(relocations, relocatable_data.offset); - const len = searchRelocEnd(relocations[start..], relocatable_data.offset + atom.size); - atom.relocs = std.ArrayListUnmanaged(Wasm.Relocation).fromOwnedSlice(relocations[start..][0..len]); - for (atom.relocs.items) |reloc| { - switch (reloc.relocation_type) { - .R_WASM_TABLE_INDEX_I32, - .R_WASM_TABLE_INDEX_I64, - .R_WASM_TABLE_INDEX_SLEB, - .R_WASM_TABLE_INDEX_SLEB64, - => { - try wasm.function_table.put(gpa, .{ - .file = object_id.toOptional(), - .index = @enumFromInt(reloc.index), - }, 0); - }, - .R_WASM_GLOBAL_INDEX_I32, - .R_WASM_GLOBAL_INDEX_LEB, - => { - const sym = object.symtable[reloc.index]; - if (sym.tag != .global) { - try wasm.got_symbols.append(gpa, .{ - .file = object_id.toOptional(), - .index = @enumFromInt(reloc.index), - }); - } - }, - else => {}, - } } } + // Otherwise it's a zero bit type; any address will do. + return 0; +} - return atom_index; +/// Asserts it is called after `Flush.data_segments` is fully populated and sorted. +pub fn errorNameTableAddr(wasm: *Wasm) u32 { + assert(wasm.flush_buffer.memory_layout_finished); + const comp = wasm.base.comp; + assert(comp.config.output_mode != .Obj); + return wasm.flush_buffer.data_segments.get(.__zig_error_name_table).?; } -fn searchRelocStart(relocs: []const Wasm.Relocation, address: u32) usize { - var min: usize = 0; - var max: usize = relocs.len; - while (min < max) { - const index = (min + max) / 2; - const curr = relocs[index]; - if (curr.offset < address) { - min = index + 1; +fn convertZcuFnType( + comp: *Compilation, + cc: std.builtin.CallingConvention, + params: []const InternPool.Index, + return_type: Zcu.Type, + target: *const std.Target, + params_buffer: *std.ArrayListUnmanaged(std.wasm.Valtype), + returns_buffer: *std.ArrayListUnmanaged(std.wasm.Valtype), +) Allocator.Error!void { + params_buffer.clearRetainingCapacity(); + returns_buffer.clearRetainingCapacity(); + + const gpa = comp.gpa; + const zcu = comp.zcu.?; + + if (CodeGen.firstParamSRet(cc, return_type, zcu, target)) { + try params_buffer.append(gpa, .i32); // memory address is always a 32-bit handle + } else if (return_type.hasRuntimeBitsIgnoreComptime(zcu)) { + if (cc == .wasm_watc) { + const res_classes = abi.classifyType(return_type, zcu); + assert(res_classes[0] == .direct and res_classes[1] == .none); + const scalar_type = abi.scalarType(return_type, zcu); + try returns_buffer.append(gpa, CodeGen.typeToValtype(scalar_type, zcu, target)); } else { - max = index; + try returns_buffer.append(gpa, CodeGen.typeToValtype(return_type, zcu, target)); } + } else if (return_type.isError(zcu)) { + try returns_buffer.append(gpa, .i32); } - return min; -} -fn searchRelocEnd(relocs: []const Wasm.Relocation, address: u32) usize { - for (relocs, 0..relocs.len) |reloc, index| { - if (reloc.offset > address) { - return index; + // param types + for (params) |param_type_ip| { + const param_type = Zcu.Type.fromInterned(param_type_ip); + if (!param_type.hasRuntimeBitsIgnoreComptime(zcu)) continue; + + switch (cc) { + .wasm_watc => { + const param_classes = abi.classifyType(param_type, zcu); + if (param_classes[1] == .none) { + if (param_classes[0] == .direct) { + const scalar_type = abi.scalarType(param_type, zcu); + try params_buffer.append(gpa, CodeGen.typeToValtype(scalar_type, zcu, target)); + } else { + try params_buffer.append(gpa, CodeGen.typeToValtype(param_type, zcu, target)); + } + } else { + // i128/f128 + try params_buffer.append(gpa, .i64); + try params_buffer.append(gpa, .i64); + } + }, + else => try params_buffer.append(gpa, CodeGen.typeToValtype(param_type, zcu, target)), } } - return relocs.len; } -pub fn internString(wasm: *Wasm, bytes: []const u8) error{OutOfMemory}!String { - const gpa = wasm.base.comp.gpa; - const gop = try wasm.string_table.getOrPutContextAdapted( - gpa, - @as([]const u8, bytes), - @as(String.TableIndexAdapter, .{ .bytes = wasm.string_bytes.items }), - @as(String.TableContext, .{ .bytes = wasm.string_bytes.items }), - ); - if (gop.found_existing) return gop.key_ptr.*; - - try wasm.string_bytes.ensureUnusedCapacity(gpa, bytes.len + 1); - const new_off: String = @enumFromInt(wasm.string_bytes.items.len); +pub fn isBss(wasm: *const Wasm, optional_name: OptionalString) bool { + const s = optional_name.slice(wasm) orelse return false; + return mem.eql(u8, s, ".bss") or mem.startsWith(u8, s, ".bss."); +} - wasm.string_bytes.appendSliceAssumeCapacity(bytes); - wasm.string_bytes.appendAssumeCapacity(0); +/// After this function is called, there may be additional entries in +/// `Wasm.uavs_obj`, `Wasm.uavs_exe`, `Wasm.navs_obj`, and `Wasm.navs_exe` +/// which have uninitialized code and relocations. This function is +/// non-recursive, so callers must coordinate additional calls to populate +/// those entries. +fn lowerZcuData(wasm: *Wasm, pt: Zcu.PerThread, ip_index: InternPool.Index) !ZcuDataObj { + const code_start: u32 = @intCast(wasm.string_bytes.items.len); + const relocs_start: u32 = @intCast(wasm.out_relocs.len); + const uav_fixups_start: u32 = @intCast(wasm.uav_fixups.items.len); + const nav_fixups_start: u32 = @intCast(wasm.nav_fixups.items.len); + const func_table_fixups_start: u32 = @intCast(wasm.func_table_fixups.items.len); + wasm.string_bytes_lock.lock(); + + try codegen.generateSymbol(&wasm.base, pt, .unneeded, .fromInterned(ip_index), &wasm.string_bytes, .none); + + const code_len: u32 = @intCast(wasm.string_bytes.items.len - code_start); + const relocs_len: u32 = @intCast(wasm.out_relocs.len - relocs_start); + const any_fixups = + uav_fixups_start != wasm.uav_fixups.items.len or + nav_fixups_start != wasm.nav_fixups.items.len or + func_table_fixups_start != wasm.func_table_fixups.items.len; + wasm.string_bytes_lock.unlock(); + + const naive_code: DataPayload = .{ + .off = @enumFromInt(code_start), + .len = code_len, + }; - gop.key_ptr.* = new_off; + // Only nonzero init values need to take up space in the output. + // If any fixups are present, we still need the string bytes allocated since + // that is the staging area for the fixups. + const code: DataPayload = if (!any_fixups and std.mem.allEqual(u8, naive_code.slice(wasm), 0)) c: { + wasm.string_bytes.shrinkRetainingCapacity(code_start); + // Indicate empty by making off and len the same value, however, still + // transmit the data size by using the size as that value. + break :c .{ + .off = .none, + .len = naive_code.len, + }; + } else c: { + wasm.any_passive_inits = wasm.any_passive_inits or wasm.base.comp.config.import_memory; + break :c naive_code; + }; - return new_off; + return .{ + .code = code, + .relocs = .{ + .off = relocs_start, + .len = relocs_len, + }, + }; } -pub fn getExistingString(wasm: *const Wasm, bytes: []const u8) ?String { - return wasm.string_table.getKeyAdapted(bytes, @as(String.TableIndexAdapter, .{ - .bytes = wasm.string_bytes.items, - })); +fn pointerAlignment(wasm: *const Wasm) Alignment { + const target = &wasm.base.comp.root_mod.resolved_target.result; + return switch (target.cpu.arch) { + .wasm32 => .@"4", + .wasm64 => .@"8", + else => unreachable, + }; } -pub fn stringSlice(wasm: *const Wasm, index: String) [:0]const u8 { - const slice = wasm.string_bytes.items[@intFromEnum(index)..]; - return slice[0..mem.indexOfScalar(u8, slice, 0).? :0]; +fn pointerSize(wasm: *const Wasm) u32 { + const target = &wasm.base.comp.root_mod.resolved_target.result; + return switch (target.cpu.arch) { + .wasm32 => 4, + .wasm64 => 8, + else => unreachable, + }; } -pub fn optionalStringSlice(wasm: *const Wasm, index: OptionalString) ?[:0]const u8 { - return stringSlice(wasm, index.unwrap() orelse return null); +fn addZcuImportReserved(wasm: *Wasm, nav_index: InternPool.Nav.Index) ZcuImportIndex { + const gop = wasm.imports.getOrPutAssumeCapacity(nav_index); + gop.value_ptr.* = {}; + return @enumFromInt(gop.index); } -pub fn castToString(wasm: *const Wasm, index: u32) String { - assert(index == 0 or wasm.string_bytes.items[index - 1] == 0); - return @enumFromInt(index); +fn resolveFunctionSynthetic( + wasm: *Wasm, + import: *FunctionImport, + res: FunctionImport.Resolution, + params: []const std.wasm.Valtype, + returns: []const std.wasm.Valtype, +) link.File.FlushError!void { + import.resolution = res; + wasm.functions.putAssumeCapacity(res, {}); + // This is not only used for type-checking but also ensures the function + // type index is interned so that it is guaranteed to exist during `flush`. + const correct_func_type = try addFuncType(wasm, .{ + .params = try internValtypeList(wasm, params), + .returns = try internValtypeList(wasm, returns), + }); + if (import.type != correct_func_type) { + const diags = &wasm.base.comp.link_diags; + return import.source_location.fail(diags, "synthetic function {s} {} imported with incorrect signature {}", .{ + @tagName(res), correct_func_type.fmt(wasm), import.type.fmt(wasm), + }); + } } -fn segmentPtr(wasm: *const Wasm, index: Segment.Index) *Segment { - return &wasm.segments.items[@intFromEnum(index)]; +pub fn addFunction( + wasm: *Wasm, + resolution: FunctionImport.Resolution, + params: []const std.wasm.Valtype, + returns: []const std.wasm.Valtype, +) Allocator.Error!void { + wasm.functions.putAssumeCapacity(resolution, {}); + _ = try wasm.addFuncType(.{ + .params = try wasm.internValtypeList(params), + .returns = try wasm.internValtypeList(returns), + }); } diff --git a/src/link/Wasm/Archive.zig b/src/link/Wasm/Archive.zig index c2078fa525..3ecdedce8a 100644 --- a/src/link/Wasm/Archive.zig +++ b/src/link/Wasm/Archive.zig @@ -142,8 +142,18 @@ pub fn parse(gpa: Allocator, file_contents: []const u8) !Archive { /// From a given file offset, starts reading for a file header. /// When found, parses the object file into an `Object` and returns it. -pub fn parseObject(archive: Archive, wasm: *Wasm, file_contents: []const u8, path: Path) !Object { - const header = mem.bytesAsValue(Header, file_contents[0..@sizeOf(Header)]); +pub fn parseObject( + archive: Archive, + wasm: *Wasm, + file_contents: []const u8, + object_offset: u32, + path: Path, + host_name: Wasm.OptionalString, + scratch_space: *Object.ScratchSpace, + must_link: bool, + gc_sections: bool, +) !Object { + const header = mem.bytesAsValue(Header, file_contents[object_offset..][0..@sizeOf(Header)]); if (!mem.eql(u8, &header.fmag, ARFMAG)) return error.BadHeaderDelimiter; const name_or_index = try header.nameOrIndex(); @@ -157,8 +167,9 @@ pub fn parseObject(archive: Archive, wasm: *Wasm, file_contents: []const u8, pat }; const object_file_size = try header.parsedSize(); + const contents = file_contents[object_offset + @sizeOf(Header) ..][0..object_file_size]; - return Object.create(wasm, file_contents[@sizeOf(Header)..][0..object_file_size], path, object_name); + return Object.parse(wasm, contents, path, object_name, host_name, scratch_space, must_link, gc_sections); } const Archive = @This(); diff --git a/src/link/Wasm/Flush.zig b/src/link/Wasm/Flush.zig new file mode 100644 index 0000000000..f862bcf427 --- /dev/null +++ b/src/link/Wasm/Flush.zig @@ -0,0 +1,1975 @@ +//! Temporary, dynamically allocated structures used only during flush. +//! Could be constructed fresh each time, or kept around between updates to reduce heap allocations. + +const Flush = @This(); +const Wasm = @import("../Wasm.zig"); +const Object = @import("Object.zig"); +const Zcu = @import("../../Zcu.zig"); +const Alignment = Wasm.Alignment; +const String = Wasm.String; +const Relocation = Wasm.Relocation; +const InternPool = @import("../../InternPool.zig"); + +const build_options = @import("build_options"); + +const std = @import("std"); +const Allocator = std.mem.Allocator; +const mem = std.mem; +const leb = std.leb; +const log = std.log.scoped(.link); +const assert = std.debug.assert; + +/// Ordered list of data segments that will appear in the final binary. +/// When sorted, to-be-merged segments will be made adjacent. +/// Values are virtual address. +data_segments: std.AutoArrayHashMapUnmanaged(Wasm.DataSegmentId, u32) = .empty, +/// Each time a `data_segment` offset equals zero it indicates a new group, and +/// the next element in this array will contain the total merged segment size. +/// Value is the virtual memory address of the end of the segment. +data_segment_groups: std.ArrayListUnmanaged(DataSegmentGroup) = .empty, + +binary_bytes: std.ArrayListUnmanaged(u8) = .empty, +missing_exports: std.AutoArrayHashMapUnmanaged(String, void) = .empty, +function_imports: std.AutoArrayHashMapUnmanaged(String, Wasm.FunctionImportId) = .empty, +global_imports: std.AutoArrayHashMapUnmanaged(String, Wasm.GlobalImportId) = .empty, +data_imports: std.AutoArrayHashMapUnmanaged(String, Wasm.DataImportId) = .empty, + +indirect_function_table: std.AutoArrayHashMapUnmanaged(Wasm.OutputFunctionIndex, void) = .empty, + +/// A subset of the full interned function type list created only during flush. +func_types: std.AutoArrayHashMapUnmanaged(Wasm.FunctionType.Index, void) = .empty, + +/// For debug purposes only. +memory_layout_finished: bool = false, + +/// Index into `func_types`. +pub const FuncTypeIndex = enum(u32) { + _, + + pub fn fromTypeIndex(i: Wasm.FunctionType.Index, f: *const Flush) FuncTypeIndex { + return @enumFromInt(f.func_types.getIndex(i).?); + } +}; + +/// Index into `indirect_function_table`. +const IndirectFunctionTableIndex = enum(u32) { + _, + + fn fromObjectFunctionHandlingWeak(wasm: *const Wasm, index: Wasm.ObjectFunctionIndex) IndirectFunctionTableIndex { + return fromOutputFunctionIndex(&wasm.flush_buffer, .fromObjectFunctionHandlingWeak(wasm, index)); + } + + fn fromSymbolName(wasm: *const Wasm, name: String) IndirectFunctionTableIndex { + return fromOutputFunctionIndex(&wasm.flush_buffer, .fromSymbolName(wasm, name)); + } + + fn fromOutputFunctionIndex(f: *const Flush, i: Wasm.OutputFunctionIndex) IndirectFunctionTableIndex { + return @enumFromInt(f.indirect_function_table.getIndex(i).?); + } + + fn fromZcuIndirectFunctionSetIndex(i: Wasm.ZcuIndirectFunctionSetIndex) IndirectFunctionTableIndex { + // These are the same since those are added to the table first. + return @enumFromInt(@intFromEnum(i)); + } + + fn toAbi(i: IndirectFunctionTableIndex) u32 { + return @intFromEnum(i) + 1; + } +}; + +const DataSegmentGroup = struct { + first_segment: Wasm.DataSegmentId, + end_addr: u32, +}; + +pub fn clear(f: *Flush) void { + f.data_segments.clearRetainingCapacity(); + f.data_segment_groups.clearRetainingCapacity(); + f.binary_bytes.clearRetainingCapacity(); + f.indirect_function_table.clearRetainingCapacity(); + f.func_types.clearRetainingCapacity(); + f.memory_layout_finished = false; +} + +pub fn deinit(f: *Flush, gpa: Allocator) void { + f.data_segments.deinit(gpa); + f.data_segment_groups.deinit(gpa); + f.binary_bytes.deinit(gpa); + f.missing_exports.deinit(gpa); + f.function_imports.deinit(gpa); + f.global_imports.deinit(gpa); + f.data_imports.deinit(gpa); + f.indirect_function_table.deinit(gpa); + f.func_types.deinit(gpa); + f.* = undefined; +} + +pub fn finish(f: *Flush, wasm: *Wasm) !void { + const comp = wasm.base.comp; + const shared_memory = comp.config.shared_memory; + const diags = &comp.link_diags; + const gpa = comp.gpa; + const import_memory = comp.config.import_memory; + const export_memory = comp.config.export_memory; + const target = &comp.root_mod.resolved_target.result; + const is64 = switch (target.cpu.arch) { + .wasm32 => false, + .wasm64 => true, + else => unreachable, + }; + const is_obj = comp.config.output_mode == .Obj; + const allow_undefined = is_obj or wasm.import_symbols; + + const entry_name = if (wasm.entry_resolution.isNavOrUnresolved(wasm)) wasm.entry_name else .none; + + if (comp.zcu) |zcu| { + const ip: *const InternPool = &zcu.intern_pool; // No mutations allowed! + + // Detect any intrinsics that were called; they need to have dependencies on the symbols marked. + // Likewise detect `@tagName` calls so those functions can be included in the output and synthesized. + for (wasm.mir_instructions.items(.tag), wasm.mir_instructions.items(.data)) |tag, *data| switch (tag) { + .call_intrinsic => { + const symbol_name = try wasm.internString(@tagName(data.intrinsic)); + const i: Wasm.FunctionImport.Index = @enumFromInt(wasm.object_function_imports.getIndex(symbol_name) orelse { + return diags.fail("missing compiler runtime intrinsic '{s}' (undefined linker symbol)", .{ + @tagName(data.intrinsic), + }); + }); + try wasm.markFunctionImport(symbol_name, i.value(wasm), i); + }, + .call_tag_name => { + assert(ip.indexToKey(data.ip_index) == .enum_type); + const gop = try wasm.zcu_funcs.getOrPut(gpa, data.ip_index); + if (!gop.found_existing) { + wasm.tag_name_table_ref_count += 1; + const int_tag_ty = Zcu.Type.fromInterned(data.ip_index).intTagType(zcu); + gop.value_ptr.* = .{ .tag_name = .{ + .symbol_name = try wasm.internStringFmt("__zig_tag_name_{d}", .{@intFromEnum(data.ip_index)}), + .type_index = try wasm.internFunctionType(.Unspecified, &.{int_tag_ty.ip_index}, .slice_const_u8_sentinel_0, target), + .table_index = @intCast(wasm.tag_name_offs.items.len), + } }; + try wasm.functions.put(gpa, .fromZcuFunc(wasm, @enumFromInt(gop.index)), {}); + const tag_names = ip.loadEnumType(data.ip_index).names; + for (tag_names.get(ip)) |tag_name| { + const slice = tag_name.toSlice(ip); + try wasm.tag_name_offs.append(gpa, @intCast(wasm.tag_name_bytes.items.len)); + try wasm.tag_name_bytes.appendSlice(gpa, slice[0 .. slice.len + 1]); + } + } + }, + else => continue, + }; + + { + var i = wasm.function_imports_len_prelink; + while (i < f.function_imports.entries.len) { + const symbol_name = f.function_imports.keys()[i]; + if (wasm.object_function_imports.getIndex(symbol_name)) |import_index_usize| { + const import_index: Wasm.FunctionImport.Index = @enumFromInt(import_index_usize); + try wasm.markFunctionImport(symbol_name, import_index.value(wasm), import_index); + f.function_imports.swapRemoveAt(i); + continue; + } + i += 1; + } + } + + { + var i = wasm.data_imports_len_prelink; + while (i < f.data_imports.entries.len) { + const symbol_name = f.data_imports.keys()[i]; + if (wasm.object_data_imports.getIndex(symbol_name)) |import_index_usize| { + const import_index: Wasm.ObjectDataImport.Index = @enumFromInt(import_index_usize); + try wasm.markDataImport(symbol_name, import_index.value(wasm), import_index); + f.data_imports.swapRemoveAt(i); + continue; + } + i += 1; + } + } + + if (wasm.error_name_table_ref_count > 0) { + // Ensure Zcu error name structures are populated. + const full_error_names = ip.global_error_set.getNamesFromMainThread(); + try wasm.error_name_offs.ensureTotalCapacity(gpa, full_error_names.len + 1); + if (wasm.error_name_offs.items.len == 0) { + // Dummy entry at index 0 to avoid a sub instruction at `@errorName` sites. + wasm.error_name_offs.appendAssumeCapacity(0); + } + const new_error_names = full_error_names[wasm.error_name_offs.items.len - 1 ..]; + for (new_error_names) |error_name| { + wasm.error_name_offs.appendAssumeCapacity(@intCast(wasm.error_name_bytes.items.len)); + const s: [:0]const u8 = error_name.toSlice(ip); + try wasm.error_name_bytes.appendSlice(gpa, s[0 .. s.len + 1]); + } + } + + for (wasm.nav_exports.keys(), wasm.nav_exports.values()) |*nav_export, export_index| { + if (ip.isFunctionType(ip.getNav(nav_export.nav_index).typeOf(ip))) { + log.debug("flush export '{s}' nav={d}", .{ nav_export.name.slice(wasm), nav_export.nav_index }); + const function_index = Wasm.FunctionIndex.fromIpNav(wasm, nav_export.nav_index).?; + const explicit = f.missing_exports.swapRemove(nav_export.name); + const is_hidden = !explicit and switch (export_index.ptr(zcu).opts.visibility) { + .hidden => true, + .default, .protected => false, + }; + if (is_hidden) { + try wasm.hidden_function_exports.put(gpa, nav_export.name, function_index); + } else { + try wasm.function_exports.put(gpa, nav_export.name, function_index); + } + _ = f.function_imports.swapRemove(nav_export.name); + + if (nav_export.name.toOptional() == entry_name) + wasm.entry_resolution = .fromIpNav(wasm, nav_export.nav_index); + } else { + // This is a data export because Zcu currently has no way to + // export wasm globals. + _ = f.missing_exports.swapRemove(nav_export.name); + _ = f.data_imports.swapRemove(nav_export.name); + if (!is_obj) { + diags.addError("unable to export data symbol '{s}'; not emitting a relocatable", .{ + nav_export.name.slice(wasm), + }); + } + } + } + + for (f.missing_exports.keys()) |exp_name| { + diags.addError("manually specified export name '{s}' undefined", .{exp_name.slice(wasm)}); + } + } + + if (entry_name.unwrap()) |name| { + if (wasm.entry_resolution == .unresolved) { + var err = try diags.addErrorWithNotes(1); + try err.addMsg("entry symbol '{s}' missing", .{name.slice(wasm)}); + err.addNote("'-fno-entry' suppresses this error", .{}); + } + } + + if (!allow_undefined) { + for (f.function_imports.keys(), f.function_imports.values()) |name, function_import_id| { + if (function_import_id.undefinedAllowed(wasm)) continue; + const src_loc = function_import_id.sourceLocation(wasm); + src_loc.addError(wasm, "undefined function: {s}", .{name.slice(wasm)}); + } + for (f.global_imports.keys(), f.global_imports.values()) |name, global_import_id| { + const src_loc = global_import_id.sourceLocation(wasm); + src_loc.addError(wasm, "undefined global: {s}", .{name.slice(wasm)}); + } + for (wasm.table_imports.keys(), wasm.table_imports.values()) |name, table_import_id| { + const src_loc = table_import_id.value(wasm).source_location; + src_loc.addError(wasm, "undefined table: {s}", .{name.slice(wasm)}); + } + for (f.data_imports.keys(), f.data_imports.values()) |name, data_import_id| { + const src_loc = data_import_id.sourceLocation(wasm); + src_loc.addError(wasm, "undefined data: {s}", .{name.slice(wasm)}); + } + } + + if (diags.hasErrors()) return error.LinkFailure; + + // Merge indirect function tables. + try f.indirect_function_table.ensureUnusedCapacity(gpa, wasm.zcu_indirect_function_set.entries.len + + wasm.object_indirect_function_import_set.entries.len + wasm.object_indirect_function_set.entries.len); + // This one goes first so the indexes can be stable for MIR lowering. + for (wasm.zcu_indirect_function_set.keys()) |nav_index| + f.indirect_function_table.putAssumeCapacity(.fromIpNav(wasm, nav_index), {}); + for (wasm.object_indirect_function_import_set.keys()) |symbol_name| + f.indirect_function_table.putAssumeCapacity(.fromSymbolName(wasm, symbol_name), {}); + for (wasm.object_indirect_function_set.keys()) |object_function_index| + f.indirect_function_table.putAssumeCapacity(.fromObjectFunction(wasm, object_function_index), {}); + + if (wasm.object_init_funcs.items.len > 0) { + // Zig has no constructors so these are only for object file inputs. + mem.sortUnstable(Wasm.InitFunc, wasm.object_init_funcs.items, {}, Wasm.InitFunc.lessThan); + try wasm.functions.put(gpa, .__wasm_call_ctors, {}); + } + + // Merge and order the data segments. Depends on garbage collection so that + // unused segments can be omitted. + try f.data_segments.ensureUnusedCapacity(gpa, wasm.data_segments.entries.len + + wasm.uavs_obj.entries.len + wasm.navs_obj.entries.len + + wasm.uavs_exe.entries.len + wasm.navs_exe.entries.len + 4); + if (is_obj) assert(wasm.uavs_exe.entries.len == 0); + if (is_obj) assert(wasm.navs_exe.entries.len == 0); + if (!is_obj) assert(wasm.uavs_obj.entries.len == 0); + if (!is_obj) assert(wasm.navs_obj.entries.len == 0); + for (0..wasm.uavs_obj.entries.len) |uavs_index| f.data_segments.putAssumeCapacityNoClobber(.pack(wasm, .{ + .uav_obj = @enumFromInt(uavs_index), + }), @as(u32, undefined)); + for (0..wasm.navs_obj.entries.len) |navs_index| f.data_segments.putAssumeCapacityNoClobber(.pack(wasm, .{ + .nav_obj = @enumFromInt(navs_index), + }), @as(u32, undefined)); + for (0..wasm.uavs_exe.entries.len) |uavs_index| f.data_segments.putAssumeCapacityNoClobber(.pack(wasm, .{ + .uav_exe = @enumFromInt(uavs_index), + }), @as(u32, undefined)); + for (0..wasm.navs_exe.entries.len) |navs_index| f.data_segments.putAssumeCapacityNoClobber(.pack(wasm, .{ + .nav_exe = @enumFromInt(navs_index), + }), @as(u32, undefined)); + if (wasm.error_name_table_ref_count > 0) { + f.data_segments.putAssumeCapacity(.__zig_error_names, @as(u32, undefined)); + f.data_segments.putAssumeCapacity(.__zig_error_name_table, @as(u32, undefined)); + } + if (wasm.tag_name_table_ref_count > 0) { + f.data_segments.putAssumeCapacity(.__zig_tag_names, @as(u32, undefined)); + f.data_segments.putAssumeCapacity(.__zig_tag_name_table, @as(u32, undefined)); + } + for (wasm.data_segments.keys()) |data_id| f.data_segments.putAssumeCapacity(data_id, @as(u32, undefined)); + + try wasm.functions.ensureUnusedCapacity(gpa, 3); + + // Passive segments are used to avoid memory being reinitialized on each + // thread's instantiation. These passive segments are initialized and + // dropped in __wasm_init_memory, which is registered as the start function + // We also initialize bss segments (using memory.fill) as part of this + // function. + if (wasm.any_passive_inits) { + try wasm.addFunction(.__wasm_init_memory, &.{}, &.{}); + } + + try wasm.tables.ensureUnusedCapacity(gpa, 1); + + if (f.indirect_function_table.entries.len > 0) { + wasm.tables.putAssumeCapacity(.__indirect_function_table, {}); + } + + // Sort order: + // 0. Segment category (tls, data, zero) + // 1. Segment name prefix + // 2. Segment alignment + // 3. Reference count, descending (optimize for LEB encoding) + // 4. Segment name suffix + // 5. Segment ID interpreted as an integer (for determinism) + // + // TLS segments are intended to be merged with each other, and segments + // with a common prefix name are intended to be merged with each other. + // Sorting ensures the segments intended to be merged will be adjacent. + // + // Each Zcu Nav and Cau has an independent data segment ID in this logic. + // For the purposes of sorting, they are implicitly all named ".data". + const Sort = struct { + wasm: *const Wasm, + segments: []const Wasm.DataSegmentId, + pub fn lessThan(ctx: @This(), lhs: usize, rhs: usize) bool { + const lhs_segment = ctx.segments[lhs]; + const rhs_segment = ctx.segments[rhs]; + const lhs_category = @intFromEnum(lhs_segment.category(ctx.wasm)); + const rhs_category = @intFromEnum(rhs_segment.category(ctx.wasm)); + switch (std.math.order(lhs_category, rhs_category)) { + .lt => return true, + .gt => return false, + .eq => {}, + } + const lhs_segment_name = lhs_segment.name(ctx.wasm); + const rhs_segment_name = rhs_segment.name(ctx.wasm); + const lhs_prefix, const lhs_suffix = splitSegmentName(lhs_segment_name); + const rhs_prefix, const rhs_suffix = splitSegmentName(rhs_segment_name); + switch (mem.order(u8, lhs_prefix, rhs_prefix)) { + .lt => return true, + .gt => return false, + .eq => {}, + } + const lhs_alignment = lhs_segment.alignment(ctx.wasm); + const rhs_alignment = rhs_segment.alignment(ctx.wasm); + switch (lhs_alignment.order(rhs_alignment)) { + .lt => return false, + .gt => return true, + .eq => {}, + } + switch (std.math.order(lhs_segment.refCount(ctx.wasm), rhs_segment.refCount(ctx.wasm))) { + .lt => return false, + .gt => return true, + .eq => {}, + } + switch (mem.order(u8, lhs_suffix, rhs_suffix)) { + .lt => return true, + .gt => return false, + .eq => {}, + } + return @intFromEnum(lhs_segment) < @intFromEnum(rhs_segment); + } + }; + f.data_segments.sortUnstable(@as(Sort, .{ + .wasm = wasm, + .segments = f.data_segments.keys(), + })); + + const page_size = std.wasm.page_size; // 64kb + const stack_alignment: Alignment = .@"16"; // wasm's stack alignment as specified by tool-convention + const heap_alignment: Alignment = .@"16"; // wasm's heap alignment as specified by tool-convention + const pointer_alignment: Alignment = .@"4"; + // Always place the stack at the start by default unless the user specified the global-base flag. + const place_stack_first, var memory_ptr: u64 = if (wasm.global_base) |base| .{ false, base } else .{ true, 0 }; + + var virtual_addrs: VirtualAddrs = .{ + .stack_pointer = undefined, + .heap_base = undefined, + .heap_end = undefined, + .tls_base = null, + .tls_align = .none, + .tls_size = null, + .init_memory_flag = null, + }; + + if (place_stack_first and !is_obj) { + memory_ptr = stack_alignment.forward(memory_ptr); + memory_ptr += wasm.base.stack_size; + virtual_addrs.stack_pointer = @intCast(memory_ptr); + } + + const segment_ids = f.data_segments.keys(); + const segment_vaddrs = f.data_segments.values(); + assert(f.data_segment_groups.items.len == 0); + const data_vaddr: u32 = @intCast(memory_ptr); + if (segment_ids.len > 0) { + var seen_tls: enum { before, during, after } = .before; + var category: Wasm.DataSegmentId.Category = undefined; + var first_segment: Wasm.DataSegmentId = segment_ids[0]; + for (segment_ids, segment_vaddrs, 0..) |segment_id, *segment_vaddr, i| { + const alignment = segment_id.alignment(wasm); + category = segment_id.category(wasm); + const start_addr = alignment.forward(memory_ptr); + + const want_new_segment = b: { + if (is_obj) break :b false; + switch (seen_tls) { + .before => switch (category) { + .tls => { + virtual_addrs.tls_base = if (shared_memory) 0 else @intCast(start_addr); + virtual_addrs.tls_align = alignment; + seen_tls = .during; + break :b f.data_segment_groups.items.len > 0; + }, + else => {}, + }, + .during => switch (category) { + .tls => { + virtual_addrs.tls_align = virtual_addrs.tls_align.maxStrict(alignment); + virtual_addrs.tls_size = @intCast(memory_ptr - virtual_addrs.tls_base.?); + break :b false; + }, + else => { + seen_tls = .after; + break :b true; + }, + }, + .after => {}, + } + break :b i >= 1 and !wantSegmentMerge(wasm, segment_ids[i - 1], segment_id, category); + }; + if (want_new_segment) { + log.debug("new segment group at 0x{x} {} {s} {}", .{ start_addr, segment_id, segment_id.name(wasm), category }); + try f.data_segment_groups.append(gpa, .{ + .end_addr = @intCast(memory_ptr), + .first_segment = first_segment, + }); + first_segment = segment_id; + } + + const size = segment_id.size(wasm); + segment_vaddr.* = @intCast(start_addr); + log.debug("0x{x} {d} {s}", .{ start_addr, @intFromEnum(segment_id), segment_id.name(wasm) }); + memory_ptr = start_addr + size; + } + if (category != .zero) try f.data_segment_groups.append(gpa, .{ + .first_segment = first_segment, + .end_addr = @intCast(memory_ptr), + }); + if (category == .tls and seen_tls == .during) { + virtual_addrs.tls_size = @intCast(memory_ptr - virtual_addrs.tls_base.?); + } + } + + if (shared_memory and wasm.any_passive_inits) { + memory_ptr = pointer_alignment.forward(memory_ptr); + virtual_addrs.init_memory_flag = @intCast(memory_ptr); + memory_ptr += 4; + } + + if (!place_stack_first and !is_obj) { + memory_ptr = stack_alignment.forward(memory_ptr); + memory_ptr += wasm.base.stack_size; + virtual_addrs.stack_pointer = @intCast(memory_ptr); + } + + memory_ptr = heap_alignment.forward(memory_ptr); + virtual_addrs.heap_base = @intCast(memory_ptr); + + if (wasm.initial_memory) |initial_memory| { + if (!mem.isAlignedGeneric(u64, initial_memory, page_size)) { + diags.addError("initial memory value {d} is not {d}-byte aligned", .{ initial_memory, page_size }); + } + if (memory_ptr > initial_memory) { + diags.addError("initial memory value {d} insufficient; minimum {d}", .{ initial_memory, memory_ptr }); + } + if (initial_memory > std.math.maxInt(u32)) { + diags.addError("initial memory value {d} exceeds 32-bit address space", .{initial_memory}); + } + if (diags.hasErrors()) return error.LinkFailure; + memory_ptr = initial_memory; + } else { + memory_ptr = mem.alignForward(u64, memory_ptr, std.wasm.page_size); + } + virtual_addrs.heap_end = @intCast(memory_ptr); + + // In case we do not import memory, but define it ourselves, set the + // minimum amount of pages on the memory section. + wasm.memories.limits.min = @intCast(memory_ptr / page_size); + log.debug("total memory pages: {d}", .{wasm.memories.limits.min}); + + if (wasm.max_memory) |max_memory| { + if (!mem.isAlignedGeneric(u64, max_memory, page_size)) { + diags.addError("maximum memory value {d} is not {d}-byte aligned", .{ max_memory, page_size }); + } + if (memory_ptr > max_memory) { + diags.addError("maximum memory value {d} insufficient; minimum {d}", .{ max_memory, memory_ptr }); + } + if (max_memory > std.math.maxInt(u32)) { + diags.addError("maximum memory value {d} exceeds 32-bit address space", .{max_memory}); + } + if (diags.hasErrors()) return error.LinkFailure; + wasm.memories.limits.max = @intCast(max_memory / page_size); + wasm.memories.limits.flags.has_max = true; + if (shared_memory) wasm.memories.limits.flags.is_shared = true; + log.debug("maximum memory pages: {?d}", .{wasm.memories.limits.max}); + } + f.memory_layout_finished = true; + + // When we have TLS GOT entries and shared memory is enabled, we must + // perform runtime relocations or else we don't create the function. + if (shared_memory and virtual_addrs.tls_base != null) { + // This logic that checks `any_tls_relocs` is missing the part where it + // also notices threadlocal globals from Zcu code. + if (wasm.any_tls_relocs) try wasm.addFunction(.__wasm_apply_global_tls_relocs, &.{}, &.{}); + try wasm.addFunction(.__wasm_init_tls, &.{.i32}, &.{}); + try wasm.globals.ensureUnusedCapacity(gpa, 3); + wasm.globals.putAssumeCapacity(.__tls_base, {}); + wasm.globals.putAssumeCapacity(.__tls_size, {}); + wasm.globals.putAssumeCapacity(.__tls_align, {}); + } + + var section_index: u32 = 0; + // Index of the code section. Used to tell relocation table where the section lives. + var code_section_index: ?u32 = null; + // Index of the data section. Used to tell relocation table where the section lives. + var data_section_index: ?u32 = null; + + const binary_bytes = &f.binary_bytes; + assert(binary_bytes.items.len == 0); + + try binary_bytes.appendSlice(gpa, &std.wasm.magic ++ &std.wasm.version); + assert(binary_bytes.items.len == 8); + + const binary_writer = binary_bytes.writer(gpa); + + // Type section. + for (f.function_imports.values()) |id| { + try f.func_types.put(gpa, id.functionType(wasm), {}); + } + for (wasm.functions.keys()) |function| { + try f.func_types.put(gpa, function.typeIndex(wasm), {}); + } + if (f.func_types.entries.len != 0) { + const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); + for (f.func_types.keys()) |func_type_index| { + const func_type = func_type_index.ptr(wasm); + try leb.writeUleb128(binary_writer, std.wasm.function_type); + const params = func_type.params.slice(wasm); + try leb.writeUleb128(binary_writer, @as(u32, @intCast(params.len))); + for (params) |param_ty| { + try leb.writeUleb128(binary_writer, @intFromEnum(param_ty)); + } + const returns = func_type.returns.slice(wasm); + try leb.writeUleb128(binary_writer, @as(u32, @intCast(returns.len))); + for (returns) |ret_ty| { + try leb.writeUleb128(binary_writer, @intFromEnum(ret_ty)); + } + } + replaceVecSectionHeader(binary_bytes, header_offset, .type, @intCast(f.func_types.entries.len)); + section_index += 1; + } + + if (!is_obj) { + // TODO: sort function_imports by ref count descending for optimal LEB encodings + // TODO: sort global_imports by ref count descending for optimal LEB encodings + // TODO: sort output functions by ref count descending for optimal LEB encodings + } + + // Import section + { + var total_imports: usize = 0; + const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); + + for (f.function_imports.values()) |id| { + const module_name = id.moduleName(wasm).slice(wasm).?; + try leb.writeUleb128(binary_writer, @as(u32, @intCast(module_name.len))); + try binary_writer.writeAll(module_name); + + const name = id.importName(wasm).slice(wasm); + try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); + try binary_writer.writeAll(name); + + try binary_writer.writeByte(@intFromEnum(std.wasm.ExternalKind.function)); + const type_index: FuncTypeIndex = .fromTypeIndex(id.functionType(wasm), f); + try leb.writeUleb128(binary_writer, @intFromEnum(type_index)); + } + total_imports += f.function_imports.entries.len; + + for (wasm.table_imports.values()) |id| { + const table_import = id.value(wasm); + const module_name = table_import.module_name.slice(wasm); + try leb.writeUleb128(binary_writer, @as(u32, @intCast(module_name.len))); + try binary_writer.writeAll(module_name); + + const name = table_import.name.slice(wasm); + try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); + try binary_writer.writeAll(name); + + try binary_writer.writeByte(@intFromEnum(std.wasm.ExternalKind.table)); + try leb.writeUleb128(binary_writer, @intFromEnum(@as(std.wasm.RefType, table_import.flags.ref_type.to()))); + try emitLimits(gpa, binary_bytes, table_import.limits()); + } + total_imports += wasm.table_imports.entries.len; + + if (import_memory) { + const name = if (is_obj) wasm.preloaded_strings.__linear_memory else wasm.preloaded_strings.memory; + try emitMemoryImport(wasm, binary_bytes, name, &.{ + // TODO the import_memory option needs to specify from which module + .module_name = wasm.object_host_name.unwrap().?, + .limits_min = wasm.memories.limits.min, + .limits_max = wasm.memories.limits.max, + .limits_has_max = wasm.memories.limits.flags.has_max, + .limits_is_shared = wasm.memories.limits.flags.is_shared, + .source_location = .none, + }); + total_imports += 1; + } + + for (f.global_imports.values()) |id| { + const module_name = id.moduleName(wasm).slice(wasm).?; + try leb.writeUleb128(binary_writer, @as(u32, @intCast(module_name.len))); + try binary_writer.writeAll(module_name); + + const name = id.importName(wasm).slice(wasm); + try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); + try binary_writer.writeAll(name); + + try binary_writer.writeByte(@intFromEnum(std.wasm.ExternalKind.global)); + const global_type = id.globalType(wasm); + try leb.writeUleb128(binary_writer, @intFromEnum(@as(std.wasm.Valtype, global_type.valtype))); + try binary_writer.writeByte(@intFromBool(global_type.mutable)); + } + total_imports += f.global_imports.entries.len; + + if (total_imports > 0) { + replaceVecSectionHeader(binary_bytes, header_offset, .import, @intCast(total_imports)); + section_index += 1; + } else { + binary_bytes.shrinkRetainingCapacity(header_offset); + } + } + + // Function section + if (wasm.functions.count() != 0) { + const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); + for (wasm.functions.keys()) |function| { + const index: FuncTypeIndex = .fromTypeIndex(function.typeIndex(wasm), f); + try leb.writeUleb128(binary_writer, @intFromEnum(index)); + } + + replaceVecSectionHeader(binary_bytes, header_offset, .function, @intCast(wasm.functions.count())); + section_index += 1; + } + + // Table section + if (wasm.tables.entries.len > 0) { + const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); + + for (wasm.tables.keys()) |table| { + try leb.writeUleb128(binary_writer, @intFromEnum(@as(std.wasm.RefType, table.refType(wasm)))); + try emitLimits(gpa, binary_bytes, table.limits(wasm)); + } + + replaceVecSectionHeader(binary_bytes, header_offset, .table, @intCast(wasm.tables.entries.len)); + section_index += 1; + } + + // Memory section. wasm currently only supports 1 linear memory segment. + if (!import_memory) { + const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); + try emitLimits(gpa, binary_bytes, wasm.memories.limits); + replaceVecSectionHeader(binary_bytes, header_offset, .memory, 1); + section_index += 1; + } + + // Global section. + const globals_len: u32 = @intCast(wasm.globals.entries.len); + if (globals_len > 0) { + const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); + + for (wasm.globals.keys()) |global_resolution| { + switch (global_resolution.unpack(wasm)) { + .unresolved => unreachable, + .__heap_base => try appendGlobal(gpa, binary_bytes, 0, virtual_addrs.heap_base), + .__heap_end => try appendGlobal(gpa, binary_bytes, 0, virtual_addrs.heap_end), + .__stack_pointer => try appendGlobal(gpa, binary_bytes, 1, virtual_addrs.stack_pointer), + .__tls_align => try appendGlobal(gpa, binary_bytes, 0, @intCast(virtual_addrs.tls_align.toByteUnits().?)), + .__tls_base => try appendGlobal(gpa, binary_bytes, 1, virtual_addrs.tls_base.?), + .__tls_size => try appendGlobal(gpa, binary_bytes, 0, virtual_addrs.tls_size.?), + .object_global => |i| { + const global = i.ptr(wasm); + try binary_bytes.appendSlice(gpa, &.{ + @intFromEnum(@as(std.wasm.Valtype, global.flags.global_type.valtype.to())), + @intFromBool(global.flags.global_type.mutable), + }); + try emitExpr(wasm, binary_bytes, global.expr); + }, + .nav_exe => unreachable, // Zig source code currently cannot represent this. + .nav_obj => unreachable, // Zig source code currently cannot represent this. + } + } + + replaceVecSectionHeader(binary_bytes, header_offset, .global, globals_len); + section_index += 1; + } + + // Export section + { + const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); + var exports_len: usize = 0; + + for (wasm.function_exports.keys(), wasm.function_exports.values()) |exp_name, function_index| { + const name = exp_name.slice(wasm); + try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); + try binary_bytes.appendSlice(gpa, name); + try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.function)); + const func_index = Wasm.OutputFunctionIndex.fromFunctionIndex(wasm, function_index); + try leb.writeUleb128(binary_writer, @intFromEnum(func_index)); + } + exports_len += wasm.function_exports.entries.len; + + if (wasm.export_table and f.indirect_function_table.entries.len > 0) { + const name = "__indirect_function_table"; + const index: u32 = @intCast(wasm.tables.getIndex(.__indirect_function_table).?); + try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); + try binary_bytes.appendSlice(gpa, name); + try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.table)); + try leb.writeUleb128(binary_writer, index); + exports_len += 1; + } + + if (export_memory) { + const name = "memory"; + try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); + try binary_bytes.appendSlice(gpa, name); + try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.memory)); + try leb.writeUleb128(binary_writer, @as(u32, 0)); + exports_len += 1; + } + + for (wasm.global_exports.items) |exp| { + const name = exp.name.slice(wasm); + try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); + try binary_bytes.appendSlice(gpa, name); + try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.global)); + try leb.writeUleb128(binary_writer, @intFromEnum(exp.global_index)); + } + exports_len += wasm.global_exports.items.len; + + if (exports_len > 0) { + replaceVecSectionHeader(binary_bytes, header_offset, .@"export", @intCast(exports_len)); + section_index += 1; + } else { + binary_bytes.shrinkRetainingCapacity(header_offset); + } + } + + // start section + if (wasm.functions.getIndex(.__wasm_init_memory)) |func_index| { + try emitStartSection(gpa, binary_bytes, .fromFunctionIndex(wasm, @enumFromInt(func_index))); + } else if (Wasm.OutputFunctionIndex.fromResolution(wasm, wasm.entry_resolution)) |func_index| { + try emitStartSection(gpa, binary_bytes, func_index); + } + + // element section + if (f.indirect_function_table.entries.len > 0) { + const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); + + // indirect function table elements + const table_index: u32 = @intCast(wasm.tables.getIndex(.__indirect_function_table).?); + // passive with implicit 0-index table or set table index manually + const flags: u32 = if (table_index == 0) 0x0 else 0x02; + try leb.writeUleb128(binary_writer, flags); + if (flags == 0x02) { + try leb.writeUleb128(binary_writer, table_index); + } + // We start at index 1, so unresolved function pointers are invalid + try emitInit(binary_writer, .{ .i32_const = 1 }); + if (flags == 0x02) { + try leb.writeUleb128(binary_writer, @as(u8, 0)); // represents funcref + } + try leb.writeUleb128(binary_writer, @as(u32, @intCast(f.indirect_function_table.entries.len))); + for (f.indirect_function_table.keys()) |func_index| { + try leb.writeUleb128(binary_writer, @intFromEnum(func_index)); + } + + replaceVecSectionHeader(binary_bytes, header_offset, .element, 1); + section_index += 1; + } + + // When the shared-memory option is enabled, we *must* emit the 'data count' section. + if (f.data_segment_groups.items.len > 0) { + const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); + replaceVecSectionHeader(binary_bytes, header_offset, .data_count, @intCast(f.data_segment_groups.items.len)); + } + + // Code section. + if (wasm.functions.count() != 0) { + const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); + + for (wasm.functions.keys()) |resolution| switch (resolution.unpack(wasm)) { + .unresolved => unreachable, + .__wasm_apply_global_tls_relocs => @panic("TODO lower __wasm_apply_global_tls_relocs"), + .__wasm_call_ctors => { + const code_start = try reserveSize(gpa, binary_bytes); + defer replaceSize(binary_bytes, code_start); + try emitCallCtorsFunction(wasm, binary_bytes); + }, + .__wasm_init_memory => { + const code_start = try reserveSize(gpa, binary_bytes); + defer replaceSize(binary_bytes, code_start); + try emitInitMemoryFunction(wasm, binary_bytes, &virtual_addrs); + }, + .__wasm_init_tls => { + const code_start = try reserveSize(gpa, binary_bytes); + defer replaceSize(binary_bytes, code_start); + try emitInitTlsFunction(wasm, binary_bytes); + }, + .object_function => |i| { + const ptr = i.ptr(wasm); + const code = ptr.code.slice(wasm); + try leb.writeUleb128(binary_writer, code.len); + const code_start = binary_bytes.items.len; + try binary_bytes.appendSlice(gpa, code); + if (!is_obj) applyRelocs(binary_bytes.items[code_start..], ptr.offset, ptr.relocations(wasm), wasm); + }, + .zcu_func => |i| { + const code_start = try reserveSize(gpa, binary_bytes); + defer replaceSize(binary_bytes, code_start); + + log.debug("lowering function code for '{s}'", .{resolution.name(wasm).?}); + + const zcu = comp.zcu.?; + const ip = &zcu.intern_pool; + const ip_index = i.key(wasm).*; + switch (ip.indexToKey(ip_index)) { + .enum_type => { + try emitTagNameFunction(wasm, binary_bytes, f.data_segments.get(.__zig_tag_name_table).?, i.value(wasm).tag_name.table_index, ip_index); + }, + else => try i.value(wasm).function.lower(wasm, binary_bytes), + } + }, + }; + + replaceVecSectionHeader(binary_bytes, header_offset, .code, @intCast(wasm.functions.entries.len)); + code_section_index = section_index; + section_index += 1; + } + + if (!is_obj) { + for (wasm.uav_fixups.items) |uav_fixup| { + const ds_id: Wasm.DataSegmentId = .pack(wasm, .{ .uav_exe = uav_fixup.uavs_exe_index }); + const vaddr = f.data_segments.get(ds_id).? + uav_fixup.addend; + if (!is64) { + mem.writeInt(u32, wasm.string_bytes.items[uav_fixup.offset..][0..4], vaddr, .little); + } else { + mem.writeInt(u64, wasm.string_bytes.items[uav_fixup.offset..][0..8], vaddr, .little); + } + } + for (wasm.nav_fixups.items) |nav_fixup| { + const ds_id: Wasm.DataSegmentId = .pack(wasm, .{ .nav_exe = nav_fixup.navs_exe_index }); + const vaddr = f.data_segments.get(ds_id).? + nav_fixup.addend; + if (!is64) { + mem.writeInt(u32, wasm.string_bytes.items[nav_fixup.offset..][0..4], vaddr, .little); + } else { + mem.writeInt(u64, wasm.string_bytes.items[nav_fixup.offset..][0..8], vaddr, .little); + } + } + for (wasm.func_table_fixups.items) |fixup| { + const table_index: IndirectFunctionTableIndex = .fromZcuIndirectFunctionSetIndex(fixup.table_index); + if (!is64) { + mem.writeInt(u32, wasm.string_bytes.items[fixup.offset..][0..4], table_index.toAbi(), .little); + } else { + mem.writeInt(u64, wasm.string_bytes.items[fixup.offset..][0..8], table_index.toAbi(), .little); + } + } + } + + // Data section. + if (f.data_segment_groups.items.len != 0) { + const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); + + var group_index: u32 = 0; + var segment_offset: u32 = 0; + var group_start_addr: u32 = data_vaddr; + var group_end_addr = f.data_segment_groups.items[group_index].end_addr; + for (segment_ids, segment_vaddrs) |segment_id, segment_vaddr| { + if (segment_vaddr >= group_end_addr) { + try binary_bytes.appendNTimes(gpa, 0, group_end_addr - group_start_addr - segment_offset); + group_index += 1; + if (group_index >= f.data_segment_groups.items.len) { + // All remaining segments are zero. + break; + } + group_start_addr = group_end_addr; + group_end_addr = f.data_segment_groups.items[group_index].end_addr; + segment_offset = 0; + } + if (segment_offset == 0) { + const group_size = group_end_addr - group_start_addr; + log.debug("emit data section group, {d} bytes", .{group_size}); + const flags: Object.DataSegmentFlags = if (segment_id.isPassive(wasm)) .passive else .active; + try leb.writeUleb128(binary_writer, @intFromEnum(flags)); + // Passive segments are initialized at runtime. + if (flags != .passive) { + try emitInit(binary_writer, .{ .i32_const = @as(i32, @bitCast(group_start_addr)) }); + } + try leb.writeUleb128(binary_writer, group_size); + } + if (segment_id.isEmpty(wasm)) { + // It counted for virtual memory but it does not go into the binary. + continue; + } + + // Padding for alignment. + const needed_offset = segment_vaddr - group_start_addr; + try binary_bytes.appendNTimes(gpa, 0, needed_offset - segment_offset); + segment_offset = needed_offset; + + const code_start = binary_bytes.items.len; + append: { + const code = switch (segment_id.unpack(wasm)) { + .__heap_base => { + mem.writeInt(u32, try binary_bytes.addManyAsArray(gpa, 4), virtual_addrs.heap_base, .little); + break :append; + }, + .__heap_end => { + mem.writeInt(u32, try binary_bytes.addManyAsArray(gpa, 4), virtual_addrs.heap_end, .little); + break :append; + }, + .__zig_error_names => { + try binary_bytes.appendSlice(gpa, wasm.error_name_bytes.items); + break :append; + }, + .__zig_error_name_table => { + if (is_obj) @panic("TODO error name table reloc"); + const base = f.data_segments.get(.__zig_error_names).?; + if (!is64) { + try emitTagNameTable(gpa, binary_bytes, wasm.error_name_offs.items, wasm.error_name_bytes.items, base, u32); + } else { + try emitTagNameTable(gpa, binary_bytes, wasm.error_name_offs.items, wasm.error_name_bytes.items, base, u64); + } + break :append; + }, + .__zig_tag_names => { + try binary_bytes.appendSlice(gpa, wasm.tag_name_bytes.items); + break :append; + }, + .__zig_tag_name_table => { + if (is_obj) @panic("TODO tag name table reloc"); + const base = f.data_segments.get(.__zig_tag_names).?; + if (!is64) { + try emitTagNameTable(gpa, binary_bytes, wasm.tag_name_offs.items, wasm.tag_name_bytes.items, base, u32); + } else { + try emitTagNameTable(gpa, binary_bytes, wasm.tag_name_offs.items, wasm.tag_name_bytes.items, base, u64); + } + break :append; + }, + .object => |i| { + const ptr = i.ptr(wasm); + try binary_bytes.appendSlice(gpa, ptr.payload.slice(wasm)); + if (!is_obj) applyRelocs(binary_bytes.items[code_start..], ptr.offset, ptr.relocations(wasm), wasm); + break :append; + }, + inline .uav_exe, .uav_obj, .nav_exe, .nav_obj => |i| i.value(wasm).code, + }; + try binary_bytes.appendSlice(gpa, code.slice(wasm)); + } + segment_offset += @intCast(binary_bytes.items.len - code_start); + } + + replaceVecSectionHeader(binary_bytes, header_offset, .data, @intCast(f.data_segment_groups.items.len)); + data_section_index = section_index; + section_index += 1; + } + + if (is_obj) { + @panic("TODO emit link section for object file and emit modified relocations"); + } else if (comp.config.debug_format != .strip) { + try emitNameSection(wasm, f.data_segment_groups.items, binary_bytes); + } + + if (comp.config.debug_format != .strip) { + // The build id must be computed on the main sections only, + // so we have to do it now, before the debug sections. + switch (wasm.base.build_id) { + .none => {}, + .fast => { + var id: [16]u8 = undefined; + std.crypto.hash.sha3.TurboShake128(null).hash(binary_bytes.items, &id, .{}); + var uuid: [36]u8 = undefined; + _ = try std.fmt.bufPrint(&uuid, "{s}-{s}-{s}-{s}-{s}", .{ + std.fmt.fmtSliceHexLower(id[0..4]), + std.fmt.fmtSliceHexLower(id[4..6]), + std.fmt.fmtSliceHexLower(id[6..8]), + std.fmt.fmtSliceHexLower(id[8..10]), + std.fmt.fmtSliceHexLower(id[10..]), + }); + try emitBuildIdSection(gpa, binary_bytes, &uuid); + }, + .hexstring => |hs| { + var buffer: [32 * 2]u8 = undefined; + const str = std.fmt.bufPrint(&buffer, "{s}", .{ + std.fmt.fmtSliceHexLower(hs.toSlice()), + }) catch unreachable; + try emitBuildIdSection(gpa, binary_bytes, str); + }, + else => |mode| { + var err = try diags.addErrorWithNotes(0); + try err.addMsg("build-id '{s}' is not supported for WebAssembly", .{@tagName(mode)}); + }, + } + + var debug_bytes = std.ArrayList(u8).init(gpa); + defer debug_bytes.deinit(); + + try emitProducerSection(gpa, binary_bytes); + try emitFeaturesSection(gpa, binary_bytes, target); + } + + // Finally, write the entire binary into the file. + const file = wasm.base.file.?; + try file.pwriteAll(binary_bytes.items, 0); + try file.setEndPos(binary_bytes.items.len); +} + +const VirtualAddrs = struct { + stack_pointer: u32, + heap_base: u32, + heap_end: u32, + tls_base: ?u32, + tls_align: Alignment, + tls_size: ?u32, + init_memory_flag: ?u32, +}; + +fn emitNameSection( + wasm: *Wasm, + data_segment_groups: []const DataSegmentGroup, + binary_bytes: *std.ArrayListUnmanaged(u8), +) !void { + const f = &wasm.flush_buffer; + const comp = wasm.base.comp; + const gpa = comp.gpa; + + const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes); + defer writeCustomSectionHeader(binary_bytes, header_offset); + + const name_name = "name"; + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, name_name.len)); + try binary_bytes.appendSlice(gpa, name_name); + + { + const sub_offset = try reserveCustomSectionHeader(gpa, binary_bytes); + defer replaceHeader(binary_bytes, sub_offset, @intFromEnum(std.wasm.NameSubsection.function)); + + const total_functions: u32 = @intCast(f.function_imports.entries.len + wasm.functions.entries.len); + try leb.writeUleb128(binary_bytes.writer(gpa), total_functions); + + for (f.function_imports.keys(), 0..) |name_index, function_index| { + const name = name_index.slice(wasm); + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(function_index))); + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); + try binary_bytes.appendSlice(gpa, name); + } + for (wasm.functions.keys(), f.function_imports.entries.len..) |resolution, function_index| { + const name = resolution.name(wasm).?; + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(function_index))); + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); + try binary_bytes.appendSlice(gpa, name); + } + } + + { + const sub_offset = try reserveCustomSectionHeader(gpa, binary_bytes); + defer replaceHeader(binary_bytes, sub_offset, @intFromEnum(std.wasm.NameSubsection.global)); + + const total_globals: u32 = @intCast(f.global_imports.entries.len + wasm.globals.entries.len); + try leb.writeUleb128(binary_bytes.writer(gpa), total_globals); + + for (f.global_imports.keys(), 0..) |name_index, global_index| { + const name = name_index.slice(wasm); + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(global_index))); + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); + try binary_bytes.appendSlice(gpa, name); + } + for (wasm.globals.keys(), f.global_imports.entries.len..) |resolution, global_index| { + const name = resolution.name(wasm).?; + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(global_index))); + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); + try binary_bytes.appendSlice(gpa, name); + } + } + + { + const sub_offset = try reserveCustomSectionHeader(gpa, binary_bytes); + defer replaceHeader(binary_bytes, sub_offset, @intFromEnum(std.wasm.NameSubsection.data_segment)); + + const total_data_segments: u32 = @intCast(data_segment_groups.len); + try leb.writeUleb128(binary_bytes.writer(gpa), total_data_segments); + + for (data_segment_groups, 0..) |group, i| { + const name, _ = splitSegmentName(group.first_segment.name(wasm)); + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(i))); + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); + try binary_bytes.appendSlice(gpa, name); + } + } +} + +fn emitFeaturesSection( + gpa: Allocator, + binary_bytes: *std.ArrayListUnmanaged(u8), + target: *const std.Target, +) Allocator.Error!void { + const feature_count = target.cpu.features.count(); + if (feature_count == 0) return; + + const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes); + defer writeCustomSectionHeader(binary_bytes, header_offset); + + const writer = binary_bytes.writer(gpa); + const target_features = "target_features"; + try leb.writeUleb128(writer, @as(u32, @intCast(target_features.len))); + try writer.writeAll(target_features); + + try leb.writeUleb128(writer, @as(u32, @intCast(feature_count))); + + var safety_count = feature_count; + for (target.cpu.arch.allFeaturesList(), 0..) |*feature, i| { + if (!std.Target.wasm.featureSetHas(target.cpu.features, @enumFromInt(i))) continue; + safety_count -= 1; + + try leb.writeUleb128(writer, @as(u32, '+')); + // Depends on llvm_name for the hyphenated version that matches wasm tooling conventions. + const name = feature.llvm_name.?; + try leb.writeUleb128(writer, @as(u32, @intCast(name.len))); + try writer.writeAll(name); + } + assert(safety_count == 0); +} + +fn emitBuildIdSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8), build_id: []const u8) !void { + const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes); + defer writeCustomSectionHeader(binary_bytes, header_offset); + + const writer = binary_bytes.writer(gpa); + const hdr_build_id = "build_id"; + try leb.writeUleb128(writer, @as(u32, @intCast(hdr_build_id.len))); + try writer.writeAll(hdr_build_id); + + try leb.writeUleb128(writer, @as(u32, 1)); + try leb.writeUleb128(writer, @as(u32, @intCast(build_id.len))); + try writer.writeAll(build_id); +} + +fn emitProducerSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8)) !void { + const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes); + defer writeCustomSectionHeader(binary_bytes, header_offset); + + const writer = binary_bytes.writer(gpa); + const producers = "producers"; + try leb.writeUleb128(writer, @as(u32, @intCast(producers.len))); + try writer.writeAll(producers); + + try leb.writeUleb128(writer, @as(u32, 2)); // 2 fields: Language + processed-by + + // language field + { + const language = "language"; + try leb.writeUleb128(writer, @as(u32, @intCast(language.len))); + try writer.writeAll(language); + + // field_value_count (TODO: Parse object files for producer sections to detect their language) + try leb.writeUleb128(writer, @as(u32, 1)); + + // versioned name + { + try leb.writeUleb128(writer, @as(u32, 3)); // len of "Zig" + try writer.writeAll("Zig"); + + try leb.writeUleb128(writer, @as(u32, @intCast(build_options.version.len))); + try writer.writeAll(build_options.version); + } + } + + // processed-by field + { + const processed_by = "processed-by"; + try leb.writeUleb128(writer, @as(u32, @intCast(processed_by.len))); + try writer.writeAll(processed_by); + + // field_value_count (TODO: Parse object files for producer sections to detect other used tools) + try leb.writeUleb128(writer, @as(u32, 1)); + + // versioned name + { + try leb.writeUleb128(writer, @as(u32, 3)); // len of "Zig" + try writer.writeAll("Zig"); + + try leb.writeUleb128(writer, @as(u32, @intCast(build_options.version.len))); + try writer.writeAll(build_options.version); + } + } +} + +fn splitSegmentName(name: []const u8) struct { []const u8, []const u8 } { + const start = @intFromBool(name.len >= 1 and name[0] == '.'); + const pivot = mem.indexOfScalarPos(u8, name, start, '.') orelse name.len; + return .{ name[0..pivot], name[pivot..] }; +} + +test splitSegmentName { + { + const a, const b = splitSegmentName(".data"); + try std.testing.expectEqualStrings(".data", a); + try std.testing.expectEqualStrings("", b); + } +} + +fn wantSegmentMerge( + wasm: *const Wasm, + a_id: Wasm.DataSegmentId, + b_id: Wasm.DataSegmentId, + b_category: Wasm.DataSegmentId.Category, +) bool { + const a_category = a_id.category(wasm); + if (a_category != b_category) return false; + if (a_category == .tls or b_category == .tls) return false; + if (a_id.isPassive(wasm) != b_id.isPassive(wasm)) return false; + if (b_category == .zero) return true; + const a_name = a_id.name(wasm); + const b_name = b_id.name(wasm); + const a_prefix, _ = splitSegmentName(a_name); + const b_prefix, _ = splitSegmentName(b_name); + return mem.eql(u8, a_prefix, b_prefix); +} + +/// section id + fixed leb contents size + fixed leb vector length +const section_header_reserve_size = 1 + 5 + 5; +const section_header_size = 5 + 1; + +fn reserveVecSectionHeader(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!u32 { + try bytes.appendNTimes(gpa, 0, section_header_reserve_size); + return @intCast(bytes.items.len - section_header_reserve_size); +} + +fn replaceVecSectionHeader( + bytes: *std.ArrayListUnmanaged(u8), + offset: u32, + section: std.wasm.Section, + n_items: u32, +) void { + const size: u32 = @intCast(bytes.items.len - offset - section_header_reserve_size + uleb128size(n_items)); + var buf: [section_header_reserve_size]u8 = undefined; + var fbw = std.io.fixedBufferStream(&buf); + const w = fbw.writer(); + w.writeByte(@intFromEnum(section)) catch unreachable; + leb.writeUleb128(w, size) catch unreachable; + leb.writeUleb128(w, n_items) catch unreachable; + bytes.replaceRangeAssumeCapacity(offset, section_header_reserve_size, fbw.getWritten()); +} + +fn reserveCustomSectionHeader(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!u32 { + try bytes.appendNTimes(gpa, 0, section_header_size); + return @intCast(bytes.items.len - section_header_size); +} + +fn writeCustomSectionHeader(bytes: *std.ArrayListUnmanaged(u8), offset: u32) void { + return replaceHeader(bytes, offset, 0); // 0 = 'custom' section +} + +fn replaceHeader(bytes: *std.ArrayListUnmanaged(u8), offset: u32, tag: u8) void { + const size: u32 = @intCast(bytes.items.len - offset - section_header_size); + var buf: [section_header_size]u8 = undefined; + var fbw = std.io.fixedBufferStream(&buf); + const w = fbw.writer(); + w.writeByte(tag) catch unreachable; + leb.writeUleb128(w, size) catch unreachable; + bytes.replaceRangeAssumeCapacity(offset, section_header_size, fbw.getWritten()); +} + +const max_size_encoding = 5; + +fn reserveSize(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!u32 { + try bytes.appendNTimes(gpa, 0, max_size_encoding); + return @intCast(bytes.items.len - max_size_encoding); +} + +fn replaceSize(bytes: *std.ArrayListUnmanaged(u8), offset: u32) void { + const size: u32 = @intCast(bytes.items.len - offset - max_size_encoding); + var buf: [max_size_encoding]u8 = undefined; + var fbw = std.io.fixedBufferStream(&buf); + leb.writeUleb128(fbw.writer(), size) catch unreachable; + bytes.replaceRangeAssumeCapacity(offset, max_size_encoding, fbw.getWritten()); +} + +fn emitLimits( + gpa: Allocator, + binary_bytes: *std.ArrayListUnmanaged(u8), + limits: std.wasm.Limits, +) Allocator.Error!void { + try binary_bytes.append(gpa, @bitCast(limits.flags)); + try leb.writeUleb128(binary_bytes.writer(gpa), limits.min); + if (limits.flags.has_max) try leb.writeUleb128(binary_bytes.writer(gpa), limits.max); +} + +fn emitMemoryImport( + wasm: *Wasm, + binary_bytes: *std.ArrayListUnmanaged(u8), + name_index: String, + memory_import: *const Wasm.MemoryImport, +) Allocator.Error!void { + const gpa = wasm.base.comp.gpa; + const module_name = memory_import.module_name.slice(wasm); + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(module_name.len))); + try binary_bytes.appendSlice(gpa, module_name); + + const name = name_index.slice(wasm); + try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); + try binary_bytes.appendSlice(gpa, name); + + try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.memory)); + try emitLimits(gpa, binary_bytes, memory_import.limits()); +} + +pub fn emitInit(writer: anytype, init_expr: std.wasm.InitExpression) !void { + switch (init_expr) { + .i32_const => |val| { + try writer.writeByte(@intFromEnum(std.wasm.Opcode.i32_const)); + try leb.writeIleb128(writer, val); + }, + .i64_const => |val| { + try writer.writeByte(@intFromEnum(std.wasm.Opcode.i64_const)); + try leb.writeIleb128(writer, val); + }, + .f32_const => |val| { + try writer.writeByte(@intFromEnum(std.wasm.Opcode.f32_const)); + try writer.writeInt(u32, @bitCast(val), .little); + }, + .f64_const => |val| { + try writer.writeByte(@intFromEnum(std.wasm.Opcode.f64_const)); + try writer.writeInt(u64, @bitCast(val), .little); + }, + .global_get => |val| { + try writer.writeByte(@intFromEnum(std.wasm.Opcode.global_get)); + try leb.writeUleb128(writer, val); + }, + } + try writer.writeByte(@intFromEnum(std.wasm.Opcode.end)); +} + +pub fn emitExpr(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanaged(u8), expr: Wasm.Expr) Allocator.Error!void { + const gpa = wasm.base.comp.gpa; + const slice = expr.slice(wasm); + try binary_bytes.appendSlice(gpa, slice[0 .. slice.len + 1]); // +1 to include end opcode +} + +fn emitSegmentInfo(wasm: *Wasm, binary_bytes: *std.ArrayList(u8)) !void { + const gpa = wasm.base.comp.gpa; + const writer = binary_bytes.writer(gpa); + try leb.writeUleb128(writer, @intFromEnum(Wasm.SubsectionType.segment_info)); + const segment_offset = binary_bytes.items.len; + + try leb.writeUleb128(writer, @as(u32, @intCast(wasm.segment_info.count()))); + for (wasm.segment_info.values()) |segment_info| { + log.debug("Emit segment: {s} align({d}) flags({b})", .{ + segment_info.name, + segment_info.alignment, + segment_info.flags, + }); + try leb.writeUleb128(writer, @as(u32, @intCast(segment_info.name.len))); + try writer.writeAll(segment_info.name); + try leb.writeUleb128(writer, segment_info.alignment.toLog2Units()); + try leb.writeUleb128(writer, segment_info.flags); + } + + var buf: [5]u8 = undefined; + leb.writeUnsignedFixed(5, &buf, @as(u32, @intCast(binary_bytes.items.len - segment_offset))); + try binary_bytes.insertSlice(segment_offset, &buf); +} + +fn uleb128size(x: u32) u32 { + var value = x; + var size: u32 = 0; + while (value != 0) : (size += 1) value >>= 7; + return size; +} + +fn emitTagNameTable( + gpa: Allocator, + code: *std.ArrayListUnmanaged(u8), + tag_name_offs: []const u32, + tag_name_bytes: []const u8, + base: u32, + comptime Int: type, +) error{OutOfMemory}!void { + const ptr_size_bytes = @divExact(@bitSizeOf(Int), 8); + try code.ensureUnusedCapacity(gpa, ptr_size_bytes * 2 * tag_name_offs.len); + for (tag_name_offs) |off| { + const name_len: u32 = @intCast(mem.indexOfScalar(u8, tag_name_bytes[off..], 0).?); + mem.writeInt(Int, code.addManyAsArrayAssumeCapacity(ptr_size_bytes), base + off, .little); + mem.writeInt(Int, code.addManyAsArrayAssumeCapacity(ptr_size_bytes), name_len, .little); + } +} + +fn applyRelocs(code: []u8, code_offset: u32, relocs: Wasm.ObjectRelocation.IterableSlice, wasm: *const Wasm) void { + for ( + relocs.slice.tags(wasm), + relocs.slice.pointees(wasm), + relocs.slice.offsets(wasm), + relocs.slice.addends(wasm), + ) |tag, pointee, offset, *addend| { + if (offset >= relocs.end) break; + const sliced_code = code[offset - code_offset ..]; + switch (tag) { + .function_index_i32 => reloc_u32_function(sliced_code, .fromObjectFunctionHandlingWeak(wasm, pointee.function)), + .function_index_leb => reloc_leb_function(sliced_code, .fromObjectFunctionHandlingWeak(wasm, pointee.function)), + .function_offset_i32 => @panic("TODO this value is not known yet"), + .function_offset_i64 => @panic("TODO this value is not known yet"), + .table_index_i32 => reloc_u32_table_index(sliced_code, .fromObjectFunctionHandlingWeak(wasm, pointee.function)), + .table_index_i64 => reloc_u64_table_index(sliced_code, .fromObjectFunctionHandlingWeak(wasm, pointee.function)), + .table_index_rel_sleb => @panic("TODO what does this reloc tag mean?"), + .table_index_rel_sleb64 => @panic("TODO what does this reloc tag mean?"), + .table_index_sleb => reloc_sleb_table_index(sliced_code, .fromObjectFunctionHandlingWeak(wasm, pointee.function)), + .table_index_sleb64 => reloc_sleb64_table_index(sliced_code, .fromObjectFunctionHandlingWeak(wasm, pointee.function)), + + .function_import_index_i32 => reloc_u32_function(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)), + .function_import_index_leb => reloc_leb_function(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)), + .function_import_offset_i32 => @panic("TODO this value is not known yet"), + .function_import_offset_i64 => @panic("TODO this value is not known yet"), + .table_import_index_i32 => reloc_u32_table_index(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)), + .table_import_index_i64 => reloc_u64_table_index(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)), + .table_import_index_rel_sleb => @panic("TODO what does this reloc tag mean?"), + .table_import_index_rel_sleb64 => @panic("TODO what does this reloc tag mean?"), + .table_import_index_sleb => reloc_sleb_table_index(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)), + .table_import_index_sleb64 => reloc_sleb64_table_index(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)), + + .global_index_i32 => reloc_u32_global(sliced_code, .fromObjectGlobalHandlingWeak(wasm, pointee.global)), + .global_index_leb => reloc_leb_global(sliced_code, .fromObjectGlobalHandlingWeak(wasm, pointee.global)), + + .global_import_index_i32 => reloc_u32_global(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)), + .global_import_index_leb => reloc_leb_global(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)), + + .memory_addr_i32 => reloc_u32_addr(sliced_code, .fromObjectData(wasm, pointee.data, addend.*)), + .memory_addr_i64 => reloc_u64_addr(sliced_code, .fromObjectData(wasm, pointee.data, addend.*)), + .memory_addr_leb => reloc_leb_addr(sliced_code, .fromObjectData(wasm, pointee.data, addend.*)), + .memory_addr_leb64 => reloc_leb64_addr(sliced_code, .fromObjectData(wasm, pointee.data, addend.*)), + .memory_addr_locrel_i32 => @panic("TODO implement relocation memory_addr_locrel_i32"), + .memory_addr_rel_sleb => @panic("TODO implement relocation memory_addr_rel_sleb"), + .memory_addr_rel_sleb64 => @panic("TODO implement relocation memory_addr_rel_sleb64"), + .memory_addr_sleb => reloc_sleb_addr(sliced_code, .fromObjectData(wasm, pointee.data, addend.*)), + .memory_addr_sleb64 => reloc_sleb64_addr(sliced_code, .fromObjectData(wasm, pointee.data, addend.*)), + .memory_addr_tls_sleb => reloc_sleb_addr(sliced_code, .fromObjectData(wasm, pointee.data, addend.*)), + .memory_addr_tls_sleb64 => reloc_sleb64_addr(sliced_code, .fromObjectData(wasm, pointee.data, addend.*)), + + .memory_addr_import_i32 => reloc_u32_addr(sliced_code, .fromSymbolName(wasm, pointee.symbol_name, addend.*)), + .memory_addr_import_i64 => reloc_u64_addr(sliced_code, .fromSymbolName(wasm, pointee.symbol_name, addend.*)), + .memory_addr_import_leb => reloc_leb_addr(sliced_code, .fromSymbolName(wasm, pointee.symbol_name, addend.*)), + .memory_addr_import_leb64 => reloc_leb64_addr(sliced_code, .fromSymbolName(wasm, pointee.symbol_name, addend.*)), + .memory_addr_import_locrel_i32 => @panic("TODO implement relocation memory_addr_import_locrel_i32"), + .memory_addr_import_rel_sleb => @panic("TODO implement relocation memory_addr_import_rel_sleb"), + .memory_addr_import_rel_sleb64 => @panic("TODO implement memory_addr_import_rel_sleb64"), + .memory_addr_import_sleb => reloc_sleb_addr(sliced_code, .fromSymbolName(wasm, pointee.symbol_name, addend.*)), + .memory_addr_import_sleb64 => reloc_sleb64_addr(sliced_code, .fromSymbolName(wasm, pointee.symbol_name, addend.*)), + .memory_addr_import_tls_sleb => @panic("TODO"), + .memory_addr_import_tls_sleb64 => @panic("TODO"), + + .section_offset_i32 => @panic("TODO this value is not known yet"), + + .table_number_leb => reloc_leb_table(sliced_code, .fromObjectTable(wasm, pointee.table)), + .table_import_number_leb => reloc_leb_table(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)), + + .type_index_leb => reloc_leb_type(sliced_code, .fromTypeIndex(pointee.type_index, &wasm.flush_buffer)), + } + } +} + +fn reloc_u32_table_index(code: []u8, i: IndirectFunctionTableIndex) void { + mem.writeInt(u32, code[0..4], i.toAbi(), .little); +} + +fn reloc_u64_table_index(code: []u8, i: IndirectFunctionTableIndex) void { + mem.writeInt(u64, code[0..8], i.toAbi(), .little); +} + +fn reloc_sleb_table_index(code: []u8, i: IndirectFunctionTableIndex) void { + leb.writeSignedFixed(5, code[0..5], i.toAbi()); +} + +fn reloc_sleb64_table_index(code: []u8, i: IndirectFunctionTableIndex) void { + leb.writeSignedFixed(11, code[0..11], i.toAbi()); +} + +fn reloc_u32_function(code: []u8, function: Wasm.OutputFunctionIndex) void { + mem.writeInt(u32, code[0..4], @intFromEnum(function), .little); +} + +fn reloc_leb_function(code: []u8, function: Wasm.OutputFunctionIndex) void { + leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(function)); +} + +fn reloc_u32_global(code: []u8, global: Wasm.GlobalIndex) void { + mem.writeInt(u32, code[0..4], @intFromEnum(global), .little); +} + +fn reloc_leb_global(code: []u8, global: Wasm.GlobalIndex) void { + leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(global)); +} + +const RelocAddr = struct { + addr: u32, + + fn fromObjectData(wasm: *const Wasm, i: Wasm.ObjectData.Index, addend: i32) RelocAddr { + return fromDataLoc(&wasm.flush_buffer, .fromObjectDataIndex(wasm, i), addend); + } + + fn fromSymbolName(wasm: *const Wasm, name: String, addend: i32) RelocAddr { + const flush = &wasm.flush_buffer; + if (wasm.object_data_imports.getPtr(name)) |import| { + return fromDataLoc(flush, import.resolution.dataLoc(wasm), addend); + } else if (wasm.data_imports.get(name)) |id| { + return fromDataLoc(flush, .fromDataImportId(wasm, id), addend); + } else { + unreachable; + } + } + + fn fromDataLoc(flush: *const Flush, data_loc: Wasm.DataLoc, addend: i32) RelocAddr { + const base_addr: i64 = flush.data_segments.get(data_loc.segment).?; + return .{ .addr = @intCast(base_addr + data_loc.offset + addend) }; + } +}; + +fn reloc_u32_addr(code: []u8, ra: RelocAddr) void { + mem.writeInt(u32, code[0..4], ra.addr, .little); +} + +fn reloc_u64_addr(code: []u8, ra: RelocAddr) void { + mem.writeInt(u64, code[0..8], ra.addr, .little); +} + +fn reloc_leb_addr(code: []u8, ra: RelocAddr) void { + leb.writeUnsignedFixed(5, code[0..5], ra.addr); +} + +fn reloc_leb64_addr(code: []u8, ra: RelocAddr) void { + leb.writeUnsignedFixed(11, code[0..11], ra.addr); +} + +fn reloc_sleb_addr(code: []u8, ra: RelocAddr) void { + leb.writeSignedFixed(5, code[0..5], ra.addr); +} + +fn reloc_sleb64_addr(code: []u8, ra: RelocAddr) void { + leb.writeSignedFixed(11, code[0..11], ra.addr); +} + +fn reloc_leb_table(code: []u8, table: Wasm.TableIndex) void { + leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(table)); +} + +fn reloc_leb_type(code: []u8, index: FuncTypeIndex) void { + leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(index)); +} + +fn emitCallCtorsFunction(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!void { + const gpa = wasm.base.comp.gpa; + + try binary_bytes.ensureUnusedCapacity(gpa, 5 + 1); + appendReservedUleb32(binary_bytes, 0); // no locals + + for (wasm.object_init_funcs.items) |init_func| { + const func = init_func.function_index.ptr(wasm); + if (!func.object_index.ptr(wasm).is_included) continue; + const ty = func.type_index.ptr(wasm); + const n_returns = ty.returns.slice(wasm).len; + + // Call function by its function index + try binary_bytes.ensureUnusedCapacity(gpa, 1 + 5 + n_returns + 1); + const call_index: Wasm.OutputFunctionIndex = .fromObjectFunction(wasm, init_func.function_index); + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.call)); + appendReservedUleb32(binary_bytes, @intFromEnum(call_index)); + + // drop all returned values from the stack as __wasm_call_ctors has no return value + binary_bytes.appendNTimesAssumeCapacity(@intFromEnum(std.wasm.Opcode.drop), n_returns); + } + + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); // end function body +} + +fn emitInitMemoryFunction( + wasm: *const Wasm, + binary_bytes: *std.ArrayListUnmanaged(u8), + virtual_addrs: *const VirtualAddrs, +) Allocator.Error!void { + const comp = wasm.base.comp; + const gpa = comp.gpa; + const shared_memory = comp.config.shared_memory; + + // Passive segments are used to avoid memory being reinitialized on each + // thread's instantiation. These passive segments are initialized and + // dropped in __wasm_init_memory, which is registered as the start function + // We also initialize bss segments (using memory.fill) as part of this + // function. + assert(wasm.any_passive_inits); + + try binary_bytes.ensureUnusedCapacity(gpa, 5 + 1); + appendReservedUleb32(binary_bytes, 0); // no locals + + if (virtual_addrs.init_memory_flag) |flag_address| { + assert(shared_memory); + try binary_bytes.ensureUnusedCapacity(gpa, 2 * 3 + 6 * 3 + 1 + 6 * 3 + 1 + 5 * 4 + 1 + 1); + // destination blocks + // based on values we jump to corresponding label + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.block)); // $drop + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.BlockType.empty)); + + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.block)); // $wait + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.BlockType.empty)); + + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.block)); // $init + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.BlockType.empty)); + + // atomically check + appendReservedI32Const(binary_bytes, flag_address); + appendReservedI32Const(binary_bytes, 0); + appendReservedI32Const(binary_bytes, 1); + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix)); + appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.AtomicsOpcode.i32_atomic_rmw_cmpxchg)); + appendReservedUleb32(binary_bytes, 2); // alignment + appendReservedUleb32(binary_bytes, 0); // offset + + // based on the value from the atomic check, jump to the label. + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br_table)); + appendReservedUleb32(binary_bytes, 2); // length of the table (we have 3 blocks but because of the mandatory default the length is 2). + appendReservedUleb32(binary_bytes, 0); // $init + appendReservedUleb32(binary_bytes, 1); // $wait + appendReservedUleb32(binary_bytes, 2); // $drop + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); + } + + const segment_groups = wasm.flush_buffer.data_segment_groups.items; + var prev_end: u32 = 0; + for (segment_groups, 0..) |group, segment_index| { + defer prev_end = group.end_addr; + const segment = group.first_segment; + if (!segment.isPassive(wasm)) continue; + + const start_addr: u32 = @intCast(segment.alignment(wasm).forward(prev_end)); + const segment_size: u32 = group.end_addr - start_addr; + + try binary_bytes.ensureUnusedCapacity(gpa, 6 + 6 + 1 + 5 + 6 + 6 + 1 + 6 * 2 + 1 + 1); + + // For passive BSS segments we can simply issue a memory.fill(0). For + // non-BSS segments we do a memory.init. Both instructions take as + // their first argument the destination address. + appendReservedI32Const(binary_bytes, start_addr); + + if (shared_memory and segment.isTls(wasm)) { + // When we initialize the TLS segment we also set the `__tls_base` + // global. This allows the runtime to use this static copy of the + // TLS data for the first/main thread. + appendReservedI32Const(binary_bytes, start_addr); + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_set)); + appendReservedUleb32(binary_bytes, virtual_addrs.tls_base.?); + } + + appendReservedI32Const(binary_bytes, 0); + appendReservedI32Const(binary_bytes, segment_size); + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.misc_prefix)); + if (segment.isBss(wasm)) { + // fill bss segment with zeroes + appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.MiscOpcode.memory_fill)); + } else { + // initialize the segment + appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.MiscOpcode.memory_init)); + appendReservedUleb32(binary_bytes, @intCast(segment_index)); + } + binary_bytes.appendAssumeCapacity(0); // memory index immediate + } + + if (virtual_addrs.init_memory_flag) |flag_address| { + assert(shared_memory); + try binary_bytes.ensureUnusedCapacity(gpa, 6 + 6 + 1 + 3 * 5 + 6 + 1 + 5 + 1 + 3 * 5 + 1 + 1 + 5 + 1 + 6 * 2 + 1 + 5 + 1 + 3 * 5 + 1 + 1 + 1); + // we set the init memory flag to value '2' + appendReservedI32Const(binary_bytes, flag_address); + appendReservedI32Const(binary_bytes, 2); + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix)); + appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.AtomicsOpcode.i32_atomic_store)); + appendReservedUleb32(binary_bytes, @as(u32, 2)); // alignment + appendReservedUleb32(binary_bytes, @as(u32, 0)); // offset + + // notify any waiters for segment initialization completion + appendReservedI32Const(binary_bytes, flag_address); + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const)); + leb.writeIleb128(binary_bytes.fixedWriter(), @as(i32, -1)) catch unreachable; // number of waiters + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix)); + appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.AtomicsOpcode.memory_atomic_notify)); + appendReservedUleb32(binary_bytes, @as(u32, 2)); // alignment + appendReservedUleb32(binary_bytes, @as(u32, 0)); // offset + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.drop)); + + // branch and drop segments + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br)); + appendReservedUleb32(binary_bytes, @as(u32, 1)); + + // wait for thread to initialize memory segments + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); // end $wait + appendReservedI32Const(binary_bytes, flag_address); + appendReservedI32Const(binary_bytes, 1); // expected flag value + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_const)); + leb.writeIleb128(binary_bytes.fixedWriter(), @as(i64, -1)) catch unreachable; // timeout + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix)); + appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.AtomicsOpcode.memory_atomic_wait32)); + appendReservedUleb32(binary_bytes, @as(u32, 2)); // alignment + appendReservedUleb32(binary_bytes, @as(u32, 0)); // offset + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.drop)); + + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); // end $drop + } + + for (segment_groups, 0..) |group, segment_index| { + const segment = group.first_segment; + if (!segment.isPassive(wasm)) continue; + if (segment.isBss(wasm)) continue; + // The TLS region should not be dropped since its is needed + // during the initialization of each thread (__wasm_init_tls). + if (shared_memory and segment.isTls(wasm)) continue; + + try binary_bytes.ensureUnusedCapacity(gpa, 1 + 5 + 5 + 1); + + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.misc_prefix)); + appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.MiscOpcode.data_drop)); + appendReservedUleb32(binary_bytes, @intCast(segment_index)); + } + + // End of the function body + binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); +} + +fn emitInitTlsFunction(wasm: *const Wasm, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!void { + const comp = wasm.base.comp; + const gpa = comp.gpa; + + assert(comp.config.shared_memory); + + try bytes.ensureUnusedCapacity(gpa, 5 * 10 + 8); + + appendReservedUleb32(bytes, 0); // no locals + + // If there's a TLS segment, initialize it during runtime using the bulk-memory feature + // TLS segment is always the first one due to how we sort the data segments. + const data_segments = wasm.flush_buffer.data_segments.keys(); + if (data_segments.len > 0 and data_segments[0].isTls(wasm)) { + const start_addr = wasm.flush_buffer.data_segments.values()[0]; + const end_addr = wasm.flush_buffer.data_segment_groups.items[0].end_addr; + const group_size = end_addr - start_addr; + const data_segment_index = 0; + + const param_local: u32 = 0; + + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get)); + appendReservedUleb32(bytes, param_local); + + const tls_base_global_index: Wasm.GlobalIndex = @enumFromInt(wasm.globals.getIndex(.__tls_base).?); + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_set)); + appendReservedUleb32(bytes, @intFromEnum(tls_base_global_index)); + + // load stack values for the bulk-memory operation + { + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get)); + appendReservedUleb32(bytes, param_local); + + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const)); + appendReservedUleb32(bytes, 0); //segment offset + + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const)); + appendReservedUleb32(bytes, group_size); //segment offset + } + + // perform the bulk-memory operation to initialize the data segment + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.misc_prefix)); + appendReservedUleb32(bytes, @intFromEnum(std.wasm.MiscOpcode.memory_init)); + // segment immediate + appendReservedUleb32(bytes, data_segment_index); + // memory index immediate (always 0) + appendReservedUleb32(bytes, 0); + } + + // If we have to perform any TLS relocations, call the corresponding function + // which performs all runtime TLS relocations. This is a synthetic function, + // generated by the linker. + if (wasm.functions.getIndex(.__wasm_apply_global_tls_relocs)) |function_index| { + const output_function_index: Wasm.OutputFunctionIndex = .fromFunctionIndex(wasm, @enumFromInt(function_index)); + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.call)); + appendReservedUleb32(bytes, @intFromEnum(output_function_index)); + } + + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); +} + +fn emitStartSection(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8), i: Wasm.OutputFunctionIndex) !void { + const header_offset = try reserveVecSectionHeader(gpa, bytes); + replaceVecSectionHeader(bytes, header_offset, .start, @intFromEnum(i)); +} + +fn emitTagNameFunction( + wasm: *Wasm, + code: *std.ArrayListUnmanaged(u8), + table_base_addr: u32, + table_index: u32, + enum_type_ip: InternPool.Index, +) !void { + const comp = wasm.base.comp; + const gpa = comp.gpa; + const diags = &comp.link_diags; + const zcu = comp.zcu.?; + const ip = &zcu.intern_pool; + const enum_type = ip.loadEnumType(enum_type_ip); + const tag_values = enum_type.values.get(ip); + + try code.ensureUnusedCapacity(gpa, 7 * 5 + 6 + 1 * 6); + appendReservedUleb32(code, 0); // no locals + + const slice_abi_size = 8; + const encoded_alignment = @ctz(@as(u32, 4)); + if (tag_values.len == 0) { + // Then it's auto-numbered and therefore a direct table lookup. + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get)); + appendReservedUleb32(code, 0); + + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get)); + appendReservedUleb32(code, 1); + + appendReservedI32Const(code, slice_abi_size); + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_mul)); + + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_load)); + appendReservedUleb32(code, encoded_alignment); + appendReservedUleb32(code, table_base_addr + table_index * 8); + + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_store)); + appendReservedUleb32(code, encoded_alignment); + appendReservedUleb32(code, 0); + } else { + const int_info = Zcu.Type.intInfo(.fromInterned(enum_type.tag_ty), zcu); + const outer_block_type: std.wasm.BlockType = switch (int_info.bits) { + 0...32 => .i32, + 33...64 => .i64, + else => return diags.fail("wasm linker does not yet implement @tagName for sparse enums with more than 64 bit integer tag types", .{}), + }; + + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get)); + appendReservedUleb32(code, 0); + + // Outer block that computes table offset. + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.block)); + code.appendAssumeCapacity(@intFromEnum(outer_block_type)); + + for (tag_values, 0..) |tag_value, tag_index| { + // block for this if case + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.block)); + code.appendAssumeCapacity(@intFromEnum(std.wasm.BlockType.empty)); + + // Tag value whose name should be returned. + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get)); + appendReservedUleb32(code, 1); + + const val: Zcu.Value = .fromInterned(tag_value); + switch (outer_block_type) { + .i32 => { + const x: u32 = switch (int_info.signedness) { + .signed => @bitCast(@as(i32, @intCast(val.toSignedInt(zcu)))), + .unsigned => @intCast(val.toUnsignedInt(zcu)), + }; + appendReservedI32Const(code, x); + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_ne)); + }, + .i64 => { + const x: u64 = switch (int_info.signedness) { + .signed => @bitCast(val.toSignedInt(zcu)), + .unsigned => val.toUnsignedInt(zcu), + }; + appendReservedI64Const(code, x); + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_ne)); + }, + else => unreachable, + } + + // if they're not equal, break out of current branch + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br_if)); + appendReservedUleb32(code, 0); + + // Put the table offset of the result on the stack. + appendReservedI32Const(code, @intCast(tag_index * slice_abi_size)); + + // break outside blocks + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br)); + appendReservedUleb32(code, 1); + + // end the block for this case + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); + } + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.@"unreachable")); + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); + + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_load)); + appendReservedUleb32(code, encoded_alignment); + appendReservedUleb32(code, table_base_addr + table_index * 8); + + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_store)); + appendReservedUleb32(code, encoded_alignment); + appendReservedUleb32(code, 0); + } + + // End of the function body + code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); +} + +/// Writes an unsigned 32-bit integer as a LEB128-encoded 'i32.const' value. +fn appendReservedI32Const(bytes: *std.ArrayListUnmanaged(u8), val: u32) void { + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const)); + leb.writeIleb128(bytes.fixedWriter(), @as(i32, @bitCast(val))) catch unreachable; +} + +/// Writes an unsigned 64-bit integer as a LEB128-encoded 'i64.const' value. +fn appendReservedI64Const(bytes: *std.ArrayListUnmanaged(u8), val: u64) void { + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_const)); + leb.writeIleb128(bytes.fixedWriter(), @as(i64, @bitCast(val))) catch unreachable; +} + +fn appendReservedUleb32(bytes: *std.ArrayListUnmanaged(u8), val: u32) void { + leb.writeUleb128(bytes.fixedWriter(), val) catch unreachable; +} + +fn appendGlobal(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8), mutable: u8, val: u32) Allocator.Error!void { + try bytes.ensureUnusedCapacity(gpa, 9); + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Valtype.i32)); + bytes.appendAssumeCapacity(mutable); + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const)); + appendReservedUleb32(bytes, val); + bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); +} diff --git a/src/link/Wasm/Object.zig b/src/link/Wasm/Object.zig index fc11f91876..4b9807fdf2 100644 --- a/src/link/Wasm/Object.zig +++ b/src/link/Wasm/Object.zig @@ -1,841 +1,1520 @@ -//! Object represents a wasm object file. When initializing a new -//! `Object`, it will parse the contents of a given file handler, and verify -//! the data on correctness. The result can then be used by the linker. const Object = @This(); const Wasm = @import("../Wasm.zig"); -const Atom = Wasm.Atom; const Alignment = Wasm.Alignment; -const Symbol = @import("Symbol.zig"); const std = @import("std"); const Allocator = std.mem.Allocator; -const leb = std.leb; -const meta = std.meta; const Path = std.Build.Cache.Path; - const log = std.log.scoped(.object); +const assert = std.debug.assert; /// Wasm spec version used for this `Object` -version: u32 = 0, +version: u32, /// For error reporting purposes only. /// Name (read path) of the object or archive file. path: Path, /// For error reporting purposes only. /// If this represents an object in an archive, it's the basename of the /// object, and path refers to the archive. -archive_member_name: ?[]const u8, -/// Parsed type section -func_types: []const std.wasm.Type = &.{}, -/// A list of all imports for this module -imports: []const Wasm.Import = &.{}, -/// Parsed function section -functions: []const std.wasm.Func = &.{}, -/// Parsed table section -tables: []const std.wasm.Table = &.{}, -/// Parsed memory section -memories: []const std.wasm.Memory = &.{}, -/// Parsed global section -globals: []const std.wasm.Global = &.{}, -/// Parsed export section -exports: []const Wasm.Export = &.{}, -/// Parsed element section -elements: []const std.wasm.Element = &.{}, +archive_member_name: Wasm.OptionalString, /// Represents the function ID that must be called on startup. /// This is `null` by default as runtimes may determine the startup /// function themselves. This is essentially legacy. -start: ?u32 = null, -/// A slice of features that tell the linker what features are mandatory, -/// used (or therefore missing) and must generate an error when another -/// object uses features that are not supported by the other. -features: []const Wasm.Feature = &.{}, -/// A table that maps the relocations we must perform where the key represents -/// the section that the list of relocations applies to. -relocations: std.AutoArrayHashMapUnmanaged(u32, []Wasm.Relocation) = .empty, -/// Table of symbols belonging to this Object file -symtable: []Symbol = &.{}, -/// Extra metadata about the linking section, such as alignment of segments and their name -segment_info: []const Wasm.NamedSegment = &.{}, -/// A sequence of function initializers that must be called on startup -init_funcs: []const Wasm.InitFunc = &.{}, -/// Comdat information -comdat_info: []const Wasm.Comdat = &.{}, -/// Represents non-synthetic sections that can essentially be mem-cpy'd into place -/// after performing relocations. -relocatable_data: std.AutoHashMapUnmanaged(RelocatableData.Tag, []RelocatableData) = .empty, -/// Amount of functions in the `import` sections. -imported_functions_count: u32 = 0, -/// Amount of globals in the `import` section. -imported_globals_count: u32 = 0, -/// Amount of tables in the `import` section. -imported_tables_count: u32 = 0, - -/// Represents a single item within a section (depending on its `type`) -pub const RelocatableData = struct { - /// The type of the relocatable data - type: Tag, - /// Pointer to the data of the segment, where its length is written to `size` - data: [*]u8, - /// The size in bytes of the data representing the segment within the section - size: u32, - /// The index within the section itself, or in case of a debug section, - /// the offset within the `string_table`. - index: u32, - /// The offset within the section where the data starts - offset: u32, - /// Represents the index of the section it belongs to - section_index: u32, - /// Whether the relocatable section is represented by a symbol or not. - /// Can only be `true` for custom sections. - represented: bool = false, - - const Tag = enum { data, code, custom }; - - /// Returns the alignment of the segment, by retrieving it from the segment - /// meta data of the given object file. - /// NOTE: Alignment is encoded as a power of 2, so we shift the symbol's - /// alignment to retrieve the natural alignment. - pub fn getAlignment(relocatable_data: RelocatableData, object: *const Object) Alignment { - if (relocatable_data.type != .data) return .@"1"; - return object.segment_info[relocatable_data.index].alignment; - } - - /// Returns the symbol kind that corresponds to the relocatable section - pub fn getSymbolKind(relocatable_data: RelocatableData) Symbol.Tag { - return switch (relocatable_data.type) { - .data => .data, - .code => .function, - .custom => .section, - }; - } - - /// Returns the index within a section, or in case of a custom section, - /// returns the section index within the object file. - pub fn getIndex(relocatable_data: RelocatableData) u32 { - if (relocatable_data.type == .custom) return relocatable_data.section_index; - return relocatable_data.index; - } +start_function: Wasm.OptionalObjectFunctionIndex, +/// A slice of features that tell the linker what features are mandatory, used +/// (or therefore missing) and must generate an error when another object uses +/// features that are not supported by the other. +features: Wasm.Feature.Set, +/// Points into `Wasm.object_functions` +functions: RelativeSlice, +/// Points into `Wasm.object_function_imports` +function_imports: RelativeSlice, +/// Points into `Wasm.object_global_imports` +global_imports: RelativeSlice, +/// Points into `Wasm.object_table_imports` +table_imports: RelativeSlice, +// Points into `Wasm.object_data_imports` +data_imports: RelativeSlice, +/// Points into Wasm object_custom_segments +custom_segments: RelativeSlice, +/// Points into Wasm object_init_funcs +init_funcs: RelativeSlice, +/// Points into Wasm object_comdats +comdats: RelativeSlice, +/// Guaranteed to be non-null when functions has nonzero length. +code_section_index: ?Wasm.ObjectSectionIndex, +/// Guaranteed to be non-null when globals has nonzero length. +global_section_index: ?Wasm.ObjectSectionIndex, +/// Guaranteed to be non-null when data segments has nonzero length. +data_section_index: ?Wasm.ObjectSectionIndex, +is_included: bool, + +pub const RelativeSlice = struct { + off: u32, + len: u32, }; -/// Initializes a new `Object` from a wasm object file. -/// This also parses and verifies the object file. -/// When a max size is given, will only parse up to the given size, -/// else will read until the end of the file. -pub fn create( - wasm: *Wasm, - file_contents: []const u8, - path: Path, - archive_member_name: ?[]const u8, -) !Object { - const gpa = wasm.base.comp.gpa; - var object: Object = .{ - .path = path, - .archive_member_name = archive_member_name, +pub const SegmentInfo = struct { + name: Wasm.String, + flags: Flags, + + /// Matches the ABI. + pub const Flags = packed struct(u32) { + /// Signals that the segment contains only null terminated strings allowing + /// the linker to perform merging. + strings: bool, + /// The segment contains thread-local data. This means that a unique copy + /// of this segment will be created for each thread. + tls: bool, + /// If the object file is included in the final link, the segment should be + /// retained in the final output regardless of whether it is used by the + /// program. + retain: bool, + alignment: Alignment, + + _: u23 = 0, }; +}; - var parser: Parser = .{ - .object = &object, - .wasm = wasm, - .reader = std.io.fixedBufferStream(file_contents), - }; - try parser.parseObject(gpa); +pub const FunctionImport = struct { + module_name: Wasm.String, + name: Wasm.String, + function_index: ScratchSpace.FuncTypeIndex, +}; - return object; -} +pub const GlobalImport = struct { + module_name: Wasm.String, + name: Wasm.String, + valtype: std.wasm.Valtype, + mutable: bool, +}; -/// Frees all memory of `Object` at once. The given `Allocator` must be -/// the same allocator that was used when `init` was called. -pub fn deinit(object: *Object, gpa: Allocator) void { - for (object.func_types) |func_ty| { - gpa.free(func_ty.params); - gpa.free(func_ty.returns); - } - gpa.free(object.func_types); - gpa.free(object.functions); - gpa.free(object.imports); - gpa.free(object.tables); - gpa.free(object.memories); - gpa.free(object.globals); - gpa.free(object.exports); - for (object.elements) |el| { - gpa.free(el.func_indexes); - } - gpa.free(object.elements); - gpa.free(object.features); - for (object.relocations.values()) |val| { - gpa.free(val); - } - object.relocations.deinit(gpa); - gpa.free(object.symtable); - gpa.free(object.comdat_info); - gpa.free(object.init_funcs); - for (object.segment_info) |info| { - gpa.free(info.name); - } - gpa.free(object.segment_info); - { - var it = object.relocatable_data.valueIterator(); - while (it.next()) |relocatable_data| { - for (relocatable_data.*) |rel_data| { - gpa.free(rel_data.data[0..rel_data.size]); - } - gpa.free(relocatable_data.*); - } - } - object.relocatable_data.deinit(gpa); - object.* = undefined; -} +pub const TableImport = struct { + module_name: Wasm.String, + name: Wasm.String, + limits_min: u32, + limits_max: u32, + limits_has_max: bool, + limits_is_shared: bool, + ref_type: std.wasm.RefType, +}; -/// Finds the import within the list of imports from a given kind and index of that kind. -/// Asserts the import exists -pub fn findImport(object: *const Object, sym: Symbol) Wasm.Import { - var i: u32 = 0; - return for (object.imports) |import| { - if (std.meta.activeTag(import.kind) == sym.tag.externalType()) { - if (i == sym.index) return import; - i += 1; - } - } else unreachable; // Only existing imports are allowed to be found -} +pub const DataSegmentFlags = enum(u32) { active, passive, active_memidx }; -/// Checks if the object file is an MVP version. -/// When that's the case, we check if there's an import table definition with its name -/// set to '__indirect_function_table". When that's also the case, -/// we initialize a new table symbol that corresponds to that import and return that symbol. -/// -/// When the object file is *NOT* MVP, we return `null`. -fn checkLegacyIndirectFunctionTable(object: *Object, wasm: *const Wasm) !?Symbol { - const diags = &wasm.base.comp.link_diags; +pub const SubsectionType = enum(u8) { + segment_info = 5, + init_funcs = 6, + comdat_info = 7, + symbol_table = 8, +}; - var table_count: usize = 0; - for (object.symtable) |sym| { - if (sym.tag == .table) table_count += 1; - } +/// Specified by https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md +pub const RelocationType = enum(u8) { + function_index_leb = 0, + table_index_sleb = 1, + table_index_i32 = 2, + memory_addr_leb = 3, + memory_addr_sleb = 4, + memory_addr_i32 = 5, + type_index_leb = 6, + global_index_leb = 7, + function_offset_i32 = 8, + section_offset_i32 = 9, + event_index_leb = 10, + memory_addr_rel_sleb = 11, + table_index_rel_sleb = 12, + global_index_i32 = 13, + memory_addr_leb64 = 14, + memory_addr_sleb64 = 15, + memory_addr_i64 = 16, + memory_addr_rel_sleb64 = 17, + table_index_sleb64 = 18, + table_index_i64 = 19, + table_number_leb = 20, + memory_addr_tls_sleb = 21, + function_offset_i64 = 22, + memory_addr_locrel_i32 = 23, + table_index_rel_sleb64 = 24, + memory_addr_tls_sleb64 = 25, + function_index_i32 = 26, +}; - // For each import table, we also have a symbol so this is not a legacy object file - if (object.imported_tables_count == table_count) return null; +pub const Symbol = struct { + flags: Wasm.SymbolFlags, + name: Wasm.OptionalString, + pointee: Pointee, + + /// https://github.com/WebAssembly/tool-conventions/blob/df8d737539eb8a8f446ba5eab9dc670c40dfb81e/Linking.md#symbol-table-subsection + const Tag = enum(u8) { + function, + data, + global, + section, + event, + table, + }; - if (table_count != 0) { - return diags.failParse(object.path, "expected a table entry symbol for each of the {d} table(s), but instead got {d} symbols.", .{ - object.imported_tables_count, - table_count, - }); - } + const Pointee = union(enum) { + function: Wasm.ObjectFunctionIndex, + function_import: ScratchSpace.FuncImportIndex, + data: Wasm.ObjectData.Index, + data_import: void, + global: Wasm.ObjectGlobalIndex, + global_import: ScratchSpace.GlobalImportIndex, + section: Wasm.ObjectSectionIndex, + table: Wasm.ObjectTableIndex, + table_import: ScratchSpace.TableImportIndex, + }; +}; - // MVP object files cannot have any table definitions, only imports (for the indirect function table). - if (object.tables.len > 0) { - return diags.failParse(object.path, "unexpected table definition without representing table symbols.", .{}); - } +pub const ScratchSpace = struct { + func_types: std.ArrayListUnmanaged(Wasm.FunctionType.Index) = .empty, + func_type_indexes: std.ArrayListUnmanaged(FuncTypeIndex) = .empty, + func_imports: std.ArrayListUnmanaged(FunctionImport) = .empty, + global_imports: std.ArrayListUnmanaged(GlobalImport) = .empty, + table_imports: std.ArrayListUnmanaged(TableImport) = .empty, + symbol_table: std.ArrayListUnmanaged(Symbol) = .empty, + segment_info: std.ArrayListUnmanaged(SegmentInfo) = .empty, + exports: std.ArrayListUnmanaged(Export) = .empty, + + const Export = struct { + name: Wasm.String, + pointee: Pointee, + + const Pointee = union(std.wasm.ExternalKind) { + function: Wasm.ObjectFunctionIndex, + table: Wasm.ObjectTableIndex, + memory: Wasm.ObjectMemory.Index, + global: Wasm.ObjectGlobalIndex, + }; + }; - if (object.imported_tables_count != 1) { - return diags.failParse(object.path, "found more than one table import, but no representing table symbols", .{}); - } + /// Index into `func_imports`. + const FuncImportIndex = enum(u32) { + _, - const table_import: Wasm.Import = for (object.imports) |imp| { - if (imp.kind == .table) { - break imp; + fn ptr(index: FuncImportIndex, ss: *const ScratchSpace) *FunctionImport { + return &ss.func_imports.items[@intFromEnum(index)]; } - } else unreachable; + }; - if (table_import.name != wasm.preloaded_strings.__indirect_function_table) { - return diags.failParse(object.path, "non-indirect function table import '{s}' is missing a corresponding symbol", .{ - wasm.stringSlice(table_import.name), - }); - } + /// Index into `global_imports`. + const GlobalImportIndex = enum(u32) { + _, - var table_symbol: Symbol = .{ - .flags = 0, - .name = table_import.name, - .tag = .table, - .index = 0, - .virtual_address = undefined, + fn ptr(index: GlobalImportIndex, ss: *const ScratchSpace) *GlobalImport { + return &ss.global_imports.items[@intFromEnum(index)]; + } }; - table_symbol.setFlag(.WASM_SYM_UNDEFINED); - table_symbol.setFlag(.WASM_SYM_NO_STRIP); - return table_symbol; -} -const Parser = struct { - reader: std.io.FixedBufferStream([]const u8), - /// Object file we're building - object: *Object, - /// Mutable so that the string table can be modified. - wasm: *Wasm, + /// Index into `table_imports`. + const TableImportIndex = enum(u32) { + _, + + fn ptr(index: TableImportIndex, ss: *const ScratchSpace) *TableImport { + return &ss.table_imports.items[@intFromEnum(index)]; + } + }; - fn parseObject(parser: *Parser, gpa: Allocator) anyerror!void { - const wasm = parser.wasm; + /// Index into `func_types`. + const FuncTypeIndex = enum(u32) { + _, - { - var magic_bytes: [4]u8 = undefined; - try parser.reader.reader().readNoEof(&magic_bytes); - if (!std.mem.eql(u8, &magic_bytes, &std.wasm.magic)) return error.BadObjectMagic; + fn ptr(index: FuncTypeIndex, ss: *const ScratchSpace) *Wasm.FunctionType.Index { + return &ss.func_types.items[@intFromEnum(index)]; } + }; - const version = try parser.reader.reader().readInt(u32, .little); - parser.object.version = version; - - var saw_linking_section = false; - - var section_index: u32 = 0; - while (parser.reader.reader().readByte()) |byte| : (section_index += 1) { - const len = try readLeb(u32, parser.reader.reader()); - var limited_reader = std.io.limitedReader(parser.reader.reader(), len); - const reader = limited_reader.reader(); - switch (@as(std.wasm.Section, @enumFromInt(byte))) { - .custom => { - const name_len = try readLeb(u32, reader); - const name = try gpa.alloc(u8, name_len); - defer gpa.free(name); - try reader.readNoEof(name); - - if (std.mem.eql(u8, name, "linking")) { - saw_linking_section = true; - try parser.parseMetadata(gpa, @as(usize, @intCast(reader.context.bytes_left))); - } else if (std.mem.startsWith(u8, name, "reloc")) { - try parser.parseRelocations(gpa); - } else if (std.mem.eql(u8, name, "target_features")) { - try parser.parseFeatures(gpa); - } else if (std.mem.startsWith(u8, name, ".debug")) { - const gop = try parser.object.relocatable_data.getOrPut(gpa, .custom); - var relocatable_data: std.ArrayListUnmanaged(RelocatableData) = .empty; - defer relocatable_data.deinit(gpa); - if (!gop.found_existing) { - gop.value_ptr.* = &.{}; - } else { - relocatable_data = std.ArrayListUnmanaged(RelocatableData).fromOwnedSlice(gop.value_ptr.*); - } - const debug_size = @as(u32, @intCast(reader.context.bytes_left)); - const debug_content = try gpa.alloc(u8, debug_size); - errdefer gpa.free(debug_content); - try reader.readNoEof(debug_content); - - try relocatable_data.append(gpa, .{ - .type = .custom, - .data = debug_content.ptr, - .size = debug_size, - .index = @intFromEnum(try wasm.internString(name)), - .offset = 0, // debug sections only contain 1 entry, so no need to calculate offset - .section_index = section_index, - }); - gop.value_ptr.* = try relocatable_data.toOwnedSlice(gpa); - } else { - try reader.skipBytes(reader.context.bytes_left, .{}); - } - }, - .type => { - for (try readVec(&parser.object.func_types, reader, gpa)) |*type_val| { - if ((try reader.readByte()) != std.wasm.function_type) return error.ExpectedFuncType; + pub fn deinit(ss: *ScratchSpace, gpa: Allocator) void { + ss.exports.deinit(gpa); + ss.func_types.deinit(gpa); + ss.func_type_indexes.deinit(gpa); + ss.func_imports.deinit(gpa); + ss.global_imports.deinit(gpa); + ss.table_imports.deinit(gpa); + ss.symbol_table.deinit(gpa); + ss.segment_info.deinit(gpa); + ss.* = undefined; + } - for (try readVec(&type_val.params, reader, gpa)) |*param| { - param.* = try readEnum(std.wasm.Valtype, reader); - } + fn clear(ss: *ScratchSpace) void { + ss.exports.clearRetainingCapacity(); + ss.func_types.clearRetainingCapacity(); + ss.func_type_indexes.clearRetainingCapacity(); + ss.func_imports.clearRetainingCapacity(); + ss.global_imports.clearRetainingCapacity(); + ss.table_imports.clearRetainingCapacity(); + ss.symbol_table.clearRetainingCapacity(); + ss.segment_info.clearRetainingCapacity(); + } +}; - for (try readVec(&type_val.returns, reader, gpa)) |*result| { - result.* = try readEnum(std.wasm.Valtype, reader); +pub fn parse( + wasm: *Wasm, + bytes: []const u8, + path: Path, + archive_member_name: ?[]const u8, + host_name: Wasm.OptionalString, + ss: *ScratchSpace, + must_link: bool, + gc_sections: bool, +) anyerror!Object { + const comp = wasm.base.comp; + const gpa = comp.gpa; + const diags = &comp.link_diags; + + var pos: usize = 0; + + if (!std.mem.eql(u8, bytes[0..std.wasm.magic.len], &std.wasm.magic)) return error.BadObjectMagic; + pos += std.wasm.magic.len; + + const version = std.mem.readInt(u32, bytes[pos..][0..4], .little); + pos += 4; + + const data_segment_start: u32 = @intCast(wasm.object_data_segments.items.len); + const custom_segment_start: u32 = @intCast(wasm.object_custom_segments.entries.len); + const functions_start: u32 = @intCast(wasm.object_functions.items.len); + const tables_start: u32 = @intCast(wasm.object_tables.items.len); + const memories_start: u32 = @intCast(wasm.object_memories.items.len); + const globals_start: u32 = @intCast(wasm.object_globals.items.len); + const init_funcs_start: u32 = @intCast(wasm.object_init_funcs.items.len); + const comdats_start: u32 = @intCast(wasm.object_comdats.items.len); + const function_imports_start: u32 = @intCast(wasm.object_function_imports.entries.len); + const global_imports_start: u32 = @intCast(wasm.object_global_imports.entries.len); + const table_imports_start: u32 = @intCast(wasm.object_table_imports.entries.len); + const data_imports_start: u32 = @intCast(wasm.object_data_imports.entries.len); + const local_section_index_base = wasm.object_total_sections; + const object_index: Wasm.ObjectIndex = @enumFromInt(wasm.objects.items.len); + const source_location: Wasm.SourceLocation = .fromObject(object_index, wasm); + + ss.clear(); + + var start_function: Wasm.OptionalObjectFunctionIndex = .none; + var opt_features: ?Wasm.Feature.Set = null; + var saw_linking_section = false; + var has_tls = false; + var table_import_symbol_count: usize = 0; + var code_section_index: ?Wasm.ObjectSectionIndex = null; + var global_section_index: ?Wasm.ObjectSectionIndex = null; + var data_section_index: ?Wasm.ObjectSectionIndex = null; + while (pos < bytes.len) : (wasm.object_total_sections += 1) { + const section_index: Wasm.ObjectSectionIndex = @enumFromInt(wasm.object_total_sections); + + const section_tag: std.wasm.Section = @enumFromInt(bytes[pos]); + pos += 1; + + const len, pos = readLeb(u32, bytes, pos); + const section_end = pos + len; + switch (section_tag) { + .custom => { + const section_name, pos = readBytes(bytes, pos); + if (std.mem.eql(u8, section_name, "linking")) { + saw_linking_section = true; + const section_version, pos = readLeb(u32, bytes, pos); + log.debug("link meta data version: {d}", .{section_version}); + if (section_version != 2) return error.UnsupportedVersion; + while (pos < section_end) { + const sub_type, pos = readLeb(u8, bytes, pos); + log.debug("found subsection: {s}", .{@tagName(@as(SubsectionType, @enumFromInt(sub_type)))}); + const payload_len, pos = readLeb(u32, bytes, pos); + if (payload_len == 0) break; + + const count, pos = readLeb(u32, bytes, pos); + + switch (@as(SubsectionType, @enumFromInt(sub_type))) { + .segment_info => { + for (try ss.segment_info.addManyAsSlice(gpa, count)) |*segment| { + const name, pos = readBytes(bytes, pos); + const alignment, pos = readLeb(u32, bytes, pos); + const flags_u32, pos = readLeb(u32, bytes, pos); + const flags: SegmentInfo.Flags = @bitCast(flags_u32); + const tls = flags.tls or + // Supports legacy object files that specified + // being TLS by the name instead of the TLS flag. + std.mem.startsWith(u8, name, ".tdata") or + std.mem.startsWith(u8, name, ".tbss"); + has_tls = has_tls or tls; + segment.* = .{ + .name = try wasm.internString(name), + .flags = .{ + .strings = flags.strings, + .tls = tls, + .alignment = @enumFromInt(alignment), + .retain = flags.retain, + }, + }; + } + }, + .init_funcs => { + for (try wasm.object_init_funcs.addManyAsSlice(gpa, count)) |*func| { + const priority, pos = readLeb(u32, bytes, pos); + const symbol_index, pos = readLeb(u32, bytes, pos); + if (symbol_index > ss.symbol_table.items.len) + return diags.failParse(path, "init_funcs before symbol table", .{}); + const sym = &ss.symbol_table.items[symbol_index]; + if (sym.pointee != .function) { + return diags.failParse(path, "init_func symbol '{s}' not a function", .{ + sym.name.slice(wasm).?, + }); + } else if (sym.flags.undefined) { + return diags.failParse(path, "init_func symbol '{s}' is an import", .{ + sym.name.slice(wasm).?, + }); + } + func.* = .{ + .priority = priority, + .function_index = sym.pointee.function, + }; + } + }, + .comdat_info => { + for (try wasm.object_comdats.addManyAsSlice(gpa, count)) |*comdat| { + const name, pos = readBytes(bytes, pos); + const flags, pos = readLeb(u32, bytes, pos); + if (flags != 0) return error.UnexpectedComdatFlags; + const symbol_count, pos = readLeb(u32, bytes, pos); + const start_off: u32 = @intCast(wasm.object_comdat_symbols.len); + try wasm.object_comdat_symbols.ensureUnusedCapacity(gpa, symbol_count); + for (0..symbol_count) |_| { + const kind, pos = readEnum(Wasm.Comdat.Symbol.Type, bytes, pos); + const index, pos = readLeb(u32, bytes, pos); + if (true) @panic("TODO rebase index depending on kind"); + wasm.object_comdat_symbols.appendAssumeCapacity(.{ + .kind = kind, + .index = index, + }); + } + comdat.* = .{ + .name = try wasm.internString(name), + .flags = flags, + .symbols = .{ + .off = start_off, + .len = @intCast(wasm.object_comdat_symbols.len - start_off), + }, + }; + } + }, + .symbol_table => { + for (try ss.symbol_table.addManyAsSlice(gpa, count)) |*symbol| { + const tag, pos = readEnum(Symbol.Tag, bytes, pos); + const flags, pos = readLeb(u32, bytes, pos); + symbol.* = .{ + .flags = @bitCast(flags), + .name = .none, + .pointee = undefined, + }; + symbol.flags.initZigSpecific(must_link, gc_sections); + + switch (tag) { + .data => { + const name, pos = readBytes(bytes, pos); + const interned_name = try wasm.internString(name); + symbol.name = interned_name.toOptional(); + if (symbol.flags.undefined) { + symbol.pointee = .data_import; + } else { + const segment_index, pos = readLeb(u32, bytes, pos); + const segment_offset, pos = readLeb(u32, bytes, pos); + const size, pos = readLeb(u32, bytes, pos); + try wasm.object_datas.append(gpa, .{ + .segment = @enumFromInt(data_segment_start + segment_index), + .offset = segment_offset, + .size = size, + .name = interned_name, + .flags = symbol.flags, + }); + symbol.pointee = .{ + .data = @enumFromInt(wasm.object_datas.items.len - 1), + }; + } + }, + .section => { + const local_section, pos = readLeb(u32, bytes, pos); + const section: Wasm.ObjectSectionIndex = @enumFromInt(local_section_index_base + local_section); + symbol.pointee = .{ .section = section }; + }, + + .function => { + const local_index, pos = readLeb(u32, bytes, pos); + if (symbol.flags.undefined) { + const function_import: ScratchSpace.FuncImportIndex = @enumFromInt(local_index); + symbol.pointee = .{ .function_import = function_import }; + if (symbol.flags.explicit_name) { + const name, pos = readBytes(bytes, pos); + symbol.name = (try wasm.internString(name)).toOptional(); + } else { + symbol.name = function_import.ptr(ss).name.toOptional(); + } + } else { + symbol.pointee = .{ .function = @enumFromInt(functions_start + (local_index - ss.func_imports.items.len)) }; + const name, pos = readBytes(bytes, pos); + symbol.name = (try wasm.internString(name)).toOptional(); + } + }, + .global => { + const local_index, pos = readLeb(u32, bytes, pos); + if (symbol.flags.undefined) { + const global_import: ScratchSpace.GlobalImportIndex = @enumFromInt(local_index); + symbol.pointee = .{ .global_import = global_import }; + if (symbol.flags.explicit_name) { + const name, pos = readBytes(bytes, pos); + symbol.name = (try wasm.internString(name)).toOptional(); + } else { + symbol.name = global_import.ptr(ss).name.toOptional(); + } + } else { + symbol.pointee = .{ .global = @enumFromInt(globals_start + (local_index - ss.global_imports.items.len)) }; + const name, pos = readBytes(bytes, pos); + symbol.name = (try wasm.internString(name)).toOptional(); + } + }, + .table => { + const local_index, pos = readLeb(u32, bytes, pos); + if (symbol.flags.undefined) { + table_import_symbol_count += 1; + const table_import: ScratchSpace.TableImportIndex = @enumFromInt(local_index); + symbol.pointee = .{ .table_import = table_import }; + if (symbol.flags.explicit_name) { + const name, pos = readBytes(bytes, pos); + symbol.name = (try wasm.internString(name)).toOptional(); + } else { + symbol.name = table_import.ptr(ss).name.toOptional(); + } + } else { + symbol.pointee = .{ .table = @enumFromInt(tables_start + (local_index - ss.table_imports.items.len)) }; + const name, pos = readBytes(bytes, pos); + symbol.name = (try wasm.internString(name)).toOptional(); + } + }, + else => { + log.debug("unrecognized symbol type tag: {x}", .{@intFromEnum(tag)}); + return error.UnrecognizedSymbolType; + }, + } + } + }, } } - try assertEnd(reader); - }, - .import => { - for (try readVec(&parser.object.imports, reader, gpa)) |*import| { - const module_len = try readLeb(u32, reader); - const module_name = try gpa.alloc(u8, module_len); - defer gpa.free(module_name); - try reader.readNoEof(module_name); - - const name_len = try readLeb(u32, reader); - const name = try gpa.alloc(u8, name_len); - defer gpa.free(name); - try reader.readNoEof(name); - - const kind = try readEnum(std.wasm.ExternalKind, reader); - const kind_value: std.wasm.Import.Kind = switch (kind) { - .function => val: { - parser.object.imported_functions_count += 1; - break :val .{ .function = try readLeb(u32, reader) }; + } else if (std.mem.startsWith(u8, section_name, "reloc.")) { + // 'The "reloc." custom sections must come after the "linking" custom section' + if (!saw_linking_section) return error.RelocBeforeLinkingSection; + + // "Relocation sections start with an identifier specifying + // which section they apply to, and must be sequenced in + // the module after that section." + // "Relocation sections can only target code, data and custom sections." + const local_section, pos = readLeb(u32, bytes, pos); + const count, pos = readLeb(u32, bytes, pos); + const section: Wasm.ObjectSectionIndex = @enumFromInt(local_section_index_base + local_section); + + log.debug("found {d} relocations for section={d}", .{ count, section }); + + var prev_offset: u32 = 0; + try wasm.object_relocations.ensureUnusedCapacity(gpa, count); + for (0..count) |_| { + const tag: RelocationType = @enumFromInt(bytes[pos]); + pos += 1; + const offset, pos = readLeb(u32, bytes, pos); + const index, pos = readLeb(u32, bytes, pos); + + if (offset < prev_offset) + return diags.failParse(path, "relocation entries not sorted by offset", .{}); + prev_offset = offset; + + const sym = &ss.symbol_table.items[index]; + + switch (tag) { + .memory_addr_leb, + .memory_addr_sleb, + .memory_addr_i32, + .memory_addr_rel_sleb, + .memory_addr_leb64, + .memory_addr_sleb64, + .memory_addr_i64, + .memory_addr_rel_sleb64, + .memory_addr_tls_sleb, + .memory_addr_locrel_i32, + .memory_addr_tls_sleb64, + => { + const addend: i32, pos = readLeb(i32, bytes, pos); + wasm.object_relocations.appendAssumeCapacity(switch (sym.pointee) { + .data => |data| .{ + .tag = .fromType(tag), + .offset = offset, + .pointee = .{ .data = data }, + .addend = addend, + }, + .data_import => .{ + .tag = .fromTypeImport(tag), + .offset = offset, + .pointee = .{ .symbol_name = sym.name.unwrap().? }, + .addend = addend, + }, + else => unreachable, + }); + }, + .function_offset_i32, .function_offset_i64 => { + const addend: i32, pos = readLeb(i32, bytes, pos); + wasm.object_relocations.appendAssumeCapacity(switch (sym.pointee) { + .function => .{ + .tag = .fromType(tag), + .offset = offset, + .pointee = .{ .function = sym.pointee.function }, + .addend = addend, + }, + .function_import => .{ + .tag = .fromTypeImport(tag), + .offset = offset, + .pointee = .{ .symbol_name = sym.name.unwrap().? }, + .addend = addend, + }, + else => unreachable, + }); }, - .memory => .{ .memory = try readLimits(reader) }, - .global => val: { - parser.object.imported_globals_count += 1; - break :val .{ .global = .{ - .valtype = try readEnum(std.wasm.Valtype, reader), - .mutable = (try reader.readByte()) == 0x01, - } }; + .section_offset_i32 => { + const addend: i32, pos = readLeb(i32, bytes, pos); + wasm.object_relocations.appendAssumeCapacity(.{ + .tag = .section_offset_i32, + .offset = offset, + .pointee = .{ .section = sym.pointee.section }, + .addend = addend, + }); }, - .table => val: { - parser.object.imported_tables_count += 1; - break :val .{ .table = .{ - .reftype = try readEnum(std.wasm.RefType, reader), - .limits = try readLimits(reader), - } }; + .type_index_leb => { + wasm.object_relocations.appendAssumeCapacity(.{ + .tag = .type_index_leb, + .offset = offset, + .pointee = .{ .type_index = ss.func_types.items[index] }, + .addend = undefined, + }); + }, + .function_index_leb, + .function_index_i32, + .table_index_sleb, + .table_index_i32, + .table_index_sleb64, + .table_index_i64, + .table_index_rel_sleb, + .table_index_rel_sleb64, + => { + wasm.object_relocations.appendAssumeCapacity(switch (sym.pointee) { + .function => .{ + .tag = .fromType(tag), + .offset = offset, + .pointee = .{ .function = sym.pointee.function }, + .addend = undefined, + }, + .function_import => .{ + .tag = .fromTypeImport(tag), + .offset = offset, + .pointee = .{ .symbol_name = sym.name.unwrap().? }, + .addend = undefined, + }, + else => unreachable, + }); + }, + .global_index_leb, .global_index_i32 => { + wasm.object_relocations.appendAssumeCapacity(switch (sym.pointee) { + .global => .{ + .tag = .fromType(tag), + .offset = offset, + .pointee = .{ .global = sym.pointee.global }, + .addend = undefined, + }, + .global_import => .{ + .tag = .fromTypeImport(tag), + .offset = offset, + .pointee = .{ .symbol_name = sym.name.unwrap().? }, + .addend = undefined, + }, + else => unreachable, + }); }, - }; - import.* = .{ - .module_name = try wasm.internString(module_name), - .name = try wasm.internString(name), - .kind = kind_value, - }; - } - try assertEnd(reader); - }, - .function => { - for (try readVec(&parser.object.functions, reader, gpa)) |*func| { - func.* = .{ .type_index = try readLeb(u32, reader) }; - } - try assertEnd(reader); - }, - .table => { - for (try readVec(&parser.object.tables, reader, gpa)) |*table| { - table.* = .{ - .reftype = try readEnum(std.wasm.RefType, reader), - .limits = try readLimits(reader), - }; + .table_number_leb => { + wasm.object_relocations.appendAssumeCapacity(switch (sym.pointee) { + .table => .{ + .tag = .fromType(tag), + .offset = offset, + .pointee = .{ .table = sym.pointee.table }, + .addend = undefined, + }, + .table_import => .{ + .tag = .fromTypeImport(tag), + .offset = offset, + .pointee = .{ .symbol_name = sym.name.unwrap().? }, + .addend = undefined, + }, + else => unreachable, + }); + }, + .event_index_leb => return diags.failParse(path, "unsupported relocation: R_WASM_EVENT_INDEX_LEB", .{}), + } } - try assertEnd(reader); - }, - .memory => { - for (try readVec(&parser.object.memories, reader, gpa)) |*memory| { - memory.* = .{ .limits = try readLimits(reader) }; + + try wasm.object_relocations_table.putNoClobber(gpa, section, .{ + .off = @intCast(wasm.object_relocations.len - count), + .len = count, + }); + } else if (std.mem.eql(u8, section_name, "target_features")) { + opt_features, pos = try parseFeatures(wasm, bytes, pos, path); + } else if (std.mem.startsWith(u8, section_name, ".debug")) { + const debug_content = bytes[pos..section_end]; + pos = section_end; + + const data_off: u32 = @intCast(wasm.string_bytes.items.len); + try wasm.string_bytes.appendSlice(gpa, debug_content); + + try wasm.object_custom_segments.put(gpa, section_index, .{ + .payload = .{ + .off = @enumFromInt(data_off), + .len = @intCast(debug_content.len), + }, + .flags = .{}, + .section_name = try wasm.internString(section_name), + }); + } else { + pos = section_end; + } + }, + .type => { + const func_types_len, pos = readLeb(u32, bytes, pos); + for (try ss.func_types.addManyAsSlice(gpa, func_types_len)) |*func_type| { + if (bytes[pos] != std.wasm.function_type) return error.ExpectedFuncType; + pos += 1; + + const params, pos = readBytes(bytes, pos); + const returns, pos = readBytes(bytes, pos); + func_type.* = try wasm.addFuncType(.{ + .params = .fromString(try wasm.internString(params)), + .returns = .fromString(try wasm.internString(returns)), + }); + } + }, + .import => { + const imports_len, pos = readLeb(u32, bytes, pos); + for (0..imports_len) |_| { + const module_name, pos = readBytes(bytes, pos); + const name, pos = readBytes(bytes, pos); + const kind, pos = readEnum(std.wasm.ExternalKind, bytes, pos); + const interned_module_name = try wasm.internString(module_name); + const interned_name = try wasm.internString(name); + switch (kind) { + .function => { + const function, pos = readLeb(u32, bytes, pos); + try ss.func_imports.append(gpa, .{ + .module_name = interned_module_name, + .name = interned_name, + .function_index = @enumFromInt(function), + }); + }, + .memory => { + const limits, pos = readLimits(bytes, pos); + const gop = try wasm.object_memory_imports.getOrPut(gpa, interned_name); + if (gop.found_existing) { + if (gop.value_ptr.module_name != interned_module_name) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("memory '{s}' mismatching module names", .{name}); + gop.value_ptr.source_location.addNote(&err, "module '{s}' here", .{ + gop.value_ptr.module_name.slice(wasm), + }); + source_location.addNote(&err, "module '{s}' here", .{module_name}); + } + // TODO error for mismatching flags + gop.value_ptr.limits_min = @min(gop.value_ptr.limits_min, limits.min); + gop.value_ptr.limits_max = @max(gop.value_ptr.limits_max, limits.max); + } else { + gop.value_ptr.* = .{ + .module_name = interned_module_name, + .limits_min = limits.min, + .limits_max = limits.max, + .limits_has_max = limits.flags.has_max, + .limits_is_shared = limits.flags.is_shared, + .source_location = source_location, + }; + } + }, + .global => { + const valtype, pos = readEnum(std.wasm.Valtype, bytes, pos); + const mutable = bytes[pos] == 0x01; + pos += 1; + try ss.global_imports.append(gpa, .{ + .name = interned_name, + .valtype = valtype, + .mutable = mutable, + .module_name = interned_module_name, + }); + }, + .table => { + const ref_type, pos = readEnum(std.wasm.RefType, bytes, pos); + const limits, pos = readLimits(bytes, pos); + try ss.table_imports.append(gpa, .{ + .name = interned_name, + .module_name = interned_module_name, + .limits_min = limits.min, + .limits_max = limits.max, + .limits_has_max = limits.flags.has_max, + .limits_is_shared = limits.flags.is_shared, + .ref_type = ref_type, + }); + }, } - try assertEnd(reader); - }, - .global => { - for (try readVec(&parser.object.globals, reader, gpa)) |*global| { - global.* = .{ + } + }, + .function => { + const functions_len, pos = readLeb(u32, bytes, pos); + for (try ss.func_type_indexes.addManyAsSlice(gpa, functions_len)) |*func_type_index| { + const i, pos = readLeb(u32, bytes, pos); + func_type_index.* = @enumFromInt(i); + } + }, + .table => { + const tables_len, pos = readLeb(u32, bytes, pos); + for (try wasm.object_tables.addManyAsSlice(gpa, tables_len)) |*table| { + const ref_type, pos = readEnum(std.wasm.RefType, bytes, pos); + const limits, pos = readLimits(bytes, pos); + table.* = .{ + .name = .none, + .module_name = .none, + .flags = .{ + .ref_type = .from(ref_type), + .limits_has_max = limits.flags.has_max, + .limits_is_shared = limits.flags.is_shared, + }, + .limits_min = limits.min, + .limits_max = limits.max, + }; + } + }, + .memory => { + const memories_len, pos = readLeb(u32, bytes, pos); + for (try wasm.object_memories.addManyAsSlice(gpa, memories_len)) |*memory| { + const limits, pos = readLimits(bytes, pos); + memory.* = .{ + .name = .none, + .flags = .{ + .limits_has_max = limits.flags.has_max, + .limits_is_shared = limits.flags.is_shared, + }, + .limits_min = limits.min, + .limits_max = limits.max, + }; + } + }, + .global => { + if (global_section_index != null) + return diags.failParse(path, "object has more than one global section", .{}); + global_section_index = section_index; + + const section_start = pos; + const globals_len, pos = readLeb(u32, bytes, pos); + for (try wasm.object_globals.addManyAsSlice(gpa, globals_len)) |*global| { + const valtype, pos = readEnum(std.wasm.Valtype, bytes, pos); + const mutable = bytes[pos] == 0x01; + pos += 1; + const init_start = pos; + const expr, pos = try readInit(wasm, bytes, pos); + global.* = .{ + .name = .none, + .flags = .{ .global_type = .{ - .valtype = try readEnum(std.wasm.Valtype, reader), - .mutable = (try reader.readByte()) == 0x01, + .valtype = .from(valtype), + .mutable = mutable, }, - .init = try readInit(reader), - }; - } - try assertEnd(reader); - }, - .@"export" => { - for (try readVec(&parser.object.exports, reader, gpa)) |*exp| { - const name_len = try readLeb(u32, reader); - const name = try gpa.alloc(u8, name_len); - defer gpa.free(name); - try reader.readNoEof(name); - exp.* = .{ - .name = try wasm.internString(name), - .kind = try readEnum(std.wasm.ExternalKind, reader), - .index = try readLeb(u32, reader), - }; + }, + .expr = expr, + .object_index = object_index, + .offset = @intCast(init_start - section_start), + .size = @intCast(pos - init_start), + }; + } + }, + .@"export" => { + const exports_len, pos = readLeb(u32, bytes, pos); + // Read into scratch space, and then later add this data as if + // it were extra symbol table entries, but allow merging with + // existing symbol table data if the name matches. + for (try ss.exports.addManyAsSlice(gpa, exports_len)) |*exp| { + const name, pos = readBytes(bytes, pos); + const kind: std.wasm.ExternalKind = @enumFromInt(bytes[pos]); + pos += 1; + const index, pos = readLeb(u32, bytes, pos); + exp.* = .{ + .name = try wasm.internString(name), + .pointee = switch (kind) { + .function => .{ .function = @enumFromInt(functions_start + (index - ss.func_imports.items.len)) }, + .table => .{ .table = @enumFromInt(tables_start + (index - ss.table_imports.items.len)) }, + .memory => .{ .memory = @enumFromInt(memories_start + index) }, + .global => .{ .global = @enumFromInt(globals_start + (index - ss.global_imports.items.len)) }, + }, + }; + } + }, + .start => { + const index, pos = readLeb(u32, bytes, pos); + start_function = @enumFromInt(functions_start + index); + }, + .element => { + log.warn("unimplemented: element section in {} {?s}", .{ path, archive_member_name }); + pos = section_end; + }, + .code => { + if (code_section_index != null) + return diags.failParse(path, "object has more than one code section", .{}); + code_section_index = section_index; + + const start = pos; + const count, pos = readLeb(u32, bytes, pos); + for (try wasm.object_functions.addManyAsSlice(gpa, count)) |*elem| { + const code_len, pos = readLeb(u32, bytes, pos); + const offset: u32 = @intCast(pos - start); + const payload = try wasm.addRelocatableDataPayload(bytes[pos..][0..code_len]); + pos += code_len; + elem.* = .{ + .flags = .{}, // populated from symbol table + .name = .none, // populated from symbol table + .type_index = undefined, // populated from func_types + .code = payload, + .offset = offset, + .object_index = object_index, + }; + } + }, + .data => { + if (data_section_index != null) + return diags.failParse(path, "object has more than one data section", .{}); + data_section_index = section_index; + + const section_start = pos; + const count, pos = readLeb(u32, bytes, pos); + for (try wasm.object_data_segments.addManyAsSlice(gpa, count)) |*elem| { + const flags, pos = readEnum(DataSegmentFlags, bytes, pos); + if (flags == .active_memidx) { + const memidx, pos = readLeb(u32, bytes, pos); + if (memidx != 0) return diags.failParse(path, "data section uses mem index {d}", .{memidx}); } - try assertEnd(reader); - }, - .start => { - parser.object.start = try readLeb(u32, reader); - try assertEnd(reader); - }, - .element => { - for (try readVec(&parser.object.elements, reader, gpa)) |*elem| { - elem.table_index = try readLeb(u32, reader); - elem.offset = try readInit(reader); + //const expr, pos = if (flags != .passive) try readInit(wasm, bytes, pos) else .{ .none, pos }; + if (flags != .passive) pos = try skipInit(bytes, pos); + const data_len, pos = readLeb(u32, bytes, pos); + const segment_start = pos; + const payload = try wasm.addRelocatableDataPayload(bytes[pos..][0..data_len]); + pos += data_len; + elem.* = .{ + .payload = payload, + .name = .none, // Populated from segment_info + .flags = .{ + .is_passive = flags == .passive, + }, // Remainder populated from segment_info + .offset = @intCast(segment_start - section_start), + .object_index = object_index, + }; + } + }, + else => pos = section_end, + } + if (pos != section_end) return error.MalformedSection; + } + if (!saw_linking_section) return error.MissingLinkingSection; - for (try readVec(&elem.func_indexes, reader, gpa)) |*idx| { - idx.* = try readLeb(u32, reader); - } - } - try assertEnd(reader); + const target_features = comp.root_mod.resolved_target.result.cpu.features; + + if (has_tls) { + if (!std.Target.wasm.featureSetHas(target_features, .atomics)) + return diags.failParse(path, "object has TLS segment but target CPU feature atomics is disabled", .{}); + if (!std.Target.wasm.featureSetHas(target_features, .bulk_memory)) + return diags.failParse(path, "object has TLS segment but target CPU feature bulk_memory is disabled", .{}); + } + + const features = opt_features orelse return error.MissingFeatures; + for (features.slice(wasm)) |feat| { + log.debug("feature: {s}{s}", .{ @tagName(feat.prefix), @tagName(feat.tag) }); + switch (feat.prefix) { + .invalid => unreachable, + .@"-" => switch (feat.tag) { + .@"shared-mem" => if (comp.config.shared_memory) { + return diags.failParse(path, "object forbids shared-mem but compilation enables it", .{}); }, - .code => { - const start = reader.context.bytes_left; - var index: u32 = 0; - const count = try readLeb(u32, reader); - const imported_function_count = parser.object.imported_functions_count; - var relocatable_data = try std.ArrayList(RelocatableData).initCapacity(gpa, count); - defer relocatable_data.deinit(); - while (index < count) : (index += 1) { - const code_len = try readLeb(u32, reader); - const offset = @as(u32, @intCast(start - reader.context.bytes_left)); - const data = try gpa.alloc(u8, code_len); - errdefer gpa.free(data); - try reader.readNoEof(data); - relocatable_data.appendAssumeCapacity(.{ - .type = .code, - .data = data.ptr, - .size = code_len, - .index = imported_function_count + index, - .offset = offset, - .section_index = section_index, - }); + else => { + const f = feat.tag.toCpuFeature().?; + if (std.Target.wasm.featureSetHas(target_features, f)) { + return diags.failParse( + path, + "object forbids {s} but specified target features include {s}", + .{ @tagName(feat.tag), @tagName(f) }, + ); } - try parser.object.relocatable_data.put(gpa, .code, try relocatable_data.toOwnedSlice()); }, - .data => { - const start = reader.context.bytes_left; - var index: u32 = 0; - const count = try readLeb(u32, reader); - var relocatable_data = try std.ArrayList(RelocatableData).initCapacity(gpa, count); - defer relocatable_data.deinit(); - while (index < count) : (index += 1) { - const flags = try readLeb(u32, reader); - const data_offset = try readInit(reader); - _ = flags; // TODO: Do we need to check flags to detect passive/active memory? - _ = data_offset; - const data_len = try readLeb(u32, reader); - const offset = @as(u32, @intCast(start - reader.context.bytes_left)); - const data = try gpa.alloc(u8, data_len); - errdefer gpa.free(data); - try reader.readNoEof(data); - relocatable_data.appendAssumeCapacity(.{ - .type = .data, - .data = data.ptr, - .size = data_len, - .index = index, - .offset = offset, - .section_index = section_index, - }); + }, + .@"+", .@"=" => switch (feat.tag) { + .@"shared-mem" => if (!comp.config.shared_memory) { + return diags.failParse(path, "object requires shared-mem but compilation disables it", .{}); + }, + else => { + const f = feat.tag.toCpuFeature().?; + if (!std.Target.wasm.featureSetHas(target_features, f)) { + return diags.failParse( + path, + "object requires {s} but specified target features exclude {s}", + .{ @tagName(feat.tag), @tagName(f) }, + ); } - try parser.object.relocatable_data.put(gpa, .data, try relocatable_data.toOwnedSlice()); }, - else => try parser.reader.reader().skipBytes(len, .{}), - } - } else |err| switch (err) { - error.EndOfStream => {}, // finished parsing the file - else => |e| return e, - } - if (!saw_linking_section) return error.MissingLinkingSection; - } - - /// Based on the "features" custom section, parses it into a list of - /// features that tell the linker what features were enabled and may be mandatory - /// to be able to link. - /// Logs an info message when an undefined feature is detected. - fn parseFeatures(parser: *Parser, gpa: Allocator) !void { - const diags = &parser.wasm.base.comp.link_diags; - const reader = parser.reader.reader(); - for (try readVec(&parser.object.features, reader, gpa)) |*feature| { - const prefix = try readEnum(Wasm.Feature.Prefix, reader); - const name_len = try leb.readUleb128(u32, reader); - const name = try gpa.alloc(u8, name_len); - defer gpa.free(name); - try reader.readNoEof(name); - - const tag = Wasm.known_features.get(name) orelse { - return diags.failParse(parser.object.path, "object file contains unknown feature: {s}", .{name}); - }; - feature.* = .{ - .prefix = prefix, - .tag = tag, - }; - } - } - - /// Parses a "reloc" custom section into a list of relocations. - /// The relocations are mapped into `Object` where the key is the section - /// they apply to. - fn parseRelocations(parser: *Parser, gpa: Allocator) !void { - const reader = parser.reader.reader(); - const section = try leb.readUleb128(u32, reader); - const count = try leb.readUleb128(u32, reader); - const relocations = try gpa.alloc(Wasm.Relocation, count); - errdefer gpa.free(relocations); - - log.debug("Found {d} relocations for section ({d})", .{ - count, - section, - }); - - for (relocations) |*relocation| { - const rel_type = try reader.readByte(); - const rel_type_enum = std.meta.intToEnum(Wasm.Relocation.RelocationType, rel_type) catch return error.MalformedSection; - relocation.* = .{ - .relocation_type = rel_type_enum, - .offset = try leb.readUleb128(u32, reader), - .index = try leb.readUleb128(u32, reader), - .addend = if (rel_type_enum.addendIsPresent()) try leb.readIleb128(i32, reader) else 0, - }; - log.debug("Found relocation: type({s}) offset({d}) index({d}) addend({?d})", .{ - @tagName(relocation.relocation_type), - relocation.offset, - relocation.index, - relocation.addend, - }); + }, } - - try parser.object.relocations.putNoClobber(gpa, section, relocations); } - /// Parses the "linking" custom section. Versions that are not - /// supported will be an error. `payload_size` is required to be able - /// to calculate the subsections we need to parse, as that data is not - /// available within the section itparser. - fn parseMetadata(parser: *Parser, gpa: Allocator, payload_size: usize) !void { - var limited = std.io.limitedReader(parser.reader.reader(), payload_size); - const limited_reader = limited.reader(); - - const version = try leb.readUleb128(u32, limited_reader); - log.debug("Link meta data version: {d}", .{version}); - if (version != 2) return error.UnsupportedVersion; - - while (limited.bytes_left > 0) { - try parser.parseSubsection(gpa, limited_reader); - } + // Apply function type information. + for (ss.func_type_indexes.items, wasm.object_functions.items[functions_start..]) |func_type, *func| { + func.type_index = func_type.ptr(ss).*; } - /// Parses a `spec.Subsection`. - /// The `reader` param for this is to provide a `LimitedReader`, which allows - /// us to only read until a max length. - /// - /// `parser` is used to provide access to other sections that may be needed, - /// such as access to the `import` section to find the name of a symbol. - fn parseSubsection(parser: *Parser, gpa: Allocator, reader: anytype) !void { - const wasm = parser.wasm; - const sub_type = try leb.readUleb128(u8, reader); - log.debug("Found subsection: {s}", .{@tagName(@as(Wasm.SubsectionType, @enumFromInt(sub_type)))}); - const payload_len = try leb.readUleb128(u32, reader); - if (payload_len == 0) return; - - var limited = std.io.limitedReader(reader, payload_len); - const limited_reader = limited.reader(); - - // every subsection contains a 'count' field - const count = try leb.readUleb128(u32, limited_reader); - - switch (@as(Wasm.SubsectionType, @enumFromInt(sub_type))) { - .WASM_SEGMENT_INFO => { - const segments = try gpa.alloc(Wasm.NamedSegment, count); - errdefer gpa.free(segments); - for (segments) |*segment| { - const name_len = try leb.readUleb128(u32, reader); - const name = try gpa.alloc(u8, name_len); - errdefer gpa.free(name); - try reader.readNoEof(name); - segment.* = .{ - .name = name, - .alignment = @enumFromInt(try leb.readUleb128(u32, reader)), - .flags = try leb.readUleb128(u32, reader), - }; - log.debug("Found segment: {s} align({d}) flags({b})", .{ - segment.name, - segment.alignment, - segment.flags, + // Apply symbol table information. + for (ss.symbol_table.items) |symbol| switch (symbol.pointee) { + .function_import => |index| { + const ptr = index.ptr(ss); + const name = symbol.name.unwrap() orelse ptr.name; + if (symbol.flags.binding == .local) { + diags.addParseError(path, "local symbol '{s}' references import", .{name.slice(wasm)}); + continue; + } + const gop = try wasm.object_function_imports.getOrPut(gpa, name); + const fn_ty_index = ptr.function_index.ptr(ss).*; + if (gop.found_existing) { + if (gop.value_ptr.type != fn_ty_index) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching function signatures", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "imported as {} here", .{ + gop.value_ptr.type.fmt(wasm), }); - - // support legacy object files that specified being TLS by the name instead of the TLS flag. - if (!segment.isTLS() and (std.mem.startsWith(u8, segment.name, ".tdata") or std.mem.startsWith(u8, segment.name, ".tbss"))) { - // set the flag so we can simply check for the flag in the rest of the linker. - segment.flags |= @intFromEnum(Wasm.NamedSegment.Flags.WASM_SEG_FLAG_TLS); + source_location.addNote(&err, "imported as {} here", .{fn_ty_index.fmt(wasm)}); + continue; + } + if (gop.value_ptr.module_name != ptr.module_name.toOptional()) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching module names", .{name.slice(wasm)}); + if (gop.value_ptr.module_name.slice(wasm)) |module_name| { + gop.value_ptr.source_location.addNote(&err, "module '{s}' here", .{module_name}); + } else { + gop.value_ptr.source_location.addNote(&err, "no module here", .{}); } + source_location.addNote(&err, "module '{s}' here", .{ptr.module_name.slice(wasm)}); + continue; } - parser.object.segment_info = segments; - }, - .WASM_INIT_FUNCS => { - const funcs = try gpa.alloc(Wasm.InitFunc, count); - errdefer gpa.free(funcs); - for (funcs) |*func| { - func.* = .{ - .priority = try leb.readUleb128(u32, reader), - .symbol_index = try leb.readUleb128(u32, reader), - }; - log.debug("Found function - prio: {d}, index: {d}", .{ func.priority, func.symbol_index }); + if (gop.value_ptr.name != ptr.name) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching import names", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "imported as '{s}' here", .{gop.value_ptr.name.slice(wasm)}); + source_location.addNote(&err, "imported as '{s}' here", .{ptr.name.slice(wasm)}); + continue; } - parser.object.init_funcs = funcs; - }, - .WASM_COMDAT_INFO => { - const comdats = try gpa.alloc(Wasm.Comdat, count); - errdefer gpa.free(comdats); - for (comdats) |*comdat| { - const name_len = try leb.readUleb128(u32, reader); - const name = try gpa.alloc(u8, name_len); - errdefer gpa.free(name); - try reader.readNoEof(name); - - const flags = try leb.readUleb128(u32, reader); - if (flags != 0) { - return error.UnexpectedValue; - } - - const symbol_count = try leb.readUleb128(u32, reader); - const symbols = try gpa.alloc(Wasm.ComdatSym, symbol_count); - errdefer gpa.free(symbols); - for (symbols) |*symbol| { - symbol.* = .{ - .kind = @as(Wasm.ComdatSym.Type, @enumFromInt(try leb.readUleb128(u8, reader))), - .index = try leb.readUleb128(u32, reader), - }; + } else { + gop.value_ptr.* = .{ + .flags = symbol.flags, + .module_name = ptr.module_name.toOptional(), + .name = ptr.name, + .source_location = source_location, + .resolution = .unresolved, + .type = fn_ty_index, + }; + } + }, + .global_import => |index| { + const ptr = index.ptr(ss); + const name = symbol.name.unwrap() orelse ptr.name; + if (symbol.flags.binding == .local) { + diags.addParseError(path, "local symbol '{s}' references import", .{name.slice(wasm)}); + continue; + } + const gop = try wasm.object_global_imports.getOrPut(gpa, name); + if (gop.found_existing) { + const existing_ty = gop.value_ptr.type(); + if (ptr.valtype != existing_ty.valtype) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching global types", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "type {s} here", .{@tagName(existing_ty.valtype)}); + source_location.addNote(&err, "type {s} here", .{@tagName(ptr.valtype)}); + continue; + } + if (ptr.mutable != existing_ty.mutable) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching global mutability", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "{s} here", .{ + if (existing_ty.mutable) "mutable" else "not mutable", + }); + source_location.addNote(&err, "{s} here", .{ + if (ptr.mutable) "mutable" else "not mutable", + }); + continue; + } + if (gop.value_ptr.module_name != ptr.module_name.toOptional()) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching module names", .{name.slice(wasm)}); + if (gop.value_ptr.module_name.slice(wasm)) |module_name| { + gop.value_ptr.source_location.addNote(&err, "module '{s}' here", .{module_name}); + } else { + gop.value_ptr.source_location.addNote(&err, "no module here", .{}); } - - comdat.* = .{ - .name = name, - .flags = flags, - .symbols = symbols, - }; + source_location.addNote(&err, "module '{s}' here", .{ptr.module_name.slice(wasm)}); + continue; } - - parser.object.comdat_info = comdats; - }, - .WASM_SYMBOL_TABLE => { - var symbols = try std.ArrayList(Symbol).initCapacity(gpa, count); - - var i: usize = 0; - while (i < count) : (i += 1) { - const symbol = symbols.addOneAssumeCapacity(); - symbol.* = try parser.parseSymbol(gpa, reader); - log.debug("Found symbol: type({s}) name({s}) flags(0b{b:0>8})", .{ - @tagName(symbol.tag), - wasm.stringSlice(symbol.name), - symbol.flags, + if (gop.value_ptr.name != ptr.name) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching import names", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "imported as '{s}' here", .{gop.value_ptr.name.slice(wasm)}); + source_location.addNote(&err, "imported as '{s}' here", .{ptr.name.slice(wasm)}); + continue; + } + } else { + gop.value_ptr.* = .{ + .flags = symbol.flags, + .module_name = ptr.module_name.toOptional(), + .name = ptr.name, + .source_location = source_location, + .resolution = .unresolved, + }; + gop.value_ptr.flags.global_type = .{ + .valtype = .from(ptr.valtype), + .mutable = ptr.mutable, + }; + } + }, + .table_import => |index| { + const ptr = index.ptr(ss); + const name = symbol.name.unwrap() orelse ptr.name; + if (symbol.flags.binding == .local) { + diags.addParseError(path, "local symbol '{s}' references import", .{name.slice(wasm)}); + continue; + } + const gop = try wasm.object_table_imports.getOrPut(gpa, name); + if (gop.found_existing) { + const existing_reftype = gop.value_ptr.flags.ref_type.to(); + if (ptr.ref_type != existing_reftype) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching table reftypes", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "{s} here", .{@tagName(existing_reftype)}); + source_location.addNote(&err, "{s} here", .{@tagName(ptr.ref_type)}); + continue; + } + if (gop.value_ptr.module_name != ptr.module_name) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching module names", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "module '{s}' here", .{ + gop.value_ptr.module_name.slice(wasm), }); + source_location.addNote(&err, "module '{s}' here", .{ptr.module_name.slice(wasm)}); + continue; } - - // we found all symbols, check for indirect function table - // in case of an MVP object file - if (try parser.object.checkLegacyIndirectFunctionTable(parser.wasm)) |symbol| { - try symbols.append(symbol); - log.debug("Found legacy indirect function table. Created symbol", .{}); + if (gop.value_ptr.name != ptr.name) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching import names", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "imported as '{s}' here", .{gop.value_ptr.name.slice(wasm)}); + source_location.addNote(&err, "imported as '{s}' here", .{ptr.name.slice(wasm)}); + continue; } - - // Not all debug sections may be represented by a symbol, for those sections - // we manually create a symbol. - if (parser.object.relocatable_data.get(.custom)) |custom_sections| { - for (custom_sections) |*data| { - if (!data.represented) { - const name = wasm.castToString(data.index); - try symbols.append(.{ - .name = name, - .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL), - .tag = .section, - .virtual_address = 0, - .index = data.section_index, - }); - data.represented = true; - log.debug("Created synthetic custom section symbol for '{s}'", .{ - wasm.stringSlice(name), - }); - } - } + if (symbol.flags.binding == .strong) gop.value_ptr.flags.binding = .strong; + if (!symbol.flags.visibility_hidden) gop.value_ptr.flags.visibility_hidden = false; + if (symbol.flags.no_strip) gop.value_ptr.flags.no_strip = true; + } else { + gop.value_ptr.* = .{ + .flags = symbol.flags, + .module_name = ptr.module_name, + .name = ptr.name, + .source_location = source_location, + .resolution = .unresolved, + .limits_min = ptr.limits_min, + .limits_max = ptr.limits_max, + }; + gop.value_ptr.flags.limits_has_max = ptr.limits_has_max; + gop.value_ptr.flags.limits_is_shared = ptr.limits_is_shared; + gop.value_ptr.flags.ref_type = .from(ptr.ref_type); + } + }, + .data_import => { + const name = symbol.name.unwrap().?; + if (symbol.flags.binding == .local) { + diags.addParseError(path, "local symbol '{s}' references import", .{name.slice(wasm)}); + continue; + } + const gop = try wasm.object_data_imports.getOrPut(gpa, name); + if (!gop.found_existing) gop.value_ptr.* = .{ + .flags = symbol.flags, + .source_location = source_location, + .resolution = .unresolved, + }; + }, + .function => |index| { + assert(!symbol.flags.undefined); + const ptr = index.ptr(wasm); + ptr.name = symbol.name; + ptr.flags = symbol.flags; + if (symbol.flags.binding == .local) continue; // No participation in symbol resolution. + const name = symbol.name.unwrap().?; + const gop = try wasm.object_function_imports.getOrPut(gpa, name); + if (gop.found_existing) { + if (gop.value_ptr.type != ptr.type_index) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("function signature mismatch: {s}", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "exported as {} here", .{ + ptr.type_index.fmt(wasm), + }); + const word = if (gop.value_ptr.resolution == .unresolved) "imported" else "exported"; + source_location.addNote(&err, "{s} as {} here", .{ word, gop.value_ptr.type.fmt(wasm) }); + continue; } + if (gop.value_ptr.resolution == .unresolved or gop.value_ptr.flags.binding == .weak) { + // Intentional: if they're both weak, take the last one. + gop.value_ptr.source_location = source_location; + gop.value_ptr.module_name = host_name; + gop.value_ptr.resolution = .fromObjectFunction(wasm, index); + gop.value_ptr.flags = symbol.flags; + continue; + } + if (ptr.flags.binding == .weak) { + // Keep the existing one. + continue; + } + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol collision: {s}", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "exported as {} here", .{ptr.type_index.fmt(wasm)}); + source_location.addNote(&err, "exported as {} here", .{gop.value_ptr.type.fmt(wasm)}); + continue; + } else { + gop.value_ptr.* = .{ + .flags = symbol.flags, + .module_name = host_name, + .name = name, + .source_location = source_location, + .resolution = .fromObjectFunction(wasm, index), + .type = ptr.type_index, + }; + } + }, + .global => |index| { + assert(!symbol.flags.undefined); + const ptr = index.ptr(wasm); + ptr.name = symbol.name; + ptr.flags = symbol.flags; + if (symbol.flags.binding == .local) continue; // No participation in symbol resolution. + const name = symbol.name.unwrap().?; + const new_ty = ptr.type(); + const gop = try wasm.object_global_imports.getOrPut(gpa, name); + if (gop.found_existing) { + const existing_ty = gop.value_ptr.type(); + if (new_ty.valtype != existing_ty.valtype) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching global types", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "type {s} here", .{@tagName(existing_ty.valtype)}); + source_location.addNote(&err, "type {s} here", .{@tagName(new_ty.valtype)}); + continue; + } + if (new_ty.mutable != existing_ty.mutable) { + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol '{s}' mismatching global mutability", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "{s} here", .{ + if (existing_ty.mutable) "mutable" else "not mutable", + }); + source_location.addNote(&err, "{s} here", .{ + if (new_ty.mutable) "mutable" else "not mutable", + }); + continue; + } + if (gop.value_ptr.resolution == .unresolved or gop.value_ptr.flags.binding == .weak) { + // Intentional: if they're both weak, take the last one. + gop.value_ptr.source_location = source_location; + gop.value_ptr.module_name = host_name; + gop.value_ptr.resolution = .fromObjectGlobal(wasm, index); + gop.value_ptr.flags = symbol.flags; + continue; + } + if (ptr.flags.binding == .weak) { + // Keep the existing one. + continue; + } + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol collision: {s}", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "exported as {s} here", .{@tagName(existing_ty.valtype)}); + source_location.addNote(&err, "exported as {s} here", .{@tagName(new_ty.valtype)}); + continue; + } else { + gop.value_ptr.* = .{ + .flags = symbol.flags, + .module_name = .none, + .name = name, + .source_location = source_location, + .resolution = .fromObjectGlobal(wasm, index), + }; + gop.value_ptr.flags.global_type = .{ + .valtype = .from(new_ty.valtype), + .mutable = new_ty.mutable, + }; + } + }, + .table => |i| { + assert(!symbol.flags.undefined); + const ptr = i.ptr(wasm); + ptr.name = symbol.name; + ptr.flags = symbol.flags; + }, + .data => |index| { + assert(!symbol.flags.undefined); + const ptr = index.ptr(wasm); + const name = ptr.name; + assert(name.toOptional() == symbol.name); + ptr.flags = symbol.flags; + if (symbol.flags.binding == .local) continue; // No participation in symbol resolution. + const gop = try wasm.object_data_imports.getOrPut(gpa, name); + if (gop.found_existing) { + if (gop.value_ptr.resolution == .unresolved or gop.value_ptr.flags.binding == .weak) { + // Intentional: if they're both weak, take the last one. + gop.value_ptr.source_location = source_location; + gop.value_ptr.resolution = .fromObjectDataIndex(wasm, index); + gop.value_ptr.flags = symbol.flags; + continue; + } + if (ptr.flags.binding == .weak) { + // Keep the existing one. + continue; + } + var err = try diags.addErrorWithNotes(2); + try err.addMsg("symbol collision: {s}", .{name.slice(wasm)}); + gop.value_ptr.source_location.addNote(&err, "exported here", .{}); + source_location.addNote(&err, "exported here", .{}); + continue; + } else { + gop.value_ptr.* = .{ + .flags = symbol.flags, + .source_location = source_location, + .resolution = .fromObjectDataIndex(wasm, index), + }; + } + }, + .section => |i| { + // Name is provided by the section directly; symbol table does not have it. + //const ptr = i.ptr(wasm); + //ptr.flags = symbol.flags; + _ = i; + if (symbol.flags.undefined and symbol.flags.binding == .local) { + const name = symbol.name.slice(wasm).?; + diags.addParseError(path, "local symbol '{s}' references import", .{name}); + } + }, + }; - parser.object.symtable = try symbols.toOwnedSlice(); + // Apply export section info. This is done after the symbol table above so + // that the symbol table can take precedence, overriding the export name. + for (ss.exports.items) |*exp| { + switch (exp.pointee) { + inline .function, .table, .memory, .global => |index| { + const ptr = index.ptr(wasm); + ptr.name = exp.name.toOptional(); + ptr.flags.exported = true; }, } } - /// Parses the symbol information based on its kind, - /// requires access to `Object` to find the name of a symbol when it's - /// an import and flag `WASM_SYM_EXPLICIT_NAME` is not set. - fn parseSymbol(parser: *Parser, gpa: Allocator, reader: anytype) !Symbol { - const wasm = parser.wasm; - const tag: Symbol.Tag = @enumFromInt(try leb.readUleb128(u8, reader)); - const flags = try leb.readUleb128(u32, reader); - var symbol: Symbol = .{ - .flags = flags, - .tag = tag, - .name = undefined, - .index = undefined, - .virtual_address = undefined, + // Apply segment_info. + const data_segments = wasm.object_data_segments.items[data_segment_start..]; + if (data_segments.len != ss.segment_info.items.len) { + return diags.failParse(path, "expected {d} segment_info entries; found {d}", .{ + data_segments.len, ss.segment_info.items.len, + }); + } + for (data_segments, ss.segment_info.items) |*data, info| { + data.name = info.name.toOptional(); + data.flags = .{ + .is_passive = data.flags.is_passive, + .strings = info.flags.strings, + .tls = info.flags.tls, + .retain = info.flags.retain, + .alignment = info.flags.alignment, }; + } - switch (tag) { - .data => { - const name_len = try leb.readUleb128(u32, reader); - const name = try gpa.alloc(u8, name_len); - defer gpa.free(name); - try reader.readNoEof(name); - symbol.name = try wasm.internString(name); - - // Data symbols only have the following fields if the symbol is defined - if (symbol.isDefined()) { - symbol.index = try leb.readUleb128(u32, reader); - // @TODO: We should verify those values - _ = try leb.readUleb128(u32, reader); - _ = try leb.readUleb128(u32, reader); - } - }, - .section => { - symbol.index = try leb.readUleb128(u32, reader); - const section_data = parser.object.relocatable_data.get(.custom).?; - for (section_data) |*data| { - if (data.section_index == symbol.index) { - symbol.name = wasm.castToString(data.index); - data.represented = true; - break; - } - } - }, - else => { - symbol.index = try leb.readUleb128(u32, reader); - const is_undefined = symbol.isUndefined(); - const explicit_name = symbol.hasFlag(.WASM_SYM_EXPLICIT_NAME); - symbol.name = if (!is_undefined or (is_undefined and explicit_name)) name: { - const name_len = try leb.readUleb128(u32, reader); - const name = try gpa.alloc(u8, name_len); - defer gpa.free(name); - try reader.readNoEof(name); - break :name try wasm.internString(name); - } else parser.object.findImport(symbol).name; - }, + // Check for indirect function table in case of an MVP object file. + legacy_indirect_function_table: { + // If there is a symbol for each import table, this is not a legacy object file. + if (ss.table_imports.items.len == table_import_symbol_count) break :legacy_indirect_function_table; + if (table_import_symbol_count != 0) { + return diags.failParse(path, "expected a table entry symbol for each of the {d} table(s), but instead got {d} symbols.", .{ + ss.table_imports.items.len, table_import_symbol_count, + }); } - return symbol; + // MVP object files cannot have any table definitions, only imports + // (for the indirect function table). + const tables = wasm.object_tables.items[tables_start..]; + if (tables.len > 0) { + return diags.failParse(path, "table definition without representing table symbols", .{}); + } + if (ss.table_imports.items.len != 1) { + return diags.failParse(path, "found more than one table import, but no representing table symbols", .{}); + } + const table_import_name = ss.table_imports.items[0].name; + if (table_import_name != wasm.preloaded_strings.__indirect_function_table) { + return diags.failParse(path, "non-indirect function table import '{s}' is missing a corresponding symbol", .{ + table_import_name.slice(wasm), + }); + } + const ptr = wasm.object_table_imports.getPtr(table_import_name).?; + ptr.flags = .{ + .undefined = true, + .no_strip = true, + }; + } + + for (wasm.object_init_funcs.items[init_funcs_start..]) |init_func| { + const func = init_func.function_index.ptr(wasm); + const params = func.type_index.ptr(wasm).params.slice(wasm); + if (params.len != 0) diags.addError("constructor function '{s}' has non-empty parameter list", .{ + func.name.slice(wasm).?, + }); } -}; -/// First reads the count from the reader and then allocate -/// a slice of ptr child's element type. -fn readVec(ptr: anytype, reader: anytype, gpa: Allocator) ![]ElementType(@TypeOf(ptr)) { - const len = try readLeb(u32, reader); - const slice = try gpa.alloc(ElementType(@TypeOf(ptr)), len); - ptr.* = slice; - return slice; + const functions_len: u32 = @intCast(wasm.object_functions.items.len - functions_start); + if (functions_len > 0 and code_section_index == null) + return diags.failParse(path, "code section missing ({d} functions)", .{functions_len}); + + return .{ + .version = version, + .path = path, + .archive_member_name = try wasm.internOptionalString(archive_member_name), + .start_function = start_function, + .features = features, + .functions = .{ + .off = functions_start, + .len = functions_len, + }, + .function_imports = .{ + .off = function_imports_start, + .len = @intCast(wasm.object_function_imports.entries.len - function_imports_start), + }, + .global_imports = .{ + .off = global_imports_start, + .len = @intCast(wasm.object_global_imports.entries.len - global_imports_start), + }, + .table_imports = .{ + .off = table_imports_start, + .len = @intCast(wasm.object_table_imports.entries.len - table_imports_start), + }, + .data_imports = .{ + .off = data_imports_start, + .len = @intCast(wasm.object_data_imports.entries.len - data_imports_start), + }, + .init_funcs = .{ + .off = init_funcs_start, + .len = @intCast(wasm.object_init_funcs.items.len - init_funcs_start), + }, + .comdats = .{ + .off = comdats_start, + .len = @intCast(wasm.object_comdats.items.len - comdats_start), + }, + .custom_segments = .{ + .off = custom_segment_start, + .len = @intCast(wasm.object_custom_segments.entries.len - custom_segment_start), + }, + .code_section_index = code_section_index, + .global_section_index = global_section_index, + .data_section_index = data_section_index, + .is_included = must_link, + }; } -fn ElementType(comptime ptr: type) type { - return meta.Elem(meta.Child(ptr)); +/// Based on the "features" custom section, parses it into a list of +/// features that tell the linker what features were enabled and may be mandatory +/// to be able to link. +fn parseFeatures( + wasm: *Wasm, + bytes: []const u8, + start_pos: usize, + path: Path, +) error{ OutOfMemory, LinkFailure }!struct { Wasm.Feature.Set, usize } { + const gpa = wasm.base.comp.gpa; + const diags = &wasm.base.comp.link_diags; + const features_len, var pos = readLeb(u32, bytes, start_pos); + // This temporary allocation could be avoided by using the string_bytes buffer as a scratch space. + const feature_buffer = try gpa.alloc(Wasm.Feature, features_len); + defer gpa.free(feature_buffer); + for (feature_buffer) |*feature| { + const prefix: Wasm.Feature.Prefix = switch (bytes[pos]) { + '-' => .@"-", + '+' => .@"+", + '=' => .@"=", + else => |b| return diags.failParse(path, "invalid feature prefix: 0x{x}", .{b}), + }; + pos += 1; + const name, pos = readBytes(bytes, pos); + const tag = std.meta.stringToEnum(Wasm.Feature.Tag, name) orelse { + return diags.failParse(path, "unrecognized wasm feature in object: {s}", .{name}); + }; + feature.* = .{ + .prefix = prefix, + .tag = tag, + }; + } + std.mem.sortUnstable(Wasm.Feature, feature_buffer, {}, Wasm.Feature.lessThan); + + return .{ + .fromString(try wasm.internString(@ptrCast(feature_buffer))), + pos, + }; } -/// Uses either `readIleb128` or `readUleb128` depending on the -/// signedness of the given type `T`. -/// Asserts `T` is an integer. -fn readLeb(comptime T: type, reader: anytype) !T { - return switch (@typeInfo(T).int.signedness) { - .signed => try leb.readIleb128(T, reader), - .unsigned => try leb.readUleb128(T, reader), +fn readLeb(comptime T: type, bytes: []const u8, pos: usize) struct { T, usize } { + var fbr = std.io.fixedBufferStream(bytes[pos..]); + return .{ + switch (@typeInfo(T).int.signedness) { + .signed => std.leb.readIleb128(T, fbr.reader()) catch unreachable, + .unsigned => std.leb.readUleb128(T, fbr.reader()) catch unreachable, + }, + pos + fbr.pos, }; } -/// Reads an enum type from the given reader. -/// Asserts `T` is an enum -fn readEnum(comptime T: type, reader: anytype) !T { - switch (@typeInfo(T)) { - .@"enum" => |enum_type| return @as(T, @enumFromInt(try readLeb(enum_type.tag_type, reader))), - else => @compileError("T must be an enum. Instead was given type " ++ @typeName(T)), - } +fn readBytes(bytes: []const u8, start_pos: usize) struct { []const u8, usize } { + const len, const pos = readLeb(u32, bytes, start_pos); + return .{ + bytes[pos..][0..len], + pos + len, + }; } -fn readLimits(reader: anytype) !std.wasm.Limits { - const flags = try reader.readByte(); - const min = try readLeb(u32, reader); - var limits: std.wasm.Limits = .{ +fn readEnum(comptime T: type, bytes: []const u8, pos: usize) struct { T, usize } { + const Tag = @typeInfo(T).@"enum".tag_type; + const int, const new_pos = readLeb(Tag, bytes, pos); + return .{ @enumFromInt(int), new_pos }; +} + +fn readLimits(bytes: []const u8, start_pos: usize) struct { std.wasm.Limits, usize } { + const flags: std.wasm.Limits.Flags = @bitCast(bytes[start_pos]); + const min, const max_pos = readLeb(u32, bytes, start_pos + 1); + const max, const end_pos = if (flags.has_max) readLeb(u32, bytes, max_pos) else .{ 0, max_pos }; + return .{ .{ .flags = flags, .min = min, - .max = undefined, - }; - if (limits.hasFlag(.WASM_LIMITS_FLAG_HAS_MAX)) { - limits.max = try readLeb(u32, reader); - } - return limits; + .max = max, + }, end_pos }; } -fn readInit(reader: anytype) !std.wasm.InitExpression { - const opcode = try reader.readByte(); - const init_expr: std.wasm.InitExpression = switch (@as(std.wasm.Opcode, @enumFromInt(opcode))) { - .i32_const => .{ .i32_const = try readLeb(i32, reader) }, - .global_get => .{ .global_get = try readLeb(u32, reader) }, - else => @panic("TODO: initexpression for other opcodes"), - }; +fn readInit(wasm: *Wasm, bytes: []const u8, pos: usize) !struct { Wasm.Expr, usize } { + const end_pos = try skipInit(bytes, pos); // one after the end opcode + return .{ try wasm.addExpr(bytes[pos..end_pos]), end_pos }; +} - if ((try readEnum(std.wasm.Opcode, reader)) != .end) return error.MissingEndForExpression; - return init_expr; +pub fn exprEndPos(bytes: []const u8, pos: usize) error{InvalidInitOpcode}!usize { + const opcode = bytes[pos]; + return switch (@as(std.wasm.Opcode, @enumFromInt(opcode))) { + .i32_const => readLeb(i32, bytes, pos + 1)[1], + .i64_const => readLeb(i64, bytes, pos + 1)[1], + .f32_const => pos + 5, + .f64_const => pos + 9, + .global_get => readLeb(u32, bytes, pos + 1)[1], + else => return error.InvalidInitOpcode, + }; } -fn assertEnd(reader: anytype) !void { - var buf: [1]u8 = undefined; - const len = try reader.read(&buf); - if (len != 0) return error.MalformedSection; - if (reader.context.bytes_left != 0) return error.MalformedSection; +fn skipInit(bytes: []const u8, pos: usize) !usize { + const end_pos = try exprEndPos(bytes, pos); + const op, const final_pos = readEnum(std.wasm.Opcode, bytes, end_pos); + if (op != .end) return error.InitExprMissingEnd; + return final_pos; } diff --git a/src/link/Wasm/Symbol.zig b/src/link/Wasm/Symbol.zig deleted file mode 100644 index b60b73c46f..0000000000 --- a/src/link/Wasm/Symbol.zig +++ /dev/null @@ -1,210 +0,0 @@ -//! Represents a WebAssembly symbol. Containing all of its properties, -//! as well as providing helper methods to determine its functionality -//! and how it will/must be linked. -//! The name of the symbol can be found by providing the offset, found -//! on the `name` field, to a string table in the wasm binary or object file. - -/// Bitfield containings flags for a symbol -/// Can contain any of the flags defined in `Flag` -flags: u32, -/// Symbol name, when the symbol is undefined the name will be taken from the import. -/// Note: This is an index into the wasm string table. -name: wasm.String, -/// Index into the list of objects based on set `tag` -/// NOTE: This will be set to `undefined` when `tag` is `data` -/// and the symbol is undefined. -index: u32, -/// Represents the kind of the symbol, such as a function or global. -tag: Tag, -/// Contains the virtual address of the symbol, relative to the start of its section. -/// This differs from the offset of an `Atom` which is relative to the start of a segment. -virtual_address: u32, - -/// Represents a symbol index where `null` represents an invalid index. -pub const Index = enum(u32) { - null, - _, -}; - -pub const Tag = enum { - function, - data, - global, - section, - event, - table, - /// synthetic kind used by the wasm linker during incremental compilation - /// to notate a symbol has been freed, but still lives in the symbol list. - dead, - undefined, - - /// From a given symbol tag, returns the `ExternalType` - /// Asserts the given tag can be represented as an external type. - pub fn externalType(tag: Tag) std.wasm.ExternalKind { - return switch (tag) { - .function => .function, - .global => .global, - .data => unreachable, // Data symbols will generate a global - .section => unreachable, // Not an external type - .event => unreachable, // Not an external type - .dead => unreachable, // Dead symbols should not be referenced - .undefined => unreachable, - .table => .table, - }; - } -}; - -pub const Flag = enum(u32) { - /// Indicates a weak symbol. - /// When linking multiple modules defining the same symbol, all weak definitions are discarded - /// in favourite of the strong definition. When no strong definition exists, all weak but one definition is discarded. - /// If multiple definitions remain, we get an error: symbol collision. - WASM_SYM_BINDING_WEAK = 0x1, - /// Indicates a local, non-exported, non-module-linked symbol. - /// The names of local symbols are not required to be unique, unlike non-local symbols. - WASM_SYM_BINDING_LOCAL = 0x2, - /// Represents the binding of a symbol, indicating if it's local or not, and weak or not. - WASM_SYM_BINDING_MASK = 0x3, - /// Indicates a hidden symbol. Hidden symbols will not be exported to the link result, but may - /// link to other modules. - WASM_SYM_VISIBILITY_HIDDEN = 0x4, - /// Indicates an undefined symbol. For non-data symbols, this must match whether the symbol is - /// an import or is defined. For data symbols however, determines whether a segment is specified. - WASM_SYM_UNDEFINED = 0x10, - /// Indicates a symbol of which its intention is to be exported from the wasm module to the host environment. - /// This differs from the visibility flag as this flag affects the static linker. - WASM_SYM_EXPORTED = 0x20, - /// Indicates the symbol uses an explicit symbol name, rather than reusing the name from a wasm import. - /// Allows remapping imports from foreign WASM modules into local symbols with a different name. - WASM_SYM_EXPLICIT_NAME = 0x40, - /// Indicates the symbol is to be included in the linker output, regardless of whether it is used or has any references to it. - WASM_SYM_NO_STRIP = 0x80, - /// Indicates a symbol is TLS - WASM_SYM_TLS = 0x100, - /// Zig specific flag. Uses the most significant bit of the flag to annotate whether a symbol is - /// alive or not. Dead symbols are allowed to be garbage collected. - alive = 0x80000000, -}; - -/// Verifies if the given symbol should be imported from the -/// host environment or not -pub fn requiresImport(symbol: Symbol) bool { - if (symbol.tag == .data) return false; - if (!symbol.isUndefined()) return false; - if (symbol.isWeak()) return false; - // if (symbol.isDefined() and symbol.isWeak()) return true; //TODO: Only when building shared lib - - return true; -} - -/// Marks a symbol as 'alive', ensuring the garbage collector will not collect the trash. -pub fn mark(symbol: *Symbol) void { - symbol.flags |= @intFromEnum(Flag.alive); -} - -pub fn unmark(symbol: *Symbol) void { - symbol.flags &= ~@intFromEnum(Flag.alive); -} - -pub fn isAlive(symbol: Symbol) bool { - return symbol.flags & @intFromEnum(Flag.alive) != 0; -} - -pub fn isDead(symbol: Symbol) bool { - return symbol.flags & @intFromEnum(Flag.alive) == 0; -} - -pub fn isTLS(symbol: Symbol) bool { - return symbol.flags & @intFromEnum(Flag.WASM_SYM_TLS) != 0; -} - -pub fn hasFlag(symbol: Symbol, flag: Flag) bool { - return symbol.flags & @intFromEnum(flag) != 0; -} - -pub fn setFlag(symbol: *Symbol, flag: Flag) void { - symbol.flags |= @intFromEnum(flag); -} - -pub fn isUndefined(symbol: Symbol) bool { - return symbol.flags & @intFromEnum(Flag.WASM_SYM_UNDEFINED) != 0; -} - -pub fn setUndefined(symbol: *Symbol, is_undefined: bool) void { - if (is_undefined) { - symbol.setFlag(.WASM_SYM_UNDEFINED); - } else { - symbol.flags &= ~@intFromEnum(Flag.WASM_SYM_UNDEFINED); - } -} - -pub fn setGlobal(symbol: *Symbol, is_global: bool) void { - if (is_global) { - symbol.flags &= ~@intFromEnum(Flag.WASM_SYM_BINDING_LOCAL); - } else { - symbol.setFlag(.WASM_SYM_BINDING_LOCAL); - } -} - -pub fn isDefined(symbol: Symbol) bool { - return !symbol.isUndefined(); -} - -pub fn isVisible(symbol: Symbol) bool { - return symbol.flags & @intFromEnum(Flag.WASM_SYM_VISIBILITY_HIDDEN) == 0; -} - -pub fn isLocal(symbol: Symbol) bool { - return symbol.flags & @intFromEnum(Flag.WASM_SYM_BINDING_LOCAL) != 0; -} - -pub fn isGlobal(symbol: Symbol) bool { - return symbol.flags & @intFromEnum(Flag.WASM_SYM_BINDING_LOCAL) == 0; -} - -pub fn isHidden(symbol: Symbol) bool { - return symbol.flags & @intFromEnum(Flag.WASM_SYM_VISIBILITY_HIDDEN) != 0; -} - -pub fn isNoStrip(symbol: Symbol) bool { - return symbol.flags & @intFromEnum(Flag.WASM_SYM_NO_STRIP) != 0; -} - -pub fn isExported(symbol: Symbol, is_dynamic: bool) bool { - if (symbol.isUndefined() or symbol.isLocal()) return false; - if (is_dynamic and symbol.isVisible()) return true; - return symbol.hasFlag(.WASM_SYM_EXPORTED); -} - -pub fn isWeak(symbol: Symbol) bool { - return symbol.flags & @intFromEnum(Flag.WASM_SYM_BINDING_WEAK) != 0; -} - -/// Formats the symbol into human-readable text -pub fn format(symbol: Symbol, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { - _ = fmt; - _ = options; - - const kind_fmt: u8 = switch (symbol.tag) { - .function => 'F', - .data => 'D', - .global => 'G', - .section => 'S', - .event => 'E', - .table => 'T', - .dead => '-', - .undefined => unreachable, - }; - const visible: []const u8 = if (symbol.isVisible()) "yes" else "no"; - const binding: []const u8 = if (symbol.isLocal()) "local" else "global"; - const undef: []const u8 = if (symbol.isUndefined()) "undefined" else ""; - - try writer.print( - "{c} binding={s} visible={s} id={d} name_offset={d} {s}", - .{ kind_fmt, binding, visible, symbol.index, symbol.name, undef }, - ); -} - -const std = @import("std"); -const Symbol = @This(); -const wasm = @import("../Wasm.zig"); diff --git a/src/link/Wasm/ZigObject.zig b/src/link/Wasm/ZigObject.zig deleted file mode 100644 index f2bce777ed..0000000000 --- a/src/link/Wasm/ZigObject.zig +++ /dev/null @@ -1,1229 +0,0 @@ -//! ZigObject encapsulates the state of the incrementally compiled Zig module. -//! It stores the associated input local and global symbols, allocated atoms, -//! and any relocations that may have been emitted. - -/// For error reporting purposes only. -path: Path, -/// Map of all `Nav` that are currently alive. -/// Each index maps to the corresponding `NavInfo`. -navs: std.AutoHashMapUnmanaged(InternPool.Nav.Index, NavInfo) = .empty, -/// List of function type signatures for this Zig module. -func_types: std.ArrayListUnmanaged(std.wasm.Type) = .empty, -/// List of `std.wasm.Func`. Each entry contains the function signature, -/// rather than the actual body. -functions: std.ArrayListUnmanaged(std.wasm.Func) = .empty, -/// List of indexes pointing to an entry within the `functions` list which has been removed. -functions_free_list: std.ArrayListUnmanaged(u32) = .empty, -/// Map of symbol locations, represented by its `Wasm.Import`. -imports: std.AutoHashMapUnmanaged(Symbol.Index, Wasm.Import) = .empty, -/// List of WebAssembly globals. -globals: std.ArrayListUnmanaged(std.wasm.Global) = .empty, -/// Mapping between an `Atom` and its type index representing the Wasm -/// type of the function signature. -atom_types: std.AutoHashMapUnmanaged(Atom.Index, u32) = .empty, -/// List of all symbols generated by Zig code. -symbols: std.ArrayListUnmanaged(Symbol) = .empty, -/// Map from symbol name to their index into the `symbols` list. -global_syms: std.AutoHashMapUnmanaged(Wasm.String, Symbol.Index) = .empty, -/// List of symbol indexes which are free to be used. -symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .empty, -/// Extra metadata about the linking section, such as alignment of segments and their name. -segment_info: std.ArrayListUnmanaged(Wasm.NamedSegment) = .empty, -/// List of indexes which contain a free slot in the `segment_info` list. -segment_free_list: std.ArrayListUnmanaged(u32) = .empty, -/// Map for storing anonymous declarations. Each anonymous decl maps to its Atom's index. -uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Atom.Index) = .empty, -/// List of atom indexes of functions that are generated by the backend. -synthetic_functions: std.ArrayListUnmanaged(Atom.Index) = .empty, -/// Represents the symbol index of the error name table -/// When this is `null`, no code references an error using runtime `@errorName`. -/// During initializion, a symbol with corresponding atom will be created that is -/// used to perform relocations to the pointer of this table. -/// The actual table is populated during `flush`. -error_table_symbol: Symbol.Index = .null, -/// Atom index of the table of symbol names. This is stored so we can clean up the atom. -error_names_atom: Atom.Index = .null, -/// Amount of functions in the `import` sections. -imported_functions_count: u32 = 0, -/// Amount of globals in the `import` section. -imported_globals_count: u32 = 0, -/// Symbol index representing the stack pointer. This will be set upon initializion -/// of a new `ZigObject`. Codegen will make calls into this to create relocations for -/// this symbol each time the stack pointer is moved. -stack_pointer_sym: Symbol.Index, -/// Debug information for the Zig module. -dwarf: ?Dwarf = null, -// Debug section atoms. These are only set when the current compilation -// unit contains Zig code. The lifetime of these atoms are extended -// until the end of the compiler's lifetime. Meaning they're not freed -// during `flush()` in incremental-mode. -debug_info_atom: ?Atom.Index = null, -debug_line_atom: ?Atom.Index = null, -debug_loc_atom: ?Atom.Index = null, -debug_ranges_atom: ?Atom.Index = null, -debug_abbrev_atom: ?Atom.Index = null, -debug_str_atom: ?Atom.Index = null, -debug_pubnames_atom: ?Atom.Index = null, -debug_pubtypes_atom: ?Atom.Index = null, -/// The index of the segment representing the custom '.debug_info' section. -debug_info_index: ?u32 = null, -/// The index of the segment representing the custom '.debug_line' section. -debug_line_index: ?u32 = null, -/// The index of the segment representing the custom '.debug_loc' section. -debug_loc_index: ?u32 = null, -/// The index of the segment representing the custom '.debug_ranges' section. -debug_ranges_index: ?u32 = null, -/// The index of the segment representing the custom '.debug_pubnames' section. -debug_pubnames_index: ?u32 = null, -/// The index of the segment representing the custom '.debug_pubtypes' section. -debug_pubtypes_index: ?u32 = null, -/// The index of the segment representing the custom '.debug_pubtypes' section. -debug_str_index: ?u32 = null, -/// The index of the segment representing the custom '.debug_pubtypes' section. -debug_abbrev_index: ?u32 = null, - -const NavInfo = struct { - atom: Atom.Index = .null, - exports: std.ArrayListUnmanaged(Symbol.Index) = .empty, - - fn @"export"(ni: NavInfo, zo: *const ZigObject, name: Wasm.String) ?Symbol.Index { - for (ni.exports.items) |sym_index| { - if (zo.symbol(sym_index).name == name) return sym_index; - } - return null; - } - - fn appendExport(ni: *NavInfo, gpa: std.mem.Allocator, sym_index: Symbol.Index) !void { - return ni.exports.append(gpa, sym_index); - } - - fn deleteExport(ni: *NavInfo, sym_index: Symbol.Index) void { - for (ni.exports.items, 0..) |idx, index| { - if (idx == sym_index) { - _ = ni.exports.swapRemove(index); - return; - } - } - unreachable; // invalid sym_index - } -}; - -/// Initializes the `ZigObject` with initial symbols. -pub fn init(zig_object: *ZigObject, wasm: *Wasm) !void { - // Initialize an undefined global with the name __stack_pointer. Codegen will use - // this to generate relocations when moving the stack pointer. This symbol will be - // resolved automatically by the final linking stage. - try zig_object.createStackPointer(wasm); - - // TODO: Initialize debug information when we reimplement Dwarf support. -} - -fn createStackPointer(zig_object: *ZigObject, wasm: *Wasm) !void { - const gpa = wasm.base.comp.gpa; - const sym_index = try zig_object.getGlobalSymbol(gpa, wasm.preloaded_strings.__stack_pointer); - const sym = zig_object.symbol(sym_index); - sym.index = zig_object.imported_globals_count; - sym.tag = .global; - const is_wasm32 = wasm.base.comp.root_mod.resolved_target.result.cpu.arch == .wasm32; - try zig_object.imports.putNoClobber(gpa, sym_index, .{ - .name = sym.name, - .module_name = wasm.host_name, - .kind = .{ .global = .{ .valtype = if (is_wasm32) .i32 else .i64, .mutable = true } }, - }); - zig_object.imported_globals_count += 1; - zig_object.stack_pointer_sym = sym_index; -} - -pub fn symbol(zig_object: *const ZigObject, index: Symbol.Index) *Symbol { - return &zig_object.symbols.items[@intFromEnum(index)]; -} - -/// Frees and invalidates all memory of the incrementally compiled Zig module. -/// It is illegal behavior to access the `ZigObject` after calling `deinit`. -pub fn deinit(zig_object: *ZigObject, wasm: *Wasm) void { - const gpa = wasm.base.comp.gpa; - for (zig_object.segment_info.items) |segment_info| { - gpa.free(segment_info.name); - } - - { - var it = zig_object.navs.valueIterator(); - while (it.next()) |nav_info| { - const atom = wasm.getAtomPtr(nav_info.atom); - for (atom.locals.items) |local_index| { - const local_atom = wasm.getAtomPtr(local_index); - local_atom.deinit(gpa); - } - atom.deinit(gpa); - nav_info.exports.deinit(gpa); - } - } - { - for (zig_object.uavs.values()) |atom_index| { - const atom = wasm.getAtomPtr(atom_index); - for (atom.locals.items) |local_index| { - const local_atom = wasm.getAtomPtr(local_index); - local_atom.deinit(gpa); - } - atom.deinit(gpa); - } - } - if (zig_object.global_syms.get(wasm.preloaded_strings.__zig_errors_len)) |sym_index| { - const atom_index = wasm.symbol_atom.get(.{ .file = .zig_object, .index = sym_index }).?; - wasm.getAtomPtr(atom_index).deinit(gpa); - } - if (wasm.symbol_atom.get(.{ .file = .zig_object, .index = zig_object.error_table_symbol })) |atom_index| { - const atom = wasm.getAtomPtr(atom_index); - atom.deinit(gpa); - } - for (zig_object.synthetic_functions.items) |atom_index| { - const atom = wasm.getAtomPtr(atom_index); - atom.deinit(gpa); - } - zig_object.synthetic_functions.deinit(gpa); - for (zig_object.func_types.items) |*ty| { - ty.deinit(gpa); - } - if (zig_object.error_names_atom != .null) { - const atom = wasm.getAtomPtr(zig_object.error_names_atom); - atom.deinit(gpa); - } - zig_object.global_syms.deinit(gpa); - zig_object.func_types.deinit(gpa); - zig_object.atom_types.deinit(gpa); - zig_object.functions.deinit(gpa); - zig_object.imports.deinit(gpa); - zig_object.navs.deinit(gpa); - zig_object.uavs.deinit(gpa); - zig_object.symbols.deinit(gpa); - zig_object.symbols_free_list.deinit(gpa); - zig_object.segment_info.deinit(gpa); - zig_object.segment_free_list.deinit(gpa); - - if (zig_object.dwarf) |*dwarf| { - dwarf.deinit(); - } - gpa.free(zig_object.path.sub_path); - zig_object.* = undefined; -} - -/// Allocates a new symbol and returns its index. -/// Will re-use slots when a symbol was freed at an earlier stage. -pub fn allocateSymbol(zig_object: *ZigObject, gpa: std.mem.Allocator) !Symbol.Index { - try zig_object.symbols.ensureUnusedCapacity(gpa, 1); - const sym: Symbol = .{ - .name = undefined, // will be set after updateDecl as well as during atom creation for decls - .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL), - .tag = .undefined, // will be set after updateDecl - .index = std.math.maxInt(u32), // will be set during atom parsing - .virtual_address = std.math.maxInt(u32), // will be set during atom allocation - }; - if (zig_object.symbols_free_list.popOrNull()) |index| { - zig_object.symbols.items[@intFromEnum(index)] = sym; - return index; - } - const index: Symbol.Index = @enumFromInt(zig_object.symbols.items.len); - zig_object.symbols.appendAssumeCapacity(sym); - return index; -} - -// Generate code for the `Nav`, storing it in memory to be later written to -// the file on flush(). -pub fn updateNav( - zig_object: *ZigObject, - wasm: *Wasm, - pt: Zcu.PerThread, - nav_index: InternPool.Nav.Index, -) !void { - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - const nav = ip.getNav(nav_index); - - const nav_val = zcu.navValue(nav_index); - const is_extern, const lib_name, const nav_init = switch (ip.indexToKey(nav_val.toIntern())) { - .variable => |variable| .{ false, .none, Value.fromInterned(variable.init) }, - .func => return, - .@"extern" => |@"extern"| if (ip.isFunctionType(nav.typeOf(ip))) - return - else - .{ true, @"extern".lib_name, nav_val }, - else => .{ false, .none, nav_val }, - }; - - if (nav_init.typeOf(zcu).hasRuntimeBits(zcu)) { - const gpa = wasm.base.comp.gpa; - const atom_index = try zig_object.getOrCreateAtomForNav(wasm, pt, nav_index); - const atom = wasm.getAtomPtr(atom_index); - atom.clear(); - - if (is_extern) - return zig_object.addOrUpdateImport(wasm, nav.name.toSlice(ip), atom.sym_index, lib_name.toSlice(ip), null); - - var code_writer = std.ArrayList(u8).init(gpa); - defer code_writer.deinit(); - - const res = try codegen.generateSymbol( - &wasm.base, - pt, - zcu.navSrcLoc(nav_index), - nav_init, - &code_writer, - .{ .atom_index = @intFromEnum(atom.sym_index) }, - ); - - const code = switch (res) { - .ok => code_writer.items, - .fail => |em| { - try zcu.failed_codegen.put(zcu.gpa, nav_index, em); - return; - }, - }; - - try zig_object.finishUpdateNav(wasm, pt, nav_index, code); - } -} - -pub fn updateFunc( - zig_object: *ZigObject, - wasm: *Wasm, - pt: Zcu.PerThread, - func_index: InternPool.Index, - air: Air, - liveness: Liveness, -) !void { - const zcu = pt.zcu; - const gpa = zcu.gpa; - const func = pt.zcu.funcInfo(func_index); - const atom_index = try zig_object.getOrCreateAtomForNav(wasm, pt, func.owner_nav); - const atom = wasm.getAtomPtr(atom_index); - atom.clear(); - - var code_writer = std.ArrayList(u8).init(gpa); - defer code_writer.deinit(); - const result = try codegen.generateFunction( - &wasm.base, - pt, - zcu.navSrcLoc(func.owner_nav), - func_index, - air, - liveness, - &code_writer, - .none, - ); - - const code = switch (result) { - .ok => code_writer.items, - .fail => |em| { - try pt.zcu.failed_codegen.put(gpa, func.owner_nav, em); - return; - }, - }; - - return zig_object.finishUpdateNav(wasm, pt, func.owner_nav, code); -} - -fn finishUpdateNav( - zig_object: *ZigObject, - wasm: *Wasm, - pt: Zcu.PerThread, - nav_index: InternPool.Nav.Index, - code: []const u8, -) !void { - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - const gpa = zcu.gpa; - const nav = ip.getNav(nav_index); - const nav_val = zcu.navValue(nav_index); - const nav_info = zig_object.navs.get(nav_index).?; - const atom_index = nav_info.atom; - const atom = wasm.getAtomPtr(atom_index); - const sym = zig_object.symbol(atom.sym_index); - sym.name = try wasm.internString(nav.fqn.toSlice(ip)); - try atom.code.appendSlice(gpa, code); - atom.size = @intCast(code.len); - - if (ip.isFunctionType(nav.typeOf(ip))) { - sym.index = try zig_object.appendFunction(gpa, .{ .type_index = zig_object.atom_types.get(atom_index).? }); - sym.tag = .function; - } else { - const is_const, const nav_init = switch (ip.indexToKey(nav_val.toIntern())) { - .variable => |variable| .{ false, variable.init }, - .@"extern" => |@"extern"| .{ @"extern".is_const, .none }, - else => .{ true, nav_val.toIntern() }, - }; - const segment_name = name: { - if (is_const) break :name ".rodata."; - - if (nav_init != .none and Value.fromInterned(nav_init).isUndefDeep(zcu)) { - break :name switch (zcu.navFileScope(nav_index).mod.optimize_mode) { - .Debug, .ReleaseSafe => ".data.", - .ReleaseFast, .ReleaseSmall => ".bss.", - }; - } - // when the decl is all zeroes, we store the atom in the bss segment, - // in all other cases it will be in the data segment. - for (atom.code.items) |byte| { - if (byte != 0) break :name ".data."; - } - break :name ".bss."; - }; - if ((wasm.base.isObject() or wasm.base.comp.config.import_memory) and - std.mem.startsWith(u8, segment_name, ".bss")) - { - @memset(atom.code.items, 0); - } - // Will be freed upon freeing of decl or after cleanup of Wasm binary. - const full_segment_name = try std.mem.concat(gpa, u8, &.{ - segment_name, - nav.fqn.toSlice(ip), - }); - errdefer gpa.free(full_segment_name); - sym.tag = .data; - sym.index = try zig_object.createDataSegment(gpa, full_segment_name, pt.navAlignment(nav_index)); - } - if (code.len == 0) return; - atom.alignment = pt.navAlignment(nav_index); -} - -/// Creates and initializes a new segment in the 'Data' section. -/// Reuses free slots in the list of segments and returns the index. -fn createDataSegment( - zig_object: *ZigObject, - gpa: std.mem.Allocator, - name: []const u8, - alignment: InternPool.Alignment, -) !u32 { - const segment_index: u32 = if (zig_object.segment_free_list.popOrNull()) |index| - index - else index: { - const idx: u32 = @intCast(zig_object.segment_info.items.len); - _ = try zig_object.segment_info.addOne(gpa); - break :index idx; - }; - zig_object.segment_info.items[segment_index] = .{ - .alignment = alignment, - .flags = 0, - .name = name, - }; - return segment_index; -} - -/// For a given `InternPool.Nav.Index` returns its corresponding `Atom.Index`. -/// When the index was not found, a new `Atom` will be created, and its index will be returned. -/// The newly created Atom is empty with default fields as specified by `Atom.empty`. -pub fn getOrCreateAtomForNav( - zig_object: *ZigObject, - wasm: *Wasm, - pt: Zcu.PerThread, - nav_index: InternPool.Nav.Index, -) !Atom.Index { - const ip = &pt.zcu.intern_pool; - const gpa = pt.zcu.gpa; - const gop = try zig_object.navs.getOrPut(gpa, nav_index); - if (!gop.found_existing) { - const sym_index = try zig_object.allocateSymbol(gpa); - gop.value_ptr.* = .{ .atom = try wasm.createAtom(sym_index, .zig_object) }; - const nav = ip.getNav(nav_index); - const sym = zig_object.symbol(sym_index); - sym.name = try wasm.internString(nav.fqn.toSlice(ip)); - } - return gop.value_ptr.atom; -} - -pub fn lowerUav( - zig_object: *ZigObject, - wasm: *Wasm, - pt: Zcu.PerThread, - uav: InternPool.Index, - explicit_alignment: InternPool.Alignment, - src_loc: Zcu.LazySrcLoc, -) !codegen.GenResult { - const gpa = wasm.base.comp.gpa; - const gop = try zig_object.uavs.getOrPut(gpa, uav); - if (!gop.found_existing) { - var name_buf: [32]u8 = undefined; - const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{ - @intFromEnum(uav), - }) catch unreachable; - - switch (try zig_object.lowerConst(wasm, pt, name, Value.fromInterned(uav), src_loc)) { - .ok => |atom_index| zig_object.uavs.values()[gop.index] = atom_index, - .fail => |em| return .{ .fail = em }, - } - } - - const atom = wasm.getAtomPtr(zig_object.uavs.values()[gop.index]); - atom.alignment = switch (atom.alignment) { - .none => explicit_alignment, - else => switch (explicit_alignment) { - .none => atom.alignment, - else => atom.alignment.maxStrict(explicit_alignment), - }, - }; - return .{ .mcv = .{ .load_symbol = @intFromEnum(atom.sym_index) } }; -} - -const LowerConstResult = union(enum) { - ok: Atom.Index, - fail: *Zcu.ErrorMsg, -}; - -fn lowerConst( - zig_object: *ZigObject, - wasm: *Wasm, - pt: Zcu.PerThread, - name: []const u8, - val: Value, - src_loc: Zcu.LazySrcLoc, -) !LowerConstResult { - const gpa = wasm.base.comp.gpa; - const zcu = wasm.base.comp.zcu.?; - - const ty = val.typeOf(zcu); - - // Create and initialize a new local symbol and atom - const sym_index = try zig_object.allocateSymbol(gpa); - const atom_index = try wasm.createAtom(sym_index, .zig_object); - var value_bytes = std.ArrayList(u8).init(gpa); - defer value_bytes.deinit(); - - const code = code: { - const atom = wasm.getAtomPtr(atom_index); - atom.alignment = ty.abiAlignment(zcu); - const segment_name = try std.mem.concat(gpa, u8, &.{ ".rodata.", name }); - errdefer gpa.free(segment_name); - zig_object.symbol(sym_index).* = .{ - .name = try wasm.internString(name), - .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL), - .tag = .data, - .index = try zig_object.createDataSegment( - gpa, - segment_name, - ty.abiAlignment(zcu), - ), - .virtual_address = undefined, - }; - - const result = try codegen.generateSymbol( - &wasm.base, - pt, - src_loc, - val, - &value_bytes, - .{ .atom_index = @intFromEnum(atom.sym_index) }, - ); - break :code switch (result) { - .ok => value_bytes.items, - .fail => |em| { - return .{ .fail = em }; - }, - }; - }; - - const atom = wasm.getAtomPtr(atom_index); - atom.size = @intCast(code.len); - try atom.code.appendSlice(gpa, code); - return .{ .ok = atom_index }; -} - -/// Returns the symbol index of the error name table. -/// -/// When the symbol does not yet exist, it will create a new one instead. -pub fn getErrorTableSymbol(zig_object: *ZigObject, wasm: *Wasm, pt: Zcu.PerThread) !Symbol.Index { - if (zig_object.error_table_symbol != .null) { - return zig_object.error_table_symbol; - } - - // no error was referenced yet, so create a new symbol and atom for it - // and then return said symbol's index. The final table will be populated - // during `flush` when we know all possible error names. - const gpa = wasm.base.comp.gpa; - const sym_index = try zig_object.allocateSymbol(gpa); - const atom_index = try wasm.createAtom(sym_index, .zig_object); - const atom = wasm.getAtomPtr(atom_index); - const slice_ty = Type.slice_const_u8_sentinel_0; - atom.alignment = slice_ty.abiAlignment(pt.zcu); - - const segment_name = try gpa.dupe(u8, ".rodata.__zig_err_name_table"); - const sym = zig_object.symbol(sym_index); - sym.* = .{ - .name = wasm.preloaded_strings.__zig_err_name_table, - .tag = .data, - .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL), - .index = try zig_object.createDataSegment(gpa, segment_name, atom.alignment), - .virtual_address = undefined, - }; - - log.debug("Error name table was created with symbol index: ({d})", .{@intFromEnum(sym_index)}); - zig_object.error_table_symbol = sym_index; - return sym_index; -} - -/// Populates the error name table, when `error_table_symbol` is not null. -/// -/// This creates a table that consists of pointers and length to each error name. -/// The table is what is being pointed to within the runtime bodies that are generated. -fn populateErrorNameTable(zig_object: *ZigObject, wasm: *Wasm, tid: Zcu.PerThread.Id) !void { - if (zig_object.error_table_symbol == .null) return; - const gpa = wasm.base.comp.gpa; - const atom_index = wasm.symbol_atom.get(.{ .file = .zig_object, .index = zig_object.error_table_symbol }).?; - - // Rather than creating a symbol for each individual error name, - // we create a symbol for the entire region of error names. We then calculate - // the pointers into the list using addends which are appended to the relocation. - const names_sym_index = try zig_object.allocateSymbol(gpa); - const names_atom_index = try wasm.createAtom(names_sym_index, .zig_object); - const names_atom = wasm.getAtomPtr(names_atom_index); - names_atom.alignment = .@"1"; - const segment_name = try gpa.dupe(u8, ".rodata.__zig_err_names"); - const names_symbol = zig_object.symbol(names_sym_index); - names_symbol.* = .{ - .name = wasm.preloaded_strings.__zig_err_names, - .tag = .data, - .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL), - .index = try zig_object.createDataSegment(gpa, segment_name, names_atom.alignment), - .virtual_address = undefined, - }; - - log.debug("Populating error names", .{}); - - // Addend for each relocation to the table - var addend: u32 = 0; - const pt: Zcu.PerThread = .activate(wasm.base.comp.zcu.?, tid); - defer pt.deactivate(); - const slice_ty = Type.slice_const_u8_sentinel_0; - const atom = wasm.getAtomPtr(atom_index); - { - // TODO: remove this unreachable entry - try atom.code.appendNTimes(gpa, 0, 4); - try atom.code.writer(gpa).writeInt(u32, 0, .little); - atom.size += @intCast(slice_ty.abiSize(pt.zcu)); - addend += 1; - - try names_atom.code.append(gpa, 0); - } - const ip = &pt.zcu.intern_pool; - for (ip.global_error_set.getNamesFromMainThread()) |error_name| { - const error_name_slice = error_name.toSlice(ip); - const len: u32 = @intCast(error_name_slice.len + 1); // names are 0-terminated - - const offset = @as(u32, @intCast(atom.code.items.len)); - // first we create the data for the slice of the name - try atom.code.appendNTimes(gpa, 0, 4); // ptr to name, will be relocated - try atom.code.writer(gpa).writeInt(u32, len - 1, .little); - // create relocation to the error name - try atom.relocs.append(gpa, .{ - .index = @intFromEnum(names_atom.sym_index), - .relocation_type = .R_WASM_MEMORY_ADDR_I32, - .offset = offset, - .addend = @intCast(addend), - }); - atom.size += @intCast(slice_ty.abiSize(pt.zcu)); - addend += len; - - // as we updated the error name table, we now store the actual name within the names atom - try names_atom.code.ensureUnusedCapacity(gpa, len); - names_atom.code.appendSliceAssumeCapacity(error_name_slice[0..len]); - - log.debug("Populated error name: '{}'", .{error_name.fmt(ip)}); - } - names_atom.size = addend; - zig_object.error_names_atom = names_atom_index; -} - -/// Either creates a new import, or updates one if existing. -/// When `type_index` is non-null, we assume an external function. -/// In all other cases, a data-symbol will be created instead. -pub fn addOrUpdateImport( - zig_object: *ZigObject, - wasm: *Wasm, - /// Name of the import - name: []const u8, - /// Symbol index that is external - symbol_index: Symbol.Index, - /// Optional library name (i.e. `extern "c" fn foo() void` - lib_name: ?[:0]const u8, - /// The index of the type that represents the function signature - /// when the extern is a function. When this is null, a data-symbol - /// is asserted instead. - type_index: ?u32, -) !void { - const gpa = wasm.base.comp.gpa; - std.debug.assert(symbol_index != .null); - // For the import name, we use the decl's name, rather than the fully qualified name - // Also mangle the name when the lib name is set and not equal to "C" so imports with the same - // name but different module can be resolved correctly. - const mangle_name = if (lib_name) |n| !std.mem.eql(u8, n, "c") else false; - const full_name = if (mangle_name) - try std.fmt.allocPrint(gpa, "{s}|{s}", .{ name, lib_name.? }) - else - name; - defer if (mangle_name) gpa.free(full_name); - - const decl_name_index = try wasm.internString(full_name); - const sym: *Symbol = &zig_object.symbols.items[@intFromEnum(symbol_index)]; - sym.setUndefined(true); - sym.setGlobal(true); - sym.name = decl_name_index; - if (mangle_name) { - // we specified a specific name for the symbol that does not match the import name - sym.setFlag(.WASM_SYM_EXPLICIT_NAME); - } - - if (type_index) |ty_index| { - const gop = try zig_object.imports.getOrPut(gpa, symbol_index); - const module_name = if (lib_name) |n| try wasm.internString(n) else wasm.host_name; - if (!gop.found_existing) zig_object.imported_functions_count += 1; - gop.value_ptr.* = .{ - .module_name = module_name, - .name = try wasm.internString(name), - .kind = .{ .function = ty_index }, - }; - sym.tag = .function; - } else { - sym.tag = .data; - } -} - -/// Returns the symbol index from a symbol of which its flag is set global, -/// such as an exported or imported symbol. -/// If the symbol does not yet exist, creates a new one symbol instead -/// and then returns the index to it. -pub fn getGlobalSymbol(zig_object: *ZigObject, gpa: std.mem.Allocator, name_index: Wasm.String) !Symbol.Index { - const gop = try zig_object.global_syms.getOrPut(gpa, name_index); - if (gop.found_existing) { - return gop.value_ptr.*; - } - - var sym: Symbol = .{ - .name = name_index, - .flags = 0, - .index = undefined, // index to type will be set after merging symbols - .tag = .function, - .virtual_address = std.math.maxInt(u32), - }; - sym.setGlobal(true); - sym.setUndefined(true); - - const sym_index = if (zig_object.symbols_free_list.popOrNull()) |index| index else blk: { - const index: Symbol.Index = @enumFromInt(zig_object.symbols.items.len); - try zig_object.symbols.ensureUnusedCapacity(gpa, 1); - zig_object.symbols.items.len += 1; - break :blk index; - }; - zig_object.symbol(sym_index).* = sym; - gop.value_ptr.* = sym_index; - return sym_index; -} - -/// For a given decl, find the given symbol index's atom, and create a relocation for the type. -/// Returns the given pointer address -pub fn getNavVAddr( - zig_object: *ZigObject, - wasm: *Wasm, - pt: Zcu.PerThread, - nav_index: InternPool.Nav.Index, - reloc_info: link.File.RelocInfo, -) !u64 { - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - const gpa = zcu.gpa; - const nav = ip.getNav(nav_index); - const target = &zcu.navFileScope(nav_index).mod.resolved_target.result; - - const target_atom_index = try zig_object.getOrCreateAtomForNav(wasm, pt, nav_index); - const target_atom = wasm.getAtom(target_atom_index); - const target_symbol_index = @intFromEnum(target_atom.sym_index); - if (nav.getExtern(ip)) |@"extern"| { - try zig_object.addOrUpdateImport( - wasm, - nav.name.toSlice(ip), - target_atom.sym_index, - @"extern".lib_name.toSlice(ip), - null, - ); - } - - std.debug.assert(reloc_info.parent.atom_index != 0); - const atom_index = wasm.symbol_atom.get(.{ - .file = .zig_object, - .index = @enumFromInt(reloc_info.parent.atom_index), - }).?; - const atom = wasm.getAtomPtr(atom_index); - const is_wasm32 = target.cpu.arch == .wasm32; - if (ip.isFunctionType(ip.getNav(nav_index).typeOf(ip))) { - std.debug.assert(reloc_info.addend == 0); // addend not allowed for function relocations - try atom.relocs.append(gpa, .{ - .index = target_symbol_index, - .offset = @intCast(reloc_info.offset), - .relocation_type = if (is_wasm32) .R_WASM_TABLE_INDEX_I32 else .R_WASM_TABLE_INDEX_I64, - }); - } else { - try atom.relocs.append(gpa, .{ - .index = target_symbol_index, - .offset = @intCast(reloc_info.offset), - .relocation_type = if (is_wasm32) .R_WASM_MEMORY_ADDR_I32 else .R_WASM_MEMORY_ADDR_I64, - .addend = @intCast(reloc_info.addend), - }); - } - - // we do not know the final address at this point, - // as atom allocation will determine the address and relocations - // will calculate and rewrite this. Therefore, we simply return the symbol index - // that was targeted. - return target_symbol_index; -} - -pub fn getUavVAddr( - zig_object: *ZigObject, - wasm: *Wasm, - uav: InternPool.Index, - reloc_info: link.File.RelocInfo, -) !u64 { - const gpa = wasm.base.comp.gpa; - const target = wasm.base.comp.root_mod.resolved_target.result; - const atom_index = zig_object.uavs.get(uav).?; - const target_symbol_index = @intFromEnum(wasm.getAtom(atom_index).sym_index); - - const parent_atom_index = wasm.symbol_atom.get(.{ - .file = .zig_object, - .index = @enumFromInt(reloc_info.parent.atom_index), - }).?; - const parent_atom = wasm.getAtomPtr(parent_atom_index); - const is_wasm32 = target.cpu.arch == .wasm32; - const zcu = wasm.base.comp.zcu.?; - const ty = Type.fromInterned(zcu.intern_pool.typeOf(uav)); - if (ty.zigTypeTag(zcu) == .@"fn") { - std.debug.assert(reloc_info.addend == 0); // addend not allowed for function relocations - try parent_atom.relocs.append(gpa, .{ - .index = target_symbol_index, - .offset = @intCast(reloc_info.offset), - .relocation_type = if (is_wasm32) .R_WASM_TABLE_INDEX_I32 else .R_WASM_TABLE_INDEX_I64, - }); - } else { - try parent_atom.relocs.append(gpa, .{ - .index = target_symbol_index, - .offset = @intCast(reloc_info.offset), - .relocation_type = if (is_wasm32) .R_WASM_MEMORY_ADDR_I32 else .R_WASM_MEMORY_ADDR_I64, - .addend = @intCast(reloc_info.addend), - }); - } - - // we do not know the final address at this point, - // as atom allocation will determine the address and relocations - // will calculate and rewrite this. Therefore, we simply return the symbol index - // that was targeted. - return target_symbol_index; -} - -pub fn deleteExport( - zig_object: *ZigObject, - wasm: *Wasm, - exported: Zcu.Exported, - name: InternPool.NullTerminatedString, -) void { - const zcu = wasm.base.comp.zcu.?; - const nav_index = switch (exported) { - .nav => |nav_index| nav_index, - .uav => @panic("TODO: implement Wasm linker code for exporting a constant value"), - }; - const nav_info = zig_object.navs.getPtr(nav_index) orelse return; - const name_interned = wasm.getExistingString(name.toSlice(&zcu.intern_pool)).?; - if (nav_info.@"export"(zig_object, name_interned)) |sym_index| { - const sym = zig_object.symbol(sym_index); - nav_info.deleteExport(sym_index); - std.debug.assert(zig_object.global_syms.remove(sym.name)); - std.debug.assert(wasm.symbol_atom.remove(.{ .file = .zig_object, .index = sym_index })); - zig_object.symbols_free_list.append(wasm.base.comp.gpa, sym_index) catch {}; - sym.tag = .dead; - } -} - -pub fn updateExports( - zig_object: *ZigObject, - wasm: *Wasm, - pt: Zcu.PerThread, - exported: Zcu.Exported, - export_indices: []const u32, -) !void { - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - const nav_index = switch (exported) { - .nav => |nav| nav, - .uav => |uav| { - _ = uav; - @panic("TODO: implement Wasm linker code for exporting a constant value"); - }, - }; - const nav = ip.getNav(nav_index); - const atom_index = try zig_object.getOrCreateAtomForNav(wasm, pt, nav_index); - const nav_info = zig_object.navs.getPtr(nav_index).?; - const atom = wasm.getAtom(atom_index); - const atom_sym = wasm.symbolLocSymbol(atom.symbolLoc()).*; - const gpa = zcu.gpa; - log.debug("Updating exports for decl '{}'", .{nav.name.fmt(ip)}); - - for (export_indices) |export_idx| { - const exp = zcu.all_exports.items[export_idx]; - if (exp.opts.section.toSlice(ip)) |section| { - try zcu.failed_exports.putNoClobber(gpa, export_idx, try Zcu.ErrorMsg.create( - gpa, - zcu.navSrcLoc(nav_index), - "Unimplemented: ExportOptions.section '{s}'", - .{section}, - )); - continue; - } - - const export_name = try wasm.internString(exp.opts.name.toSlice(ip)); - const sym_index = if (nav_info.@"export"(zig_object, export_name)) |idx| idx else index: { - const sym_index = try zig_object.allocateSymbol(gpa); - try nav_info.appendExport(gpa, sym_index); - break :index sym_index; - }; - - const sym = zig_object.symbol(sym_index); - sym.setGlobal(true); - sym.setUndefined(false); - sym.index = atom_sym.index; - sym.tag = atom_sym.tag; - sym.name = export_name; - - switch (exp.opts.linkage) { - .internal => { - sym.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); - }, - .weak => { - sym.setFlag(.WASM_SYM_BINDING_WEAK); - }, - .strong => {}, // symbols are strong by default - .link_once => { - try zcu.failed_exports.putNoClobber(gpa, export_idx, try Zcu.ErrorMsg.create( - gpa, - zcu.navSrcLoc(nav_index), - "Unimplemented: LinkOnce", - .{}, - )); - continue; - }, - } - if (exp.opts.visibility == .hidden) { - sym.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); - } - log.debug(" with name '{s}' - {}", .{ wasm.stringSlice(export_name), sym }); - try zig_object.global_syms.put(gpa, export_name, sym_index); - try wasm.symbol_atom.put(gpa, .{ .file = .zig_object, .index = sym_index }, atom_index); - } -} - -pub fn freeNav(zig_object: *ZigObject, wasm: *Wasm, nav_index: InternPool.Nav.Index) void { - const gpa = wasm.base.comp.gpa; - const zcu = wasm.base.comp.zcu.?; - const ip = &zcu.intern_pool; - const nav_info = zig_object.navs.getPtr(nav_index).?; - const atom_index = nav_info.atom; - const atom = wasm.getAtomPtr(atom_index); - zig_object.symbols_free_list.append(gpa, atom.sym_index) catch {}; - for (nav_info.exports.items) |exp_sym_index| { - const exp_sym = zig_object.symbol(exp_sym_index); - exp_sym.tag = .dead; - zig_object.symbols_free_list.append(exp_sym_index) catch {}; - } - nav_info.exports.deinit(gpa); - std.debug.assert(zig_object.navs.remove(nav_index)); - const sym = &zig_object.symbols.items[atom.sym_index]; - for (atom.locals.items) |local_atom_index| { - const local_atom = wasm.getAtom(local_atom_index); - const local_symbol = &zig_object.symbols.items[local_atom.sym_index]; - std.debug.assert(local_symbol.tag == .data); - zig_object.symbols_free_list.append(gpa, local_atom.sym_index) catch {}; - std.debug.assert(wasm.symbol_atom.remove(local_atom.symbolLoc())); - local_symbol.tag = .dead; // also for any local symbol - const segment = &zig_object.segment_info.items[local_atom.sym_index]; - gpa.free(segment.name); - segment.name = &.{}; // Ensure no accidental double free - } - - const nav = ip.getNav(nav_index); - if (nav.getExtern(ip) != null) { - std.debug.assert(zig_object.imports.remove(atom.sym_index)); - } - std.debug.assert(wasm.symbol_atom.remove(atom.symbolLoc())); - - // if (wasm.dwarf) |*dwarf| { - // dwarf.freeDecl(decl_index); - // } - - atom.prev = null; - sym.tag = .dead; - if (sym.isGlobal()) { - std.debug.assert(zig_object.global_syms.remove(atom.sym_index)); - } - if (ip.isFunctionType(nav.typeOf(ip))) { - zig_object.functions_free_list.append(gpa, sym.index) catch {}; - std.debug.assert(zig_object.atom_types.remove(atom_index)); - } else { - zig_object.segment_free_list.append(gpa, sym.index) catch {}; - const segment = &zig_object.segment_info.items[sym.index]; - gpa.free(segment.name); - segment.name = &.{}; // Prevent accidental double free - } -} - -fn getTypeIndex(zig_object: *const ZigObject, func_type: std.wasm.Type) ?u32 { - var index: u32 = 0; - while (index < zig_object.func_types.items.len) : (index += 1) { - if (zig_object.func_types.items[index].eql(func_type)) return index; - } - return null; -} - -/// Searches for a matching function signature. When no matching signature is found, -/// a new entry will be made. The value returned is the index of the type within `wasm.func_types`. -pub fn putOrGetFuncType(zig_object: *ZigObject, gpa: std.mem.Allocator, func_type: std.wasm.Type) !u32 { - if (zig_object.getTypeIndex(func_type)) |index| { - return index; - } - - // functype does not exist. - const index: u32 = @intCast(zig_object.func_types.items.len); - const params = try gpa.dupe(std.wasm.Valtype, func_type.params); - errdefer gpa.free(params); - const returns = try gpa.dupe(std.wasm.Valtype, func_type.returns); - errdefer gpa.free(returns); - try zig_object.func_types.append(gpa, .{ - .params = params, - .returns = returns, - }); - return index; -} - -/// Generates an atom containing the global error set' size. -/// This will only be generated if the symbol exists. -fn setupErrorsLen(zig_object: *ZigObject, wasm: *Wasm) !void { - const gpa = wasm.base.comp.gpa; - const sym_index = zig_object.global_syms.get(wasm.preloaded_strings.__zig_errors_len) orelse return; - - const errors_len = 1 + wasm.base.comp.zcu.?.intern_pool.global_error_set.getNamesFromMainThread().len; - // overwrite existing atom if it already exists (maybe the error set has increased) - // if not, allocate a new atom. - const atom_index = if (wasm.symbol_atom.get(.{ .file = .zig_object, .index = sym_index })) |index| blk: { - const atom = wasm.getAtomPtr(index); - atom.prev = .null; - atom.deinit(gpa); - break :blk index; - } else idx: { - // We found a call to __zig_errors_len so make the symbol a local symbol - // and define it, so the final binary or resulting object file will not attempt - // to resolve it. - const sym = zig_object.symbol(sym_index); - sym.setGlobal(false); - sym.setUndefined(false); - sym.tag = .data; - const segment_name = try gpa.dupe(u8, ".rodata.__zig_errors_len"); - sym.index = try zig_object.createDataSegment(gpa, segment_name, .@"2"); - break :idx try wasm.createAtom(sym_index, .zig_object); - }; - - const atom = wasm.getAtomPtr(atom_index); - atom.code.clearRetainingCapacity(); - atom.sym_index = sym_index; - atom.size = 2; - atom.alignment = .@"2"; - try atom.code.writer(gpa).writeInt(u16, @intCast(errors_len), .little); -} - -/// Initializes symbols and atoms for the debug sections -/// Initialization is only done when compiling Zig code. -/// When Zig is invoked as a linker instead, the atoms -/// and symbols come from the object files instead. -pub fn initDebugSections(zig_object: *ZigObject) !void { - if (zig_object.dwarf == null) return; // not compiling Zig code, so no need to pre-initialize debug sections - std.debug.assert(zig_object.debug_info_index == null); - // this will create an Atom and set the index for us. - zig_object.debug_info_atom = try zig_object.createDebugSectionForIndex(&zig_object.debug_info_index, ".debug_info"); - zig_object.debug_line_atom = try zig_object.createDebugSectionForIndex(&zig_object.debug_line_index, ".debug_line"); - zig_object.debug_loc_atom = try zig_object.createDebugSectionForIndex(&zig_object.debug_loc_index, ".debug_loc"); - zig_object.debug_abbrev_atom = try zig_object.createDebugSectionForIndex(&zig_object.debug_abbrev_index, ".debug_abbrev"); - zig_object.debug_ranges_atom = try zig_object.createDebugSectionForIndex(&zig_object.debug_ranges_index, ".debug_ranges"); - zig_object.debug_str_atom = try zig_object.createDebugSectionForIndex(&zig_object.debug_str_index, ".debug_str"); - zig_object.debug_pubnames_atom = try zig_object.createDebugSectionForIndex(&zig_object.debug_pubnames_index, ".debug_pubnames"); - zig_object.debug_pubtypes_atom = try zig_object.createDebugSectionForIndex(&zig_object.debug_pubtypes_index, ".debug_pubtypes"); -} - -/// From a given index variable, creates a new debug section. -/// This initializes the index, appends a new segment, -/// and finally, creates a managed `Atom`. -pub fn createDebugSectionForIndex(zig_object: *ZigObject, wasm: *Wasm, index: *?u32, name: []const u8) !Atom.Index { - const gpa = wasm.base.comp.gpa; - const new_index: u32 = @intCast(zig_object.segments.items.len); - index.* = new_index; - try zig_object.appendDummySegment(); - - const sym_index = try zig_object.allocateSymbol(gpa); - const atom_index = try wasm.createAtom(sym_index, .zig_object); - const atom = wasm.getAtomPtr(atom_index); - zig_object.symbols.items[sym_index] = .{ - .tag = .section, - .name = try wasm.internString(name), - .index = 0, - .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL), - }; - - atom.alignment = .@"1"; // debug sections are always 1-byte-aligned - return atom_index; -} - -pub fn updateLineNumber(zig_object: *ZigObject, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) !void { - if (zig_object.dwarf) |*dw| { - try dw.updateLineNumber(pt.zcu, ti_id); - } -} - -/// Allocates debug atoms into their respective debug sections -/// to merge them with maybe-existing debug atoms from object files. -fn allocateDebugAtoms(zig_object: *ZigObject) !void { - if (zig_object.dwarf == null) return; - - const allocAtom = struct { - fn f(ctx: *ZigObject, maybe_index: *?u32, atom_index: Atom.Index) !void { - const index = maybe_index.* orelse idx: { - const index = @as(u32, @intCast(ctx.segments.items.len)); - try ctx.appendDummySegment(); - maybe_index.* = index; - break :idx index; - }; - const atom = ctx.getAtomPtr(atom_index); - atom.size = @as(u32, @intCast(atom.code.items.len)); - ctx.symbols.items[atom.sym_index].index = index; - try ctx.appendAtomAtIndex(index, atom_index); - } - }.f; - - try allocAtom(zig_object, &zig_object.debug_info_index, zig_object.debug_info_atom.?); - try allocAtom(zig_object, &zig_object.debug_line_index, zig_object.debug_line_atom.?); - try allocAtom(zig_object, &zig_object.debug_loc_index, zig_object.debug_loc_atom.?); - try allocAtom(zig_object, &zig_object.debug_str_index, zig_object.debug_str_atom.?); - try allocAtom(zig_object, &zig_object.debug_ranges_index, zig_object.debug_ranges_atom.?); - try allocAtom(zig_object, &zig_object.debug_abbrev_index, zig_object.debug_abbrev_atom.?); - try allocAtom(zig_object, &zig_object.debug_pubnames_index, zig_object.debug_pubnames_atom.?); - try allocAtom(zig_object, &zig_object.debug_pubtypes_index, zig_object.debug_pubtypes_atom.?); -} - -/// For the given `decl_index`, stores the corresponding type representing the function signature. -/// Asserts declaration has an associated `Atom`. -/// Returns the index into the list of types. -pub fn storeDeclType(zig_object: *ZigObject, gpa: std.mem.Allocator, nav_index: InternPool.Nav.Index, func_type: std.wasm.Type) !u32 { - const nav_info = zig_object.navs.get(nav_index).?; - const index = try zig_object.putOrGetFuncType(gpa, func_type); - try zig_object.atom_types.put(gpa, nav_info.atom, index); - return index; -} - -/// The symbols in ZigObject are already represented by an atom as we need to store its data. -/// So rather than creating a new Atom and returning its index, we use this opportunity to scan -/// its relocations and create any GOT symbols or function table indexes it may require. -pub fn parseSymbolIntoAtom(zig_object: *ZigObject, wasm: *Wasm, index: Symbol.Index) !Atom.Index { - const gpa = wasm.base.comp.gpa; - const loc: Wasm.SymbolLoc = .{ .file = .zig_object, .index = index }; - const atom_index = wasm.symbol_atom.get(loc).?; - const final_index = try wasm.getMatchingSegment(.zig_object, index); - try wasm.appendAtomAtIndex(final_index, atom_index); - const atom = wasm.getAtom(atom_index); - for (atom.relocs.items) |reloc| { - const reloc_index: Symbol.Index = @enumFromInt(reloc.index); - switch (reloc.relocation_type) { - .R_WASM_TABLE_INDEX_I32, - .R_WASM_TABLE_INDEX_I64, - .R_WASM_TABLE_INDEX_SLEB, - .R_WASM_TABLE_INDEX_SLEB64, - => { - try wasm.function_table.put(gpa, .{ - .file = .zig_object, - .index = reloc_index, - }, 0); - }, - .R_WASM_GLOBAL_INDEX_I32, - .R_WASM_GLOBAL_INDEX_LEB, - => { - const sym = zig_object.symbol(reloc_index); - if (sym.tag != .global) { - try wasm.got_symbols.append(gpa, .{ - .file = .zig_object, - .index = reloc_index, - }); - } - }, - else => {}, - } - } - return atom_index; -} - -/// Creates a new Wasm function with a given symbol name and body. -/// Returns the symbol index of the new function. -pub fn createFunction( - zig_object: *ZigObject, - wasm: *Wasm, - symbol_name: []const u8, - func_ty: std.wasm.Type, - function_body: *std.ArrayList(u8), - relocations: *std.ArrayList(Wasm.Relocation), -) !Symbol.Index { - const gpa = wasm.base.comp.gpa; - const sym_index = try zig_object.allocateSymbol(gpa); - const sym = zig_object.symbol(sym_index); - sym.tag = .function; - sym.name = try wasm.internString(symbol_name); - const type_index = try zig_object.putOrGetFuncType(gpa, func_ty); - sym.index = try zig_object.appendFunction(gpa, .{ .type_index = type_index }); - - const atom_index = try wasm.createAtom(sym_index, .zig_object); - const atom = wasm.getAtomPtr(atom_index); - atom.size = @intCast(function_body.items.len); - atom.code = function_body.moveToUnmanaged(); - atom.relocs = relocations.moveToUnmanaged(); - - try zig_object.synthetic_functions.append(gpa, atom_index); - return sym_index; -} - -/// Appends a new `std.wasm.Func` to the list of functions and returns its index. -fn appendFunction(zig_object: *ZigObject, gpa: std.mem.Allocator, func: std.wasm.Func) !u32 { - const index: u32 = if (zig_object.functions_free_list.popOrNull()) |idx| - idx - else idx: { - const len: u32 = @intCast(zig_object.functions.items.len); - _ = try zig_object.functions.addOne(gpa); - break :idx len; - }; - zig_object.functions.items[index] = func; - - return index; -} - -pub fn flushModule(zig_object: *ZigObject, wasm: *Wasm, tid: Zcu.PerThread.Id) !void { - try zig_object.populateErrorNameTable(wasm, tid); - try zig_object.setupErrorsLen(wasm); -} - -const build_options = @import("build_options"); -const builtin = @import("builtin"); -const codegen = @import("../../codegen.zig"); -const link = @import("../../link.zig"); -const log = std.log.scoped(.zig_object); -const std = @import("std"); -const Path = std.Build.Cache.Path; - -const Air = @import("../../Air.zig"); -const Atom = Wasm.Atom; -const Dwarf = @import("../Dwarf.zig"); -const InternPool = @import("../../InternPool.zig"); -const Liveness = @import("../../Liveness.zig"); -const Zcu = @import("../../Zcu.zig"); -const Symbol = @import("Symbol.zig"); -const Type = @import("../../Type.zig"); -const Value = @import("../../Value.zig"); -const Wasm = @import("../Wasm.zig"); -const AnalUnit = InternPool.AnalUnit; -const ZigObject = @This(); |
