c++filt Name Demangling Utility

Note:

This section applies to C++ programs only. C language programs do not use name mangling.

When the IBM C and C++ Compilers compiles a C++ program, it encodes all function names and certain other identifiers to include type and scoping information. This encoding process is called mangling. The linker uses these mangled names to ensure type-safe linkage. These mangled names appear in the object files and final executable file.

Tools that use these files, the AIX dump utility for example, have only the mangled names and not the original source-code names, and present the mangled name in their output. This output is undesirable because the names are difficult to read.

Two utilities convert the mangled names to their original source code names:

c++filt A filter that demangles (decodes) mangled names.
demangle.h A class library that you can use to develop tools to manipulate mangled names.

 

Demangling Compiled C++ Names with c++filt
The c++filt utility converts mangled names to demangled names. You can enter any mangled name and obtain the demangled name. The filter copies characters from the given file names or standard input to standard output, replacing all mangled names with their corresponding demangled names.

To use the c++filt utility, type /usr/ibmcxx/bin/c++filt followed by any options, on the shell command line.

 

Syntax of the c++filt Utility

You can select one or more of the following options:

-m Produces a symbol map on standard output. This map contains a list of the mangled names and their corresponding demangled names. This output follows the filtered output.
-s Produces a side-by-side demangling, with each mangled name encountered in the input stream replaced by a combination of the demangled name followed by the original mangled name.
-w width Prints demangled names in fields width characters wide. If the name is shorter than width, it is padded on the right with blanks; if longer, it is truncated to width.
-C Demangles stand-alone class names such as Q2_1X1Y.
-S Demangles special compiler generated symbol names such as __vft1X.
filename Is the name of the file containing the mangled names you want to demangle. You can specify more than one filename.

 

The following example shows the symbols contained in an object file functions.o, producing a side-by-side listing of the mangled and demangled names with a field width of 40 characters:

dump -tv functions.o | c++filt -s -w 40

 

Demangling Compiled C++ Names with the demangle Class Library
The demangle class library contains a small class hierarchy that client programs can use to demangle names and examine the resulting parts of the name. It also provides a C-language interface for use in C programs. Although it is a C++ library, it uses no external C++ features so you can link it directly to C programs. Demangle is included as part of libC.a, and is automatically linked, when required, if libC.a is linked. If you do not want to link to libC.a, for example, if you want to use demangle with a C program, you can explicitly link to /usr/ibmcxx/lib/libdemangle.a.

You can write programs that use demangle.h to take a mangled name and return the demangled name, and the separate parts of the name. For example, given the mangled name of a member function, you can:

 

Using demangle.h in C++ Programs
To demangle a name (represented as a character array), create a dynamic instance of Name class, providing the character string to the class's constructor. For example, if the compiler mangled X::f(int) to the mangled name f__1XFi, in order to demangle the name, enter:

char *rest;
Name *name = Demangle("f__1XFi", rest) ;

The demangler classifies names into four categories: functions, member functions, variables, and member variables. Once your program constructs an instance of class Name, your program can ask what kind of Name the instance is, using the Kind method of Name. Based on the kind of name returned, the program can ask for the text of the different parts of the name, or the text of the entire name.

For the mangled name f__1XFi example, you can find out the following:

name->Kind() == MemberFunction
((MemberFunctionName *)name)->Scope()->Text() is "X"
((MemberFunctionName *)name)->RootName() is "f"
((MemberFunctionName *)name)->Text() is "X::f(int)"

If the supplied character string is not a name that requires demangling, because the original name was not mangled, the Demangle function returns NULL.

For further details about the demangle.h library and the C++ and C interfaces, look at the comments in the library's header file, /usr/ibmcxx/include/demangle.h.