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...@@ -1035,7 +1035,6 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
1035 const unnamed_consts = gop.value_ptr;1035 const unnamed_consts = gop.value_ptr;
10361036
1037 const atom_index = try self.createAtom();1037 const atom_index = try self.createAtom();
1038 const atom = self.getAtomPtr(atom_index);
10391038
1040 const sym_name = blk: {1039 const sym_name = blk: {
1041 const decl_name = try decl.getFullyQualifiedName(mod);1040 const decl_name = try decl.getFullyQualifiedName(mod);
...@@ -1045,11 +1044,15 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In...@@ -1045,11 +1044,15 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
1045 break :blk try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });1044 break :blk try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
1046 };1045 };
1047 defer gpa.free(sym_name);1046 defer gpa.free(sym_name);
1048 try self.setSymbolName(atom.getSymbolPtr(self), sym_name);1047 {
1049 atom.getSymbolPtr(self).section_number = @intToEnum(coff.SectionNumber, self.rdata_section_index.? + 1);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
1051 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), tv, &code_buffer, .none, .{1054 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().?,
1053 });1056 });
1054 const code = switch (res) {1057 const code = switch (res) {
1055 .ok => code_buffer.items,1058 .ok => code_buffer.items,
...@@ -1062,6 +1065,7 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In...@@ -1062,6 +1065,7 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
1062 };1065 };
10631066
1064 const required_alignment = tv.ty.abiAlignment(self.base.options.target);1067 const required_alignment = tv.ty.abiAlignment(self.base.options.target);
1068 const atom = self.getAtomPtr(atom_index);
1065 atom.alignment = required_alignment;1069 atom.alignment = required_alignment;
1066 atom.size = @intCast(u32, code.len);1070 atom.size = @intCast(u32, code.len);
1067 atom.getSymbolPtr(self).value = try self.allocateAtom(atom_index, atom.size, atom.alignment);1071 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...@@ -2600,12 +2600,11 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2600 const name = self.shstrtab.get(name_str_index).?;2600 const name = self.shstrtab.get(name_str_index).?;
26012601
2602 const atom_index = try self.createAtom();2602 const atom_index = try self.createAtom();
2603 const atom = self.getAtomPtr(atom_index);
26042603
2605 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .{2604 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .{
2606 .none = {},2605 .none = {},
2607 }, .{2606 }, .{
2608 .parent_atom_index = atom.getSymbolIndex().?,2607 .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
2609 });2608 });
2610 const code = switch (res) {2609 const code = switch (res) {
2611 .ok => code_buffer.items,2610 .ok => code_buffer.items,
...@@ -2620,7 +2619,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2620,7 +2619,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2620 const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);2619 const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
2621 const shdr_index = self.rodata_section_index.?;2620 const shdr_index = self.rodata_section_index.?;
2622 const phdr_index = self.sections.items(.phdr_index)[shdr_index];2621 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);
2624 local_sym.st_name = name_str_index;2623 local_sym.st_name = name_str_index;
2625 local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT;2624 local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT;
2626 local_sym.st_other = 0;2625 local_sym.st_other = 0;
...@@ -2631,14 +2630,14 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2631,14 +2630,14 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
26312630
2632 log.debug("allocated text block for {s} at 0x{x}", .{ name, local_sym.st_value });2631 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().?);
2635 try unnamed_consts.append(gpa, atom_index);2634 try unnamed_consts.append(gpa, atom_index);
26362635
2637 const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;2636 const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;
2638 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;2637 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
2639 try self.base.file.?.pwriteAll(code, file_offset);2638 try self.base.file.?.pwriteAll(code, file_offset);
26402639
2641 return atom.getSymbolIndex().?;2640 return self.getAtom(atom_index).getSymbolIndex().?;
2642}2641}
26432642
2644pub fn updateDeclExports(2643pub fn updateDeclExports(
src/link/MachO.zig+2-2
...@@ -2079,10 +2079,9 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu...@@ -2079,10 +2079,9 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
2079 log.debug("allocating symbol indexes for {?s}", .{name});2079 log.debug("allocating symbol indexes for {?s}", .{name});
20802080
2081 const atom_index = try self.createAtom();2081 const atom_index = try self.createAtom();
2082 const atom = self.getAtomPtr(atom_index);
20832082
2084 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .none, .{2083 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().?,
2086 });2085 });
2087 const code = switch (res) {2086 const code = switch (res) {
2088 .ok => code_buffer.items,2087 .ok => code_buffer.items,
...@@ -2095,6 +2094,7 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu...@@ -2095,6 +2094,7 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
2095 };2094 };
20962095
2097 const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);2096 const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
2097 const atom = self.getAtomPtr(atom_index);
2098 atom.size = code.len;2098 atom.size = code.len;
2099 atom.alignment = required_alignment;2099 atom.alignment = required_alignment;
2100 // TODO: work out logic for disambiguating functions from function pointers2100 // TODO: work out logic for disambiguating functions from function pointers