Skip to content

Commit

Permalink
macho: init all sections and mark atoms dead after
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Dec 16, 2023
1 parent 228be14 commit ef57b0e
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/MachO/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {

self.initPlatform();
try self.initDwarfInfo(gpa);

for (self.sections.items(.header), self.sections.items(.subsections)) |sect, subs| {
if (sect.attrs() & macho.S_ATTR_DEBUG == 0) continue;
if (!mem.eql(u8, sect.sectName(), "__eh_frame")) continue;
if (!mem.eql(u8, sect.sectName(), "__compact_unwind")) continue;
for (subs.items) |sub| {
macho_file.getAtom(sub.atom).?.flags = .{
.alive = false,
.visited = true,
};
}
}
}

inline fn isLiteral(sect: macho.section_64) bool {
Expand All @@ -148,9 +160,6 @@ 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;

const nlist_start = for (nlists, 0..) |nlist, i| {
Expand Down Expand Up @@ -220,9 +229,6 @@ 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;

const name = try std.fmt.allocPrintZ(gpa, "{s}${s}", .{ sect.segName(), sect.sectName() });
Expand Down Expand Up @@ -297,9 +303,6 @@ 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;

const name = try std.fmt.allocPrintZ(gpa, "{s}${s}", .{ sect.segName(), sect.sectName() });
Expand All @@ -318,7 +321,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 +342,16 @@ 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;
if (self.findAtomInSection(nlist.n_value, nlist.n_sect - 1)) |atom_index| {
atom.* = atom_index;
} else {
Expand Down

0 comments on commit ef57b0e

Please sign in to comment.