authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-30 19:07:36+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-08-30 19:07:36+02:00
logaaca4ff74dab5d749cd292d31957ce19ab5901c7
treea5aae705179667dc378293c321faf8230649d481
parentd997ddaa102bb9ba5f1e8480b8c78f7d102b5512
parenta1a9d7440ca7b33a73ef7d3b4ee450271337f96f
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21254 from jacobly0/fix-dwarf

Dwarf: fix issues with emitted debug info

3 files changed, 35 insertions(+), 65 deletions(-)

src/link/Dwarf.zig+19-18
......@@ -2593,6 +2593,9 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
25932593 decl_struct: {
25942594 if (loaded_struct.zir_index == .none) break :decl_struct;
25952595
2596 const type_inst_info = loaded_struct.zir_index.unwrap().?.resolveFull(ip).?;
2597 if (type_inst_info.file != inst_info.file) break :decl_struct;
2598
25962599 const value_inst = value_inst: {
25972600 const decl_value_body = decl_extra.data.getBodies(@intCast(decl_extra.end), file.zir).value_body;
25982601 const break_inst = file.zir.instructions.get(@intFromEnum(decl_value_body[decl_value_body.len - 1]));
......@@ -2608,7 +2611,6 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
26082611 };
26092612 break :value_inst value_inst;
26102613 };
2611 const type_inst_info = loaded_struct.zir_index.unwrap().?.resolveFull(ip).?;
26122614 if (type_inst_info.inst != value_inst) break :decl_struct;
26132615
26142616 const type_gop = try dwarf.types.getOrPut(dwarf.gpa, nav_val.toIntern());
......@@ -2623,6 +2625,7 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
26232625 type_gop.value_ptr.* = nav_gop.value_ptr.*;
26242626 }
26252627 wip_nav.entry = nav_gop.value_ptr.*;
2628
26262629 const diw = wip_nav.debug_info.writer(dwarf.gpa);
26272630
26282631 switch (loaded_struct.layout) {
......@@ -2714,6 +2717,9 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
27142717 decl_enum: {
27152718 if (loaded_enum.zir_index == .none) break :decl_enum;
27162719
2720 const type_inst_info = loaded_enum.zir_index.unwrap().?.resolveFull(ip).?;
2721 if (type_inst_info.file != inst_info.file) break :decl_enum;
2722
27172723 const value_inst = value_inst: {
27182724 const decl_value_body = decl_extra.data.getBodies(@intCast(decl_extra.end), file.zir).value_body;
27192725 const break_inst = file.zir.instructions.get(@intFromEnum(decl_value_body[decl_value_body.len - 1]));
......@@ -2729,7 +2735,6 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
27292735 };
27302736 break :value_inst value_inst;
27312737 };
2732 const type_inst_info = loaded_enum.zir_index.unwrap().?.resolveFull(ip).?;
27332738 if (type_inst_info.inst != value_inst) break :decl_enum;
27342739
27352740 const type_gop = try dwarf.types.getOrPut(dwarf.gpa, nav_val.toIntern());
......@@ -2797,6 +2802,9 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
27972802 } else .{ zcu.fileRootType(inst_info.file), DW.ACCESS.private };
27982803
27992804 decl_union: {
2805 const type_inst_info = loaded_union.zir_index.resolveFull(ip).?;
2806 if (type_inst_info.file != inst_info.file) break :decl_union;
2807
28002808 const value_inst = value_inst: {
28012809 const decl_value_body = decl_extra.data.getBodies(@intCast(decl_extra.end), file.zir).value_body;
28022810 const break_inst = file.zir.instructions.get(@intFromEnum(decl_value_body[decl_value_body.len - 1]));
......@@ -2812,7 +2820,6 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
28122820 };
28132821 break :value_inst value_inst;
28142822 };
2815 const type_inst_info = loaded_union.zir_index.resolveFull(ip).?;
28162823 if (type_inst_info.inst != value_inst) break :decl_union;
28172824
28182825 const type_gop = try dwarf.types.getOrPut(dwarf.gpa, nav_val.toIntern());
......@@ -2872,9 +2879,6 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
28722879 }
28732880 }
28742881 try uleb128(diw, @intFromEnum(AbbrevCode.null));
2875
2876 if (ip.indexToKey(loaded_union.enum_tag_ty).enum_type == .generated_tag)
2877 try wip_nav.pending_types.append(dwarf.gpa, loaded_union.enum_tag_ty);
28782882 } else for (0..loaded_union.field_types.len) |field_index| {
28792883 try wip_nav.abbrevCode(.untagged_union_field);
28802884 try wip_nav.strp(loaded_tag.names.get(ip)[field_index].toSlice(ip));
......@@ -2919,6 +2923,9 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
29192923 } else .{ zcu.fileRootType(inst_info.file), DW.ACCESS.private };
29202924
29212925 decl_opaque: {
2926 const type_inst_info = loaded_opaque.zir_index.resolveFull(ip).?;
2927 if (type_inst_info.file != inst_info.file) break :decl_opaque;
2928
29222929 const value_inst = value_inst: {
29232930 const decl_value_body = decl_extra.data.getBodies(@intCast(decl_extra.end), file.zir).value_body;
29242931 const break_inst = file.zir.instructions.get(@intFromEnum(decl_value_body[decl_value_body.len - 1]));
......@@ -2934,7 +2941,6 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
29342941 };
29352942 break :value_inst value_inst;
29362943 };
2937 const type_inst_info = loaded_opaque.zir_index.resolveFull(ip).?;
29382944 if (type_inst_info.inst != value_inst) break :decl_opaque;
29392945
29402946 const type_gop = try dwarf.types.getOrPut(dwarf.gpa, nav_val.toIntern());
......@@ -3381,14 +3387,12 @@ fn updateType(
33813387 .Kernel, .Fragment, .Vertex => .nocall,
33823388 })));
33833389 try wip_nav.refType(Type.fromInterned(func_type.return_type));
3384 if (!is_nullary) {
3385 for (0..func_type.param_types.len) |param_index| {
3386 try wip_nav.abbrevCode(.func_type_param);
3387 try wip_nav.refType(Type.fromInterned(func_type.param_types.get(ip)[param_index]));
3388 }
3389 if (func_type.is_var_args) try wip_nav.abbrevCode(.is_var_args);
3390 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3390 for (0..func_type.param_types.len) |param_index| {
3391 try wip_nav.abbrevCode(.func_type_param);
3392 try wip_nav.refType(Type.fromInterned(func_type.param_types.get(ip)[param_index]));
33913393 }
3394 if (func_type.is_var_args) try wip_nav.abbrevCode(.is_var_args);
3395 if (!is_nullary) try uleb128(diw, @intFromEnum(AbbrevCode.null));
33923396 },
33933397 .error_set_type => |error_set_type| {
33943398 try wip_nav.abbrevCode(if (error_set_type.names.len > 0) .enum_type else .empty_enum_type);
......@@ -3643,9 +3647,6 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
36433647 }
36443648 }
36453649 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3646
3647 if (ip.indexToKey(loaded_union.enum_tag_ty).enum_type == .generated_tag)
3648 try wip_nav.pending_types.append(dwarf.gpa, loaded_union.enum_tag_ty);
36493650 } else for (0..loaded_union.field_types.len) |field_index| {
36503651 try wip_nav.abbrevCode(.untagged_union_field);
36513652 try wip_nav.strp(loaded_tag.names.get(ip)[field_index].toSlice(ip));
......@@ -3824,7 +3825,7 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
38243825 sleb128(header.fixedWriter(), dwarf.debug_frame.header.data_alignment_factor) catch unreachable;
38253826 uleb128(header.fixedWriter(), dwarf.debug_frame.header.return_address_register) catch unreachable;
38263827 uleb128(header.fixedWriter(), 1) catch unreachable;
3827 header.appendAssumeCapacity(0x10 | 0x08 | 0x03);
3828 header.appendAssumeCapacity(DW.EH.PE.pcrel | DW.EH.PE.sdata4);
38283829 header.appendAssumeCapacity(DW.CFA.def_cfa_sf);
38293830 uleb128(header.fixedWriter(), Register.rsp.dwarfNum()) catch unreachable;
38303831 sleb128(header.fixedWriter(), -1) catch unreachable;
src/link/Elf/eh_frame.zig+10-23
......@@ -482,19 +482,24 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
482482 const gpa = comp.gpa;
483483
484484 try writer.writeByte(1); // version
485 try writer.writeByte(EH_PE.pcrel | EH_PE.sdata4);
486 try writer.writeByte(EH_PE.udata4);
487 try writer.writeByte(EH_PE.datarel | EH_PE.sdata4);
485 try writer.writeByte(DW_EH_PE.pcrel | DW_EH_PE.sdata4);
486 try writer.writeByte(DW_EH_PE.udata4);
487 try writer.writeByte(DW_EH_PE.datarel | DW_EH_PE.sdata4);
488488
489489 const shdrs = elf_file.sections.items(.shdr);
490490 const eh_frame_shdr = shdrs[elf_file.eh_frame_section_index.?];
491491 const eh_frame_hdr_shdr = shdrs[elf_file.eh_frame_hdr_section_index.?];
492492 const num_fdes = @as(u32, @intCast(@divExact(eh_frame_hdr_shdr.sh_size - eh_frame_hdr_header_size, 8)));
493 const existing_size = existing_size: {
494 const zo = elf_file.zigObjectPtr() orelse break :existing_size 0;
495 const sym = zo.symbol(zo.eh_frame_index orelse break :existing_size 0);
496 break :existing_size sym.atom(elf_file).?.size;
497 };
493498 try writer.writeInt(
494499 u32,
495500 @as(u32, @bitCast(@as(
496501 i32,
497 @truncate(@as(i64, @intCast(eh_frame_shdr.sh_addr)) - @as(i64, @intCast(eh_frame_hdr_shdr.sh_addr)) - 4),
502 @truncate(@as(i64, @intCast(eh_frame_shdr.sh_addr + existing_size)) - @as(i64, @intCast(eh_frame_hdr_shdr.sh_addr)) - 4),
498503 ))),
499504 .little,
500505 );
......@@ -543,25 +548,6 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
543548
544549const eh_frame_hdr_header_size: usize = 12;
545550
546const EH_PE = struct {
547 pub const absptr = 0x00;
548 pub const uleb128 = 0x01;
549 pub const udata2 = 0x02;
550 pub const udata4 = 0x03;
551 pub const udata8 = 0x04;
552 pub const sleb128 = 0x09;
553 pub const sdata2 = 0x0A;
554 pub const sdata4 = 0x0B;
555 pub const sdata8 = 0x0C;
556 pub const pcrel = 0x10;
557 pub const textrel = 0x20;
558 pub const datarel = 0x30;
559 pub const funcrel = 0x40;
560 pub const aligned = 0x50;
561 pub const indirect = 0x80;
562 pub const omit = 0xFF;
563};
564
565551const x86_64 = struct {
566552 fn resolveReloc(rec: anytype, elf_file: *Elf, rel: elf.Elf64_Rela, source: i64, target: i64, data: []u8) !void {
567553 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
......@@ -619,6 +605,7 @@ const relocation = @import("relocation.zig");
619605
620606const Allocator = std.mem.Allocator;
621607const Atom = @import("Atom.zig");
608const DW_EH_PE = std.dwarf.EH.PE;
622609const Elf = @import("../Elf.zig");
623610const Object = @import("Object.zig");
624611const Symbol = @import("Symbol.zig");
src/link/MachO/eh_frame.zig+6-24
......@@ -29,22 +29,22 @@ pub const Cie = struct {
2929 for (aug[1..]) |ch| switch (ch) {
3030 'R' => {
3131 const enc = try reader.readByte();
32 if (enc & 0xf != EH_PE.absptr or enc & EH_PE.pcrel == 0) {
32 if (enc != DW_EH_PE.pcrel | DW_EH_PE.absptr) {
3333 @panic("unexpected pointer encoding"); // TODO error
3434 }
3535 },
3636 'P' => {
3737 const enc = try reader.readByte();
38 if (enc != EH_PE.pcrel | EH_PE.indirect | EH_PE.sdata4) {
38 if (enc != DW_EH_PE.pcrel | DW_EH_PE.indirect | DW_EH_PE.sdata4) {
3939 @panic("unexpected personality pointer encoding"); // TODO error
4040 }
4141 _ = try reader.readInt(u32, .little); // personality pointer
4242 },
4343 'L' => {
4444 const enc = try reader.readByte();
45 switch (enc & 0xf) {
46 EH_PE.sdata4 => cie.lsda_size = .p32,
47 EH_PE.absptr => cie.lsda_size = .p64,
45 switch (enc & DW_EH_PE.type_mask) {
46 DW_EH_PE.sdata4 => cie.lsda_size = .p32,
47 DW_EH_PE.absptr => cie.lsda_size = .p64,
4848 else => unreachable, // TODO error
4949 }
5050 },
......@@ -538,25 +538,6 @@ pub fn writeRelocs(macho_file: *MachO, code: []u8, relocs: []macho.relocation_in
538538 assert(relocs.len == i);
539539}
540540
541pub const EH_PE = struct {
542 pub const absptr = 0x00;
543 pub const uleb128 = 0x01;
544 pub const udata2 = 0x02;
545 pub const udata4 = 0x03;
546 pub const udata8 = 0x04;
547 pub const sleb128 = 0x09;
548 pub const sdata2 = 0x0A;
549 pub const sdata4 = 0x0B;
550 pub const sdata8 = 0x0C;
551 pub const pcrel = 0x10;
552 pub const textrel = 0x20;
553 pub const datarel = 0x30;
554 pub const funcrel = 0x40;
555 pub const aligned = 0x50;
556 pub const indirect = 0x80;
557 pub const omit = 0xFF;
558};
559
560541const assert = std.debug.assert;
561542const leb = std.leb;
562543const macho = std.macho;
......@@ -567,6 +548,7 @@ const trace = @import("../../tracy.zig").trace;
567548
568549const Allocator = std.mem.Allocator;
569550const Atom = @import("Atom.zig");
551const DW_EH_PE = std.dwarf.EH.PE;
570552const File = @import("file.zig").File;
571553const MachO = @import("../MachO.zig");
572554const Object = @import("Object.zig");