diff --git a/src/MachO.zig b/src/MachO.zig index 21336adf..3c28f366 100644 --- a/src/MachO.zig +++ b/src/MachO.zig @@ -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); @@ -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(); @@ -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, }); diff --git a/src/MachO/relocatable.zig b/src/MachO/relocatable.zig index 17caa800..8db8832d 100644 --- a/src/MachO/relocatable.zig +++ b/src/MachO/relocatable.zig @@ -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)); @@ -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); @@ -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);