authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-29 22:16:48+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-29 22:16:48+02:00
log7e167537c032133b416f36425e29c028c10a9462
treea637fca84c61c1a100d235f88d19ca7577545610
parent79b3285aa216350e0c2ff18436a169af69e4570f

macho: simplify handling and reporting parsing errors


4 files changed, 136 insertions(+), 134 deletions(-)

src/link/MachO.zig+100-103
......@@ -401,35 +401,23 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
401401 parent: u16,
402402 }, .Dynamic).init(arena);
403403
404 var parse_error_ctx: struct {
405 detected_arch: std.Target.Cpu.Arch,
406 detected_platform: ?Platform,
407 detected_stub_targets: []const []const u8,
408 } = .{
409 .detected_arch = undefined,
410 .detected_platform = null,
411 .detected_stub_targets = &[0][]const u8{},
412 };
413 defer {
414 for (parse_error_ctx.detected_stub_targets) |target| self.base.allocator.free(target);
415 self.base.allocator.free(parse_error_ctx.detected_stub_targets);
416 }
404 var parse_ctx = ParseErrorCtx.init(arena);
417405
418406 for (libs.keys(), libs.values()) |path, lib| {
419407 const in_file = try std.fs.cwd().openFile(path, .{});
420408 defer in_file.close();
421
409 defer parse_ctx.detected_targets.clearRetainingCapacity();
422410 self.parseLibrary(
423411 in_file,
424412 path,
425413 lib,
426414 false,
427415 &dependent_libs,
428 &parse_error_ctx,
429 ) catch |err| try self.handleAndReportParseError(path, err, parse_error_ctx);
416 &parse_ctx,
417 ) catch |err| try self.handleAndReportParseError(path, err, &parse_ctx);
430418 }
431419
432 self.parseDependentLibs(&dependent_libs, &parse_error_ctx) catch |err| {
420 self.parseDependentLibs(&dependent_libs, &parse_ctx) catch |err| {
433421 // TODO convert to error
434422 log.err("parsing dependent libraries failed with err {s}", .{@errorName(err)});
435423 };
......@@ -727,9 +715,7 @@ fn resolveLib(
727715
728716const ParseError = error{
729717 UnknownFileType,
730 MissingArchFatLib,
731718 InvalidTarget,
732 InvalidLibStubTargets,
733719 DylibAlreadyExists,
734720 IncompatibleDylibVersion,
735721 OutOfMemory,
......@@ -748,19 +734,19 @@ pub fn parsePositional(
748734 path: []const u8,
749735 must_link: bool,
750736 dependent_libs: anytype,
751 error_ctx: anytype,
737 ctx: *ParseErrorCtx,
752738) ParseError!void {
753739 const tracy = trace(@src());
754740 defer tracy.end();
755741
756742 if (Object.isObject(file)) {
757 try self.parseObject(file, path, error_ctx);
743 try self.parseObject(file, path, ctx);
758744 } else {
759745 try self.parseLibrary(file, path, .{
760746 .path = null,
761747 .needed = false,
762748 .weak = false,
763 }, must_link, dependent_libs, error_ctx);
749 }, must_link, dependent_libs, ctx);
764750 }
765751}
766752
......@@ -768,7 +754,7 @@ fn parseObject(
768754 self: *MachO,
769755 file: std.fs.File,
770756 path: []const u8,
771 error_ctx: anytype,
757 ctx: *ParseErrorCtx,
772758) ParseError!void {
773759 const tracy = trace(@src());
774760 defer tracy.end();
......@@ -790,20 +776,21 @@ fn parseObject(
790776 errdefer object.deinit(gpa);
791777 try object.parse(gpa);
792778
793 const cpu_arch: std.Target.Cpu.Arch = switch (object.header.cputype) {
779 const detected_cpu_arch: std.Target.Cpu.Arch = switch (object.header.cputype) {
794780 macho.CPU_TYPE_ARM64 => .aarch64,
795781 macho.CPU_TYPE_X86_64 => .x86_64,
796782 else => unreachable,
797783 };
798 error_ctx.detected_arch = cpu_arch;
799
800 if (object.getPlatform()) |platform| {
801 error_ctx.detected_platform = platform;
802 }
784 const detected_platform = object.getPlatform();
785 const this_cpu_arch = self.base.options.target.cpu.arch;
786 const this_platform = Platform.fromTarget(self.base.options.target);
803787
804 if (self.base.options.target.cpu.arch != cpu_arch) return error.InvalidTarget;
805 if (error_ctx.detected_platform) |platform| {
806 if (!Platform.fromTarget(self.base.options.target).eqlTarget(platform)) return error.InvalidTarget;
788 if (this_cpu_arch != detected_cpu_arch or
789 (detected_platform != null and !detected_platform.?.eqlTarget(this_platform)))
790 {
791 const platform = detected_platform orelse this_platform;
792 try ctx.detected_targets.append(try platform.allocPrintTarget(ctx.arena, detected_cpu_arch));
793 return error.InvalidTarget;
807794 }
808795
809796 try self.objects.append(gpa, object);
......@@ -816,48 +803,61 @@ pub fn parseLibrary(
816803 lib: link.SystemLib,
817804 must_link: bool,
818805 dependent_libs: anytype,
819 error_ctx: anytype,
806 ctx: *ParseErrorCtx,
820807) ParseError!void {
821808 const tracy = trace(@src());
822809 defer tracy.end();
823810
824811 if (fat.isFatLibrary(file)) {
825 const offset = try self.parseFatLibrary(file, self.base.options.target.cpu.arch);
812 const offset = try self.parseFatLibrary(file, self.base.options.target.cpu.arch, ctx);
826813 try file.seekTo(offset);
827814
828815 if (Archive.isArchive(file, offset)) {
829 try self.parseArchive(path, offset, must_link, error_ctx);
816 try self.parseArchive(path, offset, must_link, ctx);
830817 } else if (Dylib.isDylib(file, offset)) {
831818 try self.parseDylib(file, path, offset, dependent_libs, .{
832819 .needed = lib.needed,
833820 .weak = lib.weak,
834 }, error_ctx);
821 }, ctx);
835822 } else return error.UnknownFileType;
836823 } else if (Archive.isArchive(file, 0)) {
837 try self.parseArchive(path, 0, must_link, error_ctx);
824 try self.parseArchive(path, 0, must_link, ctx);
838825 } else if (Dylib.isDylib(file, 0)) {
839826 try self.parseDylib(file, path, 0, dependent_libs, .{
840827 .needed = lib.needed,
841828 .weak = lib.weak,
842 }, error_ctx);
829 }, ctx);
843830 } else {
844831 self.parseLibStub(file, path, dependent_libs, .{
845832 .needed = lib.needed,
846833 .weak = lib.weak,
847 }, error_ctx) catch |err| switch (err) {
834 }, ctx) catch |err| switch (err) {
848835 error.NotLibStub, error.UnexpectedToken => return error.UnknownFileType,
849836 else => |e| return e,
850837 };
851838 }
852839}
853840
854pub fn parseFatLibrary(self: *MachO, file: std.fs.File, cpu_arch: std.Target.Cpu.Arch) ParseError!u64 {
855 _ = self;
856 var buffer: [2]fat.Arch = undefined;
857 const fat_archs = try fat.parseArchs(file, &buffer);
841pub fn parseFatLibrary(
842 self: *MachO,
843 file: std.fs.File,
844 cpu_arch: std.Target.Cpu.Arch,
845 ctx: *ParseErrorCtx,
846) ParseError!u64 {
847 const gpa = self.base.allocator;
848
849 const fat_archs = try fat.parseArchs(gpa, file);
850 defer gpa.free(fat_archs);
851
858852 const offset = for (fat_archs) |arch| {
859853 if (arch.tag == cpu_arch) break arch.offset;
860 } else return error.MissingArchFatLib;
854 } else {
855 try ctx.detected_targets.ensureTotalCapacityPrecise(fat_archs.len);
856 for (fat_archs) |arch| {
857 ctx.detected_targets.appendAssumeCapacity(try ctx.arena.dupe(u8, @tagName(arch.tag)));
858 }
859 return error.InvalidTarget;
860 };
861861 return offset;
862862}
863863
......@@ -866,7 +866,7 @@ fn parseArchive(
866866 path: []const u8,
867867 fat_offset: u64,
868868 must_link: bool,
869 error_ctx: anytype,
869 ctx: *ParseErrorCtx,
870870) ParseError!void {
871871 const gpa = self.base.allocator;
872872
......@@ -892,20 +892,21 @@ fn parseArchive(
892892 var object = try archive.parseObject(gpa, off); // TODO we are doing all this work to pull the header only!
893893 defer object.deinit(gpa);
894894
895 const cpu_arch: std.Target.Cpu.Arch = switch (object.header.cputype) {
895 const detected_cpu_arch: std.Target.Cpu.Arch = switch (object.header.cputype) {
896896 macho.CPU_TYPE_ARM64 => .aarch64,
897897 macho.CPU_TYPE_X86_64 => .x86_64,
898898 else => unreachable,
899899 };
900 error_ctx.detected_arch = cpu_arch;
901
902 if (object.getPlatform()) |platform| {
903 error_ctx.detected_platform = platform;
904 }
900 const detected_platform = object.getPlatform();
901 const this_cpu_arch = self.base.options.target.cpu.arch;
902 const this_platform = Platform.fromTarget(self.base.options.target);
905903
906 if (self.base.options.target.cpu.arch != cpu_arch) return error.InvalidTarget;
907 if (error_ctx.detected_platform) |platform| {
908 if (!Platform.fromTarget(self.base.options.target).eqlTarget(platform)) return error.InvalidTarget;
904 if (this_cpu_arch != detected_cpu_arch or
905 (detected_platform != null and !detected_platform.?.eqlTarget(this_platform)))
906 {
907 const platform = detected_platform orelse this_platform;
908 try ctx.detected_targets.append(try platform.allocPrintTarget(gpa, detected_cpu_arch));
909 return error.InvalidTarget;
909910 }
910911 }
911912
......@@ -941,7 +942,7 @@ fn parseDylib(
941942 offset: u64,
942943 dependent_libs: anytype,
943944 dylib_options: DylibOpts,
944 error_ctx: anytype,
945 ctx: *ParseErrorCtx,
945946) ParseError!void {
946947 const gpa = self.base.allocator;
947948 const file_stat = try file.stat();
......@@ -961,20 +962,21 @@ fn parseDylib(
961962 contents,
962963 );
963964
964 const cpu_arch: std.Target.Cpu.Arch = switch (dylib.header.?.cputype) {
965 const detected_cpu_arch: std.Target.Cpu.Arch = switch (dylib.header.?.cputype) {
965966 macho.CPU_TYPE_ARM64 => .aarch64,
966967 macho.CPU_TYPE_X86_64 => .x86_64,
967968 else => unreachable,
968969 };
969 error_ctx.detected_arch = cpu_arch;
970 const detected_platform = dylib.getPlatform(contents);
971 const this_cpu_arch = self.base.options.target.cpu.arch;
972 const this_platform = Platform.fromTarget(self.base.options.target);
970973
971 if (dylib.getPlatform(contents)) |platform| {
972 error_ctx.detected_platform = platform;
973 }
974
975 if (self.base.options.target.cpu.arch != cpu_arch) return error.InvalidTarget;
976 if (error_ctx.detected_platform) |platform| {
977 if (!Platform.fromTarget(self.base.options.target).eqlTarget(platform)) return error.InvalidTarget;
974 if (this_cpu_arch != detected_cpu_arch or
975 (detected_platform != null and !detected_platform.?.eqlTarget(this_platform)))
976 {
977 const platform = detected_platform orelse this_platform;
978 try ctx.detected_targets.append(try platform.allocPrintTarget(ctx.arena, detected_cpu_arch));
979 return error.InvalidTarget;
978980 }
979981
980982 try self.addDylib(dylib, .{
......@@ -989,7 +991,7 @@ fn parseLibStub(
989991 path: []const u8,
990992 dependent_libs: anytype,
991993 dylib_options: DylibOpts,
992 error_ctx: anytype,
994 ctx: *ParseErrorCtx,
993995) ParseError!void {
994996 const gpa = self.base.allocator;
995997 var lib_stub = try LibStub.loadFromFile(gpa, file);
......@@ -1004,12 +1006,17 @@ fn parseLibStub(
10041006
10051007 const first_tbd = lib_stub.inner[0];
10061008 const targets = try first_tbd.targets(gpa);
1009 defer {
1010 for (targets) |t| gpa.free(t);
1011 gpa.free(targets);
1012 }
10071013 if (!matcher.matchesTarget(targets)) {
1008 error_ctx.detected_stub_targets = targets;
1009 return error.InvalidLibStubTargets;
1014 try ctx.detected_targets.ensureUnusedCapacity(targets.len);
1015 for (targets) |t| {
1016 ctx.detected_targets.appendAssumeCapacity(try ctx.arena.dupe(u8, t));
1017 }
1018 return error.InvalidTarget;
10101019 }
1011 for (targets) |t| gpa.free(t);
1012 gpa.free(targets);
10131020 }
10141021
10151022 var dylib = Dylib{ .weak = dylib_options.weak };
......@@ -1059,7 +1066,7 @@ fn addDylib(self: *MachO, dylib: Dylib, dylib_options: DylibOpts) ParseError!voi
10591066 }
10601067}
10611068
1062pub fn parseDependentLibs(self: *MachO, dependent_libs: anytype, error_ctx: anytype) ParseError!void {
1069pub fn parseDependentLibs(self: *MachO, dependent_libs: anytype, ctx: *ParseErrorCtx) ParseError!void {
10631070 const tracy = trace(@src());
10641071 defer tracy.end();
10651072
......@@ -1105,7 +1112,7 @@ pub fn parseDependentLibs(self: *MachO, dependent_libs: anytype, error_ctx: anyt
11051112 log.debug("trying dependency at fully resolved path {s}", .{full_path});
11061113
11071114 const offset: u64 = if (fat.isFatLibrary(file)) blk: {
1108 const offset = try self.parseFatLibrary(file, self.base.options.target.cpu.arch);
1115 const offset = try self.parseFatLibrary(file, self.base.options.target.cpu.arch, ctx);
11091116 try file.seekTo(offset);
11101117 break :blk offset;
11111118 } else 0;
......@@ -1114,12 +1121,12 @@ pub fn parseDependentLibs(self: *MachO, dependent_libs: anytype, error_ctx: anyt
11141121 try self.parseDylib(file, full_path, offset, dependent_libs, .{
11151122 .dependent = true,
11161123 .weak = weak,
1117 }, error_ctx);
1124 }, ctx);
11181125 } else {
11191126 self.parseLibStub(file, full_path, dependent_libs, .{
11201127 .dependent = true,
11211128 .weak = weak,
1122 }, error_ctx) catch |err| switch (err) {
1129 }, ctx) catch |err| switch (err) {
11231130 error.NotLibStub, error.UnexpectedToken => continue,
11241131 else => |e| return e,
11251132 };
......@@ -4845,50 +4852,40 @@ pub fn getSectionPrecedence(header: macho.section_64) u8 {
48454852 return (@as(u8, @intCast(segment_precedence)) << 4) + section_precedence;
48464853}
48474854
4848pub fn handleAndReportParseError(self: *MachO, path: []const u8, err: ParseError, parse_error_ctx: anytype) !void {
4855pub const ParseErrorCtx = struct {
4856 arena: Allocator,
4857 detected_targets: std.ArrayList([]const u8),
4858
4859 pub fn init(arena: Allocator) ParseErrorCtx {
4860 return .{ .arena = arena, .detected_targets = std.ArrayList([]const u8).init(arena) };
4861 }
4862};
4863
4864pub fn handleAndReportParseError(
4865 self: *MachO,
4866 path: []const u8,
4867 err: ParseError,
4868 ctx: *const ParseErrorCtx,
4869) !void {
48494870 const cpu_arch = self.base.options.target.cpu.arch;
48504871 switch (err) {
48514872 error.DylibAlreadyExists => {},
48524873 error.UnknownFileType => try self.reportParseError(path, "unknown file type", .{}),
4853 error.MissingArchFatLib => try self.reportParseError(
4854 path,
4855 "missing architecture in universal file, expected '{s}'",
4856 .{@tagName(cpu_arch)},
4857 ),
4858 error.InvalidTarget => if (parse_error_ctx.detected_platform) |platform| {
4859 try self.reportParseError(path, "invalid target '{s}-{}', expected '{s}-{}'", .{
4860 @tagName(parse_error_ctx.detected_arch),
4861 platform.fmtTarget(),
4862 @tagName(cpu_arch),
4863 Platform.fromTarget(self.base.options.target).fmtTarget(),
4864 });
4865 } else {
4866 try self.reportParseError(
4867 path,
4868 "invalid architecture '{s}', expected '{s}'",
4869 .{ @tagName(parse_error_ctx.detected_arch), @tagName(cpu_arch) },
4870 );
4871 },
4872 error.InvalidLibStubTargets => {
4874 error.InvalidTarget => {
48734875 var targets_string = std.ArrayList(u8).init(self.base.allocator);
48744876 defer targets_string.deinit();
48754877 try targets_string.writer().writeAll("(");
4876 for (parse_error_ctx.detected_stub_targets) |t| {
4878 for (ctx.detected_targets.items) |t| {
48774879 try targets_string.writer().print("{s}, ", .{t});
48784880 }
48794881 try targets_string.resize(targets_string.items.len - 2);
48804882 try targets_string.writer().writeAll(")");
4881 try self.reportParseError(path, "invalid targets '{s}', expected '{s}-{}'", .{
4883 try self.reportParseError(path, "invalid target: expected '{}', but found '{s}'", .{
4884 Platform.fromTarget(self.base.options.target).fmtTarget(cpu_arch),
48824885 targets_string.items,
4883 @tagName(cpu_arch),
4884 Platform.fromTarget(self.base.options.target).fmtTarget(),
48854886 });
48864887 },
4887 else => |e| try self.reportParseError(
4888 path,
4889 "parsing positional argument failed with error '{s}'",
4890 .{@errorName(e)},
4891 ),
4888 else => |e| try self.reportParseError(path, "{s}: parsing object failed", .{@errorName(e)}),
48924889 }
48934890}
48944891
src/link/MachO/fat.zig+9-5
......@@ -10,12 +10,15 @@ pub const Arch = struct {
1010 offset: u64,
1111};
1212
13pub fn parseArchs(file: std.fs.File, buffer: *[2]Arch) ![]const Arch {
13/// Caller owns the memory.
14pub fn parseArchs(gpa: Allocator, file: std.fs.File) ![]const Arch {
1415 const reader = file.reader();
1516 const fat_header = try reader.readStructBig(macho.fat_header);
1617 assert(fat_header.magic == macho.FAT_MAGIC);
1718
18 var count: usize = 0;
19 var archs = try std.ArrayList(Arch).initCapacity(gpa, fat_header.nfat_arch);
20 defer archs.deinit();
21
1922 var fat_arch_index: u32 = 0;
2023 while (fat_arch_index < fat_header.nfat_arch) : (fat_arch_index += 1) {
2124 const fat_arch = try reader.readStructBig(macho.fat_arch);
......@@ -26,11 +29,11 @@ pub fn parseArchs(file: std.fs.File, buffer: *[2]Arch) ![]const Arch {
2629 macho.CPU_TYPE_X86_64 => if (fat_arch.cpusubtype == macho.CPU_SUBTYPE_X86_64_ALL) .x86_64 else continue,
2730 else => continue,
2831 };
29 buffer[count] = .{ .tag = arch, .offset = fat_arch.offset };
30 count += 1;
32
33 archs.appendAssumeCapacity(.{ .tag = arch, .offset = fat_arch.offset });
3134 }
3235
33 return buffer[0..count];
36 return archs.toOwnedSlice();
3437}
3538
3639const std = @import("std");
......@@ -38,3 +41,4 @@ const assert = std.debug.assert;
3841const log = std.log.scoped(.archive);
3942const macho = std.macho;
4043const mem = std.mem;
44const Allocator = mem.Allocator;
src/link/MachO/load_commands.zig+19-6
......@@ -384,24 +384,37 @@ pub const Platform = struct {
384384 return false;
385385 }
386386
387 pub fn fmtTarget(plat: Platform) std.fmt.Formatter(formatTarget) {
388 return .{ .data = plat };
387 pub fn fmtTarget(plat: Platform, cpu_arch: std.Target.Cpu.Arch) std.fmt.Formatter(formatTarget) {
388 return .{ .data = .{ .platform = plat, .cpu_arch = cpu_arch } };
389389 }
390390
391 const FmtCtx = struct {
392 platform: Platform,
393 cpu_arch: std.Target.Cpu.Arch,
394 };
395
391396 pub fn formatTarget(
392 plat: Platform,
397 ctx: FmtCtx,
393398 comptime unused_fmt_string: []const u8,
394399 options: std.fmt.FormatOptions,
395400 writer: anytype,
396401 ) !void {
397402 _ = unused_fmt_string;
398403 _ = options;
399 try writer.print("{s}", .{@tagName(plat.os_tag)});
400 if (plat.abi != .none) {
401 try writer.print("-{s}", .{@tagName(plat.abi)});
404 try writer.print("{s}-{s}", .{ @tagName(ctx.cpu_arch), @tagName(ctx.platform.os_tag) });
405 if (ctx.platform.abi != .none) {
406 try writer.print("-{s}", .{@tagName(ctx.platform.abi)});
402407 }
403408 }
404409
410 /// Caller owns the memory.
411 pub fn allocPrintTarget(plat: Platform, gpa: Allocator, cpu_arch: std.Target.Cpu.Arch) error{OutOfMemory}![]u8 {
412 var buffer = std.ArrayList(u8).init(gpa);
413 defer buffer.deinit();
414 try buffer.writer().print("{}", .{plat.fmtTarget(cpu_arch)});
415 return buffer.toOwnedSlice();
416 }
417
405418 pub fn eqlTarget(plat: Platform, other: Platform) bool {
406419 return plat.os_tag == other.os_tag and plat.abi == other.abi;
407420 }
src/link/MachO/zld.zig+8-20
......@@ -345,48 +345,36 @@ pub fn linkWithZld(
345345 parent: u16,
346346 }, .Dynamic).init(arena);
347347
348 var parse_error_ctx: struct {
349 detected_arch: std.Target.Cpu.Arch,
350 detected_platform: ?Platform,
351 detected_stub_targets: []const []const u8,
352 } = .{
353 .detected_arch = undefined,
354 .detected_platform = null,
355 .detected_stub_targets = &[0][]const u8{},
356 };
357 defer {
358 for (parse_error_ctx.detected_stub_targets) |t| gpa.free(t);
359 gpa.free(parse_error_ctx.detected_stub_targets);
360 }
348 var parse_ctx = MachO.ParseErrorCtx.init(arena);
361349
362350 for (positionals.items) |obj| {
363351 const in_file = try std.fs.cwd().openFile(obj.path, .{});
364352 defer in_file.close();
365
353 defer parse_ctx.detected_targets.clearRetainingCapacity();
366354 macho_file.parsePositional(
367355 in_file,
368356 obj.path,
369357 obj.must_link,
370358 &dependent_libs,
371 &parse_error_ctx,
372 ) catch |err| try macho_file.handleAndReportParseError(obj.path, err, parse_error_ctx);
359 &parse_ctx,
360 ) catch |err| try macho_file.handleAndReportParseError(obj.path, err, &parse_ctx);
373361 }
374362
375363 for (libs.keys(), libs.values()) |path, lib| {
376364 const in_file = try std.fs.cwd().openFile(path, .{});
377365 defer in_file.close();
378
366 defer parse_ctx.detected_targets.clearRetainingCapacity();
379367 macho_file.parseLibrary(
380368 in_file,
381369 path,
382370 lib,
383371 false,
384372 &dependent_libs,
385 &parse_error_ctx,
386 ) catch |err| try macho_file.handleAndReportParseError(path, err, parse_error_ctx);
373 &parse_ctx,
374 ) catch |err| try macho_file.handleAndReportParseError(path, err, &parse_ctx);
387375 }
388376
389 macho_file.parseDependentLibs(&dependent_libs, &parse_error_ctx) catch |err| {
377 macho_file.parseDependentLibs(&dependent_libs, &parse_ctx) catch |err| {
390378 // TODO convert to error
391379 log.err("parsing dependent libraries failed with err {s}", .{@errorName(err)});
392380 };