In January I wrote Compressed debug sections. The venerable zlib shows its age and there are replacements which are better in every metric except adoption and a larger memory footprint. The obvious choice was Zstandard, but I was not so confident about adoptinig it and solving the ecosystem issue. At any rate, I slowly removed some legacy .zdebug support from llvm-project so that a new format could be more easily introduced.
In June, Cole Kissane posted [RFC] Zstandard as a second compression method to LLVM on LLVM discourse forums. I learned that other folks were investigating a better compression format for ELF compressed debug sections and told myself: it's high time to propose ELFCOMPRESS_ZSTD to the generic System V Application Binary Interface (generic ABI).
ELF is an elegant format which has passed the test of time. Many things created by the forefathers from 30 years ago carry over and are still used today. Every new feature, even a small addition like introducing a new constant has to pass a significant high bar for acceptance. There were many discussions on Add new ch_type value: ELFCOMPRESS_ZSTD.
Personally I think a selected format need to have these properties:
It has an open compression algorithm and implementation.
It provides significant benefits (compression speed, decompression speed, compression ratio) with a decent memory footprint and complexity.
It has full backward compatibility. In 20 years I want to be able to decompress a debug section created today.
It has a wide range of and active use cases. When the format value is standardized, consumers are willing to add support.
It has good documentation.
It's easy to use.
A compression format satisfying all these properties are rare. ELF does not like introducing a lot of options for one feature. It's not an experiment site for every new fancy compression format. We are wary of platform fragmentation and consumers don't like support a number of formats each claiming to be a good choice at a slightly different angle. See the appendix for my recent test of many compression utilities.
I made many arguments in the proposal thread. It took about one month and ELFCOMPRESS_ZSTD was accepted in 2022-07.
The next step is to add toolchain support. The most important pieces are assemblers, linkers, and debuggers. Many other pieces are needed as well.
On the llvm-project side, there was a lot of debate on how the API should look like. In the week of 2022-09-09 we (Cole Kissane, David Blaikie, I) reached an agreement that the free function style compression API was acceptable. I have pushed some changes and llvm-objcopy --compress-debug-sections=zstd, clang -gz=std, ld.lld --compress-debug-sections=zstd are available now. Note that I chose to implement llvm-objcopy support before others so that I could test other components with llvm-objcopy.
Hex dump of section '.debug_info': NOTE: This section has relocations against it, but these have NOT been applied to this dump. 0x00000000 02000000 00000000 1a180000 00000000 ................ 0x00000010 01000000 00000000 28b52ffd 601a17ed ........(./.`... ...
ELFCOMPRESS_ZSTD (2) can be identified by the first 4 bytes. In a little-endian object file, it displays as 02000000.
If llvm-objcopy is built with zstd support, use --decompress-debug-sections to decompress an object file:
Hex dump of section '.debug_info': NOTE: This section has relocations against it, but these have NOT been applied to this dump. 0x00000000 16180000 05000108 00000000 01002100 ..............!. 0x00000010 01000000 00000000 00020000 00000000 ................ ...
On the llvm-project side we reached full feature readiness in 2022-10.
It would be nice that someone picks up the work items on the GNU side so that many Linux distributions can start investigating the adoption of zstd compressed debug sections.
GNU toolchain support
The main changes were for the binutils-gdb repository. This work turned out to be much more challenging than my work for llvm-project.
The entry points of zstd compression features were in binutils, gas, and ld. binutils and ld use bfd, so we needed to update bfd.
I created config/zstd.m4 by following config/zlib.m4. AC_ZSTD in config/zstd.m4 defines ZLIB_CFLAGS and ZLIB_LDLIBS. After plumbing it into bfd/configure.ac and bfd/Makefile.am, I needed to adding AC_ZSTD to every top-level project which uses bfd as bfd is linked as an archive and there is no good transitive dependency support.
Here was the change for bfd/Makefile.am. The pattern needed to be repeated in many other directories.
Remember to update auto-generated files with the appropriate versions of autoconf and automake:
1
PATH=~/projects/automake-1.15.1/bin:$PATH ~/projects/autoconf-2.69/bin/autoreconf -vf bfd binutils gas ld libctf sim
1
make -C bfd headers
Some bfd/ file changes require updating bfd/bfd-in2.h with make -C $build/bfd headers.
1 2 3 4 5 6 7 8
Appendix
(Conducted the experiment in 2022-10.) I have a -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=all build of trunk clang. The 3 largest DWARF v5 debug sections are .debug_info, .debug_str, and .debug_line.
ninja -t commands bin/clang dumps the compiler driver command which links the executable. Invoke the command with -fuse-ld=lld -Wl,--prproduce=/tmp/clang-debug.tar to get a tarball. Use llvm-objcopy --dump-section to extract a section.
1 2 3 4 5 6 7 8
cd /tmp tar xf clang-debug.tar cd clang-debug ld.lld @response.txt
I have tried brotli, bzip2, gzip, lz4, lzo, pigz, xz, zstd, and manually verified that zstd is the best considering compression speed, decompression speed, and compression ratio. Figuring out API for all these libraries will be inconvenient. So I take a shortcut: install these compression utilities with the package manager and hope that they use similar compiler driver options and the comparison is relative fair.
When compressing debug sections, zstd and brotli are significantly better than the other choices. zstd slightly outperforms brotli in compression speed and compression ratio while being much fast at decompression.
xz -3 has a great compression ratio (higher levels are too slow). zstd and brotli with higher levels are extremely slow and can hardly achieve the xz compression ratio, but their decompression speed may compensate for that.
zlib (used by pigz) and bzip2 look pretty bad.
For zstd, the built-in parallel compression support is a plus.