diff options
| author | Glenn Smith <glenn@vector35.com> | 2023-03-15 20:51:48 -0400 |
|---|---|---|
| committer | Glenn Smith <glenn@vector35.com> | 2023-03-29 20:34:14 -0400 |
| commit | 7977801957364139464406602038722124434273 (patch) | |
| tree | 6ddcaba6992061f10932688a0dddafb8020953d4 | |
| parent | 3911076326059945990138870942693848884934 (diff) | |
BinaryView::LookupImportedType*
| -rw-r--r-- | binaryninjaapi.h | 16 | ||||
| -rw-r--r-- | binaryninjacore.h | 3 | ||||
| -rw-r--r-- | binaryview.cpp | 30 | ||||
| -rw-r--r-- | python/binaryview.py | 44 |
4 files changed, 92 insertions, 1 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 98790d37..92898ae4 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -4560,6 +4560,14 @@ namespace BinaryNinja { void RegisterPlatformTypes(Platform* platform); + /*! Gives you details of which platform and name was imported to result in the given type name. + + \param name Name of type in the binary view + \return A pair with the platform and the name of the type in the platform, + or std::nullopt if it was not imported + */ + std::optional<std::pair<Ref<Platform>, QualifiedName>> LookupImportedTypePlatform(const QualifiedName& name); + /*! Make the contents of a type library available for type/import resolution \param lib library to register with the view @@ -4645,6 +4653,14 @@ namespace BinaryNinja { */ std::optional<std::pair<Ref<TypeLibrary>, QualifiedName>> LookupImportedObjectLibrary(Platform* tgtPlatform, uint64_t tgtAddr); + /*! Gives you details of which type library and name was imported to result in the given type name. + + \param name Name of type in the binary view + \return A pair with the library and the name of the type in the library, + or std::nullopt if it was not imported + */ + std::optional<std::pair<Ref<TypeLibrary>, QualifiedName>> LookupImportedTypeLibrary(const QualifiedName& name); + bool FindNextData( uint64_t start, const DataBuffer& data, uint64_t& result, BNFindFlag flags = FindCaseSensitive); bool FindNextText(uint64_t start, const std::string& data, uint64_t& result, Ref<DisassemblySettings> settings, diff --git a/binaryninjacore.h b/binaryninjacore.h index d0d2955b..3e0f4f06 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -4357,6 +4357,7 @@ extern "C" BINARYNINJACOREAPI char* BNGetAutoDebugTypeIdSource(void); BINARYNINJACOREAPI void BNRegisterPlatformTypes(BNBinaryView* view, BNPlatform* platform); + BINARYNINJACOREAPI bool BNLookupImportedTypePlatform(BNBinaryView* view, const BNQualifiedName* typeName, BNPlatform** platform, BNQualifiedName* resultName); BINARYNINJACOREAPI void BNReanalyzeAllFunctions(BNBinaryView* view); BINARYNINJACOREAPI void BNReanalyzeFunction(BNFunction* func, BNFunctionUpdateType type); @@ -5250,6 +5251,8 @@ extern "C" BNBinaryView* view, BNPlatform* tgtPlatform, uint64_t tgtAddr, BNTypeLibrary* lib, BNQualifiedName* name); BINARYNINJACOREAPI bool BNBinaryViewLookupImportedObjectLibrary( BNBinaryView* view, BNPlatform* tgtPlatform, uint64_t tgtAddr, BNTypeLibrary** lib, BNQualifiedName* name); + BINARYNINJACOREAPI bool BNBinaryViewLookupImportedTypeLibrary( + BNBinaryView* view, const BNQualifiedName* typeName, BNTypeLibrary** lib, BNQualifiedName* resultName); // Language Representation BINARYNINJACOREAPI BNLanguageRepresentationFunction* BNCreateLanguageRepresentationFunction( diff --git a/binaryview.cpp b/binaryview.cpp index eeef5a74..67fe011c 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -3731,6 +3731,21 @@ void BinaryView::RegisterPlatformTypes(Platform* platform) } +std::optional<std::pair<Ref<Platform>, QualifiedName>> BinaryView::LookupImportedTypePlatform(const QualifiedName& name) +{ + BNPlatform* resultLib; + BNQualifiedName resultName; + BNQualifiedName sourceName = name.GetAPIObject(); + bool result = BNLookupImportedTypePlatform(m_object, &sourceName, &resultLib, &resultName); + QualifiedName::FreeAPIObject(&sourceName); + if (!result) + return std::nullopt; + QualifiedName targetName = QualifiedName::FromAPIObject(&resultName); + BNFreeQualifiedName(&resultName); + return std::make_pair(new Platform(resultLib), targetName); +} + + void BinaryView::AddTypeLibrary(TypeLibrary* lib) { BNAddBinaryViewTypeLibrary(m_object, lib->GetObject()); @@ -3824,6 +3839,21 @@ std::optional<std::pair<Ref<TypeLibrary>, QualifiedName>> BinaryView::LookupImpo } +std::optional<std::pair<Ref<TypeLibrary>, QualifiedName>> BinaryView::LookupImportedTypeLibrary(const QualifiedName& name) +{ + BNTypeLibrary* resultLib; + BNQualifiedName resultName; + BNQualifiedName sourceName = name.GetAPIObject(); + bool result = BNBinaryViewLookupImportedTypeLibrary(m_object, &sourceName, &resultLib, &resultName); + QualifiedName::FreeAPIObject(&sourceName); + if (!result) + return std::nullopt; + QualifiedName targetName = QualifiedName::FromAPIObject(&resultName); + BNFreeQualifiedName(&resultName); + return std::make_pair(new TypeLibrary(resultLib), targetName); +} + + bool BinaryView::FindNextData(uint64_t start, const DataBuffer& data, uint64_t& result, BNFindFlag flags) { return BNFindNextData(m_object, start, data.GetBufferObject(), &result, flags); diff --git a/python/binaryview.py b/python/binaryview.py index ba007e9c..d25f8e9e 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -7176,7 +7176,7 @@ class BinaryView: def lookup_imported_object_library( self, addr: int, platform: Optional['_platform.Platform'] = None - ) -> Optional[Tuple[typelibrary.TypeLibrary, '_types.QualifiedNameType']]: + ) -> Optional[Tuple[typelibrary.TypeLibrary, '_types.QualifiedName']]: """ ``lookup_imported_object_library`` gives you details of which type library and name was used to determine the type of a symbol at a given address @@ -7199,6 +7199,27 @@ class BinaryView: core.BNFreeQualifiedName(result_name) return lib, name + def lookup_imported_type_library( + self, name: '_types.QualifiedNameType' + ) -> Optional[Tuple[typelibrary.TypeLibrary, '_types.QualifiedName']]: + """ + ``lookup_imported_type_library`` gives you details of from which type library and name + a given type in the analysis was imported. + + :param name: Name of type in analysis + :return: A tuple of [TypeLibrary, QualifiedName] with the library and name used, or None if it was not imported + :rtype: Optional[Tuple[TypeLibrary, QualifiedName]] + """ + name = _types.QualifiedName(name) + result_lib = (ctypes.POINTER(core.BNTypeLibrary) * 1)() + result_name = (core.BNQualifiedName * 1)() + if not core.BNBinaryViewLookupImportedTypeLibrary(self.handle, name._to_core_struct(), result_lib, result_name): + return None + lib = typelibrary.TypeLibrary(result_lib[0]) + name = _types.QualifiedName._from_core_struct(result_name[0]) + core.BNFreeQualifiedName(result_name) + return lib, name + def register_platform_types(self, platform: '_platform.Platform') -> None: """ ``register_platform_types`` ensures that the platform-specific types for a :py:class:`Platform` are available @@ -7215,6 +7236,27 @@ class BinaryView: """ core.BNRegisterPlatformTypes(self.handle, platform.handle) + def lookup_imported_type_platform( + self, name: '_types.QualifiedNameType' + ) -> Optional[Tuple['_platform.Platform', '_types.QualifiedName']]: + """ + ``lookup_imported_type_platform`` gives you details of from which platform and name + a given type in the analysis was imported. + + :param name: Name of type in analysis + :return: A tuple of [Platform, QualifiedName] with the platform and name used, or None if it was not imported + :rtype: Optional[Tuple[Platform, QualifiedName]] + """ + name = _types.QualifiedName(name) + result_platform = (ctypes.POINTER(core.BNPlatform) * 1)() + result_name = (core.BNQualifiedName * 1)() + if not core.BNLookupImportedTypePlatform(self.handle, name._to_core_struct(), result_platform, result_name): + return None + platform = _platform.Platform(arch=self.arch, handle=result_platform[0]) + name = _types.QualifiedName._from_core_struct(result_name[0]) + core.BNFreeQualifiedName(result_name) + return platform, name + def find_next_data(self, start: int, data: bytes, flags: FindFlag = FindFlag.FindCaseSensitive) -> Optional[int]: """ ``find_next_data`` searches for the bytes ``data`` starting at the virtual address ``start`` until the end of the BinaryView. |
