Skip to content

Commit

Permalink
fix: object size error in archive
Browse files Browse the repository at this point in the history
  • Loading branch information
zhylmzr authored and kubkon committed Apr 25, 2024
1 parent 1b90888 commit 3648d7d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/link/Elf/Archive.zig
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub fn parse(self: *Archive, elf_file: *Elf, path: []const u8, handle_index: Fil
.archive = .{
.path = try gpa.dupe(u8, path),
.offset = pos,
.size = obj_size,
},
.path = try gpa.dupe(u8, name),
.file_handle = handle_index,
Expand Down
11 changes: 7 additions & 4 deletions src/link/Elf/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1009,13 +1009,15 @@ pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, elf_file: *Elf
}

pub fn updateArSize(self: *Object, elf_file: *Elf) !void {
const handle = elf_file.fileHandle(self.file_handle);
const size = (try handle.stat()).size;
self.output_ar_state.size = size;
self.output_ar_state.size = if (self.archive) |ar| ar.size else size: {
const handle = elf_file.fileHandle(self.file_handle);
break :size (try handle.stat()).size;
};
}

pub fn writeAr(self: Object, elf_file: *Elf, writer: anytype) !void {
const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow;
const offset: u64 = if (self.archive) |ar| ar.offset else 0;
const name = self.path;
const hdr = Archive.setArHdr(.{
.name = if (name.len <= Archive.max_member_name_len)
Expand All @@ -1029,7 +1031,7 @@ pub fn writeAr(self: Object, elf_file: *Elf, writer: anytype) !void {
const gpa = elf_file.base.comp.gpa;
const data = try gpa.alloc(u8, size);
defer gpa.free(data);
const amt = try handle.preadAll(data, 0);
const amt = try handle.preadAll(data, offset);
if (amt != size) return error.InputOutput;
try writer.writeAll(data);
}
Expand Down Expand Up @@ -1349,6 +1351,7 @@ fn formatPath(
const InArchive = struct {
path: []const u8,
offset: u64,
size: u32,
};

const Object = @This();
Expand Down
1 change: 1 addition & 0 deletions src/link/MachO/Archive.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index:
.archive = .{
.path = try gpa.dupe(u8, path),
.offset = pos,
.size = hdr_size,
},
.path = try gpa.dupe(u8, name),
.file_handle = handle_index,
Expand Down
11 changes: 7 additions & 4 deletions src/link/MachO/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ output_ar_state: Archive.ArState = .{},
const InArchive = struct {
path: []const u8,
offset: u64,
size: u32,
};

pub fn isObject(path: []const u8) !bool {
Expand Down Expand Up @@ -1333,22 +1334,24 @@ pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, macho_file: *M
}

pub fn updateArSize(self: *Object, macho_file: *MachO) !void {
const file = macho_file.getFileHandle(self.file_handle);
const size = (try file.stat()).size;
self.output_ar_state.size = size;
self.output_ar_state.size = if (self.archive) |ar| ar.size else size: {
const file = macho_file.getFileHandle(self.file_handle);
break :size (try file.stat()).size;
};
}

pub fn writeAr(self: Object, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void {
// Header
const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow;
const offset: u64 = if (self.archive) |ar| ar.offset else 0;
try Archive.writeHeader(self.path, size, ar_format, writer);
// Data
const file = macho_file.getFileHandle(self.file_handle);
// TODO try using copyRangeAll
const gpa = macho_file.base.comp.gpa;
const data = try gpa.alloc(u8, size);
defer gpa.free(data);
const amt = try file.preadAll(data, 0);
const amt = try file.preadAll(data, offset);
if (amt != size) return error.InputOutput;
try writer.writeAll(data);
}
Expand Down

0 comments on commit 3648d7d

Please sign in to comment.