xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 <limits>
15 #include <optional>
16 
17 #include "LogChannelDWARF.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Expression/DWARFExpression.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
23 #include "llvm/Support/Error.h"
24 #include "llvm/Support/FormatAdapters.h"
25 #include "llvm/Support/LEB128.h"
26 
27 #include "DWARFCompileUnit.h"
28 #include "DWARFDebugAranges.h"
29 #include "DWARFDebugInfo.h"
30 #include "DWARFDeclContext.h"
31 #include "DWARFFormValue.h"
32 #include "DWARFUnit.h"
33 #include "SymbolFileDWARF.h"
34 #include "SymbolFileDWARFDwo.h"
35 
36 using namespace lldb_private;
37 using namespace lldb_private::dwarf;
38 using namespace lldb_private::plugin::dwarf;
39 extern int g_verbose;
40 
41 // Extract a debug info entry for a given DWARFUnit from the data
42 // starting at the offset in offset_ptr
Extract(const DWARFDataExtractor & data,const DWARFUnit & unit,lldb::offset_t * offset_ptr)43 bool DWARFDebugInfoEntry::Extract(const DWARFDataExtractor &data,
44                                   const DWARFUnit &unit,
45                                   lldb::offset_t *offset_ptr) {
46   m_offset = *offset_ptr;
47   auto report_error = [&](const char *fmt, const auto &...vals) {
48     unit.GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
49         "[{0:x16}]: {1}, please file a bug and "
50         "attach the file at the start of this error message",
51         static_cast<uint64_t>(m_offset), llvm::formatv(fmt, vals...));
52     *offset_ptr = std::numeric_limits<lldb::offset_t>::max();
53     return false;
54   };
55 
56   m_parent_idx = 0;
57   m_sibling_idx = 0;
58   const uint64_t abbr_idx = data.GetULEB128(offset_ptr);
59   if (abbr_idx > std::numeric_limits<uint16_t>::max())
60     return report_error("abbreviation code {0} too big", abbr_idx);
61   m_abbr_idx = abbr_idx;
62 
63   if (m_abbr_idx == 0) {
64     m_tag = llvm::dwarf::DW_TAG_null;
65     m_has_children = false;
66     return true; // NULL debug tag entry
67   }
68 
69   const auto *abbrevDecl = GetAbbreviationDeclarationPtr(&unit);
70   if (abbrevDecl == nullptr)
71     return report_error("invalid abbreviation code {0}", abbr_idx);
72 
73   m_tag = abbrevDecl->getTag();
74   m_has_children = abbrevDecl->hasChildren();
75   // Skip all data in the .debug_info or .debug_types for the attributes
76   for (const auto &attribute : abbrevDecl->attributes()) {
77     if (DWARFFormValue::SkipValue(attribute.Form, data, offset_ptr, &unit))
78       continue;
79 
80     return report_error("Unsupported DW_FORM_{1:x}", attribute.Form);
81   }
82   return true;
83 }
84 
85 static llvm::Expected<llvm::DWARFAddressRangesVector>
GetRanges(DWARFUnit & unit,const DWARFFormValue & value)86 GetRanges(DWARFUnit &unit, const DWARFFormValue &value) {
87   return (value.Form() == DW_FORM_rnglistx)
88              ? unit.FindRnglistFromIndex(value.Unsigned())
89              : unit.FindRnglistFromOffset(value.Unsigned());
90 }
91 
ExtractAttrAndFormValue(const llvm::DWARFAbbreviationDeclaration::AttributeSpec & attr_spec,dw_attr_t & attr,DWARFFormValue & form_value)92 static void ExtractAttrAndFormValue(
93     const llvm::DWARFAbbreviationDeclaration::AttributeSpec &attr_spec,
94     dw_attr_t &attr, DWARFFormValue &form_value) {
95   attr = attr_spec.Attr;
96   form_value.FormRef() = attr_spec.Form;
97   if (attr_spec.isImplicitConst())
98     form_value.SetSigned(attr_spec.getImplicitConstValue());
99 }
100 
101 // GetDIENamesAndRanges
102 //
103 // Gets the valid address ranges for a given DIE by looking for a
104 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges attributes.
GetDIENamesAndRanges(DWARFUnit * cu,const char * & name,const char * & mangled,llvm::DWARFAddressRangesVector & ranges,std::optional<int> & decl_file,std::optional<int> & decl_line,std::optional<int> & decl_column,std::optional<int> & call_file,std::optional<int> & call_line,std::optional<int> & call_column,DWARFExpressionList * frame_base) const105 bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
106     DWARFUnit *cu, const char *&name, const char *&mangled,
107     llvm::DWARFAddressRangesVector &ranges, std::optional<int> &decl_file,
108     std::optional<int> &decl_line, std::optional<int> &decl_column,
109     std::optional<int> &call_file, std::optional<int> &call_line,
110     std::optional<int> &call_column, DWARFExpressionList *frame_base) const {
111   dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
112   dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
113   std::vector<DWARFDIE> dies;
114   bool set_frame_base_loclist_addr = false;
115 
116   SymbolFileDWARF &dwarf = cu->GetSymbolFileDWARF();
117   lldb::ModuleSP module = dwarf.GetObjectFile()->GetModule();
118 
119   if (const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu)) {
120     const DWARFDataExtractor &data = cu->GetData();
121     lldb::offset_t offset = GetFirstAttributeOffset();
122 
123     if (!data.ValidOffset(offset))
124       return false;
125 
126     bool do_offset = false;
127 
128     for (const auto &attribute : abbrevDecl->attributes()) {
129       DWARFFormValue form_value(cu);
130       dw_attr_t attr;
131       ExtractAttrAndFormValue(attribute, attr, form_value);
132 
133       if (form_value.ExtractValue(data, &offset)) {
134         switch (attr) {
135         case DW_AT_low_pc:
136           lo_pc = form_value.Address();
137 
138           if (do_offset)
139             hi_pc += lo_pc;
140           do_offset = false;
141           break;
142 
143         case DW_AT_entry_pc:
144           lo_pc = form_value.Address();
145           break;
146 
147         case DW_AT_high_pc:
148           if (form_value.Form() == DW_FORM_addr ||
149               form_value.Form() == DW_FORM_addrx ||
150               form_value.Form() == DW_FORM_GNU_addr_index) {
151             hi_pc = form_value.Address();
152           } else {
153             hi_pc = form_value.Unsigned();
154             if (lo_pc == LLDB_INVALID_ADDRESS)
155               do_offset = hi_pc != LLDB_INVALID_ADDRESS;
156             else
157               hi_pc += lo_pc; // DWARF 4 introduces <offset-from-lo-pc> to save
158                               // on relocations
159           }
160           break;
161 
162         case DW_AT_ranges:
163           if (llvm::Expected<llvm::DWARFAddressRangesVector> r =
164                   GetRanges(*cu, form_value)) {
165             ranges = std::move(*r);
166           } else {
167             module->ReportError(
168                 "[{0:x16}]: DIE has DW_AT_ranges({1} {2:x16}) attribute, but "
169                 "range extraction failed ({3}), please file a bug "
170                 "and attach the file at the start of this error message",
171                 GetOffset(), llvm::dwarf::FormEncodingString(form_value.Form()),
172                 form_value.Unsigned(), fmt_consume(r.takeError()));
173           }
174           break;
175 
176         case DW_AT_name:
177           if (name == nullptr)
178             name = form_value.AsCString();
179           break;
180 
181         case DW_AT_MIPS_linkage_name:
182         case DW_AT_linkage_name:
183           if (mangled == nullptr)
184             mangled = form_value.AsCString();
185           break;
186 
187         case DW_AT_abstract_origin:
188           dies.push_back(form_value.Reference());
189           break;
190 
191         case DW_AT_specification:
192           dies.push_back(form_value.Reference());
193           break;
194 
195         case DW_AT_decl_file:
196           if (!decl_file)
197             decl_file = form_value.Unsigned();
198           break;
199 
200         case DW_AT_decl_line:
201           if (!decl_line)
202             decl_line = form_value.Unsigned();
203           break;
204 
205         case DW_AT_decl_column:
206           if (!decl_column)
207             decl_column = form_value.Unsigned();
208           break;
209 
210         case DW_AT_call_file:
211           if (!call_file)
212             call_file = form_value.Unsigned();
213           break;
214 
215         case DW_AT_call_line:
216           if (!call_line)
217             call_line = form_value.Unsigned();
218           break;
219 
220         case DW_AT_call_column:
221           if (!call_column)
222             call_column = form_value.Unsigned();
223           break;
224 
225         case DW_AT_frame_base:
226           if (frame_base) {
227             if (form_value.BlockData()) {
228               uint64_t block_offset =
229                   form_value.BlockData() - data.GetDataStart();
230               uint64_t block_length = form_value.Unsigned();
231               *frame_base =
232                   DWARFExpressionList(module,
233                                       DWARFExpression(DataExtractor(
234                                           data, block_offset, block_length)),
235                                       cu);
236             } else {
237               DataExtractor data = cu->GetLocationData();
238               const dw_offset_t offset = form_value.Unsigned();
239               if (data.ValidOffset(offset)) {
240                 data = DataExtractor(data, offset, data.GetByteSize() - offset);
241                 if (lo_pc != LLDB_INVALID_ADDRESS) {
242                   assert(lo_pc >= cu->GetBaseAddress());
243                   cu->ParseDWARFLocationList(data, *frame_base);
244                   frame_base->SetFuncFileAddress(lo_pc);
245                 } else
246                   set_frame_base_loclist_addr = true;
247               }
248             }
249           }
250           break;
251 
252         default:
253           break;
254         }
255       }
256     }
257   }
258 
259   if (ranges.empty() && lo_pc != LLDB_INVALID_ADDRESS) {
260     lldb::addr_t range_hi_pc =
261         (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc) ? hi_pc : lo_pc;
262     ranges.emplace_back(lo_pc, range_hi_pc);
263   }
264 
265   if (set_frame_base_loclist_addr && !ranges.empty()) {
266     dw_addr_t file_addr = ranges.begin()->LowPC;
267     assert(file_addr >= cu->GetBaseAddress());
268     frame_base->SetFuncFileAddress(file_addr);
269   }
270 
271   if (ranges.empty() || name == nullptr || mangled == nullptr) {
272     for (const DWARFDIE &die : dies) {
273       if (die) {
274         die.GetDIE()->GetDIENamesAndRanges(die.GetCU(), name, mangled, ranges,
275                                            decl_file, decl_line, decl_column,
276                                            call_file, call_line, call_column);
277       }
278     }
279   }
280   return !ranges.empty();
281 }
282 
283 /// Helper for the public \ref DWARFDebugInfoEntry::GetAttributes API.
284 /// Adds all attributes of the DIE at the top of the \c worklist to the
285 /// \c attributes list. Specifcations and abstract origins are added
286 /// to the \c worklist if the referenced DIE has not been seen before.
GetAttributes(llvm::SmallVectorImpl<DWARFDIE> & worklist,llvm::SmallSet<DWARFDebugInfoEntry const *,3> & seen,DWARFAttributes & attributes)287 static bool GetAttributes(llvm::SmallVectorImpl<DWARFDIE> &worklist,
288                           llvm::SmallSet<DWARFDebugInfoEntry const *, 3> &seen,
289                           DWARFAttributes &attributes) {
290   assert(!worklist.empty() && "Need at least one DIE to visit.");
291   assert(seen.size() >= 1 &&
292          "Need to have seen at least the currently visited entry.");
293 
294   DWARFDIE current = worklist.pop_back_val();
295 
296   const auto *cu = current.GetCU();
297   assert(cu);
298 
299   const auto *entry = current.GetDIE();
300   assert(entry);
301 
302   const auto *abbrevDecl =
303       entry->GetAbbreviationDeclarationPtr(current.GetCU());
304   if (!abbrevDecl)
305     return false;
306 
307   const DWARFDataExtractor &data = cu->GetData();
308   lldb::offset_t offset = current.GetDIE()->GetFirstAttributeOffset();
309 
310   const bool is_first_die = seen.size() == 1;
311 
312   for (const auto &attribute : abbrevDecl->attributes()) {
313     DWARFFormValue form_value(cu);
314     dw_attr_t attr;
315     ExtractAttrAndFormValue(attribute, attr, form_value);
316 
317     // If we are tracking down DW_AT_specification or DW_AT_abstract_origin
318     // attributes, the depth will be non-zero. We need to omit certain
319     // attributes that don't make sense.
320     switch (attr) {
321     case DW_AT_sibling:
322     case DW_AT_declaration:
323       if (!is_first_die) {
324         // This attribute doesn't make sense when combined with the DIE that
325         // references this DIE. We know a DIE is referencing this DIE because
326         // we've visited more than one DIE already.
327         break;
328       }
329       [[fallthrough]];
330     default:
331       attributes.Append(form_value, offset, attr);
332       break;
333     }
334 
335     if (attr == DW_AT_specification || attr == DW_AT_abstract_origin) {
336       if (form_value.ExtractValue(data, &offset)) {
337         if (DWARFDIE spec_die = form_value.Reference()) {
338           if (seen.insert(spec_die.GetDIE()).second)
339             worklist.push_back(spec_die);
340         }
341       }
342     } else {
343       const dw_form_t form = form_value.Form();
344       std::optional<uint8_t> fixed_skip_size =
345           DWARFFormValue::GetFixedSize(form, cu);
346       if (fixed_skip_size)
347         offset += *fixed_skip_size;
348       else
349         DWARFFormValue::SkipValue(form, data, &offset, cu);
350     }
351   }
352 
353   return true;
354 }
355 
GetAttributes(const DWARFUnit * cu,Recurse recurse) const356 DWARFAttributes DWARFDebugInfoEntry::GetAttributes(const DWARFUnit *cu,
357                                                    Recurse recurse) const {
358   // FIXME: use ElaboratingDIEIterator to follow specifications/abstract origins
359   // instead of maintaining our own worklist/seen list.
360 
361   DWARFAttributes attributes;
362 
363   llvm::SmallVector<DWARFDIE, 3> worklist;
364   worklist.emplace_back(cu, this);
365 
366   // Keep track if DIEs already seen to prevent infinite recursion.
367   // Value of '3' was picked for the same reason that
368   // DWARFDie::findRecursively does.
369   llvm::SmallSet<DWARFDebugInfoEntry const *, 3> seen;
370   seen.insert(this);
371 
372   do {
373     if (!::GetAttributes(worklist, seen, attributes)) {
374       attributes.Clear();
375       break;
376     }
377   } while (!worklist.empty() && recurse == Recurse::yes);
378 
379   return attributes;
380 }
381 
382 // GetAttributeValue
383 //
384 // Get the value of an attribute and return the .debug_info or .debug_types
385 // offset of the attribute if it was properly extracted into form_value,
386 // or zero if we fail since an offset of zero is invalid for an attribute (it
387 // would be a compile unit header).
GetAttributeValue(const DWARFUnit * cu,const dw_attr_t attr,DWARFFormValue & form_value,dw_offset_t * end_attr_offset_ptr,bool check_elaborating_dies) const388 dw_offset_t DWARFDebugInfoEntry::GetAttributeValue(
389     const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &form_value,
390     dw_offset_t *end_attr_offset_ptr, bool check_elaborating_dies) const {
391   if (const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu)) {
392     std::optional<uint32_t> attr_idx = abbrevDecl->findAttributeIndex(attr);
393 
394     if (attr_idx) {
395       const DWARFDataExtractor &data = cu->GetData();
396       lldb::offset_t offset = GetFirstAttributeOffset();
397 
398       uint32_t idx = 0;
399       while (idx < *attr_idx)
400         DWARFFormValue::SkipValue(abbrevDecl->getFormByIndex(idx++), data,
401                                   &offset, cu);
402 
403       const dw_offset_t attr_offset = offset;
404       form_value.SetUnit(cu);
405       form_value.SetForm(abbrevDecl->getFormByIndex(idx));
406       if (abbrevDecl->getAttrIsImplicitConstByIndex(idx))
407         form_value.SetValue(abbrevDecl->getAttrImplicitConstValueByIndex(idx));
408 
409       if (form_value.ExtractValue(data, &offset)) {
410         if (end_attr_offset_ptr)
411           *end_attr_offset_ptr = offset;
412         return attr_offset;
413       }
414     }
415   }
416 
417   if (check_elaborating_dies) {
418     for (dw_attr_t elaborating_attr :
419          {DW_AT_specification, DW_AT_abstract_origin, DW_AT_signature}) {
420       if (!GetAttributeValue(cu, elaborating_attr, form_value))
421         continue;
422       DWARFDIE die = form_value.Reference();
423       if (!die)
424         continue;
425       dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
426           die.GetCU(), attr, form_value, end_attr_offset_ptr, false);
427       if (die_offset)
428         return die_offset;
429     }
430   }
431   return 0;
432 }
433 
434 // GetAttributeValueAsString
435 //
436 // Get the value of an attribute as a string return it. The resulting pointer
437 // to the string data exists within the supplied SymbolFileDWARF and will only
438 // be available as long as the SymbolFileDWARF is still around and it's content
439 // doesn't change.
GetAttributeValueAsString(const DWARFUnit * cu,const dw_attr_t attr,const char * fail_value,bool check_elaborating_dies) const440 const char *DWARFDebugInfoEntry::GetAttributeValueAsString(
441     const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value,
442     bool check_elaborating_dies) const {
443   DWARFFormValue form_value;
444   if (GetAttributeValue(cu, attr, form_value, nullptr, check_elaborating_dies))
445     return form_value.AsCString();
446   return fail_value;
447 }
448 
449 // GetAttributeValueAsUnsigned
450 //
451 // Get the value of an attribute as unsigned and return it.
GetAttributeValueAsUnsigned(const DWARFUnit * cu,const dw_attr_t attr,uint64_t fail_value,bool check_elaborating_dies) const452 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned(
453     const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
454     bool check_elaborating_dies) const {
455   DWARFFormValue form_value;
456   if (GetAttributeValue(cu, attr, form_value, nullptr, check_elaborating_dies))
457     return form_value.Unsigned();
458   return fail_value;
459 }
460 
461 std::optional<uint64_t>
GetAttributeValueAsOptionalUnsigned(const DWARFUnit * cu,const dw_attr_t attr,bool check_elaborating_dies) const462 DWARFDebugInfoEntry::GetAttributeValueAsOptionalUnsigned(
463     const DWARFUnit *cu, const dw_attr_t attr,
464     bool check_elaborating_dies) const {
465   DWARFFormValue form_value;
466   if (GetAttributeValue(cu, attr, form_value, nullptr, check_elaborating_dies))
467     return form_value.Unsigned();
468   return std::nullopt;
469 }
470 
471 // GetAttributeValueAsReference
472 //
473 // Get the value of an attribute as reference and fix up and compile unit
474 // relative offsets as needed.
GetAttributeValueAsReference(const DWARFUnit * cu,const dw_attr_t attr,bool check_elaborating_dies) const475 DWARFDIE DWARFDebugInfoEntry::GetAttributeValueAsReference(
476     const DWARFUnit *cu, const dw_attr_t attr,
477     bool check_elaborating_dies) const {
478   DWARFFormValue form_value;
479   if (GetAttributeValue(cu, attr, form_value, nullptr, check_elaborating_dies))
480     return form_value.Reference();
481   return {};
482 }
483 
GetAttributeValueAsAddress(const DWARFUnit * cu,const dw_attr_t attr,uint64_t fail_value,bool check_elaborating_dies) const484 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress(
485     const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
486     bool check_elaborating_dies) const {
487   DWARFFormValue form_value;
488   if (GetAttributeValue(cu, attr, form_value, nullptr, check_elaborating_dies))
489     return form_value.Address();
490   return fail_value;
491 }
492 
493 // GetAttributeHighPC
494 //
495 // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low-
496 // pc>.
497 //
498 // Returns the hi_pc or fail_value.
499 dw_addr_t
GetAttributeHighPC(const DWARFUnit * cu,dw_addr_t lo_pc,uint64_t fail_value,bool check_elaborating_dies) const500 DWARFDebugInfoEntry::GetAttributeHighPC(const DWARFUnit *cu, dw_addr_t lo_pc,
501                                         uint64_t fail_value,
502                                         bool check_elaborating_dies) const {
503   DWARFFormValue form_value;
504   if (GetAttributeValue(cu, DW_AT_high_pc, form_value, nullptr,
505                         check_elaborating_dies)) {
506     dw_form_t form = form_value.Form();
507     if (form == DW_FORM_addr || form == DW_FORM_addrx ||
508         form == DW_FORM_GNU_addr_index)
509       return form_value.Address();
510 
511     // DWARF4 can specify the hi_pc as an <offset-from-lowpc>
512     return lo_pc + form_value.Unsigned();
513   }
514   return fail_value;
515 }
516 
517 // GetAttributeAddressRange
518 //
519 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset-
520 // from-low-pc>.
521 //
522 // Returns true or sets lo_pc and hi_pc to fail_value.
GetAttributeAddressRange(const DWARFUnit * cu,dw_addr_t & lo_pc,dw_addr_t & hi_pc,uint64_t fail_value,bool check_elaborating_dies) const523 bool DWARFDebugInfoEntry::GetAttributeAddressRange(
524     const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc,
525     uint64_t fail_value, bool check_elaborating_dies) const {
526   lo_pc = GetAttributeValueAsAddress(cu, DW_AT_low_pc, fail_value,
527                                      check_elaborating_dies);
528   if (lo_pc != fail_value) {
529     hi_pc = GetAttributeHighPC(cu, lo_pc, fail_value, check_elaborating_dies);
530     if (hi_pc != fail_value)
531       return true;
532   }
533   lo_pc = fail_value;
534   hi_pc = fail_value;
535   return false;
536 }
537 
538 llvm::Expected<llvm::DWARFAddressRangesVector>
GetAttributeAddressRanges(DWARFUnit * cu,bool check_hi_lo_pc,bool check_elaborating_dies) const539 DWARFDebugInfoEntry::GetAttributeAddressRanges(
540     DWARFUnit *cu, bool check_hi_lo_pc, bool check_elaborating_dies) const {
541 
542   DWARFFormValue form_value;
543   if (GetAttributeValue(cu, DW_AT_ranges, form_value))
544     return GetRanges(*cu, form_value);
545 
546   if (check_hi_lo_pc) {
547     dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
548     dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
549     if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS,
550                                  check_elaborating_dies) &&
551         lo_pc < hi_pc)
552       return llvm::DWARFAddressRangesVector{{lo_pc, hi_pc}};
553   }
554   return llvm::createStringError("DIE has no address range information");
555 }
556 
557 // GetName
558 //
559 // Get value of the DW_AT_name attribute and return it if one exists, else
560 // return NULL.
GetName(const DWARFUnit * cu) const561 const char *DWARFDebugInfoEntry::GetName(const DWARFUnit *cu) const {
562   return GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
563 }
564 
565 // GetMangledName
566 //
567 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one
568 // exists, else return the value of the DW_AT_name attribute
569 const char *
GetMangledName(const DWARFUnit * cu,bool substitute_name_allowed) const570 DWARFDebugInfoEntry::GetMangledName(const DWARFUnit *cu,
571                                     bool substitute_name_allowed) const {
572   const char *name = nullptr;
573 
574   name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true);
575   if (name)
576     return name;
577 
578   name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true);
579   if (name)
580     return name;
581 
582   if (!substitute_name_allowed)
583     return nullptr;
584 
585   name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
586   return name;
587 }
588 
589 // GetPubname
590 //
591 // Get value the name for a DIE as it should appear for a .debug_pubnames or
592 // .debug_pubtypes section.
GetPubname(const DWARFUnit * cu) const593 const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const {
594   const char *name = nullptr;
595   if (!cu)
596     return name;
597 
598   name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true);
599   if (name)
600     return name;
601 
602   name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true);
603   if (name)
604     return name;
605 
606   name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
607   return name;
608 }
609 
610 /// This function is builds a table very similar to the standard .debug_aranges
611 /// table, except that the actual DIE offset for the function is placed in the
612 /// table instead of the compile unit offset.
BuildFunctionAddressRangeTable(DWARFUnit * cu,DWARFDebugAranges * debug_aranges) const613 void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable(
614     DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const {
615   Log *log = GetLog(DWARFLog::DebugInfo);
616   if (m_tag) {
617     // Subprogram forward declarations don't have
618     // DW_AT_ranges/DW_AT_low_pc/DW_AT_high_pc attributes, so don't even try
619     // getting address range information for them.
620     if (m_tag == DW_TAG_subprogram &&
621         !GetAttributeValueAsOptionalUnsigned(cu, DW_AT_declaration)) {
622       if (llvm::Expected<llvm::DWARFAddressRangesVector> ranges =
623               GetAttributeAddressRanges(cu, /*check_hi_lo_pc=*/true)) {
624         for (const auto &r : *ranges)
625           debug_aranges->AppendRange(GetOffset(), r.LowPC, r.HighPC);
626       } else {
627         LLDB_LOG_ERROR(log, ranges.takeError(), "DIE({1:x}): {0}", GetOffset());
628       }
629     }
630 
631     const DWARFDebugInfoEntry *child = GetFirstChild();
632     while (child) {
633       child->BuildFunctionAddressRangeTable(cu, debug_aranges);
634       child = child->GetSibling();
635     }
636   }
637 }
638 
GetFirstAttributeOffset() const639 lldb::offset_t DWARFDebugInfoEntry::GetFirstAttributeOffset() const {
640   return GetOffset() + llvm::getULEB128Size(m_abbr_idx);
641 }
642 
643 const llvm::DWARFAbbreviationDeclaration *
GetAbbreviationDeclarationPtr(const DWARFUnit * cu) const644 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const {
645   if (!cu)
646     return nullptr;
647 
648   const llvm::DWARFAbbreviationDeclarationSet *abbrev_set =
649       cu->GetAbbreviations();
650   if (!abbrev_set)
651     return nullptr;
652 
653   return abbrev_set->getAbbreviationDeclaration(m_abbr_idx);
654 }
655 
IsGlobalOrStaticScopeVariable() const656 bool DWARFDebugInfoEntry::IsGlobalOrStaticScopeVariable() const {
657   if (Tag() != DW_TAG_variable && Tag() != DW_TAG_member)
658     return false;
659   const DWARFDebugInfoEntry *parent_die = GetParent();
660   while (parent_die != nullptr) {
661     switch (parent_die->Tag()) {
662     case DW_TAG_subprogram:
663     case DW_TAG_lexical_block:
664     case DW_TAG_inlined_subroutine:
665       return false;
666 
667     case DW_TAG_compile_unit:
668     case DW_TAG_partial_unit:
669       return true;
670 
671     default:
672       break;
673     }
674     parent_die = parent_die->GetParent();
675   }
676   return false;
677 }
678 
operator ==(const DWARFDebugInfoEntry & rhs) const679 bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const {
680   return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx &&
681          m_sibling_idx == rhs.m_sibling_idx &&
682          m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children &&
683          m_tag == rhs.m_tag;
684 }
685 
operator !=(const DWARFDebugInfoEntry & rhs) const686 bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const {
687   return !(*this == rhs);
688 }
689