Skip to content

[overlay] Support the older @rpath Darwin library for _Builtin_float's $ld$previous$ symbols #74920

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
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
102 changes: 41 additions & 61 deletions stdlib/linker-support/magic-symbols-for-install-name.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -19,74 +19,54 @@

#if defined(__APPLE__) && defined(__MACH__) && SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT

#include <Availability.h>
#include <mach-o/loader.h>
#include <TargetConditionals.h>
#include "swift/shims/Visibility.h"

#define RPATH_INSTALL_NAME_DIRECTIVE_IMPL2(name, major, minor) \
SWIFT_RUNTIME_EXPORT const char install_name_ ## major ## _ ## minor \
__asm("$ld$install_name$os" #major "." #minor "$@rpath/lib" #name ".dylib"); \
const char install_name_ ## major ## _ ## minor = 0;
// Swift was supported as an embedded library in macOS (née OS X) 10.9, iOS 7.0, watchOS 2.0, tvOS 9.0.
// It became part of the OS in macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2. Projects can continue
// to embed Swift, but the linker will see the OS version and try to link on that by default. In order
// to support back deployment, add a magic symbol to the OS library so that back deployment will link
// on the embedded library instead. When running on a newer OS, the OS version of the library will be
// used due to Xcode inserting a runpath search path of /usr/lib/swift based on the deployment target
// being less than SupportedTargets[target][SwiftOSRuntimeMinimumDeploymentTarget] in SDKSettings.plist.

#define RPATH_INSTALL_NAME_DIRECTIVE_IMPL(name, major, minor) \
RPATH_INSTALL_NAME_DIRECTIVE_IMPL2(name, major, minor)
// The linker uses a specially formatted symbol to do the back deployment:
// $ld$previous$<install-name>$<compatibility-version>$<platform>$<start-version>$<end-version>$<symbol-name>$
// compatibility-version and symbol-name are left off to apply to all library versions and symbols.
// This symbol isn't a legal C identifier, so it needs to be specified with __asm.
#define RPATH_PREVIOUS_DIRECTIVE_IMPL(name, platform, startVersion, endVersion) \
SWIFT_RUNTIME_EXPORT const char ld_previous_ ## platform \
__asm("$ld$previous$@rpath/lib" __STRING(name) ".dylib$$" __STRING(platform) "$" __STRING(startVersion) "$" __STRING(endVersion) "$$"); \
const char ld_previous_ ## platform = 0;
// Using the __STRING macro is important so that name and platform get expanded before being
// stringified. The versions could just be #version, __STRING is only used for consistency.

#define RPATH_INSTALL_NAME_DIRECTIVE(major, minor) \
RPATH_INSTALL_NAME_DIRECTIVE_IMPL(SWIFT_TARGET_LIBRARY_NAME, major, minor)
#define RPATH_PREVIOUS_DIRECTIVE(platform, startVersion, endVersion) \
RPATH_PREVIOUS_DIRECTIVE_IMPL(SWIFT_TARGET_LIBRARY_NAME, platform, startVersion, endVersion)


#if TARGET_OS_WATCH
// Check watchOS first, because TARGET_OS_IPHONE includes watchOS.
RPATH_INSTALL_NAME_DIRECTIVE( 2, 0)
RPATH_INSTALL_NAME_DIRECTIVE( 2, 1)
RPATH_INSTALL_NAME_DIRECTIVE( 2, 2)
RPATH_INSTALL_NAME_DIRECTIVE( 3, 0)
RPATH_INSTALL_NAME_DIRECTIVE( 3, 1)
RPATH_INSTALL_NAME_DIRECTIVE( 3, 2)
RPATH_INSTALL_NAME_DIRECTIVE( 4, 0)
RPATH_INSTALL_NAME_DIRECTIVE( 4, 1)
RPATH_INSTALL_NAME_DIRECTIVE( 4, 2)
RPATH_INSTALL_NAME_DIRECTIVE( 4, 3)
RPATH_INSTALL_NAME_DIRECTIVE( 5, 0)
RPATH_INSTALL_NAME_DIRECTIVE( 5, 1)
#elif TARGET_OS_IPHONE
RPATH_INSTALL_NAME_DIRECTIVE( 7, 0)
RPATH_INSTALL_NAME_DIRECTIVE( 7, 1)
RPATH_INSTALL_NAME_DIRECTIVE( 8, 0)
RPATH_INSTALL_NAME_DIRECTIVE( 8, 1)
RPATH_INSTALL_NAME_DIRECTIVE( 8, 2)
RPATH_INSTALL_NAME_DIRECTIVE( 8, 3)
RPATH_INSTALL_NAME_DIRECTIVE( 8, 4)
RPATH_INSTALL_NAME_DIRECTIVE( 9, 0)
RPATH_INSTALL_NAME_DIRECTIVE( 9, 1)
RPATH_INSTALL_NAME_DIRECTIVE( 9, 2)
RPATH_INSTALL_NAME_DIRECTIVE( 9, 3)
RPATH_INSTALL_NAME_DIRECTIVE(10, 0)
RPATH_INSTALL_NAME_DIRECTIVE(10, 1)
RPATH_INSTALL_NAME_DIRECTIVE(10, 2)
RPATH_INSTALL_NAME_DIRECTIVE(10, 3)
RPATH_INSTALL_NAME_DIRECTIVE(11, 0)
RPATH_INSTALL_NAME_DIRECTIVE(11, 1)
RPATH_INSTALL_NAME_DIRECTIVE(11, 2)
RPATH_INSTALL_NAME_DIRECTIVE(11, 3)
RPATH_INSTALL_NAME_DIRECTIVE(11, 4)
RPATH_INSTALL_NAME_DIRECTIVE(12, 0)
RPATH_INSTALL_NAME_DIRECTIVE(12, 1)
#elif TARGET_OS_OSX
RPATH_INSTALL_NAME_DIRECTIVE(10, 9)
RPATH_INSTALL_NAME_DIRECTIVE(10, 10)
RPATH_INSTALL_NAME_DIRECTIVE(10, 11)
RPATH_INSTALL_NAME_DIRECTIVE(10, 12)
RPATH_INSTALL_NAME_DIRECTIVE(10, 13)

// When building with a deployment target of < macOS 10.14,
// treat macOS 10.14 as an "older OS."
#if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_14
RPATH_INSTALL_NAME_DIRECTIVE(10, 14)
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
RPATH_PREVIOUS_DIRECTIVE(PLATFORM_MACOS, 10.9, 10.14.4)
#elif TARGET_OS_IOS && !TARGET_OS_VISION
#if TARGET_OS_SIMULATOR
RPATH_PREVIOUS_DIRECTIVE(PLATFORM_IOSSIMULATOR, 7.0, 12.2)
#else
RPATH_PREVIOUS_DIRECTIVE(PLATFORM_IOS, 7.0, 12.2)
#endif

#elif TARGET_OS_WATCH
#if TARGET_OS_SIMULATOR
RPATH_PREVIOUS_DIRECTIVE(PLATFORM_WATCHOSSIMULATOR, 2.0, 5.2)
#else
#error Unknown target.
RPATH_PREVIOUS_DIRECTIVE(PLATFORM_WATCHOS, 2.0, 5.2)
#endif
#elif TARGET_OS_TV
#if TARGET_OS_SIMULATOR
RPATH_PREVIOUS_DIRECTIVE(PLATFORM_TVOSSIMULATOR, 9.0, 12.2)
#else
RPATH_PREVIOUS_DIRECTIVE(PLATFORM_TVOS, 9.0, 12.2)
#endif
#endif
// Swift wasn't supported as an embedded library in any other OS, so no need to create back deployment
// symbols for any of the other ones.

#endif // defined(__APPLE__) && defined(__MACH__) && SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT
6 changes: 0 additions & 6 deletions stdlib/linker-support/previous-module-installname.json

This file was deleted.

10 changes: 9 additions & 1 deletion stdlib/public/ClangOverlays/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
if(NOT DEFINED SWIFT_BUILD_CLANG_OVERLAYS_SKIP_BUILTIN_FLOAT OR NOT SWIFT_BUILD_CLANG_OVERLAYS_SKIP_BUILTIN_FLOAT)
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(BUILTIN_FLOAT_SWIFT_FLAGS -Xfrontend -module-abi-name -Xfrontend Darwin)
else()
set(BUILTIN_FLOAT_SWIFT_FLAGS)
endif()

add_swift_target_library(swift_Builtin_float
${SWIFT_STDLIB_LIBRARY_BUILD_TYPES}
IS_SDK_OVERLAY

linker-support/magic-symbols-for-install-name.c

GYB_SOURCES
float.swift.gyb

SWIFT_COMPILE_FLAGS
${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
-Xfrontend -previous-module-installname-map-file -Xfrontend "${SWIFT_SOURCE_DIR}/stdlib/linker-support/previous-module-installname.json"
${BUILTIN_FLOAT_SWIFT_FLAGS}

LINK_FLAGS
${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}
Expand Down
8 changes: 0 additions & 8 deletions stdlib/public/ClangOverlays/float.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

@available(swift, deprecated: 3.0, message: "Please use 'T.radix' to get the radix of a FloatingPoint type 'T'.")
@available(macOS 10.9, iOS 7.0, watchOS 2.0, tvOS 9.0, visionOS 1.0, *)
@_originallyDefinedIn(module: "Darwin", macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0)
public let FLT_RADIX = Double.radix

%for type, prefix in [('Float', 'FLT'), ('Double', 'DBL'), ('Float80', 'LDBL')]:
Expand All @@ -25,7 +24,6 @@ public let FLT_RADIX = Double.radix
// significand bit, but Swift does not. Neither is really right or wrong.
@available(swift, deprecated: 3.0, message: "Please use '${type}.significandBitCount + 1'.")
@available(macOS 10.9, iOS 7.0, watchOS 2.0, tvOS 9.0, visionOS 1.0, *)
@_originallyDefinedIn(module: "Darwin", macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0)
public let ${prefix}_MANT_DIG = ${type}.significandBitCount + 1

// Where does the 1 come from? C models floating-point numbers as having a
Expand All @@ -34,32 +32,26 @@ public let ${prefix}_MANT_DIG = ${type}.significandBitCount + 1
// as well.
@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude.exponent + 1'.")
@available(macOS 10.9, iOS 7.0, watchOS 2.0, tvOS 9.0, visionOS 1.0, *)
@_originallyDefinedIn(module: "Darwin", macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0)
public let ${prefix}_MAX_EXP = ${type}.greatestFiniteMagnitude.exponent + 1

@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude.exponent + 1'.")
@available(macOS 10.9, iOS 7.0, watchOS 2.0, tvOS 9.0, visionOS 1.0, *)
@_originallyDefinedIn(module: "Darwin", macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0)
public let ${prefix}_MIN_EXP = ${type}.leastNormalMagnitude.exponent + 1

@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude' or '.greatestFiniteMagnitude'.")
@available(macOS 10.9, iOS 7.0, watchOS 2.0, tvOS 9.0, visionOS 1.0, *)
@_originallyDefinedIn(module: "Darwin", macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0)
public let ${prefix}_MAX = ${type}.greatestFiniteMagnitude

@available(swift, deprecated: 3.0, message: "Please use '${type}.ulpOfOne' or '.ulpOfOne'.")
@available(macOS 10.9, iOS 7.0, watchOS 2.0, tvOS 9.0, visionOS 1.0, *)
@_originallyDefinedIn(module: "Darwin", macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0)
public let ${prefix}_EPSILON = ${type}.ulpOfOne

@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude' or '.leastNormalMagnitude'.")
@available(macOS 10.9, iOS 7.0, watchOS 2.0, tvOS 9.0, visionOS 1.0, *)
@_originallyDefinedIn(module: "Darwin", macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0)
public let ${prefix}_MIN = ${type}.leastNormalMagnitude

@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNonzeroMagnitude' or '.leastNonzeroMagnitude'.")
@available(macOS 10.9, iOS 7.0, watchOS 2.0, tvOS 9.0, visionOS 1.0, *)
@_originallyDefinedIn(module: "Darwin", macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0)
public let ${prefix}_TRUE_MIN = ${type}.leastNonzeroMagnitude

% if type == "Float80":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//===--- magic-symbols-for-install-name.c - Magic linker directive symbols ===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// A file containing magic symbols that instruct the linker to use a
// different install name when targeting older OSes. This file gets
// compiled into all of the libraries that are embedded for backward
// deployment.
//
// This file is specific to the Builtin_float library; there is a matching
// file for the standard library with the same name.
//
//===----------------------------------------------------------------------===//

#if defined(__APPLE__) && defined(__MACH__) && SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT

#include <mach-o/loader.h>
#include <TargetConditionals.h>
#include "swift/shims/Visibility.h"

// Builtin_float was split out from the Darwin library in macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0,
// visionOS 2.0 Prior to macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, Darwin was supported as an
// embedded library starting in macOS (née OS X) 10.9, iOS 7.0, watchOS 2.0, tvOS 9.0. In order to
// support back deployment, add a magic symbol so that back deployment will link on Darwin instead, and
// the embedded version on particularly old deployment targets. On newer OS versions, Darwin re-exports
// Builtin_float which is where the APIs are implemented.

// The linker uses a specially formatted symbol to do the back deployment:
// $ld$previous$<install-name>$<compatibility-version>$<platform>$<start-version>$<end-version>$<symbol-name>$
// compatibility-version and symbol-name are left off to apply to all library versions and symbols.
// This symbol isn't a legal C identifier, so it needs to be specified with __asm.
#define DARWIN_RPATH_PREVIOUS_DIRECTIVE(platform, startVersion, endVersion) \
SWIFT_RUNTIME_EXPORT const char ld_previous_rpath_ ## platform \
__asm("$ld$previous$@rpath/libswiftDarwin.dylib$$" __STRING(platform) "$" __STRING(startVersion) "$" __STRING(endVersion) "$$"); \
const char ld_previous_rpath_ ## platform = 0;
// Using the __STRING macro is important so that platform gets expanded before being stringified.
// The versions could just be #version, __STRING is only used for consistency.

#define DARWIN_PREVIOUS_DIRECTIVE(platform, startVersion, endVersion) \
SWIFT_RUNTIME_EXPORT const char ld_previous_ ## platform \
__asm("$ld$previous$/usr/lib/swift/libswiftDarwin.dylib$$" __STRING(platform) "$" __STRING(startVersion) "$" __STRING(endVersion) "$$"); \
const char ld_previous_ ## platform = 0;

#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
DARWIN_RPATH_PREVIOUS_DIRECTIVE(PLATFORM_MACOS, 10.9, 10.14.4)
DARWIN_PREVIOUS_DIRECTIVE(PLATFORM_MACOS, 10.14.4, 15.0)
DARWIN_PREVIOUS_DIRECTIVE(PLATFORM_MACCATALYST, 13.1, 18.0)
#elif TARGET_OS_IOS && !TARGET_OS_VISION
#if TARGET_OS_SIMULATOR
DARWIN_RPATH_PREVIOUS_DIRECTIVE(PLATFORM_IOSSIMULATOR, 7.0, 12.2)
DARWIN_PREVIOUS_DIRECTIVE(PLATFORM_IOSSIMULATOR, 12.2, 18.0)
#else
DARWIN_RPATH_PREVIOUS_DIRECTIVE(PLATFORM_IOS, 7.0, 12.2)
DARWIN_PREVIOUS_DIRECTIVE(PLATFORM_IOS, 12.2, 18.0)
#endif
#elif TARGET_OS_WATCH
#if TARGET_OS_SIMULATOR
DARWIN_RPATH_PREVIOUS_DIRECTIVE(PLATFORM_WATCHOSSIMULATOR, 2.0, 5.2)
DARWIN_PREVIOUS_DIRECTIVE(PLATFORM_WATCHOSSIMULATOR, 5.2, 11.0)
#else
DARWIN_RPATH_PREVIOUS_DIRECTIVE(PLATFORM_WATCHOS, 2.0, 5.2)
DARWIN_PREVIOUS_DIRECTIVE(PLATFORM_WATCHOS, 5.2, 11.0)
#endif
#elif TARGET_OS_TV
#if TARGET_OS_SIMULATOR
DARWIN_RPATH_PREVIOUS_DIRECTIVE(PLATFORM_TVOSSIMULATOR, 9.0, 12.2)
DARWIN_PREVIOUS_DIRECTIVE(PLATFORM_TVOSSIMULATOR, 12.2, 18.0)
#else
DARWIN_RPATH_PREVIOUS_DIRECTIVE(PLATFORM_TVOS, 9.0, 12.2)
DARWIN_PREVIOUS_DIRECTIVE(PLATFORM_TVOS, 12.2, 18.0)
#endif
#elif TARGET_OS_VISION
#if TARGET_OS_SIMULATOR
DARWIN_PREVIOUS_DIRECTIVE(PLATFORM_VISIONOSSIMULATOR, 1.0, 2.0);
#else
DARWIN_PREVIOUS_DIRECTIVE(PLATFORM_VISIONOS, 1.0, 2.0);
#endif
#endif

#endif // defined(__APPLE__) && defined(__MACH__) && SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT
Loading