<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Naren Sirigere's blog</title><link>http://naren.wiki/tags/demangler/</link><description>All blog posts</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><copyright>© 2025 - 2026 by Naren Sirigere</copyright><lastBuildDate>Thu, 11 Jun 2026 00:00:00 +0530</lastBuildDate><atom:link href="http://naren.wiki/tags/demangler/index.xml" rel="self" type="application/rss+xml"/><item><title>What is Name Mangling and Demangling?</title><link>http://naren.wiki/posts/dlang_demangler/</link><pubDate>Thu, 11 Jun 2026 00:00:00 +0530</pubDate><guid>http://naren.wiki/posts/dlang_demangler/</guid><description>&lt;p&gt;Let&amp;rsquo;s start off with explaining what exactly a demangler is, and why modern programming languages absolutely require them.&lt;/p&gt;
&lt;p&gt;A demangler is a tool that takes a &amp;ldquo;mangled&amp;rdquo; symbol name—a uniquely encoded string that includes structural information about a function or variable—and converts it back into a human-readable format. But to understand demanglers, you first need to understand the problem they solve: &lt;strong&gt;Name Mangling&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id="the-problem-naming-conflicts"&gt;The Problem: Naming Conflicts&lt;/h2&gt;
&lt;p&gt;In older, simpler languages like C, a function&amp;rsquo;s name in your source code is typically exactly what gets emitted into the compiled object file. If you write a function &lt;code&gt;add()&lt;/code&gt;, the compiler produces a symbol called &lt;code&gt;add&lt;/code&gt; (sometimes with a leading underscore like &lt;code&gt;_add&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;The linker then takes all your object files, finds all references to the symbol &lt;code&gt;add&lt;/code&gt;, and wires them up to the correct memory address.&lt;/p&gt;
&lt;p&gt;However, modern programming languages introduced powerful abstractions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Function Overloading:&lt;/strong&gt; You can have two functions named &lt;code&gt;add&lt;/code&gt;—one that takes integers, and one that takes floats.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Namespaces and Modules:&lt;/strong&gt; You can have an &lt;code&gt;add&lt;/code&gt; function in the &lt;code&gt;Math&lt;/code&gt; module and another &lt;code&gt;add&lt;/code&gt; function in the &lt;code&gt;Vector&lt;/code&gt; module.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Templates and Generics:&lt;/strong&gt; You can write a generic &lt;code&gt;add&amp;lt;T&amp;gt;&lt;/code&gt; function that gets instantiated differently for dozens of different types.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If the compiler just emitted the symbol &lt;code&gt;add&lt;/code&gt; for all of these variations, the linker would immediately fail with a &amp;ldquo;multiple definition&amp;rdquo; error. The linker doesn&amp;rsquo;t know about C++ classes, Rust traits, or Dlang modules—it only knows about flat strings representing symbols.&lt;/p&gt;
&lt;h2 id="the-solution-name-mangling"&gt;The Solution: Name Mangling&lt;/h2&gt;
&lt;p&gt;To allow the linker to differentiate between all these identically-named functions, the compiler generates a unique string for every single variation. This process is called &lt;strong&gt;Name Mangling&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The compiler takes the function&amp;rsquo;s name and systematically encodes its context (namespaces, classes) and its signature (parameter types, return type, modifiers) into a single, flat ASCII string.&lt;/p&gt;
&lt;p&gt;For example, in C++, a simple function like &lt;code&gt;void Math::add(int, float)&lt;/code&gt; might be mangled into something like:
&lt;code&gt;_ZN4Math3addEif&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s how that breaks down conceptually:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;_Z&lt;/code&gt;: A standard prefix indicating a mangled name.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;N&lt;/code&gt;: Indicates a nested name (like a namespace or class).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;4Math&lt;/code&gt;: The namespace &lt;code&gt;Math&lt;/code&gt; (4 characters long).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;3add&lt;/code&gt;: The function &lt;code&gt;add&lt;/code&gt; (3 characters long).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;E&lt;/code&gt;: End of the nested name.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;i&lt;/code&gt;: Integer parameter.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;f&lt;/code&gt;: Float parameter.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By encoding the exact types and scopes into the string, the linker sees a 100% unique symbol for every overloaded function, completely resolving the naming conflict.&lt;/p&gt;
&lt;h2 id="the-role-of-the-demangler"&gt;The Role of the Demangler&lt;/h2&gt;
&lt;p&gt;While mangled names are great for linkers, they are a nightmare for humans.&lt;/p&gt;
&lt;p&gt;When you get a stack trace from a crash, use a debugger, or use profiling tools, you don&amp;rsquo;t want to see &lt;code&gt;_ZN4Math3addEif&lt;/code&gt; taking up 90% of your CPU time. You want to see &lt;code&gt;Math::add(int, float)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This is exactly what a &lt;strong&gt;Demangler&lt;/strong&gt; does. It understands the specific Application Binary Interface (ABI) and grammar rules of the compiler, parses the mangled string, and reconstructs the original, human-readable function signature.&lt;/p&gt;
&lt;p&gt;Because every programming language (and sometimes different compilers for the same language) designs their mangling grammar differently, demanglers must be specifically written and maintained for each language&amp;rsquo;s ABI.&lt;/p&gt;</description></item></channel></rss>