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 {...@@ -3572,8 +3572,10 @@ fn updateSectionSizes(self: *Elf) !void {
3572 if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;3572 if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;
3573 if (self.zigObjectPtr()) |zo| blk: {3573 if (self.zigObjectPtr()) |zo| blk: {
3574 const sym_index = for ([_]?Symbol.Index{3574 const sym_index = for ([_]?Symbol.Index{
3575 zo.data_index,3575 zo.text_index,
3576 zo.rodata_index,
3576 zo.data_relro_index,3577 zo.data_relro_index,
3578 zo.data_index,
3577 zo.bss_index,3579 zo.bss_index,
3578 }) |maybe_idx| {3580 }) |maybe_idx| {
3579 if (maybe_idx) |idx| break idx;3581 if (maybe_idx) |idx| break idx;
...@@ -3913,8 +3915,10 @@ pub fn allocateAllocSections(self: *Elf) !void {...@@ -3913,8 +3915,10 @@ pub fn allocateAllocSections(self: *Elf) !void {
39133915
3914 if (self.zigObjectPtr()) |zo| blk: {3916 if (self.zigObjectPtr()) |zo| blk: {
3915 const existing_size = for ([_]?Symbol.Index{3917 const existing_size = for ([_]?Symbol.Index{
3916 zo.data_index,3918 zo.text_index,
3919 zo.rodata_index,
3917 zo.data_relro_index,3920 zo.data_relro_index,
3921 zo.data_index,
3918 zo.eh_frame_index,3922 zo.eh_frame_index,
3919 }) |maybe_sym_index| {3923 }) |maybe_sym_index| {
3920 const sect_sym_index = maybe_sym_index orelse continue;3924 const sect_sym_index = maybe_sym_index orelse continue;
...@@ -3954,27 +3958,27 @@ pub fn allocateNonAllocSections(self: *Elf) !void {...@@ -3954,27 +3958,27 @@ pub fn allocateNonAllocSections(self: *Elf) !void {
3954 shdr.sh_size = 0;3958 shdr.sh_size = 0;
3955 const new_offset = try self.findFreeSpace(needed_size, shdr.sh_addralign);3959 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;
3958 log.debug("moving {s} from 0x{x} to 0x{x}", .{3977 log.debug("moving {s} from 0x{x} to 0x{x}", .{
3959 self.getShString(shdr.sh_name),3978 self.getShString(shdr.sh_name),
3960 shdr.sh_offset,3979 shdr.sh_offset,
3961 new_offset,3980 new_offset,
3962 });3981 });
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;
3978 const amt = try self.base.file.?.copyRangeAll(3982 const amt = try self.base.file.?.copyRangeAll(
3979 shdr.sh_offset,3983 shdr.sh_offset,
3980 self.base.file.?,3984 self.base.file.?,
...@@ -4068,35 +4072,28 @@ fn writeAtoms(self: *Elf) !void {...@@ -4068,35 +4072,28 @@ fn writeAtoms(self: *Elf) !void {
4068 log.debug("writing atoms in '{s}' section", .{self.getShString(shdr.sh_name)});4072 log.debug("writing atoms in '{s}' section", .{self.getShString(shdr.sh_name)});
40694073
4070 // TODO really, really handle debug section separately4074 // TODO really, really handle debug section separately
4071 const base_offset = if (self.isDebugSection(@intCast(shndx))) base_offset: {4075 const base_offset = if (self.zigObjectPtr()) |zo| base_offset: {
4072 const zo = self.zigObjectPtr().?;4076 for ([_]?Symbol.Index{
4073 for ([_]Symbol.Index{4077 zo.text_index,
4074 zo.debug_info_index.?,4078 zo.rodata_index,
4075 zo.debug_abbrev_index.?,4079 zo.data_relro_index,
4076 zo.debug_aranges_index.?,4080 zo.data_index,
4077 zo.debug_str_index.?,4081 zo.eh_frame_index,
4078 zo.debug_line_index.?,4082 zo.debug_info_index,
4079 zo.debug_line_str_index.?,4083 zo.debug_abbrev_index,
4080 zo.debug_loclists_index.?,4084 zo.debug_aranges_index,
4081 zo.debug_rnglists_index.?,4085 zo.debug_str_index,
4082 }) |sym_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;
4083 const sym = zo.symbol(sym_index);4092 const sym = zo.symbol(sym_index);
4084 const atom_ptr = sym.atom(self).?;4093 const atom_ptr = sym.atom(self).?;
4085 if (atom_ptr.output_section_index == shndx) break :base_offset atom_ptr.size;4094 if (atom_ptr.output_section_index == shndx) break :base_offset atom_ptr.size;
4086 }4095 }
4087 break :base_offset 0;4096 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;
4100 } else 0;4097 } else 0;
4101 const sh_offset = shdr.sh_offset + base_offset;4098 const sh_offset = shdr.sh_offset + base_offset;
4102 const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;4099 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 {...@@ -4799,22 +4796,6 @@ pub fn isEffectivelyDynLib(self: Elf) bool {
4799 };4796 };
4800}4797}
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
4818pub fn addPhdr(self: *Elf, opts: struct {4799pub fn addPhdr(self: *Elf, opts: struct {
4819 type: u32 = 0,4800 type: u32 = 0,
4820 flags: u32 = 0,4801 flags: u32 = 0,
src/link/Elf/ZigObject.zig+3-1
...@@ -52,8 +52,8 @@ debug_rnglists_section_dirty: bool = false,...@@ -52,8 +52,8 @@ debug_rnglists_section_dirty: bool = false,
52eh_frame_section_dirty: bool = false,52eh_frame_section_dirty: bool = false,
5353
54text_index: ?Symbol.Index = null,54text_index: ?Symbol.Index = null,
55data_relro_index: ?Symbol.Index = null,
56rodata_index: ?Symbol.Index = null,55rodata_index: ?Symbol.Index = null,
56data_relro_index: ?Symbol.Index = null,
57data_index: ?Symbol.Index = null,57data_index: ?Symbol.Index = null,
58bss_index: ?Symbol.Index = null,58bss_index: ?Symbol.Index = null,
59eh_frame_index: ?Symbol.Index = null,59eh_frame_index: ?Symbol.Index = null,
...@@ -2003,6 +2003,8 @@ fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void {...@@ -2003,6 +2003,8 @@ fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void {
2003 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits().?);2003 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits().?);
20042004
2005 const sect_atom_ptr = for ([_]?Symbol.Index{2005 const sect_atom_ptr = for ([_]?Symbol.Index{
2006 self.text_index,
2007 self.rodata_index,
2006 self.data_index,2008 self.data_index,
2007 self.data_relro_index,2009 self.data_relro_index,
2008 }) |maybe_sym_index| {2010 }) |maybe_sym_index| {
src/link/Elf/relocatable.zig+12-12
...@@ -436,18 +436,18 @@ fn writeAtoms(elf_file: *Elf) !void {...@@ -436,18 +436,18 @@ fn writeAtoms(elf_file: *Elf) !void {
436 log.debug("writing atoms in '{s}' section", .{elf_file.getShString(shdr.sh_name)});436 log.debug("writing atoms in '{s}' section", .{elf_file.getShString(shdr.sh_name)});
437437
438 // TODO really, really handle debug section separately438 // TODO really, really handle debug section separately
439 const base_offset = if (elf_file.isDebugSection(@intCast(shndx))) blk: {439 const base_offset = if (elf_file.zigObjectPtr()) |zo| blk: {
440 const zo = elf_file.zigObjectPtr().?;440 break :blk for ([_]?Symbol.Index{
441 break :blk for ([_]Symbol.Index{441 zo.debug_info_index,
442 zo.debug_info_index.?,442 zo.debug_abbrev_index,
443 zo.debug_abbrev_index.?,443 zo.debug_aranges_index,
444 zo.debug_aranges_index.?,444 zo.debug_str_index,
445 zo.debug_str_index.?,445 zo.debug_line_index,
446 zo.debug_line_index.?,446 zo.debug_line_str_index,
447 zo.debug_line_str_index.?,447 zo.debug_loclists_index,
448 zo.debug_loclists_index.?,448 zo.debug_rnglists_index,
449 zo.debug_rnglists_index.?,449 }) |maybe_sym_index| {
450 }) |sym_index| {450 const sym_index = maybe_sym_index orelse continue;
451 const sym = zo.symbol(sym_index);451 const sym = zo.symbol(sym_index);
452 const atom_ptr = sym.atom(elf_file).?;452 const atom_ptr = sym.atom(elf_file).?;
453 if (atom_ptr.output_section_index == shndx) break atom_ptr.size;453 if (atom_ptr.output_section_index == shndx) break atom_ptr.size;