title: How should I write code for -nostartfiles or -nostdlib options for gcc?
body: <p>I came across <code>-nostartfiles</code> and <code>-nostdlib</code> options under gcc compilation options. I, however, do not understand as to how one can write code for the above options. Do you need to write every startup function (like <code>_start</code>, <code>__libc_csu_init</code> etc.)?</p>
  
  <p>Can I get a simplified code for both these options just to understand it's working?</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>Also I just discovered that using <code>static</code> along with <code>nostartfiles</code> throws a <code>segmentation fault</code>. A simple c code to reproduce the same:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  void _start()
  {
      int x = main();
      exit(x);
  }
  
  int main()
  {
      printf("Hello world!\n");
      return 0;
  }
  </code></pre>
  
  <p>Compiled using:</p>
  
  <pre><code>gcc -nostartfiles -static -o foo foo.c
  </code></pre>
  
  <p>The output to the above code is a <code>segmentation fault</code>. Is it the expected output?</p>