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
6
votes
1 answer

How to achieve symbol versioning

I want to achieve something like below : I have multiple versions of a library. I dynamically load the latest version of the library using dlopen(). Then I want to see if a particular function (along with similar return type and argument list)…
Pein
  • 1,421
  • 2
  • 11
  • 9
6
votes
1 answer

What does ZTV,ZTS,ZTI mean in the result of gdb x/nfu "vtable_address"?

1. the code class Parent { public: virtual void Foo() {} virtual void FooNotOverridden() {} }; class Derived : public Parent { public: void Foo() override {} }; int main() { Parent p1, p2; Derived d1, d2; } 2. gdb command (gdb)…
JellyHan
  • 63
  • 3
6
votes
0 answers

Named C++ lambdas for profiling and debugging

We use quite a lot of C++11 lambdas in our code, but found that it leads to difficulties with profiling and debugging tools. For instance the MSVC profiler will show lambdas as: `anonymous namespace'::::operator()(void)const `anonymous…
Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
6
votes
1 answer

MSVC name mangling

I am trying to build Lua and QtLua with MSVC 2015 x86 and I have encountered an issue with exported/imported symbols. Here is what I do forbuilding Lua 5.3.2 (source): cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c ren lua.obj lua.o ren luac.obj luac.o link…
Benjamin T
  • 8,120
  • 20
  • 37
6
votes
3 answers

Is C++ name mangling (decoration) deterministic?

I hope to LoadLibrary on an unmanaged C++ DLL with managed code, and then call GetProcAddress on extern functions which have been mangled. My question is are the mangled names you get from a C++ compiler deterministic? That is: Will the name always…
Blanthor
  • 2,568
  • 8
  • 48
  • 66
6
votes
2 answers

How do I unmangle Windows filenames in Java?

From Java, I'm extracting an executable into a location specified using File.createTempFile(). When I try to run my executable, my program hangs when it tries to read the first line of output. I have discovered that if I try to run the same…
skiphoppy
  • 97,646
  • 72
  • 174
  • 218
6
votes
1 answer

What do double underscores indicate?

In Python I know that double underscores preceding a name cause Python to prepend the classname to the variable name as in _classname__variable name (name mangling). So my question is this, in an Python/Django app I have been asked to modify, there…
Gary Ridley
  • 317
  • 1
  • 4
  • 8
6
votes
3 answers

Can I ungarble GCC's RTTI names?

Using gcc, when I ask for an object/variable's type using typeid, I get a different result from the type_info::name method from what I'd expect to get on Windows. I Googled around a bit, and found out that RTTI names are…
Aleph Dvorak
  • 338
  • 1
  • 16
6
votes
4 answers

How to PInvoke an Instance Method by disabling Name Mangling

Given the following c++ class in foo.dll class a{ private: int _answer; public: a(int answer) { _answer = answer; } __declspec(dllexport) int GetAnswer() { return _answer; } } I would like the pInvoke GetAnswer from C#. To do…
Pete Baughman
  • 2,996
  • 2
  • 19
  • 33
6
votes
1 answer

GCC API unable to demangle its own exported symbols

I’m trying to use GCC’s abi::__cxa_demangle to demangle symbols exported from an object file that was produced by g++. However, I invariable get the error mangled_name is not a valid name under the C++ ABI mangling rules Here’s how I call the…
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
6
votes
1 answer

G++ name mangling of global const variables

Can someone help me understanding the gcc name mangling conventions? Consider the following test code #include const int x = 42; int y = 42; int main( int argc, const char* argv[] ) { return 0; } When running nm I get the…
C-Hoernsche
  • 63
  • 1
  • 3
6
votes
4 answers

Dealing with C library anonymous struct types in C++

We have a big, old C++ application with a lot of legacy code and a few external libraries written in C. These libraries are very rarely updated - only if we find a bug and the vendor supplies a patch. This happened last week with one library, and…
l4mpi
  • 5,103
  • 3
  • 34
  • 54
5
votes
1 answer

How can I check if an identifier is dunder or class-private (i.e. will be mangled)?

I'm writing a project that gives advice about variable names, and I want it to tell if a name matches any of the reserved classes of identifiers. The first one ("private") is pretty straightforward, just name.startswith('_'), but dunder and…
wjandrea
  • 28,235
  • 9
  • 60
  • 81
5
votes
1 answer

GNU linker: Adapt to change of name mangling algorithm

I am trying to re-compile an existing C++ application. Unfortunately, I must rely on a proprietary library I only have a pre-compiled static archive of. I use g++ version 7.3.0 and ld version 2.30. Whatever GCC version it was compiled with, it is…
Hermann
  • 604
  • 7
  • 23
5
votes
1 answer

De-mangling export functions in Python

I've been trying to dump all of the imported API function calls for a set of PE files. I have noticed that the majority of the PE files have a set of "weird" looking import functions. These are greatly increasing my number of unique function calls,…
EidolonMK
  • 313
  • 2
  • 9