1.2 Build, host and target: which files --sysroot moves
GCC 15.2.0. To run the commands: a cross compiler such as arm-linux-gnueabihf-gcc.
You have an ARM cross toolchain. You point --sysroot at your root filesystem,
and the link finds crt1.o there. You point it somewhere else, and crt1.o
moves with it, but the link still finds crtbegin.o in the old place.
A GCC installation holds files for two machines at once: the one that runs the
compiler and the one that runs the code it generates. Almost every configure
flag and command-line option belongs to one of the two. crt1.o and crtbegin.o
are both for the target, but they have different owners. crt1.o belongs to the
C library, which lives in the sysroot, and crtbegin.o belongs to GCC, which
lives under the installation prefix. Once you can say which machine and which
owner a path belongs to, most of GCC's path behaviour stops being arbitrary.
This chapter assumes Chapter 1.1, for the driver and
the programs it runs, such as cc1 and collect2.
--build, --host and --target: native, cross and Canadian cross
GCC's configure takes three machine names, --build, --host and --target,
where almost every other autoconf package takes two. GCC needs --target
because a compiler is the one program whose output is for a machine other than
the one it runs on. Reading any path in a GCC install therefore starts with
asking which machine the file is for. The install manual defines the three
names in one sentence, under Configuration:
The build machine is the system which you are using, the host machine is the system where you want to run the resulting compiler (normally the build machine), and the target machine is the system for which you want the compiler to generate code.
| Flavour | build | host | target |
|---|---|---|---|
| native | x86_64-linux-gnu | same | same |
| cross | x86_64-linux-gnu | same | arm-linux-gnueabihf |
| Canadian cross | x86_64-linux-gnu | x86_64-w64-mingw32 | arm-none-eabi |
A single make therefore produces artifacts for two different machines at once.
Almost everything confusing about GCC's configure flags comes from that fact, so
the question to ask of any flag is whether it is about the machine that runs the
compiler or the machine that runs its output.
You can ask a built compiler which target it is for:
$ arm-linux-gnueabihf-gcc -dumpmachine
It prints the target triple and exits, so it is the quickest check that you are
running the compiler you meant to run. The GCC manual documents -dumpmachine
and the -print-* options under Developer Options.
A target triple names the CPU, the vendor and the operating system, in that
order (config.sub:48-53). The name you configure with may leave a
field out, and config.sub fills it in to make the canonical form, so
x86_64-linux-gnu becomes x86_64-pc-linux-gnu
(Chapter 2.2):
| Triple | CPU | Vendor | Operating system |
|---|---|---|---|
arm-none-eabi | arm | none | eabi |
x86_64-pc-linux-gnu | x86_64 | pc | linux-gnu |
Host code and target code: libexec/ against <target>/lib/
GCC installs two kinds of code: host code, which runs on the machine that runs the compiler, and target code, which is linked into what the compiler produces. The kind decides who builds a file and which directory it lives in, so it is the first thing to settle about any path in an install tree:
| Host code | Target code | |
|---|---|---|
| Examples | gcc, cc1, cc1plus, lto1, collect2, lto-wrapper | libgcc.a, libstdc++.so, crt1.o, crtbegin.o |
| Runs | on your machine, as a process | never by itself, because it is linked into what your compiler produces |
| Built by | the build machine's system compiler | the compiler this build just produced |
| Lives in | bin/, libexec/ | lib/gcc/<t>/<v>/, <t>/lib/, and the sysroot |
libgcc.a shows why target code is easy to misjudge. It is not a library that
GCC uses but one that GCC emits calls into, compiled for a machine that may not
be able to run one instruction of the compiler that built it. When a cross
toolchain "finds the wrong libgcc.a", nothing has gone wrong with GCC's own
installation: the toolchain has picked the wrong artifact for a foreign machine.
One rule, with no exceptions, says which machine each directory serves.
libexec/ and bin/ are host. <target>/lib/ and the sysroot, the target's
stand-in for /, are target. lib/gcc/<target>/<version>/ is a mix: host data
such as the specs file and GCC's own headers, alongside target objects such as
libgcc.a and crtbegin.o.
Most flags affect one side only. -B is the exception, because it is one prefix
applied to three separate search lists at once, and those lists span host and
target. The GCC manual documents all three uses under
Directory Options, and
Chapter 1.5 shows why the result feels blunt.
What a sysroot is: add_sysrooted_prefix
A sysroot is a directory that stands in for / on the target machine. In
the driver it is a string that add_sysrooted_prefix puts in front of certain
absolute paths, so a directory follows --sysroot only if that function
registered it. That function is where to look when a file does not move with the
flag.
Inside a sysroot, usr/include/stdio.h plays the role that
/usr/include/stdio.h plays on the target, and the GCC manual's Directory
Options page gives the same example for --sysroot. A cross compiler needs one
because your own /usr/include/stdio.h describes the C library of your machine,
with the wrong sizeof(long), struct layouts, syscall numbers and #defines for
the target. A cross compile that read it would give no diagnostic, only an
object file that compiles, links and then misbehaves on the device. So the
compiler needs a second / that holds the target's files, and Listing 1.2-1
shows how the driver prepends it to a path:
add_sysrooted_prefix (struct path_prefix *pprefix, const char *prefix,
const char *component,
/* enum prefix_priority */ int priority,
int require_machine_suffix, int os_multilib)
{
if (!IS_ABSOLUTE_PATH (prefix))
fatal_error (input_location, "system path %qs is not absolute", prefix);
if (target_system_root)
{
...
prefix = concat (sysroot_no_trailing_dir_separator, prefix, NULL);
Listing 1.2-1: add_sysrooted_prefix (gcc/gcc.cc:3177-3200).
Abridged; the omitted lines strip a trailing separator and splice in a per-multilib
sysroot suffix.
add_sysrooted_prefix has a sibling, add_prefix, which registers a prefix
the same way but without the sysroot (gcc/gcc.cc:3144). No later
pass rewrites paths, and no flag can apply the sysroot to a prefix that
add_prefix registered. The omitted lines of Listing 1.2-1 also splice in a
per-multilib suffix. A multilib is one of the separate copies of the target
libraries that a toolchain ships, one for each ABI its triple supports
(Chapter 1.6).
The sysroot belongs to libc, the prefix to GCC
The sysroot belongs to the C library, and the prefix, the root that --prefix
names, belongs to GCC. GCC never installs anything into the sysroot, and the C
library never installs anything into the prefix. Almost every question of the
form "why did --sysroot not move this file?" has the same answer: the file is
GCC's, not the C library's.
Sorting the files of a normal link by owner shows the split. The C library's files are in the sysroot, and they are the half that a sysroot moves:
<stdio.h>,<pthread.h>,<fcntl.h>and the rest of the system headerslibc.so,libm.socrt1.o,crti.o,crtn.o
GCC's files are under the prefix, and a sysroot never touches them:
libgcc.a,libgcc_eh.a,libgcc_s.so.1crtbegin.oandcrtend.o, which are part of libgcclibstdc++and its headers, which live in the tooldir, the target's own subtree of the install<stddef.h>,<stdint.h>,<limits.h>and<stdarg.h>, because the C standard requires the compiler to supply them (Chapter 1.8 explains why)
Copying a sysroot from one machine to another therefore does not bring
stdint.h along, because the header is not in the sysroot and is not supposed to
be. The compiler's own headers travel with the compiler.
libsubdir, tooldir and toolexeclibdir: the install directory map
Three autoconf variables, $prefix, $exec_prefix and $libdir, underlie every
path formula in GCC, including the libsubdir, the tooldir and toolexeclibdir.
The configure option that sets a variable therefore moves every directory built
from it, which is how --exec-prefix can put a library's headers and its binary in
different trees.
The install manual documents the option that sets each variable, and its
default, under Configuration:
| Variable | What it is | Default |
|---|---|---|
$prefix | root of the install | --prefix=, else /usr/local |
$exec_prefix | root for machine-dependent files | --exec-prefix=, else $prefix |
$libdir | host libraries | $exec_prefix/lib |
$prefix and $exec_prefix are different variables that default to the same
value (gcc/Makefile.in:662, :673). If you pass
--exec-prefix, paths built from one diverge from paths built from the other.
The libstdc++ install shows the result: it puts its headers under ${prefix}
and its libraries under ${exec_prefix}, so --exec-prefix puts the C++ headers
and the C++ library in different trees.
Chapter 1.12 deals with the consequences.
Two of the directories built from these variables have names of their own in GCC's build files. The libsubdir is GCC's private per-version directory:
libsubdir = $(libdir)/gcc/$(real_target_noncanonical)/$(version)$(accel_dir_suffix)
Listing 1.2-2: libsubdir (gcc/Makefile.in:682).
It expands to something like
/opt/gcc-arm/lib/gcc/arm-linux-gnueabihf/15.2.0/, which holds specs, GCC's
own include/, libgcc.a and crtbegin.o. Because of the $(version)
component, two GCC releases have separate libsubdirs. That lets them coexist, and
it makes mixing GCC 12's headers with GCC 15's cc1 a broken toolchain, not only
an unwise one.
The tooldir is the target's own subtree, and the top-level configure.ac
defines it:
tooldir='${exec_prefix}'/${target_noncanonical}
Listing 1.2-3: tooldir (configure.ac:3121).
It expands to /opt/gcc-arm/arm-linux-gnueabihf/, which holds bin/, lib/,
include/ and sys-include/, a stand-in /usr for the target. A cross
libstdc++ installs there, and so does newlib.
The table adds toolexeclibdir and the sysroot to the map. None of the
directories that GCC installs into is sysrooted, so none of them follows
--sysroot:
| Name | Default | Contains | Sysrooted? |
|---|---|---|---|
| prefix | --prefix | everything GCC installs | no |
| libsubdir | $libdir/gcc/<target>/<ver>/ | GCC's target objects and host data | no |
| tooldir | $exec_prefix/<target>/ | bin/ lib/ include/ sys-include/ | no |
| toolexeclibdir | $tooldir/lib (cross), $libdir (native) | target libraries such as libstdc++.so | no |
| sysroot | --with-sysroot, else $tooldir/sys-root | the C library's files | it is the sysroot |
The install manual documents --with-toolexeclibdir's default as
${gcc_tooldir}/lib (install.texi:2705-2707). The
default for a bare --with-sysroot also sits inside the tooldir, and so inside
$exec_prefix, which is the condition under which a sysroot moves with a
relocated toolchain. The manual states that condition:
If the specified directory is a subdirectory of
${exec_prefix}, then it will be found relative to the GCC binaries if the installation tree is moved.
The driver code that implements that sentence is guarded by a macro that
configure defines only when the condition holds
(gcc/configure.ac:176-182). The driver acts on the macro at
gcc/gcc.cc:5542-5559, and
Chapter 1.5 reads that code.
A cross install under --prefix: host, target and mixed directories
A cross toolchain configured with --prefix=/opt/gcc-arm --target=arm-linux-gnueabihf keeps host and target files in separate
directories, and mixes them only in the libsubdir, lib/gcc/<target>/<ver>/.
The table is the map to check a real compiler's output against:
| Path | Contains | Machine |
|---|---|---|
/opt/gcc-arm/bin/ | arm-linux-gnueabihf-gcc, -g++, -gcc-ar | host |
/opt/gcc-arm/libexec/gcc/<target>/<ver>/ | cc1, cc1plus, lto1, collect2 | host |
/opt/gcc-arm/lib/gcc/<target>/<ver>/ | specs, include/, crtbegin*.o, libgcc.a | mixed |
/opt/gcc-arm/<target>/lib/ | libstdc++.so, newlib's libc.a | target |
/opt/gcc-arm/share/ | docs, man pages | host data |
| the sysroot, wherever it is | libc.so, crt1.o, <stdio.h> | target |
Four commands check the map against an installed cross compiler. The comment on each line says what it prints:
$ arm-linux-gnueabihf-gcc -dumpmachine # the target triple
$ arm-linux-gnueabihf-gcc -print-sysroot # the C library's world; empty = none
$ arm-linux-gnueabihf-gcc -print-search-dirs # install root, programs, target libraries
$ arm-linux-gnueabihf-gcc -v # the configure line it was built with
An empty line from -print-sysroot means the compiler has no sysroot, which is
meaningful rather than broken. Chapter 1.5
explains what it implies.
Why a cross build is circular: libgcc needs the libc headers
A cross build is circular because libgcc and libstdc++ are target libraries
that GCC itself ships, so they belong to GCC and to the target at once. They
need a target compiler and the target's C library, and the C library needs a
compiler in turn. The cycle drives all of Part III, and it closes in four
steps:
- Target libraries need a target compiler, so the build makes one first, an
uninstalled
xgcc. - The libgcc source includes libc headers. The install manual states the need:
"When crossing to GNU/Linux, you need the headers so GCC can build the
exception handling for libgcc." (
install.texi:2765-2766) - libstdc++ needs a libc that it can link against, not only headers, because its configure runs hundreds of link probes.
- The libc itself was built by a compiler.
The conventional way out is to build a deliberately limited GCC first:
binutils → libc headers → minimal GCC (--without-headers)
→ full libc → full GCC (libgcc + libstdc++)
An internal configure variable, inhibit_libc, makes the minimal GCC possible.
With it set, libgcc includes no C library header, although it still calls a few C
library functions, such as malloc and memcpy, through declarations of its own
(gcc/tsystem.h:47-84).
Chapter 3.2 reads the condition that sets it.
--with-headers and --with-libs: copying target files before sysroots
Before sysroots existed, --with-headers and --with-libs pointed GCC at the
target's headers and libraries, and configure copied those files into the
install tree. You will meet both options in old build scripts, and they are the
reason a sysroot toolchain still has header search paths in the tooldir.
Configure does the copy in the top-level configure.ac
(configure.ac:2931). The install manual now marks both
options "Deprecated in favor of --with-sysroot"
(install.texi:2751-2753, :2769-2770).
The paths they left behind are $tooldir/include and $tooldir/sys-include,
which stay on the header search list of a sysroot-only toolchain that never
asked for them. The surviving path is compiled in as TOOL_INCLUDE_DIR
(gcc/Makefile.in:2613), and
Chapter 1.8 returns to it.
Three things to carry away
One install tree holds code for two machines. bin/ and libexec/ run on
yours. <target>/lib/ and the sysroot belong to the target. lib/gcc/<t>/<v>/
holds both, which is why it is the confusing one.
The sysroot belongs to the C library, the prefix belongs to GCC. --sysroot
never moves libgcc, crtbegin.o, libstdc++ or GCC's own headers.
A sysroot is a string, prepended per entry. add_sysrooted_prefix prepends it
and add_prefix does not. Which function registered a directory decides whether
that directory follows --sysroot.
Documentation coverage
The three machine names are defined in the installation manual
(gcc/doc/install.texi:841-844) and again, with the native, cross
and Canadian variants, in the internals manual's
Configure Terms node (gcc/doc/configterms.texi:5).
--with-sysroot is in install.texi (:2709-2731), and
--sysroot is in Directory Options
(gcc/doc/invoke.texi:19484, :19559). The individual install directories have
configure options of their own (install.texi:910-940).
What is not documented:
- That
--sysrootmoves only the C library's half. The manual describes it as "the logical root directory for headers and libraries" (invoke.texi:19560), which reads as if it moved all of them. - The installed tree as a whole: which directories hold host code, which hold target code, and which hold both.
- That
--exec-prefixsplits libstdc++'s headers from its library.
Next: Chapter 1.3, Specs: how the driver writes a command line, the template language behind every command the driver runs.