Skip to content

Commit eb6a878

Browse files
Copilotelinor-fungam11Copilotjkotas
authored
Globally enable linker dead-code elimination on Unix (#128232)
-ffunction-sections is set globally for non-MSVC builds in eng/native/configurecompiler.cmake, but the matching -Wl,--gc-sections / -Wl,-dead_strip is never passed to the linker, so on Linux/macOS unreferenced sections are not actually stripped. Windows is unaffected because /OPT:REF is set globally. `eng/native/configurecompiler.cmake`: - **Compile**: add `-fdata-sections` alongside the existing `-ffunction-sections` (Unix/WASI) so unreferenced data, not just functions, can be stripped. - **Link, Apple**: `-Wl,-dead_strip` for Checked / Release / RelWithDebInfo. - **Link, other Unix **: `-Wl,--gc-sections` for the same three configurations. - **Debug**: untouched, matching Windows configuration Targets that need specific symbols retained can still opt out per-symbol via `KEEP` / `__attribute__((used))` / export lists; `-fvisibility=hidden` plus existing export lists already cover the common cases. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: elinor-fung <47805090+elinor-fung@users.noreply.github.com> Co-authored-by: Elinor Fung <elfung@microsoft.com> Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Jan Kotas <jkotas@microsoft.com>
1 parent a49d0cf commit eb6a878

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

eng/native/configurecompiler.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,15 @@ if (CLR_CMAKE_HOST_UNIX OR CLR_CMAKE_HOST_WASI)
740740
# We mark the function which needs exporting with DLLEXPORT
741741
add_compile_options(-fvisibility=hidden)
742742

743-
# Separate functions so linker can remove them.
743+
# Separate functions and data into their own sections so the linker can remove
744+
# unreferenced ones.
744745
add_compile_options(-ffunction-sections)
746+
add_compile_options(-fdata-sections)
747+
if(LD_OSX)
748+
add_linker_flag(-Wl,-dead_strip CHECKED RELEASE RELWITHDEBINFO)
749+
elseif(NOT LD_SOLARIS)
750+
add_linker_flag(-Wl,--gc-sections CHECKED RELEASE RELWITHDEBINFO)
751+
endif()
745752

746753
# Specify the minimum supported version of macOS
747754
# Mac Catalyst needs a special CFLAG, exclusive with mmacosx-version-min

0 commit comments

Comments
 (0)