From 2f43431e80d04539ce5c456941573a10994b092d Mon Sep 17 00:00:00 2001 From: Nibble Date: Sun, 30 May 2010 06:06:25 +0200 Subject: [PATCH] * r_bin - Add r_bin_get_main - Implement get_main for elf32 & elf64 - Rename RBinEntry to RBinAddr * rabin2 - Add flag -M to output main offset and va * bindings - Update r_bin.vapi - Minor fixup in r_util.vapi --- binr/rabin2/rabin2.c | 30 ++++++++++++++++++++++++++++-- libr/bin/bin.c | 6 ++++++ libr/bin/format/elf/elf.c | 22 ++++++++++++++++++++++ libr/bin/format/elf/elf.h | 1 + libr/bin/format/mach0/mach0.c | 6 +++--- libr/bin/format/mach0/mach0.h | 4 ++-- libr/bin/format/pe/pe.c | 6 +++--- libr/bin/format/pe/pe.h | 4 ++-- libr/bin/p/bin_dummy.c | 1 + libr/bin/p/bin_elf.c | 17 ++++++++++++++--- libr/bin/p/bin_elf64.c | 1 + libr/bin/p/bin_java.c | 7 ++++--- libr/bin/p/bin_mach0.c | 9 +++++---- libr/bin/p/bin_mach064.c | 1 + libr/bin/p/bin_pe.c | 7 ++++--- libr/bin/p/bin_pe64.c | 1 + libr/include/r_bin.h | 7 +++++-- swig/vapi/r_bin.vapi | 7 ++++--- swig/vapi/r_util.vapi | 2 +- 19 files changed, 108 insertions(+), 31 deletions(-) diff --git a/binr/rabin2/rabin2.c b/binr/rabin2/rabin2.c index 07b691768c..01823dd71d 100644 --- a/binr/rabin2/rabin2.c +++ b/binr/rabin2/rabin2.c @@ -27,6 +27,7 @@ #define ACTION_FIELDS 0x0100 #define ACTION_LIBS 0x0200 #define ACTION_SRCLINE 0x0400 +#define ACTION_MAIN 0x0800 static struct r_lib_t *l; static struct r_bin_t *bin = NULL; @@ -39,6 +40,7 @@ static char* output = "a.out"; static int rabin_show_help() { printf ("rabin2 [options] [file]\n" " -e Entrypoint\n" + " -M Main\n" " -i Imports (symbols imported from libraries)\n" " -s Symbols (exports)\n" " -S Sections\n" @@ -62,7 +64,7 @@ static int rabin_show_help() { static int rabin_show_entrypoints() { RList *entries; RListIter *iter; - RBinEntry *entry; + RBinAddr *entry; int i = 0; ut64 baddr = r_bin_get_baddr (bin); @@ -87,6 +89,25 @@ static int rabin_show_entrypoints() { return R_TRUE; } +static int rabin_show_main() { + RBinAddr *binmain; + + ut64 baddr = r_bin_get_baddr (bin); + + if ((binmain = r_bin_get_main (bin)) == NULL) + return R_FALSE; + + if (rad) printf ("fs symbols\n"); + else printf ("[Main]\n"); + + if (rad) { + printf ("f main @ 0x%08"PFMT64x"\n", va?baddr+binmain->rva:binmain->offset); + } else printf ("address=0x%08"PFMT64x" offset=0x%08"PFMT64x"\n", + baddr+binmain->rva, binmain->offset); + + return R_TRUE; +} + static int rabin_show_libs() { RList *libs; RListIter *iter; @@ -531,7 +552,7 @@ int main(int argc, char **argv) r_lib_opendir (l, LIBDIR"/radare2/"); } - while ((c = getopt (argc, argv, "m:@:VisSzIHelwO:o:f:rvLh")) != -1) { + while ((c = getopt (argc, argv, "Mm:@:VisSzIHelwO:o:f:rvLh")) != -1) { switch(c) { case 'm': at = r_num_math (NULL, optarg); @@ -557,6 +578,9 @@ int main(int argc, char **argv) case 'e': action |= ACTION_ENTRIES; break; + case 'M': + action |= ACTION_MAIN; + break; case 'l': action |= ACTION_LIBS; break; @@ -611,6 +635,8 @@ int main(int argc, char **argv) rabin_show_sections (at); if (action&ACTION_ENTRIES) rabin_show_entrypoints (); + if (action&ACTION_MAIN) + rabin_show_main (); if (action&ACTION_IMPORTS) rabin_show_imports (at); if (action&ACTION_SYMBOLS) diff --git a/libr/bin/bin.c b/libr/bin/bin.c index e9465d6f14..c6f97aa073 100644 --- a/libr/bin/bin.c +++ b/libr/bin/bin.c @@ -56,6 +56,8 @@ static void r_bin_init_items(RBin *bin) { return; if (bin->cur->baddr) bin->baddr = bin->cur->baddr (bin); + if (bin->cur->main) + bin->main = bin->cur->main (bin); if (bin->cur->entries) bin->entries = bin->cur->entries (bin); if (bin->cur->fields) @@ -151,6 +153,10 @@ R_API ut64 r_bin_get_baddr(RBin *bin) { return bin->baddr; } +R_API RBinAddr* r_bin_get_main(RBin *bin) { + return bin->main; +} + R_API RList* r_bin_get_entries(RBin *bin) { return bin->entries; } diff --git a/libr/bin/format/elf/elf.c b/libr/bin/format/elf/elf.c index cfa57439d6..9cbb6b27a4 100644 --- a/libr/bin/format/elf/elf.c +++ b/libr/bin/format/elf/elf.c @@ -208,6 +208,28 @@ ut64 Elf_(r_bin_elf_get_entry_offset)(struct Elf_(r_bin_elf_obj_t) *bin) { return bin->ehdr.e_entry - bin->baddr; } +ut64 Elf_(r_bin_elf_get_main_offset)(struct Elf_(r_bin_elf_obj_t) *bin) { + ut64 entry = Elf_(r_bin_elf_get_entry_offset) (bin); + ut8 buf[7]; + +#if R_BIN_ELF64 + if (r_buf_read_at (bin->b, entry+29, buf, 7) == -1) { + eprintf ("Error: read (entry)\n"); + return 0; + } + if (!memcmp (buf, "\x48\xc7\xc7", 3)) + return (ut64)((int)(buf[3]+(buf[4]<<8)+(buf[5]<<16)+(buf[6]<<24)))-bin->baddr; +#else + if (r_buf_read_at (bin->b, entry+23, buf, 5) == -1) { + eprintf ("Error: read (entry)\n"); + return 0; + } + if (buf[0] == '\x68') + return (ut64)((int)(buf[1]+(buf[2]<<8)+(buf[3]<<16)+(buf[4]<<24)))-bin->baddr; +#endif + return 0; +} + int Elf_(r_bin_elf_get_stripped)(struct Elf_(r_bin_elf_obj_t) *bin) { int i; diff --git a/libr/bin/format/elf/elf.h b/libr/bin/format/elf/elf.h index 81965e140e..95b692193a 100644 --- a/libr/bin/format/elf/elf.h +++ b/libr/bin/format/elf/elf.h @@ -67,6 +67,7 @@ struct Elf_(r_bin_elf_obj_t) { ut64 Elf_(r_bin_elf_get_baddr)(struct Elf_(r_bin_elf_obj_t) *bin); ut64 Elf_(r_bin_elf_get_entry_offset)(struct Elf_(r_bin_elf_obj_t) *bin); +ut64 Elf_(r_bin_elf_get_main_offset)(struct Elf_(r_bin_elf_obj_t) *bin); int Elf_(r_bin_elf_get_stripped)(struct Elf_(r_bin_elf_obj_t) *bin); int Elf_(r_bin_elf_get_static)(struct Elf_(r_bin_elf_obj_t) *bin); char* Elf_(r_bin_elf_get_data_encoding)(struct Elf_(r_bin_elf_obj_t) *bin); diff --git a/libr/bin/format/mach0/mach0.c b/libr/bin/format/mach0/mach0.c index 5123771013..e11b7c6df6 100644 --- a/libr/bin/format/mach0/mach0.c +++ b/libr/bin/format/mach0/mach0.c @@ -446,14 +446,14 @@ struct r_bin_mach0_import_t* MACH0_(r_bin_mach0_get_imports)(struct MACH0_(r_bin return imports; } -struct r_bin_mach0_entrypoint_t* MACH0_(r_bin_mach0_get_entrypoint)(struct MACH0_(r_bin_mach0_obj_t)* bin) +struct r_bin_mach0_addr_t* MACH0_(r_bin_mach0_get_entrypoint)(struct MACH0_(r_bin_mach0_obj_t)* bin) { - struct r_bin_mach0_entrypoint_t *entry; + struct r_bin_mach0_addr_t *entry; int i; if (!bin->entry && !bin->sects) return NULL; - if (!(entry = malloc(sizeof(struct r_bin_mach0_entrypoint_t)))) + if (!(entry = malloc(sizeof(struct r_bin_mach0_addr_t)))) return NULL; if (bin->entry) { entry->offset = MACH0_(r_bin_mach0_addr_to_offset)(bin, bin->entry); diff --git a/libr/bin/format/mach0/mach0.h b/libr/bin/format/mach0/mach0.h index 053c676e8d..5bbc8f3e0b 100644 --- a/libr/bin/format/mach0/mach0.h +++ b/libr/bin/format/mach0/mach0.h @@ -40,7 +40,7 @@ struct r_bin_mach0_import_t { int last; }; -struct r_bin_mach0_entrypoint_t { +struct r_bin_mach0_addr_t { ut64 offset; ut64 addr; int last; @@ -92,7 +92,7 @@ void* MACH0_(r_bin_mach0_free)(struct MACH0_(r_bin_mach0_obj_t)* bin); struct r_bin_mach0_section_t* MACH0_(r_bin_mach0_get_sections)(struct MACH0_(r_bin_mach0_obj_t)* bin); struct r_bin_mach0_symbol_t* MACH0_(r_bin_mach0_get_symbols)(struct MACH0_(r_bin_mach0_obj_t)* bin); struct r_bin_mach0_import_t* MACH0_(r_bin_mach0_get_imports)(struct MACH0_(r_bin_mach0_obj_t)* bin); -struct r_bin_mach0_entrypoint_t* MACH0_(r_bin_mach0_get_entrypoint)(struct MACH0_(r_bin_mach0_obj_t)* bin); +struct r_bin_mach0_addr_t* MACH0_(r_bin_mach0_get_entrypoint)(struct MACH0_(r_bin_mach0_obj_t)* bin); struct r_bin_mach0_lib_t* MACH0_(r_bin_mach0_get_libs)(struct MACH0_(r_bin_mach0_obj_t)* bin); ut64 MACH0_(r_bin_mach0_get_baddr)(struct MACH0_(r_bin_mach0_obj_t)* bin); char* MACH0_(r_bin_mach0_get_class)(struct MACH0_(r_bin_mach0_obj_t)* bin); diff --git a/libr/bin/format/pe/pe.c b/libr/bin/format/pe/pe.c index 2bc35f7a32..aa759c897f 100644 --- a/libr/bin/format/pe/pe.c +++ b/libr/bin/format/pe/pe.c @@ -261,11 +261,11 @@ char* PE_(r_bin_pe_get_arch)(struct PE_(r_bin_pe_obj_t)* bin) return arch; } -struct r_bin_pe_entrypoint_t* PE_(r_bin_pe_get_entrypoint)(struct PE_(r_bin_pe_obj_t)* bin) +struct r_bin_pe_addr_t* PE_(r_bin_pe_get_entrypoint)(struct PE_(r_bin_pe_obj_t)* bin) { - struct r_bin_pe_entrypoint_t *entry = NULL; + struct r_bin_pe_addr_t *entry = NULL; - if ((entry = malloc(sizeof(struct r_bin_pe_entrypoint_t))) == NULL) { + if ((entry = malloc(sizeof(struct r_bin_pe_addr_t))) == NULL) { perror("malloc (entrypoint)"); return NULL; } diff --git a/libr/bin/format/pe/pe.h b/libr/bin/format/pe/pe.h index d73a6d5290..242da18f22 100644 --- a/libr/bin/format/pe/pe.h +++ b/libr/bin/format/pe/pe.h @@ -11,7 +11,7 @@ #define R_BIN_PE_SCN_IS_READABLE(x) x & PE_IMAGE_SCN_MEM_READ #define R_BIN_PE_SCN_IS_WRITABLE(x) x & PE_IMAGE_SCN_MEM_WRITE -struct r_bin_pe_entrypoint_t { +struct r_bin_pe_addr_t { ut64 rva; ut64 offset; }; @@ -74,7 +74,7 @@ struct PE_(r_bin_pe_obj_t) { }; char* PE_(r_bin_pe_get_arch)(struct PE_(r_bin_pe_obj_t)* bin); -struct r_bin_pe_entrypoint_t* PE_(r_bin_pe_get_entrypoint)(struct PE_(r_bin_pe_obj_t)* bin); +struct r_bin_pe_addr_t* PE_(r_bin_pe_get_entrypoint)(struct PE_(r_bin_pe_obj_t)* bin); struct r_bin_pe_export_t* PE_(r_bin_pe_get_exports)(struct PE_(r_bin_pe_obj_t)* bin); // TODO int PE_(r_bin_pe_get_file_alignment)(struct PE_(r_bin_pe_obj_t)* bin); ut64 PE_(r_bin_pe_get_image_base)(struct PE_(r_bin_pe_obj_t)* bin); diff --git a/libr/bin/p/bin_dummy.c b/libr/bin/p/bin_dummy.c index f85d75491a..bb70a5ea45 100644 --- a/libr/bin/p/bin_dummy.c +++ b/libr/bin/p/bin_dummy.c @@ -36,6 +36,7 @@ struct r_bin_plugin_t r_bin_plugin_dummy = { .destroy = &destroy, .check = NULL, .baddr = &baddr, + .main = NULL, .entries = NULL, .sections = NULL, .symbols = NULL, diff --git a/libr/bin/p/bin_elf.c b/libr/bin/p/bin_elf.c index e25730d1ef..80f04541f4 100644 --- a/libr/bin/p/bin_elf.c +++ b/libr/bin/p/bin_elf.c @@ -23,16 +23,26 @@ static ut64 baddr(RBin *bin) { return Elf_(r_bin_elf_get_baddr) (bin->bin_obj); } +static RBinAddr* binmain(RBin *bin) { + RBinAddr *ret = NULL; + + if (!(ret = R_NEW (RBinAddr))) + return NULL; + memset (ret, '\0', sizeof (RBinAddr)); + ret->offset = ret->rva = Elf_(r_bin_elf_get_main_offset) (bin->bin_obj); + return ret; +} + static RList* entries(RBin *bin) { RList *ret; - RBinEntry *ptr = NULL; + RBinAddr *ptr = NULL; if (!(ret = r_list_new ())) return NULL; ret->free = free; - if (!(ptr = R_NEW (RBinEntry))) + if (!(ptr = R_NEW (RBinAddr))) return ret; - memset (ptr, '\0', sizeof (RBinEntry)); + memset (ptr, '\0', sizeof (RBinAddr)); ptr->offset = ptr->rva = Elf_(r_bin_elf_get_entry_offset) (bin->bin_obj); r_list_append (ret, ptr); return ret; @@ -244,6 +254,7 @@ struct r_bin_plugin_t r_bin_plugin_elf = { .destroy = &destroy, .check = &check, .baddr = &baddr, + .main = &binmain, .entries = &entries, .sections = §ions, .symbols = &symbols, diff --git a/libr/bin/p/bin_elf64.c b/libr/bin/p/bin_elf64.c index eddf959b28..71fd3fa5c0 100644 --- a/libr/bin/p/bin_elf64.c +++ b/libr/bin/p/bin_elf64.c @@ -28,6 +28,7 @@ struct r_bin_plugin_t r_bin_plugin_elf64 = { .destroy = &destroy, .check = &check, .baddr = &baddr, + .main = &binmain, .entries = &entries, .sections = §ions, .symbols = &symbols, diff --git a/libr/bin/p/bin_java.c b/libr/bin/p/bin_java.c index 9ded789034..280f5ca08e 100644 --- a/libr/bin/p/bin_java.c +++ b/libr/bin/p/bin_java.c @@ -21,14 +21,14 @@ static int destroy(RBin *bin) { static RList* entries(RBin *bin) { RList *ret; - RBinEntry *ptr = NULL; + RBinAddr *ptr = NULL; if (!(ret = r_list_new ())) return NULL; ret->free = free; - if (!(ptr = R_NEW (RBinEntry))) + if (!(ptr = R_NEW (RBinAddr))) return ret; - memset (ptr, '\0', sizeof (RBinEntry)); + memset (ptr, '\0', sizeof (RBinAddr)); ptr->offset = ptr->rva = r_bin_java_get_entrypoint (bin->bin_obj); r_list_append (ret, ptr); return ret; @@ -132,6 +132,7 @@ struct r_bin_plugin_t r_bin_plugin_java = { .destroy = &destroy, .check = &check, .baddr = &baddr, + .main = NULL, .entries = &entries, .sections = NULL, .symbols = &symbols, diff --git a/libr/bin/p/bin_mach0.c b/libr/bin/p/bin_mach0.c index e4c3d7ef7b..dbe22e4c34 100644 --- a/libr/bin/p/bin_mach0.c +++ b/libr/bin/p/bin_mach0.c @@ -25,16 +25,16 @@ static ut64 baddr(RBin *bin) { static RList* entries(RBin *bin) { RList *ret; - RBinEntry *ptr = NULL; - struct r_bin_mach0_entrypoint_t *entry = NULL; + RBinAddr *ptr = NULL; + struct r_bin_mach0_addr_t *entry = NULL; if (!(ret = r_list_new ())) return NULL; ret->free = free; if (!(entry = MACH0_(r_bin_mach0_get_entrypoint) (bin->bin_obj))) return ret; - if ((ptr = R_NEW (RBinEntry))) { - memset (ptr, '\0', sizeof (RBinEntry)); + if ((ptr = R_NEW (RBinAddr))) { + memset (ptr, '\0', sizeof (RBinAddr)); ptr->offset = entry->offset; ptr->rva = entry->addr; r_list_append (ret, ptr); @@ -203,6 +203,7 @@ struct r_bin_plugin_t r_bin_plugin_mach0 = { .destroy = &destroy, .check = &check, .baddr = &baddr, + .main = NULL, .entries = &entries, .sections = §ions, .symbols = &symbols, diff --git a/libr/bin/p/bin_mach064.c b/libr/bin/p/bin_mach064.c index b2fa40b478..1231f1f32f 100644 --- a/libr/bin/p/bin_mach064.c +++ b/libr/bin/p/bin_mach064.c @@ -25,6 +25,7 @@ struct r_bin_plugin_t r_bin_plugin_mach064 = { .destroy = &destroy, .check = &check, .baddr = &baddr, + .main = NULL, .entries = &entries, .sections = §ions, .symbols = &symbols, diff --git a/libr/bin/p/bin_pe.c b/libr/bin/p/bin_pe.c index 3dcdbbc946..6ace316cd0 100644 --- a/libr/bin/p/bin_pe.c +++ b/libr/bin/p/bin_pe.c @@ -25,15 +25,15 @@ static ut64 baddr(RBin *bin) { static RList* entries(RBin *bin) { RList* ret; - RBinEntry *ptr = NULL; - struct r_bin_pe_entrypoint_t *entry = NULL; + RBinAddr *ptr = NULL; + struct r_bin_pe_addr_t *entry = NULL; if (!(ret = r_list_new ())) return NULL; ret->free = free; if (!(entry = PE_(r_bin_pe_get_entrypoint) (bin->bin_obj))) return ret; - if ((ptr = R_NEW (RBinEntry))) { + if ((ptr = R_NEW (RBinAddr))) { ptr->offset = entry->offset; ptr->rva = entry->rva; r_list_append (ret, ptr); @@ -222,6 +222,7 @@ struct r_bin_plugin_t r_bin_plugin_pe = { .destroy = &destroy, .check = &check, .baddr = &baddr, + .main = NULL, .entries = &entries, .sections = §ions, .symbols = &symbols, diff --git a/libr/bin/p/bin_pe64.c b/libr/bin/p/bin_pe64.c index f513182e14..b0d33b2f93 100644 --- a/libr/bin/p/bin_pe64.c +++ b/libr/bin/p/bin_pe64.c @@ -28,6 +28,7 @@ struct r_bin_plugin_t r_bin_plugin_pe64 = { .destroy = &destroy, .check = &check, .baddr = &baddr, + .main = NULL, .entries = &entries, .sections = §ions, .symbols = &symbols, diff --git a/libr/include/r_bin.h b/libr/include/r_bin.h index 1dcd931b1c..b4ed6ea6de 100644 --- a/libr/include/r_bin.h +++ b/libr/include/r_bin.h @@ -26,6 +26,7 @@ typedef struct r_bin_t { int size; void *bin_obj; ut64 baddr; + struct r_bin_addr_t *main; struct r_bin_info_t *info; RList* entries; RList* sections; @@ -49,6 +50,7 @@ typedef struct r_bin_plugin_t { int (*destroy)(RBin *bin); int (*check)(RBin *bin); ut64 (*baddr)(RBin *bin); + struct r_bin_addr_t* (*main)(RBin *bin); RList* (*entries)(RBin *bin); RList* (*sections)(RBin *bin); RList* (*symbols)(RBin *bin); @@ -62,10 +64,10 @@ typedef struct r_bin_plugin_t { struct list_head list; } RBinPlugin; -typedef struct r_bin_entry_t { +typedef struct r_bin_addr_t { ut64 rva; ut64 offset; -} RBinEntry; +} RBinAddr; typedef struct r_bin_section_t { char name[R_BIN_SIZEOF_STRINGS]; @@ -143,6 +145,7 @@ R_API void* r_bin_free(RBin *bin); R_API int r_bin_list(RBin *bin); R_API int r_bin_load(RBin *bin, const char *file, const char *plugin_name); R_API ut64 r_bin_get_baddr(RBin *bin); +R_API RBinAddr* r_bin_get_main(RBin *bin); R_API RList* r_bin_get_entries(RBin *bin); R_API RList* r_bin_get_fields(RBin *bin); R_API RList* r_bin_get_imports(RBin *bin); diff --git a/swig/vapi/r_bin.vapi b/swig/vapi/r_bin.vapi index c7fe3b951a..1c0cc6211a 100644 --- a/swig/vapi/r_bin.vapi +++ b/swig/vapi/r_bin.vapi @@ -12,7 +12,8 @@ namespace Radare { public int load(string file, string? plugin_name = null); public int list(); public uint64 get_baddr(); - public RList get_entries(); + public RBin.Addr get_main(); + public RList get_entries(); public RList get_fields(); public RList get_imports(); public RList get_sections(); @@ -28,8 +29,8 @@ namespace Radare { public int meta_get_line(uint64 addr, ref string file, int len, out int line); public string meta_get_source_line(uint64 addr); - [CCode (cname="RBinEntry", free_function="", ref_function="", unref_function="")] - public class Entry { + [CCode (cname="RBinAddr", free_function="", ref_function="", unref_function="")] + public class Addr { public uint64 rva; public uint64 offset; } diff --git a/swig/vapi/r_util.vapi b/swig/vapi/r_util.vapi index fe670cc134..5d270b42d1 100644 --- a/swig/vapi/r_util.vapi +++ b/swig/vapi/r_util.vapi @@ -38,7 +38,7 @@ public static class Radare.RHex { [CCode (cheader_filename="r_util.h", cprefix="r_hex_", free_function="")] public static class RHex { - public static int str2bin (string input, uint8 *buf); + public static int str2bin (string input, out uint8 *buf); public static int bin2str (uint8 *buf, int len, out string str); public static string bin2strdup (uint8 *buf, int len); }