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/DWARFTypeUnit.h" 22 #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 23 #include "llvm/Object/ObjectFile.h" 24 #include "llvm/Support/DataExtractor.h" 25 #include "llvm/Support/Format.h" 26 #include "llvm/Support/FormatVariadic.h" 27 #include "llvm/Support/MathExtras.h" 28 #include "llvm/Support/ScopedPrinter.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, 111 dwarf::Attribute Attr = DW_AT_type) { 112 return D.getAttributeValueAsReferencedDie(Attr).resolveTypeUnitReference(); 113 } 114 static DWARFDie resolveReferencedType(DWARFDie D, DWARFFormValue F) { 115 return D.getAttributeValueAsReferencedDie(F).resolveTypeUnitReference(); 116 } 117 118 namespace { 119 120 // FIXME: We should have pretty printers per language. Currently we print 121 // everything as if it was C++ and fall back to the TAG type name. 122 struct DWARFTypePrinter { 123 raw_ostream &OS; 124 bool Word = true; 125 bool EndedWithTemplate = false; 126 127 DWARFTypePrinter(raw_ostream &OS) : OS(OS) {} 128 129 /// Dump the name encoded in the type tag. 130 void appendTypeTagName(dwarf::Tag T) { 131 StringRef TagStr = TagString(T); 132 static constexpr StringRef Prefix = "DW_TAG_"; 133 static constexpr StringRef Suffix = "_type"; 134 if (!TagStr.startswith(Prefix) || !TagStr.endswith(Suffix)) 135 return; 136 OS << TagStr.substr(Prefix.size(), 137 TagStr.size() - (Prefix.size() + Suffix.size())) 138 << " "; 139 } 140 141 void appendArrayType(const DWARFDie &D) { 142 for (const DWARFDie &C : D.children()) { 143 if (C.getTag() != DW_TAG_subrange_type) 144 continue; 145 Optional<uint64_t> LB; 146 Optional<uint64_t> Count; 147 Optional<uint64_t> UB; 148 Optional<unsigned> DefaultLB; 149 if (Optional<DWARFFormValue> L = C.find(DW_AT_lower_bound)) 150 LB = L->getAsUnsignedConstant(); 151 if (Optional<DWARFFormValue> CountV = C.find(DW_AT_count)) 152 Count = CountV->getAsUnsignedConstant(); 153 if (Optional<DWARFFormValue> UpperV = C.find(DW_AT_upper_bound)) 154 UB = UpperV->getAsUnsignedConstant(); 155 if (Optional<DWARFFormValue> LV = 156 D.getDwarfUnit()->getUnitDIE().find(DW_AT_language)) 157 if (Optional<uint64_t> LC = LV->getAsUnsignedConstant()) 158 if ((DefaultLB = 159 LanguageLowerBound(static_cast<dwarf::SourceLanguage>(*LC)))) 160 if (LB && *LB == *DefaultLB) 161 LB = None; 162 if (!LB && !Count && !UB) 163 OS << "[]"; 164 else if (!LB && (Count || UB) && DefaultLB) 165 OS << '[' << (Count ? *Count : *UB - *DefaultLB + 1) << ']'; 166 else { 167 OS << "[["; 168 if (LB) 169 OS << *LB; 170 else 171 OS << '?'; 172 OS << ", "; 173 if (Count) 174 if (LB) 175 OS << *LB + *Count; 176 else 177 OS << "? + " << *Count; 178 else if (UB) 179 OS << *UB + 1; 180 else 181 OS << '?'; 182 OS << ")]"; 183 } 184 } 185 EndedWithTemplate = false; 186 } 187 188 DWARFDie skipQualifiers(DWARFDie D) { 189 while (D && (D.getTag() == DW_TAG_const_type || 190 D.getTag() == DW_TAG_volatile_type)) 191 D = resolveReferencedType(D); 192 return D; 193 } 194 195 bool needsParens(DWARFDie D) { 196 D = skipQualifiers(D); 197 return D && (D.getTag() == DW_TAG_subroutine_type || D.getTag() == DW_TAG_array_type); 198 } 199 200 void appendPointerLikeTypeBefore(DWARFDie D, DWARFDie Inner, StringRef Ptr) { 201 appendQualifiedNameBefore(Inner); 202 if (Word) 203 OS << ' '; 204 if (needsParens(Inner)) 205 OS << '('; 206 OS << Ptr; 207 Word = false; 208 EndedWithTemplate = false; 209 } 210 211 DWARFDie 212 appendUnqualifiedNameBefore(DWARFDie D, 213 std::string *OriginalFullName = nullptr) { 214 Word = true; 215 if (!D) { 216 OS << "void"; 217 return DWARFDie(); 218 } 219 DWARFDie InnerDIE; 220 auto Inner = [&] { return InnerDIE = resolveReferencedType(D); }; 221 const dwarf::Tag T = D.getTag(); 222 switch (T) { 223 case DW_TAG_pointer_type: { 224 appendPointerLikeTypeBefore(D, Inner(), "*"); 225 break; 226 } 227 case DW_TAG_subroutine_type: { 228 appendQualifiedNameBefore(Inner()); 229 if (Word) { 230 OS << ' '; 231 } 232 Word = false; 233 break; 234 } 235 case DW_TAG_array_type: { 236 appendQualifiedNameBefore(Inner()); 237 break; 238 } 239 case DW_TAG_reference_type: 240 appendPointerLikeTypeBefore(D, Inner(), "&"); 241 break; 242 case DW_TAG_rvalue_reference_type: 243 appendPointerLikeTypeBefore(D, Inner(), "&&"); 244 break; 245 case DW_TAG_ptr_to_member_type: { 246 appendQualifiedNameBefore(Inner()); 247 if (needsParens(InnerDIE)) 248 OS << '('; 249 else if (Word) 250 OS << ' '; 251 if (DWARFDie Cont = resolveReferencedType(D, DW_AT_containing_type)) { 252 appendQualifiedName(Cont); 253 EndedWithTemplate = false; 254 OS << "::"; 255 } 256 OS << "*"; 257 Word = false; 258 break; 259 } 260 case DW_TAG_const_type: 261 case DW_TAG_volatile_type: 262 appendConstVolatileQualifierBefore(D); 263 break; 264 case DW_TAG_namespace: { 265 if (const char *Name = dwarf::toString(D.find(DW_AT_name), nullptr)) 266 OS << Name; 267 else 268 OS << "(anonymous namespace)"; 269 break; 270 } 271 case DW_TAG_unspecified_type: { 272 StringRef TypeName = D.getShortName(); 273 if (TypeName == "decltype(nullptr)") 274 TypeName = "std::nullptr_t"; 275 Word = true; 276 OS << TypeName; 277 EndedWithTemplate = false; 278 break; 279 } 280 /* 281 case DW_TAG_structure_type: 282 case DW_TAG_class_type: 283 case DW_TAG_enumeration_type: 284 case DW_TAG_base_type: 285 */ 286 default: { 287 const char *NamePtr = dwarf::toString(D.find(DW_AT_name), nullptr); 288 if (!NamePtr) { 289 appendTypeTagName(D.getTag()); 290 return DWARFDie(); 291 } 292 Word = true; 293 StringRef Name = NamePtr; 294 static constexpr StringRef MangledPrefix = "_STN"; 295 if (Name.startswith(MangledPrefix)) { 296 Name = Name.drop_front(MangledPrefix.size()); 297 auto Separator = Name.find('|'); 298 assert(Separator != StringRef::npos); 299 StringRef BaseName = Name.substr(0, Separator); 300 StringRef TemplateArgs = Name.substr(Separator + 1); 301 if (OriginalFullName) 302 *OriginalFullName = (BaseName + TemplateArgs).str(); 303 Name = BaseName; 304 } else 305 EndedWithTemplate = Name.endswith(">"); 306 OS << Name; 307 // This check would be insufficient for operator overloads like 308 // "operator>>" - but for now Clang doesn't try to simplify them, so this 309 // is OK. Add more nuanced operator overload handling here if/when needed. 310 if (Name.endswith(">")) 311 break; 312 if (!appendTemplateParameters(D)) 313 break; 314 315 if (EndedWithTemplate) 316 OS << ' '; 317 OS << '>'; 318 EndedWithTemplate = true; 319 Word = true; 320 break; 321 } 322 } 323 return InnerDIE; 324 } 325 326 void appendUnqualifiedNameAfter(DWARFDie D, DWARFDie Inner, 327 bool SkipFirstParamIfArtificial = false) { 328 if (!D) 329 return; 330 switch (D.getTag()) { 331 case DW_TAG_subroutine_type: { 332 appendSubroutineNameAfter(D, Inner, SkipFirstParamIfArtificial, false, 333 false); 334 break; 335 } 336 case DW_TAG_array_type: { 337 appendArrayType(D); 338 break; 339 } 340 case DW_TAG_const_type: 341 case DW_TAG_volatile_type: 342 appendConstVolatileQualifierAfter(D); 343 break; 344 case DW_TAG_ptr_to_member_type: 345 case DW_TAG_reference_type: 346 case DW_TAG_rvalue_reference_type: 347 case DW_TAG_pointer_type: { 348 if (needsParens(Inner)) 349 OS << ')'; 350 appendUnqualifiedNameAfter(Inner, resolveReferencedType(Inner), 351 /*SkipFirstParamIfArtificial=*/D.getTag() == 352 DW_TAG_ptr_to_member_type); 353 break; 354 } 355 /* 356 case DW_TAG_structure_type: 357 case DW_TAG_class_type: 358 case DW_TAG_enumeration_type: 359 case DW_TAG_base_type: 360 case DW_TAG_namespace: 361 */ 362 default: 363 break; 364 } 365 } 366 367 void appendQualifiedName(DWARFDie D) { 368 if (D) 369 appendScopes(D.getParent()); 370 appendUnqualifiedName(D); 371 } 372 DWARFDie appendQualifiedNameBefore(DWARFDie D) { 373 if (D) 374 appendScopes(D.getParent()); 375 return appendUnqualifiedNameBefore(D); 376 } 377 bool appendTemplateParameters(DWARFDie D, bool *FirstParameter = nullptr) { 378 bool FirstParameterValue = true; 379 bool IsTemplate = false; 380 if (!FirstParameter) 381 FirstParameter = &FirstParameterValue; 382 for (const DWARFDie &C : D) { 383 auto Sep = [&] { 384 if (*FirstParameter) 385 OS << '<'; 386 else 387 OS << ", "; 388 IsTemplate = true; 389 EndedWithTemplate = false; 390 *FirstParameter = false; 391 }; 392 if (C.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) { 393 IsTemplate = true; 394 appendTemplateParameters(C, FirstParameter); 395 } 396 if (C.getTag() == dwarf::DW_TAG_template_value_parameter) { 397 DWARFDie T = resolveReferencedType(C); 398 Sep(); 399 if (T.getTag() == DW_TAG_enumeration_type) { 400 auto V = C.find(DW_AT_const_value); 401 bool FoundEnumerator = false; 402 for (const DWARFDie &Enumerator : T) { 403 auto EV = Enumerator.find(DW_AT_const_value); 404 if (V && EV && 405 V->getAsSignedConstant() == EV->getAsSignedConstant()) { 406 if (T.find(DW_AT_enum_class)) { 407 appendQualifiedName(T); 408 OS << "::"; 409 } else 410 appendScopes(T.getParent()); 411 OS << Enumerator.getShortName(); 412 FoundEnumerator = true; 413 break; 414 } 415 } 416 if (FoundEnumerator) 417 continue; 418 OS << '('; 419 appendQualifiedName(T); 420 OS << ')'; 421 OS << to_string(*V->getAsSignedConstant()); 422 continue; 423 } 424 // /Maybe/ we could do pointer type parameters, looking for the 425 // symbol in the ELF symbol table to get back to the variable... 426 // but probably not worth it. 427 if (T.getTag() == DW_TAG_pointer_type) 428 continue; 429 const char *RawName = dwarf::toString(T.find(DW_AT_name), nullptr); 430 assert(RawName); 431 StringRef Name = RawName; 432 auto V = C.find(DW_AT_const_value); 433 bool IsQualifiedChar = false; 434 if (Name == "bool") { 435 OS << (*V->getAsUnsignedConstant() ? "true" : "false"); 436 } else if (Name == "short") { 437 OS << "(short)"; 438 OS << to_string(*V->getAsSignedConstant()); 439 } else if (Name == "unsigned short") { 440 OS << "(unsigned short)"; 441 OS << to_string(*V->getAsSignedConstant()); 442 } else if (Name == "int") 443 OS << to_string(*V->getAsSignedConstant()); 444 else if (Name == "long") { 445 OS << to_string(*V->getAsSignedConstant()); 446 OS << "L"; 447 } else if (Name == "long long") { 448 OS << to_string(*V->getAsSignedConstant()); 449 OS << "LL"; 450 } else if (Name == "unsigned int") { 451 OS << to_string(*V->getAsUnsignedConstant()); 452 OS << "U"; 453 } else if (Name == "unsigned long") { 454 OS << to_string(*V->getAsUnsignedConstant()); 455 OS << "UL"; 456 } else if (Name == "unsigned long long") { 457 OS << to_string(*V->getAsUnsignedConstant()); 458 OS << "ULL"; 459 } else if (Name == "char" || 460 (IsQualifiedChar = 461 (Name == "unsigned char" || Name == "signed char"))) { 462 // FIXME: check T's DW_AT_type to see if it's signed or not (since 463 // char signedness is implementation defined). 464 auto Val = *V->getAsSignedConstant(); 465 // Copied/hacked up from Clang's CharacterLiteral::print - incomplete 466 // (doesn't actually support different character types/widths, sign 467 // handling's not done, and doesn't correctly test if a character is 468 // printable or needs to use a numeric escape sequence instead) 469 if (IsQualifiedChar) { 470 OS << '('; 471 OS << Name; 472 OS << ')'; 473 } 474 switch (Val) { 475 case '\\': 476 OS << "'\\\\'"; 477 break; 478 case '\'': 479 OS << "'\\''"; 480 break; 481 case '\a': 482 // TODO: K&R: the meaning of '\\a' is different in traditional C 483 OS << "'\\a'"; 484 break; 485 case '\b': 486 OS << "'\\b'"; 487 break; 488 case '\f': 489 OS << "'\\f'"; 490 break; 491 case '\n': 492 OS << "'\\n'"; 493 break; 494 case '\r': 495 OS << "'\\r'"; 496 break; 497 case '\t': 498 OS << "'\\t'"; 499 break; 500 case '\v': 501 OS << "'\\v'"; 502 break; 503 default: 504 if ((Val & ~0xFFu) == ~0xFFu) 505 Val &= 0xFFu; 506 if (Val < 127 && Val >= 32) { 507 OS << "'"; 508 OS << (char)Val; 509 OS << "'"; 510 } else if (Val < 256) 511 OS << to_string(llvm::format("'\\x%02x'", Val)); 512 else if (Val <= 0xFFFF) 513 OS << to_string(llvm::format("'\\u%04x'", Val)); 514 else 515 OS << to_string(llvm::format("'\\U%08x'", Val)); 516 } 517 } 518 continue; 519 } 520 if (C.getTag() == dwarf::DW_TAG_GNU_template_template_param) { 521 const char *RawName = 522 dwarf::toString(C.find(DW_AT_GNU_template_name), nullptr); 523 assert(RawName); 524 StringRef Name = RawName; 525 Sep(); 526 OS << Name; 527 continue; 528 } 529 if (C.getTag() != dwarf::DW_TAG_template_type_parameter) 530 continue; 531 auto TypeAttr = C.find(DW_AT_type); 532 Sep(); 533 appendQualifiedName(TypeAttr ? resolveReferencedType(C, *TypeAttr) 534 : DWARFDie()); 535 } 536 if (IsTemplate && *FirstParameter && FirstParameter == &FirstParameterValue) { 537 OS << '<'; 538 EndedWithTemplate = false; 539 } 540 return IsTemplate; 541 } 542 void decomposeConstVolatile(DWARFDie &N, DWARFDie &T, DWARFDie &C, 543 DWARFDie &V) { 544 (N.getTag() == DW_TAG_const_type ? C : V) = N; 545 T = resolveReferencedType(N); 546 if (T) { 547 auto Tag = T.getTag(); 548 if (Tag == DW_TAG_const_type) { 549 C = T; 550 T = resolveReferencedType(T); 551 } else if (Tag == DW_TAG_volatile_type) { 552 V = T; 553 T = resolveReferencedType(T); 554 } 555 } 556 } 557 void appendConstVolatileQualifierAfter(DWARFDie N) { 558 DWARFDie C; 559 DWARFDie V; 560 DWARFDie T; 561 decomposeConstVolatile(N, T, C, V); 562 if (T && T.getTag() == DW_TAG_subroutine_type) 563 appendSubroutineNameAfter(T, resolveReferencedType(T), false, C.isValid(), 564 V.isValid()); 565 else 566 appendUnqualifiedNameAfter(T, resolveReferencedType(T)); 567 } 568 void appendConstVolatileQualifierBefore(DWARFDie N) { 569 DWARFDie C; 570 DWARFDie V; 571 DWARFDie T; 572 decomposeConstVolatile(N, T, C, V); 573 bool Subroutine = T && T.getTag() == DW_TAG_subroutine_type; 574 DWARFDie A = T; 575 while (A && A.getTag() == DW_TAG_array_type) 576 A = resolveReferencedType(A); 577 bool Leading = 578 (!A || (A.getTag() != DW_TAG_pointer_type && 579 A.getTag() != llvm::dwarf::DW_TAG_ptr_to_member_type)) && 580 !Subroutine; 581 if (Leading) { 582 if (C) 583 OS << "const "; 584 if (V) 585 OS << "volatile "; 586 } 587 appendQualifiedNameBefore(T); 588 if (!Leading && !Subroutine) { 589 Word = true; 590 if (C) 591 OS << "const"; 592 if (V) { 593 if (C) 594 OS << ' '; 595 OS << "volatile"; 596 } 597 } 598 } 599 600 /// Recursively append the DIE type name when applicable. 601 void appendUnqualifiedName(DWARFDie D, 602 std::string *OriginalFullName = nullptr) { 603 // FIXME: We should have pretty printers per language. Currently we print 604 // everything as if it was C++ and fall back to the TAG type name. 605 DWARFDie Inner = appendUnqualifiedNameBefore(D, OriginalFullName); 606 appendUnqualifiedNameAfter(D, Inner); 607 } 608 609 void appendSubroutineNameAfter(DWARFDie D, DWARFDie Inner, 610 bool SkipFirstParamIfArtificial, bool Const, 611 bool Volatile) { 612 DWARFDie FirstParamIfArtificial; 613 OS << '('; 614 EndedWithTemplate = false; 615 bool First = true; 616 bool RealFirst = true; 617 for (DWARFDie P : D) { 618 if (P.getTag() != DW_TAG_formal_parameter && 619 P.getTag() != DW_TAG_unspecified_parameters) 620 return; 621 DWARFDie T = resolveReferencedType(P); 622 if (SkipFirstParamIfArtificial && RealFirst && P.find(DW_AT_artificial)) { 623 FirstParamIfArtificial = T; 624 RealFirst = false; 625 continue; 626 } 627 if (!First) { 628 OS << ", "; 629 } 630 First = false; 631 if (P.getTag() == DW_TAG_unspecified_parameters) 632 OS << "..."; 633 else 634 appendQualifiedName(T); 635 } 636 EndedWithTemplate = false; 637 OS << ')'; 638 if (FirstParamIfArtificial) { 639 if (DWARFDie P = FirstParamIfArtificial) { 640 if (P.getTag() == DW_TAG_pointer_type) { 641 auto CVStep = [&](DWARFDie CV) { 642 if (DWARFDie U = resolveReferencedType(CV)) { 643 Const |= U.getTag() == DW_TAG_const_type; 644 Volatile |= U.getTag() == DW_TAG_volatile_type; 645 return U; 646 } 647 return DWARFDie(); 648 }; 649 if (DWARFDie CV = CVStep(P)) { 650 CVStep(CV); 651 } 652 } 653 } 654 } 655 656 if (auto CC = D.find(DW_AT_calling_convention)) { 657 switch (*CC->getAsUnsignedConstant()) { 658 case CallingConvention::DW_CC_BORLAND_stdcall: 659 OS << " __attribute__((stdcall))"; 660 break; 661 case CallingConvention::DW_CC_BORLAND_msfastcall: 662 OS << " __attribute__((fastcall))"; 663 break; 664 case CallingConvention::DW_CC_BORLAND_thiscall: 665 OS << " __attribute__((thiscall))"; 666 break; 667 case CallingConvention::DW_CC_LLVM_vectorcall: 668 OS << " __attribute__((vectorcall))"; 669 break; 670 case CallingConvention::DW_CC_BORLAND_pascal: 671 OS << " __attribute__((pascal))"; 672 break; 673 case CallingConvention::DW_CC_LLVM_Win64: 674 OS << " __attribute__((ms_abi))"; 675 break; 676 case CallingConvention::DW_CC_LLVM_X86_64SysV: 677 OS << " __attribute__((sysv_abi))"; 678 break; 679 case CallingConvention::DW_CC_LLVM_AAPCS: 680 // AArch64VectorCall missing? 681 OS << " __attribute__((pcs(\"aapcs\")))"; 682 break; 683 case CallingConvention::DW_CC_LLVM_AAPCS_VFP: 684 OS << " __attribute__((pcs(\"aapcs-vfp\")))"; 685 break; 686 case CallingConvention::DW_CC_LLVM_IntelOclBicc: 687 OS << " __attribute__((intel_ocl_bicc))"; 688 break; 689 case CallingConvention::DW_CC_LLVM_SpirFunction: 690 case CallingConvention::DW_CC_LLVM_OpenCLKernel: 691 // These aren't available as attributes, but maybe we should still 692 // render them somehow? (Clang doesn't render them, but that's an issue 693 // for template names too - since then the DWARF names of templates 694 // instantiated with function types with these calling conventions won't 695 // have distinct names - so we'd need to fix that too) 696 break; 697 case CallingConvention::DW_CC_LLVM_Swift: 698 // SwiftAsync missing 699 OS << " __attribute__((swiftcall))"; 700 break; 701 case CallingConvention::DW_CC_LLVM_PreserveMost: 702 OS << " __attribute__((preserve_most))"; 703 break; 704 case CallingConvention::DW_CC_LLVM_PreserveAll: 705 OS << " __attribute__((preserve_all))"; 706 break; 707 case CallingConvention::DW_CC_LLVM_X86RegCall: 708 OS << " __attribute__((regcall))"; 709 break; 710 } 711 } 712 713 if (Const) 714 OS << " const"; 715 if (Volatile) 716 OS << " volatile"; 717 if (D.find(DW_AT_reference)) 718 OS << " &"; 719 if (D.find(DW_AT_rvalue_reference)) 720 OS << " &&"; 721 722 appendUnqualifiedNameAfter(Inner, resolveReferencedType(Inner)); 723 } 724 void appendScopes(DWARFDie D) { 725 if (D.getTag() == DW_TAG_compile_unit) 726 return; 727 if (D.getTag() == DW_TAG_type_unit) 728 return; 729 if (D.getTag() == DW_TAG_skeleton_unit) 730 return; 731 if (D.getTag() == DW_TAG_subprogram) 732 return; 733 if (D.getTag() == DW_TAG_lexical_block) 734 return; 735 D = D.resolveTypeUnitReference(); 736 if (DWARFDie P = D.getParent()) 737 appendScopes(P); 738 appendUnqualifiedName(D); 739 OS << "::"; 740 } 741 }; 742 } // anonymous namespace 743 744 static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die, 745 const DWARFAttribute &AttrValue, unsigned Indent, 746 DIDumpOptions DumpOpts) { 747 if (!Die.isValid()) 748 return; 749 const char BaseIndent[] = " "; 750 OS << BaseIndent; 751 OS.indent(Indent + 2); 752 dwarf::Attribute Attr = AttrValue.Attr; 753 WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr); 754 755 dwarf::Form Form = AttrValue.Value.getForm(); 756 if (DumpOpts.Verbose || DumpOpts.ShowForm) 757 OS << formatv(" [{0}]", Form); 758 759 DWARFUnit *U = Die.getDwarfUnit(); 760 const DWARFFormValue &FormValue = AttrValue.Value; 761 762 OS << "\t("; 763 764 StringRef Name; 765 std::string File; 766 auto Color = HighlightColor::Enumerator; 767 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) { 768 Color = HighlightColor::String; 769 if (const auto *LT = U->getContext().getLineTableForUnit(U)) 770 if (LT->getFileNameByIndex( 771 FormValue.getAsUnsignedConstant().getValue(), 772 U->getCompilationDir(), 773 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) { 774 File = '"' + File + '"'; 775 Name = File; 776 } 777 } else if (Optional<uint64_t> Val = FormValue.getAsUnsignedConstant()) 778 Name = AttributeValueString(Attr, *Val); 779 780 if (!Name.empty()) 781 WithColor(OS, Color) << Name; 782 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line) 783 OS << *FormValue.getAsUnsignedConstant(); 784 else if (Attr == DW_AT_low_pc && 785 (FormValue.getAsAddress() == 786 dwarf::computeTombstoneAddress(U->getAddressByteSize()))) { 787 if (DumpOpts.Verbose) { 788 FormValue.dump(OS, DumpOpts); 789 OS << " ("; 790 } 791 OS << "dead code"; 792 if (DumpOpts.Verbose) 793 OS << ')'; 794 } else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose && 795 FormValue.getAsUnsignedConstant()) { 796 if (DumpOpts.ShowAddresses) { 797 // Print the actual address rather than the offset. 798 uint64_t LowPC, HighPC, Index; 799 if (Die.getLowAndHighPC(LowPC, HighPC, Index)) 800 DWARFFormValue::dumpAddress(OS, U->getAddressByteSize(), HighPC); 801 else 802 FormValue.dump(OS, DumpOpts); 803 } 804 } else if (DWARFAttribute::mayHaveLocationList(Attr) && 805 FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) 806 dumpLocationList(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4, 807 DumpOpts); 808 else if (FormValue.isFormClass(DWARFFormValue::FC_Exprloc) || 809 (DWARFAttribute::mayHaveLocationExpr(Attr) && 810 FormValue.isFormClass(DWARFFormValue::FC_Block))) 811 dumpLocationExpr(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4, 812 DumpOpts); 813 else 814 FormValue.dump(OS, DumpOpts); 815 816 std::string Space = DumpOpts.ShowAddresses ? " " : ""; 817 818 // We have dumped the attribute raw value. For some attributes 819 // having both the raw value and the pretty-printed value is 820 // interesting. These attributes are handled below. 821 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) { 822 if (const char *Name = 823 Die.getAttributeValueAsReferencedDie(FormValue).getName( 824 DINameKind::LinkageName)) 825 OS << Space << "\"" << Name << '\"'; 826 } else if (Attr == DW_AT_type) { 827 DWARFDie D = resolveReferencedType(Die, FormValue); 828 if (D && !D.isNULL()) { 829 OS << Space << "\""; 830 dumpTypeQualifiedName(D, OS); 831 OS << '"'; 832 } 833 } else if (Attr == DW_AT_APPLE_property_attribute) { 834 if (Optional<uint64_t> OptVal = FormValue.getAsUnsignedConstant()) 835 dumpApplePropertyAttribute(OS, *OptVal); 836 } else if (Attr == DW_AT_ranges) { 837 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj(); 838 // For DW_FORM_rnglistx we need to dump the offset separately, since 839 // we have only dumped the index so far. 840 if (FormValue.getForm() == DW_FORM_rnglistx) 841 if (auto RangeListOffset = 842 U->getRnglistOffset(*FormValue.getAsSectionOffset())) { 843 DWARFFormValue FV = DWARFFormValue::createFromUValue( 844 dwarf::DW_FORM_sec_offset, *RangeListOffset); 845 FV.dump(OS, DumpOpts); 846 } 847 if (auto RangesOrError = Die.getAddressRanges()) 848 dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(), 849 sizeof(BaseIndent) + Indent + 4, DumpOpts); 850 else 851 DumpOpts.RecoverableErrorHandler(createStringError( 852 errc::invalid_argument, "decoding address ranges: %s", 853 toString(RangesOrError.takeError()).c_str())); 854 } 855 856 OS << ")\n"; 857 } 858 859 void DWARFDie::getFullName(raw_string_ostream &OS, 860 std::string *OriginalFullName) const { 861 const char *NamePtr = getShortName(); 862 if (!NamePtr) 863 return; 864 if (getTag() == DW_TAG_GNU_template_parameter_pack) 865 return; 866 dumpTypeUnqualifiedName(*this, OS, OriginalFullName); 867 } 868 869 bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; } 870 871 bool DWARFDie::isSubroutineDIE() const { 872 auto Tag = getTag(); 873 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine; 874 } 875 876 Optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const { 877 if (!isValid()) 878 return None; 879 auto AbbrevDecl = getAbbreviationDeclarationPtr(); 880 if (AbbrevDecl) 881 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U); 882 return None; 883 } 884 885 Optional<DWARFFormValue> 886 DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const { 887 if (!isValid()) 888 return None; 889 auto AbbrevDecl = getAbbreviationDeclarationPtr(); 890 if (AbbrevDecl) { 891 for (auto Attr : Attrs) { 892 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U)) 893 return Value; 894 } 895 } 896 return None; 897 } 898 899 Optional<DWARFFormValue> 900 DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const { 901 SmallVector<DWARFDie, 3> Worklist; 902 Worklist.push_back(*this); 903 904 // Keep track if DIEs already seen to prevent infinite recursion. 905 // Empirically we rarely see a depth of more than 3 when dealing with valid 906 // DWARF. This corresponds to following the DW_AT_abstract_origin and 907 // DW_AT_specification just once. 908 SmallSet<DWARFDie, 3> Seen; 909 Seen.insert(*this); 910 911 while (!Worklist.empty()) { 912 DWARFDie Die = Worklist.pop_back_val(); 913 914 if (!Die.isValid()) 915 continue; 916 917 if (auto Value = Die.find(Attrs)) 918 return Value; 919 920 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin)) 921 if (Seen.insert(D).second) 922 Worklist.push_back(D); 923 924 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification)) 925 if (Seen.insert(D).second) 926 Worklist.push_back(D); 927 } 928 929 return None; 930 } 931 932 DWARFDie 933 DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const { 934 if (Optional<DWARFFormValue> F = find(Attr)) 935 return getAttributeValueAsReferencedDie(*F); 936 return DWARFDie(); 937 } 938 939 DWARFDie 940 DWARFDie::getAttributeValueAsReferencedDie(const DWARFFormValue &V) const { 941 DWARFDie Result; 942 if (auto SpecRef = V.getAsRelativeReference()) { 943 if (SpecRef->Unit) 944 Result = SpecRef->Unit->getDIEForOffset(SpecRef->Unit->getOffset() + 945 SpecRef->Offset); 946 else if (auto SpecUnit = 947 U->getUnitVector().getUnitForOffset(SpecRef->Offset)) 948 Result = SpecUnit->getDIEForOffset(SpecRef->Offset); 949 } 950 return Result; 951 } 952 953 DWARFDie DWARFDie::resolveTypeUnitReference() const { 954 if (auto Attr = find(DW_AT_signature)) { 955 if (Optional<uint64_t> Sig = Attr->getAsReferenceUVal()) { 956 if (DWARFTypeUnit *TU = U->getContext().getTypeUnitForHash( 957 U->getVersion(), *Sig, U->isDWOUnit())) 958 return TU->getDIEForOffset(TU->getTypeOffset() + TU->getOffset()); 959 } 960 } 961 return *this; 962 } 963 964 Optional<uint64_t> DWARFDie::getRangesBaseAttribute() const { 965 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base})); 966 } 967 968 Optional<uint64_t> DWARFDie::getLocBaseAttribute() const { 969 return toSectionOffset(find(DW_AT_loclists_base)); 970 } 971 972 Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const { 973 uint64_t Tombstone = dwarf::computeTombstoneAddress(U->getAddressByteSize()); 974 if (LowPC == Tombstone) 975 return None; 976 if (auto FormValue = find(DW_AT_high_pc)) { 977 if (auto Address = FormValue->getAsAddress()) { 978 // High PC is an address. 979 return Address; 980 } 981 if (auto Offset = FormValue->getAsUnsignedConstant()) { 982 // High PC is an offset from LowPC. 983 return LowPC + *Offset; 984 } 985 } 986 return None; 987 } 988 989 bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC, 990 uint64_t &SectionIndex) const { 991 auto F = find(DW_AT_low_pc); 992 auto LowPcAddr = toSectionedAddress(F); 993 if (!LowPcAddr) 994 return false; 995 if (auto HighPcAddr = getHighPC(LowPcAddr->Address)) { 996 LowPC = LowPcAddr->Address; 997 HighPC = *HighPcAddr; 998 SectionIndex = LowPcAddr->SectionIndex; 999 return true; 1000 } 1001 return false; 1002 } 1003 1004 Expected<DWARFAddressRangesVector> DWARFDie::getAddressRanges() const { 1005 if (isNULL()) 1006 return DWARFAddressRangesVector(); 1007 // Single range specified by low/high PC. 1008 uint64_t LowPC, HighPC, Index; 1009 if (getLowAndHighPC(LowPC, HighPC, Index)) 1010 return DWARFAddressRangesVector{{LowPC, HighPC, Index}}; 1011 1012 Optional<DWARFFormValue> Value = find(DW_AT_ranges); 1013 if (Value) { 1014 if (Value->getForm() == DW_FORM_rnglistx) 1015 return U->findRnglistFromIndex(*Value->getAsSectionOffset()); 1016 return U->findRnglistFromOffset(*Value->getAsSectionOffset()); 1017 } 1018 return DWARFAddressRangesVector(); 1019 } 1020 1021 bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const { 1022 auto RangesOrError = getAddressRanges(); 1023 if (!RangesOrError) { 1024 llvm::consumeError(RangesOrError.takeError()); 1025 return false; 1026 } 1027 1028 for (const auto &R : RangesOrError.get()) 1029 if (R.LowPC <= Address && Address < R.HighPC) 1030 return true; 1031 return false; 1032 } 1033 1034 Expected<DWARFLocationExpressionsVector> 1035 DWARFDie::getLocations(dwarf::Attribute Attr) const { 1036 Optional<DWARFFormValue> Location = find(Attr); 1037 if (!Location) 1038 return createStringError(inconvertibleErrorCode(), "No %s", 1039 dwarf::AttributeString(Attr).data()); 1040 1041 if (Optional<uint64_t> Off = Location->getAsSectionOffset()) { 1042 uint64_t Offset = *Off; 1043 1044 if (Location->getForm() == DW_FORM_loclistx) { 1045 if (auto LoclistOffset = U->getLoclistOffset(Offset)) 1046 Offset = *LoclistOffset; 1047 else 1048 return createStringError(inconvertibleErrorCode(), 1049 "Loclist table not found"); 1050 } 1051 return U->findLoclistFromOffset(Offset); 1052 } 1053 1054 if (Optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) { 1055 return DWARFLocationExpressionsVector{ 1056 DWARFLocationExpression{None, to_vector<4>(*Expr)}}; 1057 } 1058 1059 return createStringError( 1060 inconvertibleErrorCode(), "Unsupported %s encoding: %s", 1061 dwarf::AttributeString(Attr).data(), 1062 dwarf::FormEncodingString(Location->getForm()).data()); 1063 } 1064 1065 const char *DWARFDie::getSubroutineName(DINameKind Kind) const { 1066 if (!isSubroutineDIE()) 1067 return nullptr; 1068 return getName(Kind); 1069 } 1070 1071 const char *DWARFDie::getName(DINameKind Kind) const { 1072 if (!isValid() || Kind == DINameKind::None) 1073 return nullptr; 1074 // Try to get mangled name only if it was asked for. 1075 if (Kind == DINameKind::LinkageName) { 1076 if (auto Name = getLinkageName()) 1077 return Name; 1078 } 1079 return getShortName(); 1080 } 1081 1082 const char *DWARFDie::getShortName() const { 1083 if (!isValid()) 1084 return nullptr; 1085 1086 return dwarf::toString(findRecursively(dwarf::DW_AT_name), nullptr); 1087 } 1088 1089 const char *DWARFDie::getLinkageName() const { 1090 if (!isValid()) 1091 return nullptr; 1092 1093 return dwarf::toString(findRecursively({dwarf::DW_AT_MIPS_linkage_name, 1094 dwarf::DW_AT_linkage_name}), 1095 nullptr); 1096 } 1097 1098 uint64_t DWARFDie::getDeclLine() const { 1099 return toUnsigned(findRecursively(DW_AT_decl_line), 0); 1100 } 1101 1102 std::string 1103 DWARFDie::getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const { 1104 if (auto FormValue = findRecursively(DW_AT_decl_file)) 1105 if (auto OptString = FormValue->getAsFile(Kind)) 1106 return *OptString; 1107 return {}; 1108 } 1109 1110 void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine, 1111 uint32_t &CallColumn, 1112 uint32_t &CallDiscriminator) const { 1113 CallFile = toUnsigned(find(DW_AT_call_file), 0); 1114 CallLine = toUnsigned(find(DW_AT_call_line), 0); 1115 CallColumn = toUnsigned(find(DW_AT_call_column), 0); 1116 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0); 1117 } 1118 1119 /// Helper to dump a DIE with all of its parents, but no siblings. 1120 static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent, 1121 DIDumpOptions DumpOpts, unsigned Depth = 0) { 1122 if (!Die) 1123 return Indent; 1124 if (DumpOpts.ParentRecurseDepth > 0 && Depth >= DumpOpts.ParentRecurseDepth) 1125 return Indent; 1126 Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts, Depth + 1); 1127 Die.dump(OS, Indent, DumpOpts); 1128 return Indent + 2; 1129 } 1130 1131 void DWARFDie::dump(raw_ostream &OS, unsigned Indent, 1132 DIDumpOptions DumpOpts) const { 1133 if (!isValid()) 1134 return; 1135 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor(); 1136 const uint64_t Offset = getOffset(); 1137 uint64_t offset = Offset; 1138 if (DumpOpts.ShowParents) { 1139 DIDumpOptions ParentDumpOpts = DumpOpts; 1140 ParentDumpOpts.ShowParents = false; 1141 ParentDumpOpts.ShowChildren = false; 1142 Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts); 1143 } 1144 1145 if (debug_info_data.isValidOffset(offset)) { 1146 uint32_t abbrCode = debug_info_data.getULEB128(&offset); 1147 if (DumpOpts.ShowAddresses) 1148 WithColor(OS, HighlightColor::Address).get() 1149 << format("\n0x%8.8" PRIx64 ": ", Offset); 1150 1151 if (abbrCode) { 1152 auto AbbrevDecl = getAbbreviationDeclarationPtr(); 1153 if (AbbrevDecl) { 1154 WithColor(OS, HighlightColor::Tag).get().indent(Indent) 1155 << formatv("{0}", getTag()); 1156 if (DumpOpts.Verbose) { 1157 OS << format(" [%u] %c", abbrCode, 1158 AbbrevDecl->hasChildren() ? '*' : ' '); 1159 if (Optional<uint32_t> ParentIdx = Die->getParentIdx()) 1160 OS << format(" (0x%8.8" PRIx64 ")", 1161 U->getDIEAtIndex(*ParentIdx).getOffset()); 1162 } 1163 OS << '\n'; 1164 1165 // Dump all data in the DIE for the attributes. 1166 for (const DWARFAttribute &AttrValue : attributes()) 1167 dumpAttribute(OS, *this, AttrValue, Indent, DumpOpts); 1168 1169 if (DumpOpts.ShowChildren && DumpOpts.ChildRecurseDepth > 0) { 1170 DWARFDie Child = getFirstChild(); 1171 DumpOpts.ChildRecurseDepth--; 1172 DIDumpOptions ChildDumpOpts = DumpOpts; 1173 ChildDumpOpts.ShowParents = false; 1174 while (Child) { 1175 Child.dump(OS, Indent + 2, ChildDumpOpts); 1176 Child = Child.getSibling(); 1177 } 1178 } 1179 } else { 1180 OS << "Abbreviation code not found in 'debug_abbrev' class for code: " 1181 << abbrCode << '\n'; 1182 } 1183 } else { 1184 OS.indent(Indent) << "NULL\n"; 1185 } 1186 } 1187 } 1188 1189 LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); } 1190 1191 DWARFDie DWARFDie::getParent() const { 1192 if (isValid()) 1193 return U->getParent(Die); 1194 return DWARFDie(); 1195 } 1196 1197 DWARFDie DWARFDie::getSibling() const { 1198 if (isValid()) 1199 return U->getSibling(Die); 1200 return DWARFDie(); 1201 } 1202 1203 DWARFDie DWARFDie::getPreviousSibling() const { 1204 if (isValid()) 1205 return U->getPreviousSibling(Die); 1206 return DWARFDie(); 1207 } 1208 1209 DWARFDie DWARFDie::getFirstChild() const { 1210 if (isValid()) 1211 return U->getFirstChild(Die); 1212 return DWARFDie(); 1213 } 1214 1215 DWARFDie DWARFDie::getLastChild() const { 1216 if (isValid()) 1217 return U->getLastChild(Die); 1218 return DWARFDie(); 1219 } 1220 1221 iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const { 1222 return make_range(attribute_iterator(*this, false), 1223 attribute_iterator(*this, true)); 1224 } 1225 1226 DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End) 1227 : Die(D), Index(0) { 1228 auto AbbrDecl = Die.getAbbreviationDeclarationPtr(); 1229 assert(AbbrDecl && "Must have abbreviation declaration"); 1230 if (End) { 1231 // This is the end iterator so we set the index to the attribute count. 1232 Index = AbbrDecl->getNumAttributes(); 1233 } else { 1234 // This is the begin iterator so we extract the value for this->Index. 1235 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize(); 1236 updateForIndex(*AbbrDecl, 0); 1237 } 1238 } 1239 1240 void DWARFDie::attribute_iterator::updateForIndex( 1241 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) { 1242 Index = I; 1243 // AbbrDecl must be valid before calling this function. 1244 auto NumAttrs = AbbrDecl.getNumAttributes(); 1245 if (Index < NumAttrs) { 1246 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index); 1247 // Add the previous byte size of any previous attribute value. 1248 AttrValue.Offset += AttrValue.ByteSize; 1249 uint64_t ParseOffset = AttrValue.Offset; 1250 if (AbbrDecl.getAttrIsImplicitConstByIndex(Index)) 1251 AttrValue.Value = DWARFFormValue::createFromSValue( 1252 AbbrDecl.getFormByIndex(Index), 1253 AbbrDecl.getAttrImplicitConstValueByIndex(Index)); 1254 else { 1255 auto U = Die.getDwarfUnit(); 1256 assert(U && "Die must have valid DWARF unit"); 1257 AttrValue.Value = DWARFFormValue::createFromUnit( 1258 AbbrDecl.getFormByIndex(Index), U, &ParseOffset); 1259 } 1260 AttrValue.ByteSize = ParseOffset - AttrValue.Offset; 1261 } else { 1262 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only"); 1263 AttrValue = {}; 1264 } 1265 } 1266 1267 DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() { 1268 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr()) 1269 updateForIndex(*AbbrDecl, Index + 1); 1270 return *this; 1271 } 1272 1273 bool DWARFAttribute::mayHaveLocationList(dwarf::Attribute Attr) { 1274 switch(Attr) { 1275 case DW_AT_location: 1276 case DW_AT_string_length: 1277 case DW_AT_return_addr: 1278 case DW_AT_data_member_location: 1279 case DW_AT_frame_base: 1280 case DW_AT_static_link: 1281 case DW_AT_segment: 1282 case DW_AT_use_location: 1283 case DW_AT_vtable_elem_location: 1284 return true; 1285 default: 1286 return false; 1287 } 1288 } 1289 1290 bool DWARFAttribute::mayHaveLocationExpr(dwarf::Attribute Attr) { 1291 switch (Attr) { 1292 // From the DWARF v5 specification. 1293 case DW_AT_location: 1294 case DW_AT_byte_size: 1295 case DW_AT_bit_offset: 1296 case DW_AT_bit_size: 1297 case DW_AT_string_length: 1298 case DW_AT_lower_bound: 1299 case DW_AT_return_addr: 1300 case DW_AT_bit_stride: 1301 case DW_AT_upper_bound: 1302 case DW_AT_count: 1303 case DW_AT_data_member_location: 1304 case DW_AT_frame_base: 1305 case DW_AT_segment: 1306 case DW_AT_static_link: 1307 case DW_AT_use_location: 1308 case DW_AT_vtable_elem_location: 1309 case DW_AT_allocated: 1310 case DW_AT_associated: 1311 case DW_AT_data_location: 1312 case DW_AT_byte_stride: 1313 case DW_AT_rank: 1314 case DW_AT_call_value: 1315 case DW_AT_call_origin: 1316 case DW_AT_call_target: 1317 case DW_AT_call_target_clobbered: 1318 case DW_AT_call_data_location: 1319 case DW_AT_call_data_value: 1320 // Extensions. 1321 case DW_AT_GNU_call_site_value: 1322 case DW_AT_GNU_call_site_target: 1323 return true; 1324 default: 1325 return false; 1326 } 1327 } 1328 1329 namespace llvm { 1330 1331 void dumpTypeQualifiedName(const DWARFDie &DIE, raw_ostream &OS) { 1332 DWARFTypePrinter(OS).appendQualifiedName(DIE); 1333 } 1334 1335 void dumpTypeUnqualifiedName(const DWARFDie &DIE, raw_ostream &OS, 1336 std::string *OriginalFullName) { 1337 DWARFTypePrinter(OS).appendUnqualifiedName(DIE, OriginalFullName); 1338 } 1339 1340 } // namespace llvm 1341