55
66from typing import Callable
77import os
8+
9+ from typing import Callable
810from unicorn import UcError
911
12+ from qiling import Qiling
1013from qiling .os .posix .posix import QlOsPosix
1114from qiling .os .qnx .const import NTO_SIDE_CHANNEL , SYSMGR_PID , SYSMGR_CHID , SYSMGR_COID
1215from qiling .os .qnx .helpers import QnxConn
1316from qiling .os .qnx .structs import _thread_local_storage
14- from qiling . os . fcall import QlFunctionCall
17+
1518from qiling .cc import QlCC , intel , arm , mips , riscv
1619from qiling .const import QL_ARCH , QL_INTERCEPT
20+ from qiling .os .fcall import QlFunctionCall
21+ from qiling .os .const import *
22+ from qiling .os .posix .const import NR_OPEN
23+ from qiling .os .posix .posix import QlOsPosix
1724
1825class QlOsQnx (QlOsPosix ):
19- def __init__ (self , ql ):
26+ def __init__ (self , ql : Qiling ):
2027 super (QlOsQnx , self ).__init__ (ql )
28+
29+ self .ql = ql
30+
31+ cc : QlCC = {
32+ QL_ARCH .X86 : intel .cdecl ,
33+ QL_ARCH .X8664 : intel .amd64 ,
34+ QL_ARCH .ARM : arm .aarch32 ,
35+ QL_ARCH .ARM64 : arm .aarch64 ,
36+ QL_ARCH .MIPS : mips .mipso32 ,
37+ QL_ARCH .RISCV : riscv .riscv ,
38+ QL_ARCH .RISCV64 : riscv .riscv ,
39+ }[ql .archtype ](ql )
40+
41+ self .fcall = QlFunctionCall (ql , cc )
42+
43+ self .thread_class = None
44+ self .futexm = None
45+ self .fh = None
46+ self .function_after_load_list = []
47+ self .elf_mem_start = 0x0
2148 self .load ()
2249
2350 cc : QlCC = {
@@ -48,11 +75,12 @@ def load(self):
4875 if self .ql .code :
4976 return
5077
51- if self .ql .archtype != QL_ARCH .ARM :
52- return
53-
54- self .ql .arch .enable_vfp ()
55- self .ql .hook_intno (self .hook_syscall , 2 )
78+ # ARM
79+ if self .ql .archtype == QL_ARCH .ARM :
80+ self .ql .arch .enable_vfp ()
81+ self .ql .hook_intno (self .hook_syscall , 2 )
82+ #self.thread_class = thread.QlLinuxARMThread
83+ self .ql .arch .init_get_tls ()
5684
5785
5886 def hook_syscall (self , intno = None , int = None ):
@@ -63,7 +91,17 @@ def add_function_hook(self, fn: str, cb: Callable, intercept: QL_INTERCEPT):
6391 self .ql .os .function_hook .add_function_hook (fn , cb , intercept )
6492
6593
66- def hook_sigtrap (self , intno = None , int = None ):
94+ def register_function_after_load (self , function ):
95+ if function not in self .function_after_load_list :
96+ self .function_after_load_list .append (function )
97+
98+
99+ def run_function_after_load (self ):
100+ for f in self .function_after_load_list :
101+ f ()
102+
103+
104+ def hook_sigtrap (self , intno = None , int = None ):
67105 self .ql .log .info ("Trap Found" )
68106 self .emu_error ()
69107 exit (1 )
@@ -76,15 +114,15 @@ def run(self):
76114 if self .ql .entry_point is not None :
77115 self .ql .loader .elf_entry = self .ql .entry_point
78116
79- self .cpupage_addr = int (self .ql .os .profile .get ("OS32" , "cpupage_address" ), 16 )
80- self .cpupage_tls_addr = int (self .ql .os .profile .get ("OS32" , "cpupage_tls_address" ), 16 )
81- self .tls_data_addr = int (self .ql .os .profile .get ("OS32" , "tls_data_address" ), 16 )
82-
83- self . syspage_addr = int (self .ql .os . profile . get ( "OS32" , "syspage_address" ), 16 )
117+ self .cpupage_addr = int (self .ql .os .profile .get ("OS32" , "cpupage_address" ), 16 )
118+ self .cpupage_tls_addr = int (self .ql .os .profile .get ("OS32" , "cpupage_tls_address" ), 16 )
119+ self .tls_data_addr = int (self .ql .os .profile .get ("OS32" , "tls_data_address" ), 16 )
120+ self . syspage_addr = int ( self . ql . os . profile . get ( "OS32" , "syspage_address" ), 16 )
121+ syspage_path = os . path . join (self .ql .rootfs , "syspage.bin" )
84122
85123 self .ql .mem .map (self .syspage_addr , 0x4000 , info = "[syspage_mem]" )
86124
87- syspage_path = os . path . join ( self . ql . rootfs , "syspage.bin" )
125+
88126 with open (syspage_path , "rb" ) as sp :
89127 self .ql .mem .write (self .syspage_addr , sp .read ())
90128
@@ -108,8 +146,13 @@ def run(self):
108146 self .ql .emu_start (self .entry_point , (self .entry_point + len (self .ql .code )), self .ql .timeout , self .ql .count )
109147 else :
110148 if self .ql .loader .elf_entry != self .ql .loader .entry_point :
111- self .ql .emu_start (self .ql .loader .entry_point , self .ql .loader .elf_entry , self .ql .timeout )
112- self .ql .enable_lib_patch ()
149+ entry_address = self .ql .loader .elf_entry
150+ if self .ql .archtype == QL_ARCH .ARM and entry_address & 1 == 1 :
151+ entry_address -= 1
152+ self .ql .emu_start (self .ql .loader .entry_point , entry_address , self .ql .timeout )
153+ self .run_function_after_load ()
154+ self .ql .loader .skip_exit_check = False
155+ self .ql .write_exit_trap ()
113156
114157 self .ql .emu_start (self .ql .loader .elf_entry , self .exit_point , self .ql .timeout , self .ql .count )
115158
0 commit comments