[llvm] [llvm-objcopy] Report unsupported formats before compression (PR #202357)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 8 08:14:21 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-binary-utilities

Author: jpwang (jjppp)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/202357.diff


5 Files Affected:

- (modified) llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp (+5) 
- (added) llvm/test/tools/llvm-objcopy/ELF/compress-sections-zlib-unsupported.test (+5) 
- (added) llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test (+5) 
- (added) llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zlib.test (+18) 
- (added) llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test (+18) 


``````````diff
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));
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"

``````````

</details>


https://github.com/llvm/llvm-project/pull/202357


More information about the llvm-commits mailing list