user14554
user14554

Reputation:

Syscall from inline asm in x86_64 Linux?

Why does this print garbage instead of exiting my program gracefully? I use system calls this way on BSD, and I wonder what would I need to make it work in Linux.

int
main(int argc, char **argv)
{
    __asm ("movq $1,%rax; movq $0,%rdi; syscall"); /* exit(0) ? */
    return 0;
}

Thanks.

Upvotes: 12

Views: 5417

Answers (2)

Sean A.O. Harney
Sean A.O. Harney

Reputation: 24507

Syscall 1 is exit on i386 but write on x86-64 I believe.

EDIT: this seems inaccurate: According to the web, which does not seem to have too much information about x86-64 Linux assembly this seems to be the expected register setup before the syscall instruction.

 rax  system call number
 rbx  arg0
 rcx  return address from syscall
 rdx  arg2
 rsi  arg3
 rdi  arg4
 r8   arg5
 r9   arg1    (expected by gcc in %rcx)
 r10-r15  should be saved/restored by C code
 rbp  dito What is dito??

Upvotes: 3

Marsh Ray
Marsh Ray

Reputation: 2875

Why does this print garbage instead of exiting my program gracefully?

Per CESA-2009-001, "Syscall 1 is exit on i386 but write on x86_64".

what would I need to make it work in Linux

Use the syscall ordinals from the current unistd_64.h

Hope this helps!

Upvotes: 13

Related Questions