zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 65492b3d5276d7a57f696fdc4da4d47f36909715 (tree)
parent 9e0bca73e2ae8c29873c7280f28ce58502e6abab
Author: Jakub Konka <kubkon@jakubkonka.com>
Date:   Tue, 16 Apr 2024 18:36:03 +0200

link/elf: skip empty merge sections when resolving

Diffstat:
Msrc/link/Elf.zig | 2+-
Msrc/link/Elf/Object.zig | 4++++
Msrc/link/Elf/relocatable.zig | 5+++--
3 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/link/Elf.zig b/src/link/Elf.zig @@ -3341,7 +3341,7 @@ pub fn addCommentString(self: *Elf) !void { res.sub.* = msub_index; } -fn resolveMergeSections(self: *Elf) !void { +pub fn resolveMergeSections(self: *Elf) !void { const tracy = trace(@src()); defer tracy.end(); diff --git a/src/link/Elf/Object.zig b/src/link/Elf/Object.zig @@ -283,6 +283,7 @@ fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) error{O const name = blk: { const name = self.getString(shdr.sh_name); if (elf_file.base.isRelocatable()) break :blk name; + if (shdr.sh_flags & elf.SHF_MERGE != 0) break :blk name; const sh_name_prefixes: []const [:0]const u8 = &.{ ".text", ".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss", ".init_array", ".fini_array", ".tbss", ".tdata", ".gcc_except_table", ".ctors", @@ -740,6 +741,7 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void { for (self.merge_sections.items) |index| { const imsec = elf_file.inputMergeSection(index) orelse continue; + if (imsec.offsets.items.len == 0) continue; const msec = elf_file.mergeSection(imsec.merge_section_index); const atom_ptr = elf_file.atom(imsec.atom_index).?; const isec = atom_ptr.inputShdr(elf_file); @@ -773,6 +775,7 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void { const imsec_index = self.merge_sections.items[esym.st_shndx]; const imsec = elf_file.inputMergeSection(imsec_index) orelse continue; + if (imsec.offsets.items.len == 0) continue; const msub_index, const offset = imsec.findSubsection(@intCast(esym.st_value)) orelse { var err = try elf_file.addErrorWithNotes(2); try err.addMsg(elf_file, "invalid symbol value: {x}", .{esym.st_value}); @@ -797,6 +800,7 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void { const imsec_index = self.merge_sections.items[esym.st_shndx]; const imsec = elf_file.inputMergeSection(imsec_index) orelse continue; + if (imsec.offsets.items.len == 0) continue; const msub_index, const offset = imsec.findSubsection(@intCast(@as(i64, @intCast(esym.st_value)) + rel.r_addend)) orelse { var err = try elf_file.addErrorWithNotes(1); try err.addMsg(elf_file, "invalid relocation at offset 0x{x}", .{rel.r_offset}); diff --git a/src/link/Elf/relocatable.zig b/src/link/Elf/relocatable.zig @@ -179,10 +179,11 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const // input Object files. elf_file.resolveSymbols(); elf_file.markEhFrameAtomsDead(); - claimUnresolved(elf_file); - + try elf_file.resolveMergeSections(); try elf_file.addCommentString(); try elf_file.sortMergeSections(); + claimUnresolved(elf_file); + try initSections(elf_file); try elf_file.sortShdrs(); if (elf_file.zigObjectPtr()) |zig_object| {