This patch introduces basic aarch64 code generation by using `cranelift-codegen`'s backend. This commit *does not*: * Change the semantics of the code generation * Adds support for other Wasm instructions The most notable change in this patch is how addressing modes are handled at the MacroAssembler layer: instead of having a canonical address representation, this patch introduces the addressing mode as an associated type in the MacroAssembler trait. This approach has the advantage that gives each ISA enough flexiblity to describe the addressing modes and their constraints in isolation without having to worry on how a particular addressing mode is going to affect other ISAs. In the case of Aarch64 this becomes useful to describe indexed addressing modes (particularly from the stack pointer). This patch uses the concept of a shadow stack pointer (x28) as a workaround to Aarch64's stack pointer 16-byte alignment. This constraint is enforced by: * Introducing specialized addressing modes when using the real stack pointer; this enables auditing when the real stack pointer is used. As of this change, the real stack pointer is only used in the function's prologue and epilogue. * Asserting that the real stack pointer is not used as a base for addressing modes. * Ensuring that at any point during the code generation process where the stack pointer changes (e.g. when stack space is allocated / deallocated) the value of the real stack pointer is copied into the shadow stack pointer.
72 lines
1.9 KiB
Rust
72 lines
1.9 KiB
Rust
//! Disassembly utilities.
|
|
|
|
use anyhow::{bail, Result};
|
|
use capstone::prelude::*;
|
|
use std::fmt::Write;
|
|
use target_lexicon::Architecture;
|
|
use winch_codegen::TargetIsa;
|
|
|
|
/// Disassemble and print a machine code buffer.
|
|
pub fn disasm(bytes: &[u8], isa: &dyn TargetIsa) -> Result<Vec<String>> {
|
|
let dis = disassembler_for(isa)?;
|
|
let insts = dis.disasm_all(bytes, 0x0).unwrap();
|
|
|
|
let disassembled_lines = insts
|
|
.iter()
|
|
.map(|i| {
|
|
let mut line = String::new();
|
|
|
|
write!(&mut line, "{:4x}:\t ", i.address()).unwrap();
|
|
|
|
let mut bytes_str = String::new();
|
|
let mut len = 0;
|
|
for b in i.bytes() {
|
|
write!(&mut bytes_str, "{:02x}", b).unwrap();
|
|
len += 1;
|
|
}
|
|
write!(&mut line, "{:21}\t", bytes_str).unwrap();
|
|
if len > 8 {
|
|
write!(&mut line, "\n\t\t\t\t").unwrap();
|
|
}
|
|
|
|
if let Some(s) = i.mnemonic() {
|
|
write!(&mut line, "{}\t", s).unwrap();
|
|
}
|
|
|
|
if let Some(s) = i.op_str() {
|
|
write!(&mut line, "{}", s).unwrap();
|
|
}
|
|
|
|
line
|
|
})
|
|
.collect();
|
|
|
|
Ok(disassembled_lines)
|
|
}
|
|
|
|
fn disassembler_for(isa: &dyn TargetIsa) -> Result<Capstone> {
|
|
let disasm = match isa.triple().architecture {
|
|
Architecture::X86_64 => Capstone::new()
|
|
.x86()
|
|
.mode(arch::x86::ArchMode::Mode64)
|
|
.build()
|
|
.map_err(|e| anyhow::format_err!("{}", e))?,
|
|
|
|
Architecture::Aarch64 { .. } => {
|
|
let mut cs = Capstone::new()
|
|
.arm64()
|
|
.mode(arch::arm64::ArchMode::Arm)
|
|
.build()
|
|
.map_err(|e| anyhow::format_err!("{}", e))?;
|
|
|
|
cs.set_skipdata(true)
|
|
.map_err(|e| anyhow::format_err!("{}", e))?;
|
|
cs
|
|
}
|
|
|
|
_ => bail!("Unsupported ISA"),
|
|
};
|
|
|
|
Ok(disasm)
|
|
}
|