authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-24 10:15:09+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-30 10:00:50+02:00
logf1fedb3a51d4d9a37e4568ce1a80149bb5a610f7
tree4e0b3d7182b829eae539738d27e29bf8727cda5c
parent733d25000bcb50104253d879734c89abde5e33b5

elf: move ownership of comdat groups to Object


4 files changed, 51 insertions(+), 37 deletions(-)

src/link/Elf.zig+19-19
......@@ -219,7 +219,6 @@ merge_subsections: std.ArrayListUnmanaged(MergeSubsection) = .{},
219219/// Table of last atom index in a section and matching atom free list if any.
220220last_atom_and_free_list_table: LastAtomAndFreeListTable = .{},
221221
222comdat_groups: std.ArrayListUnmanaged(ComdatGroup) = .{},
223222comdat_groups_owners: std.ArrayListUnmanaged(ComdatGroupOwner) = .{},
224223comdat_groups_table: std.AutoHashMapUnmanaged(u32, ComdatGroupOwner.Index) = .{},
225224
......@@ -516,7 +515,6 @@ pub fn deinit(self: *Elf) void {
516515 }
517516 self.last_atom_and_free_list_table.deinit(gpa);
518517
519 self.comdat_groups.deinit(gpa);
520518 self.comdat_groups_owners.deinit(gpa);
521519 self.comdat_groups_table.deinit(gpa);
522520 self.strings.deinit(gpa);
......@@ -1998,8 +1996,7 @@ pub fn resolveSymbols(self: *Elf) void {
19981996 // Dedup comdat groups.
19991997 for (self.objects.items) |index| {
20001998 const object = self.file(index).?.object;
2001 for (object.comdat_groups.items) |cg_index| {
2002 const cg = self.comdatGroup(cg_index);
1999 for (object.comdat_groups.items) |cg| {
20032000 const cg_owner = self.comdatGroupOwner(cg.owner);
20042001 const owner_file_index = if (self.file(cg_owner.file)) |file_ptr|
20052002 file_ptr.object.index
......@@ -2011,8 +2008,7 @@ pub fn resolveSymbols(self: *Elf) void {
20112008
20122009 for (self.objects.items) |index| {
20132010 const object = self.file(index).?.object;
2014 for (object.comdat_groups.items) |cg_index| {
2015 const cg = self.comdatGroup(cg_index);
2011 for (object.comdat_groups.items) |cg| {
20162012 const cg_owner = self.comdatGroupOwner(cg.owner);
20172013 if (cg_owner.file != index) {
20182014 for (cg.comdatGroupMembers(self)) |shndx| {
......@@ -5822,18 +5818,6 @@ pub fn getOrCreateComdatGroupOwner(self: *Elf, name: [:0]const u8) !GetOrCreateC
58225818 };
58235819}
58245820
5825pub fn addComdatGroup(self: *Elf) !ComdatGroup.Index {
5826 const gpa = self.base.comp.gpa;
5827 const index = @as(ComdatGroup.Index, @intCast(self.comdat_groups.items.len));
5828 _ = try self.comdat_groups.addOne(gpa);
5829 return index;
5830}
5831
5832pub fn comdatGroup(self: *Elf, index: ComdatGroup.Index) *ComdatGroup {
5833 assert(index < self.comdat_groups.items.len);
5834 return &self.comdat_groups.items[index];
5835}
5836
58375821pub fn comdatGroupOwner(self: *Elf, index: ComdatGroupOwner.Index) *ComdatGroupOwner {
58385822 assert(index < self.comdat_groups_owners.items.len);
58395823 return &self.comdat_groups_owners.items[index];
......@@ -6233,7 +6217,7 @@ fn fmtDumpState(
62336217
62346218 try writer.writeAll("Output COMDAT groups\n");
62356219 for (self.comdat_group_sections.items) |cg| {
6236 try writer.print(" shdr({d}) : COMDAT({d})\n", .{ cg.shndx, cg.cg_index });
6220 try writer.print(" shdr({d}) : COMDAT({})\n", .{ cg.shndx, cg.cg_ref });
62376221 }
62386222
62396223 try writer.writeAll("\nOutput merge sections\n");
......@@ -6376,6 +6360,22 @@ pub const SystemLib = struct {
63766360 path: []const u8,
63776361};
63786362
6363pub const Ref = struct {
6364 index: u32,
6365 file: u32,
6366
6367 pub fn format(
6368 ref: Ref,
6369 comptime unused_fmt_string: []const u8,
6370 options: std.fmt.FormatOptions,
6371 writer: anytype,
6372 ) !void {
6373 _ = unused_fmt_string;
6374 _ = options;
6375 try writer.print("ref({},{})", .{ ref.index, ref.file });
6376 }
6377};
6378
63796379const LastAtomAndFreeList = struct {
63806380 /// Index of the last allocated atom in this section.
63816381 last_atom_index: Atom.Index = 0,
src/link/Elf/Object.zig+17-7
......@@ -11,10 +11,11 @@ strtab: std.ArrayListUnmanaged(u8) = .{},
1111first_global: ?Symbol.Index = null,
1212symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
1313atoms: std.ArrayListUnmanaged(Atom.Index) = .{},
14comdat_groups: std.ArrayListUnmanaged(Elf.ComdatGroup.Index) = .{},
15comdat_group_data: std.ArrayListUnmanaged(u32) = .{},
1614relocs: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{},
1715
16comdat_groups: std.ArrayListUnmanaged(Elf.ComdatGroup) = .{},
17comdat_group_data: std.ArrayListUnmanaged(u32) = .{},
18
1819input_merge_sections: std.ArrayListUnmanaged(InputMergeSection) = .{},
1920input_merge_sections_indexes: std.ArrayListUnmanaged(InputMergeSection.Index) = .{},
2021
......@@ -218,8 +219,8 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file:
218219 try self.comdat_group_data.appendUnalignedSlice(allocator, group_members[1..]);
219220
220221 const gop = try elf_file.getOrCreateComdatGroupOwner(group_signature);
221 const comdat_group_index = try elf_file.addComdatGroup();
222 const comdat_group = elf_file.comdatGroup(comdat_group_index);
222 const comdat_group_index = try self.addComdatGroup(allocator);
223 const comdat_group = self.comdatGroup(comdat_group_index);
223224 comdat_group.* = .{
224225 .owner = gop.index,
225226 .file = self.index,
......@@ -227,7 +228,6 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file:
227228 .members_start = group_start,
228229 .members_len = @intCast(group_nmembers - 1),
229230 };
230 try self.comdat_groups.append(allocator, comdat_group_index);
231231 },
232232
233233 elf.SHT_SYMTAB_SHNDX => @panic("TODO SHT_SYMTAB_SHNDX"),
......@@ -1206,6 +1206,17 @@ fn inputMergeSection(self: *Object, index: InputMergeSection.Index) ?*InputMerge
12061206 return &self.input_merge_sections.items[index];
12071207}
12081208
1209fn addComdatGroup(self: *Object, allocator: Allocator) !Elf.ComdatGroup.Index {
1210 const index = @as(Elf.ComdatGroup.Index, @intCast(self.comdat_groups.items.len));
1211 _ = try self.comdat_groups.addOne(allocator);
1212 return index;
1213}
1214
1215pub fn comdatGroup(self: *Object, index: Elf.ComdatGroup.Index) *Elf.ComdatGroup {
1216 assert(index < self.comdat_groups.items.len);
1217 return &self.comdat_groups.items[index];
1218}
1219
12091220pub fn format(
12101221 self: *Object,
12111222 comptime unused_fmt_string: []const u8,
......@@ -1337,8 +1348,7 @@ fn formatComdatGroups(
13371348 const object = ctx.object;
13381349 const elf_file = ctx.elf_file;
13391350 try writer.writeAll(" COMDAT groups\n");
1340 for (object.comdat_groups.items) |cg_index| {
1341 const cg = elf_file.comdatGroup(cg_index);
1351 for (object.comdat_groups.items, 0..) |cg, cg_index| {
13421352 const cg_owner = elf_file.comdatGroupOwner(cg.owner);
13431353 if (cg_owner.file != object.index) continue;
13441354 try writer.print(" COMDAT({d})\n", .{cg_index});
src/link/Elf/relocatable.zig+2-3
......@@ -319,8 +319,7 @@ fn initComdatGroups(elf_file: *Elf) !void {
319319 for (elf_file.objects.items) |index| {
320320 const object = elf_file.file(index).?.object;
321321
322 for (object.comdat_groups.items) |cg_index| {
323 const cg = elf_file.comdatGroup(cg_index);
322 for (object.comdat_groups.items, 0..) |cg, cg_index| {
324323 const cg_owner = elf_file.comdatGroupOwner(cg.owner);
325324 if (cg_owner.file != index) continue;
326325
......@@ -333,7 +332,7 @@ fn initComdatGroups(elf_file: *Elf) !void {
333332 .addralign = @alignOf(u32),
334333 .offset = std.math.maxInt(u64),
335334 }),
336 .cg_index = cg_index,
335 .cg_ref = .{ .index = @intCast(cg_index), .file = index },
337336 };
338337 }
339338 }
src/link/Elf/synthetic_sections.zig+13-8
......@@ -1670,30 +1670,35 @@ pub const VerneedSection = struct {
16701670
16711671pub const ComdatGroupSection = struct {
16721672 shndx: u32,
1673 cg_index: u32,
1673 cg_ref: Elf.Ref,
16741674
1675 fn file(cgs: ComdatGroupSection, elf_file: *Elf) ?File {
1676 const cg = elf_file.comdatGroup(cgs.cg_index);
1675 fn ownerFile(cgs: ComdatGroupSection, elf_file: *Elf) ?File {
1676 const cg = cgs.comdatGroup(elf_file);
16771677 const cg_owner = elf_file.comdatGroupOwner(cg.owner);
16781678 return elf_file.file(cg_owner.file);
16791679 }
16801680
1681 fn comdatGroup(cgs: ComdatGroupSection, elf_file: *Elf) *Elf.ComdatGroup {
1682 const cg_file = elf_file.file(cgs.cg_ref.file).?;
1683 return cg_file.object.comdatGroup(cgs.cg_ref.index);
1684 }
1685
16811686 pub fn symbol(cgs: ComdatGroupSection, elf_file: *Elf) Symbol.Index {
1682 const cg = elf_file.comdatGroup(cgs.cg_index);
1683 const object = cgs.file(elf_file).?.object;
1687 const cg = cgs.comdatGroup(elf_file);
1688 const object = cgs.ownerFile(elf_file).?.object;
16841689 const shdr = object.shdrs.items[cg.shndx];
16851690 return object.symbols.items[shdr.sh_info];
16861691 }
16871692
16881693 pub fn size(cgs: ComdatGroupSection, elf_file: *Elf) usize {
1689 const cg = elf_file.comdatGroup(cgs.cg_index);
1694 const cg = cgs.comdatGroup(elf_file);
16901695 const members = cg.comdatGroupMembers(elf_file);
16911696 return (members.len + 1) * @sizeOf(u32);
16921697 }
16931698
16941699 pub fn write(cgs: ComdatGroupSection, elf_file: *Elf, writer: anytype) !void {
1695 const cg = elf_file.comdatGroup(cgs.cg_index);
1696 const object = cgs.file(elf_file).?.object;
1700 const cg = cgs.comdatGroup(elf_file);
1701 const object = cgs.ownerFile(elf_file).?.object;
16971702 const members = cg.comdatGroupMembers(elf_file);
16981703 try writer.writeInt(u32, elf.GRP_COMDAT, .little);
16991704 for (members) |shndx| {