Reputation: 11
I am trying to compile binary with nostdlib flag on Aarch64 platform.
I've dealt successfully with it on x86-64 platform this way:
void _start() {
/* main body of program: call main(), etc */
/* exit system call */
asm("movl $1,%eax;"
"xorl %ebx,%ebx;"
"int $0x80"
);
}
Is there any analogue to do the same thing on aarch64 platform?(specifically system exit call)
Upvotes: 0
Views: 619
Reputation: 5895
The example hereafter should work on an aarch64-linux-gnu system - It does work using running qemu-aarch64 3.0 on my x86_64 linux system.
The most concise/loosely coupled source of information for learning purpose would be musl-libc source code in my humble opinion:
We should then use:
static inline long __syscall1(long n, long a)
{
register long x8 __asm__("x8") = n;
register long x0 __asm__("x0") = a;
__asm_syscall("r"(x8), "0"(x0));
}
and __NR_exit:
#define __NR_exit 93
#define __NR_exit_group 94
A basic example in C would be syscall-exit.c:
#include "syscall_arch.h"
#include "syscall.h.in"
int main(void)
{
// exiting with return code 1.
__syscall1(__NR_exit, 1);
// we should have exited.
for (;;);
}
Compiling/executing/checking return code:
/opt/linaro/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc -static -O0 -o exit-syscall exit-syscall.c
qemu-aarch64 exit-syscall
echo $?
1
A close look at the generated code for main() and __syscall1() using:
/opt/linaro/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-objdump -D exit-syscall > exit-syscall.lst
Would look like:
0000000000400554 <main>:
400554: a9bf7bfd stp x29, x30, [sp, #-16]!
400558: 910003fd mov x29, sp
40055c: d2800021 mov x1, #0x1 // #1
400560: d2800ba0 mov x0, #0x5d // #93
400564: 97fffff4 bl 400534 <__syscall1>
0000000000400534 <__syscall1>:
400534: d10043ff sub sp, sp, #0x10
400538: f90007e0 str x0, [sp, #8]
40053c: f90003e1 str x1, [sp]
400540: f94007e8 ldr x8, [sp, #8]
400544: f94003e0 ldr x0, [sp]
400548: d4000001 svc #0x0
40054c: 910043ff add sp, sp, #0x10
400550: d65f03c0 ret
See document "Procedure Call Standard for the ARM 64-bit Architecture(AArch64)" for more information.
Therefore, an Aarch64 equivalent of your x86_64 code would be exit-asm.c :
void main(void) {
/* exit system call - calling NR_exit with 1 as the return code*/
asm("mov x0, #1;"
"mov x8, #93;"
"svc #0x0;"
);
for (;;);
}
/opt/linaro/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc -static -o example example.c
qemu-aarch64 example
echo $?
1
Please note that glibc implementation of exit() does call __NR_exit_group prior to call __NR_exit.
Upvotes: 3