Skip to content

Commit

Permalink
macho: write data-in-code entries in -r mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Jan 7, 2024
1 parent 193fcbf commit b8efe50
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 23 deletions.
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

0 comments on commit b8efe50

Please sign in to comment.