4.2 Which libgcc you link: libgcc.a, libgcc_eh.a and libgcc_s.so

Source references in this chapter are to GCC 15.2.0 (releases/gcc-15.2.0).

Link one object file twice on a GNU/Linux machine, once with gcc and once with g++, and compare the -### output. The C link asks for -lgcc and then for -lgcc_s inside an --as-needed bracket. The C++ link asks for -lgcc_s first and -lgcc after it. You gave no libgcc option to either driver. Now give -shared-libgcc to arm-none-eabi-gcc. The driver accepts it, and the link line does not change.

libgcc is built as up to three files, and configure and the language driver decide which of them your link gets. libgcc.a holds code that each linked object may copy. libgcc_eh.a and libgcc_s.so.1 hold the code that must exist once per process. The libgcc spec chooses between them. Its default depends on the target, on the linker that configure found, and on which driver you ran.

This chapter assumes Chapter 1.3, for the libgcc spec and its startup rewrite (Listing 1.3-5), and Chapter 1.7, for where %G sits on the link line. Chapter 4.1 says why the compiler calls into libgcc at all. This chapter is about which file each call resolves against.

The partition: libgcc-objects, LIB2ADDEH and one copy per process

libgcc/Makefile.in builds three lists of objects: libgcc-objects for libgcc.a, libgcc-eh-objects for libgcc_eh.a and libgcc-s-objects for libgcc_s.so.1. With a shared libgcc, most helpers go into two of the lists, because the makefile compiles each one twice from the same source:

# 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)

ifeq ($(enable_shared),yes)
lib2funcs-s-o = $(patsubst %,%_s$(objext),$(lib2funcs))
$(lib2funcs-s-o): %_s$(objext): $(srcdir)/libgcc2.c
	$(gcc_s_compile) -DL$* -c $<
libgcc-s-objects += $(lib2funcs-s-o)
endif

Listing 4.2-1: one helper, two objects (libgcc/Makefile.in:509-520).

The two compiles differ in two flags. The _s object gets -DSHARED (Makefile.in:321). The archive object gets $(vis_hide), which configure sets to -fvisibility=hidden -DHIDE_EXPORTS when the compiler supports hidden visibility (libgcc/configure.ac:549-554). libgcc2.h and unwind-generic.h restore default visibility only when HIDE_EXPORTS is absent (libgcc2.h:28-30, unwind-generic.h:37-39).

So the archive copy of each helper in Listing 4.2-1 is a hidden symbol. An executable or a shared library that takes the helper from the archive gets a private copy, and no other object can bind to it. For an arithmetic helper that is harmless. The archive also has members that the shared library does not have. LIB2FUNCS_ST and LIB2ADD_ST are the lists for the static library only (Makefile.in:266-278), and Chapter 4.1 says which helpers they hold.

The exception-handling sources follow a different rule:

# Build LIB2ADDEH, LIB2ADDEHSTATIC, and LIB2ADDEHSHARED.  If we don't have
# libgcc_eh.a, only LIB2ADDEH matters.  If we do, only LIB2ADDEHSTATIC and
# LIB2ADDEHSHARED matter.  (Usually all three are identical.)

c_flags := -fexceptions

ifeq ($(enable_shared),yes)

libgcc-eh-objects += $(addsuffix $(objext),$(basename $(notdir $(LIB2ADDEHSTATIC))))
libgcc-s-objects += $(addsuffix _s$(objext),$(basename $(notdir $(LIB2ADDEHSHARED))))

iter-items := $(sort $(LIB2ADDEHSTATIC) $(LIB2ADDEHSHARED))
-include $(iterator)

else
# Not shared.  LIB2ADDEH are added to libgcc.a.

libgcc-objects += $(addsuffix $(objext),$(basename $(notdir $(LIB2ADDEH))))

iter-items := $(LIB2ADDEH)
-include $(iterator)

endif

Listing 4.2-2: where the unwinder goes (libgcc/Makefile.in:867-889).

LIB2ADDEH defaults to the DWARF and SJLJ unwinders and unwind-c.c (Makefile.in:346-350), and targets replace it. Arm EABI, for example, puts its own unwinder there (libgcc/config/arm/t-bpabi:10-12). With a shared libgcc, these objects never enter libgcc.a. They go to libgcc_eh.a for static links and to libgcc_s.so.1 for dynamic ones. Without a shared libgcc, they go into libgcc.a, and there is only one file.

The reason is state. The unwinder keeps the frame tables that objects register in static variables: registered_frames when the target has the atomic fast path (libgcc/unwind-dw2-fde.c:50-51), and the unseen_objects and seen_objects lists when it does not (:77-78). A hidden copy in each shared library is a separate set of those variables. The makefile states the rule itself, for a file that has nothing to do with exceptions:

# While emutls.c has nothing to do with EH, it is in LIB2ADDEH*
# instead of LIB2ADD because that's the way to be sure on some targets
# (e.g. *-*-darwin*) only one copy of it is linked.
LIB2ADDEH += $(srcdir)/emutls.c
LIB2ADDEHSTATIC += $(srcdir)/emutls.c
LIB2ADDEHSHARED += $(srcdir)/emutls.c

Listing 4.2-3: the EH lists mean "one copy" (libgcc/Makefile.in:443-448).

That gives three files on a target with a shared libgcc:

FileContainsIts symbols are
libgcc.athe helpers, plus the static-only membershidden, for most members
libgcc_eh.athe LIB2ADDEH set: unwinder, unwind-c.c, emutls.chidden
libgcc_s.so.1the helpers, plus the LIB2ADDEH setexported by a version script

The objects in libgcc_eh.a get $(vis_hide) too (libgcc/shared-object.mk:13-17). A shared library linked with -lgcc_eh therefore has its own private unwinder.

libgcc_s.so.1 does not export everything it contains. A version script decides. libgcc/mkmap-symver.awk writes that script at build time from two inputs: the nm output of the _s objects, and the map files the target lists, starting with libgcc-std.ver (Makefile.in:982-997). The script exports a symbol only if an object defines it and a map file lists it (mkmap-symver.awk:119). Everything else falls under local: * (:139). The map file has version nodes from GCC_3.0, which holds the integer helpers and the unwinder entry points, to GCC_14.0.0 (libgcc/libgcc-std.ver.in:19 and :1949-1962). The soname is libgcc_s.so.1 (libgcc/config/t-slibgcc:21-24).

Two details of that link matter. The shared library is linked from its _s objects and from libgcc.a (Makefile.in:1003-1015). So a helper that only the archive has can still go into libgcc_s.so.1, as a hidden copy that it does not export. And the link line ends with $(SHLIB_LC), which t-slibgcc sets to -lc (t-slibgcc:29 and :34-37). So on a target that keeps that setting, libgcc_s.so.1 cannot be linked before a C library exists. Chapter 3.1 owns that consequence for a bootstrap.

init_gcc_specs: the two forms of the libgcc spec

Chapter 1.3 showed the driver walk the libgcc spec at startup and replace each -lgcc it finds (Listing 1.3-5). The call passes three names: -lgcc_s as the shared name, -lgcc as the static name and -lgcc_eh as the EH name (gcc/gcc.cc:1931-1947). Other text in the target's spec is copied unchanged (:1967-1972). The function that builds the replacement is where the choice is made:

static void
init_gcc_specs (struct obstack *obstack, const char *shared_name,
		const char *static_name, const char *eh_name)
{
  char *buf;

#if USE_LD_AS_NEEDED
  buf = concat ("%{static|static-libgcc|static-pie:", static_name, " ", eh_name, "}"
		"%{!static:%{!static-libgcc:%{!static-pie:"
		"%{!shared-libgcc:",
		static_name, " " LD_AS_NEEDED_OPTION " ",
		shared_name, " " LD_NO_AS_NEEDED_OPTION
		"}"
		"%{shared-libgcc:",
		shared_name, "%{!shared: ", static_name, "}"
		"}}"
#else
  buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
		"%{!static:%{!static-libgcc:"
		"%{!shared:"
		"%{!shared-libgcc:", static_name, " ", eh_name, "}"
		"%{shared-libgcc:", shared_name, " ", static_name, "}"
		"}"
#ifdef LINK_EH_SPEC
		"%{shared:"
		"%{shared-libgcc:", shared_name, "}"
		"%{!shared-libgcc:", static_name, "}"
		"}"
#else
		"%{shared:", shared_name, "}"
#endif
#endif
		"}}", NULL);

  obstack_grow (obstack, buf, strlen (buf));
  free (buf);
}

Listing 4.2-4: the two forms (gcc/gcc.cc:1815-1851).

Both forms agree on the explicit cases. -static and -static-libgcc give -lgcc -lgcc_eh: the archive and the static unwinder. -shared-libgcc gives -lgcc_s -lgcc for an executable and -lgcc_s alone for a shared library. The forms differ when you give none of these options.

The as-needed form gives -lgcc --as-needed -lgcc_s --no-as-needed, for executables and shared libraries alike. Chapter 1.3 worked out that expansion. The partition explains why it works. The linker reads libgcc.a first and takes from it every helper the link needs. The references left for libgcc_s.so are the ones the archive cannot resolve. On a shared-libgcc target, those are the references into the LIB2ADDEH set. Configure describes LD_AS_NEEDED_OPTION as "the linker option to ignore unused dependencies" (gcc/configure.ac:6620-6623). So for a program that never calls into the unwinder, the linker records no dependency on libgcc_s.so.1. A program that does call into it gets the one shared copy, not a private one.

The older form gives -lgcc -lgcc_eh for an executable, so the executable gets a static unwinder. For -shared, it gives -lgcc if the target defines LINK_EH_SPEC and -lgcc_s if it does not. With -lgcc alone, the shared library carries no unwinder, because on a shared-libgcc target the unwinder is not in libgcc.a. A reference to _Unwind_Resume stays undefined in the file the linker writes.

Neither form looks at your objects. A comment above the rewrite lists heuristics that GCC could use, and does not:

  /* ??? If neither -shared-libgcc nor --static-libgcc was
     seen, then we should be making an educated guess.  Some proposed
     heuristics for ELF include:

	(1) If "-Wl,--export-dynamic", then it's a fair bet that the
	    program will be doing dynamic loading, which will likely
	    need the shared libgcc.
	...
     Doing any of this probably requires invoking an external program to
     do the actual object file scanning.  */

Listing 4.2-5: the guess GCC does not make, abridged (gcc/gcc.cc:1894-1920).

The default comes from flags and configuration, never from the objects you link. The as-needed form comes close to "shared if the link needs the unwinder". It gets there by giving that decision to the linker.

USE_LD_AS_NEEDED defaults to 0 (gcc.cc:1811-1813), so a target header must turn it on. For GNU/Linux, gnu-user.h does that when configure found a linker that accepts --as-needed. The same header defines LINK_EH_SPEC when the linker accepts --eh-frame-hdr:

#if defined(HAVE_LD_EH_FRAME_HDR)
#define LINK_EH_SPEC "%{!static|static-pie:--eh-frame-hdr} "
#endif
...
/* Use --as-needed -lgcc_s for eh support.  */
#ifdef HAVE_LD_AS_NEEDED
#define USE_LD_AS_NEEDED 1
#endif

Listing 4.2-6: the GNU/Linux settings, abridged (gcc/config/gnu-user.h:108-110 and :119-122).

Both HAVE_ macros come from tests of the linker that GCC's configure found: --eh-frame-hdr at gcc/configure.ac:6098-6127, and --as-needed at :6560-6624. The second test also picks --push-state --as-needed and --pop-state for GNU ld 2.28 or later, as Chapter 1.3 noted. Other targets set USE_LD_AS_NEEDED in their own headers, for example Solaris (sol2.h:410-413) and Alpha GNU/Linux (alpha/linux.h:116-119). A target whose headers never set it gets the older form, whatever its linker supports.

LINK_EH_SPEC has two jobs. First, the driver prepends it to the link spec (gcc.cc:1995-1998). The linker then gets --eh-frame-hdr for every link except a plain -static one. That option makes the linker write a PT_GNU_EH_FRAME header. The unwinder uses the header with dl_iterate_phdr to find an object's frame tables "to avoid register/deregister calls" (libgcc/unwind-dw2-fde-dip.c:25-27). Chapter 4.3 covers that lookup. The second job is in Listing 4.2-4. In the older form, LINK_EH_SPEC is what lets gcc -shared default to -lgcc. A shared library with no unwinder of its own can still be unwound through. It does not have to register its tables with an unwinder, because the unwinder finds them through the header.

The GCC manual documents this second job under Link Options. It says that GCC links shared libraries with the static libgcc by default if the GNU linker supports --eh-frame-hdr. Otherwise GCC uses the shared one. That is the older form. On a GNU/Linux target where configure also found --as-needed, the as-needed form applies instead, and the manual does not describe it.

t-slibgcc and --enable-shared: whether there is a choice at all

None of Listing 4.2-4 is compiled unless ENABLE_SHARED_LIBGCC is defined and REAL_LIBGCC_SPEC is not (gcc.cc:1809 and :1893). The macro comes from the command that compiles the driver:

  $(if $(SHLIB),$(if $(filter yes,@enable_shared@),-DENABLE_SHARED_LIBGCC)) \

Listing 4.2-7: ENABLE_SHARED_LIBGCC in DRIVER_DEFINES (gcc/Makefile.in:2611).

SHLIB is set by gcc/config/t-slibgcc, a fragment whose only line of make is SHLIB = true (gcc/config/t-slibgcc:1-2). So there are two conditions:

  1. The target's entry in gcc/config.gcc adds t-slibgcc to tmake_file. The generic GNU/Linux entry does (config.gcc:888-895). Bare-metal entries such as arm*-*-eabi* do not (config.gcc:1497-1530).
  2. enable_shared is yes. That is the default, and --disable-shared turns it off.

libgcc makes the same decision on its own side. Its configure parses --enable-shared with the same code as gcc/configure.ac (libgcc/configure.ac:52-69 and gcc/configure.ac:1051-1068). Its makefile then falls back to static only if no fragment defined SHLIB_LINK (libgcc/Makefile.in:392-397). The generic GNU/Linux entry of libgcc/config.host adds the fragment that defines it, t-slibgcc (config.host:313-315). The arm*-*-eabi* entry does not (config.host:578-595).

The install manual documents --enable-shared under Configuration, including the form that takes a list of packages and the package name libgcc, also spelled gcc. The code shows what the list form does to libgcc. Both configure scripts start from enable_shared=no and set yes only if the list names gcc or libgcc. So --enable-shared=libstdc++ builds a static-only libgcc, and the driver has no libgcc_s to choose.

On a target without a shared libgcc, the libgcc spec keeps the target's LIBGCC_SPEC, which defaults to -lgcc (gcc.cc:878-889). The driver accepts -static-libgcc and -shared-libgcc on every target, and its comment says that gcc.cc "understands the first two" (gcc.cc:4664-4677). But in gcc.cc, only the text that init_gcc_specs builds tests them. A target without a shared libgcc does not build that text, so the two options do nothing unless a target header tests them. The arm-none-eabi headers do not. The manual states the result under Link Options: "these options have no effect".

Two target families skip the rewrite. They define REAL_LIBGCC_SPEC, which the driver uses unmodified. Cygwin's spec gives -lgcc_s -lgcc to every link without -static or -static-libgcc (config/i386/cygwin.h:63-76). Darwin chooses by flags such as -fexceptions and by the deployment target (config/darwin.h:559-568), under a long comment about the system unwinder (:528-558).

Who adds -shared-libgcc: g++spec.cc and the other language drivers

The second input to the choice is the driver you run. Each language's lang_specific_driver can add options before the specs expand, and g++ adds one:

  if (shared_libgcc && !static_link)
    generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
		     &new_decoded_options[j++]);

Listing 4.2-8: g++ adds -shared-libgcc (gcc/cp/g++spec.cc:444-446).

shared_libgcc starts at 1 (g++spec.cc:137-138). -static-libgcc clears it (:231-232). -static sets static_link (:227-228). A driver built without ENABLE_SHARED_LIBGCC forces it to 0 (:297-301). The GCC manual documents the result under Link Options: the G++ driver adds -shared-libgcc because C++ programs typically use exceptions. The manual does not mention the other drivers:

DriverAddsSource
gcc-shared-libgcc only if an input ends in .m or .mi, and not with -static or -static-libgccc/gccspec.cc:32-92
g++, gccgo, gdc, gm2, gccrs-shared-libgcc, unless -static or -static-libgccgo/gospec.cc:425-427, d/d-spec.cc:506-510, m2/gm2spec.cc:917-918, rust/rustspec.cc:174-176
gfortran-shared-libgcc when it links libgfortran, unless -static or -static-libgccfortran/gfortranspec.cc:409-421
gnatlink-shared-libgcc for a shared GNAT runtime, otherwise a default from gcc/ada/link.c, -static-libgcc in its GNU/Linux armada/gnatlink.adb:1155-1160, :1980-2005, ada/link.c:130-134

So for the same object file, on a GNU/Linux target with the as-needed form, %G expands like this. Read each row off Listing 4.2-4:

CommandThe driver adds%G expands to
gcc foo.onothing-lgcc --as-needed -lgcc_s --no-as-needed
gcc -shared foo.onothing-lgcc --as-needed -lgcc_s --no-as-needed
g++ foo.o-shared-libgcc-lgcc_s -lgcc
g++ -shared foo.o-shared-libgcc-lgcc_s
g++ -static-libgcc foo.onothing-lgcc -lgcc_eh

With GNU ld 2.28 or later, read --push-state --as-needed and --pop-state for the two bracket options. The C link names libgcc_s inside the bracket and the C++ link names it outside, so only the C link lets the linker drop it.

Where they install: slibdir, and libgcc_s.so as a linker script

libgcc.a and libgcc_eh.a install into inst_libdir, the directory of crtbegin.o (Listing 1.7-6, and libgcc/Makefile.in:1177-1193). libgcc_s.so.1 installs into slibdir, plus the multilib OS directory (Makefile.in:316). The install manual documents --with-slibdir under Configuration, with libdir as its default. Configure's own default has one more case:

AC_ARG_WITH(slibdir,
[  --with-slibdir=DIR      shared libraries in DIR [LIBDIR]],
slibdir="$with_slibdir",
[if test "${version_specific_libs}" = yes; then
  slibdir='$(libsubdir)'
elif test -n "$with_cross_host" && test x"$with_cross_host" != x"no"; then
  case ${with_toolexeclibdir} in
    no)
      slibdir='$(exec_prefix)/$(host_noncanonical)/lib'
      ;;
    *)
      slibdir=${with_toolexeclibdir}
      ;;
  esac
else
  slibdir='$(libdir)'
fi])

Listing 4.2-9: where libgcc_s.so.1 goes (libgcc/configure.ac:114-130).

A native compiler puts it in $libdir. A cross compiler puts it in $exec_prefix/<target>/lib, the same pattern that Chapter 1.12 traced for libstdc++, where host in a target library means GCC's target. The static and shared halves of libgcc therefore install into different directories.

The link-time name libgcc_s.so is not always a symlink. t-slibgcc makes it one (t-slibgcc:30-32). A second fragment replaces the symlink with a linker script:

# Instead of creating $(SHLIB_SOLINK) symlink create a GNU ld
# linker script which sources in both $(SHLIB_SONAME) and libgcc.a.
# This is needed on targets where libgcc.a contains routines that aren't in
# $(SHLIB_SONAME) and are needed for shared libraries.

SHLIB_MAKE_SOLINK = \
	(echo "/* GNU ld script"; \
	 echo "   Use the shared library, but some functions are only in"; \
	 echo "   the static library.  */"; \
	 echo "GROUP ( $(SHLIB_SONAME) -lgcc )" \
	) > $(SHLIB_DIR)/$(SHLIB_SOLINK)

Listing 4.2-10: libgcc_s.so as a GROUP script (libgcc/config/t-slibgcc-libgcc:19-29).

Most GNU/Linux targets use it: AArch64, Arm and RISC-V in their own entries (config.host:438-447, :555-561 and :1352-1355), and x86 in a later block (:1593-1597). On AArch64, the reason is visible in t-lse, and a comment in the VxWorks block names it: the fragment "is needed on at least aarch64 (for lse)" (:1650-1651). It adds the out-of-line atomic helpers, such as __aarch64_cas4_acq, to libgcc-objects and to no other list (aarch64/t-lse:21-31), and each helper is a hidden symbol (aarch64/lse.S:175-183). Now look at the g++ -shared row of the table above: %G is -lgcc_s alone. Without the script, a C++ shared library that calls those helpers would have no libgcc.a on its link line. The script puts it back.

Watching the choice: -dumpspecs, -### and nm

These commands were not run for this chapter. What they should print is read off the listings above, so check them on your own toolchain.

Start with the libgcc spec as the driver holds it after the rewrite:

$ gcc -dumpspecs | sed -n '/^\*libgcc:/,/^$/p'

On a target without a shared libgcc, this prints the target's LIBGCC_SPEC, usually -lgcc. On a shared-libgcc target, it prints one of the two forms of Listing 4.2-4. Next, let the drivers and the flags pick, for any object file foo.o:

$ gcc -### foo.o 2>&1 | tr ' ' '\n' | grep -E 'lgcc|needed|state'
$ g++ -### foo.o 2>&1 | tr ' ' '\n' | grep -E 'lgcc|needed|state'
$ g++ -### -shared foo.o 2>&1 | tr ' ' '\n' | grep -E 'lgcc|needed|state'
$ gcc -### -static-libgcc foo.o 2>&1 | tr ' ' '\n' | grep -E 'lgcc|needed|state'

Expect each expansion of the table twice, because a GNU/Linux link has %G on both sides of the C library, as Chapter 1.7 explains.

Then check that the partition is real. On a shared-libgcc target, the unwinder's entry point is in libgcc_eh.a and libgcc_s.so.1, and not in libgcc.a:

$ nm -A "$(gcc -print-file-name=libgcc.a)" | grep -w _Unwind_RaiseException
$ nm -A "$(gcc -print-file-name=libgcc_eh.a)" | grep -w _Unwind_RaiseException
$ nm -D "$(gcc -print-file-name=libgcc_s.so.1)" | grep -w _Unwind_RaiseException

Listing 4.2-2 predicts no output from the first command on such a target, and a definition from the other two. On arm-none-eabi, the first command finds the symbol, because there the unwinder is in libgcc.a (arm/libunwind.S:356), and the other two files do not exist.

Last, ask what the link-time name is:

$ file -L "$(gcc -print-file-name=libgcc_s.so)"

As GCC installs it, this is a text file on a target that uses t-slibgcc-libgcc, and an ELF shared object on any other target with a shared libgcc. If -print-file-name echoes the bare name, the file is not on the driver's search list (Chapter 1.5).

Three things to carry away

libgcc.a holds what each object may copy, and the EH set holds what must exist once. With a shared libgcc, the unwinder and emutls.c move out of libgcc.a into libgcc_eh.a and libgcc_s.so.1.

The default comes from init_gcc_specs, not from your objects. The as-needed form lets the linker drop libgcc_s when nothing needs the unwinder. The older form links a static unwinder into executables.

The driver you run is an input. g++ and most other language drivers add -shared-libgcc, and gcc does not. A target without t-slibgcc has no choice to make.

Things that surprise people

--enable-shared=libstdc++ gives you a static-only libgcc. The list form turns shared libraries off for every package it does not name, and libgcc is a package.

g++ -shared names libgcc_s and never libgcc.a. On most GNU/Linux targets, the libgcc_s.so linker script adds -lgcc back. Replace that script with a symlink, and static-only helpers such as __aarch64_cas4_acq have nowhere to come from.

gcc foo.m adds -shared-libgcc, and gcc foo.c does not. The C driver's only libgcc rule is for Objective-C inputs.

-shared-libgcc on arm-none-eabi-gcc is accepted and does nothing. No spec for that target tests it.

Documentation coverage

The GCC manual documents -shared-libgcc and -static-libgcc under Link Options (gcc/doc/invoke.texi:19340-19371): why exceptions across shared libraries need the shared libgcc, the g++ default, and the --eh-frame-hdr default for shared libraries. The install manual documents --enable-shared and its package list, and --with-slibdir, under Configuration (gcc/doc/install.texi:1127-1145 and :942-944). The internals manual documents LIBGCC_SPEC, REAL_LIBGCC_SPEC, USE_LD_AS_NEEDED and LINK_EH_SPEC in the Driver node (gcc/doc/tm.texi:255-291), and the helpers in The GCC low-level runtime library (gcc/doc/libgcc.texi:6-18).

What is not documented:

  • libgcc_eh.a. No manual names it.
  • The partition: which sources go to libgcc.a, which to libgcc_eh.a and libgcc_s.so.1, and that a shared libgcc takes the unwinder out of libgcc.a.
  • That libgcc.a and libgcc_eh.a are compiled with hidden visibility, so each linked object gets a private copy.
  • The as-needed form, and so the default that gcc -shared has on a current GNU/Linux toolchain. Link Options describes only the older form. The internals manual defines USE_LD_AS_NEEDED but does not say which targets set it.
  • That gfortran, gccgo, gdc, gm2 and gccrs add -shared-libgcc as g++ does, that gnatlink has its own rule, and that gcc adds it for Objective-C inputs.
  • That libgcc_s.so is a linker script on some targets, and why.
  • That a cross compiler installs libgcc_s.so.1 under $exec_prefix/<target>/lib, not under libdir.

Next: the unwinder that libgcc_eh.a and libgcc_s.so.1 carry, and how a C++ throw uses its tables to find a handler.


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.