2020-12-20 23:37:45 +01:00
|
|
|
/* radare - LGPL - Copyright 2009-2020 - pancake */
|
2009-02-05 22:08:46 +01:00
|
|
|
|
|
|
|
#include <r_core.h>
|
2014-01-25 18:06:17 -06:00
|
|
|
#include <stdlib.h>
|
2015-10-14 23:50:16 +02:00
|
|
|
#include <string.h>
|
2009-02-05 22:08:46 +01:00
|
|
|
|
2020-08-12 18:54:49 +02:00
|
|
|
#define UPDATE_TIME(a) (r->times->file_open_time = r_time_now_mono () - (a))
|
2016-03-14 22:12:54 +02:00
|
|
|
|
2017-02-20 02:54:16 +01:00
|
|
|
static int r_core_file_do_load_for_debug(RCore *r, ut64 loadaddr, const char *filenameuri);
|
|
|
|
static int r_core_file_do_load_for_io_plugin(RCore *r, ut64 baseaddr, ut64 loadaddr);
|
2014-04-23 18:04:25 -05:00
|
|
|
|
2019-10-10 12:35:31 +02:00
|
|
|
static bool __isMips (RAsm *a) {
|
|
|
|
return a && a->cur && a->cur->arch && strstr (a->cur->arch, "mips");
|
|
|
|
}
|
|
|
|
|
2019-04-14 15:50:27 +02:00
|
|
|
static void loadGP(RCore *core) {
|
2020-05-13 01:28:23 +02:00
|
|
|
if (__isMips (core->rasm)) {
|
2019-04-14 15:50:27 +02:00
|
|
|
ut64 gp = r_num_math (core->num, "loc._gp");
|
|
|
|
if (!gp || gp == UT64_MAX) {
|
2019-04-14 21:04:56 +02:00
|
|
|
r_config_set (core->config, "anal.roregs", "zero");
|
2019-04-14 15:50:27 +02:00
|
|
|
r_core_cmd0 (core, "10aes@entry0");
|
2019-04-14 21:04:56 +02:00
|
|
|
r_config_set (core->config, "anal.roregs", "zero,gp");
|
2019-04-14 15:50:27 +02:00
|
|
|
gp = r_reg_getv (core->anal->reg, "gp");
|
|
|
|
}
|
2019-04-14 21:04:56 +02:00
|
|
|
// eprintf ("[mips] gp: 0x%08"PFMT64x"\n", gp);
|
2019-04-14 15:50:27 +02:00
|
|
|
r_config_set_i (core->config, "anal.gp", gp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 00:36:00 +02:00
|
|
|
R_API int r_core_file_reopen(RCore *core, const char *args, int perm, int loadbin) {
|
2014-08-18 18:06:27 +02:00
|
|
|
int isdebug = r_config_get_i (core->config, "cfg.debug");
|
2011-12-05 02:42:06 +01:00
|
|
|
char *path;
|
2017-08-25 23:19:59 +02:00
|
|
|
ut64 laddr = r_config_get_i (core->config, "bin.laddr");
|
2014-08-18 18:06:27 +02:00
|
|
|
RCoreFile *file = NULL;
|
|
|
|
RCoreFile *ofile = core->file;
|
2020-12-20 23:37:45 +01:00
|
|
|
RBinFile *bf = ofile ? r_bin_file_find_by_fd (core->bin, ofile->fd) : NULL;
|
2017-11-22 12:59:09 +01:00
|
|
|
RIODesc *odesc = (core->io && ofile) ? r_io_desc_get (core->io, ofile->fd) : NULL;
|
2018-02-18 03:08:17 +01:00
|
|
|
char *ofilepath = NULL, *obinfilepath = (bf && bf->file)? strdup (bf->file): NULL;
|
2018-01-10 13:45:37 +01:00
|
|
|
int ret = false;
|
2014-10-17 00:04:52 +02:00
|
|
|
ut64 origoff = core->offset;
|
2014-11-03 10:47:02 +01:00
|
|
|
if (odesc) {
|
|
|
|
if (odesc->referer) {
|
|
|
|
ofilepath = odesc->referer;
|
|
|
|
} else if (odesc->uri) {
|
|
|
|
ofilepath = odesc->uri;
|
|
|
|
}
|
|
|
|
}
|
2014-11-03 11:47:51 +01:00
|
|
|
|
2019-04-15 11:50:34 +02:00
|
|
|
ut64 new_baddr = UT64_MAX;
|
|
|
|
if (args) {
|
|
|
|
new_baddr = r_num_math (core->num, args);
|
|
|
|
if (new_baddr && new_baddr != UT64_MAX) {
|
|
|
|
r_config_set_i (core->config, "bin.baddr", new_baddr);
|
|
|
|
} else {
|
|
|
|
new_baddr = UT64_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (new_baddr == UT64_MAX) {
|
|
|
|
new_baddr = r_config_get_i (core->config, "bin.baddr");
|
|
|
|
}
|
|
|
|
|
2012-10-22 10:43:10 +02:00
|
|
|
if (r_sandbox_enable (0)) {
|
|
|
|
eprintf ("Cannot reopen in sandbox\n");
|
2015-02-11 21:50:37 +01:00
|
|
|
free (obinfilepath);
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2012-10-22 10:43:10 +02:00
|
|
|
}
|
2011-12-05 02:42:06 +01:00
|
|
|
if (!core->file) {
|
|
|
|
eprintf ("No file opened to reopen\n");
|
Fixed some issues in bin/dwarf.c and Fixed 1205194, 1205193, 1205192, 1205202, 1205203, 1205204, 1205205, 1205209, 1205208, 1205207, 1205206
2014-04-25 15:14:49 -05:00
|
|
|
free (ofilepath);
|
|
|
|
free (obinfilepath);
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2011-12-05 02:42:06 +01:00
|
|
|
}
|
2018-01-10 13:45:37 +01:00
|
|
|
int newpid = odesc? odesc->fd: -1;
|
2014-04-23 18:04:25 -05:00
|
|
|
|
2014-08-18 18:06:27 +02:00
|
|
|
if (isdebug) {
|
2020-03-05 14:42:32 +01:00
|
|
|
r_debug_kill (core->dbg, core->dbg->pid, core->dbg->tid, 9); // SIGKILL
|
2020-06-29 23:48:08 -03:00
|
|
|
do {
|
|
|
|
r_debug_continue (core->dbg);
|
|
|
|
} while (!r_debug_is_dead (core->dbg));
|
2020-03-14 16:24:05 +00:00
|
|
|
r_debug_detach (core->dbg, core->dbg->pid);
|
2014-10-17 00:04:52 +02:00
|
|
|
perm = 7;
|
|
|
|
} else {
|
|
|
|
if (!perm) {
|
2018-09-21 02:16:54 +02:00
|
|
|
perm = 4; //R_PERM_R;
|
2014-10-17 00:04:52 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-03 11:47:51 +01:00
|
|
|
if (!ofilepath) {
|
2014-10-17 00:04:52 +02:00
|
|
|
eprintf ("Unknown file path");
|
2015-01-27 16:03:04 +01:00
|
|
|
free (obinfilepath);
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
2014-02-21 10:58:31 +01:00
|
|
|
|
|
|
|
// HACK: move last mapped address to higher place
|
2014-04-23 18:04:25 -05:00
|
|
|
// XXX - why does this hack work?
|
2014-04-22 23:02:46 -05:00
|
|
|
// when the new memory maps are created.
|
2014-11-03 11:47:51 +01:00
|
|
|
path = strdup (ofilepath);
|
2015-04-19 02:38:41 +03:00
|
|
|
free (obinfilepath);
|
2016-06-06 22:40:34 +02:00
|
|
|
obinfilepath = strdup (ofilepath);
|
2014-11-03 11:47:51 +01:00
|
|
|
|
2018-01-08 03:22:26 +01:00
|
|
|
// r_str_trim (path);
|
2015-08-27 23:00:38 +02:00
|
|
|
file = r_core_file_open (core, path, perm, laddr);
|
2019-09-26 06:24:33 -03:00
|
|
|
if (isdebug) {
|
|
|
|
int newtid = newpid;
|
|
|
|
// XXX - select the right backend
|
|
|
|
if (core->file) {
|
|
|
|
newpid = r_io_fd_get_pid (core->io, core->file->fd);
|
|
|
|
newtid = r_io_fd_get_tid (core->io, core->file->fd);
|
|
|
|
#if __linux__
|
|
|
|
core->dbg->main_pid = newpid;
|
|
|
|
newtid = newpid;
|
|
|
|
#endif
|
|
|
|
}
|
2019-12-26 01:01:45 +02:00
|
|
|
// Reset previous pid and tid
|
|
|
|
core->dbg->pid = -1;
|
|
|
|
core->dbg->tid = -1;
|
2020-07-09 11:29:13 +03:00
|
|
|
core->dbg->recoil_mode = R_DBG_RECOIL_NONE;
|
|
|
|
memset (&core->dbg->reason, 0, sizeof (core->dbg->reason));
|
2019-12-26 01:01:45 +02:00
|
|
|
// Reopen and attach
|
2019-09-26 06:24:33 -03:00
|
|
|
r_core_setup_debugger (core, "native", true);
|
|
|
|
r_debug_select (core->dbg, newpid, newtid);
|
|
|
|
}
|
|
|
|
|
2011-12-05 02:42:06 +01:00
|
|
|
if (file) {
|
2016-06-07 10:03:04 +02:00
|
|
|
bool had_rbin_info = false;
|
2015-08-27 23:00:38 +02:00
|
|
|
|
2019-10-24 14:33:54 +02:00
|
|
|
if (ofile && bf) {
|
|
|
|
if (r_bin_file_delete (core->bin, bf->id)) {
|
2016-06-07 10:03:04 +02:00
|
|
|
had_rbin_info = true;
|
|
|
|
}
|
2014-10-17 00:36:00 +02:00
|
|
|
}
|
2014-04-23 18:04:25 -05:00
|
|
|
r_core_file_close (core, ofile);
|
2014-04-25 14:21:20 -05:00
|
|
|
r_core_file_set_by_file (core, file);
|
2014-04-23 18:04:25 -05:00
|
|
|
ofile = NULL;
|
|
|
|
odesc = NULL;
|
2017-02-20 02:54:16 +01:00
|
|
|
// core->file = file;
|
2012-10-22 10:12:13 +02:00
|
|
|
eprintf ("File %s reopened in %s mode\n", path,
|
2018-09-21 02:16:54 +02:00
|
|
|
(perm & R_PERM_W)? "read-write": "read-only");
|
2014-04-23 18:04:25 -05:00
|
|
|
|
2015-08-27 23:00:38 +02:00
|
|
|
if (loadbin && (loadbin == 2 || had_rbin_info)) {
|
2019-09-26 06:24:33 -03:00
|
|
|
ut64 baddr;
|
|
|
|
if (isdebug) {
|
|
|
|
baddr = r_debug_get_baddr (core->dbg, path);
|
|
|
|
} else if (new_baddr != UT64_MAX) {
|
2019-04-15 11:50:34 +02:00
|
|
|
baddr = new_baddr;
|
2019-09-26 06:24:33 -03:00
|
|
|
} else {
|
|
|
|
baddr = r_config_get_i (core->config, "bin.baddr");
|
2019-04-15 11:50:34 +02:00
|
|
|
}
|
2014-10-17 00:36:00 +02:00
|
|
|
ret = r_core_bin_load (core, obinfilepath, baddr);
|
2017-05-03 17:19:49 +02:00
|
|
|
r_core_bin_update_arch_bits (core);
|
2014-10-17 00:36:00 +02:00
|
|
|
if (!ret) {
|
|
|
|
eprintf ("Error: Failed to reload rbin for: %s", path);
|
|
|
|
}
|
2019-04-15 11:50:34 +02:00
|
|
|
origoff = r_num_math (core->num, "entry0");
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
|
|
|
|
2017-08-22 07:42:16 +00:00
|
|
|
if (core->bin->cur && core->io && r_io_desc_get (core->io, file->fd) && !loadbin) {
|
2017-02-20 02:54:16 +01:00
|
|
|
//force here NULL because is causing uaf look this better in future XXX @alvarofe
|
2016-06-06 22:40:34 +02:00
|
|
|
core->bin->cur = NULL;
|
|
|
|
}
|
2012-10-22 10:12:13 +02:00
|
|
|
// close old file
|
2014-08-18 18:06:27 +02:00
|
|
|
} else if (ofile) {
|
2018-11-15 19:22:54 +08:00
|
|
|
eprintf ("r_core_file_reopen: Cannot reopen file: %s with perms 0x%x,"
|
2017-02-20 02:54:16 +01:00
|
|
|
" attempting to open read-only.\n", path, perm);
|
2014-02-21 10:58:31 +01:00
|
|
|
// lower it down back
|
2018-09-21 02:16:54 +02:00
|
|
|
//ofile = r_core_file_open (core, path, R_PERM_R, addr);
|
2014-04-25 14:21:20 -05:00
|
|
|
r_core_file_set_by_file (core, ofile);
|
2014-10-17 00:04:52 +02:00
|
|
|
} else {
|
|
|
|
eprintf ("Cannot reopen\n");
|
2011-12-05 02:42:06 +01:00
|
|
|
}
|
2014-04-25 14:21:20 -05:00
|
|
|
if (core->file) {
|
2017-08-23 00:04:47 +00:00
|
|
|
r_io_use_fd (core->io, core->file->fd);
|
|
|
|
core->switch_file_view = 1;
|
|
|
|
r_core_block_read (core);
|
2017-08-22 07:42:16 +00:00
|
|
|
#if 0
|
|
|
|
else {
|
2017-02-20 02:54:16 +01:00
|
|
|
const char *name = (cf && cf->desc)? cf->desc->name: "ERROR";
|
2014-04-27 02:06:50 -05:00
|
|
|
eprintf ("Error: Unable to switch the view to file: %s\n", name);
|
|
|
|
}
|
2017-08-22 07:42:16 +00:00
|
|
|
#endif
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
2020-04-17 12:53:35 +02:00
|
|
|
r_core_seek (core, origoff, true);
|
2014-08-19 10:15:46 +02:00
|
|
|
if (isdebug) {
|
2015-01-29 02:25:00 +01:00
|
|
|
r_core_cmd0 (core, ".dm*");
|
2014-08-19 10:15:46 +02:00
|
|
|
r_core_cmd0 (core, ".dr*");
|
2015-10-31 01:57:52 +01:00
|
|
|
r_core_cmd0 (core, "sr PC");
|
2016-04-13 23:18:36 +02:00
|
|
|
} else {
|
2019-04-14 15:50:27 +02:00
|
|
|
loadGP (core);
|
2014-08-19 10:15:46 +02:00
|
|
|
}
|
2016-02-16 02:42:44 +01:00
|
|
|
// update anal io bind
|
|
|
|
r_io_bind (core->io, &(core->anal->iob));
|
2017-11-22 14:58:09 +01:00
|
|
|
if (core->file && core->file->fd >= 0) {
|
|
|
|
r_core_cmd0 (core, "o-!");
|
|
|
|
}
|
|
|
|
r_core_file_close_all_but (core);
|
2014-05-03 04:26:07 +04:00
|
|
|
// This is done to ensure that the file is correctly
|
2014-04-22 23:02:46 -05:00
|
|
|
// loaded into the view
|
2014-04-23 18:04:25 -05:00
|
|
|
free (obinfilepath);
|
2014-11-03 11:47:51 +01:00
|
|
|
//free (ofilepath);
|
2020-12-20 23:37:45 +01:00
|
|
|
// causes double free . dont free file here // R_FREE (file);
|
2011-12-05 02:42:06 +01:00
|
|
|
free (path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-04 11:30:08 +01:00
|
|
|
R_API void r_core_sysenv_end(RCore *core, const char *cmd) {
|
|
|
|
// TODO: remove tmpfilez
|
2016-06-14 01:33:46 +02:00
|
|
|
if (strstr (cmd, "R2_BLOCK")) {
|
2011-02-04 11:30:08 +01:00
|
|
|
// remove temporary BLOCK file
|
2016-06-14 01:33:46 +02:00
|
|
|
char *f = r_sys_getenv ("R2_BLOCK");
|
2012-02-09 01:38:16 +01:00
|
|
|
if (f) {
|
|
|
|
r_file_rm (f);
|
2016-06-14 01:33:46 +02:00
|
|
|
r_sys_setenv ("R2_BLOCK", NULL);
|
2014-05-03 04:26:07 +04:00
|
|
|
free (f);
|
2012-02-09 01:38:16 +01:00
|
|
|
}
|
2011-02-04 11:30:08 +01:00
|
|
|
}
|
2016-06-14 01:33:46 +02:00
|
|
|
r_sys_setenv ("R2_FILE", NULL);
|
|
|
|
r_sys_setenv ("R2_BYTES", NULL);
|
|
|
|
r_sys_setenv ("R2_OFFSET", NULL);
|
2019-12-16 23:23:17 +01:00
|
|
|
|
|
|
|
// remove temporary R2_CONFIG file
|
|
|
|
char *r2_config = r_sys_getenv ("R2_CONFIG");
|
|
|
|
if (r2_config) {
|
|
|
|
r_file_rm (r2_config);
|
|
|
|
r_sys_setenv ("R2_CONFIG", NULL);
|
|
|
|
free (r2_config);
|
|
|
|
}
|
2011-02-04 11:30:08 +01:00
|
|
|
}
|
|
|
|
|
2010-08-19 20:28:25 +02:00
|
|
|
#if DISCUSS
|
2017-09-26 11:55:48 +02:00
|
|
|
EDITOR r_sys_setenv ("EDITOR", r_config_get (core->config, "cfg.editor"));
|
2017-02-20 02:54:16 +01:00
|
|
|
CURSOR cursor position (offset from curseek)
|
|
|
|
VERBOSE cfg.verbose
|
2010-08-19 20:28:25 +02:00
|
|
|
#endif
|
2017-09-26 11:55:48 +02:00
|
|
|
|
2017-02-20 02:54:16 +01:00
|
|
|
R_API char *r_core_sysenv_begin(RCore * core, const char *cmd) {
|
2017-09-04 03:09:12 +02:00
|
|
|
char *f, *ret = cmd? strdup (cmd): NULL;
|
2017-08-22 07:42:16 +00:00
|
|
|
RIODesc *desc = core->file ? r_io_desc_get (core->io, core->file->fd) : NULL;
|
2017-09-04 03:09:12 +02:00
|
|
|
if (cmd && strstr (cmd, "R2_BYTES")) {
|
2012-02-09 01:38:16 +01:00
|
|
|
char *s = r_hex_bin2strdup (core->block, core->blocksize);
|
2016-03-20 02:24:17 +01:00
|
|
|
r_sys_setenv ("R2_BYTES", s);
|
2012-02-09 01:38:16 +01:00
|
|
|
free (s);
|
2011-02-04 11:30:08 +01:00
|
|
|
}
|
2017-10-30 12:46:33 +01:00
|
|
|
r_sys_setenv ("RABIN2_PDBSERVER", r_config_get (core->config, "pdb.server"));
|
2017-08-22 07:42:16 +00:00
|
|
|
if (desc && desc->name) {
|
|
|
|
r_sys_setenv ("R2_FILE", desc->name);
|
2018-04-10 23:52:47 +02:00
|
|
|
r_sys_setenv ("R2_SIZE", sdb_fmt ("%"PFMT64d, r_io_desc_size (desc)));
|
2017-09-04 03:09:12 +02:00
|
|
|
if (cmd && strstr (cmd, "R2_BLOCK")) {
|
2014-09-26 17:58:17 +02:00
|
|
|
// replace BLOCK in RET string
|
|
|
|
if ((f = r_file_temp ("r2block"))) {
|
2016-03-20 02:24:17 +01:00
|
|
|
if (r_file_dump (f, core->block, core->blocksize, 0)) {
|
|
|
|
r_sys_setenv ("R2_BLOCK", f);
|
|
|
|
}
|
2014-09-26 17:58:17 +02:00
|
|
|
free (f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:52:47 +02:00
|
|
|
r_sys_setenv ("R2_OFFSET", sdb_fmt ("%"PFMT64d, core->offset));
|
|
|
|
r_sys_setenv ("R2_XOFFSET", sdb_fmt ("0x%08"PFMT64x, core->offset));
|
2020-05-13 01:28:23 +02:00
|
|
|
r_sys_setenv ("R2_ENDIAN", core->rasm->big_endian? "big": "little");
|
2018-04-10 23:52:47 +02:00
|
|
|
r_sys_setenv ("R2_BSIZE", sdb_fmt ("%d", core->blocksize));
|
2019-12-16 23:23:17 +01:00
|
|
|
|
|
|
|
// dump current config file so other r2 tools can use the same options
|
|
|
|
char *config_sdb_path = NULL;
|
|
|
|
int config_sdb_fd = r_file_mkstemp (NULL, &config_sdb_path);
|
2020-01-08 12:52:00 +08:00
|
|
|
if (config_sdb_fd >= 0) {
|
|
|
|
close (config_sdb_fd);
|
|
|
|
}
|
2019-12-16 23:23:17 +01:00
|
|
|
|
|
|
|
Sdb *config_sdb = sdb_new (NULL, config_sdb_path, 0);
|
|
|
|
r_config_serialize (core->config, config_sdb);
|
|
|
|
sdb_sync (config_sdb);
|
|
|
|
sdb_free (config_sdb);
|
|
|
|
r_sys_setenv ("R2_CONFIG", config_sdb_path);
|
|
|
|
|
|
|
|
r_sys_setenv ("RABIN2_LANG", r_config_get (core->config, "bin.lang"));
|
|
|
|
r_sys_setenv ("RABIN2_DEMANGLE", r_config_get (core->config, "bin.demangle"));
|
2016-03-20 02:24:17 +01:00
|
|
|
r_sys_setenv ("R2_ARCH", r_config_get (core->config, "asm.arch"));
|
2020-11-10 01:45:29 -05:00
|
|
|
r_sys_setenv ("R2_BITS", sdb_fmt ("%"PFMT64u, r_config_get_i (core->config, "asm.bits")));
|
2016-03-22 01:31:10 +01:00
|
|
|
r_sys_setenv ("R2_COLOR", r_config_get_i (core->config, "scr.color")? "1": "0");
|
2017-02-20 02:54:16 +01:00
|
|
|
r_sys_setenv ("R2_DEBUG", r_config_get_i (core->config, "cfg.debug")? "1": "0");
|
|
|
|
r_sys_setenv ("R2_IOVA", r_config_get_i (core->config, "io.va")? "1": "0");
|
2020-01-08 12:52:00 +08:00
|
|
|
free (config_sdb_path);
|
2011-02-04 11:30:08 +01:00
|
|
|
return ret;
|
2010-08-19 20:28:25 +02:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:39:39 +08:00
|
|
|
#if !__linux__ && !__WINDOWS__
|
2013-12-17 02:10:13 +01:00
|
|
|
static ut64 get_base_from_maps(RCore *core, const char *file) {
|
|
|
|
RDebugMap *map;
|
|
|
|
RListIter *iter;
|
|
|
|
ut64 b = 0LL;
|
2014-01-09 16:57:04 -06:00
|
|
|
|
2013-12-17 02:10:13 +01:00
|
|
|
r_debug_map_sync (core->dbg); // update process memory maps
|
|
|
|
r_list_foreach (core->dbg->maps, iter, map) {
|
2016-03-20 02:24:17 +01:00
|
|
|
if ((map->perm & 5) == 5) {
|
2014-01-11 00:56:02 -06:00
|
|
|
// TODO: make this more flexible
|
|
|
|
// XXX - why "copy/" here?
|
2017-02-20 02:54:16 +01:00
|
|
|
if (map->name && strstr (map->name, "copy/")) {
|
|
|
|
return map->addr;
|
|
|
|
}
|
|
|
|
if (map->file && !strcmp (map->file, file)) {
|
|
|
|
return map->addr;
|
|
|
|
}
|
|
|
|
if (map->name && !strcmp (map->name, file)) {
|
|
|
|
return map->addr;
|
|
|
|
}
|
2014-01-11 00:56:02 -06:00
|
|
|
// XXX - Commented out, as this could unexpected results
|
|
|
|
//b = map->addr;
|
2013-12-17 02:10:13 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-10 00:55:38 +01:00
|
|
|
// fallback resolution copied from cmd_debug.c:r_debug_get_baddr
|
|
|
|
r_list_foreach (core->dbg->maps, iter, map) {
|
|
|
|
if (map->perm == 5) { // r-x
|
|
|
|
return map->addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 02:10:13 +01:00
|
|
|
return b;
|
|
|
|
}
|
2015-05-28 11:09:13 +02:00
|
|
|
#endif
|
2013-12-17 02:10:13 +01:00
|
|
|
|
2018-11-06 23:34:16 +00:00
|
|
|
#if __linux__ || __APPLE__
|
2015-10-22 04:48:56 +02:00
|
|
|
static bool setbpint(RCore *r, const char *mode, const char *sym) {
|
|
|
|
RBreakpointItem *bp;
|
|
|
|
RFlagItem *fi = r_flag_get (r->flags, sym);
|
2017-02-20 02:54:16 +01:00
|
|
|
if (!fi) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-22 04:48:56 +02:00
|
|
|
bp = r_bp_add_sw (r->dbg->bp, fi->offset, 1, R_BP_PROT_EXEC);
|
|
|
|
if (bp) {
|
|
|
|
bp->internal = true;
|
|
|
|
#if __linux__
|
2017-01-24 20:22:36 +01:00
|
|
|
bp->data = r_str_newf ("?e %s: %s", mode, sym);
|
2015-10-22 04:48:56 +02:00
|
|
|
#else
|
|
|
|
bp->data = r_str_newf ("?e %s: %s;ps@rdi", mode, sym);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
eprintf ("Cannot set breakpoint at %s\n", sym);
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-06 23:34:16 +00:00
|
|
|
#endif
|
2015-10-22 04:48:56 +02:00
|
|
|
|
2014-04-27 02:06:50 -05:00
|
|
|
// XXX - need to handle index selection during debugging
|
2017-02-20 02:54:16 +01:00
|
|
|
static int r_core_file_do_load_for_debug(RCore *r, ut64 baseaddr, const char *filenameuri) {
|
2014-04-29 11:10:35 -05:00
|
|
|
RCoreFile *cf = r_core_file_cur (r);
|
2017-08-22 07:42:16 +00:00
|
|
|
RIODesc *desc = cf ? r_io_desc_get (r->io, cf->fd) : NULL;
|
2014-04-27 02:06:50 -05:00
|
|
|
RBinFile *binfile = NULL;
|
2014-05-04 10:03:15 -05:00
|
|
|
RBinPlugin *plugin;
|
2014-04-27 02:06:50 -05:00
|
|
|
int xtr_idx = 0; // if 0, load all if xtr is used
|
2014-04-23 18:04:25 -05:00
|
|
|
|
2018-07-08 02:05:01 +02:00
|
|
|
// TODO : Honor file.path eval var too?
|
2017-02-04 17:35:09 +01:00
|
|
|
if (!strncmp ("dbg://", filenameuri, 6)) {
|
|
|
|
filenameuri += 6;
|
|
|
|
}
|
|
|
|
if (!desc) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-23 00:04:47 +00:00
|
|
|
if (cf) {
|
|
|
|
r_debug_select (r->dbg, r_io_fd_get_pid (r->io, cf->fd),
|
|
|
|
r_io_fd_get_tid (r->io, cf->fd));
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
2015-08-21 01:44:44 +02:00
|
|
|
#if !__linux__
|
2015-11-24 23:28:50 +01:00
|
|
|
#if !__WINDOWS__
|
2014-04-23 18:04:25 -05:00
|
|
|
baseaddr = get_base_from_maps (r, filenameuri);
|
2015-11-24 23:28:50 +01:00
|
|
|
#endif
|
2014-08-25 03:58:22 +02:00
|
|
|
if (baseaddr != UT64_MAX) {
|
2015-08-21 01:44:44 +02:00
|
|
|
r_config_set_i (r->config, "bin.baddr", baseaddr);
|
2014-08-25 03:58:22 +02:00
|
|
|
}
|
2015-05-19 12:05:18 +02:00
|
|
|
#endif
|
2018-11-07 17:22:41 +01:00
|
|
|
int fd = cf? cf->fd: -1;
|
|
|
|
RBinOptions opt;
|
|
|
|
r_bin_options_init (&opt, fd, baseaddr, UT64_MAX, false);
|
|
|
|
opt.xtr_idx = xtr_idx;
|
|
|
|
if (!r_bin_open (r->bin, filenameuri, &opt)) {
|
2017-02-03 20:47:09 +01:00
|
|
|
eprintf ("RBinLoad: Cannot open %s\n", filenameuri);
|
2014-09-08 23:29:01 +02:00
|
|
|
if (r_config_get_i (r->config, "bin.rawstr")) {
|
2018-11-07 17:22:41 +01:00
|
|
|
r_bin_options_init (&opt, fd, baseaddr, UT64_MAX, true);
|
|
|
|
opt.xtr_idx = xtr_idx;
|
|
|
|
if (!r_bin_open (r->bin, filenameuri, &opt)) {
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2014-09-08 23:29:01 +02:00
|
|
|
}
|
2014-05-04 10:03:15 -05:00
|
|
|
}
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
|
|
|
|
2015-10-22 03:11:03 +02:00
|
|
|
if (*r_config_get (r->config, "dbg.libs")) {
|
|
|
|
r_core_cmd0 (r, ".dmm*");
|
2015-10-22 04:48:56 +02:00
|
|
|
#if __linux__
|
2017-01-24 20:22:36 +01:00
|
|
|
setbpint (r, "dbg.libs", "sym.imp.dlopen");
|
|
|
|
setbpint (r, "dbg.libs", "sym.imp.dlmopen");
|
|
|
|
setbpint (r, "dbg.unlibs", "sym.imp.dlclose");
|
2015-10-22 04:48:56 +02:00
|
|
|
#elif __APPLE__
|
2017-01-24 20:22:36 +01:00
|
|
|
setbpint (r, "dbg.libs", "sym._dlopen");
|
|
|
|
setbpint (r, "dbg.libs", "sym._dlclose");
|
2015-10-22 04:48:56 +02:00
|
|
|
#endif
|
2015-10-22 03:11:03 +02:00
|
|
|
}
|
2014-05-04 10:03:15 -05:00
|
|
|
binfile = r_bin_cur (r->bin);
|
|
|
|
r_core_bin_set_env (r, binfile);
|
2014-05-08 18:35:04 -05:00
|
|
|
plugin = r_bin_file_cur_plugin (binfile);
|
2020-12-17 18:38:31 -05:00
|
|
|
if (plugin && !strcmp (plugin->name, "any")) {
|
2014-05-04 10:03:15 -05:00
|
|
|
// set use of raw strings
|
2017-08-23 12:39:10 +02:00
|
|
|
// r_config_set_i (r->config, "io.va", false);
|
2014-09-08 23:29:01 +02:00
|
|
|
//\\ r_config_set (r->config, "bin.rawstr", "true");
|
2014-05-04 10:03:15 -05:00
|
|
|
// get bin.minstr
|
|
|
|
r->bin->minstrlen = r_config_get_i (r->config, "bin.minstr");
|
2015-10-06 20:52:50 -04:00
|
|
|
r->bin->maxstrbuf = r_config_get_i (r->config, "bin.maxstrbuf");
|
2014-05-04 10:03:15 -05:00
|
|
|
} else if (binfile) {
|
2018-11-13 13:05:18 +01:00
|
|
|
RBinObject *obj = r_bin_cur_object (r->bin);
|
2017-02-20 02:54:16 +01:00
|
|
|
RBinInfo *info = obj? obj->info: NULL;
|
2020-12-17 18:38:31 -05:00
|
|
|
if (plugin && info) {
|
2014-05-04 22:08:46 -05:00
|
|
|
r_core_bin_set_arch_bits (r, binfile->file, info->arch, info->bits);
|
2014-05-04 10:03:15 -05:00
|
|
|
}
|
2014-04-27 02:06:50 -05:00
|
|
|
}
|
|
|
|
|
2014-05-04 10:03:15 -05:00
|
|
|
if (plugin && !strcmp (plugin->name, "dex")) {
|
2017-05-03 18:53:26 +02:00
|
|
|
r_core_cmd0 (r, "\"(fix-dex,wx `ph sha1 $s-32 @32` @12 ; wx `ph adler32 $s-12 @12` @8)\"\n");
|
2014-05-04 10:03:15 -05:00
|
|
|
}
|
2018-07-05 12:30:09 +02:00
|
|
|
|
2015-09-14 12:35:38 +02:00
|
|
|
return true;
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
|
|
|
|
2016-12-04 20:41:56 +01:00
|
|
|
static int r_core_file_do_load_for_io_plugin(RCore *r, ut64 baseaddr, ut64 loadaddr) {
|
2014-04-29 11:10:35 -05:00
|
|
|
RCoreFile *cf = r_core_file_cur (r);
|
2017-08-23 00:04:47 +00:00
|
|
|
int fd = cf ? cf->fd : -1;
|
2014-04-23 18:04:25 -05:00
|
|
|
RBinFile *binfile = NULL;
|
2014-04-27 02:06:50 -05:00
|
|
|
int xtr_idx = 0; // if 0, load all if xtr is used
|
2017-02-20 02:54:16 +01:00
|
|
|
RBinPlugin *plugin;
|
2014-04-23 18:04:25 -05:00
|
|
|
|
2017-08-23 00:04:47 +00:00
|
|
|
if (fd < 0) {
|
2016-12-04 20:41:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-23 00:04:47 +00:00
|
|
|
r_io_use_fd (r->io, fd);
|
2018-11-07 17:22:41 +01:00
|
|
|
RBinOptions opt;
|
|
|
|
r_bin_options_init (&opt, fd, baseaddr, loadaddr, r->bin->rawstr);
|
|
|
|
opt.xtr_idx = xtr_idx;
|
|
|
|
if (!r_bin_open_io (r->bin, &opt)) {
|
2014-04-29 11:10:35 -05:00
|
|
|
//eprintf ("Failed to load the bin with an IO Plugin.\n");
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2014-04-27 02:06:50 -05:00
|
|
|
}
|
2014-05-04 10:03:15 -05:00
|
|
|
binfile = r_bin_cur (r->bin);
|
2020-07-16 14:47:30 +02:00
|
|
|
if (r_core_bin_set_env (r, binfile)) {
|
|
|
|
if (!r->anal->sdb_cc->path) {
|
|
|
|
R_LOG_WARN ("No calling convention defined for this file, analysis may be inaccurate.\n");
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 18:35:04 -05:00
|
|
|
plugin = r_bin_file_cur_plugin (binfile);
|
2017-02-20 02:54:16 +01:00
|
|
|
if (plugin && !strcmp (plugin->name, "any")) {
|
2018-11-13 13:05:18 +01:00
|
|
|
RBinObject *obj = r_bin_cur_object (r->bin);
|
2017-02-20 02:54:16 +01:00
|
|
|
RBinInfo *info = obj? obj->info: NULL;
|
2016-12-04 20:41:56 +01:00
|
|
|
if (!info) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-17 18:38:31 -05:00
|
|
|
info->bits = r->rasm->bits;
|
2014-04-23 18:04:25 -05:00
|
|
|
// set use of raw strings
|
2016-11-07 19:33:49 +01:00
|
|
|
r_core_bin_set_arch_bits (r, binfile->file, info->arch, info->bits);
|
2017-08-23 12:39:10 +02:00
|
|
|
// r_config_set_i (r->config, "io.va", false);
|
2014-09-08 23:29:01 +02:00
|
|
|
// r_config_set (r->config, "bin.rawstr", "true");
|
2014-04-23 18:04:25 -05:00
|
|
|
// get bin.minstr
|
|
|
|
r->bin->minstrlen = r_config_get_i (r->config, "bin.minstr");
|
2015-10-06 20:52:50 -04:00
|
|
|
r->bin->maxstrbuf = r_config_get_i (r->config, "bin.maxstrbuf");
|
2014-05-04 10:03:15 -05:00
|
|
|
} else if (binfile) {
|
2018-11-13 13:05:18 +01:00
|
|
|
RBinObject *obj = r_bin_cur_object (r->bin);
|
2017-02-20 02:54:16 +01:00
|
|
|
RBinInfo *info = obj? obj->info: NULL;
|
2016-12-04 20:41:56 +01:00
|
|
|
if (!info) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-17 18:38:31 -05:00
|
|
|
if (plugin) {
|
2014-08-27 23:51:17 +02:00
|
|
|
r_core_bin_set_arch_bits (r, binfile->file,
|
|
|
|
info->arch, info->bits);
|
2014-05-04 10:03:15 -05:00
|
|
|
}
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
|
|
|
|
2014-05-04 10:03:15 -05:00
|
|
|
if (plugin && !strcmp (plugin->name, "dex")) {
|
2017-05-03 18:53:26 +02:00
|
|
|
r_core_cmd0 (r, "\"(fix-dex,wx `ph sha1 $s-32 @32` @12 ; wx `ph adler32 $s-12 @12` @8)\"\n");
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
2015-09-14 12:35:38 +02:00
|
|
|
return true;
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
|
|
|
|
2019-03-16 20:46:11 +01:00
|
|
|
static bool try_loadlib(RCore *core, const char *lib, ut64 addr) {
|
2020-12-20 23:37:45 +01:00
|
|
|
void *p = r_core_file_open (core, lib, 0, addr);
|
|
|
|
if (p) {
|
2019-07-05 18:43:15 +02:00
|
|
|
r_core_bin_load (core, lib, addr);
|
2020-12-20 23:37:45 +01:00
|
|
|
R_FREE (p);
|
2019-07-05 18:43:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-05-09 17:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API bool r_core_file_loadlib(RCore *core, const char *lib, ut64 libaddr) {
|
2019-07-05 18:43:15 +02:00
|
|
|
const char *dirlibs = r_config_get (core->config, "dir.libs");
|
2019-09-22 11:42:49 +03:00
|
|
|
bool free_libdir = true;
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
char *libdir = r_str_r2_prefix (R2_LIBDIR);
|
|
|
|
#else
|
|
|
|
char *libdir = strdup (R2_LIBDIR);
|
|
|
|
#endif
|
|
|
|
if (!libdir) {
|
|
|
|
libdir = R2_LIBDIR;
|
|
|
|
free_libdir = false;
|
|
|
|
}
|
2019-07-05 18:43:15 +02:00
|
|
|
if (!dirlibs || !*dirlibs) {
|
2019-09-22 11:42:49 +03:00
|
|
|
dirlibs = "." R_SYS_DIR;
|
2019-07-05 18:43:15 +02:00
|
|
|
}
|
2016-05-09 17:24:12 +02:00
|
|
|
const char *ldlibrarypath[] = {
|
2019-07-05 18:43:15 +02:00
|
|
|
dirlibs,
|
2019-09-22 11:42:49 +03:00
|
|
|
libdir,
|
|
|
|
#ifndef __WINDOWS__
|
2016-05-09 17:24:12 +02:00
|
|
|
"/usr/local/lib",
|
|
|
|
"/usr/lib",
|
|
|
|
"/lib",
|
2019-09-22 11:42:49 +03:00
|
|
|
#endif
|
|
|
|
"." R_SYS_DIR,
|
2016-05-09 17:24:12 +02:00
|
|
|
NULL
|
|
|
|
};
|
2017-02-20 02:54:16 +01:00
|
|
|
const char * *libpath = (const char * *) &ldlibrarypath;
|
2016-05-09 17:24:12 +02:00
|
|
|
|
2019-09-22 11:42:49 +03:00
|
|
|
bool ret = false;
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
if (strlen (lib) >= 3 && lib[1] == ':' && lib[2] == '\\') {
|
|
|
|
#else
|
2016-05-09 17:24:12 +02:00
|
|
|
if (*lib == '/') {
|
2019-09-22 11:42:49 +03:00
|
|
|
#endif
|
2016-05-09 17:24:12 +02:00
|
|
|
if (try_loadlib (core, lib, libaddr)) {
|
2019-09-22 11:42:49 +03:00
|
|
|
ret = true;
|
2016-05-09 17:24:12 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (*libpath) {
|
2019-09-22 11:42:49 +03:00
|
|
|
char *s = r_str_newf ("%s" R_SYS_DIR "%s", *libpath, lib);
|
2016-05-09 17:24:12 +02:00
|
|
|
if (try_loadlib (core, s, libaddr)) {
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
free (s);
|
|
|
|
if (ret) {
|
2019-09-22 11:42:49 +03:00
|
|
|
break;
|
2016-05-09 17:24:12 +02:00
|
|
|
}
|
|
|
|
libpath++;
|
|
|
|
}
|
|
|
|
}
|
2019-09-22 11:42:49 +03:00
|
|
|
if (free_libdir) {
|
|
|
|
free (libdir);
|
|
|
|
}
|
|
|
|
return ret;
|
2016-05-09 17:24:12 +02:00
|
|
|
}
|
|
|
|
|
2017-03-18 21:40:01 +01:00
|
|
|
R_API int r_core_bin_rebase(RCore *core, ut64 baddr) {
|
|
|
|
if (!core || !core->bin || !core->bin->cur) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (baddr == UT64_MAX) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
RBinFile *bf = core->bin->cur;
|
|
|
|
bf->o->baddr = baddr;
|
|
|
|
bf->o->loadaddr = baddr;
|
|
|
|
r_bin_object_set_items (bf, bf->o);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-06-15 00:36:39 +02:00
|
|
|
static void load_scripts_for(RCore *core, const char *name) {
|
2019-09-22 11:42:49 +03:00
|
|
|
// TODO:
|
2017-06-15 00:36:39 +02:00
|
|
|
char *file;
|
|
|
|
RListIter *iter;
|
2018-05-02 16:58:14 +03:00
|
|
|
char *hdir = r_str_newf (R_JOIN_2_PATHS (R2_HOME_BINRC, "bin-%s"), name);
|
2017-06-15 00:36:39 +02:00
|
|
|
char *path = r_str_home (hdir);
|
|
|
|
RList *files = r_sys_dir (path);
|
|
|
|
if (!r_list_empty (files)) {
|
|
|
|
eprintf ("[binrc] path: %s\n", path);
|
|
|
|
}
|
|
|
|
r_list_foreach (files, iter, file) {
|
|
|
|
if (*file && *file != '.') {
|
|
|
|
eprintf ("[binrc] loading %s\n", file);
|
|
|
|
r_core_cmdf (core, ". %s/%s", path, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_list_free (files);
|
|
|
|
free (path);
|
|
|
|
free (hdir);
|
|
|
|
}
|
|
|
|
|
2019-07-05 18:43:15 +02:00
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
|
|
|
bool found;
|
|
|
|
} RCoreFileData;
|
|
|
|
|
|
|
|
static bool filecb(void *user, void *data, ut32 id) {
|
|
|
|
RCoreFileData *fd = user;
|
|
|
|
RIODesc *desc = (RIODesc *)data;
|
|
|
|
if (!strcmp (desc->name, fd->name)) {
|
|
|
|
fd->found = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-07 12:42:47 +02:00
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
|
|
|
ut64 addr;
|
|
|
|
RBin *bin;
|
|
|
|
} RCoreLinkData;
|
|
|
|
|
|
|
|
static bool linkcb(void *user, void *data, ut32 id) {
|
|
|
|
RCoreLinkData *ld = user;
|
|
|
|
RIODesc *desc = (RIODesc *)data;
|
|
|
|
|
|
|
|
RBinFile *bf = r_bin_file_find_by_fd (ld->bin, desc->fd);
|
|
|
|
if (bf) {
|
|
|
|
RListIter *iter;
|
|
|
|
RBinSymbol *sym;
|
|
|
|
RList *symbols = r_bin_file_get_symbols (bf);
|
|
|
|
r_list_foreach (symbols, iter, sym) {
|
|
|
|
if (!strcmp (sym->name, ld->name)) {
|
|
|
|
ld->addr = sym->vaddr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-09-22 11:42:49 +03:00
|
|
|
}
|
2019-07-07 12:42:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-28 23:38:14 +02:00
|
|
|
R_API bool r_core_bin_load(RCore *r, const char *filenameuri, ut64 baddr) {
|
2014-04-29 11:10:35 -05:00
|
|
|
RCoreFile *cf = r_core_file_cur (r);
|
2017-08-22 07:42:16 +00:00
|
|
|
RIODesc *desc = cf ? r_io_desc_get (r->io, cf->fd) : NULL;
|
2017-08-25 14:33:14 +02:00
|
|
|
ut64 laddr = r_config_get_i (r->config, "bin.laddr");
|
2014-04-23 18:04:25 -05:00
|
|
|
RBinFile *binfile = NULL;
|
2014-05-04 10:03:15 -05:00
|
|
|
RBinPlugin *plugin = NULL;
|
2019-07-05 18:43:15 +02:00
|
|
|
bool is_io_load;
|
2018-09-13 05:12:52 +08:00
|
|
|
const char *cmd_load;
|
2017-05-28 23:38:14 +02:00
|
|
|
if (!cf) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-24 21:22:15 +01:00
|
|
|
// NULL deref guard
|
2017-08-22 07:42:16 +00:00
|
|
|
if (desc) {
|
2017-08-25 23:30:55 +02:00
|
|
|
is_io_load = desc && desc->plugin;
|
2016-09-19 13:44:47 +01:00
|
|
|
if (!filenameuri || !*filenameuri) {
|
2017-08-22 07:42:16 +00:00
|
|
|
filenameuri = desc->name;
|
2014-06-05 23:07:02 +02:00
|
|
|
}
|
2017-08-25 23:30:55 +02:00
|
|
|
} else {
|
|
|
|
is_io_load = false;
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
2014-01-11 00:56:02 -06:00
|
|
|
|
2014-04-23 18:04:25 -05:00
|
|
|
if (!filenameuri) {
|
2013-04-24 09:46:57 +02:00
|
|
|
eprintf ("r_core_bin_load: no file specified\n");
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2011-11-15 23:26:45 +01:00
|
|
|
}
|
2013-11-09 17:15:30 -06:00
|
|
|
|
2013-12-09 04:56:13 +01:00
|
|
|
r->bin->minstrlen = r_config_get_i (r->config, "bin.minstr");
|
2015-10-06 20:52:50 -04:00
|
|
|
r->bin->maxstrbuf = r_config_get_i (r->config, "bin.maxstrbuf");
|
2013-12-17 02:10:13 +01:00
|
|
|
if (is_io_load) {
|
2014-09-09 17:01:04 +02:00
|
|
|
// TODO? necessary to restore the desc back?
|
2014-01-11 00:56:02 -06:00
|
|
|
// Fix to select pid before trying to load the binary
|
2017-08-22 07:42:16 +00:00
|
|
|
if ((desc->plugin && desc->plugin->isdbg) || r_config_get_i (r->config, "cfg.debug")) {
|
2015-08-21 01:44:44 +02:00
|
|
|
r_core_file_do_load_for_debug (r, baddr, filenameuri);
|
2014-01-11 00:56:02 -06:00
|
|
|
} else {
|
2019-07-05 18:43:15 +02:00
|
|
|
r_core_file_do_load_for_io_plugin (r, baddr, 0LL);
|
2011-11-22 00:59:20 +01:00
|
|
|
}
|
2017-08-22 07:42:16 +00:00
|
|
|
r_io_use_fd (r->io, desc->fd);
|
2019-07-05 18:43:15 +02:00
|
|
|
// Restore original desc
|
2014-05-04 10:03:15 -05:00
|
|
|
}
|
2017-08-22 07:42:16 +00:00
|
|
|
if (cf && binfile && desc) {
|
|
|
|
binfile->fd = desc->fd;
|
2016-11-07 19:33:49 +01:00
|
|
|
}
|
2014-05-04 10:03:15 -05:00
|
|
|
binfile = r_bin_cur (r->bin);
|
2019-06-29 01:45:44 +02:00
|
|
|
if (r->bin->cur && r->bin->cur->o && r->bin->cur->o->plugin && r->bin->cur->o->plugin->strfilter) {
|
2015-10-29 13:55:03 +01:00
|
|
|
char msg[2];
|
2019-06-29 01:45:44 +02:00
|
|
|
msg[0] = r->bin->cur->o->plugin->strfilter;
|
2015-10-29 13:55:03 +01:00
|
|
|
msg[1] = 0;
|
2019-03-20 00:34:59 +08:00
|
|
|
r_config_set (r->config, "bin.str.filter", msg);
|
2015-10-29 13:55:03 +01:00
|
|
|
}
|
2017-08-24 23:24:07 +02:00
|
|
|
//r_core_bin_set_env (r, binfile);
|
2014-05-08 18:35:04 -05:00
|
|
|
plugin = r_bin_file_cur_plugin (binfile);
|
2017-06-15 00:36:39 +02:00
|
|
|
if (plugin && plugin->name) {
|
|
|
|
load_scripts_for (r, plugin->name);
|
|
|
|
}
|
2018-09-13 05:12:52 +08:00
|
|
|
cmd_load = r_config_get (r->config, "cmd.load");
|
|
|
|
if (cmd_load && *cmd_load) {
|
|
|
|
r_core_cmd (r, cmd_load, 0);
|
|
|
|
}
|
2017-06-15 00:36:39 +02:00
|
|
|
|
2017-10-20 11:36:38 +02:00
|
|
|
if (plugin && plugin->name) {
|
2019-05-16 14:18:16 +02:00
|
|
|
if (!strcmp (plugin->name, "any")) {
|
|
|
|
if (r_str_startswith (desc->name, "rap") && strstr (desc->name, "://")) {
|
|
|
|
r_io_map_new (r->io, desc->fd, desc->perm, 0, laddr, UT64_MAX);
|
|
|
|
} else {
|
|
|
|
r_io_map_new (r->io, desc->fd, desc->perm, 0, laddr, r_io_desc_size (desc));
|
|
|
|
}
|
2017-10-20 11:36:38 +02:00
|
|
|
// set use of raw strings
|
|
|
|
//r_config_set (r->config, "bin.rawstr", "true");
|
|
|
|
// r_config_set_i (r->config, "io.va", false);
|
|
|
|
// get bin.minstr
|
|
|
|
r->bin->minstrlen = r_config_get_i (r->config, "bin.minstr");
|
|
|
|
r->bin->maxstrbuf = r_config_get_i (r->config, "bin.maxstrbuf");
|
|
|
|
} else if (binfile) {
|
2019-07-05 18:43:15 +02:00
|
|
|
RBinObject *obj = r_bin_cur_object (r->bin);
|
2018-09-13 11:16:27 +02:00
|
|
|
if (obj) {
|
2019-07-05 18:43:15 +02:00
|
|
|
bool va = obj->info ? obj->info->has_va : 0;
|
2018-09-13 11:16:27 +02:00
|
|
|
if (!va) {
|
|
|
|
r_config_set_i (r->config, "io.va", 0);
|
|
|
|
}
|
|
|
|
//workaround to map correctly malloc:// and raw binaries
|
|
|
|
if (r_io_desc_is_dbg (desc) || (!obj->sections || !va)) {
|
2018-09-21 02:16:54 +02:00
|
|
|
r_io_map_new (r->io, desc->fd, desc->perm, 0, laddr, r_io_desc_size (desc));
|
2018-09-13 11:16:27 +02:00
|
|
|
}
|
|
|
|
RBinInfo *info = obj->info;
|
|
|
|
if (info) {
|
|
|
|
r_core_bin_set_arch_bits (r, binfile->file, info->arch, info->bits);
|
|
|
|
} else {
|
|
|
|
r_core_bin_set_arch_bits (r, binfile->file,
|
2017-10-20 11:36:38 +02:00
|
|
|
r_config_get (r->config, "asm.arch"),
|
|
|
|
r_config_get_i (r->config, "asm.bits"));
|
2018-09-13 11:16:27 +02:00
|
|
|
}
|
2015-10-26 03:08:39 +01:00
|
|
|
}
|
2017-02-20 02:54:16 +01:00
|
|
|
}
|
2017-10-20 11:36:38 +02:00
|
|
|
} else {
|
2018-12-24 02:18:12 +02:00
|
|
|
if (desc) {
|
|
|
|
r_io_map_new (r->io, desc->fd, desc->perm, 0, laddr, r_io_desc_size (desc));
|
|
|
|
}
|
2017-10-20 12:44:57 +02:00
|
|
|
if (binfile) {
|
|
|
|
r_core_bin_set_arch_bits (r, binfile->file,
|
|
|
|
r_config_get (r->config, "asm.arch"),
|
|
|
|
r_config_get_i (r->config, "asm.bits"));
|
|
|
|
}
|
2013-04-24 09:46:57 +02:00
|
|
|
}
|
2018-02-22 15:24:16 +00:00
|
|
|
if (desc && r_config_get_i (r->config, "io.exec")) {
|
2018-09-21 02:16:54 +02:00
|
|
|
desc->perm |= R_PERM_X;
|
2017-08-26 02:04:45 +02:00
|
|
|
}
|
2014-07-07 16:09:03 +02:00
|
|
|
if (plugin && plugin->name && !strcmp (plugin->name, "dex")) {
|
2017-05-03 18:53:26 +02:00
|
|
|
r_core_cmd0 (r, "\"(fix-dex,wx `ph sha1 $s-32 @32` @12 ;"
|
|
|
|
" wx `ph adler32 $s-12 @12` @8)\"\n");
|
2013-04-12 01:15:00 +02:00
|
|
|
}
|
2016-04-13 23:18:36 +02:00
|
|
|
if (!r_config_get_i (r->config, "cfg.debug")) {
|
2019-04-14 15:50:27 +02:00
|
|
|
loadGP (r);
|
2016-04-13 23:18:36 +02:00
|
|
|
}
|
2016-05-09 17:24:12 +02:00
|
|
|
if (r_config_get_i (r->config, "bin.libs")) {
|
|
|
|
const char *lib;
|
|
|
|
RListIter *iter;
|
|
|
|
RList *libs = r_bin_get_libs (r->bin);
|
|
|
|
r_list_foreach (libs, iter, lib) {
|
2019-07-05 18:43:15 +02:00
|
|
|
eprintf ("[bin.libs] Opening %s\n", lib);
|
|
|
|
RCoreFileData filedata = {lib, false};
|
|
|
|
r_id_storage_foreach (r->io->files, filecb, &filedata);
|
|
|
|
if (filedata.found) {
|
|
|
|
eprintf ("Already opened\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ut64 baddr = r_io_map_location (r->io, 0x200000);
|
2019-07-07 12:42:47 +02:00
|
|
|
if (baddr != UT64_MAX) {
|
|
|
|
r_core_file_loadlib (r, lib, baddr);
|
|
|
|
}
|
2016-05-09 17:24:12 +02:00
|
|
|
}
|
2019-07-05 18:43:15 +02:00
|
|
|
r_core_cmd0 (r, "obb 0;s entry0");
|
|
|
|
r_config_set_i (r->config, "bin.at", true);
|
2019-07-07 12:42:47 +02:00
|
|
|
eprintf ("[bin.libs] Linking imports...\n");
|
|
|
|
RBinImport *imp;
|
|
|
|
RList *imports = r_bin_get_imports (r->bin);
|
|
|
|
r_list_foreach (imports, iter, imp) {
|
|
|
|
// PLT finding
|
|
|
|
RFlagItem *impsym = r_flag_get (r->flags, sdb_fmt ("sym.imp.%s", imp->name));
|
|
|
|
if (!impsym) {
|
2019-11-28 16:22:20 +01:00
|
|
|
//eprintf ("Cannot find '%s' import in the PLT\n", imp->name);
|
2019-07-07 12:42:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ut64 imp_addr = impsym->offset;
|
|
|
|
eprintf ("Resolving %s... ", imp->name);
|
|
|
|
RCoreLinkData linkdata = {imp->name, UT64_MAX, r->bin};
|
|
|
|
r_id_storage_foreach (r->io->files, linkcb, &linkdata);
|
|
|
|
if (linkdata.addr != UT64_MAX) {
|
|
|
|
eprintf ("0x%08"PFMT64x"\n", linkdata.addr);
|
|
|
|
ut64 a = linkdata.addr;
|
|
|
|
ut64 b = imp_addr;
|
|
|
|
r_core_cmdf (r, "ax 0x%08"PFMT64x" 0x%08"PFMT64x, a, b);
|
|
|
|
} else {
|
|
|
|
eprintf ("NO\n");
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 17:24:12 +02:00
|
|
|
}
|
2018-03-15 11:46:07 +01:00
|
|
|
|
|
|
|
//If type == R_BIN_TYPE_CORE, we need to create all the maps
|
|
|
|
if (plugin && binfile && plugin->file_type
|
|
|
|
&& plugin->file_type (binfile) == R_BIN_TYPE_CORE) {
|
|
|
|
ut64 sp_addr = (ut64)-1;
|
|
|
|
RIOMap *stack_map = NULL;
|
|
|
|
|
|
|
|
// Setting the right arch and bits, so regstate will be shown correctly
|
|
|
|
if (plugin->info) {
|
|
|
|
RBinInfo *inf = plugin->info (binfile);
|
2019-07-05 18:43:15 +02:00
|
|
|
eprintf ("Setting up coredump arch-bits to: %s-%d\n", inf->arch, inf->bits);
|
2018-03-15 11:46:07 +01:00
|
|
|
r_config_set (r->config, "asm.arch", inf->arch);
|
|
|
|
r_config_set_i (r->config, "asm.bits", inf->bits);
|
2018-10-14 11:37:45 +02:00
|
|
|
r_bin_info_free (inf);
|
2019-04-17 06:49:28 -03:00
|
|
|
}
|
2018-03-15 11:46:07 +01:00
|
|
|
if (binfile->o->regstate) {
|
|
|
|
if (r_reg_arena_set_bytes (r->anal->reg, binfile->o->regstate)) {
|
|
|
|
eprintf ("Setting up coredump: Problem while setting the registers\n");
|
|
|
|
} else {
|
|
|
|
eprintf ("Setting up coredump: Registers have been set\n");
|
|
|
|
const char *regname = r_reg_get_name (r->anal->reg, R_REG_NAME_SP);
|
2018-09-09 03:38:59 +02:00
|
|
|
if (regname) {
|
|
|
|
RRegItem *reg = r_reg_get (r->anal->reg, regname, -1);
|
|
|
|
if (reg) {
|
|
|
|
sp_addr = r_reg_get_value (r->anal->reg, reg);
|
|
|
|
stack_map = r_io_map_get (r->io, sp_addr);
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 06:49:28 -03:00
|
|
|
regname = r_reg_get_name (r->anal->reg, R_REG_NAME_PC);
|
|
|
|
if (regname) {
|
|
|
|
RRegItem *reg = r_reg_get (r->anal->reg, regname, -1);
|
|
|
|
if (reg) {
|
|
|
|
ut64 seek = r_reg_get_value (r->anal->reg, reg);
|
2020-04-17 12:53:35 +02:00
|
|
|
r_core_seek (r, seek, true);
|
2019-04-17 06:49:28 -03:00
|
|
|
}
|
|
|
|
}
|
2018-03-15 11:46:07 +01:00
|
|
|
}
|
2019-04-17 06:49:28 -03:00
|
|
|
}
|
2018-03-15 11:46:07 +01:00
|
|
|
|
|
|
|
RBinObject *o = binfile->o;
|
|
|
|
int map = 0;
|
|
|
|
if (o && o->maps) {
|
|
|
|
RList *maps = o->maps;
|
|
|
|
RListIter *iter;
|
|
|
|
RBinMap *mapcore;
|
|
|
|
|
|
|
|
r_list_foreach (maps, iter, mapcore) {
|
|
|
|
RIOMap *iomap = r_io_map_get (r->io, mapcore->addr);
|
|
|
|
if (iomap && (mapcore->file || stack_map == iomap)) {
|
|
|
|
r_io_map_set_name (iomap, mapcore->file ? mapcore->file : "[stack]");
|
|
|
|
}
|
|
|
|
map++;
|
|
|
|
}
|
|
|
|
r_list_free (maps);
|
2018-03-16 16:37:42 +01:00
|
|
|
o->maps = NULL;
|
2018-03-15 11:46:07 +01:00
|
|
|
}
|
|
|
|
eprintf ("Setting up coredump: %d maps have been found and created\n", map);
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
beach:
|
2015-09-14 12:35:38 +02:00
|
|
|
return true;
|
2010-10-04 10:55:43 +02:00
|
|
|
}
|
2014-01-20 03:00:16 +01:00
|
|
|
|
2018-12-03 01:19:59 +01:00
|
|
|
R_API RCoreFile *r_core_file_open_many(RCore *r, const char *file, int perm, ut64 loadaddr) {
|
2019-07-05 18:43:15 +02:00
|
|
|
const bool openmany = r_config_get_i (r->config, "file.openmany");
|
2017-08-25 23:19:59 +02:00
|
|
|
int opened_count = 0;
|
2016-05-25 23:33:47 +02:00
|
|
|
RListIter *fd_iter, *iter2;
|
|
|
|
RIODesc *fd;
|
2014-04-29 11:10:35 -05:00
|
|
|
|
2018-12-03 01:19:59 +01:00
|
|
|
RList *list_fds = r_io_open_many (r->io, file, perm, 0644);
|
2014-01-25 18:06:17 -06:00
|
|
|
|
2017-02-20 02:54:16 +01:00
|
|
|
if (!list_fds || r_list_length (list_fds) == 0) {
|
2014-01-26 01:29:17 -06:00
|
|
|
r_list_free (list_fds);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-29 11:10:35 -05:00
|
|
|
r_list_foreach_safe (list_fds, fd_iter, iter2, fd) {
|
|
|
|
opened_count++;
|
2019-07-05 18:43:15 +02:00
|
|
|
if (openmany && opened_count > 1) {
|
2014-04-29 11:10:35 -05:00
|
|
|
// XXX - Open Many should limit the number of files
|
|
|
|
// loaded in io plugin area this needs to be more premptive
|
|
|
|
// like down in the io plugin layer.
|
|
|
|
// start closing down descriptors
|
|
|
|
r_list_delete (list_fds, fd_iter);
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-03 01:19:59 +01:00
|
|
|
RCoreFile *fh = R_NEW0 (RCoreFile);
|
2019-07-05 18:43:15 +02:00
|
|
|
if (fh) {
|
|
|
|
fh->alive = 1;
|
|
|
|
fh->core = r;
|
|
|
|
fh->fd = fd->fd;
|
|
|
|
r->file = fh;
|
|
|
|
r_bin_bind (r->bin, &(fh->binb));
|
|
|
|
r_list_append (r->files, fh);
|
|
|
|
r_core_bin_load (r, fd->name, loadaddr);
|
2014-01-27 08:24:44 -06:00
|
|
|
}
|
2014-01-23 21:05:35 -06:00
|
|
|
}
|
2017-08-27 13:42:16 +02:00
|
|
|
return NULL;
|
2014-01-23 21:05:35 -06:00
|
|
|
}
|
|
|
|
|
2016-09-26 00:45:28 +02:00
|
|
|
/* loadaddr is r2 -m (mapaddr) */
|
2016-05-25 23:33:47 +02:00
|
|
|
R_API RCoreFile *r_core_file_open(RCore *r, const char *file, int flags, ut64 loadaddr) {
|
2019-07-05 18:43:15 +02:00
|
|
|
r_return_val_if_fail (r && file, NULL);
|
2020-08-12 18:54:49 +02:00
|
|
|
ut64 prev = r_time_now_mono ();
|
2019-07-05 18:43:15 +02:00
|
|
|
const bool openmany = r_config_get_i (r->config, "file.openmany");
|
2016-03-18 13:39:45 +01:00
|
|
|
RCoreFile *fh = NULL;
|
2014-01-25 18:06:17 -06:00
|
|
|
|
2012-08-09 12:42:44 +02:00
|
|
|
if (!strcmp (file, "-")) {
|
2012-02-07 00:44:46 +01:00
|
|
|
file = "malloc://512";
|
2017-08-18 20:03:29 +02:00
|
|
|
}
|
2017-05-03 17:19:49 +02:00
|
|
|
//if not flags was passed open it with -r--
|
|
|
|
if (!flags) {
|
2018-09-21 02:16:54 +02:00
|
|
|
flags = R_PERM_R;
|
2012-08-09 12:42:44 +02:00
|
|
|
}
|
2020-05-13 01:28:23 +02:00
|
|
|
r->io->bits = r->rasm->bits; // TODO: we need an api for this
|
2018-08-11 19:18:58 +02:00
|
|
|
RIODesc *fd = r_io_open_nomap (r->io, file, flags, 0644);
|
2019-11-11 01:09:24 +02:00
|
|
|
if (r_cons_is_breaked()) {
|
|
|
|
goto beach;
|
|
|
|
}
|
2019-07-05 18:43:15 +02:00
|
|
|
if (!fd && openmany) {
|
2014-01-23 21:05:35 -06:00
|
|
|
// XXX - make this an actual option somewhere?
|
2014-10-08 00:44:31 +02:00
|
|
|
fh = r_core_file_open_many (r, file, flags, loadaddr);
|
2016-09-26 00:45:28 +02:00
|
|
|
if (fh) {
|
|
|
|
goto beach;
|
|
|
|
}
|
2014-01-23 21:05:35 -06:00
|
|
|
}
|
2016-09-19 13:44:47 +01:00
|
|
|
if (!fd) {
|
2018-09-21 02:16:54 +02:00
|
|
|
if (flags & R_PERM_W) {
|
2018-08-11 19:18:58 +02:00
|
|
|
// flags |= R_IO_CREAT;
|
2016-03-14 22:12:54 +02:00
|
|
|
if (!(fd = r_io_open_nomap (r->io, file, flags, 0644))) {
|
2016-03-18 13:39:45 +01:00
|
|
|
goto beach;
|
2016-03-14 22:12:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-03-18 13:39:45 +01:00
|
|
|
goto beach;
|
2016-03-14 22:12:54 +02:00
|
|
|
}
|
2012-07-06 02:17:44 +02:00
|
|
|
}
|
2011-02-05 00:20:28 +01:00
|
|
|
if (r_io_is_listener (r->io)) {
|
2011-04-17 20:58:32 +02:00
|
|
|
r_core_serve (r, fd);
|
2016-10-09 23:55:34 +02:00
|
|
|
r_io_desc_free (fd);
|
2016-03-18 13:39:45 +01:00
|
|
|
goto beach;
|
2011-02-05 00:20:28 +01:00
|
|
|
}
|
2009-02-05 22:08:46 +01:00
|
|
|
|
2013-12-17 02:10:13 +01:00
|
|
|
fh = R_NEW0 (RCoreFile);
|
2014-01-27 08:24:44 -06:00
|
|
|
if (!fh) {
|
2016-03-18 13:39:45 +01:00
|
|
|
goto beach;
|
2014-01-27 08:24:44 -06:00
|
|
|
}
|
2014-04-22 23:02:46 -05:00
|
|
|
fh->alive = 1;
|
|
|
|
fh->core = r;
|
2017-08-22 07:42:16 +00:00
|
|
|
fh->fd = fd->fd;
|
2016-04-04 00:17:57 +02:00
|
|
|
{
|
2018-08-11 19:18:58 +02:00
|
|
|
const char *cp = r_config_get (r->config, "cmd.open");
|
|
|
|
if (cp && *cp) {
|
|
|
|
r_core_cmd (r, cp, 0);
|
|
|
|
}
|
2016-04-04 00:17:57 +02:00
|
|
|
char *absfile = r_file_abspath (file);
|
|
|
|
r_config_set (r->config, "file.path", absfile);
|
|
|
|
free (absfile);
|
|
|
|
}
|
2014-01-25 18:06:17 -06:00
|
|
|
// check load addr to make sure its still valid
|
2014-05-07 14:04:04 -05:00
|
|
|
r_bin_bind (r->bin, &(fh->binb));
|
2017-03-07 20:08:02 +01:00
|
|
|
|
|
|
|
if (!r->files) {
|
|
|
|
r->files = r_list_newf ((RListFree)r_core_file_free);
|
|
|
|
}
|
|
|
|
|
2018-11-07 23:57:24 +01:00
|
|
|
r->file = fh;
|
|
|
|
r_io_use_fd (r->io, fd->fd);
|
|
|
|
|
2017-08-24 22:37:58 +02:00
|
|
|
r_list_append (r->files, fh);
|
2016-04-22 10:13:54 +02:00
|
|
|
if (r_config_get_i (r->config, "cfg.debug")) {
|
|
|
|
bool swstep = true;
|
2016-05-25 23:33:47 +02:00
|
|
|
if (r->dbg->h && r->dbg->h->canstep) {
|
2016-04-22 10:13:54 +02:00
|
|
|
swstep = false;
|
2016-05-25 23:33:47 +02:00
|
|
|
}
|
2016-04-22 10:13:54 +02:00
|
|
|
r_config_set_i (r->config, "dbg.swstep", swstep);
|
2019-11-05 01:55:41 +00:00
|
|
|
// Set the correct debug handle
|
|
|
|
if (fd->plugin && fd->plugin->isdbg) {
|
|
|
|
char *dh = r_str_ndup (file, (strstr (file, "://") - file));
|
|
|
|
if (dh) {
|
|
|
|
r_debug_use (r->dbg, dh);
|
|
|
|
free (dh);
|
|
|
|
}
|
|
|
|
}
|
2016-04-22 10:13:54 +02:00
|
|
|
}
|
2017-08-25 14:33:14 +02:00
|
|
|
//used by r_core_bin_load otherwise won't load correctly
|
|
|
|
//this should be argument of r_core_bin_load <shrug>
|
2019-05-07 10:27:22 +03:00
|
|
|
if (loadaddr != UT64_MAX) {
|
|
|
|
r_config_set_i (r->config, "bin.laddr", loadaddr);
|
|
|
|
}
|
2019-08-28 02:34:59 +02:00
|
|
|
r_core_cmd0 (r, "=!");
|
2016-03-18 13:39:45 +01:00
|
|
|
beach:
|
2020-08-12 18:54:49 +02:00
|
|
|
r->times->file_open_time = r_time_now_mono () - prev;
|
2009-02-05 22:08:46 +01:00
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
2011-02-07 09:46:01 +01:00
|
|
|
R_API void r_core_file_free(RCoreFile *cf) {
|
2014-05-22 13:52:16 +02:00
|
|
|
int res = 1;
|
2018-11-13 13:05:18 +01:00
|
|
|
|
|
|
|
r_return_if_fail (cf);
|
|
|
|
|
2018-03-14 15:31:08 +01:00
|
|
|
if (!cf->core) {
|
|
|
|
free (cf);
|
2014-11-03 13:36:58 +01:00
|
|
|
return;
|
2016-10-09 23:55:34 +02:00
|
|
|
}
|
2019-07-05 18:43:15 +02:00
|
|
|
res = r_list_delete_data (cf->core->files, cf);
|
2017-08-22 07:42:16 +00:00
|
|
|
if (res && cf->alive) {
|
2014-01-18 09:26:09 -06:00
|
|
|
// double free libr/io/io.c:70 performs free
|
2017-08-22 07:42:16 +00:00
|
|
|
RIO *io = cf->core->io;
|
|
|
|
if (io) {
|
2018-11-13 13:05:18 +01:00
|
|
|
RBin *bin = cf->binb.bin;
|
|
|
|
RBinFile *bf = r_bin_cur (bin);
|
|
|
|
if (bf) {
|
|
|
|
r_bin_file_deref (bin, bf);
|
|
|
|
}
|
2017-08-22 07:42:16 +00:00
|
|
|
r_io_fd_close (io, cf->fd);
|
2014-11-07 10:37:54 +01:00
|
|
|
free (cf);
|
2014-11-03 13:36:58 +01:00
|
|
|
}
|
2013-11-09 17:15:30 -06:00
|
|
|
}
|
2011-02-07 09:46:01 +01:00
|
|
|
}
|
|
|
|
|
2014-02-21 10:58:31 +01:00
|
|
|
R_API int r_core_file_close(RCore *r, RCoreFile *fh) {
|
2014-11-03 11:47:51 +01:00
|
|
|
int ret;
|
2017-08-22 07:42:16 +00:00
|
|
|
RIODesc *desc = fh && r ? r_io_desc_get (r->io, fh->fd) : NULL;
|
2017-02-20 02:54:16 +01:00
|
|
|
RCoreFile *prev_cf = r && r->file != fh? r->file: NULL;
|
2014-09-16 21:58:02 +02:00
|
|
|
|
2019-06-20 12:45:00 +08:00
|
|
|
// TODO: This is not correctly done. because map and iodesc are
|
2014-09-16 21:58:02 +02:00
|
|
|
// still referenced // we need to fully clear all R_IO structs
|
|
|
|
// related to a file as well as the ones needed for RBin.
|
|
|
|
//
|
2014-04-22 23:02:46 -05:00
|
|
|
// XXX -these checks are intended to *try* and catch
|
|
|
|
// stale objects. Unfortunately, if the file handle
|
|
|
|
// (fh) is stale and freed, and there is more than 1
|
|
|
|
// fh in the r->files list, we are hosed. (design flaw)
|
|
|
|
// TODO maybe using sdb to keep track of the allocated and
|
|
|
|
// deallocated files might be a good solutions
|
2017-02-20 02:54:16 +01:00
|
|
|
if (!r || !desc || r_list_empty (r->files)) {
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2017-02-20 02:54:16 +01:00
|
|
|
}
|
2014-04-22 23:02:46 -05:00
|
|
|
|
2017-02-20 02:54:16 +01:00
|
|
|
if (fh == r->file) {
|
|
|
|
r->file = NULL;
|
|
|
|
}
|
2014-10-17 00:04:52 +02:00
|
|
|
|
2017-08-22 07:42:16 +00:00
|
|
|
r_core_file_set_by_fd (r, fh->fd);
|
|
|
|
r_core_bin_set_by_fd (r, fh->fd);
|
2014-10-17 00:04:52 +02:00
|
|
|
|
|
|
|
/* delete filedescriptor from io descs here */
|
2017-08-26 23:47:52 +02:00
|
|
|
// r_io_desc_del (r->io, fh->fd);
|
2014-10-17 00:04:52 +02:00
|
|
|
|
2014-11-03 11:47:51 +01:00
|
|
|
// AVOID DOUBLE FREE HERE
|
|
|
|
r->files->free = NULL;
|
|
|
|
|
|
|
|
ret = r_list_delete_data (r->files, fh);
|
2014-04-22 23:02:46 -05:00
|
|
|
if (ret) {
|
2017-02-20 02:54:16 +01:00
|
|
|
if (!prev_cf && r_list_length (r->files) > 0) {
|
2014-04-23 18:04:25 -05:00
|
|
|
prev_cf = (RCoreFile *) r_list_get_n (r->files, 0);
|
2017-02-20 02:54:16 +01:00
|
|
|
}
|
2014-04-23 18:04:25 -05:00
|
|
|
|
|
|
|
if (prev_cf) {
|
2017-08-22 07:42:16 +00:00
|
|
|
RIODesc *desc = prev_cf && r ? r_io_desc_get (r->io, prev_cf->fd) : NULL;
|
2017-02-20 02:54:16 +01:00
|
|
|
if (!desc) {
|
2014-04-23 18:04:25 -05:00
|
|
|
eprintf ("Error: RCoreFile's found with out a supporting RIODesc.\n");
|
2017-02-20 02:54:16 +01:00
|
|
|
}
|
2014-04-23 18:04:25 -05:00
|
|
|
ret = r_core_file_set_by_file (r, prev_cf);
|
2014-04-22 23:02:46 -05:00
|
|
|
}
|
|
|
|
}
|
2017-08-27 00:59:57 +02:00
|
|
|
r_io_desc_close (desc);
|
2018-06-13 00:50:50 +02:00
|
|
|
r_core_file_free (fh);
|
2009-02-05 22:08:46 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-27 02:06:50 -05:00
|
|
|
R_API RCoreFile *r_core_file_get_by_fd(RCore *core, int fd) {
|
2011-02-07 09:46:01 +01:00
|
|
|
RCoreFile *file;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (core->files, iter, file) {
|
2017-08-22 07:42:16 +00:00
|
|
|
if (file->fd == fd) {
|
2010-02-01 11:55:56 +01:00
|
|
|
return file;
|
2016-10-09 23:55:34 +02:00
|
|
|
}
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-19 13:46:04 +01:00
|
|
|
R_API int r_core_file_list(RCore *core, int mode) {
|
2017-08-22 07:42:16 +00:00
|
|
|
int count = 0;
|
2011-02-07 09:46:01 +01:00
|
|
|
RCoreFile *f;
|
2017-08-22 07:42:16 +00:00
|
|
|
RIODesc *desc;
|
2014-12-19 13:46:04 +01:00
|
|
|
ut64 from;
|
2018-01-10 13:45:37 +01:00
|
|
|
RListIter *it;
|
|
|
|
RBinFile *bf;
|
2011-02-07 09:46:01 +01:00
|
|
|
RListIter *iter;
|
2020-11-25 07:46:00 +05:30
|
|
|
PJ *pj;
|
2016-11-02 03:27:37 +01:00
|
|
|
if (mode == 'j') {
|
2020-11-25 07:46:00 +05:30
|
|
|
pj = pj_new ();
|
|
|
|
if (!pj) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pj_a (pj);
|
2016-11-02 03:27:37 +01:00
|
|
|
}
|
2011-02-07 09:46:01 +01:00
|
|
|
r_list_foreach (core->files, iter, f) {
|
2017-08-22 07:42:16 +00:00
|
|
|
desc = r_io_desc_get (core->io, f->fd);
|
2018-01-10 19:18:36 +01:00
|
|
|
if (!desc) {
|
|
|
|
// cannot find desc for this fd, RCoreFile inconsistency!!!1
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-24 22:37:58 +02:00
|
|
|
from = 0LL;
|
2014-12-19 13:46:04 +01:00
|
|
|
switch (mode) {
|
2020-11-25 07:46:00 +05:30
|
|
|
case 'j': { // "oij"
|
|
|
|
pj_o (pj);
|
|
|
|
pj_kb (pj, "raised", core->io->desc->fd == f->fd);
|
|
|
|
pj_ki (pj, "fd", f->fd);
|
|
|
|
pj_ks (pj, "uri", desc->uri);
|
|
|
|
pj_kn (pj, "from", (ut64) from);
|
|
|
|
pj_kb (pj, "writable", desc->perm & R_PERM_W);
|
|
|
|
pj_ki (pj, "size", (int) r_io_desc_size (desc));
|
|
|
|
pj_end (pj);
|
2014-12-19 13:46:04 +01:00
|
|
|
break;
|
2020-11-25 07:46:00 +05:30
|
|
|
}
|
2014-12-19 13:46:04 +01:00
|
|
|
case '*':
|
|
|
|
case 'r':
|
2018-01-10 13:45:37 +01:00
|
|
|
// TODO: use a getter
|
2017-03-07 20:08:02 +01:00
|
|
|
{
|
2018-01-10 13:45:37 +01:00
|
|
|
bool fileHaveBin = false;
|
|
|
|
char *absfile = r_file_abspath (desc->uri);
|
2017-03-07 20:08:02 +01:00
|
|
|
r_list_foreach (core->bin->binfiles, it, bf) {
|
2017-08-22 07:42:16 +00:00
|
|
|
if (bf->fd == f->fd) {
|
2017-03-07 20:08:02 +01:00
|
|
|
r_cons_printf ("o %s 0x%"PFMT64x "\n", absfile, (ut64) from);
|
2018-01-10 13:45:37 +01:00
|
|
|
fileHaveBin = true;
|
2017-03-07 20:08:02 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-10 13:45:37 +01:00
|
|
|
if (!fileHaveBin && !strstr (absfile, "://")) {
|
|
|
|
r_cons_printf ("o %s 0x%"PFMT64x "\n", absfile, (ut64) from);
|
|
|
|
}
|
|
|
|
free (absfile);
|
2017-03-07 20:08:02 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
{
|
|
|
|
bool header_loaded = false;
|
|
|
|
r_list_foreach (core->bin->binfiles, it, bf) {
|
2017-08-22 07:42:16 +00:00
|
|
|
if (bf->fd == f->fd) {
|
2017-03-07 20:08:02 +01:00
|
|
|
header_loaded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!header_loaded) {
|
2017-09-13 13:45:50 +02:00
|
|
|
RList* maps = r_io_map_get_for_fd (core->io, f->fd);
|
|
|
|
RListIter *iter;
|
|
|
|
RIOMap* current_map;
|
2017-08-22 07:42:16 +00:00
|
|
|
char *absfile = r_file_abspath (desc->uri);
|
2017-09-13 13:45:50 +02:00
|
|
|
r_list_foreach (maps, iter, current_map) {
|
|
|
|
if (current_map) {
|
2020-12-08 08:57:08 -06:00
|
|
|
r_cons_printf ("on %s 0x%"PFMT64x "\n", absfile, r_io_map_begin (current_map));
|
2017-09-13 13:45:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
r_list_free (maps);
|
2017-03-07 20:08:02 +01:00
|
|
|
free(absfile);
|
2017-09-13 13:45:50 +02:00
|
|
|
|
2017-03-07 20:08:02 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-19 13:46:04 +01:00
|
|
|
break;
|
|
|
|
default:
|
2017-02-20 02:54:16 +01:00
|
|
|
{
|
2017-08-22 07:42:16 +00:00
|
|
|
ut64 sz = r_io_desc_size (desc);
|
2016-10-09 22:03:02 +02:00
|
|
|
const char *fmt;
|
|
|
|
if (sz == UT64_MAX) {
|
2017-08-22 07:42:16 +00:00
|
|
|
fmt = "%c %d %d %s @ 0x%"PFMT64x " ; %s size=%"PFMT64d "\n";
|
2016-10-09 22:03:02 +02:00
|
|
|
} else {
|
2017-08-22 07:42:16 +00:00
|
|
|
fmt = "%c %d %d %s @ 0x%"PFMT64x " ; %s size=%"PFMT64u "\n";
|
2016-10-09 22:03:02 +02:00
|
|
|
}
|
|
|
|
r_cons_printf (fmt,
|
2017-08-22 07:42:16 +00:00
|
|
|
core->io->desc->fd == f->fd ? '*': '-',
|
2017-02-20 02:54:16 +01:00
|
|
|
count,
|
2017-08-22 07:42:16 +00:00
|
|
|
(int) f->fd, desc->uri, (ut64) from,
|
2018-09-21 02:16:54 +02:00
|
|
|
desc->perm & R_PERM_W? "rw": "r",
|
2017-08-22 07:42:16 +00:00
|
|
|
r_io_desc_size (desc));
|
2017-02-20 02:54:16 +01:00
|
|
|
}
|
|
|
|
break;
|
2014-12-19 13:46:04 +01:00
|
|
|
}
|
2010-02-01 11:55:56 +01:00
|
|
|
count++;
|
|
|
|
}
|
2017-02-20 02:54:16 +01:00
|
|
|
if (mode == 'j') {
|
2020-11-25 07:46:00 +05:30
|
|
|
pj_end (pj);
|
|
|
|
r_cons_println (pj_string (pj));
|
|
|
|
pj_free (pj);
|
2016-10-09 23:55:34 +02:00
|
|
|
}
|
2010-02-01 11:55:56 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-05-08 18:35:04 -05:00
|
|
|
// XXX - needs to account for binfile index and bin object index
|
2019-06-05 19:23:59 -04:00
|
|
|
R_API bool r_core_file_bin_raise(RCore *core, ut32 bfid) {
|
2014-04-27 02:06:50 -05:00
|
|
|
RBin *bin = core->bin;
|
2019-06-05 19:23:59 -04:00
|
|
|
RBinFile *bf = r_list_get_n (bin->binfiles, bfid);
|
|
|
|
bool res = false;
|
2014-04-27 02:06:50 -05:00
|
|
|
if (bf) {
|
|
|
|
res = r_bin_file_set_cur_binfile (bin, bf);
|
2017-02-20 02:54:16 +01:00
|
|
|
if (res) {
|
2017-08-22 07:42:16 +00:00
|
|
|
r_io_use_fd (core->io, bf->fd);
|
2017-02-20 02:54:16 +01:00
|
|
|
}
|
|
|
|
res = res? r_core_file_set_by_fd (core, bf->fd): res;
|
|
|
|
if (res) {
|
|
|
|
core->switch_file_view = 1;
|
|
|
|
}
|
2014-04-27 02:06:50 -05:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_file_binlist(RCore *core) {
|
|
|
|
int count = 0;
|
|
|
|
RListIter *iter;
|
2014-05-08 18:35:04 -05:00
|
|
|
RCoreFile *cur_cf = core->file, *cf = NULL;
|
2014-05-16 04:07:03 +02:00
|
|
|
RBinFile *binfile = NULL;
|
2017-08-22 07:42:16 +00:00
|
|
|
RIODesc *desc;
|
2014-04-27 02:06:50 -05:00
|
|
|
RBin *bin = core->bin;
|
2017-02-20 02:54:16 +01:00
|
|
|
const RList *binfiles = bin? bin->binfiles: NULL;
|
2014-04-27 02:06:50 -05:00
|
|
|
|
2016-10-09 23:55:34 +02:00
|
|
|
if (!binfiles) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-27 02:06:50 -05:00
|
|
|
r_list_foreach (binfiles, iter, binfile) {
|
2014-05-28 04:34:12 +02:00
|
|
|
int fd = binfile->fd;
|
2014-05-08 18:35:04 -05:00
|
|
|
cf = r_core_file_get_by_fd (core, fd);
|
2017-08-22 07:42:16 +00:00
|
|
|
desc = r_io_desc_get (core->io, fd);
|
2017-08-24 22:37:58 +02:00
|
|
|
if (cf) {
|
|
|
|
r_cons_printf ("%c %d %s ; %s\n",
|
2017-08-22 07:42:16 +00:00
|
|
|
core->io->desc == desc ? '*': '-',
|
2018-09-21 02:16:54 +02:00
|
|
|
fd, desc->uri, desc->perm & R_PERM_W? "rw": "r");
|
2014-04-27 02:06:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
r_core_file_set_by_file (core, cur_cf);
|
2014-05-04 10:03:15 -05:00
|
|
|
//r_core_bin_bind (core, cur_bf);
|
2014-04-27 02:06:50 -05:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-10-24 16:54:57 +02:00
|
|
|
static bool close_but_cb(void *user, void *data, ut32 id) {
|
2017-11-22 14:58:09 +01:00
|
|
|
RCore *core = (RCore *)user;
|
|
|
|
RIODesc *desc = (RIODesc *)data;
|
|
|
|
if (core && desc && core->file) {
|
|
|
|
if (desc->fd != core->file->fd) {
|
|
|
|
// TODO: use the API
|
|
|
|
r_core_cmdf (core, "o-%d", desc->fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API bool r_core_file_close_all_but(RCore *core) {
|
|
|
|
r_id_storage_foreach (core->io->files, close_but_cb, core);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-30 01:40:05 +00:00
|
|
|
R_API bool r_core_file_close_fd(RCore *core, int fd) {
|
2011-02-07 09:46:01 +01:00
|
|
|
RCoreFile *file;
|
|
|
|
RListIter *iter;
|
2017-03-02 22:47:59 +01:00
|
|
|
if (fd == -1) {
|
2017-10-17 23:27:49 +00:00
|
|
|
// FIXME: Only closes files known to the core!
|
2017-03-02 22:47:59 +01:00
|
|
|
r_list_free (core->files);
|
|
|
|
core->files = NULL;
|
|
|
|
core->file = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-07 09:46:01 +01:00
|
|
|
r_list_foreach (core->files, iter, file) {
|
2017-08-22 07:42:16 +00:00
|
|
|
if (file->fd == fd) {
|
2014-04-22 23:02:46 -05:00
|
|
|
r_core_file_close (core, file);
|
2014-11-07 10:37:54 +01:00
|
|
|
if (file == core->file) {
|
|
|
|
core->file = NULL; // deref
|
|
|
|
}
|
2015-09-14 12:35:38 +02:00
|
|
|
return true;
|
2011-02-07 09:46:01 +01:00
|
|
|
}
|
|
|
|
}
|
2017-08-30 01:40:05 +00:00
|
|
|
return r_io_fd_close (core->io, fd);
|
2009-02-05 22:08:46 +01:00
|
|
|
}
|
2011-07-13 17:41:26 +02:00
|
|
|
|
2017-02-20 02:54:16 +01:00
|
|
|
R_API RCoreFile *r_core_file_find_by_fd(RCore *core, ut64 fd) {
|
2014-04-23 18:04:25 -05:00
|
|
|
RListIter *iter;
|
|
|
|
RCoreFile *cf = NULL;
|
|
|
|
r_list_foreach (core->files, iter, cf) {
|
2017-08-22 07:42:16 +00:00
|
|
|
if (cf && cf->fd == fd) {
|
2016-11-02 03:27:37 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-04-23 18:04:25 -05:00
|
|
|
cf = NULL;
|
|
|
|
}
|
|
|
|
return cf;
|
|
|
|
}
|
|
|
|
|
2017-02-20 02:54:16 +01:00
|
|
|
R_API RCoreFile *r_core_file_find_by_name(RCore *core, const char *name) {
|
2014-04-23 18:04:25 -05:00
|
|
|
RListIter *iter;
|
|
|
|
RCoreFile *cf = NULL;
|
2017-08-22 07:42:16 +00:00
|
|
|
RIODesc *desc;
|
|
|
|
|
2018-09-13 10:17:26 +02:00
|
|
|
if (!core) {
|
2017-08-22 07:42:16 +00:00
|
|
|
return NULL;
|
2018-09-13 10:17:26 +02:00
|
|
|
}
|
2014-04-23 18:04:25 -05:00
|
|
|
|
|
|
|
r_list_foreach (core->files, iter, cf) {
|
2017-08-22 07:42:16 +00:00
|
|
|
desc = r_io_desc_get (core->io, cf->fd);
|
|
|
|
if (desc && !strcmp (desc->name, name)) {
|
2016-11-02 03:27:37 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-04-23 18:04:25 -05:00
|
|
|
cf = NULL;
|
|
|
|
}
|
|
|
|
return cf;
|
|
|
|
}
|
|
|
|
|
2017-02-20 02:54:16 +01:00
|
|
|
R_API int r_core_file_set_by_fd(RCore *core, ut64 fd) {
|
2017-08-24 22:37:58 +02:00
|
|
|
if (core) {
|
|
|
|
r_io_use_fd (core->io, fd);
|
|
|
|
r_core_bin_set_by_fd (core, fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
|
|
|
|
2017-02-20 02:54:16 +01:00
|
|
|
R_API int r_core_file_set_by_name(RCore *core, const char *name) {
|
2014-04-23 18:04:25 -05:00
|
|
|
RCoreFile *cf = r_core_file_find_by_name (core, name);
|
|
|
|
return r_core_file_set_by_file (core, cf);
|
|
|
|
}
|
|
|
|
|
2017-02-20 02:54:16 +01:00
|
|
|
R_API int r_core_file_set_by_file(RCore *core, RCoreFile *cf) {
|
2017-08-24 22:37:58 +02:00
|
|
|
if (core && cf) {
|
|
|
|
if (!r_core_file_set_by_fd (core, cf->fd)) {
|
|
|
|
return false;
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
2017-08-24 22:37:58 +02:00
|
|
|
core->file = cf;
|
2015-09-14 12:35:38 +02:00
|
|
|
return true;
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2014-04-23 18:04:25 -05:00
|
|
|
}
|
2014-04-27 02:06:50 -05:00
|
|
|
|
2017-02-20 02:54:16 +01:00
|
|
|
R_API ut32 r_core_file_cur_fd(RCore *core) {
|
2017-08-22 07:42:16 +00:00
|
|
|
if (core && core->file) {
|
|
|
|
return core->file->fd;
|
2014-05-08 18:35:04 -05:00
|
|
|
}
|
2016-11-02 03:27:37 +01:00
|
|
|
return UT32_MAX;
|
2014-04-29 11:10:35 -05:00
|
|
|
}
|
|
|
|
|
2017-02-20 02:54:16 +01:00
|
|
|
R_API RCoreFile *r_core_file_cur(RCore *r) {
|
2014-04-29 11:10:35 -05:00
|
|
|
// Add any locks here
|
|
|
|
return r->file;
|
2014-05-16 04:07:03 +02:00
|
|
|
}
|