From 310cfcbcc458ca33de616fa066deeb2c04b6ad23 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Tue, 20 Jul 2021 05:22:09 -0700 Subject: [PATCH] Works around static-pie crashing static-pie is crashing early due to an issue with pthread symbols being exposed as weak. For some reason the weak symbols never get resolved and jump to zero or ELF base. When linking static-pie, force search for the pthread symbols using an undefined glob. With this, static-pie works as long as you link with a new enough version of lld (version 13.0) --- CMakeLists.txt | 8 ++++++++ External/FEXCore/Source/CMakeLists.txt | 4 ++-- Source/Tests/CMakeLists.txt | 10 +++++----- Source/Tests/LinuxSyscalls/CMakeLists.txt | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1621b22a5..fbdf33a10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,12 +56,17 @@ if (ENABLE_XRAY) link_libraries(-fxray-instrument) endif() +set (PTHREAD_LIB pthread) if (ENABLE_LLD) set (LD_OVERRIDE "-fuse-ld=lld") link_libraries(${LD_OVERRIDE}) endif() if (ENABLE_STATIC_PIE) + if (NOT ENABLE_LLD) + message (FATAL_ERROR "static-pie requires linking with lld") + endif() + file(WRITE ${PROJECT_BINARY_DIR}/CMakeFiles/CMakeTmp/Determine_iplt.c "int main(int argc, char* argv[]) { @@ -121,6 +126,9 @@ if (ENABLE_STATIC_PIE) if (SYMBOLS_FINE) # We can now exnable static-pie set (STATIC_PIE_OPTIONS "-static-pie") + # Pthreads has an issue with exposing symbols + # We need to make some concessions to the pthread gods + set (PTHREAD_LIB -Wl,--undefined-glob=pthread_* pthread) else() message (FATAL_ERROR "Application has __rela_iplt_{start,end} symbols. Which means static-pie can't be enabled") endif() diff --git a/External/FEXCore/Source/CMakeLists.txt b/External/FEXCore/Source/CMakeLists.txt index 2a87d044e..9cc0b3f8b 100644 --- a/External/FEXCore/Source/CMakeLists.txt +++ b/External/FEXCore/Source/CMakeLists.txt @@ -269,7 +269,7 @@ function(AddObject Name Type) add_dependencies(${Name} IR_INC) add_dependencies(${Name} CONFIG_INC) - target_link_libraries(${Name} pthread vixl dl fmt::fmt xxhash FEX_jemalloc) + target_link_libraries(${Name} vixl dl fmt::fmt xxhash) set_target_properties(${Name} PROPERTIES OUTPUT_NAME FEXCore) set_target_properties(${Name} PROPERTIES C_VISIBILITY_PRESET hidden) set_target_properties(${Name} PROPERTIES CXX_VISIBILITY_PRESET hidden) @@ -310,7 +310,7 @@ endfunction() function(AddLibrary Name Type) add_library(${Name} ${Type} $) - target_link_libraries(${Name} pthread vixl dl fmt::fmt xxhash FEX_jemalloc) + target_link_libraries(${Name} vixl dl fmt::fmt xxhash) set_target_properties(${Name} PROPERTIES OUTPUT_NAME FEXCore) set_target_properties(${Name} PROPERTIES C_VISIBILITY_PRESET hidden) set_target_properties(${Name} PROPERTIES CXX_VISIBILITY_PRESET hidden) diff --git a/Source/Tests/CMakeLists.txt b/Source/Tests/CMakeLists.txt index 433a17cf6..8f1c9b876 100644 --- a/Source/Tests/CMakeLists.txt +++ b/Source/Tests/CMakeLists.txt @@ -6,7 +6,7 @@ add_executable(FEXLoader FEXLoader.cpp) target_include_directories(FEXLoader PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Source/) target_include_directories(FEXLoader PRIVATE ${CMAKE_BINARY_DIR}/generated) -target_link_libraries(FEXLoader ${LIBS} LinuxEmulation ${STATIC_PIE_OPTIONS}) +target_link_libraries(FEXLoader ${LIBS} LinuxEmulation ${STATIC_PIE_OPTIONS} ${PTHREAD_LIB}) if (CMAKE_BUILD_TYPE MATCHES "RELEASE") target_link_options(FEXLoader @@ -63,7 +63,7 @@ add_executable(FEXBash FEXBash.cpp) target_include_directories(FEXBash PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Source/) target_include_directories(FEXBash PRIVATE ${CMAKE_BINARY_DIR}/generated) -target_link_libraries(FEXBash ${LIBS} LinuxEmulation ${STATIC_PIE_OPTIONS}) +target_link_libraries(FEXBash ${LIBS} LinuxEmulation ${STATIC_PIE_OPTIONS} ${PTHREAD_LIB}) if (CMAKE_BUILD_TYPE MATCHES "RELEASE") target_link_options(FEXBash @@ -83,12 +83,12 @@ add_executable(TestHarnessRunner TestHarnessRunner.cpp) target_include_directories(TestHarnessRunner PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Source/) target_include_directories(TestHarnessRunner PRIVATE ${CMAKE_BINARY_DIR}/generated) -target_link_libraries(TestHarnessRunner ${LIBS} LinuxEmulation ${STATIC_PIE_OPTIONS}) +target_link_libraries(TestHarnessRunner ${LIBS} LinuxEmulation ${STATIC_PIE_OPTIONS} ${PTHREAD_LIB}) add_executable(UnitTestGenerator UnitTestGenerator.cpp) target_include_directories(UnitTestGenerator PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Source/) -target_link_libraries(UnitTestGenerator ${LIBS} ${STATIC_PIE_OPTIONS}) +target_link_libraries(UnitTestGenerator ${LIBS} ${STATIC_PIE_OPTIONS} ${PTHREAD_LIB}) add_executable(IRLoader IRLoader.cpp @@ -97,5 +97,5 @@ add_executable(IRLoader target_include_directories(IRLoader PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Source/) target_include_directories(IRLoader PRIVATE ${CMAKE_BINARY_DIR}/generated) -target_link_libraries(IRLoader ${LIBS} LinuxEmulation ${STATIC_PIE_OPTIONS}) +target_link_libraries(IRLoader ${LIBS} LinuxEmulation ${STATIC_PIE_OPTIONS} ${PTHREAD_LIB}) diff --git a/Source/Tests/LinuxSyscalls/CMakeLists.txt b/Source/Tests/LinuxSyscalls/CMakeLists.txt index 7e5c528bd..72404d7d9 100644 --- a/Source/Tests/LinuxSyscalls/CMakeLists.txt +++ b/Source/Tests/LinuxSyscalls/CMakeLists.txt @@ -57,7 +57,7 @@ add_library(LinuxEmulation STATIC Syscalls/Stubs.cpp ) -target_link_libraries(LinuxEmulation FEXCore pthread FEX_Utils) +target_link_libraries(LinuxEmulation FEXCore FEX_Utils) target_include_directories(LinuxEmulation PRIVATE ${CMAKE_BINARY_DIR}/generated) target_include_directories(LinuxEmulation PRIVATE ${PROJECT_SOURCE_DIR}/External/drm-headers/include/)