* Implement STATIC PLUGIN support for r_parse

- Use the new 'asm-like' build system for r_parse plugins
  - Added new callback to 'assemble' parseable expressions
    into compilable asm code
  - Refactorize and remove warnings in parse_mreplace
* Added r_str_char_count() in r_util
* Some fixups in the fastcall code in r_asm

--HG--
rename : libr/parse/p/mreplace/mmemory.c => libr/parse/p/parse_mreplace/mmemory.c
rename : libr/parse/p/mreplace/mmemory.h => libr/parse/p/parse_mreplace/mmemory.h
rename : libr/parse/p/mreplace/mreplace.c => libr/parse/p/parse_mreplace/mreplace.c
rename : libr/parse/p/mreplace/mreplace.h => libr/parse/p/parse_mreplace/mreplace.h
This commit is contained in:
pancake 2009-08-14 01:44:12 +00:00
parent bc1b1b976e
commit 65da25d4c0
26 changed files with 170 additions and 46 deletions

View File

@ -12,6 +12,7 @@
#include "dis-asm.h"
#include "opcode/mips.h"
#include "fastcall_mips.h"
static int mips_mode = 0;
static unsigned long Offset = 0;
@ -99,7 +100,8 @@ struct r_asm_handle_t r_asm_plugin_mips = {
.init = NULL,
.fini = NULL,
.disassemble = &disassemble,
.assemble = NULL
.assemble = NULL,
.fastcall = fastcall
};
#ifndef CORELIB

View File

@ -42,7 +42,7 @@ struct r_asm_handle_t r_asm_plugin_x86 = {
.fini = NULL,
.disassemble = &disassemble,
.assemble = NULL,
.fastcall = &fastcall,
.fastcall = fastcall,
};
#ifndef CORELIB

View File

@ -10,6 +10,8 @@
#include "x86/bea/BeaEngine.h"
#include "fastcall_x86.h"
static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, ut8 *buf, ut64 len)
{
@ -22,8 +24,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, ut8 *buf, ut6
disasm_obj.SecurityBlock = len;
if (a->syntax == R_ASM_SYN_ATT)
disasm_obj.Options = 0x400;
else
disasm_obj.Options = 0;
else disasm_obj.Options = 0;
aop->inst_len = Disasm(&disasm_obj);
aop->disasm_obj = &disasm_obj;
@ -33,8 +34,6 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, ut8 *buf, ut6
return aop->inst_len;
}
#include "fastcall_x86.h"
struct r_asm_handle_t r_asm_plugin_x86_bea = {
.name = "asm_x86_bea",
.desc = "X86 disassembly plugin (bea engine)",
@ -44,7 +43,7 @@ struct r_asm_handle_t r_asm_plugin_x86_bea = {
.fini = NULL,
.disassemble = &disassemble,
.assemble = NULL,
.fastcall = &fastcall,
.fastcall = fastcall,
};
#ifndef CORELIB

View File

@ -62,7 +62,7 @@ struct r_asm_handle_t r_asm_plugin_x86_olly = {
.fini = NULL,
.disassemble = &disassemble,
.assemble = &assemble,
.fastcall = &fastcall,
.fastcall = fastcall,
};
#ifndef CORELIB

View File

@ -1,4 +1,4 @@
static struct r_asm_fastcall_t fastcall[R_ASM_FASTCALL_ARGS] = {
static struct r_asm_fastcall_t fastcall [R_ASM_FASTCALL_ARGS] = {
{ NULL },
{ "eax", NULL },
{ "eax", "ebx", NULL },

View File

@ -3,8 +3,6 @@ BIN=rasm2
BINDEPS=r_util r_lib r_asm r_print r_cons
LIBS+=-ldl
include ../../rules.mk
all: ${BIN} fastcall
fastcall: fastcall.o

View File

@ -16,6 +16,12 @@
&r_asm_plugin_mips, \
0
#define R_PARSE_STATIC_PLUGINS \
&r_parse_plugin_dummy, \
&r_parse_plugin_x86_pseudo, \
&r_parse_plugin_mreplace, \
0
#define R_BIN_STATIC_PLUGINS \
&r_bin_plugin_elf , \
&r_bin_plugin_elf64 , \

View File

@ -10,6 +10,7 @@ USE_RIO=1
STATIC_DEBUG=0
RUNTIME_DEBUG=1
STATIC_ASM_PLUGINS=p/x86olly.mk p/x86nasm.mk p/mips.mk p/java.mk
STATIC_PARSE_PLUGINS=p/dummy.mk p/x86_pseudo.mk p/mreplace.mk
STATIC_BIN_PLUGINS=p/elf.mk p/elf64.mk p/pe.mk p/pe64.mk p/java.mk p/dummy.mk
STATIC_BP_PLUGINS=p/x86.mk
STATIC_BININFO_PLUGINS=p/addr2line.mk

View File

@ -21,6 +21,7 @@ struct r_parse_handle_t {
int (*init)(void *user);
int (*fini)(void *user);
int (*parse)(struct r_parse_t *p, void *data, char *str);
int (*assemble)(struct r_parse_t *p, void *data, char *str);
struct list_head list;
};
@ -34,4 +35,9 @@ int r_parse_list(struct r_parse_t *p);
int r_parse_set(struct r_parse_t *p, const char *name);
int r_parse_parse(struct r_parse_t *p, void *data, char *str);
/* plugin pointers */
extern struct r_parse_handle_t r_parse_plugin_dummy;
extern struct r_parse_handle_t r_parse_plugin_x86_pseudo;
extern struct r_parse_handle_t r_parse_plugin_mreplace;
#endif

View File

@ -72,6 +72,7 @@ R_API void r_num_init(struct r_num_t *num);
R_API const char *r_str_ansi_chrn(const char *str, int n);
R_API int r_str_ansi_len(const char *str);
R_API int r_str_word_count(const char *string);
R_API int r_str_char_count(const char *string, char ch);
R_API int r_str_word_set0(char *str);
R_API char *r_str_word_get0(char *str, int idx);
R_API char *r_str_word_get_first(const char *string);

View File

@ -1,4 +1,19 @@
NAME=r_parse
OBJ=parse.o
DEPS=r_lib
foo: pre libr_parse.so plugins
CFLAGS+=-DCORELIB
include ../config.mk
include ${STATIC_PARSE_PLUGINS}
STATIC_OBJS=$(subst ..,p/..,$(subst parse_,p/parse_,$(STATIC_OBJ)))
OBJ=parse.o ${STATIC_OBJS}
pre:
@if [ ! -e libr_parse.so ]; then rm -f ${STATIC_OBJS} ; fi
plugins:
cd p && ${MAKE} all
include ../rules.mk

View File

@ -1,21 +1,41 @@
CFLAGS=-I../../include -I../arch/ -I../arch/include -Wall -fPIC -shared -Wl,-R..
CFLAGS_IMP=-I../../include -I../arch/ -I../arch/include -w -fPIC -shared -Wl,-R..
CFLAGS=-I../../include -Wall -fPIC -shared -Wl,-R..
# XXX
CFLAGS+=-DLIL_ENDIAN=1 -D__UNIX__
CFLAGS+=-DCORELIB
all: parse_dummy.so parse_x86_pseudo.so parse_mreplace.so
foo: all
ALL_TARGETS=
ARCHS=dummy.mk x86_pseudo.mk mreplace.mk
include $(ARCHS)
all: ${ALL_TARGETS}
@true
parse_dummy.so: parse_dummy.o
${CC} ${CFLAGS} -o parse_dummy.so parse_dummy.o
@#strip -s parse_dummy.so
parse_x86_pseudo.so: parse_x86_pseudo.o
${CC} ${CFLAGS} -o parse_x86_pseudo.so parse_x86_pseudo.o
@#strip -s parse_x86_pseudo.so
parse_mreplace.so: parse_mreplace.o
${CC} ${CFLAGS_IMP} -o parse_mreplace.so \
mreplace/mreplace.c mreplace/mmemory.c parse_mreplace.o
@#strip -s parse_mreplace.so
clean:
-rm -f *.so *.o
-rm -f *.so *.o ${STATIC_OBJ}
.PHONY: all clean foo
#CFLAGS=-I../../include -I../arch/ -I../arch/include -Wall -fPIC -shared -Wl,-R..
#CFLAGS_IMP=-I../../include -I../arch/ -I../arch/include -w -fPIC -shared -Wl,-R..
#
#all: parse_dummy.so parse_x86_pseudo.so parse_mreplace.so
# @true
#
#parse_dummy.so: parse_dummy.o
# ${CC} ${CFLAGS} -o parse_dummy.so parse_dummy.o
# @#strip -s parse_dummy.so
#
#parse_x86_pseudo.so: parse_x86_pseudo.o
# ${CC} ${CFLAGS} -o parse_x86_pseudo.so parse_x86_pseudo.o
# @#strip -s parse_x86_pseudo.so
#
#parse_mreplace.so: parse_mreplace.o
# ${CC} ${CFLAGS_IMP} -o parse_mreplace.so \
# mreplace/mreplace.c mreplace/mmemory.c parse_mreplace.o
# @#strip -s parse_mreplace.so
#
#clean:
# -rm -f *.so *.o

10
libr/parse/p/dummy.mk Normal file
View File

@ -0,0 +1,10 @@
OBJ_DUMMY+=parse_dummy.o
TARGET_DUMMY=parse_dummy.so
ALL_TARGETS+=${TARGET_DUMMY}
STATIC_OBJ+=${OBJ_DUMMY}
${TARGET_DUMMY}: ${OBJ_DUMMY}
${CC} ${CFLAGS} -o ${TARGET_DUMMY} ${OBJ_DUMMY}
@#strip -s asm_dummy.so

10
libr/parse/p/mreplace.mk Normal file
View File

@ -0,0 +1,10 @@
OBJ_MREPLACE+=parse_mreplace.o parse_mreplace/mreplace.o parse_mreplace/mmemory.o
TARGET_MREPLACE=parse_mreplace.so
ALL_TARGETS+=${TARGET_MREPLACE}
STATIC_OBJ+=${OBJ_MREPLACE}
${TARGET_MREPLACE}: ${OBJ_MREPLACE}
${CC} ${CFLAGS} -o ${TARGET_MREPLACE} ${OBJ_MREPLACE}
@#strip -s asm_dummy.so

View File

@ -12,7 +12,7 @@ static int parse(struct r_parse_t *p, void *data, char *str)
return R_FALSE;
}
static struct r_parse_handle_t r_parse_plugin_parse_dummy = {
struct r_parse_handle_t r_parse_plugin_dummy = {
.name = "parse_dummy",
.desc = "dummy parsing plugin",
.init = NULL,
@ -20,7 +20,9 @@ static struct r_parse_handle_t r_parse_plugin_parse_dummy = {
.parse = &parse,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_PARSE,
.data = &r_parse_plugin_parse_dummy
.data = &r_parse_plugin_dummy
};
#endif

View File

@ -3,7 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "mreplace/mreplace.h"
#include "parse_mreplace/mreplace.h"
#include <r_lib.h>
#include <r_parse.h>
@ -27,7 +27,7 @@ static int parse(struct r_parse_t *p, void *data, char *str)
return R_TRUE;
}
static struct r_parse_handle_t r_parse_plugin_parse_mreplace = {
struct r_parse_handle_t r_parse_plugin_mreplace = {
.name = "parse_mreplace",
.desc = "mreplace parsing plugin",
.init = NULL,
@ -35,7 +35,10 @@ static struct r_parse_handle_t r_parse_plugin_parse_mreplace = {
.parse = &parse,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_PARSE,
.data = &r_parse_plugin_parse_mreplace
.data = &r_parse_plugin_mreplace
};
#endif

View File

@ -102,7 +102,6 @@ void memCopy(memChunk *dest,memChunk *source){
}
void memStrCat(memChunk *dest,char *string){
long nbytes;
memChunk result,*temp;
temp = memReserve(dest->size+strlen(string)+1);

View File

@ -62,15 +62,15 @@ void sreplace(char *s,char *orig,char *rep,char multi,long dsize){
char *mreplace(char *string, char *se,char *rep)
{
int status,m,i;
char *s,noMatch=0;
int status,i;
char noMatch=0;
regex_t re;
size_t nmatch = 16;
regmatch_t pm[nmatch];
char regError[64],*p;
char *p;
ulong offset = 0;
char field[16];
char *sData,*res;
char *res;
memChunk *search,*temp,*found,*ffound;
if(!string) return "";
@ -92,7 +92,7 @@ char *mreplace(char *string, char *se,char *rep)
if(regcomp(&re, search->address, REG_EXTENDED) != 0)
if(regcomp(&re, search->address, REG_EXTENDED<<1)) noMatch=1;
if(status = regexec(&re, string, nmatch, pm, 0)) noMatch=1;
if((status = regexec(&re, string, nmatch, pm, 0))) noMatch=1;
if(noMatch){
memFree(temp);
@ -104,7 +104,7 @@ char *mreplace(char *string, char *se,char *rep)
ffound = memReserve(INPUTLINE_BUFFER_REPLACE_SIZE);
while(!status){
offset=strlen(temp->address)-strlen(string);
snprintf(found->address,INPUTLINE_BUFFER_REPLACE_SIZE,"%.*s",pm[0].rm_eo - pm[0].rm_so, &string[pm[0].rm_so],&string[pm[0].rm_so]);
snprintf(found->address,INPUTLINE_BUFFER_REPLACE_SIZE,"%.*s",pm[0].rm_eo - pm[0].rm_so, &string[pm[0].rm_so]);//,&string[pm[0].rm_so]);
#if MDEBUG3
printf("------->> found \"%s\" length => %d offset[%d]\n",
found->address,
@ -112,7 +112,7 @@ char *mreplace(char *string, char *se,char *rep)
#endif
sreplace(temp->address+offset,found->address,rep,0,INPUTLINE_BUFFER_REPLACE_SIZE-offset);
for(i=1;i<nmatch;i++){
snprintf(ffound->address,INPUTLINE_BUFFER_REPLACE_SIZE,"%.*s",pm[i].rm_eo - pm[i].rm_so, &string[pm[i].rm_so],&string[pm[i].rm_so]);
snprintf(ffound->address,INPUTLINE_BUFFER_REPLACE_SIZE,"%.*s",pm[i].rm_eo - pm[i].rm_so, &string[pm[i].rm_so]);//,&string[pm[i].rm_so]);
snprintf(field,sizeof(field),"\\%d",i);
if(strlen(ffound->address)) {
sreplace(temp->address,field,ffound->address,1,INPUTLINE_BUFFER_REPLACE_SIZE);
@ -147,11 +147,10 @@ char *mreplace(char *string, char *se,char *rep)
}
char *treplace(char *data,char *search,char *replace){
long offset=0,f;
char *newline,*p;
memChunk *result,*line;
ulong resultAllocSize;
//ulong resultAllocSize;
if(!strlen(search)) return data;

View File

@ -123,15 +123,24 @@ static int parse(struct r_parse_t *p, void *data, char *str)
return R_TRUE;
}
static struct r_parse_handle_t r_parse_plugin_parse_x86_pseudo = {
static int assemble(struct r_parse_t *p, void *data, char *str)
{
printf("assembling '%s' to generate real asm code\n", str);
return R_TRUE;
}
struct r_parse_handle_t r_parse_plugin_x86_pseudo = {
.name = "parse_x86_pseudo",
.desc = "X86 pseudo syntax",
.init = NULL,
.fini = NULL,
.parse = &parse,
.assemble = &assemble,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_PARSE,
.data = &r_parse_plugin_parse_x86_pseudo
.data = &r_parse_plugin_x86_pseudo
};
#endif

View File

@ -0,0 +1,10 @@
OBJ_X86PSEUDO+=parse_x86_pseudo.o
TARGET_X86PSEUDO=parse_x86_pseudo.so
ALL_TARGETS+=${TARGET_X86PSEUDO}
STATIC_OBJ+=${OBJ_X86PSEUDO}
${TARGET_X86PSEUDO}: ${OBJ_X86PSEUDO}
${CC} ${CFLAGS} -o ${TARGET_X86PSEUDO} ${OBJ_X86PSEUDO}
@#strip -s asm_dummy.so

View File

@ -5,6 +5,10 @@
#include <r_types.h>
#include <r_parse.h>
#include <list.h>
#include "../config.h"
static struct r_parse_handle_t *parse_static_plugins[] =
{ R_PARSE_STATIC_PLUGINS };
struct r_parse_t *r_parse_new()
{
@ -20,8 +24,11 @@ void r_parse_free(struct r_parse_t *p)
int r_parse_init(struct r_parse_t *p)
{
int i;
p->user = NULL;
INIT_LIST_HEAD(&p->parsers);
for(i=0;parse_static_plugins[i];i++)
r_parse_add(p, parse_static_plugins[i]);
return R_TRUE;
}

6
libr/parse/t/Makefile Normal file
View File

@ -0,0 +1,6 @@
OBJ=parse.o
BIN=parse
BINDEPS=r_util r_lib r_parse
LIBS+=-ldl
include ../../rules.mk

12
libr/parse/t/parse.c Normal file
View File

@ -0,0 +1,12 @@
#include "r_parse.h"
int main()
{
struct r_parse_t *p;
p = r_parse_new();
printf("List: \n");
r_parse_list(p);
printf("Using plugin: \n");
r_parse_set(p, "parse_x86_pseudo");
return 0;
}

View File

@ -76,6 +76,15 @@ R_API char *r_str_word_get0(char *str, int idx)
return ptr;
}
R_API int r_str_char_count(const char *string, char ch)
{
int i, count=0;
for(i=0;string[i];i++)
if (string[i]==ch)
count++;
return count;
}
R_API int r_str_word_count(const char *string)
{
char *text, *tmp;