authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-02 07:57:27+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-04 13:34:26+02:00
log874ef6308e6e438985a141310b94028de33286dc
tree0a2685d4717825892487bc669d7f760af42e0d4f
parent45e46f0fb9f9f8a314802c544e92d1ee865119ef

elf: do not create .eh_frame section if ZigObject already did so


2 files changed, 27 insertions(+), 21 deletions(-)

src/link/Dwarf.zig+2-2
......@@ -391,9 +391,9 @@ pub const Section = struct {
391391 const atom = zo.symbol(sec.index).atom(elf_file).?;
392392 const shndx = atom.output_section_index;
393393 if (sec == &dwarf.debug_frame.section)
394 try elf_file.growAllocSection(shndx, len)
394 try elf_file.growAllocSection(shndx, len, sec.alignment.toByteUnits().?)
395395 else
396 try elf_file.growNonAllocSection(shndx, len, @intCast(sec.alignment.toByteUnits().?), true);
396 try elf_file.growNonAllocSection(shndx, len, sec.alignment.toByteUnits().?, true);
397397 const shdr = elf_file.sections.items(.shdr)[shndx];
398398 atom.size = shdr.sh_size;
399399 atom.alignment = InternPool.Alignment.fromNonzeroByteUnits(shdr.sh_addralign);
src/link/Elf.zig+25-19
......@@ -528,7 +528,7 @@ pub fn findFreeSpace(self: *Elf, object_size: u64, min_alignment: u64) !u64 {
528528 return start;
529529}
530530
531pub fn growAllocSection(self: *Elf, shdr_index: u32, needed_size: u64) !void {
531pub fn growAllocSection(self: *Elf, shdr_index: u32, needed_size: u64, min_alignment: u64) !void {
532532 const slice = self.sections.slice();
533533 const shdr = &slice.items(.shdr)[shdr_index];
534534 assert(shdr.sh_flags & elf.SHF_ALLOC != 0);
......@@ -547,8 +547,7 @@ pub fn growAllocSection(self: *Elf, shdr_index: u32, needed_size: u64) !void {
547547 const existing_size = shdr.sh_size;
548548 shdr.sh_size = 0;
549549 // Must move the entire section.
550 const alignment = if (maybe_phdr) |phdr| phdr.p_align else shdr.sh_addralign;
551 const new_offset = try self.findFreeSpace(needed_size, alignment);
550 const new_offset = try self.findFreeSpace(needed_size, min_alignment);
552551
553552 log.debug("new '{s}' file offset 0x{x} to 0x{x}", .{
554553 self.getShString(shdr.sh_name),
......@@ -588,7 +587,7 @@ pub fn growNonAllocSection(
588587 self: *Elf,
589588 shdr_index: u32,
590589 needed_size: u64,
591 min_alignment: u32,
590 min_alignment: u64,
592591 requires_file_copy: bool,
593592) !void {
594593 const shdr = &self.sections.items(.shdr)[shdr_index];
......@@ -728,9 +727,9 @@ pub fn allocateChunk(self: *Elf, shndx: u32, size: u64, alignment: Atom.Alignmen
728727 if (expand_section) {
729728 const needed_size = res.value + size;
730729 if (shdr.sh_flags & elf.SHF_ALLOC != 0)
731 try self.growAllocSection(shndx, needed_size)
730 try self.growAllocSection(shndx, needed_size, alignment.toByteUnits().?)
732731 else
733 try self.growNonAllocSection(shndx, needed_size, @intCast(alignment.toByteUnits().?), true);
732 try self.growNonAllocSection(shndx, needed_size, alignment.toByteUnits().?, true);
734733 }
735734
736735 return res;
......@@ -1831,8 +1830,8 @@ pub fn initOutputSection(self: *Elf, args: struct {
18311830 break :blk args.name;
18321831 };
18331832 const @"type" = tt: {
1834 if (self.getTarget().cpu.arch == .x86_64 and
1835 args.type == elf.SHT_X86_64_UNWIND) break :tt elf.SHT_PROGBITS;
1833 if (self.getTarget().cpu.arch == .x86_64 and args.type == elf.SHT_X86_64_UNWIND)
1834 break :tt elf.SHT_PROGBITS;
18361835 switch (args.type) {
18371836 elf.SHT_NULL => unreachable,
18381837 elf.SHT_PROGBITS => {
......@@ -2896,21 +2895,28 @@ fn initSyntheticSections(self: *Elf) !void {
28962895 const target = self.getTarget();
28972896 const ptr_size = self.ptrWidthBytes();
28982897
2899 const needs_eh_frame = for (self.objects.items) |index| {
2898 const needs_eh_frame = if (self.zigObjectPtr()) |zo|
2899 zo.eh_frame_index != null
2900 else for (self.objects.items) |index| {
29002901 if (self.file(index).?.object.cies.items.len > 0) break true;
29012902 } else false;
29022903 if (needs_eh_frame) {
29032904 if (self.eh_frame_section_index == null) {
2904 self.eh_frame_section_index = try self.addSection(.{
2905 .name = try self.insertShString(".eh_frame"),
2906 .type = if (target.cpu.arch == .x86_64)
2907 elf.SHT_X86_64_UNWIND
2908 else
2909 elf.SHT_PROGBITS,
2910 .flags = elf.SHF_ALLOC,
2911 .addralign = ptr_size,
2912 .offset = std.math.maxInt(u64),
2913 });
2905 self.eh_frame_section_index = blk: {
2906 if (self.zigObjectPtr()) |zo| {
2907 if (zo.eh_frame_index) |idx| break :blk zo.symbol(idx).atom(self).?.output_section_index;
2908 }
2909 break :blk try self.addSection(.{
2910 .name = try self.insertShString(".eh_frame"),
2911 .type = if (target.cpu.arch == .x86_64)
2912 elf.SHT_X86_64_UNWIND
2913 else
2914 elf.SHT_PROGBITS,
2915 .flags = elf.SHF_ALLOC,
2916 .addralign = ptr_size,
2917 .offset = std.math.maxInt(u64),
2918 });
2919 };
29142920 }
29152921 if (comp.link_eh_frame_hdr) {
29162922 self.eh_frame_hdr_section_index = try self.addSection(.{