[llvm] 3c57589 - [macho2yaml] Refactor the DWARF section dumpers.

Xing GUO via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 10 19:19:06 PDT 2020


Author: Xing GUO
Date: 2020-08-11T10:18:34+08:00
New Revision: 3c5758964c591481947dd7ab99ba901688068488

URL: https://github.com/llvm/llvm-project/commit/3c5758964c591481947dd7ab99ba901688068488
DIFF: https://github.com/llvm/llvm-project/commit/3c5758964c591481947dd7ab99ba901688068488.diff

LOG: [macho2yaml] Refactor the DWARF section dumpers.

This patch refactors the DWARF section dumpers. When dumping a DWARF
section, if the DWARF parser fails to parse the section, we will dump it
as a raw content section. This patch also fixes a bug in
DWARFYAML::Data::isEmpty(). Finally, a test case that tests dumping the
__debug_aranges section is added.

Reviewed By: jhenderson, grimar

Differential Revision: https://reviews.llvm.org/D85506

Added: 
    llvm/test/tools/obj2yaml/MachO/debug-aranges.yaml
    llvm/test/tools/obj2yaml/MachO/unrecognized-debug-section.yaml

Modified: 
    llvm/lib/ObjectYAML/DWARFYAML.cpp
    llvm/test/ObjectYAML/MachO/DWARF-pubsections.yaml
    llvm/tools/obj2yaml/dwarf2yaml.cpp
    llvm/tools/obj2yaml/macho2yaml.cpp
    llvm/tools/obj2yaml/obj2yaml.h

Removed: 
    llvm/test/tools/obj2yaml/MachO/DWARF-debug_aranges-error.yaml


################################################################################
diff  --git a/llvm/lib/ObjectYAML/DWARFYAML.cpp b/llvm/lib/ObjectYAML/DWARFYAML.cpp
index 467127a9923d..048a7237774c 100644
--- a/llvm/lib/ObjectYAML/DWARFYAML.cpp
+++ b/llvm/lib/ObjectYAML/DWARFYAML.cpp
@@ -17,7 +17,7 @@
 namespace llvm {
 
 bool DWARFYAML::Data::isEmpty() const {
-  return DebugStrings.empty() && AbbrevDecls.empty() && DebugAranges &&
+  return DebugStrings.empty() && AbbrevDecls.empty() && !DebugAranges &&
          DebugRanges.empty() && !PubNames && !PubTypes && !GNUPubNames &&
          !GNUPubTypes && CompileUnits.empty() && DebugLines.empty();
 }

diff  --git a/llvm/test/ObjectYAML/MachO/DWARF-pubsections.yaml b/llvm/test/ObjectYAML/MachO/DWARF-pubsections.yaml
index 0bc510dca5f0..05c4f6660315 100644
--- a/llvm/test/ObjectYAML/MachO/DWARF-pubsections.yaml
+++ b/llvm/test/ObjectYAML/MachO/DWARF-pubsections.yaml
@@ -372,7 +372,6 @@ DWARF:
 # EMPTY-NEXT:     reserved1:       0x00000000
 # EMPTY-NEXT:     reserved2:       0x00000000
 # EMPTY-NEXT:     reserved3:       0x00000000
-# EMPTY-NEXT:     content:         ''
 # EMPTY-NEXT:   - sectname:        __debug_pubtypes
 # EMPTY-NEXT:     segname:         __DWARF
 # EMPTY-NEXT:     addr:            0x0000000000000000
@@ -385,7 +384,6 @@ DWARF:
 # EMPTY-NEXT:     reserved1:       0x00000000
 # EMPTY-NEXT:     reserved2:       0x00000000
 # EMPTY-NEXT:     reserved3:       0x00000000
-# EMPTY-NEXT:     content:         ''
 # EMPTY-NEXT: ...
 
 --- !mach-o

diff  --git a/llvm/test/tools/obj2yaml/MachO/DWARF-debug_aranges-error.yaml b/llvm/test/tools/obj2yaml/MachO/DWARF-debug_aranges-error.yaml
deleted file mode 100644
index bde5499cf1a7..000000000000
--- a/llvm/test/tools/obj2yaml/MachO/DWARF-debug_aranges-error.yaml
+++ /dev/null
@@ -1,300 +0,0 @@
-# RUN: yaml2obj %s | not obj2yaml 2>&1 | FileCheck %s
-
---- !mach-o
-FileHeader:      
-  magic:           0xFEEDFACF
-  cputype:         0x01000007
-  cpusubtype:      0x00000003
-  filetype:        0x0000000A
-  ncmds:           5
-  sizeofcmds:      1800
-  flags:           0x00000000
-  reserved:        0x00000000
-LoadCommands:    
-  - cmd:             LC_SEGMENT_64
-    cmdsize:         72
-    segname:         __PAGEZERO
-    vmaddr:          0
-    vmsize:          4294967296
-    fileoff:         0
-    filesize:        0
-    maxprot:         0
-    initprot:        0
-    nsects:          0
-    flags:           0
-  - cmd:             LC_SEGMENT_64
-    cmdsize:         472
-    segname:         __TEXT
-    vmaddr:          4294967296
-    vmsize:          4096
-    fileoff:         0
-    filesize:        0
-    maxprot:         7
-    initprot:        5
-    nsects:          5
-    flags:           0
-    Sections:        
-      - sectname:        __text
-        segname:         __TEXT
-        addr:            0x0000000100000F50
-        size:            52
-        offset:          0x00000000
-        align:           4
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x80000400
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __stubs
-        segname:         __TEXT
-        addr:            0x0000000100000F84
-        size:            6
-        offset:          0x00000000
-        align:           1
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x80000408
-        reserved1:       0x00000000
-        reserved2:       0x00000006
-        reserved3:       0x00000000
-      - sectname:        __stub_helper
-        segname:         __TEXT
-        addr:            0x0000000100000F8C
-        size:            26
-        offset:          0x00000000
-        align:           2
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x80000400
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __cstring
-        segname:         __TEXT
-        addr:            0x0000000100000FA6
-        size:            14
-        offset:          0x00000000
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000002
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __unwind_info
-        segname:         __TEXT
-        addr:            0x0000000100000FB4
-        size:            72
-        offset:          0x00000000
-        align:           2
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-  - cmd:             LC_SEGMENT_64
-    cmdsize:         232
-    segname:         __DATA
-    vmaddr:          4294971392
-    vmsize:          4096
-    fileoff:         0
-    filesize:        0
-    maxprot:         7
-    initprot:        3
-    nsects:          2
-    flags:           0
-    Sections:        
-      - sectname:        __nl_symbol_ptr
-        segname:         __DATA
-        addr:            0x0000000100001000
-        size:            16
-        offset:          0x00000000
-        align:           3
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000006
-        reserved1:       0x00000001
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __la_symbol_ptr
-        segname:         __DATA
-        addr:            0x0000000100001010
-        size:            8
-        offset:          0x00000000
-        align:           3
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000007
-        reserved1:       0x00000003
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-  - cmd:             LC_SEGMENT_64
-    cmdsize:         72
-    segname:         __LINKEDIT
-    vmaddr:          4294975488
-    vmsize:          4096
-    fileoff:         4096
-    filesize:        60
-    maxprot:         7
-    initprot:        1
-    nsects:          0
-    flags:           0
-  - cmd:             LC_SEGMENT_64
-    cmdsize:         952
-    segname:         __DWARF
-    vmaddr:          4294979584
-    vmsize:          4096
-    fileoff:         8192
-    filesize:        764
-    maxprot:         7
-    initprot:        3
-    nsects:          11
-    flags:           0
-    Sections:        
-      - sectname:        __debug_line
-        segname:         __DWARF
-        addr:            0x0000000100003000
-        size:            69
-        offset:          0x00002000
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __debug_pubnames
-        segname:         __DWARF
-        addr:            0x0000000100003045
-        size:            27
-        offset:          0x00002045
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __debug_pubtypes
-        segname:         __DWARF
-        addr:            0x0000000100003060
-        size:            35
-        offset:          0x00002060
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __debug_aranges
-        segname:         __DWARF
-        addr:            0x0000000100003083
-        size:            48
-        offset:          0x00002083
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __debug_info
-        segname:         __DWARF
-        addr:            0x00000001000030B3
-        size:            121
-        offset:          0x000020B3
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __debug_abbrev
-        segname:         __DWARF
-        addr:            0x000000010000312C
-        size:            76
-        offset:          0x0000212C
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __debug_str
-        segname:         __DWARF
-        addr:            0x0000000100003178
-        size:            142
-        offset:          0x00002178
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __apple_names
-        segname:         __DWARF
-        addr:            0x0000000100003206
-        size:            60
-        offset:          0x00002206
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __apple_namespac
-        segname:         __DWARF
-        addr:            0x0000000100003242
-        size:            36
-        offset:          0x00002242
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __apple_types
-        segname:         __DWARF
-        addr:            0x0000000100003266
-        size:            114
-        offset:          0x00002266
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-      - sectname:        __apple_objc
-        segname:         __DWARF
-        addr:            0x00000001000032D8
-        size:            36
-        offset:          0x000022D8
-        align:           0
-        reloff:          0x00000000
-        nreloc:          0
-        flags:           0x00000000
-        reserved1:       0x00000000
-        reserved2:       0x00000000
-        reserved3:       0x00000000
-DWARF:
-  debug_aranges:
-    - Length:              45
-      Version:             2
-      CuOffset:            0
-      AddressSize:         8
-      SegmentSelectorSize: 0
-      Descriptors:
-        - Address:         0x0000000100000F50
-          Length:          52
-...
-
-#CHECK: the length of address range table at offset 0x0 exceeds section size

diff  --git a/llvm/test/tools/obj2yaml/MachO/debug-aranges.yaml b/llvm/test/tools/obj2yaml/MachO/debug-aranges.yaml
new file mode 100644
index 000000000000..b9d005297717
--- /dev/null
+++ b/llvm/test/tools/obj2yaml/MachO/debug-aranges.yaml
@@ -0,0 +1,137 @@
+## a) Test that we are able to dump the __debug_aranges section.
+## The content of the __debug_aranges section should be written in
+## the "DWARF" entry and the "content" field should remain empty.
+
+# RUN: yaml2obj --docnum=1 %s | obj2yaml | FileCheck -DSEGNAME=DWARF %s --check-prefix=ARANGES
+
+#      ARANGES: Sections:
+# ARANGES-NEXT:   - sectname:        __debug_aranges
+# ARANGES-NEXT:     segname:         __[[SEGNAME]]
+# ARANGES-NEXT:     addr:            0x0000000000000000
+# ARANGES-NEXT:     size:            64
+# ARANGES-NEXT:     offset:          0x00000210
+# ARANGES-NEXT:     align:           0
+# ARANGES-NEXT:     reloff:          0x00000000
+# ARANGES-NEXT:     nreloc:          0
+# ARANGES-NEXT:     flags:           0x00000000
+# ARANGES-NEXT:     reserved1:       0x00000000
+# ARANGES-NEXT:     reserved2:       0x00000000
+# ARANGES-NEXT:     reserved3:       0x00000000
+# ARANGES-NEXT: DWARF:
+# ARANGES-NEXT:   debug_aranges:
+# ARANGES-NEXT:     - Length:          0x000000000000003C
+# ARANGES-NEXT:       Version:         2
+# ARANGES-NEXT:       CuOffset:        0x0000000000001234
+# ARANGES-NEXT:       AddressSize:     0x08
+# ARANGES-NEXT:       Descriptors:
+# ARANGES-NEXT:         - Address:         0x0000000000001234
+# ARANGES-NEXT:           Length:          0x0000000000005678
+# ARANGES-NEXT:         - Address:         0x0000000000001234
+# ARANGES-NEXT:           Length:          0x0000000000005678
+
+--- !mach-o
+FileHeader:
+  magic:      0xFEEDFACF
+  cputype:    0x01000007
+  cpusubtype: 0x00000003
+  filetype:   0x0000000A
+  ncmds:      1
+  sizeofcmds: 232
+  flags:      0x00000000
+  reserved:   0x00000000
+LoadCommands:
+  - cmd:      LC_SEGMENT_64
+    cmdsize:  152
+    segname:  __DWARF
+    vmaddr:   0x00
+    vmsize:   0x00
+    fileoff:  0x00
+    filesize: 0x00
+    maxprot:  0
+    initprot: 0
+    nsects:   1
+    flags:    0
+    Sections:
+      - sectname:  __debug_aranges
+        segname:   __DWARF
+        addr:      0x00
+        size:      64
+        offset:    528
+        align:     0
+        reloff:    0x00000000
+        nreloc:    0
+        flags:     0x00000000
+        reserved1: 0x00000000
+        reserved2: 0x00000000
+        reserved3: 0x00000000
+DWARF:
+  debug_aranges:
+    - Length:   [[LENGTH=<none>]]
+      Version:  2
+      CuOffset: 0x1234
+      Descriptors:
+        - Address: 0x1234
+          Length:  0x5678
+        - Address: 0x1234
+          Length:  0x5678
+
+## b) Test dumping a __debug_aranges section whose length field doesn't match the actual length.
+## This makes the DWARF parser fail to parse it and we will dump it as a raw content section.
+
+# RUN: yaml2obj --docnum=1 -DLENGTH=0x2 %s | obj2yaml | FileCheck %s --check-prefix=RAW-CONTENT
+
+#      RAW-CONTENT: - sectname:        __debug_aranges
+# RAW-CONTENT-NEXT:   segname:         __DWARF
+# RAW-CONTENT-NEXT:   addr:            0x0000000000000000
+# RAW-CONTENT-NEXT:   size:            64
+# RAW-CONTENT-NEXT:   offset:          0x00000210
+# RAW-CONTENT-NEXT:   align:           0
+# RAW-CONTENT-NEXT:   reloff:          0x00000000
+# RAW-CONTENT-NEXT:   nreloc:          0
+# RAW-CONTENT-NEXT:   flags:           0x00000000
+# RAW-CONTENT-NEXT:   reserved1:       0x00000000
+# RAW-CONTENT-NEXT:   reserved2:       0x00000000
+# RAW-CONTENT-NEXT:   reserved3:       0x00000000
+# RAW-CONTENT-NEXT:   content:         '02000000020034120000080000000000341200000000000078560000000000003412000000000000785600000000000000000000000000000000000000000000'
+# RAW-CONTENT-NEXT: ...
+
+## c) Test dumping a __debug_aranges section whose segname is __FOO.
+
+# RUN: yaml2obj --docnum=2 %s | obj2yaml | FileCheck %s -DSEGNAME=FOO --check-prefix=ARANGES
+
+--- !mach-o
+FileHeader:
+  magic:      0xFEEDFACF
+  cputype:    0x01000007
+  cpusubtype: 0x00000003
+  filetype:   0x0000000A
+  ncmds:      1
+  sizeofcmds: 232
+  flags:      0x00000000
+  reserved:   0x00000000
+LoadCommands:
+  - cmd:      LC_SEGMENT_64
+    cmdsize:  152
+    segname:  __DWARF
+    vmaddr:   0x00
+    vmsize:   0x00
+    fileoff:  0x00
+    filesize: 0x00
+    maxprot:  0
+    initprot: 0
+    nsects:   1
+    flags:    0
+    Sections:
+      - sectname:  __debug_aranges
+        segname:   __FOO
+        addr:      0x00
+        size:      64
+        offset:    528
+        align:     0
+        reloff:    0x00000000
+        nreloc:    0
+        flags:     0x00000000
+        reserved1: 0x00000000
+        reserved2: 0x00000000
+        reserved3: 0x00000000
+        content:   '3C000000020034120000080000000000341200000000000078560000000000003412000000000000785600000000000000000000000000000000000000000000'

diff  --git a/llvm/test/tools/obj2yaml/MachO/unrecognized-debug-section.yaml b/llvm/test/tools/obj2yaml/MachO/unrecognized-debug-section.yaml
new file mode 100644
index 000000000000..161805b30834
--- /dev/null
+++ b/llvm/test/tools/obj2yaml/MachO/unrecognized-debug-section.yaml
@@ -0,0 +1,62 @@
+## Test that macho2yaml dumps the __debug_foo section (unrecognized debug section)
+## as a raw content section.
+
+## Due to the current implementation of yaml2macho being buggy, we cannot generate a DWARF section
+## where the sectname starts with '__debug_' and the segname is '__DWARF', from a raw content section.
+## We've slightly modified the segname to be '__FOO'. macho2yaml will still treat it as a debug
+## section.
+
+# RUN: yaml2obj %s | obj2yaml | FileCheck %s --check-prefix=UNRECOGNIZED
+
+#      UNRECOGNIZED: Sections:
+# UNRECOGNIZED-NEXT:   - sectname:        __debug_foo
+# UNRECOGNIZED-NEXT:     segname:         __FOO
+# UNRECOGNIZED-NEXT:     addr:            0x0000000000000000
+# UNRECOGNIZED-NEXT:     size:            5
+# UNRECOGNIZED-NEXT:     offset:          0x00000210
+# UNRECOGNIZED-NEXT:     align:           0
+# UNRECOGNIZED-NEXT:     reloff:          0x00000000
+# UNRECOGNIZED-NEXT:     nreloc:          0
+# UNRECOGNIZED-NEXT:     flags:           0x00000000
+# UNRECOGNIZED-NEXT:     reserved1:       0x00000000
+# UNRECOGNIZED-NEXT:     reserved2:       0x00000000
+# UNRECOGNIZED-NEXT:     reserved3:       0x00000000
+# UNRECOGNIZED-NEXT:     content:         '0123456789'
+# UNRECOGNIZED-NEXT: ...
+
+--- !mach-o
+FileHeader:
+  magic:      0xFEEDFACF
+  cputype:    0x01000007
+  cpusubtype: 0x00000003
+  filetype:   0x0000000A
+  ncmds:      1
+  sizeofcmds: 232
+  flags:      0x00000000
+  reserved:   0x00000000
+LoadCommands:
+  - cmd:      LC_SEGMENT_64
+    cmdsize:  152
+    segname:  __DWARF
+    vmaddr:   0x00
+    vmsize:   0x00
+    fileoff:  0x00
+    filesize: 0x00
+    maxprot:  0
+    initprot: 0
+    nsects:   1
+    flags:    0
+    Sections:
+      - sectname:  __debug_foo
+        segname:   __FOO
+        addr:      0x00
+        size:      5
+        offset:    528
+        align:     0
+        reloff:    0x00000000
+        nreloc:    0
+        flags:     0x00000000
+        reserved1: 0x00000000
+        reserved2: 0x00000000
+        reserved3: 0x00000000
+        content:   '0123456789'

diff  --git a/llvm/tools/obj2yaml/dwarf2yaml.cpp b/llvm/tools/obj2yaml/dwarf2yaml.cpp
index 4e8677184648..65315ca01fa4 100644
--- a/llvm/tools/obj2yaml/dwarf2yaml.cpp
+++ b/llvm/tools/obj2yaml/dwarf2yaml.cpp
@@ -411,16 +411,3 @@ void dumpDebugLines(DWARFContext &DCtx, DWARFYAML::Data &Y) {
     }
   }
 }
-
-llvm::Error dwarf2yaml(DWARFContext &DCtx, DWARFYAML::Data &Y) {
-  dumpDebugAbbrev(DCtx, Y);
-  dumpDebugStrings(DCtx, Y);
-  if (Error E = dumpDebugARanges(DCtx, Y))
-    return E;
-  if (Error E = dumpDebugRanges(DCtx, Y))
-    return E;
-  dumpDebugPubSections(DCtx, Y);
-  dumpDebugInfo(DCtx, Y);
-  dumpDebugLines(DCtx, Y);
-  return ErrorSuccess();
-}

diff  --git a/llvm/tools/obj2yaml/macho2yaml.cpp b/llvm/tools/obj2yaml/macho2yaml.cpp
index 1d89bf115706..dec9b33ece46 100644
--- a/llvm/tools/obj2yaml/macho2yaml.cpp
+++ b/llvm/tools/obj2yaml/macho2yaml.cpp
@@ -10,7 +10,9 @@
 #include "obj2yaml.h"
 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
 #include "llvm/Object/MachOUniversal.h"
+#include "llvm/ObjectYAML/DWARFYAML.h"
 #include "llvm/ObjectYAML/ObjectYAML.h"
+#include "llvm/Support/Error.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/LEB128.h"
 
@@ -23,9 +25,11 @@ class MachODumper {
   template <typename StructType>
   Expected<const char *> processLoadCommandData(
       MachOYAML::LoadCommand &LC,
-      const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd);
+      const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
+      MachOYAML::Object &Y);
 
   const object::MachOObjectFile &Obj;
+  std::unique_ptr<DWARFContext> DWARFCtx;
   void dumpHeader(std::unique_ptr<MachOYAML::Object> &Y);
   Error dumpLoadCommands(std::unique_ptr<MachOYAML::Object> &Y);
   void dumpLinkEdit(std::unique_ptr<MachOYAML::Object> &Y);
@@ -44,10 +48,13 @@ class MachODumper {
   template <typename SectionType, typename SegmentType>
   Expected<const char *>
   extractSections(const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
-                  std::vector<MachOYAML::Section> &Sections);
+                  std::vector<MachOYAML::Section> &Sections,
+                  MachOYAML::Object &Y);
 
 public:
-  MachODumper(const object::MachOObjectFile &O) : Obj(O) {}
+  MachODumper(const object::MachOObjectFile &O,
+              std::unique_ptr<DWARFContext> DCtx)
+      : Obj(O), DWARFCtx(std::move(DCtx)) {}
   Expected<std::unique_ptr<MachOYAML::Object>> dump();
 };
 
@@ -58,7 +65,7 @@ class MachODumper {
     if (Obj.isLittleEndian() != sys::IsLittleEndianHost)                       \
       MachO::swapStruct(LC.Data.LCStruct##_data);                              \
     if (Expected<const char *> ExpectedEndPtr =                                \
-            processLoadCommandData<MachO::LCStruct>(LC, LoadCmd))              \
+            processLoadCommandData<MachO::LCStruct>(LC, LoadCmd, *Y.get()))    \
       EndPtr = *ExpectedEndPtr;                                                \
     else                                                                       \
       return ExpectedEndPtr.takeError();                                       \
@@ -125,10 +132,41 @@ MachODumper::constructSection(MachO::section_64 Sec, size_t SecIndex) {
   return TempSec;
 }
 
+static Error dumpDebugSection(StringRef SecName, DWARFContext &DCtx,
+                              DWARFYAML::Data &DWARF) {
+  if (SecName == "__debug_abbrev") {
+    dumpDebugAbbrev(DCtx, DWARF);
+    return Error::success();
+  }
+  if (SecName == "__debug_aranges")
+    return dumpDebugARanges(DCtx, DWARF);
+  if (SecName == "__debug_info") {
+    dumpDebugInfo(DCtx, DWARF);
+    return Error::success();
+  }
+  if (SecName == "__debug_line") {
+    dumpDebugLines(DCtx, DWARF);
+    return Error::success();
+  }
+  if (SecName.startswith("__debug_pub")) {
+    // FIXME: We should extract pub-section dumpers from this function.
+    dumpDebugPubSections(DCtx, DWARF);
+    return Error::success();
+  }
+  if (SecName == "__debug_ranges")
+    return dumpDebugRanges(DCtx, DWARF);
+  if (SecName == "__debug_str") {
+    dumpDebugStrings(DCtx, DWARF);
+    return Error::success();
+  }
+  return createStringError(errc::not_supported,
+                           "dumping " + SecName + " section is not supported");
+}
+
 template <typename SectionType, typename SegmentType>
 Expected<const char *> MachODumper::extractSections(
     const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
-    std::vector<MachOYAML::Section> &Sections) {
+    std::vector<MachOYAML::Section> &Sections, MachOYAML::Object &Y) {
   auto End = LoadCmd.Ptr + LoadCmd.C.cmdsize;
   const SectionType *Curr =
       reinterpret_cast<const SectionType *>(LoadCmd.Ptr + sizeof(SegmentType));
@@ -139,9 +177,19 @@ Expected<const char *> MachODumper::extractSections(
       MachO::swapStruct(Sec);
     // For MachO section indices start from 1.
     if (Expected<MachOYAML::Section> S =
-            constructSection(Sec, Sections.size() + 1))
+            constructSection(Sec, Sections.size() + 1)) {
+      StringRef SecName(S->sectname);
+      DWARFYAML::Data DWARF;
+      if (SecName.startswith("__debug_")) {
+        // If the DWARF section cannot be successfully parsed, emit raw content
+        // instead of an entry in the DWARF section of the YAML.
+        if (Error Err = dumpDebugSection(SecName, *DWARFCtx.get(), Y.DWARF))
+          consumeError(std::move(Err));
+        else
+          S->content.reset();
+      }
       Sections.push_back(std::move(*S));
-    else
+    } else
       return S.takeError();
   }
   return reinterpret_cast<const char *>(Curr);
@@ -150,7 +198,8 @@ Expected<const char *> MachODumper::extractSections(
 template <typename StructType>
 Expected<const char *> MachODumper::processLoadCommandData(
     MachOYAML::LoadCommand &LC,
-    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
+    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
+    MachOYAML::Object &Y) {
   return LoadCmd.Ptr + sizeof(StructType);
 }
 
@@ -158,18 +207,20 @@ template <>
 Expected<const char *>
 MachODumper::processLoadCommandData<MachO::segment_command>(
     MachOYAML::LoadCommand &LC,
-    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
-  return extractSections<MachO::section, MachO::segment_command>(LoadCmd,
-                                                                 LC.Sections);
+    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
+    MachOYAML::Object &Y) {
+  return extractSections<MachO::section, MachO::segment_command>(
+      LoadCmd, LC.Sections, Y);
 }
 
 template <>
 Expected<const char *>
 MachODumper::processLoadCommandData<MachO::segment_command_64>(
     MachOYAML::LoadCommand &LC,
-    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
+    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
+    MachOYAML::Object &Y) {
   return extractSections<MachO::section_64, MachO::segment_command_64>(
-      LoadCmd, LC.Sections);
+      LoadCmd, LC.Sections, Y);
 }
 
 template <typename StructType>
@@ -187,7 +238,8 @@ template <>
 Expected<const char *>
 MachODumper::processLoadCommandData<MachO::dylib_command>(
     MachOYAML::LoadCommand &LC,
-    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
+    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
+    MachOYAML::Object &Y) {
   return readString<MachO::dylib_command>(LC, LoadCmd);
 }
 
@@ -195,7 +247,8 @@ template <>
 Expected<const char *>
 MachODumper::processLoadCommandData<MachO::dylinker_command>(
     MachOYAML::LoadCommand &LC,
-    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
+    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
+    MachOYAML::Object &Y) {
   return readString<MachO::dylinker_command>(LC, LoadCmd);
 }
 
@@ -203,7 +256,8 @@ template <>
 Expected<const char *>
 MachODumper::processLoadCommandData<MachO::rpath_command>(
     MachOYAML::LoadCommand &LC,
-    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
+    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
+    MachOYAML::Object &Y) {
   return readString<MachO::rpath_command>(LC, LoadCmd);
 }
 
@@ -211,7 +265,8 @@ template <>
 Expected<const char *>
 MachODumper::processLoadCommandData<MachO::build_version_command>(
     MachOYAML::LoadCommand &LC,
-    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
+    const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
+    MachOYAML::Object &Y) {
   auto Start = LoadCmd.Ptr + sizeof(MachO::build_version_command);
   auto NTools = LC.Data.build_version_command_data.ntools;
   for (unsigned i = 0; i < NTools; ++i) {
@@ -233,9 +288,6 @@ Expected<std::unique_ptr<MachOYAML::Object>> MachODumper::dump() {
     return std::move(Err);
   dumpLinkEdit(Y);
 
-  std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj);
-  if (auto Err = dwarf2yaml(*DICtx, Y->DWARF))
-    return std::move(Err);
   return std::move(Y);
 }
 
@@ -261,7 +313,8 @@ Error MachODumper::dumpLoadCommands(std::unique_ptr<MachOYAML::Object> &Y) {
       if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
         MachO::swapStruct(LC.Data.load_command_data);
       if (Expected<const char *> ExpectedEndPtr =
-              processLoadCommandData<MachO::load_command>(LC, LoadCmd))
+              processLoadCommandData<MachO::load_command>(LC, LoadCmd,
+                                                          *Y.get()))
         EndPtr = *ExpectedEndPtr;
       else
         return ExpectedEndPtr.takeError();
@@ -539,7 +592,8 @@ void MachODumper::dumpSymbols(std::unique_ptr<MachOYAML::Object> &Y) {
 }
 
 Error macho2yaml(raw_ostream &Out, const object::MachOObjectFile &Obj) {
-  MachODumper Dumper(Obj);
+  std::unique_ptr<DWARFContext> DCtx = DWARFContext::create(Obj);
+  MachODumper Dumper(Obj, std::move(DCtx));
   Expected<std::unique_ptr<MachOYAML::Object>> YAML = Dumper.dump();
   if (!YAML)
     return YAML.takeError();
@@ -573,7 +627,8 @@ Error macho2yaml(raw_ostream &Out, const object::MachOUniversalBinary &Obj) {
     if (!SliceObj)
       return SliceObj.takeError();
 
-    MachODumper Dumper(*SliceObj.get());
+    std::unique_ptr<DWARFContext> DCtx = DWARFContext::create(*SliceObj.get());
+    MachODumper Dumper(*SliceObj.get(), std::move(DCtx));
     Expected<std::unique_ptr<MachOYAML::Object>> YAMLObj = Dumper.dump();
     if (!YAMLObj)
       return YAMLObj.takeError();

diff  --git a/llvm/tools/obj2yaml/obj2yaml.h b/llvm/tools/obj2yaml/obj2yaml.h
index 9dcb2fac3b83..85a7ac9a4787 100644
--- a/llvm/tools/obj2yaml/obj2yaml.h
+++ b/llvm/tools/obj2yaml/obj2yaml.h
@@ -40,7 +40,13 @@ struct Data;
 }
 }
 
-llvm::Error dwarf2yaml(llvm::DWARFContext &DCtx, llvm::DWARFYAML::Data &Y);
+void dumpDebugAbbrev(llvm::DWARFContext &DCtx, llvm::DWARFYAML::Data &Y);
 llvm::Error dumpDebugARanges(llvm::DWARFContext &DCtx,
                              llvm::DWARFYAML::Data &Y);
+void dumpDebugPubSections(llvm::DWARFContext &DCtx, llvm::DWARFYAML::Data &Y);
+void dumpDebugInfo(llvm::DWARFContext &DCtx, llvm::DWARFYAML::Data &Y);
+void dumpDebugLines(llvm::DWARFContext &DCtx, llvm::DWARFYAML::Data &Y);
+llvm::Error dumpDebugRanges(llvm::DWARFContext &DCtx, llvm::DWARFYAML::Data &Y);
+void dumpDebugStrings(llvm::DWARFContext &DCtx, llvm::DWARFYAML::Data &Y);
+
 #endif


        


More information about the llvm-commits mailing list