2011-02-25 03:19:30 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2011 nibble<.ds@gmail.com> */
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
/* TODO:
|
2011-07-25 10:22:55 +00:00
|
|
|
* Use -v to show version information.. not -V .. like the rest of tools
|
|
|
|
* --- needs sync with callers and so on..
|
2011-05-25 13:43:54 +00:00
|
|
|
* -L [lib] dlopen library and show addr
|
2010-05-28 15:15:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_list.h>
|
|
|
|
#include <r_lib.h>
|
|
|
|
#include <r_bin.h>
|
|
|
|
#include <r_flags.h>
|
|
|
|
#include <r_util.h>
|
|
|
|
|
|
|
|
#define ACTION_UNK 0x0000
|
2010-07-29 14:04:18 +00:00
|
|
|
#define ACTION_ENTRIES 0x0001
|
|
|
|
#define ACTION_IMPORTS 0x0002
|
|
|
|
#define ACTION_SYMBOLS 0x0004
|
|
|
|
#define ACTION_SECTIONS 0x0008
|
2010-05-28 15:15:20 +00:00
|
|
|
#define ACTION_INFO 0x0010
|
|
|
|
#define ACTION_OPERATION 0x0020
|
|
|
|
#define ACTION_HELP 0x0040
|
2010-07-29 14:04:18 +00:00
|
|
|
#define ACTION_STRINGS 0x0080
|
|
|
|
#define ACTION_FIELDS 0x0100
|
|
|
|
#define ACTION_LIBS 0x0200
|
|
|
|
#define ACTION_SRCLINE 0x0400
|
|
|
|
#define ACTION_MAIN 0x0800
|
|
|
|
#define ACTION_EXTRACT 0x1000
|
2010-09-10 09:11:38 +00:00
|
|
|
#define ACTION_RELOCS 0x2000
|
2010-09-24 19:23:13 +00:00
|
|
|
#define ACTION_LISTARCHS 0x4000
|
2011-07-25 19:10:25 +00:00
|
|
|
#define ACTION_CREATE 0x8000
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
static struct r_lib_t *l;
|
|
|
|
static struct r_bin_t *bin = NULL;
|
|
|
|
static int rad = R_FALSE;
|
|
|
|
static int rw = R_FALSE;
|
|
|
|
static int va = R_FALSE;
|
2010-08-16 12:35:15 +00:00
|
|
|
static ut64 gbaddr = 0LL;
|
2010-05-28 15:15:20 +00:00
|
|
|
static char* file = NULL;
|
2010-10-04 11:57:48 +00:00
|
|
|
static char* output = "out";
|
2011-07-25 19:10:25 +00:00
|
|
|
static char* create = NULL;
|
2010-08-17 11:15:18 +00:00
|
|
|
static ut64 at = 0LL;
|
|
|
|
static char *name = NULL;
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
static int rabin_show_help() {
|
|
|
|
printf ("rabin2 [options] [file]\n"
|
2011-07-25 19:10:25 +00:00
|
|
|
" -A list archs\n"
|
|
|
|
" -a [arch_bits] set arch\n"
|
|
|
|
" -b [addr] override baddr\n"
|
2011-08-08 00:07:26 +00:00
|
|
|
" -c [fmt:C:D] create [elf,mach0,pe] with Code and Data hexpairs (see -a)\n"
|
2011-07-25 19:10:25 +00:00
|
|
|
" -e entrypoint\n"
|
|
|
|
" -f [str] select sub-bin named str\n"
|
|
|
|
" -i imports (symbols imported from libraries)\n"
|
|
|
|
" -s symbols (exports)\n"
|
|
|
|
" -S sections\n"
|
|
|
|
" -M main (show address of main symbol)\n"
|
|
|
|
" -z strings\n"
|
|
|
|
" -I binary info\n"
|
|
|
|
" -H header fields\n"
|
|
|
|
" -l linked libraries\n"
|
|
|
|
" -R relocations\n"
|
|
|
|
" -O [str] write/extract operations (str=help for help)\n"
|
|
|
|
" -o [str] output file/folder for write operations (out by default)\n"
|
|
|
|
" -r radare output\n"
|
|
|
|
" -v use vaddr in radare output\n"
|
|
|
|
" -m [addr] show source line at addr\n"
|
|
|
|
" -L list supported bin plugins\n"
|
|
|
|
" -@ [addr] show section, symbol or import at addr\n"
|
|
|
|
" -n [str] show section, symbol or import named str\n"
|
|
|
|
" -x extract bins contained in file\n"
|
|
|
|
" -V show version information\n"
|
|
|
|
" -h this help\n");
|
2010-09-24 19:23:13 +00:00
|
|
|
return 1;
|
2010-05-28 15:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rabin_show_entrypoints() {
|
|
|
|
RList *entries;
|
|
|
|
RListIter *iter;
|
2010-05-30 04:06:25 +00:00
|
|
|
RBinAddr *entry;
|
2010-05-28 15:15:20 +00:00
|
|
|
int i = 0;
|
|
|
|
|
2010-08-16 12:35:15 +00:00
|
|
|
ut64 baddr = gbaddr?gbaddr:r_bin_get_baddr (bin);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
if ((entries = r_bin_get_entries (bin)) == NULL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
if (rad) printf ("fs symbols\n");
|
2010-09-24 19:23:13 +00:00
|
|
|
else eprintf ("[Entrypoints]\n");
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
r_list_foreach (entries, iter, entry) {
|
|
|
|
if (rad) {
|
|
|
|
printf ("f entry%i @ 0x%08"PFMT64x"\n", i, va?baddr+entry->rva:entry->offset);
|
|
|
|
printf ("s entry%i\n", i);
|
2011-05-25 13:43:54 +00:00
|
|
|
} else printf ("addr=0x%08"PFMT64x" off=0x%08"PFMT64x" baddr=0x%08"PFMT64x"\n",
|
2010-05-28 15:15:20 +00:00
|
|
|
baddr+entry->rva, entry->offset, baddr);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
if (!rad) eprintf ("\n%i entrypoints\n", i);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-05-30 04:06:25 +00:00
|
|
|
static int rabin_show_main() {
|
|
|
|
RBinAddr *binmain;
|
2011-07-25 19:10:25 +00:00
|
|
|
ut64 baddr = gbaddr? gbaddr: r_bin_get_baddr (bin);
|
2010-05-30 04:06:25 +00:00
|
|
|
|
2010-11-18 10:41:17 +00:00
|
|
|
if ((binmain = r_bin_get_sym (bin, R_BIN_SYM_MAIN)) == NULL)
|
2010-05-30 04:06:25 +00:00
|
|
|
return R_FALSE;
|
|
|
|
if (rad) {
|
2010-10-18 18:47:13 +00:00
|
|
|
printf ("fs symbols\n");
|
2011-07-25 19:10:25 +00:00
|
|
|
printf ("f main @ 0x%08"PFMT64x"\n", va? baddr+binmain->rva: binmain->offset);
|
2010-10-18 18:47:13 +00:00
|
|
|
} else {
|
|
|
|
eprintf ("[Main]\n");
|
2011-05-25 13:43:54 +00:00
|
|
|
printf ("addr=0x%08"PFMT64x" off=0x%08"PFMT64x"\n",
|
2010-05-30 04:06:25 +00:00
|
|
|
baddr+binmain->rva, binmain->offset);
|
2010-10-18 18:47:13 +00:00
|
|
|
}
|
2010-05-30 04:06:25 +00:00
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-09-25 10:33:30 +00:00
|
|
|
static int rabin_extract(int all) {
|
2010-10-04 11:57:48 +00:00
|
|
|
char outfile[512], outpath[512], *path, *ptr;
|
2010-10-01 09:10:59 +00:00
|
|
|
int i = 0;
|
2010-10-01 06:12:43 +00:00
|
|
|
|
2010-10-04 11:57:48 +00:00
|
|
|
// XXX: Wrong for w32 (/)
|
2010-09-25 10:33:30 +00:00
|
|
|
if (all) {
|
|
|
|
for (i=0; i<bin->narch; i++) {
|
2011-07-25 19:10:25 +00:00
|
|
|
r_bin_select_idx (bin, i);
|
2010-10-04 02:01:25 +00:00
|
|
|
if (bin->curarch.info == NULL) {
|
2010-10-01 09:58:11 +00:00
|
|
|
eprintf ("No extract info found.\n");
|
|
|
|
} else {
|
2010-10-04 11:57:48 +00:00
|
|
|
path = strdup (bin->curarch.file);
|
|
|
|
if ((ptr = strrchr (path, '/'))) {
|
|
|
|
*ptr = '\0';
|
2010-10-01 09:58:11 +00:00
|
|
|
ptr = ptr+1;
|
2010-10-04 11:57:48 +00:00
|
|
|
}
|
2010-10-04 02:01:25 +00:00
|
|
|
else ptr = bin->curarch.file;
|
2010-10-04 11:57:48 +00:00
|
|
|
snprintf (outpath, sizeof (outpath), "%s/%s", output, path);
|
|
|
|
if (!r_sys_rmkdir (outpath)) {
|
|
|
|
eprintf ("Error creating dir structure\n");
|
2010-10-01 09:58:11 +00:00
|
|
|
return R_FALSE;
|
2010-10-04 11:57:48 +00:00
|
|
|
}
|
|
|
|
snprintf (outfile, sizeof (outfile), "%s/%s.%s_%i",
|
|
|
|
outpath, ptr, bin->curarch.info->arch,
|
|
|
|
bin->curarch.info->bits);
|
|
|
|
if (!r_file_dump (outfile, bin->curarch.buf->buf, bin->curarch.size)) {
|
|
|
|
eprintf ("Error extracting %s\n", outfile);
|
|
|
|
return R_FALSE;
|
|
|
|
} else printf ("%s created (%i)\n", outfile, bin->curarch.size);
|
2010-10-01 09:58:11 +00:00
|
|
|
}
|
2010-09-25 10:33:30 +00:00
|
|
|
}
|
2010-10-04 11:57:48 +00:00
|
|
|
} else { /* XXX: Use 'output' for filename? */
|
2010-10-04 02:01:25 +00:00
|
|
|
if (bin->curarch.info == NULL) {
|
2010-10-01 09:10:59 +00:00
|
|
|
eprintf ("No extract info found.\n");
|
|
|
|
} else {
|
2010-10-04 02:01:25 +00:00
|
|
|
if ((ptr = strrchr (bin->curarch.file, '/')))
|
2010-10-01 06:12:43 +00:00
|
|
|
ptr = ptr+1;
|
2010-10-04 02:01:25 +00:00
|
|
|
else ptr = bin->curarch.file;
|
2010-10-04 11:57:48 +00:00
|
|
|
snprintf (outfile, sizeof (outfile), "%s.%s_%i", ptr,
|
2010-10-04 02:01:25 +00:00
|
|
|
bin->curarch.info->arch, bin->curarch.info->bits);
|
2010-10-04 11:57:48 +00:00
|
|
|
if (!r_file_dump (outfile, bin->curarch.buf->buf, bin->curarch.size)) {
|
|
|
|
eprintf ("Error extracting %s\n", outfile);
|
2010-09-25 10:33:30 +00:00
|
|
|
return R_FALSE;
|
2010-10-04 11:57:48 +00:00
|
|
|
} else printf ("%s created (%i)\n", outfile, bin->curarch.size);
|
2010-09-25 10:33:30 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-24 19:23:13 +00:00
|
|
|
return R_TRUE;
|
2010-07-29 14:04:18 +00:00
|
|
|
}
|
|
|
|
|
2010-05-28 15:15:20 +00:00
|
|
|
static int rabin_show_libs() {
|
|
|
|
RList *libs;
|
|
|
|
RListIter *iter;
|
|
|
|
char* lib;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if ((libs = r_bin_get_libs (bin)) == NULL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
eprintf ("[Linked libraries]\n");
|
2010-05-28 15:15:20 +00:00
|
|
|
r_list_foreach (libs, iter, lib) {
|
|
|
|
printf ("%s\n", lib);
|
|
|
|
i++;
|
|
|
|
}
|
2010-09-24 19:23:13 +00:00
|
|
|
if (!rad) eprintf ("\n%i libraries\n", i);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-09-10 09:11:38 +00:00
|
|
|
static int rabin_show_relocs() {
|
|
|
|
RList *relocs;
|
|
|
|
RListIter *iter;
|
|
|
|
RBinReloc *reloc;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
ut64 baddr = gbaddr?gbaddr:r_bin_get_baddr (bin);
|
|
|
|
|
|
|
|
if ((relocs = r_bin_get_relocs (bin)) == NULL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
2011-07-25 19:10:25 +00:00
|
|
|
if (rad) {
|
|
|
|
printf ("fs relocs\n");
|
|
|
|
r_list_foreach (relocs, iter, reloc) {
|
|
|
|
printf ("f reloc.%s @ 0x%08"PFMT64x"\n", reloc->name, va?baddr+reloc->rva:reloc->offset);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintf ("[Relocations]\n");
|
|
|
|
r_list_foreach (relocs, iter, reloc) {
|
|
|
|
printf ("sym=%02i addr=0x%08"PFMT64x" off=0x%08"PFMT64x" type=0x%08x %s\n",
|
2010-09-10 09:11:38 +00:00
|
|
|
reloc->sym, baddr+reloc->rva, reloc->offset, reloc->type, reloc->name);
|
2011-07-25 19:10:25 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
eprintf ("\n%i relocations\n", i);
|
2010-09-10 09:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-08-17 11:15:18 +00:00
|
|
|
static int rabin_show_imports() {
|
2010-05-28 15:15:20 +00:00
|
|
|
RList *imports;
|
|
|
|
RListIter *iter;
|
|
|
|
RBinImport *import;
|
|
|
|
ut64 baddr;
|
|
|
|
int i = 0;
|
|
|
|
|
2010-08-16 12:35:15 +00:00
|
|
|
baddr = gbaddr?gbaddr:r_bin_get_baddr (bin);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
if ((imports = r_bin_get_imports (bin)) == NULL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
2011-02-28 16:27:08 +00:00
|
|
|
if (!at) {
|
|
|
|
if (rad) printf ("fs imports\n");
|
|
|
|
else eprintf ("[Imports]\n");
|
|
|
|
}
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
r_list_foreach (imports, iter, import) {
|
2010-08-17 11:15:18 +00:00
|
|
|
if (name && strcmp (import->name, name))
|
|
|
|
continue;
|
2010-05-28 15:15:20 +00:00
|
|
|
if (at) {
|
|
|
|
if (baddr+import->rva == at || import->offset == at)
|
|
|
|
printf ("%s\n", import->name);
|
|
|
|
} else {
|
|
|
|
if (rad) {
|
2011-06-05 21:30:19 +00:00
|
|
|
r_name_filter (import->name, sizeof (import->name));
|
2010-08-01 09:02:55 +00:00
|
|
|
if (import->size)
|
2011-02-28 16:27:08 +00:00
|
|
|
printf ("af+ 0x%08"PFMT64x" %"PFMT64d" imp.%s i\n",
|
2010-08-01 09:02:55 +00:00
|
|
|
va?baddr+import->rva:import->offset, import->size, import->name);
|
2010-05-28 15:15:20 +00:00
|
|
|
printf ("f imp.%s @ 0x%08"PFMT64x"\n",
|
|
|
|
import->name, va?baddr+import->rva:import->offset);
|
2011-05-25 13:43:54 +00:00
|
|
|
} else printf ("addr=0x%08"PFMT64x" off=0x%08"PFMT64x" ordinal=%03"PFMT64d" "
|
2010-05-28 15:15:20 +00:00
|
|
|
"hint=%03"PFMT64d" bind=%s type=%s name=%s\n",
|
|
|
|
baddr+import->rva, import->offset,
|
|
|
|
import->ordinal, import->hint, import->bind,
|
|
|
|
import->type, import->name);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
if (!at && !rad) eprintf ("\n%i imports\n", i);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-08-17 11:15:18 +00:00
|
|
|
static int rabin_show_symbols() {
|
2010-05-28 15:15:20 +00:00
|
|
|
RList *symbols;
|
|
|
|
RListIter *iter;
|
|
|
|
RBinSymbol *symbol;
|
|
|
|
ut64 baddr;
|
|
|
|
int i = 0;
|
|
|
|
|
2010-08-16 12:35:15 +00:00
|
|
|
baddr = gbaddr?gbaddr:r_bin_get_baddr (bin);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
if ((symbols = r_bin_get_symbols (bin)) == NULL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
if (!at) {
|
|
|
|
if (rad) printf ("fs symbols\n");
|
2010-09-24 19:23:13 +00:00
|
|
|
else eprintf ("[Symbols]\n");
|
2010-05-28 15:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r_list_foreach (symbols, iter, symbol) {
|
2010-08-17 11:15:18 +00:00
|
|
|
if (name && strcmp (symbol->name, name))
|
|
|
|
continue;
|
2010-05-28 15:15:20 +00:00
|
|
|
if (at) {
|
|
|
|
if ((symbol->size != 0 &&
|
|
|
|
((baddr+symbol->rva <= at && baddr+symbol->rva+symbol->size > at) ||
|
|
|
|
(symbol->offset <= at && symbol->offset+symbol->size > at))) ||
|
|
|
|
baddr+symbol->rva == at || symbol->offset == at)
|
|
|
|
printf("%s\n", symbol->name);
|
|
|
|
} else {
|
|
|
|
if (rad) {
|
2011-02-27 19:30:41 +00:00
|
|
|
char *mn = r_bin_demangle (bin, symbol->name);
|
2011-02-25 03:19:30 +00:00
|
|
|
if (mn) {
|
2011-02-27 19:30:41 +00:00
|
|
|
printf ("s 0x%08"PFMT64x"\n\"CC %s\"\n", symbol->offset, mn);
|
2011-02-25 03:19:30 +00:00
|
|
|
free (mn);
|
|
|
|
}
|
2011-06-05 21:30:19 +00:00
|
|
|
r_name_filter (symbol->name, sizeof (symbol->name));
|
2011-02-28 16:27:08 +00:00
|
|
|
if (!strncmp (symbol->type,"OBJECT", 6))
|
2010-05-28 15:15:20 +00:00
|
|
|
printf ("Cd %"PFMT64d" @ 0x%08"PFMT64x"\n",
|
|
|
|
symbol->size, va?baddr+symbol->rva:symbol->offset);
|
|
|
|
printf ("f sym.%s %"PFMT64d" 0x%08"PFMT64x"\n",
|
|
|
|
symbol->name, symbol->size,
|
|
|
|
va?baddr+symbol->rva:symbol->offset);
|
2011-05-25 13:43:54 +00:00
|
|
|
} else printf ("addr=0x%08"PFMT64x" off=0x%08"PFMT64x" ordinal=%03"PFMT64d" "
|
|
|
|
"forwarder=%s sz=%"PFMT64d" bind=%s type=%s name=%s\n",
|
2010-05-28 15:15:20 +00:00
|
|
|
baddr+symbol->rva, symbol->offset,
|
|
|
|
symbol->ordinal, symbol->forwarder,
|
|
|
|
symbol->size, symbol->bind, symbol->type,
|
|
|
|
symbol->name);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
if (!at && !rad) eprintf ("\n%i symbols\n", i);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rabin_show_strings() {
|
|
|
|
RList *strings;
|
|
|
|
RListIter *iter;
|
|
|
|
RBinString *string;
|
|
|
|
RBinSection *section;
|
|
|
|
int i = 0;
|
2010-08-16 12:35:15 +00:00
|
|
|
ut64 baddr = gbaddr?gbaddr:r_bin_get_baddr (bin);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
if ((strings = r_bin_get_strings (bin)) == NULL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
if (rad) printf ("fs strings\n");
|
2010-09-24 19:23:13 +00:00
|
|
|
else eprintf ("[strings]\n");
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
r_list_foreach (strings, iter, string) {
|
|
|
|
section = r_bin_get_section_at (bin, string->offset, 0);
|
|
|
|
if (rad) {
|
2011-06-05 21:30:19 +00:00
|
|
|
r_name_filter (string->string, sizeof (string->string));
|
2010-05-28 15:15:20 +00:00
|
|
|
printf ("f str.%s %"PFMT64d" @ 0x%08"PFMT64x"\n"
|
2010-08-15 19:30:59 +00:00
|
|
|
"Cs %"PFMT64d" @ 0x%08"PFMT64x"\n",
|
|
|
|
string->string, string->size, va?baddr+string->rva:string->offset,
|
|
|
|
string->size, va?baddr+string->rva:string->offset);
|
2011-05-25 13:43:54 +00:00
|
|
|
} else printf ("addr=0x%08"PFMT64x" off=0x%08"PFMT64x" ordinal=%03"PFMT64d" "
|
|
|
|
"sz=%"PFMT64d" section=%s string=%s\n",
|
2010-08-15 19:30:59 +00:00
|
|
|
baddr+string->rva, string->offset,
|
|
|
|
string->ordinal, string->size,
|
|
|
|
section?section->name:"unknown", string->string);
|
2010-05-28 15:15:20 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
if (!rad) eprintf ("\n%i strings\n", i);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-08-17 11:15:18 +00:00
|
|
|
static int rabin_show_sections() {
|
2010-05-28 15:15:20 +00:00
|
|
|
RList *sections;
|
|
|
|
RListIter *iter;
|
|
|
|
RBinSection *section;
|
|
|
|
ut64 baddr;
|
|
|
|
int i = 0;
|
|
|
|
|
2010-08-16 12:35:15 +00:00
|
|
|
baddr = gbaddr?gbaddr:r_bin_get_baddr (bin);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
if ((sections = r_bin_get_sections (bin)) == NULL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
if (!at) {
|
|
|
|
if (rad) printf ("fs sections\n");
|
2010-09-24 19:23:13 +00:00
|
|
|
else eprintf ("[Sections]\n");
|
2010-05-28 15:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r_list_foreach (sections, iter, section) {
|
2010-08-17 11:15:18 +00:00
|
|
|
if (name && strcmp (section->name, name))
|
|
|
|
continue;
|
2010-05-28 15:15:20 +00:00
|
|
|
if (at) {
|
|
|
|
if ((section->size != 0 &&
|
|
|
|
((baddr+section->rva <= at && baddr+section->rva+section->size > at) ||
|
|
|
|
(section->offset <= at && section->offset+section->size > at))) ||
|
|
|
|
baddr+section->rva == at || section->offset == at)
|
|
|
|
printf ("%s\n", section->name);
|
|
|
|
} else {
|
|
|
|
if (rad) {
|
2011-06-05 21:30:19 +00:00
|
|
|
r_name_filter (section->name, sizeof (section->name));
|
2010-05-28 15:15:20 +00:00
|
|
|
printf ("S 0x%08"PFMT64x" 0x%08"PFMT64x" 0x%08"PFMT64x" 0x%08"PFMT64x" %s %d\n",
|
2010-08-15 19:30:59 +00:00
|
|
|
section->offset, baddr+section->rva,
|
|
|
|
section->size, section->vsize, section->name, (int)section->srwx);
|
2010-05-28 15:15:20 +00:00
|
|
|
printf ("f section.%s %"PFMT64d" 0x%08"PFMT64x"\n",
|
2010-08-15 19:30:59 +00:00
|
|
|
section->name, section->size, va?baddr+section->rva:section->offset);
|
2010-05-28 15:15:20 +00:00
|
|
|
printf ("CC [%02i] va=0x%08"PFMT64x" pa=0x%08"PFMT64x" sz=%"PFMT64d" vsz=%"PFMT64d" "
|
2010-08-15 19:30:59 +00:00
|
|
|
"rwx=%c%c%c%c %s @ 0x%08"PFMT64x"\n",
|
|
|
|
i, baddr+section->rva, section->offset, section->size, section->vsize,
|
|
|
|
R_BIN_SCN_SHAREABLE (section->srwx)?'s':'-',
|
|
|
|
R_BIN_SCN_READABLE (section->srwx)?'r':'-',
|
|
|
|
R_BIN_SCN_WRITABLE (section->srwx)?'w':'-',
|
|
|
|
R_BIN_SCN_EXECUTABLE (section->srwx)?'x':'-',
|
|
|
|
section->name,va?baddr+section->rva:section->offset);
|
2011-05-25 13:43:54 +00:00
|
|
|
} else printf ("idx=%02i addr=0x%08"PFMT64x" off=0x%08"PFMT64x" sz=%"PFMT64d" vsz=%"PFMT64d" "
|
|
|
|
"perm=%c%c%c%c name=%s\n",
|
2010-08-15 19:30:59 +00:00
|
|
|
i, baddr+section->rva, section->offset, section->size, section->vsize,
|
|
|
|
R_BIN_SCN_SHAREABLE (section->srwx)?'s':'-',
|
|
|
|
R_BIN_SCN_READABLE (section->srwx)?'r':'-',
|
|
|
|
R_BIN_SCN_WRITABLE (section->srwx)?'w':'-',
|
|
|
|
R_BIN_SCN_EXECUTABLE (section->srwx)?'x':'-',
|
|
|
|
section->name);
|
2010-05-28 15:15:20 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
if (!at && !rad) eprintf ("\n%i sections\n", i);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rabin_show_info() {
|
|
|
|
RBinInfo *info;
|
|
|
|
|
|
|
|
if ((info = r_bin_get_info (bin)) == NULL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
if (rad) {
|
2011-05-12 07:52:40 +00:00
|
|
|
if (!strcmp (info->type, "fs")) {
|
|
|
|
printf ("e file.type=fs\n");
|
|
|
|
printf ("m %s /root 0\n", info->arch);
|
|
|
|
} else {
|
|
|
|
printf (
|
|
|
|
"e file.type=%s\n"
|
|
|
|
"e cfg.bigendian=%s\n"
|
|
|
|
"e asm.os=%s\n"
|
|
|
|
"e asm.arch=%s\n"
|
|
|
|
"e anal.plugin=%s\n"
|
|
|
|
"e asm.bits=%i\n"
|
|
|
|
"e asm.dwarf=%s\n",
|
2010-09-28 11:58:03 +00:00
|
|
|
info->rclass, info->big_endian?"true":"false", info->os,
|
2011-03-23 23:54:09 +00:00
|
|
|
info->arch, info->arch, info->bits,
|
2010-09-28 11:58:03 +00:00
|
|
|
R_BIN_DBG_STRIPPED (info->dbg_info)?"false":"true");
|
2011-05-12 07:52:40 +00:00
|
|
|
}
|
2010-09-24 19:23:13 +00:00
|
|
|
} else {
|
2011-05-12 07:52:40 +00:00
|
|
|
// if type is 'fs' show something different?
|
2010-09-24 19:23:13 +00:00
|
|
|
eprintf ("[File info]\n");
|
|
|
|
printf ("File=%s\n"
|
|
|
|
"Type=%s\n"
|
|
|
|
"Class=%s\n"
|
|
|
|
"Arch=%s %i\n"
|
|
|
|
"Machine=%s\n"
|
|
|
|
"OS=%s\n"
|
|
|
|
"Subsystem=%s\n"
|
|
|
|
"Big endian=%s\n"
|
|
|
|
"Stripped=%s\n"
|
|
|
|
"Static=%s\n"
|
|
|
|
"Line_nums=%s\n"
|
|
|
|
"Local_syms=%s\n"
|
|
|
|
"Relocs=%s\n"
|
|
|
|
"RPath=%s\n",
|
|
|
|
info->file, info->type, info->bclass,
|
|
|
|
info->arch, info->bits, info->machine, info->os,
|
|
|
|
info->subsystem, info->big_endian?"True":"False",
|
|
|
|
R_BIN_DBG_STRIPPED (info->dbg_info)?"True":"False",
|
|
|
|
R_BIN_DBG_STATIC (info->dbg_info)?"True":"False",
|
|
|
|
R_BIN_DBG_LINENUMS (info->dbg_info)?"True":"False",
|
|
|
|
R_BIN_DBG_SYMS (info->dbg_info)?"True":"False",
|
|
|
|
R_BIN_DBG_RELOCS (info->dbg_info)?"True":"False",
|
|
|
|
info->rpath);
|
|
|
|
}
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rabin_show_fields() {
|
|
|
|
RList *fields;
|
|
|
|
RListIter *iter;
|
|
|
|
RBinField *field;
|
|
|
|
ut64 baddr;
|
|
|
|
int i = 0;
|
|
|
|
|
2010-08-16 12:35:15 +00:00
|
|
|
baddr = gbaddr?gbaddr:r_bin_get_baddr (bin);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
if ((fields = r_bin_get_fields (bin)) == NULL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
if (rad) printf ("fs header\n");
|
2010-09-24 19:23:13 +00:00
|
|
|
else eprintf ("[Header fields]\n");
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
r_list_foreach (fields, iter, field) {
|
|
|
|
if (rad) {
|
2011-06-05 21:30:19 +00:00
|
|
|
r_name_filter (field->name, sizeof (field->name));
|
2010-05-28 15:15:20 +00:00
|
|
|
printf ("f header.%s @ 0x%08"PFMT64x"\n",
|
|
|
|
field->name, va?baddr+field->rva:field->offset);
|
2011-05-25 13:43:54 +00:00
|
|
|
printf ("[%02i] addr=0x%08"PFMT64x" off=0x%08"PFMT64x" name=%s\n",
|
2010-05-28 15:15:20 +00:00
|
|
|
i, baddr+field->rva, field->offset, field->name);
|
2011-05-25 13:43:54 +00:00
|
|
|
} else printf ("idx=%02i addr=0x%08"PFMT64x" off=0x%08"PFMT64x" name=%s\n",
|
2010-05-28 15:15:20 +00:00
|
|
|
i, baddr+field->rva, field->offset, field->name);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
if (!rad) eprintf ("\n%i fields\n", i);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rabin_dump_symbols(int len) {
|
|
|
|
RList *symbols;
|
|
|
|
RListIter *iter;
|
|
|
|
RBinSymbol *symbol;
|
|
|
|
ut8 *buf;
|
|
|
|
char *ret;
|
|
|
|
int olen = len;
|
|
|
|
|
|
|
|
if ((symbols = r_bin_get_symbols (bin)) == NULL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
r_list_foreach (symbols, iter, symbol) {
|
|
|
|
if (symbol->size != 0 && (olen > symbol->size || olen == 0))
|
|
|
|
len = symbol->size;
|
|
|
|
else if (symbol->size == 0 && olen == 0)
|
|
|
|
len = 32;
|
|
|
|
else len = olen;
|
|
|
|
|
|
|
|
if (!(buf = malloc (len)) || !(ret = malloc(len*2+1)))
|
|
|
|
return R_FALSE;
|
2010-10-04 01:46:58 +00:00
|
|
|
r_buf_read_at (bin->curarch.buf, symbol->offset, buf, len);
|
2010-05-28 15:15:20 +00:00
|
|
|
r_hex_bin2str (buf, len, ret);
|
|
|
|
printf ("%s %s\n", symbol->name, ret);
|
|
|
|
free (buf);
|
|
|
|
free (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-08-17 11:15:18 +00:00
|
|
|
static int rabin_dump_sections(char *scnname) {
|
2010-05-28 15:15:20 +00:00
|
|
|
RList *sections;
|
|
|
|
RListIter *iter;
|
|
|
|
RBinSection *section;
|
|
|
|
ut8 *buf;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if ((sections = r_bin_get_sections (bin)) == NULL)
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
r_list_foreach (sections, iter, section) {
|
2010-08-17 11:15:18 +00:00
|
|
|
if (!strcmp (scnname, section->name)) {
|
2010-05-28 15:15:20 +00:00
|
|
|
if (!(buf = malloc (section->size)) ||
|
|
|
|
!(ret = malloc (section->size*2+1)))
|
|
|
|
return R_FALSE;
|
2010-10-04 01:46:58 +00:00
|
|
|
r_buf_read_at (bin->curarch.buf, section->offset, buf, section->size);
|
2010-05-28 15:15:20 +00:00
|
|
|
r_hex_bin2str (buf, section->size, ret);
|
|
|
|
printf ("%s\n", ret);
|
|
|
|
free (buf);
|
|
|
|
free (ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rabin_do_operation(const char *op) {
|
|
|
|
char *arg = NULL, *ptr = NULL, *ptr2 = NULL;
|
|
|
|
|
|
|
|
if (!strcmp (op, "help")) {
|
|
|
|
printf ("Operation string:\n"
|
|
|
|
" Dump symbols: d/s/1024\n"
|
|
|
|
" Dump section: d/S/.text\n"
|
|
|
|
" Resize section: r/.data/1024\n");
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
/* Implement alloca with fixed-size buffer? */
|
|
|
|
if (!(arg = strdup (op)))
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
if ((ptr = strchr (arg, '/'))) {
|
|
|
|
ptr[0] = '\0';
|
|
|
|
ptr = ptr + 1;
|
|
|
|
if ((ptr2 = strchr (ptr, '/'))) {
|
|
|
|
ptr2[0] = '\0';
|
|
|
|
ptr2 = ptr2 + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (arg[0]) {
|
|
|
|
case 'd':
|
|
|
|
if (!ptr)
|
|
|
|
goto _rabin_do_operation_error;
|
2011-07-17 22:07:45 +00:00
|
|
|
switch (*ptr) {
|
|
|
|
case 's':
|
2010-05-28 15:15:20 +00:00
|
|
|
if (ptr2) {
|
|
|
|
if (!rabin_dump_symbols (r_num_math(NULL, ptr2)))
|
|
|
|
return R_FALSE;
|
2011-05-24 13:25:52 +00:00
|
|
|
} else if (!rabin_dump_symbols (0))
|
2010-05-28 15:15:20 +00:00
|
|
|
return R_FALSE;
|
2011-07-17 22:07:45 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
2010-05-28 15:15:20 +00:00
|
|
|
if (!ptr2)
|
|
|
|
goto _rabin_do_operation_error;
|
|
|
|
if (!rabin_dump_sections (ptr2))
|
|
|
|
return R_FALSE;
|
2011-07-17 22:07:45 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto _rabin_do_operation_error;
|
|
|
|
}
|
2010-05-28 15:15:20 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
r_bin_wr_scn_resize (bin, ptr, r_num_math (NULL, ptr2));
|
|
|
|
r_bin_wr_output (bin, output);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_rabin_do_operation_error:
|
2010-09-24 19:23:13 +00:00
|
|
|
eprintf ("Unknown operation. use -O help\n");
|
2010-05-28 15:15:20 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (arg);
|
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-08-17 11:15:18 +00:00
|
|
|
static int rabin_show_srcline() {
|
2010-05-28 15:15:20 +00:00
|
|
|
char *srcline;
|
|
|
|
if ((srcline = r_bin_meta_get_source_line (bin, at))) {
|
|
|
|
printf ("%s\n", srcline);
|
|
|
|
free (srcline);
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bin callback */
|
|
|
|
static int __lib_bin_cb(struct r_lib_plugin_t *pl, void *user, void *data) {
|
|
|
|
struct r_bin_plugin_t *hand = (struct r_bin_plugin_t *)data;
|
|
|
|
//printf(" * Added (dis)assembly plugin\n");
|
|
|
|
r_bin_add (bin, hand);
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __lib_bin_dt(struct r_lib_plugin_t *pl, void *p, void *u) {
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
/* binxtr callback */
|
|
|
|
static int __lib_bin_xtr_cb(struct r_lib_plugin_t *pl, void *user, void *data) {
|
|
|
|
struct r_bin_xtr_plugin_t *hand = (struct r_bin_xtr_plugin_t *)data;
|
|
|
|
//printf(" * Added (dis)assembly plugin\n");
|
|
|
|
r_bin_xtr_add (bin, hand);
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __lib_bin_xtr_dt(struct r_lib_plugin_t *pl, void *p, void *u) {
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-10-03 22:42:11 +00:00
|
|
|
int main(int argc, char **argv) {
|
2010-10-01 08:09:50 +00:00
|
|
|
int c, bits = 0;
|
2010-05-28 15:15:20 +00:00
|
|
|
int action = ACTION_UNK;
|
2010-09-25 01:45:03 +00:00
|
|
|
const char *op = NULL;
|
2010-10-04 01:46:58 +00:00
|
|
|
char *arch = NULL, *arch_name = NULL;
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
bin = r_bin_new ();
|
|
|
|
l = r_lib_new ("radare_plugin");
|
|
|
|
r_lib_add_handler (l, R_LIB_TYPE_BIN, "bin plugins",
|
|
|
|
&__lib_bin_cb, &__lib_bin_dt, NULL);
|
2010-09-24 19:23:13 +00:00
|
|
|
r_lib_add_handler (l, R_LIB_TYPE_BIN_XTR, "bin xtr plugins",
|
|
|
|
&__lib_bin_xtr_cb, &__lib_bin_xtr_dt, NULL);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
{ /* load plugins everywhere */
|
|
|
|
char *homeplugindir = r_str_home (".radare/plugins");
|
|
|
|
r_lib_opendir (l, getenv ("LIBR_PLUGINS"));
|
|
|
|
r_lib_opendir (l, homeplugindir);
|
|
|
|
r_lib_opendir (l, LIBDIR"/radare2/");
|
|
|
|
}
|
|
|
|
|
2011-07-25 19:10:25 +00:00
|
|
|
while ((c = getopt (argc, argv, "Af:a:B:b:c:Mm:n:@:VisSzIHelRwO:o:rvLhx")) != -1) {
|
2010-05-28 15:15:20 +00:00
|
|
|
switch(c) {
|
2010-09-24 19:23:13 +00:00
|
|
|
case 'A':
|
|
|
|
action |= ACTION_LISTARCHS;
|
|
|
|
break;
|
|
|
|
case 'a':
|
2010-09-25 01:45:03 +00:00
|
|
|
if (optarg) arch = strdup (optarg);
|
2010-09-24 19:23:13 +00:00
|
|
|
break;
|
2011-07-25 19:10:25 +00:00
|
|
|
case 'c':
|
|
|
|
action = ACTION_CREATE;
|
|
|
|
create = strdup (optarg);
|
|
|
|
break;
|
2010-10-04 01:46:58 +00:00
|
|
|
case 'f':
|
|
|
|
if (optarg) arch_name = strdup (optarg);
|
|
|
|
break;
|
2010-09-24 19:23:13 +00:00
|
|
|
case 'B':
|
|
|
|
bits = r_num_math (NULL, optarg);
|
|
|
|
break;
|
2010-05-28 15:15:20 +00:00
|
|
|
case 'm':
|
|
|
|
at = r_num_math (NULL, optarg);
|
|
|
|
action |= ACTION_SRCLINE;
|
2010-09-24 19:23:13 +00:00
|
|
|
break;
|
2010-05-28 15:15:20 +00:00
|
|
|
case 'i':
|
|
|
|
action |= ACTION_IMPORTS;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
action |= ACTION_SYMBOLS;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
action |= ACTION_SECTIONS;
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
action |= ACTION_STRINGS;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
action |= ACTION_INFO;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
action |= ACTION_FIELDS;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
action |= ACTION_ENTRIES;
|
|
|
|
break;
|
2010-05-30 04:06:25 +00:00
|
|
|
case 'M':
|
|
|
|
action |= ACTION_MAIN;
|
|
|
|
break;
|
2010-05-28 15:15:20 +00:00
|
|
|
case 'l':
|
|
|
|
action |= ACTION_LIBS;
|
|
|
|
break;
|
2010-09-10 09:11:38 +00:00
|
|
|
case 'R':
|
|
|
|
action |= ACTION_RELOCS;
|
|
|
|
break;
|
2010-07-29 14:04:18 +00:00
|
|
|
case 'x':
|
|
|
|
action |= ACTION_EXTRACT;
|
|
|
|
break;
|
2010-05-28 15:15:20 +00:00
|
|
|
case 'w':
|
|
|
|
rw = R_TRUE;
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
op = optarg;
|
|
|
|
action |= ACTION_OPERATION;
|
2011-07-17 22:07:45 +00:00
|
|
|
if (optind==argc)
|
|
|
|
return rabin_do_operation (op);
|
2010-05-28 15:15:20 +00:00
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
output = optarg;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rad = R_TRUE;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
va = R_TRUE;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
r_bin_list (bin);
|
2010-09-24 19:23:13 +00:00
|
|
|
return 1;
|
2010-08-16 12:35:15 +00:00
|
|
|
case 'b':
|
|
|
|
gbaddr = r_num_math (NULL, optarg);
|
|
|
|
break;
|
2010-05-28 15:15:20 +00:00
|
|
|
case '@':
|
|
|
|
at = r_num_math (NULL, optarg);
|
|
|
|
break;
|
2010-08-17 11:15:18 +00:00
|
|
|
case 'n':
|
|
|
|
name = optarg;
|
|
|
|
break;
|
2010-05-28 15:15:20 +00:00
|
|
|
case 'V':
|
2010-07-15 11:34:53 +00:00
|
|
|
printf ("rabin2 v"R2_VERSION"\n");
|
2010-05-28 15:15:20 +00:00
|
|
|
return 0;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
action |= ACTION_HELP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file = argv[optind];
|
|
|
|
if (action == ACTION_HELP || action == ACTION_UNK || file == NULL)
|
|
|
|
return rabin_show_help ();
|
|
|
|
|
2010-09-25 01:45:03 +00:00
|
|
|
if (arch) {
|
|
|
|
char *ptr;
|
|
|
|
ptr = strchr (arch, '_');
|
|
|
|
if (ptr) {
|
|
|
|
*ptr = '\0';
|
|
|
|
bits = r_num_math (NULL, ptr+1);
|
|
|
|
}
|
|
|
|
}
|
2011-07-25 19:10:25 +00:00
|
|
|
if (action & ACTION_CREATE) {
|
2011-07-25 21:42:39 +00:00
|
|
|
// TODO: move in a function outside
|
2011-07-25 19:10:25 +00:00
|
|
|
RBuffer *b;
|
|
|
|
int datalen, codelen;
|
|
|
|
ut8 *data = NULL, *code = NULL;
|
|
|
|
char *p2, *p = strchr (create, ':');
|
|
|
|
if (!p) {
|
|
|
|
eprintf ("Invalid format for -c flag. Use 'format:codehexpair:datahexpair'\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*p++ = 0;
|
|
|
|
p2 = strchr (p, ':');
|
|
|
|
if (p2) {
|
|
|
|
// has data
|
|
|
|
*p2++ = 0;
|
|
|
|
data = malloc (strlen (p2));
|
|
|
|
datalen = r_hex_str2bin (p2, data);
|
|
|
|
}
|
|
|
|
code = malloc (strlen (p));
|
|
|
|
codelen = r_hex_str2bin (p, code);
|
|
|
|
if (!arch) arch = "x86";
|
|
|
|
if (!bits) bits = 32;
|
|
|
|
|
|
|
|
if (!r_bin_use_arch (bin, arch, bits, create)) {
|
|
|
|
eprintf ("Cannot set arch\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
b = r_bin_create (bin, code, codelen, data, datalen);
|
|
|
|
if (b) {
|
2011-07-25 21:42:39 +00:00
|
|
|
if (r_file_dump (file, b->buf, b->length)) {
|
2011-07-25 19:10:25 +00:00
|
|
|
eprintf ("dumped %d bytes in '%s'\n", b->length, file);
|
2011-07-25 21:42:39 +00:00
|
|
|
r_file_chmod (file, "+x", 0);
|
|
|
|
} else eprintf ("error dumping into a.out\n");
|
2011-07-25 19:10:25 +00:00
|
|
|
r_buf_free (b);
|
|
|
|
} else eprintf ("Cannot create binary for this format '%s'.\n", create);
|
|
|
|
r_bin_free (bin);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!r_bin_load (bin, file, R_FALSE) && !r_bin_load (bin, file, R_TRUE)) {
|
|
|
|
eprintf ("r_bin: Cannot open '%s'\n", file);
|
|
|
|
return 1;
|
|
|
|
}
|
2011-05-24 13:25:52 +00:00
|
|
|
if (action & ACTION_LISTARCHS && (arch || bits || arch_name)) {
|
2011-07-25 19:10:25 +00:00
|
|
|
if (!r_bin_select (bin, arch, bits, arch_name)) {
|
2011-05-24 13:25:52 +00:00
|
|
|
r_bin_list_archs (bin);
|
|
|
|
free (arch);
|
|
|
|
r_bin_free (bin);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-05-28 15:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (action&ACTION_SECTIONS)
|
|
|
|
rabin_show_sections (at);
|
|
|
|
if (action&ACTION_ENTRIES)
|
|
|
|
rabin_show_entrypoints ();
|
2010-05-30 04:06:25 +00:00
|
|
|
if (action&ACTION_MAIN)
|
|
|
|
rabin_show_main ();
|
2010-05-28 15:15:20 +00:00
|
|
|
if (action&ACTION_IMPORTS)
|
|
|
|
rabin_show_imports (at);
|
|
|
|
if (action&ACTION_SYMBOLS)
|
|
|
|
rabin_show_symbols (at);
|
|
|
|
if (action&ACTION_STRINGS)
|
|
|
|
rabin_show_strings ();
|
|
|
|
if (action&ACTION_INFO)
|
|
|
|
rabin_show_info ();
|
|
|
|
if (action&ACTION_FIELDS)
|
|
|
|
rabin_show_fields();
|
|
|
|
if (action&ACTION_LIBS)
|
|
|
|
rabin_show_libs();
|
2010-09-10 09:11:38 +00:00
|
|
|
if (action&ACTION_RELOCS)
|
|
|
|
rabin_show_relocs();
|
2010-05-28 15:15:20 +00:00
|
|
|
if (action&ACTION_SRCLINE)
|
|
|
|
rabin_show_srcline(at);
|
2010-07-29 14:04:18 +00:00
|
|
|
if (action&ACTION_EXTRACT)
|
2011-05-24 13:25:52 +00:00
|
|
|
rabin_extract ((arch==NULL && arch_name==NULL && bits==0));
|
2010-05-28 15:15:20 +00:00
|
|
|
if (op != NULL && action&ACTION_OPERATION)
|
|
|
|
rabin_do_operation (op);
|
|
|
|
|
2010-09-25 01:45:03 +00:00
|
|
|
free (arch);
|
2010-05-28 15:15:20 +00:00
|
|
|
r_bin_free (bin);
|
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
return 0;
|
2010-05-28 15:15:20 +00:00
|
|
|
}
|