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