Compiling without libc

46,600

Solution 1

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.

Solution 2

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

Share:
46,600
u149796
Author by

u149796

Updated on July 05, 2022

Comments

  • u149796
    u149796 almost 2 years

    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.

  • u149796
    u149796 about 14 years
    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?
  • Thomas Pornin
    Thomas Pornin about 14 years
    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.
  • sigalor
    sigalor about 8 years
    For 64 bit, the assembly code has to look like this: asm("mov rax,60; mov rdi,0; syscall").
  • lanoxx
    lanoxx about 8 years
    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)
  • Destructor
    Destructor almost 7 years
    @ataylor: why _start() function should always end with call to exit() ? What if I don't write exit() in start() function ?
  • Moonchild
    Moonchild almost 7 years
    @Destructor on my system, it hangs for a few seconds, then segfaults.
  • Mike Bell
    Mike Bell about 6 years
    $ gcc -nostdlib main.c Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • SilverbackNet
    SilverbackNet almost 6 years
    You have to combine -nostdlib with -nostartfiles (now?), in my experience, to get the magic stripped-down no-main experience.
  • gfan
    gfan over 5 years
    @Destructor, from the disassembly code by objdump -dS exe_file, 080480d8 <_start>: .globl _start _start:call main 80480d8: e8 00 00 00 00 call 80480dd <main> 080480dd <main>: int main() { 80480dd: 55 push %ebp ..... 80480ef: c9 leave 80480f0: c3 ret . After the call, the next op is 80480dd. If you don't want exit(), make another idea to skip 80480dd~80480f0. _exit() does
  • Ivan
    Ivan over 4 years
    To call this function anything other than _start, use -e linker option to set entry point: gcc -nostdlib -Wl,-efunction1
  • Peter Cordes
    Peter Cordes almost 4 years
    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
  • Peter Cordes
    Peter Cordes about 2 years
    Update: How Get arguments value using inline assembly in C without Glibc? shows how to define a _start in C if you really want to abuse the compiler this way, using __attribute__((force_align_arg_pointer)).