Fix r2 -d problem not loading syms. Redo the ASLR/PIE again

This commit is contained in:
pancake 2014-10-15 15:41:02 +02:00
parent 1d390d4767
commit 0e0dc63aa8
6 changed files with 55 additions and 12 deletions

View File

@ -66,6 +66,28 @@ static int verify_version(int show) {
return ret; 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) { static int main_help(int line) {
if (line<2) if (line<2)
printf ("Usage: r2 [-dDwntLqv] [-P patch] [-p prj] [-a arch] [-b bits] [-i file]\n" 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 (optind<argc) if (optind<argc)
file = r_str_concat (file, " "); file = r_str_concat (file, " ");
} }
if (!r_core_bin_load (&r, file, baddr)) { {
RBinObject *obj = r_bin_get_object (r.bin); char *diskfile = strstr (file, "://");
if (obj && obj->info) if (diskfile) {
eprintf ("bits %d\n", obj->info->bits); 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) if (r.file && r.file->desc && r.file->desc->name)
filepath = 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)) if (!r_core_bin_load (&r, filepath, baddr))
r_config_set (r.config, "io.va", "false"); r_config_set (r.config, "io.va", "false");
} }

View File

@ -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->jump = (aa)?(baddr):(addr+baddr+4);
op->eob = 1; op->eob = 1;
break; break;
#if 0
case 7: // sc/svc case 7: // sc/svc
op->type = R_ANAL_OP_TYPE_SWI; op->type = R_ANAL_OP_TYPE_SWI;
break; break;
#endif
#if 0 #if 0
case 15: // bl case 15: // bl
// OK // OK

View File

@ -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 // 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 // load the bin-properly. Many of the plugins require all content and are not
// stream based loaders // 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; if (!tdesc) return R_FALSE;
file_sz = iob->desc_size (io, tdesc); file_sz = iob->desc_size (io, tdesc);
if (file_sz == UT64_MAX) { if (file_sz == UT64_MAX) {

View File

@ -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_debug_map_sync (core->dbg); // update process memory maps
r_list_foreach (core->dbg->maps, iter, map) { 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)))) { (libname != NULL && (strstr (map->name, libname)))) {
RBinObject *o = core->bin->cur->o; RBinObject *o = core->bin->cur->o;
filter.offset = 0LL; filter.offset = 0LL;

View File

@ -127,7 +127,7 @@ static inline RIODesc *__getioplugin(RIO *io, const char *_uri, int flags, int m
} }
if (!desc) { if (!desc) {
plugin = r_io_plugin_get_default (io, uri, 0); 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) { if (desc) {
r_io_desc_add (io, desc); r_io_desc_add (io, desc);
if (desc->fd != -1) 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); r_io_desc_add (io, desc);
if (io->autofd || !io->desc) if (io->autofd || !io->desc)
r_io_use_desc (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; return desc;
} }

View File

@ -244,9 +244,14 @@ static int __plugin_open_default(RIO *io, const char *file, ut8 many) {
return r_io_def_mmap_check_default (file); 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) { 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; 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) { static int __read(RIO *io, RIODesc *fd, ut8 *buf, int len) {