Skip to content

Commit b198362

Browse files
committed
Fix the declaration of shim visibility on ELF platforms.
Declarations with protected visibility are assumed to be defined within the current linkage unit, so we have to use default visibility if we don't know that we're building that. Teach the shim visibility header to only use protected visibility when the __SWIFT_CURRENT_DYLIB macro is defined, and define it when building the standard library. Eventually we should change SWIFT_RUNTIME_STDLIB_INTERFACE and SWIFT_RUNTIME_EXPORT to be parameterized by the defining dylib so that this works for all the overlay stubs, too; for now, special-casing swiftCore is necessary to fix the LInux build.
1 parent 859ef7b commit b198362

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

cmake/modules/SwiftSource.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ function(handle_swift_sources
7979
# <rdar://problem/15972329>
8080
list(APPEND swift_compile_flags "-force-single-frontend-invocation")
8181

82+
if(SWIFTSOURCES_IS_STDLIB_CORE)
83+
list(APPEND swift_compile_flags "-Xcc" "-D__SWIFT_CURRENT_DYLIB=swiftCore")
84+
endif()
85+
8286
_compile_swift_files(
8387
dependency_target
8488
OUTPUT ${swift_obj}

stdlib/public/SwiftShims/Visibility.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,29 @@
3636
# define SWIFT_END_NULLABILITY_ANNOTATIONS
3737
#endif
3838

39+
// TODO: support using shims headers in overlays by parameterizing
40+
// SWIFT_RUNTIME_EXPORT on the library it's exported from, then setting
41+
// protected vs. default based on the current value of __SWIFT_CURRENT_DYLIB.
42+
3943
/// Attribute used to export symbols from the runtime.
4044
#if __MACH__
4145
# define SWIFT_RUNTIME_EXPORT __attribute__((__visibility__("default")))
4246
#elif __ELF__
47+
4348
// Use protected visibility for ELF, since we don't want Swift symbols to be
4449
// interposable. The relative relocations we form to metadata aren't
4550
// valid in ELF shared objects, and leaving them relocatable at load time
4651
// defeats the purpose of the relative references.
52+
//
53+
// Protected visibility on a declaration is interpreted to mean that the
54+
// symbol is defined in the current dynamic library, so if we're building
55+
// something else, we need to fall back on using default visibility.
56+
#ifdef __SWIFT_CURRENT_DYLIB
4757
# define SWIFT_RUNTIME_EXPORT __attribute__((__visibility__("protected")))
58+
#else
59+
# define SWIFT_RUNTIME_EXPORT __attribute__((__visibility__("default")))
60+
#endif
61+
4862
#elif __CYGWIN__
4963
# define SWIFT_RUNTIME_EXPORT
5064
#else

stdlib/public/runtime/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
2222
set(swift_runtime_port_sources
2323
CygwinPort.cpp)
2424
endif()
25+
26+
list(APPEND swift_runtime_compile_flags
27+
"-D__SWIFT_CURRENT_DYLIB=swiftCore")
2528

2629
set(swift_runtime_objc_sources)
2730
set(swift_runtime_unicode_normalization_sources)

0 commit comments

Comments
 (0)