- Initial (non working) implementation
  - Added dummy and x86 plugins
* r_core
  - Added 'anal' command
* r_asm
  - Removed aop parser
This commit is contained in:
Nibble 2009-02-16 00:57:03 +01:00
parent cd84224709
commit 26b5e48ede
17 changed files with 229 additions and 102 deletions

View file

@ -2,7 +2,7 @@
PREFIX=../prefix
# Libraries
LIBLIST=io util lib lang flags bin macro hash line cons line print config syscall range socket cmd asm search diff debug reg core
LIBLIST=io util lib lang flags bin macro hash line cons line print config syscall range socket cmd asm anal search diff debug reg core
# Under development

4
libr/anal/Makefile Normal file
View file

@ -0,0 +1,4 @@
NAME=r_anal
OBJ=anal.o
include ../rules.mk

View file

@ -1,19 +1,75 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
/* radare - LGPL - Copyright 2009 pancake<nopcode.org> */
struct r_anal_t *r_anal_new()
{
struct r_anal_t *a = MALLOC_STRUCT(struct r_anal_t);
r_anal_init(a);
return a;
}
#include <r_anal.h>
#include <r_util.h>
void r_anal_free(struct r_anal_t *a)
{
free(a);
}
int r_anal_init(struct r_anal_t *a)
int r_anal_init(struct r_anal_t *anal)
{
anal->user = NULL;
r_anal_set_bits(anal, 32);
r_anal_set_big_endian(anal, R_FALSE);
INIT_LIST_HEAD(&anal->anals);
return R_TRUE;
}
void r_anal_set_user_ptr(struct r_anal_t *anal, void *user)
{
anal->user = user;
}
int r_anal_add(struct r_anal_t *anal, struct r_anal_handle_t *foo)
{
if (foo->init)
foo->init(anal->user);
list_add_tail(&(foo->list), &(anal->anals));
return R_TRUE;
}
int r_anal_list(struct r_anal_t *anal)
{
struct list_head *pos;
list_for_each_prev(pos, &anal->anals) {
struct r_anal_handle_t *h = list_entry(pos, struct r_anal_handle_t, list);
printf(" %s: %s\n", h->name, h->desc);
}
return R_FALSE;
}
int r_anal_set(struct r_anal_t *anal, const char *name)
{
struct list_head *pos;
list_for_each_prev(pos, &anal->anals) {
struct r_anal_handle_t *h = list_entry(pos, struct r_anal_handle_t, list);
if (!memcmp(h->name, name, strlen(h->name))) {
anal->cur = h;
return R_TRUE;
}
}
return R_FALSE;
}
int r_anal_aop(struct r_anal_t *anal)
{
if (anal->cur && anal->cur->aop)
return anal->cur->aop(anal->user);
return R_FALSE;
}
int r_anal_set_bits(struct r_anal_t *anal, int bits)
{
switch (bits) {
case 16:
case 32:
case 64:
anal->bits = bits;
return R_TRUE;
default:
return R_FALSE;
}
}
int r_anal_set_big_endian(struct r_anal_t *anal, int boolean)
{
anal->big_endian = boolean;
return R_TRUE;
}

16
libr/anal/p/Makefile Normal file
View file

@ -0,0 +1,16 @@
CFLAGS=-I../../include -Wall
BINDEPS=
all: anal_dummy.so anal_x86.so
@true
anal_dummy.so: anal_dummy.o
${CC} ${CFLAGS} -fPIC -shared -o anal_dummy.so anal_dummy.c -Wl,-R..
@#strip -s anal_dummy.so
anal_x86.so: anal_x86.o
${CC} ${CFLAGS} -fPIC -shared -o anal_x86.so anal_x86.c -Wl,-R..
@#strip -s anal_x86.so
clean:
-rm -f *.so *.o

25
libr/anal/p/anal_dummy.c Normal file
View file

@ -0,0 +1,25 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <r_types.h>
#include <r_lib.h>
#include <r_anal.h>
static int aop(void *user)
{
printf("Dummy analysis plugin");
return R_FALSE;
}
static struct r_anal_handle_t r_anal_plugin_dummy = {
.name = "dummy",
.desc = "Dummy analysis plugin",
.init = NULL,
.fini = NULL,
.aop = &aop
};
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ANAL,
.data = &r_anal_plugin_dummy
};

View file

@ -1,35 +1,15 @@
/*
* Copyright (C) 2007, 2008
* pancake <youterm.com>
* esteve <eslack.org>
*
* radare 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.
*
* radare 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 radare; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/* code analysis functions */
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <string.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_asm.h>
#include <r_anal.h>
/* code analysis functions */
/* arch_aop for x86 */
// CMP ARG1
// 837d0801 cmp dword [ebp+0x8], 0x1
// SET VAR_41c
@ -43,8 +23,9 @@
// NOTE: buf should be at least 16 bytes!
// XXX addr should be off_t for 64 love
int r_asm_x86_aop(struct r_asm_t *a)
static int aop(void *user)
{
struct r_asm_t *a = (struct r_asm_t*)user;
struct r_anal_aop_t *aop = a->aux;
u8 *buf = a->buf;
@ -420,3 +401,16 @@ int r_asm_x86_aop(struct r_asm_t *a)
return aop->length;
}
static struct r_anal_handle_t r_anal_plugin_x86 = {
.name = "anal_x86",
.desc = "X86 analysis plugin",
.init = NULL,
.fini = NULL,
.aop = &aop
};
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ANAL,
.data = &r_anal_plugin_x86
};

View file

@ -9,7 +9,6 @@ CFLAGS+=-DLIL_ENDIAN=1
# X86
OBJ+=arch/x86/asm.o
OBJ+=arch/x86/pseudo.o
OBJ+=arch/x86/aop.o
OBJ+=arch/x86/realloc.o
# udis86
OBJ+=arch/x86/udis86/syn.o

View file

@ -110,11 +110,6 @@ int r_asm_set_parser(struct r_asm_t *a,
a->r_asm_parse = &r_asm_x86_pseudo;
break;
} else return R_FALSE;
case R_ASM_PAR_AOP:
if (a->arch == R_ASM_ARCH_X86) {
a->r_asm_parse = &r_asm_x86_aop;
break;
} else return R_FALSE;
case R_ASM_PAR_REALLOC:
if (a->arch == R_ASM_ARCH_X86 && a->syntax == R_ASM_SYN_INTEL) {
a->r_asm_parse = &r_asm_x86_realloc;

View file

@ -1,41 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <r_types.h>
#include <r_asm.h>
#include <r_anal.h>
int main()
{
struct r_asm_t a;
struct r_anal_aop_t aop;
u8 *buf = "\x74\x31"
"\x74\x31"
"\x74\x31"
"\xc7\xc0\x04\x00\x00\x00";
int ret = 0;
u64 idx = 0, len = 12;
r_asm_init(&a);
r_asm_set_arch(&a, R_ASM_ARCH_X86);
r_asm_set_bits(&a, 32);
r_asm_set_big_endian(&a, R_FALSE);
r_asm_set_syntax(&a, R_ASM_SYN_INTEL);
r_asm_set_parser(&a, R_ASM_PAR_AOP, NULL, &aop);
while (idx < len) {
r_asm_set_pc(&a, 0x8048000 + idx);
ret = r_asm_disasm(&a, buf+idx, len-idx);
printf("DISASM %s HEX %s\n", a.buf_asm, a.buf_hex);
r_asm_parse(&a);
printf("AOP_JMP 0x%08llx\nAOP_FAIL 0x%08llx\n\n",
aop.jump, aop.fail);
idx += ret;
}
return 0;
}

View file

@ -1,5 +1,5 @@
NAME=r_core
DEPS=r_config r_cons r_line r_io r_cmd r_util r_print r_flags r_asm r_lib r_macro r_debug r_hash r_bin r_lang r_io r_asm r_config r_macro r_print
DEPS=r_config r_cons r_line r_io r_cmd r_util r_print r_flags r_asm r_lib r_macro r_debug r_hash r_bin r_lang r_io r_asm r_anal r_config r_macro r_print
OBJ=core.o cmd.o file.o config.o visual.o io.o

View file

@ -327,6 +327,37 @@ static int cmd_flag(void *data, const char *input)
return 0;
}
static int cmd_anal(void *data, const char *input)
{
struct r_core_t *core = (struct r_core_t *)data;
int l, len = core->blocksize;
u32 tbs = core->blocksize;
if (input[0] && input[1]) {
l = (int) r_num_get(&core->num, input+2);
if (l>0) len = l;
if (l>tbs) {
r_core_block_size(core, l);
len = l;
}
}
switch(input[0]) {
case '\0':
r_anal_list(&core->anal);
break;
case 'o':
break;
default:
fprintf(stderr, "Usage: a[o] [len]\n"
" ao [len] Analyze raw bytes\n");
break;
}
if (tbs != core->blocksize)
r_core_block_size(core, tbs);
return 0;
}
/* TODO: simplify using r_write */
static int cmd_write(void *data, const char *input)
{
@ -968,6 +999,7 @@ int r_core_cmd_init(struct r_core_t *core)
r_cmd_init(&core->cmd);
r_cmd_set_data(&core->cmd, core);
r_cmd_add(&core->cmd, "x", "alias for px", &cmd_hexdump);
r_cmd_add(&core->cmd, "anal", "analysis", &cmd_anal);
r_cmd_add(&core->cmd, "flag", "get/set flags", &cmd_flag);
r_cmd_add(&core->cmd, "debug", "debugger operations", &cmd_debug);
r_cmd_add(&core->cmd, "info", "get file info", &cmd_info);

View file

@ -56,10 +56,10 @@ int __lib_dbg_cb(struct r_lib_plugin_t *pl, void *user, void *data)
int __lib_dbg_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; }
/* debug callback */
/* lang callback */
int __lib_lng_cb(struct r_lib_plugin_t *pl, void *user, void *data)
{
struct r_debug_handle_t *hand = (struct r_debug_handle_t *)data;
struct r_lang_handle_t *hand = (struct r_lang_handle_t *)data;
struct r_core_t *core = (struct r_core_t *)user;
//printf(" * Added language handler\n");
r_lang_add(&core->lang, hand);
@ -68,6 +68,18 @@ int __lib_lng_cb(struct r_lib_plugin_t *pl, void *user, void *data)
int __lib_lng_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; }
/* anal callback */
int __lib_anl_cb(struct r_lib_plugin_t *pl, void *user, void *data)
{
struct r_anal_handle_t *hand = (struct r_anal_handle_t *)data;
struct r_core_t *core = (struct r_core_t *)user;
//printf(" * Added language handler\n");
r_anal_add(&core->anal, hand);
return R_TRUE;
}
int __lib_anl_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; }
int r_core_init(struct r_core_t *core)
{
core->oobi = NULL;
@ -76,6 +88,7 @@ int r_core_init(struct r_core_t *core)
core->num.userptr = core;
r_lang_init(&core->lang);
r_lang_set_user_ptr(&core->lang, core);
r_anal_init(&core->anal);
r_cons_init();
core->search = r_search_new(R_SEARCH_KEYWORD);
r_io_init(&core->io);
@ -99,6 +112,8 @@ int r_core_init(struct r_core_t *core)
&__lib_dbg_cb, &__lib_dbg_dt, core);
r_lib_add_handler(&core->lib, R_LIB_TYPE_LANG, "language plugins",
&__lib_lng_cb, &__lib_lng_dt, core);
r_lib_add_handler(&core->lib, R_LIB_TYPE_ANAL, "analysis plugins",
&__lib_anl_cb, &__lib_anl_dt, core);
r_lib_opendir(&core->lib, getenv("LIBR_PLUGINS"));
{
char *homeplugindir = r_str_home(".radare/plugins");

View file

@ -1,6 +1,6 @@
OBJ=radare2.o
BIN=radare2
BINDEPS=r_core r_cons r_macro r_util r_flags r_lib r_io r_hash r_cmd r_config r_asm r_lang r_debug r_print r_line r_bin r_search
BINDEPS=r_core r_cons r_macro r_util r_flags r_lib r_io r_hash r_cmd r_config r_asm r_anal r_lang r_debug r_print r_line r_bin r_search
#LIBS=-lr_core -L..
### shared

View file

@ -4,6 +4,22 @@
#define _INCLUDE_R_ANAL_H_
#include "r_types.h"
#include "list.h"
enum {
R_ANAL_ARCH_NULL = 0,
R_ANAL_ARCH_X86,
R_ANAL_ARCH_ARM,
R_ANAL_ARCH_PPC,
R_ANAL_ARCH_M68K,
R_ANAL_ARCH_JAVA,
R_ANAL_ARCH_MIPS,
R_ANAL_ARCH_SPARC,
R_ANAL_ARCH_CSR,
R_ANAL_ARCH_MSIL,
R_ANAL_ARCH_OBJD,
R_ANAL_ARCH_BF
};
enum {
R_ANAL_AOP_TYPE_NULL = 0,
@ -65,6 +81,23 @@ enum {
R_ANAL_STACK_ARG_SET
};
struct r_anal_t {
int bits;
int big_endian;
void *user;
struct r_anal_handle_t *cur;
struct list_head anals;
};
struct r_anal_handle_t {
char *name;
char *desc;
int (*init)(void *user);
int (*fini)(void *user);
int (*aop)(void *user);
struct list_head list;
};
struct r_anal_aop_t {
int type; /* type of opcode */
int stackop; /* operation on stack? */
@ -78,15 +111,14 @@ struct r_anal_aop_t {
u64 i_dst,i_src1,i_src2; /* inmediate arguments */
};
#if 0
struct r_anal_t {
};
#endif
/* anal.c */
struct r_anal_t *r_anal_new();
void r_anal_free(struct r_anal_t *a);
int r_anal_init(struct r_anal_t *a);
int r_anal_init(struct r_anal_t *anal);
void r_anal_set_user_ptr(struct r_anal_t *anal, void *user);
int r_anal_add(struct r_anal_t *anal, struct r_anal_handle_t *foo);
int r_anal_list(struct r_anal_t *anal);
int r_anal_set(struct r_anal_t *anal, const char *name);
int r_anal_aop(struct r_anal_t *anal);
int r_anal_set_bits(struct r_anal_t *anal, int bits);
int r_anal_set_big_endian(struct r_anal_t *anal, int boolean);
#endif

View file

@ -30,7 +30,6 @@ enum {
enum {
R_ASM_PAR_NULL = 0,
R_ASM_PAR_PSEUDO,
R_ASM_PAR_AOP,
R_ASM_PAR_REALLOC
};
@ -73,8 +72,6 @@ int r_asm_x86_disasm(struct r_asm_t *a, u8 *buf, u64 len);
int r_asm_x86_asm(struct r_asm_t *a, char *buf);
/* arch/x86/pseudo.c */
int r_asm_x86_pseudo(struct r_asm_t *a);
/* arch/x86/aop.c */
int r_asm_x86_aop(struct r_asm_t *a);
/* arch/x86/realloc.c */
struct r_asm_realloc_t {
u64 offset;

View file

@ -5,6 +5,7 @@
#include "r_io.h"
#include "r_lib.h"
#include "r_lang.h"
#include "r_anal.h"
#include "r_cmd.h"
#include "r_cons.h"
#include "r_line.h"
@ -41,6 +42,7 @@ struct r_core_t {
struct r_num_t num;
struct r_lib_t lib;
struct r_cmd_t cmd;
struct r_anal_t anal;
struct r_lang_t lang;
struct r_debug_t dbg;
struct r_flag_t flags;

View file

@ -41,6 +41,7 @@ enum {
R_LIB_TYPE_LANG, /* language */
R_LIB_TYPE_ASM, /* assembler */
//R_LIB_TYPE_DIS, /* disassembler */
R_LIB_TYPE_ANAL, /* analysis */
};
struct r_lib_t {