From 36d96770aa25b130cc59c67ecd95507fb649710a Mon Sep 17 00:00:00 2001 From: Riccardo Schirone Date: Mon, 17 Aug 2015 02:05:39 +0200 Subject: [PATCH] simplify baddr usage * some style fixing * core/bin: simplify rva function * bin/bin: avoid passing baddr around * bin/bin: provide methods to work with baddr --- binr/rabin2/rabin2.c | 10 +-- binr/radare2/radare2.c | 13 +-- libr/bin/bin.c | 147 ++++++++++++++++++--------------- libr/core/anal.c | 6 +- libr/core/bin.c | 182 ++++++++++++++--------------------------- libr/core/cmd_debug.c | 13 ++- libr/core/file.c | 61 +------------- libr/include/r_bin.h | 3 +- 8 files changed, 158 insertions(+), 277 deletions(-) diff --git a/binr/rabin2/rabin2.c b/binr/rabin2/rabin2.c index b87a906f17..7231756609 100644 --- a/binr/rabin2/rabin2.c +++ b/binr/rabin2/rabin2.c @@ -684,8 +684,7 @@ int main(int argc, char **argv) { } } if (baddr != 0LL) { - bin->cur->o->baddr = baddr; - //bin->cur->o->baddr = laddr; + r_bin_set_baddr (bin, baddr); } if (rawstr == 2) { rawstr = R_FALSE; @@ -736,13 +735,6 @@ int main(int argc, char **argv) { r_core_fini (&core); return 0; } -#if 0 - // ASLR WTF - if (laddr != 0LL) { - //r_bin_set_baddr (bin, laddr); - //bin->cur->o->baddr = laddr; - } -#endif #define isradjson (rad==R_CORE_BIN_JSON&&actions>0) #define run_action(n,x,y) {\ if (action&x) {\ diff --git a/binr/radare2/radare2.c b/binr/radare2/radare2.c index 0d701bfaf1..06e8e0fde8 100644 --- a/binr/radare2/radare2.c +++ b/binr/radare2/radare2.c @@ -596,15 +596,10 @@ int main(int argc, char **argv, char **envp) { /* Load rbin info from r2 dbg:// or r2 /bin/ls */ /* the baddr should be set manually here */ - { - if (r_core_bin_load (&r, filepath, 0)) { - RBinFile *file = r_bin_cur (r.bin); - // use_baddr - if (file && file->o) - file->o->baddr = baddr; - } else { - r_config_set_i (r.config, "io.va", R_FALSE); - } + if (r_core_bin_load (&r, filepath, 0)) { + r_bin_set_baddr (r.bin, baddr); + } else { + r_config_set_i (r.config, "io.va", R_FALSE); } } } else { diff --git a/libr/bin/bin.c b/libr/bin/bin.c index aea405516f..e02bf8f2f6 100644 --- a/libr/bin/bin.c +++ b/libr/bin/bin.c @@ -46,6 +46,8 @@ static RBinFile * r_bin_file_new_from_bytes (RBin *bin, const char *file, const static int getoffset (RBin *bin, int type, int idx); static const char *getname (RBin *bin, int off); static int r_bin_file_object_add (RBinFile *binfile, RBinObject *o); +static void binfile_set_baddr (RBinFile *binfile, ut64 baddr); +static ut64 binobj_a2b (RBinObject *o, ut64 addr); R_API void r_bin_iobind(RBin *bin, RIO *io) { r_io_bind (io, &bin->iob); @@ -463,14 +465,18 @@ R_API int r_bin_load(RBin *bin, const char *file, ut64 baseaddr, ut64 loadaddr, RIOBind *iob = &(bin->iob); RIO *io; RIODesc *desc = NULL; - io = (iob&&iob->get_io) ? iob->get_io(iob) : NULL; + io = (iob && iob->get_io) ? iob->get_io(iob) : NULL; if (!io) { io = r_io_new (); if (io) r_io_bind (io, &bin->iob); } if (!io) return R_FALSE; bin->rawstr = rawstr; - desc = fd == -1 ? iob->desc_open (io, file, O_RDONLY, 0644) : iob->desc_get_by_fd (io, fd); + if (fd == -1) { + desc = iob->desc_open (io, file, O_RDONLY, 0644); + } else { + desc = iob->desc_get_by_fd (io, fd); + } if (!desc) return R_FALSE; return r_bin_load_io (bin, desc, baseaddr, loadaddr, xtr_idx); } @@ -588,39 +594,36 @@ R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, RIODesc *desc, ut64 baseaddr, if (!file_sz || file_sz == UT64_MAX) file_sz = 2*1024*1024; // 2MB #endif - if (sz == 0) - sz = file_sz; -#if 1 -if (r_list_length (bin->binfiles)==0) { - if ((file_sz == 0 || file_sz == UT64_MAX) && is_debugger) { - int fail = 1; - /* get file path from desc name */ - const char *filepath = desc->name; + if (sz == 0) sz = file_sz; - // attempt a local open and read - // This happens when a plugin like debugger does not have a fixed size. - // 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 - // NOTE: For RBin we dont need to open the file in read-write. This can be problematic - RIODesc *tdesc = iob->desc_open (io, filepath, R_IO_READ, 0); //desc->flags, R_IO_READ); - eprintf ("Assuming filepath %s\n", filepath); - if (tdesc) { - file_sz = iob->desc_size (io, tdesc); - if (file_sz != UT64_MAX) { - sz = R_MIN (file_sz, sz); - buf_bytes = iob->desc_read (io, tdesc, &sz); - fail = 0; + if (r_list_length (bin->binfiles) == 0) { + if ((file_sz == 0 || file_sz == UT64_MAX) && is_debugger) { + int fail = 1; + /* get file path from desc name */ + const char *filepath = desc->name; + + // attempt a local open and read + // This happens when a plugin like debugger does not have a fixed size. + // 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 + // NOTE: For RBin we dont need to open the file in read-write. This can be problematic + RIODesc *tdesc = iob->desc_open (io, filepath, R_IO_READ, 0); //desc->flags, R_IO_READ); + eprintf ("Assuming filepath %s\n", filepath); + if (tdesc) { + file_sz = iob->desc_size (io, tdesc); + if (file_sz != UT64_MAX) { + sz = R_MIN (file_sz, sz); + buf_bytes = iob->desc_read (io, tdesc, &sz); + fail = 0; + } + iob->desc_close (io, tdesc); } - iob->desc_close (io, tdesc); + if (fail) return R_FALSE; } - if (fail) - return R_FALSE; - } -} else -#endif - if (sz == UT64_MAX) + } else if (sz == UT64_MAX) { return R_FALSE; + } sz = R_MIN (file_sz, sz); if (!buf_bytes) { iob->desc_seek (io, desc, baseaddr); @@ -653,9 +656,7 @@ if (r_list_length (bin->binfiles)==0) { desc->name, buf_bytes, sz, file_sz, baseaddr, loadaddr, xtr_idx, desc->fd, bin->rawstr); - if (binfile->o) { - binfile->o->baddr = baseaddr; - } + binfile_set_baddr (binfile, baseaddr); } xtr = NULL; } @@ -663,14 +664,12 @@ if (r_list_length (bin->binfiles)==0) { } if (!binfile) { -//eprintf ("LOAD BINFILE FROM BYTE %lld b=0x%08llx l=0x%08llx\n", sz, baseaddr, loadaddr); binfile = r_bin_file_new_from_bytes (bin, desc->name, 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 (binfile && binfile->o) - binfile->o->baddr = baseaddr; + binfile_set_baddr (binfile, baseaddr); } } @@ -681,18 +680,18 @@ if (r_list_length (bin->binfiles)==0) { } R_API int r_bin_load_io_at_offset_as(RBin *bin, RIODesc *desc, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name) { -#if 1 // adding file_sz to help reduce the performance impact on the system // in this case the number of bytes read will be limited to 2MB (MIN_LOAD_SIZE) // if it fails, the whole file is loaded. const ut64 MAX_LOAD_SIZE = 0; //0xfffff; //128 * (1 << 10 << 10); int res = r_bin_load_io_at_offset_as_sz (bin, desc, baseaddr, loadaddr, xtr_idx, offset, name, MAX_LOAD_SIZE); - if (res) - return res; -#endif - return r_bin_load_io_at_offset_as_sz (bin, desc, baseaddr, - loadaddr, xtr_idx, offset, name, UT64_MAX); + if (!res) { + res = r_bin_load_io_at_offset_as_sz (bin, desc, baseaddr, + loadaddr, xtr_idx, offset, name, UT64_MAX); + } + + return res; } #if 0 @@ -917,7 +916,7 @@ static RBinObject * r_bin_object_new (RBinFile *binfile, RBinPlugin *plugin, ut6 ut64 bytes_sz = binfile ? r_buf_size (binfile->buf): 0; Sdb *sdb = binfile ? binfile->sdb : NULL; RBinObject *o = R_NEW0 (RBinObject); - o->obj_size = bytes && (bytes_sz >= sz+offset) ? sz : 0; + o->obj_size = bytes && (bytes_sz >= sz + offset) ? sz : 0; o->boffset = offset; o->id = r_num_rand (0xfffff000); o->kv = sdb_new0 (); @@ -1154,18 +1153,26 @@ R_API ut64 r_binfile_get_baddr (RBinFile *binfile) { return binfile && binfile->o ? binfile->o->baddr : 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); - if (o) return o->baddr; - return 0LL; + return o ? o->baddr : 0; +} + +static void binobj_set_baddr (RBinObject *o, ut64 baddr) { + if (!o) return; + o->baddr = baddr; +} + +static void binfile_set_baddr (RBinFile *binfile, ut64 baddr) { + if (!binfile) return; + binobj_set_baddr (binfile->o, baddr); } R_API void r_bin_set_baddr(RBin *bin, ut64 baddr) { RBinObject *o = r_bin_cur_object (bin); - if (o) { - o->baddr = baddr; - // XXX - update all the infos? - } + binobj_set_baddr (o, baddr); + // XXX - update all the infos? } R_API ut64 r_bin_get_boffset(RBin *bin) { @@ -1207,8 +1214,7 @@ R_API RList* r_bin_get_imports(RBin *bin) { R_API RBinInfo* r_bin_get_info(RBin *bin) { RBinObject *o = r_bin_cur_object (bin); - if (!o) return NULL; - return o->info; + return o ? o->info : NULL; } R_API RList* r_bin_get_libs(RBin *bin) { @@ -1238,9 +1244,9 @@ R_API RBinSection* r_bin_get_section_at(RBinObject *o, ut64 off, int va) { if (o) { // TODO: must be O(1) .. use sdb here r_list_foreach (o->sections, iter, section) { - from = va ? o->baddr+section->vaddr: section->paddr; - to = va ? (o->baddr+section->vaddr + section->vsize) : - (section->paddr+ section->size); + from = va ? binobj_a2b (o, section->vaddr) : section->paddr; + to = va ? (binobj_a2b (o, section->vaddr) + section->vsize) : + (section->paddr + section->size); if (off >= from && off < to) return section; } @@ -1740,30 +1746,39 @@ R_API ut64 r_bin_get_offset (RBin *bin) { return UT64_MAX; } -R_API ut64 r_binfile_get_vaddr (RBinFile *binfile, ut64 baddr, ut64 paddr, ut64 vaddr) { +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 ? vaddr : paddr; } -R_API ut64 r_bin_get_vaddr (RBin *bin, ut64 baddr, 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->is_debugger) { - if (baddr) - return baddr + paddr; + 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")) { - if (vaddr & 1) { - vaddr = (vaddr>>1)<<1; - } + if (!strcmp (bin->cur->o->info->arch, "arm") && (vaddr & 1)) { + vaddr = (vaddr >> 1) << 1; } } - return r_binfile_get_vaddr (bin->cur, baddr, paddr, vaddr); + return r_binfile_get_vaddr (bin->cur, paddr, vaddr); +} + +static ut64 binobj_a2b (RBinObject *o, ut64 addr) { + if (!o) return addr; + return o->baddr + addr; +} + +R_API ut64 r_bin_a2b (RBin *bin, ut64 addr) { + ut64 baddr = r_bin_get_baddr (bin); + return baddr + addr; } R_API ut64 r_bin_get_size (RBin *bin) { diff --git a/libr/core/anal.c b/libr/core/anal.c index 8d054daa7e..0e024eaced 100644 --- a/libr/core/anal.c +++ b/libr/core/anal.c @@ -1648,13 +1648,13 @@ R_API int r_core_anal_all(RCore *core) { } /* Main */ if ((binmain = r_bin_get_sym (core->bin, R_BIN_SYM_MAIN)) != NULL) { - ut64 addr = va? binmain->vaddr: binmain->paddr; // offset + va?baddr+binmain->vaddr:binmain->paddr; + ut64 addr = va? binmain->vaddr: binmain->paddr; r_core_anal_fcn (core, addr, -1, R_ANAL_REF_TYPE_NULL, depth); } if ((list = r_bin_get_entries (core->bin)) != NULL) r_list_foreach (list, iter, entry) - r_core_anal_fcn (core, offset + va? baddr+entry->vaddr:entry->paddr, -1, - R_ANAL_REF_TYPE_NULL, depth); + r_core_anal_fcn (core, (offset + va) ? r_bin_a2b (core->bin, entry->vaddr) + : entry->paddr, -1, R_ANAL_REF_TYPE_NULL, depth); /* Symbols (Imports are already analized by rabin2 on init) */ if ((list = r_bin_get_symbols (core->bin)) != NULL) r_list_foreach (list, iter, symbol) { diff --git a/libr/core/bin.c b/libr/core/bin.c index 96f512cd64..c8ba5b6b7b 100644 --- a/libr/core/bin.c +++ b/libr/core/bin.c @@ -19,45 +19,16 @@ static void pair(const char *a, const char *b) { // XXX - this may lead to conflicts with set by name static int r_core_bin_set_cur (RCore *core, RBinFile *binfile); -static ut64 rva (RBin *bin, int va, ut64 paddr, ut64 vaddr, ut64 baddr, ut64 laddr) { - RBinInfo *info = NULL; - if (bin && bin->cur && bin->cur->o && bin->cur->o->info) { - info = bin->cur->o->info; - } - if (info && info->type[0] != 'E') { - // if its not an executable, use va = 2 mode to load the syms - // hackaround to make -1 be va=0 and 0 to be no-laddr - if (baddr == 0) { - // its an object, so load it at base 0 - va = 2; - } - } - if (laddr == UT64_MAX) - va = 0; - if (info && info->bits != 16) { - // hackaround the hackaround for bios - if (va == 2) { - if (!baddr) baddr = 1; - // hackaround for PIE bins - } - } - switch (va) { - case 0: // pa $ rabin2 -p +static ut64 rva (RBin *bin, ut64 paddr, ut64 vaddr, int va) { + if (va) { + return r_bin_get_vaddr (bin, paddr, vaddr); + } else { return paddr; - case 1: // va $ rabin2 - { - ut64 addr = r_bin_get_vaddr (bin, baddr, paddr, vaddr); - if (baddr>addr) { - addr += baddr; - } - return addr; - } - case 2: // la $ rabin2 -B - if (!baddr && !laddr) - return vaddr; - return paddr + laddr; } - return vaddr + laddr; +} + +static ut64 rva_va (RBin *bin, ut64 paddr, ut64 vaddr) { + return r_bin_get_vaddr (bin, paddr, vaddr); } R_API int r_core_bin_set_by_fd (RCore *core, ut64 bin_fd) { @@ -128,7 +99,7 @@ R_API RBinFile * r_core_bin_cur (RCore *core) { return binfile; } -static int bin_strings (RCore *r, int mode, ut64 baddr, int va) { +static int bin_strings (RCore *r, int mode, int va) { char *q, str[R_FLAG_NAME_SIZE]; RBinSection *section; int hasstr, minstr, maxstr, rawstr; @@ -152,26 +123,13 @@ static int bin_strings (RCore *r, int mode, ut64 baddr, int va) { if (plugin->info && plugin->name) { if (!strcmp (plugin->name, "any")) { if (!rawstr) { -#if 0 - eprintf ("NOTE: Use '-e bin.rawstr=true' or 'r2 -zz' or '.!rabin2 -zz $FILE'" - " to find strings on unknown file types\n"); -#endif return R_FALSE; } } } -#if 0 - if (bin->minstrlen == 0) - bin->minstrlen = minstr; - else plugin->minstrlen? plugin->minstrlen: 4; -#endif - bin->minstrlen = minstr; -#if 0 - if (bin->minstrlen <= 0) - bin->minstrlen = R_MIN (minstr, 4); -#endif + bin->minstrlen = minstr; minstr = bin->minstrlen; if ((list = r_bin_get_strings (bin)) == NULL) @@ -180,7 +138,7 @@ static int bin_strings (RCore *r, int mode, ut64 baddr, int va) { if ((mode & R_CORE_BIN_JSON)) { r_cons_printf ("["); r_list_foreach (list, iter, string) { - ut64 vaddr = r_bin_get_vaddr (bin, baddr, + ut64 vaddr = r_bin_get_vaddr (bin, string->vaddr, string->paddr); ut64 paddr = string->paddr; if (maxstr && string->length>maxstr) @@ -200,7 +158,7 @@ static int bin_strings (RCore *r, int mode, ut64 baddr, int va) { r_cons_printf ("]"); } else if ((mode & R_CORE_BIN_SIMPLE)) { r_list_foreach (list, iter, string) { - ut64 addr = va? r_bin_get_vaddr (bin, baddr, + ut64 addr = va? r_bin_get_vaddr (bin, string->paddr, string->vaddr): string->paddr; if (maxstr && string->length>maxstr) { continue; @@ -219,8 +177,7 @@ static int bin_strings (RCore *r, int mode, ut64 baddr, int va) { r_cons_break (NULL, NULL); r_list_foreach (list, iter, string) { ut64 addr = va? string->vaddr: string->paddr; - //r_bin_get_vaddr (bin, baddr, string->vaddr, - // string->paddr): string->paddr; + if (string->lengthlength>maxstr) { @@ -511,8 +468,9 @@ static int bin_dwarf (RCore *core, int mode) { return R_TRUE; } -static int bin_pdb (RCore *core, ut64 baddr, int mode) { +static int bin_pdb (RCore *core, int mode) { R_PDB pdb = {0}; + ut64 baddr = r_bin_get_baddr (core->bin); pdb.cb_printf = r_cons_printf; if (!init_pdb_parser (&pdb, core->bin->file)) { @@ -558,16 +516,15 @@ static int bin_pdb (RCore *core, ut64 baddr, int mode) { return R_TRUE; } -static int bin_main (RCore *r, int mode, ut64 baddr, int va) { +static int bin_main (RCore *r, int mode, int va) { RBinAddr *binmain = r_bin_get_sym (r->bin, R_BIN_SYM_MAIN); ut64 main_addr = 0LL; - //RBinObject *binobj = r_bin_get_object(r->bin); - //ut64 baseaddr = binobj->baddr; + ut64 baddr = r_bin_get_baddr (r->bin); if (!binmain) return R_FALSE; if (va) { if (baddr) { - main_addr = baddr + binmain->paddr; + main_addr = r_bin_a2b (r->bin, binmain->paddr); } else { main_addr = binmain->vaddr; } @@ -594,19 +551,20 @@ static int bin_main (RCore *r, int mode, ut64 baddr, int va) { return R_TRUE; } -static int bin_entry (RCore *r, int mode, ut64 baddr, ut64 laddr, int va) { +static int bin_entry (RCore *r, int mode, ut64 laddr, int va) { char str[R_FLAG_NAME_SIZE]; RList *entries; RListIter *iter; RBinAddr *entry = NULL; int i = 0; + ut64 baddr = r_bin_get_baddr (r->bin); entries = r_bin_get_entries (r->bin); if (mode & R_CORE_BIN_JSON) { r_cons_printf ("["); r_list_foreach (entries, iter, entry) { - ut64 at = rva (r->bin, va, entry->paddr, entry->vaddr, baddr, laddr); + ut64 at = rva (r->bin, entry->paddr, entry->vaddr, va); r_cons_printf ("%s%"PFMT64d, iter->p? ",":"", at); } @@ -614,13 +572,13 @@ static int bin_entry (RCore *r, int mode, ut64 baddr, ut64 laddr, int va) { } else if (mode & R_CORE_BIN_SIMPLE) { r_list_foreach (entries, iter, entry) { - ut64 at = rva (r->bin, va, entry->paddr, entry->vaddr, baddr, laddr); + ut64 at = rva (r->bin, entry->paddr, entry->vaddr, va); r_cons_printf ("0x%08"PFMT64x"\n", at); } } else if ((mode & R_CORE_BIN_SET)) { r_list_foreach (entries, iter, entry) { - ut64 at = rva (r->bin, va, entry->paddr, entry->vaddr, baddr, laddr); + ut64 at = rva (r->bin, entry->paddr, entry->vaddr, va); if (at == 0) at = entry->vaddr; r_flag_space_set (r->flags, "symbols"); snprintf (str, R_FLAG_NAME_SIZE, "entry%i", i++); @@ -628,15 +586,15 @@ static int bin_entry (RCore *r, int mode, ut64 baddr, ut64 laddr, int va) { } /* Seek to the last entry point */ if (entry) - r_core_seek (r, va? baddr+entry->vaddr: entry->paddr, 0); + r_core_seek (r, va ? r_bin_a2b (r->bin, entry->vaddr) : entry->paddr, 0); } else { if (mode) r_cons_printf ("fs symbols\n"); else r_cons_printf ("[Entrypoints]\n"); r_list_foreach (entries, iter, entry) { ut64 paddr = entry->paddr; - ut64 vaddr = r_bin_get_vaddr (r->bin, baddr, paddr, entry->vaddr); - ut64 at = rva (r->bin, va, entry->paddr, entry->vaddr, baddr, laddr); + ut64 vaddr = r_bin_get_vaddr (r->bin, paddr, entry->vaddr); + ut64 at = rva (r->bin, entry->paddr, entry->vaddr, va); if (at > vaddr) { vaddr = at; } @@ -705,7 +663,7 @@ static char *resolveModuleOrdinal (Sdb *sdb, const char *module, int ordinal) { return NULL; } -static int bin_relocs (RCore *r, int mode, ut64 baddr, int va) { +static int bin_relocs (RCore *r, int mode, int va) { char str[R_FLAG_NAME_SIZE]; RList *relocs; RListIter *iter; @@ -898,7 +856,7 @@ static RBinSymbol *get_symbol(RBin *bin, RList *symbols, const char *name) { /* XXX: This is a hack to get PLT references in rabin2 -i */ /* imp. is a prefix that can be rewritten by the symbol table */ -static ut64 impaddr(RBin *bin, int va, ut64 baddr, const char *name) { +static ut64 impaddr(RBin *bin, int va, const char *name) { char impname[512]; RList *symbols; RBinSymbol *s; @@ -911,14 +869,14 @@ static ut64 impaddr(RBin *bin, int va, ut64 baddr, const char *name) { s = get_symbol (bin, symbols, impname); if (s) { if (va) { - return r_bin_get_vaddr (bin, baddr, s->paddr, s->vaddr); + return r_bin_get_vaddr (bin, s->paddr, s->vaddr); } return s->paddr; } return 0LL; } -static int bin_imports (RCore *r, int mode, ut64 baddr, int va, const char *name) { +static int bin_imports (RCore *r, int mode, int va, const char *name) { // int bin_demangle = r_config_get_i (r->config, "bin.demangle"); RBinImport *import; RListIter *iter; @@ -936,7 +894,7 @@ static int bin_imports (RCore *r, int mode, ut64 baddr, int va, const char *name continue; str = r_str_utf16_encode (import->name, -1); str = r_str_replace (str, "\"", "\\\"", 1); - addr = impaddr (r->bin, va, baddr, import->name); + addr = impaddr (r->bin, va, import->name); r_cons_printf ("%s{\"name\":\"%s\", \"plt\":%"PFMT64d"}", iter->p?",":"", str, addr); free (str); @@ -981,7 +939,7 @@ static int bin_imports (RCore *r, int mode, ut64 baddr, int va, const char *name r_list_foreach (imports, iter, import) { if (name && strcmp (import->name, name)) continue; - addr = impaddr (r->bin, va, baddr, import->name); + addr = impaddr (r->bin, va, import->name); if (mode) { // TODO(eddyb) use the logic below for symbols that are imports. /*r_name_filter (import->name, sizeof (import->name)); @@ -1087,7 +1045,7 @@ static void snFini(SymName *sn) { R_FREE (sn->methflag); } -static int bin_symbols (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut64 at, const char *name) { +static int bin_symbols (RCore *r, int mode, ut64 laddr, int va, ut64 at, const char *name) { RBinInfo *info = r_bin_get_info (r->bin); int is_arm = info && info->arch && !strcmp (info->arch, "arm"); int bin_demangle = r_config_get_i (r->config, "bin.demangle"); @@ -1109,8 +1067,8 @@ static int bin_symbols (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut64 r_cons_printf ("["); r_list_foreach (symbols, iter, symbol) { char *str; - ut64 at = rva (r->bin, va, symbol->paddr, symbol->vaddr, baddr, laddr); - ut64 vaddr = rva (r->bin, 1, symbol->paddr, symbol->vaddr, baddr, laddr); + ut64 at = rva (r->bin, symbol->paddr, symbol->vaddr, va); + ut64 vaddr = rva_va (r->bin, symbol->paddr, symbol->vaddr); SymName sn; snInit (r, &sn, symbol, lang); @@ -1136,8 +1094,7 @@ static int bin_symbols (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut64 } else if ((mode & R_CORE_BIN_SIMPLE)) { r_list_foreach (symbols, iter, symbol) { - ut64 at = rva (r->bin, va, symbol->paddr, - symbol->vaddr, baddr, laddr); + ut64 at = rva (r->bin, symbol->paddr, symbol->vaddr, va); char *name = strdup (symbol->name); if (bin_demangle) { const char *symname = name; @@ -1160,8 +1117,7 @@ static int bin_symbols (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut64 r_flag_space_set (r->flags, "symbols"); r_list_foreach (symbols, iter, symbol) { SymName sn; - ut64 addr = rva (r->bin, va, symbol->paddr, - symbol->vaddr, baddr, laddr); + ut64 addr = rva (r->bin, symbol->paddr, symbol->vaddr, va); snInit (r, &sn, symbol, lang); @@ -1219,7 +1175,7 @@ static int bin_symbols (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut64 } r_list_foreach (symbols, iter, symbol) { - ut64 addr = va? r_bin_get_vaddr (r->bin, baddr, 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) { @@ -1298,7 +1254,7 @@ static int bin_symbols (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut64 return R_TRUE; } -static int bin_sections (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut64 at, const char *name, const char *chksum) { +static int bin_sections (RCore *r, int mode, ut64 laddr, int va, ut64 at, const char *name, const char *chksum) { char str[R_FLAG_NAME_SIZE]; RBinSection *section; ut64 secbase = 0LL; @@ -1314,11 +1270,10 @@ static int bin_sections (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut6 r_cons_printf ("["); i = 0; r_list_foreach (sections, iter, section) { - ut64 addr = rva (r->bin, va, section->paddr, section->vaddr, baddr, laddr); - if (va) delta = section->vaddr - r_bin_get_vaddr (r->bin, baddr, section->paddr, section->vaddr); + ut64 addr = rva (r->bin, section->paddr, section->vaddr, va); + if (va) delta = section->vaddr - r_bin_get_vaddr (r->bin, section->paddr, section->vaddr); else delta = 0; - //ut64 addr = va? r_bin_get_vaddr (r->bin, baddr, section->paddr, - // section->vaddr): section->paddr; + if (chksum) { char *chkstr; ut8 *data = malloc (section->size); @@ -1355,7 +1310,7 @@ static int bin_sections (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut6 if ((mode & R_CORE_BIN_SIMPLE)) { char *chkstr = NULL; r_list_foreach (sections, iter, section) { - ut64 addr = rva (r->bin, va, section->paddr, section->vaddr, baddr, laddr); + ut64 addr = rva (r->bin, section->paddr, section->vaddr, va); if (chksum) { ut8 *data = malloc (section->size); ut32 datalen = section->size; @@ -1378,8 +1333,7 @@ static int bin_sections (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut6 int fd = r_core_file_cur_fd (r); r_flag_space_set (r->flags, "sections"); r_list_foreach (sections, iter, section) { -// baddr already implicit in section->vaddr ? - ut64 addr = rva (r->bin, va, section->paddr, section->vaddr, baddr, laddr); + ut64 addr = rva (r->bin, section->paddr, section->vaddr, va); if (!secbase || (section->vaddr && section->vaddr vaddr; #if LOAD_BSS_MALLOC @@ -1423,25 +1377,12 @@ static int bin_sections (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut6 r_io_section_add (r->io, section->paddr, addr, section->size, section->vsize, section->srwx, section->name, 0, fd); } - // H -> Header fields - if (0) { -/* - ut64 size = r_io_size (r->io); - if (size == 0) - size = r->file->size; -*/ - ut64 size = r_io_desc_size (r->io, r->file->desc); - secbase >>= 16; - secbase <<= 16; - secbase = baddr; // always override? - r_io_section_add (r->io, 0, secbase, size, size, 7, "ehdr", 0, fd); - } } else { RBinInfo *info = r_bin_get_info (r->bin); if (!at) r_cons_printf (mode? "fs sections\n": "[Sections]\n"); r_list_foreach (sections, iter, section) { - ut64 addr = rva (r->bin, va, section->paddr, section->vaddr, baddr, laddr); + ut64 addr = rva (r->bin, section->paddr, section->vaddr, va); if (name && strcmp (section->name, name)) continue; r_name_filter (section->name, sizeof (section->name)); @@ -1517,7 +1458,7 @@ static int bin_sections (RCore *r, int mode, ut64 baddr, ut64 laddr, int va, ut6 return R_TRUE; } -static int bin_fields (RCore *r, int mode, ut64 baddr, int va) { +static int bin_fields (RCore *r, int mode, int va) { RList *fields; RListIter *iter; RBinField *field; @@ -1525,6 +1466,7 @@ static int bin_fields (RCore *r, int mode, ut64 baddr, int va) { RBin *bin = r->bin; RBinFile *binfile = r_core_bin_cur (r); ut64 size = binfile ? binfile->size : UT64_MAX; + ut64 baddr = r_bin_get_baddr (r->bin); if ((fields = r_bin_get_fields (bin)) == NULL) return R_FALSE; @@ -1532,7 +1474,7 @@ static int bin_fields (RCore *r, int mode, ut64 baddr, int va) { if (mode & R_CORE_BIN_JSON) { r_cons_printf ("["); r_list_foreach (fields, iter, field) { - ut64 addr = va? r_bin_get_vaddr (bin, baddr, field->paddr, + ut64 addr = va? r_bin_get_vaddr (bin, field->paddr, field->vaddr): field->paddr; r_cons_printf ("%s{\"name\":\"%s\"," "\"paddr\":%"PFMT64d"}", @@ -1551,7 +1493,7 @@ static int bin_fields (RCore *r, int mode, ut64 baddr, int va) { else r_cons_printf ("[Header fields]\n"); r_list_foreach (fields, iter, field) { - ut64 addr = va? r_bin_get_vaddr (bin, baddr, field->paddr, + ut64 addr = va? r_bin_get_vaddr (bin, field->paddr, field->vaddr): field->paddr; if (mode) { r_name_filter (field->name, sizeof (field->name)); @@ -1745,12 +1687,7 @@ static int bin_mem (RCore *r, int mode) { R_API int r_core_bin_info (RCore *core, int action, int mode, int va, RCoreBinFilter *filter, ut64 loadaddr, const char *chksum) { int ret = R_TRUE; const char *name = NULL; - ut64 at = 0, baseaddr = 0LL; - - // WTF, should be the same but we are not keeping it - if (core->bin && core->bin->cur && core->bin->cur->o) { - baseaddr = core->bin->cur->o->baddr; - } + ut64 at = 0; if (loadaddr) va = 2; @@ -1759,28 +1696,29 @@ R_API int r_core_bin_info (RCore *core, int action, int mode, int va, RCoreBinFi at = filter->offset; if (filter && filter->name) name = filter->name; + if ((action & R_CORE_BIN_ACC_STRINGS)) - ret &= bin_strings (core, mode, baseaddr, va); + ret &= bin_strings (core, mode, va); if ((action & R_CORE_BIN_ACC_INFO)) ret &= bin_info (core, mode); if ((action & R_CORE_BIN_ACC_MAIN)) - ret &= bin_main (core, mode, baseaddr, va); + ret &= bin_main (core, mode, va); if ((action & R_CORE_BIN_ACC_DWARF)) ret &= bin_dwarf (core, mode); if ((action & R_CORE_BIN_ACC_PDB)) - ret &= bin_pdb (core, baseaddr, mode); + ret &= bin_pdb (core, mode); if ((action & R_CORE_BIN_ACC_ENTRIES)) - ret &= bin_entry (core, mode, baseaddr, loadaddr, va); + ret &= bin_entry (core, mode, loadaddr, va); if ((action & R_CORE_BIN_ACC_RELOCS)) - ret &= bin_relocs (core, mode, baseaddr, va); + ret &= bin_relocs (core, mode, va); if ((action & R_CORE_BIN_ACC_IMPORTS)) - ret &= bin_imports (core, mode, baseaddr, va, name); + ret &= bin_imports (core, mode, va, name); if ((action & R_CORE_BIN_ACC_SYMBOLS)) - ret &= bin_symbols (core, mode, baseaddr, loadaddr, va, at, name); + ret &= bin_symbols (core, mode, loadaddr, va, at, name); if ((action & R_CORE_BIN_ACC_SECTIONS)) - ret &= bin_sections (core, mode, baseaddr, loadaddr, va, at, name, chksum); + ret &= bin_sections (core, mode, loadaddr, va, at, name, chksum); if ((action & R_CORE_BIN_ACC_FIELDS)) - ret &= bin_fields (core, mode, baseaddr, va); + ret &= bin_fields (core, mode, va); if ((action & R_CORE_BIN_ACC_LIBS)) ret &= bin_libs (core, mode); if ((action & R_CORE_BIN_ACC_CLASSES)) diff --git a/libr/core/cmd_debug.c b/libr/core/cmd_debug.c index a33e9b4888..7234f5c548 100644 --- a/libr/core/cmd_debug.c +++ b/libr/core/cmd_debug.c @@ -838,17 +838,16 @@ 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 (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; + if (core->bin && + ((addr != -1 && (addr >= map->addr && addr < map->addr_end)) || + (libname != NULL && (strstr (map->name, libname))))) { filter.offset = 0LL; filter.name = (char *)symname; - baddr = o->baddr; - o->baddr = map->addr; + baddr = r_bin_get_baddr (core->bin); + r_bin_set_baddr (core->bin, map->addr); r_core_bin_info (core, R_CORE_BIN_ACC_SYMBOLS, (input[1]=='*'), R_TRUE, &filter, 0, NULL); - o->baddr = baddr; + r_bin_set_baddr (core->bin, baddr); break; } } diff --git a/libr/core/file.c b/libr/core/file.c index 02c96e3d11..baf2a8e0eb 100644 --- a/libr/core/file.c +++ b/libr/core/file.c @@ -388,65 +388,6 @@ static int r_core_file_do_load_for_io_plugin (RCore *r, ut64 baseaddr, ut64 load return R_TRUE; } -#if 0 -// XXX - remove this code after June 2014, because current code setup is sufficient -static int r_core_file_do_load_for_hex (RCore *r, ut64 baddr, ut64 loadaddr, const char *filenameuri) { - // HEXEDITOR - RBinFile * binfile = NULL; - ut64 fd = r_core_file_cur_fd (r); - int i = 0; - int xtr_idx = 0; // if 0, load all if xtr is used - int treat_as_rawstr = R_FALSE; - - if (!r_bin_load (r->bin, filenameuri, baddr, loadaddr, xtr_idx, fd, treat_as_rawstr)) { - treat_as_rawstr = R_TRUE; - if (!r_bin_load (r->bin, filenameuri, baddr, loadaddr, xtr_idx, fd, treat_as_rawstr)) - return R_FALSE; - } - - binfile = r_core_bin_cur (r); - if (binfile) { - r_core_bin_bind (r, binfile); - } - - // binary files should be treated as views into a file - // not the actual file - /* - { - RListIter *iter; - RIOMap *im; - - r_list_foreach (r->io->maps, iter, im) { - if (binfile->size > 0) { - im->delta = binfile->offset; - im->to = im->from + binfile->size; - } - } - }*/ - - if (binfile->narch>1 && r_config_get_i (r->config, "scr.prompt")) { - int narch = binfile->narch; - eprintf ("NOTE: Fat binary found. Selected sub-bin is: -a %s -b %d\n", - r->assembler->cur->arch, r->assembler->bits); - eprintf ("NOTE: Use -a and -b to select sub binary in fat binary\n"); - - for (i=0; ibin, binfile->file, i); - RBinObject *lbinobj = lbinfile ? lbinfile->o : NULL; - if (lbinobj && lbinobj->info) { - eprintf (" $ r2 -a %s -b %d %s # 0x%08"PFMT64x"\n", - lbinobj->info->arch, - lbinobj->info->bits, - binfile->file, - lbinobj->boffset); - } else eprintf ("No extract info found.\n"); - } - } - - return R_TRUE; -} -#endif - R_API int r_core_bin_load(RCore *r, const char *filenameuri, ut64 baddr) { const char *suppress_warning = r_config_get (r->config, "file.nowarn"); ut64 loadaddr = 0; @@ -630,7 +571,7 @@ R_API RCoreFile *r_core_file_open_many(RCore *r, const char *file, int flags, ut return top_file; } -R_API RCoreFile *r_core_file_open(RCore *r, const char *file, int flags, ut64 loadaddr) { +R_API RCoreFile *r_core_file_open (RCore *r, const char *file, int flags, ut64 loadaddr) { const char *suppress_warning = r_config_get (r->config, "file.nowarn"); const int openmany = r_config_get_i (r->config, "file.openmany"); const char *cp; diff --git a/libr/include/r_bin.h b/libr/include/r_bin.h index 59ee7d018a..55225b5246 100644 --- a/libr/include/r_bin.h +++ b/libr/include/r_bin.h @@ -467,7 +467,8 @@ R_API void r_bin_list_archs(RBin *bin, int mode); R_API void r_bin_set_user_ptr(RBin *bin, void *user); R_API RBuffer *r_bin_create (RBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen); R_API ut64 r_bin_get_offset (RBin *bin); -R_API ut64 r_bin_get_vaddr (RBin *bin, ut64 baddr, ut64 paddr, ut64 vaddr); +R_API ut64 r_bin_get_vaddr (RBin *bin, ut64 paddr, ut64 vaddr); +R_API ut64 r_bin_a2b (RBin *bin, ut64 addr); R_API int r_bin_file_delete(RBin *bin, ut32 bin_fd); R_API int r_bin_file_delete_all(RBin *bin); R_API int r_bin_file_set_cur_by_fd (RBin *bin, ut32 bin_fd);