1 //===-- DWARFDebugInfoEntry.cpp ---------------------------------*- C++ -*-===// 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_rnglistx: 160 case DW_FORM_sdata: 161 case DW_FORM_udata: 162 case DW_FORM_ref_udata: 163 case DW_FORM_GNU_addr_index: 164 case DW_FORM_GNU_str_index: 165 case DW_FORM_strx: 166 data.Skip_LEB128(&offset); 167 break; 168 169 case DW_FORM_indirect: 170 form_is_indirect = true; 171 form = data.GetULEB128(&offset); 172 break; 173 174 case DW_FORM_strp: 175 case DW_FORM_sec_offset: 176 data.GetU32(&offset); 177 break; 178 179 case DW_FORM_implicit_const: 180 form_size = 0; 181 break; 182 183 default: 184 *offset_ptr = m_offset; 185 return false; 186 } 187 offset += form_size; 188 189 } while (form_is_indirect); 190 } 191 } 192 *offset_ptr = offset; 193 return true; 194 } else { 195 m_tag = llvm::dwarf::DW_TAG_null; 196 m_has_children = false; 197 return true; // NULL debug tag entry 198 } 199 200 return false; 201 } 202 203 static DWARFRangeList GetRangesOrReportError(DWARFUnit &unit, 204 const DWARFDebugInfoEntry &die, 205 const DWARFFormValue &value) { 206 llvm::Expected<DWARFRangeList> expected_ranges = 207 (value.Form() == DW_FORM_rnglistx) 208 ? unit.FindRnglistFromIndex(value.Unsigned()) 209 : unit.FindRnglistFromOffset(value.Unsigned()); 210 if (expected_ranges) 211 return std::move(*expected_ranges); 212 unit.GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError( 213 "{0x%8.8x}: DIE has DW_AT_ranges(0x%" PRIx64 ") attribute, but " 214 "range extraction failed (%s), please file a bug " 215 "and attach the file at the start of this error message", 216 die.GetOffset(), value.Unsigned(), 217 toString(expected_ranges.takeError()).c_str()); 218 return DWARFRangeList(); 219 } 220 221 // GetDIENamesAndRanges 222 // 223 // Gets the valid address ranges for a given DIE by looking for a 224 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges attributes. 225 bool DWARFDebugInfoEntry::GetDIENamesAndRanges( 226 DWARFUnit *cu, const char *&name, const char *&mangled, 227 DWARFRangeList &ranges, int &decl_file, int &decl_line, int &decl_column, 228 int &call_file, int &call_line, int &call_column, 229 DWARFExpression *frame_base) const { 230 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 231 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 232 std::vector<DWARFDIE> dies; 233 bool set_frame_base_loclist_addr = false; 234 235 const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu); 236 237 SymbolFileDWARF &dwarf = cu->GetSymbolFileDWARF(); 238 lldb::ModuleSP module = dwarf.GetObjectFile()->GetModule(); 239 240 if (abbrevDecl) { 241 const DWARFDataExtractor &data = cu->GetData(); 242 lldb::offset_t offset = GetFirstAttributeOffset(); 243 244 if (!data.ValidOffset(offset)) 245 return false; 246 247 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 248 bool do_offset = false; 249 250 for (uint32_t i = 0; i < numAttributes; ++i) { 251 DWARFFormValue form_value(cu); 252 dw_attr_t attr; 253 abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value); 254 255 if (form_value.ExtractValue(data, &offset)) { 256 switch (attr) { 257 case DW_AT_low_pc: 258 lo_pc = form_value.Address(); 259 260 if (do_offset) 261 hi_pc += lo_pc; 262 do_offset = false; 263 break; 264 265 case DW_AT_entry_pc: 266 lo_pc = form_value.Address(); 267 break; 268 269 case DW_AT_high_pc: 270 if (form_value.Form() == DW_FORM_addr || 271 form_value.Form() == DW_FORM_addrx || 272 form_value.Form() == DW_FORM_GNU_addr_index) { 273 hi_pc = form_value.Address(); 274 } else { 275 hi_pc = form_value.Unsigned(); 276 if (lo_pc == LLDB_INVALID_ADDRESS) 277 do_offset = hi_pc != LLDB_INVALID_ADDRESS; 278 else 279 hi_pc += lo_pc; // DWARF 4 introduces <offset-from-lo-pc> to save 280 // on relocations 281 } 282 break; 283 284 case DW_AT_ranges: 285 ranges = GetRangesOrReportError(*cu, *this, form_value); 286 break; 287 288 case DW_AT_name: 289 if (name == nullptr) 290 name = form_value.AsCString(); 291 break; 292 293 case DW_AT_MIPS_linkage_name: 294 case DW_AT_linkage_name: 295 if (mangled == nullptr) 296 mangled = form_value.AsCString(); 297 break; 298 299 case DW_AT_abstract_origin: 300 dies.push_back(form_value.Reference()); 301 break; 302 303 case DW_AT_specification: 304 dies.push_back(form_value.Reference()); 305 break; 306 307 case DW_AT_decl_file: 308 if (decl_file == 0) 309 decl_file = form_value.Unsigned(); 310 break; 311 312 case DW_AT_decl_line: 313 if (decl_line == 0) 314 decl_line = form_value.Unsigned(); 315 break; 316 317 case DW_AT_decl_column: 318 if (decl_column == 0) 319 decl_column = form_value.Unsigned(); 320 break; 321 322 case DW_AT_call_file: 323 if (call_file == 0) 324 call_file = form_value.Unsigned(); 325 break; 326 327 case DW_AT_call_line: 328 if (call_line == 0) 329 call_line = form_value.Unsigned(); 330 break; 331 332 case DW_AT_call_column: 333 if (call_column == 0) 334 call_column = form_value.Unsigned(); 335 break; 336 337 case DW_AT_frame_base: 338 if (frame_base) { 339 if (form_value.BlockData()) { 340 uint32_t block_offset = 341 form_value.BlockData() - data.GetDataStart(); 342 uint32_t block_length = form_value.Unsigned(); 343 *frame_base = DWARFExpression( 344 module, DataExtractor(data, block_offset, block_length), cu); 345 } else { 346 DataExtractor data = dwarf.DebugLocData(); 347 const dw_offset_t offset = form_value.Unsigned(); 348 if (data.ValidOffset(offset)) { 349 data = DataExtractor(data, offset, data.GetByteSize() - offset); 350 *frame_base = DWARFExpression(module, data, cu); 351 if (lo_pc != LLDB_INVALID_ADDRESS) { 352 assert(lo_pc >= cu->GetBaseAddress()); 353 frame_base->SetLocationListAddresses(cu->GetBaseAddress(), 354 lo_pc); 355 } else { 356 set_frame_base_loclist_addr = true; 357 } 358 } 359 } 360 } 361 break; 362 363 default: 364 break; 365 } 366 } 367 } 368 } 369 370 if (ranges.IsEmpty()) { 371 if (lo_pc != LLDB_INVALID_ADDRESS) { 372 if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc) 373 ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc)); 374 else 375 ranges.Append(DWARFRangeList::Entry(lo_pc, 0)); 376 } 377 } 378 379 if (set_frame_base_loclist_addr) { 380 dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0); 381 assert(lowest_range_pc >= cu->GetBaseAddress()); 382 frame_base->SetLocationListAddresses(cu->GetBaseAddress(), lowest_range_pc); 383 } 384 385 if (ranges.IsEmpty() || name == nullptr || mangled == nullptr) { 386 for (const DWARFDIE &die : dies) { 387 if (die) { 388 die.GetDIE()->GetDIENamesAndRanges(die.GetCU(), name, mangled, ranges, 389 decl_file, decl_line, decl_column, 390 call_file, call_line, call_column); 391 } 392 } 393 } 394 return !ranges.IsEmpty(); 395 } 396 397 // Dump 398 // 399 // Dumps a debug information entry and all of it's attributes to the specified 400 // stream. 401 void DWARFDebugInfoEntry::Dump(const DWARFUnit *cu, Stream &s, 402 uint32_t recurse_depth) const { 403 const DWARFDataExtractor &data = cu->GetData(); 404 lldb::offset_t offset = m_offset; 405 406 if (data.ValidOffset(offset)) { 407 dw_uleb128_t abbrCode = data.GetULEB128(&offset); 408 409 s.Printf("\n0x%8.8x: ", m_offset); 410 s.Indent(); 411 if (abbrCode != m_abbr_idx) { 412 s.Printf("error: DWARF has been modified\n"); 413 } else if (abbrCode) { 414 const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu); 415 if (abbrevDecl) { 416 s.PutCString(DW_TAG_value_to_name(abbrevDecl->Tag())); 417 s.Printf(" [%u] %c\n", abbrCode, abbrevDecl->HasChildren() ? '*' : ' '); 418 419 // Dump all data in the .debug_info/.debug_types for the attributes 420 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 421 for (uint32_t i = 0; i < numAttributes; ++i) { 422 DWARFFormValue form_value(cu); 423 dw_attr_t attr; 424 abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value); 425 426 DumpAttribute(cu, data, &offset, s, attr, form_value); 427 } 428 429 const DWARFDebugInfoEntry *child = GetFirstChild(); 430 if (recurse_depth > 0 && child) { 431 s.IndentMore(); 432 433 while (child) { 434 child->Dump(cu, s, recurse_depth - 1); 435 child = child->GetSibling(); 436 } 437 s.IndentLess(); 438 } 439 } else 440 s.Printf("Abbreviation code note found in 'debug_abbrev' class for " 441 "code: %u\n", 442 abbrCode); 443 } else { 444 s.Printf("NULL\n"); 445 } 446 } 447 } 448 449 // DumpAttribute 450 // 451 // Dumps a debug information entry attribute along with it's form. Any special 452 // display of attributes is done (disassemble location lists, show enumeration 453 // values for attributes, etc). 454 void DWARFDebugInfoEntry::DumpAttribute( 455 const DWARFUnit *cu, const DWARFDataExtractor &data, 456 lldb::offset_t *offset_ptr, Stream &s, dw_attr_t attr, 457 DWARFFormValue &form_value) { 458 bool show_form = s.GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowForm); 459 460 s.Printf(" "); 461 s.Indent(DW_AT_value_to_name(attr)); 462 463 if (show_form) { 464 s.Printf("[%s", DW_FORM_value_to_name(form_value.Form())); 465 } 466 467 if (!form_value.ExtractValue(data, offset_ptr)) 468 return; 469 470 if (show_form) { 471 if (form_value.Form() == DW_FORM_indirect) { 472 s.Printf(" [%s]", DW_FORM_value_to_name(form_value.Form())); 473 } 474 475 s.PutCString("] "); 476 } 477 478 s.PutCString("( "); 479 480 SymbolFileDWARF &dwarf = cu->GetSymbolFileDWARF(); 481 482 // Check to see if we have any special attribute formatters 483 switch (attr) { 484 case DW_AT_stmt_list: 485 s.Printf("0x%8.8" PRIx64, form_value.Unsigned()); 486 break; 487 488 case DW_AT_language: 489 s.PutCString(DW_LANG_value_to_name(form_value.Unsigned())); 490 break; 491 492 case DW_AT_encoding: 493 s.PutCString(DW_ATE_value_to_name(form_value.Unsigned())); 494 break; 495 496 case DW_AT_frame_base: 497 case DW_AT_location: 498 case DW_AT_data_member_location: { 499 const uint8_t *blockData = form_value.BlockData(); 500 if (blockData) { 501 // Location description is inlined in data in the form value 502 DWARFDataExtractor locationData(data, 503 (*offset_ptr) - form_value.Unsigned(), 504 form_value.Unsigned()); 505 DWARFExpression::PrintDWARFExpression( 506 s, locationData, DWARFUnit::GetAddressByteSize(cu), 4, false); 507 } else { 508 // We have a location list offset as the value that is the offset into 509 // the .debug_loc section that describes the value over it's lifetime 510 uint64_t debug_loc_offset = form_value.Unsigned(); 511 DWARFExpression::PrintDWARFLocationList(s, cu, dwarf.DebugLocData(), 512 debug_loc_offset); 513 } 514 } break; 515 516 case DW_AT_abstract_origin: 517 case DW_AT_specification: { 518 DWARFDIE abstract_die = form_value.Reference(); 519 form_value.Dump(s); 520 // *ostrm_ptr << HEX32 << abstract_die.GetOffset() << " ( "; 521 abstract_die.GetName(s); 522 } break; 523 524 case DW_AT_type: { 525 DWARFDIE type_die = form_value.Reference(); 526 s.PutCString(" ( "); 527 type_die.AppendTypeName(s); 528 s.PutCString(" )"); 529 } break; 530 531 default: 532 break; 533 } 534 535 s.PutCString(" )\n"); 536 } 537 538 // Get all attribute values for a given DIE, including following any 539 // specification or abstract origin attributes and including those in the 540 // results. Any duplicate attributes will have the first instance take 541 // precedence (this can happen for declaration attributes). 542 size_t DWARFDebugInfoEntry::GetAttributes( 543 const DWARFUnit *cu, DWARFAttributes &attributes, 544 uint32_t curr_depth) const { 545 const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu); 546 if (abbrevDecl) { 547 const DWARFDataExtractor &data = cu->GetData(); 548 lldb::offset_t offset = GetFirstAttributeOffset(); 549 550 const uint32_t num_attributes = abbrevDecl->NumAttributes(); 551 for (uint32_t i = 0; i < num_attributes; ++i) { 552 DWARFFormValue form_value(cu); 553 dw_attr_t attr; 554 abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value); 555 const dw_form_t form = form_value.Form(); 556 557 // If we are tracking down DW_AT_specification or DW_AT_abstract_origin 558 // attributes, the depth will be non-zero. We need to omit certain 559 // attributes that don't make sense. 560 switch (attr) { 561 case DW_AT_sibling: 562 case DW_AT_declaration: 563 if (curr_depth > 0) { 564 // This attribute doesn't make sense when combined with the DIE that 565 // references this DIE. We know a DIE is referencing this DIE because 566 // curr_depth is not zero 567 break; 568 } 569 LLVM_FALLTHROUGH; 570 default: 571 attributes.Append(cu, offset, attr, form); 572 break; 573 } 574 575 if ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin)) { 576 if (form_value.ExtractValue(data, &offset)) { 577 DWARFDIE spec_die = form_value.Reference(); 578 if (spec_die) 579 spec_die.GetAttributes(attributes, curr_depth + 1); 580 } 581 } else { 582 llvm::Optional<uint8_t> fixed_skip_size = DWARFFormValue::GetFixedSize(form, cu); 583 if (fixed_skip_size) 584 offset += *fixed_skip_size; 585 else 586 DWARFFormValue::SkipValue(form, data, &offset, cu); 587 } 588 } 589 } else { 590 attributes.Clear(); 591 } 592 return attributes.Size(); 593 } 594 595 // GetAttributeValue 596 // 597 // Get the value of an attribute and return the .debug_info or .debug_types 598 // offset of the attribute if it was properly extracted into form_value, 599 // or zero if we fail since an offset of zero is invalid for an attribute (it 600 // would be a compile unit header). 601 dw_offset_t DWARFDebugInfoEntry::GetAttributeValue( 602 const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &form_value, 603 dw_offset_t *end_attr_offset_ptr, 604 bool check_specification_or_abstract_origin) const { 605 if (const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu)) { 606 uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr); 607 608 if (attr_idx != DW_INVALID_INDEX) { 609 const DWARFDataExtractor &data = cu->GetData(); 610 lldb::offset_t offset = GetFirstAttributeOffset(); 611 612 uint32_t idx = 0; 613 while (idx < attr_idx) 614 DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++), 615 data, &offset, cu); 616 617 const dw_offset_t attr_offset = offset; 618 form_value.SetUnit(cu); 619 form_value.SetForm(abbrevDecl->GetFormByIndex(idx)); 620 if (form_value.ExtractValue(data, &offset)) { 621 if (end_attr_offset_ptr) 622 *end_attr_offset_ptr = offset; 623 return attr_offset; 624 } 625 } 626 } 627 628 if (check_specification_or_abstract_origin) { 629 if (GetAttributeValue(cu, DW_AT_specification, form_value)) { 630 DWARFDIE die = form_value.Reference(); 631 if (die) { 632 dw_offset_t die_offset = die.GetDIE()->GetAttributeValue( 633 die.GetCU(), attr, form_value, end_attr_offset_ptr, false); 634 if (die_offset) 635 return die_offset; 636 } 637 } 638 639 if (GetAttributeValue(cu, DW_AT_abstract_origin, form_value)) { 640 DWARFDIE die = form_value.Reference(); 641 if (die) { 642 dw_offset_t die_offset = die.GetDIE()->GetAttributeValue( 643 die.GetCU(), attr, form_value, end_attr_offset_ptr, false); 644 if (die_offset) 645 return die_offset; 646 } 647 } 648 } 649 650 // If we're a unit DIE, also check the attributes of the dwo unit (if any). 651 if (GetParent()) 652 return 0; 653 SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile(); 654 if (!dwo_symbol_file) 655 return 0; 656 657 DWARFCompileUnit *dwo_cu = dwo_symbol_file->GetCompileUnit(); 658 if (!dwo_cu) 659 return 0; 660 661 DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly(); 662 if (!dwo_cu_die.IsValid()) 663 return 0; 664 665 return dwo_cu_die.GetDIE()->GetAttributeValue( 666 dwo_cu, attr, form_value, end_attr_offset_ptr, 667 check_specification_or_abstract_origin); 668 } 669 670 // GetAttributeValueAsString 671 // 672 // Get the value of an attribute as a string return it. The resulting pointer 673 // to the string data exists within the supplied SymbolFileDWARF and will only 674 // be available as long as the SymbolFileDWARF is still around and it's content 675 // doesn't change. 676 const char *DWARFDebugInfoEntry::GetAttributeValueAsString( 677 const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value, 678 bool check_specification_or_abstract_origin) const { 679 DWARFFormValue form_value; 680 if (GetAttributeValue(cu, attr, form_value, nullptr, 681 check_specification_or_abstract_origin)) 682 return form_value.AsCString(); 683 return fail_value; 684 } 685 686 // GetAttributeValueAsUnsigned 687 // 688 // Get the value of an attribute as unsigned and return it. 689 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned( 690 const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, 691 bool check_specification_or_abstract_origin) const { 692 DWARFFormValue form_value; 693 if (GetAttributeValue(cu, attr, form_value, nullptr, 694 check_specification_or_abstract_origin)) 695 return form_value.Unsigned(); 696 return fail_value; 697 } 698 699 // GetAttributeValueAsReference 700 // 701 // Get the value of an attribute as reference and fix up and compile unit 702 // relative offsets as needed. 703 DWARFDIE DWARFDebugInfoEntry::GetAttributeValueAsReference( 704 const DWARFUnit *cu, const dw_attr_t attr, 705 bool check_specification_or_abstract_origin) const { 706 DWARFFormValue form_value; 707 if (GetAttributeValue(cu, attr, form_value, nullptr, 708 check_specification_or_abstract_origin)) 709 return form_value.Reference(); 710 return {}; 711 } 712 713 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress( 714 const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, 715 bool check_specification_or_abstract_origin) const { 716 DWARFFormValue form_value; 717 if (GetAttributeValue(cu, attr, form_value, nullptr, 718 check_specification_or_abstract_origin)) 719 return form_value.Address(); 720 return fail_value; 721 } 722 723 // GetAttributeHighPC 724 // 725 // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low- 726 // pc>. 727 // 728 // Returns the hi_pc or fail_value. 729 dw_addr_t DWARFDebugInfoEntry::GetAttributeHighPC( 730 const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value, 731 bool check_specification_or_abstract_origin) const { 732 DWARFFormValue form_value; 733 if (GetAttributeValue(cu, DW_AT_high_pc, form_value, nullptr, 734 check_specification_or_abstract_origin)) { 735 dw_form_t form = form_value.Form(); 736 if (form == DW_FORM_addr || form == DW_FORM_addrx || 737 form == DW_FORM_GNU_addr_index) 738 return form_value.Address(); 739 740 // DWARF4 can specify the hi_pc as an <offset-from-lowpc> 741 return lo_pc + form_value.Unsigned(); 742 } 743 return fail_value; 744 } 745 746 // GetAttributeAddressRange 747 // 748 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset- 749 // from-low-pc>. 750 // 751 // Returns true or sets lo_pc and hi_pc to fail_value. 752 bool DWARFDebugInfoEntry::GetAttributeAddressRange( 753 const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc, 754 uint64_t fail_value, bool check_specification_or_abstract_origin) const { 755 lo_pc = GetAttributeValueAsAddress(cu, DW_AT_low_pc, fail_value, 756 check_specification_or_abstract_origin); 757 if (lo_pc != fail_value) { 758 hi_pc = GetAttributeHighPC(cu, lo_pc, fail_value, 759 check_specification_or_abstract_origin); 760 if (hi_pc != fail_value) 761 return true; 762 } 763 lo_pc = fail_value; 764 hi_pc = fail_value; 765 return false; 766 } 767 768 size_t DWARFDebugInfoEntry::GetAttributeAddressRanges( 769 DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc, 770 bool check_specification_or_abstract_origin) const { 771 ranges.Clear(); 772 773 DWARFFormValue form_value; 774 if (GetAttributeValue(cu, DW_AT_ranges, form_value)) { 775 ranges = GetRangesOrReportError(*cu, *this, form_value); 776 } else if (check_hi_lo_pc) { 777 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 778 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 779 if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS, 780 check_specification_or_abstract_origin)) { 781 if (lo_pc < hi_pc) 782 ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc)); 783 } 784 } 785 return ranges.GetSize(); 786 } 787 788 // GetName 789 // 790 // Get value of the DW_AT_name attribute and return it if one exists, else 791 // return NULL. 792 const char *DWARFDebugInfoEntry::GetName(const DWARFUnit *cu) const { 793 return GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 794 } 795 796 // GetMangledName 797 // 798 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one 799 // exists, else return the value of the DW_AT_name attribute 800 const char * 801 DWARFDebugInfoEntry::GetMangledName(const DWARFUnit *cu, 802 bool substitute_name_allowed) const { 803 const char *name = nullptr; 804 805 name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true); 806 if (name) 807 return name; 808 809 name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true); 810 if (name) 811 return name; 812 813 if (!substitute_name_allowed) 814 return nullptr; 815 816 name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 817 return name; 818 } 819 820 // GetPubname 821 // 822 // Get value the name for a DIE as it should appear for a .debug_pubnames or 823 // .debug_pubtypes section. 824 const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const { 825 const char *name = nullptr; 826 if (!cu) 827 return name; 828 829 name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true); 830 if (name) 831 return name; 832 833 name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true); 834 if (name) 835 return name; 836 837 name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 838 return name; 839 } 840 841 // BuildAddressRangeTable 842 void DWARFDebugInfoEntry::BuildAddressRangeTable( 843 const DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const { 844 if (m_tag) { 845 if (m_tag == DW_TAG_subprogram) { 846 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 847 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 848 if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS)) { 849 /// printf("BuildAddressRangeTable() 0x%8.8x: %30s: [0x%8.8x - 850 /// 0x%8.8x)\n", m_offset, DW_TAG_value_to_name(tag), lo_pc, hi_pc); 851 debug_aranges->AppendRange(cu->GetOffset(), lo_pc, hi_pc); 852 } 853 } 854 855 const DWARFDebugInfoEntry *child = GetFirstChild(); 856 while (child) { 857 child->BuildAddressRangeTable(cu, debug_aranges); 858 child = child->GetSibling(); 859 } 860 } 861 } 862 863 // BuildFunctionAddressRangeTable 864 // 865 // This function is very similar to the BuildAddressRangeTable function except 866 // that the actual DIE offset for the function is placed in the table instead 867 // of the compile unit offset (which is the way the standard .debug_aranges 868 // section does it). 869 void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable( 870 const DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const { 871 if (m_tag) { 872 if (m_tag == DW_TAG_subprogram) { 873 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 874 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 875 if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS)) { 876 // printf("BuildAddressRangeTable() 0x%8.8x: [0x%16.16" PRIx64 " - 877 // 0x%16.16" PRIx64 ")\n", m_offset, lo_pc, hi_pc); // DEBUG ONLY 878 debug_aranges->AppendRange(GetOffset(), lo_pc, hi_pc); 879 } 880 } 881 882 const DWARFDebugInfoEntry *child = GetFirstChild(); 883 while (child) { 884 child->BuildFunctionAddressRangeTable(cu, debug_aranges); 885 child = child->GetSibling(); 886 } 887 } 888 } 889 890 void DWARFDebugInfoEntry::GetDWARFDeclContext( 891 DWARFUnit *cu, DWARFDeclContext &dwarf_decl_ctx) const { 892 const dw_tag_t tag = Tag(); 893 if (tag != DW_TAG_compile_unit && tag != DW_TAG_partial_unit) { 894 dwarf_decl_ctx.AppendDeclContext(tag, GetName(cu)); 895 DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu); 896 if (parent_decl_ctx_die && parent_decl_ctx_die.GetDIE() != this) { 897 if (parent_decl_ctx_die.Tag() != DW_TAG_compile_unit && 898 parent_decl_ctx_die.Tag() != DW_TAG_partial_unit) 899 parent_decl_ctx_die.GetDIE()->GetDWARFDeclContext( 900 parent_decl_ctx_die.GetCU(), dwarf_decl_ctx); 901 } 902 } 903 } 904 905 DWARFDIE 906 DWARFDebugInfoEntry::GetParentDeclContextDIE(DWARFUnit *cu) const { 907 DWARFAttributes attributes; 908 GetAttributes(cu, attributes); 909 return GetParentDeclContextDIE(cu, attributes); 910 } 911 912 DWARFDIE 913 DWARFDebugInfoEntry::GetParentDeclContextDIE( 914 DWARFUnit *cu, const DWARFAttributes &attributes) const { 915 DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this)); 916 917 while (die) { 918 // If this is the original DIE that we are searching for a declaration for, 919 // then don't look in the cache as we don't want our own decl context to be 920 // our decl context... 921 if (die.GetDIE() != this) { 922 switch (die.Tag()) { 923 case DW_TAG_compile_unit: 924 case DW_TAG_partial_unit: 925 case DW_TAG_namespace: 926 case DW_TAG_structure_type: 927 case DW_TAG_union_type: 928 case DW_TAG_class_type: 929 return die; 930 931 default: 932 break; 933 } 934 } 935 936 DWARFDIE spec_die = attributes.FormValueAsReference(DW_AT_specification); 937 if (spec_die) { 938 DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE(); 939 if (decl_ctx_die) 940 return decl_ctx_die; 941 } 942 943 DWARFDIE abs_die = attributes.FormValueAsReference(DW_AT_abstract_origin); 944 if (abs_die) { 945 DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE(); 946 if (decl_ctx_die) 947 return decl_ctx_die; 948 } 949 950 die = die.GetParent(); 951 } 952 return DWARFDIE(); 953 } 954 955 const char *DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu, 956 std::string &storage) const { 957 DWARFAttributes attributes; 958 GetAttributes(cu, attributes); 959 return GetQualifiedName(cu, attributes, storage); 960 } 961 962 const char * 963 DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu, 964 const DWARFAttributes &attributes, 965 std::string &storage) const { 966 967 const char *name = GetName(cu); 968 969 if (name) { 970 DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu); 971 storage.clear(); 972 // TODO: change this to get the correct decl context parent.... 973 while (parent_decl_ctx_die) { 974 const dw_tag_t parent_tag = parent_decl_ctx_die.Tag(); 975 switch (parent_tag) { 976 case DW_TAG_namespace: { 977 const char *namespace_name = parent_decl_ctx_die.GetName(); 978 if (namespace_name) { 979 storage.insert(0, "::"); 980 storage.insert(0, namespace_name); 981 } else { 982 storage.insert(0, "(anonymous namespace)::"); 983 } 984 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE(); 985 } break; 986 987 case DW_TAG_class_type: 988 case DW_TAG_structure_type: 989 case DW_TAG_union_type: { 990 const char *class_union_struct_name = parent_decl_ctx_die.GetName(); 991 992 if (class_union_struct_name) { 993 storage.insert(0, "::"); 994 storage.insert(0, class_union_struct_name); 995 } 996 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE(); 997 } break; 998 999 default: 1000 parent_decl_ctx_die.Clear(); 1001 break; 1002 } 1003 } 1004 1005 if (storage.empty()) 1006 storage.append("::"); 1007 1008 storage.append(name); 1009 } 1010 if (storage.empty()) 1011 return nullptr; 1012 return storage.c_str(); 1013 } 1014 1015 bool DWARFDebugInfoEntry::LookupAddress(const dw_addr_t address, DWARFUnit *cu, 1016 DWARFDebugInfoEntry **function_die, 1017 DWARFDebugInfoEntry **block_die) { 1018 bool found_address = false; 1019 if (m_tag) { 1020 bool check_children = false; 1021 bool match_addr_range = false; 1022 // printf("0x%8.8x: %30s: address = 0x%8.8x - ", m_offset, 1023 // DW_TAG_value_to_name(tag), address); 1024 switch (m_tag) { 1025 case DW_TAG_array_type: 1026 break; 1027 case DW_TAG_class_type: 1028 check_children = true; 1029 break; 1030 case DW_TAG_entry_point: 1031 case DW_TAG_enumeration_type: 1032 case DW_TAG_formal_parameter: 1033 case DW_TAG_imported_declaration: 1034 case DW_TAG_label: 1035 break; 1036 case DW_TAG_lexical_block: 1037 check_children = true; 1038 match_addr_range = true; 1039 break; 1040 case DW_TAG_member: 1041 case DW_TAG_pointer_type: 1042 case DW_TAG_reference_type: 1043 break; 1044 case DW_TAG_compile_unit: 1045 match_addr_range = true; 1046 break; 1047 case DW_TAG_string_type: 1048 break; 1049 case DW_TAG_structure_type: 1050 check_children = true; 1051 break; 1052 case DW_TAG_subroutine_type: 1053 case DW_TAG_typedef: 1054 case DW_TAG_union_type: 1055 case DW_TAG_unspecified_parameters: 1056 case DW_TAG_variant: 1057 break; 1058 case DW_TAG_common_block: 1059 check_children = true; 1060 break; 1061 case DW_TAG_common_inclusion: 1062 case DW_TAG_inheritance: 1063 break; 1064 case DW_TAG_inlined_subroutine: 1065 check_children = true; 1066 match_addr_range = true; 1067 break; 1068 case DW_TAG_module: 1069 match_addr_range = true; 1070 break; 1071 case DW_TAG_ptr_to_member_type: 1072 case DW_TAG_set_type: 1073 case DW_TAG_subrange_type: 1074 case DW_TAG_with_stmt: 1075 case DW_TAG_access_declaration: 1076 case DW_TAG_base_type: 1077 break; 1078 case DW_TAG_catch_block: 1079 match_addr_range = true; 1080 break; 1081 case DW_TAG_const_type: 1082 case DW_TAG_constant: 1083 case DW_TAG_enumerator: 1084 case DW_TAG_file_type: 1085 case DW_TAG_friend: 1086 case DW_TAG_namelist: 1087 case DW_TAG_namelist_item: 1088 case DW_TAG_packed_type: 1089 break; 1090 case DW_TAG_subprogram: 1091 match_addr_range = true; 1092 break; 1093 case DW_TAG_template_type_parameter: 1094 case DW_TAG_template_value_parameter: 1095 case DW_TAG_GNU_template_parameter_pack: 1096 case DW_TAG_thrown_type: 1097 break; 1098 case DW_TAG_try_block: 1099 match_addr_range = true; 1100 break; 1101 case DW_TAG_variant_part: 1102 case DW_TAG_variable: 1103 case DW_TAG_volatile_type: 1104 case DW_TAG_dwarf_procedure: 1105 case DW_TAG_restrict_type: 1106 case DW_TAG_interface_type: 1107 break; 1108 case DW_TAG_namespace: 1109 check_children = true; 1110 break; 1111 case DW_TAG_imported_module: 1112 case DW_TAG_unspecified_type: 1113 break; 1114 case DW_TAG_partial_unit: 1115 match_addr_range = true; 1116 break; 1117 case DW_TAG_imported_unit: 1118 case DW_TAG_shared_type: 1119 default: 1120 break; 1121 } 1122 1123 if (match_addr_range) { 1124 dw_addr_t lo_pc = 1125 GetAttributeValueAsAddress(cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS); 1126 if (lo_pc != LLDB_INVALID_ADDRESS) { 1127 dw_addr_t hi_pc = GetAttributeHighPC(cu, lo_pc, LLDB_INVALID_ADDRESS); 1128 if (hi_pc != LLDB_INVALID_ADDRESS) { 1129 // printf("\n0x%8.8x: %30s: address = 0x%8.8x [0x%8.8x - 0x%8.8x) ", 1130 // m_offset, DW_TAG_value_to_name(tag), address, lo_pc, hi_pc); 1131 if ((lo_pc <= address) && (address < hi_pc)) { 1132 found_address = true; 1133 // puts("***MATCH***"); 1134 switch (m_tag) { 1135 case DW_TAG_compile_unit: // File 1136 case DW_TAG_partial_unit: // File 1137 check_children = 1138 ((function_die != nullptr) || (block_die != nullptr)); 1139 break; 1140 1141 case DW_TAG_subprogram: // Function 1142 if (function_die) 1143 *function_die = this; 1144 check_children = (block_die != nullptr); 1145 break; 1146 1147 case DW_TAG_inlined_subroutine: // Inlined Function 1148 case DW_TAG_lexical_block: // Block { } in code 1149 if (block_die) { 1150 *block_die = this; 1151 check_children = true; 1152 } 1153 break; 1154 1155 default: 1156 check_children = true; 1157 break; 1158 } 1159 } 1160 } else { 1161 // Compile units may not have a valid high/low pc when there 1162 // are address gaps in subroutines so we must always search 1163 // if there is no valid high and low PC. 1164 check_children = 1165 (m_tag == DW_TAG_compile_unit || m_tag == DW_TAG_partial_unit) && 1166 ((function_die != nullptr) || (block_die != nullptr)); 1167 } 1168 } else { 1169 DWARFRangeList ranges; 1170 if (GetAttributeAddressRanges(cu, ranges, /*check_hi_lo_pc*/ false) && 1171 ranges.FindEntryThatContains(address)) { 1172 found_address = true; 1173 // puts("***MATCH***"); 1174 switch (m_tag) { 1175 case DW_TAG_compile_unit: // File 1176 case DW_TAG_partial_unit: // File 1177 check_children = 1178 ((function_die != nullptr) || (block_die != nullptr)); 1179 break; 1180 1181 case DW_TAG_subprogram: // Function 1182 if (function_die) 1183 *function_die = this; 1184 check_children = (block_die != nullptr); 1185 break; 1186 1187 case DW_TAG_inlined_subroutine: // Inlined Function 1188 case DW_TAG_lexical_block: // Block { } in code 1189 if (block_die) { 1190 *block_die = this; 1191 check_children = true; 1192 } 1193 break; 1194 1195 default: 1196 check_children = true; 1197 break; 1198 } 1199 } else { 1200 check_children = false; 1201 } 1202 } 1203 } 1204 1205 if (check_children) { 1206 // printf("checking children\n"); 1207 DWARFDebugInfoEntry *child = GetFirstChild(); 1208 while (child) { 1209 if (child->LookupAddress(address, cu, function_die, block_die)) 1210 return true; 1211 child = child->GetSibling(); 1212 } 1213 } 1214 } 1215 return found_address; 1216 } 1217 1218 lldb::offset_t DWARFDebugInfoEntry::GetFirstAttributeOffset() const { 1219 return GetOffset() + llvm::getULEB128Size(m_abbr_idx); 1220 } 1221 1222 const DWARFAbbreviationDeclaration * 1223 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const { 1224 if (cu) { 1225 const DWARFAbbreviationDeclarationSet *abbrev_set = cu->GetAbbreviations(); 1226 if (abbrev_set) 1227 return abbrev_set->GetAbbreviationDeclaration(m_abbr_idx); 1228 } 1229 return nullptr; 1230 } 1231 1232 bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const { 1233 return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx && 1234 m_sibling_idx == rhs.m_sibling_idx && 1235 m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children && 1236 m_tag == rhs.m_tag; 1237 } 1238 1239 bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const { 1240 return !(*this == rhs); 1241 } 1242