authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-08 10:19:52+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-09 09:24:25+01:00
log05e221796a7c60f26c4c7bc996f12e1392c83d97
tree03e3fa5caa6156887a36d16daf522d44c7d7195d
parent4c38ba7d1b64ac69bbb7cc612503bd32661cbbe5

dwarf+d_sym: move logic for growing section to d_sym


2 files changed, 55 insertions(+), 30 deletions(-)

src/link/Dwarf.zig+11-30
......@@ -1106,7 +1106,7 @@ pub fn commitDeclState(
11061106 .macho => {
11071107 const macho_file = self.bin_file.cast(File.MachO).?;
11081108 const d_sym = &macho_file.d_sym.?;
1109 const debug_line_sect = &d_sym.sections.items[d_sym.debug_line_section_index.?];
1109 const debug_line_sect = d_sym.getSectionPtr(d_sym.debug_line_section_index.?);
11101110 const file_pos = debug_line_sect.offset + src_fn.off;
11111111 try pwriteDbgLineNops(d_sym.file, file_pos, 0, &[0]u8{}, src_fn.len);
11121112 },
......@@ -1187,7 +1187,7 @@ pub fn commitDeclState(
11871187 .macho => {
11881188 const macho_file = self.bin_file.cast(File.MachO).?;
11891189 const d_sym = &macho_file.d_sym.?;
1190 const debug_line_sect = &d_sym.sections.items[d_sym.debug_line_section_index.?];
1190 const debug_line_sect = d_sym.getSectionPtr(d_sym.debug_line_section_index.?);
11911191 if (needed_size != debug_line_sect.size) {
11921192 if (needed_size > d_sym.allocatedSize(debug_line_sect.offset)) {
11931193 const new_offset = d_sym.findFreeSpace(needed_size, 1);
......@@ -1381,7 +1381,7 @@ fn updateDeclDebugInfoAllocation(self: *Dwarf, atom: *Atom, len: u32) !void {
13811381 .macho => {
13821382 const macho_file = self.bin_file.cast(File.MachO).?;
13831383 const d_sym = &macho_file.d_sym.?;
1384 const debug_info_sect = &d_sym.sections.items[d_sym.debug_info_section_index.?];
1384 const debug_info_sect = d_sym.getSectionPtr(d_sym.debug_info_section_index.?);
13851385 const file_pos = debug_info_sect.offset + atom.off;
13861386 try pwriteDbgInfoNops(d_sym.file, file_pos, 0, &[0]u8{}, atom.len, false);
13871387 },
......@@ -1477,29 +1477,10 @@ fn writeDeclDebugInfo(self: *Dwarf, atom: *Atom, dbg_info_buf: []const u8) !void
14771477 .macho => {
14781478 const macho_file = self.bin_file.cast(File.MachO).?;
14791479 const d_sym = &macho_file.d_sym.?;
1480 const debug_info_sect = &d_sym.sections.items[d_sym.debug_info_section_index.?];
1481 if (needed_size != debug_info_sect.size) {
1482 if (needed_size > d_sym.allocatedSize(debug_info_sect.offset)) {
1483 const new_offset = d_sym.findFreeSpace(needed_size, 1);
1484 const existing_size = last_decl.off;
1485 std.log.scoped(.dsym).debug("moving __debug_info section: {} bytes from 0x{x} to 0x{x}", .{
1486 existing_size,
1487 debug_info_sect.offset,
1488 new_offset,
1489 });
1490 const amt = try d_sym.file.copyRangeAll(
1491 debug_info_sect.offset,
1492 d_sym.file,
1493 new_offset,
1494 existing_size,
1495 );
1496 if (amt != existing_size) return error.InputOutput;
1497 debug_info_sect.offset = @intCast(u32, new_offset);
1498 }
1499 debug_info_sect.size = needed_size;
1500 d_sym.debug_info_header_dirty = true;
1501 }
1502 const file_pos = debug_info_sect.offset + atom.off;
1480 const sect_index = d_sym.debug_info_section_index.?;
1481 try d_sym.growSection(sect_index, needed_size);
1482 const sect = d_sym.getSection(sect_index);
1483 const file_pos = sect.offset + atom.off;
15031484 try pwriteDbgInfoNops(
15041485 d_sym.file,
15051486 file_pos,
......@@ -1916,7 +1897,7 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u
19161897 .macho => {
19171898 const macho_file = self.bin_file.cast(File.MachO).?;
19181899 const d_sym = &macho_file.d_sym.?;
1919 const debug_info_sect = d_sym.sections.items[d_sym.debug_info_section_index.?];
1900 const debug_info_sect = d_sym.getSection(d_sym.debug_info_section_index.?);
19201901 const file_pos = debug_info_sect.offset;
19211902 try pwriteDbgInfoNops(d_sym.file, file_pos, 0, di_buf.items, jmp_amt, false);
19221903 },
......@@ -2406,7 +2387,7 @@ pub fn writeDbgLineHeader(self: *Dwarf, module: *Module) !void {
24062387
24072388 const macho_file = self.bin_file.cast(File.MachO).?;
24082389 const d_sym = &macho_file.d_sym.?;
2409 const debug_line_sect = &d_sym.sections.items[d_sym.debug_line_section_index.?];
2390 const debug_line_sect = d_sym.getSectionPtr(d_sym.debug_line_section_index.?);
24102391 const needed_size = debug_line_sect.size + delta;
24112392
24122393 if (needed_size > d_sym.allocatedSize(debug_line_sect.offset)) {
......@@ -2463,7 +2444,7 @@ pub fn writeDbgLineHeader(self: *Dwarf, module: *Module) !void {
24632444 .macho => {
24642445 const macho_file = self.bin_file.cast(File.MachO).?;
24652446 const d_sym = &macho_file.d_sym.?;
2466 const debug_line_sect = d_sym.sections.items[d_sym.debug_line_section_index.?];
2447 const debug_line_sect = d_sym.getSection(d_sym.debug_line_section_index.?);
24672448 const file_pos = debug_line_sect.offset;
24682449 try pwriteDbgLineNops(d_sym.file, file_pos, 0, di_buf.items, jmp_amt);
24692450 },
......@@ -2606,7 +2587,7 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void {
26062587 .macho => {
26072588 const macho_file = self.bin_file.cast(File.MachO).?;
26082589 const d_sym = &macho_file.d_sym.?;
2609 const debug_info_sect = &d_sym.sections.items[d_sym.debug_info_section_index.?];
2590 const debug_info_sect = d_sym.getSectionPtr(d_sym.debug_info_section_index.?);
26102591 break :blk debug_info_sect.offset;
26112592 },
26122593 // for wasm, the offset is always 0 as we write to memory first
src/link/MachO/DebugSymbols.zig+44
......@@ -148,6 +148,40 @@ fn allocateSection(self: *DebugSymbols, sectname: []const u8, size: u64, alignme
148148 return index;
149149}
150150
151pub fn growSection(self: *DebugSymbols, sect_index: u8, needed_size: u32) !void {
152 const sect = self.getSectionPtr(sect_index);
153
154 if (needed_size > self.allocatedSize(sect.offset)) {
155 const new_offset = self.findFreeSpace(needed_size, 1);
156
157 log.debug("moving {s} section: {} bytes from 0x{x} to 0x{x}", .{
158 sect.sectName(),
159 sect.size,
160 sect.offset,
161 new_offset,
162 });
163
164 const amt = try self.file.copyRangeAll(
165 sect.offset,
166 self.file,
167 new_offset,
168 sect.size,
169 );
170 if (amt != sect.size) return error.InputOutput;
171 sect.offset = @intCast(u32, new_offset);
172 }
173 sect.size = needed_size;
174 self.markDirty(sect_index);
175}
176
177pub fn markDirty(self: *DebugSymbols, sect_index: u8) void {
178 if (self.debug_info_section_index.? == sect_index) {
179 self.debug_info_header_dirty = true;
180 } else if (self.debug_line_section_index.? == sect_index) {
181 self.debug_line_header_dirty = true;
182 }
183}
184
151185fn detectAllocCollision(self: *DebugSymbols, start: u64, size: u64) ?u64 {
152186 const end = start + padToIdeal(size);
153187 for (self.sections.items) |section| {
......@@ -556,3 +590,13 @@ fn getLinkeditSegmentPtr(self: *DebugSymbols) *macho.segment_command_64 {
556590 const index = self.linkedit_segment_cmd_index.?;
557591 return &self.segments.items[index];
558592}
593
594pub fn getSectionPtr(self: *DebugSymbols, sect: u8) *macho.section_64 {
595 assert(sect < self.sections.items.len);
596 return &self.sections.items[sect];
597}
598
599pub fn getSection(self: DebugSymbols, sect: u8) macho.section_64 {
600 assert(sect < self.sections.items.len);
601 return self.sections.items[sect];
602}