87

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.

5
  • 2
    -nostdlib should do it, what platform/compiler version are you using? Commented Mar 30, 2010 at 20:36
  • "doesn't help" as in that didn't disable the library, or you couldn't compile anything with that flag? Commented Mar 30, 2010 at 20:47
  • 4
    You probably also want -nostartupfiles. Commented Mar 30, 2010 at 22:03
  • 10
    There is no -nostartupfiles option. You probably mean -nostartfiles, which is already implied by -nostdlib. Commented Mar 31, 2010 at 18:24
  • 1
    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 ](muppetlabs.com/~breadbox/software/tiny/teensy.html) Commented Apr 4, 2010 at 15:12

2 Answers 2

112

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 -m32:

// Tell the compiler incoming stack alignment is not RSP%16==8 or ESP%16==12
__attribute__((force_align_arg_pointer))
void _start() {

    /* main body of program: call main(), etc */

    /* exit system call */
    asm("movl $1,%eax;"
        "xorl %ebx,%ebx;"
        "int  $0x80"
    );
    __builtin_unreachable();  // tell the compiler to make sure side effects are done before the asm statement
}

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.

Sign up to request clarification or add additional context in comments.

11 Comments

For 64 bit, the assembly code has to look like this: asm("mov rax,60; mov rdi,0; syscall").
Adding to @sigalor's comment, to compile with gcc you will need to use AT&T syntax so this should look like the following: asm(mov $60,%rax; mov $0,%rdi; syscall)
@ataylor: why _start() function should always end with call to exit() ? What if I don't write exit() in start() function ?
@Destructor on my system, it hangs for a few seconds, then segfaults.
Note that the stack is aligned differently on entry to _start: no return address on the stack so it's still 16-byte aligned. But GCC will assume that it's a normal function, so it will violate the ABI when it calls other functions. Use -mincoming-stack-boundary=2 for that file (docs) to tell GCC that the stack might only be 4-byte aligned on function entry, or maybe an __attribte__((target("something"))) just on _start
|
13

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).

2 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.