web-dev-qa-db-fra.com

Erreur de format Exec Sous-système Windows exécutable 32 bits pour Linux?

Lorsque j'essaie d'exécuter un fichier 32 bits compilé avec gcc -m32 main.c -o main sur Sous-système Windows pour Linux , j'obtiens l'erreur suivante: bash: ./main: cannot execute binary file: Exec format error.

Si je le compile sans -m32 ça marche.

Toute solution pour exécutable exécutable 32 bits sur WSL?

15
Ford1892

Le support ELF 32 bits n'est pas encore fourni par WSL. Il ne semble pas y avoir de progrès depuis que UserVoice a été lancé - vous n'avez pas de chance.

Voir serVoice: veuillez ajouter la prise en charge ELF 32 bits au noya et Prise en charge des binaires ELF i386 32 bits .

Si possible, passez à un vrai Linux ;-)

8
P.P.

Le support QEMU et binfmt éclaire le chemin :)

https://github.com/Microsoft/wsl/issues/2468#issuecomment-37490452

Après avoir lu que le WSLInterop entre WSL et les processus Windows utilisait binfmt, je bricolais avec QEMU pour essayer un développement ARM, et j'ai d'ailleurs découvert comment faire fonctionner le support 32 bits.

Edit: nécessite "Fall Creators Update", 1709, build 16299 ou plus récent

Installez la configuration de qemu et binfmt:

Sudo apt install qemu-user-static
Sudo update-binfmts --install i386 /usr/bin/qemu-i386-static --magic '\x7fELF\x01\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x03\x00\x01\x00\x00\x00' --mask '\xff\xff\xff\xff\xff\xff\xff\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xf8\xff\xff\xff\xff\xff\xff\xff'

Vous devrez réactiver la prise en charge binfmt à chaque démarrage de WSL:

Sudo service binfmt-support start

Activez les packages d'architecture i386:

Sudo dpkg --add-architecture i386
Sudo apt update
Sudo apt install gcc:i386

Essaye le:

$ file /usr/bin/gcc-5
/usr/bin/gcc-5: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=2637bb7cb85f8f12b40f03cd015d404930c3c790, stripped

$ /usr/bin/gcc-5 --version
gcc-5 (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc helloworld.c -o helloworld

$ ./helloworld
Hello, world!

$ file helloworld
helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=3a0c7be5c6a8d45613e4ef2b7b3474df6224a5da, not stripped

Et pour prouver que cela fonctionnait vraiment, désactivez la prise en charge d'i386 et réessayez:

$ Sudo service binfmt-support stop
 * Disabling additional executable binary formats binfmt-support [ OK ]

$ ./helloworld
-bash: ./helloworld: cannot execute binary file: Exec format error
21
Froosh