Rizin
unix-like reverse engineering framework and cli tools
rz-asm.c File Reference
#include <rz_analysis.h>
#include <rz_asm.h>
#include <rz_lib.h>
#include <rz_types.h>
#include <rz_util.h>
#include <stdio.h>
#include <string.h>
#include <rz_main.h>
#include <rz_core.h>

Go to the source code of this file.

Classes

struct  RzAsmState
 

Enumerations

enum  DisasmMode {
  DISASM_MODE_DONT = 0 , DISASM_MODE_DEFAULT , DISASM_MODE_WITH_BYTES , DISASM_MODE_ESIL ,
  DISASM_MODE_IL
}
 

Functions

static void __load_plugins (RzAsmState *as)
 
static void __as_set_archbits (RzAsmState *as)
 
static RzAsmState__as_new (void)
 
static void __as_free (RzAsmState *as)
 
static char * stackop2str (int type)
 
static void showanalysis (RzAsmState *as, RzAnalysisOp *op, ut64 offset, ut8 *buf, int len, PJ *pj)
 
static int show_analinfo (RzAsmState *as, const char *arg, ut64 offset)
 
static int rasm_show_help (int v)
 
static bool print_and_check_il (RzAsmState *as, RzAnalysisOp *op)
 
static int rasm_disasm (RzAsmState *as, ut64 addr, const char *buf, int len, int bits, int bin, DisasmMode mode)
 
static void print_buf (RzAsmState *as, char *str)
 
static bool print_label (void *user, const void *k, const void *v)
 
static int rasm_asm (RzAsmState *as, const char *buf, ut64 offset, ut64 len, int bits, int bin, bool use_spp, bool hexwords)
 
static int __lib_asm_cb (RzLibPlugin *pl, void *user, void *data)
 
static int __lib_analysis_cb (RzLibPlugin *pl, void *user, void *data)
 
static int print_assembly_output (RzAsmState *as, const char *buf, ut64 offset, ut64 len, int bits, int bin, bool use_spp, bool rad, bool hexwords, const char *arch)
 
RZ_API int rz_main_rz_asm (int argc, const char *argv[])
 

Enumeration Type Documentation

◆ DisasmMode

enum DisasmMode
Enumerator
DISASM_MODE_DONT 
DISASM_MODE_DEFAULT 
DISASM_MODE_WITH_BYTES 
DISASM_MODE_ESIL 
DISASM_MODE_IL 

Definition at line 219 of file rz-asm.c.

219  {
220  DISASM_MODE_DONT = 0,
225 } DisasmMode;
DisasmMode
Definition: rz-asm.c:219
@ DISASM_MODE_DONT
Definition: rz-asm.c:220
@ DISASM_MODE_WITH_BYTES
Definition: rz-asm.c:222
@ DISASM_MODE_ESIL
Definition: rz-asm.c:223
@ DISASM_MODE_DEFAULT
Definition: rz-asm.c:221
@ DISASM_MODE_IL
Definition: rz-asm.c:224

Function Documentation

◆ __as_free()

static void __as_free ( RzAsmState as)
static

Definition at line 51 of file rz-asm.c.

51  {
52  if (as->a) {
53  rz_num_free(as->a->num);
54  }
55  rz_asm_free(as->a);
57  rz_lib_free(as->l);
58  free(as);
59 }
RZ_API RzAnalysis * rz_analysis_free(RzAnalysis *a)
Definition: analysis.c:137
RZ_API void rz_asm_free(RzAsm *a)
Definition: asm.c:315
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_lib_free(RzLib *lib)
Definition: lib.c:189
RZ_API void rz_num_free(RzNum *num)
Definition: unum.c:87
RzAnalysis * analysis
Definition: rz-asm.c:19
RzLib * l
Definition: rz-asm.c:17
RzAsm * a
Definition: rz-asm.c:18
RzNum * num
Definition: rz_asm.h:114

References RzAsmState::a, RzAsmState::analysis, free(), RzAsmState::l, rz_asm_t::num, rz_analysis_free(), rz_asm_free(), rz_lib_free(), and rz_num_free().

Referenced by rz_main_rz_asm().

◆ __as_new()

static RzAsmState* __as_new ( void  )
static

Definition at line 36 of file rz-asm.c.

36  {
38  if (as) {
39  as->l = rz_lib_new(NULL, NULL);
40  as->a = rz_asm_new();
41  if (as->a) {
42  as->a->num = rz_num_new(NULL, NULL, NULL);
43  }
44  as->analysis = rz_analysis_new();
45  __load_plugins(as);
47  }
48  return as;
49 }
RZ_API RzAnalysis * rz_analysis_new(void)
Definition: analysis.c:63
RZ_API RzAsm * rz_asm_new(void)
Definition: asm.c:262
#define NULL
Definition: cris-opc.c:27
static void __load_plugins(RzAsmState *as)
Definition: rz-asm.c:479
static void __as_set_archbits(RzAsmState *as)
Definition: rz-asm.c:28
RZ_API RzLib * rz_lib_new(const char *symname, const char *symnamefunc)
Definition: lib.c:176
RZ_API RzNum * rz_num_new(RzNumCallback cb, RzNumCallback2 cb2, void *ptr)
Definition: unum.c:75
#define RZ_NEW0(x)
Definition: rz_types.h:284

References __as_set_archbits(), __load_plugins(), RzAsmState::a, RzAsmState::analysis, RzAsmState::l, NULL, rz_asm_t::num, rz_analysis_new(), rz_asm_new(), rz_lib_new(), RZ_NEW0, and rz_num_new().

Referenced by rz_main_rz_asm().

◆ __as_set_archbits()

static void __as_set_archbits ( RzAsmState as)
static

Definition at line 28 of file rz-asm.c.

28  {
29  rz_asm_use(as->a, RZ_SYS_ARCH);
31  int sysbits = (RZ_SYS_BITS & RZ_SYS_BITS_64) ? 64 : 32;
32  rz_asm_set_bits(as->a, sysbits);
33  rz_analysis_set_bits(as->analysis, sysbits);
34 }
RZ_API bool rz_analysis_set_bits(RzAnalysis *analysis, int bits)
Definition: analysis.c:270
RZ_API bool rz_analysis_use(RzAnalysis *analysis, const char *name)
Definition: analysis.c:184
RZ_DEPRECATE RZ_API int rz_asm_set_bits(RzAsm *a, int bits)
Definition: asm.c:488
RZ_API bool rz_asm_use(RzAsm *a, const char *name)
Puts an Asm plugin in use and disables the previous one.
Definition: asm.c:429
@ RZ_SYS_BITS_64
Definition: rz_sys.h:21
#define RZ_SYS_BITS
Definition: rz_types.h:520
#define RZ_SYS_ARCH
Definition: rz_types.h:519

References RzAsmState::a, RzAsmState::analysis, rz_analysis_set_bits(), rz_analysis_use(), rz_asm_set_bits(), rz_asm_use(), RZ_SYS_ARCH, RZ_SYS_BITS, and RZ_SYS_BITS_64.

Referenced by __as_new().

◆ __lib_analysis_cb()

static int __lib_analysis_cb ( RzLibPlugin pl,
void *  user,
void *  data 
)
static

Definition at line 454 of file rz-asm.c.

454  {
455  RzAnalysisPlugin *hand = (RzAnalysisPlugin *)data;
456  RzAsmState *as = (RzAsmState *)user;
457  return rz_analysis_add(as->analysis, hand);
458 }
RZ_API int rz_analysis_add(RzAnalysis *analysis, RzAnalysisPlugin *p)
Definition: analysis.c:179

References RzAsmState::analysis, and rz_analysis_add().

Referenced by __load_plugins().

◆ __lib_asm_cb()

static int __lib_asm_cb ( RzLibPlugin pl,
void *  user,
void *  data 
)
static

Definition at line 447 of file rz-asm.c.

447  {
448  RzAsmPlugin *hand = (RzAsmPlugin *)data;
449  RzAsmState *as = (RzAsmState *)user;
450  return rz_asm_add(as->a, hand);
451 }
RZ_API bool rz_asm_add(RzAsm *a, RzAsmPlugin *p)
Definition: asm.c:333

References RzAsmState::a, and rz_asm_add().

Referenced by __load_plugins().

◆ __load_plugins()

static void __load_plugins ( RzAsmState as)
static

Definition at line 479 of file rz-asm.c.

479  {
480  char *tmp = rz_sys_getenv("RZ_ASM_NOPLUGINS");
481  if (tmp) {
482  free(tmp);
483  return;
484  }
485  rz_lib_add_handler(as->l, RZ_LIB_TYPE_ASM, "(dis)assembly plugins", &__lib_asm_cb, NULL, as);
486  rz_lib_add_handler(as->l, RZ_LIB_TYPE_ANALYSIS, "analysis/emulation plugins", &__lib_analysis_cb, NULL, as);
487 
488  char *path = rz_sys_getenv(RZ_LIB_ENV);
489  if (!RZ_STR_ISEMPTY(path)) {
490  rz_lib_opendir(as->l, path, false);
491  }
492 
493  char *homeplugindir = rz_path_home_prefix(RZ_PLUGINS);
494  // TODO: remove after 0.4.0 is released
495  char *oldhomeplugindir = rz_path_home_prefix(RZ_HOME_OLD_PLUGINS);
496  char *sysplugindir = rz_path_system(RZ_PLUGINS);
497  rz_lib_opendir(as->l, homeplugindir, false);
498  rz_lib_opendir(as->l, oldhomeplugindir, false);
499  rz_lib_opendir(as->l, sysplugindir, false);
500  free(homeplugindir);
501  free(oldhomeplugindir);
502  free(sysplugindir);
503 
504  free(tmp);
505  free(path);
506 }
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
static int __lib_analysis_cb(RzLibPlugin *pl, void *user, void *data)
Definition: rz-asm.c:454
static int __lib_asm_cb(RzLibPlugin *pl, void *user, void *data)
Definition: rz-asm.c:447
#define RZ_LIB_ENV
Definition: rz_lib.h:23
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
@ RZ_LIB_TYPE_ANALYSIS
Definition: rz_lib.h:73
RZ_API bool rz_lib_opendir(RzLib *lib, const char *path, bool force)
Open all the libraries in the given directory, if it wasn't already opened.
Definition: lib.c:417
RZ_API bool rz_lib_add_handler(RzLib *lib, int type, const char *desc, RzLibCallback ct, RzLibCallback dt, void *user)
RZ_API RZ_OWN char * rz_path_home_prefix(RZ_NULLABLE const char *path)
Return path prefixed by the home prefix.
Definition: path.c:182
RZ_API RZ_OWN char * rz_path_system(RZ_NULLABLE const char *path)
Return the full system path of the given subpath path.
Definition: path.c:162
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API char * rz_sys_getenv(const char *key)
Get the value of an environment variable named key or NULL if none exists.
Definition: sys.c:483
#define RZ_PLUGINS
Definition: rz_userconf.h:72
#define RZ_HOME_OLD_PLUGINS
Definition: rz_userconf.h:100

References __lib_analysis_cb(), __lib_asm_cb(), free(), RzAsmState::l, NULL, path, RZ_HOME_OLD_PLUGINS, rz_lib_add_handler(), RZ_LIB_ENV, rz_lib_opendir(), RZ_LIB_TYPE_ANALYSIS, RZ_LIB_TYPE_ASM, rz_path_home_prefix(), rz_path_system(), RZ_PLUGINS, RZ_STR_ISEMPTY, rz_sys_getenv(), and autogen_x86imm::tmp.

Referenced by __as_new().

◆ print_and_check_il()

static bool print_and_check_il ( RzAsmState as,
RzAnalysisOp op 
)
static

Definition at line 227 of file rz-asm.c.

227  {
228  if (op->size < 1 || !op->il_op) {
229  eprintf("Invalid instruction of lifting not implemented.\n");
230  return false;
231  }
233  if (!vm) {
234  eprintf("Failed to initialize IL VM for this architecture.\n");
235  return false;
236  }
237  bool ret = true;
239  if (!ctx) {
240  eprintf("Failed to derive context from IL VM.\n");
241  ret = false;
242  goto error_vm;
243  }
244  RzILOpEffect *il_op = op->il_op;
245  if (il_op) {
246  RzStrBuf sb;
247  rz_strbuf_init(&sb);
248  rz_il_op_effect_stringify(il_op, &sb);
249  printf("%s\n", rz_strbuf_get(&sb));
250  fflush(stdout); // to appear before validation report
251  rz_strbuf_fini(&sb);
252  }
253  char *report;
254  if (!rz_il_validate_effect(il_op, ctx, NULL, NULL, &report)) {
255  ret = false;
256  eprintf("IL Validation failed%c\n", report ? ':' : '.');
257  }
258  if (report) {
259  eprintf("%s\n", report);
260  free(report);
261  }
263 error_vm:
265  return ret;
266 }
RZ_API RZ_OWN RzAnalysisILVM * rz_analysis_il_vm_new(RzAnalysis *a, RZ_NULLABLE RzReg *init_state_reg)
Definition: analysis_il.c:98
RZ_API void rz_analysis_il_vm_free(RZ_NULLABLE RzAnalysisILVM *vm)
Definition: analysis_il.c:125
static SblHeader sb
Definition: bin_mbn.c:26
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
RZ_API void rz_il_op_effect_stringify(RZ_NONNULL RzILOpEffect *op, RZ_NONNULL RzStrBuf *sb)
Definition: il_export.c:711
RZ_API void rz_il_validate_global_context_free(RzILValidateGlobalContext *ctx)
Definition: il_validate.c:99
RZ_API RzILValidateGlobalContext * rz_il_validate_global_context_new_from_vm(RZ_NONNULL RzILVM *vm)
Definition: il_validate.c:77
RZ_API bool rz_il_validate_effect(RZ_NULLABLE RzILOpEffect *op, RZ_NONNULL RzILValidateGlobalContext *ctx, RZ_NULLABLE RZ_OUT HtPP **local_var_sorts_out, RZ_NULLABLE RZ_OUT RzILTypeEffect *type_out, RZ_NULLABLE RZ_OUT RzILValidateReport *report_out)
Definition: il_validate.c:846
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API char * rz_strbuf_get(RzStrBuf *sb)
Definition: strbuf.c:321
RZ_API void rz_strbuf_fini(RzStrBuf *sb)
Definition: strbuf.c:365
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
High-level RzIL vm to emulate disassembled code.
Definition: rz_analysis.h:1155
RZ_NONNULL RzILVM * vm
low-level vm to execute IL code
Definition: rz_analysis.h:1156
Definition: dis.c:32

References RzAsmState::analysis, eprintf, free(), NULL, printf(), rz_analysis_il_vm_free(), rz_analysis_il_vm_new(), rz_il_op_effect_stringify(), rz_il_validate_effect(), rz_il_validate_global_context_free(), rz_il_validate_global_context_new_from_vm(), rz_strbuf_fini(), rz_strbuf_get(), rz_strbuf_init(), sb, and rz_analysis_il_vm_t::vm.

Referenced by rasm_disasm().

◆ print_assembly_output()

static int print_assembly_output ( RzAsmState as,
const char *  buf,
ut64  offset,
ut64  len,
int  bits,
int  bin,
bool  use_spp,
bool  rad,
bool  hexwords,
const char *  arch 
)
static

Definition at line 460 of file rz-asm.c.

461  {
462  if (rad) {
463  printf("e asm.arch=%s\n", arch ? arch : RZ_SYS_ARCH);
464  printf("e asm.bits=%d\n", bits);
465  if (offset) {
466  printf("s 0x%" PFMT64x "\n", offset);
467  }
468  printf("wx ");
469  }
470  int ret = rasm_asm(as, (char *)buf, offset, len, as->a->bits, bin, use_spp, hexwords);
471  if (rad) {
472  printf("f entry @ $$\n");
473  printf("f label.main @ $$ + 1\n");
474  ht_pp_foreach(as->a->flags, print_label, NULL);
475  }
476  return ret;
477 }
size_t len
Definition: 6502dis.c:15
int bits(struct state *s, int need)
Definition: blast.c:72
cs_arch arch
Definition: cstool.c:13
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
static bool print_label(void *user, const void *k, const void *v)
Definition: rz-asm.c:391
static int rasm_asm(RzAsmState *as, const char *buf, ut64 offset, ut64 len, int bits, int bin, bool use_spp, bool hexwords)
Definition: rz-asm.c:396
#define PFMT64x
Definition: rz_types.h:393
Definition: malloc.c:26
int bits
Definition: rz_asm.h:100
HtPP * flags
Definition: rz_asm.h:124

References RzAsmState::a, arch, rz_asm_t::bits, bits(), rz_asm_t::flags, len, NULL, PFMT64x, print_label(), printf(), rasm_asm(), and RZ_SYS_ARCH.

Referenced by rz_main_rz_asm().

◆ print_buf()

static void print_buf ( RzAsmState as,
char *  str 
)
static

Definition at line 374 of file rz-asm.c.

374  {
375  int i;
376  if (as->coutput) {
377  printf("\"");
378  for (i = 1; *str; str += 2, i += 2) {
379  if (!(i % 41)) {
380  printf("\" \\\n\"");
381  i = 1;
382  }
383  printf("\\x%c%c", *str, str[1]);
384  }
385  printf("\"\n");
386  } else {
387  printf("%s\n", str);
388  }
389 }
lzma_index ** i
Definition: index.h:629
bool coutput
Definition: rz-asm.c:21

References RzAsmState::coutput, i, printf(), and cmd_descs_generate::str.

Referenced by rasm_asm().

◆ print_label()

static bool print_label ( void *  user,
const void *  k,
const void *  v 
)
static

Definition at line 391 of file rz-asm.c.

391  {
392  printf("f label.%s @ %s\n", (const char *)k, (const char *)v);
393  return true;
394 }
const char * k
Definition: dsignal.c:11
const char * v
Definition: dsignal.c:12

References k, printf(), and v.

Referenced by print_assembly_output().

◆ rasm_asm()

static int rasm_asm ( RzAsmState as,
const char *  buf,
ut64  offset,
ut64  len,
int  bits,
int  bin,
bool  use_spp,
bool  hexwords 
)
static

Definition at line 396 of file rz-asm.c.

396  {
397  RzAsmCode *acode;
398  int i, j, ret = 0;
399  rz_asm_set_pc(as->a, offset);
400  if (!(acode = rz_asm_rasm_assemble(as->a, buf, use_spp))) {
401  return 0;
402  }
403  if (acode->len) {
404  ret = acode->len;
405  if (bin) {
406  if ((ret = write(1, acode->bytes, acode->len)) != acode->len) {
407  eprintf("Failed to write buffer\n");
408  rz_asm_code_free(acode);
409  return 0;
410  }
411  } else {
412  int b = acode->len;
413  if (bits == 1) {
414  int bytes = (b / 8) + 1;
415  for (i = 0; i < bytes; i++) {
416  for (j = 0; j < 8 && b--; j++) {
417  printf("%c", (acode->bytes[i] & (1 << j)) ? '1' : '0');
418  }
419  }
420  printf("\n");
421  } else {
422  if (hexwords) {
423  size_t i = 0;
424  for (i = 0; i < acode->len; i += sizeof(ut32)) {
425  ut32 dword = rz_read_ble32(acode->bytes + i, RZ_SYS_ENDIAN);
426  printf("0x%08x ", dword);
427  if ((i / 4) == 7) {
428  printf("\n");
429  }
430  }
431  printf("\n");
432  } else {
433  char *str = rz_asm_code_get_hex(acode);
434  if (str) {
435  print_buf(as, str);
436  free(str);
437  }
438  }
439  }
440  }
441  }
442  rz_asm_code_free(acode);
443  return (ret > 0);
444 }
RZ_API void * rz_asm_code_free(RzAsmCode *acode)
Definition: acode.c:11
RZ_API char * rz_asm_code_get_hex(RzAsmCode *acode)
Definition: acode.c:68
RZ_API RzAsmCode * rz_asm_rasm_assemble(RzAsm *a, const char *buf, bool use_spp)
Definition: asm.c:1270
RZ_API int rz_asm_set_pc(RzAsm *a, ut64 pc)
Definition: asm.c:533
static ut8 bytes[32]
Definition: asm_arc.c:23
static static fork write
Definition: sflib.h:33
uint32_t ut32
static void print_buf(RzAsmState *as, char *str)
Definition: rz-asm.c:374
static ut32 rz_read_ble32(const void *src, bool big_endian)
Definition: rz_endian.h:497
#define RZ_SYS_ENDIAN
Definition: rz_types.h:521
#define b(i)
Definition: sha256.c:42
ut8 * bytes
Definition: rz_asm.h:80

References RzAsmState::a, b, bits(), bytes, rz_asm_code_t::bytes, eprintf, free(), i, rz_asm_code_t::len, print_buf(), printf(), rz_asm_code_free(), rz_asm_code_get_hex(), rz_asm_rasm_assemble(), rz_asm_set_pc(), rz_read_ble32(), RZ_SYS_ENDIAN, cmd_descs_generate::str, and write.

Referenced by print_assembly_output(), and rz_main_rz_asm().

◆ rasm_disasm()

static int rasm_disasm ( RzAsmState as,
ut64  addr,
const char *  buf,
int  len,
int  bits,
int  bin,
DisasmMode  mode 
)
static

Definition at line 268 of file rz-asm.c.

268  {
269  RzAsmCode *acode;
270  ut8 *data = NULL;
271  int ret = 0;
272  ut64 clen = 0;
273  if (bits == 1) {
274  len /= 8;
275  }
276  if (bin) {
277  if (len < 0) {
278  return false;
279  }
280  clen = len; // XXX
281  data = (ut8 *)buf;
282  } else {
283  clen = rz_hex_str2bin(buf, NULL);
284  if ((int)clen < 1 || !(data = malloc(clen))) {
285  ret = 0;
286  goto beach;
287  }
288  rz_hex_str2bin(buf, data);
289  len = clen;
290  }
291 
292  if (!len || clen <= len) {
293  len = clen;
294  }
295 
296  switch (mode) {
297  case DISASM_MODE_ESIL: {
298  RzAnalysisOp aop = { 0 };
299  while (ret < len) {
300  aop.size = 0;
301  if (rz_analysis_op(as->analysis, &aop, addr, data + ret, len - ret, RZ_ANALYSIS_OP_MASK_ESIL) > 0) {
302  printf("%s\n", RZ_STRBUF_SAFEGET(&aop.esil));
303  }
304  if (aop.size < 1) {
305  eprintf("Invalid\n");
306  break;
307  }
308  ret += aop.size;
309  rz_analysis_op_fini(&aop);
310  }
311  break;
312  }
313  case DISASM_MODE_IL: {
314  RzAnalysisOp aop = { 0 };
315  while (ret < len) {
316  aop.size = 0;
317  if (rz_analysis_op(as->analysis, &aop, addr, data + ret, len - ret, RZ_ANALYSIS_OP_MASK_IL) <= 0) {
318  eprintf("Invalid\n");
319  ret = 0;
320  break;
321  }
322  if (!print_and_check_il(as, &aop)) {
323  rz_analysis_op_fini(&aop);
324  ret = 0;
325  break;
326  }
327  ret += aop.size;
328  rz_analysis_op_fini(&aop);
329  }
330  break;
331  }
332  case DISASM_MODE_WITH_BYTES: {
333  RzAsmOp op;
334  rz_asm_set_pc(as->a, addr);
335  while ((len - ret) > 0) {
336  int dr = rz_asm_disassemble(as->a, &op, data + ret, len - ret);
337  if (dr == -1 || op.size < 1) {
338  op.size = 1;
339  rz_asm_op_set_asm(&op, "invalid");
340  }
341  char *op_hex = rz_asm_op_get_hex(&op);
342  printf("0x%08" PFMT64x " %2d %24s %s\n",
343  as->a->pc, op.size, op_hex,
345  free(op_hex);
346  ret += op.size;
347  rz_asm_set_pc(as->a, addr + ret);
348  }
349  break;
350  }
351  default: {
352  rz_asm_set_pc(as->a, addr);
353  if (!(acode = rz_asm_mdisassemble(as->a, data, len))) {
354  goto beach;
355  }
356  if (as->oneliner) {
357  rz_str_replace_char(acode->assembly, '\n', ';');
358  printf("%s\"\n", acode->assembly);
359  } else {
360  printf("%s", acode->assembly);
361  }
362  ret = acode->len;
363  rz_asm_code_free(acode);
364  break;
365  }
366  }
367 beach:
368  if (data && data != (ut8 *)buf) {
369  free(data);
370  }
371  return ret;
372 }
ut8 op
Definition: 6502dis.c:13
RZ_API char * rz_asm_op_get_hex(RzAsmOp *op)
Definition: aop.c:28
RZ_API void rz_asm_op_set_asm(RzAsmOp *op, const char *str)
Definition: aop.c:53
RZ_API char * rz_asm_op_get_asm(RzAsmOp *op)
Definition: aop.c:37
RZ_API RzAsmCode * rz_asm_mdisassemble(RzAsm *a, const ut8 *buf, int len)
Definition: asm.c:743
RZ_API int rz_asm_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm.c:543
const char int mode
Definition: ioapi.h:137
uint8_t ut8
Definition: lh5801.h:11
static bool print_and_check_il(RzAsmState *as, RzAnalysisOp *op)
Definition: rz-asm.c:227
void * malloc(size_t size)
Definition: malloc.c:123
RZ_API bool rz_analysis_op_fini(RzAnalysisOp *op)
Definition: op.c:37
RZ_API int rz_analysis_op(RzAnalysis *analysis, RzAnalysisOp *op, ut64 addr, const ut8 *data, int len, RzAnalysisOpMask mask)
Definition: op.c:96
@ RZ_ANALYSIS_OP_MASK_ESIL
Definition: rz_analysis.h:441
@ RZ_ANALYSIS_OP_MASK_IL
Definition: rz_analysis.h:446
RZ_API int rz_hex_str2bin(const char *in, ut8 *out)
Convert an input string in into the binary form in out.
Definition: hex.c:444
RZ_API int rz_str_replace_char(char *s, int a, int b)
Definition: str.c:169
#define RZ_STRBUF_SAFEGET(sb)
Definition: rz_strbuf.h:18
bool oneliner
Definition: rz-asm.c:20
char * assembly
Definition: rz_asm.h:81
ut64 pc
Definition: rz_asm.h:103
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58

References RzAsmState::a, addr, RzAsmState::analysis, rz_asm_code_t::assembly, bits(), DISASM_MODE_ESIL, DISASM_MODE_IL, DISASM_MODE_WITH_BYTES, eprintf, rz_analysis_op_t::esil, free(), len, rz_asm_code_t::len, malloc(), NULL, RzAsmState::oneliner, op, rz_asm_t::pc, PFMT64x, print_and_check_il(), printf(), rz_analysis_op(), rz_analysis_op_fini(), RZ_ANALYSIS_OP_MASK_ESIL, RZ_ANALYSIS_OP_MASK_IL, rz_asm_code_free(), rz_asm_disassemble(), rz_asm_mdisassemble(), rz_asm_op_get_asm(), rz_asm_op_get_hex(), rz_asm_op_set_asm(), rz_asm_set_pc(), rz_hex_str2bin(), rz_str_replace_char(), RZ_STRBUF_SAFEGET, rz_analysis_op_t::size, and ut64().

Referenced by rz_main_rz_asm().

◆ rasm_show_help()

static int rasm_show_help ( int  v)
static

Definition at line 170 of file rz-asm.c.

170  {
171  if (v < 2) {
172  printf("Usage: rz-asm [-ACdDehLBvw] [-a arch] [-b bits] [-o addr] [-s syntax]\n"
173  " [-f file] [-F fil:ter] [-i skip] [-l len] 'code'|hex|-\n");
174  }
175  if (v != 1) {
176  printf(" -a [arch] Set architecture to assemble/disassemble (see -L)\n"
177  " -A Show Analysis information from given hexpairs\n"
178  " -b [bits] Set cpu register size (8, 16, 32, 64) (RZ_ASM_BITS)\n"
179  " -B Binary input/output (-l is mandatory for binary input)\n"
180  " -c [cpu] Select specific CPU (depends on arch)\n"
181  " -C Output in C format\n"
182  " -d, -D Disassemble from hexpair bytes (-D show hexpairs)\n"
183  " -e Use big endian instead of little endian\n"
184  " -I Display lifted RzIL code (same input as in -d, IL is also validated)\n"
185  " -E Display ESIL expression (same input as in -d)\n"
186  " -f [file] Read data from file\n"
187  " -F [in:out] Specify input and/or output filters (att2intel, x86.pseudo, ...)\n"
188  " -h, -hh Show this help, -hh for long\n"
189  " -i [len] ignore/skip N bytes of the input buffer\n"
190  " -j output in json format\n"
191  " -k [kernel] Select operating system (linux, windows, darwin, ..)\n"
192  " -l [len] Input/Output length\n"
193  " -L List Asm plugins: (a=asm, d=disasm, A=analyze, e=ESIL)\n"
194  " -o,-@ [addr] Set start address for code (default 0)\n"
195  " -O [file] Output file name (rz-asm -Bf a.asm -O a)\n"
196  " -p Run SPP over input for assembly\n"
197  " -q quiet mode\n"
198  " -r output in rizin commands\n"
199  " -s [syntax] Select syntax (intel, att)\n"
200  " -v Show version information\n"
201  " -x Use hex dwords instead of hex pairs when assembling.\n"
202  " -w What's this instruction for? describe opcode\n"
203  " If '-l' value is greater than output length, output is padded with nops\n"
204  " If the last argument is '-' reads from stdin\n");
205  printf("Environment:\n"
206  " RZ_ASM_NOPLUGINS do not load shared plugins (speedup loading)\n"
207  " RZ_ASM_ARCH same as rz-asm -a\n"
208  " RZ_ASM_BITS same as rz-asm -b\n"
209  " RZ_DEBUG if defined, show error messages and crash signal\n"
210  "");
211  }
212  if (v == 2) {
213  printf("Supported Assembler directives:\n");
215  }
216  return 0;
217 }
RZ_API void rz_asm_list_directives(void)
Definition: asm.c:687

References printf(), rz_asm_list_directives(), and v.

Referenced by rz_main_rz_asm().

◆ rz_main_rz_asm()

RZ_API int rz_main_rz_asm ( int  argc,
const char *  argv[] 
)

Definition at line 508 of file rz-asm.c.

508  {
509  const char *env_arch = rz_sys_getenv("RZ_ASM_ARCH");
510  const char *env_bits = rz_sys_getenv("RZ_ASM_BITS");
511  const char *arch = NULL;
512  const char *cpu = NULL;
513  const char *kernel = NULL;
514  const char *filters = NULL;
515  const char *file = NULL;
516  bool isbig = false;
517  bool rad = false;
518  bool use_spp = false;
519  bool hexwords = false;
520  ut64 offset = 0;
521  int fd = -1, bin = 0, ret = 0, bits = 32, c, whatsop = 0;
523  int help = 0;
524  ut64 len = 0, idx = 0, skip = 0;
525  bool analinfo = false;
526 
527  if (argc < 2) {
528  return rasm_show_help(1);
529  }
530 
531  RzAsmState *as = __as_new();
532 
533  // TODO set addrbytes
534  char *rz_arch = rz_sys_getenv("RZ_ARCH");
535  if (rz_arch) {
536  arch = rz_arch;
537  }
538 
539  char *r2bits = rz_sys_getenv("RZ_BITS");
540  if (r2bits) {
541  bits = rz_num_math(NULL, r2bits);
542  free(r2bits);
543  }
544 
545  RzGetopt opt;
546  rz_getopt_init(&opt, argc, argv, "a:Ab:Bc:CdDeEIf:F:hi:jk:l:L@:o:O:pqrs:vwx");
547  while ((c = rz_getopt_next(&opt)) != -1) {
548  switch (c) {
549  case 'a':
550  arch = opt.arg;
551  break;
552  case 'A':
553  analinfo = true;
554  break;
555  case 'b':
556  bits = rz_num_math(NULL, opt.arg);
557  break;
558  case 'B':
559  bin = 1;
560  break;
561  case 'c':
562  cpu = opt.arg;
563  break;
564  case 'C':
565  as->coutput = true;
566  break;
567  case 'd':
569  break;
570  case 'D':
572  break;
573  case 'e':
574  isbig = true;
575  break;
576  case 'E':
578  break;
579  case 'I':
581  break;
582  case 'f':
583  file = opt.arg;
584  break;
585  case 'F':
586  filters = opt.arg;
587  break;
588  case 'h':
589  help++;
590  // fallthrough
591  case 'i':
592  skip = rz_num_math(NULL, opt.arg);
593  break;
594  case 'j':
595  as->json = true;
596  break;
597  case 'k':
598  kernel = opt.arg;
599  break;
600  case 'l':
601  len = rz_num_math(NULL, opt.arg);
602  break;
603  case 'L': {
604  // create a dummy RzCore with the current RzAsm/RzAnalysis
605  RzCore *core = rz_core_new();
606  RzAsm *tmp_asm = core->rasm;
607  RzAnalysis *tmp_analysis = core->analysis;
608  core->rasm = as->a;
609  core->analysis = as->analysis;
610  RzCmdStateOutput state = { 0 };
612  rz_core_asm_plugins_print(core, opt.argv[opt.ind], &state);
615  rz_cons_flush();
616  core->rasm = tmp_asm;
617  core->analysis = tmp_analysis;
618  rz_core_free(core);
619  ret = 1;
620  goto beach;
621  }
622  case '@':
623  case 'o':
624  offset = rz_num_math(NULL, opt.arg);
625  break;
626  case 'O':
627  fd = open(opt.arg, O_TRUNC | O_RDWR | O_CREAT, 0644);
628  if (fd != -1) {
629  dup2(fd, 1);
630  }
631  break;
632  case 'p':
633  use_spp = true;
634  break;
635  case 'q':
636  as->quiet = true;
637  break;
638  case 'r':
639  rad = true;
640  break;
641  case 's':
642  if (*opt.arg == '?') {
643  printf("att\nintel\nmasm\njz\nregnum\n");
644  __as_free(as);
645  return 0;
646  } else {
648  if (syntax == -1) {
649  __as_free(as);
650  return 1;
651  }
652  rz_asm_set_syntax(as->a, syntax);
653  }
654  break;
655  case 'v':
656  if (as->quiet) {
657  printf("%s\n", RZ_VERSION);
658  } else {
659  ret = rz_main_version_print("rz-asm");
660  }
661  goto beach;
662  case 'w':
663  whatsop = true;
664  break;
665  case 'x':
666  hexwords = true;
667  break;
668  default:
669  ret = rasm_show_help(0);
670  goto beach;
671  }
672  }
673 
674  if (help > 0) {
675  ret = rasm_show_help(help > 1 ? 2 : 0);
676  goto beach;
677  }
678 
679  if (arch) {
680  if (!rz_asm_use(as->a, arch)) {
681  eprintf("rz-asm: Unknown asm plugin '%s'\n", arch);
682  ret = 0;
683  goto beach;
684  }
686  } else if (env_arch) {
687  if (!rz_asm_use(as->a, env_arch)) {
688  eprintf("rz-asm: Unknown asm plugin '%s'\n", env_arch);
689  ret = 0;
690  goto beach;
691  }
692  } else if (!rz_asm_use(as->a, "x86")) {
693  eprintf("rz-asm: Cannot find asm.x86 plugin\n");
694  ret = 0;
695  goto beach;
696  }
697  rz_asm_set_cpu(as->a, cpu);
699  rz_asm_set_bits(as->a, (env_bits && *env_bits) ? atoi(env_bits) : bits);
700  rz_analysis_set_bits(as->analysis, (env_bits && *env_bits) ? atoi(env_bits) : bits);
701  as->a->syscall = rz_syscall_new();
702  rz_syscall_setup(as->a->syscall, arch, bits, cpu, kernel);
703  {
704  bool canbebig = rz_asm_set_big_endian(as->a, isbig);
705  if (isbig && !canbebig) {
706  eprintf("Warning: This architecture can't swap to big endian.\n");
707  }
708  rz_analysis_set_big_endian(as->analysis, canbebig);
709  }
710  if (whatsop) {
711  const char *s = rz_asm_describe(as->a, opt.argv[opt.ind]);
712  ret = 1;
713  if (s) {
714  printf("%s\n", s);
715  ret = 0;
716  }
717  goto beach;
718  }
719  if (filters) {
720  char *p = strchr(filters, ':');
721  if (p) {
722  *p = 0;
723  if (*filters) {
725  }
726  if (p[1]) {
727  rz_asm_sub_names_output(as->a, p + 1);
728  }
729  *p = ':';
730  } else {
731  if (dis) {
733  } else {
735  }
736  }
737  }
738 
739  if (file) {
740  char *content;
741  size_t length = 0;
742  if (!strcmp(file, "-")) {
743  int sz = 0;
744  ut8 *buf = (ut8 *)rz_stdin_slurp(&sz);
745  if (!buf || sz < 1) {
746  eprintf("Nothing to do.\n");
747  goto beach;
748  }
749  len = (ut64)sz;
750  if (dis) {
751  if (skip && length > skip) {
752  if (bin) {
753  memmove(buf, buf + skip, length - skip);
754  length -= skip;
755  }
756  }
757  ret = rasm_disasm(as, offset, (char *)buf, len, as->a->bits, bin, dis);
758  } else if (analinfo) {
759  ret = show_analinfo(as, (const char *)buf, offset);
760  } else {
761  ret = print_assembly_output(as, (char *)buf, offset, len,
762  as->a->bits, bin, use_spp, rad, hexwords, arch);
763  }
764  ret = !ret;
765  free(buf);
766  } else {
767  content = rz_file_slurp(file, &length);
768  if (content) {
769  if (length > ST32_MAX) {
770  eprintf("rz-asm: File %s is too big\n", file);
771  ret = 1;
772  } else {
773  if (len && len > 0 && len < length) {
774  length = len;
775  }
776  content[length] = '\0';
777  if (skip && length > skip) {
778  if (bin) {
779  memmove(content, content + skip, length - skip);
780  length -= skip;
781  }
782  }
783  if (dis) {
784  ret = rasm_disasm(as, offset, content,
785  length, as->a->bits, bin, dis);
786  } else if (analinfo) {
787  ret = show_analinfo(as, (const char *)content, offset);
788  } else {
789  ret = print_assembly_output(as, content, offset, length,
790  as->a->bits, bin, use_spp, rad, hexwords, arch);
791  }
792  ret = !ret;
793  }
794  free(content);
795  } else {
796  eprintf("rz-asm: Cannot open file %s\n", file);
797  ret = 1;
798  }
799  }
800  } else if (opt.argv[opt.ind]) {
801  if (!strcmp(opt.argv[opt.ind], "-")) {
802  int length;
803  do {
804  char buf[1024]; // TODO: use(implement) rz_stdin_line() or so
805  length = read(0, buf, sizeof(buf) - 1);
806  if (length < 1) {
807  break;
808  }
809  if (len > 0 && len < length) {
810  length = len;
811  }
812  buf[length] = 0;
813  if ((!bin || !dis) && feof(stdin)) {
814  break;
815  }
816  if (skip && length > skip) {
817  if (bin) {
818  memmove(buf, buf + skip, length - skip + 1);
819  length -= skip;
820  }
821  }
822  if (!bin || !dis) {
823  int buflen = strlen((const char *)buf);
824  if (buf[buflen] == '\n') {
825  buf[buflen - 1] = '\0';
826  }
827  }
828  if (dis) {
829  ret = rasm_disasm(as, offset, (char *)buf, length, as->a->bits, bin, dis);
830  } else if (analinfo) {
831  ret = show_analinfo(as, (const char *)buf, offset);
832  } else {
833  ret = rasm_asm(as, (const char *)buf, offset, length, as->a->bits, bin, use_spp, hexwords);
834  }
835  idx += ret;
836  offset += ret;
837  if (!ret) {
838  goto beach;
839  }
840  } while (!len || idx < length);
841  ret = idx;
842  goto beach;
843  }
844  if (dis) {
845  char *usrstr = strdup(opt.argv[opt.ind]);
846  len = strlen(usrstr);
847  if (skip && len > skip) {
848  skip *= 2;
849  // eprintf ("SKIP (%s) (%lld)\n", usrstr, skip);
850  memmove(usrstr, usrstr + skip, len - skip);
851  len -= skip;
852  usrstr[len] = 0;
853  }
854  // XXX this is a wrong usage of endianness
855  if (!strncmp(usrstr, "0x", 2)) {
856  memmove(usrstr, usrstr + 2, strlen(usrstr + 2) + 1);
857  }
858  if (rad) {
859  as->oneliner = true;
860  printf("e asm.arch=%s\n", arch ? arch : RZ_SYS_ARCH);
861  printf("e asm.bits=%d\n", bits);
862  printf("\"wa ");
863  }
864  ret = rasm_disasm(as, offset, (char *)usrstr, len, as->a->bits, bin, dis);
865  free(usrstr);
866  } else if (analinfo) {
867  ret = show_analinfo(as, (const char *)opt.argv[opt.ind], offset);
868  } else {
869  ret = print_assembly_output(as, opt.argv[opt.ind], offset, len, as->a->bits,
870  bin, use_spp, rad, hexwords, arch);
871  }
872  ret = !ret;
873  }
874 beach:
875  __as_free(as);
876 
877  free(rz_arch);
878  if (fd != -1) {
879  close(fd);
880  }
881  return ret;
882 }
RZ_API void rz_analysis_set_cpu(RzAnalysis *analysis, const char *cpu)
Definition: analysis.c:311
RZ_API int rz_analysis_set_big_endian(RzAnalysis *analysis, int bigend)
Definition: analysis.c:325
static ut32 cpu[32]
Definition: analysis_or1k.c:21
RZ_API int rz_asm_syntax_from_string(const char *name)
Definition: asm.c:1227
RZ_API bool rz_asm_set_big_endian(RzAsm *a, bool b)
Definition: asm.c:496
RZ_API char * rz_asm_describe(RzAsm *a, const char *str)
Definition: asm.c:1187
RZ_API bool rz_asm_set_syntax(RzAsm *a, int syntax)
Definition: asm.c:518
RZ_DEPRECATE RZ_API void rz_asm_set_cpu(RzAsm *a, const char *cpu)
Definition: asm.c:477
RZ_API int rz_asm_sub_names_input(RzAsm *a, const char *f)
Definition: asm.c:290
RZ_API int rz_asm_sub_names_output(RzAsm *a, const char *f)
Definition: asm.c:303
static int dis(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_snes.c:12
RZ_API RzCmdStatus rz_core_asm_plugins_print(RzCore *core, const char *arch, RzCmdStateOutput *state)
Definition: casm.c:150
RZ_API bool rz_cmd_state_output_init(RZ_NONNULL RzCmdStateOutput *state, RzOutputMode mode)
Initialize a RzCmdStateOutput structure and its inner fields based on the provided mode.
Definition: cmd_api.c:2634
RZ_API void rz_cmd_state_output_fini(RZ_NONNULL RzCmdStateOutput *state)
Clear the inner fields of RzCmdStateOutput structure, but do not free it.
Definition: cmd_api.c:2603
RZ_API void rz_cmd_state_output_print(RZ_NONNULL RzCmdStateOutput *state)
Print the output accumulated in state to RzCons, if necessary.
Definition: cmd_api.c:2668
RZ_API void rz_cons_flush(void)
Definition: cons.c:959
const lzma_filter * filters
Definition: container.h:315
static static fork const void static count close
Definition: sflib.h:33
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 static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
void skip(file *in, unsigned n)
Definition: gzappend.c:202
void * p
Definition: libc.cpp:67
RZ_API void rz_core_free(RzCore *c)
Definition: core.c:2683
RZ_API RzCore * rz_core_new(void)
Definition: core.c:866
static RzAsmState * __as_new(void)
Definition: rz-asm.c:36
static int print_assembly_output(RzAsmState *as, const char *buf, ut64 offset, ut64 len, int bits, int bin, bool use_spp, bool rad, bool hexwords, const char *arch)
Definition: rz-asm.c:460
static int show_analinfo(RzAsmState *as, const char *arg, ut64 offset)
Definition: rz-asm.c:123
static void __as_free(RzAsmState *as)
Definition: rz-asm.c:51
static int rasm_disasm(RzAsmState *as, ut64 addr, const char *buf, int len, int bits, int bin, DisasmMode mode)
Definition: rz-asm.c:268
static int rasm_show_help(int v)
Definition: rz-asm.c:170
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
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")
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid const char static path dup2
Definition: sflib.h:94
int idx
Definition: setup.py:197
static RzSocket * s
Definition: rtr.c:28
RZ_API char * rz_stdin_slurp(int *sz)
Definition: file.c:408
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454
RZ_API void rz_getopt_init(RzGetopt *go, int argc, const char **argv, const char *ostr)
Definition: getopt.c:17
RZ_API int rz_getopt_next(RzGetopt *opt)
Definition: getopt.c:29
RZ_API int rz_main_version_print(const char *program)
Definition: main.c:49
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
@ RZ_OUTPUT_MODE_JSON
Definition: rz_types.h:40
@ RZ_OUTPUT_MODE_STANDARD
Definition: rz_types.h:39
#define ST32_MAX
Definition: rz_types_base.h:97
#define RZ_VERSION
Definition: rz_version.h:8
#define O_CREAT
Definition: sftypes.h:489
#define O_RDWR
Definition: sftypes.h:488
#define O_TRUNC
Definition: sftypes.h:492
#define c(i)
Definition: sha256.c:43
bool json
Definition: rz-asm.c:22
bool quiet
Definition: rz-asm.c:23
Definition: gzappend.c:170
RzSyscall * syscall
Definition: rz_asm.h:113
Represent the output state of a command handler.
Definition: rz_cmd.h:91
RzAsm * rasm
Definition: rz_core.h:323
RzAnalysis * analysis
Definition: rz_core.h:322
const char ** argv
Definition: rz_getopt.h:18
const char * arg
Definition: rz_getopt.h:15
Definition: dis.h:43
ut64 buflen
Definition: core.c:76
RZ_API RzSyscall * rz_syscall_new(void)
Creates a new RzSyscall type.
Definition: syscall.c:67
RZ_API bool rz_syscall_setup(RzSyscall *s, const char *arch, int bits, const char *cpu, const char *os)
Definition: syscall.c:234
static const z80_opcode fd[]
Definition: z80_tab.h:997
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References __as_free(), __as_new(), RzAsmState::a, rz_core_t::analysis, RzAsmState::analysis, arch, rz_getopt_t::arg, rz_getopt_t::argv, argv, rz_asm_t::bits, bits(), buflen, c, close, RzAsmState::coutput, cpu, dis(), DISASM_MODE_DEFAULT, DISASM_MODE_DONT, DISASM_MODE_ESIL, DISASM_MODE_IL, DISASM_MODE_WITH_BYTES, dup2, eprintf, fd, filters, free(), cmd_descs_generate::help, setup::idx, rz_getopt_t::ind, RzAsmState::json, len, length, NULL, O_CREAT, O_RDWR, O_TRUNC, RzAsmState::oneliner, p, print_assembly_output(), printf(), RzAsmState::quiet, rz_core_t::rasm, rasm_asm(), rasm_disasm(), rasm_show_help(), read(), rz_analysis_set_big_endian(), rz_analysis_set_bits(), rz_analysis_set_cpu(), rz_analysis_use(), rz_asm_describe(), rz_asm_set_big_endian(), rz_asm_set_bits(), rz_asm_set_cpu(), rz_asm_set_syntax(), rz_asm_sub_names_input(), rz_asm_sub_names_output(), rz_asm_syntax_from_string(), rz_asm_use(), rz_cmd_state_output_fini(), rz_cmd_state_output_init(), rz_cmd_state_output_print(), rz_cons_flush(), rz_core_asm_plugins_print(), rz_core_free(), rz_core_new(), rz_file_slurp(), rz_getopt_init(), rz_getopt_next(), rz_main_version_print(), rz_num_math(), RZ_OUTPUT_MODE_JSON, RZ_OUTPUT_MODE_STANDARD, rz_stdin_slurp(), RZ_SYS_ARCH, rz_sys_getenv(), rz_syscall_new(), rz_syscall_setup(), RZ_VERSION, s, show_analinfo(), skip(), ST32_MAX, strdup(), benchmark::syntax, rz_asm_t::syscall, and ut64().

Referenced by MAIN_NAME().

◆ show_analinfo()

static int show_analinfo ( RzAsmState as,
const char *  arg,
ut64  offset 
)
static

Definition at line 123 of file rz-asm.c.

123  {
124  ut8 *buf = (ut8 *)strdup((const char *)arg);
125  int ret, len = rz_hex_str2bin((char *)buf, buf);
126  PJ *pj = NULL;
127  if (as->json) {
128  pj = pj_new();
129  if (!pj) {
130  free(buf);
131  return 0;
132  }
133  }
134 
135  RzAnalysisOp aop = { 0 };
136 
137  if (pj) {
138  pj_a(pj);
139  }
140  for (ret = 0; ret < len;) {
141  aop.size = 0;
143  eprintf("Error analyzing instruction at 0x%08" PFMT64x "\n", offset);
144  break;
145  }
146  if (aop.size < 1) {
147  if (pj) {
148  pj_o(pj);
149  pj_ks(pj, "bytes", rz_hex_bin2strdup(buf, ret));
150  pj_ks(pj, "type", "Invalid");
151  pj_end(pj);
152  } else {
153  eprintf("Invalid\n");
154  }
155  break;
156  }
157  showanalysis(as, &aop, offset, buf + ret, len - ret, pj);
158  ret += aop.size;
159  rz_analysis_op_fini(&aop);
160  }
161  if (pj) {
162  pj_end(pj);
163  printf("%s\n", pj_string(pj));
164  pj_free(pj);
165  }
166  free(buf);
167  return ret;
168 }
static const char * arg(RzAnalysis *a, csh *handle, cs_insn *insn, char *buf, int n)
Definition: arm_esil32.c:136
static void showanalysis(RzAsmState *as, RzAnalysisOp *op, ut64 offset, ut8 *buf, int len, PJ *pj)
Definition: rz-asm.c:72
@ RZ_ANALYSIS_OP_MASK_BASIC
Definition: rz_analysis.h:440
RZ_API char * rz_hex_bin2strdup(const ut8 *in, int len)
Definition: hex.c:415
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API PJ * pj_o(PJ *j)
Definition: pj.c:75
RZ_API PJ * pj_ks(PJ *j, const char *k, const char *v)
Definition: pj.c:170
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81
Definition: rz_pj.h:12

References RzAsmState::analysis, arg(), eprintf, free(), RzAsmState::json, len, NULL, PFMT64x, pj_a(), pj_end(), pj_free(), pj_ks(), pj_new(), pj_o(), pj_string(), printf(), rz_analysis_op(), rz_analysis_op_fini(), RZ_ANALYSIS_OP_MASK_BASIC, RZ_ANALYSIS_OP_MASK_ESIL, rz_hex_bin2strdup(), rz_hex_str2bin(), showanalysis(), rz_analysis_op_t::size, and strdup().

Referenced by rz_main_rz_asm().

◆ showanalysis()

static void showanalysis ( RzAsmState as,
RzAnalysisOp op,
ut64  offset,
ut8 buf,
int  len,
PJ pj 
)
static

Definition at line 72 of file rz-asm.c.

72  {
73  char *stackop = stackop2str(op->stackop);
74  const char *optype = rz_analysis_optype_to_string(op->type);
75  char *bytes = rz_hex_bin2strdup(buf, RZ_MIN(len, op->size));
76  if (as->json) {
77  pj_o(pj);
78  pj_kn(pj, "opcode", offset);
79  pj_ks(pj, "bytes", bytes);
80  pj_ks(pj, "type", optype);
81  if (op->jump != UT64_MAX) {
82  pj_kn(pj, "jump", op->jump);
83  }
84  if (op->fail != UT64_MAX) {
85  pj_kn(pj, "fail", op->fail);
86  }
87  if (op->val != UT64_MAX) {
88  pj_kn(pj, "val", op->val);
89  }
90  if (op->ptr != UT64_MAX) {
91  pj_kn(pj, "ptr", op->ptr);
92  }
93  pj_ks(pj, "stackop", stackop);
94  pj_ks(pj, "esil", rz_strbuf_get(&op->esil));
95  pj_kn(pj, "stackptr", op->stackptr);
96  pj_end(pj);
97  } else {
98  printf("offset: 0x%08" PFMT64x "\n", offset);
99  printf("bytes: %s\n", bytes);
100  printf("type: %s\n", optype);
101  if (op->jump != -1LL) {
102  printf("jump: 0x%08" PFMT64x "\n", op->jump);
103  }
104  if (op->fail != -1LL) {
105  printf("fail: 0x%08" PFMT64x "\n", op->fail);
106  }
107  // if (op->ref != -1LL)
108  // printf ("ref: 0x%08"PFMT64x"\n", op->ref);
109  if (op->val != -1LL) {
110  printf("value: 0x%08" PFMT64x "\n", op->val);
111  }
112  printf("stackop: %s\n", stackop);
113  printf("esil: %s\n", rz_strbuf_get(&op->esil));
114  printf("stackptr: %" PFMT64d "\n", op->stackptr);
115  // produces (null) printf ("decode str: %s\n", rz_analysis_op_to_string (analysis, op));
116  printf("\n");
117  }
118  free(stackop);
119  free(bytes);
120 }
static char * stackop2str(int type)
Definition: rz-asm.c:61
RZ_API const char * rz_analysis_optype_to_string(int type)
Definition: op.c:310
RZ_API PJ * pj_kn(PJ *j, const char *k, ut64 n)
Definition: pj.c:121
#define PFMT64d
Definition: rz_types.h:394
#define RZ_MIN(x, y)
#define UT64_MAX
Definition: rz_types_base.h:86
Definition: op.c:222

References bytes, free(), RzAsmState::json, len, PFMT64d, PFMT64x, pj_end(), pj_kn(), pj_ks(), pj_o(), printf(), rz_analysis_optype_to_string(), rz_hex_bin2strdup(), RZ_MIN, rz_strbuf_get(), stackop2str(), and UT64_MAX.

Referenced by show_analinfo().

◆ stackop2str()

static char* stackop2str ( int  type)
static

Definition at line 61 of file rz-asm.c.

61  {
62  switch (type) {
63  case RZ_ANALYSIS_STACK_NULL: return strdup("null");
64  case RZ_ANALYSIS_STACK_NOP: return strdup("nop");
65  // case RZ_ANALYSIS_STACK_INCSTACK: return strdup ("incstack");
66  case RZ_ANALYSIS_STACK_GET: return strdup("get");
67  case RZ_ANALYSIS_STACK_SET: return strdup("set");
68  }
69  return strdup("unknown");
70 }
int type
Definition: mipsasm.c:17
@ RZ_ANALYSIS_STACK_SET
Definition: rz_analysis.h:459
@ RZ_ANALYSIS_STACK_GET
Definition: rz_analysis.h:458
@ RZ_ANALYSIS_STACK_NULL
Definition: rz_analysis.h:455
@ RZ_ANALYSIS_STACK_NOP
Definition: rz_analysis.h:456

References RZ_ANALYSIS_STACK_GET, RZ_ANALYSIS_STACK_NOP, RZ_ANALYSIS_STACK_NULL, RZ_ANALYSIS_STACK_SET, strdup(), and type.

Referenced by showanalysis().