Skip to content

Commit

Permalink
Merge pull request #375 from kornelski/arm-disasm
Browse files Browse the repository at this point in the history
Fixes for disasm on aarch64
  • Loading branch information
pacak authored Feb 12, 2025
2 parents 272fe1d + 0a68a8f commit 31e8fed
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/disasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ fn pick_item<'a>(
let mut items = BTreeMap::new();

for file in files {
let mut addresses: Vec<_> = file
.symbols()
.filter(|s| s.is_definition() && s.kind() == SymbolKind::Text)
.map(|s| s.address() as usize)
.collect();
addresses.sort_unstable();

for (index, symbol) in file
.symbols()
.filter(|s| s.is_definition() && s.kind() == SymbolKind::Text)
Expand All @@ -101,11 +108,21 @@ fn pick_item<'a>(
continue;
};

let len = symbol.size() as usize; // sorry 32bit platforms, you are not real
let addr = symbol.address() as usize;
let mut len = symbol.size() as usize; // sorry 32bit platforms, you are not real
if len == 0 {
continue;
// Most symbols do not have a size.
// Guess size from the address of the next symbol after it.
let (Ok(idx) | Err(idx)) = addresses.binary_search(&addr);
let next_address = match addresses[idx..].iter().copied().find(|&a| a > addr) {
Some(addr) => addr,
None => {
let section = file.section_by_index(section_index)?;
(section.address() + section.size()) as usize
}
};
len = next_address - addr;
}
let addr = symbol.address() as usize;
let item = Item {
name,
hashed,
Expand Down Expand Up @@ -196,13 +213,16 @@ fn dump_slices(
if reloc_map.is_empty() {
safeprintln!("There is no relocation table");
} else {
safeprintln!("{:?}", reloc_map);
safeprintln!("reloc_map {:#?}", reloc_map);
}
}

let insns = cs.disasm_all(code, addr as u64)?;
if insns.is_empty() && fmt.verbosity > 0 {
safeprintln!("No instructions - empty code block?");
if insns.is_empty() {
if fmt.verbosity > 0 {
safeprintln!("No instructions - empty code block?");
}
return Ok(());
}

let max_width = insns.iter().map(|i| i.len()).max().unwrap_or(1);
Expand Down Expand Up @@ -360,7 +380,10 @@ fn make_capstone(
};

let mut capstone = match file.architecture() {
Architecture::Aarch64 => Capstone::new().arm64().build()?,
Architecture::Aarch64 => Capstone::new()
.arm64()
.mode(arch::arm64::ArchMode::Arm)
.build()?,
Architecture::Arm => {
let mode = if is_thumb {
arch::arm::ArchMode::Thumb
Expand Down

0 comments on commit 31e8fed

Please sign in to comment.