authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-07-13 14:33:33+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-07-13 14:33:33+02:00
log4c3625d74546e22e1bd4695cc7bdde09f645bf30
treefcdfb89bfc48889acce463caeaaa8c3173a18e82
parent2896266a038cde4b64743534df7ccdd3f5b9347f

check-object: dump ELF header


1 files changed, 34 insertions(+), 1 deletions(-)

lib/std/Build/Step/CheckObject.zig+34-1
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const assert = std.debug.assert;2const assert = std.debug.assert;
3const elf = std.elf;
3const fs = std.fs;4const fs = std.fs;
4const macho = std.macho;5const macho = std.macho;
5const math = std.math;6const math = std.math;
...@@ -338,7 +339,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -338,7 +339,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
338 .macho => try MachODumper.parseAndDump(step, contents, .{339 .macho => try MachODumper.parseAndDump(step, contents, .{
339 .dump_symtab = self.dump_symtab,340 .dump_symtab = self.dump_symtab,
340 }),341 }),
341 .elf => @panic("TODO elf parser"),342 .elf => try ElfDumper.parseAndDump(step, contents, .{
343 .dump_symtab = self.dump_symtab,
344 }),
342 .coff => @panic("TODO coff parser"),345 .coff => @panic("TODO coff parser"),
343 .wasm => try WasmDumper.parseAndDump(step, contents, .{346 .wasm => try WasmDumper.parseAndDump(step, contents, .{
344 .dump_symtab = self.dump_symtab,347 .dump_symtab = self.dump_symtab,
...@@ -695,6 +698,36 @@ const MachODumper = struct {...@@ -695,6 +698,36 @@ const MachODumper = struct {
695 }698 }
696};699};
697700
701const ElfDumper = struct {
702 const symtab_label = "symtab";
703
704 fn parseAndDump(step: *Step, bytes: []const u8, opts: Opts) ![]const u8 {
705 _ = opts;
706
707 const gpa = step.owner.allocator;
708 var stream = std.io.fixedBufferStream(bytes);
709 const reader = stream.reader();
710
711 const hdr = try reader.readStruct(elf.Elf64_Ehdr);
712 if (!mem.eql(u8, hdr.e_ident[0..4], "\x7fELF")) {
713 return error.InvalidMagicNumber;
714 }
715
716 var output = std.ArrayList(u8).init(gpa);
717 const writer = output.writer();
718
719 try dumpHeader(hdr, writer);
720
721 return output.toOwnedSlice();
722 }
723
724 fn dumpHeader(hdr: elf.Elf64_Ehdr, writer: anytype) !void {
725 try writer.writeAll("header\n");
726 try writer.print("type {s}\n", .{@tagName(hdr.e_type)});
727 try writer.print("entry {x}\n", .{hdr.e_entry});
728 }
729};
730
698const WasmDumper = struct {731const WasmDumper = struct {
699 const symtab_label = "symbols";732 const symtab_label = "symbols";
700733