2019-03-29 13:03:17 +01:00
|
|
|
/* radare - LGPL - Copyright 2010-2019 - nibble, pancake */
|
2010-05-24 18:35:08 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <r_anal.h>
|
|
|
|
#include <r_list.h>
|
|
|
|
#include <r_util.h>
|
2010-05-25 20:12:49 +02:00
|
|
|
#include <r_core.h>
|
2010-05-24 19:08:38 +02:00
|
|
|
|
2014-12-22 11:04:12 +01:00
|
|
|
R_API int r_core_gdiff_fcn(RCore *c, ut64 addr, ut64 addr2) {
|
|
|
|
RList *la, *lb;
|
2020-01-15 10:58:30 +01:00
|
|
|
RAnalFunction *fa = r_anal_get_function_at (c->anal, addr);
|
|
|
|
RAnalFunction *fb = r_anal_get_function_at (c->anal, addr2);
|
2018-05-19 17:16:13 +02:00
|
|
|
if (!fa || !fb) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-17 16:03:12 +02:00
|
|
|
RAnalBlock *bb;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (fa->bbs, iter, bb) {
|
|
|
|
r_anal_diff_fingerprint_bb (c->anal, bb);
|
|
|
|
}
|
|
|
|
r_list_foreach (fb->bbs, iter, bb) {
|
|
|
|
r_anal_diff_fingerprint_bb (c->anal, bb);
|
|
|
|
}
|
2014-12-22 11:04:12 +01:00
|
|
|
la = r_list_new ();
|
|
|
|
r_list_append (la, fa);
|
|
|
|
lb = r_list_new ();
|
|
|
|
r_list_append (lb, fb);
|
|
|
|
r_anal_diff_fcn (c->anal, la, lb);
|
|
|
|
r_list_free (la);
|
|
|
|
r_list_free (lb);
|
2018-05-19 17:16:13 +02:00
|
|
|
return true;
|
2014-12-22 11:04:12 +01:00
|
|
|
}
|
|
|
|
|
2016-04-07 13:20:11 +05:30
|
|
|
/* Fingerprint functions and blocks, then diff. */
|
2016-03-22 12:55:36 +01:00
|
|
|
R_API int r_core_gdiff(RCore *c, RCore *c2) {
|
2010-12-06 03:34:44 +01:00
|
|
|
RCore *cores[2] = {c, c2};
|
2012-07-22 12:00:35 +04:00
|
|
|
RAnalFunction *fcn;
|
2010-05-24 18:35:08 +02:00
|
|
|
RAnalBlock *bb;
|
2010-12-24 16:58:27 +01:00
|
|
|
RListIter *iter, *iter2;
|
2010-05-24 18:35:08 +02:00
|
|
|
int i;
|
|
|
|
|
2017-12-22 15:55:45 +01:00
|
|
|
if (!c || !c2) {
|
2015-09-14 12:35:38 +02:00
|
|
|
return false;
|
2017-12-22 15:55:45 +01:00
|
|
|
}
|
2010-05-24 18:35:08 +02:00
|
|
|
for (i = 0; i < 2; i++) {
|
2016-03-22 01:31:10 +01:00
|
|
|
/* remove strings */
|
|
|
|
r_list_foreach_safe (cores[i]->anal->fcns, iter, iter2, fcn) {
|
|
|
|
if (!strncmp (fcn->name, "str.", 4)) {
|
2020-01-15 10:58:30 +01:00
|
|
|
r_anal_function_delete (fcn);
|
2016-03-22 01:31:10 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-01 16:14:08 +05:30
|
|
|
/* Fingerprint fcn bbs (functions basic-blocks) */
|
2012-02-14 18:19:16 +01:00
|
|
|
r_list_foreach (cores[i]->anal->fcns, iter, fcn) {
|
|
|
|
r_list_foreach (fcn->bbs, iter2, bb) {
|
2011-03-06 15:21:13 +01:00
|
|
|
r_anal_diff_fingerprint_bb (cores[i]->anal, bb);
|
2010-12-04 15:14:53 +01:00
|
|
|
}
|
2010-05-24 18:35:08 +02:00
|
|
|
}
|
2011-03-06 15:21:13 +01:00
|
|
|
/* Fingerprint fcn */
|
2012-02-14 18:19:16 +01:00
|
|
|
r_list_foreach (cores[i]->anal->fcns, iter, fcn) {
|
2020-01-15 10:58:30 +01:00
|
|
|
r_anal_diff_fingerprint_fcn (cores[i]->anal, fcn);
|
2010-12-04 15:14:53 +01:00
|
|
|
}
|
2010-05-24 18:35:08 +02:00
|
|
|
}
|
|
|
|
/* Diff functions */
|
2011-03-06 15:21:13 +01:00
|
|
|
r_anal_diff_fcn (cores[0]->anal, cores[0]->anal->fcns, cores[1]->anal->fcns);
|
2010-05-24 18:35:08 +02:00
|
|
|
|
2015-09-14 12:35:38 +02:00
|
|
|
return true;
|
2010-05-24 18:35:08 +02:00
|
|
|
}
|
2011-10-16 13:59:05 +02:00
|
|
|
|
|
|
|
/* copypasta from radiff2 */
|
2015-10-27 05:13:46 +08:00
|
|
|
static void diffrow(ut64 addr, const char *name, ut32 size, int maxnamelen,
|
|
|
|
int digits, ut64 addr2, const char *name2, ut32 size2,
|
|
|
|
const char *match, double dist, int bare) {
|
2014-12-26 11:24:49 +10:30
|
|
|
if (bare) {
|
2016-09-22 20:31:40 +02:00
|
|
|
if (addr2 == UT64_MAX || !name2) {
|
2014-12-26 11:24:49 +10:30
|
|
|
printf ("0x%016"PFMT64x" |%8s (%f)\n", addr, match, dist);
|
2016-09-22 20:31:40 +02:00
|
|
|
} else {
|
|
|
|
printf ("0x%016"PFMT64x" |%8s (%f) | 0x%016"PFMT64x"\n", addr, match, dist, addr2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (addr2 == UT64_MAX || !name2) {
|
|
|
|
printf ("%*s %*d 0x%"PFMT64x" |%8s (%f)\n",
|
|
|
|
maxnamelen, name, digits, size, addr, match, dist);
|
|
|
|
} else {
|
|
|
|
printf ("%*s %*d 0x%"PFMT64x" |%8s (%f) | 0x%"PFMT64x" %*d %s\n",
|
|
|
|
maxnamelen, name, digits, size, addr, match, dist, addr2,
|
|
|
|
digits, size2, name2);
|
|
|
|
}
|
2014-12-26 11:24:49 +10:30
|
|
|
}
|
2011-10-16 13:59:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_diff_show(RCore *c, RCore *c2) {
|
2016-09-22 23:57:16 +02:00
|
|
|
bool bare = r_config_get_i (c->config, "diff.bare") || r_config_get_i (c2->config, "diff.bare");
|
|
|
|
RList *fcns = r_anal_get_fcns (c->anal);
|
2011-10-16 13:59:05 +02:00
|
|
|
const char *match;
|
|
|
|
RListIter *iter;
|
2012-07-22 12:00:35 +04:00
|
|
|
RAnalFunction *f;
|
2013-11-18 15:24:44 +04:00
|
|
|
int maxnamelen = 0;
|
2020-01-15 10:58:30 +01:00
|
|
|
ut64 maxsize = 0;
|
2015-10-27 05:13:46 +08:00
|
|
|
int digits = 1;
|
2013-11-18 15:24:44 +04:00
|
|
|
int len;
|
2016-09-22 23:57:16 +02:00
|
|
|
|
2013-11-18 15:24:44 +04:00
|
|
|
r_list_foreach (fcns, iter, f) {
|
2016-09-22 20:31:40 +02:00
|
|
|
if (f->name && (len = strlen (f->name)) > maxnamelen) {
|
2013-11-18 15:24:44 +04:00
|
|
|
maxnamelen = len;
|
2016-09-22 20:31:40 +02:00
|
|
|
}
|
2020-01-15 10:58:30 +01:00
|
|
|
if (r_anal_function_linear_size (f) > maxsize) {
|
|
|
|
maxsize = r_anal_function_linear_size (f);
|
2016-09-22 20:31:40 +02:00
|
|
|
}
|
2013-11-18 15:24:44 +04:00
|
|
|
}
|
|
|
|
fcns = r_anal_get_fcns (c2->anal);
|
|
|
|
r_list_foreach (fcns, iter, f) {
|
2016-09-22 20:31:40 +02:00
|
|
|
if (f->name && (len = strlen (f->name)) > maxnamelen) {
|
2013-11-18 15:24:44 +04:00
|
|
|
maxnamelen = len;
|
2016-09-22 20:31:40 +02:00
|
|
|
}
|
2020-01-15 10:58:30 +01:00
|
|
|
if (r_anal_function_linear_size (f) > maxsize) {
|
|
|
|
maxsize = r_anal_function_linear_size (f);
|
2016-09-22 20:31:40 +02:00
|
|
|
}
|
2015-10-27 05:13:46 +08:00
|
|
|
}
|
|
|
|
while (maxsize > 9) {
|
|
|
|
maxsize /= 10;
|
|
|
|
digits++;
|
2013-11-18 15:24:44 +04:00
|
|
|
}
|
2016-09-22 20:31:40 +02:00
|
|
|
|
2013-11-18 15:24:44 +04:00
|
|
|
fcns = r_anal_get_fcns (c->anal);
|
2016-09-22 20:31:40 +02:00
|
|
|
if (r_list_empty (fcns)) {
|
|
|
|
eprintf ("No functions found, try running with -A or load a project\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
r_list_sort (fcns, c->anal->columnSort);
|
|
|
|
|
2011-10-16 13:59:05 +02:00
|
|
|
r_list_foreach (fcns, iter, f) {
|
|
|
|
switch (f->type) {
|
|
|
|
case R_ANAL_FCN_TYPE_FCN:
|
|
|
|
case R_ANAL_FCN_TYPE_SYM:
|
|
|
|
switch (f->diff->type) {
|
|
|
|
case R_ANAL_DIFF_TYPE_MATCH:
|
|
|
|
match = "MATCH";
|
|
|
|
break;
|
|
|
|
case R_ANAL_DIFF_TYPE_UNMATCH:
|
|
|
|
match = "UNMATCH";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
match = "NEW";
|
2016-09-22 23:57:16 +02:00
|
|
|
f->diff->dist = 0;
|
2011-10-16 13:59:05 +02:00
|
|
|
}
|
2020-01-15 10:58:30 +01:00
|
|
|
diffrow (f->addr, f->name, r_anal_function_linear_size (f), maxnamelen, digits,
|
|
|
|
f->diff->addr, f->diff->name, f->diff->size,
|
|
|
|
match, f->diff->dist, bare);
|
2011-10-16 13:59:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fcns = r_anal_get_fcns (c2->anal);
|
2016-09-22 20:31:40 +02:00
|
|
|
r_list_sort (fcns, c2->anal->columnSort);
|
2011-10-16 13:59:05 +02:00
|
|
|
r_list_foreach (fcns, iter, f) {
|
|
|
|
switch (f->type) {
|
|
|
|
case R_ANAL_FCN_TYPE_FCN:
|
|
|
|
case R_ANAL_FCN_TYPE_SYM:
|
2016-09-22 20:31:40 +02:00
|
|
|
if (f->diff->type == R_ANAL_DIFF_TYPE_NULL) {
|
2020-01-15 10:58:30 +01:00
|
|
|
diffrow (f->addr, f->name, r_anal_function_linear_size (f), maxnamelen,
|
|
|
|
digits, f->diff->addr, f->diff->name, f->diff->size,
|
|
|
|
"NEW", 0, bare); //f->diff->dist, bare);
|
2016-09-22 20:31:40 +02:00
|
|
|
}
|
|
|
|
break;
|
2011-10-16 13:59:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|