1.5 How the driver builds its search lists: -B and --sysroot
GCC 15.2.0. To run the commands: an installed compiler, and a cross compiler such as arm-linux-gnueabihf-gcc.
Your cross compiler cannot find crt1.o. You add --sysroot=/arm-rootfs, and
now it can. crtbegin.o is missing too, so you try the same flag on it, and
it is still missing: --sysroot has no effect on it.
Both files reach the link line through the same directive, %s, and the driver
looks both up in the same list, startfile_prefixes. One follows your sysroot
and the other ignores it, because two functions register the entries of that
list, add_prefix and add_sysrooted_prefix, and only the second prepends the
sysroot. The order of the registrations is the search order, and -B,
--sysroot and the configure options each touch a different part of it.
This chapter assumes Chapter 1.1 for -### and the
programs the driver runs, and Chapter 1.2 for
add_sysrooted_prefix, the libsubdir and the tooldir. It also assumes
Chapter 1.3 for the %s, %D and %R directives, and
for a spec name as a slot whose value can change.
exec_prefixes for programs, startfile_prefixes for files
The driver keeps one search list for the host programs it runs, exec_prefixes,
and another for target files such as crt1.o and libraries,
startfile_prefixes. A flag can feed one list, the other or both, so the first
question to ask of a flag that moves a search path is which list it feeds. A
third list, for headers, is covered in
Chapter 1.8. -print-search-dirs prints the
first two:
$ gcc -print-search-dirs
It prints each list on a line of its own, under the name in the last column:
| List | Finds | Printed as |
|---|---|---|
exec_prefixes | host programs: cc1, as, ld, collect2 | programs: |
startfile_prefixes | target files: crt*.o and every -l | libraries: |
The two lists live in separate variables. -L feeds only startfile_prefixes,
and -B feeds all three lists, which makes it the blunt instrument. The GCC
manual documents both flags under Directory Options, and
-print-search-dirs with the other -print-* options under
Developer Options. Despite its name, exec_prefixes is a driver
list, not the configure variable $exec_prefix, which
Chapter 1.2 describes as the root for
machine-dependent files.
Every -L option that the driver generates for the linker comes out of
startfile_prefixes, through one directive, %D:
%D Dump out a -L option for each directory in startfile_prefixes.
If multilib_dir is set, extra entries are generated with it affixed.
Listing 1.5-1: %D (gcc/gcc.cc:592-593; handler at
:6250).
For -l, the driver's library search path is therefore not a path it searches
but a list it converts into -L options. The driver never opens a library file:
it emits -l and -L and lets ld resolve them. It does walk the list itself
for a startfile named with %s, which is how it finds crt1.o and
crtbegin.o. -print-file-name walks it too, so that it can report what ld
would conclude.
The order startfile_prefixes is built in
The driver builds startfile_prefixes at startup, and the order of the calls
that register its entries is the search order. The entries come from six places,
from the command line to the target's configuration, and the place an entry
comes from decides both its rank and whether --sysroot moves it:
-Bprefixes, from the command line (gcc.cc:4590).GCC_EXEC_PREFIX, from the environment (gcc.cc:4784).LIBRARY_PATH, from the environment, for native compilers only (gcc.cc:4923).- The relocated install prefix, computed from
argv[0](gcc.cc:4818-4835). - The tool directory,
$prefix/<target>/lib/(gcc.cc:5537-5539). - Either
STARTFILE_PREFIX_SPEC(gcc.cc:8579-8587) or theSTANDARD_STARTFILE_PREFIX*chain (gcc.cc:8590-8630), never both.
The internals manual documents this order, not the user manual. Its entry at
gcc/doc/tm.texi:570 begins "Here is the order of prefixes tried for
startfiles" and lists the prefixes with their conditions. Of the manual
references in Part I, info gccint 'Target Macros' Driver is the one to keep at
hand.
-B wins, and nothing can sort before it
-B adds its argument to all three search lists at the driver's highest
priority, so a -B directory is searched before anything from the environment
or the configuration. That reach is why -B fixes problems you did not know you
had and breaks things you did not expect. In the option's handler, watch the
three add_prefix calls at the end and the comment on a missing directory
separator:
case OPT_B:
{
size_t len = strlen (arg);
/* Catch the case where the user has forgotten to append a
directory separator to the path. Note, they may be using
-B to add an executable name prefix, eg "i386-elf-", in
order to distinguish between multiple installations of
GCC in the same directory. Hence we must check to see
if appending a directory separator actually makes a
valid directory name. */
if (!IS_DIR_SEPARATOR (arg[len - 1])
&& is_directory (arg))
{
...
}
add_prefix (&exec_prefixes, arg, NULL,
PREFIX_PRIORITY_B_OPT, 0, 0);
add_prefix (&startfile_prefixes, arg, NULL,
PREFIX_PRIORITY_B_OPT, 0, 0);
add_prefix (&include_prefixes, arg, NULL,
PREFIX_PRIORITY_B_OPT, 0, 0);
}
Listing 1.5-2: -B (gcc/gcc.cc:4590-4619).
The three add_prefix calls are the whole of -B's definition. It is not a
library path option or an executable path option, but one prefix applied to
programs, link inputs and headers at once.
The driver appends the trailing / only when the result names a directory, and
the comment gives the reason. A -B argument without a trailing separator can be
a legitimate executable name prefix, such as i386-elf-. The driver must
therefore check that appending a separator names a real directory before it
appends one. If you write -B/opt/alt and /opt/alt exists, you get
/opt/alt/. If it does not exist, the driver takes your programs to be called
/opt/altcc1 and so on.
PREFIX_PRIORITY_B_OPT is the highest priority there is, and the enum that
defines it has exactly two members:
/* Ranking of prefixes in the sort list. -B prefixes are put before
all others. */
enum path_prefix_priority
{
PREFIX_PRIORITY_B_OPT,
PREFIX_PRIORITY_LAST
};
Listing 1.5-3: the priority enum (gcc/gcc.cc:3120-3127).
Every environment contribution and every compiled-in default lands at
PREFIX_PRIORITY_LAST, where entries are ordered only by when they were added.
-B therefore beats the environment, the environment beats the compiled-in
defaults, and there is no way to express "before -B".
-B does not move C++ header search. It adds GCC's own header directories,
which is enough to change which <stdint.h> a compile uses but not which
<vector>. Chapter 1.8 and
Chapter 1.12 explain that asymmetry. The
driver's -B is also unrelated to the linker's -Bstatic.
make_relative_prefix: relocating a moved toolchain from argv[0]
The driver recomputes its install prefix from argv[0] on every run, using
libiberty's make_relative_prefix. An installed GCC tree moved to another
directory therefore keeps working, and RPATH plays no part in it. The relocated
install prefix is also the entry that finds crtbegin.o, the file that ignored
--sysroot:
gcc_libexec_prefix = standard_libexec_prefix;
#ifndef VMS
/* FIXME: make_relative_prefix doesn't yet work for VMS. */
if (!gcc_exec_prefix)
{
gcc_exec_prefix = get_relative_prefix (decoded_options[0].arg,
standard_bindir_prefix,
standard_exec_prefix);
gcc_libexec_prefix = get_relative_prefix (decoded_options[0].arg,
standard_bindir_prefix,
standard_libexec_prefix);
if (gcc_exec_prefix)
xputenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL));
}
Listing 1.5-4: relocation (gcc/gcc.cc:4822-4835). get_relative_prefix
is make_relative_prefix from libiberty, assigned at
:4816.
The three arguments give where the driver was configured to be installed,
standard_bindir_prefix and standard_exec_prefix, and where it was started
from, decoded_options[0].arg, which is argv[0]. The function applies the
same relative relationship to the new location. If you configured with
--prefix=/opt/gcc-arm and the binary is now at /home/me/toolchain/bin/gcc,
gcc_exec_prefix is computed as /home/me/toolchain/lib/gcc/. The GCC
manual states the rule under GCC_EXEC_PREFIX in
Environment Variables. When that variable is not set, GCC
"attempts to figure out an appropriate prefix to use based on the pathname it is
invoked with".
The xputenv call near the end exports the result as GCC_EXEC_PREFIX, whether
or not you set that variable yourself. Relocation and the environment variable
are therefore one mechanism, and the driver's own comment a few lines later says
so:
/* From this point onward, gcc_exec_prefix is non-null if the toolchain
is relocated. The toolchain was either relocated using GCC_EXEC_PREFIX
or an automatically created GCC_EXEC_PREFIX from
decoded_options[0].arg. */
Listing 1.5-5: the driver's summary of its own state
(gcc/gcc.cc:4855-4858).
Setting GCC_EXEC_PREFIX yourself pre-empts the computation, because the code
in Listing 1.5-4 runs only when gcc_exec_prefix is null.
Chapter 1.9 covers the variable.
Relocating the sysroot: only under $exec_prefix
Relocation moves GCC's own tree, but in general it does not move the sysroot.
The driver relocates a sysroot only when it was configured inside
$exec_prefix, so the configure line decides whether a prebuilt cross toolchain
unpacked in a new directory takes its sysroot along. The install manual states
the condition under Configuration:
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.
When the condition holds, configure defines an extra macro and the driver
relocates the sysroot too (gcc/configure.ac:176-182, acted on at
gcc/gcc.cc:5542-5559). A bare --with-sysroot, with no
argument, defaults to $exec_prefix/<target>/sys-root
(gcc/configure.ac:169-170), which meets the condition. The
default is therefore relocatable, and an explicit --with-sysroot=/arm-rootfs
is not.
The tool directory $prefix/<target>/: never sysrooted
The tool directory, or tooldir, $prefix/<target>/, goes on both lists
through add_prefix, not add_sysrooted_prefix, so --sysroot never moves it.
For a bare-metal toolchain it is the most important entry on the library list, because
newlib installs its libraries there. Two calls register it:
add_prefix (&exec_prefixes,
concat (tooldir_prefix, "bin", dir_separator_str, NULL),
"BINUTILS", PREFIX_PRIORITY_LAST, 0, 0);
add_prefix (&startfile_prefixes,
concat (tooldir_prefix, "lib", dir_separator_str, NULL),
"BINUTILS", PREFIX_PRIORITY_LAST, 0, 1);
Listing 1.5-6: the tool directory (gcc/gcc.cc:5534-5539).
The first call puts $prefix/<target>/bin/ on the program list, and the second
puts $prefix/<target>/lib/ on the library list. Because neither applies the
sysroot, -lc works with no -L and no sysroot once newlib is installed in
$prefix/<target>/lib/. For the same reason, a cross install finds
libstdc++.so there rather than in the sysroot, whatever --sysroot you pass.
The last argument of the library call, 1, marks the entry as an OS multilib
prefix, which changes how the driver expands it.
Chapter 1.6 explains that expansion.
The STANDARD_STARTFILE_PREFIX chain and the cross-compiler gate
The last step of the order adds either the target's STARTFILE_PREFIX_SPEC or
the STANDARD_STARTFILE_PREFIX chain. The chain runs only for a native compiler
or a compiler with a configured sysroot. A cross compiler with no sysroot
therefore gets no /lib and no /usr/lib, which is why its libraries: line
can look short. If the target defines STARTFILE_PREFIX_SPEC, the spec wins
outright:
/* Look for startfiles in the standard places. */
if (*startfile_prefix_spec != 0
&& do_spec_2 (startfile_prefix_spec, NULL) == 0
&& do_spec_1 (" ", 0, NULL) == 0)
{
for (const char *arg : argbuf)
add_sysrooted_prefix (&startfile_prefixes, arg, "BINUTILS",
PREFIX_PRIORITY_LAST, 0, 1);
}
Listing 1.5-7: the STARTFILE_PREFIX_SPEC branch
(gcc/gcc.cc:8579-8587).
The spec's entries go through add_sysrooted_prefix, so they follow --sysroot.
Without the spec, the standard chain runs, behind a guard:
/* We should eventually get rid of all these and stick to
startfile_prefix_spec exclusively. */
else if (*cross_compile == '0' || target_system_root)
Listing 1.5-8: the gate (gcc/gcc.cc:8588-8590).
The first condition holds for a native compiler, and the second when a sysroot is
configured. The gate is deliberate, because searching the host's /usr/lib for
target libraries gives file in wrong format errors from the linker or, worse,
a link that succeeds against the wrong objects.
The machine-dependent part of the chain also differs between native and cross
compilers. MD_EXEC_PREFIX, MD_STARTFILE_PREFIX and MD_STARTFILE_PREFIX_1
are #undef'd outright for a cross compiler, with the comment "Don't use these
prefixes for a cross compiler" (gcc/gcc.cc:1620-1624).
/lib/ and /usr/lib/ under the sysroot: why crt1.o moves
Inside the gate, the driver registers the two standard prefixes, /lib/ and
/usr/lib/, with add_sysrooted_prefix, so --sysroot=/arm-rootfs turns
/lib/ into /arm-rootfs/lib/. That call is the whole sysroot mechanism for
libraries, and it is why crt1.o follows --sysroot. The comment above the
calls ties the sysroot to relocation as well:
/* Sysrooted prefixes are relocated because target_system_root is
also relocated by gcc_exec_prefix. */
if (*standard_startfile_prefix_1)
add_sysrooted_prefix (&startfile_prefixes,
standard_startfile_prefix_1, "BINUTILS",
PREFIX_PRIORITY_LAST, 0, 1);
if (*standard_startfile_prefix_2)
add_sysrooted_prefix (&startfile_prefixes,
standard_startfile_prefix_2, "BINUTILS",
PREFIX_PRIORITY_LAST, 0, 1);
Listing 1.5-9: where the sysroot gets glued on
(gcc/gcc.cc:8621-8630).
The macros behind the two variables default to the usual system library directories:
#ifndef STANDARD_STARTFILE_PREFIX_1
#define STANDARD_STARTFILE_PREFIX_1 "/lib/"
#endif
#ifndef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_2 "/usr/lib/"
#endif
Listing 1.5-10: the two standard prefixes
(gcc/gcc.cc:1613-1618).
The GCC manual documents the same rewrite for --sysroot under
Directory Options, as dir/usr/lib in place of /usr/lib. No
other code applies the sysroot, and nothing can apply it to an entry after the
entry is registered. The entries registered with plain add_prefix, which are
-B, GCC_EXEC_PREFIX, the relocated install prefix and the tool directory,
are therefore never sysrooted.
crt1.o and crtbegin.o take the two routes. crt1.o lives in the sysroot's
/usr/lib, which Listing 1.5-9 registers. crtbegin.o lives in the libsubdir,
which the relocated install prefix reaches through add_prefix. Both use the
same %s and the same list, and the function that registered each directory
decides how the file answers --sysroot.
--sysroot=%R: the driver passes the sysroot to ld
When ld supports it, the driver passes the sysroot to ld as --sysroot=, so
that ld's own built-in search directories and any =-prefixed path inside a
linker script become sysroot-relative. The sysrooted prefixes are therefore not
the only route from --sysroot to the link. The driver adds this one by
rewriting the link spec at startup:
#ifdef HAVE_LD_SYSROOT
/* Pass the --sysroot option to the linker, if it supports that. If
there is a sysroot_suffix_spec, it has already been processed by
this point, so target_system_root really is the system root we
should be using. */
if (target_system_root)
{
obstack_grow (&obstack, "%(sysroot_spec) ", strlen ("%(sysroot_spec) "));
obstack_grow0 (&obstack, link_spec, strlen (link_spec));
set_spec ("link", XOBFINISH (&obstack, const char *), false);
}
#endif
Listing 1.5-11: prepending the sysroot to the link spec
(gcc/gcc.cc:8554-8566).
Listing 1.5-11 shows a spec name acting as a slot, as
Chapter 1.3 describes for %G: the driver rewrites the
link slot at startup, by name, through set_spec. The text it prepends refers
to another spec, which is one line:
#ifndef SYSROOT_SPEC
# define SYSROOT_SPEC "--sysroot=%R"
#endif
Listing 1.5-12: SYSROOT_SPEC (gcc/gcc.cc:1189-1191).
%R expands to the sysroot, so --sysroot=/arm-rootfs on your command line
becomes --sysroot=/arm-rootfs on ld's command line. -### shows the link
command without running it:
$ gcc --sysroot=/arm-rootfs -### hello.c 2>&1 | grep -o '\-\-sysroot=[^ "]*'
Among the matches is the --sysroot=/arm-rootfs on the link command, which %R
put there through the link spec. A compiler configured for an ld without
--sysroot support skips the rewrite in Listing 1.5-11.
Choosing among --prefix, --with-sysroot, --sysroot and -B
--prefix, --with-sysroot, --with-build-sysroot, --sysroot and -B all
look like "where GCC looks for things", but they are five different mechanisms.
Three are set at configure time and two at usage time. They move different
trees, so the question to settle before reaching for one is whose files need to
move. The install manual documents the configure-time flags under
Configuration, and the table adds whose tree each one moves:
| Flag | When | Moves | Whose tree |
|---|---|---|---|
--prefix | configure | where GCC installs itself | GCC's own tree |
--with-sysroot | configure | the default target root for libc | C library's tree |
--with-build-sysroot | configure | libc's location during the build only | C library's tree |
--sysroot | usage | overrides the configured sysroot for one run | C library's tree |
-B | usage | three search lists at once | both |
--sysroot overwrites target_system_root (gcc.cc:4559),
which every add_sysrooted_prefix call and every %R then picks up. One
--sysroot therefore moves both the sysrooted prefixes and the --sysroot= that
ld receives.
--with-build-sysroot: a sysroot used only while building
--with-build-sysroot sets the sysroot used only while building the target
libraries, and it leaves no trace in the installed compiler. The option explains
a category of cross-build bug: a libstdc++ that lacks features although the
build reported no error. The configure code is literal:
AC_ARG_WITH(build-sysroot,
[...],
[if test x"$withval" != x ; then
SYSROOT_CFLAGS_FOR_TARGET="--sysroot=$withval"
fi], ...)
Listing 1.5-13: --with-build-sysroot (gcc/configure.ac:140-146).
The option appends --sysroot= to the command line that the build uses to
compile libgcc and libstdc++. The configure script of libstdc++ runs several
hundred compile-and-link probes against the target C library, and when those
probes cannot link, the features they test configure themselves as not
available. The result is a libstdc++ with missing features rather than a build
failure, which is why the build needs the option.
Chapter 3.3 returns to it.
Which prefix supplied a file: -print-file-name and -print-search-dirs
-print-file-name and -print-search-dirs show which prefix supplied a file,
so they turn a missing crt1.o or crtbegin.o into a lookup. On a cross
toolchain, four commands cover the sysroot, the two lists and one startfile from
each owner:
$ arm-linux-gnueabihf-gcc -print-sysroot # the baked-in sysroot
$ arm-linux-gnueabihf-gcc -print-search-dirs # the two lists
$ arm-linux-gnueabihf-gcc -print-file-name=crt1.o # the libc half
$ arm-linux-gnueabihf-gcc -print-file-name=crtbegin.o # the libgcc half
-print-file-name looks the name up with find_file, and its behaviour on
failure is the whole body of that function:
find_file (const char *name)
{
char *newname = find_a_file (&startfile_prefixes, name, R_OK, true);
return newname ? newname : name;
}
Listing 1.5-14: find_file (gcc/gcc.cc:8068-8072).
If the file is not found, -print-file-name prints your argument back
unchanged, with no error and exit status 0. -print-libgcc-file-name is the same
lookup, not a separate mechanism: it is one assignment,
print_file_name = "libgcc.a" (gcc/gcc.cc:4298). Different files
come from different owners, so the name that comes back unchanged points at the
tree to fix:
| Symptom | Diagnosis |
|---|---|
crt1.o printed back unchanged | the sysroot is wrong |
crtbegin.o printed back unchanged | the prefix or -B is wrong |
libraries: short on a cross compiler | no sysroot configured (Listing 1.5-8) |
-print-search-dirs looks right, link still fails | multilib expansion (Chapter 1.6) |
-print-search-dirs prints exactly three lines, one for each printf in its
handler:
printf (_("install: %s%s\n"),
gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix,
gcc_exec_prefix ? "" : machine_suffix);
printf (_("programs: %s\n"),
build_search_list (&exec_prefixes, "", false, false));
printf (_("libraries: %s\n"),
build_search_list (&startfile_prefixes, "", false, true));
Listing 1.5-15: -print-search-dirs (gcc/gcc.cc:8795-8805).
install: is the resolved install root, and programs: and libraries: are
exec_prefixes and startfile_prefixes. Three facts decide how to read them:
- There is no
headers:line.cc1builds and prints the header search list, so no driver option can print it, and Chapter 1.8 shows how to askcc1. - The sysroot is already in the
libraries:entries, becauseadd_sysrooted_prefixprepended it when the list was built. An entry that does not start with your sysroot will never follow it. - The multilib suffixes are not in the entries, because the driver expands each
entry at lookup time, as Chapter 1.6 explains. A
directory can appear in
libraries:and still not be the one a given-mflag searches.
Three environment variables, GCC_EXEC_PREFIX, COMPILER_PATH and
LIBRARY_PATH, feed the same lists, and nothing on your command line shows
them. When a toolchain behaves differently in CI than on your desk, compare the
lists with and without them:
$ gcc -print-search-dirs
$ env -u GCC_EXEC_PREFIX -u COMPILER_PATH -u LIBRARY_PATH gcc -print-search-dirs
If the two outputs differ, one of the variables is set in that environment. Chapter 1.9 explains what each one adds.
Three things to carry away
The driver never opens a library file. It turns startfile_prefixes into
-L options and lets ld resolve every -l. It also walks the list itself, for
a %s startfile and for -print-file-name.
-B feeds all three lists, at the highest priority. It moves programs, link
inputs and GCC's headers at once.
A cross compiler with no sysroot gets no /lib and no /usr/lib. That gate
protects you from linking against the host's libraries.
Documentation coverage
The internals manual documents the startfile prefix order, with its conditions,
at gcc/doc/tm.texi:570. The GCC manual documents -B and
--sysroot under Directory Options
(gcc/doc/invoke.texi:19484), -print-search-dirs and -print-file-name under
Developer Options, and relocation under GCC_EXEC_PREFIX in
Environment Variables. The install manual documents the
configure-time flags under Configuration, from
gcc/doc/install.texi.
Four things are not documented:
- That
add_prefixversusadd_sysrooted_prefixis what decides whether a directory follows--sysroot. The manual lists which prefixes get "any sysroot modifications" per entry, which is the same information, but it never says the mechanism is per-entry and irreversible. - That
-print-search-dirsshows the list before multilib expansion. - That
-print-search-dirshas noheaders:line, and that no driver option prints header paths. - That
-print-file-namereturns its argument unchanged when the file is not found. The manual says only that it prints the full name of the file that would be used (invoke.texi:21572-21575).
Next: Chapter 1.6, Multilib and multiarch: several ABIs under one
triple, why the directory in libraries: is not
necessarily the directory that gets searched.