4.4 How libstdc++.so.6 stays compatible: version nodes and __cxx11

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

You build a program on a new distribution and copy it to an older machine. It stops before main runs. The dynamic loader prints version `%s' not found (required by %s), with a name such as GLIBCXX_3.4.34 in the first %s (elf/dl-version.c:143-144, glibc 2.42). On another day you link your code against a vendor's prebuilt library, and the link fails with an undefined reference to a function that takes std::__cxx11::basic_string<char, ...>. The vendor's header declares that exact function.

The two failures come from the same library and from two different mechanisms. The first is symbol versioning, which the loader checks at run time. The second is the dual ABI, which changes mangled names, so the linker sees it.

libstdc++ keeps one soname because no release changes what an older release exported. New symbols go under a new version name, and the C++11 std::string went in beside the old one, under a different mangled name.

This chapter assumes Chapter 1.11, for how g++ puts -lstdc++ on the link line, and Chapter 1.12, for which libstdc++.so that -l finds.

This is the book's one planned excursion into run time. Part I kept link-time search and run-time search apart, and Chapter 1.12 ended on that point. But whether a newer libstdc++.so.6 can replace an older one is a run-time question: the program meets the library a second time when the loader maps it. So this chapter follows the library past the linker, as far as the loader's version check. The loader's search order, DT_RUNPATH and LD_LIBRARY_PATH stay out of scope.

libtool_VERSION=6:34:0: where the .so.6 comes from

The library's file names come from one configure assignment:

# For libtool versioning info, format is CURRENT:REVISION:AGE
libtool_VERSION=6:34:0

Listing 4.4-1: the version triple (libstdc++-v3/acinclude.m4:4235-4236).

The link rule passes it to libtool as -version-info $(libtool_VERSION) (src/Makefile.am:163-164). On GNU/Linux, libtool turns the triple into a file-name suffix like this:

	linux)
	  func_arith $current - $age
	  major=.$func_arith_result
	  versuffix="$major.$age.$revision"
	  ;;

Listing 4.4-2: libtool's Linux rule (ltmain.sh:6546-6550).

With CURRENT 6, REVISION 34 and AGE 0, the major number is 6 and the suffix is .6.0.34. The same platform entry in libtool.m4 installs three names, the real file libstdc++.so.6.0.34 and two symbolic links, libstdc++.so.6 and libstdc++.so. It sets the soname to the name that ends at the major number, libstdc++.so.6 (libtool.m4:2526-2531). At install time, libtool creates the two links with ln -s (ltmain.sh:2294-2303).

The three names have three readers:

NameRead by
libstdc++.sothe link editor, when it resolves -lstdc++ (Chapter 1.12)
libstdc++.so.6the dynamic loader, because the program records it
libstdc++.so.6.0.34nobody by name: it is the file the links point at

The middle row is the bridge from link time to run time. The ld manual documents it under -soname, in Options: when an executable is linked with a shared object that has a DT_SONAME, the dynamic linker later loads the object that DT_SONAME names, not the file the linker was given (ld/ld.texi:699-705, binutils 2.45). The linker copies the library's DT_SONAME into the program's DT_NEEDED entry, and uses the file name only when there is no soname (bfd/elflink.c:4745-4754, binutils 2.45).

So the soname is a promise. Every program linked since the soname last changed asks the loader for libstdc++.so.6, and whatever file has that name must serve all of them. The libstdc++ manual's ABI Policy and Guidelines page lists the history. The last change was GCC 3.4.0, where libstdc++.so.6.0.0 is marked "Incompatible with previous" (doc/xml/manual/abi.xml:247). Since then, only the last number has moved. The REVISION field of the triple went from 0 to 34, and it appears only in the real file's name.

gnu.ver: one version node per release

A soname that never changes cannot tell an old library from a new one. Symbol versions can. configure selects a GNU ld version script, config/abi/pre/gnu.ver, when the linker is GNU ld (acinclude.m4:4115-4126 and :4243-4244). The libstdc++ manual documents the --enable-symvers option and its styles on its Configure page. The install manual does not list it.

The script defines a chain of version nodes. The base node, GLIBCXX_3.4, is the interface of GCC 3.4.0, and the manual's version-name table maps each later release to its node (abi.xml:313-365). The base node ends with a catch-all that hides every symbol the script does not name. Each later node names the one before it:

  # DO NOT DELETE THIS LINE.  Port-specific symbols, if any, will be here.

  local:
    *;
};

GLIBCXX_3.4.1 {

    _ZNSt12__basic_fileIcE4fileEv;

} GLIBCXX_3.4;

Listing 4.4-3: the end of the base node, and the first node after it (libstdc++-v3/config/abi/pre/gnu.ver:1109-1121).

The chain runs to GLIBCXX_3.4.34, which the file labels "GCC 15.1.0" and which is still the newest node at 15.2.0:

# GCC 15.1.0
GLIBCXX_3.4.34 {
    # std::__format::__with_encoding_conversion
    _ZNSt8__format26__with_encoding_conversionERKSt6locale;
    # std::__format::__locale_encoding_to_utf8
    _ZNSt8__format25__locale_encoding_to_utf8ERKSt6localeSt17basic_string_viewIcSt11char_traitsIcEEPv;
    # __sso_string constructor and destructor
    _ZNSt12__sso_string[CD][12]Ev;
    # void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<bool>(char const*, size_t)
    # and wide char version
    _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE12_M_constructILb[01]EEEvPK[cw][jmy];
} GLIBCXX_3.4.33;

Listing 4.4-4: the newest node (gnu.ver:2536-2547).

A second chain starts at CXXABI_1.3, with the comment "Symbols in the support library (libsupc++) have their own tag" (gnu.ver:2549-2550). That chain holds the language support that the compiler calls directly, such as __cxa_allocate_exception. One node, CXXABI_1.3.16, exists only on RISC-V (gnu.ver:2881-2888).

The script uses two kinds of pattern. Inside an extern "C++" block, a pattern matches the demangled name. Outside one, it matches the mangled name (gnu.ver:25-26 and :212-215). The ld manual documents both forms under VERSION (ld/ld.texi:6882-6892).

libstdc++-symbols.ver: the script is preprocessed

gnu.ver is not passed to the linker as it is. The build copies it, splices in the target's extra symbol files at the "DO NOT DELETE" line of Listing 4.4-3, and then runs the result through the C preprocessor:

	$(EGREP) -v '^[ 	]*#(#| |$$)' $@.tmp | \
	  $(CC) -E -P -include $(CONFIG_HEADER) - > $@ || (rm -f $@ ; exit 1)

Listing 4.4-5: the last step that builds libstdc++-symbols.ver (libstdc++-v3/src/Makefile.am:330-331). The whole rule is :313-332.

The grep drops lines that start with ##, # , or a lone #, which are the script's comments. A line such as #ifdef __riscv has no space after the #, so it survives, and the preprocessor evaluates it against the target's config.h. That is how one version script serves every target. The extra files come from configure.host, for example arm-eabi-extra.ver for Arm GNU/Linux (configure.host:369). The link then adds -Wl,--version-script=libstdc++-symbols.ver (src/Makefile.am:336-338).

What the loader checks: version names in DT_VERNEED

When the linker resolves a program's reference against a versioned definition, the program records the version node it needs, per library. The ld manual explains the purpose under VERSION: the loader can make "a quick check" that each library supplies every version node the program needs, so that it does not discover a missing symbol halfway through a run (ld/ld.texi:6813-6832).

glibc's loader does that check in _dl_check_map_versions. It walks the program's DT_VERNEED entries and, for each version name, searches the library's DT_VERDEF list (elf/dl-version.c:152-240). A name that is not there ends the load:

  /* XXX We cannot translate the message.  */
  _dl_exception_create_format
    (&exception, DSO_FILENAME (map->l_name),
     "version `%s' not found (required by %s)", string, name);

Listing 4.4-6: the error from the opening (elf/dl-version.c:141-144, glibc 2.42).

The check is by version name, not by symbol. A program that calls one function from GLIBCXX_3.4.34 needs the whole node, and a library from GCC 14 does not define it. The other direction passes, because a library built for the same target defines every node of the chain up to its own. That asymmetry is the whole guarantee. The ABI Policy page documents it as forward compatibility, and says that the reverse is not true (abi.xml:108-121).

You can see each side with tools that only read files:

$ objdump -T "$(g++ -print-file-name=libstdc++.so.6)" | grep -o 'GLIBCXX_[0-9.]*' | sort -u -V
$ objdump -p ./prog | sed -n '/^Version References/,$p'
$ LD_DEBUG=versions ./prog

The first command lists the nodes the library defines. The second prints the program's Version References section, one required from block per library (bfd/elf.c:1690-1695, binutils 2.45). The third makes glibc's loader report each version it checks, through the versions debug category (elf/rtld.c:2401-2402, glibc 2.42). These commands were not run for this book.

make check-abi: the test that keeps the promise

Version nodes only help if nobody edits an old one. The ABI Policy page lists the changes a release may make (add exported data, functions or instantiations) and the ones it may not (delete a symbol, or change its size, layout or mangling) (abi.xml:744-800). The source enforces that policy with a test, not with review alone.

A directory per target under config/abi/post/, such as x86_64-linux-gnu, holds a baseline_symbols.txt. It has one line per exported symbol, with the symbol's type and version:

FUNC:_ZNSs6appendERKSs@@GLIBCXX_3.4
FUNC:_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE6appendERKS4_@@GLIBCXX_3.4.21

Listing 4.4-7: two lines from the x86-64 GNU/Linux baseline (config/abi/post/x86_64-linux-gnu/baseline_symbols.txt:1670 and :3264).

make check-abi runs abi.exp (testsuite/Makefile.am:164-167). That script extracts the same list from the new libstdc++.so with scripts/extract_symvers, which reads the dynamic symbols with readelf --symbols (scripts/extract_symvers.in:48-66). Then it runs a small program, abi_check, on the two lists (testsuite/libstdc++-abi/abi.exp:57-71). A target with no baseline file skips the test (abi.exp:48-54). The comparison in testsuite_abi.cc fails the run on three kinds of change. A baseline symbol that is missing counts as incompatible (testsuite_abi.cc:477-483). A symbol in both lists must keep its type and its size (:320-344). And an added symbol must carry the newest version name:

      // Check that added symbols are added in the latest pre-release version.
      bool latestp = (test.version_name == "GLIBCXX_3.4.34"
		     || test.version_name == "CXXABI_1.3.16"

Listing 4.4-8: the rule for new symbols (libstdc++-v3/testsuite/util/testsuite_abi.cc:260-262).

That last rule protects against the script itself. The base node exports most of std through demangled globs, and the ld manual warns that "it's slightly crazy to use wildcards in a global spec except on the last version node", because a glob can add symbols to an old version (ld/ld.texi:6793-6797). A symbol that slipped into GLIBCXX_3.4 would pass the loader's name check on an old library that does not have it. So the globs leave gaps on purpose:

      std::basic_[p-r]*;
#     std::basic_streambuf
#     std::basic_string
#     std::basic_stringbuf
#     std::basic_stringstream;
      std::basic_[t-z]*;

Listing 4.4-9: no std::basic_s glob in the base node (gnu.ver:56-61).

No pattern in that list matches a name that starts with std::basic_s. The old std::string members are instead listed one mangled pattern at a time, starting at gnu.ver:217-222. A new member function that the globs did catch would still fail check-abi, because Listing 4.4-8 requires the newest node.

The dual ABI: _GLIBCXX_USE_CXX11_ABI and inline namespace __cxx11

C++11 forbids a copy-on-write std::string and requires a std::list to keep track of its size. Both changes alter what the object stores, and the ABI Policy page lists a change of layout as prohibited (abi.xml:744-800). The libstdc++ manual documents the outcome on its Dual ABI page. GCC 5.1 added new implementations, kept the old ones in the same library, and gave the new ones different names for linkage, by putting them in an inline namespace (using.xml:1354-1370).

The namespace is in <bits/c++config>:

#define _GLIBCXX_USE_DUAL_ABI

#if ! _GLIBCXX_USE_DUAL_ABI
// Ignore any pre-defined value of _GLIBCXX_USE_CXX11_ABI
# undef _GLIBCXX_USE_CXX11_ABI
#endif

#ifndef _GLIBCXX_USE_CXX11_ABI
#define _GLIBCXX_USE_CXX11_ABI
#endif

#if _GLIBCXX_USE_CXX11_ABI
namespace std
{
  inline namespace __cxx11 __attribute__((__abi_tag__ ("cxx11"))) { }
}
namespace __gnu_cxx
{
  inline namespace __cxx11 __attribute__((__abi_tag__ ("cxx11"))) { }
}
# define _GLIBCXX_NAMESPACE_CXX11 __cxx11::
# define _GLIBCXX_BEGIN_NAMESPACE_CXX11 namespace __cxx11 {
# define _GLIBCXX_END_NAMESPACE_CXX11 }
# define _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_ABI_TAG_CXX11

Listing 4.4-10: the switch and the namespace (libstdc++-v3/include/bits/c++config:357-380).

Three details in that listing matter.

The two #define lines have no value in the source. The build fills them in when it generates the installed c++config.h. A sed command appends a 1 or a 0 to each (include/Makefile.am:1407-1408), from stamp files that configure conditionals select (include/Makefile.am:1325-1339). --with-default-libstdcxx-abi chooses the second value, and its default is new (acinclude.m4:4899-4924). --disable-libstdcxx-dual-abi sets the first one to 0 (acinclude.m4:4878-4889). The libstdc++ manual documents both options on its Configure page (configure.xml:395-417).

The #ifndef lets each translation unit choose. A -D_GLIBCXX_USE_CXX11_ABI=0 on the command line is defined before the header runs, so the configured default does not replace it. The Macros page documents the macro. The #undef above it is the exception: in a library built without the dual ABI, the header discards your value.

The inline namespace hides itself from lookup, not from the linker. The string class is declared inside it:

_GLIBCXX_BEGIN_NAMESPACE_CXX11

  template<typename _CharT, typename _Traits = char_traits<_CharT>,
           typename _Alloc = allocator<_CharT> >
    class basic_string;

_GLIBCXX_END_NAMESPACE_CXX11

Listing 4.4-11: the declaration (libstdc++-v3/include/bits/stringfwd.h:70-76).

Source code that names std::string finds the class through the inline namespace and never spells __cxx11. The mangled name spells it. With the macro at 0, the same macros expand to nothing, and <bits/basic_string.h> includes the old copy-on-write class from cow_string.h instead (basic_string.h:64-71).

How __cxx11 reaches a mangled name: Ss and B5cxx11

The namespace changes mangled names by two routes. The first route applies to any name that contains the type. The second applies to functions that only return it.

The lost Ss abbreviation

The Itanium C++ ABI gives std::string a two-letter abbreviation, Ss. GCC's mangler applies it only when the class is declared directly in ::std:

  if (!DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl)))
    return false;

Listing 4.4-12: the test in is_std_substitution (gcc/cp/mangle.cc:504-505).

DECL_NAMESPACE_STD_P is true only for std_node itself (gcc/cp/cp-tree.h:3742-3743). The context of the new class is std::__cxx11, so the test fails, and the "Ss" branch at mangle.cc:643-663 never runs. The mangler writes the full nested name instead. The version script shows both forms for one constructor parameter, a const std::string&. The old form is RKSs, and the new form begins RKNSt7__cxx1112basic_string (gnu.ver:1742 and :1744).

So a function void use(const std::string&) gets one name under each value of the macro. Under the old ABI it is _Z3useRKSs. Under the new ABI it is _Z3useRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE. Mix the two in one link and the reference cannot meet the definition.

abi_tag and [abi:cxx11]

The first route misses one case. The mangled name of an ordinary function does not include its return type, so std::string make() would mangle the same under both ABIs, and an old-ABI caller would link against a new-ABI definition with no error. The abi_tag attribute on the namespace closes that gap. The GCC manual documents the attribute under C++ Attributes, including the inline-namespace form and the automatic tagging of a function whose return type carries a tag (gcc/doc/extend.texi:30298-30333).

The front end does the tagging when it mangles the function. write_mangled_name starts with a call to check_abi_tags (mangle.cc:794-799):

/* Check that DECL has all the ABI tags that are used in parts of its type
   that are not reflected in its mangled name.  */

void
check_abi_tags (tree decl)
{
  if (VAR_P (decl))
    check_abi_tags (decl, TREE_TYPE (decl));
  else if (TREE_CODE (decl) == FUNCTION_DECL
	   && !DECL_CONV_FN_P (decl)
	   && !mangle_return_type_p (decl))
    check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
}

Listing 4.4-13: only the parts of the type the name does not show (gcc/cp/class.cc:1903-1915).

For a function, the part checked is the return type, unless the return type is already in the mangled name, as it is for a function template instance (mangle.cc:846-852). The helper marks the tags that the parameter types already carry, with the comment "A function's parameter types are part of the signature, so we don't need to inherit any tags that are also in them" (class.cc:1844-1850). It finds a tag by walking each type's enclosing scopes up to the global namespace, so the tag on __cxx11 is found (class.cc:1752-1772). Any tag that is left is added to the function's own abi_tag attribute, and the mangler writes it after the name:

  unsigned i; tree str;
  FOR_EACH_VEC_ELT (*vec, i, str)
    {
      write_string ("B");
      write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
      write_identifier (TREE_STRING_POINTER (str));
    }

Listing 4.4-14: write_abi_tags (gcc/cp/mangle.cc:1723-1729).

So std::string make() becomes _Z4makeB5cxx11v under the new ABI and stays _Z4makev under the old one. The tag on the namespace itself is never written, because get_abi_tags returns nothing for a namespace (mangle.cc:526-527). The tag reaches a mangled name only through the functions and variables it is copied to. The demangler in libiberty prints a B tag as [abi:cxx11] (libiberty/cp-demangle.c:5100-5105). binutils carries the same code (libiberty/cp-demangle.c:5102, binutils 2.45), so binutils tools print the same [abi:cxx11]. The Dual ABI page names that tag as the second form of this link error (using.xml:1467-1475). -Wabi-tag makes the front end warn when it adds such a tag (class.cc:1693-1697).

One library, both strings: cow-string-inst.cc

The library must define both versions of every exported std::string member. It does that by compiling one instantiation file twice. string-inst.cc picks the new ABI by default:

#ifndef _GLIBCXX_USE_CXX11_ABI
// Instantiations in this file use the new SSO std::string ABI unless included
// by another file which defines _GLIBCXX_USE_CXX11_ABI=0.
# define _GLIBCXX_USE_CXX11_ABI 1
#endif

Listing 4.4-15: libstdc++-v3/src/c++11/string-inst.cc:32-36.

and cow-string-inst.cc includes it with the macro already at 0:

#define _GLIBCXX_USE_CXX11_ABI 0
#include "string-inst.cc"

Listing 4.4-16: libstdc++-v3/src/c++11/cow-string-inst.cc:29-30.

The two objects define different mangled names, so both link into one libstdc++.so. The version script then files them in different nodes. The old names (_ZNSs...) are in GLIBCXX_3.4, as Listing 4.4-9's gaps require. The new names are under the comment "ABI-tagged std::basic_string" in GLIBCXX_3.4.21, the GCC 5.1 node (gnu.ver:1765-1767). Listing 4.4-7 shows the result for append: one member function, two exports, two versions.

The dual ABI therefore needed no new soname. A program built with GCC 4.9 that calls append refers to _ZNSs6appendERKSs at version GLIBCXX_3.4, and the GCC 15 baseline still lists that symbol (Listing 4.4-7).

Two files are enough. lib.cc defines the function, and main.cc calls it:

$ printf '#include <string>\nvoid use(const std::string&) {}\n' > lib.cc
$ printf '#include <string>\nvoid use(const std::string&);\nint main() { use("x"); }\n' > main.cc
$ g++ -c -D_GLIBCXX_USE_CXX11_ABI=0 lib.cc
$ nm lib.o | grep use
$ g++ main.cc lib.o

These commands were not run for this book. By Listing 4.4-12, nm shows _Z3useRKSs in lib.o, and main.cc, compiled with the default, references the __cxx11 name, so the last command fails with an undefined reference. Remove the -D and the link succeeds. Add std::string make() { return {}; } to lib.cc and compile it twice, with and without the -D, and nm -C shows make[abi:cxx11]() only in the default build.

The Dual ABI page documents the cure under Troubleshooting: rebuild the library with the new ABI, or compile your own code with the macro at 0 (using.xml:1467-1475).

Three things to carry away

The soname has been libstdc++.so.6 since GCC 3.4, because each release only adds. The release number is in the REVISION field of libtool_VERSION, which only the real file name shows.

The loader checks version names, not symbols. A program needs every node it used. It runs on any newer library, and fails before main on an older library that lacks one of those nodes.

__cxx11 renames the new std::string in two ways. The namespace changes every name that contains the type, and the abi_tag changes functions that only return it.

Things that surprise people

-std=c++98 does not select the old std::string. The default value of _GLIBCXX_USE_CXX11_ABI is the same for every dialect. The Dual ABI page documents this, so that C++03 and C++11 code can link together (using.xml:1386-1392).

A class with a std::string member has one mangled name under both ABIs. Its layout changes and its name does not, so a mismatch links and fails at run time. The Dual ABI page says so, and points to -Wabi-tag (using.xml:1477-1484).

--with-default-libstdcxx-abi=gcc4-compatible still ships both strings. It changes only the header default, and the manual says it "does not change the library ABI" (configure.xml:406-417). --disable-libstdcxx-dual-abi removes the new string from the library.

--enable-symvers=gnu-versioned-namespace changes the soname to libstdc++.so.8. It sets libtool_VERSION=8:0:0 and its own version script (acinclude.m4:4248-4250), and it turns the dual ABI off (:4880-4883). The library then puts nearly all of std in an inline namespace __8 (c++config:389-393). A program built for libstdc++.so.6 cannot use it.

Documentation coverage

The libstdc++ manual owns this subject, and its pages are rendered at the pin. ABI Policy and Guidelines (libstdc++-v3/doc/xml/manual/abi.xml) covers the soname history, the version-name history, the allowed and prohibited changes, and make check-abi. Dual ABI (doc/xml/manual/using.xml:1350) covers the two strings, the macro and the troubleshooting. Configure (doc/xml/manual/configure.xml:309, :395 and :406) covers --enable-symvers, --disable-libstdcxx-dual-abi and --with-default-libstdcxx-abi, and Macros (doc/xml/manual/using.xml:1218) covers _GLIBCXX_USE_CXX11_ABI. The GCC manual documents abi_tag under C++ Attributes (gcc/doc/extend.texi:30298), and -Wabi-tag and -fabi-version under C++ Dialect Options (gcc/doc/invoke.texi:3762 and :2934). Outside GCC, the ld manual documents -soname and version scripts under VERSION. The install manual mentions none of the libstdc++ configure options in this chapter.

What is not documented:

  • That the release counter is the REVISION field of libtool_VERSION, and that libtool turns 6:34:0 into libstdc++.so.6.0.34.
  • The manual's version tables stop at GCC 14.1.0. At the pin, the library is libstdc++.so.6.0.34, and GLIBCXX_3.4.34 is the newest node. Neither has a row in the file-name table (abi.xml:233-285) or the version-name table (:313-365).
  • That the version script goes through the C preprocessor with the target's config.h, and that each target's extra symbols are spliced in at the "DO NOT DELETE" line.
  • That check-abi fails any added symbol that does not carry the newest version name, and that the base node's globs leave out every std::basic_s name.
  • How the namespace reaches a mangled name: the lost Ss abbreviation, and the front end's rule that tags only the return type and only when the parameters do not already carry the tag.

Next: Chapter 4.5 chooses between static and shared runtimes, and shows why two shared objects that each carry their own libstdc++ cannot catch each other's exceptions.


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.