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 == 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(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(), value.Unsigned(), 221 toString(expected_ranges.takeError()).c_str()); 222 return DWARFRangeList(); 223 } 224 225 // GetDIENamesAndRanges 226 // 227 // Gets the valid address ranges for a given DIE by looking for a 228 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges attributes. 229 bool DWARFDebugInfoEntry::GetDIENamesAndRanges( 230 DWARFUnit *cu, const char *&name, const char *&mangled, 231 DWARFRangeList &ranges, int &decl_file, int &decl_line, int &decl_column, 232 int &call_file, int &call_line, int &call_column, 233 DWARFExpression *frame_base) const { 234 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 235 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 236 std::vector<DWARFDIE> dies; 237 bool set_frame_base_loclist_addr = false; 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 = DWARFExpression( 348 module, DataExtractor(data, block_offset, block_length), cu); 349 } else { 350 DataExtractor data = cu->GetLocationData(); 351 const dw_offset_t offset = form_value.Unsigned(); 352 if (data.ValidOffset(offset)) { 353 data = DataExtractor(data, offset, data.GetByteSize() - offset); 354 *frame_base = DWARFExpression(module, data, cu); 355 if (lo_pc != LLDB_INVALID_ADDRESS) { 356 assert(lo_pc >= cu->GetBaseAddress()); 357 frame_base->SetLocationListAddresses(cu->GetBaseAddress(), 358 lo_pc); 359 } else { 360 set_frame_base_loclist_addr = true; 361 } 362 } 363 } 364 } 365 break; 366 367 default: 368 break; 369 } 370 } 371 } 372 } 373 374 if (ranges.IsEmpty()) { 375 if (lo_pc != LLDB_INVALID_ADDRESS) { 376 if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc) 377 ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc)); 378 else 379 ranges.Append(DWARFRangeList::Entry(lo_pc, 0)); 380 } 381 } 382 383 if (set_frame_base_loclist_addr) { 384 dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0); 385 assert(lowest_range_pc >= cu->GetBaseAddress()); 386 frame_base->SetLocationListAddresses(cu->GetBaseAddress(), lowest_range_pc); 387 } 388 389 if (ranges.IsEmpty() || name == nullptr || mangled == nullptr) { 390 for (const DWARFDIE &die : dies) { 391 if (die) { 392 die.GetDIE()->GetDIENamesAndRanges(die.GetCU(), name, mangled, ranges, 393 decl_file, decl_line, decl_column, 394 call_file, call_line, call_column); 395 } 396 } 397 } 398 return !ranges.IsEmpty(); 399 } 400 401 // Get all attribute values for a given DIE, including following any 402 // specification or abstract origin attributes and including those in the 403 // results. Any duplicate attributes will have the first instance take 404 // precedence (this can happen for declaration attributes). 405 size_t DWARFDebugInfoEntry::GetAttributes(DWARFUnit *cu, 406 DWARFAttributes &attributes, 407 Recurse recurse, 408 uint32_t curr_depth) const { 409 const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu); 410 if (abbrevDecl) { 411 const DWARFDataExtractor &data = cu->GetData(); 412 lldb::offset_t offset = GetFirstAttributeOffset(); 413 414 const uint32_t num_attributes = abbrevDecl->NumAttributes(); 415 for (uint32_t i = 0; i < num_attributes; ++i) { 416 DWARFFormValue form_value(cu); 417 dw_attr_t attr; 418 abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value); 419 const dw_form_t form = form_value.Form(); 420 421 // If we are tracking down DW_AT_specification or DW_AT_abstract_origin 422 // attributes, the depth will be non-zero. We need to omit certain 423 // attributes that don't make sense. 424 switch (attr) { 425 case DW_AT_sibling: 426 case DW_AT_declaration: 427 if (curr_depth > 0) { 428 // This attribute doesn't make sense when combined with the DIE that 429 // references this DIE. We know a DIE is referencing this DIE because 430 // curr_depth is not zero 431 break; 432 } 433 LLVM_FALLTHROUGH; 434 default: 435 attributes.Append(form_value, offset, attr); 436 break; 437 } 438 439 if (recurse == Recurse::yes && 440 ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin))) { 441 if (form_value.ExtractValue(data, &offset)) { 442 DWARFDIE spec_die = form_value.Reference(); 443 if (spec_die) 444 spec_die.GetDIE()->GetAttributes(spec_die.GetCU(), attributes, 445 recurse, curr_depth + 1); 446 } 447 } else { 448 llvm::Optional<uint8_t> fixed_skip_size = DWARFFormValue::GetFixedSize(form, cu); 449 if (fixed_skip_size) 450 offset += *fixed_skip_size; 451 else 452 DWARFFormValue::SkipValue(form, data, &offset, cu); 453 } 454 } 455 } else { 456 attributes.Clear(); 457 } 458 return attributes.Size(); 459 } 460 461 // GetAttributeValue 462 // 463 // Get the value of an attribute and return the .debug_info or .debug_types 464 // offset of the attribute if it was properly extracted into form_value, 465 // or zero if we fail since an offset of zero is invalid for an attribute (it 466 // would be a compile unit header). 467 dw_offset_t DWARFDebugInfoEntry::GetAttributeValue( 468 const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &form_value, 469 dw_offset_t *end_attr_offset_ptr, 470 bool check_specification_or_abstract_origin) const { 471 if (const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu)) { 472 uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr); 473 474 if (attr_idx != DW_INVALID_INDEX) { 475 const DWARFDataExtractor &data = cu->GetData(); 476 lldb::offset_t offset = GetFirstAttributeOffset(); 477 478 uint32_t idx = 0; 479 while (idx < attr_idx) 480 DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++), 481 data, &offset, cu); 482 483 const dw_offset_t attr_offset = offset; 484 form_value.SetUnit(cu); 485 form_value.SetForm(abbrevDecl->GetFormByIndex(idx)); 486 if (form_value.ExtractValue(data, &offset)) { 487 if (end_attr_offset_ptr) 488 *end_attr_offset_ptr = offset; 489 return attr_offset; 490 } 491 } 492 } 493 494 if (check_specification_or_abstract_origin) { 495 if (GetAttributeValue(cu, DW_AT_specification, 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 if (GetAttributeValue(cu, DW_AT_abstract_origin, form_value)) { 506 DWARFDIE die = form_value.Reference(); 507 if (die) { 508 dw_offset_t die_offset = die.GetDIE()->GetAttributeValue( 509 die.GetCU(), attr, form_value, end_attr_offset_ptr, false); 510 if (die_offset) 511 return die_offset; 512 } 513 } 514 } 515 return 0; 516 } 517 518 // GetAttributeValueAsString 519 // 520 // Get the value of an attribute as a string return it. The resulting pointer 521 // to the string data exists within the supplied SymbolFileDWARF and will only 522 // be available as long as the SymbolFileDWARF is still around and it's content 523 // doesn't change. 524 const char *DWARFDebugInfoEntry::GetAttributeValueAsString( 525 const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value, 526 bool check_specification_or_abstract_origin) const { 527 DWARFFormValue form_value; 528 if (GetAttributeValue(cu, attr, form_value, nullptr, 529 check_specification_or_abstract_origin)) 530 return form_value.AsCString(); 531 return fail_value; 532 } 533 534 // GetAttributeValueAsUnsigned 535 // 536 // Get the value of an attribute as unsigned and return it. 537 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned( 538 const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, 539 bool check_specification_or_abstract_origin) const { 540 DWARFFormValue form_value; 541 if (GetAttributeValue(cu, attr, form_value, nullptr, 542 check_specification_or_abstract_origin)) 543 return form_value.Unsigned(); 544 return fail_value; 545 } 546 547 // GetAttributeValueAsReference 548 // 549 // Get the value of an attribute as reference and fix up and compile unit 550 // relative offsets as needed. 551 DWARFDIE DWARFDebugInfoEntry::GetAttributeValueAsReference( 552 const DWARFUnit *cu, const dw_attr_t attr, 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.Reference(); 558 return {}; 559 } 560 561 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress( 562 const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, 563 bool check_specification_or_abstract_origin) const { 564 DWARFFormValue form_value; 565 if (GetAttributeValue(cu, attr, form_value, nullptr, 566 check_specification_or_abstract_origin)) 567 return form_value.Address(); 568 return fail_value; 569 } 570 571 // GetAttributeHighPC 572 // 573 // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low- 574 // pc>. 575 // 576 // Returns the hi_pc or fail_value. 577 dw_addr_t DWARFDebugInfoEntry::GetAttributeHighPC( 578 const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value, 579 bool check_specification_or_abstract_origin) const { 580 DWARFFormValue form_value; 581 if (GetAttributeValue(cu, DW_AT_high_pc, form_value, nullptr, 582 check_specification_or_abstract_origin)) { 583 dw_form_t form = form_value.Form(); 584 if (form == DW_FORM_addr || form == DW_FORM_addrx || 585 form == DW_FORM_GNU_addr_index) 586 return form_value.Address(); 587 588 // DWARF4 can specify the hi_pc as an <offset-from-lowpc> 589 return lo_pc + form_value.Unsigned(); 590 } 591 return fail_value; 592 } 593 594 // GetAttributeAddressRange 595 // 596 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset- 597 // from-low-pc>. 598 // 599 // Returns true or sets lo_pc and hi_pc to fail_value. 600 bool DWARFDebugInfoEntry::GetAttributeAddressRange( 601 const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc, 602 uint64_t fail_value, bool check_specification_or_abstract_origin) const { 603 lo_pc = GetAttributeValueAsAddress(cu, DW_AT_low_pc, fail_value, 604 check_specification_or_abstract_origin); 605 if (lo_pc != fail_value) { 606 hi_pc = GetAttributeHighPC(cu, lo_pc, fail_value, 607 check_specification_or_abstract_origin); 608 if (hi_pc != fail_value) 609 return true; 610 } 611 lo_pc = fail_value; 612 hi_pc = fail_value; 613 return false; 614 } 615 616 size_t DWARFDebugInfoEntry::GetAttributeAddressRanges( 617 DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc, 618 bool check_specification_or_abstract_origin) const { 619 ranges.Clear(); 620 621 DWARFFormValue form_value; 622 if (GetAttributeValue(cu, DW_AT_ranges, form_value)) { 623 ranges = GetRangesOrReportError(*cu, *this, form_value); 624 } else if (check_hi_lo_pc) { 625 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 626 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 627 if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS, 628 check_specification_or_abstract_origin)) { 629 if (lo_pc < hi_pc) 630 ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc)); 631 } 632 } 633 return ranges.GetSize(); 634 } 635 636 // GetName 637 // 638 // Get value of the DW_AT_name attribute and return it if one exists, else 639 // return NULL. 640 const char *DWARFDebugInfoEntry::GetName(const DWARFUnit *cu) const { 641 return GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 642 } 643 644 // GetMangledName 645 // 646 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one 647 // exists, else return the value of the DW_AT_name attribute 648 const char * 649 DWARFDebugInfoEntry::GetMangledName(const DWARFUnit *cu, 650 bool substitute_name_allowed) const { 651 const char *name = nullptr; 652 653 name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true); 654 if (name) 655 return name; 656 657 name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true); 658 if (name) 659 return name; 660 661 if (!substitute_name_allowed) 662 return nullptr; 663 664 name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 665 return name; 666 } 667 668 // GetPubname 669 // 670 // Get value the name for a DIE as it should appear for a .debug_pubnames or 671 // .debug_pubtypes section. 672 const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const { 673 const char *name = nullptr; 674 if (!cu) 675 return name; 676 677 name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true); 678 if (name) 679 return name; 680 681 name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true); 682 if (name) 683 return name; 684 685 name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 686 return name; 687 } 688 689 /// This function is builds a table very similar to the standard .debug_aranges 690 /// table, except that the actual DIE offset for the function is placed in the 691 /// table instead of the compile unit offset. 692 void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable( 693 DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const { 694 if (m_tag) { 695 if (m_tag == DW_TAG_subprogram) { 696 DWARFRangeList ranges; 697 GetAttributeAddressRanges(cu, ranges, 698 /*check_hi_lo_pc=*/true); 699 for (const auto &r : ranges) { 700 debug_aranges->AppendRange(GetOffset(), r.GetRangeBase(), 701 r.GetRangeEnd()); 702 } 703 } 704 705 const DWARFDebugInfoEntry *child = GetFirstChild(); 706 while (child) { 707 child->BuildFunctionAddressRangeTable(cu, debug_aranges); 708 child = child->GetSibling(); 709 } 710 } 711 } 712 713 DWARFDeclContext 714 DWARFDebugInfoEntry::GetDWARFDeclContextStatic(const DWARFDebugInfoEntry *die, 715 DWARFUnit *cu) { 716 DWARFDeclContext dwarf_decl_ctx; 717 for (;;) { 718 const dw_tag_t tag = die->Tag(); 719 if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit) 720 return dwarf_decl_ctx; 721 dwarf_decl_ctx.AppendDeclContext(tag, die->GetName(cu)); 722 DWARFDIE parent_decl_ctx_die = die->GetParentDeclContextDIE(cu); 723 if (!parent_decl_ctx_die || parent_decl_ctx_die.GetDIE() == die) 724 return dwarf_decl_ctx; 725 if (parent_decl_ctx_die.Tag() == DW_TAG_compile_unit || 726 parent_decl_ctx_die.Tag() == DW_TAG_partial_unit) 727 return dwarf_decl_ctx; 728 die = parent_decl_ctx_die.GetDIE(); 729 cu = parent_decl_ctx_die.GetCU(); 730 } 731 } 732 733 DWARFDeclContext DWARFDebugInfoEntry::GetDWARFDeclContext(DWARFUnit *cu) const { 734 return GetDWARFDeclContextStatic(this, cu); 735 } 736 737 DWARFDIE 738 DWARFDebugInfoEntry::GetParentDeclContextDIE(DWARFUnit *cu) const { 739 DWARFAttributes attributes; 740 GetAttributes(cu, attributes, Recurse::yes); 741 return GetParentDeclContextDIE(cu, attributes); 742 } 743 744 DWARFDIE 745 DWARFDebugInfoEntry::GetParentDeclContextDIE( 746 DWARFUnit *cu, const DWARFAttributes &attributes) const { 747 DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this)); 748 749 while (die) { 750 // If this is the original DIE that we are searching for a declaration for, 751 // then don't look in the cache as we don't want our own decl context to be 752 // our decl context... 753 if (die.GetDIE() != this) { 754 switch (die.Tag()) { 755 case DW_TAG_compile_unit: 756 case DW_TAG_partial_unit: 757 case DW_TAG_namespace: 758 case DW_TAG_structure_type: 759 case DW_TAG_union_type: 760 case DW_TAG_class_type: 761 return die; 762 763 default: 764 break; 765 } 766 } 767 768 DWARFDIE spec_die = attributes.FormValueAsReference(DW_AT_specification); 769 if (spec_die) { 770 DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE(); 771 if (decl_ctx_die) 772 return decl_ctx_die; 773 } 774 775 DWARFDIE abs_die = attributes.FormValueAsReference(DW_AT_abstract_origin); 776 if (abs_die) { 777 DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE(); 778 if (decl_ctx_die) 779 return decl_ctx_die; 780 } 781 782 die = die.GetParent(); 783 } 784 return DWARFDIE(); 785 } 786 787 const char *DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu, 788 std::string &storage) const { 789 DWARFAttributes attributes; 790 GetAttributes(cu, attributes, Recurse::yes); 791 return GetQualifiedName(cu, attributes, storage); 792 } 793 794 const char * 795 DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu, 796 const DWARFAttributes &attributes, 797 std::string &storage) const { 798 799 const char *name = GetName(cu); 800 801 if (name) { 802 DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu); 803 storage.clear(); 804 // TODO: change this to get the correct decl context parent.... 805 while (parent_decl_ctx_die) { 806 const dw_tag_t parent_tag = parent_decl_ctx_die.Tag(); 807 switch (parent_tag) { 808 case DW_TAG_namespace: { 809 const char *namespace_name = parent_decl_ctx_die.GetName(); 810 if (namespace_name) { 811 storage.insert(0, "::"); 812 storage.insert(0, namespace_name); 813 } else { 814 storage.insert(0, "(anonymous namespace)::"); 815 } 816 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE(); 817 } break; 818 819 case DW_TAG_class_type: 820 case DW_TAG_structure_type: 821 case DW_TAG_union_type: { 822 const char *class_union_struct_name = parent_decl_ctx_die.GetName(); 823 824 if (class_union_struct_name) { 825 storage.insert(0, "::"); 826 storage.insert(0, class_union_struct_name); 827 } 828 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE(); 829 } break; 830 831 default: 832 parent_decl_ctx_die.Clear(); 833 break; 834 } 835 } 836 837 if (storage.empty()) 838 storage.append("::"); 839 840 storage.append(name); 841 } 842 if (storage.empty()) 843 return nullptr; 844 return storage.c_str(); 845 } 846 847 lldb::offset_t DWARFDebugInfoEntry::GetFirstAttributeOffset() const { 848 return GetOffset() + llvm::getULEB128Size(m_abbr_idx); 849 } 850 851 const DWARFAbbreviationDeclaration * 852 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const { 853 if (cu) { 854 const DWARFAbbreviationDeclarationSet *abbrev_set = cu->GetAbbreviations(); 855 if (abbrev_set) 856 return abbrev_set->GetAbbreviationDeclaration(m_abbr_idx); 857 } 858 return nullptr; 859 } 860 861 bool DWARFDebugInfoEntry::IsGlobalOrStaticScopeVariable() const { 862 if (Tag() != DW_TAG_variable) 863 return false; 864 const DWARFDebugInfoEntry *parent_die = GetParent(); 865 while (parent_die != nullptr) { 866 switch (parent_die->Tag()) { 867 case DW_TAG_subprogram: 868 case DW_TAG_lexical_block: 869 case DW_TAG_inlined_subroutine: 870 return false; 871 872 case DW_TAG_compile_unit: 873 case DW_TAG_partial_unit: 874 return true; 875 876 default: 877 break; 878 } 879 parent_die = parent_die->GetParent(); 880 } 881 return false; 882 } 883 884 bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const { 885 return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx && 886 m_sibling_idx == rhs.m_sibling_idx && 887 m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children && 888 m_tag == rhs.m_tag; 889 } 890 891 bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const { 892 return !(*this == rhs); 893 } 894