2010-03-08 18:40:21 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2010 nibble<.ds@gmail.com> */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
/* TODO:
|
|
|
|
* dlopen library and show address
|
|
|
|
*/
|
|
|
|
|
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>
|
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
|
|
|
|
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
|
|
|
|
2010-10-17 18:38:19 +00:00
|
|
|
static void get_strings_range(RBinArch *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;
|
2009-03-16 23:34:45 +00:00
|
|
|
|
2010-06-23 15:30:16 +00:00
|
|
|
for(i = from; i < to; i++) {
|
2010-09-24 19:23:13 +00:00
|
|
|
if ((IS_PRINTABLE (arch->buf->buf[i])) && matches < R_BIN_SIZEOF_STRINGS-1) {
|
|
|
|
str[matches] = arch->buf->buf[i];
|
2010-06-20 22:48:06 +00:00
|
|
|
matches++;
|
2009-03-16 23:34:45 +00:00
|
|
|
} else {
|
2010-06-23 15:30:16 +00:00
|
|
|
/* check if the length fits in our request */
|
2009-03-16 23:34:45 +00:00
|
|
|
if (matches >= min) {
|
2010-03-12 12:35:10 +00:00
|
|
|
if (!(ptr = R_NEW (RBinString))) {
|
2010-06-20 22:48:06 +00:00
|
|
|
eprintf ("Error allocating string\n");
|
2010-02-07 12:17:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-03-16 23:34:45 +00:00
|
|
|
str[matches] = '\0';
|
2010-10-17 18:38:19 +00:00
|
|
|
ptr->offset = i-matches;
|
|
|
|
ptr->rva = ptr->offset-from+scnrva;
|
2010-02-07 14:43:42 +00:00
|
|
|
ptr->size = matches;
|
|
|
|
ptr->ordinal = ctr;
|
2010-06-20 22:48:06 +00:00
|
|
|
// copying so many bytes here..
|
2010-02-07 14:43:42 +00:00
|
|
|
memcpy (ptr->string, str, R_BIN_SIZEOF_STRINGS);
|
|
|
|
ptr->string[R_BIN_SIZEOF_STRINGS-1] = '\0';
|
2010-06-23 15:30:16 +00:00
|
|
|
r_list_append (list, ptr);
|
2009-03-16 23:34:45 +00:00
|
|
|
ctr++;
|
|
|
|
}
|
|
|
|
matches = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-06-23 15:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check only in data section. filter chars only in -r mode
|
2010-09-24 19:23:13 +00:00
|
|
|
static RList* get_strings(RBinArch *arch, int min) {
|
2010-06-23 15:30:16 +00:00
|
|
|
RList *ret;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (!(ret = r_list_new ())) {
|
|
|
|
eprintf ("Error allocating array\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret->free = free;
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
if (arch->sections) {
|
2010-06-23 15:30:16 +00:00
|
|
|
RBinSection *section;
|
|
|
|
RListIter *iter;
|
2010-09-24 19:23:13 +00:00
|
|
|
r_list_foreach (arch->sections, iter, section) {
|
2010-09-07 19:38:54 +00:00
|
|
|
// XXX: DIRTY HACK! should we check sections srwx to be READ ONLY and NONEXEC?
|
2010-09-24 19:23:13 +00:00
|
|
|
if ((strstr (arch->info->bclass, "MACH0") && strstr (section->name, "_cstring")) || // OSX
|
2010-10-17 18:38:19 +00:00
|
|
|
(strstr (arch->info->bclass, "ELF") && strstr (section->name, "data")) || // LINUX
|
|
|
|
(strstr (arch->info->bclass, "PE"))) { // WIN
|
2010-06-23 15:30:16 +00:00
|
|
|
count ++;
|
2010-09-24 19:23:13 +00:00
|
|
|
get_strings_range (arch, ret, min,
|
2010-10-17 18:38:19 +00:00
|
|
|
section->offset, section->offset+section->size, section->rva);
|
2010-06-23 15:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count == 0)
|
2010-10-17 18:38:19 +00:00
|
|
|
get_strings_range (arch, ret, min, 0, arch->size, 0);
|
2009-03-16 23:34:45 +00:00
|
|
|
return ret;
|
2009-03-16 20:07:31 +00:00
|
|
|
}
|
|
|
|
|
2010-10-04 01:46:58 +00:00
|
|
|
static int r_bin_init_items(RBin *bin, int dummy) {
|
|
|
|
struct list_head *pos;
|
|
|
|
RBinArch *arch = &bin->curarch;
|
|
|
|
|
|
|
|
arch->curplugin = NULL;
|
|
|
|
list_for_each (pos, &bin->bins) {
|
|
|
|
RBinPlugin *h = list_entry (pos, RBinPlugin, list);
|
|
|
|
if ((dummy && !strncmp (h->name, "dummy", 5)) ||
|
|
|
|
(!dummy && (h->check && h->check (&bin->curarch)))) {
|
|
|
|
bin->curarch.curplugin = h;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!arch->curplugin || !arch->curplugin->load ||
|
|
|
|
!arch->curplugin->load (arch))
|
|
|
|
return R_FALSE;
|
2010-09-24 19:23:13 +00:00
|
|
|
if (arch->curplugin->baddr)
|
|
|
|
arch->baddr = arch->curplugin->baddr (arch);
|
|
|
|
if (arch->curplugin->main)
|
|
|
|
arch->main = arch->curplugin->main (arch);
|
|
|
|
if (arch->curplugin->entries)
|
|
|
|
arch->entries = arch->curplugin->entries (arch);
|
|
|
|
if (arch->curplugin->fields)
|
|
|
|
arch->fields = arch->curplugin->fields (arch);
|
|
|
|
if (arch->curplugin->imports)
|
|
|
|
arch->imports = arch->curplugin->imports (arch);
|
|
|
|
if (arch->curplugin->info)
|
|
|
|
arch->info = arch->curplugin->info (arch);
|
|
|
|
if (arch->curplugin->libs)
|
|
|
|
arch->libs = arch->curplugin->libs (arch);
|
|
|
|
if (arch->curplugin->relocs)
|
|
|
|
arch->relocs = arch->curplugin->relocs (arch);
|
|
|
|
if (arch->curplugin->sections)
|
|
|
|
arch->sections = arch->curplugin->sections (arch);
|
|
|
|
if (arch->curplugin->strings)
|
|
|
|
arch->strings = arch->curplugin->strings (arch);
|
|
|
|
else arch->strings = get_strings (arch, 4);
|
|
|
|
if (arch->curplugin->symbols)
|
|
|
|
arch->symbols = arch->curplugin->symbols (arch);
|
2010-10-04 01:46:58 +00:00
|
|
|
return R_TRUE;
|
2010-02-07 12:17:51 +00:00
|
|
|
}
|
|
|
|
|
2010-05-28 00:44:51 +00:00
|
|
|
/* TODO: Free plugins */
|
2010-10-04 01:46:58 +00:00
|
|
|
static void r_bin_free_items(RBin *bin) {
|
|
|
|
RBinArch *arch = &bin->curarch;
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
if (arch->entries)
|
|
|
|
r_list_free (arch->entries);
|
|
|
|
if (arch->fields)
|
|
|
|
r_list_free (arch->fields);
|
|
|
|
if (arch->imports)
|
|
|
|
r_list_free (arch->imports);
|
|
|
|
if (arch->info)
|
|
|
|
free (arch->info);
|
|
|
|
if (arch->libs)
|
|
|
|
r_list_free (arch->libs);
|
|
|
|
if (arch->relocs)
|
|
|
|
r_list_free (arch->relocs);
|
|
|
|
if (arch->sections)
|
|
|
|
r_list_free (arch->sections);
|
|
|
|
if (arch->strings)
|
|
|
|
r_list_free (arch->strings);
|
|
|
|
if (arch->symbols)
|
|
|
|
r_list_free (arch->symbols);
|
|
|
|
if (arch->main)
|
|
|
|
free (arch->main);
|
2010-10-01 02:26:52 +00:00
|
|
|
if (arch->file)
|
|
|
|
free (arch->file);
|
2010-10-04 02:20:19 +00:00
|
|
|
if (arch->curplugin && arch->curplugin->destroy)
|
|
|
|
arch->curplugin->destroy (arch);
|
2010-09-24 19:23:13 +00:00
|
|
|
}
|
|
|
|
|
2010-10-04 01:46:58 +00:00
|
|
|
static void r_bin_init(RBin *bin) {
|
2010-09-24 19:23:13 +00:00
|
|
|
struct list_head *pos;
|
2010-10-04 01:46:58 +00:00
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
bin->curxtr = NULL;
|
|
|
|
list_for_each (pos, &bin->binxtrs) {
|
|
|
|
RBinXtrPlugin *h = list_entry (pos, RBinXtrPlugin, list);
|
2010-10-04 01:46:58 +00:00
|
|
|
if (h->check && h->check (bin)) {
|
2010-09-24 19:23:13 +00:00
|
|
|
bin->curxtr = h;
|
2010-10-04 01:46:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-09-24 19:23:13 +00:00
|
|
|
}
|
2010-10-04 01:46:58 +00:00
|
|
|
if (bin->curxtr && bin->curxtr->load)
|
|
|
|
bin->curxtr->load (bin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int r_bin_extract(RBin *bin, int idx) {
|
|
|
|
ut8 *buf;
|
|
|
|
int n = 1;
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
if (bin->curxtr && bin->curxtr->extract)
|
2010-10-04 01:46:58 +00:00
|
|
|
n = bin->curxtr->extract (bin, idx);
|
2010-09-24 19:23:13 +00:00
|
|
|
else {
|
2010-10-04 01:46:58 +00:00
|
|
|
bin->curarch.file = strdup (bin->file);
|
|
|
|
if (!(buf = (ut8*)r_file_slurp (bin->file, &bin->curarch.size)))
|
2010-09-24 19:23:13 +00:00
|
|
|
return 0;
|
2010-10-04 01:46:58 +00:00
|
|
|
bin->curarch.buf = r_buf_new ();
|
|
|
|
if (!r_buf_set_bytes (bin->curarch.buf, buf, bin->curarch.size)) {
|
2010-09-24 19:23:13 +00:00
|
|
|
free (buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free (buf);
|
|
|
|
}
|
|
|
|
return n;
|
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) {
|
2009-03-09 01:14:50 +00:00
|
|
|
struct list_head *pos;
|
2010-10-04 01:46:58 +00:00
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
if (foo->init)
|
2010-02-07 12:17:51 +00:00
|
|
|
foo->init (bin->user);
|
|
|
|
list_for_each_prev (pos, &bin->bins) {
|
2010-05-25 23:42:22 +00:00
|
|
|
RBinPlugin *h = list_entry (pos, RBinPlugin, list);
|
2010-02-07 12:17:51 +00:00
|
|
|
if (!strcmp (h->name, foo->name))
|
2009-03-08 23:49:15 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
2010-02-07 12:17:51 +00:00
|
|
|
list_add_tail (&(foo->list), &(bin->bins));
|
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) {
|
|
|
|
struct list_head *pos;
|
2010-10-04 01:46:58 +00:00
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
if (foo->init)
|
|
|
|
foo->init (bin->user);
|
|
|
|
list_for_each_prev (pos, &bin->binxtrs) {
|
|
|
|
RBinXtrPlugin *h = list_entry (pos, RBinXtrPlugin, list);
|
|
|
|
if (!strcmp (h->name, foo->name))
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
list_add_tail (&(foo->list), &(bin->binxtrs));
|
|
|
|
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) {
|
2010-01-24 11:40:48 +00:00
|
|
|
if (!bin)
|
2010-01-08 12:25:03 +00:00
|
|
|
return NULL;
|
2010-10-04 01:46:58 +00:00
|
|
|
r_bin_free_items (bin);
|
|
|
|
if (bin->curxtr && bin->curxtr->destroy)
|
|
|
|
bin->curxtr->destroy (bin);
|
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) {
|
2009-09-24 10:29:05 +00:00
|
|
|
struct list_head *pos;
|
2009-07-16 12:17:32 +00:00
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
list_for_each_prev(pos, &bin->bins) {
|
2010-05-25 23:42:22 +00:00
|
|
|
RBinPlugin *h = list_entry (pos, RBinPlugin, list);
|
2010-02-07 12:17:51 +00:00
|
|
|
printf ("bin %-10s %s\n", h->name, h->desc);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-09-24 19:23:13 +00:00
|
|
|
list_for_each_prev(pos, &bin->binxtrs) {
|
|
|
|
RBinXtrPlugin *h = list_entry (pos, RBinXtrPlugin, list);
|
|
|
|
printf ("bin-xtr %-10s %s\n", h->name, h->desc);
|
|
|
|
}
|
2010-01-24 11:40:48 +00:00
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
R_API int r_bin_load(RBin *bin, const char *file, int dummy) {
|
2010-01-24 14:35:28 +00:00
|
|
|
if (!bin || !file)
|
2010-02-07 12:17:51 +00:00
|
|
|
return R_FALSE;
|
2010-10-04 01:46:58 +00:00
|
|
|
bin->file = r_file_abspath (file);
|
|
|
|
r_bin_init (bin);
|
|
|
|
bin->narch = r_bin_extract (bin, 0);
|
2010-09-24 19:23:13 +00:00
|
|
|
if (bin->narch == 0)
|
|
|
|
return R_FALSE;
|
2010-10-04 01:46:58 +00:00
|
|
|
return r_bin_init_items (bin, dummy);
|
2009-03-08 15:49:15 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-05-25 22:59:10 +00:00
|
|
|
// remove this getters.. we have no threads or mutexes to protect here
|
2010-02-07 12:17:51 +00:00
|
|
|
R_API ut64 r_bin_get_baddr(RBin *bin) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.baddr;
|
2009-03-08 15:49:15 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-05-30 04:06:25 +00:00
|
|
|
R_API RBinAddr* r_bin_get_main(RBin *bin) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.main;
|
2010-05-30 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2010-03-10 10:01:38 +00:00
|
|
|
R_API RList* r_bin_get_entries(RBin *bin) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.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) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.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) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.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) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.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) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.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) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.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) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.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) {
|
|
|
|
RBinSection *section;
|
2010-03-10 10:01:38 +00:00
|
|
|
RListIter *iter;
|
2010-02-23 17:26:02 +00:00
|
|
|
ut64 from, to;
|
|
|
|
|
2010-10-04 01:46:58 +00:00
|
|
|
if (bin->curarch.sections)
|
|
|
|
r_list_foreach (bin->curarch.sections, iter, section) {
|
|
|
|
from = va ? bin->curarch.baddr+section->rva : section->offset;
|
|
|
|
to = va ? bin->curarch.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
|
|
|
}
|
|
|
|
|
2010-03-10 10:01:38 +00:00
|
|
|
R_API RList* r_bin_get_strings(RBin *bin) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.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) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.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) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return bin->curarch.info->big_endian;
|
2010-03-13 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_bin_is_stripped (RBin *bin) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return R_BIN_DBG_STRIPPED (bin->curarch.info->dbg_info);
|
2010-03-13 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_bin_is_static (RBin *bin) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return R_BIN_DBG_STATIC (bin->curarch.info->dbg_info);
|
2010-03-13 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX Implement in r_bin_meta and deprecate? */
|
|
|
|
R_API int r_bin_has_dbg_linenums (RBin *bin) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return R_BIN_DBG_LINENUMS (bin->curarch.info->dbg_info);
|
2010-03-13 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_bin_has_dbg_syms (RBin *bin) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return R_BIN_DBG_SYMS (bin->curarch.info->dbg_info);
|
2010-03-13 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_bin_has_dbg_relocs (RBin *bin) {
|
2010-10-04 01:46:58 +00:00
|
|
|
return R_BIN_DBG_RELOCS (bin->curarch.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() {
|
|
|
|
RBin *bin;
|
2010-05-28 00:44:51 +00:00
|
|
|
RBinPlugin *static_plugin;
|
2010-09-24 19:23:13 +00:00
|
|
|
RBinXtrPlugin *static_xtr_plugin;
|
2010-05-20 15:40:58 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
bin = R_NEW (RBin);
|
|
|
|
if (bin) {
|
2010-08-16 12:58:10 +00:00
|
|
|
memset (bin, 0, sizeof (RBin));
|
2010-05-20 15:40:58 +00:00
|
|
|
INIT_LIST_HEAD (&bin->bins);
|
2010-08-16 12:58:10 +00:00
|
|
|
for (i=0; bin_static_plugins[i]; i++) {
|
2010-05-28 00:44:51 +00:00
|
|
|
static_plugin = R_NEW (RBinPlugin);
|
|
|
|
memcpy (static_plugin, bin_static_plugins[i], sizeof (RBinPlugin));
|
|
|
|
r_bin_add (bin, static_plugin);
|
|
|
|
}
|
2010-09-24 19:23:13 +00:00
|
|
|
INIT_LIST_HEAD (&bin->binxtrs);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-10-01 08:09:50 +00:00
|
|
|
R_API int r_bin_set_arch(RBin *bin, const char *arch, int bits, const char *name) {
|
2010-09-24 19:23:13 +00:00
|
|
|
int i;
|
|
|
|
|
2010-10-01 08:09:50 +00:00
|
|
|
for (i = 0; i < bin->narch; i++) {
|
2010-10-04 01:46:58 +00:00
|
|
|
r_bin_set_archidx (bin, i);
|
|
|
|
if (!bin->curarch.info || !bin->curarch.file ||
|
|
|
|
(arch && !strstr (bin->curarch.info->arch, arch)) ||
|
|
|
|
(bits && bits != bin->curarch.info->bits) ||
|
|
|
|
(name && !strstr (bin->curarch.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;
|
|
|
|
}
|
|
|
|
|
2010-10-04 01:46:58 +00:00
|
|
|
R_API int r_bin_set_archidx(RBin *bin, int idx) {
|
|
|
|
r_bin_free_items (bin);
|
|
|
|
if (r_bin_extract (bin, idx))
|
|
|
|
return r_bin_init_items (bin, R_FALSE);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_bin_list_archs(RBin *bin) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < bin->narch; i++)
|
|
|
|
if (r_bin_set_archidx (bin, i) && bin->curarch.info)
|
|
|
|
printf ("%03i %s %s_%i (%s)\n", i, bin->curarch.file,
|
|
|
|
bin->curarch.info->arch, bin->curarch.info->bits,
|
|
|
|
bin->curarch.info->machine);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
R_API RBinObj *r_bin_get_object(RBin *bin, int flags) {
|
|
|
|
RBinObj *obj = R_NEW (RBinObj);
|
|
|
|
if (obj) {
|
|
|
|
obj->symbols = r_bin_get_symbols (bin);
|
|
|
|
obj->imports = r_bin_get_imports (bin);
|
|
|
|
obj->entries = r_bin_get_entries (bin);
|
|
|
|
obj->main = r_bin_get_main (bin);
|
|
|
|
obj->baddr = r_bin_get_baddr (bin);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_bin_object_free(RBinObj *obj) {
|
|
|
|
// XXX: leak
|
|
|
|
free (obj);
|
|
|
|
}
|