<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/56074>56074</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            ld64.lld issue with $ld$previous symbols and macOS 13 SDK
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld:MachO
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          keith
      </td>
    </tr>
</table>

<pre>
    With this simple swift program:

```swift
import Foundation

print(NSString("hi"))
```

Built with the Xcode 14 beta:

```
DEVELOPER_DIR=/Applications/Xcode-14.0.0-beta1.app swiftc basic.swift -emit-object -o basic.o
```

and linked with lld:

```
./bin/ld64.lld \
 basic.o \
 /Applications/Xcode-14.0.0-beta1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/lib/darwin/libclang_rt.osx.a \
 -syslibroot \
 /Applications/Xcode-14.0.0-beta1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk \
 -lobjc \
 -lSystem \
 -arch \
 arm64 \
 -L \
 /Applications/Xcode-14.0.0-beta1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx \
 -L \
 /Applications/Xcode-14.0.0-beta1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/usr/lib/swift \
 -platform_version \
 macos \
 12.0.0 \
 13.0.0 \
 -o \
 foo
```

crashes on launch:

```
% ./foo
dyld[74309]: missing symbol called
zsh: abort      ./foo
```

This binary links fine with ld64 from Xcode 13 and 14. After a bit of debugging I believe the issue is incomplete handling of `$ld$previous` symbols, specifically this omission https://github.com/llvm/llvm-project/blob/272bf0fc41e6a50f89dd01b55a33a3aabcdaf5a8/lld/MachO/InputFiles.cpp#L1754-L1756

Scanning the ld64 source I believe in this case what we need to do is add the specified symbol name using the custom install name provided in the symbol. The specific previous symbol in question here is:

```                       '$ld$previous$/usr/lib/swift/libswiftFoundation.dylib$$1$10.15$13.0$_$sSo8NSStringC10FoundationE13stringLiteralABs12StaticStringV_tcfC$', 
```

You can see that we're missing the library specified:

```
% otool -L from-lld | grep Foundation
        /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1932.101.0)
        /usr/lib/swift/libswiftCoreFoundation.dylib (compatibility version 1.0.0, current version 120.100.0, weak)
```

While linking with ld64 provides it:

```
% otool -L from-ld64 | grep Foundation
        /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1932.101.0)
        /usr/lib/swift/libswiftCoreFoundation.dylib (compatibility version 1.0.0, current version 120.100.0, weak)
        /usr/lib/swift/libswiftFoundation.dylib (compatibility version 300.0.0, current version 1932.101.0)
```

And you can see the symbol is pointing to the wrong library:

```
% nm from-lld -m | grep Static
(undefined) external _$sSo8NSStringC10FoundationE13stringLiteralABs12StaticStringV_tcfC (from Foundation)
% nm from-ld64 -m | grep Static
(undefined) external _$sSo8NSStringC10FoundationE13stringLiteralABs12StaticStringV_tcfC (from libswiftFoundation)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztV0tv4zYQ_jXyhbCgpx8HHxI7BoKmzWIdZLengKIoiw0lqiSVrPvrO0NZtvLyJtgt2kMNWRI55PDjDOebUaby3eKLsCWxpTDEiKqRnJhHUVjSaLXVtPLiMy9YeUF_nwTd5QZ1fTBLaUvWqq1zaoWqhxMaLWrrRbPfNhsLr1t49aKoFHDzojleT_UO5563Qlry2OHj5CtTOSdhQjJu6Vu4uubq4vbi6vrTxee71eVnL1550fqsaaRgDp-BplM2DhM_8IMxKgx92jTd3hnJqBHM7wwx5pWwY5X9wRk01F6mTuCmdU6kqO953oGXMj8N1wdAmajhLvNJ4sN44qXLTtavN-h5725AsFS15bXFMSv-wKVquIb3G6UkK6k4Tl7xgrbS-t-Y7WUgag2OliKDe-fxaM0kRT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