mirror of
https://github.com/radareorg/radare2.git
synced 2024-11-23 13:19:54 +00:00
* More work on visual core
- Added initial cursor support - Q also for quit - Added visual prompt * Added date.c * Move cursor responsability to r_print * Make r_cons_invert work also for noncolor terminals * Added r_num_minmax_swap_i in r_util
This commit is contained in:
parent
9bd9cbe135
commit
e8b70a1a0b
8
TODO
Normal file
8
TODO
Normal file
@ -0,0 +1,8 @@
|
||||
* Specify binmask in hexpairs
|
||||
- wx 1234:ff0f
|
||||
- /x 1234:ff0f
|
||||
* radare2 -e dbg.engine=vm -d ls
|
||||
- resolve path for ls
|
||||
- load the program using r_bin in virtual space
|
||||
- initialize vm and set regs
|
||||
- debug backend should use the vm
|
@ -2,9 +2,13 @@
|
||||
|
||||
#include "r_core.h"
|
||||
|
||||
static int cursor = 0;
|
||||
static int flags = R_PRINT_FLAGS_ADDRMOD;
|
||||
#define NPF 5
|
||||
static int printidx = 0;
|
||||
const char *printfmt[] = { "x", "pd", "p8", "pc", "ps" };
|
||||
|
||||
static int curset = 0, cursor = -1, ocursor=-1;
|
||||
static int color = 1;
|
||||
static int flags = R_PRINT_FLAGS_ADDRMOD;
|
||||
|
||||
/* TODO: use r_cmd here in core->vcmd..optimize over 255 table */
|
||||
int r_core_visual_cmd(struct r_core_t *core, int ch)
|
||||
@ -13,41 +17,71 @@ int r_core_visual_cmd(struct r_core_t *core, int ch)
|
||||
|
||||
switch(ch) {
|
||||
case 'c':
|
||||
cursor ^= 1;
|
||||
if (cursor) flags|=R_PRINT_FLAGS_CURSOR;
|
||||
curset ^= 1;
|
||||
if (curset) flags|=R_PRINT_FLAGS_CURSOR; // XXX dupped flag imho
|
||||
else flags &= !(flags&R_PRINT_FLAGS_CURSOR);
|
||||
r_print_set_flags(flags);
|
||||
break;
|
||||
case 'C':
|
||||
cursor ^= 1;
|
||||
if (cursor) flags|=R_PRINT_FLAGS_COLOR;
|
||||
color ^= 1;
|
||||
if (color) flags|=R_PRINT_FLAGS_COLOR;
|
||||
else flags &= !(flags&R_PRINT_FLAGS_COLOR);
|
||||
r_print_set_flags(flags);
|
||||
break;
|
||||
/* select */
|
||||
case 'H':
|
||||
if (curset) {
|
||||
if (ocursor ==-1) ocursor=cursor;
|
||||
cursor--;
|
||||
} else
|
||||
r_core_cmd(core, "s- 2", 0);
|
||||
break;
|
||||
case 'L':
|
||||
r_core_cmd(core, "s+ 2", 0);
|
||||
break;
|
||||
case 'h':
|
||||
r_core_cmd(core, "s- 1", 0);
|
||||
break;
|
||||
case 'l':
|
||||
r_core_cmd(core, "s+ 1", 0);
|
||||
break;
|
||||
case 'j':
|
||||
r_core_cmd(core, "s+ 16", 0);
|
||||
break;
|
||||
case 'k':
|
||||
r_core_cmd(core, "s- 16", 0);
|
||||
break;
|
||||
case 'J':
|
||||
if (curset) {
|
||||
if (ocursor ==-1) ocursor=cursor;
|
||||
cursor+=16;
|
||||
} else
|
||||
r_core_cmd(core, "s++", 0);
|
||||
break;
|
||||
case 'K':
|
||||
if (curset) {
|
||||
if (ocursor ==-1) ocursor=cursor;
|
||||
cursor-=16;
|
||||
} else
|
||||
r_core_cmd(core, "s--", 0);
|
||||
break;
|
||||
case 'L':
|
||||
if (curset) {
|
||||
if (ocursor ==-1) ocursor=cursor;
|
||||
cursor++;
|
||||
} else
|
||||
r_core_cmd(core, "s+ 2", 0);
|
||||
break;
|
||||
/* move */
|
||||
case 'h':
|
||||
if (curset) {
|
||||
cursor--;
|
||||
ocursor=-1;
|
||||
} else r_core_cmd(core, "s- 1", 0);
|
||||
break;
|
||||
case 'l':
|
||||
if (curset) {
|
||||
cursor++;
|
||||
ocursor=-1;
|
||||
} else r_core_cmd(core, "s+ 1", 0);
|
||||
break;
|
||||
case 'j':
|
||||
if (curset) {
|
||||
cursor+=16;
|
||||
ocursor=-1;
|
||||
} else r_core_cmd(core, "s+ 16", 0);
|
||||
break;
|
||||
case 'k':
|
||||
if (curset) {
|
||||
cursor-=16;
|
||||
ocursor=-1;
|
||||
} else r_core_cmd(core, "s- 16", 0);
|
||||
break;
|
||||
case 'p':
|
||||
printidx++;
|
||||
break;
|
||||
@ -84,20 +118,25 @@ int r_core_visual_cmd(struct r_core_t *core, int ch)
|
||||
r_cons_any_key();
|
||||
break;
|
||||
case 'q':
|
||||
case 'Q':
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void r_core_visual_prompt(struct r_core_t *core)
|
||||
{
|
||||
r_cons_printf("[0x%08llx] %s\n", core->seek, printfmt[printidx%NPF]);
|
||||
}
|
||||
|
||||
int r_core_visual(struct r_core_t *core, const char *input)
|
||||
{
|
||||
int ch;
|
||||
char *printfmt[] = { "x", "pd", "p8", "pc", "ps" };
|
||||
|
||||
while(input[0]) {
|
||||
if (!r_core_visual_cmd(core, input[0])) {
|
||||
r_cons_clear00();
|
||||
r_core_cmd(core, printfmt[printidx%5], 0);
|
||||
r_core_cmd(core, printfmt[printidx%NPF], 0);
|
||||
r_cons_flush();
|
||||
r_cons_any_key();
|
||||
return 0;
|
||||
@ -105,9 +144,12 @@ int r_core_visual(struct r_core_t *core, const char *input)
|
||||
input = input + 1;
|
||||
}
|
||||
|
||||
color = r_config_get_i(&core->config, "scr.color");
|
||||
do {
|
||||
r_cons_clear00();
|
||||
r_core_cmd(core, printfmt[printidx%5], 0);
|
||||
r_print_set_cursor(curset, ocursor, cursor);
|
||||
r_core_visual_prompt(core);
|
||||
r_core_cmd(core, printfmt[printidx%NPF], 0);
|
||||
r_cons_flush();
|
||||
ch = r_cons_readchar();
|
||||
} while (r_core_visual_cmd(core, ch));
|
||||
|
@ -132,4 +132,7 @@ extern char r_cons_palette[CONS_PALETTE_SIZE][8];
|
||||
const char *r_cons_get_buffer();
|
||||
void r_cons_grep(const char *str);
|
||||
|
||||
void r_cons_invert(int set, int color);
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -35,6 +35,7 @@ struct r_debug_t {
|
||||
int r_debug_handle_add(struct r_debug_t *dbg, struct r_debug_handle_t *foo);
|
||||
int r_debug_handle_set(struct r_debug_t *dbg, const char *str);
|
||||
int r_debug_handle_init(struct r_debug_t *dbg);
|
||||
int r_debug_init(struct r_debug_t *dbg);
|
||||
|
||||
int r_debug_continue(struct r_debug_t *dbg);
|
||||
|
||||
|
@ -7,11 +7,14 @@
|
||||
#define R_PRINT_FLAGS_ADDRMOD 0x00000002
|
||||
#define R_PRINT_FLAGS_CURSOR 0x00000003
|
||||
|
||||
extern int r_print_cursor;
|
||||
void r_print_set_flags(int flags);
|
||||
void r_print_addr(u64 addr);
|
||||
void r_print_hexdump(u64 addr, u8 *buf, int len, int step, int columns, int header);
|
||||
void r_print_bytes(const u8* buf, int len, const char *fmt);
|
||||
void r_print_raw(const u8* buf, int len);
|
||||
void r_print_cursor(int cur, int set);
|
||||
void r_print_set_cursor(int curset, int ocursor, int cursor);
|
||||
void r_print_code(u64 addr, u8 *buf, int len, int step, int columns, int header);
|
||||
void r_print_string(u64 addr, u8 *buf, int len, int step, int columns, int header);
|
||||
|
||||
#endif
|
||||
|
@ -79,6 +79,8 @@ int r_search_strings_update_char(const unsigned char *buf, int min, int max, int
|
||||
/* pattern search */
|
||||
int r_search_pattern(struct r_search_t *s, u32 size);
|
||||
int r_search_strings(struct r_search_t *s, u32 min, u32 max);
|
||||
int r_search_set_callback(struct r_search_t *s, int (*callback)(struct r_search_kw_t *, void *, u64), void *user);
|
||||
int r_search_initialize(struct r_search_t *s);
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -33,6 +33,7 @@ struct r_num_t {
|
||||
};
|
||||
|
||||
void r_num_minmax_swap(u64 *a, u64 *b);
|
||||
void r_num_minmax_swap_i(int *a, int *b);
|
||||
u64 r_num_math(struct r_num_t *num, const char *str);
|
||||
u64 r_num_get(struct r_num_t *num, const char *str);
|
||||
struct r_num_t *r_num_new(u64 (*cb)(void*,const char *,int*), void *ptr);
|
||||
|
90
libr/print/date.c
Normal file
90
libr/print/date.c
Normal file
@ -0,0 +1,90 @@
|
||||
/* radare - LGPL - Copyright 2007-2009 pancake<nopcode.org> */
|
||||
|
||||
#include "r_print.h"
|
||||
|
||||
void print_msdos_date(unsigned char _time[2], unsigned char _date[2])
|
||||
{
|
||||
unsigned int t = _time[1]<<8 | _time[0];
|
||||
unsigned int d = _date[1]<<8 | _date[0];
|
||||
unsigned int year = ((d&0xfe00)>>9)+1980;
|
||||
unsigned int month = (d&0x01e0)>>5;
|
||||
unsigned int day = (d&0x001f)>>0;
|
||||
unsigned int hour = (t&0xf800)>>11;
|
||||
unsigned int minutes = (t&0x07e0)>>5;
|
||||
unsigned int seconds = (t&0x001f)<<1;
|
||||
|
||||
/* la data de modificacio del fitxer, no de creacio del zip */
|
||||
r_cons_printf("%d-%02d-%02d %d:%d:%d",
|
||||
year, month, day, hour, minutes, seconds);
|
||||
}
|
||||
|
||||
void getHTTPDate(char *DATE)
|
||||
{
|
||||
DATE[0]=0;
|
||||
#if __UNIX__
|
||||
struct tm curt; /* current time */
|
||||
time_t l;
|
||||
char week_day[4], month[4];
|
||||
char *week_str[7]= { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
||||
char *month_str[7]= { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
|
||||
"Aug", "Sep", "Oct", "Nov", "Dec" };
|
||||
|
||||
l = time(0);
|
||||
localtime_r(&l, &curt);
|
||||
|
||||
if ((curt.tm_wday <0 || curt.tm_wday > 6)
|
||||
|| (curt.tm_mon < 0 || curt.tm_mon > 11))
|
||||
return;
|
||||
|
||||
sprintf(DATE, "%s, %02d %s %d %02d:%02d:%02d GMT",
|
||||
week_str[curt.tm_wday],
|
||||
curt.tm_mday,
|
||||
month_str[curt.tm_mon],
|
||||
curt.tm_year + 1900, curt.tm_hour,
|
||||
curt.tm_min, curt.tm_sec);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if 0
|
||||
case FMT_TIME_UNIX: {
|
||||
time_t t;
|
||||
char datestr[256];
|
||||
const char *datefmt;
|
||||
for(i=0;!config.interrupted && i<len;i+=4) {
|
||||
endian_memcpy((unsigned char*)&t, config.block+i, sizeof(time_t));
|
||||
//printf("%s", (char *)ctime((const time_t*)&t));
|
||||
datefmt = config_get("cfg.datefmt");
|
||||
|
||||
if (datefmt&&datefmt[0])
|
||||
tmp = strftime(datestr,256,datefmt,
|
||||
(const struct tm*)gmtime((const time_t*)&t));
|
||||
else tmp = strftime(datestr,256,"%d:%m:%Y %H:%M:%S %z",
|
||||
(const struct tm*)gmtime((const time_t*)&t));
|
||||
// TODO colorize depending on the distance between dates
|
||||
if (tmp) cons_printf("%s",datestr); else printf("*failed*");
|
||||
cons_newline();
|
||||
} } break;
|
||||
|
||||
----
|
||||
|
||||
case FMT_TIME_FTIME: {
|
||||
unsigned long long l, L = 0x2b6109100LL;
|
||||
time_t t;
|
||||
char datestr[256];
|
||||
const char *datefmt;
|
||||
for(i=0;!config.interrupted && i<len;i+=8) {
|
||||
endian_memcpy((unsigned char*)&l, config.block+i, sizeof(unsigned long long));
|
||||
l /= 10000000; // 100ns to s
|
||||
l = (l > L ? l-L : 0); // isValidUnixTime?
|
||||
t = (time_t) l; // TODO limit above!
|
||||
datefmt = config_get("cfg.datefmt");
|
||||
if (datefmt&&datefmt[0])
|
||||
tmp = strftime(datestr, 256, datefmt,
|
||||
(const struct tm*)gmtime((const time_t*)&t));
|
||||
else tmp = strftime(datestr, 256, "%d:%m:%Y %H:%M:%S %z",
|
||||
(const struct tm*)gmtime((const time_t*)&t));
|
||||
if (tmp) cons_printf("%s", datestr); else cons_printf("*failed*");
|
||||
cons_newline();
|
||||
} } break;
|
||||
|
||||
#endif
|
@ -12,7 +12,34 @@ void r_print_set_flags(int _flags)
|
||||
flags = _flags;
|
||||
}
|
||||
|
||||
int r_print_cursor = 0;
|
||||
static int r_print_cur_enabled = 0;
|
||||
static int r_print_cur = -1;
|
||||
static int r_print_ocur = -1;
|
||||
|
||||
void r_print_set_cursor(int enable, int ocursor, int cursor)
|
||||
{
|
||||
r_print_cur_enabled = enable;
|
||||
//if (ocursor<0) ocursor=0;
|
||||
r_print_ocur = ocursor;
|
||||
if (cursor<0) cursor=0;
|
||||
r_print_cur = cursor;
|
||||
}
|
||||
|
||||
void r_print_cursor(int cur, int set)
|
||||
{
|
||||
if (!r_print_cur_enabled)
|
||||
return;
|
||||
if (r_print_ocur == -1) {
|
||||
if (cur==r_print_cur)
|
||||
r_cons_invert(set, flags&R_PRINT_FLAGS_COLOR);
|
||||
} else {
|
||||
int from = r_print_ocur;
|
||||
int to = r_print_cur;
|
||||
r_num_minmax_swap_i(&from, &to);
|
||||
if (cur>=from&&cur<=to)
|
||||
r_cons_invert(set, flags&R_PRINT_FLAGS_COLOR);
|
||||
}
|
||||
}
|
||||
|
||||
void r_print_addr(u64 addr)
|
||||
{
|
||||
@ -28,9 +55,9 @@ void r_print_addr(u64 addr)
|
||||
|
||||
void r_print_byte(int idx, u8 ch)
|
||||
{
|
||||
if (flags & R_PRINT_FLAGS_CURSOR && idx == r_print_cursor)
|
||||
r_cons_printf("[%c]", ch);
|
||||
else r_cons_printf("%c", ch);
|
||||
// if (flags & R_PRINT_FLAGS_CURSOR && idx == r_print_cur)
|
||||
r_cons_printf("%c", ch);
|
||||
// else r_cons_printf("%c", ch);
|
||||
}
|
||||
|
||||
void r_print_code(u64 addr, u8 *buf, int len, int step, int columns, int header)
|
||||
@ -39,22 +66,25 @@ void r_print_code(u64 addr, u8 *buf, int len, int step, int columns, int header)
|
||||
r_cons_printf("#define _BUFFER_SIZE %d\n", len);
|
||||
r_cons_printf("unsigned char buffer[%d] = {", len);
|
||||
for(i=0;i<len;i++) {
|
||||
if (!(w%columns)) {
|
||||
if (!(w%columns))
|
||||
r_cons_printf("\n ");
|
||||
}
|
||||
r_print_cursor(i, 1);
|
||||
r_cons_printf("0x%02x, ", buf[i]);
|
||||
r_print_cursor(i, 0);
|
||||
w+=6;
|
||||
}
|
||||
r_cons_printf("}\n");
|
||||
r_cons_printf("};\n");
|
||||
}
|
||||
|
||||
void r_print_string(u64 addr, u8 *buf, int len, int step, int columns, int header)
|
||||
{
|
||||
int i;
|
||||
for(i=0;i<len;i++) {
|
||||
r_print_cursor(i, 1);
|
||||
if (IS_PRINTABLE(buf[i]))
|
||||
r_cons_printf("%c", buf[i]);
|
||||
else r_cons_printf("\\x%02x", buf[i]);
|
||||
r_print_cursor(i, 0);
|
||||
}
|
||||
r_cons_newline();
|
||||
}
|
||||
@ -91,7 +121,9 @@ void r_print_hexdump(u64 addr, u8 *buf, int len, int step, int columns, int head
|
||||
if (j%2) r_cons_printf(" ");
|
||||
continue;
|
||||
}
|
||||
r_print_cursor(j, 1);
|
||||
r_cons_printf("%02x", (u8)buf[j]);
|
||||
r_print_cursor(j, 0);
|
||||
//print_color_byte_i(j, "%02x", (unsigned char)buf[j]);
|
||||
if (j%2) r_cons_strcat(" ");
|
||||
}
|
||||
@ -99,9 +131,13 @@ void r_print_hexdump(u64 addr, u8 *buf, int len, int step, int columns, int head
|
||||
for(j=i; j<i+inc; j++) {
|
||||
if (j >= len)
|
||||
r_cons_strcat(" ");
|
||||
else r_cons_printf("%c",
|
||||
else {
|
||||
r_print_cursor(j, 1);
|
||||
r_cons_printf("%c",
|
||||
(IS_PRINTABLE(buf[j]))?
|
||||
buf[j] : '.');
|
||||
r_print_cursor(j, 0);
|
||||
}
|
||||
}
|
||||
r_cons_newline();
|
||||
addr+=inc;
|
||||
|
@ -38,6 +38,7 @@ int r_search_set_mode(struct r_search_t *s, int mode)
|
||||
case R_SEARCH_REGEXP:
|
||||
case R_SEARCH_PATTERN:
|
||||
case R_SEARCH_STRING:
|
||||
case R_SEARCH_XREFS:
|
||||
case R_SEARCH_AES:
|
||||
s->mode = mode;
|
||||
ret = 1;
|
||||
@ -128,6 +129,9 @@ int r_search_update(struct r_search_t *s, u64 *from, const u8 *buf, u32 len)
|
||||
case R_SEARCH_KEYWORD:
|
||||
r_search_mybinparse_update(s, *from, buf, len);
|
||||
break;
|
||||
case R_SEARCH_XREFS:
|
||||
//r_search_xrefs_update(s, *from, buf, len);
|
||||
break;
|
||||
case R_SEARCH_REGEXP:
|
||||
break;
|
||||
case R_SEARCH_AES:
|
||||
|
461
libr/search/xrefs.c
Normal file
461
libr/search/xrefs.c
Normal file
@ -0,0 +1,461 @@
|
||||
/*
|
||||
* Copyright (C) 2007, 2008
|
||||
* pancake <youterm.com>
|
||||
*
|
||||
* 'xrefs' is part of the radare project.
|
||||
*
|
||||
* xrefs is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* xrefs is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with xrefs; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
=====================================================================
|
||||
|
||||
xrefs - find relative inverse references to an offset inside a file.
|
||||
|
||||
author: pancake <pancake@youterm.com>
|
||||
date: 2006-12-30
|
||||
context: external radare utility
|
||||
|
||||
=====================================================================
|
||||
|
||||
usage example:
|
||||
|
||||
~:$ cat example.c
|
||||
#include <stdio.h>
|
||||
|
||||
void func() {
|
||||
printf("Hello ");
|
||||
}
|
||||
|
||||
int main() {
|
||||
func(); func(); func();
|
||||
}
|
||||
|
||||
~:$ gcc example.c
|
||||
|
||||
~:$ ./a.out
|
||||
Hello Hello Hello
|
||||
|
||||
~:$ objdump -d a.out | grep func
|
||||
10000400 <func>:
|
||||
1000044c: 4b ff ff b5 bl 10000400 <func>
|
||||
10000450: 4b ff ff b1 bl 10000400 <func>
|
||||
10000454: 4b ff ff ad bl 10000400 <func>
|
||||
|
||||
~:$ ./xrefs -a ppc a.out 0x400
|
||||
match value ffffffb5 (ffffb5) at offset 0x44c
|
||||
match value ffffffb1 (ffffb1) at offset 0x450
|
||||
match value ffffffad (ffffad) at offset 0x454
|
||||
|
||||
*/
|
||||
|
||||
/**
|
||||
========================================================================
|
||||
|
||||
XXX and TODO:
|
||||
-------------
|
||||
|
||||
- 64 bit offsets support
|
||||
- set opcode byte (bl == 0x4b, ...)
|
||||
-- set and find the jump/call byte before the offset address
|
||||
|
||||
========================================================================
|
||||
**/
|
||||
|
||||
/* setup 64 bit environment! */
|
||||
#include "radare.h"
|
||||
|
||||
#if __FreeBSD__ || __linux__ || __NetBSD__ || __OpenBSD__
|
||||
#define __UNIX__ 1
|
||||
#else
|
||||
#define __UNIX__ 0
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <getopt.h>
|
||||
#include <limits.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#if __UNIX__
|
||||
#include <sys/mman.h>
|
||||
#include <sys/ioctl.h>
|
||||
#endif
|
||||
|
||||
u32 base = 0;
|
||||
u32 delta = 0;
|
||||
u32 range = 0;
|
||||
u32 xylum = 0;
|
||||
u32 gamme = 0;
|
||||
u32 size = 4;
|
||||
int sysendian = 0; // initialized in main
|
||||
int endian = -1; // little endian by default
|
||||
int verbose = 0;
|
||||
int found = 0;
|
||||
int quite = 0;
|
||||
int arch = ARCH_NULL;
|
||||
unsigned char *ma = NULL;
|
||||
|
||||
static int show_usage()
|
||||
{
|
||||
printf(
|
||||
"Usage: xrefs [-options] [file] [offset]\n"
|
||||
" -v Verbose mode\n"
|
||||
" -V Show version\n"
|
||||
" -q quite mode\n"
|
||||
" -h Show this helpy message\n"
|
||||
" -e Use big endian for offsets to search\n"
|
||||
" -a [arch] Architecture profile (fmi: help) (autodetects ELF and PE hdrs)\n"
|
||||
" -b [address] base address (0x8048000 f.ex)\n"
|
||||
" -f [from] start scanning from this offset (default 0)\n"
|
||||
" -t [to] limit address (default 99999999)\n"
|
||||
" -r [range] Range in bytes of allowed relative offsets\n"
|
||||
" -s [size] Size of offset (4 for intel, 3 for ppc, ...)\n"
|
||||
" -d [offset] Sets a negative delta offset as padding (default 1)\n"
|
||||
" -X [offset] Print out debugging information of a certain relative offset\n");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static u32 file_size_fd(int fd)
|
||||
{
|
||||
u32 curr = lseek(fd, 0, SEEK_CUR);
|
||||
u32 size = lseek(fd, 0, SEEK_END); // XXX: this is not size, is rest!
|
||||
lseek(fd, curr, SEEK_SET);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/* TODO: move+share in offset.c ? */
|
||||
static u32 get_value32(const char *arg)
|
||||
{
|
||||
int i;
|
||||
u32 ret;
|
||||
|
||||
for(i=0;arg[i]==' ';i++);
|
||||
for(;arg[i]=='\\';i++); i++;
|
||||
|
||||
if (arg[i] == 'x')
|
||||
sscanf(arg, "0x%08llx", (u64 *)&ret);
|
||||
else
|
||||
sscanf(arg, "%lld", (u64 *)&ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int get_system_endian()
|
||||
{
|
||||
int a = 1;
|
||||
char *b = (char*)&a;
|
||||
return (int)(b[0]);
|
||||
}
|
||||
|
||||
int set_arch_settings()
|
||||
{
|
||||
switch(arch) {
|
||||
case ARCH_PPC:
|
||||
gamme = 1;
|
||||
delta = 1;
|
||||
size = 3;
|
||||
break;
|
||||
case ARCH_ARM:
|
||||
gamme = -1;
|
||||
delta = 1;
|
||||
size = 3;
|
||||
break;
|
||||
case ARCH_X86:
|
||||
gamme = 1;
|
||||
delta = 0; //-5;
|
||||
size = 4;
|
||||
break;
|
||||
case ARCH_NULL:
|
||||
/* autodetect architecture */
|
||||
// ELF
|
||||
if (!memcmp(ma, "\x7f\x45\x4c\x46", 4)) {
|
||||
short ar = (ma[0x12]<<8) + ma[0x13];
|
||||
switch(ar) {
|
||||
case 0x0300:
|
||||
if (endian==-1)
|
||||
endian = 1;
|
||||
if (!quite)
|
||||
printf("# -a x86\n");
|
||||
arch = ARCH_X86;
|
||||
endian = 1;
|
||||
return 1;
|
||||
case 0x0014:
|
||||
if (endian==-1)
|
||||
endian = 0;
|
||||
if (!quite)
|
||||
printf("# -a ppc\n");
|
||||
arch = ARCH_PPC;
|
||||
return 1;
|
||||
case 0x2800:
|
||||
if (endian==-1)
|
||||
endian = 1;
|
||||
if (!quite)
|
||||
printf("# -a arm\n");
|
||||
arch = ARCH_ARM;
|
||||
return 1;
|
||||
default:
|
||||
printf("Unsupported architecture '%04x'.\n", ar);
|
||||
exit(1);
|
||||
}
|
||||
} else
|
||||
// MZ
|
||||
if (!memcmp(ma, "\x4d\x5a",2)) {
|
||||
unsigned short off = ma[0x3c];
|
||||
if (!memcmp(ma+off, "PE\x00\x00",4)) {
|
||||
unsigned short ar = (ma[off+4]<<8)+ma[off+5];
|
||||
switch(ar) {
|
||||
case 0x4c01: // x86
|
||||
if (endian==-1)
|
||||
endian = 1;
|
||||
printf("# -a x86\n");
|
||||
arch = ARCH_X86;
|
||||
endian = 1;
|
||||
return 1;
|
||||
case 0xc001: // arm
|
||||
if (endian==-1)
|
||||
endian = 1;
|
||||
printf("# -a arm\n");
|
||||
arch = ARCH_ARM;
|
||||
endian = 1;
|
||||
return 1;
|
||||
default:
|
||||
fprintf(stderr, "Unknown architecture.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "Plz. gimmie an architecture. (xrefs -a [arch])\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
u64 i, c, src;
|
||||
u64 offset = 0;
|
||||
u64 from = 1,
|
||||
to = INT_MAX;
|
||||
u64 sa;
|
||||
|
||||
if (argc==2)
|
||||
if (!strcmp(argv[1],"-V")) {
|
||||
printf("%s\n", VERSION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (argc<3)
|
||||
return show_usage();
|
||||
|
||||
/* parse arguments */
|
||||
while ((c = getopt(argc, argv, "qa:d:hves:f:t:r:X:b:")) != -1) {
|
||||
switch( c ) {
|
||||
case 'q':
|
||||
quite = 1;
|
||||
break;
|
||||
case 'a':
|
||||
if (!strcmp(optarg, "intel"))
|
||||
arch = ARCH_X86;
|
||||
else
|
||||
if (!strcmp(optarg, "x86"))
|
||||
arch = ARCH_X86;
|
||||
else
|
||||
if (!strcmp(optarg, "arm"))
|
||||
arch = ARCH_ARM;
|
||||
else
|
||||
if (!strcmp(optarg, "ppc")) {
|
||||
arch = ARCH_PPC;
|
||||
} else {
|
||||
printf("arm ppc x86\n");
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
case 'b':
|
||||
base = get_value32(optarg);
|
||||
break;
|
||||
case 'd':
|
||||
delta = get_value32(optarg);
|
||||
break;
|
||||
case 'X':
|
||||
xylum = get_value32(optarg);
|
||||
break;
|
||||
case 'e':
|
||||
endian = 1;
|
||||
break;
|
||||
case 'r':
|
||||
range = get_value32(optarg);
|
||||
if (range<0) range = -range;
|
||||
break;
|
||||
case 'v':
|
||||
verbose = 1;
|
||||
break;
|
||||
case 'f':
|
||||
from = get_value32(optarg);
|
||||
break;
|
||||
case 't':
|
||||
to = get_value32(optarg);
|
||||
break;
|
||||
case 's':
|
||||
size = get_value32(optarg);
|
||||
break;
|
||||
case 'h':
|
||||
return show_usage();
|
||||
}
|
||||
}
|
||||
|
||||
if (optind+2 != argc) {
|
||||
fprintf(stderr, "Plz. gimmie a file and offset.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* openning stuff */
|
||||
src = open(argv[optind], O_RDONLY);
|
||||
if (src == -1) {
|
||||
fprintf(stderr, "Cannot open file source %s\n", argv[optind]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
offset = get_value32(argv[optind+1]);
|
||||
if (offset >= base)
|
||||
offset -= base;
|
||||
|
||||
sa = file_size_fd(src) - size;
|
||||
#if __UNIX__
|
||||
ma = mmap(NULL, sa, PROT_READ, MAP_SHARED, src, 0);
|
||||
if (sa < 0x50) {
|
||||
fprintf(stderr, "Minimum length is 0x50 bytes.\n");
|
||||
return 1;
|
||||
}
|
||||
#elif __WINDOWS__
|
||||
fprintf(stderr, "Not yet implemented\n");
|
||||
#else
|
||||
fprintf(stderr, "No MMAP for this platform? report it!\n");
|
||||
#endif
|
||||
if (ma == NULL) {
|
||||
perror("Error mmaping");
|
||||
fprintf(stderr, "cannot open %s\n", argv[optind]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* configure environment */
|
||||
sysendian = get_system_endian();
|
||||
|
||||
while( set_arch_settings() );
|
||||
|
||||
if (endian == -1)
|
||||
endian = 0;
|
||||
|
||||
/* loopize looking for xrefs */
|
||||
for(i=from; i<sa && i<to; i++) {
|
||||
u32 value = offset - i + delta;
|
||||
u32 ovalue = value;
|
||||
u32 tmpvalue = 0;
|
||||
unsigned char *buf = (unsigned char *)&value;
|
||||
|
||||
if (range!=0) {
|
||||
if (value<0 && -value>range)
|
||||
continue;
|
||||
else
|
||||
if (value>0 && value>range)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
printf("0x%08llx try %02x %02x %02x %02x (0x%08llx) - %lld\n",
|
||||
(u64)i, buf[0], buf[1], buf[2], buf[3], (u64) base+value, (u64) (base+value));
|
||||
|
||||
if (xylum && i == xylum) {
|
||||
printf("# offset: 0x%08llx\n", (u64)i);
|
||||
printf("# delta: %lld\n", (u64)delta);
|
||||
printf("# size: %lld\n", (u64)size);
|
||||
printf("# value: %lld\n", (u64)value);
|
||||
printf("# bytes: %02x %02x %02x %02x (0x%08llx) - %lld\n",
|
||||
buf[0], buf[1], buf[2], buf[3], (u64)value, (u64)value);
|
||||
tmpvalue = ma[i+gamme];
|
||||
printf("# found: %02x %02x %02x %02x\n",
|
||||
ma[i+gamme+0], ma[i+gamme+1],
|
||||
ma[i+gamme+2], ma[i+gamme+3]);
|
||||
}
|
||||
|
||||
switch(arch) {
|
||||
case ARCH_ARM:
|
||||
value = (value-8)/4;
|
||||
break;
|
||||
case ARCH_X86:
|
||||
value-=5;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// force little endian //
|
||||
if (sysendian) {
|
||||
unsigned char tmp;
|
||||
tmp = buf[0]; buf[0]= buf[3]; buf[3] = tmp;
|
||||
tmp = buf[1]; buf[1]= buf[2]; buf[2] = tmp;
|
||||
}
|
||||
// target architecture endian //
|
||||
if (endian) {
|
||||
unsigned char tmp;
|
||||
tmp = buf[0]; buf[0] = buf[3]; buf[3] = tmp;
|
||||
tmp = buf[1]; buf[1] = buf[2]; buf[2] = tmp;
|
||||
}
|
||||
if (arch==ARCH_ARM) {
|
||||
buf[3] = buf[2]; buf[2] = buf[1]; buf[1] = buf[0];
|
||||
}
|
||||
|
||||
if (xylum && ovalue == xylum) {
|
||||
printf("# buf: %02x %02x %02x %02x (+%lld)\n",
|
||||
buf[0], buf[1], buf[2], buf[3], (u64)(4-size));
|
||||
printf("# map: %02x %02x %02x \n",
|
||||
ma[i+gamme], ma[i+1+gamme], ma[i+2+gamme]);
|
||||
printf("# cmp: %02x %02x %02x\n", ma[i], ma[i+1], ma[i+2]);
|
||||
}
|
||||
|
||||
if (xylum && i == xylum) {
|
||||
printf("# a: %02x %02x %02x %02x\n",
|
||||
ma[i+gamme+0], ma[i+gamme+1],
|
||||
ma[i+gamme+2], ma[i+gamme+3]);
|
||||
printf("# b: %02x %02x %02x %02x\n",
|
||||
buf[0], buf[1], buf[2], buf[3]);
|
||||
}
|
||||
|
||||
if (memcmp((unsigned char *)ma+i+gamme, (unsigned char *)buf+(4-size), size) == 0) {
|
||||
if (quite)
|
||||
printf("0x%08llx\n", (u64)i);
|
||||
else
|
||||
printf("match value 0x%08llx (%02x%02x%02x) at offset 0x%08llx\n",
|
||||
(u64)ovalue,
|
||||
buf[0+(4-size)], buf[1+(4-size)], buf[2+(4-size)],
|
||||
(u64)((u32)i)+((gamme<0)?-1:0));
|
||||
found++;
|
||||
}
|
||||
}
|
||||
|
||||
if (found == 0 && !quite)
|
||||
puts("no matches found.");
|
||||
|
||||
return 0;
|
||||
}
|
@ -32,6 +32,15 @@ void r_num_minmax_swap(u64 *a, u64 *b)
|
||||
}
|
||||
}
|
||||
|
||||
void r_num_minmax_swap_i(int *a, int *b)
|
||||
{
|
||||
if (*a>*b) {
|
||||
u64 tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
void r_num_init(struct r_num_t *num)
|
||||
{
|
||||
num->callback = NULL;
|
||||
|
Loading…
Reference in New Issue
Block a user