From 5eed82c2bb7d1236d6b0bcb8fe1b6dc220284df1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 14 May 2026 21:18:10 +0000 Subject: [PATCH] Enable linker dead code elimination for apphost, dotnet, and nethost Co-authored-by: elinor-fung <47805090+elinor-fung@users.noreply.github.com> --- eng/native/functions.cmake | 18 ++++++++++++++++++ .../corehost/apphost/standalone/CMakeLists.txt | 1 + src/native/corehost/dotnet/CMakeLists.txt | 1 + src/native/corehost/nethost/CMakeLists.txt | 2 ++ 4 files changed, 22 insertions(+) diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index f7066de09a764c..52459476d143d7 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -655,6 +655,24 @@ function(link_natvis_sources_for_target targetName linkKind) endforeach() endfunction() +# Enable linker dead code elimination for the given target in CHECKED/RELEASE/RELWITHDEBINFO +function(enable_dead_code_elimination targetName) + set(dce_configs "$,$,$>") + + if (MSVC) + target_link_options(${targetName} PRIVATE $<${dce_configs}:/OPT:REF>) + return() + endif() + + target_compile_options(${targetName} PRIVATE $<${dce_configs}:-fdata-sections>) + + if (CLR_CMAKE_HOST_APPLE) + target_link_options(${targetName} PRIVATE $<${dce_configs}:LINKER:-dead_strip>) + else() + target_link_options(${targetName} PRIVATE $<${dce_configs}:LINKER:--gc-sections>) + endif() +endfunction() + # Add sanitizer runtime support code to the target. function(add_sanitizer_runtime_support targetName) # Add sanitizer support functions. diff --git a/src/native/corehost/apphost/standalone/CMakeLists.txt b/src/native/corehost/apphost/standalone/CMakeLists.txt index 17b114fd0c373c..0f3e1ecc7a32ee 100644 --- a/src/native/corehost/apphost/standalone/CMakeLists.txt +++ b/src/native/corehost/apphost/standalone/CMakeLists.txt @@ -34,6 +34,7 @@ add_executable(apphost ${SOURCES} ${RESOURCES}) target_link_libraries(apphost PRIVATE hostmisc fxr_resolver) add_sanitizer_runtime_support(apphost) +enable_dead_code_elimination(apphost) if(NOT CLR_CMAKE_TARGET_WIN32) disable_pax_mprotect(apphost) diff --git a/src/native/corehost/dotnet/CMakeLists.txt b/src/native/corehost/dotnet/CMakeLists.txt index 5704c911ac9568..2aa3052967be8e 100644 --- a/src/native/corehost/dotnet/CMakeLists.txt +++ b/src/native/corehost/dotnet/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable(dotnet ${SOURCES}) target_link_libraries(dotnet PRIVATE hostmisc fxr_resolver) add_sanitizer_runtime_support(dotnet) +enable_dead_code_elimination(dotnet) if(NOT CLR_CMAKE_TARGET_WIN32) disable_pax_mprotect(dotnet) diff --git a/src/native/corehost/nethost/CMakeLists.txt b/src/native/corehost/nethost/CMakeLists.txt index 83c6f4aac8e3ec..b10dac5492b37d 100644 --- a/src/native/corehost/nethost/CMakeLists.txt +++ b/src/native/corehost/nethost/CMakeLists.txt @@ -38,6 +38,8 @@ target_link_libraries(libnethost PRIVATE hostmisc::public fxr_resolver) target_compile_definitions(nethost PRIVATE FEATURE_LIBHOST NETHOST_EXPORT) target_compile_definitions(libnethost PRIVATE FEATURE_LIBHOST NETHOST_EXPORT) +enable_dead_code_elimination(nethost) + set_target_properties(nethost PROPERTIES MACOSX_RPATH TRUE) set_target_properties(libnethost PROPERTIES MACOSX_RPATH TRUE) set_target_properties(libnethost PROPERTIES PREFIX "")