authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-03 13:49:14+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-04 13:34:26+02:00
log88e0d49febf5839654e4a5f3dc02ad6b2a82e242
tree80f54a7fc23430c05a8d3b2983ada99b6078b382
parent2ef3e30e2d04f16510d0953e6d266a97bcb4eb49

elf: init rela sections in a separate pass for ZigObject


2 files changed, 25 insertions(+), 4 deletions(-)

src/link/Elf/ZigObject.zig+22-4
...@@ -803,7 +803,7 @@ pub fn writeAr(self: ZigObject, writer: anytype) !void {...@@ -803,7 +803,7 @@ pub fn writeAr(self: ZigObject, writer: anytype) !void {
803 try writer.writeAll(self.data.items);803 try writer.writeAll(self.data.items);
804}804}
805805
806pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {806pub fn initRelaSections(self: *ZigObject, elf_file: *Elf) !void {
807 const gpa = elf_file.base.comp.gpa;807 const gpa = elf_file.base.comp.gpa;
808 for (self.atoms_indexes.items) |atom_index| {808 for (self.atoms_indexes.items) |atom_index| {
809 const atom_ptr = self.atom(atom_index) orelse continue;809 const atom_ptr = self.atom(atom_index) orelse continue;
...@@ -819,10 +819,28 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {...@@ -819,10 +819,28 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
819 elf_file.getShString(out_shdr.sh_name),819 elf_file.getShString(out_shdr.sh_name),
820 });820 });
821 defer gpa.free(rela_sect_name);821 defer gpa.free(rela_sect_name);
822 const out_rela_shndx = if (elf_file.sectionByName(rela_sect_name)) |out_rela_shndx|822 _ = elf_file.sectionByName(rela_sect_name) orelse
823 out_rela_shndx
824 else
825 try elf_file.addRelaShdr(try elf_file.insertShString(rela_sect_name), out_shndx);823 try elf_file.addRelaShdr(try elf_file.insertShString(rela_sect_name), out_shndx);
824 }
825}
826
827pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
828 const gpa = elf_file.base.comp.gpa;
829 for (self.atoms_indexes.items) |atom_index| {
830 const atom_ptr = self.atom(atom_index) orelse continue;
831 if (!atom_ptr.alive) continue;
832 if (atom_ptr.output_section_index == elf_file.eh_frame_section_index) continue;
833 const rela_shndx = atom_ptr.relocsShndx() orelse continue;
834 // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level
835 if (self.relocs.items[rela_shndx].items.len == 0) continue;
836 const out_shndx = atom_ptr.output_section_index;
837 const out_shdr = elf_file.sections.items(.shdr)[out_shndx];
838 if (out_shdr.sh_type == elf.SHT_NOBITS) continue;
839 const rela_sect_name = try std.fmt.allocPrintZ(gpa, ".rela{s}", .{
840 elf_file.getShString(out_shdr.sh_name),
841 });
842 defer gpa.free(rela_sect_name);
843 const out_rela_shndx = elf_file.sectionByName(rela_sect_name).?;
826 const out_rela_shdr = &elf_file.sections.items(.shdr)[out_rela_shndx];844 const out_rela_shdr = &elf_file.sections.items(.shdr)[out_rela_shndx];
827 out_rela_shdr.sh_info = out_shndx;845 out_rela_shdr.sh_info = out_shndx;
828 out_rela_shdr.sh_link = elf_file.symtab_section_index.?;846 out_rela_shdr.sh_link = elf_file.symtab_section_index.?;
src/link/Elf/relocatable.zig+3
...@@ -288,6 +288,9 @@ fn claimUnresolved(elf_file: *Elf) void {...@@ -288,6 +288,9 @@ fn claimUnresolved(elf_file: *Elf) void {
288}288}
289289
290fn initSections(elf_file: *Elf) !void {290fn initSections(elf_file: *Elf) !void {
291 if (elf_file.zigObjectPtr()) |zo| {
292 try zo.initRelaSections(elf_file);
293 }
291 for (elf_file.objects.items) |index| {294 for (elf_file.objects.items) |index| {
292 const object = elf_file.file(index).?.object;295 const object = elf_file.file(index).?.object;
293 try object.initOutputSections(elf_file);296 try object.initOutputSections(elf_file);