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 @@ -775,23 +775,40 @@ return createStringError(YIn.error(), GeneratedDiag.getMessage()); StringMap> DebugSections; - Error Err = emitDebugSectionImpl(DI, &DWARFYAML::emitDebugInfo, "debug_info", - DebugSections); - Err = joinErrors(std::move(Err), - emitDebugSectionImpl(DI, &DWARFYAML::emitDebugLine, - "debug_line", DebugSections)); - Err = joinErrors(std::move(Err), - emitDebugSectionImpl(DI, &DWARFYAML::emitDebugStr, - "debug_str", DebugSections)); - Err = joinErrors(std::move(Err), - emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAbbrev, - "debug_abbrev", DebugSections)); - Err = joinErrors(std::move(Err), - emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAranges, - "debug_aranges", DebugSections)); - Err = joinErrors(std::move(Err), - emitDebugSectionImpl(DI, &DWARFYAML::emitDebugRanges, - "debug_ranges", DebugSections)); + + Error Err = Error::success(); + cantFail(std::move(Err)); + + for (StringRef SecName : DI.getNonEmptySectionNames()) { + if (SecName == "debug_abbrev") + Err = joinErrors(std::move(Err), + emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAbbrev, + SecName, DebugSections)); + else if (SecName == "debug_aranges") + Err = joinErrors(std::move(Err), + emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAranges, + SecName, DebugSections)); + else if (SecName == "debug_info") + Err = joinErrors(std::move(Err), + emitDebugSectionImpl(DI, &DWARFYAML::emitDebugInfo, + SecName, DebugSections)); + else if (SecName == "debug_line") + Err = joinErrors(std::move(Err), + emitDebugSectionImpl(DI, &DWARFYAML::emitDebugLine, + SecName, DebugSections)); + else if (SecName == "debug_ranges") + Err = joinErrors(std::move(Err), + emitDebugSectionImpl(DI, &DWARFYAML::emitDebugRanges, + SecName, DebugSections)); + else if (SecName == "debug_str") + Err = joinErrors(std::move(Err), + emitDebugSectionImpl(DI, &DWARFYAML::emitDebugStr, + SecName, DebugSections)); + else + Err = joinErrors(std::move(Err), + createStringError(errc::not_supported, + SecName + " is not supported")); + } if (Err) return std::move(Err); diff --git a/llvm/unittests/ObjectYAML/DWARFYAMLTest.cpp b/llvm/unittests/ObjectYAML/DWARFYAMLTest.cpp --- a/llvm/unittests/ObjectYAML/DWARFYAMLTest.cpp +++ b/llvm/unittests/ObjectYAML/DWARFYAMLTest.cpp @@ -201,3 +201,12 @@ EXPECT_THAT_ERROR(parseDWARFYAML(Yaml, Data), FailedWithMessage("missing required key 'Descriptor'")); } + +TEST(EmitDebugSections, UnsupportedSection) { + StringRef Yaml = R"( +debug_rnglists: [] +)"; + auto SectionsOrErr = DWARFYAML::emitDebugSections(Yaml); + EXPECT_THAT_ERROR(SectionsOrErr.takeError(), + FailedWithMessage("debug_rnglists is not supported")); +}