Skip to content

Commit

Permalink
macho: use binary search for findAtom
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Dec 21, 2023
1 parent 628b379 commit f77e085
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/MachO/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ fn initLiteralSections(self: *Object, macho_file: *MachO) !void {
}

pub fn findAtom(self: Object, addr: u64) ?Atom.Index {
const tracy = trace(@src());
defer tracy.end();
for (self.sections.items(.header), 0..) |sect, n_sect| {
if ((sect.addr <= addr and addr < sect.addr + sect.size) or
(sect.addr == addr and sect.size == 0))
Expand All @@ -352,19 +354,27 @@ fn findAtomInSection(self: Object, addr: u64, n_sect: u8) ?Atom.Index {
const slice = self.sections.slice();
const sect = slice.items(.header)[n_sect];
const subsections = slice.items(.subsections)[n_sect];
var idx: usize = 0;
while (idx < subsections.items.len) : (idx += 1) {

var min: usize = 0;
var max: usize = subsections.items.len;
while (min < max) {
const idx = (min + max) / 2;
const sub = subsections.items[idx];
const sub_addr = sect.addr + sub.off;
const sub_size = if (idx + 1 < subsections.items.len)
subsections.items[idx + 1].off - sub.off
else
sect.size - sub.off;
if ((sub_addr <= addr and addr < sub_addr + sub_size) or
(sub_addr == addr and sub_size == 0))
return sub.atom;
if (sub_addr == addr) return sub.atom;
if (sub_addr < addr and addr < sub_addr + sub_size) return sub.atom;
if (sub_addr < addr) {
min = idx + 1;
} else {
max = idx;
}
}
return null;

return subsections.items[min].atom;
}

fn linkNlistToAtom(self: *Object, macho_file: *MachO) !void {
Expand Down

0 comments on commit f77e085

Please sign in to comment.