[llvm] r288986 - [obj2yaml] Refactor and abstract dwarf2yaml

Chris Bieneman via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 7 13:47:29 PST 2016


Author: cbieneman
Date: Wed Dec  7 15:47:28 2016
New Revision: 288986

URL: http://llvm.org/viewvc/llvm-project?rev=288986&view=rev
Log:
[obj2yaml] Refactor and abstract dwarf2yaml

This makes the dwarf2yaml code separated and reusable allowing ELF and COFF to share implementations with MachO.

Added:
    llvm/trunk/tools/obj2yaml/dwarf2yaml.cpp
Modified:
    llvm/trunk/tools/obj2yaml/CMakeLists.txt
    llvm/trunk/tools/obj2yaml/macho2yaml.cpp
    llvm/trunk/tools/obj2yaml/obj2yaml.h

Modified: llvm/trunk/tools/obj2yaml/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/obj2yaml/CMakeLists.txt?rev=288986&r1=288985&r2=288986&view=diff
==============================================================================
--- llvm/trunk/tools/obj2yaml/CMakeLists.txt (original)
+++ llvm/trunk/tools/obj2yaml/CMakeLists.txt Wed Dec  7 15:47:28 2016
@@ -8,6 +8,7 @@ set(LLVM_LINK_COMPONENTS
 add_llvm_tool(obj2yaml
   obj2yaml.cpp
   coff2yaml.cpp
+  dwarf2yaml.cpp
   elf2yaml.cpp
   macho2yaml.cpp
   Error.cpp

Added: llvm/trunk/tools/obj2yaml/dwarf2yaml.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/obj2yaml/dwarf2yaml.cpp?rev=288986&view=auto
==============================================================================
--- llvm/trunk/tools/obj2yaml/dwarf2yaml.cpp (added)
+++ llvm/trunk/tools/obj2yaml/dwarf2yaml.cpp Wed Dec  7 15:47:28 2016
@@ -0,0 +1,53 @@
+//===------ dwarf2yaml.cpp - obj2yaml conversion tool -----------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Error.h"
+#include "llvm/DebugInfo/DWARF/DWARFContext.h"
+#include "llvm/ObjectYAML/DWARFYAML.h"
+
+using namespace llvm;
+
+void dumpDebugAbbrev(DWARFContextInMemory &DCtx, DWARFYAML::DWARFData &Y) {
+  auto AbbrevSetPtr = DCtx.getDebugAbbrev();
+  if (AbbrevSetPtr) {
+    for (auto AbbrvDeclSet : *AbbrevSetPtr) {
+      for (auto AbbrvDecl : AbbrvDeclSet.second) {
+        DWARFYAML::DWARFAbbrev Abbrv;
+        Abbrv.Code = AbbrvDecl.getCode();
+        Abbrv.Tag = AbbrvDecl.getTag();
+        Abbrv.Children = AbbrvDecl.hasChildren() ? dwarf::DW_CHILDREN_yes
+                                                 : dwarf::DW_CHILDREN_no;
+        for (auto Attribute : AbbrvDecl.attributes()) {
+          DWARFYAML::DWARFAttributeAbbrev AttAbrv;
+          AttAbrv.Attribute = Attribute.Attr;
+          AttAbrv.Form = Attribute.Form;
+          Abbrv.Attributes.push_back(AttAbrv);
+        }
+        Y.AbbrevDecls.push_back(Abbrv);
+      }
+    }
+  }
+}
+
+void dumpDebugStrings(DWARFContextInMemory &DCtx, DWARFYAML::DWARFData &Y) {
+  StringRef RemainingTable = DCtx.getStringSection();
+  while (RemainingTable.size() > 0) {
+    auto SymbolPair = RemainingTable.split('\0');
+    RemainingTable = SymbolPair.second;
+    Y.DebugStrings.push_back(SymbolPair.first);
+  }
+}
+
+std::error_code dwarf2yaml(DWARFContextInMemory &DCtx,
+                           DWARFYAML::DWARFData &Y) {
+  dumpDebugAbbrev(DCtx, Y);
+  dumpDebugStrings(DCtx, Y);
+
+  return obj2yaml_error::success;
+}

Modified: llvm/trunk/tools/obj2yaml/macho2yaml.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/obj2yaml/macho2yaml.cpp?rev=288986&r1=288985&r2=288986&view=diff
==============================================================================
--- llvm/trunk/tools/obj2yaml/macho2yaml.cpp (original)
+++ llvm/trunk/tools/obj2yaml/macho2yaml.cpp Wed Dec  7 15:47:28 2016
@@ -35,7 +35,6 @@ class MachODumper {
                        ArrayRef<uint8_t> OpcodeBuffer, bool Lazy = false);
   void dumpExportTrie(std::unique_ptr<MachOYAML::Object> &Y);
   void dumpSymbols(std::unique_ptr<MachOYAML::Object> &Y);
-  void dumpDWARF(std::unique_ptr<MachOYAML::Object> &Y);
   void dumpDebugAbbrev(DWARFContextInMemory &DCtx,
                         std::unique_ptr<MachOYAML::Object> &Y);
   void dumpDebugStrings(DWARFContextInMemory &DCtx,
@@ -169,7 +168,10 @@ Expected<std::unique_ptr<MachOYAML::Obje
   dumpHeader(Y);
   dumpLoadCommands(Y);
   dumpLinkEdit(Y);
-  dumpDWARF(Y);
+
+  DWARFContextInMemory DICtx(Obj);
+  if(auto Err = dwarf2yaml(DICtx, Y->DWARF))
+    return errorCodeToError(Err);
   return std::move(Y);
 }
 
@@ -466,45 +468,6 @@ void MachODumper::dumpSymbols(std::uniqu
   }
 }
 
-void MachODumper::dumpDWARF(std::unique_ptr<MachOYAML::Object> &Y) {
-  DWARFContextInMemory DICtx(Obj);
-  dumpDebugStrings(DICtx, Y);
-  dumpDebugAbbrev(DICtx, Y);
-}
-
-void MachODumper::dumpDebugStrings(DWARFContextInMemory &DICtx,
-                                   std::unique_ptr<MachOYAML::Object> &Y) {
-  StringRef RemainingTable = DICtx.getStringSection();
-  while (RemainingTable.size() > 0) {
-    auto SymbolPair = RemainingTable.split('\0');
-    RemainingTable = SymbolPair.second;
-    Y->DWARF.DebugStrings.push_back(SymbolPair.first);
-  }
-}
-
-void MachODumper::dumpDebugAbbrev(DWARFContextInMemory &DCtx,
-                        std::unique_ptr<MachOYAML::Object> &Y) {
-  auto AbbrevSetPtr = DCtx.getDebugAbbrev();
-  if(AbbrevSetPtr) {
-    for(auto AbbrvDeclSet : *AbbrevSetPtr) {
-      for(auto AbbrvDecl : AbbrvDeclSet.second) {
-        DWARFYAML::DWARFAbbrev Abbrv;
-        Abbrv.Code = AbbrvDecl.getCode();
-        Abbrv.Tag = AbbrvDecl.getTag();
-        Abbrv.Children = AbbrvDecl.hasChildren() ? dwarf::DW_CHILDREN_yes
-                                                 : dwarf::DW_CHILDREN_no;
-        for(auto Attribute : AbbrvDecl.attributes()) {
-          DWARFYAML::DWARFAttributeAbbrev AttAbrv;
-          AttAbrv.Attribute = Attribute.Attr;
-          AttAbrv.Form = Attribute.Form;
-          Abbrv.Attributes.push_back(AttAbrv);
-        }
-        Y->DWARF.AbbrevDecls.push_back(Abbrv);
-      }
-    }
-  }
-}
-
 Error macho2yaml(raw_ostream &Out, const object::MachOObjectFile &Obj) {
   MachODumper Dumper(Obj);
   Expected<std::unique_ptr<MachOYAML::Object>> YAML = Dumper.dump();

Modified: llvm/trunk/tools/obj2yaml/obj2yaml.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/obj2yaml/obj2yaml.h?rev=288986&r1=288985&r2=288986&view=diff
==============================================================================
--- llvm/trunk/tools/obj2yaml/obj2yaml.h (original)
+++ llvm/trunk/tools/obj2yaml/obj2yaml.h Wed Dec  7 15:47:28 2016
@@ -24,4 +24,15 @@ std::error_code elf2yaml(llvm::raw_ostre
 std::error_code macho2yaml(llvm::raw_ostream &Out,
                            const llvm::object::Binary &Obj);
 
+// Forward decls for dwarf2yaml
+namespace llvm {
+class DWARFContextInMemory;
+namespace DWARFYAML {
+struct DWARFData;
+}
+}
+
+std::error_code dwarf2yaml(llvm::DWARFContextInMemory &DCtx,
+                           llvm::DWARFYAML::DWARFData &Y);
+
 #endif




More information about the llvm-commits mailing list