diff options
| author | Rusty Wagner <rusty.wagner@gmail.com> | 2025-07-02 18:53:31 -0400 |
|---|---|---|
| committer | Rusty Wagner <rusty.wagner@gmail.com> | 2025-07-03 12:44:56 -0400 |
| commit | 2cd19b867772f15d87eaee9560b94a9fd9ba13e4 (patch) | |
| tree | 00fc16b815a0c1176e96b388e9c11cb7621391a8 | |
| parent | b4918a512bef271d7f6e27980ea875208c7f2954 (diff) | |
Add API to get all type field references at the same time to avoid duplicating work
| -rw-r--r-- | binaryninjaapi.h | 24 | ||||
| -rw-r--r-- | binaryninjacore.h | 18 | ||||
| -rw-r--r-- | binaryview.cpp | 40 |
3 files changed, 77 insertions, 5 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index edac64b8..66f40fac 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -4546,6 +4546,14 @@ namespace BinaryNinja { BNRegisterValue ToAPIObject(); }; + struct AllTypeFieldReferences + { + std::vector<TypeFieldReference> codeRefs; + std::vector<uint64_t> dataRefsTo; + std::vector<uint64_t> dataRefsFrom; + std::vector<TypeReferenceSource> typeRefs; + }; + struct QualifiedNameAndType; struct PossibleValueSet; class Metadata; @@ -5638,13 +5646,21 @@ namespace BinaryNinja { */ std::vector<TypeReferenceSource> GetTypeReferencesForTypeField(const QualifiedName& type, uint64_t offset); + /*! Returns a all references to a specific type field. This includes code, data, and type references. + + \param type QualifiedName of the type + \param offset Offset of the field, relative to the start of the type + \return AllTypeFieldReferences structure with all references + */ + AllTypeFieldReferences GetAllReferencesForTypeField(const QualifiedName& type, uint64_t offset); + /*! Returns a list of types referenced by code at ReferenceSource \c src - If no function is specified, references from all functions and containing the address will be returned. - If no architecture is specified, the architecture of the function will be used. + If no function is specified, references from all functions and containing the address will be returned. + If no architecture is specified, the architecture of the function will be used. - \param src Source of the reference to check - \return vector of TypeReferenceSources + \param src Source of the reference to check + \return vector of TypeReferenceSources */ std::vector<TypeReferenceSource> GetCodeReferencesForTypeFrom(ReferenceSource src); diff --git a/binaryninjacore.h b/binaryninjacore.h index b65dac73..215cd6b2 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -37,7 +37,7 @@ // Current ABI version for linking to the core. This is incremented any time // there are changes to the API that affect linking, including new functions, // new types, or modifications to existing functions or types. -#define BN_CURRENT_CORE_ABI_VERSION 118 +#define BN_CURRENT_CORE_ABI_VERSION 119 // Minimum ABI version that is supported for loading of plugins. Plugins that // are linked to an ABI version less than this will not be able to load and @@ -3728,6 +3728,18 @@ extern "C" bool higherToLowerDirect; } BNExprMapInfo; + typedef struct BNAllTypeFieldReferences + { + BNTypeFieldReference* codeRefs; + size_t codeRefCount; + uint64_t* dataRefsTo; + size_t dataRefToCount; + uint64_t* dataRefsFrom; + size_t dataRefFromCount; + BNTypeReferenceSource* typeRefs; + size_t typeRefCount; + } BNAllTypeFieldReferences; + BINARYNINJACOREAPI char* BNAllocString(const char* contents); BINARYNINJACOREAPI char* BNAllocStringWithLength(const char* contents, size_t len); BINARYNINJACOREAPI void BNFreeString(char* str); @@ -5043,6 +5055,10 @@ extern "C" BINARYNINJACOREAPI BNTypeReferenceSource* BNGetTypeReferencesForTypeField( BNBinaryView* view, BNQualifiedName* type, uint64_t offset, size_t* count); + BINARYNINJACOREAPI BNAllTypeFieldReferences BNGetAllReferencesForTypeField( + BNBinaryView* view, BNQualifiedName* type, uint64_t offset); + BINARYNINJACOREAPI void BNFreeAllTypeFieldReferences(BNAllTypeFieldReferences* refs); + BINARYNINJACOREAPI BNTypeReferenceSource* BNGetCodeReferencesForTypeFrom( BNBinaryView* view, BNReferenceSource* addr, size_t* count); BINARYNINJACOREAPI BNTypeReferenceSource* BNGetCodeReferencesForTypeFromInRange( diff --git a/binaryview.cpp b/binaryview.cpp index 598f666c..6585f5c4 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -2685,6 +2685,46 @@ vector<TypeReferenceSource> BinaryView::GetTypeReferencesForTypeField(const Qual } +AllTypeFieldReferences BinaryView::GetAllReferencesForTypeField(const QualifiedName& type, uint64_t offset) +{ + BNQualifiedName nameObj = type.GetAPIObject(); + BNAllTypeFieldReferences refs = BNGetAllReferencesForTypeField(m_object, &nameObj, offset); + QualifiedName::FreeAPIObject(&nameObj); + + AllTypeFieldReferences result; + + result.codeRefs.reserve(refs.codeRefCount); + for (size_t i = 0; i < refs.codeRefCount; i++) + { + TypeFieldReference src; + src.func = new Function(BNNewFunctionReference(refs.codeRefs[i].func)); + src.arch = new CoreArchitecture(refs.codeRefs[i].arch); + src.addr = refs.codeRefs[i].addr; + src.size = refs.codeRefs[i].size; + BNTypeWithConfidence& tc = refs.codeRefs[i].incomingType; + Ref<Type> type = tc.type ? new Type(BNNewTypeReference(tc.type)) : nullptr; + src.incomingType = Confidence<Ref<Type>>(type, tc.confidence); + result.codeRefs.push_back(src); + } + + result.dataRefsTo = vector<uint64_t>(refs.dataRefsTo, &refs.dataRefsTo[refs.dataRefToCount]); + result.dataRefsFrom = vector<uint64_t>(refs.dataRefsFrom, &refs.dataRefsFrom[refs.dataRefFromCount]); + + result.typeRefs.reserve(refs.typeRefCount); + for (size_t i = 0; i < refs.typeRefCount; i++) + { + TypeReferenceSource src; + src.name = QualifiedName::FromAPIObject(&refs.typeRefs[i].name); + src.offset = refs.typeRefs[i].offset; + src.type = refs.typeRefs[i].type; + result.typeRefs.push_back(src); + } + + BNFreeAllTypeFieldReferences(&refs); + return result; +} + + vector<TypeReferenceSource> BinaryView::GetCodeReferencesForTypeFrom(ReferenceSource src) { size_t count; |
