I'm trying to produce a executable without dependency on libc (or any other). First I have done this:
// test.c
void _start()
{
// write(1, "hello!\n", 7);
asm ("int $0x80"::"a"(4), "b"(1), "c"("hello!\n"), "d"(7));
// exit(0);
asm ("int $0x80"::"a"(1), "b"(0));
}
Compiling with gcc -m32 -nostdlib test.c -o test
:
hello
So far so good. Later I attempted to use some more "advanced" C like long long
. On 32-bit platforms (my case) this requires libgcc
:
// test.c
void _start()
{
volatile long long int a = 10;
volatile long long int b = 5;
volatile int c = a/b; // Implemented as a call to '__divdi3'
}
This fails compilation with undefined reference to '__divdi3'
. Seems correct as I didn't actually told it to link. But adding the flag -static-libgcc
does not fix the issue! Why?
Note that I cannot link dynamically to libgcc. The following must hold true:
$ ldd test
not a dynamic executable
I'm compiling from 64-bit Ubuntu 14.04 with gcc 4.8.2 (nothing fancy).