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