LLVM 21.0.0git
DWARFVerifier.cpp
Go to the documentation of this file.
1//===- DWARFVerifier.cpp --------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
10#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/SmallSet.h"
29#include "llvm/Object/Error.h"
30#include "llvm/Support/DJB.h"
31#include "llvm/Support/Error.h"
35#include "llvm/Support/JSON.h"
38#include <map>
39#include <set>
40#include <vector>
41
42using namespace llvm;
43using namespace dwarf;
44using namespace object;
45
46namespace llvm {
48}
49
50std::optional<DWARFAddressRange>
52 auto Begin = Ranges.begin();
53 auto End = Ranges.end();
54 auto Pos = std::lower_bound(Begin, End, R);
55
56 // Check for exact duplicates which is an allowed special case
57 if (Pos != End && *Pos == R) {
58 return std::nullopt;
59 }
60
61 if (Pos != End) {
63 if (Pos->merge(R))
64 return Range;
65 }
66 if (Pos != Begin) {
67 auto Iter = Pos - 1;
69 if (Iter->merge(R))
70 return Range;
71 }
72
73 Ranges.insert(Pos, R);
74 return std::nullopt;
75}
76
79 if (RI.Ranges.empty())
80 return Children.end();
81
82 auto End = Children.end();
83 auto Iter = Children.begin();
84 while (Iter != End) {
85 if (Iter->intersects(RI))
86 return Iter;
87 ++Iter;
88 }
89 Children.insert(RI);
90 return Children.end();
91}
92
94 auto I1 = Ranges.begin(), E1 = Ranges.end();
95 auto I2 = RHS.Ranges.begin(), E2 = RHS.Ranges.end();
96 if (I2 == E2)
97 return true;
98
99 DWARFAddressRange R = *I2;
100 while (I1 != E1) {
101 bool Covered = I1->LowPC <= R.LowPC;
102 if (R.LowPC == R.HighPC || (Covered && R.HighPC <= I1->HighPC)) {
103 if (++I2 == E2)
104 return true;
105 R = *I2;
106 continue;
107 }
108 if (!Covered)
109 return false;
110 if (R.LowPC < I1->HighPC)
111 R.LowPC = I1->HighPC;
112 ++I1;
113 }
114 return false;
115}
116
118 auto I1 = Ranges.begin(), E1 = Ranges.end();
119 auto I2 = RHS.Ranges.begin(), E2 = RHS.Ranges.end();
120 while (I1 != E1 && I2 != E2) {
121 if (I1->intersects(*I2)) {
122 // Exact duplicates are allowed
123 if (!(*I1 == *I2))
124 return true;
125 }
126 if (I1->LowPC < I2->LowPC)
127 ++I1;
128 else
129 ++I2;
130 }
131 return false;
132}
133
134bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
135 uint64_t *Offset, unsigned UnitIndex,
136 uint8_t &UnitType, bool &isUnitDWARF64) {
137 uint64_t AbbrOffset, Length;
138 uint8_t AddrSize = 0;
140 bool Success = true;
141
142 bool ValidLength = false;
143 bool ValidVersion = false;
144 bool ValidAddrSize = false;
145 bool ValidType = true;
146 bool ValidAbbrevOffset = true;
147
148 uint64_t OffsetStart = *Offset;
150 std::tie(Length, Format) = DebugInfoData.getInitialLength(Offset);
151 isUnitDWARF64 = Format == DWARF64;
152 Version = DebugInfoData.getU16(Offset);
153
154 if (Version >= 5) {
155 UnitType = DebugInfoData.getU8(Offset);
156 AddrSize = DebugInfoData.getU8(Offset);
157 AbbrOffset = isUnitDWARF64 ? DebugInfoData.getU64(Offset) : DebugInfoData.getU32(Offset);
158 ValidType = dwarf::isUnitType(UnitType);
159 } else {
160 UnitType = 0;
161 AbbrOffset = isUnitDWARF64 ? DebugInfoData.getU64(Offset) : DebugInfoData.getU32(Offset);
162 AddrSize = DebugInfoData.getU8(Offset);
163 }
164
167 if (!AbbrevSetOrErr) {
168 ValidAbbrevOffset = false;
169 // FIXME: A problematic debug_abbrev section is reported below in the form
170 // of a `note:`. We should propagate this error there (or elsewhere) to
171 // avoid losing the specific problem with the debug_abbrev section.
172 consumeError(AbbrevSetOrErr.takeError());
173 }
174
175 ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
177 ValidAddrSize = DWARFContext::isAddressSizeSupported(AddrSize);
178 if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
179 !ValidType) {
180 Success = false;
181 bool HeaderShown = false;
182 auto ShowHeaderOnce = [&]() {
183 if (!HeaderShown) {
184 error() << format("Units[%d] - start offset: 0x%08" PRIx64 " \n",
185 UnitIndex, OffsetStart);
186 HeaderShown = true;
187 }
188 };
189 if (!ValidLength)
190 ErrorCategory.Report(
191 "Unit Header Length: Unit too large for .debug_info provided", [&]() {
192 ShowHeaderOnce();
193 note() << "The length for this unit is too "
194 "large for the .debug_info provided.\n";
195 });
196 if (!ValidVersion)
197 ErrorCategory.Report(
198 "Unit Header Length: 16 bit unit header version is not valid", [&]() {
199 ShowHeaderOnce();
200 note() << "The 16 bit unit header version is not valid.\n";
201 });
202 if (!ValidType)
203 ErrorCategory.Report(
204 "Unit Header Length: Unit type encoding is not valid", [&]() {
205 ShowHeaderOnce();
206 note() << "The unit type encoding is not valid.\n";
207 });
208 if (!ValidAbbrevOffset)
209 ErrorCategory.Report(
210 "Unit Header Length: Offset into the .debug_abbrev section is not "
211 "valid",
212 [&]() {
213 ShowHeaderOnce();
214 note() << "The offset into the .debug_abbrev section is "
215 "not valid.\n";
216 });
217 if (!ValidAddrSize)
218 ErrorCategory.Report("Unit Header Length: Address size is unsupported",
219 [&]() {
220 ShowHeaderOnce();
221 note() << "The address size is unsupported.\n";
222 });
223 }
224 *Offset = OffsetStart + Length + (isUnitDWARF64 ? 12 : 4);
225 return Success;
226}
227
228bool DWARFVerifier::verifyName(const DWARFDie &Die) {
229 // FIXME Add some kind of record of which DIE names have already failed and
230 // don't bother checking a DIE that uses an already failed DIE.
231
232 std::string ReconstructedName;
233 raw_string_ostream OS(ReconstructedName);
234 std::string OriginalFullName;
235 Die.getFullName(OS, &OriginalFullName);
236 OS.flush();
237 if (OriginalFullName.empty() || OriginalFullName == ReconstructedName)
238 return false;
239
240 ErrorCategory.Report(
241 "Simplified template DW_AT_name could not be reconstituted", [&]() {
242 error()
243 << "Simplified template DW_AT_name could not be reconstituted:\n"
244 << formatv(" original: {0}\n"
245 " reconstituted: {1}\n",
246 OriginalFullName, ReconstructedName);
247 dump(Die) << '\n';
248 dump(Die.getDwarfUnit()->getUnitDIE()) << '\n';
249 });
250 return true;
251}
252
253unsigned DWARFVerifier::verifyUnitContents(DWARFUnit &Unit,
254 ReferenceMap &UnitLocalReferences,
255 ReferenceMap &CrossUnitReferences) {
256 unsigned NumUnitErrors = 0;
257 unsigned NumDies = Unit.getNumDIEs();
258 for (unsigned I = 0; I < NumDies; ++I) {
259 auto Die = Unit.getDIEAtIndex(I);
260
261 if (Die.getTag() == DW_TAG_null)
262 continue;
263
264 for (auto AttrValue : Die.attributes()) {
265 NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
266 NumUnitErrors += verifyDebugInfoForm(Die, AttrValue, UnitLocalReferences,
267 CrossUnitReferences);
268 }
269
270 NumUnitErrors += verifyName(Die);
271
272 if (Die.hasChildren()) {
273 if (Die.getFirstChild().isValid() &&
274 Die.getFirstChild().getTag() == DW_TAG_null) {
275 warn() << dwarf::TagString(Die.getTag())
276 << " has DW_CHILDREN_yes but DIE has no children: ";
277 Die.dump(OS);
278 }
279 }
280
281 NumUnitErrors += verifyDebugInfoCallSite(Die);
282 }
283
284 DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false);
285 if (!Die) {
286 ErrorCategory.Report("Compilation unit missing DIE", [&]() {
287 error() << "Compilation unit without DIE.\n";
288 });
289 NumUnitErrors++;
290 return NumUnitErrors;
291 }
292
293 if (!dwarf::isUnitType(Die.getTag())) {
294 ErrorCategory.Report("Compilation unit root DIE is not a unit DIE", [&]() {
295 error() << "Compilation unit root DIE is not a unit DIE: "
296 << dwarf::TagString(Die.getTag()) << ".\n";
297 });
298 NumUnitErrors++;
299 }
300
301 uint8_t UnitType = Unit.getUnitType();
303 ErrorCategory.Report("Mismatched unit type", [&]() {
304 error() << "Compilation unit type (" << dwarf::UnitTypeString(UnitType)
305 << ") and root DIE (" << dwarf::TagString(Die.getTag())
306 << ") do not match.\n";
307 });
308 NumUnitErrors++;
309 }
310
311 // According to DWARF Debugging Information Format Version 5,
312 // 3.1.2 Skeleton Compilation Unit Entries:
313 // "A skeleton compilation unit has no children."
314 if (Die.getTag() == dwarf::DW_TAG_skeleton_unit && Die.hasChildren()) {
315 ErrorCategory.Report("Skeleton CU has children", [&]() {
316 error() << "Skeleton compilation unit has children.\n";
317 });
318 NumUnitErrors++;
319 }
320
321 DieRangeInfo RI;
322 NumUnitErrors += verifyDieRanges(Die, RI);
323
324 return NumUnitErrors;
325}
326
327unsigned DWARFVerifier::verifyDebugInfoCallSite(const DWARFDie &Die) {
328 if (Die.getTag() != DW_TAG_call_site && Die.getTag() != DW_TAG_GNU_call_site)
329 return 0;
330
331 DWARFDie Curr = Die.getParent();
332 for (; Curr.isValid() && !Curr.isSubprogramDIE(); Curr = Die.getParent()) {
333 if (Curr.getTag() == DW_TAG_inlined_subroutine) {
334 ErrorCategory.Report(
335 "Call site nested entry within inlined subroutine", [&]() {
336 error() << "Call site entry nested within inlined subroutine:";
337 Curr.dump(OS);
338 });
339 return 1;
340 }
341 }
342
343 if (!Curr.isValid()) {
344 ErrorCategory.Report(
345 "Call site entry not nested within valid subprogram", [&]() {
346 error() << "Call site entry not nested within a valid subprogram:";
347 Die.dump(OS);
348 });
349 return 1;
350 }
351
352 std::optional<DWARFFormValue> CallAttr = Curr.find(
353 {DW_AT_call_all_calls, DW_AT_call_all_source_calls,
354 DW_AT_call_all_tail_calls, DW_AT_GNU_all_call_sites,
355 DW_AT_GNU_all_source_call_sites, DW_AT_GNU_all_tail_call_sites});
356 if (!CallAttr) {
357 ErrorCategory.Report(
358 "Subprogram with call site entry has no DW_AT_call attribute", [&]() {
359 error()
360 << "Subprogram with call site entry has no DW_AT_call attribute:";
361 Curr.dump(OS);
362 Die.dump(OS, /*indent*/ 1);
363 });
364 return 1;
365 }
366
367 return 0;
368}
369
370unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
371 if (!Abbrev)
372 return 0;
373
376 if (!AbbrDeclsOrErr) {
377 std::string ErrMsg = toString(AbbrDeclsOrErr.takeError());
378 ErrorCategory.Report("Abbreviation Declaration error",
379 [&]() { error() << ErrMsg << "\n"; });
380 return 1;
381 }
382
383 const auto *AbbrDecls = *AbbrDeclsOrErr;
384 unsigned NumErrors = 0;
385 for (auto AbbrDecl : *AbbrDecls) {
387 for (auto Attribute : AbbrDecl.attributes()) {
388 auto Result = AttributeSet.insert(Attribute.Attr);
389 if (!Result.second) {
390 ErrorCategory.Report(
391 "Abbreviation declartion contains multiple attributes", [&]() {
392 error() << "Abbreviation declaration contains multiple "
393 << AttributeString(Attribute.Attr) << " attributes.\n";
394 AbbrDecl.dump(OS);
395 });
396 ++NumErrors;
397 }
398 }
399 }
400 return NumErrors;
401}
402
404 OS << "Verifying .debug_abbrev...\n";
405
406 const DWARFObject &DObj = DCtx.getDWARFObj();
407 unsigned NumErrors = 0;
408 if (!DObj.getAbbrevSection().empty())
409 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev());
410 if (!DObj.getAbbrevDWOSection().empty())
411 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO());
412
413 return NumErrors == 0;
414}
415
416unsigned DWARFVerifier::verifyUnits(const DWARFUnitVector &Units) {
417 unsigned NumDebugInfoErrors = 0;
418 ReferenceMap CrossUnitReferences;
419
420 unsigned Index = 1;
421 for (const auto &Unit : Units) {
422 OS << "Verifying unit: " << Index << " / " << Units.getNumUnits();
423 if (const char* Name = Unit->getUnitDIE(true).getShortName())
424 OS << ", \"" << Name << '\"';
425 OS << '\n';
426 OS.flush();
427 ReferenceMap UnitLocalReferences;
428 NumDebugInfoErrors +=
429 verifyUnitContents(*Unit, UnitLocalReferences, CrossUnitReferences);
430 NumDebugInfoErrors += verifyDebugInfoReferences(
431 UnitLocalReferences, [&](uint64_t Offset) { return Unit.get(); });
432 ++Index;
433 }
434
435 NumDebugInfoErrors += verifyDebugInfoReferences(
436 CrossUnitReferences, [&](uint64_t Offset) -> DWARFUnit * {
437 if (DWARFUnit *U = Units.getUnitForOffset(Offset))
438 return U;
439 return nullptr;
440 });
441
442 return NumDebugInfoErrors;
443}
444
445unsigned DWARFVerifier::verifyUnitSection(const DWARFSection &S) {
446 const DWARFObject &DObj = DCtx.getDWARFObj();
447 DWARFDataExtractor DebugInfoData(DObj, S, DCtx.isLittleEndian(), 0);
448 unsigned NumDebugInfoErrors = 0;
449 uint64_t Offset = 0, UnitIdx = 0;
450 uint8_t UnitType = 0;
451 bool isUnitDWARF64 = false;
452 bool isHeaderChainValid = true;
453 bool hasDIE = DebugInfoData.isValidOffset(Offset);
454 DWARFUnitVector TypeUnitVector;
455 DWARFUnitVector CompileUnitVector;
456 /// A map that tracks all references (converted absolute references) so we
457 /// can verify each reference points to a valid DIE and not an offset that
458 /// lies between to valid DIEs.
459 ReferenceMap CrossUnitReferences;
460 while (hasDIE) {
461 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
462 isUnitDWARF64)) {
463 isHeaderChainValid = false;
464 if (isUnitDWARF64)
465 break;
466 }
467 hasDIE = DebugInfoData.isValidOffset(Offset);
468 ++UnitIdx;
469 }
470 if (UnitIdx == 0 && !hasDIE) {
471 warn() << "Section is empty.\n";
472 isHeaderChainValid = true;
473 }
474 if (!isHeaderChainValid)
475 ++NumDebugInfoErrors;
476 return NumDebugInfoErrors;
477}
478
479unsigned DWARFVerifier::verifyIndex(StringRef Name,
480 DWARFSectionKind InfoColumnKind,
481 StringRef IndexStr) {
482 if (IndexStr.empty())
483 return 0;
484 OS << "Verifying " << Name << "...\n";
485 DWARFUnitIndex Index(InfoColumnKind);
486 DataExtractor D(IndexStr, DCtx.isLittleEndian(), 0);
487 if (!Index.parse(D))
488 return 1;
489 using MapType = IntervalMap<uint64_t, uint64_t>;
490 MapType::Allocator Alloc;
491 std::vector<std::unique_ptr<MapType>> Sections(Index.getColumnKinds().size());
492 for (const DWARFUnitIndex::Entry &E : Index.getRows()) {
493 uint64_t Sig = E.getSignature();
494 if (!E.getContributions())
495 continue;
496 for (auto E : enumerate(
497 InfoColumnKind == DW_SECT_INFO
498 ? ArrayRef(E.getContributions(), Index.getColumnKinds().size())
499 : ArrayRef(E.getContribution(), 1))) {
501 int Col = E.index();
502 if (SC.getLength() == 0)
503 continue;
504 if (!Sections[Col])
505 Sections[Col] = std::make_unique<MapType>(Alloc);
506 auto &M = *Sections[Col];
507 auto I = M.find(SC.getOffset());
508 if (I != M.end() && I.start() < (SC.getOffset() + SC.getLength())) {
509 StringRef Category = InfoColumnKind == DWARFSectionKind::DW_SECT_INFO
510 ? "Overlapping CU index entries"
511 : "Overlapping TU index entries";
512 ErrorCategory.Report(Category, [&]() {
513 error() << llvm::formatv(
514 "overlapping index entries for entries {0:x16} "
515 "and {1:x16} for column {2}\n",
516 *I, Sig, toString(Index.getColumnKinds()[Col]));
517 });
518 return 1;
519 }
520 M.insert(SC.getOffset(), SC.getOffset() + SC.getLength() - 1, Sig);
521 }
522 }
523
524 return 0;
525}
526
528 return verifyIndex(".debug_cu_index", DWARFSectionKind::DW_SECT_INFO,
529 DCtx.getDWARFObj().getCUIndexSection()) == 0;
530}
531
533 return verifyIndex(".debug_tu_index", DWARFSectionKind::DW_SECT_EXT_TYPES,
534 DCtx.getDWARFObj().getTUIndexSection()) == 0;
535}
536
538 const DWARFObject &DObj = DCtx.getDWARFObj();
539 unsigned NumErrors = 0;
540
541 OS << "Verifying .debug_info Unit Header Chain...\n";
542 DObj.forEachInfoSections([&](const DWARFSection &S) {
543 NumErrors += verifyUnitSection(S);
544 });
545
546 OS << "Verifying .debug_types Unit Header Chain...\n";
547 DObj.forEachTypesSections([&](const DWARFSection &S) {
548 NumErrors += verifyUnitSection(S);
549 });
550
551 OS << "Verifying non-dwo Units...\n";
552 NumErrors += verifyUnits(DCtx.getNormalUnitsVector());
553
554 OS << "Verifying dwo Units...\n";
555 NumErrors += verifyUnits(DCtx.getDWOUnitsVector());
556 return NumErrors == 0;
557}
558
559unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die,
560 DieRangeInfo &ParentRI) {
561 unsigned NumErrors = 0;
562
563 if (!Die.isValid())
564 return NumErrors;
565
566 DWARFUnit *Unit = Die.getDwarfUnit();
567
568 auto RangesOrError = Die.getAddressRanges();
569 if (!RangesOrError) {
570 // FIXME: Report the error.
571 if (!Unit->isDWOUnit())
572 ++NumErrors;
573 llvm::consumeError(RangesOrError.takeError());
574 return NumErrors;
575 }
576
577 const DWARFAddressRangesVector &Ranges = RangesOrError.get();
578 // Build RI for this DIE and check that ranges within this DIE do not
579 // overlap.
580 DieRangeInfo RI(Die);
581
582 // TODO support object files better
583 //
584 // Some object file formats (i.e. non-MachO) support COMDAT. ELF in
585 // particular does so by placing each function into a section. The DWARF data
586 // for the function at that point uses a section relative DW_FORM_addrp for
587 // the DW_AT_low_pc and a DW_FORM_data4 for the offset as the DW_AT_high_pc.
588 // In such a case, when the Die is the CU, the ranges will overlap, and we
589 // will flag valid conflicting ranges as invalid.
590 //
591 // For such targets, we should read the ranges from the CU and partition them
592 // by the section id. The ranges within a particular section should be
593 // disjoint, although the ranges across sections may overlap. We would map
594 // the child die to the entity that it references and the section with which
595 // it is associated. The child would then be checked against the range
596 // information for the associated section.
597 //
598 // For now, simply elide the range verification for the CU DIEs if we are
599 // processing an object file.
600
601 if (!IsObjectFile || IsMachOObject || Die.getTag() != DW_TAG_compile_unit) {
602 bool DumpDieAfterError = false;
603 for (const auto &Range : Ranges) {
604 if (!Range.valid()) {
605 ++NumErrors;
606 ErrorCategory.Report("Invalid address range", [&]() {
607 error() << "Invalid address range " << Range << "\n";
608 DumpDieAfterError = true;
609 });
610 continue;
611 }
612
613 // Verify that ranges don't intersect and also build up the DieRangeInfo
614 // address ranges. Don't break out of the loop below early, or we will
615 // think this DIE doesn't have all of the address ranges it is supposed
616 // to have. Compile units often have DW_AT_ranges that can contain one or
617 // more dead stripped address ranges which tend to all be at the same
618 // address: 0 or -1.
619 if (auto PrevRange = RI.insert(Range)) {
620 ++NumErrors;
621 ErrorCategory.Report("DIE has overlapping DW_AT_ranges", [&]() {
622 error() << "DIE has overlapping ranges in DW_AT_ranges attribute: "
623 << *PrevRange << " and " << Range << '\n';
624 DumpDieAfterError = true;
625 });
626 }
627 }
628 if (DumpDieAfterError)
629 dump(Die, 2) << '\n';
630 }
631
632 // Verify that children don't intersect.
633 const auto IntersectingChild = ParentRI.insert(RI);
634 if (IntersectingChild != ParentRI.Children.end()) {
635 ++NumErrors;
636 ErrorCategory.Report("DIEs have overlapping address ranges", [&]() {
637 error() << "DIEs have overlapping address ranges:";
638 dump(Die);
639 dump(IntersectingChild->Die) << '\n';
640 });
641 }
642
643 // Verify that ranges are contained within their parent.
644 bool ShouldBeContained = !RI.Ranges.empty() && !ParentRI.Ranges.empty() &&
645 !(Die.getTag() == DW_TAG_subprogram &&
646 ParentRI.Die.getTag() == DW_TAG_subprogram);
647 if (ShouldBeContained && !ParentRI.contains(RI)) {
648 ++NumErrors;
649 ErrorCategory.Report(
650 "DIE address ranges are not contained by parent ranges", [&]() {
651 error()
652 << "DIE address ranges are not contained in its parent's ranges:";
653 dump(ParentRI.Die);
654 dump(Die, 2) << '\n';
655 });
656 }
657
658 // Recursively check children.
659 for (DWARFDie Child : Die)
660 NumErrors += verifyDieRanges(Child, RI);
661
662 return NumErrors;
663}
664
665unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
666 DWARFAttribute &AttrValue) {
667 unsigned NumErrors = 0;
668 auto ReportError = [&](StringRef category, const Twine &TitleMsg) {
669 ++NumErrors;
670 ErrorCategory.Report(category, [&]() {
671 error() << TitleMsg << '\n';
672 dump(Die) << '\n';
673 });
674 };
675
676 const DWARFObject &DObj = DCtx.getDWARFObj();
677 DWARFUnit *U = Die.getDwarfUnit();
678 const auto Attr = AttrValue.Attr;
679 switch (Attr) {
680 case DW_AT_ranges:
681 // Make sure the offset in the DW_AT_ranges attribute is valid.
682 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
683 unsigned DwarfVersion = U->getVersion();
684 const DWARFSection &RangeSection = DwarfVersion < 5
685 ? DObj.getRangesSection()
686 : DObj.getRnglistsSection();
687 if (U->isDWOUnit() && RangeSection.Data.empty())
688 break;
689 if (*SectionOffset >= RangeSection.Data.size())
690 ReportError("DW_AT_ranges offset out of bounds",
691 "DW_AT_ranges offset is beyond " +
692 StringRef(DwarfVersion < 5 ? ".debug_ranges"
693 : ".debug_rnglists") +
694 " bounds: " + llvm::formatv("{0:x8}", *SectionOffset));
695 break;
696 }
697 ReportError("Invalid DW_AT_ranges encoding",
698 "DIE has invalid DW_AT_ranges encoding:");
699 break;
700 case DW_AT_stmt_list:
701 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
702 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
703 if (*SectionOffset >= U->getLineSection().Data.size())
704 ReportError("DW_AT_stmt_list offset out of bounds",
705 "DW_AT_stmt_list offset is beyond .debug_line bounds: " +
706 llvm::formatv("{0:x8}", *SectionOffset));
707 break;
708 }
709 ReportError("Invalid DW_AT_stmt_list encoding",
710 "DIE has invalid DW_AT_stmt_list encoding:");
711 break;
712 case DW_AT_location: {
713 // FIXME: It might be nice if there's a way to walk location expressions
714 // without trying to resolve the address ranges - it'd be a more efficient
715 // API (since the API is currently unnecessarily resolving addresses for
716 // this use case which only wants to validate the expressions themselves) &
717 // then the expressions could be validated even if the addresses can't be
718 // resolved.
719 // That sort of API would probably look like a callback "for each
720 // expression" with some way to lazily resolve the address ranges when
721 // needed (& then the existing API used here could be built on top of that -
722 // using the callback API to build the data structure and return it).
723 if (Expected<std::vector<DWARFLocationExpression>> Loc =
724 Die.getLocations(DW_AT_location)) {
725 for (const auto &Entry : *Loc) {
727 DWARFExpression Expression(Data, U->getAddressByteSize(),
728 U->getFormParams().Format);
729 bool Error =
731 return Op.isError();
732 });
733 if (Error || !Expression.verify(U))
734 ReportError("Invalid DWARF expressions",
735 "DIE contains invalid DWARF expression:");
736 }
737 } else if (Error Err = handleErrors(
738 Loc.takeError(), [&](std::unique_ptr<ResolverError> E) {
739 return U->isDWOUnit() ? Error::success()
740 : Error(std::move(E));
741 }))
742 ReportError("Invalid DW_AT_location", toString(std::move(Err)));
743 break;
744 }
745 case DW_AT_specification:
746 case DW_AT_abstract_origin: {
747 if (auto ReferencedDie = Die.getAttributeValueAsReferencedDie(Attr)) {
748 auto DieTag = Die.getTag();
749 auto RefTag = ReferencedDie.getTag();
750 if (DieTag == RefTag)
751 break;
752 if (DieTag == DW_TAG_inlined_subroutine && RefTag == DW_TAG_subprogram)
753 break;
754 if (DieTag == DW_TAG_variable && RefTag == DW_TAG_member)
755 break;
756 // This might be reference to a function declaration.
757 if (DieTag == DW_TAG_GNU_call_site && RefTag == DW_TAG_subprogram)
758 break;
759 ReportError("Incompatible DW_AT_abstract_origin tag reference",
760 "DIE with tag " + TagString(DieTag) + " has " +
761 AttributeString(Attr) +
762 " that points to DIE with "
763 "incompatible tag " +
764 TagString(RefTag));
765 }
766 break;
767 }
768 case DW_AT_type: {
769 DWARFDie TypeDie = Die.getAttributeValueAsReferencedDie(DW_AT_type);
770 if (TypeDie && !isType(TypeDie.getTag())) {
771 ReportError("Incompatible DW_AT_type attribute tag",
772 "DIE has " + AttributeString(Attr) +
773 " with incompatible tag " + TagString(TypeDie.getTag()));
774 }
775 break;
776 }
777 case DW_AT_call_file:
778 case DW_AT_decl_file: {
779 if (auto FileIdx = AttrValue.Value.getAsUnsignedConstant()) {
780 if (U->isDWOUnit() && !U->isTypeUnit())
781 break;
782 const auto *LT = U->getContext().getLineTableForUnit(U);
783 if (LT) {
784 if (!LT->hasFileAtIndex(*FileIdx)) {
785 bool IsZeroIndexed = LT->Prologue.getVersion() >= 5;
786 if (std::optional<uint64_t> LastFileIdx =
787 LT->getLastValidFileIndex()) {
788 ReportError("Invalid file index in DW_AT_decl_file",
789 "DIE has " + AttributeString(Attr) +
790 " with an invalid file index " +
791 llvm::formatv("{0}", *FileIdx) +
792 " (valid values are [" +
793 (IsZeroIndexed ? "0-" : "1-") +
794 llvm::formatv("{0}", *LastFileIdx) + "])");
795 } else {
796 ReportError("Invalid file index in DW_AT_decl_file",
797 "DIE has " + AttributeString(Attr) +
798 " with an invalid file index " +
799 llvm::formatv("{0}", *FileIdx) +
800 " (the file table in the prologue is empty)");
801 }
802 }
803 } else {
804 ReportError(
805 "File index in DW_AT_decl_file reference CU with no line table",
806 "DIE has " + AttributeString(Attr) +
807 " that references a file with index " +
808 llvm::formatv("{0}", *FileIdx) +
809 " and the compile unit has no line table");
810 }
811 } else {
812 ReportError("Invalid encoding in DW_AT_decl_file",
813 "DIE has " + AttributeString(Attr) +
814 " with invalid encoding");
815 }
816 break;
817 }
818 case DW_AT_call_line:
819 case DW_AT_decl_line: {
820 if (!AttrValue.Value.getAsUnsignedConstant()) {
821 ReportError(
822 Attr == DW_AT_call_line ? "Invalid file index in DW_AT_decl_line"
823 : "Invalid file index in DW_AT_call_line",
824 "DIE has " + AttributeString(Attr) + " with invalid encoding");
825 }
826 break;
827 }
828 default:
829 break;
830 }
831 return NumErrors;
832}
833
834unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
835 DWARFAttribute &AttrValue,
836 ReferenceMap &LocalReferences,
837 ReferenceMap &CrossUnitReferences) {
838 auto DieCU = Die.getDwarfUnit();
839 unsigned NumErrors = 0;
840 const auto Form = AttrValue.Value.getForm();
841 switch (Form) {
842 case DW_FORM_ref1:
843 case DW_FORM_ref2:
844 case DW_FORM_ref4:
845 case DW_FORM_ref8:
846 case DW_FORM_ref_udata: {
847 // Verify all CU relative references are valid CU offsets.
848 std::optional<uint64_t> RefVal = AttrValue.Value.getAsRelativeReference();
849 assert(RefVal);
850 if (RefVal) {
851 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
852 auto CUOffset = AttrValue.Value.getRawUValue();
853 if (CUOffset >= CUSize) {
854 ++NumErrors;
855 ErrorCategory.Report("Invalid CU offset", [&]() {
856 error() << FormEncodingString(Form) << " CU offset "
857 << format("0x%08" PRIx64, CUOffset)
858 << " is invalid (must be less than CU size of "
859 << format("0x%08" PRIx64, CUSize) << "):\n";
860 Die.dump(OS, 0, DumpOpts);
861 dump(Die) << '\n';
862 });
863 } else {
864 // Valid reference, but we will verify it points to an actual
865 // DIE later.
866 LocalReferences[AttrValue.Value.getUnit()->getOffset() + *RefVal]
867 .insert(Die.getOffset());
868 }
869 }
870 break;
871 }
872 case DW_FORM_ref_addr: {
873 // Verify all absolute DIE references have valid offsets in the
874 // .debug_info section.
875 std::optional<uint64_t> RefVal = AttrValue.Value.getAsDebugInfoReference();
876 assert(RefVal);
877 if (RefVal) {
878 if (*RefVal >= DieCU->getInfoSection().Data.size()) {
879 ++NumErrors;
880 ErrorCategory.Report("DW_FORM_ref_addr offset out of bounds", [&]() {
881 error() << "DW_FORM_ref_addr offset beyond .debug_info "
882 "bounds:\n";
883 dump(Die) << '\n';
884 });
885 } else {
886 // Valid reference, but we will verify it points to an actual
887 // DIE later.
888 CrossUnitReferences[*RefVal].insert(Die.getOffset());
889 }
890 }
891 break;
892 }
893 case DW_FORM_strp:
894 case DW_FORM_strx:
895 case DW_FORM_strx1:
896 case DW_FORM_strx2:
897 case DW_FORM_strx3:
898 case DW_FORM_strx4:
899 case DW_FORM_line_strp: {
900 if (Error E = AttrValue.Value.getAsCString().takeError()) {
901 ++NumErrors;
902 std::string ErrMsg = toString(std::move(E));
903 ErrorCategory.Report("Invalid DW_FORM attribute", [&]() {
904 error() << ErrMsg << ":\n";
905 dump(Die) << '\n';
906 });
907 }
908 break;
909 }
910 default:
911 break;
912 }
913 return NumErrors;
914}
915
916unsigned DWARFVerifier::verifyDebugInfoReferences(
917 const ReferenceMap &References,
918 llvm::function_ref<DWARFUnit *(uint64_t)> GetUnitForOffset) {
919 auto GetDIEForOffset = [&](uint64_t Offset) {
920 if (DWARFUnit *U = GetUnitForOffset(Offset))
921 return U->getDIEForOffset(Offset);
922 return DWARFDie();
923 };
924 unsigned NumErrors = 0;
925 for (const std::pair<const uint64_t, std::set<uint64_t>> &Pair :
926 References) {
927 if (GetDIEForOffset(Pair.first))
928 continue;
929 ++NumErrors;
930 ErrorCategory.Report("Invalid DIE reference", [&]() {
931 error() << "invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
932 << ". Offset is in between DIEs:\n";
933 for (auto Offset : Pair.second)
934 dump(GetDIEForOffset(Offset)) << '\n';
935 OS << "\n";
936 });
937 }
938 return NumErrors;
939}
940
941void DWARFVerifier::verifyDebugLineStmtOffsets() {
942 std::map<uint64_t, DWARFDie> StmtListToDie;
943 for (const auto &CU : DCtx.compile_units()) {
944 auto Die = CU->getUnitDIE();
945 // Get the attribute value as a section offset. No need to produce an
946 // error here if the encoding isn't correct because we validate this in
947 // the .debug_info verifier.
948 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
949 if (!StmtSectionOffset)
950 continue;
951 const uint64_t LineTableOffset = *StmtSectionOffset;
952 auto LineTable = DCtx.getLineTableForUnit(CU.get());
953 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
954 if (!LineTable) {
955 ++NumDebugLineErrors;
956 ErrorCategory.Report("Unparsable .debug_line entry", [&]() {
957 error() << ".debug_line[" << format("0x%08" PRIx64, LineTableOffset)
958 << "] was not able to be parsed for CU:\n";
959 dump(Die) << '\n';
960 });
961 continue;
962 }
963 } else {
964 // Make sure we don't get a valid line table back if the offset is wrong.
965 assert(LineTable == nullptr);
966 // Skip this line table as it isn't valid. No need to create an error
967 // here because we validate this in the .debug_info verifier.
968 continue;
969 }
970 auto [Iter, Inserted] = StmtListToDie.try_emplace(LineTableOffset, Die);
971 if (!Inserted) {
972 ++NumDebugLineErrors;
973 const auto &OldDie = Iter->second;
974 ErrorCategory.Report("Identical DW_AT_stmt_list section offset", [&]() {
975 error() << "two compile unit DIEs, "
976 << format("0x%08" PRIx64, OldDie.getOffset()) << " and "
977 << format("0x%08" PRIx64, Die.getOffset())
978 << ", have the same DW_AT_stmt_list section offset:\n";
979 dump(OldDie);
980 dump(Die) << '\n';
981 });
982 // Already verified this line table before, no need to do it again.
983 }
984 }
985}
986
987void DWARFVerifier::verifyDebugLineRows() {
988 for (const auto &CU : DCtx.compile_units()) {
989 auto Die = CU->getUnitDIE();
990 auto LineTable = DCtx.getLineTableForUnit(CU.get());
991 // If there is no line table we will have created an error in the
992 // .debug_info verifier or in verifyDebugLineStmtOffsets().
993 if (!LineTable)
994 continue;
995
996 // Verify prologue.
997 bool isDWARF5 = LineTable->Prologue.getVersion() >= 5;
998 uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size();
999 uint32_t MinFileIndex = isDWARF5 ? 0 : 1;
1000 uint32_t FileIndex = MinFileIndex;
1001 StringMap<uint16_t> FullPathMap;
1002 for (const auto &FileName : LineTable->Prologue.FileNames) {
1003 // Verify directory index.
1004 if (FileName.DirIdx > MaxDirIndex) {
1005 ++NumDebugLineErrors;
1006 ErrorCategory.Report(
1007 "Invalid index in .debug_line->prologue.file_names->dir_idx",
1008 [&]() {
1009 error() << ".debug_line["
1010 << format("0x%08" PRIx64,
1011 *toSectionOffset(Die.find(DW_AT_stmt_list)))
1012 << "].prologue.file_names[" << FileIndex
1013 << "].dir_idx contains an invalid index: "
1014 << FileName.DirIdx << "\n";
1015 });
1016 }
1017
1018 // Check file paths for duplicates.
1019 std::string FullPath;
1020 const bool HasFullPath = LineTable->getFileNameByIndex(
1021 FileIndex, CU->getCompilationDir(),
1022 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FullPath);
1023 assert(HasFullPath && "Invalid index?");
1024 (void)HasFullPath;
1025 auto [It, Inserted] = FullPathMap.try_emplace(FullPath, FileIndex);
1026 if (!Inserted && It->second != FileIndex && DumpOpts.Verbose) {
1027 warn() << ".debug_line["
1028 << format("0x%08" PRIx64,
1029 *toSectionOffset(Die.find(DW_AT_stmt_list)))
1030 << "].prologue.file_names[" << FileIndex
1031 << "] is a duplicate of file_names[" << It->second << "]\n";
1032 }
1033
1034 FileIndex++;
1035 }
1036
1037 // Nothing to verify in a line table with a single row containing the end
1038 // sequence.
1039 if (LineTable->Rows.size() == 1 && LineTable->Rows.front().EndSequence)
1040 continue;
1041
1042 // Verify rows.
1043 uint64_t PrevAddress = 0;
1044 uint32_t RowIndex = 0;
1045 for (const auto &Row : LineTable->Rows) {
1046 // Verify row address.
1047 if (Row.Address.Address < PrevAddress) {
1048 ++NumDebugLineErrors;
1049 ErrorCategory.Report(
1050 "decreasing address between debug_line rows", [&]() {
1051 error() << ".debug_line["
1052 << format("0x%08" PRIx64,
1053 *toSectionOffset(Die.find(DW_AT_stmt_list)))
1054 << "] row[" << RowIndex
1055 << "] decreases in address from previous row:\n";
1056
1058 if (RowIndex > 0)
1059 LineTable->Rows[RowIndex - 1].dump(OS);
1060 Row.dump(OS);
1061 OS << '\n';
1062 });
1063 }
1064
1065 if (!LineTable->hasFileAtIndex(Row.File)) {
1066 ++NumDebugLineErrors;
1067 ErrorCategory.Report("Invalid file index in debug_line", [&]() {
1068 error() << ".debug_line["
1069 << format("0x%08" PRIx64,
1070 *toSectionOffset(Die.find(DW_AT_stmt_list)))
1071 << "][" << RowIndex << "] has invalid file index " << Row.File
1072 << " (valid values are [" << MinFileIndex << ','
1073 << LineTable->Prologue.FileNames.size()
1074 << (isDWARF5 ? ")" : "]") << "):\n";
1076 Row.dump(OS);
1077 OS << '\n';
1078 });
1079 }
1080 if (Row.EndSequence)
1081 PrevAddress = 0;
1082 else
1083 PrevAddress = Row.Address.Address;
1084 ++RowIndex;
1085 }
1086 }
1087}
1088
1090 DIDumpOptions DumpOpts)
1091 : OS(S), DCtx(D), DumpOpts(std::move(DumpOpts)), IsObjectFile(false),
1092 IsMachOObject(false) {
1093 ErrorCategory.ShowDetail(this->DumpOpts.Verbose ||
1094 !this->DumpOpts.ShowAggregateErrors);
1095 if (const auto *F = DCtx.getDWARFObj().getFile()) {
1096 IsObjectFile = F->isRelocatableObject();
1097 IsMachOObject = F->isMachO();
1098 }
1099}
1100
1102 NumDebugLineErrors = 0;
1103 OS << "Verifying .debug_line...\n";
1104 verifyDebugLineStmtOffsets();
1105 verifyDebugLineRows();
1106 return NumDebugLineErrors == 0;
1107}
1108
1109unsigned DWARFVerifier::verifyAppleAccelTable(const DWARFSection *AccelSection,
1110 DataExtractor *StrData,
1111 const char *SectionName) {
1112 unsigned NumErrors = 0;
1113 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
1114 DCtx.isLittleEndian(), 0);
1115 AppleAcceleratorTable AccelTable(AccelSectionData, *StrData);
1116
1117 OS << "Verifying " << SectionName << "...\n";
1118
1119 // Verify that the fixed part of the header is not too short.
1120 if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) {
1121 ErrorCategory.Report("Section is too small to fit a section header", [&]() {
1122 error() << "Section is too small to fit a section header.\n";
1123 });
1124 return 1;
1125 }
1126
1127 // Verify that the section is not too short.
1128 if (Error E = AccelTable.extract()) {
1129 std::string Msg = toString(std::move(E));
1130 ErrorCategory.Report("Section is too small to fit a section header",
1131 [&]() { error() << Msg << '\n'; });
1132 return 1;
1133 }
1134
1135 // Verify that all buckets have a valid hash index or are empty.
1136 uint32_t NumBuckets = AccelTable.getNumBuckets();
1137 uint32_t NumHashes = AccelTable.getNumHashes();
1138
1139 uint64_t BucketsOffset =
1140 AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength();
1141 uint64_t HashesBase = BucketsOffset + NumBuckets * 4;
1142 uint64_t OffsetsBase = HashesBase + NumHashes * 4;
1143 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
1144 uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset);
1145 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
1146 ErrorCategory.Report("Invalid hash index", [&]() {
1147 error() << format("Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
1148 HashIdx);
1149 });
1150 ++NumErrors;
1151 }
1152 }
1153 uint32_t NumAtoms = AccelTable.getAtomsDesc().size();
1154 if (NumAtoms == 0) {
1155 ErrorCategory.Report("No atoms", [&]() {
1156 error() << "No atoms: failed to read HashData.\n";
1157 });
1158 return 1;
1159 }
1160 if (!AccelTable.validateForms()) {
1161 ErrorCategory.Report("Unsupported form", [&]() {
1162 error() << "Unsupported form: failed to read HashData.\n";
1163 });
1164 return 1;
1165 }
1166
1167 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
1168 uint64_t HashOffset = HashesBase + 4 * HashIdx;
1169 uint64_t DataOffset = OffsetsBase + 4 * HashIdx;
1170 uint32_t Hash = AccelSectionData.getU32(&HashOffset);
1171 uint64_t HashDataOffset = AccelSectionData.getU32(&DataOffset);
1172 if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset,
1173 sizeof(uint64_t))) {
1174 ErrorCategory.Report("Invalid HashData offset", [&]() {
1175 error() << format("Hash[%d] has invalid HashData offset: "
1176 "0x%08" PRIx64 ".\n",
1177 HashIdx, HashDataOffset);
1178 });
1179 ++NumErrors;
1180 }
1181
1182 uint64_t StrpOffset;
1183 uint64_t StringOffset;
1184 uint32_t StringCount = 0;
1186 unsigned Tag;
1187 while ((StrpOffset = AccelSectionData.getU32(&HashDataOffset)) != 0) {
1188 const uint32_t NumHashDataObjects =
1189 AccelSectionData.getU32(&HashDataOffset);
1190 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
1191 ++HashDataIdx) {
1192 std::tie(Offset, Tag) = AccelTable.readAtoms(&HashDataOffset);
1193 auto Die = DCtx.getDIEForOffset(Offset);
1194 if (!Die) {
1195 const uint32_t BucketIdx =
1196 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
1197 StringOffset = StrpOffset;
1198 const char *Name = StrData->getCStr(&StringOffset);
1199 if (!Name)
1200 Name = "<NULL>";
1201
1202 ErrorCategory.Report("Invalid DIE offset", [&]() {
1203 error() << format(
1204 "%s Bucket[%d] Hash[%d] = 0x%08x "
1205 "Str[%u] = 0x%08" PRIx64 " DIE[%d] = 0x%08" PRIx64 " "
1206 "is not a valid DIE offset for \"%s\".\n",
1207 SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset,
1208 HashDataIdx, Offset, Name);
1209 });
1210
1211 ++NumErrors;
1212 continue;
1213 }
1214 if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
1215 ErrorCategory.Report("Mismatched Tag in accellerator table", [&]() {
1216 error() << "Tag " << dwarf::TagString(Tag)
1217 << " in accelerator table does not match Tag "
1218 << dwarf::TagString(Die.getTag()) << " of DIE["
1219 << HashDataIdx << "].\n";
1220 });
1221 ++NumErrors;
1222 }
1223 }
1224 ++StringCount;
1225 }
1226 }
1227 return NumErrors;
1228}
1229
1230unsigned
1231DWARFVerifier::verifyDebugNamesCULists(const DWARFDebugNames &AccelTable) {
1232 // A map from CU offset to the (first) Name Index offset which claims to index
1233 // this CU.
1235 const uint64_t NotIndexed = std::numeric_limits<uint64_t>::max();
1236
1237 CUMap.reserve(DCtx.getNumCompileUnits());
1238 for (const auto &CU : DCtx.compile_units())
1239 CUMap[CU->getOffset()] = NotIndexed;
1240
1241 unsigned NumErrors = 0;
1242 for (const DWARFDebugNames::NameIndex &NI : AccelTable) {
1243 if (NI.getCUCount() == 0) {
1244 ErrorCategory.Report("Name Index doesn't index any CU", [&]() {
1245 error() << formatv("Name Index @ {0:x} does not index any CU\n",
1246 NI.getUnitOffset());
1247 });
1248 ++NumErrors;
1249 continue;
1250 }
1251 for (uint32_t CU = 0, End = NI.getCUCount(); CU < End; ++CU) {
1252 uint64_t Offset = NI.getCUOffset(CU);
1253 auto Iter = CUMap.find(Offset);
1254
1255 if (Iter == CUMap.end()) {
1256 ErrorCategory.Report("Name Index references non-existing CU", [&]() {
1257 error() << formatv(
1258 "Name Index @ {0:x} references a non-existing CU @ {1:x}\n",
1259 NI.getUnitOffset(), Offset);
1260 });
1261 ++NumErrors;
1262 continue;
1263 }
1264
1265 if (Iter->second != NotIndexed) {
1266 ErrorCategory.Report("Duplicate Name Index", [&]() {
1267 error() << formatv(
1268 "Name Index @ {0:x} references a CU @ {1:x}, but "
1269 "this CU is already indexed by Name Index @ {2:x}\n",
1270 NI.getUnitOffset(), Offset, Iter->second);
1271 });
1272 continue;
1273 }
1274 Iter->second = NI.getUnitOffset();
1275 }
1276 }
1277
1278 for (const auto &KV : CUMap) {
1279 if (KV.second == NotIndexed)
1280 warn() << formatv("CU @ {0:x} not covered by any Name Index\n", KV.first);
1281 }
1282
1283 return NumErrors;
1284}
1285
1286unsigned
1287DWARFVerifier::verifyNameIndexBuckets(const DWARFDebugNames::NameIndex &NI,
1288 const DataExtractor &StrData) {
1289 struct BucketInfo {
1290 uint32_t Bucket;
1292
1293 constexpr BucketInfo(uint32_t Bucket, uint32_t Index)
1294 : Bucket(Bucket), Index(Index) {}
1295 bool operator<(const BucketInfo &RHS) const { return Index < RHS.Index; }
1296 };
1297
1298 uint32_t NumErrors = 0;
1299 if (NI.getBucketCount() == 0) {
1300 warn() << formatv("Name Index @ {0:x} does not contain a hash table.\n",
1301 NI.getUnitOffset());
1302 return NumErrors;
1303 }
1304
1305 // Build up a list of (Bucket, Index) pairs. We use this later to verify that
1306 // each Name is reachable from the appropriate bucket.
1307 std::vector<BucketInfo> BucketStarts;
1308 BucketStarts.reserve(NI.getBucketCount() + 1);
1309 for (uint32_t Bucket = 0, End = NI.getBucketCount(); Bucket < End; ++Bucket) {
1310 uint32_t Index = NI.getBucketArrayEntry(Bucket);
1311 if (Index > NI.getNameCount()) {
1312 ErrorCategory.Report("Name Index Bucket contains invalid value", [&]() {
1313 error() << formatv("Bucket {0} of Name Index @ {1:x} contains invalid "
1314 "value {2}. Valid range is [0, {3}].\n",
1315 Bucket, NI.getUnitOffset(), Index,
1316 NI.getNameCount());
1317 });
1318 ++NumErrors;
1319 continue;
1320 }
1321 if (Index > 0)
1322 BucketStarts.emplace_back(Bucket, Index);
1323 }
1324
1325 // If there were any buckets with invalid values, skip further checks as they
1326 // will likely produce many errors which will only confuse the actual root
1327 // problem.
1328 if (NumErrors > 0)
1329 return NumErrors;
1330
1331 // Sort the list in the order of increasing "Index" entries.
1332 array_pod_sort(BucketStarts.begin(), BucketStarts.end());
1333
1334 // Insert a sentinel entry at the end, so we can check that the end of the
1335 // table is covered in the loop below.
1336 BucketStarts.emplace_back(NI.getBucketCount(), NI.getNameCount() + 1);
1337
1338 // Loop invariant: NextUncovered is the (1-based) index of the first Name
1339 // which is not reachable by any of the buckets we processed so far (and
1340 // hasn't been reported as uncovered).
1341 uint32_t NextUncovered = 1;
1342 for (const BucketInfo &B : BucketStarts) {
1343 // Under normal circumstances B.Index be equal to NextUncovered, but it can
1344 // be less if a bucket points to names which are already known to be in some
1345 // bucket we processed earlier. In that case, we won't trigger this error,
1346 // but report the mismatched hash value error instead. (We know the hash
1347 // will not match because we have already verified that the name's hash
1348 // puts it into the previous bucket.)
1349 if (B.Index > NextUncovered) {
1350 ErrorCategory.Report("Name table entries uncovered by hash table", [&]() {
1351 error() << formatv("Name Index @ {0:x}: Name table entries [{1}, {2}] "
1352 "are not covered by the hash table.\n",
1353 NI.getUnitOffset(), NextUncovered, B.Index - 1);
1354 });
1355 ++NumErrors;
1356 }
1357 uint32_t Idx = B.Index;
1358
1359 // The rest of the checks apply only to non-sentinel entries.
1360 if (B.Bucket == NI.getBucketCount())
1361 break;
1362
1363 // This triggers if a non-empty bucket points to a name with a mismatched
1364 // hash. Clients are likely to interpret this as an empty bucket, because a
1365 // mismatched hash signals the end of a bucket, but if this is indeed an
1366 // empty bucket, the producer should have signalled this by marking the
1367 // bucket as empty.
1368 uint32_t FirstHash = NI.getHashArrayEntry(Idx);
1369 if (FirstHash % NI.getBucketCount() != B.Bucket) {
1370 ErrorCategory.Report("Name Index point to mismatched hash value", [&]() {
1371 error() << formatv(
1372 "Name Index @ {0:x}: Bucket {1} is not empty but points to a "
1373 "mismatched hash value {2:x} (belonging to bucket {3}).\n",
1374 NI.getUnitOffset(), B.Bucket, FirstHash,
1375 FirstHash % NI.getBucketCount());
1376 });
1377 ++NumErrors;
1378 }
1379
1380 // This find the end of this bucket and also verifies that all the hashes in
1381 // this bucket are correct by comparing the stored hashes to the ones we
1382 // compute ourselves.
1383 while (Idx <= NI.getNameCount()) {
1384 uint32_t Hash = NI.getHashArrayEntry(Idx);
1385 if (Hash % NI.getBucketCount() != B.Bucket)
1386 break;
1387
1388 const char *Str = NI.getNameTableEntry(Idx).getString();
1389 if (caseFoldingDjbHash(Str) != Hash) {
1390 ErrorCategory.Report(
1391 "String hash doesn't match Name Index hash", [&]() {
1392 error() << formatv(
1393 "Name Index @ {0:x}: String ({1}) at index {2} "
1394 "hashes to {3:x}, but "
1395 "the Name Index hash is {4:x}\n",
1396 NI.getUnitOffset(), Str, Idx, caseFoldingDjbHash(Str), Hash);
1397 });
1398 ++NumErrors;
1399 }
1400
1401 ++Idx;
1402 }
1403 NextUncovered = std::max(NextUncovered, Idx);
1404 }
1405 return NumErrors;
1406}
1407
1408unsigned DWARFVerifier::verifyNameIndexAttribute(
1411 StringRef FormName = dwarf::FormEncodingString(AttrEnc.Form);
1412 if (FormName.empty()) {
1413 ErrorCategory.Report("Unknown NameIndex Abbreviation", [&]() {
1414 error() << formatv("NameIndex @ {0:x}: Abbreviation {1:x}: {2} uses an "
1415 "unknown form: {3}.\n",
1416 NI.getUnitOffset(), Abbr.Code, AttrEnc.Index,
1417 AttrEnc.Form);
1418 });
1419 return 1;
1420 }
1421
1422 if (AttrEnc.Index == DW_IDX_type_hash) {
1423 if (AttrEnc.Form != dwarf::DW_FORM_data8) {
1424 ErrorCategory.Report("Unexpected NameIndex Abbreviation", [&]() {
1425 error() << formatv(
1426 "NameIndex @ {0:x}: Abbreviation {1:x}: DW_IDX_type_hash "
1427 "uses an unexpected form {2} (should be {3}).\n",
1428 NI.getUnitOffset(), Abbr.Code, AttrEnc.Form, dwarf::DW_FORM_data8);
1429 });
1430 return 1;
1431 }
1432 return 0;
1433 }
1434
1435 if (AttrEnc.Index == dwarf::DW_IDX_parent) {
1436 constexpr static auto AllowedForms = {dwarf::Form::DW_FORM_flag_present,
1437 dwarf::Form::DW_FORM_ref4};
1438 if (!is_contained(AllowedForms, AttrEnc.Form)) {
1439 ErrorCategory.Report("Unexpected NameIndex Abbreviation", [&]() {
1440 error() << formatv(
1441 "NameIndex @ {0:x}: Abbreviation {1:x}: DW_IDX_parent "
1442 "uses an unexpected form {2} (should be "
1443 "DW_FORM_ref4 or DW_FORM_flag_present).\n",
1444 NI.getUnitOffset(), Abbr.Code, AttrEnc.Form);
1445 });
1446 return 1;
1447 }
1448 return 0;
1449 }
1450
1451 // A list of known index attributes and their expected form classes.
1452 // DW_IDX_type_hash is handled specially in the check above, as it has a
1453 // specific form (not just a form class) we should expect.
1454 struct FormClassTable {
1457 StringLiteral ClassName;
1458 };
1459 static constexpr FormClassTable Table[] = {
1460 {dwarf::DW_IDX_compile_unit, DWARFFormValue::FC_Constant, {"constant"}},
1461 {dwarf::DW_IDX_type_unit, DWARFFormValue::FC_Constant, {"constant"}},
1462 {dwarf::DW_IDX_die_offset, DWARFFormValue::FC_Reference, {"reference"}},
1463 };
1464
1466 auto Iter = find_if(TableRef, [AttrEnc](const FormClassTable &T) {
1467 return T.Index == AttrEnc.Index;
1468 });
1469 if (Iter == TableRef.end()) {
1470 warn() << formatv("NameIndex @ {0:x}: Abbreviation {1:x} contains an "
1471 "unknown index attribute: {2}.\n",
1472 NI.getUnitOffset(), Abbr.Code, AttrEnc.Index);
1473 return 0;
1474 }
1475
1476 if (!DWARFFormValue(AttrEnc.Form).isFormClass(Iter->Class)) {
1477 ErrorCategory.Report("Unexpected NameIndex Abbreviation", [&]() {
1478 error() << formatv("NameIndex @ {0:x}: Abbreviation {1:x}: {2} uses an "
1479 "unexpected form {3} (expected form class {4}).\n",
1480 NI.getUnitOffset(), Abbr.Code, AttrEnc.Index,
1481 AttrEnc.Form, Iter->ClassName);
1482 });
1483 return 1;
1484 }
1485 return 0;
1486}
1487
1488unsigned
1489DWARFVerifier::verifyNameIndexAbbrevs(const DWARFDebugNames::NameIndex &NI) {
1490 unsigned NumErrors = 0;
1491 for (const auto &Abbrev : NI.getAbbrevs()) {
1492 StringRef TagName = dwarf::TagString(Abbrev.Tag);
1493 if (TagName.empty()) {
1494 warn() << formatv("NameIndex @ {0:x}: Abbreviation {1:x} references an "
1495 "unknown tag: {2}.\n",
1496 NI.getUnitOffset(), Abbrev.Code, Abbrev.Tag);
1497 }
1499 for (const auto &AttrEnc : Abbrev.Attributes) {
1500 if (!Attributes.insert(AttrEnc.Index).second) {
1501 ErrorCategory.Report(
1502 "NameIndex Abbreviateion contains multiple attributes", [&]() {
1503 error() << formatv(
1504 "NameIndex @ {0:x}: Abbreviation {1:x} contains "
1505 "multiple {2} attributes.\n",
1506 NI.getUnitOffset(), Abbrev.Code, AttrEnc.Index);
1507 });
1508 ++NumErrors;
1509 continue;
1510 }
1511 NumErrors += verifyNameIndexAttribute(NI, Abbrev, AttrEnc);
1512 }
1513
1514 if (NI.getCUCount() > 1 && !Attributes.count(dwarf::DW_IDX_compile_unit) &&
1515 !Attributes.count(dwarf::DW_IDX_type_unit)) {
1516 ErrorCategory.Report("Abbreviation contains no attribute", [&]() {
1517 error() << formatv("NameIndex @ {0:x}: Indexing multiple compile units "
1518 "and abbreviation {1:x} has no DW_IDX_compile_unit "
1519 "or DW_IDX_type_unit attribute.\n",
1520 NI.getUnitOffset(), Abbrev.Code);
1521 });
1522 ++NumErrors;
1523 }
1524 if (!Attributes.count(dwarf::DW_IDX_die_offset)) {
1525 ErrorCategory.Report("Abbreviate in NameIndex missing attribute", [&]() {
1526 error() << formatv(
1527 "NameIndex @ {0:x}: Abbreviation {1:x} has no {2} attribute.\n",
1528 NI.getUnitOffset(), Abbrev.Code, dwarf::DW_IDX_die_offset);
1529 });
1530 ++NumErrors;
1531 }
1532 }
1533 return NumErrors;
1534}
1535
1537 bool IncludeStrippedTemplateNames,
1538 bool IncludeObjCNames = true,
1539 bool IncludeLinkageName = true) {
1541 if (const char *Str = DIE.getShortName()) {
1542 StringRef Name(Str);
1543 Result.emplace_back(Name);
1544 if (IncludeStrippedTemplateNames) {
1545 if (std::optional<StringRef> StrippedName =
1546 StripTemplateParameters(Result.back()))
1547 // Convert to std::string and push; emplacing the StringRef may trigger
1548 // a vector resize which may destroy the StringRef memory.
1549 Result.push_back(StrippedName->str());
1550 }
1551
1552 if (IncludeObjCNames) {
1553 if (std::optional<ObjCSelectorNames> ObjCNames =
1555 Result.emplace_back(ObjCNames->ClassName);
1556 Result.emplace_back(ObjCNames->Selector);
1557 if (ObjCNames->ClassNameNoCategory)
1558 Result.emplace_back(*ObjCNames->ClassNameNoCategory);
1559 if (ObjCNames->MethodNameNoCategory)
1560 Result.push_back(std::move(*ObjCNames->MethodNameNoCategory));
1561 }
1562 }
1563 } else if (DIE.getTag() == dwarf::DW_TAG_namespace)
1564 Result.emplace_back("(anonymous namespace)");
1565
1566 if (IncludeLinkageName) {
1567 if (const char *Str = DIE.getLinkageName())
1568 Result.emplace_back(Str);
1569 }
1570
1571 return Result;
1572}
1573
1574unsigned DWARFVerifier::verifyNameIndexEntries(
1577 const char *CStr = NTE.getString();
1578 if (!CStr) {
1579 ErrorCategory.Report("Unable to get string associated with name", [&]() {
1580 error() << formatv("Name Index @ {0:x}: Unable to get string associated "
1581 "with name {1}.\n",
1582 NI.getUnitOffset(), NTE.getIndex());
1583 });
1584 return 1;
1585 }
1586 StringRef Str(CStr);
1587
1588 unsigned NumErrors = 0;
1589 unsigned NumEntries = 0;
1590 uint64_t EntryID = NTE.getEntryOffset();
1591 uint64_t NextEntryID = EntryID;
1592 Expected<DWARFDebugNames::Entry> EntryOr = NI.getEntry(&NextEntryID);
1593 for (; EntryOr; ++NumEntries, EntryID = NextEntryID,
1594 EntryOr = NI.getEntry(&NextEntryID)) {
1595
1596 std::optional<uint64_t> CUIndex = EntryOr->getRelatedCUIndex();
1597 std::optional<uint64_t> TUIndex = EntryOr->getTUIndex();
1598 if (CUIndex && *CUIndex >= NI.getCUCount()) {
1599 ErrorCategory.Report("Name Index entry contains invalid CU index", [&]() {
1600 error() << formatv("Name Index @ {0:x}: Entry @ {1:x} contains an "
1601 "invalid CU index ({2}).\n",
1602 NI.getUnitOffset(), EntryID, *CUIndex);
1603 });
1604 ++NumErrors;
1605 continue;
1606 }
1607 const uint32_t NumLocalTUs = NI.getLocalTUCount();
1608 const uint32_t NumForeignTUs = NI.getForeignTUCount();
1609 if (TUIndex && *TUIndex >= (NumLocalTUs + NumForeignTUs)) {
1610 ErrorCategory.Report("Name Index entry contains invalid TU index", [&]() {
1611 error() << formatv("Name Index @ {0:x}: Entry @ {1:x} contains an "
1612 "invalid TU index ({2}).\n",
1613 NI.getUnitOffset(), EntryID, *TUIndex);
1614 });
1615 ++NumErrors;
1616 continue;
1617 }
1618 std::optional<uint64_t> UnitOffset;
1619 if (TUIndex) {
1620 // We have a local or foreign type unit.
1621 if (*TUIndex >= NumLocalTUs) {
1622 // This is a foreign type unit, we will find the right type unit by
1623 // type unit signature later in this function.
1624
1625 // Foreign type units must have a valid CU index, either from a
1626 // DW_IDX_comp_unit attribute value or from the .debug_names table only
1627 // having a single compile unit. We need the originating compile unit
1628 // because foreign type units can come from any .dwo file, yet only one
1629 // copy of the type unit will end up in the .dwp file.
1630 if (CUIndex) {
1631 // We need the local skeleton unit offset for the code below.
1632 UnitOffset = NI.getCUOffset(*CUIndex);
1633 } else {
1634 ErrorCategory.Report(
1635 "Name Index entry contains foreign TU index with invalid CU "
1636 "index",
1637 [&]() {
1638 error() << formatv(
1639 "Name Index @ {0:x}: Entry @ {1:x} contains an "
1640 "foreign TU index ({2}) with no CU index.\n",
1641 NI.getUnitOffset(), EntryID, *TUIndex);
1642 });
1643 ++NumErrors;
1644 continue;
1645 }
1646 } else {
1647 // Local type unit, get the DWARF unit offset for the type unit.
1648 UnitOffset = NI.getLocalTUOffset(*TUIndex);
1649 }
1650 } else if (CUIndex) {
1651 // Local CU entry, get the DWARF unit offset for the CU.
1652 UnitOffset = NI.getCUOffset(*CUIndex);
1653 }
1654
1655 // Watch for tombstoned type unit entries.
1656 if (!UnitOffset || UnitOffset == UINT32_MAX)
1657 continue;
1658 // For split DWARF entries we need to make sure we find the non skeleton
1659 // DWARF unit that is needed and use that's DWARF unit offset as the
1660 // DIE offset to add the DW_IDX_die_offset to.
1661 DWARFUnit *DU = DCtx.getUnitForOffset(*UnitOffset);
1662 if (DU == nullptr || DU->getOffset() != *UnitOffset) {
1663 // If we didn't find a DWARF Unit from the UnitOffset, or if the offset
1664 // of the unit doesn't match exactly, report an error.
1665 ErrorCategory.Report(
1666 "Name Index entry contains invalid CU or TU offset", [&]() {
1667 error() << formatv("Name Index @ {0:x}: Entry @ {1:x} contains an "
1668 "invalid CU or TU offset {2:x}.\n",
1669 NI.getUnitOffset(), EntryID, *UnitOffset);
1670 });
1671 ++NumErrors;
1672 continue;
1673 }
1674 // This function will try to get the non skeleton unit DIE, but if it is
1675 // unable to load the .dwo file from the .dwo or .dwp, it will return the
1676 // unit DIE of the DWARFUnit in "DU". So we need to check if the DWARFUnit
1677 // has a .dwo file, but we couldn't load it.
1678
1679 // FIXME: Need a follow up patch to fix usage of
1680 // DWARFUnit::getNonSkeletonUnitDIE() so that it returns an empty DWARFDie
1681 // if the .dwo file isn't available and clean up other uses of this function
1682 // call to properly deal with it. It isn't clear that getNonSkeletonUnitDIE
1683 // will return the unit DIE of DU if we aren't able to get the .dwo file,
1684 // but that is what the function currently does.
1685 DWARFDie UnitDie = DU->getUnitDIE();
1686 DWARFDie NonSkeletonUnitDie = DU->getNonSkeletonUnitDIE();
1687 if (DU->getDWOId() && UnitDie == NonSkeletonUnitDie) {
1688 ErrorCategory.Report("Unable to get load .dwo file", [&]() {
1689 error() << formatv(
1690 "Name Index @ {0:x}: Entry @ {1:x} unable to load "
1691 ".dwo file \"{2}\" for DWARF unit @ {3:x}.\n",
1692 NI.getUnitOffset(), EntryID,
1693 dwarf::toString(UnitDie.find({DW_AT_dwo_name, DW_AT_GNU_dwo_name})),
1694 *UnitOffset);
1695 });
1696 ++NumErrors;
1697 continue;
1698 }
1699 DWARFUnit *NonSkeletonUnit = nullptr;
1700 if (TUIndex && *TUIndex >= NumLocalTUs) {
1701 // We have a foreign TU index, which either means we have a .dwo file
1702 // that has one or more type units, or we have a .dwp file with one or
1703 // more type units. We need to get the type unit from the DWARFContext
1704 // of the .dwo. We got the NonSkeletonUnitDie above that has the .dwo
1705 // or .dwp DWARF context, so we have to get the type unit from that file.
1706 // We have also verified that NonSkeletonUnitDie points to a DWO file
1707 // above, so we know we have the right file.
1708 const uint32_t ForeignTUIdx = *TUIndex - NumLocalTUs;
1709 const uint64_t TypeSig = NI.getForeignTUSignature(ForeignTUIdx);
1710 llvm::DWARFContext &SkeletonDCtx =
1711 NonSkeletonUnitDie.getDwarfUnit()->getContext();
1712 // Now find the type unit from the type signature and then update the
1713 // NonSkeletonUnitDie to point to the actual type unit in the .dwo/.dwp.
1714 NonSkeletonUnit =
1715 SkeletonDCtx.getTypeUnitForHash(TypeSig, /*IsDWO=*/true);
1716 NonSkeletonUnitDie = NonSkeletonUnit->getUnitDIE(true);
1717 // If we have foreign type unit in a DWP file, then we need to ignore
1718 // any entries from type units that don't match the one that made it into
1719 // the .dwp file.
1720 if (SkeletonDCtx.isDWP()) {
1721 StringRef DUDwoName = dwarf::toStringRef(
1722 UnitDie.find({DW_AT_dwo_name, DW_AT_GNU_dwo_name}));
1723 StringRef TUDwoName = dwarf::toStringRef(
1724 NonSkeletonUnitDie.find({DW_AT_dwo_name, DW_AT_GNU_dwo_name}));
1725 if (DUDwoName != TUDwoName)
1726 continue; // Skip this TU, it isn't the one in the .dwp file.
1727 }
1728 } else {
1729 NonSkeletonUnit = NonSkeletonUnitDie.getDwarfUnit();
1730 }
1731 uint64_t DIEOffset =
1732 NonSkeletonUnit->getOffset() + *EntryOr->getDIEUnitOffset();
1733 const uint64_t NextUnitOffset = NonSkeletonUnit->getNextUnitOffset();
1734 // DIE offsets are relative to the specified CU or TU. Make sure the DIE
1735 // offsets is a valid relative offset.
1736 if (DIEOffset >= NextUnitOffset) {
1737 ErrorCategory.Report("NameIndex relative DIE offset too large", [&]() {
1738 error() << formatv("Name Index @ {0:x}: Entry @ {1:x} references a "
1739 "DIE @ {2:x} when CU or TU ends at {3:x}.\n",
1740 NI.getUnitOffset(), EntryID, DIEOffset,
1741 NextUnitOffset);
1742 });
1743 continue;
1744 }
1745 DWARFDie DIE = NonSkeletonUnit->getDIEForOffset(DIEOffset);
1746
1747 if (!DIE) {
1748 ErrorCategory.Report("NameIndex references nonexistent DIE", [&]() {
1749 error() << formatv("Name Index @ {0:x}: Entry @ {1:x} references a "
1750 "non-existing DIE @ {2:x}.\n",
1751 NI.getUnitOffset(), EntryID, DIEOffset);
1752 });
1753 ++NumErrors;
1754 continue;
1755 }
1756 // Only compare the DIE we found's DWARFUnit offset if the DIE lives in
1757 // the DWARFUnit from the DW_IDX_comp_unit or DW_IDX_type_unit. If we are
1758 // using split DWARF, then the DIE's DWARFUnit doesn't need to match the
1759 // skeleton unit.
1760 if (DIE.getDwarfUnit() == DU &&
1761 DIE.getDwarfUnit()->getOffset() != *UnitOffset) {
1762 ErrorCategory.Report("Name index contains mismatched CU of DIE", [&]() {
1763 error() << formatv(
1764 "Name Index @ {0:x}: Entry @ {1:x}: mismatched CU of "
1765 "DIE @ {2:x}: index - {3:x}; debug_info - {4:x}.\n",
1766 NI.getUnitOffset(), EntryID, DIEOffset, *UnitOffset,
1767 DIE.getDwarfUnit()->getOffset());
1768 });
1769 ++NumErrors;
1770 }
1771 if (DIE.getTag() != EntryOr->tag()) {
1772 ErrorCategory.Report("Name Index contains mismatched Tag of DIE", [&]() {
1773 error() << formatv(
1774 "Name Index @ {0:x}: Entry @ {1:x}: mismatched Tag of "
1775 "DIE @ {2:x}: index - {3}; debug_info - {4}.\n",
1776 NI.getUnitOffset(), EntryID, DIEOffset, EntryOr->tag(),
1777 DIE.getTag());
1778 });
1779 ++NumErrors;
1780 }
1781
1782 // We allow an extra name for functions: their name without any template
1783 // parameters.
1784 auto IncludeStrippedTemplateNames =
1785 DIE.getTag() == DW_TAG_subprogram ||
1786 DIE.getTag() == DW_TAG_inlined_subroutine;
1787 auto EntryNames = getNames(DIE, IncludeStrippedTemplateNames);
1788 if (!is_contained(EntryNames, Str)) {
1789 ErrorCategory.Report("Name Index contains mismatched name of DIE", [&]() {
1790 error() << formatv("Name Index @ {0:x}: Entry @ {1:x}: mismatched Name "
1791 "of DIE @ {2:x}: index - {3}; debug_info - {4}.\n",
1792 NI.getUnitOffset(), EntryID, DIEOffset, Str,
1793 make_range(EntryNames.begin(), EntryNames.end()));
1794 });
1795 ++NumErrors;
1796 }
1797 }
1799 EntryOr.takeError(),
1800 [&](const DWARFDebugNames::SentinelError &) {
1801 if (NumEntries > 0)
1802 return;
1803 ErrorCategory.Report(
1804 "NameIndex Name is not associated with any entries", [&]() {
1805 error() << formatv("Name Index @ {0:x}: Name {1} ({2}) is "
1806 "not associated with any entries.\n",
1807 NI.getUnitOffset(), NTE.getIndex(), Str);
1808 });
1809 ++NumErrors;
1810 },
1811 [&](const ErrorInfoBase &Info) {
1812 ErrorCategory.Report("Uncategorized NameIndex error", [&]() {
1813 error() << formatv("Name Index @ {0:x}: Name {1} ({2}): {3}\n",
1814 NI.getUnitOffset(), NTE.getIndex(), Str,
1815 Info.message());
1816 });
1817 ++NumErrors;
1818 });
1819 return NumErrors;
1820}
1821
1822static bool isVariableIndexable(const DWARFDie &Die, DWARFContext &DCtx) {
1824 Die.getLocations(DW_AT_location);
1825 if (!Loc) {
1826 consumeError(Loc.takeError());
1827 return false;
1828 }
1829 DWARFUnit *U = Die.getDwarfUnit();
1830 for (const auto &Entry : *Loc) {
1831 DataExtractor Data(toStringRef(Entry.Expr), DCtx.isLittleEndian(),
1832 U->getAddressByteSize());
1833 DWARFExpression Expression(Data, U->getAddressByteSize(),
1834 U->getFormParams().Format);
1835 bool IsInteresting =
1837 return !Op.isError() && (Op.getCode() == DW_OP_addr ||
1838 Op.getCode() == DW_OP_form_tls_address ||
1839 Op.getCode() == DW_OP_GNU_push_tls_address);
1840 });
1841 if (IsInteresting)
1842 return true;
1843 }
1844 return false;
1845}
1846
1847unsigned DWARFVerifier::verifyNameIndexCompleteness(
1848 const DWARFDie &Die, const DWARFDebugNames::NameIndex &NI) {
1849
1850 // First check, if the Die should be indexed. The code follows the DWARF v5
1851 // wording as closely as possible.
1852
1853 // "All non-defining declarations (that is, debugging information entries
1854 // with a DW_AT_declaration attribute) are excluded."
1855 if (Die.find(DW_AT_declaration))
1856 return 0;
1857
1858 // "DW_TAG_namespace debugging information entries without a DW_AT_name
1859 // attribute are included with the name “(anonymous namespace)”.
1860 // All other debugging information entries without a DW_AT_name attribute
1861 // are excluded."
1862 // "If a subprogram or inlined subroutine is included, and has a
1863 // DW_AT_linkage_name attribute, there will be an additional index entry for
1864 // the linkage name."
1865 auto IncludeLinkageName = Die.getTag() == DW_TAG_subprogram ||
1866 Die.getTag() == DW_TAG_inlined_subroutine;
1867 // We *allow* stripped template names / ObjectiveC names as extra entries into
1868 // the table, but we don't *require* them to pass the completeness test.
1869 auto IncludeStrippedTemplateNames = false;
1870 auto IncludeObjCNames = false;
1871 auto EntryNames = getNames(Die, IncludeStrippedTemplateNames,
1872 IncludeObjCNames, IncludeLinkageName);
1873 if (EntryNames.empty())
1874 return 0;
1875
1876 // We deviate from the specification here, which says:
1877 // "The name index must contain an entry for each debugging information entry
1878 // that defines a named subprogram, label, variable, type, or namespace,
1879 // subject to ..."
1880 // Explicitly exclude all TAGs that we know shouldn't be indexed.
1881 switch (Die.getTag()) {
1882 // Compile units and modules have names but shouldn't be indexed.
1883 case DW_TAG_compile_unit:
1884 case DW_TAG_module:
1885 return 0;
1886
1887 // Function and template parameters are not globally visible, so we shouldn't
1888 // index them.
1889 case DW_TAG_formal_parameter:
1890 case DW_TAG_template_value_parameter:
1891 case DW_TAG_template_type_parameter:
1892 case DW_TAG_GNU_template_parameter_pack:
1893 case DW_TAG_GNU_template_template_param:
1894 return 0;
1895
1896 // Object members aren't globally visible.
1897 case DW_TAG_member:
1898 return 0;
1899
1900 // According to a strict reading of the specification, enumerators should not
1901 // be indexed (and LLVM currently does not do that). However, this causes
1902 // problems for the debuggers, so we may need to reconsider this.
1903 case DW_TAG_enumerator:
1904 return 0;
1905
1906 // Imported declarations should not be indexed according to the specification
1907 // and LLVM currently does not do that.
1908 case DW_TAG_imported_declaration:
1909 return 0;
1910
1911 // "DW_TAG_subprogram, DW_TAG_inlined_subroutine, and DW_TAG_label debugging
1912 // information entries without an address attribute (DW_AT_low_pc,
1913 // DW_AT_high_pc, DW_AT_ranges, or DW_AT_entry_pc) are excluded."
1914 case DW_TAG_subprogram:
1915 case DW_TAG_inlined_subroutine:
1916 case DW_TAG_label:
1917 if (Die.findRecursively(
1918 {DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_entry_pc}))
1919 break;
1920 return 0;
1921
1922 // "DW_TAG_variable debugging information entries with a DW_AT_location
1923 // attribute that includes a DW_OP_addr or DW_OP_form_tls_address operator are
1924 // included; otherwise, they are excluded."
1925 //
1926 // LLVM extension: We also add DW_OP_GNU_push_tls_address to this list.
1927 case DW_TAG_variable:
1928 if (isVariableIndexable(Die, DCtx))
1929 break;
1930 return 0;
1931
1932 default:
1933 break;
1934 }
1935
1936 // Now we know that our Die should be present in the Index. Let's check if
1937 // that's the case.
1938 unsigned NumErrors = 0;
1939 uint64_t DieUnitOffset = Die.getOffset() - Die.getDwarfUnit()->getOffset();
1940 for (StringRef Name : EntryNames) {
1941 if (none_of(NI.equal_range(Name), [&](const DWARFDebugNames::Entry &E) {
1942 return E.getDIEUnitOffset() == DieUnitOffset;
1943 })) {
1944 ErrorCategory.Report("Name Index DIE entry missing name", [&]() {
1945 error() << formatv(
1946 "Name Index @ {0:x}: Entry for DIE @ {1:x} ({2}) with "
1947 "name {3} missing.\n",
1948 NI.getUnitOffset(), Die.getOffset(), Die.getTag(), Name);
1949 });
1950 ++NumErrors;
1951 }
1952 }
1953 return NumErrors;
1954}
1955
1956unsigned DWARFVerifier::verifyDebugNames(const DWARFSection &AccelSection,
1957 const DataExtractor &StrData) {
1958 unsigned NumErrors = 0;
1959 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), AccelSection,
1960 DCtx.isLittleEndian(), 0);
1961 DWARFDebugNames AccelTable(AccelSectionData, StrData);
1962
1963 OS << "Verifying .debug_names...\n";
1964
1965 // This verifies that we can read individual name indices and their
1966 // abbreviation tables.
1967 if (Error E = AccelTable.extract()) {
1968 std::string Msg = toString(std::move(E));
1969 ErrorCategory.Report("Accelerator Table Error",
1970 [&]() { error() << Msg << '\n'; });
1971 return 1;
1972 }
1973
1974 NumErrors += verifyDebugNamesCULists(AccelTable);
1975 for (const auto &NI : AccelTable)
1976 NumErrors += verifyNameIndexBuckets(NI, StrData);
1977 for (const auto &NI : AccelTable)
1978 NumErrors += verifyNameIndexAbbrevs(NI);
1979
1980 // Don't attempt Entry validation if any of the previous checks found errors
1981 if (NumErrors > 0)
1982 return NumErrors;
1983 for (const auto &NI : AccelTable)
1984 for (const DWARFDebugNames::NameTableEntry &NTE : NI)
1985 NumErrors += verifyNameIndexEntries(NI, NTE);
1986
1987 for (const std::unique_ptr<DWARFUnit> &U : DCtx.info_section_units()) {
1988 if (const DWARFDebugNames::NameIndex *NI =
1989 AccelTable.getCUOrTUNameIndex(U->getOffset())) {
1990 DWARFCompileUnit *CU = dyn_cast<DWARFCompileUnit>(U.get());
1991 if (CU) {
1992 if (CU->getDWOId()) {
1993 DWARFDie CUDie = CU->getUnitDIE(true);
1994 DWARFDie NonSkeletonUnitDie =
1995 CUDie.getDwarfUnit()->getNonSkeletonUnitDIE(false);
1996 if (CUDie != NonSkeletonUnitDie) {
1997 for (const DWARFDebugInfoEntry &Die :
1998 NonSkeletonUnitDie.getDwarfUnit()->dies())
1999 NumErrors += verifyNameIndexCompleteness(
2000 DWARFDie(NonSkeletonUnitDie.getDwarfUnit(), &Die), *NI);
2001 }
2002 } else {
2003 for (const DWARFDebugInfoEntry &Die : CU->dies())
2004 NumErrors += verifyNameIndexCompleteness(DWARFDie(CU, &Die), *NI);
2005 }
2006 }
2007 }
2008 }
2009 return NumErrors;
2010}
2011
2013 const DWARFObject &D = DCtx.getDWARFObj();
2014 DataExtractor StrData(D.getStrSection(), DCtx.isLittleEndian(), 0);
2015 unsigned NumErrors = 0;
2016 if (!D.getAppleNamesSection().Data.empty())
2017 NumErrors += verifyAppleAccelTable(&D.getAppleNamesSection(), &StrData,
2018 ".apple_names");
2019 if (!D.getAppleTypesSection().Data.empty())
2020 NumErrors += verifyAppleAccelTable(&D.getAppleTypesSection(), &StrData,
2021 ".apple_types");
2022 if (!D.getAppleNamespacesSection().Data.empty())
2023 NumErrors += verifyAppleAccelTable(&D.getAppleNamespacesSection(), &StrData,
2024 ".apple_namespaces");
2025 if (!D.getAppleObjCSection().Data.empty())
2026 NumErrors += verifyAppleAccelTable(&D.getAppleObjCSection(), &StrData,
2027 ".apple_objc");
2028
2029 if (!D.getNamesSection().Data.empty())
2030 NumErrors += verifyDebugNames(D.getNamesSection(), StrData);
2031 return NumErrors == 0;
2032}
2033
2035 OS << "Verifying .debug_str_offsets...\n";
2036 const DWARFObject &DObj = DCtx.getDWARFObj();
2037 bool Success = true;
2038
2039 // dwo sections may contain the legacy debug_str_offsets format (and they
2040 // can't be mixed with dwarf 5's format). This section format contains no
2041 // header.
2042 // As such, check the version from debug_info and, if we are in the legacy
2043 // mode (Dwarf <= 4), extract Dwarf32/Dwarf64.
2044 std::optional<DwarfFormat> DwoLegacyDwarf4Format;
2045 DObj.forEachInfoDWOSections([&](const DWARFSection &S) {
2046 if (DwoLegacyDwarf4Format)
2047 return;
2048 DWARFDataExtractor DebugInfoData(DObj, S, DCtx.isLittleEndian(), 0);
2049 uint64_t Offset = 0;
2050 DwarfFormat InfoFormat = DebugInfoData.getInitialLength(&Offset).second;
2051 if (uint16_t InfoVersion = DebugInfoData.getU16(&Offset); InfoVersion <= 4)
2052 DwoLegacyDwarf4Format = InfoFormat;
2053 });
2054
2056 DwoLegacyDwarf4Format, ".debug_str_offsets.dwo",
2059 /*LegacyFormat=*/std::nullopt, ".debug_str_offsets",
2060 DObj.getStrOffsetsSection(), DObj.getStrSection());
2061 return Success;
2062}
2063
2065 std::optional<DwarfFormat> LegacyFormat, StringRef SectionName,
2066 const DWARFSection &Section, StringRef StrData) {
2067 const DWARFObject &DObj = DCtx.getDWARFObj();
2068
2069 DWARFDataExtractor DA(DObj, Section, DCtx.isLittleEndian(), 0);
2071 uint64_t NextUnit = 0;
2072 bool Success = true;
2073 while (C.seek(NextUnit), C.tell() < DA.getData().size()) {
2076 uint64_t StartOffset = C.tell();
2077 if (LegacyFormat) {
2078 Format = *LegacyFormat;
2079 Length = DA.getData().size();
2080 NextUnit = C.tell() + Length;
2081 } else {
2082 std::tie(Length, Format) = DA.getInitialLength(C);
2083 if (!C)
2084 break;
2085 if (C.tell() + Length > DA.getData().size()) {
2086 ErrorCategory.Report(
2087 "Section contribution length exceeds available space", [&]() {
2088 error() << formatv(
2089 "{0}: contribution {1:X}: length exceeds available space "
2090 "(contribution "
2091 "offset ({1:X}) + length field space ({2:X}) + length "
2092 "({3:X}) == "
2093 "{4:X} > section size {5:X})\n",
2094 SectionName, StartOffset, C.tell() - StartOffset, Length,
2095 C.tell() + Length, DA.getData().size());
2096 });
2097 Success = false;
2098 // Nothing more to do - no other contributions to try.
2099 break;
2100 }
2101 NextUnit = C.tell() + Length;
2102 uint8_t Version = DA.getU16(C);
2103 if (C && Version != 5) {
2104 ErrorCategory.Report("Invalid Section version", [&]() {
2105 error() << formatv("{0}: contribution {1:X}: invalid version {2}\n",
2106 SectionName, StartOffset, Version);
2107 });
2108 Success = false;
2109 // Can't parse the rest of this contribution, since we don't know the
2110 // version, but we can pick up with the next contribution.
2111 continue;
2112 }
2113 (void)DA.getU16(C); // padding
2114 }
2115 uint64_t OffsetByteSize = getDwarfOffsetByteSize(Format);
2116 DA.setAddressSize(OffsetByteSize);
2117 uint64_t Remainder = (Length - 4) % OffsetByteSize;
2118 if (Remainder != 0) {
2119 ErrorCategory.Report("Invalid section contribution length", [&]() {
2120 error() << formatv(
2121 "{0}: contribution {1:X}: invalid length ((length ({2:X}) "
2122 "- header (0x4)) % offset size {3:X} == {4:X} != 0)\n",
2123 SectionName, StartOffset, Length, OffsetByteSize, Remainder);
2124 });
2125 Success = false;
2126 }
2127 for (uint64_t Index = 0; C && C.tell() + OffsetByteSize <= NextUnit; ++Index) {
2128 uint64_t OffOff = C.tell();
2129 uint64_t StrOff = DA.getAddress(C);
2130 // check StrOff refers to the start of a string
2131 if (StrOff == 0)
2132 continue;
2133 if (StrData.size() <= StrOff) {
2134 ErrorCategory.Report(
2135 "String offset out of bounds of string section", [&]() {
2136 error() << formatv(
2137 "{0}: contribution {1:X}: index {2:X}: invalid string "
2138 "offset *{3:X} == {4:X}, is beyond the bounds of the string "
2139 "section of length {5:X}\n",
2140 SectionName, StartOffset, Index, OffOff, StrOff,
2141 StrData.size());
2142 });
2143 continue;
2144 }
2145 if (StrData[StrOff - 1] == '\0')
2146 continue;
2147 ErrorCategory.Report(
2148 "Section contribution contains invalid string offset", [&]() {
2149 error() << formatv(
2150 "{0}: contribution {1:X}: index {2:X}: invalid string "
2151 "offset *{3:X} == {4:X}, is neither zero nor "
2152 "immediately following a null character\n",
2153 SectionName, StartOffset, Index, OffOff, StrOff);
2154 });
2155 Success = false;
2156 }
2157 }
2158
2159 if (Error E = C.takeError()) {
2160 std::string Msg = toString(std::move(E));
2161 ErrorCategory.Report("String offset error", [&]() {
2162 error() << SectionName << ": " << Msg << '\n';
2163 return false;
2164 });
2165 }
2166 return Success;
2167}
2168
2170 StringRef s, std::function<void(void)> detailCallback) {
2171 Aggregation[std::string(s)]++;
2172 if (IncludeDetail)
2173 detailCallback();
2174}
2175
2177 std::function<void(StringRef, unsigned)> handleCounts) {
2178 for (auto &&[name, count] : Aggregation) {
2179 handleCounts(name, count);
2180 }
2181}
2182
2184 if (DumpOpts.ShowAggregateErrors && ErrorCategory.GetNumCategories()) {
2185 error() << "Aggregated error counts:\n";
2186 ErrorCategory.EnumerateResults([&](StringRef s, unsigned count) {
2187 error() << s << " occurred " << count << " time(s).\n";
2188 });
2189 }
2190 if (!DumpOpts.JsonErrSummaryFile.empty()) {
2191 std::error_code EC;
2192 raw_fd_ostream JsonStream(DumpOpts.JsonErrSummaryFile, EC,
2194 if (EC) {
2195 error() << "unable to open json summary file '"
2196 << DumpOpts.JsonErrSummaryFile
2197 << "' for writing: " << EC.message() << '\n';
2198 return;
2199 }
2200
2201 llvm::json::Object Categories;
2202 uint64_t ErrorCount = 0;
2203 ErrorCategory.EnumerateResults([&](StringRef Category, unsigned Count) {
2205 Val.try_emplace("count", Count);
2206 Categories.try_emplace(Category, std::move(Val));
2207 ErrorCount += Count;
2208 });
2209 llvm::json::Object RootNode;
2210 RootNode.try_emplace("error-categories", std::move(Categories));
2211 RootNode.try_emplace("error-count", ErrorCount);
2212
2213 JsonStream << llvm::json::Value(std::move(RootNode));
2214 }
2215}
2216
2217raw_ostream &DWARFVerifier::error() const { return WithColor::error(OS); }
2218
2219raw_ostream &DWARFVerifier::warn() const { return WithColor::warning(OS); }
2220
2221raw_ostream &DWARFVerifier::note() const { return WithColor::note(OS); }
2222
2223raw_ostream &DWARFVerifier::dump(const DWARFDie &Die, unsigned indent) const {
2224 Die.dump(OS, indent, DumpOpts);
2225 return OS;
2226}
#define Success
ArrayRef< TableEntry > TableRef
AMDGPU Kernel Attributes
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static bool isVariableIndexable(const DWARFDie &Die, DWARFContext &DCtx)
static SmallVector< std::string, 3 > getNames(const DWARFDie &DIE, bool IncludeStrippedTemplateNames, bool IncludeObjCNames=true, bool IncludeLinkageName=true)
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file contains constants used for implementing Dwarf debug support.
std::string Name
bool End
Definition: ELF_riscv.cpp:480
This file implements a coalescing interval map for small objects.
This file supports working with JSON data.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Definition: SMEABIPass.cpp:46
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
This file defines the SmallSet class.
#define error(X)
Value * RHS
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
Definition: AccelTable.h:202
This implements the Apple accelerator table format, a precursor of the DWARF 5 accelerator table form...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:157
A structured debug information entry.
Definition: DIE.h:819
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:857
dwarf::Tag getTag() const
Definition: DIE.h:855
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:48
static bool isSupportedVersion(unsigned version)
Definition: DWARFContext.h:406
unsigned getNumCompileUnits()
Get the number of compile units in this context.
Definition: DWARFContext.h:238
DWARFDie getDIEForOffset(uint64_t Offset)
Get a DIE given an exact offset.
const DWARFDebugAbbrev * getDebugAbbrevDWO()
Get a pointer to the parsed dwo abbreviations object.
compile_unit_range compile_units()
Get compile units in this context.
Definition: DWARFContext.h:188
const DWARFDebugAbbrev * getDebugAbbrev()
Get a pointer to the parsed DebugAbbrev object.
bool isDWP() const
Return true of this DWARF context is a DWP file.
bool isLittleEndian() const
Definition: DWARFContext.h:404
const DWARFDebugLine::LineTable * getLineTableForUnit(DWARFUnit *U)
Get a pointer to a parsed line table corresponding to a compile unit.
DWARFUnit * getUnitForOffset(uint64_t Offset)
Return the DWARF unit that includes an offset (relative to .debug_info).
const DWARFUnitVector & getNormalUnitsVector()
Definition: DWARFContext.h:176
DWARFTypeUnit * getTypeUnitForHash(uint64_t Hash, bool IsDWO)
unit_iterator_range info_section_units()
Get units from .debug_info in this context.
Definition: DWARFContext.h:169
static bool isAddressSizeSupported(unsigned AddressSize)
Definition: DWARFContext.h:413
const DWARFUnitVector & getDWOUnitsVector()
Definition: DWARFContext.h:208
const DWARFObject & getDWARFObj() const
Definition: DWARFContext.h:147
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
std::pair< uint64_t, dwarf::DwarfFormat > getInitialLength(uint64_t *Off, Error *Err=nullptr) const
Extracts the DWARF "initial length" field, which can either be a 32-bit value smaller than 0xfffffff0...
Expected< const DWARFAbbreviationDeclarationSet * > getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const
DWARFDebugInfoEntry - A DIE with only the minimum required data.
DWARF v5-specific implementation of an Accelerator Entry.
Represents a single accelerator table within the DWARF v5 .debug_names section.
uint32_t getHashArrayEntry(uint32_t Index) const
Reads an entry in the Hash Array for the given Index.
uint64_t getLocalTUOffset(uint32_t TU) const
Reads offset of local type unit TU, TU is 0-based.
uint32_t getBucketArrayEntry(uint32_t Bucket) const
Reads an entry in the Bucket Array for the given Bucket.
iterator_range< ValueIterator > equal_range(StringRef Key) const
Look up all entries in this Name Index matching Key.
uint64_t getCUOffset(uint32_t CU) const
Reads offset of compilation unit CU. CU is 0-based.
Expected< Entry > getEntry(uint64_t *Offset) const
NameTableEntry getNameTableEntry(uint32_t Index) const
Reads an entry in the Name Table for the given Index.
const DenseSet< Abbrev, AbbrevMapInfo > & getAbbrevs() const
uint64_t getForeignTUSignature(uint32_t TU) const
Reads signature of foreign type unit TU. TU is 0-based.
A single entry in the Name Table (DWARF v5 sect.
uint64_t getEntryOffset() const
Returns the offset of the first Entry in the list.
const char * getString() const
Return the string referenced by this name table entry or nullptr if the string offset is not valid.
uint32_t getIndex() const
Return the index of this name in the parent Name Index.
Error returned by NameIndex::getEntry to report it has reached the end of the entry list.
.debug_names section consists of one or more units.
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition: DWARFDie.h:42
void getFullName(raw_string_ostream &, std::string *OriginalFullName=nullptr) const
Definition: DWARFDie.cpp:232
uint64_t getOffset() const
Get the absolute offset into the debug info or types section.
Definition: DWARFDie.h:67
Expected< DWARFAddressRangesVector > getAddressRanges() const
Get the address ranges for this DIE.
Definition: DWARFDie.cpp:386
DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE as the referenced DIE.
Definition: DWARFDie.cpp:305
DWARFDie getParent() const
Get the parent of this DIE object.
Definition: DWARFDie.cpp:654
std::optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition: DWARFDie.cpp:249
DWARFUnit * getDwarfUnit() const
Definition: DWARFDie.h:54
bool hasChildren() const
Definition: DWARFDie.h:79
bool isSubprogramDIE() const
Returns true if DIE represents a subprogram (not inlined).
Definition: DWARFDie.cpp:242
std::optional< DWARFFormValue > findRecursively(ArrayRef< dwarf::Attribute > Attrs) const
Extract the first value of any attribute in Attrs from this DIE and recurse into any DW_AT_specificat...
Definition: DWARFDie.cpp:273
DWARFDie getFirstChild() const
Get the first child of this DIE object.
Definition: DWARFDie.cpp:672
dwarf::Tag getTag() const
Definition: DWARFDie.h:72
Expected< DWARFLocationExpressionsVector > getLocations(dwarf::Attribute Attr) const
Definition: DWARFDie.cpp:426
bool isValid() const
Definition: DWARFDie.h:51
iterator_range< attribute_iterator > attributes() const
Get an iterator range to all attributes in the current DIE only.
Definition: DWARFDie.cpp:684
void dump(raw_ostream &OS, unsigned indent=0, DIDumpOptions DumpOpts=DIDumpOptions()) const
Dump the DIE and all of its attributes to the supplied stream.
Definition: DWARFDie.cpp:594
This class represents an Operation in the Expression.
std::optional< uint64_t > getAsSectionOffset() const
bool isFormClass(FormClass FC) const
std::optional< uint64_t > getAsRelativeReference() const
getAsFoo functions below return the extracted value as Foo if only DWARFFormValue has form class is s...
std::optional< uint64_t > getAsDebugInfoReference() const
std::optional< uint64_t > getAsUnsignedConstant() const
Expected< const char * > getAsCString() const
const DWARFUnit * getUnit() const
dwarf::Form getForm() const
uint64_t getRawUValue() const
virtual StringRef getStrDWOSection() const
Definition: DWARFObject.h:68
virtual StringRef getAbbrevDWOSection() const
Definition: DWARFObject.h:64
virtual StringRef getAbbrevSection() const
Definition: DWARFObject.h:40
virtual const DWARFSection & getStrOffsetsDWOSection() const
Definition: DWARFObject.h:69
virtual void forEachInfoDWOSections(function_ref< void(const DWARFSection &)> F) const
Definition: DWARFObject.h:61
virtual void forEachInfoSections(function_ref< void(const DWARFSection &)> F) const
Definition: DWARFObject.h:37
virtual const DWARFSection & getRangesSection() const
Definition: DWARFObject.h:49
virtual StringRef getTUIndexSection() const
Definition: DWARFObject.h:84
virtual void forEachTypesSections(function_ref< void(const DWARFSection &)> F) const
Definition: DWARFObject.h:39
virtual const DWARFSection & getStrOffsetsSection() const
Definition: DWARFObject.h:59
virtual const DWARFSection & getLineSection() const
Definition: DWARFObject.h:46
virtual const DWARFSection & getRnglistsSection() const
Definition: DWARFObject.h:50
virtual StringRef getCUIndexSection() const
Definition: DWARFObject.h:82
virtual StringRef getStrSection() const
Definition: DWARFObject.h:48
virtual const object::ObjectFile * getFile() const
Definition: DWARFObject.h:32
Describe a collection of units.
Definition: DWARFUnit.h:128
std::optional< uint64_t > getDWOId()
Definition: DWARFUnit.h:458
DWARFDie getNonSkeletonUnitDIE(bool ExtractUnitDIEOnly=true, StringRef DWOAlternativeLocation={})
Definition: DWARFUnit.h:450
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:443
DWARFContext & getContext() const
Definition: DWARFUnit.h:319
DWARFDie getDIEForOffset(uint64_t Offset)
Return the DIE object for a given offset Offset inside the unit's DIE vector.
Definition: DWARFUnit.h:533
die_iterator_range dies()
Definition: DWARFUnit.h:560
static bool isMatchingUnitTypeAndTag(uint8_t UnitType, dwarf::Tag Tag)
Definition: DWARFUnit.h:424
uint64_t getNextUnitOffset() const
Definition: DWARFUnit.h:338
uint64_t getOffset() const
Definition: DWARFUnit.h:321
bool handleAccelTables()
Verify the information in accelerator tables, if they exist.
bool verifyDebugStrOffsets(std::optional< dwarf::DwarfFormat > LegacyFormat, StringRef SectionName, const DWARFSection &Section, StringRef StrData)
bool handleDebugTUIndex()
Verify the information in the .debug_tu_index section.
bool handleDebugStrOffsets()
Verify the information in the .debug_str_offsets[.dwo].
bool handleDebugCUIndex()
Verify the information in the .debug_cu_index section.
DWARFVerifier(raw_ostream &S, DWARFContext &D, DIDumpOptions DumpOpts=DIDumpOptions::getForSingleDIE())
bool handleDebugInfo()
Verify the information in the .debug_info and .debug_types sections.
bool handleDebugLine()
Verify the information in the .debug_line section.
void summarize()
Emits any aggregate information collected, depending on the dump options.
bool handleDebugAbbrev()
Verify the information in any of the following sections, if available: .debug_abbrev,...
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
const char * getCStr(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a C string from *offset_ptr.
uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
uint64_t getU64(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint64_t value from *offset_ptr.
bool isValidOffset(uint64_t offset) const
Test the validity of offset.
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
iterator end()
Definition: DenseMap.h:84
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
Base class for error info classes.
Definition: Error.h:45
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
Class representing an expression and its matching format.
void ShowDetail(bool showDetail)
Definition: DWARFVerifier.h:41
void Report(StringRef s, std::function< void()> detailCallback)
void EnumerateResults(std::function< void(StringRef, unsigned)> handleCounts)
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition: DenseSet.h:298
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:132
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:853
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
Definition: StringMap.h:368
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition: WithColor.cpp:85
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:83
static raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition: WithColor.cpp:87
An efficient, type-erasing, non-owning reference to a callable.
An Object is a JSON object, which maps strings to heterogenous JSON values.
Definition: JSON.h:98
std::pair< iterator, bool > try_emplace(const ObjectKey &K, Ts &&... Args)
Definition: JSON.h:126
A Value is an JSON value of unknown type.
Definition: JSON.h:288
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:460
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
StringRef AttributeString(unsigned Attribute)
Definition: Dwarf.cpp:72
StringRef FormEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:105
StringRef UnitTypeString(unsigned)
Definition: Dwarf.cpp:672
StringRef TagString(unsigned Tag)
Definition: Dwarf.cpp:21
@ Entry
Definition: COFF.h:844
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
bool isUnitType(uint8_t UnitType)
Definition: Dwarf.h:896
UnitType
Constants for unit types in DWARF v5.
Definition: Dwarf.h:882
bool isType(Tag T)
Definition: Dwarf.h:112
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:92
@ DWARF64
Definition: Dwarf.h:92
std::optional< uint64_t > toSectionOffset(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
StringRef toStringRef(const std::optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition: Dwarf.h:1080
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition: FileSystem.h:754
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
@ Offset
Definition: DWP.cpp:480
@ Length
Definition: DWP.cpp:480
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:361
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2448
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:977
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Definition: Error.h:954
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
DWARFSectionKind
The enum of section identifiers to be used in internal interfaces.
@ DW_SECT_EXT_TYPES
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1746
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1753
std::optional< StringRef > StripTemplateParameters(StringRef Name)
If Name is the name of a templated function that includes template parameters, returns a substring of...
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
uint32_t caseFoldingDjbHash(StringRef Buffer, uint32_t H=5381)
Computes the Bernstein hash after folding the input according to the Dwarf 5 standard case folding ru...
Definition: DJB.cpp:72
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1938
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1873
std::optional< ObjCSelectorNames > getObjCNamesIfSelector(StringRef Name)
If Name is the AT_name of a DIE which refers to an Objective-C selector, returns an instance of ObjCS...
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1766
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1903
const char * toString(DWARFSectionKind Kind)
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition: STLExtras.h:1624
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:196
std::string JsonErrSummaryFile
Definition: DIContext.h:212
Encapsulates a DWARF attribute value and all of the data required to describe the attribute value.
DWARFFormValue Value
The form and value for this attribute.
dwarf::Attribute Attr
The attribute enumeration of this attribute.
static void dumpTableHeader(raw_ostream &OS, unsigned Indent)
Abbreviation describing the encoding of Name Index entries.
uint32_t Code
< Abbreviation offset in the .debug_names section
Index attribute and its encoding.
A class that keeps the address range information for a single DIE.
Definition: DWARFVerifier.h:51
std::vector< DWARFAddressRange > Ranges
Sorted DWARFAddressRanges.
Definition: DWARFVerifier.h:55
bool contains(const DieRangeInfo &RHS) const
Return true if ranges in this object contains all ranges within RHS.
std::set< DieRangeInfo >::const_iterator die_range_info_iterator
Definition: DWARFVerifier.h:67
bool intersects(const DieRangeInfo &RHS) const
Return true if any range in this object intersects with any range in RHS.
std::optional< DWARFAddressRange > insert(const DWARFAddressRange &R)
Inserts the address range.