authorgravatar for zhylmzr@gmail.comzhylmzr <zhylmzr@gmail.com> 2024-04-24 17:17:47+08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-05-01 12:04:03-07:00
logef9fb428b7433238b9c0bd38d3ff36accf0f3d12
tree6cb8338c0d60e344b615041b95146fa422d3c209
parent600b652825ef4e1360f6e9d6ad5a20c32fada5ca

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
6464 .archive = .{
6565 .path = try gpa.dupe(u8, path),
6666 .offset = pos,
67 .size = obj_size,
6768 },
6869 .path = try gpa.dupe(u8, name),
6970 .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
10091009}
10101010
10111011pub fn updateArSize(self: *Object, elf_file: *Elf) !void {
1012 const handle = elf_file.fileHandle(self.file_handle);
1013 const size = (try handle.stat()).size;
1014 self.output_ar_state.size = size;
1012 self.output_ar_state.size = if (self.archive) |ar| ar.size else size: {
1013 const handle = elf_file.fileHandle(self.file_handle);
1014 break :size (try handle.stat()).size;
1015 };
10151016}
10161017
10171018pub fn writeAr(self: Object, elf_file: *Elf, writer: anytype) !void {
10181019 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;
10191021 const name = self.path;
10201022 const hdr = Archive.setArHdr(.{
10211023 .name = if (name.len <= Archive.max_member_name_len)
......@@ -1029,7 +1031,7 @@ pub fn writeAr(self: Object, elf_file: *Elf, writer: anytype) !void {
10291031 const gpa = elf_file.base.comp.gpa;
10301032 const data = try gpa.alloc(u8, size);
10311033 defer gpa.free(data);
1032 const amt = try handle.preadAll(data, 0);
1034 const amt = try handle.preadAll(data, offset);
10331035 if (amt != size) return error.InputOutput;
10341036 try writer.writeAll(data);
10351037}
......@@ -1349,6 +1351,7 @@ fn formatPath(
13491351const InArchive = struct {
13501352 path: []const u8,
13511353 offset: u64,
1354 size: u32,
13521355};
13531356
13541357const 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:
7070 .archive = .{
7171 .path = try gpa.dupe(u8, path),
7272 .offset = pos,
73 .size = hdr_size,
7374 },
7475 .path = try gpa.dupe(u8, name),
7576 .file_handle = handle_index,
src/link/MachO/Object.zig+7-4
......@@ -34,6 +34,7 @@ output_ar_state: Archive.ArState = .{},
3434const InArchive = struct {
3535 path: []const u8,
3636 offset: u64,
37 size: u32,
3738};
3839
3940pub fn isObject(path: []const u8) !bool {
......@@ -1330,14 +1331,16 @@ pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, macho_file: *M
13301331}
13311332
13321333pub fn updateArSize(self: *Object, macho_file: *MachO) !void {
1333 const file = macho_file.getFileHandle(self.file_handle);
1334 const size = (try file.stat()).size;
1335 self.output_ar_state.size = size;
1334 self.output_ar_state.size = if (self.archive) |ar| ar.size else size: {
1335 const file = macho_file.getFileHandle(self.file_handle);
1336 break :size (try file.stat()).size;
1337 };
13361338}
13371339
13381340pub fn writeAr(self: Object, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void {
13391341 // Header
13401342 const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow;
1343 const offset: u64 = if (self.archive) |ar| ar.offset else 0;
13411344 try Archive.writeHeader(self.path, size, ar_format, writer);
13421345 // Data
13431346 const file = macho_file.getFileHandle(self.file_handle);
......@@ -1345,7 +1348,7 @@ pub fn writeAr(self: Object, ar_format: Archive.Format, macho_file: *MachO, writ
13451348 const gpa = macho_file.base.comp.gpa;
13461349 const data = try gpa.alloc(u8, size);
13471350 defer gpa.free(data);
1348 const amt = try file.preadAll(data, 0);
1351 const amt = try file.preadAll(data, offset);
13491352 if (amt != size) return error.InputOutput;
13501353 try writer.writeAll(data);
13511354}