xref: /openbsd-src/gnu/llvm/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick //===- DWARFDie.cpp -------------------------------------------------------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick 
909467b48Spatrick #include "llvm/DebugInfo/DWARF/DWARFDie.h"
1009467b48Spatrick #include "llvm/ADT/SmallSet.h"
1109467b48Spatrick #include "llvm/ADT/StringRef.h"
1209467b48Spatrick #include "llvm/BinaryFormat/Dwarf.h"
1309467b48Spatrick #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
1409467b48Spatrick #include "llvm/DebugInfo/DWARF/DWARFContext.h"
15*d415bd75Srobert #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
16*d415bd75Srobert #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
1709467b48Spatrick #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
1809467b48Spatrick #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
19*d415bd75Srobert #include "llvm/DebugInfo/DWARF/DWARFTypePrinter.h"
20*d415bd75Srobert #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
2109467b48Spatrick #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
2209467b48Spatrick #include "llvm/Object/ObjectFile.h"
2309467b48Spatrick #include "llvm/Support/DataExtractor.h"
2409467b48Spatrick #include "llvm/Support/Format.h"
2509467b48Spatrick #include "llvm/Support/FormatVariadic.h"
2609467b48Spatrick #include "llvm/Support/MathExtras.h"
2709467b48Spatrick #include "llvm/Support/WithColor.h"
2809467b48Spatrick #include "llvm/Support/raw_ostream.h"
2909467b48Spatrick #include <cassert>
3009467b48Spatrick #include <cinttypes>
3109467b48Spatrick #include <cstdint>
3209467b48Spatrick #include <string>
3309467b48Spatrick #include <utility>
3409467b48Spatrick 
3509467b48Spatrick using namespace llvm;
3609467b48Spatrick using namespace dwarf;
3709467b48Spatrick using namespace object;
3809467b48Spatrick 
dumpApplePropertyAttribute(raw_ostream & OS,uint64_t Val)3909467b48Spatrick static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
4009467b48Spatrick   OS << " (";
4109467b48Spatrick   do {
4209467b48Spatrick     uint64_t Shift = countTrailingZeros(Val);
4309467b48Spatrick     assert(Shift < 64 && "undefined behavior");
4409467b48Spatrick     uint64_t Bit = 1ULL << Shift;
4509467b48Spatrick     auto PropName = ApplePropertyString(Bit);
4609467b48Spatrick     if (!PropName.empty())
4709467b48Spatrick       OS << PropName;
4809467b48Spatrick     else
4909467b48Spatrick       OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
5009467b48Spatrick     if (!(Val ^= Bit))
5109467b48Spatrick       break;
5209467b48Spatrick     OS << ", ";
5309467b48Spatrick   } while (true);
5409467b48Spatrick   OS << ")";
5509467b48Spatrick }
5609467b48Spatrick 
dumpRanges(const DWARFObject & Obj,raw_ostream & OS,const DWARFAddressRangesVector & Ranges,unsigned AddressSize,unsigned Indent,const DIDumpOptions & DumpOpts)5709467b48Spatrick static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
5809467b48Spatrick                        const DWARFAddressRangesVector &Ranges,
5909467b48Spatrick                        unsigned AddressSize, unsigned Indent,
6009467b48Spatrick                        const DIDumpOptions &DumpOpts) {
6109467b48Spatrick   if (!DumpOpts.ShowAddresses)
6209467b48Spatrick     return;
6309467b48Spatrick 
6409467b48Spatrick   for (const DWARFAddressRange &R : Ranges) {
6509467b48Spatrick     OS << '\n';
6609467b48Spatrick     OS.indent(Indent);
6709467b48Spatrick     R.dump(OS, AddressSize, DumpOpts, &Obj);
6809467b48Spatrick   }
6909467b48Spatrick }
7009467b48Spatrick 
dumpLocationList(raw_ostream & OS,const DWARFFormValue & FormValue,DWARFUnit * U,unsigned Indent,DIDumpOptions DumpOpts)7173471bf0Spatrick static void dumpLocationList(raw_ostream &OS, const DWARFFormValue &FormValue,
7209467b48Spatrick                              DWARFUnit *U, unsigned Indent,
7309467b48Spatrick                              DIDumpOptions DumpOpts) {
7473471bf0Spatrick   assert(FormValue.isFormClass(DWARFFormValue::FC_SectionOffset) &&
7573471bf0Spatrick          "bad FORM for location list");
7609467b48Spatrick   DWARFContext &Ctx = U->getContext();
7709467b48Spatrick   uint64_t Offset = *FormValue.getAsSectionOffset();
7809467b48Spatrick 
7909467b48Spatrick   if (FormValue.getForm() == DW_FORM_loclistx) {
8009467b48Spatrick     FormValue.dump(OS, DumpOpts);
8109467b48Spatrick 
8209467b48Spatrick     if (auto LoclistOffset = U->getLoclistOffset(Offset))
8309467b48Spatrick       Offset = *LoclistOffset;
8409467b48Spatrick     else
8509467b48Spatrick       return;
8609467b48Spatrick   }
87*d415bd75Srobert   U->getLocationTable().dumpLocationList(
88*d415bd75Srobert       &Offset, OS, U->getBaseAddress(), Ctx.getDWARFObj(), U, DumpOpts, Indent);
8909467b48Spatrick }
9009467b48Spatrick 
dumpLocationExpr(raw_ostream & OS,const DWARFFormValue & FormValue,DWARFUnit * U,unsigned Indent,DIDumpOptions DumpOpts)9173471bf0Spatrick static void dumpLocationExpr(raw_ostream &OS, const DWARFFormValue &FormValue,
9273471bf0Spatrick                              DWARFUnit *U, unsigned Indent,
9373471bf0Spatrick                              DIDumpOptions DumpOpts) {
9473471bf0Spatrick   assert((FormValue.isFormClass(DWARFFormValue::FC_Block) ||
9573471bf0Spatrick           FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) &&
9673471bf0Spatrick          "bad FORM for location expression");
9773471bf0Spatrick   DWARFContext &Ctx = U->getContext();
9873471bf0Spatrick   ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
9973471bf0Spatrick   DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
10073471bf0Spatrick                      Ctx.isLittleEndian(), 0);
10173471bf0Spatrick   DWARFExpression(Data, U->getAddressByteSize(), U->getFormParams().Format)
102*d415bd75Srobert       .print(OS, DumpOpts, U);
10309467b48Spatrick }
10409467b48Spatrick 
resolveReferencedType(DWARFDie D,DWARFFormValue F)105*d415bd75Srobert static DWARFDie resolveReferencedType(DWARFDie D, DWARFFormValue F) {
106*d415bd75Srobert   return D.getAttributeValueAsReferencedDie(F).resolveTypeUnitReference();
10709467b48Spatrick }
10809467b48Spatrick 
dumpAttribute(raw_ostream & OS,const DWARFDie & Die,const DWARFAttribute & AttrValue,unsigned Indent,DIDumpOptions DumpOpts)10909467b48Spatrick static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
11073471bf0Spatrick                           const DWARFAttribute &AttrValue, unsigned Indent,
11109467b48Spatrick                           DIDumpOptions DumpOpts) {
11209467b48Spatrick   if (!Die.isValid())
11309467b48Spatrick     return;
11409467b48Spatrick   const char BaseIndent[] = "            ";
11509467b48Spatrick   OS << BaseIndent;
11609467b48Spatrick   OS.indent(Indent + 2);
11773471bf0Spatrick   dwarf::Attribute Attr = AttrValue.Attr;
11809467b48Spatrick   WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
11909467b48Spatrick 
12073471bf0Spatrick   dwarf::Form Form = AttrValue.Value.getForm();
12109467b48Spatrick   if (DumpOpts.Verbose || DumpOpts.ShowForm)
12209467b48Spatrick     OS << formatv(" [{0}]", Form);
12309467b48Spatrick 
12409467b48Spatrick   DWARFUnit *U = Die.getDwarfUnit();
12573471bf0Spatrick   const DWARFFormValue &FormValue = AttrValue.Value;
12609467b48Spatrick 
12709467b48Spatrick   OS << "\t(";
12809467b48Spatrick 
12909467b48Spatrick   StringRef Name;
13009467b48Spatrick   std::string File;
13109467b48Spatrick   auto Color = HighlightColor::Enumerator;
13209467b48Spatrick   if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
13309467b48Spatrick     Color = HighlightColor::String;
134*d415bd75Srobert     if (const auto *LT = U->getContext().getLineTableForUnit(U)) {
135*d415bd75Srobert       if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant()) {
13609467b48Spatrick         if (LT->getFileNameByIndex(
137*d415bd75Srobert                 *Val, U->getCompilationDir(),
138*d415bd75Srobert                 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
139*d415bd75Srobert                 File)) {
14009467b48Spatrick           File = '"' + File + '"';
14109467b48Spatrick           Name = File;
14209467b48Spatrick         }
143*d415bd75Srobert       }
144*d415bd75Srobert     }
145*d415bd75Srobert   } else if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
14609467b48Spatrick     Name = AttributeValueString(Attr, *Val);
14709467b48Spatrick 
14809467b48Spatrick   if (!Name.empty())
14909467b48Spatrick     WithColor(OS, Color) << Name;
150*d415bd75Srobert   else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line) {
151*d415bd75Srobert     if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
152*d415bd75Srobert       OS << *Val;
153*d415bd75Srobert     else
154*d415bd75Srobert       FormValue.dump(OS, DumpOpts);
155*d415bd75Srobert   } else if (Attr == DW_AT_low_pc &&
15673471bf0Spatrick              (FormValue.getAsAddress() ==
15773471bf0Spatrick               dwarf::computeTombstoneAddress(U->getAddressByteSize()))) {
15873471bf0Spatrick     if (DumpOpts.Verbose) {
15973471bf0Spatrick       FormValue.dump(OS, DumpOpts);
16073471bf0Spatrick       OS << " (";
16173471bf0Spatrick     }
16273471bf0Spatrick     OS << "dead code";
16373471bf0Spatrick     if (DumpOpts.Verbose)
16473471bf0Spatrick       OS << ')';
16573471bf0Spatrick   } else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
16609467b48Spatrick              FormValue.getAsUnsignedConstant()) {
16709467b48Spatrick     if (DumpOpts.ShowAddresses) {
16809467b48Spatrick       // Print the actual address rather than the offset.
16909467b48Spatrick       uint64_t LowPC, HighPC, Index;
17009467b48Spatrick       if (Die.getLowAndHighPC(LowPC, HighPC, Index))
17173471bf0Spatrick         DWARFFormValue::dumpAddress(OS, U->getAddressByteSize(), HighPC);
17209467b48Spatrick       else
17309467b48Spatrick         FormValue.dump(OS, DumpOpts);
17409467b48Spatrick     }
17573471bf0Spatrick   } else if (DWARFAttribute::mayHaveLocationList(Attr) &&
17673471bf0Spatrick              FormValue.isFormClass(DWARFFormValue::FC_SectionOffset))
17773471bf0Spatrick     dumpLocationList(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4,
17873471bf0Spatrick                      DumpOpts);
17973471bf0Spatrick   else if (FormValue.isFormClass(DWARFFormValue::FC_Exprloc) ||
18073471bf0Spatrick            (DWARFAttribute::mayHaveLocationExpr(Attr) &&
18173471bf0Spatrick             FormValue.isFormClass(DWARFFormValue::FC_Block)))
18273471bf0Spatrick     dumpLocationExpr(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4,
18373471bf0Spatrick                      DumpOpts);
18409467b48Spatrick   else
18509467b48Spatrick     FormValue.dump(OS, DumpOpts);
18609467b48Spatrick 
18709467b48Spatrick   std::string Space = DumpOpts.ShowAddresses ? " " : "";
18809467b48Spatrick 
18909467b48Spatrick   // We have dumped the attribute raw value. For some attributes
19009467b48Spatrick   // having both the raw value and the pretty-printed value is
19109467b48Spatrick   // interesting. These attributes are handled below.
19209467b48Spatrick   if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
19309467b48Spatrick     if (const char *Name =
19409467b48Spatrick             Die.getAttributeValueAsReferencedDie(FormValue).getName(
19509467b48Spatrick                 DINameKind::LinkageName))
19609467b48Spatrick       OS << Space << "\"" << Name << '\"';
197*d415bd75Srobert   } else if (Attr == DW_AT_type || Attr == DW_AT_containing_type) {
198*d415bd75Srobert     DWARFDie D = resolveReferencedType(Die, FormValue);
199*d415bd75Srobert     if (D && !D.isNULL()) {
20009467b48Spatrick       OS << Space << "\"";
201*d415bd75Srobert       dumpTypeQualifiedName(D, OS);
20209467b48Spatrick       OS << '"';
203*d415bd75Srobert     }
20409467b48Spatrick   } else if (Attr == DW_AT_APPLE_property_attribute) {
205*d415bd75Srobert     if (std::optional<uint64_t> OptVal = FormValue.getAsUnsignedConstant())
20609467b48Spatrick       dumpApplePropertyAttribute(OS, *OptVal);
20709467b48Spatrick   } else if (Attr == DW_AT_ranges) {
20809467b48Spatrick     const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
20909467b48Spatrick     // For DW_FORM_rnglistx we need to dump the offset separately, since
21009467b48Spatrick     // we have only dumped the index so far.
21109467b48Spatrick     if (FormValue.getForm() == DW_FORM_rnglistx)
21209467b48Spatrick       if (auto RangeListOffset =
21309467b48Spatrick               U->getRnglistOffset(*FormValue.getAsSectionOffset())) {
21409467b48Spatrick         DWARFFormValue FV = DWARFFormValue::createFromUValue(
21509467b48Spatrick             dwarf::DW_FORM_sec_offset, *RangeListOffset);
21609467b48Spatrick         FV.dump(OS, DumpOpts);
21709467b48Spatrick       }
21809467b48Spatrick     if (auto RangesOrError = Die.getAddressRanges())
21909467b48Spatrick       dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
22009467b48Spatrick                  sizeof(BaseIndent) + Indent + 4, DumpOpts);
22109467b48Spatrick     else
222097a140dSpatrick       DumpOpts.RecoverableErrorHandler(createStringError(
223097a140dSpatrick           errc::invalid_argument, "decoding address ranges: %s",
224097a140dSpatrick           toString(RangesOrError.takeError()).c_str()));
22509467b48Spatrick   }
22609467b48Spatrick 
22709467b48Spatrick   OS << ")\n";
22809467b48Spatrick }
22909467b48Spatrick 
getFullName(raw_string_ostream & OS,std::string * OriginalFullName) const230*d415bd75Srobert void DWARFDie::getFullName(raw_string_ostream &OS,
231*d415bd75Srobert                            std::string *OriginalFullName) const {
232*d415bd75Srobert   const char *NamePtr = getShortName();
233*d415bd75Srobert   if (!NamePtr)
234*d415bd75Srobert     return;
235*d415bd75Srobert   if (getTag() == DW_TAG_GNU_template_parameter_pack)
236*d415bd75Srobert     return;
237*d415bd75Srobert   dumpTypeUnqualifiedName(*this, OS, OriginalFullName);
238*d415bd75Srobert }
239*d415bd75Srobert 
isSubprogramDIE() const24009467b48Spatrick bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
24109467b48Spatrick 
isSubroutineDIE() const24209467b48Spatrick bool DWARFDie::isSubroutineDIE() const {
24309467b48Spatrick   auto Tag = getTag();
24409467b48Spatrick   return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
24509467b48Spatrick }
24609467b48Spatrick 
find(dwarf::Attribute Attr) const247*d415bd75Srobert std::optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
24809467b48Spatrick   if (!isValid())
249*d415bd75Srobert     return std::nullopt;
25009467b48Spatrick   auto AbbrevDecl = getAbbreviationDeclarationPtr();
25109467b48Spatrick   if (AbbrevDecl)
25209467b48Spatrick     return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
253*d415bd75Srobert   return std::nullopt;
25409467b48Spatrick }
25509467b48Spatrick 
256*d415bd75Srobert std::optional<DWARFFormValue>
find(ArrayRef<dwarf::Attribute> Attrs) const25709467b48Spatrick DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
25809467b48Spatrick   if (!isValid())
259*d415bd75Srobert     return std::nullopt;
26009467b48Spatrick   auto AbbrevDecl = getAbbreviationDeclarationPtr();
26109467b48Spatrick   if (AbbrevDecl) {
26209467b48Spatrick     for (auto Attr : Attrs) {
26309467b48Spatrick       if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
26409467b48Spatrick         return Value;
26509467b48Spatrick     }
26609467b48Spatrick   }
267*d415bd75Srobert   return std::nullopt;
26809467b48Spatrick }
26909467b48Spatrick 
270*d415bd75Srobert std::optional<DWARFFormValue>
findRecursively(ArrayRef<dwarf::Attribute> Attrs) const27109467b48Spatrick DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
272097a140dSpatrick   SmallVector<DWARFDie, 3> Worklist;
27309467b48Spatrick   Worklist.push_back(*this);
27409467b48Spatrick 
27509467b48Spatrick   // Keep track if DIEs already seen to prevent infinite recursion.
27609467b48Spatrick   // Empirically we rarely see a depth of more than 3 when dealing with valid
27709467b48Spatrick   // DWARF. This corresponds to following the DW_AT_abstract_origin and
27809467b48Spatrick   // DW_AT_specification just once.
27909467b48Spatrick   SmallSet<DWARFDie, 3> Seen;
28009467b48Spatrick   Seen.insert(*this);
28109467b48Spatrick 
28209467b48Spatrick   while (!Worklist.empty()) {
28373471bf0Spatrick     DWARFDie Die = Worklist.pop_back_val();
28409467b48Spatrick 
28509467b48Spatrick     if (!Die.isValid())
28609467b48Spatrick       continue;
28709467b48Spatrick 
28809467b48Spatrick     if (auto Value = Die.find(Attrs))
28909467b48Spatrick       return Value;
29009467b48Spatrick 
29109467b48Spatrick     if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
29209467b48Spatrick       if (Seen.insert(D).second)
29309467b48Spatrick         Worklist.push_back(D);
29409467b48Spatrick 
29509467b48Spatrick     if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
29609467b48Spatrick       if (Seen.insert(D).second)
29709467b48Spatrick         Worklist.push_back(D);
29809467b48Spatrick   }
29909467b48Spatrick 
300*d415bd75Srobert   return std::nullopt;
30109467b48Spatrick }
30209467b48Spatrick 
30309467b48Spatrick DWARFDie
getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const30409467b48Spatrick DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
305*d415bd75Srobert   if (std::optional<DWARFFormValue> F = find(Attr))
30609467b48Spatrick     return getAttributeValueAsReferencedDie(*F);
30709467b48Spatrick   return DWARFDie();
30809467b48Spatrick }
30909467b48Spatrick 
31009467b48Spatrick DWARFDie
getAttributeValueAsReferencedDie(const DWARFFormValue & V) const31109467b48Spatrick DWARFDie::getAttributeValueAsReferencedDie(const DWARFFormValue &V) const {
312*d415bd75Srobert   DWARFDie Result;
31309467b48Spatrick   if (auto SpecRef = V.getAsRelativeReference()) {
31409467b48Spatrick     if (SpecRef->Unit)
315*d415bd75Srobert       Result = SpecRef->Unit->getDIEForOffset(SpecRef->Unit->getOffset() +
316*d415bd75Srobert                                               SpecRef->Offset);
317*d415bd75Srobert     else if (auto SpecUnit =
318*d415bd75Srobert                  U->getUnitVector().getUnitForOffset(SpecRef->Offset))
319*d415bd75Srobert       Result = SpecUnit->getDIEForOffset(SpecRef->Offset);
32009467b48Spatrick   }
321*d415bd75Srobert   return Result;
32209467b48Spatrick }
32309467b48Spatrick 
resolveTypeUnitReference() const324*d415bd75Srobert DWARFDie DWARFDie::resolveTypeUnitReference() const {
325*d415bd75Srobert   if (auto Attr = find(DW_AT_signature)) {
326*d415bd75Srobert     if (std::optional<uint64_t> Sig = Attr->getAsReferenceUVal()) {
327*d415bd75Srobert       if (DWARFTypeUnit *TU = U->getContext().getTypeUnitForHash(
328*d415bd75Srobert               U->getVersion(), *Sig, U->isDWOUnit()))
329*d415bd75Srobert         return TU->getDIEForOffset(TU->getTypeOffset() + TU->getOffset());
330*d415bd75Srobert     }
331*d415bd75Srobert   }
332*d415bd75Srobert   return *this;
333*d415bd75Srobert }
334*d415bd75Srobert 
getRangesBaseAttribute() const335*d415bd75Srobert std::optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
33609467b48Spatrick   return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
33709467b48Spatrick }
33809467b48Spatrick 
getLocBaseAttribute() const339*d415bd75Srobert std::optional<uint64_t> DWARFDie::getLocBaseAttribute() const {
34009467b48Spatrick   return toSectionOffset(find(DW_AT_loclists_base));
34109467b48Spatrick }
34209467b48Spatrick 
getHighPC(uint64_t LowPC) const343*d415bd75Srobert std::optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
34473471bf0Spatrick   uint64_t Tombstone = dwarf::computeTombstoneAddress(U->getAddressByteSize());
34573471bf0Spatrick   if (LowPC == Tombstone)
346*d415bd75Srobert     return std::nullopt;
34709467b48Spatrick   if (auto FormValue = find(DW_AT_high_pc)) {
34809467b48Spatrick     if (auto Address = FormValue->getAsAddress()) {
34909467b48Spatrick       // High PC is an address.
35009467b48Spatrick       return Address;
35109467b48Spatrick     }
35209467b48Spatrick     if (auto Offset = FormValue->getAsUnsignedConstant()) {
35309467b48Spatrick       // High PC is an offset from LowPC.
35409467b48Spatrick       return LowPC + *Offset;
35509467b48Spatrick     }
35609467b48Spatrick   }
357*d415bd75Srobert   return std::nullopt;
35809467b48Spatrick }
35909467b48Spatrick 
getLowAndHighPC(uint64_t & LowPC,uint64_t & HighPC,uint64_t & SectionIndex) const36009467b48Spatrick bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
36109467b48Spatrick                                uint64_t &SectionIndex) const {
36209467b48Spatrick   auto F = find(DW_AT_low_pc);
36309467b48Spatrick   auto LowPcAddr = toSectionedAddress(F);
36409467b48Spatrick   if (!LowPcAddr)
36509467b48Spatrick     return false;
36609467b48Spatrick   if (auto HighPcAddr = getHighPC(LowPcAddr->Address)) {
36709467b48Spatrick     LowPC = LowPcAddr->Address;
36809467b48Spatrick     HighPC = *HighPcAddr;
36909467b48Spatrick     SectionIndex = LowPcAddr->SectionIndex;
37009467b48Spatrick     return true;
37109467b48Spatrick   }
37209467b48Spatrick   return false;
37309467b48Spatrick }
37409467b48Spatrick 
getAddressRanges() const37509467b48Spatrick Expected<DWARFAddressRangesVector> DWARFDie::getAddressRanges() const {
37609467b48Spatrick   if (isNULL())
37709467b48Spatrick     return DWARFAddressRangesVector();
37809467b48Spatrick   // Single range specified by low/high PC.
37909467b48Spatrick   uint64_t LowPC, HighPC, Index;
38009467b48Spatrick   if (getLowAndHighPC(LowPC, HighPC, Index))
38109467b48Spatrick     return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
38209467b48Spatrick 
383*d415bd75Srobert   std::optional<DWARFFormValue> Value = find(DW_AT_ranges);
38409467b48Spatrick   if (Value) {
38509467b48Spatrick     if (Value->getForm() == DW_FORM_rnglistx)
38609467b48Spatrick       return U->findRnglistFromIndex(*Value->getAsSectionOffset());
38709467b48Spatrick     return U->findRnglistFromOffset(*Value->getAsSectionOffset());
38809467b48Spatrick   }
38909467b48Spatrick   return DWARFAddressRangesVector();
39009467b48Spatrick }
39109467b48Spatrick 
addressRangeContainsAddress(const uint64_t Address) const39209467b48Spatrick bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
39309467b48Spatrick   auto RangesOrError = getAddressRanges();
39409467b48Spatrick   if (!RangesOrError) {
39509467b48Spatrick     llvm::consumeError(RangesOrError.takeError());
39609467b48Spatrick     return false;
39709467b48Spatrick   }
39809467b48Spatrick 
39909467b48Spatrick   for (const auto &R : RangesOrError.get())
40009467b48Spatrick     if (R.LowPC <= Address && Address < R.HighPC)
40109467b48Spatrick       return true;
40209467b48Spatrick   return false;
40309467b48Spatrick }
40409467b48Spatrick 
40509467b48Spatrick Expected<DWARFLocationExpressionsVector>
getLocations(dwarf::Attribute Attr) const40609467b48Spatrick DWARFDie::getLocations(dwarf::Attribute Attr) const {
407*d415bd75Srobert   std::optional<DWARFFormValue> Location = find(Attr);
40809467b48Spatrick   if (!Location)
40909467b48Spatrick     return createStringError(inconvertibleErrorCode(), "No %s",
41009467b48Spatrick                              dwarf::AttributeString(Attr).data());
41109467b48Spatrick 
412*d415bd75Srobert   if (std::optional<uint64_t> Off = Location->getAsSectionOffset()) {
41309467b48Spatrick     uint64_t Offset = *Off;
41409467b48Spatrick 
41509467b48Spatrick     if (Location->getForm() == DW_FORM_loclistx) {
41609467b48Spatrick       if (auto LoclistOffset = U->getLoclistOffset(Offset))
41709467b48Spatrick         Offset = *LoclistOffset;
41809467b48Spatrick       else
41909467b48Spatrick         return createStringError(inconvertibleErrorCode(),
42009467b48Spatrick                                  "Loclist table not found");
42109467b48Spatrick     }
42209467b48Spatrick     return U->findLoclistFromOffset(Offset);
42309467b48Spatrick   }
42409467b48Spatrick 
425*d415bd75Srobert   if (std::optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) {
42609467b48Spatrick     return DWARFLocationExpressionsVector{
427*d415bd75Srobert         DWARFLocationExpression{std::nullopt, to_vector<4>(*Expr)}};
42809467b48Spatrick   }
42909467b48Spatrick 
43009467b48Spatrick   return createStringError(
43109467b48Spatrick       inconvertibleErrorCode(), "Unsupported %s encoding: %s",
43209467b48Spatrick       dwarf::AttributeString(Attr).data(),
43309467b48Spatrick       dwarf::FormEncodingString(Location->getForm()).data());
43409467b48Spatrick }
43509467b48Spatrick 
getSubroutineName(DINameKind Kind) const43609467b48Spatrick const char *DWARFDie::getSubroutineName(DINameKind Kind) const {
43709467b48Spatrick   if (!isSubroutineDIE())
43809467b48Spatrick     return nullptr;
43909467b48Spatrick   return getName(Kind);
44009467b48Spatrick }
44109467b48Spatrick 
getName(DINameKind Kind) const44209467b48Spatrick const char *DWARFDie::getName(DINameKind Kind) const {
44309467b48Spatrick   if (!isValid() || Kind == DINameKind::None)
44409467b48Spatrick     return nullptr;
44509467b48Spatrick   // Try to get mangled name only if it was asked for.
44609467b48Spatrick   if (Kind == DINameKind::LinkageName) {
447097a140dSpatrick     if (auto Name = getLinkageName())
44809467b48Spatrick       return Name;
44909467b48Spatrick   }
450097a140dSpatrick   return getShortName();
451097a140dSpatrick }
452097a140dSpatrick 
getShortName() const453097a140dSpatrick const char *DWARFDie::getShortName() const {
454097a140dSpatrick   if (!isValid())
45509467b48Spatrick     return nullptr;
456097a140dSpatrick 
457097a140dSpatrick   return dwarf::toString(findRecursively(dwarf::DW_AT_name), nullptr);
458097a140dSpatrick }
459097a140dSpatrick 
getLinkageName() const460097a140dSpatrick const char *DWARFDie::getLinkageName() const {
461097a140dSpatrick   if (!isValid())
462097a140dSpatrick     return nullptr;
463097a140dSpatrick 
464097a140dSpatrick   return dwarf::toString(findRecursively({dwarf::DW_AT_MIPS_linkage_name,
465097a140dSpatrick                                           dwarf::DW_AT_linkage_name}),
466097a140dSpatrick                          nullptr);
46709467b48Spatrick }
46809467b48Spatrick 
getDeclLine() const46909467b48Spatrick uint64_t DWARFDie::getDeclLine() const {
47009467b48Spatrick   return toUnsigned(findRecursively(DW_AT_decl_line), 0);
47109467b48Spatrick }
47209467b48Spatrick 
47373471bf0Spatrick std::string
getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const47473471bf0Spatrick DWARFDie::getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const {
475*d415bd75Srobert   if (auto FormValue = findRecursively(DW_AT_decl_file))
476*d415bd75Srobert     if (auto OptString = FormValue->getAsFile(Kind))
477*d415bd75Srobert       return *OptString;
478*d415bd75Srobert   return {};
47973471bf0Spatrick }
48073471bf0Spatrick 
getCallerFrame(uint32_t & CallFile,uint32_t & CallLine,uint32_t & CallColumn,uint32_t & CallDiscriminator) const48109467b48Spatrick void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
48209467b48Spatrick                               uint32_t &CallColumn,
48309467b48Spatrick                               uint32_t &CallDiscriminator) const {
48409467b48Spatrick   CallFile = toUnsigned(find(DW_AT_call_file), 0);
48509467b48Spatrick   CallLine = toUnsigned(find(DW_AT_call_line), 0);
48609467b48Spatrick   CallColumn = toUnsigned(find(DW_AT_call_column), 0);
48709467b48Spatrick   CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
48809467b48Spatrick }
48909467b48Spatrick 
getTypeSize(uint64_t PointerSize)490*d415bd75Srobert std::optional<uint64_t> DWARFDie::getTypeSize(uint64_t PointerSize) {
491*d415bd75Srobert   if (auto SizeAttr = find(DW_AT_byte_size))
492*d415bd75Srobert     if (std::optional<uint64_t> Size = SizeAttr->getAsUnsignedConstant())
493*d415bd75Srobert       return Size;
494*d415bd75Srobert 
495*d415bd75Srobert   switch (getTag()) {
496*d415bd75Srobert   case DW_TAG_pointer_type:
497*d415bd75Srobert   case DW_TAG_reference_type:
498*d415bd75Srobert   case DW_TAG_rvalue_reference_type:
499*d415bd75Srobert     return PointerSize;
500*d415bd75Srobert   case DW_TAG_ptr_to_member_type: {
501*d415bd75Srobert     if (DWARFDie BaseType = getAttributeValueAsReferencedDie(DW_AT_type))
502*d415bd75Srobert       if (BaseType.getTag() == DW_TAG_subroutine_type)
503*d415bd75Srobert         return 2 * PointerSize;
504*d415bd75Srobert     return PointerSize;
505*d415bd75Srobert   }
506*d415bd75Srobert   case DW_TAG_const_type:
507*d415bd75Srobert   case DW_TAG_immutable_type:
508*d415bd75Srobert   case DW_TAG_volatile_type:
509*d415bd75Srobert   case DW_TAG_restrict_type:
510*d415bd75Srobert   case DW_TAG_typedef: {
511*d415bd75Srobert     if (DWARFDie BaseType = getAttributeValueAsReferencedDie(DW_AT_type))
512*d415bd75Srobert       return BaseType.getTypeSize(PointerSize);
513*d415bd75Srobert     break;
514*d415bd75Srobert   }
515*d415bd75Srobert   case DW_TAG_array_type: {
516*d415bd75Srobert     DWARFDie BaseType = getAttributeValueAsReferencedDie(DW_AT_type);
517*d415bd75Srobert     if (!BaseType)
518*d415bd75Srobert       return std::nullopt;
519*d415bd75Srobert     std::optional<uint64_t> BaseSize = BaseType.getTypeSize(PointerSize);
520*d415bd75Srobert     if (!BaseSize)
521*d415bd75Srobert       return std::nullopt;
522*d415bd75Srobert     uint64_t Size = *BaseSize;
523*d415bd75Srobert     for (DWARFDie Child : *this) {
524*d415bd75Srobert       if (Child.getTag() != DW_TAG_subrange_type)
525*d415bd75Srobert         continue;
526*d415bd75Srobert 
527*d415bd75Srobert       if (auto ElemCountAttr = Child.find(DW_AT_count))
528*d415bd75Srobert         if (std::optional<uint64_t> ElemCount =
529*d415bd75Srobert                 ElemCountAttr->getAsUnsignedConstant())
530*d415bd75Srobert           Size *= *ElemCount;
531*d415bd75Srobert       if (auto UpperBoundAttr = Child.find(DW_AT_upper_bound))
532*d415bd75Srobert         if (std::optional<int64_t> UpperBound =
533*d415bd75Srobert                 UpperBoundAttr->getAsSignedConstant()) {
534*d415bd75Srobert           int64_t LowerBound = 0;
535*d415bd75Srobert           if (auto LowerBoundAttr = Child.find(DW_AT_lower_bound))
536*d415bd75Srobert             LowerBound = LowerBoundAttr->getAsSignedConstant().value_or(0);
537*d415bd75Srobert           Size *= *UpperBound - LowerBound + 1;
538*d415bd75Srobert         }
539*d415bd75Srobert     }
540*d415bd75Srobert     return Size;
541*d415bd75Srobert   }
542*d415bd75Srobert   default:
543*d415bd75Srobert     if (DWARFDie BaseType = getAttributeValueAsReferencedDie(DW_AT_type))
544*d415bd75Srobert       return BaseType.getTypeSize(PointerSize);
545*d415bd75Srobert     break;
546*d415bd75Srobert   }
547*d415bd75Srobert   return std::nullopt;
548*d415bd75Srobert }
549*d415bd75Srobert 
55009467b48Spatrick /// Helper to dump a DIE with all of its parents, but no siblings.
dumpParentChain(DWARFDie Die,raw_ostream & OS,unsigned Indent,DIDumpOptions DumpOpts,unsigned Depth=0)55109467b48Spatrick static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
55209467b48Spatrick                                 DIDumpOptions DumpOpts, unsigned Depth = 0) {
55309467b48Spatrick   if (!Die)
55409467b48Spatrick     return Indent;
55509467b48Spatrick   if (DumpOpts.ParentRecurseDepth > 0 && Depth >= DumpOpts.ParentRecurseDepth)
55609467b48Spatrick     return Indent;
55709467b48Spatrick   Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts, Depth + 1);
55809467b48Spatrick   Die.dump(OS, Indent, DumpOpts);
55909467b48Spatrick   return Indent + 2;
56009467b48Spatrick }
56109467b48Spatrick 
dump(raw_ostream & OS,unsigned Indent,DIDumpOptions DumpOpts) const56209467b48Spatrick void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
56309467b48Spatrick                     DIDumpOptions DumpOpts) const {
56409467b48Spatrick   if (!isValid())
56509467b48Spatrick     return;
56609467b48Spatrick   DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
56709467b48Spatrick   const uint64_t Offset = getOffset();
56809467b48Spatrick   uint64_t offset = Offset;
56909467b48Spatrick   if (DumpOpts.ShowParents) {
57009467b48Spatrick     DIDumpOptions ParentDumpOpts = DumpOpts;
57109467b48Spatrick     ParentDumpOpts.ShowParents = false;
57209467b48Spatrick     ParentDumpOpts.ShowChildren = false;
57309467b48Spatrick     Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
57409467b48Spatrick   }
57509467b48Spatrick 
57609467b48Spatrick   if (debug_info_data.isValidOffset(offset)) {
57709467b48Spatrick     uint32_t abbrCode = debug_info_data.getULEB128(&offset);
57809467b48Spatrick     if (DumpOpts.ShowAddresses)
57909467b48Spatrick       WithColor(OS, HighlightColor::Address).get()
58009467b48Spatrick           << format("\n0x%8.8" PRIx64 ": ", Offset);
58109467b48Spatrick 
58209467b48Spatrick     if (abbrCode) {
58309467b48Spatrick       auto AbbrevDecl = getAbbreviationDeclarationPtr();
58409467b48Spatrick       if (AbbrevDecl) {
58509467b48Spatrick         WithColor(OS, HighlightColor::Tag).get().indent(Indent)
58609467b48Spatrick             << formatv("{0}", getTag());
587*d415bd75Srobert         if (DumpOpts.Verbose) {
58809467b48Spatrick           OS << format(" [%u] %c", abbrCode,
58909467b48Spatrick                        AbbrevDecl->hasChildren() ? '*' : ' ');
590*d415bd75Srobert           if (std::optional<uint32_t> ParentIdx = Die->getParentIdx())
591*d415bd75Srobert             OS << format(" (0x%8.8" PRIx64 ")",
592*d415bd75Srobert                          U->getDIEAtIndex(*ParentIdx).getOffset());
593*d415bd75Srobert         }
59409467b48Spatrick         OS << '\n';
59509467b48Spatrick 
59609467b48Spatrick         // Dump all data in the DIE for the attributes.
59773471bf0Spatrick         for (const DWARFAttribute &AttrValue : attributes())
59873471bf0Spatrick           dumpAttribute(OS, *this, AttrValue, Indent, DumpOpts);
59909467b48Spatrick 
60073471bf0Spatrick         if (DumpOpts.ShowChildren && DumpOpts.ChildRecurseDepth > 0) {
60173471bf0Spatrick           DWARFDie Child = getFirstChild();
60209467b48Spatrick           DumpOpts.ChildRecurseDepth--;
60309467b48Spatrick           DIDumpOptions ChildDumpOpts = DumpOpts;
60409467b48Spatrick           ChildDumpOpts.ShowParents = false;
60573471bf0Spatrick           while (Child) {
60673471bf0Spatrick             Child.dump(OS, Indent + 2, ChildDumpOpts);
60773471bf0Spatrick             Child = Child.getSibling();
60809467b48Spatrick           }
60909467b48Spatrick         }
61009467b48Spatrick       } else {
61109467b48Spatrick         OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
61209467b48Spatrick            << abbrCode << '\n';
61309467b48Spatrick       }
61409467b48Spatrick     } else {
61509467b48Spatrick       OS.indent(Indent) << "NULL\n";
61609467b48Spatrick     }
61709467b48Spatrick   }
61809467b48Spatrick }
61909467b48Spatrick 
dump() const62009467b48Spatrick LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
62109467b48Spatrick 
getParent() const62209467b48Spatrick DWARFDie DWARFDie::getParent() const {
62309467b48Spatrick   if (isValid())
62409467b48Spatrick     return U->getParent(Die);
62509467b48Spatrick   return DWARFDie();
62609467b48Spatrick }
62709467b48Spatrick 
getSibling() const62809467b48Spatrick DWARFDie DWARFDie::getSibling() const {
62909467b48Spatrick   if (isValid())
63009467b48Spatrick     return U->getSibling(Die);
63109467b48Spatrick   return DWARFDie();
63209467b48Spatrick }
63309467b48Spatrick 
getPreviousSibling() const63409467b48Spatrick DWARFDie DWARFDie::getPreviousSibling() const {
63509467b48Spatrick   if (isValid())
63609467b48Spatrick     return U->getPreviousSibling(Die);
63709467b48Spatrick   return DWARFDie();
63809467b48Spatrick }
63909467b48Spatrick 
getFirstChild() const64009467b48Spatrick DWARFDie DWARFDie::getFirstChild() const {
64109467b48Spatrick   if (isValid())
64209467b48Spatrick     return U->getFirstChild(Die);
64309467b48Spatrick   return DWARFDie();
64409467b48Spatrick }
64509467b48Spatrick 
getLastChild() const64609467b48Spatrick DWARFDie DWARFDie::getLastChild() const {
64709467b48Spatrick   if (isValid())
64809467b48Spatrick     return U->getLastChild(Die);
64909467b48Spatrick   return DWARFDie();
65009467b48Spatrick }
65109467b48Spatrick 
attributes() const65209467b48Spatrick iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const {
65309467b48Spatrick   return make_range(attribute_iterator(*this, false),
65409467b48Spatrick                     attribute_iterator(*this, true));
65509467b48Spatrick }
65609467b48Spatrick 
attribute_iterator(DWARFDie D,bool End)65709467b48Spatrick DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End)
65809467b48Spatrick     : Die(D), Index(0) {
65909467b48Spatrick   auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
66009467b48Spatrick   assert(AbbrDecl && "Must have abbreviation declaration");
66109467b48Spatrick   if (End) {
66209467b48Spatrick     // This is the end iterator so we set the index to the attribute count.
66309467b48Spatrick     Index = AbbrDecl->getNumAttributes();
66409467b48Spatrick   } else {
66509467b48Spatrick     // This is the begin iterator so we extract the value for this->Index.
66609467b48Spatrick     AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
66709467b48Spatrick     updateForIndex(*AbbrDecl, 0);
66809467b48Spatrick   }
66909467b48Spatrick }
67009467b48Spatrick 
updateForIndex(const DWARFAbbreviationDeclaration & AbbrDecl,uint32_t I)67109467b48Spatrick void DWARFDie::attribute_iterator::updateForIndex(
67209467b48Spatrick     const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
67309467b48Spatrick   Index = I;
67409467b48Spatrick   // AbbrDecl must be valid before calling this function.
67509467b48Spatrick   auto NumAttrs = AbbrDecl.getNumAttributes();
67609467b48Spatrick   if (Index < NumAttrs) {
67709467b48Spatrick     AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
67809467b48Spatrick     // Add the previous byte size of any previous attribute value.
67909467b48Spatrick     AttrValue.Offset += AttrValue.ByteSize;
68009467b48Spatrick     uint64_t ParseOffset = AttrValue.Offset;
68173471bf0Spatrick     if (AbbrDecl.getAttrIsImplicitConstByIndex(Index))
68273471bf0Spatrick       AttrValue.Value = DWARFFormValue::createFromSValue(
68373471bf0Spatrick           AbbrDecl.getFormByIndex(Index),
68473471bf0Spatrick           AbbrDecl.getAttrImplicitConstValueByIndex(Index));
68573471bf0Spatrick     else {
68609467b48Spatrick       auto U = Die.getDwarfUnit();
68709467b48Spatrick       assert(U && "Die must have valid DWARF unit");
68809467b48Spatrick       AttrValue.Value = DWARFFormValue::createFromUnit(
68909467b48Spatrick           AbbrDecl.getFormByIndex(Index), U, &ParseOffset);
69073471bf0Spatrick     }
69109467b48Spatrick     AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
69209467b48Spatrick   } else {
69309467b48Spatrick     assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
69409467b48Spatrick     AttrValue = {};
69509467b48Spatrick   }
69609467b48Spatrick }
69709467b48Spatrick 
operator ++()69809467b48Spatrick DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
69909467b48Spatrick   if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
70009467b48Spatrick     updateForIndex(*AbbrDecl, Index + 1);
70109467b48Spatrick   return *this;
70209467b48Spatrick }
70309467b48Spatrick 
mayHaveLocationList(dwarf::Attribute Attr)70473471bf0Spatrick bool DWARFAttribute::mayHaveLocationList(dwarf::Attribute Attr) {
70573471bf0Spatrick   switch(Attr) {
70673471bf0Spatrick   case DW_AT_location:
70773471bf0Spatrick   case DW_AT_string_length:
70873471bf0Spatrick   case DW_AT_return_addr:
70973471bf0Spatrick   case DW_AT_data_member_location:
71073471bf0Spatrick   case DW_AT_frame_base:
71173471bf0Spatrick   case DW_AT_static_link:
71273471bf0Spatrick   case DW_AT_segment:
71373471bf0Spatrick   case DW_AT_use_location:
71473471bf0Spatrick   case DW_AT_vtable_elem_location:
71573471bf0Spatrick     return true;
71673471bf0Spatrick   default:
71773471bf0Spatrick     return false;
71873471bf0Spatrick   }
71973471bf0Spatrick }
72073471bf0Spatrick 
mayHaveLocationExpr(dwarf::Attribute Attr)72173471bf0Spatrick bool DWARFAttribute::mayHaveLocationExpr(dwarf::Attribute Attr) {
72209467b48Spatrick   switch (Attr) {
72309467b48Spatrick   // From the DWARF v5 specification.
72409467b48Spatrick   case DW_AT_location:
72509467b48Spatrick   case DW_AT_byte_size:
72673471bf0Spatrick   case DW_AT_bit_offset:
72709467b48Spatrick   case DW_AT_bit_size:
72809467b48Spatrick   case DW_AT_string_length:
72909467b48Spatrick   case DW_AT_lower_bound:
73009467b48Spatrick   case DW_AT_return_addr:
73109467b48Spatrick   case DW_AT_bit_stride:
73209467b48Spatrick   case DW_AT_upper_bound:
73309467b48Spatrick   case DW_AT_count:
73409467b48Spatrick   case DW_AT_data_member_location:
73509467b48Spatrick   case DW_AT_frame_base:
73609467b48Spatrick   case DW_AT_segment:
73709467b48Spatrick   case DW_AT_static_link:
73809467b48Spatrick   case DW_AT_use_location:
73909467b48Spatrick   case DW_AT_vtable_elem_location:
74009467b48Spatrick   case DW_AT_allocated:
74109467b48Spatrick   case DW_AT_associated:
74273471bf0Spatrick   case DW_AT_data_location:
74309467b48Spatrick   case DW_AT_byte_stride:
74409467b48Spatrick   case DW_AT_rank:
74509467b48Spatrick   case DW_AT_call_value:
74609467b48Spatrick   case DW_AT_call_origin:
74709467b48Spatrick   case DW_AT_call_target:
74809467b48Spatrick   case DW_AT_call_target_clobbered:
74909467b48Spatrick   case DW_AT_call_data_location:
75009467b48Spatrick   case DW_AT_call_data_value:
75109467b48Spatrick   // Extensions.
75209467b48Spatrick   case DW_AT_GNU_call_site_value:
75309467b48Spatrick   case DW_AT_GNU_call_site_target:
75409467b48Spatrick     return true;
75509467b48Spatrick   default:
75609467b48Spatrick     return false;
75709467b48Spatrick   }
75809467b48Spatrick }
759*d415bd75Srobert 
760*d415bd75Srobert namespace llvm {
761*d415bd75Srobert 
dumpTypeQualifiedName(const DWARFDie & DIE,raw_ostream & OS)762*d415bd75Srobert void dumpTypeQualifiedName(const DWARFDie &DIE, raw_ostream &OS) {
763*d415bd75Srobert   DWARFTypePrinter(OS).appendQualifiedName(DIE);
764*d415bd75Srobert }
765*d415bd75Srobert 
dumpTypeUnqualifiedName(const DWARFDie & DIE,raw_ostream & OS,std::string * OriginalFullName)766*d415bd75Srobert void dumpTypeUnqualifiedName(const DWARFDie &DIE, raw_ostream &OS,
767*d415bd75Srobert                              std::string *OriginalFullName) {
768*d415bd75Srobert   DWARFTypePrinter(OS).appendUnqualifiedName(DIE, OriginalFullName);
769*d415bd75Srobert }
770*d415bd75Srobert 
771*d415bd75Srobert } // namespace llvm
772