--compress-sections <section-glib>=[zlib|zstd] is like a generalized
--compress-debug-sections that applies to arbitrary sections, including
SHF_ALLOC ones. This option has a number of candidate use cases for metadata
sections, including:
- code coverage sections https://github.com/llvm/llvm-project/issues/48499
- __asan_globals section
- -fexperimental-sanitize-metadata= sections
For SHF_ALLOC use cases, a supporting runtime library can identify the section
content with a pair of symbols __start_<sectionname> and
__stop_<sectionname> and check the header to know whether it is compressed or
not. There are some caveats:
- We compute the section content/size once in finalizeAddressDependentContent before compression. If the content or size changes, the compressed content will be invalid, but we don't detect changed content (e.g., data commands). However, we detect size changes in assignOffsets.
- If there are dynamic relocations, rtld do not skip these relocations and will cause runtime crash or writable data corruption. In general, label differences should be used (see foo0 in the test) and the runtime library needs to adjust the differences.
- Symbols defined relative to the output section desginate the offsets to the uncompressed content.
In addition, compressing synthetic sections like .symtab/.strtab and regular
data/code sections will be problematic, but we don't report an error.
GNU ld feature request: https://sourceware.org/bugzilla/show_bug.cgi?id=27452
Link: https://discourse.llvm.org/t/rfc-compress-arbitrary-sections-with-ld-lld-compress-sections/71674
Link: https://groups.google.com/g/generic-abi/c/HUVhliUrTG0 ("Allow SHF_ALLOC | SHF_COMPRESSED sections")