2016-09-22 18:31:40 +00:00
|
|
|
/* radare - LGPL - Copyright 2010-2016 - nibble, pancake */
|
2010-05-24 16:35:08 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <r_anal.h>
|
|
|
|
#include <r_list.h>
|
|
|
|
#include <r_util.h>
|
2010-05-25 18:12:49 +00:00
|
|
|
#include <r_core.h>
|
2010-05-24 17:08:38 +00:00
|
|
|
|
2014-12-22 10:04:12 +00:00
|
|
|
R_API int r_core_gdiff_fcn(RCore *c, ut64 addr, ut64 addr2) {
|
|
|
|
RList *la, *lb;
|
|
|
|
RAnalFunction *fa = r_anal_get_fcn_at (c->anal, addr, 0);
|
|
|
|
RAnalFunction *fb = r_anal_get_fcn_at (c->anal, addr2, 0);
|
|
|
|
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);
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2014-12-22 10:04:12 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 07:50:11 +00:00
|
|
|
/* Fingerprint functions and blocks, then diff. */
|
2016-03-22 11:55:36 +00:00
|
|
|
R_API int r_core_gdiff(RCore *c, RCore *c2) {
|
2010-12-06 02:34:44 +00:00
|
|
|
RCore *cores[2] = {c, c2};
|
2012-07-22 08:00:35 +00:00
|
|
|
RAnalFunction *fcn;
|
2010-05-24 16:35:08 +00:00
|
|
|
RAnalBlock *bb;
|
2010-12-24 15:58:27 +00:00
|
|
|
RListIter *iter, *iter2;
|
2010-05-24 16:35:08 +00:00
|
|
|
int i;
|
|
|
|
|
2014-11-24 04:53:14 +00:00
|
|
|
if (!c || !c2)
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2010-05-24 16:35:08 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2016-03-22 00:31:10 +00:00
|
|
|
/* remove strings */
|
|
|
|
r_list_foreach_safe (cores[i]->anal->fcns, iter, iter2, fcn) {
|
|
|
|
if (!strncmp (fcn->name, "str.", 4)) {
|
|
|
|
r_list_delete (cores[i]->anal->fcns, iter);
|
|
|
|
}
|
|
|
|
}
|
2016-04-01 10:44:08 +00:00
|
|
|
/* Fingerprint fcn bbs (functions basic-blocks) */
|
2012-02-14 17:19:16 +00:00
|
|
|
r_list_foreach (cores[i]->anal->fcns, iter, fcn) {
|
|
|
|
r_list_foreach (fcn->bbs, iter2, bb) {
|
2011-03-06 14:21:13 +00:00
|
|
|
r_anal_diff_fingerprint_bb (cores[i]->anal, bb);
|
2010-12-04 14:14:53 +00:00
|
|
|
}
|
2010-05-24 16:35:08 +00:00
|
|
|
}
|
2011-03-06 14:21:13 +00:00
|
|
|
/* Fingerprint fcn */
|
2012-02-14 17:19:16 +00:00
|
|
|
r_list_foreach (cores[i]->anal->fcns, iter, fcn) {
|
2016-05-15 12:37:22 +00:00
|
|
|
int newsize = r_anal_diff_fingerprint_fcn (cores[i]->anal, fcn);
|
|
|
|
r_anal_fcn_set_size (fcn, newsize);
|
2010-12-04 14:14:53 +00:00
|
|
|
}
|
2010-05-24 16:35:08 +00:00
|
|
|
}
|
|
|
|
/* Diff functions */
|
2011-03-06 14:21:13 +00:00
|
|
|
r_anal_diff_fcn (cores[0]->anal, cores[0]->anal->fcns, cores[1]->anal->fcns);
|
2010-05-24 16:35:08 +00:00
|
|
|
|
2015-09-14 10:35:38 +00:00
|
|
|
return true;
|
2010-05-24 16:35:08 +00:00
|
|
|
}
|
2011-10-16 11:59:05 +00:00
|
|
|
|
|
|
|
/* copypasta from radiff2 */
|
2015-10-26 21:13:46 +00: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 00:54:49 +00:00
|
|
|
if (bare) {
|
2016-09-22 18:31:40 +00:00
|
|
|
if (addr2 == UT64_MAX || !name2) {
|
2014-12-26 00:54:49 +00:00
|
|
|
printf ("0x%016"PFMT64x" |%8s (%f)\n", addr, match, dist);
|
2016-09-22 18:31:40 +00: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 00:54:49 +00:00
|
|
|
}
|
2011-10-16 11:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_diff_show(RCore *c, RCore *c2) {
|
2016-09-22 21:57:16 +00: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 11:59:05 +00:00
|
|
|
const char *match;
|
|
|
|
RListIter *iter;
|
2012-07-22 08:00:35 +00:00
|
|
|
RAnalFunction *f;
|
2013-11-18 11:24:44 +00:00
|
|
|
int maxnamelen = 0;
|
2015-10-26 21:13:46 +00:00
|
|
|
int maxsize = 0;
|
|
|
|
int digits = 1;
|
2013-11-18 11:24:44 +00:00
|
|
|
int len;
|
2016-09-22 21:57:16 +00:00
|
|
|
|
|
|
|
|
2013-11-18 11:24:44 +00:00
|
|
|
r_list_foreach (fcns, iter, f) {
|
2016-09-22 18:31:40 +00:00
|
|
|
if (f->name && (len = strlen (f->name)) > maxnamelen) {
|
2013-11-18 11:24:44 +00:00
|
|
|
maxnamelen = len;
|
2016-09-22 18:31:40 +00:00
|
|
|
}
|
|
|
|
if (r_anal_fcn_size (f) > maxsize) {
|
2016-05-15 12:37:22 +00:00
|
|
|
maxsize = r_anal_fcn_size (f);
|
2016-09-22 18:31:40 +00:00
|
|
|
}
|
2013-11-18 11:24:44 +00:00
|
|
|
}
|
|
|
|
fcns = r_anal_get_fcns (c2->anal);
|
|
|
|
r_list_foreach (fcns, iter, f) {
|
2016-09-22 18:31:40 +00:00
|
|
|
if (f->name && (len = strlen (f->name)) > maxnamelen) {
|
2013-11-18 11:24:44 +00:00
|
|
|
maxnamelen = len;
|
2016-09-22 18:31:40 +00:00
|
|
|
}
|
|
|
|
if (r_anal_fcn_size (f) > maxsize) {
|
2016-05-15 12:37:22 +00:00
|
|
|
maxsize = r_anal_fcn_size (f);
|
2016-09-22 18:31:40 +00:00
|
|
|
}
|
2015-10-26 21:13:46 +00:00
|
|
|
}
|
|
|
|
while (maxsize > 9) {
|
|
|
|
maxsize /= 10;
|
|
|
|
digits++;
|
2013-11-18 11:24:44 +00:00
|
|
|
}
|
2016-09-22 18:31:40 +00:00
|
|
|
|
2013-11-18 11:24:44 +00:00
|
|
|
fcns = r_anal_get_fcns (c->anal);
|
2016-09-22 18:31:40 +00: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 11:59:05 +00: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 21:57:16 +00:00
|
|
|
f->diff->dist = 0;
|
2011-10-16 11:59:05 +00:00
|
|
|
}
|
2016-09-22 21:57:16 +00:00
|
|
|
diffrow (f->addr, f->name, r_anal_fcn_size (f), maxnamelen, digits,
|
|
|
|
f->diff->addr, f->diff->name, f->diff->size,
|
2014-12-26 00:54:49 +00:00
|
|
|
match, f->diff->dist, bare);
|
2011-10-16 11:59:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fcns = r_anal_get_fcns (c2->anal);
|
2016-09-22 18:31:40 +00:00
|
|
|
r_list_sort (fcns, c2->anal->columnSort);
|
2011-10-16 11:59:05 +00: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 18:31:40 +00:00
|
|
|
if (f->diff->type == R_ANAL_DIFF_TYPE_NULL) {
|
2016-05-15 12:37:22 +00:00
|
|
|
diffrow (f->addr, f->name, r_anal_fcn_size (f), maxnamelen,
|
2015-10-26 21:13:46 +00:00
|
|
|
digits, f->diff->addr, f->diff->name, f->diff->size,
|
2016-09-22 21:57:16 +00:00
|
|
|
"NEW", 0, bare); //f->diff->dist, bare);
|
2016-09-22 18:31:40 +00:00
|
|
|
}
|
|
|
|
break;
|
2011-10-16 11:59:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|