3.1 The libgcc–libc cycle: make all-gcc and the three cross recipes
GCC 15.2.0, with glibc 2.42. To run the commands: a GCC checkout.
Every recipe that builds a cross toolchain from nothing has the same odd shape.
It configures GCC and then types make all-gcc where a native build types
make. In a hosted recipe it installs half of a C library, its headers and start
files, and then goes back to the GCC build tree to finish the job. If you follow such a recipe
for a Linux target but type make at that point, the build compiles the
compiler, starts on libgcc and stops there. libgcc wants the C library's
headers, and the C library does not exist yet.
libgcc needs the target's C library to finish, and the C library needs libgcc to
link. make all-gcc builds the compiler and stops before libgcc, and that stop
is the cut that every recipe makes. What a recipe builds in the gap depends on
the target, and one question about the target's C library picks the recipe.
This chapter assumes Chapter 1.2, which stated the
cycle in four steps, drew the directory map and named inhibit_libc, and
Chapter 1.7, which showed which start files the C
library supplies and which libgcc supplies. It also uses fixincludes from
Chapter 1.8, the -nodefaultlibs table of
Chapter 1.10, libgcc's config.host and
its makefile fragments from Chapter 2.1, the
top-level makefile and its maybe- targets from
Chapter 2.3, and the native bootstrap of
Chapter 2.5.
tsystem.h: where libgcc asks for <stdio.h>
libgcc reaches the C library's headers through one header from the compiler's
directory, gcc/tsystem.h, and that header is the first edge of the cycle.
Unless inhibit_libc is defined, it includes headers that only the C library
installs, so libgcc does not compile until the C library's headers are
installed. The header has two halves, and Listing 3.1-1 shows the one with those
includes.
Chapter 1.2 quoted the --without-headers entry of the install manual's
Configuration page. It says that a GNU/Linux target needs the C
library's headers "so GCC can build the exception handling for libgcc". The
source asks for them in more places than the exception handling. libgcc2.c,
the unwinder's unwind-dw2.c, crtstuff.c and emutls.c all include
tsystem.h, and libgcc's include path reaches into gcc/ to find it
(libgcc/libgcc2.c:27,
unwind-dw2.c:26, crtstuff.c:61,
emutls.c:27,
libgcc/Makefile.in:286-287).
#else /* ! inhibit_libc */
/* We disable this when inhibit_libc, so that gcc can still be built without
needing header files first. */
/* ??? This is not a good solution, since prototypes may be required in
some cases for correct code. */
/* GCC supplies this header. */
#include <stdarg.h>
/* All systems have this header. */
#include <stdio.h>
...
/* If these system headers do not exist, fixincludes must create them. */
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
...
#endif /* inhibit_libc */
Listing 3.1-1: the C library branch of tsystem.h, abridged
(gcc/tsystem.h:85-118).
Without inhibit_libc, every one of those files reads <stdio.h>, <stdlib.h>,
<unistd.h> and four more headers that only the C library installs. The other
branch declares malloc, free, memcpy and five more functions by hand, and
turns abort () into __builtin_trap ()
(gcc/tsystem.h:47-83). The comment at the top of
Listing 3.1-1 gives GCC's reason for the switch: "so that gcc can still be built
without needing header files first".
inhibit_libc reaches the compiler as -Dinhibit_libc
(gcc/Makefile.in:791-793). In outline, gcc/configure sets
it to true when two clauses both hold. The first is a cross build with no
sysroot, or --with-newlib. The second is --without-headers, or no stdio.h
among the target's headers (gcc/configure.ac:2569-2583). A
hosted recipe's first GCC build, with a sysroot and without --with-newlib,
therefore keeps it false, and a bare-metal first build, with no sysroot and no C
library headers, gets it true. Chapter 3.2 reads
the test and the flags that it needs.
linux-unwind.h and dl_iterate_phdr: the unwinder reads the C library
On Linux, libgcc's unwinder reads the C library through more than its headers.
linux-unwind.h uses the types that the C library declares, and
dl_iterate_phdr is a function that the C library implements. Both
dependencies sit in libgcc's exception-handling code, which is the half of
libgcc that the cycle runs through.
For aarch64*-*-linux*, libgcc's config.host selects
config/aarch64/linux-unwind.h as the unwinder's target header
(libgcc/config.host:438-441). Its signal-frame code sits
under a guard on inhibit_libc and reads two C library headers:
#ifndef inhibit_libc
#include <signal.h>
#include <sys/ucontext.h>
...
/* The kernel creates an rt_sigframe on the stack immediately prior
to delivering a signal.
This structure must have the same shape as the linux kernel
equivalent. */
struct rt_sigframe
{
siginfo_t info;
ucontext_t uc;
};
Listing 3.1-2: the signal-frame fallback of the AArch64 Linux unwinder, abridged
(libgcc/config/aarch64/linux-unwind.h:26-59).
The fallback lets the unwinder pass through the frame of a signal handler. It
reads the frame that the kernel wrote, through types from the C library's
headers. Under inhibit_libc the whole fallback disappears, because the
#ifndef closes on the last line of the file
(linux-unwind.h:163).
Chapter 4.3 covers the unwinder itself.
Every *-*-linux* target also gets t-eh-dw2-dip
(config.host:313-314), a makefile fragment that
config.host adds to libgcc's build through tmake_file. The fragment puts
unwind-dw2-fde-dip.c into libgcc
(libgcc/config/t-eh-dw2-dip:1-3). That file finds the unwind
tables of a running program, the frame description entries (FDEs) that the
assembler writes into .eh_frame for each function.
To find them, unwind-dw2-fde-dip.c asks the C library where each loaded
object's PT_GNU_EH_FRAME segment is, and Chapter 4.3 follows that lookup, which
tries other methods first. Its fallback asks through dl_iterate_phdr
(unwind-dw2-fde-dip.c:569), a C library function that
<link.h> declares. The file includes <link.h> and
uses that method only when inhibit_libc is not defined
(:49-88). Otherwise it falls back to the generic
unwind-dw2-fde.c (:576-580).
SHLIB_LC = -lc: libgcc_s.so links the C library
libgcc also links a shared library, libgcc_s.so, and its link line adds the
C library through SHLIB_LC = -lc, so libgcc cannot finish until the C library
can be linked. The headers were the compiling half of the dependency, and this
is the linking half. A target gets libgcc_s.so when shared libraries are
enabled and its config.host entry names the fragment t-slibgcc
(libgcc/Makefile.in:395-397), as the *-*-linux* entry
does (config.host:313-314). The link rule is in
libgcc/config/t-slibgcc:
SHLIB_LC = -lc
...
SHLIB_LINK = $(CC) $(LIBGCC2_CFLAGS) -shared -nodefaultlibs \
$(SHLIB_LDFLAGS) $(LDFLAGS) \
-o $(SHLIB_DIR)/$(SHLIB_SONAME).tmp @multilib_flags@ \
$(SHLIB_OBJS) $(SHLIB_LC) && \
Listing 3.1-3: how libgcc_s.so is linked, abridged
(libgcc/config/t-slibgcc:29-37).
$(CC) is the new compiler. -nodefaultlibs removes the default libraries, and
$(SHLIB_LC) then puts the C library back by hand. The GCC manual documents
under Link Options that -nodefaultlibs keeps the start files, and
the table in Chapter 1.10 shows it. A
-shared link on GNU/Linux therefore asks for crti.o and crtn.o, which the C
library installs (Listing 1.7-5). The shared half of libgcc needs three things
from the C library before it can link: crti.o, crtn.o, and a library that
answers -lc.
When shared libraries are enabled, libgcc puts the exception-handling objects
into libgcc_s.so and libgcc_eh.a, not into libgcc.a
(libgcc/Makefile.in:867-890).
Chapter 4.2 says which file holds what.
The split matters here because the C library links only one of those files,
libgcc.a.
libc.so-gnulib in glibc's Makeconfig: the C library links libgcc
glibc links libc.so with -lgcc through the variable libc.so-gnulib, so
the C library cannot link until a libgcc.a exists, and that edge closes the
cycle. The variable is in glibc's top-level Makeconfig, which belongs to glibc
and not to GCC, and its comment states what glibc expects from libgcc in glibc's
own words. Watch the comment's split between libgcc.a and the
exception-handling functions, and the last line:
# How to link against libgcc. Some libgcc functions, such as those
# for "long long" arithmetic or software floating point, can always be
# built without use of C library headers and do not have any global
# state so can safely be linked statically into any executable or
# shared library requiring them; these functions are in libgcc.a.
# Other functions, relating to exception handling, may require C
# library headers to build and it may not be safe to have more than
# one copy of them in a process; these functions are only in
# libgcc_s.so and libgcc_eh.a.
#
# To avoid circular dependencies when bootstrapping, it is desirable
# to avoid use of libgcc_s and libgcc_eh in building glibc. Where any
# glibc functionality (in particular, thread cancellation) requires
# exception handling, this is implemented through dlopen of libgcc_s
# to avoid unnecessary dependencies on libgcc_s by programs not using
# that functionality; executables built with glibc do not use
# exception handling other than through thread cancellation.
...
libc.so-gnulib := -lgcc
Listing 3.1-4: glibc's view of libgcc, abridged
(Makeconfig:672-688 and :732, at the tag
glibc-2.42).
The comment also gives the way out. libgcc.a holds the functions that "can
always be built without use of C library headers". The parts that need the C
library are the exception-handling ones, and glibc's build avoids them on
purpose. So the cycle does not run between two whole libraries. It runs between
half of libgcc and the C library, and each side needs this much from the other:
- glibc needs only
libgcc.a, and only the functions in it that use no C library header. - The rest of libgcc, the unwinder and
libgcc_s.so, builds once glibc's headers, its start files and alibc.soexist.
The list gives what each side needs, not the order that a recipe follows. A
hosted recipe installs the C library's headers before it builds libgcc, so its
libgcc.a is compiled with them. That libgcc.a still links glibc, because
glibc uses none of the parts that need the headers.
GCC's configure machinery states its side of the same problem. libgcc's
configure calls GCC_NO_EXECUTABLES
(libgcc/configure.ac:219). The comment on that macro gives
the reason: "Several of the support libraries that are often built with GCC
can't assume the tool-chain is already capable of linking a program". The quotation
continues: "the compiler often expects to be able to link with some
of such libraries" (config/no-executables.m4:8-11).
make all-gcc: the target that stops before libgcc
make all-gcc builds the compiler and the host modules that it needs, and no
target module. Both sides of the cycle need that compiler first, because libgcc
and the C library are built for the target with it. The top-level makefile's
rules show both directions: nothing that all-gcc needs is a target module, and
every target module waits for all-gcc.
Plain make runs the all rule, which builds all-host all-target
(Makefile.tpl:860-877): every host module, then every target
module. Of the two modules that matter here, libgcc is a target module
(Makefile.def:183-185), and gcc, the directory that builds
xgcc and cc1, is a host module.
all-gcc builds the gcc host module and what it depends on. Every one of
those dependencies is a host or build module, such as binutils, libiberty,
libcpp or GMP, and no target module is on the list
(Makefile.def:367-398).
The target modules' dependencies point the other way. The configure step of
each target module waits for all-gcc (Makefile.tpl:1927-1930), because
the xgcc that all-gcc produces is the compiler for every target library.
That compiler is CC_FOR_TARGET (Makefile.tpl:591), set to
gcc/xgcc -B$$r/$(HOST_SUBDIR)/gcc/ at
configure.ac:4059. Each target module except libgcc also
waits for libgcc (Makefile.tpl:2108-2111).
Two greps over the top-level Makefile.in check both directions. You can run
them in a GCC source tree with nothing configured, because the file is generated
and checked in. Each edge points at a maybe- target, which stays empty when
configure leaves its module out (Chapter 2.3):
$ grep -E '^configure-target-(libgcc|libstdc\+\+-v3): maybe-all' Makefile.in
configure-target-libstdc++-v3: maybe-all-gcc
configure-target-libgcc: maybe-all-gcc
configure-target-libstdc++-v3: maybe-all-target-libgcc
configure-target-libstdc++-v3: maybe-all-target-newlib maybe-all-target-libgloss
$ grep -E '^(all|configure)-gcc:.*target' Makefile.in
$
Output 3.1-1: grep over the top-level Makefile.in, GCC 15.2.0 on an
unconfigured releases/gcc-15.2.0 tree.
The first command shows the order: the compiler, then libgcc, then the other libraries. Its last line shows that libstdc++ also waits for newlib and libgloss, which Chapter 3.6 uses. The second command prints nothing, because no rule makes the compiler wait for a target library.
If you type a plain make for a hosted target that has no C library yet, it
continues from all-gcc into libgcc and fails there, so every recipe names its
make targets. The later targets split the build the same way.
all-target-libgcc builds libgcc alone (Makefile.in:50400),
and install-gcc and install-target-libgcc install each half
(:12862, :50809).
A cross build uses these rules unchanged, because it does not bootstrap by
default. configure turns bootstrapping on by default only when the host and
the target are both the build machine (configure.ac:1532-1541).
The install manual documents that default for a native build in its
--disable-bootstrap entry, under Configuration.
stmp-fixinc: what make all-gcc still reads from the target
make all-gcc compiles nothing for the target, but its stmp-fixinc rule
still reads the target's header directory, because it runs fixincludes over it.
That read can stop the build before libgcc, so a hosted recipe has to prepare
the directory before its first GCC build. In a cross compiler, all-gcc reaches
fixincludes through four make targets:
all.cross, thealltarget ofgcc/Makefile.inin a cross compiler (gcc/configure.ac:2550)libgcc-support, whichall.crossincludes (gcc/Makefile.in:2218-2220)stmp-int-hdrs, whichlibgcc-supportneeds (:2459-2460)stmp-fixinc, whichstmp-int-hdrsneeds (:3475)
stmp-fixinc runs fixincludes, the subject of
Chapter 1.8, over the target's header
directory. First it checks that the directory exists:
if ! $(inhibit_libc) && test ! -d ${BUILD_SYSTEM_HEADER_DIR}; then \
echo "The directory (BUILD_SYSTEM_HEADER_DIR) that should contain system headers does not exist:" >&2 ; \
...
tooldir_sysinc=`echo "${gcc_tooldir}/sys-include" | sed -e :a -e "s,[^/]*/\.\.\/,," -e ta`; \
if test "x${BUILD_SYSTEM_HEADER_DIR}" = "x$${tooldir_sysinc}"; \
then sleep 1; else exit 1; fi; \
fi; \
Listing 3.1-5: the header check in stmp-fixinc, abridged
(gcc/Makefile.in:3616-3627).
Which directory the check tests depends on the sysroot that you configure:
- With a sysroot, it is the sysroot's
/usr/include(gcc/configure.ac:174, with the default/usr/includefromgcc/config.gcc:253). - With
--with-build-sysroot, it is the same path under the build sysroot (gcc/configure.ac:2539-2545). - Without a sysroot, it is
$tooldir/sys-include(gcc/configure.ac:188).
The install manual documents --with-sysroot under
Configuration, as the root of "(a subset of) the root
filesystem of the target operating system". The same page documents
--with-build-sysroot, a second sysroot that replaces the first while the
target libraries build.
The check stops make all-gcc when inhibit_libc is false and the directory is
missing. The only exception is a missing $tooldir/sys-include, which costs one
second of sleep and no error. In this rule, $(inhibit_libc) is the make
variable behind -Dinhibit_libc, and its value, the word true or false,
runs as a shell command (Chapter 3.2).
The comment above the rule does not match the code. It promises an abort
"unless building a crosscompiler"
(gcc/Makefile.in:3600-3601), but the code also aborts
for a cross compiler with a sysroot.
The check tests only that the directory exists. If you configure a hosted
recipe with a sysroot, inhibit_libc stays false, and the recipe's kernel
headers make the directory exist. They go into the sysroot's /usr/include
before the first GCC build in any case, because glibc's headers include them.
Phases of a hosted bootstrap around make all-gcc
The edges in tsystem.h, t-slibgcc, glibc's Makeconfig and stmp-fixinc
fix the order of a hosted bootstrap, for example for aarch64-linux-gnu. Each
phase can start only when the phases that it waits for are done. The table gives
the reason for each wait, so a failed step can be traced to the phase that it
needed.
Untested recipe. The order comes from the sources that the table cites. The sequence was not run end to end.
In Part III, stage 1 is the first GCC build, the one that starts with
make all-gcc, and stage 2 is the second, complete GCC build, in a new build
directory. Neither is the stage1 of a native bootstrap in
Chapter 2.5.
Chapter 1.2 drew this order as binutils → libc headers → minimal GCC → full libc → full GCC. The table splits its "libc headers" step in two. The Linux
kernel headers come before stage 1, because all-gcc needs the directory.
glibc's own headers and start files come after it, because they are built for
the target. Its "full GCC" step splits too: libgcc comes before the full C
library, and the rest of GCC after it.
| Phase | Waits for | Why |
|---|---|---|
| binutils for the target | nothing | GCC's configure needs the target assembler and linker, as the install manual's Building a cross compiler section says (install.texi:3281-3285) |
| Linux kernel headers, into the sysroot | nothing | glibc's headers include them (local_lim.h:38), and all-gcc needs the directory (Listing 3.1-5) |
Stage 1: make all-gcc | binutils, the sysroot's /usr/include | all-gcc depends on host and build modules only (Makefile.def:367-398) |
C library headers, crti.o, crtn.o and a placeholder libc.so | the stage-1 compiler | they are built for the target, and the placeholder only has to answer -lc for the libgcc_s.so link |
libgcc, with libgcc_s.so, in the stage-1 tree (make all-target-libgcc) | C library headers, crti.o, crtn.o, a libc.so | Listings 3.1-1 and 3.1-3 |
| The full C library | libgcc.a | libc.so links with -lgcc (Listing 3.1-4) |
| Stage 2: the full GCC, with libstdc++ and the other target libraries | the full C library | they wait for libgcc (Makefile.tpl:2108-2111), and Chapter 3.3 explains what they need from the C library |
The placeholder libc.so breaks the cycle on the link side. libgcc links
libgcc_s.so against it, and the full C library in the next row replaces it.
Step 4 of Chapter 3.3 makes it, and that
chapter gives the commands for each phase.
A bare-metal target removes rows from the table, because there is no kernel and
no sysroot. The arm*-*-eabi* entry in libgcc's config.host names no
t-slibgcc, so there is no libgcc_s.so to link against a C library
(config.host:578-584). inhibit_libc is true for a
bare-metal stage 1, so tsystem.h asks for no C library header.
GCC's own makefile expects libgcc to build first on such a target. In a
combined tree, newlib's configure step waits for libgcc
(Makefile.in:69375).
Chapter 3.4 gives the bare-metal sequence.
Prebuilt sysroot, hosted or bare metal: the C library decides
A prebuilt sysroot, a hosted bootstrap and a bare-metal bootstrap are the three cross recipes, and one question picks among them. Does a C library for the target already exist, with its headers, its start files and a library that you can link? The answer picks one of Chapters 3.3 to 3.5, which are alternative routes through the cycle, not a sequence. Chapter 3.6 is a variant of the bare-metal route.
-
Yes. Chapter 3.5 covers this case, and there is no cycle to break. One
configurewith--with-sysrootand onemakebuild everything. This is the case that the install manual documents, and a native rebuild on a machine that already has its C library is this case too. -
No, for a Linux target. You need the hosted bootstrap of Chapter 3.3, with each phase of the hosted table as a step.
-
No, for a bare-metal target. The target has no operating system, so you need the bare-metal bootstrap of Chapter 3.4. If newlib's source can sit next to GCC's, Chapter 3.6 builds the same toolchain with one
make.
The install manual's Building a cross compiler section is the
documented path, and it covers only the first case and the in-tree variant. It
says that make builds the compiler and then the "runtime libraries using the
compiler from the previous step" (install.texi:3276). It then
says: "If you are not building a C library in the same source tree as GCC, you
should also provide the target libraries and headers before configuring GCC"
(:3313-3315). The page never names the cycle or
make all-gcc.
aarch64-linux-gnu against arm-none-eabi: hosted and bare metal
The hosted triple aarch64-linux-gnu and the bare-metal triple arm-none-eabi
differ in the rows that the cycle touches, and Chapters 3.3 and 3.4 follow
them. The bare-metal column needs less from outside GCC: no kernel to supply
headers, no shared libgcc to link against the C library, and start files that
libgcc mostly supplies itself.
Hosted, aarch64-linux-gnu | Bare metal, arm-none-eabi | |
|---|---|---|
| C library | glibc, or musl for a *-musl triple (config.gcc:913-926) | newlib, or none |
| Linux kernel headers | needed before the C library (local_lim.h:38) | none |
| Where the C library goes | a sysroot (Chapter 1.2) | the tooldir, $prefix/<target>/, which the driver searches (gcc.cc:5536-5538) |
| Start files from the C library | crt1.o, crti.o, crtn.o (Listings 1.7-4 and 1.7-5) | crt0.o (unknown-elf.h:32-45) |
| Start files from libgcc | crtbegin*.o, crtend*.o | crti.o, crtn.o, crtbegin.o, crtend.o (config.host:584) |
libgcc_s.so, linked with -lc | yes (config.host:313-314) | no (config.host:578-584) |
| GCC builds | two (Chapter 3.3) | two (Chapter 3.4), or one in a combined tree (Chapter 3.6) |
On arm-none-eabi, libgcc builds crti.o and crtn.o itself, so the C library
supplies only crt0.o. The configure flags that make stage 1 possible also
differ between the two columns, and Chapter 3.2
explains them.
Three things to carry away
Only half of libgcc is in the cycle. glibc links libc.so with -lgcc
alone, and the functions that it takes from libgcc.a need no C library header.
The Linux unwinder and libgcc_s.so do not build without the C library.
make all-gcc builds the compiler and stops before libgcc. No dependency of
all-gcc names a target module, and every target module waits for all-gcc.
One question picks the recipe. If the target's C library exists, build once against it. If it does not, the target's operating system decides between the hosted and the bare-metal recipe.
Things that surprise people
"Stage 1" in a cross recipe is not stage1 in a bootstrap. The install
manual says that a three-stage bootstrap of a cross compiler "is not generally
possible" (install.texi:3249-3251), and a cross build
does not bootstrap by default (configure.ac:1532-1541). The
stage1- directories of Chapter 2.5 never
appear in a Part III build.
make all-gcc can fail on an empty sysroot. When inhibit_libc is false,
stmp-fixinc stops if the sysroot has no /usr/include (Listing 3.1-5).
glibc avoids libgcc_s.so on purpose. Its makefile says so, "to avoid
circular dependencies when bootstrapping" (Listing 3.1-4).
Documentation coverage
The install manual documents the case with no cycle in it.
Building a cross compiler (gcc/doc/install.texi:3247-3322)
describes one make that builds the compiler and then the runtime libraries. It
tells you to provide the target headers and libraries before you configure, and
it names STARTFILE_SPEC as the place to look up a target's start files
(:3321-3322). Configuration documents --with-sysroot
(:2709-2731), --without-headers (:2763-2766) and --with-newlib
(:2776-2780), one at a time.
What is not documented:
- The cycle itself. The
--without-headersentry says that a GNU/Linux target needs headers to build libgcc's exception handling. No page says thatlibgcc_s.solinks against the C library, or that the C library links libgcc. make all-gcc. No manual names the top-level target that every cross recipe uses to split the build at libgcc, orall-target-libgccafter it. The internals manual's Makefile Targets node says that the top-level makefile uses "the new compiler to build target libraries" (gcc/doc/makefile.texi:81-84). Its list of top-level targets (:108) names only bootstrap targets.tsystem.h, the header through which libgcc includes the C library's headers.- That
make all-gccruns fixincludes, and stops when the sysroot's header directory is missing. - Which recipe a given target needs. The manual has one procedure, for a target whose C library already exists.
Next: Chapter 3.2, "inhibit_libc: the switch that
makes stage 1 possible", reads the configure test that picks the half of
tsystem.h that libgcc compiles, and shows why --without-headers alone does
not set it.