Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions objdiff-core/src/arch/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ impl Arch for ArchArm {
elf::R_ARM_PC24 => Some("R_ARM_PC24"),
elf::R_ARM_XPC25 => Some("R_ARM_XPC25"),
elf::R_ARM_CALL => Some("R_ARM_CALL"),
elf::R_ARM_JUMP24 => Some("R_ARM_JUMP24"),
elf::R_ARM_THM_PC11 => Some("R_ARM_THM_PC11"),
elf::R_ARM_THM_PC9 => Some("R_ARM_THM_PC9"),
elf::R_ARM_V4BX => Some("R_ARM_V4BX"),
Expand All @@ -435,6 +436,21 @@ impl Arch for ArchArm {
}
}

fn reloc_branch_pc_bias(&self, flags: RelocationFlags) -> i64 {
match flags {
RelocationFlags::Elf(
elf::R_ARM_THM_PC22
| elf::R_ARM_THM_XPC22
| elf::R_ARM_THM_PC11
| elf::R_ARM_THM_PC9,
) => 4,
RelocationFlags::Elf(
elf::R_ARM_PC24 | elf::R_ARM_XPC25 | elf::R_ARM_CALL | elf::R_ARM_JUMP24,
) => 8,
_ => 0,
}
}

fn data_reloc_size(&self, flags: RelocationFlags) -> usize {
match flags {
RelocationFlags::Elf(r_type) => match r_type {
Expand All @@ -448,6 +464,7 @@ impl Arch for ArchArm {
elf::R_ARM_PC24 => 4,
elf::R_ARM_XPC25 => 4,
elf::R_ARM_CALL => 4,
elf::R_ARM_JUMP24 => 4,
elf::R_ARM_THM_PC11 => 2,
elf::R_ARM_THM_PC9 => 2,
_ => 1,
Expand Down Expand Up @@ -610,6 +627,7 @@ impl unarm::FormatIns for ArgsFormatter<'_> {
| RelocationFlags::Elf(elf::R_ARM_PC24)
| RelocationFlags::Elf(elf::R_ARM_XPC25)
| RelocationFlags::Elf(elf::R_ARM_CALL)
| RelocationFlags::Elf(elf::R_ARM_JUMP24)
| RelocationFlags::Elf(elf::R_ARM_THM_PC11)
| RelocationFlags::Elf(elf::R_ARM_THM_PC9) => {
return self.write(InstructionPart::reloc());
Expand Down Expand Up @@ -674,3 +692,141 @@ impl unarm::FormatIns for ArgsFormatter<'_> {
Ok(())
}
}

#[cfg(test)]
mod test {
use alloc::{boxed::Box, vec};

use super::*;
use crate::obj::{Object, ResolvedSymbol, SectionData};

fn make_arm_obj(
mode: unarm::ParseMode,
code: Vec<u8>,
caller_size: u64,
callee_size: u64,
label_addr: u64,
relocations: Vec<Relocation>,
) -> Object {
let mut disasm_modes = BTreeMap::new();
disasm_modes.insert(0, vec![DisasmMode { address: 0, mapping: mode }]);
let arch = Box::new(ArchArm {
disasm_modes,
detected_version: Some(unarm::Version::V5Te),
endianness: object::Endianness::Little,
});
let section = Section {
name: ".text".into(),
address: 0,
size: code.len() as u64,
kind: SectionKind::Code,
data: SectionData(code),
relocations,
..Default::default()
};
let symbols = vec![
Symbol {
name: "caller".into(),
address: 0,
size: caller_size,
kind: SymbolKind::Function,
section: Some(0),
..Default::default()
},
Symbol {
name: "callee".into(),
address: caller_size,
size: callee_size,
kind: SymbolKind::Function,
section: Some(0),
..Default::default()
},
Symbol {
name: "label".into(),
address: label_addr,
size: 0,
kind: SymbolKind::Unknown,
section: Some(0),
..Default::default()
},
];
Object { arch, sections: vec![section], symbols, ..Default::default() }
}

#[test]
fn test_arm_branch_pc_bias_adjacent_callee() {
// 0x00: bl #0 (0xebfffffe) -> R_ARM_CALL to callee (0x10), addend -8
// 0x04: b #0 (0xeafffffe) -> R_ARM_JUMP24 to label (0x0c), addend -8
// 0x08: nop (0xe1a00000)
// 0x0c: bx lr (0xe12fff1e) <- label
// 0x10: bx lr (0xe12fff1e) <- callee (immediately after caller)
let mut code = Vec::new();
for insn in [0xebff_fffeu32, 0xeaff_fffe, 0xe1a0_0000, 0xe12f_ff1e, 0xe12f_ff1e] {
code.extend_from_slice(&insn.to_le_bytes());
}
let relocs = vec![
Relocation {
flags: RelocationFlags::Elf(elf::R_ARM_CALL),
address: 0x0,
target_symbol: 1, // callee at 0x10
addend: -8,
},
Relocation {
flags: RelocationFlags::Elf(elf::R_ARM_JUMP24),
address: 0x4,
target_symbol: 2, // label at 0x0c
addend: -8,
},
];
let obj = make_arm_obj(unarm::ParseMode::Arm, code, 0x10, 0x4, 0x0c, relocs);
let resolved = ResolvedSymbol {
obj: &obj,
symbol_index: 0,
symbol: &obj.symbols[0],
section_index: 0,
section: &obj.sections[0],
data: &obj.sections[0].data[..0x10],
};
let insns = obj.arch.scan_instructions(resolved, &DiffObjConfig::default()).unwrap();
assert_eq!(insns[0].branch_dest, None);
assert_eq!(insns[1].branch_dest, Some(0x0c));
}

#[test]
fn test_thumb_branch_pc_bias_adjacent_callee() {
// 0x00: bl #0 (0xf7ff 0xfffe) -> R_ARM_THM_PC22 to callee (0x08), addend -4
// 0x04: b #0 (0xe7fe) -> R_ARM_THM_PC11 to label (0x06), addend -4
// 0x06: bx lr (0x4770) <- label
// 0x08: bx lr (0x4770) <- callee (immediately after caller)
let mut code = Vec::new();
for hw in [0xf7ffu16, 0xfffe, 0xe7fe, 0x4770, 0x4770] {
code.extend_from_slice(&hw.to_le_bytes());
}
let relocs = vec![
Relocation {
flags: RelocationFlags::Elf(elf::R_ARM_THM_PC22),
address: 0x0,
target_symbol: 1, // callee at 0x08
addend: -4,
},
Relocation {
flags: RelocationFlags::Elf(elf::R_ARM_THM_PC11),
address: 0x4,
target_symbol: 2, // label at 0x06
addend: -4,
},
];
let obj = make_arm_obj(unarm::ParseMode::Thumb, code, 0x8, 0x2, 0x6, relocs);
let resolved = ResolvedSymbol {
obj: &obj,
symbol_index: 0,
symbol: &obj.symbols[0],
section_index: 0,
section: &obj.sections[0],
data: &obj.sections[0].data[..0x8],
};
let insns = obj.arch.scan_instructions(resolved, &DiffObjConfig::default()).unwrap();
assert_eq!(insns[0].branch_dest, None);
assert_eq!(insns[1].branch_dest, Some(0x6));
}
}
12 changes: 11 additions & 1 deletion objdiff-core/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,11 @@ impl dyn Arch {
if target.section != Some(resolved.section_index) {
continue;
}
let Some(target_address) = target.address.checked_add_signed(reloc.addend) else {
let Some(target_address) = reloc
.addend
.checked_add(self.reloc_branch_pc_bias(reloc.flags))
.and_then(|offset| target.address.checked_add_signed(offset))
else {
continue;
};
// If the target address is within the function range, set it as a branch destination
Expand Down Expand Up @@ -456,6 +460,12 @@ pub trait Arch: Any + Debug + Send + Sync {

fn data_reloc_size(&self, flags: RelocationFlags) -> usize;

/// Bias to add to `reloc.addend` to cancel out a PC-relative branch's pipeline PC offset
/// and recover the offset into the target symbol. On ARM, `bl`/`b` read PC as the
/// instruction address + 4 (Thumb) or + 8 (ARM), so the relocation addend includes -4/-8
/// and this method returns +4/+8.
fn reloc_branch_pc_bias(&self, _flags: RelocationFlags) -> i64 { 0 }

fn symbol_address(&self, address: u64, _kind: SymbolKind) -> u64 { address }

fn extra_symbol_flags(&self, _symbol: &object::Symbol) -> SymbolFlagSet {
Expand Down
Loading
Loading