[lld] dd74d31 - [ELF] Refactor ELFCOMPRESS_ZLIB handling and improve diagnostics

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 8 14:04:23 PDT 2022


Author: Fangrui Song
Date: 2022-07-08T14:04:19-07:00
New Revision: dd74d3117de0fd96383beeb4b4b913efcb9f4328

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

LOG: [ELF] Refactor ELFCOMPRESS_ZLIB handling and improve diagnostics

And add some tests.

Added: 
    lld/test/ELF/compressed-input-err-zlib.s
    lld/test/ELF/compressed-input-err.s

Modified: 
    lld/ELF/InputSection.cpp

Removed: 
    lld/test/ELF/compressed-debug-input-err.s


################################################################################
diff  --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index f34718e8ed750..4ed87d8bdf18d 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -72,12 +72,8 @@ InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
 
   // If SHF_COMPRESSED is set, parse the header. The legacy .zdebug format is no
   // longer supported.
-  if (flags & SHF_COMPRESSED) {
-    if (!compression::zlib::isAvailable())
-      error(toString(file) + ": contains a compressed section, " +
-            "but zlib is not available");
+  if (flags & SHF_COMPRESSED)
     invokeELFT(parseCompressedHeader);
-  }
 }
 
 // Drop SHF_GROUP bit unless we are producing a re-linkable object file.
@@ -212,8 +208,13 @@ template <typename ELFT> void InputSectionBase::parseCompressedHeader() {
   }
 
   auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(rawData.data());
-  if (hdr->ch_type != ELFCOMPRESS_ZLIB) {
-    error(toString(this) + ": unsupported compression type");
+  if (hdr->ch_type == ELFCOMPRESS_ZLIB) {
+    if (!compression::zlib::isAvailable())
+      error(toString(this) + " is compressed with ELFCOMPRESS_ZLIB, but lld is "
+                             "not built with zlib support");
+  } else {
+    error(toString(this) + ": unsupported compression type (" +
+          Twine(hdr->ch_type) + ")");
     return;
   }
 

diff  --git a/lld/test/ELF/compressed-debug-input-err.s b/lld/test/ELF/compressed-debug-input-err.s
deleted file mode 100644
index 0683a4b1e5ee0..0000000000000
--- a/lld/test/ELF/compressed-debug-input-err.s
+++ /dev/null
@@ -1,19 +0,0 @@
-# REQUIRES: zlib
-# RUN: yaml2obj %s -o %t.o
-# RUN: not ld.lld %t.o -o /dev/null -shared 2>&1 | FileCheck %s
-
-## Check we are able to report zlib uncompress errors.
-# CHECK: error: {{.*}}.o:(.debug_info): uncompress failed: zlib error: Z_DATA_ERROR
-
-!ELF
-FileHeader:
-  Class:   ELFCLASS64
-  Data:    ELFDATA2LSB
-  Type:    ET_REL
-  Machine: EM_X86_64
-Sections:
-  - Type:         SHT_PROGBITS
-    Name:         .debug_info
-    Flags:        [ SHF_COMPRESSED ]
-    AddressAlign: 0x04
-    Content:      "010000000000000004000000000000000100000000000000ffff"

diff  --git a/lld/test/ELF/compressed-input-err-zlib.s b/lld/test/ELF/compressed-input-err-zlib.s
new file mode 100644
index 0000000000000..6143fb3f4c141
--- /dev/null
+++ b/lld/test/ELF/compressed-input-err-zlib.s
@@ -0,0 +1,18 @@
+# UNSUPPORTED: zlib
+# RUN: yaml2obj %s -o %t.o
+# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: error: {{.*}}.o:(.debug_info) is compressed with ELFCOMPRESS_ZLIB, but lld is not built with zlib support
+
+--- !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:      "010000000000000000000000000000000100000000000000789c030000000001"

diff  --git a/lld/test/ELF/compressed-input-err.s b/lld/test/ELF/compressed-input-err.s
new file mode 100644
index 0000000000000..8c34bbe41b190
--- /dev/null
+++ b/lld/test/ELF/compressed-input-err.s
@@ -0,0 +1,53 @@
+# REQUIRES: zlib
+# RUN: yaml2obj --docnum=1 %s -o %t1.o
+# RUN: not ld.lld %t1.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=TOO-SHORT
+# TOO-SHORT: error: {{.*}}.o:(.debug_info): corrupted compressed section
+
+# RUN: yaml2obj --docnum=2 %s -o %t2.o
+# RUN: not ld.lld %t2.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=UNKNOWN
+# UNKNOWN: error: {{.*}}.o:(.debug_info): unsupported compression type (3)
+
+# RUN: yaml2obj --docnum=3 %s -o %t3.o
+# RUN: not ld.lld %t3.o -o /dev/null -shared 2>&1 | FileCheck %s
+
+## Check we are able to report zlib uncompress errors.
+# CHECK: error: {{.*}}.o:(.debug_info): uncompress failed: zlib error: Z_DATA_ERROR
+
+--- !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:      "0100000000000000040000000000000001000000000000"
+
+--- !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:      "030000000000000000000000000000000100000000000000789c030000000001"
+
+--- !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:      "010000000000000004000000000000000100000000000000ffff"


        


More information about the llvm-commits mailing list