| ... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const assert = std.debug.assert; |
| 3 | const elf = std.elf; |
| 3 | 4 | const fs = std.fs; |
| 4 | 5 | const macho = std.macho; |
| 5 | 6 | const math = std.math; |
| ... | ... | @@ -338,7 +339,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { |
| 338 | 339 | .macho => try MachODumper.parseAndDump(step, contents, .{ |
| 339 | 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 | 345 | .coff => @panic("TODO coff parser"), |
| 343 | 346 | .wasm => try WasmDumper.parseAndDump(step, contents, .{ |
| 344 | 347 | .dump_symtab = self.dump_symtab, |
| ... | ... | @@ -695,6 +698,36 @@ const MachODumper = struct { |
| 695 | 698 | } |
| 696 | 699 | }; |
| 697 | 700 | |
| 701 | const 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 | |
| 698 | 731 | const WasmDumper = struct { |
| 699 | 732 | const symtab_label = "symbols"; |
| 700 | 733 | |