1 //===-- DWARFCompileUnit.cpp ------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "DWARFCompileUnit.h" 11 12 #include "lldb/Core/Mangled.h" 13 #include "lldb/Core/Module.h" 14 #include "lldb/Core/Stream.h" 15 #include "lldb/Core/Timer.h" 16 #include "lldb/Host/StringConvert.h" 17 #include "lldb/Symbol/CompileUnit.h" 18 #include "lldb/Symbol/LineTable.h" 19 #include "lldb/Symbol/ObjectFile.h" 20 #include "lldb/Target/ObjCLanguageRuntime.h" 21 22 #include "DWARFDebugAbbrev.h" 23 #include "DWARFDebugAranges.h" 24 #include "DWARFDebugInfo.h" 25 #include "DWARFDIECollection.h" 26 #include "DWARFFormValue.h" 27 #include "LogChannelDWARF.h" 28 #include "NameToDIE.h" 29 #include "SymbolFileDWARF.h" 30 #include "SymbolFileDWARFDebugMap.h" 31 32 using namespace lldb; 33 using namespace lldb_private; 34 using namespace std; 35 36 37 extern int g_verbose; 38 39 DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF* dwarf2Data) : 40 m_dwarf2Data (dwarf2Data), 41 m_abbrevs (NULL), 42 m_user_data (NULL), 43 m_die_array (), 44 m_func_aranges_ap (), 45 m_base_addr (0), 46 m_offset (DW_INVALID_OFFSET), 47 m_length (0), 48 m_version (0), 49 m_addr_size (DWARFCompileUnit::GetDefaultAddressSize()), 50 m_producer (eProducerInvalid), 51 m_producer_version_major (0), 52 m_producer_version_minor (0), 53 m_producer_version_update (0), 54 m_language_type (eLanguageTypeUnknown), 55 m_is_dwarf64 (false), 56 m_is_optimized (eLazyBoolCalculate) 57 { 58 } 59 60 void 61 DWARFCompileUnit::Clear() 62 { 63 m_offset = DW_INVALID_OFFSET; 64 m_length = 0; 65 m_version = 0; 66 m_abbrevs = NULL; 67 m_addr_size = DWARFCompileUnit::GetDefaultAddressSize(); 68 m_base_addr = 0; 69 m_die_array.clear(); 70 m_func_aranges_ap.reset(); 71 m_user_data = NULL; 72 m_producer = eProducerInvalid; 73 m_language_type = eLanguageTypeUnknown; 74 m_is_dwarf64 = false; 75 m_is_optimized = eLazyBoolCalculate; 76 } 77 78 bool 79 DWARFCompileUnit::Extract(const DWARFDataExtractor &debug_info, lldb::offset_t *offset_ptr) 80 { 81 Clear(); 82 83 m_offset = *offset_ptr; 84 85 if (debug_info.ValidOffset(*offset_ptr)) 86 { 87 dw_offset_t abbr_offset; 88 const DWARFDebugAbbrev *abbr = m_dwarf2Data->DebugAbbrev(); 89 m_length = debug_info.GetDWARFInitialLength(offset_ptr); 90 m_is_dwarf64 = debug_info.IsDWARF64(); 91 m_version = debug_info.GetU16(offset_ptr); 92 abbr_offset = debug_info.GetDWARFOffset(offset_ptr); 93 m_addr_size = debug_info.GetU8 (offset_ptr); 94 95 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1); 96 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version); 97 bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset); 98 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8)); 99 100 if (length_OK && version_OK && addr_size_OK && abbr_offset_OK && abbr != NULL) 101 { 102 m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset); 103 return true; 104 } 105 106 // reset the offset to where we tried to parse from if anything went wrong 107 *offset_ptr = m_offset; 108 } 109 110 return false; 111 } 112 113 114 void 115 DWARFCompileUnit::ClearDIEs(bool keep_compile_unit_die) 116 { 117 if (m_die_array.size() > 1) 118 { 119 // std::vectors never get any smaller when resized to a smaller size, 120 // or when clear() or erase() are called, the size will report that it 121 // is smaller, but the memory allocated remains intact (call capacity() 122 // to see this). So we need to create a temporary vector and swap the 123 // contents which will cause just the internal pointers to be swapped 124 // so that when "tmp_array" goes out of scope, it will destroy the 125 // contents. 126 127 // Save at least the compile unit DIE 128 DWARFDebugInfoEntry::collection tmp_array; 129 m_die_array.swap(tmp_array); 130 if (keep_compile_unit_die) 131 m_die_array.push_back(tmp_array.front()); 132 } 133 } 134 135 //---------------------------------------------------------------------- 136 // ParseCompileUnitDIEsIfNeeded 137 // 138 // Parses a compile unit and indexes its DIEs if it hasn't already been 139 // done. 140 //---------------------------------------------------------------------- 141 size_t 142 DWARFCompileUnit::ExtractDIEsIfNeeded (bool cu_die_only) 143 { 144 const size_t initial_die_array_size = m_die_array.size(); 145 if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1) 146 return 0; // Already parsed 147 148 Timer scoped_timer (__PRETTY_FUNCTION__, 149 "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )", 150 m_offset, 151 cu_die_only); 152 153 // Set the offset to that of the first DIE and calculate the start of the 154 // next compilation unit header. 155 lldb::offset_t offset = GetFirstDIEOffset(); 156 lldb::offset_t next_cu_offset = GetNextCompileUnitOffset(); 157 158 DWARFDebugInfoEntry die; 159 // Keep a flat array of the DIE for binary lookup by DIE offset 160 if (!cu_die_only) 161 { 162 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_LOOKUPS)); 163 if (log) 164 { 165 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log, 166 "DWARFCompileUnit::ExtractDIEsIfNeeded () for compile unit at .debug_info[0x%8.8x]", 167 GetOffset()); 168 } 169 } 170 171 uint32_t depth = 0; 172 // We are in our compile unit, parse starting at the offset 173 // we were told to parse 174 const DWARFDataExtractor& debug_info_data = m_dwarf2Data->get_debug_info_data(); 175 std::vector<uint32_t> die_index_stack; 176 die_index_stack.reserve(32); 177 die_index_stack.push_back(0); 178 bool prev_die_had_children = false; 179 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize(), m_is_dwarf64); 180 while (offset < next_cu_offset && 181 die.FastExtract (debug_info_data, this, fixed_form_sizes, &offset)) 182 { 183 // if (log) 184 // log->Printf("0x%8.8x: %*.*s%s%s", 185 // die.GetOffset(), 186 // depth * 2, depth * 2, "", 187 // DW_TAG_value_to_name (die.Tag()), 188 // die.HasChildren() ? " *" : ""); 189 190 const bool null_die = die.IsNULL(); 191 if (depth == 0) 192 { 193 uint64_t base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS); 194 if (base_addr == LLDB_INVALID_ADDRESS) 195 base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_entry_pc, 0); 196 SetBaseAddress (base_addr); 197 if (initial_die_array_size == 0) 198 AddDIE (die); 199 if (cu_die_only) 200 return 1; 201 } 202 else 203 { 204 if (null_die) 205 { 206 if (prev_die_had_children) 207 { 208 // This will only happen if a DIE says is has children 209 // but all it contains is a NULL tag. Since we are removing 210 // the NULL DIEs from the list (saves up to 25% in C++ code), 211 // we need a way to let the DIE know that it actually doesn't 212 // have children. 213 if (!m_die_array.empty()) 214 m_die_array.back().SetEmptyChildren(true); 215 } 216 } 217 else 218 { 219 die.SetParentIndex(m_die_array.size() - die_index_stack[depth-1]); 220 221 if (die_index_stack.back()) 222 m_die_array[die_index_stack.back()].SetSiblingIndex(m_die_array.size()-die_index_stack.back()); 223 224 // Only push the DIE if it isn't a NULL DIE 225 m_die_array.push_back(die); 226 } 227 } 228 229 if (null_die) 230 { 231 // NULL DIE. 232 if (!die_index_stack.empty()) 233 die_index_stack.pop_back(); 234 235 if (depth > 0) 236 --depth; 237 if (depth == 0) 238 break; // We are done with this compile unit! 239 240 prev_die_had_children = false; 241 } 242 else 243 { 244 die_index_stack.back() = m_die_array.size() - 1; 245 // Normal DIE 246 const bool die_has_children = die.HasChildren(); 247 if (die_has_children) 248 { 249 die_index_stack.push_back(0); 250 ++depth; 251 } 252 prev_die_had_children = die_has_children; 253 } 254 } 255 256 // Give a little bit of info if we encounter corrupt DWARF (our offset 257 // should always terminate at or before the start of the next compilation 258 // unit header). 259 if (offset > next_cu_offset) 260 { 261 m_dwarf2Data->GetObjectFile()->GetModule()->ReportWarning ("DWARF compile unit extends beyond its bounds cu 0x%8.8x at 0x%8.8" PRIx64 "\n", 262 GetOffset(), 263 offset); 264 } 265 266 // Since std::vector objects will double their size, we really need to 267 // make a new array with the perfect size so we don't end up wasting 268 // space. So here we copy and swap to make sure we don't have any extra 269 // memory taken up. 270 271 if (m_die_array.size () < m_die_array.capacity()) 272 { 273 DWARFDebugInfoEntry::collection exact_size_die_array (m_die_array.begin(), m_die_array.end()); 274 exact_size_die_array.swap (m_die_array); 275 } 276 Log *verbose_log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_DEBUG_INFO | DWARF_LOG_VERBOSE)); 277 if (verbose_log) 278 { 279 StreamString strm; 280 Dump(&strm); 281 if (m_die_array.empty()) 282 strm.Printf("error: no DIE for compile unit"); 283 else 284 m_die_array[0].Dump(m_dwarf2Data, this, strm, UINT32_MAX); 285 verbose_log->PutCString (strm.GetString().c_str()); 286 } 287 288 return m_die_array.size(); 289 } 290 291 292 dw_offset_t 293 DWARFCompileUnit::GetAbbrevOffset() const 294 { 295 return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET; 296 } 297 298 299 300 bool 301 DWARFCompileUnit::Verify(Stream *s) const 302 { 303 const DWARFDataExtractor& debug_info = m_dwarf2Data->get_debug_info_data(); 304 bool valid_offset = debug_info.ValidOffset(m_offset); 305 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1); 306 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version); 307 bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(GetAbbrevOffset()); 308 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8)); 309 bool verbose = s->GetVerbose(); 310 if (valid_offset && length_OK && version_OK && addr_size_OK && abbr_offset_OK) 311 { 312 if (verbose) 313 s->Printf(" 0x%8.8x: OK\n", m_offset); 314 return true; 315 } 316 else 317 { 318 s->Printf(" 0x%8.8x: ", m_offset); 319 320 m_dwarf2Data->get_debug_info_data().Dump (s, m_offset, lldb::eFormatHex, 1, Size(), 32, LLDB_INVALID_ADDRESS, 0, 0); 321 s->EOL(); 322 if (valid_offset) 323 { 324 if (!length_OK) 325 s->Printf(" The length (0x%8.8x) for this compile unit is too large for the .debug_info provided.\n", m_length); 326 if (!version_OK) 327 s->Printf(" The 16 bit compile unit header version is not supported.\n"); 328 if (!abbr_offset_OK) 329 s->Printf(" The offset into the .debug_abbrev section (0x%8.8x) is not valid.\n", GetAbbrevOffset()); 330 if (!addr_size_OK) 331 s->Printf(" The address size is unsupported: 0x%2.2x\n", m_addr_size); 332 } 333 else 334 s->Printf(" The start offset of the compile unit header in the .debug_info is invalid.\n"); 335 } 336 return false; 337 } 338 339 340 void 341 DWARFCompileUnit::Dump(Stream *s) const 342 { 343 s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at {0x%8.8x})\n", 344 m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size, GetNextCompileUnitOffset()); 345 } 346 347 348 static uint8_t g_default_addr_size = 4; 349 350 uint8_t 351 DWARFCompileUnit::GetAddressByteSize(const DWARFCompileUnit* cu) 352 { 353 if (cu) 354 return cu->GetAddressByteSize(); 355 return DWARFCompileUnit::GetDefaultAddressSize(); 356 } 357 358 bool 359 DWARFCompileUnit::IsDWARF64(const DWARFCompileUnit* cu) 360 { 361 if (cu) 362 return cu->IsDWARF64(); 363 return false; 364 } 365 366 uint8_t 367 DWARFCompileUnit::GetDefaultAddressSize() 368 { 369 return g_default_addr_size; 370 } 371 372 void 373 DWARFCompileUnit::SetDefaultAddressSize(uint8_t addr_size) 374 { 375 g_default_addr_size = addr_size; 376 } 377 378 void 379 DWARFCompileUnit::BuildAddressRangeTable (SymbolFileDWARF* dwarf2Data, 380 DWARFDebugAranges* debug_aranges) 381 { 382 // This function is usually called if there in no .debug_aranges section 383 // in order to produce a compile unit level set of address ranges that 384 // is accurate. 385 386 // First get the compile unit DIE only and check if it has a DW_AT_ranges 387 const DWARFDebugInfoEntry* die = GetCompileUnitDIEOnly(); 388 389 const dw_offset_t cu_offset = GetOffset(); 390 if (die) 391 { 392 DWARFDebugRanges::RangeList ranges; 393 const size_t num_ranges = die->GetAttributeAddressRanges(dwarf2Data, this, ranges, false); 394 if (num_ranges > 0) 395 { 396 // This compile unit has DW_AT_ranges, assume this is correct if it 397 // is present since clang no longer makes .debug_aranges by default 398 // and it emits DW_AT_ranges for DW_TAG_compile_units. GCC also does 399 // this with recent GCC builds. 400 for (size_t i=0; i<num_ranges; ++i) 401 { 402 const DWARFDebugRanges::RangeList::Entry &range = ranges.GetEntryRef(i); 403 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(), range.GetRangeEnd()); 404 } 405 406 return; // We got all of our ranges from the DW_AT_ranges attribute 407 } 408 } 409 // We don't have a DW_AT_ranges attribute, so we need to parse the DWARF 410 411 // If the DIEs weren't parsed, then we don't want all dies for all compile units 412 // to stay loaded when they weren't needed. So we can end up parsing the DWARF 413 // and then throwing them all away to keep memory usage down. 414 const bool clear_dies = ExtractDIEsIfNeeded (false) > 1; 415 416 die = DIE(); 417 if (die) 418 die->BuildAddressRangeTable(dwarf2Data, this, debug_aranges); 419 420 if (debug_aranges->IsEmpty()) 421 { 422 // We got nothing from the functions, maybe we have a line tables only 423 // situation. Check the line tables and build the arange table from this. 424 SymbolContext sc; 425 sc.comp_unit = dwarf2Data->GetCompUnitForDWARFCompUnit(this); 426 if (sc.comp_unit) 427 { 428 SymbolFileDWARFDebugMap *debug_map_sym_file = m_dwarf2Data->GetDebugMapSymfile(); 429 if (debug_map_sym_file == NULL) 430 { 431 LineTable *line_table = sc.comp_unit->GetLineTable(); 432 433 if (line_table) 434 { 435 LineTable::FileAddressRanges file_ranges; 436 const bool append = true; 437 const size_t num_ranges = line_table->GetContiguousFileAddressRanges (file_ranges, append); 438 for (uint32_t idx=0; idx<num_ranges; ++idx) 439 { 440 const LineTable::FileAddressRanges::Entry &range = file_ranges.GetEntryRef(idx); 441 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(), range.GetRangeEnd()); 442 printf ("0x%8.8x: [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n", GetOffset(), range.GetRangeBase(), range.GetRangeEnd()); 443 } 444 } 445 } 446 else 447 debug_map_sym_file->AddOSOARanges(dwarf2Data,debug_aranges); 448 } 449 } 450 451 if (debug_aranges->IsEmpty()) 452 { 453 // We got nothing from the functions, maybe we have a line tables only 454 // situation. Check the line tables and build the arange table from this. 455 SymbolContext sc; 456 sc.comp_unit = dwarf2Data->GetCompUnitForDWARFCompUnit(this); 457 if (sc.comp_unit) 458 { 459 LineTable *line_table = sc.comp_unit->GetLineTable(); 460 461 if (line_table) 462 { 463 LineTable::FileAddressRanges file_ranges; 464 const bool append = true; 465 const size_t num_ranges = line_table->GetContiguousFileAddressRanges (file_ranges, append); 466 for (uint32_t idx=0; idx<num_ranges; ++idx) 467 { 468 const LineTable::FileAddressRanges::Entry &range = file_ranges.GetEntryRef(idx); 469 debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(), range.GetRangeEnd()); 470 } 471 } 472 } 473 } 474 475 // Keep memory down by clearing DIEs if this generate function 476 // caused them to be parsed 477 if (clear_dies) 478 ClearDIEs (true); 479 480 } 481 482 483 const DWARFDebugAranges & 484 DWARFCompileUnit::GetFunctionAranges () 485 { 486 if (m_func_aranges_ap.get() == NULL) 487 { 488 m_func_aranges_ap.reset (new DWARFDebugAranges()); 489 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES)); 490 491 if (log) 492 { 493 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log, 494 "DWARFCompileUnit::GetFunctionAranges() for compile unit at .debug_info[0x%8.8x]", 495 GetOffset()); 496 } 497 const DWARFDebugInfoEntry* die = DIE(); 498 if (die) 499 die->BuildFunctionAddressRangeTable (m_dwarf2Data, this, m_func_aranges_ap.get()); 500 const bool minimize = false; 501 m_func_aranges_ap->Sort(minimize); 502 } 503 return *m_func_aranges_ap.get(); 504 } 505 506 bool 507 DWARFCompileUnit::LookupAddress 508 ( 509 const dw_addr_t address, 510 DWARFDebugInfoEntry** function_die_handle, 511 DWARFDebugInfoEntry** block_die_handle 512 ) 513 { 514 bool success = false; 515 516 if (function_die_handle != NULL && DIE()) 517 { 518 519 const DWARFDebugAranges &func_aranges = GetFunctionAranges (); 520 521 // Re-check the aranges auto pointer contents in case it was created above 522 if (!func_aranges.IsEmpty()) 523 { 524 *function_die_handle = GetDIEPtr(func_aranges.FindAddress(address)); 525 if (*function_die_handle != NULL) 526 { 527 success = true; 528 if (block_die_handle != NULL) 529 { 530 DWARFDebugInfoEntry* child = (*function_die_handle)->GetFirstChild(); 531 while (child) 532 { 533 if (child->LookupAddress(address, m_dwarf2Data, this, NULL, block_die_handle)) 534 break; 535 child = child->GetSibling(); 536 } 537 } 538 } 539 } 540 } 541 return success; 542 } 543 544 //---------------------------------------------------------------------- 545 // Compare function DWARFDebugAranges::Range structures 546 //---------------------------------------------------------------------- 547 static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2) 548 { 549 return die1.GetOffset() < die2.GetOffset(); 550 } 551 552 //---------------------------------------------------------------------- 553 // GetDIEPtr() 554 // 555 // Get the DIE (Debug Information Entry) with the specified offset. 556 //---------------------------------------------------------------------- 557 DWARFDebugInfoEntry* 558 DWARFCompileUnit::GetDIEPtr(dw_offset_t die_offset) 559 { 560 if (die_offset != DW_INVALID_OFFSET) 561 { 562 ExtractDIEsIfNeeded (false); 563 DWARFDebugInfoEntry compare_die; 564 compare_die.SetOffset(die_offset); 565 DWARFDebugInfoEntry::iterator end = m_die_array.end(); 566 DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset); 567 if (pos != end) 568 { 569 if (die_offset == (*pos).GetOffset()) 570 return &(*pos); 571 } 572 } 573 return NULL; // Not found in any compile units 574 } 575 576 //---------------------------------------------------------------------- 577 // GetDIEPtrContainingOffset() 578 // 579 // Get the DIE (Debug Information Entry) that contains the specified 580 // .debug_info offset. 581 //---------------------------------------------------------------------- 582 const DWARFDebugInfoEntry* 583 DWARFCompileUnit::GetDIEPtrContainingOffset(dw_offset_t die_offset) 584 { 585 if (die_offset != DW_INVALID_OFFSET) 586 { 587 ExtractDIEsIfNeeded (false); 588 DWARFDebugInfoEntry compare_die; 589 compare_die.SetOffset(die_offset); 590 DWARFDebugInfoEntry::iterator end = m_die_array.end(); 591 DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset); 592 if (pos != end) 593 { 594 if (die_offset >= (*pos).GetOffset()) 595 { 596 DWARFDebugInfoEntry::iterator next = pos + 1; 597 if (next != end) 598 { 599 if (die_offset < (*next).GetOffset()) 600 return &(*pos); 601 } 602 } 603 } 604 } 605 return NULL; // Not found in any compile units 606 } 607 608 609 610 size_t 611 DWARFCompileUnit::AppendDIEsWithTag (const dw_tag_t tag, DWARFDIECollection& dies, uint32_t depth) const 612 { 613 size_t old_size = dies.Size(); 614 DWARFDebugInfoEntry::const_iterator pos; 615 DWARFDebugInfoEntry::const_iterator end = m_die_array.end(); 616 for (pos = m_die_array.begin(); pos != end; ++pos) 617 { 618 if (pos->Tag() == tag) 619 dies.Append (&(*pos)); 620 } 621 622 // Return the number of DIEs added to the collection 623 return dies.Size() - old_size; 624 } 625 626 //void 627 //DWARFCompileUnit::AddGlobalDIEByIndex (uint32_t die_idx) 628 //{ 629 // m_global_die_indexes.push_back (die_idx); 630 //} 631 // 632 // 633 //void 634 //DWARFCompileUnit::AddGlobal (const DWARFDebugInfoEntry* die) 635 //{ 636 // // Indexes to all file level global and static variables 637 // m_global_die_indexes; 638 // 639 // if (m_die_array.empty()) 640 // return; 641 // 642 // const DWARFDebugInfoEntry* first_die = &m_die_array[0]; 643 // const DWARFDebugInfoEntry* end = first_die + m_die_array.size(); 644 // if (first_die <= die && die < end) 645 // m_global_die_indexes.push_back (die - first_die); 646 //} 647 648 649 void 650 DWARFCompileUnit::Index (const uint32_t cu_idx, 651 NameToDIE& func_basenames, 652 NameToDIE& func_fullnames, 653 NameToDIE& func_methods, 654 NameToDIE& func_selectors, 655 NameToDIE& objc_class_selectors, 656 NameToDIE& globals, 657 NameToDIE& types, 658 NameToDIE& namespaces) 659 { 660 const DWARFDataExtractor* debug_str = &m_dwarf2Data->get_debug_str_data(); 661 662 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize(), m_is_dwarf64); 663 664 Log *log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_LOOKUPS)); 665 666 if (log) 667 { 668 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log, 669 "DWARFCompileUnit::Index() for compile unit at .debug_info[0x%8.8x]", 670 GetOffset()); 671 } 672 673 const LanguageType cu_language = GetLanguageType(); 674 DWARFDebugInfoEntry::const_iterator pos; 675 DWARFDebugInfoEntry::const_iterator begin = m_die_array.begin(); 676 DWARFDebugInfoEntry::const_iterator end = m_die_array.end(); 677 for (pos = begin; pos != end; ++pos) 678 { 679 const DWARFDebugInfoEntry &die = *pos; 680 681 const dw_tag_t tag = die.Tag(); 682 683 switch (tag) 684 { 685 case DW_TAG_subprogram: 686 case DW_TAG_inlined_subroutine: 687 case DW_TAG_base_type: 688 case DW_TAG_class_type: 689 case DW_TAG_constant: 690 case DW_TAG_enumeration_type: 691 case DW_TAG_string_type: 692 case DW_TAG_subroutine_type: 693 case DW_TAG_structure_type: 694 case DW_TAG_union_type: 695 case DW_TAG_typedef: 696 case DW_TAG_namespace: 697 case DW_TAG_variable: 698 case DW_TAG_unspecified_type: 699 break; 700 701 default: 702 continue; 703 } 704 705 DWARFDebugInfoEntry::Attributes attributes; 706 const char *name = NULL; 707 const char *mangled_cstr = NULL; 708 bool is_declaration = false; 709 //bool is_artificial = false; 710 bool has_address = false; 711 bool has_location = false; 712 bool is_global_or_static_variable = false; 713 714 dw_offset_t specification_die_offset = DW_INVALID_OFFSET; 715 const size_t num_attributes = die.GetAttributes(m_dwarf2Data, this, fixed_form_sizes, attributes); 716 if (num_attributes > 0) 717 { 718 for (uint32_t i=0; i<num_attributes; ++i) 719 { 720 dw_attr_t attr = attributes.AttributeAtIndex(i); 721 DWARFFormValue form_value; 722 switch (attr) 723 { 724 case DW_AT_name: 725 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) 726 name = form_value.AsCString(debug_str); 727 break; 728 729 case DW_AT_declaration: 730 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) 731 is_declaration = form_value.Unsigned() != 0; 732 break; 733 734 // case DW_AT_artificial: 735 // if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) 736 // is_artificial = form_value.Unsigned() != 0; 737 // break; 738 739 case DW_AT_MIPS_linkage_name: 740 case DW_AT_linkage_name: 741 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) 742 mangled_cstr = form_value.AsCString(debug_str); 743 break; 744 745 case DW_AT_low_pc: 746 case DW_AT_high_pc: 747 case DW_AT_ranges: 748 has_address = true; 749 break; 750 751 case DW_AT_entry_pc: 752 has_address = true; 753 break; 754 755 case DW_AT_location: 756 has_location = true; 757 if (tag == DW_TAG_variable) 758 { 759 const DWARFDebugInfoEntry* parent_die = die.GetParent(); 760 while ( parent_die != NULL ) 761 { 762 switch (parent_die->Tag()) 763 { 764 case DW_TAG_subprogram: 765 case DW_TAG_lexical_block: 766 case DW_TAG_inlined_subroutine: 767 // Even if this is a function level static, we don't add it. We could theoretically 768 // add these if we wanted to by introspecting into the DW_AT_location and seeing 769 // if the location describes a hard coded address, but we dont want the performance 770 // penalty of that right now. 771 is_global_or_static_variable = false; 772 // if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value)) 773 // { 774 // // If we have valid block data, then we have location expression bytes 775 // // that are fixed (not a location list). 776 // const uint8_t *block_data = form_value.BlockData(); 777 // if (block_data) 778 // { 779 // uint32_t block_length = form_value.Unsigned(); 780 // if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize()) 781 // { 782 // if (block_data[0] == DW_OP_addr) 783 // add_die = true; 784 // } 785 // } 786 // } 787 parent_die = NULL; // Terminate the while loop. 788 break; 789 790 case DW_TAG_compile_unit: 791 is_global_or_static_variable = true; 792 parent_die = NULL; // Terminate the while loop. 793 break; 794 795 default: 796 parent_die = parent_die->GetParent(); // Keep going in the while loop. 797 break; 798 } 799 } 800 } 801 break; 802 803 case DW_AT_specification: 804 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) 805 specification_die_offset = form_value.Reference(); 806 break; 807 } 808 } 809 } 810 811 switch (tag) 812 { 813 case DW_TAG_subprogram: 814 if (has_address) 815 { 816 if (name) 817 { 818 // Note, this check is also done in ParseMethodName, but since this is a hot loop, we do the 819 // simple inlined check outside the call. 820 ObjCLanguageRuntime::MethodName objc_method(name, true); 821 if (objc_method.IsValid(true)) 822 { 823 ConstString objc_class_name_with_category (objc_method.GetClassNameWithCategory()); 824 ConstString objc_selector_name (objc_method.GetSelector()); 825 ConstString objc_fullname_no_category_name (objc_method.GetFullNameWithoutCategory(true)); 826 ConstString objc_class_name_no_category (objc_method.GetClassName()); 827 func_fullnames.Insert (ConstString(name), die.GetOffset()); 828 if (objc_class_name_with_category) 829 objc_class_selectors.Insert(objc_class_name_with_category, die.GetOffset()); 830 if (objc_class_name_no_category && objc_class_name_no_category != objc_class_name_with_category) 831 objc_class_selectors.Insert(objc_class_name_no_category, die.GetOffset()); 832 if (objc_selector_name) 833 func_selectors.Insert (objc_selector_name, die.GetOffset()); 834 if (objc_fullname_no_category_name) 835 func_fullnames.Insert (objc_fullname_no_category_name, die.GetOffset()); 836 } 837 // If we have a mangled name, then the DW_AT_name attribute 838 // is usually the method name without the class or any parameters 839 const DWARFDebugInfoEntry *parent = die.GetParent(); 840 bool is_method = false; 841 if (parent) 842 { 843 dw_tag_t parent_tag = parent->Tag(); 844 if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type) 845 { 846 is_method = true; 847 } 848 else 849 { 850 if (specification_die_offset != DW_INVALID_OFFSET) 851 { 852 const DWARFDebugInfoEntry *specification_die = m_dwarf2Data->DebugInfo()->GetDIEPtr (specification_die_offset, NULL); 853 if (specification_die) 854 { 855 parent = specification_die->GetParent(); 856 if (parent) 857 { 858 parent_tag = parent->Tag(); 859 860 if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type) 861 is_method = true; 862 } 863 } 864 } 865 } 866 } 867 868 869 if (is_method) 870 func_methods.Insert (ConstString(name), die.GetOffset()); 871 else 872 func_basenames.Insert (ConstString(name), die.GetOffset()); 873 874 if (!is_method && !mangled_cstr && !objc_method.IsValid(true)) 875 func_fullnames.Insert (ConstString(name), die.GetOffset()); 876 } 877 if (mangled_cstr) 878 { 879 // Make sure our mangled name isn't the same string table entry 880 // as our name. If it starts with '_', then it is ok, else compare 881 // the string to make sure it isn't the same and we don't end up 882 // with duplicate entries 883 if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (name && ::strcmp(name, mangled_cstr) != 0))) 884 { 885 Mangled mangled (ConstString(mangled_cstr), true); 886 func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset()); 887 ConstString demangled = mangled.GetDemangledName(cu_language); 888 if (demangled) 889 func_fullnames.Insert (demangled, die.GetOffset()); 890 } 891 } 892 } 893 break; 894 895 case DW_TAG_inlined_subroutine: 896 if (has_address) 897 { 898 if (name) 899 func_basenames.Insert (ConstString(name), die.GetOffset()); 900 if (mangled_cstr) 901 { 902 // Make sure our mangled name isn't the same string table entry 903 // as our name. If it starts with '_', then it is ok, else compare 904 // the string to make sure it isn't the same and we don't end up 905 // with duplicate entries 906 if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) 907 { 908 Mangled mangled (ConstString(mangled_cstr), true); 909 func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset()); 910 ConstString demangled = mangled.GetDemangledName(cu_language); 911 if (demangled) 912 func_fullnames.Insert (demangled, die.GetOffset()); 913 } 914 } 915 else 916 func_fullnames.Insert (ConstString(name), die.GetOffset()); 917 } 918 break; 919 920 case DW_TAG_base_type: 921 case DW_TAG_class_type: 922 case DW_TAG_constant: 923 case DW_TAG_enumeration_type: 924 case DW_TAG_string_type: 925 case DW_TAG_subroutine_type: 926 case DW_TAG_structure_type: 927 case DW_TAG_union_type: 928 case DW_TAG_typedef: 929 case DW_TAG_unspecified_type: 930 if (name && is_declaration == false) 931 { 932 types.Insert (ConstString(name), die.GetOffset()); 933 } 934 break; 935 936 case DW_TAG_namespace: 937 if (name) 938 namespaces.Insert (ConstString(name), die.GetOffset()); 939 break; 940 941 case DW_TAG_variable: 942 if (name && has_location && is_global_or_static_variable) 943 { 944 globals.Insert (ConstString(name), die.GetOffset()); 945 // Be sure to include variables by their mangled and demangled 946 // names if they have any since a variable can have a basename 947 // "i", a mangled named "_ZN12_GLOBAL__N_11iE" and a demangled 948 // mangled name "(anonymous namespace)::i"... 949 950 // Make sure our mangled name isn't the same string table entry 951 // as our name. If it starts with '_', then it is ok, else compare 952 // the string to make sure it isn't the same and we don't end up 953 // with duplicate entries 954 if (mangled_cstr && name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) 955 { 956 Mangled mangled (ConstString(mangled_cstr), true); 957 globals.Insert (mangled.GetMangledName(), die.GetOffset()); 958 ConstString demangled = mangled.GetDemangledName(cu_language); 959 if (demangled) 960 globals.Insert (demangled, die.GetOffset()); 961 } 962 } 963 break; 964 965 default: 966 continue; 967 } 968 } 969 } 970 971 bool 972 DWARFCompileUnit::Supports_unnamed_objc_bitfields () 973 { 974 if (GetProducer() == eProducerClang) 975 { 976 const uint32_t major_version = GetProducerVersionMajor(); 977 if (major_version > 425 || (major_version == 425 && GetProducerVersionUpdate() >= 13)) 978 return true; 979 else 980 return false; 981 } 982 return true; // Assume all other compilers didn't have incorrect ObjC bitfield info 983 } 984 985 bool 986 DWARFCompileUnit::Supports_DW_AT_APPLE_objc_complete_type () 987 { 988 if (GetProducer() == eProducerLLVMGCC) 989 return false; 990 return true; 991 } 992 993 bool 994 DWARFCompileUnit::DW_AT_decl_file_attributes_are_invalid() 995 { 996 // llvm-gcc makes completely invalid decl file attributes and won't ever 997 // be fixed, so we need to know to ignore these. 998 return GetProducer() == eProducerLLVMGCC; 999 } 1000 1001 void 1002 DWARFCompileUnit::ParseProducerInfo () 1003 { 1004 m_producer_version_major = UINT32_MAX; 1005 m_producer_version_minor = UINT32_MAX; 1006 m_producer_version_update = UINT32_MAX; 1007 1008 const DWARFDebugInfoEntry *die = GetCompileUnitDIEOnly(); 1009 if (die) 1010 { 1011 1012 const char *producer_cstr = die->GetAttributeValueAsString(m_dwarf2Data, this, DW_AT_producer, NULL); 1013 if (producer_cstr) 1014 { 1015 RegularExpression llvm_gcc_regex("^4\\.[012]\\.[01] \\(Based on Apple Inc\\. build [0-9]+\\) \\(LLVM build [\\.0-9]+\\)$"); 1016 if (llvm_gcc_regex.Execute (producer_cstr)) 1017 { 1018 m_producer = eProducerLLVMGCC; 1019 } 1020 else if (strstr(producer_cstr, "clang")) 1021 { 1022 static RegularExpression g_clang_version_regex("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)"); 1023 RegularExpression::Match regex_match(3); 1024 if (g_clang_version_regex.Execute (producer_cstr, ®ex_match)) 1025 { 1026 std::string str; 1027 if (regex_match.GetMatchAtIndex (producer_cstr, 1, str)) 1028 m_producer_version_major = StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10); 1029 if (regex_match.GetMatchAtIndex (producer_cstr, 2, str)) 1030 m_producer_version_minor = StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10); 1031 if (regex_match.GetMatchAtIndex (producer_cstr, 3, str)) 1032 m_producer_version_update = StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10); 1033 } 1034 m_producer = eProducerClang; 1035 } 1036 else if (strstr(producer_cstr, "GNU")) 1037 m_producer = eProducerGCC; 1038 } 1039 } 1040 if (m_producer == eProducerInvalid) 1041 m_producer = eProcucerOther; 1042 } 1043 1044 DWARFCompileUnit::Producer 1045 DWARFCompileUnit::GetProducer () 1046 { 1047 if (m_producer == eProducerInvalid) 1048 ParseProducerInfo (); 1049 return m_producer; 1050 } 1051 1052 1053 uint32_t 1054 DWARFCompileUnit::GetProducerVersionMajor() 1055 { 1056 if (m_producer_version_major == 0) 1057 ParseProducerInfo (); 1058 return m_producer_version_major; 1059 } 1060 1061 uint32_t 1062 DWARFCompileUnit::GetProducerVersionMinor() 1063 { 1064 if (m_producer_version_minor == 0) 1065 ParseProducerInfo (); 1066 return m_producer_version_minor; 1067 } 1068 1069 uint32_t 1070 DWARFCompileUnit::GetProducerVersionUpdate() 1071 { 1072 if (m_producer_version_update == 0) 1073 ParseProducerInfo (); 1074 return m_producer_version_update; 1075 } 1076 1077 LanguageType 1078 DWARFCompileUnit::LanguageTypeFromDWARF(uint64_t val) 1079 { 1080 // Note: user languages between lo_user and hi_user 1081 // must be handled explicitly here. 1082 switch (val) 1083 { 1084 case DW_LANG_Mips_Assembler: 1085 return eLanguageTypeMipsAssembler; 1086 case 0x8e57: // FIXME: needs to be added to llvm 1087 return eLanguageTypeExtRenderScript; 1088 default: 1089 return static_cast<LanguageType>(val); 1090 } 1091 } 1092 1093 LanguageType 1094 DWARFCompileUnit::GetLanguageType() 1095 { 1096 if (m_language_type != eLanguageTypeUnknown) 1097 return m_language_type; 1098 1099 const DWARFDebugInfoEntry *die = GetCompileUnitDIEOnly(); 1100 if (die) 1101 m_language_type = LanguageTypeFromDWARF( 1102 die->GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_language, 0)); 1103 return m_language_type; 1104 } 1105 1106 bool 1107 DWARFCompileUnit::IsDWARF64() const 1108 { 1109 return m_is_dwarf64; 1110 } 1111 1112 bool 1113 DWARFCompileUnit::GetIsOptimized () 1114 { 1115 if (m_is_optimized == eLazyBoolCalculate) 1116 { 1117 const DWARFDebugInfoEntry *die = GetCompileUnitDIEOnly(); 1118 if (die) 1119 { 1120 m_is_optimized = eLazyBoolNo; 1121 if (die->GetAttributeValueAsUnsigned (m_dwarf2Data, this, DW_AT_APPLE_optimized, 0) == 1) 1122 { 1123 m_is_optimized = eLazyBoolYes; 1124 } 1125 } 1126 } 1127 if (m_is_optimized == eLazyBoolYes) 1128 { 1129 return true; 1130 } 1131 else 1132 { 1133 return false; 1134 } 1135 } 1136