4.1 What libgcc is actually for
Source references in this chapter are to GCC 15.2.0 (releases/gcc-15.2.0).
Your firmware for a Cortex-M0 divides two long long values in one function. You
link it with -nostdlib and your own startup code, and the linker reports an
undefined reference to __aeabi_ldivmod. You add -lgcc and the link works.
Chapter 1.10 gave that fix and its reason
in one sentence: the compiler emitted the call when it compiled your code. It did
not show what is at the other end of the call, or why a compiler needs a library
at all.
libgcc is not a library that you call. It is the second half of GCC's code generator. When the target has no instruction for an operation, the compiler emits a call to a helper with a fixed name, and libgcc holds the body under that name. The compiler and libgcc agree on each name and on the machine mode of each argument. No header file and no other library takes part in that agreement.
This chapter assumes Chapter 1.10, for Listing 1.10-5 and the -lgcc that you
put back by hand, Chapter 1.7, for the -lgcc slots on
the link line, and Chapter 1.6, because each multilib
has its own libgcc.a.
Both halves from the outside: -S, -print-libgcc-file-name and nm
Start from Listing 1.10-5 and make the operands 64 bits wide:
$ printf 'long long div64 (long long a, long long b) { return a / b; }\n' > div64.c
$ arm-none-eabi-gcc -O2 -mcpu=cortex-m0 -S -o - div64.c | grep aeabi
$ lib=$(arm-none-eabi-gcc -mcpu=cortex-m0 -print-libgcc-file-name)
$ arm-none-eabi-nm -A "$lib" | grep -w -e __aeabi_ldivmod \
-e __gnu_ldivmod_helper -e __divdi3 -e __udivmoddi4
The first command shows the call that the compiler wrote into the assembly, to
__aeabi_ldivmod. The second asks the driver which libgcc.a it links for this
multilib. The GCC manual documents -print-libgcc-file-name under
Developer Options, as the same as -print-file-name=libgcc.a. The
nm -A line puts the archive member in front of each symbol. A T line is a
definition, and a U line is a call out of that member. The output is a small
call graph inside libgcc.
Without a cross compiler, run the same experiment on x86-64 with __int128:
$ printf '__int128 div128 (__int128 a, __int128 b) { return a / b; }\n' > div128.c
$ gcc -O2 -S -o - div128.c | grep divti3
$ nm -A "$(gcc -print-libgcc-file-name)" | grep -w __divti3
This chapter shows no output for these commands. Each name that it predicts comes from the source cited in the sections below.
The name is the contract: gen_libfunc and optabs.def
The compiler never reads libgcc. It knows each helper only by its name, and it makes that name with one rule:
/* Initialize the libfunc fields of an entire group of entries in some
optab. Each entry is set equal to a string consisting of a leading
pair of underscores followed by a generic operation name followed by
a mode name (downshifted to lowercase) followed by a single character
representing the number of operands for the given operation (which is
usually one of the characters '2', '3', or '4').
Listing 4.1-1: the naming rule, above gen_libfunc
(gcc/optabs-libfuncs.cc:137-142).
The row for signed division in optabs.def supplies the operation name and the
operand count, "div" and '3' (gcc/optabs.def:139). On a
32-bit target a long long is two words (gcc/defaults.h:508-510),
which is the 8-byte mode DImode (gcc/machmode.def:212). So a signed
long long division is __ + div + di + 3, which is __divdi3. The
internals manual lists the helpers by these mode names, and it says that they
"take arguments and return values of a specific machine mode, not a specific C
type" (gcc/doc/libgcc.texi:28-29).
The same row serves floating point. Its generator calls the float generator for a
float mode and the integer generator for an integer mode
(optabs-libfuncs.cc:315-324). So one row names __divdi3,
__divsf3 and __divdf3, and the soft-float routines are the same kind of
helper as the integer ones.
The integer generator does not name a helper for every integer mode:
int maxsize = 2 * BITS_PER_WORD;
int minsize = BITS_PER_WORD;
...
if (GET_MODE_BITSIZE (int_mode) < minsize
|| GET_MODE_BITSIZE (int_mode) > maxsize)
return;
gen_libfunc (optable, opname, suffix, int_mode);
Listing 4.1-2: integer helpers exist for one word and two words, abridged
(gcc/optabs-libfuncs.cc:187-205).
On a 32-bit target the range is 32 to 64 bits, which gives __divsi3 and
__divdi3. On a 64-bit target it is 64 to 128 bits, which gives __divdi3 and
__divti3. For the trapping optabs the floor drops to the size of int
(:198-201), so __addvsi3 exists on a 64-bit target too.
Which of these names the compiler uses for one statement, and when it uses an instruction instead, is decided by the optab lookup during expansion. Chapter 5.5 owns that decision. This chapter follows the name to the library side.
One file, one archive member per helper: libgcc2.c and -DL_divdi3
Most integer helpers are in one file, libgcc/libgcc2.c, and the build compiles
it once for each helper, with a different macro each time:
# Build lib2funcs. For the static library also include LIB2FUNCS_ST.
lib2funcs-o = $(patsubst %,%$(objext),$(lib2funcs) $(LIB2FUNCS_ST))
$(lib2funcs-o): %$(objext): $(srcdir)/libgcc2.c
$(gcc_compile) -DL$* -c $< $(vis_hide)
libgcc-objects += $(lib2funcs-o)
Listing 4.1-3: one compile of libgcc2.c per helper
(libgcc/Makefile.in:509-513).
lib2funcs is a list of stems such as _muldi3, _negdi2 and _lshrdi3
(Makefile.in:451-460). For the stem _divdi3, $* is
_divdi3, and the command defines L_divdi3. Each helper in libgcc2.c is in
its own #ifdef:
#ifdef L_divdi3
DWtype
__divdi3 (DWtype u, DWtype v)
{
...
w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
if (c)
w = -w;
return w;
}
#endif
Listing 4.1-4: __divdi3, abridged (libgcc/libgcc2.c:1209-1231).
The omitted lines make both operands positive and remember the sign.
So the member _divdi3.o holds only __divdi3, and a link takes that member
only when something calls __divdi3.
__divdi3 calls __udivmoddi4, but not the one in the member
_udivmoddi4.o. When the build compiles one of the division helpers, libgcc2.c
also defines L_udivmoddi4 (libgcc2.c:663-667), and it
compiles that copy of __udivmoddi4 as static inline with always_inline
(:989-993). Each division member carries its own copy. The
separate _udivmoddi4.o is for callers outside libgcc2.c, such as the Arm
routine later in this chapter.
The division helpers also have a list of their own, with this comment: "These
might cause a divide overflow trap and so are compiled with unwinder info"
(Makefile.in:476-480). By default they get -fexceptions -fnon-call-exceptions (:544-554). The Arm fragment removes the
second flag, because on Arm "specifying -fnon-call-exceptions will needlessly pull
in the unwinder in simple programs which use 64-bit division"
(libgcc/config/arm/t-bpabi:17-20). A helper is target code, like
your own code, and its compile flags decide what else it brings into your link.
Chapter 4.3 explains the unwinder.
__divdi3 is a macro: libgcc2.h and the 128-bit helpers
The name __divdi3 in Listing 4.1-4 is not always the symbol name. libgcc2.h
makes it from the word size:
#elif LIBGCC2_UNITS_PER_WORD == 4
#define W_TYPE_SIZE (4 * __CHAR_BIT__)
#define Wtype SItype
...
#define DWtype DItype
#define UDWtype UDItype
...
#define __NW(a,b) __ ## a ## si ## b
#define __NDW(a,b) __ ## a ## di ## b
...
#define __divdi3 __NDW(div,3)
Listing 4.1-5: names made from the word size, abridged
(libgcc/libgcc2.h:219-233 and :286-288).
DWtype is the double-word integer type, and __NDW pastes the double-word mode
into the name. With a 4-byte word, __divdi3 stays __divdi3. With an 8-byte
word, the branch above this one makes DWtype a TItype and pastes ti
(libgcc2.h:203-217). The same function then compiles as __divti3.
libgcc2.c takes the word size from the target's MIN_UNITS_PER_WORD
(libgcc2.c:38-52).
The 128-bit helpers of a 64-bit target are the 64-bit helpers of a 32-bit
target, compiled with a wider word. This matches Listing 4.1-2: the compiler
names helpers for one word and two words, and libgcc2.h builds those two.
The conversions between integer and float modes, such as __floatdidf, work the
same way (Makefile.in:461-474, libgcc2.h:299-314).
With an 8-byte word, __floatdidf compiles as __floattidf.
The float modes come from the compiler that builds libgcc. The build passes
-fbuilding-libgcc (Makefile.in:248-251), an option that
c.opt marks Undocumented (gcc/c-family/c.opt:1707-1708). With
it, the compiler predefines macros such as __LIBGCC_HAS_DF_MODE__
(c-cppbuiltin.cc:1380-1400), and libgcc2.h reads them
(libgcc2.h:40-68).
Chapter 2.6 explains how target selection
fixes predefined macros. libgcc does not probe the target. It asks its compiler.
The
_BitInthelpers are new in GCC 14._mulbitint3and_divmodbitint4are on the lists atreleases/gcc-14.1.0(libgcc/Makefile.in:455and:476), and neither is inlibgcc/Makefile.inatreleases/gcc-13.3.0.
__aeabi_*: arm_init_libfuncs and DECLARE_LIBRARY_RENAMES
The Arm EABI does not use GCC's names. Its run-time ABI document gives its own, and GCC follows that document in both halves.
The compiler side is a target hook. The internals manual documents
TARGET_INIT_LIBFUNCS in Implicit Calls to Library Routines: it
"should declare additional library routines or rename existing ones"
(gcc/doc/tm.texi:5866-5874). init_optabs calls it after
it sets the default names (optabs-libfuncs.cc:905). Arm's hook
cites its source:
/* There are no special library functions unless we are using the
ARM BPABI. */
if (!TARGET_BPABI)
return;
/* The functions below are described in Section 4 of the "Run-Time
ABI for the ARM architecture", Version 1.0. */
...
set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
/* For SImode division the ABI provides div-without-mod routines,
which are faster. */
set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
Listing 4.1-6: arm_init_libfuncs, abridged
(gcc/config/arm/arm.cc:2573-2666).
arm-none-eabi includes arm/bpabi.h (gcc/config.gcc:1503),
and there TARGET_BPABI is true for every AAPCS-based configuration
(gcc/config/arm/bpabi.h:30). So the division in div64.c
becomes __aeabi_ldivmod, not __divdi3. A divmod routine can serve a plain
division because, as the comment above the sdiv_optab lines says, "The quotient
is returned in the usual location" (arm.cc:2651-2654).
The library side must then define every __aeabi_ name. It does so in three
ways.
An alias for a C helper. config.host adds arm/bpabi-lib.h to libgcc's
target headers for Arm EABI (libgcc/config.host:580). That header
gives some libgcc2.c helpers a second name:
/* Make __aeabi_AEABI_NAME an alias for __GCC_NAME. */
#define RENAME_LIBRARY(GCC_NAME, AEABI_NAME) \
typeof (__##GCC_NAME) __aeabi_##AEABI_NAME \
__attribute__((alias ("__" #GCC_NAME)));
/* Give some libgcc functions an additional __aeabi name. */
#ifdef L_muldi3
#define DECLARE_LIBRARY_RENAMES RENAME_LIBRARY (muldi3, lmul)
#endif
Listing 4.1-7: the libgcc half of a rename
(libgcc/config/arm/bpabi-lib.h:26-34).
libgcc2.c expands DECLARE_LIBRARY_RENAMES near its top
(libgcc2.c:57-59), so _muldi3.o defines __muldi3 and
__aeabi_lmul at one address. The internals manual documents the macro in the
same node (tm.texi:5857-5862).
An alias in assembly. The 32-bit division is hand-written in
libgcc/config/arm/lib1funcs.S, and the alias is one directive:
#ifdef L_divsi3
#if defined(__prefer_thumb__)
FUNC_START divsi3
FUNC_ALIAS aeabi_idiv divsi3
Listing 4.1-8: __aeabi_idiv is __divsi3
(libgcc/config/arm/lib1funcs.S:1227-1232).
On a core with a divide instruction, the same routine is one sdiv
(lib1funcs.S:1282-1289).
A rename before compilation. On a Thumb-1 multilib, the soft-float routines
come from the C soft-fp code. Arm's sfp-machine.h defines __adddf3 as
__aeabi_dadd, and the other names in the same way, before that code compiles
(libgcc/config/arm/sfp-machine.h:70-110). In that build the
routine has only the Arm name.
Some __aeabi_ helpers have no GCC twin. __aeabi_ldivmod returns the quotient
and the remainder together, so it is its own assembly routine. lib1funcs.S
includes bpabi.S for the 32-bit instruction sets and bpabi-v6m.S for Thumb-1
(lib1funcs.S:2255-2264). The bpabi.S version calls
__udivmoddi4 (bpabi.S:184-194). The bpabi-v6m.S version calls
__gnu_ldivmod_helper (bpabi-v6m.S:126-135), a C function that
calls __divdi3 (libgcc/config/arm/bpabi.c:31-41). Both
versions first test for a zero divisor and branch to __aeabi_ldiv0
(bpabi.S:104). That is the call graph of the first section. When
the driver picks a Thumb-1 multilib for -mcpu=cortex-m0, one long long
division takes at least _aeabi_ldivmod.o, bpabi.o and _divdi3.o into the
link, plus the member that defines __aeabi_ldiv0.
Both halves follow a document outside GCC, Arm's run-time ABI. The comment in Listing 4.1-6 is the only place where GCC names it.
How a target picks its helpers: config.host and LIB1ASMFUNCS
libgcc has its own config.gcc. libgcc/config.host maps the target triple to a
list of makefile fragments, tmake_file. Its header says that it "bears an
obvious resemblance to gcc/config.gcc" (libgcc/config.host:26-30).
libgcc is built for the target, so this file switches on ${host}, and that
host is GCC's target. The entry for arm-none-eabi:
arm*-*-eabi* | arm*-*-symbianelf* | arm*-*-rtems*)
tmake_file="${tmake_file} arm/t-arm arm/t-elf t-fixedpoint-gnu-prefix"
tm_file="$tm_file arm/bpabi-lib.h"
case ${host} in
arm*-*-eabi* | arm*-*-rtems*)
tmake_file="${tmake_file} arm/t-bpabi arm/t-sync t-crtfm"
extra_parts="crtbegin.o crtend.o crti.o crtn.o"
;;
...
esac
tmake_file="$tmake_file t-softfp-sfdf t-softfp-excl arm/t-softfp t-softfp"
extra_parts="$extra_parts crtfastmath.o"
unwind_header=config/arm/unwind-arm.h
;;
Listing 4.1-9: the helper set for Arm EABI, abridged
(libgcc/config.host:578-595).
Each fragment sets a few variables. LIB1ASMFUNCS lists helpers written in
assembly, in the one file that LIB1ASMSRC names
(libgcc/config/arm/t-arm:1-3). LIB2ADD adds whole files, such as
bpabi.c (t-bpabi:5-6). LIB2FUNCS_EXCLUDE removes helpers. The
softfp_* variables choose the soft-float routines
(t-softfp:19-61). The generic makefile then removes from the C list
everything that a fragment supplies another way:
# Remove any objects from lib2funcs and LIB2_DIVMOD_FUNCS that are
# defined as optimized assembly code in LIB1ASMFUNCS or as C code
# in LIB2FUNCS_EXCLUDE.
lib2funcs := $(filter-out $(LIB2FUNCS_EXCLUDE) $(LIB1ASMFUNCS),$(lib2funcs))
LIB2_DIVMOD_FUNCS := $(filter-out $(LIB2FUNCS_EXCLUDE) $(LIB1ASMFUNCS), \
$(LIB2_DIVMOD_FUNCS))
Listing 4.1-10: assembly replaces C (libgcc/Makefile.in:482-487).
arm/t-elf puts _divsi3, _udivsi3, the 64-bit shifts and _clzsi2 into
LIB1ASMFUNCS (t-elf:21-29). So on Arm those helpers come from
lib1funcs.S, and libgcc2.c is not compiled for them.
GCC keeps no fixed list of required helpers. Every target gets the generic
libgcc2.c list, less what its fragments replace or exclude. Soft-float,
fixed-point and decimal float helpers come only from fragments that the target
names. The requirement is per target: libgcc must define every name that the
compiler can emit for that configuration. For Arm, the compiler half of that
list is in arm.cc, and the library half is in t-bpabi, t-elf and
bpabi-lib.h.
The soft-float code has an owner outside GCC. Its README says: "The files in this
directory are part of the GNU C Library, not part of GCC"
(libgcc/soft-fp/README:1-4). Each file is one routine, such as
__adddf3 (soft-fp/adddf3.c:32-49).
Helpers from builtins and options: __popcountsi2, __addvsi3, __muldc3
Arithmetic operators are not the only source of helper calls. Some builtins and some options produce them as well:
| You write | Helper | Compiler side | libgcc side |
|---|---|---|---|
__builtin_popcount (x) | __popcountsi2 and relatives | builtins.cc:8203-8207, optabs.def:188 | libgcc2.c:819-821 |
a + b with -ftrapv | __addvsi3 and relatives | optabs-tree.cc:256-262, optabs-libfuncs.cc:280-294 | libgcc2.c:74-84 |
a * b on _Complex double | __muldc3 | tree.cc:10225-10266, tree-complex.cc:1178-1188 | libgcc2.c:2694-2698 |
__builtin_powi (x, n) | __powidf2 | optabs.def:204 | libgcc2.c:2557-2588 |
The -ftrapv row is a helper that changes what your program does. The GCC manual
documents -ftrapv under Code Gen Options: it "generates traps
for signed overflow on addition, subtraction, multiplication operations". For a
signed type, -ftrapv makes the compiler choose addv_optab and its relatives,
whose library names have a v after the operation. The helper calls abort when
the addition overflows:
#ifdef L_addvsi3
Wtype
__addvSI3 (Wtype a, Wtype b)
{
Wtype w;
if (__builtin_add_overflow (a, b, &w))
abort ();
return w;
}
Listing 4.1-11: __addvsi3 (libgcc/libgcc2.c:74-84).
So the "trap" from the manual can be a call into libgcc, and from there a call
into the C library. The internals manual says the same about the whole trapping
family: "These functions call the libc function abort upon signed arithmetic
overflow" (libgcc.texi:145-149).
The complex row needs no option. C uses the C99 rules for complex
multiplication by default (gcc/c-family/c-opts.cc:230-232).
Under those rules, at -O0 or when optimising for size, a complex
multiplication of two variables is a call to __muldc3
(tree-complex.cc:1178-1188).
Where libgcc needs the C library: tsystem.h and inhibit_libc
Part III depends on one claim: GCC needs a C library to finish building itself. Chapter 3.1 states the cycle. This section shows where the claim is true in libgcc's source.
libgcc2.c includes tsystem.h as its second line of code
(libgcc2.c:26-27). The file is in gcc/, and libgcc finds
it through -I$(srcdir)/../gcc (Makefile.in:285-287). It begins
with a switch:
#ifdef inhibit_libc
...
#else /* ! inhibit_libc */
/* We disable this when inhibit_libc, so that gcc can still be built without
needing header files first. */
...
/* 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 4.1-12: the C library headers in every helper, abridged
(gcc/tsystem.h:47-118).
Every helper compiled from libgcc2.c reads the target's <stdio.h>,
<stdlib.h> and <unistd.h>, including __divdi3, which calls nothing in the
C library. The arithmetic does not need the headers. One #include at the top
of a shared file does. Without the target's C library headers, the compile of
_divdi3.o fails, unless the build defines inhibit_libc. In that case
tsystem.h declares by hand the few functions that libgcc uses, and it turns
abort () into __builtin_trap () (tsystem.h:47-84).
LIBGCC2_CFLAGS carries the switch as $(INHIBIT_LIBC_CFLAGS)
(Makefile.in:248-251), which gcc/Makefile.in sets to
-Dinhibit_libc (gcc/Makefile.in:791-794). The condition that
sets it, and how the value travels from gcc/ to libgcc, belong to
Chapter 3.2. Beyond the headers, a few pieces of
libgcc use the C library itself, and each has its own guard:
| Piece | What it uses from the C library | With inhibit_libc | Source |
|---|---|---|---|
__addvsi3 and the trapping family | abort | __builtin_trap | Listing 4.1-11 |
__eprintf, in libgcc.a only | fprintf, fflush, abort | not compiled | libgcc2.c:2975-2995 |
unwinder lookup through PT_GNU_EH_FRAME | <link.h>, dl_iterate_phdr | registered frames only | unwind-dw2-fde-dip.c:49-89, :569, :576-580 |
-fsplit-stack support | <signal.h>, <sys/mman.h> and more | file compiles to nothing | generic-morestack.c:37-52 |
The last row carries a comment that states the whole trade:
/* 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. */
Listing 4.1-13: why a stage-1 libgcc is smaller
(libgcc/generic-morestack.c:37-43).
One more call into the C library depends on the target, not on inhibit_libc.
On Arm, division by zero ends in __aeabi_idiv0. On bare metal that is a weak
function that only returns (lib1funcs.S:1459-1468). For
arm*-linux-gnueabi*, t-linux-eabi swaps in a version that calls raise with
SIGFPE (libgcc/config/arm/t-linux-eabi:1-2,
lib1funcs.S:1477-1499). So libgcc can need libc at link time as
well as at build time. Chapter 1.7 shows how the -lgcc that follows -lc on
the link line resolves those calls.
What libgcc leaves to others: memcpy and __atomic_load_16
Not every call that the compiler invents goes to libgcc. The internals manual
says: "GCC will also generate calls to C library routines, such as memcpy and
memset, in some cases" (libgcc.texi:20-23). The GCC manual
documents the full rule in Standards: "GCC requires the
freestanding environment provide memcpy, memmove, memset and memcmp"
(gcc/doc/standards.texi:190-198). So -nostdlib -lgcc is not
all that you owe on bare metal. Those four functions are also on the list.
Atomic operations are the same shape of problem with a third library. When the
target has no instruction for an atomic load of some size, the builtin falls
through to a normal call (builtins.cc:8732-8741 and
:9068-9070). The callee has a name such as __atomic_load_16
(gcc/sync-builtins.def:309-310). libgcc does not define it.
libatomic does (libatomic/libatomic.map:63). The generic
driver never adds -latomic: neither gcc/gcc.cc nor gcc/configure.ac
mentions libatomic at the pin. A few target specs add it, such as RTEMS
(gcc/config/rtems.h:52). Elsewhere, a program that needs an
atomic call links only when you add -latomic yourself.
Why proprietary code can link it: COPYING.RUNTIME
libgcc's files are under the GPL version 3 with an extra permission, the GCC
Runtime Library Exception in the top-level COPYING.RUNTIME
(libgcc2.c:17-19). The exception states its purpose: "When
you use GCC to compile a program, GCC may combine portions of certain GCC header
files and runtime libraries with the compiled program"
(COPYING.RUNTIME:16-20). It lets you convey the result "under
terms of your choice", if "all Target Code was generated by Eligible Compilation
Processes" (:61-66). A compilation by GCC, alone or with
GPL-compatible software, is eligible. A compilation in which non-GPL-compatible
software optimises GCC's intermediate representation is not
(:53-57). The exception exists because of this chapter's
claim: the compiler puts libgcc into programs whose authors never asked for it.
The calls that this chapter followed from the outside start inside cc1, where
expansion looks for an instruction and falls back to the name.
Chapter 5.5 shows that lookup on an integer division like
the one in div64.c.
Three things to carry away
libgcc is the second half of the code generator. When the target has no instruction, the compiler emits a call by name, and libgcc holds the body under that name.
A helper's name is operation, mode and operand count, unless the target renames
it. __divdi3 is division in DImode with three operands. Arm renames it in both
halves, in arm_init_libfuncs and in bpabi-lib.h.
libgcc needs the C library headers to build, even for helpers that never call
the C library. tsystem.h includes <stdio.h> unless inhibit_libc is set,
and that is where the bootstrap cycle bites.
Things that surprise people
On a 64-bit target, the __divdi3 in libgcc2.c is __divti3 in the
library. libgcc2.h makes the name from the word size.
-ftrapv can end in the C library's abort. The trapping helpers call
abort. Only an inhibit_libc build replaces it with __builtin_trap.
On a Thumb-1 multilib, nm finds __aeabi_dadd but no __adddf3.
sfp-machine.h renames the soft-fp routine before it compiles.
On Arm GNU/Linux, a division by zero inside libgcc calls raise. The
bare-metal __aeabi_idiv0 only returns.
Documentation coverage
The internals manual has a chapter on libgcc,
The GCC low-level runtime library (gcc/doc/libgcc.texi:6). It
lists the helpers by mode name in
Routines for integer arithmetic (:48) and
Routines for floating point emulation (:266), with sections for
decimal float (:561), fixed point (:883) and miscellaneous routines (:2341).
Its section on exception handling routines (:2305) is a list of names
under the text "document me!" (:2308). The target side is in
Implicit Calls to Library Routines (gcc/doc/tm.texi:5849), with
DECLARE_LIBRARY_RENAMES (:5857), TARGET_INIT_LIBFUNCS (:5866) and
TARGET_LIBFUNC_GNU_PREFIX (:5876).
The GCC manual explains why -nostdlib usually needs -lgcc under
Link Options (gcc/doc/invoke.texi:19245-19259). It documents
-print-libgcc-file-name under Developer Options (:21611), -ftrapv
under Code Gen Options (:19680), and the four functions that a
freestanding environment must supply under Standards
(gcc/doc/standards.texi:190-198).
What is not documented:
- The rule that builds a helper name from the operation, the mode and the operand count, and that integer helpers exist only for one word and two words.
- That
libgcc2.cis compiled once per helper with-DL_name, and that its__divdi3becomes__divti3on a 64-bit target. - That each division helper carries a private inline copy of
__udivmoddi4. - The libgcc fragment variables that select the helpers:
LIB1ASMFUNCS,LIB1ASMSRC,LIB2ADDandLIB2FUNCS_EXCLUDE. The internals manual's Target Makefile Fragments node documentsLIB2FUNCS_EXTRAinstead (gcc/doc/fragments.texi:46-49), and neitherlibgcc/Makefile.innorgcc/Makefile.inreads that variable at the pin. - That every
libgcc2.chelper includes the target's C library headers throughtsystem.h, unlessinhibit_libcis set. - That the trap from
-ftrapvis a libgcc call that callsabort. The internals manual says the helpers callabort, but the GCC manual's text for-ftrapvdoes not say that a library is involved. - The three ways that Arm's libgcc provides the
__aeabi_names. -fbuilding-libgcc, whichc.optmarksUndocumented.- The Runtime Library Exception. It is in
COPYING.RUNTIME, not in the manuals.
Two places in the manuals mislead. The -nostdlib text sends the reader to an
internals node, "Interfacing to GCC Output" (gcc/doc/invoke.texi:19249), and
no file in gcc/doc at the pin defines that node. The prototypes in the libgcc
chapter use an illustrative mapping in which long long is TImode
(libgcc.texi:30-38). On a 32-bit target a long long is
DImode, so read the mode in the helper's name, not the C type in the prototype.
Next: the static archive, the exception archive and the shared library that libgcc is built into, and how the driver chooses between them.
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.