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