authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-02 16:39:45+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-30 13:44:50+01:00
loge4dbfc109bb32334f8d0dde9277ddd80c9b6f126
tree37c8f79c045abd769fec803ae009343acd412bf1
parent8fdcdb8c69712ebbfbd06e0c18d373eee462fe31
signaturelock-open Commit is signed but in an unrecognized format.

dont dupe state you silly billy


1 files changed, 32 insertions(+), 25 deletions(-)

lib/std/debug/SelfInfo.zig+32-25
......@@ -100,8 +100,6 @@ const Module = switch (native_os) {
100100 text_base: usize,
101101 load_offset: usize,
102102 name: []const u8,
103 unwind_info: ?[]const u8,
104 eh_frame: ?[]const u8,
105103 fn key(m: *const Module) usize {
106104 return m.text_base;
107105 }
......@@ -120,11 +118,11 @@ const Module = switch (native_os) {
120118 .ncmds = header.ncmds,
121119 .buffer = @as([*]u8, @ptrCast(header))[@sizeOf(macho.mach_header_64)..][0..header.sizeofcmds],
122120 };
123 const text_segment_cmd, const text_sections = while (it.next()) |load_cmd| {
121 const text_segment_cmd = while (it.next()) |load_cmd| {
124122 if (load_cmd.cmd() != .SEGMENT_64) continue;
125123 const segment_cmd = load_cmd.cast(macho.segment_command_64).?;
126124 if (!mem.eql(u8, segment_cmd.segName(), "__TEXT")) continue;
127 break .{ segment_cmd, load_cmd.getSections() };
125 break segment_cmd;
128126 } else continue;
129127
130128 const seg_start = load_offset + text_segment_cmd.vmaddr;
......@@ -132,26 +130,12 @@ const Module = switch (native_os) {
132130 const seg_end = seg_start + text_segment_cmd.vmsize;
133131 if (address < seg_start or address >= seg_end) continue;
134132
135 // We've found the matching __TEXT segment. This is the image we need, but we must look
136 // for unwind info in it before returning.
137
138 var result: Module = .{
133 // We've found the matching __TEXT segment. This is the image we need.
134 return .{
139135 .text_base = text_base,
140136 .load_offset = load_offset,
141137 .name = mem.span(std.c._dyld_get_image_name(@intCast(image_idx))),
142 .unwind_info = null,
143 .eh_frame = null,
144138 };
145 for (text_sections) |sect| {
146 if (mem.eql(u8, sect.sectName(), "__unwind_info")) {
147 const sect_ptr: [*]u8 = @ptrFromInt(@as(usize, @intCast(load_offset + sect.addr)));
148 result.unwind_info = sect_ptr[0..@intCast(sect.size)];
149 } else if (mem.eql(u8, sect.sectName(), "__eh_frame")) {
150 const sect_ptr: [*]u8 = @ptrFromInt(@as(usize, @intCast(load_offset + sect.addr)));
151 result.eh_frame = sect_ptr[0..@intCast(sect.size)];
152 }
153 }
154 return result;
155139 }
156140 return error.MissingDebugInfo;
157141 }
......@@ -270,11 +254,35 @@ const Module = switch (native_os) {
270254 };
271255 }
272256 fn loadUnwindInfo(module: *const Module, gpa: Allocator, di: *Module.DebugInfo) !void {
273 if (di.unwind != null) return;
274257 _ = gpa;
258
259 const header: *std.macho.mach_header = @ptrFromInt(module.text_base);
260
261 var it: macho.LoadCommandIterator = .{
262 .ncmds = header.ncmds,
263 .buffer = @as([*]u8, @ptrCast(header))[@sizeOf(macho.mach_header_64)..][0..header.sizeofcmds],
264 };
265 const sections = while (it.next()) |load_cmd| {
266 if (load_cmd.cmd() != .SEGMENT_64) continue;
267 const segment_cmd = load_cmd.cast(macho.segment_command_64).?;
268 if (!mem.eql(u8, segment_cmd.segName(), "__TEXT")) continue;
269 break load_cmd.getSections();
270 } else unreachable;
271
272 var unwind_info: ?[]const u8 = null;
273 var eh_frame: ?[]const u8 = null;
274 for (sections) |sect| {
275 if (mem.eql(u8, sect.sectName(), "__unwind_info")) {
276 const sect_ptr: [*]u8 = @ptrFromInt(@as(usize, @intCast(module.load_offset + sect.addr)));
277 unwind_info = sect_ptr[0..@intCast(sect.size)];
278 } else if (mem.eql(u8, sect.sectName(), "__eh_frame")) {
279 const sect_ptr: [*]u8 = @ptrFromInt(@as(usize, @intCast(module.load_offset + sect.addr)));
280 eh_frame = sect_ptr[0..@intCast(sect.size)];
281 }
282 }
275283 di.unwind = .{
276 .unwind_info = module.unwind_info,
277 .eh_frame = module.eh_frame,
284 .unwind_info = unwind_info,
285 .eh_frame = eh_frame,
278286 };
279287 }
280288 fn getSymbolAtAddress(module: *const Module, gpa: Allocator, di: *DebugInfo, address: usize) !std.debug.Symbol {
......@@ -362,7 +370,6 @@ const Module = switch (native_os) {
362370 const DebugInfo = struct {
363371 unwind: ?struct {
364372 // Backed by the in-memory sections mapped by the loader
365 // MLUGG TODO: these are duplicated state. i actually reckon they should be removed from Module, and loadLocationInfo should be the one discovering them!
366373 unwind_info: ?[]const u8,
367374 eh_frame: ?[]const u8,
368375 },
......@@ -517,7 +524,7 @@ const Module = switch (native_os) {
517524 };
518525 };
519526 fn key(m: Module) usize {
520 return m.load_offset; // MLUGG TODO: is this technically valid? idk
527 return m.load_offset;
521528 }
522529 fn lookup(cache: *LookupCache, gpa: Allocator, address: usize) !Module {
523530 _ = cache;