From c71991c869fc8c997c2714fe1b53caef23449c9c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 11 Mar 2020 14:34:13 -0400 Subject: [PATCH] fix compilation errors for emitRaw --- lib/std/build/emit_raw.zig | 2 +- lib/std/elf.zig | 33 ++++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/std/build/emit_raw.zig b/lib/std/build/emit_raw.zig index 63c2329947..b56305403c 100644 --- a/lib/std/build/emit_raw.zig +++ b/lib/std/build/emit_raw.zig @@ -164,7 +164,7 @@ fn emitRaw(allocator: *Allocator, elf_path: []const u8, raw_path: []const u8) !v var out_file = try fs.cwd().createFile(raw_path, .{}); defer out_file.close(); - const binary_elf_output = BinaryElfOutput.parse(allocator, elf_file); + const binary_elf_output = try BinaryElfOutput.parse(allocator, elf_file); defer binary_elf_output.deinit(); for (binary_elf_output.sections.toSlice()) |section| { diff --git a/lib/std/elf.zig b/lib/std/elf.zig index 3aefd87f09..5cb3c566af 100644 --- a/lib/std/elf.zig +++ b/lib/std/elf.zig @@ -346,13 +346,13 @@ const Header = struct { pub fn readHeader(file: File) !Header { var hdr_buf: [@sizeOf(Elf64_Ehdr)]u8 align(@alignOf(Elf64_Ehdr)) = undefined; - try in_stream.preadAll(&hdr_buf, 0); - const hdr32 = @ptrCast(*elf.Elf32_Ehdr, &hdr_buf); - const hdr64 = @ptrCast(*elf.Elf64_Ehdr, &hdr_buf); + try preadNoEof(file, &hdr_buf, 0); + const hdr32 = @ptrCast(*Elf32_Ehdr, &hdr_buf); + const hdr64 = @ptrCast(*Elf64_Ehdr, &hdr_buf); if (!mem.eql(u8, hdr32.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic; if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion; - const endian = switch (hdr32.e_ident[elf.EI_DATA]) { + const endian = switch (hdr32.e_ident[EI_DATA]) { ELFDATA2LSB => .Little, ELFDATA2MSB => .Big, else => return error.InvalidElfEndian, @@ -407,10 +407,10 @@ pub fn readAllHeaders(allocator: *mem.Allocator, file: File) !AllHeaders { // non-matching endian files, we post-process to correct integer endianness and offsets. const shdr_buf = std.mem.sliceToBytes(hdrs.section_headers)[0 .. hdrs.header.shentsize * hdrs.header.shnum]; - const phdr_buf = std.mem.sliceToBytes(hdrs.section_headers)[0 .. hdrs.header.phentsize * hdrs.header.phnum]; + const phdr_buf = std.mem.sliceToBytes(hdrs.program_headers)[0 .. hdrs.header.phentsize * hdrs.header.phnum]; - try file.preadAll(phdr_buf, hdrs.header.phoff); - try file.preadAll(shdr_buf, hdrs.header.shoff); + try preadNoEof(file, shdr_buf, hdrs.header.shoff); + try preadNoEof(file, phdr_buf, hdrs.header.phoff); const shdrs32 = @ptrCast([*]Elf32_Shdr, @alignCast(@alignOf(Elf32_Shdr), shdr_buf.ptr))[0..hdrs.header.shnum]; const phdrs32 = @ptrCast([*]Elf32_Phdr, @alignCast(@alignOf(Elf32_Phdr), phdr_buf.ptr))[0..hdrs.header.phnum]; @@ -459,6 +459,25 @@ pub fn int(is_64: bool, need_bswap: bool, int_32: var, int_64: var) @TypeOf(int_ } } +fn preadNoEof(file: std.fs.File, buf: []u8, offset: u64) !void { + var i: u64 = 0; + while (i < buf.len) { + const len = file.pread(buf[i .. buf.len - i], offset + i) catch |err| switch (err) { + error.SystemResources => return error.SystemResources, + error.IsDir => return error.UnableToReadElfFile, + error.OperationAborted => return error.UnableToReadElfFile, + error.BrokenPipe => return error.UnableToReadElfFile, + error.Unseekable => return error.UnableToReadElfFile, + error.ConnectionResetByPeer => return error.UnableToReadElfFile, + error.InputOutput => return error.FileSystem, + error.Unexpected => return error.Unexpected, + error.WouldBlock => return error.Unexpected, + }; + if (len == 0) return error.UnexpectedEndOfFile; + i += len; + } +} + pub const EI_NIDENT = 16; pub const EI_CLASS = 4;