authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-29 23:23:39+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-04 13:34:25+02:00
logacb91f4b30e6cbb14bb81406ca6ca71241dc3a98
tree23b3a270fcac6d18c9de6025ed2318b78b2973a0
parent25fa092bb1940ccd88f413a6ebe0a5d4d3befa71

elf: fix emitting correct .rela. sections in -r mode


1 files changed, 12 insertions(+), 12 deletions(-)

src/link/Elf/ZigObject.zig+12-12
...@@ -494,14 +494,6 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi...@@ -494,14 +494,6 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi
494 }494 }
495 }495 }
496 }496 }
497
498 if (elf_file.base.isRelocatable() and relocs.items.len > 0) {
499 const rela_sect_name = try std.fmt.allocPrintZ(gpa, ".rela{s}", .{elf_file.getShString(shdr.sh_name)});
500 defer gpa.free(rela_sect_name);
501 if (elf_file.sectionByName(rela_sect_name) == null) {
502 _ = try elf_file.addRelaShdr(try elf_file.insertShString(rela_sect_name), shndx);
503 }
504 }
505 }497 }
506498
507 self.debug_abbrev_section_dirty = false;499 self.debug_abbrev_section_dirty = false;
...@@ -820,6 +812,7 @@ pub fn writeAr(self: ZigObject, writer: anytype) !void {...@@ -820,6 +812,7 @@ pub fn writeAr(self: ZigObject, writer: anytype) !void {
820}812}
821813
822pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {814pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
815 const gpa = elf_file.base.comp.gpa;
823 for (self.atoms_indexes.items) |atom_index| {816 for (self.atoms_indexes.items) |atom_index| {
824 const atom_ptr = self.atom(atom_index) orelse continue;817 const atom_ptr = self.atom(atom_index) orelse continue;
825 if (!atom_ptr.alive) continue;818 if (!atom_ptr.alive) continue;
...@@ -829,11 +822,18 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {...@@ -829,11 +822,18 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
829 const out_shndx = atom_ptr.output_section_index;822 const out_shndx = atom_ptr.output_section_index;
830 const out_shdr = elf_file.sections.items(.shdr)[out_shndx];823 const out_shdr = elf_file.sections.items(.shdr)[out_shndx];
831 if (out_shdr.sh_type == elf.SHT_NOBITS) continue;824 if (out_shdr.sh_type == elf.SHT_NOBITS) continue;
832 const out_rela_shndx = for (elf_file.sections.items(.shdr), 0..) |out_rela_shdr, out_rela_shndx| {825 const rela_sect_name = try std.fmt.allocPrintZ(gpa, ".rela{s}", .{
833 if (out_rela_shdr.sh_type == elf.SHT_RELA and out_rela_shdr.sh_info == out_shndx) break out_rela_shndx;826 elf_file.getShString(out_shdr.sh_name),
834 } else unreachable;827 });
828 defer gpa.free(rela_sect_name);
829 const out_rela_shndx = if (elf_file.sectionByName(rela_sect_name)) |out_rela_shndx|
830 out_rela_shndx
831 else
832 try elf_file.addRelaShdr(try elf_file.insertShString(rela_sect_name), out_shndx);
833 const out_rela_shdr = &elf_file.sections.items(.shdr)[out_rela_shndx];
834 out_rela_shdr.sh_info = out_shndx;
835 out_rela_shdr.sh_link = elf_file.symtab_section_index.?;
835 const atom_list = &elf_file.sections.items(.atom_list)[out_rela_shndx];836 const atom_list = &elf_file.sections.items(.atom_list)[out_rela_shndx];
836 const gpa = elf_file.base.comp.gpa;
837 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });837 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
838 }838 }
839}839}