Commit e79ac14ef3

Jakub Konka <kubkon@jakubkonka.com>
2024-08-21 10:28:31
elf: refactor tracking debug section sizes
1 parent db48a78
Changed files (3)
src/link/Elf/relocatable.zig
@@ -431,24 +431,21 @@ fn writeAtoms(elf_file: *Elf) !void {
 
         // TODO really, really handle debug section separately
         const base_offset = if (elf_file.isDebugSection(@intCast(shndx))) blk: {
-            const zig_object = elf_file.zigObjectPtr().?;
-            if (shndx == elf_file.debug_info_section_index.?)
-                break :blk zig_object.debug_info_section_zig_size;
-            if (shndx == elf_file.debug_abbrev_section_index.?)
-                break :blk zig_object.debug_abbrev_section_zig_size;
-            if (shndx == elf_file.debug_str_section_index.?)
-                break :blk zig_object.debug_str_section_zig_size;
-            if (shndx == elf_file.debug_aranges_section_index.?)
-                break :blk zig_object.debug_aranges_section_zig_size;
-            if (shndx == elf_file.debug_line_section_index.?)
-                break :blk zig_object.debug_line_section_zig_size;
-            if (shndx == elf_file.debug_line_str_section_index.?)
-                break :blk zig_object.debug_line_str_section_zig_size;
-            if (shndx == elf_file.debug_loclists_section_index.?)
-                break :blk zig_object.debug_loclists_section_zig_size;
-            if (shndx == elf_file.debug_rnglists_section_index.?)
-                break :blk zig_object.debug_rnglists_section_zig_size;
-            unreachable;
+            const zo = elf_file.zigObjectPtr().?;
+            break :blk for ([_]Symbol.Index{
+                zo.debug_info_index.?,
+                zo.debug_abbrev_index.?,
+                zo.debug_aranges_index.?,
+                zo.debug_str_index.?,
+                zo.debug_line_index.?,
+                zo.debug_line_str_index.?,
+                zo.debug_loclists_index.?,
+                zo.debug_rnglists_index.?,
+            }) |sym_index| {
+                const sym = zo.symbol(sym_index);
+                const atom_ptr = sym.atom(elf_file).?;
+                if (atom_ptr.output_section_index == shndx) break atom_ptr.size;
+            } else 0;
         } else 0;
         const sh_offset = shdr.sh_offset + base_offset;
         const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;
@@ -594,3 +591,4 @@ const Compilation = @import("../../Compilation.zig");
 const Elf = @import("../Elf.zig");
 const File = @import("file.zig").File;
 const Object = @import("Object.zig");
+const Symbol = @import("Symbol.zig");
src/link/Elf/ZigObject.zig
@@ -59,17 +59,6 @@ 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,
-debug_abbrev_section_zig_size: u64 = 0,
-debug_str_section_zig_size: u64 = 0,
-debug_aranges_section_zig_size: u64 = 0,
-debug_line_section_zig_size: u64 = 0,
-debug_line_str_section_zig_size: u64 = 0,
-debug_loclists_section_zig_size: u64 = 0,
-debug_rnglists_section_zig_size: u64 = 0,
-
 pub const global_symbol_bit: u32 = 0x80000000;
 pub const symbol_mask: u32 = 0x7fffffff;
 pub const SHN_ATOM: u16 = 0x100;
@@ -328,8 +317,6 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi
         self.debug_aranges_section_dirty = false;
         self.debug_rnglists_section_dirty = false;
         self.debug_str_section_dirty = false;
-
-        self.saveDebugSectionsSizes(elf_file);
     }
 
     // The point of flushModule() is to commit changes, so in theory, nothing should
@@ -342,33 +329,6 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi
     assert(!self.debug_str_section_dirty);
 }
 
-fn saveDebugSectionsSizes(self: *ZigObject, elf_file: *Elf) void {
-    if (elf_file.debug_info_section_index) |shndx| {
-        self.debug_info_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
-    }
-    if (elf_file.debug_abbrev_section_index) |shndx| {
-        self.debug_abbrev_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
-    }
-    if (elf_file.debug_str_section_index) |shndx| {
-        self.debug_str_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
-    }
-    if (elf_file.debug_aranges_section_index) |shndx| {
-        self.debug_aranges_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
-    }
-    if (elf_file.debug_line_section_index) |shndx| {
-        self.debug_line_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
-    }
-    if (elf_file.debug_line_str_section_index) |shndx| {
-        self.debug_line_str_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
-    }
-    if (elf_file.debug_loclists_section_index) |shndx| {
-        self.debug_loclists_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
-    }
-    if (elf_file.debug_rnglists_section_index) |shndx| {
-        self.debug_rnglists_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
-    }
-}
-
 fn newSymbol(self: *ZigObject, allocator: Allocator, name_off: u32, st_bind: u4) !Symbol.Index {
     try self.symtab.ensureUnusedCapacity(allocator, 1);
     try self.symbols.ensureUnusedCapacity(allocator, 1);
src/link/Elf.zig
@@ -4184,26 +4184,21 @@ pub fn allocateNonAllocSections(self: *Elf) !void {
                     shdr.sh_offset,
                     new_offset,
                 });
-                const zig_object = self.zigObjectPtr().?;
-                const existing_size = blk: {
-                    if (shndx == self.debug_info_section_index.?)
-                        break :blk zig_object.debug_info_section_zig_size;
-                    if (shndx == self.debug_abbrev_section_index.?)
-                        break :blk zig_object.debug_abbrev_section_zig_size;
-                    if (shndx == self.debug_str_section_index.?)
-                        break :blk zig_object.debug_str_section_zig_size;
-                    if (shndx == self.debug_aranges_section_index.?)
-                        break :blk zig_object.debug_aranges_section_zig_size;
-                    if (shndx == self.debug_line_section_index.?)
-                        break :blk zig_object.debug_line_section_zig_size;
-                    if (shndx == self.debug_line_str_section_index.?)
-                        break :blk zig_object.debug_line_str_section_zig_size;
-                    if (shndx == self.debug_loclists_section_index.?)
-                        break :blk zig_object.debug_loclists_section_zig_size;
-                    if (shndx == self.debug_rnglists_section_index.?)
-                        break :blk zig_object.debug_rnglists_section_zig_size;
-                    unreachable;
-                };
+                const zo = self.zigObjectPtr().?;
+                const existing_size = for ([_]Symbol.Index{
+                    zo.debug_info_index.?,
+                    zo.debug_abbrev_index.?,
+                    zo.debug_aranges_index.?,
+                    zo.debug_str_index.?,
+                    zo.debug_line_index.?,
+                    zo.debug_line_str_index.?,
+                    zo.debug_loclists_index.?,
+                    zo.debug_rnglists_index.?,
+                }) |sym_index| {
+                    const sym = zo.symbol(sym_index);
+                    const atom_ptr = sym.atom(self).?;
+                    if (atom_ptr.output_section_index == shndx) break atom_ptr.size;
+                } else 0;
                 const amt = try self.base.file.?.copyRangeAll(
                     shdr.sh_offset,
                     self.base.file.?,
@@ -4299,24 +4294,21 @@ fn writeAtoms(self: *Elf) !void {
 
         // TODO really, really handle debug section separately
         const base_offset = if (self.isDebugSection(@intCast(shndx))) blk: {
-            const zig_object = self.zigObjectPtr().?;
-            if (shndx == self.debug_info_section_index.?)
-                break :blk zig_object.debug_info_section_zig_size;
-            if (shndx == self.debug_abbrev_section_index.?)
-                break :blk zig_object.debug_abbrev_section_zig_size;
-            if (shndx == self.debug_str_section_index.?)
-                break :blk zig_object.debug_str_section_zig_size;
-            if (shndx == self.debug_aranges_section_index.?)
-                break :blk zig_object.debug_aranges_section_zig_size;
-            if (shndx == self.debug_line_section_index.?)
-                break :blk zig_object.debug_line_section_zig_size;
-            if (shndx == self.debug_line_str_section_index.?)
-                break :blk zig_object.debug_line_str_section_zig_size;
-            if (shndx == self.debug_loclists_section_index.?)
-                break :blk zig_object.debug_loclists_section_zig_size;
-            if (shndx == self.debug_rnglists_section_index.?)
-                break :blk zig_object.debug_rnglists_section_zig_size;
-            unreachable;
+            const zo = self.zigObjectPtr().?;
+            break :blk for ([_]Symbol.Index{
+                zo.debug_info_index.?,
+                zo.debug_abbrev_index.?,
+                zo.debug_aranges_index.?,
+                zo.debug_str_index.?,
+                zo.debug_line_index.?,
+                zo.debug_line_str_index.?,
+                zo.debug_loclists_index.?,
+                zo.debug_rnglists_index.?,
+            }) |sym_index| {
+                const sym = zo.symbol(sym_index);
+                const atom_ptr = sym.atom(self).?;
+                if (atom_ptr.output_section_index == shndx) break atom_ptr.size;
+            } else 0;
         } else 0;
         const sh_offset = shdr.sh_offset + base_offset;
         const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;