1 //===- DWARFDie.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 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
10 #include "llvm/ADT/None.h"
11 #include "llvm/ADT/Optional.h"
12 #include "llvm/ADT/SmallSet.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/BinaryFormat/Dwarf.h"
15 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
16 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
18 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
19 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
20 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/DataExtractor.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/FormatAdapters.h"
25 #include "llvm/Support/FormatVariadic.h"
26 #include "llvm/Support/MathExtras.h"
27 #include "llvm/Support/WithColor.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <algorithm>
30 #include <cassert>
31 #include <cinttypes>
32 #include <cstdint>
33 #include <string>
34 #include <utility>
35 
36 using namespace llvm;
37 using namespace dwarf;
38 using namespace object;
39 
40 static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
41   OS << " (";
42   do {
43     uint64_t Shift = countTrailingZeros(Val);
44     assert(Shift < 64 && "undefined behavior");
45     uint64_t Bit = 1ULL << Shift;
46     auto PropName = ApplePropertyString(Bit);
47     if (!PropName.empty())
48       OS << PropName;
49     else
50       OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
51     if (!(Val ^= Bit))
52       break;
53     OS << ", ";
54   } while (true);
55   OS << ")";
56 }
57 
58 static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
59                        const DWARFAddressRangesVector &Ranges,
60                        unsigned AddressSize, unsigned Indent,
61                        const DIDumpOptions &DumpOpts) {
62   if (!DumpOpts.ShowAddresses)
63     return;
64 
65   for (const DWARFAddressRange &R : Ranges) {
66     OS << '\n';
67     OS.indent(Indent);
68     R.dump(OS, AddressSize, DumpOpts, &Obj);
69   }
70 }
71 
72 static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue,
73                          DWARFUnit *U, unsigned Indent,
74                          DIDumpOptions DumpOpts) {
75   DWARFContext &Ctx = U->getContext();
76   const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
77   if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
78       FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) {
79     ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
80     DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
81                        Ctx.isLittleEndian(), 0);
82     DWARFExpression(Data, U->getAddressByteSize(), U->getFormParams().Format)
83         .print(OS, DumpOpts, MRI, U);
84     return;
85   }
86 
87   if (FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) {
88     uint64_t Offset = *FormValue.getAsSectionOffset();
89 
90     if (FormValue.getForm() == DW_FORM_loclistx) {
91       FormValue.dump(OS, DumpOpts);
92 
93       if (auto LoclistOffset = U->getLoclistOffset(Offset))
94         Offset = *LoclistOffset;
95       else
96         return;
97     }
98     U->getLocationTable().dumpLocationList(&Offset, OS, U->getBaseAddress(),
99                                            MRI, Ctx.getDWARFObj(), U, DumpOpts,
100                                            Indent);
101     return;
102   }
103 
104   FormValue.dump(OS, DumpOpts);
105 }
106 
107 /// Dump the name encoded in the type tag.
108 static void dumpTypeTagName(raw_ostream &OS, dwarf::Tag T) {
109   StringRef TagStr = TagString(T);
110   if (!TagStr.startswith("DW_TAG_") || !TagStr.endswith("_type"))
111     return;
112   OS << TagStr.substr(7, TagStr.size() - 12) << " ";
113 }
114 
115 static void dumpArrayType(raw_ostream &OS, const DWARFDie &D) {
116   for (const DWARFDie &C : D.children())
117     if (C.getTag() == DW_TAG_subrange_type) {
118       Optional<uint64_t> LB;
119       Optional<uint64_t> Count;
120       Optional<uint64_t> UB;
121       Optional<unsigned> DefaultLB;
122       if (Optional<DWARFFormValue> L = C.find(DW_AT_lower_bound))
123         LB = L->getAsUnsignedConstant();
124       if (Optional<DWARFFormValue> CountV = C.find(DW_AT_count))
125         Count = CountV->getAsUnsignedConstant();
126       if (Optional<DWARFFormValue> UpperV = C.find(DW_AT_upper_bound))
127         UB = UpperV->getAsUnsignedConstant();
128       if (Optional<DWARFFormValue> LV =
129               D.getDwarfUnit()->getUnitDIE().find(DW_AT_language))
130         if (Optional<uint64_t> LC = LV->getAsUnsignedConstant())
131           if ((DefaultLB =
132                    LanguageLowerBound(static_cast<dwarf::SourceLanguage>(*LC))))
133             if (LB && *LB == *DefaultLB)
134               LB = None;
135       if (!LB && !Count && !UB)
136         OS << "[]";
137       else if (!LB && (Count || UB) && DefaultLB)
138         OS << '[' << (Count ? *Count : *UB - *DefaultLB + 1) << ']';
139       else {
140         OS << "[[";
141         if (LB)
142           OS << *LB;
143         else
144           OS << '?';
145         OS << ", ";
146         if (Count)
147           if (LB)
148             OS << *LB + *Count;
149           else
150             OS << "? + " << *Count;
151         else if (UB)
152           OS << *UB + 1;
153         else
154           OS << '?';
155         OS << ")]";
156       }
157     }
158 }
159 
160 /// Recursively dump the DIE type name when applicable.
161 static void dumpTypeName(raw_ostream &OS, const DWARFDie &D) {
162   if (!D.isValid())
163     return;
164 
165   if (const char *Name = D.getName(DINameKind::LinkageName)) {
166     OS << Name;
167     return;
168   }
169 
170   // FIXME: We should have pretty printers per language. Currently we print
171   // everything as if it was C++ and fall back to the TAG type name.
172   const dwarf::Tag T = D.getTag();
173   switch (T) {
174   case DW_TAG_array_type:
175   case DW_TAG_pointer_type:
176   case DW_TAG_ptr_to_member_type:
177   case DW_TAG_reference_type:
178   case DW_TAG_rvalue_reference_type:
179   case DW_TAG_subroutine_type:
180     break;
181   default:
182     dumpTypeTagName(OS, T);
183   }
184 
185   // Follow the DW_AT_type if possible.
186   DWARFDie TypeDie = D.getAttributeValueAsReferencedDie(DW_AT_type);
187   dumpTypeName(OS, TypeDie);
188 
189   switch (T) {
190   case DW_TAG_subroutine_type: {
191     if (!TypeDie)
192       OS << "void";
193     OS << '(';
194     bool First = true;
195     for (const DWARFDie &C : D.children()) {
196       if (C.getTag() == DW_TAG_formal_parameter) {
197         if (!First)
198           OS << ", ";
199         First = false;
200         dumpTypeName(OS, C.getAttributeValueAsReferencedDie(DW_AT_type));
201       }
202     }
203     OS << ')';
204     break;
205   }
206   case DW_TAG_array_type: {
207     dumpArrayType(OS, D);
208     break;
209   }
210   case DW_TAG_pointer_type:
211     OS << '*';
212     break;
213   case DW_TAG_ptr_to_member_type:
214     if (DWARFDie Cont =
215             D.getAttributeValueAsReferencedDie(DW_AT_containing_type)) {
216       dumpTypeName(OS << ' ', Cont);
217       OS << "::";
218     }
219     OS << '*';
220     break;
221   case DW_TAG_reference_type:
222     OS << '&';
223     break;
224   case DW_TAG_rvalue_reference_type:
225     OS << "&&";
226     break;
227   default:
228     break;
229   }
230 }
231 
232 static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
233                           uint64_t *OffsetPtr, dwarf::Attribute Attr,
234                           dwarf::Form Form, unsigned Indent,
235                           DIDumpOptions DumpOpts) {
236   if (!Die.isValid())
237     return;
238   const char BaseIndent[] = "            ";
239   OS << BaseIndent;
240   OS.indent(Indent + 2);
241   WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
242 
243   if (DumpOpts.Verbose || DumpOpts.ShowForm)
244     OS << formatv(" [{0}]", Form);
245 
246   DWARFUnit *U = Die.getDwarfUnit();
247   DWARFFormValue FormValue = DWARFFormValue::createFromUnit(Form, U, OffsetPtr);
248 
249   OS << "\t(";
250 
251   StringRef Name;
252   std::string File;
253   auto Color = HighlightColor::Enumerator;
254   if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
255     Color = HighlightColor::String;
256     if (const auto *LT = U->getContext().getLineTableForUnit(U))
257       if (LT->getFileNameByIndex(
258               FormValue.getAsUnsignedConstant().getValue(),
259               U->getCompilationDir(),
260               DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
261         File = '"' + File + '"';
262         Name = File;
263       }
264   } else if (Optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
265     Name = AttributeValueString(Attr, *Val);
266 
267   if (!Name.empty())
268     WithColor(OS, Color) << Name;
269   else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
270     OS << *FormValue.getAsUnsignedConstant();
271   else if (Attr == DW_AT_low_pc &&
272            (FormValue.getAsAddress() ==
273             dwarf::computeTombstoneAddress(U->getAddressByteSize()))) {
274     if (DumpOpts.Verbose) {
275       FormValue.dump(OS, DumpOpts);
276       OS << " (";
277     }
278     OS << "dead code";
279     if (DumpOpts.Verbose)
280       OS << ')';
281   } else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
282              FormValue.getAsUnsignedConstant()) {
283     if (DumpOpts.ShowAddresses) {
284       // Print the actual address rather than the offset.
285       uint64_t LowPC, HighPC, Index;
286       if (Die.getLowAndHighPC(LowPC, HighPC, Index))
287         DWARFFormValue::dumpAddress(OS, U->getAddressByteSize(), HighPC);
288       else
289         FormValue.dump(OS, DumpOpts);
290     }
291   } else if (Form == dwarf::Form::DW_FORM_exprloc ||
292              DWARFAttribute::mayHaveLocationDescription(Attr))
293     dumpLocation(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
294   else
295     FormValue.dump(OS, DumpOpts);
296 
297   std::string Space = DumpOpts.ShowAddresses ? " " : "";
298 
299   // We have dumped the attribute raw value. For some attributes
300   // having both the raw value and the pretty-printed value is
301   // interesting. These attributes are handled below.
302   if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
303     if (const char *Name =
304             Die.getAttributeValueAsReferencedDie(FormValue).getName(
305                 DINameKind::LinkageName))
306       OS << Space << "\"" << Name << '\"';
307   } else if (Attr == DW_AT_type) {
308     OS << Space << "\"";
309     dumpTypeName(OS, Die.getAttributeValueAsReferencedDie(FormValue));
310     OS << '"';
311   } else if (Attr == DW_AT_APPLE_property_attribute) {
312     if (Optional<uint64_t> OptVal = FormValue.getAsUnsignedConstant())
313       dumpApplePropertyAttribute(OS, *OptVal);
314   } else if (Attr == DW_AT_ranges) {
315     const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
316     // For DW_FORM_rnglistx we need to dump the offset separately, since
317     // we have only dumped the index so far.
318     if (FormValue.getForm() == DW_FORM_rnglistx)
319       if (auto RangeListOffset =
320               U->getRnglistOffset(*FormValue.getAsSectionOffset())) {
321         DWARFFormValue FV = DWARFFormValue::createFromUValue(
322             dwarf::DW_FORM_sec_offset, *RangeListOffset);
323         FV.dump(OS, DumpOpts);
324       }
325     if (auto RangesOrError = Die.getAddressRanges())
326       dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
327                  sizeof(BaseIndent) + Indent + 4, DumpOpts);
328     else
329       DumpOpts.RecoverableErrorHandler(createStringError(
330           errc::invalid_argument, "decoding address ranges: %s",
331           toString(RangesOrError.takeError()).c_str()));
332   }
333 
334   OS << ")\n";
335 }
336 
337 bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
338 
339 bool DWARFDie::isSubroutineDIE() const {
340   auto Tag = getTag();
341   return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
342 }
343 
344 Optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
345   if (!isValid())
346     return None;
347   auto AbbrevDecl = getAbbreviationDeclarationPtr();
348   if (AbbrevDecl)
349     return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
350   return None;
351 }
352 
353 Optional<DWARFFormValue>
354 DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
355   if (!isValid())
356     return None;
357   auto AbbrevDecl = getAbbreviationDeclarationPtr();
358   if (AbbrevDecl) {
359     for (auto Attr : Attrs) {
360       if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
361         return Value;
362     }
363   }
364   return None;
365 }
366 
367 Optional<DWARFFormValue>
368 DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
369   SmallVector<DWARFDie, 3> Worklist;
370   Worklist.push_back(*this);
371 
372   // Keep track if DIEs already seen to prevent infinite recursion.
373   // Empirically we rarely see a depth of more than 3 when dealing with valid
374   // DWARF. This corresponds to following the DW_AT_abstract_origin and
375   // DW_AT_specification just once.
376   SmallSet<DWARFDie, 3> Seen;
377   Seen.insert(*this);
378 
379   while (!Worklist.empty()) {
380     DWARFDie Die = Worklist.back();
381     Worklist.pop_back();
382 
383     if (!Die.isValid())
384       continue;
385 
386     if (auto Value = Die.find(Attrs))
387       return Value;
388 
389     if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
390       if (Seen.insert(D).second)
391         Worklist.push_back(D);
392 
393     if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
394       if (Seen.insert(D).second)
395         Worklist.push_back(D);
396   }
397 
398   return None;
399 }
400 
401 DWARFDie
402 DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
403   if (Optional<DWARFFormValue> F = find(Attr))
404     return getAttributeValueAsReferencedDie(*F);
405   return DWARFDie();
406 }
407 
408 DWARFDie
409 DWARFDie::getAttributeValueAsReferencedDie(const DWARFFormValue &V) const {
410   if (auto SpecRef = V.getAsRelativeReference()) {
411     if (SpecRef->Unit)
412       return SpecRef->Unit->getDIEForOffset(SpecRef->Unit->getOffset() + SpecRef->Offset);
413     if (auto SpecUnit = U->getUnitVector().getUnitForOffset(SpecRef->Offset))
414       return SpecUnit->getDIEForOffset(SpecRef->Offset);
415   }
416   return DWARFDie();
417 }
418 
419 Optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
420   return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
421 }
422 
423 Optional<uint64_t> DWARFDie::getLocBaseAttribute() const {
424   return toSectionOffset(find(DW_AT_loclists_base));
425 }
426 
427 Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
428   uint64_t Tombstone = dwarf::computeTombstoneAddress(U->getAddressByteSize());
429   if (LowPC == Tombstone)
430     return None;
431   if (auto FormValue = find(DW_AT_high_pc)) {
432     if (auto Address = FormValue->getAsAddress()) {
433       // High PC is an address.
434       return Address;
435     }
436     if (auto Offset = FormValue->getAsUnsignedConstant()) {
437       // High PC is an offset from LowPC.
438       return LowPC + *Offset;
439     }
440   }
441   return None;
442 }
443 
444 bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
445                                uint64_t &SectionIndex) const {
446   auto F = find(DW_AT_low_pc);
447   auto LowPcAddr = toSectionedAddress(F);
448   if (!LowPcAddr)
449     return false;
450   if (auto HighPcAddr = getHighPC(LowPcAddr->Address)) {
451     LowPC = LowPcAddr->Address;
452     HighPC = *HighPcAddr;
453     SectionIndex = LowPcAddr->SectionIndex;
454     return true;
455   }
456   return false;
457 }
458 
459 Expected<DWARFAddressRangesVector> DWARFDie::getAddressRanges() const {
460   if (isNULL())
461     return DWARFAddressRangesVector();
462   // Single range specified by low/high PC.
463   uint64_t LowPC, HighPC, Index;
464   if (getLowAndHighPC(LowPC, HighPC, Index))
465     return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
466 
467   Optional<DWARFFormValue> Value = find(DW_AT_ranges);
468   if (Value) {
469     if (Value->getForm() == DW_FORM_rnglistx)
470       return U->findRnglistFromIndex(*Value->getAsSectionOffset());
471     return U->findRnglistFromOffset(*Value->getAsSectionOffset());
472   }
473   return DWARFAddressRangesVector();
474 }
475 
476 void DWARFDie::collectChildrenAddressRanges(
477     DWARFAddressRangesVector &Ranges) const {
478   if (isNULL())
479     return;
480   if (isSubprogramDIE()) {
481     if (auto DIERangesOrError = getAddressRanges())
482       llvm::append_range(Ranges, DIERangesOrError.get());
483     else
484       llvm::consumeError(DIERangesOrError.takeError());
485   }
486 
487   for (auto Child : children())
488     Child.collectChildrenAddressRanges(Ranges);
489 }
490 
491 bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
492   auto RangesOrError = getAddressRanges();
493   if (!RangesOrError) {
494     llvm::consumeError(RangesOrError.takeError());
495     return false;
496   }
497 
498   for (const auto &R : RangesOrError.get())
499     if (R.LowPC <= Address && Address < R.HighPC)
500       return true;
501   return false;
502 }
503 
504 Expected<DWARFLocationExpressionsVector>
505 DWARFDie::getLocations(dwarf::Attribute Attr) const {
506   Optional<DWARFFormValue> Location = find(Attr);
507   if (!Location)
508     return createStringError(inconvertibleErrorCode(), "No %s",
509                              dwarf::AttributeString(Attr).data());
510 
511   if (Optional<uint64_t> Off = Location->getAsSectionOffset()) {
512     uint64_t Offset = *Off;
513 
514     if (Location->getForm() == DW_FORM_loclistx) {
515       if (auto LoclistOffset = U->getLoclistOffset(Offset))
516         Offset = *LoclistOffset;
517       else
518         return createStringError(inconvertibleErrorCode(),
519                                  "Loclist table not found");
520     }
521     return U->findLoclistFromOffset(Offset);
522   }
523 
524   if (Optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) {
525     return DWARFLocationExpressionsVector{
526         DWARFLocationExpression{None, to_vector<4>(*Expr)}};
527   }
528 
529   return createStringError(
530       inconvertibleErrorCode(), "Unsupported %s encoding: %s",
531       dwarf::AttributeString(Attr).data(),
532       dwarf::FormEncodingString(Location->getForm()).data());
533 }
534 
535 const char *DWARFDie::getSubroutineName(DINameKind Kind) const {
536   if (!isSubroutineDIE())
537     return nullptr;
538   return getName(Kind);
539 }
540 
541 const char *DWARFDie::getName(DINameKind Kind) const {
542   if (!isValid() || Kind == DINameKind::None)
543     return nullptr;
544   // Try to get mangled name only if it was asked for.
545   if (Kind == DINameKind::LinkageName) {
546     if (auto Name = getLinkageName())
547       return Name;
548   }
549   return getShortName();
550 }
551 
552 const char *DWARFDie::getShortName() const {
553   if (!isValid())
554     return nullptr;
555 
556   return dwarf::toString(findRecursively(dwarf::DW_AT_name), nullptr);
557 }
558 
559 const char *DWARFDie::getLinkageName() const {
560   if (!isValid())
561     return nullptr;
562 
563   return dwarf::toString(findRecursively({dwarf::DW_AT_MIPS_linkage_name,
564                                           dwarf::DW_AT_linkage_name}),
565                          nullptr);
566 }
567 
568 uint64_t DWARFDie::getDeclLine() const {
569   return toUnsigned(findRecursively(DW_AT_decl_line), 0);
570 }
571 
572 std::string
573 DWARFDie::getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const {
574   std::string FileName;
575   if (auto DeclFile = toUnsigned(findRecursively(DW_AT_decl_file))) {
576     if (const auto *LT = U->getContext().getLineTableForUnit(U)) {
577       LT->getFileNameByIndex(*DeclFile, U->getCompilationDir(), Kind, FileName);
578     }
579   }
580   return FileName;
581 }
582 
583 void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
584                               uint32_t &CallColumn,
585                               uint32_t &CallDiscriminator) const {
586   CallFile = toUnsigned(find(DW_AT_call_file), 0);
587   CallLine = toUnsigned(find(DW_AT_call_line), 0);
588   CallColumn = toUnsigned(find(DW_AT_call_column), 0);
589   CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
590 }
591 
592 /// Helper to dump a DIE with all of its parents, but no siblings.
593 static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
594                                 DIDumpOptions DumpOpts, unsigned Depth = 0) {
595   if (!Die)
596     return Indent;
597   if (DumpOpts.ParentRecurseDepth > 0 && Depth >= DumpOpts.ParentRecurseDepth)
598     return Indent;
599   Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts, Depth + 1);
600   Die.dump(OS, Indent, DumpOpts);
601   return Indent + 2;
602 }
603 
604 void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
605                     DIDumpOptions DumpOpts) const {
606   if (!isValid())
607     return;
608   DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
609   const uint64_t Offset = getOffset();
610   uint64_t offset = Offset;
611   if (DumpOpts.ShowParents) {
612     DIDumpOptions ParentDumpOpts = DumpOpts;
613     ParentDumpOpts.ShowParents = false;
614     ParentDumpOpts.ShowChildren = false;
615     Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
616   }
617 
618   if (debug_info_data.isValidOffset(offset)) {
619     uint32_t abbrCode = debug_info_data.getULEB128(&offset);
620     if (DumpOpts.ShowAddresses)
621       WithColor(OS, HighlightColor::Address).get()
622           << format("\n0x%8.8" PRIx64 ": ", Offset);
623 
624     if (abbrCode) {
625       auto AbbrevDecl = getAbbreviationDeclarationPtr();
626       if (AbbrevDecl) {
627         WithColor(OS, HighlightColor::Tag).get().indent(Indent)
628             << formatv("{0}", getTag());
629         if (DumpOpts.Verbose)
630           OS << format(" [%u] %c", abbrCode,
631                        AbbrevDecl->hasChildren() ? '*' : ' ');
632         OS << '\n';
633 
634         // Dump all data in the DIE for the attributes.
635         for (const auto &AttrSpec : AbbrevDecl->attributes()) {
636           if (AttrSpec.Form == DW_FORM_implicit_const) {
637             // We are dumping .debug_info section ,
638             // implicit_const attribute values are not really stored here,
639             // but in .debug_abbrev section. So we just skip such attrs.
640             continue;
641           }
642           dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
643                         Indent, DumpOpts);
644         }
645 
646         DWARFDie child = getFirstChild();
647         if (DumpOpts.ShowChildren && DumpOpts.ChildRecurseDepth > 0 && child) {
648           DumpOpts.ChildRecurseDepth--;
649           DIDumpOptions ChildDumpOpts = DumpOpts;
650           ChildDumpOpts.ShowParents = false;
651           while (child) {
652             child.dump(OS, Indent + 2, ChildDumpOpts);
653             child = child.getSibling();
654           }
655         }
656       } else {
657         OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
658            << abbrCode << '\n';
659       }
660     } else {
661       OS.indent(Indent) << "NULL\n";
662     }
663   }
664 }
665 
666 LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
667 
668 DWARFDie DWARFDie::getParent() const {
669   if (isValid())
670     return U->getParent(Die);
671   return DWARFDie();
672 }
673 
674 DWARFDie DWARFDie::getSibling() const {
675   if (isValid())
676     return U->getSibling(Die);
677   return DWARFDie();
678 }
679 
680 DWARFDie DWARFDie::getPreviousSibling() const {
681   if (isValid())
682     return U->getPreviousSibling(Die);
683   return DWARFDie();
684 }
685 
686 DWARFDie DWARFDie::getFirstChild() const {
687   if (isValid())
688     return U->getFirstChild(Die);
689   return DWARFDie();
690 }
691 
692 DWARFDie DWARFDie::getLastChild() const {
693   if (isValid())
694     return U->getLastChild(Die);
695   return DWARFDie();
696 }
697 
698 iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const {
699   return make_range(attribute_iterator(*this, false),
700                     attribute_iterator(*this, true));
701 }
702 
703 DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End)
704     : Die(D), Index(0) {
705   auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
706   assert(AbbrDecl && "Must have abbreviation declaration");
707   if (End) {
708     // This is the end iterator so we set the index to the attribute count.
709     Index = AbbrDecl->getNumAttributes();
710   } else {
711     // This is the begin iterator so we extract the value for this->Index.
712     AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
713     updateForIndex(*AbbrDecl, 0);
714   }
715 }
716 
717 void DWARFDie::attribute_iterator::updateForIndex(
718     const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
719   Index = I;
720   // AbbrDecl must be valid before calling this function.
721   auto NumAttrs = AbbrDecl.getNumAttributes();
722   if (Index < NumAttrs) {
723     AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
724     // Add the previous byte size of any previous attribute value.
725     AttrValue.Offset += AttrValue.ByteSize;
726     uint64_t ParseOffset = AttrValue.Offset;
727     auto U = Die.getDwarfUnit();
728     assert(U && "Die must have valid DWARF unit");
729     AttrValue.Value = DWARFFormValue::createFromUnit(
730         AbbrDecl.getFormByIndex(Index), U, &ParseOffset);
731     AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
732   } else {
733     assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
734     AttrValue = {};
735   }
736 }
737 
738 DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
739   if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
740     updateForIndex(*AbbrDecl, Index + 1);
741   return *this;
742 }
743 
744 bool DWARFAttribute::mayHaveLocationDescription(dwarf::Attribute Attr) {
745   switch (Attr) {
746   // From the DWARF v5 specification.
747   case DW_AT_location:
748   case DW_AT_byte_size:
749   case DW_AT_bit_size:
750   case DW_AT_string_length:
751   case DW_AT_lower_bound:
752   case DW_AT_return_addr:
753   case DW_AT_bit_stride:
754   case DW_AT_upper_bound:
755   case DW_AT_count:
756   case DW_AT_data_member_location:
757   case DW_AT_frame_base:
758   case DW_AT_segment:
759   case DW_AT_static_link:
760   case DW_AT_use_location:
761   case DW_AT_vtable_elem_location:
762   case DW_AT_allocated:
763   case DW_AT_associated:
764   case DW_AT_byte_stride:
765   case DW_AT_rank:
766   case DW_AT_call_value:
767   case DW_AT_call_origin:
768   case DW_AT_call_target:
769   case DW_AT_call_target_clobbered:
770   case DW_AT_call_data_location:
771   case DW_AT_call_data_value:
772   // Extensions.
773   case DW_AT_GNU_call_site_value:
774   case DW_AT_GNU_call_site_target:
775     return true;
776   default:
777     return false;
778   }
779 }
780