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