tags:

views:

543

answers:

4

I want to compile my C-code without the (g)libc. How can I deactivate it and which functions depend on it?

I tried -nostdlib but it doesn't help: The code is compilable and runs, but I can still find the name of the libc in the hexdump of my executable.

+2  A: 

The simplest way to is compile the C code to object files (gcc -c to get some *.o files) and then link them directly with the linker (ld). You will have to link your object files with a few extra object files such as /usr/lib/crt1.o in order to get a working executable (between the entry point, as seen by the kernel, and the main() function, there is a bit of work to do). To know what to link with, try linking with the glibc, using gcc -v: this should show you what normally comes into the executable.

You will find that gcc generates code which may have some dependencies to a few hidden functions. Most of them are in libgcc.a. There may also be hidden calls to memcpy(), memmove(), memset() and memcmp(), which are in the libc, so you may have to provide your own versions (which is not hard, at least as long as you are not too picky about performance).

Things might get clearer at times if you look at the produced assembly (use the -S flag).

Thomas Pornin
I have to use _start instead of main, but when I try to call a libc function gcc doesn't complain. Does the libc-link disappear if I remove all libc-calls?
knizz
Not directly. If you try `gcc -v`, you will see that `gcc` gives some object files to the linker (the `*.o`). The linker includes all object files it is given. "Disappearance" occurs only with libraries (`*.a`) because they are repositories of object files that the linker is free to use or not to use.
Thomas Pornin
+5  A: 

If you compile your code with -nostdlib, you won't be able to call any C library functions (of course), but you also don't get the regular C bootstrap code. In particular, the real entry point of a program on linux is not main(), but rather a function called _start(). The standard libraries normally provide a version of this that runs some initialization code, then calls main().

Try compiling this with gcc -nostdlib:

void _start() {
    /* exit system call */
    asm("movl $1,%eax;"
        "xorl %ebx,%ebx;"
        "int  $0x80"
    );
}

The _start() function should always end with a call to exit (or other non-returning system call such as exec). The above example invokes the system call directly with inline assembly since the usual exit() is not available.

ataylor
+4  A: 

http://blog.ksplice.com/2010/03/libc-free-world/ has a very good description of precisely controlling the gcc's programatic output.

Edit: They (ksplice) just put out part 2 of the above tutorial/guide. See it here: http://blog.ksplice.com/2010/04/libc-free-world-2/ This mostly deals with linker settings to remove unnecessary fluff from files.

Adam Shiemke
+1, very informative link.
Tim Post
+2  A: 

Here is an excellent article on optimizing elf binaries which covers stripping stdlib off executables: A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux

Michał Trybus
I already read that. :D But thank you for posting this.
knizz