Skip to content

Commit cabe7c4

Browse files
committed
Work around relocation R_X86_64_PC32 link error
A more complete discussion of the error and its background can be found in SR-1023. The brief overview is that in versions of `ld` that would be effected by this dynamic relocation issue there are two flags: `-z nocopyreloc` and `-z noextern-protected-data`. If `ld` supports the flags then they are sento to the linker. This patch checks that the linker supports the flags and then optionally sets them. The code to check if `ld` supports the flags is inspired by this CMake mailing list entry. https://cmake.org/pipermail/cmake/2011-July/045525.html
1 parent 1a0891c commit cabe7c4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,19 @@ function(_add_swift_library_single target name)
801801
list(APPEND link_flags "-fuse-ld=gold")
802802
endif()
803803

804+
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
805+
INCLUDE(CheckCXXCompilerFlag)
806+
SET(CMAKE_REQUIRED_FLAGS "-Wl,-z,nocopyreloc")
807+
CHECK_CXX_COMPILER_FLAG("" LINKER_SUPPORTS_NOCOPYRELOC)
808+
IF(LINKER_SUPPORTS_NOCOPYRELOC)
809+
list(APPEND link_flags ${CMAKE_REQUIRED_FLAGS})
810+
ENDIF()
811+
SET(CMAKE_REQUIRED_FLAGS "-Wl,-z,noextern-protected-data")
812+
CHECK_CXX_COMPILER_FLAG("" LINKER_SUPPORTS_NOEXTERN_PROTECTED_DATA)
813+
IF(LINKER_SUPPORTS_NOEXTERN_PROTECTED_DATA)
814+
list(APPEND link_flags ${CMAKE_REQUIRED_FLAGS})
815+
ENDIF()
816+
804817
# Configure plist creation for OS X.
805818
set(PLIST_INFO_PLIST "Info.plist" CACHE STRING "Plist name")
806819
if(APPLE AND SWIFTLIB_SINGLE_IS_STDLIB)
@@ -1416,6 +1429,18 @@ function(_add_swift_executable_single name)
14161429
list(APPEND link_flags "-fuse-ld=gold")
14171430
endif()
14181431

1432+
INCLUDE(CheckCXXCompilerFlag)
1433+
SET(CMAKE_REQUIRED_FLAGS "-Wl,-z,nocopyreloc")
1434+
CHECK_CXX_COMPILER_FLAG("" LINKER_SUPPORTS_NOCOPYRELOC)
1435+
IF(LINKER_SUPPORTS_NOCOPYRELOC)
1436+
list(APPEND link_flags ${CMAKE_REQUIRED_FLAGS})
1437+
ENDIF()
1438+
SET(CMAKE_REQUIRED_FLAGS "-Wl,-z,noextern-protected-data")
1439+
CHECK_CXX_COMPILER_FLAG("" LINKER_SUPPORTS_NOEXTERN_PROTECTED_DATA)
1440+
IF(LINKER_SUPPORTS_NOEXTERN_PROTECTED_DATA)
1441+
list(APPEND link_flags ${CMAKE_REQUIRED_FLAGS})
1442+
ENDIF()
1443+
14191444
# Find the names of dependency library targets.
14201445
#
14211446
# We don't add the ${ARCH} to the target suffix because we want to link

0 commit comments

Comments
 (0)