Skip to content

Commit

Permalink
macho: fix incorrect address calc for unwind records
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Dec 3, 2023
1 parent 8050ca1 commit f9c4ba5
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 209 deletions.
2 changes: 1 addition & 1 deletion src/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1949,7 +1949,7 @@ fn writeDataInCode(self: *MachO) !void {
for (object.atoms.items) |atom_index| {
if (next_dice >= in_dices.len) break;
const atom = self.getAtom(atom_index) orelse continue;
const start_off = atom.getInputSection(self).addr + atom.off;
const start_off = atom.getInputAddress(self);
const end_off = start_off + atom.size;
const start_dice = next_dice;

Expand Down
4 changes: 4 additions & 0 deletions src/MachO/Atom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ pub fn getInputSection(self: Atom, macho_file: *MachO) macho.section_64 {
return object.sections.items(.header)[self.n_sect];
}

pub fn getInputAddress(self: Atom, macho_file: *MachO) u64 {
return self.getInputSection(macho_file).addr + self.off;
}

pub fn getPriority(self: Atom, macho_file: *MachO) u64 {
const object = self.getObject(macho_file);
return (@as(u64, @intCast(object.index)) << 32) | @as(u64, @intCast(self.n_sect));
Expand Down
23 changes: 9 additions & 14 deletions src/MachO/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ fn initSymbols(self: *Object, macho_file: *MachO) !void {
const value = if (nlist.abs())
nlist.n_value
else
nlist.n_value - atom.off - atom.getInputSection(macho_file).addr;
nlist.n_value - atom.getInputAddress(macho_file);
symbol.* = .{
.value = value,
.name = try macho_file.string_intern.insert(gpa, name),
Expand All @@ -356,9 +356,7 @@ fn initSymbols(self: *Object, macho_file: *MachO) !void {
fn sortAtoms(self: *Object, macho_file: *MachO) !void {
const lessThanAtom = struct {
fn lessThanAtom(ctx: *MachO, lhs: Atom.Index, rhs: Atom.Index) bool {
const lhsa = ctx.getAtom(lhs).?;
const rhsa = ctx.getAtom(rhs).?;
return lhsa.getInputSection(ctx).addr + lhsa.off < rhsa.getInputSection(ctx).addr + rhsa.off;
return ctx.getAtom(lhs).?.getInputAddress(ctx) < ctx.getAtom(rhs).?.getInputAddress(ctx);
}
}.lessThanAtom;
mem.sort(Atom.Index, self.atoms.items, macho_file, lessThanAtom);
Expand Down Expand Up @@ -484,9 +482,7 @@ fn initEhFrameRecords(self: *Object, sect_id: u8, macho_file: *MachO) !void {

const sortFn = struct {
fn sortFn(ctx: *MachO, lhs: Fde, rhs: Fde) bool {
const lhsa = lhs.getAtom(ctx);
const rhsa = rhs.getAtom(ctx);
return lhsa.getInputSection(ctx).addr + lhsa.off < rhsa.getInputSection(ctx).addr + rhsa.off;
return lhs.getAtom(ctx).getInputAddress(ctx) < rhs.getAtom(ctx).getInputAddress(ctx);
}
}.sortFn;

Expand Down Expand Up @@ -544,7 +540,7 @@ fn initUnwindRecords(self: *Object, sect_id: u8, macho_file: *MachO) !void {
.local => {
out.atom = self.findAtom(rec.rangeStart);
const atom = out.getAtom(macho_file);
out.atom_offset = @intCast(rec.rangeStart - atom.getInputSection(macho_file).addr - atom.off);
out.atom_offset = @intCast(atom.getInputAddress(macho_file) - rec.rangeStart);
},
},
16 => { // personality function
Expand All @@ -559,7 +555,7 @@ fn initUnwindRecords(self: *Object, sect_id: u8, macho_file: *MachO) !void {
.local => {
out.lsda = self.findAtom(rec.lsda);
const atom = out.getLsdaAtom(macho_file).?;
out.lsda_offset = @intCast(rec.lsda - atom.getInputSection(macho_file).addr - atom.off);
out.lsda_offset = @intCast(atom.getInputAddress(macho_file) - rec.lsda);
},
},
else => {},
Expand All @@ -579,13 +575,13 @@ fn initUnwindRecords(self: *Object, sect_id: u8, macho_file: *MachO) !void {
for (self.unwind_records.items) |rec_index| {
const rec = macho_file.getUnwindRecord(rec_index);
const atom = rec.getAtom(macho_file);
const addr = atom.getInputSection(macho_file).addr + atom.off + rec.atom_offset;
const addr = atom.getInputAddress(macho_file) + rec.atom_offset;
superposition.putAssumeCapacityNoClobber(addr, .{ rec_index, null });
}

for (self.fdes.items, 0..) |fde, fde_index| {
const atom = fde.getAtom(macho_file);
const addr = atom.getInputSection(macho_file).addr + atom.off + fde.atom_offset;
const addr = atom.getInputAddress(macho_file) + fde.atom_offset;
const gop = superposition.getOrPutAssumeCapacity(addr);
if (!gop.found_existing) {
gop.value_ptr.* = .{ null, null };
Expand Down Expand Up @@ -633,8 +629,7 @@ fn initUnwindRecords(self: *Object, sect_id: u8, macho_file: *MachO) !void {
const rhs = ctx.getUnwindRecord(rhs_index);
const lhsa = lhs.getAtom(ctx);
const rhsa = rhs.getAtom(ctx);
return lhsa.getInputSection(ctx).addr + lhsa.off + lhs.atom_offset <
rhsa.getInputSection(ctx).addr + rhsa.off + rhs.atom_offset;
return lhsa.getInputAddress(ctx) + lhs.atom_offset < rhsa.getInputAddress(ctx) + rhs.atom_offset;
}
}.sortFn;
mem.sort(UnwindInfo.Record.Index, self.unwind_records.items, macho_file, sortFn);
Expand Down Expand Up @@ -716,7 +711,7 @@ pub fn resolveSymbols(self: *Object, macho_file: *MachO) void {
if (self.asFile().getSymbolRank(nlist, !self.alive) < symbol.getSymbolRank(macho_file)) {
const value = if (!nlist.tentative() and !nlist.abs()) blk: {
const atom = macho_file.getAtom(atom_index).?;
break :blk nlist.n_value - atom.off - atom.getInputSection(macho_file).addr;
break :blk nlist.n_value - atom.getInputAddress(macho_file);
} else nlist.n_value;
symbol.value = value;
symbol.atom = atom_index;
Expand Down
Loading

0 comments on commit f9c4ba5

Please sign in to comment.