From 6cdbb14cf95284d1fe949c13fb3e478dca101d48 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 14 May 2026 22:25:35 +0000 Subject: [PATCH 1/3] Globally enable linker dead-code elimination on Unix Co-authored-by: elinor-fung <47805090+elinor-fung@users.noreply.github.com> --- eng/native/configurecompiler.cmake | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index e3669f462a9efc..4ca1fb70c1e854 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -321,6 +321,21 @@ if(CLR_CMAKE_HOST_UNIX) endforeach() endif(CLR_CMAKE_HOST_UNIX) +# Strip unreferenced sections at link time, paired with -ffunction-sections/-fdata-sections +# (set in the Unix/WASI compile-options block below). Gated to non-Debug builds so Debug +# keeps full symbols and incremental linking. Windows uses /OPT:REF, configured above. +if(CLR_CMAKE_HOST_UNIX) + if(CLR_CMAKE_HOST_APPLE) + add_linker_flag(-Wl,-dead_strip CHECKED) + add_linker_flag(-Wl,-dead_strip RELEASE) + add_linker_flag(-Wl,-dead_strip RELWITHDEBINFO) + else() + add_linker_flag(-Wl,--gc-sections CHECKED) + add_linker_flag(-Wl,--gc-sections RELEASE) + add_linker_flag(-Wl,--gc-sections RELWITHDEBINFO) + endif() +endif() + if(CLR_CMAKE_HOST_LINUX) add_compile_options($<$:-Wa,--noexecstack>) add_linker_flag(-Wl,--build-id=sha1) @@ -740,8 +755,11 @@ if (CLR_CMAKE_HOST_UNIX OR CLR_CMAKE_HOST_WASI) # We mark the function which needs exporting with DLLEXPORT add_compile_options(-fvisibility=hidden) - # Separate functions so linker can remove them. + # Separate functions and data into their own sections so the linker can remove + # unreferenced ones via --gc-sections (ELF) / -dead_strip (Mach-O), which are + # enabled below for non-Debug configurations. add_compile_options(-ffunction-sections) + add_compile_options(-fdata-sections) # Specify the minimum supported version of macOS # Mac Catalyst needs a special CFLAG, exclusive with mmacosx-version-min From 41177a5560210b67a17f52ce581c6187fb65e719 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Thu, 14 May 2026 18:22:04 -0700 Subject: [PATCH 2/3] Update eng/native/configurecompiler.cmake Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com> --- eng/native/configurecompiler.cmake | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index 4ca1fb70c1e854..847697fb0dc66d 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -326,13 +326,9 @@ endif(CLR_CMAKE_HOST_UNIX) # keeps full symbols and incremental linking. Windows uses /OPT:REF, configured above. if(CLR_CMAKE_HOST_UNIX) if(CLR_CMAKE_HOST_APPLE) - add_linker_flag(-Wl,-dead_strip CHECKED) - add_linker_flag(-Wl,-dead_strip RELEASE) - add_linker_flag(-Wl,-dead_strip RELWITHDEBINFO) + add_linker_flag($<$:-Wl,-dead_strip>) else() - add_linker_flag(-Wl,--gc-sections CHECKED) - add_linker_flag(-Wl,--gc-sections RELEASE) - add_linker_flag(-Wl,--gc-sections RELWITHDEBINFO) + add_linker_flag($<$:-Wl,--gc-sections>) endif() endif() From 1b6ca5222348526f595ef8da8137f0d7381f2d91 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Fri, 15 May 2026 08:40:07 -0700 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- eng/native/configurecompiler.cmake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index 847697fb0dc66d..c4c438106ed20c 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -326,9 +326,9 @@ endif(CLR_CMAKE_HOST_UNIX) # keeps full symbols and incremental linking. Windows uses /OPT:REF, configured above. if(CLR_CMAKE_HOST_UNIX) if(CLR_CMAKE_HOST_APPLE) - add_linker_flag($<$:-Wl,-dead_strip>) + add_linker_flag(-Wl,-dead_strip CHECKED RELEASE RELWITHDEBINFO) else() - add_linker_flag($<$:-Wl,--gc-sections>) + add_linker_flag(-Wl,--gc-sections CHECKED RELEASE RELWITHDEBINFO) endif() endif() @@ -753,7 +753,7 @@ if (CLR_CMAKE_HOST_UNIX OR CLR_CMAKE_HOST_WASI) # Separate functions and data into their own sections so the linker can remove # unreferenced ones via --gc-sections (ELF) / -dead_strip (Mach-O), which are - # enabled below for non-Debug configurations. + # enabled earlier in this file for non-Debug configurations. add_compile_options(-ffunction-sections) add_compile_options(-fdata-sections)