Skip to content

Instantly share code, notes, and snippets.

@sengkyaut
Created October 12, 2019 11:55
Show Gist options
  • Save sengkyaut/c72f893b5ed0a37e64f76cd4342ef9b1 to your computer and use it in GitHub Desktop.
Save sengkyaut/c72f893b5ed0a37e64f76cd4342ef9b1 to your computer and use it in GitHub Desktop.
32-bit support in WSL
https://github.com/microsoft/wsl/issues/2468#issuecomment-374904520
sudo service binfmt-support start
https://github.com/WhitewaterFoundry/Pengwin/issues/273#issuecomment-451274200
Based on some tinkering I was doing with qemu for some ARM dev, I think I may have found a technique to allow general 32-bit support in WSL. Hat-tip to @therealkenc for the concept 😁
Edit: requires "Fall Creators Update", 1709, build 16299 or newer (I think)
Presuming a fresh Ubuntu WSL instance, you'll need to install the qemu-user-static package, add the i386 binfmt, enable the i386 architecture, update your package lists, and install some i386 packages:
Install qemu and binfmt
sudo apt update
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'
[Edit: whoops, need to update package lists, added sudo apt update]
This will activate i386 support by causing them to be executed through qemu-i386-static, and drop a config file into /var/lib/binfmts/ for future reactivation.
You will need to reactivate this every time you restart WSL and want i386 support:
sudo service binfmt-support start
Enable i386 architecture and packages
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
Proof
And to prove it really was working, disable i386 support and try again:
$ sudo service binfmt-support stop
* Disabling additional executable binary formats binfmt-support [ OK ]
$ ./helloworld
-bash: ./helloworld: cannot execute binary file: Exec format error
-------------------------
Just a small update. I was able to get the above workaround to work in wlinux by doing the following.
git clone git://git.qemu-project.org/qemu.git
mkdir qemu/build && cd qemu/build
sudo apt install libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev libaio-dev libbluetooth-dev libbrlapi-dev libbz2-dev libcap-dev libcap-ng-dev libcurl4-gnutls-dev libgtk-3-dev libibverbs-dev libjpeg-dev libncurses5-dev libnuma-dev librbd-dev librdmacm-dev libsasl2-dev libsdl2-dev libseccomp-dev libsnappy-dev libssh2-1-dev libvde-dev libvdeplug-dev libvte-dev libxen-dev liblzo2-dev valgrind xfslibs-dev
../configure --target-list=i386-linux-user,i386-softmmu
make -j$(nproc)
sudo make -j$(nproc) install
If it was a success you can now
cd ../.. && rm -rf qemu
then run
sudo apt install binfmt-support
sudo update-binfmts --install i386 /usr/local/bin/qemu-i386 --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'
sudo service binfmt-support start
sudo dpkg --add-architecture i386
sudo apt update
sudo apt install gcc-multilib g++-multilib
You should now be able to execute both 32-bit and 64 bit code. The best part is it appears to stay running. I only had to run the binfmt-support start command once.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment