Section 12.1

Cross-compilation

Definition

Cross-compilation is building a program on one kind of computer so that it runs on a different kind. The computer that runs the compiler, the tool that translates source code into machine instructions, is called the host. The computer that will run the finished program is called the target. A compiler used this way is a cross-compiler. Its output follows the target’s rules, not the host’s: machine instructions, data sizes, function-calling rules and memory addresses. Building for the same kind of computer as the host is called native compilation.

The idea

A microcontroller §6.1, a small computer on one chip, typically has far too little memory to run the compiler that builds its program, so the program is built on a desktop computer. The desktop has a 64-bit x86 processor and an operating system. The microcontroller has a 32-bit Arm processor and no operating system. The two processors do not understand the same instructions, so a program built for the desktop cannot run on the chip.

The core idea is that a compiler is a translator, and a translator does not have to live where its readers live. A translator in London can write a letter in Japanese for a reader in Tokyo, but the letter must also follow the reader’s conventions for dates, addresses and units. For a program, those conventions are the size of each data type, the rules for passing values between functions, the library of standard functions, and the memory addresses that the program may occupy. The picture stops being accurate in one respect: a processor, unlike a human reader, does not cope with a small mistake. A convention taken from the host by mistake can pass the build without an error message and fail on the target.

How it works

Four choices fix what a cross build produces.

A cross build then runs in this order.

  1. The build system, the tool that runs the commands of a build, is told to cross-compile. With CMake §12.12, a tool that generates build instructions, a settings file called a toolchain §12.2 file (a toolchain is a compiler with its companion tools and libraries) names the cross-compiler and sets the variable CMAKE_SYSTEM_NAME to the name of the target’s operating system, or to Generic for a target without one. Setting that variable is what makes CMake treat the build as a cross build.

  2. The cross-compiler translates each source file into an object file, the compiled form of that one file, for the target. The widely used compiler gcc is a separate program for each target, named with the triple as a prefix, such as arm-none-eabi-gcc. The compiler Clang §12.17 is one program for all targets and takes the triple in its -target option.

  3. The linker joins the object files with the target’s libraries and places everything at the addresses that the linker script gives. The finished program is then copied to the target, or run in an emulator, a program that imitates the target’s processor. The host’s own processor cannot run the program directly when its instructions differ from the target’s.

Worked example

One line of C is compiled twice without any change, once for the host and once for a target:

struct record { void *next; uint32_t count; };

The structure holds a pointer, which is a memory address, and a 32-bit number. The host compiler is gcc on 64-bit x86 Linux, whose target triple is x86_64-linux-gnu. The cross-compiler is arm-none-eabi-gcc with the option -mcpu=cortex-m4. The two compilers lay the structure out differently:

host build target build
size of a pointer 8 bytes 4 bytes
position of count in the structure byte 8 byte 4
size of the structure 16 bytes 8 bytes
processor named in the output file x86-64 Arm

The arithmetic for the host: the pointer takes 8 bytes and count takes 4, which is 8+4=128 + 4 = 12 bytes. On this host a pointer must start at an address that is a multiple of 8. The compiler therefore makes the structure a multiple of 8 bytes long, so that every pointer in a row of such structures starts at a multiple of 8. It adds 4 unused padding bytes §4.27: 12+4=1612 + 4 = 16. For the target: 4+4=84 + 4 = 8 bytes, already a multiple of 4, so nothing is added.

The result means that the two builds disagree about data, not only about instructions. If a host program writes the 16 bytes of one structure to a file, the target program reads 8 bytes and looks for count at byte 4, which in the host’s bytes is the middle of the pointer. A test that passes when this code is compiled for the host has tested the 16-byte layout only.

Stated precisely

The output of a cross build depends on the four choices above, on the source files, and on the remaining options and settings of the build tools. The host’s own processor, data sizes and libraries are not among these inputs. They can enter the output only by mistake, for example when the build finds a header file (a file of shared declarations) or a library of the host in place of the target’s.

The contract for linking is: every object file and every library joined into one program must use only instructions that the target processor has, and all of them must follow the same ABI. The tools check only part of this contract. The linker rejects an object file made for a different processor family. The linker for Arm also rejects a mix of the two ways of passing floating-point arguments. Its message says that one file uses VFP register arguments, meaning floating-point registers, and the output does not. The linker does not compare the structure layouts or the argument types that two files assume.

Limits and common mistakes

A build that succeeds proves that translation and linking finished. It does not prove that the intended target was selected. If the setting that selects the target is missing, a build system that falls back to the host’s own compiler builds for the host, and no message says that the target is wrong.

A test that runs on the host tests a different program. The host build has the host’s data sizes, the host’s C library and the host’s instructions. It does not exercise the target’s interrupts §6.11 (hardware signals that pause the running code), hardware devices or timing.

The word host has two meanings. In everyday use, and in CMake, the host is the computer that runs the build. Many open-source packages are prepared for a build by a configure script that the tool Autoconf generates. In such a script, --build names the computer that runs the build, and --host names the computer that will run the program.

Data that host and target exchange needs a format that is defined byte by byte. The raw memory contents of a structure are not such a format, as the worked example shows.

Questions answered

Which compiler target, libc, ABI, and linker script determine the output?

The compiler target, the C library (libc), the application binary interface (ABI) and the linker script that determine the output are the ones that the build actually used, and the build’s own command lines and output files show which those were. A project name or the name of a development kit settles none of the four. The C library holds the precompiled standard functions of the C language, and the ABI is the set of binary rules that compiled pieces of code share.

All four must describe the same target. To establish them for one build, read the complete command lines that the build ran and the map file of the link.

Related entries: Toolchain (§12.2), Linker script (§12.5), Instruction set architecture (ISA) (§4.38), Native application binary interface (ABI) (§12.47), CMake (§12.12), Struct padding and packing (§4.27). Further reading: [82, 108].