From eb9925ffec6c5e0b0fc4404bbff11d7ba79090cf Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Sun, 14 Jun 2026 16:20:48 -0400 Subject: [PATCH] Only pass -pie when linking executables Passing -pie globally made the driver link Scrt1.o into shared libraries, which fails with an undefined reference to main. --- CMakeLists.txt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4213914..252a2fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,12 +51,16 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") add_compile_options("-Wno-maybe-uninitialized") endif() -set(full_relro_flags "-pie;LINKER:-z,relro,-z,now,-z,noexecstack") +set(relro_flags "LINKER:-z,relro,-z,now,-z,noexecstack") +set(full_relro_flags "-pie;${relro_flags}") cmake_push_check_state() list(APPEND CMAKE_REQUIRED_LINK_OPTIONS ${full_relro_flags}) check_cxx_source_compiles("int main(){}" HAS_FULL_RELRO FAIL_REGEX "warning:") if(HAS_FULL_RELRO) - add_link_options(${full_relro_flags}) + # -pie only applies to executables; passing it when linking a shared library + # makes the driver pull in Scrt1.o, which requires main. + add_link_options("$<$,EXECUTABLE>:-pie>" + ${relro_flags}) endif() cmake_pop_check_state()