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