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