From dbb8f3ff3ff66d3f1f0a0116ef70c81ebf3d6d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 8 Dec 2022 02:51:28 +0900 Subject: [PATCH] Compile native code with `-ffunction-sections` (#79322) --- eng/native/configurecompiler.cmake | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index 9090289f50e3..ae8a244a5b99 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -160,9 +160,8 @@ elseif (CLR_CMAKE_HOST_UNIX) list(JOIN CLR_LINK_SANITIZERS "," CLR_LINK_SANITIZERS_OPTIONS) list(APPEND CLR_SANITIZE_LINK_OPTIONS "-fsanitize=${CLR_LINK_SANITIZERS_OPTIONS}") - # -fdata-sections -ffunction-sections: each function has own section instead of one per .o file (needed for --gc-sections) # -O1: optimization level used instead of -O0 to avoid compile error "invalid operand for inline asm constraint" - add_compile_options("$<$,$>:${CLR_SANITIZE_CXX_OPTIONS};-fdata-sections;--ffunction-sections;-O1>") + add_compile_options("$<$,$>:${CLR_SANITIZE_CXX_OPTIONS};-fdata-sections;-O1>") add_linker_flag("${CLR_SANITIZE_LINK_OPTIONS}" DEBUG CHECKED) # -Wl and --gc-sections: drop unused sections\functions (similar to Windows /Gy function-level-linking) add_linker_flag("-Wl,--gc-sections" DEBUG CHECKED) @@ -478,6 +477,12 @@ if (CLR_CMAKE_HOST_UNIX) # We mark the function which needs exporting with DLLEXPORT add_compile_options(-fvisibility=hidden) + + # Separate functions so linker can remove them. But not on tvOS because + # -ffunction-sections is not supported with -fembed-bitcode. + if (NOT CLR_CMAKE_HOST_TVOS) + add_compile_options(-ffunction-sections) + endif() # Specify the minimum supported version of macOS # Mac Catalyst needs a special CFLAG, exclusive with mmacosx-version-min