lake@PC-HM6DD43:~$ sudo apt install qemu-user-binfmt
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
qemu-user
The following NEW packages will be installed:
qemu-user qemu-user-binfmt
lake@PC-HM6DD43:~$ qemu-
qemu-aarch64 qemu-hppa qemu-mips qemu-mipsn32el qemu-ppc64abi32 qemu-sparc
qemu-alpha qemu-i386 qemu-mips64 qemu-nios2 qemu-ppc64le qemu-sparc32plus
qemu-arm qemu-m68k qemu-mips64el qemu-or1k qemu-s390x qemu-sparc64
qemu-armeb qemu-microblaze qemu-mipsel qemu-ppc qemu-sh4 qemu-tilegx
qemu-cris qemu-microblazeel qemu-mipsn32 qemu-ppc64 qemu-sh4eb qemu-x86_64
sudo service binfmt-support start
root@PC-HM6DD43:~# cat /proc/sys/fs/binfmt_misc/qemu-aarch64
enabled
interpreter /usr/bin/qemu-aarch64
flags: OC
offset 0
magic 7f454c460201010000000000000000000200b700
mask ffffffffffffff00fffffffffffffffffeffffff
lake@PC-HM6DD43:~$ file /mnt/c/work/a.out
/mnt/c/work/a.out: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, for GNU/Linux 3.7.0, BuildID[sha1]=be5b1d8e502df629837ec2c74c74bb7b2441862c, with debug_info, not stripped
lake@PC-HM6DD43:~$ qemu-aarch64 /mnt/c/work/a.out
Hello world
Q:WSL/Ubuntu18/qemu-user-binfmt/prepare_binprm: Bad file descriptor
root@PC-HM6DD43:/mnt/c/work# file a.out
a.out: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, for GNU/Linux 3.7.0, BuildID[sha1]=be5b1d8e502df629837ec2c74c74bb7b2441862c, with debug_info, not stripped
root@PC-HM6DD43:~# /mnt/c/work/a.out
prepare_binprm: Bad file descriptor
Refer WSL's builtin binfmt support doesn't seem to like the fancy flags: OC. https://github.com/Microsoft/WSL/issues/2620
## Remove binfmt_misc/qemu-aarch64 and add agin
lake@PC-HM6DD43:/mnt/c/work# sudo /bin/bash
root@PC-HM6DD43:/mnt/c/work# echo -1 > /proc/sys/fs/binfmt_misc/qemu-aarch64
root@PC-HM6DD43:/mnt/c/work# ls -l /proc/sys/fs/binfmt_misc/
total 0
-rw-r--r-- 1 root root 0 Jul 20 16:01 WSLInterop
-rw-r--r-- 1 root root 0 Jul 20 16:53 python3.6
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-alpha
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-arm
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-armeb
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-cris
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-m68k
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-microblaze
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-mips
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-mips64
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-mips64el
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-mipsel
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-ppc
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-ppc64
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-ppc64abi32
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-ppc64le
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-s390x
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-sh4
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-sh4eb
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-sparc
-rw-r--r-- 1 root root 0 Jul 20 16:01 qemu-sparc32plus
-rw-r--r-- 1 root root 0 Jul 20 16:03 qemu-sparc64
--w------- 1 root root 0 Jul 20 15:41 register
-rw-r--r-- 1 root root 0 Jul 20 15:41 status
root@PC-HM6DD43:/mnt/c/work# sudo echo ":qemu-aarch64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-aarch64:" > /proc/sys/fs/binfmt_misc/register
root@PC-HM6DD43:/mnt/c/work# ./a.out
Hello world from aarch64 elf
root@PC-HM6DD43:/mnt/c/work#
root@PC-HM6DD43:/mnt/c/work#
# Try Chroot/qemu-user-static Ubuntu18/aarch64 from WSL/Ubuntu18 x86_64
sudo mount --bind /dev Ubuntu18-aarch64-rootfs/dev
sudo mount --bind /dev/pts Ubuntu18-aarch64-rootfs/dev/pts
sudo mount --bind /proc Ubuntu18-aarch64-rootfs/proc
sudo mount --bind /sys Ubuntu18-aarch64-rootfs/sys
sudo chroot Ubuntu18-aarch64-rootfs/ /bin/bash