[llvm-objcopy] Support --decompress-debug-sections when zlib is disabled
authorFangrui Song <i@maskray.me>
Wed, 12 Oct 2022 18:52:05 +0000 (11:52 -0700)
committerFangrui Song <i@maskray.me>
Wed, 12 Oct 2022 18:52:05 +0000 (11:52 -0700)
When zlib is disabled at build time, the diagnostic `LLVM was not compiled with
LLVM_ENABLE_ZLIB: cannot decompress` for --decompress-debug-sections may be
inaccurate: if zstd is enabled, we should still support zstd decompression.

It's not useful to test zlib and zstd. Just remove the diagnostic and add a new
one before `compression::decompress`.

This fixes compress-debug-sections-zstd.test

Reviewed By: mariusz-sikora-at-amd, jhenderson, phosek

Differential Revision: https://reviews.llvm.org/D135744

llvm/lib/ObjCopy/ELF/ELFObject.cpp
llvm/test/tools/llvm-objcopy/ELF/decompress-debug-sections-unsupported-zlib.test [new file with mode: 0644]
llvm/tools/llvm-objcopy/ObjcopyOptions.cpp

index abbced6..2ff94ba 100644 (file)
@@ -453,6 +453,11 @@ Error ELFSectionWriter<ELFT>::visit(const DecompressedSection &Sec) {
                                  Twine(Sec.ChType) + ") of section '" +
                                  Sec.Name + "' is unsupported");
   }
+  if (auto *Reason =
+          compression::getReasonIfUnsupported(compression::formatFor(Type)))
+    return createStringError(errc::invalid_argument,
+                             "failed to decompress section '" + Sec.Name +
+                                 "': " + Reason);
   if (Error E = compression::decompress(Type, Compressed, Decompressed,
                                         static_cast<size_t>(Sec.Size)))
     return createStringError(errc::invalid_argument,
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
new file mode 100644 (file)
index 0000000..34ca53c
--- /dev/null
@@ -0,0 +1,18 @@
+# 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"
index b2250ca..1f7321e 100644 (file)
@@ -993,11 +993,6 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
         "--decompress-debug-sections");
   }
 
-  if (Config.DecompressDebugSections && !compression::zlib::isAvailable())
-    return createStringError(
-        errc::invalid_argument,
-        "LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress");
-
   if (Config.ExtractPartition && Config.ExtractMainPartition)
     return createStringError(errc::invalid_argument,
                              "cannot specify --extract-partition together with "