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 "llvm/Support/LEB128.h"
16 
17 #include "lldb/Core/Module.h"
18 #include "lldb/Expression/DWARFExpression.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Utility/Stream.h"
21 
22 #include "DWARFUnit.h"
23 #include "DWARFDebugAbbrev.h"
24 #include "DWARFDebugAranges.h"
25 #include "DWARFDebugInfo.h"
26 #include "DWARFDebugRanges.h"
27 #include "DWARFDeclContext.h"
28 #include "DWARFFormValue.h"
29 #include "SymbolFileDWARF.h"
30 #include "SymbolFileDWARFDwo.h"
31 
32 using namespace lldb_private;
33 using namespace std;
34 extern int g_verbose;
35 
36 // Extract a debug info entry for a given DWARFUnit from the data
37 // starting at the offset in offset_ptr
38 bool DWARFDebugInfoEntry::Extract(const DWARFDataExtractor &data,
39                                   const DWARFUnit *cu,
40                                   lldb::offset_t *offset_ptr) {
41   m_offset = *offset_ptr;
42   m_parent_idx = 0;
43   m_sibling_idx = 0;
44   const uint64_t abbr_idx = data.GetULEB128(offset_ptr);
45   lldbassert(abbr_idx <= UINT16_MAX);
46   m_abbr_idx = abbr_idx;
47 
48   // assert (fixed_form_sizes);  // For best performance this should be
49   // specified!
50 
51   if (m_abbr_idx) {
52     lldb::offset_t offset = *offset_ptr;
53     auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu);
54     if (abbrevDecl == nullptr) {
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 or .debug_types 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       llvm::Optional<uint8_t> fixed_skip_size =
72           DWARFFormValue::GetFixedSize(form, cu);
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/.debug_types
83           case DW_FORM_exprloc:
84           case DW_FORM_block:
85             form_size = data.GetULEB128(&offset);
86             break;
87           case DW_FORM_block1:
88             form_size = data.GetU8_unchecked(&offset);
89             break;
90           case DW_FORM_block2:
91             form_size = data.GetU16_unchecked(&offset);
92             break;
93           case DW_FORM_block4:
94             form_size = data.GetU32_unchecked(&offset);
95             break;
96 
97           // Inlined NULL terminated C-strings
98           case DW_FORM_string:
99             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             data.Skip_LEB128(&offset);
166             break;
167 
168           case DW_FORM_indirect:
169             form_is_indirect = true;
170             form = data.GetULEB128(&offset);
171             break;
172 
173           case DW_FORM_strp:
174           case DW_FORM_sec_offset:
175             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 static DWARFRangeList GetRangesOrReportError(const DWARFUnit &unit,
203                                              const DWARFDebugInfoEntry &die,
204                                              const DWARFFormValue &value) {
205   llvm::Expected<DWARFRangeList> expected_ranges =
206       (value.Form() == DW_FORM_rnglistx)
207           ? unit.FindRnglistFromIndex(value.Unsigned())
208           : unit.FindRnglistFromOffset(value.Unsigned());
209   if (expected_ranges)
210     return std::move(*expected_ranges);
211   unit.GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError(
212       "{0x%8.8x}: DIE has DW_AT_ranges(0x%" PRIx64 ") attribute, but "
213       "range extraction failed (%s), please file a bug "
214       "and attach the file at the start of this error message",
215       die.GetOffset(), value.Unsigned(),
216       toString(expected_ranges.takeError()).c_str());
217   return DWARFRangeList();
218 }
219 
220 // GetDIENamesAndRanges
221 //
222 // Gets the valid address ranges for a given DIE by looking for a
223 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges attributes.
224 bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
225     const DWARFUnit *cu, const char *&name, const char *&mangled,
226     DWARFRangeList &ranges, int &decl_file, int &decl_line, int &decl_column,
227     int &call_file, int &call_line, int &call_column,
228     DWARFExpression *frame_base) const {
229   SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile();
230   if (dwo_symbol_file)
231     return GetDIENamesAndRanges(
232         dwo_symbol_file->GetCompileUnit(), name, mangled, ranges, decl_file,
233         decl_line, decl_column, call_file, call_line, call_column, frame_base);
234 
235   dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
236   dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
237   std::vector<DIERef> die_refs;
238   bool set_frame_base_loclist_addr = false;
239 
240   auto abbrevDecl = GetAbbreviationDeclarationPtr(cu);
241 
242   SymbolFileDWARF *dwarf2Data = cu->GetSymbolFileDWARF();
243   lldb::ModuleSP module = dwarf2Data->GetObjectFile()->GetModule();
244 
245   if (abbrevDecl) {
246     const DWARFDataExtractor &data = cu->GetData();
247     lldb::offset_t offset = GetFirstAttributeOffset();
248 
249     if (!data.ValidOffset(offset))
250       return false;
251 
252     const uint32_t numAttributes = abbrevDecl->NumAttributes();
253     bool do_offset = false;
254 
255     for (uint32_t i = 0; i < numAttributes; ++i) {
256       DWARFFormValue form_value(cu);
257       dw_attr_t attr;
258       abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
259 
260       if (form_value.ExtractValue(data, &offset)) {
261         switch (attr) {
262         case DW_AT_low_pc:
263           lo_pc = form_value.Address();
264 
265           if (do_offset)
266             hi_pc += lo_pc;
267           do_offset = false;
268           break;
269 
270         case DW_AT_entry_pc:
271           lo_pc = form_value.Address();
272           break;
273 
274         case DW_AT_high_pc:
275           if (form_value.Form() == DW_FORM_addr ||
276               form_value.Form() == DW_FORM_addrx ||
277               form_value.Form() == DW_FORM_GNU_addr_index) {
278             hi_pc = form_value.Address();
279           } else {
280             hi_pc = form_value.Unsigned();
281             if (lo_pc == LLDB_INVALID_ADDRESS)
282               do_offset = hi_pc != LLDB_INVALID_ADDRESS;
283             else
284               hi_pc += lo_pc; // DWARF 4 introduces <offset-from-lo-pc> to save
285                               // on relocations
286           }
287           break;
288 
289         case DW_AT_ranges:
290           ranges = GetRangesOrReportError(*cu, *this, form_value);
291           break;
292 
293         case DW_AT_name:
294           if (name == nullptr)
295             name = form_value.AsCString();
296           break;
297 
298         case DW_AT_MIPS_linkage_name:
299         case DW_AT_linkage_name:
300           if (mangled == nullptr)
301             mangled = form_value.AsCString();
302           break;
303 
304         case DW_AT_abstract_origin:
305           die_refs.emplace_back(form_value);
306           break;
307 
308         case DW_AT_specification:
309           die_refs.emplace_back(form_value);
310           break;
311 
312         case DW_AT_decl_file:
313           if (decl_file == 0)
314             decl_file = form_value.Unsigned();
315           break;
316 
317         case DW_AT_decl_line:
318           if (decl_line == 0)
319             decl_line = form_value.Unsigned();
320           break;
321 
322         case DW_AT_decl_column:
323           if (decl_column == 0)
324             decl_column = form_value.Unsigned();
325           break;
326 
327         case DW_AT_call_file:
328           if (call_file == 0)
329             call_file = form_value.Unsigned();
330           break;
331 
332         case DW_AT_call_line:
333           if (call_line == 0)
334             call_line = form_value.Unsigned();
335           break;
336 
337         case DW_AT_call_column:
338           if (call_column == 0)
339             call_column = form_value.Unsigned();
340           break;
341 
342         case DW_AT_frame_base:
343           if (frame_base) {
344             if (form_value.BlockData()) {
345               uint32_t block_offset =
346                   form_value.BlockData() - data.GetDataStart();
347               uint32_t block_length = form_value.Unsigned();
348               *frame_base = DWARFExpression(module, data, cu,
349                                             block_offset, block_length);
350             } else {
351               const DWARFDataExtractor &debug_loc_data =
352                   dwarf2Data->DebugLocData();
353               const dw_offset_t debug_loc_offset = form_value.Unsigned();
354 
355               size_t loc_list_length = DWARFExpression::LocationListSize(
356                   cu, debug_loc_data, debug_loc_offset);
357               if (loc_list_length > 0) {
358                 *frame_base =
359                     DWARFExpression(module, debug_loc_data, cu,
360                                     debug_loc_offset, loc_list_length);
361                 if (lo_pc != LLDB_INVALID_ADDRESS) {
362                   assert(lo_pc >= cu->GetBaseAddress());
363                   frame_base->SetLocationListSlide(lo_pc -
364                                                    cu->GetBaseAddress());
365                 } else {
366                   set_frame_base_loclist_addr = true;
367                 }
368               }
369             }
370           }
371           break;
372 
373         default:
374           break;
375         }
376       }
377     }
378   }
379 
380   if (ranges.IsEmpty()) {
381     if (lo_pc != LLDB_INVALID_ADDRESS) {
382       if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc)
383         ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
384       else
385         ranges.Append(DWARFRangeList::Entry(lo_pc, 0));
386     }
387   }
388 
389   if (set_frame_base_loclist_addr) {
390     dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0);
391     assert(lowest_range_pc >= cu->GetBaseAddress());
392     frame_base->SetLocationListSlide(lowest_range_pc - cu->GetBaseAddress());
393   }
394 
395   if (ranges.IsEmpty() || name == nullptr || mangled == nullptr) {
396     for (const DIERef &die_ref : die_refs) {
397       if (die_ref.die_offset != DW_INVALID_OFFSET) {
398         DWARFDIE die = dwarf2Data->GetDIE(die_ref);
399         if (die)
400           die.GetDIE()->GetDIENamesAndRanges(die.GetCU(), name, mangled, ranges,
401                                              decl_file, decl_line, decl_column,
402                                              call_file, call_line, call_column);
403       }
404     }
405   }
406   return !ranges.IsEmpty();
407 }
408 
409 // Dump
410 //
411 // Dumps a debug information entry and all of it's attributes to the specified
412 // stream.
413 void DWARFDebugInfoEntry::Dump(const DWARFUnit *cu, Stream &s,
414                                uint32_t recurse_depth) const {
415   const DWARFDataExtractor &data = cu->GetData();
416   lldb::offset_t offset = m_offset;
417 
418   if (data.ValidOffset(offset)) {
419     dw_uleb128_t abbrCode = data.GetULEB128(&offset);
420 
421     s.Printf("\n0x%8.8x: ", m_offset);
422     s.Indent();
423     if (abbrCode != m_abbr_idx) {
424       s.Printf("error: DWARF has been modified\n");
425     } else if (abbrCode) {
426       auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu);
427       if (abbrevDecl) {
428         s.PutCString(DW_TAG_value_to_name(abbrevDecl->Tag()));
429         s.Printf(" [%u] %c\n", abbrCode, abbrevDecl->HasChildren() ? '*' : ' ');
430 
431         // Dump all data in the .debug_info/.debug_types for the attributes
432         const uint32_t numAttributes = abbrevDecl->NumAttributes();
433         for (uint32_t i = 0; i < numAttributes; ++i) {
434           DWARFFormValue form_value(cu);
435           dw_attr_t attr;
436           abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
437 
438           DumpAttribute(cu, data, &offset, s, attr, form_value);
439         }
440 
441         const DWARFDebugInfoEntry *child = GetFirstChild();
442         if (recurse_depth > 0 && child) {
443           s.IndentMore();
444 
445           while (child) {
446             child->Dump(cu, s, recurse_depth - 1);
447             child = child->GetSibling();
448           }
449           s.IndentLess();
450         }
451       } else
452         s.Printf("Abbreviation code note found in 'debug_abbrev' class for "
453                  "code: %u\n",
454                  abbrCode);
455     } else {
456       s.Printf("NULL\n");
457     }
458   }
459 }
460 
461 // DumpAttribute
462 //
463 // Dumps a debug information entry attribute along with it's form. Any special
464 // display of attributes is done (disassemble location lists, show enumeration
465 // values for attributes, etc).
466 void DWARFDebugInfoEntry::DumpAttribute(
467     const DWARFUnit *cu, const DWARFDataExtractor &data,
468     lldb::offset_t *offset_ptr, Stream &s, dw_attr_t attr,
469     DWARFFormValue &form_value) {
470   bool show_form = s.GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowForm);
471 
472   s.Printf("            ");
473   s.Indent(DW_AT_value_to_name(attr));
474 
475   if (show_form) {
476     s.Printf("[%s", DW_FORM_value_to_name(form_value.Form()));
477   }
478 
479   if (!form_value.ExtractValue(data, offset_ptr))
480     return;
481 
482   if (show_form) {
483     if (form_value.Form() == DW_FORM_indirect) {
484       s.Printf(" [%s]", DW_FORM_value_to_name(form_value.Form()));
485     }
486 
487     s.PutCString("] ");
488   }
489 
490   s.PutCString("( ");
491 
492   SymbolFileDWARF *dwarf2Data = cu->GetSymbolFileDWARF();
493 
494   // Check to see if we have any special attribute formatters
495   switch (attr) {
496   case DW_AT_stmt_list:
497     s.Printf("0x%8.8" PRIx64, form_value.Unsigned());
498     break;
499 
500   case DW_AT_language:
501     s.PutCString(DW_LANG_value_to_name(form_value.Unsigned()));
502     break;
503 
504   case DW_AT_encoding:
505     s.PutCString(DW_ATE_value_to_name(form_value.Unsigned()));
506     break;
507 
508   case DW_AT_frame_base:
509   case DW_AT_location:
510   case DW_AT_data_member_location: {
511     const uint8_t *blockData = form_value.BlockData();
512     if (blockData) {
513       // Location description is inlined in data in the form value
514       DWARFDataExtractor locationData(data,
515                                       (*offset_ptr) - form_value.Unsigned(),
516                                       form_value.Unsigned());
517       DWARFExpression::PrintDWARFExpression(
518           s, locationData, DWARFUnit::GetAddressByteSize(cu), 4, false);
519     } else {
520       // We have a location list offset as the value that is the offset into
521       // the .debug_loc section that describes the value over it's lifetime
522       uint64_t debug_loc_offset = form_value.Unsigned();
523       if (dwarf2Data) {
524         DWARFExpression::PrintDWARFLocationList(
525             s, cu, dwarf2Data->DebugLocData(), debug_loc_offset);
526       }
527     }
528   } break;
529 
530   case DW_AT_abstract_origin:
531   case DW_AT_specification: {
532     DWARFDIE abstract_die = form_value.Reference();
533     form_value.Dump(s);
534     //  *ostrm_ptr << HEX32 << abstract_die.GetOffset() << " ( ";
535     abstract_die.GetName(s);
536   } break;
537 
538   case DW_AT_type: {
539     DWARFDIE type_die = form_value.Reference();
540     s.PutCString(" ( ");
541     type_die.AppendTypeName(s);
542     s.PutCString(" )");
543   } break;
544 
545   default:
546     break;
547   }
548 
549   s.PutCString(" )\n");
550 }
551 
552 // Get all attribute values for a given DIE, including following any
553 // specification or abstract origin attributes and including those in the
554 // results. Any duplicate attributes will have the first instance take
555 // precedence (this can happen for declaration attributes).
556 size_t DWARFDebugInfoEntry::GetAttributes(
557     const DWARFUnit *cu, DWARFAttributes &attributes,
558     uint32_t curr_depth) const {
559   auto abbrevDecl = GetAbbreviationDeclarationPtr(cu);
560   if (abbrevDecl) {
561     const DWARFDataExtractor &data = cu->GetData();
562     lldb::offset_t offset = GetFirstAttributeOffset();
563 
564     const uint32_t num_attributes = abbrevDecl->NumAttributes();
565     for (uint32_t i = 0; i < num_attributes; ++i) {
566       DWARFFormValue form_value(cu);
567       dw_attr_t attr;
568       abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
569       const dw_form_t form = form_value.Form();
570 
571       // If we are tracking down DW_AT_specification or DW_AT_abstract_origin
572       // attributes, the depth will be non-zero. We need to omit certain
573       // attributes that don't make sense.
574       switch (attr) {
575       case DW_AT_sibling:
576       case DW_AT_declaration:
577         if (curr_depth > 0) {
578           // This attribute doesn't make sense when combined with the DIE that
579           // references this DIE. We know a DIE is referencing this DIE because
580           // curr_depth is not zero
581           break;
582         }
583         LLVM_FALLTHROUGH;
584       default:
585         attributes.Append(cu, offset, attr, form);
586         break;
587       }
588 
589       if ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin)) {
590         if (form_value.ExtractValue(data, &offset)) {
591           DWARFDIE spec_die = form_value.Reference();
592           if (spec_die)
593             spec_die.GetAttributes(attributes, curr_depth + 1);
594         }
595       } else {
596         llvm::Optional<uint8_t> fixed_skip_size = DWARFFormValue::GetFixedSize(form, cu);
597         if (fixed_skip_size)
598           offset += *fixed_skip_size;
599         else
600           DWARFFormValue::SkipValue(form, data, &offset, cu);
601       }
602     }
603   } else {
604     attributes.Clear();
605   }
606   return attributes.Size();
607 }
608 
609 // GetAttributeValue
610 //
611 // Get the value of an attribute and return the .debug_info or .debug_types
612 // offset of the attribute if it was properly extracted into form_value,
613 // or zero if we fail since an offset of zero is invalid for an attribute (it
614 // would be a compile unit header).
615 dw_offset_t DWARFDebugInfoEntry::GetAttributeValue(
616     const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &form_value,
617     dw_offset_t *end_attr_offset_ptr,
618     bool check_specification_or_abstract_origin) const {
619   SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile();
620   if (dwo_symbol_file && m_tag != DW_TAG_compile_unit &&
621                          m_tag != DW_TAG_partial_unit)
622     return GetAttributeValue(dwo_symbol_file->GetCompileUnit(), attr,
623                              form_value, end_attr_offset_ptr,
624                              check_specification_or_abstract_origin);
625 
626   auto abbrevDecl = GetAbbreviationDeclarationPtr(cu);
627 
628   if (abbrevDecl) {
629     uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr);
630 
631     if (attr_idx != DW_INVALID_INDEX) {
632       const DWARFDataExtractor &data = cu->GetData();
633       lldb::offset_t offset = GetFirstAttributeOffset();
634 
635       uint32_t idx = 0;
636       while (idx < attr_idx)
637         DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++),
638                                   data, &offset, cu);
639 
640       const dw_offset_t attr_offset = offset;
641       form_value.SetUnit(cu);
642       form_value.SetForm(abbrevDecl->GetFormByIndex(idx));
643       if (form_value.ExtractValue(data, &offset)) {
644         if (end_attr_offset_ptr)
645           *end_attr_offset_ptr = offset;
646         return attr_offset;
647       }
648     }
649   }
650 
651   if (check_specification_or_abstract_origin) {
652     if (GetAttributeValue(cu, DW_AT_specification, form_value)) {
653       DWARFDIE die = form_value.Reference();
654       if (die) {
655         dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
656             die.GetCU(), attr, form_value, end_attr_offset_ptr, false);
657         if (die_offset)
658           return die_offset;
659       }
660     }
661 
662     if (GetAttributeValue(cu, DW_AT_abstract_origin, form_value)) {
663       DWARFDIE die = form_value.Reference();
664       if (die) {
665         dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
666             die.GetCU(), attr, form_value, end_attr_offset_ptr, false);
667         if (die_offset)
668           return die_offset;
669       }
670     }
671   }
672 
673   if (!dwo_symbol_file)
674     return 0;
675 
676   DWARFUnit *dwo_cu = dwo_symbol_file->GetCompileUnit();
677   if (!dwo_cu)
678     return 0;
679 
680   DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly();
681   if (!dwo_cu_die.IsValid())
682     return 0;
683 
684   return dwo_cu_die.GetDIE()->GetAttributeValue(
685       dwo_cu, attr, form_value, end_attr_offset_ptr,
686       check_specification_or_abstract_origin);
687 }
688 
689 // GetAttributeValueAsString
690 //
691 // Get the value of an attribute as a string return it. The resulting pointer
692 // to the string data exists within the supplied SymbolFileDWARF and will only
693 // be available as long as the SymbolFileDWARF is still around and it's content
694 // doesn't change.
695 const char *DWARFDebugInfoEntry::GetAttributeValueAsString(
696     const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value,
697     bool check_specification_or_abstract_origin) const {
698   DWARFFormValue form_value;
699   if (GetAttributeValue(cu, attr, form_value, nullptr,
700                         check_specification_or_abstract_origin))
701     return form_value.AsCString();
702   return fail_value;
703 }
704 
705 // GetAttributeValueAsUnsigned
706 //
707 // Get the value of an attribute as unsigned and return it.
708 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned(
709     const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
710     bool check_specification_or_abstract_origin) const {
711   DWARFFormValue form_value;
712   if (GetAttributeValue(cu, attr, form_value, nullptr,
713                         check_specification_or_abstract_origin))
714     return form_value.Unsigned();
715   return fail_value;
716 }
717 
718 // GetAttributeValueAsReference
719 //
720 // Get the value of an attribute as reference and fix up and compile unit
721 // relative offsets as needed.
722 DWARFDIE DWARFDebugInfoEntry::GetAttributeValueAsReference(
723     const DWARFUnit *cu, const dw_attr_t attr,
724     bool check_specification_or_abstract_origin) const {
725   DWARFFormValue form_value;
726   if (GetAttributeValue(cu, attr, form_value, nullptr,
727                         check_specification_or_abstract_origin))
728     return form_value.Reference();
729   return {};
730 }
731 
732 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress(
733     const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
734     bool check_specification_or_abstract_origin) const {
735   DWARFFormValue form_value;
736   if (GetAttributeValue(cu, attr, form_value, nullptr,
737                         check_specification_or_abstract_origin))
738     return form_value.Address();
739   return fail_value;
740 }
741 
742 // GetAttributeHighPC
743 //
744 // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low-
745 // pc>.
746 //
747 // Returns the hi_pc or fail_value.
748 dw_addr_t DWARFDebugInfoEntry::GetAttributeHighPC(
749     const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value,
750     bool check_specification_or_abstract_origin) const {
751   DWARFFormValue form_value;
752   if (GetAttributeValue(cu, DW_AT_high_pc, form_value, nullptr,
753                         check_specification_or_abstract_origin)) {
754     dw_form_t form = form_value.Form();
755     if (form == DW_FORM_addr || form == DW_FORM_addrx ||
756         form == DW_FORM_GNU_addr_index)
757       return form_value.Address();
758 
759     // DWARF4 can specify the hi_pc as an <offset-from-lowpc>
760     return lo_pc + form_value.Unsigned();
761   }
762   return fail_value;
763 }
764 
765 // GetAttributeAddressRange
766 //
767 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset-
768 // from-low-pc>.
769 //
770 // Returns true or sets lo_pc and hi_pc to fail_value.
771 bool DWARFDebugInfoEntry::GetAttributeAddressRange(
772     const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc,
773     uint64_t fail_value, bool check_specification_or_abstract_origin) const {
774   lo_pc = GetAttributeValueAsAddress(cu, DW_AT_low_pc, fail_value,
775                                      check_specification_or_abstract_origin);
776   if (lo_pc != fail_value) {
777     hi_pc = GetAttributeHighPC(cu, lo_pc, fail_value,
778                                check_specification_or_abstract_origin);
779     if (hi_pc != fail_value)
780       return true;
781   }
782   lo_pc = fail_value;
783   hi_pc = fail_value;
784   return false;
785 }
786 
787 size_t DWARFDebugInfoEntry::GetAttributeAddressRanges(
788     const DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc,
789     bool check_specification_or_abstract_origin) const {
790   ranges.Clear();
791 
792   DWARFFormValue form_value;
793   if (GetAttributeValue(cu, DW_AT_ranges, form_value)) {
794     ranges = GetRangesOrReportError(*cu, *this, form_value);
795   } else if (check_hi_lo_pc) {
796     dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
797     dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
798     if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS,
799                                  check_specification_or_abstract_origin)) {
800       if (lo_pc < hi_pc)
801         ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
802     }
803   }
804   return ranges.GetSize();
805 }
806 
807 // GetName
808 //
809 // Get value of the DW_AT_name attribute and return it if one exists, else
810 // return NULL.
811 const char *DWARFDebugInfoEntry::GetName(const DWARFUnit *cu) const {
812   return GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
813 }
814 
815 // GetMangledName
816 //
817 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one
818 // exists, else return the value of the DW_AT_name attribute
819 const char *
820 DWARFDebugInfoEntry::GetMangledName(const DWARFUnit *cu,
821                                     bool substitute_name_allowed) const {
822   const char *name = nullptr;
823 
824   name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true);
825   if (name)
826     return name;
827 
828   name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true);
829   if (name)
830     return name;
831 
832   if (!substitute_name_allowed)
833     return nullptr;
834 
835   name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
836   return name;
837 }
838 
839 // GetPubname
840 //
841 // Get value the name for a DIE as it should appear for a .debug_pubnames or
842 // .debug_pubtypes section.
843 const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const {
844   const char *name = nullptr;
845   if (!cu)
846     return name;
847 
848   name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true);
849   if (name)
850     return name;
851 
852   name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true);
853   if (name)
854     return name;
855 
856   name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
857   return name;
858 }
859 
860 // BuildAddressRangeTable
861 void DWARFDebugInfoEntry::BuildAddressRangeTable(
862     const DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const {
863   if (m_tag) {
864     if (m_tag == DW_TAG_subprogram) {
865       dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
866       dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
867       if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS)) {
868         /// printf("BuildAddressRangeTable() 0x%8.8x: %30s: [0x%8.8x -
869         /// 0x%8.8x)\n", m_offset, DW_TAG_value_to_name(tag), lo_pc, hi_pc);
870         debug_aranges->AppendRange(cu->GetOffset(), lo_pc, hi_pc);
871       }
872     }
873 
874     const DWARFDebugInfoEntry *child = GetFirstChild();
875     while (child) {
876       child->BuildAddressRangeTable(cu, debug_aranges);
877       child = child->GetSibling();
878     }
879   }
880 }
881 
882 // BuildFunctionAddressRangeTable
883 //
884 // This function is very similar to the BuildAddressRangeTable function except
885 // that the actual DIE offset for the function is placed in the table instead
886 // of the compile unit offset (which is the way the standard .debug_aranges
887 // section does it).
888 void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable(
889     const DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const {
890   if (m_tag) {
891     if (m_tag == DW_TAG_subprogram) {
892       dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
893       dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
894       if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS)) {
895         //  printf("BuildAddressRangeTable() 0x%8.8x: [0x%16.16" PRIx64 " -
896         //  0x%16.16" PRIx64 ")\n", m_offset, lo_pc, hi_pc); // DEBUG ONLY
897         debug_aranges->AppendRange(GetOffset(), lo_pc, hi_pc);
898       }
899     }
900 
901     const DWARFDebugInfoEntry *child = GetFirstChild();
902     while (child) {
903       child->BuildFunctionAddressRangeTable(cu, debug_aranges);
904       child = child->GetSibling();
905     }
906   }
907 }
908 
909 void DWARFDebugInfoEntry::GetDWARFDeclContext(
910     DWARFUnit *cu, DWARFDeclContext &dwarf_decl_ctx) const {
911   const dw_tag_t tag = Tag();
912   if (tag != DW_TAG_compile_unit && tag != DW_TAG_partial_unit) {
913     dwarf_decl_ctx.AppendDeclContext(tag, GetName(cu));
914     DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu);
915     if (parent_decl_ctx_die && parent_decl_ctx_die.GetDIE() != this) {
916       if (parent_decl_ctx_die.Tag() != DW_TAG_compile_unit &&
917           parent_decl_ctx_die.Tag() != DW_TAG_partial_unit)
918         parent_decl_ctx_die.GetDIE()->GetDWARFDeclContext(
919             parent_decl_ctx_die.GetCU(), dwarf_decl_ctx);
920     }
921   }
922 }
923 
924 DWARFDIE
925 DWARFDebugInfoEntry::GetParentDeclContextDIE(DWARFUnit *cu) const {
926   DWARFAttributes attributes;
927   GetAttributes(cu, attributes);
928   return GetParentDeclContextDIE(cu, attributes);
929 }
930 
931 DWARFDIE
932 DWARFDebugInfoEntry::GetParentDeclContextDIE(
933     DWARFUnit *cu, const DWARFAttributes &attributes) const {
934   DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this));
935 
936   while (die) {
937     // If this is the original DIE that we are searching for a declaration for,
938     // then don't look in the cache as we don't want our own decl context to be
939     // our decl context...
940     if (die.GetDIE() != this) {
941       switch (die.Tag()) {
942       case DW_TAG_compile_unit:
943       case DW_TAG_partial_unit:
944       case DW_TAG_namespace:
945       case DW_TAG_structure_type:
946       case DW_TAG_union_type:
947       case DW_TAG_class_type:
948         return die;
949 
950       default:
951         break;
952       }
953     }
954 
955     DWARFDIE spec_die = attributes.FormValueAsReference(DW_AT_specification);
956     if (spec_die) {
957       DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE();
958       if (decl_ctx_die)
959         return decl_ctx_die;
960     }
961 
962     DWARFDIE abs_die = attributes.FormValueAsReference(DW_AT_abstract_origin);
963     if (abs_die) {
964       DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE();
965       if (decl_ctx_die)
966         return decl_ctx_die;
967     }
968 
969     die = die.GetParent();
970   }
971   return DWARFDIE();
972 }
973 
974 const char *DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu,
975                                                   std::string &storage) const {
976   DWARFAttributes attributes;
977   GetAttributes(cu, attributes);
978   return GetQualifiedName(cu, attributes, storage);
979 }
980 
981 const char *
982 DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu,
983                                       const DWARFAttributes &attributes,
984                                       std::string &storage) const {
985 
986   const char *name = GetName(cu);
987 
988   if (name) {
989     DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu);
990     storage.clear();
991     // TODO: change this to get the correct decl context parent....
992     while (parent_decl_ctx_die) {
993       const dw_tag_t parent_tag = parent_decl_ctx_die.Tag();
994       switch (parent_tag) {
995       case DW_TAG_namespace: {
996         const char *namespace_name = parent_decl_ctx_die.GetName();
997         if (namespace_name) {
998           storage.insert(0, "::");
999           storage.insert(0, namespace_name);
1000         } else {
1001           storage.insert(0, "(anonymous namespace)::");
1002         }
1003         parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
1004       } break;
1005 
1006       case DW_TAG_class_type:
1007       case DW_TAG_structure_type:
1008       case DW_TAG_union_type: {
1009         const char *class_union_struct_name = parent_decl_ctx_die.GetName();
1010 
1011         if (class_union_struct_name) {
1012           storage.insert(0, "::");
1013           storage.insert(0, class_union_struct_name);
1014         }
1015         parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
1016       } break;
1017 
1018       default:
1019         parent_decl_ctx_die.Clear();
1020         break;
1021       }
1022     }
1023 
1024     if (storage.empty())
1025       storage.append("::");
1026 
1027     storage.append(name);
1028   }
1029   if (storage.empty())
1030     return nullptr;
1031   return storage.c_str();
1032 }
1033 
1034 bool DWARFDebugInfoEntry::LookupAddress(const dw_addr_t address,
1035                                         const DWARFUnit *cu,
1036                                         DWARFDebugInfoEntry **function_die,
1037                                         DWARFDebugInfoEntry **block_die) {
1038   bool found_address = false;
1039   if (m_tag) {
1040     bool check_children = false;
1041     bool match_addr_range = false;
1042     //  printf("0x%8.8x: %30s: address = 0x%8.8x - ", m_offset,
1043     //  DW_TAG_value_to_name(tag), address);
1044     switch (m_tag) {
1045     case DW_TAG_array_type:
1046       break;
1047     case DW_TAG_class_type:
1048       check_children = true;
1049       break;
1050     case DW_TAG_entry_point:
1051     case DW_TAG_enumeration_type:
1052     case DW_TAG_formal_parameter:
1053     case DW_TAG_imported_declaration:
1054     case DW_TAG_label:
1055       break;
1056     case DW_TAG_lexical_block:
1057       check_children = true;
1058       match_addr_range = true;
1059       break;
1060     case DW_TAG_member:
1061     case DW_TAG_pointer_type:
1062     case DW_TAG_reference_type:
1063       break;
1064     case DW_TAG_compile_unit:
1065       match_addr_range = true;
1066       break;
1067     case DW_TAG_string_type:
1068       break;
1069     case DW_TAG_structure_type:
1070       check_children = true;
1071       break;
1072     case DW_TAG_subroutine_type:
1073     case DW_TAG_typedef:
1074     case DW_TAG_union_type:
1075     case DW_TAG_unspecified_parameters:
1076     case DW_TAG_variant:
1077       break;
1078     case DW_TAG_common_block:
1079       check_children = true;
1080       break;
1081     case DW_TAG_common_inclusion:
1082     case DW_TAG_inheritance:
1083       break;
1084     case DW_TAG_inlined_subroutine:
1085       check_children = true;
1086       match_addr_range = true;
1087       break;
1088     case DW_TAG_module:
1089       match_addr_range = true;
1090       break;
1091     case DW_TAG_ptr_to_member_type:
1092     case DW_TAG_set_type:
1093     case DW_TAG_subrange_type:
1094     case DW_TAG_with_stmt:
1095     case DW_TAG_access_declaration:
1096     case DW_TAG_base_type:
1097       break;
1098     case DW_TAG_catch_block:
1099       match_addr_range = true;
1100       break;
1101     case DW_TAG_const_type:
1102     case DW_TAG_constant:
1103     case DW_TAG_enumerator:
1104     case DW_TAG_file_type:
1105     case DW_TAG_friend:
1106     case DW_TAG_namelist:
1107     case DW_TAG_namelist_item:
1108     case DW_TAG_packed_type:
1109       break;
1110     case DW_TAG_subprogram:
1111       match_addr_range = true;
1112       break;
1113     case DW_TAG_template_type_parameter:
1114     case DW_TAG_template_value_parameter:
1115     case DW_TAG_GNU_template_parameter_pack:
1116     case DW_TAG_thrown_type:
1117       break;
1118     case DW_TAG_try_block:
1119       match_addr_range = true;
1120       break;
1121     case DW_TAG_variant_part:
1122     case DW_TAG_variable:
1123     case DW_TAG_volatile_type:
1124     case DW_TAG_dwarf_procedure:
1125     case DW_TAG_restrict_type:
1126     case DW_TAG_interface_type:
1127       break;
1128     case DW_TAG_namespace:
1129       check_children = true;
1130       break;
1131     case DW_TAG_imported_module:
1132     case DW_TAG_unspecified_type:
1133       break;
1134     case DW_TAG_partial_unit:
1135       match_addr_range = true;
1136       break;
1137     case DW_TAG_imported_unit:
1138     case DW_TAG_shared_type:
1139     default:
1140       break;
1141     }
1142 
1143     if (match_addr_range) {
1144       dw_addr_t lo_pc =
1145           GetAttributeValueAsAddress(cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
1146       if (lo_pc != LLDB_INVALID_ADDRESS) {
1147         dw_addr_t hi_pc = GetAttributeHighPC(cu, lo_pc, LLDB_INVALID_ADDRESS);
1148         if (hi_pc != LLDB_INVALID_ADDRESS) {
1149           //  printf("\n0x%8.8x: %30s: address = 0x%8.8x  [0x%8.8x - 0x%8.8x) ",
1150           //  m_offset, DW_TAG_value_to_name(tag), address, lo_pc, hi_pc);
1151           if ((lo_pc <= address) && (address < hi_pc)) {
1152             found_address = true;
1153             //  puts("***MATCH***");
1154             switch (m_tag) {
1155             case DW_TAG_compile_unit: // File
1156             case DW_TAG_partial_unit: // File
1157               check_children =
1158                   ((function_die != nullptr) || (block_die != nullptr));
1159               break;
1160 
1161             case DW_TAG_subprogram: // Function
1162               if (function_die)
1163                 *function_die = this;
1164               check_children = (block_die != nullptr);
1165               break;
1166 
1167             case DW_TAG_inlined_subroutine: // Inlined Function
1168             case DW_TAG_lexical_block:      // Block { } in code
1169               if (block_die) {
1170                 *block_die = this;
1171                 check_children = true;
1172               }
1173               break;
1174 
1175             default:
1176               check_children = true;
1177               break;
1178             }
1179           }
1180         } else {
1181           // Compile units may not have a valid high/low pc when there
1182           // are address gaps in subroutines so we must always search
1183           // if there is no valid high and low PC.
1184           check_children =
1185               (m_tag == DW_TAG_compile_unit || m_tag == DW_TAG_partial_unit) &&
1186               ((function_die != nullptr) || (block_die != nullptr));
1187         }
1188       } else {
1189         DWARFRangeList ranges;
1190         if (GetAttributeAddressRanges(cu, ranges, /*check_hi_lo_pc*/ false) &&
1191             ranges.FindEntryThatContains(address)) {
1192           found_address = true;
1193           //  puts("***MATCH***");
1194           switch (m_tag) {
1195           case DW_TAG_compile_unit: // File
1196           case DW_TAG_partial_unit: // File
1197               check_children =
1198                   ((function_die != nullptr) || (block_die != nullptr));
1199               break;
1200 
1201           case DW_TAG_subprogram: // Function
1202             if (function_die)
1203               *function_die = this;
1204             check_children = (block_die != nullptr);
1205             break;
1206 
1207           case DW_TAG_inlined_subroutine: // Inlined Function
1208           case DW_TAG_lexical_block:      // Block { } in code
1209             if (block_die) {
1210               *block_die = this;
1211               check_children = true;
1212             }
1213             break;
1214 
1215           default:
1216             check_children = true;
1217             break;
1218           }
1219         } else {
1220           check_children = false;
1221         }
1222       }
1223     }
1224 
1225     if (check_children) {
1226       //  printf("checking children\n");
1227       DWARFDebugInfoEntry *child = GetFirstChild();
1228       while (child) {
1229         if (child->LookupAddress(address, cu, function_die, block_die))
1230           return true;
1231         child = child->GetSibling();
1232       }
1233     }
1234   }
1235   return found_address;
1236 }
1237 
1238 lldb::offset_t DWARFDebugInfoEntry::GetFirstAttributeOffset() const {
1239   return GetOffset() + llvm::getULEB128Size(m_abbr_idx);
1240 }
1241 
1242 const DWARFAbbreviationDeclaration *
1243 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const {
1244   if (cu) {
1245     const DWARFAbbreviationDeclarationSet *abbrev_set = cu->GetAbbreviations();
1246     if (abbrev_set)
1247       return abbrev_set->GetAbbreviationDeclaration(m_abbr_idx);
1248   }
1249   return nullptr;
1250 }
1251 
1252 bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const {
1253   return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx &&
1254          m_sibling_idx == rhs.m_sibling_idx &&
1255          m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children &&
1256          m_tag == rhs.m_tag;
1257 }
1258 
1259 bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const {
1260   return !(*this == rhs);
1261 }
1262