[lld] r300674 - Simplify createHeader and inline it.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 19 04:31:59 PDT 2017


Author: ruiu
Date: Wed Apr 19 06:31:58 2017
New Revision: 300674

URL: http://llvm.org/viewvc/llvm-project?rev=300674&view=rev
Log:
Simplify createHeader and inline it.

createHeader didn't use data members of Elf_Chdr type and write
directly to a given buffer. That is not a good practice because
the function had a knowledge of the struct layout.

Modified:
    lld/trunk/ELF/OutputSections.cpp

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=300674&r1=300673&r2=300674&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Wed Apr 19 06:31:58 2017
@@ -84,53 +84,31 @@ static bool compareByFilePosition(InputS
   return LA->OutSecOff < LB->OutSecOff;
 }
 
-// Compressed sections has header which we create in this function.
-// Format is explaned here:
-// https://docs.oracle.com/cd/E53394_01/html/E54813/section_compression.html
-template <class ELFT>
-static std::vector<uint8_t> createHeader(size_t Size, uint32_t Alignment) {
-  const endianness E = ELFT::TargetEndianness;
-
-  std::vector<uint8_t> Ret(sizeof(typename ELFT::Chdr));
-  uint8_t *Buf = &Ret[0];
-  write32<E>(Buf, ELFCOMPRESS_ZLIB);
-  Buf += 4;
-
-  if (Config->Is64) {
-    Buf += sizeof(Elf64_Word); // Skip ch_reserved field.
-    write64<E>(Buf, Size);
-    Buf += sizeof(ELFT::Chdr::ch_size);
-    write64<E>(Buf, Alignment);
-    Buf += sizeof(ELFT::Chdr::ch_addralign);
-  } else {
-    write32<E>(Buf, Size);
-    Buf += sizeof(ELFT::Chdr::ch_size);
-    write32<E>(Buf, Alignment);
-    Buf += sizeof(ELFT::Chdr::ch_addralign);
-  }
-
-  return Ret;
-}
-
+// Compress section contents if this section contains debug info.
 template <class ELFT> void OutputSection::maybeCompress() {
-  // If -compress-debug-sections is specified, we compress output debug
-  // sections.
-  if (!Config->CompressDebugSections || !Name.startswith(".debug_") ||
-      (Flags & SHF_ALLOC))
-    return;
-
-  this->Flags |= SHF_COMPRESSED;
-  CompressedHeader = createHeader<ELFT>(this->Size, this->Alignment);
+  typedef typename ELFT::Chdr Elf_Chdr;
 
-  // Here we write relocated content of sections and compress it.
-  std::vector<uint8_t> Data(this->Size);
-  this->writeTo<ELFT>(&Data[0]);
+  // Compress only DWARF debug sections.
+  if (!Config->CompressDebugSections || !(Flags & SHF_ALLOC) ||
+      !Name.startswith(".debug_"))
+    return;
 
-  if (Error E = zlib::compress(StringRef((char *)Data.data(), Data.size()),
-                               CompressedData))
+  // Create a section header.
+  CompressedHeader.resize(sizeof(Elf_Chdr));
+  auto *Hdr = reinterpret_cast<Elf_Chdr *>(CompressedHeader.data());
+  Hdr->ch_type = ELFCOMPRESS_ZLIB;
+  Hdr->ch_size = Size;
+  Hdr->ch_addralign = Alignment;
+
+  // Write section contents to a temporary buffer and compress it.
+  std::vector<uint8_t> Buf(Size);
+  writeTo<ELFT>(Buf.data());
+  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
     fatal("compress failed: " + llvm::toString(std::move(E)));
 
-  this->Size = this->CompressedHeader.size() + this->CompressedData.size();
+  // Update section headers.
+  Size = sizeof(Elf_Chdr) + CompressedData.size();
+  Flags |= SHF_COMPRESSED;
 }
 
 template <class ELFT> void OutputSection::finalize() {




More information about the llvm-commits mailing list