Skip to content

Commit

Permalink
macho: calc number of relocations for each section
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Jan 2, 2024
1 parent ed4f9ad commit 1c983fb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
28 changes: 28 additions & 0 deletions src/MachO/eh_frame.zig
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,34 @@ pub fn calcSize(macho_file: *MachO) !u32 {
return offset;
}

pub fn calcNumRelocs(macho_file: *MachO) u32 {
const tracy = trace(@src());
defer tracy.end();

var nreloc: u32 = 0;

for (macho_file.objects.items) |index| {
const object = macho_file.getFile(index).?.object;
for (object.cies.items) |cie| {
if (!cie.alive) continue;
if (cie.getPersonality(macho_file)) |_| {
nreloc += 1; // personality
}
}

for (object.fdes.items) |fde| {
if (!fde.alive) continue;
nreloc += 1; // CIE ptr
nreloc += 1; // function
if (fde.getLsdaAtom(macho_file)) |_| {
nreloc += 1; // LSDA
}
}
}

return nreloc;
}

pub fn write(macho_file: *MachO, buffer: []u8) void {
const tracy = trace(@src());
defer tracy.end();
Expand Down
33 changes: 20 additions & 13 deletions src/MachO/relocatable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -142,41 +142,48 @@ fn calcSectionSizes(macho_file: *MachO) !void {
atom.value = offset;
header.size += padding + atom.size;
header.@"align" = @max(header.@"align", atom.alignment);
header.nreloc += @intCast(atom.relocs.len);
}
}

if (macho_file.unwind_info_sect_index) |index| {
const sect = &macho_file.sections.items(.header)[index];
sect.size = calcCompactUnwindSize(macho_file);
sect.@"align" = 3;
calcCompactUnwindSize(macho_file, index);
}

if (macho_file.eh_frame_sect_index) |index| {
const sect = &macho_file.sections.items(.header)[index];
sect.size = try eh_frame.calcSize(macho_file);
sect.@"align" = 3;
sect.nreloc = eh_frame.calcNumRelocs(macho_file);
}

// TODO __DWARF sections

// TODO relocations
// they should follow contiguously *after* we lay out contents of each section
// *but* they should be before __LINKEDIT sections (symtab, data-in-code)

}

fn calcCompactUnwindSize(macho_file: *MachO) usize {
var size: usize = 0;
fn calcCompactUnwindSize(macho_file: *MachO, sect_index: u8) void {
var size: u32 = 0;
var nreloc: u32 = 0;

for (macho_file.objects.items) |index| {
const object = macho_file.getFile(index).?.object;
for (object.unwind_records.items) |irec| {
const rec = macho_file.getUnwindRecord(irec);
if (rec.alive) {
size += 1;
if (!rec.alive) continue;
size += @sizeOf(macho.compact_unwind_entry);
nreloc += 1;
if (rec.getPersonality(macho_file)) |_| {
nreloc += 1;
}
if (rec.getLsdaAtom(macho_file)) |_| {
nreloc += 1;
}
}
}
return size * @sizeOf(macho.compact_unwind_entry);

const sect = &macho_file.sections.items(.header)[sect_index];
sect.size = size;
sect.nreloc = nreloc;
sect.@"align" = 3;
}

fn allocateSections(macho_file: *MachO) !void {
Expand Down

0 comments on commit 1c983fb

Please sign in to comment.