Rizin
unix-like reverse engineering framework and cli tools
cfile.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2021 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_core.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "core_private.h"
8 #include "core_private_base.h"
9 
10 static bool core_file_do_load_for_debug(RzCore *r, ut64 loadaddr, const char *filenameuri);
11 static bool core_file_do_load_for_io_plugin(RzCore *r, ut64 baseaddr, ut64 loadaddr);
12 
13 static RzCoreFile *core_file_new(RzCore *core, int fd) {
15  if (!r) {
16  return NULL;
17  }
18  r->core = core;
19  r->fd = fd;
20  rz_pvector_init(&r->binfiles, NULL);
21  rz_pvector_init(&r->extra_files, NULL);
22  rz_pvector_init(&r->maps, NULL);
23  return r;
24 }
25 
27  if (!cf) {
28  return;
29  }
32  rz_pvector_fini(&cf->maps);
33  free(cf);
34 }
35 
36 static bool __isMips(RzAsm *a) {
37  return a && a->cur && a->cur->arch && strstr(a->cur->arch, "mips");
38 }
39 
40 static void loadGP(RzCore *core) {
41  if (__isMips(core->rasm)) {
42  ut64 gp = rz_num_math(core->num, "loc._gp");
43  if (!gp || gp == UT64_MAX) {
44  rz_config_set(core->config, "analysis.roregs", "zero");
45  ut64 addr = rz_num_math(core->num, "entry");
46  rz_core_seek_opt(core, addr, true, false);
47  rz_core_debug_step_one(core, 10);
48  rz_config_set(core->config, "analysis.roregs", "zero,gp");
49  gp = rz_reg_getv(core->analysis->reg, "gp");
50  }
51  // RZ_LOG_DEBUG("[mips] gp: 0x%08"PFMT64x"\n", gp);
52  rz_config_set_i(core->config, "analysis.gp", gp);
53  }
54 }
55 
58  RzListIter *it;
59  RzBinSection *sec;
60  RzList *old_sections = rz_list_new();
61 
62  // Return an empty list
63  if (!sections) {
64  RZ_LOG_WARN("No sections found, functions and flags won't be rebased\n");
65  return old_sections;
66  }
67 
68  old_sections->free = sections->free;
69  rz_list_foreach (sections, it, sec) {
70  RzBinSection *old_sec = RZ_NEW0(RzBinSection);
71  if (!old_sec) {
72  break;
73  }
74  *old_sec = *sec;
75  old_sec->name = strdup(sec->name);
76  old_sec->format = NULL;
77  rz_list_append(old_sections, old_sec);
78  }
79  return old_sections;
80 }
81 
87  int type;
88 };
89 
90 #define __is_inside_section(item_addr, section) \
91  (item_addr >= old_base + section->vaddr && item_addr <= old_base + section->vaddr + section->vsize)
92 
93 static bool __rebase_flags(RzFlagItem *flag, void *user) {
94  struct __rebase_struct *reb = user;
95  ut64 old_base = reb->old_base;
96  RzListIter *it;
97  RzBinSection *sec;
98  // Only rebase flags that were in the rebased sections, otherwise it will take too long
99  rz_list_foreach (reb->old_sections, it, sec) {
100  if (__is_inside_section(flag->offset, sec)) {
101  rz_flag_set(reb->core->flags, flag->name, flag->offset + reb->diff, flag->size);
102  break;
103  }
104  }
105  return true;
106 }
107 
108 static bool __rebase_xrefs_i(void *user, const ut64 k, const void *v) {
109  struct __rebase_struct *reb = (void *)user;
110  RzAnalysisXRef *xref = (RzAnalysisXRef *)v;
111  xref->from += reb->diff;
112  xref->to += reb->diff;
113  rz_analysis_xrefs_set(reb->core->analysis, xref->from, xref->to, xref->type);
114  return true;
115 }
116 
117 static bool __rebase_xrefs(void *user, const ut64 k, const void *v) {
118  HtUP *ht = (HtUP *)v;
119  ht_up_foreach(ht, __rebase_xrefs_i, user);
120  return true;
121 }
122 
124  RzListIter *it, *itit, *ititit;
125  RzAnalysisFunction *fcn;
126  ut64 new_base = core->bin->cur->o->baddr_shift;
127  RzBinSection *old_section;
128  ut64 diff = new_base - old_base;
129  if (!diff) {
130  return;
131  }
132  // FUNCTIONS
133  rz_list_foreach (core->analysis->fcns, it, fcn) {
134  rz_list_foreach (old_sections, itit, old_section) {
135  if (!__is_inside_section(fcn->addr, old_section)) {
136  continue;
137  }
140  RzAnalysisBlock *bb;
141  ut64 new_sec_addr = new_base + old_section->vaddr;
142  rz_list_foreach (fcn->bbs, ititit, bb) {
143  if (bb->addr >= new_sec_addr && bb->addr <= new_sec_addr + old_section->vsize) {
144  // Todo: Find better way to check if bb was already rebased
145  continue;
146  }
147  rz_analysis_block_relocate(bb, bb->addr + diff, bb->size);
148  if (bb->jump != UT64_MAX) {
149  bb->jump += diff;
150  }
151  if (bb->fail != UT64_MAX) {
152  bb->fail += diff;
153  }
154  }
155  break;
156  }
157  }
158 
159  // FLAGS
160  struct __rebase_struct reb = {
161  core,
162  old_sections,
163  old_base,
164  diff
165  };
167 
168  // META
170 
171  // XREFS
172  HtUP *xrefs_from = core->analysis->ht_xrefs_from;
173  HtUP *xrefs_to = core->analysis->ht_xrefs_to;
177  ht_up_foreach(xrefs_from, __rebase_xrefs, &reb);
178  ht_up_free(xrefs_from);
179  ht_up_free(xrefs_to);
180 
181  // BREAKPOINTS
182  rz_debug_bp_rebase(core->dbg, old_base, new_base);
183 }
184 
189 RZ_API bool rz_core_file_open_load(RZ_NONNULL RzCore *core, RZ_NONNULL const char *filepath, ut64 addr, int perms, bool write_mode) {
190  rz_return_val_if_fail(core && filepath, false);
191  RzCoreFile *cfile = rz_core_file_open(core, filepath, perms, addr);
192  if (!cfile) {
193  RZ_LOG_ERROR("Cannot open file '%s'\n", filepath);
194  return false;
195  }
196 
197  core->num->value = cfile->fd;
198  if (addr == 0) { // if no baddr defined, use the one provided by the file
199  addr = UT64_MAX;
200  }
201  if (!rz_core_bin_load(core, filepath, addr)) {
202  RZ_LOG_ERROR("Cannot load binary info of '%s'.\n", filepath);
203  return false;
204  }
205  if (write_mode) {
207  if (!desc || !(desc->perm & RZ_PERM_W)) {
208  RZ_LOG_WARN("Cannot make maps for %s writable.\n", filepath);
209  return false;
210  }
211  void **it;
212  rz_pvector_foreach (&cfile->maps, it) {
213  RzIOMap *map = *it;
214  map->perm |= RZ_PERM_WX;
215  }
216  }
217 
219  return true;
220 }
221 
224  RzIODesc *desc;
225  RzCoreFile *file;
226  int fd;
227 
228  if (!ofile || !(desc = rz_io_desc_get(core->io, ofile->fd)) || !desc->uri) {
229  RZ_LOG_ERROR("No file open?\n");
230  return;
231  }
232 
233  core->dbg->main_arena_resolved = false;
236  int bits = core->rasm->bits;
237  rz_config_set_i(core->config, "asm.bits", bits);
238  rz_config_set_b(core->config, "cfg.debug", true);
239  // Set referer as the original uri so we could return to it with `oo`
240  desc->referer = desc->uri;
241  desc->uri = strdup(uri);
242 
243  if ((file = rz_core_file_open(core, uri, RZ_PERM_R | RZ_PERM_W, addr))) {
244  fd = file->fd;
245  core->num->value = fd;
246  // if no baddr is defined, use the one provided by the file
247  if (addr == 0) {
249  if (desc->plugin->isdbg) {
250  addr = rz_debug_get_baddr(core->dbg, desc->name);
251  } else {
253  }
254  }
255  rz_core_bin_load(core, uri, addr);
256  } else {
257  RZ_LOG_ERROR("Cannot open file '%s'\n", uri);
259  return;
260  }
262  if (rz_config_get_i(core->config, "dbg.rebase")) {
264  }
266  rz_core_seek_to_register(core, "PC", false);
267 }
268 
271  RzIODesc *desc;
272 
273  if (!ofile || !(desc = rz_io_desc_get(core->io, ofile->fd)) || !desc->uri) {
274  RZ_LOG_ERROR("No file open?\n");
275  return;
276  }
277 
278  // Reopen the original file as read only since we can't open native debug while the
279  // file is open with write permissions
280  if (!(desc->plugin && desc->plugin->isdbg) && (desc->perm & RZ_PERM_W)) {
281  RZ_LOG_ERROR("Cannot debug file (%s) with permissions set to 0x%x.\n"
282  "Reopening the original file in read-only mode.\n",
283  desc->name, desc->perm);
284  rz_io_reopen(core->io, ofile->fd, RZ_PERM_R, 644);
285  desc = rz_io_desc_get(core->io, ofile->fd);
286  }
287 
289  char *binpath = (bf && bf->file) ? strdup(bf->file) : NULL;
290  if (!binpath) {
291  if (rz_file_exists(desc->name)) {
292  binpath = strdup(desc->name);
293  }
294  }
295  if (!binpath) {
296  /* fallback to oo */
298  return;
299  }
300  core->dbg->main_arena_resolved = false;
303  int bits = core->rasm->bits;
304  char *bin_abspath = rz_file_abspath(binpath);
305  char *escaped_path = rz_str_arg_escape(bin_abspath);
306  char *newfile = rz_str_newf("dbg://%s %s", escaped_path, args);
307  desc->uri = newfile;
308  desc->referer = NULL;
309  rz_config_set_i(core->config, "asm.bits", bits);
310  rz_config_set_b(core->config, "cfg.debug", true);
311  rz_core_file_reopen(core, newfile, 0, 2);
312  if (rz_config_get_i(core->config, "dbg.rebase")) {
314  }
316  rz_core_seek_to_register(core, "PC", false);
317  free(bin_abspath);
318  free(escaped_path);
319  free(binpath);
320 }
321 
322 RZ_API bool rz_core_file_reopen(RzCore *core, const char *args, int perm, int loadbin) {
323  int isdebug = rz_config_get_b(core->config, "cfg.debug");
324  char *path;
325  ut64 laddr = rz_config_get_i(core->config, "bin.laddr");
326  RzCoreFile *file = NULL;
329  : NULL;
330  RzIODesc *odesc = (core->io && ofile) ? rz_io_desc_get(core->io, ofile->fd) : NULL;
331  char *ofilepath = NULL, *obinfilepath = (bf && bf->file) ? strdup(bf->file) : NULL;
332  int ret = false;
333  ut64 origoff = core->offset;
334  if (odesc) {
335  if (odesc->referer) {
336  ofilepath = odesc->referer;
337  } else if (odesc->uri) {
338  ofilepath = odesc->uri;
339  }
340  }
341 
342  ut64 new_baddr = UT64_MAX;
343  if (args) {
344  new_baddr = rz_num_math(core->num, args);
345  if (new_baddr && new_baddr != UT64_MAX) {
346  rz_config_set_i(core->config, "bin.baddr", new_baddr);
347  } else {
348  new_baddr = UT64_MAX;
349  }
350  }
351  if (new_baddr == UT64_MAX) {
352  new_baddr = rz_config_get_i(core->config, "bin.baddr");
353  }
354 
355  if (!core->file) {
356  RZ_LOG_ERROR("No file opened to reopen\n");
357  free(ofilepath);
358  free(obinfilepath);
359  return false;
360  }
361  int newpid = odesc ? odesc->fd : -1;
362 
363  if (isdebug) {
364  rz_debug_kill(core->dbg, core->dbg->pid, core->dbg->tid, 9); // SIGKILL
365  do {
367  } while (!rz_debug_is_dead(core->dbg));
369  perm = 7;
370  } else {
371  if (!perm) {
372  perm = 4; // RZ_PERM_R;
373  }
374  }
375  if (!ofilepath) {
376  RZ_LOG_ERROR("Unknown file path");
377  free(obinfilepath);
378  return false;
379  }
380 
381  // HACK: move last mapped address to higher place
382  // XXX - why does this hack work?
383  // when the new memory maps are created.
384  path = strdup(ofilepath);
385  free(obinfilepath);
386  obinfilepath = strdup(ofilepath);
387 
388  // rz_str_trim (path);
389  file = rz_core_file_open(core, path, perm, laddr);
390 
391  if (isdebug) {
392  int newtid = newpid;
393  // XXX - select the right backend
394  if (core->file) {
395  newpid = rz_io_fd_get_pid(core->io, core->file->fd);
396 #if __linux__
397  core->dbg->main_pid = newpid;
398  newtid = newpid;
399 #else
400  newtid = rz_io_fd_get_tid(core->io, core->file->fd);
401 #endif
402  }
403 
404  // Reset previous pid and tid
405  core->dbg->pid = -1;
406  core->dbg->tid = -1;
408  memset(&core->dbg->reason, 0, sizeof(core->dbg->reason));
409  // Reopen and attach
410  rz_core_setup_debugger(core, "native", true);
411  rz_debug_select(core->dbg, newpid, newtid);
412  }
413 
414  if (file) {
415  bool had_rbin_info = false;
416 
417  if (ofile && bf) {
418  if (rz_bin_file_delete(core->bin, bf)) {
419  had_rbin_info = true;
420  }
421  }
424  ofile = NULL;
425  odesc = NULL;
426  // core->file = file;
427  RZ_LOG_ERROR("File %s reopened in %s mode\n", path,
428  (perm & RZ_PERM_W) ? "read-write" : "read-only");
429 
430  if (loadbin && (loadbin == 2 || had_rbin_info)) {
431  ut64 baddr;
432  if (isdebug) {
434  } else if (new_baddr != UT64_MAX) {
435  baddr = new_baddr;
436  } else {
437  baddr = rz_config_get_i(core->config, "bin.baddr");
438  }
439  ret = rz_core_bin_load(core, obinfilepath, baddr);
441  if (!ret) {
442  RZ_LOG_ERROR("Error: Failed to reload rbin for: '%s'", path);
443  }
444  origoff = rz_num_math(core->num, "entry0");
445  }
446 
447  if (core->bin->cur && core->io && rz_io_desc_get(core->io, file->fd) && !loadbin) {
448  // force here NULL because is causing uaf look this better in future XXX @alvarofe
449  core->bin->cur = NULL;
450  }
451  // close old file
452  } else if (ofile) {
453  RZ_LOG_ERROR("rz_core_file_reopen: Cannot reopen file: '%s' with perms 0x%x,"
454  " attempting to open read-only.\n",
455  path, perm);
456  // lower it down back
457  // ofile = rz_core_file_open (core, path, RZ_PERM_R, addr);
459  } else {
460  RZ_LOG_ERROR("Cannot reopen '%s'\n", path);
461  }
462  if (core->file) {
465  }
466  rz_core_seek(core, origoff, true);
467  if (isdebug) {
470  rz_core_seek_to_register(core, "PC", false);
471  } else {
472  loadGP(core);
473  }
474  // update analysis io bind
475  rz_io_bind(core->io, &(core->analysis->iob));
476  if (core->file && core->file->fd >= 0) {
478  }
480  // This is done to ensure that the file is correctly
481  // loaded into the view
482  free(obinfilepath);
483  // free (ofilepath);
484  free(path);
485  return ret;
486 }
487 
488 static bool file_resize(RzCore *core, ut64 newsize, st64 delta) {
489  int ret;
490  ut64 oldsize = (core->file) ? rz_io_fd_size(core->io, core->file->fd) : 0;
491  if (delta) {
492  newsize = oldsize + delta;
493  }
494  bool grow = (newsize > oldsize);
495  if (grow) {
496  ret = rz_io_resize(core->io, newsize);
497  if (ret < 1) {
498  RZ_LOG_ERROR("rz_io_resize: cannot resize\n");
499  return false;
500  }
501  }
502  if (delta && core->offset < newsize) {
503  rz_io_shift(core->io, core->offset, grow ? newsize : oldsize, delta);
504  }
505  if (!grow) {
506  ret = rz_io_resize(core->io, newsize);
507  if (ret < 1) {
508  RZ_LOG_ERROR("rz_io_resize: cannot resize\n");
509  return false;
510  }
511  }
512  if (newsize < core->offset + core->blocksize || oldsize < core->offset + core->blocksize) {
514  }
515  return true;
516 }
517 
519  return file_resize(core, newsize, 0);
520 }
521 
523  return file_resize(core, 0, delta);
524 }
525 
527  // This will be deprecated when moving the . commands to newshell
528  rz_sys_setenv("RZ_FILE", NULL);
529  rz_sys_setenv("RZ_OFFSET", NULL);
530 
531  // remove temporary RZ_CONFIG file
532  char *rz_config = rz_sys_getenv("RZ_CONFIG");
533  if (rz_config) {
534  rz_file_rm(rz_config);
535  rz_sys_setenv("RZ_CONFIG", NULL);
536  free(rz_config);
537  }
538 }
539 
541  // This will be deprecated when moving the . commands to newshell
543  rz_sys_setenv("RZ_BIN_PDBSERVER", rz_config_get(core->config, "pdb.server"));
544  if (desc && desc->name) {
545  rz_sys_setenv("RZ_FILE", desc->name);
546  rz_sys_setenv("RZ_SIZE", sdb_fmt("%" PFMT64d, rz_io_desc_size(desc)));
547  }
548  rz_sys_setenv("RZ_OFFSET", sdb_fmt("%" PFMT64d, core->offset));
549  rz_sys_setenv("RZ_XOFFSET", sdb_fmt("0x%08" PFMT64x, core->offset));
550  rz_sys_setenv("RZ_ENDIAN", core->rasm->big_endian ? "big" : "little");
551  rz_sys_setenv("RZ_BSIZE", sdb_fmt("%d", core->blocksize));
552 
553  // dump current config file so other r2 tools can use the same options
554  char *config_sdb_path = NULL;
555  int config_sdb_fd = rz_file_mkstemp(NULL, &config_sdb_path);
556  if (config_sdb_fd >= 0) {
557  close(config_sdb_fd);
558  }
559 
560  Sdb *config_sdb = sdb_new(NULL, config_sdb_path, 0);
561  rz_config_serialize(core->config, config_sdb);
562  sdb_sync(config_sdb);
563  sdb_free(config_sdb);
564  rz_sys_setenv("RZ_CONFIG", config_sdb_path);
565  rz_sys_setenv("RZ_BIN_LANG", rz_config_get(core->config, "bin.lang"));
566  rz_sys_setenv("RZ_BIN_DEMANGLE", rz_config_get(core->config, "bin.demangle"));
567  rz_sys_setenv("RZ_ARCH", rz_config_get(core->config, "asm.arch"));
568  rz_sys_setenv("RZ_BITS", rz_config_get(core->config, "asm.bits"));
569  rz_sys_setenv("RZ_COLOR", rz_config_get_i(core->config, "scr.color") ? "1" : "0");
570  rz_sys_setenv("RZ_DEBUG", rz_config_get_b(core->config, "cfg.debug") ? "1" : "0");
571  rz_sys_setenv("RZ_IOVA", rz_config_get_i(core->config, "io.va") ? "1" : "0");
572  free(config_sdb_path);
573 }
574 
575 #if !__linux__ && !__WINDOWS__
576 static ut64 get_base_from_maps(RzCore *core, const char *file) {
577  RzDebugMap *map;
578  RzListIter *iter;
579  ut64 b = 0LL;
580 
581  rz_debug_map_sync(core->dbg); // update process memory maps
582  rz_list_foreach (core->dbg->maps, iter, map) {
583  if ((map->perm & 5) == 5) {
584  // TODO: make this more flexible
585  // XXX - why "copy/" here?
586  if (map->name && strstr(map->name, "copy/")) {
587  return map->addr;
588  }
589  if (map->file && !strcmp(map->file, file)) {
590  return map->addr;
591  }
592  if (map->name && !strcmp(map->name, file)) {
593  return map->addr;
594  }
595  // XXX - Commented out, as this could unexpected results
596  // b = map->addr;
597  }
598  }
599  // fallback resolution copied from cmd_debug.c:rz_debug_get_baddr
600  rz_list_foreach (core->dbg->maps, iter, map) {
601  if (map->perm == 5) { // r-x
602  return map->addr;
603  }
604  }
605 
606  return b;
607 }
608 #endif
609 
610 #if __linux__ || __APPLE__
611 static bool setbpint(RzCore *r, const char *mode, const char *sym) {
612  RzBreakpointItem *bp;
613  RzFlagItem *fi = rz_flag_get(r->flags, sym);
614  if (!fi) {
615  return false;
616  }
617  bp = rz_bp_add_sw(r->dbg->bp, fi->offset, 1, RZ_PERM_X);
618  if (bp) {
619  bp->internal = true;
620 #if __linux__
621  bp->data = rz_str_newf("?e %s: %s", mode, sym);
622 #else
623  bp->data = rz_str_newf("?e %s: %s;ps@rdi", mode, sym);
624 #endif
625  return true;
626  }
627  RZ_LOG_ERROR("Cannot set breakpoint at %s\n", sym);
628  return false;
629 }
630 #endif
631 
632 // XXX - need to handle index selection during debugging
633 static bool core_file_do_load_for_debug(RzCore *r, ut64 baseaddr, const char *filenameuri) {
635  RzIODesc *desc = cf ? rz_io_desc_get(r->io, cf->fd) : NULL;
636  RzBinPlugin *plugin;
637  int xtr_idx = 0; // if 0, load all if xtr is used
638 
639  // TODO : Honor file.path eval var too?
640  if (!strncmp("dbg://", filenameuri, 6)) {
641  filenameuri += 6;
642  }
643  if (!desc) {
644  return false;
645  }
646  if (cf) {
647  rz_debug_select(r->dbg, rz_io_fd_get_pid(r->io, cf->fd),
648  rz_io_fd_get_tid(r->io, cf->fd));
649  }
650 #if !__linux__
651 #if !__WINDOWS__
652  baseaddr = get_base_from_maps(r, filenameuri);
653 #endif
654  if (baseaddr != UT64_MAX) {
655  rz_config_set_i(r->config, "bin.baddr", baseaddr);
656  } else if (desc) {
657  ut64 base;
658  if (rz_io_desc_get_base(desc, &base) && base != UT64_MAX) {
659  baseaddr = base;
660  rz_config_set_i(r->config, "bin.baddr", baseaddr);
661  }
662  }
663 #endif
664  int fd = cf ? cf->fd : -1;
665 
666  RzBinOptions opt;
667  rz_bin_options_init(&opt, fd, baseaddr, UT64_MAX, false);
668  opt.obj_opts.elf_load_sections = rz_config_get_b(r->config, "elf.load.sections");
669  opt.obj_opts.elf_checks_sections = rz_config_get_b(r->config, "elf.checks.sections");
670  opt.obj_opts.elf_checks_segments = rz_config_get_b(r->config, "elf.checks.segments");
671  opt.obj_opts.big_endian = rz_config_get_b(r->config, "cfg.bigendian");
672  opt.xtr_idx = xtr_idx;
673  RzBinFile *binfile = rz_bin_open(r->bin, filenameuri, &opt);
674  if (!binfile) {
675  RZ_LOG_ERROR("bin: debug: Cannot open '%s'\n", filenameuri);
676  return false;
677  }
678 
679  if (binfile && cf) {
680  rz_pvector_push(&cf->binfiles, binfile);
681  }
682 
683  if (*rz_config_get(r->config, "dbg.libs")) {
684  rz_core_cmd0(r, ".dmm*");
685 #if __linux__
686  setbpint(r, "dbg.libs", "sym.imp.dlopen");
687  setbpint(r, "dbg.libs", "sym.imp.dlmopen");
688  setbpint(r, "dbg.unlibs", "sym.imp.dlclose");
689 #elif __APPLE__
690  setbpint(r, "dbg.libs", "sym._dlopen");
691  setbpint(r, "dbg.libs", "sym._dlclose");
692 #endif
693  }
694  rz_core_bin_apply_all_info(r, binfile);
696  plugin = rz_bin_file_cur_plugin(binfile);
697  if (plugin && !strcmp(plugin->name, "any")) {
698  // set use of raw strings
699  // rz_config_set_i (r->config, "io.va", false);
700  // get bin.minstr
701  r->bin->minstrlen = rz_config_get_i(r->config, "bin.minstr");
702  r->bin->maxstrbuf = rz_config_get_i(r->config, "bin.maxstrbuf");
703  } else if (binfile) {
704  RzBinObject *obj = rz_bin_cur_object(r->bin);
705  RzBinInfo *info = obj ? obj->info : NULL;
706  if (plugin && info) {
708  }
709  }
710 
711  return true;
712 }
713 
716  int fd = cf ? cf->fd : -1;
717  int xtr_idx = 0; // if 0, load all if xtr is used
718  RzBinPlugin *plugin;
719 
720  if (fd < 0) {
721  return false;
722  }
723  rz_io_use_fd(r->io, fd);
724  RzBinOptions opt;
725  rz_core_bin_options_init(r, &opt, fd, baseaddr, loadaddr);
726  opt.xtr_idx = xtr_idx;
727  RzBinFile *binfile = rz_bin_open_io(r->bin, &opt);
728  if (!binfile) {
729  // RZ_LOG_ERROR("Failed to load the bin with an IO Plugin.\n");
730  return false;
731  }
732  if (cf) {
733  rz_pvector_push(&cf->binfiles, binfile);
734  }
735  if (rz_core_bin_apply_all_info(r, binfile)) {
736  if (!r->analysis->sdb_cc->path) {
737  RZ_LOG_WARN("No calling convention defined for this file, analysis may be inaccurate.\n");
738  }
739  }
740  plugin = rz_bin_file_cur_plugin(binfile);
741  if (plugin && !strcmp(plugin->name, "any")) {
742  RzBinObject *obj = rz_bin_cur_object(r->bin);
743  RzBinInfo *info = obj ? obj->info : NULL;
744  if (!info) {
745  return false;
746  }
747  info->bits = r->rasm->bits;
748  // set use of raw strings
750  // rz_config_set_i (r->config, "io.va", false);
751  // get bin.minstr
752  r->bin->minstrlen = rz_config_get_i(r->config, "bin.minstr");
753  r->bin->maxstrbuf = rz_config_get_i(r->config, "bin.maxstrbuf");
754  } else if (binfile) {
755  RzBinObject *obj = rz_bin_cur_object(r->bin);
756  RzBinInfo *info = obj ? obj->info : NULL;
757  if (!info) {
758  return false;
759  }
760  if (plugin && info) {
761  rz_core_bin_set_arch_bits(r, binfile->file,
762  info->arch, info->bits);
763  }
764  }
765 
766  return true;
767 }
768 
769 static bool try_loadlib(RzCore *core, const char *lib, ut64 addr) {
770  if (rz_core_file_open(core, lib, 0, addr) != NULL) {
771  rz_core_bin_load(core, lib, addr);
772  return true;
773  }
774  return false;
775 }
776 
777 RZ_API bool rz_core_file_loadlib(RzCore *core, const char *lib, ut64 libaddr) {
778  const char *dirlibs = rz_config_get(core->config, "dir.libs");
779  char *libdir = rz_path_libdir();
780  if (!dirlibs || !*dirlibs) {
781  dirlibs = "." RZ_SYS_DIR;
782  }
783  const char *ldlibrarypath[] = {
784  dirlibs,
785  libdir,
786 #ifndef __WINDOWS__
787  "/usr/local/lib",
788  "/usr/lib",
789  "/lib",
790 #endif
791  "." RZ_SYS_DIR,
792  NULL
793  };
794  const char **libpath = (const char **)&ldlibrarypath;
795 
796  bool ret = false;
797 #ifdef __WINDOWS__
798  if (strlen(lib) >= 3 && lib[1] == ':' && lib[2] == '\\') {
799 #else
800  if (*lib == '/') {
801 #endif
802  if (try_loadlib(core, lib, libaddr)) {
803  ret = true;
804  }
805  } else {
806  while (*libpath) {
807  char *s = rz_str_newf("%s" RZ_SYS_DIR "%s", *libpath, lib);
808  if (try_loadlib(core, s, libaddr)) {
809  ret = true;
810  }
811  free(s);
812  if (ret) {
813  break;
814  }
815  libpath++;
816  }
817  }
818  free(libdir);
819  return ret;
820 }
821 
823  if (!core || !core->bin || !core->bin->cur) {
824  return 0;
825  }
826  if (baddr == UT64_MAX) {
827  return 0;
828  }
829  RzBinFile *bf = core->bin->cur;
830  bf->o->opts.baseaddr = baddr;
831  bf->o->opts.loadaddr = baddr;
832  rz_bin_object_set_items(bf, bf->o);
833  return 1;
834 }
835 
836 static void load_scripts_for(RzCore *core, const char *name) {
837  char *file;
838  RzListIter *iter;
839  char *binrc = rz_path_home_prefix(RZ_BINRC);
840  char tmp[50];
841  char *hdir = rz_file_path_join(binrc, rz_strf(tmp, "bin-%s", name));
842  free(binrc);
843  RzList *files = rz_sys_dir(hdir);
844  if (!rz_list_empty(files)) {
845  RZ_LOG_INFO("[binrc] path: '%s'\n", hdir);
846  }
847  rz_list_foreach (files, iter, file) {
848  if (*file && *file != '.') {
849  RZ_LOG_INFO("[binrc] loading '%s'\n", file);
850  char *fullpath = rz_file_path_join(hdir, file);
851  rz_core_run_script(core, fullpath);
852  free(fullpath);
853  }
854  }
856  free(hdir);
857 }
858 
859 typedef struct {
860  const char *name;
861  bool found;
863 
864 static bool filecb(void *user, void *data, ut32 id) {
865  RzCoreFileData *filedata = user;
866  RzIODesc *desc = (RzIODesc *)data;
867  if (!strcmp(desc->name, filedata->name)) {
868  filedata->found = true;
869  }
870  return true;
871 }
872 
873 static bool file_is_loaded(RzCore *core, const char *lib) {
874  RzCoreFileData filedata = { lib, false };
875  rz_id_storage_foreach(core->io->files, filecb, &filedata);
876  return filedata.found;
877 }
878 
879 typedef struct {
880  const char *name;
884 
886  rz_return_val_if_fail(ld && desc, false);
887  RzBinFile *bf = rz_bin_file_find_by_fd(ld->bin, desc->fd);
888  if (!bf) {
889  return true;
890  }
891  RzListIter *iter;
892  RzBinSymbol *sym;
894  rz_list_foreach (symbols, iter, sym) {
895  if (!strcmp(sym->name, ld->name)) {
896  ld->addr = sym->vaddr;
897  return false;
898  }
899  }
900  return true;
901 }
902 
903 static bool map_multi_dex(RzCore *core, RzIODesc *desc, ut32 id) {
904  rz_return_val_if_fail(core && desc, false);
905  if (!rz_str_endswith(desc->name, ".dex")) {
906  return true;
907  }
908  RzCoreFile *cf = rz_core_file_cur(core);
909 
910  // adds the current size and aligns next address
911  ut64 base_address = RZ_CORE_BASE_ADDRESS_DEX;
913  if (cur) {
914  RzIODesc *iod = rz_io_desc_get(core->io, cur->fd);
915  if (iod) {
916  base_address = cur->loadaddr;
917  base_address += rz_io_desc_size(iod);
918  rz_core_align_base_address(base_address);
919  }
920  }
921 
923  RZ_LOG_INFO("Mapping at 0x%08" PFMT64x " with size 0x08%" PFMT64x " %s\n", base_address, size, desc->name);
924 
925  rz_io_use_fd(core->io, desc->fd);
926  RzBinOptions opt;
927  rz_core_bin_options_init(core, &opt, desc->fd, base_address, 0);
928  opt.xtr_idx = 0;
929  RzBinFile *binfile = rz_bin_open_io(core->bin, &opt);
930  if (!binfile) {
931  RZ_LOG_ERROR("Cannot load bin file %s.\n", desc->name);
932  return true;
933  }
934  binfile->loadaddr = base_address;
935 
936  rz_pvector_push(&cf->binfiles, binfile);
937  rz_core_bin_apply_all_info(core, binfile);
938 
939  return true;
940 }
941 
942 RZ_API bool rz_core_bin_load(RZ_NONNULL RzCore *r, RZ_NULLABLE const char *filenameuri, ut64 baddr) {
943  rz_return_val_if_fail(r, false);
944 
946  RzIODesc *desc = cf ? rz_io_desc_get(r->io, cf->fd) : NULL;
947  ut64 laddr = rz_config_get_i(r->config, "bin.laddr");
948  RzBinFile *binfile = NULL;
949  RzBinPlugin *plugin = NULL;
950  bool is_io_load = false;
951  const char *cmd_load;
952  if (!cf) {
953  return false;
954  }
955 
956  is_io_load = desc && desc->plugin;
957  if (desc && RZ_STR_ISEMPTY(filenameuri)) {
958  filenameuri = desc->name;
959  }
960 
961  if (RZ_STR_ISEMPTY(filenameuri)) {
962  RZ_LOG_ERROR("rz_core_bin_load: no file specified\n");
963  return false;
964  }
965 
966  r->bin->minstrlen = rz_config_get_i(r->config, "bin.minstr");
967  r->bin->maxstrbuf = rz_config_get_i(r->config, "bin.maxstrbuf");
968  if (is_io_load) {
969  // TODO? necessary to restore the desc back?
970  // Fix to select pid before trying to load the binary
971  if ((desc->plugin && desc->plugin->isdbg) || rz_config_get_b(r->config, "cfg.debug")) {
972  core_file_do_load_for_debug(r, baddr, filenameuri);
973  } else {
975  if (!strncmp(filenameuri, "apk://", 6) && r->io->files->size > 1) {
976  RZ_LOG_INFO("Found multidex APK, mapping extra files\n");
978  // forbid loading libraries even when set.
979  rz_config_set_b(r->config, "bin.libs", false);
980  }
981  }
982  // Restore original desc
983  rz_io_use_fd(r->io, desc->fd);
984  }
985  binfile = rz_bin_cur(r->bin);
986  if (cf && binfile && desc) {
987  binfile->fd = desc->fd;
988  }
989  // rz_core_bin_apply_all_info (r, binfile);
990  plugin = rz_bin_file_cur_plugin(binfile);
991  if (plugin) {
992  if (plugin->strfilter) {
993  char msg[2];
994  msg[0] = plugin->strfilter;
995  msg[1] = 0;
996  rz_config_set(r->config, "bin.str.filter", msg);
997  }
998  if (plugin->name) {
999  load_scripts_for(r, plugin->name);
1000  }
1001  }
1002 
1003  cmd_load = rz_config_get(r->config, "cmd.load");
1004  if (cmd_load && *cmd_load) {
1005  rz_core_cmd(r, cmd_load, 0);
1006  }
1007 
1008  if (plugin && plugin->name) {
1009  if (!strcmp(plugin->name, "any")) {
1010  if (rz_str_startswith(desc->name, "rap") && strstr(desc->name, "://")) {
1011  rz_io_map_new(r->io, desc->fd, desc->perm, 0, laddr, UT64_MAX);
1012  } else {
1013  rz_io_map_new(r->io, desc->fd, desc->perm, 0, laddr, rz_io_desc_size(desc));
1014  }
1015  // set use of raw strings
1016  // rz_config_set_i (r->config, "io.va", false);
1017  // get bin.minstr
1018  r->bin->minstrlen = rz_config_get_i(r->config, "bin.minstr");
1019  r->bin->maxstrbuf = rz_config_get_i(r->config, "bin.maxstrbuf");
1020  } else if (binfile) {
1021  RzBinObject *obj = rz_bin_cur_object(r->bin);
1022  if (obj) {
1023  bool va = obj->info ? obj->info->has_va : 0;
1024  if (!va) {
1025  rz_config_set_i(r->config, "io.va", 0);
1026  }
1027  // workaround to map correctly malloc:// and raw binaries
1028  if (rz_io_desc_is_dbg(desc) || (!obj->maps || !va)) {
1029  rz_io_map_new(r->io, desc->fd, desc->perm, 0, laddr, rz_io_desc_size(desc));
1030  }
1031  RzBinInfo *info = obj->info;
1032  if (info) {
1034  } else {
1035  rz_core_bin_set_arch_bits(r, binfile->file,
1036  rz_config_get(r->config, "asm.arch"),
1037  rz_config_get_i(r->config, "asm.bits"));
1038  }
1039  }
1040  }
1041  } else {
1042  if (desc) {
1043  rz_io_map_new(r->io, desc->fd, desc->perm, 0, laddr, rz_io_desc_size(desc));
1044  }
1045  if (binfile) {
1046  ut16 bits = rz_config_get_i(r->config, "asm.bits");
1047  const char *arch = rz_config_get(r->config, "asm.arch");
1048  rz_core_bin_set_arch_bits(r, binfile->file, arch, bits);
1049  }
1050  }
1051  if (desc && rz_config_get_i(r->config, "io.exec")) {
1052  desc->perm |= RZ_PERM_X;
1053  }
1054  if (!rz_config_get_b(r->config, "cfg.debug")) {
1055  loadGP(r);
1056  }
1057  if (rz_config_get_b(r->config, "bin.libs")) {
1058  const char *lib;
1059  RzListIter *iter;
1060  RzList *libs = rz_bin_get_libs(r->bin);
1061  rz_list_foreach (libs, iter, lib) {
1062  if (file_is_loaded(r, lib)) {
1063  continue;
1064  }
1065  RZ_LOG_INFO("Opening library %s\n", lib);
1066  ut64 baddr = rz_io_map_location(r->io, 0x200000);
1067  if (baddr != UT64_MAX) {
1068  rz_core_file_loadlib(r, lib, baddr);
1069  }
1070  }
1071 
1072  rz_core_bin_raise(r, 0);
1073  ut64 ocurr = r->offset;
1074  ut64 entry0addr = rz_num_math(r->num, "entry0");
1075  rz_core_seek(r, entry0addr, true);
1076 
1077  rz_config_set_b(r->config, "bin.at", true);
1078  RZ_LOG_INFO("Linking imports...\n");
1079  RzBinImport *imp;
1081  rz_list_foreach (imports, iter, imp) {
1082  char *name = rz_str_newf("sym.imp.%s", imp->name);
1083  rz_name_filter(name + 8, strlen(name + 8) + 1, true);
1084 
1085  RzFlagItem *flag = rz_flag_get(r->flags, name);
1086  if (!flag) {
1087  // RZ_LOG_DEBUG("Cannot find flag %s\n", name);
1088  free(name);
1089  continue;
1090  }
1091  ut64 imp_addr = flag->offset;
1092  RzCoreLinkData linkdata = { imp->name, UT64_MAX, r->bin };
1094  if (linkdata.addr != UT64_MAX) {
1095  RZ_LOG_INFO("Resolved %s with address 0x%08" PFMT64x "\n", name, linkdata.addr);
1096  ut64 a = linkdata.addr;
1097  ut64 b = imp_addr;
1099  } else {
1100  RZ_LOG_ERROR("Cannot resolve %s\n", name);
1101  }
1102  free(name);
1103  }
1104  rz_core_seek(r, ocurr, true);
1105  }
1106 
1107  // If type == RZ_BIN_TYPE_CORE, we need to create all the maps
1108  if (plugin && binfile && plugin->file_type && plugin->file_type(binfile) == RZ_BIN_TYPE_CORE) {
1109  // Setting the right arch and bits, so regstate will be shown correctly
1110  if (plugin->info) {
1111  RzBinInfo *inf = plugin->info(binfile);
1112  RZ_LOG_INFO("Setting up coredump arch-bits to: %s-%d\n", inf->arch, inf->bits);
1113  rz_config_set(r->config, "asm.arch", inf->arch);
1114  rz_config_set_i(r->config, "asm.bits", inf->bits);
1116  }
1117  if (binfile->o->regstate) {
1118  if (rz_reg_arena_set_bytes(r->analysis->reg, binfile->o->regstate)) {
1119  RZ_LOG_WARN("Setting up coredump: Problem while setting the registers\n");
1120  } else {
1121  RZ_LOG_INFO("Setting up coredump: Registers have been set\n");
1122  }
1123  }
1124  }
1125  return true;
1126 }
1127 
1134  RzList *list_fds = rz_io_open_many(r->io, file, perm, 0644);
1135 
1136  if (rz_list_empty(list_fds)) {
1137  rz_list_free(list_fds);
1138  return NULL;
1139  }
1140 
1141  if (!base_address) {
1142  base_address = RZ_CORE_BASE_ADDRESS_DEFAULT;
1143  }
1144 
1145  RzListIter *it = NULL;
1146  RzIODesc *desc = NULL;
1147  RzIODesc *first = NULL;
1148  rz_list_foreach (list_fds, it, desc) {
1149  if (!rz_io_desc_add(r->io, desc)) {
1151  continue;
1152  } else if (!first) {
1153  first = desc;
1154  }
1155 
1156  RzCoreFile *fh = core_file_new(r, desc->fd);
1157  if (!fh) {
1158  continue;
1159  }
1160  r->file = fh;
1161  rz_list_append(r->files, fh);
1163  RZ_LOG_INFO("Mapping at 0x%08" PFMT64x " with size 0x08%" PFMT64x " %s\n", base_address, size, desc->name);
1164  if (!rz_core_bin_load(r, desc->name, base_address)) {
1165  RZ_LOG_ERROR("failed to load %s\n", desc->name);
1166  }
1167  // adds the current size and aligns next address
1168  base_address += size;
1169  rz_core_align_base_address(base_address);
1170  }
1171 
1172  rz_list_free(list_fds);
1173  return rz_list_first(r->files);
1174 }
1175 
1184  ut64 prev = rz_time_now_mono();
1185  const bool openmany = rz_config_get_i(r->config, "file.openmany");
1186  RzCoreFile *fh = NULL;
1187 
1188  if (!strcmp(file, "=")) {
1189  file = "malloc://512";
1190  }
1191  // if not flags was passed open it with -r--
1192  if (!flags) {
1193  flags = RZ_PERM_R;
1194  }
1195  r->io->bits = r->rasm->bits; // TODO: we need an api for this
1196  RzIODesc *fd = rz_io_open_nomap(r->io, file, flags, 0644);
1197  if (rz_cons_is_breaked()) {
1198  goto beach;
1199  }
1200  if (!fd && openmany) {
1201  if (!(fh = rz_core_file_open_many(r, file, flags, loadaddr))) {
1202  goto beach;
1203  }
1204  fd = rz_io_desc_get(r->io, fh->fd);
1205  }
1206  if (!fd) {
1207  if (flags & RZ_PERM_W) {
1208  // flags |= RZ_IO_CREAT;
1209  if (!(fd = rz_io_open_nomap(r->io, file, flags, 0644))) {
1210  goto beach;
1211  }
1212  } else {
1213  goto beach;
1214  }
1215  }
1216  if (rz_io_is_listener(r->io)) {
1217  rz_core_serve(r, fd);
1219  goto beach;
1220  }
1221 
1222  if (!fh) {
1223  fh = core_file_new(r, fd->fd);
1224  if (!fh) {
1225  RZ_LOG_ERROR("rz_core_file_open: failed to allocate RzCoreFile.\n");
1226  goto beach;
1227  }
1228  }
1229  {
1230  const char *cp = rz_config_get(r->config, "cmd.open");
1231  if (cp && *cp) {
1232  rz_core_cmd(r, cp, 0);
1233  }
1234  char *absfile = rz_file_abspath(file);
1235  rz_config_set(r->config, "file.path", absfile);
1236  free(absfile);
1237  }
1238 
1239  r->file = fh;
1240  rz_io_use_fd(r->io, fd->fd);
1241 
1242  if (!rz_list_find_ptr(r->files, fh)) {
1243  rz_list_append(r->files, fh);
1244  }
1245  if (rz_config_get_b(r->config, "cfg.debug")) {
1246  bool swstep = true;
1247  if (r->dbg->cur && r->dbg->cur->canstep) {
1248  swstep = false;
1249  }
1250  rz_config_set_i(r->config, "dbg.swstep", swstep);
1251  // Set the correct debug handle
1252  if (fd->plugin && fd->plugin->isdbg) {
1253  char *dh = rz_str_ndup(file, (strstr(file, "://") - file));
1254  if (dh) {
1255  rz_debug_use(r->dbg, dh);
1256  free(dh);
1257  }
1258  }
1259  }
1260  // used by rz_core_bin_load otherwise won't load correctly
1261  // this should be argument of rz_core_bin_load <shrug>
1262  if (loadaddr != UT64_MAX) {
1263  rz_config_set_i(r->config, "bin.laddr", loadaddr);
1264  }
1265  rz_core_cmd0(r, "R!");
1266 beach:
1267  r->times->file_open_time = rz_time_now_mono() - prev;
1268  return fh;
1269 }
1270 
1272  // remove all references to the closed desc
1273  RzListIter *it;
1274  RzCoreFile *cf;
1275  rz_list_foreach (core->files, it, cf) {
1277  }
1278 }
1279 
1281  // remove all references to the deleted map
1282  RzListIter *it;
1283  RzCoreFile *cf;
1284  rz_list_foreach (core->files, it, cf) {
1286  }
1288 }
1289 
1291  // remove all references to the deleted binfile
1292  RzListIter *it;
1293  RzCoreFile *cf;
1294  rz_list_foreach (core->files, it, cf) {
1295  rz_pvector_remove_data(&cf->binfiles, bf);
1296  }
1297 }
1298 
1300  rz_return_if_fail(fh && fh->core);
1301  RzCore *r = fh->core;
1302  RzListIter *fh_it = rz_list_find_ptr(r->files, fh);
1303  rz_return_if_fail(fh_it);
1304  RzIODesc *desc = rz_io_desc_get(r->io, fh->fd);
1305  if (desc) {
1307  }
1308  while (!rz_pvector_empty(&fh->maps)) {
1309  // The element will automatically be removed from the vector through events
1310  // always delete the last to avoid unnecessary copies
1311  RzIOMap *map = rz_pvector_at(&fh->maps, rz_pvector_len(&fh->maps) - 1);
1312  rz_io_map_del(r->io, map->id);
1313  }
1314  while (!rz_pvector_empty(&fh->extra_files)) {
1315  // same as for maps above
1316  rz_io_desc_close(rz_pvector_at(&fh->extra_files, rz_pvector_len(&fh->extra_files) - 1));
1317  }
1318  while (!rz_pvector_empty(&fh->binfiles)) {
1319  // same as for maps above
1320  rz_bin_file_delete(r->bin, rz_pvector_at(&fh->binfiles, rz_pvector_len(&fh->binfiles) - 1));
1321  }
1322  if (r->file == fh) {
1323  r->file = NULL;
1324  }
1325  rz_list_delete(r->files, fh_it);
1326 }
1327 
1329  RzCoreFile *file;
1330  RzListIter *iter;
1331  rz_list_foreach (core->files, iter, file) {
1332  if (file->fd == fd) {
1333  return file;
1334  }
1335  }
1336  return NULL;
1337 }
1338 
1340  RzCoreFile *f;
1341  RzIODesc *desc;
1342  RzBinFile *bf;
1343  RzListIter *it1, *it2, *it3;
1344  rz_list_foreach (core->files, it1, f) {
1345  desc = rz_io_desc_get(core->io, f->fd);
1346  if (!desc) {
1347  continue;
1348  }
1349  bool header_loaded = false;
1350  rz_list_foreach (core->bin->binfiles, it2, bf) {
1351  if (bf->fd == f->fd) {
1352  header_loaded = true;
1353  break;
1354  }
1355  }
1356  if (!header_loaded) {
1357  RzList *maps = rz_io_map_get_for_fd(core->io, f->fd);
1358  RzIOMap *current_map;
1359  char *absfile = rz_file_abspath(desc->uri);
1360  rz_list_foreach (maps, it3, current_map) {
1361  if (current_map) {
1362  rz_cons_printf("on %s 0x%" PFMT64x "\n", absfile, current_map->itv.addr);
1363  }
1364  }
1365  rz_list_free(maps);
1366  free(absfile);
1367  }
1368  }
1369  return true;
1370 }
1371 
1373  int count = 0;
1374  RzCoreFile *f;
1375  RzIODesc *desc;
1376  ut64 from;
1377  RzListIter *it;
1378  RzBinFile *bf;
1379  RzListIter *iter;
1380  PJ *pj = NULL;
1381  if (mode == RZ_OUTPUT_MODE_JSON) {
1382  pj = pj_new();
1383  if (!pj) {
1384  return false;
1385  }
1386  pj_a(pj);
1387  }
1388  rz_list_foreach (core->files, iter, f) {
1389  desc = rz_io_desc_get(core->io, f->fd);
1390  if (!desc) {
1391  // cannot find desc for this fd, RzCoreFile inconsistency!!!1
1392  continue;
1393  }
1394  from = 0LL;
1395  switch (mode) {
1396  case RZ_OUTPUT_MODE_JSON: { // "oij"
1397  pj_o(pj);
1398  pj_kb(pj, "raised", core->io->desc->fd == f->fd);
1399  pj_ki(pj, "fd", f->fd);
1400  pj_ks(pj, "uri", desc->uri);
1401  pj_kn(pj, "from", (ut64)from);
1402  pj_kb(pj, "writable", desc->perm & RZ_PERM_W);
1403  pj_ki(pj, "size", (int)rz_io_desc_size(desc));
1404  pj_end(pj);
1405  break;
1406  }
1407  case RZ_OUTPUT_MODE_RIZIN:
1408  // TODO: use a getter
1409  {
1410  bool fileHaveBin = false;
1411  char *absfile = rz_file_abspath(desc->uri);
1412  rz_list_foreach (core->bin->binfiles, it, bf) {
1413  if (bf->fd == f->fd) {
1414  rz_cons_printf("o %s 0x%" PFMT64x "\n", absfile, (ut64)from);
1415  fileHaveBin = true;
1416  }
1417  }
1418  if (!fileHaveBin && !strstr(absfile, "://")) {
1419  rz_cons_printf("o %s 0x%" PFMT64x "\n", absfile, (ut64)from);
1420  }
1421  free(absfile);
1422  }
1423  break;
1424  default: {
1425  ut64 sz = rz_io_desc_size(desc);
1426  const char *fmt;
1427  if (sz == UT64_MAX) {
1428  fmt = "%c %d %d %s @ 0x%" PFMT64x " ; %s size=%" PFMT64d "\n";
1429  } else {
1430  fmt = "%c %d %d %s @ 0x%" PFMT64x " ; %s size=%" PFMT64u "\n";
1431  }
1432  rz_cons_printf(fmt,
1433  core->io->desc->fd == f->fd ? '*' : '-',
1434  count,
1435  (int)f->fd, desc->uri, (ut64)from,
1436  desc->perm & RZ_PERM_W ? "rw" : "r",
1438  } break;
1439  }
1440  count++;
1441  }
1442  if (mode == RZ_OUTPUT_MODE_JSON) {
1443  pj_end(pj);
1445  pj_free(pj);
1446  }
1447  return true;
1448 }
1449 
1450 // XXX - needs to account for binfile index and bin object index
1452  RzBin *bin = core->bin;
1453  RzBinFile *bf = rz_list_get_n(bin->binfiles, bfid);
1454  bool res = false;
1455  if (bf) {
1456  res = rz_bin_file_set_cur_binfile(bin, bf);
1457  if (res) {
1458  rz_io_use_fd(core->io, bf->fd);
1459  }
1460  res = res ? rz_core_file_set_by_fd(core, bf->fd) : res;
1461  }
1462  return res;
1463 }
1464 
1466  int count = 0;
1467  RzListIter *iter;
1468  RzCoreFile *cur_cf = core->file, *cf = NULL;
1469  RzBinFile *binfile = NULL;
1470  RzIODesc *desc;
1471  RzBin *bin = core->bin;
1472  const RzList *binfiles = bin ? bin->binfiles : NULL;
1473 
1474  if (!binfiles) {
1475  return false;
1476  }
1477  rz_list_foreach (binfiles, iter, binfile) {
1478  int fd = binfile->fd;
1479  cf = rz_core_file_get_by_fd(core, fd);
1480  desc = rz_io_desc_get(core->io, fd);
1481  if (cf) {
1482  rz_cons_printf("%c %d %s ; %s\n",
1483  core->io->desc == desc ? '*' : '-',
1484  fd, desc->uri, desc->perm & RZ_PERM_W ? "rw" : "r");
1485  }
1486  }
1487  rz_core_file_set_by_file(core, cur_cf);
1488  // rz_core_bin_bind (core, cur_bf);
1489  return count;
1490 }
1491 
1492 static bool close_but_cb(void *user, void *data, ut32 id) {
1493  RzCore *core = (RzCore *)user;
1494  RzIODesc *desc = (RzIODesc *)data;
1495  if (core && desc && core->file) {
1496  if (desc->fd != core->file->fd) {
1497  if (!rz_core_file_close_fd(core, desc->fd)) {
1498  return false;
1499  }
1500  }
1501  }
1502  return true;
1503 }
1504 
1506  rz_id_storage_foreach(core->io->files, close_but_cb, core);
1507  return true;
1508 }
1509 
1511  RzCoreFile *file;
1512  RzListIter *iter;
1513  if (fd == -1) {
1514  while (!rz_list_empty(core->files)) {
1516  }
1517  return true;
1518  }
1519  rz_list_foreach (core->files, iter, file) {
1520  if (file->fd == fd) {
1522  return true;
1523  }
1524  }
1525  return rz_io_fd_close(core->io, fd);
1526 }
1527 
1529  RzListIter *iter;
1530  RzCoreFile *cf = NULL;
1531  rz_list_foreach (core->files, iter, cf) {
1532  if (cf && cf->fd == fd) {
1533  break;
1534  }
1535  cf = NULL;
1536  }
1537  return cf;
1538 }
1539 
1541  RzListIter *iter;
1542  RzCoreFile *cf = NULL;
1543  RzIODesc *desc;
1544 
1545  if (!core) {
1546  return NULL;
1547  }
1548 
1549  rz_list_foreach (core->files, iter, cf) {
1550  desc = rz_io_desc_get(core->io, cf->fd);
1551  if (desc && !strcmp(desc->name, name)) {
1552  break;
1553  }
1554  cf = NULL;
1555  }
1556  return cf;
1557 }
1558 
1560  if (core) {
1561  rz_io_use_fd(core->io, fd);
1562  rz_core_bin_set_by_fd(core, fd);
1563  return true;
1564  }
1565  return false;
1566 }
1567 
1568 RZ_API int rz_core_file_set_by_name(RzCore *core, const char *name) {
1570  return rz_core_file_set_by_file(core, cf);
1571 }
1572 
1574  if (core && cf) {
1575  if (!rz_core_file_set_by_fd(core, cf->fd)) {
1576  return false;
1577  }
1578  core->file = cf;
1579  return true;
1580  }
1581  return false;
1582 }
1583 
1585  if (core && core->file) {
1586  return core->file->fd;
1587  }
1588  return UT32_MAX;
1589 }
1590 
1592  // Add any locks here
1593  return r->file;
1594 }
1595 
1602  rz_return_if_fail(core && fd >= 0);
1603  if (!rz_config_get_b(core->config, "cfg.debug")) {
1604  rz_io_reopen(core->io, fd, RZ_PERM_R, 644);
1605  return;
1606  }
1607  RzBinFile *bf = rz_bin_cur(core->bin);
1608  if (!(bf && rz_file_exists(bf->file))) {
1609  RZ_LOG_WARN("Cannot open current RzBinFile.\n");
1610  return;
1611  }
1612 
1613  // Escape spaces so that o's argv parse will detect the path properly
1614  char *file = rz_str_path_escape(bf->file);
1615  // Backup the baddr and sections that were already rebased to
1616  // revert the rebase after the debug session is closed
1617  ut64 orig_baddr = core->bin->cur->o->baddr_shift;
1618  RzList *orig_sections = __save_old_sections(core);
1619 
1620  rz_bin_file_delete_all(core->bin);
1621  rz_io_close_all(core->io);
1622  rz_config_set_b(core->config, "cfg.debug", false);
1623 
1625  if (!cfile) {
1626  rz_list_free(orig_sections);
1627  RZ_LOG_ERROR("Cannot open file '%s'\n", file);
1628  return;
1629  }
1630  core->num->value = cfile->fd;
1631  // If no baddr defined, use the one provided by the file
1632  if (!rz_core_bin_load(core, file, UT64_MAX)) {
1633  rz_list_free(orig_sections);
1634  RZ_LOG_ERROR("Cannot load binary info of '%s'.\n", file);
1635  return;
1636  }
1637  rz_core_block_read(core);
1638 
1639  __rebase_everything(core, orig_sections, orig_baddr);
1640  rz_list_free(orig_sections);
1641  free(file);
1642 }
1643 
1650 RZ_API void rz_core_io_file_reopen(RZ_NONNULL RzCore *core, int fd, int perms) {
1651  rz_return_if_fail(core && fd >= 0);
1652  if (rz_io_reopen(core->io, fd, perms, 644)) {
1653  void **it;
1654  RzPVector *maps = rz_io_maps(core->io);
1655  if (!maps) {
1656  return;
1657  }
1659  RzIOMap *map = *it;
1660  if (map->fd == fd) {
1661  map->perm |= RZ_PERM_WX;
1662  }
1663  }
1664  }
1665 }
1666 
1667 /* --------------------------------------------------------------------------------- */
1668 
1671  if (!info) {
1672  return NULL;
1673  }
1674  info->cf = cf;
1675  info->perm_orig = perm_orig;
1676  return info;
1677 }
1678 
1680  free(info);
1681 }
RZ_API bool rz_analysis_function_relocate(RzAnalysisFunction *fcn, ut64 addr)
Definition: function.c:204
#define RZ_IPI
Definition: analysis_wasm.c:11
RZ_API int rz_reg_arena_set_bytes(RzReg *reg, const char *str)
Definition: arena.c:314
RZ_API bool rz_bin_file_set_cur_binfile(RzBin *bin, RzBinFile *bf)
Definition: bfile.c:288
RZ_API ut64 rz_bin_file_delete_all(RzBin *bin)
Definition: bfile.c:199
RZ_API RzList * rz_bin_file_get_symbols(RzBinFile *bf)
Definition: bfile.c:572
RZ_API RzBinFile * rz_bin_file_find_by_fd(RzBin *bin, ut32 bin_fd)
Definition: bfile.c:226
RZ_API bool rz_bin_file_delete(RzBin *bin, RzBinFile *bf)
Definition: bfile.c:213
RZ_API RzBinPlugin * rz_bin_file_cur_plugin(RzBinFile *bf)
Definition: bfile.c:348
RZ_API void rz_bin_info_free(RzBinInfo *rb)
Definition: bin.c:97
RZ_API RzBinObject * rz_bin_cur_object(RzBin *bin)
Definition: bin.c:900
RZ_DEPRECATE RZ_API RZ_BORROW RzList * rz_bin_get_imports(RZ_NONNULL RzBin *bin)
Definition: bin.c:579
RZ_DEPRECATE RZ_API RZ_BORROW RzList * rz_bin_get_libs(RZ_NONNULL RzBin *bin)
Definition: bin.c:591
RZ_API RzBinFile * rz_bin_open_io(RzBin *bin, RzBinOptions *opt)
Definition: bin.c:283
RZ_API ut64 rz_bin_get_baddr(RzBin *bin)
Definition: bin.c:536
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 RzBinFile * rz_bin_cur(RzBin *bin)
Definition: bin.c:895
RZ_DEPRECATE RZ_API RZ_BORROW RzList * rz_bin_get_sections(RZ_NONNULL RzBin *bin)
Definition: bin.c:597
static ut64 baddr(RzBinFile *bf)
Definition: bin_any.c:58
static RzList * maps(RzBinFile *bf)
Definition: bin_bf.c:116
static RzList * libs(RzBinFile *bf)
Definition: bin_coff.c:379
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
RzList * symbols(RzBinFile *bf)
Definition: bin_ne.c:102
RzList * imports(RzBinFile *bf)
Definition: bin_ne.c:106
RzList * sections(RzBinFile *bf)
Definition: bin_ne.c:110
const char * desc
Definition: bin_vsf.c:19
int bits(struct state *s, int need)
Definition: blast.c:72
RZ_API bool rz_analysis_block_relocate(RzAnalysisBlock *block, ut64 addr, ut64 size)
Definition: block.c:213
RZ_API int rz_bin_object_set_items(RzBinFile *bf, RzBinObject *o)
Definition: bobj.c:392
RZ_API RZ_BORROW RzBreakpointItem * rz_bp_add_sw(RZ_NONNULL RzBreakpoint *bp, ut64 addr, int size, int perm)
Add a software breakpoint size preferred size of the breakpoint, or 0 to determine automatically.
Definition: bp.c:280
FILE * fh
Definition: cabinfo.c:52
RZ_API int rz_core_bin_set_by_fd(RzCore *core, ut64 bin_fd)
Definition: cbin.c:100
RZ_API int rz_core_bin_update_arch_bits(RzCore *r)
Definition: cbin.c:4503
RZ_API void rz_core_bin_options_init(RzCore *core, RZ_OUT RzBinOptions *opts, int fd, ut64 baseaddr, ut64 loadaddr)
Definition: cbin.c:87
RZ_API bool rz_core_bin_raise(RzCore *core, ut32 bfid)
Definition: cbin.c:4524
RZ_API int rz_core_bin_set_arch_bits(RzCore *r, const char *name, const char *arch, ut16 bits)
Definition: cbin.c:4467
RZ_API bool rz_core_bin_apply_all_info(RzCore *r, RzBinFile *binfile)
Definition: cbin.c:311
RZ_API void rz_core_debug_map_update_flags(RzCore *core)
Definition: cdebug.c:462
RZ_API bool rz_core_debug_step_one(RzCore *core, int times)
Definition: cdebug.c:56
static bool filecb(void *user, void *data, ut32 id)
Definition: cfile.c:864
RZ_API ut32 rz_core_file_cur_fd(RzCore *core)
Definition: cfile.c:1584
RZ_API RZ_BORROW RzCoreFile * rz_core_file_open_many(RZ_NONNULL RzCore *r, RZ_NULLABLE const char *file, int perm, ut64 base_address)
Open the file as a compilation of files.
Definition: cfile.c:1133
static bool __isMips(RzAsm *a)
Definition: cfile.c:36
RZ_IPI void rz_core_file_io_desc_closed(RzCore *core, RzIODesc *desc)
Definition: cfile.c:1271
RZ_API bool rz_core_file_close_all_but(RzCore *core)
Definition: cfile.c:1505
RZ_API void rz_core_sysenv_begin(RzCore *core)
Definition: cfile.c:540
RZ_API bool rz_core_file_loadlib(RzCore *core, const char *lib, ut64 libaddr)
Definition: cfile.c:777
static void loadGP(RzCore *core)
Definition: cfile.c:40
static bool core_file_do_load_for_debug(RzCore *r, ut64 loadaddr, const char *filenameuri)
Definition: cfile.c:633
RZ_API RzCoreFile * rz_core_file_cur(RzCore *r)
Definition: cfile.c:1591
RZ_IPI RzCoreIOMapInfo * rz_core_io_map_info_new(RzCoreFile *cf, int perm_orig)
Definition: cfile.c:1669
RZ_API bool rz_core_file_reopen(RzCore *core, const char *args, int perm, int loadbin)
Definition: cfile.c:322
RZ_API RzCoreFile * rz_core_file_find_by_fd(RzCore *core, ut64 fd)
Definition: cfile.c:1528
RZ_API bool rz_core_file_print(RzCore *core, RzOutputMode mode)
Definition: cfile.c:1372
static RzCoreFile * core_file_new(RzCore *core, int fd)
Definition: cfile.c:13
static ut64 get_base_from_maps(RzCore *core, const char *file)
Definition: cfile.c:576
#define __is_inside_section(item_addr, section)
Definition: cfile.c:90
RZ_API void rz_core_file_reopen_debug(RzCore *core, const char *args)
Definition: cfile.c:269
RZ_API void rz_core_file_close(RzCoreFile *fh)
Definition: cfile.c:1299
RZ_API void rz_core_io_file_open(RZ_NONNULL RzCore *core, int fd)
Open file use read-only Permission.
Definition: cfile.c:1601
RZ_API int rz_core_file_set_by_file(RzCore *core, RzCoreFile *cf)
Definition: cfile.c:1573
RZ_API bool rz_core_file_resize(RzCore *core, ut64 newsize)
Definition: cfile.c:518
RZ_API bool rz_core_file_bin_raise(RzCore *core, ut32 bfid)
Definition: cfile.c:1451
RZ_API void rz_core_file_reopen_remote_debug(RzCore *core, const char *uri, ut64 addr)
Definition: cfile.c:222
static void __rebase_everything(RzCore *core, RzList *old_sections, ut64 old_base)
Definition: cfile.c:123
RZ_API bool rz_core_raw_file_print(RzCore *core)
Definition: cfile.c:1339
static bool core_file_do_load_for_io_plugin(RzCore *r, ut64 baseaddr, ut64 loadaddr)
Definition: cfile.c:714
RZ_API int rz_core_bin_rebase(RzCore *core, ut64 baddr)
Definition: cfile.c:822
RZ_IPI void rz_core_io_map_info_free(RzCoreIOMapInfo *info)
Definition: cfile.c:1679
static bool file_resize(RzCore *core, ut64 newsize, st64 delta)
Definition: cfile.c:488
static void load_scripts_for(RzCore *core, const char *name)
Definition: cfile.c:836
RZ_API void rz_core_io_file_reopen(RZ_NONNULL RzCore *core, int fd, int perms)
Reopen file.
Definition: cfile.c:1650
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_IPI void rz_core_file_io_map_deleted(RzCore *core, RzIOMap *map)
Definition: cfile.c:1280
RZ_API void rz_core_sysenv_end(RzCore *core)
Definition: cfile.c:526
RZ_API RzCoreFile * rz_core_file_get_by_fd(RzCore *core, int fd)
Definition: cfile.c:1328
RZ_API RzCoreFile * rz_core_file_find_by_name(RzCore *core, const char *name)
Definition: cfile.c:1540
static bool __rebase_flags(RzFlagItem *flag, void *user)
Definition: cfile.c:93
RZ_IPI void rz_core_file_bin_file_deleted(RzCore *core, RzBinFile *bf)
Definition: cfile.c:1290
static bool try_loadlib(RzCore *core, const char *lib, ut64 addr)
Definition: cfile.c:769
static bool resolve_import_cb(RzCoreLinkData *ld, RzIODesc *desc, ut32 id)
Definition: cfile.c:885
static bool map_multi_dex(RzCore *core, RzIODesc *desc, ut32 id)
Definition: cfile.c:903
static bool __rebase_xrefs_i(void *user, const ut64 k, const void *v)
Definition: cfile.c:108
RZ_API int rz_core_file_binlist(RzCore *core)
Definition: cfile.c:1465
RZ_API int rz_core_file_set_by_name(RzCore *core, const char *name)
Definition: cfile.c:1568
RZ_API bool rz_core_file_close_fd(RzCore *core, int fd)
Definition: cfile.c:1510
static bool file_is_loaded(RzCore *core, const char *lib)
Definition: cfile.c:873
static RZ_OWN RzList * __save_old_sections(RzCore *core)
Definition: cfile.c:56
RZ_API bool rz_core_file_resize_delta(RzCore *core, st64 delta)
Definition: cfile.c:522
RZ_IPI void rz_core_file_free(RzCoreFile *cf)
Definition: cfile.c:26
static bool __rebase_xrefs(void *user, const ut64 k, const void *v)
Definition: cfile.c:117
RZ_API bool rz_core_file_open_load(RZ_NONNULL RzCore *core, RZ_NONNULL const char *filepath, ut64 addr, int perms, bool write_mode)
Tries to open the file, load binary info and make RzIOMap.
Definition: cfile.c:189
RZ_API bool rz_core_bin_load(RZ_NONNULL RzCore *r, RZ_NULLABLE const char *filenameuri, ut64 baddr)
Definition: cfile.c:942
static bool close_but_cb(void *user, void *data, ut32 id)
Definition: cfile.c:1492
RZ_API int rz_core_file_set_by_fd(RzCore *core, ut64 fd)
Definition: cfile.c:1559
RZ_API int rz_core_setup_debugger(RzCore *r, const char *debugbackend, bool attach)
Definition: cio.c:7
RZ_API int rz_core_block_read(RzCore *core)
Definition: cio.c:243
RZ_API int rz_core_cmd0(RzCore *core, const char *cmd)
Definition: cmd.c:5428
RZ_API int rz_core_cmd(RzCore *core, const char *cstr, int log)
Definition: cmd.c:5328
RZ_API bool rz_core_run_script(RzCore *core, RZ_NONNULL const char *file)
Definition: cmd.c:457
static char * ofile
Definition: cmd_magic.c:9
RZ_IPI bool rz_core_seek_to_register(RzCore *core, const char *regname, bool is_silent)
Definition: cmd_seek.c:22
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 RzConfigNode * rz_config_set(RzConfig *cfg, RZ_NONNULL const char *name, const char *value)
Definition: config.c:267
RZ_API RzConfigNode * rz_config_set_i(RzConfig *cfg, RZ_NONNULL const char *name, const ut64 i)
Definition: config.c:419
RZ_API RZ_BORROW const char * rz_config_get(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:75
RZ_API RzConfigNode * rz_config_set_b(RzConfig *cfg, RZ_NONNULL const char *name, bool value)
Definition: config.c:201
RZ_API void rz_config_serialize(RZ_NONNULL RzConfig *config, RZ_NONNULL Sdb *db)
Definition: config.c:547
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
RZ_API bool rz_cons_is_breaked(void)
Definition: cons.c:373
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
#define RZ_API
#define RZ_CORE_BASE_ADDRESS_DEFAULT
#define rz_core_align_base_address(x)
#define RZ_CORE_BASE_ADDRESS_DEX
RZ_API void rz_core_reg_update_flags(RzCore *core)
Update or create flags for all registers where it makes sense.
Definition: creg.c:106
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
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
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 count
Definition: sflib.h:98
uint16_t ut16
uint32_t ut32
RZ_API bool rz_debug_map_sync(RzDebug *dbg)
Definition: dmap.c:33
RZ_API bool rz_debug_reg_profile_sync(RzDebug *dbg)
Definition: dreg.c:116
const char * k
Definition: dsignal.c:11
const char * v
Definition: dsignal.c:12
size_t map(int syms, int left, int len)
Definition: enough.c:237
checking print the parsed form of the magic use in n conjunction with m to debug a new magic file n before installing it n output MIME type special files
Definition: file_opts.h:46
RZ_API RzFlagItem * rz_flag_get(RzFlag *f, const char *name)
Definition: flag.c:310
RZ_API RzFlagItem * rz_flag_set(RzFlag *f, const char *name, ut64 off, ut32 size)
Definition: flag.c:521
RZ_API void rz_flag_foreach(RzFlag *f, RzFlagItemCb cb, void *user)
Definition: flag.c:800
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
return memset(p, 0, total)
RZ_API bool rz_core_serve(RzCore *core, RzIODesc *file)
Definition: core.c:2904
RZ_API int rz_debug_kill(RzDebug *dbg, int pid, int tid, int sig)
Definition: debug.c:1598
RZ_API void rz_debug_bp_rebase(RzDebug *dbg, ut64 old_base, ut64 new_base)
Definition: debug.c:1737
RZ_API int rz_debug_detach(RzDebug *dbg, int pid)
Definition: debug.c:583
RZ_API int rz_debug_continue(RzDebug *dbg)
Definition: debug.c:1332
RZ_API ut64 rz_debug_get_baddr(RzDebug *dbg, const char *file)
Definition: debug.c:1682
RZ_API bool rz_debug_is_dead(RzDebug *dbg)
Definition: debug.c:1632
RZ_API bool rz_debug_select(RzDebug *dbg, int pid, int tid)
Definition: debug.c:595
RZ_API RZ_BORROW RzListIter * rz_list_find_ptr(RZ_NONNULL const RzList *list, RZ_NONNULL const void *ptr)
Returns the RzListIter of the given pointer, if found.
Definition: list.c:600
RZ_API void rz_list_delete(RZ_NONNULL RzList *list, RZ_NONNULL RzListIter *iter)
Removes an entry in the list by using the RzListIter pointer.
Definition: list.c:162
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API RZ_BORROW void * rz_list_first(RZ_NONNULL const RzList *list)
Returns the first element of the list.
Definition: list.c:77
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_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c: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")
RZ_API void rz_meta_rebase(RzAnalysis *analysis, ut64 diff)
Definition: meta.c:251
int args
Definition: mipsasm.c:18
RZ_API bool rz_debug_use(RzDebug *dbg, const char *str)
Definition: plugin.c:17
RZ_API ut64 rz_reg_getv(RzReg *reg, const char *name)
Definition: reg.c:332
static RzSocket * s
Definition: rtr.c:28
@ RZ_ANALYSIS_XREF_TYPE_NULL
Definition: rz_analysis.h:899
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
@ RZ_BIN_TYPE_CORE
Definition: rz_bin.h:182
@ RZ_DBG_RECOIL_NONE
Definition: rz_debug.h:81
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
RZ_API char * rz_file_abspath(const char *file)
Definition: file.c:267
RZ_API int rz_file_mkstemp(RZ_NULLABLE const char *prefix, char **oname)
Definition: file.c:1058
RZ_API bool rz_file_rm(const char *file)
Definition: file.c:865
RZ_API RZ_OWN char * rz_file_path_join(RZ_NONNULL const char *s1, RZ_NULLABLE const char *s2)
Concatenate two paths to create a new one with s1+s2 with the correct path separator.
Definition: file.c:1312
bool(* RzIDStorageForeachCb)(void *user, void *data, ut32 id)
Definition: rz_idpool.h:34
RZ_API bool rz_id_storage_foreach(RzIDStorage *storage, RzIDStorageForeachCb cb, void *user)
Definition: idpool.c:254
RZ_API ut64 rz_io_fd_size(RzIO *io, int fd)
Definition: io_fd.c:42
RZ_API void rz_io_desc_free(RzIODesc *desc)
Definition: io_desc.c:35
RZ_API int rz_io_fd_get_tid(RzIO *io, int fd)
Definition: io_fd.c:100
RZ_API RzList * rz_io_open_many(RzIO *io, const char *uri, int flags, int mode)
Definition: io.c:201
RZ_API bool rz_io_map_del(RzIO *io, ut32 id)
Definition: io_map.c:192
RZ_API bool rz_io_shift(RzIO *io, ut64 start, ut64 end, st64 move)
Definition: io.c:596
RZ_API bool rz_io_resize(RzIO *io, ut64 newsize)
Definition: io.c:418
RZ_API bool rz_io_desc_add(RzIO *io, RzIODesc *desc)
Definition: io_desc.c:49
RZ_API ut64 rz_io_desc_size(RzIODesc *desc)
Definition: io_desc.c:224
RZ_API RzIODesc * rz_io_desc_get(RzIO *io, int fd)
Definition: io_desc.c:73
RZ_API RzList * rz_io_map_get_for_fd(RzIO *io, int fd)
Definition: io_map.c:388
RZ_API void rz_io_bind(RzIO *io, RzIOBind *bnd)
Definition: io.c:550
RZ_API bool rz_io_is_listener(RzIO *io)
Definition: io.c:404
RZ_API int rz_io_close_all(RzIO *io)
Definition: io.c:258
RZ_API RZ_BORROW RzPVector * rz_io_maps(RzIO *io)
Returns the pointer to vector containing maps list.
Definition: io_map.c:435
RZ_API bool rz_io_reopen(RzIO *io, int fd, int flags, int mode)
Definition: io.c:231
RZ_API RzIODesc * rz_io_open_nomap(RzIO *io, const char *uri, int flags, int mode)
Definition: io.c:145
RZ_API int rz_io_fd_get_pid(RzIO *io, int fd)
Definition: io_fd.c:92
RZ_API bool rz_io_use_fd(RzIO *io, int fd)
Definition: io_fd.c:118
RZ_API RzIOMap * rz_io_map_new(RzIO *io, int fd, int flags, ut64 delta, ut64 addr, ut64 size)
Definition: io_map.c:50
RZ_API bool rz_io_desc_is_dbg(RzIODesc *desc)
Definition: io_desc.c:303
RZ_API ut64 rz_io_map_location(RzIO *io, ut64 size)
Returns a memory location that can hold enough bytes without overlapping.
Definition: io_map.c:425
RZ_API bool rz_io_fd_close(RzIO *io, int fd)
Definition: io_fd.c:11
RZ_API bool rz_io_desc_close(RzIODesc *desc)
Definition: io_desc.c:165
RZ_API bool rz_io_desc_get_base(RzIODesc *desc, ut64 *base)
Definition: io_desc.c:344
#define RZ_LOG_INFO(fmtstr,...)
Definition: rz_log.h:54
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API bool rz_name_filter(char *name, int len, bool strict)
Definition: name.c:43
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
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_libdir(void)
Return the directory where the Rizin libraries are placed.
Definition: path.c:155
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API PJ * pj_kb(PJ *j, const char *k, bool v)
Definition: pj.c:177
RZ_API PJ * pj_ki(PJ *j, const char *k, int d)
Definition: pj.c:149
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_kn(PJ *j, const char *k, ut64 n)
Definition: pj.c:121
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API char * rz_str_ndup(RZ_NULLABLE const char *ptr, int len)
Create new copy of string ptr limited to size len.
Definition: str.c:1006
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API char * rz_str_path_escape(const char *path)
Definition: str.c:2463
RZ_API bool rz_str_startswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string starts with a specifc sequence of characters (case sensitive)
Definition: str.c:3286
RZ_API bool rz_str_endswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string ends with a specifc sequence of characters (case sensitive)
Definition: str.c:3329
RZ_API char * rz_str_arg_escape(const char *arg)
Definition: str.c:2413
#define rz_strf(buf,...)
Convenience macro for local temporary strings.
Definition: rz_str.h:59
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 RzList * rz_sys_dir(const char *path)
Definition: sys.c:216
RZ_API ut64 rz_time_now_mono(void)
Returns the current time in microseconds, using the monotonic clock.
Definition: time.c:102
#define RZ_SYS_DIR
Definition: rz_types.h:218
#define PFMT64d
Definition: rz_types.h:394
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_NEW(x)
Definition: rz_types.h:285
#define RZ_PERM_W
Definition: rz_types.h:94
#define RZ_PERM_X
Definition: rz_types.h:95
#define RZ_PERM_WX
Definition: rz_types.h:99
#define PFMT64u
Definition: rz_types.h:395
RzOutputMode
Enum to describe the way data are printed.
Definition: rz_types.h:38
@ RZ_OUTPUT_MODE_JSON
Definition: rz_types.h:40
@ RZ_OUTPUT_MODE_RIZIN
Definition: rz_types.h:41
#define PFMT64x
Definition: rz_types.h:393
#define RZ_BORROW
Definition: rz_types.h:63
#define st64
Definition: rz_types_base.h:10
#define UT32_MAX
Definition: rz_types_base.h:99
#define UT64_MAX
Definition: rz_types_base.h:86
#define RZ_BINRC
Definition: rz_userconf.h:90
static void * rz_pvector_tail(RzPVector *vec)
Definition: rz_vector.h:269
RZ_API void rz_pvector_remove_data(RzPVector *vec, void *x)
Definition: vector.c:362
RZ_API void rz_pvector_init(RzPVector *vec, RzPVectorFree free)
Definition: vector.c:298
RZ_API void rz_pvector_fini(RzPVector *vec)
Definition: vector.c:331
static size_t rz_pvector_len(const RzPVector *vec)
Definition: rz_vector.h:231
#define rz_pvector_foreach_prev(vec, it)
Definition: rz_vector.h:338
static bool rz_pvector_empty(RzPVector *vec)
Definition: rz_vector.h:246
static void ** rz_pvector_push(RzPVector *vec, void *x)
Definition: rz_vector.h:300
static void * rz_pvector_at(const RzPVector *vec, size_t index)
Definition: rz_vector.h:236
#define rz_pvector_foreach(vec, it)
Definition: rz_vector.h:334
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
RZ_API bool sdb_sync(Sdb *s)
Definition: sdb.c:803
RZ_API bool rz_core_seek_opt(RzCore *core, ut64 addr, bool rb, bool save)
Seek to addr and optionally save the current offset in seek history.
Definition: seek.c:141
RZ_API bool rz_core_seek(RzCore *core, ut64 addr, bool rb)
Seek to addr.
Definition: seek.c:116
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119
#define b(i)
Definition: sha256.c:42
#define f(i)
Definition: sha256.c:46
#define a(i)
Definition: sha256.c:41
bool found
Definition: cfile.c:861
const char * name
Definition: cfile.c:860
ut64 addr
Definition: cfile.c:881
RzBin * bin
Definition: cfile.c:882
const char * name
Definition: cfile.c:880
RzCore * core
Definition: cfile.c:83
RzList * old_sections
Definition: cfile.c:84
ut64 diff
Definition: cfile.c:86
ut64 old_base
Definition: cfile.c:85
Definition: malloc.c:26
Definition: gzappend.c:170
int fd
Definition: gzappend.c:171
Definition: z80asm.h:102
Definition: rz_pj.h:12
RzAnalysisXRefType type
Definition: rz_analysis.h:909
RzList * fcns
Definition: rz_analysis.h:565
HtUP * ht_xrefs_to
Definition: rz_analysis.h:592
HtUP * ht_xrefs_from
Definition: rz_analysis.h:591
RzIOBind iob
Definition: rz_analysis.h:574
int big_endian
Definition: rz_asm.h:101
int bits
Definition: rz_asm.h:100
ut64 loadaddr
starting physical address to read from the target file
Definition: rz_bin.h:249
ut64 baseaddr
where the linker maps the binary in memory
Definition: rz_bin.h:248
XX curplugin == o->plugin.
Definition: rz_bin.h:298
ut64 loadaddr
Definition: rz_bin.h:307
RzBinObject * o
Definition: rz_bin.h:305
int fd
when used in combination with RzIO, this refers to the io fd.
Definition: rz_bin.h:300
char * file
Definition: rz_bin.h:299
char * name
Definition: rz_bin.h:701
int has_va
Definition: rz_bin.h:228
char * arch
Definition: rz_bin.h:214
RzBinObjectLoadOptions opts
Definition: rz_bin.h:260
RzList * maps
Definition: rz_bin.h:266
RzBinInfo * info
Definition: rz_bin.h:287
char * regstate
Definition: rz_bin.h:286
st64 baddr_shift
Definition: rz_bin.h:261
int(* file_type)(RzBinFile *bf)
Definition: rz_bin.h:554
char * name
Definition: rz_bin.h:509
char strfilter
Definition: rz_bin.h:557
RzBinInfo *(* info)(RzBinFile *bf)
Definition: rz_bin.h:532
char * format
Definition: rz_bin.h:630
char * name
Definition: rz_bin.h:619
char * name
Definition: rz_bin.h:675
RZ_DEPRECATE RzBinFile * cur
never use this in new code! Get a file from the binfiles list or track it yourself.
Definition: rz_bin.h:330
RzList * binfiles
Definition: rz_bin.h:344
char * data
Definition: rz_bp.h:59
int internal
Definition: rz_bp.h:52
RzPVector binfiles
all bin files that have been created for this core file
Definition: rz_core.h:155
RzPVector extra_files
additional files opened during mapping, for example for zeroed maps
Definition: rz_core.h:156
RzPVector maps
all maps that have been created as a result of loading this file
Definition: rz_core.h:157
RzList * files
Definition: rz_core.h:315
RzBin * bin
Definition: rz_core.h:298
ut64 offset
Definition: rz_core.h:301
RzAsm * rasm
Definition: rz_core.h:323
RzAnalysis * analysis
Definition: rz_core.h:322
RzDebug * dbg
Definition: rz_core.h:329
RzIO * io
Definition: rz_core.h:313
RzNum * num
Definition: rz_core.h:316
RzFlag * flags
Definition: rz_core.h:330
RzCoreFile * file
Definition: rz_core.h:314
ut32 blocksize
Definition: rz_core.h:303
RzConfig * config
Definition: rz_core.h:300
RzDebugRecoilMode recoil_mode
Definition: rz_debug.h:277
bool main_arena_resolved
Definition: rz_debug.h:320
RzList * maps
Definition: rz_debug.h:306
RzDebugReason reason
Definition: rz_debug.h:276
int main_pid
Definition: rz_debug.h:246
ut64 offset
Definition: rz_flag.h:38
char * name
Definition: rz_flag.h:35
ut64 addr
Definition: rz_itv.h:15
int fd
Definition: rz_io.h:96
char * uri
Definition: rz_io.h:98
char * referer
Definition: rz_io.h:100
RzInterval itv
Definition: rz_io.h:149
RzIDStorage * files
Definition: rz_io.h:75
struct rz_io_desc_t * desc
Definition: rz_io.h:60
RzListFree free
Definition: rz_list.h:21
ut64 value
Definition: rz_num.h:63
Definition: sdb.h:63
RZ_API bool rz_analysis_function_rebase_vars(RzAnalysis *a, RzAnalysisFunction *fcn)
Definition: var.c:44
static st64 delta
Definition: vmenus.c:2425
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
RZ_API bool rz_analysis_xrefs_init(RzAnalysis *analysis)
Definition: xrefs.c:245
RZ_API bool rz_analysis_xrefs_set(RzAnalysis *analysis, ut64 from, ut64 to, RzAnalysisXRefType type)
Definition: xrefs.c:117
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int baseaddr
Definition: z80asm.c:79
static int file
Definition: z80asm.c:58
static int addr
Definition: z80asm.c:58
int inf(FILE *source, FILE *dest)
Definition: zpipe.c:92