'Run a native X86 binary from inside an ARM chroot

I have setup a chroot for an aarch64 rootfs. I am using qemu-aarch64-static as an emulator. This works. I can login to the chroot and execute aarch64 binaries.

Now I would like to run a native (x86_64) cross compiler from within this environment. (I have a large application which does not build using a cross compiler. Using a qemu emulated gcc is too slow). I cannot find a way to run x86 executables from the chroot.

  1. First I mount the native filesystem into the chroot

    mount -o bind / /mnt/rpi_rootfs/mnt/native

  2. prepare chroot

    cd /mnt/rpi_rootfs

    sudo mount -t proc /proc proc/

    sudo mount --rbind /sys sys/

    sudo mount --rbind /dev dev/

  3. login to the chroot

    sudo chroot /mnt/rpi_rootfs/

  4. Create a link to the x86 dynamic linker/loader

    ln -s /mnt/native/lib/ld-linux.so.2 /lib/ld-linux.so.2

  5. Try to run any x86 native binary.

    LD_LIBRARY_PATH=/mnt/native/lib:/mnt/native/usr/lib /mnt/native/bin/pwd

Error:

>/mnt/native/bin/pwd: No such file or directory

I was inspired by this approach: https://gitlab.com/postmarketOS/pmbootstrap/-/issues/1731

Notes: On the native system: ls /proc/sys/fs/binfmt_misc/ shows the various registered emulators, such as qemu-aarch64. In the chroot ls /proc/sys/fs/binfmt_misc/ is empty.



Solution 1:[1]

I use the 'pwd' app as an example.

Execute

file /bin/pwd

/bin/pwd: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2

This shows that actually /lib64/ld-linux-x86-64.so.2 is required to run the application. Thus step 4 above needs to be changed.

Note: /lib64/ld-linux-x86-64.so.2 is a symlink.

Activate the chroot and next, inside the chroot environment create a symlink from the expected location of the dynamic linker to the actual file on the host:

>ln -s /mnt/native/lib/x86_64-linux-gnu/ld-2.31.so /lib64/ld-linux-x86-64.so.2

When this is done it is finally possible to run native x86 applications in the aarch64 chroot. This allows one to run high performance cross compilers from within the chroot.

>LD_LIBRARY_PATH=/mnt/native/lib:/mnt/native/usr/lib:/mnt/native/lib/x86_64-linux-gnu /mnt/native/bin/pwd
>/
                                                                                           

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1