authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-08 22:53:53+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-08 22:53:53+02:00
log465431807b318aba078c911419fd74d44e24a9ae
tree75c057d8d6db0e02e06243c9b20ff727bd789fe9
parent69738a07c2309530463873538a5fd62d6174a26c

elf: write $got symbols into the symtab


2 files changed, 21 insertions(+), 47 deletions(-)

src/link/Elf.zig+10
...@@ -2736,6 +2736,11 @@ fn updateSymtabSize(self: *Elf) !void {...@@ -2736,6 +2736,11 @@ fn updateSymtabSize(self: *Elf) !void {
2736 sizes.nglobals += zig_module.output_symtab_size.nglobals;2736 sizes.nglobals += zig_module.output_symtab_size.nglobals;
2737 }2737 }
27382738
2739 if (self.got_section_index) |_| {
2740 self.got.updateSymtabSize(self);
2741 sizes.nlocals += self.got.output_symtab_size.nlocals;
2742 }
2743
2739 const shdr = &self.sections.items(.shdr)[self.symtab_section_index.?];2744 const shdr = &self.sections.items(.shdr)[self.symtab_section_index.?];
2740 shdr.sh_info = sizes.nlocals + 1;2745 shdr.sh_info = sizes.nlocals + 1;
2741 self.markDirty(self.symtab_section_index.?, null);2746 self.markDirty(self.symtab_section_index.?, null);
...@@ -2780,6 +2785,11 @@ fn writeSymtab(self: *Elf) !void {...@@ -2780,6 +2785,11 @@ fn writeSymtab(self: *Elf) !void {
2780 ctx.iglobal += zig_module.output_symtab_size.nglobals;2785 ctx.iglobal += zig_module.output_symtab_size.nglobals;
2781 }2786 }
27822787
2788 if (self.got_section_index) |_| {
2789 try self.got.writeSymtab(self, ctx);
2790 ctx.ilocal += self.got.output_symtab_size.nlocals;
2791 }
2792
2783 const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian();2793 const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian();
2784 switch (self.ptr_width) {2794 switch (self.ptr_width) {
2785 .p32 => {2795 .p32 => {
src/link/Elf/synthetic_sections.zig+11-47
...@@ -356,52 +356,30 @@ pub const GotSection = struct {...@@ -356,52 +356,30 @@ pub const GotSection = struct {
356 // return num;356 // return num;
357 // }357 // }
358358
359 pub fn calcSymtabSize(got: *GotSection, elf_file: *Elf) !void {359 pub fn updateSymtabSize(got: *GotSection, elf_file: *Elf) void {
360 got.output_symtab_size.nlocals = @as(u32, @intCast(got.symbols.items.len));360 _ = elf_file;
361 for (got.symbols.items) |sym| {361 got.output_symtab_size.nlocals = @as(u32, @intCast(got.entries.items.len));
362 const suffix_len = switch (sym) {
363 .tlsgd => "$tlsgd".len,
364 .got => "$got".len,
365 .gottp => "$gottp".len,
366 .tlsdesc => "$tlsdesc".len,
367 };
368 const symbol = elf_file.getSymbol(sym.getIndex());
369 const name_len = symbol.getName(elf_file).len;
370 got.output_symtab_size.strsize += @as(u32, @intCast(name_len + suffix_len + 1));
371 }
372
373 if (got.emit_tlsld) {
374 got.output_symtab_size.nlocals += 1;
375 got.output_symtab_size.strsize += @as(u32, @intCast("$tlsld".len + 1));
376 }
377 }362 }
378363
379 pub fn writeSymtab(got: GotSection, elf_file: *Elf, ctx: anytype) !void {364 pub fn writeSymtab(got: GotSection, elf_file: *Elf, ctx: anytype) !void {
380 const gpa = elf_file.base.allocator;365 const gpa = elf_file.base.allocator;
381366 for (got.entries.items, ctx.ilocal..) |entry, ilocal| {
382 var ilocal = ctx.ilocal;367 const suffix = switch (entry.tag) {
383 for (got.symbols.items) |sym| {368 .tlsld => "$tlsld",
384 const suffix = switch (sym) {
385 .tlsgd => "$tlsgd",369 .tlsgd => "$tlsgd",
386 .got => "$got",370 .got => "$got",
387 .gottp => "$gottp",371 .gottp => "$gottp",
388 .tlsdesc => "$tlsdesc",372 .tlsdesc => "$tlsdesc",
389 };373 };
390 const symbol = elf_file.getSymbol(sym.getIndex());374 const symbol = elf_file.symbol(entry.symbol_index);
391 const name = try std.fmt.allocPrint(gpa, "{s}{s}", .{ symbol.getName(elf_file), suffix });375 const name = try std.fmt.allocPrint(gpa, "{s}{s}", .{ symbol.name(elf_file), suffix });
392 defer gpa.free(name);376 defer gpa.free(name);
393 const st_name = try ctx.strtab.insert(gpa, name);377 const st_name = try elf_file.strtab.insert(gpa, name);
394 const st_value = switch (sym) {378 const st_value = switch (entry.tag) {
395 // .tlsgd => symbol.tlsGdAddress(elf_file),
396 .got => symbol.gotAddress(elf_file),379 .got => symbol.gotAddress(elf_file),
397 // .gottp => symbol.gotTpAddress(elf_file),
398 // .tlsdesc => symbol.tlsDescAddress(elf_file),
399 else => unreachable,380 else => unreachable,
400 };381 };
401 const st_size: u64 = switch (sym) {382 const st_size: u64 = entry.len() * elf_file.archPtrWidthBytes();
402 .tlsgd, .tlsdesc => 16,
403 .got, .gottp => 8,
404 };
405 ctx.symtab[ilocal] = .{383 ctx.symtab[ilocal] = .{
406 .st_name = st_name,384 .st_name = st_name,
407 .st_info = elf.STT_OBJECT,385 .st_info = elf.STT_OBJECT,
...@@ -410,21 +388,7 @@ pub const GotSection = struct {...@@ -410,21 +388,7 @@ pub const GotSection = struct {
410 .st_value = st_value,388 .st_value = st_value,
411 .st_size = st_size,389 .st_size = st_size,
412 };390 };
413 ilocal += 1;
414 }391 }
415
416 // if (got.emit_tlsld) {
417 // const st_name = try ctx.strtab.insert(gpa, "$tlsld");
418 // ctx.symtab[ilocal] = .{
419 // .st_name = st_name,
420 // .st_info = elf.STT_OBJECT,
421 // .st_other = 0,
422 // .st_shndx = elf_file.got_sect_index.?,
423 // .st_value = elf_file.getTlsLdAddress(),
424 // .st_size = 16,
425 // };
426 // ilocal += 1;
427 // }
428 }392 }
429393
430 const FormatCtx = struct {394 const FormatCtx = struct {