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