[llvm] 3cda698 - [obj2yaml] Stop parsing the debug_str section when it encounters a string without the null terminator.

Xing GUO via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 8 01:10:38 PDT 2020


Author: Xing GUO
Date: 2020-09-08T16:09:36+08:00
New Revision: 3cda69872362526b1672ae23de4ac968b7564c2b

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

LOG: [obj2yaml] Stop parsing the debug_str section when it encounters a string without the null terminator.

When obj2yaml encounters a string without the null terminator, it should
stop parsing the debug_str section. This patch addresses comments in
[D86867](https://reviews.llvm.org/D86867#inline-803291).

Reviewed By: jhenderson

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

Added: 
    

Modified: 
    llvm/test/ObjectYAML/MachO/DWARF-debug_str.yaml
    llvm/test/tools/obj2yaml/ELF/DWARF/debug-str.yaml
    llvm/tools/obj2yaml/dwarf2yaml.cpp
    llvm/tools/obj2yaml/elf2yaml.cpp
    llvm/tools/obj2yaml/macho2yaml.cpp
    llvm/tools/obj2yaml/obj2yaml.h

Removed: 
    


################################################################################
diff  --git a/llvm/test/ObjectYAML/MachO/DWARF-debug_str.yaml b/llvm/test/ObjectYAML/MachO/DWARF-debug_str.yaml
index 29247b334a1a..9bb55ea35091 100644
--- a/llvm/test/ObjectYAML/MachO/DWARF-debug_str.yaml
+++ b/llvm/test/ObjectYAML/MachO/DWARF-debug_str.yaml
@@ -321,3 +321,61 @@ DWARF:
 # EMPTY-STRING-NEXT:   debug_str:
 # EMPTY-STRING-NEXT:     - ''
 # EMPTY-STRING-NEXT: ...
+
+## d) Test generating and dumping a __debug_str section which contains a string without a null terminator.
+
+# RUN: yaml2obj --docnum=3 %s | obj2yaml | FileCheck %s --check-prefix=NO-TERMINATOR
+
+#  NO-TERMINATOR-NOT: DWARF:
+#      NO-TERMINATOR: Sections:
+# NO-TERMINATOR-NEXT:   - sectname:  __debug_str
+# NO-TERMINATOR-NEXT:     segname:   __DWARF
+# NO-TERMINATOR-NEXT:     addr:      0x0000000000000000
+# NO-TERMINATOR-NEXT:     size:      7
+# NO-TERMINATOR-NEXT:     offset:    0x00000210
+# NO-TERMINATOR-NEXT:     align:     0
+# NO-TERMINATOR-NEXT:     reloff:    0x00000000
+# NO-TERMINATOR-NEXT:     nreloc:    0
+# NO-TERMINATOR-NEXT:     flags:     0x00000000
+# NO-TERMINATOR-NEXT:     reserved1: 0x00000000
+# NO-TERMINATOR-NEXT:     reserved2: 0x00000000
+# NO-TERMINATOR-NEXT:     reserved3: 0x00000000
+# NO-TERMINATOR-NEXT:     content:   '61626300616263'
+# NO-TERMINATOR-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_str
+        segname:   __DWARF
+        addr:      0x00
+        size:      7
+        offset:    0x210
+        align:     0
+        reloff:    0x00000000
+        nreloc:    0
+        flags:     0x00000000
+        reserved1: 0x00000000
+        reserved2: 0x00000000
+        reserved3: 0x00000000
+        content:   '61626300616263' ## "abc\0abc"

diff  --git a/llvm/test/tools/obj2yaml/ELF/DWARF/debug-str.yaml b/llvm/test/tools/obj2yaml/ELF/DWARF/debug-str.yaml
index e05864287724..76c1c5c1b365 100644
--- a/llvm/test/tools/obj2yaml/ELF/DWARF/debug-str.yaml
+++ b/llvm/test/tools/obj2yaml/ELF/DWARF/debug-str.yaml
@@ -99,3 +99,27 @@ FileHeader:
   Type:  ET_EXEC
 DWARF:
   debug_str: []
+
+## d) Test that yaml2obj stops parsing the .debug_str section if it encounters a
+## string without a null terminator. The output uses a raw content section instead of
+## the DWARF tag to represent the broken .debug_str section.
+
+# RUN: yaml2obj --docnum=3 %s | obj2yaml | FileCheck %s --check-prefix=NO-TERMINATOR
+
+#  NO-TERMINATOR-NOT: DWARF:
+#      NO-TERMINATOR: Sections:
+# NO-TERMINATOR-NEXT:   - Name:    .debug_str
+# NO-TERMINATOR-NEXT:     Type:    SHT_PROGBITS
+# NO-TERMINATOR-NEXT:     Flags:   [ SHF_MERGE, SHF_STRINGS ]
+# NO-TERMINATOR-NEXT:     Content: '61626300616263'
+# NO-TERMINATOR-NEXT: ...
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data:  ELFDATA2LSB
+  Type:  ET_EXEC
+Sections:
+  - Name:    .debug_str
+    Type:    SHT_PROGBITS
+    Content: "61626300616263" ## "abc\0abc"

diff  --git a/llvm/tools/obj2yaml/dwarf2yaml.cpp b/llvm/tools/obj2yaml/dwarf2yaml.cpp
index 513fa0fdef01..cef7b699805c 100644
--- a/llvm/tools/obj2yaml/dwarf2yaml.cpp
+++ b/llvm/tools/obj2yaml/dwarf2yaml.cpp
@@ -46,14 +46,20 @@ 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);
+Error dumpDebugStrings(DWARFContext &DCtx, DWARFYAML::Data &Y) {
+  DataExtractor StrData = DCtx.getStringExtractor();
+  uint64_t Offset = 0;
+  std::vector<StringRef> DebugStr;
+  Error Err = Error::success();
+  while (StrData.isValidOffset(Offset)) {
+    const char *CStr = StrData.getCStr(&Offset, &Err);
+    if (Err)
+      return Err;
+    DebugStr.push_back(CStr);
   }
+
+  Y.DebugStrings = DebugStr;
+  return Err;
 }
 
 Error dumpDebugARanges(DWARFContext &DCtx, DWARFYAML::Data &Y) {

diff  --git a/llvm/tools/obj2yaml/elf2yaml.cpp b/llvm/tools/obj2yaml/elf2yaml.cpp
index 9f524479bb04..264bc4d1dbf3 100644
--- a/llvm/tools/obj2yaml/elf2yaml.cpp
+++ b/llvm/tools/obj2yaml/elf2yaml.cpp
@@ -415,7 +415,7 @@ Optional<DWARFYAML::Data> ELFDumper<ELFT>::dumpDWARFSections(
       if (RawSec->Name == ".debug_aranges")
         Err = dumpDebugARanges(*DWARFCtx.get(), DWARF);
       else if (RawSec->Name == ".debug_str")
-        dumpDebugStrings(*DWARFCtx.get(), DWARF);
+        Err = dumpDebugStrings(*DWARFCtx.get(), DWARF);
 
       // If the DWARF section cannot be successfully parsed, emit raw content
       // instead of an entry in the DWARF section of the YAML.

diff  --git a/llvm/tools/obj2yaml/macho2yaml.cpp b/llvm/tools/obj2yaml/macho2yaml.cpp
index 3a93d5c6846b..49347431b9a4 100644
--- a/llvm/tools/obj2yaml/macho2yaml.cpp
+++ b/llvm/tools/obj2yaml/macho2yaml.cpp
@@ -154,10 +154,8 @@ static Error dumpDebugSection(StringRef SecName, DWARFContext &DCtx,
   }
   if (SecName == "__debug_ranges")
     return dumpDebugRanges(DCtx, DWARF);
-  if (SecName == "__debug_str") {
-    dumpDebugStrings(DCtx, DWARF);
-    return Error::success();
-  }
+  if (SecName == "__debug_str")
+    return dumpDebugStrings(DCtx, DWARF);
   return createStringError(errc::not_supported,
                            "dumping " + SecName + " section is not supported");
 }

diff  --git a/llvm/tools/obj2yaml/obj2yaml.h b/llvm/tools/obj2yaml/obj2yaml.h
index 85a7ac9a4787..66a2d2753622 100644
--- a/llvm/tools/obj2yaml/obj2yaml.h
+++ b/llvm/tools/obj2yaml/obj2yaml.h
@@ -47,6 +47,7 @@ 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);
+llvm::Error dumpDebugStrings(llvm::DWARFContext &DCtx,
+                             llvm::DWARFYAML::Data &Y);
 
 #endif


        


More information about the llvm-commits mailing list