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 {...@@ -914,6 +914,7 @@ fn parseInputFileWorker(self: *MachO, file: File) void {
914 switch (err) {914 switch (err) {
915 error.MalformedObject,915 error.MalformedObject,
916 error.MalformedDylib,916 error.MalformedDylib,
917 error.MalformedTbd,
917 error.InvalidCpuArch,918 error.InvalidCpuArch,
918 error.InvalidTarget,919 error.InvalidTarget,
919 => {}, // already reported920 => {}, // already reported
...@@ -4637,7 +4638,6 @@ const ObjcStubsSection = synthetic.ObjcStubsSection;...@@ -4637,7 +4638,6 @@ const ObjcStubsSection = synthetic.ObjcStubsSection;
4637const Object = @import("MachO/Object.zig");4638const Object = @import("MachO/Object.zig");
4638const LazyBind = bind.LazyBind;4639const LazyBind = bind.LazyBind;
4639const LaSymbolPtrSection = synthetic.LaSymbolPtrSection;4640const LaSymbolPtrSection = synthetic.LaSymbolPtrSection;
4640const LibStub = tapi.LibStub;
4641const Liveness = @import("../Liveness.zig");4641const Liveness = @import("../Liveness.zig");
4642const LlvmObject = @import("../codegen/llvm.zig").Object;4642const LlvmObject = @import("../codegen/llvm.zig").Object;
4643const Md5 = std.crypto.hash.Md5;4643const Md5 = std.crypto.hash.Md5;
src/link/MachO/Dylib.zig+4-1
...@@ -270,7 +270,10 @@ fn parseTbd(self: *Dylib, macho_file: *MachO) !void {...@@ -270,7 +270,10 @@ fn parseTbd(self: *Dylib, macho_file: *MachO) !void {
270 log.debug("parsing dylib from stub: {s}", .{self.path});270 log.debug("parsing dylib from stub: {s}", .{self.path});
271271
272 const file = macho_file.getFileHandle(self.file_handle);272 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 };
274 defer lib_stub.deinit();277 defer lib_stub.deinit();
275 const umbrella_lib = lib_stub.inner[0];278 const umbrella_lib = lib_stub.inner[0];
276279
src/link/tapi.zig+9-3
...@@ -129,8 +129,8 @@ pub const Tbd = union(enum) {...@@ -129,8 +129,8 @@ pub const Tbd = union(enum) {
129129
130pub const TapiError = error{130pub const TapiError = error{
131 NotLibStub,131 NotLibStub,
132 FileTooBig,132 InputOutput,
133} || yaml.YamlError || std.fs.File.ReadError;133} || yaml.YamlError || std.fs.File.PReadError;
134134
135pub const LibStub = struct {135pub const LibStub = struct {
136 /// Underlying memory for stub's contents.136 /// Underlying memory for stub's contents.
...@@ -140,8 +140,14 @@ pub const LibStub = struct {...@@ -140,8 +140,14 @@ pub const LibStub = struct {
140 inner: []Tbd,140 inner: []Tbd,
141141
142 pub fn loadFromFile(allocator: Allocator, file: fs.File) TapiError!LibStub {142 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);
144 defer allocator.free(source);148 defer allocator.free(source);
149 const amt = try file.preadAll(source, 0);
150 if (amt != filesize) return error.InputOutput;
145151
146 var lib_stub = LibStub{152 var lib_stub = LibStub{
147 .yaml = try Yaml.load(allocator, source),153 .yaml = try Yaml.load(allocator, source),