authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-27 07:31:07+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-09 12:27:30-07:00
logbd5fc899dbf134de0bc76b772f99565a6529c75f
tree4373172ceaa333d25674d2c70893854d88405369
parentbae3dbffdf7a0b00f7e6c9100a8fedf62ced4701

elf: do not re-allocate AtomLists unless dirtied


3 files changed, 16 insertions(+), 3 deletions(-)

src/link/Elf.zig+4
......@@ -3586,19 +3586,23 @@ fn updateSectionSizes(self: *Elf) !void {
35863586 const slice = self.sections.slice();
35873587 for (slice.items(.shdr), slice.items(.atom_list_2)) |shdr, *atom_list| {
35883588 if (atom_list.atoms.keys().len == 0) continue;
3589 if (!atom_list.dirty) continue;
35893590 if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;
35903591 atom_list.updateSize(self);
35913592 try atom_list.allocate(self);
3593 atom_list.dirty = false;
35923594 }
35933595
35943596 if (self.requiresThunks()) {
35953597 for (slice.items(.shdr), slice.items(.atom_list_2)) |shdr, *atom_list| {
35963598 if (shdr.sh_flags & elf.SHF_EXECINSTR == 0) continue;
35973599 if (atom_list.atoms.keys().len == 0) continue;
3600 if (!atom_list.dirty) continue;
35983601
35993602 // Create jump/branch range extenders if needed.
36003603 try self.createThunks(atom_list);
36013604 try atom_list.allocate(self);
3605 atom_list.dirty = false;
36023606 }
36033607
36043608 // FIXME:JK this will hopefully not be needed once we create a link from Atom/Thunk to AtomList.
src/link/Elf/AtomList.zig+10-3
......@@ -5,6 +5,8 @@ output_section_index: u32 = 0,
55// atoms: std.ArrayListUnmanaged(Elf.Ref) = .empty,
66atoms: std.AutoArrayHashMapUnmanaged(Elf.Ref, void) = .empty,
77
8dirty: bool = true,
9
810pub fn deinit(list: *AtomList, allocator: Allocator) void {
911 list.atoms.deinit(allocator);
1012}
......@@ -20,9 +22,7 @@ pub fn offset(list: AtomList, elf_file: *Elf) u64 {
2022}
2123
2224pub fn updateSize(list: *AtomList, elf_file: *Elf) void {
23 // TODO perhaps a 'stale' flag would be better here?
24 list.size = 0;
25 list.alignment = .@"1";
25 assert(list.dirty);
2626 for (list.atoms.keys()) |ref| {
2727 const atom_ptr = elf_file.atom(ref).?;
2828 assert(atom_ptr.alive);
......@@ -35,6 +35,8 @@ pub fn updateSize(list: *AtomList, elf_file: *Elf) void {
3535}
3636
3737pub fn allocate(list: *AtomList, elf_file: *Elf) !void {
38 assert(list.dirty);
39
3840 const alloc_res = try elf_file.allocateChunk(.{
3941 .shndx = list.output_section_index,
4042 .size = list.size,
......@@ -43,6 +45,8 @@ pub fn allocate(list: *AtomList, elf_file: *Elf) !void {
4345 });
4446 list.value = @intCast(alloc_res.value);
4547
48 log.debug("allocated atom_list({d}) at 0x{x}", .{ list.output_section_index, list.address(elf_file) });
49
4650 const slice = elf_file.sections.slice();
4751 const shdr = &slice.items(.shdr)[list.output_section_index];
4852 const last_atom_ref = &slice.items(.last_atom)[list.output_section_index];
......@@ -80,12 +84,15 @@ pub fn allocate(list: *AtomList, elf_file: *Elf) !void {
8084 atom_ptr.output_section_index = list.output_section_index;
8185 atom_ptr.value += list.value;
8286 }
87
88 list.dirty = false;
8389}
8490
8591pub fn write(list: AtomList, buffer: *std.ArrayList(u8), undefs: anytype, elf_file: *Elf) !void {
8692 const gpa = elf_file.base.comp.gpa;
8793 const osec = elf_file.sections.items(.shdr)[list.output_section_index];
8894 assert(osec.sh_type != elf.SHT_NOBITS);
95 assert(!list.dirty);
8996
9097 log.debug("writing atoms in section '{s}'", .{elf_file.getShString(osec.sh_name)});
9198
src/link/Elf/relocatable.zig+2
......@@ -336,8 +336,10 @@ fn updateSectionSizes(elf_file: *Elf) !void {
336336 const slice = elf_file.sections.slice();
337337 for (slice.items(.atom_list_2)) |*atom_list| {
338338 if (atom_list.atoms.keys().len == 0) continue;
339 if (!atom_list.dirty) continue;
339340 atom_list.updateSize(elf_file);
340341 try atom_list.allocate(elf_file);
342 atom_list.dirty = false;
341343 }
342344
343345 for (slice.items(.shdr), 0..) |*shdr, shndx| {