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 {...@@ -2774,6 +2774,12 @@ fn updateSymtabSize(self: *Elf) !void {
2774 sizes.nglobals += zig_module.output_symtab_size.nglobals;2774 sizes.nglobals += zig_module.output_symtab_size.nglobals;
2775 }2775 }
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
2777 if (self.got_section_index) |_| {2783 if (self.got_section_index) |_| {
2778 self.got.updateSymtabSize(self);2784 self.got.updateSymtabSize(self);
2779 sizes.nlocals += self.got.output_symtab_size.nlocals;2785 sizes.nlocals += self.got.output_symtab_size.nlocals;
...@@ -2823,6 +2829,12 @@ fn writeSymtab(self: *Elf) !void {...@@ -2823,6 +2829,12 @@ fn writeSymtab(self: *Elf) !void {
2823 ctx.iglobal += zig_module.output_symtab_size.nglobals;2829 ctx.iglobal += zig_module.output_symtab_size.nglobals;
2824 }2830 }
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
2826 if (self.got_section_index) |_| {2838 if (self.got_section_index) |_| {
2827 try self.got.writeSymtab(self, ctx);2839 try self.got.writeSymtab(self, ctx);
2828 ctx.ilocal += self.got.output_symtab_size.nlocals;2840 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) = .{},...@@ -3,7 +3,7 @@ symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
3symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},3symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
4alive: bool = true,4alive: bool = true,
55
6// output_symtab_size: Elf.SymtabSize = .{},6output_symtab_size: Elf.SymtabSize = .{},
77
8pub fn deinit(self: *LinkerDefined, allocator: Allocator) void {8pub fn deinit(self: *LinkerDefined, allocator: Allocator) void {
9 self.symtab.deinit(allocator);9 self.symtab.deinit(allocator);
...@@ -56,33 +56,25 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {...@@ -56,33 +56,25 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {
56// }56// }
57// }57// }
5858
59// pub fn calcSymtabSize(self: *InternalObject, elf_file: *Elf) !void {59pub fn updateSymtabSize(self: *LinkerDefined, elf_file: *Elf) void {
60// if (elf_file.options.strip_all) return;60 for (self.globals()) |global_index| {
6161 const global = elf_file.symbol(global_index);
62// for (self.getGlobals()) |global_index| {62 if (global.file(elf_file)) |file| if (file.index() != self.index) continue;
63// const global = elf_file.getSymbol(global_index);63 global.flags.output_symtab = true;
64// if (global.getFile(elf_file)) |file| if (file.getIndex() != self.index) continue;64 self.output_symtab_size.nlocals += 1;
65// global.flags.output_symtab = true;65 }
66// self.output_symtab_size.nlocals += 1;66}
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;
7567
76// var ilocal = ctx.ilocal;68pub fn writeSymtab(self: *LinkerDefined, elf_file: *Elf, ctx: anytype) void {
77// for (self.getGlobals()) |global_index| {69 var ilocal = ctx.ilocal;
78// const global = elf_file.getSymbol(global_index);70 for (self.globals()) |global_index| {
79// if (global.getFile(elf_file)) |file| if (file.getIndex() != self.index) continue;71 const global = elf_file.symbol(global_index);
80// if (!global.flags.output_symtab) continue;72 if (global.file(elf_file)) |file| if (file.index() != self.index) continue;
81// const st_name = try ctx.strtab.insert(gpa, global.getName(elf_file));73 if (!global.flags.output_symtab) continue;
82// ctx.symtab[ilocal] = global.asElfSym(st_name, elf_file);74 global.setOutputSym(elf_file, &ctx.symtab[ilocal]);
83// ilocal += 1;75 ilocal += 1;
84// }76 }
85// }77}
8678
87pub fn sourceSymbol(self: *LinkerDefined, symbol_index: Symbol.Index) *elf.Elf64_Sym {79pub fn sourceSymbol(self: *LinkerDefined, symbol_index: Symbol.Index) *elf.Elf64_Sym {
88 return &self.symtab.items[symbol_index];80 return &self.symtab.items[symbol_index];