elf: calculate required size for .rela.eh_frame

This commit is contained in:
Jakub Konka 2023-11-09 12:02:20 +01:00
parent 666ac6bf9b
commit 0efc471122
2 changed files with 21 additions and 0 deletions

View File

@ -4245,6 +4245,11 @@ fn updateSectionSizesObject(self: *Elf) !void {
if (self.eh_frame_section_index) |index| {
self.shdrs.items[index].sh_size = try eh_frame.calcEhFrameSize(self);
if (self.eh_frame_rela_section_index) |rela_index| {
const shdr = &self.shdrs.items[rela_index];
shdr.sh_size = eh_frame.calcEhFrameRelocs(self) * shdr.sh_entsize;
}
}
try self.updateSymtabSize();

View File

@ -282,6 +282,22 @@ pub fn calcEhFrameHdrSize(elf_file: *Elf) usize {
return eh_frame_hdr_header_size + count * 8;
}
pub fn calcEhFrameRelocs(elf_file: *Elf) usize {
var count: usize = 0;
for (elf_file.objects.items) |index| {
const object = elf_file.file(index).?.object;
for (object.cies.items) |cie| {
if (!cie.alive) continue;
count += cie.relocs(elf_file).len;
}
for (object.fdes.items) |fde| {
if (!fde.alive) continue;
count += fde.relocs(elf_file).len;
}
}
return count;
}
fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file: *Elf, contents: []u8) !void {
const offset = std.math.cast(usize, rel.r_offset - rec.offset) orelse return error.Overflow;
const P = @as(i64, @intCast(rec.address(elf_file) + offset));