Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

ARM and Thumb branches point at their real targets (#363)

GGWAd57649cf7a4c
diff --git a/crates/disasm/src/decode.rs b/crates/disasm/src/decode.rs
index de134a60..6c3a3718 100644
--- a/crates/disasm/src/decode.rs
+++ b/crates/disasm/src/decode.rs
@@ -869,7 +869,15 @@ fn decode_a32(mode: Mode, bytes: &[u8], addr: u64, fmt: bool) -> Option<Insn> {
ops.push(Op::Addr(t));
}
Operand::BranchThumbOffset(off) => {
- let t = (addr.wrapping_add(pc_bias - 4)).wrapping_add((i64::from(off) * 2) as u64);
+ // The PC reads as the instruction address plus 4 (Thumb) or 8 (A32).
+ // yaxpeax already counts one halfword into CBZ/CBNZ and 16-bit conditional B offsets.
+ let counted = matches!(inst.opcode, Opcode::CBZ | Opcode::CBNZ) || (inst.opcode == Opcode::B && len == 2 && inst.condition != C::AL);
+ let mut pc = addr.wrapping_add(pc_bias).wrapping_sub(if counted { 2 } else { 0 });
+ // Thumb BLX switches to A32 and branches from the word-aligned PC.
+ if mode == Mode::T32 && inst.opcode == Opcode::BLX {
+ pc &= !3;
+ }
+ let t = pc.wrapping_add((i64::from(off) * 2) as u64);
target = Some(t);
ops.push(Op::Addr(t));
}
@@ -900,6 +908,7 @@ fn decode_a32(mode: Mode, bytes: &[u8], addr: u64, fmt: bool) -> Option<Insn> {
(Some(t), false) => Flow::Jump { target: t },
_ => Flow::IndirectJump { slot: None },
},
+ Opcode::CBZ | Opcode::CBNZ => target.map_or(Flow::Next, |t| Flow::Cond { target: t }),
Opcode::BL | Opcode::BLX => match target {
Some(t) => Flow::Call { target: t },
None => Flow::IndirectCall { slot: None },
@@ -934,7 +943,7 @@ fn decode_a32(mode: Mode, bytes: &[u8], addr: u64, fmt: bool) -> Option<Insn> {
Opcode::POP => Kind::Pop,
Opcode::LDR => Kind::Load,
Opcode::STR => Kind::Store,
- Opcode::B | Opcode::BX => Kind::Branch,
+ Opcode::B | Opcode::BX | Opcode::CBZ | Opcode::CBNZ => Kind::Branch,
Opcode::BL | Opcode::BLX => Kind::Call,
_ => Kind::Other,
};
@@ -1158,6 +1167,68 @@ mod tests {
assert_eq!(c.value("x0"), Some(0x2010));
}
+ #[test]
+ fn a_thumb_bl_call_targets_its_destination() {
+ for fmt in [false, true] {
+ let i = decode(Mode::T32, &[0x00, 0xf0, 0x05, 0xf8], 0x1002, fmt).unwrap();
+ assert_eq!(i.flow, Flow::Call { target: 0x1010 });
+ assert_eq!(i.ops, vec![Op::Addr(0x1010)]);
+ let i = decode(Mode::T32, &[0xff, 0xf7, 0xf7, 0xff], 0x1010, fmt).unwrap();
+ assert_eq!(i.flow, Flow::Call { target: 0x1002 });
+ }
+ }
+
+ #[test]
+ fn arm_and_thumb_branches_target_what_llvm_objdump_reports() {
+ // Bytes and targets from clang-assembled code disassembled by llvm-objdump.
+ let cases: &[(Mode, &str, &[u8], u64, u64)] = &[
+ (Mode::T32, "b", &[0x14, 0xe0], 0x1000, 0x102c),
+ (Mode::T32, "beq", &[0x13, 0xd0], 0x1002, 0x102c),
+ (Mode::T32, "b.w", &[0x00, 0xf0, 0x12, 0xb8], 0x1004, 0x102c),
+ (Mode::T32, "beq.w", &[0x00, 0xf0, 0x10, 0x80], 0x1008, 0x102c),
+ (Mode::T32, "bl", &[0x00, 0xf0, 0x0e, 0xf8], 0x100c, 0x102c),
+ (Mode::T32, "cbz", &[0x60, 0xb1], 0x1010, 0x102c),
+ (Mode::T32, "cbnz", &[0x59, 0xb9], 0x1012, 0x102c),
+ (Mode::T32, "blx", &[0xff, 0xf7, 0xfe, 0xef], 0x1016, 0x1014),
+ (Mode::T32, "bne.w", &[0x40, 0xf0, 0x06, 0x80], 0x1024, 0x1034),
+ (Mode::T32, "bne", &[0x03, 0xd1], 0x102a, 0x1034),
+ (Mode::A32, "b", &[0x04, 0x00, 0x00, 0xea], 0x1000, 0x1018),
+ (Mode::A32, "beq", &[0x03, 0x00, 0x00, 0x0a], 0x1004, 0x1018),
+ (Mode::A32, "bl", &[0xfe, 0xff, 0xff, 0xeb], 0x1008, 0x1008),
+ (Mode::A32, "blx", &[0xfe, 0xff, 0xff, 0xfa], 0x100c, 0x100c),
+ (Mode::A32, "b back", &[0xfa, 0xff, 0xff, 0xea], 0x1010, 0x1000),
+ ];
+ for (mode, name, bytes, at, want) in cases {
+ let i = decode(*mode, bytes, *at, false).unwrap();
+ let got = match i.flow {
+ Flow::Call { target } | Flow::Jump { target } | Flow::Cond { target } => target,
+ other => panic!("{mode:?} {name}: {other:?}"),
+ };
+ assert_eq!(got, *want, "{mode:?} {name}");
+ }
+ let cbz = decode(Mode::T32, &[0x60, 0xb1], 0x1010, false).unwrap();
+ assert_eq!(cbz.reads, vec!["r0".to_string()]);
+ assert!(cbz.writes.is_empty());
+ }
+
+ #[test]
+ fn a_thumb_bl_discovers_the_callee_instead_of_padding() {
+ use crate::{Image, Limits, NoControl, Program};
+ use craft_anything_formats::{Arch, Binary};
+ use std::sync::Arc;
+
+ let bytes = [0x10, 0xb5, 0x00, 0xf0, 0x05, 0xf8, 0x10, 0xbd, 0, 0, 0, 0, 0, 0, 0, 0, 0x70, 0x47];
+ let mut bin = Binary::raw(Arch::Arm, 0x1000, bytes.len() as u64, 0);
+ bin.entry = Some(0x1001);
+ let img = Image::from_binary(Arc::from(bytes), bin);
+ assert_eq!(img.mode, Some(Mode::T32));
+ let p = Program::analyze(&img, &Limits::default(), &NoControl).unwrap();
+ let caller = p.functions.get(&0x1000).unwrap();
+ assert_eq!(caller.calls.first().unwrap().target, Some(0x1010));
+ assert!(p.functions.contains_key(&0x1010));
+ assert!(!p.functions.contains_key(&0x100c));
+ }
+
#[test]
fn a32_doubleword_rt15_is_invalid() {
assert!(decode(Mode::A32, &[0xf3, 0xfe, 0xc7, 0xc0], 0, true).is_none());