* r_bin & r_bininfo

- Added "check" function to plugins
    (used by _open for auto setting)
  - Removed hardcoded format autodetection
This commit is contained in:
Nibble 2009-03-25 20:51:26 +01:00
parent 8e72d7f7d9
commit 0819be62de
11 changed files with 101 additions and 60 deletions

View File

@ -129,39 +129,12 @@ int r_bin_open(struct r_bin_t *bin, const char *file, int rw, char *plugin_name)
else return R_FALSE;
bin->rw = rw;
if (plugin_name == NULL) {
u8 buf[1024];
plugin_name = malloc(32);
if ((bin->fd = open(bin->file, 0)) == -1) {
return -1;
}
lseek(bin->fd, 0, SEEK_SET);
read(bin->fd, buf, 1024);
close(bin->fd);
if (!memcmp(buf, "\x7F\x45\x4c\x46", 4)) {
if (buf[4] == 2) /* buf[EI_CLASS] == ELFCLASS64 */
strcpy(plugin_name, "bin_elf64");
else strcpy(plugin_name, "bin_elf");
} else if (!memcmp(buf, "\x4d\x5a", 2) &&
!memcmp(buf+(buf[0x3c]|(buf[0x3d]<<8)), "\x50\x45", 2)) {
if (!memcmp(buf+(buf[0x3c]|buf[0x3d]<<8)+0x18, "\x0b\x02", 2))
strcpy(plugin_name, "bin_pe64");
else strcpy(plugin_name, "bin_pe");
} else if (!memcmp(buf, "\xca\xfe\xba\xbe", 4))
strcpy(plugin_name, "bin_java");
else return R_FALSE;
}
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);
if (!strcmp(h->name, plugin_name)) {
if ((plugin_name && !strcmp(h->name, plugin_name)) ||
(h->check && h->check(bin)))
bin->cur = h;
}
}
if (bin->cur && bin->cur->open)

View File

@ -178,6 +178,23 @@ static u64 resize_section(struct r_bin_t *bin, char *name, u64 size)
}
#if !R_BIN_ELF64
static int check(struct r_bin_t *bin)
{
u8 buf[1024];
if ((bin->fd = open(bin->file, 0)) == -1)
return R_FALSE;
lseek(bin->fd, 0, SEEK_SET);
read(bin->fd, buf, 1024);
close(bin->fd);
if (!memcmp(buf, "\x7F\x45\x4c\x46", 4) &&
buf[4] == 1) /* buf[EI_CLASS] == ELFCLASS32 */
return R_TRUE;
return R_FALSE;
}
struct r_bin_handle_t r_bin_plugin_elf = {
.name = "bin_elf",
.desc = "elf bin plugin",
@ -185,6 +202,7 @@ struct r_bin_handle_t r_bin_plugin_elf = {
.fini = NULL,
.open = &bopen,
.close = &bclose,
.check = &check,
.baddr = &baddr,
.entry = &entry,
.sections = &sections,

View File

@ -3,6 +3,23 @@
#define R_BIN_ELF64 1
#include "bin_elf.c"
static int check(struct r_bin_t *bin)
{
u8 buf[1024];
if ((bin->fd = open(bin->file, 0)) == -1)
return R_FALSE;
lseek(bin->fd, 0, SEEK_SET);
read(bin->fd, buf, 1024);
close(bin->fd);
if (!memcmp(buf, "\x7F\x45\x4c\x46", 4) &&
buf[4] == 2) /* buf[EI_CLASS] == ELFCLASS64 */
return R_TRUE;
return R_FALSE;
}
struct r_bin_handle_t r_bin_plugin_elf64 = {
.name = "bin_elf64",
.desc = "elf64 bin plugin",
@ -10,6 +27,7 @@ struct r_bin_handle_t r_bin_plugin_elf64 = {
.fini = NULL,
.open = &bopen,
.close = &bclose,
.check = &check,
.baddr = &baddr,
.entry = &entry,
.sections = &sections,

View File

@ -128,6 +128,22 @@ static struct r_bin_info_t* info(struct r_bin_t *bin)
return ret;
}
static int check(struct r_bin_t *bin)
{
u8 buf[1024];
if ((bin->fd = open(bin->file, 0)) == -1)
return R_FALSE;
lseek(bin->fd, 0, SEEK_SET);
read(bin->fd, buf, 1024);
close(bin->fd);
if (!memcmp(buf, "\xca\xfe\xba\xbe", 4))
return R_TRUE;
return R_FALSE;
}
struct r_bin_handle_t r_bin_plugin_java = {
.name = "bin_java",
.desc = "java bin plugin",
@ -135,6 +151,7 @@ struct r_bin_handle_t r_bin_plugin_java = {
.fini = NULL,
.open = &bopen,
.close = &bclose,
.check = &check,
.baddr = &baddr,
.entry = &entry,
.sections = NULL,

View File

@ -187,6 +187,24 @@ static struct r_bin_info_t* info(struct r_bin_t *bin)
}
#if !R_BIN_PE64
static int check(struct r_bin_t *bin)
{
u8 buf[1024];
if ((bin->fd = open(bin->file, 0)) == -1)
return R_FALSE;
lseek(bin->fd, 0, SEEK_SET);
read(bin->fd, buf, 1024);
close(bin->fd);
if (!memcmp(buf, "\x4d\x5a", 2) &&
!memcmp(buf+(buf[0x3c]|(buf[0x3d]<<8)), "\x50\x45", 2) &&
!memcmp(buf+(buf[0x3c]|buf[0x3d]<<8)+0x18, "\x0b\x01", 2))
return R_TRUE;
return R_FALSE;
}
struct r_bin_handle_t r_bin_plugin_pe = {
.name = "bin_pe",
.desc = "pe bin plugin",
@ -194,6 +212,7 @@ struct r_bin_handle_t r_bin_plugin_pe = {
.fini = NULL,
.open = &bopen,
.close = &bclose,
.check = &check,
.baddr = &baddr,
.entry = &entry,
.sections = &sections,

View File

@ -3,6 +3,24 @@
#define R_BIN_PE64 1
#include "bin_pe.c"
static int check(struct r_bin_t *bin)
{
u8 buf[1024];
if ((bin->fd = open(bin->file, 0)) == -1)
return R_FALSE;
lseek(bin->fd, 0, SEEK_SET);
read(bin->fd, buf, 1024);
close(bin->fd);
if (!memcmp(buf, "\x4d\x5a", 2) &&
!memcmp(buf+(buf[0x3c]|(buf[0x3d]<<8)), "\x50\x45", 2) &&
!memcmp(buf+(buf[0x3c]|buf[0x3d]<<8)+0x18, "\x0b\x02", 2))
return R_TRUE;
return R_FALSE;
}
struct r_bin_handle_t r_bin_plugin_pe64 = {
.name = "bin_pe64",
.desc = "pe64 (pe32+) bin plugin",
@ -10,6 +28,7 @@ struct r_bin_handle_t r_bin_plugin_pe64 = {
.fini = NULL,
.open = &bopen,
.close = &bclose,
.check = &check,
.baddr = &baddr,
.entry = &entry,
.sections = &sections,

View File

@ -400,7 +400,8 @@ int main(int argc, char **argv)
return R_FALSE;
}
free (plugin_name);
if (plugin_name != NULL)
free (plugin_name);
if (action&ACTION_ENTRY)
rabin_show_entrypoint();

View File

@ -107,39 +107,12 @@ int r_bininfo_open(struct r_bininfo_t *bin, const char *file, int rw, char *plug
else return R_FALSE;
bin->rw = rw;
if (plugin_name == NULL) {
u8 buf[1024];
plugin_name = malloc(32);
if ((bin->fd = open(bin->file, 0)) == -1) {
return -1;
}
lseek(bin->fd, 0, SEEK_SET);
read(bin->fd, buf, 1024);
close(bin->fd);
if (!memcmp(buf, "\x7F\x45\x4c\x46", 4)) {
if (buf[4] == 2) /* buf[EI_CLASS] == ELFCLASS64 */
strcpy(plugin_name, "bininfo_elf64");
else strcpy(plugin_name, "bininfo_elf");
} else if (!memcmp(buf, "\x4d\x5a", 2) &&
!memcmp(buf+(buf[0x3c]|(buf[0x3d]<<8)), "\x50\x45", 2)) {
if (!memcmp(buf+(buf[0x3c]|buf[0x3d]<<8)+0x18, "\x0b\x02", 2))
strcpy(plugin_name, "bininfo_pe64");
else strcpy(plugin_name, "bininfo_pe");
} else if (!memcmp(buf, "\xca\xfe\xba\xbe", 4))
strcpy(plugin_name, "bininfo_java");
else return R_FALSE;
}
struct list_head *pos;
list_for_each_prev(pos, &bin->bins) {
struct r_bininfo_handle_t *h = list_entry(pos, struct r_bininfo_handle_t, list);
if (!strcmp(h->name, plugin_name)) {
if ((plugin_name && !strcmp(h->name, plugin_name)) ||
(h->check && h->check(bin)))
bin->cur = h;
}
}
if (bin->cur && bin->cur->open)

View File

@ -66,11 +66,12 @@ struct r_bininfo_handle_t r_bininfo_plugin_addr2line = {
.fini = NULL,
.open = &a2l_open,
.close = NULL,
.check = NULL,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_BIN,
.type = R_LIB_TYPE_BININFO,
.data = &r_bininfo_plugin_addr2line
};
#endif

View File

@ -37,6 +37,7 @@ struct r_bin_handle_t {
int (*fini)(void *user);
int (*open)(struct r_bin_t *bin);
int (*close)(struct r_bin_t *bin);
int (*check)(struct r_bin_t *bin);
u64 (*baddr)(struct r_bin_t *bin);
struct r_bin_entry_t* (*entry)(struct r_bin_t *bin);
struct r_bin_section_t* (*sections)(struct r_bin_t *bin);

View File

@ -37,6 +37,7 @@ struct r_bininfo_handle_t {
char *(*get_function_name)(struct r_bininfo_t *bi, u64 addr, char *file, int len);
int (*open)(struct r_bininfo_t *bin);
int (*close)(struct r_bininfo_t *bin);
int (*check)(struct r_bininfo_t *bin);
struct list_head list;
};