24 using namespace dwarf;
25 using namespace object;
29 uint8_t &
UnitType,
bool &isUnitDWARF64) {
35 bool ValidLength =
false;
36 bool ValidVersion =
false;
37 bool ValidAddrSize =
false;
38 bool ValidType =
true;
39 bool ValidAbbrevOffset =
true;
42 Length = DebugInfoData.
getU32(Offset);
43 if (Length == UINT32_MAX) {
46 "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n",
50 Version = DebugInfoData.
getU16(Offset);
53 UnitType = DebugInfoData.
getU8(Offset);
54 AddrSize = DebugInfoData.
getU8(Offset);
55 AbbrOffset = DebugInfoData.
getU32(Offset);
59 AbbrOffset = DebugInfoData.
getU32(Offset);
60 AddrSize = DebugInfoData.
getU8(Offset);
63 if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
64 ValidAbbrevOffset =
false;
66 ValidLength = DebugInfoData.
isValidOffset(OffsetStart + Length + 3);
68 ValidAddrSize = AddrSize == 4 || AddrSize == 8;
69 if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
72 OS <<
format(
"Units[%d] - start offset: 0x%08x \n", UnitIndex, OffsetStart);
74 OS <<
"\tError: The length for this unit is too "
75 "large for the .debug_info provided.\n";
77 OS <<
"\tError: The 16 bit unit header version is not valid.\n";
79 OS <<
"\tError: The unit type encoding is not valid.\n";
80 if (!ValidAbbrevOffset)
81 OS <<
"\tError: The offset into the .debug_abbrev section is "
84 OS <<
"\tError: The address size is unsupported.\n";
86 *Offset = OffsetStart + Length + 4;
93 for (
unsigned I = 0;
I < NumDies; ++
I) {
95 if (Die.getTag() == DW_TAG_null)
97 for (
auto AttrValue : Die.attributes()) {
98 NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
99 NumUnitErrors += verifyDebugInfoForm(Die, AttrValue);
102 return NumUnitErrors == 0;
106 OS <<
"Verifying .debug_info Unit Header Chain...\n";
111 uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
112 uint8_t UnitType = 0;
113 bool isUnitDWARF64 =
false;
114 bool isHeaderChainValid =
true;
118 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
120 isHeaderChainValid =
false;
124 std::unique_ptr<DWARFUnit>
Unit;
126 case dwarf::DW_UT_type:
127 case dwarf::DW_UT_split_type: {
130 DCtx, DCtx.getInfoSection(), DCtx.getDebugAbbrev(),
131 &DCtx.getRangeSection(), DCtx.getStringSection(),
132 DCtx.getStringOffsetSection(), &DCtx.getAppleObjCSection(),
133 DCtx.getLineSection(), DCtx.isLittleEndian(),
false, TUSection,
137 case dwarf::DW_UT_skeleton:
138 case dwarf::DW_UT_split_compile:
139 case dwarf::DW_UT_compile:
140 case dwarf::DW_UT_partial:
146 DCtx, DCtx.getInfoSection(), DCtx.getDebugAbbrev(),
147 &DCtx.getRangeSection(), DCtx.getStringSection(),
148 DCtx.getStringOffsetSection(), &DCtx.getAppleObjCSection(),
149 DCtx.getLineSection(), DCtx.isLittleEndian(),
false, CUSection,
155 Unit->extract(DebugInfoData, &OffsetStart);
156 if (!verifyUnitContents(*Unit))
157 ++NumDebugInfoErrors;
162 if (UnitIdx == 0 && !hasDIE) {
163 OS <<
"Warning: .debug_info is empty.\n";
164 isHeaderChainValid =
true;
166 NumDebugInfoErrors += verifyDebugInfoReferences();
167 return (isHeaderChainValid && NumDebugInfoErrors == 0);
170 unsigned DWARFVerifier::verifyDebugInfoAttribute(
const DWARFDie &Die,
172 unsigned NumErrors = 0;
173 const auto Attr = AttrValue.
Attr;
178 if (*SectionOffset >= DCtx.getRangeSection().Data.size()) {
180 OS <<
"error: DW_AT_ranges offset is beyond .debug_ranges "
187 OS <<
"error: DIE has invalid DW_AT_ranges encoding:\n";
192 case DW_AT_stmt_list:
195 if (*SectionOffset >= DCtx.getLineSection().Data.size()) {
197 OS <<
"error: DW_AT_stmt_list offset is beyond .debug_line "
199 <<
format(
"0x%08" PRIx64, *SectionOffset) <<
"\n";
205 OS <<
"error: DIE has invalid DW_AT_stmt_list encoding:\n";
217 unsigned DWARFVerifier::verifyDebugInfoForm(
const DWARFDie &Die,
219 unsigned NumErrors = 0;
226 case DW_FORM_ref_udata: {
234 if (CUOffset >= CUSize) {
237 <<
format(
"0x%08" PRIx64, CUOffset)
238 <<
" is invalid (must be less than CU size of "
239 <<
format(
"0x%08" PRIx32, CUSize) <<
"):\n";
245 ReferenceToDIEOffsets[*RefVal].insert(Die.
getOffset());
250 case DW_FORM_ref_addr: {
256 if (*RefVal >= DCtx.getInfoSection().Data.size()) {
258 OS <<
"error: DW_FORM_ref_addr offset beyond .debug_info "
265 ReferenceToDIEOffsets[*RefVal].insert(Die.
getOffset());
273 if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) {
275 OS <<
"error: DW_FORM_strp offset beyond .debug_str bounds:\n";
287 unsigned DWARFVerifier::verifyDebugInfoReferences() {
290 OS <<
"Verifying .debug_info references...\n";
291 unsigned NumErrors = 0;
292 for (
auto Pair : ReferenceToDIEOffsets) {
293 auto Die = DCtx.getDIEForOffset(Pair.first);
297 OS <<
"error: invalid DIE reference " <<
format(
"0x%08" PRIx64, Pair.first)
298 <<
". Offset is in between DIEs:\n";
299 for (
auto Offset : Pair.second) {
300 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
301 ReferencingDie.dump(OS, 0);
309 void DWARFVerifier::verifyDebugLineStmtOffsets() {
310 std::map<uint64_t, DWARFDie> StmtListToDie;
311 for (
const auto &CU : DCtx.compile_units()) {
312 auto Die = CU->getUnitDIE();
317 if (!StmtSectionOffset)
319 const uint32_t LineTableOffset = *StmtSectionOffset;
320 auto LineTable = DCtx.getLineTableForUnit(CU.get());
321 if (LineTableOffset < DCtx.getLineSection().Data.size()) {
323 ++NumDebugLineErrors;
324 OS <<
"error: .debug_line[" <<
format(
"0x%08" PRIx32, LineTableOffset)
325 <<
"] was not able to be parsed for CU:\n";
332 assert(LineTable ==
nullptr);
337 auto Iter = StmtListToDie.find(LineTableOffset);
338 if (Iter != StmtListToDie.end()) {
339 ++NumDebugLineErrors;
340 OS <<
"error: two compile unit DIEs, "
341 <<
format(
"0x%08" PRIx32, Iter->second.getOffset()) <<
" and "
343 <<
", have the same DW_AT_stmt_list section offset:\n";
344 Iter->second.dump(OS, 0);
350 StmtListToDie[LineTableOffset] = Die;
354 void DWARFVerifier::verifyDebugLineRows() {
355 for (
const auto &CU : DCtx.compile_units()) {
356 auto Die = CU->getUnitDIE();
357 auto LineTable = DCtx.getLineTableForUnit(CU.get());
362 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
363 uint64_t PrevAddress = 0;
365 for (
const auto &Row : LineTable->Rows) {
366 if (Row.Address < PrevAddress) {
367 ++NumDebugLineErrors;
368 OS <<
"error: .debug_line["
371 <<
"] row[" << RowIndex
372 <<
"] decreases in address from previous row:\n";
376 LineTable->Rows[RowIndex - 1].dump(OS);
381 if (Row.File > MaxFileIndex) {
382 ++NumDebugLineErrors;
383 OS <<
"error: .debug_line["
386 <<
"][" << RowIndex <<
"] has invalid file index " << Row.File
387 <<
" (valid values are [1," << MaxFileIndex <<
"]):\n";
395 PrevAddress = Row.Address;
402 NumDebugLineErrors = 0;
403 OS <<
"Verifying .debug_line...\n";
404 verifyDebugLineStmtOffsets();
405 verifyDebugLineRows();
406 return NumDebugLineErrors == 0;
410 NumAppleNamesErrors = 0;
413 DCtx.isLittleEndian(), 0);
414 DataExtractor StrData(DCtx.getStringSection(), DCtx.isLittleEndian(), 0);
417 if (!AppleNames.extract()) {
421 OS <<
"Verifying .apple_names...\n";
424 uint32_t NumBuckets = AppleNames.getNumBuckets();
425 uint32_t NumHashes = AppleNames.getNumHashes();
428 AppleNames.getSizeHdr() + AppleNames.getHeaderDataLength();
429 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
430 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
432 for (
uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
433 uint32_t HashIdx = AppleNamesSection.getU32(&BucketsOffset);
434 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
435 OS <<
format(
"error: Bucket[%d] has invalid hash index: %u\n", BucketIdx,
437 ++NumAppleNamesErrors;
441 uint32_t NumAtoms = AppleNames.getAtomsDesc().size();
443 OS <<
"error: no atoms; failed to read HashData\n";
444 ++NumAppleNamesErrors;
448 if (!AppleNames.validateForms()) {
449 OS <<
"error: unsupported form; failed to read HashData\n";
450 ++NumAppleNamesErrors;
454 for (
uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
455 uint32_t HashOffset = HashesBase + 4 * HashIdx;
456 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
458 uint32_t HashDataOffset = AppleNamesSection.getU32(&DataOffset);
459 if (!AppleNamesSection.isValidOffsetForDataOfSize(HashDataOffset,
461 OS <<
format(
"error: Hash[%d] has invalid HashData offset: 0x%08x\n",
462 HashIdx, HashDataOffset);
463 ++NumAppleNamesErrors;
471 while ((StrpOffset = AppleNamesSection.getU32(&HashDataOffset)) != 0) {
473 AppleNamesSection.getU32(&HashDataOffset);
474 for (
uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
476 DieOffset = AppleNames.readAtoms(HashDataOffset);
477 if (!DCtx.getDIEForOffset(DieOffset)) {
479 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
480 StringOffset = StrpOffset;
481 const char *
Name = StrData.getCStr(&StringOffset);
486 "error: .apple_names Bucket[%d] Hash[%d] = 0x%08x "
488 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
489 BucketIdx, HashIdx, Hash, StringCount, StrpOffset, HashDataIdx,
492 ++NumAppleNamesErrors;
498 return NumAppleNamesErrors == 0;
dwarf::Attribute Attr
The attribute enumeration of this attribute.
bool handleDebugLine()
Verify the information in the .debug_line section.
uint32_t getNextUnitOffset() const
bool handleDebugInfo()
Verify the information in the .debug_info section.
DWARFFormValue Value
The form and value for this attribute.
uint32_t getOffset() const
Get the absolute offset into the debug info or types section.
StringRef FormEncodingString(unsigned Encoding)
unsigned getNumDIEs()
Returns the number of DIEs in the unit.
Encapsulates a DWARF attribute value and all of the data required to describe the attribute value...
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Optional< uint64_t > toSectionOffset(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
DWARFUnit * getDwarfUnit() const
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
const uint32_t DW_INVALID_OFFSET
Optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
UnitType
Constants for unit types in DWARF v5.
Concrete instance of DWARFUnitSection, specialized for one Unit type.
static void dumpTableHeader(raw_ostream &OS)
llvm_unreachable("Unknown RegBank!")
static bool isValidUnitType(uint8_t UnitType)
static bool isSupportedVersion(unsigned version)
DWARFDie getDIEAtIndex(unsigned Index)
Return the DIE object at the given index.
void dump(raw_ostream &OS, unsigned recurseDepth, unsigned indent=0, DIDumpOptions DumpOpts=DIDumpOptions()) const
Dump the DIE and all of its attributes to the supplied stream.
std::vector< uint8_t > Unit
bool handleAppleNames()
Verify the information in the .apple_names accelerator table.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::string Hash(const Unit &U)