Personal tools
PGI Workstation User's Guide - 13 C++ Name Mangling
13 C++ Name Mangling
Name mangling transforms the names of entities so that the names include information on aspects of the entity's type and fully qualified name. This is necessary since the intermediate language into which a program is translated contains fewer and simpler name spaces than there are in the C++ language. Specifically:
- Overloaded function names are not allowed in the intermediate language.
- Classes have their own scopes in C++, but not in the generated intermediate language. For example, an entity x from inside a class must not conflict with an entity x from the file scope.
- External names in the object code form a completely flat name space. The names of entities with external linkage must be projected onto that name space so that they do not conflict with one another. A function f from a class A, for example, must not have the same external name as a function f from class B.
- Some names are not names in the conventional sense of the word, they're not strings of alphanumeric characters, for example operator=.
We can see that there are two problems here:
- Generating external names that will not clash.
- Generating alphanumeric names for entities with strange names in C++.
Name mangling solves these problems by generating external names that will not clash, and alphanumeric names for entities with strange names in C++. It also solves the problem of generating hidden names for some behind-the-scenes language support in such a way that they will match up across separate compilations.
You will see mangled names if you view files that are translated by PGCC C++, and you do not use tools that demangle the C++ names. Intermediate files that use mangled names include the assembly and object files created by the pgCC command and the C-like file that can be viewed as output from pgCC using the +i command-line option
The name mangling algorithm for the PGCC C++ compiler is the same as that for cfront, and also matches the description in Section 7.2, Function Name Encoding, of The Annotated C++ Reference Manual (except for some minor details). Refer to the ARM for a complete description of name mangling.
13.1 Types of Mangling
The following entity names are mangled:
- Function names including non-member function names are mangled, to deal with overloading. Names of functions with extern "C" linkage are not mangled.
- Mangled function names have the function name followed by __ followed by F followed by the mangled description of the types of the parameters of the function. If the function is a member function, the mangled form of the class name precedes the F. If the member function is static, an S also precedes the F.
int f(float); // f__Ff
class A
int f(float); // f__1AFf
static int g(float); // g__1ASFf
;
- Special and operator function names, like constructors and operator=(). The encoding is similar to that for normal functions, but a coded name is used instead of the routine name:
class A
int operator+(float); // __pl__1Aff
A(float); // __ct__1Aff
;
int operator+(A, float); // __pl__F1Af
- Static data member names. The mangled form is the member name followed by __ followed by the mangled form of the class name:
class A
static int i; // i__1A
;
- Names of variables generated for virtual function tables. These have names like vtblmangled-class-name or vtblmangled-base-class-namemangled-class-name.
- Names of variables generated to contain runtime type information. These have names like Ttype-encoding and TIDtype-encoding.
13.2 Mangling Summary
This section lists some of the C++ entities that are mangled and provides some details on the mangling algorithm. For more details, refer to The Annotated C++ Reference Manual.
13.2.1 Type Name Mangling
Using PGCC C++, each type has a corresponding mangled encoding. For example, a class type is represented as the class name preceded by the number of characters in the class name, as in 5abcde for abcde. Simple types are encoded as lower-case letters, as in i for int or f for float. Type modifiers and declarators are encoded as upper-case letters preceding the types they modify, as in U for unsigned or P for pointer.
13.2.2 Nested Class Name Mangling
Nested class types are encoded as a Q followed by a digit indicating the depth of nesting, followed by a _, followed by the mangled-form names of the class types in the fully-qualified name of the class, from outermost to innermost:
class A
class B // Q2_1A1B
;
;
13.2.3 Local Class Name Mangling
The name of the nested class itself is mangled to the form described above with a prefix __, which serves to make the class name distinct from all user names. Local class names are encoded as L followed by a number (which has no special meaning; it's just an identifying number assigned to the class) followed by __ followed by the mangled name of the class (this is not in the ARM, and cfront encodes local class names slightly differently):
void f()
class A // L1__1A}
;
;
This form is used when encoding the local class name as a type. It's not necessary to mangle the name of the local class itself unless it's also a nested class.
13.2.4 Template Class Name Mangling
Template classes have mangled names that encode the arguments of the template:
template<class T1, class T2> class abc ;
abc<int, int> x;
abc__pt__3_ii
This describes two template arguments of type int with the total length of template argument list string, including the underscore, and a fixed string, indicates parameterized type as well, the name of the class template.
<< " border=0> > " border=0>