First you need to be able to command your toolchain (gcc?) to not to include anything extra other than your code. Something like -nostartfiles -nodefaultlibs
to gcc
should work.
Then you need to be nice working with Linux
, meaning your elf need to be loaded properly by the os, meaning it needs to have _start
point visible. Below would be such an example:
void _start() __attribute__ ((naked));
void _start() {
main();
asm volatile(
"mov r7, #1\n" /* exit */
"svc #0\n"
);
}
You can then create a main
which contain what you want to do.
int main() {
linuxc('X');
return 42;
}
Then doing extra with write syscall...
void linuxc(int c) {
asm volatile(
"mov r0, #1\n" /* stdout */
"mov r1, %[buf]\n" /* write buffer */
"mov r2, #1\n" /* size */
"mov r7, #4\n" /* write syscall */
"svc #0\n"
: /* output */ : [buf] "r" (&c) : "r0", "r1", "r2", "r7", "memory"
);
}
I have a more complete example of that at my github. I like the teensy one most.