Skip to content

Commit

Permalink
remove the Architecture Handle
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed Apr 24, 2024
1 parent 98a5094 commit 88d2765
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 365 deletions.
77 changes: 26 additions & 51 deletions arch/riscv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use std::fmt;
use std::hash::Hash;
use std::marker::PhantomData;

use binaryninja::architecture::ArchitectureExt;
use binaryninja::relocation::{Relocation, RelocationHandlerExt};
use binaryninja::{
add_optional_plugin_dependency, architecture,
architecture::{
llvm_assemble, Architecture, ArchitectureExt, CoreArchitecture, CustomArchitectureHandle,
ImplicitRegisterExtend, InstructionInfo, LlvmServicesCodeModel, LlvmServicesDialect,
LlvmServicesRelocMode, Register as Reg, RegisterInfo, UnusedFlag, UnusedRegisterStack,
UnusedRegisterStackInfo,
llvm_assemble, Architecture, CoreArchitecture, ImplicitRegisterExtend, InstructionInfo,
LlvmServicesCodeModel, LlvmServicesDialect, LlvmServicesRelocMode, Register as Reg,
RegisterInfo, UnusedFlag, UnusedRegisterStack, UnusedRegisterStackInfo,
},
binaryview::{BinaryView, BinaryViewExt},
callingconvention::{register_calling_convention, CallingConventionBase, ConventionBuilder},
Expand Down Expand Up @@ -624,14 +624,11 @@ impl<D: RiscVDisassembler> architecture::Intrinsic for RiscVIntrinsic<D> {
}

struct RiscVArch<D: 'static + RiscVDisassembler + Send + Sync> {
handle: CoreArchitecture,
custom_handle: CustomArchitectureHandle<RiscVArch<D>>,
handle: &'static CoreArchitecture,
_dis: PhantomData<D>,
}

impl<D: 'static + RiscVDisassembler + Send + Sync> architecture::Architecture for RiscVArch<D> {
type Handle = CustomArchitectureHandle<Self>;

type RegisterInfo = Register<D>;
type Register = Register<D>;
type RegisterStackInfo = UnusedRegisterStackInfo<Self::Register>;
Expand Down Expand Up @@ -674,7 +671,7 @@ impl<D: 'static + RiscVDisassembler + Send + Sync> architecture::Architecture fo
self.max_instr_len()
}

fn associated_arch_by_addr(&self, _addr: &mut u64) -> CoreArchitecture {
fn associated_arch_by_addr(&self, _addr: &mut u64) -> &'static CoreArchitecture {
self.handle
}

Expand Down Expand Up @@ -977,29 +974,20 @@ impl<D: 'static + RiscVDisassembler + Send + Sync> architecture::Architecture fo
Text,
));
} else {
res.push(InstructionTextToken::new(
",",
OperandSeparator,
));
res.push(InstructionTextToken::new(",", OperandSeparator));
res.push(InstructionTextToken::new(" ", Text));
}

match *oper {
Operand::R(r) => {
let reg = self::Register::from(r);

res.push(InstructionTextToken::new(
&reg.name(),
Register,
));
res.push(InstructionTextToken::new(&reg.name(), Register));
}
Operand::F(r) => {
let reg = self::Register::from(r);

res.push(InstructionTextToken::new(
&reg.name(),
Register,
));
res.push(InstructionTextToken::new(&reg.name(), Register));
}
Operand::I(i) => {
match op {
Expand Down Expand Up @@ -1032,10 +1020,7 @@ impl<D: 'static + RiscVDisassembler + Send + Sync> architecture::Architecture fo
Operand::M(i, b) => {
let reg = self::Register::from(b);

res.push(InstructionTextToken::new(
"",
BeginMemoryOperand,
));
res.push(InstructionTextToken::new("", BeginMemoryOperand));
res.push(InstructionTextToken::new(
&if i < 0 {
format!("-0x{:x}", -i)
Expand All @@ -1046,15 +1031,9 @@ impl<D: 'static + RiscVDisassembler + Send + Sync> architecture::Architecture fo
));

res.push(InstructionTextToken::new("(", Brace));
res.push(InstructionTextToken::new(
&reg.name(),
Register,
));
res.push(InstructionTextToken::new(&reg.name(), Register));
res.push(InstructionTextToken::new(")", Brace));
res.push(InstructionTextToken::new(
"",
EndMemoryOperand,
));
res.push(InstructionTextToken::new("", EndMemoryOperand));
}
Operand::RM(r) => {
res.push(InstructionTextToken::new(r.name(), Register));
Expand Down Expand Up @@ -2140,8 +2119,8 @@ impl<D: 'static + RiscVDisassembler + Send + Sync> architecture::Architecture fo
true
}

fn handle(&self) -> CustomArchitectureHandle<Self> {
self.custom_handle
fn core(&self) -> &'static CoreArchitecture {
self.handle
}
}

Expand Down Expand Up @@ -2869,22 +2848,18 @@ pub extern "C" fn CorePluginInit() -> bool {
binaryninja::logger::init(log::LevelFilter::Trace).expect("Failed to set up logging");

use riscv_dis::{RiscVIMACDisassembler, Rv32GRegs, Rv64GRegs};
let arch32 =
architecture::register_architecture("rv32gc", |custom_handle, core_arch| RiscVArch::<
RiscVIMACDisassembler<Rv32GRegs>,
> {
handle: core_arch,
custom_handle,
_dis: PhantomData,
});
let arch64 =
architecture::register_architecture("rv64gc", |custom_handle, core_arch| RiscVArch::<
RiscVIMACDisassembler<Rv64GRegs>,
> {
handle: core_arch,
custom_handle,
_dis: PhantomData,
});
let arch32 = architecture::register_architecture("rv32gc", |core_arch| RiscVArch::<
RiscVIMACDisassembler<Rv32GRegs>,
> {
handle: core_arch,
_dis: PhantomData,
});
let arch64 = architecture::register_architecture("rv64gc", |core_arch| RiscVArch::<
RiscVIMACDisassembler<Rv64GRegs>,
> {
handle: core_arch,
_dis: PhantomData,
});

arch32.register_relocation_handler("ELF", |custom_handle, core_handler| {
RiscVELFRelocationHandler::<RiscVIMACDisassembler<Rv32GRegs>> {
Expand Down
2 changes: 1 addition & 1 deletion rust/examples/pdb-ng/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use binaryninja::downloadprovider::{DownloadInstanceInputOutputCallbacks, Downlo
use binaryninja::interaction::{MessageBoxButtonResult, MessageBoxButtonSet};
use binaryninja::settings::Settings;
use binaryninja::string::BnString;
use binaryninja::{add_optional_plugin_dependency, interaction, logger, user_directory};
use binaryninja::{interaction, logger, user_directory};
use parser::PDBParserInstance;

/// PDB Parser!!
Expand Down
2 changes: 1 addition & 1 deletion rust/examples/pdb-ng/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct PDBParserInstance<'a, S: Source<'a> + 'a> {
/// Parent binary view (usually during BinaryView::Finalize)
pub(crate) bv: &'a BinaryView,
/// Default arch of self.bv
pub(crate) arch: CoreArchitecture,
pub(crate) arch: &'static CoreArchitecture,
/// Default calling convention for self.arch
pub(crate) default_cc: Ref<CallingConvention<CoreArchitecture>>,
/// Thiscall calling convention for self.bv, or default_cc if we can't find one
Expand Down
34 changes: 14 additions & 20 deletions rust/examples/pdb-ng/src/type_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,31 +673,31 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
// TODO: Pointer suffix is not exposed
match data.indirection {
Some(Indirection::Near16) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
&self.arch,
self.arch,
base.as_ref(),
))))),
Some(Indirection::Far16) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
&self.arch,
self.arch,
base.as_ref(),
))))),
Some(Indirection::Huge16) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
&self.arch,
self.arch,
base.as_ref(),
))))),
Some(Indirection::Near32) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
&self.arch,
self.arch,
base.as_ref(),
))))),
Some(Indirection::Far32) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
&self.arch,
self.arch,
base.as_ref(),
))))),
Some(Indirection::Near64) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
&self.arch,
self.arch,
base.as_ref(),
))))),
Some(Indirection::Near128) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
&self.arch,
self.arch,
base.as_ref(),
))))),
None => Ok(Some(Box::new(ParsedType::Bare(base)))),
Expand Down Expand Up @@ -843,10 +843,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
Type::structure(builder.finalize().as_ref()),
max_confidence(),
),
name: bitfield_name(
last_bitfield_offset,
last_bitfield_idx,
),
name: bitfield_name(last_bitfield_offset, last_bitfield_idx),
offset: last_bitfield_offset,
access: MemberAccess::PublicAccess,
scope: MemberScope::NoScope,
Expand Down Expand Up @@ -880,10 +877,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
Type::structure(builder.finalize().as_ref()),
max_confidence(),
),
name: bitfield_name(
last_bitfield_offset,
last_bitfield_idx,
),
name: bitfield_name(last_bitfield_offset, last_bitfield_idx),
offset: last_bitfield_offset,
access: MemberAccess::PublicAccess,
scope: MemberScope::NoScope,
Expand Down Expand Up @@ -1081,7 +1075,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
for (offset, (name, method)) in virt_methods {
vt.insert(
&Conf::new(
Type::pointer(&self.arch, &Conf::new(method.method_type, max_confidence())),
Type::pointer(self.arch, &Conf::new(method.method_type, max_confidence())),
max_confidence(),
),
&name,
Expand All @@ -1106,7 +1100,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
self.named_types.insert(vt_name.clone(), vt_type.clone());

let vt_pointer = Type::pointer(
&self.arch,
self.arch,
&Conf::new(
Type::named_type_from_type(&QualifiedName::from(vt_name), vt_type.as_ref()),
max_confidence(),
Expand Down Expand Up @@ -1224,7 +1218,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
// Return UDT??
// This probably means the return value got pushed to the stack
fancy_return_type = Type::pointer(
&self.arch,
self.arch,
&Conf::new(return_type.clone(), max_confidence()),
);
fancy_arguments.insert(
Expand Down Expand Up @@ -1507,7 +1501,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
if return_stacky {
// Stack return via a pointer in the first parameter
fancy_return_type =
Conf::new(Type::pointer(&self.arch, &return_type), max_confidence());
Conf::new(Type::pointer(self.arch, &return_type), max_confidence());
fancy_arguments.insert(
0,
FunctionParameter::new(fancy_return_type.clone(), "__return".to_string(), None),
Expand Down Expand Up @@ -1562,7 +1556,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {

if let Some(base) = base {
Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
&self.arch,
self.arch,
base.as_ref(),
)))))
} else {
Expand Down
Loading

0 comments on commit 88d2765

Please sign in to comment.