Skip to content

Commit ec89f70

Browse files
committed
Add binary view parameter to calling convention callbacks
1 parent 20c6392 commit ec89f70

8 files changed

Lines changed: 410 additions & 253 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
@@ -3828,7 +3828,7 @@ class X86BaseCallingConvention: public CallingConvention
38283828
return result;
38293829
}
38303830

3831-
bool IsReturnTypeRegisterCompatible(Type* type) override
3831+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
38323832
{
38333833
if (!type)
38343834
return false;
@@ -3854,7 +3854,7 @@ class X86SystemVCallingConvention: public X86BaseCallingConvention
38543854
{
38553855
}
38563856

3857-
bool IsReturnTypeRegisterCompatible(Type* type) override
3857+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
38583858
{
38593859
if (!type)
38603860
return false;
@@ -3868,7 +3868,7 @@ class X86SystemVCallingConvention: public X86BaseCallingConvention
38683868
return false;
38693869
}
38703870

3871-
int64_t GetStackAdjustmentForLocations(const std::optional<ValueLocation>& returnValue,
3871+
int64_t GetStackAdjustmentForLocations(BinaryView*, const std::optional<ValueLocation>& returnValue,
38723872
const vector<ValueLocation>&, const vector<Ref<Type>>&) override
38733873
{
38743874
if (!returnValue.has_value())
@@ -3919,7 +3919,7 @@ class X86SystemVStdcallCallingConvention: public X86BaseCallingConvention
39193919
return true;
39203920
}
39213921

3922-
bool IsReturnTypeRegisterCompatible(Type* type) override
3922+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
39233923
{
39243924
if (!type)
39253925
return false;
@@ -3947,7 +3947,7 @@ class X86RegParmCallingConvention: public X86BaseCallingConvention
39473947
return vector<uint32_t>{ XED_REG_EAX, XED_REG_EDX, XED_REG_ECX };
39483948
}
39493949

3950-
bool IsReturnTypeRegisterCompatible(Type* type) override
3950+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
39513951
{
39523952
if (!type)
39533953
return false;
@@ -3961,7 +3961,7 @@ class X86RegParmCallingConvention: public X86BaseCallingConvention
39613961
return false;
39623962
}
39633963

3964-
bool IsArgumentTypeRegisterCompatible(Type* type) override
3964+
bool IsArgumentTypeRegisterCompatible(BinaryView*, Type* type) override
39653965
{
39663966
if (!type)
39673967
return false;
@@ -4008,7 +4008,7 @@ class X86GCCFastcallCallingConvention: public X86BaseCallingConvention
40084008
return true;
40094009
}
40104010

4011-
bool IsReturnTypeRegisterCompatible(Type* type) override
4011+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
40124012
{
40134013
if (!type)
40144014
return false;
@@ -4046,7 +4046,7 @@ class X86ClangFastcallCallingConvention: public X86BaseCallingConvention
40464046
return true;
40474047
}
40484048

4049-
bool IsReturnTypeRegisterCompatible(Type* type) override
4049+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
40504050
{
40514051
if (!type)
40524052
return false;
@@ -4113,7 +4113,7 @@ class X86GCCThiscallCallingConvention: public X86BaseCallingConvention
41134113
return true;
41144114
}
41154115

4116-
bool IsReturnTypeRegisterCompatible(Type* type) override
4116+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
41174117
{
41184118
if (!type)
41194119
return false;
@@ -4156,7 +4156,7 @@ class X86ClangThiscallCallingConvention: public X86BaseCallingConvention
41564156
return true;
41574157
}
41584158

4159-
bool IsReturnTypeRegisterCompatible(Type* type) override
4159+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
41604160
{
41614161
if (!type)
41624162
return false;
@@ -4226,7 +4226,7 @@ class X86PascalCallingConvention : public X86BaseCallingConvention
42264226
public:
42274227
X86PascalCallingConvention(Architecture* arch) : X86BaseCallingConvention(arch, "pascal") {}
42284228

4229-
bool IsNonRegisterArgumentIndirect(Type* type) override
4229+
bool IsNonRegisterArgumentIndirect(BinaryView*, Type* type) override
42304230
{
42314231
return type && !type->IsFloat() && type->GetWidth() > 4;
42324232
}
@@ -4265,7 +4265,7 @@ class X86PascalRegisterCallingConvention : public X86BaseCallingConvention
42654265
return { XED_REG_EAX, XED_REG_EDX, XED_REG_ECX };
42664266
}
42674267

4268-
bool IsNonRegisterArgumentIndirect(Type* type) override
4268+
bool IsNonRegisterArgumentIndirect(BinaryView*, Type* type) override
42694269
{
42704270
return type && !type->IsFloat() && type->GetWidth() > 4;
42714271
}
@@ -4317,18 +4317,18 @@ class X86GoStackCallingConvention: public CallingConvention
43174317
return result;
43184318
}
43194319

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

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

43334333
if (returnValue.type.GetValue() && returnValue.type->GetClass() != VoidTypeClass)
43344334
{
@@ -4368,7 +4368,7 @@ class X86GoStackCallingConvention: public CallingConvention
43684368
}
43694369
}
43704370

4371-
result.registerStackAdjustments = GetRegisterStackAdjustments(result.returnValue, result.parameters);
4371+
result.registerStackAdjustments = GetRegisterStackAdjustments(view, result.returnValue, result.parameters);
43724372
return result;
43734373
}
43744374
};
@@ -4481,7 +4481,7 @@ class X64WindowsCallingConvention: public X64BaseCallingConvention
44814481
return true;
44824482
}
44834483

4484-
bool IsReturnTypeRegisterCompatible(Type* type) override
4484+
bool IsReturnTypeRegisterCompatible(BinaryView*, Type* type) override
44854485
{
44864486
if (!type)
44874487
return false;
@@ -4496,7 +4496,7 @@ class X64WindowsCallingConvention: public X64BaseCallingConvention
44964496
return Variable::Register(XED_REG_RAX);
44974497
}
44984498

4499-
bool IsArgumentTypeRegisterCompatible(Type* type) override
4499+
bool IsArgumentTypeRegisterCompatible(BinaryView*, Type* type) override
45004500
{
45014501
if (!type)
45024502
return false;
@@ -4506,7 +4506,7 @@ class X64WindowsCallingConvention: public X64BaseCallingConvention
45064506
|| type->GetWidth() == 8;
45074507
}
45084508

4509-
bool IsNonRegisterArgumentIndirect(Type*) override
4509+
bool IsNonRegisterArgumentIndirect(BinaryView*, Type*) override
45104510
{
45114511
return true;
45124512
}
@@ -4585,18 +4585,18 @@ class X64GoStackCallingConvention: public CallingConvention
45854585
return result;
45864586
}
45874587

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

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

46014601
if (returnValue.type.GetValue() && returnValue.type->GetClass() != VoidTypeClass)
46024602
{
@@ -4636,7 +4636,7 @@ class X64GoStackCallingConvention: public CallingConvention
46364636
}
46374637
}
46384638

4639-
result.registerStackAdjustments = GetRegisterStackAdjustments(result.returnValue, result.parameters);
4639+
result.registerStackAdjustments = GetRegisterStackAdjustments(view, result.returnValue, result.parameters);
46404640
return result;
46414641
}
46424642
};

binaryninjaapi.h

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

17905-
static bool IsReturnTypeRegisterCompatibleCallback(void* ctxt, BNType* type);
17905+
static bool IsReturnTypeRegisterCompatibleCallback(void* ctxt, BNBinaryView* view, BNType* type);
1790617906
static void GetIndirectReturnValueLocationCallback(void* ctxt, BNVariable* outVar);
1790717907
static bool GetReturnedIndirectReturnValuePointerCallback(void* ctxt, BNVariable* outVar);
1790817908

17909-
static bool IsArgumentTypeRegisterCompatibleCallback(void* ctxt, BNType* type);
17910-
static bool IsNonRegisterArgumentIndirectCallback(void* ctxt, BNType* type);
17909+
static bool IsArgumentTypeRegisterCompatibleCallback(void* ctxt, BNBinaryView* view, BNType* type);
17910+
static bool IsNonRegisterArgumentIndirectCallback(void* ctxt, BNBinaryView* view, BNType* type);
1791117911
static bool AreStackArgumentsNaturallyAlignedCallback(void* ctxt);
1791217912
static bool AreStackArgumentsPushedLeftToRightCallback(void* ctxt);
1791317913

17914-
static void GetCallLayoutCallback(void* ctxt, BNReturnValue* returnValue, BNFunctionParameter* params,
17915-
size_t paramCount, bool hasPermittedRegs, uint32_t* permittedRegs, size_t permittedRegCount,
17916-
BNCallLayout* result);
17914+
static void GetCallLayoutCallback(void* ctxt, BNBinaryView* view, BNReturnValue* returnValue,
17915+
BNFunctionParameter* params, size_t paramCount, bool hasPermittedRegs, uint32_t* permittedRegs,
17916+
size_t permittedRegCount, BNCallLayout* result);
1791717917
static void FreeCallLayoutCallback(void* ctxt, BNCallLayout* layout);
1791817918
static void GetReturnValueLocationCallback(
17919-
void* ctxt, BNReturnValue* returnValue, BNValueLocation* outLocation);
17919+
void* ctxt, BNBinaryView* view, BNReturnValue* returnValue, BNValueLocation* outLocation);
1792017920
static void FreeValueLocationCallback(void* ctxt, BNValueLocation* location);
17921-
static BNValueLocation* GetParameterLocationsCallback(void* ctxt, BNValueLocation* returnValue,
17922-
BNFunctionParameter* params, size_t paramCount, bool hasPermittedRegs, uint32_t* permittedRegs,
17923-
size_t permittedRegCount, size_t* outLocationCount);
17921+
static BNValueLocation* GetParameterLocationsCallback(void* ctxt, BNBinaryView* view,
17922+
BNValueLocation* returnValue, BNFunctionParameter* params, size_t paramCount, bool hasPermittedRegs,
17923+
uint32_t* permittedRegs, size_t permittedRegCount, size_t* outLocationCount);
1792417924
static void FreeParameterLocationsCallback(void* ctxt, BNValueLocation* locations, size_t count);
1792517925
static BNVariable* GetParameterOrderingForVariablesCallback(
17926-
void* ctxt, BNVariable* vars, BNType** types, size_t paramCount, size_t* outCount);
17926+
void* ctxt, BNBinaryView* view, BNVariable* vars, BNType** types, size_t paramCount, size_t* outCount);
1792717927
static void FreeVariableListCallback(void* ctxt, BNVariable* vars, size_t count);
17928-
static int64_t GetStackAdjustmentForLocationsCallback(
17929-
void* ctxt, BNValueLocation* returnValue, BNValueLocation* locations, BNType** types, size_t paramCount);
17930-
static size_t GetRegisterStackAdjustmentsCallback(void* ctxt, BNValueLocation* returnValue,
17928+
static int64_t GetStackAdjustmentForLocationsCallback(void* ctxt, BNBinaryView* view,
17929+
BNValueLocation* returnValue, BNValueLocation* locations, BNType** types, size_t paramCount);
17930+
static size_t GetRegisterStackAdjustmentsCallback(void* ctxt, BNBinaryView* view, BNValueLocation* returnValue,
1793117931
BNValueLocation* params, size_t paramCount, uint32_t** outRegs, int32_t** outAdjust);
1793217932
static void FreeRegisterStackAdjustmentsCallback(void* ctxt, uint32_t* regs, int32_t* adjust, size_t count);
1793317933

@@ -17973,36 +17973,40 @@ namespace BinaryNinja {
1797317973
virtual Variable GetIncomingVariableForParameterVariable(const Variable& var, Function* func);
1797417974
virtual Variable GetParameterVariableForIncomingVariable(const Variable& var, Function* func);
1797517975

17976-
virtual bool IsReturnTypeRegisterCompatible(Type* type);
17976+
virtual bool IsReturnTypeRegisterCompatible(BinaryView* view, Type* type);
1797717977
bool DefaultIsReturnTypeRegisterCompatible(Type* type);
1797817978
virtual Variable GetIndirectReturnValueLocation();
1797917979
Variable GetDefaultIndirectReturnValueLocation();
1798017980
virtual std::optional<Variable> GetReturnedIndirectReturnValuePointer();
1798117981

17982-
virtual bool IsArgumentTypeRegisterCompatible(Type* type);
17982+
virtual bool IsArgumentTypeRegisterCompatible(BinaryView* view, Type* type);
1798317983
bool DefaultIsArgumentTypeRegisterCompatible(Type* type);
17984-
virtual bool IsNonRegisterArgumentIndirect(Type* type);
17984+
virtual bool IsNonRegisterArgumentIndirect(BinaryView* view, Type* type);
1798517985
virtual bool AreStackArgumentsNaturallyAligned();
1798617986
virtual bool AreStackArgumentsPushedLeftToRight();
1798717987

17988-
virtual CallLayout GetCallLayout(const ReturnValue& returnValue, const std::vector<FunctionParameter>& params,
17989-
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
17990-
virtual ValueLocation GetReturnValueLocation(const ReturnValue& returnValue);
17991-
virtual std::vector<ValueLocation> GetParameterLocations(const std::optional<ValueLocation>& returnValue,
17988+
virtual CallLayout GetCallLayout(BinaryView* view, const ReturnValue& returnValue,
1799217989
const std::vector<FunctionParameter>& params,
1799317990
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
17994-
virtual std::vector<Variable> GetParameterOrderingForVariables(const std::map<Variable, Ref<Type>>& params);
17995-
virtual int64_t GetStackAdjustmentForLocations(const std::optional<ValueLocation>& returnValue,
17996-
const std::vector<ValueLocation>& locations, const std::vector<Ref<Type>>& types);
17997-
virtual std::map<uint32_t, int32_t> GetRegisterStackAdjustments(
17991+
virtual ValueLocation GetReturnValueLocation(BinaryView* view, const ReturnValue& returnValue);
17992+
virtual std::vector<ValueLocation> GetParameterLocations(BinaryView* view,
17993+
const std::optional<ValueLocation>& returnValue, const std::vector<FunctionParameter>& params,
17994+
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
17995+
virtual std::vector<Variable> GetParameterOrderingForVariables(
17996+
BinaryView* view, const std::map<Variable, Ref<Type>>& params);
17997+
virtual int64_t GetStackAdjustmentForLocations(BinaryView* view,
17998+
const std::optional<ValueLocation>& returnValue, const std::vector<ValueLocation>& locations,
17999+
const std::vector<Ref<Type>>& types);
18000+
virtual std::map<uint32_t, int32_t> GetRegisterStackAdjustments(BinaryView* view,
1799818001
const std::optional<ValueLocation>& returnValue, const std::vector<ValueLocation>& params);
1799918002

18000-
CallLayout GetDefaultCallLayout(const ReturnValue& returnValue, const std::vector<FunctionParameter>& params,
18001-
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
18002-
ValueLocation GetDefaultReturnValueLocation(const ReturnValue& returnValue);
18003-
std::vector<ValueLocation> GetDefaultParameterLocations(const std::optional<ValueLocation>& returnValue,
18003+
CallLayout GetDefaultCallLayout(BinaryView* view, const ReturnValue& returnValue,
1800418004
const std::vector<FunctionParameter>& params,
1800518005
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
18006+
ValueLocation GetDefaultReturnValueLocation(BinaryView* view, const ReturnValue& returnValue);
18007+
std::vector<ValueLocation> GetDefaultParameterLocations(BinaryView* view,
18008+
const std::optional<ValueLocation>& returnValue, const std::vector<FunctionParameter>& params,
18009+
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt);
1800618010
std::vector<Variable> GetDefaultParameterOrderingForVariables(const std::map<Variable, Ref<Type>>& params);
1800718011
int64_t GetDefaultStackAdjustmentForLocations(const std::optional<ValueLocation>& returnValue,
1800818012
const std::vector<ValueLocation>& locations, const std::vector<Ref<Type>>& types);
@@ -18043,26 +18047,28 @@ namespace BinaryNinja {
1804318047
virtual Variable GetIncomingVariableForParameterVariable(const Variable& var, Function* func) override;
1804418048
virtual Variable GetParameterVariableForIncomingVariable(const Variable& var, Function* func) override;
1804518049

18046-
virtual bool IsReturnTypeRegisterCompatible(Type* type) override;
18050+
virtual bool IsReturnTypeRegisterCompatible(BinaryView* view, Type* type) override;
1804718051
virtual Variable GetIndirectReturnValueLocation() override;
1804818052
virtual std::optional<Variable> GetReturnedIndirectReturnValuePointer() override;
1804918053

18050-
virtual bool IsArgumentTypeRegisterCompatible(Type* type) override;
18051-
virtual bool IsNonRegisterArgumentIndirect(Type* type) override;
18054+
virtual bool IsArgumentTypeRegisterCompatible(BinaryView* view, Type* type) override;
18055+
virtual bool IsNonRegisterArgumentIndirect(BinaryView* view, Type* type) override;
1805218056
virtual bool AreStackArgumentsNaturallyAligned() override;
1805318057
virtual bool AreStackArgumentsPushedLeftToRight() override;
1805418058

18055-
virtual CallLayout GetCallLayout(const ReturnValue& returnValue, const std::vector<FunctionParameter>& params,
18056-
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt) override;
18057-
virtual ValueLocation GetReturnValueLocation(const ReturnValue& returnValue) override;
18058-
virtual std::vector<ValueLocation> GetParameterLocations(const std::optional<ValueLocation>& returnValue,
18059+
virtual CallLayout GetCallLayout(BinaryView* view, const ReturnValue& returnValue,
1805918060
const std::vector<FunctionParameter>& params,
1806018061
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt) override;
18062+
virtual ValueLocation GetReturnValueLocation(BinaryView* view, const ReturnValue& returnValue) override;
18063+
virtual std::vector<ValueLocation> GetParameterLocations(BinaryView* view,
18064+
const std::optional<ValueLocation>& returnValue, const std::vector<FunctionParameter>& params,
18065+
const std::optional<std::set<uint32_t>>& permittedRegs = std::nullopt) override;
1806118066
virtual std::vector<Variable> GetParameterOrderingForVariables(
18062-
const std::map<Variable, Ref<Type>>& params) override;
18063-
virtual int64_t GetStackAdjustmentForLocations(const std::optional<ValueLocation>& returnValue,
18064-
const std::vector<ValueLocation>& locations, const std::vector<Ref<Type>>& types) override;
18065-
virtual std::map<uint32_t, int32_t> GetRegisterStackAdjustments(
18067+
BinaryView* view, const std::map<Variable, Ref<Type>>& params) override;
18068+
virtual int64_t GetStackAdjustmentForLocations(BinaryView* view,
18069+
const std::optional<ValueLocation>& returnValue, const std::vector<ValueLocation>& locations,
18070+
const std::vector<Ref<Type>>& types) override;
18071+
virtual std::map<uint32_t, int32_t> GetRegisterStackAdjustments(BinaryView* view,
1806618072
const std::optional<ValueLocation>& returnValue, const std::vector<ValueLocation>& params) override;
1806718073
};
1806818074

0 commit comments

Comments
 (0)