diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2024-08-21 10:28:31 +0200 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2024-08-21 10:28:31 +0200 |
| commit | e79ac14ef34d8ceea945de972b7f395b8f53621f (patch) | |
| tree | 1dcda4337dc446f1d1c5e3bb478ea4b9678c383c /src/link | |
| parent | db48a789632ed901cedfa9af480bb987c23fcdbf (diff) | |
| download | zig-e79ac14ef34d8ceea945de972b7f395b8f53621f.tar.gz zig-e79ac14ef34d8ceea945de972b7f395b8f53621f.zip | |
elf: refactor tracking debug section sizes
Diffstat (limited to 'src/link')
| -rw-r--r-- | src/link/Elf.zig | 68 | ||||
| -rw-r--r-- | src/link/Elf/ZigObject.zig | 40 | ||||
| -rw-r--r-- | src/link/Elf/relocatable.zig | 34 |
3 files changed, 46 insertions, 96 deletions
diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 5c8f0583ac..333e490d17 100644 --- a/src/link/Elf.zig +++ b/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; diff --git a/src/link/Elf/ZigObject.zig b/src/link/Elf/ZigObject.zig index a61ae15573..3c51d5d11b 100644 --- a/src/link/Elf/ZigObject.zig +++ b/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); diff --git a/src/link/Elf/relocatable.zig b/src/link/Elf/relocatable.zig index 710c3f85fa..50b9b562d1 100644 --- a/src/link/Elf/relocatable.zig +++ b/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"); |
