<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>vtables under the surface on Pierre Gradot</title>
		<link>https://gradot.dev/blog/series/vtables-under-the-surface/</link>
		<description>Recent content in vtables under the surface on Pierre Gradot</description>
		<generator>Hugo</generator>
		<language>en-us</language>
		
		
		
		
			<lastBuildDate>Sun, 07 Jan 2024 20:00:00 +0100</lastBuildDate>
		
			<atom:link href="https://gradot.dev/blog/series/vtables-under-the-surface/index.xml" rel="self" type="application/rss+xml" />
			<item>
				<title>vtables under the surface | Episode 3 - How virtual functions are actually called</title>
				<link>https://gradot.dev/blog/posts/2024/vtables-3-construction-and-call/</link>
				<pubDate>Sun, 07 Jan 2024 20:00:00 +0100</pubDate>
				<guid>https://gradot.dev/blog/posts/2024/vtables-3-construction-and-call/</guid>
				<description>&lt;p&gt;In this episode, we will see how invoking a virtual function in C++ translates into assembly instructions. We will see how our class instance is constructed and how it relates to the vtable. Then, we will see how this vtable is used to call the appropriate function.&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;If you have actually built the project and analyzed the binary in the previous episode, don&amp;rsquo;t forget to remove the &lt;code&gt;-fno-rtti&lt;/code&gt; option and to rebuild the project. I will use this binary as a reference here.&lt;/p&gt;&#xA;&lt;/blockquote&gt;</description>
			</item>
			<item>
				<title>vtables under the surface | Episode 2 - ELF files</title>
				<link>https://gradot.dev/blog/posts/2024/vtables-2-elf/</link>
				<pubDate>Sat, 06 Jan 2024 20:00:00 +0100</pubDate>
				<guid>https://gradot.dev/blog/posts/2024/vtables-2-elf/</guid>
				<description>&lt;p&gt;In this episode, we will explore what vtables mean in terms of bytes within ELF files.&lt;/p&gt;&#xA;&lt;h2 id=&#34;build-output&#34;&gt;Build Output&lt;/h2&gt;&#xA;&lt;p&gt;On Linux, GCC produces ELF files as the result of the compilation process. In our project, the file is &lt;code&gt;a.out&lt;/code&gt;, and we can use the &lt;code&gt;file&lt;/code&gt; command to get details about it:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;$ file a.out &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;a.out: ELF 64-bit LSB pie executable, x86-64, version &lt;span class=&#34;m&#34;&gt;1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;SYSV&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;, dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID&lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;sha1&lt;span class=&#34;o&#34;&gt;]=&lt;/span&gt;a87e1cb2356338a14f1a9aa2fef85fb7036bee65, &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; GNU/Linux 3.2.0, not stripped&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If the compiler has generated vtables for &lt;code&gt;Base&lt;/code&gt; and &lt;code&gt;Derived&lt;/code&gt;, there must be corresponding symbols and bytes in the binary.&lt;/p&gt;</description>
			</item>
			<item>
				<title>vtables under the surface | Episode 1 - Concepts</title>
				<link>https://gradot.dev/blog/posts/2024/vtables-1-concepts/</link>
				<pubDate>Fri, 05 Jan 2024 20:00:00 +0100</pubDate>
				<guid>https://gradot.dev/blog/posts/2024/vtables-1-concepts/</guid>
				<description>&lt;p&gt;If you&amp;rsquo;ve been around C++ for a while, you&amp;rsquo;ve likely come across the terms &amp;ldquo;vtable&amp;rdquo;, &amp;ldquo;virtual table&amp;rdquo; or &amp;ldquo;&lt;a href=&#34;https://en.wikipedia.org/wiki/Virtual_method_table&#34;&gt;virtual method table&lt;/a&gt;&amp;rdquo;. Vtables are not part of the C++ standard, even though this concept pops up almost immediately when you try to understand how virtual functions actually work in C++. Indeed, vtables are the most common implementation of polymorphism in C++.&lt;/p&gt;&#xA;&lt;p&gt;You may also have already encountered a cryptic compilation error like &amp;ldquo;undefined reference to &lt;code&gt;vtable for MyClass&lt;/code&gt;&amp;rdquo;. This may have left you perplexed because you didn&amp;rsquo;t explicitly create anything named &amp;ldquo;vtable&amp;rdquo; in your code. This error signals that, under the hood, the compiler generates vtables to handle virtual functions. If you&amp;rsquo;re curious about this error, here is a &lt;a href=&#34;https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable&#34;&gt;good discussion&lt;/a&gt; on Stack Overflow (the &lt;a href=&#34;https://stackoverflow.com/a/57504289&#34;&gt;second answer&lt;/a&gt; is particularly enlightening).&lt;/p&gt;</description>
			</item>
	</channel>
</rss>
