authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-15 19:00:13+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-15 19:00:13+01:00
log6f3bbd5eaa61a93f96245bfec8d9429f4ea9f88e
tree2f2b618f5a59e9752cdadebf463ed778f86d71d5
parent760ce69734e8c6fca02356c53729909ccb5d8ff9

elf: we were writing too many symbols in the symtab


5 files changed, 180 insertions(+), 54 deletions(-)

src/link/Elf/LinkerDefined.zig+33-1
......@@ -48,10 +48,42 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {
4848 }
4949}
5050
51pub fn globals(self: *LinkerDefined) []const Symbol.Index {
51pub fn globals(self: LinkerDefined) []const Symbol.Index {
5252 return self.symbols.items;
5353}
5454
55pub fn updateSymtabSize(self: *LinkerDefined, elf_file: *Elf) !void {
56 for (self.globals()) |global_index| {
57 const global = elf_file.symbol(global_index);
58 const file_ptr = global.file(elf_file) orelse continue;
59 if (file_ptr.index() != self.index) continue;
60 global.flags.output_symtab = true;
61 if (global.isLocal(elf_file)) {
62 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
63 self.output_symtab_ctx.nlocals += 1;
64 } else {
65 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
66 self.output_symtab_ctx.nglobals += 1;
67 }
68 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
69 }
70}
71
72pub fn writeSymtab(self: LinkerDefined, elf_file: *Elf) void {
73 for (self.globals()) |global_index| {
74 const global = elf_file.symbol(global_index);
75 const file_ptr = global.file(elf_file) orelse continue;
76 if (file_ptr.index() != self.index) continue;
77 const idx = global.outputSymtabIndex(elf_file) orelse continue;
78 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
79 elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
80 elf_file.strtab.appendAssumeCapacity(0);
81 const out_sym = &elf_file.symtab.items[idx];
82 out_sym.st_name = st_name;
83 global.setOutputSym(elf_file, out_sym);
84 }
85}
86
5587pub fn asFile(self: *LinkerDefined) File {
5688 return .{ .linker_defined = self };
5789}
src/link/Elf/Object.zig+57
......@@ -741,6 +741,63 @@ pub fn writeAr(self: Object, writer: anytype) !void {
741741 try writer.writeAll(self.data);
742742}
743743
744pub fn updateSymtabSize(self: *Object, elf_file: *Elf) !void {
745 for (self.locals()) |local_index| {
746 const local = elf_file.symbol(local_index);
747 if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
748 const esym = local.elfSym(elf_file);
749 switch (esym.st_type()) {
750 elf.STT_SECTION, elf.STT_NOTYPE => continue,
751 else => {},
752 }
753 local.flags.output_symtab = true;
754 try local.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
755 self.output_symtab_ctx.nlocals += 1;
756 self.output_symtab_ctx.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1;
757 }
758
759 for (self.globals()) |global_index| {
760 const global = elf_file.symbol(global_index);
761 const file_ptr = global.file(elf_file) orelse continue;
762 if (file_ptr.index() != self.index) continue;
763 if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
764 global.flags.output_symtab = true;
765 if (global.isLocal(elf_file)) {
766 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
767 self.output_symtab_ctx.nlocals += 1;
768 } else {
769 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
770 self.output_symtab_ctx.nglobals += 1;
771 }
772 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
773 }
774}
775
776pub fn writeSymtab(self: Object, elf_file: *Elf) void {
777 for (self.locals()) |local_index| {
778 const local = elf_file.symbol(local_index);
779 const idx = local.outputSymtabIndex(elf_file) orelse continue;
780 const out_sym = &elf_file.symtab.items[idx];
781 out_sym.st_name = @intCast(elf_file.strtab.items.len);
782 elf_file.strtab.appendSliceAssumeCapacity(local.name(elf_file));
783 elf_file.strtab.appendAssumeCapacity(0);
784 local.setOutputSym(elf_file, out_sym);
785 }
786
787 for (self.globals()) |global_index| {
788 const global = elf_file.symbol(global_index);
789 const file_ptr = global.file(elf_file) orelse continue;
790 if (file_ptr.index() != self.index) continue;
791 const idx = global.outputSymtabIndex(elf_file) orelse continue;
792 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
793 elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
794 elf_file.strtab.appendAssumeCapacity(0);
795 const out_sym = &elf_file.symtab.items[idx];
796 out_sym.st_name = st_name;
797 global.setOutputSym(elf_file, out_sym);
798 }
799}
800
744801pub fn locals(self: Object) []const Symbol.Index {
745802 if (self.symbols.items.len == 0) return &[0]Symbol.Index{};
746803 const end = self.first_global orelse self.symbols.items.len;
src/link/Elf/SharedObject.zig+28
......@@ -191,6 +191,34 @@ pub fn globals(self: SharedObject) []const Symbol.Index {
191191 return self.symbols.items;
192192}
193193
194pub fn updateSymtabSize(self: *SharedObject, elf_file: *Elf) !void {
195 for (self.globals()) |global_index| {
196 const global = elf_file.symbol(global_index);
197 const file_ptr = global.file(elf_file) orelse continue;
198 if (file_ptr.index() != self.index) continue;
199 if (global.isLocal(elf_file)) continue;
200 global.flags.output_symtab = true;
201 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
202 self.output_symtab_ctx.nglobals += 1;
203 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
204 }
205}
206
207pub fn writeSymtab(self: SharedObject, elf_file: *Elf) void {
208 for (self.globals()) |global_index| {
209 const global = elf_file.symbol(global_index);
210 const file_ptr = global.file(elf_file) orelse continue;
211 if (file_ptr.index() != self.index) continue;
212 const idx = global.outputSymtabIndex(elf_file) orelse continue;
213 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
214 elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
215 elf_file.strtab.appendAssumeCapacity(0);
216 const out_sym = &elf_file.symtab.items[idx];
217 out_sym.st_name = st_name;
218 global.setOutputSym(elf_file, out_sym);
219 }
220}
221
194222pub fn shdrContents(self: SharedObject, index: u16) []const u8 {
195223 const shdr = self.shdrs.items[index];
196224 return self.data[shdr.sh_offset..][0..shdr.sh_size];
src/link/Elf/ZigObject.zig+57
......@@ -528,6 +528,63 @@ pub fn globals(self: ZigObject) []const Symbol.Index {
528528 return self.global_symbols.items;
529529}
530530
531pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {
532 for (self.locals()) |local_index| {
533 const local = elf_file.symbol(local_index);
534 if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
535 const esym = local.elfSym(elf_file);
536 switch (esym.st_type()) {
537 elf.STT_SECTION, elf.STT_NOTYPE => continue,
538 else => {},
539 }
540 local.flags.output_symtab = true;
541 try local.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
542 self.output_symtab_ctx.nlocals += 1;
543 self.output_symtab_ctx.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1;
544 }
545
546 for (self.globals()) |global_index| {
547 const global = elf_file.symbol(global_index);
548 const file_ptr = global.file(elf_file) orelse continue;
549 if (file_ptr.index() != self.index) continue;
550 if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
551 global.flags.output_symtab = true;
552 if (global.isLocal(elf_file)) {
553 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
554 self.output_symtab_ctx.nlocals += 1;
555 } else {
556 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
557 self.output_symtab_ctx.nglobals += 1;
558 }
559 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
560 }
561}
562
563pub fn writeSymtab(self: ZigObject, elf_file: *Elf) void {
564 for (self.locals()) |local_index| {
565 const local = elf_file.symbol(local_index);
566 const idx = local.outputSymtabIndex(elf_file) orelse continue;
567 const out_sym = &elf_file.symtab.items[idx];
568 out_sym.st_name = @intCast(elf_file.strtab.items.len);
569 elf_file.strtab.appendSliceAssumeCapacity(local.name(elf_file));
570 elf_file.strtab.appendAssumeCapacity(0);
571 local.setOutputSym(elf_file, out_sym);
572 }
573
574 for (self.globals()) |global_index| {
575 const global = elf_file.symbol(global_index);
576 const file_ptr = global.file(elf_file) orelse continue;
577 if (file_ptr.index() != self.index) continue;
578 const idx = global.outputSymtabIndex(elf_file) orelse continue;
579 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
580 elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
581 elf_file.strtab.appendAssumeCapacity(0);
582 const out_sym = &elf_file.symtab.items[idx];
583 out_sym.st_name = st_name;
584 global.setOutputSym(elf_file, out_sym);
585 }
586}
587
531588pub fn asFile(self: *ZigObject) File {
532589 return .{ .zig_object = self };
533590}
src/link/Elf/file.zig+5-53
......@@ -128,63 +128,15 @@ pub const File = union(enum) {
128128 }
129129
130130 pub fn updateSymtabSize(file: File, elf_file: *Elf) !void {
131 const output_symtab_ctx = switch (file) {
132 inline else => |x| &x.output_symtab_ctx,
131 return switch (file) {
132 inline else => |x| x.updateSymtabSize(elf_file),
133133 };
134 for (file.locals()) |local_index| {
135 const local = elf_file.symbol(local_index);
136 if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
137 const esym = local.elfSym(elf_file);
138 switch (esym.st_type()) {
139 elf.STT_SECTION, elf.STT_NOTYPE => continue,
140 else => {},
141 }
142 local.flags.output_symtab = true;
143 try local.setOutputSymtabIndex(output_symtab_ctx.nlocals, elf_file);
144 output_symtab_ctx.nlocals += 1;
145 output_symtab_ctx.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1;
146 }
147
148 for (file.globals()) |global_index| {
149 const global = elf_file.symbol(global_index);
150 const file_ptr = global.file(elf_file) orelse continue;
151 if (file_ptr.index() != file.index()) continue;
152 if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
153 global.flags.output_symtab = true;
154 if (global.isLocal(elf_file)) {
155 try global.setOutputSymtabIndex(output_symtab_ctx.nlocals, elf_file);
156 output_symtab_ctx.nlocals += 1;
157 } else {
158 try global.setOutputSymtabIndex(output_symtab_ctx.nglobals, elf_file);
159 output_symtab_ctx.nglobals += 1;
160 }
161 output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
162 }
163134 }
164135
165136 pub fn writeSymtab(file: File, elf_file: *Elf) void {
166 for (file.locals()) |local_index| {
167 const local = elf_file.symbol(local_index);
168 const idx = local.outputSymtabIndex(elf_file) orelse continue;
169 const out_sym = &elf_file.symtab.items[idx];
170 out_sym.st_name = @intCast(elf_file.strtab.items.len);
171 elf_file.strtab.appendSliceAssumeCapacity(local.name(elf_file));
172 elf_file.strtab.appendAssumeCapacity(0);
173 local.setOutputSym(elf_file, out_sym);
174 }
175
176 for (file.globals()) |global_index| {
177 const global = elf_file.symbol(global_index);
178 const file_ptr = global.file(elf_file) orelse continue;
179 if (file_ptr.index() != file.index()) continue;
180 const idx = global.outputSymtabIndex(elf_file) orelse continue;
181 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
182 elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
183 elf_file.strtab.appendAssumeCapacity(0);
184 const out_sym = &elf_file.symtab.items[idx];
185 out_sym.st_name = st_name;
186 global.setOutputSym(elf_file, out_sym);
187 }
137 return switch (file) {
138 inline else => |x| x.writeSymtab(elf_file),
139 };
188140 }
189141
190142 pub fn updateArSymtab(file: File, ar_symtab: *Archive.ArSymtab, elf_file: *Elf) !void {