diff --git a/libr/anal/function.c b/libr/anal/function.c index 82a826e74be8d..31a6c275791e3 100644 --- a/libr/anal/function.c +++ b/libr/anal/function.c @@ -94,7 +94,6 @@ R_API void r_anal_function_free(void *_fcn) { free (fcn->name); free (fcn->attr); - r_list_free (fcn->fcn_locs); fcn->bbs = NULL; free (fcn->fingerprint); r_anal_diff_free (fcn->diff); @@ -286,11 +285,6 @@ R_API ut64 r_anal_function_realsize(const RAnalFunction *fcn) { r_list_foreach (fcn->bbs, iter, bb) { sz += bb->size; } - r_list_foreach (fcn->fcn_locs, fiter, f) { - r_list_foreach (f->bbs, iter, bb) { - sz += bb->size; - } - } } return sz; } diff --git a/libr/core/canal.c b/libr/core/canal.c index abf8c7518e42d..7d5312a665542 100644 --- a/libr/core/canal.c +++ b/libr/core/canal.c @@ -839,15 +839,6 @@ static int __core_anal_fcn(RCore *core, ut64 at, ut64 from, int reftype, int dep /* New function: Add initial xref */ if (from != UT64_MAX) { - if (fcn->type == R_ANAL_FCN_TYPE_LOC) { - RAnalFunction *f = r_anal_get_fcn_in (core->anal, from, -1); - if (f) { - if (!f->fcn_locs) { - f->fcn_locs = r_list_new (); - } - r_list_add_sorted (f->fcn_locs, fcn, &cmpfcn); - } - } r_anal_xrefs_set (core->anal, from, fcn->addr, reftype); } // XXX: this is wrong. See CID 1134565 diff --git a/libr/core/cmd_print.c b/libr/core/cmd_print.c index b980d0a78d6e6..d07759fd4b380 100644 --- a/libr/core/cmd_print.c +++ b/libr/core/cmd_print.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2019 - pancake */ +/* radare - LGPL - Copyright 2009-2020 - pancake */ #include "r_asm.h" #include "r_core.h" @@ -3918,11 +3918,6 @@ static void _pointer_table(RCore *core, ut64 origin, ut64 offset, const ut8 *buf } } -// TODO: this function is a temporary fix. All analysis should be based on realsize. However, now for same architectures realisze is not used -static ut32 tmp_get_contsize(RAnalFunction *f) { - return r_anal_function_linear_size (f); -} - static void __printPattern(RCore *core, const char *_input) { char *input = strdup (_input); const char *arg = r_str_nextword (input, ' '); @@ -4130,123 +4125,6 @@ dsmap { #endif #define P(x) (core->cons && core->cons->context->pal.x)? core->cons->context->pal.x -#if 0 -static void disasm_recursive_old(RCore *core, ut64 addr, char type_print) { - bool push[512]; - int pushes = 0; - RAnalOp aop = {0}; - int i, j, ret; - ut8 *buf = calloc (core->blocksize, 1); // begin of instruction - if (!buf) { - return; - } - ut8 *raw = calloc (core->blocksize, 1); // instruction coverage - if (!raw) { - free (buf); - return; - } - int count = 64; - int base = 0; - for (i = 0; count > 0 && i < core->blocksize; i++) { - r_anal_op_fini (&aop); - ret = r_anal_op (core->anal, &aop, addr + i, core->block + i , core->blocksize - i); - if (ret < 0 || aop.size < 1) { - continue; - } - buf[i] = 1; - for (j = i; j < i + aop.size; j++) { - raw[j] = 1; - } - // r_core_cmdf (core, "pd 1 @ 0x%08"PFMT64x, addr + i); - switch (aop.type) { - case R_ANAL_OP_TYPE_JMP: - r_core_cmdf (core, "pD %d @ 0x%08"PFMT64x, i + aop.size - base, addr + base); - i = aop.jump - addr - 1; - base = i + 1; - count--; - continue; - break; -#if 0 - case R_ANAL_OP_TYPE_CJMP: - if (aop.jump > addr + i) { - if (pushes > 500) { - eprintf ("Too deep\n"); - } - push[pushes++] = i + aop.size; - i = aop.jump - addr - 1; - continue; - } - break; -#endif - case R_ANAL_OP_TYPE_UCJMP: - break; - } - i += aop.size - 1; - } -#if 0 - if (base < i) { -r_cons_printf ("base:\n"); - r_core_cmdf (core, "pD %d @ 0x%08"PFMT64x, i - base, addr + base); //+ aop.size - base, addr + base); -r_cons_printf ("base:\n"); - - } -#endif - // unlikely - int p; - for (p = 0; pblocksize; i++) { - if (buf[i]) { - break; - } - buf[i] = 1; - r_anal_op_fini (&aop); - ret = r_anal_op (core->anal, &aop, addr + i, core->block + i , core->blocksize - i); - if (ret < 0 || aop.size < 1) { - continue; - } - // r_core_cmdf (core, "pd 1 @ 0x%08"PFMT64x, addr + i); - switch (aop.type) { - case R_ANAL_OP_TYPE_JMP: - if (aop.jump > addr + i) { - r_core_cmdf (core, "pD %d @ 0x%08"PFMT64x, i + aop.size - base, addr + base); - i = aop.jump - addr - 1; - base = i + 1; - continue; - } - break; -#if 0 - case R_ANAL_OP_TYPE_CJMP: - if (aop.jump > addr + i) { - if (pushes > 500) { - eprintf ("Too deep\n"); - } - push[pushes++] = i + aop.size; - i = aop.jump - addr - 1; - continue; - } - break; -#endif - case R_ANAL_OP_TYPE_UCJMP: - break; - } - i += aop.size - 1; - } - } -#if 0 - // linear disasm - for (i = 0; i< core->blocksize; i++) { - if (!buf[i]) { - continue; - } - r_core_cmdf (core, "pd 1 @ 0x%08"PFMT64x, addr + i); - r_anal_op_fini (&aop); - ret = r_anal_op (core->anal, &aop, addr + i, core->block + i , core->blocksize - i); - i += aop.size - } -#endif -} -#endif static void disasm_until_ret(RCore *core, ut64 addr, char type_print, const char *arg) { int p = 0; @@ -4367,94 +4245,13 @@ static void disasm_recursive(RCore *core, ut64 addr, int count, char type_print) } } -#if 0 -static void _disasm_recursive(RCore *core, ut64 addr, char type_print) { - bool show_flags = r_config_get_i (core->config, "asm.flags"); - bool show_bytes = r_config_get_i (core->config, "asm.bytes"); - bool show_offset = r_config_get_i (core->config, "asm.offset"); - bool show_imtrim = r_config_get_i (core->config, "asm.imm.trim"); - Sdb *db = sdb_new0 (); - RAsmOp asmop = {0}; - RAnalOp aop = {0}; - int i, ret; - ut8 *buf = core->block; - int loop, len = core->blocksize; - for (loop = 0; loop < 2 ; loop ++) { - for (i = 0; i < len; i+= aop.size) { - r_anal_op_fini (&aop); - r_asm_set_pc (core->assembler, addr + i); - //if (i + 8 >= len) { - // eprintf ("WARNING: block size is too small\n"); - // TODO: reimplement using dynamic memory accesses, not just the block - // break; - //} - ret = r_asm_disassemble (core->assembler, &asmop, buf + i, len - i); - if (ret < 0) { - asmop.size = 1; - continue; - } - ret = r_anal_op (core->anal, &aop, addr + i, buf + i , len - i); - if (ret < 0) { - aop.size = 1; - continue; - } - if (loop > 0) { - if (show_flags) { - const char *x = sdb_const_get (db, sdb_fmt ("label.0x%"PFMT64x, addr + i), NULL); - if (x) { - r_cons_printf ("%s:\n", x); - } - } - char *asm_str = asmop.buf_asm; - char *color_reg = P(reg): Color_YELLOW; - char *color_num = P(num): Color_CYAN; - asm_str = r_print_colorize_opcode (core->print, asm_str, color_reg, color_num, false); - if (show_imtrim) { - r_parse_immtrim (asm_str); - } - if (show_offset) { - r_print_offset (core->print, addr + i, 0, 0, 0, 0, NULL); - } - if (show_bytes) { - char *hexstr = r_print_hexpair (core->print, asmop.buf_hex, -1); - const char *pad = r_str_pad (' ', 20 - strlen (asmop.buf_hex)); - r_cons_printf (" %s%s %s\n", pad, hexstr, asm_str); - free (hexstr); - } else { - r_cons_printf (" %s\n", asm_str); - } - } - switch (aop.type) { - case R_ANAL_OP_TYPE_JMP: - break; - case R_ANAL_OP_TYPE_CALL: - case R_ANAL_OP_TYPE_CJMP: - sdb_set (db, sdb_fmt ("label.0x%"PFMT64x, aop.jump), - sdb_fmt ("from.0x%"PFMT64x, addr + i), 0); - break; - } - if (aop.size < 1) { - aop.size = 1; - } - } - } - r_anal_op_fini (&aop); - sdb_free (db); -} -#endif - static void func_walk_blocks(RCore *core, RAnalFunction *f, char input, char type_print, bool fromHere) { RListIter *iter; RAnalBlock *b = NULL; - RAnalFunction *tmp_func; - RListIter *locs_it = NULL; const char *orig_bb_middle = r_config_get (core->config, "asm.bb.middle"); r_config_set_i (core->config, "asm.bb.middle", false); PJ *pj = NULL; - if (f->fcn_locs) { - locs_it = f->fcn_locs->head; - } // XXX: hack must be reviewed/fixed in code analysis if (!b) { if (r_list_length (f->bbs) >= 1) { @@ -4472,33 +4269,6 @@ static void func_walk_blocks(RCore *core, RAnalFunction *f, char input, char typ return; } pj_a (pj); - for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) { - if (r_cons_is_breaked ()) { - break; - } - if (fromHere) { - if (b->addr < core->offset) { - core->cons->null = true; - } else { - core->cons->null = false; - } - } - if (tmp_func->addr > f->addr) { - break; - } - r_list_foreach (tmp_func->bbs, iter, b) { -// const char *cmd = (type_print == 'D')? "pDj": "pIj"; -// r_core_cmdf (core, "%s %d @ 0x%"PFMT64x, cmd, b->size, b->addr); - ut8 *buf = malloc (b->size); - if (buf) { - r_io_read_at (core->io, b->addr, buf, b->size); - r_core_print_disasm_json (core, b->addr, buf, b->size, 0, pj); - free (buf); - } else { - eprintf ("cannot allocate %"PFMT64u" byte(s)\n", b->size); - } - } - } r_list_foreach (f->bbs, iter, b) { if (fromHere) { if (b->addr < core->offset) { @@ -4507,11 +4277,6 @@ static void func_walk_blocks(RCore *core, RAnalFunction *f, char input, char typ core->cons->null = false; } } -#if 0 - r_core_print_disasm_json (core, core->offset, buf, bsize, 0); - const char *cmd = (type_print == 'D')? "pDj": "pIj"; - r_core_cmdf (core, "%s %d @ 0x%"PFMT64x, cmd, b->size, b->addr); -#endif ut8 *buf = malloc (b->size); if (buf) { r_io_read_at (core->io, b->addr, buf, b->size); @@ -4521,33 +4286,6 @@ static void func_walk_blocks(RCore *core, RAnalFunction *f, char input, char typ eprintf ("cannot allocate %"PFMT64u" byte(s)\n", b->size); } } - for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) { - if (r_cons_is_breaked ()) { - break; - } - r_list_foreach (tmp_func->bbs, iter, b) { - if (fromHere) { - if (b->addr < core->offset) { - core->cons->null = true; - } else { - core->cons->null = false; - } - } -#if 0 - const char *cmd = (type_print == 'D')? "pDj": "pIj"; - r_core_cmdf (core, "%s %d @0x%"PFMT64x, cmd, b->size, b->addr); -#endif - ut8 *buf = malloc (b->size); - if (buf) { - r_io_read_at (core->io, b->addr, buf, b->size); - r_core_print_disasm_json (core, b->addr, buf, b->size, 0, pj); - free (buf); - } else { - eprintf ("cannot allocate %"PFMT64u" byte(s)\n", b->size); - } - } - } - pj_end (pj); r_cons_printf ("%s", pj_string (pj)); pj_free (pj); @@ -4562,26 +4300,9 @@ static void func_walk_blocks(RCore *core, RAnalFunction *f, char input, char typ saved_arena = r_reg_arena_peek (core->anal->reg); } r_config_set_i (core->config, "asm.lines.bb", 0); - for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) { - if (tmp_func->addr >= f->addr) { - break; - } - r_list_foreach (tmp_func->bbs, iter, b) { - pr_bb (core, tmp_func, b, emu, saved_gp, saved_arena, type_print, fromHere); - } - } r_list_foreach (f->bbs, iter, b) { pr_bb (core, f, b, emu, saved_gp, saved_arena, type_print, fromHere); } - for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) { - if (r_cons_is_breaked ()) { - break; - } - // this should be more advanced - r_list_foreach (tmp_func->bbs, iter, b) { - pr_bb (core, tmp_func, b, emu, saved_gp, saved_arena, type_print, fromHere); - } - } if (emu) { core->anal->gp = saved_gp; if (saved_arena) { @@ -4825,7 +4546,6 @@ R_API void r_print_code(RPrint *p, ut64 addr, const ut8 *buf, int len, char lang } static void print_json_string(RCore *core, const char* block, int len, const char* type) { - char *str; const char* section_name = r_core_get_section_name (core, core->offset); if (section_name && strlen (section_name) < 1) { section_name = "unknown"; @@ -4836,13 +4556,6 @@ static void print_json_string(RCore *core, const char* block, int len, const cha for (p = (char*) section_name; *p && *p != ' '; p++) {} *p = '\0'; } - - r_cons_printf ("{\"string\":"); - str = r_str_utf16_encode (block, len); - r_cons_printf ("\"%s\"", str); - r_cons_printf (",\"offset\":%"PFMT64u, core->offset); - r_cons_printf (",\"section\":\"%s\"", section_name); - r_cons_printf (",\"length\":%d", len); if (!type) { switch (get_string_type (core->block, len)) { case 'w': type = "wide"; break; @@ -4851,8 +4564,29 @@ static void print_json_string(RCore *core, const char* block, int len, const cha default: type = "unknown"; break; } } - r_cons_printf (",\"type\":\"%s\"}", type); +#if 0 + PJ *pj = pj_new (); + pj_o (pj); + // TODO: add pj_kd for data to pass key(string) and value(data,len) instead of pj_ks which null terminates + pj_ks (pj, "string", str); + pj_kn (pj, "offset", core->offset); + pj_ks (pj, "section", section_name); + pj_ki (pj, "legnth", len); + r_cons_printf (",\"length\":%d", len); + pj_ks (pj, "type", type); + pj_end (pj); + r_cons_printf ("%s\n", pj_string (pj)); + pj_free (pj); +#else + r_cons_printf ("{\"string\":"); + char *str = r_str_utf16_encode (block, len); // XXX just block + len should be fine, pj takes care of this + r_cons_printf ("\"%s\"", str); free (str); + r_cons_printf (",\"offset\":%"PFMT64u, core->offset); + r_cons_printf (",\"section\":\"%s\"", section_name); + r_cons_printf (",\"length\":%d", len); + r_cons_printf (",\"type\":\"%s\"}", type); +#endif } static char *__op_refs(RCore *core, RAnalOp *op, int n) { @@ -5352,7 +5086,6 @@ static int cmd_print(void *data, const char *input) { } else if (input[2] == 'j') { r_core_cmdf (core, "pdfj%s", input + 3); } else if (input[2] == 'c') { // "pifc" - RListIter *iter; RAnalRef *refi; RList *refs = NULL; @@ -5683,14 +5416,8 @@ static int cmd_print(void *data, const char *input) { if (!f) { f = r_anal_get_fcn_in (core->anal, core->offset, 0); } - RAnalFunction *tmp_func; - ut32 cont_size = 0; RListIter *locs_it = NULL; - if (f && f->fcn_locs) { - locs_it = f->fcn_locs->head; - } if (f && input[2] == 'j') { // "pdfj" - ut8 *loc_buf = NULL; RAnalBlock *b; ut32 fcn_size = r_anal_function_realsize (f); const char *orig_bb_middle = r_config_get (core->config, "asm.bb.middle"); @@ -5705,22 +5432,9 @@ static int cmd_print(void *data, const char *input) { pj_kn (pj, "addr", f->addr); pj_k (pj, "ops"); pj_a (pj); - // instructions are all outputted as a json list - // DEAD CODE cont_size = f->_size > 0 ? f->_size : r_anal_function_realsize (f); - // TODO: can loc jump to another locs? - for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) { - if (tmp_func->addr > f->addr) { - break; - } - cont_size = tmp_get_contsize (tmp_func); - loc_buf = calloc (cont_size, 1); - r_io_read_at (core->io, tmp_func->addr, loc_buf, cont_size); - r_core_print_disasm_json (core, tmp_func->addr, loc_buf, cont_size, 0, pj); - free (loc_buf); - } r_list_sort (f->bbs, bb_cmpaddr); r_list_foreach (f->bbs, locs_it, b) { - + ut8 *buf = malloc (b->size); if (buf) { r_io_read_at (core->io, b->addr, buf, b->size); @@ -5730,15 +5444,6 @@ static int cmd_print(void *data, const char *input) { eprintf ("cannot allocate %"PFMT64u" byte(s)\n", b->size); } } - for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) { - cont_size = tmp_get_contsize (tmp_func); - loc_buf = calloc (cont_size, 1); - if (loc_buf) { - r_io_read_at (core->io, tmp_func->addr, loc_buf, cont_size); - r_core_print_disasm_json (core, tmp_func->addr, loc_buf, cont_size, 0, pj); - free (loc_buf); - } - } pj_end (pj); pj_end (pj); r_cons_printf ("%s\n", pj_string (pj)); @@ -5746,16 +5451,6 @@ static int cmd_print(void *data, const char *input) { pd_result = 0; r_config_set (core->config, "asm.bb.middle", orig_bb_middle); } else if (f) { -#if 0 - for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) { - if (tmp_func->addr > f->addr) { - break; - } - cont_size = tmp_get_contsize (tmp_func); - r_core_cmdf (core, "pD %d @ 0x%08" PFMT64x, cont_size, tmp_func->addr); - } - cont_size = tmp_get_contsize (f); -#endif ut64 linearsz = r_anal_function_linear_size (f); ut64 realsz = r_anal_function_realsize (f); if (realsz + 4096 < linearsz) { @@ -5764,17 +5459,13 @@ static int cmd_print(void *data, const char *input) { ut64 at = f->addr; // TODO: should be min from r_anal_fcn_get_range()? ut64 sz = R_MAX (linearsz, realsz); ut8 *buf = calloc (sz, 1); - (void)r_io_read_at (core->io, at, buf, sz); - core->num->value = r_core_print_disasm (core->print, core, at, buf, sz, sz, 0, 1, 0, NULL, f); - free (buf); - // r_core_cmdf (core, "pD %d @ 0x%08" PFMT64x, f->_size > 0 ? f->_size: r_anal_function_realsize (f), f->addr); - } -#if 0 - for (; locs_it && (tmp_func = locs_it->data); locs_it = locs_it->n) { - cont_size = tmp_get_contsize (tmp_func); - r_core_cmdf (core, "pD %d @ 0x%08" PFMT64x, cont_size, tmp_func->addr); + if (buf) { + (void)r_io_read_at (core->io, at, buf, sz); + core->num->value = r_core_print_disasm (core->print, core, at, buf, sz, sz, 0, 1, 0, NULL, f); + free (buf); + // r_core_cmdf (core, "pD %d @ 0x%08" PFMT64x, f->_size > 0 ? f->_size: r_anal_function_realsize (f), f->addr); + } } -#endif pd_result = 0; } else { eprintf ("pdf: Cannot find function at 0x%08"PFMT64x "\n", core->offset); @@ -5921,7 +5612,7 @@ l = use_blocksize; } free (block1); if (formatted_json) { - r_cons_print ("\n"); + r_cons_newline (); } } if (processed_cmd) { diff --git a/libr/include/r_anal.h b/libr/include/r_anal.h index 4cc06e3a41bcf..64b62fb7334f8 100644 --- a/libr/include/r_anal.h +++ b/libr/include/r_anal.h @@ -272,7 +272,6 @@ typedef struct r_anal_function_t { ut8 *fingerprint; // TODO: make is fuzzy and smarter size_t fingerprint_size; RAnalDiff *diff; - RList *fcn_locs; //sorted list of a function *.loc refs //RList *locals; // list of local labels -> moved to anal->sdb_fcns RList *bbs; // TODO: should be RPVector RAnalFcnMeta meta; diff --git a/test/new/db/anal/x86_32 b/test/new/db/anal/x86_32 index 6c16d5b03ecb1..11b1c4189f7d9 100644 --- a/test/new/db/anal/x86_32 +++ b/test/new/db/anal/x86_32 @@ -400,7 +400,7 @@ EXPECT=< 277 fcn.0040103b +0x0040103b 3 15 fcn.0040103b 0x00401335 3 262 loc.00401335 0x00404ad7 1 8 fcn.00404ad7 EOF @@ -418,7 +418,7 @@ EXPECT=< 277 root.0040103b +0x0040103b 3 15 root.0040103b 0x00401335 3 262 loc.00401335 0x00404ad7 1 8 root.00404ad7 EOF