diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2024-09-01 15:13:09 +0200 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2024-09-04 13:34:25 +0200 |
| commit | 45e46f0fb9f9f8a314802c544e92d1ee865119ef (patch) | |
| tree | 6a39cf64542c3e8e9a4f4160ffb61736bf3da8b6 /src | |
| parent | 1ef96f05eb7806d7123919d30f4b672e82a64d75 (diff) | |
| download | zig-45e46f0fb9f9f8a314802c544e92d1ee865119ef.tar.gz zig-45e46f0fb9f9f8a314802c544e92d1ee865119ef.zip | |
elf: allocate atom chunks using allocateChunk mechanics in objects
Diffstat (limited to 'src')
| -rw-r--r-- | src/link/Elf.zig | 54 | ||||
| -rw-r--r-- | src/link/Elf/Object.zig | 41 | ||||
| -rw-r--r-- | src/link/Elf/relocatable.zig | 1 |
3 files changed, 52 insertions, 44 deletions
diff --git a/src/link/Elf.zig b/src/link/Elf.zig index cb5db6dec9..897deb86c5 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -727,7 +727,10 @@ pub fn allocateChunk(self: *Elf, shndx: u32, size: u64, alignment: Atom.Alignmen true; if (expand_section) { const needed_size = res.value + size; - try self.growAllocSection(shndx, needed_size); + if (shdr.sh_flags & elf.SHF_ALLOC != 0) + try self.growAllocSection(shndx, needed_size) + else + try self.growNonAllocSection(shndx, needed_size, @intCast(alignment.toByteUnits().?), true); } return res; @@ -1036,9 +1039,6 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod try self.setHashSections(); try self.setVersionSymtab(); - for (self.objects.items) |index| { - try self.file(index).?.object.addAtomsToOutputSections(self); - } try self.sortInitFini(); try self.updateMergeSectionSizes(); try self.updateSectionSizes(); @@ -3560,29 +3560,29 @@ fn updateSectionSizes(self: *Elf) !void { } const slice = self.sections.slice(); - for (slice.items(.shdr), slice.items(.atom_list)) |*shdr, atom_list| { - if (atom_list.items.len == 0) continue; - if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue; - for (atom_list.items) |ref| { - const atom_ptr = self.atom(ref) orelse continue; - if (!atom_ptr.alive) continue; - const offset = atom_ptr.alignment.forward(shdr.sh_size); - const padding = offset - shdr.sh_size; - atom_ptr.value = @intCast(offset); - shdr.sh_size += padding + atom_ptr.size; - shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1); - } - } - - if (self.requiresThunks()) { - for (slice.items(.shdr), slice.items(.atom_list), 0..) |*shdr, atom_list, shndx| { - if (shdr.sh_flags & elf.SHF_EXECINSTR == 0) continue; - if (atom_list.items.len == 0) continue; - - // Create jump/branch range extenders if needed. - try self.createThunks(shdr, @intCast(shndx)); - } - } + // for (slice.items(.shdr), slice.items(.atom_list)) |*shdr, atom_list| { + // if (atom_list.items.len == 0) continue; + // if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue; + // for (atom_list.items) |ref| { + // const atom_ptr = self.atom(ref) orelse continue; + // if (!atom_ptr.alive) continue; + // const offset = atom_ptr.alignment.forward(shdr.sh_size); + // const padding = offset - shdr.sh_size; + // atom_ptr.value = @intCast(offset); + // shdr.sh_size += padding + atom_ptr.size; + // shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1); + // } + // } + + // if (self.requiresThunks()) { + // for (slice.items(.shdr), slice.items(.atom_list), 0..) |*shdr, atom_list, shndx| { + // if (shdr.sh_flags & elf.SHF_EXECINSTR == 0) continue; + // if (atom_list.items.len == 0) continue; + + // // Create jump/branch range extenders if needed. + // try self.createThunks(shdr, @intCast(shndx)); + // } + // } const shdrs = slice.items(.shdr); if (self.eh_frame_section_index) |index| { diff --git a/src/link/Elf/Object.zig b/src/link/Elf/Object.zig index 3337bfb1a2..26c2f2585b 100644 --- a/src/link/Elf/Object.zig +++ b/src/link/Elf/Object.zig @@ -955,27 +955,26 @@ pub fn initOutputSections(self: *Object, elf_file: *Elf) !void { } pub fn allocateAtoms(self: *Object, elf_file: *Elf) !void { - _ = elf_file; for (self.section_chunks.items) |*chunk| { chunk.updateSize(self); } -} -pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void { - for (self.atoms_indexes.items) |atom_index| { - const atom_ptr = self.atom(atom_index) orelse continue; - if (!atom_ptr.alive) continue; - const shdr = atom_ptr.inputShdr(elf_file); - atom_ptr.output_section_index = elf_file.initOutputSection(.{ - .name = self.getString(shdr.sh_name), - .flags = shdr.sh_flags, - .type = shdr.sh_type, - }) catch unreachable; + for (self.section_chunks.items) |*chunk| { + const alloc_res = try elf_file.allocateChunk(chunk.output_section_index, chunk.size, chunk.alignment); + chunk.value = @intCast(alloc_res.value); - const comp = elf_file.base.comp; - const gpa = comp.gpa; - const atom_list = &elf_file.sections.items(.atom_list)[atom_ptr.output_section_index]; - try atom_list.append(gpa, .{ .index = atom_index, .file = self.index }); + const slice = elf_file.sections.slice(); + const shdr = &slice.items(.shdr)[chunk.output_section_index]; + const last_atom_ref = &slice.items(.last_atom)[chunk.output_section_index]; + + const expand_section = if (elf_file.atom(alloc_res.placement)) |placement_atom| + placement_atom.nextAtom(elf_file) == null + else + true; + if (expand_section) last_atom_ref.* = chunk.lastAtom(self).ref(); + shdr.sh_addralign = @max(shdr.sh_addralign, chunk.alignment.toByteUnits().?); + + // TODO create back and forward links } } @@ -1599,6 +1598,16 @@ const SectionChunk = struct { } } + fn firstAtom(chunk: SectionChunk, object: *Object) *Atom { + assert(chunk.atoms.items.len > 0); + return object.atom(chunk.atoms.items[0]).?; + } + + fn lastAtom(chunk: SectionChunk, object: *Object) *Atom { + assert(chunk.atoms.items.len > 0); + return object.atom(chunk.atoms.items[chunk.atoms.items.len - 1]).?; + } + pub fn format( chunk: SectionChunk, comptime unused_fmt_string: []const u8, diff --git a/src/link/Elf/relocatable.zig b/src/link/Elf/relocatable.zig index 4397bc2ca3..71d28abb32 100644 --- a/src/link/Elf/relocatable.zig +++ b/src/link/Elf/relocatable.zig @@ -208,7 +208,6 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const } for (elf_file.objects.items) |index| { const object = elf_file.file(index).?.object; - try object.addAtomsToOutputSections(elf_file); try object.addAtomsToRelaSections(elf_file); } try elf_file.updateMergeSectionSizes(); |
