Commit 20240e9cd5

Jakub Konka <kubkon@jakubkonka.com>
2024-08-23 15:04:46
elf: store atom refs for rela sections until we can do better
1 parent 1af0f4c
Changed files (4)
src/link/Elf/Object.zig
@@ -1018,7 +1018,7 @@ pub fn initRelaSections(self: *Object, elf_file: *Elf) !void {
     }
 }
 
-pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) void {
+pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {
     for (self.atoms_indexes.items) |atom_index| {
         const atom_ptr = self.atom(atom_index) orelse continue;
         if (!atom_ptr.alive) continue;
@@ -1031,6 +1031,9 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) void {
         const shdr = &slice.items(.shdr)[shndx];
         shdr.sh_info = atom_ptr.output_section_index;
         shdr.sh_link = elf_file.symtab_section_index.?;
+        const gpa = elf_file.base.comp.gpa;
+        const atom_list = &elf_file.sections.items(.atom_list)[shndx];
+        try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
     }
 }
 
src/link/Elf/relocatable.zig
@@ -209,7 +209,7 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
     for (elf_file.objects.items) |index| {
         const object = elf_file.file(index).?.object;
         try object.addAtomsToOutputSections(elf_file);
-        object.addAtomsToRelaSections(elf_file);
+        try object.addAtomsToRelaSections(elf_file);
     }
     try elf_file.updateMergeSectionSizes();
     try updateSectionSizes(elf_file);
@@ -349,8 +349,8 @@ fn initComdatGroups(elf_file: *Elf) !void {
 fn updateSectionSizes(elf_file: *Elf) !void {
     const slice = elf_file.sections.slice();
     for (slice.items(.shdr), 0..) |*shdr, shndx| {
+        const atom_list = slice.items(.atom_list)[shndx];
         if (shdr.sh_type != elf.SHT_RELA) {
-            const atom_list = slice.items(.atom_list)[shndx];
             for (atom_list.items) |ref| {
                 const atom_ptr = elf_file.atom(ref) orelse continue;
                 if (!atom_ptr.alive) continue;
@@ -361,7 +361,6 @@ fn updateSectionSizes(elf_file: *Elf) !void {
                 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);
             }
         } else {
-            const atom_list = slice.items(.atom_list)[shdr.sh_info];
             for (atom_list.items) |ref| {
                 const atom_ptr = elf_file.atom(ref) orelse continue;
                 if (!atom_ptr.alive) continue;
@@ -492,9 +491,8 @@ fn writeSyntheticSections(elf_file: *Elf) !void {
     const gpa = elf_file.base.comp.gpa;
     const slice = elf_file.sections.slice();
 
-    for (slice.items(.shdr)) |shdr| {
+    for (slice.items(.shdr), slice.items(.atom_list)) |shdr, atom_list| {
         if (shdr.sh_type != elf.SHT_RELA) continue;
-        const atom_list = slice.items(.atom_list)[shdr.sh_info];
         if (atom_list.items.len == 0) continue;
 
         const num_relocs = math.cast(usize, @divExact(shdr.sh_size, shdr.sh_entsize)) orelse
src/link/Elf/ZigObject.zig
@@ -967,7 +967,10 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
         const out_shndx = atom_ptr.output_section_index;
         const out_shdr = elf_file.sections.items(.shdr)[out_shndx];
         if (out_shdr.sh_type == elf.SHT_NOBITS) continue;
-        const atom_list = &elf_file.sections.items(.atom_list)[out_shndx];
+        const out_rela_shndx = for (elf_file.sections.items(.shdr), 0..) |out_rela_shdr, out_rela_shndx| {
+            if (out_rela_shdr.sh_type == elf.SHT_RELA and out_rela_shdr.sh_info == out_shndx) break out_rela_shndx;
+        } else unreachable;
+        const atom_list = &elf_file.sections.items(.atom_list)[out_rela_shndx];
         const gpa = elf_file.base.comp.gpa;
         try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
     }
src/link/Elf.zig
@@ -3373,7 +3373,10 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
 
     for (self.sections.items(.shdr)) |*shdr| {
         if (shdr.sh_type != elf.SHT_RELA) continue;
-        shdr.sh_link = backlinks[shdr.sh_link];
+        // FIXME:JK we should spin up .symtab potentially earlier, or set all non-dynamic RELA sections
+        // to point at symtab
+        // shdr.sh_link = backlinks[shdr.sh_link];
+        shdr.sh_link = self.symtab_section_index.?;
         shdr.sh_info = backlinks[shdr.sh_info];
     }