title: Linux syscalls and errno
body: <p><strong>Context:</strong> I am trying to write a small C program with inline asm that should run under Linux on an x86_64 system and being compiled with gcc in order to better understand how syscalls work under Linux.</p>
  
  <p>My question is: How are error numbers returned from a syscall (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 <code>errno</code> variable. But where is the error number stored when I call a syscall directly through inline assembler? Will it be stored inside a separate register, or will it be encoded in <code>%rax</code>?</p>
  
  <p>Let take the write syscall on linux as an example:</p>
  
  <p>When call <code>write</code> then after the syscall returns I find it stores <code>0xfffffffffffffff2</code> inside <code>%rax</code>, do I need to 
  somehow extract the error code from that? </p>
  
  <p>If I have the error code number, where should I look to identify the actual error that occured? Lets say I get the number 5 returned, which header file do I need to consult to find the corresponding symbolic error name.</p>
  
  <p>I am calling the write syscall like this:</p>
  
  <pre><code>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 */
      );
  </code></pre>
  
  <p>with <code>result</code>, <code>msg</code> and <code>len</code> defined like so:</p>
  
  <pre><code>long result = 0;
  char* msg = "Hello World\n";
  long len = 12;
  </code></pre>