* Initial hello world for rarc2 in ARM with GAS working

- Few changes needed to work with rasm
* Cleanup some warnings
* Handle foo() as a function definition, so emit frame in rarc
This commit is contained in:
pancake 2010-09-15 10:50:43 +02:00
parent 1b1599c6df
commit 9b2d428d1b
9 changed files with 58 additions and 63 deletions

View File

@ -26,7 +26,7 @@ static char *curfile = NULL;
static ut64 bsize = 4096;
static int hexstr = 0;
static struct r_print_t *pr = NULL;
LIST_HEAD(kws_head);
static LIST_HEAD(kws_head);
typedef struct {
char *str;
@ -34,7 +34,6 @@ typedef struct {
} BoxedString;
static int hit(RSearchKeyword *kw, void *user, ut64 addr) {
//const ut8 *buf = (ut8*)user;
int delta = addr-cur;
if (rad) {
printf("f hit%d_%d 0x%08"PFMT64x" ; %s\n", 0, kw->count, addr, curfile);
@ -72,40 +71,43 @@ static int show_help(char *argv0, int line) {
static int rafind_open(char *file) {
int ret, last = 0;
struct list_head *pos;
io = r_io_new();
fd = r_io_open(io, file, R_IO_READ, 0);
if (fd == -1) {
fprintf (stderr, "Cannot open file '%s'\n", file);
eprintf ("Cannot open file '%s'\n", file);
return 1;
}
r_cons_new();
rs = r_search_new(mode);
buffer = malloc(bsize);
r_search_set_callback(rs, &hit, buffer);
if (to == -1) {
to = r_io_size(io, fd);
rs = r_search_new (mode);
buffer = malloc (bsize);
if (buffer==NULL) {
eprintf ("Cannot allocate %"PFMT64d" bytes\n", bsize);
return 1;
}
r_search_set_callback (rs, &hit, buffer);
if (to == -1)
to = r_io_size(io, fd);
if (mode == R_SEARCH_KEYWORD) {
list_for_each(pos, &(kws_head)) {
BoxedString *kw = list_entry(pos, BoxedString, list);
r_search_kw_add (rs, (hexstr)?
r_search_keyword_new_hex (kw->str, mask, NULL) :
r_search_keyword_new_str (kw->str, mask, NULL));
free(kw);
free (kw);
}
}
curfile = file;
r_search_begin(rs);
r_io_seek(io, from, R_IO_SEEK_SET);
r_search_begin (rs);
r_io_seek (io, from, R_IO_SEEK_SET);
//printf("; %s 0x%08"PFMT64x"-0x%08"PFMT64x"\n", file, from, to);
for(cur=from; !last && cur<to;cur+=bsize) {
if ((cur+bsize)>to) {
bsize = to-cur;
last=1;
}
ret = r_io_read(io, buffer, bsize);
ret = r_io_read (io, buffer, bsize);
if (ret == 0) {
if (nonstop) continue;
// fprintf(stderr, "Error reading at 0x%08"PFMT64x"\n", cur);
@ -113,9 +115,9 @@ static int rafind_open(char *file) {
}
if (ret != bsize)
bsize = ret;
r_search_update_i(rs, cur, buffer, bsize);
r_search_update_i (rs, cur, buffer, bsize);
}
rs = r_search_free(rs);
rs = r_search_free (rs);
return 0;
}
@ -173,10 +175,10 @@ int main(int argc, char **argv) {
}
if (optind == argc)
return show_help(argv[0], 1);
return show_help (argv[0], 1);
for (;optind < argc;optind++)
rafind_open(argv[optind]);
rafind_open (argv[optind]);
return 0;
}

View File

@ -11,7 +11,7 @@ static int do_hash(const char *algo, const ut8 *buf, int len, int bsize, int rad
ut64 algobit = r_hash_name_to_bits (algo);
if (algobit == R_HASH_NONE) {
fprintf(stderr, "Invalid hashing algorithm specified\n");
eprintf ("Invalid hashing algorithm specified\n");
return 1;
}
if (bsize>len)

View File

@ -1,6 +1,10 @@
RCC : Ralang/Relocatable Code Compiler
======================================
TODO:
-----
add support for /* comments */
Compiler pipeline: the picture
------------------------------

View File

@ -20,15 +20,15 @@ static char *emit_syscall (int num) {
}
static void emit_frame (int sz) {
rcc_printf (" push {fp,lr}\n");
if (sz>0) rcc_printf (
" push {fp,lr}\n"
//" mov "R_BP", "R_SP"\n"
" add fp, sp, 4\n" // size of arguments
" sub sp, %d\n", sz); // size of stackframe 8, 16, ..
" add fp, sp, $4\n" // size of arguments
" sub sp, $%d\n", sz); // size of stackframe 8, 16, ..
}
static void emit_frame_end (int sz, int ctx) {
if (sz>0) rcc_printf (" add "R_SP", %d\n", sz);
if (sz>0) rcc_printf (" add sp, fp, $%d\n", sz);
if (ctx>0) rcc_puts (" pop {fp,pc}\n");
}
@ -54,14 +54,16 @@ static void emit_syscall_args(int nargs) {
}
static void emit_set_string(const char *dstvar, const char *str, int j) {
int off = 0;
off = strlen (str);
off += (off%4);
rcc_printf (" add pc, %d\n", off);
int rest, off = 0;
off = strlen (str)+1;
rest = (off%4);
if (rest) rest = 4-rest;
off += rest-8;
rcc_printf (" add pc, $%d\n", (off));
// XXX: does not handle \n and so on.. must use r_util
rcc_printf (".string \"%s\"\n", str);
if (off%4) rcc_printf (".fill %d, 1, 0\n", (off%4));
rcc_printf (" sub r0, pc, %d\n", off);
if (rest) rcc_printf (".fill %d, 1, 0\n", (rest));
rcc_printf (" sub r0, pc, $%d\n", off+16);
}
static void emit_call(const char *str, int atr) {
@ -75,6 +77,8 @@ static void emit_arg (int xs, int num, const char *str) {
int d = atoi (str);
if (!attsyntax && (*str=='$'))
str++;
// ARGMENTS ONLY IN R0, R1, .. ooh common!
return; // XXX
switch (xs) {
case 0:
rcc_printf (" push {%s}\n", str);
@ -95,7 +99,7 @@ static void emit_get_result(const char *ocn) {
}
static void emit_restore_stack (int size) {
rcc_printf(" add sp, %d\n", size);
//rcc_printf(" add sp, %d\n", size);
}
static void emit_get_while_end (char *str, const char *ctxpush, const char *label) {
@ -103,9 +107,10 @@ static void emit_get_while_end (char *str, const char *ctxpush, const char *labe
}
static void emit_while_end (const char *labelback) {
rcc_printf (" pop "R_AX"\n");
rcc_printf (" cmp "R_AX", "R_AX"\n"); // XXX MUST SUPPORT != 0 COMPARE HERE
rcc_printf (" beq %s\n", labelback);
rcc_printf (
" pop "R_AX"\n"
" cmp "R_AX", "R_AX"\n" // XXX MUST SUPPORT != 0 COMPARE HERE
" beq %s\n", labelback);
}
static void emit_get_var (int type, char *out, int idx) {
@ -150,13 +155,8 @@ static void emit_branch(char *b, char *g, char *e, char *n, int sz, const char *
if (*arg=='=') arg++; /* for <=, >=, ... */
p = mk_var (str, arg, 0);
if (attsyntax) {
rcc_printf (" pop %%"R_AX"\n"); /* TODO: add support for more than one arg get arg0 */
rcc_printf (" cmp%c %s, %%"R_AX"\n", sz, p);
} else {
rcc_printf (" pop "R_AX"\n"); /* TODO: add support for more than one arg get arg0 */
rcc_printf (" cmp %s, "R_AX"\n", p);
}
rcc_printf (" pop "R_AX"\n"); /* TODO: add support for more than one arg get arg0 */
rcc_printf (" cmp %s, "R_AX"\n", p);
// if (context>0)
rcc_printf (" %s %s\n", op, dst);
}

View File

@ -629,6 +629,7 @@ static int parsechar(char c) {
elem[elem_n] = '\0';
if (elem_n > 0)
rcc_element (elem);
else emit->frame (0);
elem_n = 0;
} else elem[elem_n++] = c;
elem[elem_n] = '\0';

View File

@ -18,7 +18,7 @@ enum {
LAST
};
#define eprintf(x,y...) (fprintf(stderr,"\x1b[31m"x"\x1b[0m",##y),1)
#define eprintf(x,y...) (fprintf(stderr,"\x1b[33m"x"\x1b[0m",##y),1)
#define FREE(x) free(x); x=NULL
#define IS_VAR(x) (x[0]=='.'||((x[0]=='*'||x[0]=='&')&&x[1]=='.'))
#define MAX 255

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009 nibble<.ds@gmail.com> */
/* radare - LGPL - Copyright 2009-2010 nibble<.ds@gmail.com> */
#include <stdio.h>
#include <string.h>
@ -13,7 +13,6 @@ static struct r_lib_t *l;
static struct r_asm_t *a;
static int coutput = R_FALSE;
// TODO: remove or gtfo
static void r_asm_list(RAsm *a) {
RAsmPlugin *h;
RListIter *iter;
@ -48,17 +47,15 @@ static int rasm_disasm(char *buf, ut64 offset, ut64 len, int ascii, int bin) {
ut64 word = 0, clen = 0;
if (bin) {
clen = len; //XXX
clen = len; // XXX
data = (ut8*)buf;
} else if (ascii) {
clen = strlen (buf);
data = (ut8*)buf;
} else {
while (ptr[0]) {
for (; *ptr; ptr++)
if (ptr[0]!= ' ' && ptr[0]!= '\n' && ptr[0]!= '\r')
if (0==(++word%2))clen++;
ptr += 1;
}
if (!(++word%2)) clen++;
data = alloca (clen);
if (r_hex_str2bin (buf, data)==-1)
return 0;
@ -97,13 +94,6 @@ static int rasm_asm(char *buf, ut64 offset, ut64 len, int bin) {
struct r_asm_aop_t aop;
int ret, idx, i;
#if 0
/* TODO: Arch, syntax... */
if (!r_asm_use(&a, "x86.olly")) {
fprintf(stderr, "Error: Cannot find asm_x86 plugin\n");
return 1;
}
#endif
r_asm_set_pc (a, offset);
if (!(acode = r_asm_massemble (a, buf)))
return 0;
@ -125,16 +115,14 @@ static int rasm_asm(char *buf, ut64 offset, ut64 len, int bin) {
}
/* asm callback */
static int __lib_asm_cb(struct r_lib_plugin_t *pl, void *user, void *data)
{
static int __lib_asm_cb(struct r_lib_plugin_t *pl, void *user, void *data) {
RAsmPlugin *hand = (struct r_asm_plugin_t *)data;
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[])
{
int main(int argc, char *argv[]) {
char *arch = NULL, *file = NULL;
ut64 offset = 0x8048000;
int dis = 0, ascii = 0, bin = 0, ret = 0, bits = 32, c;
@ -253,8 +241,7 @@ int main(int argc, char *argv[])
}
if (dis) ret = rasm_disasm (argv[optind], offset, len, ascii, bin);
else ret = rasm_asm (argv[optind], offset, len, bin);
if (!ret)
eprintf ("invalid\n");
if (!ret) eprintf ("invalid\n");
return ret;
}
return 0;

View File

@ -19,7 +19,7 @@ int main(int argc, char **argv) {
if (argc == 1) {
while (!feof (stdin)) {
fgets (buf, 1023, stdin);
fgets (buf, sizeof (buf)-1, stdin);
if (feof (stdin)) break;
buf[strlen (buf)-1] = '\0';
if (!rax (buf)) break;

View File

@ -17,7 +17,7 @@ static int r_asm_pseudo_align(struct r_asm_aop_t *aop, char *input) {
}
static int r_asm_pseudo_string(struct r_asm_aop_t *aop, char *input) {
int len = strlen (input)+1;
int len = r_str_escape (input);
r_hex_bin2str ((ut8*)input, len, aop->buf_hex);
strncpy ((char*)aop->buf, input, R_ASM_BUFSIZE);
return len;
@ -319,7 +319,8 @@ R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf) {
for (ptr_start = buf_token; *ptr_start &&
isseparator (*ptr_start); ptr_start++);
ptr = strchr (ptr_start, '#'); /* Comments */
if (ptr) *ptr = '\0';
if (ptr && !R_BETWEEN (ptr[1],'0','9'))
*ptr = '\0';
if (stage == 2) {
r_asm_set_pc (a, a->pc + ret);
off = a->pc;