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 "lldb/Core/Module.h"
16 #include "lldb/Expression/DWARFExpression.h"
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Utility/Stream.h"
19 
20 #include "DWARFUnit.h"
21 #include "DWARFDebugAbbrev.h"
22 #include "DWARFDebugAranges.h"
23 #include "DWARFDebugInfo.h"
24 #include "DWARFDebugRanges.h"
25 #include "DWARFDeclContext.h"
26 #include "DWARFFormValue.h"
27 #include "SymbolFileDWARF.h"
28 #include "SymbolFileDWARFDwo.h"
29 
30 using namespace lldb_private;
31 using namespace std;
32 extern int g_verbose;
33 
34 bool DWARFDebugInfoEntry::FastExtract(
35     const DWARFDataExtractor &debug_info_data, const DWARFUnit *cu,
36     const DWARFFormValue::FixedFormSizes &fixed_form_sizes,
37     lldb::offset_t *offset_ptr) {
38   m_offset = *offset_ptr;
39   m_parent_idx = 0;
40   m_sibling_idx = 0;
41   const uint64_t abbr_idx = debug_info_data.GetULEB128(offset_ptr);
42   lldbassert(abbr_idx <= UINT16_MAX);
43   m_abbr_idx = abbr_idx;
44 
45   // assert (fixed_form_sizes);  // For best performance this should be
46   // specified!
47 
48   if (m_abbr_idx) {
49     lldb::offset_t offset = *offset_ptr;
50 
51     const DWARFAbbreviationDeclaration *abbrevDecl =
52         cu->GetAbbreviations()->GetAbbreviationDeclaration(m_abbr_idx);
53 
54     if (abbrevDecl == NULL) {
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 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 
72       const uint8_t fixed_skip_size = fixed_form_sizes.GetSize(form);
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
83           case DW_FORM_exprloc:
84           case DW_FORM_block:
85             form_size = debug_info_data.GetULEB128(&offset);
86             break;
87           case DW_FORM_block1:
88             form_size = debug_info_data.GetU8_unchecked(&offset);
89             break;
90           case DW_FORM_block2:
91             form_size = debug_info_data.GetU16_unchecked(&offset);
92             break;
93           case DW_FORM_block4:
94             form_size = debug_info_data.GetU32_unchecked(&offset);
95             break;
96 
97           // Inlined NULL terminated C-strings
98           case DW_FORM_string:
99             debug_info_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             debug_info_data.Skip_LEB128(&offset);
166             break;
167 
168           case DW_FORM_indirect:
169             form_is_indirect = true;
170             form = debug_info_data.GetULEB128(&offset);
171             break;
172 
173           case DW_FORM_strp:
174           case DW_FORM_sec_offset:
175             debug_info_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 // Extract
203 //
204 // Extract a debug info entry for a given compile unit from the .debug_info and
205 // .debug_abbrev data within the SymbolFileDWARF class starting at the given
206 // offset
207 bool DWARFDebugInfoEntry::Extract(const DWARFUnit *cu,
208                                   lldb::offset_t *offset_ptr) {
209   const DWARFDataExtractor &debug_info_data = cu->GetData();
210   //    const DWARFDataExtractor& debug_str_data =
211   //    dwarf2Data->get_debug_str_data();
212   const uint32_t cu_end_offset = cu->GetNextUnitOffset();
213   lldb::offset_t offset = *offset_ptr;
214   //  if (offset >= cu_end_offset)
215   //      Log::Status("DIE at offset 0x%8.8x is beyond the end of the current
216   //      compile unit (0x%8.8x)", m_offset, cu_end_offset);
217   if ((offset < cu_end_offset) && debug_info_data.ValidOffset(offset)) {
218     m_offset = offset;
219 
220     const uint64_t abbr_idx = debug_info_data.GetULEB128(&offset);
221     lldbassert(abbr_idx <= UINT16_MAX);
222     m_abbr_idx = abbr_idx;
223     if (abbr_idx) {
224       const DWARFAbbreviationDeclaration *abbrevDecl =
225           cu->GetAbbreviations()->GetAbbreviationDeclaration(abbr_idx);
226 
227       if (abbrevDecl) {
228         m_tag = abbrevDecl->Tag();
229         m_has_children = abbrevDecl->HasChildren();
230 
231         bool isCompileUnitTag = (m_tag == DW_TAG_compile_unit ||
232                                  m_tag == DW_TAG_partial_unit);
233         if (cu && isCompileUnitTag)
234           const_cast<DWARFUnit *>(cu)->SetBaseAddress(0);
235 
236         // Skip all data in the .debug_info for the attributes
237         const uint32_t numAttributes = abbrevDecl->NumAttributes();
238         for (uint32_t i = 0; i < numAttributes; ++i) {
239           DWARFFormValue form_value(cu);
240           dw_attr_t attr;
241           abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
242           dw_form_t form = form_value.Form();
243 
244           if (isCompileUnitTag &&
245               ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) {
246             if (form_value.ExtractValue(debug_info_data, &offset)) {
247               if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc)
248                 const_cast<DWARFUnit *>(cu)->SetBaseAddress(
249                     form_value.Address());
250             }
251           } else {
252             bool form_is_indirect = false;
253             do {
254               form_is_indirect = false;
255               uint32_t form_size = 0;
256               switch (form) {
257               // Blocks if inlined data that have a length field and the data
258               // bytes inlined in the .debug_info
259               case DW_FORM_exprloc:
260               case DW_FORM_block:
261                 form_size = debug_info_data.GetULEB128(&offset);
262                 break;
263               case DW_FORM_block1:
264                 form_size = debug_info_data.GetU8(&offset);
265                 break;
266               case DW_FORM_block2:
267                 form_size = debug_info_data.GetU16(&offset);
268                 break;
269               case DW_FORM_block4:
270                 form_size = debug_info_data.GetU32(&offset);
271                 break;
272 
273               // Inlined NULL terminated C-strings
274               case DW_FORM_string:
275                 debug_info_data.GetCStr(&offset);
276                 break;
277 
278               // Compile unit address sized values
279               case DW_FORM_addr:
280                 form_size = cu->GetAddressByteSize();
281                 break;
282               case DW_FORM_ref_addr:
283                 if (cu->GetVersion() <= 2)
284                   form_size = cu->GetAddressByteSize();
285                 else
286                   form_size = 4;
287                 break;
288 
289               // 0 sized form
290               case DW_FORM_flag_present:
291               case DW_FORM_implicit_const:
292                 form_size = 0;
293                 break;
294 
295               // 1 byte values
296               case DW_FORM_data1:
297               case DW_FORM_flag:
298               case DW_FORM_ref1:
299                 form_size = 1;
300                 break;
301 
302               // 2 byte values
303               case DW_FORM_data2:
304               case DW_FORM_ref2:
305                 form_size = 2;
306                 break;
307 
308               // 4 byte values
309               case DW_FORM_data4:
310               case DW_FORM_ref4:
311                 form_size = 4;
312                 break;
313 
314               // 8 byte values
315               case DW_FORM_data8:
316               case DW_FORM_ref8:
317               case DW_FORM_ref_sig8:
318                 form_size = 8;
319                 break;
320 
321               // signed or unsigned LEB 128 values
322               case DW_FORM_addrx:
323               case DW_FORM_sdata:
324               case DW_FORM_udata:
325               case DW_FORM_ref_udata:
326               case DW_FORM_GNU_addr_index:
327               case DW_FORM_GNU_str_index:
328                 debug_info_data.Skip_LEB128(&offset);
329                 break;
330 
331               case DW_FORM_indirect:
332                 form = debug_info_data.GetULEB128(&offset);
333                 form_is_indirect = true;
334                 break;
335 
336               case DW_FORM_strp:
337               case DW_FORM_sec_offset:
338                 debug_info_data.GetU32(&offset);
339                 break;
340 
341               default:
342                 *offset_ptr = offset;
343                 return false;
344               }
345 
346               offset += form_size;
347             } while (form_is_indirect);
348           }
349         }
350         *offset_ptr = offset;
351         return true;
352       }
353     } else {
354       m_tag = 0;
355       m_has_children = false;
356       *offset_ptr = offset;
357       return true; // NULL debug tag entry
358     }
359   }
360 
361   return false;
362 }
363 
364 static dw_offset_t GetRangesOffset(const DWARFDebugRangesBase *debug_ranges,
365                                    DWARFFormValue &form_value) {
366   if (form_value.Form() == DW_FORM_rnglistx)
367     return debug_ranges->GetOffset(form_value.Unsigned());
368   return form_value.Unsigned();
369 }
370 
371 // GetDIENamesAndRanges
372 //
373 // Gets the valid address ranges for a given DIE by looking for a
374 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges attributes.
375 bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
376     const DWARFUnit *cu, const char *&name, const char *&mangled,
377     DWARFRangeList &ranges, int &decl_file, int &decl_line, int &decl_column,
378     int &call_file, int &call_line, int &call_column,
379     DWARFExpression *frame_base) const {
380   SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile();
381   if (dwo_symbol_file)
382     return GetDIENamesAndRanges(
383         dwo_symbol_file->GetCompileUnit(), name, mangled, ranges, decl_file,
384         decl_line, decl_column, call_file, call_line, call_column, frame_base);
385 
386   dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
387   dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
388   std::vector<DIERef> die_refs;
389   bool set_frame_base_loclist_addr = false;
390 
391   lldb::offset_t offset;
392   const DWARFAbbreviationDeclaration *abbrevDecl =
393       GetAbbreviationDeclarationPtr(cu, offset);
394 
395   SymbolFileDWARF *dwarf2Data = cu->GetSymbolFileDWARF();
396   lldb::ModuleSP module = dwarf2Data->GetObjectFile()->GetModule();
397 
398   if (abbrevDecl) {
399     const DWARFDataExtractor &debug_info_data = cu->GetData();
400 
401     if (!debug_info_data.ValidOffset(offset))
402       return false;
403 
404     const uint32_t numAttributes = abbrevDecl->NumAttributes();
405     bool do_offset = false;
406 
407     for (uint32_t i = 0; i < numAttributes; ++i) {
408       DWARFFormValue form_value(cu);
409       dw_attr_t attr;
410       abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
411 
412       if (form_value.ExtractValue(debug_info_data, &offset)) {
413         switch (attr) {
414         case DW_AT_low_pc:
415           lo_pc = form_value.Address();
416 
417           if (do_offset)
418             hi_pc += lo_pc;
419           do_offset = false;
420           break;
421 
422         case DW_AT_entry_pc:
423           lo_pc = form_value.Address();
424           break;
425 
426         case DW_AT_high_pc:
427           if (form_value.Form() == DW_FORM_addr ||
428               form_value.Form() == DW_FORM_addrx ||
429               form_value.Form() == DW_FORM_GNU_addr_index) {
430             hi_pc = form_value.Address();
431           } else {
432             hi_pc = form_value.Unsigned();
433             if (lo_pc == LLDB_INVALID_ADDRESS)
434               do_offset = hi_pc != LLDB_INVALID_ADDRESS;
435             else
436               hi_pc += lo_pc; // DWARF 4 introduces <offset-from-lo-pc> to save
437                               // on relocations
438           }
439           break;
440 
441         case DW_AT_ranges: {
442           const DWARFDebugRangesBase *debug_ranges = dwarf2Data->DebugRanges();
443           if (debug_ranges)
444             debug_ranges->FindRanges(cu, GetRangesOffset(debug_ranges, form_value), ranges);
445           else
446             cu->GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError(
447                 "{0x%8.8x}: DIE has DW_AT_ranges(0x%" PRIx64
448                 ") attribute yet DWARF has no .debug_ranges, please file a bug "
449                 "and attach the file at the start of this error message",
450                 m_offset, form_value.Unsigned());
451         } break;
452 
453         case DW_AT_name:
454           if (name == NULL)
455             name = form_value.AsCString();
456           break;
457 
458         case DW_AT_MIPS_linkage_name:
459         case DW_AT_linkage_name:
460           if (mangled == NULL)
461             mangled = form_value.AsCString();
462           break;
463 
464         case DW_AT_abstract_origin:
465           die_refs.emplace_back(form_value);
466           break;
467 
468         case DW_AT_specification:
469           die_refs.emplace_back(form_value);
470           break;
471 
472         case DW_AT_decl_file:
473           if (decl_file == 0)
474             decl_file = form_value.Unsigned();
475           break;
476 
477         case DW_AT_decl_line:
478           if (decl_line == 0)
479             decl_line = form_value.Unsigned();
480           break;
481 
482         case DW_AT_decl_column:
483           if (decl_column == 0)
484             decl_column = form_value.Unsigned();
485           break;
486 
487         case DW_AT_call_file:
488           if (call_file == 0)
489             call_file = form_value.Unsigned();
490           break;
491 
492         case DW_AT_call_line:
493           if (call_line == 0)
494             call_line = form_value.Unsigned();
495           break;
496 
497         case DW_AT_call_column:
498           if (call_column == 0)
499             call_column = form_value.Unsigned();
500           break;
501 
502         case DW_AT_frame_base:
503           if (frame_base) {
504             if (form_value.BlockData()) {
505               uint32_t block_offset =
506                   form_value.BlockData() - debug_info_data.GetDataStart();
507               uint32_t block_length = form_value.Unsigned();
508               frame_base->SetOpcodeData(module, debug_info_data, block_offset,
509                                         block_length);
510             } else {
511               const DWARFDataExtractor &debug_loc_data =
512                   dwarf2Data->DebugLocData();
513               const dw_offset_t debug_loc_offset = form_value.Unsigned();
514 
515               size_t loc_list_length = DWARFExpression::LocationListSize(
516                   cu, debug_loc_data, debug_loc_offset);
517               if (loc_list_length > 0) {
518                 frame_base->SetOpcodeData(module, debug_loc_data,
519                                           debug_loc_offset, loc_list_length);
520                 if (lo_pc != LLDB_INVALID_ADDRESS) {
521                   assert(lo_pc >= cu->GetBaseAddress());
522                   frame_base->SetLocationListSlide(lo_pc -
523                                                    cu->GetBaseAddress());
524                 } else {
525                   set_frame_base_loclist_addr = true;
526                 }
527               }
528             }
529           }
530           break;
531 
532         default:
533           break;
534         }
535       }
536     }
537   }
538 
539   if (ranges.IsEmpty()) {
540     if (lo_pc != LLDB_INVALID_ADDRESS) {
541       if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc)
542         ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
543       else
544         ranges.Append(DWARFRangeList::Entry(lo_pc, 0));
545     }
546   }
547 
548   if (set_frame_base_loclist_addr) {
549     dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0);
550     assert(lowest_range_pc >= cu->GetBaseAddress());
551     frame_base->SetLocationListSlide(lowest_range_pc - cu->GetBaseAddress());
552   }
553 
554   if (ranges.IsEmpty() || name == NULL || mangled == NULL) {
555     for (const DIERef &die_ref : die_refs) {
556       if (die_ref.die_offset != DW_INVALID_OFFSET) {
557         DWARFDIE die = dwarf2Data->GetDIE(die_ref);
558         if (die)
559           die.GetDIE()->GetDIENamesAndRanges(die.GetCU(), name, mangled, ranges,
560                                              decl_file, decl_line, decl_column,
561                                              call_file, call_line, call_column);
562       }
563     }
564   }
565   return !ranges.IsEmpty();
566 }
567 
568 // Dump
569 //
570 // Dumps a debug information entry and all of it's attributes to the specified
571 // stream.
572 void DWARFDebugInfoEntry::Dump(const DWARFUnit *cu, Stream &s,
573                                uint32_t recurse_depth) const {
574   const DWARFDataExtractor &debug_info_data = cu->GetData();
575   lldb::offset_t offset = m_offset;
576 
577   if (debug_info_data.ValidOffset(offset)) {
578     dw_uleb128_t abbrCode = debug_info_data.GetULEB128(&offset);
579 
580     s.Printf("\n0x%8.8x: ", m_offset);
581     s.Indent();
582     if (abbrCode != m_abbr_idx) {
583       s.Printf("error: DWARF has been modified\n");
584     } else if (abbrCode) {
585       const DWARFAbbreviationDeclaration *abbrevDecl =
586           cu->GetAbbreviations()->GetAbbreviationDeclaration(abbrCode);
587 
588       if (abbrevDecl) {
589         s.PutCString(DW_TAG_value_to_name(abbrevDecl->Tag()));
590         s.Printf(" [%u] %c\n", abbrCode, abbrevDecl->HasChildren() ? '*' : ' ');
591 
592         // Dump all data in the .debug_info for the attributes
593         const uint32_t numAttributes = abbrevDecl->NumAttributes();
594         for (uint32_t i = 0; i < numAttributes; ++i) {
595           DWARFFormValue form_value(cu);
596           dw_attr_t attr;
597           abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
598 
599           DumpAttribute(cu, debug_info_data, &offset, s, attr, form_value);
600         }
601 
602         const DWARFDebugInfoEntry *child = GetFirstChild();
603         if (recurse_depth > 0 && child) {
604           s.IndentMore();
605 
606           while (child) {
607             child->Dump(cu, s, recurse_depth - 1);
608             child = child->GetSibling();
609           }
610           s.IndentLess();
611         }
612       } else
613         s.Printf("Abbreviation code note found in 'debug_abbrev' class for "
614                  "code: %u\n",
615                  abbrCode);
616     } else {
617       s.Printf("NULL\n");
618     }
619   }
620 }
621 
622 // DumpAttribute
623 //
624 // Dumps a debug information entry attribute along with it's form. Any special
625 // display of attributes is done (disassemble location lists, show enumeration
626 // values for attributes, etc).
627 void DWARFDebugInfoEntry::DumpAttribute(
628     const DWARFUnit *cu, const DWARFDataExtractor &debug_info_data,
629     lldb::offset_t *offset_ptr, Stream &s, dw_attr_t attr,
630     DWARFFormValue &form_value) {
631   bool show_form = s.GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowForm);
632 
633   s.Printf("            ");
634   s.Indent(DW_AT_value_to_name(attr));
635 
636   if (show_form) {
637     s.Printf("[%s", DW_FORM_value_to_name(form_value.Form()));
638   }
639 
640   if (!form_value.ExtractValue(debug_info_data, offset_ptr))
641     return;
642 
643   if (show_form) {
644     if (form_value.Form() == DW_FORM_indirect) {
645       s.Printf(" [%s]", DW_FORM_value_to_name(form_value.Form()));
646     }
647 
648     s.PutCString("] ");
649   }
650 
651   s.PutCString("( ");
652 
653   SymbolFileDWARF *dwarf2Data = cu->GetSymbolFileDWARF();
654 
655   // Check to see if we have any special attribute formatters
656   switch (attr) {
657   case DW_AT_stmt_list:
658     s.Printf("0x%8.8" PRIx64, form_value.Unsigned());
659     break;
660 
661   case DW_AT_language:
662     s.PutCString(DW_LANG_value_to_name(form_value.Unsigned()));
663     break;
664 
665   case DW_AT_encoding:
666     s.PutCString(DW_ATE_value_to_name(form_value.Unsigned()));
667     break;
668 
669   case DW_AT_frame_base:
670   case DW_AT_location:
671   case DW_AT_data_member_location: {
672     const uint8_t *blockData = form_value.BlockData();
673     if (blockData) {
674       // Location description is inlined in data in the form value
675       DWARFDataExtractor locationData(debug_info_data,
676                                       (*offset_ptr) - form_value.Unsigned(),
677                                       form_value.Unsigned());
678       DWARFExpression::PrintDWARFExpression(
679           s, locationData, DWARFUnit::GetAddressByteSize(cu), 4, false);
680     } else {
681       // We have a location list offset as the value that is the offset into
682       // the .debug_loc section that describes the value over it's lifetime
683       uint64_t debug_loc_offset = form_value.Unsigned();
684       if (dwarf2Data) {
685         DWARFExpression::PrintDWARFLocationList(
686             s, cu, dwarf2Data->DebugLocData(), debug_loc_offset);
687       }
688     }
689   } break;
690 
691   case DW_AT_abstract_origin:
692   case DW_AT_specification: {
693     DWARFDIE abstract_die = form_value.Reference();
694     form_value.Dump(s);
695     //  *ostrm_ptr << HEX32 << abstract_die.GetOffset() << " ( ";
696     GetName(abstract_die.GetCU(), abstract_die.GetOffset(), s);
697   } break;
698 
699   case DW_AT_type: {
700     DWARFDIE type_die = form_value.Reference();
701     s.PutCString(" ( ");
702     AppendTypeName(type_die.GetCU(), type_die.GetOffset(), s);
703     s.PutCString(" )");
704   } break;
705 
706   case DW_AT_ranges: {
707     lldb::offset_t ranges_offset =
708         GetRangesOffset(dwarf2Data->DebugRanges(), form_value);
709     dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
710     DWARFDebugRanges::Dump(s, dwarf2Data->get_debug_ranges_data(),
711                            &ranges_offset, base_addr);
712   } break;
713 
714   default:
715     break;
716   }
717 
718   s.PutCString(" )\n");
719 }
720 
721 // Get all attribute values for a given DIE, including following any
722 // specification or abstract origin attributes and including those in the
723 // results. Any duplicate attributes will have the first instance take
724 // precedence (this can happen for declaration attributes).
725 size_t DWARFDebugInfoEntry::GetAttributes(
726     const DWARFUnit *cu, DWARFFormValue::FixedFormSizes fixed_form_sizes,
727     DWARFAttributes &attributes, uint32_t curr_depth) const {
728   const DWARFAbbreviationDeclaration *abbrevDecl = nullptr;
729   lldb::offset_t offset = 0;
730   if (cu)
731     abbrevDecl = GetAbbreviationDeclarationPtr(cu, offset);
732 
733   if (abbrevDecl) {
734     const DWARFDataExtractor &debug_info_data = cu->GetData();
735 
736     if (fixed_form_sizes.Empty())
737       fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize(
738           cu->GetAddressByteSize());
739 
740     const uint32_t num_attributes = abbrevDecl->NumAttributes();
741     for (uint32_t i = 0; i < num_attributes; ++i) {
742       DWARFFormValue form_value(cu);
743       dw_attr_t attr;
744       abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
745       const dw_form_t form = form_value.Form();
746 
747       // If we are tracking down DW_AT_specification or DW_AT_abstract_origin
748       // attributes, the depth will be non-zero. We need to omit certain
749       // attributes that don't make sense.
750       switch (attr) {
751       case DW_AT_sibling:
752       case DW_AT_declaration:
753         if (curr_depth > 0) {
754           // This attribute doesn't make sense when combined with the DIE that
755           // references this DIE. We know a DIE is referencing this DIE because
756           // curr_depth is not zero
757           break;
758         }
759         LLVM_FALLTHROUGH;
760       default:
761         attributes.Append(cu, offset, attr, form);
762         break;
763       }
764 
765       if ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin)) {
766         if (form_value.ExtractValue(debug_info_data, &offset)) {
767           DWARFDIE spec_die = form_value.Reference();
768           if (spec_die)
769             spec_die.GetAttributes(attributes, curr_depth + 1);
770         }
771       } else {
772         const uint8_t fixed_skip_size = fixed_form_sizes.GetSize(form);
773         if (fixed_skip_size)
774           offset += fixed_skip_size;
775         else
776           DWARFFormValue::SkipValue(form, debug_info_data, &offset, cu);
777       }
778     }
779   } else {
780     attributes.Clear();
781   }
782   return attributes.Size();
783 }
784 
785 // GetAttributeValue
786 //
787 // Get the value of an attribute and return the .debug_info offset of the
788 // attribute if it was properly extracted into form_value, or zero if we fail
789 // since an offset of zero is invalid for an attribute (it would be a compile
790 // unit header).
791 dw_offset_t DWARFDebugInfoEntry::GetAttributeValue(
792     const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &form_value,
793     dw_offset_t *end_attr_offset_ptr,
794     bool check_specification_or_abstract_origin) const {
795   SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile();
796   if (dwo_symbol_file && m_tag != DW_TAG_compile_unit &&
797                          m_tag != DW_TAG_partial_unit)
798     return GetAttributeValue(dwo_symbol_file->GetCompileUnit(), attr,
799                              form_value, end_attr_offset_ptr,
800                              check_specification_or_abstract_origin);
801 
802   lldb::offset_t offset;
803   const DWARFAbbreviationDeclaration *abbrevDecl =
804       GetAbbreviationDeclarationPtr(cu, offset);
805 
806   if (abbrevDecl) {
807     uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr);
808 
809     if (attr_idx != DW_INVALID_INDEX) {
810       const DWARFDataExtractor &debug_info_data = cu->GetData();
811 
812       uint32_t idx = 0;
813       while (idx < attr_idx)
814         DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++),
815                                   debug_info_data, &offset, cu);
816 
817       const dw_offset_t attr_offset = offset;
818       form_value.SetUnit(cu);
819       form_value.SetForm(abbrevDecl->GetFormByIndex(idx));
820       if (form_value.ExtractValue(debug_info_data, &offset)) {
821         if (end_attr_offset_ptr)
822           *end_attr_offset_ptr = offset;
823         return attr_offset;
824       }
825     }
826   }
827 
828   if (check_specification_or_abstract_origin) {
829     if (GetAttributeValue(cu, DW_AT_specification, form_value)) {
830       DWARFDIE die = form_value.Reference();
831       if (die) {
832         dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
833             die.GetCU(), attr, form_value, end_attr_offset_ptr, false);
834         if (die_offset)
835           return die_offset;
836       }
837     }
838 
839     if (GetAttributeValue(cu, DW_AT_abstract_origin, form_value)) {
840       DWARFDIE die = form_value.Reference();
841       if (die) {
842         dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
843             die.GetCU(), attr, form_value, end_attr_offset_ptr, false);
844         if (die_offset)
845           return die_offset;
846       }
847     }
848   }
849 
850   if (!dwo_symbol_file)
851     return 0;
852 
853   DWARFUnit *dwo_cu = dwo_symbol_file->GetCompileUnit();
854   if (!dwo_cu)
855     return 0;
856 
857   DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly();
858   if (!dwo_cu_die.IsValid())
859     return 0;
860 
861   return dwo_cu_die.GetDIE()->GetAttributeValue(
862       dwo_cu, attr, form_value, end_attr_offset_ptr,
863       check_specification_or_abstract_origin);
864 }
865 
866 // GetAttributeValueAsString
867 //
868 // Get the value of an attribute as a string return it. The resulting pointer
869 // to the string data exists within the supplied SymbolFileDWARF and will only
870 // be available as long as the SymbolFileDWARF is still around and it's content
871 // doesn't change.
872 const char *DWARFDebugInfoEntry::GetAttributeValueAsString(
873     const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value,
874     bool check_specification_or_abstract_origin) const {
875   DWARFFormValue form_value;
876   if (GetAttributeValue(cu, attr, form_value, nullptr,
877                         check_specification_or_abstract_origin))
878     return form_value.AsCString();
879   return fail_value;
880 }
881 
882 // GetAttributeValueAsUnsigned
883 //
884 // Get the value of an attribute as unsigned and return it.
885 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned(
886     const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
887     bool check_specification_or_abstract_origin) const {
888   DWARFFormValue form_value;
889   if (GetAttributeValue(cu, attr, form_value, nullptr,
890                         check_specification_or_abstract_origin))
891     return form_value.Unsigned();
892   return fail_value;
893 }
894 
895 // GetAttributeValueAsReference
896 //
897 // Get the value of an attribute as reference and fix up and compile unit
898 // relative offsets as needed.
899 DWARFDIE DWARFDebugInfoEntry::GetAttributeValueAsReference(
900     const DWARFUnit *cu, const dw_attr_t attr,
901     bool check_specification_or_abstract_origin) const {
902   DWARFFormValue form_value;
903   if (GetAttributeValue(cu, attr, form_value, nullptr,
904                         check_specification_or_abstract_origin))
905     return form_value.Reference();
906   return {};
907 }
908 
909 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress(
910     const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
911     bool check_specification_or_abstract_origin) const {
912   DWARFFormValue form_value;
913   if (GetAttributeValue(cu, attr, form_value, nullptr,
914                         check_specification_or_abstract_origin))
915     return form_value.Address();
916   return fail_value;
917 }
918 
919 // GetAttributeHighPC
920 //
921 // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low-
922 // pc>.
923 //
924 // Returns the hi_pc or fail_value.
925 dw_addr_t DWARFDebugInfoEntry::GetAttributeHighPC(
926     const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value,
927     bool check_specification_or_abstract_origin) const {
928   DWARFFormValue form_value;
929   if (GetAttributeValue(cu, DW_AT_high_pc, form_value, nullptr,
930                         check_specification_or_abstract_origin)) {
931     dw_form_t form = form_value.Form();
932     if (form == DW_FORM_addr || form == DW_FORM_addrx ||
933         form == DW_FORM_GNU_addr_index)
934       return form_value.Address();
935 
936     // DWARF4 can specify the hi_pc as an <offset-from-lowpc>
937     return lo_pc + form_value.Unsigned();
938   }
939   return fail_value;
940 }
941 
942 // GetAttributeAddressRange
943 //
944 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset-
945 // from-low-pc>.
946 //
947 // Returns true or sets lo_pc and hi_pc to fail_value.
948 bool DWARFDebugInfoEntry::GetAttributeAddressRange(
949     const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc,
950     uint64_t fail_value, bool check_specification_or_abstract_origin) const {
951   lo_pc = GetAttributeValueAsAddress(cu, DW_AT_low_pc, fail_value,
952                                      check_specification_or_abstract_origin);
953   if (lo_pc != fail_value) {
954     hi_pc = GetAttributeHighPC(cu, lo_pc, fail_value,
955                                check_specification_or_abstract_origin);
956     if (hi_pc != fail_value)
957       return true;
958   }
959   lo_pc = fail_value;
960   hi_pc = fail_value;
961   return false;
962 }
963 
964 size_t DWARFDebugInfoEntry::GetAttributeAddressRanges(
965     const DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc,
966     bool check_specification_or_abstract_origin) const {
967   ranges.Clear();
968 
969   SymbolFileDWARF *dwarf2Data = cu->GetSymbolFileDWARF();
970 
971   DWARFFormValue form_value;
972   if (GetAttributeValue(cu, DW_AT_ranges, form_value)) {
973     if (DWARFDebugRangesBase *debug_ranges = dwarf2Data->DebugRanges())
974       debug_ranges->FindRanges(cu, GetRangesOffset(debug_ranges, form_value),
975                                ranges);
976   } else if (check_hi_lo_pc) {
977     dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
978     dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
979     if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS,
980                                  check_specification_or_abstract_origin)) {
981       if (lo_pc < hi_pc)
982         ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
983     }
984   }
985   return ranges.GetSize();
986 }
987 
988 // GetName
989 //
990 // Get value of the DW_AT_name attribute and return it if one exists, else
991 // return NULL.
992 const char *DWARFDebugInfoEntry::GetName(const DWARFUnit *cu) const {
993   return GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
994 }
995 
996 // GetMangledName
997 //
998 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one
999 // exists, else return the value of the DW_AT_name attribute
1000 const char *
1001 DWARFDebugInfoEntry::GetMangledName(const DWARFUnit *cu,
1002                                     bool substitute_name_allowed) const {
1003   const char *name = nullptr;
1004 
1005   name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true);
1006   if (name)
1007     return name;
1008 
1009   name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true);
1010   if (name)
1011     return name;
1012 
1013   if (!substitute_name_allowed)
1014     return nullptr;
1015 
1016   name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
1017   return name;
1018 }
1019 
1020 // GetPubname
1021 //
1022 // Get value the name for a DIE as it should appear for a .debug_pubnames or
1023 // .debug_pubtypes section.
1024 const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const {
1025   const char *name = nullptr;
1026   if (!cu)
1027     return name;
1028 
1029   name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true);
1030   if (name)
1031     return name;
1032 
1033   name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true);
1034   if (name)
1035     return name;
1036 
1037   name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
1038   return name;
1039 }
1040 
1041 // GetName
1042 //
1043 // Get value of the DW_AT_name attribute for a debug information entry that
1044 // exists at offset "die_offset" and place that value into the supplied stream
1045 // object. If the DIE is a NULL object "NULL" is placed into the stream, and if
1046 // no DW_AT_name attribute exists for the DIE then nothing is printed.
1047 bool DWARFDebugInfoEntry::GetName(const DWARFUnit *cu,
1048                                   const dw_offset_t die_offset, Stream &s) {
1049   if (cu == NULL) {
1050     s.PutCString("NULL");
1051     return false;
1052   }
1053 
1054   DWARFDebugInfoEntry die;
1055   lldb::offset_t offset = die_offset;
1056   if (die.Extract(cu, &offset)) {
1057     if (die.IsNULL()) {
1058       s.PutCString("NULL");
1059       return true;
1060     } else {
1061       const char *name =
1062           die.GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
1063       if (name) {
1064         s.PutCString(name);
1065         return true;
1066       }
1067     }
1068   }
1069   return false;
1070 }
1071 
1072 // AppendTypeName
1073 //
1074 // Follows the type name definition down through all needed tags to end up with
1075 // a fully qualified type name and dump the results to the supplied stream.
1076 // This is used to show the name of types given a type identifier.
1077 bool DWARFDebugInfoEntry::AppendTypeName(const DWARFUnit *cu,
1078                                          const dw_offset_t die_offset,
1079                                          Stream &s) {
1080   if (cu == NULL) {
1081     s.PutCString("NULL");
1082     return false;
1083   }
1084 
1085   DWARFDebugInfoEntry die;
1086   lldb::offset_t offset = die_offset;
1087   if (die.Extract(cu, &offset)) {
1088     if (die.IsNULL()) {
1089       s.PutCString("NULL");
1090       return true;
1091     } else {
1092       const char *name = die.GetPubname(cu);
1093       if (name)
1094         s.PutCString(name);
1095       else {
1096         bool result = true;
1097         const DWARFAbbreviationDeclaration *abbrevDecl =
1098             die.GetAbbreviationDeclarationPtr(cu, offset);
1099 
1100         if (abbrevDecl == NULL)
1101           return false;
1102 
1103         switch (abbrevDecl->Tag()) {
1104         case DW_TAG_array_type:
1105           break; // print out a "[]" after printing the full type of the element
1106                  // below
1107         case DW_TAG_base_type:
1108           s.PutCString("base ");
1109           break;
1110         case DW_TAG_class_type:
1111           s.PutCString("class ");
1112           break;
1113         case DW_TAG_const_type:
1114           s.PutCString("const ");
1115           break;
1116         case DW_TAG_enumeration_type:
1117           s.PutCString("enum ");
1118           break;
1119         case DW_TAG_file_type:
1120           s.PutCString("file ");
1121           break;
1122         case DW_TAG_interface_type:
1123           s.PutCString("interface ");
1124           break;
1125         case DW_TAG_packed_type:
1126           s.PutCString("packed ");
1127           break;
1128         case DW_TAG_pointer_type:
1129           break; // print out a '*' after printing the full type below
1130         case DW_TAG_ptr_to_member_type:
1131           break; // print out a '*' after printing the full type below
1132         case DW_TAG_reference_type:
1133           break; // print out a '&' after printing the full type below
1134         case DW_TAG_restrict_type:
1135           s.PutCString("restrict ");
1136           break;
1137         case DW_TAG_set_type:
1138           s.PutCString("set ");
1139           break;
1140         case DW_TAG_shared_type:
1141           s.PutCString("shared ");
1142           break;
1143         case DW_TAG_string_type:
1144           s.PutCString("string ");
1145           break;
1146         case DW_TAG_structure_type:
1147           s.PutCString("struct ");
1148           break;
1149         case DW_TAG_subrange_type:
1150           s.PutCString("subrange ");
1151           break;
1152         case DW_TAG_subroutine_type:
1153           s.PutCString("function ");
1154           break;
1155         case DW_TAG_thrown_type:
1156           s.PutCString("thrown ");
1157           break;
1158         case DW_TAG_union_type:
1159           s.PutCString("union ");
1160           break;
1161         case DW_TAG_unspecified_type:
1162           s.PutCString("unspecified ");
1163           break;
1164         case DW_TAG_volatile_type:
1165           s.PutCString("volatile ");
1166           break;
1167         default:
1168           return false;
1169         }
1170 
1171         // Follow the DW_AT_type if possible
1172         DWARFFormValue form_value;
1173         if (die.GetAttributeValue(cu, DW_AT_type, form_value)) {
1174           DWARFDIE next_die = form_value.Reference();
1175           result = AppendTypeName(next_die.GetCU(), next_die.GetOffset(), s);
1176         }
1177 
1178         switch (abbrevDecl->Tag()) {
1179         case DW_TAG_array_type:
1180           s.PutCString("[]");
1181           break;
1182         case DW_TAG_pointer_type:
1183           s.PutChar('*');
1184           break;
1185         case DW_TAG_ptr_to_member_type:
1186           s.PutChar('*');
1187           break;
1188         case DW_TAG_reference_type:
1189           s.PutChar('&');
1190           break;
1191         default:
1192           break;
1193         }
1194         return result;
1195       }
1196     }
1197   }
1198   return false;
1199 }
1200 
1201 // BuildAddressRangeTable
1202 void DWARFDebugInfoEntry::BuildAddressRangeTable(
1203     const DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const {
1204   if (m_tag) {
1205     if (m_tag == DW_TAG_subprogram) {
1206       dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
1207       dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
1208       if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS)) {
1209         /// printf("BuildAddressRangeTable() 0x%8.8x: %30s: [0x%8.8x -
1210         /// 0x%8.8x)\n", m_offset, DW_TAG_value_to_name(tag), lo_pc, hi_pc);
1211         debug_aranges->AppendRange(cu->GetOffset(), lo_pc, hi_pc);
1212       }
1213     }
1214 
1215     const DWARFDebugInfoEntry *child = GetFirstChild();
1216     while (child) {
1217       child->BuildAddressRangeTable(cu, debug_aranges);
1218       child = child->GetSibling();
1219     }
1220   }
1221 }
1222 
1223 // BuildFunctionAddressRangeTable
1224 //
1225 // This function is very similar to the BuildAddressRangeTable function except
1226 // that the actual DIE offset for the function is placed in the table instead
1227 // of the compile unit offset (which is the way the standard .debug_aranges
1228 // section does it).
1229 void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable(
1230     const DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const {
1231   if (m_tag) {
1232     if (m_tag == DW_TAG_subprogram) {
1233       dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
1234       dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
1235       if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS)) {
1236         //  printf("BuildAddressRangeTable() 0x%8.8x: [0x%16.16" PRIx64 " -
1237         //  0x%16.16" PRIx64 ")\n", m_offset, lo_pc, hi_pc); // DEBUG ONLY
1238         debug_aranges->AppendRange(GetOffset(), lo_pc, hi_pc);
1239       }
1240     }
1241 
1242     const DWARFDebugInfoEntry *child = GetFirstChild();
1243     while (child) {
1244       child->BuildFunctionAddressRangeTable(cu, debug_aranges);
1245       child = child->GetSibling();
1246     }
1247   }
1248 }
1249 
1250 std::vector<DWARFDIE>
1251 DWARFDebugInfoEntry::GetDeclContextDIEs(DWARFUnit *cu) const {
1252 
1253   DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this));
1254   return die.GetDeclContextDIEs();
1255 }
1256 
1257 void DWARFDebugInfoEntry::GetDWARFDeclContext(
1258     DWARFUnit *cu, DWARFDeclContext &dwarf_decl_ctx) const {
1259   const dw_tag_t tag = Tag();
1260   if (tag != DW_TAG_compile_unit && tag != DW_TAG_partial_unit) {
1261     dwarf_decl_ctx.AppendDeclContext(tag, GetName(cu));
1262     DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu);
1263     if (parent_decl_ctx_die && parent_decl_ctx_die.GetDIE() != this) {
1264       if (parent_decl_ctx_die.Tag() != DW_TAG_compile_unit &&
1265           parent_decl_ctx_die.Tag() != DW_TAG_partial_unit)
1266         parent_decl_ctx_die.GetDIE()->GetDWARFDeclContext(
1267             parent_decl_ctx_die.GetCU(), dwarf_decl_ctx);
1268     }
1269   }
1270 }
1271 
1272 bool DWARFDebugInfoEntry::MatchesDWARFDeclContext(
1273     DWARFUnit *cu, const DWARFDeclContext &dwarf_decl_ctx) const {
1274 
1275   DWARFDeclContext this_dwarf_decl_ctx;
1276   GetDWARFDeclContext(cu, this_dwarf_decl_ctx);
1277   return this_dwarf_decl_ctx == dwarf_decl_ctx;
1278 }
1279 
1280 DWARFDIE
1281 DWARFDebugInfoEntry::GetParentDeclContextDIE(DWARFUnit *cu) const {
1282   DWARFAttributes attributes;
1283   GetAttributes(cu, DWARFFormValue::FixedFormSizes(), attributes);
1284   return GetParentDeclContextDIE(cu, attributes);
1285 }
1286 
1287 DWARFDIE
1288 DWARFDebugInfoEntry::GetParentDeclContextDIE(
1289     DWARFUnit *cu, const DWARFAttributes &attributes) const {
1290   DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this));
1291 
1292   while (die) {
1293     // If this is the original DIE that we are searching for a declaration for,
1294     // then don't look in the cache as we don't want our own decl context to be
1295     // our decl context...
1296     if (die.GetDIE() != this) {
1297       switch (die.Tag()) {
1298       case DW_TAG_compile_unit:
1299       case DW_TAG_partial_unit:
1300       case DW_TAG_namespace:
1301       case DW_TAG_structure_type:
1302       case DW_TAG_union_type:
1303       case DW_TAG_class_type:
1304         return die;
1305 
1306       default:
1307         break;
1308       }
1309     }
1310 
1311     DWARFDIE spec_die = attributes.FormValueAsReference(DW_AT_specification);
1312     if (spec_die) {
1313       DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE();
1314       if (decl_ctx_die)
1315         return decl_ctx_die;
1316     }
1317 
1318     DWARFDIE abs_die = attributes.FormValueAsReference(DW_AT_abstract_origin);
1319     if (abs_die) {
1320       DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE();
1321       if (decl_ctx_die)
1322         return decl_ctx_die;
1323     }
1324 
1325     die = die.GetParent();
1326   }
1327   return DWARFDIE();
1328 }
1329 
1330 const char *DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu,
1331                                                   std::string &storage) const {
1332   DWARFAttributes attributes;
1333   GetAttributes(cu, DWARFFormValue::FixedFormSizes(), attributes);
1334   return GetQualifiedName(cu, attributes, storage);
1335 }
1336 
1337 const char *
1338 DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu,
1339                                       const DWARFAttributes &attributes,
1340                                       std::string &storage) const {
1341 
1342   const char *name = GetName(cu);
1343 
1344   if (name) {
1345     DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu);
1346     storage.clear();
1347     // TODO: change this to get the correct decl context parent....
1348     while (parent_decl_ctx_die) {
1349       const dw_tag_t parent_tag = parent_decl_ctx_die.Tag();
1350       switch (parent_tag) {
1351       case DW_TAG_namespace: {
1352         const char *namespace_name = parent_decl_ctx_die.GetName();
1353         if (namespace_name) {
1354           storage.insert(0, "::");
1355           storage.insert(0, namespace_name);
1356         } else {
1357           storage.insert(0, "(anonymous namespace)::");
1358         }
1359         parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
1360       } break;
1361 
1362       case DW_TAG_class_type:
1363       case DW_TAG_structure_type:
1364       case DW_TAG_union_type: {
1365         const char *class_union_struct_name = parent_decl_ctx_die.GetName();
1366 
1367         if (class_union_struct_name) {
1368           storage.insert(0, "::");
1369           storage.insert(0, class_union_struct_name);
1370         }
1371         parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
1372       } break;
1373 
1374       default:
1375         parent_decl_ctx_die.Clear();
1376         break;
1377       }
1378     }
1379 
1380     if (storage.empty())
1381       storage.append("::");
1382 
1383     storage.append(name);
1384   }
1385   if (storage.empty())
1386     return NULL;
1387   return storage.c_str();
1388 }
1389 
1390 // LookupAddress
1391 bool DWARFDebugInfoEntry::LookupAddress(const dw_addr_t address,
1392                                         const DWARFUnit *cu,
1393                                         DWARFDebugInfoEntry **function_die,
1394                                         DWARFDebugInfoEntry **block_die) {
1395   bool found_address = false;
1396   if (m_tag) {
1397     bool check_children = false;
1398     bool match_addr_range = false;
1399     //  printf("0x%8.8x: %30s: address = 0x%8.8x - ", m_offset,
1400     //  DW_TAG_value_to_name(tag), address);
1401     switch (m_tag) {
1402     case DW_TAG_array_type:
1403       break;
1404     case DW_TAG_class_type:
1405       check_children = true;
1406       break;
1407     case DW_TAG_entry_point:
1408       break;
1409     case DW_TAG_enumeration_type:
1410       break;
1411     case DW_TAG_formal_parameter:
1412       break;
1413     case DW_TAG_imported_declaration:
1414       break;
1415     case DW_TAG_label:
1416       break;
1417     case DW_TAG_lexical_block:
1418       check_children = true;
1419       match_addr_range = true;
1420       break;
1421     case DW_TAG_member:
1422       break;
1423     case DW_TAG_pointer_type:
1424       break;
1425     case DW_TAG_reference_type:
1426       break;
1427     case DW_TAG_compile_unit:
1428       match_addr_range = true;
1429       break;
1430     case DW_TAG_string_type:
1431       break;
1432     case DW_TAG_structure_type:
1433       check_children = true;
1434       break;
1435     case DW_TAG_subroutine_type:
1436       break;
1437     case DW_TAG_typedef:
1438       break;
1439     case DW_TAG_union_type:
1440       break;
1441     case DW_TAG_unspecified_parameters:
1442       break;
1443     case DW_TAG_variant:
1444       break;
1445     case DW_TAG_common_block:
1446       check_children = true;
1447       break;
1448     case DW_TAG_common_inclusion:
1449       break;
1450     case DW_TAG_inheritance:
1451       break;
1452     case DW_TAG_inlined_subroutine:
1453       check_children = true;
1454       match_addr_range = true;
1455       break;
1456     case DW_TAG_module:
1457       match_addr_range = true;
1458       break;
1459     case DW_TAG_ptr_to_member_type:
1460       break;
1461     case DW_TAG_set_type:
1462       break;
1463     case DW_TAG_subrange_type:
1464       break;
1465     case DW_TAG_with_stmt:
1466       break;
1467     case DW_TAG_access_declaration:
1468       break;
1469     case DW_TAG_base_type:
1470       break;
1471     case DW_TAG_catch_block:
1472       match_addr_range = true;
1473       break;
1474     case DW_TAG_const_type:
1475       break;
1476     case DW_TAG_constant:
1477       break;
1478     case DW_TAG_enumerator:
1479       break;
1480     case DW_TAG_file_type:
1481       break;
1482     case DW_TAG_friend:
1483       break;
1484     case DW_TAG_namelist:
1485       break;
1486     case DW_TAG_namelist_item:
1487       break;
1488     case DW_TAG_packed_type:
1489       break;
1490     case DW_TAG_subprogram:
1491       match_addr_range = true;
1492       break;
1493     case DW_TAG_template_type_parameter:
1494       break;
1495     case DW_TAG_template_value_parameter:
1496       break;
1497     case DW_TAG_GNU_template_parameter_pack:
1498       break;
1499     case DW_TAG_thrown_type:
1500       break;
1501     case DW_TAG_try_block:
1502       match_addr_range = true;
1503       break;
1504     case DW_TAG_variant_part:
1505       break;
1506     case DW_TAG_variable:
1507       break;
1508     case DW_TAG_volatile_type:
1509       break;
1510     case DW_TAG_dwarf_procedure:
1511       break;
1512     case DW_TAG_restrict_type:
1513       break;
1514     case DW_TAG_interface_type:
1515       break;
1516     case DW_TAG_namespace:
1517       check_children = true;
1518       break;
1519     case DW_TAG_imported_module:
1520       break;
1521     case DW_TAG_unspecified_type:
1522       break;
1523     case DW_TAG_partial_unit:
1524       match_addr_range = true;
1525       break;
1526     case DW_TAG_imported_unit:
1527       break;
1528     case DW_TAG_shared_type:
1529       break;
1530     default:
1531       break;
1532     }
1533 
1534     if (match_addr_range) {
1535       dw_addr_t lo_pc =
1536           GetAttributeValueAsAddress(cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
1537       if (lo_pc != LLDB_INVALID_ADDRESS) {
1538         dw_addr_t hi_pc = GetAttributeHighPC(cu, lo_pc, LLDB_INVALID_ADDRESS);
1539         if (hi_pc != LLDB_INVALID_ADDRESS) {
1540           //  printf("\n0x%8.8x: %30s: address = 0x%8.8x  [0x%8.8x - 0x%8.8x) ",
1541           //  m_offset, DW_TAG_value_to_name(tag), address, lo_pc, hi_pc);
1542           if ((lo_pc <= address) && (address < hi_pc)) {
1543             found_address = true;
1544             //  puts("***MATCH***");
1545             switch (m_tag) {
1546             case DW_TAG_compile_unit: // File
1547             case DW_TAG_partial_unit: // File
1548               check_children = ((function_die != NULL) || (block_die != NULL));
1549               break;
1550 
1551             case DW_TAG_subprogram: // Function
1552               if (function_die)
1553                 *function_die = this;
1554               check_children = (block_die != NULL);
1555               break;
1556 
1557             case DW_TAG_inlined_subroutine: // Inlined Function
1558             case DW_TAG_lexical_block:      // Block { } in code
1559               if (block_die) {
1560                 *block_die = this;
1561                 check_children = true;
1562               }
1563               break;
1564 
1565             default:
1566               check_children = true;
1567               break;
1568             }
1569           }
1570         } else {
1571           // Compile units may not have a valid high/low pc when there
1572           // are address gaps in subroutines so we must always search
1573           // if there is no valid high and low PC.
1574           check_children = (m_tag == DW_TAG_compile_unit ||
1575                             m_tag == DW_TAG_partial_unit) &&
1576                            ((function_die != NULL) || (block_die != NULL));
1577         }
1578       } else {
1579         DWARFFormValue form_value;
1580         if (GetAttributeValue(cu, DW_AT_ranges, form_value)) {
1581           DWARFRangeList ranges;
1582           SymbolFileDWARF *dwarf2Data = cu->GetSymbolFileDWARF();
1583           DWARFDebugRangesBase *debug_ranges = dwarf2Data->DebugRanges();
1584           debug_ranges->FindRanges(
1585               cu, GetRangesOffset(debug_ranges, form_value), ranges);
1586 
1587           if (ranges.FindEntryThatContains(address)) {
1588             found_address = true;
1589             //  puts("***MATCH***");
1590             switch (m_tag) {
1591             case DW_TAG_compile_unit: // File
1592             case DW_TAG_partial_unit: // File
1593               check_children = ((function_die != NULL) || (block_die != NULL));
1594               break;
1595 
1596             case DW_TAG_subprogram: // Function
1597               if (function_die)
1598                 *function_die = this;
1599               check_children = (block_die != NULL);
1600               break;
1601 
1602             case DW_TAG_inlined_subroutine: // Inlined Function
1603             case DW_TAG_lexical_block:      // Block { } in code
1604               if (block_die) {
1605                 *block_die = this;
1606                 check_children = true;
1607               }
1608               break;
1609 
1610             default:
1611               check_children = true;
1612               break;
1613             }
1614           } else {
1615             check_children = false;
1616           }
1617         }
1618       }
1619     }
1620 
1621     if (check_children) {
1622       //  printf("checking children\n");
1623       DWARFDebugInfoEntry *child = GetFirstChild();
1624       while (child) {
1625         if (child->LookupAddress(address, cu, function_die, block_die))
1626           return true;
1627         child = child->GetSibling();
1628       }
1629     }
1630   }
1631   return found_address;
1632 }
1633 
1634 const DWARFAbbreviationDeclaration *
1635 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr(
1636     const DWARFUnit *cu, lldb::offset_t &offset) const {
1637   if (cu) {
1638     offset = GetOffset();
1639 
1640     const DWARFAbbreviationDeclarationSet *abbrev_set = cu->GetAbbreviations();
1641     if (abbrev_set) {
1642       const DWARFAbbreviationDeclaration *abbrev_decl =
1643           abbrev_set->GetAbbreviationDeclaration(m_abbr_idx);
1644       if (abbrev_decl) {
1645         // Make sure the abbreviation code still matches. If it doesn't and the
1646         // DWARF data was mmap'ed, the backing file might have been modified
1647         // which is bad news.
1648         const uint64_t abbrev_code = cu->GetData().GetULEB128(&offset);
1649 
1650         if (abbrev_decl->Code() == abbrev_code)
1651           return abbrev_decl;
1652 
1653         SymbolFileDWARF *dwarf2Data = cu->GetSymbolFileDWARF();
1654 
1655         dwarf2Data->GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
1656             "0x%8.8x: the DWARF debug information has been modified (abbrev "
1657             "code was %u, and is now %u)",
1658             GetOffset(), (uint32_t)abbrev_decl->Code(), (uint32_t)abbrev_code);
1659       }
1660     }
1661   }
1662   offset = DW_INVALID_OFFSET;
1663   return NULL;
1664 }
1665 
1666 bool DWARFDebugInfoEntry::OffsetLessThan(const DWARFDebugInfoEntry &a,
1667                                          const DWARFDebugInfoEntry &b) {
1668   return a.GetOffset() < b.GetOffset();
1669 }
1670 
1671 bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const {
1672   return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx &&
1673          m_sibling_idx == rhs.m_sibling_idx &&
1674          m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children &&
1675          m_tag == rhs.m_tag;
1676 }
1677 
1678 bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const {
1679   return !(*this == rhs);
1680 }
1681