Skip to content

Commit 2cd19b8

Browse files
committed
Add API to get all type field references at the same time to avoid duplicating work
1 parent b4918a5 commit 2cd19b8

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

binaryninjaapi.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,6 +4546,14 @@ namespace BinaryNinja {
45464546
BNRegisterValue ToAPIObject();
45474547
};
45484548

4549+
struct AllTypeFieldReferences
4550+
{
4551+
std::vector<TypeFieldReference> codeRefs;
4552+
std::vector<uint64_t> dataRefsTo;
4553+
std::vector<uint64_t> dataRefsFrom;
4554+
std::vector<TypeReferenceSource> typeRefs;
4555+
};
4556+
45494557
struct QualifiedNameAndType;
45504558
struct PossibleValueSet;
45514559
class Metadata;
@@ -5638,13 +5646,21 @@ namespace BinaryNinja {
56385646
*/
56395647
std::vector<TypeReferenceSource> GetTypeReferencesForTypeField(const QualifiedName& type, uint64_t offset);
56405648

5649+
/*! Returns a all references to a specific type field. This includes code, data, and type references.
5650+
5651+
\param type QualifiedName of the type
5652+
\param offset Offset of the field, relative to the start of the type
5653+
\return AllTypeFieldReferences structure with all references
5654+
*/
5655+
AllTypeFieldReferences GetAllReferencesForTypeField(const QualifiedName& type, uint64_t offset);
5656+
56415657
/*! Returns a list of types referenced by code at ReferenceSource \c src
56425658

5643-
If no function is specified, references from all functions and containing the address will be returned.
5644-
If no architecture is specified, the architecture of the function will be used.
5659+
If no function is specified, references from all functions and containing the address will be returned.
5660+
If no architecture is specified, the architecture of the function will be used.
56455661

5646-
\param src Source of the reference to check
5647-
\return vector of TypeReferenceSources
5662+
\param src Source of the reference to check
5663+
\return vector of TypeReferenceSources
56485664
*/
56495665
std::vector<TypeReferenceSource> GetCodeReferencesForTypeFrom(ReferenceSource src);
56505666

binaryninjacore.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// Current ABI version for linking to the core. This is incremented any time
3838
// there are changes to the API that affect linking, including new functions,
3939
// new types, or modifications to existing functions or types.
40-
#define BN_CURRENT_CORE_ABI_VERSION 118
40+
#define BN_CURRENT_CORE_ABI_VERSION 119
4141

4242
// Minimum ABI version that is supported for loading of plugins. Plugins that
4343
// are linked to an ABI version less than this will not be able to load and
@@ -3728,6 +3728,18 @@ extern "C"
37283728
bool higherToLowerDirect;
37293729
} BNExprMapInfo;
37303730

3731+
typedef struct BNAllTypeFieldReferences
3732+
{
3733+
BNTypeFieldReference* codeRefs;
3734+
size_t codeRefCount;
3735+
uint64_t* dataRefsTo;
3736+
size_t dataRefToCount;
3737+
uint64_t* dataRefsFrom;
3738+
size_t dataRefFromCount;
3739+
BNTypeReferenceSource* typeRefs;
3740+
size_t typeRefCount;
3741+
} BNAllTypeFieldReferences;
3742+
37313743
BINARYNINJACOREAPI char* BNAllocString(const char* contents);
37323744
BINARYNINJACOREAPI char* BNAllocStringWithLength(const char* contents, size_t len);
37333745
BINARYNINJACOREAPI void BNFreeString(char* str);
@@ -5043,6 +5055,10 @@ extern "C"
50435055
BINARYNINJACOREAPI BNTypeReferenceSource* BNGetTypeReferencesForTypeField(
50445056
BNBinaryView* view, BNQualifiedName* type, uint64_t offset, size_t* count);
50455057

5058+
BINARYNINJACOREAPI BNAllTypeFieldReferences BNGetAllReferencesForTypeField(
5059+
BNBinaryView* view, BNQualifiedName* type, uint64_t offset);
5060+
BINARYNINJACOREAPI void BNFreeAllTypeFieldReferences(BNAllTypeFieldReferences* refs);
5061+
50465062
BINARYNINJACOREAPI BNTypeReferenceSource* BNGetCodeReferencesForTypeFrom(
50475063
BNBinaryView* view, BNReferenceSource* addr, size_t* count);
50485064
BINARYNINJACOREAPI BNTypeReferenceSource* BNGetCodeReferencesForTypeFromInRange(

binaryview.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,6 +2685,46 @@ vector<TypeReferenceSource> BinaryView::GetTypeReferencesForTypeField(const Qual
26852685
}
26862686

26872687

2688+
AllTypeFieldReferences BinaryView::GetAllReferencesForTypeField(const QualifiedName& type, uint64_t offset)
2689+
{
2690+
BNQualifiedName nameObj = type.GetAPIObject();
2691+
BNAllTypeFieldReferences refs = BNGetAllReferencesForTypeField(m_object, &nameObj, offset);
2692+
QualifiedName::FreeAPIObject(&nameObj);
2693+
2694+
AllTypeFieldReferences result;
2695+
2696+
result.codeRefs.reserve(refs.codeRefCount);
2697+
for (size_t i = 0; i < refs.codeRefCount; i++)
2698+
{
2699+
TypeFieldReference src;
2700+
src.func = new Function(BNNewFunctionReference(refs.codeRefs[i].func));
2701+
src.arch = new CoreArchitecture(refs.codeRefs[i].arch);
2702+
src.addr = refs.codeRefs[i].addr;
2703+
src.size = refs.codeRefs[i].size;
2704+
BNTypeWithConfidence& tc = refs.codeRefs[i].incomingType;
2705+
Ref<Type> type = tc.type ? new Type(BNNewTypeReference(tc.type)) : nullptr;
2706+
src.incomingType = Confidence<Ref<Type>>(type, tc.confidence);
2707+
result.codeRefs.push_back(src);
2708+
}
2709+
2710+
result.dataRefsTo = vector<uint64_t>(refs.dataRefsTo, &refs.dataRefsTo[refs.dataRefToCount]);
2711+
result.dataRefsFrom = vector<uint64_t>(refs.dataRefsFrom, &refs.dataRefsFrom[refs.dataRefFromCount]);
2712+
2713+
result.typeRefs.reserve(refs.typeRefCount);
2714+
for (size_t i = 0; i < refs.typeRefCount; i++)
2715+
{
2716+
TypeReferenceSource src;
2717+
src.name = QualifiedName::FromAPIObject(&refs.typeRefs[i].name);
2718+
src.offset = refs.typeRefs[i].offset;
2719+
src.type = refs.typeRefs[i].type;
2720+
result.typeRefs.push_back(src);
2721+
}
2722+
2723+
BNFreeAllTypeFieldReferences(&refs);
2724+
return result;
2725+
}
2726+
2727+
26882728
vector<TypeReferenceSource> BinaryView::GetCodeReferencesForTypeFrom(ReferenceSource src)
26892729
{
26902730
size_t count;

0 commit comments

Comments
 (0)