| ... | ... | @@ -98,6 +98,16 @@ const DeclInfo = struct { |
| 98 | 98 | fn appendExport(di: *DeclInfo, gpa: std.mem.Allocator, sym_index: u32) !void { |
| 99 | 99 | return di.exports.append(gpa, sym_index); |
| 100 | 100 | } |
| 101 | |
| 102 | fn deleteExport(di: *DeclInfo, sym_index: u32) void { |
| 103 | for (di.exports.items, 0..) |idx, index| { |
| 104 | if (idx == sym_index) { |
| 105 | _ = di.exports.swapRemove(index); |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | unreachable; // invalid sym_index |
| 110 | } |
| 101 | 111 | }; |
| 102 | 112 | |
| 103 | 113 | /// Initializes the `ZigObject` with initial symbols. |
| ... | ... | @@ -800,12 +810,23 @@ pub fn deleteDeclExport( |
| 800 | 810 | zig_object: *ZigObject, |
| 801 | 811 | wasm_file: *Wasm, |
| 802 | 812 | decl_index: InternPool.DeclIndex, |
| 813 | name: InternPool.NullTerminatedString, |
| 803 | 814 | ) void { |
| 804 | | const decl_info = zig_object.decls_map.get(decl_index) orelse return; |
| 805 | | const sym_index = wasm_file.getAtom(decl_info.atom).sym_index; |
| 806 | | const loc: Wasm.SymbolLoc = .{ .file = zig_object.index, .index = sym_index }; |
| 807 | | const sym = loc.getSymbol(wasm_file); |
| 808 | | std.debug.assert(zig_object.global_syms.remove(sym.name)); |
| 815 | const mod = wasm_file.base.comp.module.?; |
| 816 | const decl_info = zig_object.decls_map.getPtr(decl_index) orelse return; |
| 817 | const export_name = mod.intern_pool.stringToSlice(name); |
| 818 | if (decl_info.@"export"(zig_object, export_name)) |sym_index| { |
| 819 | const sym = zig_object.symbol(sym_index); |
| 820 | decl_info.deleteExport(sym_index); |
| 821 | std.debug.assert(zig_object.global_syms.remove(sym.name)); |
| 822 | std.debug.assert(wasm_file.symbol_atom.remove(.{ .file = zig_object.index, .index = sym_index })); |
| 823 | zig_object.symbols_free_list.append(wasm_file.base.comp.gpa, sym_index) catch {}; |
| 824 | |
| 825 | if (sym.tag == .function) { |
| 826 | std.debug.assert(zig_object.functions.remove(sym_index)); |
| 827 | } |
| 828 | sym.tag = .dead; |
| 829 | } |
| 809 | 830 | } |
| 810 | 831 | |
| 811 | 832 | pub fn updateExports( |
| ... | ... | @@ -846,6 +867,17 @@ pub fn updateExports( |
| 846 | 867 | else index: { |
| 847 | 868 | const sym_index = try zig_object.allocateSymbol(gpa); |
| 848 | 869 | try decl_info.appendExport(gpa, sym_index); |
| 870 | |
| 871 | // For functions, we also need to put the alias in the function section. |
| 872 | // We simply copy the aliased function. |
| 873 | // The final linakge will deduplicate these functions. |
| 874 | if (decl.ty.zigTypeTag(mod) == .Fn) { |
| 875 | try zig_object.functions.putNoClobber( |
| 876 | gpa, |
| 877 | sym_index, |
| 878 | zig_object.functions.get(atom.sym_index).?, |
| 879 | ); |
| 880 | } |
| 849 | 881 | break :index sym_index; |
| 850 | 882 | }; |
| 851 | 883 | |
| ... | ... | @@ -879,6 +911,7 @@ pub fn updateExports( |
| 879 | 911 | sym.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); |
| 880 | 912 | } |
| 881 | 913 | try zig_object.global_syms.put(gpa, export_name, sym_index); |
| 914 | try wasm_file.symbol_atom.put(gpa, .{ .file = zig_object.index, .index = sym_index }, atom_index); |
| 882 | 915 | } |
| 883 | 916 | } |
| 884 | 917 | |
| ... | ... | @@ -1145,8 +1178,8 @@ pub fn storeDeclType(zig_object: *ZigObject, gpa: std.mem.Allocator, decl_index: |
| 1145 | 1178 | pub fn parseSymbolIntoAtom(zig_object: *ZigObject, wasm_file: *Wasm, index: u32) !Atom.Index { |
| 1146 | 1179 | const gpa = wasm_file.base.comp.gpa; |
| 1147 | 1180 | const loc: Wasm.SymbolLoc = .{ .file = zig_object.index, .index = index }; |
| 1148 | | const final_index = try wasm_file.getMatchingSegment(zig_object.index, index); |
| 1149 | 1181 | const atom_index = wasm_file.symbol_atom.get(loc).?; |
| 1182 | const final_index = try wasm_file.getMatchingSegment(zig_object.index, index); |
| 1150 | 1183 | try wasm_file.appendAtomAtIndex(final_index, atom_index); |
| 1151 | 1184 | const atom = wasm_file.getAtom(atom_index); |
| 1152 | 1185 | for (atom.relocs.items) |reloc| { |