authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-01 15:13:09+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-04 13:34:25+02:00
log45e46f0fb9f9f8a314802c544e92d1ee865119ef
tree6a39cf64542c3e8e9a4f4160ffb61736bf3da8b6
parent1ef96f05eb7806d7123919d30f4b672e82a64d75

elf: allocate atom chunks using allocateChunk mechanics in objects


3 files changed, 52 insertions(+), 44 deletions(-)

src/link/Elf.zig+27-27
......@@ -727,7 +727,10 @@ pub fn allocateChunk(self: *Elf, shndx: u32, size: u64, alignment: Atom.Alignmen
727727 true;
728728 if (expand_section) {
729729 const needed_size = res.value + size;
730 try self.growAllocSection(shndx, needed_size);
730 if (shdr.sh_flags & elf.SHF_ALLOC != 0)
731 try self.growAllocSection(shndx, needed_size)
732 else
733 try self.growNonAllocSection(shndx, needed_size, @intCast(alignment.toByteUnits().?), true);
731734 }
732735
733736 return res;
......@@ -1036,9 +1039,6 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
10361039 try self.setHashSections();
10371040 try self.setVersionSymtab();
10381041
1039 for (self.objects.items) |index| {
1040 try self.file(index).?.object.addAtomsToOutputSections(self);
1041 }
10421042 try self.sortInitFini();
10431043 try self.updateMergeSectionSizes();
10441044 try self.updateSectionSizes();
......@@ -3560,29 +3560,29 @@ fn updateSectionSizes(self: *Elf) !void {
35603560 }
35613561
35623562 const slice = self.sections.slice();
3563 for (slice.items(.shdr), slice.items(.atom_list)) |*shdr, atom_list| {
3564 if (atom_list.items.len == 0) continue;
3565 if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;
3566 for (atom_list.items) |ref| {
3567 const atom_ptr = self.atom(ref) orelse continue;
3568 if (!atom_ptr.alive) continue;
3569 const offset = atom_ptr.alignment.forward(shdr.sh_size);
3570 const padding = offset - shdr.sh_size;
3571 atom_ptr.value = @intCast(offset);
3572 shdr.sh_size += padding + atom_ptr.size;
3573 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);
3574 }
3575 }
3576
3577 if (self.requiresThunks()) {
3578 for (slice.items(.shdr), slice.items(.atom_list), 0..) |*shdr, atom_list, shndx| {
3579 if (shdr.sh_flags & elf.SHF_EXECINSTR == 0) continue;
3580 if (atom_list.items.len == 0) continue;
3581
3582 // Create jump/branch range extenders if needed.
3583 try self.createThunks(shdr, @intCast(shndx));
3584 }
3585 }
3563 // for (slice.items(.shdr), slice.items(.atom_list)) |*shdr, atom_list| {
3564 // if (atom_list.items.len == 0) continue;
3565 // if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;
3566 // for (atom_list.items) |ref| {
3567 // const atom_ptr = self.atom(ref) orelse continue;
3568 // if (!atom_ptr.alive) continue;
3569 // const offset = atom_ptr.alignment.forward(shdr.sh_size);
3570 // const padding = offset - shdr.sh_size;
3571 // atom_ptr.value = @intCast(offset);
3572 // shdr.sh_size += padding + atom_ptr.size;
3573 // shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);
3574 // }
3575 // }
3576
3577 // if (self.requiresThunks()) {
3578 // for (slice.items(.shdr), slice.items(.atom_list), 0..) |*shdr, atom_list, shndx| {
3579 // if (shdr.sh_flags & elf.SHF_EXECINSTR == 0) continue;
3580 // if (atom_list.items.len == 0) continue;
3581
3582 // // Create jump/branch range extenders if needed.
3583 // try self.createThunks(shdr, @intCast(shndx));
3584 // }
3585 // }
35863586
35873587 const shdrs = slice.items(.shdr);
35883588 if (self.eh_frame_section_index) |index| {
src/link/Elf/Object.zig+25-16
......@@ -955,27 +955,26 @@ pub fn initOutputSections(self: *Object, elf_file: *Elf) !void {
955955}
956956
957957pub fn allocateAtoms(self: *Object, elf_file: *Elf) !void {
958 _ = elf_file;
959958 for (self.section_chunks.items) |*chunk| {
960959 chunk.updateSize(self);
961960 }
962}
963961
964pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void {
965 for (self.atoms_indexes.items) |atom_index| {
966 const atom_ptr = self.atom(atom_index) orelse continue;
967 if (!atom_ptr.alive) continue;
968 const shdr = atom_ptr.inputShdr(elf_file);
969 atom_ptr.output_section_index = elf_file.initOutputSection(.{
970 .name = self.getString(shdr.sh_name),
971 .flags = shdr.sh_flags,
972 .type = shdr.sh_type,
973 }) catch unreachable;
962 for (self.section_chunks.items) |*chunk| {
963 const alloc_res = try elf_file.allocateChunk(chunk.output_section_index, chunk.size, chunk.alignment);
964 chunk.value = @intCast(alloc_res.value);
974965
975 const comp = elf_file.base.comp;
976 const gpa = comp.gpa;
977 const atom_list = &elf_file.sections.items(.atom_list)[atom_ptr.output_section_index];
978 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
966 const slice = elf_file.sections.slice();
967 const shdr = &slice.items(.shdr)[chunk.output_section_index];
968 const last_atom_ref = &slice.items(.last_atom)[chunk.output_section_index];
969
970 const expand_section = if (elf_file.atom(alloc_res.placement)) |placement_atom|
971 placement_atom.nextAtom(elf_file) == null
972 else
973 true;
974 if (expand_section) last_atom_ref.* = chunk.lastAtom(self).ref();
975 shdr.sh_addralign = @max(shdr.sh_addralign, chunk.alignment.toByteUnits().?);
976
977 // TODO create back and forward links
979978 }
980979}
981980
......@@ -1599,6 +1598,16 @@ const SectionChunk = struct {
15991598 }
16001599 }
16011600
1601 fn firstAtom(chunk: SectionChunk, object: *Object) *Atom {
1602 assert(chunk.atoms.items.len > 0);
1603 return object.atom(chunk.atoms.items[0]).?;
1604 }
1605
1606 fn lastAtom(chunk: SectionChunk, object: *Object) *Atom {
1607 assert(chunk.atoms.items.len > 0);
1608 return object.atom(chunk.atoms.items[chunk.atoms.items.len - 1]).?;
1609 }
1610
16021611 pub fn format(
16031612 chunk: SectionChunk,
16041613 comptime unused_fmt_string: []const u8,
src/link/Elf/relocatable.zig-1
......@@ -208,7 +208,6 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
208208 }
209209 for (elf_file.objects.items) |index| {
210210 const object = elf_file.file(index).?.object;
211 try object.addAtomsToOutputSections(elf_file);
212211 try object.addAtomsToRelaSections(elf_file);
213212 }
214213 try elf_file.updateMergeSectionSizes();