authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-02-01 17:39:07+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-02-01 17:46:57+01:00
log1aa0f8aa2f382fb56639ea6833a62c4b8b031247
tree575223642adddd9ca12a89a2aca76eb309109766
parente0f3975fc8a7afd8a613802321fd46e64d8970d5

link: fix pointer invalidation issues in Elf, MachO and Coff


3 files changed, 14 insertions(+), 11 deletions(-)

src/link/Coff.zig+8-4
......@@ -1035,7 +1035,6 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
10351035 const unnamed_consts = gop.value_ptr;
10361036
10371037 const atom_index = try self.createAtom();
1038 const atom = self.getAtomPtr(atom_index);
10391038
10401039 const sym_name = blk: {
10411040 const decl_name = try decl.getFullyQualifiedName(mod);
......@@ -1045,11 +1044,15 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
10451044 break :blk try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
10461045 };
10471046 defer gpa.free(sym_name);
1048 try self.setSymbolName(atom.getSymbolPtr(self), sym_name);
1049 atom.getSymbolPtr(self).section_number = @intToEnum(coff.SectionNumber, self.rdata_section_index.? + 1);
1047 {
1048 const atom = self.getAtom(atom_index);
1049 const sym = atom.getSymbolPtr(self);
1050 try self.setSymbolName(sym, sym_name);
1051 sym.section_number = @intToEnum(coff.SectionNumber, self.rdata_section_index.? + 1);
1052 }
10501053
10511054 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), tv, &code_buffer, .none, .{
1052 .parent_atom_index = atom.getSymbolIndex().?,
1055 .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
10531056 });
10541057 const code = switch (res) {
10551058 .ok => code_buffer.items,
......@@ -1062,6 +1065,7 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
10621065 };
10631066
10641067 const required_alignment = tv.ty.abiAlignment(self.base.options.target);
1068 const atom = self.getAtomPtr(atom_index);
10651069 atom.alignment = required_alignment;
10661070 atom.size = @intCast(u32, code.len);
10671071 atom.getSymbolPtr(self).value = try self.allocateAtom(atom_index, atom.size, atom.alignment);
src/link/Elf.zig+4-5
......@@ -2600,12 +2600,11 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
26002600 const name = self.shstrtab.get(name_str_index).?;
26012601
26022602 const atom_index = try self.createAtom();
2603 const atom = self.getAtomPtr(atom_index);
26042603
26052604 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .{
26062605 .none = {},
26072606 }, .{
2608 .parent_atom_index = atom.getSymbolIndex().?,
2607 .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
26092608 });
26102609 const code = switch (res) {
26112610 .ok => code_buffer.items,
......@@ -2620,7 +2619,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
26202619 const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
26212620 const shdr_index = self.rodata_section_index.?;
26222621 const phdr_index = self.sections.items(.phdr_index)[shdr_index];
2623 const local_sym = atom.getSymbolPtr(self);
2622 const local_sym = self.getAtom(atom_index).getSymbolPtr(self);
26242623 local_sym.st_name = name_str_index;
26252624 local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT;
26262625 local_sym.st_other = 0;
......@@ -2631,14 +2630,14 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
26312630
26322631 log.debug("allocated text block for {s} at 0x{x}", .{ name, local_sym.st_value });
26332632
2634 try self.writeSymbol(atom.getSymbolIndex().?);
2633 try self.writeSymbol(self.getAtom(atom_index).getSymbolIndex().?);
26352634 try unnamed_consts.append(gpa, atom_index);
26362635
26372636 const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;
26382637 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
26392638 try self.base.file.?.pwriteAll(code, file_offset);
26402639
2641 return atom.getSymbolIndex().?;
2640 return self.getAtom(atom_index).getSymbolIndex().?;
26422641}
26432642
26442643pub fn updateDeclExports(
src/link/MachO.zig+2-2
......@@ -2079,10 +2079,9 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
20792079 log.debug("allocating symbol indexes for {?s}", .{name});
20802080
20812081 const atom_index = try self.createAtom();
2082 const atom = self.getAtomPtr(atom_index);
20832082
20842083 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .none, .{
2085 .parent_atom_index = atom.getSymbolIndex().?,
2084 .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
20862085 });
20872086 const code = switch (res) {
20882087 .ok => code_buffer.items,
......@@ -2095,6 +2094,7 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
20952094 };
20962095
20972096 const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
2097 const atom = self.getAtomPtr(atom_index);
20982098 atom.size = code.len;
20992099 atom.alignment = required_alignment;
21002100 // TODO: work out logic for disambiguating functions from function pointers