Skip to content

[cmake] Work around relocation R_X86_64_PC32 link error #2012

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

Closed
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
25 changes: 25 additions & 0 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,19 @@ function(_add_swift_library_single target name)
list(APPEND link_flags "-fuse-ld=gold")
endif()

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
INCLUDE(CheckCXXCompilerFlag)
SET(CMAKE_REQUIRED_FLAGS "-Wl,-z,nocopyreloc")
CHECK_CXX_COMPILER_FLAG("" LINKER_SUPPORTS_NOCOPYRELOC)
IF(LINKER_SUPPORTS_NOCOPYRELOC)
list(APPEND link_flags ${CMAKE_REQUIRED_FLAGS})
ENDIF()
SET(CMAKE_REQUIRED_FLAGS "-Wl,-z,noextern-protected-data")
CHECK_CXX_COMPILER_FLAG("" LINKER_SUPPORTS_NOEXTERN_PROTECTED_DATA)
IF(LINKER_SUPPORTS_NOEXTERN_PROTECTED_DATA)
list(APPEND link_flags ${CMAKE_REQUIRED_FLAGS})
ENDIF()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you should include() or run tests in this function. This function is executed many times.

You should run the test once in the top-level CMakeLists.txt.

Also, our CMake code uses lowercase for function names, and two spaces for indentation, could you make it consistent?


# Configure plist creation for OS X.
set(PLIST_INFO_PLIST "Info.plist" CACHE STRING "Plist name")
if(APPLE AND SWIFTLIB_SINGLE_IS_STDLIB)
Expand Down Expand Up @@ -1416,6 +1429,18 @@ function(_add_swift_executable_single name)
list(APPEND link_flags "-fuse-ld=gold")
endif()

INCLUDE(CheckCXXCompilerFlag)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't think this block is required. I merely included it because I think it might be. Someone who understands the CMake scripts a little better might be able to quickly deduce its necessity.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this block is necessary. This function is used to configure compiler executables, which don't have any Swift code in them.

SET(CMAKE_REQUIRED_FLAGS "-Wl,-z,nocopyreloc")
CHECK_CXX_COMPILER_FLAG("" LINKER_SUPPORTS_NOCOPYRELOC)
IF(LINKER_SUPPORTS_NOCOPYRELOC)
list(APPEND link_flags ${CMAKE_REQUIRED_FLAGS})
ENDIF()
SET(CMAKE_REQUIRED_FLAGS "-Wl,-z,noextern-protected-data")
CHECK_CXX_COMPILER_FLAG("" LINKER_SUPPORTS_NOEXTERN_PROTECTED_DATA)
IF(LINKER_SUPPORTS_NOEXTERN_PROTECTED_DATA)
list(APPEND link_flags ${CMAKE_REQUIRED_FLAGS})
ENDIF()

# Find the names of dependency library targets.
#
# We don't add the ${ARCH} to the target suffix because we want to link
Expand Down
3 changes: 3 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,9 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
Arguments.push_back(context.Args.MakeArgString(Target));
}

Arguments.push_back("-Wl,-z,nocopyreloc");
Arguments.push_back("-Wl,-z,noextern-protected-data");

// Add the runtime library link path, which is platform-specific and found
// relative to the compiler.
llvm::SmallString<128> RuntimeLibPath;
Expand Down