Rizin
unix-like reverse engineering framework and cli tools
rz-bin.c File Reference
#include <rz_core.h>
#include <rz_demangler.h>
#include <rz_types.h>
#include <rz_util.h>
#include <stdio.h>
#include <stdlib.h>
#include <rz_main.h>
#include "../../librz/bin/pdb/pdb_downloader.h"

Go to the source code of this file.

Macros

#define STDIN_BUF_SIZE   96096
 
#define is_active(x)   (action & (x))
 
#define set_action(x)
 
#define unset_action(x)   action &= ~x
 
#define ismodejson   (out_mode == RZ_MODE_JSON && actions > 0)
 
#define run_action(n, x, y)
 

Functions

static void start_state (RzCmdStateOutput *state)
 
static void end_state (RzCmdStateOutput *state)
 
static bool add_footer (RzCmdStateOutput *main_state, RzCmdStateOutput *state)
 
static RzCmdStateOutputadd_header (RzCmdStateOutput *main_state, RzOutputMode mode, const char *header)
 
static void classes_as_source_print (RzCore *core, RzCmdStateOutput *state)
 
static RzOutputMode rad2outputmode (int rad)
 
static ut32 actions2mask (ut64 action)
 
static int rabin_show_help (int v)
 
static char * stdin_gets (bool liberate)
 
static void __sdb_prompt (Sdb *sdb)
 
static bool isBinopHelp (const char *op)
 
static bool extract_binobj (const RzBinFile *bf, RzBinXtrData *data, int idx)
 
static int rabin_extract (RzBin *bin, int all)
 
static int rabin_dump_symbols (RzBin *bin, int len)
 
static bool __dumpSections (RzBin *bin, const char *scnname, const char *output, const char *file)
 
static int rabin_do_operation (RzBin *bin, const char *op, int rad, const char *output, const char *file)
 
static int rabin_show_srcline (RzBin *bin, ut64 at)
 
static int __lib_demangler_cb (RzLibPlugin *pl, void *user, void *data)
 
static int __lib_demangler_dt (RzLibPlugin *pl, void *p, void *u)
 
static int __lib_bin_cb (RzLibPlugin *pl, void *user, void *data)
 
static int __lib_bin_dt (RzLibPlugin *pl, void *p, void *u)
 
static int __lib_bin_xtr_cb (RzLibPlugin *pl, void *user, void *data)
 
static int __lib_bin_xtr_dt (RzLibPlugin *pl, void *p, void *u)
 
static void __listPlugins (RzBin *bin, const char *plugin_name, PJ *pj, int rad)
 
static bool print_demangler_info (const RzDemanglerPlugin *plugin, void *user)
 
static void print_string (RzBinFile *bf, RzBinString *string, PJ *pj, int mode)
 
RZ_API int rz_main_rz_bin (int argc, const char **argv)
 

Macro Definition Documentation

◆ is_active

#define is_active (   x)    (action & (x))

◆ ismodejson

#define ismodejson   (out_mode == RZ_MODE_JSON && actions > 0)

◆ run_action

#define run_action (   n,
  x,
 
)
Value:
if (action & (x)) { \
RzCmdStateOutput *st = add_header(&state, mode, n); \
y(&core, st); \
add_footer(&state, st); \
}
const char int mode
Definition: ioapi.h:137
static RzCmdStateOutput * add_header(RzCmdStateOutput *main_state, RzOutputMode mode, const char *header)
Definition: rz-bin.c:45
int x
Definition: mipsasm.c:20
int n
Definition: mipsasm.c:19
Definition: dis.h:43

◆ set_action

#define set_action (   x)
Value:
{ \
actions++; \
action |= (x); \
}

◆ STDIN_BUF_SIZE

#define STDIN_BUF_SIZE   96096

◆ unset_action

#define unset_action (   x)    action &= ~x

Function Documentation

◆ __dumpSections()

static bool __dumpSections ( RzBin bin,
const char *  scnname,
const char *  output,
const char *  file 
)
static

Definition at line 418 of file rz-bin.c.

418  {
419  RzList *sections;
420  RzListIter *iter;
422  ut8 *buf;
423  char *ret;
424  int r;
425 
426  if (!(sections = rz_bin_get_sections(bin))) {
427  return false;
428  }
429 
430  rz_list_foreach (sections, iter, section) {
431  if (!strcmp(scnname, section->name)) {
432  if (!(buf = malloc(section->size))) {
433  return false;
434  }
435  if ((section->size * 2) + 1 < section->size) {
436  free(buf);
437  return false;
438  }
439  if (!(ret = malloc(section->size * 2 + 1))) {
440  free(buf);
441  return false;
442  }
443  if (section->paddr > rz_buf_size(bin->cur->buf) ||
444  section->paddr + section->size > rz_buf_size(bin->cur->buf)) {
445  free(buf);
446  free(ret);
447  return false;
448  }
449  r = rz_buf_read_at(bin->cur->buf, section->paddr,
450  buf, section->size);
451  if (r < 1) {
452  free(buf);
453  free(ret);
454  return false;
455  }
456  // it does mean the user specified an output file
457  if (strcmp(output, file)) {
459  } else {
460  rz_hex_bin2str(buf, section->size, ret);
461  printf("%s\n", ret);
462  }
463  free(buf);
464  free(ret);
465  break;
466  }
467  }
468  return true;
469 }
RZ_DEPRECATE RZ_API RZ_BORROW RzList * rz_bin_get_sections(RZ_NONNULL RzBin *bin)
Definition: bin.c:597
RzList * sections(RzBinFile *bf)
Definition: bin_ne.c:110
#define r
Definition: crypto_rc6.c:12
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * malloc(size_t size)
Definition: malloc.c:123
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
RZ_API bool rz_file_dump(const char *file, const ut8 *buf, int len, bool append)
Definition: file.c:838
RZ_API int rz_hex_bin2str(const ut8 *in, int len, char *out)
Definition: hex.c:382
Definition: malloc.c:26
unsigned char * buf
Definition: gzjoin.c:83
Definition: gzappend.c:170
uint32_t size
diff_output_t output
Definition: zipcmp.c:237

References bin::buf, free(), malloc(), output, printf(), r, rz_bin_get_sections(), rz_buf_read_at(), rz_buf_size(), rz_file_dump(), rz_hex_bin2str(), sections(), and section::size.

Referenced by rabin_do_operation().

◆ __lib_bin_cb()

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

Definition at line 585 of file rz-bin.c.

585  {
586  struct rz_bin_plugin_t *hand = (struct rz_bin_plugin_t *)data;
587  RzBin *bin = user;
588  rz_bin_plugin_add(bin, hand);
589  return true;
590 }
RZ_API bool rz_bin_plugin_add(RzBin *bin, RzBinPlugin *foo)
Definition: bin.c:404
void * user
Definition: rz_bin.h:558

References rz_bin_plugin_add(), and rz_bin_plugin_t::user.

Referenced by rz_main_rz_bin().

◆ __lib_bin_dt()

static int __lib_bin_dt ( RzLibPlugin pl,
void *  p,
void *  u 
)
static

Definition at line 592 of file rz-bin.c.

592  {
593  return true;
594 }

Referenced by rz_main_rz_bin().

◆ __lib_bin_xtr_cb()

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

Definition at line 597 of file rz-bin.c.

597  {
598  struct rz_bin_xtr_plugin_t *hand = (struct rz_bin_xtr_plugin_t *)data;
599  RzBin *bin = user;
600  // printf(" * Added (dis)assembly plugin\n");
601  rz_bin_xtr_add(bin, hand);
602  return true;
603 }
RZ_API bool rz_bin_xtr_add(RzBin *bin, RzBinXtrPlugin *foo)
Definition: bin.c:421

References rz_bin_xtr_add().

Referenced by rz_main_rz_bin().

◆ __lib_bin_xtr_dt()

static int __lib_bin_xtr_dt ( RzLibPlugin pl,
void *  p,
void *  u 
)
static

Definition at line 605 of file rz-bin.c.

605  {
606  return true;
607 }

Referenced by rz_main_rz_bin().

◆ __lib_demangler_cb()

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

Definition at line 576 of file rz-bin.c.

576  {
578  return true;
579 }
RZ_API bool rz_demangler_plugin_add(RZ_NONNULL RzDemangler *dem, RZ_NONNULL RzDemanglerPlugin *plugin)
Adds a new demangler plugin to the plugin list.
Definition: demangler.c:144

References rz_demangler_plugin_add().

Referenced by rz_main_rz_bin().

◆ __lib_demangler_dt()

static int __lib_demangler_dt ( RzLibPlugin pl,
void *  p,
void *  u 
)
static

Definition at line 581 of file rz-bin.c.

581  {
582  return true;
583 }

Referenced by rz_main_rz_bin().

◆ __listPlugins()

static void __listPlugins ( RzBin bin,
const char *  plugin_name,
PJ pj,
int  rad 
)
static

Definition at line 609 of file rz-bin.c.

609  {
610  int format = 0;
611  RzCmdStateOutput state = { 0 };
612  if (rad == RZ_MODE_JSON) {
613  format = 'j';
615  } else if (rad) {
616  format = 'q';
618  } else {
620  }
621  bin->cb_printf = (PrintfCallback)printf;
622  if (plugin_name) {
623  rz_bin_list_plugin(bin, plugin_name, pj, format);
624  } else {
628  rz_cons_flush();
629  }
630 }
RZ_API bool rz_bin_list_plugin(RzBin *bin, const char *name, PJ *pj, int json)
Definition: bin.c:510
RZ_API RzCmdStatus rz_core_bin_plugins_print(RzBin *bin, RzCmdStateOutput *state)
Definition: cbin.c:4855
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
int(* PrintfCallback)(const char *str,...) RZ_PRINTF_CHECK(1
Definition: rz_types.h:233
#define RZ_MODE_JSON
Definition: rz_types.h:29
@ RZ_OUTPUT_MODE_JSON
Definition: rz_types.h:40
@ RZ_OUTPUT_MODE_QUIET
Definition: rz_types.h:42
@ RZ_OUTPUT_MODE_STANDARD
Definition: rz_types.h:39
Represent the output state of a command handler.
Definition: rz_cmd.h:91

References printf(), rz_bin_list_plugin(), rz_cmd_state_output_fini(), rz_cmd_state_output_init(), rz_cmd_state_output_print(), rz_cons_flush(), rz_core_bin_plugins_print(), RZ_MODE_JSON, RZ_OUTPUT_MODE_JSON, RZ_OUTPUT_MODE_QUIET, and RZ_OUTPUT_MODE_STANDARD.

Referenced by rz_main_rz_bin().

◆ __sdb_prompt()

static void __sdb_prompt ( Sdb sdb)
static

Definition at line 260 of file rz-bin.c.

260  {
261  char *line;
262  for (; (line = stdin_gets(false));) {
263  sdb_query(sdb, line);
264  free(line);
265  }
266 }
static char * stdin_gets(bool liberate)
Definition: rz-bin.c:235
RZ_API int sdb_query(Sdb *s, const char *cmd)
Definition: query.c:785
line
Definition: setup.py:34

References free(), setup::line, sdb_query(), and stdin_gets().

Referenced by rz_main_rz_bin().

◆ actions2mask()

static ut32 actions2mask ( ut64  action)
static

Definition at line 79 of file rz-bin.c.

79  {
80  ut32 res = 0;
82  res |= RZ_CORE_BIN_ACC_SECTIONS;
83  }
85  res |= RZ_CORE_BIN_ACC_SEGMENTS;
86  }
87  if (action & RZ_BIN_REQ_ENTRIES) {
88  res |= RZ_CORE_BIN_ACC_ENTRIES;
89  }
91  res |= RZ_CORE_BIN_ACC_INITFINI;
92  }
93  if (action & RZ_BIN_REQ_MAIN) {
94  res |= RZ_CORE_BIN_ACC_MAIN;
95  }
96  if (action & RZ_BIN_REQ_IMPORTS) {
97  res |= RZ_CORE_BIN_ACC_IMPORTS;
98  }
99  if (action & RZ_BIN_REQ_CLASSES) {
100  res |= RZ_CORE_BIN_ACC_CLASSES;
101  }
102  if (action & RZ_BIN_REQ_SYMBOLS) {
103  res |= RZ_CORE_BIN_ACC_SYMBOLS;
104  }
105  if (action & RZ_BIN_REQ_EXPORTS) {
106  res |= RZ_CORE_BIN_ACC_EXPORTS;
107  }
109  res |= RZ_CORE_BIN_ACC_RESOURCES;
110  }
111  if (action & RZ_BIN_REQ_STRINGS) {
112  res |= RZ_CORE_BIN_ACC_STRINGS;
113  }
114  if (action & RZ_BIN_REQ_INFO) {
115  res |= RZ_CORE_BIN_ACC_INFO;
116  }
117  if (action & RZ_BIN_REQ_FIELDS) {
118  res |= RZ_CORE_BIN_ACC_FIELDS;
119  }
120  if (action & RZ_BIN_REQ_HEADER) {
121  res |= RZ_CORE_BIN_ACC_HEADER;
122  }
123  if (action & RZ_BIN_REQ_LIBS) {
124  res |= RZ_CORE_BIN_ACC_LIBS;
125  }
126  if (action & RZ_BIN_REQ_RELOCS) {
127  res |= RZ_CORE_BIN_ACC_RELOCS;
128  }
129  if (action & RZ_BIN_REQ_DWARF) {
130  res |= RZ_CORE_BIN_ACC_DWARF;
131  }
132  if (action & RZ_BIN_REQ_PDB) {
133  res |= RZ_CORE_BIN_ACC_PDB;
134  }
135  if (action & RZ_BIN_REQ_SIZE) {
136  res |= RZ_CORE_BIN_ACC_SIZE;
137  }
139  res |= RZ_CORE_BIN_ACC_VERSIONINFO;
140  }
142  res |= RZ_CORE_BIN_ACC_SIGNATURE;
143  }
145  res |= RZ_CORE_BIN_ACC_SECTIONS_MAPPING;
146  }
147  if (action & RZ_BIN_REQ_BASEFIND) {
148  res |= RZ_CORE_BIN_ACC_BASEFIND;
149  }
150  return res;
151 }
uint32_t ut32
#define RZ_BIN_REQ_VERSIONINFO
Definition: rz_bin.h:68
#define RZ_BIN_REQ_SECTIONS_MAPPING
Definition: rz_bin.h:78
#define RZ_BIN_REQ_INITFINI
Definition: rz_bin.h:73
#define RZ_BIN_REQ_RELOCS
Definition: rz_bin.h:58
#define RZ_BIN_REQ_SECTIONS
Definition: rz_bin.h:48
#define RZ_BIN_REQ_ENTRIES
Definition: rz_bin.h:45
#define RZ_BIN_REQ_HEADER
Definition: rz_bin.h:70
#define RZ_BIN_REQ_PDB
Definition: rz_bin.h:64
#define RZ_BIN_REQ_LIBS
Definition: rz_bin.h:54
#define RZ_BIN_REQ_SYMBOLS
Definition: rz_bin.h:47
#define RZ_BIN_REQ_CLASSES
Definition: rz_bin.h:61
#define RZ_BIN_REQ_IMPORTS
Definition: rz_bin.h:46
#define RZ_BIN_REQ_BASEFIND
Definition: rz_bin.h:80
#define RZ_BIN_REQ_DWARF
Definition: rz_bin.h:62
#define RZ_BIN_REQ_RESOURCES
Definition: rz_bin.h:72
#define RZ_BIN_REQ_FIELDS
Definition: rz_bin.h:53
#define RZ_BIN_REQ_MAIN
Definition: rz_bin.h:56
#define RZ_BIN_REQ_STRINGS
Definition: rz_bin.h:52
#define RZ_BIN_REQ_INFO
Definition: rz_bin.h:49
#define RZ_BIN_REQ_SIZE
Definition: rz_bin.h:63
#define RZ_BIN_REQ_SIGNATURE
Definition: rz_bin.h:76
#define RZ_BIN_REQ_EXPORTS
Definition: rz_bin.h:67
#define RZ_BIN_REQ_SEGMENTS
Definition: rz_bin.h:74

References test-lz4-speed::action, RZ_BIN_REQ_BASEFIND, RZ_BIN_REQ_CLASSES, RZ_BIN_REQ_DWARF, RZ_BIN_REQ_ENTRIES, RZ_BIN_REQ_EXPORTS, RZ_BIN_REQ_FIELDS, RZ_BIN_REQ_HEADER, RZ_BIN_REQ_IMPORTS, RZ_BIN_REQ_INFO, RZ_BIN_REQ_INITFINI, RZ_BIN_REQ_LIBS, RZ_BIN_REQ_MAIN, RZ_BIN_REQ_PDB, RZ_BIN_REQ_RELOCS, RZ_BIN_REQ_RESOURCES, RZ_BIN_REQ_SECTIONS, RZ_BIN_REQ_SECTIONS_MAPPING, RZ_BIN_REQ_SEGMENTS, RZ_BIN_REQ_SIGNATURE, RZ_BIN_REQ_SIZE, RZ_BIN_REQ_STRINGS, RZ_BIN_REQ_SYMBOLS, and RZ_BIN_REQ_VERSIONINFO.

Referenced by rz_main_rz_bin().

◆ add_footer()

static bool add_footer ( RzCmdStateOutput main_state,
RzCmdStateOutput state 
)
static

Definition at line 29 of file rz-bin.c.

29  {
30  if (state->mode == RZ_OUTPUT_MODE_TABLE) {
31  char *s = rz_table_tostring(state->d.t);
32  if (!s) {
33  return false;
34  }
35  rz_cons_printf("%s\n", s);
36  free(s);
37  } else if (state->mode == RZ_OUTPUT_MODE_JSON) {
38  const char *state_json = pj_string(state->d.pj);
39  pj_raw(main_state->d.pj, state_json);
40  }
42  return true;
43 }
RZ_API void rz_cmd_state_output_free(RZ_NONNULL RzCmdStateOutput *state)
Free the RzCmdStateOutput structure and its inner fields appropriately.
Definition: cmd_api.c:2624
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
static RzSocket * s
Definition: rtr.c:28
RZ_API void pj_raw(PJ *j, const char *k)
Definition: pj.c:7
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API char * rz_table_tostring(RzTable *t)
Definition: table.c:510
@ RZ_OUTPUT_MODE_TABLE
Definition: rz_types.h:46
union rz_cmd_state_output_t::@259 d

References rz_cmd_state_output_t::d, free(), rz_cmd_state_output_t::pj, pj_raw(), pj_string(), rz_cmd_state_output_free(), rz_cons_printf(), RZ_OUTPUT_MODE_JSON, RZ_OUTPUT_MODE_TABLE, rz_table_tostring(), and s.

Referenced by rz_main_rz_bin().

◆ add_header()

static RzCmdStateOutput* add_header ( RzCmdStateOutput main_state,
RzOutputMode  mode,
const char *  header 
)
static

Definition at line 45 of file rz-bin.c.

45  {
49  rz_cons_printf("[%c%s]\n", toupper(header[0]), header + 1);
50  } else if (mode == RZ_OUTPUT_MODE_JSON) {
51  pj_k(main_state->d.pj, header);
52  }
53  return state;
54 }
#define header(is_bt, len_min, ret_op)
RZ_API PJ * pj_k(PJ *j, const char *k)
Definition: pj.c:104
#define RZ_NEW(x)
Definition: rz_types.h:285
#define toupper(c)
Definition: safe-ctype.h:147

References rz_cmd_state_output_t::d, header, rz_cmd_state_output_t::pj, pj_k(), rz_cmd_state_output_init(), rz_cons_printf(), RZ_NEW, RZ_OUTPUT_MODE_JSON, RZ_OUTPUT_MODE_STANDARD, RZ_OUTPUT_MODE_TABLE, and toupper.

Referenced by rz_main_rz_bin().

◆ classes_as_source_print()

static void classes_as_source_print ( RzCore core,
RzCmdStateOutput state 
)
static

Definition at line 56 of file rz-bin.c.

56  {
57  RzBinFile *bf = rz_bin_cur(core->bin);
58  if (bf) {
60  }
61 }
RZ_API RzBinFile * rz_bin_cur(RzBin *bin)
Definition: bin.c:895
RZ_API bool rz_core_bin_class_as_source_print(RZ_NONNULL RzCore *core, RZ_NONNULL RzBinFile *bf, const char *class_name)
Definition: cbin.c:3773
#define NULL
Definition: cris-opc.c:27
XX curplugin == o->plugin.
Definition: rz_bin.h:298
RzBin * bin
Definition: rz_core.h:298

References rz_core_t::bin, NULL, rz_bin_cur(), and rz_core_bin_class_as_source_print().

Referenced by rz_main_rz_bin().

◆ end_state()

static void end_state ( RzCmdStateOutput state)
static

Definition at line 19 of file rz-bin.c.

19  {
20  if (state->mode == RZ_OUTPUT_MODE_JSON) {
21  pj_end(state->d.pj);
22  const char *s = pj_string(state->d.pj);
23  if (s) {
24  rz_cons_printf("%s\n", s);
25  }
26  }
27 }
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87

References pj_end(), pj_string(), rz_cons_printf(), RZ_OUTPUT_MODE_JSON, and s.

Referenced by rz_main_rz_bin().

◆ extract_binobj()

static bool extract_binobj ( const RzBinFile bf,
RzBinXtrData data,
int  idx 
)
static

Definition at line 284 of file rz-bin.c.

284  {
285  ut64 bin_size = data ? data->size : 0;
286  ut8 *bytes;
287  const char *xtr_type = "";
288  char *arch = "unknown";
289  int bits = 0, nb;
290  char *libname = NULL;
291  const char *filename = bf ? bf->file : NULL;
292  char *path = NULL, *ptr = NULL;
293  bool res = false;
294 
295  if (!bf || !data || !filename) {
296  return false;
297  }
298  if (data->metadata) {
299  arch = data->metadata->arch;
300  bits = data->metadata->bits;
301  libname = data->metadata->libname;
302  xtr_type = data->metadata->xtr_type;
303  }
304  if (!strcmp(xtr_type, "fat") && bin_size == bf->size && bin_size) {
305  eprintf("This is not a fat bin\n");
306  return false;
307  }
308  bytes = malloc(bin_size);
309  if (!bytes) {
310  eprintf("error: BinFile buffer is empty\n");
311  return false;
312  }
313  nb = rz_buf_read_at(data->buf, 0, bytes, bin_size);
314  if (nb <= 0) {
315  eprintf("Couldn't read xtrdata\n");
316  return false;
317  }
318  if (!arch) {
319  arch = "unknown";
320  }
321  path = strdup(filename);
322  if (!path) {
323  return false;
324  }
325  ptr = (char *)rz_file_basename(path);
326  char *outpath = rz_str_newf("%s.fat", ptr);
327  if (!outpath || !rz_sys_mkdirp(outpath)) {
328  free(path);
329  free(outpath);
330  eprintf("Error creating dir structure\n");
331  return false;
332  }
333 
334  char *outfile = libname
335  ? rz_str_newf("%s/%s.%s.%s_%i.%d", outpath, ptr, arch, libname, bits, idx)
336  : rz_str_newf("%s/%s.%s_%i.%d", outpath, ptr, arch, bits, idx);
337 
338  if (!outfile || !rz_file_dump(outfile, bytes, bin_size, 0)) {
339  eprintf("Error extracting %s\n", outfile);
340  res = false;
341  } else {
342  printf("%s created (%" PFMT64d ")\n", outfile, bin_size);
343  res = true;
344  }
345 
346  free(outfile);
347  free(outpath);
348  free(path);
349  free(bytes);
350  return res;
351 }
static ut8 bytes[32]
Definition: asm_arc.c:23
int bits(struct state *s, int need)
Definition: blast.c:72
cs_arch arch
Definition: cstool.c:13
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
FILE * outfile
Definition: fuzz_diff.c:16
const char * filename
Definition: ioapi.h:137
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")
int idx
Definition: setup.py:197
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API const char * rz_file_basename(const char *path)
Definition: file.c:83
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API bool rz_sys_mkdirp(const char *dir)
Definition: sys.c:691
#define PFMT64d
Definition: rz_types.h:394
char * file
Definition: rz_bin.h:299
RzBuffer * buf
Definition: rz_bin.h:380
RzBinXtrMetadata * metadata
Definition: rz_bin.h:386
const char * xtr_type
Definition: rz_bin.h:374
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References rz_bin_xtr_metadata_t::arch, arch, rz_bin_xtr_metadata_t::bits, bits(), rz_bin_xtr_extract_t::buf, bytes, eprintf, rz_bin_file_t::file, free(), setup::idx, rz_bin_xtr_metadata_t::libname, malloc(), rz_bin_xtr_extract_t::metadata, NULL, outfile, path, PFMT64d, printf(), rz_buf_read_at(), rz_file_basename(), rz_file_dump(), rz_str_newf(), rz_sys_mkdirp(), rz_bin_file_t::size, rz_bin_xtr_extract_t::size, strdup(), ut64(), and rz_bin_xtr_metadata_t::xtr_type.

Referenced by rabin_extract().

◆ isBinopHelp()

static bool isBinopHelp ( const char *  op)
static

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

268  {
269  if (!op) {
270  return false;
271  }
272  if (!strcmp(op, "help")) {
273  return true;
274  }
275  if (!strcmp(op, "?")) {
276  return true;
277  }
278  if (!strcmp(op, "h")) {
279  return true;
280  }
281  return false;
282 }
Definition: dis.c:32

Referenced by rz_main_rz_bin().

◆ print_demangler_info()

static bool print_demangler_info ( const RzDemanglerPlugin plugin,
void *  user 
)
static

Definition at line 632 of file rz-bin.c.

632  {
633  (void)user;
634  printf("%-6s %-8s %s\n", plugin->language, plugin->license, plugin->author);
635  return true;
636 }
const char * language
demangler language
Definition: rz_demangler.h:17
const char * author
demangler author
Definition: rz_demangler.h:18
const char * license
demangler license
Definition: rz_demangler.h:19

References rz_demangler_plugin_t::author, rz_demangler_plugin_t::language, rz_demangler_plugin_t::license, and printf().

Referenced by rz_main_rz_bin().

◆ print_string()

static void print_string ( RzBinFile bf,
RzBinString string,
PJ pj,
int  mode 
)
static

Definition at line 638 of file rz-bin.c.

638  {
639  rz_return_if_fail(bf && string);
640 
641  ut64 vaddr;
642  RzBinSection *s = rz_bin_get_section_at(bf->o, string->paddr, false);
643  if (s) {
644  string->vaddr = s->vaddr + (string->paddr - s->paddr);
645  }
646  vaddr = bf->o ? rz_bin_object_get_vaddr(bf->o, string->paddr, string->vaddr) : UT64_MAX;
647  const char *type_string = rz_str_enc_as_string(string->type);
648  const char *section_name = s ? s->name : "";
649 
650  switch (mode) {
651  case RZ_MODE_JSON:
652  pj_o(pj);
653  pj_kn(pj, "vaddr", vaddr);
654  pj_kn(pj, "paddr", string->paddr);
655  pj_kn(pj, "ordinal", string->ordinal);
656  pj_kn(pj, "size", string->size);
657  pj_kn(pj, "length", string->length);
658  pj_ks(pj, "section", section_name);
659  pj_ks(pj, "type", type_string);
660  pj_ks(pj, "string", string->string);
661  pj_end(pj);
662  break;
663  case RZ_MODE_SIMPLEST:
664  printf("%s\n", string->string);
665  break;
666  case RZ_MODE_SIMPLE:
667  printf("0x%" PFMT64x " %u %u %s\n", vaddr, string->size, string->length, string->string);
668  break;
669  case RZ_MODE_PRINT:
670  printf("%03u 0x%08" PFMT64x " 0x%08" PFMT64x " %u %u (%s) %s %s\n",
671  string->ordinal, string->paddr, vaddr,
672  string->length, string->size,
673  section_name, type_string, string->string);
674  break;
675  default:
677  break;
678  }
679 }
RZ_API RZ_BORROW RzBinSection * rz_bin_get_section_at(RzBinObject *o, ut64 off, int va)
Find the binary section at offset off.
Definition: bin.c:611
RZ_API ut64 rz_bin_object_get_vaddr(RzBinObject *o, ut64 paddr, ut64 vaddr)
Definition: bobj.c:669
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
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_kn(PJ *j, const char *k, ut64 n)
Definition: pj.c:121
RZ_API const char * rz_str_enc_as_string(RzStrEnc enc)
Definition: str.c:44
#define RZ_MODE_SIMPLEST
Definition: rz_types.h:31
#define RZ_MODE_PRINT
Definition: rz_types.h:25
#define RZ_MODE_SIMPLE
Definition: rz_types.h:28
#define PFMT64x
Definition: rz_types.h:393
#define UT64_MAX
Definition: rz_types_base.h:86
RzBinObject * o
Definition: rz_bin.h:305
ut32 ordinal
Definition: rz_bin.h:755
char * string
Definition: rz_bin.h:752

References rz_bin_string_t::length, rz_bin_file_t::o, rz_bin_string_t::ordinal, rz_bin_string_t::paddr, PFMT64x, pj_end(), pj_kn(), pj_ks(), pj_o(), printf(), rz_bin_get_section_at(), rz_bin_object_get_vaddr(), RZ_MODE_JSON, RZ_MODE_PRINT, RZ_MODE_SIMPLE, RZ_MODE_SIMPLEST, rz_return_if_fail, rz_str_enc_as_string(), rz_warn_if_reached, s, rz_bin_string_t::size, rz_bin_string_t::string, rz_bin_string_t::type, ut64(), UT64_MAX, and rz_bin_string_t::vaddr.

Referenced by rz_main_rz_bin().

◆ rabin_do_operation()

static int rabin_do_operation ( RzBin bin,
const char *  op,
int  rad,
const char *  output,
const char *  file 
)
static

Definition at line 471 of file rz-bin.c.

471  {
472  char *arg = NULL, *ptr = NULL, *ptr2 = NULL;
473  bool rc = true;
474 
475  /* Implement alloca with fixed-size buffer? */
476  if (!(arg = strdup(op))) {
477  return false;
478  }
479  if ((ptr = strchr(arg, '/'))) {
480  *ptr++ = 0;
481  if ((ptr2 = strchr(ptr, '/'))) {
482  ptr2[0] = '\0';
483  ptr2++;
484  }
485  }
486  if (!output) {
487  output = file;
488  }
489  RzBinFile *bf = rz_bin_cur(bin);
490  if (bf) {
491  RzBuffer *nb = rz_buf_new_with_buf(bf->buf);
492  rz_buf_free(bf->buf);
493  bf->buf = nb;
494  }
495 
496  switch (arg[0]) {
497  case 'd':
498  if (!ptr) {
499  goto _rabin_do_operation_error;
500  }
501  switch (*ptr) {
502  case 's': {
503  ut64 a = ptr2 ? rz_num_math(NULL, ptr2) : 0;
504  if (!rabin_dump_symbols(bin, a)) {
505  goto error;
506  }
507  } break;
508  case 'S':
509  if (!ptr2) {
510  goto _rabin_do_operation_error;
511  }
512  if (!__dumpSections(bin, ptr2, output, file)) {
513  goto error;
514  }
515  break;
516  default:
517  goto _rabin_do_operation_error;
518  }
519  break;
520  case 'C': {
521  RzBinFile *cur = rz_bin_cur(bin);
523  if (!plg && cur) {
524  // are we in xtr?
525  if (cur->xtr_data) {
526  // load the first one
527  RzBinXtrData *xtr_data = rz_list_get_n(cur->xtr_data, 0);
528  RzBinObjectLoadOptions obj_opts = {
529  .baseaddr = UT64_MAX,
530  .loadaddr = rz_bin_get_laddr(bin)
531  };
532  if (xtr_data && !xtr_data->loaded && !rz_bin_file_object_new_from_xtr_data(bin, cur, &obj_opts, xtr_data)) {
533  break;
534  }
535  }
536  plg = rz_bin_file_cur_plugin(cur);
537  if (!plg) {
538  break;
539  }
540  }
541  if (plg && plg->signature) {
542  char *sign = plg->signature(cur, rad == RZ_MODE_JSON);
543  if (sign) {
544  rz_cons_println(sign);
545  rz_cons_flush();
546  free(sign);
547  }
548  }
549  } break;
550  default:
551  _rabin_do_operation_error:
552  eprintf("Unknown operation. use -O help\n");
553  goto error;
554  }
555  if (!rc) {
556  eprintf("Cannot dump :(\n");
557  }
558  free(arg);
559  return true;
560 error:
561  free(arg);
562  return false;
563 }
RZ_API RzBinPlugin * rz_bin_file_cur_plugin(RzBinFile *bf)
Definition: bfile.c:348
RZ_API bool rz_bin_file_object_new_from_xtr_data(RzBin *bin, RzBinFile *bf, RzBinObjectLoadOptions *opts, RzBinXtrData *data)
Definition: bfile.c:94
RZ_API ut64 rz_bin_get_laddr(RzBin *bin)
Definition: bin.c:542
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
static bool __dumpSections(RzBin *bin, const char *scnname, const char *output, const char *file)
Definition: rz-bin.c:418
static int rabin_dump_symbols(RzBin *bin, int len)
Definition: rz-bin.c:380
RZ_API RZ_BORROW void * rz_list_get_n(RZ_NONNULL const RzList *list, ut32 n)
Returns the N-th element of the list.
Definition: list.c:574
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_buf(RzBuffer *b)
Creates a new buffer from a source buffer.
Definition: buf.c:448
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
#define a(i)
Definition: sha256.c:41
ut64 baseaddr
where the linker maps the binary in memory
Definition: rz_bin.h:248
RzBuffer * buf
Definition: rz_bin.h:303
RzList * xtr_data
Definition: rz_bin.h:314
char *(* signature)(RzBinFile *bf, bool json)
Definition: rz_bin.h:543
void error(const char *msg)
Definition: untgz.c:593
static int file
Definition: z80asm.c:58

References __dumpSections(), a, rz_bin_file_load_options_t::baseaddr, rz_bin_file_t::buf, eprintf, error(), file, free(), rz_bin_xtr_extract_t::loaded, NULL, output, rabin_dump_symbols(), rz_bin_cur(), rz_bin_file_cur_plugin(), rz_bin_file_object_new_from_xtr_data(), rz_bin_get_laddr(), rz_buf_free(), rz_buf_new_with_buf(), rz_cons_flush(), rz_cons_println(), rz_list_get_n(), RZ_MODE_JSON, rz_num_math(), rz_bin_plugin_t::signature, strdup(), ut64(), UT64_MAX, and rz_bin_file_t::xtr_data.

Referenced by rz_main_rz_bin().

◆ rabin_dump_symbols()

static int rabin_dump_symbols ( RzBin bin,
int  len 
)
static

Definition at line 380 of file rz-bin.c.

380  {
382  if (!symbols) {
383  return false;
384  }
385 
386  RzListIter *iter;
387  RzBinSymbol *symbol;
388  int olen = len;
389  rz_list_foreach (symbols, iter, symbol) {
390  if (symbol->size && (olen > symbol->size || !olen)) {
391  len = symbol->size;
392  } else if (!symbol->size && !olen) {
393  len = 32;
394  } else {
395  len = olen;
396  }
397  ut8 *buf = calloc(1, len);
398  if (!buf) {
399  return false;
400  }
401  char *ret = malloc((len * 2) + 1);
402  if (!ret) {
403  free(buf);
404  return false;
405  }
406  if (rz_buf_read_at(bin->cur->buf, symbol->paddr, buf, len) == len) {
407  rz_hex_bin2str(buf, len, ret);
408  printf("%s %s\n", symbol->name, ret);
409  } else {
410  eprintf("Cannot read from buffer\n");
411  }
412  free(buf);
413  free(ret);
414  }
415  return true;
416 }
size_t len
Definition: 6502dis.c:15
RZ_DEPRECATE RZ_API RZ_BORROW RzList * rz_bin_get_symbols(RZ_NONNULL RzBin *bin)
Definition: bin.c:696
RzList * symbols(RzBinFile *bf)
Definition: bin_ne.c:102
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
char * name
Definition: rz_bin.h:675

References bin::buf, calloc(), eprintf, free(), len, malloc(), rz_bin_symbol_t::name, rz_bin_symbol_t::paddr, printf(), rz_bin_get_symbols(), rz_buf_read_at(), rz_hex_bin2str(), rz_bin_symbol_t::size, and symbols().

Referenced by rabin_do_operation().

◆ rabin_extract()

static int rabin_extract ( RzBin bin,
int  all 
)
static

Definition at line 353 of file rz-bin.c.

353  {
354  RzBinXtrData *data = NULL;
355  int res = false;
356  RzBinFile *bf = rz_bin_cur(bin);
357 
358  if (!bf) {
359  return res;
360  }
361  if (all) {
362  int idx = 0;
363  RzListIter *iter;
364  rz_list_foreach (bf->xtr_data, iter, data) {
365  res = extract_binobj(bf, data, idx++);
366  if (!res) {
367  break;
368  }
369  }
370  } else {
371  data = rz_list_get_n(bf->xtr_data, 0);
372  if (!data) {
373  return res;
374  }
375  res = extract_binobj(bf, data, 0);
376  }
377  return res;
378 }
static bool extract_binobj(const RzBinFile *bf, RzBinXtrData *data, int idx)
Definition: rz-bin.c:284

References test_group_name::all, extract_binobj(), setup::idx, NULL, rz_bin_cur(), rz_list_get_n(), and rz_bin_file_t::xtr_data.

Referenced by rz_main_rz_bin().

◆ rabin_show_help()

static int rabin_show_help ( int  v)
static

Definition at line 153 of file rz-bin.c.

153  {
154  printf("Usage: rz-bin [-AcdeEghHiIjlLMqrRsSUvVxzZ] [-@ at] [-a arch] [-b bits] [-B addr]\n"
155  " [-C F:C:D] [-f str] [-m addr] [-n str] [-N m:M] [-P[-P] pdb]\n"
156  " [-o str] [-O str] [-k query] [-D lang symname] file\n");
157  if (v) {
158  printf(
159  " -@ [addr] show section, symbol or import at addr\n"
160  " -A list sub-binaries and their arch-bits pairs\n"
161  " -a [arch] set arch (x86, arm, .. or <arch>_<bits>)\n"
162  " -b [bits] set bits (32, 64 ...)\n"
163  " -B [addr] override base address (pie bins)\n"
164  " -c list classes\n"
165  " -cc list classes in header format\n"
166  " -C [fmt:C:D] create [elf,mach0,pe] with Code and Data hexpairs (see -a)\n"
167  " -d show debug/dwarf information\n"
168  " -D lang name demangle symbol name (-D all for bin.demangle=true)\n"
169  " -e entrypoint\n"
170  " -ee constructor/destructor entrypoints\n"
171  " -E globally exportable symbols\n"
172  " -f [str] select sub-bin named str\n"
173  " -F [binfmt] force to use that bin plugin (ignore header check)\n"
174  " -g same as -SMZIHVResizcld -SS -SSS -ee (show all info)\n"
175  " -G [addr] load address . offset to header\n"
176  " -h this help message\n"
177  " -H header fields\n"
178  " -i imports (symbols imported from libraries)\n"
179  " -I binary info\n"
180  " -j output in json\n"
181  " -k [sdb-query] run sdb query. for example: '*'\n"
182  " -K [algo] calculate checksums (md5, sha1, ..)\n"
183  " -l linked libraries\n"
184  " -L [plugin] list supported bin plugins or plugin details\n"
185  " -m [addr] show source line at addr\n"
186  " -M main (show address of main symbol)\n"
187  " -n [str] show section, symbol or import named str\n"
188  " -N [min:max] force min:max number of chars per string (see -z and -zz)\n"
189  " -o [str] output file/folder for write operations (out by default)\n"
190  " -O [str] write/extract operations (-O help)\n"
191  " -p show physical addresses\n"
192  " -P show debug/pdb information\n"
193  " -PP download pdb file for binary\n"
194  " -q be quiet, just show fewer data\n"
195  " -qq show less info (no offset/size for -z for ex.)\n"
196  " -Q show load address used by dlopen (non-aslr libs)\n"
197  " -r rizin output\n"
198  " -R relocations\n"
199  " -s symbols\n"
200  " -S sections\n"
201  " -SS segments\n"
202  " -SSS sections mapping to segments\n"
203  " -T display file signature\n"
204  " -u unfiltered (no rename duplicated symbols/sections)\n"
205  " -U resoUrces\n"
206  " -v display version and quit\n"
207  " -V Show binary version information\n"
208  " -w display try/catch blocks\n"
209  " -x extract bins contained in file\n"
210  " -X [fmt] [f] .. package in fat or zip the given files and bins contained in file\n"
211  " -Y [fw file] calculates all the possibles base address candidates of a firmware bin\n"
212  " -z strings (from data section)\n"
213  " -zz strings (from raw strings from bin)\n"
214  " -zzz dump raw strings to stdout (for huge files)\n"
215  " -Z guess size of binary program\n");
216  }
217  if (v) {
218  printf("Environment:\n"
219  " RZ_BIN_LANG: e bin.lang # assume lang for demangling\n"
220  " RZ_BIN_NOPLUGINS: # do not load shared plugins (speedup loading)\n"
221  " RZ_BIN_DEMANGLE=0:e bin.demangle # do not demangle symbols\n"
222  " RZ_BIN_MAXSTRBUF: e bin.maxstrbuf # specify maximum buffer size\n"
223  " RZ_BIN_STRFILTER: e bin.str.filter # rizin -qc 'e bin.str.filter=?"
224  "?' -\n"
225  " RZ_BIN_STRPURGE: e bin.str.purge # try to purge false positives\n"
226  " RZ_BIN_DEBASE64: e bin.debase64 # try to debase64 all strings\n"
227  " RZ_BIN_PDBSERVER: e pdb.server # use alternative PDB server\n"
228  " RZ_BIN_SYMSTORE: e pdb.symstore # path to downstream symbol store\n"
229  " RZ_BIN_PREFIX: e bin.prefix # prefix symbols/sections/relocs with a specific string\n"
230  " RZ_CONFIG: # sdb config file\n");
231  }
232  return 1;
233 }
const char * v
Definition: dsignal.c:12

References printf(), and v.

Referenced by rz_main_rz_bin().

◆ rabin_show_srcline()

static int rabin_show_srcline ( RzBin bin,
ut64  at 
)
static

Definition at line 565 of file rz-bin.c.

565  {
566  char *srcline;
567  if (at != UT64_MAX && (srcline = rz_bin_addr2text(bin, at, true))) {
568  printf("%s\n", srcline);
569  free(srcline);
570  return true;
571  }
572  return false;
573 }
RZ_DEPRECATE RZ_API char * rz_bin_addr2text(RzBin *bin, ut64 addr, int origin)
Definition: dbginfo.c:217

References free(), printf(), rz_bin_addr2text(), and UT64_MAX.

Referenced by rz_main_rz_bin().

◆ rad2outputmode()

static RzOutputMode rad2outputmode ( int  rad)
static

Definition at line 63 of file rz-bin.c.

63  {
64  switch (rad) {
65  case RZ_MODE_JSON:
66  return RZ_OUTPUT_MODE_JSON;
67  case RZ_MODE_SIMPLE:
68  return RZ_OUTPUT_MODE_QUIET;
69  case RZ_MODE_SIMPLEST:
71  case RZ_MODE_RIZINCMD:
72  return RZ_OUTPUT_MODE_RIZIN;
73  case RZ_MODE_PRINT:
74  default:
76  }
77 }
#define RZ_MODE_RIZINCMD
Definition: rz_types.h:26
@ RZ_OUTPUT_MODE_QUIETEST
Definition: rz_types.h:47
@ RZ_OUTPUT_MODE_RIZIN
Definition: rz_types.h:41

References RZ_MODE_JSON, RZ_MODE_PRINT, RZ_MODE_RIZINCMD, RZ_MODE_SIMPLE, RZ_MODE_SIMPLEST, RZ_OUTPUT_MODE_JSON, RZ_OUTPUT_MODE_QUIET, RZ_OUTPUT_MODE_QUIETEST, RZ_OUTPUT_MODE_RIZIN, and RZ_OUTPUT_MODE_STANDARD.

Referenced by rz_main_rz_bin().

◆ rz_main_rz_bin()

RZ_API int rz_main_rz_bin ( int  argc,
const char **  argv 
)

Definition at line 681 of file rz-bin.c.

681  {
682  RzBin *bin = NULL;
683  const char *name = NULL;
684  const char *file = NULL;
685  const char *output = NULL;
686  int out_mode = RZ_MODE_PRINT;
687  ut64 laddr = UT64_MAX;
688  ut64 baddr = UT64_MAX;
689  const char *do_demangle = NULL;
690  const char *query = NULL;
691  int c, bits = 0, actions = 0;
692  char *create = NULL;
694  char *tmp, *ptr, *arch_name = NULL;
695  const char *arch = NULL;
696  const char *forcebin = NULL;
697  const char *chksum = NULL;
698  const char *op = NULL;
699  RzCoreFile *fh = NULL;
700  RzCoreBinFilter filter;
701  int xtr_idx = 0; // load all files if extraction is necessary.
702  bool rawstr = false;
703  int fd = -1;
704  RzCore core = { 0 };
705  ut64 at = UT64_MAX;
706  int result = 0;
707 
708  rz_core_init(&core);
709  bin = core.bin;
710  if (!(tmp = rz_sys_getenv("RZ_BIN_NOPLUGINS"))) {
711  char *homeplugindir = rz_path_home_prefix(RZ_PLUGINS);
712  // TODO: remove after 0.4.0 is released
713  char *oldhomeplugindir = rz_path_home_prefix(RZ_HOME_OLD_PLUGINS);
714  char *plugindir = rz_path_system(RZ_PLUGINS);
715  RzLib *l = rz_lib_new(NULL, NULL);
716  rz_lib_add_handler(l, RZ_LIB_TYPE_DEMANGLER, "demangler plugins",
717  &__lib_demangler_cb, &__lib_demangler_dt, bin->demangler);
718  rz_lib_add_handler(l, RZ_LIB_TYPE_BIN, "bin plugins",
720  rz_lib_add_handler(l, RZ_LIB_TYPE_BIN_XTR, "bin xtr plugins",
722  /* load plugins everywhere */
723  char *path = rz_sys_getenv(RZ_LIB_ENV);
724  if (!RZ_STR_ISEMPTY(path)) {
725  rz_lib_opendir(l, path, false);
726  }
727  rz_lib_opendir(l, homeplugindir, false);
728  rz_lib_opendir(l, oldhomeplugindir, false);
729  rz_lib_opendir(l, plugindir, false);
730  free(homeplugindir);
731  free(oldhomeplugindir);
732  free(plugindir);
733  free(path);
734  rz_lib_free(l);
735  }
736  free(tmp);
737 
738  if ((tmp = rz_sys_getenv("RZ_CONFIG"))) {
739  Sdb *config_sdb = sdb_new(NULL, tmp, 0);
740  if (config_sdb) {
741  rz_config_unserialize(core.config, config_sdb, NULL);
742  sdb_free(config_sdb);
743  } else {
744  eprintf("Cannot open file specified in RZ_CONFIG\n");
745  }
746  free(tmp);
747  }
748  if ((tmp = rz_sys_getenv("RZ_BIN_LANG"))) {
749  rz_config_set(core.config, "bin.lang", tmp);
750  free(tmp);
751  }
752  if ((tmp = rz_sys_getenv("RZ_BIN_DEMANGLE"))) {
753  rz_config_set(core.config, "bin.demangle", tmp);
754  free(tmp);
755  }
756  if ((tmp = rz_sys_getenv("RZ_BIN_MAXSTRBUF"))) {
757  rz_config_set(core.config, "bin.maxstrbuf", tmp);
758  free(tmp);
759  }
760  if ((tmp = rz_sys_getenv("RZ_BIN_STRFILTER"))) {
761  rz_config_set(core.config, "bin.str.filter", tmp);
762  free(tmp);
763  }
764  if ((tmp = rz_sys_getenv("RZ_BIN_STRPURGE"))) {
765  rz_config_set(core.config, "bin.str.purge", tmp);
766  free(tmp);
767  }
768  if ((tmp = rz_sys_getenv("RZ_BIN_DEBASE64"))) {
769  rz_config_set(core.config, "bin.debase64", tmp);
770  free(tmp);
771  }
772  if ((tmp = rz_sys_getenv("RZ_BIN_PDBSERVER"))) {
773  rz_config_set(core.config, "pdb.server", tmp);
774  free(tmp);
775  }
776 
777 #define is_active(x) (action & (x))
778 #define set_action(x) \
779  { \
780  actions++; \
781  action |= (x); \
782  }
783 #define unset_action(x) action &= ~x
784  RzGetopt opt;
785  rz_getopt_init(&opt, argc, argv, "DjgAf:F:a:B:G:b:cC:k:K:dD:Mm:n:N:@:isSVIHeEUlRwO:o:pPqQrTvLhuxYXzZ");
786  while ((c = rz_getopt_next(&opt)) != -1) {
787  switch (c) {
788  case 'g':
806  break;
807  case 'V': set_action(RZ_BIN_REQ_VERSIONINFO); break;
808  case 'T': set_action(RZ_BIN_REQ_SIGNATURE); break;
809  case 'w': set_action(RZ_BIN_REQ_TRYCATCH); break;
810  case 'q':
811  out_mode = (out_mode & RZ_MODE_SIMPLE ? RZ_MODE_SIMPLEST : RZ_MODE_SIMPLE);
812  break;
813  case 'j': out_mode = RZ_MODE_JSON; break;
814  case 'A': set_action(RZ_BIN_REQ_LISTARCHS); break;
815  case 'a': arch = opt.arg; break;
816  case 'C':
818  create = strdup(opt.arg);
819  break;
820  case 'u': bin->filter = 0; break;
821  case 'k': query = opt.arg; break;
822  case 'K': chksum = opt.arg; break;
823  case 'c':
827  } else {
829  }
830  break;
831  case 'f': arch_name = strdup(opt.arg); break;
832  case 'F': forcebin = opt.arg; break;
833  case 'b': bits = rz_num_math(NULL, opt.arg); break;
834  case 'm':
835  at = rz_num_math(NULL, opt.arg);
837  break;
838  case 'i':
840  break;
841  case 's':
843  break;
844  case 'S':
848  } else if (is_active(RZ_BIN_REQ_SECTIONS)) {
851  } else {
853  }
854  break;
855  case 'z':
857  rawstr = true;
858  } else {
860  }
861  break;
862  case 'Z': set_action(RZ_BIN_REQ_SIZE); break;
863  case 'I': set_action(RZ_BIN_REQ_INFO); break;
864  case 'H':
866  break;
867  case 'd': set_action(RZ_BIN_REQ_DWARF); break;
868  case 'P':
869  if (is_active(RZ_BIN_REQ_PDB)) {
872  } else {
874  }
875  break;
876  case 'D':
877  if (argv[opt.ind] && argv[opt.ind + 1] &&
878  (!argv[opt.ind + 1][0] || !strcmp(argv[opt.ind + 1], "all"))) {
879  rz_config_set(core.config, "bin.lang", argv[opt.ind]);
880  rz_config_set(core.config, "bin.demangle", "true");
881  opt.ind += 2;
882  } else {
883  do_demangle = argv[opt.ind];
884  }
885  break;
886  case 'e':
887  if (action & RZ_BIN_REQ_ENTRIES) {
890  } else {
892  }
893  break;
894  case 'E': set_action(RZ_BIN_REQ_EXPORTS); break;
895  case 'U': set_action(RZ_BIN_REQ_RESOURCES); break;
896  case 'Q': set_action(RZ_BIN_REQ_DLOPEN); break;
897  case 'M': set_action(RZ_BIN_REQ_MAIN); break;
898  case 'l': set_action(RZ_BIN_REQ_LIBS); break;
899  case 'R': set_action(RZ_BIN_REQ_RELOCS); break;
900  case 'Y': set_action(RZ_BIN_REQ_BASEFIND); break;
901  case 'x': set_action(RZ_BIN_REQ_EXTRACT); break;
902  case 'O':
903  op = opt.arg;
905  if (*op == 'c') {
906  rz_sys_setenv("RZ_BIN_CODESIGN_VERBOSE", "1");
907  }
908  if (isBinopHelp(op)) {
909  printf("Usage: iO [expression]:\n"
910  " d/s/1024 dump symbols\n"
911  " d/S/.text dump section\n"
912  " c show Codesign data\n"
913  " C show LDID entitlements\n");
914  rz_core_fini(&core);
915  return 0;
916  }
917  if (opt.ind == argc) {
918  eprintf("Missing filename\n");
919  rz_core_fini(&core);
920  return 1;
921  }
922  break;
923  case 'o': output = opt.arg; break;
924  case 'p': core.io->va = false; break;
925  case 'r': out_mode = RZ_MODE_RIZINCMD; break;
926  case 'v':
927  rz_core_fini(&core);
928  return rz_main_version_print("rz-bin");
929  case 'L':
931  break;
932  case 'G':
933  laddr = rz_num_math(NULL, opt.arg);
934  if (laddr == UT64_MAX) {
935  core.io->va = false;
936  }
937  break;
938  case 'B':
939  baddr = rz_num_math(NULL, opt.arg);
940  break;
941  case '@':
942  at = rz_num_math(NULL, opt.arg);
943  if (at == 0LL && *opt.arg != '0') {
944  at = UT64_MAX;
945  }
946  break;
947  case 'n':
948  name = opt.arg;
949  break;
950  case 'N':
951  tmp = strchr(opt.arg, ':');
952  rz_config_set(core.config, "bin.minstr", opt.arg);
953  if (tmp) {
954  rz_config_set(core.config, "bin.maxstr", tmp + 1);
955  }
956  break;
957  case 'h':
958  rz_core_fini(&core);
959  return rabin_show_help(1);
960  default:
962  break;
963  }
964  }
965 
967  const char *plugin_name = NULL;
968  if (opt.ind < argc) {
969  plugin_name = argv[opt.ind];
970  }
971  PJ *pj = pj_new();
972  if (!pj) {
973  rz_core_fini(&core);
974  return 1;
975  }
976  __listPlugins(bin, plugin_name, pj, out_mode);
977  if (out_mode == RZ_MODE_JSON) {
979  rz_cons_flush();
980  }
981  rz_core_fini(&core);
982  return 0;
983  }
984 
985  if (do_demangle) {
986  int ret_num = 1;
987  char *res = NULL;
988  const RzDemanglerPlugin *plugin = NULL;
989  if ((argc - opt.ind) < 2) {
990  rz_core_fini(&core);
991  return rabin_show_help(0);
992  }
993  file = argv[opt.ind + 1];
994  plugin = rz_demangler_plugin_get(bin->demangler, do_demangle);
995  if (!plugin) {
996  printf("Language '%s' is unsupported\nList of supported languages:\n", do_demangle);
998  rz_core_fini(&core);
999  return 1;
1000  }
1001  if (!strcmp(file, "-")) {
1002  for (;;) {
1003  file = stdin_gets(false);
1004  if (!file || !*file) {
1005  break;
1006  }
1007  res = rz_demangler_plugin_demangle(plugin, file);
1008  if (RZ_STR_ISNOTEMPTY(res)) {
1009  printf("%s\n", res);
1010  ret_num = 0;
1011  } else if (file && *file) {
1012  printf("%s\n", file);
1013  }
1014  RZ_FREE(res);
1015  RZ_FREE(file);
1016  }
1017  stdin_gets(true);
1018  } else {
1019  res = rz_demangler_plugin_demangle(plugin, file);
1020  if (RZ_STR_ISNOTEMPTY(res)) {
1021  printf("%s\n", res);
1022  ret_num = 0;
1023  } else {
1024  printf("%s\n", file);
1025  }
1026  }
1027  free(res);
1028  rz_core_fini(&core);
1029  return ret_num;
1030  }
1031  file = argv[opt.ind];
1032 
1033  if (file && !*file) {
1034  eprintf("Cannot open empty path\n");
1035  rz_core_fini(&core);
1036  return 1;
1037  }
1038 
1039  if (!query) {
1040  if (action & RZ_BIN_REQ_HELP || action == RZ_BIN_REQ_UNK || !file) {
1041  rz_core_fini(&core);
1042  return rabin_show_help(0);
1043  }
1044  }
1045  if (arch) {
1046  ptr = strchr(arch, '_');
1047  if (ptr) {
1048  *ptr = '\0';
1049  bits = rz_num_math(NULL, ptr + 1);
1050  }
1051  }
1052  if (action & RZ_BIN_REQ_CREATE) {
1053  // TODO: move in a function outside
1054  RzBuffer *b;
1055  int datalen, codelen;
1056  ut8 *data = NULL, *code = NULL;
1057  char *p2, *p = strchr(create, ':');
1058  if (!p) {
1059  eprintf("Invalid format for -C flag. Use 'format:codehexpair:datahexpair'\n");
1060  rz_core_fini(&core);
1061  return 1;
1062  }
1063  *p++ = 0;
1064  p2 = strchr(p, ':');
1065  if (p2) {
1066  // has data
1067  *p2++ = 0;
1068  data = malloc(strlen(p2) + 1);
1069  datalen = rz_hex_str2bin(p2, data);
1070  if (datalen < 0) {
1071  datalen = -datalen;
1072  }
1073  } else {
1074  data = NULL;
1075  datalen = 0;
1076  }
1077  code = malloc(strlen(p) + 1);
1078  if (!code) {
1079  rz_core_fini(&core);
1080  return 1;
1081  }
1082  codelen = rz_hex_str2bin(p, code);
1083  RzBinArchOptions opts;
1085  b = rz_bin_create(bin, create, code, codelen, data, datalen, &opts);
1086  if (b) {
1087  ut64 tmpsz;
1088  const ut8 *tmp = rz_buf_data(b, &tmpsz);
1089  if (rz_file_dump(file, tmp, tmpsz, 0)) {
1090  eprintf("Dumped %" PFMT64d " bytes in '%s'\n",
1091  tmpsz, file);
1092  (void)rz_file_chmod(file, "+x", 0);
1093  } else {
1094  eprintf("Error dumping into a.out\n");
1095  }
1096  rz_buf_free(b);
1097  } else {
1098  eprintf("Cannot create binary for this format '%s'.\n", create);
1099  }
1100  rz_core_fini(&core);
1101  return 0;
1102  }
1103  if (rawstr) {
1105  }
1106 
1107  if (!file) {
1108  eprintf("Missing file.\n");
1109  rz_core_fini(&core);
1110  return 1;
1111  }
1112 
1113  if (file && *file && action & RZ_BIN_REQ_DLOPEN) {
1114 #if __UNIX__
1115  int child = rz_sys_fork();
1116  if (child == -1) {
1117  rz_core_fini(&core);
1118  return 1;
1119  }
1120  if (child == 0) {
1121  return waitpid(child, NULL, 0);
1122  }
1123 #endif
1124  void *addr = rz_lib_dl_open(file);
1125  if (addr) {
1126  eprintf("%s is loaded at 0x%" PFMT64x "\n", file, (ut64)(size_t)(addr));
1128  rz_core_fini(&core);
1129  return 0;
1130  }
1131  eprintf("Cannot open the '%s' library\n", file);
1132  rz_core_fini(&core);
1133  return 0;
1134  }
1135 
1136  if (RZ_STR_ISNOTEMPTY(file)) {
1137  if ((fh = rz_core_file_open(&core, file, RZ_PERM_R, 0))) {
1138  fd = rz_io_fd_get_current(core.io);
1139  if (fd == -1) {
1140  eprintf("rz_core: Cannot open file '%s'\n", file);
1141  result = 1;
1142  goto err;
1143  }
1144  } else {
1145  eprintf("rz_core: Cannot open file '%s'\n", file);
1146  rz_core_fini(&core);
1147  return 1;
1148  }
1149  }
1150  bin->minstrlen = rz_config_get_i(core.config, "bin.minstr");
1151  bin->maxstrbuf = rz_config_get_i(core.config, "bin.maxstrbuf");
1152 
1153  rz_bin_force_plugin(bin, forcebin);
1155 
1156  RzBinOptions bo;
1157  rz_bin_options_init(&bo, fd, baddr, laddr, false);
1158  bo.obj_opts.elf_load_sections = rz_config_get_b(core.config, "elf.load.sections");
1159  bo.obj_opts.elf_checks_sections = rz_config_get_b(core.config, "elf.checks.sections");
1160  bo.obj_opts.elf_checks_segments = rz_config_get_b(core.config, "elf.checks.segments");
1161  bo.obj_opts.big_endian = rz_config_get_b(core.config, "cfg.bigendian");
1162  bo.xtr_idx = xtr_idx;
1163 
1164  RzBinFile *bf = rz_bin_open(bin, file, &bo);
1165  if (!bf) {
1166  eprintf("rz-bin: Cannot open file\n");
1167  result = 1;
1168  goto err;
1169  }
1170  /* required to automatically select a sub-bin when not specified */
1171  (void)rz_core_bin_update_arch_bits(&core);
1172 
1173  if (baddr != UT64_MAX) {
1175  }
1176  if (rawstr) {
1177  PJ *pj = NULL;
1178  if (out_mode == RZ_MODE_JSON) {
1179  pj = pj_new();
1180  if (!pj) {
1181  eprintf("rz-bin: Cannot allocate buffer for json array\n");
1182  result = 1;
1183  goto err;
1184  }
1185  pj_a(pj);
1186  }
1187  RzList *list = rz_bin_file_strings(bf, bin->minstrlen, true);
1188  RzListIter *it;
1189  RzBinString *string;
1190  rz_list_foreach (list, it, string) {
1191  print_string(bf, string, pj, out_mode);
1192  }
1193  rz_list_free(list);
1194  if (pj) {
1195  pj_end(pj);
1196  printf("%s", pj_string(pj));
1197  pj_free(pj);
1198  }
1199  }
1200  if (query) {
1201  if (out_mode) {
1203  rz_cons_flush();
1204  } else {
1205  if (!strcmp(query, "-")) {
1206  __sdb_prompt(bin->cur->sdb);
1207  } else {
1208  sdb_query(bin->cur->sdb, query);
1209  }
1210  }
1211  result = 0;
1212  goto err;
1213  }
1214 #define ismodejson (out_mode == RZ_MODE_JSON && actions > 0)
1215 #define run_action(n, x, y) \
1216  if (action & (x)) { \
1217  RzCmdStateOutput *st = add_header(&state, mode, n); \
1218  y(&core, st); \
1219  add_footer(&state, st); \
1220  }
1221 
1222  core.bin = bin;
1223  bin->cb_printf = rz_cons_printf;
1224  filter.offset = at;
1225  filter.name = name;
1226  RzList *chksum_list = NULL;
1227  if (RZ_STR_ISNOTEMPTY(chksum)) {
1228  chksum_list = rz_str_split_duplist_n(chksum, ",", 0, true);
1229  if (!chksum_list) {
1230  result = 1;
1231  goto err;
1232  }
1233  }
1234  rz_cons_new()->context->is_interactive = false;
1235 
1237  RzOutputMode mode = rad2outputmode(out_mode);
1239  result = 1;
1240  goto chksum_err;
1241  }
1242  start_state(&state);
1243 
1244  // List fatmach0 sub-binaries, etc
1245  if (action & RZ_BIN_REQ_LISTARCHS || ((arch || bits || arch_name) && !rz_bin_select(bin, arch, bits, arch_name))) {
1248  add_footer(&state, st);
1249  free(arch_name);
1250  }
1251  if (action & RZ_BIN_REQ_PDB_DWNLD) {
1252  SPDBOptions pdbopts;
1253  pdbopts.symbol_server = (char *)rz_config_get(core.config, "pdb.server");
1254  pdbopts.extract = rz_config_get_i(core.config, "pdb.extract");
1255 
1256  if ((tmp = rz_sys_getenv("RZ_BIN_SYMSTORE"))) {
1257  rz_config_set(core.config, "pdb.symstore", tmp);
1258  RZ_FREE(tmp);
1259  }
1260  pdbopts.symbol_store_path = (char *)rz_config_get(core.config, "pdb.symstore");
1261  result = rz_bin_pdb_download(core.bin, state.mode == RZ_OUTPUT_MODE_JSON ? state.d.pj : NULL, ismodejson, &pdbopts);
1262  }
1263 
1264  if ((tmp = rz_sys_getenv("RZ_BIN_PREFIX"))) {
1265  rz_config_set(core.config, "bin.prefix", tmp);
1266  free(tmp);
1267  }
1268 
1270  rz_core_bin_print(&core, bf, mask, &filter, &state, chksum_list);
1271 
1273  if (action & RZ_BIN_REQ_SRCLINE) {
1274  rabin_show_srcline(bin, at);
1275  }
1276  if (action & RZ_BIN_REQ_EXTRACT) {
1277  if (bf->xtr_data) {
1278  rabin_extract(bin, (!arch && !arch_name && !bits));
1279  } else {
1280  eprintf(
1281  "Cannot extract bins from '%s'. No supported "
1282  "plugins found!\n",
1283  bin->file);
1284  }
1285  }
1286  if (op && action & RZ_BIN_REQ_OPERATION) {
1287  rabin_do_operation(bin, op, out_mode, output, file);
1288  }
1289  end_state(&state);
1291 
1292  rz_cons_flush();
1293 
1294 chksum_err:
1295  rz_list_free(chksum_list);
1296 err:
1298  rz_core_fini(&core);
1299 
1300  return result;
1301 }
#define mask()
static bool err
Definition: armass.c:435
RZ_API RZ_OWN RzList * rz_bin_file_strings(RZ_NONNULL RzBinFile *bf, size_t min_length, bool raw_strings)
Generates a RzList struct containing RzBinString from a given RzBinFile.
Definition: bfile_string.c:325
RZ_API void rz_bin_load_filter(RzBin *bin, ut64 rules)
Definition: bin.c:930
RZ_API void rz_bin_arch_options_init(RzBinArchOptions *opt, const char *arch, int bits)
Definition: bin.c:84
RZ_API void rz_bin_force_plugin(RzBin *bin, const char *name)
Definition: bin.c:906
RZ_API RzBuffer * rz_bin_create(RzBin *bin, const char *p, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt)
Definition: bin.c:862
RZ_API void rz_bin_options_init(RzBinOptions *opt, int fd, ut64 baseaddr, ut64 loadaddr, bool patch_relocs)
Definition: bin.c:75
RZ_API RzBinFile * rz_bin_open(RzBin *bin, const char *file, RzBinOptions *opt)
Definition: bin.c:200
RZ_API bool rz_bin_select(RzBin *bin, const char *arch, int bits, const char *name)
Definition: bin.c:813
RZ_API void rz_bin_set_baddr(RzBin *bin, ut64 baddr)
Definition: bin.c:549
static ut64 baddr(RzBinFile *bf)
Definition: bin_any.c:58
static RzBuffer * create(RzBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt)
Definition: bin_cgc.c:16
FILE * fh
Definition: cabinfo.c:52
RZ_API void rz_core_bin_export_info(RzCore *core, int mode)
Definition: cbin.c:108
RZ_API bool rz_core_bin_print(RzCore *core, RZ_NONNULL RzBinFile *bf, ut32 mask, RzCoreBinFilter *filter, RzCmdStateOutput *state, RzList *hashes)
Print (to RzCons or inside RzCmdStateOutput) the binary information specified in mask.
Definition: cbin.c:386
RZ_API int rz_core_bin_update_arch_bits(RzCore *r)
Definition: cbin.c:4503
RZ_API bool rz_core_bin_archs_print(RZ_NONNULL RzBin *bin, RZ_NONNULL RzCmdStateOutput *state)
Definition: cbin.c:5217
RZ_API void rz_core_file_close(RzCoreFile *fh)
Definition: cfile.c:1299
RZ_API RZ_BORROW RzCoreFile * rz_core_file_open(RZ_NONNULL RzCore *r, RZ_NONNULL const char *file, int flags, ut64 loadaddr)
Tries to open the file as is, otherwise tries as is a compilation of files.
Definition: cfile.c:1182
RZ_API ut64 rz_config_get_i(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:119
RZ_API bool rz_config_get_b(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:142
RZ_API bool rz_config_unserialize(RZ_NONNULL RzConfig *config, RZ_NONNULL Sdb *db, RZ_NULLABLE char **err)
Definition: config.c:564
RZ_API RzConfigNode * rz_config_set(RzConfig *cfg, RZ_NONNULL const char *name, const char *value)
Definition: config.c:267
RZ_API RZ_BORROW const char * rz_config_get(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:75
RZ_API RzCons * rz_cons_new(void)
Definition: cons.c:589
void * p
Definition: libc.cpp:67
RZ_API void rz_core_fini(RzCore *c)
Definition: core.c:2623
RZ_API bool rz_core_init(RzCore *core)
Definition: core.c:2381
RZ_API void rz_demangler_plugin_iterate(RZ_NONNULL RzDemangler *dem, RZ_NONNULL RzDemanglerIter iter, RZ_NULLABLE void *data)
Iterates over the plugin list.
Definition: demangler.c:127
RZ_API RZ_BORROW const RzDemanglerPlugin * rz_demangler_plugin_get(RZ_NONNULL RzDemangler *dem, RZ_NONNULL const char *language)
Returns a demangler plugin pointer based on the language that is found.
Definition: demangler.c:168
static RzOutputMode rad2outputmode(int rad)
Definition: rz-bin.c:63
static void print_string(RzBinFile *bf, RzBinString *string, PJ *pj, int mode)
Definition: rz-bin.c:638
#define run_action(n, x, y)
#define unset_action(x)
static int __lib_bin_cb(RzLibPlugin *pl, void *user, void *data)
Definition: rz-bin.c:585
static int rabin_extract(RzBin *bin, int all)
Definition: rz-bin.c:353
static void end_state(RzCmdStateOutput *state)
Definition: rz-bin.c:19
static void classes_as_source_print(RzCore *core, RzCmdStateOutput *state)
Definition: rz-bin.c:56
static int __lib_bin_xtr_dt(RzLibPlugin *pl, void *p, void *u)
Definition: rz-bin.c:605
static ut32 actions2mask(ut64 action)
Definition: rz-bin.c:79
static int __lib_bin_dt(RzLibPlugin *pl, void *p, void *u)
Definition: rz-bin.c:592
static int __lib_demangler_cb(RzLibPlugin *pl, void *user, void *data)
Definition: rz-bin.c:576
static void __sdb_prompt(Sdb *sdb)
Definition: rz-bin.c:260
static bool add_footer(RzCmdStateOutput *main_state, RzCmdStateOutput *state)
Definition: rz-bin.c:29
static int rabin_show_help(int v)
Definition: rz-bin.c:153
#define is_active(x)
static bool print_demangler_info(const RzDemanglerPlugin *plugin, void *user)
Definition: rz-bin.c:632
static void start_state(RzCmdStateOutput *state)
Definition: rz-bin.c:13
static int rabin_show_srcline(RzBin *bin, ut64 at)
Definition: rz-bin.c:565
static int __lib_demangler_dt(RzLibPlugin *pl, void *p, void *u)
Definition: rz-bin.c:581
#define ismodejson
static int __lib_bin_xtr_cb(RzLibPlugin *pl, void *user, void *data)
Definition: rz-bin.c:597
static int rabin_do_operation(RzBin *bin, const char *op, int rad, const char *output, const char *file)
Definition: rz-bin.c:471
static bool isBinopHelp(const char *op)
Definition: rz-bin.c:268
static void __listPlugins(RzBin *bin, const char *plugin_name, PJ *pj, int rad)
Definition: rz-bin.c:609
#define set_action(x)
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
const char * name
Definition: op.c:541
static bool filter(RzParse *p, ut64 addr, RzFlag *f, RzAnalysisHint *hint, char *data, char *str, int len, bool big_endian)
Definition: filter.c:185
RZ_API int rz_bin_pdb_download(RZ_NONNULL RzBin *bin, RZ_NULLABLE PJ *pj, int isradjson, RZ_NONNULL SPDBOptions *options)
Download PDB file for currently opened RzBin file.
#define RZ_BIN_REQ_HELP
Definition: rz_bin.h:51
#define RZ_BIN_REQ_CLASSES_SOURCES
Definition: rz_bin.h:79
#define RZ_BIN_REQ_EXTRACT
Definition: rz_bin.h:57
#define RZ_BIN_REQ_PDB_DWNLD
Definition: rz_bin.h:65
#define RZ_BIN_REQ_CREATE
Definition: rz_bin.h:60
#define RZ_BIN_REQ_LISTPLUGINS
Definition: rz_bin.h:71
#define RZ_BIN_REQ_DLOPEN
Definition: rz_bin.h:66
#define RZ_BIN_REQ_LISTARCHS
Definition: rz_bin.h:59
#define RZ_BIN_REQ_SRCLINE
Definition: rz_bin.h:55
#define RZ_BIN_REQ_OPERATION
Definition: rz_bin.h:50
#define RZ_BIN_REQ_TRYCATCH
Definition: rz_bin.h:77
#define RZ_BIN_REQ_UNK
Definition: rz_bin.h:44
RZ_DEPRECATE RZ_API RZ_BORROW ut8 * rz_buf_data(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut64 *size)
Return a borrowed array of bytes representing the buffer data.
Definition: buf.c:1287
#define rz_demangler_plugin_demangle(x, y)
Definition: rz_demangler.h:29
bool(* RzDemanglerIter)(const RzDemanglerPlugin *plugin, void *data)
Definition: rz_demangler.h:27
RZ_API bool rz_file_chmod(const char *file, const char *mod, int recursive)
Definition: chmod.c:19
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_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_io_fd_get_current(RzIO *io)
Definition: io_fd.c:135
RZ_API void rz_lib_free(RzLib *lib)
Definition: lib.c:189
#define RZ_LIB_ENV
Definition: rz_lib.h:23
RZ_API RzLib * rz_lib_new(const char *symname, const char *symnamefunc)
Definition: lib.c:176
RZ_API void * rz_lib_dl_open(const char *libname)
Definition: lib.c:54
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
@ RZ_LIB_TYPE_BIN_XTR
Definition: rz_lib.h:76
@ RZ_LIB_TYPE_DEMANGLER
Definition: rz_lib.h:85
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 int rz_lib_dl_close(void *handler)
Definition: lib.c:104
RZ_API int rz_main_version_print(const char *program)
Definition: main.c:49
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
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API RzList * rz_str_split_duplist_n(const char *str, const char *c, int n, bool trim)
Split the string str according to the substring c and returns a RzList with the result.
Definition: str.c:3485
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
RZ_API int rz_sys_setenv(const char *key, const char *value)
Set an environment variable in the calling process.
Definition: sys.c:405
RZ_API int rz_sys_fork(void)
Definition: sys.c:1679
#define RZ_PERM_R
Definition: rz_types.h:93
RzOutputMode
Enum to describe the way data are printed.
Definition: rz_types.h:38
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_PLUGINS
Definition: rz_userconf.h:72
#define RZ_HOME_OLD_PLUGINS
Definition: rz_userconf.h:100
RZ_API Sdb * sdb_new(const char *path, const char *name, int lock)
Definition: sdb.c:47
RZ_API bool sdb_free(Sdb *s)
Definition: sdb.c:206
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
const char * symbol_server
const char * symbol_store_path
Definition: inftree9.h:24
Definition: z80asm.h:102
Definition: rz_pj.h:12
bool is_interactive
Definition: rz_cons.h:485
RzConsContext * context
Definition: rz_cons.h:502
RzIO * io
Definition: rz_core.h:313
RzConfig * config
Definition: rz_core.h:300
const char * arg
Definition: rz_getopt.h:15
int va
Definition: rz_io.h:63
Definition: sdb.h:63
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int addr
Definition: z80asm.c:58

References __lib_bin_cb(), __lib_bin_dt(), __lib_bin_xtr_cb(), __lib_bin_xtr_dt(), __lib_demangler_cb(), __lib_demangler_dt(), __listPlugins(), __sdb_prompt(), test-lz4-speed::action, actions2mask(), add_footer(), add_header(), addr, arch, rz_getopt_t::arg, argv, b, baddr(), rz_core_t::bin, bits(), c, classes_as_source_print(), rz_core_t::config, rz_cons_t::context, create(), end_state(), eprintf, err, SPDBOptions::extract, fd, fh, filter(), free(), rz_getopt_t::ind, rz_core_t::io, is_active, rz_cons_context_t::is_interactive, isBinopHelp(), ismodejson, list(), malloc(), mask, name, NULL, output, p, path, PFMT64d, PFMT64x, pj_a(), pj_end(), pj_free(), pj_new(), pj_string(), print_demangler_info(), print_string(), printf(), rabin_do_operation(), rabin_extract(), rabin_show_help(), rabin_show_srcline(), rad2outputmode(), run_action, rz_bin_arch_options_init(), rz_bin_create(), rz_bin_file_strings(), rz_bin_force_plugin(), rz_bin_load_filter(), rz_bin_open(), rz_bin_options_init(), rz_bin_pdb_download(), RZ_BIN_REQ_BASEFIND, RZ_BIN_REQ_CLASSES, RZ_BIN_REQ_CLASSES_SOURCES, RZ_BIN_REQ_CREATE, RZ_BIN_REQ_DLOPEN, RZ_BIN_REQ_DWARF, RZ_BIN_REQ_ENTRIES, RZ_BIN_REQ_EXPORTS, RZ_BIN_REQ_EXTRACT, RZ_BIN_REQ_FIELDS, RZ_BIN_REQ_HELP, RZ_BIN_REQ_IMPORTS, RZ_BIN_REQ_INFO, RZ_BIN_REQ_INITFINI, RZ_BIN_REQ_LIBS, RZ_BIN_REQ_LISTARCHS, RZ_BIN_REQ_LISTPLUGINS, RZ_BIN_REQ_MAIN, RZ_BIN_REQ_OPERATION, RZ_BIN_REQ_PDB, RZ_BIN_REQ_PDB_DWNLD, RZ_BIN_REQ_RELOCS, RZ_BIN_REQ_RESOURCES, RZ_BIN_REQ_SECTIONS, RZ_BIN_REQ_SECTIONS_MAPPING, RZ_BIN_REQ_SEGMENTS, RZ_BIN_REQ_SIGNATURE, RZ_BIN_REQ_SIZE, RZ_BIN_REQ_SRCLINE, RZ_BIN_REQ_STRINGS, RZ_BIN_REQ_SYMBOLS, RZ_BIN_REQ_TRYCATCH, RZ_BIN_REQ_UNK, RZ_BIN_REQ_VERSIONINFO, rz_bin_select(), rz_bin_set_baddr(), rz_buf_data(), rz_buf_free(), rz_cmd_state_output_fini(), rz_cmd_state_output_init(), rz_config_get(), rz_config_get_b(), rz_config_get_i(), rz_config_set(), rz_config_unserialize(), rz_cons_flush(), rz_cons_new(), rz_cons_printf(), rz_cons_println(), rz_core_bin_archs_print(), rz_core_bin_export_info(), rz_core_bin_print(), rz_core_bin_update_arch_bits(), rz_core_file_close(), rz_core_file_open(), rz_core_fini(), rz_core_init(), rz_demangler_plugin_demangle, rz_demangler_plugin_get(), rz_demangler_plugin_iterate(), rz_file_chmod(), rz_file_dump(), RZ_FREE, rz_getopt_init(), rz_getopt_next(), rz_hex_str2bin(), RZ_HOME_OLD_PLUGINS, rz_io_fd_get_current(), rz_lib_add_handler(), rz_lib_dl_close(), rz_lib_dl_open(), RZ_LIB_ENV, rz_lib_free(), rz_lib_new(), rz_lib_opendir(), RZ_LIB_TYPE_BIN, RZ_LIB_TYPE_BIN_XTR, RZ_LIB_TYPE_DEMANGLER, rz_list_free(), rz_main_version_print(), RZ_MODE_JSON, RZ_MODE_PRINT, RZ_MODE_RIZINCMD, RZ_MODE_SIMPLE, RZ_MODE_SIMPLEST, rz_num_math(), RZ_OUTPUT_MODE_JSON, RZ_OUTPUT_MODE_STANDARD, RZ_OUTPUT_MODE_TABLE, rz_path_home_prefix(), rz_path_system(), RZ_PERM_R, RZ_PLUGINS, RZ_STR_ISEMPTY, RZ_STR_ISNOTEMPTY, rz_str_split_duplist_n(), rz_sys_fork(), rz_sys_getenv(), rz_sys_setenv(), sdb_free(), sdb_new(), sdb_query(), set_action, start_state(), stdin_gets(), strdup(), SPDBOptions::symbol_server, SPDBOptions::symbol_store_path, autogen_x86imm::tmp, unset_action, ut64(), UT64_MAX, rz_io_t::va, and rz_bin_file_t::xtr_data.

Referenced by MAIN_NAME(), and rz_main_rizin().

◆ start_state()

static void start_state ( RzCmdStateOutput state)
static

Definition at line 13 of file rz-bin.c.

13  {
14  if (state->mode == RZ_OUTPUT_MODE_JSON) {
15  pj_o(state->d.pj);
16  }
17 }

References pj_o(), and RZ_OUTPUT_MODE_JSON.

Referenced by parser__reductions_after_sequence(), and rz_main_rz_bin().

◆ stdin_gets()

static char* stdin_gets ( bool  liberate)
static

Definition at line 235 of file rz-bin.c.

235  {
236  static char *stdin_buf = NULL;
237 #define STDIN_BUF_SIZE 96096
238  if (liberate) {
239  free(stdin_buf);
240  stdin_buf = NULL;
241  return NULL;
242  }
243  if (!stdin_buf) {
244  /* XXX: never freed. leaks! */
245  stdin_buf = malloc(STDIN_BUF_SIZE);
246  if (!stdin_buf) {
247  return NULL;
248  }
249  }
250  memset(stdin_buf, 0, STDIN_BUF_SIZE);
251  if (!fgets(stdin_buf, STDIN_BUF_SIZE, stdin)) {
252  return NULL;
253  }
254  if (feof(stdin)) {
255  return NULL;
256  }
257  return strdup(stdin_buf);
258 }
return memset(p, 0, total)
#define STDIN_BUF_SIZE

References free(), malloc(), memset(), NULL, STDIN_BUF_SIZE, and strdup().

Referenced by __sdb_prompt(), and rz_main_rz_bin().