<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Dlopen on Cheat Sheet</title><link>/tags/dlopen/</link><description>Recent content in Dlopen on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/dlopen/index.xml" rel="self" type="application/rss+xml"/><item><title>Shared Libraries</title><link>/linux/system-programming/shared-libraries/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/linux/system-programming/shared-libraries/</guid><description>&lt;p&gt;Based on &lt;em&gt;The Linux Programming Interface&lt;/em&gt; Chapter 41 (Fundamentals of Shared Libraries) and Chapter 42 (Advanced Features of Shared Libraries).&lt;/p&gt;
&lt;h1 id="shared-libraries"&gt;Shared Libraries&lt;a class="anchor" href="#shared-libraries"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;hr&gt;
&lt;h2 id="1-header-vs-static-vs-shared-library-tlpi-ch-41"&gt;1. Header vs Static vs Shared Library (TLPI Ch 41)&lt;a class="anchor" href="#1-header-vs-static-vs-shared-library-tlpi-ch-41"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C libraries (eg. &lt;code&gt;glibc&lt;/code&gt;) separate compile-time declarations from link-time and runtime binaries.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Artifact&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Extension &amp;amp; Example Path&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Role&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Phase&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Header&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;.h&lt;/code&gt; (&lt;code&gt;/usr/include/dlfcn.h&lt;/code&gt;)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Plain-text function signatures, types, and macros&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compile time (&lt;code&gt;gcc -c&lt;/code&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Static Library&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;.a&lt;/code&gt; (&lt;code&gt;libc.a&lt;/code&gt;)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Archive of &lt;code&gt;.o&lt;/code&gt; object files copied directly into the executable&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Link time (&lt;code&gt;gcc -static&lt;/code&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Shared Library&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;.so&lt;/code&gt; (&lt;code&gt;/lib/x86_64-linux-gnu/libc.so.6&lt;/code&gt;)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Precompiled Position-Independent machine code loaded once into memory and shared across processes&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Runtime (&lt;code&gt;ld.so&lt;/code&gt; dynamic linker)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="building-and-inspecting-shared-libraries"&gt;Building and Inspecting Shared Libraries&lt;a class="anchor" href="#building-and-inspecting-shared-libraries"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ gcc -fPIC -shared -Wall -o libfoo.so foo.c
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ ldd &amp;lt;binary&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ nm -D libfoo.so&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-fPIC&lt;/code&gt;: Generate Position-Independent Code so the library can be mapped at any virtual address&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-shared&lt;/code&gt;: Produce a shared object (&lt;code&gt;.so&lt;/code&gt;) instead of an executable requiring &lt;code&gt;main&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ldd&lt;/code&gt;: List shared libraries required by a binary and their resolved paths&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nm -D&lt;/code&gt;: Inspect exported dynamic symbols in a &lt;code&gt;.so&lt;/code&gt; file&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="2-dynamic-loading-api-dlopen--dlsym-tlpi-ch-42"&gt;2. Dynamic Loading API: &lt;code&gt;dlopen&lt;/code&gt; &amp;amp; &lt;code&gt;dlsym&lt;/code&gt; (TLPI Ch 42)&lt;a class="anchor" href="#2-dynamic-loading-api-dlopen--dlsym-tlpi-ch-42"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;glibc&lt;/code&gt; API for loading shared libraries into memory and resolving symbol addresses dynamically &lt;strong&gt;at runtime&lt;/strong&gt; rather than at program startup (&lt;code&gt;man 3 dlopen&lt;/code&gt;, &lt;code&gt;man 3 dlsym&lt;/code&gt;). The &lt;strong&gt;&lt;code&gt;RTLD_&lt;/code&gt;&lt;/strong&gt; prefix stands for &lt;strong&gt;R&lt;/strong&gt;un-&lt;strong&gt;T&lt;/strong&gt;ime &lt;strong&gt;L&lt;/strong&gt;ink-&lt;strong&gt;D&lt;/strong&gt;ynamic.&lt;/p&gt;</description></item></channel></rss>