[DWARFYAML] Make the debug_str section optional.
authorXing GUO <higuoxing@gmail.com>
Tue, 1 Sep 2020 01:57:03 +0000 (09:57 +0800)
committerXing GUO <higuoxing@gmail.com>
Tue, 1 Sep 2020 02:02:09 +0000 (10:02 +0800)
This patch makes the debug_str section optional. When the debug_str
section exists but doesn't contain anything, yaml2obj will emit a
section header for it.

Reviewed By: grimar

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

llvm/include/llvm/ObjectYAML/DWARFYAML.h
llvm/lib/ObjectYAML/DWARFEmitter.cpp
llvm/lib/ObjectYAML/DWARFYAML.cpp
llvm/test/ObjectYAML/MachO/DWARF-debug_str.yaml
llvm/test/tools/yaml2obj/ELF/DWARF/debug-str.yaml
llvm/tools/obj2yaml/dwarf2yaml.cpp

index d1bfb3e..19b7f35 100644 (file)
@@ -211,7 +211,7 @@ struct Data {
   bool IsLittleEndian;
   bool Is64BitAddrSize;
   std::vector<AbbrevTable> DebugAbbrev;
-  std::vector<StringRef> DebugStrings;
+  Optional<std::vector<StringRef>> DebugStrings;
   Optional<std::vector<StringOffsetsTable>> DebugStrOffsets;
   Optional<std::vector<ARange>> DebugAranges;
   std::vector<Ranges> DebugRanges;
index 8375efe..a0a445a 100644 (file)
@@ -86,7 +86,7 @@ static void writeDWARFOffset(uint64_t Offset, dwarf::DwarfFormat Format,
 }
 
 Error DWARFYAML::emitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
-  for (auto Str : DI.DebugStrings) {
+  for (StringRef Str : *DI.DebugStrings) {
     OS.write(Str.data(), Str.size());
     OS.write('\0');
   }
index 25ddf6b..046dddb 100644 (file)
@@ -24,7 +24,7 @@ bool DWARFYAML::Data::isEmpty() const {
 
 SetVector<StringRef> DWARFYAML::Data::getNonEmptySectionNames() const {
   SetVector<StringRef> SecNames;
-  if (!DebugStrings.empty())
+  if (DebugStrings)
     SecNames.insert("debug_str");
   if (DebugAranges)
     SecNames.insert("debug_aranges");
index 4089034..29247b3 100644 (file)
@@ -1,4 +1,6 @@
-# RUN: yaml2obj %s | obj2yaml | FileCheck %s
+## a) Test that obj2yaml is able to dump the __debug_str section.
+
+# RUN: yaml2obj --docnum=1 %s | obj2yaml | FileCheck %s
 
 --- !mach-o
 FileHeader:      
@@ -252,3 +254,70 @@ DWARF:
 #CHECK:     - int
 #CHECK:     - char
 
+## b) Test dumping an empty __debug_str section.
+
+# RUN: yaml2obj --docnum=2 -DCONTENT='[]' %s | obj2yaml | FileCheck %s --check-prefix=EMPTY-CONTENT
+
+#      EMPTY-CONTENT: Sections:
+# EMPTY-CONTENT-NEXT:   - sectname:  __debug_str
+# EMPTY-CONTENT-NEXT:     segname:   __DWARF
+# EMPTY-CONTENT-NEXT:     addr:      0x0000000000000000
+# EMPTY-CONTENT-NEXT:     size:      0
+# EMPTY-CONTENT-NEXT:     offset:    0x00000210
+# EMPTY-CONTENT-NEXT:     align:     0
+# EMPTY-CONTENT-NEXT:     reloff:    0x00000000
+# EMPTY-CONTENT-NEXT:     nreloc:    0
+# EMPTY-CONTENT-NEXT:     flags:     0x00000000
+# EMPTY-CONTENT-NEXT:     reserved1: 0x00000000
+# EMPTY-CONTENT-NEXT:     reserved2: 0x00000000
+# EMPTY-CONTENT-NEXT:     reserved3: 0x00000000
+# EMPTY-CONTENT-NEXT: DWARF:
+# EMPTY-CONTENT-NEXT:   debug_str: []
+
+--- !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_str
+        segname:   __DWARF
+        addr:      0x00
+        size:      [[SIZE=0]]
+        offset:    0x210
+        align:     0
+        reloff:    0x00000000
+        nreloc:    0
+        flags:     0x00000000
+        reserved1: 0x00000000
+        reserved2: 0x00000000
+        reserved3: 0x00000000
+DWARF:
+  debug_str:
+    [[CONTENT]]
+
+## c) Test generating and dumping a __debug_str section who only has an empty string.
+
+# RUN: yaml2obj --docnum=2 -DCONTENT='[ "" ]' -DSIZE=1 %s | obj2yaml | FileCheck %s --check-prefix=EMPTY-STRING
+
+#      EMPTY-STRING: DWARF:
+# EMPTY-STRING-NEXT:   debug_str:
+# EMPTY-STRING-NEXT:     - ''
+# EMPTY-STRING-NEXT: ...
index c263563..27d0aa6 100644 (file)
 ## Check the default sh_type, sh_entsize, sh_info, sh_flags and sh_addralign of the
 ## .debug_str section header.
 
-# RUN: llvm-readelf -S %t1.o | FileCheck %s --check-prefix=SHDRS-DEFAULT
+# RUN: llvm-readelf -S %t1.o | FileCheck -DSIZE=000006 %s --check-prefix=SHDRS-DEFAULT
 
-# SHDRS-DEFAULT: Name       Type     Address          Off    Size   ES Flg Lk Inf Al
-# SHDRS-DEFAULT: .debug_str PROGBITS 0000000000000000 000040 000006 01 MS  0  0   1
+# SHDRS-DEFAULT: Name       Type     Address          Off    Size     ES Flg Lk Inf Al
+# SHDRS-DEFAULT: .debug_str PROGBITS 0000000000000000 000040 [[SIZE]] 01 MS  0  0   1
 
 --- !ELF
 FileHeader:
@@ -213,3 +213,17 @@ Sections:
 DWARF:
   debug_str:
     - a
+
+## i) Test that the .debug_str section header is emitted if the "debug_str"
+## entry is empty.
+
+# RUN: yaml2obj --docnum=10 %s -o %t10.o
+# RUN: llvm-readelf -S %t10.o | FileCheck -DSIZE=000000 %s --check-prefix=SHDRS-DEFAULT
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data:  ELFDATA2LSB
+  Type:  ET_EXEC
+DWARF:
+  debug_str: []
index 13e502d..513fa0f 100644 (file)
@@ -48,10 +48,11 @@ void dumpDebugAbbrev(DWARFContext &DCtx, DWARFYAML::Data &Y) {
 
 void dumpDebugStrings(DWARFContext &DCtx, DWARFYAML::Data &Y) {
   StringRef RemainingTable = DCtx.getDWARFObj().getStrSection();
+  Y.DebugStrings.emplace();
   while (RemainingTable.size() > 0) {
     auto SymbolPair = RemainingTable.split('\0');
     RemainingTable = SymbolPair.second;
-    Y.DebugStrings.push_back(SymbolPair.first);
+    Y.DebugStrings->push_back(SymbolPair.first);
   }
 }