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