authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2024-01-31 17:21:32+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2024-02-29 15:23:04+01:00
log5a0f2af7e4aa01f861d86bfe9fb457ffde3d335e
tree4b6beeb03bc5ab32c3edb77cfeb7fdb98f023755
parentc153f94c892fc3b718d29ba4ae3234e99d4baba4
signaturelock-open Commit is signed but in an unrecognized format.

wasm: reimplement Zig errors in linker


2 files changed, 37 insertions(+), 73 deletions(-)

src/link/Wasm.zig+6-9
......@@ -1333,13 +1333,6 @@ fn resolveLazySymbols(wasm: *Wasm) !void {
13331333 }
13341334 }
13351335 }
1336 if (wasm.string_table.getOffset("__zig_errors_len")) |name_offset| {
1337 if (wasm.undefs.fetchSwapRemove(name_offset)) |kv| {
1338 const loc = try wasm.createSyntheticSymbolOffset(name_offset, .data);
1339 try wasm.discarded.putNoClobber(gpa, kv.value, loc);
1340 _ = wasm.resolved_symbols.swapRemove(kv.value);
1341 }
1342 }
13431336}
13441337
13451338// Tries to find a global symbol by its name. Returns null when not found,
......@@ -2009,8 +2002,7 @@ fn mergeSections(wasm: *Wasm) !void {
20092002
20102003 for (wasm.resolved_symbols.keys()) |sym_loc| {
20112004 const obj_file = wasm.file(sym_loc.file) orelse {
2012 // Zig code-generated symbols are already within the sections and do not
2013 // require to be merged
2005 // Synthetic symbols already live in the corresponding sections.
20142006 continue;
20152007 };
20162008
......@@ -2056,6 +2048,7 @@ fn mergeSections(wasm: *Wasm) !void {
20562048 symbol.index = @as(u32, @intCast(wasm.tables.items.len)) + wasm.imported_tables_count;
20572049 try wasm.tables.append(gpa, original_table);
20582050 },
2051 .dead, .undefined => unreachable,
20592052 else => {},
20602053 }
20612054 }
......@@ -2719,6 +2712,10 @@ pub fn flushModule(wasm: *Wasm, arena: Allocator, prog_node: *std.Progress.Node)
27192712 sub_prog_node.activate();
27202713 defer sub_prog_node.end();
27212714
2715 if (wasm.zigObjectPtr()) |zig_object| {
2716 try zig_object.flushModule(wasm);
2717 }
2718
27222719 // ensure the error names table is populated when an error name is referenced
27232720 // try wasm.populateErrorNameTable();
27242721
src/link/Wasm/ZigObject.zig+31-64
......@@ -551,16 +551,15 @@ pub fn getErrorTableSymbol(zig_object: *ZigObject, wasm_file: *Wasm) !u32 {
551551 atom.alignment = slice_ty.abiAlignment(mod);
552552
553553 const sym_name = try zig_object.string_table.insert(gpa, "__zig_err_name_table");
554 const segment_name = try gpa.dupe(u8, ".rodata.__zig_err_name_table");
554555 const sym = zig_object.symbol(sym_index);
555556 sym.* = .{
556557 .name = sym_name,
557558 .tag = .data,
558559 .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL),
559 .index = 0,
560 .index = try zig_object.createDataSegment(gpa, segment_name, atom.alignment),
560561 .virtual_address = undefined,
561562 };
562 // TODO: can we remove this?
563 // sym.mark();
564563
565564 log.debug("Error name table was created with symbol index: ({d})", .{sym_index});
566565 zig_object.error_table_symbol = sym_index;
......@@ -584,15 +583,15 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void {
584583 const names_atom = wasm_file.getAtomPtr(names_atom_index);
585584 names_atom.alignment = .@"1";
586585 const sym_name = try zig_object.string_table.insert(gpa, "__zig_err_names");
586 const segment_name = try gpa.dupe(u8, ".rodata.__zig_err_names");
587587 const names_symbol = &zig_object.symbols.items[names_sym_index];
588588 names_symbol.* = .{
589589 .name = sym_name,
590590 .tag = .data,
591591 .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL),
592 .index = 0,
592 .index = try zig_object.createDataSegment(gpa, segment_name, names_atom.alignment),
593593 .virtual_address = undefined,
594594 };
595 names_symbol.mark();
596595
597596 log.debug("Populating error names", .{});
598597
......@@ -628,11 +627,6 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void {
628627 log.debug("Populated error name: '{s}'", .{error_name});
629628 }
630629 names_atom.size = addend;
631
632 // link the atoms with the rest of the binary so they can be allocated
633 // and relocations will be performed.
634 try wasm_file.parseAtom(atom_index, .{ .data = .read_only });
635 try wasm_file.parseAtom(names_atom_index, .{ .data = .read_only });
636630}
637631
638632/// Either creates a new import, or updates one if existing.
......@@ -995,76 +989,44 @@ pub fn putOrGetFuncType(zig_object: *ZigObject, gpa: std.mem.Allocator, func_typ
995989 return index;
996990}
997991
998/// Kind represents the type of an Atom, which is only
999/// used to parse a decl into an Atom to define in which section
1000/// or segment it should be placed.
1001const Kind = union(enum) {
1002 /// Represents the segment the data symbol should
1003 /// be inserted into.
1004 /// TODO: Add TLS segments
1005 data: enum {
1006 read_only,
1007 uninitialized,
1008 initialized,
1009 },
1010 function: void,
1011
1012 /// Returns the segment name the data kind represents.
1013 /// Asserts `kind` has its active tag set to `data`.
1014 fn segmentName(kind: Kind) []const u8 {
1015 switch (kind.data) {
1016 .read_only => return ".rodata.",
1017 .uninitialized => return ".bss.",
1018 .initialized => return ".data.",
1019 }
1020 }
1021};
1022
1023/// Parses an Atom and inserts its metadata into the corresponding sections.
1024pub fn parseAtom(zig_object: *ZigObject, wasm_file: *Wasm, atom_index: Atom.Index, kind: Kind) !void {
1025 // TODO: Revisit
1026 _ = zig_object;
1027 _ = wasm_file;
1028 _ = atom_index;
1029 _ = kind;
1030}
1031
1032992/// Generates an atom containing the global error set' size.
1033993/// This will only be generated if the symbol exists.
1034994fn setupErrorsLen(zig_object: *ZigObject, wasm_file: *Wasm) !void {
1035995 const gpa = wasm_file.base.comp.gpa;
1036 const loc = zig_object.findGlobalSymbol("__zig_errors_len") orelse return;
996 const sym_index = zig_object.findGlobalSymbol("__zig_errors_len") orelse return;
1037997
1038998 const errors_len = wasm_file.base.comp.module.?.global_error_set.count();
1039999 // overwrite existing atom if it already exists (maybe the error set has increased)
10401000 // if not, allcoate a new atom.
1041 const atom_index = if (wasm_file.symbol_atom.get(loc)) |index| blk: {
1001 const atom_index = if (wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = sym_index })) |index| blk: {
10421002 const atom = wasm_file.getAtomPtr(index);
1043 if (atom.next) |next_atom_index| {
1044 const next_atom = wasm_file.getAtomPtr(next_atom_index);
1045 next_atom.prev = atom.prev;
1046 atom.next = null;
1047 }
1048 if (atom.prev) |prev_index| {
1049 const prev_atom = wasm_file.getAtomPtr(prev_index);
1050 prev_atom.next = atom.next;
1051 atom.prev = null;
1052 }
1003 atom.prev = null;
10531004 atom.deinit(gpa);
10541005 break :blk index;
1055 } else new_atom: {
1056 const atom_index: Atom.Index = @intCast(wasm_file.managed_atoms.items.len);
1057 try wasm_file.symbol_atom.put(gpa, loc, atom_index);
1058 try wasm_file.managed_atoms.append(gpa, undefined);
1059 break :new_atom atom_index;
1006 } else idx: {
1007 // We found a call to __zig_errors_len so make the symbol a local symbol
1008 // and define it, so the final binary or resulting object file will not attempt
1009 // to resolve it.
1010 const sym = zig_object.symbol(sym_index);
1011 sym.setGlobal(false);
1012 sym.setUndefined(false);
1013 sym.tag = .data;
1014 const segment_name = try gpa.dupe(u8, ".rodata.__zig_errors_len");
1015 sym.index = try zig_object.createDataSegment(gpa, segment_name, .@"2");
1016 break :idx try wasm_file.createAtom(sym_index, zig_object.index);
10601017 };
1018
10611019 const atom = wasm_file.getAtomPtr(atom_index);
1062 atom.* = Atom.empty;
1063 atom.sym_index = loc.index;
1020 atom.code.clearRetainingCapacity();
1021 atom.sym_index = sym_index;
10641022 atom.size = 2;
1023 atom.alignment = .@"2";
10651024 try atom.code.writer(gpa).writeInt(u16, @intCast(errors_len), .little);
1025}
10661026
1067 // try wasm.parseAtom(atom_index, .{ .data = .read_only });
1027fn findGlobalSymbol(zig_object: *ZigObject, name: []const u8) ?u32 {
1028 const offset = zig_object.string_table.getOffset(name) orelse return null;
1029 return zig_object.global_syms.get(offset);
10681030}
10691031
10701032/// Initializes symbols and atoms for the debug sections
......@@ -1232,6 +1194,11 @@ fn appendFunction(zig_object: *ZigObject, gpa: std.mem.Allocator, func: std.wasm
12321194 return index;
12331195}
12341196
1197pub fn flushModule(zig_object: *ZigObject, wasm_file: *Wasm) !void {
1198 try zig_object.populateErrorNameTable(wasm_file);
1199 try zig_object.setupErrorsLen(wasm_file);
1200}
1201
12351202const build_options = @import("build_options");
12361203const builtin = @import("builtin");
12371204const codegen = @import("../../codegen.zig");