[llvm] 7181c4e - [llvm-objcopy] --compress-debug-sections: fix uninitialized ch_reserved for Elf64_Chdr

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 24 22:19:09 PDT 2022


Author: Fangrui Song
Date: 2022-07-24T22:19:00-07:00
New Revision: 7181c4e10a215bde53d91456c3f22d74fd71fac1

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

LOG: [llvm-objcopy] --compress-debug-sections: fix uninitialized ch_reserved for Elf64_Chdr

ch_reserved is uninitialized and the output is not deterministic. Fix it.
Rewrite and improve compress-debug-sections-zlib.test.

Added: 
    

Modified: 
    llvm/lib/ObjCopy/ELF/ELFObject.cpp
    llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zlib.test

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index 958ffaae7a58a..9f7408cad3e77 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -501,7 +501,7 @@ Error BinarySectionWriter::visit(const CompressedSection &Sec) {
 template <class ELFT>
 Error ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
   uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
-  Elf_Chdr_Impl<ELFT> Chdr;
+  Elf_Chdr_Impl<ELFT> Chdr = {};
   switch (Sec.CompressionType) {
   case DebugCompressionType::None:
     std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);

diff  --git a/llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zlib.test b/llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zlib.test
index 481071a9829ce..80844f794400f 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zlib.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zlib.test
@@ -1,60 +1,30 @@
 # REQUIRES: zlib
-
-# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t.o
-# RUN: llvm-objcopy --compress-debug-sections=zlib %t.o %t-compressed.o
-# RUN: llvm-objcopy --decompress-debug-sections %t-compressed.o %t-decompressed.o
-
-# RUN: llvm-objdump -s %t.o --section=.debug_foo | FileCheck %s
-# RUN: llvm-objdump -s %t-compressed.o | FileCheck %s --check-prefix=CHECK-COMPRESSED
-# RUN: llvm-readobj --relocations -S %t-compressed.o | FileCheck %s --check-prefix=CHECK-FLAGS
-# RUN: llvm-readobj --relocations -S %t-decompressed.o | FileCheck %s --check-prefix=CHECK-HEADER
-# RUN: llvm-readobj --relocations -S %t.o | FileCheck %s --check-prefix=CHECK-HEADER
-# RUN: llvm-objdump -s %t-decompressed.o --section=.debug_foo | FileCheck %s
-
-# CHECK: .debug_foo:
-# CHECK-NEXT: 0000 00000000 00000000
-
-# CHECK-HEADER:      Name: .debug_foo
-# CHECK-HEADER-NEXT: Type: SHT_PROGBITS
-# CHECK-HEADER-NEXT: Flags [
-# CHECK-HEADER-NEXT: ]
-# CHECK-HEADER-NEXT: Address:
-# CHECK-HEADER-NEXT: Offset:
-# CHECK-HEADER-NEXT: Size: 8
-
-# CHECK-COMPRESSED: .debug_foo:
-# CHECK-COMPRESSED: .notdebug_foo:
-
-# CHECK-FLAGS:      Name: .debug_foo
-# CHECK-FLAGS-NEXT: Type: SHT_PROGBITS
-# CHECK-FLAGS-NEXT: Flags [
-# CHECK-FLAGS-NEXT: SHF_COMPRESSED
-# CHECK-FLAGS-NEXT: ]
-# CHECK-FLAGS-NEXT: Address:
-# CHECK-FLAGS-NEXT: Offset:
-# CHECK-FLAGS-NEXT: Size: 35
-# CHECK-FLAGS-NOT: Name: .debug_foo
-
-# CHECK-FLAGS: Name: .notdebug_foo
-# CHECK-FLAGS-NEXT: Type: SHT_PROGBITS
-# CHECK-FLAGS-NEXT: Flags [
-# CHECK-FLAGS-NEXT: ]
-# CHECK-FLAGS-NEXT: Address:
-# CHECK-FLAGS-NEXT: Offset:
-# CHECK-FLAGS-NEXT: Size: 8
-
-# CHECK-FLAGS: Name: .rela.debug_foo
-# CHECK-FLAGS-NEXT: Type: SHT_RELA
-# CHECK-FLAGS-NEXT: Flags [
-# CHECK-FLAGS-NEXT: ]
-# CHECK-FLAGS-NEXT: Address:
-# CHECK-FLAGS-NEXT: Offset:
-# CHECK-FLAGS-NEXT: Size:
-# CHECK-FLAGS-NEXT: Link:
-
-# CHECK-FLAGS: Relocations [
-# CHECK-FLAGS-NEXT:   .rela.debug_foo {
-# CHECK-FLAGS-NEXT:     0x1 R_X86_64_32 .debug_foo 0x0
-# CHECK-FLAGS-NEXT:     0x2 R_X86_64_32 .notdebug_foo 0x0
-# CHECK-FLAGS-NEXT:   }
-# CHECK-FLAGS-NEXT: ]
+## Test --compress-debug-sections=zlib and uncompression.
+
+# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t.o && llvm-objcopy %t.o
+# RUN: llvm-objcopy --compress-debug-sections=zlib %t.o %t-zlib.o
+# RUN: llvm-objcopy --decompress-debug-sections %t-zlib.o %t-un.o
+# RUN: cmp %t.o %t-un.o
+
+# RUN: llvm-readelf -S -r -x .debug_foo %t-zlib.o | FileCheck %s --check-prefixes=CHECK,COMPRESSED
+# RUN: llvm-readelf -S -r -x .debug_foo %t-un.o | FileCheck %s --check-prefixes=CHECK,UNCOMPRESSED
+
+# CHECK:             Name              Type            Address          Off    Size   ES Flg Lk Inf Al
+# COMPRESSED:        .debug_foo        PROGBITS        0000000000000000 000040 {{.*}} 00   C  0   0  8
+# COMPRESSED-NEXT:   .notdebug_foo     PROGBITS        0000000000000000 {{.*}} 000008 00      0   0  0
+# UNCOMPRESSED:      .debug_foo        PROGBITS        0000000000000000 000040 000008 00      0   0  0
+# UNCOMPRESSED-NEXT: .notdebug_foo     PROGBITS        0000000000000000 {{.*}} 000008 00      0   0  0
+
+## Relocations do not change.
+# CHECK:             Relocation section '.rela.debug_foo' at offset {{.*}} contains 2 entries:
+# CHECK-NEXT:            Offset
+# CHECK-NEXT:        0000000000000001  000000010000000a R_X86_64_32            0000000000000000 .debug_foo + 0
+# CHECK-NEXT:        0000000000000002  000000020000000a R_X86_64_32            0000000000000000 .notdebug_foo + 0
+
+# COMPRESSED:        Hex dump of section '.debug_foo':
+# COMPRESSED-NEXT:   0x00000000 01000000 {{.*}}   08000000 00000000
+# COMPRESSED-NEXT:   0x00000010 00000000 00000000 {{.*}}
+
+## --compress-debug-sections does not update a compressed section.
+# RUN: llvm-objcopy --compress-debug-sections=zlib %t-zlib.o %t-zlib-zlib.o
+# RUN: cmp %t-zlib.o %t-zlib-zlib.o


        


More information about the llvm-commits mailing list