mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 12:59:04 +00:00
elf: simplify output section symbol tracking
This commit is contained in:
parent
95c30fb486
commit
7173ea1a15
@ -4013,7 +4013,7 @@ pub fn updateSymtabSize(self: *Elf) !void {
|
||||
nlocals += @intCast(self.sections.slice().len);
|
||||
|
||||
if (self.requiresThunks()) for (self.thunks.items) |*th| {
|
||||
th.output_symtab_ctx.ilocal = nlocals + 1;
|
||||
th.output_symtab_ctx.ilocal = nlocals;
|
||||
th.calcSymtabSize(self);
|
||||
nlocals += th.output_symtab_ctx.nlocals;
|
||||
strsize += th.output_symtab_ctx.strsize;
|
||||
@ -4024,8 +4024,8 @@ pub fn updateSymtabSize(self: *Elf) !void {
|
||||
const ctx = switch (file_ptr) {
|
||||
inline else => |x| &x.output_symtab_ctx,
|
||||
};
|
||||
ctx.ilocal = nlocals + 1;
|
||||
ctx.iglobal = nglobals + 1;
|
||||
ctx.ilocal = nlocals;
|
||||
ctx.iglobal = nglobals;
|
||||
try file_ptr.updateSymtabSize(self);
|
||||
nlocals += ctx.nlocals;
|
||||
nglobals += ctx.nglobals;
|
||||
@ -4033,21 +4033,21 @@ pub fn updateSymtabSize(self: *Elf) !void {
|
||||
}
|
||||
|
||||
if (self.got_section_index) |_| {
|
||||
self.got.output_symtab_ctx.ilocal = nlocals + 1;
|
||||
self.got.output_symtab_ctx.ilocal = nlocals;
|
||||
self.got.updateSymtabSize(self);
|
||||
nlocals += self.got.output_symtab_ctx.nlocals;
|
||||
strsize += self.got.output_symtab_ctx.strsize;
|
||||
}
|
||||
|
||||
if (self.plt_section_index) |_| {
|
||||
self.plt.output_symtab_ctx.ilocal = nlocals + 1;
|
||||
self.plt.output_symtab_ctx.ilocal = nlocals;
|
||||
self.plt.updateSymtabSize(self);
|
||||
nlocals += self.plt.output_symtab_ctx.nlocals;
|
||||
strsize += self.plt.output_symtab_ctx.strsize;
|
||||
}
|
||||
|
||||
if (self.plt_got_section_index) |_| {
|
||||
self.plt_got.output_symtab_ctx.ilocal = nlocals + 1;
|
||||
self.plt_got.output_symtab_ctx.ilocal = nlocals;
|
||||
self.plt_got.updateSymtabSize(self);
|
||||
nlocals += self.plt_got.output_symtab_ctx.nlocals;
|
||||
strsize += self.plt_got.output_symtab_ctx.strsize;
|
||||
@ -4063,14 +4063,14 @@ pub fn updateSymtabSize(self: *Elf) !void {
|
||||
|
||||
const slice = self.sections.slice();
|
||||
const symtab_shdr = &slice.items(.shdr)[self.symtab_section_index.?];
|
||||
symtab_shdr.sh_info = nlocals + 1;
|
||||
symtab_shdr.sh_info = nlocals;
|
||||
symtab_shdr.sh_link = self.strtab_section_index.?;
|
||||
|
||||
const sym_size: u64 = switch (self.ptr_width) {
|
||||
.p32 => @sizeOf(elf.Elf32_Sym),
|
||||
.p64 => @sizeOf(elf.Elf64_Sym),
|
||||
};
|
||||
const needed_size = (nlocals + nglobals + 1) * sym_size;
|
||||
const needed_size = (nlocals + nglobals) * sym_size;
|
||||
symtab_shdr.sh_size = needed_size;
|
||||
|
||||
const strtab = &slice.items(.shdr)[self.strtab_section_index.?];
|
||||
@ -4243,7 +4243,17 @@ pub fn writeSymtab(self: *Elf) !void {
|
||||
const needed_strtab_size = math.cast(usize, strtab_shdr.sh_size - 1) orelse return error.Overflow;
|
||||
try self.strtab.ensureUnusedCapacity(gpa, needed_strtab_size);
|
||||
|
||||
self.writeSectionSymbols();
|
||||
for (slice.items(.shdr), 0..) |shdr, shndx| {
|
||||
const out_sym = &self.symtab.items[shndx];
|
||||
out_sym.* = .{
|
||||
.st_name = 0,
|
||||
.st_value = shdr.sh_addr,
|
||||
.st_info = if (shdr.sh_type == elf.SHT_NULL) elf.STT_NOTYPE else elf.STT_SECTION,
|
||||
.st_shndx = @intCast(shndx),
|
||||
.st_size = 0,
|
||||
.st_other = 0,
|
||||
};
|
||||
}
|
||||
|
||||
if (self.requiresThunks()) for (self.thunks.items) |th| {
|
||||
th.writeSymtab(self);
|
||||
@ -4309,42 +4319,6 @@ pub fn writeSymtab(self: *Elf) !void {
|
||||
try self.base.file.?.pwriteAll(self.strtab.items, strtab_shdr.sh_offset);
|
||||
}
|
||||
|
||||
fn writeSectionSymbols(self: *Elf) void {
|
||||
const slice = self.sections.slice();
|
||||
var ilocal: u32 = 1;
|
||||
for (slice.items(.shdr), 0..) |shdr, shndx| {
|
||||
const out_sym = &self.symtab.items[ilocal];
|
||||
out_sym.* = .{
|
||||
.st_name = 0,
|
||||
.st_value = shdr.sh_addr,
|
||||
.st_info = elf.STT_SECTION,
|
||||
.st_shndx = @intCast(shndx),
|
||||
.st_size = 0,
|
||||
.st_other = 0,
|
||||
};
|
||||
ilocal += 1;
|
||||
}
|
||||
|
||||
if (self.eh_frame_section_index) |shndx| {
|
||||
const shdr = slice.items(.shdr)[shndx];
|
||||
const out_sym = &self.symtab.items[ilocal];
|
||||
out_sym.* = .{
|
||||
.st_name = 0,
|
||||
.st_value = shdr.sh_addr,
|
||||
.st_info = elf.STT_SECTION,
|
||||
.st_shndx = @intCast(shndx),
|
||||
.st_size = 0,
|
||||
.st_other = 0,
|
||||
};
|
||||
ilocal += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sectionSymbolOutputSymtabIndex(self: Elf, shndx: u32) u32 {
|
||||
_ = self;
|
||||
return shndx + 1;
|
||||
}
|
||||
|
||||
/// Always 4 or 8 depending on whether this is 32-bit ELF or 64-bit ELF.
|
||||
pub fn ptrWidthBytes(self: Elf) u8 {
|
||||
return switch (self.ptr_width) {
|
||||
@ -5535,8 +5509,13 @@ pub const SymbolResolver = struct {
|
||||
};
|
||||
|
||||
const Section = struct {
|
||||
/// Section header.
|
||||
shdr: elf.Elf64_Shdr,
|
||||
|
||||
/// Assigned program header index if any.
|
||||
phndx: ?u32 = null,
|
||||
|
||||
/// List of atoms contributing to this section.
|
||||
atom_list: std.ArrayListUnmanaged(Ref) = .{},
|
||||
|
||||
/// Index of the last allocated atom in this section.
|
||||
|
||||
@ -336,10 +336,7 @@ pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.El
|
||||
switch (target.type(elf_file)) {
|
||||
elf.STT_SECTION => {
|
||||
r_addend += @intCast(target.address(.{}, elf_file));
|
||||
r_sym = if (target.outputShndx(elf_file)) |osec|
|
||||
elf_file.sectionSymbolOutputSymtabIndex(osec)
|
||||
else
|
||||
0;
|
||||
r_sym = target.outputShndx(elf_file) orelse 0;
|
||||
},
|
||||
else => {
|
||||
r_sym = target.outputSymtabIndex(elf_file) orelse 0;
|
||||
|
||||
@ -425,7 +425,7 @@ fn emitReloc(elf_file: *Elf, rec: anytype, sym: *const Symbol, rel: elf.Elf64_Re
|
||||
switch (sym.type(elf_file)) {
|
||||
elf.STT_SECTION => {
|
||||
r_addend += @intCast(sym.address(.{}, elf_file));
|
||||
r_sym = elf_file.sectionSymbolOutputSymtabIndex(sym.outputShndx(elf_file).?);
|
||||
r_sym = sym.outputShndx(elf_file).?;
|
||||
},
|
||||
else => {
|
||||
r_sym = sym.outputSymtabIndex(elf_file) orelse 0;
|
||||
|
||||
@ -393,8 +393,7 @@ fn updateComdatGroupsSizes(elf_file: *Elf) void {
|
||||
shdr.sh_link = elf_file.symtab_section_index.?;
|
||||
|
||||
const sym = cg.symbol(elf_file);
|
||||
shdr.sh_info = sym.outputSymtabIndex(elf_file) orelse
|
||||
elf_file.sectionSymbolOutputSymtabIndex(sym.outputShndx(elf_file).?);
|
||||
shdr.sh_info = sym.outputSymtabIndex(elf_file) orelse sym.outputShndx(elf_file).?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user