[PATCH] Use zlib to uncompress debug sections in DWARF parser library

Alexey Samsonov samsonov at google.com
Fri Apr 19 00:48:10 PDT 2013


Hi echristo,

This patch makes LLVM tools like llvm-dwarfdump and llvm-symbolizer
understand debug info sections compressed by ld.gold linker. Depends on D683.

http://llvm-reviews.chandlerc.com/D693

Files:
  test/DebugInfo/Inputs/dwarfdump-test-zlib.cc
  test/DebugInfo/Inputs/dwarfdump-test-zlib.elf-x86-64
  test/DebugInfo/dwarfdump-zlib.test
  lib/DebugInfo/DWARFContext.h
  lib/DebugInfo/DWARFContext.cpp

Index: test/DebugInfo/Inputs/dwarfdump-test-zlib.cc
===================================================================
--- /dev/null
+++ test/DebugInfo/Inputs/dwarfdump-test-zlib.cc
@@ -0,0 +1,24 @@
+class DummyClass {
+  int a_;
+ public:
+  DummyClass(int a) : a_(a) {}
+  int add(int b) {
+    return a_ + b;
+  }
+};
+
+int f(int a, int b) {
+  DummyClass c(a);
+  return c.add(b);
+}
+
+int main() {
+  return f(2, 3);
+}
+
+// Built with Clang 3.2 and ld.gold linker:
+// $ mkdir -p /tmp/dbginfo
+// $ cp dwarfdump-test-zlib.cc /tmp/dbginfo
+// $ cd /tmp/dbginfo
+// $ clang++ -g dwarfdump-test-zlib.cc -Wl,--compress-debug-sections=zlib \
+//   -o <output>
Index: test/DebugInfo/dwarfdump-zlib.test
===================================================================
--- /dev/null
+++ test/DebugInfo/dwarfdump-zlib.test
@@ -0,0 +1,10 @@
+RUN: llvm-dwarfdump %p/Inputs/dwarfdump-test-zlib.elf-x86-64  \
+RUN:   | FileCheck %s -check-prefix FULLDUMP
+RUN: llvm-dwarfdump %p/Inputs/dwarfdump-test-zlib.elf-x86-64  \
+RUN:   --address=0x400559 --functions | FileCheck %s -check-prefix MAIN
+
+FULLDUMP: .debug_abbrev contents
+FULLDUMP: .debug_info contents
+
+MAIN: main
+MAIN-NEXT: /tmp/dbginfo{{[/\\]}}dwarfdump-test-zlib.cc:16
Index: lib/DebugInfo/DWARFContext.h
===================================================================
--- lib/DebugInfo/DWARFContext.h
+++ lib/DebugInfo/DWARFContext.h
@@ -161,8 +161,11 @@
   StringRef RangeDWOSection;
   StringRef AddrSection;
 
+  SmallVector<MemoryBuffer*, 4> UncompressedSections;
+
 public:
   DWARFContextInMemory(object::ObjectFile *);
+  ~DWARFContextInMemory();
   virtual bool isLittleEndian() const { return IsLittleEndian; }
   virtual uint8_t getAddressSize() const { return AddressSize; }
   virtual const RelocAddrMap &infoRelocMap() const { return InfoRelocMap; }
Index: lib/DebugInfo/DWARFContext.cpp
===================================================================
--- lib/DebugInfo/DWARFContext.cpp
+++ lib/DebugInfo/DWARFContext.cpp
@@ -10,6 +10,8 @@
 #include "DWARFContext.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringSwitch.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Compression.h"
 #include "llvm/Support/Dwarf.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/Path.h"
@@ -483,6 +485,22 @@
   return InliningInfo;
 }
 
+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;
+}
+
 DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
   IsLittleEndian(Obj->isLittleEndian()),
   AddressSize(Obj->getBytesInAddress()) {
@@ -497,6 +515,22 @@
 
     name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
 
+    // Check if debug info section is compressed with zlib.
+    if (name.startswith("zdebug_")) {
+      uint64_t OriginalSize;
+      if (!zlib::isAvailable() ||
+          !consumeCompressedDebugSectionHeader(data, OriginalSize))
+        continue;
+      OwningPtr<MemoryBuffer> UncompressedSection;
+      if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
+          zlib::StatusOK)
+        continue;
+      // Make data point to uncompressed section contents and save its contents.
+      name = name.substr(1);
+      data = UncompressedSection->getBuffer();
+      UncompressedSections.push_back(UncompressedSection.take());
+    }
+
     StringRef *Section = StringSwitch<StringRef*>(name)
         .Case("debug_info", &InfoSection)
         .Case("debug_abbrev", &AbbrevSection)
@@ -584,4 +618,8 @@
   }
 }
 
+DWARFContextInMemory::~DWARFContextInMemory() {
+  DeleteContainerPointers(UncompressedSections);
+}
+
 void DWARFContextInMemory::anchor() { }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D693.1.patch
Type: text/x-patch
Size: 4104 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130419/dc651ed9/attachment.bin>


More information about the llvm-commits mailing list