authorgravatar for zhylmzr@gmail.comzhylmzr <zhylmzr@gmail.com> 2024-04-24 17:17:47+08:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-25 18:27:11+02:00
log3648d7df19642b6902b3f75538a826a322211673
tree4fb33092a03d8aaf449875906173dcbf4f93906b
parent1b90888f576b4863f4a61213a9ca32b97aa57859

fix: object size error in archive


4 files changed, 16 insertions(+), 8 deletions(-)

src/link/Elf/Archive.zig+1
...@@ -64,6 +64,7 @@ pub fn parse(self: *Archive, elf_file: *Elf, path: []const u8, handle_index: Fil...@@ -64,6 +64,7 @@ pub fn parse(self: *Archive, elf_file: *Elf, path: []const u8, handle_index: Fil
64 .archive = .{64 .archive = .{
65 .path = try gpa.dupe(u8, path),65 .path = try gpa.dupe(u8, path),
66 .offset = pos,66 .offset = pos,
67 .size = obj_size,
67 },68 },
68 .path = try gpa.dupe(u8, name),69 .path = try gpa.dupe(u8, name),
69 .file_handle = handle_index,70 .file_handle = handle_index,
src/link/Elf/Object.zig+7-4
...@@ -1009,13 +1009,15 @@ pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, elf_file: *Elf...@@ -1009,13 +1009,15 @@ pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, elf_file: *Elf
1009}1009}
10101010
1011pub fn updateArSize(self: *Object, elf_file: *Elf) !void {1011pub fn updateArSize(self: *Object, elf_file: *Elf) !void {
1012 const handle = elf_file.fileHandle(self.file_handle);1012 self.output_ar_state.size = if (self.archive) |ar| ar.size else size: {
1013 const size = (try handle.stat()).size;1013 const handle = elf_file.fileHandle(self.file_handle);
1014 self.output_ar_state.size = size;1014 break :size (try handle.stat()).size;
1015 };
1015}1016}
10161017
1017pub fn writeAr(self: Object, elf_file: *Elf, writer: anytype) !void {1018pub fn writeAr(self: Object, elf_file: *Elf, writer: anytype) !void {
1018 const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow;1019 const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow;
1020 const offset: u64 = if (self.archive) |ar| ar.offset else 0;
1019 const name = self.path;1021 const name = self.path;
1020 const hdr = Archive.setArHdr(.{1022 const hdr = Archive.setArHdr(.{
1021 .name = if (name.len <= Archive.max_member_name_len)1023 .name = if (name.len <= Archive.max_member_name_len)
...@@ -1029,7 +1031,7 @@ pub fn writeAr(self: Object, elf_file: *Elf, writer: anytype) !void {...@@ -1029,7 +1031,7 @@ pub fn writeAr(self: Object, elf_file: *Elf, writer: anytype) !void {
1029 const gpa = elf_file.base.comp.gpa;1031 const gpa = elf_file.base.comp.gpa;
1030 const data = try gpa.alloc(u8, size);1032 const data = try gpa.alloc(u8, size);
1031 defer gpa.free(data);1033 defer gpa.free(data);
1032 const amt = try handle.preadAll(data, 0);1034 const amt = try handle.preadAll(data, offset);
1033 if (amt != size) return error.InputOutput;1035 if (amt != size) return error.InputOutput;
1034 try writer.writeAll(data);1036 try writer.writeAll(data);
1035}1037}
...@@ -1349,6 +1351,7 @@ fn formatPath(...@@ -1349,6 +1351,7 @@ fn formatPath(
1349const InArchive = struct {1351const InArchive = struct {
1350 path: []const u8,1352 path: []const u8,
1351 offset: u64,1353 offset: u64,
1354 size: u32,
1352};1355};
13531356
1354const Object = @This();1357const Object = @This();
src/link/MachO/Archive.zig+1
...@@ -70,6 +70,7 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index:...@@ -70,6 +70,7 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index:
70 .archive = .{70 .archive = .{
71 .path = try gpa.dupe(u8, path),71 .path = try gpa.dupe(u8, path),
72 .offset = pos,72 .offset = pos,
73 .size = hdr_size,
73 },74 },
74 .path = try gpa.dupe(u8, name),75 .path = try gpa.dupe(u8, name),
75 .file_handle = handle_index,76 .file_handle = handle_index,
src/link/MachO/Object.zig+7-4
...@@ -34,6 +34,7 @@ output_ar_state: Archive.ArState = .{},...@@ -34,6 +34,7 @@ output_ar_state: Archive.ArState = .{},
34const InArchive = struct {34const InArchive = struct {
35 path: []const u8,35 path: []const u8,
36 offset: u64,36 offset: u64,
37 size: u32,
37};38};
3839
39pub fn isObject(path: []const u8) !bool {40pub fn isObject(path: []const u8) !bool {
...@@ -1333,14 +1334,16 @@ pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, macho_file: *M...@@ -1333,14 +1334,16 @@ pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, macho_file: *M
1333}1334}
13341335
1335pub fn updateArSize(self: *Object, macho_file: *MachO) !void {1336pub fn updateArSize(self: *Object, macho_file: *MachO) !void {
1336 const file = macho_file.getFileHandle(self.file_handle);1337 self.output_ar_state.size = if (self.archive) |ar| ar.size else size: {
1337 const size = (try file.stat()).size;1338 const file = macho_file.getFileHandle(self.file_handle);
1338 self.output_ar_state.size = size;1339 break :size (try file.stat()).size;
1340 };
1339}1341}
13401342
1341pub fn writeAr(self: Object, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void {1343pub fn writeAr(self: Object, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void {
1342 // Header1344 // Header
1343 const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow;1345 const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow;
1346 const offset: u64 = if (self.archive) |ar| ar.offset else 0;
1344 try Archive.writeHeader(self.path, size, ar_format, writer);1347 try Archive.writeHeader(self.path, size, ar_format, writer);
1345 // Data1348 // Data
1346 const file = macho_file.getFileHandle(self.file_handle);1349 const file = macho_file.getFileHandle(self.file_handle);
...@@ -1348,7 +1351,7 @@ pub fn writeAr(self: Object, ar_format: Archive.Format, macho_file: *MachO, writ...@@ -1348,7 +1351,7 @@ pub fn writeAr(self: Object, ar_format: Archive.Format, macho_file: *MachO, writ
1348 const gpa = macho_file.base.comp.gpa;1351 const gpa = macho_file.base.comp.gpa;
1349 const data = try gpa.alloc(u8, size);1352 const data = try gpa.alloc(u8, size);
1350 defer gpa.free(data);1353 defer gpa.free(data);
1351 const amt = try file.preadAll(data, 0);1354 const amt = try file.preadAll(data, offset);
1352 if (amt != size) return error.InputOutput;1355 if (amt != size) return error.InputOutput;
1353 try writer.writeAll(data);1356 try writer.writeAll(data);
1354}1357}