Commit ced958e8a8

Luuk de Gram <luuk@degram.dev>
2022-02-16 23:34:21
wasm-linker: Simplify symbol names
No longer duplicate the symbol name and instead take the pointer from the decl itself. Also fix 32bit build
1 parent 4ebe8a5
Changed files (2)
src
src/link/Wasm/Object.zig
@@ -301,7 +301,7 @@ fn Parser(comptime ReaderType: type) type {
 
                         if (std.mem.eql(u8, name, "linking")) {
                             is_object_file.* = true;
-                            try self.parseMetadata(gpa, reader.context.bytes_left);
+                            try self.parseMetadata(gpa, @intCast(usize, reader.context.bytes_left));
                         } else if (std.mem.startsWith(u8, name, "reloc")) {
                             try self.parseRelocations(gpa);
                         } else if (std.mem.eql(u8, name, "target_features")) {
src/link/Wasm.zig
@@ -173,7 +173,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
     };
     const symbol = try wasm_bin.symbols.addOne(allocator);
     symbol.* = .{
-        .name = try allocator.dupeZ(u8, "__stack_pointer"),
+        .name = "__stack_pointer",
         .tag = .global,
         .flags = 0,
         .index = 0,
@@ -298,6 +298,10 @@ pub fn deinit(self: *Wasm) void {
     var decl_it = self.decls.keyIterator();
     while (decl_it.next()) |decl_ptr| {
         const decl = decl_ptr.*;
+        const atom: *Atom = &decl.link.wasm;
+        for (atom.locals.items) |local| {
+            gpa.free(mem.sliceTo(self.symbols.items[local.sym_index].name, 0));
+        }
         decl.link.wasm.deinit(gpa);
     }
 
@@ -312,12 +316,6 @@ pub fn deinit(self: *Wasm) void {
         object.deinit(gpa);
     }
 
-    for (self.symbols.items) |symbol| {
-        if (symbol.tag != .dead) {
-            gpa.free(mem.sliceTo(symbol.name, 0));
-        }
-    }
-
     self.decls.deinit(gpa);
     self.symbols.deinit(gpa);
     self.symbols_free_list.deinit(gpa);
@@ -463,7 +461,7 @@ fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, code: []const u8) !void {
     atom.size = @intCast(u32, code.len);
     atom.alignment = decl.ty.abiAlignment(self.base.options.target);
     const symbol = &self.symbols.items[atom.sym_index];
-    symbol.name = try self.base.allocator.dupeZ(u8, std.mem.sliceTo(decl.name, 0));
+    symbol.name = decl.name;
     symbol.setFlag(.WASM_SYM_BINDING_LOCAL);
     try atom.code.appendSlice(self.base.allocator, code);
 }
@@ -565,13 +563,13 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void {
     const atom = &decl.link.wasm;
     self.symbols_free_list.append(self.base.allocator, atom.sym_index) catch {};
     _ = self.decls.remove(decl);
-    self.symbols.items[atom.sym_index].tag = .dead; // to ensure it does not end in the names section
+    self.symbols.items[atom.sym_index].tag = .dead;
     for (atom.locals.items) |local_atom| {
-        self.symbols.items[local_atom.sym_index].tag = .dead; // also for any local symbol
-        // self.base.allocator.free(mem.sliceTo(self.symbols.items[local_atom.sym_index].name, 0));
+        const local_symbol = &self.symbols.items[local_atom.sym_index];
+        local_symbol.tag = .dead; // also for any local symbol
+        self.base.allocator.free(mem.sliceTo(local_symbol.name, 0));
         self.symbols_free_list.append(self.base.allocator, local_atom.sym_index) catch {};
     }
-    // self.base.allocator.free(mem.sliceTo(self.symbols.items[atom.sym_index].name, 0));
 
     if (decl.isExtern()) {
         assert(self.imports.remove(.{ .file = null, .index = atom.sym_index }));
@@ -600,11 +598,14 @@ fn mapFunctionTable(self: *Wasm) void {
 fn addOrUpdateImport(self: *Wasm, decl: *Module.Decl) !void {
     const symbol_index = decl.link.wasm.sym_index;
     const symbol: *Symbol = &self.symbols.items[symbol_index];
-    const decl_name = mem.sliceTo(decl.name, 0);
-    symbol.name = try self.base.allocator.dupeZ(u8, decl_name);
+    symbol.name = decl.name;
     symbol.setUndefined(true);
     // also add it as a global so it can be resolved
-    try self.globals.putNoClobber(self.base.allocator, decl_name, .{ .file = null, .index = symbol_index });
+    try self.globals.putNoClobber(
+        self.base.allocator,
+        mem.sliceTo(symbol.name, 0),
+        .{ .file = null, .index = symbol_index },
+    );
     switch (decl.ty.zigTypeTag()) {
         .Fn => {
             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 {
             if (!gop.found_existing) {
                 gop.value_ptr.* = .{
                     .module_name = module_name,
-                    .name = std.mem.span(symbol.name),
+                    .name = mem.sliceTo(symbol.name, 0),
                     .kind = .{ .function = decl.fn_link.wasm.type_index },
                 };
             }