Skip to content

Commit e79ac14

Browse files
committed
elf: refactor tracking debug section sizes
1 parent db48a78 commit e79ac14

File tree

3 files changed

+46
-96
lines changed

3 files changed

+46
-96
lines changed

src/link/Elf.zig

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4184,26 +4184,21 @@ pub fn allocateNonAllocSections(self: *Elf) !void {
41844184
shdr.sh_offset,
41854185
new_offset,
41864186
});
4187-
const zig_object = self.zigObjectPtr().?;
4188-
const existing_size = blk: {
4189-
if (shndx == self.debug_info_section_index.?)
4190-
break :blk zig_object.debug_info_section_zig_size;
4191-
if (shndx == self.debug_abbrev_section_index.?)
4192-
break :blk zig_object.debug_abbrev_section_zig_size;
4193-
if (shndx == self.debug_str_section_index.?)
4194-
break :blk zig_object.debug_str_section_zig_size;
4195-
if (shndx == self.debug_aranges_section_index.?)
4196-
break :blk zig_object.debug_aranges_section_zig_size;
4197-
if (shndx == self.debug_line_section_index.?)
4198-
break :blk zig_object.debug_line_section_zig_size;
4199-
if (shndx == self.debug_line_str_section_index.?)
4200-
break :blk zig_object.debug_line_str_section_zig_size;
4201-
if (shndx == self.debug_loclists_section_index.?)
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-
};
4187+
const zo = self.zigObjectPtr().?;
4188+
const existing_size = for ([_]Symbol.Index{
4189+
zo.debug_info_index.?,
4190+
zo.debug_abbrev_index.?,
4191+
zo.debug_aranges_index.?,
4192+
zo.debug_str_index.?,
4193+
zo.debug_line_index.?,
4194+
zo.debug_line_str_index.?,
4195+
zo.debug_loclists_index.?,
4196+
zo.debug_rnglists_index.?,
4197+
}) |sym_index| {
4198+
const sym = zo.symbol(sym_index);
4199+
const atom_ptr = sym.atom(self).?;
4200+
if (atom_ptr.output_section_index == shndx) break atom_ptr.size;
4201+
} else 0;
42074202
const amt = try self.base.file.?.copyRangeAll(
42084203
shdr.sh_offset,
42094204
self.base.file.?,
@@ -4299,24 +4294,21 @@ fn writeAtoms(self: *Elf) !void {
42994294

43004295
// TODO really, really handle debug section separately
43014296
const base_offset = if (self.isDebugSection(@intCast(shndx))) blk: {
4302-
const zig_object = self.zigObjectPtr().?;
4303-
if (shndx == self.debug_info_section_index.?)
4304-
break :blk zig_object.debug_info_section_zig_size;
4305-
if (shndx == self.debug_abbrev_section_index.?)
4306-
break :blk zig_object.debug_abbrev_section_zig_size;
4307-
if (shndx == self.debug_str_section_index.?)
4308-
break :blk zig_object.debug_str_section_zig_size;
4309-
if (shndx == self.debug_aranges_section_index.?)
4310-
break :blk zig_object.debug_aranges_section_zig_size;
4311-
if (shndx == self.debug_line_section_index.?)
4312-
break :blk zig_object.debug_line_section_zig_size;
4313-
if (shndx == self.debug_line_str_section_index.?)
4314-
break :blk zig_object.debug_line_str_section_zig_size;
4315-
if (shndx == self.debug_loclists_section_index.?)
4316-
break :blk zig_object.debug_loclists_section_zig_size;
4317-
if (shndx == self.debug_rnglists_section_index.?)
4318-
break :blk zig_object.debug_rnglists_section_zig_size;
4319-
unreachable;
4297+
const zo = self.zigObjectPtr().?;
4298+
break :blk for ([_]Symbol.Index{
4299+
zo.debug_info_index.?,
4300+
zo.debug_abbrev_index.?,
4301+
zo.debug_aranges_index.?,
4302+
zo.debug_str_index.?,
4303+
zo.debug_line_index.?,
4304+
zo.debug_line_str_index.?,
4305+
zo.debug_loclists_index.?,
4306+
zo.debug_rnglists_index.?,
4307+
}) |sym_index| {
4308+
const sym = zo.symbol(sym_index);
4309+
const atom_ptr = sym.atom(self).?;
4310+
if (atom_ptr.output_section_index == shndx) break atom_ptr.size;
4311+
} else 0;
43204312
} else 0;
43214313
const sh_offset = shdr.sh_offset + base_offset;
43224314
const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;

src/link/Elf/ZigObject.zig

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,6 @@ debug_line_str_index: ?Symbol.Index = null,
5959
debug_loclists_index: ?Symbol.Index = null,
6060
debug_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.
64-
debug_info_section_zig_size: u64 = 0,
65-
debug_abbrev_section_zig_size: u64 = 0,
66-
debug_str_section_zig_size: u64 = 0,
67-
debug_aranges_section_zig_size: u64 = 0,
68-
debug_line_section_zig_size: u64 = 0,
69-
debug_line_str_section_zig_size: u64 = 0,
70-
debug_loclists_section_zig_size: u64 = 0,
71-
debug_rnglists_section_zig_size: u64 = 0,
72-
7362
pub const global_symbol_bit: u32 = 0x80000000;
7463
pub const symbol_mask: u32 = 0x7fffffff;
7564
pub const SHN_ATOM: u16 = 0x100;
@@ -328,8 +317,6 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi
328317
self.debug_aranges_section_dirty = false;
329318
self.debug_rnglists_section_dirty = false;
330319
self.debug_str_section_dirty = false;
331-
332-
self.saveDebugSectionsSizes(elf_file);
333320
}
334321

335322
// 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
342329
assert(!self.debug_str_section_dirty);
343330
}
344331

345-
fn 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-
372332
fn newSymbol(self: *ZigObject, allocator: Allocator, name_off: u32, st_bind: u4) !Symbol.Index {
373333
try self.symtab.ensureUnusedCapacity(allocator, 1);
374334
try self.symbols.ensureUnusedCapacity(allocator, 1);

src/link/Elf/relocatable.zig

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -431,24 +431,21 @@ fn writeAtoms(elf_file: *Elf) !void {
431431

432432
// TODO really, really handle debug section separately
433433
const base_offset = if (elf_file.isDebugSection(@intCast(shndx))) blk: {
434-
const zig_object = elf_file.zigObjectPtr().?;
435-
if (shndx == elf_file.debug_info_section_index.?)
436-
break :blk zig_object.debug_info_section_zig_size;
437-
if (shndx == elf_file.debug_abbrev_section_index.?)
438-
break :blk zig_object.debug_abbrev_section_zig_size;
439-
if (shndx == elf_file.debug_str_section_index.?)
440-
break :blk zig_object.debug_str_section_zig_size;
441-
if (shndx == elf_file.debug_aranges_section_index.?)
442-
break :blk zig_object.debug_aranges_section_zig_size;
443-
if (shndx == elf_file.debug_line_section_index.?)
444-
break :blk zig_object.debug_line_section_zig_size;
445-
if (shndx == elf_file.debug_line_str_section_index.?)
446-
break :blk zig_object.debug_line_str_section_zig_size;
447-
if (shndx == elf_file.debug_loclists_section_index.?)
448-
break :blk zig_object.debug_loclists_section_zig_size;
449-
if (shndx == elf_file.debug_rnglists_section_index.?)
450-
break :blk zig_object.debug_rnglists_section_zig_size;
451-
unreachable;
434+
const zo = elf_file.zigObjectPtr().?;
435+
break :blk for ([_]Symbol.Index{
436+
zo.debug_info_index.?,
437+
zo.debug_abbrev_index.?,
438+
zo.debug_aranges_index.?,
439+
zo.debug_str_index.?,
440+
zo.debug_line_index.?,
441+
zo.debug_line_str_index.?,
442+
zo.debug_loclists_index.?,
443+
zo.debug_rnglists_index.?,
444+
}) |sym_index| {
445+
const sym = zo.symbol(sym_index);
446+
const atom_ptr = sym.atom(elf_file).?;
447+
if (atom_ptr.output_section_index == shndx) break atom_ptr.size;
448+
} else 0;
452449
} else 0;
453450
const sh_offset = shdr.sh_offset + base_offset;
454451
const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;
@@ -594,3 +591,4 @@ const Compilation = @import("../../Compilation.zig");
594591
const Elf = @import("../Elf.zig");
595592
const File = @import("file.zig").File;
596593
const Object = @import("Object.zig");
594+
const Symbol = @import("Symbol.zig");

0 commit comments

Comments
 (0)