Running syscall without libc using the ARM built-in assembly

I want to write a tiny standalone executable without using libc. what I need to model some libc functions is to have a function to make system calls using the built-in assembly:

int syscall(int a,...) {
return __asm__ volatile (/* DO STH HERE */);
}

I use a Linux processor and an ARM.

EDIT: found a solution:

int syscall(int n,...) {
return __asm__ volatile ("mov r7,r0\nmov r0,r1\nmov r1,r2\nmov r2,r3\nswi #1\n");
}
+3
source share
1 answer

First you need to specify the toolchain (gcc?) Command so that you don’t include anything more than your code. Something like -nostartfiles -nodefaultlibsto is needed gcc.

Linux, , os, _start. :

void _start() __attribute__ ((naked));
void _start() {
    main();
    asm volatile(
        "mov r7, #1\n" /* exit */
        "svc #0\n"
    );
}

main, , .

int main() {
    linuxc('X');
    return 42;
}

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"
    );
}

my github. teensy.

+2

All Articles