Section 12.2

Toolchain

Definition

A toolchain is the set of programs and precompiled libraries that together turn source code into a program that one kind of machine can run. Its core members are a compiler, which translates source code into the machine’s instructions; an assembler, which encodes those instructions as bytes; a linker §12.4, which joins the compiled pieces into one program; and the libraries, collections of ready-made code, that are linked into every program. All members are built for one target machine. The output of each tool is the input of the next.

The idea

Two developers build the same source code with the same compiler version, and their programs differ. The cause is a part of the toolchain that neither of them named. The command that a developer types, for example gcc main.c, looks like one tool. It is a small driver program that runs several other programs in turn, and the last of them adds library code that appears nowhere in the developer’s source. The two machines hold different versions of that library code.

The picture is a production line with its own stock of prefabricated parts: two lines with different stock turn the same order into different products. The picture understates one thing. For a small program the prefabricated parts can be nearly the whole product, as the worked example shows.

How it works

A toolchain for the C language works in stages. The names in parentheses are the programs of the GNU toolchain. GNU, a name whose letters stand for GNU’s Not Unix, is the free-software project that publishes them.

  1. Driver (gcc). The command that is run by the developer or by the build system, the tool that issues the commands of a build. It reads the options and runs the other programs with the right arguments.

  2. Preprocessor. Carries out the lines that start with #include and #define. It copies in header files, the files of shared declarations that those lines name. Some of them belong to the C library and to the compiler and are not part of the project. By default gcc performs this stage inside the next program.

  3. Compiler proper (cc1). Translates the preprocessed source into assembly language, a text form of machine instructions.

  4. Assembler (as). Encodes the assembly text as bytes and writes an object file: machine code, plus a list of the names it defines and the names it still needs.

  5. Linker (ld). Joins the object files with startup code, which runs before the program’s own first function, and with object files taken from libraries, assigns memory addresses according to a linker script §12.5, a file that describes the memory layout, and writes the program.

  6. Binary utilities. objcopy changes the file format of the result, ar packs object files into a library, whose parts are then called members, and strip removes names.

The linker adds three kinds of code that the developer did not write.

The driver chooses these files itself. Options that select the processor or the calling rules make it choose a different precompiled copy of each.

Worked example

A program has two source files. scale.c holds one function, float scale(float x) { return x * 2.5f; }, which multiplies a floating-point number §4.49 (a number with a fractional part) by 2.5. main.c calls it once and stores the result in a global variable. They are built for a microcontroller §6.1, a small computer on one chip, that has an Arm processor. The compiler is version 13.2 of arm-none-eabi-gcc, and the options are -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -O1 --specs=nosys.specs. The first two options select the processor and its instruction set. The option -mfloat-abi=soft makes the compiler call library routines for floating-point arithmetic instead of using floating-point hardware. -O1 asks for a basic level of optimization. The last option supplies placeholder versions of the operating-system functions that the C library expects.

Adding the option -v makes the driver print what it runs. For each source file it runs cc1 and then as. At the end it runs collect2, a wrapper that runs the linker ld.

The compiled code of scale.c is 16 bytes long, and it does not multiply. It calls a routine named __aeabi_fmul, which the source never mentions. The linker’s map file, a report of what was linked, shows where that routine comes from: member _arm_mulsf3.o of the library libgcc.a. In total the map file lists 31 library members that were added to the program: 24 from the C library libc.a, which in this toolchain is the library newlib §12.19, 6 from the library of placeholder functions, and 1 from libgcc.a.

The code section of the finished program, the part of the file that holds its machine instructions, is 5,104 bytes long. The two source files contributed 24+16=4024 + 16 = 40 of those bytes, 24 from main.c and 16 from scale.c. The other 5104−40=50645104 - 40 = 5064 bytes, which is 99.2% of the code, were added by the toolchain: startup code, library code and the padding between the pieces.

Changing one option changes the libraries. With -mfloat-abi=hard -mfpu=fpv4-sp-d16 in place of -mfloat-abi=soft, the compiled code of scale.c is 10 bytes long and multiplies with one hardware instruction. No routine from libgcc.a is needed, the driver takes every library from a different directory, and the code section is 4,748 bytes long. The result means that the name and the version of the compiler do not identify the program that comes out. The libraries, the startup files and the options decide the rest.

Stated precisely

The exact statement is a dependency: every input that a tool reads during the build can change the bytes of the build’s output. The inputs are the source files, the programs of the toolchain, the libraries and header files, the linker script, the options, and values taken from the surroundings, such as the current time, the build directory and the search paths, the lists of directories in which programs and libraries are looked up.

A reproducible build §13.8, as the Reproducible Builds project defines it, is one where the same source code, build environment and build instructions let anyone recreate bit-for-bit identical copies of all the output files that the project specifies. It needs two things. Every input above is pinned, meaning recorded exactly, by version or by file, so that it cannot change unnoticed. And the tools add nothing that varies from one run to the next.

Four things commonly vary between runs of one pinned toolchain:

Limits and common mistakes

A version number does not identify a toolchain. The default settings of a compiler depend on how that copy of the compiler was configured when it was built, and the C library that accompanies it is a separate project with its own version. Pin the exact files, for example by a cryptographic hash §5.1, a short fingerprint computed from the bytes, of the toolchain archive.

A recorded version is not proof of use. The search path for programs decides which program named gcc or ld actually runs. The output of the driver’s -v option and the linker’s map file show what ran and what was linked.

A fully pinned toolchain is not a correct one. Compilers and libraries contain bugs, and pinning makes those bugs repeatable, not absent. Pinning also does not make source code with undefined behavior §4.12 safe. Such code breaks a language rule for which the language defines no outcome, and another compiler may translate it differently.

Questions answered

Which tools and libraries must be pinned to reproduce a build?

To reproduce a build, pin every program that runs during the build and every file that those programs read, in addition to the source code itself. To pin something is to record exactly which version or which file is used, so that it cannot change unnoticed. The list is:

Pinning makes the inputs equal. For output that is equal bit for bit, the build must also remove what varies between runs: embedded build times, embedded directory names, timestamps inside libraries and the order of input files. State which output is to be reproduced, for example the final program image, and compare its bytes.

Related entries: Cross-compilation (§12.1), Linker (§12.4), Incremental validation (§10.42), Reproducible build (§13.8), Newlib and Picolibc (§12.19), Compiler flag (§12.3). Further reading: [120].