| ... | @@ -567,6 +567,8 @@ const MachODumper = struct { | ... | @@ -567,6 +567,8 @@ const MachODumper = struct { |
| 567 | var sections = std.ArrayList(macho.section_64).init(gpa); | 567 | var sections = std.ArrayList(macho.section_64).init(gpa); |
| 568 | var imports = std.ArrayList([]const u8).init(gpa); | 568 | var imports = std.ArrayList([]const u8).init(gpa); |
| 569 | | 569 | |
| | 570 | try dumpHeader(hdr, writer); |
| | 571 | |
| 570 | var it: LoadCommandIterator = .{ | 572 | var it: LoadCommandIterator = .{ |
| 571 | .ncmds = hdr.ncmds, | 573 | .ncmds = hdr.ncmds, |
| 572 | .buffer = bytes[@sizeOf(macho.mach_header_64)..][0..hdr.sizeofcmds], | 574 | .buffer = bytes[@sizeOf(macho.mach_header_64)..][0..hdr.sizeofcmds], |
| ... | @@ -609,6 +611,74 @@ const MachODumper = struct { | ... | @@ -609,6 +611,74 @@ const MachODumper = struct { |
| 609 | return output.toOwnedSlice(); | 611 | return output.toOwnedSlice(); |
| 610 | } | 612 | } |
| 611 | | 613 | |
| | 614 | fn dumpHeader(hdr: macho.mach_header_64, writer: anytype) !void { |
| | 615 | const cputype = switch (hdr.cputype) { |
| | 616 | macho.CPU_TYPE_ARM64 => "ARM64", |
| | 617 | macho.CPU_TYPE_X86_64 => "X86_64", |
| | 618 | else => "Unknown", |
| | 619 | }; |
| | 620 | const filetype = switch (hdr.filetype) { |
| | 621 | macho.MH_OBJECT => "MH_OBJECT", |
| | 622 | macho.MH_EXECUTE => "MH_EXECUTE", |
| | 623 | macho.MH_FVMLIB => "MH_FVMLIB", |
| | 624 | macho.MH_CORE => "MH_CORE", |
| | 625 | macho.MH_PRELOAD => "MH_PRELOAD", |
| | 626 | macho.MH_DYLIB => "MH_DYLIB", |
| | 627 | macho.MH_DYLINKER => "MH_DYLINKER", |
| | 628 | macho.MH_BUNDLE => "MH_BUNDLE", |
| | 629 | macho.MH_DYLIB_STUB => "MH_DYLIB_STUB", |
| | 630 | macho.MH_DSYM => "MH_DSYM", |
| | 631 | macho.MH_KEXT_BUNDLE => "MH_KEXT_BUNDLE", |
| | 632 | else => "Unknown", |
| | 633 | }; |
| | 634 | |
| | 635 | try writer.print( |
| | 636 | \\header |
| | 637 | \\cputype {s} |
| | 638 | \\filetype {s} |
| | 639 | \\ncmds {d} |
| | 640 | \\sizeofcmds {x} |
| | 641 | \\flags |
| | 642 | , .{ |
| | 643 | cputype, |
| | 644 | filetype, |
| | 645 | hdr.ncmds, |
| | 646 | hdr.sizeofcmds, |
| | 647 | }); |
| | 648 | |
| | 649 | if (hdr.flags > 0) { |
| | 650 | if (hdr.flags & macho.MH_NOUNDEFS != 0) try writer.writeAll(" NOUNDEFS"); |
| | 651 | if (hdr.flags & macho.MH_INCRLINK != 0) try writer.writeAll(" INCRLINK"); |
| | 652 | if (hdr.flags & macho.MH_DYLDLINK != 0) try writer.writeAll(" DYLDLINK"); |
| | 653 | if (hdr.flags & macho.MH_BINDATLOAD != 0) try writer.writeAll(" BINDATLOAD"); |
| | 654 | if (hdr.flags & macho.MH_PREBOUND != 0) try writer.writeAll(" PREBOUND"); |
| | 655 | if (hdr.flags & macho.MH_SPLIT_SEGS != 0) try writer.writeAll(" SPLIT_SEGS"); |
| | 656 | if (hdr.flags & macho.MH_LAZY_INIT != 0) try writer.writeAll(" LAZY_INIT"); |
| | 657 | if (hdr.flags & macho.MH_TWOLEVEL != 0) try writer.writeAll(" TWOLEVEL"); |
| | 658 | if (hdr.flags & macho.MH_FORCE_FLAT != 0) try writer.writeAll(" FORCE_FLAT"); |
| | 659 | if (hdr.flags & macho.MH_NOMULTIDEFS != 0) try writer.writeAll(" NOMULTIDEFS"); |
| | 660 | if (hdr.flags & macho.MH_NOFIXPREBINDING != 0) try writer.writeAll(" NOFIXPREBINDING"); |
| | 661 | if (hdr.flags & macho.MH_PREBINDABLE != 0) try writer.writeAll(" PREBINDABLE"); |
| | 662 | if (hdr.flags & macho.MH_ALLMODSBOUND != 0) try writer.writeAll(" ALLMODSBOUND"); |
| | 663 | if (hdr.flags & macho.MH_SUBSECTIONS_VIA_SYMBOLS != 0) try writer.writeAll(" SUBSECTIONS_VIA_SYMBOLS"); |
| | 664 | if (hdr.flags & macho.MH_CANONICAL != 0) try writer.writeAll(" CANONICAL"); |
| | 665 | if (hdr.flags & macho.MH_WEAK_DEFINES != 0) try writer.writeAll(" WEAK_DEFINES"); |
| | 666 | if (hdr.flags & macho.MH_BINDS_TO_WEAK != 0) try writer.writeAll(" BINDS_TO_WEAK"); |
| | 667 | if (hdr.flags & macho.MH_ALLOW_STACK_EXECUTION != 0) try writer.writeAll(" ALLOW_STACK_EXECUTION"); |
| | 668 | if (hdr.flags & macho.MH_ROOT_SAFE != 0) try writer.writeAll(" ROOT_SAFE"); |
| | 669 | if (hdr.flags & macho.MH_SETUID_SAFE != 0) try writer.writeAll(" SETUID_SAFE"); |
| | 670 | if (hdr.flags & macho.MH_NO_REEXPORTED_DYLIBS != 0) try writer.writeAll(" NO_REEXPORTED_DYLIBS"); |
| | 671 | if (hdr.flags & macho.MH_PIE != 0) try writer.writeAll(" PIE"); |
| | 672 | if (hdr.flags & macho.MH_DEAD_STRIPPABLE_DYLIB != 0) try writer.writeAll(" DEAD_STRIPPABLE_DYLIB"); |
| | 673 | if (hdr.flags & macho.MH_HAS_TLV_DESCRIPTORS != 0) try writer.writeAll(" HAS_TLV_DESCRIPTORS"); |
| | 674 | if (hdr.flags & macho.MH_NO_HEAP_EXECUTION != 0) try writer.writeAll(" NO_HEAP_EXECUTION"); |
| | 675 | if (hdr.flags & macho.MH_APP_EXTENSION_SAFE != 0) try writer.writeAll(" APP_EXTENSION_SAFE"); |
| | 676 | if (hdr.flags & macho.MH_NLIST_OUTOFSYNC_WITH_DYLDINFO != 0) try writer.writeAll(" NLIST_OUTOFSYNC_WITH_DYLDINFO"); |
| | 677 | } |
| | 678 | |
| | 679 | try writer.writeByte('\n'); |
| | 680 | } |
| | 681 | |
| 612 | fn dumpLoadCommand(lc: macho.LoadCommandIterator.LoadCommand, index: usize, writer: anytype) !void { | 682 | fn dumpLoadCommand(lc: macho.LoadCommandIterator.LoadCommand, index: usize, writer: anytype) !void { |
| 613 | // print header first | 683 | // print header first |
| 614 | try writer.print( | 684 | try writer.print( |