2015-01-10 00:00:01 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2015 - nibble, pancake */
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-06-01 12:50:24 +00:00
|
|
|
#include <getopt.c>
|
2011-11-12 03:20:22 +00:00
|
|
|
#include <r_core.h>
|
2013-04-04 00:32:13 +00:00
|
|
|
#include "../blob/version.c"
|
2014-12-11 14:33:32 +00:00
|
|
|
#include "../../libr/bin/pdb/pdb_downloader.h"
|
2010-05-28 15:15:20 +00:00
|
|
|
|
2011-09-04 01:56:35 +00:00
|
|
|
#define ACTION_UNK 0x00000
|
|
|
|
#define ACTION_ENTRIES 0x00001
|
|
|
|
#define ACTION_IMPORTS 0x00002
|
|
|
|
#define ACTION_SYMBOLS 0x00004
|
|
|
|
#define ACTION_SECTIONS 0x00008
|
|
|
|
#define ACTION_INFO 0x00010
|
|
|
|
#define ACTION_OPERATION 0x00020
|
|
|
|
#define ACTION_HELP 0x00040
|
|
|
|
#define ACTION_STRINGS 0x00080
|
|
|
|
#define ACTION_FIELDS 0x00100
|
|
|
|
#define ACTION_LIBS 0x00200
|
|
|
|
#define ACTION_SRCLINE 0x00400
|
|
|
|
#define ACTION_MAIN 0x00800
|
|
|
|
#define ACTION_EXTRACT 0x01000
|
|
|
|
#define ACTION_RELOCS 0x02000
|
|
|
|
#define ACTION_LISTARCHS 0x04000
|
|
|
|
#define ACTION_CREATE 0x08000
|
2011-10-05 00:38:37 +00:00
|
|
|
#define ACTION_CLASSES 0x10000
|
2012-06-27 23:27:40 +00:00
|
|
|
#define ACTION_DWARF 0x20000
|
2012-08-23 10:46:55 +00:00
|
|
|
#define ACTION_SIZE 0x40000
|
2014-11-20 20:32:32 +00:00
|
|
|
#define ACTION_PDB 0x80000
|
2014-12-11 14:33:32 +00:00
|
|
|
#define ACTION_PDB_DWNLD 0x100000
|
2015-04-18 19:57:24 +00:00
|
|
|
#define ACTION_DLOPEN 0x200000
|
2010-05-28 15:15:20 +00:00
|
|
|
|
|
|
|
static struct r_bin_t *bin = NULL;
|
2012-11-02 02:35:50 +00:00
|
|
|
static char* output = NULL;
|
|
|
|
static char* create = NULL;
|
2010-05-28 15:15:20 +00:00
|
|
|
static int rad = R_FALSE;
|
2014-07-29 22:33:54 +00:00
|
|
|
static ut64 laddr = 0LL;
|
2015-01-29 00:45:39 +00:00
|
|
|
static ut64 baddr = 0LL;
|
2010-05-28 15:15:20 +00:00
|
|
|
static char* file = NULL;
|
2010-08-17 11:15:18 +00:00
|
|
|
static char *name = NULL;
|
2012-11-02 02:35:50 +00:00
|
|
|
static int rw = R_FALSE;
|
2014-08-23 00:40:24 +00:00
|
|
|
static int va = R_TRUE;
|
2015-01-10 00:00:01 +00:00
|
|
|
static const char *do_demangle = NULL;
|
2012-11-02 02:35:50 +00:00
|
|
|
static ut64 at = 0LL;
|
|
|
|
static RLib *l;
|
2010-05-28 15:15:20 +00:00
|
|
|
|
2013-04-02 10:11:20 +00:00
|
|
|
static int rabin_show_help(int v) {
|
|
|
|
printf ("Usage: rabin2 [-ACdehHiIjlLMqrRsSvVxzZ] [-@ addr] [-a arch] [-b bits]\n"
|
2014-10-21 02:39:37 +00:00
|
|
|
" [-B addr] [-c F:C:D] [-f str] [-m addr] [-n str] [-N m:M]\n"
|
2015-01-10 00:00:01 +00:00
|
|
|
" [-o str] [-O str] [-k query] [-D lang symname] | file>\n");
|
2013-04-02 10:11:20 +00:00
|
|
|
if (v) printf (
|
2012-12-09 00:39:27 +00:00
|
|
|
" -@ [addr] show section, symbol or import at addr\n"
|
2011-07-25 19:10:25 +00:00
|
|
|
" -A list archs\n"
|
2012-08-04 21:48:06 +00:00
|
|
|
" -a [arch] set arch (x86, arm, .. or <arch>_<bits>)\n"
|
2011-11-21 23:59:20 +00:00
|
|
|
" -b [bits] set bits (32, 64 ...)\n"
|
2013-11-06 00:36:40 +00:00
|
|
|
" -B [addr] override base address (pie bins)\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-10-24 02:35:42 +00:00
|
|
|
" -C list classes\n"
|
2012-07-12 00:47:04 +00:00
|
|
|
" -d show debug/dwarf information\n"
|
2015-01-12 19:55:22 +00:00
|
|
|
" -D lang name demangle symbol name\n"
|
2011-07-25 19:10:25 +00:00
|
|
|
" -e entrypoint\n"
|
2015-04-18 19:57:24 +00:00
|
|
|
" -E show loading offset (useful for non-ASLR libraries)"
|
2011-07-25 19:10:25 +00:00
|
|
|
" -f [str] select sub-bin named str\n"
|
2015-01-11 18:40:26 +00:00
|
|
|
" -F [binfmt] force to use that bin plugin (ignore header check)\n"
|
2014-05-18 23:42:36 +00:00
|
|
|
" -k [query] perform sdb query on loaded file\n"
|
2014-08-15 01:30:45 +00:00
|
|
|
" -K [algo] calculate checksums (md5, sha1, ..)\n"
|
2015-03-08 17:07:27 +00:00
|
|
|
" -g same as -SMResiz (show all info)\n"
|
2015-04-18 19:57:24 +00:00
|
|
|
" -G [addr] load address . offset to header\n"
|
2012-12-09 00:39:27 +00:00
|
|
|
" -h this help\n"
|
|
|
|
" -H header fields\n"
|
2011-07-25 19:10:25 +00:00
|
|
|
" -i imports (symbols imported from libraries)\n"
|
|
|
|
" -I binary info\n"
|
2012-12-09 00:39:27 +00:00
|
|
|
" -j output in json\n"
|
2014-12-20 23:54:42 +00:00
|
|
|
" -k [sdb-query] run sdb query. for example: '*'\n"
|
2011-07-25 19:10:25 +00:00
|
|
|
" -l linked libraries\n"
|
|
|
|
" -L list supported bin plugins\n"
|
2012-12-09 00:39:27 +00:00
|
|
|
" -m [addr] show source line at addr\n"
|
|
|
|
" -M main (show address of main symbol)\n"
|
2011-07-25 19:10:25 +00:00
|
|
|
" -n [str] show section, symbol or import named str\n"
|
2014-10-21 02:39:37 +00:00
|
|
|
" -N [min:max] force min:max number of chars per string (see -z and -zz)\n"
|
2012-12-09 00:39:27 +00:00
|
|
|
" -o [str] output file/folder for write operations (out by default)\n"
|
|
|
|
" -O [str] write/extract operations (-O help)\n"
|
2014-08-24 08:41:32 +00:00
|
|
|
" -p show physical addresses\n"
|
2015-01-11 18:40:26 +00:00
|
|
|
" -P show debug/pdb information\n"
|
|
|
|
" -PP download pdb file for binary\n"
|
2012-11-05 01:00:34 +00:00
|
|
|
" -q be quiet, just show fewer data\n"
|
2012-12-09 00:39:27 +00:00
|
|
|
" -r radare output\n"
|
|
|
|
" -R relocations\n"
|
|
|
|
" -s symbols (exports)\n"
|
|
|
|
" -S sections\n"
|
2014-08-23 00:40:24 +00:00
|
|
|
" -v display version and quit\n"
|
2011-07-25 19:10:25 +00:00
|
|
|
" -x extract bins contained in file\n"
|
2012-12-26 01:01:26 +00:00
|
|
|
" -z strings (from data section)\n"
|
|
|
|
" -zz strings (from raw bins [e bin.rawstr=1])\n"
|
2014-10-24 16:25:27 +00:00
|
|
|
" -zzz dump raw strings to stdout (for huge files)\n"
|
2013-04-02 10:11:20 +00:00
|
|
|
" -Z guess size of binary program\n"
|
2012-12-09 00:39:27 +00:00
|
|
|
);
|
2010-09-24 19:23:13 +00:00
|
|
|
return 1;
|
2010-05-28 15:15:20 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 22:12:30 +00:00
|
|
|
static char *stdin_gets() {
|
|
|
|
static char buf[96096];
|
|
|
|
fgets (buf, sizeof (buf)-1, stdin);
|
|
|
|
if (feof (stdin)) return NULL;
|
|
|
|
buf[strlen (buf)-1] = 0;
|
|
|
|
return strdup (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __sdb_prompt(Sdb *sdb) {
|
|
|
|
char *line;
|
|
|
|
for (;(line = stdin_gets ());) {
|
|
|
|
sdb_query (sdb, line);
|
|
|
|
free (line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-08 16:29:53 +00:00
|
|
|
static int extract_binobj (const RBinFile *bf, const RBinObject *o, int idx) {
|
2014-05-08 23:35:04 +00:00
|
|
|
ut64 boffset = o ? o->boffset : 0;
|
|
|
|
ut64 bin_size = o ? o->obj_size : 0;
|
|
|
|
const ut8 *bytes = bf ? r_buf_buffer (bf->buf) : NULL;
|
2014-05-17 00:37:29 +00:00
|
|
|
//ut64 sz = bf ? r_buf_size (bf->buf) : 0;
|
2014-05-08 23:35:04 +00:00
|
|
|
RBinInfo *info = o ? o->info : NULL;
|
|
|
|
const char *arch = info ? info->arch : "unknown";
|
|
|
|
char bits = info ? info->bits : 0;
|
|
|
|
const char *filename = bf ? bf->file : NULL;
|
|
|
|
char *path = NULL, *outpath = NULL, *outfile = NULL, *ptr = NULL;
|
|
|
|
ut32 outfile_sz = 0, outpath_sz = 0;
|
|
|
|
int res = R_FALSE;
|
|
|
|
|
|
|
|
if (!bf || !o || !filename ) return R_FALSE;
|
|
|
|
|
|
|
|
path = strdup (filename);
|
|
|
|
|
|
|
|
// XXX: Wrong for w32 (/)
|
|
|
|
|
2015-03-08 16:29:53 +00:00
|
|
|
ptr = strrchr (path, DIRSEP);
|
|
|
|
if (ptr) {
|
|
|
|
*ptr++ = '\0';
|
|
|
|
} else {
|
|
|
|
ptr = path;
|
|
|
|
}
|
2014-05-08 23:35:04 +00:00
|
|
|
|
|
|
|
outpath_sz = strlen (path) + 20;
|
|
|
|
|
2015-03-08 16:29:53 +00:00
|
|
|
if (outpath_sz > 0)
|
2014-05-08 23:35:04 +00:00
|
|
|
outpath = malloc (outpath_sz);
|
|
|
|
|
|
|
|
if (outpath)
|
|
|
|
snprintf (outpath, outpath_sz, "%s.fat", ptr);
|
|
|
|
|
|
|
|
if (!outpath || !r_sys_rmkdir (outpath)) {
|
|
|
|
free (path);
|
|
|
|
free (outpath);
|
|
|
|
eprintf ("Error creating dir structure\n");
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
2015-03-08 16:29:53 +00:00
|
|
|
outfile_sz = outpath_sz + strlen (ptr) + strlen (arch) + 23;
|
2014-05-08 23:35:04 +00:00
|
|
|
if (outfile_sz)
|
|
|
|
outfile = malloc (outfile_sz);
|
|
|
|
|
|
|
|
if (outfile)
|
|
|
|
snprintf (outfile, outfile_sz, "%s/%s.%s_%i.%d",
|
|
|
|
outpath, ptr, arch, bits, idx);
|
2010-10-01 06:12:43 +00:00
|
|
|
|
2014-09-26 15:30:20 +00:00
|
|
|
if (boffset > r_buf_size (bf->buf)) {
|
2015-03-08 16:29:53 +00:00
|
|
|
eprintf ("Invalid offsets\n");
|
|
|
|
res = R_FALSE;
|
2014-05-08 23:35:04 +00:00
|
|
|
} else {
|
2015-04-03 02:04:46 +00:00
|
|
|
if (!outfile || !r_file_dump (outfile, bytes+boffset, bin_size, 0)) {
|
2014-09-26 15:30:20 +00:00
|
|
|
eprintf ("Error extracting %s\n", outfile);
|
|
|
|
res = R_FALSE;
|
|
|
|
} else {
|
|
|
|
printf ("%s created (%"PFMT64d")\n", outfile, bin_size);
|
|
|
|
res = R_TRUE;
|
|
|
|
}
|
2014-05-08 23:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free (outfile);
|
|
|
|
free (outpath);
|
|
|
|
free (path);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rabin_extract(int all) {
|
2015-03-08 16:29:53 +00:00
|
|
|
RBinObject *obj = NULL;
|
2014-05-08 23:35:04 +00:00
|
|
|
int res = R_FALSE;
|
|
|
|
RBinFile *bf = r_bin_cur (bin);
|
2014-05-25 13:59:52 +00:00
|
|
|
if (!bf) return res;
|
2010-09-25 10:33:30 +00:00
|
|
|
if (all) {
|
2014-05-08 23:35:04 +00:00
|
|
|
int idx = 0;
|
|
|
|
RListIter *iter = NULL;
|
2014-05-25 13:59:52 +00:00
|
|
|
r_list_foreach (bf->objs, iter, obj)
|
2014-05-08 23:35:04 +00:00
|
|
|
res = extract_binobj (bf, obj, idx++);
|
|
|
|
} else {
|
|
|
|
obj = r_bin_cur_object (bin);
|
2014-05-25 13:59:52 +00:00
|
|
|
if (!obj) return res;
|
2014-05-08 23:35:04 +00:00
|
|
|
res = extract_binobj (bf, obj, 0);
|
2010-09-25 10:33:30 +00:00
|
|
|
}
|
2014-05-08 23:35:04 +00:00
|
|
|
return res;
|
2010-07-29 14:04:18 +00:00
|
|
|
}
|
|
|
|
|
2010-05-28 15:15:20 +00:00
|
|
|
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;
|
2014-01-16 23:10:09 +00:00
|
|
|
if (!(buf = malloc (len))) {
|
2010-05-28 15:15:20 +00:00
|
|
|
return R_FALSE;
|
2014-01-16 23:10:09 +00:00
|
|
|
}
|
|
|
|
if (!(ret = malloc (len*2+1))) {
|
|
|
|
free (buf);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
2014-05-21 01:18:00 +00:00
|
|
|
r_buf_read_at (bin->cur->buf, symbol->paddr, 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)) {
|
2014-04-30 19:18:43 +00:00
|
|
|
if (!(buf = malloc (section->size)))
|
2010-05-28 15:15:20 +00:00
|
|
|
return R_FALSE;
|
2014-04-30 19:18:43 +00:00
|
|
|
if (!(ret = malloc (section->size*2+1))) {
|
|
|
|
free (buf);
|
|
|
|
return R_FALSE;
|
|
|
|
}
|
2014-05-21 01:18:00 +00:00
|
|
|
r_buf_read_at (bin->cur->buf, section->paddr, buf, section->size);
|
2011-11-30 19:59:58 +00:00
|
|
|
if (output) {
|
2015-04-03 02:04:46 +00:00
|
|
|
r_file_dump (output, buf, section->size, 0);
|
2011-11-30 19:59:58 +00:00
|
|
|
} else {
|
|
|
|
r_hex_bin2str (buf, section->size, ret);
|
|
|
|
printf ("%s\n", ret);
|
|
|
|
}
|
2010-05-28 15:15:20 +00:00
|
|
|
free (buf);
|
|
|
|
free (ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rabin_do_operation(const char *op) {
|
|
|
|
char *arg = NULL, *ptr = NULL, *ptr2 = NULL;
|
|
|
|
|
|
|
|
/* Implement alloca with fixed-size buffer? */
|
|
|
|
if (!(arg = strdup (op)))
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
if ((ptr = strchr (arg, '/'))) {
|
|
|
|
ptr[0] = '\0';
|
2012-01-26 02:18:45 +00:00
|
|
|
ptr++;
|
2010-05-28 15:15:20 +00:00
|
|
|
if ((ptr2 = strchr (ptr, '/'))) {
|
|
|
|
ptr2[0] = '\0';
|
2011-11-30 19:59:58 +00:00
|
|
|
ptr2++;
|
2010-05-28 15:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2011-11-30 19:59:58 +00:00
|
|
|
if (!rabin_dump_symbols (r_num_math (NULL, ptr2)))
|
2010-05-28 15:15:20 +00:00
|
|
|
return R_FALSE;
|
2014-05-01 09:41:20 +00:00
|
|
|
} else if (!rabin_dump_symbols (0)) {
|
|
|
|
free (arg);
|
|
|
|
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;
|
2014-05-01 09:41:20 +00:00
|
|
|
if (!rabin_dump_sections (ptr2)) {
|
|
|
|
free (arg);
|
2010-05-28 15:15:20 +00:00
|
|
|
return R_FALSE;
|
2014-05-01 09:41:20 +00:00
|
|
|
}
|
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));
|
2011-11-30 19:59:58 +00:00
|
|
|
if (!output) output = "out";
|
2010-05-28 15:15:20 +00:00
|
|
|
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");
|
2014-05-19 02:05:15 +00:00
|
|
|
free (arg);
|
2010-05-28 15:15:20 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (arg);
|
|
|
|
return R_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-10-09 23:48:08 +00:00
|
|
|
static int rabin_show_srcline(ut64 at) {
|
2010-05-28 15:15:20 +00:00
|
|
|
char *srcline;
|
2014-03-10 01:27:32 +00:00
|
|
|
if ((srcline = r_bin_addr2text (bin, at))) {
|
2010-05-28 15:15:20 +00:00
|
|
|
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) {
|
2014-05-18 23:42:36 +00:00
|
|
|
const char *query = NULL;
|
2013-07-01 15:05:15 +00:00
|
|
|
int c, bits = 0, actions_done = 0, actions = 0, action = ACTION_UNK;
|
|
|
|
char *homeplugindir = r_str_home (R2_HOMEDIR"/plugins");
|
2013-04-02 10:11:20 +00:00
|
|
|
char *ptr, *arch = NULL, *arch_name = NULL;
|
2012-11-02 02:35:50 +00:00
|
|
|
const char *op = NULL;
|
2014-08-15 01:30:45 +00:00
|
|
|
const char *chksum = NULL;
|
2015-01-11 18:40:26 +00:00
|
|
|
const char *forcebin = NULL;
|
2013-04-02 10:11:20 +00:00
|
|
|
RCoreBinFilter filter;
|
2012-11-02 02:35:50 +00:00
|
|
|
RCore core;
|
2014-04-27 07:06:50 +00:00
|
|
|
RCoreFile *cf = NULL;
|
|
|
|
int xtr_idx = 0; // load all files if extraction is necessary.
|
|
|
|
int fd = -1;
|
2014-05-19 00:41:53 +00:00
|
|
|
int rawstr = 0;
|
2010-05-28 15:15:20 +00:00
|
|
|
|
2012-12-26 01:01:26 +00:00
|
|
|
r_core_init (&core);
|
2013-04-02 10:11:20 +00:00
|
|
|
bin = core.bin;
|
2010-05-28 15:15:20 +00:00
|
|
|
l = r_lib_new ("radare_plugin");
|
|
|
|
r_lib_add_handler (l, R_LIB_TYPE_BIN, "bin plugins",
|
2014-02-05 23:26:17 +00:00
|
|
|
&__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",
|
2014-02-05 23:26:17 +00:00
|
|
|
&__lib_bin_xtr_cb, &__lib_bin_xtr_dt, NULL);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
2013-04-02 10:11:20 +00:00
|
|
|
/* load plugins everywhere */
|
2012-11-02 02:35:50 +00:00
|
|
|
r_lib_opendir (l, getenv ("LIBR_PLUGINS"));
|
|
|
|
r_lib_opendir (l, homeplugindir);
|
2014-02-05 23:26:17 +00:00
|
|
|
r_lib_opendir (l, LIBDIR"/radare2/"R2_VERSION);
|
2012-11-02 02:35:50 +00:00
|
|
|
|
2012-12-26 01:01:26 +00:00
|
|
|
#define is_active(x) (action&x)
|
2014-10-24 16:25:27 +00:00
|
|
|
#define set_action(x) actions++; action |= x
|
|
|
|
#define unset_action(x) action &= ~x
|
2015-04-18 19:57:24 +00:00
|
|
|
while ((c = getopt (argc, argv, "DjgqAf:F:a:B:G:b:c:Ck:K:dD:Mm:n:N:@:isSIHeElRwO:o:pPrvLhxzZ")) != -1) {
|
2012-11-02 02:35:50 +00:00
|
|
|
switch (c) {
|
2013-11-09 02:11:03 +00:00
|
|
|
case 'g':
|
|
|
|
set_action (ACTION_CLASSES);
|
2014-04-26 22:31:04 +00:00
|
|
|
set_action (ACTION_IMPORTS);
|
|
|
|
set_action (ACTION_SYMBOLS);
|
|
|
|
set_action (ACTION_SECTIONS);
|
|
|
|
set_action (ACTION_STRINGS);
|
|
|
|
set_action (ACTION_SIZE);
|
|
|
|
set_action (ACTION_INFO);
|
|
|
|
set_action (ACTION_FIELDS);
|
|
|
|
set_action (ACTION_DWARF);
|
|
|
|
set_action (ACTION_ENTRIES);
|
|
|
|
set_action (ACTION_MAIN);
|
|
|
|
set_action (ACTION_LIBS);
|
|
|
|
set_action (ACTION_RELOCS);
|
2013-11-09 02:11:03 +00:00
|
|
|
break;
|
2012-11-02 02:35:50 +00:00
|
|
|
case 'q': rad = R_CORE_BIN_SIMPLE; break;
|
|
|
|
case 'j': rad = R_CORE_BIN_JSON; break;
|
2013-06-20 00:49:39 +00:00
|
|
|
case 'A': set_action (ACTION_LISTARCHS); break;
|
2013-07-25 13:18:34 +00:00
|
|
|
case 'a': if (optarg) arch = optarg; break;
|
2011-07-25 19:10:25 +00:00
|
|
|
case 'c':
|
2012-05-31 00:41:45 +00:00
|
|
|
if (!optarg) {
|
|
|
|
eprintf ("Missing argument for -c");
|
2014-08-19 23:21:31 +00:00
|
|
|
r_core_fini (&core);
|
2012-05-31 00:41:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2012-11-02 02:35:50 +00:00
|
|
|
set_action (ACTION_CREATE);
|
2011-07-25 19:10:25 +00:00
|
|
|
create = strdup (optarg);
|
|
|
|
break;
|
2014-05-18 23:42:36 +00:00
|
|
|
case 'k': query = optarg; break;
|
2014-08-15 01:30:45 +00:00
|
|
|
case 'K': chksum = optarg; break;
|
2012-11-02 02:35:50 +00:00
|
|
|
case 'C': set_action (ACTION_CLASSES); break;
|
2012-07-01 22:38:02 +00:00
|
|
|
case 'f': if (optarg) arch_name = strdup (optarg); break;
|
2015-01-11 18:40:26 +00:00
|
|
|
case 'F': forcebin = optarg; break;
|
2012-07-01 22:38:02 +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);
|
2012-11-02 02:35:50 +00:00
|
|
|
set_action (ACTION_SRCLINE);
|
2010-09-24 19:23:13 +00:00
|
|
|
break;
|
2012-11-02 02:35:50 +00:00
|
|
|
case 'i': set_action (ACTION_IMPORTS); break;
|
2013-04-26 01:15:39 +00:00
|
|
|
case 's': set_action (ACTION_SYMBOLS); break;
|
|
|
|
case 'S': set_action (ACTION_SECTIONS); break;
|
2014-04-26 22:31:04 +00:00
|
|
|
case 'z':
|
2013-04-26 01:15:39 +00:00
|
|
|
if (is_active (ACTION_STRINGS)) {
|
2014-10-24 16:25:27 +00:00
|
|
|
if (rawstr) {
|
|
|
|
/* rawstr mode 2 means that we are not going */
|
|
|
|
/* to store them just dump'm all to stdout */
|
|
|
|
rawstr = 2;
|
|
|
|
} else {
|
|
|
|
rawstr = R_TRUE;
|
|
|
|
}
|
2014-05-19 00:41:53 +00:00
|
|
|
} else set_action (ACTION_STRINGS);
|
2012-12-26 01:01:26 +00:00
|
|
|
break;
|
2013-01-29 18:39:41 +00:00
|
|
|
case 'Z': set_action (ACTION_SIZE); break;
|
|
|
|
case 'I': set_action (ACTION_INFO); break;
|
|
|
|
case 'H': set_action (ACTION_FIELDS); break;
|
|
|
|
case 'd': set_action (ACTION_DWARF); break;
|
2014-12-11 14:33:32 +00:00
|
|
|
case 'P':
|
|
|
|
if (is_active(ACTION_PDB)) {
|
2015-01-10 00:00:01 +00:00
|
|
|
set_action (ACTION_PDB_DWNLD);
|
2014-12-11 14:33:32 +00:00
|
|
|
} else {
|
|
|
|
set_action (ACTION_PDB);
|
|
|
|
}
|
|
|
|
break;
|
2015-01-10 00:00:01 +00:00
|
|
|
case 'D': do_demangle = argv[optind]; break;
|
2013-01-29 18:39:41 +00:00
|
|
|
case 'e': set_action (ACTION_ENTRIES); break;
|
2015-04-18 19:57:24 +00:00
|
|
|
case 'E': set_action (ACTION_DLOPEN); break;
|
2013-01-29 18:39:41 +00:00
|
|
|
case 'M': set_action (ACTION_MAIN); break;
|
|
|
|
case 'l': set_action (ACTION_LIBS); break;
|
|
|
|
case 'R': set_action (ACTION_RELOCS); break;
|
|
|
|
case 'x': set_action (ACTION_EXTRACT); break;
|
2012-07-01 22:38:02 +00:00
|
|
|
case 'w': rw = R_TRUE; break;
|
2010-05-28 15:15:20 +00:00
|
|
|
case 'O':
|
|
|
|
op = optarg;
|
2012-11-02 02:35:50 +00:00
|
|
|
set_action (ACTION_OPERATION);
|
2014-05-05 03:08:46 +00:00
|
|
|
if (op && !strcmp (op, "help")) {
|
2013-03-10 13:27:01 +00:00
|
|
|
printf ("Operation string:\n"
|
|
|
|
" Dump symbols: d/s/1024\n"
|
|
|
|
" Dump section: d/S/.text\n"
|
|
|
|
" Resize section: r/.data/1024\n");
|
2014-08-19 23:21:31 +00:00
|
|
|
r_core_fini (&core);
|
2013-03-10 13:27:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-05-30 01:32:20 +00:00
|
|
|
if (optind==argc) {
|
|
|
|
eprintf ("Missing filename\n");
|
2014-08-19 23:21:31 +00:00
|
|
|
r_core_fini (&core);
|
2012-05-30 01:32:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2010-05-28 15:15:20 +00:00
|
|
|
break;
|
2012-07-01 22:38:02 +00:00
|
|
|
case 'o': output = optarg; break;
|
2014-08-24 08:41:32 +00:00
|
|
|
case 'p': va = R_FALSE; break;
|
2012-07-01 22:38:02 +00:00
|
|
|
case 'r': rad = R_TRUE; break;
|
2014-08-23 00:40:24 +00:00
|
|
|
case 'v': return blob_version ("rabin2");
|
2012-07-01 22:38:02 +00:00
|
|
|
case 'L': r_bin_list (bin); return 1;
|
2015-01-29 00:45:39 +00:00
|
|
|
case 'G':
|
2014-08-23 01:18:03 +00:00
|
|
|
laddr = r_num_math (NULL, optarg);
|
|
|
|
if (laddr == 0LL)
|
|
|
|
va = R_FALSE;
|
|
|
|
break;
|
2015-01-29 00:45:39 +00:00
|
|
|
case 'B':
|
|
|
|
baddr = r_num_math (NULL, optarg);
|
|
|
|
break;
|
2012-07-01 22:38:02 +00:00
|
|
|
case '@': at = r_num_math (NULL, optarg); break;
|
|
|
|
case 'n': name = optarg; break;
|
2014-10-24 18:13:18 +00:00
|
|
|
case 'N': {
|
2014-10-21 02:39:37 +00:00
|
|
|
char *q, *p = strdup (optarg);
|
|
|
|
q = strchr (p, ':');
|
|
|
|
if (q) {
|
|
|
|
r_config_set (core.config, "bin.minstr", p);
|
|
|
|
r_config_set (core.config, "bin.maxstr", q+1);
|
|
|
|
} else {
|
|
|
|
r_config_set (core.config, "bin.minstr", optarg);
|
|
|
|
}
|
|
|
|
free (p);
|
2014-10-24 18:13:18 +00:00
|
|
|
} break;
|
2014-08-19 23:21:31 +00:00
|
|
|
case 'h':
|
2015-01-10 00:00:01 +00:00
|
|
|
r_core_fini (&core);
|
|
|
|
return rabin_show_help (1);
|
2013-04-02 10:11:20 +00:00
|
|
|
default: action |= ACTION_HELP;
|
2010-05-28 15:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:00:01 +00:00
|
|
|
if (do_demangle) {
|
|
|
|
char *res = NULL;
|
|
|
|
int type;
|
|
|
|
if ((argc-optind)<2) {
|
|
|
|
return rabin_show_help (0);
|
|
|
|
}
|
|
|
|
type = r_bin_demangle_type (do_demangle);
|
|
|
|
file = argv[optind +1];
|
|
|
|
switch (type) {
|
|
|
|
case R_BIN_NM_CXX: res = r_bin_demangle_cxx (file); break;
|
|
|
|
case R_BIN_NM_JAVA: res = r_bin_demangle_java (file); break;
|
|
|
|
case R_BIN_NM_OBJC: res = r_bin_demangle_objc (NULL, file); break;
|
|
|
|
case R_BIN_NM_SWIFT: res = r_bin_demangle_swift (file); break;
|
2015-02-19 17:39:55 +00:00
|
|
|
case R_BIN_NM_MSVC: res = r_bin_demangle_msvc(file); break;
|
2015-01-10 00:00:01 +00:00
|
|
|
default:
|
|
|
|
eprintf ("Unknown lang to demangle. Use: cxx, java, objc, swift\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (res && *res) {
|
|
|
|
printf ("%s\n", res);
|
2015-02-19 17:39:55 +00:00
|
|
|
free(res);
|
2015-01-10 00:00:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free (res);
|
|
|
|
//eprintf ("%s\n", file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
file = argv[optind ];
|
2014-05-18 23:42:36 +00:00
|
|
|
if (!query)
|
2012-11-02 02:35:50 +00:00
|
|
|
if (action & ACTION_HELP || action == ACTION_UNK || file == NULL) {
|
2014-08-19 23:21:31 +00:00
|
|
|
r_core_fini (&core);
|
2013-04-02 10:11:20 +00:00
|
|
|
return rabin_show_help (0);
|
2012-11-02 02:35:50 +00:00
|
|
|
}
|
2010-05-28 15:15:20 +00:00
|
|
|
|
2010-09-25 01:45:03 +00:00
|
|
|
if (arch) {
|
|
|
|
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");
|
2014-08-19 23:21:31 +00:00
|
|
|
r_core_fini (&core);
|
2011-07-25 19:10:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*p++ = 0;
|
|
|
|
p2 = strchr (p, ':');
|
|
|
|
if (p2) {
|
|
|
|
// has data
|
|
|
|
*p2++ = 0;
|
2013-12-02 23:14:59 +00:00
|
|
|
data = malloc (strlen (p2)+1);
|
2011-07-25 19:10:25 +00:00
|
|
|
datalen = r_hex_str2bin (p2, data);
|
2011-12-05 23:27:57 +00:00
|
|
|
} else {
|
|
|
|
data = NULL;
|
|
|
|
datalen = 0;
|
2011-07-25 19:10:25 +00:00
|
|
|
}
|
2014-04-26 22:31:04 +00:00
|
|
|
code = malloc (strlen (p)+1);
|
2014-08-19 23:21:31 +00:00
|
|
|
if (!code) {
|
|
|
|
r_core_fini (&core);
|
2015-04-18 19:57:24 +00:00
|
|
|
return 1;
|
2014-08-19 23:21:31 +00:00
|
|
|
}
|
2011-07-25 19:10:25 +00:00
|
|
|
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");
|
2014-08-19 23:21:31 +00:00
|
|
|
r_core_fini (&core);
|
2011-07-25 19:10:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
b = r_bin_create (bin, code, codelen, data, datalen);
|
|
|
|
if (b) {
|
2015-04-03 02:04:46 +00:00
|
|
|
if (r_file_dump (file, b->buf, b->length, 0)) {
|
2015-01-11 18:40:26 +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);
|
2015-01-11 18:40:26 +00:00
|
|
|
} 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);
|
2014-08-19 23:21:31 +00:00
|
|
|
r_core_fini (&core);
|
2011-07-25 19:10:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-24 16:25:27 +00:00
|
|
|
if (rawstr == 2) {
|
|
|
|
unset_action (ACTION_STRINGS);
|
|
|
|
}
|
2014-05-19 00:41:53 +00:00
|
|
|
r_config_set_i (core.config, "bin.rawstr", rawstr);
|
2015-04-18 19:57:24 +00:00
|
|
|
|
|
|
|
if (file && *file && action&ACTION_DLOPEN) {
|
|
|
|
void *addr = r_lib_dl_open (file);
|
|
|
|
if (addr) {
|
2015-03-29 08:23:14 +00:00
|
|
|
printf ("%s is loaded at 0x%"PFMT64x"\n", file, (ut64)(size_t)(addr));
|
2015-04-18 19:57:24 +00:00
|
|
|
r_lib_dl_close (addr);
|
|
|
|
} else
|
|
|
|
printf("Cannot open the '%s' library\n", file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-21 01:04:49 +00:00
|
|
|
if (file && *file) {
|
|
|
|
cf = r_core_file_open (&core, file, R_IO_READ, 0);
|
|
|
|
fd = cf ? r_core_file_cur_fd (&core) : -1;
|
|
|
|
if (!cf || fd == -1) {
|
|
|
|
eprintf ("r_core: Cannot open file\n");
|
|
|
|
r_core_fini (&core);
|
|
|
|
return 1;
|
|
|
|
}
|
2011-07-25 19:10:25 +00:00
|
|
|
}
|
2014-05-26 01:06:29 +00:00
|
|
|
|
2014-10-24 18:13:18 +00:00
|
|
|
bin->minstrlen = r_config_get_i (core.config, "bin.minstr");
|
2015-01-11 18:40:26 +00:00
|
|
|
r_bin_force_plugin (bin, forcebin);
|
2014-07-29 22:33:54 +00:00
|
|
|
if (!r_bin_load (bin, file, laddr, 0, xtr_idx, fd, rawstr)) {
|
|
|
|
if (!r_bin_load (bin, file, laddr, 0, xtr_idx, fd, rawstr)) {
|
2014-05-21 21:19:47 +00:00
|
|
|
eprintf ("r_bin: Cannot open file\n");
|
2014-08-19 23:21:31 +00:00
|
|
|
r_core_fini (&core);
|
2014-04-27 07:06:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2015-01-29 00:45:39 +00:00
|
|
|
if (baddr != 0LL) {
|
|
|
|
bin->cur->o->baddr = baddr;
|
|
|
|
//bin->cur->o->baddr = laddr;
|
|
|
|
}
|
2014-10-24 16:25:27 +00:00
|
|
|
if (rawstr == 2) {
|
|
|
|
rawstr = R_FALSE;
|
|
|
|
r_bin_dump_strings (core.bin->cur, bin->minstrlen);
|
|
|
|
}
|
2011-09-04 00:19:43 +00:00
|
|
|
|
2014-05-18 23:42:36 +00:00
|
|
|
if (query) {
|
2014-09-08 02:17:38 +00:00
|
|
|
if (rad) {
|
|
|
|
// TODO: Should be moved into core, to load those flags and formats into r2
|
|
|
|
Sdb *db = sdb_ns (bin->cur->sdb, "info", 0);
|
|
|
|
char *flagname;
|
|
|
|
if (db) {
|
|
|
|
|
|
|
|
SdbListIter *iter;
|
|
|
|
SdbKv *kv;
|
2014-09-08 10:20:50 +00:00
|
|
|
printf ("fs format\n");
|
2014-09-08 02:17:38 +00:00
|
|
|
// iterate over all keys
|
|
|
|
ls_foreach (db->ht->list, iter, kv) {
|
|
|
|
char *k = kv->key;
|
|
|
|
char *v = kv->value;
|
|
|
|
char *dup = strdup (k);
|
|
|
|
|
|
|
|
if ((flagname=strstr (dup, ".offset"))) {
|
|
|
|
*flagname = 0;
|
|
|
|
flagname = dup;
|
|
|
|
|
2014-09-08 02:37:34 +00:00
|
|
|
printf ("f %s @ %s\n", flagname, v);
|
2014-09-08 02:17:38 +00:00
|
|
|
}
|
2014-09-23 08:23:28 +00:00
|
|
|
if ((flagname=strstr (dup, ".cparse"))) {
|
|
|
|
printf ("\"td %s\"\n", v);
|
|
|
|
}
|
2014-09-08 02:17:38 +00:00
|
|
|
if ((flagname=strstr (dup, ".format"))) {
|
|
|
|
*flagname = 0;
|
|
|
|
flagname = dup;
|
|
|
|
|
2014-09-08 02:37:34 +00:00
|
|
|
printf ("pf.%s %s\n", flagname, v);
|
2014-09-08 02:17:38 +00:00
|
|
|
}
|
|
|
|
free (dup);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//sdb_query (bin->cur->sdb, "info/*");
|
|
|
|
} else {
|
|
|
|
if (!strcmp (query, "-")) {
|
|
|
|
__sdb_prompt (bin->cur->sdb);
|
|
|
|
} else sdb_query (bin->cur->sdb, query);
|
|
|
|
}
|
2014-08-19 23:21:31 +00:00
|
|
|
r_core_fini (&core);
|
2014-05-18 23:42:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-02 02:35:50 +00:00
|
|
|
// XXX: TODO move this to libr/core/bin.c
|
2011-09-04 00:44:53 +00:00
|
|
|
if (action & ACTION_LISTARCHS || ((arch || bits || arch_name) &&
|
|
|
|
!r_bin_select (bin, arch, bits, arch_name))) {
|
2012-11-02 02:35:50 +00:00
|
|
|
if (rad == R_CORE_BIN_JSON) {
|
|
|
|
int i;
|
|
|
|
printf ("[");
|
|
|
|
for (i = 0; i < bin->narch; i++) {
|
2014-04-27 07:06:50 +00:00
|
|
|
if (r_bin_select_idx (bin, bin->file, i)) {
|
|
|
|
RBinObject *o = r_bin_cur_object (bin);
|
2014-05-04 15:03:15 +00:00
|
|
|
RBinInfo *info = o ? o->info : NULL;
|
2012-11-02 02:35:50 +00:00
|
|
|
printf ("%s{\"arch\":\"%s\",\"bits\":%d,"
|
|
|
|
"\"offset\":%"PFMT64d",\"machine\":\"%s\"}",
|
|
|
|
i?",":"",info->arch, info->bits,
|
2014-01-18 15:26:09 +00:00
|
|
|
bin->cur->offset, info->machine);
|
2012-11-02 02:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf ("]");
|
2014-04-23 01:54:06 +00:00
|
|
|
} else r_bin_list_archs (bin, 1);
|
2011-09-04 00:44:53 +00:00
|
|
|
free (arch_name);
|
2010-05-28 15:15:20 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 22:33:54 +00:00
|
|
|
// ASLR WTF
|
|
|
|
if (laddr != 0LL) {
|
|
|
|
//r_bin_set_baddr (bin, laddr);
|
|
|
|
//bin->cur->o->baddr = laddr;
|
2014-05-26 01:06:29 +00:00
|
|
|
}
|
2014-07-29 22:33:54 +00:00
|
|
|
r_config_set_i (core.config, "bin.laddr", laddr);
|
2011-11-24 01:48:36 +00:00
|
|
|
|
2011-11-12 03:20:22 +00:00
|
|
|
core.bin = bin;
|
|
|
|
filter.offset = at;
|
|
|
|
filter.name = name;
|
|
|
|
|
2011-11-21 23:59:20 +00:00
|
|
|
r_cons_new ()->is_interactive = R_FALSE;
|
2012-11-02 02:35:50 +00:00
|
|
|
|
2015-03-08 16:29:53 +00:00
|
|
|
#define isradjson (rad==R_CORE_BIN_JSON&&actions>0)
|
|
|
|
#define run_action(n,x,y) {\
|
|
|
|
if (action&x) {\
|
|
|
|
if (isradjson) r_cons_printf ("\"%s\":",n);\
|
|
|
|
if (!r_core_bin_info (&core, y, rad, va, &filter, laddr, chksum)) {\
|
|
|
|
if (isradjson) r_cons_printf ("false");\
|
|
|
|
};\
|
|
|
|
actions_done++;\
|
|
|
|
if (isradjson) r_cons_printf (actions==actions_done? "":",");\
|
|
|
|
}\
|
|
|
|
}
|
|
|
|
if (action & ACTION_PDB_DWNLD) {
|
|
|
|
int ret;
|
2015-04-02 01:10:49 +00:00
|
|
|
char *env_pdbserver = r_sys_getenv ("PDB_SERVER");
|
2015-05-25 18:45:37 +00:00
|
|
|
char *env_pdbextract = r_sys_getenv("PDB_EXTRACT");
|
|
|
|
char *env_useragent = r_sys_getenv("PDB_USER_AGENT");
|
2014-12-11 14:33:32 +00:00
|
|
|
SPDBDownloader pdb_downloader;
|
|
|
|
SPDBDownloaderOpt opt;
|
2015-04-01 12:57:36 +00:00
|
|
|
RBinInfo *info = r_bin_get_info (core.bin);
|
|
|
|
char *path;
|
2015-05-09 22:05:34 +00:00
|
|
|
|
|
|
|
if (!info || !info->debug_file_name) {
|
|
|
|
eprintf ("Can't find debug filename\n");
|
|
|
|
r_core_fini (&core);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-01 12:57:36 +00:00
|
|
|
if (info->file) {
|
|
|
|
path = r_file_dirname (info->file);
|
|
|
|
} else {
|
|
|
|
path = strdup (".");
|
|
|
|
}
|
2015-05-25 18:45:37 +00:00
|
|
|
|
2014-12-24 18:49:07 +00:00
|
|
|
if (env_pdbserver && *env_pdbserver)
|
|
|
|
r_config_set (core.config, "pdb.server", env_pdbserver);
|
2015-05-25 18:45:37 +00:00
|
|
|
if (env_useragent && *env_useragent)
|
|
|
|
r_config_set (core.config, "pdb.user_agent", env_pdbextract);
|
|
|
|
if (env_pdbextract && *env_pdbextract)
|
|
|
|
r_config_set_i (core.config, "pdb.extract", !(*env_pdbextract == '0'));
|
|
|
|
|
2014-12-11 14:33:32 +00:00
|
|
|
opt.dbg_file = info->debug_file_name;
|
|
|
|
opt.guid = info->guid;
|
2015-03-08 16:29:53 +00:00
|
|
|
opt.symbol_server = (char *)r_config_get (core.config, "pdb.server");
|
|
|
|
opt.user_agent = (char *)r_config_get (core.config, "pdb.user_agent");
|
2014-12-11 14:33:32 +00:00
|
|
|
opt.path = path;
|
2015-05-25 14:48:43 +00:00
|
|
|
opt.extract = r_config_get_i(core.config, "pdb.extract");
|
2014-12-11 14:33:32 +00:00
|
|
|
|
2015-03-08 16:29:53 +00:00
|
|
|
init_pdb_downloader (&opt, &pdb_downloader);
|
|
|
|
ret = pdb_downloader.download (&pdb_downloader);
|
|
|
|
if (isradjson) {
|
|
|
|
printf ("{\"pdb\":{\"file\":\"%s\",\"download\":%s}}\n",
|
|
|
|
opt.dbg_file, ret?"true":"false");
|
2014-12-11 14:33:32 +00:00
|
|
|
} else {
|
2015-03-08 16:29:53 +00:00
|
|
|
printf ("PDB \"%s\" download %s\n",
|
|
|
|
opt.dbg_file, ret? "success": "failed");
|
2014-12-11 14:33:32 +00:00
|
|
|
}
|
2015-03-08 16:29:53 +00:00
|
|
|
deinit_pdb_downloader (&pdb_downloader);
|
2014-12-11 14:33:32 +00:00
|
|
|
|
2015-03-08 16:29:53 +00:00
|
|
|
free (path);
|
2014-12-11 14:33:32 +00:00
|
|
|
r_core_fini (&core);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-02 02:35:50 +00:00
|
|
|
if (isradjson) r_cons_printf ("{");
|
|
|
|
run_action ("sections", ACTION_SECTIONS, R_CORE_BIN_ACC_SECTIONS);
|
|
|
|
run_action ("entries", ACTION_ENTRIES, R_CORE_BIN_ACC_ENTRIES);
|
|
|
|
run_action ("main", ACTION_MAIN, R_CORE_BIN_ACC_MAIN);
|
|
|
|
run_action ("imports", ACTION_IMPORTS, R_CORE_BIN_ACC_IMPORTS);
|
|
|
|
run_action ("classes", ACTION_CLASSES, R_CORE_BIN_ACC_CLASSES);
|
|
|
|
run_action ("symbols", ACTION_SYMBOLS, R_CORE_BIN_ACC_SYMBOLS);
|
|
|
|
run_action ("strings", ACTION_STRINGS, R_CORE_BIN_ACC_STRINGS);
|
|
|
|
run_action ("info", ACTION_INFO, R_CORE_BIN_ACC_INFO);
|
|
|
|
run_action ("fields", ACTION_FIELDS, R_CORE_BIN_ACC_FIELDS);
|
|
|
|
run_action ("libs", ACTION_LIBS, R_CORE_BIN_ACC_LIBS);
|
|
|
|
run_action ("relocs", ACTION_RELOCS, R_CORE_BIN_ACC_RELOCS);
|
|
|
|
run_action ("dwarf", ACTION_DWARF, R_CORE_BIN_ACC_DWARF);
|
2014-10-13 08:20:45 +00:00
|
|
|
run_action ("pdb", ACTION_PDB, R_CORE_BIN_ACC_PDB);
|
2012-11-02 02:35:50 +00:00
|
|
|
run_action ("size", ACTION_SIZE, R_CORE_BIN_ACC_SIZE);
|
2010-05-28 15:15:20 +00:00
|
|
|
if (action&ACTION_SRCLINE)
|
2011-10-09 23:48:08 +00:00
|
|
|
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);
|
2012-11-02 02:35:50 +00:00
|
|
|
if (isradjson)
|
|
|
|
printf ("}");
|
2011-11-12 03:51:45 +00:00
|
|
|
r_cons_flush ();
|
2013-09-27 18:24:59 +00:00
|
|
|
r_core_fini (&core);
|
2010-05-28 15:15:20 +00:00
|
|
|
|
2010-09-24 19:23:13 +00:00
|
|
|
return 0;
|
2010-05-28 15:15:20 +00:00
|
|
|
}
|