title: Why is the assembler using FS:40
body: <p>When I initialize an array in c, the assembly code generated by gcc adds a line that looks like this:</p>
  
  <pre><code>mov rax, QWORD PTR fs:40
  </code></pre>
  
  <p>I'm having a hard time understanding what fs:40 is doing. In gdb the fs pointer is pointing to zero:</p>
  
  <pre><code>(gdb) i r fs
  fs             0x0  0
  </code></pre>
  
  <p>So based on <a href="http://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64" rel="nofollow noreferrer">this</a> the first 128 bytes is just garbage. So why is it copying 8 bytes of garbage after the array? </p>
  
  <p>This is my code:</p>
  
  <pre><code>int main(){
      char buffer[8];
  }
  </code></pre>
  
  <p>This is how I compiled it into intel assembly format:</p>
  
  <pre><code> gcc -S -O0 -masm=intel ch2.c -fno-asynchronous-unwind-tables
  </code></pre>
  
  <p>This is the output assembly file I get. I'm only showing the "Main" function area:</p>
  
  <pre><code>main:
      push    rbp
      mov rbp, rsp
      sub rsp, 16
      mov rax, QWORD PTR fs:40   ;;&lt;&lt;&lt; this is the line
      mov QWORD PTR -8[rbp], rax
      xor eax, eax
      mov eax, 0
      mov rdx, QWORD PTR -8[rbp]
      xor rdx, QWORD PTR fs:40
      je  .L4
      call    __stack_chk_fail@PLT
  .L4:
      leave
      ret
  </code></pre>
  
  <p>Thanks in advance.</p>