1 //===-- DWARFDebugInfoEntry.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 "DWARFDebugInfoEntry.h" 10 11 #include <cassert> 12 13 #include <algorithm> 14 15 #include "llvm/Support/LEB128.h" 16 17 #include "lldb/Core/Module.h" 18 #include "lldb/Expression/DWARFExpression.h" 19 #include "lldb/Symbol/ObjectFile.h" 20 #include "lldb/Utility/Stream.h" 21 22 #include "DWARFCompileUnit.h" 23 #include "DWARFDebugAbbrev.h" 24 #include "DWARFDebugAranges.h" 25 #include "DWARFDebugInfo.h" 26 #include "DWARFDebugRanges.h" 27 #include "DWARFDeclContext.h" 28 #include "DWARFFormValue.h" 29 #include "DWARFUnit.h" 30 #include "SymbolFileDWARF.h" 31 #include "SymbolFileDWARFDwo.h" 32 33 using namespace lldb_private; 34 using namespace lldb_private::dwarf; 35 extern int g_verbose; 36 37 // Extract a debug info entry for a given DWARFUnit from the data 38 // starting at the offset in offset_ptr 39 bool DWARFDebugInfoEntry::Extract(const DWARFDataExtractor &data, 40 const DWARFUnit *cu, 41 lldb::offset_t *offset_ptr) { 42 m_offset = *offset_ptr; 43 m_parent_idx = 0; 44 m_sibling_idx = 0; 45 const uint64_t abbr_idx = data.GetULEB128(offset_ptr); 46 lldbassert(abbr_idx <= UINT16_MAX); 47 m_abbr_idx = abbr_idx; 48 49 // assert (fixed_form_sizes); // For best performance this should be 50 // specified! 51 52 if (m_abbr_idx == 0) { 53 m_tag = llvm::dwarf::DW_TAG_null; 54 m_has_children = false; 55 return true; // NULL debug tag entry 56 } 57 58 lldb::offset_t offset = *offset_ptr; 59 const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu); 60 if (abbrevDecl == nullptr) { 61 cu->GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError( 62 "{0x%8.8x}: invalid abbreviation code %u, please file a bug and " 63 "attach the file at the start of this error message", 64 m_offset, (unsigned)abbr_idx); 65 // WE can't parse anymore if the DWARF is borked... 66 *offset_ptr = UINT32_MAX; 67 return false; 68 } 69 m_tag = abbrevDecl->Tag(); 70 m_has_children = abbrevDecl->HasChildren(); 71 // Skip all data in the .debug_info or .debug_types for the attributes 72 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 73 uint32_t i; 74 dw_form_t form; 75 for (i = 0; i < numAttributes; ++i) { 76 form = abbrevDecl->GetFormByIndexUnchecked(i); 77 llvm::Optional<uint8_t> fixed_skip_size = 78 DWARFFormValue::GetFixedSize(form, cu); 79 if (fixed_skip_size) 80 offset += *fixed_skip_size; 81 else { 82 bool form_is_indirect = false; 83 do { 84 form_is_indirect = false; 85 uint32_t form_size = 0; 86 switch (form) { 87 // Blocks if inlined data that have a length field and the data bytes 88 // inlined in the .debug_info/.debug_types 89 case DW_FORM_exprloc: 90 case DW_FORM_block: 91 form_size = data.GetULEB128(&offset); 92 break; 93 case DW_FORM_block1: 94 form_size = data.GetU8_unchecked(&offset); 95 break; 96 case DW_FORM_block2: 97 form_size = data.GetU16_unchecked(&offset); 98 break; 99 case DW_FORM_block4: 100 form_size = data.GetU32_unchecked(&offset); 101 break; 102 103 // Inlined NULL terminated C-strings 104 case DW_FORM_string: 105 data.GetCStr(&offset); 106 break; 107 108 // Compile unit address sized values 109 case DW_FORM_addr: 110 form_size = cu->GetAddressByteSize(); 111 break; 112 case DW_FORM_ref_addr: 113 if (cu->GetVersion() <= 2) 114 form_size = cu->GetAddressByteSize(); 115 else 116 form_size = 4; 117 break; 118 119 // 0 sized form 120 case DW_FORM_flag_present: 121 form_size = 0; 122 break; 123 124 // 1 byte values 125 case DW_FORM_addrx1: 126 case DW_FORM_data1: 127 case DW_FORM_flag: 128 case DW_FORM_ref1: 129 case DW_FORM_strx1: 130 form_size = 1; 131 break; 132 133 // 2 byte values 134 case DW_FORM_addrx2: 135 case DW_FORM_data2: 136 case DW_FORM_ref2: 137 case DW_FORM_strx2: 138 form_size = 2; 139 break; 140 141 // 3 byte values 142 case DW_FORM_addrx3: 143 case DW_FORM_strx3: 144 form_size = 3; 145 break; 146 147 // 4 byte values 148 case DW_FORM_addrx4: 149 case DW_FORM_data4: 150 case DW_FORM_ref4: 151 case DW_FORM_strx4: 152 form_size = 4; 153 break; 154 155 // 8 byte values 156 case DW_FORM_data8: 157 case DW_FORM_ref8: 158 case DW_FORM_ref_sig8: 159 form_size = 8; 160 break; 161 162 // signed or unsigned LEB 128 values 163 case DW_FORM_addrx: 164 case DW_FORM_loclistx: 165 case DW_FORM_rnglistx: 166 case DW_FORM_sdata: 167 case DW_FORM_udata: 168 case DW_FORM_ref_udata: 169 case DW_FORM_GNU_addr_index: 170 case DW_FORM_GNU_str_index: 171 case DW_FORM_strx: 172 data.Skip_LEB128(&offset); 173 break; 174 175 case DW_FORM_indirect: 176 form_is_indirect = true; 177 form = data.GetULEB128(&offset); 178 break; 179 180 case DW_FORM_strp: 181 case DW_FORM_line_strp: 182 case DW_FORM_sec_offset: 183 data.GetU32(&offset); 184 break; 185 186 case DW_FORM_implicit_const: 187 form_size = 0; 188 break; 189 190 default: 191 cu->GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError( 192 "{0x%8.8x}: Unsupported DW_FORM_0x%x, please file a bug and " 193 "attach the file at the start of this error message", 194 m_offset, (unsigned)form); 195 *offset_ptr = m_offset; 196 return false; 197 } 198 offset += form_size; 199 200 } while (form_is_indirect); 201 } 202 } 203 *offset_ptr = offset; 204 return true; 205 } 206 207 static DWARFRangeList GetRangesOrReportError(DWARFUnit &unit, 208 const DWARFDebugInfoEntry &die, 209 const DWARFFormValue &value) { 210 llvm::Expected<DWARFRangeList> expected_ranges = 211 (value.Form() == DW_FORM_rnglistx) 212 ? unit.FindRnglistFromIndex(value.Unsigned()) 213 : unit.FindRnglistFromOffset(value.Unsigned()); 214 if (expected_ranges) 215 return std::move(*expected_ranges); 216 unit.GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError( 217 "{0x%8.8x}: DIE has DW_AT_ranges(%s 0x%" PRIx64 ") attribute, but " 218 "range extraction failed (%s), please file a bug " 219 "and attach the file at the start of this error message", 220 die.GetOffset(), 221 llvm::dwarf::FormEncodingString(value.Form()).str().c_str(), 222 value.Unsigned(), toString(expected_ranges.takeError()).c_str()); 223 return DWARFRangeList(); 224 } 225 226 // GetDIENamesAndRanges 227 // 228 // Gets the valid address ranges for a given DIE by looking for a 229 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges attributes. 230 bool DWARFDebugInfoEntry::GetDIENamesAndRanges( 231 DWARFUnit *cu, const char *&name, const char *&mangled, 232 DWARFRangeList &ranges, int &decl_file, int &decl_line, int &decl_column, 233 int &call_file, int &call_line, int &call_column, 234 DWARFExpressionList *frame_base) const { 235 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 236 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 237 std::vector<DWARFDIE> dies; 238 239 const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu); 240 241 SymbolFileDWARF &dwarf = cu->GetSymbolFileDWARF(); 242 lldb::ModuleSP module = dwarf.GetObjectFile()->GetModule(); 243 244 if (abbrevDecl) { 245 const DWARFDataExtractor &data = cu->GetData(); 246 lldb::offset_t offset = GetFirstAttributeOffset(); 247 248 if (!data.ValidOffset(offset)) 249 return false; 250 251 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 252 bool do_offset = false; 253 254 for (uint32_t i = 0; i < numAttributes; ++i) { 255 DWARFFormValue form_value(cu); 256 dw_attr_t attr; 257 abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value); 258 259 if (form_value.ExtractValue(data, &offset)) { 260 switch (attr) { 261 case DW_AT_low_pc: 262 lo_pc = form_value.Address(); 263 264 if (do_offset) 265 hi_pc += lo_pc; 266 do_offset = false; 267 break; 268 269 case DW_AT_entry_pc: 270 lo_pc = form_value.Address(); 271 break; 272 273 case DW_AT_high_pc: 274 if (form_value.Form() == DW_FORM_addr || 275 form_value.Form() == DW_FORM_addrx || 276 form_value.Form() == DW_FORM_GNU_addr_index) { 277 hi_pc = form_value.Address(); 278 } else { 279 hi_pc = form_value.Unsigned(); 280 if (lo_pc == LLDB_INVALID_ADDRESS) 281 do_offset = hi_pc != LLDB_INVALID_ADDRESS; 282 else 283 hi_pc += lo_pc; // DWARF 4 introduces <offset-from-lo-pc> to save 284 // on relocations 285 } 286 break; 287 288 case DW_AT_ranges: 289 ranges = GetRangesOrReportError(*cu, *this, form_value); 290 break; 291 292 case DW_AT_name: 293 if (name == nullptr) 294 name = form_value.AsCString(); 295 break; 296 297 case DW_AT_MIPS_linkage_name: 298 case DW_AT_linkage_name: 299 if (mangled == nullptr) 300 mangled = form_value.AsCString(); 301 break; 302 303 case DW_AT_abstract_origin: 304 dies.push_back(form_value.Reference()); 305 break; 306 307 case DW_AT_specification: 308 dies.push_back(form_value.Reference()); 309 break; 310 311 case DW_AT_decl_file: 312 if (decl_file == 0) 313 decl_file = form_value.Unsigned(); 314 break; 315 316 case DW_AT_decl_line: 317 if (decl_line == 0) 318 decl_line = form_value.Unsigned(); 319 break; 320 321 case DW_AT_decl_column: 322 if (decl_column == 0) 323 decl_column = form_value.Unsigned(); 324 break; 325 326 case DW_AT_call_file: 327 if (call_file == 0) 328 call_file = form_value.Unsigned(); 329 break; 330 331 case DW_AT_call_line: 332 if (call_line == 0) 333 call_line = form_value.Unsigned(); 334 break; 335 336 case DW_AT_call_column: 337 if (call_column == 0) 338 call_column = form_value.Unsigned(); 339 break; 340 341 case DW_AT_frame_base: 342 if (frame_base) { 343 if (form_value.BlockData()) { 344 uint32_t block_offset = 345 form_value.BlockData() - data.GetDataStart(); 346 uint32_t block_length = form_value.Unsigned(); 347 *frame_base = 348 DWARFExpressionList(module, 349 DWARFExpression(DataExtractor( 350 data, block_offset, block_length)), 351 cu); 352 } else { 353 DataExtractor data = cu->GetLocationData(); 354 const dw_offset_t offset = form_value.Unsigned(); 355 if (data.ValidOffset(offset)) { 356 data = DataExtractor(data, offset, data.GetByteSize() - offset); 357 DWARFExpression::ParseDWARFLocationList(cu, data, frame_base); 358 } 359 } 360 } 361 break; 362 363 default: 364 break; 365 } 366 } 367 } 368 } 369 370 if (ranges.IsEmpty()) { 371 if (lo_pc != LLDB_INVALID_ADDRESS) { 372 if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc) 373 ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc)); 374 else 375 ranges.Append(DWARFRangeList::Entry(lo_pc, 0)); 376 } 377 } 378 379 if (ranges.IsEmpty() || name == nullptr || mangled == nullptr) { 380 for (const DWARFDIE &die : dies) { 381 if (die) { 382 die.GetDIE()->GetDIENamesAndRanges(die.GetCU(), name, mangled, ranges, 383 decl_file, decl_line, decl_column, 384 call_file, call_line, call_column); 385 } 386 } 387 } 388 return !ranges.IsEmpty(); 389 } 390 391 // Get all attribute values for a given DIE, including following any 392 // specification or abstract origin attributes and including those in the 393 // results. Any duplicate attributes will have the first instance take 394 // precedence (this can happen for declaration attributes). 395 size_t DWARFDebugInfoEntry::GetAttributes(DWARFUnit *cu, 396 DWARFAttributes &attributes, 397 Recurse recurse, 398 uint32_t curr_depth) const { 399 const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu); 400 if (abbrevDecl) { 401 const DWARFDataExtractor &data = cu->GetData(); 402 lldb::offset_t offset = GetFirstAttributeOffset(); 403 404 const uint32_t num_attributes = abbrevDecl->NumAttributes(); 405 for (uint32_t i = 0; i < num_attributes; ++i) { 406 DWARFFormValue form_value(cu); 407 dw_attr_t attr; 408 abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value); 409 const dw_form_t form = form_value.Form(); 410 411 // If we are tracking down DW_AT_specification or DW_AT_abstract_origin 412 // attributes, the depth will be non-zero. We need to omit certain 413 // attributes that don't make sense. 414 switch (attr) { 415 case DW_AT_sibling: 416 case DW_AT_declaration: 417 if (curr_depth > 0) { 418 // This attribute doesn't make sense when combined with the DIE that 419 // references this DIE. We know a DIE is referencing this DIE because 420 // curr_depth is not zero 421 break; 422 } 423 LLVM_FALLTHROUGH; 424 default: 425 attributes.Append(form_value, offset, attr); 426 break; 427 } 428 429 if (recurse == Recurse::yes && 430 ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin))) { 431 if (form_value.ExtractValue(data, &offset)) { 432 DWARFDIE spec_die = form_value.Reference(); 433 if (spec_die) 434 spec_die.GetDIE()->GetAttributes(spec_die.GetCU(), attributes, 435 recurse, curr_depth + 1); 436 } 437 } else { 438 llvm::Optional<uint8_t> fixed_skip_size = DWARFFormValue::GetFixedSize(form, cu); 439 if (fixed_skip_size) 440 offset += *fixed_skip_size; 441 else 442 DWARFFormValue::SkipValue(form, data, &offset, cu); 443 } 444 } 445 } else { 446 attributes.Clear(); 447 } 448 return attributes.Size(); 449 } 450 451 // GetAttributeValue 452 // 453 // Get the value of an attribute and return the .debug_info or .debug_types 454 // offset of the attribute if it was properly extracted into form_value, 455 // or zero if we fail since an offset of zero is invalid for an attribute (it 456 // would be a compile unit header). 457 dw_offset_t DWARFDebugInfoEntry::GetAttributeValue( 458 const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &form_value, 459 dw_offset_t *end_attr_offset_ptr, 460 bool check_specification_or_abstract_origin) const { 461 if (const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu)) { 462 uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr); 463 464 if (attr_idx != DW_INVALID_INDEX) { 465 const DWARFDataExtractor &data = cu->GetData(); 466 lldb::offset_t offset = GetFirstAttributeOffset(); 467 468 uint32_t idx = 0; 469 while (idx < attr_idx) 470 DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++), 471 data, &offset, cu); 472 473 const dw_offset_t attr_offset = offset; 474 form_value.SetUnit(cu); 475 form_value.SetForm(abbrevDecl->GetFormByIndex(idx)); 476 if (form_value.ExtractValue(data, &offset)) { 477 if (end_attr_offset_ptr) 478 *end_attr_offset_ptr = offset; 479 return attr_offset; 480 } 481 } 482 } 483 484 if (check_specification_or_abstract_origin) { 485 if (GetAttributeValue(cu, DW_AT_specification, form_value)) { 486 DWARFDIE die = form_value.Reference(); 487 if (die) { 488 dw_offset_t die_offset = die.GetDIE()->GetAttributeValue( 489 die.GetCU(), attr, form_value, end_attr_offset_ptr, false); 490 if (die_offset) 491 return die_offset; 492 } 493 } 494 495 if (GetAttributeValue(cu, DW_AT_abstract_origin, form_value)) { 496 DWARFDIE die = form_value.Reference(); 497 if (die) { 498 dw_offset_t die_offset = die.GetDIE()->GetAttributeValue( 499 die.GetCU(), attr, form_value, end_attr_offset_ptr, false); 500 if (die_offset) 501 return die_offset; 502 } 503 } 504 } 505 return 0; 506 } 507 508 // GetAttributeValueAsString 509 // 510 // Get the value of an attribute as a string return it. The resulting pointer 511 // to the string data exists within the supplied SymbolFileDWARF and will only 512 // be available as long as the SymbolFileDWARF is still around and it's content 513 // doesn't change. 514 const char *DWARFDebugInfoEntry::GetAttributeValueAsString( 515 const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value, 516 bool check_specification_or_abstract_origin) const { 517 DWARFFormValue form_value; 518 if (GetAttributeValue(cu, attr, form_value, nullptr, 519 check_specification_or_abstract_origin)) 520 return form_value.AsCString(); 521 return fail_value; 522 } 523 524 // GetAttributeValueAsUnsigned 525 // 526 // Get the value of an attribute as unsigned and return it. 527 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned( 528 const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, 529 bool check_specification_or_abstract_origin) const { 530 DWARFFormValue form_value; 531 if (GetAttributeValue(cu, attr, form_value, nullptr, 532 check_specification_or_abstract_origin)) 533 return form_value.Unsigned(); 534 return fail_value; 535 } 536 537 // GetAttributeValueAsReference 538 // 539 // Get the value of an attribute as reference and fix up and compile unit 540 // relative offsets as needed. 541 DWARFDIE DWARFDebugInfoEntry::GetAttributeValueAsReference( 542 const DWARFUnit *cu, const dw_attr_t attr, 543 bool check_specification_or_abstract_origin) const { 544 DWARFFormValue form_value; 545 if (GetAttributeValue(cu, attr, form_value, nullptr, 546 check_specification_or_abstract_origin)) 547 return form_value.Reference(); 548 return {}; 549 } 550 551 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress( 552 const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, 553 bool check_specification_or_abstract_origin) const { 554 DWARFFormValue form_value; 555 if (GetAttributeValue(cu, attr, form_value, nullptr, 556 check_specification_or_abstract_origin)) 557 return form_value.Address(); 558 return fail_value; 559 } 560 561 // GetAttributeHighPC 562 // 563 // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low- 564 // pc>. 565 // 566 // Returns the hi_pc or fail_value. 567 dw_addr_t DWARFDebugInfoEntry::GetAttributeHighPC( 568 const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value, 569 bool check_specification_or_abstract_origin) const { 570 DWARFFormValue form_value; 571 if (GetAttributeValue(cu, DW_AT_high_pc, form_value, nullptr, 572 check_specification_or_abstract_origin)) { 573 dw_form_t form = form_value.Form(); 574 if (form == DW_FORM_addr || form == DW_FORM_addrx || 575 form == DW_FORM_GNU_addr_index) 576 return form_value.Address(); 577 578 // DWARF4 can specify the hi_pc as an <offset-from-lowpc> 579 return lo_pc + form_value.Unsigned(); 580 } 581 return fail_value; 582 } 583 584 // GetAttributeAddressRange 585 // 586 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset- 587 // from-low-pc>. 588 // 589 // Returns true or sets lo_pc and hi_pc to fail_value. 590 bool DWARFDebugInfoEntry::GetAttributeAddressRange( 591 const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc, 592 uint64_t fail_value, bool check_specification_or_abstract_origin) const { 593 lo_pc = GetAttributeValueAsAddress(cu, DW_AT_low_pc, fail_value, 594 check_specification_or_abstract_origin); 595 if (lo_pc != fail_value) { 596 hi_pc = GetAttributeHighPC(cu, lo_pc, fail_value, 597 check_specification_or_abstract_origin); 598 if (hi_pc != fail_value) 599 return true; 600 } 601 lo_pc = fail_value; 602 hi_pc = fail_value; 603 return false; 604 } 605 606 size_t DWARFDebugInfoEntry::GetAttributeAddressRanges( 607 DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc, 608 bool check_specification_or_abstract_origin) const { 609 ranges.Clear(); 610 611 DWARFFormValue form_value; 612 if (GetAttributeValue(cu, DW_AT_ranges, form_value)) { 613 ranges = GetRangesOrReportError(*cu, *this, form_value); 614 } else if (check_hi_lo_pc) { 615 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 616 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 617 if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS, 618 check_specification_or_abstract_origin)) { 619 if (lo_pc < hi_pc) 620 ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc)); 621 } 622 } 623 return ranges.GetSize(); 624 } 625 626 // GetName 627 // 628 // Get value of the DW_AT_name attribute and return it if one exists, else 629 // return NULL. 630 const char *DWARFDebugInfoEntry::GetName(const DWARFUnit *cu) const { 631 return GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 632 } 633 634 // GetMangledName 635 // 636 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one 637 // exists, else return the value of the DW_AT_name attribute 638 const char * 639 DWARFDebugInfoEntry::GetMangledName(const DWARFUnit *cu, 640 bool substitute_name_allowed) const { 641 const char *name = nullptr; 642 643 name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true); 644 if (name) 645 return name; 646 647 name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true); 648 if (name) 649 return name; 650 651 if (!substitute_name_allowed) 652 return nullptr; 653 654 name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 655 return name; 656 } 657 658 // GetPubname 659 // 660 // Get value the name for a DIE as it should appear for a .debug_pubnames or 661 // .debug_pubtypes section. 662 const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const { 663 const char *name = nullptr; 664 if (!cu) 665 return name; 666 667 name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true); 668 if (name) 669 return name; 670 671 name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true); 672 if (name) 673 return name; 674 675 name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 676 return name; 677 } 678 679 /// This function is builds a table very similar to the standard .debug_aranges 680 /// table, except that the actual DIE offset for the function is placed in the 681 /// table instead of the compile unit offset. 682 void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable( 683 DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const { 684 if (m_tag) { 685 if (m_tag == DW_TAG_subprogram) { 686 DWARFRangeList ranges; 687 GetAttributeAddressRanges(cu, ranges, 688 /*check_hi_lo_pc=*/true); 689 for (const auto &r : ranges) { 690 debug_aranges->AppendRange(GetOffset(), r.GetRangeBase(), 691 r.GetRangeEnd()); 692 } 693 } 694 695 const DWARFDebugInfoEntry *child = GetFirstChild(); 696 while (child) { 697 child->BuildFunctionAddressRangeTable(cu, debug_aranges); 698 child = child->GetSibling(); 699 } 700 } 701 } 702 703 DWARFDeclContext 704 DWARFDebugInfoEntry::GetDWARFDeclContextStatic(const DWARFDebugInfoEntry *die, 705 DWARFUnit *cu) { 706 DWARFDeclContext dwarf_decl_ctx; 707 for (;;) { 708 const dw_tag_t tag = die->Tag(); 709 if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit) 710 return dwarf_decl_ctx; 711 dwarf_decl_ctx.AppendDeclContext(tag, die->GetName(cu)); 712 DWARFDIE parent_decl_ctx_die = die->GetParentDeclContextDIE(cu); 713 if (!parent_decl_ctx_die || parent_decl_ctx_die.GetDIE() == die) 714 return dwarf_decl_ctx; 715 if (parent_decl_ctx_die.Tag() == DW_TAG_compile_unit || 716 parent_decl_ctx_die.Tag() == DW_TAG_partial_unit) 717 return dwarf_decl_ctx; 718 die = parent_decl_ctx_die.GetDIE(); 719 cu = parent_decl_ctx_die.GetCU(); 720 } 721 } 722 723 DWARFDeclContext DWARFDebugInfoEntry::GetDWARFDeclContext(DWARFUnit *cu) const { 724 return GetDWARFDeclContextStatic(this, cu); 725 } 726 727 DWARFDIE 728 DWARFDebugInfoEntry::GetParentDeclContextDIE(DWARFUnit *cu) const { 729 DWARFAttributes attributes; 730 GetAttributes(cu, attributes, Recurse::yes); 731 return GetParentDeclContextDIE(cu, attributes); 732 } 733 734 DWARFDIE 735 DWARFDebugInfoEntry::GetParentDeclContextDIE( 736 DWARFUnit *cu, const DWARFAttributes &attributes) const { 737 DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this)); 738 739 while (die) { 740 // If this is the original DIE that we are searching for a declaration for, 741 // then don't look in the cache as we don't want our own decl context to be 742 // our decl context... 743 if (die.GetDIE() != this) { 744 switch (die.Tag()) { 745 case DW_TAG_compile_unit: 746 case DW_TAG_partial_unit: 747 case DW_TAG_namespace: 748 case DW_TAG_structure_type: 749 case DW_TAG_union_type: 750 case DW_TAG_class_type: 751 return die; 752 753 default: 754 break; 755 } 756 } 757 758 DWARFDIE spec_die = attributes.FormValueAsReference(DW_AT_specification); 759 if (spec_die) { 760 DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE(); 761 if (decl_ctx_die) 762 return decl_ctx_die; 763 } 764 765 DWARFDIE abs_die = attributes.FormValueAsReference(DW_AT_abstract_origin); 766 if (abs_die) { 767 DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE(); 768 if (decl_ctx_die) 769 return decl_ctx_die; 770 } 771 772 die = die.GetParent(); 773 } 774 return DWARFDIE(); 775 } 776 777 const char *DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu, 778 std::string &storage) const { 779 DWARFAttributes attributes; 780 GetAttributes(cu, attributes, Recurse::yes); 781 return GetQualifiedName(cu, attributes, storage); 782 } 783 784 const char * 785 DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu, 786 const DWARFAttributes &attributes, 787 std::string &storage) const { 788 789 const char *name = GetName(cu); 790 791 if (name) { 792 DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu); 793 storage.clear(); 794 // TODO: change this to get the correct decl context parent.... 795 while (parent_decl_ctx_die) { 796 const dw_tag_t parent_tag = parent_decl_ctx_die.Tag(); 797 switch (parent_tag) { 798 case DW_TAG_namespace: { 799 const char *namespace_name = parent_decl_ctx_die.GetName(); 800 if (namespace_name) { 801 storage.insert(0, "::"); 802 storage.insert(0, namespace_name); 803 } else { 804 storage.insert(0, "(anonymous namespace)::"); 805 } 806 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE(); 807 } break; 808 809 case DW_TAG_class_type: 810 case DW_TAG_structure_type: 811 case DW_TAG_union_type: { 812 const char *class_union_struct_name = parent_decl_ctx_die.GetName(); 813 814 if (class_union_struct_name) { 815 storage.insert(0, "::"); 816 storage.insert(0, class_union_struct_name); 817 } 818 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE(); 819 } break; 820 821 default: 822 parent_decl_ctx_die.Clear(); 823 break; 824 } 825 } 826 827 if (storage.empty()) 828 storage.append("::"); 829 830 storage.append(name); 831 } 832 if (storage.empty()) 833 return nullptr; 834 return storage.c_str(); 835 } 836 837 lldb::offset_t DWARFDebugInfoEntry::GetFirstAttributeOffset() const { 838 return GetOffset() + llvm::getULEB128Size(m_abbr_idx); 839 } 840 841 const DWARFAbbreviationDeclaration * 842 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const { 843 if (cu) { 844 const DWARFAbbreviationDeclarationSet *abbrev_set = cu->GetAbbreviations(); 845 if (abbrev_set) 846 return abbrev_set->GetAbbreviationDeclaration(m_abbr_idx); 847 } 848 return nullptr; 849 } 850 851 bool DWARFDebugInfoEntry::IsGlobalOrStaticScopeVariable() const { 852 if (Tag() != DW_TAG_variable) 853 return false; 854 const DWARFDebugInfoEntry *parent_die = GetParent(); 855 while (parent_die != nullptr) { 856 switch (parent_die->Tag()) { 857 case DW_TAG_subprogram: 858 case DW_TAG_lexical_block: 859 case DW_TAG_inlined_subroutine: 860 return false; 861 862 case DW_TAG_compile_unit: 863 case DW_TAG_partial_unit: 864 return true; 865 866 default: 867 break; 868 } 869 parent_die = parent_die->GetParent(); 870 } 871 return false; 872 } 873 874 bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const { 875 return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx && 876 m_sibling_idx == rhs.m_sibling_idx && 877 m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children && 878 m_tag == rhs.m_tag; 879 } 880 881 bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const { 882 return !(*this == rhs); 883 } 884