diff --git a/libr/asm/arch/bf/asm.c b/libr/asm/arch/bf/asm.c deleted file mode 100644 index c25ce30162..0000000000 --- a/libr/asm/arch/bf/asm.c +++ /dev/null @@ -1,64 +0,0 @@ -/* radare - LGPL - Copyright 2009 pancake - nibble<.ds@gmail.com> */ - -#include -#include - -#include -#include - - -int r_asm_bf_disasm(struct r_asm_t *a, u8 *buf, u64 len) -{ - int i; - char *buf_cp, *b; - - if ((b = buf_cp = alloca(len+1)) == NULL) - return 0; - memcpy(buf_cp, buf, len+1); - - for(i=0;b[0] == b[1] && ibuf_hex, buf_cp, 256); - - switch(buf[0]) { - case '[': - strcpy(a->buf_asm, "[ loop {"); - break; - case ']': - strcpy(a->buf_asm, "] }"); // TODO: detect clause and put label name - break; - case '>': - if (i>1) strcpy(a->buf_asm, "> add [ptr]"); - else strcpy(a->buf_asm, "> inc [ptr]"); - break; - case '<': - if (i>1) strcpy(a->buf_asm, "< sub [ptr]"); - else strcpy(a->buf_asm, "< dec [ptr]"); - break; - case '+': - if (i>1) strcpy(a->buf_asm, "+ add [ptr]"); - else strcpy(a->buf_asm, "+ inc [ptr]"); - break; - case '-': - if (i>1) strcpy(a->buf_asm, "- sub [ptr]"); - else strcpy(a->buf_asm, "- dec [ptr]"); - break; - case ',': - strcpy(a->buf_asm, ", [ptr] = getch()"); - break; - case '.': - strcpy(a->buf_asm, ". print( [ptr] )"); - break; - case '\x00': - strcpy(a->buf_asm, " trap"); - break; - default: - strcpy(a->buf_asm, " nop"); - break; - } - - if (i>0) sprintf(a->buf_asm, "%s, %d", a->buf_asm, i+1); - if (i<1) i=1; else i++; - - return i; -} diff --git a/libr/asm/arch/csr/asm.c b/libr/asm/arch/csr/asm.c deleted file mode 100644 index 0160ae8a96..0000000000 --- a/libr/asm/arch/csr/asm.c +++ /dev/null @@ -1,19 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include - -#include -#include -#include - -#include "csr_disasm/dis.h" - -int r_asm_csr_disasm(struct r_asm_t *a, u8 *buf, u64 len) -{ - r_hex_bin2str((u8*)buf, 2, a->buf_hex); - arch_csr_disasm(a->buf_asm, buf, a->pc); - memcpy(a->buf, buf, 2); - a->inst_len = 2; - - return a->inst_len; -} diff --git a/libr/asm/arch/m68k/asm.c b/libr/asm/arch/m68k/asm.c deleted file mode 100644 index e25fdd4fd8..0000000000 --- a/libr/asm/arch/m68k/asm.c +++ /dev/null @@ -1,33 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include -#include - -#include -#include -#include - -#include "m68k_disasm/m68k_disasm.h" - -int r_asm_m68k_disasm(struct r_asm_t *a, u8 *buf, u64 len) -{ - m68k_word bof[4]; - m68k_word iaddr = (m68k_word)a->pc; - char opcode[256]; - char operands[256]; - - struct DisasmPara_68k dp; - /* initialize DisasmPara */ - memcpy(bof, buf, 4); - dp.opcode = opcode; - dp.operands = operands; - dp.iaddr = &iaddr; - dp.instr = bof; - M68k_Disassemble(&dp); - sprintf(a->buf_asm, "%s %s", opcode, operands); - r_hex_bin2str((u8*)bof, 4, a->buf_hex); - memcpy(a->buf, bof, 4); - a->inst_len = 4; - - return a->inst_len; -} diff --git a/libr/asm/arch/ppc/asm.c b/libr/asm/arch/ppc/asm.c deleted file mode 100644 index 01de70c12b..0000000000 --- a/libr/asm/arch/ppc/asm.c +++ /dev/null @@ -1,33 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include -#include - -#include -#include -#include - -#include "ppc_disasm/ppc_disasm.h" - -int r_asm_ppc_disasm(struct r_asm_t *a, u8 *buf, u64 len) -{ - ppc_word iaddr = (ppc_word)a->pc; - ppc_word bof[4]; - char opcode[128]; - char operands[128]; - - struct DisasmPara_PPC dp; - /* initialize DisasmPara */ - memcpy(bof, buf, 4); - dp.opcode = opcode; - dp.operands = operands; - dp.iaddr = &iaddr; - dp.instr = bof; - PPC_Disassemble(&dp, a->big_endian); - r_hex_bin2str((u8*)bof, 4, a->buf_hex); - sprintf(a->buf_asm, "%s %s", opcode, operands); - memcpy(a->buf, bof, 4); - a->inst_len = 4; - - return a->inst_len; -} diff --git a/libr/asm/arch/x86/realloc.c b/libr/asm/arch/x86/realloc.c deleted file mode 100644 index 615fd87051..0000000000 --- a/libr/asm/arch/x86/realloc.c +++ /dev/null @@ -1,127 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include -#include -#include - -#include -#include -#include - -static int r_asm_x86_deltify(int argc, const char *argv[], char *newstr) -{ - int i,j,k; - struct { - char *op; - char *str; - } ops[] = { - { "cmp", "cmp 1, 2"}, - { "test", "cmp 1, 2"}, - { "lea", "1 = 2"}, - { "mov", "1 = 2"}, - { "cmovl","ifnot zf,1 = 2"}, - { "xor", "1 ^= 2"}, - { "and", "1 &= 2"}, - { "or", "1 |= 2"}, - { "add", "1 += 2"}, - { "sub", "1 -= 2"}, - { "mul", "1 *= 2"}, - { "div", "1 /= 2"}, - { "call", "call 1"}, - { "jmp", "goto 1"}, - { "je", "je 1"}, - { "push", "push 1"}, - { "pop", "pop 1"}, - { "ret", "ret"}, - { NULL } - }; - - for(i=0;ops[i].op != NULL;i++) { - if (!strcmp(ops[i].op, argv[0])) { - if (newstr != NULL) { - for(j=k=0;ops[i].str[j]!='\0';j++,k++) { - if (ops[i].str[j]>='0' && ops[i].str[j]<='9') { - const char *w = argv[ ops[i].str[j]-'0' ]; - if (w != NULL) { - strcpy(newstr+k, w); - k += strlen(w)-1; - } - } else newstr[k] = ops[i].str[j]; - } - newstr[k]='\0'; - } - return R_TRUE; - } - } - - if (newstr != NULL) { - newstr[0] = '\0'; - for (i=0; iaux; - int i, len = strlen(a->buf_asm); - char w0[32]; - char w1[32]; - char w2[32]; - char w3[32]; - char *str, *ptr, *optr; - - if ((str = alloca(len+1)) == NULL) - return R_FALSE; - memcpy(str, a->buf_asm, len+1); - - if (str[0]!='\0') { - w0[0]='\0'; - w1[0]='\0'; - w2[0]='\0'; - w3[0]='\0'; - ptr = strchr(str, ' '); - if (ptr == NULL) - ptr = strchr(str, '\t'); - if (ptr) { - ptr[0]='\0'; - for(ptr=ptr+1;ptr[0]==' ';ptr=ptr+1); - strcpy(w0, str); - strcpy(w1, ptr); - - optr=ptr; - ptr = strchr(ptr, ','); - if (ptr) { - ptr[0]='\0'; - for(ptr=ptr+1;ptr[0]==' ';ptr=ptr+1); - strcpy(w1, optr); - strcpy(w2, ptr); - ptr = strchr(ptr, ','); - if (ptr) { - ptr[0]='\0'; - for(ptr=ptr+1;ptr[0]==' ';ptr=ptr+1); - strcpy(w2, optr); - strcpy(w3, ptr); - } - } - } - { - const char *wa[] = { w0, w1, w2, w3 }; - int nw=0; - - for(i=0;i<4;i++) { - if (wa[i][0] != '\0') - nw++; - } - - r_asm_x86_deltify(nw, wa, aux->str); - } - } - - return R_TRUE; -} - diff --git a/libr/asm/p/Makefile b/libr/asm/p/Makefile index 0567409949..20e7dbed83 100644 --- a/libr/asm/p/Makefile +++ b/libr/asm/p/Makefile @@ -1,11 +1,10 @@ -CFLAGS=-I../../include -I../arch/ -Wall -BINDEPS= +CFLAGS=-I../../include -I../arch/ -I../arch/include -Wall # XXX CFLAGS+=-DLIL_ENDIAN=1 -# X86 +## X86 +OBJ_X86=asm_x86.o # udis86 -OBJ_X86+=asm_x86.o OBJ_X86+=../arch/x86/udis86/syn.o OBJ_X86+=../arch/x86/udis86/input.o OBJ_X86+=../arch/x86/udis86/udis86.o @@ -17,8 +16,39 @@ OBJ_X86+=../arch/x86/udis86/syn-att.o OBJ_X86+=../arch/x86/ollyasm/disasm.o OBJ_X86+=../arch/x86/ollyasm/asmserv.o OBJ_X86+=../arch/x86/ollyasm/assembl.o +## ARM +OBJ_ARM=asm_arm.o +# gnu arm-dis +OBJ_ARM+=../arch/arm/gnu/arm-dis.o +## MIPS +OBJ_MIPS=asm_mips.o +# gnu mips-dis +OBJ_MIPS+=../arch/mips/gnu/mips-dis.o +OBJ_MIPS+=../arch/mips/gnu/mips16-opc.o +OBJ_MIPS+=../arch/mips/gnu/mips-opc.o +## SPARC +OBJ_SPARC=asm_sparc.o +# gnu sparc-dis +OBJ_SPARC+=../arch/sparc/gnu/sparc-dis.o +OBJ_SPARC+=../arch/sparc/gnu/sparc-opc.o +## PPC +OBJ_PPC=asm_ppc.o +# ppc-disasm +OBJ_PPC+=../arch/ppc/ppc_disasm/ppc_disasm.o +## BF +OBJ_BF=asm_bf.o +## CSR +OBJ_CSR=asm_csr.o +# csr-disasm +OBJ_CSR+=../arch/csr/csr_disasm/dis.o +## M68K +OBJ_M68K=asm_m68k.o +# mk8k-disasm +OBJ_M68K+=../arch/m68k/m68k_disasm/m68k_disasm.o -all: asm_dummy.so asm_x86.so +all: asm_dummy.so asm_x86.so asm_arm.so \ + asm_mips.so asm_sparc.so asm_ppc.so \ + asm_bf.so asm_csr.so asm_m68k.so @true asm_dummy.so: asm_dummy.o @@ -29,50 +59,36 @@ asm_x86.so: ${OBJ_X86} ${CC} ${CFLAGS} -fPIC -shared -o asm_x86.so ${OBJ_X86} -Wl,-R.. @#strip -s asm_x86.so -clean: - -rm -f *.so *.o +asm_arm.so: ${OBJ_ARM} + ${CC} ${CFLAGS} -fPIC -shared -o asm_arm.so ${OBJ_ARM} -Wl,-R.. + @#strip -s asm_x86.so -#NAME=r_asm -#OBJ=asm.o -#DEPS=r_util -# -#CFLAGS+=-Iarch/include -# -# -## ARM -#OBJ+=arch/arm/asm.o -## gnu arm-dis -#OBJ+=arch/arm/gnu/arm-dis.o -# -## MIPS -#OBJ+=arch/mips/asm.o -## gnu mips-dis -#OBJ+=arch/mips/gnu/mips-dis.o -#OBJ+=arch/mips/gnu/mips16-opc.o -#OBJ+=arch/mips/gnu/mips-opc.o -# -## SPARC -#OBJ+=arch/sparc/asm.o -## gnu sparc-dis -#OBJ+=arch/sparc/gnu/sparc-dis.o -#OBJ+=arch/sparc/gnu/sparc-opc.o -# -## PPC -#OBJ+=arch/ppc/asm.o -## ppc-disasm -#OBJ+=arch/ppc/ppc_disasm/ppc_disasm.o -# -## BF -#OBJ+=arch/bf/asm.o -# -## CSR -#OBJ+=arch/csr/asm.o -## ppc-disasm -#OBJ+=arch/csr/csr_disasm/dis.o -# -## M68K -#OBJ+=arch/m68k/asm.o -## mk8k-disasm -#OBJ+=arch/m68k/m68k_disasm/m68k_disasm.o -# -#include ../rules.mk +asm_mips.so: ${OBJ_MIPS} + ${CC} ${CFLAGS} -fPIC -shared -o asm_mips.so ${OBJ_MIPS} -Wl,-R.. + @#strip -s asm_x86.so + +asm_sparc.so: ${OBJ_SPARC} + ${CC} ${CFLAGS} -fPIC -shared -o asm_sparc.so ${OBJ_SPARC} -Wl,-R.. + @#strip -s asm_x86.so + +asm_ppc.so: ${OBJ_PPC} + ${CC} ${CFLAGS} -fPIC -shared -o asm_ppc.so ${OBJ_PPC} -Wl,-R.. + @#strip -s asm_x86.so + +asm_bf.so: ${OBJ_BF} + ${CC} ${CFLAGS} -fPIC -shared -o asm_bf.so ${OBJ_BF} -Wl,-R.. + @#strip -s asm_x86.so + +asm_csr.so: ${OBJ_CSR} + ${CC} ${CFLAGS} -fPIC -shared -o asm_csr.so ${OBJ_CSR} -Wl,-R.. + @#strip -s asm_x86.so + +asm_m68k.so: ${OBJ_M68K} + ${CC} ${CFLAGS} -fPIC -shared -o asm_m68k.so ${OBJ_M68K} -Wl,-R.. + @#strip -s asm_x86.so + +clean: + -rm -f *.so *.o \ + ${OBJ_X86} ${OBJ_ARM} ${OBJ_MIPS} \ + ${OBJ_SPARC} ${OBJ_PPC} ${OBJ_BF} \ + ${OBJ_CSR} ${OBJ_M68K} diff --git a/libr/asm/arch/arm/asm.c b/libr/asm/p/asm_arm.c similarity index 72% rename from libr/asm/arch/arm/asm.c rename to libr/asm/p/asm_arm.c index 97105e6d51..22cb0869d2 100644 --- a/libr/asm/arch/arm/asm.c +++ b/libr/asm/p/asm_arm.c @@ -6,6 +6,7 @@ #include #include +#include #include #include "dis-asm.h" @@ -55,14 +56,14 @@ static int buf_fprintf(void *stream, const char *format, ...) return 0; } -int r_asm_arm_disasm(struct r_asm_t *a, u8 *buf, u64 len) +static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) { struct disassemble_info disasm_obj; - buf_global = a->buf_asm; + buf_global = aop->buf_asm; Offset = a->pc; memcpy(bytes, buf, 4); // TODO handle thumb - r_hex_bin2str(bytes, 4, a->buf_hex); + r_hex_bin2str(bytes, 4, aop->buf_hex); /* prepare disassembler */ memset(&disasm_obj,'\0', sizeof(struct disassemble_info)); @@ -77,14 +78,28 @@ int r_asm_arm_disasm(struct r_asm_t *a, u8 *buf, u64 len) disasm_obj.fprintf_func = &buf_fprintf; disasm_obj.stream = stdout; - a->buf_asm[0]='\0'; - a->inst_len = print_insn_arm((bfd_vma)Offset, &disasm_obj); + aop->buf_asm[0]='\0'; + aop->inst_len = print_insn_arm((bfd_vma)Offset, &disasm_obj); - if (a->inst_len == -1) - strcpy(a->buf_asm, " (data)"); + if (aop->inst_len == -1) + strcpy(aop->buf_asm, " (data)"); - if (a->inst_len > 0) - memcpy(a->buf, buf, a->inst_len); + if (aop->inst_len > 0) + memcpy(aop->buf, buf, aop->inst_len); - return a->inst_len; + return aop->inst_len; } + +static struct r_asm_handle_t r_asm_plugin_arm = { + .name = "asm_arm", + .desc = "ARM disassembly plugin", + .init = NULL, + .fini = NULL, + .disassemble = &disassemble, + .assemble = NULL +}; + +struct r_lib_struct_t radare_plugin = { + .type = R_LIB_TYPE_ASM, + .data = &r_asm_plugin_arm +}; diff --git a/libr/asm/p/asm_bf.c b/libr/asm/p/asm_bf.c new file mode 100644 index 0000000000..b0a773f120 --- /dev/null +++ b/libr/asm/p/asm_bf.c @@ -0,0 +1,79 @@ +/* radare - LGPL - Copyright 2009 pancake - nibble<.ds@gmail.com> */ + +#include +#include + +#include +#include +#include + + +static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) +{ + int i; + char *buf_cp, *b; + + if ((b = buf_cp = alloca(len+1)) == NULL) + return 0; + memcpy(buf_cp, buf, len+1); + + for(i=0;b[0] == b[1] && ibuf_hex, buf_cp, 256); + + switch(buf[0]) { + case '[': + strcpy(aop->buf_asm, "[ loop {"); + break; + case ']': + strcpy(aop->buf_asm, "] }"); // TODO: detect clause and put label name + break; + case '>': + if (i>1) strcpy(aop->buf_asm, "> add [ptr]"); + else strcpy(aop->buf_asm, "> inc [ptr]"); + break; + case '<': + if (i>1) strcpy(aop->buf_asm, "< sub [ptr]"); + else strcpy(aop->buf_asm, "< dec [ptr]"); + break; + case '+': + if (i>1) strcpy(aop->buf_asm, "+ add [ptr]"); + else strcpy(aop->buf_asm, "+ inc [ptr]"); + break; + case '-': + if (i>1) strcpy(aop->buf_asm, "- sub [ptr]"); + else strcpy(aop->buf_asm, "- dec [ptr]"); + break; + case ',': + strcpy(aop->buf_asm, ", [ptr] = getch()"); + break; + case '.': + strcpy(aop->buf_asm, ". print( [ptr] )"); + break; + case '\x00': + strcpy(aop->buf_asm, " trap"); + break; + default: + strcpy(aop->buf_asm, " nop"); + break; + } + + if (i>0) sprintf(aop->buf_asm, "%s, %d", aop->buf_asm, i+1); + if (i<1) i=1; else i++; + + return i; +} + +static struct r_asm_handle_t r_asm_plugin_bf = { + .name = "asm_bf", + .desc = "BF disassembly plugin", + .init = NULL, + .fini = NULL, + .disassemble = &disassemble, + .assemble = NULL +}; + +struct r_lib_struct_t radare_plugin = { + .type = R_LIB_TYPE_ASM, + .data = &r_asm_plugin_bf +}; diff --git a/libr/asm/p/asm_csr.c b/libr/asm/p/asm_csr.c new file mode 100644 index 0000000000..52ff15edba --- /dev/null +++ b/libr/asm/p/asm_csr.c @@ -0,0 +1,35 @@ +/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ + +#include + +#include +#include +#include +#include + +#include "csr/csr_disasm/dis.h" + + +static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) +{ + r_hex_bin2str((u8*)buf, 2, aop->buf_hex); + arch_csr_disasm(aop->buf_asm, buf, a->pc); + memcpy(aop->buf, buf, 2); + aop->inst_len = 2; + + return aop->inst_len; +} + +static struct r_asm_handle_t r_asm_plugin_csr = { + .name = "asm_csr", + .desc = "CSR disassembly plugin", + .init = NULL, + .fini = NULL, + .disassemble = &disassemble, + .assemble = NULL +}; + +struct r_lib_struct_t radare_plugin = { + .type = R_LIB_TYPE_ASM, + .data = &r_asm_plugin_csr +}; diff --git a/libr/asm/p/asm_dummy.c b/libr/asm/p/asm_dummy.c index 7070ff5455..d902e022a3 100644 --- a/libr/asm/p/asm_dummy.c +++ b/libr/asm/p/asm_dummy.c @@ -4,6 +4,7 @@ #include #include + static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) { printf("Dummy (dis)assembly plugin"); diff --git a/libr/asm/p/asm_m68k.c b/libr/asm/p/asm_m68k.c new file mode 100644 index 0000000000..8d8adaa6c3 --- /dev/null +++ b/libr/asm/p/asm_m68k.c @@ -0,0 +1,49 @@ +/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ + +#include +#include + +#include +#include +#include +#include + +#include "m68k/m68k_disasm/m68k_disasm.h" + + +static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) +{ + m68k_word bof[4]; + m68k_word iaddr = (m68k_word)a->pc; + char opcode[256]; + char operands[256]; + + struct DisasmPara_68k dp; + /* initialize DisasmPara */ + memcpy(bof, buf, 4); + dp.opcode = opcode; + dp.operands = operands; + dp.iaddr = &iaddr; + dp.instr = bof; + M68k_Disassemble(&dp); + sprintf(aop->buf_asm, "%s %s", opcode, operands); + r_hex_bin2str((u8*)bof, 4, aop->buf_hex); + memcpy(aop->buf, bof, 4); + aop->inst_len = 4; + + return aop->inst_len; +} + +static struct r_asm_handle_t r_asm_plugin_m68k = { + .name = "asm_m68k", + .desc = "M68K disassembly plugin", + .init = NULL, + .fini = NULL, + .disassemble = &disassemble, + .assemble = NULL +}; + +struct r_lib_struct_t radare_plugin = { + .type = R_LIB_TYPE_ASM, + .data = &r_asm_plugin_m68k +}; diff --git a/libr/asm/arch/mips/asm.c b/libr/asm/p/asm_mips.c similarity index 71% rename from libr/asm/arch/mips/asm.c rename to libr/asm/p/asm_mips.c index da9357f1fc..83bb97820f 100644 --- a/libr/asm/arch/mips/asm.c +++ b/libr/asm/p/asm_mips.c @@ -5,12 +5,14 @@ #include #include +#include #include #include #include "dis-asm.h" #include "opcode/mips.h" + static int mips_mode = 0; static unsigned long Offset = 0; static char *buf_global = NULL; @@ -54,14 +56,14 @@ static int buf_fprintf(void *stream, const char *format, ...) return 0; } -int r_asm_mips_disasm(struct r_asm_t *a, u8 *buf, u64 len) +static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) { struct disassemble_info disasm_obj; - buf_global = a->buf_asm; + buf_global = aop->buf_asm; Offset = a->pc; memcpy(bytes, buf, 4); // TODO handle thumb - r_hex_bin2str(bytes, 4, a->buf_hex); + r_hex_bin2str(bytes, 4, aop->buf_hex); /* prepare disassembler */ memset(&disasm_obj,'\0', sizeof(struct disassemble_info)); @@ -78,16 +80,30 @@ int r_asm_mips_disasm(struct r_asm_t *a, u8 *buf, u64 len) disasm_obj.fprintf_func = &buf_fprintf; disasm_obj.stream = stdout; - a->buf_asm[0]='\0'; + aop->buf_asm[0]='\0'; if (a->big_endian) - a->inst_len = print_insn_big_mips((bfd_vma)Offset, &disasm_obj); - else a->inst_len = print_insn_little_mips((bfd_vma)Offset, &disasm_obj); + aop->inst_len = print_insn_big_mips((bfd_vma)Offset, &disasm_obj); + else aop->inst_len = print_insn_little_mips((bfd_vma)Offset, &disasm_obj); - if (a->inst_len == -1) - strcpy(a->buf_asm, " (data)"); + if (aop->inst_len == -1) + strcpy(aop->buf_asm, " (data)"); - if (a->inst_len > 0) - memcpy(a->buf, buf, a->inst_len); + if (aop->inst_len > 0) + memcpy(aop->buf, buf, aop->inst_len); - return a->inst_len; + return aop->inst_len; } + +static struct r_asm_handle_t r_asm_plugin_mips = { + .name = "asm_mips", + .desc = "MIPS disassembly plugin", + .init = NULL, + .fini = NULL, + .disassemble = &disassemble, + .assemble = NULL +}; + +struct r_lib_struct_t radare_plugin = { + .type = R_LIB_TYPE_ASM, + .data = &r_asm_plugin_mips +}; diff --git a/libr/asm/p/asm_ppc.c b/libr/asm/p/asm_ppc.c new file mode 100644 index 0000000000..8550ad0376 --- /dev/null +++ b/libr/asm/p/asm_ppc.c @@ -0,0 +1,49 @@ +/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ + +#include +#include + +#include +#include +#include +#include + +#include "ppc/ppc_disasm/ppc_disasm.h" + + +static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) +{ + ppc_word iaddr = (ppc_word)a->pc; + ppc_word bof[4]; + char opcode[128]; + char operands[128]; + + struct DisasmPara_PPC dp; + /* initialize DisasmPara */ + memcpy(bof, buf, 4); + dp.opcode = opcode; + dp.operands = operands; + dp.iaddr = &iaddr; + dp.instr = bof; + PPC_Disassemble(&dp, a->big_endian); + r_hex_bin2str((u8*)bof, 4, aop->buf_hex); + sprintf(aop->buf_asm, "%s %s", opcode, operands); + memcpy(aop->buf, bof, 4); + aop->inst_len = 4; + + return aop->inst_len; +} + +static struct r_asm_handle_t r_asm_plugin_ppc = { + .name = "asm_ppc", + .desc = "PPC disassembly plugin", + .init = NULL, + .fini = NULL, + .disassemble = &disassemble, + .assemble = NULL +}; + +struct r_lib_struct_t radare_plugin = { + .type = R_LIB_TYPE_ASM, + .data = &r_asm_plugin_ppc +}; diff --git a/libr/asm/arch/sparc/asm.c b/libr/asm/p/asm_sparc.c similarity index 71% rename from libr/asm/arch/sparc/asm.c rename to libr/asm/p/asm_sparc.c index 21d51dc897..ce52198df2 100644 --- a/libr/asm/arch/sparc/asm.c +++ b/libr/asm/p/asm_sparc.c @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -55,14 +56,14 @@ static int buf_fprintf(void *stream, const char *format, ...) return 0; } -int r_asm_sparc_disasm(struct r_asm_t *a, u8 *buf, u64 len) +static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len) { struct disassemble_info disasm_obj; - buf_global = a->buf_asm; + buf_global = aop->buf_asm; Offset = a->pc; memcpy(bytes, buf, 4); // TODO handle thumb - r_hex_bin2str(bytes, 4, a->buf_hex); + r_hex_bin2str(bytes, 4, aop->buf_hex); /* prepare disassembler */ memset(&disasm_obj,'\0', sizeof(struct disassemble_info)); @@ -76,14 +77,28 @@ int r_asm_sparc_disasm(struct r_asm_t *a, u8 *buf, u64 len) disasm_obj.fprintf_func = &buf_fprintf; disasm_obj.stream = stdout; - a->buf_asm[0]='\0'; - a->inst_len = print_insn_sparc((bfd_vma)Offset, &disasm_obj); + aop->buf_asm[0]='\0'; + aop->inst_len = print_insn_sparc((bfd_vma)Offset, &disasm_obj); - if (a->inst_len == -1) - strcpy(a->buf_asm, " (data)"); + if (aop->inst_len == -1) + strcpy(aop->buf_asm, " (data)"); - if (a->inst_len > 0) - memcpy(a->buf, buf, a->inst_len); + if (aop->inst_len > 0) + memcpy(aop->buf, buf, aop->inst_len); - return a->inst_len; + return aop->inst_len; } + +static struct r_asm_handle_t r_asm_plugin_sparc = { + .name = "asm_sparc", + .desc = "SPARC disassembly plugin", + .init = NULL, + .fini = NULL, + .disassemble = &disassemble, + .assemble = NULL +}; + +struct r_lib_struct_t radare_plugin = { + .type = R_LIB_TYPE_ASM, + .data = &r_asm_plugin_sparc +}; diff --git a/libr/asm/p/asm_x86.c b/libr/asm/p/asm_x86.c index c5814210b8..10f8a58639 100644 --- a/libr/asm/p/asm_x86.c +++ b/libr/asm/p/asm_x86.c @@ -92,7 +92,7 @@ static struct r_asm_handle_t r_asm_plugin_x86 = { .init = NULL, .fini = NULL, .disassemble = &disassemble, - .assemble = &assemble, + .assemble = &assemble }; struct r_lib_struct_t radare_plugin = { diff --git a/libr/asm/t/Makefile b/libr/asm/t/Makefile new file mode 100644 index 0000000000..3d450ababf --- /dev/null +++ b/libr/asm/t/Makefile @@ -0,0 +1,6 @@ +OBJ=rasm2.o +BIN=rasm2 +BINDEPS=r_util r_lib r_asm +LIBS+=-ldl + +include ../../rules.mk diff --git a/libr/asm/t/asm_x86.c b/libr/asm/t/asm_x86.c deleted file mode 100644 index d22f96d300..0000000000 --- a/libr/asm/t/asm_x86.c +++ /dev/null @@ -1,24 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include -#include - -#include -#include - -int main() -{ - struct r_asm_t a; - char *buf = "push 0x8059a00"; - int ret; - - r_asm_init(&a); - r_asm_set_syntax(&a, R_ASM_SYN_OLLY); - r_asm_set_pc(&a, 0x08049a4b); - ret = r_asm_asm(&a, buf); - if (!ret) - printf("invalid\n"); - else printf("DISASM %s HEX %s\n", a.buf_asm, a.buf_hex); - - return 0; -} diff --git a/libr/asm/t/disasm_arm.c b/libr/asm/t/disasm_arm.c deleted file mode 100644 index 78e5c875b7..0000000000 --- a/libr/asm/t/disasm_arm.c +++ /dev/null @@ -1,31 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include - -#include -#include - -int main() -{ - struct r_asm_t a; - u8 *buf = "\x7c\x20\xa0\xe3"; - int ret = 0; - u64 idx = 0, len = 4; - - r_asm_init(&a); - r_asm_set_arch(&a, R_ASM_ARCH_ARM); - r_asm_set_bits(&a, 32); - r_asm_set_big_endian(&a, R_FALSE); - r_asm_set_syntax(&a, R_ASM_SYN_INTEL); - - while (idx < len) { - r_asm_set_pc(&a, 0x000089d8 + idx); - - ret = r_asm_disasm(&a, buf+idx, len-idx); - printf("DISASM %s HEX %s\n", a.buf_asm, a.buf_hex); - - idx += ret; - } - - return 0; -} diff --git a/libr/asm/t/disasm_bf.c b/libr/asm/t/disasm_bf.c deleted file mode 100644 index 085ee4cc1a..0000000000 --- a/libr/asm/t/disasm_bf.c +++ /dev/null @@ -1,33 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include - -#include -#include - -int main() -{ - struct r_asm_t a; - u8 *buf = "++++++++++[>+++++++>++++++++++>+++>" - "+<<<<-]>++.>+.+++++++..+++.>++.<<++++++++" - "+++++++.>.+++.------.--------.>+.>."; - int ret = 0; - u64 idx = 0, len = strlen(buf); - - r_asm_init(&a); - r_asm_set_arch(&a, R_ASM_ARCH_BF); - r_asm_set_bits(&a, 32); - r_asm_set_big_endian(&a, R_FALSE); - r_asm_set_syntax(&a, R_ASM_SYN_INTEL); - - 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); - - idx += ret; - } - - return 0; -} diff --git a/libr/asm/t/disasm_csr.c b/libr/asm/t/disasm_csr.c deleted file mode 100644 index 643dff2f98..0000000000 --- a/libr/asm/t/disasm_csr.c +++ /dev/null @@ -1,32 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include -#include - -#include -#include - -int main() -{ - struct r_asm_t a; - u8 *buf = "\xd2\xa1\x88\xe2"; - int ret = 0; - u64 idx = 0, len = 4; - - r_asm_init(&a); - r_asm_set_arch(&a, R_ASM_ARCH_CSR); - r_asm_set_bits(&a, 32); - r_asm_set_big_endian(&a, R_FALSE); - r_asm_set_syntax(&a, R_ASM_SYN_INTEL); - - while (idx < len) { - r_asm_set_pc(&a, 0x000089d8 + idx); - - ret = r_asm_disasm(&a, buf+idx, len-idx); - printf("DISASM %s HEX %s\n", a.buf_asm, a.buf_hex); - - idx += ret; - } - - return 0; -} diff --git a/libr/asm/t/disasm_m68k.c b/libr/asm/t/disasm_m68k.c deleted file mode 100644 index 75cf91924c..0000000000 --- a/libr/asm/t/disasm_m68k.c +++ /dev/null @@ -1,32 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include -#include - -#include -#include - -int main() -{ - struct r_asm_t a; - u8 *buf = "\x8b\x10\x85\xd2"; - int ret = 0; - u64 idx = 0, len = 4; - - r_asm_init(&a); - r_asm_set_arch(&a, R_ASM_ARCH_M68K); - r_asm_set_bits(&a, 32); - r_asm_set_big_endian(&a, R_FALSE); - r_asm_set_syntax(&a, R_ASM_SYN_INTEL); - - while (idx < len) { - r_asm_set_pc(&a, 0x000089d8 + idx); - - ret = r_asm_disasm(&a, buf+idx, len-idx); - printf("DISASM %s HEX %s\n", a.buf_asm, a.buf_hex); - - idx += ret; - } - - return 0; -} diff --git a/libr/asm/t/disasm_mips.c b/libr/asm/t/disasm_mips.c deleted file mode 100644 index df89facd3e..0000000000 --- a/libr/asm/t/disasm_mips.c +++ /dev/null @@ -1,32 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include -#include - -#include -#include - -int main() -{ - struct r_asm_t a; - u8 *buf = "\x04\x28\x9c\x27\x21\xe0\x9f\x03"; - int ret = 0; - u64 idx = 0, len = 8; - - r_asm_init(&a); - r_asm_set_arch(&a, R_ASM_ARCH_MIPS); - r_asm_set_bits(&a, 32); - r_asm_set_big_endian(&a, R_FALSE); - r_asm_set_syntax(&a, R_ASM_SYN_INTEL); - - while (idx < len) { - r_asm_set_pc(&a, 0x000089d8 + idx); - - ret = r_asm_disasm(&a, buf+idx, len-idx); - printf("DISASM %s HEX %s\n", a.buf_asm, a.buf_hex); - - idx += ret; - } - - return 0; -} diff --git a/libr/asm/t/disasm_ppc.c b/libr/asm/t/disasm_ppc.c deleted file mode 100644 index f3b30e77d9..0000000000 --- a/libr/asm/t/disasm_ppc.c +++ /dev/null @@ -1,32 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include -#include - -#include -#include - -int main() -{ - struct r_asm_t a; - u8 *buf = "\x3c\x5f\x00\x00"; - int ret = 0; - u64 idx = 0, len = 4; - - r_asm_init(&a); - r_asm_set_arch(&a, R_ASM_ARCH_PPC); - r_asm_set_bits(&a, 32); - r_asm_set_big_endian(&a, R_FALSE); - r_asm_set_syntax(&a, R_ASM_SYN_INTEL); - - while (idx < len) { - r_asm_set_pc(&a, 0x000089d8 + idx); - - ret = r_asm_disasm(&a, buf+idx, len-idx); - printf("DISASM %s HEX %s\n", a.buf_asm, a.buf_hex); - - idx += ret; - } - - return 0; -} diff --git a/libr/asm/t/disasm_sparc.c b/libr/asm/t/disasm_sparc.c deleted file mode 100644 index d0531055d8..0000000000 --- a/libr/asm/t/disasm_sparc.c +++ /dev/null @@ -1,32 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include -#include - -#include -#include - -int main() -{ - struct r_asm_t a; - u8 *buf = "\xbc\x10\x20\x00"; - int ret = 0; - u64 idx = 0, len = 4; - - r_asm_init(&a); - r_asm_set_arch(&a, R_ASM_ARCH_SPARC); - r_asm_set_bits(&a, 32); - r_asm_set_big_endian(&a, R_TRUE); - r_asm_set_syntax(&a, R_ASM_SYN_INTEL); - - while (idx < len) { - r_asm_set_pc(&a, 0x00010f98 + idx); - - ret = r_asm_disasm(&a, buf+idx, len-idx); - printf("DISASM %s HEX %s\n", a.buf_asm, a.buf_hex); - - idx += ret; - } - - return 0; -} diff --git a/libr/asm/t/disasm_x86.c b/libr/asm/t/disasm_x86.c deleted file mode 100644 index f1b9619432..0000000000 --- a/libr/asm/t/disasm_x86.c +++ /dev/null @@ -1,39 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include - -#include -#include - -int main() -{ - struct r_asm_t a; - char str[256]; - 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_PSEUDO, NULL, str); - - 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("PAR_PSEUDO %s\n\n", str); - - idx += ret; - } - - return 0; -} diff --git a/libr/asm/t/rasm2.c b/libr/asm/t/rasm2.c index 16eaaf46ed..4087e5c6c8 100644 --- a/libr/asm/t/rasm2.c +++ b/libr/asm/t/rasm2.c @@ -7,6 +7,11 @@ #include #include #include +#include + + +static struct r_lib_t l; +static struct r_asm_t a; static int rasm_show_help() { @@ -23,7 +28,7 @@ static int rasm_show_help() static int rasm_disasm(char *buf, u64 offset, char *arch, char *syntax, int big_endian) { - struct r_asm_t a; + struct r_asm_aop_t aop; u8 *data; char *ptr = buf; int ret = 0; @@ -37,11 +42,9 @@ static int rasm_disasm(char *buf, u64 offset, char *arch, char *syntax, int big_ data = alloca(len); r_hex_str2bin(buf, data); - r_asm_init(&a); - if (!strcmp(arch, "arm")) - r_asm_set_arch(&a, R_ASM_ARCH_ARM); - else r_asm_set_arch(&a, R_ASM_ARCH_X86); + r_asm_set(&a, "asm_arm"); + else r_asm_set(&a, "asm_x86"); if (syntax != NULL) { if (!strcmp(syntax, "att")) @@ -56,9 +59,9 @@ static int rasm_disasm(char *buf, u64 offset, char *arch, char *syntax, int big_ while (idx < len) { r_asm_set_pc(&a, a.pc + ret); - ret = r_asm_disasm(&a, data+idx, len-idx); + ret = r_asm_disassemble(&a, &aop, data+idx, len-idx); idx += ret; - printf("%s\n", a.buf_asm); + printf("%s\n", aop.buf_asm); } return (int)idx; @@ -66,26 +69,36 @@ static int rasm_disasm(char *buf, u64 offset, char *arch, char *syntax, int big_ static int rasm_asm(char *buf, u64 offset, char *arch, char *syntax, int big_endian) { - struct r_asm_t a; + struct r_asm_aop_t aop; int ret; - r_asm_init(&a); /* TODO: Arch, syntax... */ - r_asm_set_arch(&a, R_ASM_ARCH_X86); + r_asm_set(&a, "asm_x86"); r_asm_set_syntax(&a, R_ASM_SYN_OLLY); r_asm_set_big_endian(&a, big_endian); r_asm_set_pc(&a, offset); - ret = r_asm_asm(&a, buf); + ret = r_asm_assemble(&a, &aop, buf); if (!ret) printf("invalid\n"); - else printf("%s\n", a.buf_hex); + else printf("%s\n", aop.buf_hex); return ret; } +/* asm callback */ +static int __lib_asm_cb(struct r_lib_plugin_t *pl, void *user, void *data) +{ + struct r_asm_handle_t *hand = (struct r_asm_handle_t *)data; + struct r_core_t *core = (struct r_core_t *)user; + //printf(" * Added (dis)assembly handler\n"); + r_asm_add(&a, hand); + return R_TRUE; +} +static int __lib_asm_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; } + int main(int argc, char *argv[]) { char *arch, *syntax; @@ -95,6 +108,12 @@ int main(int argc, char *argv[]) if (argc<2) return rasm_show_help(); + r_asm_init(&a); + r_lib_init(&l, "radare_plugin"); + r_lib_add_handler(&l, R_LIB_TYPE_ASM, "(dis)assembly plugins", + &__lib_asm_cb, &__lib_asm_dt, NULL); + r_lib_opendir(&l, getenv("LIBR_PLUGINS")); + while ((c = getopt(argc, argv, "da:s:o:h")) != -1) { switch( c ) { diff --git a/libr/asm/t/realloc_x86.c b/libr/asm/t/realloc_x86.c deleted file mode 100644 index 5d8eef0480..0000000000 --- a/libr/asm/t/realloc_x86.c +++ /dev/null @@ -1,84 +0,0 @@ -/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */ - -#include - -#include "r_asm.h" -#include "r_bin.h" -#include "r_util.h" -#include "r_types.h" - - -static int cb(struct r_asm_t *a) -{ - struct r_asm_realloc_t *aux = (struct r_asm_realloc_t*)a->aux; - - printf("REALLOC: %s\n", aux->str); - return R_TRUE; -} - -int main(int argc, char *argv[]) -{ - r_bin_obj bin; - struct r_asm_t a; - struct r_asm_realloc_t r; - r_bin_section *sections; - char *file, *section; - u8 *buf; - u64 size = 0, idx = 0, len = 0; - int ret = 0, i = 0; - - if (argc != 4) { - fprintf(stderr, "Usage: %s elf_file section_name new_size\n", argv[0]); - return 1; - } - - file = argv[1]; - section = argv[2]; - size = r_num_math(NULL, argv[3]); - - if (r_bin_init(&bin, file, 1) == -1) { - fprintf(stderr, "Cannot open file\n"); - return 1; - } - - /* Resize sections */ - if ((r.delta = r_bin_resize_section(&bin, section, size)) == 0) { - fprintf(stderr, "Delta = 0\n"); - return R_TRUE; - } - - r.offset = r_bin_get_section_rva(&bin, section) + r_bin_get_baddr(&bin); - sections = r_bin_get_sections(&bin); - - r_bin_close(&bin); - - /* Parse executable sections */ - 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_REALLOC, &cb, &r); - - for (i=0; !sections[i].last; i++) - if (R_BIN_SCN_EXECUTABLE(sections[i].characteristics)) { - if ((buf = buf = r_file_slurp_range(file, sections[i].offset, sections[i].size)) == NULL) { - fprintf(stderr, "Error slurping sections\n"); - return 1; - } - idx = 0; len = sections[i].size; - while (idx < len) { - r_asm_set_pc(&a, sections[i].rva + idx); - - ret = r_asm_disasm(&a, buf+idx, len-idx); - r_asm_parse(&a); - - idx += ret; - } - free(buf); - } - - free(sections); - - return R_FALSE; -} diff --git a/libr/asm/arch/x86/pseudo.c b/libr/parse/pseudo.c similarity index 100% rename from libr/asm/arch/x86/pseudo.c rename to libr/parse/pseudo.c