Index: include/llvm/ObjectYAML/DWARFEmitter.h =================================================================== --- include/llvm/ObjectYAML/DWARFEmitter.h +++ include/llvm/ObjectYAML/DWARFEmitter.h @@ -1,5 +1,4 @@ -//===--- DWARFEmitter.h - -------------------------------------------*- C++ -//-*-===// +//===--- DWARFEmitter.h - ---------------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -39,9 +38,12 @@ void EmitDebugLine(llvm::raw_ostream &OS, const llvm::DWARFYAML::Data &DI); Expected>> -EmitDebugSections(StringRef YAMLString, +EmitDebugSections(StringRef YAMLString, bool ApplyFixups, bool IsLittleEndian = sys::IsLittleEndianHost); +StringMap> +EmitDebugSections(llvm::DWARFYAML::Data &DI, bool ApplyFixups); + } // namespace DWARFYAML } // namespace llvm Index: lib/ObjectYAML/DWARFEmitter.cpp =================================================================== --- lib/ObjectYAML/DWARFEmitter.cpp +++ lib/ObjectYAML/DWARFEmitter.cpp @@ -285,11 +285,48 @@ OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data); } +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, +DWARFYAML::EmitDebugSections(StringRef YAMLString, bool ApplyFixups, bool IsLittleEndian) { - StringMap> DebugSections; - yaml::Input YIn(YAMLString); DWARFYAML::Data DI; @@ -297,7 +334,18 @@ YIn >> DI; if (YIn.error()) return errorCodeToError(YIn.error()); + return EmitDebugSections(DI, ApplyFixups); +} + +StringMap> +DWARFYAML::EmitDebugSections(DWARFYAML::Data &DI, bool ApplyFixups) { + if (ApplyFixups) { + DIEFixupVisitor DIFixer(DI); + DIFixer.traverseDebugInfo(); + } + + StringMap> DebugSections; EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info", DebugSections); EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line", @@ -308,5 +356,5 @@ DebugSections); EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges", DebugSections); - return std::move(DebugSections); + return DebugSections; } Index: unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp =================================================================== --- unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp +++ unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp @@ -1173,7 +1173,7 @@ " Attributes:\n" "debug_info:\n" " - Length:\n" - " TotalLength: 9\n" + " TotalLength: 0\n" " Version: 4\n" " AbbrOffset: 0\n" " AddrSize: 8\n" @@ -1183,7 +1183,7 @@ " - AbbrCode: 0x00000000\n" " Values:\n"; - auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata)); + auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata), true); ASSERT_TRUE((bool)ErrOrSections); auto &DebugSections = *ErrOrSections;