1 //===-- DWARFDebugInfoEntry.cpp -------------------------------------------===//
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 "DWARFCompileUnit.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 "DWARFUnit.h"
30 #include "SymbolFileDWARF.h"
31 #include "SymbolFileDWARFDwo.h"
32 
33 using namespace lldb_private;
34 using namespace std;
35 extern int g_verbose;
36 
37 // Extract a debug info entry for a given DWARFUnit from the data
38 // starting at the offset in offset_ptr
39 bool DWARFDebugInfoEntry::Extract(const DWARFDataExtractor &data,
40                                   const DWARFUnit *cu,
41                                   lldb::offset_t *offset_ptr) {
42   m_offset = *offset_ptr;
43   m_parent_idx = 0;
44   m_sibling_idx = 0;
45   const uint64_t abbr_idx = data.GetULEB128(offset_ptr);
46   lldbassert(abbr_idx <= UINT16_MAX);
47   m_abbr_idx = abbr_idx;
48 
49   // assert (fixed_form_sizes);  // For best performance this should be
50   // specified!
51 
52   if (m_abbr_idx) {
53     lldb::offset_t offset = *offset_ptr;
54     const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu);
55     if (abbrevDecl == nullptr) {
56       cu->GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
57           "{0x%8.8x}: invalid abbreviation code %u, please file a bug and "
58           "attach the file at the start of this error message",
59           m_offset, (unsigned)abbr_idx);
60       // WE can't parse anymore if the DWARF is borked...
61       *offset_ptr = UINT32_MAX;
62       return false;
63     }
64     m_tag = abbrevDecl->Tag();
65     m_has_children = abbrevDecl->HasChildren();
66     // Skip all data in the .debug_info or .debug_types for the attributes
67     const uint32_t numAttributes = abbrevDecl->NumAttributes();
68     uint32_t i;
69     dw_form_t form;
70     for (i = 0; i < numAttributes; ++i) {
71       form = abbrevDecl->GetFormByIndexUnchecked(i);
72       llvm::Optional<uint8_t> fixed_skip_size =
73           DWARFFormValue::GetFixedSize(form, cu);
74       if (fixed_skip_size)
75         offset += *fixed_skip_size;
76       else {
77         bool form_is_indirect = false;
78         do {
79           form_is_indirect = false;
80           uint32_t form_size = 0;
81           switch (form) {
82           // Blocks if inlined data that have a length field and the data bytes
83           // inlined in the .debug_info/.debug_types
84           case DW_FORM_exprloc:
85           case DW_FORM_block:
86             form_size = data.GetULEB128(&offset);
87             break;
88           case DW_FORM_block1:
89             form_size = data.GetU8_unchecked(&offset);
90             break;
91           case DW_FORM_block2:
92             form_size = data.GetU16_unchecked(&offset);
93             break;
94           case DW_FORM_block4:
95             form_size = data.GetU32_unchecked(&offset);
96             break;
97 
98           // Inlined NULL terminated C-strings
99           case DW_FORM_string:
100             data.GetCStr(&offset);
101             break;
102 
103           // Compile unit address sized values
104           case DW_FORM_addr:
105             form_size = cu->GetAddressByteSize();
106             break;
107           case DW_FORM_ref_addr:
108             if (cu->GetVersion() <= 2)
109               form_size = cu->GetAddressByteSize();
110             else
111               form_size = 4;
112             break;
113 
114           // 0 sized form
115           case DW_FORM_flag_present:
116             form_size = 0;
117             break;
118 
119           // 1 byte values
120           case DW_FORM_addrx1:
121           case DW_FORM_data1:
122           case DW_FORM_flag:
123           case DW_FORM_ref1:
124           case DW_FORM_strx1:
125             form_size = 1;
126             break;
127 
128           // 2 byte values
129           case DW_FORM_addrx2:
130           case DW_FORM_data2:
131           case DW_FORM_ref2:
132           case DW_FORM_strx2:
133             form_size = 2;
134             break;
135 
136           // 3 byte values
137           case DW_FORM_addrx3:
138           case DW_FORM_strx3:
139             form_size = 3;
140             break;
141 
142           // 4 byte values
143           case DW_FORM_addrx4:
144           case DW_FORM_data4:
145           case DW_FORM_ref4:
146           case DW_FORM_strx4:
147             form_size = 4;
148             break;
149 
150           // 8 byte values
151           case DW_FORM_data8:
152           case DW_FORM_ref8:
153           case DW_FORM_ref_sig8:
154             form_size = 8;
155             break;
156 
157           // signed or unsigned LEB 128 values
158           case DW_FORM_addrx:
159           case DW_FORM_loclistx:
160           case DW_FORM_rnglistx:
161           case DW_FORM_sdata:
162           case DW_FORM_udata:
163           case DW_FORM_ref_udata:
164           case DW_FORM_GNU_addr_index:
165           case DW_FORM_GNU_str_index:
166           case DW_FORM_strx:
167             data.Skip_LEB128(&offset);
168             break;
169 
170           case DW_FORM_indirect:
171             form_is_indirect = true;
172             form = data.GetULEB128(&offset);
173             break;
174 
175           case DW_FORM_strp:
176           case DW_FORM_sec_offset:
177             data.GetU32(&offset);
178             break;
179 
180           case DW_FORM_implicit_const:
181             form_size = 0;
182             break;
183 
184           default:
185             *offset_ptr = m_offset;
186             return false;
187           }
188           offset += form_size;
189 
190         } while (form_is_indirect);
191       }
192     }
193     *offset_ptr = offset;
194     return true;
195   } else {
196     m_tag = llvm::dwarf::DW_TAG_null;
197     m_has_children = false;
198     return true; // NULL debug tag entry
199   }
200 
201   return false;
202 }
203 
204 static DWARFRangeList GetRangesOrReportError(DWARFUnit &unit,
205                                              const DWARFDebugInfoEntry &die,
206                                              const DWARFFormValue &value) {
207   llvm::Expected<DWARFRangeList> expected_ranges =
208       (value.Form() == DW_FORM_rnglistx)
209           ? unit.FindRnglistFromIndex(value.Unsigned())
210           : unit.FindRnglistFromOffset(value.Unsigned());
211   if (expected_ranges)
212     return std::move(*expected_ranges);
213   unit.GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
214       "{0x%8.8x}: DIE has DW_AT_ranges(0x%" PRIx64 ") attribute, but "
215       "range extraction failed (%s), please file a bug "
216       "and attach the file at the start of this error message",
217       die.GetOffset(), value.Unsigned(),
218       toString(expected_ranges.takeError()).c_str());
219   return DWARFRangeList();
220 }
221 
222 // GetDIENamesAndRanges
223 //
224 // Gets the valid address ranges for a given DIE by looking for a
225 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges attributes.
226 bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
227     DWARFUnit *cu, const char *&name, const char *&mangled,
228     DWARFRangeList &ranges, int &decl_file, int &decl_line, int &decl_column,
229     int &call_file, int &call_line, int &call_column,
230     DWARFExpression *frame_base) const {
231   dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
232   dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
233   std::vector<DWARFDIE> dies;
234   bool set_frame_base_loclist_addr = false;
235 
236   const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu);
237 
238   SymbolFileDWARF &dwarf = cu->GetSymbolFileDWARF();
239   lldb::ModuleSP module = dwarf.GetObjectFile()->GetModule();
240 
241   if (abbrevDecl) {
242     const DWARFDataExtractor &data = cu->GetData();
243     lldb::offset_t offset = GetFirstAttributeOffset();
244 
245     if (!data.ValidOffset(offset))
246       return false;
247 
248     const uint32_t numAttributes = abbrevDecl->NumAttributes();
249     bool do_offset = false;
250 
251     for (uint32_t i = 0; i < numAttributes; ++i) {
252       DWARFFormValue form_value(cu);
253       dw_attr_t attr;
254       abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
255 
256       if (form_value.ExtractValue(data, &offset)) {
257         switch (attr) {
258         case DW_AT_low_pc:
259           lo_pc = form_value.Address();
260 
261           if (do_offset)
262             hi_pc += lo_pc;
263           do_offset = false;
264           break;
265 
266         case DW_AT_entry_pc:
267           lo_pc = form_value.Address();
268           break;
269 
270         case DW_AT_high_pc:
271           if (form_value.Form() == DW_FORM_addr ||
272               form_value.Form() == DW_FORM_addrx ||
273               form_value.Form() == DW_FORM_GNU_addr_index) {
274             hi_pc = form_value.Address();
275           } else {
276             hi_pc = form_value.Unsigned();
277             if (lo_pc == LLDB_INVALID_ADDRESS)
278               do_offset = hi_pc != LLDB_INVALID_ADDRESS;
279             else
280               hi_pc += lo_pc; // DWARF 4 introduces <offset-from-lo-pc> to save
281                               // on relocations
282           }
283           break;
284 
285         case DW_AT_ranges:
286           ranges = GetRangesOrReportError(*cu, *this, form_value);
287           break;
288 
289         case DW_AT_name:
290           if (name == nullptr)
291             name = form_value.AsCString();
292           break;
293 
294         case DW_AT_MIPS_linkage_name:
295         case DW_AT_linkage_name:
296           if (mangled == nullptr)
297             mangled = form_value.AsCString();
298           break;
299 
300         case DW_AT_abstract_origin:
301           dies.push_back(form_value.Reference());
302           break;
303 
304         case DW_AT_specification:
305           dies.push_back(form_value.Reference());
306           break;
307 
308         case DW_AT_decl_file:
309           if (decl_file == 0)
310             decl_file = form_value.Unsigned();
311           break;
312 
313         case DW_AT_decl_line:
314           if (decl_line == 0)
315             decl_line = form_value.Unsigned();
316           break;
317 
318         case DW_AT_decl_column:
319           if (decl_column == 0)
320             decl_column = form_value.Unsigned();
321           break;
322 
323         case DW_AT_call_file:
324           if (call_file == 0)
325             call_file = form_value.Unsigned();
326           break;
327 
328         case DW_AT_call_line:
329           if (call_line == 0)
330             call_line = form_value.Unsigned();
331           break;
332 
333         case DW_AT_call_column:
334           if (call_column == 0)
335             call_column = form_value.Unsigned();
336           break;
337 
338         case DW_AT_frame_base:
339           if (frame_base) {
340             if (form_value.BlockData()) {
341               uint32_t block_offset =
342                   form_value.BlockData() - data.GetDataStart();
343               uint32_t block_length = form_value.Unsigned();
344               *frame_base = DWARFExpression(
345                   module, DataExtractor(data, block_offset, block_length), cu);
346             } else {
347               DataExtractor data = cu->GetLocationData();
348               const dw_offset_t offset = form_value.Unsigned();
349               if (data.ValidOffset(offset)) {
350                 data = DataExtractor(data, offset, data.GetByteSize() - offset);
351                 *frame_base = DWARFExpression(module, data, cu);
352                 if (lo_pc != LLDB_INVALID_ADDRESS) {
353                   assert(lo_pc >= cu->GetBaseAddress());
354                   frame_base->SetLocationListAddresses(cu->GetBaseAddress(),
355                                                        lo_pc);
356                 } else {
357                   set_frame_base_loclist_addr = true;
358                 }
359               }
360             }
361           }
362           break;
363 
364         default:
365           break;
366         }
367       }
368     }
369   }
370 
371   if (ranges.IsEmpty()) {
372     if (lo_pc != LLDB_INVALID_ADDRESS) {
373       if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc)
374         ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
375       else
376         ranges.Append(DWARFRangeList::Entry(lo_pc, 0));
377     }
378   }
379 
380   if (set_frame_base_loclist_addr) {
381     dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0);
382     assert(lowest_range_pc >= cu->GetBaseAddress());
383     frame_base->SetLocationListAddresses(cu->GetBaseAddress(), lowest_range_pc);
384   }
385 
386   if (ranges.IsEmpty() || name == nullptr || mangled == nullptr) {
387     for (const DWARFDIE &die : dies) {
388       if (die) {
389         die.GetDIE()->GetDIENamesAndRanges(die.GetCU(), name, mangled, ranges,
390                                            decl_file, decl_line, decl_column,
391                                            call_file, call_line, call_column);
392       }
393     }
394   }
395   return !ranges.IsEmpty();
396 }
397 
398 // Get all attribute values for a given DIE, including following any
399 // specification or abstract origin attributes and including those in the
400 // results. Any duplicate attributes will have the first instance take
401 // precedence (this can happen for declaration attributes).
402 size_t DWARFDebugInfoEntry::GetAttributes(
403     const DWARFUnit *cu, DWARFAttributes &attributes,
404     uint32_t curr_depth) const {
405   const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu);
406   if (abbrevDecl) {
407     const DWARFDataExtractor &data = cu->GetData();
408     lldb::offset_t offset = GetFirstAttributeOffset();
409 
410     const uint32_t num_attributes = abbrevDecl->NumAttributes();
411     for (uint32_t i = 0; i < num_attributes; ++i) {
412       DWARFFormValue form_value(cu);
413       dw_attr_t attr;
414       abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
415       const dw_form_t form = form_value.Form();
416 
417       // If we are tracking down DW_AT_specification or DW_AT_abstract_origin
418       // attributes, the depth will be non-zero. We need to omit certain
419       // attributes that don't make sense.
420       switch (attr) {
421       case DW_AT_sibling:
422       case DW_AT_declaration:
423         if (curr_depth > 0) {
424           // This attribute doesn't make sense when combined with the DIE that
425           // references this DIE. We know a DIE is referencing this DIE because
426           // curr_depth is not zero
427           break;
428         }
429         LLVM_FALLTHROUGH;
430       default:
431         attributes.Append(cu, offset, attr, form);
432         break;
433       }
434 
435       if ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin)) {
436         if (form_value.ExtractValue(data, &offset)) {
437           DWARFDIE spec_die = form_value.Reference();
438           if (spec_die)
439             spec_die.GetAttributes(attributes, curr_depth + 1);
440         }
441       } else {
442         llvm::Optional<uint8_t> fixed_skip_size = DWARFFormValue::GetFixedSize(form, cu);
443         if (fixed_skip_size)
444           offset += *fixed_skip_size;
445         else
446           DWARFFormValue::SkipValue(form, data, &offset, cu);
447       }
448     }
449   } else {
450     attributes.Clear();
451   }
452   return attributes.Size();
453 }
454 
455 // GetAttributeValue
456 //
457 // Get the value of an attribute and return the .debug_info or .debug_types
458 // offset of the attribute if it was properly extracted into form_value,
459 // or zero if we fail since an offset of zero is invalid for an attribute (it
460 // would be a compile unit header).
461 dw_offset_t DWARFDebugInfoEntry::GetAttributeValue(
462     const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &form_value,
463     dw_offset_t *end_attr_offset_ptr,
464     bool check_specification_or_abstract_origin) const {
465   if (const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu)) {
466     uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr);
467 
468     if (attr_idx != DW_INVALID_INDEX) {
469       const DWARFDataExtractor &data = cu->GetData();
470       lldb::offset_t offset = GetFirstAttributeOffset();
471 
472       uint32_t idx = 0;
473       while (idx < attr_idx)
474         DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++),
475                                   data, &offset, cu);
476 
477       const dw_offset_t attr_offset = offset;
478       form_value.SetUnit(cu);
479       form_value.SetForm(abbrevDecl->GetFormByIndex(idx));
480       if (form_value.ExtractValue(data, &offset)) {
481         if (end_attr_offset_ptr)
482           *end_attr_offset_ptr = offset;
483         return attr_offset;
484       }
485     }
486   }
487 
488   if (check_specification_or_abstract_origin) {
489     if (GetAttributeValue(cu, DW_AT_specification, form_value)) {
490       DWARFDIE die = form_value.Reference();
491       if (die) {
492         dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
493             die.GetCU(), attr, form_value, end_attr_offset_ptr, false);
494         if (die_offset)
495           return die_offset;
496       }
497     }
498 
499     if (GetAttributeValue(cu, DW_AT_abstract_origin, form_value)) {
500       DWARFDIE die = form_value.Reference();
501       if (die) {
502         dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
503             die.GetCU(), attr, form_value, end_attr_offset_ptr, false);
504         if (die_offset)
505           return die_offset;
506       }
507     }
508   }
509   return 0;
510 }
511 
512 // GetAttributeValueAsString
513 //
514 // Get the value of an attribute as a string return it. The resulting pointer
515 // to the string data exists within the supplied SymbolFileDWARF and will only
516 // be available as long as the SymbolFileDWARF is still around and it's content
517 // doesn't change.
518 const char *DWARFDebugInfoEntry::GetAttributeValueAsString(
519     const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value,
520     bool check_specification_or_abstract_origin) const {
521   DWARFFormValue form_value;
522   if (GetAttributeValue(cu, attr, form_value, nullptr,
523                         check_specification_or_abstract_origin))
524     return form_value.AsCString();
525   return fail_value;
526 }
527 
528 // GetAttributeValueAsUnsigned
529 //
530 // Get the value of an attribute as unsigned and return it.
531 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned(
532     const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
533     bool check_specification_or_abstract_origin) const {
534   DWARFFormValue form_value;
535   if (GetAttributeValue(cu, attr, form_value, nullptr,
536                         check_specification_or_abstract_origin))
537     return form_value.Unsigned();
538   return fail_value;
539 }
540 
541 // GetAttributeValueAsReference
542 //
543 // Get the value of an attribute as reference and fix up and compile unit
544 // relative offsets as needed.
545 DWARFDIE DWARFDebugInfoEntry::GetAttributeValueAsReference(
546     const DWARFUnit *cu, const dw_attr_t attr,
547     bool check_specification_or_abstract_origin) const {
548   DWARFFormValue form_value;
549   if (GetAttributeValue(cu, attr, form_value, nullptr,
550                         check_specification_or_abstract_origin))
551     return form_value.Reference();
552   return {};
553 }
554 
555 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress(
556     const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
557     bool check_specification_or_abstract_origin) const {
558   DWARFFormValue form_value;
559   if (GetAttributeValue(cu, attr, form_value, nullptr,
560                         check_specification_or_abstract_origin))
561     return form_value.Address();
562   return fail_value;
563 }
564 
565 // GetAttributeHighPC
566 //
567 // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low-
568 // pc>.
569 //
570 // Returns the hi_pc or fail_value.
571 dw_addr_t DWARFDebugInfoEntry::GetAttributeHighPC(
572     const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value,
573     bool check_specification_or_abstract_origin) const {
574   DWARFFormValue form_value;
575   if (GetAttributeValue(cu, DW_AT_high_pc, form_value, nullptr,
576                         check_specification_or_abstract_origin)) {
577     dw_form_t form = form_value.Form();
578     if (form == DW_FORM_addr || form == DW_FORM_addrx ||
579         form == DW_FORM_GNU_addr_index)
580       return form_value.Address();
581 
582     // DWARF4 can specify the hi_pc as an <offset-from-lowpc>
583     return lo_pc + form_value.Unsigned();
584   }
585   return fail_value;
586 }
587 
588 // GetAttributeAddressRange
589 //
590 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset-
591 // from-low-pc>.
592 //
593 // Returns true or sets lo_pc and hi_pc to fail_value.
594 bool DWARFDebugInfoEntry::GetAttributeAddressRange(
595     const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc,
596     uint64_t fail_value, bool check_specification_or_abstract_origin) const {
597   lo_pc = GetAttributeValueAsAddress(cu, DW_AT_low_pc, fail_value,
598                                      check_specification_or_abstract_origin);
599   if (lo_pc != fail_value) {
600     hi_pc = GetAttributeHighPC(cu, lo_pc, fail_value,
601                                check_specification_or_abstract_origin);
602     if (hi_pc != fail_value)
603       return true;
604   }
605   lo_pc = fail_value;
606   hi_pc = fail_value;
607   return false;
608 }
609 
610 size_t DWARFDebugInfoEntry::GetAttributeAddressRanges(
611     DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc,
612     bool check_specification_or_abstract_origin) const {
613   ranges.Clear();
614 
615   DWARFFormValue form_value;
616   if (GetAttributeValue(cu, DW_AT_ranges, form_value)) {
617     ranges = GetRangesOrReportError(*cu, *this, form_value);
618   } else if (check_hi_lo_pc) {
619     dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
620     dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
621     if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS,
622                                  check_specification_or_abstract_origin)) {
623       if (lo_pc < hi_pc)
624         ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
625     }
626   }
627   return ranges.GetSize();
628 }
629 
630 // GetName
631 //
632 // Get value of the DW_AT_name attribute and return it if one exists, else
633 // return NULL.
634 const char *DWARFDebugInfoEntry::GetName(const DWARFUnit *cu) const {
635   return GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
636 }
637 
638 // GetMangledName
639 //
640 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one
641 // exists, else return the value of the DW_AT_name attribute
642 const char *
643 DWARFDebugInfoEntry::GetMangledName(const DWARFUnit *cu,
644                                     bool substitute_name_allowed) const {
645   const char *name = nullptr;
646 
647   name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true);
648   if (name)
649     return name;
650 
651   name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true);
652   if (name)
653     return name;
654 
655   if (!substitute_name_allowed)
656     return nullptr;
657 
658   name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
659   return name;
660 }
661 
662 // GetPubname
663 //
664 // Get value the name for a DIE as it should appear for a .debug_pubnames or
665 // .debug_pubtypes section.
666 const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const {
667   const char *name = nullptr;
668   if (!cu)
669     return name;
670 
671   name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true);
672   if (name)
673     return name;
674 
675   name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true);
676   if (name)
677     return name;
678 
679   name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
680   return name;
681 }
682 
683 /// This function is builds a table very similar to the standard .debug_aranges
684 /// table, except that the actual DIE offset for the function is placed in the
685 /// table instead of the compile unit offset.
686 void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable(
687     const DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const {
688   if (m_tag) {
689     if (m_tag == DW_TAG_subprogram) {
690       dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
691       dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
692       if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS)) {
693         debug_aranges->AppendRange(GetOffset(), lo_pc, hi_pc);
694       }
695     }
696 
697     const DWARFDebugInfoEntry *child = GetFirstChild();
698     while (child) {
699       child->BuildFunctionAddressRangeTable(cu, debug_aranges);
700       child = child->GetSibling();
701     }
702   }
703 }
704 
705 DWARFDeclContext
706 DWARFDebugInfoEntry::GetDWARFDeclContextStatic(const DWARFDebugInfoEntry *die,
707                                                DWARFUnit *cu) {
708   DWARFDeclContext dwarf_decl_ctx;
709   for (;;) {
710     const dw_tag_t tag = die->Tag();
711     if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
712       return dwarf_decl_ctx;
713     dwarf_decl_ctx.AppendDeclContext(tag, die->GetName(cu));
714     DWARFDIE parent_decl_ctx_die = die->GetParentDeclContextDIE(cu);
715     if (!parent_decl_ctx_die || parent_decl_ctx_die.GetDIE() == die)
716       return dwarf_decl_ctx;
717     if (parent_decl_ctx_die.Tag() == DW_TAG_compile_unit ||
718         parent_decl_ctx_die.Tag() == DW_TAG_partial_unit)
719       return dwarf_decl_ctx;
720     die = parent_decl_ctx_die.GetDIE();
721     cu = parent_decl_ctx_die.GetCU();
722   }
723 }
724 
725 DWARFDeclContext DWARFDebugInfoEntry::GetDWARFDeclContext(DWARFUnit *cu) const {
726   return GetDWARFDeclContextStatic(this, cu);
727 }
728 
729 DWARFDIE
730 DWARFDebugInfoEntry::GetParentDeclContextDIE(DWARFUnit *cu) const {
731   DWARFAttributes attributes;
732   GetAttributes(cu, attributes);
733   return GetParentDeclContextDIE(cu, attributes);
734 }
735 
736 DWARFDIE
737 DWARFDebugInfoEntry::GetParentDeclContextDIE(
738     DWARFUnit *cu, const DWARFAttributes &attributes) const {
739   DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this));
740 
741   while (die) {
742     // If this is the original DIE that we are searching for a declaration for,
743     // then don't look in the cache as we don't want our own decl context to be
744     // our decl context...
745     if (die.GetDIE() != this) {
746       switch (die.Tag()) {
747       case DW_TAG_compile_unit:
748       case DW_TAG_partial_unit:
749       case DW_TAG_namespace:
750       case DW_TAG_structure_type:
751       case DW_TAG_union_type:
752       case DW_TAG_class_type:
753         return die;
754 
755       default:
756         break;
757       }
758     }
759 
760     DWARFDIE spec_die = attributes.FormValueAsReference(DW_AT_specification);
761     if (spec_die) {
762       DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE();
763       if (decl_ctx_die)
764         return decl_ctx_die;
765     }
766 
767     DWARFDIE abs_die = attributes.FormValueAsReference(DW_AT_abstract_origin);
768     if (abs_die) {
769       DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE();
770       if (decl_ctx_die)
771         return decl_ctx_die;
772     }
773 
774     die = die.GetParent();
775   }
776   return DWARFDIE();
777 }
778 
779 const char *DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu,
780                                                   std::string &storage) const {
781   DWARFAttributes attributes;
782   GetAttributes(cu, attributes);
783   return GetQualifiedName(cu, attributes, storage);
784 }
785 
786 const char *
787 DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu,
788                                       const DWARFAttributes &attributes,
789                                       std::string &storage) const {
790 
791   const char *name = GetName(cu);
792 
793   if (name) {
794     DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu);
795     storage.clear();
796     // TODO: change this to get the correct decl context parent....
797     while (parent_decl_ctx_die) {
798       const dw_tag_t parent_tag = parent_decl_ctx_die.Tag();
799       switch (parent_tag) {
800       case DW_TAG_namespace: {
801         const char *namespace_name = parent_decl_ctx_die.GetName();
802         if (namespace_name) {
803           storage.insert(0, "::");
804           storage.insert(0, namespace_name);
805         } else {
806           storage.insert(0, "(anonymous namespace)::");
807         }
808         parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
809       } break;
810 
811       case DW_TAG_class_type:
812       case DW_TAG_structure_type:
813       case DW_TAG_union_type: {
814         const char *class_union_struct_name = parent_decl_ctx_die.GetName();
815 
816         if (class_union_struct_name) {
817           storage.insert(0, "::");
818           storage.insert(0, class_union_struct_name);
819         }
820         parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
821       } break;
822 
823       default:
824         parent_decl_ctx_die.Clear();
825         break;
826       }
827     }
828 
829     if (storage.empty())
830       storage.append("::");
831 
832     storage.append(name);
833   }
834   if (storage.empty())
835     return nullptr;
836   return storage.c_str();
837 }
838 
839 lldb::offset_t DWARFDebugInfoEntry::GetFirstAttributeOffset() const {
840   return GetOffset() + llvm::getULEB128Size(m_abbr_idx);
841 }
842 
843 const DWARFAbbreviationDeclaration *
844 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const {
845   if (cu) {
846     const DWARFAbbreviationDeclarationSet *abbrev_set = cu->GetAbbreviations();
847     if (abbrev_set)
848       return abbrev_set->GetAbbreviationDeclaration(m_abbr_idx);
849   }
850   return nullptr;
851 }
852 
853 bool DWARFDebugInfoEntry::IsGlobalOrStaticScopeVariable() const {
854   if (Tag() != DW_TAG_variable)
855     return false;
856   const DWARFDebugInfoEntry *parent_die = GetParent();
857   while (parent_die != nullptr) {
858     switch (parent_die->Tag()) {
859     case DW_TAG_subprogram:
860     case DW_TAG_lexical_block:
861     case DW_TAG_inlined_subroutine:
862       return false;
863 
864     case DW_TAG_compile_unit:
865     case DW_TAG_partial_unit:
866       return true;
867 
868     default:
869       break;
870     }
871     parent_die = parent_die->GetParent();
872   }
873   return false;
874 }
875 
876 bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const {
877   return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx &&
878          m_sibling_idx == rhs.m_sibling_idx &&
879          m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children &&
880          m_tag == rhs.m_tag;
881 }
882 
883 bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const {
884   return !(*this == rhs);
885 }
886