2009-02-05 21:08:46 +00:00
|
|
|
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
|
|
|
|
|
|
|
|
/* TODO:
|
|
|
|
* Linked libraries
|
|
|
|
* dlopen library and show address
|
|
|
|
* Strings
|
|
|
|
* XRefs
|
|
|
|
* Generic resize
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_lib.h>
|
|
|
|
#include <r_bin.h>
|
2009-03-09 12:03:42 +00:00
|
|
|
#include "../config.h"
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-03-09 12:03:42 +00:00
|
|
|
static struct r_bin_handle_t *bin_static_plugins[] =
|
|
|
|
{ R_BIN_STATIC_PLUGINS };
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-03-16 23:34:45 +00:00
|
|
|
static struct r_bin_string_t *get_strings(struct r_bin_t *bin, int min)
|
2009-03-16 20:07:31 +00:00
|
|
|
{
|
2009-03-16 23:34:45 +00:00
|
|
|
struct r_bin_string_t *ret = NULL;
|
|
|
|
u8 *buf = NULL;
|
|
|
|
u64 len, max_str = 0;
|
|
|
|
int i, matches = 0, ctr = 0;
|
|
|
|
char str[R_BIN_SIZEOF_NAMES];
|
|
|
|
|
|
|
|
len = lseek(bin->fd, 0, SEEK_END);
|
|
|
|
max_str = (u64)(len/min);
|
|
|
|
|
|
|
|
ret = malloc(max_str*sizeof(struct r_bin_string_t));
|
|
|
|
|
|
|
|
buf = malloc(len);
|
|
|
|
if (buf == NULL) {
|
|
|
|
fprintf(stderr, "Error allocating file\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
lseek(bin->fd, 0, SEEK_SET);
|
|
|
|
read(bin->fd, buf, len);
|
|
|
|
|
|
|
|
for(i = 0; i < len && ctr < max_str; i++) {
|
|
|
|
if ((IS_PRINTABLE(buf[i]))) {
|
|
|
|
str[matches] = buf[i];
|
|
|
|
if (matches < sizeof(str))
|
|
|
|
matches++;
|
|
|
|
} else {
|
|
|
|
/* check if the length fits on our request */
|
|
|
|
if (matches >= min) {
|
|
|
|
str[matches] = '\0';
|
|
|
|
ret[ctr].offset = i-matches;
|
|
|
|
ret[ctr].size = matches;
|
|
|
|
memcpy(ret[ctr].string, str, R_BIN_SIZEOF_NAMES);
|
|
|
|
ret[ctr].string[R_BIN_SIZEOF_NAMES-1] = '\0';
|
|
|
|
ret[ctr].last = 0;
|
|
|
|
ctr++;
|
|
|
|
}
|
|
|
|
matches = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret[ctr].last = 1;
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return ret;
|
2009-03-16 20:07:31 +00:00
|
|
|
}
|
|
|
|
|
2009-03-25 14:52:17 +00:00
|
|
|
struct r_bin_t *r_bin_new()
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-03-08 15:49:15 +00:00
|
|
|
struct r_bin_t *bin = MALLOC_STRUCT(struct r_bin_t);
|
2009-03-25 14:52:17 +00:00
|
|
|
r_bin_init(bin);
|
2009-02-05 21:08:46 +00:00
|
|
|
return bin;
|
|
|
|
}
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
void r_bin_free(struct r_bin_t *bin)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
|
|
|
free(bin);
|
|
|
|
}
|
|
|
|
|
2009-03-25 14:52:17 +00:00
|
|
|
int r_bin_init(struct r_bin_t *bin)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-03-09 12:03:42 +00:00
|
|
|
int i;
|
2009-03-10 11:21:46 +00:00
|
|
|
bin->cur = NULL;
|
2009-03-08 15:49:15 +00:00
|
|
|
bin->user = NULL;
|
2009-03-25 14:52:17 +00:00
|
|
|
bin->file = NULL;
|
|
|
|
bin->rw = 0;
|
2009-03-08 15:49:15 +00:00
|
|
|
INIT_LIST_HEAD(&bin->bins);
|
2009-03-09 12:03:42 +00:00
|
|
|
for(i=0;bin_static_plugins[i];i++)
|
|
|
|
r_bin_add(bin, bin_static_plugins[i]);
|
2009-03-08 15:49:15 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
void r_bin_set_user_ptr(struct r_bin_t *bin, void *user)
|
|
|
|
{
|
|
|
|
bin->user = user;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
int r_bin_add(struct r_bin_t *bin, struct r_bin_handle_t *foo)
|
|
|
|
{
|
2009-03-09 01:14:50 +00:00
|
|
|
struct list_head *pos;
|
2009-03-08 15:49:15 +00:00
|
|
|
if (foo->init)
|
|
|
|
foo->init(bin->user);
|
2009-03-08 23:49:15 +00:00
|
|
|
/* avoid dupped plugins */
|
2009-03-09 01:14:50 +00:00
|
|
|
list_for_each_prev(pos, &bin->bins) {
|
|
|
|
struct r_bin_handle_t *h = list_entry(pos, struct r_bin_handle_t, list);
|
2009-03-08 23:49:15 +00:00
|
|
|
if (!strcmp(h->name, foo->name))
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
2009-03-08 15:49:15 +00:00
|
|
|
list_add_tail(&(foo->list), &(bin->bins));
|
|
|
|
return R_TRUE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
int r_bin_list(struct r_bin_t *bin)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-03-08 15:49:15 +00:00
|
|
|
struct list_head *pos;
|
|
|
|
list_for_each_prev(pos, &bin->bins) {
|
|
|
|
struct r_bin_handle_t *h = list_entry(pos, struct r_bin_handle_t, list);
|
|
|
|
printf(" %s: %s\n", h->name, h->desc);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2009-03-08 15:49:15 +00:00
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2009-03-25 14:52:17 +00:00
|
|
|
int r_bin_open(struct r_bin_t *bin, const char *file, int rw, char *plugin_name)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-03-25 14:52:17 +00:00
|
|
|
if (file != NULL)
|
|
|
|
bin->file = strdup(file);
|
|
|
|
else return R_FALSE;
|
|
|
|
bin->rw = rw;
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
struct list_head *pos;
|
|
|
|
list_for_each_prev(pos, &bin->bins) {
|
|
|
|
struct r_bin_handle_t *h = list_entry(pos, struct r_bin_handle_t, list);
|
2009-03-25 19:51:26 +00:00
|
|
|
if ((plugin_name && !strcmp(h->name, plugin_name)) ||
|
|
|
|
(h->check && h->check(bin)))
|
2009-03-08 15:49:15 +00:00
|
|
|
bin->cur = h;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
if (bin->cur && bin->cur->open)
|
|
|
|
return bin->cur->open(bin);
|
|
|
|
|
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
int r_bin_close(struct r_bin_t *bin)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-03-08 15:49:15 +00:00
|
|
|
if (bin->cur && bin->cur->close)
|
|
|
|
return bin->cur->close(bin);
|
|
|
|
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
u64 r_bin_get_baddr(struct r_bin_t *bin)
|
|
|
|
{
|
|
|
|
if (bin->cur && bin->cur->baddr)
|
|
|
|
return bin->cur->baddr(bin);
|
|
|
|
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
struct r_bin_entry_t* r_bin_get_entry(struct r_bin_t *bin)
|
|
|
|
{
|
|
|
|
if (bin->cur && bin->cur->entry)
|
|
|
|
return bin->cur->entry(bin);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
struct r_bin_section_t* r_bin_get_sections(struct r_bin_t *bin)
|
|
|
|
{
|
|
|
|
if (bin->cur && bin->cur->sections)
|
|
|
|
return bin->cur->sections(bin);
|
|
|
|
|
2009-02-05 21:08:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
struct r_bin_symbol_t* r_bin_get_symbols(struct r_bin_t *bin)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-03-08 15:49:15 +00:00
|
|
|
if (bin->cur && bin->cur->symbols)
|
|
|
|
return bin->cur->symbols(bin);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
struct r_bin_import_t* r_bin_get_imports(struct r_bin_t *bin)
|
|
|
|
{
|
|
|
|
if (bin->cur && bin->cur->imports)
|
|
|
|
return bin->cur->imports(bin);
|
|
|
|
|
2009-02-05 21:08:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-16 20:07:31 +00:00
|
|
|
struct r_bin_string_t* r_bin_get_strings(struct r_bin_t *bin)
|
|
|
|
{
|
|
|
|
if (bin->cur && bin->cur->strings)
|
|
|
|
return bin->cur->strings(bin);
|
2009-03-16 23:34:45 +00:00
|
|
|
else return get_strings(bin, 5);
|
2009-03-16 20:07:31 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
struct r_bin_info_t* r_bin_get_info(struct r_bin_t *bin)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-03-08 15:49:15 +00:00
|
|
|
if (bin->cur && bin->cur->info)
|
|
|
|
return bin->cur->info(bin);
|
|
|
|
|
2009-02-05 21:08:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
u64 r_bin_resize_section(struct r_bin_t *bin, char *name, u64 size)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-03-08 15:49:15 +00:00
|
|
|
if (bin->cur && bin->cur->resize_section)
|
|
|
|
return bin->cur->resize_section(bin, name, size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 r_bin_get_section_offset(struct r_bin_t *bin, char *name)
|
|
|
|
{
|
|
|
|
struct r_bin_section_t *sections, *sectionsp;
|
2009-02-05 21:08:46 +00:00
|
|
|
u64 ret = -1;
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
if (!(sections = r_bin_get_sections(bin)))
|
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
sectionsp = sections;
|
|
|
|
while (!sectionsp->last) {
|
|
|
|
if (!strcmp(sectionsp->name, name)) {
|
|
|
|
ret = sectionsp->offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sectionsp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(sections);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
u64 r_bin_get_section_rva(struct r_bin_t *bin, char *name)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-03-08 15:49:15 +00:00
|
|
|
struct r_bin_section_t *sections, *sectionsp;
|
2009-02-05 21:08:46 +00:00
|
|
|
u64 ret = -1;
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
if (!(sections = r_bin_get_sections(bin)))
|
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
sectionsp = sections;
|
|
|
|
while (!sectionsp->last) {
|
|
|
|
if (!strcmp(sectionsp->name, name)) {
|
|
|
|
ret = sectionsp->rva;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sectionsp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(sections);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
u64 r_bin_get_section_size(struct r_bin_t *bin, char *name)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-03-08 15:49:15 +00:00
|
|
|
struct r_bin_section_t *sections, *sectionsp;
|
2009-02-05 21:08:46 +00:00
|
|
|
u64 ret = -1;
|
|
|
|
|
2009-03-08 15:49:15 +00:00
|
|
|
if (!(sections = r_bin_get_sections(bin)))
|
|
|
|
return R_FALSE;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
sectionsp = sections;
|
|
|
|
while (!sectionsp->last) {
|
|
|
|
if (!strcmp(sectionsp->name, name)) {
|
|
|
|
ret = sectionsp->size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sectionsp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(sections);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
int r_bin_get_libs()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|