Running other architectures with an external emulator (experimental)

A Mach-O program contains code for one or more CPU architectures. Darling runs a program's code in one of three ways:

  1. On the host CPU. An x86_64 host runs x86_64 and i386 code, an aarch64 host runs arm64 code.
  2. With an in-process emulator backend. Builds that include one run other architectures inside the mldr process (for example the FEX backend, which runs x86_64 and i386 programs on aarch64 hosts when Darling is built with it).
  3. With an external emulator. mldr replaces itself with an emulator process of your choice, which runs Darling's loader built for the program's architecture. This page describes this route.

The external route is generic: it is configured per guest architecture and works the same way for, say, i386 programs on an aarch64 host, arm64 programs on an x86_64 host or PowerPC programs, provided that an emulator and a Darling loader exist for that architecture. Darling does not ship an emulator.

When it is used

For each program, mldr looks at the architecture of the slice it would run:

  • If an in-process backend of this build runs that architecture, it is always used. Which architectures those are is decided when Darling is built (the CMake option DARLING_EMU_GUESTS; on aarch64 hosts it defaults to x86_64 i386). To use an external emulator for one of them, build without it in that list.
  • If the host runs the architecture itself, the host's own route is used, unless the configuration says to replace it (see DARLING_EMULATOR_<ARCH>_REPLACE_NATIVE below).
  • Otherwise, if an external emulator is configured for the architecture, it is used.
  • Otherwise the program fails as before, for example with No supported architecture found in fat binary. or Unsupported 64-bit Mach-O cputype.

For a universal (fat) file, mldr first chooses a slice as usual: a slice forced with arch or binary preferences, else the host's own slices. If that slice is of an architecture with an external emulator, the program goes to it. If the file has no slice that runs here, mldr takes the first slice of an architecture with a configured emulator, in this order: x86_64, arm64, ppc64, i386, arm, ppc. Thin (single-architecture) files work the same way.

Nothing changes for an architecture that has no emulator configured.

Configuration

The configuration comes from environment variables, per guest architecture. <ARCH> is one of X86_64, I386, ARM64, ARM, PPC and PPC64.

VariableMeaning
DARLING_EMULATOR_<ARCH>The emulator's absolute path, followed by its own arguments, separated by spaces or tabs. There is no quoting, so no argument can contain a space. The path is not looked up in PATH.
DARLING_EMULATOR_<ARCH>_LOADERThe absolute path of the Darling loader to run under the emulator (see below). Optional for I386 and for the architecture mldr itself is built for.
DARLING_EMULATOR_<ARCH>_REPLACE_NATIVESet to 1 to use the emulator even for an architecture the host runs itself. One use is a kernel without 32-bit compatibility support (for example Linux booted with ia32_emulation=off), where i386 programs cannot run directly.

mldr reads these variables from the environment of the program it starts, and child processes inherit them. Set them in the environment darling is started with:

DARLING_EMULATOR_I386="/usr/bin/qemu-i386" darling shell

Programs that launchd starts (for example applications opened with open) get the environment the container was started with. Run darling shutdown before you change the configuration.

An unusable configuration (a relative path, or a missing loader for an architecture that has no default) stops the program with a message naming the variable, but only when the emulator would actually be used.

The loader

The emulator does not run the Mach-O program directly. It runs Darling's loader for the guest architecture: a Linux ELF program built for that architecture from the same Darling source as the host's mldr. That loader then loads the program inside the emulated process.

  • i386: mldr32. By default, mldr32 next to mldr is used (the file name of mldr with 32 added). x86_64 builds install it. On other hosts it has to be built for i386 separately.
  • The architecture mldr is built for: mldr itself. This is only useful together with _REPLACE_NATIVE.
  • Any other architecture: there is no default. Set DARLING_EMULATOR_<ARCH>_LOADER to an mldr built for that architecture.

The loader must come from the same Darling source as mldr, because the variables described in the next section are part of the interface between the two.

What mldr runs

mldr replaces itself (with execv) with:

<emulator> <emulator arguments...> <loader> <the program's argv[0]> <the program's other arguments...>
  • Process. The process stays the same: same process ID, same parent, same open file descriptors (including the darlingserver connection details mldr received). darlingserver keeps seeing the same process. The program checks in with darlingserver itself, through the guest architecture's libsystem_kernel, as any other program does.

  • Environment. The environment is the one mldr received, plus:

    • __mldr_emulated_arch: the name of the guest architecture (as in <ARCH> above). The loader runs only that architecture's slice, and never hands the program to another external emulator.
    • __mldr_exec_path: the host path of the Mach-O file, when that path is not the program's argv[0]. An emulator sets the loader's argv[0] itself, so the path cannot travel there.

    The loader takes both variables out of the environment before the program runs, so the program and its children never see them.

  • New programs. When the program starts another program, the new program goes through the host's mldr again and is routed by the same rules.

What the emulator must provide

  • It runs Linux programs of the guest architecture as <emulator> [arguments] <program> <program arguments>, with the program's arguments unchanged.
  • It passes the environment through unchanged, keeps all inherited file descriptors open and runs in the same process (no fork).
  • It supports what Darling's loader and libsystem_kernel need from Linux: threads, signals (darlingserver interrupts threads with real-time signals), Unix-domain sockets with descriptor passing, mmap at fixed addresses, and execve of host programs (so that new programs go through the host's mldr).
  • The guest's address space must have room for what the loader maps. For example, i386 programs need the commpage at 0xffff0000 and often a first segment at 0x1000, which needs vm.mmap_min_addr at 4096 or lower (see Common issues).
  • Any host libraries the program's frameworks load (X11, Mesa and so on) must be available to the emulator for the guest architecture. For example, an emulator that runs i386 Linux programs needs i386 builds of those libraries in the file system it uses.

What does not work out of process

  • Debuggers and ptrace. A host debugger attached to the process sees the emulator's own state, not the program's registers. Darling's lldb has not been tried this way.
  • Host page size. The launcher's muvm relaunch for hosts with large pages applies to the in-process backend only. If the emulator needs a different page size than the host's kernel has, run Darling in a virtual machine that has it.
  • Speed. Everything, including Darling's own libraries, runs emulated.

Examples

i386 programs on an x86_64 kernel without 32-bit support

With qemu's user-mode emulator (Debian package qemu-user) and the i386 host libraries mldr32 needs:

export DARLING_EMULATOR_I386="/usr/bin/qemu-i386"
export DARLING_EMULATOR_I386_REPLACE_NATIVE=1
darling shutdown
darling shell

i386 programs on an aarch64 host

When i386 is not one of the build's in-process guests (for example a build with -DDARLING_EMU_GUESTS=x86_64), an emulator that runs i386 Linux programs can be used, for example FEX's FEXInterpreter, together with an mldr32 built for i386 from the same source:

export DARLING_EMULATOR_I386="/usr/bin/FEXInterpreter"
export DARLING_EMULATOR_I386_LOADER=/usr/local/libexec/darling/bin/mldr32

Requirements found when this was tried with FEX:

  • 4K pages. FEX refuses kernels with 16K pages, such as those on Apple Silicon hosts. Run it inside a virtual machine with 4K pages, for example with muvm.
  • vm.mmap_min_addr at 4096 inside that virtual machine, for programs whose first segment is at 0x1000.
  • Top of the 32-bit address space. mldr32 maps the commpage at 0xffff0000. The FEX build used reserved that range (Cannot mmap commpage: File exists). Getting past it took a change to FEX that is not in upstream FEX.
  • i386 libraries in FEX's root file system: glibc, libstdc++, X11, Mesa (for OpenGL programs) and whatever else the program's frameworks load. A missing one shows up as, for example, Cannot load libX11.so.6 (ELF): wrong ELF class: ELFCLASS64.
  • Shared memory with X. If the program runs in a virtual machine and the X server on the host, MIT-SHM does not work across the boundary and windows stay black. Use an X server with MIT-SHM disabled, for example Xvfb -extension MIT-SHM.

arm64 programs on an x86_64 host

An arm64 Darling loader is an mldr built for aarch64 Linux. With qemu:

export DARLING_EMULATOR_ARM64="/usr/bin/qemu-aarch64 -L /usr/aarch64-linux-gnu"
export DARLING_EMULATOR_ARM64_LOADER=/opt/darling-arm64/libexec/darling/bin/mldr

The arm64 loader expects Darling's arm64 libraries in the prefix, so this needs a prefix whose libraries include arm64 slices.

PowerPC programs

Darling can build a PowerPC slice of its libraries, but there is no PowerPC loader yet, so DARLING_EMULATOR_PPC has nothing to run.