Implement RBinFile.merge() and obm command to use it ##bin

This commit is contained in:
pancake 2022-12-07 22:22:40 +01:00 committed by pancake
parent 3fe62b7679
commit a544c0d15a
7 changed files with 59 additions and 14 deletions

View File

@ -1149,11 +1149,18 @@ R_API RList *r_bin_file_get_symbols(RBinFile *bf) {
return o? o->symbols: NULL;
}
#if R2_580
R_API RBinFile *r_bin_file_open(RBin *bin, const char *file, RBinFileOptions *opt) {
if (r_bin_open (bin, file, opt)) {
return r_bin_cur (bin);
}
return NULL;
}
#endif
// TODO Improve this API
R_API void r_bin_file_merge(RBinFile *dst, RBinFile *src) {
// merge imports
// merge dbginfo
sdb_merge (dst->o->kv, src->o->kv);
sdb_merge (dst->sdb_addrinfo, src->sdb_addrinfo);
sdb_merge (dst->sdb_info, src->sdb_info);
}

View File

@ -501,7 +501,7 @@ static const ut8 *parse_line_header_source(RBinFile *bf, const ut8 *buf, const u
if (include_dir && include_dir[0] != '/') {
comp_dir = sdb_get (bf->sdb_addrinfo, "DW_AT_comp_dir", 0);
if (comp_dir) {
include_dir = r_str_newf("%s/%s/", comp_dir, include_dir);
include_dir = r_str_newf ("%s/%s/", comp_dir, include_dir);
}
}
} else {
@ -746,7 +746,7 @@ static const ut8 *parse_ext_opcode(const RBin *bin, const ut8 *obuf,
if (binfile && binfile->sdb_addrinfo && hdr->file_names) {
int fnidx = regs->file - 1;
if (fnidx >= 0 && fnidx < hdr->file_names_count) {
add_sdb_addrline(binfile->sdb_addrinfo, regs->address,
add_sdb_addrline (binfile->sdb_addrinfo, regs->address,
hdr->file_names[fnidx].name, regs->line, mode, print);
}
}

View File

@ -407,15 +407,16 @@ static int cmd_meta_lineinfo(RCore *core, const char *input) {
filter_count = 0;
fscache = sdb_new0 ();
PJ *pj = NULL;
RBinFile *bf = r_bin_cur (core->bin);
if (use_json) {
pj = r_core_pj_new (core);
pj_a (pj);
if (core->bin->cur && core->bin->cur->sdb_addrinfo) {
sdb_foreach (core->bin->cur->sdb_addrinfo, print_addrinfo_json, pj);
if (bf && bf->sdb_addrinfo) {
sdb_foreach (bf->sdb_addrinfo, print_addrinfo_json, pj);
}
} else {
if (core->bin->cur && core->bin->cur->sdb_addrinfo) {
sdb_foreach (core->bin->cur->sdb_addrinfo, print_addrinfo, NULL);
if (bf && bf->sdb_addrinfo) {
sdb_foreach (bf->sdb_addrinfo, print_addrinfo, NULL);
}
}
if (filter_count == 0) {

View File

@ -104,12 +104,15 @@ static const char *help_msg_oba[] = {
static const char *help_msg_ob[] = {
"Usage:", "ob", " # List open binary files backed by fd",
"ob", " [bfid]", "switch to open given objid",
"ob", " [name|bfid]", "switch to open given objid (or name)",
"ob", "", "list opened binary files and objid",
"ob*", "", "list opened binary files and objid (r2 commands)",
"ob", " *", "select all bins (use 'ob bfid' to pick one)",
"obm", "([id])", "merge current selected binfile into previous binfile (id-1)",
"obm-", "([id])", "same as obm, but deletes the current binfile",
"ob-", "*", "delete all binfiles",
"ob-", "[objid]", "delete binfile by binobjid",
"ob--", "", "delete the last binfile",
"ob.", " ([addr])", "show bfid at current address",
"ob=", "", "show ascii art table having the list of open files",
"obL", "", "same as iL or Li",
@ -118,7 +121,6 @@ static const char *help_msg_ob[] = {
"oba", " [addr]", "open bin info from the given address",
"obf", " ([file])", "load bininfo for current file (useful for r2 -n)",
"obj", "", "list opened binary files and objid (JSON format)",
"obn", " [name]", "select binfile by name",
"obo", " [fd]", "switch to open binfile by fd number",
"obr", " [baddr]", "rebase current bin object",
NULL
@ -341,7 +343,7 @@ static void cmd_open_bin(RCore *core, const char *input) {
r_list_free (files);
}
break;
case ' ': // "ob "
case ' ': // "ob " // select bf by id or name
{
ut32 id;
const char *tmp;
@ -386,6 +388,41 @@ static void cmd_open_bin(RCore *core, const char *input) {
value = input[2] ? input + 2 : NULL;
}
break;
case 'm': // "obm"
{
int dstid = atoi (input + 2);
// TODO take argument with given id to merge to
RBinFile *src = r_bin_cur (core->bin);
int current = src? src->id: -1;
if (current > 0) {
if (dstid < 1) {
dstid = current - 1;
}
RBinFile *dst = r_bin_file_find_by_id (core->bin, dstid);
if (dst) {
r_bin_file_merge (dst, src);
R_LOG_DEBUG ("merge %d into %d", current, dstid);
if (strchr (input + 2, '-')) { // "obm-"
int curfd = -1;
if (core->io->desc) {
curfd = core->io->desc->fd;
}
r_core_cmd_callf (core, "op %d", dst->fd);
r_bin_file_set_cur_binfile (core->bin, dst);
r_bin_file_delete (core->bin, current);
if (curfd >= 0) {
r_io_fd_close (core->io, curfd);
// r_core_cmd_callf (core, "o-%d", curfd);
}
}
break;
} else {
R_LOG_ERROR ("Cannot find binfile with id=%d", dstid);
}
}
R_LOG_INFO ("Nothing to merge");
}
break;
case 'o': // "obo"
if (input[2] == ' ') {
ut32 fd = r_num_math (core->num, input + 3);

View File

@ -783,6 +783,7 @@ R_API bool r_bin_file_set_cur_by_id(RBin *bin, ut32 bin_id);
R_API bool r_bin_file_set_cur_by_name(RBin *bin, const char *name);
R_API ut64 r_bin_file_delete_all(RBin *bin);
R_API bool r_bin_file_delete(RBin *bin, ut32 bin_id);
R_API void r_bin_file_merge(RBinFile *bo, RBinFile *b);
R_API RList *r_bin_file_compute_hashes(RBin *bin, ut64 limit);
R_API RList *r_bin_file_set_hashes(RBin *bin, RList *new_hashes);
R_API RBinPlugin *r_bin_file_cur_plugin(RBinFile *binfile);

View File

@ -315,7 +315,7 @@ R_API bool r_io_bank_map_priorize(RIO *io, const ut32 bankid, const ut32 mapid)
}
return false;
found:
if (iter == bank->maprefs->tail) { //tail is top
if (iter == bank->maprefs->tail) { // tail is top
return r_io_map_get_by_ref (io, mapref) ? true : false;
}
RIOSubMap *sm = r_io_submap_new (io, mapref);

View File

@ -1486,8 +1486,7 @@ R_API int r_main_radare2(int argc, const char **argv) {
r_config_set (r->config, "asm.os", asmos);
}
debug = r->io->desc && iod && (r->io->desc->fd == iod->fd) && iod->plugin && \
iod->plugin->isdbg;
debug = r->io->desc && iod && (r->io->desc->fd == iod->fd) && iod->plugin && iod->plugin->isdbg;
if (debug) {
r_core_setup_debugger (r, debugbackend, baddr == UT64_MAX);
}