[llvm] [llvm-objcopy] Report unsupported formats before compression (PR #202357)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 8 08:13:11 PDT 2026
https://github.com/jjppp created https://github.com/llvm/llvm-project/pull/202357
Fixes #197877
This PR fixes the crash due to `llvm_unreachable` by calling `compression::getReasonIfUnsupported` before compression. Since actual compression is performed in the constructor of `CompressedSection`, a check is placed before the construction.
Decompression is not handled as it is performed by `ELFSectionWriter` and is already guarded with a `getReasonIfUnsupported`.
Four tests are added to ensure compression/decompression works in the absence of zlib/zstd.
I did not use any AI tools when fixing this issue.
>From 711af477ab36f0b8be6f87a03c26c3bcd53345d9 Mon Sep 17 00:00:00 2001
From: jpwang <jpwang at smail.nju.edu.cn>
Date: Mon, 8 Jun 2026 21:06:29 +0800
Subject: [PATCH 1/2] [llvm-objcopy] Report unsupported formats before
compression
---
llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
index edb6ae0a5b108..e2a82ed2b0460 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
@@ -242,6 +242,11 @@ Error Object::compressOrDecompressSections(const CommonConfig &Config) {
ToReplace.emplace_back(
&Sec, [=] { return &addSection<DecompressedSection>(*CS); });
} else if (*CType != DebugCompressionType::None) {
+ if (auto *Reason = compression::getReasonIfUnsupported(
+ compression::formatFor(*CType)))
+ return createStringError(errc::invalid_argument,
+ "failed to compress section '" + Sec.Name +
+ "': " + Reason);
ToReplace.emplace_back(&Sec, [=, S = &Sec] {
return &addSection<CompressedSection>(
CompressedSection(*S, *CType, Is64Bits));
>From f93df76efac461074e655eaf09dd25888a5df5b1 Mon Sep 17 00:00:00 2001
From: jpwang <jpwang at smail.nju.edu.cn>
Date: Mon, 8 Jun 2026 22:00:03 +0800
Subject: [PATCH 2/2] [llvm-objcopy] Add tests
---
.../compress-sections-zlib-unsupported.test | 5 +++++
.../compress-sections-zstd-unsupported.test | 5 +++++
.../decompress-sections-unsupported-zlib.test | 18 ++++++++++++++++++
.../decompress-sections-unsupported-zstd.test | 18 ++++++++++++++++++
4 files changed, 46 insertions(+)
create mode 100644 llvm/test/tools/llvm-objcopy/ELF/compress-sections-zlib-unsupported.test
create mode 100644 llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test
create mode 100644 llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zlib.test
create mode 100644 llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test
diff --git a/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zlib-unsupported.test b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zlib-unsupported.test
new file mode 100644
index 0000000000000..7d00f5ca020ab
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zlib-unsupported.test
@@ -0,0 +1,5 @@
+# UNSUPPORTED: zlib
+# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t
+# RUN: not llvm-objcopy --compress-sections '.debug_foo=zlib' %t /dev/null 2>&1 | FileCheck %s -DFILE=%t
+
+# CHECK: error: '[[FILE]]': failed to compress section '.debug_foo': LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at build time
\ No newline at end of file
diff --git a/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test
new file mode 100644
index 0000000000000..28b70b2bc5905
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test
@@ -0,0 +1,5 @@
+# UNSUPPORTED: zstd
+# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t
+# RUN: not llvm-objcopy --compress-sections '.debug_foo=zstd' %t /dev/null 2>&1 | FileCheck %s -DFILE=%t
+
+# CHECK: error: '[[FILE]]': failed to compress section '.debug_foo': LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at build time
\ No newline at end of file
diff --git a/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zlib.test b/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zlib.test
new file mode 100644
index 0000000000000..f312a10fa0d3c
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zlib.test
@@ -0,0 +1,18 @@
+# UNSUPPORTED: zlib
+# RUN: yaml2obj %s -o %t
+# RUN: not llvm-objcopy --compress-sections '.debug_info=none' %t /dev/null 2>&1 | FileCheck %s -DFILE=%t
+
+# CHECK: error: '[[FILE]]': failed to decompress section '.debug_info': LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at build time
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Type: SHT_PROGBITS
+ Name: .debug_info
+ Flags: [ SHF_COMPRESSED ]
+ AddressAlign: 8
+ Content: "010000000000000001000000000000000100000000000000789c63040000020002"
diff --git a/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test b/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test
new file mode 100644
index 0000000000000..8433a036e8e19
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test
@@ -0,0 +1,18 @@
+# UNSUPPORTED: zlib
+# RUN: yaml2obj %s -o %t
+# RUN: not llvm-objcopy --compress-sections '.debug_info=none' %t /dev/null 2>&1 | FileCheck %s -DFILE=%t
+
+# CHECK: error: '[[FILE]]': failed to decompress section '.debug_info': LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at build time
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Type: SHT_PROGBITS
+ Name: .debug_info
+ Flags: [ SHF_COMPRESSED ]
+ AddressAlign: 8
+ Content: "02000000000000000100000000000000010000000000000028b52ffd200109000001"
More information about the llvm-commits
mailing list