From c0a6317d376c72c20638adefb4a4c177c1623e08 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Fri, 20 Apr 2018 12:33:49 +0000 Subject: [PATCH] [ObjectYAML] Add ability for DWARFYAML to calculate DIE lengths This patch adds the ability for the ObjectYAML DWARFEmitter to calculate the lengths of DIEs. This is accomplished by creating a DIEFixupVisitor class which traverses the DWARF DIEs to calculate and fix up the lengths in the Compile Unit header. The DIEFixupVisitor can be extended in the future to enable more complex fix ups which will enable simplified YAML string representations. This is also very useful when using the YAML format in unit tests because you no longer need to know the length of the compile unit when writing the YAML string. Differential commandeered from Chris Bieneman (beanz) Differential revision: https://reviews.llvm.org/D30666 llvm-svn: 330421 --- include/llvm/ObjectYAML/DWARFEmitter.h | 5 +- lib/ObjectYAML/DWARFEmitter.cpp | 52 +++++++++++++++++-- .../DebugInfo/DWARF/DWARFDebugInfoTest.cpp | 4 +- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/include/llvm/ObjectYAML/DWARFEmitter.h b/include/llvm/ObjectYAML/DWARFEmitter.h index 0d7d8b4efbd..6a761c95d68 100644 --- a/include/llvm/ObjectYAML/DWARFEmitter.h +++ b/include/llvm/ObjectYAML/DWARFEmitter.h @@ -39,11 +39,12 @@ void EmitDebugInfo(raw_ostream &OS, const Data &DI); void EmitDebugLine(raw_ostream &OS, const Data &DI); Expected>> -EmitDebugSections(StringRef YAMLString, +EmitDebugSections(StringRef YAMLString, bool ApplyFixups = false, bool IsLittleEndian = sys::IsLittleEndianHost); +StringMap> +EmitDebugSections(llvm::DWARFYAML::Data &DI, bool ApplyFixups); } // end namespace DWARFYAML - } // end namespace llvm #endif // LLVM_OBJECTYAML_DWARFEMITTER_H diff --git a/lib/ObjectYAML/DWARFEmitter.cpp b/lib/ObjectYAML/DWARFEmitter.cpp index c49c2249cdf..1135127eddc 100644 --- a/lib/ObjectYAML/DWARFEmitter.cpp +++ b/lib/ObjectYAML/DWARFEmitter.cpp @@ -149,7 +149,6 @@ protected: writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian); writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian); } - } void onStartDIE(const DWARFYAML::Unit &CU, @@ -308,11 +307,48 @@ EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc, OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data); } -Expected>> -DWARFYAML::EmitDebugSections(StringRef YAMLString, - bool IsLittleEndian) { - StringMap> DebugSections; +class DIEFixupVisitor : public DWARFYAML::Visitor { + uint64_t Length; +public: + DIEFixupVisitor(DWARFYAML::Data &DI) : DWARFYAML::Visitor(DI){}; + +private: + virtual void onStartCompileUnit(DWARFYAML::Unit &CU) { Length = 7; } + + virtual void onEndCompileUnit(DWARFYAML::Unit &CU) { + CU.Length.setLength(Length); + } + + virtual void onStartDIE(DWARFYAML::Unit &CU, DWARFYAML::Entry &DIE) { + Length += getULEB128Size(DIE.AbbrCode); + } + + virtual void onValue(const uint8_t U) { Length += 1; } + virtual void onValue(const uint16_t U) { Length += 2; } + virtual void onValue(const uint32_t U) { Length += 4; } + virtual void onValue(const uint64_t U, const bool LEB = false) { + if (LEB) + Length += getULEB128Size(U); + else + Length += 8; + } + virtual void onValue(const int64_t S, const bool LEB = false) { + if (LEB) + Length += getSLEB128Size(S); + else + Length += 8; + } + virtual void onValue(const StringRef String) { Length += String.size() + 1; } + + virtual void onValue(const MemoryBufferRef MBR) { + Length += MBR.getBufferSize(); + } +}; + +Expected>> +DWARFYAML::EmitDebugSections(StringRef YAMLString, bool ApplyFixups, + bool IsLittleEndian) { yaml::Input YIn(YAMLString); DWARFYAML::Data DI; @@ -321,6 +357,12 @@ DWARFYAML::EmitDebugSections(StringRef YAMLString, if (YIn.error()) return errorCodeToError(YIn.error()); + if (ApplyFixups) { + DIEFixupVisitor DIFixer(DI); + DIFixer.traverseDebugInfo(); + } + + StringMap> DebugSections; EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info", DebugSections); EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line", diff --git a/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp b/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp index a61ab7d3aff..27c361f7644 100644 --- a/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp +++ b/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp @@ -1166,7 +1166,7 @@ TEST(DWARFDebugInfo, TestEmptyChildren) { " Attributes:\n" "debug_info:\n" " - Length:\n" - " TotalLength: 9\n" + " TotalLength: 0\n" " Version: 4\n" " AbbrOffset: 0\n" " AddrSize: 8\n" @@ -1176,7 +1176,7 @@ TEST(DWARFDebugInfo, TestEmptyChildren) { " - AbbrCode: 0x00000000\n" " Values:\n"; - auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata)); + auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata), true); ASSERT_TRUE((bool)ErrOrSections); std::unique_ptr DwarfContext = DWARFContext::create(*ErrOrSections, 8);