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 {
803803 try writer.writeAll(self.data.items);
804804}
805805
806pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
806pub fn initRelaSections(self: *ZigObject, elf_file: *Elf) !void {
807807 const gpa = elf_file.base.comp.gpa;
808808 for (self.atoms_indexes.items) |atom_index| {
809809 const atom_ptr = self.atom(atom_index) orelse continue;
......@@ -819,10 +819,28 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
819819 elf_file.getShString(out_shdr.sh_name),
820820 });
821821 defer gpa.free(rela_sect_name);
822 const out_rela_shndx = if (elf_file.sectionByName(rela_sect_name)) |out_rela_shndx|
823 out_rela_shndx
824 else
822 _ = elf_file.sectionByName(rela_sect_name) orelse
825823 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).?;
826844 const out_rela_shdr = &elf_file.sections.items(.shdr)[out_rela_shndx];
827845 out_rela_shdr.sh_info = out_shndx;
828846 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 {
288288}
289289
290290fn initSections(elf_file: *Elf) !void {
291 if (elf_file.zigObjectPtr()) |zo| {
292 try zo.initRelaSections(elf_file);
293 }
291294 for (elf_file.objects.items) |index| {
292295 const object = elf_file.file(index).?.object;
293296 try object.initOutputSections(elf_file);