ABI Policy and Guidelines

C++ applications often depend on specific language support routines, say for throwing exceptions, or catching exceptions, and perhaps also depend on features in the C++ Standard Library.

The C++ Standard Library has many include files, types defined in those include files, specific named functions, and other behavior. The text of these behaviors, as written in source include files, is called the Application Programing Interface, or API.

Furthermore, C++ source that is compiled into object files is transformed by the compiler: it arranges objects with specific alignment and in a particular layout, mangling names according to a well-defined algorithm, has specific arrangements for the support of virtual functions, etc. These details are defined as the compiler Application Binary Interface, or ABI. The GNU C++ compiler uses an industry-standard C++ ABI starting with version 3. Details can be found in the ABI specification.

The GNU C++ compiler, g++, has a compiler command line option to switch between various different C++ ABIs. This explicit version switch is the flag -fabi-version. In addition, some g++ command line options may change the ABI as a side-effect of use. Such flags include -fpack-struct and -fno-exceptions, but include others: see the complete list in the GCC manual under the heading Options for Code Generation Conventions.

The configure options used when building a specific libstdc++ version may also impact the resulting library ABI. The available configure options, and their impact on the library ABI, are documented here.

Putting all of these ideas together results in the C++ Standard library ABI, which is the compilation of a given library API by a given compiler ABI. In a nutshell:

library API + compiler ABI = library ABI

The library ABI is mostly of interest for end-users who have unresolved symbols and are linking dynamically to the C++ Standard library, and who thus must be careful to compile their application with a compiler that is compatible with the available C++ Standard library binary. In this case, compatible is defined with the equation above: given an application compiled with a given compiler ABI and library API, it will work correctly with a Standard C++ Library created with the same constraints.

To use a specific version of the C++ ABI, one must use a corresponding GNU C++ toolchain (i.e., g++ and libstdc++) that implements the C++ ABI in question.

The C++ interface has evolved throughout the history of the GNU C++ toolchain. With each release, various details have been changed so as to give distinct versions to the C++ interface.

How can this complexity be managed? What does C++ versioning mean? Because library and compiler changes often make binaries compiled with one version of the GNU tools incompatible with binaries compiled with other (either newer or older) versions of the same GNU tools, specific techniques are used to make managing this complexity easier.

The following techniques are used:

  1. Release versioning on the libgcc_s.so binary.

    This is implemented via file names and the ELF DT_SONAME mechanism (at least on ELF systems). It is versioned as follows:

    For m68k-linux the versions differ as follows:

    For hppa-linux the versions differ as follows:

  2. Symbol versioning on the libgcc_s.so binary.

    It is versioned with the following labels and version definitions, where the version definition is the maximum for a particular release. Labels are cumulative. If a particular release is not listed, it has the same version labels as the preceding release.

    This corresponds to the mapfile: gcc/libgcc-std.ver

  3. Release versioning on the libstdc++.so binary, implemented in the same way as the libgcc_s.so binary above. Listed is the filename: DT_SONAME can be deduced from the filename by removing the last two period-delimited numbers. For example, filename libstdc++.so.5.0.4 corresponds to a DT_SONAME of libstdc++.so.5. Binaries with equivalent DT_SONAMEs are forward-compatibile: in the table below, releases incompatible with the previous one are explicitly noted.

    It is versioned as follows:

    Note 1: Error should be libstdc++.so.3.0.3.

    Note 2: Not strictly required.

    Note 3: This release (but not previous or subsequent) has one known incompatibility, see 33678 in the GCC bug database.

  4. Symbol versioning on the libstdc++.so binary.

    mapfile: libstdc++-v3/config/abi/pre/gnu.ver

    It is versioned with the following labels and version definitions, where the version definition is the maximum for a particular release. Note, only symbols which are newly introduced will use the maximum version definition. Thus, for release series with the same label, but incremented version definitions, the later release has both versions. (An example of this would be the gcc-3.2.1 release, which has GLIBCPP_3.2.1 for new symbols and GLIBCPP_3.2 for symbols that were introduced in the gcc-3.2.0 release.) If a particular release is not listed, it has the same version labels as the preceding release.

    • gcc-3.0.0: (Error, not versioned)

    • gcc-3.0.1: (Error, not versioned)

    • gcc-3.0.2: (Error, not versioned)

    • gcc-3.0.3: (Error, not versioned)

    • gcc-3.0.4: (Error, not versioned)

    • gcc-3.1.0: GLIBCPP_3.1, CXXABI_1

    • gcc-3.1.1: GLIBCPP_3.1, CXXABI_1

    • gcc-3.2.0: GLIBCPP_3.2, CXXABI_1.2

    • gcc-3.2.1: GLIBCPP_3.2.1, CXXABI_1.2

    • gcc-3.2.2: GLIBCPP_3.2.2, CXXABI_1.2

    • gcc-3.2.3: GLIBCPP_3.2.2, CXXABI_1.2

    • gcc-3.3.0: GLIBCPP_3.2.2, CXXABI_1.2.1

    • gcc-3.3.1: GLIBCPP_3.2.3, CXXABI_1.2.1

    • gcc-3.3.2: GLIBCPP_3.2.3, CXXABI_1.2.1

    • gcc-3.3.3: GLIBCPP_3.2.3, CXXABI_1.2.1

    • gcc-3.4.0: GLIBCXX_3.4, CXXABI_1.3

    • gcc-3.4.1: GLIBCXX_3.4.1, CXXABI_1.3

    • gcc-3.4.2: GLIBCXX_3.4.2

    • gcc-3.4.3: GLIBCXX_3.4.3

    • gcc-4.0.0: GLIBCXX_3.4.4, CXXABI_1.3.1

    • gcc-4.0.1: GLIBCXX_3.4.5

    • gcc-4.0.2: GLIBCXX_3.4.6

    • gcc-4.0.3: GLIBCXX_3.4.7

    • gcc-4.1.1: GLIBCXX_3.4.8

    • gcc-4.2.0: GLIBCXX_3.4.9

    • gcc-4.3.0: GLIBCXX_3.4.10, CXXABI_1.3.2

    • gcc-4.4.0: GLIBCXX_3.4.11, CXXABI_1.3.3

    • gcc-4.4.1: GLIBCXX_3.4.12, CXXABI_1.3.3

    • gcc-4.4.2: GLIBCXX_3.4.13, CXXABI_1.3.3

    • gcc-4.5.0: GLIBCXX_3.4.14, CXXABI_1.3.4

  5. Incremental bumping of a compiler pre-defined macro, __GXX_ABI_VERSION. This macro is defined as the version of the compiler v3 ABI, with g++ 3.0.x being version 100. This macro will be automatically defined whenever g++ is used (the curious can test this by invoking g++ with the '-v' flag.)

    This macro was defined in the file "lang-specs.h" in the gcc/cp directory. Later versions defined it in "c-common.c" in the gcc directory, and from G++ 3.4 it is defined in c-cppbuiltin.c and its value determined by the '-fabi-version' command line option.

    It is versioned as follows, where 'n' is given by '-fabi-version=n':

    • gcc-3.0.x: 100

    • gcc-3.1.x: 100 (Error, should be 101)

    • gcc-3.2.x: 102

    • gcc-3.3.x: 102

    • gcc-3.4.x, gcc-4.[0-5].x: 102 (when n=1)

    • gcc-3.4.x, gcc-4.[0-5].x: 1000 + n (when n>1)

    • gcc-3.4.x, gcc-4.[0-5].x: 999999 (when n=0)

  6. Changes to the default compiler option for -fabi-version.

    It is versioned as follows:

    • gcc-3.0.x: (Error, not versioned)

    • gcc-3.1.x: (Error, not versioned)

    • gcc-3.2.x: -fabi-version=1

    • gcc-3.3.x: -fabi-version=1

    • gcc-3.4.x, gcc-4.[0-5].x: -fabi-version=2 (Incompatible with previous)

  7. Incremental bumping of a library pre-defined macro. For releases before 3.4.0, the macro is __GLIBCPP__. For later releases, it's __GLIBCXX__. (The libstdc++ project generously changed from CPP to CXX throughout its source to allow the "C" pre-processor the CPP macro namespace.) These macros are defined as the date the library was released, in compressed ISO date format, as an unsigned long.

    This macro is defined in the file "c++config" in the "libstdc++-v3/include/bits" directory. (Up to gcc-4.1.0, it was changed every night by an automated script. Since gcc-4.1.0, it is the same value as gcc/DATESTAMP.)

    It is versioned as follows:

    • gcc-3.0.0: 20010615

    • gcc-3.0.1: 20010819

    • gcc-3.0.2: 20011023

    • gcc-3.0.3: 20011220

    • gcc-3.0.4: 20020220

    • gcc-3.1.0: 20020514

    • gcc-3.1.1: 20020725

    • gcc-3.2.0: 20020814

    • gcc-3.2.1: 20021119

    • gcc-3.2.2: 20030205

    • gcc-3.2.3: 20030422

    • gcc-3.3.0: 20030513

    • gcc-3.3.1: 20030804

    • gcc-3.3.2: 20031016

    • gcc-3.3.3: 20040214

    • gcc-3.4.0: 20040419

    • gcc-3.4.1: 20040701

    • gcc-3.4.2: 20040906

    • gcc-3.4.3: 20041105

    • gcc-3.4.4: 20050519

    • gcc-3.4.5: 20051201

    • gcc-3.4.6: 20060306

    • gcc-4.0.0: 20050421

    • gcc-4.0.1: 20050707

    • gcc-4.0.2: 20050921

    • gcc-4.0.3: 20060309

    • gcc-4.1.0: 20060228

    • gcc-4.1.1: 20060524

    • gcc-4.1.2: 20070214

    • gcc-4.2.0: 20070514

    • gcc-4.2.1: 20070719

    • gcc-4.2.2: 20071007

    • gcc-4.2.3: 20080201

    • gcc-4.2.4: 20080519

    • gcc-4.3.0: 20080306

    • gcc-4.3.1: 20080606

    • gcc-4.3.2: 20080827

    • gcc-4.3.3: 20090124

    • gcc-4.4.0: 20090421

    • gcc-4.4.1: 20090722

    • gcc-4.4.2: 20091015

  8. Incremental bumping of a library pre-defined macro, _GLIBCPP_VERSION. This macro is defined as the released version of the library, as a string literal. This is only implemented in gcc-3.1.0 releases and higher, and is deprecated in 3.4 (where it is called _GLIBCXX_VERSION).

    This macro is defined in the file "c++config" in the "libstdc++-v3/include/bits" directory and is generated automatically by autoconf as part of the configure-time generation of config.h.

    It is versioned as follows:

    • gcc-3.0.0: "3.0.0"

    • gcc-3.0.1: "3.0.0" (Error, should be "3.0.1")

    • gcc-3.0.2: "3.0.0" (Error, should be "3.0.2")

    • gcc-3.0.3: "3.0.0" (Error, should be "3.0.3")

    • gcc-3.0.4: "3.0.0" (Error, should be "3.0.4")

    • gcc-3.1.0: "3.1.0"

    • gcc-3.1.1: "3.1.1"

    • gcc-3.2.0: "3.2"

    • gcc-3.2.1: "3.2.1"

    • gcc-3.2.2: "3.2.2"

    • gcc-3.2.3: "3.2.3"

    • gcc-3.3.0: "3.3"

    • gcc-3.3.1: "3.3.1"

    • gcc-3.3.2: "3.3.2"

    • gcc-3.3.3: "3.3.3"

    • gcc-3.4.x: "version-unused"

    • gcc-4.[0-5].x: "version-unused"

  9. Matching each specific C++ compiler release to a specific set of C++ include files. This is only implemented in gcc-3.1.1 releases and higher.

    All C++ includes are installed in include/c++, then nest in a directory hierarchy corresponding to the C++ compiler's released version. This version corresponds to the variable "gcc_version" in "libstdc++-v3/acinclude.m4," and more details can be found in that file's macro GLIBCXX_CONFIGURE (GLIBCPP_CONFIGURE before gcc-3.4.0).

    C++ includes are versioned as follows:

    • gcc-3.0.0: include/g++-v3

    • gcc-3.0.1: include/g++-v3

    • gcc-3.0.2: include/g++-v3

    • gcc-3.0.3: include/g++-v3

    • gcc-3.0.4: include/g++-v3

    • gcc-3.1.0: include/g++-v3

    • gcc-3.1.1: include/c++/3.1.1

    • gcc-3.2.0: include/c++/3.2

    • gcc-3.2.1: include/c++/3.2.1

    • gcc-3.2.2: include/c++/3.2.2

    • gcc-3.2.3: include/c++/3.2.3

    • gcc-3.3.0: include/c++/3.3

    • gcc-3.3.1: include/c++/3.3.1

    • gcc-3.3.2: include/c++/3.3.2

    • gcc-3.3.3: include/c++/3.3.3

    • gcc-3.4.0: include/c++/3.4.0

    • gcc-3.4.1: include/c++/3.4.1

    • gcc-3.4.2: include/c++/3.4.2

    • gcc-3.4.3: include/c++/3.4.3

    • gcc-3.4.4: include/c++/3.4.4

    • gcc-3.4.5: include/c++/3.4.5

    • gcc-3.4.6: include/c++/3.4.6

    • gcc-4.0.0: include/c++/4.0.0

    • gcc-4.0.1: include/c++/4.0.1

    • gcc-4.0.2: include/c++/4.0.2

    • gcc-4.0.3: include/c++/4.0.3

    • gcc-4.1.0: include/c++/4.1.0

    • gcc-4.1.1: include/c++/4.1.1

    • gcc-4.1.2: include/c++/4.1.2

    • gcc-4.2.0: include/c++/4.2.0

    • gcc-4.2.1: include/c++/4.2.1

    • gcc-4.2.2: include/c++/4.2.2

    • gcc-4.2.3: include/c++/4.2.3

    • gcc-4.2.4: include/c++/4.2.4

    • gcc-4.3.0: include/c++/4.3.0

    • gcc-4.3.1: include/c++/4.3.1

    • gcc-4.3.3: include/c++/4.3.3

    • gcc-4.3.4: include/c++/4.3.4

    • gcc-4.4.0: include/c++/4.4.0

    • gcc-4.4.1: include/c++/4.4.1

    • gcc-4.4.2: include/c++/4.4.2

    • gcc-4.5.0: include/c++/4.5.0

Taken together, these techniques can accurately specify interface and implementation changes in the GNU C++ tools themselves. Used properly, they allow both the GNU C++ tools implementation, and programs using them, an evolving yet controlled development that maintains backward compatibility.

It turns out that most of the configure options that change default behavior will impact the mangled names of exported symbols, and thus impact versioning and compatibility.

For more information on configure options, including ABI impacts, see: here

There is one flag that explicitly deals with symbol versioning: --enable-symvers.

In particular, libstdc++-v3/acinclude.m4 has a macro called GLIBCXX_ENABLE_SYMVERS that defaults to yes (or the argument passed in via --enable-symvers=foo). At that point, the macro attempts to make sure that all the requirement for symbol versioning are in place. For more information, please consult acinclude.m4.

The following will cause the library minor version number to increase, say from "libstdc++.so.3.0.4" to "libstdc++.so.3.0.5".

Other allowed changes are possible.

The following non-exhaustive list will cause the library major version number to increase, say from "libstdc++.so.3.0.4" to "libstdc++.so.4.0.0".

  1. Separation of interface and implementation

    This is accomplished by two techniques that separate the API from the ABI: forcing undefined references to link against a library binary for definitions.

    In addition, these techniques have the additional benefit that they reduce binary size, which can increase runtime performance.

  2. Namespaces linking symbol definitions to export mapfiles

    All symbols in the shared library binary are processed by a linker script at build time that either allows or disallows external linkage. Because of this, some symbols, regardless of normal C/C++ linkage, are not visible. Symbols that are internal have several appealing characteristics: by not exporting the symbols, there are no relocations when the shared library is started and thus this makes for faster runtime loading performance by the underlying dynamic loading mechanism. In addition, they have the possibility of changing without impacting ABI compatibility.

    The following namespaces are transformed by the mapfile:

  3. Freezing the API

    Disallowed changes, as above, are not made on a stable release branch. Enforcement tends to be less strict with GNU extensions that standard includes.

Testing for GNU C++ ABI changes is composed of two distinct areas: testing the C++ compiler (g++) for compiler changes, and testing the C++ library (libstdc++) for library changes.

Testing the C++ compiler ABI can be done various ways.

One. Intel ABI checker.

Two. The second is yet unreleased, but has been announced on the gcc mailing list. It is yet unspecified if these tools will be freely available, and able to be included in a GNU project. Please contact Mark Mitchell ([email protected]) for more details, and current status.

Three. Involves using the vlad.consistency test framework. This has also been discussed on the gcc mailing lists.

Testing the C++ library ABI can also be done various ways.

One. (Brendan Kehoe, Jeff Law suggestion to run 'make check-c++' two ways, one with a new compiler and an old library, and the other with an old compiler and a new library, and look for testsuite regressions)

Details on how to set this kind of test up can be found here: http://gcc.gnu.org/ml/gcc/2002-08/msg00142.html

Two. Use the 'make check-abi' rule in the libstdc++ Makefile.

This is a proactive check of the library ABI. Currently, exported symbol names that are either weak or defined are checked against a last known good baseline. Currently, this baseline is keyed off of 3.4.0 binaries, as this was the last time the .so number was incremented. In addition, all exported names are demangled, and the exported objects are checked to make sure they are the same size as the same object in the baseline. Notice that each baseline is relative to a default configured library and compiler: in particular, if options such as --enable-clocale, or --with-cpu, in case of multilibs, are used at configure time, the check may fail, either because of substantive differences or because of limitations of the current checking machinery.

This dataset is insufficient, yet a start. Also needed is a comprehensive check for all user-visible types part of the standard library for sizeof() and alignof() changes.

Verifying compatible layouts of objects is not even attempted. It should be possible to use sizeof, alignof, and offsetof to compute offsets for each structure and type in the standard library, saving to another datafile. Then, compute this in a similar way for new binaries, and look for differences.

Another approach might be to use the -fdump-class-hierarchy flag to get information. However, currently this approach gives insufficient data for use in library testing, as class data members, their offsets, and other detailed data is not displayed with this flag. (See PR g++/7470 on how this was used to find bugs.)

Perhaps there are other C++ ABI checkers. If so, please notify us. We'd like to know about them!

A "C" application, dynamically linked to two shared libraries, liba, libb. The dependent library liba is a C++ shared library compiled with gcc-3.3.x, and uses io, exceptions, locale, etc. The dependent library libb is a C++ shared library compiled with gcc-3.4.x, and also uses io, exceptions, locale, etc.

As above, libone is constructed as follows:

%$bld/H-x86-gcc-3.4.0/bin/g++ -fPIC -DPIC -c a.cc

%$bld/H-x86-gcc-3.4.0/bin/g++ -shared -Wl,-soname -Wl,libone.so.1 -Wl,-O1 -Wl,-z,defs a.o -o libone.so.1.0.0

%ln -s libone.so.1.0.0 libone.so

%$bld/H-x86-gcc-3.4.0/bin/g++ -c a.cc

%ar cru libone.a a.o

And, libtwo is constructed as follows:

%$bld/H-x86-gcc-3.3.3/bin/g++ -fPIC -DPIC -c b.cc

%$bld/H-x86-gcc-3.3.3/bin/g++ -shared -Wl,-soname -Wl,libtwo.so.1 -Wl,-O1 -Wl,-z,defs b.o -o libtwo.so.1.0.0

%ln -s libtwo.so.1.0.0 libtwo.so

%$bld/H-x86-gcc-3.3.3/bin/g++ -c b.cc

%ar cru libtwo.a b.o

...with the resulting libraries looking like

%ldd libone.so.1.0.0
	libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x40016000)
	libm.so.6 => /lib/tls/libm.so.6 (0x400fa000)
	libgcc_s.so.1 => /mnt/hd/bld/gcc/gcc/libgcc_s.so.1 (0x4011c000)
	libc.so.6 => /lib/tls/libc.so.6 (0x40125000)
	/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x00355000)

%ldd libtwo.so.1.0.0
	libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x40027000)
	libm.so.6 => /lib/tls/libm.so.6 (0x400e1000)
	libgcc_s.so.1 => /mnt/hd/bld/gcc/gcc/libgcc_s.so.1 (0x40103000)
	libc.so.6 => /lib/tls/libc.so.6 (0x4010c000)
	/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x00355000)

Then, the "C" compiler is used to compile a source file that uses functions from each library.

gcc test.c -g -O2 -L. -lone -ltwo /usr/lib/libstdc++.so.5 /usr/lib/libstdc++.so.6

Which gives the expected:

%ldd a.out
	libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x00764000)
	libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x40015000)
	libc.so.6 => /lib/tls/libc.so.6 (0x0036d000)
	libm.so.6 => /lib/tls/libm.so.6 (0x004a8000)
	libgcc_s.so.1 => /mnt/hd/bld/gcc/gcc/libgcc_s.so.1 (0x400e5000)
	/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x00355000)

This resulting binary, when executed, will be able to safely use code from both liba, and the dependent libstdc++.so.6, and libb, with the dependent libstdc++.so.5.

Some features in the C++ language make versioning especially difficult. In particular, compiler generated constructs such as implicit instantiations for templates, typeinfo information, and virtual tables all may cause ABI leakage across shared library boundaries. Because of this, mixing C++ ABIs is not recommended at this time.

For more background on this issue, see these bugzilla entries:

24660: versioning weak symbols in libstdc++

19664: libstdc++ headers should have pop/push of the visibility around the declarations