authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-23 10:33:00+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-23 10:33:02+02:00
logf1af53f68ec629ca091452aeeeb2f7f7596e63b7
treedaf6ffa68ea9aeb1b98738b1fe3e9008d176e320
parent7c37c55161b718ff3df0b6c76480a4d538519a84

macho: use pread syscall when loading tapi file

This avoids mixing preads with reads which do not mix well especially on Windows.

3 files changed, 14 insertions(+), 5 deletions(-)

src/link/MachO.zig+1-1
......@@ -914,6 +914,7 @@ fn parseInputFileWorker(self: *MachO, file: File) void {
914914 switch (err) {
915915 error.MalformedObject,
916916 error.MalformedDylib,
917 error.MalformedTbd,
917918 error.InvalidCpuArch,
918919 error.InvalidTarget,
919920 => {}, // already reported
......@@ -4637,7 +4638,6 @@ const ObjcStubsSection = synthetic.ObjcStubsSection;
46374638const Object = @import("MachO/Object.zig");
46384639const LazyBind = bind.LazyBind;
46394640const LaSymbolPtrSection = synthetic.LaSymbolPtrSection;
4640const LibStub = tapi.LibStub;
46414641const Liveness = @import("../Liveness.zig");
46424642const LlvmObject = @import("../codegen/llvm.zig").Object;
46434643const Md5 = std.crypto.hash.Md5;
src/link/MachO/Dylib.zig+4-1
......@@ -270,7 +270,10 @@ fn parseTbd(self: *Dylib, macho_file: *MachO) !void {
270270 log.debug("parsing dylib from stub: {s}", .{self.path});
271271
272272 const file = macho_file.getFileHandle(self.file_handle);
273 var lib_stub = LibStub.loadFromFile(gpa, file) catch return error.NotLibStub;
273 var lib_stub = LibStub.loadFromFile(gpa, file) catch |err| {
274 try macho_file.reportParseError2(self.index, "failed to parse TBD file: {s}", .{@errorName(err)});
275 return error.MalformedTbd;
276 };
274277 defer lib_stub.deinit();
275278 const umbrella_lib = lib_stub.inner[0];
276279
src/link/tapi.zig+9-3
......@@ -129,8 +129,8 @@ pub const Tbd = union(enum) {
129129
130130pub const TapiError = error{
131131 NotLibStub,
132 FileTooBig,
133} || yaml.YamlError || std.fs.File.ReadError;
132 InputOutput,
133} || yaml.YamlError || std.fs.File.PReadError;
134134
135135pub const LibStub = struct {
136136 /// Underlying memory for stub's contents.
......@@ -140,8 +140,14 @@ pub const LibStub = struct {
140140 inner: []Tbd,
141141
142142 pub fn loadFromFile(allocator: Allocator, file: fs.File) TapiError!LibStub {
143 const source = try file.readToEndAlloc(allocator, std.math.maxInt(u32));
143 const filesize = blk: {
144 const stat = file.stat() catch break :blk std.math.maxInt(u32);
145 break :blk @min(stat.size, std.math.maxInt(u32));
146 };
147 const source = try allocator.alloc(u8, filesize);
144148 defer allocator.free(source);
149 const amt = try file.preadAll(source, 0);
150 if (amt != filesize) return error.InputOutput;
145151
146152 var lib_stub = LibStub{
147153 .yaml = try Yaml.load(allocator, source),