authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-16 13:16:30-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-20 22:58:16-04:00
log2c76020e772787c3bc8bf4bcf56e2e0f41016b26
tree50f1438b6b05e9153f4712ec88d7b4349ce03096
parent618b0eb3d3ac5ecb84acb4296c591f53ba9c4298

debug: load the macho unwind sections from the already-mapped image


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

lib/std/debug.zig+18-25
......@@ -1259,24 +1259,7 @@ fn readMachODebugInfo(allocator: mem.Allocator, macho_file: File) !ModuleDebugIn
12591259 .ncmds = hdr.ncmds,
12601260 .buffer = mapped_mem[@sizeOf(macho.mach_header_64)..][0..hdr.sizeofcmds],
12611261 };
1262 var unwind_info: ?[]const u8 = null;
1263 var eh_frame: ?[]const u8 = null;
12641262 const symtab = while (it.next()) |cmd| switch (cmd.cmd()) {
1265 .SEGMENT_64 => {
1266 for (cmd.getSections()) |sect| {
1267 if (std.mem.eql(u8, "__TEXT", sect.segName())) {
1268 if (mem.eql(u8, "__unwind_info", sect.sectName())) {
1269 unwind_info = try chopSlice(mapped_mem, sect.offset, sect.size);
1270 continue;
1271 }
1272
1273 if (mem.eql(u8, "__eh_frame", sect.sectName())) {
1274 eh_frame = try chopSlice(mapped_mem, sect.offset, sect.size);
1275 continue;
1276 }
1277 }
1278 }
1279 },
12801263 .SYMTAB => break cmd.cast(macho.symtab_command).?,
12811264 else => {},
12821265 } else return error.MissingDebugInfo;
......@@ -1386,8 +1369,6 @@ fn readMachODebugInfo(allocator: mem.Allocator, macho_file: File) !ModuleDebugIn
13861369 .ofiles = ModuleDebugInfo.OFileTable.init(allocator),
13871370 .symbols = symbols,
13881371 .strings = strings,
1389 .unwind_info = unwind_info,
1390 .eh_frame = eh_frame,
13911372 };
13921373}
13931374
......@@ -1602,19 +1583,28 @@ pub const DebugInfo = struct {
16021583 )[0..header.sizeofcmds]),
16031584 };
16041585
1586 var unwind_info: ?[]const u8 = null;
1587 var eh_frame: ?[]const u8 = null;
16051588 while (it.next()) |cmd| switch (cmd.cmd()) {
16061589 .SEGMENT_64 => {
16071590 const segment_cmd = cmd.cast(macho.segment_command_64).?;
16081591 if (!mem.eql(u8, "__TEXT", segment_cmd.segName())) continue;
16091592
1610 const original_address = address - vmaddr_slide;
1611 const seg_start = segment_cmd.vmaddr;
1593 const seg_start = segment_cmd.vmaddr + vmaddr_slide;
16121594 const seg_end = seg_start + segment_cmd.vmsize;
1613 if (original_address >= seg_start and original_address < seg_end) {
1595 if (address >= seg_start and address < seg_end) {
16141596 if (self.address_map.get(base_address)) |obj_di| {
16151597 return obj_di;
16161598 }
16171599
1600 for (cmd.getSections()) |sect| {
1601 if (mem.eql(u8, "__unwind_info", sect.sectName())) {
1602 unwind_info = @as([*]const u8, @ptrFromInt(sect.addr + vmaddr_slide))[0..sect.size];
1603 } else if (mem.eql(u8, "__eh_frame", sect.sectName())) {
1604 eh_frame = @as([*]const u8, @ptrFromInt(sect.addr + vmaddr_slide))[0..sect.size];
1605 }
1606 }
1607
16181608 const obj_di = try self.allocator.create(ModuleDebugInfo);
16191609 errdefer self.allocator.destroy(obj_di);
16201610
......@@ -1628,6 +1618,8 @@ pub const DebugInfo = struct {
16281618 obj_di.* = try readMachODebugInfo(self.allocator, macho_file);
16291619 obj_di.base_address = base_address;
16301620 obj_di.vmaddr_slide = vmaddr_slide;
1621 obj_di.unwind_info = unwind_info;
1622 obj_di.eh_frame = eh_frame;
16311623
16321624 try self.address_map.putNoClobber(base_address, obj_di);
16331625
......@@ -1932,9 +1924,10 @@ pub const ModuleDebugInfo = switch (native_os) {
19321924 symbols: []const MachoSymbol,
19331925 strings: [:0]const u8,
19341926 ofiles: OFileTable,
1935 // Backed by mapped_memory
1936 unwind_info: ?[]const u8,
1937 eh_frame: ?[]const u8,
1927
1928 // Backed by the in-memory sections mapped by the loader
1929 unwind_info: ?[]const u8 = null,
1930 eh_frame: ?[]const u8 = null,
19381931
19391932 const OFileTable = std.StringHashMap(OFileInfo);
19401933 const OFileInfo = struct {