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