authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-24 06:42:43+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-30 10:00:50+02:00
log733d25000bcb50104253d879734c89abde5e33b5
tree5f12232769a4711baefeec7c1e983aa088f00c7e
parentf219286573a7a1edafe3e1b5bc1e521a379ee2e2

elf: move ownership of input merge sections to Object


2 files changed, 35 insertions(+), 33 deletions(-)

src/link/Elf.zig-20
......@@ -215,8 +215,6 @@ merge_sections: std.ArrayListUnmanaged(MergeSection) = .{},
215215/// List of output merge subsections.
216216/// Each subsection is akin to Atom but belongs to a MergeSection.
217217merge_subsections: std.ArrayListUnmanaged(MergeSubsection) = .{},
218/// List of input merge sections as parsed from input relocatables.
219merge_input_sections: std.ArrayListUnmanaged(InputMergeSection) = .{},
220218
221219/// Table of last atom index in a section and matching atom free list if any.
222220last_atom_and_free_list_table: LastAtomAndFreeListTable = .{},
......@@ -390,8 +388,6 @@ pub fn createEmpty(
390388 _ = try self.addSection(.{ .name = "" });
391389 // Append null symbol in output symtab
392390 try self.symtab.append(gpa, null_sym);
393 // Append null input merge section.
394 try self.merge_input_sections.append(gpa, .{});
395391
396392 if (!is_obj_or_ar) {
397393 try self.dynstrtab.append(gpa, 0);
......@@ -515,10 +511,6 @@ pub fn deinit(self: *Elf) void {
515511 }
516512 self.merge_sections.deinit(gpa);
517513 self.merge_subsections.deinit(gpa);
518 for (self.merge_input_sections.items) |*sect| {
519 sect.deinit(gpa);
520 }
521 self.merge_input_sections.deinit(gpa);
522514 for (self.last_atom_and_free_list_table.values()) |*value| {
523515 value.free_list.deinit(gpa);
524516 }
......@@ -5847,18 +5839,6 @@ pub fn comdatGroupOwner(self: *Elf, index: ComdatGroupOwner.Index) *ComdatGroupO
58475839 return &self.comdat_groups_owners.items[index];
58485840}
58495841
5850pub fn addInputMergeSection(self: *Elf) !InputMergeSection.Index {
5851 const index: InputMergeSection.Index = @intCast(self.merge_input_sections.items.len);
5852 const msec = try self.merge_input_sections.addOne(self.base.comp.gpa);
5853 msec.* = .{};
5854 return index;
5855}
5856
5857pub fn inputMergeSection(self: *Elf, index: InputMergeSection.Index) ?*InputMergeSection {
5858 if (index == 0) return null;
5859 return &self.merge_input_sections.items[index];
5860}
5861
58625842pub fn addMergeSubsection(self: *Elf) !MergeSubsection.Index {
58635843 const index: MergeSubsection.Index = @intCast(self.merge_subsections.items.len);
58645844 const msec = try self.merge_subsections.addOne(self.base.comp.gpa);
src/link/Elf/Object.zig+35-13
......@@ -15,7 +15,8 @@ comdat_groups: std.ArrayListUnmanaged(Elf.ComdatGroup.Index) = .{},
1515comdat_group_data: std.ArrayListUnmanaged(u32) = .{},
1616relocs: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{},
1717
18merge_sections: std.ArrayListUnmanaged(InputMergeSection.Index) = .{},
18input_merge_sections: std.ArrayListUnmanaged(InputMergeSection) = .{},
19input_merge_sections_indexes: std.ArrayListUnmanaged(InputMergeSection.Index) = .{},
1920
2021fdes: std.ArrayListUnmanaged(Fde) = .{},
2122cies: std.ArrayListUnmanaged(Cie) = .{},
......@@ -53,7 +54,11 @@ pub fn deinit(self: *Object, allocator: Allocator) void {
5354 self.fdes.deinit(allocator);
5455 self.cies.deinit(allocator);
5556 self.eh_frame_data.deinit(allocator);
56 self.merge_sections.deinit(allocator);
57 for (self.input_merge_sections.items) |*isec| {
58 isec.deinit(allocator);
59 }
60 self.input_merge_sections.deinit(allocator);
61 self.input_merge_sections_indexes.deinit(allocator);
5762}
5863
5964pub fn parse(self: *Object, elf_file: *Elf) !void {
......@@ -62,6 +67,10 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {
6267 const handle = elf_file.fileHandle(self.file_handle);
6368
6469 try self.parseCommon(gpa, handle, elf_file);
70
71 // Append null input merge section
72 try self.input_merge_sections.append(gpa, .{});
73
6574 try self.initAtoms(gpa, handle, elf_file);
6675 try self.initSymtab(gpa, elf_file);
6776
......@@ -664,8 +673,9 @@ pub fn checkDuplicates(self: *Object, dupes: anytype, elf_file: *Elf) error{OutO
664673pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
665674 const gpa = elf_file.base.comp.gpa;
666675
667 try self.merge_sections.resize(gpa, self.shdrs.items.len);
668 @memset(self.merge_sections.items, 0);
676 try self.input_merge_sections.ensureUnusedCapacity(gpa, self.shdrs.items.len);
677 try self.input_merge_sections_indexes.resize(gpa, self.shdrs.items.len);
678 @memset(self.input_merge_sections_indexes.items, 0);
669679
670680 for (self.shdrs.items, 0..) |shdr, shndx| {
671681 if (shdr.sh_flags & elf.SHF_MERGE == 0) continue;
......@@ -675,9 +685,9 @@ pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
675685 if (!atom_ptr.flags.alive) continue;
676686 if (atom_ptr.relocs(elf_file).len > 0) continue;
677687
678 const imsec_idx = try elf_file.addInputMergeSection();
679 const imsec = elf_file.inputMergeSection(imsec_idx).?;
680 self.merge_sections.items[shndx] = imsec_idx;
688 const imsec_idx = try self.addInputMergeSection(gpa);
689 const imsec = self.inputMergeSection(imsec_idx).?;
690 self.input_merge_sections_indexes.items[shndx] = imsec_idx;
681691
682692 imsec.merge_section_index = try elf_file.getOrCreateMergeSection(atom_ptr.name(elf_file), shdr.sh_flags, shdr.sh_type);
683693 imsec.atom_index = atom_index;
......@@ -741,8 +751,8 @@ pub fn initMergeSections(self: *Object, elf_file: *Elf) !void {
741751pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
742752 const gpa = elf_file.base.comp.gpa;
743753
744 for (self.merge_sections.items) |index| {
745 const imsec = elf_file.inputMergeSection(index) orelse continue;
754 for (self.input_merge_sections_indexes.items) |index| {
755 const imsec = self.inputMergeSection(index) orelse continue;
746756 if (imsec.offsets.items.len == 0) continue;
747757 const msec = elf_file.mergeSection(imsec.merge_section_index);
748758 const atom_ptr = elf_file.atom(imsec.atom_index).?;
......@@ -776,8 +786,8 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
776786
777787 if (esym.st_shndx == elf.SHN_COMMON or esym.st_shndx == elf.SHN_UNDEF or esym.st_shndx == elf.SHN_ABS) continue;
778788
779 const imsec_index = self.merge_sections.items[esym.st_shndx];
780 const imsec = elf_file.inputMergeSection(imsec_index) orelse continue;
789 const imsec_index = self.input_merge_sections_indexes.items[esym.st_shndx];
790 const imsec = self.inputMergeSection(imsec_index) orelse continue;
781791 if (imsec.offsets.items.len == 0) continue;
782792 const msub_index, const offset = imsec.findSubsection(@intCast(esym.st_value)) orelse {
783793 var err = try elf_file.base.addErrorWithNotes(2);
......@@ -801,8 +811,8 @@ pub fn resolveMergeSubsections(self: *Object, elf_file: *Elf) !void {
801811 const esym = self.symtab.items[rel.r_sym()];
802812 if (esym.st_type() != elf.STT_SECTION) continue;
803813
804 const imsec_index = self.merge_sections.items[esym.st_shndx];
805 const imsec = elf_file.inputMergeSection(imsec_index) orelse continue;
814 const imsec_index = self.input_merge_sections_indexes.items[esym.st_shndx];
815 const imsec = self.inputMergeSection(imsec_index) orelse continue;
806816 if (imsec.offsets.items.len == 0) continue;
807817 const msub_index, const offset = imsec.findSubsection(@intCast(@as(i64, @intCast(esym.st_value)) + rel.r_addend)) orelse {
808818 var err = try elf_file.base.addErrorWithNotes(1);
......@@ -1184,6 +1194,18 @@ fn preadRelocsAlloc(self: Object, allocator: Allocator, handle: std.fs.File, shn
11841194 return @as([*]align(1) const elf.Elf64_Rela, @ptrCast(raw.ptr))[0..num];
11851195}
11861196
1197fn addInputMergeSection(self: *Object, allocator: Allocator) !InputMergeSection.Index {
1198 const index: InputMergeSection.Index = @intCast(self.input_merge_sections.items.len);
1199 const msec = try self.input_merge_sections.addOne(allocator);
1200 msec.* = .{};
1201 return index;
1202}
1203
1204fn inputMergeSection(self: *Object, index: InputMergeSection.Index) ?*InputMergeSection {
1205 if (index == 0) return null;
1206 return &self.input_merge_sections.items[index];
1207}
1208
11871209pub fn format(
11881210 self: *Object,
11891211 comptime unused_fmt_string: []const u8,