3.2 inhibit_libc: the switch that makes stage 1 possible
Source references in this chapter are to GCC 15.2.0 (releases/gcc-15.2.0).
You configure a stage-1 cross compiler for aarch64-linux-gnu with a sysroot and
--without-headers. make all-gcc finishes. Then libgcc stops on its first
file:
fatal error: stdio.h: No such file or directory
The install manual's Configuration page says that
--without-headers "Tells GCC not use any target headers from a libc"
(install.texi:2763-2766). The flag did its job. It is
one half of a condition, and the sysroot switched off the other half.
--without-headers alone does not set inhibit_libc. inhibit_libc is a
variable in gcc/configure. It decides whether libgcc is compiled with the target
C library's headers. Two clauses decide it, and --without-headers answers only
the second.
This chapter assumes Chapter 1.2, for the tooldir
and the sysroot, and Chapter 3.1, for the
cycle that stage 1 breaks and for make all-gcc.
The condition in gcc/configure.ac
The whole switch is one shell if, with the two comments that introduce it:
# If this is a cross-compiler that does not
# have its own set of headers then define
# inhibit_libc
# If this is using newlib, without having the headers available now,
# then define inhibit_libc in LIBGCC2_CFLAGS.
# This prevents libgcc2 from containing any code which requires libc
# support.
: ${inhibit_libc=false}
if { { test x$host != x$target && test "x$with_sysroot" = x ; } ||
test x$with_newlib = xyes ; } &&
{ test "x$with_headers" = xno || test ! -f "$target_header_dir/stdio.h"; } ; then
inhibit_libc=true
fi
AC_SUBST(inhibit_libc)
Listing 3.2-1: the condition (gcc/configure.ac:2569-2583).
Read it as two groups joined by &&. Each group is an || of two tests:
| Clause | True when | The flag that makes it true |
|---|---|---|
| first | a cross build with no --with-sysroot, or --with-newlib | --with-newlib |
| second | --without-headers, or no stdio.h in $target_header_dir | --without-headers |
inhibit_libc is true only when both clauses are true. The two comments describe
two cases: a cross compiler with no headers, and a newlib build whose headers do
not exist yet. The code merges the two cases into one test, so no single flag
controls the result.
The flags reach this test as plain shell variables. Autoconf turns
--without-headers into with_headers=no (configure:1267-1281),
and the top level passes your options down to gcc/configure
(configure.ac:3596-3608).
The first line, : ${inhibit_libc=false}, assigns false only if the variable
has no value yet. gcc/configure.ac runs the target's config.gcc entry long
before this point (gcc/configure.ac:1931). One entry uses
that: mcore-*-elf sets inhibit_libc=true itself
(gcc/config.gcc:2471-2475). After that line, the if can only set
true. Nothing in gcc/configure.ac sets the variable back to false.
Before GCC 5, the second clause did not look at the disk. At
releases/gcc-4.9.0it readtest "x$with_headers" = x || test "x$with_headers" = xno(gcc/configure.ac:1886-1891), so every build that did not pass--with-headerssatisfied it.releases/gcc-5.1.0has the current form (gcc/configure.ac:1994-1999). The ChangeLog entry of 2014-09-22 records the change: "When considering $with_headers, just check it it's explicitly "no". If not, also check if $target_header_dir/stdio.h is present." (gcc/ChangeLog-2014:17598-17606).
--with-sysroot turns the first clause off
The first clause tests "x$with_sysroot" = x, that is, an empty value. Any
--with-sysroot makes the value non-empty, with a directory or without one. The
install manual documents both forms under Configuration: the
directory is the root in which the compiler looks for target headers, libraries
and run-time object files. So on a cross build with a sysroot, only
--with-newlib can make the first clause true.
That is the case from the start of this chapter. These are the three flags of the hosted stage-1 configure line that matter here (Chapter 3.3 gives the rest):
--target=aarch64-linux-gnu --with-sysroot=/opt/cross/sysroot --without-headers
--without-headers makes the second clause true. --with-sysroot makes the first
clause false, and this is not a newlib build. So inhibit_libc stays false.
The build still gets through make all-gcc, and Chapter 3.1 explains why the
recipe stops there. But all-gcc reads the value once. A cross build's all
target depends on libgcc-support (gcc/Makefile.in:2219-2220),
which depends on the fixincludes stamp
(:2459-2460, :3475). The fixincludes rule
stops the build when the system header directory is missing and inhibit_libc is
false:
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 ; \
echo " ${BUILD_SYSTEM_HEADER_DIR}" >&2 ; \
...
if test "x${BUILD_SYSTEM_HEADER_DIR}" = "x$${tooldir_sysinc}"; \
then sleep 1; else exit 1; fi; \
Listing 3.2-2: the one test in all-gcc that reads inhibit_libc, abridged
(gcc/Makefile.in:3616-3626).
Here $(inhibit_libc) runs as a shell command. Its value is the word true or
false, so the test becomes ! true or ! false. The comment above the rule
admits that this is a proxy: "FIXME: abort unless building --without-headers
would be more accurate and less ugly" (gcc/Makefile.in:3600-3601).
The sleep 1 branch lets a build continue when the missing directory is the
tooldir's sys-include, which is the header directory of a cross compiler with
no sysroot (gcc/configure.ac:2544, :185-189). With a
sysroot, the directory is the sysroot's usr/include
(:174), and it must exist. In the hosted recipe it does,
because the kernel headers go into it before stage 1 (Chapter 3.3).
libgcc is where the build fails. Its sources include gcc/tsystem.h, and with
inhibit_libc false that header includes <stdio.h> (Listing 3.2-6). A sysroot
that holds only kernel headers has no stdio.h.
To get inhibit_libc with a sysroot, add --with-newlib. It is the only part of
the first clause that a sysroot cannot cancel. The install manual documents it
under Configuration as the flag that names newlib as the target
C library. But it is not private to this condition. The top level reads it to
decide whether to build newlib (configure.ac:292-296).
libstdc++'s configure reads it too.
On a cross build it then configures for newlib, with a hard-coded list of features
(libstdc++-v3/configure.ac:335-343). A C-only stage 1 builds no
libstdc++. A later stage that builds libstdc++ for glibc must not get the flag.
In a combined tree with newlib, you do not pass --with-newlib at all. The top
level adds it to the gcc/configure arguments itself when newlib is one of the
target directories (configure.ac:2997-3003).
Chapter 3.6 covers that build.
$target_header_dir: where configure looks for stdio.h
The second clause tests one file in one directory. gcc/configure.ac computes the
directory a few lines earlier:
if test "x$with_build_sysroot" != "x"; then
target_header_dir="${with_build_sysroot}${native_system_header_dir}"
elif test "x$with_sysroot" = x; then
target_header_dir="${test_exec_prefix}/${target_noncanonical}/sys-include"
elif test "x$with_sysroot" = xyes; then
target_header_dir="${test_exec_prefix}/${target_noncanonical}/sys-root${native_system_header_dir}"
else
target_header_dir="${with_sysroot}${native_system_header_dir}"
fi
else
target_header_dir=${native_system_header_dir}
fi
Listing 3.2-3: the probe directory (gcc/configure.ac:2556-2567).
The else at the bottom is for a native build with no sysroot
(:2539-2540). test_exec_prefix is --exec-prefix, else
--prefix, else /usr/local (:149-158).
native_system_header_dir is /usr/include unless the target's config.gcc
entry or --with-native-system-header-dir changes it
(gcc/config.gcc:253, gcc/configure.ac:2054-2056). So:
| Configuration | configure looks for |
|---|---|
--with-build-sysroot=B | B/usr/include/stdio.h |
cross, no --with-sysroot | $exec_prefix/<target>/sys-include/stdio.h |
--with-sysroot with no directory | $exec_prefix/<target>/sys-root/usr/include/stdio.h |
--with-sysroot=S | S/usr/include/stdio.h |
| native, no sysroot | /usr/include/stdio.h |
Three things in this table cost people time.
--with-build-sysroot wins. When you give both, the probe looks in the build
sysroot, not in the one the installed compiler will use. The install manual
documents the option under Configuration, as the system root
for the compiler that builds the target libraries.
With no sysroot, the probe looks in sys-include, not in include. The
tooldir has both directories (Chapter 1.2), and the compiler searches both
(gcc/Makefile.in:3408-3409). The probe looks only in
sys-include. A C library whose headers are installed in <target>/include does
not exist as far as the probe can tell.
The probe runs once, when gcc/configure runs. Headers that you install later
do not change the answer until gcc/ is configured again. So each stage of a
bootstrap needs its own run of gcc/configure.
The stdio.h probe adds to your flags, never cancels them
The second clause is an ||. Either test is enough. Two consequences follow, and
both are easy to read the wrong way round.
--without-headers wins over the disk. With the flag, the second clause is
true whether stdio.h exists or not. The disk check cannot cancel it.
A missing stdio.h sets the second clause without the flag. You do not need
--without-headers when the probe directory is empty.
Now apply both clauses to a bare-metal stage 1, such as arm-none-eabi with no
sysroot. The build is a cross with no --with-sysroot, so the first clause is
already true. If sys-include has no stdio.h, the second clause is true too. So
inhibit_libc is true with neither flag. The conventional
--without-headers --with-newlib pair still earns its place. Each flag pins one
clause, so the result no longer depends on what is on the disk, or on a
--with-sysroot added later. Chapter 3.4
uses the pair.
The same rule can work against a bare-metal compiler's final stage. Take a final
stage with no sysroot, configured with --with-newlib and no other header flag.
The first clause is true. If the C library's headers are in <target>/include
and not in <target>/sys-include, the probe finds no stdio.h, and the second
clause is true too. So inhibit_libc is still true in the compiler you install.
The next section shows the flag that puts the headers where the probe looks.
--with-headers=DIR puts headers where the probe looks
--with-headers=DIR is the positive form of --without-headers. The top-level
configure handles it, not gcc/configure:
# Handle --with-headers=XXX. If the value is not "yes", the contents of
# the named directory are copied to $(tooldir)/sys-include.
if test x"${with_headers}" != x && test x"${with_headers}" != xno ; then
if test x${is_cross_compiler} = xno ; then
echo 1>&2 '***' --with-headers is only supported when cross compiling
exit 1
fi
if test x"${with_headers}" != xyes ; then
x=${gcc_cv_tool_prefix}
copy_dirs="${copy_dirs} ${with_headers} $x/${target_noncanonical}/sys-include"
fi
fi
Listing 3.2-4: --with-headers (configure.ac:2931-2942).
gcc_cv_tool_prefix is the same --exec-prefix, else --prefix, else the default
(config/acx.m4:258-267). So the copy lands in the directory that
Listing 3.2-3 probes when there is no sysroot. The copy itself runs inside the
top-level configure, with tar (configure.ac:3005-3033).
gcc/configure runs later, from the top-level Makefile
(configure.ac:3319-3321). So the probe finds stdio.h if
DIR has it.
Three details:
--with-headerswith no directory copies nothing. It also leaves the second clause to the disk, because the clause tests only forno.- A native build stops with an error. The option is for cross compilers only.
- The install manual's Configuration page marks the option
"Deprecated in favor of
--with-sysroot", and says the directory form "is required" whenprefix/target/sys-includedoes not already exist (gcc/doc/install.texi:2751-2761). The probe explains that sentence.
What -Dinhibit_libc removes from libgcc
gcc/configure decides, and libgcc obeys. gcc/Makefile.in turns the configure
variable into a preprocessor flag:
# "true" if the target C library headers are unavailable; "false"
# otherwise.
inhibit_libc = @inhibit_libc@
ifeq ($(inhibit_libc),true)
INHIBIT_LIBC_CFLAGS = -Dinhibit_libc
endif
Listing 3.2-5: from variable to flag (gcc/Makefile.in:789-794).
The comment states what the variable means: target C library headers. The rule
for libgcc.mvars writes INHIBIT_LIBC_CFLAGS into that file
(gcc/Makefile.in:2462-2466). libgcc's Makefile includes the file
(libgcc/Makefile.in:184) and puts the flag into the options for
libgcc2.a and for crtbegin.o and crtend.o
(:248-251, :302-306). libgcc never runs
the probe again. It uses the answer that gcc/ wrote.
Four targets do not ask. Their libgcc fragments set the flag themselves: BPF,
nvptx and AMD GCN assign INHIBIT_LIBC_CFLAGS = -Dinhibit_libc
(t-bpf:22-23, t-nvptx:15-18, t-amdgcn:22-23),
and AVR adds -Dinhibit_libc to its own flags (t-avr:129). libgcc's
Makefile includes the fragments after libgcc.mvars
(libgcc/Makefile.in:379-380), so their value wins.
gcc/tsystem.h: headers out, declarations in
Most of libgcc reaches the C library through one header, gcc/tsystem.h. The
unwinder and the emulated TLS code include it, for example
(libgcc/unwind-dw2-fde.c:28, libgcc/emutls.c:27):
#ifdef inhibit_libc
#ifndef malloc
extern void *malloc (size_t);
#endif
...
#ifndef abort
#define abort() __builtin_trap ()
#endif
...
#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. */
...
/* All systems have this header. */
#include <stdio.h>
Listing 3.2-6: tsystem.h, abridged (gcc/tsystem.h:47-95).
With inhibit_libc, the header declares malloc, calloc, realloc, free,
atexit, strlen, memcpy and memset itself, and turns abort () into a trap
instruction. Without it, the header includes <stdio.h>, <sys/types.h>,
<errno.h>, <string.h>, <stdlib.h>, <unistd.h> and <time.h>
(gcc/tsystem.h:85-118).
inhibit_libc removes the C library's headers from libgcc, not its calls. The
unwinder still calls malloc (libgcc/unwind-dw2-fde.c:171). The
declaration now comes from tsystem.h, and the definition must still come from a
C library when something links.
The code that inhibit_libc compiles out
Code that needs more than those declarations sits behind #ifndef inhibit_libc.
At the pin, these are the parts that a libgcc built with -Dinhibit_libc does
not have:
| Source | Without the C library's headers | What you lose |
|---|---|---|
libgcc/libgcc2.c:2975-2995 | __eprintf is not built | old objects that call __eprintf |
libgcc/unwind-dw2-fde-dip.c:49-84 | no dl_iterate_phdr search for unwind tables | the unwinder uses only frames registered at startup (:576-580) |
libgcc/config/*/linux-unwind.h, for example i386 | no signal-frame decoding | unwinding through a signal handler |
libgcc/generic-morestack.c:37-43 | the file compiles to nothing | -fsplit-stack |
libgcc/libgcov-driver.c:41-46, libgcov-merge.c:28-33 | gcov's runtime becomes empty stubs | .gcda output from --coverage |
libgcc/config/i386/heap-trampoline.c:3-4 (also AArch64) | the file compiles to nothing | -ftrampoline-impl=heap (common.opt:3121-3123) |
The split-stack file states the trade-off in its own words:
/* If inhibit_libc is defined, we cannot compile this file. The
effect is that people will not be able to use -fsplit-stack. That
is much better than failing the build particularly since people
will want to define inhibit_libc while building a compiler which
can build glibc. */
#ifndef inhibit_libc
Listing 3.2-7: the policy in one comment (libgcc/generic-morestack.c:37-43).
The gcov row needs one more sentence. By default the compiler adds a constructor
that calls __gcov_init and a destructor that calls __gcov_exit to each
--coverage object (gcc/coverage.cc:1195-1201, built at
:1069-1119). With
inhibit_libc, __gcov_init is an empty function, and __gcov_exit is not
defined at all, because its definition is inside the block that needs the C
library (libgcov-driver.c:66-68, :744-746). So the
source predicts an undefined __gcov_exit when such a program links. One gcov
entry point survives: __gcov_info_to_gcda, "intended for freestanding
environments which do not support the C library file I/O"
(libgcov-driver.c:787-792). It pairs with
-fprofile-info-section, which the GCC manual documents under
Instrumentation Options. That option replaces the constructor and
the destructor with a pointer to the profile data in a named section, for
freestanding environments.
One file goes the other way on purpose. crtstuff.c builds the same crtbegin.o
for a glibc target with or without inhibit_libc:
#if defined(OBJECT_FORMAT_ELF) \
&& !defined(OBJECT_FORMAT_FLAT) \
&& defined(HAVE_LD_EH_FRAME_HDR) \
&& !defined(CRTSTUFFT_O) \
&& defined(inhibit_libc) \
&& (defined(__GLIBC__) || defined(__gnu_linux__) || defined(__GNU__))
/* On systems using glibc, an inhibit_libc build of libgcc is only
part of a bootstrap process. Build the same crt*.o as would be
built with headers present, so that it is not necessary to build
glibc more than once for the bootstrap to converge. */
# define USE_PT_GNU_EH_FRAME
#endif
Listing 3.2-8: the glibc exception (libgcc/crtstuff.c:121-132).
The other USE_PT_GNU_EH_FRAME blocks above it require !defined(inhibit_libc)
(crtstuff.c:88-119). This one requires the opposite, so a
stage-1 crtbegin.o for Linux matches the final one.
What inhibit_libc does not decide: libgcc_s.so
The shared libgcc is a separate decision. libgcc builds it when shared libraries
are enabled and the target knows how to link one
(libgcc/Makefile.in:392-397). The link rule ends in -lc
(libgcc/config/t-slibgcc:29-37), and inhibit_libc does not change
that. A stage 1 that builds libgcc with no C library therefore turns shared
libraries off as well. Chapter 4.2 covers
libgcc_s.so.
Reading the answer from gcc/Makefile and libgcc.mvars
The observable for this chapter is weak, and it is fair to say so. inhibit_libc
is a configure variable, not a compiler option. No driver option prints it. You
read it from the build tree:
$ grep '^inhibit_libc' build/gcc/Makefile
$ grep '^INHIBIT_LIBC_CFLAGS' build/gcc/libgcc.mvars
The first command works as soon as gcc/ is configured. Listing 3.2-5 shows that
the line reads inhibit_libc = true or inhibit_libc = false. The second works
after make all-gcc, which builds libgcc.mvars through libgcc-support. Its
value is -Dinhibit_libc or nothing. These commands were not run for this
chapter, because no GCC build tree was available. The values above come from the
rules in Listings 3.2-1 and 3.2-5.
To predict the value before you build, find the probe file from the table in this chapter and test for it:
$ ls /opt/cross/sysroot/usr/include/stdio.h
Diagnosing inhibit_libc from the error message
| Symptom | Step | Cause |
|---|---|---|
fatal error: stdio.h: No such file or directory in a libgcc source | make all-target-libgcc | inhibit_libc is false, so tsystem.h includes <stdio.h>. Check both clauses. |
The directory (BUILD_SYSTEM_HEADER_DIR) that should contain system headers does not exist: | make all-gcc | inhibit_libc is false, and the sysroot has no usr/include (Listing 3.2-2). |
undefined __gcov_exit when a --coverage program links | your link | libgcov was built with inhibit_libc. |
a -fsplit-stack program does not link | your link | generic-morestack.c compiled to nothing (Listing 3.2-7). |
The first message comes from the preprocessor, which reports a missing include as
the file name and the system's error text
(libcpp/files.cc:1869-1874, libcpp/errors.cc:358-359).
The last two rows are predictions from the source, not reproduced failures.
A stage-2 compiler that builds but gives a libstdc++ with features missing is a different problem. Its cause is libstdc++'s configure, not this switch, and Chapter 3.3 covers it.
Three things to carry away
inhibit_libc is two clauses, and each flag answers one. --with-newlib, or a
cross build with no sysroot, makes the first true. --without-headers, or a
missing stdio.h, makes the second true.
--with-sysroot turns the first clause off. A hosted stage 1 with a sysroot
keeps inhibit_libc false despite --without-headers, unless you add
--with-newlib.
It removes headers from libgcc, not calls. libgcc still needs malloc and
memcpy at link time, and it loses -fsplit-stack, gcov output and signal-frame
unwinding.
Things that surprise people
The probe looks in <target>/sys-include, not <target>/include. A bare-metal
compiler with no sysroot, configured with --with-newlib, keeps inhibit_libc
true in its final stage unless stdio.h is in sys-include.
A misspelled --without-header is accepted in silence. Neither the top-level
configure.ac nor gcc/configure.ac declares --with-headers or
--with-newlib, so configure --help does not list them. Unknown options
produce no warning, because the top level turns option checking off
(configure.ac:36) and passes --disable-option-checking to every
subdirectory (:3644-3646).
The misspelled flag sets a variable that neither script reads.
--with-headers=DIR writes into your install prefix at configure time. The
copy happens in the top-level configure, before any make.
On BPF, nvptx, AMD GCN and AVR, libgcc always uses -Dinhibit_libc. The
target's libgcc fragment sets it, whatever configure decided.
Documentation coverage
The install manual's Configuration page documents each flag on its
own: --with-sysroot (gcc/doc/install.texi:2709), --with-build-sysroot
(:2733), --with-headers (:2751), --without-headers (:2763) and
--with-newlib (:2776). Its Building page tells you to provide the
target headers with --with-sysroot or --with-headers before you configure
(install.texi:3313-3322). The Instrumentation Options node documents
-fprofile-info-section for freestanding targets (gcc/doc/invoke.texi:17800).
What is not documented:
inhibit_libcitself. The name does not appear ingcc/doc/.- That
--without-headersand--with-newlibanswer the two halves of one condition. This is the fact that would prevent the failure at the start of this chapter. - That
--with-sysrootturns the first half off. - That configure probes for
stdio.h, and which directory it probes. The--with-headersentry namesprefix/target/sys-include. The code uses the exec prefix, which is the same directory unless you give--exec-prefix. - That the documented effect of
--with-newlib, omitting__eprintffromlibgcc.a(install.texi:2776-2780), happens only throughinhibit_libc. So it happens only when the second half is also true. - That libgcc built this way still calls
mallocand the other functions thattsystem.hdeclares. - That such a libgcc has no split-stack support, stub gcov functions and no signal-frame unwinding.
Next: the seven steps of a hosted cross bootstrap, and why its stage 1 stops at
make all-gcc even though inhibit_libc is false.
All source references in this chapter are to GCC 15.2.0
(releases/gcc-15.2.0). Line numbers in other releases will differ; the
surrounding code rarely does. Where behaviour itself changed across a major
version, it is flagged inline.