- 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
This commit is contained in:
Nibble 2010-05-30 06:06:25 +02:00
parent 6fa84400a6
commit 2f43431e80
19 changed files with 108 additions and 31 deletions

View File

@ -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)

View File

@ -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;
}

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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);

View File

@ -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,

View File

@ -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 = &sections,
.symbols = &symbols,

View File

@ -28,6 +28,7 @@ struct r_bin_plugin_t r_bin_plugin_elf64 = {
.destroy = &destroy,
.check = &check,
.baddr = &baddr,
.main = &binmain,
.entries = &entries,
.sections = &sections,
.symbols = &symbols,

View File

@ -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,

View File

@ -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 = &sections,
.symbols = &symbols,

View File

@ -25,6 +25,7 @@ struct r_bin_plugin_t r_bin_plugin_mach064 = {
.destroy = &destroy,
.check = &check,
.baddr = &baddr,
.main = NULL,
.entries = &entries,
.sections = &sections,
.symbols = &symbols,

View File

@ -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 = &sections,
.symbols = &symbols,

View File

@ -28,6 +28,7 @@ struct r_bin_plugin_t r_bin_plugin_pe64 = {
.destroy = &destroy,
.check = &check,
.baddr = &baddr,
.main = NULL,
.entries = &entries,
.sections = &sections,
.symbols = &symbols,

View File

@ -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);

View File

@ -12,7 +12,8 @@ namespace Radare {
public int load(string file, string? plugin_name = null);
public int list();
public uint64 get_baddr();
public RList<RBin.Entry> get_entries();
public RBin.Addr get_main();
public RList<RBin.Addr> get_entries();
public RList<RBin.Field> get_fields();
public RList<RBin.Import> get_imports();
public RList<RBin.Section> 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;
}

View File

@ -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);
}