[llvm] r288774 - [ObjectYAML] First bit of support for encoding DWARF in MachO

Chris Bieneman via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 5 22:00:49 PST 2016


Author: cbieneman
Date: Tue Dec  6 00:00:49 2016
New Revision: 288774

URL: http://llvm.org/viewvc/llvm-project?rev=288774&view=rev
Log:
[ObjectYAML] First bit of support for encoding DWARF in MachO

This patch adds the starting support for encoding data from the MachO __DWARF segment. The first section supported is the __debug_str section because it is the simplest.

Added:
    llvm/trunk/test/ObjectYAML/MachO/DWARF-debug_str.yaml
Modified:
    llvm/trunk/include/llvm/ObjectYAML/MachOYAML.h
    llvm/trunk/lib/ObjectYAML/MachOYAML.cpp
    llvm/trunk/tools/obj2yaml/CMakeLists.txt
    llvm/trunk/tools/obj2yaml/macho2yaml.cpp
    llvm/trunk/tools/yaml2obj/yaml2macho.cpp

Modified: llvm/trunk/include/llvm/ObjectYAML/MachOYAML.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ObjectYAML/MachOYAML.h?rev=288774&r1=288773&r2=288774&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ObjectYAML/MachOYAML.h (original)
+++ llvm/trunk/include/llvm/ObjectYAML/MachOYAML.h Tue Dec  6 00:00:49 2016
@@ -104,11 +104,18 @@ struct LinkEditData {
   bool isEmpty() const;
 };
 
+struct DWARFData {
+  std::vector<StringRef> DebugStrings;
+
+  bool isEmpty() const;
+};
+
 struct Object {
   FileHeader Header;
   std::vector<LoadCommand> LoadCommands;
   std::vector<Section> Sections;
   LinkEditData LinkEdit;
+  DWARFData DWARF;
 };
 
 struct FatHeader {
@@ -198,6 +205,10 @@ template <> struct MappingTraits<MachOYA
   static void mapping(IO &IO, MachOYAML::NListEntry &NListEntry);
 };
 
+template <> struct MappingTraits<MachOYAML::DWARFData> {
+  static void mapping(IO &IO, MachOYAML::DWARFData &DWARF);
+};
+
 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct)                         \
   io.enumCase(value, #LCName, MachO::LCName);
 

Modified: llvm/trunk/lib/ObjectYAML/MachOYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ObjectYAML/MachOYAML.cpp?rev=288774&r1=288773&r2=288774&view=diff
==============================================================================
--- llvm/trunk/lib/ObjectYAML/MachOYAML.cpp (original)
+++ llvm/trunk/lib/ObjectYAML/MachOYAML.cpp Tue Dec  6 00:00:49 2016
@@ -23,7 +23,14 @@ namespace llvm {
 MachOYAML::LoadCommand::~LoadCommand() {}
 
 bool MachOYAML::LinkEditData::isEmpty() const {
-  return 0 == RebaseOpcodes.size() + BindOpcodes.size() + WeakBindOpcodes.size() + LazyBindOpcodes.size() + ExportTrie.Children.size() + NameList.size() + StringTable.size();
+  return 0 ==
+         RebaseOpcodes.size() + BindOpcodes.size() + WeakBindOpcodes.size() +
+             LazyBindOpcodes.size() + ExportTrie.Children.size() +
+             NameList.size() + StringTable.size();
+}
+
+bool MachOYAML::DWARFData::isEmpty() const {
+  return 0 == DebugStrings.size();
 }
 
 namespace yaml {
@@ -102,6 +109,9 @@ void MappingTraits<MachOYAML::Object>::m
   if(!Object.LinkEdit.isEmpty() || !IO.outputting())
     IO.mapOptional("LinkEditData", Object.LinkEdit);
 
+  if(!Object.DWARF.isEmpty() || !IO.outputting())
+    IO.mapOptional("DWARF", Object.DWARF);
+
   if (IO.getContext() == &Object)
     IO.setContext(nullptr);
 }
@@ -547,6 +557,11 @@ void MappingTraits<MachO::version_min_co
   IO.mapRequired("sdk", LoadCommand.sdk);
 }
 
+void MappingTraits<MachOYAML::DWARFData>::mapping(
+    IO &IO, MachOYAML::DWARFData &DWARF) {
+  IO.mapRequired("DebugStrings", DWARF.DebugStrings);
+}
+
 } // namespace llvm::yaml
 
 } // namespace llvm

Added: llvm/trunk/test/ObjectYAML/MachO/DWARF-debug_str.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/ObjectYAML/MachO/DWARF-debug_str.yaml?rev=288774&view=auto
==============================================================================
--- llvm/trunk/test/ObjectYAML/MachO/DWARF-debug_str.yaml (added)
+++ llvm/trunk/test/ObjectYAML/MachO/DWARF-debug_str.yaml Tue Dec  6 00:00:49 2016
@@ -0,0 +1,266 @@
+# RUN: yaml2obj %s | obj2yaml | FileCheck %s
+
+--- !mach-o
+FileHeader:      
+  magic:           0xFEEDFACF
+  cputype:         0x01000007
+  cpusubtype:      0x00000003
+  filetype:        0x0000000A
+  ncmds:           6
+  sizeofcmds:      1376
+  flags:           0x00000000
+  reserved:        0x00000000
+LoadCommands:    
+  - cmd:             LC_UUID
+    cmdsize:         24
+    uuid:            9304404B-E522-3BBA-A861-AF5938908725
+  - cmd:             LC_SYMTAB
+    cmdsize:         24
+    symoff:          4096
+    nsyms:           2
+    stroff:          4128
+    strsize:         28
+  - 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:         232
+    segname:         __TEXT
+    vmaddr:          4294967296
+    vmsize:          4096
+    fileoff:         0
+    filesize:        0
+    maxprot:         7
+    initprot:        5
+    nsects:          2
+    flags:           0
+    Sections:        
+      - sectname:        __text
+        segname:         __TEXT
+        addr:            0x0000000100000FA0
+        size:            22
+        offset:          0x00000000
+        align:           4
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x80000400
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+      - sectname:        __unwind_info
+        segname:         __TEXT
+        addr:            0x0000000100000FB8
+        size:            72
+        offset:          0x00000000
+        align:           2
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+  - cmd:             LC_SEGMENT_64
+    cmdsize:         72
+    segname:         __LINKEDIT
+    vmaddr:          4294971392
+    vmsize:          4096
+    fileoff:         4096
+    filesize:        60
+    maxprot:         7
+    initprot:        1
+    nsects:          0
+    flags:           0
+  - cmd:             LC_SEGMENT_64
+    cmdsize:         952
+    segname:         __DWARF
+    vmaddr:          4294975488
+    vmsize:          4096
+    fileoff:         8192
+    filesize:        765
+    maxprot:         7
+    initprot:        3
+    nsects:          11
+    flags:           0
+    Sections:        
+      - sectname:        __debug_line
+        segname:         __DWARF
+        addr:            0x0000000100002000
+        size:            70
+        offset:          0x00002000
+        align:           0
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+      - sectname:        __debug_pubnames
+        segname:         __DWARF
+        addr:            0x0000000100002046
+        size:            27
+        offset:          0x00002046
+        align:           0
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+      - sectname:        __debug_pubtypes
+        segname:         __DWARF
+        addr:            0x0000000100002061
+        size:            35
+        offset:          0x00002061
+        align:           0
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+      - sectname:        __debug_aranges
+        segname:         __DWARF
+        addr:            0x0000000100002084
+        size:            48
+        offset:          0x00002084
+        align:           0
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+      - sectname:        __debug_info
+        segname:         __DWARF
+        addr:            0x00000001000020B4
+        size:            121
+        offset:          0x000020B4
+        align:           0
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+      - sectname:        __debug_abbrev
+        segname:         __DWARF
+        addr:            0x000000010000212D
+        size:            76
+        offset:          0x0000212D
+        align:           0
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+      - sectname:        __debug_str
+        segname:         __DWARF
+        addr:            0x0000000100002179
+        size:            142
+        offset:          0x00002179
+        align:           0
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+      - sectname:        __apple_names
+        segname:         __DWARF
+        addr:            0x0000000100002207
+        size:            60
+        offset:          0x00002207
+        align:           0
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+      - sectname:        __apple_namespac
+        segname:         __DWARF
+        addr:            0x0000000100002243
+        size:            36
+        offset:          0x00002243
+        align:           0
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+      - sectname:        __apple_types
+        segname:         __DWARF
+        addr:            0x0000000100002267
+        size:            114
+        offset:          0x00002267
+        align:           0
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+      - sectname:        __apple_objc
+        segname:         __DWARF
+        addr:            0x00000001000022D9
+        size:            36
+        offset:          0x000022D9
+        align:           0
+        reloff:          0x00000000
+        nreloc:          0
+        flags:           0x00000000
+        reserved1:       0x00000000
+        reserved2:       0x00000000
+        reserved3:       0x00000000
+LinkEditData:    
+  NameList:        
+    - n_strx:          2
+      n_type:          0x0F
+      n_sect:          1
+      n_desc:          16
+      n_value:         4294967296
+    - n_strx:          22
+      n_type:          0x0F
+      n_sect:          1
+      n_desc:          0
+      n_value:         4294971296
+  StringTable:     
+    - ''
+    - ''
+    - __mh_execute_header
+    - _main
+DWARF:           
+  DebugStrings:    
+    - ''
+    - 'clang version 4.0.0 (trunk 288677) (llvm/trunk 288676)'
+    - hello_world.c
+    - /Users/cbieneman/dev/open-source/llvm-build-rel
+    - main
+    - argc
+    - argv
+    - int
+    - char
+...
+
+#CHECK: DWARF:           
+#CHECK:   DebugStrings:    
+#CHECK:     - ''
+#CHECK:     - 'clang version 4.0.0 (trunk 288677) (llvm/trunk 288676)'
+#CHECK:     - hello_world.c
+#CHECK:     - /Users/cbieneman/dev/open-source/llvm-build-rel
+#CHECK:     - main
+#CHECK:     - argc
+#CHECK:     - argv
+#CHECK:     - int
+#CHECK:     - char
+

Modified: llvm/trunk/tools/obj2yaml/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/obj2yaml/CMakeLists.txt?rev=288774&r1=288773&r2=288774&view=diff
==============================================================================
--- llvm/trunk/tools/obj2yaml/CMakeLists.txt (original)
+++ llvm/trunk/tools/obj2yaml/CMakeLists.txt Tue Dec  6 00:00:49 2016
@@ -1,4 +1,5 @@
 set(LLVM_LINK_COMPONENTS
+  DebugInfoDWARF
   Object
   ObjectYAML
   Support

Modified: llvm/trunk/tools/obj2yaml/macho2yaml.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/obj2yaml/macho2yaml.cpp?rev=288774&r1=288773&r2=288774&view=diff
==============================================================================
--- llvm/trunk/tools/obj2yaml/macho2yaml.cpp (original)
+++ llvm/trunk/tools/obj2yaml/macho2yaml.cpp Tue Dec  6 00:00:49 2016
@@ -9,6 +9,7 @@
 
 #include "Error.h"
 #include "obj2yaml.h"
+#include "llvm/DebugInfo/DWARF/DWARFContext.h"
 #include "llvm/Object/MachOUniversal.h"
 #include "llvm/ObjectYAML/ObjectYAML.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -34,6 +35,8 @@ 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 dumpDebugStrings(DWARFContextInMemory &DCtx, std::unique_ptr<MachOYAML::Object> &Y);
 
 public:
   MachODumper(const object::MachOObjectFile &O) : Obj(O) {}
@@ -163,6 +166,7 @@ Expected<std::unique_ptr<MachOYAML::Obje
   dumpHeader(Y);
   dumpLoadCommands(Y);
   dumpLinkEdit(Y);
+  dumpDWARF(Y);
   return std::move(Y);
 }
 
@@ -459,6 +463,21 @@ void MachODumper::dumpSymbols(std::uniqu
   }
 }
 
+void MachODumper::dumpDWARF(std::unique_ptr<MachOYAML::Object> &Y) {
+  DWARFContextInMemory DICtx(Obj);
+  dumpDebugStrings(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);
+  }
+}
+
 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/yaml2obj/yaml2macho.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/yaml2obj/yaml2macho.cpp?rev=288774&r1=288773&r2=288774&view=diff
==============================================================================
--- llvm/trunk/tools/yaml2obj/yaml2macho.cpp (original)
+++ llvm/trunk/tools/yaml2obj/yaml2macho.cpp Tue Dec  6 00:00:49 2016
@@ -41,6 +41,8 @@ private:
   Error writeLoadCommands(raw_ostream &OS);
   Error writeSectionData(raw_ostream &OS);
   Error writeLinkEditData(raw_ostream &OS);
+  Error writeDWARFData(raw_ostream &OS,
+                       std::vector<MachOYAML::Section> &Sections);
   void writeBindOpcodes(raw_ostream &OS,
                         std::vector<MachOYAML::BindOpcode> &BindOpcodes);
   // LinkEdit writers
@@ -240,6 +242,9 @@ Error MachOWriter::writeSectionData(raw_
       if (0 == strncmp(&segname[0], "__LINKEDIT", 16)) {
         if (auto Err = writeLinkEditData(OS))
           return Err;
+      } else if (0 == strncmp(&segname[0], "__DWARF", 16)) {
+        if (auto Err = writeDWARFData(OS, LC.Sections))
+          return Err;
       } else {
         // Zero Fill any data between the end of the last thing we wrote and the
         // start of this section.
@@ -252,7 +257,8 @@ Error MachOWriter::writeSectionData(raw_
           // the
           // start of this section.
           assert(
-              OS.tell() - fileStart <= Sec.offset &&
+              (OS.tell() - fileStart <= Sec.offset ||
+               Sec.offset == (uint32_t)0) &&
               "Wrote too much data somewhere, section offsets don't line up.");
           currOffset = OS.tell() - fileStart;
           if (currOffset < Sec.offset) {
@@ -378,6 +384,20 @@ Error MachOWriter::writeLinkEditData(raw
   return Error::success();
 }
 
+Error MachOWriter::writeDWARFData(raw_ostream &OS,
+                                  std::vector<MachOYAML::Section> &Sections) {
+  for(auto Section : Sections) {
+    ZeroToOffset(OS, Section.offset);
+    if (0 == strncmp(&Section.sectname[0], "__debug_str", 16)) {
+      for (auto Str : Obj.DWARF.DebugStrings) {
+        OS.write(Str.data(), Str.size());
+        OS.write('\0');
+      }
+    }
+  }
+  return Error::success();
+}
+
 Error MachOWriter::writeRebaseOpcodes(raw_ostream &OS) {
   MachOYAML::LinkEditData &LinkEdit = Obj.LinkEdit;
 




More information about the llvm-commits mailing list