From aa37d7780ab70531d9821ecad4aa33ea550b7ffc Mon Sep 17 00:00:00 2001 From: Jordan Wiens Date: Thu, 6 May 2021 08:52:45 -0400 Subject: adding get_function_by_name and plural API to bv --- python/binaryview.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'python') 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")) + + >>> + """ + 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")) + + >>> + """ + 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``: -- cgit v1.3.1