Search code examples
clinuxstatic-linking

Crash in static i386 Linux exe


This simple testcase:

#include <unistd.h>

void _start(void) {
        _exit(0);
}

crashes when run on Debian 12.6 i386.

Compiled with gcc -lc -nostdlib -static -Os. Note that this is on an i386 version of Debian.

In gdb:

 0x804905b <_exit+59>                    call   *%gs:0x10  

$gs is 0.

It still crashes if I remove the call to _exit.

Is Debian i386 borked when it comes to running static exes without main? I have no problem on amd64 systems (FreeBSD 14.0 with clang and Fedora 39). Also no problem on FreeBSD when compiling for i386.

UPDATE: On Fedora 39 amd64 glibc 2.39 GCC 14.2.1, same problem.


Solution

  • In the manual page of exit according to the following:

    Up to glibc 2.3, the _exit() wrapper function invoked the kernel system call of the same name. Since glibc 2.3, the wrapper function invokes exit_group(2), in order to terminate all of the threads in a process.

    the _exit function needs to call a library function with the 2.3 version of glibc. I think debian 12.6 uses glibc 2.3 and other systems you mentioned uses a prior version. However, I am not sure how you were able to compile it statically if this is the case.