Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macho: write data-in-code entries in -r mode #99

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ pub fn flush(self: *MachO) !void {
off = mem.alignForward(u32, off, @alignOf(u64));
off = try self.writeFunctionStarts(off);
off = mem.alignForward(u32, off, @alignOf(u64));
off = try self.writeDataInCode(off);
off = try self.writeDataInCode(self.getTextSegment().vmaddr, off);
try self.calcSymtabSize();
off = mem.alignForward(u32, off, @alignOf(u64));
off = try self.writeSymtab(off);
Expand Down Expand Up @@ -2308,12 +2308,10 @@ fn writeFunctionStarts(self: *MachO, off: u32) !u32 {
return off;
}

fn writeDataInCode(self: *MachO, off: u32) !u32 {
pub fn writeDataInCode(self: *MachO, base_address: u64, off: u32) !u32 {
const cmd = &self.data_in_code_cmd;
cmd.dataoff = off;

const base = self.getTextSegment().vmaddr;

const gpa = self.base.allocator;
var dices = std.ArrayList(macho.data_in_code_entry).init(gpa);
defer dices.deinit();
Expand All @@ -2340,7 +2338,7 @@ fn writeDataInCode(self: *MachO, off: u32) !u32 {

if (atom.flags.alive) for (in_dices[start_dice..next_dice]) |dice| {
dices.appendAssumeCapacity(.{
.offset = @intCast(atom.value + dice.offset - start_off - base),
.offset = @intCast(atom.value + dice.offset - start_off - base_address),
.length = dice.length,
.kind = dice.kind,
});
Expand Down
19 changes: 1 addition & 18 deletions src/MachO/relocatable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn flush(macho_file: *MachO) !void {
try writeEhFrame(macho_file);

off = mem.alignForward(u32, off, @alignOf(u64));
off = try writeDataInCode(macho_file, off);
off = try macho_file.writeDataInCode(0, off);
off = mem.alignForward(u32, off, @alignOf(u64));
off = try macho_file.writeSymtab(off);
off = mem.alignForward(u32, off, @alignOf(u64));
Expand Down Expand Up @@ -348,13 +348,6 @@ fn writeEhFrame(macho_file: *MachO) !void {
try macho_file.base.file.pwriteAll(mem.sliceAsBytes(relocs.items), header.reloff);
}

fn writeDataInCode(macho_file: *MachO, off: u32) !u32 {
// TODO actually write it out
const cmd = &macho_file.data_in_code_cmd;
cmd.dataoff = off;
return off;
}

fn writeLoadCommands(macho_file: *MachO) !struct { usize, usize } {
const gpa = macho_file.base.allocator;
const needed_size = load_commands.calcLoadCommandsSizeObject(macho_file);
Expand Down Expand Up @@ -426,16 +419,6 @@ fn writeHeader(macho_file: *MachO, ncmds: usize, sizeofcmds: usize) !void {
else => {},
}

if (macho_file.has_tlv) {
header.flags |= macho.MH_HAS_TLV_DESCRIPTORS;
}
if (macho_file.binds_to_weak) {
header.flags |= macho.MH_BINDS_TO_WEAK;
}
if (macho_file.weak_defines) {
header.flags |= macho.MH_WEAK_DEFINES;
}

header.ncmds = @intCast(ncmds);
header.sizeofcmds = @intCast(sizeofcmds);

Expand Down