Skip to content

Commit 72f8924

Browse files
committed
Refactor calling convention Rust API to allow default implementations
Also implements the new calling convention APIs in Rust
1 parent 9e5296e commit 72f8924

7 files changed

Lines changed: 1554 additions & 297 deletions

File tree

arch/riscv/src/lib.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use binaryninja::{
1414
UnusedRegisterStack,
1515
},
1616
binary_view::{BinaryView, BinaryViewType},
17-
calling_convention::{register_calling_convention, CallingConvention, ConventionBuilder},
17+
calling_convention::{
18+
register_calling_convention, CallingConvention, ConventionBuilder, CoreCallingConvention,
19+
},
1820
disassembly::{InstructionTextToken, InstructionTextTokenKind},
1921
function::Function,
2022
function_recognizer::FunctionRecognizer,
@@ -2755,12 +2757,22 @@ impl<D: RiscVDisassembler> AsRef<CoreRelocationHandler> for RiscVELFRelocationHa
27552757
}
27562758

27572759
struct RiscVCC<D: RiscVDisassembler> {
2760+
core: CoreCallingConvention,
27582761
_dis: PhantomData<D>,
27592762
}
27602763

27612764
impl<D: RiscVDisassembler> RiscVCC<D> {
2762-
fn new() -> Self {
2763-
RiscVCC { _dis: PhantomData }
2765+
fn new(core: CoreCallingConvention) -> Self {
2766+
RiscVCC {
2767+
core,
2768+
_dis: PhantomData,
2769+
}
2770+
}
2771+
}
2772+
2773+
impl<D: RiscVDisassembler> AsRef<CoreCallingConvention> for RiscVCC<D> {
2774+
fn as_ref(&self) -> &CoreCallingConvention {
2775+
&self.core
27642776
}
27652777
}
27662778

@@ -3087,17 +3099,13 @@ pub extern "C" fn CorePluginInit() -> bool {
30873099
arch32.register_function_recognizer(RiscVELFPLTRecognizer);
30883100
arch64.register_function_recognizer(RiscVELFPLTRecognizer);
30893101

3090-
let cc32 = register_calling_convention(
3091-
arch32,
3092-
"default",
3093-
RiscVCC::<RiscVIMACDisassembler<Rv32GRegs>>::new(),
3094-
);
3102+
let cc32 = register_calling_convention(arch32, "default", |core| {
3103+
RiscVCC::<RiscVIMACDisassembler<Rv32GRegs>>::new(core)
3104+
});
30953105
arch32.set_default_calling_convention(&cc32);
3096-
let cc64 = register_calling_convention(
3097-
arch64,
3098-
"default",
3099-
RiscVCC::<RiscVIMACDisassembler<Rv64GRegs>>::new(),
3100-
);
3106+
let cc64 = register_calling_convention(arch64, "default", |core| {
3107+
RiscVCC::<RiscVIMACDisassembler<Rv64GRegs>>::new(core)
3108+
});
31013109
arch64.set_default_calling_convention(&cc64);
31023110

31033111
if let Some(bvt) = BinaryViewType::by_name("ELF") {

plugins/pdb-ng/src/type_parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2421,7 +2421,7 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
24212421
// that the calling convention can determine this by default
24222422
let default_return_location = convention
24232423
.contents
2424-
.return_value_location(self.bv, return_value.clone());
2424+
.return_value_location(Some(self.bv), &return_value);
24252425
if default_return_location.indirect {
24262426
None
24272427
} else {

rust/src/architecture.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::{
2626
calling_convention::CoreCallingConvention,
2727
data_buffer::DataBuffer,
2828
disassembly::InstructionTextToken,
29+
ffi::INVALID_REGISTER,
2930
function::Function,
3031
platform::Platform,
3132
rc::*,
@@ -665,6 +666,13 @@ impl CoreArchitecture {
665666
pub fn name(&self) -> String {
666667
unsafe { BnString::into_string(BNGetArchitectureName(self.handle)) }
667668
}
669+
670+
pub fn register_stack_for_register(&self, reg: CoreRegister) -> Option<CoreRegisterStack> {
671+
match unsafe { BNGetArchitectureRegisterStackForRegister(self.handle, reg.id().0) } {
672+
INVALID_REGISTER => None,
673+
reg_stack => CoreRegisterStack::new(*self, RegisterStackId::from(reg_stack)),
674+
}
675+
}
668676
}
669677

670678
unsafe impl Send for CoreArchitecture {}
@@ -967,14 +975,14 @@ impl Architecture for CoreArchitecture {
967975

968976
fn stack_pointer_reg(&self) -> Option<CoreRegister> {
969977
match unsafe { BNGetArchitectureStackPointerRegister(self.handle) } {
970-
0xffff_ffff => None,
978+
INVALID_REGISTER => None,
971979
reg => Some(CoreRegister::new(*self, reg.into())?),
972980
}
973981
}
974982

975983
fn link_reg(&self) -> Option<CoreRegister> {
976984
match unsafe { BNGetArchitectureLinkRegister(self.handle) } {
977-
0xffff_ffff => None,
985+
INVALID_REGISTER => None,
978986
reg => Some(CoreRegister::new(*self, reg.into())?),
979987
}
980988
}
@@ -1257,7 +1265,7 @@ pub trait ArchitectureExt: Architecture {
12571265
let name = name.to_cstr();
12581266

12591267
match unsafe { BNGetArchitectureRegisterByName(self.as_ref().handle, name.as_ptr()) } {
1260-
0xffff_ffff => None,
1268+
INVALID_REGISTER => None,
12611269
reg => self.register_from_id(reg.into()),
12621270
}
12631271
}
@@ -2130,7 +2138,7 @@ where
21302138
if let Some(reg) = custom_arch.stack_pointer_reg() {
21312139
reg.id().0
21322140
} else {
2133-
0xffff_ffff
2141+
INVALID_REGISTER
21342142
}
21352143
}
21362144

@@ -2143,7 +2151,7 @@ where
21432151
if let Some(reg) = custom_arch.link_reg() {
21442152
reg.id().0
21452153
} else {
2146-
0xffff_ffff
2154+
INVALID_REGISTER
21472155
}
21482156
}
21492157

@@ -2198,7 +2206,7 @@ where
21982206
result.firstTopRelativeReg = reg.id().0;
21992207
result.topRelativeCount = count as u32;
22002208
} else {
2201-
result.firstTopRelativeReg = 0xffff_ffff;
2209+
result.firstTopRelativeReg = INVALID_REGISTER;
22022210
result.topRelativeCount = 0;
22032211
}
22042212

0 commit comments

Comments
 (0)