]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
meson: add -ffunction-sections and -fdata-sections to compiler flags
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 4 Jul 2026 16:32:27 +0000 (01:32 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 10 Jul 2026 07:29:54 +0000 (16:29 +0900)
Enable compiler section splitting for functions and data to allow the
linker's `--gc-sections` optimization to accurately drop unused code.

Note that in highly optimized production builds with Link-Time Optimization
(LTO) enabled (such as `-Db_lto=true`), the compiler already performs
intensive dead-code elimination, resulting in negligible binary size changes
from this change.

However, explicit section splitting provides clear benefits in other areas:
1. Significant size reduction for `.standalone` executables and libraries
   in non-LTO environments.
2. Crucially, it provides the necessary infrastructure for the linker to
   garbage-collect unused `dlopen` metadata notes implemented in subsequent
   commits, ensuring precise dependency tracking regardless of the LTO state.

Below is a binary size comparison (unstripped) across different build types:
- `build-debug`: `-Dbuildtype=debug`
- `build-plain`: `-Dbuildtype=plain` (without LTO)
- `build-plain-lto`: `-Dbuildtype=plain -Db_lto=true`

Before:
```
-rwxr-xr-x 1 watanabe watanabe 6143952 Jul 10 03:10 build-debug/libsystemd.so.0.44.0
-rwxr-xr-x 1 watanabe watanabe  830224 Jul 10 03:10 build-debug/systemd-repart
-rwxr-xr-x 1 watanabe watanabe 9107880 Jul 10 03:10 build-debug/systemd-repart.standalone
-rwxr-xr-x 1 watanabe watanabe 3581856 Jul 10 03:11 build-plain-lto/libsystemd.so.0.44.0
-rwxr-xr-x 1 watanabe watanabe  515536 Jul 10 03:11 build-plain-lto/systemd-repart
-rwxr-xr-x 1 watanabe watanabe 5017000 Jul 10 03:11 build-plain-lto/systemd-repart.standalone
-rwxr-xr-x 1 watanabe watanabe 3352616 Jul 10 03:11 build-plain/libsystemd.so.0.44.0
-rwxr-xr-x 1 watanabe watanabe  515232 Jul 10 03:11 build-plain/systemd-repart
-rwxr-xr-x 1 watanabe watanabe 5103176 Jul 10 03:11 build-plain/systemd-repart.standalone
```

After:
```
-rwxr-xr-x 1 watanabe watanabe 5424064 Jul 10 03:04 build-debug/libsystemd.so.0.44.0
-rwxr-xr-x 1 watanabe watanabe  850880 Jul 10 03:04 build-debug/systemd-repart
-rwxr-xr-x 1 watanabe watanabe 7138496 Jul 10 03:04 build-debug/systemd-repart.standalone
-rwxr-xr-x 1 watanabe watanabe 3581856 Jul 10 03:06 build-plain-lto/libsystemd.so.0.44.0
-rwxr-xr-x 1 watanabe watanabe  515536 Jul 10 03:06 build-plain-lto/systemd-repart
-rwxr-xr-x 1 watanabe watanabe 5017000 Jul 10 03:06 build-plain-lto/systemd-repart.standalone
-rwxr-xr-x 1 watanabe watanabe 2321048 Jul 10 03:06 build-plain/libsystemd.so.0.44.0
-rwxr-xr-x 1 watanabe watanabe  514056 Jul 10 03:06 build-plain/systemd-repart
-rwxr-xr-x 1 watanabe watanabe 2716264 Jul 10 03:06 build-plain/systemd-repart.standalone
```

meson.build

index f9c84767dee83ecaa1bea535c0fac1ac04b2a872..b9de4656b708bfb5fc7ad289ae264b4a0302e864 100644 (file)
@@ -431,7 +431,9 @@ possible_common_cc_flags = [
         '-Wno-string-plus-int',  # clang
 
         '-fdiagnostics-show-option',
+        '-fdata-sections',
         '-fexcess-precision=standard',
+        '-ffunction-sections',
         '-fno-common',
         '-fstack-protector',
         '-fstack-protector-strong',