diff options
| author | Peter LaFosse <peter@vector35.com> | 2021-09-28 13:59:09 -0400 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2021-09-28 13:59:09 -0400 |
| commit | ebb964c2793e7833dd55e2d5705a33f256b8b899 (patch) | |
| tree | 022c2e88afde95ee82371eb67f2475c2e03a46df /python | |
| parent | 8edbc11b490acd704a54a384973d744bd1a3c009 (diff) | |
Add Refactor Symbol to CoreSymbol and Symbol
Diffstat (limited to 'python')
| -rw-r--r-- | python/function.py | 8 | ||||
| -rw-r--r-- | python/types.py | 89 |
2 files changed, 49 insertions, 48 deletions
diff --git a/python/function.py b/python/function.py index ee9921a0..f42e8582 100644 --- a/python/function.py +++ b/python/function.py @@ -312,7 +312,7 @@ class Function: return self.symbol.name @name.setter - def name(self, value:Union[str, 'types.Symbol']) -> None: # type: ignore + def name(self, value:Union[str, 'types.CoreSymbol']) -> None: # type: ignore if value is None: if self.symbol is not None: self.view.undefine_user_symbol(self.symbol) @@ -386,11 +386,11 @@ class Function: return result @property - def symbol(self) -> 'types.Symbol': + def symbol(self) -> 'types.CoreSymbol': """Function symbol(read-only)""" sym = core.BNGetFunctionSymbol(self.handle) assert sym is not None, "core.BNGetFunctionSymbol returned None" - return types.Symbol(None, None, None, handle = sym) + return types.CoreSymbol(sym) @property def auto(self) -> bool: @@ -2133,7 +2133,7 @@ class Function: settings_obj = None return flowgraph.CoreFlowGraph(core.BNCreateFunctionGraph(self.handle, graph_type, settings_obj)) - def apply_imported_types(self, sym:'types.Symbol', type:'types.Type'=None) -> None: + def apply_imported_types(self, sym:'types.CoreSymbol', type:'types.Type'=None) -> None: core.BNApplyImportedTypes(self.handle, sym.handle, None if type is None else type.handle) def apply_auto_discovered_type(self, func_type:'types.Type') -> None: diff --git a/python/types.py b/python/types.py index 33186904..2374c6b1 100644 --- a/python/types.py +++ b/python/types.py @@ -209,46 +209,16 @@ class NameSpace(QualifiedName): return NameSpace(name)._to_core_struct() -class Symbol: - """ - Symbols are defined as one of the following types: - - =========================== ============================================================== - SymbolType Description - =========================== ============================================================== - FunctionSymbol Symbol for function that exists in the current binary - ImportAddressSymbol Symbol defined in the Import Address Table - ImportedFunctionSymbol Symbol for a function that is not defined in the current binary - DataSymbol Symbol for data in the current binary - ImportedDataSymbol Symbol for data that is not defined in the current binary - ExternalSymbol Symbols for data and code that reside outside the BinaryView - LibraryFunctionSymbol Symbols for external functions outside the library - =========================== ============================================================== - """ - def __init__(self, sym_type, addr, short_name, full_name=None, raw_name=None, handle=None, binding=None, namespace=None, ordinal=0): - if handle is not None: - SymbolPointer = ctypes.POINTER(core.BNSymbol) - _handle = ctypes.cast(handle, SymbolPointer) - else: - if isinstance(sym_type, str): - sym_type = SymbolType[sym_type] - if full_name is None: - full_name = short_name - if raw_name is None: - raw_name = full_name - if binding is None: - binding = SymbolBinding.NoBinding - _namespace = NameSpace.get_core_struct(namespace) - _handle = core.BNCreateSymbol(sym_type, short_name, full_name, raw_name, addr, binding, _namespace, ordinal) - assert _handle is not None - self._handle = _handle +class CoreSymbol: + def __init__(self, handle:core.BNSymbolHandle): + self._handle = handle def __del__(self): if core is not None: core.BNFreeSymbol(self._handle) def __repr__(self): - return "<%s: \"%s\" @ %#x>" % (self.type, self.full_name, self.address) + return f"<{self.type.name}: \"{self.full_name}\" @ {self.address:#x}>" def __eq__(self, other): if not isinstance(other, self.__class__): @@ -264,17 +234,17 @@ class Symbol: return hash(ctypes.addressof(self._handle.contents)) @property - def type(self): + def type(self) -> SymbolType: """Symbol type (read-only)""" return SymbolType(core.BNGetSymbolType(self._handle)) @property - def binding(self): + def binding(self) -> SymbolBinding: """Symbol binding (read-only)""" return SymbolBinding(core.BNGetSymbolBinding(self._handle)) @property - def namespace(self): + def namespace(self) -> 'NameSpace': """Symbol namespace (read-only)""" ns = core.BNGetSymbolNameSpace(self._handle) result = NameSpace._from_core_struct(ns) @@ -282,37 +252,37 @@ class Symbol: return result @property - def name(self): + def name(self) -> str: """Symbol name (read-only)""" return core.BNGetSymbolRawName(self._handle) @property - def short_name(self): + def short_name(self) -> str: """Symbol short name (read-only)""" return core.BNGetSymbolShortName(self._handle) @property - def full_name(self): + def full_name(self) -> str: """Symbol full name (read-only)""" return core.BNGetSymbolFullName(self._handle) @property - def raw_name(self): + def raw_name(self) -> str: """Symbol raw name (read-only)""" return core.BNGetSymbolRawName(self._handle) @property - def address(self): + def address(self) -> int: """Symbol address (read-only)""" return core.BNGetSymbolAddress(self._handle) @property - def ordinal(self): + def ordinal(self) -> int: """Symbol ordinal (read-only)""" return core.BNGetSymbolOrdinal(self._handle) @property - def auto(self): + def auto(self) -> bool: return core.BNIsSymbolAutoDefined(self._handle) @property @@ -320,6 +290,37 @@ class Symbol: return self._handle +class Symbol(CoreSymbol): + """ + Symbols are defined as one of the following types: + + =========================== ============================================================== + SymbolType Description + =========================== ============================================================== + FunctionSymbol Symbol for function that exists in the current binary + ImportAddressSymbol Symbol defined in the Import Address Table + ImportedFunctionSymbol Symbol for a function that is not defined in the current binary + DataSymbol Symbol for data in the current binary + ImportedDataSymbol Symbol for data that is not defined in the current binary + ExternalSymbol Symbols for data and code that reside outside the BinaryView + LibraryFunctionSymbol Symbols for external functions outside the library + =========================== ============================================================== + """ + def __init__(self, sym_type, addr, short_name, full_name=None, raw_name=None, binding=None, namespace=None, ordinal=0): + if isinstance(sym_type, str): + sym_type = SymbolType[sym_type] + if full_name is None: + full_name = short_name + if raw_name is None: + raw_name = full_name + if binding is None: + binding = SymbolBinding.NoBinding + _namespace = NameSpace.get_core_struct(namespace) + _handle = core.BNCreateSymbol(sym_type, short_name, full_name, raw_name, addr, binding, _namespace, ordinal) + assert _handle is not None, "core.BNCreateSymbol return None" + super(Symbol, self).__init__(_handle) + + @dataclass class FunctionParameter: type:SomeType |
