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