简体   繁体   English

Exec 格式错误适用于 Linux 的 32 位可执行 Windows 子系统?

[英]Exec format error 32-bit executable Windows Subsystem for Linux?

When I try to execute a 32-bit file compiled with gcc -m32 main.c -o main on Windows Subsystem for Linux , I get the following error: bash: ./main: cannot execute binary file: Exec format error .当我尝试在Windows Subsystem for Linux上执行使用gcc -m32 main.c -o main编译的 32 位文件时,出现以下错误: bash: ./main: cannot execute binary file: Exec format error

If I compile it without -m32 it runs.如果我在没有-m32情况下编译它它会运行。

Any solution for running 32-bit executable on WSL ?在 WSL 上运行 32 位可执行文件的任何解决方案?

QEMU and binfmt support light the way :) QEMU 和 binfmt 支持点亮方式:)

https://github.com/microsoft/wsl/issues/2468#issuecomment-374904520 https://github.com/microsoft/wsl/issues/2468#issuecomment-374904520

After reading that the WSLInterop between WSL and Windows processes used binfmt, I was tinkering with QEMU to try some ARM development, and incidentally discovered how to get 32-bit support working.看到WSL和Windows进程之间的WSLInterop使用了binfmt之后,我正在摆弄QEMU尝试一些ARM的开发,偶然发现了如何让32位支持工作。

Edit: requires "Fall Creators Update", 1709, build 16299 or newer编辑:需要“秋季创作者更新”,1709,构建 16299 或更新

Install qemu and binfmt config:安装 qemu 和 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'

You'll need to reactivate binfmt support every time you start WSL:每次启动 WSL 时都需要重新激活 binfmt 支持:

sudo service binfmt-support start

Enable i386 architecture packages:启用 i386 架构包:

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

Try it out:试试看:

$ 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

And to prove it really was working, disable i386 support and try again:为了证明它确实有效,请禁用 i386 支持并重试:

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

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

32-bit ELF support isn't provided by WSL (yet). WSL(目前)不提供 32 位 ELF 支持。 There doesn't seem to be any progress since the UserVoice was raised - you are out luck.自从 UserVoice 被提出以来,似乎没有任何进展——你走运了。

See UserVoice: Please add 32 bit ELF support to the kernel and Support for 32-bit i386 ELF binaries .请参阅UserVoice: Please add 32 bit ELF support to the kernelSupport for 32-bit i386 ELF binaries

If possible, switch to a real Linux ;-)如果可能,请切换到真正的Linux ;-)


Since this was originally posted, the support has been available on WSL2 which does support real Linux kernel !由于这是最初发布的,WSL2 上已经提供了支持,它确实支持真正的 Linux 内核 So that should be the preferred way.所以这应该是首选方式。

As noted in the linked github issue , there's also qemu-user which can be used if WSL1 is still used.正如链接的github 问题中所述,如果仍然使用 WSL1,也可以使用qemu-user

WSL2 runs in a real virtual machine using a real Linux kernel , therefore it's actually possible to do anything a Linux VM can do, including running 32-bit code. WSL2 在使用真正Linux 内核真实虚拟机中运行,因此实际上可以执行 Linux VM 可以执行的任何操作,包括运行 32 位代码。 Just install 32-bit libs by running只需通过运行安装 32 位库

sudo dpkg --add-architecture i386
sudo apt-get update

For more information read欲了解更多信息,请阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 64 位 Linux 系统上为 ARM 32 位可执行文件编译 C 代码 - How to compile C-code on a 64-bit Linux system for an ARM 32-bit executable 如何使用 GCC 和 Linux 子系统为 Windows 编译可执行文件? - How to compile executable for Windows with GCC with Linux Subsystem? 64 位机器上 64 位可执行文件的 Exec 格式错误 - Exec Format error of a 64-bit executable on a 64-bit machine 无法将UNIX / Linux程序编译为32位程序 - Can't compile UNIX/Linux program as 32-bit program 有没有办法在64位Windows机器上得到Mingw-w64的gcc到output一个32位的可执行文件而不用重新安装? - Is there any way to get Mingw-w64's gcc to output a 32-bit executable file on a 64-bit Windows machine without reinstalling? linux x86_64 vm gcc可执行输出的体系结构中的错误,exec文件格式错误 - linux x86_64 vm gcc error in architecture of executable output, exec file format error gcc如何确定默认情况下是否生成32位或64位可执行文件? - How does gcc determine if to generate a 32-bit or 64-bit executable file by default? 在32位Linux和64位Linux和MPFR上的long long int - long long int on 32-bit Linux vs 64-bit Linux and MPFR 32位构建中的“bad codegen,boost diff in boost”错误 - “bad codegen, pointer diff in boost” error in 32-bit build 使用 GCC 编译 32 位程序时出现奇怪的错误 - Weird error when compiling 32-bit programs with GCC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM