authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-06 19:29:23+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-09 09:24:25+01:00
logf5c764d8923d301bb7e46b50cb034285640fcca2
treee573f5faca2995d766b56e1e001989a343ff08b3
parent4c4821d9939447d65966b897a5780e39eb22334b

dwarf: track source files via *const Module.File pointers


5 files changed, 162 insertions(+), 152 deletions(-)

src/link/Dwarf.zig+141-126
......@@ -22,7 +22,7 @@ const Value = @import("../value.zig").Value;
2222const Type = @import("../type.zig").Type;
2323
2424allocator: Allocator,
25tag: File.Tag,
25bin_file: *File,
2626ptr_width: PtrWidth,
2727target: std.Target,
2828
......@@ -44,10 +44,9 @@ abbrev_table_offset: ?u64 = null,
4444/// Table of debug symbol names.
4545strtab: std.ArrayListUnmanaged(u8) = .{},
4646
47file_names_buffer: std.ArrayListUnmanaged(u8) = .{},
48file_names: std.ArrayListUnmanaged(u32) = .{},
49file_names_free_list: std.ArrayListUnmanaged(u28) = .{},
50file_names_lookup: std.AutoHashMapUnmanaged(*Module.File, u28) = .{},
47di_files: std.ArrayListUnmanaged(DIFile) = .{},
48di_files_free_list: std.ArrayListUnmanaged(u28) = .{},
49di_files_lookup: std.AutoHashMapUnmanaged(*const Module.File, u28) = .{},
5150
5251/// List of atoms that are owned directly by the DWARF module.
5352/// TODO convert links in DebugInfoAtom into indices and make
......@@ -56,6 +55,15 @@ managed_atoms: std.ArrayListUnmanaged(*Atom) = .{},
5655
5756global_abbrev_relocs: std.ArrayListUnmanaged(AbbrevRelocation) = .{},
5857
58const DIFile = struct {
59 file_source: *const Module.File,
60 ref_count: u32,
61
62 fn getFullPath(dif: DIFile, allocator: Allocator) ![]const u8 {
63 return dif.file_source.fullPath(allocator);
64 }
65};
66
5967pub const Atom = struct {
6068 /// Previous/next linked list pointers.
6169 /// This is the linked list node for this Decl's corresponding .debug_info tag.
......@@ -892,7 +900,7 @@ const min_nop_size = 2;
892900/// actual_capacity + (actual_capacity / ideal_factor)
893901const ideal_factor = 3;
894902
895pub fn init(allocator: Allocator, tag: File.Tag, target: std.Target) Dwarf {
903pub fn init(allocator: Allocator, bin_file: *File, target: std.Target) Dwarf {
896904 const ptr_width: PtrWidth = switch (target.cpu.arch.ptrBitWidth()) {
897905 0...32 => .p32,
898906 33...64 => .p64,
......@@ -900,7 +908,7 @@ pub fn init(allocator: Allocator, tag: File.Tag, target: std.Target) Dwarf {
900908 };
901909 return Dwarf{
902910 .allocator = allocator,
903 .tag = tag,
911 .bin_file = bin_file,
904912 .ptr_width = ptr_width,
905913 .target = target,
906914 };
......@@ -911,10 +919,9 @@ pub fn deinit(self: *Dwarf) void {
911919 self.dbg_line_fn_free_list.deinit(gpa);
912920 self.atom_free_list.deinit(gpa);
913921 self.strtab.deinit(gpa);
914 self.file_names_buffer.deinit(gpa);
915 self.file_names.deinit(gpa);
916 self.file_names_free_list.deinit(gpa);
917 self.file_names_lookup.deinit(gpa);
922 self.di_files.deinit(gpa);
923 self.di_files_free_list.deinit(gpa);
924 self.di_files_lookup.deinit(gpa);
918925 self.global_abbrev_relocs.deinit(gpa);
919926
920927 for (self.managed_atoms.items) |atom| {
......@@ -977,7 +984,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: Module.Decl.Index)
977984 assert(self.getRelocDbgFileIndex() == dbg_line_buffer.items.len);
978985 // Once we support more than one source file, this will have the ability to be more
979986 // than one possible value.
980 const file_index = try self.addFileName(mod, decl_index);
987 const file_index = try self.addDIFile(mod, decl_index);
981988 leb128.writeUnsignedFixed(
982989 4,
983990 dbg_line_buffer.addManyAsArrayAssumeCapacity(4),
......@@ -1008,7 +1015,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: Module.Decl.Index)
10081015 dbg_info_buffer.items.len += 4; // DW.AT.high_pc, DW.FORM.data4
10091016 //
10101017 if (fn_ret_has_bits) {
1011 const atom = getDbgInfoAtom(self.tag, mod, decl_index);
1018 const atom = getDbgInfoAtom(self.bin_file.tag, mod, decl_index);
10121019 try decl_state.addTypeRelocGlobal(atom, fn_ret_type, @intCast(u32, dbg_info_buffer.items.len));
10131020 dbg_info_buffer.items.len += 4; // DW.AT.type, DW.FORM.ref4
10141021 }
......@@ -1026,7 +1033,6 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: Module.Decl.Index)
10261033
10271034pub fn commitDeclState(
10281035 self: *Dwarf,
1029 file: *File,
10301036 module: *Module,
10311037 decl_index: Module.Decl.Index,
10321038 sym_addr: u64,
......@@ -1082,7 +1088,7 @@ pub fn commitDeclState(
10821088 // This logic is nearly identical to the logic below in `updateDeclDebugInfo` for
10831089 // `TextBlock` and the .debug_info. If you are editing this logic, you
10841090 // probably need to edit that logic too.
1085 const src_fn = switch (self.tag) {
1091 const src_fn = switch (self.bin_file.tag) {
10861092 .elf => &decl.fn_link.elf,
10871093 .macho => &decl.fn_link.macho,
10881094 .wasm => &decl.fn_link.wasm.src_fn,
......@@ -1103,22 +1109,22 @@ pub fn commitDeclState(
11031109 next.prev = src_fn.prev;
11041110 src_fn.next = null;
11051111 // Populate where it used to be with NOPs.
1106 switch (self.tag) {
1112 switch (self.bin_file.tag) {
11071113 .elf => {
1108 const elf_file = file.cast(File.Elf).?;
1114 const elf_file = self.bin_file.cast(File.Elf).?;
11091115 const debug_line_sect = &elf_file.sections.items[elf_file.debug_line_section_index.?];
11101116 const file_pos = debug_line_sect.sh_offset + src_fn.off;
11111117 try pwriteDbgLineNops(elf_file.base.file.?, file_pos, 0, &[0]u8{}, src_fn.len);
11121118 },
11131119 .macho => {
1114 const macho_file = file.cast(File.MachO).?;
1120 const macho_file = self.bin_file.cast(File.MachO).?;
11151121 const d_sym = &macho_file.d_sym.?;
11161122 const debug_line_sect = &d_sym.sections.items[d_sym.debug_line_section_index.?];
11171123 const file_pos = debug_line_sect.offset + src_fn.off;
11181124 try pwriteDbgLineNops(d_sym.file, file_pos, 0, &[0]u8{}, src_fn.len);
11191125 },
11201126 .wasm => {
1121 const wasm_file = file.cast(File.Wasm).?;
1127 const wasm_file = self.bin_file.cast(File.Wasm).?;
11221128 const debug_line = wasm_file.debug_line_atom.?.code;
11231129 writeDbgLineNopsBuffered(debug_line.items, src_fn.off, 0, &.{}, src_fn.len);
11241130 },
......@@ -1156,9 +1162,9 @@ pub fn commitDeclState(
11561162
11571163 // We only have support for one compilation unit so far, so the offsets are directly
11581164 // from the .debug_line section.
1159 switch (self.tag) {
1165 switch (self.bin_file.tag) {
11601166 .elf => {
1161 const elf_file = file.cast(File.Elf).?;
1167 const elf_file = self.bin_file.cast(File.Elf).?;
11621168 const debug_line_sect = &elf_file.sections.items[elf_file.debug_line_section_index.?];
11631169 if (needed_size != debug_line_sect.sh_size) {
11641170 if (needed_size > elf_file.allocatedSize(debug_line_sect.sh_offset)) {
......@@ -1193,7 +1199,7 @@ pub fn commitDeclState(
11931199 },
11941200
11951201 .macho => {
1196 const macho_file = file.cast(File.MachO).?;
1202 const macho_file = self.bin_file.cast(File.MachO).?;
11971203 const d_sym = &macho_file.d_sym.?;
11981204 const debug_line_sect = &d_sym.sections.items[d_sym.debug_line_section_index.?];
11991205 if (needed_size != debug_line_sect.size) {
......@@ -1228,7 +1234,7 @@ pub fn commitDeclState(
12281234 },
12291235
12301236 .wasm => {
1231 const wasm_file = file.cast(File.Wasm).?;
1237 const wasm_file = self.bin_file.cast(File.Wasm).?;
12321238 const atom = wasm_file.debug_line_atom.?;
12331239 const debug_line = &atom.code;
12341240 const segment_size = debug_line.items.len;
......@@ -1261,7 +1267,7 @@ pub fn commitDeclState(
12611267 if (dbg_info_buffer.items.len == 0)
12621268 return;
12631269
1264 const atom = getDbgInfoAtom(self.tag, module, decl_index);
1270 const atom = getDbgInfoAtom(self.bin_file.tag, module, decl_index);
12651271 if (decl_state.abbrev_table.items.len > 0) {
12661272 // Now we emit the .debug_info types of the Decl. These will count towards the size of
12671273 // the buffer, so we have to do it before computing the offset, and we can't perform the actual
......@@ -1288,7 +1294,7 @@ pub fn commitDeclState(
12881294 }
12891295
12901296 log.debug("updateDeclDebugInfoAllocation for '{s}'", .{decl.name});
1291 try self.updateDeclDebugInfoAllocation(file, atom, @intCast(u32, dbg_info_buffer.items.len));
1297 try self.updateDeclDebugInfoAllocation(atom, @intCast(u32, dbg_info_buffer.items.len));
12921298
12931299 while (decl_state.abbrev_relocs.popOrNull()) |reloc| {
12941300 if (reloc.target) |target| {
......@@ -1333,9 +1339,9 @@ pub fn commitDeclState(
13331339 }
13341340
13351341 while (decl_state.exprloc_relocs.popOrNull()) |reloc| {
1336 switch (self.tag) {
1342 switch (self.bin_file.tag) {
13371343 .macho => {
1338 const macho_file = file.cast(File.MachO).?;
1344 const macho_file = self.bin_file.cast(File.MachO).?;
13391345 const d_sym = &macho_file.d_sym.?;
13401346 try d_sym.relocs.append(d_sym.allocator, .{
13411347 .type = switch (reloc.type) {
......@@ -1353,10 +1359,10 @@ pub fn commitDeclState(
13531359 }
13541360
13551361 log.debug("writeDeclDebugInfo for '{s}", .{decl.name});
1356 try self.writeDeclDebugInfo(file, atom, dbg_info_buffer.items);
1362 try self.writeDeclDebugInfo(atom, dbg_info_buffer.items);
13571363}
13581364
1359fn updateDeclDebugInfoAllocation(self: *Dwarf, file: *File, atom: *Atom, len: u32) !void {
1365fn updateDeclDebugInfoAllocation(self: *Dwarf, atom: *Atom, len: u32) !void {
13601366 const tracy = trace(@src());
13611367 defer tracy.end();
13621368
......@@ -1379,22 +1385,22 @@ fn updateDeclDebugInfoAllocation(self: *Dwarf, file: *File, atom: *Atom, len: u3
13791385 next.prev = atom.prev;
13801386 atom.next = null;
13811387 // Populate where it used to be with NOPs.
1382 switch (self.tag) {
1388 switch (self.bin_file.tag) {
13831389 .elf => {
1384 const elf_file = file.cast(File.Elf).?;
1390 const elf_file = self.bin_file.cast(File.Elf).?;
13851391 const debug_info_sect = &elf_file.sections.items[elf_file.debug_info_section_index.?];
13861392 const file_pos = debug_info_sect.sh_offset + atom.off;
13871393 try pwriteDbgInfoNops(elf_file.base.file.?, file_pos, 0, &[0]u8{}, atom.len, false);
13881394 },
13891395 .macho => {
1390 const macho_file = file.cast(File.MachO).?;
1396 const macho_file = self.bin_file.cast(File.MachO).?;
13911397 const d_sym = &macho_file.d_sym.?;
13921398 const debug_info_sect = &d_sym.sections.items[d_sym.debug_info_section_index.?];
13931399 const file_pos = debug_info_sect.offset + atom.off;
13941400 try pwriteDbgInfoNops(d_sym.file, file_pos, 0, &[0]u8{}, atom.len, false);
13951401 },
13961402 .wasm => {
1397 const wasm_file = file.cast(File.Wasm).?;
1403 const wasm_file = self.bin_file.cast(File.Wasm).?;
13981404 const debug_info = &wasm_file.debug_info_atom.?.code;
13991405 try writeDbgInfoNopsToArrayList(gpa, debug_info, atom.off, 0, &.{0}, atom.len, false);
14001406 },
......@@ -1425,7 +1431,7 @@ fn updateDeclDebugInfoAllocation(self: *Dwarf, file: *File, atom: *Atom, len: u3
14251431 }
14261432}
14271433
1428fn writeDeclDebugInfo(self: *Dwarf, file: *File, atom: *Atom, dbg_info_buf: []const u8) !void {
1434fn writeDeclDebugInfo(self: *Dwarf, atom: *Atom, dbg_info_buf: []const u8) !void {
14291435 const tracy = trace(@src());
14301436 defer tracy.end();
14311437
......@@ -1445,9 +1451,9 @@ fn writeDeclDebugInfo(self: *Dwarf, file: *File, atom: *Atom, dbg_info_buf: []co
14451451
14461452 // We only have support for one compilation unit so far, so the offsets are directly
14471453 // from the .debug_info section.
1448 switch (self.tag) {
1454 switch (self.bin_file.tag) {
14491455 .elf => {
1450 const elf_file = file.cast(File.Elf).?;
1456 const elf_file = self.bin_file.cast(File.Elf).?;
14511457 const debug_info_sect = &elf_file.sections.items[elf_file.debug_info_section_index.?];
14521458 if (needed_size != debug_info_sect.sh_size) {
14531459 if (needed_size > elf_file.allocatedSize(debug_info_sect.sh_offset)) {
......@@ -1483,7 +1489,7 @@ fn writeDeclDebugInfo(self: *Dwarf, file: *File, atom: *Atom, dbg_info_buf: []co
14831489 },
14841490
14851491 .macho => {
1486 const macho_file = file.cast(File.MachO).?;
1492 const macho_file = self.bin_file.cast(File.MachO).?;
14871493 const d_sym = &macho_file.d_sym.?;
14881494 const debug_info_sect = &d_sym.sections.items[d_sym.debug_info_section_index.?];
14891495 if (needed_size != debug_info_sect.size) {
......@@ -1519,7 +1525,7 @@ fn writeDeclDebugInfo(self: *Dwarf, file: *File, atom: *Atom, dbg_info_buf: []co
15191525 },
15201526
15211527 .wasm => {
1522 const wasm_file = file.cast(File.Wasm).?;
1528 const wasm_file = self.bin_file.cast(File.Wasm).?;
15231529 const info_atom = wasm_file.debug_info_atom.?;
15241530 const debug_info = &info_atom.code;
15251531 const segment_size = debug_info.items.len;
......@@ -1549,7 +1555,7 @@ fn writeDeclDebugInfo(self: *Dwarf, file: *File, atom: *Atom, dbg_info_buf: []co
15491555 }
15501556}
15511557
1552pub fn updateDeclLineNumber(self: *Dwarf, file: *File, decl: *const Module.Decl) !void {
1558pub fn updateDeclLineNumber(self: *Dwarf, decl: *const Module.Decl) !void {
15531559 const tracy = trace(@src());
15541560 defer tracy.end();
15551561
......@@ -1563,22 +1569,22 @@ pub fn updateDeclLineNumber(self: *Dwarf, file: *File, decl: *const Module.Decl)
15631569 var data: [4]u8 = undefined;
15641570 leb128.writeUnsignedFixed(4, &data, line);
15651571
1566 switch (self.tag) {
1572 switch (self.bin_file.tag) {
15671573 .elf => {
1568 const elf_file = file.cast(File.Elf).?;
1574 const elf_file = self.bin_file.cast(File.Elf).?;
15691575 const shdr = elf_file.sections.items[elf_file.debug_line_section_index.?];
15701576 const file_pos = shdr.sh_offset + decl.fn_link.elf.off + self.getRelocDbgLineOff();
15711577 try elf_file.base.file.?.pwriteAll(&data, file_pos);
15721578 },
15731579 .macho => {
1574 const macho_file = file.cast(File.MachO).?;
1580 const macho_file = self.bin_file.cast(File.MachO).?;
15751581 const d_sym = macho_file.d_sym.?;
15761582 const sect = d_sym.sections.items[d_sym.debug_line_section_index.?];
15771583 const file_pos = sect.offset + decl.fn_link.macho.off + self.getRelocDbgLineOff();
15781584 try d_sym.file.pwriteAll(&data, file_pos);
15791585 },
15801586 .wasm => {
1581 const wasm_file = file.cast(File.Wasm).?;
1587 const wasm_file = self.bin_file.cast(File.Wasm).?;
15821588 const offset = decl.fn_link.wasm.src_fn.off + self.getRelocDbgLineOff();
15831589 const atom = wasm_file.debug_line_atom.?;
15841590 mem.copy(u8, atom.code.items[offset..], &data);
......@@ -1615,7 +1621,7 @@ pub fn freeDecl(self: *Dwarf, decl: *Module.Decl) void {
16151621 // TODO make this logic match freeTextBlock. Maybe abstract the logic out since the same thing
16161622 // is desired for both.
16171623 const gpa = self.allocator;
1618 const fn_link = switch (self.tag) {
1624 const fn_link = switch (self.bin_file.tag) {
16191625 .elf => &decl.fn_link.elf,
16201626 .macho => &decl.fn_link.macho,
16211627 .wasm => &decl.fn_link.wasm.src_fn,
......@@ -1641,9 +1647,19 @@ pub fn freeDecl(self: *Dwarf, decl: *Module.Decl) void {
16411647 if (self.dbg_line_fn_last == fn_link) {
16421648 self.dbg_line_fn_last = fn_link.prev;
16431649 }
1650
1651 const file_source = decl.getFileScope();
1652 if (self.di_files_lookup.get(file_source)) |dif_index| {
1653 const dif = &self.di_files.items[dif_index];
1654 dif.ref_count -= 1;
1655 if (dif.ref_count == 0) {
1656 self.di_files_free_list.append(gpa, dif_index) catch {};
1657 }
1658 _ = self.di_files_lookup.remove(file_source);
1659 }
16441660}
16451661
1646pub fn writeDbgAbbrev(self: *Dwarf, file: *File) !void {
1662pub fn writeDbgAbbrev(self: *Dwarf) !void {
16471663 // These are LEB encoded but since the values are all less than 127
16481664 // we can simply append these bytes.
16491665 const abbrev_buf = [_]u8{
......@@ -1777,9 +1793,9 @@ pub fn writeDbgAbbrev(self: *Dwarf, file: *File) !void {
17771793 self.abbrev_table_offset = abbrev_offset;
17781794
17791795 const needed_size = abbrev_buf.len;
1780 switch (self.tag) {
1796 switch (self.bin_file.tag) {
17811797 .elf => {
1782 const elf_file = file.cast(File.Elf).?;
1798 const elf_file = self.bin_file.cast(File.Elf).?;
17831799 const debug_abbrev_sect = &elf_file.sections.items[elf_file.debug_abbrev_section_index.?];
17841800 const allocated_size = elf_file.allocatedSize(debug_abbrev_sect.sh_offset);
17851801 if (needed_size > allocated_size) {
......@@ -1796,7 +1812,7 @@ pub fn writeDbgAbbrev(self: *Dwarf, file: *File) !void {
17961812 try elf_file.base.file.?.pwriteAll(&abbrev_buf, file_pos);
17971813 },
17981814 .macho => {
1799 const macho_file = file.cast(File.MachO).?;
1815 const macho_file = self.bin_file.cast(File.MachO).?;
18001816 const d_sym = &macho_file.d_sym.?;
18011817 const dwarf_segment = d_sym.segments.items[d_sym.dwarf_segment_cmd_index.?];
18021818 const debug_abbrev_sect = &d_sym.sections.items[d_sym.debug_abbrev_section_index.?];
......@@ -1816,7 +1832,7 @@ pub fn writeDbgAbbrev(self: *Dwarf, file: *File) !void {
18161832 try d_sym.file.pwriteAll(&abbrev_buf, file_pos);
18171833 },
18181834 .wasm => {
1819 const wasm_file = file.cast(File.Wasm).?;
1835 const wasm_file = self.bin_file.cast(File.Wasm).?;
18201836 const debug_abbrev = &wasm_file.debug_abbrev_atom.?.code;
18211837 try debug_abbrev.resize(wasm_file.base.allocator, needed_size);
18221838 mem.copy(u8, debug_abbrev.items, &abbrev_buf);
......@@ -1830,7 +1846,7 @@ fn dbgInfoHeaderBytes(self: *Dwarf) usize {
18301846 return 120;
18311847}
18321848
1833pub fn writeDbgInfoHeader(self: *Dwarf, file: *File, module: *Module, low_pc: u64, high_pc: u64) !void {
1849pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u64) !void {
18341850 // If this value is null it means there is an error in the module;
18351851 // leave debug_info_header_dirty=true.
18361852 const first_dbg_info_off = self.getDebugInfoOff() orelse return;
......@@ -1842,7 +1858,7 @@ pub fn writeDbgInfoHeader(self: *Dwarf, file: *File, module: *Module, low_pc: u6
18421858 defer di_buf.deinit();
18431859
18441860 const target_endian = self.target.cpu.arch.endian();
1845 const init_len_size: usize = if (self.tag == .macho)
1861 const init_len_size: usize = if (self.bin_file.tag == .macho)
18461862 4
18471863 else switch (self.ptr_width) {
18481864 .p32 => @as(usize, 4),
......@@ -1856,7 +1872,7 @@ pub fn writeDbgInfoHeader(self: *Dwarf, file: *File, module: *Module, low_pc: u6
18561872 // +1 for the final 0 that ends the compilation unit children.
18571873 const dbg_info_end = self.getDebugInfoEnd().? + 1;
18581874 const init_len = dbg_info_end - after_init_len;
1859 if (self.tag == .macho) {
1875 if (self.bin_file.tag == .macho) {
18601876 mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @intCast(u32, init_len));
18611877 } else switch (self.ptr_width) {
18621878 .p32 => {
......@@ -1869,7 +1885,7 @@ pub fn writeDbgInfoHeader(self: *Dwarf, file: *File, module: *Module, low_pc: u6
18691885 }
18701886 mem.writeInt(u16, di_buf.addManyAsArrayAssumeCapacity(2), 4, target_endian); // DWARF version
18711887 const abbrev_offset = self.abbrev_table_offset.?;
1872 if (self.tag == .macho) {
1888 if (self.bin_file.tag == .macho) {
18731889 mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @intCast(u32, abbrev_offset));
18741890 di_buf.appendAssumeCapacity(8); // address size
18751891 } else switch (self.ptr_width) {
......@@ -1888,7 +1904,7 @@ pub fn writeDbgInfoHeader(self: *Dwarf, file: *File, module: *Module, low_pc: u6
18881904 const producer_strp = try self.makeString(link.producer_string);
18891905
18901906 di_buf.appendAssumeCapacity(@enumToInt(AbbrevKind.compile_unit));
1891 if (self.tag == .macho) {
1907 if (self.bin_file.tag == .macho) {
18921908 mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), 0); // DW.AT.stmt_list, DW.FORM.sec_offset
18931909 mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), low_pc);
18941910 mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), high_pc);
......@@ -1913,22 +1929,22 @@ pub fn writeDbgInfoHeader(self: *Dwarf, file: *File, module: *Module, low_pc: u6
19131929 @panic("TODO: handle .debug_info header exceeding its padding");
19141930 }
19151931 const jmp_amt = first_dbg_info_off - di_buf.items.len;
1916 switch (self.tag) {
1932 switch (self.bin_file.tag) {
19171933 .elf => {
1918 const elf_file = file.cast(File.Elf).?;
1934 const elf_file = self.bin_file.cast(File.Elf).?;
19191935 const debug_info_sect = elf_file.sections.items[elf_file.debug_info_section_index.?];
19201936 const file_pos = debug_info_sect.sh_offset;
19211937 try pwriteDbgInfoNops(elf_file.base.file.?, file_pos, 0, di_buf.items, jmp_amt, false);
19221938 },
19231939 .macho => {
1924 const macho_file = file.cast(File.MachO).?;
1940 const macho_file = self.bin_file.cast(File.MachO).?;
19251941 const d_sym = &macho_file.d_sym.?;
19261942 const debug_info_sect = d_sym.sections.items[d_sym.debug_info_section_index.?];
19271943 const file_pos = debug_info_sect.offset;
19281944 try pwriteDbgInfoNops(d_sym.file, file_pos, 0, di_buf.items, jmp_amt, false);
19291945 },
19301946 .wasm => {
1931 const wasm_file = file.cast(File.Wasm).?;
1947 const wasm_file = self.bin_file.cast(File.Wasm).?;
19321948 const debug_info = &wasm_file.debug_info_atom.?.code;
19331949 try writeDbgInfoNopsToArrayList(self.allocator, debug_info, 0, 0, di_buf.items, jmp_amt, false);
19341950 },
......@@ -2158,9 +2174,9 @@ fn writeDbgInfoNopsToArrayList(
21582174 }
21592175}
21602176
2161pub fn writeDbgAranges(self: *Dwarf, file: *File, addr: u64, size: u64) !void {
2177pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void {
21622178 const target_endian = self.target.cpu.arch.endian();
2163 const init_len_size: usize = if (self.tag == .macho)
2179 const init_len_size: usize = if (self.bin_file.tag == .macho)
21642180 4
21652181 else switch (self.ptr_width) {
21662182 .p32 => @as(usize, 4),
......@@ -2182,7 +2198,7 @@ pub fn writeDbgAranges(self: *Dwarf, file: *File, addr: u64, size: u64) !void {
21822198 mem.writeInt(u16, di_buf.addManyAsArrayAssumeCapacity(2), 2, target_endian); // version
21832199 // When more than one compilation unit is supported, this will be the offset to it.
21842200 // For now it is always at offset 0 in .debug_info.
2185 if (self.tag == .macho) {
2201 if (self.bin_file.tag == .macho) {
21862202 mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), 0); // __debug_info offset
21872203 } else {
21882204 self.writeAddrAssumeCapacity(&di_buf, 0); // .debug_info offset
......@@ -2205,7 +2221,7 @@ pub fn writeDbgAranges(self: *Dwarf, file: *File, addr: u64, size: u64) !void {
22052221
22062222 // Go back and populate the initial length.
22072223 const init_len = di_buf.items.len - after_init_len;
2208 if (self.tag == .macho) {
2224 if (self.bin_file.tag == .macho) {
22092225 mem.writeIntLittle(u32, di_buf.items[init_len_index..][0..4], @intCast(u32, init_len));
22102226 } else switch (self.ptr_width) {
22112227 .p32 => {
......@@ -2220,9 +2236,9 @@ pub fn writeDbgAranges(self: *Dwarf, file: *File, addr: u64, size: u64) !void {
22202236 }
22212237
22222238 const needed_size = di_buf.items.len;
2223 switch (self.tag) {
2239 switch (self.bin_file.tag) {
22242240 .elf => {
2225 const elf_file = file.cast(File.Elf).?;
2241 const elf_file = self.bin_file.cast(File.Elf).?;
22262242 const debug_aranges_sect = &elf_file.sections.items[elf_file.debug_aranges_section_index.?];
22272243 const allocated_size = elf_file.allocatedSize(debug_aranges_sect.sh_offset);
22282244 if (needed_size > allocated_size) {
......@@ -2238,7 +2254,7 @@ pub fn writeDbgAranges(self: *Dwarf, file: *File, addr: u64, size: u64) !void {
22382254 try elf_file.base.file.?.pwriteAll(di_buf.items, file_pos);
22392255 },
22402256 .macho => {
2241 const macho_file = file.cast(File.MachO).?;
2257 const macho_file = self.bin_file.cast(File.MachO).?;
22422258 const d_sym = &macho_file.d_sym.?;
22432259 const dwarf_seg = d_sym.segments.items[d_sym.dwarf_segment_cmd_index.?];
22442260 const debug_aranges_sect = &d_sym.sections.items[d_sym.debug_aranges_section_index.?];
......@@ -2258,7 +2274,7 @@ pub fn writeDbgAranges(self: *Dwarf, file: *File, addr: u64, size: u64) !void {
22582274 try d_sym.file.pwriteAll(di_buf.items, file_pos);
22592275 },
22602276 .wasm => {
2261 const wasm_file = file.cast(File.Wasm).?;
2277 const wasm_file = self.bin_file.cast(File.Wasm).?;
22622278 const debug_ranges = &wasm_file.debug_ranges_atom.?.code;
22632279 try debug_ranges.resize(wasm_file.base.allocator, needed_size);
22642280 mem.copy(u8, debug_ranges.items, di_buf.items);
......@@ -2267,10 +2283,10 @@ pub fn writeDbgAranges(self: *Dwarf, file: *File, addr: u64, size: u64) !void {
22672283 }
22682284}
22692285
2270pub fn writeDbgLineHeader(self: *Dwarf, file: *File, module: *Module) !void {
2286pub fn writeDbgLineHeader(self: *Dwarf, module: *Module) !void {
22712287 const ptr_width_bytes: u8 = self.ptrWidthBytes();
22722288 const target_endian = self.target.cpu.arch.endian();
2273 const init_len_size: usize = if (self.tag == .macho)
2289 const init_len_size: usize = if (self.bin_file.tag == .macho)
22742290 4
22752291 else switch (self.ptr_width) {
22762292 .p32 => @as(usize, 4),
......@@ -2293,7 +2309,7 @@ pub fn writeDbgLineHeader(self: *Dwarf, file: *File, module: *Module) !void {
22932309 // not including the initial length itself.
22942310 const after_init_len = di_buf.items.len + init_len_size;
22952311 const init_len = dbg_line_prg_end - after_init_len;
2296 if (self.tag == .macho) {
2312 if (self.bin_file.tag == .macho) {
22972313 mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @intCast(u32, init_len));
22982314 } else switch (self.ptr_width) {
22992315 .p32 => {
......@@ -2312,7 +2328,7 @@ pub fn writeDbgLineHeader(self: *Dwarf, file: *File, module: *Module) !void {
23122328 // Therefore we rely on the NOP jump at the beginning of the Line Number Program for
23132329 // padding rather than this field.
23142330 const before_header_len = di_buf.items.len;
2315 di_buf.items.len += if (self.tag == .macho) @sizeOf(u32) else ptr_width_bytes; // We will come back and write this.
2331 di_buf.items.len += if (self.bin_file.tag == .macho) @sizeOf(u32) else ptr_width_bytes; // We will come back and write this.
23162332 const after_header_len = di_buf.items.len;
23172333
23182334 const opcode_base = DW.LNS.set_isa + 1;
......@@ -2340,20 +2356,12 @@ pub fn writeDbgLineHeader(self: *Dwarf, file: *File, module: *Module) !void {
23402356 1, // `DW.LNS.set_isa`
23412357 0, // include_directories (none except the compilation unit cwd)
23422358 });
2343 // // file_names[0]
2344 // di_buf.appendSliceAssumeCapacity(module.root_pkg.root_src_path); // relative path name
2345 // di_buf.appendSliceAssumeCapacity(&[_]u8{
2346 // 0, // null byte for the relative path name
2347 // 0, // directory_index
2348 // 0, // mtime (TODO supply this)
2349 // 0, // file size bytes (TODO supply this)
2350 // 0, // file_names sentinel
2351 // });
2352
2353 for (self.file_names.items) |off, i| {
2354 const file_name = self.getFileName(off);
2355 log.debug("file_name[{d}] = {s}", .{ i + 1, file_name });
2356 di_buf.appendSliceAssumeCapacity(file_name);
2359
2360 for (self.di_files.items) |dif, i| {
2361 const full_path = try dif.getFullPath(self.allocator);
2362 defer self.allocator.free(full_path);
2363 log.debug("adding new file name at {d} of '{s}'", .{ i + 1, full_path });
2364 di_buf.appendSliceAssumeCapacity(full_path);
23572365 di_buf.appendSliceAssumeCapacity(&[_]u8{
23582366 0, // null byte for the relative path name
23592367 0, // directory_index
......@@ -2364,7 +2372,7 @@ pub fn writeDbgLineHeader(self: *Dwarf, file: *File, module: *Module) !void {
23642372 di_buf.appendAssumeCapacity(0); // file names sentinel
23652373
23662374 const header_len = di_buf.items.len - after_header_len;
2367 if (self.tag == .macho) {
2375 if (self.bin_file.tag == .macho) {
23682376 mem.writeIntLittle(u32, di_buf.items[before_header_len..][0..4], @intCast(u32, header_len));
23692377 } else switch (self.ptr_width) {
23702378 .p32 => {
......@@ -2381,22 +2389,22 @@ pub fn writeDbgLineHeader(self: *Dwarf, file: *File, module: *Module) !void {
23812389 @panic("TODO: handle .debug_line header exceeding its padding");
23822390 }
23832391 const jmp_amt = dbg_line_prg_off - di_buf.items.len;
2384 switch (self.tag) {
2392 switch (self.bin_file.tag) {
23852393 .elf => {
2386 const elf_file = file.cast(File.Elf).?;
2394 const elf_file = self.bin_file.cast(File.Elf).?;
23872395 const debug_line_sect = elf_file.sections.items[elf_file.debug_line_section_index.?];
23882396 const file_pos = debug_line_sect.sh_offset;
23892397 try pwriteDbgLineNops(elf_file.base.file.?, file_pos, 0, di_buf.items, jmp_amt);
23902398 },
23912399 .macho => {
2392 const macho_file = file.cast(File.MachO).?;
2400 const macho_file = self.bin_file.cast(File.MachO).?;
23932401 const d_sym = &macho_file.d_sym.?;
23942402 const debug_line_sect = d_sym.sections.items[d_sym.debug_line_section_index.?];
23952403 const file_pos = debug_line_sect.offset;
23962404 try pwriteDbgLineNops(d_sym.file, file_pos, 0, di_buf.items, jmp_amt);
23972405 },
23982406 .wasm => {
2399 const wasm_file = file.cast(File.Wasm).?;
2407 const wasm_file = self.bin_file.cast(File.Wasm).?;
24002408 const debug_line = wasm_file.debug_line_atom.?.code;
24012409 writeDbgLineNopsBuffered(debug_line.items, 0, 0, di_buf.items, jmp_amt);
24022410 },
......@@ -2436,15 +2444,21 @@ fn dbgLineNeededHeaderBytes(self: Dwarf, module: *Module) u32 {
24362444 const directory_entry_format_count = 1;
24372445 const file_name_entry_format_count = 1;
24382446 const directory_count = 1;
2439 const file_name_count = self.file_names.items.len;
2447 const file_name_count = self.di_files.items.len;
24402448 const root_src_dir_path_len = if (module.root_pkg.root_src_directory.path) |p| p.len else 1; // "."
24412449
2450 var file_names_len: usize = 0;
2451 for (self.di_files.items) |dif| {
2452 const dir_path = dif.file_source.pkg.root_src_directory.path orelse ".";
2453 file_names_len += dir_path.len + dif.file_source.sub_file_path.len + 1;
2454 }
2455
24422456 return @intCast(u32, 53 + directory_entry_format_count * 2 + file_name_entry_format_count * 2 +
24432457 directory_count * 8 + file_name_count * 8 +
24442458 // These are encoded as DW.FORM.string rather than DW.FORM.strp as we would like
24452459 // because of a workaround for readelf and gdb failing to understand DWARFv5 correctly.
24462460 root_src_dir_path_len +
2447 self.file_names_buffer.items.len);
2461 file_names_len);
24482462}
24492463
24502464/// The reloc offset for the line offset of a function from the previous function's line.
......@@ -2476,7 +2490,7 @@ fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
24762490 std.math.maxInt(@TypeOf(actual_size));
24772491}
24782492
2479pub fn flushModule(self: *Dwarf, file: *File, module: *Module) !void {
2493pub fn flushModule(self: *Dwarf, module: *Module) !void {
24802494 if (self.global_abbrev_relocs.items.len > 0) {
24812495 const gpa = self.allocator;
24822496 var arena_alloc = std.heap.ArenaAllocator.init(gpa);
......@@ -2507,19 +2521,19 @@ pub fn flushModule(self: *Dwarf, file: *File, module: *Module) !void {
25072521
25082522 try self.managed_atoms.append(gpa, atom);
25092523 log.debug("updateDeclDebugInfoAllocation in flushModule", .{});
2510 try self.updateDeclDebugInfoAllocation(file, atom, @intCast(u32, dbg_info_buffer.items.len));
2524 try self.updateDeclDebugInfoAllocation(atom, @intCast(u32, dbg_info_buffer.items.len));
25112525 log.debug("writeDeclDebugInfo in flushModule", .{});
2512 try self.writeDeclDebugInfo(file, atom, dbg_info_buffer.items);
2526 try self.writeDeclDebugInfo(atom, dbg_info_buffer.items);
25132527
25142528 const file_pos = blk: {
2515 switch (self.tag) {
2529 switch (self.bin_file.tag) {
25162530 .elf => {
2517 const elf_file = file.cast(File.Elf).?;
2531 const elf_file = self.bin_file.cast(File.Elf).?;
25182532 const debug_info_sect = &elf_file.sections.items[elf_file.debug_info_section_index.?];
25192533 break :blk debug_info_sect.sh_offset;
25202534 },
25212535 .macho => {
2522 const macho_file = file.cast(File.MachO).?;
2536 const macho_file = self.bin_file.cast(File.MachO).?;
25232537 const d_sym = &macho_file.d_sym.?;
25242538 const debug_info_sect = &d_sym.sections.items[d_sym.debug_info_section_index.?];
25252539 break :blk debug_info_sect.offset;
......@@ -2534,18 +2548,18 @@ pub fn flushModule(self: *Dwarf, file: *File, module: *Module) !void {
25342548 mem.writeInt(u32, &buf, atom.off, self.target.cpu.arch.endian());
25352549
25362550 while (self.global_abbrev_relocs.popOrNull()) |reloc| {
2537 switch (self.tag) {
2551 switch (self.bin_file.tag) {
25382552 .elf => {
2539 const elf_file = file.cast(File.Elf).?;
2553 const elf_file = self.bin_file.cast(File.Elf).?;
25402554 try elf_file.base.file.?.pwriteAll(&buf, file_pos + reloc.atom.off + reloc.offset);
25412555 },
25422556 .macho => {
2543 const macho_file = file.cast(File.MachO).?;
2557 const macho_file = self.bin_file.cast(File.MachO).?;
25442558 const d_sym = &macho_file.d_sym.?;
25452559 try d_sym.file.pwriteAll(&buf, file_pos + reloc.atom.off + reloc.offset);
25462560 },
25472561 .wasm => {
2548 const wasm_file = file.cast(File.Wasm).?;
2562 const wasm_file = self.bin_file.cast(File.Wasm).?;
25492563 const debug_info = wasm_file.debug_info_atom.?.code;
25502564 mem.copy(u8, debug_info.items[reloc.atom.off + reloc.offset ..], &buf);
25512565 },
......@@ -2555,17 +2569,17 @@ pub fn flushModule(self: *Dwarf, file: *File, module: *Module) !void {
25552569 }
25562570}
25572571
2558fn allocateFileIndex(self: *Dwarf) !u28 {
2559 try self.file_names.ensureUnusedCapacity(self.allocator, 1);
2572fn allocateDIFileIndex(self: *Dwarf) !u28 {
2573 try self.di_files.ensureUnusedCapacity(self.allocator, 1);
25602574
25612575 const index = blk: {
2562 if (self.file_names_free_list.popOrNull()) |index| {
2563 log.debug(" (reusing file name index {d})", .{index});
2576 if (self.di_files_free_list.popOrNull()) |index| {
2577 log.debug(" (reusing DIFile index {d})", .{index});
25642578 break :blk index;
25652579 } else {
2566 const index = @intCast(u28, self.file_names.items.len);
2567 log.debug(" (allocating file name index {d})", .{index});
2568 _ = self.file_names.addOneAssumeCapacity();
2580 const index = @intCast(u28, self.di_files.items.len);
2581 log.debug(" (allocating DIFile index {d})", .{index});
2582 _ = self.di_files.addOneAssumeCapacity();
25692583 break :blk index;
25702584 }
25712585 };
......@@ -2573,27 +2587,28 @@ fn allocateFileIndex(self: *Dwarf) !u28 {
25732587 return index;
25742588}
25752589
2576fn addFileName(self: *Dwarf, mod: *Module, decl_index: Module.Decl.Index) !u28 {
2590fn addDIFile(self: *Dwarf, mod: *Module, decl_index: Module.Decl.Index) !u28 {
25772591 const decl = mod.declPtr(decl_index);
25782592 const file_scope = decl.getFileScope();
2579 if (self.file_names_lookup.get(file_scope)) |file_index| {
2580 return file_index;
2581 }
2582 const index = try self.allocateFileIndex();
2583 const file_name = try file_scope.fullPath(self.allocator);
2584 defer self.allocator.free(file_name);
2585 try self.file_names_buffer.ensureUnusedCapacity(self.allocator, file_name.len + 1);
2586 const off = @intCast(u32, self.file_names_buffer.items.len);
2587 self.file_names_buffer.appendSliceAssumeCapacity(file_name);
2588 self.file_names_buffer.appendAssumeCapacity(0);
2589 self.file_names.items[index] = off;
2590 try self.file_names_lookup.putNoClobber(self.allocator, file_scope, index);
2591 return index;
2592}
2593 const gop = try self.di_files_lookup.getOrPut(self.allocator, file_scope);
2594 if (!gop.found_existing) {
2595 gop.value_ptr.* = try self.allocateDIFileIndex();
2596 self.di_files.items[gop.value_ptr.*] = .{
2597 .file_source = file_scope,
2598 .ref_count = 1,
2599 };
25932600
2594fn getFileName(self: Dwarf, off: u32) []const u8 {
2595 assert(off < self.file_names_buffer.items.len);
2596 return mem.sliceTo(@ptrCast([*:0]const u8, self.file_names_buffer.items.ptr) + off, 0);
2601 switch (self.bin_file.tag) {
2602 .elf => self.bin_file.cast(File.Elf).?.debug_line_header_dirty = true,
2603 .macho => self.bin_file.cast(File.MachO).?.d_sym.?.debug_line_header_dirty = true,
2604 .wasm => {},
2605 else => unreachable,
2606 }
2607 } else {
2608 const dif = &self.di_files.items[gop.value_ptr.*];
2609 dif.ref_count += 1;
2610 }
2611 return gop.value_ptr.*;
25972612}
25982613
25992614fn addDbgInfoErrorSet(
src/link/Elf.zig+7-9
......@@ -312,7 +312,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Elf {
312312 };
313313
314314 var dwarf: ?Dwarf = if (!options.strip and options.module != null)
315 Dwarf.init(gpa, .elf, options.target)
315 Dwarf.init(gpa, &self.base, options.target)
316316 else
317317 null;
318318
......@@ -972,7 +972,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
972972 const foreign_endian = target_endian != builtin.cpu.arch.endian();
973973
974974 if (self.dwarf) |*dw| {
975 try dw.flushModule(&self.base, module);
975 try dw.flushModule(module);
976976 }
977977
978978 {
......@@ -1020,7 +1020,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10201020
10211021 if (self.dwarf) |*dw| {
10221022 if (self.debug_abbrev_section_dirty) {
1023 try dw.writeDbgAbbrev(&self.base);
1023 try dw.writeDbgAbbrev();
10241024 if (!self.shdr_table_dirty) {
10251025 // Then it won't get written with the others and we need to do it.
10261026 try self.writeSectHeader(self.debug_abbrev_section_index.?);
......@@ -1034,7 +1034,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10341034 const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?];
10351035 const low_pc = text_phdr.p_vaddr;
10361036 const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz;
1037 try dw.writeDbgInfoHeader(&self.base, module, low_pc, high_pc);
1037 try dw.writeDbgInfoHeader(module, low_pc, high_pc);
10381038 self.debug_info_header_dirty = false;
10391039 }
10401040
......@@ -1042,7 +1042,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10421042 // Currently only one compilation unit is supported, so the address range is simply
10431043 // identical to the main program header virtual address and memory size.
10441044 const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?];
1045 try dw.writeDbgAranges(&self.base, text_phdr.p_vaddr, text_phdr.p_memsz);
1045 try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz);
10461046 if (!self.shdr_table_dirty) {
10471047 // Then it won't get written with the others and we need to do it.
10481048 try self.writeSectHeader(self.debug_aranges_section_index.?);
......@@ -1051,7 +1051,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10511051 }
10521052
10531053 if (self.debug_line_header_dirty) {
1054 try dw.writeDbgLineHeader(&self.base, module);
1054 try dw.writeDbgLineHeader(module);
10551055 self.debug_line_header_dirty = false;
10561056 }
10571057 }
......@@ -2422,7 +2422,6 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
24222422 const local_sym = try self.updateDeclCode(decl_index, code, elf.STT_FUNC);
24232423 if (decl_state) |*ds| {
24242424 try self.dwarf.?.commitDeclState(
2425 &self.base,
24262425 module,
24272426 decl_index,
24282427 local_sym.st_value,
......@@ -2499,7 +2498,6 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
24992498 const local_sym = try self.updateDeclCode(decl_index, code, elf.STT_OBJECT);
25002499 if (decl_state) |*ds| {
25012500 try self.dwarf.?.commitDeclState(
2502 &self.base,
25032501 module,
25042502 decl_index,
25052503 local_sym.st_value,
......@@ -2692,7 +2690,7 @@ pub fn updateDeclLineNumber(self: *Elf, mod: *Module, decl: *const Module.Decl)
26922690
26932691 if (self.llvm_object) |_| return;
26942692 if (self.dwarf) |*dw| {
2695 try dw.updateDeclLineNumber(&self.base, decl);
2693 try dw.updateDeclLineNumber(decl);
26962694 }
26972695}
26982696
src/link/MachO.zig+3-5
......@@ -347,7 +347,7 @@ pub fn openPath(allocator: Allocator, options: link.Options) !*MachO {
347347
348348 self.d_sym = .{
349349 .allocator = allocator,
350 .dwarf = link.File.Dwarf.init(allocator, .macho, options.target),
350 .dwarf = link.File.Dwarf.init(allocator, &self.base, options.target),
351351 .file = d_sym_file,
352352 .page_size = self.page_size,
353353 };
......@@ -449,7 +449,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
449449 const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented;
450450
451451 if (self.d_sym) |*d_sym| {
452 try d_sym.dwarf.flushModule(&self.base, module);
452 try d_sym.dwarf.flushModule(module);
453453 }
454454
455455 var libs = std.StringArrayHashMap(link.SystemLib).init(arena);
......@@ -2213,7 +2213,6 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv
22132213
22142214 if (decl_state) |*ds| {
22152215 try self.d_sym.?.dwarf.commitDeclState(
2216 &self.base,
22172216 module,
22182217 decl_index,
22192218 addr,
......@@ -2364,7 +2363,6 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
23642363
23652364 if (decl_state) |*ds| {
23662365 try self.d_sym.?.dwarf.commitDeclState(
2367 &self.base,
23682366 module,
23692367 decl_index,
23702368 addr,
......@@ -2603,7 +2601,7 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []const u8)
26032601pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl: *const Module.Decl) !void {
26042602 _ = module;
26052603 if (self.d_sym) |*d_sym| {
2606 try d_sym.dwarf.updateDeclLineNumber(&self.base, decl);
2604 try d_sym.dwarf.updateDeclLineNumber(decl);
26072605 }
26082606}
26092607
src/link/MachO/DebugSymbols.zig+4-4
......@@ -213,7 +213,7 @@ pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void {
213213 }
214214
215215 if (self.debug_abbrev_section_dirty) {
216 try self.dwarf.writeDbgAbbrev(&macho_file.base);
216 try self.dwarf.writeDbgAbbrev();
217217 self.debug_abbrev_section_dirty = false;
218218 }
219219
......@@ -223,7 +223,7 @@ pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void {
223223 const text_section = macho_file.sections.items(.header)[macho_file.text_section_index.?];
224224 const low_pc = text_section.addr;
225225 const high_pc = text_section.addr + text_section.size;
226 try self.dwarf.writeDbgInfoHeader(&macho_file.base, module, low_pc, high_pc);
226 try self.dwarf.writeDbgInfoHeader(module, low_pc, high_pc);
227227 self.debug_info_header_dirty = false;
228228 }
229229
......@@ -231,12 +231,12 @@ pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void {
231231 // Currently only one compilation unit is supported, so the address range is simply
232232 // identical to the main program header virtual address and memory size.
233233 const text_section = macho_file.sections.items(.header)[macho_file.text_section_index.?];
234 try self.dwarf.writeDbgAranges(&macho_file.base, text_section.addr, text_section.size);
234 try self.dwarf.writeDbgAranges(text_section.addr, text_section.size);
235235 self.debug_aranges_section_dirty = false;
236236 }
237237
238238 if (self.debug_line_header_dirty) {
239 try self.dwarf.writeDbgLineHeader(&macho_file.base, module);
239 try self.dwarf.writeDbgLineHeader(module);
240240 self.debug_line_header_dirty = false;
241241 }
242242
src/link/Wasm.zig+7-8
......@@ -361,7 +361,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
361361 }
362362
363363 if (!options.strip and options.module != null) {
364 wasm_bin.dwarf = Dwarf.init(allocator, .wasm, options.target);
364 wasm_bin.dwarf = Dwarf.init(allocator, &wasm_bin.base, options.target);
365365 try wasm_bin.initDebugSections();
366366 }
367367
......@@ -910,7 +910,6 @@ pub fn updateFunc(wasm: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes
910910
911911 if (wasm.dwarf) |*dwarf| {
912912 try dwarf.commitDeclState(
913 &wasm.base,
914913 mod,
915914 decl_index,
916915 // Actual value will be written after relocation.
......@@ -990,7 +989,7 @@ pub fn updateDeclLineNumber(wasm: *Wasm, mod: *Module, decl: *const Module.Decl)
990989 defer wasm.base.allocator.free(decl_name);
991990
992991 log.debug("updateDeclLineNumber {s}{*}", .{ decl_name, decl });
993 try dw.updateDeclLineNumber(&wasm.base, decl);
992 try dw.updateDeclLineNumber(decl);
994993 }
995994}
996995
......@@ -2299,7 +2298,7 @@ pub fn flushModule(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
22992298 }
23002299
23012300 if (wasm.dwarf) |*dwarf| {
2302 try dwarf.flushModule(&wasm.base, wasm.base.options.module.?);
2301 try dwarf.flushModule(wasm.base.options.module.?);
23032302 }
23042303 }
23052304
......@@ -2668,12 +2667,12 @@ pub fn flushModule(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
26682667 if (!wasm.base.options.strip) {
26692668 if (wasm.dwarf) |*dwarf| {
26702669 const mod = wasm.base.options.module.?;
2671 try dwarf.writeDbgAbbrev(&wasm.base);
2670 try dwarf.writeDbgAbbrev();
26722671 // for debug info and ranges, the address is always 0,
26732672 // as locations are always offsets relative to 'code' section.
2674 try dwarf.writeDbgInfoHeader(&wasm.base, mod, 0, code_section_size);
2675 try dwarf.writeDbgAranges(&wasm.base, 0, code_section_size);
2676 try dwarf.writeDbgLineHeader(&wasm.base, mod);
2673 try dwarf.writeDbgInfoHeader(mod, 0, code_section_size);
2674 try dwarf.writeDbgAranges(0, code_section_size);
2675 try dwarf.writeDbgLineHeader(mod);
26772676 }
26782677
26792678 var debug_bytes = std.ArrayList(u8).init(wasm.base.allocator);