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 {...@@ -698,14 +698,12 @@ pub const File = struct {
698 CurrentWorkingDirectoryUnlinked,698 CurrentWorkingDirectoryUnlinked,
699 DivisionByZero,699 DivisionByZero,
700 DllImportLibraryNotFound,700 DllImportLibraryNotFound,
701 EmptyStubFile,
702 ExpectedFuncType,701 ExpectedFuncType,
703 FailedToEmit,702 FailedToEmit,
704 FailedToResolveRelocationTarget,703 FailedToResolveRelocationTarget,
705 FileSystem,704 FileSystem,
706 FilesOpenedWithWrongFlags,705 FilesOpenedWithWrongFlags,
707 FlushFailure,706 FlushFailure,
708 FrameworkNotFound,
709 FunctionSignatureMismatch,707 FunctionSignatureMismatch,
710 GlobalTypeMismatch,708 GlobalTypeMismatch,
711 HotSwapUnavailableOnHostOperatingSystem,709 HotSwapUnavailableOnHostOperatingSystem,
...@@ -722,14 +720,12 @@ pub const File = struct {...@@ -722,14 +720,12 @@ pub const File = struct {
722 LLD_LinkingIsTODO_ForSpirV,720 LLD_LinkingIsTODO_ForSpirV,
723 LibCInstallationMissingCRTDir,721 LibCInstallationMissingCRTDir,
724 LibCInstallationNotAvailable,722 LibCInstallationNotAvailable,
725 LibraryNotFound,
726 LinkingWithoutZigSourceUnimplemented,723 LinkingWithoutZigSourceUnimplemented,
727 MalformedArchive,724 MalformedArchive,
728 MalformedDwarf,725 MalformedDwarf,
729 MalformedSection,726 MalformedSection,
730 MemoryTooBig,727 MemoryTooBig,
731 MemoryTooSmall,728 MemoryTooSmall,
732 MismatchedCpuArchitecture,
733 MissAlignment,729 MissAlignment,
734 MissingEndForBody,730 MissingEndForBody,
735 MissingEndForExpression,731 MissingEndForExpression,
...@@ -738,9 +734,7 @@ pub const File = struct {...@@ -738,9 +734,7 @@ pub const File = struct {
738 MissingSymbol,734 MissingSymbol,
739 MissingTableSymbols,735 MissingTableSymbols,
740 ModuleNameMismatch,736 ModuleNameMismatch,
741 MultipleSymbolDefinitions,
742 NoObjectsToLink,737 NoObjectsToLink,
743 NotObject,
744 NotObjectFile,738 NotObjectFile,
745 NotSupported,739 NotSupported,
746 OutOfMemory,740 OutOfMemory,
...@@ -756,16 +750,12 @@ pub const File = struct {...@@ -756,16 +750,12 @@ pub const File = struct {
756 UnableToSpawnWasm,750 UnableToSpawnWasm,
757 UnableToWriteArchive,751 UnableToWriteArchive,
758 UndefinedLocal,752 UndefinedLocal,
759 /// TODO: merge with UndefinedSymbolReference
760 UndefinedSymbol,753 UndefinedSymbol,
761 /// TODO: merge with UndefinedSymbol
762 UndefinedSymbolReference,
763 Underflow,754 Underflow,
764 UnexpectedRemainder,755 UnexpectedRemainder,
765 UnexpectedTable,756 UnexpectedTable,
766 UnexpectedValue,757 UnexpectedValue,
767 UnhandledDwFormValue,758 UnhandledDwFormValue,
768 UnhandledSymbolType,
769 UnknownFeature,759 UnknownFeature,
770 Unseekable,760 Unseekable,
771 UnsupportedCpuArchitecture,761 UnsupportedCpuArchitecture,
src/link/MachO.zig+44-22
...@@ -1709,26 +1709,14 @@ fn resolveSymbolsInObject(self: *MachO, object_id: u32) !void {...@@ -1709,26 +1709,14 @@ fn resolveSymbolsInObject(self: *MachO, object_id: u32) !void {
1709 while (sym_index < in_symtab.len) : (sym_index += 1) {1709 while (sym_index < in_symtab.len) : (sym_index += 1) {
1710 const sym = &object.symtab[sym_index];1710 const sym = &object.symtab[sym_index];
1711 const sym_name = object.getSymbolName(sym_index);1711 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()) {1717 if (sym.stab() or sym.indr() or sym.abs()) {
1714 log.err("unhandled symbol type: stab", .{});1718 try self.reportUnhandledSymbolType(sym_with_loc);
1715 log.err(" symbol '{s}'", .{sym_name});1719 continue;
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;
1732 }1720 }
17331721
1734 if (sym.sect() and !sym.ext()) {1722 if (sym.sect() and !sym.ext()) {
...@@ -4892,7 +4880,7 @@ pub fn handleAndReportParseError(...@@ -4892,7 +4880,7 @@ pub fn handleAndReportParseError(
4892 path: []const u8,4880 path: []const u8,
4893 err: ParseError,4881 err: ParseError,
4894 ctx: *const ParseErrorCtx,4882 ctx: *const ParseErrorCtx,
4895) !void {4883) error{OutOfMemory}!void {
4896 const cpu_arch = self.base.options.target.cpu.arch;4884 const cpu_arch = self.base.options.target.cpu.arch;
4897 switch (err) {4885 switch (err) {
4898 error.DylibAlreadyExists => {},4886 error.DylibAlreadyExists => {},
...@@ -4943,7 +4931,7 @@ fn reportDependencyError(...@@ -4943,7 +4931,7 @@ fn reportDependencyError(
4943 path: ?[]const u8,4931 path: ?[]const u8,
4944 comptime format: []const u8,4932 comptime format: []const u8,
4945 args: anytype,4933 args: anytype,
4946) !void {4934) error{OutOfMemory}!void {
4947 const gpa = self.base.allocator;4935 const gpa = self.base.allocator;
4948 try self.misc_errors.ensureUnusedCapacity(gpa, 1);4936 try self.misc_errors.ensureUnusedCapacity(gpa, 1);
4949 var notes = try std.ArrayList(File.ErrorMsg).initCapacity(gpa, 2);4937 var notes = try std.ArrayList(File.ErrorMsg).initCapacity(gpa, 2);
...@@ -4958,7 +4946,12 @@ fn reportDependencyError(...@@ -4958,7 +4946,12 @@ fn reportDependencyError(
4958 });4946 });
4959}4947}
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 {
4962 const gpa = self.base.allocator;4955 const gpa = self.base.allocator;
4963 try self.misc_errors.ensureUnusedCapacity(gpa, 1);4956 try self.misc_errors.ensureUnusedCapacity(gpa, 1);
4964 var notes = try gpa.alloc(File.ErrorMsg, 1);4957 var notes = try gpa.alloc(File.ErrorMsg, 1);
...@@ -5030,6 +5023,35 @@ fn reportSymbolCollision(...@@ -5030,6 +5023,35 @@ fn reportSymbolCollision(
5030 self.misc_errors.appendAssumeCapacity(err_msg);5023 self.misc_errors.appendAssumeCapacity(err_msg);
5031}5024}
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
5033/// Binary search5055/// Binary search
5034pub fn bsearch(comptime T: type, haystack: []align(1) const T, predicate: anytype) usize {5056pub fn bsearch(comptime T: type, haystack: []align(1) const T, predicate: anytype) usize {
5035 if (!@hasDecl(@TypeOf(predicate), "predicate"))5057 if (!@hasDecl(@TypeOf(predicate), "predicate"))