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
29
votes
6 answers

questions about name mangling in C++

I am trying to learn and understand name mangling in C++. Here are some questions: (1) From devx When a global function is overloaded, the generated mangled name for each overloaded version is unique. Name mangling is also applied to variables.…
Tim
  • 1
  • 141
  • 372
  • 590
29
votes
3 answers

How to make gdb show the original non-mangling function name on disassembly model?

void outputString(const char *str) { cout << "outputString(const char *str) : " << str << endl; } turns out to be Dump of assembler code for function _Z12outputStringPKc: 0x004013ee <_Z12outputStringPKc+0>: push ebp 0x004013ef…
Jichao
  • 40,341
  • 47
  • 125
  • 198
24
votes
2 answers

Why do I have two destructor implementations in my assembly output?

And objdump of my .o file reveals that I have two different destructors for the same class. Why? Disassembly of section .text._ZN1AD0Ev: 0000000000000000 <_ZN1AD0Ev>: 0: 53 push %rbx 1: be 00 00 00 00 mov …
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
23
votes
1 answer

GCC C++ Name mangling reference

Looking around, I see mostly questions about demangling C++ symbols rather than how to mangle them. Yes, one could invoke g++, using the -S option, on some dummy code containing the symbols to be mangled, and then examine the resulting assembly, but…
Mona the Monad
  • 2,265
  • 3
  • 19
  • 30
23
votes
1 answer

In the Itanium C++ ABI, why does the mangled name for template functions not resolve dependent typedefs?

For example: template struct foo { using bar = int; }; // _Z3bazi void baz(foo::bar quux) { } template void baz(typename foo::bar quux) { } // _Z3bazIiEvN3fooIT_E3barE template void baz(foo::bar…
Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
21
votes
4 answers

get a c++ function mangled name at compile time (or runtime)

I have a function class method, ValueHolder::printValue class ValueHolder { public: void printValue (); } ; How do I determine its mangled name at compile time (or runtime). For instance I would like to do this: const char *mangled_name =…
iamacomputer
  • 518
  • 3
  • 14
20
votes
1 answer

What does the GCC function suffix "isra" mean?

While profiling a program compiled with gcc, I noticed functions like foo.isra.3. What does isra indicate? I notice that one of the functions is only called in a few places, and one of the arguments is always specified as a literal value. Maybe it…
z0r
  • 8,185
  • 4
  • 64
  • 83
19
votes
3 answers

typeid() returns extra characters in g++

class foo { public: void say_type_name() { std::cout << typeid(this).name() << std::endl; } }; int main() { foo f;; f.say_type_name(); } Above code prints P3foo on my ubuntu machine with g++. I am not getting why it is printing P3foo…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184
19
votes
3 answers

Function to mangle/demangle functions

I have previously, here, been shown that C++ functions aren't easily represented in assembly. Now I am interested in reading them one way or another because Callgrind, part of Valgrind, show them demangled while in assembly they are shown…
Syntax_Error
  • 5,964
  • 15
  • 53
  • 73
19
votes
1 answer

What does the GCC function suffix .constprop mean?

Looking at the disassembly of a C++ program, I see functions like _Z41__static_initialization_and_destruction_0ii.constprop.221. What does the constprop mean in this case? It appears to be similar in appearance to the isra suffix (and is sometimes…
nneonneo
  • 171,345
  • 36
  • 312
  • 383
18
votes
4 answers

How to unmangle mangled names of C++ lambdas?

After compilation with g++-4.9.3 -std=c++11 the code #include #include using namespace std; int main() { cout << typeid([]{}).name() << endl; } outputs Z4mainEUlvE_ as the mangled name of the given lambda on Linux x86_64.…
jotik
  • 17,044
  • 13
  • 58
  • 123
18
votes
2 answers

What does '_GLOBAL__sub_I_' mean in nm output?

While I was trying to resolve a problem in static linking, I encounter a couple of _GLOBAL__sub_I_ prefixes in front of symbol names. It appears in that form although I used nm --demangle(-C). I stumbled upon this answer (How to find global static…
CremeBaldEagle
  • 191
  • 1
  • 1
  • 4
17
votes
2 answers

stdcall name mangling using extern c and dllexport vs module definitions (msvc++)

I was trying to export a simple test function for a dll to work with an application (fyi: mIRC) that specifies the calling convention as: int __stdcall test_func(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause) Now, to call…
mina
  • 439
  • 2
  • 7
  • 13
17
votes
3 answers

C++ template-argument dependent decltype in ABI mangled name

Consider the following function: template auto Min(A&& a, B&& b) -> decltype(a < b ? std::forward(a) : std::forward(b)) { return a < b ? std::forward(a) : std::forward(b); } The fragment Min(0, 1)…
Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
17
votes
4 answers

What is the benefit of private name mangling?

Python provides private name mangling for class methods and attributes. Are there any concrete cases where this feature is required, or is it just a carry over from Java and C++? Please describe a use case where Python name mangling should be used,…
cmcginty
  • 113,384
  • 42
  • 163
  • 163
1
2
3
21 22