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 {
27362736 sizes.nglobals += zig_module.output_symtab_size.nglobals;
27372737 }
27382738
2739 if (self.got_section_index) |_| {
2740 self.got.updateSymtabSize(self);
2741 sizes.nlocals += self.got.output_symtab_size.nlocals;
2742 }
2743
27392744 const shdr = &self.sections.items(.shdr)[self.symtab_section_index.?];
27402745 shdr.sh_info = sizes.nlocals + 1;
27412746 self.markDirty(self.symtab_section_index.?, null);
......@@ -2780,6 +2785,11 @@ fn writeSymtab(self: *Elf) !void {
27802785 ctx.iglobal += zig_module.output_symtab_size.nglobals;
27812786 }
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
27832793 const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian();
27842794 switch (self.ptr_width) {
27852795 .p32 => {
src/link/Elf/synthetic_sections.zig+11-47
......@@ -356,52 +356,30 @@ pub const GotSection = struct {
356356 // return num;
357357 // }
358358
359 pub fn calcSymtabSize(got: *GotSection, elf_file: *Elf) !void {
360 got.output_symtab_size.nlocals = @as(u32, @intCast(got.symbols.items.len));
361 for (got.symbols.items) |sym| {
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 }
359 pub fn updateSymtabSize(got: *GotSection, elf_file: *Elf) void {
360 _ = elf_file;
361 got.output_symtab_size.nlocals = @as(u32, @intCast(got.entries.items.len));
377362 }
378363
379364 pub fn writeSymtab(got: GotSection, elf_file: *Elf, ctx: anytype) !void {
380365 const gpa = elf_file.base.allocator;
381
382 var ilocal = ctx.ilocal;
383 for (got.symbols.items) |sym| {
384 const suffix = switch (sym) {
366 for (got.entries.items, ctx.ilocal..) |entry, ilocal| {
367 const suffix = switch (entry.tag) {
368 .tlsld => "$tlsld",
385369 .tlsgd => "$tlsgd",
386370 .got => "$got",
387371 .gottp => "$gottp",
388372 .tlsdesc => "$tlsdesc",
389373 };
390 const symbol = elf_file.getSymbol(sym.getIndex());
391 const name = try std.fmt.allocPrint(gpa, "{s}{s}", .{ symbol.getName(elf_file), suffix });
374 const symbol = elf_file.symbol(entry.symbol_index);
375 const name = try std.fmt.allocPrint(gpa, "{s}{s}", .{ symbol.name(elf_file), suffix });
392376 defer gpa.free(name);
393 const st_name = try ctx.strtab.insert(gpa, name);
394 const st_value = switch (sym) {
395 // .tlsgd => symbol.tlsGdAddress(elf_file),
377 const st_name = try elf_file.strtab.insert(gpa, name);
378 const st_value = switch (entry.tag) {
396379 .got => symbol.gotAddress(elf_file),
397 // .gottp => symbol.gotTpAddress(elf_file),
398 // .tlsdesc => symbol.tlsDescAddress(elf_file),
399380 else => unreachable,
400381 };
401 const st_size: u64 = switch (sym) {
402 .tlsgd, .tlsdesc => 16,
403 .got, .gottp => 8,
404 };
382 const st_size: u64 = entry.len() * elf_file.archPtrWidthBytes();
405383 ctx.symtab[ilocal] = .{
406384 .st_name = st_name,
407385 .st_info = elf.STT_OBJECT,
......@@ -410,21 +388,7 @@ pub const GotSection = struct {
410388 .st_value = st_value,
411389 .st_size = st_size,
412390 };
413 ilocal += 1;
414391 }
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 // }
428392 }
429393
430394 const FormatCtx = struct {