From 26661fda5af5407e1a69baa22063625f45e0c908 Mon Sep 17 00:00:00 2001 From: Noratrieb <48135649+Noratrieb@users.noreply.github.com> Date: Thu, 13 Feb 2025 22:58:06 +0100 Subject: [PATCH] finish lesson 1 --- lesson-1.html | 137 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 133 insertions(+), 4 deletions(-) diff --git a/lesson-1.html b/lesson-1.html index 1540875..0667f81 100644 --- a/lesson-1.html +++ b/lesson-1.html @@ -48,8 +48,12 @@ void f() {} This empty function will be mangled to _Z1fv. The 1f signifies the name (we will look at this in more detail later in this lesson) and the v signifies the - function type (which we will visit in more detail in a future - lesson). + function type. +

+

+ We will see the v function type a lot in the rest of + this guide. It stands for a function that takes no arguments and + returns void.

@@ -79,9 +83,31 @@ void f() {}

-

+

+ For names, there are two cases to consider for now. Either the name + is in the global scope, or it is in a namespace. +

+

For global names, we just prefix the name with its length.

+
+void hello_world() {}
+          
+

+ This will therefore get mangled as _Z11hello_worldv. + The length of hello_world is 11, so we concatenate + 11 and hello_world. This entire thing is + then appended to the previously mentioned prefix _Z and + then we add the type, which is just v here, at the end. +

-
+

What is the mangling of the following identifier?

+
+void meow() {}
+            
+
+
+

+ Functions that are declared in a namespace get a bit more + complicated. They are referred to as nested names, because + they are nested in a namespace. They can also be nested in + multiple namespaces, the encoding is the same. +

+

+ Nested names start with an N and end with an + E. Between those two letters, the hierarchy of the + namespace is represented by putting on namespace name after another, + with the function name last. Every name has the leading length and + then the name itself, just like with global names. +

+
+namespace outer {
+  void inner() {}
+}
+          
+

+ That means that this function will be mangled as + _ZN5outer5innerEv. We can decode this into the + following structure +

+ +

Nested namespaces follow the same structure.

+
+namespace a {
+  namespace b {
+    namespace c {
+      void inner() {}
+    }
+  }
+}
+          
+

+ This function will mangle as _ZN1a1b1c5innerEv. We get + all the concatinated names as 1a1b1c5inner, with the + previously mentioned characters around them. +

+ +
+

What is the mangling of the following identifier?

+
+namespace cats {
+  namespace like {
+    void meow() {}
+  }
+}
+            
+ + + +
+ +
+
+
+

+ Good job! You have successfully answered all the question and now + know the basic makeup of an Itanium-mangled C++ symbol. +

+

+ In the next lesson, we will use this knowledge to look at basic + function types beyond v. Mangling function types is + important for function overloading, but I don't want to overload you + with information, so feel free to take a break and let the previous + knowledge sink in. +

+

+ If you want to try out more code and look at its mangling, I + recommend using Compiler Explorer on + godbolt.org. Under "Output", you + can uncheck the box to demangle identifiers to see the mangled + identifiers for any C++ code you enter on the left. +

+
+ + Lesson 2: something that has not been written yet. + +
+