authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-25 16:47:43+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-30 10:00:50+02:00
logc575e3daa4cdb39e38cc0b32fc8fe4a917947d34
treed0e01d739543c5783d57e046e301c9179ea5692c
parentfa09276510b03292ace9b8cc72064341530a1940

elf: resolve COMDATs in more parallel-friendly way


7 files changed, 138 insertions(+), 125 deletions(-)

src/link/Elf.zig+27-92
......@@ -215,11 +215,8 @@ merge_subsections: std.ArrayListUnmanaged(MergeSubsection) = .{},
215215/// Table of last atom index in a section and matching atom free list if any.
216216last_atom_and_free_list_table: LastAtomAndFreeListTable = .{},
217217
218comdat_groups_owners: std.ArrayListUnmanaged(ComdatGroupOwner) = .{},
219comdat_groups_table: std.ArrayHashMapUnmanaged(ComdatGroupKey, ComdatGroupOwner.Index, ComdatGroupContext, false) = .{},
220
221218/// Global string table used to provide quick access to global symbol resolvers
222/// such as `resolver` and `comdat_groups_table`.
219/// such as `resolver`.
223220strings: StringTable = .{},
224221
225222first_eflags: ?elf.Elf64_Word = null,
......@@ -506,8 +503,6 @@ pub fn deinit(self: *Elf) void {
506503 }
507504 self.last_atom_and_free_list_table.deinit(gpa);
508505
509 self.comdat_groups_owners.deinit(gpa);
510 self.comdat_groups_table.deinit(gpa);
511506 self.strings.deinit(gpa);
512507
513508 self.got.deinit(gpa);
......@@ -1305,7 +1300,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
13051300 // input Object files.
13061301 // Any qualifing unresolved symbol will be upgraded to an absolute, weak
13071302 // symbol for potential resolution at load-time.
1308 self.resolveSymbols();
1303 try self.resolveSymbols();
13091304 self.markEhFrameAtomsDead();
13101305 try self.resolveMergeSections();
13111306
......@@ -1955,7 +1950,7 @@ fn accessLibPath(
19551950/// 4. Reset state of all resolved globals since we will redo this bit on the pruned set.
19561951/// 5. Remove references to dead objects/shared objects
19571952/// 6. Re-run symbol resolution on pruned objects and shared objects sets.
1958pub fn resolveSymbols(self: *Elf) void {
1953pub fn resolveSymbols(self: *Elf) !void {
19591954 // Resolve symbols in the ZigObject. For now, we assume that it's always live.
19601955 if (self.zigObjectPtr()) |zig_object| zig_object.asFile().resolveSymbols(self);
19611956 // Resolve symbols on the set of all objects and shared objects (even if some are unneeded).
......@@ -1986,32 +1981,17 @@ pub fn resolveSymbols(self: *Elf) void {
19861981 } else i += 1;
19871982 }
19881983
1989 // Dedup comdat groups.
1990 for (self.objects.items) |index| {
1991 const object = self.file(index).?.object;
1992 for (object.comdat_groups.items) |cg| {
1993 const cg_owner = self.comdatGroupOwner(cg.owner);
1994 const owner_file_index = if (self.file(cg_owner.file)) |file_ptr|
1995 file_ptr.object.index
1996 else
1997 std.math.maxInt(File.Index);
1998 cg_owner.file = @min(owner_file_index, index);
1984 {
1985 // Dedup comdat groups.
1986 var table = std.StringHashMap(Ref).init(self.base.comp.gpa);
1987 defer table.deinit();
1988
1989 for (self.objects.items) |index| {
1990 try self.file(index).?.object.resolveComdatGroups(self, &table);
19991991 }
2000 }
20011992
2002 for (self.objects.items) |index| {
2003 const object = self.file(index).?.object;
2004 for (object.comdat_groups.items) |cg| {
2005 const cg_owner = self.comdatGroupOwner(cg.owner);
2006 if (cg_owner.file != index) {
2007 for (cg.comdatGroupMembers(self)) |shndx| {
2008 const atom_index = object.atoms_indexes.items[shndx];
2009 if (object.atom(atom_index)) |atom_ptr| {
2010 atom_ptr.flags.alive = false;
2011 atom_ptr.markFdesDead(self);
2012 }
2013 }
2014 }
1993 for (self.objects.items) |index| {
1994 self.file(index).?.object.markComdatGroupsDead(self);
20151995 }
20161996 }
20171997
......@@ -5632,6 +5612,10 @@ pub fn atom(self: *Elf, ref: Ref) ?*Atom {
56325612 return file_ptr.atom(ref.index);
56335613}
56345614
5615pub fn comdatGroup(self: *Elf, ref: Ref) *ComdatGroup {
5616 return self.file(ref.file).?.comdatGroup(ref.index);
5617}
5618
56355619/// Returns pointer-to-symbol described at sym_index.
56365620pub fn symbol(self: *Elf, sym_index: Symbol.Index) *Symbol {
56375621 return &self.symbols.items[sym_index];
......@@ -5737,31 +5721,6 @@ pub fn zigObjectPtr(self: *Elf) ?*ZigObject {
57375721 return self.file(index).?.zig_object;
57385722}
57395723
5740const GetOrCreateComdatGroupOwnerResult = struct {
5741 found_existing: bool,
5742 index: ComdatGroupOwner.Index,
5743};
5744
5745pub fn getOrCreateComdatGroupOwner(self: *Elf, key: ComdatGroupKey) !GetOrCreateComdatGroupOwnerResult {
5746 const gpa = self.base.comp.gpa;
5747 const gop = try self.comdat_groups_table.getOrPutContext(gpa, key, .{ .elf_file = self });
5748 if (!gop.found_existing) {
5749 const index: ComdatGroupOwner.Index = @intCast(self.comdat_groups_owners.items.len);
5750 const owner = try self.comdat_groups_owners.addOne(gpa);
5751 owner.* = .{};
5752 gop.value_ptr.* = index;
5753 }
5754 return .{
5755 .found_existing = gop.found_existing,
5756 .index = gop.value_ptr.*,
5757 };
5758}
5759
5760pub fn comdatGroupOwner(self: *Elf, index: ComdatGroupOwner.Index) *ComdatGroupOwner {
5761 assert(index < self.comdat_groups_owners.items.len);
5762 return &self.comdat_groups_owners.items[index];
5763}
5764
57655724pub fn addMergeSubsection(self: *Elf) !MergeSubsection.Index {
57665725 const index: MergeSubsection.Index = @intCast(self.merge_subsections.items.len);
57675726 const msec = try self.merge_subsections.addOne(self.base.comp.gpa);
......@@ -6243,48 +6202,24 @@ const default_entry_addr = 0x8000000;
62436202
62446203pub const base_tag: link.File.Tag = .elf;
62456204
6246const ComdatGroupKey = struct {
6247 /// String table offset.
6248 off: u32,
6249
6250 /// File index.
6205pub const ComdatGroup = struct {
6206 signature_off: u32,
62516207 file_index: File.Index,
6208 shndx: u32,
6209 members_start: u32,
6210 members_len: u32,
6211 alive: bool = true,
62526212
6253 pub fn get(key: ComdatGroupKey, elf_file: *Elf) [:0]const u8 {
6254 const file_ptr = elf_file.file(key.file_index).?;
6255 return file_ptr.getString(key.off);
6256 }
6257};
6258
6259const ComdatGroupContext = struct {
6260 elf_file: *Elf,
6261
6262 pub fn eql(ctx: ComdatGroupContext, a: ComdatGroupKey, b: ComdatGroupKey, b_index: usize) bool {
6263 _ = b_index;
6264 const elf_file = ctx.elf_file;
6265 return mem.eql(u8, a.get(elf_file), b.get(elf_file));
6213 pub fn file(cg: ComdatGroup, elf_file: *Elf) File {
6214 return elf_file.file(cg.file_index).?;
62666215 }
62676216
6268 pub fn hash(ctx: ComdatGroupContext, a: ComdatGroupKey) u32 {
6269 return std.array_hash_map.hashString(a.get(ctx.elf_file));
6217 pub fn signature(cg: ComdatGroup, elf_file: *Elf) [:0]const u8 {
6218 return cg.file(elf_file).object.getString(cg.signature_off);
62706219 }
6271};
6272
6273const ComdatGroupOwner = struct {
6274 file: File.Index = 0,
6275
6276 const Index = u32;
6277};
6278
6279pub const ComdatGroup = struct {
6280 owner: ComdatGroupOwner.Index,
6281 file: File.Index,
6282 shndx: u32,
6283 members_start: u32,
6284 members_len: u32,
62856220
62866221 pub fn comdatGroupMembers(cg: ComdatGroup, elf_file: *Elf) []const u32 {
6287 const object = elf_file.file(cg.file).?.object;
6222 const object = cg.file(elf_file).object;
62886223 return object.comdat_group_data.items[cg.members_start..][0..cg.members_len];
62896224 }
62906225
src/link/Elf/Atom.zig+4-1
......@@ -346,7 +346,10 @@ pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.El
346346 r_sym = elf_file.sectionSymbolOutputSymtabIndex(msub.mergeSection(elf_file).output_section_index);
347347 } else {
348348 r_addend += @intCast(target.address(.{}, elf_file));
349 r_sym = elf_file.sectionSymbolOutputSymtabIndex(target.outputShndx().?);
349 r_sym = if (target.outputShndx()) |osec|
350 elf_file.sectionSymbolOutputSymtabIndex(osec)
351 else
352 0;
350353 },
351354 else => {
352355 r_sym = target.outputSymtabIndex(elf_file) orelse 0;
src/link/Elf/Object.zig+58-13
......@@ -216,27 +216,41 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file:
216216 const shndx = @as(u32, @intCast(i));
217217 const group_raw_data = try self.preadShdrContentsAlloc(allocator, handle, shndx);
218218 defer allocator.free(group_raw_data);
219 const group_nmembers = @divExact(group_raw_data.len, @sizeOf(u32));
219 const group_nmembers = math.divExact(usize, group_raw_data.len, @sizeOf(u32)) catch {
220 try elf_file.reportParseError2(
221 self.index,
222 "corrupt section group: not evenly divisible ",
223 .{},
224 );
225 return error.MalformedObject;
226 };
227 if (group_nmembers == 0) {
228 try elf_file.reportParseError2(
229 self.index,
230 "corrupt section group: empty section",
231 .{},
232 );
233 return error.MalformedObject;
234 }
220235 const group_members = @as([*]align(1) const u32, @ptrCast(group_raw_data.ptr))[0..group_nmembers];
221236
222237 if (group_members[0] != elf.GRP_COMDAT) {
223 // TODO convert into an error
224 log.debug("{}: unknown SHT_GROUP format", .{self.fmtPath()});
225 continue;
238 try elf_file.reportParseError2(
239 self.index,
240 "corrupt section group: unknown SHT_GROUP format",
241 .{},
242 );
243 return error.MalformedObject;
226244 }
227245
228246 const group_start = @as(u32, @intCast(self.comdat_group_data.items.len));
229247 try self.comdat_group_data.appendUnalignedSlice(allocator, group_members[1..]);
230248
231 const gop = try elf_file.getOrCreateComdatGroupOwner(.{
232 .off = group_signature,
233 .file_index = self.index,
234 });
235249 const comdat_group_index = try self.addComdatGroup(allocator);
236250 const comdat_group = self.comdatGroup(comdat_group_index);
237251 comdat_group.* = .{
238 .owner = gop.index,
239 .file = self.index,
252 .signature_off = group_signature,
253 .file_index = self.index,
240254 .shndx = shndx,
241255 .members_start = group_start,
242256 .members_len = @intCast(group_nmembers - 1),
......@@ -912,6 +926,37 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void {
912926 }
913927}
914928
929pub fn resolveComdatGroups(self: *Object, elf_file: *Elf, table: anytype) !void {
930 for (self.comdat_groups.items, 0..) |*cg, cgi| {
931 const signature = cg.signature(elf_file);
932 const gop = try table.getOrPut(signature);
933 if (!gop.found_existing) {
934 gop.value_ptr.* = .{ .index = @intCast(cgi), .file = self.index };
935 continue;
936 }
937 const current = elf_file.comdatGroup(gop.value_ptr.*);
938 cg.alive = false;
939 if (self.index < current.file_index) {
940 current.alive = false;
941 cg.alive = true;
942 gop.value_ptr.* = .{ .index = @intCast(cgi), .file = self.index };
943 }
944 }
945}
946
947pub fn markComdatGroupsDead(self: *Object, elf_file: *Elf) void {
948 for (self.comdat_groups.items) |cg| {
949 if (cg.alive) continue;
950 for (cg.comdatGroupMembers(elf_file)) |shndx| {
951 const atom_index = self.atoms_indexes.items[shndx];
952 if (self.atom(atom_index)) |atom_ptr| {
953 atom_ptr.flags.alive = false;
954 atom_ptr.markFdesDead(elf_file);
955 }
956 }
957 }
958}
959
915960pub fn initOutputSections(self: *Object, elf_file: *Elf) !void {
916961 for (self.atoms_indexes.items) |atom_index| {
917962 const atom_ptr = self.atom(atom_index) orelse continue;
......@@ -1428,9 +1473,9 @@ fn formatComdatGroups(
14281473 const elf_file = ctx.elf_file;
14291474 try writer.writeAll(" COMDAT groups\n");
14301475 for (object.comdat_groups.items, 0..) |cg, cg_index| {
1431 const cg_owner = elf_file.comdatGroupOwner(cg.owner);
1432 if (cg_owner.file != object.index) continue;
1433 try writer.print(" COMDAT({d})\n", .{cg_index});
1476 try writer.print(" COMDAT({d})", .{cg_index});
1477 if (!cg.alive) try writer.writeAll(" : [*]");
1478 try writer.writeByte('\n');
14341479 const cg_members = cg.comdatGroupMembers(elf_file);
14351480 for (cg_members) |shndx| {
14361481 const atom_index = object.atoms_indexes.items[shndx];
src/link/Elf/file.zig+7
......@@ -137,6 +137,13 @@ pub const File = union(enum) {
137137 };
138138 }
139139
140 pub fn comdatGroup(file: File, ind: Elf.ComdatGroup.Index) *Elf.ComdatGroup {
141 return switch (file) {
142 .linker_defined, .shared_object, .zig_object => unreachable,
143 .object => |x| x.comdatGroup(ind),
144 };
145 }
146
140147 pub fn symbol(file: File, ind: Symbol.Index) Symbol.Index {
141148 return switch (file) {
142149 .zig_object => |x| x.symbol(ind),
src/link/Elf/relocatable.zig+2-5
......@@ -190,7 +190,7 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
190190 // Now, we are ready to resolve the symbols across all input files.
191191 // We will first resolve the files in the ZigObject, next in the parsed
192192 // input Object files.
193 elf_file.resolveSymbols();
193 try elf_file.resolveSymbols();
194194 elf_file.markEhFrameAtomsDead();
195195 try elf_file.resolveMergeSections();
196196 try elf_file.addCommentString();
......@@ -318,11 +318,8 @@ fn initComdatGroups(elf_file: *Elf) !void {
318318
319319 for (elf_file.objects.items) |index| {
320320 const object = elf_file.file(index).?.object;
321
322321 for (object.comdat_groups.items, 0..) |cg, cg_index| {
323 const cg_owner = elf_file.comdatGroupOwner(cg.owner);
324 if (cg_owner.file != index) continue;
325
322 if (!cg.alive) continue;
326323 const cg_sec = try elf_file.comdat_group_sections.addOne(gpa);
327324 cg_sec.* = .{
328325 .shndx = try elf_file.addSection(.{
src/link/Elf/synthetic_sections.zig+2-8
......@@ -1672,12 +1672,6 @@ pub const ComdatGroupSection = struct {
16721672 shndx: u32,
16731673 cg_ref: Elf.Ref,
16741674
1675 fn ownerFile(cgs: ComdatGroupSection, elf_file: *Elf) ?File {
1676 const cg = cgs.comdatGroup(elf_file);
1677 const cg_owner = elf_file.comdatGroupOwner(cg.owner);
1678 return elf_file.file(cg_owner.file);
1679 }
1680
16811675 fn comdatGroup(cgs: ComdatGroupSection, elf_file: *Elf) *Elf.ComdatGroup {
16821676 const cg_file = elf_file.file(cgs.cg_ref.file).?;
16831677 return cg_file.object.comdatGroup(cgs.cg_ref.index);
......@@ -1685,7 +1679,7 @@ pub const ComdatGroupSection = struct {
16851679
16861680 pub fn symbol(cgs: ComdatGroupSection, elf_file: *Elf) Symbol.Index {
16871681 const cg = cgs.comdatGroup(elf_file);
1688 const object = cgs.ownerFile(elf_file).?.object;
1682 const object = cg.file(elf_file).object;
16891683 const shdr = object.shdrs.items[cg.shndx];
16901684 return object.symbols.items[shdr.sh_info];
16911685 }
......@@ -1698,7 +1692,7 @@ pub const ComdatGroupSection = struct {
16981692
16991693 pub fn write(cgs: ComdatGroupSection, elf_file: *Elf, writer: anytype) !void {
17001694 const cg = cgs.comdatGroup(elf_file);
1701 const object = cgs.ownerFile(elf_file).?.object;
1695 const object = cg.file(elf_file).object;
17021696 const members = cg.comdatGroupMembers(elf_file);
17031697 try writer.writeInt(u32, elf.GRP_COMDAT, .little);
17041698 for (members) |shndx| {
test/link/elf.zig+38-6
......@@ -404,9 +404,7 @@ fn testComdatElimination(b: *Build, opts: Options) *Step {
404404 main_o.linkLibCpp();
405405
406406 {
407 const exe = addExecutable(b, opts, .{
408 .name = "main1",
409 });
407 const exe = addExecutable(b, opts, .{ .name = "main1" });
410408 exe.addObject(a_o);
411409 exe.addObject(main_o);
412410 exe.linkLibCpp();
......@@ -431,9 +429,7 @@ fn testComdatElimination(b: *Build, opts: Options) *Step {
431429 }
432430
433431 {
434 const exe = addExecutable(b, opts, .{
435 .name = "main2",
436 });
432 const exe = addExecutable(b, opts, .{ .name = "main2" });
437433 exe.addObject(main_o);
438434 exe.addObject(a_o);
439435 exe.linkLibCpp();
......@@ -457,6 +453,42 @@ fn testComdatElimination(b: *Build, opts: Options) *Step {
457453 test_step.dependOn(&check.step);
458454 }
459455
456 {
457 const c_o = addObject(b, opts, .{ .name = "c" });
458 c_o.addObject(main_o);
459 c_o.addObject(a_o);
460
461 const exe = addExecutable(b, opts, .{ .name = "main3" });
462 exe.addObject(c_o);
463 exe.linkLibCpp();
464
465 const run = addRunArtifact(exe);
466 run.expectStdOutEqual(
467 \\calling foo in main
468 \\calling foo in main
469 \\
470 );
471 test_step.dependOn(&run.step);
472 }
473
474 {
475 const d_o = addObject(b, opts, .{ .name = "d" });
476 d_o.addObject(a_o);
477 d_o.addObject(main_o);
478
479 const exe = addExecutable(b, opts, .{ .name = "main4" });
480 exe.addObject(d_o);
481 exe.linkLibCpp();
482
483 const run = addRunArtifact(exe);
484 run.expectStdOutEqual(
485 \\calling foo in a
486 \\calling foo in a
487 \\
488 );
489 test_step.dependOn(&run.step);
490 }
491
460492 return test_step;
461493}
462494