JDevlieghere created this revision.
JDevlieghere added reviewers: labath, clayborg.
Herald added a subscriber: aprantl.
Herald added a project: LLDB.

This adds two convenience methods named `GetAsLLVM` to the LLDB versions of the 
DWARF DataExtractor and the DWARF context.


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D64535

Files:
  lldb/include/lldb/Core/Section.h
  lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
  lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.h
  lldb/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.cpp
  lldb/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.h

Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.h
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.h
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.h
@@ -11,6 +11,7 @@
 
 #include "lldb/Core/dwarf.h"
 #include "lldb/Utility/DataExtractor.h"
+#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
 
 namespace lldb_private {
 
@@ -28,6 +29,8 @@
 
   size_t GetDWARFSizeofInitialLength() const { return 4; }
   size_t GetDWARFSizeOfOffset() const { return 4; }
+
+  llvm::DWARFDataExtractor GetAsLLVM() const;
 };
 }
 
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "DWARFDataExtractor.h"
+#include "llvm/ADT/StringRef.h"
 
 namespace lldb_private {
 
@@ -19,4 +20,11 @@
 DWARFDataExtractor::GetDWARFOffset(lldb::offset_t *offset_ptr) const {
   return GetMaxU64(offset_ptr, GetDWARFSizeOfOffset());
 }
+
+llvm::DWARFDataExtractor DWARFDataExtractor::GetAsLLVM() const {
+  return llvm::DWARFDataExtractor(
+      llvm::StringRef(reinterpret_cast<const char *>(GetDataStart()),
+                      GetByteSize()),
+      GetByteOrder() == lldb::eByteOrderLittle, GetAddressByteSize());
 }
+} // namespace lldb_private
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.h
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.h
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.h
@@ -12,6 +12,7 @@
 #include "DWARFDataExtractor.h"
 #include "lldb/Core/Section.h"
 #include "llvm/ADT/Optional.h"
+#include "llvm/DebugInfo/DWARF/DWARFContext.h"
 #include "llvm/Support/Threading.h"
 #include <memory>
 
@@ -64,6 +65,8 @@
   const DWARFDataExtractor &getOrLoadStrData();
   const DWARFDataExtractor &getOrLoadStrOffsetsData();
   const DWARFDataExtractor &getOrLoadDebugTypesData();
+
+  std::unique_ptr<llvm::DWARFContext> GetAsLLVM() const;
 };
 } // namespace lldb_private
 
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
@@ -100,3 +100,34 @@
   return LoadOrGetSection(eSectionTypeDWARFDebugTypes,
                           eSectionTypeDWARFDebugTypesDwo, m_data_debug_types);
 }
+
+std::unique_ptr<llvm::DWARFContext> DWARFContext::GetAsLLVM() const {
+  llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> section_map;
+  uint8_t addr_size = 0;
+
+  auto AddSection = [&](Section &section) {
+    DataExtractor section_data;
+    section.GetSectionData(section_data);
+
+    // Set the address size the first time we see it.
+    if (addr_size == 0)
+      addr_size = section_data.GetByteSize();
+
+    llvm::StringRef data = llvm::toStringRef(section_data.GetData());
+    llvm::StringRef name = section.GetName().GetStringRef();
+    section_map.try_emplace(
+        name, llvm::MemoryBuffer::getMemBuffer(data, name, false));
+  };
+
+  if (m_main_section_list) {
+    for (auto &section : *m_main_section_list)
+      AddSection(*section);
+  }
+
+  if (m_dwo_section_list) {
+    for (auto &section : *m_dwo_section_list)
+      AddSection(*section);
+  }
+
+  return llvm::DWARFContext::create(section_map, addr_size);
+}
Index: lldb/include/lldb/Core/Section.h
===================================================================
--- lldb/include/lldb/Core/Section.h
+++ lldb/include/lldb/Core/Section.h
@@ -38,6 +38,11 @@
   typedef collection::iterator iterator;
   typedef collection::const_iterator const_iterator;
 
+  const_iterator begin() const { return m_sections.begin(); }
+  const_iterator end() const { return m_sections.end(); }
+  const_iterator begin() { return m_sections.begin(); }
+  const_iterator end() { return m_sections.end(); }
+
   SectionList();
 
   ~SectionList();
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to