Better link-time garbage collection


Today I learned that by default, when linking static library, linker includes whole object that contains symbol required. In other word, linker do not eliminate unused symbols, only unused objects. Also I learned that there are GCC-specific compilation flags "-ffunction-sections -fdata-sections" and "-Wl,--gc-sections" link flags that together enable per-symbol granularity.

I was very enthusiastic about this discovery, but when enabled these flags globally in "pkgs/stdenv/adapters.nix", I got error when building busybox:

    GEN     include/applet_tables.h include/NUM_APPLETS.h
    HOSTCC  applets/usage_pod
    CC      applets/applets.o
    LD      applets/built-in.o
  /nix/store/f7b0y2q80kgnqjkjmh7cng048wsy6mw9-x86_64-unknown-linux-musl-binutils-2.31.1/bin/x86_64-unknown-linux-musl-ld: gc-sections requires either an entry or an undefined symbol
  collect2: error: ld returned 1 exit status
  make[1]: *** [scripts/Makefile.build:264: applets/built-in.o] Error 1
  make: *** [Makefile:372: applets_dir] Error 2

Documentation for --gc-sections mentions that in case of partial linking, garbage collection root must be provided with either --entry or --undefined. According to same manual, partial linking is process when output of linker is passed to another invocation of linker. Why would anyone want it?