[PATCH] D134385: [llvm-objcopy] --compress-debug-sections: remove tail padding for ELFCLASS32

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 21 14:00:16 PDT 2022


MaskRay created this revision.
MaskRay added reviewers: dblaikie, jhenderson, leonardchan, phosek, ckissane.
Herald added subscribers: StephenFan, abrachet, hiraditya, emaste.
Herald added a reviewer: alexander-shaposhnikov.
Herald added a project: All.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

For an ELFCLASS32 object, the --compress-debug-sections={zlib,zstd} output has a
tail padding of 12 zero bytes. zlib happily allows this while zstd is rigid and
reports `error: '{{.*}}': failed to decompress section '.debug_foo': Src size is incorrect`.

Cole Kissane reported the problem to me.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134385

Files:
  llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
  llvm/lib/ObjCopy/ELF/ELFObject.cpp
  llvm/lib/ObjCopy/ELF/ELFObject.h
  llvm/test/tools/llvm-objcopy/ELF/Inputs/compress-debug-sections.yaml
  llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zstd.test


Index: llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zstd.test
===================================================================
--- llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zstd.test
+++ llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections-zstd.test
@@ -32,6 +32,12 @@
 # RUN: cmp %t-zstd %t-zstd-zstd
 # RUN: %if zlib %{ llvm-objcopy --compress-debug-sections=zlib %t-zstd %t-zstd-zlib && cmp %t-zstd %t-zstd-zlib %}
 
+## Test roundtrip for ELFCLASS32.
+# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t32 && llvm-objcopy %t32
+# RUN: llvm-objcopy --compress-debug-sections=zstd %t32 %t32-zstd
+# RUN: llvm-objcopy --decompress-debug-sections %t32-zstd %t32-de
+# RUN: cmp %t32 %t32-de
+
 # RUN: yaml2obj %s -o %t-corrupted
 # RUN: not llvm-objcopy --decompress-debug-sections %t-corrupted /dev/null 2>&1 | FileCheck %s -DFILE=%t-corrupted --check-prefix=ERR
 
Index: llvm/test/tools/llvm-objcopy/ELF/Inputs/compress-debug-sections.yaml
===================================================================
--- llvm/test/tools/llvm-objcopy/ELF/Inputs/compress-debug-sections.yaml
+++ llvm/test/tools/llvm-objcopy/ELF/Inputs/compress-debug-sections.yaml
@@ -1,6 +1,6 @@
 --- !ELF
 FileHeader:
-  Class:           ELFCLASS64
+  Class:           ELFCLASS[[BITS=64]]
   Data:            ELFDATA2LSB
   Type:            ET_REL
   Machine:         EM_X86_64
Index: llvm/lib/ObjCopy/ELF/ELFObject.h
===================================================================
--- llvm/lib/ObjCopy/ELF/ELFObject.h
+++ llvm/lib/ObjCopy/ELF/ELFObject.h
@@ -544,7 +544,7 @@
 
 public:
   CompressedSection(const SectionBase &Sec,
-                    DebugCompressionType CompressionType);
+    DebugCompressionType CompressionType, bool Is64Bits);
   CompressedSection(ArrayRef<uint8_t> CompressedData, uint32_t ChType,
                     uint64_t DecompressedSize, uint64_t DecompressedAlign);
 
@@ -1045,6 +1045,7 @@
   Segment ElfHdrSegment;
   Segment ProgramHdrSegment;
 
+  bool Is64Bits;
   uint8_t OSABI;
   uint8_t ABIVersion;
   uint64_t Entry;
Index: llvm/lib/ObjCopy/ELF/ELFObject.cpp
===================================================================
--- llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -527,18 +527,16 @@
 }
 
 CompressedSection::CompressedSection(const SectionBase &Sec,
-                                     DebugCompressionType CompressionType)
+                                     DebugCompressionType CompressionType,
+                                     bool Is64Bits)
     : SectionBase(Sec), CompressionType(CompressionType),
       DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
   compression::compress(compression::Params(CompressionType), OriginalData,
                         CompressedData);
 
   Flags |= ELF::SHF_COMPRESSED;
-  size_t ChdrSize =
-      std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>),
-                        sizeof(object::Elf_Chdr_Impl<object::ELF64BE>)),
-               std::max(sizeof(object::Elf_Chdr_Impl<object::ELF32LE>),
-                        sizeof(object::Elf_Chdr_Impl<object::ELF32BE>)));
+  size_t ChdrSize = Is64Bits ? sizeof(object::Elf_Chdr_Impl<object::ELF64LE>)
+                             : sizeof(object::Elf_Chdr_Impl<object::ELF32LE>);
   Size = ChdrSize + CompressedData.size();
   Align = 8;
 }
@@ -1873,6 +1871,7 @@
     return HeadersFile.takeError();
 
   const typename ELFFile<ELFT>::Elf_Ehdr &Ehdr = HeadersFile->getHeader();
+  Obj.Is64Bits = Ehdr.e_ident[EI_CLASS] == ELFCLASS64;
   Obj.OSABI = Ehdr.e_ident[EI_OSABI];
   Obj.ABIVersion = Ehdr.e_ident[EI_ABIVERSION];
   Obj.Type = Ehdr.e_type;
Index: llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
===================================================================
--- llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
+++ llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
@@ -510,7 +510,7 @@
             Obj, isCompressable,
             [&Config, &Obj](const SectionBase *S) -> Expected<SectionBase *> {
               return &Obj.addSection<CompressedSection>(
-                  CompressedSection(*S, Config.CompressionType));
+                  CompressedSection(*S, Config.CompressionType, Obj.Is64Bits));
             }))
       return Err;
   } else if (Config.DecompressDebugSections) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134385.462001.patch
Type: text/x-patch
Size: 4318 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220921/5001d06b/attachment.bin>


More information about the llvm-commits mailing list