Apple Linker Magic & Swift Runtime
This article explores how apps link differently against the runtime depending on the deployment target.
[…]
How can you link against the same dylib, which has a single install name, but any linked binaries record different dylib install names?
[…]
ld64
sources are published by Apple, so we can see how the linker actually works. The code can be found in macho_dylib_file.cpp inside the methodFile<A>::addSymbol()
. It defines the magic format as$ld$ <action> $ <condition> $ <symbol-name>
.
The various
$ld$
magic symbols are used heavily by Apple’s system frameworks to maintain backwards compatibility while moving symbols between frameworks. If you look at the various .tbd files in the Xcode SDKs you’ll see all sorts of shenanigans along these lines.
Example: class
NSObject
moved from CoreFoundation to libobjc in macOS 10.8. libobjc has$ld$hide
symbols for old OS versions (“NSObject
is here, but it used to be somewhere else”). CF has$ld$add
symbols for old OS versions (“NSObject
is elsewhere, but it used to be here”).