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.
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.Preprocessor. Carries out the lines that start with
#includeand#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 defaultgccperforms this stage inside the next program.Compiler proper (
cc1). Translates the preprocessed source into assembly language, a text form of machine instructions.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.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.Binary utilities.
objcopychanges the file format of the result,arpacks object files into a library, whose parts are then called members, andstripremoves names.
The linker adds three kinds of code that the developer did not write.
The C library: standard functions such as
memcpyandprintf.The compiler’s runtime library: small routines that the compiler calls on its own initiative for operations too complicated to expand in place, such as floating-point arithmetic on a processor without floating-point hardware. For
gccthis library islibgcc.Startup files: object files with the code that runs before the function
main, for example to clear memory.
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 of those bytes, 24 from main.c and 16 from scale.c. The other 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:
the build date and time, which the predefined names
__DATE__and__TIME__insert into the program.gccuses the value ofSOURCE_DATE_EPOCHin their place when that environment variable, a named setting passed to a program by whatever starts it, is set;the build directory, which compilers record in debug information §12.9, the data that lets a debugger, a tool for inspecting a running program, relate the program to its source. The
gccoption-ffile-prefix-mapreplaces it with a fixed name of the builder’s choice;the timestamps and owner numbers that
arstores in a library. ItsDmodifier stores zero for them;the order of the input files, if a build step takes that order from the file system.
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:
the compiler, including the driver program that runs its parts, as the exact installed files and not only as a version number;
the assembler and the linker, the tools that encode the machine instructions and join the compiled pieces into one program, and the utilities that convert or pack the output;
the C library, the compiler’s runtime library (routines that the compiler calls on its own) and the startup files (code that runs before the program’s main function), in the precompiled variant that the processor options select, together with their header files;
the linker script, the file that fixes the memory layout;
the build system, and every program that generates source code or configuration during the build;
every option passed to these tools, and every configuration file that produces options;
the values that the tools take from their surroundings: the search paths, the build directory and the time.
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].