authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-18 20:14:42-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-20 22:58:16-04:00
log253e6971ad77a9665348fd6a2085b2ffd8c84219
treeaf68ece38de03bc42a96a0c98ea3176530deecf5
parent4421b14878eb722da8a57069b2152f7b43ba7ffc

dwarf: implement aarch64 default register rules


3 files changed, 38 insertions(+), 38 deletions(-)

lib/std/dwarf.zig+14-29
......@@ -1740,21 +1740,7 @@ pub const DwarfInfo = struct {
17401740 context.reg_context.eh_frame = cie.version != 4;
17411741 context.reg_context.is_macho = di.is_macho;
17421742
1743 if (comptime builtin.target.isDarwin()) {
1744 std.debug.print(" state before:\n", .{});
1745 std.debug.print(" cfa {?x}:\n", .{context.cfa});
1746 for (context.thread_context.mcontext.ss.regs, 0..) |reg, i| {
1747 std.debug.print(" {}:0x{x}\n", .{i, reg});
1748 }
1749 std.debug.print(" fp:0x{x}\n", .{context.thread_context.mcontext.ss.fp});
1750 std.debug.print(" lr:0x{x}\n", .{context.thread_context.mcontext.ss.lr});
1751 std.debug.print(" sp:0x{x}\n", .{context.thread_context.mcontext.ss.sp});
1752 std.debug.print(" pc:0x{x}\n", .{context.thread_context.mcontext.ss.pc});
1753 }
1754
17551743 const row = try context.vm.runToNative(context.allocator, context.pc, cie, fde);
1756 std.debug.print(" ran to 0x{x}\n", .{row.offset + fde.pc_begin});
1757
17581744 context.cfa = switch (row.cfa.rule) {
17591745 .val_offset => |offset| blk: {
17601746 const register = row.cfa.register orelse return error.InvalidCFARule;
......@@ -1789,47 +1775,48 @@ pub const DwarfInfo = struct {
17891775
17901776 const RegisterUpdate = struct {
17911777 // Backed by thread_context
1792 old_value: []u8,
1778 dest: []u8,
17931779 // Backed by arena
1794 new_value: []const u8,
1780 src: []const u8,
17951781 prev: ?*@This(),
17961782 };
17971783
17981784 var update_tail: ?*RegisterUpdate = null;
1799 var has_next_ip = true;
1785 var has_return_address= true;
18001786 for (context.vm.rowColumns(row)) |column| {
18011787 if (column.register) |register| {
18021788 if (register == cie.return_address_register) {
1803 has_next_ip = column.rule != .undefined;
1789 has_return_address = column.rule != .undefined;
18041790 }
1805 std.debug.print(" updated {}\n", .{register});
18061791
1807 const old_value = try abi.regBytes(context.thread_context, register, context.reg_context);
1808 const new_value = try update_allocator.alloc(u8, old_value.len);
1792 const dest = try abi.regBytes(context.thread_context, register, context.reg_context);
1793 const src = try update_allocator.alloc(u8, dest.len);
18091794
18101795 const prev = update_tail;
18111796 update_tail = try update_allocator.create(RegisterUpdate);
18121797 update_tail.?.* = .{
1813 .old_value = old_value,
1814 .new_value = new_value,
1798 .dest = dest,
1799 .src = src,
18151800 .prev = prev,
18161801 };
18171802
18181803 try column.resolveValue(
18191804 context,
18201805 expression_context,
1821 new_value,
1806 src,
18221807 );
18231808 }
18241809 }
18251810
1811 // On all implemented architectures, the CFA is defined as being the previous frame's SP
18261812 (try abi.regValueNative(usize, context.thread_context, abi.spRegNum(context.reg_context), context.reg_context)).* = context.cfa.?;
1813
18271814 while (update_tail) |tail| {
1828 @memcpy(tail.old_value, tail.new_value);
1815 @memcpy(tail.dest, tail.src);
18291816 update_tail = tail.prev;
18301817 }
18311818
1832 if (has_next_ip) {
1819 if (has_return_address) {
18331820 context.pc = abi.stripInstructionPtrAuthCode(mem.readIntSliceNative(usize, try abi.regBytes(
18341821 context.thread_context,
18351822 cie.return_address_register,
......@@ -1838,10 +1825,8 @@ pub const DwarfInfo = struct {
18381825 } else {
18391826 context.pc = 0;
18401827 }
1841 (try abi.regValueNative(usize, context.thread_context, abi.ipRegNum(), context.reg_context)).* = context.pc;
1842 std.debug.print(" new context.pc: 0x{x}\n", .{context.pc});
18431828
1844 (try abi.regValueNative(usize, context.thread_context, abi.spRegNum(context.reg_context), context.reg_context)).* = context.cfa.?;
1829 (try abi.regValueNative(usize, context.thread_context, abi.ipRegNum(), context.reg_context)).* = context.pc;
18451830
18461831 // The call instruction will have pushed the address of the instruction that follows the call as the return address
18471832 // However, this return address may be past the end of the function if the caller was `noreturn`. By subtracting one,
lib/std/dwarf/abi.zig+15-6
......@@ -367,12 +367,21 @@ pub fn regBytes(
367367}
368368
369369/// Returns the ABI-defined default value this register has in the unwinding table
370/// before running any of the CIE instructions. The DWARF spec defines these values
371/// to be undefined, but allows ABI authors to override that default.
372pub fn getRegDefaultValue(reg_number: u8, out: []u8) void {
373
374 // Implement any ABI-specific rules here
370/// before running any of the CIE instructions. The DWARF spec defines these as having
371/// the .undefined rule by default, but allows ABI authors to override that.
372pub fn getRegDefaultValue(reg_number: u8, context: *std.dwarf.UnwindContext, out: []u8) !void {
373 switch (builtin.cpu.arch) {
374 .aarch64 => {
375 // Callee-saved registers are initialized as if they had the .same_value rule
376 if (reg_number >= 19 and reg_number <= 28) {
377 const src = try regBytes(context.thread_context, reg_number, context.reg_context);
378 if (src.len != out.len) return error.RegisterSizeMismatch;
379 @memcpy(out, src);
380 return;
381 }
382 },
383 else => {},
384 }
375385
376 _ = reg_number;
377386 @memset(out, undefined);
378387}
lib/std/dwarf/call_frame.zig+9-3
......@@ -295,12 +295,18 @@ pub const VirtualMachine = struct {
295295 switch (self.rule) {
296296 .default => {
297297 const register = self.register orelse return error.InvalidRegister;
298 abi.getRegDefaultValue(register, out);
298 try abi.getRegDefaultValue(register, context, out);
299299 },
300300 .undefined => {
301301 @memset(out, undefined);
302302 },
303 .same_value => {},
303 .same_value => {
304 // TODO: This copy could be eliminated if callers always copy the state then call this function to update it
305 const register = self.register orelse return error.InvalidRegister;
306 const src = try abi.regBytes(context.thread_context, register, context.reg_context);
307 if (src.len != out.len) return error.RegisterSizeMismatch;
308 @memcpy(out, src);
309 },
304310 .offset => |offset| {
305311 if (context.cfa) |cfa| {
306312 const addr = try applyOffset(cfa, offset);
......@@ -316,7 +322,7 @@ pub const VirtualMachine = struct {
316322 },
317323 .register => |register| {
318324 const src = try abi.regBytes(context.thread_context, register, context.reg_context);
319 if (src.len != out.len) return error.RegisterTypeMismatch;
325 if (src.len != out.len) return error.RegisterSizeMismatch;
320326 @memcpy(out, try abi.regBytes(context.thread_context, register, context.reg_context));
321327 },
322328 .expression => |expression| {