authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-24 22:06:12+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-30 10:00:50+02:00
log494ae149e0a52c4afd71f6741379d4a7a9afe3f3
tree96d2ca5cd12b65bfd4c2811b510e80d69d1eb0b2
parent669f28594393e90e4d1aacd0d28f67ebe015b922

elf: skip storing comdat group signature globally


3 files changed, 42 insertions(+), 7 deletions(-)

src/link/Elf.zig+30-4
...@@ -216,7 +216,7 @@ merge_subsections: std.ArrayListUnmanaged(MergeSubsection) = .{},...@@ -216,7 +216,7 @@ merge_subsections: std.ArrayListUnmanaged(MergeSubsection) = .{},
216last_atom_and_free_list_table: LastAtomAndFreeListTable = .{},216last_atom_and_free_list_table: LastAtomAndFreeListTable = .{},
217217
218comdat_groups_owners: std.ArrayListUnmanaged(ComdatGroupOwner) = .{},218comdat_groups_owners: std.ArrayListUnmanaged(ComdatGroupOwner) = .{},
219comdat_groups_table: std.AutoHashMapUnmanaged(u32, ComdatGroupOwner.Index) = .{},219comdat_groups_table: std.ArrayHashMapUnmanaged(ComdatGroupKey, ComdatGroupOwner.Index, ComdatGroupContext, false) = .{},
220220
221/// Global string table used to provide quick access to global symbol resolvers221/// Global string table used to provide quick access to global symbol resolvers
222/// such as `resolver` and `comdat_groups_table`.222/// such as `resolver` and `comdat_groups_table`.
...@@ -5742,10 +5742,9 @@ const GetOrCreateComdatGroupOwnerResult = struct {...@@ -5742,10 +5742,9 @@ const GetOrCreateComdatGroupOwnerResult = struct {
5742 index: ComdatGroupOwner.Index,5742 index: ComdatGroupOwner.Index,
5743};5743};
57445744
5745pub fn getOrCreateComdatGroupOwner(self: *Elf, name: [:0]const u8) !GetOrCreateComdatGroupOwnerResult {5745pub fn getOrCreateComdatGroupOwner(self: *Elf, key: ComdatGroupKey) !GetOrCreateComdatGroupOwnerResult {
5746 const gpa = self.base.comp.gpa;5746 const gpa = self.base.comp.gpa;
5747 const off = try self.strings.insert(gpa, name);5747 const gop = try self.comdat_groups_table.getOrPutContext(gpa, key, .{ .elf_file = self });
5748 const gop = try self.comdat_groups_table.getOrPut(gpa, off);
5749 if (!gop.found_existing) {5748 if (!gop.found_existing) {
5750 const index: ComdatGroupOwner.Index = @intCast(self.comdat_groups_owners.items.len);5749 const index: ComdatGroupOwner.Index = @intCast(self.comdat_groups_owners.items.len);
5751 const owner = try self.comdat_groups_owners.addOne(gpa);5750 const owner = try self.comdat_groups_owners.addOne(gpa);
...@@ -6244,6 +6243,33 @@ const default_entry_addr = 0x8000000;...@@ -6244,6 +6243,33 @@ const default_entry_addr = 0x8000000;
62446243
6245pub const base_tag: link.File.Tag = .elf;6244pub const base_tag: link.File.Tag = .elf;
62466245
6246const ComdatGroupKey = struct {
6247 /// String table offset.
6248 off: u32,
6249
6250 /// File index.
6251 file_index: File.Index,
6252
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));
6266 }
6267
6268 pub fn hash(ctx: ComdatGroupContext, a: ComdatGroupKey) u32 {
6269 return std.array_hash_map.hashString(a.get(ctx.elf_file));
6270 }
6271};
6272
6247const ComdatGroupOwner = struct {6273const ComdatGroupOwner = struct {
6248 file: File.Index = 0,6274 file: File.Index = 0,
62496275
src/link/Elf/Object.zig+6-3
...@@ -208,9 +208,9 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file:...@@ -208,9 +208,9 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file:
208 const group_signature = blk: {208 const group_signature = blk: {
209 if (group_info_sym.st_name == 0 and group_info_sym.st_type() == elf.STT_SECTION) {209 if (group_info_sym.st_name == 0 and group_info_sym.st_type() == elf.STT_SECTION) {
210 const sym_shdr = shdrs[group_info_sym.st_shndx];210 const sym_shdr = shdrs[group_info_sym.st_shndx];
211 break :blk self.getString(sym_shdr.sh_name);211 break :blk sym_shdr.sh_name;
212 }212 }
213 break :blk self.getString(group_info_sym.st_name);213 break :blk group_info_sym.st_name;
214 };214 };
215215
216 const shndx = @as(u32, @intCast(i));216 const shndx = @as(u32, @intCast(i));
...@@ -228,7 +228,10 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file:...@@ -228,7 +228,10 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file:
228 const group_start = @as(u32, @intCast(self.comdat_group_data.items.len));228 const group_start = @as(u32, @intCast(self.comdat_group_data.items.len));
229 try self.comdat_group_data.appendUnalignedSlice(allocator, group_members[1..]);229 try self.comdat_group_data.appendUnalignedSlice(allocator, group_members[1..]);
230230
231 const gop = try elf_file.getOrCreateComdatGroupOwner(group_signature);231 const gop = try elf_file.getOrCreateComdatGroupOwner(.{
232 .off = group_signature,
233 .file_index = self.index,
234 });
232 const comdat_group_index = try self.addComdatGroup(allocator);235 const comdat_group_index = try self.addComdatGroup(allocator);
233 const comdat_group = self.comdatGroup(comdat_group_index);236 const comdat_group = self.comdatGroup(comdat_group_index);
234 comdat_group.* = .{237 comdat_group.* = .{
src/link/Elf/file.zig+6
...@@ -157,6 +157,12 @@ pub const File = union(enum) {...@@ -157,6 +157,12 @@ pub const File = union(enum) {
157 };157 };
158 }158 }
159159
160 pub fn getString(file: File, off: u32) [:0]const u8 {
161 return switch (file) {
162 inline else => |x| x.getString(off),
163 };
164 }
165
160 pub fn updateSymtabSize(file: File, elf_file: *Elf) !void {166 pub fn updateSymtabSize(file: File, elf_file: *Elf) !void {
161 return switch (file) {167 return switch (file) {
162 inline else => |x| x.updateSymtabSize(elf_file),168 inline else => |x| x.updateSymtabSize(elf_file),