diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/binaryview.py | 72 | ||||
| -rw-r--r-- | python/exceptions.py | 4 | ||||
| -rw-r--r-- | python/externallibrary.py | 112 |
3 files changed, 158 insertions, 30 deletions
diff --git a/python/binaryview.py b/python/binaryview.py index 74f6c2ff..cfce2c96 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -45,7 +45,7 @@ from .enums import ( AnalysisState, SymbolType, Endianness, ModificationStatus, StringType, SegmentFlag, SectionSemantics, FindFlag, TypeClass, BinaryViewEventType, FunctionGraphType, TagReferenceType, TagTypeType, RegisterValueType, DisassemblyOption ) -from .exceptions import RelocationWriteException, ILException +from .exceptions import RelocationWriteException, ILException, ExternalLinkException from . import associateddatastore # required for _BinaryViewAssociatedDataStore from .log import log_warn, log_error, Logger @@ -9143,6 +9143,14 @@ class BinaryView: return Logger(self.file.session_id, logger_name) def add_external_library(self, name: str, backing_file: Optional['project.ProjectFile'] = None, auto: bool = False) -> externallibrary.ExternalLibrary: + """ + Add an ExternalLibrary to this BinaryView + + :param name: Name of the external library + :param backing_file: Optional ProjectFile that backs the external library + :param auto: Whether or not this action is the result of automated analysis + :return: The created ExternalLibrary + """ file_handle = None if backing_file is not None: file_handle = backing_file._handle @@ -9151,15 +9159,32 @@ class BinaryView: return externallibrary.ExternalLibrary(handle) def remove_external_library(self, name: str): + """ + Remove an ExternalLibrary from this BinaryView by name. + Any associated ExternalLocations will be unassociated from the ExternalLibrary + + :param name: Name of the external library to remove + """ core.BNBinaryViewRemoveExternalLibrary(self.handle, name) def get_external_library(self, name: str) -> Optional[externallibrary.ExternalLibrary]: + """ + Get an ExternalLibrary in this BinaryView by name + + :param name: Name of the external library + :return: An ExternalLibrary with the given name, or None + """ handle = core.BNBinaryViewGetExternalLibrary(self.handle, name) if handle is None: return None return externallibrary.ExternalLibrary(handle) def get_external_libraries(self) -> List[externallibrary.ExternalLibrary]: + """ + Get a list of all ExternalLibrary in this BinaryView + + :return: A list of ExternalLibraries in this BinaryView + """ count = ctypes.c_ulonglong(0) handles = core.BNBinaryViewGetExternalLibraries(self.handle, count) assert handles is not None, "core.BNBinaryViewGetExternalLibraries returned None" @@ -9173,25 +9198,54 @@ class BinaryView: finally: core.BNFreeExternalLibraryList(handles, count.value) - def add_external_location(self, symbol: '_types.CoreSymbol', library: Optional[externallibrary.ExternalLibrary], external_symbol: Optional[str], external_address: Optional[int], auto: bool = False) -> externallibrary.ExternalLocation: + def add_external_location(self, source_symbol: '_types.CoreSymbol', library: Optional[externallibrary.ExternalLibrary], target_symbol: Optional[str], target_address: Optional[int], auto: bool = False) -> externallibrary.ExternalLocation: + """ + Add an ExternalLocation with its source in this BinaryView. + ExternalLocations must have a target address and/or symbol. + + :param source_symbol: Symbol that the association is from + :param library: Library that the ExternalLocation belongs to + :param target_symbol: Symbol that the ExternalLocation points to + :param target_address: Address that the ExternalLocation points to + :param auto: Whether or not this action is the result of automated analysis + :return: The created ExternalLocation + """ c_addr = None - if external_address is not None: - c_addr = ctypes.c_ulonglong(external_address) + if target_address is not None: + c_addr = ctypes.c_ulonglong(target_address) + elif target_symbol is None: + raise ExternalLinkException("External locations must have a target address and/or symbol") - handle = core.BNBinaryViewAddExternalLocation(self.handle, symbol.handle, library._handle if library else None, external_symbol, c_addr, auto) + handle = core.BNBinaryViewAddExternalLocation(self.handle, source_symbol.handle, library._handle if library else None, target_symbol, c_addr, auto) assert handle is not None, "core.BNBinaryViewAddExternalLocation returned None" return externallibrary.ExternalLocation(handle) - def remove_external_location(self, symbol: '_types.CoreSymbol'): - core.BNBinaryViewRemoveExternalLocation(self.handle, symbol._handle) + def remove_external_location(self, source_symbol: '_types.CoreSymbol'): + """ + Remove the ExternalLocation with the given source symbol from this BinaryView + + :param source_symbol: Source symbol that will be used to determine the ExternalLocation to remove + """ + core.BNBinaryViewRemoveExternalLocation(self.handle, source_symbol._handle) + + def get_external_location(self, source_symbol: '_types.CoreSymbol') -> Optional[externallibrary.ExternalLocation]: + """ + Get the ExternalLocation with the given source symbol in this BinaryView - def get_external_location(self, symbol: '_types.CoreSymbol') -> Optional[externallibrary.ExternalLocation]: - handle = core.BNBinaryViewGetExternalLocation(self.handle, symbol.handle) + :param source_symbol: The source symbol of the ExternalLocation + :return: An ExternalLocation with the given source symbol, or None + """ + handle = core.BNBinaryViewGetExternalLocation(self.handle, source_symbol.handle) if handle is None: return None return externallibrary.ExternalLocation(handle) def get_external_locations(self) -> List[externallibrary.ExternalLocation]: + """ + Get a list of ExternalLocations in this BinaryView + + :return: A list of ExternalLocations in this BinaryView + """ count = ctypes.c_ulonglong(0) handles = core.BNBinaryViewGetExternalLocations(self.handle, count) assert handles is not None, "core.BNBinaryViewGetExternalLocations returned None" diff --git a/python/exceptions.py b/python/exceptions.py index a2ba197a..a9b4b5aa 100644 --- a/python/exceptions.py +++ b/python/exceptions.py @@ -9,3 +9,7 @@ class ILException(Exception): class ProjectException(Exception): """ Exception raised when project operations fail """ pass + +class ExternalLinkException(Exception): + """ Exception raised when external library/location operations fail """ + pass diff --git a/python/externallibrary.py b/python/externallibrary.py index 8164465a..c4aa0528 100644 --- a/python/externallibrary.py +++ b/python/externallibrary.py @@ -29,6 +29,9 @@ from . import types class ExternalLibrary: + """ + An ExternalLibrary is an abstraction for a library that is optionally backed by a ProjectFile. + """ def __init__(self, handle: core.BNExternalLibrary): self._handle = handle @@ -44,10 +47,20 @@ class ExternalLibrary: @property def name(self) -> str: + """ + Get the name of this external library + + :return: The name of this external library + """ return core.BNExternalLibraryGetName(self._handle) # type: ignore @property def backing_file(self) -> Optional[project.ProjectFile]: + """ + Get the file backing this external library + + :return: The file backing this external library or None + """ handle = core.BNExternalLibraryGetBackingFile(self._handle) if handle is None: return None @@ -55,6 +68,11 @@ class ExternalLibrary: @backing_file.setter def backing_file(self, new_file: Optional[project.ProjectFile]): + """ + Set the file backing this external library + + :param new_file: The file that will back this external library + """ new_file_handle = None if new_file is not None: new_file_handle = new_file._handle @@ -62,6 +80,9 @@ class ExternalLibrary: class ExternalLocation: + """ + An ExternalLocation is an association from a source symbol in a binary view to a target symbol and/or address in an ExternalLibrary. + """ def __init__(self, handle: core.BNExternalLocation): self._handle = handle @@ -70,50 +91,94 @@ class ExternalLocation: core.BNFreeExternalLocation(self._handle) def __repr__(self) -> str: - return f'<ExternalLocation: {self.internal_symbol}>' + return f'<ExternalLocation: {self.source_symbol}>' def __str__(self) -> str: - return f'<ExternalLocation: {self.internal_symbol}>' + return f'<ExternalLocation: {self.source_symbol}>' @property - def internal_symbol(self) -> 'types.CoreSymbol': - sym = core.BNExternalLocationGetInternalSymbol(self._handle) - assert sym is not None, "core.BNExternalLocationGetInternalSymbol returned None" + def source_symbol(self) -> 'types.CoreSymbol': + """ + Get the source symbol for this ExternalLocation + + :return: The source symbol for this ExternalLocation + """ + sym = core.BNExternalLocationGetSourceSymbol(self._handle) + assert sym is not None, "core.BNExternalLocationGetSourceSymbol returned None" return types.CoreSymbol(sym) @property - def has_address(self) -> bool: - return core.BNExternalLocationHasAddress(self._handle) + def has_target_address(self) -> bool: + """ + Check if this ExternalLocation has a target address + + :return: True is this ExternalLocation has a target address, False otherwise + """ + return core.BNExternalLocationHasTargetAddress(self._handle) @property - def has_symbol(self) -> bool: - return core.BNExternalLocationHasSymbol(self._handle) + def has_target_symbol(self) -> bool: + """ + Check if this ExternalLocation has a target symbol + + :return: True is this ExternalLocation has a target symbol, False otherwise + """ + return core.BNExternalLocationHasTargetSymbol(self._handle) @property - def address(self) -> Optional[int]: - if not self.has_address: + def target_address(self) -> Optional[int]: + """ + Get the address pointed to by this ExternalLocation + + :return: The address pointed to by this ExternalLocation if one exists, None otherwise + """ + if not self.has_target_address: return None - return core.BNExternalLocationGetAddress(self._handle) + return core.BNExternalLocationGetTargetAddress(self._handle) + + @target_address.setter + def target_address(self, new_address: Optional[int]) -> bool: + """ + Set the address pointed to by this ExternalLocation. + ExternalLocations must have a valid target address and/or symbol set. - @address.setter - def address(self, new_address: Optional[int]): + :param new_address: The address that this ExternalLocation will point to + :return: True if the address was set, False otherwise + """ c_addr = None if new_address is not None: c_addr = ctypes.c_ulonglong(new_address) - return core.BNExternalLocationSetAddress(self._handle, c_addr) + return core.BNExternalLocationSetTargetAddress(self._handle, c_addr) @property - def symbol(self) -> Optional[str]: - if not self.has_symbol: + def target_symbol(self) -> Optional[str]: + """ + Get the symbol pointed to by this ExternalLocation + + :return: The symbol pointed to by this ExternalLocation if one exists, None otherwise + """ + if not self.has_target_symbol: return None - return core.BNExternalLocationGetSymbol(self._handle) + return core.BNExternalLocationGetTargetSymbol(self._handle) - @symbol.setter - def symbol(self, new_symbol: Optional[str]): - return core.BNExternalLocationSetSymbol(self._handle, new_symbol) + @target_symbol.setter + def target_symbol(self, new_symbol: Optional[str]) -> bool: + """ + Set the symbol pointed to by this ExternalLocation. + ExternalLocations must have a valid target address and/or symbol set. + + :param new_symbol: The raw symbol that this ExternalLocation will point to + :return: True if the symbol was set, False otherwise + """ + return core.BNExternalLocationSetTargetSymbol(self._handle, new_symbol) @property def library(self) -> Optional[ExternalLibrary]: + """ + Get the ExternalLibrary that this ExternalLocation targets + + :return: The ExternalLibrary pointed in to by this ExternalLocation if one exists, None otherwise + """ handle = core.BNExternalLocationGetExternalLibrary(self._handle) if handle is None: return None @@ -121,5 +186,10 @@ class ExternalLocation: @library.setter def library(self, new_library: Optional[ExternalLibrary]): + """ + Set the ExternalLibrary that this ExternalLocation targets + + :param new_library: The ExternalLibrary that this ExternalLocation will pointed in to + """ lib_handle = new_library._handle if new_library is not None else None return core.BNExternalLocationSetExternalLibrary(self._handle, lib_handle) |
