2014-01-20 02:29:50 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2014 - pancake, nibble */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2012-08-07 08:28:03 +00:00
|
|
|
// TODO: dlopen library and show address
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
#include <r_types.h>
|
2010-01-24 11:40:48 +00:00
|
|
|
#include <r_util.h>
|
2009-03-08 15:49:15 +00:00
|
|
|
#include <r_lib.h>
|
2010-03-10 10:01:38 +00:00
|
|
|
#include <r_list.h>
|
2009-03-08 15:49:15 +00:00
|
|
|
#include <r_bin.h>
|
2013-11-09 23:15:30 +00:00
|
|
|
#include <r_io.h>
|
2010-03-10 10:01:38 +00:00
|
|
|
#include <list.h>
|
2009-03-09 12:03:42 +00:00
|
|
|
#include "../config.h"
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2013-06-14 00:51:33 +00:00
|
|
|
R_LIB_VERSION(r_bin);
|
|
|
|
|
2014-02-13 00:48:15 +00:00
|
|
|
#define DB a->db;
|
2014-01-18 15:26:09 +00:00
|
|
|
#define RBINLISTFREE(x) if(x){r_list_free(x);x=NULL;}
|
|
|
|
|
2010-05-25 23:42:22 +00:00
|
|
|
static RBinPlugin *bin_static_plugins[] = { R_BIN_STATIC_PLUGINS };
|
2010-09-24 19:23:13 +00:00
|
|
|
static RBinXtrPlugin *bin_xtr_static_plugins[] = { R_BIN_XTR_STATIC_PLUGINS };
|
2010-02-07 12:17:51 +00:00
|
|
|
|
2014-01-18 15:26:09 +00:00
|
|
|
static void get_strings_range(RBinFile *arch, RList *list, int min, ut64 from, ut64 to, ut64 scnrva);
|
|
|
|
static void set_bin_items(RBin *bin, RBinPlugin *cp);
|
|
|
|
static void r_bin_free_bin_files (RBin *bin);
|
|
|
|
static void r_bin_file_free (RBinFile *a);
|
|
|
|
static void r_bin_free_items(RBin *bin);
|
|
|
|
static void r_bin_init(RBin *bin, int rawstr, ut64 baseaddr, ut64 loadaddr);
|
|
|
|
|
|
|
|
static void get_strings_range(RBinFile *arch, RList *list, int min, ut64 from, ut64 to, ut64 scnrva) {
|
2009-07-05 14:49:47 +00:00
|
|
|
char str[R_BIN_SIZEOF_STRINGS];
|
2010-03-10 10:01:38 +00:00
|
|
|
int i, matches = 0, ctr = 0;
|
2010-06-23 15:30:16 +00:00
|
|
|
RBinString *ptr = NULL;
|
2014-02-10 16:19:18 +00:00
|
|
|
char type = 'A';
|
2009-03-16 23:34:45 +00:00
|
|
|
|
2013-04-25 22:04:05 +00:00
|
|
|
if (!arch->rawstr)
|
|
|
|
if (!arch->curplugin || !arch->curplugin->info)
|
|
|
|
return;
|
2014-02-13 00:48:15 +00:00
|
|
|
if (arch->curplugin && min==0)
|
2013-12-29 02:42:18 +00:00
|
|
|
min = arch->curplugin->minstrlen;
|
|
|
|
if (min==0)
|
|
|
|
min = 4; // defaults
|
|
|
|
if (min <= 0)
|
|
|
|
return;
|
|
|
|
|
2013-04-25 22:04:05 +00:00
|
|
|
if (arch && arch->buf && (!to || to > arch->buf->length))
|
2011-09-02 01:09:26 +00:00
|
|
|
to = arch->buf->length;
|
2013-12-04 02:58:18 +00:00
|
|
|
if (to != 0 && (to<1 || to > 0xf00000)) {
|
2013-02-13 00:20:42 +00:00
|
|
|
eprintf ("WARNING: bin_strings buffer is too big at 0x%08"PFMT64x"\n", from);
|
2012-01-26 02:18:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-12-29 02:42:18 +00:00
|
|
|
if (!arch->buf)
|
|
|
|
return;
|
|
|
|
if (to == 0 && arch->buf)
|
2012-12-09 00:39:27 +00:00
|
|
|
to = arch->buf->length;
|
2012-08-13 15:42:25 +00:00
|
|
|
if (arch->buf && arch->buf->buf)
|
2014-01-11 06:56:02 +00:00
|
|
|
for (i = from; i < to; i++) {
|
2012-07-01 22:38:02 +00:00
|
|
|
if ((IS_PRINTABLE (arch->buf->buf[i])) && \
|
|
|
|
matches < R_BIN_SIZEOF_STRINGS-1) {
|
2010-09-24 19:23:13 +00:00
|
|
|
str[matches] = arch->buf->buf[i];
|
2012-07-01 22:38:02 +00:00
|
|
|
/* add support for wide char strings */
|
|
|
|
if (arch->buf->buf[i+1]==0) {
|
2014-02-10 16:19:18 +00:00
|
|
|
if (IS_PRINTABLE (arch->buf->buf[i+2])) {
|
|
|
|
if (arch->buf->buf[i+3]==0) {
|
2012-07-01 22:38:02 +00:00
|
|
|
i++;
|
2014-02-10 16:19:18 +00:00
|
|
|
type = 'W';
|
|
|
|
}
|
|
|
|
}
|
2012-07-01 22:38:02 +00:00
|
|
|
}
|
2010-06-20 22:48:06 +00:00
|
|
|
matches++;
|
2011-02-23 19:53:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* check if the length fits in our request */
|
|
|
|
if (matches >= min) {
|
|
|
|
if (!(ptr = R_NEW (RBinString))) {
|
|
|
|
eprintf ("Error allocating string\n");
|
|
|
|
break;
|
2009-03-16 23:34:45 +00:00
|
|
|
}
|
2011-02-23 19:53:56 +00:00
|
|
|
str[matches] = '\0';
|
|
|
|
ptr->offset = i-matches;
|
2013-06-18 00:14:57 +00:00
|
|
|
if (scnrva) {
|
2014-02-13 00:48:15 +00:00
|
|
|
ptr->rva = (ptr->offset+scnrva-from);
|
2013-06-18 00:14:57 +00:00
|
|
|
} else {
|
|
|
|
ptr->rva = ptr->offset;
|
|
|
|
}
|
2011-12-01 08:32:16 +00:00
|
|
|
//HACK if (scnrva) ptr->rva = ptr->offset-from+scnrva; else ptr->rva = ptr->offset;
|
2011-06-09 00:12:46 +00:00
|
|
|
ptr->size = matches+1;
|
2014-02-10 23:08:12 +00:00
|
|
|
ptr->length = ptr->size << ((type=='W')? 1:0);
|
2014-02-10 16:19:18 +00:00
|
|
|
ptr->type = type;
|
|
|
|
type = 'A';
|
2011-02-23 19:53:56 +00:00
|
|
|
ptr->ordinal = ctr;
|
|
|
|
// copying so many bytes here..
|
|
|
|
memcpy (ptr->string, str, R_BIN_SIZEOF_STRINGS);
|
|
|
|
ptr->string[R_BIN_SIZEOF_STRINGS-1] = '\0';
|
2012-07-01 22:38:02 +00:00
|
|
|
//r_name_filter (ptr->string, R_BIN_SIZEOF_STRINGS-1);
|
2011-02-23 19:53:56 +00:00
|
|
|
r_list_append (list, ptr);
|
2014-02-13 00:48:15 +00:00
|
|
|
//if (!sdb_add (DB,
|
2011-02-23 19:53:56 +00:00
|
|
|
ctr++;
|
2009-03-16 23:34:45 +00:00
|
|
|
}
|
2011-02-23 19:53:56 +00:00
|
|
|
matches = 0;
|
2009-03-16 23:34:45 +00:00
|
|
|
}
|
2010-06-23 15:30:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-18 15:26:09 +00:00
|
|
|
static int is_data_section(RBinFile *a, RBinSection *s) {
|
2012-08-04 21:48:06 +00:00
|
|
|
RBinObject *o = a->o;
|
|
|
|
if (strstr (o->info->bclass, "MACH0") && strstr (s->name, "_cstring")) // OSX
|
2011-10-05 00:38:37 +00:00
|
|
|
return 1;
|
2013-03-08 11:58:03 +00:00
|
|
|
if (strstr (o->info->bclass, "ELF") && strstr (s->name, "data") && !strstr (s->name, "rel")) // LINUX
|
2011-10-05 00:38:37 +00:00
|
|
|
return 1;
|
2012-05-30 01:23:53 +00:00
|
|
|
#define X 1
|
|
|
|
#define ROW (4|2)
|
2014-02-13 00:48:15 +00:00
|
|
|
if (strstr (o->info->bclass, "PE") && s->srwx & ROW && !(s->srwx&X) && s->size>0 )
|
2011-10-05 00:38:37 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-18 15:26:09 +00:00
|
|
|
static RList* get_strings(RBinFile *a, int min) {
|
2010-06-23 15:30:16 +00:00
|
|
|
int count = 0;
|
2012-11-07 03:25:42 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RBinSection *section;
|
|
|
|
RList *ret = r_list_new ();
|
|
|
|
if (!ret) {
|
2010-06-23 15:30:16 +00:00
|
|
|
eprintf ("Error allocating array\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret->free = free;
|
2013-11-08 23:28:58 +00:00
|
|
|
if (a->o->sections && !a->rawstr) {
|
2012-08-04 21:48:06 +00:00
|
|
|
r_list_foreach (a->o->sections, iter, section) {
|
2011-10-05 00:38:37 +00:00
|
|
|
if (is_data_section (a, section)) {
|
2012-11-07 03:25:42 +00:00
|
|
|
count++;
|
2012-08-07 08:28:03 +00:00
|
|
|
get_strings_range (a, ret, min,
|
2012-11-07 03:25:42 +00:00
|
|
|
section->offset,
|
|
|
|
section->offset+section->size,
|
|
|
|
section->rva);
|
2010-06-23 15:30:16 +00:00
|
|
|
}
|
2014-01-11 06:56:02 +00:00
|
|
|
}
|
2013-05-23 01:47:57 +00:00
|
|
|
if (r_list_empty (a->o->sections)) {
|
|
|
|
int i, next = 0, from = 0, funn = 0, to = 0;
|
|
|
|
ut8 *buf = a->buf->buf;
|
|
|
|
for (i=0; i<a->buf->length; i++) {
|
|
|
|
if (!buf[i] || IS_PRINTABLE (buf[i])) {
|
|
|
|
if (buf[i]) {
|
|
|
|
if (!from) from = i;
|
|
|
|
funn++;
|
|
|
|
next = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
next++;
|
|
|
|
if (next>5) from = 0;
|
|
|
|
if (!to) to = i;
|
|
|
|
to = i;
|
|
|
|
if (from && next==5 && funn>16) {
|
|
|
|
get_strings_range (a, ret, min, from, to, 0);
|
|
|
|
//eprintf ("FUNN %d\n", funn);
|
|
|
|
//eprintf ("MIN %d %d\n", from, to);
|
|
|
|
funn = 0;
|
|
|
|
from = 0;
|
|
|
|
to = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 22:15:16 +00:00
|
|
|
} else {
|
|
|
|
get_strings_range (a, ret, min,
|
|
|
|
0, a->size, 0);
|
2010-06-23 15:30:16 +00:00
|
|
|
}
|
2009-03-16 23:34:45 +00:00
|
|
|
return ret;
|
2009-03-16 20:07:31 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 01:00:49 +00:00
|
|
|
R_API int r_bin_load_languages(RBin *bin) {
|
|
|
|
if (r_bin_lang_objc (bin))
|
|
|
|
return R_BIN_NM_OBJC;
|
|
|
|
if (r_bin_lang_cxx (bin))
|
|
|
|
return R_BIN_NM_CXX;
|
|
|
|
return R_BIN_NM_NONE;
|
2012-11-07 03:25:42 +00:00
|
|
|
}
|
2013-11-24 23:45:18 +00:00
|
|
|
|
2013-11-09 23:15:30 +00:00
|
|
|
static void set_bin_items(RBin *bin, RBinPlugin *cp) {
|
2014-01-18 15:26:09 +00:00
|
|
|
RBinFile *a = bin->cur;
|
2013-11-09 23:15:30 +00:00
|
|
|
RBinObject *o = a->o;
|
|
|
|
int i, minlen = bin->minstrlen;
|
|
|
|
|
|
|
|
if (cp->baddr) o->baddr = cp->baddr (a);
|
2013-12-19 18:43:44 +00:00
|
|
|
if (cp->boffset) o->boffset = cp->boffset (a);
|
2013-11-09 23:15:30 +00:00
|
|
|
// XXX: no way to get info from xtr pluginz?
|
|
|
|
if (cp->size) o->size = cp->size (a);
|
|
|
|
if (cp->binsym)
|
|
|
|
for (i=0; i<R_BIN_SYM_LAST; i++)
|
|
|
|
o->binsym[i] = cp->binsym (a, i);
|
|
|
|
if (cp->entries) o->entries = cp->entries (a);
|
|
|
|
if (cp->fields) o->fields = cp->fields (a);
|
|
|
|
if (cp->imports) o->imports = cp->imports (a);
|
|
|
|
o->info = cp->info? cp->info (a): NULL;
|
|
|
|
if (cp->libs) o->libs = cp->libs (a);
|
|
|
|
if (cp->relocs) o->relocs = cp->relocs (a);
|
|
|
|
if (cp->sections) o->sections = cp->sections (a);
|
|
|
|
if (cp->strings) o->strings = cp->strings (a);
|
|
|
|
else o->strings = get_strings (a, minlen);
|
|
|
|
if (cp->symbols) o->symbols = cp->symbols (a);
|
|
|
|
if (cp->classes) o->classes = cp->classes (a);
|
|
|
|
if (cp->lines) o->lines = cp->lines (a);
|
|
|
|
o->lang = r_bin_load_languages (bin);
|
|
|
|
}
|
|
|
|
|
2014-01-11 06:56:02 +00:00
|
|
|
R_API int r_bin_io_load(RBin *bin, RIO *io, RIODesc *desc, ut64 baseaddr, ut64 loadaddr, int dummy) {
|
2013-11-09 23:15:30 +00:00
|
|
|
int rawstr = 0;
|
2013-12-10 02:35:59 +00:00
|
|
|
ut8* buf_bytes;
|
2014-01-11 06:56:02 +00:00
|
|
|
ut64 start, end,
|
|
|
|
sz = -1,
|
2013-11-09 23:15:30 +00:00
|
|
|
offset = 0;
|
|
|
|
|
2014-01-11 06:56:02 +00:00
|
|
|
RListIter *it;
|
|
|
|
RBinXtrPlugin *xtr;
|
|
|
|
RBinPlugin *any, *plugin;
|
2014-01-18 15:26:09 +00:00
|
|
|
RBinFile *a;
|
2014-01-11 06:56:02 +00:00
|
|
|
|
2013-12-10 02:35:59 +00:00
|
|
|
if (!io || !io->plugin || !io->plugin->read || !io->plugin->lseek)
|
2013-11-09 23:15:30 +00:00
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
buf_bytes = NULL;
|
2014-01-18 15:26:09 +00:00
|
|
|
end = desc->plugin->lseek (io, desc, 0, SEEK_END);
|
|
|
|
start = desc->plugin->lseek (io, desc, 0, SEEK_SET);
|
2013-11-09 23:15:30 +00:00
|
|
|
sz = -1;
|
|
|
|
offset = 0;
|
2014-01-11 06:56:02 +00:00
|
|
|
|
2014-02-27 02:01:14 +00:00
|
|
|
if (end == -1 || start == -1)
|
|
|
|
return R_FALSE;
|
2013-11-09 23:15:30 +00:00
|
|
|
|
|
|
|
sz = end - start;
|
2014-02-27 02:01:14 +00:00
|
|
|
buf_bytes = malloc (sz);
|
2014-01-11 06:56:02 +00:00
|
|
|
|
2014-01-18 15:26:09 +00:00
|
|
|
if (!buf_bytes || !desc->plugin->read (io, desc, buf_bytes, sz)) {
|
2013-12-10 02:35:59 +00:00
|
|
|
free (buf_bytes);
|
2013-11-09 23:15:30 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur = R_NEW0 (RBinFile);
|
|
|
|
bin->cur->file = strdup (desc->name);
|
|
|
|
bin->cur->buf = r_buf_new ();
|
2014-02-27 02:01:14 +00:00
|
|
|
bin->cur->rawstr = NULL;
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->file = desc->name;
|
|
|
|
|
|
|
|
if (bin->cur->buf)
|
|
|
|
r_buf_set_bytes (bin->cur->buf, buf_bytes, sz);
|
2013-12-10 02:35:59 +00:00
|
|
|
free (buf_bytes);
|
2013-11-09 23:15:30 +00:00
|
|
|
|
|
|
|
// Here is the pertinent code from r_bin_init
|
|
|
|
// we can't call r_bin_init, because it will
|
|
|
|
// deref all work done previously by IO Plugin.
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->o = R_NEW0 (RBinObject);
|
2014-01-11 06:56:02 +00:00
|
|
|
|
|
|
|
// XXX - probably not the best mechanism but its better
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->o->loadaddr = loadaddr;
|
|
|
|
bin->cur->o->baddr = baseaddr;
|
2014-01-11 06:56:02 +00:00
|
|
|
|
|
|
|
r_list_foreach (bin->binxtrs, it, xtr) {
|
2014-01-23 03:29:07 +00:00
|
|
|
if (xtr->check && xtr->check (bin)) {
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->curxtr = xtr;
|
2014-01-11 06:56:02 +00:00
|
|
|
break;
|
2013-11-09 23:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-18 15:26:09 +00:00
|
|
|
if (bin->cur->curxtr && bin->cur->curxtr->load)
|
|
|
|
bin->cur->curxtr->load (bin);
|
2013-11-09 23:15:30 +00:00
|
|
|
|
2014-01-18 15:26:09 +00:00
|
|
|
a = bin->cur;
|
2014-01-11 06:56:02 +00:00
|
|
|
//int minlen = bin->minstrlen;
|
|
|
|
a->curplugin = NULL;
|
|
|
|
|
|
|
|
r_list_foreach (bin->plugins, it, plugin) {
|
|
|
|
if (strncmp (plugin->name, "any", 5)==0) any = plugin;
|
|
|
|
if ((dummy && !strncmp (plugin->name, "any", 5)) ||
|
|
|
|
(!dummy && (plugin->check && plugin->check (a)))) {
|
|
|
|
a->curplugin = plugin;
|
|
|
|
break;
|
2013-11-09 23:15:30 +00:00
|
|
|
}
|
2014-01-11 06:56:02 +00:00
|
|
|
}
|
2013-12-04 02:58:18 +00:00
|
|
|
|
2014-01-11 06:56:02 +00:00
|
|
|
if (a->curplugin == NULL)
|
|
|
|
a->curplugin = any;
|
2013-12-03 04:46:19 +00:00
|
|
|
|
2014-01-11 06:56:02 +00:00
|
|
|
if (a->curplugin && a->curplugin->minstrlen)
|
|
|
|
bin->minstrlen = a->curplugin->minstrlen;
|
|
|
|
|
|
|
|
if (a->curplugin && a->curplugin->load) {
|
|
|
|
if (! a->curplugin->load (a) )
|
|
|
|
return R_FALSE;
|
|
|
|
set_bin_items (bin, a->curplugin);
|
2013-11-09 23:15:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-27 02:01:14 +00:00
|
|
|
bin->cur->o->referenced ++;
|
2014-01-18 15:26:09 +00:00
|
|
|
r_list_append (bin->binfiles, bin->cur);
|
2013-11-09 23:15:30 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_bin_init_items(RBin *bin, int dummy) {
|
2013-12-09 03:56:13 +00:00
|
|
|
int minlen = bin->minstrlen;
|
2011-12-13 13:00:22 +00:00
|
|
|
RListIter *it;
|
2012-08-04 21:48:06 +00:00
|
|
|
RBinPlugin *plugin, *cp;
|
2014-01-18 15:26:09 +00:00
|
|
|
RBinFile *a = bin->cur = R_NEW0 (RBinFile);
|
|
|
|
RBinObject *o = a->o = R_NEW0 (RBinObject);
|
|
|
|
|
|
|
|
bin->cur->file = strdup (bin->file);
|
|
|
|
a->buf = r_buf_mmap (bin->cur->file, 0);
|
|
|
|
a->size = a->buf ? a->buf->length: 0;
|
2011-10-05 00:38:37 +00:00
|
|
|
a->curplugin = NULL;
|
2013-07-03 09:57:37 +00:00
|
|
|
|
2012-08-04 21:48:06 +00:00
|
|
|
r_list_foreach (bin->plugins, it, plugin) {
|
2011-12-13 13:00:22 +00:00
|
|
|
if ((dummy && !strncmp (plugin->name, "any", 5)) ||
|
2013-11-09 23:15:30 +00:00
|
|
|
(!dummy && (plugin->check && plugin->check (a)))) {
|
|
|
|
a->curplugin = plugin;
|
2010-10-04 01:46:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-03 09:57:37 +00:00
|
|
|
cp = a->curplugin;
|
2013-12-03 04:46:19 +00:00
|
|
|
if (minlen<=0) {
|
2014-01-11 06:56:02 +00:00
|
|
|
if (cp && cp->minstrlen)
|
2013-12-16 04:04:34 +00:00
|
|
|
bin->minstrlen = cp->minstrlen;
|
|
|
|
else bin->minstrlen = -minlen;
|
2012-12-09 00:39:27 +00:00
|
|
|
}
|
|
|
|
if (!cp || !cp->load || !cp->load (a)) {
|
2013-07-03 09:57:37 +00:00
|
|
|
// already freed in format/pe/pe.c:r_bin_pe_free()
|
|
|
|
// r_buf_free (a->buf);
|
2014-01-18 15:26:09 +00:00
|
|
|
//a->buf = r_buf_mmap (bin->cur->file, 0);
|
|
|
|
//a->size = a->buf? a->buf->length: 0;
|
2012-12-09 00:39:27 +00:00
|
|
|
o->strings = get_strings (a, minlen);
|
2010-10-04 01:46:58 +00:00
|
|
|
return R_FALSE;
|
2012-12-09 00:39:27 +00:00
|
|
|
}
|
2013-12-20 00:20:17 +00:00
|
|
|
set_bin_items (bin, cp);
|
2010-10-04 01:46:58 +00:00
|
|
|
return R_TRUE;
|
2010-02-07 12:17:51 +00:00
|
|
|
}
|
|
|
|
|
2014-01-18 15:26:09 +00:00
|
|
|
static int remove_bin_file_by_binfile (RBin *bin, RBinFile * binfile) {
|
|
|
|
RListIter *iter;
|
|
|
|
RBinFile *tmp_binfile = NULL;
|
|
|
|
int found_bin = R_FALSE;
|
|
|
|
r_list_foreach (bin->binfiles, iter, tmp_binfile) {
|
|
|
|
if (tmp_binfile == binfile) {
|
|
|
|
r_list_delete (bin->binfiles, iter);
|
|
|
|
found_bin = R_TRUE;
|
2014-01-22 22:58:23 +00:00
|
|
|
break;
|
2014-01-18 15:26:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return found_bin;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void r_bin_free_bin_files (RBin *bin) {
|
|
|
|
RListIter *iter, *t_iter;
|
|
|
|
RBinFile *a;
|
|
|
|
r_list_foreach_safe (bin->binfiles, iter, t_iter, a) {
|
|
|
|
r_bin_file_free (a);
|
|
|
|
r_list_delete(bin->binfiles,iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void r_bin_file_free (RBinFile *a) {
|
|
|
|
int i;
|
|
|
|
RBinObject *o = a->o;
|
|
|
|
RBINLISTFREE (o->entries);
|
|
|
|
RBINLISTFREE (o->fields);
|
|
|
|
RBINLISTFREE (o->imports);
|
|
|
|
RBINLISTFREE (o->libs);
|
|
|
|
RBINLISTFREE (o->relocs);
|
|
|
|
RBINLISTFREE (o->sections);
|
|
|
|
RBINLISTFREE (o->strings);
|
|
|
|
RBINLISTFREE (o->symbols);
|
|
|
|
RBINLISTFREE (o->classes);
|
|
|
|
free (o->info);
|
|
|
|
o->info = NULL;
|
|
|
|
|
2014-01-22 22:58:23 +00:00
|
|
|
if (o->binsym != NULL)
|
2014-01-18 15:26:09 +00:00
|
|
|
for (i=0; i<R_BIN_SYM_LAST; i++){
|
|
|
|
free (o->binsym[i]);
|
|
|
|
o->binsym[i] = NULL;
|
|
|
|
}
|
|
|
|
if (a->curplugin && a->curplugin->destroy)
|
|
|
|
a->curplugin->destroy (a);
|
|
|
|
else if (a->buf)
|
|
|
|
r_buf_free (a->buf);
|
|
|
|
|
|
|
|
free (a->file);
|
2014-01-12 01:21:38 +00:00
|
|
|
memset (a, 0, sizeof (RBinFile));
|
|
|
|
//free (a);
|
2014-01-18 15:26:09 +00:00
|
|
|
|
|
|
|
//bin->cur = r_list_get_n (bin->binfiles, 0);
|
|
|
|
//if (bin->cur) r_bin_bind (bin, bin->cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX - This is called on everytime a new bin created
|
|
|
|
|
2010-10-04 01:46:58 +00:00
|
|
|
static void r_bin_free_items(RBin *bin) {
|
2010-11-18 10:41:17 +00:00
|
|
|
int i;
|
2014-01-18 15:26:09 +00:00
|
|
|
RBinFile *a = bin->cur;
|
2012-08-04 21:48:06 +00:00
|
|
|
RBinObject *o = a->o;
|
2014-01-18 15:26:09 +00:00
|
|
|
remove_bin_file_by_binfile (bin, a);
|
2012-08-04 21:48:06 +00:00
|
|
|
RBINLISTFREE (o->entries);
|
|
|
|
RBINLISTFREE (o->fields);
|
|
|
|
RBINLISTFREE (o->imports);
|
|
|
|
RBINLISTFREE (o->libs);
|
|
|
|
RBINLISTFREE (o->relocs);
|
|
|
|
RBINLISTFREE (o->sections);
|
|
|
|
RBINLISTFREE (o->strings);
|
|
|
|
RBINLISTFREE (o->symbols);
|
|
|
|
RBINLISTFREE (o->classes);
|
|
|
|
free (o->info);
|
|
|
|
o->info = NULL;
|
2014-01-18 15:26:09 +00:00
|
|
|
|
2012-08-04 21:48:06 +00:00
|
|
|
if (o->binsym)
|
2013-11-09 23:15:30 +00:00
|
|
|
for (i=0; i<R_BIN_SYM_LAST; i++){
|
2012-08-04 21:48:06 +00:00
|
|
|
free (o->binsym[i]);
|
2013-11-09 23:15:30 +00:00
|
|
|
o->binsym[i] = NULL;
|
|
|
|
}
|
2011-10-05 00:38:37 +00:00
|
|
|
if (a->curplugin && a->curplugin->destroy)
|
|
|
|
a->curplugin->destroy (a);
|
2014-01-29 01:42:22 +00:00
|
|
|
else free (a->buf);
|
2014-01-18 15:26:09 +00:00
|
|
|
|
|
|
|
free (a->file);
|
|
|
|
memset (a, 0, sizeof(RBinFile));
|
|
|
|
free (a);
|
2010-09-24 19:23:13 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 06:56:02 +00:00
|
|
|
static void r_bin_init(RBin *bin, int rawstr, ut64 baseaddr, ut64 loadaddr) {
|
2011-12-13 13:00:22 +00:00
|
|
|
RListIter *it;
|
|
|
|
RBinXtrPlugin *xtr;
|
2010-10-04 01:46:58 +00:00
|
|
|
|
2014-01-18 15:26:09 +00:00
|
|
|
if (bin->cur && bin->cur->o) {
|
|
|
|
if (!bin->cur->o->referenced)
|
2012-09-18 01:39:32 +00:00
|
|
|
r_bin_free_items (bin);
|
2012-08-04 21:48:06 +00:00
|
|
|
}
|
2014-01-11 06:56:02 +00:00
|
|
|
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur = R_NEW0 (RBinFile);
|
|
|
|
bin->cur->file = strdup (bin->file);
|
|
|
|
bin->cur->o = R_NEW0 (RBinObject);
|
|
|
|
bin->cur->o->loadaddr = loadaddr;
|
|
|
|
bin->cur->o->baddr = baseaddr;
|
2014-01-11 06:56:02 +00:00
|
|
|
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->curxtr = NULL;
|
2012-01-26 02:18:45 +00:00
|
|
|
r_list_foreach (bin->binxtrs, it, xtr) {
|
2011-12-13 13:00:22 +00:00
|
|
|
if (xtr->check && xtr->check (bin)) {
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->curxtr = xtr;
|
2010-10-04 01:46:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-09-24 19:23:13 +00:00
|
|
|
}
|
2014-01-18 15:26:09 +00:00
|
|
|
if (bin->cur->curxtr && bin->cur->curxtr->load)
|
|
|
|
bin->cur->curxtr->load (bin);
|
|
|
|
bin->cur->rawstr = rawstr;
|
2010-10-04 01:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int r_bin_extract(RBin *bin, int idx) {
|
2014-02-22 01:48:13 +00:00
|
|
|
if (!bin || !bin->cur)
|
|
|
|
return R_FALSE;
|
2014-01-18 15:26:09 +00:00
|
|
|
if (bin->cur->curxtr && bin->cur->curxtr->extract)
|
|
|
|
return bin->cur->curxtr->extract (bin, idx);
|
2012-09-18 01:39:32 +00:00
|
|
|
if (!bin->file)
|
|
|
|
return R_FALSE;
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->file = strdup (bin->file);
|
|
|
|
bin->cur->buf = r_buf_mmap (bin->file, 0);
|
2012-09-18 01:39:32 +00:00
|
|
|
return R_TRUE;
|
2010-02-07 12:17:51 +00:00
|
|
|
}
|
|
|
|
|
2010-05-25 23:42:22 +00:00
|
|
|
R_API int r_bin_add(RBin *bin, RBinPlugin *foo) {
|
2011-12-13 13:00:22 +00:00
|
|
|
RListIter *it;
|
|
|
|
RBinPlugin *plugin;
|
2009-03-08 15:49:15 +00:00
|
|
|
if (foo->init)
|
2010-02-07 12:17:51 +00:00
|
|
|
foo->init (bin->user);
|
2013-12-10 01:20:51 +00:00
|
|
|
r_list_foreach (bin->plugins, it, plugin) {
|
2011-12-13 13:00:22 +00:00
|
|
|
if (!strcmp (plugin->name, foo->name))
|
2009-03-08 23:49:15 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
2013-12-10 02:35:59 +00:00
|
|
|
plugin = R_NEW0 (RBinPlugin);
|
|
|
|
memcpy (plugin, foo, sizeof (RBinPlugin));
|
2013-12-10 01:20:51 +00:00
|
|
|
r_list_append (bin->plugins, plugin);
|
2009-03-08 15:49:15 +00:00
|
|
|
return R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
R_API int r_bin_xtr_add(RBin *bin, RBinXtrPlugin *foo) {
|
2011-12-13 13:00:22 +00:00
|
|
|
RListIter *it;
|
|
|
|
RBinXtrPlugin *xtr;
|
2010-10-04 01:46:58 +00:00
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
if (foo->init)
|
|
|
|
foo->init (bin->user);
|
2011-12-13 13:00:22 +00:00
|
|
|
|
|
|
|
// avoid duplicates
|
2012-08-07 08:28:03 +00:00
|
|
|
r_list_foreach (bin->binxtrs, it, xtr) {
|
2011-12-13 13:00:22 +00:00
|
|
|
if (!strcmp (xtr->name, foo->name))
|
2010-09-24 19:23:13 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
2012-08-07 08:28:03 +00:00
|
|
|
r_list_append (bin->binxtrs, foo);
|
2011-12-13 13:00:22 +00:00
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
return R_TRUE;
|
2010-07-29 14:04:18 +00:00
|
|
|
}
|
|
|
|
|
2010-02-07 12:17:51 +00:00
|
|
|
R_API void* r_bin_free(RBin *bin) {
|
2011-02-23 19:53:56 +00:00
|
|
|
if (!bin) return NULL;
|
2014-01-26 07:29:17 +00:00
|
|
|
bin->file = NULL;
|
|
|
|
//r_bin_free_bin_files (bin);
|
|
|
|
r_list_free (bin->binfiles);
|
2014-01-20 02:29:50 +00:00
|
|
|
if (bin->cur && bin->cur->curxtr && bin->cur->curxtr->destroy)
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->curxtr->destroy (bin);
|
2012-07-01 22:38:02 +00:00
|
|
|
r_list_free (bin->binxtrs);
|
|
|
|
r_list_free (bin->plugins);
|
2010-03-04 00:46:25 +00:00
|
|
|
free (bin);
|
2010-01-08 12:25:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-07 12:17:51 +00:00
|
|
|
R_API int r_bin_list(RBin *bin) {
|
2011-12-13 13:00:22 +00:00
|
|
|
RListIter *it;
|
2013-12-10 03:19:04 +00:00
|
|
|
RBinXtrPlugin *bp;
|
|
|
|
RBinXtrPlugin *bx;
|
|
|
|
r_list_foreach (bin->plugins, it, bp) {
|
|
|
|
printf ("bin %-11s %s (%s)\n",
|
|
|
|
bp->name, bp->desc, bp->license);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2013-12-10 03:19:04 +00:00
|
|
|
r_list_foreach (bin->binxtrs, it, bx) {
|
|
|
|
printf ("xtr %-11s %s (%s)\n", bx->name,
|
|
|
|
bx->desc, bx->license);
|
2010-09-24 19:23:13 +00:00
|
|
|
}
|
2010-01-24 11:40:48 +00:00
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 06:56:02 +00:00
|
|
|
R_API int r_bin_load(RBin *bin, const char *file, ut64 baseaddr, ut64 loadaddr, int dummy) {
|
2014-01-18 15:26:09 +00:00
|
|
|
int res = 0;
|
|
|
|
|
2010-01-24 14:35:28 +00:00
|
|
|
if (!bin || !file)
|
2010-02-07 12:17:51 +00:00
|
|
|
return R_FALSE;
|
2014-01-18 15:26:09 +00:00
|
|
|
|
2010-10-04 01:46:58 +00:00
|
|
|
bin->file = r_file_abspath (file);
|
2014-01-18 15:26:09 +00:00
|
|
|
//if (!bin->cur) bin->cur = R_NEW0 (RBinFile);
|
|
|
|
|
|
|
|
r_bin_init (bin, bin->cur->rawstr, baseaddr, loadaddr);
|
2014-01-11 06:56:02 +00:00
|
|
|
|
2010-10-04 01:46:58 +00:00
|
|
|
bin->narch = r_bin_extract (bin, 0);
|
2010-09-24 19:23:13 +00:00
|
|
|
if (bin->narch == 0)
|
|
|
|
return R_FALSE;
|
2012-02-04 22:25:10 +00:00
|
|
|
/* FIXME: temporary hack to fix malloc:// */
|
2014-01-18 15:26:09 +00:00
|
|
|
if (bin->cur->buf == NULL)
|
2012-02-04 22:25:10 +00:00
|
|
|
return R_FALSE;
|
2014-01-18 15:26:09 +00:00
|
|
|
res = r_bin_init_items (bin, dummy);
|
|
|
|
|
|
|
|
bin->cur->o->referenced += 1;
|
|
|
|
r_list_append (bin->binfiles, bin->cur);
|
|
|
|
return res;
|
2009-03-08 15:49:15 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-02-07 12:17:51 +00:00
|
|
|
R_API ut64 r_bin_get_baddr(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->baddr;
|
2009-03-08 15:49:15 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2014-02-03 05:50:06 +00:00
|
|
|
R_API void r_bin_set_baddr(RBin *bin, ut64 baddr) {
|
|
|
|
if (bin && bin->cur && bin->cur->o) {
|
|
|
|
bin->cur->o->baddr = baddr;
|
|
|
|
// XXX - update all the infos?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 18:43:44 +00:00
|
|
|
R_API ut64 r_bin_get_boffset(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->boffset;
|
2013-12-19 18:43:44 +00:00
|
|
|
}
|
|
|
|
|
2010-11-18 10:41:17 +00:00
|
|
|
R_API RBinAddr* r_bin_get_sym(RBin *bin, int sym) {
|
|
|
|
if (sym<0 || sym>=R_BIN_SYM_LAST)
|
|
|
|
return NULL;
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->binsym[sym];
|
2010-05-30 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2012-08-04 21:48:06 +00:00
|
|
|
// XXX: those accessors are redundant
|
2010-03-10 10:01:38 +00:00
|
|
|
R_API RList* r_bin_get_entries(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->entries;
|
2009-03-08 15:49:15 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-03-10 10:01:38 +00:00
|
|
|
R_API RList* r_bin_get_fields(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->fields;
|
2009-03-08 15:49:15 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-03-10 10:01:38 +00:00
|
|
|
R_API RList* r_bin_get_imports(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->imports;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-02-07 12:17:51 +00:00
|
|
|
R_API RBinInfo* r_bin_get_info(RBin *bin) {
|
2014-02-14 10:51:08 +00:00
|
|
|
if (!bin || !bin->cur || !bin->cur->buf)
|
|
|
|
return NULL;
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->info;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-03-10 10:01:38 +00:00
|
|
|
R_API RList* r_bin_get_libs(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->libs;
|
2009-04-24 10:12:15 +00:00
|
|
|
}
|
|
|
|
|
2010-09-10 09:11:38 +00:00
|
|
|
R_API RList* r_bin_get_relocs(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->relocs;
|
2010-09-10 09:11:38 +00:00
|
|
|
}
|
|
|
|
|
2010-03-10 10:01:38 +00:00
|
|
|
R_API RList* r_bin_get_sections(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->sections;
|
2010-01-24 11:40:48 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-02-23 17:26:02 +00:00
|
|
|
R_API RBinSection* r_bin_get_section_at(RBin *bin, ut64 off, int va) {
|
2014-01-18 15:26:09 +00:00
|
|
|
RBinObject *o = bin->cur->o;
|
2010-02-23 17:26:02 +00:00
|
|
|
RBinSection *section;
|
2010-03-10 10:01:38 +00:00
|
|
|
RListIter *iter;
|
2010-02-23 17:26:02 +00:00
|
|
|
ut64 from, to;
|
|
|
|
|
2012-08-04 21:48:06 +00:00
|
|
|
if (o->sections)
|
|
|
|
r_list_foreach (o->sections, iter, section) {
|
|
|
|
from = va ? o->baddr+section->rva : section->offset;
|
|
|
|
to = va ? o->baddr+section->rva+section->vsize :
|
2010-02-23 17:26:02 +00:00
|
|
|
section->offset + section->size;
|
|
|
|
if (off >= from && off < to)
|
|
|
|
return section;
|
|
|
|
}
|
|
|
|
return NULL;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2013-12-06 05:51:17 +00:00
|
|
|
R_API RList* r_bin_reset_strings(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
RBinFile *a = bin->cur;
|
2013-12-06 05:51:17 +00:00
|
|
|
RBinObject *o = a->o;
|
|
|
|
if (o->strings) {
|
2013-12-29 02:42:18 +00:00
|
|
|
r_list_destroy (o->strings);
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->o->strings = NULL;
|
2013-12-06 05:51:17 +00:00
|
|
|
}
|
2014-01-11 06:56:02 +00:00
|
|
|
|
2013-12-06 05:51:17 +00:00
|
|
|
if (bin->minstrlen <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
2013-12-29 02:42:18 +00:00
|
|
|
if (a->curplugin && a->curplugin->strings)
|
|
|
|
o->strings = a->curplugin->strings (a);
|
2013-12-06 05:51:17 +00:00
|
|
|
else o->strings = get_strings (a, bin->minstrlen);
|
|
|
|
return o->strings;
|
|
|
|
}
|
|
|
|
|
2010-03-10 10:01:38 +00:00
|
|
|
R_API RList* r_bin_get_strings(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->strings;
|
2010-02-07 12:17:51 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-03-10 10:01:38 +00:00
|
|
|
R_API RList* r_bin_get_symbols(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->symbols;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-03-13 19:25:03 +00:00
|
|
|
R_API int r_bin_is_big_endian (RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->info->big_endian;
|
2010-03-13 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_bin_is_stripped (RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return R_BIN_DBG_STRIPPED (bin->cur->o->info->dbg_info);
|
2010-03-13 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_bin_is_static (RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
if (r_list_length (bin->cur->o->libs)>0)
|
2013-06-07 08:26:37 +00:00
|
|
|
return R_FALSE;
|
2014-01-18 15:26:09 +00:00
|
|
|
return R_BIN_DBG_STATIC (bin->cur->o->info->dbg_info);
|
2010-03-13 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
2012-07-16 09:39:43 +00:00
|
|
|
// TODO: Integrate with r_bin_dbg */
|
2010-03-13 19:25:03 +00:00
|
|
|
R_API int r_bin_has_dbg_linenums (RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return R_BIN_DBG_LINENUMS (bin->cur->o->info->dbg_info);
|
2010-03-13 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_bin_has_dbg_syms (RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return R_BIN_DBG_SYMS (bin->cur->o->info->dbg_info);
|
2010-03-13 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_bin_has_dbg_relocs (RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return R_BIN_DBG_RELOCS (bin->cur->o->info->dbg_info);
|
2010-03-13 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
2010-02-07 12:17:51 +00:00
|
|
|
R_API RBin* r_bin_new() {
|
2011-02-23 19:53:56 +00:00
|
|
|
int i;
|
2010-09-24 19:23:13 +00:00
|
|
|
RBinXtrPlugin *static_xtr_plugin;
|
2012-08-04 21:48:06 +00:00
|
|
|
RBin *bin = R_NEW0 (RBin);
|
2012-07-16 09:39:43 +00:00
|
|
|
if (!bin) return NULL;
|
|
|
|
bin->plugins = r_list_new();
|
|
|
|
bin->plugins->free = free;
|
2012-12-09 00:39:27 +00:00
|
|
|
bin->minstrlen = -2;
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur = R_NEW0 (RBinFile);
|
|
|
|
bin->cur->o = R_NEW0 (RBinObject);
|
|
|
|
bin->binfiles = r_list_new();
|
2014-02-10 23:08:12 +00:00
|
|
|
bin->binfiles->free = (RListFree)r_bin_file_free;
|
2012-07-16 09:39:43 +00:00
|
|
|
for (i=0; bin_static_plugins[i]; i++) {
|
2013-12-10 01:20:51 +00:00
|
|
|
r_bin_add (bin, bin_static_plugins[i]); //static_plugin);
|
2012-07-16 09:39:43 +00:00
|
|
|
}
|
2012-08-04 21:48:06 +00:00
|
|
|
bin->binxtrs = r_list_new ();
|
2012-07-16 09:39:43 +00:00
|
|
|
bin->binxtrs->free = free;
|
|
|
|
for (i=0; bin_xtr_static_plugins[i]; i++) {
|
|
|
|
static_xtr_plugin = R_NEW (RBinXtrPlugin);
|
|
|
|
memcpy (static_xtr_plugin, bin_xtr_static_plugins[i],
|
|
|
|
sizeof (RBinXtrPlugin));
|
|
|
|
r_bin_xtr_add (bin, static_xtr_plugin);
|
2010-05-20 15:40:58 +00:00
|
|
|
}
|
2010-01-24 11:40:48 +00:00
|
|
|
return bin;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2011-09-04 00:23:53 +00:00
|
|
|
/* arch and bits are implicit in the plugin name, do we really need
|
2014-01-18 15:26:09 +00:00
|
|
|
* to overwrite bin->cur->info? */
|
2011-07-25 19:10:25 +00:00
|
|
|
R_API int r_bin_use_arch(RBin *bin, const char *arch, int bits, const char *name) {
|
2014-01-18 15:26:09 +00:00
|
|
|
RBinObject *o = bin->cur->o;
|
2011-12-13 13:00:22 +00:00
|
|
|
RListIter *it;
|
|
|
|
RBinPlugin *plugin;
|
2011-07-25 19:10:25 +00:00
|
|
|
|
2012-08-04 21:48:06 +00:00
|
|
|
if (!o->info) o->info = R_NEW0 (RBinInfo);
|
|
|
|
strncpy (o->info->arch, arch, R_BIN_SIZEOF_STRINGS);
|
|
|
|
o->info->bits = bits;
|
2011-08-27 01:31:55 +00:00
|
|
|
|
2012-08-04 21:48:06 +00:00
|
|
|
r_list_foreach (bin->plugins, it, plugin) {
|
2011-12-13 13:00:22 +00:00
|
|
|
if (!strcmp (name, plugin->name)) {
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->curplugin = plugin;
|
2011-07-25 19:10:25 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-08-27 01:31:55 +00:00
|
|
|
// DUPDUPDUP
|
2011-07-25 19:10:25 +00:00
|
|
|
R_API int r_bin_select(RBin *bin, const char *arch, int bits, const char *name) {
|
2010-09-24 19:23:13 +00:00
|
|
|
int i;
|
2012-08-06 15:34:01 +00:00
|
|
|
RBinInfo *info;
|
2013-02-24 20:12:30 +00:00
|
|
|
//if (bin->narch >1) // fix double load when no multiarch bin is loaded
|
2011-08-27 01:31:55 +00:00
|
|
|
for (i=0; i<bin->narch; i++) {
|
2011-07-25 19:10:25 +00:00
|
|
|
r_bin_select_idx (bin, i);
|
2014-01-18 15:26:09 +00:00
|
|
|
info = bin->cur->o->info;
|
|
|
|
if (!info || !bin->cur->file ||
|
2012-08-04 21:48:06 +00:00
|
|
|
(arch && !strstr (info->arch, arch)) ||
|
|
|
|
(bits && bits != info->bits) ||
|
|
|
|
(name && !strstr (info->file, name)))
|
2010-10-01 09:58:11 +00:00
|
|
|
continue;
|
2010-10-01 08:09:50 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
2010-09-24 19:23:13 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-07-25 19:10:25 +00:00
|
|
|
R_API int r_bin_select_idx(RBin *bin, int idx) {
|
2014-02-22 01:48:13 +00:00
|
|
|
if (bin && bin->cur) {
|
2014-02-22 01:52:56 +00:00
|
|
|
r_bin_free_items (bin);
|
|
|
|
if (bin->cur->curxtr && bin->cur->curxtr->extract) {
|
2014-02-22 01:48:13 +00:00
|
|
|
if (r_bin_extract (bin, idx))
|
|
|
|
return r_bin_init_items (bin, R_FALSE);
|
|
|
|
}
|
2014-02-22 01:52:56 +00:00
|
|
|
return r_bin_init_items (bin, R_FALSE);
|
2014-02-22 01:48:13 +00:00
|
|
|
}
|
2010-10-04 01:46:58 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_bin_list_archs(RBin *bin) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < bin->narch; i++)
|
2012-08-04 21:48:06 +00:00
|
|
|
if (r_bin_select_idx (bin, i)) {
|
2014-01-18 15:26:09 +00:00
|
|
|
RBinInfo *info = bin->cur->o->info;
|
2014-01-11 06:56:02 +00:00
|
|
|
printf ("%03i 0x%08"PFMT64x" %d %s_%i %s %s\n", i,
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->offset, bin->cur->size, info->arch,
|
|
|
|
info->bits, info->machine, bin->cur->file);
|
2013-01-29 18:39:41 +00:00
|
|
|
} else eprintf ("%03i 0x%08"PFMT64x" %d unknown_0\n", i,
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->offset, bin->cur->size);
|
2010-10-04 01:46:58 +00:00
|
|
|
}
|
|
|
|
|
2010-02-07 12:17:51 +00:00
|
|
|
R_API void r_bin_set_user_ptr(RBin *bin, void *user) {
|
2010-01-24 11:40:48 +00:00
|
|
|
bin->user = user;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-10-04 08:55:43 +00:00
|
|
|
|
2011-06-26 18:29:24 +00:00
|
|
|
static int getoffset (RBin *bin, int type, int idx) {
|
2014-01-18 15:26:09 +00:00
|
|
|
RBinFile *a = bin->cur;
|
2011-06-26 18:29:24 +00:00
|
|
|
if (a && a->curplugin && a->curplugin->get_offset)
|
|
|
|
return a->curplugin->get_offset (a, type, idx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-20 10:31:38 +00:00
|
|
|
static const char *getname (RBin *bin, int off) {
|
|
|
|
// walk symbols, find index, return name, ignore offset wtf
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-26 18:29:24 +00:00
|
|
|
R_API void r_bin_bind (RBin *bin, RBinBind *b) {
|
2014-01-20 02:29:50 +00:00
|
|
|
if (b) {
|
|
|
|
b->bin = bin;
|
|
|
|
b->get_offset = getoffset;
|
|
|
|
b->get_name = getname;
|
|
|
|
}
|
2011-06-26 18:29:24 +00:00
|
|
|
}
|
|
|
|
|
2011-07-25 19:10:25 +00:00
|
|
|
R_API RBuffer *r_bin_create (RBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen) {
|
2014-01-18 15:26:09 +00:00
|
|
|
RBinFile *a = bin->cur;
|
2011-07-25 22:30:45 +00:00
|
|
|
if (codelen<0) codelen = 0;
|
|
|
|
if (datalen<0) datalen = 0;
|
2011-07-25 19:10:25 +00:00
|
|
|
if (a && a->curplugin && a->curplugin->create)
|
|
|
|
return a->curplugin->create (bin, code, codelen, data, datalen);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-03 04:03:48 +00:00
|
|
|
R_API RBinObject *r_bin_get_object(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->o->referenced = R_TRUE;
|
|
|
|
return bin->cur->o;
|
2012-08-04 21:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_bin_object_free(RBinObject *obj) {
|
2010-10-04 08:55:43 +00:00
|
|
|
free (obj);
|
|
|
|
}
|
2011-10-05 00:38:37 +00:00
|
|
|
|
|
|
|
R_API RList* /*<RBinClass>*/r_bin_get_classes(RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->classes;
|
2011-10-05 00:38:37 +00:00
|
|
|
}
|
2011-11-21 23:59:20 +00:00
|
|
|
|
2012-11-07 09:41:12 +00:00
|
|
|
R_API RBinClass *r_bin_class_new (RBin *bin, const char *name, const char *super, int view) {
|
2014-01-18 15:26:09 +00:00
|
|
|
RList *list = bin->cur->o->classes;
|
2012-11-07 09:41:12 +00:00
|
|
|
RBinClass *c;
|
|
|
|
if (!name) return NULL;
|
|
|
|
c = r_bin_class_get (bin, name);
|
|
|
|
if (c) {
|
|
|
|
if (super) {
|
|
|
|
free (c->super);
|
|
|
|
c->super = strdup (super);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
c = R_NEW0 (RBinClass);
|
|
|
|
if (!c) return NULL;
|
|
|
|
c->name = strdup (name);
|
|
|
|
c->super = super? strdup (super): NULL;
|
|
|
|
c->index = r_list_length (list);
|
|
|
|
c->methods = r_list_new ();
|
|
|
|
c->fields = r_list_new ();
|
|
|
|
c->visibility = view;
|
|
|
|
if (!list)
|
2014-01-18 15:26:09 +00:00
|
|
|
list = bin->cur->o->classes = r_list_new ();
|
2012-11-07 09:41:12 +00:00
|
|
|
r_list_append (list, c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API RBinClass *r_bin_class_get (RBin *bin, const char *name) {
|
2014-01-18 15:26:09 +00:00
|
|
|
RList *list = bin->cur->o->classes;
|
2012-11-07 09:41:12 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RBinClass *c;
|
|
|
|
r_list_foreach (list, iter, c) {
|
|
|
|
if (!strcmp (c->name, name))
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_bin_class_add_method (RBin *bin, const char *classname, const char *name, int nargs) {
|
|
|
|
RBinClass *c = r_bin_class_get (bin, classname);
|
|
|
|
name = strdup (name); // XXX
|
|
|
|
if (c) {
|
|
|
|
r_list_append (c->methods, (void*)name);
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
2013-11-24 23:45:18 +00:00
|
|
|
c = r_bin_class_new (bin, classname, NULL, 0);
|
|
|
|
r_list_append (c->methods, (void*)name);
|
2012-11-07 09:41:12 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_bin_class_add_field (RBin *bin, const char *classname, const char *name) {
|
2013-02-15 12:24:09 +00:00
|
|
|
#warning TODO: add_field into class
|
|
|
|
//eprintf ("TODO add field: %s \n", name);
|
2012-11-07 09:41:12 +00:00
|
|
|
}
|
|
|
|
|
2011-11-21 23:59:20 +00:00
|
|
|
R_API ut64 r_bin_get_offset (RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->offset;
|
2011-11-21 23:59:20 +00:00
|
|
|
}
|
2012-08-23 10:46:55 +00:00
|
|
|
|
2013-11-20 08:45:17 +00:00
|
|
|
R_API ut64 r_bin_get_vaddr (RBin *bin, ut64 baddr, ut64 paddr, ut64 vaddr) {
|
2014-01-18 15:26:09 +00:00
|
|
|
RBinPlugin *cp = bin->cur->curplugin;
|
2013-11-24 23:45:18 +00:00
|
|
|
if (cp && cp->get_vaddr)
|
2014-01-18 15:26:09 +00:00
|
|
|
return cp->get_vaddr (bin->cur, baddr, paddr, vaddr);
|
2013-11-20 08:45:17 +00:00
|
|
|
|
|
|
|
ut32 delta;
|
|
|
|
if (!baddr) return vaddr;
|
|
|
|
delta = (paddr & 0xfffff000) | (vaddr & 0xfff);
|
|
|
|
return baddr + delta;
|
|
|
|
}
|
|
|
|
|
2012-08-23 10:46:55 +00:00
|
|
|
R_API ut64 r_bin_get_size (RBin *bin) {
|
2014-01-18 15:26:09 +00:00
|
|
|
return bin->cur->o->size;
|
2012-08-23 10:46:55 +00:00
|
|
|
}
|
2014-01-18 15:26:09 +00:00
|
|
|
|