authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-16 18:36:03+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-20 23:36:41+02:00
log65492b3d5276d7a57f696fdc4da4d47f36909715
tree8ce4c78f636ef6fdfe91a69a600ce9c8d302c929
parent9e0bca73e2ae8c29873c7280f28ce58502e6abab

link/elf: skip empty merge sections when resolving


3 files changed, 8 insertions(+), 3 deletions(-)

src/link/Elf.zig+1-1
...@@ -3341,7 +3341,7 @@ pub fn addCommentString(self: *Elf) !void {...@@ -3341,7 +3341,7 @@ pub fn addCommentString(self: *Elf) !void {
3341 res.sub.* = msub_index;3341 res.sub.* = msub_index;
3342}3342}
33433343
3344fn resolveMergeSections(self: *Elf) !void {3344pub fn resolveMergeSections(self: *Elf) !void {
3345 const tracy = trace(@src());3345 const tracy = trace(@src());
3346 defer tracy.end();3346 defer tracy.end();
33473347
src/link/Elf/Object.zig+4
...@@ -283,6 +283,7 @@ fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) error{O...@@ -283,6 +283,7 @@ fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) error{O
283 const name = blk: {283 const name = blk: {
284 const name = self.getString(shdr.sh_name);284 const name = self.getString(shdr.sh_name);
285 if (elf_file.base.isRelocatable()) break :blk name;285 if (elf_file.base.isRelocatable()) break :blk name;
286 if (shdr.sh_flags & elf.SHF_MERGE != 0) break :blk name;
286 const sh_name_prefixes: []const [:0]const u8 = &.{287 const sh_name_prefixes: []const [:0]const u8 = &.{
287 ".text", ".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",288 ".text", ".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
288 ".init_array", ".fini_array", ".tbss", ".tdata", ".gcc_except_table", ".ctors",289 ".init_array", ".fini_array", ".tbss", ".tdata", ".gcc_except_table", ".ctors",
...@@ -740,6 +741,7 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {...@@ -740,6 +741,7 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
740741
741 for (self.merge_sections.items) |index| {742 for (self.merge_sections.items) |index| {
742 const imsec = elf_file.inputMergeSection(index) orelse continue;743 const imsec = elf_file.inputMergeSection(index) orelse continue;
744 if (imsec.offsets.items.len == 0) continue;
743 const msec = elf_file.mergeSection(imsec.merge_section_index);745 const msec = elf_file.mergeSection(imsec.merge_section_index);
744 const atom_ptr = elf_file.atom(imsec.atom_index).?;746 const atom_ptr = elf_file.atom(imsec.atom_index).?;
745 const isec = atom_ptr.inputShdr(elf_file);747 const isec = atom_ptr.inputShdr(elf_file);
...@@ -773,6 +775,7 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {...@@ -773,6 +775,7 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
773775
774 const imsec_index = self.merge_sections.items[esym.st_shndx];776 const imsec_index = self.merge_sections.items[esym.st_shndx];
775 const imsec = elf_file.inputMergeSection(imsec_index) orelse continue;777 const imsec = elf_file.inputMergeSection(imsec_index) orelse continue;
778 if (imsec.offsets.items.len == 0) continue;
776 const msub_index, const offset = imsec.findSubsection(@intCast(esym.st_value)) orelse {779 const msub_index, const offset = imsec.findSubsection(@intCast(esym.st_value)) orelse {
777 var err = try elf_file.addErrorWithNotes(2);780 var err = try elf_file.addErrorWithNotes(2);
778 try err.addMsg(elf_file, "invalid symbol value: {x}", .{esym.st_value});781 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 {...@@ -797,6 +800,7 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
797800
798 const imsec_index = self.merge_sections.items[esym.st_shndx];801 const imsec_index = self.merge_sections.items[esym.st_shndx];
799 const imsec = elf_file.inputMergeSection(imsec_index) orelse continue;802 const imsec = elf_file.inputMergeSection(imsec_index) orelse continue;
803 if (imsec.offsets.items.len == 0) continue;
800 const msub_index, const offset = imsec.findSubsection(@intCast(@as(i64, @intCast(esym.st_value)) + rel.r_addend)) orelse {804 const msub_index, const offset = imsec.findSubsection(@intCast(@as(i64, @intCast(esym.st_value)) + rel.r_addend)) orelse {
801 var err = try elf_file.addErrorWithNotes(1);805 var err = try elf_file.addErrorWithNotes(1);
802 try err.addMsg(elf_file, "invalid relocation at offset 0x{x}", .{rel.r_offset});806 try err.addMsg(elf_file, "invalid relocation at offset 0x{x}", .{rel.r_offset});
src/link/Elf/relocatable.zig+3-2
...@@ -179,10 +179,11 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const...@@ -179,10 +179,11 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
179 // input Object files.179 // input Object files.
180 elf_file.resolveSymbols();180 elf_file.resolveSymbols();
181 elf_file.markEhFrameAtomsDead();181 elf_file.markEhFrameAtomsDead();
182 claimUnresolved(elf_file);182 try elf_file.resolveMergeSections();
183
184 try elf_file.addCommentString();183 try elf_file.addCommentString();
185 try elf_file.sortMergeSections();184 try elf_file.sortMergeSections();
185 claimUnresolved(elf_file);
186
186 try initSections(elf_file);187 try initSections(elf_file);
187 try elf_file.sortShdrs();188 try elf_file.sortShdrs();
188 if (elf_file.zigObjectPtr()) |zig_object| {189 if (elf_file.zigObjectPtr()) |zig_object| {