From 81a8b2a3d5c8c31c581dc5a901bd38816448354a Mon Sep 17 00:00:00 2001 From: Riccardo Schirone Date: Tue, 18 Aug 2015 00:18:04 +0200 Subject: [PATCH] fix baddr >= 0 and laddr --- binr/rabin2/rabin2.c | 12 ++++++------ binr/radare2/radare2.c | 9 +++------ libr/bin/bin.c | 36 +++++++++++++++++++----------------- libr/bin/dbginfo.c | 3 ++- libr/core/bin.c | 7 +++++-- libr/include/r_bin.h | 1 + 6 files changed, 36 insertions(+), 32 deletions(-) diff --git a/binr/rabin2/rabin2.c b/binr/rabin2/rabin2.c index 7231756609..a54c269b7b 100644 --- a/binr/rabin2/rabin2.c +++ b/binr/rabin2/rabin2.c @@ -35,8 +35,8 @@ static struct r_bin_t *bin = NULL; static char* output = NULL; static char* create = NULL; static int rad = R_FALSE; -static ut64 laddr = 0LL; -static ut64 baddr = 0LL; +static ut64 laddr = UT64_MAX; +static ut64 baddr = UT64_MAX; static char* file = NULL; static char *name = NULL; static int rw = R_FALSE; @@ -507,7 +507,7 @@ int main(int argc, char **argv) { case 'L': r_bin_list (bin); return 1; case 'G': laddr = r_num_math (NULL, optarg); - if (laddr == 0LL) + if (laddr == UT64_MAX) va = R_FALSE; break; case 'B': @@ -676,14 +676,14 @@ int main(int argc, char **argv) { bin->minstrlen = r_config_get_i (core.config, "bin.minstr"); r_bin_force_plugin (bin, forcebin); - if (!r_bin_load (bin, file, laddr, 0, 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)) { + if (!r_bin_load (bin, file, baddr, laddr, xtr_idx, fd, rawstr)) { eprintf ("r_bin: Cannot open file\n"); r_core_fini (&core); return 1; } } - if (baddr != 0LL) { + if (baddr != UT64_MAX) { r_bin_set_baddr (bin, baddr); } if (rawstr == 2) { diff --git a/binr/radare2/radare2.c b/binr/radare2/radare2.c index 06e8e0fde8..b291a249e8 100644 --- a/binr/radare2/radare2.c +++ b/binr/radare2/radare2.c @@ -226,7 +226,7 @@ int main(int argc, char **argv, char **envp) { int run_rc = 1; int ret, i, c, perms = R_IO_READ; int sandbox = 0; - ut64 baddr = 0; + ut64 baddr = UT64_MAX; ut64 seek = UT64_MAX; char *pfile = NULL, *file = NULL; char *cmdfile[32]; @@ -312,9 +312,6 @@ int main(int argc, char **argv, char **envp) { case 'B': baddr = r_num_math (r.num, optarg); va = 2; - // hackaround. baddr=0: no laddr and -1 means baddr=0 - if (baddr==0) - baddr = UT64_MAX; break; case 'c': r_list_append (cmds, optarg); break; case 'C': @@ -424,7 +421,7 @@ int main(int argc, char **argv, char **envp) { switch (va) { case 0: r_config_set_i (r.config, "io.va", R_FALSE); - baddr = 0; + baddr = UT64_MAX; break; case 2: 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 */ // NOTE: the baddr is redefined to support PIE/ASLR 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)) { RBinObject *obj = r_bin_get_object (r.bin); if (obj && obj->info) diff --git a/libr/bin/bin.c b/libr/bin/bin.c index e02bf8f2f6..ba005dbead 100644 --- a/libr/bin/bin.c +++ b/libr/bin/bin.c @@ -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; if (!io || !desc) return R_FALSE; + if (baseaddr == UT64_MAX) baseaddr = 0; + if (loadaddr == UT64_MAX) loadaddr = 0; buf_bytes = NULL; 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); if (!buf_bytes) { - iob->desc_seek (io, desc, baseaddr); + iob->desc_seek (io, desc, loadaddr); 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, desc->fd, name, NULL, offset); /* hack to force baseaddr, looks like rbinfilenewfrombytes() ignores the value */ - if (loadaddr) { + if (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->kv = sdb_new0 (); o->baddr = baseaddr; + o->baddr_shift = 0; // XXX more checking will be needed here // only use LoadBytes if buffer offset != 0 //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->loadaddr = loadaddr; o->baddr = baseaddr; + o->baddr_shift = 0; // XXX - binfile could be null here meaning an improper load // 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 @@ -1149,19 +1153,23 @@ R_API int r_bin_list(RBin *bin) { 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) { - 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 */ R_API ut64 r_bin_get_baddr(RBin *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) { - if (!o) return; - o->baddr = baddr; + if (!o || baddr == UT64_MAX) return; + o->baddr_shift = baddr - o->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; if (binfile && binfile->o && binfile->o->info) 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) { - 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 if (bin->cur->o && bin->cur->o->info && bin->cur->o->info->arch) { 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) { if (!o) return addr; - return o->baddr + addr; + return o->baddr_shift + addr; } R_API ut64 r_bin_a2b (RBin *bin, ut64 addr) { - ut64 baddr = r_bin_get_baddr (bin); - return baddr + addr; + RBinObject *o = r_bin_cur_object (bin); + return o ? o->baddr_shift + addr : addr; } R_API ut64 r_bin_get_size (RBin *bin) { diff --git a/libr/bin/dbginfo.c b/libr/bin/dbginfo.c index f3e3732a84..eb0f35fa1d 100644 --- a/libr/bin/dbginfo.c +++ b/libr/bin/dbginfo.c @@ -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); RBinObject *o = r_bin_cur_object (bin); RBinPlugin *cp = r_bin_file_cur_plugin (binfile); + ut64 baddr = r_bin_get_baddr (bin); 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) return cp->dbginfo->get_line (bin->cur, addr, file, len, line); diff --git a/libr/core/bin.c b/libr/core/bin.c index c8ba5b6b7b..58bec2fdd1 100644 --- a/libr/core/bin.c +++ b/libr/core/bin.c @@ -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) { - 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)) continue; 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; ut64 at = 0; - if (loadaddr) + if (loadaddr == UT64_MAX) loadaddr = 0; + + if (loadaddr) { va = 2; + } if (filter && filter->offset) at = filter->offset; diff --git a/libr/include/r_bin.h b/libr/include/r_bin.h index 55225b5246..d784d4690b 100644 --- a/libr/include/r_bin.h +++ b/libr/include/r_bin.h @@ -119,6 +119,7 @@ typedef struct r_bin_info_t { typedef struct r_bin_object_t { ut32 id; ut64 baddr; + ut64 baddr_shift; ut64 loadaddr; ut64 boffset; int size;