Fix loading the RBin information of PIE bins when debugging

This commit is contained in:
pancake 2013-12-17 02:10:13 +01:00
parent 3a9b6c686f
commit f664821d8e
7 changed files with 76 additions and 26 deletions

View File

@ -183,3 +183,5 @@ Already up-to-date
How about a nice game of chess?
THE ONLY WINNING MOVE IS NOT TO PLAY.
SHALL WE PLAY A GAME?
sudo make me a pancake
bitch

View File

@ -87,7 +87,7 @@ static int cmd_info(void *data, const char *input) {
switch (*input) {
case 'o': r_core_bin_load (core, input[1]==' '?
input+1: core->file->filename,
input+2: core->file->filename,
r_config_get_i (core->config, "bin.baddr"));
break;
#define RBININFO(x) r_core_bin_info(core,x,mode,va,NULL,offset); if (newline) r_cons_newline()
@ -123,7 +123,7 @@ static int cmd_info(void *data, const char *input) {
" 'j' output in json\n"
" 'q' simple quiet output\n"
"Actions:\n"
" io [file] ; load info from given file (or last opened)\n"
" io [file] ; load info from file (or last opened) use bin.baddr\n"
" ia ; show all info (imports, exports, sections..)\n"
" ic ; list classes\n"
" id ; debug information (source lines)\n"

View File

@ -1177,10 +1177,9 @@ R_API RBuffer *r_core_syscall (RCore *core, const char *name, const char *args)
// TODO: setup arch/bits/os?
r_egg_load (core->egg, code, 0);
if (!r_egg_compile (core->egg))
eprintf ("Cannot compile.\n" );
if (!r_egg_assemble (core->egg)) {
eprintf ("Cannot compile.\n");
if (!r_egg_assemble (core->egg))
eprintf ("r_egg_assemble: invalid assembly\n");
}
if ((b = r_egg_get_bin (core->egg))) {
if (b->length>0) {
for (i=0; i<b->length; i++)

View File

@ -137,6 +137,32 @@ R_API char *r_core_sysenv_begin(RCore *core, const char *cmd) {
return ret;
}
static ut64 get_base_from_maps(RCore *core, const char *file) {
RDebugMap *map;
RListIter *iter;
ut64 b = 0LL;
r_debug_map_sync (core->dbg); // update process memory maps
r_list_foreach (core->dbg->maps, iter, map) {
if ((map->perm & 5)==5) {
if (strstr (map->name, "copy/"))
return map->addr;
if (map->file) {
if (!strcmp (map->file, file)) // TODO: make this more flexible
return map->addr;
continue;
}
if (map->name) {
if (!strcmp (map->name, file)) // TODO: make this more flexible
return map->addr;
continue;
}
b = map->addr;
}
}
return b;
}
R_API int r_core_bin_load(RCore *r, const char *file, ut64 baddr) {
int i, va = r->io->va || r->io->debug;
RListIter *iter;
@ -163,11 +189,30 @@ R_API int r_core_bin_load(RCore *r, const char *file, ut64 baddr) {
/* r_bin_select, r_bin_select_idx and r_bin_load end up loading the bin */
r->bin->cur.rawstr = r_config_get_i (r->config, "bin.rawstr");
r->bin->minstrlen = r_config_get_i (r->config, "bin.minstr");
if( is_io_load ) {
if (is_io_load) {
// DEBUGGER
// Fix to select pid before trying to load the binary
if (r_config_get_i (r->config, "cfg.debug")) {
int newpid = -1;
if (r->file && r->file->fd)
newpid = r->file->fd->fd;
r_debug_select (r->dbg, newpid, newpid);
}
baddr = get_base_from_maps (r, file);
r_config_set_i (r->config, "bin.baddr", baddr);
r_core_bin_info (r, R_CORE_BIN_ACC_ALL, R_CORE_BIN_SET, va, NULL, offset);
r_bin_load (r->bin, file, R_FALSE);
r->file->obj = r_bin_get_object (r->bin);
if (baddr)
r->file->obj->baddr = baddr;
r_config_set_i (r->config, "io.va",
(r->file->obj->info)? r->file->obj->info->has_va: 0);
offset = r_bin_get_offset (r->bin);
#if 0
// XXX - May need to handle additional extraction here as well
r_bin_io_load (r->bin, r->io, r->file->fd, R_FALSE);
if ( r->bin->cur.curplugin &&
strncmp (r->bin->cur.curplugin->name, "any", 5)==0 ) {
strncmp (r->bin->cur.curplugin->name, "any", 5)==0 ) {
// set use of raw strings
r_config_set (r->config, "bin.rawstr", "true");
// get bin.minstr
@ -184,7 +229,10 @@ R_API int r_core_bin_load(RCore *r, const char *file, ut64 baddr) {
r_bin_select (r->bin, r->assembler->cur->arch, r->assembler->bits, NULL);
}
#endif
//r->file->fd->data = data;
} else if (r_bin_load (r->bin, file, R_FALSE)) { // --->
// HEXEDITOR
if (r->bin->narch>1 && r_config_get_i (r->config, "scr.prompt")) {
RBinObject *o = r->bin->cur.o;
eprintf ("NOTE: Fat binary found. Selected sub-bin is: -a %s -b %d\n",
@ -257,11 +305,8 @@ R_API RCoreFile *r_core_file_open(RCore *r, const char *file, int mode, ut64 loa
return NULL;
}
fh = malloc(sizeof(RCoreFile));
memset(fh, 0, sizeof(RCoreFile));
fh->uri = strdup(file);
fh = R_NEW0 (RCoreFile);
fh->uri = strdup (file);
fh->fd = fd;
fh->size = r_io_desc_size (r->io, fd);
fh->filename = strdup (fd->name);
@ -286,12 +331,11 @@ R_API RCoreFile * r_core_file_find_by_fd(RCore* core, int fd){
RCoreFile *result = NULL, *cf = NULL;
RListIter *iter;
if (!core || !core->files){
if (!core || !core->files)
return result;
}
r_list_foreach(core->files, iter, cf){
if(cf && cf->fd->fd == fd){
r_list_foreach (core->files, iter, cf) {
if (cf && cf->fd->fd == fd) {
result = cf;
break;
}
@ -300,7 +344,7 @@ R_API RCoreFile * r_core_file_find_by_fd(RCore* core, int fd){
}
R_API void r_core_file_free(RCoreFile *cf) {
if (cf){
if (cf) {
if (cf->map) free(cf->map);
if (cf->filename) free(cf->filename);
if (cf->uri) free(cf->uri);

View File

@ -67,6 +67,7 @@ R_API RDebugMap *r_debug_map_new (char *name, ut64 addr, ut64 addr_end, int perm
map = R_NEW (RDebugMap);
if (map) {
map->name = strdup (name);
map->file = NULL;
map->addr = addr;
map->addr_end = addr_end;
map->size = addr_end-addr;

View File

@ -233,6 +233,11 @@ static int __system(struct r_io_t *io, RIODesc *fd, const char *cmd) {
//printf("ptrace io command (%s)\n", cmd);
/* XXX ugly hack for testing purposes */
if (!strcmp (cmd, "pid")) {
if (!cmd[3]) {
int pid = RIOMACH_PID (fd->data);
eprintf ("%d\n", pid);
return 0;
}
int pid = atoi (cmd+4);
if (pid != 0) {
task_t task = pid_to_task (pid);
@ -242,20 +247,18 @@ static int __system(struct r_io_t *io, RIODesc *fd, const char *cmd) {
riom->task = task;
return 0;
}
eprintf ("io_mach_system: Invalid pid\n");
return 1;
}
eprintf ("io_mach_system: Invalid pid\n");
eprintf ("io_mach_system: Invalid pid %d\n", pid);
return 1;
} else eprintf ("Try: '=!pid'\n");
return R_TRUE;
return 1;
}
// TODO: rename ptrace to io_mach .. err io.ptrace ??
struct r_io_plugin_t r_io_plugin_mach = {
RIOPlugin r_io_plugin_mach = {
.name = "mach",
.desc = "mach debugger io plugin (mach://pid)",
.license = "LGPL3",
.license = "LGPL",
.open = __open,
.close = __close,
.read = __read,
@ -267,9 +270,10 @@ struct r_io_plugin_t r_io_plugin_mach = {
};
#else
struct r_io_plugin_t r_io_plugin_mach = {
RIOPlugin r_io_plugin_mach = {
.name = "mach",
.desc = "mach debug io (unsupported in this platform)"
.desc = "mach debug io (unsupported in this platform)",
.license = "LGPL"
};
#endif

View File

@ -87,7 +87,7 @@ R_API void r_io_section_list(RIO *io, ut64 offset, int rad) {
PFMT64x" 0x%08"PFMT64x" %s %s\n", s->offset,
s->vaddr, s->size, s->vsize, n, r_str_rwx_i (s->rwx));
} else {
io->printf ("[%.2d] %c 0x%08"PFMT64x" %s va=0x%08"PFMT64x
io->printf ("[%02d] %c 0x%08"PFMT64x" %s va=0x%08"PFMT64x
" sz=0x%08"PFMT64x" vsz=%08"PFMT64x" %s",
s->id, (offset>=s->offset && offset<s->offset+s->size)?'*':'.',
s->offset, r_str_rwx_i (s->rwx), s->vaddr, s->size, s->vsize, s->name);