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

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 22 02:52:02 PDT 2026


Author: jpwang
Date: 2026-06-22T09:51:58Z
New Revision: 283514b070fd2ae647f56cf046ea74be15a887b1

URL: https://github.com/llvm/llvm-project/commit/283514b070fd2ae647f56cf046ea74be15a887b1
DIFF: https://github.com/llvm/llvm-project/commit/283514b070fd2ae647f56cf046ea74be15a887b1.diff

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

Fixes #197877.

`llvm-objcopy --compress-sections` now reports unavailable compression
support while parsing the option, matching the behavior of
`--compress-debug-sections`. This avoids reaching compression code with
an unavailable format.

Decompression (either via `--decompress-debug-sections` or
`--compress-sections` with a `none` format) is still checked when the
relevant compressed section is processed, because the required
compression format is determined from the section header rather than
from the command-line option.

The `compress-sections-within-segment` test now requires zlib because
`--compress-sections .text=zlib` is diagnosed during option parsing when
zlib support is unavailable, before the test can reach the intended
"section within a segment" diagnostic.

Added: 
    llvm/test/tools/llvm-objcopy/ELF/compress-sections-zlib-unsupported.test
    llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test
    llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zlib.test
    llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test

Modified: 
    llvm/docs/ReleaseNotes.md
    llvm/test/tools/llvm-objcopy/ELF/compress-sections-within-segment.s
    llvm/tools/llvm-objcopy/ObjcopyOptions.cpp

Removed: 
    llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zstd-err.test
    llvm/test/tools/llvm-objcopy/ELF/decompress-debug-sections-unsupported-zlib.test


################################################################################
diff  --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index df4ced91e8f5e..cf0e8aeb97da1 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -345,6 +345,9 @@ Makes programs 10x faster by doing Special New Thing.
 * `llvm-profgen` now supports ETM trace decoding using the OpenCSD library for Cortex-M targets. OpenCSD version 1.5.4 or higher is required.
 
 * `llvm-objcopy` no longer corrupts the symbol table when `--update-section` is called for ELF files.
+* `llvm-objcopy` now reports an error when `--compress-sections` requests unavailable zlib or zstd support.
+  The diagnostic is emitted while parsing the option, matching `--compress-debug-sections`.
+  Such commands may now fail even if the input file contains no sections that would be compressed.
 * `FileCheck` option `-check-prefix` now accepts a comma-separated list of
   prefixes, making it an alias of the existing `-check-prefixes` option.
 * Add `-mtune` option to `llc`.

diff  --git a/llvm/test/tools/llvm-objcopy/ELF/compress-sections-within-segment.s b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-within-segment.s
index 064ffcadc12e3..c6635d93b0465 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/compress-sections-within-segment.s
+++ b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-within-segment.s
@@ -1,5 +1,6 @@
 ## Disallow (de)compression for sections within a segment as they are
 ## effectively immutable.
+# REQUIRES: zlib
 # RUN: rm -rf %t && mkdir %t && cd %t
 # RUN: yaml2obj %s -o a
 # RUN: not llvm-objcopy a /dev/null --compress-sections .text=zlib 2>&1 | FileCheck %s --implicit-check-not=error:

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..23170d8582a93
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zlib-unsupported.test
@@ -0,0 +1,6 @@
+# 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
+# RUN: not llvm-objcopy --compress-debug-sections=zlib %t /dev/null 2>&1 | FileCheck %s
+
+# CHECK: error: 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-debug-sections-zstd-err.test b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test
similarity index 73%
rename from llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zstd-err.test
rename to llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test
index 94eebd85deda4..c055714d3348d 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zstd-err.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/compress-sections-zstd-unsupported.test
@@ -1,5 +1,6 @@
 # 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
 # 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/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
new file mode 100644
index 0000000000000..1549ad84be368
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zlib.test
@@ -0,0 +1,40 @@
+## Show that a proper error message is printed when decompressing sections
+## using an unsupported format (zlib). Since both --decompress-debug-sections
+## and --compress-sections with format set to 'none' perform decompression,
+## here we test them both.
+
+# UNSUPPORTED: zlib
+# RUN: yaml2obj %s -o %t
+# RUN: not llvm-objcopy --decompress-debug-sections %t /dev/null 2>&1 | FileCheck %s -DFILE=%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 ]
+    Content:      "010000000000000001000000000000000100000000000000789c63040000020002"
+
+## The compressed ELF above was generated from the following uncompressed input:
+## --- !ELF
+## FileHeader:
+##   Class:   ELFCLASS64
+##   Data:    ELFDATA2LSB
+##   Type:    ET_REL
+##   Machine: EM_X86_64
+## Sections:
+##   - Type:         SHT_PROGBITS
+##     Name:         .debug_info
+##     Content:      '01'
+##
+## Regenerate via:
+##   yaml2obj uncompressed.yaml -o uncompressed.o
+##   llvm-objcopy --compress-sections .debug_info=zlib uncompressed.o compressed.o
+##   obj2yaml compressed.o

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..58884be3cccb4
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/decompress-sections-unsupported-zstd.test
@@ -0,0 +1,40 @@
+## Show that a proper error message is printed when decompressing sections
+## using an unsupported format (zstd). Since both --decompress-debug-sections
+## and --compress-sections with format set to 'none' perform decompression,
+## here we test them both.
+
+# UNSUPPORTED: zstd
+# RUN: yaml2obj %s -o %t
+# RUN: not llvm-objcopy --decompress-debug-sections %t /dev/null 2>&1 | FileCheck %s -DFILE=%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 ]
+    Content:      "02000000000000000100000000000000010000000000000028b52ffd200109000001"
+
+## The compressed ELF above was generated from the following uncompressed input:
+## --- !ELF
+## FileHeader:
+##   Class:   ELFCLASS64
+##   Data:    ELFDATA2LSB
+##   Type:    ET_REL
+##   Machine: EM_X86_64
+## Sections:
+##   - Type:         SHT_PROGBITS
+##     Name:         .debug_info
+##     Content:      '01'
+##
+## Regenerate via:
+##   yaml2obj uncompressed.yaml -o uncompressed.o
+##   llvm-objcopy --compress-sections .debug_info=zstd uncompressed.o compressed.o
+##   obj2yaml compressed.o

diff  --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index c79dfca0a12be..59e750f5e88df 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -945,6 +945,11 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> ArgsArr,
           "invalid or unsupported --compress-sections format: %s",
           A->getValue());
     }
+    if (Type != DebugCompressionType::None) {
+      if (const char *Reason =
+              compression::getReasonIfUnsupported(compression::formatFor(Type)))
+        return createStringError(errc::invalid_argument, Reason);
+    }
 
     auto &P = Config.compressSections.emplace_back();
     P.second = Type;


        


More information about the llvm-commits mailing list