Context: I am trying to write a small C program with built-in asm that should run under Linux on x86_64 and compiled with gcc to better understand how syscalls work under Linux.
My question is: how do error numbers return from a system call (e.g. write) in this environment? I understand that when I use a library such as glibc, it takes care of saving the resulting error code in the global variable errno . But where is the error number saved when I call syscall directly through the inline assembler? Will it be stored inside a separate register or will it be encoded in %rax ?
Take the Linux write script as an example:
When calling write after the syscall message is issued, I find that it stores 0xfffffffffffffff2 inside %rax , do I need to somehow extract the error code from this?
If I have an error code, where should I look for the actual error that occurred? Suppose I got a return number of 5, in which header file I need to consult to find the corresponding symbolic name for the error.
I call syscall like this:
asm ("mov $1,%%rax;" "mov $1,%%rdi;" "mov %1,%%rsi;" "mov %2,%%rdx;" "syscall;" "mov %%rax,%0;" : "=r" (result) : "r" (msg), "r" (len) : "%rdx", "%rsi", "%rax", "%rdi" /* EDIT: this is needed or else the registers will be overwritten */ );
with result , msg and len defined like this:
long result = 0; char* msg = "Hello World\n"; long len = 12;
source share