authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-12-05 13:28:47+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-12-05 13:28:47+01:00
logaf8621db2d2de4675240dad0ff885f23dc33f518
tree6d3085ccc8bd161aacfd2d5d1b3c9f9a771ea33d
parent72568c131dcfc9303de0a809e02290c7ac464663

elf: report error at the point where it is happening


2 files changed, 29 insertions(+), 0 deletions(-)

src/link/Elf.zig+14
......@@ -1760,6 +1760,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
17601760}
17611761
17621762const ParseError = error{
1763 LinkFail,
17631764 UnknownFileType,
17641765 InvalidCpuArch,
17651766 OutOfMemory,
......@@ -1769,6 +1770,7 @@ const ParseError = error{
17691770 FileSystem,
17701771 NotSupported,
17711772 InvalidCharacter,
1773 MalformedObject,
17721774} || LdScript.Error || std.os.AccessError || std.os.SeekError || std.fs.File.OpenError || std.fs.File.ReadError;
17731775
17741776fn parsePositional(self: *Elf, path: []const u8, must_link: bool, ctx: *ParseErrorCtx) ParseError!void {
......@@ -6057,6 +6059,7 @@ fn handleAndReportParseError(
60576059) error{OutOfMemory}!void {
60586060 const cpu_arch = self.base.options.target.cpu.arch;
60596061 switch (err) {
6062 error.LinkFail => {}, // already reported
60606063 error.UnknownFileType => try self.reportParseError(path, "unknown file type", .{}),
60616064 error.InvalidCpuArch => try self.reportParseError(
60626065 path,
......@@ -6082,6 +6085,17 @@ fn reportParseError(
60826085 try err.addNote(self, "while parsing {s}", .{path});
60836086}
60846087
6088pub fn reportParseError2(
6089 self: *Elf,
6090 file_index: File.Index,
6091 comptime format: []const u8,
6092 args: anytype,
6093) error{OutOfMemory}!void {
6094 var err = try self.addErrorWithNotes(1);
6095 try err.addMsg(self, format, args);
6096 try err.addNote(self, "while parsing {}", .{self.file(file_index).?.fmtPath()});
6097}
6098
60856099const FormatShdrCtx = struct {
60866100 elf_file: *Elf,
60876101 shdr: elf.Elf64_Shdr,
src/link/Elf/Object.zig+15
......@@ -58,6 +58,17 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {
5858
5959 const gpa = elf_file.base.allocator;
6060
61 if (self.data.len < self.header.?.e_shoff or
62 self.data.len < self.header.?.e_shoff + self.header.?.e_shnum * @sizeOf(elf.Elf64_Shdr))
63 {
64 try elf_file.reportParseError2(
65 self.index,
66 "corrupted header: section header table extends past the end of file",
67 .{},
68 );
69 return error.LinkFail;
70 }
71
6172 const shoff = math.cast(usize, self.header.?.e_shoff) orelse return error.Overflow;
6273 const shdrs = @as(
6374 [*]align(1) const elf.Elf64_Shdr,
......@@ -66,6 +77,10 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {
6677 try self.shdrs.ensureTotalCapacityPrecise(gpa, shdrs.len);
6778
6879 for (shdrs) |shdr| {
80 if (self.data.len < shdr.sh_offset or self.data.len < shdr.sh_offset + shdr.sh_size) {
81 try elf_file.reportParseError2(self.index, "corrupted section header", .{});
82 return error.LinkFail;
83 }
6984 self.shdrs.appendAssumeCapacity(try ElfShdr.fromElf64Shdr(shdr));
7085 }
7186