authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-28 11:36:51+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-30 10:00:50+02:00
log801c37209889883ad2d7b9cfb232fd8d9e0ed66c
treed64ffbd0691769032edd96392153039951394caf
parent0701646beb8421464346ce6e8993394d8d2c7d5c

elf: init output merge sections in a separate step


3 files changed, 25 insertions(+), 9 deletions(-)

src/link/Elf.zig+8-2
......@@ -3308,7 +3308,7 @@ pub fn resolveMergeSections(self: *Elf) !void {
33083308 for (self.objects.items) |index| {
33093309 const file_ptr = self.file(index).?;
33103310 if (!file_ptr.isAlive()) continue;
3311 file_ptr.object.initMergeSections(self) catch |err| switch (err) {
3311 file_ptr.object.initInputMergeSections(self) catch |err| switch (err) {
33123312 error.MalformedObject => has_errors = true,
33133313 else => |e| return e,
33143314 };
......@@ -3316,6 +3316,12 @@ pub fn resolveMergeSections(self: *Elf) !void {
33163316
33173317 if (has_errors) return error.FlushFailure;
33183318
3319 for (self.objects.items) |index| {
3320 const file_ptr = self.file(index).?;
3321 if (!file_ptr.isAlive()) continue;
3322 try file_ptr.object.initOutputMergeSections(self);
3323 }
3324
33193325 for (self.objects.items) |index| {
33203326 const file_ptr = self.file(index).?;
33213327 if (!file_ptr.isAlive()) continue;
......@@ -3330,7 +3336,7 @@ pub fn resolveMergeSections(self: *Elf) !void {
33303336
33313337pub fn finalizeMergeSections(self: *Elf) !void {
33323338 for (self.merge_sections.items) |*msec| {
3333 try msec.finalize(self);
3339 try msec.finalize(self.base.comp.gpa);
33343340 }
33353341}
33363342
src/link/Elf/Object.zig+14-3
......@@ -691,7 +691,7 @@ pub fn checkDuplicates(self: *Object, dupes: anytype, elf_file: *Elf) error{OutO
691691 }
692692}
693693
694pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
694pub fn initInputMergeSections(self: *Object, elf_file: *Elf) !void {
695695 const gpa = elf_file.base.comp.gpa;
696696
697697 try self.input_merge_sections.ensureUnusedCapacity(gpa, self.shdrs.items.len);
......@@ -709,8 +709,6 @@ pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
709709 const imsec_idx = try self.addInputMergeSection(gpa);
710710 const imsec = self.inputMergeSection(imsec_idx).?;
711711 self.input_merge_sections_indexes.items[shndx] = imsec_idx;
712
713 imsec.merge_section_index = try elf_file.getOrCreateMergeSection(atom_ptr.name(elf_file), shdr.sh_flags, shdr.sh_type);
714712 imsec.atom_index = atom_index;
715713
716714 const data = try self.codeDecompressAlloc(elf_file, atom_index);
......@@ -769,6 +767,19 @@ pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
769767 }
770768}
771769
770pub fn initOutputMergeSections(self: *Object, elf_file: *Elf) !void {
771 for (self.input_merge_sections_indexes.items) |index| {
772 const imsec = self.inputMergeSection(index) orelse continue;
773 const atom_ptr = self.atom(imsec.atom_index).?;
774 const shdr = atom_ptr.inputShdr(elf_file);
775 imsec.merge_section_index = try elf_file.getOrCreateMergeSection(
776 atom_ptr.name(elf_file),
777 shdr.sh_flags,
778 shdr.sh_type,
779 );
780 }
781}
782
772783pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
773784 const gpa = elf_file.base.comp.gpa;
774785
src/link/Elf/merge_section.zig+3-4
......@@ -60,9 +60,8 @@ pub const MergeSection = struct {
6060
6161 /// Finalizes the merge section and clears hash table.
6262 /// Sorts all owned subsections.
63 pub fn finalize(msec: *MergeSection, elf_file: *Elf) !void {
64 const gpa = elf_file.base.comp.gpa;
65 try msec.finalized_subsections.ensureTotalCapacityPrecise(gpa, msec.subsections.items.len);
63 pub fn finalize(msec: *MergeSection, allocator: Allocator) !void {
64 try msec.finalized_subsections.ensureTotalCapacityPrecise(allocator, msec.subsections.items.len);
6665
6766 var it = msec.table.iterator();
6867 while (it.next()) |entry| {
......@@ -70,7 +69,7 @@ pub const MergeSection = struct {
7069 if (!msub.alive) continue;
7170 msec.finalized_subsections.appendAssumeCapacity(entry.value_ptr.*);
7271 }
73 msec.table.clearAndFree(gpa);
72 msec.table.clearAndFree(allocator);
7473
7574 const sortFn = struct {
7675 pub fn sortFn(ctx: *MergeSection, lhs: MergeSubsection.Index, rhs: MergeSubsection.Index) bool {