From a7240f0c99426a546b05f7e8bb086805a2766ea9 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 30 Aug 2023 22:29:24 +0200 Subject: [PATCH] macho: remove error.UnhandledDwFormValue from link.File Eventually, we will validate DWARF info upfront and report errors to the user but this will require a rewrite of several parts of the linker so leaving as a TODO for the near future. --- src/link.zig | 1 - src/link/MachO.zig | 32 ++++++++++++++++++++++---------- src/link/MachO/DwarfInfo.zig | 3 ++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/link.zig b/src/link.zig index b57ab6fa7b..634f9679c4 100644 --- a/src/link.zig +++ b/src/link.zig @@ -751,7 +751,6 @@ pub const File = struct { UnexpectedRemainder, UnexpectedTable, UnexpectedValue, - UnhandledDwFormValue, UnknownFeature, Unseekable, UnsupportedCpuArchitecture, diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 10848da7f7..7cde05cf04 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -4101,6 +4101,9 @@ fn writeSymtab(self: *MachO) !SymtabCtx { }; } +// TODO this function currently skips generating symbol stabs in case errors are encountered in DWARF data. +// I think we should actually report those errors to the user and let them decide if they want to strip debug info +// in that case or not. fn generateSymbolStabs( self: *MachO, object: Object, @@ -4127,10 +4130,14 @@ fn generateSymbolStabs( }; var abbrev_it = compile_unit.getAbbrevEntryIterator(debug_info); - const cu_entry: DwarfInfo.AbbrevEntry = while (try abbrev_it.next(lookup)) |entry| switch (entry.tag) { - dwarf.TAG.compile_unit => break entry, - else => continue, - } else { + const maybe_cu_entry: ?DwarfInfo.AbbrevEntry = blk: { + while (abbrev_it.next(lookup) catch break :blk null) |entry| switch (entry.tag) { + dwarf.TAG.compile_unit => break :blk entry, + else => continue, + } else break :blk null; + }; + + const cu_entry = maybe_cu_entry orelse { log.debug("missing DWARF_TAG_compile_unit tag in {s}; skipping", .{object.name}); return; }; @@ -4139,11 +4146,13 @@ fn generateSymbolStabs( var maybe_tu_comp_dir: ?[]const u8 = null; var attr_it = cu_entry.getAttributeIterator(debug_info, compile_unit.cuh); - while (try attr_it.next()) |attr| switch (attr.name) { - dwarf.AT.comp_dir => maybe_tu_comp_dir = attr.getString(debug_info, compile_unit.cuh) orelse continue, - dwarf.AT.name => maybe_tu_name = attr.getString(debug_info, compile_unit.cuh) orelse continue, - else => continue, - }; + blk: { + while (attr_it.next() catch break :blk) |attr| switch (attr.name) { + dwarf.AT.comp_dir => maybe_tu_comp_dir = attr.getString(debug_info, compile_unit.cuh) orelse continue, + dwarf.AT.name => maybe_tu_name = attr.getString(debug_info, compile_unit.cuh) orelse continue, + else => continue, + }; + } if (maybe_tu_name == null or maybe_tu_comp_dir == null) { log.debug("missing DWARF_AT_comp_dir and DWARF_AT_name attributes {s}; skipping", .{object.name}); @@ -4183,7 +4192,10 @@ fn generateSymbolStabs( var name_lookup = DwarfInfo.SubprogramLookupByName.init(gpa); errdefer name_lookup.deinit(); try name_lookup.ensureUnusedCapacity(@as(u32, @intCast(object.atoms.items.len))); - try debug_info.genSubprogramLookupByName(compile_unit, lookup, &name_lookup); + debug_info.genSubprogramLookupByName(compile_unit, lookup, &name_lookup) catch |err| switch (err) { + error.UnhandledDwFormValue => {}, // TODO I don't like the fact we constantly re-iterate and hit this; we should validate once a priori + else => |e| return e, + }; break :blk name_lookup; } else null; defer if (name_lookup) |*nl| nl.deinit(); diff --git a/src/link/MachO/DwarfInfo.zig b/src/link/MachO/DwarfInfo.zig index 7b0536f60f..1f151a1dd5 100644 --- a/src/link/MachO/DwarfInfo.zig +++ b/src/link/MachO/DwarfInfo.zig @@ -444,7 +444,8 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head }, else => { - log.err("unhandled DW_FORM_* value with identifier {x}", .{form}); + // TODO figure out how to handle this + log.debug("unhandled DW_FORM_* value with identifier {x}", .{form}); return error.UnhandledDwFormValue; }, }