Skip to content

Commit

Permalink
macho: fix invalid strtab calculation when writing out stabs
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Nov 25, 2023
1 parent 5fe403b commit 73a5eae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
25 changes: 16 additions & 9 deletions src/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1867,13 +1867,16 @@ fn calcSymtabSize(self: *MachO) !void {
strsize += ctx.strsize;
}

for (self.objects.items) |index| {
const object = self.getFile(index).?.object;
const ctx = &object.output_symtab_ctx;
ctx.istab = nstabs;
object.calcStabsSize(self);
nstabs += ctx.nstabs;
strsize += ctx.strsize;
if (!self.options.strip) {
for (self.objects.items) |index| {
const object = self.getFile(index).?.object;
const ctx = &object.output_symtab_ctx;
const no_stabs_strsize = ctx.strsize;
ctx.istab = nstabs;
object.calcStabsSize(self);
nstabs += ctx.nstabs;
strsize += ctx.strsize - no_stabs_strsize;
}
}

for (files.items) |index| {
Expand Down Expand Up @@ -1915,8 +1918,10 @@ fn writeSymtab(self: *MachO) !void {
for (self.objects.items) |index| {
self.getFile(index).?.writeSymtab(self);
}
for (self.objects.items) |index| {
self.getFile(index).?.object.writeStabs(self);
if (!self.options.strip) {
for (self.objects.items) |index| {
self.getFile(index).?.object.writeStabs(self);
}
}
for (self.dylibs.items) |index| {
self.getFile(index).?.writeSymtab(self);
Expand All @@ -1925,6 +1930,8 @@ fn writeSymtab(self: *MachO) !void {
internal.writeSymtab(self);
}

assert(self.strtab.items.len == cmd.strsize);

try self.base.file.pwriteAll(mem.sliceAsBytes(self.symtab.items), cmd.symoff);

self.getLinkeditSegment().filesize += cmd.nsyms * @sizeOf(macho.nlist_64);
Expand Down
6 changes: 3 additions & 3 deletions src/MachO/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ pub fn calcSymtabSize(self: *Object, macho_file: *MachO) !void {
}

pub fn calcStabsSize(self: *Object, macho_file: *MachO) void {
if (macho_file.options.strip) return;
assert(!macho_file.options.strip);
if (!self.hasDebugInfo()) return;

// TODO handle multiple CUs
Expand All @@ -581,7 +581,7 @@ pub fn calcStabsSize(self: *Object, macho_file: *MachO) void {
self.output_symtab_ctx.strsize += @as(u32, @intCast(tu_name.len + 1)); // tu_name

if (self.archive) |path| {
self.output_symtab_ctx.strsize += @as(u32, @intCast(path.len + 1 + 1 + self.path.len + 1 + 1));
self.output_symtab_ctx.strsize += @as(u32, @intCast(path.len + 1 + self.path.len + 1 + 1));
} else {
self.output_symtab_ctx.strsize += @as(u32, @intCast(self.path.len + 1));
}
Expand Down Expand Up @@ -637,7 +637,7 @@ pub fn writeSymtab(self: Object, macho_file: *MachO) void {
}

pub fn writeStabs(self: Object, macho_file: *MachO) void {
if (macho_file.options.strip) return;
assert(!macho_file.options.strip);
if (!self.hasDebugInfo()) return;
const writeFuncStab = struct {
inline fn writeFuncStab(
Expand Down

0 comments on commit 73a5eae

Please sign in to comment.