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...@@ -727,7 +727,10 @@ pub fn allocateChunk(self: *Elf, shndx: u32, size: u64, alignment: Atom.Alignmen
727 true;727 true;
728 if (expand_section) {728 if (expand_section) {
729 const needed_size = res.value + size;729 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);
731 }734 }
732735
733 return res;736 return res;
...@@ -1036,9 +1039,6 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod...@@ -1036,9 +1039,6 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
1036 try self.setHashSections();1039 try self.setHashSections();
1037 try self.setVersionSymtab();1040 try self.setVersionSymtab();
10381041
1039 for (self.objects.items) |index| {
1040 try self.file(index).?.object.addAtomsToOutputSections(self);
1041 }
1042 try self.sortInitFini();1042 try self.sortInitFini();
1043 try self.updateMergeSectionSizes();1043 try self.updateMergeSectionSizes();
1044 try self.updateSectionSizes();1044 try self.updateSectionSizes();
...@@ -3560,29 +3560,29 @@ fn updateSectionSizes(self: *Elf) !void {...@@ -3560,29 +3560,29 @@ fn updateSectionSizes(self: *Elf) !void {
3560 }3560 }
35613561
3562 const slice = self.sections.slice();3562 const slice = self.sections.slice();
3563 for (slice.items(.shdr), slice.items(.atom_list)) |*shdr, atom_list| {3563 // for (slice.items(.shdr), slice.items(.atom_list)) |*shdr, atom_list| {
3564 if (atom_list.items.len == 0) continue;3564 // if (atom_list.items.len == 0) continue;
3565 if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;3565 // if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;
3566 for (atom_list.items) |ref| {3566 // for (atom_list.items) |ref| {
3567 const atom_ptr = self.atom(ref) orelse continue;3567 // const atom_ptr = self.atom(ref) orelse continue;
3568 if (!atom_ptr.alive) continue;3568 // if (!atom_ptr.alive) continue;
3569 const offset = atom_ptr.alignment.forward(shdr.sh_size);3569 // const offset = atom_ptr.alignment.forward(shdr.sh_size);
3570 const padding = offset - shdr.sh_size;3570 // const padding = offset - shdr.sh_size;
3571 atom_ptr.value = @intCast(offset);3571 // atom_ptr.value = @intCast(offset);
3572 shdr.sh_size += padding + atom_ptr.size;3572 // shdr.sh_size += padding + atom_ptr.size;
3573 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);3573 // shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);
3574 }3574 // }
3575 }3575 // }
35763576
3577 if (self.requiresThunks()) {3577 // if (self.requiresThunks()) {
3578 for (slice.items(.shdr), slice.items(.atom_list), 0..) |*shdr, atom_list, shndx| {3578 // for (slice.items(.shdr), slice.items(.atom_list), 0..) |*shdr, atom_list, shndx| {
3579 if (shdr.sh_flags & elf.SHF_EXECINSTR == 0) continue;3579 // if (shdr.sh_flags & elf.SHF_EXECINSTR == 0) continue;
3580 if (atom_list.items.len == 0) continue;3580 // if (atom_list.items.len == 0) continue;
35813581
3582 // Create jump/branch range extenders if needed.3582 // // Create jump/branch range extenders if needed.
3583 try self.createThunks(shdr, @intCast(shndx));3583 // try self.createThunks(shdr, @intCast(shndx));
3584 }3584 // }
3585 }3585 // }
35863586
3587 const shdrs = slice.items(.shdr);3587 const shdrs = slice.items(.shdr);
3588 if (self.eh_frame_section_index) |index| {3588 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 {...@@ -955,27 +955,26 @@ pub fn initOutputSections(self: *Object, elf_file: *Elf) !void {
955}955}
956956
957pub fn allocateAtoms(self: *Object, elf_file: *Elf) !void {957pub fn allocateAtoms(self: *Object, elf_file: *Elf) !void {
958 _ = elf_file;
959 for (self.section_chunks.items) |*chunk| {958 for (self.section_chunks.items) |*chunk| {
960 chunk.updateSize(self);959 chunk.updateSize(self);
961 }960 }
962}
963961
964pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void {962 for (self.section_chunks.items) |*chunk| {
965 for (self.atoms_indexes.items) |atom_index| {963 const alloc_res = try elf_file.allocateChunk(chunk.output_section_index, chunk.size, chunk.alignment);
966 const atom_ptr = self.atom(atom_index) orelse continue;964 chunk.value = @intCast(alloc_res.value);
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;
974965
975 const comp = elf_file.base.comp;966 const slice = elf_file.sections.slice();
976 const gpa = comp.gpa;967 const shdr = &slice.items(.shdr)[chunk.output_section_index];
977 const atom_list = &elf_file.sections.items(.atom_list)[atom_ptr.output_section_index];968 const last_atom_ref = &slice.items(.last_atom)[chunk.output_section_index];
978 try atom_list.append(gpa, .{ .index = atom_index, .file = self.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
979 }978 }
980}979}
981980
...@@ -1599,6 +1598,16 @@ const SectionChunk = struct {...@@ -1599,6 +1598,16 @@ const SectionChunk = struct {
1599 }1598 }
1600 }1599 }
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
1602 pub fn format(1611 pub fn format(
1603 chunk: SectionChunk,1612 chunk: SectionChunk,
1604 comptime unused_fmt_string: []const u8,1613 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...@@ -208,7 +208,6 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
208 }208 }
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);
212 try object.addAtomsToRelaSections(elf_file);211 try object.addAtomsToRelaSections(elf_file);
213 }212 }
214 try elf_file.updateMergeSectionSizes();213 try elf_file.updateMergeSectionSizes();