authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-21 10:28:31+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-21 10:28:31+02:00
loge79ac14ef34d8ceea945de972b7f395b8f53621f
tree1dcda4337dc446f1d1c5e3bb478ea4b9678c383c
parentdb48a789632ed901cedfa9af480bb987c23fcdbf

elf: refactor tracking debug section sizes


3 files changed, 46 insertions(+), 96 deletions(-)

src/link/Elf.zig+30-38
...@@ -4184,26 +4184,21 @@ pub fn allocateNonAllocSections(self: *Elf) !void {...@@ -4184,26 +4184,21 @@ pub fn allocateNonAllocSections(self: *Elf) !void {
4184 shdr.sh_offset,4184 shdr.sh_offset,
4185 new_offset,4185 new_offset,
4186 });4186 });
4187 const zig_object = self.zigObjectPtr().?;4187 const zo = self.zigObjectPtr().?;
4188 const existing_size = blk: {4188 const existing_size = for ([_]Symbol.Index{
4189 if (shndx == self.debug_info_section_index.?)4189 zo.debug_info_index.?,
4190 break :blk zig_object.debug_info_section_zig_size;4190 zo.debug_abbrev_index.?,
4191 if (shndx == self.debug_abbrev_section_index.?)4191 zo.debug_aranges_index.?,
4192 break :blk zig_object.debug_abbrev_section_zig_size;4192 zo.debug_str_index.?,
4193 if (shndx == self.debug_str_section_index.?)4193 zo.debug_line_index.?,
4194 break :blk zig_object.debug_str_section_zig_size;4194 zo.debug_line_str_index.?,
4195 if (shndx == self.debug_aranges_section_index.?)4195 zo.debug_loclists_index.?,
4196 break :blk zig_object.debug_aranges_section_zig_size;4196 zo.debug_rnglists_index.?,
4197 if (shndx == self.debug_line_section_index.?)4197 }) |sym_index| {
4198 break :blk zig_object.debug_line_section_zig_size;4198 const sym = zo.symbol(sym_index);
4199 if (shndx == self.debug_line_str_section_index.?)4199 const atom_ptr = sym.atom(self).?;
4200 break :blk zig_object.debug_line_str_section_zig_size;4200 if (atom_ptr.output_section_index == shndx) break atom_ptr.size;
4201 if (shndx == self.debug_loclists_section_index.?)4201 } else 0;
4202 break :blk zig_object.debug_loclists_section_zig_size;
4203 if (shndx == self.debug_rnglists_section_index.?)
4204 break :blk zig_object.debug_rnglists_section_zig_size;
4205 unreachable;
4206 };
4207 const amt = try self.base.file.?.copyRangeAll(4202 const amt = try self.base.file.?.copyRangeAll(
4208 shdr.sh_offset,4203 shdr.sh_offset,
4209 self.base.file.?,4204 self.base.file.?,
...@@ -4299,24 +4294,21 @@ fn writeAtoms(self: *Elf) !void {...@@ -4299,24 +4294,21 @@ fn writeAtoms(self: *Elf) !void {
42994294
4300 // TODO really, really handle debug section separately4295 // TODO really, really handle debug section separately
4301 const base_offset = if (self.isDebugSection(@intCast(shndx))) blk: {4296 const base_offset = if (self.isDebugSection(@intCast(shndx))) blk: {
4302 const zig_object = self.zigObjectPtr().?;4297 const zo = self.zigObjectPtr().?;
4303 if (shndx == self.debug_info_section_index.?)4298 break :blk for ([_]Symbol.Index{
4304 break :blk zig_object.debug_info_section_zig_size;4299 zo.debug_info_index.?,
4305 if (shndx == self.debug_abbrev_section_index.?)4300 zo.debug_abbrev_index.?,
4306 break :blk zig_object.debug_abbrev_section_zig_size;4301 zo.debug_aranges_index.?,
4307 if (shndx == self.debug_str_section_index.?)4302 zo.debug_str_index.?,
4308 break :blk zig_object.debug_str_section_zig_size;4303 zo.debug_line_index.?,
4309 if (shndx == self.debug_aranges_section_index.?)4304 zo.debug_line_str_index.?,
4310 break :blk zig_object.debug_aranges_section_zig_size;4305 zo.debug_loclists_index.?,
4311 if (shndx == self.debug_line_section_index.?)4306 zo.debug_rnglists_index.?,
4312 break :blk zig_object.debug_line_section_zig_size;4307 }) |sym_index| {
4313 if (shndx == self.debug_line_str_section_index.?)4308 const sym = zo.symbol(sym_index);
4314 break :blk zig_object.debug_line_str_section_zig_size;4309 const atom_ptr = sym.atom(self).?;
4315 if (shndx == self.debug_loclists_section_index.?)4310 if (atom_ptr.output_section_index == shndx) break atom_ptr.size;
4316 break :blk zig_object.debug_loclists_section_zig_size;4311 } else 0;
4317 if (shndx == self.debug_rnglists_section_index.?)
4318 break :blk zig_object.debug_rnglists_section_zig_size;
4319 unreachable;
4320 } else 0;4312 } else 0;
4321 const sh_offset = shdr.sh_offset + base_offset;4313 const sh_offset = shdr.sh_offset + base_offset;
4322 const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;4314 const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;
src/link/Elf/ZigObject.zig-40
...@@ -59,17 +59,6 @@ debug_line_str_index: ?Symbol.Index = null,...@@ -59,17 +59,6 @@ debug_line_str_index: ?Symbol.Index = null,
59debug_loclists_index: ?Symbol.Index = null,59debug_loclists_index: ?Symbol.Index = null,
60debug_rnglists_index: ?Symbol.Index = null,60debug_rnglists_index: ?Symbol.Index = null,
6161
62/// Size contribution of Zig's metadata to each debug section.
63/// Used to track start of metadata from input object files.
64debug_info_section_zig_size: u64 = 0,
65debug_abbrev_section_zig_size: u64 = 0,
66debug_str_section_zig_size: u64 = 0,
67debug_aranges_section_zig_size: u64 = 0,
68debug_line_section_zig_size: u64 = 0,
69debug_line_str_section_zig_size: u64 = 0,
70debug_loclists_section_zig_size: u64 = 0,
71debug_rnglists_section_zig_size: u64 = 0,
72
73pub const global_symbol_bit: u32 = 0x80000000;62pub const global_symbol_bit: u32 = 0x80000000;
74pub const symbol_mask: u32 = 0x7fffffff;63pub const symbol_mask: u32 = 0x7fffffff;
75pub const SHN_ATOM: u16 = 0x100;64pub const SHN_ATOM: u16 = 0x100;
...@@ -328,8 +317,6 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi...@@ -328,8 +317,6 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi
328 self.debug_aranges_section_dirty = false;317 self.debug_aranges_section_dirty = false;
329 self.debug_rnglists_section_dirty = false;318 self.debug_rnglists_section_dirty = false;
330 self.debug_str_section_dirty = false;319 self.debug_str_section_dirty = false;
331
332 self.saveDebugSectionsSizes(elf_file);
333 }320 }
334321
335 // The point of flushModule() is to commit changes, so in theory, nothing should322 // 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...@@ -342,33 +329,6 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi
342 assert(!self.debug_str_section_dirty);329 assert(!self.debug_str_section_dirty);
343}330}
344331
345fn saveDebugSectionsSizes(self: *ZigObject, elf_file: *Elf) void {
346 if (elf_file.debug_info_section_index) |shndx| {
347 self.debug_info_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
348 }
349 if (elf_file.debug_abbrev_section_index) |shndx| {
350 self.debug_abbrev_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
351 }
352 if (elf_file.debug_str_section_index) |shndx| {
353 self.debug_str_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
354 }
355 if (elf_file.debug_aranges_section_index) |shndx| {
356 self.debug_aranges_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
357 }
358 if (elf_file.debug_line_section_index) |shndx| {
359 self.debug_line_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
360 }
361 if (elf_file.debug_line_str_section_index) |shndx| {
362 self.debug_line_str_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
363 }
364 if (elf_file.debug_loclists_section_index) |shndx| {
365 self.debug_loclists_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
366 }
367 if (elf_file.debug_rnglists_section_index) |shndx| {
368 self.debug_rnglists_section_zig_size = elf_file.shdrs.items[shndx].sh_size;
369 }
370}
371
372fn newSymbol(self: *ZigObject, allocator: Allocator, name_off: u32, st_bind: u4) !Symbol.Index {332fn newSymbol(self: *ZigObject, allocator: Allocator, name_off: u32, st_bind: u4) !Symbol.Index {
373 try self.symtab.ensureUnusedCapacity(allocator, 1);333 try self.symtab.ensureUnusedCapacity(allocator, 1);
374 try self.symbols.ensureUnusedCapacity(allocator, 1);334 try self.symbols.ensureUnusedCapacity(allocator, 1);
src/link/Elf/relocatable.zig+16-18
...@@ -431,24 +431,21 @@ fn writeAtoms(elf_file: *Elf) !void {...@@ -431,24 +431,21 @@ fn writeAtoms(elf_file: *Elf) !void {
431431
432 // TODO really, really handle debug section separately432 // TODO really, really handle debug section separately
433 const base_offset = if (elf_file.isDebugSection(@intCast(shndx))) blk: {433 const base_offset = if (elf_file.isDebugSection(@intCast(shndx))) blk: {
434 const zig_object = elf_file.zigObjectPtr().?;434 const zo = elf_file.zigObjectPtr().?;
435 if (shndx == elf_file.debug_info_section_index.?)435 break :blk for ([_]Symbol.Index{
436 break :blk zig_object.debug_info_section_zig_size;436 zo.debug_info_index.?,
437 if (shndx == elf_file.debug_abbrev_section_index.?)437 zo.debug_abbrev_index.?,
438 break :blk zig_object.debug_abbrev_section_zig_size;438 zo.debug_aranges_index.?,
439 if (shndx == elf_file.debug_str_section_index.?)439 zo.debug_str_index.?,
440 break :blk zig_object.debug_str_section_zig_size;440 zo.debug_line_index.?,
441 if (shndx == elf_file.debug_aranges_section_index.?)441 zo.debug_line_str_index.?,
442 break :blk zig_object.debug_aranges_section_zig_size;442 zo.debug_loclists_index.?,
443 if (shndx == elf_file.debug_line_section_index.?)443 zo.debug_rnglists_index.?,
444 break :blk zig_object.debug_line_section_zig_size;444 }) |sym_index| {
445 if (shndx == elf_file.debug_line_str_section_index.?)445 const sym = zo.symbol(sym_index);
446 break :blk zig_object.debug_line_str_section_zig_size;446 const atom_ptr = sym.atom(elf_file).?;
447 if (shndx == elf_file.debug_loclists_section_index.?)447 if (atom_ptr.output_section_index == shndx) break atom_ptr.size;
448 break :blk zig_object.debug_loclists_section_zig_size;448 } else 0;
449 if (shndx == elf_file.debug_rnglists_section_index.?)
450 break :blk zig_object.debug_rnglists_section_zig_size;
451 unreachable;
452 } else 0;449 } else 0;
453 const sh_offset = shdr.sh_offset + base_offset;450 const sh_offset = shdr.sh_offset + base_offset;
454 const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;451 const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;
...@@ -594,3 +591,4 @@ const Compilation = @import("../../Compilation.zig");...@@ -594,3 +591,4 @@ const Compilation = @import("../../Compilation.zig");
594const Elf = @import("../Elf.zig");591const Elf = @import("../Elf.zig");
595const File = @import("file.zig").File;592const File = @import("file.zig").File;
596const Object = @import("Object.zig");593const Object = @import("Object.zig");
594const Symbol = @import("Symbol.zig");