From a40f0e1e494af967a35ca4a8977596f4693b5c0e Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Thu, 29 Sep 2016 18:41:13 -0400 Subject: Adding APIs for segments and sections --- binaryninjaapi.h | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'binaryninjaapi.h') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index a62b4239..ee1c9c0e 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -729,6 +729,22 @@ namespace BinaryNinja bool autoDiscovered; }; + struct Segment + { + uint64_t start, length; + uint64_t dataOffset, dataLength; + uint32_t flags; + }; + + struct Section + { + std::string name, type; + uint64_t start, length; + std::string linkedSection, infoSection; + uint64_t infoData; + uint64_t align, entrySize; + }; + struct NameAndType; /*! BinaryView is the base class for creating views on binary data (e.g. ELF, PE, Mach-O). @@ -742,8 +758,9 @@ namespace BinaryNinja /*! BinaryView constructor \param typeName name of the BinaryView (e.g. ELF, PE, Mach-O, ...) \param file a file to create a view from + \param parentView optional view that contains the raw data used by this view */ - BinaryView(const std::string& typeName, FileMetadata* file); + BinaryView(const std::string& typeName, FileMetadata* file, BinaryView* parentView = nullptr); /*! PerformRead provides a mapping between the flat file and virtual offsets in the file. @@ -770,7 +787,7 @@ namespace BinaryNinja virtual BNEndianness PerformGetDefaultEndianness() const; virtual size_t PerformGetAddressSize() const; - virtual bool PerformSave(FileAccessor* file) { (void)file; return false; } + virtual bool PerformSave(FileAccessor* file); void NotifyDataWritten(uint64_t offset, size_t len); void NotifyDataInserted(uint64_t offset, size_t len); @@ -804,6 +821,7 @@ namespace BinaryNinja virtual bool Init() { return true; } FileMetadata* GetFile() const { return m_file; } + Ref GetParentView() const; std::string GetTypeName() const; bool IsModified() const; @@ -968,6 +986,29 @@ namespace BinaryNinja bool GetAddressInput(uint64_t& result, const std::string& prompt, const std::string& title); bool GetAddressInput(uint64_t& result, const std::string& prompt, const std::string& title, uint64_t currentAddress); + + void AddAutoSegment(uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, uint32_t flags); + void RemoveAutoSegment(uint64_t start, uint64_t length); + void AddUserSegment(uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, uint32_t flags); + void RemoveUserSegment(uint64_t start, uint64_t length); + std::vector GetSegments(); + bool GetSegmentAt(uint64_t addr, Segment& result); + + void AddAutoSection(const std::string& name, uint64_t start, uint64_t length, const std::string& type = "", + uint64_t align = 1, uint64_t entrySize = 0, const std::string& linkedSection = "", + const std::string& infoSection = "", uint64_t infoData = 0); + void RemoveAutoSection(const std::string& name); + void AddUserSection(const std::string& name, uint64_t start, uint64_t length, const std::string& type = "", + uint64_t align = 1, uint64_t entrySize = 0, const std::string& linkedSection = "", + const std::string& infoSection = "", uint64_t infoData = 0); + void RemoveUserSection(const std::string& name); + std::vector
GetSections(); + std::vector
GetSectionsAt(uint64_t addr); + bool GetSectionByName(const std::string& name, Section& result); + + std::vector GetUniqueSectionNames(const std::vector& names); + + std::vector GetAllocatedRanges(); }; class BinaryData: public BinaryView -- cgit v1.3.1 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(-) (limited to 'binaryninjaapi.h') 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 From 254d5aa23c0d114e9a275f60538fcb3b76ac1c69 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Thu, 20 Oct 2016 00:14:41 -0400 Subject: Add API for multiple architectures distinguished by address --- architecture.cpp | 20 ++++++++++++++++++++ binaryninjaapi.h | 4 ++++ binaryninjacore.h | 2 ++ 3 files changed, 26 insertions(+) (limited to 'binaryninjaapi.h') diff --git a/architecture.cpp b/architecture.cpp index af7b0cba..3c7d9af8 100644 --- a/architecture.cpp +++ b/architecture.cpp @@ -111,6 +111,13 @@ size_t Architecture::GetOpcodeDisplayLengthCallback(void* ctxt) } +BNArchitecture* Architecture::GetAssociatedArchitectureByAddressCallback(void* ctxt, uint64_t* addr) +{ + Architecture* arch = (Architecture*)ctxt; + return arch->GetAssociatedArchitectureByAddress(*addr)->GetObject(); +} + + bool Architecture::GetInstructionInfoCallback(void* ctxt, const uint8_t* data, uint64_t addr, size_t maxLen, BNInstructionInfo* result) { @@ -408,6 +415,7 @@ void Architecture::Register(Architecture* arch) callbacks.getDefaultIntegerSize = GetDefaultIntegerSizeCallback; callbacks.getMaxInstructionLength = GetMaxInstructionLengthCallback; callbacks.getOpcodeDisplayLength = GetOpcodeDisplayLengthCallback; + callbacks.getAssociatedArchitectureByAddress = GetAssociatedArchitectureByAddressCallback; callbacks.getInstructionInfo = GetInstructionInfoCallback; callbacks.getInstructionText = GetInstructionTextCallback; callbacks.freeInstructionText = FreeInstructionTextCallback; @@ -499,6 +507,12 @@ size_t Architecture::GetOpcodeDisplayLength() const } +Ref Architecture::GetAssociatedArchitectureByAddress(uint64_t&) +{ + return this; +} + + bool Architecture::GetInstructionLowLevelIL(const uint8_t*, uint64_t, size_t&, LowLevelILFunction& il) { il.AddInstruction(il.Undefined()); @@ -919,6 +933,12 @@ size_t CoreArchitecture::GetOpcodeDisplayLength() const } +Ref CoreArchitecture::GetAssociatedArchitectureByAddress(uint64_t& addr) +{ + return new CoreArchitecture(BNGetAssociatedArchitectureByAddress(m_object, &addr)); +} + + bool CoreArchitecture::GetInstructionInfo(const uint8_t* data, uint64_t addr, size_t maxLen, InstructionInfo& result) { return BNGetInstructionInfo(m_object, data, addr, maxLen, &result); diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 7c297f73..6311271c 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -1257,6 +1257,7 @@ namespace BinaryNinja static size_t GetDefaultIntegerSizeCallback(void* ctxt); static size_t GetMaxInstructionLengthCallback(void* ctxt); static size_t GetOpcodeDisplayLengthCallback(void* ctxt); + static BNArchitecture* GetAssociatedArchitectureByAddressCallback(void* ctxt, uint64_t* addr); static bool GetInstructionInfoCallback(void* ctxt, const uint8_t* data, uint64_t addr, size_t maxLen, BNInstructionInfo* result); static bool GetInstructionTextCallback(void* ctxt, const uint8_t* data, uint64_t addr, @@ -1311,6 +1312,8 @@ namespace BinaryNinja virtual size_t GetMaxInstructionLength() const; virtual size_t GetOpcodeDisplayLength() const; + virtual Ref GetAssociatedArchitectureByAddress(uint64_t& addr); + virtual bool GetInstructionInfo(const uint8_t* data, uint64_t addr, size_t maxLen, InstructionInfo& result) = 0; virtual bool GetInstructionText(const uint8_t* data, uint64_t addr, size_t& len, std::vector& result) = 0; @@ -1454,6 +1457,7 @@ namespace BinaryNinja virtual size_t GetDefaultIntegerSize() const override; virtual size_t GetMaxInstructionLength() const override; virtual size_t GetOpcodeDisplayLength() const override; + virtual Ref GetAssociatedArchitectureByAddress(uint64_t& addr) override; virtual bool GetInstructionInfo(const uint8_t* data, uint64_t addr, size_t maxLen, InstructionInfo& result) override; virtual bool GetInstructionText(const uint8_t* data, uint64_t addr, size_t& len, std::vector& result) override; diff --git a/binaryninjacore.h b/binaryninjacore.h index ac615922..ea81c388 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -725,6 +725,7 @@ extern "C" size_t (*getDefaultIntegerSize)(void* ctxt); size_t (*getMaxInstructionLength)(void* ctxt); size_t (*getOpcodeDisplayLength)(void* ctxt); + BNArchitecture* (*getAssociatedArchitectureByAddress)(void* ctxt, uint64_t* addr); bool (*getInstructionInfo)(void* ctxt, const uint8_t* data, uint64_t addr, size_t maxLen, BNInstructionInfo* result); bool (*getInstructionText)(void* ctxt, const uint8_t* data, uint64_t addr, size_t* len, BNInstructionTextToken** result, size_t* count); @@ -1516,6 +1517,7 @@ extern "C" BINARYNINJACOREAPI size_t BNGetArchitectureDefaultIntegerSize(BNArchitecture* arch); BINARYNINJACOREAPI size_t BNGetArchitectureMaxInstructionLength(BNArchitecture* arch); BINARYNINJACOREAPI size_t BNGetArchitectureOpcodeDisplayLength(BNArchitecture* arch); + BINARYNINJACOREAPI BNArchitecture* BNGetAssociatedArchitectureByAddress(BNArchitecture* arch, uint64_t* addr); BINARYNINJACOREAPI bool BNGetInstructionInfo(BNArchitecture* arch, const uint8_t* data, uint64_t addr, size_t maxLen, BNInstructionInfo* result); BINARYNINJACOREAPI bool BNGetInstructionText(BNArchitecture* arch, const uint8_t* data, uint64_t addr, -- cgit v1.3.1 From eef9d1eff05975c31862db4157ac2f1b2bb30502 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Mon, 31 Oct 2016 22:51:44 -0400 Subject: Added APIs for dealing with Thumb more easily --- binaryninjaapi.h | 2 ++ binaryninjacore.h | 3 +++ binaryview.cpp | 7 +++++++ platform.cpp | 9 +++++++++ python/__init__.py | 21 +++++++++++++++++++++ 5 files changed, 42 insertions(+) (limited to 'binaryninjaapi.h') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 6311271c..a918fc42 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -926,6 +926,7 @@ namespace BinaryNinja std::vector> GetSymbolsOfType(BNSymbolType type, uint64_t start, uint64_t len); void DefineAutoSymbol(Ref sym); + void DefineAutoSymbolAndVariableOrFunction(Ref platform, Ref sym, Ref type); void UndefineAutoSymbol(Ref sym); void DefineUserSymbol(Ref sym); @@ -2243,6 +2244,7 @@ namespace BinaryNinja Ref GetRelatedPlatform(Architecture* arch); void AddRelatedPlatform(Architecture* arch, Platform* platform); + Ref GetAssociatedPlatformByAddress(uint64_t& addr); }; class ScriptingOutputListener diff --git a/binaryninjacore.h b/binaryninjacore.h index ea81c388..b2ed7cd3 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -1877,6 +1877,8 @@ extern "C" BINARYNINJACOREAPI void BNDefineUserSymbol(BNBinaryView* view, BNSymbol* sym); BINARYNINJACOREAPI void BNUndefineUserSymbol(BNBinaryView* view, BNSymbol* sym); BINARYNINJACOREAPI void BNDefineImportedFunction(BNBinaryView* view, BNSymbol* importAddressSym, BNFunction* func); + BINARYNINJACOREAPI void BNDefineAutoSymbolAndVariableOrFunction(BNBinaryView* view, BNPlatform* platform, + BNSymbol* sym, BNType* type); BINARYNINJACOREAPI BNSymbol* BNImportedFunctionFromImportAddressSymbol(BNSymbol* sym, uint64_t addr); @@ -2135,6 +2137,7 @@ extern "C" BINARYNINJACOREAPI BNPlatform* BNGetRelatedPlatform(BNPlatform* platform, BNArchitecture* arch); BINARYNINJACOREAPI void BNAddRelatedPlatform(BNPlatform* platform, BNArchitecture* arch, BNPlatform* related); + BINARYNINJACOREAPI BNPlatform* BNGetAssociatedPlatformByAddress(BNPlatform* platform, uint64_t* addr); //Demangler BINARYNINJACOREAPI bool BNDemangleMS(BNArchitecture* arch, diff --git a/binaryview.cpp b/binaryview.cpp index ff19de18..cc6667c5 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -1156,6 +1156,13 @@ void BinaryView::DefineAutoSymbol(Ref sym) } +void BinaryView::DefineAutoSymbolAndVariableOrFunction(Ref platform, Ref sym, Ref type) +{ + BNDefineAutoSymbolAndVariableOrFunction(m_object, platform ? platform->GetObject() : nullptr, sym->GetObject(), + type ? type->GetObject() : nullptr); +} + + void BinaryView::UndefineAutoSymbol(Ref sym) { BNUndefineAutoSymbol(m_object, sym->GetObject()); diff --git a/platform.cpp b/platform.cpp index 9b4053db..7a6571cc 100644 --- a/platform.cpp +++ b/platform.cpp @@ -244,3 +244,12 @@ void Platform::AddRelatedPlatform(Architecture* arch, Platform* platform) { BNAddRelatedPlatform(m_object, arch->GetObject(), platform->GetObject()); } + + +Ref Platform::GetAssociatedPlatformByAddress(uint64_t& addr) +{ + BNPlatform* platform = BNGetAssociatedPlatformByAddress(m_object, &addr); + if (!platform) + return nullptr; + return new Platform(platform); +} diff --git a/python/__init__.py b/python/__init__.py index 6c7e298c..1751fefa 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -2630,6 +2630,21 @@ class BinaryView(object): """ core.BNDefineAutoSymbol(self.handle, sym.handle) + def define_auto_symbol_and_var_or_function(self, sym, sym_type, platform = None): + """ + ``define_auto_symbol`` adds a symbol to the internal list of automatically discovered Symbol objects. + + :param Symbol sym: the symbol to define + :rtype: None + """ + if platform is None: + platform = self.platform + if platform is not None: + platform = platform.handle + if sym_type is not None: + sym_type = sym_type.handle + core.BNDefineAutoSymbolAndVariableOrFunction(self.handle, platform, sym.handle, sym_type) + def undefine_auto_symbol(self, sym): """ ``undefine_auto_symbol`` removes a symbol from the internal list of automatically discovered Symbol objects. @@ -9730,6 +9745,12 @@ class Platform(object): def add_related_platform(self, arch, platform): core.BNAddRelatedPlatform(self.handle, arch.handle, platform.handle) + def get_associated_platform_by_address(self, addr): + new_addr = ctypes.c_ulonglong() + new_addr.value = addr + result = core.BNGetAssociatedPlatformByAddress(self.handle, new_addr) + return Platform(None, handle = result), new_addr.value + class ScriptingOutputListener(object): def _register(self, handle): self._cb = core.BNScriptingOutputListener() -- cgit v1.3.1