TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Anatomy of Linux Dynamic Libraries

119 pointsby begoonover 2 years ago

8 comments

chrsigover 2 years ago
This is a good primer on the subject.<p>One of the major projects I maintain at $work is an apache module, which has given me so much grief because of the fact that it gets dynamically loaded (and then unloaded, and then reloaded)<p>It&#x27;s been very enlightening, but there&#x27;s no shortage of frustration.<p>One major points is that a dynamically loaded library can have undefined symbols that are found (or not!) at runtime. This can introduce a lot of trouble when trying to make a test binary. Especially in apache&#x27;s case, since any of the `ap_` symbols are only compiled into the executable, and aren&#x27;t available as a static or shared library.<p>In that situation, the only recourse that I&#x27;m aware of is to supply your own implementations of those functions that get compiled into the test binary (<i>not</i> the dynamic library under test, since that will create a collision when it&#x27;s loaded)<p>Another thing to consider is that linux&#x2F;elf shared libraries can be either dynamically loaded or dynamically linked. This is not true on all platforms, so it&#x27;s not advisable to lean into it if portability matters.<p>I recommend reading the ld.so manpage[0] -- in particular it outlines many environment variables to control the runtime linker. Notably, LD_DEBUG can be a life saver.<p>Lastly, I&#x27;ll also warn against using any global&#x2F;static memory in a dynamically loaded library. It can be unloaded&#x2F;reloaded, and that can create a lot of havoc, especially if a pointer to that memory gets saved somewhere that survives the unload, and then is accessible after the library gets reloaded.<p>libprotobuf is a major victim of these types of issues - which is why (or a major contributor) to why the libprotobuf-lite library exists.<p>[0] <a href="https:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man8&#x2F;ld.so.8.html" rel="nofollow">https:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man8&#x2F;ld.so.8.html</a>
评论 #33192523 未加载
ghotliover 2 years ago
I have to care about this sort of thing at great length in my current work. Targeting hundreds of linux &#x2F; embedded environments. I picked up a tip or two and in general this is a very well written overview of the moving pieces I wish I&#x27;d found when I first had to dig into this domain
coleyover 2 years ago
The first time I learned about dynamic loading was while watching an episode of Casey Muratori&#x27;s Handmade Hero - a series where he builds a video game, in C, from scratch.<p>He compiles the game code as a dll and dynamically loads it at runtime in the Win32 platform layer code. This way he can keep platform code and game code separate and reload the game code at runtime if any changes are made. Being new to this technique, I was impressed to say the least.<p>I always assumed the same functionality was available in the linux environment, but I hadn&#x27;t bothered to look it up. Now I know.<p>For those who want to learn how to do this in Win32, here&#x27;s the episode I mentioned above - <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=WMSBRk5WG58" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=WMSBRk5WG58</a>
gnufxover 2 years ago
From the other side of it, there&#x27;s &quot;How to Write Shared Libraries&quot;: <a href="https:&#x2F;&#x2F;akkadia.org&#x2F;drepper&#x2F;dsohowto.pdf" rel="nofollow">https:&#x2F;&#x2F;akkadia.org&#x2F;drepper&#x2F;dsohowto.pdf</a><p>Edit: LD_PRELOAD would have been useful to mention in the article. It&#x27;s a useful mechanism, e.g. for profiling by hooking relevant functions, like the PMPI mechanism in the MPI standard.
评论 #33194832 未加载
stevewatson301over 2 years ago
This article, along with [1] (HN discussion [2]) is what finally made the linking process &quot;click&quot; in my brain.<p>[1] <a href="https:&#x2F;&#x2F;werat.dev&#x2F;blog&#x2F;how-wine-works-101&#x2F;" rel="nofollow">https:&#x2F;&#x2F;werat.dev&#x2F;blog&#x2F;how-wine-works-101&#x2F;</a><p>[2] <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=33156727" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=33156727</a>
matheusmoreiraover 2 years ago
Very useful article! I wish I had read this a long time ago, would&#x27;ve made a lot of things much easier to understand.<p>I&#x27;d like to comment on this:<p>&gt; When the relocations are complete, the dynamic linker allows any loaded shared object to execute optional initialization code. This functionality allows the library to initialize internal data and prepare for use.<p>&gt; This code is defined in the .init section of the ELF image. When the library is unloaded, it may also call a termination function (defined as the .fini section in the image).<p>There are huge problems with this stuff. Are these sections still used? I know you can add functions to these sections with a GCC attribute but I don&#x27;t know why anyone would do that. Do other languages like C++ use these sections?<p><a href="https:&#x2F;&#x2F;blogs.oracle.com&#x2F;solaris&#x2F;post&#x2F;init-and-fini-processing-who-designed-this" rel="nofollow">https:&#x2F;&#x2F;blogs.oracle.com&#x2F;solaris&#x2F;post&#x2F;init-and-fini-processi...</a>
评论 #33192713 未加载
评论 #33193569 未加载
评论 #33194438 未加载
photochemsynover 2 years ago
Very helpful article, clear and informative. Understanding at least the basics of how the linker works at a basic level is very illuminating, and for embedded programming even more so. Found this recently which is a nice complement to this article:<p><a href="https:&#x2F;&#x2F;microcontrollerslab.com&#x2F;bare-metal-embedded-systems-linker-script-file&#x2F;" rel="nofollow">https:&#x2F;&#x2F;microcontrollerslab.com&#x2F;bare-metal-embedded-systems-...</a>
gnufxover 2 years ago
Concerning ldd, see the security warning in ldd(1). I&#x27;m not sure if it&#x27;s still an issue in current GNU binutils, but lddtree is supposed to be safe and the tree may be more useful.