Platform:Itanium C++ ABI/Name mangling
How GCC and Clang mangle C++ names: _Z + an encoding built from length-prefixed source names, N...E nested names, I...E template arguments, one-letter builtin types, back-references S_/S0_ and T_/T0_, and special names for vtables, RTTI, thunks and guard variables. Test vectors are from LLVM's demangler test suite.
Structure
<mangled-name> ::= _Z <encoding> [.<clone suffix>] (macOS adds a leading underscore: __Z). The encoding of a function is its name followed by its parameter types; the return type is mangled only for function templates (not for constructors, destructors and conversion operators). Data and special names have no parameter list.
- Source names are length-prefixed:
3foo=foo._ZN12_GLOBAL__N_1...is(anonymous namespace). - Nested names:
N [K|V|r] [R|O] <prefix>... <name> E;KafterNmakes a const member function,R/Oan&/&&ref-qualified one:_ZNKSt9exception4whatEv=std::exception::what() const. - std:
St=std::; abbreviationsSa(std::allocator),Sb(std::basic_string),Ss(std::string, i.e.std::basic_string<char, std::char_traits<char>, std::allocator<char>>),Si(std::istream),So(std::ostream),Sd(std::iostream). - Templates:
I <args> Eafter a name;L <type> <value> Eis a literal argument (Li1E= 1,Lb0E= false),J...Ean argument pack. - Local names:
Z <function encoding> E <entity> [_<n>]:_ZGVZN5clang22getClangRepositoryPathEvE13SVNRepository=guard variable for clang::getClangRepositoryPath()::SVNRepository.Ul <params> E [n] _is a lambda ('lambda'(...),'lambda0'for the second),Ut [n] _an unnamed type. - ABI tags:
B <source-name>:_ZNKSt3_V214error_category10_M_messageB5cxx11Ei=std::_V2::error_category::_M_message[abi:cxx11](int) const(libstdc++'s C++11 std::string ABI).
Types
| Code | Type | Code | Type |
|---|---|---|---|
| v | void | w | wchar_t |
| b | bool | c | char |
| a | signed char | h | unsigned char |
| s | short | t | unsigned short |
| i | int | j | unsigned int |
| l | long | m | unsigned long |
| x | long long | y | unsigned long long |
| n | __int128 | o | unsigned __int128 |
| f | float | d | double |
| e | long double | g | __float128 |
| z | ... (ellipsis) | Dn | std::nullptr_t |
| Di | char32_t | Ds | char16_t |
| Du | char8_t | DF16_ | _Float16 |
Constructors: P pointer, R lvalue reference, O rvalue reference, K/V/r const/volatile/restrict (printed after the type: PKc = char const*), A<n>_<type> array, F <ret> <params> E function type, M <class> <member type> pointer to member, Dp pack expansion, Do noexcept.
Compression
Every prefix of a nested name, every template-id and every non-builtin type is entered into a substitution table as it is parsed; S_ refers to entry 0, S0_ to entry 1, S1_ to entry 2 (base-36 with digits and capitals). The complete name of a function is not entered. T_, T0_ refer to the template arguments of the function being mangled. Example: _ZN4dyld27findCoalescedExportedSymbolEPKcPPKN11ImageLoader6SymbolEPPKS2_ ends with S2_ = entry 3 = ImageLoader (entries: 0 dyld, 1 char const, 2 char const*, 3 ImageLoader, 4 ImageLoader::Symbol, 5 ImageLoader::Symbol const...), so the last parameter is ImageLoader const**.
Special names
| Prefix | Meaning | Example (LLVM test suite) |
|---|---|---|
_ZTV |
vtable | _ZTV11ImageLoader = vtable for ImageLoader
|
_ZTI |
type_info object | _ZTISt12length_error = typeinfo for std::length_error
|
_ZTS |
type name string | _ZTSSt11logic_error = typeinfo name for std::logic_error
|
_ZTT |
VTT (table of vtables used while constructing classes with virtual bases) | _ZTTSd = VTT for std::iostream
|
_ZTC |
construction vtable | _ZTC14SSDatabaseImpl0_N8Security10CssmClient6DbImplE = construction vtable for Security::CssmClient::DbImpl-in-SSDatabaseImpl
|
_ZTh |
non-virtual this-adjusting thunk | _ZThn1912_N12_GLOBAL__N_126PrecompilePreambleConsumerD1Ev
|
_ZTv |
virtual thunk (adjustment read from the vtable) | _ZTv0_n12_NSt9strstreamD0Ev = virtual thunk to std::strstream::~strstream()
|
_ZTc |
covariant return thunk | |
_ZGV |
guard variable of a static local or template static | |
_ZGR |
reference temporary | _ZGRZN1N1gEvE1a
|
_ZTW / _ZTH |
thread_local wrapper / initialisation function | _ZTW1x, _ZTHN3fooE
|
_ZGTt |
transaction-safe clone | _ZGTtNKSt9exception4whatEv (exported by libstdc++)
|
h <n> _ is a non-virtual offset (n = minus), v <n> _ <m> _ a virtual one: _ZTv0_n24_NSiD1Ev (exported by libstdc++) adjusts this by the value stored 24 bytes before the address point of the vtable.
Pitfalls
- GCC appends clone suffixes after a dot:
.constprop.0,.isra.0,.part.0,.cold; they are not part of the mangling. C1/C2/C3andD0/D1/D2demangle identically; see Platform:Itanium C++ ABI#Conventions.- Type names inside type_info objects are the mangled type without
_Z(St9exception,N3foo3BarE,PKc); prepend_Zand a dummy function to demangle them with tools that only accept full names, or use a demangler's type entry point. - Expressions in template arguments (
X...E,sr,decltype) are the hard part of the grammar; small demanglers (including the one on this wiki) refuse them.
Evidence & references
- Grammar: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling.
- llvm-project 4bd435d59f89ded8e537e5c6e0b560ad89518c3f
libcxxabi/src/demangle/ItaniumDemangle.h(parseSpecialName lines 5551-5690, parseSubstitution 5878-5920, parseCtorDtorName 3570-3601) and the 29,753 test vectors oflibcxxabi/test/DemangleTestCases.inc(lines 550, 554, 533, 809, 26730, 29725, 29726, 29756, 29768, 29769 quoted above). - The
_ZTT,_ZGTt,_ZTv0_n24_andB5cxx11examples are exports of a MinGW-w64 GCC build of libstdc++-6.dll.