Questions tagged [name-mangling]

Name-mangling is a technique used by compilers (mainly C++ compilers) to encode information in strings that can be supported by linkers designed to handle C code.

Questions relating to the schemes used for name mangling, linking problems in the face of mangling, or the tools used to decode mangled names (like c++filt or dem) are appropriate for this tag.

In C++, name mangling is used to encode things like the symbol's namespace and/or function signature.

For example, given this C++ code:

namespace Foo {
namespace Bar {
    class A {
        public:
            A();
            ~A();
    };
}

int f(int i)
{
    Bar::A a;
}

int f(double d)
{
    Bar::A a;
}

}

the g++ compiler on a Linux box might generate the following "mangled" names for the 2 overloads of f() and A's constructor and destructor:

  • _ZN3Foo1fEd
  • _ZN3Foo1fEi
  • _ZN3Foo3Bar1AC1Ev
  • _ZN3Foo3Bar1AD1Ev

Using a tool like c++filt, we can decode these names:

$ echo _ZN3Foo1fEd | c++filt
Foo::f(double) 

$ echo _ZN3Foo3Bar1AC1Ev | c++filt
Foo::Bar::A::A() 

Of course, if you're not using g++ on Linux, the mangled form for your symbols may (likely WILL) be different. (The C++ FAQ lite subtly suggests that if two C++ compilers have different ABIs, even in small details, then they should intentionally have different name mangling schemes, so that things break in an obvious way and not a subtle way.)

316 questions
9
votes
1 answer

CUDA Driver API and Function Mangling

I have a project that requires C++11, so I separate the files into two categories: those that use C++11, and those that use C++03 and hence are compatible with the nvcc compiler. When I have a kernel that is not a template function, it is easy to…
void-pointer
  • 14,247
  • 11
  • 43
  • 61
8
votes
6 answers

View Compiler Mangled Names in C++

How do I view the compiler-generated mangled names for overloaded functions in C++? I'm using VC9 but answers for other compilers are welcome too. Edit: I find all the answers useful here. Accepting the one I liked best.
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
8
votes
1 answer

VC++ prevent all symbol name decorations

I'm working on a DLL which will be used from another language (so no import libs and including the dll's headers) using the _stdcall calling convetion. The problem is that VC++ seems to always do some name decoration on its exported symbols. All the…
SyncViews
  • 125
  • 1
  • 5
8
votes
1 answer

How do I get the mangled name of a NamedDecl in Clang?

I am using Clang to parse some C++ code. I would like to print the name and mangled name for every FunctionDecl that I encounter. I can print the function name fairly easily by adding this to my RecursiveASTVisitor: bool…
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
8
votes
1 answer

Demangling typeclass functions in GHC profiler output

When profiling a Haskell program written in GHC, the names of typeclass functions are mangled in the .prof file to distinguish one instance's implementations of them from another. How can I demangle these names to find out which type's instance it…
Paul Kuliniewicz
  • 2,711
  • 18
  • 24
8
votes
1 answer

64bit name mangling for c++

I have a bit of code which has the following line #pragma comment(linker, "/include:_test@12") The project which uses this code works fine when I compile the code using C++ Visual Studio 2010 with configuration type 32bit (I am also on a 32 bit…
user1612986
  • 1,373
  • 3
  • 22
  • 38
7
votes
1 answer

Why do gcc and clang generate different symbol names for an instantiation of a function template?

Consider the following example: struct A { using type = int; }; template using B = A; template typename B::type f() { return {}; } template B::type f(); Clang generates symbol named int f() while GCC…
vitaut
  • 49,672
  • 25
  • 199
  • 336
7
votes
1 answer

How to automate generating an import library from undecorated stdcall DLL?

The general way to deal with creating LIB from DLL is described in How to make a .lib file when have a .dll file and a header file - still, to create an import library for DLL with undecorated stdcall functions (e.g. core WinAPI DLL's, kernel32.dll…
Ivan Ivanov
  • 115
  • 1
  • 4
7
votes
5 answers

Why doesn't g++ generate "raw" symbols?

From C we know what legal variable names are. The general regex for the legal names looks similar to [\w_](\w\d_)*. Using dlsym we can load arbitrary strings, and C++ mangles names that include @ in the ABI.. My question is: can arbitrary strings be…
Ultimate Hawk
  • 580
  • 4
  • 16
7
votes
3 answers

Is there anything to change the exports name mangling scheme in GCC?

I'm trying to build a project I have and it has several exported functions. The functions follow the stdcall convention and they get mangled if compiled with GCC as Func@X Other compilers mangle the name like this: _Func@X Is any way I can force…
JP.
  • 71
  • 1
  • 2
7
votes
2 answers

How to demangle names in Visual Studio assembler output?

Using Visual Studio 2010/2012, one can compile a c++ source file with the /FAs switch to generate the assembly output of the resulting code. But the generated asm file contains all symbols in their mangled form. Is there a switch or other smart way…
Bloops
  • 485
  • 4
  • 11
7
votes
1 answer

"Private" name mangling and instance vs class attributes

I was writing a decorator that needs to access private variables and found this discrepancy. Can anyone explain this? (Python 2.5) Naming mangling works as expected for attributes defined in the class: >>> class Tester(object): ... __foo =…
Rafe
  • 1,937
  • 22
  • 31
6
votes
3 answers

Is there a way to suppress c++ name mangling?

I have a DLL that is written in C++ and I want to suppress the name mangling for a few exported methods. The methods are global and are not members of any class. Is there a way to achieve this? BTW: I'm using VS2008.
Ohad Horesh
  • 4,340
  • 6
  • 28
  • 45
6
votes
1 answer

What are all the commonly used C++ ABIs and mangling schemes?

The C++ ABI / mangling scheme that gcc/Linux uses is called the Itanium C++ ABI. Mangling is section 5.1. Apart from that one, how many other commonly used C++ ABIs and mangling schemes are there? What are they called? (For example, I assume MSVC…
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
6
votes
2 answers

Why name-mangling has no effect on main function in C++?

C++ compiler often mangle function names to support many features. Programer can suppress default name-mangling using extern "C" way. However, why int main(int, char **) not affected ever? // test.cpp int max(int a, int b) { return a > b ? a :…
Hans Allen
  • 61
  • 3