authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-02-16 23:34:21+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-02-17 18:11:48+01:00
logced958e8a800dc099d81e26f34d99f6c977febf6
tree09fd8e72bb39bdf0899179cb02b688edab00cf55
parent4ebe8a53cab2c218657090f984b8ba10ef06b23a

wasm-linker: Simplify symbol names

No longer duplicate the symbol name and instead take the pointer from the decl itself. Also fix 32bit build

2 files changed, 18 insertions(+), 17 deletions(-)

src/link/Wasm.zig+17-16
...@@ -173,7 +173,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option...@@ -173,7 +173,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
173 };173 };
174 const symbol = try wasm_bin.symbols.addOne(allocator);174 const symbol = try wasm_bin.symbols.addOne(allocator);
175 symbol.* = .{175 symbol.* = .{
176 .name = try allocator.dupeZ(u8, "__stack_pointer"),176 .name = "__stack_pointer",
177 .tag = .global,177 .tag = .global,
178 .flags = 0,178 .flags = 0,
179 .index = 0,179 .index = 0,
...@@ -298,6 +298,10 @@ pub fn deinit(self: *Wasm) void {...@@ -298,6 +298,10 @@ pub fn deinit(self: *Wasm) void {
298 var decl_it = self.decls.keyIterator();298 var decl_it = self.decls.keyIterator();
299 while (decl_it.next()) |decl_ptr| {299 while (decl_it.next()) |decl_ptr| {
300 const decl = decl_ptr.*;300 const decl = decl_ptr.*;
301 const atom: *Atom = &decl.link.wasm;
302 for (atom.locals.items) |local| {
303 gpa.free(mem.sliceTo(self.symbols.items[local.sym_index].name, 0));
304 }
301 decl.link.wasm.deinit(gpa);305 decl.link.wasm.deinit(gpa);
302 }306 }
303307
...@@ -312,12 +316,6 @@ pub fn deinit(self: *Wasm) void {...@@ -312,12 +316,6 @@ pub fn deinit(self: *Wasm) void {
312 object.deinit(gpa);316 object.deinit(gpa);
313 }317 }
314318
315 for (self.symbols.items) |symbol| {
316 if (symbol.tag != .dead) {
317 gpa.free(mem.sliceTo(symbol.name, 0));
318 }
319 }
320
321 self.decls.deinit(gpa);319 self.decls.deinit(gpa);
322 self.symbols.deinit(gpa);320 self.symbols.deinit(gpa);
323 self.symbols_free_list.deinit(gpa);321 self.symbols_free_list.deinit(gpa);
...@@ -463,7 +461,7 @@ fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, code: []const u8) !void {...@@ -463,7 +461,7 @@ fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, code: []const u8) !void {
463 atom.size = @intCast(u32, code.len);461 atom.size = @intCast(u32, code.len);
464 atom.alignment = decl.ty.abiAlignment(self.base.options.target);462 atom.alignment = decl.ty.abiAlignment(self.base.options.target);
465 const symbol = &self.symbols.items[atom.sym_index];463 const symbol = &self.symbols.items[atom.sym_index];
466 symbol.name = try self.base.allocator.dupeZ(u8, std.mem.sliceTo(decl.name, 0));464 symbol.name = decl.name;
467 symbol.setFlag(.WASM_SYM_BINDING_LOCAL);465 symbol.setFlag(.WASM_SYM_BINDING_LOCAL);
468 try atom.code.appendSlice(self.base.allocator, code);466 try atom.code.appendSlice(self.base.allocator, code);
469}467}
...@@ -565,13 +563,13 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void {...@@ -565,13 +563,13 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void {
565 const atom = &decl.link.wasm;563 const atom = &decl.link.wasm;
566 self.symbols_free_list.append(self.base.allocator, atom.sym_index) catch {};564 self.symbols_free_list.append(self.base.allocator, atom.sym_index) catch {};
567 _ = self.decls.remove(decl);565 _ = self.decls.remove(decl);
568 self.symbols.items[atom.sym_index].tag = .dead; // to ensure it does not end in the names section566 self.symbols.items[atom.sym_index].tag = .dead;
569 for (atom.locals.items) |local_atom| {567 for (atom.locals.items) |local_atom| {
570 self.symbols.items[local_atom.sym_index].tag = .dead; // also for any local symbol568 const local_symbol = &self.symbols.items[local_atom.sym_index];
571 // self.base.allocator.free(mem.sliceTo(self.symbols.items[local_atom.sym_index].name, 0));569 local_symbol.tag = .dead; // also for any local symbol
570 self.base.allocator.free(mem.sliceTo(local_symbol.name, 0));
572 self.symbols_free_list.append(self.base.allocator, local_atom.sym_index) catch {};571 self.symbols_free_list.append(self.base.allocator, local_atom.sym_index) catch {};
573 }572 }
574 // self.base.allocator.free(mem.sliceTo(self.symbols.items[atom.sym_index].name, 0));
575573
576 if (decl.isExtern()) {574 if (decl.isExtern()) {
577 assert(self.imports.remove(.{ .file = null, .index = atom.sym_index }));575 assert(self.imports.remove(.{ .file = null, .index = atom.sym_index }));
...@@ -600,11 +598,14 @@ fn mapFunctionTable(self: *Wasm) void {...@@ -600,11 +598,14 @@ fn mapFunctionTable(self: *Wasm) void {
600fn addOrUpdateImport(self: *Wasm, decl: *Module.Decl) !void {598fn addOrUpdateImport(self: *Wasm, decl: *Module.Decl) !void {
601 const symbol_index = decl.link.wasm.sym_index;599 const symbol_index = decl.link.wasm.sym_index;
602 const symbol: *Symbol = &self.symbols.items[symbol_index];600 const symbol: *Symbol = &self.symbols.items[symbol_index];
603 const decl_name = mem.sliceTo(decl.name, 0);601 symbol.name = decl.name;
604 symbol.name = try self.base.allocator.dupeZ(u8, decl_name);
605 symbol.setUndefined(true);602 symbol.setUndefined(true);
606 // also add it as a global so it can be resolved603 // also add it as a global so it can be resolved
607 try self.globals.putNoClobber(self.base.allocator, decl_name, .{ .file = null, .index = symbol_index });604 try self.globals.putNoClobber(
605 self.base.allocator,
606 mem.sliceTo(symbol.name, 0),
607 .{ .file = null, .index = symbol_index },
608 );
608 switch (decl.ty.zigTypeTag()) {609 switch (decl.ty.zigTypeTag()) {
609 .Fn => {610 .Fn => {
610 const gop = try self.imports.getOrPut(self.base.allocator, .{ .index = symbol_index, .file = null });611 const gop = try self.imports.getOrPut(self.base.allocator, .{ .index = symbol_index, .file = null });
...@@ -614,7 +615,7 @@ fn addOrUpdateImport(self: *Wasm, decl: *Module.Decl) !void {...@@ -614,7 +615,7 @@ fn addOrUpdateImport(self: *Wasm, decl: *Module.Decl) !void {
614 if (!gop.found_existing) {615 if (!gop.found_existing) {
615 gop.value_ptr.* = .{616 gop.value_ptr.* = .{
616 .module_name = module_name,617 .module_name = module_name,
617 .name = std.mem.span(symbol.name),618 .name = mem.sliceTo(symbol.name, 0),
618 .kind = .{ .function = decl.fn_link.wasm.type_index },619 .kind = .{ .function = decl.fn_link.wasm.type_index },
619 };620 };
620 }621 }
src/link/Wasm/Object.zig+1-1
...@@ -301,7 +301,7 @@ fn Parser(comptime ReaderType: type) type {...@@ -301,7 +301,7 @@ fn Parser(comptime ReaderType: type) type {
301301
302 if (std.mem.eql(u8, name, "linking")) {302 if (std.mem.eql(u8, name, "linking")) {
303 is_object_file.* = true;303 is_object_file.* = true;
304 try self.parseMetadata(gpa, reader.context.bytes_left);304 try self.parseMetadata(gpa, @intCast(usize, reader.context.bytes_left));
305 } else if (std.mem.startsWith(u8, name, "reloc")) {305 } else if (std.mem.startsWith(u8, name, "reloc")) {
306 try self.parseRelocations(gpa);306 try self.parseRelocations(gpa);
307 } else if (std.mem.eql(u8, name, "target_features")) {307 } else if (std.mem.eql(u8, name, "target_features")) {