[llvm] r261296 - llvm-dwp: Support compressed input

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 18 17:51:45 PST 2016


Author: dblaikie
Date: Thu Feb 18 19:51:44 2016
New Revision: 261296

URL: http://llvm.org/viewvc/llvm-project?rev=261296&view=rev
Log:
llvm-dwp: Support compressed input

Added:
    llvm/trunk/test/tools/llvm-dwp/Inputs/compress/
    llvm/trunk/test/tools/llvm-dwp/Inputs/compress/a.dwo
    llvm/trunk/test/tools/llvm-dwp/X86/compress.test
Modified:
    llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp

Added: llvm/trunk/test/tools/llvm-dwp/Inputs/compress/a.dwo
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dwp/Inputs/compress/a.dwo?rev=261296&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-dwp/Inputs/compress/a.dwo (added) and llvm/trunk/test/tools/llvm-dwp/Inputs/compress/a.dwo Thu Feb 18 19:51:44 2016 differ

Added: llvm/trunk/test/tools/llvm-dwp/X86/compress.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dwp/X86/compress.test?rev=261296&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-dwp/X86/compress.test (added)
+++ llvm/trunk/test/tools/llvm-dwp/X86/compress.test Thu Feb 18 19:51:44 2016
@@ -0,0 +1,17 @@
+RUN: llvm-dwp %p/../Inputs/compress/a.dwo -o %t
+RUN: llvm-dwarfdump %t | FileCheck %s
+
+Simple test built from this input which produces DWARF long enough to be compressed in the .[z]debug_info section:
+
+  void f(int a, int b, int c, int d) {
+  }
+
+Since the compression is pretty orthogonal, we're not trying to test that the
+compression library functioned correctly, just that dwp used it to decompress
+the section - so test a few simple features and be done with it.
+
+CHECK: .debug_info.dwo contents:
+CHECK: Compile Unit:
+CHECK:   DW_TAG_compile_unit
+CHECK:     DW_TAG_subprogram
+CHECK:       DW_TAG_formal_parameter

Modified: llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp?rev=261296&r1=261295&r2=261296&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp (original)
+++ llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp Thu Feb 18 19:51:44 2016
@@ -12,6 +12,7 @@
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
 #include "llvm/Object/ObjectFile.h"
+#include "llvm/Support/Compression.h"
 #include "llvm/Support/DataExtractor.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/MathExtras.h"
@@ -237,7 +238,7 @@ static void writeIndex(MCStreamer &Out,
     auto H = S & Mask;
     while (Buckets[H]) {
       assert(S != IndexEntries[Buckets[H] - 1].Signature &&
-             "Duplicate type unit");
+             "Duplicate unit");
       H = (H + (((S >> 32) & Mask) | 1)) % Buckets.size();
     }
     Buckets[H] = i + 1;
@@ -270,6 +271,22 @@ static void writeIndex(MCStreamer &Out,
   writeIndexTable(Out, ContributionOffsets, IndexEntries,
                   &DWARFUnitIndex::Entry::SectionContribution::Length);
 }
+static bool consumeCompressedDebugSectionHeader(StringRef &data,
+                                                uint64_t &OriginalSize) {
+  // Consume "ZLIB" prefix.
+  if (!data.startswith("ZLIB"))
+    return false;
+  data = data.substr(4);
+  // Consume uncompressed section size (big-endian 8 bytes).
+  DataExtractor extractor(data, false, 8);
+  uint32_t Offset = 0;
+  OriginalSize = extractor.getU64(&Offset);
+  if (Offset == 0)
+    return false;
+  data = data.substr(Offset);
+  return true;
+}
+
 static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
   const auto &MCOFI = *Out.getContext().getObjectFileInfo();
   MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
@@ -311,20 +328,43 @@ static std::error_code write(MCStreamer
     StringRef CurCUIndexSection;
     StringRef CurTUIndexSection;
 
+    SmallVector<SmallString<32>, 4> UncompressedSections;
+
     for (const auto &Section : ErrOrObj->getBinary()->sections()) {
+      if (Section.isBSS())
+        continue;
+      if (Section.isVirtual())
+        continue;
+
       StringRef Name;
       if (std::error_code Err = Section.getName(Name))
         return Err;
 
-      auto SectionPair =
-          KnownSections.find(Name.substr(Name.find_first_not_of("._")));
-      if (SectionPair == KnownSections.end())
-        continue;
+      Name = Name.substr(Name.find_first_not_of("._"));
 
       StringRef Contents;
       if (auto Err = Section.getContents(Contents))
         return Err;
 
+      if (Name.startswith("zdebug_")) {
+        uint64_t OriginalSize;
+        if (!zlib::isAvailable() ||
+            !consumeCompressedDebugSectionHeader(Contents, OriginalSize))
+          continue;
+        UncompressedSections.resize(UncompressedSections.size() + 1);
+        if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
+            zlib::StatusOK) {
+          UncompressedSections.pop_back();
+          continue;
+        }
+        Name = Name.substr(1);
+        Contents = UncompressedSections.back();
+      }
+
+      auto SectionPair = KnownSections.find(Name);
+      if (SectionPair == KnownSections.end())
+        continue;
+
       if (DWARFSectionKind Kind = SectionPair->second.second) {
         auto Index = Kind - DW_SECT_INFO;
         if (Kind != DW_SECT_TYPES) {




More information about the llvm-commits mailing list