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