From 6b53dc946117110907867ee67425585ae3729750 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 2 Sep 2024 09:08:37 +0200 Subject: [PATCH] elf: actually allocate atoms within each section chunk --- src/link/Elf.zig | 6 +++++- src/link/Elf/Object.zig | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 752d0082c8..b6efa66247 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -3505,8 +3505,12 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void { } for (self.objects.items) |index| { - for (self.file(index).?.object.section_chunks.items) |*chunk| { + const object = self.file(index).?.object; + for (object.section_chunks.items) |*chunk| { chunk.output_section_index = backlinks[chunk.output_section_index]; + for (chunk.atoms.items) |atom_index| { + object.atom(atom_index).?.output_section_index = chunk.output_section_index; + } } } diff --git a/src/link/Elf/Object.zig b/src/link/Elf/Object.zig index 590f7b6762..dd58727202 100644 --- a/src/link/Elf/Object.zig +++ b/src/link/Elf/Object.zig @@ -975,6 +975,12 @@ pub fn allocateAtoms(self: *Object, elf_file: *Elf) !void { shdr.sh_addralign = @max(shdr.sh_addralign, chunk.alignment.toByteUnits().?); // TODO create back and forward links + // TODO if we had a link from Atom to parent Chunk we would not need to update Atom's value or osec index + for (chunk.atoms.items) |atom_index| { + const atom_ptr = self.atom(atom_index).?; + atom_ptr.output_section_index = chunk.output_section_index; + atom_ptr.value += chunk.value; + } } } @@ -1008,7 +1014,7 @@ pub fn writeAtoms(self: *Object, elf_file: *Elf) !void { const atom_ptr = self.atom(atom_index).?; assert(atom_ptr.alive); - const offset = math.cast(usize, atom_ptr.value) orelse return error.Overflow; + const offset = math.cast(usize, atom_ptr.value - chunk.value) orelse return error.Overflow; const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow; log.debug(" * atom({d}) at 0x{x}", .{ atom_index, chunk.offset(elf_file) + offset });