elf: move basic parse error reporting to SharedObject

This commit is contained in:
Jakub Konka 2023-12-05 13:53:11 +01:00
parent 3f42ed3ca2
commit 2e1dd1e554

View File

@ -52,6 +52,27 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void {
const reader = stream.reader();
self.header = try reader.readStruct(elf.Elf64_Ehdr);
if (elf_file.base.options.target.cpu.arch != self.header.?.e_machine.toTargetCpuArch().?) {
try elf_file.reportParseError2(
self.index,
"invalid cpu architecture: {s}",
.{@tagName(self.header.?.e_machine.toTargetCpuArch().?)},
);
return error.InvalidCpuArch;
}
if (self.data.len < self.header.?.e_shoff or
self.data.len < self.header.?.e_shoff + self.header.?.e_shnum * @sizeOf(elf.Elf64_Shdr))
{
try elf_file.reportParseError2(
self.index,
"corrupted header: section header table extends past the end of file",
.{},
);
return error.LinkFail;
}
const shoff = std.math.cast(usize, self.header.?.e_shoff) orelse return error.Overflow;
const shdrs = @as(
@ -61,6 +82,10 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void {
try self.shdrs.ensureTotalCapacityPrecise(gpa, shdrs.len);
for (shdrs, 0..) |shdr, i| {
if (self.data.len < shdr.sh_offset or self.data.len < shdr.sh_offset + shdr.sh_size) {
try elf_file.reportParseError2(self.index, "corrupted section header", .{});
return error.LinkFail;
}
self.shdrs.appendAssumeCapacity(try ElfShdr.fromElf64Shdr(shdr));
switch (shdr.sh_type) {
elf.SHT_DYNSYM => self.dynsym_sect_index = @as(u16, @intCast(i)),