diff options
Diffstat (limited to '')
| -rw-r--r-- | miasm2/arch/aarch64/jit.py | 14 | ||||
| -rw-r--r-- | miasm2/arch/arm/jit.py | 20 | ||||
| -rw-r--r-- | miasm2/arch/x86/jit.py | 72 |
3 files changed, 79 insertions, 27 deletions
diff --git a/miasm2/arch/aarch64/jit.py b/miasm2/arch/aarch64/jit.py index 255bb91d..31570f52 100644 --- a/miasm2/arch/aarch64/jit.py +++ b/miasm2/arch/aarch64/jit.py @@ -37,7 +37,7 @@ class jitter_aarch64l(jitter): def func_args_stdcall(self, n_args): args = [] for i in xrange(min(n_args, self.max_reg_arg)): - args.append(self.cpu.get_gpreg()['X%d' % i]) + args.append(getattr(self.cpu, 'X%d' % i)) for i in xrange(max(0, n_args - self.max_reg_arg)): args.append(self.get_stack_arg(i)) ret_ad = self.cpu.LR @@ -56,6 +56,18 @@ class jitter_aarch64l(jitter): arg = self.get_stack_arg(index - self.max_reg_arg) return arg + def func_prepare_stdcall(self, ret_addr, *args): + for index in xrange(min(len(args), 4)): + setattr(self.cpu, 'X%d' % index, args[index]) + for index in xrange(4, len(args)): + self.vm.set_mem(self.cpu.SP + 8 * (index - 4), pck64(args[index])) + self.cpu.LR = ret_addr + + func_args_systemv = func_args_stdcall + func_ret_systemv = func_ret_stdcall + get_arg_n_systemv = get_arg_n_stdcall + func_prepare_systemv = func_prepare_stdcall + def init_run(self, *args, **kwargs): jitter.init_run(self, *args, **kwargs) self.cpu.PC = self.pc diff --git a/miasm2/arch/arm/jit.py b/miasm2/arch/arm/jit.py index 70c708e1..545d60de 100644 --- a/miasm2/arch/arm/jit.py +++ b/miasm2/arch/arm/jit.py @@ -34,11 +34,7 @@ class jitter_arml(jitter): @named_arguments def func_args_stdcall(self, n_args): - args = [] - for i in xrange(min(n_args, 4)): - args.append(self.cpu.get_gpreg()['R%d' % i]) - for i in xrange(max(0, n_args - 4)): - args.append(self.get_stack_arg(i)) + args = [self.get_arg_n_stdcall(i) for i in xrange(n_args)] ret_ad = self.cpu.LR return ret_ad, args @@ -48,13 +44,25 @@ class jitter_arml(jitter): self.cpu.R0 = ret_value return True + def func_prepare_stdcall(self, ret_addr, *args): + for index in xrange(min(len(args), 4)): + setattr(self.cpu, 'R%d' % index, args[index]) + for index in xrange(4, len(args)): + self.vm.set_mem(self.cpu.SP + 4 * (index - 4), pck32(args[index])) + self.cpu.LR = ret_addr + def get_arg_n_stdcall(self, index): if index < 4: - arg = self.cpu.get_gpreg()['R%d' % index] + arg = getattr(self.cpu, 'R%d' % index) else: arg = self.get_stack_arg(index-4) return arg + func_args_systemv = func_args_stdcall + func_ret_systemv = func_ret_stdcall + func_prepare_systemv = func_prepare_stdcall + get_arg_n_systemv = get_arg_n_stdcall + def init_run(self, *args, **kwargs): jitter.init_run(self, *args, **kwargs) self.cpu.PC = self.pc diff --git a/miasm2/arch/x86/jit.py b/miasm2/arch/x86/jit.py index cfdabf8c..ef1f162b 100644 --- a/miasm2/arch/x86/jit.py +++ b/miasm2/arch/x86/jit.py @@ -92,6 +92,10 @@ class jitter_x86_32(jitter): def get_stack_arg(self, index): return upck32(self.vm.get_mem(self.cpu.ESP + 4 * index, 4)) + def init_run(self, *args, **kwargs): + jitter.init_run(self, *args, **kwargs) + self.cpu.EIP = self.pc + # calling conventions # stdcall @@ -108,6 +112,13 @@ class jitter_x86_32(jitter): if ret_value2 is not None: self.cpu.EDX = ret_value2 + def func_prepare_stdcall(self, ret_addr, *args): + for arg in reversed(args): + self.push_uint32_t(arg) + self.push_uint32_t(ret_addr) + + get_arg_n_stdcall = get_stack_arg + # cdecl @named_arguments def func_args_cdecl(self, n_args): @@ -115,18 +126,24 @@ class jitter_x86_32(jitter): args = [self.get_stack_arg(i) for i in xrange(n_args)] return ret_ad, args - def func_ret_cdecl(self, ret_addr, ret_value): + def func_ret_cdecl(self, ret_addr, ret_value=None): self.cpu.EIP = ret_addr - self.cpu.EAX = ret_value + if ret_value is not None: + self.cpu.EAX = ret_value - def init_run(self, *args, **kwargs): - jitter.init_run(self, *args, **kwargs) - self.cpu.EIP = self.pc + get_arg_n_cdecl = get_stack_arg + + # System V + func_args_systemv = func_args_cdecl + func_ret_systemv = func_ret_cdecl + func_prepare_systemv = func_prepare_stdcall + get_arg_n_systemv = get_stack_arg class jitter_x86_64(jitter): C_Gen = x86_64_CGen + args_regs_systemv = ['RDI', 'RSI', 'RDX', 'RCX', 'R8', 'R9'] def __init__(self, *args, **kwargs): sp = asmblock.AsmSymbolPool() @@ -152,6 +169,13 @@ class jitter_x86_64(jitter): def get_stack_arg(self, index): return upck64(self.vm.get_mem(self.cpu.RSP + 8 * index, 8)) + def init_run(self, *args, **kwargs): + jitter.init_run(self, *args, **kwargs) + self.cpu.RIP = self.pc + + # calling conventions + + # stdcall @named_arguments def func_args_stdcall(self, n_args): args_regs = ['RCX', 'RDX', 'R8', 'R9'] @@ -169,23 +193,31 @@ class jitter_x86_64(jitter): self.cpu.RAX = ret_value return True + # cdecl + func_args_cdecl = func_args_stdcall + func_ret_cdecl = func_ret_stdcall + + # System V + + def get_arg_n_systemv(self, index): + args_regs = self.args_regs_systemv + if index < len(args_regs): + return getattr(self.cpu, args_regs[index]) + return self.get_stack_arg(index - len(args_regs)) + @named_arguments - def func_args_cdecl(self, n_args): - args_regs = ['RCX', 'RDX', 'R8', 'R9'] + def func_args_systemv(self, n_args): 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)) + args = [self.get_arg_n_systemv(index) for index in xrange(n_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 + func_ret_systemv = func_ret_cdecl - def init_run(self, *args, **kwargs): - jitter.init_run(self, *args, **kwargs) - self.cpu.RIP = self.pc + def func_prepare_systemv(self, ret_addr, *args): + args_regs = self.args_regs_systemv + self.push_uint64_t(ret_addr) + for i in xrange(min(len(args), len(args_regs))): + setattr(self.cpu, args_regs[i], args[i]) + remaining_args = args[len(args_regs):] + for arg in reversed(remaining_args): + self.push_uint64_t(arg) |