aboutsummaryrefslogtreecommitdiff
path: root/src/link/Wasm/ZigObject.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2024-11-01 02:16:36 -0700
committerGitHub <noreply@github.com>2024-11-01 02:16:36 -0700
commitd30e287543ff525cf62ecf7f4cfcd20fee23a8ee (patch)
tree0b83c082e5505451b342d479ee485d5580f41c48 /src/link/Wasm/ZigObject.zig
parenta99449d8bebfe551ae4bf91fefd11ec4512cf417 (diff)
parente501cf51a01f6f0ae4326180cdd4274588ceed1d (diff)
downloadzig-d30e287543ff525cf62ecf7f4cfcd20fee23a8ee.tar.gz
zig-d30e287543ff525cf62ecf7f4cfcd20fee23a8ee.zip
Merge pull request #21874 from ziglang/wasm-linker
link.File.Wasm: unify the string tables
Diffstat (limited to 'src/link/Wasm/ZigObject.zig')
-rw-r--r--src/link/Wasm/ZigObject.zig82
1 files changed, 32 insertions, 50 deletions
diff --git a/src/link/Wasm/ZigObject.zig b/src/link/Wasm/ZigObject.zig
index 437c6479b3..d82329dda6 100644
--- a/src/link/Wasm/ZigObject.zig
+++ b/src/link/Wasm/ZigObject.zig
@@ -23,16 +23,14 @@ globals: std.ArrayListUnmanaged(std.wasm.Global) = .empty,
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 offset to their index into the `symbols` list.
-global_syms: std.AutoHashMapUnmanaged(u32, Symbol.Index) = .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,
-/// File encapsulated string table, used to deduplicate strings within the generated file.
-string_table: StringTable = .{},
/// 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.
@@ -88,13 +86,9 @@ const NavInfo = struct {
atom: Atom.Index = .null,
exports: std.ArrayListUnmanaged(Symbol.Index) = .empty,
- fn @"export"(ni: NavInfo, zig_object: *const ZigObject, name: []const u8) ?Symbol.Index {
+ fn @"export"(ni: NavInfo, zo: *const ZigObject, name: Wasm.String) ?Symbol.Index {
for (ni.exports.items) |sym_index| {
- const sym_name_index = zig_object.symbol(sym_index).name;
- const sym_name = zig_object.string_table.getAssumeExists(sym_name_index);
- if (std.mem.eql(u8, name, sym_name)) {
- return sym_index;
- }
+ if (zo.symbol(sym_index).name == name) return sym_index;
}
return null;
}
@@ -126,14 +120,14 @@ pub fn init(zig_object: *ZigObject, wasm: *Wasm) !void {
fn createStackPointer(zig_object: *ZigObject, wasm: *Wasm) !void {
const gpa = wasm.base.comp.gpa;
- const sym_index = try zig_object.getGlobalSymbol(gpa, "__stack_pointer");
+ 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 = try zig_object.string_table.insert(gpa, wasm.host_name),
+ .module_name = wasm.host_name,
.kind = .{ .global = .{ .valtype = if (is_wasm32) .i32 else .i64, .mutable = true } },
});
zig_object.imported_globals_count += 1;
@@ -174,7 +168,7 @@ pub fn deinit(zig_object: *ZigObject, wasm: *Wasm) void {
atom.deinit(gpa);
}
}
- if (zig_object.findGlobalSymbol("__zig_errors_len")) |sym_index| {
+ 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);
}
@@ -206,7 +200,6 @@ pub fn deinit(zig_object: *ZigObject, wasm: *Wasm) void {
zig_object.segment_info.deinit(gpa);
zig_object.segment_free_list.deinit(gpa);
- zig_object.string_table.deinit(gpa);
if (zig_object.dwarf) |*dwarf| {
dwarf.deinit();
}
@@ -219,7 +212,7 @@ pub fn deinit(zig_object: *ZigObject, wasm: *Wasm) void {
pub fn allocateSymbol(zig_object: *ZigObject, gpa: std.mem.Allocator) !Symbol.Index {
try zig_object.symbols.ensureUnusedCapacity(gpa, 1);
const sym: Symbol = .{
- .name = std.math.maxInt(u32), // will be set after updateDecl as well as during atom creation for decls
+ .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
@@ -345,7 +338,7 @@ fn finishUpdateNav(
const atom_index = nav_info.atom;
const atom = wasm.getAtomPtr(atom_index);
const sym = zig_object.symbol(atom.sym_index);
- sym.name = try zig_object.string_table.insert(gpa, nav.fqn.toSlice(ip));
+ sym.name = try wasm.internString(nav.fqn.toSlice(ip));
try atom.code.appendSlice(gpa, code);
atom.size = @intCast(code.len);
@@ -432,7 +425,7 @@ pub fn getOrCreateAtomForNav(
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 zig_object.string_table.insert(gpa, nav.fqn.toSlice(ip));
+ sym.name = try wasm.internString(nav.fqn.toSlice(ip));
}
return gop.value_ptr.atom;
}
@@ -500,7 +493,7 @@ fn lowerConst(
const segment_name = try std.mem.concat(gpa, u8, &.{ ".rodata.", name });
errdefer gpa.free(segment_name);
zig_object.symbol(sym_index).* = .{
- .name = try zig_object.string_table.insert(gpa, name),
+ .name = try wasm.internString(name),
.flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL),
.tag = .data,
.index = try zig_object.createDataSegment(
@@ -551,11 +544,10 @@ pub fn getErrorTableSymbol(zig_object: *ZigObject, wasm: *Wasm, pt: Zcu.PerThrea
const slice_ty = Type.slice_const_u8_sentinel_0;
atom.alignment = slice_ty.abiAlignment(pt.zcu);
- const sym_name = try zig_object.string_table.insert(gpa, "__zig_err_name_table");
const segment_name = try gpa.dupe(u8, ".rodata.__zig_err_name_table");
const sym = zig_object.symbol(sym_index);
sym.* = .{
- .name = sym_name,
+ .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),
@@ -583,11 +575,10 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm: *Wasm, tid: Zcu.PerThrea
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 sym_name = try zig_object.string_table.insert(gpa, "__zig_err_names");
const segment_name = try gpa.dupe(u8, ".rodata.__zig_err_names");
const names_symbol = zig_object.symbol(names_sym_index);
names_symbol.* = .{
- .name = sym_name,
+ .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),
@@ -661,14 +652,14 @@ pub fn addOrUpdateImport(
// 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 = lib_name != null and
- !std.mem.eql(u8, lib_name.?, "c");
- const full_name = if (mangle_name) full_name: {
- break :full_name try std.fmt.allocPrint(gpa, "{s}|{s}", .{ name, lib_name.? });
- } else name;
+ 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 zig_object.string_table.insert(gpa, 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);
@@ -680,13 +671,11 @@ pub fn addOrUpdateImport(
if (type_index) |ty_index| {
const gop = try zig_object.imports.getOrPut(gpa, symbol_index);
- const module_name = if (lib_name) |l_name| l_name else wasm.host_name;
- if (!gop.found_existing) {
- zig_object.imported_functions_count += 1;
- }
+ 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 = try zig_object.string_table.insert(gpa, module_name),
- .name = try zig_object.string_table.insert(gpa, name),
+ .module_name = module_name,
+ .name = try wasm.internString(name),
.kind = .{ .function = ty_index },
};
sym.tag = .function;
@@ -699,8 +688,7 @@ pub fn addOrUpdateImport(
/// 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: []const u8) !Symbol.Index {
- const name_index = try zig_object.string_table.insert(gpa, name);
+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.*;
@@ -840,7 +828,8 @@ pub fn deleteExport(
.uav => @panic("TODO: implement Wasm linker code for exporting a constant value"),
};
const nav_info = zig_object.navs.getPtr(nav_index) orelse return;
- if (nav_info.@"export"(zig_object, name.toSlice(&zcu.intern_pool))) |sym_index| {
+ 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));
@@ -886,14 +875,13 @@ pub fn updateExports(
continue;
}
- const export_string = exp.opts.name.toSlice(ip);
- const sym_index = if (nav_info.@"export"(zig_object, export_string)) |idx| idx else index: {
+ 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 export_name = try zig_object.string_table.insert(gpa, export_string);
const sym = zig_object.symbol(sym_index);
sym.setGlobal(true);
sym.setUndefined(false);
@@ -922,7 +910,7 @@ pub fn updateExports(
if (exp.opts.visibility == .hidden) {
sym.setFlag(.WASM_SYM_VISIBILITY_HIDDEN);
}
- log.debug(" with name '{s}' - {}", .{ export_string, sym });
+ 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);
}
@@ -1014,7 +1002,7 @@ pub fn putOrGetFuncType(zig_object: *ZigObject, gpa: std.mem.Allocator, func_typ
/// 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.findGlobalSymbol("__zig_errors_len") orelse return;
+ 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)
@@ -1045,11 +1033,6 @@ fn setupErrorsLen(zig_object: *ZigObject, wasm: *Wasm) !void {
try atom.code.writer(gpa).writeInt(u16, @intCast(errors_len), .little);
}
-fn findGlobalSymbol(zig_object: *ZigObject, name: []const u8) ?Symbol.Index {
- const offset = zig_object.string_table.getOffset(name) orelse return null;
- return zig_object.global_syms.get(offset);
-}
-
/// 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
@@ -1082,7 +1065,7 @@ pub fn createDebugSectionForIndex(zig_object: *ZigObject, wasm: *Wasm, index: *?
const atom = wasm.getAtomPtr(atom_index);
zig_object.symbols.items[sym_index] = .{
.tag = .section,
- .name = try zig_object.string_table.put(gpa, name),
+ .name = try wasm.internString(name),
.index = 0,
.flags = @intFromEnum(Symbol.Flag.WASM_SYM_BINDING_LOCAL),
};
@@ -1197,7 +1180,7 @@ pub fn createFunction(
const sym_index = try zig_object.allocateSymbol(gpa);
const sym = zig_object.symbol(sym_index);
sym.tag = .function;
- sym.name = try zig_object.string_table.insert(gpa, symbol_name);
+ 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 });
@@ -1244,7 +1227,6 @@ const Dwarf = @import("../Dwarf.zig");
const InternPool = @import("../../InternPool.zig");
const Liveness = @import("../../Liveness.zig");
const Zcu = @import("../../Zcu.zig");
-const StringTable = @import("../StringTable.zig");
const Symbol = @import("Symbol.zig");
const Type = @import("../../Type.zig");
const Value = @import("../../Value.zig");