authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-11 14:34:13-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-11 14:34:13-04:00
logc71991c869fc8c997c2714fe1b53caef23449c9c
treec6c2e26fc09dccbb4ed07e66ce2859a9829e1596
parentd96b6c0d9f7a4fc6e7e28dd92d18e548f00885de
signaturelock-open Commit is signed but in an unrecognized format.

fix compilation errors for emitRaw


2 files changed, 27 insertions(+), 8 deletions(-)

lib/std/build/emit_raw.zig+1-1
......@@ -164,7 +164,7 @@ fn emitRaw(allocator: *Allocator, elf_path: []const u8, raw_path: []const u8) !v
164164 var out_file = try fs.cwd().createFile(raw_path, .{});
165165 defer out_file.close();
166166
167 const binary_elf_output = BinaryElfOutput.parse(allocator, elf_file);
167 const binary_elf_output = try BinaryElfOutput.parse(allocator, elf_file);
168168 defer binary_elf_output.deinit();
169169
170170 for (binary_elf_output.sections.toSlice()) |section| {
lib/std/elf.zig+26-7
......@@ -346,13 +346,13 @@ const Header = struct {
346346
347347pub fn readHeader(file: File) !Header {
348348 var hdr_buf: [@sizeOf(Elf64_Ehdr)]u8 align(@alignOf(Elf64_Ehdr)) = undefined;
349 try in_stream.preadAll(&hdr_buf, 0);
350 const hdr32 = @ptrCast(*elf.Elf32_Ehdr, &hdr_buf);
351 const hdr64 = @ptrCast(*elf.Elf64_Ehdr, &hdr_buf);
349 try preadNoEof(file, &hdr_buf, 0);
350 const hdr32 = @ptrCast(*Elf32_Ehdr, &hdr_buf);
351 const hdr64 = @ptrCast(*Elf64_Ehdr, &hdr_buf);
352352 if (!mem.eql(u8, hdr32.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic;
353353 if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion;
354354
355 const endian = switch (hdr32.e_ident[elf.EI_DATA]) {
355 const endian = switch (hdr32.e_ident[EI_DATA]) {
356356 ELFDATA2LSB => .Little,
357357 ELFDATA2MSB => .Big,
358358 else => return error.InvalidElfEndian,
......@@ -407,10 +407,10 @@ pub fn readAllHeaders(allocator: *mem.Allocator, file: File) !AllHeaders {
407407 // non-matching endian files, we post-process to correct integer endianness and offsets.
408408
409409 const shdr_buf = std.mem.sliceToBytes(hdrs.section_headers)[0 .. hdrs.header.shentsize * hdrs.header.shnum];
410 const phdr_buf = std.mem.sliceToBytes(hdrs.section_headers)[0 .. hdrs.header.phentsize * hdrs.header.phnum];
410 const phdr_buf = std.mem.sliceToBytes(hdrs.program_headers)[0 .. hdrs.header.phentsize * hdrs.header.phnum];
411411
412 try file.preadAll(phdr_buf, hdrs.header.phoff);
413 try file.preadAll(shdr_buf, hdrs.header.shoff);
412 try preadNoEof(file, shdr_buf, hdrs.header.shoff);
413 try preadNoEof(file, phdr_buf, hdrs.header.phoff);
414414
415415 const shdrs32 = @ptrCast([*]Elf32_Shdr, @alignCast(@alignOf(Elf32_Shdr), shdr_buf.ptr))[0..hdrs.header.shnum];
416416 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_
459459 }
460460}
461461
462fn preadNoEof(file: std.fs.File, buf: []u8, offset: u64) !void {
463 var i: u64 = 0;
464 while (i < buf.len) {
465 const len = file.pread(buf[i .. buf.len - i], offset + i) catch |err| switch (err) {
466 error.SystemResources => return error.SystemResources,
467 error.IsDir => return error.UnableToReadElfFile,
468 error.OperationAborted => return error.UnableToReadElfFile,
469 error.BrokenPipe => return error.UnableToReadElfFile,
470 error.Unseekable => return error.UnableToReadElfFile,
471 error.ConnectionResetByPeer => return error.UnableToReadElfFile,
472 error.InputOutput => return error.FileSystem,
473 error.Unexpected => return error.Unexpected,
474 error.WouldBlock => return error.Unexpected,
475 };
476 if (len == 0) return error.UnexpectedEndOfFile;
477 i += len;
478 }
479}
480
462481pub const EI_NIDENT = 16;
463482
464483pub const EI_CLASS = 4;