authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-10-16 13:20:19+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-02 22:05:21-05:00
log0769afbb0f2fbb9c72d97ec0bcdcaba0ac916341
treefa23c874202671f84c7fcc5b7b751094c1e50170
parentc824b350511780581c0e5c1da85d0d9d769701ea

macho: refactors errors from parsing DWARF

Currently we don't report any errors to the user due to a bug in self-hosted x86_64-macos backend.

2 files changed, 18 insertions(+), 16 deletions(-)

src/link/MachO/Dwarf.zig+7-9
......@@ -29,8 +29,7 @@ pub const InfoReader = struct {
2929 return p.ctx.debug_info;
3030 }
3131
32 pub fn readCompileUnitHeader(p: *InfoReader, macho_file: *MachO) !CompileUnitHeader {
33 _ = macho_file;
32 pub fn readCompileUnitHeader(p: *InfoReader) !CompileUnitHeader {
3433 var length: u64 = try p.readInt(u32);
3534 const is_64bit = length == 0xffffffff;
3635 if (is_64bit) {
......@@ -67,7 +66,7 @@ pub const InfoReader = struct {
6766 };
6867 }
6968
70 pub fn seekToDie(p: *InfoReader, code: Code, cuh: CompileUnitHeader, abbrev_reader: *AbbrevReader, macho_file: *MachO) !void {
69 pub fn seekToDie(p: *InfoReader, code: Code, cuh: CompileUnitHeader, abbrev_reader: *AbbrevReader) !void {
7170 const cuh_length = math.cast(usize, cuh.length) orelse return error.Overflow;
7271 const end_pos = p.pos + switch (cuh.format) {
7372 .dwarf32 => @as(usize, 4),
......@@ -79,7 +78,7 @@ pub const InfoReader = struct {
7978 if (di_code == code) return;
8079
8180 while (try abbrev_reader.readAttr()) |attr| {
82 try p.skip(attr.form, cuh, macho_file);
81 try p.skip(attr.form, cuh);
8382 }
8483 }
8584 return error.UnexpectedEndOfFile;
......@@ -87,8 +86,7 @@ pub const InfoReader = struct {
8786
8887 /// When skipping attributes, we don't really need to be able to handle them all
8988 /// since we only ever care about the DW_TAG_compile_unit.
90 pub fn skip(p: *InfoReader, form: Form, cuh: CompileUnitHeader, macho_file: *MachO) !void {
91 _ = macho_file;
89 pub fn skip(p: *InfoReader, form: Form, cuh: CompileUnitHeader) !void {
9290 switch (form) {
9391 dw.FORM.sec_offset,
9492 dw.FORM.ref_addr,
......@@ -158,8 +156,8 @@ pub const InfoReader = struct {
158156 _ = try p.readIndex(form);
159157 },
160158
161 else => return error.UnknownForm,
162 } else return error.UnknownForm,
159 else => return error.UnhandledForm,
160 } else return error.UnhandledForm,
163161 }
164162 }
165163
......@@ -195,7 +193,7 @@ pub const InfoReader = struct {
195193 return switch (form) {
196194 dw.FORM.strx1, dw.FORM.addrx1 => try p.readByte(),
197195 dw.FORM.strx2, dw.FORM.addrx2 => try p.readInt(u16),
198 dw.FORM.strx3, dw.FORM.addrx3 => error.UnhandledDwForm,
196 dw.FORM.strx3, dw.FORM.addrx3 => error.UnhandledForm,
199197 dw.FORM.strx4, dw.FORM.addrx4 => try p.readInt(u32),
200198 dw.FORM.strx, dw.FORM.addrx => try p.readUleb128(u64),
201199 else => return error.UnhandledIndexForm,
src/link/MachO/Object.zig+11-7
......@@ -1362,6 +1362,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void {
13621362 if (mem.eql(u8, sect.sectName(), "__debug_str")) {
13631363 dwarf.debug_str = try self.readSectionData(gpa, file, n_sect);
13641364 }
1365 // __debug_str_offs[ets] section is a new addition in DWARFv5 and is generally
1366 // required in order to correctly parse strings.
13651367 if (mem.eql(u8, sect.sectName(), "__debug_str_offs")) {
13661368 dwarf.debug_str_offsets = try self.readSectionData(gpa, file, n_sect);
13671369 }
......@@ -1369,20 +1371,22 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void {
13691371
13701372 if (dwarf.debug_info.len == 0) return;
13711373
1372 self.compile_unit = try self.findCompileUnit(gpa, dwarf, macho_file);
1374 // TODO return error once we fix emitting DWARF in self-hosted backend.
1375 // https://github.com/ziglang/zig/issues/21719
1376 self.compile_unit = self.findCompileUnit(gpa, dwarf) catch null;
13731377}
13741378
1375fn findCompileUnit(self: *Object, gpa: Allocator, ctx: Dwarf, macho_file: *MachO) !CompileUnit {
1379fn findCompileUnit(self: *Object, gpa: Allocator, ctx: Dwarf) !CompileUnit {
13761380 var info_reader = Dwarf.InfoReader{ .ctx = ctx };
13771381 var abbrev_reader = Dwarf.AbbrevReader{ .ctx = ctx };
13781382
1379 const cuh = try info_reader.readCompileUnitHeader(macho_file);
1383 const cuh = try info_reader.readCompileUnitHeader();
13801384 try abbrev_reader.seekTo(cuh.debug_abbrev_offset);
13811385
13821386 const cu_decl = (try abbrev_reader.readDecl()) orelse return error.UnexpectedEndOfFile;
13831387 if (cu_decl.tag != Dwarf.TAG.compile_unit) return error.UnexpectedTag;
13841388
1385 try info_reader.seekToDie(cu_decl.code, cuh, &abbrev_reader, macho_file);
1389 try info_reader.seekToDie(cu_decl.code, cuh, &abbrev_reader);
13861390
13871391 const Pos = struct {
13881392 pos: usize,
......@@ -1405,10 +1409,10 @@ fn findCompileUnit(self: *Object, gpa: Allocator, ctx: Dwarf, macho_file: *MachO
14051409 Dwarf.AT.str_offsets_base => saved.str_offsets_base = pos,
14061410 else => {},
14071411 }
1408 try info_reader.skip(attr.form, cuh, macho_file);
1412 try info_reader.skip(attr.form, cuh);
14091413 }
14101414
1411 if (saved.comp_dir == null) return error.MissingCompDir;
1415 if (saved.comp_dir == null) return error.MissingCompileDir;
14121416 if (saved.tu_name == null) return error.MissingTuName;
14131417
14141418 const str_offsets_base: ?u64 = if (saved.str_offsets_base) |str_offsets_base| str_offsets_base: {
......@@ -1433,7 +1437,7 @@ fn findCompileUnit(self: *Object, gpa: Allocator, ctx: Dwarf, macho_file: *MachO
14331437 Dwarf.FORM.strx3,
14341438 Dwarf.FORM.strx4,
14351439 => blk: {
1436 const base = str_offsets_base orelse return error.MalformedDwarf;
1440 const base = str_offsets_base orelse return error.MissingStrOffsetsBase;
14371441 break :blk try self.addString(gpa, try info_reader.readStringIndexed(pos.form, cuh, base));
14381442 },
14391443 else => return error.InvalidForm,