- Added arm plugin
  - Added bf plugin
  - Added csr plugin
  - Added m68k plugin
  - Added mips plugin
  - Added ppc plugin
  - Added sparc plugin
  - Removed deprecated test programs
  - Updated rasm2 (not working)
* r_parse
  - Initial import

--HG--
rename : libr/asm/arch/arm/asm.c => libr/asm/p/asm_arm.c
rename : libr/asm/arch/bf/asm.c => libr/asm/p/asm_bf.c
rename : libr/asm/arch/csr/asm.c => libr/asm/p/asm_csr.c
rename : libr/asm/arch/m68k/asm.c => libr/asm/p/asm_m68k.c
rename : libr/asm/arch/mips/asm.c => libr/asm/p/asm_mips.c
rename : libr/asm/arch/ppc/asm.c => libr/asm/p/asm_ppc.c
rename : libr/asm/arch/sparc/asm.c => libr/asm/p/asm_sparc.c
rename : libr/asm/arch/x86/pseudo.c => libr/parse/pseudo.c
This commit is contained in:
Nibble 2009-02-18 03:47:40 +01:00
parent 23afb7eeb3
commit 777235bb87
28 changed files with 395 additions and 742 deletions

View File

@ -1,64 +0,0 @@
/* radare - LGPL - Copyright 2009 pancake <youterm.com> - nibble<.ds@gmail.com> */
#include <stdio.h>
#include <string.h>
#include <r_types.h>
#include <r_asm.h>
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] && i<len; b=b+1,i++); b[1] = '\0';
strncpy(a->buf_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;
}

View File

@ -1,19 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <r_types.h>
#include <r_util.h>
#include <r_asm.h>
#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;
}

View File

@ -1,33 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <string.h>
#include <r_types.h>
#include <r_util.h>
#include <r_asm.h>
#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;
}

View File

@ -1,33 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <string.h>
#include <r_types.h>
#include <r_util.h>
#include <r_asm.h>
#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;
}

View File

@ -1,127 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <r_types.h>
#include <r_util.h>
#include <r_asm.h>
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; i<argc; i++) {
strcat(newstr, argv[i]);
strcat(newstr, (i == 0 || i== argc - 1)?" ":",");
}
}
return R_FALSE;
}
int r_asm_x86_realloc(struct r_asm_t *a)
{
struct r_asm_realloc_t *aux = (struct r_asm_realloc_t*)a->aux;
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;
}

View File

@ -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}

View File

@ -6,6 +6,7 @@
#include <r_types.h>
#include <r_util.h>
#include <r_lib.h>
#include <r_asm.h>
#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
};

79
libr/asm/p/asm_bf.c Normal file
View File

@ -0,0 +1,79 @@
/* radare - LGPL - Copyright 2009 pancake <youterm.com> - nibble<.ds@gmail.com> */
#include <stdio.h>
#include <string.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_asm.h>
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] && i<len; b=b+1,i++); b[1] = '\0';
strncpy(aop->buf_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
};

35
libr/asm/p/asm_csr.c Normal file
View File

@ -0,0 +1,35 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_util.h>
#include <r_asm.h>
#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
};

View File

@ -4,6 +4,7 @@
#include <r_lib.h>
#include <r_asm.h>
static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64 len)
{
printf("Dummy (dis)assembly plugin");

49
libr/asm/p/asm_m68k.c Normal file
View File

@ -0,0 +1,49 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <string.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_util.h>
#include <r_asm.h>
#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
};

View File

@ -5,12 +5,14 @@
#include <string.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_util.h>
#include <r_asm.h>
#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
};

49
libr/asm/p/asm_ppc.c Normal file
View File

@ -0,0 +1,49 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <string.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_util.h>
#include <r_asm.h>
#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
};

View File

@ -5,6 +5,7 @@
#include <string.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_util.h>
#include <r_asm.h>
@ -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
};

View File

@ -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 = {

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

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

View File

@ -1,24 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <stdlib.h>
#include <r_types.h>
#include <r_asm.h>
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;
}

View File

@ -1,31 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <r_types.h>
#include <r_asm.h>
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;
}

View File

@ -1,33 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <r_types.h>
#include <r_asm.h>
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;
}

View File

@ -1,32 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <stdlib.h>
#include <r_types.h>
#include <r_asm.h>
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;
}

View File

@ -1,32 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <stdlib.h>
#include <r_types.h>
#include <r_asm.h>
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;
}

View File

@ -1,32 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <stdlib.h>
#include <r_types.h>
#include <r_asm.h>
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;
}

View File

@ -1,32 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <stdlib.h>
#include <r_types.h>
#include <r_asm.h>
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;
}

View File

@ -1,32 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <stdlib.h>
#include <r_types.h>
#include <r_asm.h>
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;
}

View File

@ -1,39 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <r_types.h>
#include <r_asm.h>
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;
}

View File

@ -7,6 +7,11 @@
#include <r_types.h>
#include <r_asm.h>
#include <r_util.h>
#include <r_lib.h>
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 ) {

View File

@ -1,84 +0,0 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
#include <stdio.h>
#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;
}