diff --git a/llvm/include/llvm/ObjectYAML/DWARFYAML.h b/llvm/include/llvm/ObjectYAML/DWARFYAML.h --- a/llvm/include/llvm/ObjectYAML/DWARFYAML.h +++ b/llvm/include/llvm/ObjectYAML/DWARFYAML.h @@ -211,7 +211,7 @@ bool IsLittleEndian; bool Is64BitAddrSize; std::vector DebugAbbrev; - std::vector DebugStrings; + Optional> DebugStrings; Optional> DebugStrOffsets; Optional> DebugAranges; std::vector DebugRanges; diff --git a/llvm/lib/ObjectYAML/DWARFEmitter.cpp b/llvm/lib/ObjectYAML/DWARFEmitter.cpp --- a/llvm/lib/ObjectYAML/DWARFEmitter.cpp +++ b/llvm/lib/ObjectYAML/DWARFEmitter.cpp @@ -86,7 +86,7 @@ } 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'); } diff --git a/llvm/lib/ObjectYAML/DWARFYAML.cpp b/llvm/lib/ObjectYAML/DWARFYAML.cpp --- a/llvm/lib/ObjectYAML/DWARFYAML.cpp +++ b/llvm/lib/ObjectYAML/DWARFYAML.cpp @@ -24,7 +24,7 @@ SetVector DWARFYAML::Data::getNonEmptySectionNames() const { SetVector SecNames; - if (!DebugStrings.empty()) + if (DebugStrings) SecNames.insert("debug_str"); if (DebugAranges) SecNames.insert("debug_aranges"); diff --git a/llvm/test/ObjectYAML/MachO/DWARF-debug_str.yaml b/llvm/test/ObjectYAML/MachO/DWARF-debug_str.yaml --- a/llvm/test/ObjectYAML/MachO/DWARF-debug_str.yaml +++ b/llvm/test/ObjectYAML/MachO/DWARF-debug_str.yaml @@ -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 @@ #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: ... diff --git a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-str.yaml b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-str.yaml --- a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-str.yaml +++ b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-str.yaml @@ -13,10 +13,10 @@ ## 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 @@ 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: [] diff --git a/llvm/tools/obj2yaml/dwarf2yaml.cpp b/llvm/tools/obj2yaml/dwarf2yaml.cpp --- a/llvm/tools/obj2yaml/dwarf2yaml.cpp +++ b/llvm/tools/obj2yaml/dwarf2yaml.cpp @@ -48,10 +48,11 @@ 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); } }