Rizin
unix-like reverse engineering framework and cli tools
emit_x86.c File Reference
#include <rz_egg.h>
#include <rz_types.h>

Go to the source code of this file.

Macros

#define attsyntax   0
 
#define EMIT_NAME   emit_x86
 
#define RZ_ARCH   "x86"
 
#define RZ_SZ   4
 
#define RZ_SP   "esp"
 
#define RZ_BP   "ebp"
 
#define RZ_AX   "eax"
 
#define SYSCALL_ATT   "int $0x80"
 
#define SYSCALL_INTEL   "int 0x80"
 
#define RZ_REG_AR_OFF   0
 
#define RZ_NGP   (sizeof(regs) / sizeof(char *))
 
#define BPOFF   (RZ_SZ - 4)
 
#define M32(x)   (unsigned int)((x)&0xffffffff)
 

Functions

static void emit_init (RzEgg *egg)
 
static char * emit_syscall (RzEgg *egg, int nargs)
 
static void emit_frame (RzEgg *egg, int sz)
 
static void emit_frame_end (RzEgg *egg, int sz, int ctx)
 
static void emit_comment (RzEgg *egg, const char *fmt,...)
 
static void emit_equ (RzEgg *egg, const char *key, const char *value)
 
static const char * getreg (int i)
 
static void emit_syscall_args (RzEgg *egg, int nargs)
 
static void emit_string (RzEgg *egg, const char *dstvar, const char *str, int j)
 
static void emit_call (RzEgg *egg, const char *str, int atr)
 
static void emit_jmp (RzEgg *egg, const char *str, int atr)
 
static void emit_arg (RzEgg *egg, int xs, int num, const char *str)
 
static void emit_get_result (RzEgg *egg, const char *ocn)
 
static void emit_restore_stack (RzEgg *egg, int size)
 
static void emit_get_while_end (RzEgg *egg, char *str, const char *ctxpush, const char *label)
 
static void emit_while_end (RzEgg *egg, const char *labelback)
 
static void emit_get_var (RzEgg *egg, int type, char *out, int idx)
 
static void emit_trap (RzEgg *egg)
 
static void emit_load_ptr (RzEgg *egg, const char *dst)
 
static void emit_branch (RzEgg *egg, char *b, char *g, char *e, char *n, int sz, const char *dst)
 
static void emit_load (RzEgg *egg, const char *dst, int sz)
 
static void emit_mathop (RzEgg *egg, int ch, int vs, int type, const char *eq, const char *p)
 
static const char * emit_regs (RzEgg *egg, int idx)
 
static void emit_get_ar (RzEgg *egg, char *out, int idx)
 

Variables

static char * regs [] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp" }
 
RzEggEmit EMIT_NAME
 

Macro Definition Documentation

◆ attsyntax

#define attsyntax   0

Definition at line 10 of file emit_x86.c.

◆ BPOFF

#define BPOFF   (RZ_SZ - 4)

◆ EMIT_NAME

#define EMIT_NAME   emit_x86

Definition at line 24 of file emit_x86.c.

◆ M32

#define M32 (   x)    (unsigned int)((x)&0xffffffff)

◆ RZ_ARCH

#define RZ_ARCH   "x86"

Definition at line 25 of file emit_x86.c.

◆ RZ_AX

#define RZ_AX   "eax"

Definition at line 29 of file emit_x86.c.

◆ RZ_BP

#define RZ_BP   "ebp"

Definition at line 28 of file emit_x86.c.

◆ RZ_NGP

#define RZ_NGP   (sizeof(regs) / sizeof(char *))

Definition at line 36 of file emit_x86.c.

◆ RZ_REG_AR_OFF

#define RZ_REG_AR_OFF   0

Definition at line 32 of file emit_x86.c.

◆ RZ_SP

#define RZ_SP   "esp"

Definition at line 27 of file emit_x86.c.

◆ RZ_SZ

#define RZ_SZ   4

Definition at line 26 of file emit_x86.c.

◆ SYSCALL_ATT

#define SYSCALL_ATT   "int $0x80"

Definition at line 30 of file emit_x86.c.

◆ SYSCALL_INTEL

#define SYSCALL_INTEL   "int 0x80"

Definition at line 31 of file emit_x86.c.

Function Documentation

◆ emit_arg()

static void emit_arg ( RzEgg egg,
int  xs,
int  num,
const char *  str 
)
static

Definition at line 265 of file emit_x86.c.

265  {
266  int d = atoi(str);
267  if (!attsyntax && (*str == '$')) {
268  str = str + 1;
269  }
270  switch (xs) {
271  case 0:
272 #ifdef ARCH_X86_64
273  /* push imm64 instruction not exist, it´s translated to:
274  mov rax, 0x0102030405060708
275  push rax
276  */
277  if (attsyntax) {
278  rz_egg_printf(egg, " mov %s, %%" RZ_AX "\n", str);
279  rz_egg_printf(egg, " push %%" RZ_AX "\n");
280  } else {
281  rz_egg_printf(egg, " mov " RZ_AX ", %s\n", str);
282  rz_egg_printf(egg, " push " RZ_AX "\n");
283  }
284 #else
285  rz_egg_printf(egg, " push %s\n", str);
286 #endif
287  break;
288  case '*':
289  if (attsyntax) {
290  rz_egg_printf(egg, " push (%s)\n", str);
291  } else {
292  rz_egg_printf(egg, " push [%s]\n", str);
293  }
294  break;
295  case '&':
296  if (attsyntax) {
297  if (d != 0) {
298  rz_egg_printf(egg, " addl $%d, %%" RZ_BP "\n", d);
299  }
300  rz_egg_printf(egg, " pushl %%" RZ_BP "\n");
301  if (d != 0) {
302  rz_egg_printf(egg, " subl $%d, %%" RZ_BP "\n", d);
303  }
304  } else {
305  if (d != 0) {
306  rz_egg_printf(egg, " add " RZ_BP ", %d\n", d);
307  }
308  rz_egg_printf(egg, " push " RZ_BP "\n");
309  if (d != 0) {
310  rz_egg_printf(egg, " sub " RZ_BP ", %d\n", d);
311  }
312  }
313  break;
314  }
315 }
RZ_API void rz_egg_printf(RzEgg *egg, const char *fmt,...)
Definition: egg.c:336
#define attsyntax
Definition: emit_x86.c:10
#define RZ_AX
Definition: emit_x86.c:29
#define RZ_BP
Definition: emit_x86.c:28
#define d(i)
Definition: sha256.c:44

References attsyntax, d, RZ_AX, RZ_BP, rz_egg_printf(), and cmd_descs_generate::str.

◆ emit_branch()

static void emit_branch ( RzEgg egg,
char *  b,
char *  g,
char *  e,
char *  n,
int  sz,
const char *  dst 
)
static

Definition at line 411 of file emit_x86.c.

411  {
412  char *p, str[64];
413  char *arg = NULL;
414  char *op = "jz";
415  int signed_value = 1; // XXX: add support for signed/unsigned variables
416  /* NOTE that jb/ja are inverted to fit cmp opcode */
417  if (b) {
418  *b = '\0';
419  if (signed_value) {
420  op = e ? "jge" : "jg";
421  } else {
422  op = e ? "jae" : "ja";
423  }
424  arg = b + 1;
425  } else if (g) {
426  *g = '\0';
427  if (signed_value) {
428  op = e ? "jle" : "jl";
429  } else {
430  op = e ? "jbe" : "jb";
431  }
432  arg = g + 1;
433  }
434  if (!arg) {
435  if (e) {
436  arg = e + 1;
437  op = "jne";
438  } else {
439  arg = attsyntax ? "$0" : "0";
440  if (n) {
441  op = "jnz";
442  } else {
443  op = "jz";
444  }
445  }
446  }
447 
448  if (*arg == '=') {
449  arg++; /* for <=, >=, ... */
450  }
451  p = rz_egg_mkvar(egg, str, arg, 0);
452  if (attsyntax) {
453  rz_egg_printf(egg, " pop %%" RZ_AX "\n"); /* TODO: add support for more than one arg get arg0 */
454  rz_egg_printf(egg, " cmp%c %s, %%" RZ_AX "\n", sz, p);
455  } else {
456  rz_egg_printf(egg, " pop " RZ_AX "\n"); /* TODO: add support for more than one arg get arg0 */
457  rz_egg_printf(egg, " cmp " RZ_AX ", %s\n", p);
458  }
459  // if (context>0)
460  free(p);
461  rz_egg_printf(egg, " %s %s\n", op, dst);
462 }
#define e(frag)
#define NULL
Definition: cris-opc.c:27
RZ_API char * rz_egg_mkvar(RzEgg *egg, char *out, const char *_str, int delta)
Definition: egg_lang.c:538
struct @667 g
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * p
Definition: libc.cpp:67
char * dst
Definition: lz4.h:724
int n
Definition: mipsasm.c:19
#define b(i)
Definition: sha256.c:42
Definition: dis.c:32

References attsyntax, b, dst, e, free(), g, n, NULL, p, RZ_AX, rz_egg_mkvar(), rz_egg_printf(), and cmd_descs_generate::str.

◆ emit_call()

static void emit_call ( RzEgg egg,
const char *  str,
int  atr 
)
static

Definition at line 237 of file emit_x86.c.

237  {
238  if (atr) {
239  if (attsyntax) {
240  rz_egg_printf(egg, " call *%s\n", str);
241  } else {
242  rz_egg_printf(egg, " call [%s]\n", str);
243  }
244  } else {
245  rz_egg_printf(egg, " call %s\n", str);
246  }
247 }

References attsyntax, rz_egg_printf(), and cmd_descs_generate::str.

◆ emit_comment()

static void emit_comment ( RzEgg egg,
const char *  fmt,
  ... 
)
static

Definition at line 112 of file emit_x86.c.

112  {
113  va_list ap;
114  char buf[1024];
115  va_start(ap, fmt);
116  vsnprintf(buf, sizeof(buf), fmt, ap);
117  if (attsyntax) {
118  rz_egg_printf(egg, " /* %s */\n", buf);
119  } else {
120  rz_egg_printf(egg, "# %s\n", buf);
121  }
122  va_end(ap);
123 }
voidpf void * buf
Definition: ioapi.h:138
vsnprintf
Definition: kernel.h:366

References attsyntax, rz_egg_printf(), and vsnprintf.

◆ emit_equ()

static void emit_equ ( RzEgg egg,
const char *  key,
const char *  value 
)
static

Definition at line 125 of file emit_x86.c.

125  {
126  rz_egg_printf(egg, ".equ %s,%s\n", key, value);
127 }
static int value
Definition: cmd_api.c:93
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118

References key, rz_egg_printf(), and value.

◆ emit_frame()

static void emit_frame ( RzEgg egg,
int  sz 
)
static

Definition at line 78 of file emit_x86.c.

78  {
79  if (sz < 1) {
80  return;
81  }
82  if (attsyntax) {
83  rz_egg_printf(egg,
84  " push %%" RZ_BP "\n"
85  " mov %%" RZ_SP ", %%" RZ_BP "\n"
86  " sub $%d, %%" RZ_SP "\n",
87  sz);
88  } else {
89  rz_egg_printf(egg,
90  " push " RZ_BP "\n"
91  " mov " RZ_BP ", " RZ_SP "\n"
92  " sub " RZ_SP ", %d\n",
93  sz);
94  }
95 }
#define RZ_SP
Definition: emit_x86.c:27

References attsyntax, RZ_BP, rz_egg_printf(), and RZ_SP.

◆ emit_frame_end()

static void emit_frame_end ( RzEgg egg,
int  sz,
int  ctx 
)
static

Definition at line 97 of file emit_x86.c.

97  {
98  if (sz > 0) {
99  if (attsyntax) {
100  rz_egg_printf(egg, " add $%d, %%" RZ_SP "\n", sz);
101  rz_egg_printf(egg, " pop %%" RZ_BP "\n");
102  } else {
103  rz_egg_printf(egg, " add " RZ_SP ", %d\n", sz);
104  rz_egg_printf(egg, " pop " RZ_BP "\n");
105  }
106  }
107  if (ctx > 0) {
108  rz_egg_printf(egg, " ret\n");
109  }
110 }

References attsyntax, RZ_BP, rz_egg_printf(), and RZ_SP.

◆ emit_get_ar()

static void emit_get_ar ( RzEgg egg,
char *  out,
int  idx 
)
static

Definition at line 543 of file emit_x86.c.

543  {
544  const char *reg = emit_regs(egg, RZ_REG_AR_OFF + idx);
545 
546  if (reg) {
547  strcpy(out, reg);
548  }
549 }
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define RZ_REG_AR_OFF
Definition: emit_x86.c:32
static const char * emit_regs(RzEgg *egg, int idx)
Definition: emit_x86.c:539
#define reg(n)
int idx
Definition: setup.py:197

References emit_regs(), setup::idx, out, reg, and RZ_REG_AR_OFF.

◆ emit_get_result()

static void emit_get_result ( RzEgg egg,
const char *  ocn 
)
static

Definition at line 317 of file emit_x86.c.

317  {
318  if (attsyntax) {
319  rz_egg_printf(egg, " mov %%" RZ_AX ", %s\n", ocn);
320  } else {
321  rz_egg_printf(egg, " mov %s, " RZ_AX "\n", ocn);
322  }
323 }

References attsyntax, RZ_AX, and rz_egg_printf().

◆ emit_get_var()

static void emit_get_var ( RzEgg egg,
int  type,
char *  out,
int  idx 
)
static

Definition at line 352 of file emit_x86.c.

352  {
353  switch (type) {
354  case 0: /* variable */
355  if (idx > 0) {
356  sprintf(out, "[" RZ_BP "+%d]", idx);
357  } else if (idx < 0) {
358  sprintf(out, "[" RZ_BP "%d]", idx);
359  } else {
360  strcpy(out, "[" RZ_BP "]");
361  }
362  break;
363  case 1: /* argument */
364  // OMG WE CAN'T stuff found in relative address in stack in the stack
365  eprintf("WARNING: Using stack vars in naked functions\n");
366  idx = 8; // HACK to make arg0, arg4, ... work
367  if (idx > 0) {
368  sprintf(out, "[" RZ_SP "+%d]", idx);
369  } else if (idx < 0) {
370  sprintf(out, "[" RZ_SP "%d]", idx);
371  } else {
372  strcpy(out, "[" RZ_SP "]");
373  }
374  break;
375  case 2:
376  if (idx > 0) {
377  sprintf(out, "[" RZ_BP "+%d]", idx);
378  } else if (idx < 0) {
379  sprintf(out, "[" RZ_BP "%d]", idx);
380  } else {
381  strcpy(out, "[" RZ_BP "]");
382  }
383  break;
384  }
385 }
sprintf
Definition: kernel.h:365
int type
Definition: mipsasm.c:17
#define eprintf(x, y...)
Definition: rlcc.c:7

References eprintf, setup::idx, out, RZ_BP, RZ_SP, sprintf, and type.

◆ emit_get_while_end()

static void emit_get_while_end ( RzEgg egg,
char *  str,
const char *  ctxpush,
const char *  label 
)
static

Definition at line 333 of file emit_x86.c.

333  {
334  sprintf(str, " push %s\n jmp %s\n", ctxpush, label);
335 }
Definition: dis.h:35

References sprintf, and cmd_descs_generate::str.

◆ emit_init()

static void emit_init ( RzEgg egg)
static

Definition at line 38 of file emit_x86.c.

38  {
39  // TODO: add 'andb rsp, 0xf0'
40  if (attsyntax) {
41  rz_egg_printf(egg, "mov %%" RZ_SP ", %%" RZ_BP "\n");
42  } else {
43  rz_egg_printf(egg, "mov " RZ_BP ", " RZ_SP "\n");
44  }
45 }

References attsyntax, RZ_BP, rz_egg_printf(), and RZ_SP.

◆ emit_jmp()

static void emit_jmp ( RzEgg egg,
const char *  str,
int  atr 
)
static

Definition at line 249 of file emit_x86.c.

249  {
250  if (str) {
251  if (atr) {
252  if (attsyntax) {
253  rz_egg_printf(egg, " jmp *%s\n", str);
254  } else {
255  rz_egg_printf(egg, " jmp [%s]\n", str);
256  }
257  } else {
258  rz_egg_printf(egg, " jmp %s\n", str);
259  }
260  } else {
261  eprintf("Jump without destination\n");
262  }
263 }

References attsyntax, eprintf, rz_egg_printf(), and cmd_descs_generate::str.

◆ emit_load()

static void emit_load ( RzEgg egg,
const char *  dst,
int  sz 
)
static

Definition at line 464 of file emit_x86.c.

464  {
465  if (attsyntax) {
466  switch (sz) {
467  case 'l':
468  rz_egg_printf(egg, " movl %s, %%" RZ_AX "\n", dst);
469  rz_egg_printf(egg, " movl (%%" RZ_AX "), %%" RZ_AX "\n");
470  break;
471  case 'b':
472  rz_egg_printf(egg, " movl %s, %%" RZ_AX "\n", dst);
473  rz_egg_printf(egg, " movzb (%%" RZ_AX "), %%" RZ_AX "\n");
474  break;
475  default:
476  // TODO: unhandled?!?
477  rz_egg_printf(egg, " mov%c %s, %%" RZ_AX "\n", sz, dst);
478  rz_egg_printf(egg, " mov%c (%%" RZ_AX "), %%" RZ_AX "\n", sz);
479  }
480  } else {
481  switch (sz) {
482  case 'l':
483  rz_egg_printf(egg, " mov " RZ_AX ", %s\n", dst);
484  rz_egg_printf(egg, " mov " RZ_AX ", [" RZ_AX "]\n");
485  break;
486  case 'b':
487  rz_egg_printf(egg, " mov " RZ_AX ", %s\n", dst);
488  rz_egg_printf(egg, " movz " RZ_AX ", [" RZ_AX "]\n");
489  break;
490  default:
491  // TODO: unhandled?!?
492  rz_egg_printf(egg, " mov " RZ_AX ", %s\n", dst);
493  rz_egg_printf(egg, " mov " RZ_AX ", [" RZ_AX "]\n");
494  }
495  }
496 }

References attsyntax, dst, RZ_AX, and rz_egg_printf().

◆ emit_load_ptr()

static void emit_load_ptr ( RzEgg egg,
const char *  dst 
)
static

Definition at line 391 of file emit_x86.c.

391  {
392  int d = atoi(dst);
393  if (d == 0) { // hack to handle stackvarptrz
394  char *p = strchr(dst, '+');
395  if (p) {
396  d = atoi(p + 1);
397  }
398  }
399  // eprintf ("emit_load_ptr: HACK\n");
400  // XXX: 32/64bit care
401  // rz_egg_printf (egg, "# DELTA IS (%s)\n", dst);
402  if (attsyntax) {
403  rz_egg_printf(egg, " leal %d(%%" RZ_BP "), %%" RZ_AX "\n", d);
404  } else {
405  rz_egg_printf(egg, " lea " RZ_AX ", [" RZ_BP "+%d]\n", d);
406  }
407  // rz_egg_printf (egg, " movl %%"RZ_BP", %%"RZ_AX"\n");
408  // rz_egg_printf (egg, " addl $%d, %%"RZ_AX"\n", d);
409 }

References attsyntax, d, dst, p, RZ_AX, RZ_BP, and rz_egg_printf().

◆ emit_mathop()

static void emit_mathop ( RzEgg egg,
int  ch,
int  vs,
int  type,
const char *  eq,
const char *  p 
)
static

Definition at line 498 of file emit_x86.c.

498  {
499  char *op;
500  switch (ch) {
501  case '^': op = "xor"; break;
502  case '&': op = "and"; break;
503  case '|': op = "or"; break;
504  case '-': op = "sub"; break;
505  case '+': op = "add"; break;
506  case '*': op = "mul"; break;
507  case '/': op = "div"; break;
508  default: op = "mov"; break;
509  }
510  if (attsyntax) {
511  if (!eq) {
512  eq = "%" RZ_AX;
513  }
514  if (!p) {
515  p = "%" RZ_AX;
516  }
517  rz_egg_printf(egg, " %s%c %c%s, %s\n", op, vs, type, eq, p);
518  } else {
519  if (!eq) {
520  eq = RZ_AX;
521  }
522  if (!p) {
523  p = RZ_AX;
524  }
525  // TODO:
526 #if 0
527  eprintf ("TYPE = %c\n", type);
528  eprintf (" %s%c %c%s, %s\n", op, vs, type, eq, p);
529  eprintf (" %s %s, [%s]\n", op, p, eq);
530 #endif
531  if (type == '*') {
532  rz_egg_printf(egg, " %s %s, [%s]\n", op, p, eq);
533  } else {
534  rz_egg_printf(egg, " %s %s, %s\n", op, p, eq);
535  }
536  }
537 }
ut8 op
Definition: 6502dis.c:13

References attsyntax, eprintf, eq, op, p, RZ_AX, rz_egg_printf(), and type.

◆ emit_regs()

static const char* emit_regs ( RzEgg egg,
int  idx 
)
static

Definition at line 539 of file emit_x86.c.

539  {
540  return regs[idx % RZ_NGP];
541 }
static char * regs[]
Definition: emit_x86.c:33
#define RZ_NGP
Definition: emit_x86.c:36

References setup::idx, regs, and RZ_NGP.

Referenced by emit_get_ar().

◆ emit_restore_stack()

static void emit_restore_stack ( RzEgg egg,
int  size 
)
static

Definition at line 325 of file emit_x86.c.

325  {
326  if (attsyntax) {
327  rz_egg_printf(egg, " add $%d, %%" RZ_SP " /* args */\n", size);
328  } else {
329  rz_egg_printf(egg, " add " RZ_SP ", %d\n", size);
330  }
331 }
voidpf void uLong size
Definition: ioapi.h:138

References attsyntax, rz_egg_printf(), and RZ_SP.

◆ emit_string()

static void emit_string ( RzEgg egg,
const char *  dstvar,
const char *  str,
int  j 
)
static

Definition at line 159 of file emit_x86.c.

159  {
160  char *p, str2[64];
161  int i, oj = j;
162 
163  int len = strlen(str);
164  char *s = calloc(1, len + 8);
165  if (!s) {
166  return;
167  }
168  memcpy(s, str, len);
169  memset(s + len, 0, 4);
170 
171  /* XXX: Hack: Adjust offset in RZ_BP correctly for 64b addresses */
172 #define BPOFF (RZ_SZ - 4)
173 #define M32(x) (unsigned int)((x)&0xffffffff)
174  /* XXX: Assumes sizeof(ut32) == 4 */
175  for (i = 4; i <= oj; i += 4) {
176  /* XXX endian issues (non-portable asm) */
177  ut32 *n = (ut32 *)(s + i - 4);
178  p = rz_egg_mkvar(egg, str2, dstvar, i + BPOFF);
179  if (attsyntax) {
180  rz_egg_printf(egg, " movl $0x%x, %s\n", M32(*n), p);
181  } else {
182  rz_egg_printf(egg, " mov dword %s, 0x%x\n", p, M32(*n));
183  }
184  free(p);
185  j -= 4;
186  }
187 #undef M32
188 
189  /* zero */
190  p = rz_egg_mkvar(egg, str2, dstvar, i + BPOFF);
191  if (attsyntax) {
192  rz_egg_printf(egg, " movl $0, %s\n", p);
193  } else {
194  rz_egg_printf(egg, " mov dword %s, 0\n", p);
195  }
196  free(p);
197 
198  /* store pointer */
199  p = rz_egg_mkvar(egg, str2, dstvar, j + 4 + BPOFF);
200  if (attsyntax) {
201  rz_egg_printf(egg, " lea %s, %%" RZ_AX "\n", p);
202  } else {
203  rz_egg_printf(egg, " lea " RZ_AX ", %s\n", p);
204  }
205  free(p);
206 
207  p = rz_egg_mkvar(egg, str2, dstvar, 0);
208  if (attsyntax) {
209  rz_egg_printf(egg, " mov %%" RZ_AX ", %s\n", p);
210  } else {
211  rz_egg_printf(egg, " mov %s, " RZ_AX "\n", p);
212  }
213  free(p);
214 
215 #undef BPOFF
216 #if 0
217  char *p, str2[64];
218  int i, oj = j;
219  for (i=0; i<oj; i+=4) {
220  /* XXX endian and 32/64bit issues */
221  int *n = (int *)(str+i);
222  p = rz_egg_mkvar (egg, str2, dstvar, j);
223  if (attsyntax) rz_egg_printf (egg, " movl $0x%x, %s\n", *n, p);
224  else rz_egg_printf (egg, " mov %s, 0x%x\n", p, *n);
225  j -= 4;
226  }
227  p = rz_egg_mkvar (egg, str2, dstvar, oj);
228  if (attsyntax) rz_egg_printf (egg, " lea %s, %%"RZ_AX"\n", p);
229  else rz_egg_printf (egg, " lea "RZ_AX", %s\n", p);
230  p = rz_egg_mkvar (egg, str2, dstvar, 0);
231  if (attsyntax) rz_egg_printf (egg, " mov %%"RZ_AX", %s\n", p);
232  else rz_egg_printf (egg, " mov %s, "RZ_AX"\n", p);
233 #endif
234  free(s);
235 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
uint32_t ut32
#define BPOFF
#define M32(x)
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
static RzSocket * s
Definition: rtr.c:28

References attsyntax, BPOFF, calloc(), free(), i, len, M32, memcpy(), memset(), n, p, RZ_AX, rz_egg_mkvar(), rz_egg_printf(), s, and cmd_descs_generate::str.

◆ emit_syscall()

static char* emit_syscall ( RzEgg egg,
int  nargs 
)
static

Definition at line 47 of file emit_x86.c.

47  {
48  char p[512];
49  if (attsyntax) {
50  return strdup(": mov $`.arg`, %" RZ_AX "\n: " SYSCALL_ATT "\n");
51  }
52  switch (egg->os) {
53  case RZ_EGG_OS_LINUX:
54  strcpy(p, "\n : mov " RZ_AX ", `.arg`\n : " SYSCALL_INTEL "\n");
55  break;
56  case RZ_EGG_OS_OSX:
57  case RZ_EGG_OS_MACOS:
58  case RZ_EGG_OS_DARWIN:
59 #if ARCH_X86_64
60  snprintf(p, sizeof(p), "\n"
61  " : mov rax, `.arg`\n"
62  " : syscall\n");
63 #else
64  snprintf(p, sizeof(p), "\n"
65  " : mov eax, `.arg`\n"
66  " : push eax\n"
67  " : int 0x80\n"
68  " : add esp, %d\n",
69  4); //(nargs+2)*(egg->bits/8));
70 #endif
71  break;
72  default:
73  return NULL;
74  }
75  return strdup(p);
76 }
#define SYSCALL_INTEL
Definition: emit_x86.c:31
#define SYSCALL_ATT
Definition: emit_x86.c:30
snprintf
Definition: kernel.h:364
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
#define RZ_EGG_OS_DARWIN
Definition: rz_egg.h:129
#define RZ_EGG_OS_MACOS
Definition: rz_egg.h:132
#define RZ_EGG_OS_OSX
Definition: rz_egg.h:128
#define RZ_EGG_OS_LINUX
Definition: rz_egg.h:127
ut32 os
Definition: rz_egg.h:109

References attsyntax, NULL, rz_egg_t::os, p, RZ_AX, RZ_EGG_OS_DARWIN, RZ_EGG_OS_LINUX, RZ_EGG_OS_MACOS, RZ_EGG_OS_OSX, snprintf, strdup(), SYSCALL_ATT, and SYSCALL_INTEL.

◆ emit_syscall_args()

static void emit_syscall_args ( RzEgg egg,
int  nargs 
)
static

Definition at line 136 of file emit_x86.c.

136  {
137  int j, k;
138  for (j = 0; j < nargs; j++) {
139  k = j * RZ_SZ;
140  const char *reg = getreg(j + 1);
141  if (!reg) {
142  eprintf("Cannot find gpr %d\n", j + 1);
143  break;
144  }
145  if (attsyntax) {
146  rz_egg_printf(egg, " mov %d(%%" RZ_SP "), %%%s\n", k, reg);
147  } else {
148  if (k > 0) {
149  rz_egg_printf(egg, " mov %s, [" RZ_SP "+%d]\n", reg, k);
150  } else if (k < 0) {
151  rz_egg_printf(egg, " mov %s, [" RZ_SP "%d]\n", reg, k);
152  } else {
153  rz_egg_printf(egg, " mov %s, [" RZ_SP "]\n", reg);
154  }
155  }
156  }
157 }
const char * k
Definition: dsignal.c:11
#define RZ_SZ
Definition: emit_x86.c:26
static const char * getreg(int i)
Definition: emit_x86.c:129

References attsyntax, eprintf, getreg(), k, cmd_descs_generate::nargs, reg, rz_egg_printf(), RZ_SP, and RZ_SZ.

◆ emit_trap()

static void emit_trap ( RzEgg egg)
static

Definition at line 387 of file emit_x86.c.

387  {
388  rz_egg_printf(egg, " int3\n");
389 }

References rz_egg_printf().

◆ emit_while_end()

static void emit_while_end ( RzEgg egg,
const char *  labelback 
)
static

Definition at line 337 of file emit_x86.c.

337  {
338 #if 0
339  if (attsyntax) {
340  rz_egg_printf (egg, " pop %%"RZ_AX"\n");
341  rz_egg_printf (egg, " cmp $0, %%"RZ_AX"\n"); // XXX MUST SUPPORT != 0 COMPARE HERE
342  rz_egg_printf (egg, " jnz %s\n", labelback);
343  } else {
344 #endif
345  rz_egg_printf(egg, " pop " RZ_AX "\n");
346  rz_egg_printf(egg, " test " RZ_AX ", " RZ_AX "\n"); // XXX MUST SUPPORT != 0 COMPARE HERE
347  rz_egg_printf(egg, " jnz %s\n", labelback);
348  // }
349 }

References attsyntax, RZ_AX, and rz_egg_printf().

◆ getreg()

static const char* getreg ( int  i)
static

Definition at line 129 of file emit_x86.c.

129  {
130  if (i < 0 || i >= RZ_NGP) {
131  return NULL;
132  }
133  return regs[i];
134 }

References i, NULL, regs, and RZ_NGP.

Referenced by emit_syscall_args().

Variable Documentation

◆ EMIT_NAME

RzEggEmit EMIT_NAME
Initial value:
= {
.retvar = RZ_AX,
.arch = RZ_ARCH,
.size = RZ_SZ,
.init = emit_init,
.jmp = emit_jmp,
.call = emit_call,
.equ = emit_equ,
.regs = emit_regs,
.trap = emit_trap,
.frame = emit_frame,
.frame_end = emit_frame_end,
.comment = emit_comment,
.push_arg = emit_arg,
.restore_stack = emit_restore_stack,
.get_result = emit_get_result,
.syscall_args = emit_syscall_args,
.set_string = emit_string,
.get_ar = emit_get_ar,
.get_var = emit_get_var,
.while_end = emit_while_end,
.get_while_end = emit_get_while_end,
.branch = emit_branch,
.load = emit_load,
.load_ptr = emit_load_ptr,
.mathop = emit_mathop,
.syscall = emit_syscall,
}
static void emit_comment(RzEgg *egg, const char *fmt,...)
Definition: emit_x86.c:112
static void emit_branch(RzEgg *egg, char *b, char *g, char *e, char *n, int sz, const char *dst)
Definition: emit_x86.c:411
static char * emit_syscall(RzEgg *egg, int nargs)
Definition: emit_x86.c:47
static void emit_string(RzEgg *egg, const char *dstvar, const char *str, int j)
Definition: emit_x86.c:159
static void emit_jmp(RzEgg *egg, const char *str, int atr)
Definition: emit_x86.c:249
static void emit_call(RzEgg *egg, const char *str, int atr)
Definition: emit_x86.c:237
#define RZ_ARCH
Definition: emit_x86.c:25
static void emit_init(RzEgg *egg)
Definition: emit_x86.c:38
static void emit_frame_end(RzEgg *egg, int sz, int ctx)
Definition: emit_x86.c:97
static void emit_get_ar(RzEgg *egg, char *out, int idx)
Definition: emit_x86.c:543
static void emit_get_while_end(RzEgg *egg, char *str, const char *ctxpush, const char *label)
Definition: emit_x86.c:333
static void emit_trap(RzEgg *egg)
Definition: emit_x86.c:387
static void emit_restore_stack(RzEgg *egg, int size)
Definition: emit_x86.c:325
static void emit_while_end(RzEgg *egg, const char *labelback)
Definition: emit_x86.c:337
static void emit_get_var(RzEgg *egg, int type, char *out, int idx)
Definition: emit_x86.c:352
static void emit_load(RzEgg *egg, const char *dst, int sz)
Definition: emit_x86.c:464
static void emit_arg(RzEgg *egg, int xs, int num, const char *str)
Definition: emit_x86.c:265
static void emit_mathop(RzEgg *egg, int ch, int vs, int type, const char *eq, const char *p)
Definition: emit_x86.c:498
static void emit_syscall_args(RzEgg *egg, int nargs)
Definition: emit_x86.c:136
static void emit_get_result(RzEgg *egg, const char *ocn)
Definition: emit_x86.c:317
static void emit_frame(RzEgg *egg, int sz)
Definition: emit_x86.c:78
static void emit_load_ptr(RzEgg *egg, const char *dst)
Definition: emit_x86.c:391
static void emit_equ(RzEgg *egg, const char *key, const char *value)
Definition: emit_x86.c:125

Definition at line 551 of file emit_x86.c.

◆ regs

char* regs[] = { "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp" }
static

Definition at line 33 of file emit_x86.c.

Referenced by emit_regs(), and getreg().