[lld] r274729 - Fix endianness issue.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 6 20:55:55 PDT 2016


Author: ruiu
Date: Wed Jul  6 22:55:55 2016
New Revision: 274729

URL: http://llvm.org/viewvc/llvm-project?rev=274729&view=rev
Log:
Fix endianness issue.

Previously, ch_size was read in host byte order, so if a host and
a target are different in byte order, we would produce a corrupted
output.

Modified:
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/ELF/InputSection.h
    lld/trunk/test/ELF/compressed-debug-input.s

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=274729&r1=274728&r2=274729&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Wed Jul  6 22:55:55 2016
@@ -84,23 +84,24 @@ typename ELFT::uint InputSectionBase<ELF
 }
 
 template <class ELFT> void InputSectionBase<ELFT>::uncompress() {
-  typedef typename std::conditional<ELFT::Is64Bits, Elf64_Chdr,
-                                    Elf32_Chdr>::type Elf_Chdr;
-  const endianness E = ELFT::TargetEndianness;
-
   if (!zlib::isAvailable())
     fatal("build lld with zlib to enable compressed sections support");
 
+  // A compressed section consists of a header of Elf_Chdr type
+  // followed by compressed data.
   ArrayRef<uint8_t> Data =
       check(this->File->getObj().getSectionContents(this->Header));
-  if (read32<E>(Data.data()) != ELFCOMPRESS_ZLIB)
-    fatal("unsupported elf compression type");
+  if (Data.size() < sizeof(Elf_Chdr))
+    fatal("corrupt compressed section");
+
+  auto *Hdr = reinterpret_cast<const Elf_Chdr *>(Data.data());
+  Data = Data.slice(sizeof(Elf_Chdr));
+
+  if (Hdr->ch_type != ELFCOMPRESS_ZLIB)
+    fatal("unsupported compression type");
 
-  size_t UncompressedSize =
-      reinterpret_cast<const Elf_Chdr *>(Data.data())->ch_size;
-  size_t HdrSize = sizeof(Elf_Chdr);
-  StringRef Buf((const char *)Data.data() + HdrSize, Data.size() - HdrSize);
-  if (zlib::uncompress(Buf, Uncompressed, UncompressedSize) != zlib::StatusOK)
+  StringRef Buf((const char *)Data.data(), Data.size());
+  if (zlib::uncompress(Buf, Uncompressed, Hdr->ch_size) != zlib::StatusOK)
     fatal("error uncompressing section");
 }
 

Modified: lld/trunk/ELF/InputSection.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.h?rev=274729&r1=274728&r2=274729&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.h (original)
+++ lld/trunk/ELF/InputSection.h Wed Jul  6 22:55:55 2016
@@ -31,6 +31,7 @@ template <class ELFT> class OutputSectio
 // This corresponds to a section of an input file.
 template <class ELFT> class InputSectionBase {
 protected:
+  typedef typename ELFT::Chdr Elf_Chdr;
   typedef typename ELFT::Rel Elf_Rel;
   typedef typename ELFT::Rela Elf_Rela;
   typedef typename ELFT::Shdr Elf_Shdr;

Modified: lld/trunk/test/ELF/compressed-debug-input.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/compressed-debug-input.s?rev=274729&r1=274728&r2=274729&view=diff
==============================================================================
--- lld/trunk/test/ELF/compressed-debug-input.s (original)
+++ lld/trunk/test/ELF/compressed-debug-input.s Wed Jul  6 22:55:55 2016
@@ -22,9 +22,8 @@
 # COMPRESSED-NEXT: }
 
 # RUN: ld.lld %t -o %t.so -shared
-# RUN: llvm-readobj -sections %t.so | FileCheck -check-prefix=UNCOMPRESSED %s
+# RUN: llvm-readobj -sections -section-data %t.so | FileCheck -check-prefix=UNCOMPRESSED %s
 
-## Check that section is decompressed and compression flag is removed.
 # UNCOMPRESSED:      Section {
 # UNCOMPRESSED:        Index: 6
 # UNCOMPRESSED:        Name: .debug_str
@@ -40,6 +39,13 @@
 # UNCOMPRESSED-NEXT:   Info: 0
 # UNCOMPRESSED-NEXT:   AddressAlignment: 1
 # UNCOMPRESSED-NEXT:   EntrySize: 1
+# UNCOMPRESSED-NEXT:   SectionData (
+# UNCOMPRESSED-NEXT:     0000: 73686F72 7420756E 7369676E 65642069  |short unsigned i|
+# UNCOMPRESSED-NEXT:     0010: 6E740075 6E736967 6E656420 696E7400  |nt.unsigned int.|
+# UNCOMPRESSED-NEXT:     0020: 6C6F6E67 20756E73 69676E65 6420696E  |long unsigned in|
+# UNCOMPRESSED-NEXT:     0030: 74006368 61720075 6E736967 6E656420  |t.char.unsigned |
+# UNCOMPRESSED-NEXT:     0040: 63686172 00                          |char.|
+# UNCOMPRESSED-NEXT:   )
 # UNCOMPRESSED-NEXT: }
 
 .section .debug_str,"MS", at progbits,1




More information about the llvm-commits mailing list