authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-23 15:04:46+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-25 10:20:15+02:00
log20240e9cd5a796b63bc2571aa70bb77144e8860c
tree3e10a5ab15fa4228bb724d1da584ce70b96ecc29
parent1af0f4cd00f7a6b9150346449d2e7dc14798bada

elf: store atom refs for rela sections until we can do better


4 files changed, 15 insertions(+), 8 deletions(-)

src/link/Elf.zig+4-1
......@@ -3373,7 +3373,10 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
33733373
33743374 for (self.sections.items(.shdr)) |*shdr| {
33753375 if (shdr.sh_type != elf.SHT_RELA) continue;
3376 shdr.sh_link = backlinks[shdr.sh_link];
3376 // FIXME:JK we should spin up .symtab potentially earlier, or set all non-dynamic RELA sections
3377 // to point at symtab
3378 // shdr.sh_link = backlinks[shdr.sh_link];
3379 shdr.sh_link = self.symtab_section_index.?;
33773380 shdr.sh_info = backlinks[shdr.sh_info];
33783381 }
33793382
src/link/Elf/Object.zig+4-1
......@@ -1018,7 +1018,7 @@ pub fn initRelaSections(self: *Object, elf_file: *Elf) !void {
10181018 }
10191019}
10201020
1021pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) void {
1021pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {
10221022 for (self.atoms_indexes.items) |atom_index| {
10231023 const atom_ptr = self.atom(atom_index) orelse continue;
10241024 if (!atom_ptr.alive) continue;
......@@ -1031,6 +1031,9 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) void {
10311031 const shdr = &slice.items(.shdr)[shndx];
10321032 shdr.sh_info = atom_ptr.output_section_index;
10331033 shdr.sh_link = elf_file.symtab_section_index.?;
1034 const gpa = elf_file.base.comp.gpa;
1035 const atom_list = &elf_file.sections.items(.atom_list)[shndx];
1036 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
10341037 }
10351038}
10361039
src/link/Elf/ZigObject.zig+4-1
......@@ -967,7 +967,10 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
967967 const out_shndx = atom_ptr.output_section_index;
968968 const out_shdr = elf_file.sections.items(.shdr)[out_shndx];
969969 if (out_shdr.sh_type == elf.SHT_NOBITS) continue;
970 const atom_list = &elf_file.sections.items(.atom_list)[out_shndx];
970 const out_rela_shndx = for (elf_file.sections.items(.shdr), 0..) |out_rela_shdr, out_rela_shndx| {
971 if (out_rela_shdr.sh_type == elf.SHT_RELA and out_rela_shdr.sh_info == out_shndx) break out_rela_shndx;
972 } else unreachable;
973 const atom_list = &elf_file.sections.items(.atom_list)[out_rela_shndx];
971974 const gpa = elf_file.base.comp.gpa;
972975 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
973976 }
src/link/Elf/relocatable.zig+3-5
......@@ -209,7 +209,7 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
209209 for (elf_file.objects.items) |index| {
210210 const object = elf_file.file(index).?.object;
211211 try object.addAtomsToOutputSections(elf_file);
212 object.addAtomsToRelaSections(elf_file);
212 try object.addAtomsToRelaSections(elf_file);
213213 }
214214 try elf_file.updateMergeSectionSizes();
215215 try updateSectionSizes(elf_file);
......@@ -349,8 +349,8 @@ fn initComdatGroups(elf_file: *Elf) !void {
349349fn updateSectionSizes(elf_file: *Elf) !void {
350350 const slice = elf_file.sections.slice();
351351 for (slice.items(.shdr), 0..) |*shdr, shndx| {
352 const atom_list = slice.items(.atom_list)[shndx];
352353 if (shdr.sh_type != elf.SHT_RELA) {
353 const atom_list = slice.items(.atom_list)[shndx];
354354 for (atom_list.items) |ref| {
355355 const atom_ptr = elf_file.atom(ref) orelse continue;
356356 if (!atom_ptr.alive) continue;
......@@ -361,7 +361,6 @@ fn updateSectionSizes(elf_file: *Elf) !void {
361361 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);
362362 }
363363 } else {
364 const atom_list = slice.items(.atom_list)[shdr.sh_info];
365364 for (atom_list.items) |ref| {
366365 const atom_ptr = elf_file.atom(ref) orelse continue;
367366 if (!atom_ptr.alive) continue;
......@@ -492,9 +491,8 @@ fn writeSyntheticSections(elf_file: *Elf) !void {
492491 const gpa = elf_file.base.comp.gpa;
493492 const slice = elf_file.sections.slice();
494493
495 for (slice.items(.shdr)) |shdr| {
494 for (slice.items(.shdr), slice.items(.atom_list)) |shdr, atom_list| {
496495 if (shdr.sh_type != elf.SHT_RELA) continue;
497 const atom_list = slice.items(.atom_list)[shdr.sh_info];
498496 if (atom_list.items.len == 0) continue;
499497
500498 const num_relocs = math.cast(usize, @divExact(shdr.sh_size, shdr.sh_entsize)) orelse