title: Build static ELF without libc using unistd.h from Linux headers
body: <p>I'm interested in building a static ELF program without (g)libc, using unistd.h provided by the Linux headers.</p>
  
  <p>I've read through these articles/question which give a rough idea of what I'm trying to do, but not quite:
  <a href="http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html" rel="nofollow noreferrer">http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2548486/compiling-without-libc">Compiling without libc</a></p>
  
  <p><a href="https://blogs.oracle.com/ksplice/entry/hello_from_a_libc_free" rel="nofollow noreferrer">https://blogs.oracle.com/ksplice/entry/hello_from_a_libc_free</a></p>
  
  <p>I have basic code which depends only on unistd.h, of which, my understanding is that each of those functions are provided by the kernel, and that libc should not be needed. Here's the path I've taken that seems the most promising:</p>
  
  <pre><code>    $ gcc -I /usr/include/asm/ -nostdlib grabbytes.c -o grabbytesstatic
      /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400144
      /tmp/ccn1mSkn.o: In function `main':
      grabbytes.c:(.text+0x38): undefined reference to `open'
      grabbytes.c:(.text+0x64): undefined reference to `lseek'
      grabbytes.c:(.text+0x8f): undefined reference to `lseek'
      grabbytes.c:(.text+0xaa): undefined reference to `read'
      grabbytes.c:(.text+0xc5): undefined reference to `write'
      grabbytes.c:(.text+0xe0): undefined reference to `read'
      collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>Before this, I had to manually define SEEK_END and SEEK_SET according to the values found in the kernel headers. Else it would error saying that those were not defined, which makes sense.</p>
  
  <p>I imagine that I need to link into an unstripped vmlinux to provide the symbols to utilize. However, I read through the symbols and while there were plenty of llseeks, they were not llseek verbatim.</p>
  
  <p>So my question can go in a few directions:</p>
  
  <p>How can I specify an ELF file to utilize symbols from? And I'm guessing if/how that's possible, the symbols won't match up. If this is correct, is there an existing header file which will redefine llseek and default_llseek or whatever is exactly in the kernel?</p>
  
  <p>Is there a better way to write Posix code in C without a libc?</p>
  
  <p>My goal is to write or port fairly standard C code using (perhaps solely) unistd.h and invoke it without libc. I'm probably okay without a few unistd functions, and am not sure which ones exist "purely" as kernel calls or not. I love assembly, but that's not my goal here. Hoping to stay as strictly C as possible (I'm fine with a few external assembly files if I have to), to allow for a libc-less static system at some point.</p>
  
  <p>Thank you for reading!</p>