mirror of
https://github.com/radareorg/radare2.git
synced 2024-11-27 15:10:53 +00:00
Fix #313 - Add bin.baddr and r2 -B to set base address for r_bin
This commit is contained in:
parent
1cec548b68
commit
f1c9943df2
@ -73,7 +73,7 @@ static int main_help(int line) {
|
||||
" -a [arch] set asm.arch\n"
|
||||
" -A run 'aa' command to analyze all referenced code\n"
|
||||
" -b [bits] set asm.bits\n"
|
||||
" -B [size] initial block size\n"
|
||||
" -B [baddr] set base address for PIE binaries\n"
|
||||
" -c 'cmd..' execute radare command\n"
|
||||
" -C file is host:port (alias for -c+=http://%%s/cmd/)\n"
|
||||
" -d use 'file' as a program to debug\n"
|
||||
@ -174,7 +174,7 @@ int main(int argc, char **argv) {
|
||||
int help = 0;
|
||||
int debug = 0;
|
||||
int fullfile = 0;
|
||||
ut32 bsize = 0;
|
||||
ut64 baddr = 0;
|
||||
ut64 seek = 0;
|
||||
char *pfile = NULL, *file = NULL;
|
||||
char *cmdfile[32];
|
||||
@ -281,7 +281,7 @@ int main(int argc, char **argv) {
|
||||
case 'a': asmarch = optarg; break;
|
||||
case 'k': asmos = optarg; break;
|
||||
case 'b': asmbits = optarg; break;
|
||||
case 'B': bsize = (ut32) r_num_math (r.num, optarg); break;
|
||||
case 'B': baddr = r_num_math (r.num, optarg); break;
|
||||
case 's': seek = r_num_math (r.num, optarg); break;
|
||||
case 'L': list_io_plugins (r.io); return 0;
|
||||
default: return 1;
|
||||
@ -299,6 +299,7 @@ int main(int argc, char **argv) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
r_config_set_i (r.config, "bin.baddr", baddr);
|
||||
// DUP
|
||||
if (asmarch) r_config_set (r.config, "asm.arch", asmarch);
|
||||
if (asmbits) r_config_set (r.config, "asm.bits", asmbits);
|
||||
@ -347,7 +348,7 @@ int main(int argc, char **argv) {
|
||||
if (optind<argc)
|
||||
file = r_str_concat (file, " ");
|
||||
}
|
||||
if (!r_core_bin_load (&r, file)) {
|
||||
if (!r_core_bin_load (&r, file, baddr)) {
|
||||
RBinObject *obj = r_bin_get_object (r.bin);
|
||||
if (obj && obj->info)
|
||||
eprintf ("bits %d\n", obj->info->bits);
|
||||
@ -426,7 +427,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
if (r.file && r.file->filename)
|
||||
filepath = r.file->filename;
|
||||
if (!r_core_bin_load (&r, filepath))
|
||||
if (!r_core_bin_load (&r, filepath, baddr))
|
||||
r_config_set (r.config, "io.va", "false");
|
||||
}
|
||||
}
|
||||
@ -487,7 +488,6 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
if (fullfile) r_core_block_size (&r, r.file->size);
|
||||
else if (bsize) r_core_block_size (&r, bsize);
|
||||
|
||||
r_core_seek (&r, r.offset, 1); // read current block
|
||||
|
||||
|
@ -135,7 +135,7 @@ static RList* imports(RBinArch *arch) {
|
||||
strncpy (ptr->name, name, R_BIN_SIZEOF_STRINGS);
|
||||
strncpy (ptr->type, type, R_BIN_SIZEOF_STRINGS);
|
||||
ptr->ordinal = imports[i].ord;
|
||||
if(bin->imports_by_ord && ptr->ordinal < bin->imports_by_ord_size)
|
||||
if (bin->imports_by_ord && ptr->ordinal < bin->imports_by_ord_size)
|
||||
bin->imports_by_ord[ptr->ordinal] = ptr;
|
||||
r_list_append (ret, ptr);
|
||||
}
|
||||
|
@ -477,6 +477,25 @@ static int bin_relocs (RCore *r, int mode, ut64 baddr, int va) {
|
||||
return R_TRUE;
|
||||
}
|
||||
|
||||
/* XXX: This is a hack to get PLT references in rabin2 -i */
|
||||
/* imp. is a prefix that can be rewritten by the symbol table */
|
||||
static ut64 impaddr(RBin *bin, int va, ut64 baddr, const char *name) {
|
||||
RBinSymbol *symbol;
|
||||
RList *symbols;
|
||||
RListIter *iter;
|
||||
if (!name) return R_FALSE;
|
||||
if ((symbols = r_bin_get_symbols (bin)) == NULL)
|
||||
return R_FALSE;
|
||||
r_list_foreach (symbols, iter, symbol) {
|
||||
if (strncmp (symbol->name, "imp.", 4))
|
||||
continue;
|
||||
if (!strcmp (symbol->name+4, name))
|
||||
return va? get_vaddr (baddr, symbol->offset,
|
||||
symbol->rva): symbol->offset;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bin_imports (RCore *r, int mode, ut64 baddr, int va, const char *name) {
|
||||
RBinImport *import;
|
||||
RListIter *iter;
|
||||
@ -520,12 +539,14 @@ static int bin_imports (RCore *r, int mode, ut64 baddr, int va, const char *name
|
||||
}
|
||||
}*/
|
||||
} else {
|
||||
ut64 addr;
|
||||
if (mode) r_cons_printf ("fs imports\n");
|
||||
else r_cons_printf ("[Imports]\n");
|
||||
|
||||
r_list_foreach (imports, iter, import) {
|
||||
if (name && strcmp (import->name, name))
|
||||
continue;
|
||||
addr = impaddr (r->bin, va, baddr, import->name);
|
||||
if (mode) {
|
||||
// TODO(eddyb) use the logic below for symbols that are imports.
|
||||
/*r_name_filter (import->name, sizeof (import->name));
|
||||
@ -545,8 +566,9 @@ static int bin_imports (RCore *r, int mode, ut64 baddr, int va, const char *name
|
||||
import->size, import->name);
|
||||
r_cons_printf ("f imp.%s @ 0x%08"PFMT64x"\n",
|
||||
import->name, va?baddr+import->rva:import->offset);*/
|
||||
} else r_cons_printf ("ordinal=%03"PFMT64d" bind=%s type=%s name=%s\n",
|
||||
import->ordinal, import->bind, import->type, import->name);
|
||||
} else r_cons_printf ("ordinal=%03"PFMT64d" plt=0x%08"PFMT64x" bind=%s type=%s name=%s\n",
|
||||
import->ordinal, addr,
|
||||
import->bind, import->type, import->name);
|
||||
i++;
|
||||
}
|
||||
if (!mode) r_cons_printf ("\n%i imports\n", i);
|
||||
|
@ -315,7 +315,8 @@ static int cmd_cmp(void *data, const char *input) {
|
||||
r_anal_diff_setup (core->anal, diffops, -1, -1);
|
||||
r_anal_diff_setup (core2->anal, diffops, -1, -1);
|
||||
|
||||
r_core_bin_load (core2, file2);
|
||||
r_core_bin_load (core2, file2,
|
||||
r_config_get_i (core->config, "bin.baddr"));
|
||||
r_core_gdiff (core, core2);
|
||||
r_core_diff_show (core, core2);
|
||||
r_core_free (core2);
|
||||
|
@ -86,7 +86,9 @@ static int cmd_info(void *data, const char *input) {
|
||||
|
||||
switch (*input) {
|
||||
case 'o': r_core_bin_load (core, input[1]==' '?
|
||||
input+1: core->file->filename); break;
|
||||
input+1: core->file->filename,
|
||||
r_config_get_i (core->config, "bin.baddr"));
|
||||
break;
|
||||
#define RBININFO(x) r_core_bin_info(core,x,mode,va,NULL,offset)
|
||||
case 'S': RBININFO (R_CORE_BIN_ACC_SECTIONS); break;
|
||||
case 'h': RBININFO (R_CORE_BIN_ACC_FIELDS); break;
|
||||
|
@ -3,12 +3,12 @@
|
||||
static int cmd_open(void *data, const char *input) {
|
||||
RCore *core = (RCore*)data;
|
||||
int perms = R_IO_READ;
|
||||
ut64 addr, baddr = r_config_get_i (core->config, "bin.baddr");
|
||||
RIOMap *map = NULL;
|
||||
RCoreFile *file;
|
||||
RListIter *iter;
|
||||
int num = -1;
|
||||
int isn = 0;
|
||||
ut64 addr;
|
||||
char *ptr;
|
||||
|
||||
switch (*input) {
|
||||
@ -36,7 +36,7 @@ static int cmd_open(void *data, const char *input) {
|
||||
if (file) {
|
||||
// MUST CLEAN BEFORE LOADING
|
||||
if (!isn)
|
||||
r_core_bin_load (core, fn);
|
||||
r_core_bin_load (core, fn, baddr);
|
||||
} else eprintf ("Cannot open file '%s'\n", fn);
|
||||
} else r_io_raise (core->io, num);
|
||||
r_core_block_read (core, 0);
|
||||
@ -141,7 +141,7 @@ static int cmd_open(void *data, const char *input) {
|
||||
r_core_init (core);
|
||||
if (!r_core_file_open (core, input+2, R_IO_READ, 0))
|
||||
eprintf ("Cannot open file\n");
|
||||
if (!r_core_bin_load (core, NULL))
|
||||
if (!r_core_bin_load (core, NULL, baddr))
|
||||
r_config_set (core->config, "io.va", "false");
|
||||
break;
|
||||
case '?':
|
||||
|
@ -600,6 +600,7 @@ R_API int r_core_config_init(RCore *core) {
|
||||
#endif
|
||||
|
||||
/* bin */
|
||||
SETI("bin.baddr", 0, "Set base address for loading binaries ('o')");
|
||||
SETPREF("bin.dwarf", "false", "Load dwarf information on startup if available");
|
||||
SETI("bin.minstr", 0, "Minimum string length for r_bin");
|
||||
SETPREF("bin.rawstr", "false", "Load strings from raw binaries");
|
||||
|
@ -835,11 +835,12 @@ reaccept:
|
||||
eprintf ("Cannot malloc in rmt-open len = %d\n", cmd);
|
||||
} else {
|
||||
RCoreFile *file;
|
||||
ut64 baddr = r_config_get_i (core->config, "bin.baddr");
|
||||
r_socket_read_block (c, ptr, cmd); //filename
|
||||
ptr[cmd] = 0;
|
||||
file = r_core_file_open (core, (const char *)ptr, R_IO_READ, 0); // XXX: write mode?
|
||||
if (file) {
|
||||
r_core_bin_load (core, NULL);
|
||||
r_core_bin_load (core, NULL, baddr);
|
||||
file->map = r_io_map_add (core->io, file->fd->fd, R_IO_READ, 0, 0, file->size);
|
||||
pipefd = core->file->fd->fd;
|
||||
eprintf ("(flags: %d) len: %d filename: '%s'\n",
|
||||
|
@ -137,7 +137,7 @@ R_API char *r_core_sysenv_begin(RCore *core, const char *cmd) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
R_API int r_core_bin_load(RCore *r, const char *file) {
|
||||
R_API int r_core_bin_load(RCore *r, const char *file, ut64 baddr) {
|
||||
int i, va = r->io->va || r->io->debug;
|
||||
RListIter *iter;
|
||||
const char *p;
|
||||
@ -193,6 +193,7 @@ R_API int r_core_bin_load(RCore *r, const char *file) {
|
||||
return R_TRUE;
|
||||
}
|
||||
r->file->obj = r_bin_get_object (r->bin);
|
||||
r->file->obj->baddr = baddr;
|
||||
|
||||
r_config_set_i (r->config, "io.va",
|
||||
(r->file->obj->info)? r->file->obj->info->has_va: 0);
|
||||
|
@ -268,7 +268,7 @@ R_API int r_core_print_disasm(RPrint *p, RCore *core, ut64 addr, ut8 *buf, int l
|
||||
R_API int r_core_print_disasm_json(RCore *core, ut64 addr, ut8 *buf, int len);
|
||||
R_API int r_core_print_disasm_instructions (RCore *core, int len, int l);
|
||||
|
||||
R_API int r_core_bin_load(RCore *core, const char *file);
|
||||
R_API int r_core_bin_load(RCore *core, const char *file, ut64 baddr);
|
||||
R_API int r_core_hash_load(RCore *core, const char *file);
|
||||
|
||||
/* gdiff.c */
|
||||
|
@ -8,7 +8,7 @@ radare2 \- Advanced commandline hexadecimal editor, disassembler and debugger
|
||||
.Nm radare2
|
||||
.Op Fl a Ar arch
|
||||
.Op Fl b Ar bits
|
||||
.Op Fl B Ar blocksize
|
||||
.Op Fl B Ar baddr
|
||||
.Op Fl c Ar cmd
|
||||
.Op Fl e Ar k=v
|
||||
.Op Fl i Ar file
|
||||
@ -33,8 +33,8 @@ force asm.arch (x86, ppc, arm, mips, bf, java, ...)
|
||||
run 'aa' command before prompt or patch to analyze all referenced code
|
||||
.It Fl b Ar bits
|
||||
force asm.bits (16, 32, 64)
|
||||
.It Fl B Ar bsize
|
||||
Set block size
|
||||
.It Fl B Ar baddr
|
||||
Specify the base address to be used when loading a new binary. See 'e?bin.baddr'
|
||||
.It Fl c Ar cmd
|
||||
Execute the given command before giving prompt
|
||||
.It Fl d
|
||||
|
Loading…
Reference in New Issue
Block a user