diff --git a/binr/radare2/radare2.c b/binr/radare2/radare2.c index 2b4a0d2729..1fccdc983c 100644 --- a/binr/radare2/radare2.c +++ b/binr/radare2/radare2.c @@ -66,6 +66,28 @@ static int verify_version(int show) { return ret; } +// we should probably move this functionality into the r_debug API +// r_debug_get_baddr +static ut64 getBaddrFromDebugger(RCore *r, const char *file) { + RListIter *iter; + RDebugMap *map; + r_debug_attach (r->dbg, r->io->desc->fd); + r_debug_map_sync (r->dbg); + r_list_foreach (r->dbg->maps, iter, map) { + if (!strcmp (file, map->name)) { + return map->addr; + } + } + // fallback resolution (osx/w32?) + // we asume maps to be loaded in order, so lower addresses come first + r_list_foreach (r->dbg->maps, iter, map) { + if (map->perm == 5) { // r-x + return map->addr; + } + } + return 0LL; +} + static int main_help(int line) { if (line<2) printf ("Usage: r2 [-dDwntLqv] [-P patch] [-p prj] [-a arch] [-b bits] [-i file]\n" @@ -442,14 +464,24 @@ int main(int argc, char **argv, char **envp) { if (optindinfo) - eprintf ("bits %d\n", obj->info->bits); + { + char *diskfile = strstr (file, "://"); + if (diskfile) { + diskfile += 3; + } else diskfile = file; + fh = r_core_file_open (&r, file, perms, mapaddr); + if (fh != NULL) + r_debug_use (r.dbg, is_gdb? "gdb": debugbackend); + /* load symbols when doing r2 -d ls */ + // NOTE: the baddr is redefined to support PIE/ASLR + baddr = getBaddrFromDebugger (&r, diskfile); + if (baddr) eprintf ("Using BADDR %llx\n", baddr); + if (r_core_bin_load (&r, diskfile, baddr)) { + RBinObject *obj = r_bin_get_object (r.bin); + if (obj && obj->info) + eprintf ("bits %d\n", obj->info->bits); + } } - fh = r_core_file_open (&r, file, perms, mapaddr); - if (fh != NULL) - r_debug_use (r.dbg, is_gdb? "gdb": debugbackend); } } @@ -480,6 +512,8 @@ int main(int argc, char **argv, char **envp) { if (r.file && r.file->desc && r.file->desc->name) filepath = r.file->desc->name; + /* Load rbin info from r2 dbg:// or r2 /bin/ls */ + /* the baddr should be set manually here */ if (!r_core_bin_load (&r, filepath, baddr)) r_config_set (r.config, "io.va", "false"); } diff --git a/libr/anal/p/anal_ppc.c b/libr/anal/p/anal_ppc.c index 098dee2d79..c9ebdae7da 100644 --- a/libr/anal/p/anal_ppc.c +++ b/libr/anal/p/anal_ppc.c @@ -46,9 +46,11 @@ static int ppc_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *_bytes, int le op->jump = (aa)?(baddr):(addr+baddr+4); op->eob = 1; break; +#if 0 case 7: // sc/svc op->type = R_ANAL_OP_TYPE_SWI; break; +#endif #if 0 case 15: // bl // OK diff --git a/libr/bin/bin.c b/libr/bin/bin.c index cd7532ccca..daf31faf6f 100644 --- a/libr/bin/bin.c +++ b/libr/bin/bin.c @@ -491,7 +491,8 @@ R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, RIODesc *desc, ut64 baseaddr, // if there is no fixed size or its MAXED, there is no way to definitively // load the bin-properly. Many of the plugins require all content and are not // stream based loaders - RIODesc *tdesc = iob->desc_open (io, desc->name, desc->flags, R_IO_READ); + // NOTE: For RBin we dont need to open the file in read-write. This can be problematic + RIODesc *tdesc = iob->desc_open (io, desc->name, R_IO_READ, 0); //desc->flags, R_IO_READ); if (!tdesc) return R_FALSE; file_sz = iob->desc_size (io, tdesc); if (file_sz == UT64_MAX) { diff --git a/libr/core/cmd_debug.c b/libr/core/cmd_debug.c index 0ef40cd879..2535ccd763 100644 --- a/libr/core/cmd_debug.c +++ b/libr/core/cmd_debug.c @@ -422,7 +422,8 @@ static int cmd_debug_map(RCore *core, const char *input) { } r_debug_map_sync (core->dbg); // update process memory maps r_list_foreach (core->dbg->maps, iter, map) { - if ((addr != -1 && (addr >= map->addr && addr < map->addr_end)) || + if (core && core->bin && core->bin->cur && core->bin->cur->o && \ + (addr != -1 && (addr >= map->addr && addr < map->addr_end)) || (libname != NULL && (strstr (map->name, libname)))) { RBinObject *o = core->bin->cur->o; filter.offset = 0LL; diff --git a/libr/io/io.c b/libr/io/io.c index 0084e435ab..76a99fab82 100644 --- a/libr/io/io.c +++ b/libr/io/io.c @@ -127,7 +127,7 @@ static inline RIODesc *__getioplugin(RIO *io, const char *_uri, int flags, int m } if (!desc) { plugin = r_io_plugin_get_default (io, uri, 0); - desc = plugin ? plugin->open (io, uri, flags, mode) : NULL; + desc = (plugin&&plugin->open) ? plugin->open (io, uri, flags, mode) : NULL; if (desc) { r_io_desc_add (io, desc); if (desc->fd != -1) @@ -197,7 +197,7 @@ R_API RIODesc *r_io_open_nomap(RIO *io, const char *file, int flags, int mode) { r_io_desc_add (io, desc); if (io->autofd || !io->desc) r_io_use_desc (io, desc); - } else eprintf ("r_io_open_nomap: Unable to open file: %s\n", file); + } else eprintf ("r_io_open_nomap: Unable to open file: %s\n", file); return desc; } diff --git a/libr/io/p/io_default.c b/libr/io/p/io_default.c index 78310df312..b28c928669 100644 --- a/libr/io/p/io_default.c +++ b/libr/io/p/io_default.c @@ -244,9 +244,14 @@ static int __plugin_open_default(RIO *io, const char *file, ut8 many) { return r_io_def_mmap_check_default (file); } +// default open should permit opening static RIODesc *__open_default(RIO *io, const char *file, int flags, int mode) { + RIODesc *iod; if (!r_io_def_mmap_check_default (file) ) return NULL; - return r_io_def_mmap_open (io, file, flags, mode); + iod = r_io_def_mmap_open (io, file, flags, mode); + return iod; +// NTOE: uncomment this line to support loading files in ro as fallback is rw fails +// return iod? iod: r_io_def_mmap_open (io, file, R_IO_READ, mode); } static int __read(RIO *io, RIODesc *fd, ut8 *buf, int len) {