diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index e3669f462a9efc..c4c438106ed20c 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -321,6 +321,17 @@ 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 RELEASE RELWITHDEBINFO) + else() + add_linker_flag(-Wl,--gc-sections CHECKED RELEASE RELWITHDEBINFO) + endif() +endif() + if(CLR_CMAKE_HOST_LINUX) add_compile_options($<$:-Wa,--noexecstack>) add_linker_flag(-Wl,--build-id=sha1) @@ -740,8 +751,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 earlier in this file 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