3
3
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
4
4
#
5
5
6
+ from typing import MutableSequence
6
7
from .const import *
7
8
from qiling .const import *
8
9
9
10
10
- def _invert_dict (d ) :
11
+ def _invert_dict (d : Mapping ) -> Mapping :
11
12
return { v :k for k , v in d .items ()}
12
13
13
14
14
- def _constant_mapping (bits , d_map , ret = None , single_mapping = False ):
15
- if ret is None :
16
- ret = []
17
-
15
+ def _constant_mapping (bits : int , d_map : Mapping [str , int ], ret : MutableSequence [str ] = [], single_mapping : bool = False ) -> str :
18
16
b_map = _invert_dict (d_map )
19
17
20
18
if single_mapping :
21
19
return b_map [bits ]
22
20
23
21
for val , sym in b_map .items ():
24
- if val & bits != 0 :
22
+ if val & bits :
25
23
bits ^= val
26
24
ret .append (sym )
27
25
28
- if bits != 0 :
29
- ret .append (str ( bits ) )
26
+ if bits :
27
+ ret .append (f' { bits :#x } ' )
30
28
31
29
return " | " .join (ret )
32
30
@@ -227,13 +225,13 @@ def open_flags_mapping(flags, arch):
227
225
arm64_open_flags = arm_open_flags
228
226
229
227
mapping_dict = {
230
- QL_ARCH .X86 : linux_open_flags ,
231
- QL_ARCH .X8664 : linux_open_flags ,
232
- QL_ARCH .ARM : arm_open_flags ,
233
- QL_ARCH .ARM64 : arm64_open_flags ,
234
- QL_ARCH .MIPS : mips_open_flags ,
235
- QL_OS .MACOS : mac_open_flags ,
236
- }. get ( arch )
228
+ QL_ARCH .X86 : linux_open_flags ,
229
+ QL_ARCH .X8664 : linux_open_flags ,
230
+ QL_ARCH .ARM : arm_open_flags ,
231
+ QL_ARCH .ARM64 : arm64_open_flags ,
232
+ QL_ARCH .MIPS : mips_open_flags ,
233
+ QL_OS .MACOS : mac_open_flags ,
234
+ }[ arch ]
237
235
238
236
ret = ["O_RDONLY" ]
239
237
@@ -270,50 +268,56 @@ def mmap_prot_mapping(prots):
270
268
'PROT_READ' : 0x1 ,
271
269
'PROT_WRITE' : 0x2 ,
272
270
'PROT_EXEC' : 0x4 ,
271
+
272
+ # not supported by unicorn
273
+ 'PROT_GROWSDOWN' : 0x01000000 ,
274
+ 'PROT_GROWSUP' : 0x02000000
273
275
}
274
276
275
277
return _constant_mapping (prots , mmap_prots )
276
278
277
279
278
280
def socket_type_mapping (t , arch ):
279
281
socket_type_map = {
280
- QL_ARCH .X86 : linux_socket_types ,
281
- QL_ARCH .X8664 : linux_socket_types ,
282
- QL_ARCH .ARM : arm_socket_types ,
283
- QL_ARCH .ARM_THUMB : arm_socket_types ,
284
- QL_ARCH .ARM64 : arm_socket_types ,
285
- QL_ARCH .MIPS : mips_socket_types ,
286
- QL_OS .MACOS : linux_socket_types ,
287
- }.get (arch )
282
+ QL_ARCH .X86 : linux_socket_types ,
283
+ QL_ARCH .X8664 : linux_socket_types ,
284
+ QL_ARCH .ARM : arm_socket_types ,
285
+ QL_ARCH .ARM_THUMB : arm_socket_types ,
286
+ QL_ARCH .ARM64 : arm_socket_types ,
287
+ QL_ARCH .MIPS : mips_socket_types ,
288
+ QL_OS .MACOS : linux_socket_types ,
289
+ }[arch ]
290
+
288
291
# https://code.woboq.org/linux/linux/net/socket.c.html#1363
289
292
t &= SOCK_TYPE_MASK
293
+
290
294
return _constant_mapping (t , socket_type_map , single_mapping = True )
291
295
292
296
293
297
def socket_domain_mapping (p , arch ):
294
298
socket_domain_map = {
295
- QL_ARCH .X86 : linux_socket_domain ,
296
- QL_ARCH .X8664 : linux_socket_domain ,
297
- QL_ARCH .ARM : arm_socket_domain ,
298
- QL_ARCH .ARM_THUMB : arm_socket_domain ,
299
- QL_ARCH .ARM64 : arm_socket_domain ,
300
- QL_ARCH .MIPS : mips_socket_domain ,
301
- QL_OS .MACOS : macos_socket_domain ,
302
- }. get ( arch )
299
+ QL_ARCH .X86 : linux_socket_domain ,
300
+ QL_ARCH .X8664 : linux_socket_domain ,
301
+ QL_ARCH .ARM : arm_socket_domain ,
302
+ QL_ARCH .ARM_THUMB : arm_socket_domain ,
303
+ QL_ARCH .ARM64 : arm_socket_domain ,
304
+ QL_ARCH .MIPS : mips_socket_domain ,
305
+ QL_OS .MACOS : macos_socket_domain ,
306
+ }[ arch ]
303
307
304
308
return _constant_mapping (p , socket_domain_map , single_mapping = True )
305
309
306
310
307
311
def socket_level_mapping (t , arch ):
308
312
socket_level_map = {
309
- QL_ARCH .X86 : linux_socket_level ,
310
- QL_ARCH .X8664 : linux_socket_level ,
311
- QL_ARCH .ARM : arm_socket_level ,
312
- QL_ARCH .ARM_THUMB : arm_socket_level ,
313
- QL_ARCH .ARM64 : arm_socket_level ,
314
- QL_ARCH .MIPS : mips_socket_level ,
315
- QL_OS .MACOS : linux_socket_level ,
316
- }. get ( arch )
313
+ QL_ARCH .X86 : linux_socket_level ,
314
+ QL_ARCH .X8664 : linux_socket_level ,
315
+ QL_ARCH .ARM : arm_socket_level ,
316
+ QL_ARCH .ARM_THUMB : arm_socket_level ,
317
+ QL_ARCH .ARM64 : arm_socket_level ,
318
+ QL_ARCH .MIPS : mips_socket_level ,
319
+ QL_OS .MACOS : linux_socket_level ,
320
+ }[ arch ]
317
321
318
322
return _constant_mapping (t , socket_level_map , single_mapping = True )
319
323
@@ -327,20 +331,20 @@ def socket_ip_option_mapping(t, arch):
327
331
QL_ARCH .ARM64 : linux_socket_ip_options ,
328
332
QL_ARCH .MIPS : mips_socket_ip_options ,
329
333
QL_OS .MACOS : macos_socket_ip_options ,
330
- }. get ( arch )
334
+ }[ arch ]
331
335
332
336
return _constant_mapping (t , socket_option_map , single_mapping = True )
333
337
334
338
335
339
def socket_option_mapping (t , arch ):
336
340
socket_option_map = {
337
- QL_ARCH .X86 : linux_socket_options ,
338
- QL_ARCH .X8664 : linux_socket_options ,
339
- QL_ARCH .ARM : arm_socket_options ,
340
- QL_ARCH .ARM_THUMB : arm_socket_options ,
341
- QL_ARCH .ARM64 : arm_socket_options ,
342
- QL_ARCH .MIPS : mips_socket_options ,
343
- QL_OS .MACOS : linux_socket_options ,
344
- }. get ( arch )
341
+ QL_ARCH .X86 : linux_socket_options ,
342
+ QL_ARCH .X8664 : linux_socket_options ,
343
+ QL_ARCH .ARM : arm_socket_options ,
344
+ QL_ARCH .ARM_THUMB : arm_socket_options ,
345
+ QL_ARCH .ARM64 : arm_socket_options ,
346
+ QL_ARCH .MIPS : mips_socket_options ,
347
+ QL_OS .MACOS : linux_socket_options ,
348
+ }[ arch ]
345
349
346
350
return _constant_mapping (t , socket_option_map , single_mapping = True )
0 commit comments