<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bengisu Batmaz on Medium]]></title>
        <description><![CDATA[Stories by Bengisu Batmaz on Medium]]></description>
        <link>https://medium.com/@bengisu.batmaz?source=rss-7aee69df08cf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*5y30pGPIPypVtqSH.</url>
            <title>Stories by Bengisu Batmaz on Medium</title>
            <link>https://medium.com/@bengisu.batmaz?source=rss-7aee69df08cf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 15:26:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bengisu.batmaz/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Name Mangling with Itanium ABI]]></title>
            <link>https://medium.com/@bengisu.batmaz/name-mangling-with-itanium-abi-00a5c4dbc3c4?source=rss-7aee69df08cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/00a5c4dbc3c4</guid>
            <dc:creator><![CDATA[Bengisu Batmaz]]></dc:creator>
            <pubDate>Mon, 11 Mar 2024 12:39:33 GMT</pubDate>
            <atom:updated>2024-03-11T14:46:14.268Z</atom:updated>
            <content:encoded><![CDATA[<p>In this blog post, we go through the Itanium C++ ABI to demystify C++ name mangling rules.</p><h3><strong>What and Why</strong></h3><p>One of the many improvements of C++ over C is its flexibility in naming functions. With both the ability to define namespaces and support for function overloading; C++ must be able to reflect this hierarchy on its exported symbols. However, the symbol tables that linkers and loaders use to resolve references are flat tables. Since C++ should be able to differentiate two identically named functions under different namespaces (or different type signatures), the compiler should find a way of encoding this additional information into the symbol name. This is the problem that name mangling solves: It is a set of rules for storing this information in the symbol string.</p><p>Officially, C++ does not have one standardized way of mangling symbols. Therefore, different compilers have come up with their own solutions. For instance, the same function can be mangled differently by the MSVC compiler than what GCC would produce, which would be different than Clang’s output. The result was that the older versions of GCC and Clang could not load or link translation units produced by each other. This would not be a problem if the symbols were statically linked during compilation time; because the symbols would be replaced with static jumps and removed after the linking phase anyway. However, with dynamic linking, exported symbols should remain in the symbol table.</p><p>Different system architectures with different application binary interfaces (ABI), in an attempt to standardize how their system loaders reference functions in shared objects, produced their own mangling rules. The good news is that some of the major compilers like GCC and Clang* converged on the <a href="https://itanium-cxx-abi.github.io/cxx-abi/abi.html">Itanium (Intel x64) ABI</a> rules on Unix-based environments. The conformance to the ABI’s mangling standards ensures that an object produced by Clang can be used by GCC (and vice versa) today. With Microsoft having their own ABI for Windows operating systems; compilers on Windows like MSVC do not conform to the Itanium ABI.</p><p><em>(*): In fact, all C++ compilers that are based on LLVM probably conform to the Itanium ABI, based on </em><a href="https://github.com/llvm/llvm-project/tree/124d0b787b5d1ff4aa06bbd61a98d3fc344d0cc6/llvm/lib/Demangle"><em>this package in the LLVM repository</em></a><em>, and not just Clang. Also, note that the LLVM family of C++ compilers do conform to the Windows ABI when compiling to Windows targets. Furthermore, some LLVM-based languages like D and Rust also define their own name-mangling schemes in the LLVM codebase, so unless special directives are added, the artifacts generated by these languages cannot directly be linked with C++ binaries.</em></p><h3>Name Mangling in C</h3><p>We have quoted features like namespaces and polymorphism as the main reasons for name mangling in C++. Naturally, readers may wrongly assume that C, having neither of these features, does not need name mangling at all. It might be the case for *nix environments since they conform with Itanium ABI rules, however, it might not be the case in every platform e.g. Windows. In fact, many online resources claim that name mangling could be disabled for a C++ function by defining it inside an extern “C” block. This is not entirely true, and in many cases, C functions can get mangled as well.</p><p><a href="https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170#FormatC">Windows compilers do this</a> for example. Windows ABI has different call conventions:</p><ul><li>__cdecl is the default call convention for C programs; it adds a leading _. (Example: _func )</li><li>__stdcall is used for Win32 API functions, adds a leading _ and a tailing @ sign, followed by the number of bytes in the parameter list. (Example: _func@5 )</li><li>__fastcall is used for passing arguments in the registers, like __stdcall but with a leading @ sign instead of _. (Example: @func@5 )</li><li>__vectorcall is similar to __fastcall, but with more registers and SIMD extensions. Mangled like __fastcall, but with two leading @ signs. (Example: @@func@5 )</li></ul><p>Among these, __stdcall and __fastcall are x86-only; so specifying a function as __stdcall on a 64-bit (or ARM) platform will not have any effect. On 64-bit environments, only __vectorcall functions are mangled. As for global variables; they are never mangled in C.</p><p>GCC (3.x and higher) can add a leading underscore on C functions on macOS unless an -fno-leading-underscore flag is provided (Note, however, this option can break ABI conformance and your binary might not link or load). For example, the function int my_func() can be exported as _my_func. Some other rules and flags affecting how LLVM decides whether a C function should be mangled or not <a href="https://github.com/llvm/llvm-project/blob/862c7e0218f27b55a5b75ae59a4f73cd4610448d/clang/lib/AST/Mangle.cpp#L105">can be found here</a>.</p><p>Before we move on to our main topic; one final nitpick: Clang actually allows C function overloading via the <a href="https://opensource.apple.com/source/clang/clang-23/clang/tools/clang/docs/LanguageExtensions.html#overloading-in-c">“overloadable” attribute</a>. These functions are then mangled like C++ functions.</p><h3>Itanium C++ ABI</h3><p>Name mangling in Unix systems is governed by the <a href="https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.unscoped-name">Itanium C++ ABI</a> (also known as IA-64 ABI). Since the ABI was initially written for the <a href="https://en.wikipedia.org/wiki/Itanium">Intel Itanium</a> architecture, there are architecture-specific references throughout the document; however, the name mangling section is mostly portable.</p><p>Let’s define two functions named func():</p><pre>int func(){ return 0; } <br>int func(int){ return 1; }</pre><p>The resulting symbols can be something following this pattern:</p><pre>0000000000001124 T <strong>_Z4funci</strong><br>0000000000001119 T <strong>_Z4funcv</strong></pre><p>In the IA-64 convention, all C++ mangled names start with <strong>_Z</strong>. This differentiates C++-mangled symbols; for example, Rust’s mangling scheme starts with <strong>_R</strong>, whereas D language uses <strong>_D</strong> as a prefix.</p><p>Right after _Z comes the “function encoding”, describing the name and location of the function. This can be in one of the following formats:</p><ul><li><strong>Unscoped:</strong> Just a number (indicating name length), then the name string. This indicates a “top-level” symbol. It can also be prefixed with “<strong>St</strong>” which means ::std::.</li><li><strong>Nested: </strong>Starts with <strong>N</strong>, then a bunch of length-name pairs, then <strong>E</strong>. Each string is a scope (e.g. a namespace) and the last one is the symbol name.</li><li><strong>Local: </strong>Starts with <strong>Z</strong>, followed by another function encoding that describes the outer scope of the local function, followed by an <strong>E</strong>, and then the inner function’s encoding.</li></ul><p>Any template arguments are prefixed with an <strong>I</strong>, and followed by an <strong>E</strong>.</p><p>The remaining characters are a list of function parameters. Here; <strong>i</strong> indicates <strong>int</strong>, while <strong>v</strong> indicates <strong>void</strong>. <a href="https://itanium-cxx-abi.github.io/cxx-abi/abi-mangling.html">Here is a full list of all supported type identifiers.</a></p><p>First of all; both of our simple example functions above are unscoped functions. <strong>_Z4funcv </strong>is not prefixed with N, I, Z, or St; it simply starts with a number. Because it says 4; we read the next 4 characters: The function name is “func”. Since the only remaining character is “v”, indicating void; then the function signature for _Z4funcv is func(void), or simply func(). Similarly; _Z4funci is demangled as func(int). Note that we do not specify return types in any of the demangled symbols: <strong>Itanium ABI does not encode return types in C++ symbols (MSVC does)</strong>.</p><p>If we wanted to mangle myNamespace::inner::func(std::string, int); we would use the nested encoding:</p><ul><li>Start with _Z to indicate C++ name-mangling,</li><li>Add N for nested encoding,</li><li>Length of myNamespace (11), then the string itself,</li><li>Length of inner (5), then the string itself,</li><li>Length of func (4), then the string itself,</li><li>End the list with E,</li><li>Parameters: Ss for std::string, i for int.</li></ul><p>Put together; the mangled symbol would be: _ZN11myNamespace5inner4funcESsi</p><p>We can use the c++filt command to check the demangled symbol:</p><p>$ c++filt -n _ZN11myNamespace5inner4funcESsi</p><pre>myNamespace::inner::func(std::string, int)</pre><p>Let’s try to mangle the following constructor:</p><pre>MyMap&lt;int, std::string const&amp;&gt;::MyMap(std::vector&lt;std::string const&amp;&gt;)</pre><p>It might be better to approach this in parts. Let us first mangle the MyMap&lt;int, std::string const&amp;&gt; part:</p><ul><li>Start with 5MyMap.</li><li>Define the template arguments: starting with I (Which is basically &lt; in the type).</li><li>The first type is integer; add an i.</li><li>The second is a std::string, we have a shortcut for this: Ss. The string is passed as a reference, not a value; so we prefix it with R. Also, the const keyword is there; which we can describe using K. Together they become RKSs (the ordering matters here).</li><li>To end the template arguments, we add an E (Which corresponds to “&gt;”).</li></ul><p>Together, MyMap&lt;int, std::string const&amp;&gt; becomes 5MyMapIiRKSsE. Next, we should define the nested symbol with the constructor:</p><ul><li>Start with N for local scope description.</li><li>Add the first scope name. We already wrote this as 5MyMapIiRKSsE.</li><li>The second scope name is just the constructor operand; which is C1.</li><li>Finish the scope list with E.</li></ul><p>Now we should pass the constructor parameter list.</p><ul><li>For std::vector; we should first grab the std prefix using the St shortcut.</li><li>Encode the vector name as 6vector.</li><li>Vector is generic, open an angle bracket with I.</li><li>We know how to encode std::string const&amp;, add RKSs.</li><li>Close the bracket with E.</li></ul><p>If we put them all together, we get _ZN5MyMapIiRKSsEC1ESt6vectorIRKSsE, which c++filt demangles as:</p><p>$ c++filt -n _ZN5MyMapIiRKSsEC1ESt6vectorIRKSsE</p><pre>MyMap&lt;int, std::string const&amp;&gt;::MyMap(std::vector&lt;std::string const&amp;&gt;)</pre><p>One last thing to do here: We wrote the std::string const&amp; part twice. There are only 4 characters (RKSs) here, but in other cases, replicating a type description can be verbose and error-prone. So the spec has a “substitution encoding” feature for us. Instead of RKSs we can use S1_ here: S identifies a substitution, 1 is the “sequence index” of the std::string const&amp; type in our demangled string, and the substitution always ends with an underscore. The output is identical, and we shaved off a byte here:</p><p>$ c++filt -n _ZN5MyMapIiRKSsEC1ESt6vectorIS1_E</p><pre>MyMap&lt;int, std::string const&amp;&gt;::MyMap(std::vector&lt;std::string const&amp;&gt;)</pre><h3>Conclusion</h3><p>With C++ specification growing every day with new features and concepts; the full name mangling specification is getting larger and more complicated. Template expressions, variadic arguments, lambda functions, anonymous unions, decltype, virtual tables, custom ABI tags and literals are just a portion of things that you can express with the Itanium ABI’s mangling specification. C++ is a vast language, and its sheer size shows itself in the mangling specification. And we did not even delve into the MSVC spec!</p><p>Even then, knowing the basics of how name mangling works makes those complicated symbols much more approachable. Being able to read some type information, and knowing where to look when something novel appears can be an invaluable tool when debugging. So, whether you are a seasoned developer or just starting your programming journey, taking the time to comprehend name mangling and its implications can help tremendously in your projects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=00a5c4dbc3c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>