| ... | ... | @@ -3794,261 +3794,6 @@ pub fn findFirst(comptime T: type, haystack: []align(1) const T, start: usize, p |
| 3794 | 3794 | return i; |
| 3795 | 3795 | } |
| 3796 | 3796 | |
| 3797 | | // fn snapshotState(self: *MachO) !void { |
| 3798 | | // const emit = self.base.options.emit orelse { |
| 3799 | | // log.debug("no emit directory found; skipping snapshot...", .{}); |
| 3800 | | // return; |
| 3801 | | // }; |
| 3802 | | |
| 3803 | | // const Snapshot = struct { |
| 3804 | | // const Node = struct { |
| 3805 | | // const Tag = enum { |
| 3806 | | // section_start, |
| 3807 | | // section_end, |
| 3808 | | // atom_start, |
| 3809 | | // atom_end, |
| 3810 | | // relocation, |
| 3811 | | |
| 3812 | | // pub fn jsonStringify( |
| 3813 | | // tag: Tag, |
| 3814 | | // options: std.json.StringifyOptions, |
| 3815 | | // out_stream: anytype, |
| 3816 | | // ) !void { |
| 3817 | | // _ = options; |
| 3818 | | // switch (tag) { |
| 3819 | | // .section_start => try out_stream.writeAll("\"section_start\""), |
| 3820 | | // .section_end => try out_stream.writeAll("\"section_end\""), |
| 3821 | | // .atom_start => try out_stream.writeAll("\"atom_start\""), |
| 3822 | | // .atom_end => try out_stream.writeAll("\"atom_end\""), |
| 3823 | | // .relocation => try out_stream.writeAll("\"relocation\""), |
| 3824 | | // } |
| 3825 | | // } |
| 3826 | | // }; |
| 3827 | | // const Payload = struct { |
| 3828 | | // name: []const u8 = "", |
| 3829 | | // aliases: [][]const u8 = &[0][]const u8{}, |
| 3830 | | // is_global: bool = false, |
| 3831 | | // target: u64 = 0, |
| 3832 | | // }; |
| 3833 | | // address: u64, |
| 3834 | | // tag: Tag, |
| 3835 | | // payload: Payload, |
| 3836 | | // }; |
| 3837 | | // timestamp: i128, |
| 3838 | | // nodes: []Node, |
| 3839 | | // }; |
| 3840 | | |
| 3841 | | // var arena_allocator = std.heap.ArenaAllocator.init(self.base.allocator); |
| 3842 | | // defer arena_allocator.deinit(); |
| 3843 | | // const arena = arena_allocator.allocator(); |
| 3844 | | |
| 3845 | | // const out_file = try emit.directory.handle.createFile("snapshots.json", .{ |
| 3846 | | // .truncate = false, |
| 3847 | | // .read = true, |
| 3848 | | // }); |
| 3849 | | // defer out_file.close(); |
| 3850 | | |
| 3851 | | // if (out_file.seekFromEnd(-1)) { |
| 3852 | | // try out_file.writer().writeByte(','); |
| 3853 | | // } else |err| switch (err) { |
| 3854 | | // error.Unseekable => try out_file.writer().writeByte('['), |
| 3855 | | // else => |e| return e, |
| 3856 | | // } |
| 3857 | | // const writer = out_file.writer(); |
| 3858 | | |
| 3859 | | // var snapshot = Snapshot{ |
| 3860 | | // .timestamp = std.time.nanoTimestamp(), |
| 3861 | | // .nodes = undefined, |
| 3862 | | // }; |
| 3863 | | // var nodes = std.ArrayList(Snapshot.Node).init(arena); |
| 3864 | | |
| 3865 | | // for (self.section_ordinals.keys()) |key| { |
| 3866 | | // const sect = self.getSection(key); |
| 3867 | | // const sect_name = try std.fmt.allocPrint(arena, "{s},{s}", .{ sect.segName(), sect.sectName() }); |
| 3868 | | // try nodes.append(.{ |
| 3869 | | // .address = sect.addr, |
| 3870 | | // .tag = .section_start, |
| 3871 | | // .payload = .{ .name = sect_name }, |
| 3872 | | // }); |
| 3873 | | |
| 3874 | | // const is_tlv = sect.type_() == macho.S_THREAD_LOCAL_VARIABLES; |
| 3875 | | |
| 3876 | | // var atom: *Atom = self.atoms.get(key) orelse { |
| 3877 | | // try nodes.append(.{ |
| 3878 | | // .address = sect.addr + sect.size, |
| 3879 | | // .tag = .section_end, |
| 3880 | | // .payload = .{}, |
| 3881 | | // }); |
| 3882 | | // continue; |
| 3883 | | // }; |
| 3884 | | |
| 3885 | | // while (atom.prev) |prev| { |
| 3886 | | // atom = prev; |
| 3887 | | // } |
| 3888 | | |
| 3889 | | // while (true) { |
| 3890 | | // const atom_sym = atom.getSymbol(self); |
| 3891 | | // var node = Snapshot.Node{ |
| 3892 | | // .address = atom_sym.n_value, |
| 3893 | | // .tag = .atom_start, |
| 3894 | | // .payload = .{ |
| 3895 | | // .name = atom.getName(self), |
| 3896 | | // .is_global = self.globals.contains(atom.getName(self)), |
| 3897 | | // }, |
| 3898 | | // }; |
| 3899 | | |
| 3900 | | // var aliases = std.ArrayList([]const u8).init(arena); |
| 3901 | | // for (atom.contained.items) |sym_off| { |
| 3902 | | // if (sym_off.offset == 0) { |
| 3903 | | // try aliases.append(self.getSymbolName(.{ |
| 3904 | | // .sym_index = sym_off.sym_index, |
| 3905 | | // .file = atom.file, |
| 3906 | | // })); |
| 3907 | | // } |
| 3908 | | // } |
| 3909 | | // node.payload.aliases = aliases.toOwnedSlice(); |
| 3910 | | // try nodes.append(node); |
| 3911 | | |
| 3912 | | // var relocs = try std.ArrayList(Snapshot.Node).initCapacity(arena, atom.relocs.items.len); |
| 3913 | | // for (atom.relocs.items) |rel| { |
| 3914 | | // const source_addr = blk: { |
| 3915 | | // const source_sym = atom.getSymbol(self); |
| 3916 | | // break :blk source_sym.n_value + rel.offset; |
| 3917 | | // }; |
| 3918 | | // const target_addr = blk: { |
| 3919 | | // const target_atom = rel.getTargetAtom(self) orelse { |
| 3920 | | // // If there is no atom for target, we still need to check for special, atom-less |
| 3921 | | // // symbols such as `___dso_handle`. |
| 3922 | | // const target_name = self.getSymbolName(rel.target); |
| 3923 | | // if (self.globals.contains(target_name)) { |
| 3924 | | // const atomless_sym = self.getSymbol(rel.target); |
| 3925 | | // break :blk atomless_sym.n_value; |
| 3926 | | // } |
| 3927 | | // break :blk 0; |
| 3928 | | // }; |
| 3929 | | // const target_sym = if (target_atom.isSymbolContained(rel.target, self)) |
| 3930 | | // self.getSymbol(rel.target) |
| 3931 | | // else |
| 3932 | | // target_atom.getSymbol(self); |
| 3933 | | // const base_address: u64 = if (is_tlv) base_address: { |
| 3934 | | // const sect_id: u16 = sect_id: { |
| 3935 | | // if (self.tlv_data_section_index) |i| { |
| 3936 | | // break :sect_id i; |
| 3937 | | // } else if (self.tlv_bss_section_index) |i| { |
| 3938 | | // break :sect_id i; |
| 3939 | | // } else unreachable; |
| 3940 | | // }; |
| 3941 | | // break :base_address self.getSection(.{ |
| 3942 | | // .seg = self.data_segment_cmd_index.?, |
| 3943 | | // .sect = sect_id, |
| 3944 | | // }).addr; |
| 3945 | | // } else 0; |
| 3946 | | // break :blk target_sym.n_value - base_address; |
| 3947 | | // }; |
| 3948 | | |
| 3949 | | // relocs.appendAssumeCapacity(.{ |
| 3950 | | // .address = source_addr, |
| 3951 | | // .tag = .relocation, |
| 3952 | | // .payload = .{ .target = target_addr }, |
| 3953 | | // }); |
| 3954 | | // } |
| 3955 | | |
| 3956 | | // if (atom.contained.items.len == 0) { |
| 3957 | | // try nodes.appendSlice(relocs.items); |
| 3958 | | // } else { |
| 3959 | | // // Need to reverse iteration order of relocs since by default for relocatable sources |
| 3960 | | // // they come in reverse. For linking, this doesn't matter in any way, however, for |
| 3961 | | // // arranging the memoryline for displaying it does. |
| 3962 | | // std.mem.reverse(Snapshot.Node, relocs.items); |
| 3963 | | |
| 3964 | | // var next_i: usize = 0; |
| 3965 | | // var last_rel: usize = 0; |
| 3966 | | // while (next_i < atom.contained.items.len) : (next_i += 1) { |
| 3967 | | // const loc = SymbolWithLoc{ |
| 3968 | | // .sym_index = atom.contained.items[next_i].sym_index, |
| 3969 | | // .file = atom.file, |
| 3970 | | // }; |
| 3971 | | // const cont_sym = self.getSymbol(loc); |
| 3972 | | // const cont_sym_name = self.getSymbolName(loc); |
| 3973 | | // var contained_node = Snapshot.Node{ |
| 3974 | | // .address = cont_sym.n_value, |
| 3975 | | // .tag = .atom_start, |
| 3976 | | // .payload = .{ |
| 3977 | | // .name = cont_sym_name, |
| 3978 | | // .is_global = self.globals.contains(cont_sym_name), |
| 3979 | | // }, |
| 3980 | | // }; |
| 3981 | | |
| 3982 | | // // Accumulate aliases |
| 3983 | | // var inner_aliases = std.ArrayList([]const u8).init(arena); |
| 3984 | | // while (true) { |
| 3985 | | // if (next_i + 1 >= atom.contained.items.len) break; |
| 3986 | | // const next_sym_loc = SymbolWithLoc{ |
| 3987 | | // .sym_index = atom.contained.items[next_i + 1].sym_index, |
| 3988 | | // .file = atom.file, |
| 3989 | | // }; |
| 3990 | | // const next_sym = self.getSymbol(next_sym_loc); |
| 3991 | | // if (next_sym.n_value != cont_sym.n_value) break; |
| 3992 | | // const next_sym_name = self.getSymbolName(next_sym_loc); |
| 3993 | | // if (self.globals.contains(next_sym_name)) { |
| 3994 | | // try inner_aliases.append(contained_node.payload.name); |
| 3995 | | // contained_node.payload.name = next_sym_name; |
| 3996 | | // contained_node.payload.is_global = true; |
| 3997 | | // } else try inner_aliases.append(next_sym_name); |
| 3998 | | // next_i += 1; |
| 3999 | | // } |
| 4000 | | |
| 4001 | | // const cont_size = if (next_i + 1 < atom.contained.items.len) |
| 4002 | | // self.getSymbol(.{ |
| 4003 | | // .sym_index = atom.contained.items[next_i + 1].sym_index, |
| 4004 | | // .file = atom.file, |
| 4005 | | // }).n_value - cont_sym.n_value |
| 4006 | | // else |
| 4007 | | // atom_sym.n_value + atom.size - cont_sym.n_value; |
| 4008 | | |
| 4009 | | // contained_node.payload.aliases = inner_aliases.toOwnedSlice(); |
| 4010 | | // try nodes.append(contained_node); |
| 4011 | | |
| 4012 | | // for (relocs.items[last_rel..]) |rel| { |
| 4013 | | // if (rel.address >= cont_sym.n_value + cont_size) { |
| 4014 | | // break; |
| 4015 | | // } |
| 4016 | | // try nodes.append(rel); |
| 4017 | | // last_rel += 1; |
| 4018 | | // } |
| 4019 | | |
| 4020 | | // try nodes.append(.{ |
| 4021 | | // .address = cont_sym.n_value + cont_size, |
| 4022 | | // .tag = .atom_end, |
| 4023 | | // .payload = .{}, |
| 4024 | | // }); |
| 4025 | | // } |
| 4026 | | // } |
| 4027 | | |
| 4028 | | // try nodes.append(.{ |
| 4029 | | // .address = atom_sym.n_value + atom.size, |
| 4030 | | // .tag = .atom_end, |
| 4031 | | // .payload = .{}, |
| 4032 | | // }); |
| 4033 | | |
| 4034 | | // if (atom.next) |next| { |
| 4035 | | // atom = next; |
| 4036 | | // } else break; |
| 4037 | | // } |
| 4038 | | |
| 4039 | | // try nodes.append(.{ |
| 4040 | | // .address = sect.addr + sect.size, |
| 4041 | | // .tag = .section_end, |
| 4042 | | // .payload = .{}, |
| 4043 | | // }); |
| 4044 | | // } |
| 4045 | | |
| 4046 | | // snapshot.nodes = nodes.toOwnedSlice(); |
| 4047 | | |
| 4048 | | // try std.json.stringify(snapshot, .{}, writer); |
| 4049 | | // try writer.writeByte(']'); |
| 4050 | | // } |
| 4051 | | |
| 4052 | 3797 | pub fn logSections(self: *MachO) void { |
| 4053 | 3798 | log.debug("sections:", .{}); |
| 4054 | 3799 | for (self.sections.items(.header), 0..) |header, i| { |