diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2023-02-01 17:39:07 +0100 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2023-02-01 17:46:57 +0100 |
| commit | 1aa0f8aa2f382fb56639ea6833a62c4b8b031247 (patch) | |
| tree | 575223642adddd9ca12a89a2aca76eb309109766 /src/link | |
| parent | e0f3975fc8a7afd8a613802321fd46e64d8970d5 (diff) | |
| download | zig-1aa0f8aa2f382fb56639ea6833a62c4b8b031247.tar.gz zig-1aa0f8aa2f382fb56639ea6833a62c4b8b031247.zip | |
link: fix pointer invalidation issues in Elf, MachO and Coff
Diffstat (limited to 'src/link')
| -rw-r--r-- | src/link/Coff.zig | 12 | ||||
| -rw-r--r-- | src/link/Elf.zig | 9 | ||||
| -rw-r--r-- | src/link/MachO.zig | 4 |
3 files changed, 14 insertions, 11 deletions
diff --git a/src/link/Coff.zig b/src/link/Coff.zig index f563a617c7..2922e783e1 100644 --- a/src/link/Coff.zig +++ b/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); diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 3e0c6d2b57..45952da6c0 100644 --- a/src/link/Elf.zig +++ b/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( diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 22eb58775b..24ef275c5b 100644 --- a/src/link/MachO.zig +++ b/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 |
