From 1f747b9132c57d0a14e1f3bbb03e19c7fa4e2721 Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Sun, 27 Aug 2023 12:13:47 +0200 Subject: [PATCH] CMake: Use `CMAKE_POSITION_INDEPENDENT_CODE` instead of explicit `-fpic` This makes CMake pass `-fpie` instead of `-fpic` to the compiler when building the Kernel and userland *executables*. This allows the compiler to make certain optimizations based on the fact that the code will be used in an executable, such as not having to emit `.localalias` symbols. This leads to a 450 KiB decrease in the size of the Kernel binary. --- CMakeLists.txt | 2 -- Meta/CMake/serenity_compile_options.cmake | 2 ++ Userland/DynamicLoader/CMakeLists.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 09570e7dd17..13bc7d23475 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,8 +167,6 @@ add_link_options(LINKER:-z,text) add_link_options(LINKER:--no-allow-shlib-undefined) add_compile_definitions(SANITIZE_PTRS) -set(CMAKE_CXX_FLAGS_STATIC "${CMAKE_CXX_FLAGS} -static") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pie -fpic") if (ENABLE_COMPILETIME_FORMAT_CHECK) add_compile_definitions(ENABLE_COMPILETIME_FORMAT_CHECK) diff --git a/Meta/CMake/serenity_compile_options.cmake b/Meta/CMake/serenity_compile_options.cmake index 6bab7b77085..7cc39f5a25d 100644 --- a/Meta/CMake/serenity_compile_options.cmake +++ b/Meta/CMake/serenity_compile_options.cmake @@ -27,6 +27,8 @@ add_link_options(-fstack-protector-strong) add_compile_options(-g1) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") add_compile_options(-Wno-maybe-uninitialized) add_compile_options(-Wcast-align) diff --git a/Userland/DynamicLoader/CMakeLists.txt b/Userland/DynamicLoader/CMakeLists.txt index e76ff69b680..469054966e0 100644 --- a/Userland/DynamicLoader/CMakeLists.txt +++ b/Userland/DynamicLoader/CMakeLists.txt @@ -30,7 +30,7 @@ add_definitions(-D_DYNAMIC_LOADER) set(SOURCES ${LOADER_SOURCES} ${AK_SOURCES} ${ELF_SOURCES} ${LIBC_SOURCES1} ${LIBC_SOURCES2} ${LIBC_SOURCES3} ${LIBSYSTEM_SOURCES}) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -nostdlib -pie -fpic -DNO_TLS") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -nostdlib -pie -fpie -DNO_TLS") if ("${SERENITY_ARCH}" STREQUAL "aarch64") # On aarch64 the stack protector would be accessed before the Loader can relocate itself.