aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2024-07-23 10:33:00 +0200
committerJakub Konka <kubkon@jakubkonka.com>2024-07-23 10:33:02 +0200
commitf1af53f68ec629ca091452aeeeb2f7f7596e63b7 (patch)
treedaf6ffa68ea9aeb1b98738b1fe3e9008d176e320 /src
parent7c37c55161b718ff3df0b6c76480a4d538519a84 (diff)
downloadzig-f1af53f68ec629ca091452aeeeb2f7f7596e63b7.tar.gz
zig-f1af53f68ec629ca091452aeeeb2f7f7596e63b7.zip
macho: use pread syscall when loading tapi file
This avoids mixing preads with reads which do not mix well especially on Windows.
Diffstat (limited to 'src')
-rw-r--r--src/link/MachO.zig2
-rw-r--r--src/link/MachO/Dylib.zig5
-rw-r--r--src/link/tapi.zig12
3 files changed, 14 insertions, 5 deletions
diff --git a/src/link/MachO.zig b/src/link/MachO.zig
index 45fdac3640..aa7a2a96aa 100644
--- a/src/link/MachO.zig
+++ b/src/link/MachO.zig
@@ -914,6 +914,7 @@ fn parseInputFileWorker(self: *MachO, file: File) void {
switch (err) {
error.MalformedObject,
error.MalformedDylib,
+ error.MalformedTbd,
error.InvalidCpuArch,
error.InvalidTarget,
=> {}, // already reported
@@ -4637,7 +4638,6 @@ const ObjcStubsSection = synthetic.ObjcStubsSection;
const Object = @import("MachO/Object.zig");
const LazyBind = bind.LazyBind;
const LaSymbolPtrSection = synthetic.LaSymbolPtrSection;
-const LibStub = tapi.LibStub;
const Liveness = @import("../Liveness.zig");
const LlvmObject = @import("../codegen/llvm.zig").Object;
const Md5 = std.crypto.hash.Md5;
diff --git a/src/link/MachO/Dylib.zig b/src/link/MachO/Dylib.zig
index decaf054a3..ce12397be4 100644
--- a/src/link/MachO/Dylib.zig
+++ b/src/link/MachO/Dylib.zig
@@ -270,7 +270,10 @@ fn parseTbd(self: *Dylib, macho_file: *MachO) !void {
log.debug("parsing dylib from stub: {s}", .{self.path});
const file = macho_file.getFileHandle(self.file_handle);
- var lib_stub = LibStub.loadFromFile(gpa, file) catch return error.NotLibStub;
+ var lib_stub = LibStub.loadFromFile(gpa, file) catch |err| {
+ try macho_file.reportParseError2(self.index, "failed to parse TBD file: {s}", .{@errorName(err)});
+ return error.MalformedTbd;
+ };
defer lib_stub.deinit();
const umbrella_lib = lib_stub.inner[0];
diff --git a/src/link/tapi.zig b/src/link/tapi.zig
index 6fc62e585d..1ebd006636 100644
--- a/src/link/tapi.zig
+++ b/src/link/tapi.zig
@@ -129,8 +129,8 @@ pub const Tbd = union(enum) {
pub const TapiError = error{
NotLibStub,
- FileTooBig,
-} || yaml.YamlError || std.fs.File.ReadError;
+ InputOutput,
+} || yaml.YamlError || std.fs.File.PReadError;
pub const LibStub = struct {
/// Underlying memory for stub's contents.
@@ -140,8 +140,14 @@ pub const LibStub = struct {
inner: []Tbd,
pub fn loadFromFile(allocator: Allocator, file: fs.File) TapiError!LibStub {
- const source = try file.readToEndAlloc(allocator, std.math.maxInt(u32));
+ const filesize = blk: {
+ const stat = file.stat() catch break :blk std.math.maxInt(u32);
+ break :blk @min(stat.size, std.math.maxInt(u32));
+ };
+ const source = try allocator.alloc(u8, filesize);
defer allocator.free(source);
+ const amt = try file.preadAll(source, 0);
+ if (amt != filesize) return error.InputOutput;
var lib_stub = LibStub{
.yaml = try Yaml.load(allocator, source),