From fc54e936817dc4f9339e6a34422b69d11e46ae25 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Mon, 3 Jun 2024 19:37:48 -0600 Subject: [PATCH] API updates for ARM backend --- objdiff-core/src/arch/arm.rs | 37 +++++++++++++++++------------------ objdiff-core/src/arch/mips.rs | 1 + objdiff-core/src/arch/mod.rs | 1 + objdiff-core/src/arch/ppc.rs | 1 + objdiff-core/src/arch/x86.rs | 1 + objdiff-core/src/diff/code.rs | 9 ++++++++- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/objdiff-core/src/arch/arm.rs b/objdiff-core/src/arch/arm.rs index 406313e..e4049a8 100644 --- a/objdiff-core/src/arch/arm.rs +++ b/objdiff-core/src/arch/arm.rs @@ -1,6 +1,9 @@ -use std::{borrow::Cow, collections::HashMap}; +use std::{ + borrow::Cow, + collections::{BTreeMap, HashMap}, +}; -use anyhow::{anyhow, bail, Result}; +use anyhow::{bail, Result}; use object::{ elf, File, Object, ObjectSection, ObjectSymbol, Relocation, RelocationFlags, SectionIndex, SectionKind, Symbol, @@ -14,7 +17,7 @@ use unarm::{ use crate::{ arch::{ObjArch, ProcessCodeResult}, diff::DiffObjConfig, - obj::{ObjInfo, ObjIns, ObjInsArg, ObjInsArgValue, ObjSection, SymbolRef}, + obj::{ObjIns, ObjInsArg, ObjInsArgValue, ObjReloc, ObjSection}, }; pub struct ObjArchArm { @@ -50,28 +53,25 @@ impl ObjArchArm { impl ObjArch for ObjArchArm { fn process_code( &self, - obj: &ObjInfo, - symbol_ref: SymbolRef, + address: u64, + code: &[u8], + section_index: usize, + relocations: &[ObjReloc], + line_info: &BTreeMap, config: &DiffObjConfig, ) -> Result { - let (section, symbol) = obj.section_symbol(symbol_ref); - let section = section.ok_or_else(|| anyhow!("Code symbol section not found"))?; - let code = §ion.data - [symbol.section_address as usize..(symbol.section_address + symbol.size) as usize]; - - let start_addr = symbol.address as u32; - let end_addr = start_addr + symbol.size as u32; + let start_addr = address as u32; + let end_addr = start_addr + code.len() as u32; // Mapping symbols decide what kind of data comes after it. $a for ARM code, $t for Thumb code and $d for data. - let fallback_mappings = - [DisasmMode { address: symbol.address as u32, mapping: ParseMode::Arm }]; + let fallback_mappings = [DisasmMode { address: start_addr, mapping: ParseMode::Arm }]; let mapping_symbols = self .disasm_modes - .get(&SectionIndex(section.orig_index)) + .get(&SectionIndex(section_index)) .map(|x| x.as_slice()) .unwrap_or(&fallback_mappings); let first_mapping_idx = - match mapping_symbols.binary_search_by_key(&(symbol.address as u32), |x| x.address) { + match mapping_symbols.binary_search_by_key(&start_addr, |x| x.address) { Ok(idx) => idx, Err(idx) => idx - 1, }; @@ -96,10 +96,9 @@ impl ObjArch for ObjArchArm { } } - let line = section.line_info.range(..=address as u64).last().map(|(_, &b)| b); + let line = line_info.range(..=address as u64).last().map(|(_, &b)| b); - let reloc = - section.relocations.iter().find(|r| (r.address as u32 & !1) == address).cloned(); + let reloc = relocations.iter().find(|r| (r.address as u32 & !1) == address).cloned(); let mut reloc_arg = None; if let Some(reloc) = &reloc { diff --git a/objdiff-core/src/arch/mips.rs b/objdiff-core/src/arch/mips.rs index dcdc391..f9e51a2 100644 --- a/objdiff-core/src/arch/mips.rs +++ b/objdiff-core/src/arch/mips.rs @@ -59,6 +59,7 @@ impl ObjArch for ObjArchMips { &self, address: u64, code: &[u8], + _section_index: usize, relocations: &[ObjReloc], line_info: &BTreeMap, config: &DiffObjConfig, diff --git a/objdiff-core/src/arch/mod.rs b/objdiff-core/src/arch/mod.rs index 3dfe1d5..2cf02ee 100644 --- a/objdiff-core/src/arch/mod.rs +++ b/objdiff-core/src/arch/mod.rs @@ -22,6 +22,7 @@ pub trait ObjArch: Send + Sync { &self, address: u64, code: &[u8], + section_index: usize, relocations: &[ObjReloc], line_info: &BTreeMap, config: &DiffObjConfig, diff --git a/objdiff-core/src/arch/ppc.rs b/objdiff-core/src/arch/ppc.rs index 377243b..cba31d3 100644 --- a/objdiff-core/src/arch/ppc.rs +++ b/objdiff-core/src/arch/ppc.rs @@ -33,6 +33,7 @@ impl ObjArch for ObjArchPpc { &self, address: u64, code: &[u8], + _section_index: usize, relocations: &[ObjReloc], line_info: &BTreeMap, config: &DiffObjConfig, diff --git a/objdiff-core/src/arch/x86.rs b/objdiff-core/src/arch/x86.rs index 44ff73a..f50c544 100644 --- a/objdiff-core/src/arch/x86.rs +++ b/objdiff-core/src/arch/x86.rs @@ -30,6 +30,7 @@ impl ObjArch for ObjArchX86 { &self, address: u64, code: &[u8], + _section_index: usize, relocations: &[ObjReloc], line_info: &BTreeMap, config: &DiffObjConfig, diff --git a/objdiff-core/src/diff/code.rs b/objdiff-core/src/diff/code.rs index 19343f6..8297cc8 100644 --- a/objdiff-core/src/diff/code.rs +++ b/objdiff-core/src/diff/code.rs @@ -25,7 +25,14 @@ pub fn process_code_symbol( let section = section.ok_or_else(|| anyhow!("Code symbol section not found"))?; let code = §ion.data [symbol.section_address as usize..(symbol.section_address + symbol.size) as usize]; - obj.arch.process_code(symbol.address, code, §ion.relocations, §ion.line_info, config) + obj.arch.process_code( + symbol.address, + code, + section.orig_index, + §ion.relocations, + §ion.line_info, + config, + ) } pub fn no_diff_code(out: &ProcessCodeResult, symbol_ref: SymbolRef) -> Result {