From 54da527186c00338556bff6d2acf0b79947f8ebc Mon Sep 17 00:00:00 2001 From: Cheng Shao <terrorjack@type.dance> Date: Thu, 23 May 2024 13:39:22 +0000 Subject: [PATCH] driver: build C/C++ with -ffunction-sections -fdata-sections when split sections is enabled When -fsplit-sections is passed to GHC, pass -ffunction-sections -fdata-sections to gcc/clang when building C/C++. Previously, -fsplit-sections was only respected by the NCG/LLVM backends, but not the unregisterised backend; the GHC driver did not pass -fdata-sections and -ffunction-sections to the C compiler, which resulted in excessive executable sizes. Fixes #23381. ------------------------- Metric Decrease: size_hello_artifact size_hello_unicode ------------------------- (cherry picked from commit 02b1f91e93b4e26c8fc227f48878a66f342cb68f) (cherry picked from commit 74cdf5648eab0c0843ab6264e003fe7167e701bf) (cherry picked from commit 03da9015b3b12613aa3f2964f07b669ef52df5ed) --- compiler/GHC/Driver/Pipeline/Execute.hs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compiler/GHC/Driver/Pipeline/Execute.hs b/compiler/GHC/Driver/Pipeline/Execute.hs index fac2a595f22..f47d9672720 100644 --- a/compiler/GHC/Driver/Pipeline/Execute.hs +++ b/compiler/GHC/Driver/Pipeline/Execute.hs @@ -534,6 +534,13 @@ runCcPhase cc_phase pipe_env hsc_env location input_fn = do , not $ target32Bit (targetPlatform dflags) ] + -- if -fsplit-sections is enabled, we should also + -- build with these flags. + ++ (if gopt Opt_SplitSections dflags && + platformOS (targetPlatform dflags) /= OSDarwin + then ["-ffunction-sections", "-fdata-sections"] + else []) + -- Stub files generated for foreign exports references the runIO_closure -- and runNonIO_closure symbols, which are defined in the base package. -- These symbols are imported into the stub.c file via RtsAPI.h, and the -- GitLab