| ... | @@ -551,16 +551,15 @@ pub fn getErrorTableSymbol(zig_object: *ZigObject, wasm_file: *Wasm) !u32 { | ... | @@ -551,16 +551,15 @@ pub fn getErrorTableSymbol(zig_object: *ZigObject, wasm_file: *Wasm) !u32 { |
| 551 | atom.alignment = slice_ty.abiAlignment(mod); | 551 | atom.alignment = slice_ty.abiAlignment(mod); |
| 552 | | 552 | |
| 553 | const sym_name = try zig_object.string_table.insert(gpa, "__zig_err_name_table"); | 553 | 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"); |
| 554 | const sym = zig_object.symbol(sym_index); | 555 | const sym = zig_object.symbol(sym_index); |
| 555 | sym.* = .{ | 556 | sym.* = .{ |
| 556 | .name = sym_name, | 557 | .name = sym_name, |
| 557 | .tag = .data, | 558 | .tag = .data, |
| 558 | .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL), | 559 | .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL), |
| 559 | .index = 0, | 560 | .index = try zig_object.createDataSegment(gpa, segment_name, atom.alignment), |
| 560 | .virtual_address = undefined, | 561 | .virtual_address = undefined, |
| 561 | }; | 562 | }; |
| 562 | // TODO: can we remove this? | | |
| 563 | // sym.mark(); | | |
| 564 | | 563 | |
| 565 | log.debug("Error name table was created with symbol index: ({d})", .{sym_index}); | 564 | log.debug("Error name table was created with symbol index: ({d})", .{sym_index}); |
| 566 | zig_object.error_table_symbol = sym_index; | 565 | zig_object.error_table_symbol = sym_index; |
| ... | @@ -584,15 +583,15 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { | ... | @@ -584,15 +583,15 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { |
| 584 | const names_atom = wasm_file.getAtomPtr(names_atom_index); | 583 | const names_atom = wasm_file.getAtomPtr(names_atom_index); |
| 585 | names_atom.alignment = .@"1"; | 584 | names_atom.alignment = .@"1"; |
| 586 | const sym_name = try zig_object.string_table.insert(gpa, "__zig_err_names"); | 585 | 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"); |
| 587 | const names_symbol = &zig_object.symbols.items[names_sym_index]; | 587 | const names_symbol = &zig_object.symbols.items[names_sym_index]; |
| 588 | names_symbol.* = .{ | 588 | names_symbol.* = .{ |
| 589 | .name = sym_name, | 589 | .name = sym_name, |
| 590 | .tag = .data, | 590 | .tag = .data, |
| 591 | .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL), | 591 | .flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL), |
| 592 | .index = 0, | 592 | .index = try zig_object.createDataSegment(gpa, segment_name, names_atom.alignment), |
| 593 | .virtual_address = undefined, | 593 | .virtual_address = undefined, |
| 594 | }; | 594 | }; |
| 595 | names_symbol.mark(); | | |
| 596 | | 595 | |
| 597 | log.debug("Populating error names", .{}); | 596 | log.debug("Populating error names", .{}); |
| 598 | | 597 | |
| ... | @@ -628,11 +627,6 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { | ... | @@ -628,11 +627,6 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { |
| 628 | log.debug("Populated error name: '{s}'", .{error_name}); | 627 | log.debug("Populated error name: '{s}'", .{error_name}); |
| 629 | } | 628 | } |
| 630 | names_atom.size = addend; | 629 | 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 }); | | |
| 636 | } | 630 | } |
| 637 | | 631 | |
| 638 | /// Either creates a new import, or updates one if existing. | 632 | /// 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 | ... | @@ -995,76 +989,44 @@ pub fn putOrGetFuncType(zig_object: *ZigObject, gpa: std.mem.Allocator, func_typ |
| 995 | return index; | 989 | return index; |
| 996 | } | 990 | } |
| 997 | | 991 | |
| 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. | | |
| 1001 | const 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. | | |
| 1024 | pub 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 | | | |
| 1032 | /// Generates an atom containing the global error set' size. | 992 | /// Generates an atom containing the global error set' size. |
| 1033 | /// This will only be generated if the symbol exists. | 993 | /// This will only be generated if the symbol exists. |
| 1034 | fn setupErrorsLen(zig_object: *ZigObject, wasm_file: *Wasm) !void { | 994 | fn setupErrorsLen(zig_object: *ZigObject, wasm_file: *Wasm) !void { |
| 1035 | const gpa = wasm_file.base.comp.gpa; | 995 | 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; |
| 1037 | | 997 | |
| 1038 | const errors_len = wasm_file.base.comp.module.?.global_error_set.count(); | 998 | const errors_len = wasm_file.base.comp.module.?.global_error_set.count(); |
| 1039 | // overwrite existing atom if it already exists (maybe the error set has increased) | 999 | // overwrite existing atom if it already exists (maybe the error set has increased) |
| 1040 | // if not, allcoate a new atom. | 1000 | // 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: { |
| 1042 | const atom = wasm_file.getAtomPtr(index); | 1002 | const atom = wasm_file.getAtomPtr(index); |
| 1043 | if (atom.next) |next_atom_index| { | 1003 | atom.prev = null; |
| 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 | } | | |
| 1053 | atom.deinit(gpa); | 1004 | atom.deinit(gpa); |
| 1054 | break :blk index; | 1005 | break :blk index; |
| 1055 | } else new_atom: { | 1006 | } else idx: { |
| 1056 | const atom_index: Atom.Index = @intCast(wasm_file.managed_atoms.items.len); | 1007 | // We found a call to __zig_errors_len so make the symbol a local symbol |
| 1057 | try wasm_file.symbol_atom.put(gpa, loc, atom_index); | 1008 | // and define it, so the final binary or resulting object file will not attempt |
| 1058 | try wasm_file.managed_atoms.append(gpa, undefined); | 1009 | // to resolve it. |
| 1059 | break :new_atom atom_index; | 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); |
| 1060 | }; | 1017 | }; |
| | 1018 | |
| 1061 | const atom = wasm_file.getAtomPtr(atom_index); | 1019 | const atom = wasm_file.getAtomPtr(atom_index); |
| 1062 | atom.* = Atom.empty; | 1020 | atom.code.clearRetainingCapacity(); |
| 1063 | atom.sym_index = loc.index; | 1021 | atom.sym_index = sym_index; |
| 1064 | atom.size = 2; | 1022 | atom.size = 2; |
| | 1023 | atom.alignment = .@"2"; |
| 1065 | try atom.code.writer(gpa).writeInt(u16, @intCast(errors_len), .little); | 1024 | try atom.code.writer(gpa).writeInt(u16, @intCast(errors_len), .little); |
| | 1025 | } |
| 1066 | | 1026 | |
| 1067 | // try wasm.parseAtom(atom_index, .{ .data = .read_only }); | 1027 | fn 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); |
| 1068 | } | 1030 | } |
| 1069 | | 1031 | |
| 1070 | /// Initializes symbols and atoms for the debug sections | 1032 | /// Initializes symbols and atoms for the debug sections |
| ... | @@ -1232,6 +1194,11 @@ fn appendFunction(zig_object: *ZigObject, gpa: std.mem.Allocator, func: std.wasm | ... | @@ -1232,6 +1194,11 @@ fn appendFunction(zig_object: *ZigObject, gpa: std.mem.Allocator, func: std.wasm |
| 1232 | return index; | 1194 | return index; |
| 1233 | } | 1195 | } |
| 1234 | | 1196 | |
| | 1197 | pub 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 | |
| 1235 | const build_options = @import("build_options"); | 1202 | const build_options = @import("build_options"); |
| 1236 | const builtin = @import("builtin"); | 1203 | const builtin = @import("builtin"); |
| 1237 | const codegen = @import("../../codegen.zig"); | 1204 | const codegen = @import("../../codegen.zig"); |