Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling without libc

Tags:

c

gcc

glibc

libc

demo

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.

like image 331
u149796 Avatar asked Mar 30 '10 20:03

u149796


People also ask

Is libc automatically linked?

libc is always searched automatically.

Does GCC depend on glibc?

Is it possible to compile a program using gcc without depending on glibc? Yes, there are alternative libc versions, such as Musl, uClibc, dietLibc, etc. See their documentation on how to use them. Your problem does not appear to be a GLIBC dependency, but rather a mismatch between your build and your target hosts.

Is libc installed?

Here's how to check if a library is installed: Type ldconfig -p | grep libc++ into the terminal. It does not matter what system you are using. If libc++ is not installed, the terminal will not say anything.

How do I know if libc is installed?

The process for checking your installed version of libc will be the same regardless of your Linux distro. Simply use the ldd command as seen below. $ ldd --version ldd (Ubuntu GLIBC 2.35-0ubuntu3) 2.35 ... As you can see from the first line of the output and in the previous screenshot, we have version 2.35 installed.


2 Answers

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:

void _start() {      /* main body of program: call main(), etc */      /* 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.

like image 142
ataylor Avatar answered Sep 28 '22 03:09

ataylor


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

like image 39
Thomas Pornin Avatar answered Sep 28 '22 03:09

Thomas Pornin