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