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

via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 10 22:56:11 PDT 2026


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

>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/5] [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/5] [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"

>From 486816e67a849e6ef7c11afe8565646734c5e052 Mon Sep 17 00:00:00 2001
From: jpwang <jpwang at smail.nju.edu.cn>
Date: Mon, 8 Jun 2026 23:26:20 +0800
Subject: [PATCH 3/5] [llvm-objcopy] Fix typo in test

---
 .../llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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
index 8433a036e8e19..5db39786b5fb9 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test
@@ -1,4 +1,4 @@
-# UNSUPPORTED: zlib
+# UNSUPPORTED: zstd
 # RUN: yaml2obj %s -o %t
 # RUN: not llvm-objcopy --compress-sections '.debug_info=none' %t /dev/null 2>&1 | FileCheck %s -DFILE=%t
 

>From 4244975bd1a505cfa45c58f76a1b3b3e4749750d Mon Sep 17 00:00:00 2001
From: jpwang <jpwang at smail.nju.edu.cn>
Date: Tue, 9 Jun 2026 17:49:06 +0800
Subject: [PATCH 4/5] [llvm-objcopy] Include a new line at EOF for tests

---
 .../llvm-objcopy/ELF/compress-sections-zlib-unsupported.test    | 2 +-
 .../llvm-objcopy/ELF/compress-sections-zstd-unsupported.test    | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

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
index 7d00f5ca020ab..4064f43dacf48 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zlib-unsupported.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zlib-unsupported.test
@@ -2,4 +2,4 @@
 # 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
+# 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
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
index 28b70b2bc5905..d50a574fd135c 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test
@@ -2,4 +2,4 @@
 # 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
+# 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

>From 6542fdb716073ca85524b25089225f42eb770f54 Mon Sep 17 00:00:00 2001
From: jpwang <jpwang at smail.nju.edu.cn>
Date: Thu, 11 Jun 2026 13:55:44 +0800
Subject: [PATCH 5/5] [llvm-objcopy] Address review comments

---
 .../ELF/compress-debug-sections-zstd-err.test |  5 ----
 .../compress-sections-zlib-unsupported.test   |  6 +++--
 .../compress-sections-zstd-unsupported.test   |  6 +++--
 ...press-debug-sections-unsupported-zlib.test | 18 --------------
 .../decompress-sections-unsupported-zlib.test | 24 +++++++++++++++++--
 .../decompress-sections-unsupported-zstd.test | 24 +++++++++++++++++--
 llvm/tools/llvm-objcopy/ObjcopyOptions.cpp    |  3 ---
 7 files changed, 52 insertions(+), 34 deletions(-)
 delete mode 100644 llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zstd-err.test
 delete mode 100644 llvm/test/tools/llvm-objcopy/ELF/decompress-debug-sections-unsupported-zlib.test

diff --git a/llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zstd-err.test b/llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zstd-err.test
deleted file mode 100644
index 94eebd85deda4..0000000000000
--- a/llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zstd-err.test
+++ /dev/null
@@ -1,5 +0,0 @@
-# UNSUPPORTED: zstd
-# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t
-# RUN: not llvm-objcopy --compress-debug-sections=zstd %t /dev/null 2>&1 | FileCheck %s
-
-# CHECK: error: LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at build time
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
index 4064f43dacf48..d6aecbc39ce7f 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zlib-unsupported.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zlib-unsupported.test
@@ -1,5 +1,7 @@
 # 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
+# RUN: not llvm-objcopy --compress-sections '.debug_foo=zlib' %t /dev/null 2>&1 | FileCheck %s -DFILE=%t --check-prefix=CHECK1
+# RUN: not llvm-objcopy --compress-debug-sections=zlib %t /dev/null 2>&1 | FileCheck %s -DFILE=%t --check-prefix=CHECK2
 
-# 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
+# CHECK1: error: '[[FILE]]': failed to compress section '.debug_foo': LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at build time
+# CHECK2: error: '[[FILE]]': failed to compress section '.debug_foo': LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at build time
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
index d50a574fd135c..3c1071e27632c 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test
@@ -1,5 +1,7 @@
 # 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
+# RUN: not llvm-objcopy --compress-sections '.debug_foo=zstd' %t /dev/null 2>&1 | FileCheck %s -DFILE=%t --check-prefix=CHECK1
+# RUN: not llvm-objcopy --compress-debug-sections=zstd %t /dev/null 2>&1 | FileCheck %s -DFILE=%t --check-prefix=CHECK2
 
-# 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
+# CHECK1: error: '[[FILE]]': failed to compress section '.debug_foo': LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at build time
+# CHECK2: error: '[[FILE]]': failed to compress section '.debug_foo': LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at build time
diff --git a/llvm/test/tools/llvm-objcopy/ELF/decompress-debug-sections-unsupported-zlib.test b/llvm/test/tools/llvm-objcopy/ELF/decompress-debug-sections-unsupported-zlib.test
deleted file mode 100644
index 34ca53c270fc3..0000000000000
--- a/llvm/test/tools/llvm-objcopy/ELF/decompress-debug-sections-unsupported-zlib.test
+++ /dev/null
@@ -1,18 +0,0 @@
-# UNSUPPORTED: zlib
-# RUN: yaml2obj %s -o %t
-# RUN: not llvm-objcopy --decompress-debug-sections %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-zlib.test b/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zlib.test
index f312a10fa0d3c..4351b5fcfee38 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zlib.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zlib.test
@@ -1,8 +1,15 @@
+## Show that a proper error message is printed when decompressing sections
+## using unsupported format (zlib). Since both --decompress-debug-sections
+## and --compress-sections with format set to 'none' performs decompression,
+## here we test them both.
+
 # 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
+# RUN: not llvm-objcopy --decompress-debug-sections %t /dev/null 2>&1 | FileCheck %s -DFILE=%t --check-prefix=CHECK1
+# RUN: not llvm-objcopy --compress-sections '.debug_info=none' %t /dev/null 2>&1 | FileCheck %s -DFILE=%t --check-prefix=CHECK2
 
-# 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
+# CHECK1: error: '[[FILE]]': failed to decompress section '.debug_info': LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at build time
+# CHECK2: 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:
@@ -16,3 +23,16 @@ Sections:
     Flags:        [ SHF_COMPRESSED ]
     AddressAlign: 8
     Content:      "010000000000000001000000000000000100000000000000789c63040000020002"
+
+## Original yaml
+# --- !ELF
+# FileHeader:
+#   Class:   ELFCLASS64
+#   Data:    ELFDATA2LSB
+#   Type:    ET_REL
+#   Machine: EM_X86_64
+# Sections:
+#   - Type:         SHT_PROGBITS
+#     Name:         .debug_info
+#     AddressAlign: 0x1
+#     Content:      '01'
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
index 5db39786b5fb9..8791c24a14832 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test
@@ -1,8 +1,15 @@
+## Show that a proper error message is printed when decompressing sections
+## using unsupported format (zstd). Since both --decompress-debug-sections
+## and --compress-sections with format set to 'none' performs decompression,
+## here we test them both.
+
 # UNSUPPORTED: zstd
 # RUN: yaml2obj %s -o %t
-# RUN: not llvm-objcopy --compress-sections '.debug_info=none' %t /dev/null 2>&1 | FileCheck %s -DFILE=%t
+# RUN: not llvm-objcopy --decompress-debug-sections %t /dev/null 2>&1 | FileCheck %s -DFILE=%t --check-prefix=CHECK1
+# RUN: not llvm-objcopy --compress-sections '.debug_info=none' %t /dev/null 2>&1 | FileCheck %s -DFILE=%t --check-prefix=CHECK2
 
-# 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
+# CHECK1: error: '[[FILE]]': failed to decompress section '.debug_info': LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at build time
+# CHECK2: 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:
@@ -16,3 +23,16 @@ Sections:
     Flags:        [ SHF_COMPRESSED ]
     AddressAlign: 8
     Content:      "02000000000000000100000000000000010000000000000028b52ffd200109000001"
+
+## Original yaml
+# --- !ELF
+# FileHeader:
+#   Class:   ELFCLASS64
+#   Data:    ELFDATA2LSB
+#   Type:    ET_REL
+#   Machine: EM_X86_64
+# Sections:
+#   - Type:         SHT_PROGBITS
+#     Name:         .debug_info
+#     AddressAlign: 0x1
+#     Content:      '01'
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index c79dfca0a12be..a7c178ce919f5 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -920,9 +920,6 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> ArgsArr,
           "invalid or unsupported --compress-debug-sections format: %s",
           A->getValue());
     }
-    if (const char *Reason = compression::getReasonIfUnsupported(
-            compression::formatFor(Config.CompressionType)))
-      return createStringError(errc::invalid_argument, Reason);
   }
 
   for (const auto *A : InputArgs.filtered(OBJCOPY_compress_sections)) {



More information about the llvm-commits mailing list