Skip to content

Commit

Permalink
macho: init output sections in relocatable mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Dec 28, 2023
1 parent 97cc9e1 commit 47d98bf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ fn getSectionRank(self: *MachO, sect_index: u8) u8 {
return (@as(u8, @intCast(segment_rank)) << 4) + section_rank;
}

fn sortSections(self: *MachO) !void {
pub fn sortSections(self: *MachO) !void {
const Entry = struct {
index: u8,

Expand Down Expand Up @@ -1629,7 +1629,7 @@ fn sortSections(self: *MachO) !void {
}
}

fn addAtomsToSections(self: *MachO) !void {
pub fn addAtomsToSections(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

Expand Down
38 changes: 38 additions & 0 deletions src/MachO/relocatable.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
pub fn flush(macho_file: *MachO) !void {
claimUnresolved(macho_file);
try initOutputSections(macho_file);
try macho_file.sortSections();
try macho_file.addAtomsToSections();
try calcSectionSizes(macho_file);

state_log.debug("{}", .{macho_file.dumpState()});

Expand Down Expand Up @@ -30,8 +34,42 @@ fn claimUnresolved(macho_file: *MachO) void {
}
}

fn initOutputSections(macho_file: *MachO) !void {
for (macho_file.objects.items) |index| {
const object = macho_file.getFile(index).?.object;
for (object.atoms.items) |atom_index| {
const atom = macho_file.getAtom(atom_index) orelse continue;
if (!atom.flags.alive) continue;
atom.out_n_sect = try Atom.initOutputSection(atom.getInputSection(macho_file), macho_file);
}
}

const needs_unwind_info = for (macho_file.objects.items) |index| {
if (macho_file.getFile(index).?.object.has_unwind) break true;
} else false;
if (needs_unwind_info) {
macho_file.unwind_info_sect_index = try macho_file.addSection("__TEXT", "__unwind_info", .{});
}

const needs_eh_frame = for (macho_file.objects.items) |index| {
if (macho_file.getFile(index).?.object.has_eh_frame) break true;
} else false;
if (needs_eh_frame) {
assert(needs_unwind_info);
macho_file.eh_frame_sect_index = try macho_file.addSection("__TEXT", "__eh_frame", .{});
}

// TODO __DWARF sections
}

fn calcSectionSizes(macho_file: *MachO) !void {
_ = macho_file;
}

const assert = std.debug.assert;
const state_log = std.log.scoped(.state);
const std = @import("std");

const Atom = @import("Atom.zig");
const MachO = @import("../MachO.zig");
const Symbol = @import("Symbol.zig");

0 comments on commit 47d98bf

Please sign in to comment.