| 1 |
use super::arch::ArchAarch64; |
| 2 |
use super::unwind_rule::UnwindRuleAarch64; |
| 3 |
use crate::instruction_analysis::InstructionAnalysis; |
| 4 |
use crate::macho::{CompactUnwindInfoUnwinderError, CompactUnwindInfoUnwinding, CuiUnwindResult}; |
| 5 |
use macho_unwind_info::opcodes::OpcodeArm64; |
| 6 |
use macho_unwind_info::Function; |
| 7 |
|
| 8 |
impl CompactUnwindInfoUnwinding for ArchAarch64 { |
| 9 |
fn unwind_frame( |
| 10 |
function: Function, |
| 11 |
is_first_frame: bool, |
| 12 |
address_offset_within_function: usize, |
| 13 |
function_bytes: Option<&[u8]>, |
| 14 |
) -> Result<CuiUnwindResult<UnwindRuleAarch64>, CompactUnwindInfoUnwinderError> { |
| 15 |
let opcode = OpcodeArm64::parse(function.opcode); |
| 16 |
if is_first_frame { |
| 17 |
if opcode == OpcodeArm64::Null { |
| 18 |
return Ok(CuiUnwindResult::ExecRule(UnwindRuleAarch64::NoOp)); |
| 19 |
} |
| 20 |
// The pc might be in a prologue or an epilogue. The compact unwind info format ignores |
| 21 |
// prologues and epilogues; the opcodes only describe the function body. So we do some |
| 22 |
// instruction analysis to check for prologues and epilogues. |
| 23 |
if let Some(function_bytes) = function_bytes { |
| 24 |
if let Some(rule) = Self::rule_from_instruction_analysis( |
| 25 |
function_bytes, |
| 26 |
address_offset_within_function, |
| 27 |
) { |
| 28 |
// We are inside a prologue / epilogue. Ignore the opcode and use the rule from |
| 29 |
// instruction analysis. |
| 30 |
return Ok(CuiUnwindResult::ExecRule(rule)); |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// At this point we know with high certainty that we are in a function body. |
| 36 |
let r = match opcode { |
| 37 |
OpcodeArm64::Null => { |
| 38 |
return Err(CompactUnwindInfoUnwinderError::FunctionHasNoInfo); |
| 39 |
} |
| 40 |
OpcodeArm64::Frameless { |
| 41 |
stack_size_in_bytes, |
| 42 |
} => { |
| 43 |
if is_first_frame { |
| 44 |
if stack_size_in_bytes == 0 { |
| 45 |
CuiUnwindResult::ExecRule(UnwindRuleAarch64::NoOp) |
| 46 |
} else { |
| 47 |
CuiUnwindResult::ExecRule(UnwindRuleAarch64::OffsetSp { |
| 48 |
sp_offset_by_16: stack_size_in_bytes / 16, |
| 49 |
}) |
| 50 |
} |
| 51 |
} else { |
| 52 |
return Err(CompactUnwindInfoUnwinderError::CallerCannotBeFrameless); |
| 53 |
} |
| 54 |
} |
| 55 |
OpcodeArm64::Dwarf { eh_frame_fde } => CuiUnwindResult::NeedDwarf(eh_frame_fde), |
| 56 |
OpcodeArm64::FrameBased { .. } => { |
| 57 |
CuiUnwindResult::ExecRule(UnwindRuleAarch64::UseFramePointer) |
| 58 |
} |
| 59 |
OpcodeArm64::UnrecognizedKind(kind) => { |
| 60 |
return Err(CompactUnwindInfoUnwinderError::BadOpcodeKind(kind)) |
| 61 |
} |
| 62 |
}; |
| 63 |
Ok(r) |
| 64 |
} |
| 65 |
|
| 66 |
fn rule_for_stub_helper( |
| 67 |
offset: u32, |
| 68 |
) -> Result<CuiUnwindResult<UnwindRuleAarch64>, CompactUnwindInfoUnwinderError> { |
| 69 |
// shared: |
| 70 |
// +0x0 1d309c B1 94 48 10 adr x17, #0x100264330 |
| 71 |
// +0x4 1d30a0 1F 20 03 D5 nop |
| 72 |
// +0x8 1d30a4 F0 47 BF A9 stp x16, x17, [sp, #-0x10]! |
| 73 |
// +0xc 1d30a8 1F 20 03 D5 nop |
| 74 |
// +0x10 1d30ac F0 7A 32 58 ldr x16, #dyld_stub_binder_100238008 |
| 75 |
// +0x14 1d30b0 00 02 1F D6 br x16 |
| 76 |
// first stub: |
| 77 |
// +0x18 1d30b4 50 00 00 18 ldr w16, =0x1800005000000000 |
| 78 |
// +0x1c 1d30b8 F9 FF FF 17 b 0x1001d309c |
| 79 |
// +0x20 1d30bc 00 00 00 00 (padding) |
| 80 |
// second stub: |
| 81 |
// +0x24 1d30c0 50 00 00 18 ldr w16, =0x1800005000000012 |
| 82 |
// +0x28 1d30c4 F6 FF FF 17 b 0x1001d309c |
| 83 |
// +0x2c 1d30c8 00 00 00 00 (padding) |
| 84 |
let rule = if offset < 0xc { |
| 85 |
// Stack pointer hasn't been touched, just follow lr |
| 86 |
UnwindRuleAarch64::NoOp |
| 87 |
} else if offset < 0x18 { |
| 88 |
// Add 0x10 to the stack pointer and follow lr |
| 89 |
UnwindRuleAarch64::OffsetSp { sp_offset_by_16: 1 } |
| 90 |
} else { |
| 91 |
// Stack pointer hasn't been touched, just follow lr |
| 92 |
UnwindRuleAarch64::NoOp |
| 93 |
}; |
| 94 |
Ok(CuiUnwindResult::ExecRule(rule)) |
| 95 |
} |
| 96 |
} |