diff options
| author | Peter LaFosse <peter@vector35.com> | 2017-05-14 09:41:28 -0400 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2017-05-14 10:11:50 -0400 |
| commit | 2111f1f77241c938b30bd3cf2851919094429dd6 (patch) | |
| tree | a0201812b8409528b64549295b1fed6bad19e93b | |
| parent | 12d16ae11dd2c0524a36c59c5b025c38edb4135b (diff) | |
Expose some core API's for the demangling and type creation
| -rw-r--r-- | binaryninjaapi.h | 20 | ||||
| -rw-r--r-- | binaryninjacore.h | 16 | ||||
| -rw-r--r-- | demangle.cpp | 16 | ||||
| -rw-r--r-- | python/demangle.py | 4 | ||||
| -rw-r--r-- | type.cpp | 99 |
5 files changed, 148 insertions, 7 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index b5ca60b4..5fb78367 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -407,6 +407,10 @@ namespace BinaryNinja const std::string& mangledName, Type** outType, QualifiedName& outVarName); + bool DemangleGNU3(Architecture* arch, + const std::string& mangledName, + Type** outType, + QualifiedName& outVarName); void RegisterMainThread(MainThreadActionHandler* handler); Ref<MainThreadAction> ExecuteOnMainThread(const std::function<void()>& action); @@ -1624,6 +1628,7 @@ namespace BinaryNinja BNTypeClass GetClass() const; uint64_t GetWidth() const; size_t GetAlignment() const; + QualifiedName GetTypeName() const; bool IsSigned() const; bool IsConst() const; bool IsVolatile() const; @@ -1636,6 +1641,13 @@ namespace BinaryNinja Ref<Structure> GetStructure() const; Ref<Enumeration> GetEnumeration() const; Ref<NamedTypeReference> GetNamedTypeReference() const; + BNMemberScope GetScope() const; + void SetScope(BNMemberScope scope); + BNMemberAccess GetAccess() const; + void SetAccess(BNMemberAccess access); + void SetConst(bool cnst); + void SetVolatile(bool vltl); + void SetTypeName(const QualifiedName& name); uint64_t GetElementCount() const; @@ -1664,6 +1676,8 @@ namespace BinaryNinja static Ref<Type> EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0, bool issigned = false); static Ref<Type> PointerType(Architecture* arch, Type* type, bool cnst = false, bool vltl = false, BNReferenceType refType = PointerReferenceType); + static Ref<Type> PointerType(size_t width, Type* type, bool cnst = false, bool vltl = false, + BNReferenceType refType = PointerReferenceType); static Ref<Type> ArrayType(Type* type, uint64_t elem); static Ref<Type> FunctionType(Type* returnValue, CallingConvention* callingConvention, const std::vector<NameAndType>& params, bool varArg = false); @@ -1671,6 +1685,8 @@ namespace BinaryNinja static std::string GenerateAutoTypeId(const std::string& source, const QualifiedName& name); static std::string GenerateAutoDemangledTypeId(const QualifiedName& name); static std::string GetAutoDemangledTypeIdSource(); + static std::string GenerateAutoDebugTypeId(const QualifiedName& name); + static std::string GetAutoDebugTypeIdSource(); }; class NamedTypeReference: public CoreRefCountObject<BNNamedTypeReference, BNNewNamedTypeReference, @@ -1691,6 +1707,8 @@ namespace BinaryNinja const std::string& source, const QualifiedName& name); static Ref<NamedTypeReference> GenerateAutoDemangledTypeReference(BNNamedTypeReferenceClass cls, const QualifiedName& name); + static Ref<NamedTypeReference> GenerateAutoDebugTypeReference(BNNamedTypeReferenceClass cls, + const QualifiedName& name); }; struct StructureMember @@ -1705,6 +1723,7 @@ namespace BinaryNinja public: Structure(); Structure(BNStructure* s); + Structure(BNStructureType type, bool isUnion = false, bool packed = false); std::vector<StructureMember> GetMembers() const; uint64_t GetWidth() const; @@ -1732,6 +1751,7 @@ namespace BinaryNinja class Enumeration: public CoreRefCountObject<BNEnumeration, BNNewEnumerationReference, BNFreeEnumeration> { public: + Enumeration(); Enumeration(BNEnumeration* e); std::vector<EnumerationMember> GetMembers() const; diff --git a/binaryninjacore.h b/binaryninjacore.h index 8838ca97..c6a79cf5 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -457,7 +457,8 @@ extern "C" NoScope, StaticScope, VirtualScope, - ThunkScope + ThunkScope, + FriendScope }; enum BNMemberAccess @@ -2097,6 +2098,8 @@ extern "C" BINARYNINJACOREAPI char* BNGenerateAutoDemangledTypeId(BNQualifiedName* name); BINARYNINJACOREAPI char* BNGetAutoPlatformTypeIdSource(BNPlatform* platform); BINARYNINJACOREAPI char* BNGetAutoDemangledTypeIdSource(void); + BINARYNINJACOREAPI char* BNGenerateAutoDebugTypeId(BNQualifiedName* name); + BINARYNINJACOREAPI char* BNGetAutoDebugTypeIdSource(void); BINARYNINJACOREAPI void BNRegisterPlatformTypes(BNBinaryView* view, BNPlatform* platform); @@ -2428,6 +2431,8 @@ extern "C" BINARYNINJACOREAPI BNType* BNCreateEnumerationType(BNArchitecture* arch, BNEnumeration* e, size_t width, bool isSigned); BINARYNINJACOREAPI BNType* BNCreatePointerType(BNArchitecture* arch, BNType* type, bool cnst, bool vltl, BNReferenceType refType); + BINARYNINJACOREAPI BNType* BNCreatePointerTypeOfWidth(size_t width, BNType* type, bool cnst, bool vltl, + BNReferenceType refType); BINARYNINJACOREAPI BNType* BNCreateArrayType(BNType* type, uint64_t elem); BINARYNINJACOREAPI BNType* BNCreateFunctionType(BNType* returnValue, BNCallingConvention* callingConvention, BNNameAndType* params, size_t paramCount, bool varArg); @@ -2436,6 +2441,8 @@ extern "C" BINARYNINJACOREAPI char* BNGetTypeAndName(BNType* type, BNQualifiedName* name); BINARYNINJACOREAPI void BNFreeType(BNType* type); + BINARYNINJACOREAPI BNQualifiedName BNTypeGetTypeName(BNType* nt); + BINARYNINJACOREAPI void BNTypeSetTypeName(BNType* type, BNQualifiedName* name); BINARYNINJACOREAPI BNTypeClass BNGetTypeClass(BNType* type); BINARYNINJACOREAPI uint64_t BNGetTypeWidth(BNType* type); BINARYNINJACOREAPI size_t BNGetTypeAlignment(BNType* type); @@ -2454,6 +2461,12 @@ extern "C" BINARYNINJACOREAPI BNNamedTypeReference* BNGetTypeNamedTypeReference(BNType* type); BINARYNINJACOREAPI uint64_t BNGetTypeElementCount(BNType* type); BINARYNINJACOREAPI void BNSetFunctionCanReturn(BNType* type, bool canReturn); + BINARYNINJACOREAPI BNMemberScope BNTypeGetMemberScope(BNType* type); + BINARYNINJACOREAPI void BNTypeSetMemberScope(BNType* type, BNMemberScope scope); + BINARYNINJACOREAPI BNMemberAccess BNTypeGetMemberAccess(BNType* type); + BINARYNINJACOREAPI void BNTypeSetMemberAccess(BNType* type, BNMemberAccess access); + BINARYNINJACOREAPI void BNTypeSetConst(BNType* type, bool cnst); + BINARYNINJACOREAPI void BNTypeSetVolatile(BNType* type, bool vltl); BINARYNINJACOREAPI char* BNGetTypeString(BNType* type); BINARYNINJACOREAPI char* BNGetTypeStringBeforeName(BNType* type); @@ -2478,6 +2491,7 @@ extern "C" BINARYNINJACOREAPI BNNamedTypeReference* BNNewNamedTypeReference(BNNamedTypeReference* nt); BINARYNINJACOREAPI BNStructure* BNCreateStructure(void); + BINARYNINJACOREAPI BNStructure* BNCreateStructureWithOptions(BNStructureType type, bool isUnion, bool packed); BINARYNINJACOREAPI BNStructure* BNNewStructureReference(BNStructure* s); BINARYNINJACOREAPI void BNFreeStructure(BNStructure* s); diff --git a/demangle.cpp b/demangle.cpp index 70a2fe08..677998d2 100644 --- a/demangle.cpp +++ b/demangle.cpp @@ -1,18 +1,22 @@ #include "binaryninjaapi.h" -using namespace BinaryNinja; using namespace std; +namespace BinaryNinja +{ bool DemangleMS(Architecture* arch, const std::string& mangledName, Type** outType, QualifiedName& outVarName) { - BNType* localType = (*outType)->GetObject(); + BNType* localType = nullptr; char** localVarName = nullptr; size_t localSize = 0; if (!BNDemangleMS(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize)) return false; + if (!localType) + return false; + *outType = new Type(localType); for (size_t i = 0; i < localSize; i++) { outVarName.push_back(localVarName[i]); @@ -23,16 +27,19 @@ bool DemangleMS(Architecture* arch, } -bool DemangleGNU3(Architecture* arch, +bool DemangleGNU3(Ref<Architecture> arch, const std::string& mangledName, Type** outType, QualifiedName& outVarName) { - BNType* localType = (*outType)->GetObject(); + BNType* localType; char** localVarName = nullptr; size_t localSize = 0; if (!BNDemangleGNU3(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize)) return false; + if (!localType) + return false; + *outType = new Type(localType); for (size_t i = 0; i < localSize; i++) { outVarName.push_back(localVarName[i]); @@ -41,3 +48,4 @@ bool DemangleGNU3(Architecture* arch, delete [] localVarName; return true; } +}
\ No newline at end of file diff --git a/python/demangle.py b/python/demangle.py index ed38674a..2a17cdfd 100644 --- a/python/demangle.py +++ b/python/demangle.py @@ -48,8 +48,8 @@ def demangle_ms(arch, mangled_name): :param Architecture arch: Architecture for the symbol. Required for pointer and integer sizes. :param str mangled_name: a mangled Microsoft Visual Studio C++ name - :return: returns a Type object for the mangled name - :rtype: Type + :return: returns tuple of (Type, demangled_name) or (None, mangled_name) on error + :rtype: Tuple :Example: >>> demangle_ms(Architecture["x86_64"], "?testf@Foobar@@SA?AW4foo@1@W421@@Z") @@ -279,6 +279,42 @@ bool Type::IsFloat() const } +BNMemberScope Type::GetScope() const +{ + return BNTypeGetMemberScope(m_object); +} + + +void Type::SetScope(BNMemberScope scope) +{ + return BNTypeSetMemberScope(m_object, scope); +} + + +BNMemberAccess Type::GetAccess() const +{ + return BNTypeGetMemberAccess(m_object); +} + + +void Type::SetAccess(BNMemberAccess access) +{ + return BNTypeSetMemberAccess(m_object, access); +} + + +void Type::SetConst(bool cnst) +{ + BNTypeSetConst(m_object, cnst); +} + + +void Type::SetVolatile(bool vltl) +{ + BNTypeSetVolatile(m_object, vltl); +} + + Ref<Type> Type::GetChildType() const { BNType* type = BNGetChildType(m_object); @@ -547,6 +583,12 @@ Ref<Type> Type::PointerType(Architecture* arch, Type* type, bool cnst, bool vltl } +Ref<Type> Type::PointerType(size_t width, Type* type, bool cnst, bool vltl, BNReferenceType refType) +{ + return new Type(BNCreatePointerTypeOfWidth(width, type->GetObject(), cnst, vltl, refType)); +} + + Ref<Type> Type::ArrayType(Type* type, uint64_t elem) { return new Type(BNCreateArrayType(type->GetObject(), elem)); @@ -608,6 +650,43 @@ string Type::GetAutoDemangledTypeIdSource() } +string Type::GenerateAutoDebugTypeId(const QualifiedName& name) +{ + BNQualifiedName nameObj = name.GetAPIObject(); + char* str = BNGenerateAutoDebugTypeId(&nameObj); + string result = str; + QualifiedName::FreeAPIObject(&nameObj); + BNFreeString(str); + return result; +} + + +string Type::GetAutoDebugTypeIdSource() +{ + char* str = BNGetAutoDebugTypeIdSource(); + string result = str; + BNFreeString(str); + return result; +} + + +QualifiedName Type::GetTypeName() const +{ + BNQualifiedName name = BNTypeGetTypeName(m_object); + QualifiedName result = QualifiedName::FromAPIObject(&name); + BNFreeQualifiedName(&name); + return result; +} + + +void Type::SetTypeName(const QualifiedName& names) +{ + BNQualifiedName nameObj = names.GetAPIObject(); + BNTypeSetTypeName(m_object, &nameObj); + QualifiedName::FreeAPIObject(&nameObj); +} + + NamedTypeReference::NamedTypeReference(BNNamedTypeReference* nt) { m_object = nt; @@ -691,12 +770,26 @@ Ref<NamedTypeReference> NamedTypeReference::GenerateAutoDemangledTypeReference(B } +Ref<NamedTypeReference> NamedTypeReference::GenerateAutoDebugTypeReference(BNNamedTypeReferenceClass cls, + const QualifiedName& name) +{ + string id = Type::GenerateAutoDebugTypeId(name); + return new NamedTypeReference(cls, id, name); +} + + Structure::Structure() { m_object = BNCreateStructure(); } +Structure::Structure(BNStructureType type, bool isUnion, bool packed) +{ + m_object = BNCreateStructureWithOptions(type, isUnion, packed); +} + + Structure::Structure(BNStructure* s) { m_object = s; @@ -801,6 +894,12 @@ Enumeration::Enumeration(BNEnumeration* e) } +Enumeration::Enumeration() +{ + m_object = BNCreateEnumeration(); +} + + vector<EnumerationMember> Enumeration::GetMembers() const { size_t count; |
