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 3296f79
Show file tree
Hide file tree
Showing 18 changed files with 354 additions and 336 deletions.
39 changes: 16 additions & 23 deletions arch/riscv/src/lib.rs
Original file line number Diff line number Diff line change
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 @@ -2140,8 +2137,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 +2866,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/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 3296f79

Please sign in to comment.