From 2e6e4b53b2966a4f777882578885b5305722d9d0 Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Thu, 10 Oct 2024 19:21:20 +0200 Subject: [PATCH] [Lib/Optimizations]: Garbage-collect unused functions Somewhat similar in effect as LTO, but much less prone to bugs. --- lib/optimizations.nix | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/optimizations.nix b/lib/optimizations.nix index 38e6237..88e74a0 100644 --- a/lib/optimizations.nix +++ b/lib/optimizations.nix @@ -17,6 +17,11 @@ let # Perform loop distribution of patterns that can be code generated with calls to a library (activated at O3 and more) "-ftree-loop-distribute-patterns" + # Put each function declaration in its own text section + "-ffunction-sections" + # Instruct the linker to garbage-collect unused sections, which combined with the above removes dead functions + "-Wl,--gc-sections" + "-frecord-gcc-switches" # Record build flags in binary. Also supported by Clang ]; @@ -26,6 +31,7 @@ let ]; genericLinkerFlags = [ "-Wl,-sort-common" + "-Wl,--gc-sections" ]; ltoFlags = { threads ? 1, thin ? false }: [ # Fat LTO objects are object files that contain both the intermediate language and the object code. This makes them usable for both LTO linking and normal linking.