* Made Capstone an optional dependency (fixes #382). * Introduced feature 'disas' for disassembly (related to #382). * Made 'disas' a default feature in cretonne-tools. * Fixed errors in src/compile.rs introduced by get_disassembler changes. - Moves `use` statements before the function declaration. - Returns an error if the disassembler cannot be found created.
This commit is contained in:
committed by
Dan Gohman
parent
ff5660624d
commit
e5014e0fff
@@ -29,12 +29,13 @@ docopt = "1"
|
|||||||
serde = "1.0.8"
|
serde = "1.0.8"
|
||||||
serde_derive = "1.0.8"
|
serde_derive = "1.0.8"
|
||||||
term = "0.5.1"
|
term = "0.5.1"
|
||||||
capstone = "0.4"
|
capstone = { version = "0.4", optional = true }
|
||||||
wabt = { version = "0.3", optional = true }
|
wabt = { version = "0.3", optional = true }
|
||||||
target-lexicon = "0.0.2"
|
target-lexicon = "0.0.2"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["wasm"]
|
default = ["disas", "wasm"]
|
||||||
|
disas = ["capstone"]
|
||||||
wasm = ["wabt", "cretonne-wasm"]
|
wasm = ["wabt", "cretonne-wasm"]
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
//! CLI tool to read Cretonne IR files and compile them into native code.
|
//! CLI tool to read Cretonne IR files and compile them into native code.
|
||||||
|
|
||||||
use capstone::prelude::*;
|
|
||||||
use cretonne_codegen::isa::TargetIsa;
|
use cretonne_codegen::isa::TargetIsa;
|
||||||
use cretonne_codegen::print_errors::pretty_error;
|
use cretonne_codegen::print_errors::pretty_error;
|
||||||
use cretonne_codegen::settings::FlagsOrIsa;
|
use cretonne_codegen::settings::FlagsOrIsa;
|
||||||
@@ -9,7 +8,6 @@ use cretonne_codegen::{binemit, ir};
|
|||||||
use cretonne_reader::parse_test;
|
use cretonne_reader::parse_test;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use target_lexicon::Architecture;
|
|
||||||
use utils::{parse_sets_and_triple, read_to_string};
|
use utils::{parse_sets_and_triple, read_to_string};
|
||||||
|
|
||||||
struct PrintRelocs {
|
struct PrintRelocs {
|
||||||
@@ -121,8 +119,53 @@ fn handle_module(
|
|||||||
}
|
}
|
||||||
print!("{}", byte);
|
print!("{}", byte);
|
||||||
}
|
}
|
||||||
println!();
|
|
||||||
|
|
||||||
|
println!();
|
||||||
|
print_disassembly(isa, &mem)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg_if! {
|
||||||
|
if #[cfg(feature = "disas")] {
|
||||||
|
use capstone::prelude::*;
|
||||||
|
use target_lexicon::Architecture;
|
||||||
|
|
||||||
|
fn get_disassembler(isa: &TargetIsa) -> Result<Capstone, String> {
|
||||||
|
let cs = match isa.triple().architecture {
|
||||||
|
Architecture::Riscv32 | Architecture::Riscv64 => {
|
||||||
|
return Err(String::from("No disassembler for RiscV"))
|
||||||
|
}
|
||||||
|
Architecture::I386 | Architecture::I586 | Architecture::I686 => Capstone::new()
|
||||||
|
.x86()
|
||||||
|
.mode(arch::x86::ArchMode::Mode32)
|
||||||
|
.build(),
|
||||||
|
Architecture::X86_64 => Capstone::new()
|
||||||
|
.x86()
|
||||||
|
.mode(arch::x86::ArchMode::Mode64)
|
||||||
|
.build(),
|
||||||
|
Architecture::Arm
|
||||||
|
| Architecture::Armv4t
|
||||||
|
| Architecture::Armv5te
|
||||||
|
| Architecture::Armv7
|
||||||
|
| Architecture::Armv7s => Capstone::new().arm().mode(arch::arm::ArchMode::Arm).build(),
|
||||||
|
Architecture::Thumbv6m | Architecture::Thumbv7em | Architecture::Thumbv7m => Capstone::new(
|
||||||
|
).arm()
|
||||||
|
.mode(arch::arm::ArchMode::Thumb)
|
||||||
|
.build(),
|
||||||
|
Architecture::Aarch64 => Capstone::new()
|
||||||
|
.arm64()
|
||||||
|
.mode(arch::arm64::ArchMode::Arm)
|
||||||
|
.build(),
|
||||||
|
_ => return Err(String::from("Unknown ISA")),
|
||||||
|
};
|
||||||
|
|
||||||
|
cs.map_err(|err| err.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_disassembly(isa: &TargetIsa, mem: &[u8]) -> Result<(), String> {
|
||||||
let mut cs = get_disassembler(isa)?;
|
let mut cs = get_disassembler(isa)?;
|
||||||
|
|
||||||
println!("\nDisassembly:");
|
println!("\nDisassembly:");
|
||||||
@@ -130,40 +173,12 @@ fn handle_module(
|
|||||||
for i in insns.iter() {
|
for i in insns.iter() {
|
||||||
println!("{}", i);
|
println!("{}", i);
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fn print_disassembly(_: &TargetIsa, _: &[u8]) -> Result<(), String> {
|
||||||
|
println!("\nNo disassembly available.");
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_disassembler(isa: &TargetIsa) -> Result<Capstone, String> {
|
|
||||||
let cs = match isa.triple().architecture {
|
|
||||||
Architecture::Riscv32 | Architecture::Riscv64 => {
|
|
||||||
return Err(String::from("No disassembler for RiscV"))
|
|
||||||
}
|
|
||||||
Architecture::I386 | Architecture::I586 | Architecture::I686 => Capstone::new()
|
|
||||||
.x86()
|
|
||||||
.mode(arch::x86::ArchMode::Mode32)
|
|
||||||
.build(),
|
|
||||||
Architecture::X86_64 => Capstone::new()
|
|
||||||
.x86()
|
|
||||||
.mode(arch::x86::ArchMode::Mode64)
|
|
||||||
.build(),
|
|
||||||
Architecture::Arm
|
|
||||||
| Architecture::Armv4t
|
|
||||||
| Architecture::Armv5te
|
|
||||||
| Architecture::Armv7
|
|
||||||
| Architecture::Armv7s => Capstone::new().arm().mode(arch::arm::ArchMode::Arm).build(),
|
|
||||||
Architecture::Thumbv6m | Architecture::Thumbv7em | Architecture::Thumbv7m => Capstone::new(
|
|
||||||
).arm()
|
|
||||||
.mode(arch::arm::ArchMode::Thumb)
|
|
||||||
.build(),
|
|
||||||
Architecture::Aarch64 => Capstone::new()
|
|
||||||
.arm64()
|
|
||||||
.mode(arch::arm64::ArchMode::Arm)
|
|
||||||
.build(),
|
|
||||||
_ => return Err(String::from("Unknown ISA")),
|
|
||||||
};
|
|
||||||
|
|
||||||
cs.map_err(|err| err.to_string())
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ extern crate docopt;
|
|||||||
extern crate filecheck;
|
extern crate filecheck;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
#[cfg(feature = "disas")]
|
||||||
extern crate capstone;
|
extern crate capstone;
|
||||||
extern crate term;
|
extern crate term;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user