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...@@ -42,9 +42,7 @@ pub fn parse(self: *Archive, elf_file: *Elf, path: []const u8, handle_index: Fil
42 }42 }
4343
44 const obj_size = try hdr.size();44 const obj_size = try hdr.size();
45 defer {45 defer pos += obj_size;
46 pos += obj_size;
47 }
4846
49 if (hdr.isSymtab() or hdr.isSymtab64()) continue;47 if (hdr.isSymtab() or hdr.isSymtab64()) continue;
50 if (hdr.isStrtab()) {48 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:...@@ -24,20 +24,18 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index:
24 const handle = macho_file.getFileHandle(handle_index);24 const handle = macho_file.getFileHandle(handle_index);
25 const offset = if (fat_arch) |ar| ar.offset else 0;25 const offset = if (fat_arch) |ar| ar.offset else 0;
26 const size = if (fat_arch) |ar| ar.size else (try handle.stat()).size;26 const size = if (fat_arch) |ar| ar.size else (try handle.stat()).size;
27 try handle.seekTo(offset);
2827
29 const reader = handle.reader();28 var pos: usize = offset + SARMAG;
30 _ = try reader.readBytesNoEof(SARMAG);
31
32 var pos: usize = SARMAG;
33 while (true) {29 while (true) {
34 if (pos >= size) break;30 if (pos >= size) break;
35 if (!mem.isAligned(pos, 2)) {31 if (!mem.isAligned(pos, 2)) pos += 1;
36 try handle.seekBy(1);
37 pos += 1;
38 }
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)).*;
41 pos += @sizeOf(ar_hdr);39 pos += @sizeOf(ar_hdr);
4240
43 if (!mem.eql(u8, &hdr.ar_fmag, ARFMAG)) {41 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:...@@ -53,17 +51,15 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index:
53 if (try hdr.nameLength()) |len| {51 if (try hdr.nameLength()) |len| {
54 hdr_size -= len;52 hdr_size -= len;
55 const buf = try arena.allocator().alloc(u8, len);53 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;
57 pos += len;56 pos += len;
58 const actual_len = mem.indexOfScalar(u8, buf, @as(u8, 0)) orelse len;57 const actual_len = mem.indexOfScalar(u8, buf, @as(u8, 0)) orelse len;
59 break :name buf[0..actual_len];58 break :name buf[0..actual_len];
60 }59 }
61 unreachable;60 unreachable;
62 };61 };
63 defer {62 defer pos += hdr_size;
64 _ = handle.seekBy(hdr_size) catch {};
65 pos += hdr_size;
66 }
6763
68 if (mem.eql(u8, name, SYMDEF) or64 if (mem.eql(u8, name, SYMDEF) or
69 mem.eql(u8, name, SYMDEF64) or65 mem.eql(u8, name, SYMDEF64) or
...@@ -73,7 +69,7 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index:...@@ -73,7 +69,7 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index:
73 const object = Object{69 const object = Object{
74 .archive = .{70 .archive = .{
75 .path = try gpa.dupe(u8, path),71 .path = try gpa.dupe(u8, path),
76 .offset = offset + pos,72 .offset = pos,
77 },73 },
78 .path = try gpa.dupe(u8, name),74 .path = try gpa.dupe(u8, name),
79 .file_handle = handle_index,75 .file_handle = handle_index,