Repository navigation
Compress DWARF debug sections in object files - #109306
Merged
Merged
Conversation
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]>
Contributor
|
Workflow [PR], commit [b21eac4] Summary: ❌
AI ReviewSummaryThis PR changes Findings❌ Blockers
Final Verdict
|
…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]>
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]>
… 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]>
Contributor
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 41/51 (80.39%) · Uncovered code |
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]>
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.
-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]>
azat
reviewed
Jul 6, 2026
azat
reviewed
Jul 6, 2026
azat
reviewed
Jul 6, 2026
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]>
azat
reviewed
Jul 7, 2026
…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]>
azat
approved these changes
Jul 7, 2026
Merged
via the queue into
ClickHouse:master
with commit Jul 8, 2026
133b482
173 of 175 checks passed
1 task
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
…ctions Compress DWARF debug sections in object files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
COMPRESS_DEBUG_SECTIONS— compresses DWARF debug sections in object files with-gz=zlibto 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/.afiles shrink. Auto-enabled where the compiler supports-gz;-DCOMPRESS_DEBUG_SECTIONS=OFFforces 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 builtLLVM_ENABLE_ZLIB=OFFtoday (off there); #109596 enables it so-gzis on for CI and default builds too.Measured (aarch64, clean Debug build):
.o)clickhousebinary-gzChangelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added a
COMPRESS_DEBUG_SECTIONSCMake 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
26.7.1.640(included in26.7and later)