Index: llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h =================================================================== --- llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h +++ llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h @@ -72,6 +72,7 @@ DWARFUnitVector DWOUnits; std::unique_ptr AbbrevDWO; std::unique_ptr MacinfoDWO; + std::unique_ptr MacroDWO; /// The maximum DWARF version of all units. unsigned MaxVersion = 0; @@ -110,8 +111,8 @@ enum MacroSecType { MacinfoSection, MacinfoDwoSection, - MacroSection - // FIXME: Add support for.debug_macro.dwo section. + MacroSection, + MacroDwoSection }; public: @@ -291,6 +292,9 @@ /// Get a pointer to the parsed DebugMacro information object. const DWARFDebugMacro *getDebugMacro(); + /// Get a pointer to the parsed DebugMacroDWO information object. + const DWARFDebugMacro *getDebugMacroDWO(); + /// Get a reference to the parsed accelerator table object. const DWARFDebugNames &getDebugNames(); @@ -316,9 +320,24 @@ getLineTableForUnit(DWARFUnit *U, function_ref RecoverableErrorHandler); + DWARFDataExtractor getStringOffsetsExtractor() const { + return DWARFDataExtractor(*DObj, DObj->getStrOffsetsSection(), + isLittleEndian(), 0); + } + + DWARFDataExtractor getStringOffsetsDWOExtractor() const { + return DWARFDataExtractor(*DObj, DObj->getStrOffsetsDWOSection(), + isLittleEndian(), 0); + } + + DataExtractor getStringDWOExtractor() const { + return DataExtractor(DObj->getStrDWOSection(), false, 0); + } + DataExtractor getStringExtractor() const { return DataExtractor(DObj->getStrSection(), false, 0); } + DataExtractor getLineStringExtractor() const { return DataExtractor(DObj->getLineStrSection(), false, 0); } Index: llvm/include/llvm/DebugInfo/DWARF/DWARFDebugMacro.h =================================================================== --- llvm/include/llvm/DebugInfo/DWARF/DWARFDebugMacro.h +++ llvm/include/llvm/DebugInfo/DWARF/DWARFDebugMacro.h @@ -9,6 +9,7 @@ #ifndef LLVM_DEBUGINFO_DWARF_DWARFDEBUGMACRO_H #define LLVM_DEBUGINFO_DWARF_DWARFDEBUGMACRO_H +#include "llvm//DebugInfo/DWARF/DWARFUnit.h" #include "llvm/ADT/SmallVector.h" #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" #include "llvm/Support/Errc.h" @@ -103,7 +104,9 @@ /// Parse the debug_macinfo/debug_macro section accessible via the 'Data' /// parameter. - Error parse(DataExtractor StringExtractor, DWARFDataExtractor Data, + Error parse(DWARFUnitVector::iterator_range CUs, + DWARFDataExtractor StringOffsetsExtractor, + DataExtractor StringExtractor, DWARFDataExtractor Data, bool IsMacro); /// Return whether the section has any entries. Index: llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h =================================================================== --- llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h +++ llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h @@ -48,6 +48,7 @@ virtual const DWARFSection &getRangesSection() const { return Dummy; } virtual const DWARFSection &getRnglistsSection() const { return Dummy; } virtual const DWARFSection &getMacroSection() const { return Dummy; } + virtual StringRef getMacroDWOSection() const { return ""; } virtual StringRef getMacinfoSection() const { return ""; } virtual StringRef getMacinfoDWOSection() const { return ""; } virtual const DWARFSection &getPubnamesSection() const { return Dummy; } Index: llvm/include/llvm/MC/MCObjectFileInfo.h =================================================================== --- llvm/include/llvm/MC/MCObjectFileInfo.h +++ llvm/include/llvm/MC/MCObjectFileInfo.h @@ -113,6 +113,7 @@ MCSection *DwarfLocDWOSection = nullptr; MCSection *DwarfStrOffDWOSection = nullptr; MCSection *DwarfMacinfoDWOSection = nullptr; + MCSection *DwarfMacroDWOSection = nullptr; /// The DWARF v5 string offset and address table sections. MCSection *DwarfStrOffSection = nullptr; Index: llvm/lib/DebugInfo/DWARF/DWARFContext.cpp =================================================================== --- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -296,7 +296,13 @@ DWARFContext::parseMacroOrMacinfo(MacroSecType SectionType) { auto Macro = std::make_unique(); auto ParseAndDump = [&](DWARFDataExtractor &Data, bool IsMacro) { - if (Error Err = Macro->parse(getStringExtractor(), Data, IsMacro)) { + if (Error Err = Macro->parse( + compile_units(), + SectionType != MacroDwoSection ? getStringOffsetsExtractor() + : getStringOffsetsDWOExtractor(), + SectionType != MacroDwoSection ? getStringExtractor() + : getStringDWOExtractor(), + Data, IsMacro)) { RecoverableErrorHandler(std::move(Err)); Macro = nullptr; } @@ -318,6 +324,11 @@ ParseAndDump(Data, /*IsMacro=*/true); break; } + case MacroDwoSection: { + DWARFDataExtractor Data(DObj->getMacroDWOSection(), isLittleEndian(), 0); + ParseAndDump(Data, /*IsMacro=*/true); + break; + } } return Macro; } @@ -480,6 +491,12 @@ Macro->dump(OS); } + if (shouldDump(Explicit, ".debug_macro.dwo", DIDT_ID_DebugMacro, + DObj->getMacroDWOSection())) { + if (auto MacroDWO = getDebugMacroDWO()) + MacroDWO->dump(OS); + } + if (shouldDump(Explicit, ".debug_macinfo", DIDT_ID_DebugMacro, DObj->getMacinfoSection())) { if (auto Macinfo = getDebugMacinfo()) @@ -859,6 +876,12 @@ return Macro.get(); } +const DWARFDebugMacro *DWARFContext::getDebugMacroDWO() { + if (!MacroDWO) + MacroDWO = parseMacroOrMacinfo(MacroDwoSection); + return MacroDWO.get(); +} + const DWARFDebugMacro *DWARFContext::getDebugMacinfo() { if (!Macinfo) Macinfo = parseMacroOrMacinfo(MacinfoSection); @@ -1548,6 +1571,7 @@ StringRef StrSection; StringRef MacinfoSection; StringRef MacinfoDWOSection; + StringRef MacroDWOSection; StringRef AbbrevDWOSection; StringRef StrDWOSection; StringRef CUIndexSection; @@ -1568,6 +1592,7 @@ .Case("debug_str", &StrSection) .Case("debug_macinfo", &MacinfoSection) .Case("debug_macinfo.dwo", &MacinfoDWOSection) + .Case("debug_macro.dwo", &MacroDWOSection) .Case("debug_abbrev.dwo", &AbbrevDWOSection) .Case("debug_str.dwo", &StrDWOSection) .Case("debug_cu_index", &CUIndexSection) @@ -1886,6 +1911,7 @@ return RnglistsSection; } const DWARFSection &getMacroSection() const override { return MacroSection; } + StringRef getMacroDWOSection() const override { return MacroDWOSection; } StringRef getMacinfoSection() const override { return MacinfoSection; } StringRef getMacinfoDWOSection() const override { return MacinfoDWOSection; } const DWARFSection &getPubnamesSection() const override { return PubnamesSection; } Index: llvm/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp =================================================================== --- llvm/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp +++ llvm/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp @@ -62,6 +62,8 @@ case DW_MACRO_undef: case DW_MACRO_define_strp: case DW_MACRO_undef_strp: + case DW_MACRO_define_strx: + case DW_MACRO_undef_strx: OS << " - lineno: " << E.Line; OS << " macro: " << E.MacroStr; break; @@ -84,7 +86,9 @@ } } -Error DWARFDebugMacro::parse(DataExtractor StringExtractor, +Error DWARFDebugMacro::parse(DWARFUnitVector::iterator_range CUs, + DWARFDataExtractor StringOffsetsExtractor, + DataExtractor StringExtractor, DWARFDataExtractor Data, bool IsMacro) { uint64_t Offset = 0; MacroList *M = nullptr; @@ -111,6 +115,21 @@ continue; } + auto GetStrOffsetsBase = [&]() { + for (auto &CU : CUs) + if (auto CUDIE = CU->getUnitDIE()) + if (M->Offset == toSectionOffset(CUDIE.find(DW_AT_macros))) + if (toSectionOffset(CUDIE.find(DW_AT_str_offsets_base))) + return *toSectionOffset(CUDIE.find(DW_AT_str_offsets_base)); + // Since we didn't find DW_AT_str_offsets_base we must be parsing + // a dwo object. In that case string offset table offset table is located + // beneath the header of debug_str_offsets section. + // Header size is 8 bytes and 16 bytes for DWARF32 and DWARF64 + // respectively. + + // FIXME: Add support for DWARF64 + return (uint64_t)8; + }; switch (E.Type) { default: // Got a corrupted ".debug_macinfo" section (invalid macinfo type). @@ -141,6 +160,21 @@ E.MacroStr = StringExtractor.getCStr(&StrOffset); break; } + case DW_MACRO_define_strx: + case DW_MACRO_undef_strx: { + uint64_t StrOffsetBases = GetStrOffsetsBase(); + + E.Line = Data.getULEB128(&Offset); + uint64_t OffsetIndex = Data.getULEB128(&Offset); + // Offset entry size is 4 and 8 bytes for DWARF32 and DWARF64 + // respectively. + // FIXME: Add support for DWARF64 + uint64_t IndexOffset = StrOffsetBases + OffsetIndex * /*EntrySize=*/4; + uint64_t StrOffset = StringOffsetsExtractor.getRelocatedValue( + /*OffsetSize=*/4, &IndexOffset); + E.MacroStr = StringExtractor.getCStr(&StrOffset); + break; + } case DW_MACRO_start_file: // 2. Source line E.Line = Data.getULEB128(&Offset); Index: llvm/lib/MC/MCObjectFileInfo.cpp =================================================================== --- llvm/lib/MC/MCObjectFileInfo.cpp +++ llvm/lib/MC/MCObjectFileInfo.cpp @@ -473,6 +473,8 @@ Ctx->getELFSection(".debug_rnglists.dwo", DebugSecType, ELF::SHF_EXCLUDE); DwarfMacinfoDWOSection = Ctx->getELFSection(".debug_macinfo.dwo", DebugSecType, ELF::SHF_EXCLUDE); + DwarfMacroDWOSection = + Ctx->getELFSection(".debug_macro.dwo", DebugSecType, ELF::SHF_EXCLUDE); DwarfLoclistsDWOSection = Ctx->getELFSection(".debug_loclists.dwo", DebugSecType, ELF::SHF_EXCLUDE); @@ -649,6 +651,11 @@ COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata(), "debug_macinfo.dwo"); + DwarfMacroDWOSection = Ctx->getCOFFSection( + ".debug_macro.dwo", + COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata(), "debug_macro.dwo"); DwarfInfoDWOSection = Ctx->getCOFFSection( ".debug_info.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | Index: llvm/test/DebugInfo/X86/debug-macro-dwo.s =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/debug-macro-dwo.s @@ -0,0 +1,87 @@ +## This test checks that llvm-dwarfdump can dump debug_macro.dwo +## section present in a dwo object. + +# RUN: llvm-mc -triple x86_64-unknown-linux -filetype=obj %s -o -| \ +# RUN: llvm-dwarfdump -debug-macro - | FileCheck -strict-whitespace -match-full-lines %s + +# CHECK:.debug_macro.dwo contents: +# CHECK-NEXT:0x00000000: +# CHECK-NEXT:macro header: version = 0x0005, flags = 0x02, debug_line_offset = 0x0000 +# CHECK-NEXT:DW_MACRO_start_file - lineno: 0 filenum: 0 +# CHECK-NEXT: DW_MACRO_define_strx - lineno: 1 macro: DWARF_VERSION 5 +# CHECK-NEXT: DW_MACRO_undef_strx - lineno: 4 macro: DWARF_VERSION +# CHECK-NEXT:DW_MACRO_end_file + + .section .debug_macro.dwo,"e",@progbits +.Lcu_macro_begin0: + .short 5 # Macro information version + .byte 2 # Flags: 32 bit, debug_line_offset present + .long 0 # debug_line_offset + .byte 3 # DW_MACRO_start_file + .byte 0 # Line Number + .byte 0 # File Number + .byte 11 # DW_MACRO_define_strx + .byte 1 # Line Number + .byte 5 # Macro String Index + .byte 12 # DW_MACRO_undef_strx + .byte 4 # Line Number + .byte 6 # Macro String Index + .byte 4 # DW_MACRO_end_file + .byte 0 # End Of Macro List Mark + + .section .debug_str_offsets.dwo,"e",@progbits + .long 28 + .short 5 + .short 0 + .section .debug_str.dwo,"eMS",@progbits,1 +.Linfo_string0: + .asciz "main" # string offset=0 +.Linfo_string1: + .asciz "int" # string offset=5 +.Linfo_string2: + .asciz "clang version 11.0.0 " # string offset=9 +.Linfo_string3: + .asciz "macro.c" # string offset=31 +.Linfo_string4: + .asciz "macro.dwo" # string offset=39 +.Linfo_string5: + .asciz "DWARF_VERSION 5" # string offset=49 +.Linfo_string6: + .asciz "DWARF_VERSION" # string offset=65 + .section .debug_str_offsets.dwo,"e",@progbits + .long 0 + .long 5 + .long 9 + .long 31 + .long 39 + .long 49 + .long 65 + .section .debug_info.dwo,"e",@progbits + .long .Ldebug_info_dwo_end0-.Ldebug_info_dwo_start0 # Length of Unit +.Ldebug_info_dwo_start0: + .short 5 # DWARF version number + .byte 5 # DWARF Unit Type + .byte 8 # Address Size (in bytes) + .long 0 # Offset Into Abbrev. Section + .quad 1536875774479801980 + .byte 1 # Abbrev [1] 0x14:0x1a DW_TAG_compile_unit + .byte 2 # DW_AT_producer + .short 12 # DW_AT_language + .byte 3 # DW_AT_name + .byte 4 # DW_AT_dwo_name + .byte 0 # End Of Children Mark +.Ldebug_info_dwo_end0: + .section .debug_abbrev.dwo,"e",@progbits + .byte 1 # Abbreviation Code + .byte 17 # DW_TAG_compile_unit + .byte 1 # DW_CHILDREN_yes + .byte 37 # DW_AT_producer + .byte 37 # DW_FORM_strx1 + .byte 19 # DW_AT_language + .byte 5 # DW_FORM_data2 + .byte 3 # DW_AT_name + .byte 37 # DW_FORM_strx1 + .byte 118 # DW_AT_dwo_name + .byte 37 # DW_FORM_strx1 + .byte 0 # EOM(1) + .byte 0 # EOM(2) Index: llvm/test/DebugInfo/X86/debug-macro-multi-cu-strx.s =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/debug-macro-multi-cu-strx.s @@ -0,0 +1,193 @@ +## This test checks that llvm-dwarfdump can dump debug_macro +## section containing contributions from multiple CU's represented +## using DW_MACRO_define_strx form. + +# RUN: llvm-mc -triple x86_64-unknown-linux -filetype=obj %s -o -| \ +# RUN: llvm-dwarfdump -debug-macro - | FileCheck -strict-whitespace -match-full-lines %s + +# CHECK:.debug_macro contents: +# CHECK-NEXT:0x00000000: +# CHECK-NEXT:macro header: version = 0x0005, flags = 0x02, debug_line_offset = 0x0000 +# CHECK-NEXT:DW_MACRO_start_file - lineno: 0 filenum: 0 +# CHECK-NEXT: DW_MACRO_define_strx - lineno: 1 macro: DWARF_VERSION 5 +# CHECK-NEXT: DW_MACRO_define_strx - lineno: 2 macro: COMPILE_UNIT 1 +# CHECK-NEXT: DW_MACRO_undef_strx - lineno: 3 macro: COMPILE_UNIT +# CHECK-NEXT:DW_MACRO_end_file + +# CHECK:0x00000015: +# CHECK-NEXT:macro header: version = 0x0005, flags = 0x02, debug_line_offset = 0x0000 +# CHECK-NEXT:DW_MACRO_start_file - lineno: 1 filenum: 3 +# CHECK-NEXT: DW_MACRO_define_strx - lineno: 2 macro: COMPILE_UNIT 2 +# CHECK-NEXT: DW_MACRO_undef_strx - lineno: 3 macro: COMPILE_UNIT +# CHECK-NEXT:DW_MACRO_end_file + + .text + .file "cu1.c" + .section .debug_abbrev,"",@progbits + .byte 1 # Abbreviation Code + .byte 17 # DW_TAG_compile_unit + .byte 0 # DW_CHILDREN_no + .byte 37 # DW_AT_producer + .byte 37 # DW_FORM_strx1 + .byte 19 # DW_AT_language + .byte 5 # DW_FORM_data2 + .byte 3 # DW_AT_name + .byte 37 # DW_FORM_strx1 + .byte 114 # DW_AT_str_offsets_base + .byte 23 # DW_FORM_sec_offset + .byte 16 # DW_AT_stmt_list + .byte 23 # DW_FORM_sec_offset + .byte 27 # DW_AT_comp_dir + .byte 37 # DW_FORM_strx1 + .byte 121 # DW_AT_macros + .byte 23 # DW_FORM_sec_offset + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 0 # EOM(3) + .section .debug_info,"",@progbits +.Lcu_begin0: + .long .Ldebug_info_end0-.Ldebug_info_start0 # Length of Unit +.Ldebug_info_start0: + .short 5 # DWARF version number + .byte 1 # DWARF Unit Type + .byte 8 # Address Size (in bytes) + .long .debug_abbrev # Offset Into Abbrev. Section + .byte 1 # Abbrev [1] 0xc:0x12 DW_TAG_compile_unit + .byte 0 # DW_AT_producer + .short 12 # DW_AT_language + .byte 1 # DW_AT_name + .long .Lstr_offsets_base0 # DW_AT_str_offsets_base + .long .Lline_table_start0 # DW_AT_stmt_list + .byte 2 # DW_AT_comp_dir + .long .Lcu_macro_begin0 # DW_AT_macros +.Ldebug_info_end0: + .section .debug_macro,"",@progbits +.Lcu_macro_begin0: + .short 5 # Macro information version + .byte 2 # Flags: 32 bit, debug_line_offset present + .long .Lline_table_start0 # debug_line_offset + .byte 3 # DW_MACRO_start_file + .byte 0 # Line Number + .byte 0 # File Number + .byte 11 # DW_MACRO_define_strx + .byte 1 # Line Number + .byte 3 # Macro String + .byte 11 # DW_MACRO_define_strx + .byte 2 # Line Number + .byte 4 # Macro String + .byte 12 # DW_MACRO_undef_strx + .byte 3 # Line Number + .byte 5 # Macro String + .byte 4 # DW_MACRO_end_file + .byte 0 # End Of Macro List Mark + .section .debug_str_offsets,"",@progbits + .long 28 + .short 5 + .short 0 +.Lstr_offsets_base0: + .section .debug_str,"MS",@progbits,1 +.Linfo_string0: + .asciz "clang version 11.0.0" # string offset=0 +.Linfo_string1: + .asciz "cu1.c" # string offset=105 +.Linfo_string2: + .asciz "/" # string offset=111 +.Linfo_string3: + .asciz "DWARF_VERSION 5" # string offset=134 +.Linfo_string4: + .asciz "COMPILE_UNIT 1" # string offset=150 +.Linfo_string5: + .asciz "COMPILE_UNIT" # string offset=165 + .section .debug_str_offsets,"",@progbits + .long .Linfo_string0 + .long .Linfo_string1 + .long .Linfo_string2 + .long .Linfo_string3 + .long .Linfo_string4 + .long .Linfo_string5 + .ident "clang version 11.0.0" + .section ".note.GNU-stack","",@progbits + .addrsig + .section .debug_line,"",@progbits +.Lline_table_start0: + .text + .file "cu2.c" + .section .debug_abbrev,"",@progbits + .byte 1 # Abbreviation Code + .byte 17 # DW_TAG_compile_unit + .byte 0 # DW_CHILDREN_no + .byte 37 # DW_AT_producer + .byte 37 # DW_FORM_strx1 + .byte 19 # DW_AT_language + .byte 5 # DW_FORM_data2 + .byte 3 # DW_AT_name + .byte 37 # DW_FORM_strx1 + .byte 114 # DW_AT_str_offsets_base + .byte 23 # DW_FORM_sec_offset + .byte 16 # DW_AT_stmt_list + .byte 23 # DW_FORM_sec_offset + .byte 27 # DW_AT_comp_dir + .byte 37 # DW_FORM_strx1 + .byte 121 # DW_AT_macros + .byte 23 # DW_FORM_sec_offset + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 0 # EOM(3) + .section .debug_info,"",@progbits +.Lcu_begin1: + .long .Ldebug_info_end1-.Ldebug_info_start1 # Length of Unit +.Ldebug_info_start1: + .short 5 # DWARF version number + .byte 1 # DWARF Unit Type + .byte 8 # Address Size (in bytes) + .long .debug_abbrev # Offset Into Abbrev. Section + .byte 1 # Abbrev [1] 0xc:0x12 DW_TAG_compile_unit + .byte 0 # DW_AT_producer + .short 12 # DW_AT_language + .byte 1 # DW_AT_name + .long .Lstr_offsets_base1 # DW_AT_str_offsets_base + .long .Lline_table_start1 # DW_AT_stmt_list + .byte 2 # DW_AT_comp_dir + .long .Lcu_macro_begin1 # DW_AT_macros +.Ldebug_info_end1: + .section .debug_macro,"",@progbits +.Lcu_macro_begin1: + .short 5 # Macro information version + .byte 2 # Flags: 32 bit, debug_line_offset present + .long .Lline_table_start1 # debug_line_offset + .byte 3 # DW_MACRO_start_file + .byte 1 # Line Number + .byte 3 # Macro String + .byte 11 # DW_MACRO_define_strx + .byte 2 # Line Number + .byte 4 # Macro String + .byte 12 # DW_MACRO_undef_strx + .byte 3 # Line Number + .byte 5 # Macro String + .byte 4 # DW_MACRO_end_file + .section .debug_str_offsets,"",@progbits + .long 28 + .short 5 + .short 0 +.Lstr_offsets_base1: + .section .debug_str,"MS",@progbits,1 +.Linfo_string6: + .asciz "clang version 11.0.0" # string offset=0 +.Linfo_string7: + .asciz "cu2.c" # string offset=105 +.Linfo_string8: + .asciz "/" # string offset=111 +.Linfo_string9: + .asciz "DWARF_VERSION 5" # string offset=134 +.Linfo_string10: + .asciz "COMPILE_UNIT 2" # string offset=150 +.Linfo_string11: + .asciz "COMPILE_UNIT" # string offset=165 + .section .debug_str_offsets,"",@progbits + .long .Linfo_string6 + .long .Linfo_string7 + .long .Linfo_string8 + .long .Linfo_string9 + .long .Linfo_string10 + .long .Linfo_string11 +.Lline_table_start1: