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.

Dynamic Linking Best Practices

134 pointsby begriffsalmost 4 years ago

8 comments

pjmlpalmost 4 years ago
&gt; The design typically used nowadays for dynamic linking (in BSD, MacOS, and Linux) came from SunOS in 1988. The paper Shared Libraries in SunOS neatly explains the goals, design, and implementation.<p>Aix shared libraries follow a model similar to Windows, with export files.<p><a href="https:&#x2F;&#x2F;www.ibm.com&#x2F;docs&#x2F;en&#x2F;aix&#x2F;7.2?topic=memory-creating-shared-library" rel="nofollow">https:&#x2F;&#x2F;www.ibm.com&#x2F;docs&#x2F;en&#x2F;aix&#x2F;7.2?topic=memory-creating-sh...</a><p><a href="https:&#x2F;&#x2F;www.ibm.com&#x2F;docs&#x2F;en&#x2F;aix&#x2F;7.2?topic=formats-xcoff-object-file-format" rel="nofollow">https:&#x2F;&#x2F;www.ibm.com&#x2F;docs&#x2F;en&#x2F;aix&#x2F;7.2?topic=formats-xcoff-obje...</a>
secondcomingalmost 4 years ago
Compiling a shared lib means adding &#x27;fpic&#x27;, which can mean poor codegen if you don&#x27;t handle your symbol visibility well. By default symbols are globally visible and the compiler must assume that at runtime you may do an LD_PRELOAD in order to hook into some functions, and to do that the functions need to be called indirectly rather than directly.<p>At the very least, add &#x27;-fvisibility-inlines-hidden&#x27;.
评论 #27730934 未加载
marco_craveiroalmost 4 years ago
Very interesting. There was also the old Drepper paper, which was very good but this seems more accessible. [1]<p>[1] <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>
jancialmost 4 years ago
How this compares to Windows? IIRC it is all handled by the executable itself? The kernel32.dll is loaded at known address and it has LoadLibrary function to get a handle of DLL and GetProcAddress that resolves a function name to a function pointer. It loads DLLs from the working directory or system directories. No versioning. (But there are some compatibility tricks to provide correct version of the library even when the dll name does not change)
评论 #27735169 未加载
评论 #27734179 未加载
lggalmost 4 years ago
It is good article, but I wish it discussed the differences in dynamic symbol resolution, which is often a source of subtle compatibility issues when porting between POSIX OSes.<p>Linux uses a flat namespace for linking, where each bind lists the symbol name, and then at runtime ld.so searches for that symbol based on the library search order.<p>macOS uses a two level namespace for linking, where each bind lists both the symbol name and the installname of the library that provides the symbol. This means at runtime the dynamic linker does not need use a library search path. This has a number of performance and binary compatibility benefits, though it can make building software a bit more complex.<p>Solaris uses a flat namespace, but retrofitted two levelnamespace semantics via Direct Binding[1][2]. I am not sure how pervasively they are used.<p>I know there were some efforts to port Solaris Direct Binding support to Linux maybe 15 years ago[3][4], but it is a very hard change to make without breaking bin and source compatibility for projects that depend on insert libraries in the search path for partial refinements via interposing symbols.<p>1: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Direct_binding" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Direct_binding</a> 2: <a href="http:&#x2F;&#x2F;www.linker-aliens.org&#x2F;blogs&#x2F;ali&#x2F;entry&#x2F;the_cost_of_elf_symbol&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.linker-aliens.org&#x2F;blogs&#x2F;ali&#x2F;entry&#x2F;the_cost_of_elf...</a> 3: <a href="https:&#x2F;&#x2F;lwn.net&#x2F;Articles&#x2F;192624&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lwn.net&#x2F;Articles&#x2F;192624&#x2F;</a> 4: <a href="https:&#x2F;&#x2F;bugs.gentoo.org&#x2F;114008" rel="nofollow">https:&#x2F;&#x2F;bugs.gentoo.org&#x2F;114008</a>
Miikoalmost 4 years ago
Good coverage of dynamic linking, but for this part:<p>&gt; To solve these problems, I suggest bundling all development (linking) library files together into a different directory structure per version.<p>Isn&#x27;t it almost exactly what&#x27;s called &quot;framework&quot; on macOS (author&#x27;s proposed structure is different, but the idea looks the same)?
rualcaalmost 4 years ago
This article is a superb treasure trove of experience on dynamic linking. Thanks for sharing this gem.
Digitalis33almost 4 years ago
As someone who&#x27;s just now diving into low-level programming on unix, this article is just what I needed.<p>Saving this for future reference.