title: GAS: Explanation of .cfi_def_cfa_offset
body: <p>I would like an explanation for the values used with the .cfi_def_cfa_offset directives in assembly generated by GCC. I know vaguely that the .cfi directives are involved in call frames and stack unwinding, but I would like a more detailed explanation of why, for example, the values 16 and 8 are used in the assembly outputted by GCC in compiling the following C program on my 64-bit Ubuntu machine.</p>
  
  <p>The C program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(int argc, char** argv)
  {
          printf("%d", 0);
          return 0;
  }
  </code></pre>
  
  <p>I invoked GCC on the source file test.c as follows: <code>gcc -S -O3 test.c</code>. I know that -O3 enables nonstandard optimization, but I wanted to limit the size of the generated assembly for the sake of brevity.</p>
  
  <p>The generated assembly:</p>
  
  <pre><code>        .file   "test.c"
          .section        .rodata.str1.1,"aMS",@progbits,1
  .LC0:
          .string "%d"
          .text
          .p2align 4,,15
  .globl main
          .type   main, @function
  main:
  .LFB22:
          .cfi_startproc
          subq    $8, %rsp
          .cfi_def_cfa_offset 16
          xorl    %edx, %edx
          movl    $.LC0, %esi
          movl    $1, %edi
          xorl    %eax, %eax
          call    __printf_chk
          xorl    %eax, %eax
          addq    $8, %rsp
          .cfi_def_cfa_offset 8
          ret
              .cfi_endproc
  .LFE22:
          .size   main, .-main
          .ident  "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2"
          .section        .note.GNU-stack,"",@progbits
  </code></pre>
  
  <p>Why are the values 16 and 8 used for the .cfi_def_cfa_offset directives in the generated assembly? Also, why is the number 22 used for the local function begin and function end labels?</p>