Skip to content

Commit

Permalink
added name mangling
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh672003 committed Jan 14, 2024
1 parent abb1613 commit 788a1f9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
65 changes: 65 additions & 0 deletions src/35-name-mangling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Name Mangling

Name mangling in C++ is a technique used to resolve conflicts between identifiers that have the same name but belong to
different scopes or have different types. This process transforms function names into unique identifiers that can be
distinguished by the linker during the linking phase of compilation.

However, it's important to note that name mangling is platform-specific. The C++ ABI (Application Binary Interface) of
the platform you are running on defines the name mangling. There are two common C++ ABIs, which are the Itanium ABI and
Microsoft's ABI. Depending on the platform and the compiler being used, different name mangling schemes may be applied .

The C++ standard does not define a specific naming convention for mangled names, so different compilers may implement
name mangling differently. However, mangled names typically start with \_Z followed by the mangled name of the function
or variable, including namespace, classes, and name, with a length and the identifier 1.

Here's an example of name mangling in action:

```cpp
namespace MyNamespace {
class MyClass {
public:
void myMethod(int param);
};
} // namespace MyNamespace
```
The mangled name of `myMethod` might look something like `_ZN11MyNamespace7MyClass9myMethodEi`, which includes the
namespace (`MyNamespace`), the class (`MyClass`), the method name (`myMethod`), and the parameter type (`int`).
To understand the mangled names, you can use a tool like `c++filt`. `c++filt` is a command-line utility that comes with
the GNU Binutils package and is used to demangle C++ symbols. By piping the output of `nm` (another command-line utility
that lists symbols from object files) through `c++filt`, you can see the demangled names of the symbols.
Here's how you might use `c++filt`:
```bash
nm myprogram.o | c++filt
```

This command will print out the names of all symbols in `myprogram.o`, with the mangled names replaced by their
demangled equivalents.

Output:

```
0000000000003df0 d _DYNAMIC
0000000000003fe8 d _GLOBAL_OFFSET_TABLE_
0000000000002000 R _IO_stdin_used
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
0000000000002004 r __GNU_EH_FRAME_HDR
0000000000004010 D __TMC_END__
0000000000004010 B __bss_start
w __cxa_finalize@GLIBC_2.2.5
0000000000004000 D __data_start
0000000000004008 D __dso_handle
w __gmon_start__
U __libc_start_main@GLIBC_2.34
0000000000004010 D _edata
0000000000004018 B _end
000000000000112c T _fini
0000000000001000 T _init
0000000000001020 T _start
0000000000004000 W data_start
0000000000001119 T main
```
3 changes: 2 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- [Structuring-codebase](18-structuring-codebase.md)
- [Scope](19-scope.md)
- [Namespace](20-namespace.md)
- [Dont use namesapce std](20.1-dont-use-namesapce-std.md)
- [Dont use namespace std](20.1-dont-use-namespace-std.md)
- [Code-splitting](21-code-splitting.md)
- [Forward-declaration](22-forward-declaration.md)
- [Structs-classes](23-structs-classes.md)
Expand All @@ -39,3 +39,4 @@
- [Type-casting](32-type-casting.md)
- [Standard-template-library](33-standard-template-library.md)
- [Templates](34-templates.md)
- [Name Mangling](35-name-mangling.md)

0 comments on commit 788a1f9

Please sign in to comment.