<< >> Title Contents Index Home Help

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:

We can see that there are two problems here:

  1. Generating external names that will not clash.
  2. 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:

int f(float);              // f__Ff 
class A
int f(float); // f__1AFf
static int g(float); // g__1ASFf
class A 
int operator+(float); // __pl__1Aff
A(float); // __ct__1Aff
int operator+(A, float); // __pl__F1Af
class A
static int i; // i__1A

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;

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.

14 The PGPROF Profiler

<< >> Title Contents Index Home Help