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