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