Commit 41f2302865

Jakub Konka <kubkon@jakubkonka.com>
2024-08-18 09:21:11
elf: create section symbols and atoms per each ZigObject debug section
1 parent 84105be
Changed files (2)
src/link/Elf/ZigObject.zig
@@ -50,6 +50,15 @@ debug_line_str_section_dirty: bool = false,
 debug_loclists_section_dirty: bool = false,
 debug_rnglists_section_dirty: bool = false,
 
+debug_info_index: ?Symbol.Index = null,
+debug_abbrev_index: ?Symbol.Index = null,
+debug_aranges_index: ?Symbol.Index = null,
+debug_str_index: ?Symbol.Index = null,
+debug_line_index: ?Symbol.Index = null,
+debug_line_str_index: ?Symbol.Index = null,
+debug_loclists_index: ?Symbol.Index = null,
+debug_rnglists_index: ?Symbol.Index = null,
+
 /// Size contribution of Zig's metadata to each debug section.
 /// Used to track start of metadata from input object files.
 debug_info_section_zig_size: u64 = 0,
@@ -278,7 +287,7 @@ fn newAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Atom.Index {
     return index;
 }
 
-fn newSymbolWithAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Symbol.Index {
+pub fn newSymbolWithAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Symbol.Index {
     const atom_index = try self.newAtom(allocator, name_off);
     const sym_index = try self.newLocalSymbol(allocator, name_off);
     const sym = self.symbol(sym_index);
@@ -1529,7 +1538,7 @@ pub fn asFile(self: *ZigObject) File {
     return .{ .zig_object = self };
 }
 
-fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !u32 {
+pub fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !u32 {
     return self.strtab.insert(allocator, string);
 }
 
src/link/Elf.zig
@@ -585,7 +585,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
     const ptr_size = self.ptrWidthBytes();
     const target = self.base.comp.root_mod.resolved_target.result;
     const ptr_bit_width = target.ptrBitWidth();
-    const zig_object = self.zigObjectPtr().?;
+    const zo = self.zigObjectPtr().?;
 
     const fillSection = struct {
         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 {
         try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_bss_section_index.?, .{});
     }
 
-    if (zig_object.dwarf) |*dwarf| {
+    if (zo.dwarf) |*dwarf| {
+        const addSectionSymbol = struct {
+            fn addSectionSymbol(
+                zig_object: *ZigObject,
+                alloc: Allocator,
+                name: [:0]const u8,
+                alignment: Atom.Alignment,
+                shndx: u32,
+            ) !Symbol.Index {
+                const name_off = try zig_object.addString(alloc, name);
+                const index = try zig_object.newSymbolWithAtom(alloc, name_off);
+                const sym = zig_object.symbol(index);
+                const esym = &zig_object.symtab.items(.elf_sym)[sym.esym_index];
+                esym.st_info |= elf.STT_SECTION;
+                const atom_ptr = zig_object.atom(sym.ref.index).?;
+                atom_ptr.alignment = alignment;
+                atom_ptr.output_section_index = shndx;
+                return index;
+            }
+        }.addSectionSymbol;
+
         if (self.debug_str_section_index == null) {
             self.debug_str_section_index = try self.addSection(.{
                 .name = try self.insertShString(".debug_str"),
@@ -775,7 +795,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
                 .type = elf.SHT_PROGBITS,
                 .addralign = 1,
             });
-            zig_object.debug_str_section_dirty = true;
+            zo.debug_str_section_dirty = true;
+            zo.debug_str_index = try addSectionSymbol(zo, gpa, ".debug_str", .@"1", self.debug_str_section_index.?);
             try self.output_sections.putNoClobber(gpa, self.debug_str_section_index.?, .{});
         }
 
@@ -785,7 +806,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
                 .type = elf.SHT_PROGBITS,
                 .addralign = 1,
             });
-            zig_object.debug_info_section_dirty = true;
+            zo.debug_info_section_dirty = true;
+            zo.debug_info_index = try addSectionSymbol(zo, gpa, ".debug_info", .@"1", self.debug_info_section_index.?);
             try self.output_sections.putNoClobber(gpa, self.debug_info_section_index.?, .{});
         }
 
@@ -795,7 +817,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
                 .type = elf.SHT_PROGBITS,
                 .addralign = 1,
             });
-            zig_object.debug_abbrev_section_dirty = true;
+            zo.debug_abbrev_section_dirty = true;
+            zo.debug_abbrev_index = try addSectionSymbol(zo, gpa, ".debug_abbrev", .@"1", self.debug_abbrev_section_index.?);
             try self.output_sections.putNoClobber(gpa, self.debug_abbrev_section_index.?, .{});
         }
 
@@ -805,7 +828,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
                 .type = elf.SHT_PROGBITS,
                 .addralign = 16,
             });
-            zig_object.debug_aranges_section_dirty = true;
+            zo.debug_aranges_section_dirty = true;
+            zo.debug_aranges_index = try addSectionSymbol(zo, gpa, ".debug_aranges", .@"16", self.debug_aranges_section_index.?);
             try self.output_sections.putNoClobber(gpa, self.debug_aranges_section_index.?, .{});
         }
 
@@ -815,7 +839,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
                 .type = elf.SHT_PROGBITS,
                 .addralign = 1,
             });
-            zig_object.debug_line_section_dirty = true;
+            zo.debug_line_section_dirty = true;
+            zo.debug_line_index = try addSectionSymbol(zo, gpa, ".debug_line", .@"1", self.debug_line_section_index.?);
             try self.output_sections.putNoClobber(gpa, self.debug_line_section_index.?, .{});
         }
 
@@ -827,7 +852,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
                 .type = elf.SHT_PROGBITS,
                 .addralign = 1,
             });
-            zig_object.debug_line_str_section_dirty = true;
+            zo.debug_line_str_section_dirty = true;
+            zo.debug_line_str_index = try addSectionSymbol(zo, gpa, ".debug_line_str", .@"1", self.debug_line_str_section_index.?);
             try self.output_sections.putNoClobber(gpa, self.debug_line_str_section_index.?, .{});
         }
 
@@ -837,7 +863,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
                 .type = elf.SHT_PROGBITS,
                 .addralign = 1,
             });
-            zig_object.debug_loclists_section_dirty = true;
+            zo.debug_loclists_section_dirty = true;
+            zo.debug_loclists_index = try addSectionSymbol(zo, gpa, ".debug_loclists", .@"1", self.debug_loclists_section_index.?);
             try self.output_sections.putNoClobber(gpa, self.debug_loclists_section_index.?, .{});
         }
 
@@ -847,7 +874,8 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
                 .type = elf.SHT_PROGBITS,
                 .addralign = 1,
             });
-            zig_object.debug_rnglists_section_dirty = true;
+            zo.debug_rnglists_section_dirty = true;
+            zo.debug_rnglists_index = try addSectionSymbol(zo, gpa, ".debug_rnglists", .@"1", self.debug_rnglists_section_index.?);
             try self.output_sections.putNoClobber(gpa, self.debug_rnglists_section_index.?, .{});
         }