macho: fix invalid ZigObject size calculation

This commit is contained in:
Jakub Konka 2024-02-07 18:05:20 +01:00
parent 35ac066f1d
commit 82144a9073
2 changed files with 2 additions and 8 deletions

View File

@ -285,15 +285,9 @@ pub fn checkDuplicates(self: *ZigObject, dupes: anytype, macho_file: *MachO) !vo
/// This is just a temporary helper function that allows us to re-read what we wrote to file into a buffer.
/// We need this so that we can write to an archive.
/// TODO implement writing ZigObject data directly to a buffer instead.
pub fn readFileContents(self: *ZigObject, macho_file: *MachO) !void {
pub fn readFileContents(self: *ZigObject, size: usize, macho_file: *MachO) !void {
const gpa = macho_file.base.comp.gpa;
var end_pos: u64 = 0;
for (macho_file.segments.items) |seg| {
end_pos = @max(end_pos, seg.fileoff + seg.filesize);
}
const size = std.math.cast(usize, end_pos) orelse return error.Overflow;
try self.data.resize(gpa, size);
const amt = try macho_file.base.file.?.preadAll(self.data.items, 0);
if (amt != size) return error.InputOutput;
}

View File

@ -152,7 +152,7 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?
// TODO we can avoid reading in the file contents we just wrote if we give the linker
// ability to write directly to a buffer.
try zo.readFileContents(macho_file);
try zo.readFileContents(off, macho_file);
}
var files = std.ArrayList(File.Index).init(gpa);