Skip to content

Commit 4f134f7

Browse files
committed
Add API to get all type references at the same time to avoid duplicating work
1 parent 2b3ce70 commit 4f134f7

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

binaryninjaapi.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4554,6 +4554,13 @@ namespace BinaryNinja {
45544554
BNRegisterValue ToAPIObject();
45554555
};
45564556

4557+
struct AllTypeReferences
4558+
{
4559+
std::vector<ReferenceSource> codeRefs;
4560+
std::vector<uint64_t> dataRefs;
4561+
std::vector<TypeReferenceSource> typeRefs;
4562+
};
4563+
45574564
struct AllTypeFieldReferences
45584565
{
45594566
std::vector<TypeFieldReference> codeRefs;
@@ -5654,6 +5661,13 @@ namespace BinaryNinja {
56545661
*/
56555662
std::vector<TypeReferenceSource> GetTypeReferencesForTypeField(const QualifiedName& type, uint64_t offset);
56565663

5664+
/*! Returns a all references to a specific type. This includes code, data, and type references.
5665+
5666+
\param type QualifiedName of the type
5667+
\return AllTypeReferences structure with all references
5668+
*/
5669+
AllTypeReferences GetAllReferencesForType(const QualifiedName& type);
5670+
56575671
/*! Returns a all references to a specific type field. This includes code, data, and type references.
56585672

56595673
\param type QualifiedName of the type

binaryninjacore.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3732,6 +3732,16 @@ extern "C"
37323732
bool higherToLowerDirect;
37333733
} BNExprMapInfo;
37343734

3735+
typedef struct BNAllTypeReferences
3736+
{
3737+
BNReferenceSource* codeRefs;
3738+
size_t codeRefCount;
3739+
uint64_t* dataRefs;
3740+
size_t dataRefCount;
3741+
BNTypeReferenceSource* typeRefs;
3742+
size_t typeRefCount;
3743+
} BNAllTypeReferences;
3744+
37353745
typedef struct BNAllTypeFieldReferences
37363746
{
37373747
BNTypeFieldReference* codeRefs;
@@ -5063,6 +5073,8 @@ extern "C"
50635073
BINARYNINJACOREAPI BNTypeReferenceSource* BNGetTypeReferencesForTypeField(
50645074
BNBinaryView* view, BNQualifiedName* type, uint64_t offset, size_t* count);
50655075

5076+
BINARYNINJACOREAPI BNAllTypeReferences BNGetAllReferencesForType(BNBinaryView* view, BNQualifiedName* type);
5077+
BINARYNINJACOREAPI void BNFreeAllTypeReferences(BNAllTypeReferences* refs);
50665078
BINARYNINJACOREAPI BNAllTypeFieldReferences BNGetAllReferencesForTypeField(
50675079
BNBinaryView* view, BNQualifiedName* type, uint64_t offset);
50685080
BINARYNINJACOREAPI void BNFreeAllTypeFieldReferences(BNAllTypeFieldReferences* refs);

binaryview.cpp

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

26872687

2688+
AllTypeReferences BinaryView::GetAllReferencesForType(const QualifiedName& type)
2689+
{
2690+
BNQualifiedName nameObj = type.GetAPIObject();
2691+
BNAllTypeReferences refs = BNGetAllReferencesForType(m_object, &nameObj);
2692+
QualifiedName::FreeAPIObject(&nameObj);
2693+
2694+
AllTypeReferences result;
2695+
2696+
result.codeRefs.reserve(refs.codeRefCount);
2697+
for (size_t i = 0; i < refs.codeRefCount; i++)
2698+
{
2699+
ReferenceSource 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+
result.codeRefs.push_back(src);
2704+
}
2705+
2706+
result.dataRefs = vector<uint64_t>(refs.dataRefs, &refs.dataRefs[refs.dataRefCount]);
2707+
2708+
result.typeRefs.reserve(refs.typeRefCount);
2709+
for (size_t i = 0; i < refs.typeRefCount; i++)
2710+
{
2711+
TypeReferenceSource src;
2712+
src.name = QualifiedName::FromAPIObject(&refs.typeRefs[i].name);
2713+
src.offset = refs.typeRefs[i].offset;
2714+
src.type = refs.typeRefs[i].type;
2715+
result.typeRefs.push_back(src);
2716+
}
2717+
2718+
BNFreeAllTypeReferences(&refs);
2719+
return result;
2720+
}
2721+
2722+
26882723
AllTypeFieldReferences BinaryView::GetAllReferencesForTypeField(const QualifiedName& type, uint64_t offset)
26892724
{
26902725
BNQualifiedName nameObj = type.GetAPIObject();

0 commit comments

Comments
 (0)