about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFabrice Desclaux <fabrice.desclaux@cea.fr>2014-10-31 10:49:20 +0100
committerFabrice Desclaux <fabrice.desclaux@cea.fr>2014-10-31 10:49:20 +0100
commit8e7d594ecbb94ff06dc0bd34f713950c9129f198 (patch)
tree3a32d9a8f33cb183934fe244f8d097c328fcac5c
parent63e618f17434fec8236d0410ac11b16469e6f771 (diff)
downloadmiasm-8e7d594ecbb94ff06dc0bd34f713950c9129f198.tar.gz
miasm-8e7d594ecbb94ff06dc0bd34f713950c9129f198.zip
X86/Jit: support api for 64 bit emulation
-rw-r--r--miasm2/arch/x86/jit.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/miasm2/arch/x86/jit.py b/miasm2/arch/x86/jit.py
index e448e68b..36afcce5 100644
--- a/miasm2/arch/x86/jit.py
+++ b/miasm2/arch/x86/jit.py
@@ -168,3 +168,67 @@ class jitter_x86_64(jitter):
     def init_run(self, *args, **kwargs):
         jitter.init_run(self, *args, **kwargs)
         self.cpu.RIP = self.pc
+
+    def func_args_stdcall(self, n_args):
+        args_regs = ['RCX', 'RDX', 'R8', 'R9']
+        ret_ad = self.pop_uint64_t()
+
+        args = []
+        for i in xrange(min(n_args, 4)):
+            args.append(self.cpu.get_gpreg()[args_regs[i]])
+        for i in xrange(max(0, n_args - 4)):
+            args.append(self.get_stack_arg(i))
+
+        log.debug('%s %s %s' % (whoami(), hex(ret_ad), [hex(x) for x in args]))
+        return ret_ad, args
+
+    def func_ret_stdcall(self, ret_addr, ret_value=None):
+        self.pc = self.cpu.RIP = ret_addr
+        if ret_value is not None:
+            self.cpu.RAX = ret_value
+        return True
+
+    def func_args_cdecl(self, n_args):
+        args_regs = ['RCX', 'RDX', 'R8', 'R9']
+        ret_ad = self.pop_uint64_t()
+
+        args = []
+        for i in xrange(min(n_args, 4)):
+            args.append(self.cpu.get_gpreg()[args_regs[i]])
+        for i in xrange(max(0, n_args - 4)):
+            args.append(self.get_stack_arg(i))
+
+        log.debug('%s %s %s' % (whoami(), hex(ret_ad), [hex(x) for x in args]))
+        return ret_ad, args
+
+    def func_ret_cdecl(self, ret_addr, ret_value=None):
+        self.pc = self.cpu.RIP = ret_addr
+        if ret_value is not None:
+            self.cpu.RAX = ret_value
+        return True
+
+    def add_lib_handler(self, libs, user_globals=None):
+        """Add a function to handle libs call with breakpoints
+        @libs: libimp instance
+        @user_globals: dictionnary for defined user function
+        """
+        if user_globals is None:
+            user_globals = {}
+
+        from miasm2.os_dep import win_api_x86_32
+
+        def handle_lib(jitter):
+            fname = libs.fad2cname[jitter.pc]
+            if fname in user_globals:
+                f = user_globals[fname]
+            elif fname in win_api_x86_32.__dict__:
+                f = win_api_x86_32.__dict__[fname]
+            else:
+                log.debug('%s' % repr(fname))
+                raise ValueError('unknown api', hex(jitter.pop_uint64_t()), repr(fname))
+            f(jitter)
+            jitter.pc = getattr(jitter.cpu, jitter.ir_arch.pc.name)
+            return True
+
+        for f_addr in libs.fad2cname:
+            self.add_breakpoint(f_addr, handle_lib)