Skip to content

Commit

Permalink
Mark the bytes that is referenced as data and not belonging to any fu…
Browse files Browse the repository at this point in the history
…nction

* add flag space xdata to label these data
* show hex instead of invalid for labeled position
  • Loading branch information
PeiweiHu committed Aug 28, 2023
1 parent a5780a3 commit dc04387
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
60 changes: 60 additions & 0 deletions librz/core/canalysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -4685,6 +4685,60 @@ static void core_analysis_using_plugins(RzCore *core) {
}
}

/**
* \brief Mark the bytes that are referenced and don't belong
* to any functions as data.
*/
static void core_analysis_referenced_data(RzCore *core) {
RzList *xrefs = rz_analysis_xrefs_list(core->analysis);
RzListIter *it, *it1;
RzAnalysisXRef *x, *x1;
rz_list_foreach (xrefs, it, x) {
if (x->type != RZ_ANALYSIS_XREF_TYPE_DATA) {
continue;
}
ut64 to = x->to;

// the location <at> doesn't belong to any function
RzList *funcs = rz_analysis_get_functions_in(core->analysis, to);
if (!rz_list_empty(funcs)) {
rz_list_free(funcs);
continue;
}
rz_list_free(funcs);

// the location <at> is only referenced as DATA instead of CODE
RzList *to_xrefs = rz_analysis_xrefs_get_to(core->analysis, to);
bool has_nondata_ref = false;
rz_list_foreach (to_xrefs, it1, x1) {
if (x1->type != RZ_ANALYSIS_XREF_TYPE_DATA) {
has_nondata_ref = true;
}
}
rz_list_free(to_xrefs);
if (has_nondata_ref) {
continue;
}

// skip the offset that already with flag indicating already analyzed
const RzList *flags = rz_flag_get_list(core->flags, to);
if (!rz_list_empty(flags)) {
continue;
}

char *flagname = rz_str_newf("xdata.%08" PFMT64x, to);
if (!flagname) {
RZ_LOG_ERROR("Failed allocate flag name buffer for xdata\n");
return;
}

rz_flag_space_push(core->flags, RZ_FLAGS_FS_XDATA);
rz_flag_set(core->flags, flagname, to, core->analysis->bits / 8);
rz_flag_space_pop(core->flags);
free(flagname);
}
}

/**
* Runs all the steps of the deep analysis.
*
Expand Down Expand Up @@ -4894,6 +4948,12 @@ RZ_API bool rz_core_analysis_everything(RzCore *core, bool experimental, char *d
rz_core_notify_done(core, "Use -AA or aaaa to perform additional experimental analysis.");
}

notify = "Analyze the referenced data (xdata)";
rz_core_notify_begin(core, "%s", notify);
core_analysis_referenced_data(core);
rz_core_notify_done(core, "%s", notify);
rz_core_task_yield(&core->tasks);

rz_core_seek_undo(core);
if (dh_orig) {
rz_config_set(core->config, "dbg.backend", dh_orig);
Expand Down
10 changes: 10 additions & 0 deletions librz/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,16 @@ static void ds_build_op_str(RzDisasmState *ds, bool print_color) {
}
return;
}
// if the <invalid> is referred as data, show bytes
if (!strcmp(ds->opstr, "invalid")) {
RzFlagItem *fi = rz_flag_get_i(ds->core->flags, ds->at);
if (fi && rz_str_startswith(fi->name, "xdata.")) {
int op_size = rz_asm_op_get_size(&ds->asmop);
ut64 data = rz_read_ble(rz_asm_op_get_buf(&ds->asmop), ds->core->analysis->big_endian, op_size * 8);
free(ds->opstr);
ds->opstr = rz_str_newf("0x%" PFMT64x, data);
}
}
if (ds->hint && ds->hint->opcode) {
free(ds->opstr);
ds->opstr = strdup(ds->hint->opcode);
Expand Down
1 change: 1 addition & 0 deletions librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ RZ_LIB_VERSION_HEADER(rz_core);
#define RZ_FLAGS_FS_GLOBALS "globals"
#define RZ_FLAGS_FS_DEBUG_MAPS "maps"
#define RZ_FLAGS_FS_POINTERS "pointers"
#define RZ_FLAGS_FS_XDATA "xdata"

///
#define RZ_CONS_COLOR_DEF(x, def) ((core->cons && core->cons->context->pal.x) ? core->cons->context->pal.x : def)
Expand Down

0 comments on commit dc04387

Please sign in to comment.