From 1584c0f2ae591eafe6b69a9d663dbff4d32beaff Mon Sep 17 00:00:00 2001 From: plafosse Date: Thu, 25 Aug 2016 00:22:43 +0100 Subject: adding gnu3 demangler apis --- binaryninjaapi.h | 12 ++++++++++++ binaryninjacore.h | 23 +++++++++++++++++++++-- demangle.cpp | 20 ++++++++++++++++++++ python/__init__.py | 19 +++++++++++++++++++ type.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 2 deletions(-) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index b115263c..ed714be6 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -1449,6 +1449,7 @@ namespace BinaryNinja }; class Structure; + class UnknownType; class Enumeration; struct NameAndType @@ -1476,6 +1477,8 @@ namespace BinaryNinja bool CanReturn() const; Ref GetStructure() const; Ref GetEnumeration() const; + Ref GetUnknownType() const; + uint64_t GetElementCount() const; void SetFunctionCanReturn(bool canReturn); @@ -1492,6 +1495,7 @@ namespace BinaryNinja static Ref IntegerType(size_t width, bool sign, const std::string& altName = ""); static Ref FloatType(size_t width, const std::string& typeName = ""); static Ref StructureType(Structure* strct); + static Ref UnknownNamedType(UnknownType* unknwn); static Ref EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0, bool issigned = false); static Ref PointerType(Architecture* arch, Type* type, bool cnst = false, bool vltl = false, BNReferenceType refType = PointerReferenceType); @@ -1502,6 +1506,14 @@ namespace BinaryNinja static std::string GetQualifiedName(const std::vector& names); }; + class UnknownType: public CoreRefCountObject + { + public: + UnknownType(BNUnknownType* s, std::vector name = {}); + std::vector GetName() const; + void SetName(const std::vector& name); + }; + struct StructureMember { Ref type; diff --git a/binaryninjacore.h b/binaryninjacore.h index b6d2f7ed..e841b2d7 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -94,6 +94,7 @@ extern "C" struct BNLowLevelILFunction; struct BNType; struct BNStructure; + struct BNUnknownType; struct BNEnumeration; struct BNCallingConvention; struct BNPlatform; @@ -355,7 +356,8 @@ extern "C" ArrayTypeClass = 7, FunctionTypeClass = 8, VarArgsTypeClass = 9, - ValueTypeClass = 10 + ValueTypeClass = 10, + UnknownTypeClass = 11 }; enum BNStructureType @@ -471,7 +473,11 @@ extern "C" RttiBaseClassDescriptor, RttiBaseClassArray, RttiClassHeirarchyDescriptor, - RttiCompleteObjectLocator + RttiCompleteObjectLocator, + OperatorUnaryMinusNameType, + OperatorUnaryPlusNameType, + OperatorUnaryBitAndNameType, + OperatorUnaryStarNameType }; enum BNCallingConventionName @@ -1878,6 +1884,13 @@ extern "C" BINARYNINJACOREAPI char* BNGetTypeStringBeforeName(BNType* type); BINARYNINJACOREAPI char* BNGetTypeStringAfterName(BNType* type); + BINARYNINJACOREAPI BNType* BNCreateUnknownNamedType(BNUnknownType* ut); + BINARYNINJACOREAPI BNUnknownType* BNCreateUnknownType(void); + BINARYNINJACOREAPI void BNSetUnknownTypeName(BNUnknownType* ut, const char** name, size_t size); + BINARYNINJACOREAPI char** BNGetUnknownTypeName(BNUnknownType* ut, size_t* size); + BINARYNINJACOREAPI void BNFreeUnknownType(BNUnknownType* ut); + BINARYNINJACOREAPI BNUnknownType* BNNewUnknownTypeReference(BNUnknownType* ut); + BINARYNINJACOREAPI BNStructure* BNCreateStructure(void); BINARYNINJACOREAPI BNStructure* BNNewStructureReference(BNStructure* s); BINARYNINJACOREAPI void BNFreeStructure(BNStructure* s); @@ -2143,6 +2156,12 @@ extern "C" BINARYNINJACOREAPI BNMessageBoxButtonResult BNShowMessageBox(const char* title, const char* text, BNMessageBoxButtonSet buttons, BNMessageBoxIcon icon); + BINARYNINJACOREAPI bool BNDemangleGNU3(BNArchitecture* arch, + const char* mangledName, + BNType** outType, + char*** outVarName, + size_t* outVarNameElements); + BINARYNINJACOREAPI void BNFreeDemangledName(char*** name, size_t nameElements); #ifdef __cplusplus } #endif diff --git a/demangle.cpp b/demangle.cpp index 1a998a87..a12303ca 100644 --- a/demangle.cpp +++ b/demangle.cpp @@ -21,3 +21,23 @@ bool DemangleMS(Architecture* arch, delete [] localVarName; return true; } + + +bool DemangleGNU3(Architecture* arch, + const std::string& mangledName, + Type** outType, + std::vector& outVarName) +{ + BNType* localType = (*outType)->GetObject(); + char** localVarName = nullptr; + size_t localSize = 0; + if (!BNDemangleGNU3(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize)) + return false; + for (size_t i = 0; i < localSize; i++) + { + outVarName.push_back(localVarName[i]); + BNFreeString(localVarName[i]); + } + delete [] localVarName; + return true; +} diff --git a/python/__init__.py b/python/__init__.py index 26c8fb5d..f5a46798 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -4150,6 +4150,10 @@ class Type(object): def unknown_type(self, unknown_type): return Type(core.BNCreateUnknownType(unknown_type.handle)) + @classmethod + def unknown_type(self, s): + return Type(core.BNCreateUnknownType(s.handle)) + @classmethod def enumeration_type(self, arch, e, width = None): if width is None: @@ -10839,6 +10843,21 @@ def demangle_ms(arch, mangled_name): names.append(outName[i]) #core.BNFreeDemangledName(outName.value, outSize.value) return (Type(handle), names) + return (None, mangledName) + + +def demangle_gnu3(arch, mangledName): + handle = ctypes.POINTER(core.BNType)() + outName = ctypes.POINTER(ctypes.c_char_p)() + outSize = ctypes.c_ulonglong() + names = [] + if core.BNDemangleGNU3(arch.handle, mangledName, ctypes.byref(handle), ctypes.byref(outName), ctypes.byref(outSize)): + for i in xrange(outSize.value): + names.append(outName[i]) + #core.BNFreeDemangledName(outName.value, outSize.value) + if not handle: + return (None, names) + return (Type(handle), names) return (None, mangled_name) diff --git a/type.cpp b/type.cpp index 34e142b6..e0235664 100644 --- a/type.cpp +++ b/type.cpp @@ -235,6 +235,12 @@ Ref Type::StructureType(Structure* strct) } +Ref Type::UnknownNamedType(UnknownType* unknwn) +{ + return new Type(BNCreateUnknownNamedType(unknwn->GetObject())); +} + + Ref Type::EnumerationType(Architecture* arch, Enumeration* enm, size_t width, bool isSigned) { return new Type(BNCreateEnumerationType(arch->GetObject(), enm->GetObject(), width, isSigned)); @@ -277,6 +283,46 @@ void Type::SetFunctionCanReturn(bool canReturn) } +UnknownType::UnknownType(BNUnknownType* ut, vector names) +{ + m_object = ut; + const char ** nameList = new const char*[names.size()]; + for (size_t i = 0; i < names.size(); i++) + { + nameList[i] = names[i].c_str(); + } + BNSetUnknownTypeName(ut, nameList, names.size()); + delete [] nameList; +} + + +void UnknownType::SetName(const vector& names) +{ + const char ** nameList = new const char*[names.size()]; + for (size_t i = 0; i < names.size(); i++) + { + nameList[i] = names[i].c_str(); + } + BNSetUnknownTypeName(m_object, nameList, names.size()); + delete [] nameList; +} + + +vector UnknownType::GetName() const +{ + size_t size; + char** name = BNGetUnknownTypeName(m_object, &size); + vector result; + for (size_t i = 0; i < size; i++) + { + result.push_back(name[i]); + BNFreeString(name[i]); + } + delete [] name; + return result; +} + + Structure::Structure(BNStructure* s) { m_object = s; -- cgit v1.3.1