authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-23 10:20:53+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-25 10:20:15+02:00
log7173ea1a152e0b546f158cbfec017d81b3665047
tree8f50eefa54b4ffb7ad9838d4299c469a68c09dce
parent95c30fb4864ddde2f8d8da7fc64f6f90baa42325

elf: simplify output section symbol tracking


4 files changed, 27 insertions(+), 52 deletions(-)

src/link/Elf.zig+24-45
......@@ -4013,7 +4013,7 @@ pub fn updateSymtabSize(self: *Elf) !void {
40134013 nlocals += @intCast(self.sections.slice().len);
40144014
40154015 if (self.requiresThunks()) for (self.thunks.items) |*th| {
4016 th.output_symtab_ctx.ilocal = nlocals + 1;
4016 th.output_symtab_ctx.ilocal = nlocals;
40174017 th.calcSymtabSize(self);
40184018 nlocals += th.output_symtab_ctx.nlocals;
40194019 strsize += th.output_symtab_ctx.strsize;
......@@ -4024,8 +4024,8 @@ pub fn updateSymtabSize(self: *Elf) !void {
40244024 const ctx = switch (file_ptr) {
40254025 inline else => |x| &x.output_symtab_ctx,
40264026 };
4027 ctx.ilocal = nlocals + 1;
4028 ctx.iglobal = nglobals + 1;
4027 ctx.ilocal = nlocals;
4028 ctx.iglobal = nglobals;
40294029 try file_ptr.updateSymtabSize(self);
40304030 nlocals += ctx.nlocals;
40314031 nglobals += ctx.nglobals;
......@@ -4033,21 +4033,21 @@ pub fn updateSymtabSize(self: *Elf) !void {
40334033 }
40344034
40354035 if (self.got_section_index) |_| {
4036 self.got.output_symtab_ctx.ilocal = nlocals + 1;
4036 self.got.output_symtab_ctx.ilocal = nlocals;
40374037 self.got.updateSymtabSize(self);
40384038 nlocals += self.got.output_symtab_ctx.nlocals;
40394039 strsize += self.got.output_symtab_ctx.strsize;
40404040 }
40414041
40424042 if (self.plt_section_index) |_| {
4043 self.plt.output_symtab_ctx.ilocal = nlocals + 1;
4043 self.plt.output_symtab_ctx.ilocal = nlocals;
40444044 self.plt.updateSymtabSize(self);
40454045 nlocals += self.plt.output_symtab_ctx.nlocals;
40464046 strsize += self.plt.output_symtab_ctx.strsize;
40474047 }
40484048
40494049 if (self.plt_got_section_index) |_| {
4050 self.plt_got.output_symtab_ctx.ilocal = nlocals + 1;
4050 self.plt_got.output_symtab_ctx.ilocal = nlocals;
40514051 self.plt_got.updateSymtabSize(self);
40524052 nlocals += self.plt_got.output_symtab_ctx.nlocals;
40534053 strsize += self.plt_got.output_symtab_ctx.strsize;
......@@ -4063,14 +4063,14 @@ pub fn updateSymtabSize(self: *Elf) !void {
40634063
40644064 const slice = self.sections.slice();
40654065 const symtab_shdr = &slice.items(.shdr)[self.symtab_section_index.?];
4066 symtab_shdr.sh_info = nlocals + 1;
4066 symtab_shdr.sh_info = nlocals;
40674067 symtab_shdr.sh_link = self.strtab_section_index.?;
40684068
40694069 const sym_size: u64 = switch (self.ptr_width) {
40704070 .p32 => @sizeOf(elf.Elf32_Sym),
40714071 .p64 => @sizeOf(elf.Elf64_Sym),
40724072 };
4073 const needed_size = (nlocals + nglobals + 1) * sym_size;
4073 const needed_size = (nlocals + nglobals) * sym_size;
40744074 symtab_shdr.sh_size = needed_size;
40754075
40764076 const strtab = &slice.items(.shdr)[self.strtab_section_index.?];
......@@ -4243,7 +4243,17 @@ pub fn writeSymtab(self: *Elf) !void {
42434243 const needed_strtab_size = math.cast(usize, strtab_shdr.sh_size - 1) orelse return error.Overflow;
42444244 try self.strtab.ensureUnusedCapacity(gpa, needed_strtab_size);
42454245
4246 self.writeSectionSymbols();
4246 for (slice.items(.shdr), 0..) |shdr, shndx| {
4247 const out_sym = &self.symtab.items[shndx];
4248 out_sym.* = .{
4249 .st_name = 0,
4250 .st_value = shdr.sh_addr,
4251 .st_info = if (shdr.sh_type == elf.SHT_NULL) elf.STT_NOTYPE else elf.STT_SECTION,
4252 .st_shndx = @intCast(shndx),
4253 .st_size = 0,
4254 .st_other = 0,
4255 };
4256 }
42474257
42484258 if (self.requiresThunks()) for (self.thunks.items) |th| {
42494259 th.writeSymtab(self);
......@@ -4309,42 +4319,6 @@ pub fn writeSymtab(self: *Elf) !void {
43094319 try self.base.file.?.pwriteAll(self.strtab.items, strtab_shdr.sh_offset);
43104320}
43114321
4312fn writeSectionSymbols(self: *Elf) void {
4313 const slice = self.sections.slice();
4314 var ilocal: u32 = 1;
4315 for (slice.items(.shdr), 0..) |shdr, shndx| {
4316 const out_sym = &self.symtab.items[ilocal];
4317 out_sym.* = .{
4318 .st_name = 0,
4319 .st_value = shdr.sh_addr,
4320 .st_info = elf.STT_SECTION,
4321 .st_shndx = @intCast(shndx),
4322 .st_size = 0,
4323 .st_other = 0,
4324 };
4325 ilocal += 1;
4326 }
4327
4328 if (self.eh_frame_section_index) |shndx| {
4329 const shdr = slice.items(.shdr)[shndx];
4330 const out_sym = &self.symtab.items[ilocal];
4331 out_sym.* = .{
4332 .st_name = 0,
4333 .st_value = shdr.sh_addr,
4334 .st_info = elf.STT_SECTION,
4335 .st_shndx = @intCast(shndx),
4336 .st_size = 0,
4337 .st_other = 0,
4338 };
4339 ilocal += 1;
4340 }
4341}
4342
4343pub fn sectionSymbolOutputSymtabIndex(self: Elf, shndx: u32) u32 {
4344 _ = self;
4345 return shndx + 1;
4346}
4347
43484322/// Always 4 or 8 depending on whether this is 32-bit ELF or 64-bit ELF.
43494323pub fn ptrWidthBytes(self: Elf) u8 {
43504324 return switch (self.ptr_width) {
......@@ -5535,8 +5509,13 @@ pub const SymbolResolver = struct {
55355509};
55365510
55375511const Section = struct {
5512 /// Section header.
55385513 shdr: elf.Elf64_Shdr,
5514
5515 /// Assigned program header index if any.
55395516 phndx: ?u32 = null,
5517
5518 /// List of atoms contributing to this section.
55405519 atom_list: std.ArrayListUnmanaged(Ref) = .{},
55415520
55425521 /// Index of the last allocated atom in this section.
src/link/Elf/Atom.zig+1-4
......@@ -336,10 +336,7 @@ pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.El
336336 switch (target.type(elf_file)) {
337337 elf.STT_SECTION => {
338338 r_addend += @intCast(target.address(.{}, elf_file));
339 r_sym = if (target.outputShndx(elf_file)) |osec|
340 elf_file.sectionSymbolOutputSymtabIndex(osec)
341 else
342 0;
339 r_sym = target.outputShndx(elf_file) orelse 0;
343340 },
344341 else => {
345342 r_sym = target.outputSymtabIndex(elf_file) orelse 0;
src/link/Elf/eh_frame.zig+1-1
......@@ -425,7 +425,7 @@ fn emitReloc(elf_file: *Elf, rec: anytype, sym: *const Symbol, rel: elf.Elf64_Re
425425 switch (sym.type(elf_file)) {
426426 elf.STT_SECTION => {
427427 r_addend += @intCast(sym.address(.{}, elf_file));
428 r_sym = elf_file.sectionSymbolOutputSymtabIndex(sym.outputShndx(elf_file).?);
428 r_sym = sym.outputShndx(elf_file).?;
429429 },
430430 else => {
431431 r_sym = sym.outputSymtabIndex(elf_file) orelse 0;
src/link/Elf/relocatable.zig+1-2
......@@ -393,8 +393,7 @@ fn updateComdatGroupsSizes(elf_file: *Elf) void {
393393 shdr.sh_link = elf_file.symtab_section_index.?;
394394
395395 const sym = cg.symbol(elf_file);
396 shdr.sh_info = sym.outputSymtabIndex(elf_file) orelse
397 elf_file.sectionSymbolOutputSymtabIndex(sym.outputShndx(elf_file).?);
396 shdr.sh_info = sym.outputSymtabIndex(elf_file) orelse sym.outputShndx(elf_file).?;
398397 }
399398}
400399