authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 12:38:40+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:06+02:00
logee1c3c70580bc8d0259a495cf67c3090196983b5
tree3810b12e3b66754fee915f0dfd3293e183eede35
parentb0e2c6323bfa8f5991546cb96122a29a599e0365

elf: correctly copy and write out debug sections


2 files changed, 64 insertions(+), 15 deletions(-)

src/link/Dwarf.zig+2-3
......@@ -1851,8 +1851,7 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u
18511851 // not including the initial length itself.
18521852 // We have to come back and write it later after we know the size.
18531853 const after_init_len = di_buf.items.len + init_len_size;
1854 // +1 for the final 0 that ends the compilation unit children.
1855 const dbg_info_end = self.getDebugInfoEnd().? + 1;
1854 const dbg_info_end = self.getDebugInfoEnd().?;
18561855 const init_len = dbg_info_end - after_init_len;
18571856 if (self.bin_file.tag == .macho) {
18581857 mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, @intCast(init_len)));
......@@ -2501,7 +2500,7 @@ fn getDebugInfoOff(self: Dwarf) ?u32 {
25012500fn getDebugInfoEnd(self: Dwarf) ?u32 {
25022501 const last_index = self.di_atom_last_index orelse return null;
25032502 const last = self.getAtom(.di_atom, last_index);
2504 return last.off + last.len;
2503 return last.off + last.len + 1;
25052504}
25062505
25072506fn getDebugLineProgramOff(self: Dwarf) ?u32 {
src/link/Elf.zig+62-12
......@@ -108,12 +108,21 @@ zig_rodata_section_index: ?u16 = null,
108108zig_data_section_index: ?u16 = null,
109109zig_bss_section_index: ?u16 = null,
110110zig_got_section_index: ?u16 = null,
111
111112debug_info_section_index: ?u16 = null,
112113debug_abbrev_section_index: ?u16 = null,
113114debug_str_section_index: ?u16 = null,
114115debug_aranges_section_index: ?u16 = null,
115116debug_line_section_index: ?u16 = null,
116117
118/// Size contribution of Zig's metadata to each debug section.
119/// Used to track start of metadata from input object files.
120debug_info_section_zig_size: u64 = 0,
121debug_abbrev_section_zig_size: u64 = 0,
122debug_str_section_zig_size: u64 = 0,
123debug_aranges_section_zig_size: u64 = 0,
124debug_line_section_zig_size: u64 = 0,
125
117126copy_rel_section_index: ?u16 = null,
118127dynamic_section_index: ?u16 = null,
119128dynstrtab_section_index: ?u16 = null,
......@@ -924,7 +933,7 @@ pub fn growNonAllocSection(
924933 shdr.sh_offset = new_offset;
925934 }
926935
927 shdr.sh_size = needed_size; // anticipating adding the global symbols later
936 shdr.sh_size = needed_size;
928937
929938 self.markDirty(shdr_index);
930939}
......@@ -1522,8 +1531,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
15221531 }
15231532
15241533 if (self.debug_info_header_dirty) {
1525 // Currently only one compilation unit is supported, so the address range is simply
1526 // identical to the main program header virtual address and memory size.
15271534 const text_phdr = &self.phdrs.items[self.phdr_zig_load_re_index.?];
15281535 const low_pc = text_phdr.p_vaddr;
15291536 const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz;
......@@ -1532,8 +1539,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
15321539 }
15331540
15341541 if (self.debug_aranges_section_dirty) {
1535 // Currently only one compilation unit is supported, so the address range is simply
1536 // identical to the main program header virtual address and memory size.
15371542 const text_phdr = &self.phdrs.items[self.phdr_zig_load_re_index.?];
15381543 try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz);
15391544 self.debug_aranges_section_dirty = false;
......@@ -1552,6 +1557,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
15521557 self.debug_strtab_dirty = false;
15531558 }
15541559 }
1560
1561 self.saveDebugSectionsSizes();
15551562 }
15561563
15571564 // Generate and emit non-incremental sections.
......@@ -4345,6 +4352,24 @@ fn sortShdrs(self: *Elf) !void {
43454352 }
43464353}
43474354
4355fn saveDebugSectionsSizes(self: *Elf) void {
4356 if (self.debug_info_section_index) |shndx| {
4357 self.debug_info_section_zig_size = self.shdrs.items[shndx].sh_size;
4358 }
4359 if (self.debug_abbrev_section_index) |shndx| {
4360 self.debug_abbrev_section_zig_size = self.shdrs.items[shndx].sh_size;
4361 }
4362 if (self.debug_str_section_index) |shndx| {
4363 self.debug_str_section_zig_size = self.shdrs.items[shndx].sh_size;
4364 }
4365 if (self.debug_aranges_section_index) |shndx| {
4366 self.debug_aranges_section_zig_size = self.shdrs.items[shndx].sh_size;
4367 }
4368 if (self.debug_line_section_index) |shndx| {
4369 self.debug_line_section_zig_size = self.shdrs.items[shndx].sh_size;
4370 }
4371}
4372
43484373fn updateSectionSizes(self: *Elf) !void {
43494374 for (self.output_sections.keys(), self.output_sections.values()) |shndx, atom_list| {
43504375 if (atom_list.items.len == 0) continue;
......@@ -4675,18 +4700,31 @@ fn allocateNonAllocSections(self: *Elf) !void {
46754700 const new_offset = self.findFreeSpace(needed_size, shdr.sh_addralign);
46764701
46774702 if (self.isDebugSection(@intCast(shndx))) {
4703 log.debug("moving {s} from 0x{x} to 0x{x}", .{
4704 self.shstrtab.getAssumeExists(shdr.sh_name),
4705 shdr.sh_offset,
4706 new_offset,
4707 });
4708 const existing_size = blk: {
4709 if (shndx == self.debug_info_section_index.?) break :blk self.debug_info_section_zig_size;
4710 if (shndx == self.debug_abbrev_section_index.?) break :blk self.debug_abbrev_section_zig_size;
4711 if (shndx == self.debug_str_section_index.?) break :blk self.debug_str_section_zig_size;
4712 if (shndx == self.debug_aranges_section_index.?) break :blk self.debug_aranges_section_zig_size;
4713 if (shndx == self.debug_line_section_index.?) break :blk self.debug_line_section_zig_size;
4714 unreachable;
4715 };
46784716 const amt = try self.base.file.?.copyRangeAll(
46794717 shdr.sh_offset,
46804718 self.base.file.?,
46814719 new_offset,
4682 needed_size, // TODO this will copy too much but ah well
4720 existing_size,
46834721 );
4684 if (amt != needed_size) return error.InputOutput;
4722 if (amt != existing_size) return error.InputOutput;
46854723 }
46864724
46874725 shdr.sh_offset = new_offset;
4726 shdr.sh_size = needed_size;
46884727 }
4689 shdr.sh_size = needed_size;
46904728 }
46914729}
46924730
......@@ -4771,7 +4809,19 @@ fn writeAtoms(self: *Elf) !void {
47714809
47724810 log.debug("writing atoms in '{s}' section", .{self.shstrtab.getAssumeExists(shdr.sh_name)});
47734811
4774 const buffer = try gpa.alloc(u8, shdr.sh_size);
4812 // TODO really, really handle debug section separately
4813 const base_offset = if (self.isDebugSection(@intCast(shndx))) blk: {
4814 if (shndx == self.debug_info_section_index.?) break :blk self.debug_info_section_zig_size;
4815 if (shndx == self.debug_abbrev_section_index.?) break :blk self.debug_abbrev_section_zig_size;
4816 if (shndx == self.debug_str_section_index.?) break :blk self.debug_str_section_zig_size;
4817 if (shndx == self.debug_aranges_section_index.?) break :blk self.debug_aranges_section_zig_size;
4818 if (shndx == self.debug_line_section_index.?) break :blk self.debug_line_section_zig_size;
4819 unreachable;
4820 } else 0;
4821 const sh_offset = shdr.sh_offset + base_offset;
4822 const sh_size = shdr.sh_size - base_offset;
4823
4824 const buffer = try gpa.alloc(u8, sh_size);
47754825 defer gpa.free(buffer);
47764826 const padding_byte: u8 = if (shdr.sh_type == elf.SHT_PROGBITS and
47774827 shdr.sh_flags & elf.SHF_EXECINSTR != 0)
......@@ -4785,9 +4835,9 @@ fn writeAtoms(self: *Elf) !void {
47854835 assert(atom_ptr.flags.alive);
47864836
47874837 const object = atom_ptr.file(self).?.object;
4788 const offset = atom_ptr.value - shdr.sh_addr;
4838 const offset = atom_ptr.value - shdr.sh_addr - base_offset;
47894839
4790 log.debug("writing atom({d}) at 0x{x}", .{ atom_index, shdr.sh_offset + offset });
4840 log.debug("writing atom({d}) at 0x{x}", .{ atom_index, sh_offset + offset });
47914841
47924842 // TODO decompress directly into provided buffer
47934843 const out_code = buffer[offset..][0..atom_ptr.size];
......@@ -4808,7 +4858,7 @@ fn writeAtoms(self: *Elf) !void {
48084858 }
48094859 }
48104860
4811 try self.base.file.?.pwriteAll(buffer, shdr.sh_offset);
4861 try self.base.file.?.pwriteAll(buffer, sh_offset);
48124862 }
48134863
48144864 try self.reportUndefined(&undefs);