authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-13 19:18:27+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-13 19:18:27+01:00
logc22bb3805821d7ffe60e048f1efe362aad703668
treec34145087c21c8915f0df56dc799e8ca8ccbc148
parentde30b30202f8327fd152dba8c74e40849c2ddad3

macho: scrap reader for preads when parsing archives


2 files changed, 13 insertions(+), 19 deletions(-)

src/link/Elf/Archive.zig+1-3
......@@ -42,9 +42,7 @@ pub fn parse(self: *Archive, elf_file: *Elf, path: []const u8, handle_index: Fil
4242 }
4343
4444 const obj_size = try hdr.size();
45 defer {
46 pos += obj_size;
47 }
45 defer pos += obj_size;
4846
4947 if (hdr.isSymtab() or hdr.isSymtab64()) continue;
5048 if (hdr.isStrtab()) {
src/link/MachO/Archive.zig+12-16
......@@ -24,20 +24,18 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index:
2424 const handle = macho_file.getFileHandle(handle_index);
2525 const offset = if (fat_arch) |ar| ar.offset else 0;
2626 const size = if (fat_arch) |ar| ar.size else (try handle.stat()).size;
27 try handle.seekTo(offset);
2827
29 const reader = handle.reader();
30 _ = try reader.readBytesNoEof(SARMAG);
31
32 var pos: usize = SARMAG;
28 var pos: usize = offset + SARMAG;
3329 while (true) {
3430 if (pos >= size) break;
35 if (!mem.isAligned(pos, 2)) {
36 try handle.seekBy(1);
37 pos += 1;
38 }
31 if (!mem.isAligned(pos, 2)) pos += 1;
3932
40 const hdr = try reader.readStruct(ar_hdr);
33 var hdr_buffer: [@sizeOf(ar_hdr)]u8 = undefined;
34 {
35 const amt = try handle.preadAll(&hdr_buffer, pos);
36 if (amt != @sizeOf(ar_hdr)) return error.InputOutput;
37 }
38 const hdr = @as(*align(1) const ar_hdr, @ptrCast(&hdr_buffer)).*;
4139 pos += @sizeOf(ar_hdr);
4240
4341 if (!mem.eql(u8, &hdr.ar_fmag, ARFMAG)) {
......@@ -53,17 +51,15 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index:
5351 if (try hdr.nameLength()) |len| {
5452 hdr_size -= len;
5553 const buf = try arena.allocator().alloc(u8, len);
56 try reader.readNoEof(buf);
54 const amt = try handle.preadAll(buf, pos);
55 if (amt != len) return error.InputOutput;
5756 pos += len;
5857 const actual_len = mem.indexOfScalar(u8, buf, @as(u8, 0)) orelse len;
5958 break :name buf[0..actual_len];
6059 }
6160 unreachable;
6261 };
63 defer {
64 _ = handle.seekBy(hdr_size) catch {};
65 pos += hdr_size;
66 }
62 defer pos += hdr_size;
6763
6864 if (mem.eql(u8, name, SYMDEF) or
6965 mem.eql(u8, name, SYMDEF64) or
......@@ -73,7 +69,7 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index:
7369 const object = Object{
7470 .archive = .{
7571 .path = try gpa.dupe(u8, path),
76 .offset = offset + pos,
72 .offset = pos,
7773 },
7874 .path = try gpa.dupe(u8, name),
7975 .file_handle = handle_index,