authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-28 17:34:10+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-29 11:40:20+02:00
log2473ccc3358a33c0827ec7f4ea2ecfe18a1055ec
tree68f5abf0836c542cbb9f7a18d8a93071813ea9e5
parent1820aed786a2bb61a6526873e7a8ddf47d45e9fd

macho: create an explicit error set for parse functions


4 files changed, 43 insertions(+), 23 deletions(-)

src/link/MachO.zig+32-20
......@@ -419,6 +419,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
419419 &dependent_libs,
420420 &parse_error_ctx,
421421 ) catch |err| switch (err) {
422 error.DylibAlreadyExists => {},
422423 error.UnknownFileType => try self.reportParseError(path, "unknown file type", .{}),
423424 error.MissingArchFatLib => try self.reportParseError(
424425 path,
......@@ -723,6 +724,22 @@ fn resolveLib(
723724 return full_path;
724725}
725726
727const ParseError = error{
728 UnknownFileType,
729 MissingArchFatLib,
730 InvalidArch,
731 DylibAlreadyExists,
732 IncompatibleDylibVersion,
733 OutOfMemory,
734 Overflow,
735 InputOutput,
736 MalformedArchive,
737 NotLibStub,
738 EndOfStream,
739 FileSystem,
740 NotSupported,
741} || std.os.SeekError || std.fs.File.OpenError || std.fs.File.ReadError || tapi.TapiError;
742
726743pub fn parsePositional(
727744 self: *MachO,
728745 file: std.fs.File,
......@@ -730,7 +747,7 @@ pub fn parsePositional(
730747 must_link: bool,
731748 dependent_libs: anytype,
732749 error_ctx: anytype,
733) !void {
750) ParseError!void {
734751 const tracy = trace(@src());
735752 defer tracy.end();
736753
......@@ -750,7 +767,7 @@ fn parseObject(
750767 file: std.fs.File,
751768 path: []const u8,
752769 error_ctx: anytype,
753) !void {
770) ParseError!void {
754771 const tracy = trace(@src());
755772 defer tracy.end();
756773
......@@ -793,7 +810,7 @@ pub fn parseLibrary(
793810 must_link: bool,
794811 dependent_libs: anytype,
795812 error_ctx: anytype,
796) !void {
813) ParseError!void {
797814 const tracy = trace(@src());
798815 defer tracy.end();
799816
......@@ -829,7 +846,7 @@ pub fn parseLibrary(
829846 }
830847}
831848
832pub fn parseFatLibrary(self: *MachO, file: std.fs.File, cpu_arch: std.Target.Cpu.Arch) !u64 {
849pub fn parseFatLibrary(self: *MachO, file: std.fs.File, cpu_arch: std.Target.Cpu.Arch) ParseError!u64 {
833850 _ = self;
834851 var buffer: [2]fat.Arch = undefined;
835852 const fat_archs = try fat.parseArchs(file, &buffer);
......@@ -846,7 +863,7 @@ fn parseArchive(
846863 must_link: bool,
847864 cpu_arch: std.Target.Cpu.Arch,
848865 error_ctx: anytype,
849) !void {
866) ParseError!void {
850867 const gpa = self.base.allocator;
851868
852869 // We take ownership of the file so that we can store it for the duration of symbol resolution.
......@@ -915,7 +932,7 @@ fn parseDylib(
915932 dependent_libs: anytype,
916933 dylib_options: DylibOpts,
917934 error_ctx: anytype,
918) !void {
935) ParseError!void {
919936 const gpa = self.base.allocator;
920937 const self_cpu_arch = self.base.options.target.cpu.arch;
921938
......@@ -948,13 +965,10 @@ fn parseDylib(
948965
949966 // TODO verify platform
950967
951 self.addDylib(dylib, .{
968 try self.addDylib(dylib, .{
952969 .needed = dylib_options.needed,
953970 .weak = dylib_options.weak,
954 }) catch |err| switch (err) {
955 error.DylibAlreadyExists => dylib.deinit(gpa),
956 else => |e| return e,
957 };
971 });
958972}
959973
960974fn parseLibStub(
......@@ -963,7 +977,7 @@ fn parseLibStub(
963977 path: []const u8,
964978 dependent_libs: anytype,
965979 dylib_options: DylibOpts,
966) !void {
980) ParseError!void {
967981 const gpa = self.base.allocator;
968982 var lib_stub = try LibStub.loadFromFile(gpa, file);
969983 defer lib_stub.deinit();
......@@ -984,16 +998,13 @@ fn parseLibStub(
984998 path,
985999 );
9861000
987 self.addDylib(dylib, .{
1001 try self.addDylib(dylib, .{
9881002 .needed = dylib_options.needed,
9891003 .weak = dylib_options.weak,
990 }) catch |err| switch (err) {
991 error.DylibAlreadyExists => dylib.deinit(gpa),
992 else => |e| return e,
993 };
1004 });
9941005}
9951006
996fn addDylib(self: *MachO, dylib: Dylib, dylib_options: DylibOpts) !void {
1007fn addDylib(self: *MachO, dylib: Dylib, dylib_options: DylibOpts) ParseError!void {
9971008 if (dylib_options.id) |id| {
9981009 if (dylib.id.?.current_version < id.compatibility_version) {
9991010 // TODO convert into an error
......@@ -1022,7 +1033,7 @@ fn addDylib(self: *MachO, dylib: Dylib, dylib_options: DylibOpts) !void {
10221033 }
10231034}
10241035
1025pub fn parseDependentLibs(self: *MachO, dependent_libs: anytype, error_ctx: anytype) !void {
1036pub fn parseDependentLibs(self: *MachO, dependent_libs: anytype, error_ctx: anytype) ParseError!void {
10261037 const tracy = trace(@src());
10271038 defer tracy.end();
10281039
......@@ -5145,6 +5156,7 @@ const link = @import("../link.zig");
51455156const llvm_backend = @import("../codegen/llvm.zig");
51465157const load_commands = @import("MachO/load_commands.zig");
51475158const stubs = @import("MachO/stubs.zig");
5159const tapi = @import("tapi.zig");
51485160const target_util = @import("../target.zig");
51495161const thunks = @import("MachO/thunks.zig");
51505162const trace = @import("../tracy.zig").trace;
......@@ -5162,7 +5174,7 @@ const DwarfInfo = @import("MachO/DwarfInfo.zig");
51625174const Dylib = @import("MachO/Dylib.zig");
51635175const File = link.File;
51645176const Object = @import("MachO/Object.zig");
5165const LibStub = @import("tapi.zig").LibStub;
5177const LibStub = tapi.LibStub;
51665178const Liveness = @import("../Liveness.zig");
51675179const LlvmObject = @import("../codegen/llvm.zig").Object;
51685180const Md5 = std.crypto.hash.Md5;
src/link/MachO/Dylib.zig+1-1
......@@ -320,7 +320,7 @@ pub fn parseFromStub(
320320 dependent_libs: anytype,
321321 name: []const u8,
322322) !void {
323 if (lib_stub.inner.len == 0) return error.EmptyStubFile;
323 if (lib_stub.inner.len == 0) return error.NotLibStub;
324324
325325 log.debug("parsing shared library from stub '{s}'", .{name});
326326
src/link/MachO/zld.zig+2
......@@ -361,6 +361,7 @@ pub fn linkWithZld(
361361 &dependent_libs,
362362 &parse_error_ctx,
363363 ) catch |err| switch (err) {
364 error.DylibAlreadyExists => {},
364365 error.UnknownFileType => try macho_file.reportParseError(obj.path, "unknown file type", .{}),
365366 error.MissingArchFatLib => try macho_file.reportParseError(
366367 obj.path,
......@@ -392,6 +393,7 @@ pub fn linkWithZld(
392393 &dependent_libs,
393394 &parse_error_ctx,
394395 ) catch |err| switch (err) {
396 error.DylibAlreadyExists => {},
395397 error.UnknownFileType => try macho_file.reportParseError(path, "unknown file type", .{}),
396398 error.MissingArchFatLib => try macho_file.reportParseError(
397399 path,
src/link/tapi.zig+8-2
......@@ -2,9 +2,10 @@ const std = @import("std");
22const fs = std.fs;
33const mem = std.mem;
44const log = std.log.scoped(.tapi);
5const yaml = @import("tapi/yaml.zig");
56
67const Allocator = mem.Allocator;
7const Yaml = @import("tapi/yaml.zig").Yaml;
8const Yaml = yaml.Yaml;
89
910const VersionField = union(enum) {
1011 string: []const u8,
......@@ -102,6 +103,11 @@ pub const Tbd = union(enum) {
102103 }
103104};
104105
106pub const TapiError = error{
107 NotLibStub,
108 FileTooBig,
109} || yaml.YamlError || std.fs.File.ReadError;
110
105111pub const LibStub = struct {
106112 /// Underlying memory for stub's contents.
107113 yaml: Yaml,
......@@ -109,7 +115,7 @@ pub const LibStub = struct {
109115 /// Typed contents of the tbd file.
110116 inner: []Tbd,
111117
112 pub fn loadFromFile(allocator: Allocator, file: fs.File) !LibStub {
118 pub fn loadFromFile(allocator: Allocator, file: fs.File) TapiError!LibStub {
113119 const source = try file.readToEndAlloc(allocator, std.math.maxInt(u32));
114120 defer allocator.free(source);
115121