diff --git a/libr/bin/bin.c b/libr/bin/bin.c index 562c3fcb90..3a6fd15a37 100644 --- a/libr/bin/bin.c +++ b/libr/bin/bin.c @@ -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) diff --git a/libr/bin/p/bin_elf.c b/libr/bin/p/bin_elf.c index 7fb3ca6f0d..e4b2470a0b 100644 --- a/libr/bin/p/bin_elf.c +++ b/libr/bin/p/bin_elf.c @@ -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 = §ions, diff --git a/libr/bin/p/bin_elf64.c b/libr/bin/p/bin_elf64.c index 1deb5dc865..3e1c986e94 100644 --- a/libr/bin/p/bin_elf64.c +++ b/libr/bin/p/bin_elf64.c @@ -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 = §ions, diff --git a/libr/bin/p/bin_java.c b/libr/bin/p/bin_java.c index b6ceeaab2a..a0f9a51731 100644 --- a/libr/bin/p/bin_java.c +++ b/libr/bin/p/bin_java.c @@ -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, diff --git a/libr/bin/p/bin_pe.c b/libr/bin/p/bin_pe.c index c924dfd5b5..52b81f8a9b 100644 --- a/libr/bin/p/bin_pe.c +++ b/libr/bin/p/bin_pe.c @@ -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 = §ions, diff --git a/libr/bin/p/bin_pe64.c b/libr/bin/p/bin_pe64.c index f3a2f11a20..1412ce8d19 100644 --- a/libr/bin/p/bin_pe64.c +++ b/libr/bin/p/bin_pe64.c @@ -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 = §ions, diff --git a/libr/bin/t/rabin2.c b/libr/bin/t/rabin2.c index 1173f4fd70..8be669f9a6 100644 --- a/libr/bin/t/rabin2.c +++ b/libr/bin/t/rabin2.c @@ -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(); diff --git a/libr/bininfo/bininfo.c b/libr/bininfo/bininfo.c index 3c6863a5b9..ded8ff839f 100644 --- a/libr/bininfo/bininfo.c +++ b/libr/bininfo/bininfo.c @@ -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) diff --git a/libr/bininfo/p/bininfo_addr2line.c b/libr/bininfo/p/bininfo_addr2line.c index 1a2c64c785..e568d0e9ab 100644 --- a/libr/bininfo/p/bininfo_addr2line.c +++ b/libr/bininfo/p/bininfo_addr2line.c @@ -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 diff --git a/libr/include/r_bin.h b/libr/include/r_bin.h index f91e3f09d5..bc4407f4d0 100644 --- a/libr/include/r_bin.h +++ b/libr/include/r_bin.h @@ -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); diff --git a/libr/include/r_bininfo.h b/libr/include/r_bininfo.h index d389562e95..d93fe5afb0 100644 --- a/libr/include/r_bininfo.h +++ b/libr/include/r_bininfo.h @@ -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; };