authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-10 09:10:00+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-10 09:10:00+02:00
log1c3fd16c3777d8bf61e681a49fbcff0803dd9609
tree212223f1861749c881c26038ff75e96302e8629e
parenta455b5692a657d01f07f5228a1abcb38f1abcc38

elf: write linker-defined symbols to symtab


2 files changed, 31 insertions(+), 27 deletions(-)

src/link/Elf.zig+12
......@@ -2774,6 +2774,12 @@ fn updateSymtabSize(self: *Elf) !void {
27742774 sizes.nglobals += zig_module.output_symtab_size.nglobals;
27752775 }
27762776
2777 if (self.linker_defined_index) |index| {
2778 const linker_defined = self.file(index).?.linker_defined;
2779 linker_defined.updateSymtabSize(self);
2780 sizes.nlocals += linker_defined.output_symtab_size.nlocals;
2781 }
2782
27772783 if (self.got_section_index) |_| {
27782784 self.got.updateSymtabSize(self);
27792785 sizes.nlocals += self.got.output_symtab_size.nlocals;
......@@ -2823,6 +2829,12 @@ fn writeSymtab(self: *Elf) !void {
28232829 ctx.iglobal += zig_module.output_symtab_size.nglobals;
28242830 }
28252831
2832 if (self.linker_defined_index) |index| {
2833 const linker_defined = self.file(index).?.linker_defined;
2834 linker_defined.writeSymtab(self, ctx);
2835 ctx.ilocal += linker_defined.output_symtab_size.nlocals;
2836 }
2837
28262838 if (self.got_section_index) |_| {
28272839 try self.got.writeSymtab(self, ctx);
28282840 ctx.ilocal += self.got.output_symtab_size.nlocals;
src/link/Elf/LinkerDefined.zig+19-27
......@@ -3,7 +3,7 @@ symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
33symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
44alive: bool = true,
55
6// output_symtab_size: Elf.SymtabSize = .{},
6output_symtab_size: Elf.SymtabSize = .{},
77
88pub fn deinit(self: *LinkerDefined, allocator: Allocator) void {
99 self.symtab.deinit(allocator);
......@@ -56,33 +56,25 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {
5656// }
5757// }
5858
59// pub fn calcSymtabSize(self: *InternalObject, elf_file: *Elf) !void {
60// if (elf_file.options.strip_all) return;
61
62// for (self.getGlobals()) |global_index| {
63// const global = elf_file.getSymbol(global_index);
64// if (global.getFile(elf_file)) |file| if (file.getIndex() != self.index) continue;
65// global.flags.output_symtab = true;
66// self.output_symtab_size.nlocals += 1;
67// self.output_symtab_size.strsize += @as(u32, @intCast(global.getName(elf_file).len + 1));
68// }
69// }
70
71// pub fn writeSymtab(self: *LinkerDefined, elf_file: *Elf, ctx: Elf.WriteSymtabCtx) !void {
72// if (elf_file.options.strip_all) return;
73
74// const gpa = elf_file.base.allocator;
59pub fn updateSymtabSize(self: *LinkerDefined, elf_file: *Elf) void {
60 for (self.globals()) |global_index| {
61 const global = elf_file.symbol(global_index);
62 if (global.file(elf_file)) |file| if (file.index() != self.index) continue;
63 global.flags.output_symtab = true;
64 self.output_symtab_size.nlocals += 1;
65 }
66}
7567
76// var ilocal = ctx.ilocal;
77// for (self.getGlobals()) |global_index| {
78// const global = elf_file.getSymbol(global_index);
79// if (global.getFile(elf_file)) |file| if (file.getIndex() != self.index) continue;
80// if (!global.flags.output_symtab) continue;
81// const st_name = try ctx.strtab.insert(gpa, global.getName(elf_file));
82// ctx.symtab[ilocal] = global.asElfSym(st_name, elf_file);
83// ilocal += 1;
84// }
85// }
68pub fn writeSymtab(self: *LinkerDefined, elf_file: *Elf, ctx: anytype) void {
69 var ilocal = ctx.ilocal;
70 for (self.globals()) |global_index| {
71 const global = elf_file.symbol(global_index);
72 if (global.file(elf_file)) |file| if (file.index() != self.index) continue;
73 if (!global.flags.output_symtab) continue;
74 global.setOutputSym(elf_file, &ctx.symtab[ilocal]);
75 ilocal += 1;
76 }
77}
8678
8779pub fn sourceSymbol(self: *LinkerDefined, symbol_index: Symbol.Index) *elf.Elf64_Sym {
8880 return &self.symtab.items[symbol_index];