Skip to content

Commit e487857

Browse files
committed
Add binary view parameter to calling convention callbacks
1 parent 3d10f49 commit e487857

8 files changed

Lines changed: 403 additions & 248 deletions

File tree

arch/arm64/arch_arm64.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,7 +2652,7 @@ class Arm64CallingConvention : public CallingConvention
26522652
virtual uint32_t GetFloatReturnValueRegister() override { return REG_V0; }
26532653

26542654

2655-
bool IsReturnTypeRegisterCompatible(Type* type) override
2655+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
26562656
{
26572657
if (!type)
26582658
return false;
@@ -2668,7 +2668,7 @@ class Arm64CallingConvention : public CallingConvention
26682668
}
26692669

26702670

2671-
bool IsArgumentTypeRegisterCompatible(Type* type) override
2671+
bool IsArgumentTypeRegisterCompatible(BinaryView*, Type* type) override
26722672
{
26732673
if (!type)
26742674
return false;
@@ -2678,7 +2678,7 @@ class Arm64CallingConvention : public CallingConvention
26782678
}
26792679

26802680

2681-
bool IsNonRegisterArgumentIndirect(Type*) override
2681+
bool IsNonRegisterArgumentIndirect(BinaryView*, Type*) override
26822682
{
26832683
return true;
26842684
}

arch/x86/arch_x86.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3824,7 +3824,7 @@ class X86BaseCallingConvention: public CallingConvention
38243824
return result;
38253825
}
38263826

3827-
bool IsReturnTypeRegisterCompatible(Type* type) override
3827+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
38283828
{
38293829
if (!type)
38303830
return false;
@@ -3850,7 +3850,7 @@ class X86SystemVCallingConvention: public X86BaseCallingConvention
38503850
{
38513851
}
38523852

3853-
bool IsReturnTypeRegisterCompatible(Type* type) override
3853+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
38543854
{
38553855
if (!type)
38563856
return false;
@@ -3864,7 +3864,7 @@ class X86SystemVCallingConvention: public X86BaseCallingConvention
38643864
return false;
38653865
}
38663866

3867-
int64_t GetStackAdjustmentForLocations(const std::optional<ValueLocation>& returnValue,
3867+
int64_t GetStackAdjustmentForLocations(BinaryView*, const std::optional<ValueLocation>& returnValue,
38683868
const vector<ValueLocation>&, const vector<Ref<Type>>&) override
38693869
{
38703870
if (!returnValue.has_value())
@@ -3915,7 +3915,7 @@ class X86SystemVStdcallCallingConvention: public X86BaseCallingConvention
39153915
return true;
39163916
}
39173917

3918-
bool IsReturnTypeRegisterCompatible(Type* type) override
3918+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
39193919
{
39203920
if (!type)
39213921
return false;
@@ -3943,7 +3943,7 @@ class X86RegParmCallingConvention: public X86BaseCallingConvention
39433943
return vector<uint32_t>{ XED_REG_EAX, XED_REG_EDX, XED_REG_ECX };
39443944
}
39453945

3946-
bool IsReturnTypeRegisterCompatible(Type* type) override
3946+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
39473947
{
39483948
if (!type)
39493949
return false;
@@ -3957,7 +3957,7 @@ class X86RegParmCallingConvention: public X86BaseCallingConvention
39573957
return false;
39583958
}
39593959

3960-
bool IsArgumentTypeRegisterCompatible(Type* type) override
3960+
bool IsArgumentTypeRegisterCompatible(BinaryView*, Type* type) override
39613961
{
39623962
if (!type)
39633963
return false;
@@ -4004,7 +4004,7 @@ class X86GCCFastcallCallingConvention: public X86BaseCallingConvention
40044004
return true;
40054005
}
40064006

4007-
bool IsReturnTypeRegisterCompatible(Type* type) override
4007+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
40084008
{
40094009
if (!type)
40104010
return false;
@@ -4042,7 +4042,7 @@ class X86ClangFastcallCallingConvention: public X86BaseCallingConvention
40424042
return true;
40434043
}
40444044

4045-
bool IsReturnTypeRegisterCompatible(Type* type) override
4045+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
40464046
{
40474047
if (!type)
40484048
return false;
@@ -4109,7 +4109,7 @@ class X86GCCThiscallCallingConvention: public X86BaseCallingConvention
41094109
return true;
41104110
}
41114111

4112-
bool IsReturnTypeRegisterCompatible(Type* type) override
4112+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
41134113
{
41144114
if (!type)
41154115
return false;
@@ -4152,7 +4152,7 @@ class X86ClangThiscallCallingConvention: public X86BaseCallingConvention
41524152
return true;
41534153
}
41544154

4155-
bool IsReturnTypeRegisterCompatible(Type* type) override
4155+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
41564156
{
41574157
if (!type)
41584158
return false;
@@ -4222,7 +4222,7 @@ class X86PascalCallingConvention : public X86BaseCallingConvention
42224222
public:
42234223
X86PascalCallingConvention(Architecture* arch) : X86BaseCallingConvention(arch, "pascal") {}
42244224

4225-
bool IsNonRegisterArgumentIndirect(Type* type) override
4225+
bool IsNonRegisterArgumentIndirect(BinaryView*, Type* type) override
42264226
{
42274227
return type && !type->IsFloat() && type->GetWidth() > 4;
42284228
}
@@ -4261,7 +4261,7 @@ class X86PascalRegisterCallingConvention : public X86BaseCallingConvention
42614261
return { XED_REG_EAX, XED_REG_EDX, XED_REG_ECX };
42624262
}
42634263

4264-
bool IsNonRegisterArgumentIndirect(Type* type) override
4264+
bool IsNonRegisterArgumentIndirect(BinaryView*, Type* type) override
42654265
{
42664266
return type && !type->IsFloat() && type->GetWidth() > 4;
42674267
}
@@ -4313,18 +4313,18 @@ class X86GoStackCallingConvention: public CallingConvention
43134313
return result;
43144314
}
43154315

4316-
ValueLocation GetReturnValueLocation(const ReturnValue&) override
4316+
ValueLocation GetReturnValueLocation(BinaryView*, const ReturnValue&) override
43174317
{
43184318
// It is not possible for this API to determine the return value location on the stack at
43194319
// this point, return an invalid location and fall back to GetCallLayout.
43204320
return ValueLocation();
43214321
}
43224322

4323-
CallLayout GetCallLayout(const ReturnValue& returnValue, const vector<FunctionParameter>& params,
4323+
CallLayout GetCallLayout(BinaryView* view, const ReturnValue& returnValue, const vector<FunctionParameter>& params,
43244324
const std::optional<set<uint32_t>>& permittedRegs) override
43254325
{
43264326
CallLayout result;
4327-
result.parameters = GetParameterLocations(result.returnValue, params, permittedRegs);
4327+
result.parameters = GetParameterLocations(view, result.returnValue, params, permittedRegs);
43284328

43294329
if (returnValue.type.GetValue() && returnValue.type->GetClass() != VoidTypeClass)
43304330
{
@@ -4364,7 +4364,7 @@ class X86GoStackCallingConvention: public CallingConvention
43644364
}
43654365
}
43664366

4367-
result.registerStackAdjustments = GetRegisterStackAdjustments(result.returnValue, result.parameters);
4367+
result.registerStackAdjustments = GetRegisterStackAdjustments(view, result.returnValue, result.parameters);
43684368
return result;
43694369
}
43704370
};
@@ -4477,7 +4477,7 @@ class X64WindowsCallingConvention: public X64BaseCallingConvention
44774477
return true;
44784478
}
44794479

4480-
bool IsReturnTypeRegisterCompatible(Type* type) override
4480+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
44814481
{
44824482
if (!type)
44834483
return false;
@@ -4492,7 +4492,7 @@ class X64WindowsCallingConvention: public X64BaseCallingConvention
44924492
return Variable::Register(XED_REG_RAX);
44934493
}
44944494

4495-
bool IsArgumentTypeRegisterCompatible(Type* type) override
4495+
bool IsArgumentTypeRegisterCompatible(BinaryView*, Type* type) override
44964496
{
44974497
if (!type)
44984498
return false;
@@ -4502,7 +4502,7 @@ class X64WindowsCallingConvention: public X64BaseCallingConvention
45024502
|| type->GetWidth() == 8;
45034503
}
45044504

4505-
bool IsNonRegisterArgumentIndirect(Type*) override
4505+
bool IsNonRegisterArgumentIndirect(BinaryView*, Type*) override
45064506
{
45074507
return true;
45084508
}
@@ -4581,18 +4581,18 @@ class X64GoStackCallingConvention: public CallingConvention
45814581
return result;
45824582
}
45834583

4584-
ValueLocation GetReturnValueLocation(const ReturnValue&) override
4584+
ValueLocation GetReturnValueLocation(BinaryView*, const ReturnValue&) override
45854585
{
45864586
// It is not possible for this API to determine the return value location on the stack at
45874587
// this point, return an invalid location and fall back to GetCallLayout.
45884588
return ValueLocation();
45894589
}
45904590

4591-
CallLayout GetCallLayout(const ReturnValue& returnValue, const vector<FunctionParameter>& params,
4591+
CallLayout GetCallLayout(BinaryView* view, const ReturnValue& returnValue, const vector<FunctionParameter>& params,
45924592
const std::optional<set<uint32_t>>& permittedRegs) override
45934593
{
45944594
CallLayout result;
4595-
result.parameters = GetParameterLocations(result.returnValue, params, permittedRegs);
4595+
result.parameters = GetParameterLocations(view, result.returnValue, params, permittedRegs);
45964596

45974597
if (returnValue.type.GetValue() && returnValue.type->GetClass() != VoidTypeClass)
45984598
{
@@ -4632,7 +4632,7 @@ class X64GoStackCallingConvention: public CallingConvention
46324632
}
46334633
}
46344634

4635-
result.registerStackAdjustments = GetRegisterStackAdjustments(result.returnValue, result.parameters);
4635+
result.registerStackAdjustments = GetRegisterStackAdjustments(view, result.returnValue, result.parameters);
46364636
return result;
46374637
}
46384638
};

binaryninjaapi.h

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17853,32 +17853,32 @@ namespace BinaryNinja {
1785317853
static void GetParameterVariableForIncomingVariableCallback(
1785417854
void* ctxt, const BNVariable* var, BNFunction* func, BNVariable* result);
1785517855

17856-
static bool IsReturnTypeRegisterCompatibleCallback(void* ctxt, BNType* type);
17856+
static bool IsReturnTypeRegisterCompatibleCallback(void* ctxt, BNBinaryView* view, BNType* type);
1785717857
static void GetIndirectReturnValueLocationCallback(void* ctxt, BNVariable* outVar);
1785817858
static bool GetReturnedIndirectReturnValuePointerCallback(void* ctxt, BNVariable* outVar);
1785917859

17860-
static bool IsArgumentTypeRegisterCompatibleCallback(void* ctxt, BNType* type);
17861-
static bool IsNonRegisterArgumentIndirectCallback(void* ctxt, BNType* type);
17860+
static bool IsArgumentTypeRegisterCompatibleCallback(void* ctxt, BNBinaryView* view, BNType* type);
17861+
static bool IsNonRegisterArgumentIndirectCallback(void* ctxt, BNBinaryView* view, BNType* type);
1786217862
static bool AreStackArgumentsNaturallyAlignedCallback(void* ctxt);
1786317863
static bool AreStackArgumentsPushedLeftToRightCallback(void* ctxt);
1786417864

17865-
static void GetCallLayoutCallback(void* ctxt, BNReturnValue* returnValue, BNFunctionParameter* params,
17866-
size_t paramCount, bool hasPermittedRegs, uint32_t* permittedRegs, size_t permittedRegCount,
17867-
BNCallLayout* result);
17865+
static void GetCallLayoutCallback(void* ctxt, BNBinaryView* view, BNReturnValue* returnValue,
17866+
BNFunctionParameter* params, size_t paramCount, bool hasPermittedRegs, uint32_t* permittedRegs,
17867+
size_t permittedRegCount, BNCallLayout* result);
1786817868
static void FreeCallLayoutCallback(void* ctxt, BNCallLayout* layout);
1786917869
static void GetReturnValueLocationCallback(
17870-
void* ctxt, BNReturnValue* returnValue, BNValueLocation* outLocation);
17870+
void* ctxt, BNBinaryView* view, BNReturnValue* returnValue, BNValueLocation* outLocation);
1787117871
static void FreeValueLocationCallback(void* ctxt, BNValueLocation* location);
17872-
static BNValueLocation* GetParameterLocationsCallback(void* ctxt, BNValueLocation* returnValue,
17873-
BNFunctionParameter* params, size_t paramCount, bool hasPermittedRegs, uint32_t* permittedRegs,
17874-
size_t permittedRegCount, size_t* outLocationCount);
17872+
static BNValueLocation* GetParameterLocationsCallback(void* ctxt, BNBinaryView* view,
17873+
BNValueLocation* returnValue, BNFunctionParameter* params, size_t paramCount, bool hasPermittedRegs,
17874+
uint32_t* permittedRegs, size_t permittedRegCount, size_t* outLocationCount);
1787517875
static void FreeParameterLocationsCallback(void* ctxt, BNValueLocation* locations, size_t count);
1787617876
static BNVariable* GetParameterOrderingForVariablesCallback(
17877-
void* ctxt, BNVariable* vars, BNType** types, size_t paramCount, size_t* outCount);
17877+
void* ctxt, BNBinaryView* view, BNVariable* vars, BNType** types, size_t paramCount, size_t* outCount);
1787817878
static void FreeVariableListCallback(void* ctxt, BNVariable* vars, size_t count);
17879-
static int64_t GetStackAdjustmentForLocationsCallback(
17880-
void* ctxt, BNValueLocation* returnValue, BNValueLocation* locations, BNType** types, size_t paramCount);
17881-
static size_t GetRegisterStackAdjustmentsCallback(void* ctxt, BNValueLocation* returnValue,
17879+
static int64_t GetStackAdjustmentForLocationsCallback(void* ctxt, BNBinaryView* view,
17880+
BNValueLocation* returnValue, BNValueLocation* locations, BNType** types, size_t paramCount);
17881+
static size_t GetRegisterStackAdjustmentsCallback(void* ctxt, BNBinaryView* view, BNValueLocation* returnValue,
1788217882
BNValueLocation* params, size_t paramCount, uint32_t** outRegs, int32_t** outAdjust);
1788317883
static void FreeRegisterStackAdjustmentsCallback(void* ctxt, uint32_t* regs, int32_t* adjust, size_t count);
1788417884

@@ -17924,36 +17924,40 @@ namespace BinaryNinja {
1792417924
virtual Variable GetIncomingVariableForParameterVariable(const Variable& var, Function* func);
1792517925
virtual Variable GetParameterVariableForIncomingVariable(const Variable& var, Function* func);
1792617926

17927-
virtual bool IsReturnTypeRegisterCompatible(Type* type);
17927+
virtual bool IsReturnTypeRegisterCompatible(BinaryView* view, Type* type);
1792817928
bool DefaultIsReturnTypeRegisterCompatible(Type* type);
1792917929
virtual Variable GetIndirectReturnValueLocation();
1793017930
Variable GetDefaultIndirectReturnValueLocation();
1793117931
virtual std::optional<Variable> GetReturnedIndirectReturnValuePointer();
1793217932

17933-
virtual bool IsArgumentTypeRegisterCompatible(Type* type);
17933+
virtual bool IsArgumentTypeRegisterCompatible(BinaryView* view, Type* type);
1793417934
bool DefaultIsArgumentTypeRegisterCompatible(Type* type);
17935-
virtual bool IsNonRegisterArgumentIndirect(Type* type);
17935+
virtual bool IsNonRegisterArgumentIndirect(BinaryView* view, Type* type);
1793617936
virtual bool AreStackArgumentsNaturallyAligned();
1793717937
virtual bool AreStackArgumentsPushedLeftToRight();
1793817938

17939-
virtual CallLayout GetCallLayout(const ReturnValue& returnValue, const std::vector<FunctionParameter>& params,
17940-
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
17941-
virtual ValueLocation GetReturnValueLocation(const ReturnValue& returnValue);
17942-
virtual std::vector<ValueLocation> GetParameterLocations(const std::optional<ValueLocation>& returnValue,
17939+
virtual CallLayout GetCallLayout(BinaryView* view, const ReturnValue& returnValue,
1794317940
const std::vector<FunctionParameter>& params,
1794417941
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
17945-
virtual std::vector<Variable> GetParameterOrderingForVariables(const std::map<Variable, Ref<Type>>& params);
17946-
virtual int64_t GetStackAdjustmentForLocations(const std::optional<ValueLocation>& returnValue,
17947-
const std::vector<ValueLocation>& locations, const std::vector<Ref<Type>>& types);
17948-
virtual std::map<uint32_t, int32_t> GetRegisterStackAdjustments(
17942+
virtual ValueLocation GetReturnValueLocation(BinaryView* view, const ReturnValue& returnValue);
17943+
virtual std::vector<ValueLocation> GetParameterLocations(BinaryView* view,
17944+
const std::optional<ValueLocation>& returnValue, const std::vector<FunctionParameter>& params,
17945+
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
17946+
virtual std::vector<Variable> GetParameterOrderingForVariables(
17947+
BinaryView* view, const std::map<Variable, Ref<Type>>& params);
17948+
virtual int64_t GetStackAdjustmentForLocations(BinaryView* view,
17949+
const std::optional<ValueLocation>& returnValue, const std::vector<ValueLocation>& locations,
17950+
const std::vector<Ref<Type>>& types);
17951+
virtual std::map<uint32_t, int32_t> GetRegisterStackAdjustments(BinaryView* view,
1794917952
const std::optional<ValueLocation>& returnValue, const std::vector<ValueLocation>& params);
1795017953

17951-
CallLayout GetDefaultCallLayout(const ReturnValue& returnValue, const std::vector<FunctionParameter>& params,
17952-
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
17953-
ValueLocation GetDefaultReturnValueLocation(const ReturnValue& returnValue);
17954-
std::vector<ValueLocation> GetDefaultParameterLocations(const std::optional<ValueLocation>& returnValue,
17954+
CallLayout GetDefaultCallLayout(BinaryView* view, const ReturnValue& returnValue,
1795517955
const std::vector<FunctionParameter>& params,
1795617956
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
17957+
ValueLocation GetDefaultReturnValueLocation(BinaryView* view, const ReturnValue& returnValue);
17958+
std::vector<ValueLocation> GetDefaultParameterLocations(BinaryView* view,
17959+
const std::optional<ValueLocation>& returnValue, const std::vector<FunctionParameter>& params,
17960+
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
1795717961
std::vector<Variable> GetDefaultParameterOrderingForVariables(const std::map<Variable, Ref<Type>>& params);
1795817962
int64_t GetDefaultStackAdjustmentForLocations(const std::optional<ValueLocation>& returnValue,
1795917963
const std::vector<ValueLocation>& locations, const std::vector<Ref<Type>>& types);
@@ -17994,26 +17998,28 @@ namespace BinaryNinja {
1799417998
virtual Variable GetIncomingVariableForParameterVariable(const Variable& var, Function* func) override;
1799517999
virtual Variable GetParameterVariableForIncomingVariable(const Variable& var, Function* func) override;
1799618000

17997-
virtual bool IsReturnTypeRegisterCompatible(Type* type) override;
18001+
virtual bool IsReturnTypeRegisterCompatible(BinaryView* view, Type* type) override;
1799818002
virtual Variable GetIndirectReturnValueLocation() override;
1799918003
virtual std::optional<Variable> GetReturnedIndirectReturnValuePointer() override;
1800018004

18001-
virtual bool IsArgumentTypeRegisterCompatible(Type* type) override;
18002-
virtual bool IsNonRegisterArgumentIndirect(Type* type) override;
18005+
virtual bool IsArgumentTypeRegisterCompatible(BinaryView* view, Type* type) override;
18006+
virtual bool IsNonRegisterArgumentIndirect(BinaryView* view, Type* type) override;
1800318007
virtual bool AreStackArgumentsNaturallyAligned() override;
1800418008
virtual bool AreStackArgumentsPushedLeftToRight() override;
1800518009

18006-
virtual CallLayout GetCallLayout(const ReturnValue& returnValue, const std::vector<FunctionParameter>& params,
18007-
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt) override;
18008-
virtual ValueLocation GetReturnValueLocation(const ReturnValue& returnValue) override;
18009-
virtual std::vector<ValueLocation> GetParameterLocations(const std::optional<ValueLocation>& returnValue,
18010+
virtual CallLayout GetCallLayout(BinaryView* view, const ReturnValue& returnValue,
1801018011
const std::vector<FunctionParameter>& params,
1801118012
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt) override;
18013+
virtual ValueLocation GetReturnValueLocation(BinaryView* view, const ReturnValue& returnValue) override;
18014+
virtual std::vector<ValueLocation> GetParameterLocations(BinaryView* view,
18015+
const std::optional<ValueLocation>& returnValue, const std::vector<FunctionParameter>& params,
18016+
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt) override;
1801218017
virtual std::vector<Variable> GetParameterOrderingForVariables(
18013-
const std::map<Variable, Ref<Type>>& params) override;
18014-
virtual int64_t GetStackAdjustmentForLocations(const std::optional<ValueLocation>& returnValue,
18015-
const std::vector<ValueLocation>& locations, const std::vector<Ref<Type>>& types) override;
18016-
virtual std::map<uint32_t, int32_t> GetRegisterStackAdjustments(
18018+
BinaryView* view, const std::map<Variable, Ref<Type>>& params) override;
18019+
virtual int64_t GetStackAdjustmentForLocations(BinaryView* view,
18020+
const std::optional<ValueLocation>& returnValue, const std::vector<ValueLocation>& locations,
18021+
const std::vector<Ref<Type>>& types) override;
18022+
virtual std::map<uint32_t, int32_t> GetRegisterStackAdjustments(BinaryView* view,
1801718023
const std::optional<ValueLocation>& returnValue, const std::vector<ValueLocation>& params) override;
1801818024
};
1801918025

0 commit comments

Comments
 (0)