README
1r_egg
2===== --pancake
3
4This is a rewrite of rarc2, a relocatable code compiler for radare.
5
6Syntax of the language
7======================
8The code of r_egg is compiled as in a flow. It is a one-pass compiler;
9this means that you have to define the proper stackframe size at the
10beginning of the function, and you have to define the functions in
11order to avoid getting compilation errors.
12
13The compiler generates assembly code for x86-{32,64} and arm. But it aims
14to support more platforms. This code is the compiled with r_asm and
15injected into a tiny binary with r_bin.
16
17You may like to use r_egg to create standalone binaries, position-
18independent raw eggs to be injected on running processes or to patch
19on-disk binaries.
20
21The generated code is not yet optimized, but it's safe to be executed
22at any place in the code.
23
24Preprocessor
25------------
26There's no standard preprocessor integrated with it. But if you feel
27the need to use one you may use cpp or spp*.
28
29 * hg clone http://hg.youterm.com/spp
30
31Aliases
32-------
33Sometimes you just need to replace at compile time a single entity on
34multiple places. Aliases are translated into 'equ' statements in assembly
35language. This is just an assembler-level keyword redefinition.
36
37 AF_INET@alias(2);
38 printf@alias(0x8053940);
39
40Includes
41--------
42Use cat(1) or the preprocessor to concatenate multiple files to be compiled.
43It's not a task of a compiler to look for external sources, so it's a
44delegated task right now.. but we will probably add native support for
45spp (merge into)
46
47TODO: this is not yet implemented
48
49 INCDIR@alias("/usr/include/ragg2");
50 sys-osx.r@include(INCDIR);
51
52Hashbang
53--------
54eggs can use a hashbang to make them executable.
55
56 $ head -n1 hello.r
57 #!/usr/bin/ragg2 -X
58 $ ./hello.r
59 Hello World!
60
61Main
62----
63The execution of the code is done as in a flow. The first function to be
64defined will be the first one to be executed. If you want to run main()
65just do like this:
66
67 #!/usr/bin/ragg2 -X
68 main();
69 ...
70 main@global(128,64) {
71 ...
72
73Function definition
74-------------------
75You may like to split up your code into several code blocks. Those blocks
76are bound to a label followed by root brackets '{ ... }'
77
78Function signatures
79-------------------
80name@type(stackframesize,staticframesize) { body }
81
82name : name of the function to define
83type : see function types below
84stackframesize : get space from stack to store local variables
85staticframesize : get space from stack to store static variables (strings)
86body : code of the function
87
88Function types
89--------------
90 alias ; Used to create aliases
91 data ; the body of the block is defined in .data
92 inline ; the function body is inlined when called
93 global ; make the symbol global
94 fastcall ; function that is called using the fast calling convention
95 syscall ; define syscall calling convention signature
96
97Syscalls
98--------
99r_egg offers a syntax sugar for defining syscalls. The syntax is like this:
100
101 exit@syscall(1);
102 @syscall() {
103 : mov eax, `.arg`
104 : int 0x80
105 }
106 main@global() {
107 exit (0);
108 }
109
110Libraries
111---------
112At the moment there is no support for linking r_egg programs to system
113libraries. but if you inject the code into a program (disk/memory) you
114can define the address of each function using the @alias syntax.
115
116Core library
117------------
118There's a work-in-progress libc-like library written completely in r_egg
119
120Variables
121---------
122.arg
123.arg0
124.arg1
125.arg2
126.var0
127.var2
128.fix
129.ret ; eax for x86, r0 for arm
130.bp
131.pc
132.sp
133
134Arrays
135------
136Supported as raw pointers. TODO: enhance this feature
137
138Tracing
139-------
140Sometimes r_egg programs will break or just not work as expected. Use the
141'trace' architecture to get a arch-backend call trace:
142
143 $ ragg2 -a trace -s yourprogram.r
144
145Pointers
146--------
147TODO: Theorically '*' is used to get contents of a memory pointer.
148
149Virtual registers
150-----------------
151TODO: a0, a1, a2, a3, sp, fp, bp, pc
152
153Return values
154-------------
155The return value is stored in the a0 register, this register is set when
156calling a function or when typing a variable name without assignment.
157
158 $ cat test.r
159 add@global(4) {
160 .var0 = .arg0 + .arg1;
161 .var0;
162 }
163 main@global() {
164 add (3,4);
165 }
166
167 $ ragg2 -F -o test test.r
168 $ ./test
169 $ echo $?
170 7
171
172Traps
173-----
174Each architecture have a different instruction to break the execution of
175the program. REgg language captures calls to 'break()' to run the emit_trap
176callback of the selected arch. The
177
178 break(); --> compiles into 'int3' on x86
179 break; --> compiles into 'int3' on x86
180
181Inline assembly
182---------------
183Lines prefixed with ':' char are just inlined in the output assembly.
184
185 : jmp 0x8048400
186 : .byte 33,44
187
188Labels
189------
190You can define labels using the ':' keyword like this:
191
192 :label_name:
193 /* loop forever */
194 goto(label_name)
195
196Control flow
197------------
198 goto (addr) -- branch execution
199 while (cond)
200 if (cond)
201 break () -- executes a trap instruction
202
203Comments
204--------
205Supported syntax for comments are:
206
207 /* multiline comment */'
208 // single line comment
209 # single line comment
210