diff options
| author | Peter LaFosse <peter@vector35.com> | 2018-10-14 21:37:09 -0400 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2018-10-15 23:18:23 -0400 |
| commit | 7006bdf444f2b9533bcc3aee2933ba7ff8e70d2e (patch) | |
| tree | d4af914aa4b4ebad400106a278a7ed6791a66ff4 | |
| parent | 50b3ff664665a6186d6d82364a0845d544ec8fac (diff) | |
Refactor to how symbols and namespaces work
| -rw-r--r-- | binaryninjaapi.h | 19 | ||||
| -rw-r--r-- | binaryninjacore.h | 16 | ||||
| -rw-r--r-- | binaryview.cpp | 46 | ||||
| -rw-r--r-- | python/binaryview.py | 54 | ||||
| -rw-r--r-- | python/types.py | 8 | ||||
| -rw-r--r-- | suite/testcommon.py | 10 |
6 files changed, 71 insertions, 82 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 7c39c5d3..565be2f7 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -1002,8 +1002,9 @@ namespace BinaryNinja { public: Symbol(BNSymbolType type, const std::string& shortName, const std::string& fullName, const std::string& rawName, - uint64_t addr, BNSymbolBinding binding=NoBinding); - Symbol(BNSymbolType type, const std::string& name, uint64_t addr, BNSymbolBinding binding=NoBinding); + uint64_t addr, BNSymbolBinding binding=NoBinding, const NameSpace& nameSpace=NameSpace(DEFAULT_INTERNAL_NAMESPACE)); + Symbol(BNSymbolType type, const std::string& name, uint64_t addr, BNSymbolBinding binding=NoBinding, + const NameSpace& nameSpace=NameSpace(DEFAULT_INTERNAL_NAMESPACE)); Symbol(BNSymbol* sym); BNSymbolType GetType() const; @@ -1367,12 +1368,12 @@ namespace BinaryNinja std::vector<Ref<Symbol>> GetSymbolsOfType(BNSymbolType type, const NameSpace& nameSpace=NameSpace()); std::vector<Ref<Symbol>> GetSymbolsOfType(BNSymbolType type, uint64_t start, uint64_t len, const NameSpace& nameSpace=NameSpace()); - void DefineAutoSymbol(Ref<Symbol> sym, const NameSpace& nameSpace=NameSpace()); - void DefineAutoSymbolAndVariableOrFunction(Ref<Platform> platform, Ref<Symbol> sym, Ref<Type> type, const NameSpace& nameSpace=NameSpace()); - void UndefineAutoSymbol(Ref<Symbol> sym, const NameSpace& nameSpace=NameSpace()); + void DefineAutoSymbol(Ref<Symbol> sym); + void DefineAutoSymbolAndVariableOrFunction(Ref<Platform> platform, Ref<Symbol> sym, Ref<Type> type); + void UndefineAutoSymbol(Ref<Symbol> sym); - void DefineUserSymbol(Ref<Symbol> sym, const NameSpace& nameSpace=NameSpace()); - void UndefineUserSymbol(Ref<Symbol> sym, const NameSpace& nameSpace=NameSpace()); + void DefineUserSymbol(Ref<Symbol> sym); + void UndefineUserSymbol(Ref<Symbol> sym); void DefineImportedFunction(Ref<Symbol> importAddressSym, Ref<Function> func); @@ -1481,8 +1482,8 @@ namespace BinaryNinja void SetNewAutoFunctionAnalysisSuppressed(bool suppress); std::set<NameSpace> GetNameSpaces() const; - NameSpace GetInternalNameSpace() const; - NameSpace GetExternalNameSpace() const; + static NameSpace GetInternalNameSpace(); + static NameSpace GetExternalNameSpace(); }; diff --git a/binaryninjacore.h b/binaryninjacore.h index 9a90fb6f..d2189f08 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -2180,8 +2180,8 @@ extern "C" BINARYNINJACOREAPI BNNameSpace* BNGetNameSpaces(BNBinaryView* view, size_t* count); BINARYNINJACOREAPI void BNFreeNameSpaceList(BNNameSpace* nameSpace, size_t count); - BINARYNINJACOREAPI BNNameSpace BNGetExternalNameSpace(BNBinaryView* view); - BINARYNINJACOREAPI BNNameSpace BNGetInternalNameSpace(BNBinaryView* view); + BINARYNINJACOREAPI BNNameSpace BNGetExternalNameSpace(); + BINARYNINJACOREAPI BNNameSpace BNGetInternalNameSpace(); BINARYNINJACOREAPI void BNFreeNameSpace(BNNameSpace* name); BINARYNINJACOREAPI BNAddressRange* BNGetAllocatedRanges(BNBinaryView* view, size_t* count); @@ -2827,7 +2827,7 @@ extern "C" // Symbols BINARYNINJACOREAPI BNSymbol* BNCreateSymbol(BNSymbolType type, const char* shortName, const char* fullName, - const char* rawName, uint64_t addr, BNSymbolBinding binding); + const char* rawName, uint64_t addr, BNSymbolBinding binding, const BNNameSpace* nameSpace); BINARYNINJACOREAPI BNSymbol* BNNewSymbolReference(BNSymbol* sym); BINARYNINJACOREAPI void BNFreeSymbol(BNSymbol* sym); BINARYNINJACOREAPI BNSymbolType BNGetSymbolType(BNSymbol* sym); @@ -2849,13 +2849,13 @@ extern "C" uint64_t start, uint64_t len, size_t* count, const BNNameSpace* nameSpace); BINARYNINJACOREAPI void BNFreeSymbolList(BNSymbol** syms, size_t count); - BINARYNINJACOREAPI void BNDefineAutoSymbol(BNBinaryView* view, BNSymbol* sym, const BNNameSpace* nameSpace); - BINARYNINJACOREAPI void BNUndefineAutoSymbol(BNBinaryView* view, BNSymbol* sym, const BNNameSpace* nameSpace); - BINARYNINJACOREAPI void BNDefineUserSymbol(BNBinaryView* view, BNSymbol* sym, const BNNameSpace* nameSpace); - BINARYNINJACOREAPI void BNUndefineUserSymbol(BNBinaryView* view, BNSymbol* sym, const BNNameSpace* nameSpace); + BINARYNINJACOREAPI void BNDefineAutoSymbol(BNBinaryView* view, BNSymbol* sym); + BINARYNINJACOREAPI void BNUndefineAutoSymbol(BNBinaryView* view, BNSymbol* sym); + 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, const BNNameSpace* nameSpace); + BNSymbol* sym, BNType* type); BINARYNINJACOREAPI BNSymbol* BNImportedFunctionFromImportAddressSymbol(BNSymbol* sym, uint64_t addr); diff --git a/binaryview.cpp b/binaryview.cpp index 2dc7398a..cdeb0a9f 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -169,15 +169,20 @@ BinaryDataNotification::BinaryDataNotification() Symbol::Symbol(BNSymbolType type, const string& shortName, const string& fullName, const string& rawName, uint64_t addr, - BNSymbolBinding binding) + BNSymbolBinding binding, const NameSpace& nameSpace) { - m_object = BNCreateSymbol(type, shortName.c_str(), fullName.c_str(), rawName.c_str(), addr, binding); + BNNameSpace ns = nameSpace.GetAPIObject(); + m_object = BNCreateSymbol(type, shortName.c_str(), fullName.c_str(), rawName.c_str(), addr, binding, &ns); + NameSpace::FreeAPIObject(&ns); } -Symbol::Symbol(BNSymbolType type, const std::string& name, uint64_t addr, BNSymbolBinding binding) +Symbol::Symbol(BNSymbolType type, const std::string& name, uint64_t addr, BNSymbolBinding binding, const NameSpace& nameSpace) { - m_object = BNCreateSymbol(type, name.c_str(), name.c_str(), name.c_str(), addr, binding); + BNNameSpace ns = nameSpace.GetAPIObject(); + m_object = BNCreateSymbol(type, name.c_str(), name.c_str(), name.c_str(), addr, binding, &ns); + NameSpace::FreeAPIObject(&ns); + } @@ -1540,39 +1545,34 @@ vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type, uint64_t sta } -void BinaryView::DefineAutoSymbol(Ref<Symbol> sym, const NameSpace& nameSpace) +void BinaryView::DefineAutoSymbol(Ref<Symbol> sym) { - BNNameSpace ns = nameSpace.GetAPIObject(); - BNDefineAutoSymbol(m_object, sym->GetObject(), &ns); + BNDefineAutoSymbol(m_object, sym->GetObject()); } -void BinaryView::DefineAutoSymbolAndVariableOrFunction(Ref<Platform> platform, Ref<Symbol> sym, Ref<Type> type, const NameSpace& nameSpace) +void BinaryView::DefineAutoSymbolAndVariableOrFunction(Ref<Platform> platform, Ref<Symbol> sym, Ref<Type> type) { - BNNameSpace ns = nameSpace.GetAPIObject(); BNDefineAutoSymbolAndVariableOrFunction(m_object, platform ? platform->GetObject() : nullptr, sym->GetObject(), - type ? type->GetObject() : nullptr, &ns); + type ? type->GetObject() : nullptr); } -void BinaryView::UndefineAutoSymbol(Ref<Symbol> sym, const NameSpace& nameSpace) +void BinaryView::UndefineAutoSymbol(Ref<Symbol> sym) { - BNNameSpace ns = nameSpace.GetAPIObject(); - BNUndefineAutoSymbol(m_object, sym->GetObject(), &ns); + BNUndefineAutoSymbol(m_object, sym->GetObject()); } -void BinaryView::DefineUserSymbol(Ref<Symbol> sym, const NameSpace& nameSpace) +void BinaryView::DefineUserSymbol(Ref<Symbol> sym) { - BNNameSpace ns = nameSpace.GetAPIObject(); - BNDefineUserSymbol(m_object, sym->GetObject(), &ns); + BNDefineUserSymbol(m_object, sym->GetObject()); } -void BinaryView::UndefineUserSymbol(Ref<Symbol> sym, const NameSpace& nameSpace) +void BinaryView::UndefineUserSymbol(Ref<Symbol> sym) { - BNNameSpace ns = nameSpace.GetAPIObject(); - BNUndefineUserSymbol(m_object, sym->GetObject(), &ns); + BNUndefineUserSymbol(m_object, sym->GetObject()); } @@ -2283,18 +2283,18 @@ set<NameSpace> BinaryView::GetNameSpaces() const } -NameSpace BinaryView::GetInternalNameSpace() const +NameSpace BinaryView::GetInternalNameSpace() { - BNNameSpace ns = BNGetInternalNameSpace(m_object); + BNNameSpace ns = BNGetInternalNameSpace(); NameSpace nameSpace = NameSpace::FromAPIObject(&ns); BNFreeNameSpace(&ns); return nameSpace; } -NameSpace BinaryView::GetExternalNameSpace() const +NameSpace BinaryView::GetExternalNameSpace() { - BNNameSpace ns = BNGetExternalNameSpace(m_object); + BNNameSpace ns = BNGetExternalNameSpace(); NameSpace nameSpace = NameSpace::FromAPIObject(&ns); BNFreeNameSpace(&ns); return nameSpace; diff --git a/python/binaryview.py b/python/binaryview.py index ecb25503..fe7a9dc9 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -1060,24 +1060,25 @@ class BinaryView(object): core.BNFreeSymbolList(syms, count.value) return result - @property + @classmethod def internal_namespace(self): """Internal namespace for the current BinaryView""" - ns = core.BNGetInternalNameSpace(self.handle) + ns = core.BNGetInternalNameSpace() result = types.NameSpace._from_core_struct(ns) core.BNFreeNameSpace(ns) return result - @property + @classmethod def external_namespace(self): """External namespace for the current BinaryView""" - ns = core.BNGetExternalNameSpace(self.handle) + ns = core.BNGetExternalNameSpace() result = types.NameSpace._from_core_struct(ns) core.BNFreeNameSpace(ns) return result @property def namespaces(self): + """Returns a list of namespaces for the current BinaryView""" count = ctypes.c_ulonglong(0) nameSpaceList = core.BNGetNameSpaces(self.handle, count) result = [] @@ -2610,7 +2611,7 @@ class BinaryView(object): core.BNFreeSymbolList(syms, count.value) return result - def define_auto_symbol(self, sym, namespace=None): + def define_auto_symbol(self, sym): """ ``define_auto_symbol`` adds a symbol to the internal list of automatically discovered Symbol objects in a given namespace. @@ -2618,16 +2619,11 @@ class BinaryView(object): .. warning:: If multiple symbols for the same address are defined, only the most recent symbol will ever be used. :param Symbol sym: the symbol to define - :param NameSpace namespace: the namespace of the symbol :rtype: None """ - if isinstance(namespace, str): - namespace = types.NameSpace(namespace) - if isinstance(namespace, types.NameSpace): - namespace = namespace._get_core_struct() - core.BNDefineAutoSymbol(self.handle, sym.handle, namespace) + core.BNDefineAutoSymbol(self.handle, sym.handle) - def define_auto_symbol_and_var_or_function(self, sym, sym_type, plat=None, namespace=None): + def define_auto_symbol_and_var_or_function(self, sym, sym_type, plat=None): """ ``define_auto_symbol_and_var_or_function`` @@ -2636,7 +2632,6 @@ class BinaryView(object): :param Symbol sym: the symbol to define :param SymbolType sym_type: Type of symbol being defined :param Platform plat: (optional) platform - :param NameSpace namespace: the namespace of the symbol :rtype: None """ if plat is None: @@ -2645,55 +2640,36 @@ class BinaryView(object): plat = plat.handle if sym_type is not None: sym_type = sym_type.handle - if isinstance(namespace, str): - namespace = types.NameSpace(namespace) - if isinstance(namespace, types.NameSpace): - namespace = namespace._get_core_struct() - core.BNDefineAutoSymbolAndVariableOrFunction(self.handle, plat, sym.handle, sym_type, namespace) + core.BNDefineAutoSymbolAndVariableOrFunction(self.handle, plat, sym.handle, sym_type) - def undefine_auto_symbol(self, sym, namespace=None): + def undefine_auto_symbol(self, sym): """ ``undefine_auto_symbol`` removes a symbol from the internal list of automatically discovered Symbol objects. :param Symbol sym: the symbol to undefine - :param NameSpace namespace: the namespace of the symbol :rtype: None """ - if isinstance(namespace, str): - namespace = types.NameSpace(namespace) - if isinstance(namespace, types.NameSpace): - namespace = namespace._get_core_struct() - core.BNUndefineAutoSymbol(self.handle, sym.handle, namespace) + core.BNUndefineAutoSymbol(self.handle, sym.handle) - def define_user_symbol(self, sym, namespace=None): + def define_user_symbol(self, sym): """ ``define_user_symbol`` adds a symbol to the internal list of user added Symbol objects. .. warning:: If multiple symbols for the same address are defined, only the most recent symbol will ever be used. :param Symbol sym: the symbol to define - :param NameSpace namespace: the namespace of the symbol :rtype: None """ - if isinstance(namespace, str): - namespace = types.NameSpace(namespace) - if isinstance(namespace, types.NameSpace): - namespace = namespace._get_core_struct() - core.BNDefineUserSymbol(self.handle, sym.handle, namespace) + core.BNDefineUserSymbol(self.handle, sym.handle) - def undefine_user_symbol(self, sym, namespace=None): + def undefine_user_symbol(self, sym): """ ``undefine_user_symbol`` removes a symbol from the internal list of user added Symbol objects. :param Symbol sym: the symbol to undefine - :param NameSpace namespace: the namespace of the symbol :rtype: None """ - if isinstance(namespace, str): - namespace = types.NameSpace(namespace) - if isinstance(namespace, types.NameSpace): - namespace = namespace._get_core_struct() - core.BNUndefineUserSymbol(self.handle, sym.handle, namespace) + core.BNUndefineUserSymbol(self.handle, sym.handle) def define_imported_function(self, import_addr_sym, func): """ diff --git a/python/types.py b/python/types.py index 72a39378..c7ebaf19 100644 --- a/python/types.py +++ b/python/types.py @@ -154,7 +154,7 @@ class Symbol(object): ExternalSymbol Symbols for data and code that reside outside the BinaryView =========================== ============================================================== """ - def __init__(self, sym_type, addr, short_name, full_name=None, raw_name=None, handle=None, binding=None): + def __init__(self, sym_type, addr, short_name, full_name=None, raw_name=None, handle=None, binding=None, namespace=None): if handle is not None: self.handle = core.handle_of_type(handle, core.BNSymbol) else: @@ -166,7 +166,11 @@ class Symbol(object): raw_name = full_name if binding is None: binding = SymbolBinding.NoBinding - self.handle = core.BNCreateSymbol(sym_type, short_name, full_name, raw_name, addr, binding) + if isinstance(namespace, str): + namespace = NameSpace(namespace) + if isinstance(namespace, NameSpace): + namespace = namespace._get_core_struct() + self.handle = core.BNCreateSymbol(sym_type, short_name, full_name, raw_name, addr, binding, namespace) def __del__(self): core.BNFreeSymbol(self.handle) diff --git a/suite/testcommon.py b/suite/testcommon.py index 22534600..497e85c4 100644 --- a/suite/testcommon.py +++ b/suite/testcommon.py @@ -117,7 +117,7 @@ class BinaryViewTestBuilder(Builder): """Function.symbol.name list doesnt match""" result = [] for x in self.bv.functions: - result.append("Symbol: " + x.symbol.name + ' ' + str(x.symbol.type) + ' ' + hex(x.symbol.address)) + result.append("Symbol: " + x.symbol.name + ' ' + str(x.symbol.type) + ' ' + hex(x.symbol.address) + ' ' + str(x.symbol.namespace)) return fixOutput(result) def test_function_can_return(self): @@ -158,6 +158,14 @@ class BinaryViewTestBuilder(Builder): """Symbols list doesn't match""" return ["Symbol: " + str(i) for i in sorted(self.bv.symbols)] + def test_symbol_namespaces(self): + """Symbol namespaces don't match""" + return self.bv.namespaces + + def test_internal_external_namespaces(self): + """Symbol namespaces don't match""" + return [BinaryView.internal_namespace(), BinaryView.external_namespace()] + def test_strings(self): """Strings list doesn't match""" return fixOutput(["String: " + str(x.value) + ' type: ' + str(x.type) + ' at: ' + hex(x.start) for x in self.bv.strings]) |
