diff --git a/CMakeLists.txt b/CMakeLists.txt index e098d8d74740..178b6be11a13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -622,16 +622,19 @@ include(cmake/dbms_glob_sources.cmake) # For some sophisticated platforms we have different issues with fPIC: # For example riscv toolchain itself compiled without fPIC # +# The code is compiled with -fPIE, not -fPIC: it is linked into an executable, where the symbol +# interposition that -fPIC pays for (GOT loads, TLS descriptors) never happens. +# # This has to be configured before the first targets are created, in particular before # cmake/linux/default_libs.cmake adds the C++ runtime libraries from contrib: CMake copies the flags # into each subdirectory when add_subdirectory is called, so targets created earlier would silently -# compile without -fPIC. That only breaks with compilers that do not default to PIE +# compile without -fPIE. That only breaks with compilers that do not default to PIE # (clang with CLANG_DEFAULT_PIE_ON_LINUX=OFF, as configured by some distributions), because then the # non-relocatable objects of those libraries cannot be linked into the PIE executable. if (ARCH_AMD64 OR ARCH_AARCH64) set(ENABLE_POSITION_INDEPENDENT_BINARY 1) - # -fPIC only makes the code relocatable; whether the executable itself is position independent is + # -fPIE only makes the code relocatable; whether the executable itself is position independent is # the compiler driver's default unless the linker is asked explicitly, so ask it. # musl links statically, and -static-pie needs an rcrt1.o that contrib/musl-cmake does not build. # The FreeBSD and Darwin sysroots carry no PIC startup objects (Scrt1.o). @@ -642,8 +645,8 @@ endif() if (ENABLE_POSITION_INDEPENDENT_BINARY) set (CMAKE_POSITION_INDEPENDENT_CODE OFF) - set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") - set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE") + set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE") endif() add_library(global-group INTERFACE) @@ -659,13 +662,6 @@ elseif (OS_WASM) include(cmake/wasm/default_libs.cmake) endif () -if (ENABLE_POSITION_INDEPENDENT_BINARY) - # The C++ runtime libraries are linked only into executables, so they are compiled as PIE (the last flag wins). - foreach (runtime_target cxx cxxabi unwind) - target_compile_options (${runtime_target} PRIVATE $<$:-fPIE>) - endforeach () -endif () - link_libraries(global-group) target_link_libraries(global-group INTERFACE $) diff --git a/ci/jobs/compatibility_check.py b/ci/jobs/compatibility_check.py index ea0c561d4850..08f5452dc027 100644 --- a/ci/jobs/compatibility_check.py +++ b/ci/jobs/compatibility_check.py @@ -56,7 +56,7 @@ def process_glibc_check(): def process_pie_check(): - # Nothing in the build fails when the binary comes out position dependent: -fPIC only makes the + # Nothing in the build fails when the binary comes out position dependent: -fPIE only makes the # code relocatable, and whether the executable itself is position independent is the linker's # decision, so it is checked on the packaged binary instead. header = Shell.get_output( diff --git a/cmake/cuda.cmake b/cmake/cuda.cmake index 9c20b8c92601..763b00c9277f 100644 --- a/cmake/cuda.cmake +++ b/cmake/cuda.cmake @@ -130,7 +130,7 @@ if (ENABLE_GPU) # It has to reach gcc through -Xcompiler. nvcc rejects `-std=gnu++20` for itself # ("Value 'gnu++20' is not defined for option 'std'"), and CMAKE_CUDA_EXTENSIONS is a # no-op here - CMake emits -std=c++20 for nvcc either way. - string (APPEND CMAKE_CUDA_FLAGS " --expt-relaxed-constexpr -Xcompiler -fPIC -Xcompiler -std=gnu++20") + string (APPEND CMAKE_CUDA_FLAGS " --expt-relaxed-constexpr -Xcompiler -fPIE -Xcompiler -std=gnu++20") option (ENABLE_GPU_KERNEL_LINEINFO "Emit CUDA line info, for profiling kernels in Nsight Compute" OFF) @@ -166,7 +166,6 @@ if (ENABLE_GPU) COMPILE_OPTIONS "" COMPILE_DEFINITIONS "" INCLUDE_DIRECTORIES "" - POSITION_INDEPENDENT_CODE ON ) get_target_property (_gpu_island_srcs ${target} SOURCES) diff --git a/cmake/sanitize.cmake b/cmake/sanitize.cmake index 1df7f3a0501c..8143ac5f195a 100644 --- a/cmake/sanitize.cmake +++ b/cmake/sanitize.cmake @@ -102,8 +102,8 @@ if (SANITIZE) endif () # Linking can fail due to relocation overflows (see #49145), caused by too big object files / libraries. - # Work around this with position-independent builds (-fPIC and -fpie), this is slightly slower than non-PIC/PIE but that's okay. - set (MSAN_FLAGS "-fsanitize=memory -fsanitize-memory-use-after-dtor -fsanitize-memory-track-origins=${MSAN_TRACK_ORIGINS_LEVEL} -fPIC -fpie ${MSAN_X86_VZEROUPPER_FLAGS}") + # Work around this with position-independent builds (-fPIE), this is slightly slower than non-PIC/PIE but that's okay. + set (MSAN_FLAGS "-fsanitize=memory -fsanitize-memory-use-after-dtor -fsanitize-memory-track-origins=${MSAN_TRACK_ORIGINS_LEVEL} -fPIE ${MSAN_X86_VZEROUPPER_FLAGS}") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS} ${MSAN_FLAGS}") set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS} ${MSAN_FLAGS}") diff --git a/contrib/cld2-cmake/CMakeLists.txt b/contrib/cld2-cmake/CMakeLists.txt index 8600856ea36a..73e0918b8dfe 100644 --- a/contrib/cld2-cmake/CMakeLists.txt +++ b/contrib/cld2-cmake/CMakeLists.txt @@ -27,7 +27,6 @@ set (SRCS "${LIBRARY_DIR}/internal/cld_generated_score_quad_octa_2.cc" ) add_library(_cld2 ${SRCS}) -set_property(TARGET _cld2 PROPERTY POSITION_INDEPENDENT_CODE ON) target_compile_options (_cld2 PRIVATE -Wno-reserved-id-macro -Wno-c++11-narrowing) target_include_directories(_cld2 SYSTEM BEFORE PUBLIC "${LIBRARY_DIR}/public") add_library(ch_contrib::cld2 ALIAS _cld2) diff --git a/contrib/cudf-cmake/CMakeLists.txt b/contrib/cudf-cmake/CMakeLists.txt index 75781e9cd792..eb65c3fd9f24 100644 --- a/contrib/cudf-cmake/CMakeLists.txt +++ b/contrib/cudf-cmake/CMakeLists.txt @@ -125,7 +125,6 @@ function (cudf_add_fragment FRAGMENT INSTANCE UDF_TYPE FILE_INDEX) CUDA_SEPARABLE_COMPILATION ON CUDA_FATBIN_COMPILATION ON INTERPROCEDURAL_OPTIMIZATION ON - POSITION_INDEPENDENT_CODE ON CUDA_STANDARD 20 CUDA_STANDARD_REQUIRED ON CUDA_VISIBILITY_PRESET hidden) diff --git a/contrib/xgboost-cmake/CMakeLists.txt b/contrib/xgboost-cmake/CMakeLists.txt index cb4fea1ff192..be9fe2d7e5a5 100644 --- a/contrib/xgboost-cmake/CMakeLists.txt +++ b/contrib/xgboost-cmake/CMakeLists.txt @@ -159,7 +159,6 @@ configure_file( set(DMLC_CORE_COMPILE_OPTIONS -Wall -Wno-unknown-pragmas - -fPIC ) # `-msse2` is only meaningful on x86. @@ -291,7 +290,7 @@ set(XGBOOST_SOURCES add_library(_xgboost ${XGBOOST_SOURCES}) target_include_directories(_xgboost SYSTEM PUBLIC ${XGBOOST_INCLUDE_DIR} ${DMLC_CORE_INCLUDE_DIR}) -target_compile_options(_xgboost PRIVATE -fPIC -std=c++17 -include exception) +target_compile_options(_xgboost PRIVATE -std=c++17 -include exception) target_link_libraries(_xgboost PRIVATE _dmlc_core Threads::Threads ${CMAKE_THREAD_LIBS_INIT}