Commit 1aa0f8aa2f

Jakub Konka <kubkon@jakubkonka.com>
2023-02-01 17:39:07
link: fix pointer invalidation issues in Elf, MachO and Coff
1 parent e0f3975
Changed files (3)
src/link/Coff.zig
@@ -1035,7 +1035,6 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
     const unnamed_consts = gop.value_ptr;
 
     const atom_index = try self.createAtom();
-    const atom = self.getAtomPtr(atom_index);
 
     const sym_name = blk: {
         const decl_name = try decl.getFullyQualifiedName(mod);
@@ -1045,11 +1044,15 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
         break :blk try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
     };
     defer gpa.free(sym_name);
-    try self.setSymbolName(atom.getSymbolPtr(self), sym_name);
-    atom.getSymbolPtr(self).section_number = @intToEnum(coff.SectionNumber, self.rdata_section_index.? + 1);
+    {
+        const atom = self.getAtom(atom_index);
+        const sym = atom.getSymbolPtr(self);
+        try self.setSymbolName(sym, sym_name);
+        sym.section_number = @intToEnum(coff.SectionNumber, self.rdata_section_index.? + 1);
+    }
 
     const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), tv, &code_buffer, .none, .{
-        .parent_atom_index = atom.getSymbolIndex().?,
+        .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
     });
     const code = switch (res) {
         .ok => code_buffer.items,
@@ -1062,6 +1065,7 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
     };
 
     const required_alignment = tv.ty.abiAlignment(self.base.options.target);
+    const atom = self.getAtomPtr(atom_index);
     atom.alignment = required_alignment;
     atom.size = @intCast(u32, code.len);
     atom.getSymbolPtr(self).value = try self.allocateAtom(atom_index, atom.size, atom.alignment);
src/link/Elf.zig
@@ -2600,12 +2600,11 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
     const name = self.shstrtab.get(name_str_index).?;
 
     const atom_index = try self.createAtom();
-    const atom = self.getAtomPtr(atom_index);
 
     const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .{
         .none = {},
     }, .{
-        .parent_atom_index = atom.getSymbolIndex().?,
+        .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
     });
     const code = switch (res) {
         .ok => code_buffer.items,
@@ -2620,7 +2619,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
     const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
     const shdr_index = self.rodata_section_index.?;
     const phdr_index = self.sections.items(.phdr_index)[shdr_index];
-    const local_sym = atom.getSymbolPtr(self);
+    const local_sym = self.getAtom(atom_index).getSymbolPtr(self);
     local_sym.st_name = name_str_index;
     local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT;
     local_sym.st_other = 0;
@@ -2631,14 +2630,14 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
 
     log.debug("allocated text block for {s} at 0x{x}", .{ name, local_sym.st_value });
 
-    try self.writeSymbol(atom.getSymbolIndex().?);
+    try self.writeSymbol(self.getAtom(atom_index).getSymbolIndex().?);
     try unnamed_consts.append(gpa, atom_index);
 
     const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;
     const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
     try self.base.file.?.pwriteAll(code, file_offset);
 
-    return atom.getSymbolIndex().?;
+    return self.getAtom(atom_index).getSymbolIndex().?;
 }
 
 pub fn updateDeclExports(
src/link/MachO.zig
@@ -2079,10 +2079,9 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
     log.debug("allocating symbol indexes for {?s}", .{name});
 
     const atom_index = try self.createAtom();
-    const atom = self.getAtomPtr(atom_index);
 
     const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .none, .{
-        .parent_atom_index = atom.getSymbolIndex().?,
+        .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
     });
     const code = switch (res) {
         .ok => code_buffer.items,
@@ -2095,6 +2094,7 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
     };
 
     const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
+    const atom = self.getAtomPtr(atom_index);
     atom.size = code.len;
     atom.alignment = required_alignment;
     // TODO: work out logic for disambiguating functions from function pointers