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