diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp index 1befdf480eb0c..5bd15d6baac85 100644 --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -2108,9 +2108,14 @@ void DylibFile::handleLDPreviousSymbol(StringRef name, StringRef originalName) { auto *dylib = getSyntheticDylib(installName, newCurrentVersionForSymbol, newCompatibilityVersion); - // Just adding the symbol to the symtab works because dylibs contain their - // symbols in alphabetical order, guaranteeing $ld$ symbols to precede - // normal symbols. + // The tbd file usually contains the $ld$previous symbol for an old version, + // and then the symbol itself later, for newer deployment targets, like so: + // symbols: [ + // '$ld$previous$/Another$$1$3.0$14.0$_zzz$', + // _zzz, + // ] + // Since the symbols are sorted, adding them to the symtab in the given + // order means the $ld$previous version of _zzz will prevail, as desired. dylib->symbols.push_back(symtab->addDylib( saver().save(symbolName), dylib, /*isWeakDef=*/false, /*isTlv=*/false)); return; diff --git a/lld/test/MachO/special-symbol-ld-previous.s b/lld/test/MachO/special-symbol-ld-previous.s index 8cdfb976dd045..eb143160e5873 100644 --- a/lld/test/MachO/special-symbol-ld-previous.s +++ b/lld/test/MachO/special-symbol-ld-previous.s @@ -29,13 +29,9 @@ ## The remaining cases test handling when a symbol name is part of $ld$previous. -## Case 4: special symbol $ld$previous affects the install name / compatibility version -## when the specified version 11.0.0 is within the affected range [3.0, 14.0) when a symbol -## is part of $previous$ if and only if that named symbol is referenced. -## That is, for $ld$previous$/NewName$$3.0$14.0$_symNam$, if _symNam is -## referenced, it refers to dylib /NewName if the deployment target is -## in [3.0, 14.0). - +## Case 4: special symbol $ld$previous affects the install name / compatibility version if and only if: +## * the specified version 11.0.0 is within the affected range [3.0, 14.0), and +## * the symbol name after $previous$ points to a referenced symbol # RUN: %lld -o %t/libfoo4_yes.dylib %t/libLDPreviousInstallName-Symbol.tbd %t/ref_ySyy.o -dylib -platform_version macos 11.0.0 11.0.0 # RUN: llvm-otool -L %t/libfoo4_yes.dylib | FileCheck --check-prefix=CASE4-YES --implicit-check-not=/New %s # CASE4-YES: /Old (compatibility version 1.2.3, current version 1.2.3)