authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-18 09:21:11+02:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-08-21 01:43:21-04:00
log41f2302865b92db81d839788393b126a34a56e62
tree3484488236d741a7f92fc2ae33ce6739f103b8bc
parent84105be4df410e8e62a1d589e75fc6875b9f6b80

elf: create section symbols and atoms per each ZigObject debug section


2 files changed, 49 insertions(+), 12 deletions(-)

src/link/Elf.zig+38-10
......@@ -585,7 +585,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
585585 const ptr_size = self.ptrWidthBytes();
586586 const target = self.base.comp.root_mod.resolved_target.result;
587587 const ptr_bit_width = target.ptrBitWidth();
588 const zig_object = self.zigObjectPtr().?;
588 const zo = self.zigObjectPtr().?;
589589
590590 const fillSection = struct {
591591 fn fillSection(elf_file: *Elf, shdr: *elf.Elf64_Shdr, size: u64, phndx: ?u16) !void {
......@@ -766,7 +766,27 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
766766 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_bss_section_index.?, .{});
767767 }
768768
769 if (zig_object.dwarf) |*dwarf| {
769 if (zo.dwarf) |*dwarf| {
770 const addSectionSymbol = struct {
771 fn addSectionSymbol(
772 zig_object: *ZigObject,
773 alloc: Allocator,
774 name: [:0]const u8,
775 alignment: Atom.Alignment,
776 shndx: u32,
777 ) !Symbol.Index {
778 const name_off = try zig_object.addString(alloc, name);
779 const index = try zig_object.newSymbolWithAtom(alloc, name_off);
780 const sym = zig_object.symbol(index);
781 const esym = &zig_object.symtab.items(.elf_sym)[sym.esym_index];
782 esym.st_info |= elf.STT_SECTION;
783 const atom_ptr = zig_object.atom(sym.ref.index).?;
784 atom_ptr.alignment = alignment;
785 atom_ptr.output_section_index = shndx;
786 return index;
787 }
788 }.addSectionSymbol;
789
770790 if (self.debug_str_section_index == null) {
771791 self.debug_str_section_index = try self.addSection(.{
772792 .name = try self.insertShString(".debug_str"),
......@@ -775,7 +795,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
775795 .type = elf.SHT_PROGBITS,
776796 .addralign = 1,
777797 });
778 zig_object.debug_str_section_dirty = true;
798 zo.debug_str_section_dirty = true;
799 zo.debug_str_index = try addSectionSymbol(zo, gpa, ".debug_str", .@"1", self.debug_str_section_index.?);
779800 try self.output_sections.putNoClobber(gpa, self.debug_str_section_index.?, .{});
780801 }
781802
......@@ -785,7 +806,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
785806 .type = elf.SHT_PROGBITS,
786807 .addralign = 1,
787808 });
788 zig_object.debug_info_section_dirty = true;
809 zo.debug_info_section_dirty = true;
810 zo.debug_info_index = try addSectionSymbol(zo, gpa, ".debug_info", .@"1", self.debug_info_section_index.?);
789811 try self.output_sections.putNoClobber(gpa, self.debug_info_section_index.?, .{});
790812 }
791813
......@@ -795,7 +817,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
795817 .type = elf.SHT_PROGBITS,
796818 .addralign = 1,
797819 });
798 zig_object.debug_abbrev_section_dirty = true;
820 zo.debug_abbrev_section_dirty = true;
821 zo.debug_abbrev_index = try addSectionSymbol(zo, gpa, ".debug_abbrev", .@"1", self.debug_abbrev_section_index.?);
799822 try self.output_sections.putNoClobber(gpa, self.debug_abbrev_section_index.?, .{});
800823 }
801824
......@@ -805,7 +828,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
805828 .type = elf.SHT_PROGBITS,
806829 .addralign = 16,
807830 });
808 zig_object.debug_aranges_section_dirty = true;
831 zo.debug_aranges_section_dirty = true;
832 zo.debug_aranges_index = try addSectionSymbol(zo, gpa, ".debug_aranges", .@"16", self.debug_aranges_section_index.?);
809833 try self.output_sections.putNoClobber(gpa, self.debug_aranges_section_index.?, .{});
810834 }
811835
......@@ -815,7 +839,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
815839 .type = elf.SHT_PROGBITS,
816840 .addralign = 1,
817841 });
818 zig_object.debug_line_section_dirty = true;
842 zo.debug_line_section_dirty = true;
843 zo.debug_line_index = try addSectionSymbol(zo, gpa, ".debug_line", .@"1", self.debug_line_section_index.?);
819844 try self.output_sections.putNoClobber(gpa, self.debug_line_section_index.?, .{});
820845 }
821846
......@@ -827,7 +852,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
827852 .type = elf.SHT_PROGBITS,
828853 .addralign = 1,
829854 });
830 zig_object.debug_line_str_section_dirty = true;
855 zo.debug_line_str_section_dirty = true;
856 zo.debug_line_str_index = try addSectionSymbol(zo, gpa, ".debug_line_str", .@"1", self.debug_line_str_section_index.?);
831857 try self.output_sections.putNoClobber(gpa, self.debug_line_str_section_index.?, .{});
832858 }
833859
......@@ -837,7 +863,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
837863 .type = elf.SHT_PROGBITS,
838864 .addralign = 1,
839865 });
840 zig_object.debug_loclists_section_dirty = true;
866 zo.debug_loclists_section_dirty = true;
867 zo.debug_loclists_index = try addSectionSymbol(zo, gpa, ".debug_loclists", .@"1", self.debug_loclists_section_index.?);
841868 try self.output_sections.putNoClobber(gpa, self.debug_loclists_section_index.?, .{});
842869 }
843870
......@@ -847,7 +874,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
847874 .type = elf.SHT_PROGBITS,
848875 .addralign = 1,
849876 });
850 zig_object.debug_rnglists_section_dirty = true;
877 zo.debug_rnglists_section_dirty = true;
878 zo.debug_rnglists_index = try addSectionSymbol(zo, gpa, ".debug_rnglists", .@"1", self.debug_rnglists_section_index.?);
851879 try self.output_sections.putNoClobber(gpa, self.debug_rnglists_section_index.?, .{});
852880 }
853881
src/link/Elf/ZigObject.zig+11-2
......@@ -50,6 +50,15 @@ debug_line_str_section_dirty: bool = false,
5050debug_loclists_section_dirty: bool = false,
5151debug_rnglists_section_dirty: bool = false,
5252
53debug_info_index: ?Symbol.Index = null,
54debug_abbrev_index: ?Symbol.Index = null,
55debug_aranges_index: ?Symbol.Index = null,
56debug_str_index: ?Symbol.Index = null,
57debug_line_index: ?Symbol.Index = null,
58debug_line_str_index: ?Symbol.Index = null,
59debug_loclists_index: ?Symbol.Index = null,
60debug_rnglists_index: ?Symbol.Index = null,
61
5362/// Size contribution of Zig's metadata to each debug section.
5463/// Used to track start of metadata from input object files.
5564debug_info_section_zig_size: u64 = 0,
......@@ -278,7 +287,7 @@ fn newAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Atom.Index {
278287 return index;
279288}
280289
281fn newSymbolWithAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Symbol.Index {
290pub fn newSymbolWithAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Symbol.Index {
282291 const atom_index = try self.newAtom(allocator, name_off);
283292 const sym_index = try self.newLocalSymbol(allocator, name_off);
284293 const sym = self.symbol(sym_index);
......@@ -1529,7 +1538,7 @@ pub fn asFile(self: *ZigObject) File {
15291538 return .{ .zig_object = self };
15301539}
15311540
1532fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !u32 {
1541pub fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !u32 {
15331542 return self.strtab.insert(allocator, string);
15341543}
15351544