2022-08-22 10:21:33 +00:00
|
|
|
/* radare2 - LGPL - Copyright 2009-2022 - pancake */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
#include "r_core.h"
|
|
|
|
|
2021-12-21 18:52:17 +00:00
|
|
|
R_API int r_core_setup_debugger(RCore *r, const char *debugbackend, bool attach) {
|
2014-04-23 23:04:25 +00:00
|
|
|
int pid, *p = NULL;
|
2016-09-13 01:25:15 +00:00
|
|
|
bool is_gdb = !strcmp (debugbackend, "gdb");
|
2021-01-06 22:44:18 +00:00
|
|
|
RIODesc * fd = r->io->desc;
|
2016-05-18 08:01:39 +00:00
|
|
|
const char *prompt = NULL;
|
|
|
|
|
2014-04-23 23:04:25 +00:00
|
|
|
p = fd ? fd->data : NULL;
|
2021-03-14 21:22:04 +00:00
|
|
|
r_config_set_b (r->config, "cfg.debug", true);
|
2014-04-23 23:04:25 +00:00
|
|
|
if (!p) {
|
2022-08-22 10:21:33 +00:00
|
|
|
R_LOG_ERROR ("Invalid debug io");
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2014-04-23 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 09:46:18 +00:00
|
|
|
r_config_set_b (r->config, "io.ff", true);
|
2017-06-05 12:45:45 +00:00
|
|
|
r_core_cmdf (r, "dL %s", debugbackend);
|
2016-09-13 01:25:15 +00:00
|
|
|
if (!is_gdb) {
|
2017-09-10 23:22:21 +00:00
|
|
|
pid = r_io_desc_get_pid (fd);
|
2021-10-04 18:56:34 +00:00
|
|
|
if (pid >= 0) {
|
|
|
|
r_core_cmdf (r, "dp=%d", pid);
|
|
|
|
if (attach) {
|
|
|
|
r_core_cmdf (r, "dpa %d", pid);
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-01 07:56:51 +00:00
|
|
|
R_LOG_ERROR ("Cannot retrieve pid from io");
|
2016-09-13 01:25:15 +00:00
|
|
|
}
|
2016-09-12 21:04:44 +00:00
|
|
|
}
|
2016-01-02 22:25:20 +00:00
|
|
|
//this makes to attach twice showing warnings in the output
|
|
|
|
//we get "resource busy" so it seems isn't an issue
|
2014-04-23 23:04:25 +00:00
|
|
|
r_core_cmd (r, ".dr*", 0);
|
|
|
|
/* honor dbg.bep */
|
|
|
|
{
|
|
|
|
const char *bep = r_config_get (r->config, "dbg.bep");
|
|
|
|
if (bep) {
|
|
|
|
if (!strcmp (bep, "loader")) {
|
|
|
|
/* do nothing here */
|
2015-11-17 21:37:48 +00:00
|
|
|
} else if (!strcmp (bep, "entry")) {
|
2014-04-23 23:04:25 +00:00
|
|
|
r_core_cmd (r, "dcu entry0", 0);
|
2015-11-17 21:37:48 +00:00
|
|
|
} else {
|
|
|
|
r_core_cmdf (r, "dcu %s", bep);
|
|
|
|
}
|
2014-04-23 23:04:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-31 00:57:52 +00:00
|
|
|
r_core_cmd (r, "sr PC", 0);
|
2016-05-18 08:01:39 +00:00
|
|
|
|
|
|
|
/* set the prompt if it's not been set already by the callbacks */
|
|
|
|
prompt = r_config_get (r->config, "cmd.prompt");
|
|
|
|
if (prompt && !strcmp (prompt, "")) {
|
2016-09-12 21:04:44 +00:00
|
|
|
if (r_config_get_i (r->config, "dbg.status")) {
|
2016-05-18 08:01:39 +00:00
|
|
|
r_config_set (r->config, "cmd.prompt", ".dr*;drd;sr PC;pi 1;s-");
|
2016-09-12 21:04:44 +00:00
|
|
|
} else {
|
2016-05-18 08:01:39 +00:00
|
|
|
r_config_set (r->config, "cmd.prompt", ".dr*");
|
2016-09-12 21:04:44 +00:00
|
|
|
}
|
2016-05-18 08:01:39 +00:00
|
|
|
}
|
2014-04-23 23:04:25 +00:00
|
|
|
r_config_set (r->config, "cmd.vprompt", ".dr*");
|
2019-06-10 16:07:33 +00:00
|
|
|
r_config_set (r->config, "cmd.gprompt", ".dr*");
|
2015-09-14 10:35:38 +00:00
|
|
|
return true;
|
2014-04-23 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 18:52:17 +00:00
|
|
|
R_API int r_core_seek_base(RCore *core, const char *hex) {
|
2015-11-17 21:37:48 +00:00
|
|
|
ut64 addr = r_num_tail (core->num, core->offset, hex);
|
2020-04-17 10:53:35 +00:00
|
|
|
return r_core_seek (core, addr, true);
|
2013-09-11 23:55:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 17:34:09 +00:00
|
|
|
R_API bool r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size, int append) {
|
2012-04-21 12:28:53 +00:00
|
|
|
ut64 i;
|
|
|
|
ut8 *buf;
|
|
|
|
int bs = core->blocksize;
|
|
|
|
FILE *fd;
|
2015-04-03 02:04:46 +00:00
|
|
|
if (append) {
|
|
|
|
fd = r_sandbox_fopen (file, "ab");
|
|
|
|
} else {
|
|
|
|
r_sys_truncate (file, 0);
|
|
|
|
fd = r_sandbox_fopen (file, "wb");
|
|
|
|
}
|
2012-04-21 12:28:53 +00:00
|
|
|
if (!fd) {
|
2022-08-01 07:56:51 +00:00
|
|
|
R_LOG_ERROR ("Cannot open '%s' for writing", file);
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2012-04-21 12:28:53 +00:00
|
|
|
}
|
2015-08-18 23:58:27 +00:00
|
|
|
/* some io backends seems to be buggy in those cases */
|
2018-09-13 08:17:26 +00:00
|
|
|
if (bs > 4096) {
|
2015-08-18 23:58:27 +00:00
|
|
|
bs = 4096;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2012-04-21 12:28:53 +00:00
|
|
|
buf = malloc (bs);
|
2015-08-18 23:58:27 +00:00
|
|
|
if (!buf) {
|
2022-08-22 10:21:33 +00:00
|
|
|
R_LOG_ERROR ("Cannot alloc %d byte(s)", bs);
|
2015-08-19 08:37:02 +00:00
|
|
|
fclose (fd);
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2015-08-18 23:58:27 +00:00
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_push (NULL, NULL);
|
|
|
|
for (i = 0; i < size; i += bs) {
|
|
|
|
if (r_cons_is_breaked ()) {
|
2012-04-21 12:28:53 +00:00
|
|
|
break;
|
2016-11-20 18:20:14 +00:00
|
|
|
}
|
|
|
|
if ((i + bs) > size) {
|
2015-08-18 23:58:27 +00:00
|
|
|
bs = size - i;
|
2016-11-20 18:20:14 +00:00
|
|
|
}
|
2015-08-18 23:58:27 +00:00
|
|
|
r_io_read_at (core->io, addr + i, buf, bs);
|
|
|
|
if (fwrite (buf, bs, 1, fd) < 1) {
|
2022-08-22 10:21:33 +00:00
|
|
|
R_LOG_ERROR ("write error");
|
2012-04-21 12:28:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_pop ();
|
2012-04-21 12:28:53 +00:00
|
|
|
fclose (fd);
|
|
|
|
free (buf);
|
2015-09-14 10:35:38 +00:00
|
|
|
return true;
|
2012-04-21 12:28:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-31 14:46:07 +00:00
|
|
|
R_API ut8* r_core_transform_op(RCore *core, const char *arg, char op) {
|
|
|
|
int i, j;
|
2019-04-15 11:24:15 +00:00
|
|
|
ut64 len;
|
2016-04-05 19:59:38 +00:00
|
|
|
char *str = NULL;
|
2021-09-18 20:16:45 +00:00
|
|
|
ut8 *buf = (ut8 *)malloc (core->blocksize);
|
2017-12-03 12:10:59 +00:00
|
|
|
if (!buf) {
|
2019-08-31 14:46:07 +00:00
|
|
|
return NULL;
|
2017-12-03 12:10:59 +00:00
|
|
|
}
|
2022-09-09 10:32:43 +00:00
|
|
|
bool isnum = false;
|
|
|
|
const char *plus = arg? strchr (arg, '+'): NULL;
|
|
|
|
int numsize = 1;
|
|
|
|
if (plus) {
|
|
|
|
numsize = (*arg=='+')? 1: atoi (arg);
|
|
|
|
if (numsize < 1) {
|
|
|
|
numsize = 1;
|
|
|
|
}
|
|
|
|
isnum = true;
|
|
|
|
arg = r_str_trim_head_ro (plus + 1);
|
|
|
|
}
|
2021-09-18 20:16:45 +00:00
|
|
|
if (op == 'i') { // "woi"
|
|
|
|
int hbs = core->blocksize / 2;
|
|
|
|
int j = core->blocksize - 1;
|
|
|
|
for (i = 0; i < hbs; i++, j--) {
|
|
|
|
buf[i] = core->block[j];
|
|
|
|
buf[j] = core->block[i];
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
2010-02-01 10:55:56 +00:00
|
|
|
memcpy (buf, core->block, core->blocksize);
|
2016-04-05 19:59:38 +00:00
|
|
|
|
2021-09-18 20:16:45 +00:00
|
|
|
if (op != 'e') {
|
2016-04-05 19:59:38 +00:00
|
|
|
// fill key buffer either from arg or from clipboard
|
2022-09-09 10:32:43 +00:00
|
|
|
if (arg && !isnum) { // parse arg for key
|
2016-04-07 13:45:59 +00:00
|
|
|
// r_hex_str2bin() is guaranteed to output maximum half the
|
|
|
|
// input size, or 1 byte if there is just a single nibble.
|
2022-09-08 22:05:18 +00:00
|
|
|
str = (char *)malloc ((strlen (arg) / 2) + 1);
|
2018-09-13 08:17:26 +00:00
|
|
|
if (!str) {
|
2016-04-05 19:59:38 +00:00
|
|
|
goto beach;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2016-04-05 19:59:38 +00:00
|
|
|
len = r_hex_str2bin (arg, (ut8 *)str);
|
2016-04-07 13:45:59 +00:00
|
|
|
// Output is invalid if there was just a single nibble,
|
|
|
|
// but in that case, len is negative (-1).
|
2016-04-05 19:59:38 +00:00
|
|
|
if (len <= 0) {
|
2022-08-22 10:21:33 +00:00
|
|
|
R_LOG_ERROR ("Invalid hexpair string");
|
2016-04-05 19:59:38 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
} else { // use clipboard as key
|
2019-05-15 13:34:06 +00:00
|
|
|
const ut8 *tmp = r_buf_data (core->yank_buf, &len);
|
2019-04-15 11:24:15 +00:00
|
|
|
str = r_mem_dup (tmp, len);
|
2018-09-13 08:17:26 +00:00
|
|
|
if (!str) {
|
2016-04-05 19:59:38 +00:00
|
|
|
goto beach;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2016-04-07 13:31:36 +00:00
|
|
|
}
|
2018-09-13 08:17:26 +00:00
|
|
|
} else {
|
|
|
|
len = 0;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2016-04-05 19:59:38 +00:00
|
|
|
// execute the operand
|
2021-09-18 20:16:45 +00:00
|
|
|
if (op == 'e') {
|
2015-09-07 21:36:57 +00:00
|
|
|
int wordsize = 1;
|
2022-09-12 20:04:41 +00:00
|
|
|
char *os, *p, *s = strdup (arg? arg: "");
|
2018-12-26 11:54:12 +00:00
|
|
|
int n = 0, from = 0, to = UT8_MAX, dif = 0, step = 1;
|
2015-09-07 21:36:57 +00:00
|
|
|
os = s;
|
2013-05-02 21:49:58 +00:00
|
|
|
p = strchr (s, ' ');
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
2015-09-07 21:36:57 +00:00
|
|
|
from = r_num_math (core->num, s);
|
2017-12-03 12:10:59 +00:00
|
|
|
s = p + 1;
|
2013-05-02 21:49:58 +00:00
|
|
|
}
|
2015-09-07 21:36:57 +00:00
|
|
|
p = strchr (s, ' ');
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
to = r_num_math (core->num, s);
|
2017-12-03 12:10:59 +00:00
|
|
|
s = p + 1;
|
2015-09-07 21:36:57 +00:00
|
|
|
}
|
|
|
|
p = strchr (s, ' ');
|
2013-05-02 21:49:58 +00:00
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
2015-09-07 21:36:57 +00:00
|
|
|
step = r_num_math (core->num, s);
|
2017-10-22 21:52:39 +00:00
|
|
|
s = p + 1;
|
2015-09-07 21:36:57 +00:00
|
|
|
wordsize = r_num_math (core->num, s);
|
|
|
|
} else {
|
|
|
|
step = r_num_math (core->num, s);
|
2013-05-02 21:49:58 +00:00
|
|
|
}
|
2015-09-07 21:36:57 +00:00
|
|
|
free (os);
|
2022-08-22 10:21:33 +00:00
|
|
|
R_LOG_INFO ("from %d to %d step %d size %d", from, to, step, wordsize);
|
2017-10-22 21:52:39 +00:00
|
|
|
dif = (to <= from)? UT8_MAX: to - from + 1;
|
|
|
|
if (wordsize == 1) {
|
|
|
|
from %= (UT8_MAX + 1);
|
|
|
|
}
|
|
|
|
if (dif < 1) {
|
|
|
|
dif = UT8_MAX + 1;
|
|
|
|
}
|
|
|
|
if (step < 1) {
|
|
|
|
step = 1;
|
|
|
|
}
|
|
|
|
if (wordsize < 1) {
|
|
|
|
wordsize = 1;
|
|
|
|
}
|
2015-09-07 21:36:57 +00:00
|
|
|
if (wordsize == 1) {
|
2017-10-22 21:52:39 +00:00
|
|
|
for (i = n = 0; i < core->blocksize; i++, n += step) {
|
2016-04-26 09:09:15 +00:00
|
|
|
buf[i] = (ut8)(n % dif) + from;
|
2017-10-22 21:52:39 +00:00
|
|
|
}
|
2015-09-07 21:36:57 +00:00
|
|
|
} else if (wordsize == 2) {
|
|
|
|
ut16 num16 = from;
|
2016-04-26 09:09:15 +00:00
|
|
|
for (i = 0; i < core->blocksize; i += wordsize, num16 += step) {
|
|
|
|
r_write_le16 (buf + i, num16);
|
2015-09-07 21:36:57 +00:00
|
|
|
}
|
|
|
|
} else if (wordsize == 4) {
|
|
|
|
ut32 num32 = from;
|
2016-04-26 09:09:15 +00:00
|
|
|
for (i = 0; i < core->blocksize; i += wordsize, num32 += step) {
|
|
|
|
r_write_le32 (buf + i, num32);
|
2015-09-07 21:36:57 +00:00
|
|
|
}
|
|
|
|
} else if (wordsize == 8) {
|
|
|
|
ut64 num64 = from;
|
2016-04-26 09:09:15 +00:00
|
|
|
for (i = 0; i < core->blocksize; i += wordsize, num64 += step) {
|
|
|
|
r_write_le64 (buf + i, num64);
|
2015-09-07 21:36:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-22 10:21:33 +00:00
|
|
|
R_LOG_ERROR ("Invalid word size. Use 1, 2, 4 or 8");
|
2015-09-07 21:36:57 +00:00
|
|
|
}
|
2019-09-03 07:49:14 +00:00
|
|
|
} else if (op == '2' || op == '4' || op == '8') { // "wo2" "wo4" "wo8"
|
2019-09-02 18:54:55 +00:00
|
|
|
int inc = op - '0';
|
|
|
|
ut8 tmp;
|
2019-09-03 07:49:14 +00:00
|
|
|
for (i = 0; (i + inc) <= core->blocksize; i += inc) {
|
2019-09-02 18:54:55 +00:00
|
|
|
if (inc == 2) {
|
|
|
|
tmp = buf[i];
|
|
|
|
buf[i] = buf[i+1];
|
|
|
|
buf[i+1] = tmp;
|
|
|
|
} else if (inc == 4) {
|
|
|
|
tmp = buf[i];
|
2015-07-12 19:37:44 +00:00
|
|
|
buf[i] = buf[i+3];
|
|
|
|
buf[i+3] = tmp;
|
2019-09-02 18:54:55 +00:00
|
|
|
tmp = buf[i+1];
|
|
|
|
buf[i+1] = buf[i+2];
|
|
|
|
buf[i+2] = tmp;
|
2019-09-02 19:12:57 +00:00
|
|
|
} else if (inc == 8) {
|
|
|
|
tmp = buf[i];
|
|
|
|
buf[i] = buf[i+7];
|
|
|
|
buf[i+7] = tmp;
|
|
|
|
|
|
|
|
tmp = buf[i+1];
|
|
|
|
buf[i+1] = buf[i+6];
|
|
|
|
buf[i+6] = tmp;
|
|
|
|
|
|
|
|
tmp = buf[i+2];
|
|
|
|
buf[i+2] = buf[i+5];
|
|
|
|
buf[i+5] = tmp;
|
|
|
|
|
|
|
|
tmp = buf[i+3];
|
|
|
|
buf[i+3] = buf[i+4];
|
|
|
|
buf[i+4] = tmp;
|
|
|
|
} else {
|
2022-08-22 10:21:33 +00:00
|
|
|
R_LOG_ERROR ("Invalid inc, use 2, 4 or 8");
|
2019-09-02 19:12:57 +00:00
|
|
|
break;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-02-01 10:55:56 +00:00
|
|
|
}
|
2010-03-08 11:45:22 +00:00
|
|
|
} else {
|
2022-09-09 10:32:43 +00:00
|
|
|
if (isnum) {
|
|
|
|
ut64 n = r_num_math (core->num, arg);
|
2022-10-25 09:52:10 +00:00
|
|
|
bool be = r_config_get_b (core->config, "cfg.bigendian");
|
2022-09-09 10:32:43 +00:00
|
|
|
free (str);
|
2022-10-25 09:52:10 +00:00
|
|
|
len = 0;
|
2022-09-09 10:32:43 +00:00
|
|
|
str = calloc (8, 1);
|
2022-10-25 09:52:10 +00:00
|
|
|
if (R_LIKELY (str)) {
|
|
|
|
switch (numsize) {
|
|
|
|
case 1:
|
|
|
|
if (n > UT8_MAX) {
|
|
|
|
R_LOG_ERROR ("%d doesnt fit in ut8.max", n);
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
str[0] = n;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (n > UT16_MAX) {
|
|
|
|
R_LOG_ERROR ("%d doesnt fit in ut16.max", n);
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
r_write_ble16 (str, n, be);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
if (n > UT32_MAX) {
|
|
|
|
R_LOG_ERROR ("%d doesnt fit in ut32.max", n);
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
r_write_ble32 (str, n, be);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
r_write_ble64 (str, n, be);
|
|
|
|
break;
|
2022-09-09 11:24:55 +00:00
|
|
|
}
|
2022-10-25 09:52:10 +00:00
|
|
|
len = numsize;
|
2019-08-17 23:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = j = 0; i < core->blocksize; i++) {
|
2010-02-01 10:55:56 +00:00
|
|
|
switch (op) {
|
2010-03-08 11:45:22 +00:00
|
|
|
case 'x': buf[i] ^= str[j]; break;
|
|
|
|
case 'a': buf[i] += str[j]; break;
|
|
|
|
case 's': buf[i] -= str[j]; break;
|
|
|
|
case 'm': buf[i] *= str[j]; break;
|
2012-11-07 03:25:42 +00:00
|
|
|
case 'w': buf[i] = str[j]; break;
|
2019-08-20 22:24:33 +00:00
|
|
|
case 'd': buf[i] = (str[j])? (buf[i] / str[j]): 0; break;
|
2010-03-08 11:45:22 +00:00
|
|
|
case 'r': buf[i] >>= str[j]; break;
|
|
|
|
case 'l': buf[i] <<= str[j]; break;
|
|
|
|
case 'o': buf[i] |= str[j]; break;
|
|
|
|
case 'A': buf[i] &= str[j]; break;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2018-09-13 08:17:26 +00:00
|
|
|
j++;
|
|
|
|
if (j >= len) {
|
|
|
|
j = 0; /* cyclic key */
|
|
|
|
}
|
2010-02-01 10:55:56 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-31 14:46:07 +00:00
|
|
|
free (str);
|
|
|
|
return buf;
|
2012-09-26 08:01:43 +00:00
|
|
|
beach:
|
|
|
|
free (str);
|
2019-08-31 14:46:07 +00:00
|
|
|
free (buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_write_op(RCore *core, const char *arg, char op) {
|
2021-09-18 20:16:45 +00:00
|
|
|
ut8 *buf = r_core_transform_op (core, arg, op);
|
2019-08-31 14:46:07 +00:00
|
|
|
if (!buf) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-03 07:49:14 +00:00
|
|
|
int ret = r_core_write_at (core, core->offset, buf, core->blocksize);
|
|
|
|
free (buf);
|
2009-02-05 21:08:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-15 19:24:03 +00:00
|
|
|
// Get address-specific bits and arch at a certain address.
|
|
|
|
// If there are no specific infos (i.e. asm.bits and asm.arch should apply), the bits and arch will be 0 or NULL respectively!
|
|
|
|
R_API void r_core_arch_bits_at(RCore *core, ut64 addr, R_OUT R_NULLABLE int *bits, R_OUT R_BORROW R_NULLABLE const char **arch) {
|
|
|
|
int bitsval = 0;
|
|
|
|
const char *archval = NULL;
|
2019-01-13 02:07:51 +00:00
|
|
|
RBinObject *o = r_bin_cur_object (core->bin);
|
2019-12-15 19:24:03 +00:00
|
|
|
RBinSection *s = o ? r_bin_get_section_at (o, addr, core->io->va) : NULL;
|
2019-01-13 02:07:51 +00:00
|
|
|
if (s) {
|
2019-12-15 19:24:03 +00:00
|
|
|
if (!core->fixedarch) {
|
|
|
|
archval = s->arch;
|
|
|
|
}
|
2019-12-18 16:33:56 +00:00
|
|
|
if (!core->fixedbits && s->bits) {
|
|
|
|
// only enforce if there's one bits set
|
|
|
|
switch (s->bits) {
|
|
|
|
case R_SYS_BITS_16:
|
|
|
|
case R_SYS_BITS_32:
|
|
|
|
case R_SYS_BITS_64:
|
2019-12-28 17:46:14 +00:00
|
|
|
bitsval = s->bits * 8;
|
2019-12-18 16:33:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-12-15 19:24:03 +00:00
|
|
|
}
|
2019-01-13 02:07:51 +00:00
|
|
|
}
|
2020-02-13 10:48:24 +00:00
|
|
|
//if we found bits related with anal hints pick it up
|
2019-12-15 19:24:03 +00:00
|
|
|
if (bits && !bitsval && !core->fixedbits) {
|
2020-02-13 10:48:24 +00:00
|
|
|
bitsval = r_anal_hint_bits_at (core->anal, addr, NULL);
|
|
|
|
}
|
|
|
|
if (arch && !archval && !core->fixedarch) {
|
|
|
|
archval = r_anal_hint_arch_at (core->anal, addr, NULL);
|
2019-12-15 19:24:03 +00:00
|
|
|
}
|
2019-12-18 16:33:56 +00:00
|
|
|
if (bits && bitsval) {
|
2019-12-15 19:24:03 +00:00
|
|
|
*bits = bitsval;
|
2016-08-07 17:05:55 +00:00
|
|
|
}
|
2019-12-18 16:33:56 +00:00
|
|
|
if (arch && archval) {
|
2019-12-15 19:24:03 +00:00
|
|
|
*arch = archval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_seek_arch_bits(RCore *core, ut64 addr) {
|
|
|
|
int bits = 0;
|
|
|
|
const char *arch = NULL;
|
|
|
|
r_core_arch_bits_at (core, addr, &bits, &arch);
|
|
|
|
if (bits) {
|
2022-10-18 21:17:17 +00:00
|
|
|
if (bits != core->anal->config->bits) {
|
|
|
|
r_config_set_i (core->config, "asm.bits", bits);
|
|
|
|
}
|
2013-03-31 02:34:46 +00:00
|
|
|
}
|
2019-12-15 19:24:03 +00:00
|
|
|
if (arch) {
|
2022-10-18 21:17:17 +00:00
|
|
|
if (core->anal->config->arch && strcmp (arch, core->anal->config->arch)) {
|
|
|
|
r_config_set (core->config, "asm.arch", arch);
|
|
|
|
}
|
2016-08-07 17:05:55 +00:00
|
|
|
}
|
2013-03-31 02:34:46 +00:00
|
|
|
}
|
|
|
|
|
2016-01-03 01:05:13 +00:00
|
|
|
R_API bool r_core_seek(RCore *core, ut64 addr, bool rb) {
|
2017-09-02 20:30:47 +00:00
|
|
|
core->offset = r_io_seek (core->io, addr, R_IO_SEEK_SET);
|
2010-01-31 01:30:59 +00:00
|
|
|
if (rb) {
|
2017-08-22 07:42:16 +00:00
|
|
|
r_core_block_read (core);
|
2012-11-30 00:06:30 +00:00
|
|
|
}
|
2019-06-02 15:16:08 +00:00
|
|
|
if (core->binat) {
|
|
|
|
RBinFile *bf = r_bin_file_at (core->bin, core->offset);
|
|
|
|
if (bf) {
|
|
|
|
core->bin->cur = bf;
|
2019-06-05 23:23:59 +00:00
|
|
|
r_bin_select_bfid (core->bin, bf->id);
|
2019-07-05 16:43:15 +00:00
|
|
|
// XXX r_core_cmdf (core, "obb %d", bf->id);
|
2019-06-02 15:16:08 +00:00
|
|
|
} else {
|
|
|
|
core->bin->cur = NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-09-02 20:30:47 +00:00
|
|
|
return core->offset == addr;
|
2009-04-07 11:28:22 +00:00
|
|
|
}
|
|
|
|
|
2013-03-31 02:34:46 +00:00
|
|
|
R_API int r_core_seek_delta(RCore *core, st64 addr) {
|
|
|
|
ut64 tmp = core->offset;
|
2017-03-15 10:06:19 +00:00
|
|
|
if (addr == 0) {
|
2015-09-14 10:35:38 +00:00
|
|
|
return true;
|
2017-03-15 10:06:19 +00:00
|
|
|
}
|
|
|
|
if (addr > 0LL) {
|
|
|
|
/* TODO: check end of file */
|
|
|
|
addr += tmp;
|
2013-03-31 02:34:46 +00:00
|
|
|
} else {
|
|
|
|
/* check < 0 */
|
2017-03-15 10:06:19 +00:00
|
|
|
if (-addr > tmp) {
|
|
|
|
addr = 0;
|
|
|
|
} else {
|
|
|
|
addr += tmp;
|
|
|
|
}
|
2013-03-31 02:34:46 +00:00
|
|
|
}
|
|
|
|
core->offset = addr;
|
2020-04-17 10:53:35 +00:00
|
|
|
return r_core_seek (core, addr, true);
|
2013-03-31 02:34:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 21:44:57 +00:00
|
|
|
// TODO: kill this wrapper
|
2017-08-22 14:52:40 +00:00
|
|
|
R_API bool r_core_write_at(RCore *core, ut64 addr, const ut8 *buf, int size) {
|
2019-05-21 21:44:57 +00:00
|
|
|
r_return_val_if_fail (core && buf && addr != UT64_MAX, false);
|
|
|
|
if (size < 1) {
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2016-10-19 21:00:44 +00:00
|
|
|
}
|
2019-05-21 21:44:57 +00:00
|
|
|
bool ret = r_io_write_at (core->io, addr, buf, size);
|
2018-02-18 01:05:04 +00:00
|
|
|
if (addr >= core->offset && addr <= core->offset + core->blocksize - 1) {
|
2017-08-22 07:42:16 +00:00
|
|
|
r_core_block_read (core);
|
2009-09-02 00:10:51 +00:00
|
|
|
}
|
2017-08-22 07:42:16 +00:00
|
|
|
return ret;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 12:22:01 +00:00
|
|
|
R_API bool r_core_extend_at(RCore *core, ut64 addr, int size) {
|
2021-01-06 22:44:18 +00:00
|
|
|
if (!core->io || !core->io->desc || size < 1) {
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2017-03-15 10:06:19 +00:00
|
|
|
}
|
2020-04-08 12:22:01 +00:00
|
|
|
int io_va = r_config_get_i (core->config, "io.va");
|
|
|
|
if (io_va) {
|
2021-03-12 15:35:12 +00:00
|
|
|
RIOMap *map = r_io_map_get_at (core->io, core->offset);
|
2020-04-08 12:22:01 +00:00
|
|
|
if (map) {
|
2020-12-08 14:57:08 +00:00
|
|
|
addr = addr - r_io_map_begin (map) + map->delta;
|
2017-03-15 10:06:19 +00:00
|
|
|
}
|
2020-04-08 12:22:01 +00:00
|
|
|
r_config_set_i (core->config, "io.va", false);
|
2014-03-25 03:00:26 +00:00
|
|
|
}
|
2020-04-08 12:22:01 +00:00
|
|
|
int ret = r_io_extend_at (core->io, addr, size);
|
|
|
|
if (addr >= core->offset && addr <= core->offset+core->blocksize) {
|
|
|
|
r_core_block_read (core);
|
|
|
|
}
|
|
|
|
r_config_set_i (core->config, "io.va", io_va);
|
|
|
|
return ret;
|
2014-03-25 03:00:26 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 16:06:22 +00:00
|
|
|
R_API int r_core_shift_block(RCore *core, ut64 addr, ut64 b_size, st64 dist) {
|
2014-03-27 03:39:56 +00:00
|
|
|
// bstart - block start, fstart file start
|
2014-06-14 00:09:41 +00:00
|
|
|
ut64 fend = 0, fstart = 0, bstart = 0, file_sz = 0;
|
2014-03-27 03:39:56 +00:00
|
|
|
ut8 * shift_buf = NULL;
|
2015-09-14 10:35:38 +00:00
|
|
|
int res = false;
|
2014-03-27 03:39:56 +00:00
|
|
|
|
2021-01-06 22:44:18 +00:00
|
|
|
if (!core->io || !core->io->desc) {
|
2017-03-15 10:06:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-27 03:39:56 +00:00
|
|
|
if (b_size == 0 || b_size == (ut64) -1) {
|
2021-01-06 22:44:18 +00:00
|
|
|
r_io_use_fd (core->io, core->io->desc->fd);
|
2014-03-27 03:39:56 +00:00
|
|
|
file_sz = r_io_size (core->io);
|
2017-08-25 21:45:37 +00:00
|
|
|
if (file_sz == UT64_MAX) {
|
|
|
|
file_sz = 0;
|
|
|
|
}
|
|
|
|
#if 0
|
2014-03-27 03:39:56 +00:00
|
|
|
bstart = r_io_seek (core->io, addr, R_IO_SEEK_SET);
|
|
|
|
fend = r_io_seek (core->io, 0, R_IO_SEEK_END);
|
2017-08-25 21:45:37 +00:00
|
|
|
if (fend < 1) {
|
|
|
|
fend = 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
bstart = 0;
|
|
|
|
fend = file_sz;
|
|
|
|
#endif
|
2014-03-28 17:04:57 +00:00
|
|
|
fstart = file_sz - fend;
|
2014-03-27 03:39:56 +00:00
|
|
|
b_size = fend > bstart ? fend - bstart: 0;
|
|
|
|
}
|
|
|
|
|
2017-08-27 11:42:16 +00:00
|
|
|
if ((st64)b_size < 1) {
|
2017-04-17 17:59:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-25 21:45:37 +00:00
|
|
|
shift_buf = calloc (b_size, 1);
|
|
|
|
if (!shift_buf) {
|
2022-08-22 10:21:33 +00:00
|
|
|
R_LOG_ERROR ("Cannot allocate %d byte(s)", (int)b_size);
|
2017-08-25 21:45:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-03-27 03:39:56 +00:00
|
|
|
|
2014-03-28 17:04:57 +00:00
|
|
|
// cases
|
2014-03-27 03:39:56 +00:00
|
|
|
// addr + b_size + dist > file_end
|
2022-08-18 12:37:29 +00:00
|
|
|
//if ((addr+b_size) + dist > file_end ) {
|
2015-09-14 10:35:38 +00:00
|
|
|
// res = false;
|
2014-03-27 03:39:56 +00:00
|
|
|
//}
|
|
|
|
// addr + b_size + dist < file_start (should work since dist is signed)
|
2022-08-18 12:37:29 +00:00
|
|
|
//else if ((addr+b_size) + dist < 0 ) {
|
2015-09-14 10:35:38 +00:00
|
|
|
// res = false;
|
2014-03-27 03:39:56 +00:00
|
|
|
//}
|
|
|
|
// addr + dist < file_start
|
2017-03-15 10:06:19 +00:00
|
|
|
if (addr + dist < fstart) {
|
2015-09-14 10:35:38 +00:00
|
|
|
res = false;
|
2014-03-27 03:39:56 +00:00
|
|
|
// addr + dist > file_end
|
2022-08-18 12:37:29 +00:00
|
|
|
} else if ((addr) + dist > fend) {
|
2015-09-14 10:35:38 +00:00
|
|
|
res = false;
|
2014-03-27 03:39:56 +00:00
|
|
|
} else {
|
|
|
|
r_io_read_at (core->io, addr, shift_buf, b_size);
|
2017-08-25 21:45:37 +00:00
|
|
|
r_io_write_at (core->io, addr + dist, shift_buf, b_size);
|
2015-09-14 10:35:38 +00:00
|
|
|
res = true;
|
2014-03-27 03:39:56 +00:00
|
|
|
}
|
2020-04-17 10:53:35 +00:00
|
|
|
r_core_seek (core, addr, true);
|
2014-03-28 17:04:57 +00:00
|
|
|
free (shift_buf);
|
2014-03-27 03:39:56 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-08-15 18:56:23 +00:00
|
|
|
R_API int r_core_block_read(RCore *core) {
|
2022-09-23 19:37:50 +00:00
|
|
|
int res = -1;
|
|
|
|
R_CRITICAL_ENTER (core);
|
2017-08-22 07:42:16 +00:00
|
|
|
if (core && core->block) {
|
2022-09-23 19:37:50 +00:00
|
|
|
res = r_io_read_at (core->io, core->offset, core->block, core->blocksize);
|
2011-02-12 00:52:41 +00:00
|
|
|
}
|
2022-09-23 19:37:50 +00:00
|
|
|
R_CRITICAL_LEAVE (core);
|
|
|
|
return res;
|
2009-04-07 11:28:22 +00:00
|
|
|
}
|