2018-01-10 13:45:37 +01:00
|
|
|
/* radare - LGPL - Copyright 2009-2018 - 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
|
|
|
|
2018-08-30 13:16:50 +02:00
|
|
|
#define UPDATE_TIME(a) (r->times->file_open_time = r_sys_now () - (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
|
|
|
|
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;
|
2017-08-22 07:42:16 +00:00
|
|
|
RBinFile *bf = ofile ? r_bin_file_find_by_fd (core->bin, ofile->fd)
|
2017-02-20 02:54:16 +01:00
|
|
|
: 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
|
|
|
|
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) {
|
2016-06-06 22:40:34 +02:00
|
|
|
r_debug_kill (core->dbg, core->dbg->pid, core->dbg->tid, 9); // KILL
|
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);
|
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
|
|
|
|
2017-08-22 07:42:16 +00:00
|
|
|
if (ofile) {
|
|
|
|
if (r_bin_file_delete (core->bin, ofile->fd)) {
|
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)) {
|
|
|
|
ut64 baddr = r_config_get_i (core->config, "bin.baddr");
|
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);
|
|
|
|
}
|
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-08-18 18:06:27 +02:00
|
|
|
if (isdebug) {
|
2016-08-26 23:51:56 +02:00
|
|
|
int newtid = newpid;
|
2014-04-23 18:04:25 -05:00
|
|
|
// XXX - select the right backend
|
2017-08-22 07:42:16 +00:00
|
|
|
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);
|
2017-01-16 12:22:24 +01:00
|
|
|
#if __linux__
|
|
|
|
core->dbg->main_pid = newpid;
|
|
|
|
newtid = newpid;
|
|
|
|
#endif
|
2017-08-22 07:42:16 +00:00
|
|
|
#pragma message ("fix debugger-concept in core")
|
2016-08-26 23:51:56 +02:00
|
|
|
#if __WINDOWS__
|
|
|
|
r_debug_select (core->dbg, newpid, newtid);
|
2017-07-14 16:38:27 +02:00
|
|
|
core->dbg->reason.type = R_DEBUG_REASON_NONE;
|
2016-08-26 23:51:56 +02:00
|
|
|
#endif
|
2016-06-07 10:03:04 +02:00
|
|
|
}
|
2016-01-02 23:25:20 +01:00
|
|
|
//reopen and attach
|
|
|
|
r_core_setup_debugger (core, "native", true);
|
2016-08-26 23:51:56 +02:00
|
|
|
r_debug_select (core->dbg, newpid, newtid);
|
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
|
|
|
}
|
2014-10-20 22:48:50 +02:00
|
|
|
r_core_seek (core, origoff, 1);
|
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 {
|
|
|
|
ut64 gp = r_num_math (core->num, "loc._gp");
|
|
|
|
if (gp && gp != UT64_MAX) {
|
|
|
|
r_config_set_i (core->config, "anal.gp", gp);
|
|
|
|
}
|
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);
|
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);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 13:21:12 +02:00
|
|
|
r_sys_setenv ("RABIN2_LANG", r_config_get (core->config, "bin.lang"));
|
|
|
|
r_sys_setenv ("RABIN2_DEMANGLE", r_config_get (core->config, "bin.demangle"));
|
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));
|
2016-03-20 02:24:17 +01:00
|
|
|
r_sys_setenv ("R2_ENDIAN", core->assembler->big_endian? "big": "little");
|
2018-04-10 23:52:47 +02:00
|
|
|
r_sys_setenv ("R2_BSIZE", sdb_fmt ("%d", core->blocksize));
|
2016-03-20 02:24:17 +01:00
|
|
|
r_sys_setenv ("R2_ARCH", r_config_get (core->config, "asm.arch"));
|
2018-04-10 23:52:47 +02:00
|
|
|
r_sys_setenv ("R2_BITS", sdb_fmt ("%d", 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");
|
2011-02-04 11:30:08 +01:00
|
|
|
return ret;
|
2010-08-19 20:28:25 +02:00
|
|
|
}
|
|
|
|
|
2015-05-28 11:09:13 +02:00
|
|
|
#if !__linux__
|
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
|
|
|
|
2014-01-11 00:56:02 -06:00
|
|
|
R_API int r_core_bin_reload(RCore *r, const char *file, ut64 baseaddr) {
|
2014-01-09 16:57:04 -06:00
|
|
|
int result = 0;
|
2014-05-08 18:35:04 -05:00
|
|
|
RCoreFile *cf = r_core_file_cur (r);
|
|
|
|
RBinFile *bf = NULL;
|
2017-08-23 00:04:47 +00:00
|
|
|
if (cf) {
|
|
|
|
result = r_bin_reload (r->bin, cf->fd, baseaddr);
|
2016-11-03 11:51:10 +01:00
|
|
|
}
|
2014-05-08 18:35:04 -05:00
|
|
|
bf = r_bin_cur (r->bin);
|
|
|
|
r_core_bin_set_env (r, bf);
|
2014-01-09 16:57:04 -06:00
|
|
|
return result;
|
2014-01-03 21:36:02 -06: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);
|
2015-10-22 04:48:56 +02:00
|
|
|
if (plugin && !strncmp (plugin->name, "any", 5)) {
|
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;
|
2014-05-04 10:03:15 -05:00
|
|
|
if (plugin && strcmp (plugin->name, "any") && 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);
|
|
|
|
r_core_bin_set_env (r, binfile);
|
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;
|
|
|
|
}
|
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;
|
|
|
|
}
|
2014-05-04 10:03:15 -05:00
|
|
|
if (plugin && strcmp (plugin->name, "any") && info) {
|
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
|
|
|
}
|
|
|
|
|
2016-05-09 17:24:12 +02:00
|
|
|
static int try_loadlib(RCore *core, const char *lib, ut64 addr) {
|
|
|
|
RCoreFile *cf = r_core_file_open (core, lib, 0, addr);
|
|
|
|
if (!cf) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API bool r_core_file_loadlib(RCore *core, const char *lib, ut64 libaddr) {
|
|
|
|
const char *ldlibrarypath[] = {
|
2018-07-13 18:19:56 +02:00
|
|
|
R2_LIBDIR,
|
2016-05-09 17:24:12 +02:00
|
|
|
"/usr/local/lib",
|
|
|
|
"/usr/lib",
|
|
|
|
"/lib",
|
|
|
|
"./",
|
|
|
|
NULL
|
|
|
|
};
|
2017-02-20 02:54:16 +01:00
|
|
|
const char * *libpath = (const char * *) &ldlibrarypath;
|
2016-05-09 17:24:12 +02:00
|
|
|
|
|
|
|
if (*lib == '/') {
|
|
|
|
if (try_loadlib (core, lib, libaddr)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (*libpath) {
|
|
|
|
bool ret = false;
|
|
|
|
char *s = r_str_newf ("%s/%s", *libpath, lib);
|
|
|
|
if (try_loadlib (core, s, libaddr)) {
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
free (s);
|
|
|
|
if (ret) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
libpath++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
// TODO:
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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-28 22:48:28 +02:00
|
|
|
int va = 1;
|
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;
|
2017-08-25 15:01:25 +02:00
|
|
|
RBinObject *obj = NULL;
|
2016-05-24 21:22:15 +01:00
|
|
|
int 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;
|
2018-10-04 16:12:32 +02:00
|
|
|
#if 0
|
2017-08-22 07:42:16 +00:00
|
|
|
} else if (desc->name && strcmp (filenameuri, desc->name)) {
|
2014-06-05 23:07:02 +02:00
|
|
|
// XXX - this needs to be handled appropriately
|
|
|
|
// if the cf does not match the filenameuri then
|
|
|
|
// either that RCoreFIle * needs to be loaded or a
|
|
|
|
// new RCoreFile * should be opened.
|
2018-10-01 03:55:08 -04:00
|
|
|
eprintf ("Error: The filenameuri '%s' is not the same as in RCoreFile: %s\n",
|
|
|
|
filenameuri, desc->name);
|
2018-10-04 16:12:32 +02:00
|
|
|
#endif
|
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 {
|
2015-08-28 20:14:26 +02:00
|
|
|
r_core_file_do_load_for_io_plugin (r, baddr, laddr);
|
2011-11-22 00:59:20 +01:00
|
|
|
}
|
2014-05-28 15:03:31 +02:00
|
|
|
// Restore original desc
|
2017-08-22 07:42:16 +00:00
|
|
|
r_io_use_fd (r->io, desc->fd);
|
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);
|
2015-10-29 18:41:21 +01:00
|
|
|
if (r->bin->cur && r->bin->cur->curplugin && r->bin->cur->curplugin->strfilter) {
|
2015-10-29 13:55:03 +01:00
|
|
|
char msg[2];
|
|
|
|
msg[0] = r->bin->cur->curplugin->strfilter;
|
|
|
|
msg[1] = 0;
|
|
|
|
r_config_set (r->config, "bin.strfilter", msg);
|
|
|
|
}
|
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) {
|
|
|
|
if (!strncmp (plugin->name, "any", 3)) {
|
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));
|
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) {
|
2018-11-13 13:05:18 +01:00
|
|
|
obj = r_bin_cur_object (r->bin);
|
2018-09-13 11:16:27 +02:00
|
|
|
if (obj) {
|
|
|
|
va = obj->info ? obj->info->has_va : va;
|
|
|
|
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
|
|
|
}
|
2018-08-24 23:39:08 -07:00
|
|
|
|
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-09-21 02:16:54 +02:00
|
|
|
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")) {
|
|
|
|
/* load GP for mips */
|
|
|
|
ut64 gp = r_num_math (r->num, "loc._gp");
|
|
|
|
if (gp && gp != UT64_MAX) {
|
|
|
|
r_config_set_i (r->config, "anal.gp", gp);
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 17:24:12 +02:00
|
|
|
if (r_config_get_i (r->config, "bin.libs")) {
|
2017-02-20 02:54:16 +01:00
|
|
|
ut64 libaddr = (r->assembler->bits == 64)? 0x00007fff00000000LL: 0x7f000000;
|
2016-05-09 17:24:12 +02:00
|
|
|
const char *lib;
|
|
|
|
RListIter *iter;
|
|
|
|
RList *libs = r_bin_get_libs (r->bin);
|
|
|
|
r_list_foreach (libs, iter, lib) {
|
|
|
|
eprintf ("Opening %s\n", lib);
|
|
|
|
r_core_file_loadlib (r, lib, libaddr);
|
|
|
|
libaddr += 0x2000000;
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
eprintf ("Setting up coredump: asm.arch <-> %s and asm.bits <-> %d\n",
|
|
|
|
inf->arch,
|
|
|
|
inf->bits);
|
|
|
|
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);
|
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);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2014-10-08 00:44:31 +02:00
|
|
|
R_API RCoreFile *r_core_file_open_many(RCore *r, const char *file, int flags, ut64 loadaddr) {
|
2017-08-25 23:19:59 +02:00
|
|
|
bool openmany = r_config_get_i (r->config, "file.openmany");
|
|
|
|
int opened_count = 0;
|
|
|
|
// ut64 current_loadaddr = loadaddr;
|
2017-08-27 13:59:08 +02:00
|
|
|
RCoreFile *fh; //, *top_file = NULL;
|
2016-05-25 23:33:47 +02:00
|
|
|
RListIter *fd_iter, *iter2;
|
2014-10-17 00:04:52 +02:00
|
|
|
RList *list_fds = NULL;
|
2016-05-25 23:33:47 +02:00
|
|
|
RIODesc *fd;
|
2014-04-29 11:10:35 -05:00
|
|
|
|
2014-10-08 00:44:31 +02:00
|
|
|
list_fds = r_io_open_many (r->io, file, flags, 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++;
|
|
|
|
if (opened_count > openmany) {
|
|
|
|
// 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;
|
|
|
|
}
|
2014-01-23 21:05:35 -06:00
|
|
|
fh = R_NEW0 (RCoreFile);
|
2014-01-27 08:24:44 -06:00
|
|
|
if (!fh) {
|
|
|
|
eprintf ("file.c:r_core_many failed to allocate new RCoreFile.\n");
|
|
|
|
break;
|
|
|
|
}
|
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;
|
2014-01-23 21:05:35 -06:00
|
|
|
r->file = fh;
|
|
|
|
// XXX - load addr should be at a set offset
|
2017-08-24 22:37:58 +02:00
|
|
|
//r_core_file_get_next_map (r, fh, flags, current_loadaddr);
|
2014-05-07 14:04:04 -05:00
|
|
|
r_bin_bind (r->bin, &(fh->binb));
|
2014-01-23 21:05:35 -06:00
|
|
|
r_list_append (r->files, fh);
|
2017-08-24 22:37:58 +02:00
|
|
|
r_core_bin_load (r, fd->name, 0LL);
|
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) {
|
2017-02-20 02:54:16 +01:00
|
|
|
ut64 prev = r_sys_now ();
|
2014-05-21 23:03:09 +02:00
|
|
|
const int 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
|
|
|
|
2016-03-14 22:12:54 +02:00
|
|
|
if (!file || !*file) {
|
2016-03-18 13:39:45 +01:00
|
|
|
goto beach;
|
2016-03-14 22:12:54 +02: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
|
|
|
}
|
2012-05-30 01:35:41 +02:00
|
|
|
r->io->bits = r->assembler->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);
|
2016-09-19 13:44:47 +01:00
|
|
|
if (!fd && openmany > 2) {
|
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) {
|
2014-05-28 18:58:53 +02:00
|
|
|
eprintf ("core/file.c: r_core_open failed to allocate RCoreFile.\n");
|
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;
|
2009-02-18 01:43:57 +01:00
|
|
|
|
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);
|
|
|
|
}
|
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>
|
|
|
|
r_config_set_i (r->config, "bin.laddr", loadaddr);
|
2016-03-18 13:39:45 +01:00
|
|
|
beach:
|
2017-02-20 02:54:16 +01:00
|
|
|
r->times->file_open_time = r_sys_now () - prev;
|
2009-02-05 22:08:46 +01:00
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
2016-01-02 23:25:20 +01:00
|
|
|
R_API int r_core_files_free(const RCore *core, RCoreFile *cf) {
|
2016-10-09 23:55:34 +02:00
|
|
|
if (!core || !core->files || !cf) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-22 23:02:46 -05:00
|
|
|
return r_list_delete_data (core->files, cf);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-08-22 07:42:16 +00:00
|
|
|
res = r_core_files_free (cf->core, cf);
|
|
|
|
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
|
|
|
|
2015-09-30 13:10:49 +02:00
|
|
|
// TODO: This is not correclty 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;
|
2016-11-02 03:27:37 +01:00
|
|
|
if (mode == 'j') {
|
2014-12-19 13:46:04 +01:00
|
|
|
r_cons_printf ("[");
|
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) {
|
|
|
|
case 'j':
|
|
|
|
r_cons_printf ("{\"raised\":%s,\"fd\":%d,\"uri\":\"%s\",\"from\":%"
|
2017-08-23 01:45:33 +02:00
|
|
|
PFMT64d ",\"writable\":%s,\"size\":%d}%s",
|
2017-08-22 07:42:16 +00:00
|
|
|
core->io->desc->fd == f->fd ? "true": "false",
|
|
|
|
(int) f->fd, desc->uri, (ut64) from,
|
2018-09-21 02:16:54 +02:00
|
|
|
desc->perm & R_PERM_W? "true": "false",
|
2017-08-22 07:42:16 +00:00
|
|
|
(int) r_io_desc_size (desc),
|
2017-02-20 02:54:16 +01:00
|
|
|
iter->n? ",": "");
|
2014-12-19 13:46:04 +01:00
|
|
|
break;
|
|
|
|
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) {
|
2017-08-31 23:41:03 -07:00
|
|
|
r_cons_printf ("on %s 0x%"PFMT64x "\n", absfile, current_map->itv.addr);
|
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') {
|
2014-12-19 13:46:04 +01:00
|
|
|
r_cons_printf ("]\n");
|
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
|
2017-02-20 02:54:16 +01:00
|
|
|
R_API int r_core_file_bin_raise(RCore *core, ut32 binfile_idx) {
|
2014-04-27 02:06:50 -05:00
|
|
|
RBin *bin = core->bin;
|
2017-02-20 02:54:16 +01:00
|
|
|
int v = binfile_idx > 1? binfile_idx: 1;
|
2014-04-27 02:06:50 -05:00
|
|
|
RBinFile *bf = r_list_get_n (bin->binfiles, v);
|
2015-09-14 12:35:38 +02:00
|
|
|
int 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
|
|
|
|
|
|
|
R_API int r_core_hash_load(RCore *r, const char *file) {
|
|
|
|
const ut8 *md5, *sha1;
|
|
|
|
char hash[128], *p;
|
2014-09-28 01:37:56 +02:00
|
|
|
int i;
|
2014-09-30 12:06:37 +02:00
|
|
|
int buf_len = 0;
|
2013-02-07 09:41:05 +01:00
|
|
|
ut8 *buf = NULL;
|
2011-07-13 17:41:26 +02:00
|
|
|
RHash *ctx;
|
|
|
|
ut64 limit;
|
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;
|
|
|
|
if (!file && desc) {
|
|
|
|
file = desc->name;
|
2015-08-25 12:31:04 +02:00
|
|
|
}
|
|
|
|
if (!file) {
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2015-08-25 12:31:04 +02:00
|
|
|
}
|
2011-07-13 17:41:26 +02:00
|
|
|
|
|
|
|
limit = r_config_get_i (r->config, "cfg.hashlimit");
|
2017-08-22 07:42:16 +00:00
|
|
|
if (desc && r_io_desc_size (desc) > limit) {
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2017-01-14 21:45:13 +01:00
|
|
|
}
|
2017-02-20 02:54:16 +01:00
|
|
|
buf = (ut8 *) r_file_slurp (file, &buf_len);
|
2017-01-14 21:45:13 +01:00
|
|
|
if (!buf) {
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2017-01-14 21:45:13 +01:00
|
|
|
}
|
2015-09-14 12:35:38 +02:00
|
|
|
ctx = r_hash_new (true, R_HASH_MD5);
|
2011-07-13 17:41:26 +02:00
|
|
|
md5 = r_hash_do_md5 (ctx, buf, buf_len);
|
|
|
|
p = hash;
|
2016-11-02 03:27:37 +01:00
|
|
|
for (i = 0; i < R_HASH_SIZE_MD5; i++) {
|
2011-07-13 17:41:26 +02:00
|
|
|
sprintf (p, "%02x", md5[i]);
|
2011-11-11 17:14:09 +01:00
|
|
|
p += 2;
|
2011-07-13 17:41:26 +02:00
|
|
|
}
|
2011-11-11 17:14:09 +01:00
|
|
|
*p = 0;
|
2011-07-13 17:41:26 +02:00
|
|
|
r_config_set (r->config, "file.md5", hash);
|
|
|
|
r_hash_free (ctx);
|
2015-09-14 12:35:38 +02:00
|
|
|
ctx = r_hash_new (true, R_HASH_SHA1);
|
2011-07-13 17:41:26 +02:00
|
|
|
sha1 = r_hash_do_sha1 (ctx, buf, buf_len);
|
|
|
|
p = hash;
|
2016-11-02 03:27:37 +01:00
|
|
|
for (i = 0; i < R_HASH_SIZE_SHA1; i++) {
|
2011-07-13 17:41:26 +02:00
|
|
|
sprintf (p, "%02x", sha1[i]);
|
2012-08-04 23:48:06 +02:00
|
|
|
p += 2;
|
2011-07-13 17:41:26 +02:00
|
|
|
}
|
2012-08-04 23:48:06 +02:00
|
|
|
*p = 0;
|
2011-07-13 17:41:26 +02:00
|
|
|
r_config_set (r->config, "file.sha1", hash);
|
|
|
|
r_hash_free (ctx);
|
2011-11-11 17:14:09 +01:00
|
|
|
free (buf);
|
2015-09-14 12:35:38 +02:00
|
|
|
return true;
|
2011-07-13 17:41:26 +02:00
|
|
|
}
|
2014-04-23 18:04:25 -05: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
|
|
|
}
|