name-mangling
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| name-mangling [February 16, 2026 at 01:45] – yanevskiv | name-mangling [May 14, 2026 at 11:38] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | # Name mangling | ||
| + | **Name mangling** is how C++ compilers encode object types then and reconstruct the type back. | ||
| + | |||
| + | Let's say we got a following piece of C++ code. | ||
| + | ```cpp | ||
| + | namespace Hello { | ||
| + | struct World { | ||
| + | template < | ||
| + | return x + y; | ||
| + | } | ||
| + | }; | ||
| + | } | ||
| + | ``` | ||
| + | We use it in the following way: | ||
| + | ```cpp | ||
| + | int main() | ||
| + | { | ||
| + | Hello:: | ||
| + | return world.add(2, | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | The question is how does this translate to assembly language? The assembly language knows nothing about C++ syntax (namespaces, | ||
| + | |||
| + | The answer is a type like `Hello:: | ||
| + | |||
| + | - `_Z` - All mangled names start with this | ||
| + | - `N` - Namespace start | ||
| + | - `5Hello` - 5 character identifier (" | ||
| + | - `5World` - 5 character identifier (" | ||
| + | - `3add` - 3 character identifier (" | ||
| + | - `IiE` - Template implemented as `T = int` (i means int) | ||
| + | - `E` - Namespace end | ||
| + | - `T_` - Return type is the same as template type | ||
| + | - `S2_` - First argument as well | ||
| + | - `S2_` - Second argument as well | ||
| + | |||
| + | Assembly code: | ||
| + | ``` | ||
| + | .weak | ||
| + | .type | ||
| + | _ZN5Hello5World3addIiEET_S2_S2_: | ||
| + | push rbp | ||
| + | mov rbp, rsp | ||
| + | mov QWORD PTR -8[rbp], rdi | ||
| + | mov DWORD PTR -12[rbp], esi | ||
| + | mov DWORD PTR -16[rbp], edx | ||
| + | mov edx, DWORD PTR -12[rbp] | ||
| + | mov eax, DWORD PTR -16[rbp] | ||
| + | add eax, edx | ||
| + | pop rbp | ||
| + | ret | ||
| + | ``` | ||
| + | |||
| + | ## C | ||
| + | In C programming language symbols have the same name as they do in the code. You can tell by running `gcc -masm=intel -S func.c -o func.s` on this following code and looking at assembly `main.s` | ||
| + | |||
| + | The symbol created by the label `func:` has the same name as the function `func()`. | ||
| + | |||
| + | ```c | ||
| + | /* | ||
| + | | ||
| + | | ||
| + | ----------------------------------------------- | ||
| + | .file " | ||
| + | .intel_syntax noprefix | ||
| + | .text | ||
| + | .globl | ||
| + | .type func, @function | ||
| + | func: | ||
| + | .LFB0: | ||
| + | push rbp | ||
| + | mov rbp, rsp | ||
| + | mov DWORD PTR -4[rbp], edi | ||
| + | mov DWORD PTR -8[rbp], esi | ||
| + | mov edx, DWORD PTR -4[rbp] | ||
| + | mov eax, DWORD PTR -8[rbp] | ||
| + | add eax, edx | ||
| + | pop rbp | ||
| + | ret | ||
| + | .LFE0: | ||
| + | .size func, .-func | ||
| + | .ident | ||
| + | .section | ||
| + | */ | ||
| + | |||
| + | int func(int x, int y) | ||
| + | { | ||
| + | return x + y; | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | ## C++ | ||
| + | In C++ programming language, symbol names are mangled. They' | ||
| + | |||
| + | When you name the exact same code `func.cpp` and compile it with `g++` to assembly, using `g++ -masm=intel -S func.cpp -o func.s`, then | ||
| + | |||
| + | The symbol created by the label `_Z4funcii` | ||
| + | ```cpp | ||
| + | /* | ||
| + | | ||
| + | | ||
| + | -------------------------------------------------- | ||
| + | .file " | ||
| + | .intel_syntax noprefix | ||
| + | .text | ||
| + | .globl | ||
| + | .type | ||
| + | _Z4funcii: | ||
| + | .LFB0: | ||
| + | push rbp | ||
| + | mov rbp, rsp | ||
| + | mov DWORD PTR -4[rbp], edi | ||
| + | mov DWORD PTR -8[rbp], esi | ||
| + | mov edx, DWORD PTR -4[rbp] | ||
| + | mov eax, DWORD PTR -8[rbp] | ||
| + | add eax, edx | ||
| + | pop rbp | ||
| + | ret | ||
| + | .LFE0: | ||
| + | .size | ||
| + | .ident | ||
| + | .section | ||
| + | */ | ||
| + | |||
| + | int func(int x, int y) | ||
| + | { | ||
| + | return x + y; | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | ## Itanium ABI | ||
| + | |||
| + | - https:// | ||
| + | |||
| + | ## Links | ||
| + | |||
| + | - https:// | ||
