- Added pseudoinstruction .asciz
  - asm_x86_olly assembles the shorter opcode
* rasm2
  - Minor fixups and refactoring
* Misc
  - Added new quote into fortunes
This commit is contained in:
Nibble 2009-04-15 01:19:42 +02:00
parent 724d900e62
commit b5c9a13366
4 changed files with 65 additions and 35 deletions

View file

@ -105,3 +105,4 @@ Microloft Visual Radare.NET 2008. Now OOXML Powered!
Enjoy the 'two girls one backup' viral video
A C program is like a fast dance on a newly waxed dance floor by people carrying razors - Waldi Ravens
radare2 is like windows 7 but even better
Enlarge your radare2

View file

@ -12,14 +12,30 @@
static struct r_asm_handle_t *asm_static_plugins[] =
{ R_ASM_STATIC_PLUGINS };
static int r_asm_byte(void *data, const char *input)
static int r_asm_asciz(void *data, const char *input)
{
int ret;
int len = 0;
struct r_asm_aop_t **aop = (struct r_asm_aop_t**)data;
char *arg = strchr(input, ' ');
ret = r_hex_str2bin(arg, (*aop)->buf);
strncpy((*aop)->buf_hex, r_str_trim(arg), 1024);
return ret;
if (arg && (len = strlen(arg+1)+1)) {
arg += 1;
r_hex_bin2str((u8*)arg, len, (*aop)->buf_hex);
strncpy((char*)(*aop)->buf, arg, 1024);
}
return len;
}
static int r_asm_byte(void *data, const char *input)
{
int len = 0;
struct r_asm_aop_t **aop = (struct r_asm_aop_t**)data;
char *arg = strchr(input, ' ');
if (arg) {
arg += 1;
len = r_hex_str2bin(arg, (*aop)->buf);
strncpy((*aop)->buf_hex, r_str_trim(arg), 1024);
}
return len;
}
R_API struct r_asm_t *r_asm_new()
@ -47,6 +63,8 @@ R_API int r_asm_init(struct r_asm_t *a)
for(i=0;asm_static_plugins[i];i++)
r_asm_add(a, asm_static_plugins[i]);
r_cmd_init(&a->cmd);
r_cmd_add(&a->cmd, "a", ".asciz", &r_asm_asciz);
r_cmd_add_long(&a->cmd, "asciz", "a", ".asciz");
r_cmd_add(&a->cmd, "b", ".byte", &r_asm_byte);
r_cmd_add_long(&a->cmd, "byte", "b", ".byte");
return R_TRUE;
@ -75,7 +93,7 @@ R_API int r_asm_add(struct r_asm_t *a, struct r_asm_handle_t *foo)
R_API int r_asm_del(struct r_asm_t *a, const char *name)
{
#warning TODO: Implement r_asm_del
/* TODO: Implement r_asm_del */
return R_FALSE;
}
@ -171,14 +189,15 @@ R_API int r_asm_assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, const char
list_for_each_prev(pos, &a->asms) {
struct r_asm_handle_t *h = list_entry(pos, struct r_asm_handle_t, list);
if (h->arch && h->assemble && has_bits(h, a->bits) && !strcmp(a->cur->arch, h->arch)) {
printf("NAME %s\n", h->name);
ret = h->assemble(a, aop, buf);
break;
}
}
}
if (aop && ret > 0)
if (aop && ret > 0) {
r_hex_bin2str(aop->buf, ret, aop->buf_hex);
strncpy(aop->buf_asm, buf, 1024);
}
return ret;
}
@ -201,7 +220,7 @@ R_API int r_asm_massemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf)
for (ret = idx = i = 0, *buf_hex='\0'; i <= ctr; i++, idx+=ret) {
r_asm_set_pc(a, a->pc + ret);
if ((ptr = strchr(tokens[i], '.'))) /* Pseudo */
ret = r_cmd_call(&a->cmd, ptr+1);
ret = r_cmd_call_long(&a->cmd, ptr+1);
else /* Instruction */
ret = r_asm_assemble(a, aop, tokens[i]);
if (ret) {
@ -209,7 +228,6 @@ R_API int r_asm_massemble(struct r_asm_t *a, struct r_asm_aop_t *aop, char *buf)
buf_bin[idx+j] = aop->buf[j];
strcat(buf_hex, aop->buf_hex);
} else {
fprintf(stderr, "invalid\n");
return 0;
}
}

View file

@ -24,19 +24,25 @@ static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, u8 *buf, u64
static int assemble(struct r_asm_t *a, struct r_asm_aop_t *aop, const char *buf)
{
static t_asmmodel asm_obj;
int idx;
int attempt, constsize, oattempt = 0, oconstsize = 0, ret = 0, oret = 0xCAFE;
aop->buf_err[0] = '\0';
/* attempt == 0: First attempt */
/* constsize == 0: Address constants and inmediate data of 16/32b */
aop->inst_len = Assemble((char*)buf, a->pc, &asm_obj, 0, 0, aop->buf_err);
aop->disasm_obj = &asm_obj;
for (constsize = 0; constsize < 4; constsize++) {
for (attempt = 0, aop->buf_err[0] = '\0'; !aop->buf_err[0]; attempt++) {
ret = Assemble((char*)buf, a->pc, &asm_obj, attempt, constsize, aop->buf_err);
if (ret > 0 && ret < oret) {
oret = ret;
oattempt = attempt;
oconstsize = constsize;
}
}
}
aop->inst_len = Assemble((char*)buf, a->pc, &asm_obj, oattempt, oconstsize, aop->buf_err);
if (aop->buf_err[0])
aop->inst_len = 0;
else {
snprintf(aop->buf_asm, 1024, "%s", buf);
}
aop->disasm_obj = &asm_obj;
if (aop->inst_len > 0)
memcpy(aop->buf, asm_obj.code, aop->inst_len);

View file

@ -27,7 +27,7 @@ static int rasm_show_help()
" -e Use big endian\n"
" If '-l' value is greater than output length, output is padded with nops\n"
" If the last argument is '-' reads from stdin\n");
return R_TRUE;
return 0;
}
static int rasm_disasm(char *buf, u64 offset, u64 len, int ascii, int bin)
@ -81,25 +81,21 @@ static int rasm_asm(char *buf, u64 offset, u64 len, int bin)
}
#endif
r_asm_set_pc(&a, offset);
idx = r_asm_massemble(&a, &aop, buf);
if (!(idx = r_asm_massemble(&a, &aop, buf)))
return 0;
if (bin)
for (i = 0; i < idx; i++)
printf("%c", aop.buf[i]);
else printf("%s\n", aop.buf_hex);
for (ret = 0; idx < len; idx+=ret) {
ret = r_asm_assemble(&a, &aop, "nop");
if (ret) {
if (bin)
for (i = 0; i < ret; i++)
printf("%c", aop.buf[i]);
else printf("%s", aop.buf_hex);
} else {
fprintf(stderr, "invalid\n");
if (!(ret = r_asm_assemble(&a, &aop, "nop")))
return 0;
}
if (bin)
for (i = 0; i < ret; i++)
printf("%c", aop.buf[i]);
else printf("%s", aop.buf_hex);
}
if (!bin && idx == len) printf("\n");
if (!bin && len && idx == len) printf("\n");
return idx;
}
@ -174,14 +170,14 @@ int main(int argc, char *argv[])
if (!r_asm_set(&a, str)) {
fprintf(stderr, "Error: Unknown plugin\n");
free (str);
return 1;
return 0;
}
free (str);
if (!strcmp(arch, "bf"))
ascii = 1;
} else if (!r_asm_set(&a, "asm_x86")) {
fprintf(stderr, "Error: Cannot find asm_x86 plugin\n");
return 1;
return 0;
}
if (!r_asm_set_bits(&a, bits))
fprintf(stderr, "cannot set bits (triying with 32)\n");
@ -201,13 +197,22 @@ int main(int argc, char *argv[])
}
idx += ret;
offset += ret;
if (!ret || (len && idx >= len))
if (!ret) {
fprintf(stderr, "invalid\n");
return 0;
}
if (len && idx >= len)
break;
}
return idx;
}
if (dis) return rasm_disasm(argv[optind], offset, len, ascii, bin);
else return rasm_asm(argv[optind], offset, len, bin);
if (dis) ret = rasm_disasm(argv[optind], offset, len, ascii, bin);
else ret = rasm_asm(argv[optind], offset, len, bin);
if (!ret) {
fprintf(stderr, "invalid\n");
return 0;
}
return ret;
}
return 0;