Skip to content

Compress DWARF debug sections in object files - #109306

Merged
murphy-4o merged 17 commits into
ClickHouse:masterfrom
murphy-4o:compress-debug-sections
Jul 8, 2026
Merged

murphy-4o merged 17 commits into
ClickHouse:masterfrom
murphy-4o:compress-debug-sections

Conversation

@murphy-4o

@murphy-4o murphy-4o commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

Adds COMPRESS_DEBUG_SECTIONS — compresses DWARF debug sections in object files with -gz=zlib to shrink the Debug build directory (debug info dominates it). It is compile-only (add_compile_options, never on the link line): the linker decompresses on input and emits uncompressed binaries, so runtime stack-trace symbolization is unaffected — only the .o/.a files shrink. Auto-enabled where the compiler supports -gz; -DCOMPRESS_DEBUG_SECTIONS=OFF forces it off.

Support depends on the compiler's zlib linkage (LLVM_ENABLE_ZLIB), not its version, so it is probed at configure time. Our own toolchain is built LLVM_ENABLE_ZLIB=OFF today (off there); #109596 enables it so -gz is on for CI and default builds too.

Measured (aarch64, clean Debug build):

object files (.o) build dir clickhouse binary
off 13.1 GiB 47 GiB 6.7 GiB
-gz 6.5 GiB (−50%) 35 GiB (−26%) 6.7 GiB (unchanged)

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Added a COMPRESS_DEBUG_SECTIONS CMake option that compresses DWARF debug sections in object files (-gz), auto-enabled where the compiler supports it, to shrink Debug build directories; final binaries are left uncompressed, so runtime stack-trace symbolization is unaffected.

Version info

  • Merged into: 26.7.1.640 (included in 26.7 and later)

murphy-4o and others added 2 commits July 3, 2026 21:50
A Debug build's on-disk size is dominated by DWARF debug info, both in the
object files under the build directory and in the final binaries. Enable
`-gz=zlib` at compile time and `--compress-debug-sections=zlib` at link time by
default for Debug builds on Linux to cut that size substantially.

Compressing debug sections sets the `SHF_COMPRESSED` flag, which ClickHouse's
built-in symbolizer previously could not read: `Dwarf::getSection` returned an
empty view for any compressed section. That would have silently degraded stack
traces (uncaught exceptions, the fatal signal handler, `system.stack_trace`,
`addressToLine`) to function names only, losing source file, line, and inlined
frames - the main value of a Debug build.

Teach `Dwarf::getSection` to decompress `SHF_COMPRESSED` sections (both zlib and
zstd, which are already linked into `clickhouse_common_io`) into buffers owned by
the `Dwarf` instance, so symbolization keeps working transparently. The backing
storage is declared before the section views so it is constructed first, and
uses `std::list` for stable addresses. Decompression failures degrade gracefully
to no line info rather than throwing, since the symbolizer runs while handling
other exceptions and fatal signals.

The behavior is controlled by the new `COMPRESS_DEBUG_SECTIONS` CMake option,
which defaults to ON only for Debug builds on Linux.

Co-Authored-By: Claude <[email protected]>
Empty commit documenting the on-disk size reduction of enabling
`COMPRESS_DEBUG_SECTIONS` (`-gz=zlib` at compile time plus
`--compress-debug-sections=zlib` at link time). These numbers are for the PR
description.

Measured on an aarch64 `Debug` build (`-Og -g`). Sizes were obtained by applying
`llvm-objcopy --compress-debug-sections=zlib` to artifacts that were already
built without `-gz`, which reproduces the exact `SHF_COMPRESSED` zlib sections
the compiler and linker flags emit.

Final `clickhouse` binary:
  before: 5.84 GiB (6,270,115,248 bytes)
  after:  2.41 GiB (2,589,723,680 bytes)
  saved:  3.43 GiB  (-58.7%)
  (debug info alone accounts for ~5.2 GiB of the uncompressed binary)

Object files in the build directory (sampled 392 of 39,279 `.o`):
  reduction:    -44.9%  (compressed/original ratio 0.551)
  extrapolated: 16.1 GiB -> ~8.9 GiB, ~7.2 GiB saved

The final binary compresses more than the object files because it is almost
entirely debug info (~89%), whereas object files also contain code, symbol
tables, and relocations that `--compress-debug-sections` does not touch.

Co-Authored-By: Claude <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b21eac4]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug) FAIL
Logical error: 'Inconsistent AST formatting: the original AST: (STID: 1941-26fa) FAIL cidb, issue

AI Review

Summary

This PR changes COMPRESS_DEBUG_SECTIONS from an opt-in knob to an auto-detected default and keeps the flag compile-only so final binaries stay uncompressed. Restricting the change to object files avoids the earlier symbolizer regressions, but the new auto-detection is still weaker than the contract it controls: check_compiler_flag only proves that -gz=zlib was accepted on a trivial probe source, not that the current toolchain can actually compress the real debug-producing inputs in this tree. I would not merge it until that probe is made effect-based again.

Findings

❌ Blockers

  • [CMakeLists.txt:141-145] COMPILER_SUPPORTS_GZ_ZLIB is derived from check_compiler_flag, but CMake's helper only compiles a trivial int main source and defines success as "no diagnostic". That is not enough for -gz=zlib: a no-zlib or otherwise ineffective toolchain can still pass this probe when the tiny test does not trigger real debug-section compression, then fail later when the global add_compile_options reaches large DWARF-producing translation units or the ASM .S sources. Suggested fix: return to an effect-based probe that emits debug info and verifies the output really gets compressed, or use a custom probe source that exercises the same C and assembler paths the build will use.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: replace the current check_compiler_flag auto-detection with a probe that proves -gz actually works on the real debug-producing compile paths before defaulting COMPRESS_DEBUG_SECTIONS to ON.

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label Jul 3, 2026
murphy-4o and others added 3 commits July 3, 2026 22:56
…le check

The `check-style` CI rule flags `catch (...)` blocks that silently swallow
exceptions unless they handle/rethrow/log or carry a comment containing the word
'Ok'. The swallow here is intentional - the symbolizer must not throw while
handling other exceptions or fatal signals - so document it accordingly.

Co-Authored-By: Claude <[email protected]>
Some toolchains ship a clang/LLVM built without zlib, where `-gz=zlib` emits
`-Wdebug-compression-unavailable`, which is fatal under `-Werror`. This broke the
`arm_tidy` CI build (its clang lacks zlib), failing on `.S` files in the `zstd`
and `openssl` contribs with:

    clang: error: cannot compress debug sections (zlib not enabled)
           [-Werror,-Wdebug-compression-unavailable]

Probe the compiler at configure time (compile a trivial TU with `-g -gz=zlib
-Werror`, compile-only via `CMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY` so an
unrelated link issue cannot mask the result) and only enable compression when
the probe succeeds; otherwise fall back to uncompressed debug info with a status
message. The linker's zlib comes from the same LLVM build as the compiler's, so
testing the compiler is sufficient.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109306&sha=8ad1fc774bf92958763eb0ef344631460ffbec36&name_0=PR&name_1=Build%20%28arm_tidy%29

Co-Authored-By: Claude <[email protected]>
…den probe

ClickHouse's own LLVM toolchain is built with LLVM_ENABLE_ZLIB=OFF and
LLVM_ENABLE_ZSTD=OFF (ci/jobs/build_toolchain.py), so its clang and lld reject
`-gz` with `error: cannot compress debug sections (zlib not enabled)`. That
broke the `arm_tidy` build. `-gz` therefore cannot be enabled by default for
ClickHouse-toolchain builds.

The previous attempt to auto-detect zlib support used `check_cxx_source_compiles`,
but configure-time compile probes are forbidden in ClickHouse
(`cmake/block_build_time_checks.cmake` fatally blocks `try_compile`); that made
the `arm_tidy` CMake configuration itself fail.

Resolve both by making COMPRESS_DEBUG_SECTIONS opt-in (default OFF) and removing
the probe: with the default OFF, no build adds `-gz`, so CI is unaffected;
developers building with a zlib-enabled compiler and linker (for example a
system clang) can turn it on to get the size reduction. The symbolizer change in
`src/Common/Dwarf.cpp` is kept so ClickHouse can read `SHF_COMPRESSED` debug
sections whenever they are present.

Enabling compression by default would require building the ClickHouse toolchain
with zstd (or zlib) support first; that is left as follow-up.

Co-Authored-By: Claude <[email protected]>
@murphy-4o murphy-4o changed the title Compress DWARF debug sections in Debug builds by default Add opt-in compression of DWARF debug sections (-gz) and read them in the symbolizer Jul 3, 2026
@murphy-4o murphy-4o changed the title Add opt-in compression of DWARF debug sections (-gz) and read them in the symbolizer Compress DWARF debug sections in Debug builds Jul 3, 2026
The `arm_tidy` build's clang-tidy flagged the local `ElfCompressionHeader header;`
(immediately overwritten via `memcpy`) with
`cppcoreguidelines-pro-type-member-init` / `hicpp-member-init` (uninitialized
record type), which is treated as an error. Value-initialize it (`{}`); the cost
is negligible and it is overwritten right after.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109306&sha=59812b8f99aaa68dea757f49f647fb7c7e6f9f3d&name_0=PR&name_1=Build%20%28arm_tidy%29

Co-Authored-By: Claude <[email protected]>
Comment thread src/Common/Dwarf.cpp Resolved
Comment thread src/Common/Dwarf.cpp Resolved
… failure

Building a COMPRESS_DEBUG_SECTIONS=ON clickhouse and running addressToLine over
system.stack_trace resolved 0 source lines: the binary's .debug_info is ~2.16 GiB
uncompressed, and contrib zlib's single-shot uncompress returns Z_DATA_ERROR for
outputs larger than 2 GiB. Since .debug_info is a required section, the Dwarf was
disabled and no frame resolved to file:line - so the symbolizer never actually
read compressed sections of the main binary.

Drive inflate in bounded (1 GiB) chunks instead of one-shot uncompress so sections
of any size decompress. After the fix the compressed build resolves the same lines
as an uncompressed build (222 vs 0 across live stack traces).

Also fail closed: if any present section fails to decompress, disable the whole
Dwarf in the constructor instead of leaving an empty view for a section the DWARF
still references (later readers would dereference it and throw while handling
another exception or a fatal signal). Addresses review comment B2.

Co-Authored-By: Claude <[email protected]>
Comment thread src/Common/Dwarf.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.60% 85.50% -0.10%
Functions 92.70% 92.70% +0.00%
Branches 77.80% 77.70% -0.10%

Changed lines: Changed C/C++ lines covered: 41/51 (80.39%) · Uncovered code

Full report · Diff report

murphy-4o and others added 2 commits July 6, 2026 11:44
Follow-up to the fail-closed handling: if a later .debug_* section failed to
decompress after an earlier one already inflated, the constructor disabled the
Dwarf but kept the already-inflated buffers (e.g. a ~2.16 GiB .debug_info) owned
by the cached Dwarf, and the initializer list kept decompressing the rest. In a
long-lived cache (addressToLine) one failed symbolization could pin gigabytes
while returning no line info.

decompressSection now returns early once decompression_failed_ is set (no further
inflation), and the constructor clears decompressed_sections_ when it disables the
Dwarf - safe because findAddress bails on the null elf_/macho_ before touching any
section view.

Co-Authored-By: Claude <[email protected]>
…binaries

Compressing the sections the in-process symbolizer reads forced it to inflate
multi-GiB .debug_info into heap on every symbolization (the runtime reads debug
info from the binary and from any external .debug file) - unacceptable on the
exception / fatal-signal path.

Instead compress only object files: pass -gz=zlib as a compile-only option
(add_compile_options, so it never reaches the link line) and let the linker
decompress on input and emit uncompressed binaries. The build directory shrinks
(objects dominate it) while binaries stay uncompressed, so the runtime symbolizer
reads them exactly as before - no decompression, no heap, and no change to
src/Common/Dwarf.* (reverted).

Still opt-in / default OFF: ClickHouse's toolchain clang lacks zlib and
configure-time probes are forbidden, so the flag cannot be auto-detected.

Co-Authored-By: Claude <[email protected]>
@murphy-4o murphy-4o changed the title Compress DWARF debug sections in Debug builds Compress DWARF debug sections in object files (opt-in -gz) Jul 6, 2026
Comment thread CMakeLists.txt Resolved
add_compile_options is language-agnostic, so -gz=zlib also reached ISA-L's
ASM_NASM sources on amd64, and nasm rejects the flag (the same reason ISA-L
already strips -fno-sanitize=undefined). Restrict it to C/CXX/ASM via a
COMPILE_LANGUAGE generator expression. Default OFF, so no build was affected;
this only bit an amd64 build that enabled the option with ENABLE_ISAL_LIBRARY=ON.
Comment thread CMakeLists.txt Resolved
@murphy-4o murphy-4o changed the title Compress DWARF debug sections in object files (opt-in -gz) Compress DWARF debug sections in object files Jul 6, 2026
-gz only compresses debug sections, so the option is inert without debug
info and off Linux. Mirror ENABLE_XRAY: emit a status message and reset the
variable to 0 on those configs, instead of leaving an inert ON in the cache.

Co-Authored-By: Claude <[email protected]>
@murphy-4o
murphy-4o marked this pull request as ready for review July 6, 2026 11:00
@murphy-4o
murphy-4o requested review from Algunenano and azat July 6, 2026 13:56
@azat azat self-assigned this Jul 6, 2026
Comment thread CMakeLists.txt Resolved
Comment thread CMakeLists.txt Resolved
Comment thread CMakeLists.txt Resolved
murphy-4o and others added 5 commits July 7, 2026 11:10
Whether clang accepts -gz=zlib depends on zlib linkage (LLVM_ENABLE_ZLIB), not
its version, so probe it (check_cxx_source_compiles) and use the result as the
option default - auto-enabling compression wherever the compiler supports it.
The probe is placed before block_build_time_checks.cmake, a deliberate,
reviewer-approved exception for this one cross-compilation-independent flag.

Drops the OS_LINUX/DISABLE_ALL_DEBUG_SYMBOLS force-off guard: -gz is a no-op
without debug info and the probe already covers platform support.

Co-Authored-By: Claude <[email protected]>
A clang built without zlib does not error on -gz=zlib - it warns
(-Wdebug-compression-unavailable) and proceeds. The probe compiled without
-Werror, so it returned Success on such a toolchain (e.g. the bundled
LLVM_ENABLE_ZLIB=OFF one), defaulting the option ON; the -Werror build
(arm_tidy) then promoted the warning to a fatal error. Add
-Werror=debug-compression-unavailable so a warning counts as unsupported.

Co-Authored-By: Claude <[email protected]>
A clang built without zlib still *accepts* -gz=zlib - it only warns
("cannot compress debug sections") and emits uncompressed output - so a
flag-acceptance check false-positives (CMake docs note check_compiler_flag
cannot tell whether a flag has any effect). Use check_cxx_source_compiles with
FAIL_REGEX to fail the probe when the compiler prints that warning, plus a
source with enough debug info that the compiler actually attempts compression
(a trivial main leaves .debug_* too small, so it never warns). No -Werror
needed. Runs before block_build_time_checks.cmake bans probes - azat-approved
for this one cross-compilation-independent capability check.

Co-Authored-By: Claude <[email protected]>
The warning-based probes false-positived on the bundled toolchain: a C++ TU's
debug sections stay below clang's compression threshold there, so -gz never
attempts compression and never warns - only contrib's large .S assembly files
do (and fail under -Werror, which contrib's C/C++ warning relaxation doesn't
cover for the ASM language). Verify the *effect* instead: a no-zlib clang emits
identical output with and without -gz, so comparing object sizes returns OFF
regardless of threshold / warning / -Werror; only a zlib clang yields a smaller
object. This guarantees no-zlib builds (e.g. arm_tidy) keep the option off.

Co-Authored-By: Claude <[email protected]>
Root cause of the arm_tidy failures: the CI image resolves clang++-21 (apt,
zlib-enabled) and clang-21 (bundled toolchain, no zlib) from different
installs. A CXX-only probe honestly reports "supported", but CMake hands C
and .S files to the no-zlib clang-21, whose "cannot compress" warning turns
fatal under -Werror for ASM (contrib's warning relaxation covers C/C++ only).

Probe by effect (object must shrink under -gz) for every compiler the flag
reaches - CMAKE_C_COMPILER (also assembles .S) and CMAKE_CXX_COMPILER - and
enable only if all of them compress. Log a STATUS line per compiler so CI
configure output shows the verdicts.

Verified both paths: zlib+zlib -> ON (16708 -gz rules); simulated mixed
(C wrapper stripping -gz, like a no-zlib clang) -> OFF, zero -gz rules.

Co-Authored-By: Claude <[email protected]>
Comment thread CMakeLists.txt Resolved
Comment thread CMakeLists.txt Resolved
…navailable

Replace the object-size comparison with the standard check_compiler_flag on
both the C and C++ compilers (per azat), promoting only the compression
warning via -Werror=debug-compression-unavailable. A no-zlib clang accepts
-gz and merely warns "cannot compress", so the targeted -Werror turns that
into a compile error -> check fails -> option off; a zlib clang passes both
-> on. Targeted (not blanket) -Werror so an unrelated warning can't
false-negative. Checks BOTH compilers because the CI image pairs a
zlib-enabled clang++ with a zlib-less clang (which also assembles .S).

Verified against the actual bundled toolchain clang (LLVM_ENABLE_ZLIB=OFF):
-Werror=debug-compression-unavailable + -gz=zlib exits 1; a zlib clang ->
both checks Success, 16708 -gz rules.

Co-Authored-By: Claude <[email protected]>
Comment thread CMakeLists.txt Resolved
@murphy-4o
murphy-4o added this pull request to the merge queue Jul 8, 2026
Merged via the queue into ClickHouse:master with commit 133b482 Jul 8, 2026
173 of 175 checks passed
@murphy-4o
murphy-4o deleted the compress-debug-sections branch July 8, 2026 01:55
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 8, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ctions

Compress DWARF debug sections in object files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-build Pull request with build/testing/packaging improvement pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants