Embedded Handbook/General/Compiling with qemu user chroot

Cross compiling software with a QEMU user chroot. Cross compiling software with a QEMU user chroot.

Usage
In order to take advantage of QEMU user mode a few steps are necesary. Firs the package must be emerged with the right settings. Specifically this means building with  and setting QEMU_USER_TARGETS to include the targets that will be utilized.

See for other ways of doing this:

Tweak the list here to include the necessary target(s). See the output of for the full list:

To build all targets:

Then install the package:

At this point it is wise to create a binary package for QEMU:

The kernel needs support for the binfmt_misc module. Add the  or   symbols to the host's kernel  file. If this module is not built already, then the development host will require a reboot after the kernel update and modules_install.

Mount the binfmt_misc handler if it is not already mounted, then register the format with the kernel via the procfs:

Do not register a handler that matches the host machine (in this example, registers for x86 and AMD64 has been excluded since the host machine).

32-bit registers:

Add 64-bit registers:

After this, make sure the binfmt service is (re-)started:

OpenRC
It may be wise for the services to be started by default on boot:

systemd
For the service, add files containing the desired handler registration strings under. For example:

With this done, restart the service:

It's started automatically per default, but you might want to confirm it's running properly:

Setup chroot
Download the desired stage tarball:

Unpack the tarball:

Install the static qemu into the chroot:

Mount the required directories:

Chroot into the environment:

Keep QEMU from being altered within the chroot (if deleted or reinstalled within the chroot, breakage will occur):

Unmount stuff when not in use:

Sometimes we'll need to pass additional args to QEMU (CPU model), so we'll create a wrapper script (in C) that'll call QEMU with it:

Compile the wrapper with:

Then copy into the chroot. Notice the first example ARM entry in the binfmt_misc section uses this method.

Caveat
Currently, qemu doesn't support pid-sandbox and network-sandbox.

Execute

Or, insert the following line in /etc/portage/make.conf on chroot.