| ... | @@ -36,7 +36,7 @@ pub fn create( | ... | @@ -36,7 +36,7 @@ pub fn create( |
| 36 | .makeFn = make, | 36 | .makeFn = make, |
| 37 | }), | 37 | }), |
| 38 | .source = source.dupe(owner), | 38 | .source = source.dupe(owner), |
| 39 | .checks = .init(gpa), | 39 | .checks = std.ArrayList(Check).init(gpa), |
| 40 | .obj_format = obj_format, | 40 | .obj_format = obj_format, |
| 41 | }; | 41 | }; |
| 42 | check_object.source.addStepDependencies(&check_object.step); | 42 | check_object.source.addStepDependencies(&check_object.step); |
| ... | @@ -75,7 +75,7 @@ const Action = struct { | ... | @@ -75,7 +75,7 @@ const Action = struct { |
| 75 | b: *std.Build, | 75 | b: *std.Build, |
| 76 | step: *Step, | 76 | step: *Step, |
| 77 | haystack: []const u8, | 77 | haystack: []const u8, |
| 78 | global_vars: *std.StringHashMap(u64), | 78 | global_vars: anytype, |
| 79 | ) !bool { | 79 | ) !bool { |
| 80 | assert(act.tag == .extract); | 80 | assert(act.tag == .extract); |
| 81 | const hay = mem.trim(u8, haystack, " "); | 81 | const hay = mem.trim(u8, haystack, " "); |
| ... | @@ -154,11 +154,11 @@ const Action = struct { | ... | @@ -154,11 +154,11 @@ const Action = struct { |
| 154 | /// Will return true if the `phrase` is correctly parsed into an RPN program and | 154 | /// Will return true if the `phrase` is correctly parsed into an RPN program and |
| 155 | /// its reduced, computed value compares using `op` with the expected value, either | 155 | /// its reduced, computed value compares using `op` with the expected value, either |
| 156 | /// a literal or another extracted variable. | 156 | /// a literal or another extracted variable. |
| 157 | fn computeCmp(act: Action, b: *std.Build, step: *Step, global_vars: std.StringHashMap(u64)) !bool { | 157 | fn computeCmp(act: Action, b: *std.Build, step: *Step, global_vars: anytype) !bool { |
| 158 | const gpa = step.owner.allocator; | 158 | const gpa = step.owner.allocator; |
| 159 | const phrase = act.phrase.resolve(b, step); | 159 | const phrase = act.phrase.resolve(b, step); |
| 160 | var op_stack: std.ArrayList(enum { add, sub, mod, mul }) = .init(gpa); | 160 | var op_stack = std.ArrayList(enum { add, sub, mod, mul }).init(gpa); |
| 161 | var values: std.ArrayList(u64) = .init(gpa); | 161 | var values = std.ArrayList(u64).init(gpa); |
| 162 | | 162 | |
| 163 | var it = mem.tokenizeScalar(u8, phrase, ' '); | 163 | var it = mem.tokenizeScalar(u8, phrase, ' '); |
| 164 | while (it.next()) |next| { | 164 | while (it.next()) |next| { |
| ... | @@ -230,11 +230,11 @@ const ComputeCompareExpected = struct { | ... | @@ -230,11 +230,11 @@ const ComputeCompareExpected = struct { |
| 230 | literal: u64, | 230 | literal: u64, |
| 231 | }, | 231 | }, |
| 232 | | 232 | |
| 233 | pub fn format(value: ComputeCompareExpected, bw: *Writer) Writer.Error!void { | 233 | pub fn format(value: ComputeCompareExpected, w: *Writer) Writer.Error!void { |
| 234 | try bw.print("{s} ", .{@tagName(value.op)}); | 234 | try w.print("{t} ", .{value.op}); |
| 235 | switch (value.value) { | 235 | switch (value.value) { |
| 236 | .variable => |name| try bw.writeAll(name), | 236 | .variable => |name| try w.writeAll(name), |
| 237 | .literal => |x| try bw.print("{x}", .{x}), | 237 | .literal => |x| try w.print("{x}", .{x}), |
| 238 | } | 238 | } |
| 239 | } | 239 | } |
| 240 | }; | 240 | }; |
| ... | @@ -242,63 +242,56 @@ const ComputeCompareExpected = struct { | ... | @@ -242,63 +242,56 @@ const ComputeCompareExpected = struct { |
| 242 | const Check = struct { | 242 | const Check = struct { |
| 243 | kind: Kind, | 243 | kind: Kind, |
| 244 | payload: Payload, | 244 | payload: Payload, |
| 245 | allocator: Allocator, | 245 | data: std.ArrayList(u8), |
| 246 | data: std.ArrayListUnmanaged(u8), | 246 | actions: std.ArrayList(Action), |
| 247 | actions: std.ArrayListUnmanaged(Action), | | |
| 248 | | 247 | |
| 249 | fn create(allocator: Allocator, kind: Kind) Check { | 248 | fn create(allocator: Allocator, kind: Kind) Check { |
| 250 | return .{ | 249 | return .{ |
| 251 | .kind = kind, | 250 | .kind = kind, |
| 252 | .payload = .{ .none = {} }, | 251 | .payload = .{ .none = {} }, |
| 253 | .allocator = allocator, | 252 | .data = std.ArrayList(u8).init(allocator), |
| 254 | .data = .empty, | 253 | .actions = std.ArrayList(Action).init(allocator), |
| 255 | .actions = .empty, | | |
| 256 | }; | 254 | }; |
| 257 | } | 255 | } |
| 258 | | 256 | |
| 259 | fn dumpSection(gpa: Allocator, name: [:0]const u8) Check { | 257 | fn dumpSection(allocator: Allocator, name: [:0]const u8) Check { |
| 260 | var check = Check.create(gpa, .dump_section); | 258 | var check = Check.create(allocator, .dump_section); |
| 261 | const off: u32 = @intCast(check.data.items.len); | 259 | const off: u32 = @intCast(check.data.items.len); |
| 262 | check.data.print(gpa, "{s}\x00", .{name}) catch @panic("OOM"); | 260 | check.data.writer().print("{s}\x00", .{name}) catch @panic("OOM"); |
| 263 | check.payload = .{ .dump_section = off }; | 261 | check.payload = .{ .dump_section = off }; |
| 264 | return check; | 262 | return check; |
| 265 | } | 263 | } |
| 266 | | 264 | |
| 267 | fn extract(check: *Check, phrase: SearchPhrase) void { | 265 | fn extract(check: *Check, phrase: SearchPhrase) void { |
| 268 | const gpa = check.allocator; | 266 | check.actions.append(.{ |
| 269 | check.actions.append(gpa, .{ | | |
| 270 | .tag = .extract, | 267 | .tag = .extract, |
| 271 | .phrase = phrase, | 268 | .phrase = phrase, |
| 272 | }) catch @panic("OOM"); | 269 | }) catch @panic("OOM"); |
| 273 | } | 270 | } |
| 274 | | 271 | |
| 275 | fn exact(check: *Check, phrase: SearchPhrase) void { | 272 | fn exact(check: *Check, phrase: SearchPhrase) void { |
| 276 | const gpa = check.allocator; | 273 | check.actions.append(.{ |
| 277 | check.actions.append(gpa, .{ | | |
| 278 | .tag = .exact, | 274 | .tag = .exact, |
| 279 | .phrase = phrase, | 275 | .phrase = phrase, |
| 280 | }) catch @panic("OOM"); | 276 | }) catch @panic("OOM"); |
| 281 | } | 277 | } |
| 282 | | 278 | |
| 283 | fn contains(check: *Check, phrase: SearchPhrase) void { | 279 | fn contains(check: *Check, phrase: SearchPhrase) void { |
| 284 | const gpa = check.allocator; | 280 | check.actions.append(.{ |
| 285 | check.actions.append(gpa, .{ | | |
| 286 | .tag = .contains, | 281 | .tag = .contains, |
| 287 | .phrase = phrase, | 282 | .phrase = phrase, |
| 288 | }) catch @panic("OOM"); | 283 | }) catch @panic("OOM"); |
| 289 | } | 284 | } |
| 290 | | 285 | |
| 291 | fn notPresent(check: *Check, phrase: SearchPhrase) void { | 286 | fn notPresent(check: *Check, phrase: SearchPhrase) void { |
| 292 | const gpa = check.allocator; | 287 | check.actions.append(.{ |
| 293 | check.actions.append(gpa, .{ | | |
| 294 | .tag = .not_present, | 288 | .tag = .not_present, |
| 295 | .phrase = phrase, | 289 | .phrase = phrase, |
| 296 | }) catch @panic("OOM"); | 290 | }) catch @panic("OOM"); |
| 297 | } | 291 | } |
| 298 | | 292 | |
| 299 | fn computeCmp(check: *Check, phrase: SearchPhrase, expected: ComputeCompareExpected) void { | 293 | fn computeCmp(check: *Check, phrase: SearchPhrase, expected: ComputeCompareExpected) void { |
| 300 | const gpa = check.allocator; | 294 | check.actions.append(.{ |
| 301 | check.actions.append(gpa, .{ | | |
| 302 | .tag = .compute_cmp, | 295 | .tag = .compute_cmp, |
| 303 | .phrase = phrase, | 296 | .phrase = phrase, |
| 304 | .expected = expected, | 297 | .expected = expected, |
| ... | @@ -751,14 +744,14 @@ const MachODumper = struct { | ... | @@ -751,14 +744,14 @@ const MachODumper = struct { |
| 751 | }, | 744 | }, |
| 752 | .SYMTAB => { | 745 | .SYMTAB => { |
| 753 | const lc = cmd.cast(macho.symtab_command).?; | 746 | const lc = cmd.cast(macho.symtab_command).?; |
| 754 | const symtab = @as([*]align(1) const macho.nlist_64, @ptrCast(ctx.data[lc.symoff..].ptr))[0..lc.nsyms]; | 747 | const symtab = @as([*]align(1) const macho.nlist_64, @ptrCast(ctx.data.ptr + lc.symoff))[0..lc.nsyms]; |
| 755 | const strtab = ctx.data[lc.stroff..][0..lc.strsize]; | 748 | const strtab = ctx.data[lc.stroff..][0..lc.strsize]; |
| 756 | try ctx.symtab.appendUnalignedSlice(ctx.gpa, symtab); | 749 | try ctx.symtab.appendUnalignedSlice(ctx.gpa, symtab); |
| 757 | try ctx.strtab.appendSlice(ctx.gpa, strtab); | 750 | try ctx.strtab.appendSlice(ctx.gpa, strtab); |
| 758 | }, | 751 | }, |
| 759 | .DYSYMTAB => { | 752 | .DYSYMTAB => { |
| 760 | const lc = cmd.cast(macho.dysymtab_command).?; | 753 | const lc = cmd.cast(macho.dysymtab_command).?; |
| 761 | const indexes = @as([*]align(1) const u32, @ptrCast(ctx.data[lc.indirectsymoff..].ptr))[0..lc.nindirectsyms]; | 754 | const indexes = @as([*]align(1) const u32, @ptrCast(ctx.data.ptr + lc.indirectsymoff))[0..lc.nindirectsyms]; |
| 762 | try ctx.indsymtab.appendUnalignedSlice(ctx.gpa, indexes); | 755 | try ctx.indsymtab.appendUnalignedSlice(ctx.gpa, indexes); |
| 763 | }, | 756 | }, |
| 764 | .LOAD_DYLIB, | 757 | .LOAD_DYLIB, |
| ... | @@ -776,7 +769,7 @@ const MachODumper = struct { | ... | @@ -776,7 +769,7 @@ const MachODumper = struct { |
| 776 | | 769 | |
| 777 | fn getString(ctx: ObjectContext, off: u32) [:0]const u8 { | 770 | fn getString(ctx: ObjectContext, off: u32) [:0]const u8 { |
| 778 | assert(off < ctx.strtab.items.len); | 771 | assert(off < ctx.strtab.items.len); |
| 779 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(ctx.strtab.items[off..].ptr)), 0); | 772 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(ctx.strtab.items.ptr + off)), 0); |
| 780 | } | 773 | } |
| 781 | | 774 | |
| 782 | fn getLoadCommandIterator(ctx: ObjectContext) macho.LoadCommandIterator { | 775 | fn getLoadCommandIterator(ctx: ObjectContext) macho.LoadCommandIterator { |
| ... | @@ -806,7 +799,7 @@ const MachODumper = struct { | ... | @@ -806,7 +799,7 @@ const MachODumper = struct { |
| 806 | return null; | 799 | return null; |
| 807 | } | 800 | } |
| 808 | | 801 | |
| 809 | fn dumpHeader(hdr: macho.mach_header_64, bw: *Writer) !void { | 802 | fn dumpHeader(hdr: macho.mach_header_64, writer: anytype) !void { |
| 810 | const cputype = switch (hdr.cputype) { | 803 | const cputype = switch (hdr.cputype) { |
| 811 | macho.CPU_TYPE_ARM64 => "ARM64", | 804 | macho.CPU_TYPE_ARM64 => "ARM64", |
| 812 | macho.CPU_TYPE_X86_64 => "X86_64", | 805 | macho.CPU_TYPE_X86_64 => "X86_64", |
| ... | @@ -827,7 +820,7 @@ const MachODumper = struct { | ... | @@ -827,7 +820,7 @@ const MachODumper = struct { |
| 827 | else => "Unknown", | 820 | else => "Unknown", |
| 828 | }; | 821 | }; |
| 829 | | 822 | |
| 830 | try bw.print( | 823 | try writer.print( |
| 831 | \\header | 824 | \\header |
| 832 | \\cputype {s} | 825 | \\cputype {s} |
| 833 | \\filetype {s} | 826 | \\filetype {s} |
| ... | @@ -842,41 +835,41 @@ const MachODumper = struct { | ... | @@ -842,41 +835,41 @@ const MachODumper = struct { |
| 842 | }); | 835 | }); |
| 843 | | 836 | |
| 844 | if (hdr.flags > 0) { | 837 | if (hdr.flags > 0) { |
| 845 | if (hdr.flags & macho.MH_NOUNDEFS != 0) try bw.writeAll(" NOUNDEFS"); | 838 | if (hdr.flags & macho.MH_NOUNDEFS != 0) try writer.writeAll(" NOUNDEFS"); |
| 846 | if (hdr.flags & macho.MH_INCRLINK != 0) try bw.writeAll(" INCRLINK"); | 839 | if (hdr.flags & macho.MH_INCRLINK != 0) try writer.writeAll(" INCRLINK"); |
| 847 | if (hdr.flags & macho.MH_DYLDLINK != 0) try bw.writeAll(" DYLDLINK"); | 840 | if (hdr.flags & macho.MH_DYLDLINK != 0) try writer.writeAll(" DYLDLINK"); |
| 848 | if (hdr.flags & macho.MH_BINDATLOAD != 0) try bw.writeAll(" BINDATLOAD"); | 841 | if (hdr.flags & macho.MH_BINDATLOAD != 0) try writer.writeAll(" BINDATLOAD"); |
| 849 | if (hdr.flags & macho.MH_PREBOUND != 0) try bw.writeAll(" PREBOUND"); | 842 | if (hdr.flags & macho.MH_PREBOUND != 0) try writer.writeAll(" PREBOUND"); |
| 850 | if (hdr.flags & macho.MH_SPLIT_SEGS != 0) try bw.writeAll(" SPLIT_SEGS"); | 843 | if (hdr.flags & macho.MH_SPLIT_SEGS != 0) try writer.writeAll(" SPLIT_SEGS"); |
| 851 | if (hdr.flags & macho.MH_LAZY_INIT != 0) try bw.writeAll(" LAZY_INIT"); | 844 | if (hdr.flags & macho.MH_LAZY_INIT != 0) try writer.writeAll(" LAZY_INIT"); |
| 852 | if (hdr.flags & macho.MH_TWOLEVEL != 0) try bw.writeAll(" TWOLEVEL"); | 845 | if (hdr.flags & macho.MH_TWOLEVEL != 0) try writer.writeAll(" TWOLEVEL"); |
| 853 | if (hdr.flags & macho.MH_FORCE_FLAT != 0) try bw.writeAll(" FORCE_FLAT"); | 846 | if (hdr.flags & macho.MH_FORCE_FLAT != 0) try writer.writeAll(" FORCE_FLAT"); |
| 854 | if (hdr.flags & macho.MH_NOMULTIDEFS != 0) try bw.writeAll(" NOMULTIDEFS"); | 847 | if (hdr.flags & macho.MH_NOMULTIDEFS != 0) try writer.writeAll(" NOMULTIDEFS"); |
| 855 | if (hdr.flags & macho.MH_NOFIXPREBINDING != 0) try bw.writeAll(" NOFIXPREBINDING"); | 848 | if (hdr.flags & macho.MH_NOFIXPREBINDING != 0) try writer.writeAll(" NOFIXPREBINDING"); |
| 856 | if (hdr.flags & macho.MH_PREBINDABLE != 0) try bw.writeAll(" PREBINDABLE"); | 849 | if (hdr.flags & macho.MH_PREBINDABLE != 0) try writer.writeAll(" PREBINDABLE"); |
| 857 | if (hdr.flags & macho.MH_ALLMODSBOUND != 0) try bw.writeAll(" ALLMODSBOUND"); | 850 | if (hdr.flags & macho.MH_ALLMODSBOUND != 0) try writer.writeAll(" ALLMODSBOUND"); |
| 858 | if (hdr.flags & macho.MH_SUBSECTIONS_VIA_SYMBOLS != 0) try bw.writeAll(" SUBSECTIONS_VIA_SYMBOLS"); | 851 | if (hdr.flags & macho.MH_SUBSECTIONS_VIA_SYMBOLS != 0) try writer.writeAll(" SUBSECTIONS_VIA_SYMBOLS"); |
| 859 | if (hdr.flags & macho.MH_CANONICAL != 0) try bw.writeAll(" CANONICAL"); | 852 | if (hdr.flags & macho.MH_CANONICAL != 0) try writer.writeAll(" CANONICAL"); |
| 860 | if (hdr.flags & macho.MH_WEAK_DEFINES != 0) try bw.writeAll(" WEAK_DEFINES"); | 853 | if (hdr.flags & macho.MH_WEAK_DEFINES != 0) try writer.writeAll(" WEAK_DEFINES"); |
| 861 | if (hdr.flags & macho.MH_BINDS_TO_WEAK != 0) try bw.writeAll(" BINDS_TO_WEAK"); | 854 | if (hdr.flags & macho.MH_BINDS_TO_WEAK != 0) try writer.writeAll(" BINDS_TO_WEAK"); |
| 862 | if (hdr.flags & macho.MH_ALLOW_STACK_EXECUTION != 0) try bw.writeAll(" ALLOW_STACK_EXECUTION"); | 855 | if (hdr.flags & macho.MH_ALLOW_STACK_EXECUTION != 0) try writer.writeAll(" ALLOW_STACK_EXECUTION"); |
| 863 | if (hdr.flags & macho.MH_ROOT_SAFE != 0) try bw.writeAll(" ROOT_SAFE"); | 856 | if (hdr.flags & macho.MH_ROOT_SAFE != 0) try writer.writeAll(" ROOT_SAFE"); |
| 864 | if (hdr.flags & macho.MH_SETUID_SAFE != 0) try bw.writeAll(" SETUID_SAFE"); | 857 | if (hdr.flags & macho.MH_SETUID_SAFE != 0) try writer.writeAll(" SETUID_SAFE"); |
| 865 | if (hdr.flags & macho.MH_NO_REEXPORTED_DYLIBS != 0) try bw.writeAll(" NO_REEXPORTED_DYLIBS"); | 858 | if (hdr.flags & macho.MH_NO_REEXPORTED_DYLIBS != 0) try writer.writeAll(" NO_REEXPORTED_DYLIBS"); |
| 866 | if (hdr.flags & macho.MH_PIE != 0) try bw.writeAll(" PIE"); | 859 | if (hdr.flags & macho.MH_PIE != 0) try writer.writeAll(" PIE"); |
| 867 | if (hdr.flags & macho.MH_DEAD_STRIPPABLE_DYLIB != 0) try bw.writeAll(" DEAD_STRIPPABLE_DYLIB"); | 860 | if (hdr.flags & macho.MH_DEAD_STRIPPABLE_DYLIB != 0) try writer.writeAll(" DEAD_STRIPPABLE_DYLIB"); |
| 868 | if (hdr.flags & macho.MH_HAS_TLV_DESCRIPTORS != 0) try bw.writeAll(" HAS_TLV_DESCRIPTORS"); | 861 | if (hdr.flags & macho.MH_HAS_TLV_DESCRIPTORS != 0) try writer.writeAll(" HAS_TLV_DESCRIPTORS"); |
| 869 | if (hdr.flags & macho.MH_NO_HEAP_EXECUTION != 0) try bw.writeAll(" NO_HEAP_EXECUTION"); | 862 | if (hdr.flags & macho.MH_NO_HEAP_EXECUTION != 0) try writer.writeAll(" NO_HEAP_EXECUTION"); |
| 870 | if (hdr.flags & macho.MH_APP_EXTENSION_SAFE != 0) try bw.writeAll(" APP_EXTENSION_SAFE"); | 863 | if (hdr.flags & macho.MH_APP_EXTENSION_SAFE != 0) try writer.writeAll(" APP_EXTENSION_SAFE"); |
| 871 | if (hdr.flags & macho.MH_NLIST_OUTOFSYNC_WITH_DYLDINFO != 0) try bw.writeAll(" NLIST_OUTOFSYNC_WITH_DYLDINFO"); | 864 | if (hdr.flags & macho.MH_NLIST_OUTOFSYNC_WITH_DYLDINFO != 0) try writer.writeAll(" NLIST_OUTOFSYNC_WITH_DYLDINFO"); |
| 872 | } | 865 | } |
| 873 | | 866 | |
| 874 | try bw.writeByte('\n'); | 867 | try writer.writeByte('\n'); |
| 875 | } | 868 | } |
| 876 | | 869 | |
| 877 | fn dumpLoadCommand(lc: macho.LoadCommandIterator.LoadCommand, index: usize, bw: *Writer) !void { | 870 | fn dumpLoadCommand(lc: macho.LoadCommandIterator.LoadCommand, index: usize, writer: anytype) !void { |
| 878 | // print header first | 871 | // print header first |
| 879 | try bw.print( | 872 | try writer.print( |
| 880 | \\LC {d} | 873 | \\LC {d} |
| 881 | \\cmd {s} | 874 | \\cmd {s} |
| 882 | \\cmdsize {d} | 875 | \\cmdsize {d} |
| ... | @@ -885,8 +878,8 @@ const MachODumper = struct { | ... | @@ -885,8 +878,8 @@ const MachODumper = struct { |
| 885 | switch (lc.cmd()) { | 878 | switch (lc.cmd()) { |
| 886 | .SEGMENT_64 => { | 879 | .SEGMENT_64 => { |
| 887 | const seg = lc.cast(macho.segment_command_64).?; | 880 | const seg = lc.cast(macho.segment_command_64).?; |
| 888 | try bw.writeByte('\n'); | 881 | try writer.writeByte('\n'); |
| 889 | try bw.print( | 882 | try writer.print( |
| 890 | \\segname {s} | 883 | \\segname {s} |
| 891 | \\vmaddr {x} | 884 | \\vmaddr {x} |
| 892 | \\vmsize {x} | 885 | \\vmsize {x} |
| ... | @@ -901,8 +894,8 @@ const MachODumper = struct { | ... | @@ -901,8 +894,8 @@ const MachODumper = struct { |
| 901 | }); | 894 | }); |
| 902 | | 895 | |
| 903 | for (lc.getSections()) |sect| { | 896 | for (lc.getSections()) |sect| { |
| 904 | try bw.writeByte('\n'); | 897 | try writer.writeByte('\n'); |
| 905 | try bw.print( | 898 | try writer.print( |
| 906 | \\sectname {s} | 899 | \\sectname {s} |
| 907 | \\addr {x} | 900 | \\addr {x} |
| 908 | \\size {x} | 901 | \\size {x} |
| ... | @@ -924,8 +917,8 @@ const MachODumper = struct { | ... | @@ -924,8 +917,8 @@ const MachODumper = struct { |
| 924 | .REEXPORT_DYLIB, | 917 | .REEXPORT_DYLIB, |
| 925 | => { | 918 | => { |
| 926 | const dylib = lc.cast(macho.dylib_command).?; | 919 | const dylib = lc.cast(macho.dylib_command).?; |
| 927 | try bw.writeByte('\n'); | 920 | try writer.writeByte('\n'); |
| 928 | try bw.print( | 921 | try writer.print( |
| 929 | \\name {s} | 922 | \\name {s} |
| 930 | \\timestamp {d} | 923 | \\timestamp {d} |
| 931 | \\current version {x} | 924 | \\current version {x} |
| ... | @@ -940,16 +933,16 @@ const MachODumper = struct { | ... | @@ -940,16 +933,16 @@ const MachODumper = struct { |
| 940 | | 933 | |
| 941 | .MAIN => { | 934 | .MAIN => { |
| 942 | const main = lc.cast(macho.entry_point_command).?; | 935 | const main = lc.cast(macho.entry_point_command).?; |
| 943 | try bw.writeByte('\n'); | 936 | try writer.writeByte('\n'); |
| 944 | try bw.print( | 937 | try writer.print( |
| 945 | \\entryoff {x} | 938 | \\entryoff {x} |
| 946 | \\stacksize {x} | 939 | \\stacksize {x} |
| 947 | , .{ main.entryoff, main.stacksize }); | 940 | , .{ main.entryoff, main.stacksize }); |
| 948 | }, | 941 | }, |
| 949 | | 942 | |
| 950 | .RPATH => { | 943 | .RPATH => { |
| 951 | try bw.writeByte('\n'); | 944 | try writer.writeByte('\n'); |
| 952 | try bw.print( | 945 | try writer.print( |
| 953 | \\path {s} | 946 | \\path {s} |
| 954 | , .{ | 947 | , .{ |
| 955 | lc.getRpathPathName(), | 948 | lc.getRpathPathName(), |
| ... | @@ -958,8 +951,8 @@ const MachODumper = struct { | ... | @@ -958,8 +951,8 @@ const MachODumper = struct { |
| 958 | | 951 | |
| 959 | .UUID => { | 952 | .UUID => { |
| 960 | const uuid = lc.cast(macho.uuid_command).?; | 953 | const uuid = lc.cast(macho.uuid_command).?; |
| 961 | try bw.writeByte('\n'); | 954 | try writer.writeByte('\n'); |
| 962 | try bw.print("uuid {x}", .{&uuid.uuid}); | 955 | try writer.print("uuid {x}", .{&uuid.uuid}); |
| 963 | }, | 956 | }, |
| 964 | | 957 | |
| 965 | .DATA_IN_CODE, | 958 | .DATA_IN_CODE, |
| ... | @@ -967,8 +960,8 @@ const MachODumper = struct { | ... | @@ -967,8 +960,8 @@ const MachODumper = struct { |
| 967 | .CODE_SIGNATURE, | 960 | .CODE_SIGNATURE, |
| 968 | => { | 961 | => { |
| 969 | const llc = lc.cast(macho.linkedit_data_command).?; | 962 | const llc = lc.cast(macho.linkedit_data_command).?; |
| 970 | try bw.writeByte('\n'); | 963 | try writer.writeByte('\n'); |
| 971 | try bw.print( | 964 | try writer.print( |
| 972 | \\dataoff {x} | 965 | \\dataoff {x} |
| 973 | \\datasize {x} | 966 | \\datasize {x} |
| 974 | , .{ llc.dataoff, llc.datasize }); | 967 | , .{ llc.dataoff, llc.datasize }); |
| ... | @@ -976,8 +969,8 @@ const MachODumper = struct { | ... | @@ -976,8 +969,8 @@ const MachODumper = struct { |
| 976 | | 969 | |
| 977 | .DYLD_INFO_ONLY => { | 970 | .DYLD_INFO_ONLY => { |
| 978 | const dlc = lc.cast(macho.dyld_info_command).?; | 971 | const dlc = lc.cast(macho.dyld_info_command).?; |
| 979 | try bw.writeByte('\n'); | 972 | try writer.writeByte('\n'); |
| 980 | try bw.print( | 973 | try writer.print( |
| 981 | \\rebaseoff {x} | 974 | \\rebaseoff {x} |
| 982 | \\rebasesize {x} | 975 | \\rebasesize {x} |
| 983 | \\bindoff {x} | 976 | \\bindoff {x} |
| ... | @@ -1004,8 +997,8 @@ const MachODumper = struct { | ... | @@ -1004,8 +997,8 @@ const MachODumper = struct { |
| 1004 | | 997 | |
| 1005 | .SYMTAB => { | 998 | .SYMTAB => { |
| 1006 | const slc = lc.cast(macho.symtab_command).?; | 999 | const slc = lc.cast(macho.symtab_command).?; |
| 1007 | try bw.writeByte('\n'); | 1000 | try writer.writeByte('\n'); |
| 1008 | try bw.print( | 1001 | try writer.print( |
| 1009 | \\symoff {x} | 1002 | \\symoff {x} |
| 1010 | \\nsyms {x} | 1003 | \\nsyms {x} |
| 1011 | \\stroff {x} | 1004 | \\stroff {x} |
| ... | @@ -1020,8 +1013,8 @@ const MachODumper = struct { | ... | @@ -1020,8 +1013,8 @@ const MachODumper = struct { |
| 1020 | | 1013 | |
| 1021 | .DYSYMTAB => { | 1014 | .DYSYMTAB => { |
| 1022 | const dlc = lc.cast(macho.dysymtab_command).?; | 1015 | const dlc = lc.cast(macho.dysymtab_command).?; |
| 1023 | try bw.writeByte('\n'); | 1016 | try writer.writeByte('\n'); |
| 1024 | try bw.print( | 1017 | try writer.print( |
| 1025 | \\ilocalsym {x} | 1018 | \\ilocalsym {x} |
| 1026 | \\nlocalsym {x} | 1019 | \\nlocalsym {x} |
| 1027 | \\iextdefsym {x} | 1020 | \\iextdefsym {x} |
| ... | @@ -1044,8 +1037,8 @@ const MachODumper = struct { | ... | @@ -1044,8 +1037,8 @@ const MachODumper = struct { |
| 1044 | | 1037 | |
| 1045 | .BUILD_VERSION => { | 1038 | .BUILD_VERSION => { |
| 1046 | const blc = lc.cast(macho.build_version_command).?; | 1039 | const blc = lc.cast(macho.build_version_command).?; |
| 1047 | try bw.writeByte('\n'); | 1040 | try writer.writeByte('\n'); |
| 1048 | try bw.print( | 1041 | try writer.print( |
| 1049 | \\platform {s} | 1042 | \\platform {s} |
| 1050 | \\minos {d}.{d}.{d} | 1043 | \\minos {d}.{d}.{d} |
| 1051 | \\sdk {d}.{d}.{d} | 1044 | \\sdk {d}.{d}.{d} |
| ... | @@ -1061,12 +1054,12 @@ const MachODumper = struct { | ... | @@ -1061,12 +1054,12 @@ const MachODumper = struct { |
| 1061 | blc.ntools, | 1054 | blc.ntools, |
| 1062 | }); | 1055 | }); |
| 1063 | for (lc.getBuildVersionTools()) |tool| { | 1056 | for (lc.getBuildVersionTools()) |tool| { |
| 1064 | try bw.writeByte('\n'); | 1057 | try writer.writeByte('\n'); |
| 1065 | switch (tool.tool) { | 1058 | switch (tool.tool) { |
| 1066 | .CLANG, .SWIFT, .LD, .LLD, .ZIG => try bw.print("tool {s}\n", .{@tagName(tool.tool)}), | 1059 | .CLANG, .SWIFT, .LD, .LLD, .ZIG => try writer.print("tool {s}\n", .{@tagName(tool.tool)}), |
| 1067 | else => |x| try bw.print("tool {d}\n", .{@intFromEnum(x)}), | 1060 | else => |x| try writer.print("tool {d}\n", .{@intFromEnum(x)}), |
| 1068 | } | 1061 | } |
| 1069 | try bw.print( | 1062 | try writer.print( |
| 1070 | \\version {d}.{d}.{d} | 1063 | \\version {d}.{d}.{d} |
| 1071 | , .{ | 1064 | , .{ |
| 1072 | tool.version >> 16, | 1065 | tool.version >> 16, |
| ... | @@ -1082,8 +1075,8 @@ const MachODumper = struct { | ... | @@ -1082,8 +1075,8 @@ const MachODumper = struct { |
| 1082 | .VERSION_MIN_TVOS, | 1075 | .VERSION_MIN_TVOS, |
| 1083 | => { | 1076 | => { |
| 1084 | const vlc = lc.cast(macho.version_min_command).?; | 1077 | const vlc = lc.cast(macho.version_min_command).?; |
| 1085 | try bw.writeByte('\n'); | 1078 | try writer.writeByte('\n'); |
| 1086 | try bw.print( | 1079 | try writer.print( |
| 1087 | \\version {d}.{d}.{d} | 1080 | \\version {d}.{d}.{d} |
| 1088 | \\sdk {d}.{d}.{d} | 1081 | \\sdk {d}.{d}.{d} |
| 1089 | , .{ | 1082 | , .{ |
| ... | @@ -1100,8 +1093,8 @@ const MachODumper = struct { | ... | @@ -1100,8 +1093,8 @@ const MachODumper = struct { |
| 1100 | } | 1093 | } |
| 1101 | } | 1094 | } |
| 1102 | | 1095 | |
| 1103 | fn dumpSymtab(ctx: ObjectContext, bw: *Writer) !void { | 1096 | fn dumpSymtab(ctx: ObjectContext, writer: anytype) !void { |
| 1104 | try bw.writeAll(symtab_label ++ "\n"); | 1097 | try writer.writeAll(symtab_label ++ "\n"); |
| 1105 | | 1098 | |
| 1106 | for (ctx.symtab.items) |sym| { | 1099 | for (ctx.symtab.items) |sym| { |
| 1107 | const sym_name = ctx.getString(sym.n_strx); | 1100 | const sym_name = ctx.getString(sym.n_strx); |
| ... | @@ -1116,32 +1109,32 @@ const MachODumper = struct { | ... | @@ -1116,32 +1109,32 @@ const MachODumper = struct { |
| 1116 | macho.N_STSYM => "STSYM", | 1109 | macho.N_STSYM => "STSYM", |
| 1117 | else => "UNKNOWN STAB", | 1110 | else => "UNKNOWN STAB", |
| 1118 | }; | 1111 | }; |
| 1119 | try bw.print("{x}", .{sym.n_value}); | 1112 | try writer.print("{x}", .{sym.n_value}); |
| 1120 | if (sym.n_sect > 0) { | 1113 | if (sym.n_sect > 0) { |
| 1121 | const sect = ctx.sections.items[sym.n_sect - 1]; | 1114 | const sect = ctx.sections.items[sym.n_sect - 1]; |
| 1122 | try bw.print(" ({s},{s})", .{ sect.segName(), sect.sectName() }); | 1115 | try writer.print(" ({s},{s})", .{ sect.segName(), sect.sectName() }); |
| 1123 | } | 1116 | } |
| 1124 | try bw.print(" {s} (stab) {s}\n", .{ tt, sym_name }); | 1117 | try writer.print(" {s} (stab) {s}\n", .{ tt, sym_name }); |
| 1125 | } else if (sym.sect()) { | 1118 | } else if (sym.sect()) { |
| 1126 | const sect = ctx.sections.items[sym.n_sect - 1]; | 1119 | const sect = ctx.sections.items[sym.n_sect - 1]; |
| 1127 | try bw.print("{x} ({s},{s})", .{ | 1120 | try writer.print("{x} ({s},{s})", .{ |
| 1128 | sym.n_value, | 1121 | sym.n_value, |
| 1129 | sect.segName(), | 1122 | sect.segName(), |
| 1130 | sect.sectName(), | 1123 | sect.sectName(), |
| 1131 | }); | 1124 | }); |
| 1132 | if (sym.n_desc & macho.REFERENCED_DYNAMICALLY != 0) try bw.writeAll(" [referenced dynamically]"); | 1125 | if (sym.n_desc & macho.REFERENCED_DYNAMICALLY != 0) try writer.writeAll(" [referenced dynamically]"); |
| 1133 | if (sym.weakDef()) try bw.writeAll(" weak"); | 1126 | if (sym.weakDef()) try writer.writeAll(" weak"); |
| 1134 | if (sym.weakRef()) try bw.writeAll(" weakref"); | 1127 | if (sym.weakRef()) try writer.writeAll(" weakref"); |
| 1135 | if (sym.ext()) { | 1128 | if (sym.ext()) { |
| 1136 | if (sym.pext()) try bw.writeAll(" private"); | 1129 | if (sym.pext()) try writer.writeAll(" private"); |
| 1137 | try bw.writeAll(" external"); | 1130 | try writer.writeAll(" external"); |
| 1138 | } else if (sym.pext()) try bw.writeAll(" (was private external)"); | 1131 | } else if (sym.pext()) try writer.writeAll(" (was private external)"); |
| 1139 | try bw.print(" {s}\n", .{sym_name}); | 1132 | try writer.print(" {s}\n", .{sym_name}); |
| 1140 | } else if (sym.tentative()) { | 1133 | } else if (sym.tentative()) { |
| 1141 | const alignment = (sym.n_desc >> 8) & 0x0F; | 1134 | const alignment = (sym.n_desc >> 8) & 0x0F; |
| 1142 | try bw.print(" 0x{x:0>16} (common) (alignment 2^{d})", .{ sym.n_value, alignment }); | 1135 | try writer.print(" 0x{x:0>16} (common) (alignment 2^{d})", .{ sym.n_value, alignment }); |
| 1143 | if (sym.ext()) try bw.writeAll(" external"); | 1136 | if (sym.ext()) try writer.writeAll(" external"); |
| 1144 | try bw.print(" {s}\n", .{sym_name}); | 1137 | try writer.print(" {s}\n", .{sym_name}); |
| 1145 | } else if (sym.undf()) { | 1138 | } else if (sym.undf()) { |
| 1146 | const ordinal = @divFloor(@as(i16, @bitCast(sym.n_desc)), macho.N_SYMBOL_RESOLVER); | 1139 | const ordinal = @divFloor(@as(i16, @bitCast(sym.n_desc)), macho.N_SYMBOL_RESOLVER); |
| 1147 | const import_name = blk: { | 1140 | const import_name = blk: { |
| ... | @@ -1160,10 +1153,10 @@ const MachODumper = struct { | ... | @@ -1160,10 +1153,10 @@ const MachODumper = struct { |
| 1160 | const ext = mem.lastIndexOfScalar(u8, basename, '.') orelse basename.len; | 1153 | const ext = mem.lastIndexOfScalar(u8, basename, '.') orelse basename.len; |
| 1161 | break :blk basename[0..ext]; | 1154 | break :blk basename[0..ext]; |
| 1162 | }; | 1155 | }; |
| 1163 | try bw.writeAll("(undefined)"); | 1156 | try writer.writeAll("(undefined)"); |
| 1164 | if (sym.weakRef()) try bw.writeAll(" weakref"); | 1157 | if (sym.weakRef()) try writer.writeAll(" weakref"); |
| 1165 | if (sym.ext()) try bw.writeAll(" external"); | 1158 | if (sym.ext()) try writer.writeAll(" external"); |
| 1166 | try bw.print(" {s} (from {s})\n", .{ | 1159 | try writer.print(" {s} (from {s})\n", .{ |
| 1167 | sym_name, | 1160 | sym_name, |
| 1168 | import_name, | 1161 | import_name, |
| 1169 | }); | 1162 | }); |
| ... | @@ -1171,8 +1164,8 @@ const MachODumper = struct { | ... | @@ -1171,8 +1164,8 @@ const MachODumper = struct { |
| 1171 | } | 1164 | } |
| 1172 | } | 1165 | } |
| 1173 | | 1166 | |
| 1174 | fn dumpIndirectSymtab(ctx: ObjectContext, bw: *Writer) !void { | 1167 | fn dumpIndirectSymtab(ctx: ObjectContext, writer: anytype) !void { |
| 1175 | try bw.writeAll(indirect_symtab_label ++ "\n"); | 1168 | try writer.writeAll(indirect_symtab_label ++ "\n"); |
| 1176 | | 1169 | |
| 1177 | var sects_buffer: [3]macho.section_64 = undefined; | 1170 | var sects_buffer: [3]macho.section_64 = undefined; |
| 1178 | const sects = blk: { | 1171 | const sects = blk: { |
| ... | @@ -1210,33 +1203,35 @@ const MachODumper = struct { | ... | @@ -1210,33 +1203,35 @@ const MachODumper = struct { |
| 1210 | break :blk @sizeOf(u64); | 1203 | break :blk @sizeOf(u64); |
| 1211 | }; | 1204 | }; |
| 1212 | | 1205 | |
| 1213 | try bw.print("{s},{s}\n", .{ sect.segName(), sect.sectName() }); | 1206 | try writer.print("{s},{s}\n", .{ sect.segName(), sect.sectName() }); |
| 1214 | try bw.print("nentries {d}\n", .{end - start}); | 1207 | try writer.print("nentries {d}\n", .{end - start}); |
| 1215 | for (ctx.indsymtab.items[start..end], 0..) |index, j| { | 1208 | for (ctx.indsymtab.items[start..end], 0..) |index, j| { |
| 1216 | const sym = ctx.symtab.items[index]; | 1209 | const sym = ctx.symtab.items[index]; |
| 1217 | const addr = sect.addr + entry_size * j; | 1210 | const addr = sect.addr + entry_size * j; |
| 1218 | try bw.print("0x{x} {d} {s}\n", .{ addr, index, ctx.getString(sym.n_strx) }); | 1211 | try writer.print("0x{x} {d} {s}\n", .{ addr, index, ctx.getString(sym.n_strx) }); |
| 1219 | } | 1212 | } |
| 1220 | } | 1213 | } |
| 1221 | } | 1214 | } |
| 1222 | | 1215 | |
| 1223 | fn dumpRebaseInfo(ctx: ObjectContext, data: []const u8, bw: *Writer) !void { | 1216 | fn dumpRebaseInfo(ctx: ObjectContext, data: []const u8, writer: anytype) !void { |
| 1224 | var rebases: std.ArrayList(u64) = .init(ctx.gpa); | 1217 | var rebases = std.ArrayList(u64).init(ctx.gpa); |
| 1225 | defer rebases.deinit(); | 1218 | defer rebases.deinit(); |
| 1226 | try ctx.parseRebaseInfo(data, &rebases); | 1219 | try ctx.parseRebaseInfo(data, &rebases); |
| 1227 | mem.sort(u64, rebases.items, {}, std.sort.asc(u64)); | 1220 | mem.sort(u64, rebases.items, {}, std.sort.asc(u64)); |
| 1228 | for (rebases.items) |addr| { | 1221 | for (rebases.items) |addr| { |
| 1229 | try bw.print("0x{x}\n", .{addr}); | 1222 | try writer.print("0x{x}\n", .{addr}); |
| 1230 | } | 1223 | } |
| 1231 | } | 1224 | } |
| 1232 | | 1225 | |
| 1233 | fn parseRebaseInfo(ctx: ObjectContext, data: []const u8, rebases: *std.ArrayList(u64)) !void { | 1226 | fn parseRebaseInfo(ctx: ObjectContext, data: []const u8, rebases: *std.ArrayList(u64)) !void { |
| 1234 | var r: std.io.Reader = .fixed(data); | 1227 | var stream = std.io.fixedBufferStream(data); |
| | 1228 | var creader = std.io.countingReader(stream.reader()); |
| | 1229 | const reader = creader.reader(); |
| 1235 | | 1230 | |
| 1236 | var seg_id: ?u8 = null; | 1231 | var seg_id: ?u8 = null; |
| 1237 | var offset: u64 = 0; | 1232 | var offset: u64 = 0; |
| 1238 | while (true) { | 1233 | while (true) { |
| 1239 | const byte = r.takeByte() catch break; | 1234 | const byte = reader.readByte() catch break; |
| 1240 | const opc = byte & macho.REBASE_OPCODE_MASK; | 1235 | const opc = byte & macho.REBASE_OPCODE_MASK; |
| 1241 | const imm = byte & macho.REBASE_IMMEDIATE_MASK; | 1236 | const imm = byte & macho.REBASE_IMMEDIATE_MASK; |
| 1242 | switch (opc) { | 1237 | switch (opc) { |
| ... | @@ -1244,17 +1239,17 @@ const MachODumper = struct { | ... | @@ -1244,17 +1239,17 @@ const MachODumper = struct { |
| 1244 | macho.REBASE_OPCODE_SET_TYPE_IMM => {}, | 1239 | macho.REBASE_OPCODE_SET_TYPE_IMM => {}, |
| 1245 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB => { | 1240 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB => { |
| 1246 | seg_id = imm; | 1241 | seg_id = imm; |
| 1247 | offset = try r.takeLeb128(u64); | 1242 | offset = try std.leb.readUleb128(u64, reader); |
| 1248 | }, | 1243 | }, |
| 1249 | macho.REBASE_OPCODE_ADD_ADDR_IMM_SCALED => { | 1244 | macho.REBASE_OPCODE_ADD_ADDR_IMM_SCALED => { |
| 1250 | offset += imm * @sizeOf(u64); | 1245 | offset += imm * @sizeOf(u64); |
| 1251 | }, | 1246 | }, |
| 1252 | macho.REBASE_OPCODE_ADD_ADDR_ULEB => { | 1247 | macho.REBASE_OPCODE_ADD_ADDR_ULEB => { |
| 1253 | const addend = try r.takeLeb128(u64); | 1248 | const addend = try std.leb.readUleb128(u64, reader); |
| 1254 | offset += addend; | 1249 | offset += addend; |
| 1255 | }, | 1250 | }, |
| 1256 | macho.REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB => { | 1251 | macho.REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB => { |
| 1257 | const addend = try r.takeLeb128(u64); | 1252 | const addend = try std.leb.readUleb128(u64, reader); |
| 1258 | const seg = ctx.segments.items[seg_id.?]; | 1253 | const seg = ctx.segments.items[seg_id.?]; |
| 1259 | const addr = seg.vmaddr + offset; | 1254 | const addr = seg.vmaddr + offset; |
| 1260 | try rebases.append(addr); | 1255 | try rebases.append(addr); |
| ... | @@ -1271,11 +1266,11 @@ const MachODumper = struct { | ... | @@ -1271,11 +1266,11 @@ const MachODumper = struct { |
| 1271 | ntimes = imm; | 1266 | ntimes = imm; |
| 1272 | }, | 1267 | }, |
| 1273 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES => { | 1268 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES => { |
| 1274 | ntimes = try r.takeLeb128(u64); | 1269 | ntimes = try std.leb.readUleb128(u64, reader); |
| 1275 | }, | 1270 | }, |
| 1276 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB => { | 1271 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB => { |
| 1277 | ntimes = try r.takeLeb128(u64); | 1272 | ntimes = try std.leb.readUleb128(u64, reader); |
| 1278 | skip = try r.takeLeb128(u64); | 1273 | skip = try std.leb.readUleb128(u64, reader); |
| 1279 | }, | 1274 | }, |
| 1280 | else => unreachable, | 1275 | else => unreachable, |
| 1281 | } | 1276 | } |
| ... | @@ -1317,8 +1312,8 @@ const MachODumper = struct { | ... | @@ -1317,8 +1312,8 @@ const MachODumper = struct { |
| 1317 | }; | 1312 | }; |
| 1318 | }; | 1313 | }; |
| 1319 | | 1314 | |
| 1320 | fn dumpBindInfo(ctx: ObjectContext, data: []const u8, bw: *Writer) !void { | 1315 | fn dumpBindInfo(ctx: ObjectContext, data: []const u8, writer: anytype) !void { |
| 1321 | var bindings: std.ArrayList(Binding) = .init(ctx.gpa); | 1316 | var bindings = std.ArrayList(Binding).init(ctx.gpa); |
| 1322 | defer { | 1317 | defer { |
| 1323 | for (bindings.items) |*b| { | 1318 | for (bindings.items) |*b| { |
| 1324 | b.deinit(ctx.gpa); | 1319 | b.deinit(ctx.gpa); |
| ... | @@ -1328,20 +1323,22 @@ const MachODumper = struct { | ... | @@ -1328,20 +1323,22 @@ const MachODumper = struct { |
| 1328 | try ctx.parseBindInfo(data, &bindings); | 1323 | try ctx.parseBindInfo(data, &bindings); |
| 1329 | mem.sort(Binding, bindings.items, {}, Binding.lessThan); | 1324 | mem.sort(Binding, bindings.items, {}, Binding.lessThan); |
| 1330 | for (bindings.items) |binding| { | 1325 | for (bindings.items) |binding| { |
| 1331 | try bw.print("0x{x} [addend: {d}]", .{ binding.address, binding.addend }); | 1326 | try writer.print("0x{x} [addend: {d}]", .{ binding.address, binding.addend }); |
| 1332 | try bw.writeAll(" ("); | 1327 | try writer.writeAll(" ("); |
| 1333 | switch (binding.tag) { | 1328 | switch (binding.tag) { |
| 1334 | .self => try bw.writeAll("self"), | 1329 | .self => try writer.writeAll("self"), |
| 1335 | .exe => try bw.writeAll("main executable"), | 1330 | .exe => try writer.writeAll("main executable"), |
| 1336 | .flat => try bw.writeAll("flat lookup"), | 1331 | .flat => try writer.writeAll("flat lookup"), |
| 1337 | .ord => try bw.writeAll(std.fs.path.basename(ctx.imports.items[binding.ordinal - 1])), | 1332 | .ord => try writer.writeAll(std.fs.path.basename(ctx.imports.items[binding.ordinal - 1])), |
| 1338 | } | 1333 | } |
| 1339 | try bw.print(") {s}\n", .{binding.name}); | 1334 | try writer.print(") {s}\n", .{binding.name}); |
| 1340 | } | 1335 | } |
| 1341 | } | 1336 | } |
| 1342 | | 1337 | |
| 1343 | fn parseBindInfo(ctx: ObjectContext, data: []const u8, bindings: *std.ArrayList(Binding)) !void { | 1338 | fn parseBindInfo(ctx: ObjectContext, data: []const u8, bindings: *std.ArrayList(Binding)) !void { |
| 1344 | var r: std.io.Reader = .fixed(data); | 1339 | var stream = std.io.fixedBufferStream(data); |
| | 1340 | var creader = std.io.countingReader(stream.reader()); |
| | 1341 | const reader = creader.reader(); |
| 1345 | | 1342 | |
| 1346 | var seg_id: ?u8 = null; | 1343 | var seg_id: ?u8 = null; |
| 1347 | var tag: Binding.Tag = .self; | 1344 | var tag: Binding.Tag = .self; |
| ... | @@ -1349,10 +1346,11 @@ const MachODumper = struct { | ... | @@ -1349,10 +1346,11 @@ const MachODumper = struct { |
| 1349 | var offset: u64 = 0; | 1346 | var offset: u64 = 0; |
| 1350 | var addend: i64 = 0; | 1347 | var addend: i64 = 0; |
| 1351 | | 1348 | |
| 1352 | var name_buf: std.io.Writer.Allocating = .init(ctx.gpa); | 1349 | var name_buf = std.ArrayList(u8).init(ctx.gpa); |
| 1353 | defer name_buf.deinit(); | 1350 | defer name_buf.deinit(); |
| 1354 | | 1351 | |
| 1355 | while (r.takeByte()) |byte| { | 1352 | while (true) { |
| | 1353 | const byte = reader.readByte() catch break; |
| 1356 | const opc = byte & macho.BIND_OPCODE_MASK; | 1354 | const opc = byte & macho.BIND_OPCODE_MASK; |
| 1357 | const imm = byte & macho.BIND_IMMEDIATE_MASK; | 1355 | const imm = byte & macho.BIND_IMMEDIATE_MASK; |
| 1358 | switch (opc) { | 1356 | switch (opc) { |
| ... | @@ -1373,18 +1371,18 @@ const MachODumper = struct { | ... | @@ -1373,18 +1371,18 @@ const MachODumper = struct { |
| 1373 | }, | 1371 | }, |
| 1374 | macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB => { | 1372 | macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB => { |
| 1375 | seg_id = imm; | 1373 | seg_id = imm; |
| 1376 | offset = try r.takeLeb128(u64); | 1374 | offset = try std.leb.readUleb128(u64, reader); |
| 1377 | }, | 1375 | }, |
| 1378 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM => { | 1376 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM => { |
| 1379 | name_buf.clearRetainingCapacity(); | 1377 | name_buf.clearRetainingCapacity(); |
| 1380 | _ = try r.streamDelimiterLimit(&name_buf.writer, 0, .limited(std.math.maxInt(u32))); | 1378 | try reader.readUntilDelimiterArrayList(&name_buf, 0, std.math.maxInt(u32)); |
| 1381 | try name_buf.writer.writeByte(0); | 1379 | try name_buf.append(0); |
| 1382 | }, | 1380 | }, |
| 1383 | macho.BIND_OPCODE_SET_ADDEND_SLEB => { | 1381 | macho.BIND_OPCODE_SET_ADDEND_SLEB => { |
| 1384 | addend = try r.takeLeb128(i64); | 1382 | addend = try std.leb.readIleb128(i64, reader); |
| 1385 | }, | 1383 | }, |
| 1386 | macho.BIND_OPCODE_ADD_ADDR_ULEB => { | 1384 | macho.BIND_OPCODE_ADD_ADDR_ULEB => { |
| 1387 | const x = try r.takeLeb128(u64); | 1385 | const x = try std.leb.readUleb128(u64, reader); |
| 1388 | offset = @intCast(@as(i64, @intCast(offset)) + @as(i64, @bitCast(x))); | 1386 | offset = @intCast(@as(i64, @intCast(offset)) + @as(i64, @bitCast(x))); |
| 1389 | }, | 1387 | }, |
| 1390 | macho.BIND_OPCODE_DO_BIND, | 1388 | macho.BIND_OPCODE_DO_BIND, |
| ... | @@ -1399,14 +1397,14 @@ const MachODumper = struct { | ... | @@ -1399,14 +1397,14 @@ const MachODumper = struct { |
| 1399 | switch (opc) { | 1397 | switch (opc) { |
| 1400 | macho.BIND_OPCODE_DO_BIND => {}, | 1398 | macho.BIND_OPCODE_DO_BIND => {}, |
| 1401 | macho.BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB => { | 1399 | macho.BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB => { |
| 1402 | add_addr = try r.takeLeb128(u64); | 1400 | add_addr = try std.leb.readUleb128(u64, reader); |
| 1403 | }, | 1401 | }, |
| 1404 | macho.BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED => { | 1402 | macho.BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED => { |
| 1405 | add_addr = imm * @sizeOf(u64); | 1403 | add_addr = imm * @sizeOf(u64); |
| 1406 | }, | 1404 | }, |
| 1407 | macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB => { | 1405 | macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB => { |
| 1408 | count = try r.takeLeb128(u64); | 1406 | count = try std.leb.readUleb128(u64, reader); |
| 1409 | skip = try r.takeLeb128(u64); | 1407 | skip = try std.leb.readUleb128(u64, reader); |
| 1410 | }, | 1408 | }, |
| 1411 | else => unreachable, | 1409 | else => unreachable, |
| 1412 | } | 1410 | } |
| ... | @@ -1420,25 +1418,25 @@ const MachODumper = struct { | ... | @@ -1420,25 +1418,25 @@ const MachODumper = struct { |
| 1420 | .addend = addend, | 1418 | .addend = addend, |
| 1421 | .tag = tag, | 1419 | .tag = tag, |
| 1422 | .ordinal = ordinal, | 1420 | .ordinal = ordinal, |
| 1423 | .name = try ctx.gpa.dupe(u8, name_buf.getWritten()), | 1421 | .name = try ctx.gpa.dupe(u8, name_buf.items), |
| 1424 | }); | 1422 | }); |
| 1425 | offset += skip + @sizeOf(u64) + add_addr; | 1423 | offset += skip + @sizeOf(u64) + add_addr; |
| 1426 | } | 1424 | } |
| 1427 | }, | 1425 | }, |
| 1428 | else => break, | 1426 | else => break, |
| 1429 | } | 1427 | } |
| 1430 | } else |_| {} | 1428 | } |
| 1431 | } | 1429 | } |
| 1432 | | 1430 | |
| 1433 | fn dumpExportsTrie(ctx: ObjectContext, data: []const u8, bw: *Writer) !void { | 1431 | fn dumpExportsTrie(ctx: ObjectContext, data: []const u8, writer: anytype) !void { |
| 1434 | const seg = ctx.getSegmentByName("__TEXT") orelse return; | 1432 | const seg = ctx.getSegmentByName("__TEXT") orelse return; |
| 1435 | | 1433 | |
| 1436 | var arena = std.heap.ArenaAllocator.init(ctx.gpa); | 1434 | var arena = std.heap.ArenaAllocator.init(ctx.gpa); |
| 1437 | defer arena.deinit(); | 1435 | defer arena.deinit(); |
| 1438 | | 1436 | |
| 1439 | var exports: std.ArrayList(Export) = .init(arena.allocator()); | 1437 | var exports = std.ArrayList(Export).init(arena.allocator()); |
| 1440 | var r: std.io.Reader = .fixed(data); | 1438 | var it = TrieIterator{ .data = data }; |
| 1441 | try parseTrieNode(arena.allocator(), &r, "", &exports); | 1439 | try parseTrieNode(arena.allocator(), &it, "", &exports); |
| 1442 | | 1440 | |
| 1443 | mem.sort(Export, exports.items, {}, Export.lessThan); | 1441 | mem.sort(Export, exports.items, {}, Export.lessThan); |
| 1444 | | 1442 | |
| ... | @@ -1447,26 +1445,66 @@ const MachODumper = struct { | ... | @@ -1447,26 +1445,66 @@ const MachODumper = struct { |
| 1447 | .@"export" => { | 1445 | .@"export" => { |
| 1448 | const info = exp.data.@"export"; | 1446 | const info = exp.data.@"export"; |
| 1449 | if (info.kind != .regular or info.weak) { | 1447 | if (info.kind != .regular or info.weak) { |
| 1450 | try bw.writeByte('['); | 1448 | try writer.writeByte('['); |
| 1451 | } | 1449 | } |
| 1452 | switch (info.kind) { | 1450 | switch (info.kind) { |
| 1453 | .regular => {}, | 1451 | .regular => {}, |
| 1454 | .absolute => try bw.writeAll("ABS, "), | 1452 | .absolute => try writer.writeAll("ABS, "), |
| 1455 | .tlv => try bw.writeAll("THREAD_LOCAL, "), | 1453 | .tlv => try writer.writeAll("THREAD_LOCAL, "), |
| 1456 | } | 1454 | } |
| 1457 | if (info.weak) try bw.writeAll("WEAK"); | 1455 | if (info.weak) try writer.writeAll("WEAK"); |
| 1458 | if (info.kind != .regular or info.weak) { | 1456 | if (info.kind != .regular or info.weak) { |
| 1459 | try bw.writeAll("] "); | 1457 | try writer.writeAll("] "); |
| 1460 | } | 1458 | } |
| 1461 | try bw.print("{x} ", .{seg.vmaddr + info.vmoffset}); | 1459 | try writer.print("{x} ", .{seg.vmaddr + info.vmoffset}); |
| 1462 | }, | 1460 | }, |
| 1463 | else => {}, | 1461 | else => {}, |
| 1464 | } | 1462 | } |
| 1465 | | 1463 | |
| 1466 | try bw.print("{s}\n", .{exp.name}); | 1464 | try writer.print("{s}\n", .{exp.name}); |
| 1467 | } | 1465 | } |
| 1468 | } | 1466 | } |
| 1469 | | 1467 | |
| | 1468 | const TrieIterator = struct { |
| | 1469 | data: []const u8, |
| | 1470 | pos: usize = 0, |
| | 1471 | |
| | 1472 | fn getStream(it: *TrieIterator) std.io.FixedBufferStream([]const u8) { |
| | 1473 | return std.io.fixedBufferStream(it.data[it.pos..]); |
| | 1474 | } |
| | 1475 | |
| | 1476 | fn readUleb128(it: *TrieIterator) !u64 { |
| | 1477 | var stream = it.getStream(); |
| | 1478 | var creader = std.io.countingReader(stream.reader()); |
| | 1479 | const reader = creader.reader(); |
| | 1480 | const value = try std.leb.readUleb128(u64, reader); |
| | 1481 | it.pos += creader.bytes_read; |
| | 1482 | return value; |
| | 1483 | } |
| | 1484 | |
| | 1485 | fn readString(it: *TrieIterator) ![:0]const u8 { |
| | 1486 | var stream = it.getStream(); |
| | 1487 | const reader = stream.reader(); |
| | 1488 | |
| | 1489 | var count: usize = 0; |
| | 1490 | while (true) : (count += 1) { |
| | 1491 | const byte = try reader.readByte(); |
| | 1492 | if (byte == 0) break; |
| | 1493 | } |
| | 1494 | |
| | 1495 | const str = @as([*:0]const u8, @ptrCast(it.data.ptr + it.pos))[0..count :0]; |
| | 1496 | it.pos += count + 1; |
| | 1497 | return str; |
| | 1498 | } |
| | 1499 | |
| | 1500 | fn readByte(it: *TrieIterator) !u8 { |
| | 1501 | var stream = it.getStream(); |
| | 1502 | const value = try stream.reader().readByte(); |
| | 1503 | it.pos += 1; |
| | 1504 | return value; |
| | 1505 | } |
| | 1506 | }; |
| | 1507 | |
| 1470 | const Export = struct { | 1508 | const Export = struct { |
| 1471 | name: []const u8, | 1509 | name: []const u8, |
| 1472 | tag: enum { @"export", reexport, stub_resolver }, | 1510 | tag: enum { @"export", reexport, stub_resolver }, |
| ... | @@ -1506,17 +1544,17 @@ const MachODumper = struct { | ... | @@ -1506,17 +1544,17 @@ const MachODumper = struct { |
| 1506 | | 1544 | |
| 1507 | fn parseTrieNode( | 1545 | fn parseTrieNode( |
| 1508 | arena: Allocator, | 1546 | arena: Allocator, |
| 1509 | r: *std.io.Reader, | 1547 | it: *TrieIterator, |
| 1510 | prefix: []const u8, | 1548 | prefix: []const u8, |
| 1511 | exports: *std.ArrayList(Export), | 1549 | exports: *std.ArrayList(Export), |
| 1512 | ) !void { | 1550 | ) !void { |
| 1513 | const size = try r.takeLeb128(u64); | 1551 | const size = try it.readUleb128(); |
| 1514 | if (size > 0) { | 1552 | if (size > 0) { |
| 1515 | const flags = try r.takeLeb128(u8); | 1553 | const flags = try it.readUleb128(); |
| 1516 | switch (flags) { | 1554 | switch (flags) { |
| 1517 | macho.EXPORT_SYMBOL_FLAGS_REEXPORT => { | 1555 | macho.EXPORT_SYMBOL_FLAGS_REEXPORT => { |
| 1518 | const ord = try r.takeLeb128(u64); | 1556 | const ord = try it.readUleb128(); |
| 1519 | const name = try r.takeSentinel(0); | 1557 | const name = try arena.dupe(u8, try it.readString()); |
| 1520 | try exports.append(.{ | 1558 | try exports.append(.{ |
| 1521 | .name = if (name.len > 0) name else prefix, | 1559 | .name = if (name.len > 0) name else prefix, |
| 1522 | .tag = .reexport, | 1560 | .tag = .reexport, |
| ... | @@ -1524,8 +1562,8 @@ const MachODumper = struct { | ... | @@ -1524,8 +1562,8 @@ const MachODumper = struct { |
| 1524 | }); | 1562 | }); |
| 1525 | }, | 1563 | }, |
| 1526 | macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER => { | 1564 | macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER => { |
| 1527 | const stub_offset = try r.takeLeb128(u64); | 1565 | const stub_offset = try it.readUleb128(); |
| 1528 | const resolver_offset = try r.takeLeb128(u64); | 1566 | const resolver_offset = try it.readUleb128(); |
| 1529 | try exports.append(.{ | 1567 | try exports.append(.{ |
| 1530 | .name = prefix, | 1568 | .name = prefix, |
| 1531 | .tag = .stub_resolver, | 1569 | .tag = .stub_resolver, |
| ... | @@ -1536,7 +1574,7 @@ const MachODumper = struct { | ... | @@ -1536,7 +1574,7 @@ const MachODumper = struct { |
| 1536 | }); | 1574 | }); |
| 1537 | }, | 1575 | }, |
| 1538 | else => { | 1576 | else => { |
| 1539 | const vmoff = try r.takeLeb128(u64); | 1577 | const vmoff = try it.readUleb128(); |
| 1540 | try exports.append(.{ | 1578 | try exports.append(.{ |
| 1541 | .name = prefix, | 1579 | .name = prefix, |
| 1542 | .tag = .@"export", | 1580 | .tag = .@"export", |
| ... | @@ -1555,21 +1593,21 @@ const MachODumper = struct { | ... | @@ -1555,21 +1593,21 @@ const MachODumper = struct { |
| 1555 | } | 1593 | } |
| 1556 | } | 1594 | } |
| 1557 | | 1595 | |
| 1558 | const nedges = try r.takeByte(); | 1596 | const nedges = try it.readByte(); |
| 1559 | for (0..nedges) |_| { | 1597 | for (0..nedges) |_| { |
| 1560 | const label = try r.takeSentinel(0); | 1598 | const label = try it.readString(); |
| 1561 | const off = try r.takeLeb128(usize); | 1599 | const off = try it.readUleb128(); |
| 1562 | const prefix_label = try std.fmt.allocPrint(arena, "{s}{s}", .{ prefix, label }); | 1600 | const prefix_label = try std.fmt.allocPrint(arena, "{s}{s}", .{ prefix, label }); |
| 1563 | const seek = r.seek; | 1601 | const curr = it.pos; |
| 1564 | r.seek = off; | 1602 | it.pos = off; |
| 1565 | try parseTrieNode(arena, r, prefix_label, exports); | 1603 | try parseTrieNode(arena, it, prefix_label, exports); |
| 1566 | r.seek = seek; | 1604 | it.pos = curr; |
| 1567 | } | 1605 | } |
| 1568 | } | 1606 | } |
| 1569 | | 1607 | |
| 1570 | fn dumpSection(ctx: ObjectContext, sect: macho.section_64, bw: *Writer) !void { | 1608 | fn dumpSection(ctx: ObjectContext, sect: macho.section_64, writer: anytype) !void { |
| 1571 | const data = ctx.data[sect.offset..][0..sect.size]; | 1609 | const data = ctx.data[sect.offset..][0..sect.size]; |
| 1572 | try bw.print("{s}", .{data}); | 1610 | try writer.print("{s}", .{data}); |
| 1573 | } | 1611 | } |
| 1574 | }; | 1612 | }; |
| 1575 | | 1613 | |
| ... | @@ -1583,30 +1621,29 @@ const MachODumper = struct { | ... | @@ -1583,30 +1621,29 @@ const MachODumper = struct { |
| 1583 | var ctx = ObjectContext{ .gpa = gpa, .data = bytes, .header = hdr }; | 1621 | var ctx = ObjectContext{ .gpa = gpa, .data = bytes, .header = hdr }; |
| 1584 | try ctx.parse(); | 1622 | try ctx.parse(); |
| 1585 | | 1623 | |
| 1586 | var aw: std.io.Writer.Allocating = .init(gpa); | 1624 | var output = std.ArrayList(u8).init(gpa); |
| 1587 | defer aw.deinit(); | 1625 | const writer = output.writer(); |
| 1588 | const bw = &aw.writer; | | |
| 1589 | | 1626 | |
| 1590 | switch (check.kind) { | 1627 | switch (check.kind) { |
| 1591 | .headers => { | 1628 | .headers => { |
| 1592 | try ObjectContext.dumpHeader(ctx.header, bw); | 1629 | try ObjectContext.dumpHeader(ctx.header, writer); |
| 1593 | | 1630 | |
| 1594 | var it = ctx.getLoadCommandIterator(); | 1631 | var it = ctx.getLoadCommandIterator(); |
| 1595 | var i: usize = 0; | 1632 | var i: usize = 0; |
| 1596 | while (it.next()) |cmd| { | 1633 | while (it.next()) |cmd| { |
| 1597 | try ObjectContext.dumpLoadCommand(cmd, i, bw); | 1634 | try ObjectContext.dumpLoadCommand(cmd, i, writer); |
| 1598 | try bw.writeByte('\n'); | 1635 | try writer.writeByte('\n'); |
| 1599 | | 1636 | |
| 1600 | i += 1; | 1637 | i += 1; |
| 1601 | } | 1638 | } |
| 1602 | }, | 1639 | }, |
| 1603 | | 1640 | |
| 1604 | .symtab => if (ctx.symtab.items.len > 0) { | 1641 | .symtab => if (ctx.symtab.items.len > 0) { |
| 1605 | try ctx.dumpSymtab(bw); | 1642 | try ctx.dumpSymtab(writer); |
| 1606 | } else return step.fail("no symbol table found", .{}), | 1643 | } else return step.fail("no symbol table found", .{}), |
| 1607 | | 1644 | |
| 1608 | .indirect_symtab => if (ctx.symtab.items.len > 0 and ctx.indsymtab.items.len > 0) { | 1645 | .indirect_symtab => if (ctx.symtab.items.len > 0 and ctx.indsymtab.items.len > 0) { |
| 1609 | try ctx.dumpIndirectSymtab(bw); | 1646 | try ctx.dumpIndirectSymtab(writer); |
| 1610 | } else return step.fail("no indirect symbol table found", .{}), | 1647 | } else return step.fail("no indirect symbol table found", .{}), |
| 1611 | | 1648 | |
| 1612 | .dyld_rebase, | 1649 | .dyld_rebase, |
| ... | @@ -1621,26 +1658,26 @@ const MachODumper = struct { | ... | @@ -1621,26 +1658,26 @@ const MachODumper = struct { |
| 1621 | switch (check.kind) { | 1658 | switch (check.kind) { |
| 1622 | .dyld_rebase => if (lc.rebase_size > 0) { | 1659 | .dyld_rebase => if (lc.rebase_size > 0) { |
| 1623 | const data = ctx.data[lc.rebase_off..][0..lc.rebase_size]; | 1660 | const data = ctx.data[lc.rebase_off..][0..lc.rebase_size]; |
| 1624 | try bw.writeAll(dyld_rebase_label ++ "\n"); | 1661 | try writer.writeAll(dyld_rebase_label ++ "\n"); |
| 1625 | try ctx.dumpRebaseInfo(data, bw); | 1662 | try ctx.dumpRebaseInfo(data, writer); |
| 1626 | } else return step.fail("no rebase data found", .{}), | 1663 | } else return step.fail("no rebase data found", .{}), |
| 1627 | | 1664 | |
| 1628 | .dyld_bind => if (lc.bind_size > 0) { | 1665 | .dyld_bind => if (lc.bind_size > 0) { |
| 1629 | const data = ctx.data[lc.bind_off..][0..lc.bind_size]; | 1666 | const data = ctx.data[lc.bind_off..][0..lc.bind_size]; |
| 1630 | try bw.writeAll(dyld_bind_label ++ "\n"); | 1667 | try writer.writeAll(dyld_bind_label ++ "\n"); |
| 1631 | try ctx.dumpBindInfo(data, bw); | 1668 | try ctx.dumpBindInfo(data, writer); |
| 1632 | } else return step.fail("no bind data found", .{}), | 1669 | } else return step.fail("no bind data found", .{}), |
| 1633 | | 1670 | |
| 1634 | .dyld_weak_bind => if (lc.weak_bind_size > 0) { | 1671 | .dyld_weak_bind => if (lc.weak_bind_size > 0) { |
| 1635 | const data = ctx.data[lc.weak_bind_off..][0..lc.weak_bind_size]; | 1672 | const data = ctx.data[lc.weak_bind_off..][0..lc.weak_bind_size]; |
| 1636 | try bw.writeAll(dyld_weak_bind_label ++ "\n"); | 1673 | try writer.writeAll(dyld_weak_bind_label ++ "\n"); |
| 1637 | try ctx.dumpBindInfo(data, bw); | 1674 | try ctx.dumpBindInfo(data, writer); |
| 1638 | } else return step.fail("no weak bind data found", .{}), | 1675 | } else return step.fail("no weak bind data found", .{}), |
| 1639 | | 1676 | |
| 1640 | .dyld_lazy_bind => if (lc.lazy_bind_size > 0) { | 1677 | .dyld_lazy_bind => if (lc.lazy_bind_size > 0) { |
| 1641 | const data = ctx.data[lc.lazy_bind_off..][0..lc.lazy_bind_size]; | 1678 | const data = ctx.data[lc.lazy_bind_off..][0..lc.lazy_bind_size]; |
| 1642 | try bw.writeAll(dyld_lazy_bind_label ++ "\n"); | 1679 | try writer.writeAll(dyld_lazy_bind_label ++ "\n"); |
| 1643 | try ctx.dumpBindInfo(data, bw); | 1680 | try ctx.dumpBindInfo(data, writer); |
| 1644 | } else return step.fail("no lazy bind data found", .{}), | 1681 | } else return step.fail("no lazy bind data found", .{}), |
| 1645 | | 1682 | |
| 1646 | else => unreachable, | 1683 | else => unreachable, |
| ... | @@ -1652,8 +1689,8 @@ const MachODumper = struct { | ... | @@ -1652,8 +1689,8 @@ const MachODumper = struct { |
| 1652 | const lc = cmd.cast(macho.dyld_info_command).?; | 1689 | const lc = cmd.cast(macho.dyld_info_command).?; |
| 1653 | if (lc.export_size > 0) { | 1690 | if (lc.export_size > 0) { |
| 1654 | const data = ctx.data[lc.export_off..][0..lc.export_size]; | 1691 | const data = ctx.data[lc.export_off..][0..lc.export_size]; |
| 1655 | try bw.writeAll(exports_label ++ "\n"); | 1692 | try writer.writeAll(exports_label ++ "\n"); |
| 1656 | try ctx.dumpExportsTrie(data, bw); | 1693 | try ctx.dumpExportsTrie(data, writer); |
| 1657 | break :blk; | 1694 | break :blk; |
| 1658 | } | 1695 | } |
| 1659 | } | 1696 | } |
| ... | @@ -1661,20 +1698,20 @@ const MachODumper = struct { | ... | @@ -1661,20 +1698,20 @@ const MachODumper = struct { |
| 1661 | }, | 1698 | }, |
| 1662 | | 1699 | |
| 1663 | .dump_section => { | 1700 | .dump_section => { |
| 1664 | const name = mem.sliceTo(@as([*:0]const u8, @ptrCast(check.data.items[check.payload.dump_section..].ptr)), 0); | 1701 | const name = mem.sliceTo(@as([*:0]const u8, @ptrCast(check.data.items.ptr + check.payload.dump_section)), 0); |
| 1665 | const sep_index = mem.indexOfScalar(u8, name, ',') orelse | 1702 | const sep_index = mem.indexOfScalar(u8, name, ',') orelse |
| 1666 | return step.fail("invalid section name: {s}", .{name}); | 1703 | return step.fail("invalid section name: {s}", .{name}); |
| 1667 | const segname = name[0..sep_index]; | 1704 | const segname = name[0..sep_index]; |
| 1668 | const sectname = name[sep_index + 1 ..]; | 1705 | const sectname = name[sep_index + 1 ..]; |
| 1669 | const sect = ctx.getSectionByName(segname, sectname) orelse | 1706 | const sect = ctx.getSectionByName(segname, sectname) orelse |
| 1670 | return step.fail("section '{s}' not found", .{name}); | 1707 | return step.fail("section '{s}' not found", .{name}); |
| 1671 | try ctx.dumpSection(sect, bw); | 1708 | try ctx.dumpSection(sect, writer); |
| 1672 | }, | 1709 | }, |
| 1673 | | 1710 | |
| 1674 | else => return step.fail("invalid check kind for MachO file format: {s}", .{@tagName(check.kind)}), | 1711 | else => return step.fail("invalid check kind for MachO file format: {s}", .{@tagName(check.kind)}), |
| 1675 | } | 1712 | } |
| 1676 | | 1713 | |
| 1677 | return aw.toOwnedSlice(); | 1714 | return output.toOwnedSlice(); |
| 1678 | } | 1715 | } |
| 1679 | }; | 1716 | }; |
| 1680 | | 1717 | |
| ... | @@ -1693,138 +1730,161 @@ const ElfDumper = struct { | ... | @@ -1693,138 +1730,161 @@ const ElfDumper = struct { |
| 1693 | | 1730 | |
| 1694 | fn parseAndDumpArchive(step: *Step, check: Check, bytes: []const u8) ![]const u8 { | 1731 | fn parseAndDumpArchive(step: *Step, check: Check, bytes: []const u8) ![]const u8 { |
| 1695 | const gpa = step.owner.allocator; | 1732 | const gpa = step.owner.allocator; |
| 1696 | var r: std.io.Reader = .fixed(bytes); | 1733 | var stream = std.io.fixedBufferStream(bytes); |
| | 1734 | const reader = stream.reader(); |
| 1697 | | 1735 | |
| 1698 | if (!mem.eql(u8, try r.takeArray(elf.ARMAG.len), elf.ARMAG)) return error.InvalidArchiveMagicNumber; | 1736 | const magic = try reader.readBytesNoEof(elf.ARMAG.len); |
| | 1737 | if (!mem.eql(u8, &magic, elf.ARMAG)) { |
| | 1738 | return error.InvalidArchiveMagicNumber; |
| | 1739 | } |
| 1699 | | 1740 | |
| 1700 | var ctx: ArchiveContext = .{ | 1741 | var ctx = ArchiveContext{ |
| 1701 | .gpa = gpa, | 1742 | .gpa = gpa, |
| 1702 | .data = bytes, | 1743 | .data = bytes, |
| 1703 | .symtab = &.{}, | 1744 | .strtab = &[0]u8{}, |
| 1704 | .strtab = &.{}, | | |
| 1705 | .objects = .empty, | | |
| 1706 | }; | 1745 | }; |
| 1707 | defer ctx.deinit(); | 1746 | defer { |
| | 1747 | for (ctx.objects.items) |*object| { |
| | 1748 | gpa.free(object.name); |
| | 1749 | } |
| | 1750 | ctx.objects.deinit(gpa); |
| | 1751 | } |
| | 1752 | |
| | 1753 | while (true) { |
| | 1754 | if (stream.pos >= ctx.data.len) break; |
| | 1755 | if (!mem.isAligned(stream.pos, 2)) stream.pos += 1; |
| 1708 | | 1756 | |
| 1709 | while (r.seek < bytes.len) { | 1757 | const hdr = try reader.readStruct(elf.ar_hdr); |
| 1710 | const hdr_seek = std.mem.alignForward(usize, r.seek, 2); | | |
| 1711 | r.seek = hdr_seek; | | |
| 1712 | const hdr = try r.takeStruct(elf.ar_hdr); | | |
| 1713 | | 1758 | |
| 1714 | if (!mem.eql(u8, &hdr.ar_fmag, elf.ARFMAG)) return error.InvalidArchiveHeaderMagicNumber; | 1759 | if (!mem.eql(u8, &hdr.ar_fmag, elf.ARFMAG)) return error.InvalidArchiveHeaderMagicNumber; |
| 1715 | | 1760 | |
| 1716 | const data = try r.take(try hdr.size()); | 1761 | const size = try hdr.size(); |
| | 1762 | defer { |
| | 1763 | _ = stream.seekBy(size) catch {}; |
| | 1764 | } |
| 1717 | | 1765 | |
| 1718 | if (hdr.isSymtab()) { | 1766 | if (hdr.isSymtab()) { |
| 1719 | try ctx.parseSymtab(data, .p32); | 1767 | try ctx.parseSymtab(ctx.data[stream.pos..][0..size], .p32); |
| 1720 | continue; | 1768 | continue; |
| 1721 | } | 1769 | } |
| 1722 | if (hdr.isSymtab64()) { | 1770 | if (hdr.isSymtab64()) { |
| 1723 | try ctx.parseSymtab(data, .p64); | 1771 | try ctx.parseSymtab(ctx.data[stream.pos..][0..size], .p64); |
| 1724 | continue; | 1772 | continue; |
| 1725 | } | 1773 | } |
| 1726 | if (hdr.isStrtab()) { | 1774 | if (hdr.isStrtab()) { |
| 1727 | ctx.strtab = data; | 1775 | ctx.strtab = ctx.data[stream.pos..][0..size]; |
| 1728 | continue; | 1776 | continue; |
| 1729 | } | 1777 | } |
| 1730 | if (hdr.isSymdef() or hdr.isSymdefSorted()) continue; | 1778 | if (hdr.isSymdef() or hdr.isSymdefSorted()) continue; |
| 1731 | | 1779 | |
| 1732 | const name = hdr.name() orelse ctx.getString((try hdr.nameOffset()).?); | 1780 | const name = if (hdr.name()) |name| |
| 1733 | try ctx.objects.putNoClobber(gpa, hdr_seek, .{ | 1781 | try gpa.dupe(u8, name) |
| 1734 | .name = name, | 1782 | else if (try hdr.nameOffset()) |off| |
| 1735 | .data = data, | 1783 | try gpa.dupe(u8, ctx.getString(off)) |
| 1736 | }); | 1784 | else |
| | 1785 | unreachable; |
| | 1786 | |
| | 1787 | try ctx.objects.append(gpa, .{ .name = name, .off = stream.pos, .len = size }); |
| 1737 | } | 1788 | } |
| 1738 | | 1789 | |
| 1739 | var aw: std.io.Writer.Allocating = .init(gpa); | 1790 | var output = std.ArrayList(u8).init(gpa); |
| 1740 | defer aw.deinit(); | 1791 | const writer = output.writer(); |
| 1741 | const bw = &aw.writer; | | |
| 1742 | | 1792 | |
| 1743 | switch (check.kind) { | 1793 | switch (check.kind) { |
| 1744 | .archive_symtab => if (ctx.symtab.len > 0) { | 1794 | .archive_symtab => if (ctx.symtab.items.len > 0) { |
| 1745 | try ctx.dumpSymtab(bw); | 1795 | try ctx.dumpSymtab(writer); |
| 1746 | } else return step.fail("no archive symbol table found", .{}), | 1796 | } else return step.fail("no archive symbol table found", .{}), |
| 1747 | | 1797 | |
| 1748 | else => if (ctx.objects.count() > 0) { | 1798 | else => if (ctx.objects.items.len > 0) { |
| 1749 | try ctx.dumpObjects(step, check, bw); | 1799 | try ctx.dumpObjects(step, check, writer); |
| 1750 | } else return step.fail("empty archive", .{}), | 1800 | } else return step.fail("empty archive", .{}), |
| 1751 | } | 1801 | } |
| 1752 | | 1802 | |
| 1753 | return aw.toOwnedSlice(); | 1803 | return output.toOwnedSlice(); |
| 1754 | } | 1804 | } |
| 1755 | | 1805 | |
| 1756 | const ArchiveContext = struct { | 1806 | const ArchiveContext = struct { |
| 1757 | gpa: Allocator, | 1807 | gpa: Allocator, |
| 1758 | data: []const u8, | 1808 | data: []const u8, |
| 1759 | symtab: []ArSymtabEntry, | 1809 | symtab: std.ArrayListUnmanaged(ArSymtabEntry) = .empty, |
| 1760 | strtab: []const u8, | 1810 | strtab: []const u8, |
| 1761 | objects: std.AutoArrayHashMapUnmanaged(usize, struct { name: []const u8, data: []const u8 }), | 1811 | objects: std.ArrayListUnmanaged(struct { name: []const u8, off: usize, len: usize }) = .empty, |
| 1762 | | 1812 | |
| 1763 | fn deinit(ctx: *ArchiveContext) void { | 1813 | fn parseSymtab(ctx: *ArchiveContext, raw: []const u8, ptr_width: enum { p32, p64 }) !void { |
| 1764 | ctx.gpa.free(ctx.symtab); | 1814 | var stream = std.io.fixedBufferStream(raw); |
| 1765 | ctx.objects.deinit(ctx.gpa); | 1815 | const reader = stream.reader(); |
| 1766 | } | | |
| 1767 | | | |
| 1768 | fn parseSymtab(ctx: *ArchiveContext, data: []const u8, ptr_width: enum { p32, p64 }) !void { | | |
| 1769 | var r: std.io.Reader = .fixed(data); | | |
| 1770 | const num = switch (ptr_width) { | 1816 | const num = switch (ptr_width) { |
| 1771 | .p32 => try r.takeInt(u32, .big), | 1817 | .p32 => try reader.readInt(u32, .big), |
| 1772 | .p64 => try r.takeInt(u64, .big), | 1818 | .p64 => try reader.readInt(u64, .big), |
| 1773 | }; | 1819 | }; |
| 1774 | const ptr_size: usize = switch (ptr_width) { | 1820 | const ptr_size: usize = switch (ptr_width) { |
| 1775 | .p32 => @sizeOf(u32), | 1821 | .p32 => @sizeOf(u32), |
| 1776 | .p64 => @sizeOf(u64), | 1822 | .p64 => @sizeOf(u64), |
| 1777 | }; | 1823 | }; |
| 1778 | _ = try r.discard(.limited(num * ptr_size)); | 1824 | const strtab_off = (num + 1) * ptr_size; |
| 1779 | const strtab = r.buffered(); | 1825 | const strtab_len = raw.len - strtab_off; |
| | 1826 | const strtab = raw[strtab_off..][0..strtab_len]; |
| 1780 | | 1827 | |
| 1781 | assert(ctx.symtab.len == 0); | 1828 | try ctx.symtab.ensureTotalCapacityPrecise(ctx.gpa, num); |
| 1782 | ctx.symtab = try ctx.gpa.alloc(ArSymtabEntry, num); | | |
| 1783 | | 1829 | |
| 1784 | var stroff: usize = 0; | 1830 | var stroff: usize = 0; |
| 1785 | for (ctx.symtab) |*entry| { | 1831 | for (0..num) |_| { |
| 1786 | const off = switch (ptr_width) { | 1832 | const off = switch (ptr_width) { |
| 1787 | .p32 => try r.takeInt(u32, .big), | 1833 | .p32 => try reader.readInt(u32, .big), |
| 1788 | .p64 => try r.takeInt(u64, .big), | 1834 | .p64 => try reader.readInt(u64, .big), |
| 1789 | }; | 1835 | }; |
| 1790 | const name = mem.sliceTo(@as([*:0]const u8, @ptrCast(strtab[stroff..].ptr)), 0); | 1836 | const name = mem.sliceTo(@as([*:0]const u8, @ptrCast(strtab.ptr + stroff)), 0); |
| 1791 | stroff += name.len + 1; | 1837 | stroff += name.len + 1; |
| 1792 | entry.* = .{ .off = off, .name = name }; | 1838 | ctx.symtab.appendAssumeCapacity(.{ .off = off, .name = name }); |
| 1793 | } | 1839 | } |
| 1794 | } | 1840 | } |
| 1795 | | 1841 | |
| 1796 | fn dumpSymtab(ctx: ArchiveContext, bw: *Writer) !void { | 1842 | fn dumpSymtab(ctx: ArchiveContext, writer: anytype) !void { |
| 1797 | var symbols: std.AutoArrayHashMap(usize, std.ArrayList([]const u8)) = .init(ctx.gpa); | 1843 | var files = std.AutoHashMap(usize, []const u8).init(ctx.gpa); |
| | 1844 | defer files.deinit(); |
| | 1845 | try files.ensureUnusedCapacity(@intCast(ctx.objects.items.len)); |
| | 1846 | |
| | 1847 | for (ctx.objects.items) |object| { |
| | 1848 | files.putAssumeCapacityNoClobber(object.off - @sizeOf(elf.ar_hdr), object.name); |
| | 1849 | } |
| | 1850 | |
| | 1851 | var symbols = std.AutoArrayHashMap(usize, std.ArrayList([]const u8)).init(ctx.gpa); |
| 1798 | defer { | 1852 | defer { |
| 1799 | for (symbols.values()) |*value| value.deinit(); | 1853 | for (symbols.values()) |*value| { |
| | 1854 | value.deinit(); |
| | 1855 | } |
| 1800 | symbols.deinit(); | 1856 | symbols.deinit(); |
| 1801 | } | 1857 | } |
| 1802 | | 1858 | |
| 1803 | for (ctx.symtab) |entry| { | 1859 | for (ctx.symtab.items) |entry| { |
| 1804 | const gop = try symbols.getOrPut(@intCast(entry.off)); | 1860 | const gop = try symbols.getOrPut(@intCast(entry.off)); |
| 1805 | if (!gop.found_existing) gop.value_ptr.* = .init(ctx.gpa); | 1861 | if (!gop.found_existing) { |
| | 1862 | gop.value_ptr.* = std.ArrayList([]const u8).init(ctx.gpa); |
| | 1863 | } |
| 1806 | try gop.value_ptr.append(entry.name); | 1864 | try gop.value_ptr.append(entry.name); |
| 1807 | } | 1865 | } |
| 1808 | | 1866 | |
| 1809 | try bw.print("{s}\n", .{archive_symtab_label}); | 1867 | try writer.print("{s}\n", .{archive_symtab_label}); |
| 1810 | for (symbols.keys(), symbols.values()) |off, values| { | 1868 | for (symbols.keys(), symbols.values()) |off, values| { |
| 1811 | try bw.print("in object {s}\n", .{ctx.objects.get(off).?.name}); | 1869 | try writer.print("in object {s}\n", .{files.get(off).?}); |
| 1812 | for (values.items) |value| try bw.print("{s}\n", .{value}); | 1870 | for (values.items) |value| { |
| | 1871 | try writer.print("{s}\n", .{value}); |
| | 1872 | } |
| 1813 | } | 1873 | } |
| 1814 | } | 1874 | } |
| 1815 | | 1875 | |
| 1816 | fn dumpObjects(ctx: ArchiveContext, step: *Step, check: Check, bw: *Writer) !void { | 1876 | fn dumpObjects(ctx: ArchiveContext, step: *Step, check: Check, writer: anytype) !void { |
| 1817 | for (ctx.objects.values()) |object| { | 1877 | for (ctx.objects.items) |object| { |
| 1818 | try bw.print("object {s}\n", .{object.name}); | 1878 | try writer.print("object {s}\n", .{object.name}); |
| 1819 | const output = try parseAndDumpObject(step, check, object.data); | 1879 | const output = try parseAndDumpObject(step, check, ctx.data[object.off..][0..object.len]); |
| 1820 | defer ctx.gpa.free(output); | 1880 | defer ctx.gpa.free(output); |
| 1821 | try bw.print("{s}\n", .{output}); | 1881 | try writer.print("{s}\n", .{output}); |
| 1822 | } | 1882 | } |
| 1823 | } | 1883 | } |
| 1824 | | 1884 | |
| 1825 | fn getString(ctx: ArchiveContext, off: u32) []const u8 { | 1885 | fn getString(ctx: ArchiveContext, off: u32) []const u8 { |
| 1826 | assert(off < ctx.strtab.len); | 1886 | assert(off < ctx.strtab.len); |
| 1827 | const name = mem.sliceTo(@as([*:'\n']const u8, @ptrCast(ctx.strtab[off..].ptr)), 0); | 1887 | const name = mem.sliceTo(@as([*:'\n']const u8, @ptrCast(ctx.strtab.ptr + off)), 0); |
| 1828 | return name[0 .. name.len - 1]; | 1888 | return name[0 .. name.len - 1]; |
| 1829 | } | 1889 | } |
| 1830 | | 1890 | |
| ... | @@ -1836,23 +1896,24 @@ const ElfDumper = struct { | ... | @@ -1836,23 +1896,24 @@ const ElfDumper = struct { |
| 1836 | | 1896 | |
| 1837 | fn parseAndDumpObject(step: *Step, check: Check, bytes: []const u8) ![]const u8 { | 1897 | fn parseAndDumpObject(step: *Step, check: Check, bytes: []const u8) ![]const u8 { |
| 1838 | const gpa = step.owner.allocator; | 1898 | const gpa = step.owner.allocator; |
| 1839 | var r: std.io.Reader = .fixed(bytes); | 1899 | var stream = std.io.fixedBufferStream(bytes); |
| | 1900 | const reader = stream.reader(); |
| 1840 | | 1901 | |
| 1841 | const hdr = try r.takeStruct(elf.Elf64_Ehdr); | 1902 | const hdr = try reader.readStruct(elf.Elf64_Ehdr); |
| 1842 | if (!mem.eql(u8, hdr.e_ident[0..4], "\x7fELF")) return error.InvalidMagicNumber; | 1903 | if (!mem.eql(u8, hdr.e_ident[0..4], "\x7fELF")) { |
| | 1904 | return error.InvalidMagicNumber; |
| | 1905 | } |
| 1843 | | 1906 | |
| 1844 | const shdrs = @as([*]align(1) const elf.Elf64_Shdr, @ptrCast(bytes[hdr.e_shoff..].ptr))[0..hdr.e_shnum]; | 1907 | const shdrs = @as([*]align(1) const elf.Elf64_Shdr, @ptrCast(bytes.ptr + hdr.e_shoff))[0..hdr.e_shnum]; |
| 1845 | const phdrs = @as([*]align(1) const elf.Elf64_Phdr, @ptrCast(bytes[hdr.e_phoff..].ptr))[0..hdr.e_phnum]; | 1908 | const phdrs = @as([*]align(1) const elf.Elf64_Phdr, @ptrCast(bytes.ptr + hdr.e_phoff))[0..hdr.e_phnum]; |
| 1846 | | 1909 | |
| 1847 | var ctx: ObjectContext = .{ | 1910 | var ctx = ObjectContext{ |
| 1848 | .gpa = gpa, | 1911 | .gpa = gpa, |
| 1849 | .data = bytes, | 1912 | .data = bytes, |
| 1850 | .hdr = hdr, | 1913 | .hdr = hdr, |
| 1851 | .shdrs = shdrs, | 1914 | .shdrs = shdrs, |
| 1852 | .phdrs = phdrs, | 1915 | .phdrs = phdrs, |
| 1853 | .shstrtab = undefined, | 1916 | .shstrtab = undefined, |
| 1854 | .symtab = .{}, | | |
| 1855 | .dysymtab = .{}, | | |
| 1856 | }; | 1917 | }; |
| 1857 | ctx.shstrtab = ctx.getSectionContents(ctx.hdr.e_shstrndx); | 1918 | ctx.shstrtab = ctx.getSectionContents(ctx.hdr.e_shstrndx); |
| 1858 | | 1919 | |
| ... | @@ -1883,121 +1944,120 @@ const ElfDumper = struct { | ... | @@ -1883,121 +1944,120 @@ const ElfDumper = struct { |
| 1883 | else => {}, | 1944 | else => {}, |
| 1884 | }; | 1945 | }; |
| 1885 | | 1946 | |
| 1886 | var aw: std.io.Writer.Allocating = .init(gpa); | 1947 | var output = std.ArrayList(u8).init(gpa); |
| 1887 | defer aw.deinit(); | 1948 | const writer = output.writer(); |
| 1888 | const bw = &aw.writer; | | |
| 1889 | | 1949 | |
| 1890 | switch (check.kind) { | 1950 | switch (check.kind) { |
| 1891 | .headers => { | 1951 | .headers => { |
| 1892 | try ctx.dumpHeader(bw); | 1952 | try ctx.dumpHeader(writer); |
| 1893 | try ctx.dumpShdrs(bw); | 1953 | try ctx.dumpShdrs(writer); |
| 1894 | try ctx.dumpPhdrs(bw); | 1954 | try ctx.dumpPhdrs(writer); |
| 1895 | }, | 1955 | }, |
| 1896 | | 1956 | |
| 1897 | .symtab => if (ctx.symtab.symbols.len > 0) { | 1957 | .symtab => if (ctx.symtab.symbols.len > 0) { |
| 1898 | try ctx.dumpSymtab(.symtab, bw); | 1958 | try ctx.dumpSymtab(.symtab, writer); |
| 1899 | } else return step.fail("no symbol table found", .{}), | 1959 | } else return step.fail("no symbol table found", .{}), |
| 1900 | | 1960 | |
| 1901 | .dynamic_symtab => if (ctx.dysymtab.symbols.len > 0) { | 1961 | .dynamic_symtab => if (ctx.dysymtab.symbols.len > 0) { |
| 1902 | try ctx.dumpSymtab(.dysymtab, bw); | 1962 | try ctx.dumpSymtab(.dysymtab, writer); |
| 1903 | } else return step.fail("no dynamic symbol table found", .{}), | 1963 | } else return step.fail("no dynamic symbol table found", .{}), |
| 1904 | | 1964 | |
| 1905 | .dynamic_section => if (ctx.getSectionByName(".dynamic")) |shndx| { | 1965 | .dynamic_section => if (ctx.getSectionByName(".dynamic")) |shndx| { |
| 1906 | try ctx.dumpDynamicSection(shndx, bw); | 1966 | try ctx.dumpDynamicSection(shndx, writer); |
| 1907 | } else return step.fail("no .dynamic section found", .{}), | 1967 | } else return step.fail("no .dynamic section found", .{}), |
| 1908 | | 1968 | |
| 1909 | .dump_section => { | 1969 | .dump_section => { |
| 1910 | const name = mem.sliceTo(@as([*:0]const u8, @ptrCast(check.data.items[check.payload.dump_section..].ptr)), 0); | 1970 | const name = mem.sliceTo(@as([*:0]const u8, @ptrCast(check.data.items.ptr + check.payload.dump_section)), 0); |
| 1911 | const shndx = ctx.getSectionByName(name) orelse return step.fail("no '{s}' section found", .{name}); | 1971 | const shndx = ctx.getSectionByName(name) orelse return step.fail("no '{s}' section found", .{name}); |
| 1912 | try ctx.dumpSection(shndx, bw); | 1972 | try ctx.dumpSection(shndx, writer); |
| 1913 | }, | 1973 | }, |
| 1914 | | 1974 | |
| 1915 | else => return step.fail("invalid check kind for ELF file format: {s}", .{@tagName(check.kind)}), | 1975 | else => return step.fail("invalid check kind for ELF file format: {s}", .{@tagName(check.kind)}), |
| 1916 | } | 1976 | } |
| 1917 | | 1977 | |
| 1918 | return aw.toOwnedSlice(); | 1978 | return output.toOwnedSlice(); |
| 1919 | } | 1979 | } |
| 1920 | | 1980 | |
| 1921 | const ObjectContext = struct { | 1981 | const ObjectContext = struct { |
| 1922 | gpa: Allocator, | 1982 | gpa: Allocator, |
| 1923 | data: []const u8, | 1983 | data: []const u8, |
| 1924 | hdr: *align(1) const elf.Elf64_Ehdr, | 1984 | hdr: elf.Elf64_Ehdr, |
| 1925 | shdrs: []align(1) const elf.Elf64_Shdr, | 1985 | shdrs: []align(1) const elf.Elf64_Shdr, |
| 1926 | phdrs: []align(1) const elf.Elf64_Phdr, | 1986 | phdrs: []align(1) const elf.Elf64_Phdr, |
| 1927 | shstrtab: []const u8, | 1987 | shstrtab: []const u8, |
| 1928 | symtab: Symtab, | 1988 | symtab: Symtab = .{}, |
| 1929 | dysymtab: Symtab, | 1989 | dysymtab: Symtab = .{}, |
| 1930 | | 1990 | |
| 1931 | fn dumpHeader(ctx: ObjectContext, bw: *Writer) !void { | 1991 | fn dumpHeader(ctx: ObjectContext, writer: anytype) !void { |
| 1932 | try bw.writeAll("header\n"); | 1992 | try writer.writeAll("header\n"); |
| 1933 | try bw.print("type {s}\n", .{@tagName(ctx.hdr.e_type)}); | 1993 | try writer.print("type {s}\n", .{@tagName(ctx.hdr.e_type)}); |
| 1934 | try bw.print("entry {x}\n", .{ctx.hdr.e_entry}); | 1994 | try writer.print("entry {x}\n", .{ctx.hdr.e_entry}); |
| 1935 | } | 1995 | } |
| 1936 | | 1996 | |
| 1937 | fn dumpPhdrs(ctx: ObjectContext, bw: *Writer) !void { | 1997 | fn dumpPhdrs(ctx: ObjectContext, writer: anytype) !void { |
| 1938 | if (ctx.phdrs.len == 0) return; | 1998 | if (ctx.phdrs.len == 0) return; |
| 1939 | | 1999 | |
| 1940 | try bw.writeAll("program headers\n"); | 2000 | try writer.writeAll("program headers\n"); |
| 1941 | | 2001 | |
| 1942 | for (ctx.phdrs, 0..) |phdr, phndx| { | 2002 | for (ctx.phdrs, 0..) |phdr, phndx| { |
| 1943 | try bw.print("phdr {d}\n", .{phndx}); | 2003 | try writer.print("phdr {d}\n", .{phndx}); |
| 1944 | try bw.print("type {f}\n", .{fmtPhType(phdr.p_type)}); | 2004 | try writer.print("type {f}\n", .{fmtPhType(phdr.p_type)}); |
| 1945 | try bw.print("vaddr {x}\n", .{phdr.p_vaddr}); | 2005 | try writer.print("vaddr {x}\n", .{phdr.p_vaddr}); |
| 1946 | try bw.print("paddr {x}\n", .{phdr.p_paddr}); | 2006 | try writer.print("paddr {x}\n", .{phdr.p_paddr}); |
| 1947 | try bw.print("offset {x}\n", .{phdr.p_offset}); | 2007 | try writer.print("offset {x}\n", .{phdr.p_offset}); |
| 1948 | try bw.print("memsz {x}\n", .{phdr.p_memsz}); | 2008 | try writer.print("memsz {x}\n", .{phdr.p_memsz}); |
| 1949 | try bw.print("filesz {x}\n", .{phdr.p_filesz}); | 2009 | try writer.print("filesz {x}\n", .{phdr.p_filesz}); |
| 1950 | try bw.print("align {x}\n", .{phdr.p_align}); | 2010 | try writer.print("align {x}\n", .{phdr.p_align}); |
| 1951 | | 2011 | |
| 1952 | { | 2012 | { |
| 1953 | const flags = phdr.p_flags; | 2013 | const flags = phdr.p_flags; |
| 1954 | try bw.writeAll("flags"); | 2014 | try writer.writeAll("flags"); |
| 1955 | if (flags > 0) try bw.writeByte(' '); | 2015 | if (flags > 0) try writer.writeByte(' '); |
| 1956 | if (flags & elf.PF_R != 0) { | 2016 | if (flags & elf.PF_R != 0) { |
| 1957 | try bw.writeByte('R'); | 2017 | try writer.writeByte('R'); |
| 1958 | } | 2018 | } |
| 1959 | if (flags & elf.PF_W != 0) { | 2019 | if (flags & elf.PF_W != 0) { |
| 1960 | try bw.writeByte('W'); | 2020 | try writer.writeByte('W'); |
| 1961 | } | 2021 | } |
| 1962 | if (flags & elf.PF_X != 0) { | 2022 | if (flags & elf.PF_X != 0) { |
| 1963 | try bw.writeByte('E'); | 2023 | try writer.writeByte('E'); |
| 1964 | } | 2024 | } |
| 1965 | if (flags & elf.PF_MASKOS != 0) { | 2025 | if (flags & elf.PF_MASKOS != 0) { |
| 1966 | try bw.writeAll("OS"); | 2026 | try writer.writeAll("OS"); |
| 1967 | } | 2027 | } |
| 1968 | if (flags & elf.PF_MASKPROC != 0) { | 2028 | if (flags & elf.PF_MASKPROC != 0) { |
| 1969 | try bw.writeAll("PROC"); | 2029 | try writer.writeAll("PROC"); |
| 1970 | } | 2030 | } |
| 1971 | try bw.writeByte('\n'); | 2031 | try writer.writeByte('\n'); |
| 1972 | } | 2032 | } |
| 1973 | } | 2033 | } |
| 1974 | } | 2034 | } |
| 1975 | | 2035 | |
| 1976 | fn dumpShdrs(ctx: ObjectContext, bw: *Writer) !void { | 2036 | fn dumpShdrs(ctx: ObjectContext, writer: anytype) !void { |
| 1977 | if (ctx.shdrs.len == 0) return; | 2037 | if (ctx.shdrs.len == 0) return; |
| 1978 | | 2038 | |
| 1979 | try bw.writeAll("section headers\n"); | 2039 | try writer.writeAll("section headers\n"); |
| 1980 | | 2040 | |
| 1981 | for (ctx.shdrs, 0..) |shdr, shndx| { | 2041 | for (ctx.shdrs, 0..) |shdr, shndx| { |
| 1982 | try bw.print("shdr {d}\n", .{shndx}); | 2042 | try writer.print("shdr {d}\n", .{shndx}); |
| 1983 | try bw.print("name {s}\n", .{ctx.getSectionName(shndx)}); | 2043 | try writer.print("name {s}\n", .{ctx.getSectionName(shndx)}); |
| 1984 | try bw.print("type {f}\n", .{fmtShType(shdr.sh_type)}); | 2044 | try writer.print("type {f}\n", .{fmtShType(shdr.sh_type)}); |
| 1985 | try bw.print("addr {x}\n", .{shdr.sh_addr}); | 2045 | try writer.print("addr {x}\n", .{shdr.sh_addr}); |
| 1986 | try bw.print("offset {x}\n", .{shdr.sh_offset}); | 2046 | try writer.print("offset {x}\n", .{shdr.sh_offset}); |
| 1987 | try bw.print("size {x}\n", .{shdr.sh_size}); | 2047 | try writer.print("size {x}\n", .{shdr.sh_size}); |
| 1988 | try bw.print("addralign {x}\n", .{shdr.sh_addralign}); | 2048 | try writer.print("addralign {x}\n", .{shdr.sh_addralign}); |
| 1989 | // TODO dump formatted sh_flags | 2049 | // TODO dump formatted sh_flags |
| 1990 | } | 2050 | } |
| 1991 | } | 2051 | } |
| 1992 | | 2052 | |
| 1993 | fn dumpDynamicSection(ctx: ObjectContext, shndx: usize, bw: *Writer) !void { | 2053 | fn dumpDynamicSection(ctx: ObjectContext, shndx: usize, writer: anytype) !void { |
| 1994 | const shdr = ctx.shdrs[shndx]; | 2054 | const shdr = ctx.shdrs[shndx]; |
| 1995 | const strtab = ctx.getSectionContents(shdr.sh_link); | 2055 | const strtab = ctx.getSectionContents(shdr.sh_link); |
| 1996 | const data = ctx.getSectionContents(shndx); | 2056 | const data = ctx.getSectionContents(shndx); |
| 1997 | const nentries = @divExact(data.len, @sizeOf(elf.Elf64_Dyn)); | 2057 | const nentries = @divExact(data.len, @sizeOf(elf.Elf64_Dyn)); |
| 1998 | const entries = @as([*]align(1) const elf.Elf64_Dyn, @ptrCast(data.ptr))[0..nentries]; | 2058 | const entries = @as([*]align(1) const elf.Elf64_Dyn, @ptrCast(data.ptr))[0..nentries]; |
| 1999 | | 2059 | |
| 2000 | try bw.writeAll(ElfDumper.dynamic_section_label ++ "\n"); | 2060 | try writer.writeAll(ElfDumper.dynamic_section_label ++ "\n"); |
| 2001 | | 2061 | |
| 2002 | for (entries) |entry| { | 2062 | for (entries) |entry| { |
| 2003 | const key = @as(u64, @bitCast(entry.d_tag)); | 2063 | const key = @as(u64, @bitCast(entry.d_tag)); |
| ... | @@ -2038,7 +2098,7 @@ const ElfDumper = struct { | ... | @@ -2038,7 +2098,7 @@ const ElfDumper = struct { |
| 2038 | elf.DT_NULL => "NULL", | 2098 | elf.DT_NULL => "NULL", |
| 2039 | else => "UNKNOWN", | 2099 | else => "UNKNOWN", |
| 2040 | }; | 2100 | }; |
| 2041 | try bw.print("{s}", .{key_str}); | 2101 | try writer.print("{s}", .{key_str}); |
| 2042 | | 2102 | |
| 2043 | switch (key) { | 2103 | switch (key) { |
| 2044 | elf.DT_NEEDED, | 2104 | elf.DT_NEEDED, |
| ... | @@ -2047,7 +2107,7 @@ const ElfDumper = struct { | ... | @@ -2047,7 +2107,7 @@ const ElfDumper = struct { |
| 2047 | elf.DT_RUNPATH, | 2107 | elf.DT_RUNPATH, |
| 2048 | => { | 2108 | => { |
| 2049 | const name = getString(strtab, @intCast(value)); | 2109 | const name = getString(strtab, @intCast(value)); |
| 2050 | try bw.print(" {s}", .{name}); | 2110 | try writer.print(" {s}", .{name}); |
| 2051 | }, | 2111 | }, |
| 2052 | | 2112 | |
| 2053 | elf.DT_INIT_ARRAY, | 2113 | elf.DT_INIT_ARRAY, |
| ... | @@ -2065,7 +2125,7 @@ const ElfDumper = struct { | ... | @@ -2065,7 +2125,7 @@ const ElfDumper = struct { |
| 2065 | elf.DT_INIT, | 2125 | elf.DT_INIT, |
| 2066 | elf.DT_FINI, | 2126 | elf.DT_FINI, |
| 2067 | elf.DT_NULL, | 2127 | elf.DT_NULL, |
| 2068 | => try bw.print(" {x}", .{value}), | 2128 | => try writer.print(" {x}", .{value}), |
| 2069 | | 2129 | |
| 2070 | elf.DT_INIT_ARRAYSZ, | 2130 | elf.DT_INIT_ARRAYSZ, |
| 2071 | elf.DT_FINI_ARRAYSZ, | 2131 | elf.DT_FINI_ARRAYSZ, |
| ... | @@ -2075,77 +2135,77 @@ const ElfDumper = struct { | ... | @@ -2075,77 +2135,77 @@ const ElfDumper = struct { |
| 2075 | elf.DT_RELASZ, | 2135 | elf.DT_RELASZ, |
| 2076 | elf.DT_RELAENT, | 2136 | elf.DT_RELAENT, |
| 2077 | elf.DT_RELACOUNT, | 2137 | elf.DT_RELACOUNT, |
| 2078 | => try bw.print(" {d}", .{value}), | 2138 | => try writer.print(" {d}", .{value}), |
| 2079 | | 2139 | |
| 2080 | elf.DT_PLTREL => try bw.writeAll(switch (value) { | 2140 | elf.DT_PLTREL => try writer.writeAll(switch (value) { |
| 2081 | elf.DT_REL => " REL", | 2141 | elf.DT_REL => " REL", |
| 2082 | elf.DT_RELA => " RELA", | 2142 | elf.DT_RELA => " RELA", |
| 2083 | else => " UNKNOWN", | 2143 | else => " UNKNOWN", |
| 2084 | }), | 2144 | }), |
| 2085 | | 2145 | |
| 2086 | elf.DT_FLAGS => if (value > 0) { | 2146 | elf.DT_FLAGS => if (value > 0) { |
| 2087 | if (value & elf.DF_ORIGIN != 0) try bw.writeAll(" ORIGIN"); | 2147 | if (value & elf.DF_ORIGIN != 0) try writer.writeAll(" ORIGIN"); |
| 2088 | if (value & elf.DF_SYMBOLIC != 0) try bw.writeAll(" SYMBOLIC"); | 2148 | if (value & elf.DF_SYMBOLIC != 0) try writer.writeAll(" SYMBOLIC"); |
| 2089 | if (value & elf.DF_TEXTREL != 0) try bw.writeAll(" TEXTREL"); | 2149 | if (value & elf.DF_TEXTREL != 0) try writer.writeAll(" TEXTREL"); |
| 2090 | if (value & elf.DF_BIND_NOW != 0) try bw.writeAll(" BIND_NOW"); | 2150 | if (value & elf.DF_BIND_NOW != 0) try writer.writeAll(" BIND_NOW"); |
| 2091 | if (value & elf.DF_STATIC_TLS != 0) try bw.writeAll(" STATIC_TLS"); | 2151 | if (value & elf.DF_STATIC_TLS != 0) try writer.writeAll(" STATIC_TLS"); |
| 2092 | }, | 2152 | }, |
| 2093 | | 2153 | |
| 2094 | elf.DT_FLAGS_1 => if (value > 0) { | 2154 | elf.DT_FLAGS_1 => if (value > 0) { |
| 2095 | if (value & elf.DF_1_NOW != 0) try bw.writeAll(" NOW"); | 2155 | if (value & elf.DF_1_NOW != 0) try writer.writeAll(" NOW"); |
| 2096 | if (value & elf.DF_1_GLOBAL != 0) try bw.writeAll(" GLOBAL"); | 2156 | if (value & elf.DF_1_GLOBAL != 0) try writer.writeAll(" GLOBAL"); |
| 2097 | if (value & elf.DF_1_GROUP != 0) try bw.writeAll(" GROUP"); | 2157 | if (value & elf.DF_1_GROUP != 0) try writer.writeAll(" GROUP"); |
| 2098 | if (value & elf.DF_1_NODELETE != 0) try bw.writeAll(" NODELETE"); | 2158 | if (value & elf.DF_1_NODELETE != 0) try writer.writeAll(" NODELETE"); |
| 2099 | if (value & elf.DF_1_LOADFLTR != 0) try bw.writeAll(" LOADFLTR"); | 2159 | if (value & elf.DF_1_LOADFLTR != 0) try writer.writeAll(" LOADFLTR"); |
| 2100 | if (value & elf.DF_1_INITFIRST != 0) try bw.writeAll(" INITFIRST"); | 2160 | if (value & elf.DF_1_INITFIRST != 0) try writer.writeAll(" INITFIRST"); |
| 2101 | if (value & elf.DF_1_NOOPEN != 0) try bw.writeAll(" NOOPEN"); | 2161 | if (value & elf.DF_1_NOOPEN != 0) try writer.writeAll(" NOOPEN"); |
| 2102 | if (value & elf.DF_1_ORIGIN != 0) try bw.writeAll(" ORIGIN"); | 2162 | if (value & elf.DF_1_ORIGIN != 0) try writer.writeAll(" ORIGIN"); |
| 2103 | if (value & elf.DF_1_DIRECT != 0) try bw.writeAll(" DIRECT"); | 2163 | if (value & elf.DF_1_DIRECT != 0) try writer.writeAll(" DIRECT"); |
| 2104 | if (value & elf.DF_1_TRANS != 0) try bw.writeAll(" TRANS"); | 2164 | if (value & elf.DF_1_TRANS != 0) try writer.writeAll(" TRANS"); |
| 2105 | if (value & elf.DF_1_INTERPOSE != 0) try bw.writeAll(" INTERPOSE"); | 2165 | if (value & elf.DF_1_INTERPOSE != 0) try writer.writeAll(" INTERPOSE"); |
| 2106 | if (value & elf.DF_1_NODEFLIB != 0) try bw.writeAll(" NODEFLIB"); | 2166 | if (value & elf.DF_1_NODEFLIB != 0) try writer.writeAll(" NODEFLIB"); |
| 2107 | if (value & elf.DF_1_NODUMP != 0) try bw.writeAll(" NODUMP"); | 2167 | if (value & elf.DF_1_NODUMP != 0) try writer.writeAll(" NODUMP"); |
| 2108 | if (value & elf.DF_1_CONFALT != 0) try bw.writeAll(" CONFALT"); | 2168 | if (value & elf.DF_1_CONFALT != 0) try writer.writeAll(" CONFALT"); |
| 2109 | if (value & elf.DF_1_ENDFILTEE != 0) try bw.writeAll(" ENDFILTEE"); | 2169 | if (value & elf.DF_1_ENDFILTEE != 0) try writer.writeAll(" ENDFILTEE"); |
| 2110 | if (value & elf.DF_1_DISPRELDNE != 0) try bw.writeAll(" DISPRELDNE"); | 2170 | if (value & elf.DF_1_DISPRELDNE != 0) try writer.writeAll(" DISPRELDNE"); |
| 2111 | if (value & elf.DF_1_DISPRELPND != 0) try bw.writeAll(" DISPRELPND"); | 2171 | if (value & elf.DF_1_DISPRELPND != 0) try writer.writeAll(" DISPRELPND"); |
| 2112 | if (value & elf.DF_1_NODIRECT != 0) try bw.writeAll(" NODIRECT"); | 2172 | if (value & elf.DF_1_NODIRECT != 0) try writer.writeAll(" NODIRECT"); |
| 2113 | if (value & elf.DF_1_IGNMULDEF != 0) try bw.writeAll(" IGNMULDEF"); | 2173 | if (value & elf.DF_1_IGNMULDEF != 0) try writer.writeAll(" IGNMULDEF"); |
| 2114 | if (value & elf.DF_1_NOKSYMS != 0) try bw.writeAll(" NOKSYMS"); | 2174 | if (value & elf.DF_1_NOKSYMS != 0) try writer.writeAll(" NOKSYMS"); |
| 2115 | if (value & elf.DF_1_NOHDR != 0) try bw.writeAll(" NOHDR"); | 2175 | if (value & elf.DF_1_NOHDR != 0) try writer.writeAll(" NOHDR"); |
| 2116 | if (value & elf.DF_1_EDITED != 0) try bw.writeAll(" EDITED"); | 2176 | if (value & elf.DF_1_EDITED != 0) try writer.writeAll(" EDITED"); |
| 2117 | if (value & elf.DF_1_NORELOC != 0) try bw.writeAll(" NORELOC"); | 2177 | if (value & elf.DF_1_NORELOC != 0) try writer.writeAll(" NORELOC"); |
| 2118 | if (value & elf.DF_1_SYMINTPOSE != 0) try bw.writeAll(" SYMINTPOSE"); | 2178 | if (value & elf.DF_1_SYMINTPOSE != 0) try writer.writeAll(" SYMINTPOSE"); |
| 2119 | if (value & elf.DF_1_GLOBAUDIT != 0) try bw.writeAll(" GLOBAUDIT"); | 2179 | if (value & elf.DF_1_GLOBAUDIT != 0) try writer.writeAll(" GLOBAUDIT"); |
| 2120 | if (value & elf.DF_1_SINGLETON != 0) try bw.writeAll(" SINGLETON"); | 2180 | if (value & elf.DF_1_SINGLETON != 0) try writer.writeAll(" SINGLETON"); |
| 2121 | if (value & elf.DF_1_STUB != 0) try bw.writeAll(" STUB"); | 2181 | if (value & elf.DF_1_STUB != 0) try writer.writeAll(" STUB"); |
| 2122 | if (value & elf.DF_1_PIE != 0) try bw.writeAll(" PIE"); | 2182 | if (value & elf.DF_1_PIE != 0) try writer.writeAll(" PIE"); |
| 2123 | }, | 2183 | }, |
| 2124 | | 2184 | |
| 2125 | else => try bw.print(" {x}", .{value}), | 2185 | else => try writer.print(" {x}", .{value}), |
| 2126 | } | 2186 | } |
| 2127 | try bw.writeByte('\n'); | 2187 | try writer.writeByte('\n'); |
| 2128 | } | 2188 | } |
| 2129 | } | 2189 | } |
| 2130 | | 2190 | |
| 2131 | fn dumpSymtab(ctx: ObjectContext, comptime @"type": enum { symtab, dysymtab }, bw: *Writer) !void { | 2191 | fn dumpSymtab(ctx: ObjectContext, comptime @"type": enum { symtab, dysymtab }, writer: anytype) !void { |
| 2132 | const symtab = switch (@"type") { | 2192 | const symtab = switch (@"type") { |
| 2133 | .symtab => ctx.symtab, | 2193 | .symtab => ctx.symtab, |
| 2134 | .dysymtab => ctx.dysymtab, | 2194 | .dysymtab => ctx.dysymtab, |
| 2135 | }; | 2195 | }; |
| 2136 | | 2196 | |
| 2137 | try bw.writeAll(switch (@"type") { | 2197 | try writer.writeAll(switch (@"type") { |
| 2138 | .symtab => symtab_label, | 2198 | .symtab => symtab_label, |
| 2139 | .dysymtab => dynamic_symtab_label, | 2199 | .dysymtab => dynamic_symtab_label, |
| 2140 | } ++ "\n"); | 2200 | } ++ "\n"); |
| 2141 | | 2201 | |
| 2142 | for (symtab.symbols, 0..) |sym, index| { | 2202 | for (symtab.symbols, 0..) |sym, index| { |
| 2143 | try bw.print("{x} {x}", .{ sym.st_value, sym.st_size }); | 2203 | try writer.print("{x} {x}", .{ sym.st_value, sym.st_size }); |
| 2144 | | 2204 | |
| 2145 | { | 2205 | { |
| 2146 | if (elf.SHN_LORESERVE <= sym.st_shndx and sym.st_shndx < elf.SHN_HIRESERVE) { | 2206 | if (elf.SHN_LORESERVE <= sym.st_shndx and sym.st_shndx < elf.SHN_HIRESERVE) { |
| 2147 | if (elf.SHN_LOPROC <= sym.st_shndx and sym.st_shndx < elf.SHN_HIPROC) { | 2207 | if (elf.SHN_LOPROC <= sym.st_shndx and sym.st_shndx < elf.SHN_HIPROC) { |
| 2148 | try bw.print(" LO+{d}", .{sym.st_shndx - elf.SHN_LOPROC}); | 2208 | try writer.print(" LO+{d}", .{sym.st_shndx - elf.SHN_LOPROC}); |
| 2149 | } else { | 2209 | } else { |
| 2150 | const sym_ndx = switch (sym.st_shndx) { | 2210 | const sym_ndx = switch (sym.st_shndx) { |
| 2151 | elf.SHN_ABS => "ABS", | 2211 | elf.SHN_ABS => "ABS", |
| ... | @@ -2153,12 +2213,12 @@ const ElfDumper = struct { | ... | @@ -2153,12 +2213,12 @@ const ElfDumper = struct { |
| 2153 | elf.SHN_LIVEPATCH => "LIV", | 2213 | elf.SHN_LIVEPATCH => "LIV", |
| 2154 | else => "UNK", | 2214 | else => "UNK", |
| 2155 | }; | 2215 | }; |
| 2156 | try bw.print(" {s}", .{sym_ndx}); | 2216 | try writer.print(" {s}", .{sym_ndx}); |
| 2157 | } | 2217 | } |
| 2158 | } else if (sym.st_shndx == elf.SHN_UNDEF) { | 2218 | } else if (sym.st_shndx == elf.SHN_UNDEF) { |
| 2159 | try bw.writeAll(" UND"); | 2219 | try writer.writeAll(" UND"); |
| 2160 | } else { | 2220 | } else { |
| 2161 | try bw.print(" {x}", .{sym.st_shndx}); | 2221 | try writer.print(" {x}", .{sym.st_shndx}); |
| 2162 | } | 2222 | } |
| 2163 | } | 2223 | } |
| 2164 | | 2224 | |
| ... | @@ -2175,12 +2235,12 @@ const ElfDumper = struct { | ... | @@ -2175,12 +2235,12 @@ const ElfDumper = struct { |
| 2175 | elf.STT_NUM => "NUM", | 2235 | elf.STT_NUM => "NUM", |
| 2176 | elf.STT_GNU_IFUNC => "IFUNC", | 2236 | elf.STT_GNU_IFUNC => "IFUNC", |
| 2177 | else => if (elf.STT_LOPROC <= tt and tt < elf.STT_HIPROC) { | 2237 | else => if (elf.STT_LOPROC <= tt and tt < elf.STT_HIPROC) { |
| 2178 | break :blk try bw.print(" LOPROC+{d}", .{tt - elf.STT_LOPROC}); | 2238 | break :blk try writer.print(" LOPROC+{d}", .{tt - elf.STT_LOPROC}); |
| 2179 | } else if (elf.STT_LOOS <= tt and tt < elf.STT_HIOS) { | 2239 | } else if (elf.STT_LOOS <= tt and tt < elf.STT_HIOS) { |
| 2180 | break :blk try bw.print(" LOOS+{d}", .{tt - elf.STT_LOOS}); | 2240 | break :blk try writer.print(" LOOS+{d}", .{tt - elf.STT_LOOS}); |
| 2181 | } else "UNK", | 2241 | } else "UNK", |
| 2182 | }; | 2242 | }; |
| 2183 | try bw.print(" {s}", .{sym_type}); | 2243 | try writer.print(" {s}", .{sym_type}); |
| 2184 | } | 2244 | } |
| 2185 | | 2245 | |
| 2186 | blk: { | 2246 | blk: { |
| ... | @@ -2191,28 +2251,28 @@ const ElfDumper = struct { | ... | @@ -2191,28 +2251,28 @@ const ElfDumper = struct { |
| 2191 | elf.STB_WEAK => "WEAK", | 2251 | elf.STB_WEAK => "WEAK", |
| 2192 | elf.STB_NUM => "NUM", | 2252 | elf.STB_NUM => "NUM", |
| 2193 | else => if (elf.STB_LOPROC <= bind and bind < elf.STB_HIPROC) { | 2253 | else => if (elf.STB_LOPROC <= bind and bind < elf.STB_HIPROC) { |
| 2194 | break :blk try bw.print(" LOPROC+{d}", .{bind - elf.STB_LOPROC}); | 2254 | break :blk try writer.print(" LOPROC+{d}", .{bind - elf.STB_LOPROC}); |
| 2195 | } else if (elf.STB_LOOS <= bind and bind < elf.STB_HIOS) { | 2255 | } else if (elf.STB_LOOS <= bind and bind < elf.STB_HIOS) { |
| 2196 | break :blk try bw.print(" LOOS+{d}", .{bind - elf.STB_LOOS}); | 2256 | break :blk try writer.print(" LOOS+{d}", .{bind - elf.STB_LOOS}); |
| 2197 | } else "UNKNOWN", | 2257 | } else "UNKNOWN", |
| 2198 | }; | 2258 | }; |
| 2199 | try bw.print(" {s}", .{sym_bind}); | 2259 | try writer.print(" {s}", .{sym_bind}); |
| 2200 | } | 2260 | } |
| 2201 | | 2261 | |
| 2202 | const sym_vis = @as(elf.STV, @enumFromInt(@as(u2, @truncate(sym.st_other)))); | 2262 | const sym_vis = @as(elf.STV, @enumFromInt(@as(u2, @truncate(sym.st_other)))); |
| 2203 | try bw.print(" {s}", .{@tagName(sym_vis)}); | 2263 | try writer.print(" {s}", .{@tagName(sym_vis)}); |
| 2204 | | 2264 | |
| 2205 | const sym_name = switch (sym.st_type()) { | 2265 | const sym_name = switch (sym.st_type()) { |
| 2206 | elf.STT_SECTION => ctx.getSectionName(sym.st_shndx), | 2266 | elf.STT_SECTION => ctx.getSectionName(sym.st_shndx), |
| 2207 | else => symtab.getName(index).?, | 2267 | else => symtab.getName(index).?, |
| 2208 | }; | 2268 | }; |
| 2209 | try bw.print(" {s}\n", .{sym_name}); | 2269 | try writer.print(" {s}\n", .{sym_name}); |
| 2210 | } | 2270 | } |
| 2211 | } | 2271 | } |
| 2212 | | 2272 | |
| 2213 | fn dumpSection(ctx: ObjectContext, shndx: usize, bw: *Writer) !void { | 2273 | fn dumpSection(ctx: ObjectContext, shndx: usize, writer: anytype) !void { |
| 2214 | const data = ctx.getSectionContents(shndx); | 2274 | const data = ctx.getSectionContents(shndx); |
| 2215 | try bw.print("{s}", .{data}); | 2275 | try writer.print("{s}", .{data}); |
| 2216 | } | 2276 | } |
| 2217 | | 2277 | |
| 2218 | inline fn getSectionName(ctx: ObjectContext, shndx: usize) []const u8 { | 2278 | inline fn getSectionName(ctx: ObjectContext, shndx: usize) []const u8 { |
| ... | @@ -2250,15 +2310,15 @@ const ElfDumper = struct { | ... | @@ -2250,15 +2310,15 @@ const ElfDumper = struct { |
| 2250 | }; | 2310 | }; |
| 2251 | | 2311 | |
| 2252 | fn getString(strtab: []const u8, off: u32) []const u8 { | 2312 | fn getString(strtab: []const u8, off: u32) []const u8 { |
| 2253 | const str = strtab[off..]; | 2313 | assert(off < strtab.len); |
| 2254 | return str[0..std.mem.indexOfScalar(u8, str, 0).?]; | 2314 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(strtab.ptr + off)), 0); |
| 2255 | } | 2315 | } |
| 2256 | | 2316 | |
| 2257 | fn fmtShType(sh_type: u32) std.fmt.Formatter(u32, formatShType) { | 2317 | fn fmtShType(sh_type: u32) std.fmt.Formatter(u32, formatShType) { |
| 2258 | return .{ .data = sh_type }; | 2318 | return .{ .data = sh_type }; |
| 2259 | } | 2319 | } |
| 2260 | | 2320 | |
| 2261 | fn formatShType(sh_type: u32, w: *Writer) Writer.Error!void { | 2321 | fn formatShType(sh_type: u32, writer: *Writer) Writer.Error!void { |
| 2262 | const name = switch (sh_type) { | 2322 | const name = switch (sh_type) { |
| 2263 | elf.SHT_NULL => "NULL", | 2323 | elf.SHT_NULL => "NULL", |
| 2264 | elf.SHT_PROGBITS => "PROGBITS", | 2324 | elf.SHT_PROGBITS => "PROGBITS", |
| ... | @@ -2284,21 +2344,21 @@ const ElfDumper = struct { | ... | @@ -2284,21 +2344,21 @@ const ElfDumper = struct { |
| 2284 | elf.SHT_GNU_VERNEED => "VERNEED", | 2344 | elf.SHT_GNU_VERNEED => "VERNEED", |
| 2285 | elf.SHT_GNU_VERSYM => "VERSYM", | 2345 | elf.SHT_GNU_VERSYM => "VERSYM", |
| 2286 | else => if (elf.SHT_LOOS <= sh_type and sh_type < elf.SHT_HIOS) { | 2346 | else => if (elf.SHT_LOOS <= sh_type and sh_type < elf.SHT_HIOS) { |
| 2287 | return try w.print("LOOS+0x{x}", .{sh_type - elf.SHT_LOOS}); | 2347 | return try writer.print("LOOS+0x{x}", .{sh_type - elf.SHT_LOOS}); |
| 2288 | } else if (elf.SHT_LOPROC <= sh_type and sh_type < elf.SHT_HIPROC) { | 2348 | } else if (elf.SHT_LOPROC <= sh_type and sh_type < elf.SHT_HIPROC) { |
| 2289 | return try w.print("LOPROC+0x{x}", .{sh_type - elf.SHT_LOPROC}); | 2349 | return try writer.print("LOPROC+0x{x}", .{sh_type - elf.SHT_LOPROC}); |
| 2290 | } else if (elf.SHT_LOUSER <= sh_type and sh_type < elf.SHT_HIUSER) { | 2350 | } else if (elf.SHT_LOUSER <= sh_type and sh_type < elf.SHT_HIUSER) { |
| 2291 | return try w.print("LOUSER+0x{x}", .{sh_type - elf.SHT_LOUSER}); | 2351 | return try writer.print("LOUSER+0x{x}", .{sh_type - elf.SHT_LOUSER}); |
| 2292 | } else "UNKNOWN", | 2352 | } else "UNKNOWN", |
| 2293 | }; | 2353 | }; |
| 2294 | try w.writeAll(name); | 2354 | try writer.writeAll(name); |
| 2295 | } | 2355 | } |
| 2296 | | 2356 | |
| 2297 | fn fmtPhType(ph_type: u32) std.fmt.Formatter(u32, formatPhType) { | 2357 | fn fmtPhType(ph_type: u32) std.fmt.Formatter(u32, formatPhType) { |
| 2298 | return .{ .data = ph_type }; | 2358 | return .{ .data = ph_type }; |
| 2299 | } | 2359 | } |
| 2300 | | 2360 | |
| 2301 | fn formatPhType(ph_type: u32, w: *Writer) Writer.Error!void { | 2361 | fn formatPhType(ph_type: u32, writer: *Writer) Writer.Error!void { |
| 2302 | const p_type = switch (ph_type) { | 2362 | const p_type = switch (ph_type) { |
| 2303 | elf.PT_NULL => "NULL", | 2363 | elf.PT_NULL => "NULL", |
| 2304 | elf.PT_LOAD => "LOAD", | 2364 | elf.PT_LOAD => "LOAD", |
| ... | @@ -2313,12 +2373,12 @@ const ElfDumper = struct { | ... | @@ -2313,12 +2373,12 @@ const ElfDumper = struct { |
| 2313 | elf.PT_GNU_STACK => "GNU_STACK", | 2373 | elf.PT_GNU_STACK => "GNU_STACK", |
| 2314 | elf.PT_GNU_RELRO => "GNU_RELRO", | 2374 | elf.PT_GNU_RELRO => "GNU_RELRO", |
| 2315 | else => if (elf.PT_LOOS <= ph_type and ph_type < elf.PT_HIOS) { | 2375 | else => if (elf.PT_LOOS <= ph_type and ph_type < elf.PT_HIOS) { |
| 2316 | return try w.print("LOOS+0x{x}", .{ph_type - elf.PT_LOOS}); | 2376 | return try writer.print("LOOS+0x{x}", .{ph_type - elf.PT_LOOS}); |
| 2317 | } else if (elf.PT_LOPROC <= ph_type and ph_type < elf.PT_HIPROC) { | 2377 | } else if (elf.PT_LOPROC <= ph_type and ph_type < elf.PT_HIPROC) { |
| 2318 | return try w.print("LOPROC+0x{x}", .{ph_type - elf.PT_LOPROC}); | 2378 | return try writer.print("LOPROC+0x{x}", .{ph_type - elf.PT_LOPROC}); |
| 2319 | } else "UNKNOWN", | 2379 | } else "UNKNOWN", |
| 2320 | }; | 2380 | }; |
| 2321 | try w.writeAll(p_type); | 2381 | try writer.writeAll(p_type); |
| 2322 | } | 2382 | } |
| 2323 | }; | 2383 | }; |
| 2324 | | 2384 | |
| ... | @@ -2327,38 +2387,49 @@ const WasmDumper = struct { | ... | @@ -2327,38 +2387,49 @@ const WasmDumper = struct { |
| 2327 | | 2387 | |
| 2328 | fn parseAndDump(step: *Step, check: Check, bytes: []const u8) ![]const u8 { | 2388 | fn parseAndDump(step: *Step, check: Check, bytes: []const u8) ![]const u8 { |
| 2329 | const gpa = step.owner.allocator; | 2389 | const gpa = step.owner.allocator; |
| 2330 | var r: std.io.Reader = .fixed(bytes); | 2390 | var fbs = std.io.fixedBufferStream(bytes); |
| 2331 | | 2391 | const reader = fbs.reader(); |
| 2332 | const buf = try r.takeArray(8); | | |
| 2333 | if (!mem.eql(u8, buf[0..4], &std.wasm.magic)) return error.InvalidMagicByte; | | |
| 2334 | if (!mem.eql(u8, buf[4..8], &std.wasm.version)) return error.UnsupportedWasmVersion; | | |
| 2335 | | 2392 | |
| 2336 | var aw: std.io.Writer.Allocating = .init(gpa); | 2393 | const buf = try reader.readBytesNoEof(8); |
| 2337 | defer aw.deinit(); | 2394 | if (!mem.eql(u8, buf[0..4], &std.wasm.magic)) { |
| 2338 | const bw = &aw.writer; | 2395 | return error.InvalidMagicByte; |
| | 2396 | } |
| | 2397 | if (!mem.eql(u8, buf[4..], &std.wasm.version)) { |
| | 2398 | return error.UnsupportedWasmVersion; |
| | 2399 | } |
| 2339 | | 2400 | |
| 2340 | parseAndDumpInner(step, check, &r, bw) catch |err| switch (err) { | 2401 | var output = std.ArrayList(u8).init(gpa); |
| 2341 | error.EndOfStream => try bw.writeAll("\n<UnexpectedEndOfStream>"), | 2402 | defer output.deinit(); |
| | 2403 | parseAndDumpInner(step, check, bytes, &fbs, &output) catch |err| switch (err) { |
| | 2404 | error.EndOfStream => try output.appendSlice("\n<UnexpectedEndOfStream>"), |
| 2342 | else => |e| return e, | 2405 | else => |e| return e, |
| 2343 | }; | 2406 | }; |
| 2344 | return aw.toOwnedSlice(); | 2407 | return output.toOwnedSlice(); |
| 2345 | } | 2408 | } |
| 2346 | | 2409 | |
| 2347 | fn parseAndDumpInner( | 2410 | fn parseAndDumpInner( |
| 2348 | step: *Step, | 2411 | step: *Step, |
| 2349 | check: Check, | 2412 | check: Check, |
| 2350 | r: *std.io.Reader, | 2413 | bytes: []const u8, |
| 2351 | bw: *Writer, | 2414 | fbs: *std.io.FixedBufferStream([]const u8), |
| | 2415 | output: *std.ArrayList(u8), |
| 2352 | ) !void { | 2416 | ) !void { |
| | 2417 | const reader = fbs.reader(); |
| | 2418 | const writer = output.writer(); |
| | 2419 | |
| 2353 | switch (check.kind) { | 2420 | switch (check.kind) { |
| 2354 | .headers => while (r.takeEnum(std.wasm.Section, .little)) |section| { | 2421 | .headers => { |
| 2355 | var section_reader: std.io.Reader = .fixed(try r.take(try r.takeLeb128(u32))); | 2422 | while (reader.readByte()) |current_byte| { |
| 2356 | try parseAndDumpSection(step, section, &section_reader, bw); | 2423 | const section = std.enums.fromInt(std.wasm.Section, current_byte) orelse { |
| 2357 | } else |err| switch (err) { | 2424 | return step.fail("Found invalid section id '{d}'", .{current_byte}); |
| 2358 | error.InvalidEnumTag => return step.fail("invalid section id", .{}), | 2425 | }; |
| 2359 | error.EndOfStream => {}, | 2426 | |
| 2360 | else => |e| return e, | 2427 | const section_length = try std.leb.readUleb128(u32, reader); |
| | 2428 | try parseAndDumpSection(step, section, bytes[fbs.pos..][0..section_length], writer); |
| | 2429 | fbs.pos += section_length; |
| | 2430 | } else |_| {} // reached end of stream |
| 2361 | }, | 2431 | }, |
| | 2432 | |
| 2362 | else => return step.fail("invalid check kind for Wasm file format: {s}", .{@tagName(check.kind)}), | 2433 | else => return step.fail("invalid check kind for Wasm file format: {s}", .{@tagName(check.kind)}), |
| 2363 | } | 2434 | } |
| 2364 | } | 2435 | } |
| ... | @@ -2366,13 +2437,16 @@ const WasmDumper = struct { | ... | @@ -2366,13 +2437,16 @@ const WasmDumper = struct { |
| 2366 | fn parseAndDumpSection( | 2437 | fn parseAndDumpSection( |
| 2367 | step: *Step, | 2438 | step: *Step, |
| 2368 | section: std.wasm.Section, | 2439 | section: std.wasm.Section, |
| 2369 | r: *std.io.Reader, | 2440 | data: []const u8, |
| 2370 | bw: *Writer, | 2441 | writer: anytype, |
| 2371 | ) !void { | 2442 | ) !void { |
| 2372 | try bw.print( | 2443 | var fbs = std.io.fixedBufferStream(data); |
| | 2444 | const reader = fbs.reader(); |
| | 2445 | |
| | 2446 | try writer.print( |
| 2373 | \\Section {s} | 2447 | \\Section {s} |
| 2374 | \\size {d} | 2448 | \\size {d} |
| 2375 | , .{ @tagName(section), r.buffer.len }); | 2449 | , .{ @tagName(section), data.len }); |
| 2376 | | 2450 | |
| 2377 | switch (section) { | 2451 | switch (section) { |
| 2378 | .type, | 2452 | .type, |
| ... | @@ -2386,83 +2460,96 @@ const WasmDumper = struct { | ... | @@ -2386,83 +2460,96 @@ const WasmDumper = struct { |
| 2386 | .code, | 2460 | .code, |
| 2387 | .data, | 2461 | .data, |
| 2388 | => { | 2462 | => { |
| 2389 | const entries = try r.takeLeb128(u32); | 2463 | const entries = try std.leb.readUleb128(u32, reader); |
| 2390 | try bw.print("\nentries {d}\n", .{entries}); | 2464 | try writer.print("\nentries {d}\n", .{entries}); |
| 2391 | try parseSection(step, section, r, entries, bw); | 2465 | try parseSection(step, section, data[fbs.pos..], entries, writer); |
| 2392 | }, | 2466 | }, |
| 2393 | .custom => { | 2467 | .custom => { |
| 2394 | const name = try r.take(try r.takeLeb128(u32)); | 2468 | const name_length = try std.leb.readUleb128(u32, reader); |
| 2395 | try bw.print("\nname {s}\n", .{name}); | 2469 | const name = data[fbs.pos..][0..name_length]; |
| | 2470 | fbs.pos += name_length; |
| | 2471 | try writer.print("\nname {s}\n", .{name}); |
| 2396 | | 2472 | |
| 2397 | if (mem.eql(u8, name, "name")) { | 2473 | if (mem.eql(u8, name, "name")) { |
| 2398 | try parseDumpNames(step, r, bw); | 2474 | try parseDumpNames(step, reader, writer, data); |
| 2399 | } else if (mem.eql(u8, name, "producers")) { | 2475 | } else if (mem.eql(u8, name, "producers")) { |
| 2400 | try parseDumpProducers(r, bw); | 2476 | try parseDumpProducers(reader, writer, data); |
| 2401 | } else if (mem.eql(u8, name, "target_features")) { | 2477 | } else if (mem.eql(u8, name, "target_features")) { |
| 2402 | try parseDumpFeatures(r, bw); | 2478 | try parseDumpFeatures(reader, writer, data); |
| 2403 | } | 2479 | } |
| 2404 | // TODO: Implement parsing and dumping other custom sections (such as relocations) | 2480 | // TODO: Implement parsing and dumping other custom sections (such as relocations) |
| 2405 | }, | 2481 | }, |
| 2406 | .start => { | 2482 | .start => { |
| 2407 | const start = try r.takeLeb128(u32); | 2483 | const start = try std.leb.readUleb128(u32, reader); |
| 2408 | try bw.print("\nstart {d}\n", .{start}); | 2484 | try writer.print("\nstart {d}\n", .{start}); |
| 2409 | }, | 2485 | }, |
| 2410 | .data_count => { | 2486 | .data_count => { |
| 2411 | const count = try r.takeLeb128(u32); | 2487 | const count = try std.leb.readUleb128(u32, reader); |
| 2412 | try bw.print("\ncount {d}\n", .{count}); | 2488 | try writer.print("\ncount {d}\n", .{count}); |
| 2413 | }, | 2489 | }, |
| 2414 | else => {}, // skip unknown sections | 2490 | else => {}, // skip unknown sections |
| 2415 | } | 2491 | } |
| 2416 | } | 2492 | } |
| 2417 | | 2493 | |
| 2418 | fn parseSection(step: *Step, section: std.wasm.Section, r: *std.io.Reader, entries: u32, bw: *Writer) !void { | 2494 | fn parseSection(step: *Step, section: std.wasm.Section, data: []const u8, entries: u32, writer: anytype) !void { |
| | 2495 | var fbs = std.io.fixedBufferStream(data); |
| | 2496 | const reader = fbs.reader(); |
| | 2497 | |
| 2419 | switch (section) { | 2498 | switch (section) { |
| 2420 | .type => { | 2499 | .type => { |
| 2421 | var i: u32 = 0; | 2500 | var i: u32 = 0; |
| 2422 | while (i < entries) : (i += 1) { | 2501 | while (i < entries) : (i += 1) { |
| 2423 | const func_type = try r.takeByte(); | 2502 | const func_type = try reader.readByte(); |
| 2424 | if (func_type != std.wasm.function_type) { | 2503 | if (func_type != std.wasm.function_type) { |
| 2425 | return step.fail("expected function type, found byte '{d}'", .{func_type}); | 2504 | return step.fail("expected function type, found byte '{d}'", .{func_type}); |
| 2426 | } | 2505 | } |
| 2427 | const params = try r.takeLeb128(u32); | 2506 | const params = try std.leb.readUleb128(u32, reader); |
| 2428 | try bw.print("params {d}\n", .{params}); | 2507 | try writer.print("params {d}\n", .{params}); |
| 2429 | var index: u32 = 0; | 2508 | var index: u32 = 0; |
| 2430 | while (index < params) : (index += 1) { | 2509 | while (index < params) : (index += 1) { |
| 2431 | _ = try parseDumpType(step, std.wasm.Valtype, r, bw); | 2510 | _ = try parseDumpType(step, std.wasm.Valtype, reader, writer); |
| 2432 | } else index = 0; | 2511 | } else index = 0; |
| 2433 | const returns = try r.takeLeb128(u32); | 2512 | const returns = try std.leb.readUleb128(u32, reader); |
| 2434 | try bw.print("returns {d}\n", .{returns}); | 2513 | try writer.print("returns {d}\n", .{returns}); |
| 2435 | while (index < returns) : (index += 1) { | 2514 | while (index < returns) : (index += 1) { |
| 2436 | _ = try parseDumpType(step, std.wasm.Valtype, r, bw); | 2515 | _ = try parseDumpType(step, std.wasm.Valtype, reader, writer); |
| 2437 | } | 2516 | } |
| 2438 | } | 2517 | } |
| 2439 | }, | 2518 | }, |
| 2440 | .import => { | 2519 | .import => { |
| 2441 | var i: u32 = 0; | 2520 | var i: u32 = 0; |
| 2442 | while (i < entries) : (i += 1) { | 2521 | while (i < entries) : (i += 1) { |
| 2443 | const module_name = try r.take(try r.takeLeb128(u32)); | 2522 | const module_name_len = try std.leb.readUleb128(u32, reader); |
| 2444 | const name = try r.take(try r.takeLeb128(u32)); | 2523 | const module_name = data[fbs.pos..][0..module_name_len]; |
| 2445 | const kind = r.takeEnum(std.wasm.ExternalKind, .little) catch |err| switch (err) { | 2524 | fbs.pos += module_name_len; |
| 2446 | error.InvalidEnumTag => return step.fail("invalid import kind", .{}), | 2525 | const name_len = try std.leb.readUleb128(u32, reader); |
| 2447 | else => |e| return e, | 2526 | const name = data[fbs.pos..][0..name_len]; |
| | 2527 | fbs.pos += name_len; |
| | 2528 | |
| | 2529 | const kind = std.enums.fromInt(std.wasm.ExternalKind, try reader.readByte()) orelse { |
| | 2530 | return step.fail("invalid import kind", .{}); |
| 2448 | }; | 2531 | }; |
| 2449 | | 2532 | |
| 2450 | try bw.print( | 2533 | try writer.print( |
| 2451 | \\module {s} | 2534 | \\module {s} |
| 2452 | \\name {s} | 2535 | \\name {s} |
| 2453 | \\kind {s} | 2536 | \\kind {s} |
| 2454 | , .{ module_name, name, @tagName(kind) }); | 2537 | , .{ module_name, name, @tagName(kind) }); |
| 2455 | try bw.writeByte('\n'); | 2538 | try writer.writeByte('\n'); |
| 2456 | switch (kind) { | 2539 | switch (kind) { |
| 2457 | .function => try bw.print("index {d}\n", .{try r.takeLeb128(u32)}), | 2540 | .function => { |
| 2458 | .memory => try parseDumpLimits(r, bw), | 2541 | try writer.print("index {d}\n", .{try std.leb.readUleb128(u32, reader)}); |
| | 2542 | }, |
| | 2543 | .memory => { |
| | 2544 | try parseDumpLimits(reader, writer); |
| | 2545 | }, |
| 2459 | .global => { | 2546 | .global => { |
| 2460 | _ = try parseDumpType(step, std.wasm.Valtype, r, bw); | 2547 | _ = try parseDumpType(step, std.wasm.Valtype, reader, writer); |
| 2461 | try bw.print("mutable {}\n", .{0x01 == try r.takeLeb128(u32)}); | 2548 | try writer.print("mutable {}\n", .{0x01 == try std.leb.readUleb128(u32, reader)}); |
| 2462 | }, | 2549 | }, |
| 2463 | .table => { | 2550 | .table => { |
| 2464 | _ = try parseDumpType(step, std.wasm.RefType, r, bw); | 2551 | _ = try parseDumpType(step, std.wasm.RefType, reader, writer); |
| 2465 | try parseDumpLimits(r, bw); | 2552 | try parseDumpLimits(reader, writer); |
| 2466 | }, | 2553 | }, |
| 2467 | } | 2554 | } |
| 2468 | } | 2555 | } |
| ... | @@ -2470,58 +2557,60 @@ const WasmDumper = struct { | ... | @@ -2470,58 +2557,60 @@ const WasmDumper = struct { |
| 2470 | .function => { | 2557 | .function => { |
| 2471 | var i: u32 = 0; | 2558 | var i: u32 = 0; |
| 2472 | while (i < entries) : (i += 1) { | 2559 | while (i < entries) : (i += 1) { |
| 2473 | try bw.print("index {d}\n", .{try r.takeLeb128(u32)}); | 2560 | try writer.print("index {d}\n", .{try std.leb.readUleb128(u32, reader)}); |
| 2474 | } | 2561 | } |
| 2475 | }, | 2562 | }, |
| 2476 | .table => { | 2563 | .table => { |
| 2477 | var i: u32 = 0; | 2564 | var i: u32 = 0; |
| 2478 | while (i < entries) : (i += 1) { | 2565 | while (i < entries) : (i += 1) { |
| 2479 | _ = try parseDumpType(step, std.wasm.RefType, r, bw); | 2566 | _ = try parseDumpType(step, std.wasm.RefType, reader, writer); |
| 2480 | try parseDumpLimits(r, bw); | 2567 | try parseDumpLimits(reader, writer); |
| 2481 | } | 2568 | } |
| 2482 | }, | 2569 | }, |
| 2483 | .memory => { | 2570 | .memory => { |
| 2484 | var i: u32 = 0; | 2571 | var i: u32 = 0; |
| 2485 | while (i < entries) : (i += 1) { | 2572 | while (i < entries) : (i += 1) { |
| 2486 | try parseDumpLimits(r, bw); | 2573 | try parseDumpLimits(reader, writer); |
| 2487 | } | 2574 | } |
| 2488 | }, | 2575 | }, |
| 2489 | .global => { | 2576 | .global => { |
| 2490 | var i: u32 = 0; | 2577 | var i: u32 = 0; |
| 2491 | while (i < entries) : (i += 1) { | 2578 | while (i < entries) : (i += 1) { |
| 2492 | _ = try parseDumpType(step, std.wasm.Valtype, r, bw); | 2579 | _ = try parseDumpType(step, std.wasm.Valtype, reader, writer); |
| 2493 | try bw.print("mutable {}\n", .{0x01 == try r.takeLeb128(u1)}); | 2580 | try writer.print("mutable {}\n", .{0x01 == try std.leb.readUleb128(u1, reader)}); |
| 2494 | try parseDumpInit(step, r, bw); | 2581 | try parseDumpInit(step, reader, writer); |
| 2495 | } | 2582 | } |
| 2496 | }, | 2583 | }, |
| 2497 | .@"export" => { | 2584 | .@"export" => { |
| 2498 | var i: u32 = 0; | 2585 | var i: u32 = 0; |
| 2499 | while (i < entries) : (i += 1) { | 2586 | while (i < entries) : (i += 1) { |
| 2500 | const name = try r.take(try r.takeLeb128(u32)); | 2587 | const name_len = try std.leb.readUleb128(u32, reader); |
| 2501 | const kind = r.takeEnum(std.wasm.ExternalKind, .little) catch |err| switch (err) { | 2588 | const name = data[fbs.pos..][0..name_len]; |
| 2502 | error.InvalidEnumTag => return step.fail("invalid export kind value", .{}), | 2589 | fbs.pos += name_len; |
| 2503 | else => |e| return e, | 2590 | const kind_byte = try std.leb.readUleb128(u8, reader); |
| | 2591 | const kind = std.enums.fromInt(std.wasm.ExternalKind, kind_byte) orelse { |
| | 2592 | return step.fail("invalid export kind value '{d}'", .{kind_byte}); |
| 2504 | }; | 2593 | }; |
| 2505 | const index = try r.takeLeb128(u32); | 2594 | const index = try std.leb.readUleb128(u32, reader); |
| 2506 | try bw.print( | 2595 | try writer.print( |
| 2507 | \\name {s} | 2596 | \\name {s} |
| 2508 | \\kind {s} | 2597 | \\kind {s} |
| 2509 | \\index {d} | 2598 | \\index {d} |
| 2510 | , .{ name, @tagName(kind), index }); | 2599 | , .{ name, @tagName(kind), index }); |
| 2511 | try bw.writeByte('\n'); | 2600 | try writer.writeByte('\n'); |
| 2512 | } | 2601 | } |
| 2513 | }, | 2602 | }, |
| 2514 | .element => { | 2603 | .element => { |
| 2515 | var i: u32 = 0; | 2604 | var i: u32 = 0; |
| 2516 | while (i < entries) : (i += 1) { | 2605 | while (i < entries) : (i += 1) { |
| 2517 | try bw.print("table index {d}\n", .{try r.takeLeb128(u32)}); | 2606 | try writer.print("table index {d}\n", .{try std.leb.readUleb128(u32, reader)}); |
| 2518 | try parseDumpInit(step, r, bw); | 2607 | try parseDumpInit(step, reader, writer); |
| 2519 | | 2608 | |
| 2520 | const function_indexes = try r.takeLeb128(u32); | 2609 | const function_indexes = try std.leb.readUleb128(u32, reader); |
| 2521 | var function_index: u32 = 0; | 2610 | var function_index: u32 = 0; |
| 2522 | try bw.print("indexes {d}\n", .{function_indexes}); | 2611 | try writer.print("indexes {d}\n", .{function_indexes}); |
| 2523 | while (function_index < function_indexes) : (function_index += 1) { | 2612 | while (function_index < function_indexes) : (function_index += 1) { |
| 2524 | try bw.print("index {d}\n", .{try r.takeLeb128(u32)}); | 2613 | try writer.print("index {d}\n", .{try std.leb.readUleb128(u32, reader)}); |
| 2525 | } | 2614 | } |
| 2526 | } | 2615 | } |
| 2527 | }, | 2616 | }, |
| ... | @@ -2529,95 +2618,101 @@ const WasmDumper = struct { | ... | @@ -2529,95 +2618,101 @@ const WasmDumper = struct { |
| 2529 | .data => { | 2618 | .data => { |
| 2530 | var i: u32 = 0; | 2619 | var i: u32 = 0; |
| 2531 | while (i < entries) : (i += 1) { | 2620 | while (i < entries) : (i += 1) { |
| 2532 | const flags: packed struct(u32) { | 2621 | const flags = try std.leb.readUleb128(u32, reader); |
| 2533 | passive: bool, | 2622 | const index = if (flags & 0x02 != 0) |
| 2534 | memidx: bool, | 2623 | try std.leb.readUleb128(u32, reader) |
| 2535 | unused: u30, | 2624 | else |
| 2536 | } = @bitCast(try r.takeLeb128(u32)); | 2625 | 0; |
| 2537 | const index = if (flags.memidx) try r.takeLeb128(u32) else 0; | 2626 | try writer.print("memory index 0x{x}\n", .{index}); |
| 2538 | try bw.print("memory index 0x{x}\n", .{index}); | 2627 | if (flags == 0) { |
| 2539 | if (!flags.passive) try parseDumpInit(step, r, bw); | 2628 | try parseDumpInit(step, reader, writer); |
| 2540 | const size = try r.takeLeb128(u32); | 2629 | } |
| 2541 | try bw.print("size {d}\n", .{size}); | 2630 | |
| 2542 | _ = try r.discard(.limited(size)); // we do not care about the content of the segments | 2631 | const size = try std.leb.readUleb128(u32, reader); |
| | 2632 | try writer.print("size {d}\n", .{size}); |
| | 2633 | try reader.skipBytes(size, .{}); // we do not care about the content of the segments |
| 2543 | } | 2634 | } |
| 2544 | }, | 2635 | }, |
| 2545 | else => unreachable, | 2636 | else => unreachable, |
| 2546 | } | 2637 | } |
| 2547 | } | 2638 | } |
| 2548 | | 2639 | |
| 2549 | fn parseDumpType(step: *Step, comptime E: type, r: *std.io.Reader, bw: *Writer) !E { | 2640 | fn parseDumpType(step: *Step, comptime E: type, reader: anytype, writer: anytype) !E { |
| 2550 | const tag = r.takeEnum(E, .little) catch |err| switch (err) { | 2641 | const byte = try reader.readByte(); |
| 2551 | error.InvalidEnumTag => return step.fail("invalid wasm type value", .{}), | 2642 | const tag = std.enums.fromInt(E, byte) orelse { |
| 2552 | else => |e| return e, | 2643 | return step.fail("invalid wasm type value '{d}'", .{byte}); |
| 2553 | }; | 2644 | }; |
| 2554 | try bw.print("type {s}\n", .{@tagName(tag)}); | 2645 | try writer.print("type {s}\n", .{@tagName(tag)}); |
| 2555 | return tag; | 2646 | return tag; |
| 2556 | } | 2647 | } |
| 2557 | | 2648 | |
| 2558 | fn parseDumpLimits(r: *std.io.Reader, bw: *Writer) !void { | 2649 | fn parseDumpLimits(reader: anytype, writer: anytype) !void { |
| 2559 | const flags = try r.takeLeb128(u8); | 2650 | const flags = try std.leb.readUleb128(u8, reader); |
| 2560 | const min = try r.takeLeb128(u32); | 2651 | const min = try std.leb.readUleb128(u32, reader); |
| 2561 | | 2652 | |
| 2562 | try bw.print("min {x}\n", .{min}); | 2653 | try writer.print("min {x}\n", .{min}); |
| 2563 | if (flags != 0) try bw.print("max {x}\n", .{try r.takeLeb128(u32)}); | 2654 | if (flags != 0) { |
| | 2655 | try writer.print("max {x}\n", .{try std.leb.readUleb128(u32, reader)}); |
| | 2656 | } |
| 2564 | } | 2657 | } |
| 2565 | | 2658 | |
| 2566 | fn parseDumpInit(step: *Step, r: *std.io.Reader, bw: *Writer) !void { | 2659 | fn parseDumpInit(step: *Step, reader: anytype, writer: anytype) !void { |
| 2567 | const opcode = r.takeEnum(std.wasm.Opcode, .little) catch |err| switch (err) { | 2660 | const byte = try reader.readByte(); |
| 2568 | error.InvalidEnumTag => return step.fail("invalid wasm opcode", .{}), | 2661 | const opcode = std.enums.fromInt(std.wasm.Opcode, byte) orelse { |
| 2569 | else => |e| return e, | 2662 | return step.fail("invalid wasm opcode '{d}'", .{byte}); |
| 2570 | }; | 2663 | }; |
| 2571 | switch (opcode) { | 2664 | switch (opcode) { |
| 2572 | .i32_const => try bw.print("i32.const {x}\n", .{try r.takeLeb128(i32)}), | 2665 | .i32_const => try writer.print("i32.const {x}\n", .{try std.leb.readIleb128(i32, reader)}), |
| 2573 | .i64_const => try bw.print("i64.const {x}\n", .{try r.takeLeb128(i64)}), | 2666 | .i64_const => try writer.print("i64.const {x}\n", .{try std.leb.readIleb128(i64, reader)}), |
| 2574 | .f32_const => try bw.print("f32.const {x}\n", .{@as(f32, @bitCast(try r.takeInt(u32, .little)))}), | 2667 | .f32_const => try writer.print("f32.const {x}\n", .{@as(f32, @bitCast(try reader.readInt(u32, .little)))}), |
| 2575 | .f64_const => try bw.print("f64.const {x}\n", .{@as(f64, @bitCast(try r.takeInt(u64, .little)))}), | 2668 | .f64_const => try writer.print("f64.const {x}\n", .{@as(f64, @bitCast(try reader.readInt(u64, .little)))}), |
| 2576 | .global_get => try bw.print("global.get {x}\n", .{try r.takeLeb128(u32)}), | 2669 | .global_get => try writer.print("global.get {x}\n", .{try std.leb.readUleb128(u32, reader)}), |
| 2577 | else => unreachable, | 2670 | else => unreachable, |
| 2578 | } | 2671 | } |
| 2579 | const end_opcode = try r.takeLeb128(u8); | 2672 | const end_opcode = try std.leb.readUleb128(u8, reader); |
| 2580 | if (end_opcode != @intFromEnum(std.wasm.Opcode.end)) { | 2673 | if (end_opcode != @intFromEnum(std.wasm.Opcode.end)) { |
| 2581 | return step.fail("expected 'end' opcode in init expression", .{}); | 2674 | return step.fail("expected 'end' opcode in init expression", .{}); |
| 2582 | } | 2675 | } |
| 2583 | } | 2676 | } |
| 2584 | | 2677 | |
| 2585 | /// https://webassembly.github.io/spec/core/appendix/custom.html | 2678 | /// https://webassembly.github.io/spec/core/appendix/custom.html |
| 2586 | fn parseDumpNames(step: *Step, r: *std.io.Reader, bw: *Writer) !void { | 2679 | fn parseDumpNames(step: *Step, reader: anytype, writer: anytype, data: []const u8) !void { |
| 2587 | var subsection_br: std.io.Reader = undefined; | 2680 | while (reader.context.pos < data.len) { |
| 2588 | while (r.seek < r.buffer.len) { | 2681 | switch (try parseDumpType(step, std.wasm.NameSubsection, reader, writer)) { |
| 2589 | switch (try parseDumpType(step, std.wasm.NameSubsection, r, bw)) { | | |
| 2590 | // The module name subsection ... consists of a single name | 2682 | // The module name subsection ... consists of a single name |
| 2591 | // that is assigned to the module itself. | 2683 | // that is assigned to the module itself. |
| 2592 | .module => { | 2684 | .module => { |
| 2593 | subsection_br = .fixed(try r.take(try r.takeLeb128(u32))); | 2685 | const size = try std.leb.readUleb128(u32, reader); |
| 2594 | const name = try subsection_br.take(try subsection_br.takeLeb128(u32)); | 2686 | const name_len = try std.leb.readUleb128(u32, reader); |
| 2595 | try bw.print( | 2687 | if (size != name_len + 1) return error.BadSubsectionSize; |
| 2596 | \\name {s} | 2688 | if (reader.context.pos + name_len > data.len) return error.UnexpectedEndOfStream; |
| 2597 | \\ | 2689 | try writer.print("name {s}\n", .{data[reader.context.pos..][0..name_len]}); |
| 2598 | , .{name}); | 2690 | reader.context.pos += name_len; |
| 2599 | if (subsection_br.seek != subsection_br.buffer.len) return error.BadSubsectionSize; | | |
| 2600 | }, | 2691 | }, |
| 2601 | | 2692 | |
| 2602 | // The function name subsection ... consists of a name map | 2693 | // The function name subsection ... consists of a name map |
| 2603 | // assigning function names to function indices. | 2694 | // assigning function names to function indices. |
| 2604 | .function, .global, .data_segment => { | 2695 | .function, .global, .data_segment => { |
| 2605 | subsection_br = .fixed(try r.take(try r.takeLeb128(u32))); | 2696 | const size = try std.leb.readUleb128(u32, reader); |
| 2606 | const entries = try r.takeLeb128(u32); | 2697 | const entries = try std.leb.readUleb128(u32, reader); |
| 2607 | try bw.print( | 2698 | try writer.print( |
| | 2699 | \\size {d} |
| 2608 | \\names {d} | 2700 | \\names {d} |
| 2609 | \\ | 2701 | \\ |
| 2610 | , .{entries}); | 2702 | , .{ size, entries }); |
| 2611 | for (0..entries) |_| { | 2703 | for (0..entries) |_| { |
| 2612 | const index = try r.takeLeb128(u32); | 2704 | const index = try std.leb.readUleb128(u32, reader); |
| 2613 | const name = try r.take(try r.takeLeb128(u32)); | 2705 | const name_len = try std.leb.readUleb128(u32, reader); |
| 2614 | try bw.print( | 2706 | if (reader.context.pos + name_len > data.len) return error.UnexpectedEndOfStream; |
| | 2707 | const name = data[reader.context.pos..][0..name_len]; |
| | 2708 | reader.context.pos += name.len; |
| | 2709 | |
| | 2710 | try writer.print( |
| 2615 | \\index {d} | 2711 | \\index {d} |
| 2616 | \\name {s} | 2712 | \\name {s} |
| 2617 | \\ | 2713 | \\ |
| 2618 | , .{ index, name }); | 2714 | , .{ index, name }); |
| 2619 | } | 2715 | } |
| 2620 | if (subsection_br.seek != subsection_br.buffer.len) return error.BadSubsectionSize; | | |
| 2621 | }, | 2716 | }, |
| 2622 | | 2717 | |
| 2623 | // The local name subsection ... consists of an indirect name | 2718 | // The local name subsection ... consists of an indirect name |
| ... | @@ -2632,49 +2727,52 @@ const WasmDumper = struct { | ... | @@ -2632,49 +2727,52 @@ const WasmDumper = struct { |
| 2632 | } | 2727 | } |
| 2633 | } | 2728 | } |
| 2634 | | 2729 | |
| 2635 | fn parseDumpProducers(r: *std.io.Reader, bw: *Writer) !void { | 2730 | fn parseDumpProducers(reader: anytype, writer: anytype, data: []const u8) !void { |
| 2636 | const field_count = try r.takeLeb128(u32); | 2731 | const field_count = try std.leb.readUleb128(u32, reader); |
| 2637 | try bw.print( | 2732 | try writer.print("fields {d}\n", .{field_count}); |
| 2638 | \\fields {d} | | |
| 2639 | \\ | | |
| 2640 | , .{field_count}); | | |
| 2641 | var current_field: u32 = 0; | 2733 | var current_field: u32 = 0; |
| 2642 | while (current_field < field_count) : (current_field += 1) { | 2734 | while (current_field < field_count) : (current_field += 1) { |
| 2643 | const field_name = try r.take(try r.takeLeb128(u32)); | 2735 | const field_name_length = try std.leb.readUleb128(u32, reader); |
| 2644 | const value_count = try r.takeLeb128(u32); | 2736 | const field_name = data[reader.context.pos..][0..field_name_length]; |
| 2645 | try bw.print( | 2737 | reader.context.pos += field_name_length; |
| | 2738 | |
| | 2739 | const value_count = try std.leb.readUleb128(u32, reader); |
| | 2740 | try writer.print( |
| 2646 | \\field_name {s} | 2741 | \\field_name {s} |
| 2647 | \\values {d} | 2742 | \\values {d} |
| 2648 | \\ | | |
| 2649 | , .{ field_name, value_count }); | 2743 | , .{ field_name, value_count }); |
| | 2744 | try writer.writeByte('\n'); |
| 2650 | var current_value: u32 = 0; | 2745 | var current_value: u32 = 0; |
| 2651 | while (current_value < value_count) : (current_value += 1) { | 2746 | while (current_value < value_count) : (current_value += 1) { |
| 2652 | const value = try r.take(try r.takeLeb128(u32)); | 2747 | const value_length = try std.leb.readUleb128(u32, reader); |
| 2653 | const version = try r.take(try r.takeLeb128(u32)); | 2748 | const value = data[reader.context.pos..][0..value_length]; |
| 2654 | try bw.print( | 2749 | reader.context.pos += value_length; |
| | 2750 | |
| | 2751 | const version_length = try std.leb.readUleb128(u32, reader); |
| | 2752 | const version = data[reader.context.pos..][0..version_length]; |
| | 2753 | reader.context.pos += version_length; |
| | 2754 | |
| | 2755 | try writer.print( |
| 2655 | \\value_name {s} | 2756 | \\value_name {s} |
| 2656 | \\version {s} | 2757 | \\version {s} |
| 2657 | \\ | | |
| 2658 | , .{ value, version }); | 2758 | , .{ value, version }); |
| | 2759 | try writer.writeByte('\n'); |
| 2659 | } | 2760 | } |
| 2660 | } | 2761 | } |
| 2661 | } | 2762 | } |
| 2662 | | 2763 | |
| 2663 | fn parseDumpFeatures(r: *std.io.Reader, bw: *Writer) !void { | 2764 | fn parseDumpFeatures(reader: anytype, writer: anytype, data: []const u8) !void { |
| 2664 | const feature_count = try r.takeLeb128(u32); | 2765 | const feature_count = try std.leb.readUleb128(u32, reader); |
| 2665 | try bw.print( | 2766 | try writer.print("features {d}\n", .{feature_count}); |
| 2666 | \\features {d} | | |
| 2667 | \\ | | |
| 2668 | , .{feature_count}); | | |
| 2669 | | 2767 | |
| 2670 | var index: u32 = 0; | 2768 | var index: u32 = 0; |
| 2671 | while (index < feature_count) : (index += 1) { | 2769 | while (index < feature_count) : (index += 1) { |
| 2672 | const prefix_byte = try r.takeLeb128(u8); | 2770 | const prefix_byte = try std.leb.readUleb128(u8, reader); |
| 2673 | const feature_name = try r.take(try r.takeLeb128(u32)); | 2771 | const name_length = try std.leb.readUleb128(u32, reader); |
| 2674 | try bw.print( | 2772 | const feature_name = data[reader.context.pos..][0..name_length]; |
| 2675 | \\{c} {s} | 2773 | reader.context.pos += name_length; |
| 2676 | \\ | 2774 | |
| 2677 | , .{ prefix_byte, feature_name }); | 2775 | try writer.print("{c} {s}\n", .{ prefix_byte, feature_name }); |
| 2678 | } | 2776 | } |
| 2679 | } | 2777 | } |
| 2680 | }; | 2778 | }; |