# ENOEXEC while exec'ing /sbin/init

## ulidtko

Hi all. My kernel refuses to exec /sbin/init, which i unpacked from x86 stage3. It says "Exec format error".

I wrote a small test program, here it is:

```
#include <unistd.h>

#include <stdio.h>

int main() {

    char* init = "/sbin/init";

    char* argv[] = {init, NULL};

    execv(init, argv);

    /* execv does not return on success */

    perror("execv failed");

    return 0;

}
```

I compiled it (gcc -static init_exec_test.c -o init_exec_test) and placed it to my initramfs image. That image is nothing special; it has just busybox inside and small /init script which roughly mounts root and gives me a shell session. Then in the session i make a link /sbin/init -> /newroot/sbin/init and try

```
# ./init_exec_test

execv failed: Exec format error
```

The same error has been reported when i tried to use busybox' switch_root to pivot to the real root. After all, it uses exactly the same execv call as i repeated in my init_exec_test.c.

Here is what file utility thinks about format of /sbin/init and folks:

```
$ file /media/gentoo/sbin/init bin/busybox init_exec_test

/media/gentoo/sbin/init: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped

bin/busybox:             ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, for GNU/Linux 2.6.9, stripped

init_exec_test:          ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, for GNU/Linux 2.6.15, stripped

```

Suspiciously enough, the two static binaries execed pretty successfully, so i started to admit that the issue is related to shared libs and dynamical linkage.

Here is another thingy:

```
# ldd /sbin/init

   linux-gate.so.1 =>  (0x00be6000)

   libc.so.6 => /lib/libc.so.6 (0x0049d000)

   /lib/ld-linux.so.2 (0x00a94000)

# file -L /lib/libc.so.6 /lib/ld-linux.so.2 

/lib/libc.so.6:     ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, stripped

/lib/ld-linux.so.2: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), stripped

```

I have googled a bit about that mysterious linux-gate.so.1, but looks like this has nothing to do with the problem.

So, what's wrong with /sbin/init from stage3-x86-2008.0.tar.bz2 or my binaries or kernel or... something else?

The kernel is 2.6.31-gentoo-r6, i can give the config if needed.

----------

## LesCoke

 *LesCoke wrote:*   

> Is your kernel compiled to support ELF executables (CONFIG_BINFMT_ELF=y)?  I would think busybox would fail to load as well if this was not enabled.
> 
> Another thing to check is if the file has executable permissions, but that would generate a different error.
> 
> 

 

Sorry, I totally mis-read where the error was coming from.  Your use of execv looks good.  If you create a hello world executable and try exec-ing it do you get the same results?  I assume you do have the shared libraries listed by ldd in your initrd and the LD_LIBRARY environment variable, err,.. I mean /etc/ld.so.conf setup?

Les

----------

