authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-30 20:16:37+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-30 20:16:37+02:00
log5144132320ae86320e9a6bf335bfbdccf52e2621
treee697b935a5aa549a8d85283a5ddf88ed4816f810
parent5806e761bb676cdd537308f6c4a197e42228416d

macho: report formatted error for unhandled symbol types


2 files changed, 44 insertions(+), 32 deletions(-)

src/link.zig-10
......@@ -698,14 +698,12 @@ pub const File = struct {
698698 CurrentWorkingDirectoryUnlinked,
699699 DivisionByZero,
700700 DllImportLibraryNotFound,
701 EmptyStubFile,
702701 ExpectedFuncType,
703702 FailedToEmit,
704703 FailedToResolveRelocationTarget,
705704 FileSystem,
706705 FilesOpenedWithWrongFlags,
707706 FlushFailure,
708 FrameworkNotFound,
709707 FunctionSignatureMismatch,
710708 GlobalTypeMismatch,
711709 HotSwapUnavailableOnHostOperatingSystem,
......@@ -722,14 +720,12 @@ pub const File = struct {
722720 LLD_LinkingIsTODO_ForSpirV,
723721 LibCInstallationMissingCRTDir,
724722 LibCInstallationNotAvailable,
725 LibraryNotFound,
726723 LinkingWithoutZigSourceUnimplemented,
727724 MalformedArchive,
728725 MalformedDwarf,
729726 MalformedSection,
730727 MemoryTooBig,
731728 MemoryTooSmall,
732 MismatchedCpuArchitecture,
733729 MissAlignment,
734730 MissingEndForBody,
735731 MissingEndForExpression,
......@@ -738,9 +734,7 @@ pub const File = struct {
738734 MissingSymbol,
739735 MissingTableSymbols,
740736 ModuleNameMismatch,
741 MultipleSymbolDefinitions,
742737 NoObjectsToLink,
743 NotObject,
744738 NotObjectFile,
745739 NotSupported,
746740 OutOfMemory,
......@@ -756,16 +750,12 @@ pub const File = struct {
756750 UnableToSpawnWasm,
757751 UnableToWriteArchive,
758752 UndefinedLocal,
759 /// TODO: merge with UndefinedSymbolReference
760753 UndefinedSymbol,
761 /// TODO: merge with UndefinedSymbol
762 UndefinedSymbolReference,
763754 Underflow,
764755 UnexpectedRemainder,
765756 UnexpectedTable,
766757 UnexpectedValue,
767758 UnhandledDwFormValue,
768 UnhandledSymbolType,
769759 UnknownFeature,
770760 Unseekable,
771761 UnsupportedCpuArchitecture,
src/link/MachO.zig+44-22
......@@ -1709,26 +1709,14 @@ fn resolveSymbolsInObject(self: *MachO, object_id: u32) !void {
17091709 while (sym_index < in_symtab.len) : (sym_index += 1) {
17101710 const sym = &object.symtab[sym_index];
17111711 const sym_name = object.getSymbolName(sym_index);
1712 const sym_with_loc = SymbolWithLoc{
1713 .sym_index = sym_index,
1714 .file = object_id + 1,
1715 };
17121716
1713 if (sym.stab()) {
1714 log.err("unhandled symbol type: stab", .{});
1715 log.err(" symbol '{s}'", .{sym_name});
1716 log.err(" first definition in '{s}'", .{object.name});
1717 return error.UnhandledSymbolType;
1718 }
1719
1720 if (sym.indr()) {
1721 log.err("unhandled symbol type: indirect", .{});
1722 log.err(" symbol '{s}'", .{sym_name});
1723 log.err(" first definition in '{s}'", .{object.name});
1724 return error.UnhandledSymbolType;
1725 }
1726
1727 if (sym.abs()) {
1728 log.err("unhandled symbol type: absolute", .{});
1729 log.err(" symbol '{s}'", .{sym_name});
1730 log.err(" first definition in '{s}'", .{object.name});
1731 return error.UnhandledSymbolType;
1717 if (sym.stab() or sym.indr() or sym.abs()) {
1718 try self.reportUnhandledSymbolType(sym_with_loc);
1719 continue;
17321720 }
17331721
17341722 if (sym.sect() and !sym.ext()) {
......@@ -4892,7 +4880,7 @@ pub fn handleAndReportParseError(
48924880 path: []const u8,
48934881 err: ParseError,
48944882 ctx: *const ParseErrorCtx,
4895) !void {
4883) error{OutOfMemory}!void {
48964884 const cpu_arch = self.base.options.target.cpu.arch;
48974885 switch (err) {
48984886 error.DylibAlreadyExists => {},
......@@ -4943,7 +4931,7 @@ fn reportDependencyError(
49434931 path: ?[]const u8,
49444932 comptime format: []const u8,
49454933 args: anytype,
4946) !void {
4934) error{OutOfMemory}!void {
49474935 const gpa = self.base.allocator;
49484936 try self.misc_errors.ensureUnusedCapacity(gpa, 1);
49494937 var notes = try std.ArrayList(File.ErrorMsg).initCapacity(gpa, 2);
......@@ -4958,7 +4946,12 @@ fn reportDependencyError(
49584946 });
49594947}
49604948
4961fn reportParseError(self: *MachO, path: []const u8, comptime format: []const u8, args: anytype) !void {
4949fn reportParseError(
4950 self: *MachO,
4951 path: []const u8,
4952 comptime format: []const u8,
4953 args: anytype,
4954) error{OutOfMemory}!void {
49624955 const gpa = self.base.allocator;
49634956 try self.misc_errors.ensureUnusedCapacity(gpa, 1);
49644957 var notes = try gpa.alloc(File.ErrorMsg, 1);
......@@ -5030,6 +5023,35 @@ fn reportSymbolCollision(
50305023 self.misc_errors.appendAssumeCapacity(err_msg);
50315024}
50325025
5026fn reportUnhandledSymbolType(self: *MachO, sym_with_loc: SymbolWithLoc) error{OutOfMemory}!void {
5027 const gpa = self.base.allocator;
5028 try self.misc_errors.ensureUnusedCapacity(gpa, 1);
5029
5030 const notes = try gpa.alloc(File.ErrorMsg, 1);
5031 errdefer gpa.free(notes);
5032
5033 const file = sym_with_loc.getFile().?;
5034 notes[0] = .{ .msg = try std.fmt.allocPrint(gpa, "defined in {s}", .{self.objects.items[file].name}) };
5035
5036 const sym = self.getSymbol(sym_with_loc);
5037 const sym_type = if (sym.stab())
5038 "stab"
5039 else if (sym.indr())
5040 "indirect"
5041 else if (sym.abs())
5042 "absolute"
5043 else
5044 unreachable;
5045
5046 self.misc_errors.appendAssumeCapacity(.{
5047 .msg = try std.fmt.allocPrint(gpa, "unhandled symbol type: '{s}' has type {s}", .{
5048 self.getSymbolName(sym_with_loc),
5049 sym_type,
5050 }),
5051 .notes = notes,
5052 });
5053}
5054
50335055/// Binary search
50345056pub fn bsearch(comptime T: type, haystack: []align(1) const T, predicate: anytype) usize {
50355057 if (!@hasDecl(@TypeOf(predicate), "predicate"))