fix baddr >= 0 and laddr

This commit is contained in:
Riccardo Schirone 2015-08-18 00:18:04 +02:00
parent 36d96770aa
commit 81a8b2a3d5
6 changed files with 36 additions and 32 deletions

View File

@ -35,8 +35,8 @@ static struct r_bin_t *bin = NULL;
static char* output = NULL; static char* output = NULL;
static char* create = NULL; static char* create = NULL;
static int rad = R_FALSE; static int rad = R_FALSE;
static ut64 laddr = 0LL; static ut64 laddr = UT64_MAX;
static ut64 baddr = 0LL; static ut64 baddr = UT64_MAX;
static char* file = NULL; static char* file = NULL;
static char *name = NULL; static char *name = NULL;
static int rw = R_FALSE; static int rw = R_FALSE;
@ -507,7 +507,7 @@ int main(int argc, char **argv) {
case 'L': r_bin_list (bin); return 1; case 'L': r_bin_list (bin); return 1;
case 'G': case 'G':
laddr = r_num_math (NULL, optarg); laddr = r_num_math (NULL, optarg);
if (laddr == 0LL) if (laddr == UT64_MAX)
va = R_FALSE; va = R_FALSE;
break; break;
case 'B': case 'B':
@ -676,14 +676,14 @@ int main(int argc, char **argv) {
bin->minstrlen = r_config_get_i (core.config, "bin.minstr"); bin->minstrlen = r_config_get_i (core.config, "bin.minstr");
r_bin_force_plugin (bin, forcebin); r_bin_force_plugin (bin, forcebin);
if (!r_bin_load (bin, file, laddr, 0, xtr_idx, fd, rawstr)) { if (!r_bin_load (bin, file, baddr, laddr, xtr_idx, fd, rawstr)) {
if (!r_bin_load (bin, file, laddr, 0, xtr_idx, fd, rawstr)) { if (!r_bin_load (bin, file, baddr, laddr, xtr_idx, fd, rawstr)) {
eprintf ("r_bin: Cannot open file\n"); eprintf ("r_bin: Cannot open file\n");
r_core_fini (&core); r_core_fini (&core);
return 1; return 1;
} }
} }
if (baddr != 0LL) { if (baddr != UT64_MAX) {
r_bin_set_baddr (bin, baddr); r_bin_set_baddr (bin, baddr);
} }
if (rawstr == 2) { if (rawstr == 2) {

View File

@ -226,7 +226,7 @@ int main(int argc, char **argv, char **envp) {
int run_rc = 1; int run_rc = 1;
int ret, i, c, perms = R_IO_READ; int ret, i, c, perms = R_IO_READ;
int sandbox = 0; int sandbox = 0;
ut64 baddr = 0; ut64 baddr = UT64_MAX;
ut64 seek = UT64_MAX; ut64 seek = UT64_MAX;
char *pfile = NULL, *file = NULL; char *pfile = NULL, *file = NULL;
char *cmdfile[32]; char *cmdfile[32];
@ -312,9 +312,6 @@ int main(int argc, char **argv, char **envp) {
case 'B': case 'B':
baddr = r_num_math (r.num, optarg); baddr = r_num_math (r.num, optarg);
va = 2; va = 2;
// hackaround. baddr=0: no laddr and -1 means baddr=0
if (baddr==0)
baddr = UT64_MAX;
break; break;
case 'c': r_list_append (cmds, optarg); break; case 'c': r_list_append (cmds, optarg); break;
case 'C': case 'C':
@ -424,7 +421,7 @@ int main(int argc, char **argv, char **envp) {
switch (va) { switch (va) {
case 0: case 0:
r_config_set_i (r.config, "io.va", R_FALSE); r_config_set_i (r.config, "io.va", R_FALSE);
baddr = 0; baddr = UT64_MAX;
break; break;
case 2: case 2:
r_config_set_i (r.config, "bin.laddr", baddr); r_config_set_i (r.config, "bin.laddr", baddr);
@ -556,7 +553,7 @@ int main(int argc, char **argv, char **envp) {
/* load symbols when doing r2 -d ls */ /* load symbols when doing r2 -d ls */
// NOTE: the baddr is redefined to support PIE/ASLR // NOTE: the baddr is redefined to support PIE/ASLR
baddr = getBaddrFromDebugger (&r, diskfile); baddr = getBaddrFromDebugger (&r, diskfile);
if (baddr) eprintf ("Using BADDR 0x%"PFMT64x"\n", baddr); if (baddr != UT64_MAX) eprintf ("Using BADDR 0x%"PFMT64x"\n", baddr);
if (r_core_bin_load (&r, diskfile, baddr)) { if (r_core_bin_load (&r, diskfile, baddr)) {
RBinObject *obj = r_bin_get_object (r.bin); RBinObject *obj = r_bin_get_object (r.bin);
if (obj && obj->info) if (obj && obj->info)

View File

@ -586,6 +586,8 @@ R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, RIODesc *desc, ut64 baseaddr,
ut8 is_debugger = desc && desc->plugin && desc->plugin->isdbg; ut8 is_debugger = desc && desc->plugin && desc->plugin->isdbg;
if (!io || !desc) return R_FALSE; if (!io || !desc) return R_FALSE;
if (baseaddr == UT64_MAX) baseaddr = 0;
if (loadaddr == UT64_MAX) loadaddr = 0;
buf_bytes = NULL; buf_bytes = NULL;
file_sz = iob->desc_size (io, desc); file_sz = iob->desc_size (io, desc);
@ -626,7 +628,7 @@ R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, RIODesc *desc, ut64 baseaddr,
} }
sz = R_MIN (file_sz, sz); sz = R_MIN (file_sz, sz);
if (!buf_bytes) { if (!buf_bytes) {
iob->desc_seek (io, desc, baseaddr); iob->desc_seek (io, desc, loadaddr);
buf_bytes = iob->desc_read (io, desc, &sz); buf_bytes = iob->desc_read (io, desc, &sz);
} }
@ -668,7 +670,7 @@ R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, RIODesc *desc, ut64 baseaddr,
buf_bytes, sz, file_sz, bin->rawstr, baseaddr, loadaddr, buf_bytes, sz, file_sz, bin->rawstr, baseaddr, loadaddr,
desc->fd, name, NULL, offset); desc->fd, name, NULL, offset);
/* hack to force baseaddr, looks like rbinfilenewfrombytes() ignores the value */ /* hack to force baseaddr, looks like rbinfilenewfrombytes() ignores the value */
if (loadaddr) { if (baseaddr) {
binfile_set_baddr (binfile, baseaddr); binfile_set_baddr (binfile, baseaddr);
} }
} }
@ -921,6 +923,7 @@ static RBinObject * r_bin_object_new (RBinFile *binfile, RBinPlugin *plugin, ut6
o->id = r_num_rand (0xfffff000); o->id = r_num_rand (0xfffff000);
o->kv = sdb_new0 (); o->kv = sdb_new0 ();
o->baddr = baseaddr; o->baddr = baseaddr;
o->baddr_shift = 0;
// XXX more checking will be needed here // XXX more checking will be needed here
// only use LoadBytes if buffer offset != 0 // only use LoadBytes if buffer offset != 0
//if (offset != 0 && bytes && plugin && plugin->load_bytes && (bytes_sz >= sz + offset) ) { //if (offset != 0 && bytes && plugin && plugin->load_bytes && (bytes_sz >= sz + offset) ) {
@ -956,6 +959,7 @@ static RBinObject * r_bin_object_new (RBinFile *binfile, RBinPlugin *plugin, ut6
o->plugin = plugin; o->plugin = plugin;
o->loadaddr = loadaddr; o->loadaddr = loadaddr;
o->baddr = baseaddr; o->baddr = baseaddr;
o->baddr_shift = 0;
// XXX - binfile could be null here meaning an improper load // XXX - binfile could be null here meaning an improper load
// XXX - object size cant be set here and needs to be set where // XXX - object size cant be set here and needs to be set where
// where the object is created from. The reason for this is to prevent // where the object is created from. The reason for this is to prevent
@ -1149,19 +1153,23 @@ R_API int r_bin_list(RBin *bin) {
return R_FALSE; return R_FALSE;
} }
static ut64 binobj_get_baddr (RBinObject *o) {
return o ? o->baddr + o->baddr_shift : 0;
}
R_API ut64 r_binfile_get_baddr (RBinFile *binfile) { R_API ut64 r_binfile_get_baddr (RBinFile *binfile) {
return binfile && binfile->o ? binfile->o->baddr : 0LL; return binfile ? binobj_get_baddr(binfile->o) : 0LL;
} }
/* returns the base address of bin or 0 in case of errors */ /* returns the base address of bin or 0 in case of errors */
R_API ut64 r_bin_get_baddr(RBin *bin) { R_API ut64 r_bin_get_baddr(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin); RBinObject *o = r_bin_cur_object (bin);
return o ? o->baddr : 0; return binobj_get_baddr (o);
} }
static void binobj_set_baddr (RBinObject *o, ut64 baddr) { static void binobj_set_baddr (RBinObject *o, ut64 baddr) {
if (!o) return; if (!o || baddr == UT64_MAX) return;
o->baddr = baddr; o->baddr_shift = baddr - o->baddr;
} }
static void binfile_set_baddr (RBinFile *binfile, ut64 baddr) { static void binfile_set_baddr (RBinFile *binfile, ut64 baddr) {
@ -1750,18 +1758,12 @@ R_API ut64 r_binfile_get_vaddr (RBinFile *binfile, ut64 paddr, ut64 vaddr) {
int use_va = 0; int use_va = 0;
if (binfile && binfile->o && binfile->o->info) if (binfile && binfile->o && binfile->o->info)
use_va = binfile->o->info->has_va; use_va = binfile->o->info->has_va;
return use_va ? vaddr : paddr; return use_va ? binobj_a2b (binfile->o, vaddr) : paddr;
} }
R_API ut64 r_bin_get_vaddr (RBin *bin, ut64 paddr, ut64 vaddr) { R_API ut64 r_bin_get_vaddr (RBin *bin, ut64 paddr, ut64 vaddr) {
ut64 baddr = r_bin_get_baddr (bin); if (!bin || !bin->cur) return UT64_MAX;
if (!bin || !bin->cur)
return UT64_MAX;
if (bin->is_debugger && baddr) {
return r_bin_a2b (bin, paddr);
}
// autodetect thumb // autodetect thumb
if (bin->cur->o && bin->cur->o->info && bin->cur->o->info->arch) { if (bin->cur->o && bin->cur->o->info && bin->cur->o->info->arch) {
if (!strcmp (bin->cur->o->info->arch, "arm") && (vaddr & 1)) { if (!strcmp (bin->cur->o->info->arch, "arm") && (vaddr & 1)) {
@ -1773,12 +1775,12 @@ R_API ut64 r_bin_get_vaddr (RBin *bin, ut64 paddr, ut64 vaddr) {
static ut64 binobj_a2b (RBinObject *o, ut64 addr) { static ut64 binobj_a2b (RBinObject *o, ut64 addr) {
if (!o) return addr; if (!o) return addr;
return o->baddr + addr; return o->baddr_shift + addr;
} }
R_API ut64 r_bin_a2b (RBin *bin, ut64 addr) { R_API ut64 r_bin_a2b (RBin *bin, ut64 addr) {
ut64 baddr = r_bin_get_baddr (bin); RBinObject *o = r_bin_cur_object (bin);
return baddr + addr; return o ? o->baddr_shift + addr : addr;
} }
R_API ut64 r_bin_get_size (RBin *bin) { R_API ut64 r_bin_get_size (RBin *bin) {

View File

@ -7,9 +7,10 @@ R_API int r_bin_addr2line(RBin *bin, ut64 addr, char *file, int len, int *line)
RBinFile *binfile = r_bin_cur (bin); RBinFile *binfile = r_bin_cur (bin);
RBinObject *o = r_bin_cur_object (bin); RBinObject *o = r_bin_cur_object (bin);
RBinPlugin *cp = r_bin_file_cur_plugin (binfile); RBinPlugin *cp = r_bin_file_cur_plugin (binfile);
ut64 baddr = r_bin_get_baddr (bin);
if (cp && cp->dbginfo) { if (cp && cp->dbginfo) {
if (o && addr >= o->baddr && addr < (o->baddr+bin->cur->o->size)) if (o && addr >= baddr && addr < baddr + bin->cur->o->size)
if (cp->dbginfo->get_line) if (cp->dbginfo->get_line)
return cp->dbginfo->get_line (bin->cur, return cp->dbginfo->get_line (bin->cur,
addr, file, len, line); addr, file, len, line);

View File

@ -1175,7 +1175,7 @@ static int bin_symbols (RCore *r, int mode, ut64 laddr, int va, ut64 at, const c
} }
r_list_foreach (symbols, iter, symbol) { r_list_foreach (symbols, iter, symbol) {
ut64 addr = va? r_bin_get_vaddr (r->bin, symbol->paddr, symbol->vaddr): symbol->paddr; ut64 addr = va ? r_bin_get_vaddr (r->bin, symbol->paddr, symbol->vaddr) : symbol->paddr;
if (name && strcmp (symbol->name, name)) if (name && strcmp (symbol->name, name))
continue; continue;
if (at) { if (at) {
@ -1689,8 +1689,11 @@ R_API int r_core_bin_info (RCore *core, int action, int mode, int va, RCoreBinFi
const char *name = NULL; const char *name = NULL;
ut64 at = 0; ut64 at = 0;
if (loadaddr) if (loadaddr == UT64_MAX) loadaddr = 0;
if (loadaddr) {
va = 2; va = 2;
}
if (filter && filter->offset) if (filter && filter->offset)
at = filter->offset; at = filter->offset;

View File

@ -119,6 +119,7 @@ typedef struct r_bin_info_t {
typedef struct r_bin_object_t { typedef struct r_bin_object_t {
ut32 id; ut32 id;
ut64 baddr; ut64 baddr;
ut64 baddr_shift;
ut64 loadaddr; ut64 loadaddr;
ut64 boffset; ut64 boffset;
int size; int size;