Index: include/llvm/DebugInfo/DWARF/DWARFUnit.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFUnit.h +++ include/llvm/DebugInfo/DWARF/DWARFUnit.h @@ -293,6 +293,31 @@ UnitType == dwarf::DW_UT_split_type; } + static bool isValidUnitTag(dwarf::Tag Tag) { + return Tag == dwarf::DW_TAG_compile_unit || + Tag == dwarf::DW_TAG_type_unit || + Tag == dwarf::DW_TAG_partial_unit || + Tag == dwarf::DW_TAG_skeleton_unit || + Tag == dwarf::DW_TAG_imported_unit; + } + + static bool isMatchingUnitTypeAndTag(uint8_t UnitType, dwarf::Tag Tag) { + switch (UnitType) { + case dwarf::DW_UT_compile: + return Tag == dwarf::DW_TAG_compile_unit; + case dwarf::DW_UT_type: + return Tag == dwarf::DW_TAG_type_unit; + case dwarf::DW_UT_partial: + return Tag == dwarf::DW_TAG_partial_unit; + case dwarf::DW_UT_skeleton: + return Tag == dwarf::DW_TAG_skeleton_unit; + case dwarf::DW_UT_split_compile: + case dwarf::DW_UT_split_type: + return isValidUnitTag(Tag); + } + return false; + } + /// \brief Return the number of bytes for the header of a unit of /// UnitType type. /// Index: include/llvm/DebugInfo/DWARF/DWARFVerifier.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFVerifier.h +++ include/llvm/DebugInfo/DWARF/DWARFVerifier.h @@ -136,8 +136,22 @@ uint32_t *Offset, unsigned UnitIndex, uint8_t &UnitType, bool &isUnitDWARF64); - - bool verifyUnitContents(DWARFUnit Unit); + /// Verifies the header of a unit in the .debug_info section. + /// + /// This function currently verifies: + /// - The debug info attributes. + /// - The debug info form=s. + /// - The presence of a root DIE. + /// - That the root DIE is a unit DIE. + /// - If a unit type is provided, that the unit DIE matches the unit type. + /// - The DIE ranges. + /// + /// \param Unit The DWARF Unit to verifiy. + /// \param UnitType An optional unit type which will be used to verify the + /// type of the unit DIE. + /// + /// \returns true if the content is verified successfully, false otherwise. + bool verifyUnitContents(DWARFUnit Unit, uint8_t UnitType = 0); /// Verify that all Die ranges are valid. /// Index: lib/DebugInfo/DWARF/DWARFVerifier.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFVerifier.cpp +++ lib/DebugInfo/DWARF/DWARFVerifier.cpp @@ -169,7 +169,7 @@ return Success; } -bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit) { +bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit, uint8_t UnitType) { uint32_t NumUnitErrors = 0; unsigned NumDies = Unit.getNumDIEs(); for (unsigned I = 0; I < NumDies; ++I) { @@ -182,14 +182,30 @@ } } - if (DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false)) { - DieRangeInfo RI; - NumUnitErrors += verifyDieRanges(Die, RI); - } else { - error() << "Compilation unit without unit DIE.\n"; + DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false); + if (!Die) { + error() << "Compilation unit without DIE.\n"; + NumUnitErrors++; + return NumUnitErrors == 0; + } + + if (!DWARFUnit::isValidUnitTag(Die.getTag())) { + error() << "Compilation unit root DIE is not a unit DIE: " + << dwarf::TagString(Die.getTag()) << ".\n"; NumUnitErrors++; } + if (UnitType != 0 && + !DWARFUnit::isMatchingUnitTypeAndTag(UnitType, Die.getTag())) { + error() << "Compilation unit type (" << dwarf::UnitTypeString(UnitType) + << ") and root DIE (" << dwarf::TagString(Die.getTag()) + << ") do not match.\n"; + NumUnitErrors++; + } + + DieRangeInfo RI; + NumUnitErrors += verifyDieRanges(Die, RI); + return NumUnitErrors == 0; } @@ -286,7 +302,7 @@ default: { llvm_unreachable("Invalid UnitType."); } } Unit->extract(DebugInfoData, &OffsetStart); - if (!verifyUnitContents(*Unit)) + if (!verifyUnitContents(*Unit, UnitType)) ++NumDebugInfoErrors; } hasDIE = DebugInfoData.isValidOffset(Offset); Index: test/DebugInfo/dwarfdump-header.test =================================================================== --- test/DebugInfo/dwarfdump-header.test +++ test/DebugInfo/dwarfdump-header.test @@ -1,4 +1,5 @@ RUN: llvm-dwarfdump -v %p/Inputs/dwarfdump-header.elf-x86-64 | FileCheck %s +RUN: llvm-dwarfdump -v --verify %p/Inputs/dwarfdump-header.elf-x86-64 The input file is hand-coded assembler to generate all the units, so we're willing to make exact checks for offsets and such. Index: test/tools/llvm-dwarfdump/X86/empty-CU.s =================================================================== --- test/tools/llvm-dwarfdump/X86/empty-CU.s +++ test/tools/llvm-dwarfdump/X86/empty-CU.s @@ -1,7 +1,7 @@ # RUN: llvm-mc %s -filetype obj -triple x86_64-apple-darwin -o - \ # RUN: | not llvm-dwarfdump --verify --debug-info - \ # RUN: | FileCheck %s -# CHECK: error: Compilation unit without unit DIE. +# CHECK: error: Compilation unit without DIE. .section __DWARF,__debug_info,regular,debug .long 8 # CU length Index: test/tools/llvm-dwarfdump/X86/verify_debug_info.s =================================================================== --- test/tools/llvm-dwarfdump/X86/verify_debug_info.s +++ test/tools/llvm-dwarfdump/X86/verify_debug_info.s @@ -11,6 +11,8 @@ # CHECK-NEXT: DW_AT_comp_dir [DW_FORM_strp] ( .debug_str[0x0000003f] = "/Users/sgravani/Development/tests") # CHECK-NEXT: DW_AT_low_pc [DW_FORM_addr] (0x0000000000000000) # CHECK-NEXT: DW_AT_high_pc [DW_FORM_data4] (0x00000016){{[[:space:]]}} +# CHECK-NEXT: error: Compilation unit root DIE is not a unit DIE: DW_TAG_null. +# CHECK-NEXT: error: Compilation unit type (DW_UT_compile) and root DIE (DW_TAG_null) do not match. # CHECK-NEXT: error: Units[2] - start offset: 0x00000068 # CHECK-NEXT: note: The length for this unit is too large for the .debug_info provided. # CHECK-NEXT: note: The unit type encoding is not valid. Index: test/tools/llvm-dwarfdump/X86/verify_unit_header_chain.s =================================================================== --- test/tools/llvm-dwarfdump/X86/verify_unit_header_chain.s +++ test/tools/llvm-dwarfdump/X86/verify_unit_header_chain.s @@ -9,6 +9,8 @@ # CHECK-NEXT: error: Units[2] - start offset: 0x00000026 # CHECK-NEXT: note: The 16 bit unit header version is not valid. # CHECK-NEXT: note: The offset into the .debug_abbrev section is not valid. +# CHECK-NEXT: error: Compilation unit root DIE is not a unit DIE: DW_TAG_null. +# CHECK-NEXT: error: Compilation unit type (DW_UT_compile) and root DIE (DW_TAG_null) do not match. # CHECK-NEXT: error: Units[4] - start offset: 0x00000041 # CHECK-NEXT: note: The length for this unit is too large for the .debug_info provided.