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 {...@@ -3373,7 +3373,10 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
33733373
3374 for (self.sections.items(.shdr)) |*shdr| {3374 for (self.sections.items(.shdr)) |*shdr| {
3375 if (shdr.sh_type != elf.SHT_RELA) continue;3375 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.?;
3377 shdr.sh_info = backlinks[shdr.sh_info];3380 shdr.sh_info = backlinks[shdr.sh_info];
3378 }3381 }
33793382
src/link/Elf/Object.zig+4-1
...@@ -1018,7 +1018,7 @@ pub fn initRelaSections(self: *Object, elf_file: *Elf) !void {...@@ -1018,7 +1018,7 @@ pub fn initRelaSections(self: *Object, elf_file: *Elf) !void {
1018 }1018 }
1019}1019}
10201020
1021pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) void {1021pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {
1022 for (self.atoms_indexes.items) |atom_index| {1022 for (self.atoms_indexes.items) |atom_index| {
1023 const atom_ptr = self.atom(atom_index) orelse continue;1023 const atom_ptr = self.atom(atom_index) orelse continue;
1024 if (!atom_ptr.alive) continue;1024 if (!atom_ptr.alive) continue;
...@@ -1031,6 +1031,9 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) void {...@@ -1031,6 +1031,9 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) void {
1031 const shdr = &slice.items(.shdr)[shndx];1031 const shdr = &slice.items(.shdr)[shndx];
1032 shdr.sh_info = atom_ptr.output_section_index;1032 shdr.sh_info = atom_ptr.output_section_index;
1033 shdr.sh_link = elf_file.symtab_section_index.?;1033 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 });
1034 }1037 }
1035}1038}
10361039
src/link/Elf/ZigObject.zig+4-1
...@@ -967,7 +967,10 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {...@@ -967,7 +967,10 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
967 const out_shndx = atom_ptr.output_section_index;967 const out_shndx = atom_ptr.output_section_index;
968 const out_shdr = elf_file.sections.items(.shdr)[out_shndx];968 const out_shdr = elf_file.sections.items(.shdr)[out_shndx];
969 if (out_shdr.sh_type == elf.SHT_NOBITS) continue;969 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];
971 const gpa = elf_file.base.comp.gpa;974 const gpa = elf_file.base.comp.gpa;
972 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });975 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
973 }976 }
src/link/Elf/relocatable.zig+3-5
...@@ -209,7 +209,7 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const...@@ -209,7 +209,7 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
209 for (elf_file.objects.items) |index| {209 for (elf_file.objects.items) |index| {
210 const object = elf_file.file(index).?.object;210 const object = elf_file.file(index).?.object;
211 try object.addAtomsToOutputSections(elf_file);211 try object.addAtomsToOutputSections(elf_file);
212 object.addAtomsToRelaSections(elf_file);212 try object.addAtomsToRelaSections(elf_file);
213 }213 }
214 try elf_file.updateMergeSectionSizes();214 try elf_file.updateMergeSectionSizes();
215 try updateSectionSizes(elf_file);215 try updateSectionSizes(elf_file);
...@@ -349,8 +349,8 @@ fn initComdatGroups(elf_file: *Elf) !void {...@@ -349,8 +349,8 @@ fn initComdatGroups(elf_file: *Elf) !void {
349fn updateSectionSizes(elf_file: *Elf) !void {349fn updateSectionSizes(elf_file: *Elf) !void {
350 const slice = elf_file.sections.slice();350 const slice = elf_file.sections.slice();
351 for (slice.items(.shdr), 0..) |*shdr, shndx| {351 for (slice.items(.shdr), 0..) |*shdr, shndx| {
352 const atom_list = slice.items(.atom_list)[shndx];
352 if (shdr.sh_type != elf.SHT_RELA) {353 if (shdr.sh_type != elf.SHT_RELA) {
353 const atom_list = slice.items(.atom_list)[shndx];
354 for (atom_list.items) |ref| {354 for (atom_list.items) |ref| {
355 const atom_ptr = elf_file.atom(ref) orelse continue;355 const atom_ptr = elf_file.atom(ref) orelse continue;
356 if (!atom_ptr.alive) continue;356 if (!atom_ptr.alive) continue;
...@@ -361,7 +361,6 @@ fn updateSectionSizes(elf_file: *Elf) !void {...@@ -361,7 +361,6 @@ fn updateSectionSizes(elf_file: *Elf) !void {
361 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);361 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);
362 }362 }
363 } else {363 } else {
364 const atom_list = slice.items(.atom_list)[shdr.sh_info];
365 for (atom_list.items) |ref| {364 for (atom_list.items) |ref| {
366 const atom_ptr = elf_file.atom(ref) orelse continue;365 const atom_ptr = elf_file.atom(ref) orelse continue;
367 if (!atom_ptr.alive) continue;366 if (!atom_ptr.alive) continue;
...@@ -492,9 +491,8 @@ fn writeSyntheticSections(elf_file: *Elf) !void {...@@ -492,9 +491,8 @@ fn writeSyntheticSections(elf_file: *Elf) !void {
492 const gpa = elf_file.base.comp.gpa;491 const gpa = elf_file.base.comp.gpa;
493 const slice = elf_file.sections.slice();492 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| {
496 if (shdr.sh_type != elf.SHT_RELA) continue;495 if (shdr.sh_type != elf.SHT_RELA) continue;
497 const atom_list = slice.items(.atom_list)[shdr.sh_info];
498 if (atom_list.items.len == 0) continue;496 if (atom_list.items.len == 0) continue;
499497
500 const num_relocs = math.cast(usize, @divExact(shdr.sh_size, shdr.sh_entsize)) orelse498 const num_relocs = math.cast(usize, @divExact(shdr.sh_size, shdr.sh_entsize)) orelse