@@ -80,32 +80,36 @@ def __init__(self, ql: Qiling):
8080 }
8181
8282 self .__syscall_id_reg = {
83- QL_ARCH .ARM64 : UC_ARM64_REG_X8 ,
84- QL_ARCH .ARM : UC_ARM_REG_R7 ,
85- QL_ARCH .MIPS : UC_MIPS_REG_V0 ,
86- QL_ARCH .X86 : UC_X86_REG_EAX ,
87- QL_ARCH .X8664 : UC_X86_REG_RAX ,
88- QL_ARCH .RISCV : UC_RISCV_REG_A7 ,
89- QL_ARCH .RISCV64 : UC_RISCV_REG_A7
83+ QL_ARCH .ARM64 : UC_ARM64_REG_X8 ,
84+ QL_ARCH .ARM : UC_ARM_REG_R7 ,
85+ QL_ARCH .MIPS : UC_MIPS_REG_V0 ,
86+ QL_ARCH .X86 : UC_X86_REG_EAX ,
87+ QL_ARCH .X8664 : UC_X86_REG_RAX ,
88+ QL_ARCH .RISCV : UC_RISCV_REG_A7 ,
89+ QL_ARCH .RISCV64 : UC_RISCV_REG_A7
9090 }[self .ql .arch .type ]
9191
92- # handle a special case
92+ # handle some special cases
9393 if (self .ql .arch .type == QL_ARCH .ARM64 ) and (self .ql .ostype == QL_OS .MACOS ):
9494 self .__syscall_id_reg = UC_ARM64_REG_X16
95- if (self .ql .arch .type == QL_ARCH .ARM ) and (self .ql .ostype == QL_OS .QNX ):
95+
96+ elif (self .ql .arch .type == QL_ARCH .ARM ) and (self .ql .ostype == QL_OS .QNX ):
9697 self .__syscall_id_reg = UC_ARM_REG_R12
9798
9899 # TODO: use abstract to access __syscall_cc and __syscall_id_reg by defining a system call class
99100 self .__syscall_cc : QlCC = {
100- QL_ARCH .ARM64 : aarch64 ,
101- QL_ARCH .ARM : aarch32 ,
102- QL_ARCH .MIPS : mipso32 ,
103- QL_ARCH .X86 : intel32 ,
104- QL_ARCH .X8664 : intel64 ,
105- QL_ARCH .RISCV : riscv32 ,
106- QL_ARCH .RISCV64 : riscv64 ,
101+ QL_ARCH .ARM64 : aarch64 ,
102+ QL_ARCH .ARM : aarch32 ,
103+ QL_ARCH .MIPS : mipso32 ,
104+ QL_ARCH .X86 : intel32 ,
105+ QL_ARCH .X8664 : intel64 ,
106+ QL_ARCH .RISCV : riscv32 ,
107+ QL_ARCH .RISCV64 : riscv64
107108 }[self .ql .arch .type ](self .ql .arch )
108109
110+ # select syscall mapping function based on emulated OS and architecture
111+ self .syscall_mapper = ql_syscall_mapping_function (self .ql .ostype , self .ql .arch .type )
112+
109113 self ._fd = QlFileDes ()
110114
111115 # the QlOs constructor cannot assign the standard streams using their designated properties since
@@ -141,10 +145,6 @@ def root(self, enabled: bool) -> None:
141145 self .euid = 0 if enabled else self .uid
142146 self .egid = 0 if enabled else self .gid
143147
144- @property
145- def syscall (self ):
146- return self .get_syscall ()
147-
148148 def set_syscall (self , target : Union [int , str ], handler : Callable , intercept : QL_INTERCEPT = QL_INTERCEPT .CALL ):
149149 """Either hook or replace a system call with a custom one.
150150
@@ -178,10 +178,8 @@ def getNameFromErrorCode(ret: int) -> str:
178178 return f'{ ret :#x} { f" ({ errors [- ret ]} )" if - ret in errors else f"" } '
179179
180180 def load_syscall (self ):
181- # import syscall mapping function
182- map_syscall = ql_syscall_mapping_function (self .ql .ostype )
183- syscall_id = self .syscall
184- syscall_name = map_syscall (self .ql , syscall_id )
181+ syscall_id = self .get_syscall ()
182+ syscall_name = self .syscall_mapper (syscall_id )
185183
186184 # get syscall on-enter hook (if any)
187185 hooks_dict = self .posix_syscall_hooks [QL_INTERCEPT .ENTER ]
@@ -196,14 +194,14 @@ def load_syscall(self):
196194 syscall_hook = hooks_dict .get (syscall_name ) or hooks_dict .get (syscall_id )
197195
198196 if not syscall_hook :
199- osname = ostype_convert_str (self .ql .ostype )
200- os_syscalls = ql_get_module_function (f"qiling.os.{ osname .lower ()} " , "syscall" )
201- posix_syscalls = ql_get_module_function (f"qiling.os.posix" , "syscall" )
197+ def __get_os_module (osname : str ):
198+ return ql_get_module_function (f'qiling.os.{ osname .lower ()} ' , 'syscall' )
199+
200+ os_syscalls = __get_os_module (ostype_convert_str (self .ql .ostype ))
201+ posix_syscalls = __get_os_module ('posix' )
202202
203203 # look in os-specific and posix syscall hooks
204- if syscall_name :
205- self .ql .log .debug ("syscall hooked 0x%x: %s()" % (self .ql .arch .regs .arch_pc , syscall_name ))
206- syscall_hook = getattr (os_syscalls , syscall_name , None ) or getattr (posix_syscalls , syscall_name , None )
204+ syscall_hook = getattr (os_syscalls , syscall_name , None ) or getattr (posix_syscalls , syscall_name , None )
207205
208206 if syscall_hook :
209207 syscall_name = syscall_hook .__name__
0 commit comments