authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-08 10:37:21+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-09 09:24:25+01:00
log4bb66b63ba108a1c7e5c920348d72ed1e169fd99
tree4b668e4283f3b6a9ba51bef5786aeb4903b66028
parent05e221796a7c60f26c4c7bc996f12e1392c83d97

macho: add helper for getting ptr to DebugSymbols


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

src/link/Dwarf.zig+13-25
...@@ -1104,8 +1104,7 @@ pub fn commitDeclState(...@@ -1104,8 +1104,7 @@ pub fn commitDeclState(
1104 try pwriteDbgLineNops(elf_file.base.file.?, file_pos, 0, &[0]u8{}, src_fn.len);1104 try pwriteDbgLineNops(elf_file.base.file.?, file_pos, 0, &[0]u8{}, src_fn.len);
1105 },1105 },
1106 .macho => {1106 .macho => {
1107 const macho_file = self.bin_file.cast(File.MachO).?;1107 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
1108 const d_sym = &macho_file.d_sym.?;
1109 const debug_line_sect = d_sym.getSectionPtr(d_sym.debug_line_section_index.?);1108 const debug_line_sect = d_sym.getSectionPtr(d_sym.debug_line_section_index.?);
1110 const file_pos = debug_line_sect.offset + src_fn.off;1109 const file_pos = debug_line_sect.offset + src_fn.off;
1111 try pwriteDbgLineNops(d_sym.file, file_pos, 0, &[0]u8{}, src_fn.len);1110 try pwriteDbgLineNops(d_sym.file, file_pos, 0, &[0]u8{}, src_fn.len);
...@@ -1327,8 +1326,7 @@ pub fn commitDeclState(...@@ -1327,8 +1326,7 @@ pub fn commitDeclState(
1327 while (decl_state.exprloc_relocs.popOrNull()) |reloc| {1326 while (decl_state.exprloc_relocs.popOrNull()) |reloc| {
1328 switch (self.bin_file.tag) {1327 switch (self.bin_file.tag) {
1329 .macho => {1328 .macho => {
1330 const macho_file = self.bin_file.cast(File.MachO).?;1329 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
1331 const d_sym = &macho_file.d_sym.?;
1332 try d_sym.relocs.append(d_sym.allocator, .{1330 try d_sym.relocs.append(d_sym.allocator, .{
1333 .type = switch (reloc.type) {1331 .type = switch (reloc.type) {
1334 .direct_load => .direct_load,1332 .direct_load => .direct_load,
...@@ -1379,8 +1377,7 @@ fn updateDeclDebugInfoAllocation(self: *Dwarf, atom: *Atom, len: u32) !void {...@@ -1379,8 +1377,7 @@ fn updateDeclDebugInfoAllocation(self: *Dwarf, atom: *Atom, len: u32) !void {
1379 try pwriteDbgInfoNops(elf_file.base.file.?, file_pos, 0, &[0]u8{}, atom.len, false);1377 try pwriteDbgInfoNops(elf_file.base.file.?, file_pos, 0, &[0]u8{}, atom.len, false);
1380 },1378 },
1381 .macho => {1379 .macho => {
1382 const macho_file = self.bin_file.cast(File.MachO).?;1380 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
1383 const d_sym = &macho_file.d_sym.?;
1384 const debug_info_sect = d_sym.getSectionPtr(d_sym.debug_info_section_index.?);1381 const debug_info_sect = d_sym.getSectionPtr(d_sym.debug_info_section_index.?);
1385 const file_pos = debug_info_sect.offset + atom.off;1382 const file_pos = debug_info_sect.offset + atom.off;
1386 try pwriteDbgInfoNops(d_sym.file, file_pos, 0, &[0]u8{}, atom.len, false);1383 try pwriteDbgInfoNops(d_sym.file, file_pos, 0, &[0]u8{}, atom.len, false);
...@@ -1475,8 +1472,7 @@ fn writeDeclDebugInfo(self: *Dwarf, atom: *Atom, dbg_info_buf: []const u8) !void...@@ -1475,8 +1472,7 @@ fn writeDeclDebugInfo(self: *Dwarf, atom: *Atom, dbg_info_buf: []const u8) !void
1475 },1472 },
14761473
1477 .macho => {1474 .macho => {
1478 const macho_file = self.bin_file.cast(File.MachO).?;1475 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
1479 const d_sym = &macho_file.d_sym.?;
1480 const sect_index = d_sym.debug_info_section_index.?;1476 const sect_index = d_sym.debug_info_section_index.?;
1481 try d_sym.growSection(sect_index, needed_size);1477 try d_sym.growSection(sect_index, needed_size);
1482 const sect = d_sym.getSection(sect_index);1478 const sect = d_sym.getSection(sect_index);
...@@ -1544,9 +1540,8 @@ pub fn updateDeclLineNumber(self: *Dwarf, decl: *const Module.Decl) !void {...@@ -1544,9 +1540,8 @@ pub fn updateDeclLineNumber(self: *Dwarf, decl: *const Module.Decl) !void {
1544 try elf_file.base.file.?.pwriteAll(&data, file_pos);1540 try elf_file.base.file.?.pwriteAll(&data, file_pos);
1545 },1541 },
1546 .macho => {1542 .macho => {
1547 const macho_file = self.bin_file.cast(File.MachO).?;1543 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
1548 const d_sym = macho_file.d_sym.?;1544 const sect = d_sym.getSection(d_sym.debug_line_section_index.?);
1549 const sect = d_sym.sections.items[d_sym.debug_line_section_index.?];
1550 const file_pos = sect.offset + decl.fn_link.macho.off + self.getRelocDbgLineOff();1545 const file_pos = sect.offset + decl.fn_link.macho.off + self.getRelocDbgLineOff();
1551 try d_sym.file.pwriteAll(&data, file_pos);1546 try d_sym.file.pwriteAll(&data, file_pos);
1552 },1547 },
...@@ -1769,8 +1764,7 @@ pub fn writeDbgAbbrev(self: *Dwarf) !void {...@@ -1769,8 +1764,7 @@ pub fn writeDbgAbbrev(self: *Dwarf) !void {
1769 try elf_file.base.file.?.pwriteAll(&abbrev_buf, file_pos);1764 try elf_file.base.file.?.pwriteAll(&abbrev_buf, file_pos);
1770 },1765 },
1771 .macho => {1766 .macho => {
1772 const macho_file = self.bin_file.cast(File.MachO).?;1767 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
1773 const d_sym = &macho_file.d_sym.?;
1774 const dwarf_segment = d_sym.segments.items[d_sym.dwarf_segment_cmd_index.?];1768 const dwarf_segment = d_sym.segments.items[d_sym.dwarf_segment_cmd_index.?];
1775 const debug_abbrev_sect = &d_sym.sections.items[d_sym.debug_abbrev_section_index.?];1769 const debug_abbrev_sect = &d_sym.sections.items[d_sym.debug_abbrev_section_index.?];
1776 const allocated_size = d_sym.allocatedSize(debug_abbrev_sect.offset);1770 const allocated_size = d_sym.allocatedSize(debug_abbrev_sect.offset);
...@@ -1895,8 +1889,7 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u...@@ -1895,8 +1889,7 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u
1895 try pwriteDbgInfoNops(elf_file.base.file.?, file_pos, 0, di_buf.items, jmp_amt, false);1889 try pwriteDbgInfoNops(elf_file.base.file.?, file_pos, 0, di_buf.items, jmp_amt, false);
1896 },1890 },
1897 .macho => {1891 .macho => {
1898 const macho_file = self.bin_file.cast(File.MachO).?;1892 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
1899 const d_sym = &macho_file.d_sym.?;
1900 const debug_info_sect = d_sym.getSection(d_sym.debug_info_section_index.?);1893 const debug_info_sect = d_sym.getSection(d_sym.debug_info_section_index.?);
1901 const file_pos = debug_info_sect.offset;1894 const file_pos = debug_info_sect.offset;
1902 try pwriteDbgInfoNops(d_sym.file, file_pos, 0, di_buf.items, jmp_amt, false);1895 try pwriteDbgInfoNops(d_sym.file, file_pos, 0, di_buf.items, jmp_amt, false);
...@@ -2227,8 +2220,7 @@ pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void {...@@ -2227,8 +2220,7 @@ pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void {
2227 try elf_file.base.file.?.pwriteAll(di_buf.items, file_pos);2220 try elf_file.base.file.?.pwriteAll(di_buf.items, file_pos);
2228 },2221 },
2229 .macho => {2222 .macho => {
2230 const macho_file = self.bin_file.cast(File.MachO).?;2223 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
2231 const d_sym = &macho_file.d_sym.?;
2232 const dwarf_seg = d_sym.segments.items[d_sym.dwarf_segment_cmd_index.?];2224 const dwarf_seg = d_sym.segments.items[d_sym.dwarf_segment_cmd_index.?];
2233 const debug_aranges_sect = &d_sym.sections.items[d_sym.debug_aranges_section_index.?];2225 const debug_aranges_sect = &d_sym.sections.items[d_sym.debug_aranges_section_index.?];
2234 const allocated_size = d_sym.allocatedSize(debug_aranges_sect.offset);2226 const allocated_size = d_sym.allocatedSize(debug_aranges_sect.offset);
...@@ -2385,8 +2377,7 @@ pub fn writeDbgLineHeader(self: *Dwarf, module: *Module) !void {...@@ -2385,8 +2377,7 @@ pub fn writeDbgLineHeader(self: *Dwarf, module: *Module) !void {
2385 const needed_with_padding = padToIdeal(needed_bytes);2377 const needed_with_padding = padToIdeal(needed_bytes);
2386 const delta = needed_with_padding - dbg_line_prg_off;2378 const delta = needed_with_padding - dbg_line_prg_off;
23872379
2388 const macho_file = self.bin_file.cast(File.MachO).?;2380 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
2389 const d_sym = &macho_file.d_sym.?;
2390 const debug_line_sect = d_sym.getSectionPtr(d_sym.debug_line_section_index.?);2381 const debug_line_sect = d_sym.getSectionPtr(d_sym.debug_line_section_index.?);
2391 const needed_size = debug_line_sect.size + delta;2382 const needed_size = debug_line_sect.size + delta;
23922383
...@@ -2442,8 +2433,7 @@ pub fn writeDbgLineHeader(self: *Dwarf, module: *Module) !void {...@@ -2442,8 +2433,7 @@ pub fn writeDbgLineHeader(self: *Dwarf, module: *Module) !void {
2442 try pwriteDbgLineNops(elf_file.base.file.?, file_pos, 0, di_buf.items, jmp_amt);2433 try pwriteDbgLineNops(elf_file.base.file.?, file_pos, 0, di_buf.items, jmp_amt);
2443 },2434 },
2444 .macho => {2435 .macho => {
2445 const macho_file = self.bin_file.cast(File.MachO).?;2436 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
2446 const d_sym = &macho_file.d_sym.?;
2447 const debug_line_sect = d_sym.getSection(d_sym.debug_line_section_index.?);2437 const debug_line_sect = d_sym.getSection(d_sym.debug_line_section_index.?);
2448 const file_pos = debug_line_sect.offset;2438 const file_pos = debug_line_sect.offset;
2449 try pwriteDbgLineNops(d_sym.file, file_pos, 0, di_buf.items, jmp_amt);2439 try pwriteDbgLineNops(d_sym.file, file_pos, 0, di_buf.items, jmp_amt);
...@@ -2585,8 +2575,7 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void {...@@ -2585,8 +2575,7 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void {
2585 break :blk debug_info_sect.sh_offset;2575 break :blk debug_info_sect.sh_offset;
2586 },2576 },
2587 .macho => {2577 .macho => {
2588 const macho_file = self.bin_file.cast(File.MachO).?;2578 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
2589 const d_sym = &macho_file.d_sym.?;
2590 const debug_info_sect = d_sym.getSectionPtr(d_sym.debug_info_section_index.?);2579 const debug_info_sect = d_sym.getSectionPtr(d_sym.debug_info_section_index.?);
2591 break :blk debug_info_sect.offset;2580 break :blk debug_info_sect.offset;
2592 },2581 },
...@@ -2606,8 +2595,7 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void {...@@ -2606,8 +2595,7 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void {
2606 try elf_file.base.file.?.pwriteAll(&buf, file_pos + reloc.atom.off + reloc.offset);2595 try elf_file.base.file.?.pwriteAll(&buf, file_pos + reloc.atom.off + reloc.offset);
2607 },2596 },
2608 .macho => {2597 .macho => {
2609 const macho_file = self.bin_file.cast(File.MachO).?;2598 const d_sym = self.bin_file.cast(File.MachO).?.getDebugSymbols().?;
2610 const d_sym = &macho_file.d_sym.?;
2611 try d_sym.file.pwriteAll(&buf, file_pos + reloc.atom.off + reloc.offset);2599 try d_sym.file.pwriteAll(&buf, file_pos + reloc.atom.off + reloc.offset);
2612 },2600 },
2613 .wasm => {2601 .wasm => {
src/link/MachO.zig+5
...@@ -4300,6 +4300,11 @@ pub fn getEntryPoint(self: MachO) error{MissingMainEntrypoint}!SymbolWithLoc {...@@ -4300,6 +4300,11 @@ pub fn getEntryPoint(self: MachO) error{MissingMainEntrypoint}!SymbolWithLoc {
4300 return global;4300 return global;
4301}4301}
43024302
4303pub fn getDebugSymbols(self: *MachO) ?*DebugSymbols {
4304 if (self.d_sym == null) return null;
4305 return &self.d_sym.?;
4306}
4307
4303pub fn findFirst(comptime T: type, haystack: []align(1) const T, start: usize, predicate: anytype) usize {4308pub fn findFirst(comptime T: type, haystack: []align(1) const T, start: usize, predicate: anytype) usize {
4304 if (!@hasDecl(@TypeOf(predicate), "predicate"))4309 if (!@hasDecl(@TypeOf(predicate), "predicate"))
4305 @compileError("Predicate is required to define fn predicate(@This(), T) bool");4310 @compileError("Predicate is required to define fn predicate(@This(), T) bool");