authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-02 23:45:14+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:04+02:00
log14cff77d87c1580e10fce60b6e07022ea9456948
tree63b2d8d109c8a86d183e505f2ee9c972885f3cd6
parent540ef3e010ea00d9bfb3ba8c2e43ebf8fa226084

elf: create atom lists indexed by section index


1 files changed, 25 insertions(+), 1 deletions(-)

src/link/Elf.zig+25-1
......@@ -18,6 +18,9 @@ objects: std.ArrayListUnmanaged(File.Index) = .{},
1818/// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write.
1919/// Same order as in the file.
2020shdrs: std.ArrayListUnmanaged(elf.Elf64_Shdr) = .{},
21/// Given index to a section, returns a list of atoms allocated within it.
22/// Excludes incrementally allocated atoms - for those, use linked-list approach.
23atoms_by_shdr_table: std.AutoArrayHashMapUnmanaged(u16, AtomList) = .{},
2124/// Given index to a section, pulls index of containing phdr if any.
2225phdr_to_shdr_table: std.AutoHashMapUnmanaged(u16, u16) = .{},
2326/// File offset into the shdr table.
......@@ -159,6 +162,7 @@ comdat_groups: std.ArrayListUnmanaged(ComdatGroup) = .{},
159162comdat_groups_owners: std.ArrayListUnmanaged(ComdatGroupOwner) = .{},
160163comdat_groups_table: std.AutoHashMapUnmanaged(u32, ComdatGroupOwner.Index) = .{},
161164
165const AtomList = std.ArrayListUnmanaged(Atom.Index);
162166const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Symbol.Index));
163167const AnonDeclTable = std.AutoHashMapUnmanaged(InternPool.Index, Symbol.Index);
164168const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, LazySymbolMetadata);
......@@ -315,6 +319,12 @@ pub fn deinit(self: *Elf) void {
315319 self.objects.deinit(gpa);
316320
317321 self.shdrs.deinit(gpa);
322
323 for (self.atoms_by_shdr_table.values()) |*list| {
324 list.deinit(gpa);
325 }
326 self.atoms_by_shdr_table.deinit(gpa);
327
318328 self.phdr_to_shdr_table.deinit(gpa);
319329 self.phdrs.deinit(gpa);
320330 self.shstrtab.deinit(gpa);
......@@ -1244,6 +1254,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
12441254 // Generate and emit non-incremental sections.
12451255 try self.initSections();
12461256 try self.sortSections();
1257 try self.addAtomsToSections();
12471258
12481259 // Dump the state for easy debugging.
12491260 // State can be dumped via `--debug-log link_state`.
......@@ -3561,7 +3572,7 @@ fn sortSections(self: *Elf) !void {
35613572 }
35623573
35633574 for (self.objects.items) |index| {
3564 for (self.file(index).?.object.atoms.items) |atom_index| {
3575 for (self.file(index).?.atoms()) |atom_index| {
35653576 const atom_ptr = self.atom(atom_index) orelse continue;
35663577 if (!atom_ptr.flags.alive) continue;
35673578 atom_ptr.output_section_index = backlinks[atom_ptr.output_section_index];
......@@ -3587,6 +3598,19 @@ fn sortSections(self: *Elf) !void {
35873598 }
35883599}
35893600
3601fn addAtomsToSections(self: *Elf) !void {
3602 const gpa = self.base.allocator;
3603 for (self.objects.items) |index| {
3604 for (self.file(index).?.atoms()) |atom_index| {
3605 const atom_ptr = self.atom(atom_index) orelse continue;
3606 if (!atom_ptr.flags.alive) continue;
3607 const gop = try self.atoms_by_shdr_table.getOrPut(gpa, atom_ptr.output_section_index);
3608 if (!gop.found_existing) gop.value_ptr.* = .{};
3609 try gop.value_ptr.append(gpa, atom_index);
3610 }
3611 }
3612}
3613
35903614fn updateSyntheticSectionSizes(self: *Elf) !void {
35913615 if (self.got_section_index) |index| {
35923616 if (self.got.dirty) {