authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-29 22:08:36+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-04 13:34:25+02:00
logbc39bddd5fd66037bc524bb7c824230fbb249e01
tree3bda71689e44edf36b97cc44305e89cb91256e69
parentef6ee90ff8762624852ee9a71241bd5a926371ab

elf: remove isDebugSection helper


3 files changed, 54 insertions(+), 71 deletions(-)

src/link/Elf.zig+39-58
......@@ -3572,8 +3572,10 @@ fn updateSectionSizes(self: *Elf) !void {
35723572 if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;
35733573 if (self.zigObjectPtr()) |zo| blk: {
35743574 const sym_index = for ([_]?Symbol.Index{
3575 zo.data_index,
3575 zo.text_index,
3576 zo.rodata_index,
35763577 zo.data_relro_index,
3578 zo.data_index,
35773579 zo.bss_index,
35783580 }) |maybe_idx| {
35793581 if (maybe_idx) |idx| break idx;
......@@ -3913,8 +3915,10 @@ pub fn allocateAllocSections(self: *Elf) !void {
39133915
39143916 if (self.zigObjectPtr()) |zo| blk: {
39153917 const existing_size = for ([_]?Symbol.Index{
3916 zo.data_index,
3918 zo.text_index,
3919 zo.rodata_index,
39173920 zo.data_relro_index,
3921 zo.data_index,
39183922 zo.eh_frame_index,
39193923 }) |maybe_sym_index| {
39203924 const sect_sym_index = maybe_sym_index orelse continue;
......@@ -3954,27 +3958,27 @@ pub fn allocateNonAllocSections(self: *Elf) !void {
39543958 shdr.sh_size = 0;
39553959 const new_offset = try self.findFreeSpace(needed_size, shdr.sh_addralign);
39563960
3957 if (self.isDebugSection(@intCast(shndx))) {
3961 if (self.zigObjectPtr()) |zo| blk: {
3962 const existing_size = for ([_]?Symbol.Index{
3963 zo.debug_info_index,
3964 zo.debug_abbrev_index,
3965 zo.debug_aranges_index,
3966 zo.debug_str_index,
3967 zo.debug_line_index,
3968 zo.debug_line_str_index,
3969 zo.debug_loclists_index,
3970 zo.debug_rnglists_index,
3971 }) |maybe_sym_index| {
3972 const sym_index = maybe_sym_index orelse continue;
3973 const sym = zo.symbol(sym_index);
3974 const atom_ptr = sym.atom(self).?;
3975 if (atom_ptr.output_section_index == shndx) break atom_ptr.size;
3976 } else break :blk;
39583977 log.debug("moving {s} from 0x{x} to 0x{x}", .{
39593978 self.getShString(shdr.sh_name),
39603979 shdr.sh_offset,
39613980 new_offset,
39623981 });
3963 const zo = self.zigObjectPtr().?;
3964 const existing_size = for ([_]Symbol.Index{
3965 zo.debug_info_index.?,
3966 zo.debug_abbrev_index.?,
3967 zo.debug_aranges_index.?,
3968 zo.debug_str_index.?,
3969 zo.debug_line_index.?,
3970 zo.debug_line_str_index.?,
3971 zo.debug_loclists_index.?,
3972 zo.debug_rnglists_index.?,
3973 }) |sym_index| {
3974 const sym = zo.symbol(sym_index);
3975 const atom_ptr = sym.atom(self).?;
3976 if (atom_ptr.output_section_index == shndx) break atom_ptr.size;
3977 } else 0;
39783982 const amt = try self.base.file.?.copyRangeAll(
39793983 shdr.sh_offset,
39803984 self.base.file.?,
......@@ -4068,35 +4072,28 @@ fn writeAtoms(self: *Elf) !void {
40684072 log.debug("writing atoms in '{s}' section", .{self.getShString(shdr.sh_name)});
40694073
40704074 // TODO really, really handle debug section separately
4071 const base_offset = if (self.isDebugSection(@intCast(shndx))) base_offset: {
4072 const zo = self.zigObjectPtr().?;
4073 for ([_]Symbol.Index{
4074 zo.debug_info_index.?,
4075 zo.debug_abbrev_index.?,
4076 zo.debug_aranges_index.?,
4077 zo.debug_str_index.?,
4078 zo.debug_line_index.?,
4079 zo.debug_line_str_index.?,
4080 zo.debug_loclists_index.?,
4081 zo.debug_rnglists_index.?,
4082 }) |sym_index| {
4075 const base_offset = if (self.zigObjectPtr()) |zo| base_offset: {
4076 for ([_]?Symbol.Index{
4077 zo.text_index,
4078 zo.rodata_index,
4079 zo.data_relro_index,
4080 zo.data_index,
4081 zo.eh_frame_index,
4082 zo.debug_info_index,
4083 zo.debug_abbrev_index,
4084 zo.debug_aranges_index,
4085 zo.debug_str_index,
4086 zo.debug_line_index,
4087 zo.debug_line_str_index,
4088 zo.debug_loclists_index,
4089 zo.debug_rnglists_index,
4090 }) |maybe_sym_index| {
4091 const sym_index = maybe_sym_index orelse continue;
40834092 const sym = zo.symbol(sym_index);
40844093 const atom_ptr = sym.atom(self).?;
40854094 if (atom_ptr.output_section_index == shndx) break :base_offset atom_ptr.size;
40864095 }
40874096 break :base_offset 0;
4088 } else if (self.zigObjectPtr()) |zo| base_offset: {
4089 const sym_index = for ([_]?Symbol.Index{
4090 zo.data_index,
4091 zo.data_relro_index,
4092 zo.eh_frame_index,
4093 }) |maybe_idx| {
4094 if (maybe_idx) |idx| break idx;
4095 } else break :base_offset 0;
4096 const sym = zo.symbol(sym_index);
4097 const atom_ptr = sym.atom(self).?;
4098 if (atom_ptr.output_section_index == @as(u32, @intCast(shndx))) break :base_offset atom_ptr.size;
4099 break :base_offset 0;
41004097 } else 0;
41014098 const sh_offset = shdr.sh_offset + base_offset;
41024099 const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;
......@@ -4799,22 +4796,6 @@ pub fn isEffectivelyDynLib(self: Elf) bool {
47994796 };
48004797}
48014798
4802pub fn isDebugSection(self: Elf, shndx: u32) bool {
4803 inline for (&[_]?u32{
4804 self.debug_info_section_index,
4805 self.debug_abbrev_section_index,
4806 self.debug_str_section_index,
4807 self.debug_aranges_section_index,
4808 self.debug_line_section_index,
4809 self.debug_line_str_section_index,
4810 self.debug_loclists_section_index,
4811 self.debug_rnglists_section_index,
4812 }) |index| {
4813 if (index == shndx) return true;
4814 }
4815 return false;
4816}
4817
48184799pub fn addPhdr(self: *Elf, opts: struct {
48194800 type: u32 = 0,
48204801 flags: u32 = 0,
src/link/Elf/ZigObject.zig+3-1
......@@ -52,8 +52,8 @@ debug_rnglists_section_dirty: bool = false,
5252eh_frame_section_dirty: bool = false,
5353
5454text_index: ?Symbol.Index = null,
55data_relro_index: ?Symbol.Index = null,
5655rodata_index: ?Symbol.Index = null,
56data_relro_index: ?Symbol.Index = null,
5757data_index: ?Symbol.Index = null,
5858bss_index: ?Symbol.Index = null,
5959eh_frame_index: ?Symbol.Index = null,
......@@ -2003,6 +2003,8 @@ fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void {
20032003 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits().?);
20042004
20052005 const sect_atom_ptr = for ([_]?Symbol.Index{
2006 self.text_index,
2007 self.rodata_index,
20062008 self.data_index,
20072009 self.data_relro_index,
20082010 }) |maybe_sym_index| {
src/link/Elf/relocatable.zig+12-12
......@@ -436,18 +436,18 @@ fn writeAtoms(elf_file: *Elf) !void {
436436 log.debug("writing atoms in '{s}' section", .{elf_file.getShString(shdr.sh_name)});
437437
438438 // TODO really, really handle debug section separately
439 const base_offset = if (elf_file.isDebugSection(@intCast(shndx))) blk: {
440 const zo = elf_file.zigObjectPtr().?;
441 break :blk for ([_]Symbol.Index{
442 zo.debug_info_index.?,
443 zo.debug_abbrev_index.?,
444 zo.debug_aranges_index.?,
445 zo.debug_str_index.?,
446 zo.debug_line_index.?,
447 zo.debug_line_str_index.?,
448 zo.debug_loclists_index.?,
449 zo.debug_rnglists_index.?,
450 }) |sym_index| {
439 const base_offset = if (elf_file.zigObjectPtr()) |zo| blk: {
440 break :blk for ([_]?Symbol.Index{
441 zo.debug_info_index,
442 zo.debug_abbrev_index,
443 zo.debug_aranges_index,
444 zo.debug_str_index,
445 zo.debug_line_index,
446 zo.debug_line_str_index,
447 zo.debug_loclists_index,
448 zo.debug_rnglists_index,
449 }) |maybe_sym_index| {
450 const sym_index = maybe_sym_index orelse continue;
451451 const sym = zo.symbol(sym_index);
452452 const atom_ptr = sym.atom(elf_file).?;
453453 if (atom_ptr.output_section_index == shndx) break atom_ptr.size;