diff options
| author | Jordan Wiens <jordan@psifertex.com> | 2021-05-06 08:52:45 -0400 |
|---|---|---|
| committer | Jordan Wiens <jordan@psifertex.com> | 2021-05-06 10:02:04 -0400 |
| commit | aa37d7780ab70531d9821ecad4aa33ea550b7ffc (patch) | |
| tree | fb4c529f6c6ea6a5d027e8cf8d287a8402fd19fb /python/binaryview.py | |
| parent | 7e343913d3a30b3a169040131c2a7a23ff5f5efb (diff) | |
adding get_function_by_name and plural API to bv
Diffstat (limited to 'python/binaryview.py')
| -rw-r--r-- | python/binaryview.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/python/binaryview.py b/python/binaryview.py index 5adbff87..6fe0869f 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -3225,6 +3225,64 @@ class BinaryView(object): result = [func for func in result in func.platform == plat] return result + def get_function_by_name(self, name, plat=None, precedence=[SymbolType.FunctionSymbol, SymbolType.ImportedFunctionSymbol, SymbolType.LibraryFunctionSymbol] ): + """``get_function_by_name`` returns a Function object for the first + function with a Symbol of ``name``. In instances where there + may be multiple function symbols with the same name, one instance + will be returned, but the order is not guaranteed. When multiple symbols + exist, setting the optional precedence list can guarantee particular + symbol types if they exist. + + :param int name: name of the function + :param Platform plat: Optional platform + :param list(SymbolType) precedence: Optional list of symbols expressing the desired precedence + :return: returns a Function object or None + :rtype: Function + :Example: + + >>> bv.get_function_by_name("main")) + <func: x86_64@0x1587> + >>> + """ + syms = self.get_symbols_by_name(name) + if len(syms) == 0: + return None + for symtype in precedence: + for sym in syms: + if (sym.type == symtype and self.get_function_at(sym.address, plat)): + return self.get_function_at(sym.address, plat) + if plat == None: + plat = self.platform + for sym in syms: + for fn in self.get_functions_at(sym.address): + if fn.platfrom == plat: + return fn + return None + + def get_functions_by_name(self, name, plat=None): + """``get_functions_by_name`` returns a list of Function objects + function with a Symbol of ``name``. + + :param int name: name of the functions + :param Platform plat: Optional platform + :return: returns a list of Function objects or an empty list + :rtype: list(Function) + :Example: + + >>> bv.get_function_by_name("main")) + <func: x86_64@0x1587> + >>> + """ + if plat == None: + plat = self.platform + syms = self.get_symbols_by_name(name) + fns = [] + for sym in syms: + for fn in self.get_functions_at(sym.address): + if fn.platform == plat: + fns.append(fn) + return fns + def get_function_at(self, addr, plat=None): """ ``get_function_at`` gets a Function object for the function that starts at virtual address ``addr``: |
