diff --git a/qiling/os/posix/posix.py b/qiling/os/posix/posix.py index 496d0bad7..d71069ba0 100644 --- a/qiling/os/posix/posix.py +++ b/qiling/os/posix/posix.py @@ -26,7 +26,7 @@ from qiling.exception import QlErrorSyscallNotFound from qiling.os.os import QlOs from qiling.os.posix.const import NR_OPEN, errors -from qiling.utils import ql_get_module_function +from qiling.utils import ql_get_module, ql_get_module_function SYSCALL_PREF: str = f'ql_syscall_' diff --git a/qiling/utils.py b/qiling/utils.py index 68815734e..0f1fe42ed 100644 --- a/qiling/utils.py +++ b/qiling/utils.py @@ -12,6 +12,7 @@ import importlib, os from configparser import ConfigParser +from types import ModuleType from typing import TYPE_CHECKING, Any, Callable, Mapping, Optional, Tuple, TypeVar, Union from unicorn import UC_ERR_READ_UNMAPPED, UC_ERR_FETCH_UNMAPPED @@ -66,24 +67,23 @@ def debugger_convert(debugger: str) -> Optional[QL_DEBUGGER]: def arch_os_convert(arch: QL_ARCH) -> Optional[QL_OS]: return arch_os_map.get(arch) -# Call `function_name` in `module_name`. -# e.g. map_syscall in qiling.os.linux.map_syscall -def ql_get_module_function(module_name: str, function_name: str): - +def ql_get_module(module_name: str) -> ModuleType: try: - imp_module = importlib.import_module(module_name, 'qiling') - except ModuleNotFoundError: - raise QlErrorModuleNotFound(f'Unable to import module {module_name}') - except KeyError: + module = importlib.import_module(module_name, 'qiling') + except (ModuleNotFoundError, KeyError): raise QlErrorModuleNotFound(f'Unable to import module {module_name}') + return module + +def ql_get_module_function(module_name: str, member_name: str): + module = ql_get_module(module_name) + try: - module_function = getattr(imp_module, function_name) + member = getattr(module, member_name) except AttributeError: - raise QlErrorModuleFunctionNotFound(f'Unable to import {function_name} from {imp_module}') - - return module_function + raise QlErrorModuleFunctionNotFound(f'Unable to import {member_name} from {module_name}') + return member def __emu_env_from_pathname(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS], Optional[QL_ENDIAN]]: if os.path.isdir(path) and path.endswith('.kext'):