|
11 | 11 | from math import floor |
12 | 12 | import ctypes |
13 | 13 |
|
14 | | -class timespec(ctypes.Structure): |
15 | | - _fields_ = [ |
16 | | - ("tv_sec", ctypes.c_uint64), |
17 | | - ("tv_nsec", ctypes.c_int64) |
18 | | - ] |
| 14 | +def __get_timespec_struct(archbits: int): |
| 15 | + long = getattr(ctypes, f'c_int{archbits}') |
| 16 | + ulong = getattr(ctypes, f'c_uint{archbits}') |
19 | 17 |
|
20 | | - _pack_ = 8 |
| 18 | + class timespec(ctypes.Structure): |
| 19 | + _pack_ = archbits // 8 |
21 | 20 |
|
| 21 | + _fields_ = ( |
| 22 | + ('tv_sec', ulong), |
| 23 | + ('tv_nsec', long) |
| 24 | + ) |
22 | 25 |
|
23 | | -# Temporary dirty fix. |
24 | | -# TODO: Pack ctypes.Structure according to ql.arch.type and ql.ostype? |
25 | | -class timespec32(ctypes.Structure): |
26 | | - _fields_ = [ |
27 | | - ("tv_sec", ctypes.c_uint32), |
28 | | - ("tv_nsec", ctypes.c_int32) |
29 | | - ] |
| 26 | + return timespec |
| 27 | + |
| 28 | +def __get_timespec_obj(archbits: int): |
| 29 | + now = datetime.now().timestamp() |
| 30 | + |
| 31 | + tv_sec = floor(now) |
| 32 | + tv_nsec = floor((now - floor(now)) * 1e6) |
| 33 | + ts_cls = __get_timespec_struct(archbits) |
| 34 | + |
| 35 | + return ts_cls(tv_sec=tv_sec, tv_nsec=tv_nsec) |
30 | 36 |
|
31 | | - _pack_ = 4 |
32 | 37 |
|
33 | 38 | def ql_syscall_set_thread_area(ql: Qiling, u_info_addr: int): |
34 | 39 | if ql.arch.type == QL_ARCH.X86: |
@@ -62,39 +67,25 @@ def ql_syscall_set_thread_area(ql: Qiling, u_info_addr: int): |
62 | 67 | return 0 |
63 | 68 |
|
64 | 69 |
|
65 | | -def ql_syscall_set_tls(ql, address, *args, **kw): |
| 70 | +def ql_syscall_set_tls(ql: Qiling, address: int): |
66 | 71 | if ql.arch.type == QL_ARCH.ARM: |
67 | 72 | ql.arch.regs.c13_c0_3 = address |
68 | 73 | ql.mem.write_ptr(ql.arch.arm_get_tls_addr + 16, address, 4) |
69 | 74 | ql.arch.regs.r0 = address |
70 | 75 | ql.log.debug("settls(0x%x)" % address) |
71 | 76 |
|
72 | | -def ql_syscall_clock_gettime(ql, clock_gettime_clock_id, clock_gettime_timespec, *args, **kw): |
73 | | - now = datetime.now().timestamp() |
74 | | - tv_sec = floor(now) |
75 | | - tv_nsec = floor((now - floor(now)) * 1e6) |
76 | | - if ql.arch.type == QL_ARCH.X8664: |
77 | | - tp = timespec(tv_sec= tv_sec, tv_nsec=tv_nsec) |
78 | | - else: |
79 | | - tp = timespec32(tv_sec= tv_sec, tv_nsec=tv_nsec) |
80 | | - ql.mem.write(clock_gettime_timespec, bytes(tp)) |
81 | | - |
82 | | - ql.log.debug("clock_gettime(clock_id = %d, timespec = 0x%x)" % (clock_gettime_clock_id, clock_gettime_timespec)) |
83 | | - |
| 77 | +def ql_syscall_clock_gettime(ql: Qiling, clock_id: int, tp: int): |
| 78 | + ts_obj = __get_timespec_obj(ql.arch.bits) |
| 79 | + ql.mem.write(tp, bytes(ts_obj)) |
| 80 | + |
84 | 81 | return 0 |
85 | 82 |
|
86 | | -def ql_syscall_gettimeofday(ql, gettimeofday_tv, gettimeofday_tz, *args, **kw): |
87 | | - now = datetime.now().timestamp() |
88 | | - tv_sec = floor(now) |
89 | | - tv_nsec = floor((now - floor(now)) * 1e6) |
90 | | - if ql.arch.type == QL_ARCH.X8664: |
91 | | - tp = timespec(tv_sec= tv_sec, tv_nsec=tv_nsec) |
92 | | - else: |
93 | | - tp = timespec32(tv_sec= tv_sec, tv_nsec=tv_nsec) |
94 | | - |
95 | | - if gettimeofday_tv != 0: |
96 | | - ql.mem.write(gettimeofday_tv, bytes(tp)) |
97 | | - if gettimeofday_tz != 0: |
98 | | - ql.mem.write(gettimeofday_tz, b'\x00' * 8) |
99 | | - regreturn = 0 |
100 | | - return regreturn |
| 83 | +def ql_syscall_gettimeofday(ql: Qiling, tv: int, tz: int): |
| 84 | + if tv: |
| 85 | + ts_obj = __get_timespec_obj(ql.arch.bits) |
| 86 | + ql.mem.write(tv, bytes(ts_obj)) |
| 87 | + |
| 88 | + if tz: |
| 89 | + ql.mem.write(tz, b'\x00' * 8) |
| 90 | + |
| 91 | + return 0 |
0 commit comments