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 {...@@ -585,7 +585,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
585 const ptr_size = self.ptrWidthBytes();585 const ptr_size = self.ptrWidthBytes();
586 const target = self.base.comp.root_mod.resolved_target.result;586 const target = self.base.comp.root_mod.resolved_target.result;
587 const ptr_bit_width = target.ptrBitWidth();587 const ptr_bit_width = target.ptrBitWidth();
588 const zig_object = self.zigObjectPtr().?;588 const zo = self.zigObjectPtr().?;
589589
590 const fillSection = struct {590 const fillSection = struct {
591 fn fillSection(elf_file: *Elf, shdr: *elf.Elf64_Shdr, size: u64, phndx: ?u16) !void {591 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 {...@@ -766,7 +766,27 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
766 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_bss_section_index.?, .{});766 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_bss_section_index.?, .{});
767 }767 }
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
770 if (self.debug_str_section_index == null) {790 if (self.debug_str_section_index == null) {
771 self.debug_str_section_index = try self.addSection(.{791 self.debug_str_section_index = try self.addSection(.{
772 .name = try self.insertShString(".debug_str"),792 .name = try self.insertShString(".debug_str"),
...@@ -775,7 +795,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -775,7 +795,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
775 .type = elf.SHT_PROGBITS,795 .type = elf.SHT_PROGBITS,
776 .addralign = 1,796 .addralign = 1,
777 });797 });
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.?);
779 try self.output_sections.putNoClobber(gpa, self.debug_str_section_index.?, .{});800 try self.output_sections.putNoClobber(gpa, self.debug_str_section_index.?, .{});
780 }801 }
781802
...@@ -785,7 +806,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -785,7 +806,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
785 .type = elf.SHT_PROGBITS,806 .type = elf.SHT_PROGBITS,
786 .addralign = 1,807 .addralign = 1,
787 });808 });
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.?);
789 try self.output_sections.putNoClobber(gpa, self.debug_info_section_index.?, .{});811 try self.output_sections.putNoClobber(gpa, self.debug_info_section_index.?, .{});
790 }812 }
791813
...@@ -795,7 +817,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -795,7 +817,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
795 .type = elf.SHT_PROGBITS,817 .type = elf.SHT_PROGBITS,
796 .addralign = 1,818 .addralign = 1,
797 });819 });
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.?);
799 try self.output_sections.putNoClobber(gpa, self.debug_abbrev_section_index.?, .{});822 try self.output_sections.putNoClobber(gpa, self.debug_abbrev_section_index.?, .{});
800 }823 }
801824
...@@ -805,7 +828,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -805,7 +828,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
805 .type = elf.SHT_PROGBITS,828 .type = elf.SHT_PROGBITS,
806 .addralign = 16,829 .addralign = 16,
807 });830 });
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.?);
809 try self.output_sections.putNoClobber(gpa, self.debug_aranges_section_index.?, .{});833 try self.output_sections.putNoClobber(gpa, self.debug_aranges_section_index.?, .{});
810 }834 }
811835
...@@ -815,7 +839,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -815,7 +839,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
815 .type = elf.SHT_PROGBITS,839 .type = elf.SHT_PROGBITS,
816 .addralign = 1,840 .addralign = 1,
817 });841 });
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.?);
819 try self.output_sections.putNoClobber(gpa, self.debug_line_section_index.?, .{});844 try self.output_sections.putNoClobber(gpa, self.debug_line_section_index.?, .{});
820 }845 }
821846
...@@ -827,7 +852,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -827,7 +852,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
827 .type = elf.SHT_PROGBITS,852 .type = elf.SHT_PROGBITS,
828 .addralign = 1,853 .addralign = 1,
829 });854 });
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.?);
831 try self.output_sections.putNoClobber(gpa, self.debug_line_str_section_index.?, .{});857 try self.output_sections.putNoClobber(gpa, self.debug_line_str_section_index.?, .{});
832 }858 }
833859
...@@ -837,7 +863,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -837,7 +863,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
837 .type = elf.SHT_PROGBITS,863 .type = elf.SHT_PROGBITS,
838 .addralign = 1,864 .addralign = 1,
839 });865 });
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.?);
841 try self.output_sections.putNoClobber(gpa, self.debug_loclists_section_index.?, .{});868 try self.output_sections.putNoClobber(gpa, self.debug_loclists_section_index.?, .{});
842 }869 }
843870
...@@ -847,7 +874,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -847,7 +874,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
847 .type = elf.SHT_PROGBITS,874 .type = elf.SHT_PROGBITS,
848 .addralign = 1,875 .addralign = 1,
849 });876 });
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.?);
851 try self.output_sections.putNoClobber(gpa, self.debug_rnglists_section_index.?, .{});879 try self.output_sections.putNoClobber(gpa, self.debug_rnglists_section_index.?, .{});
852 }880 }
853881
src/link/Elf/ZigObject.zig+11-2
...@@ -50,6 +50,15 @@ debug_line_str_section_dirty: bool = false,...@@ -50,6 +50,15 @@ debug_line_str_section_dirty: bool = false,
50debug_loclists_section_dirty: bool = false,50debug_loclists_section_dirty: bool = false,
51debug_rnglists_section_dirty: bool = false,51debug_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
53/// Size contribution of Zig's metadata to each debug section.62/// Size contribution of Zig's metadata to each debug section.
54/// Used to track start of metadata from input object files.63/// Used to track start of metadata from input object files.
55debug_info_section_zig_size: u64 = 0,64debug_info_section_zig_size: u64 = 0,
...@@ -278,7 +287,7 @@ fn newAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Atom.Index {...@@ -278,7 +287,7 @@ fn newAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Atom.Index {
278 return index;287 return index;
279}288}
280289
281fn newSymbolWithAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Symbol.Index {290pub fn newSymbolWithAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Symbol.Index {
282 const atom_index = try self.newAtom(allocator, name_off);291 const atom_index = try self.newAtom(allocator, name_off);
283 const sym_index = try self.newLocalSymbol(allocator, name_off);292 const sym_index = try self.newLocalSymbol(allocator, name_off);
284 const sym = self.symbol(sym_index);293 const sym = self.symbol(sym_index);
...@@ -1529,7 +1538,7 @@ pub fn asFile(self: *ZigObject) File {...@@ -1529,7 +1538,7 @@ pub fn asFile(self: *ZigObject) File {
1529 return .{ .zig_object = self };1538 return .{ .zig_object = self };
1530}1539}
15311540
1532fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !u32 {1541pub fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !u32 {
1533 return self.strtab.insert(allocator, string);1542 return self.strtab.insert(allocator, string);
1534}1543}
15351544