Skip to content

Commit

Permalink
macho: skip debug sections when matching nlists to subsections
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Dec 16, 2023
1 parent 228be14 commit e9856b3
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/MachO/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,17 @@ inline fn isLiteral(sect: macho.section_64) bool {
};
}

inline fn isDebug(sect: macho.section_64) bool {
return sect.attrs() & macho.S_ATTR_DEBUG != 0 or
mem.eql(u8, sect.sectName(), "__eh_frame") or
mem.eql(u8, sect.sectName(), "__compact_unwind");
}

fn initSubsections(self: *Object, nlists: anytype, macho_file: *MachO) !void {
const gpa = macho_file.base.allocator;
const slice = self.sections.slice();
for (slice.items(.header), slice.items(.subsections), 0..) |sect, *subsections, n_sect| {
if (sect.attrs() & macho.S_ATTR_DEBUG != 0) continue;
if (self.eh_frame_sect_index) |index| if (index == n_sect) continue;
if (self.compact_unwind_sect_index) |index| if (index == n_sect) continue;
if (isLiteral(sect)) continue;
if (isDebug(sect) or isLiteral(sect)) continue;

const nlist_start = for (nlists, 0..) |nlist, i| {
if (nlist.nlist.n_sect - 1 == n_sect) break i;
Expand Down Expand Up @@ -220,10 +223,7 @@ fn initSections(self: *Object, nlists: anytype, macho_file: *MachO) !void {
try self.atoms.ensureUnusedCapacity(gpa, self.sections.items(.header).len);

for (slice.items(.header), 0..) |sect, n_sect| {
if (sect.attrs() & macho.S_ATTR_DEBUG != 0) continue;
if (self.eh_frame_sect_index) |index| if (index == n_sect) continue;
if (self.compact_unwind_sect_index) |index| if (index == n_sect) continue;
if (isLiteral(sect)) continue;
if (isDebug(sect) or isLiteral(sect)) continue;

const name = try std.fmt.allocPrintZ(gpa, "{s}${s}", .{ sect.segName(), sect.sectName() });
defer gpa.free(name);
Expand Down Expand Up @@ -297,10 +297,7 @@ fn initLiteralSections(self: *Object, macho_file: *MachO) !void {
try self.atoms.ensureUnusedCapacity(gpa, self.sections.items(.header).len);

for (slice.items(.header), 0..) |sect, n_sect| {
if (sect.attrs() & macho.S_ATTR_DEBUG != 0) continue;
if (self.eh_frame_sect_index) |index| if (index == n_sect) continue;
if (self.compact_unwind_sect_index) |index| if (index == n_sect) continue;
if (!isLiteral(sect)) continue;
if (isDebug(sect) or !isLiteral(sect)) continue;

const name = try std.fmt.allocPrintZ(gpa, "{s}${s}", .{ sect.segName(), sect.sectName() });
defer gpa.free(name);
Expand All @@ -318,7 +315,9 @@ fn initLiteralSections(self: *Object, macho_file: *MachO) !void {

pub fn findAtom(self: Object, addr: u64) ?Atom.Index {
for (self.sections.items(.header), 0..) |sect, n_sect| {
if (sect.addr <= addr and addr < sect.addr + sect.size) {
if ((sect.addr <= addr and addr < sect.addr + sect.size) or
(sect.addr == addr and sect.size == 0))
{
return self.findAtomInSection(addr, @intCast(n_sect));
}
}
Expand All @@ -337,15 +336,18 @@ fn findAtomInSection(self: Object, addr: u64, n_sect: u8) ?Atom.Index {
subsections.items[idx + 1].off - sub.off
else
sect.size - sub.off;
if (sub_addr <= addr and addr < sub_addr + sub_size) return sub.atom;
if ((sub_addr <= addr and addr < sub_addr + sub_size) or
(sub_addr == addr and sub_size == 0))
return sub.atom;
}
return null;
}

fn linkNlistToAtom(self: *Object, macho_file: *MachO) !void {
for (self.symtab.items(.nlist), self.symtab.items(.atom)) |nlist, *atom| {
if (!nlist.stab() and nlist.sect()) {
// if (sect.attrs() & macho.S_ATTR_DEBUG != 0) continue;
const sect = self.sections.items(.header)[nlist.n_sect - 1];
if (isDebug(sect)) continue;
if (self.findAtomInSection(nlist.n_value, nlist.n_sect - 1)) |atom_index| {
atom.* = atom_index;
} else {
Expand Down

0 comments on commit e9856b3

Please sign in to comment.