Skip to content

IRGen: consider deployment target when emitting $ld$previous symbols #63757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/IRGen/TBDGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,18 @@ getInnermostIntroVersion(ArrayRef<Decl*> DeclStack, PlatformKind Platform) {
return None;
}

/// Using the introducing version of a symbol as the start version to redirect
/// linkage path isn't sufficient. This is because the executable can be deployed
/// to OS versions that were before the symbol was introduced. When that happens,
/// strictly using the introductory version can lead to NOT redirecting.
static llvm::VersionTuple calculateLdPreviousVersionStart(ASTContext &ctx,
llvm::VersionTuple introVer) {
auto minDep = ctx.LangOpts.getMinPlatformVersion();
if (minDep < introVer)
return llvm::VersionTuple(1, 0);
return introVer;
}

void TBDGenVisitor::addLinkerDirectiveSymbolsLdPrevious(StringRef name,
llvm::MachO::SymbolKind kind) {
if (kind != llvm::MachO::SymbolKind::GlobalSymbol)
Expand Down Expand Up @@ -323,7 +335,8 @@ void TBDGenVisitor::addLinkerDirectiveSymbolsLdPrevious(StringRef name,
static auto getMinor = [](Optional<unsigned> Minor) {
return Minor.has_value() ? *Minor : 0;
};
OS << IntroVer->getMajor() << "." << getMinor(IntroVer->getMinor()) << "$";
auto verStart = calculateLdPreviousVersionStart(Ctx, *IntroVer);
OS << verStart.getMajor() << "." << getMinor(verStart.getMinor()) << "$";
OS << Ver.Version.getMajor() << "." << getMinor(Ver.Version.getMinor()) << "$";
OS << name << "$";
addSymbolInternal(OS.str(), SymbolKind::GlobalSymbol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// RUN: not %target-swift-frontend -swift-version 4 -enforce-exclusivity=checked %s -emit-ir -module-name CurrentModule -D CURRENT_MODULE -previous-module-installname-map-file %t/nil.json >& %t/error.txt
// RUN: %FileCheck %s -check-prefix=CHECK-ERROR < %t/error.txt

// RUN: %target-swift-frontend -target x86_64-apple-macosx10.6 -swift-version 4 -enforce-exclusivity=checked %s -emit-ir -module-name CurrentModule -D CURRENT_MODULE -previous-module-installname-map-file %t/install-name.json | %FileCheck %s -check-prefix=CHECK-SYMBOLS-LOW-TARGET

@available(OSX 10.8, *)
@_originallyDefinedIn(module: "OriginalModule", macOS 10.10)
public struct Entity {
Expand All @@ -19,4 +21,10 @@ public struct Entity {
// CHECK-SYMBOLS: $ld$previous$/System/OriginalModule.dylib$$1$10.8$10.10$_$s14OriginalModule6EntityVMa$
// CHECK-SYMBOLS: $ld$previous$/System/OriginalModule.dylib$$1$10.8$10.10$_$s14OriginalModule6EntityV06removeC0yyACF$

// CHECK-SYMBOLS-LOW-TARGET: $ld$previous$/System/OriginalModule.dylib$$1$1.0$10.10$_$s14OriginalModule6EntityVMn$
// CHECK-SYMBOLS-LOW-TARGET: $ld$previous$/System/OriginalModule.dylib$$1$1.0$10.10$_$s14OriginalModule6EntityV03addC0yyACF$
// CHECK-SYMBOLS-LOW-TARGET: $ld$previous$/System/OriginalModule.dylib$$1$1.0$10.10$_$s14OriginalModule6EntityVN$
// CHECK-SYMBOLS-LOW-TARGET: $ld$previous$/System/OriginalModule.dylib$$1$1.0$10.10$_$s14OriginalModule6EntityVMa$
// CHECK-SYMBOLS-LOW-TARGET: $ld$previous$/System/OriginalModule.dylib$$1$1.0$10.10$_$s14OriginalModule6EntityV06removeC0yyACF$

// CHECK-ERROR: cannot open previous install name map