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
Extract(const DWARFDataExtractor & data,const DWARFUnit * cu,lldb::offset_t * 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
GetRangesOrReportError(DWARFUnit & unit,const DWARFDebugInfoEntry & die,const DWARFFormValue & value)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.
GetDIENamesAndRanges(DWARFUnit * cu,const char * & name,const char * & mangled,DWARFRangeList & ranges,int & decl_file,int & decl_line,int & decl_column,int & call_file,int & call_line,int & call_column,DWARFExpression * frame_base) const226 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).
GetAttributes(DWARFUnit * cu,DWARFAttributes & attributes,Recurse recurse,uint32_t curr_depth) const402 size_t DWARFDebugInfoEntry::GetAttributes(DWARFUnit *cu,
403 DWARFAttributes &attributes,
404 Recurse recurse,
405 uint32_t curr_depth) const {
406 const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu);
407 if (abbrevDecl) {
408 const DWARFDataExtractor &data = cu->GetData();
409 lldb::offset_t offset = GetFirstAttributeOffset();
410
411 const uint32_t num_attributes = abbrevDecl->NumAttributes();
412 for (uint32_t i = 0; i < num_attributes; ++i) {
413 DWARFFormValue form_value(cu);
414 dw_attr_t attr;
415 abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
416 const dw_form_t form = form_value.Form();
417
418 // If we are tracking down DW_AT_specification or DW_AT_abstract_origin
419 // attributes, the depth will be non-zero. We need to omit certain
420 // attributes that don't make sense.
421 switch (attr) {
422 case DW_AT_sibling:
423 case DW_AT_declaration:
424 if (curr_depth > 0) {
425 // This attribute doesn't make sense when combined with the DIE that
426 // references this DIE. We know a DIE is referencing this DIE because
427 // curr_depth is not zero
428 break;
429 }
430 LLVM_FALLTHROUGH;
431 default:
432 attributes.Append(cu, offset, attr, form);
433 break;
434 }
435
436 if (recurse == Recurse::yes &&
437 ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin))) {
438 if (form_value.ExtractValue(data, &offset)) {
439 DWARFDIE spec_die = form_value.Reference();
440 if (spec_die)
441 spec_die.GetDIE()->GetAttributes(spec_die.GetCU(), attributes,
442 recurse, curr_depth + 1);
443 }
444 } else {
445 llvm::Optional<uint8_t> fixed_skip_size = DWARFFormValue::GetFixedSize(form, cu);
446 if (fixed_skip_size)
447 offset += *fixed_skip_size;
448 else
449 DWARFFormValue::SkipValue(form, data, &offset, cu);
450 }
451 }
452 } else {
453 attributes.Clear();
454 }
455 return attributes.Size();
456 }
457
458 // GetAttributeValue
459 //
460 // Get the value of an attribute and return the .debug_info or .debug_types
461 // offset of the attribute if it was properly extracted into form_value,
462 // or zero if we fail since an offset of zero is invalid for an attribute (it
463 // 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_specification_or_abstract_origin) const464 dw_offset_t DWARFDebugInfoEntry::GetAttributeValue(
465 const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &form_value,
466 dw_offset_t *end_attr_offset_ptr,
467 bool check_specification_or_abstract_origin) const {
468 if (const auto *abbrevDecl = GetAbbreviationDeclarationPtr(cu)) {
469 uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr);
470
471 if (attr_idx != DW_INVALID_INDEX) {
472 const DWARFDataExtractor &data = cu->GetData();
473 lldb::offset_t offset = GetFirstAttributeOffset();
474
475 uint32_t idx = 0;
476 while (idx < attr_idx)
477 DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++),
478 data, &offset, cu);
479
480 const dw_offset_t attr_offset = offset;
481 form_value.SetUnit(cu);
482 form_value.SetForm(abbrevDecl->GetFormByIndex(idx));
483 if (form_value.ExtractValue(data, &offset)) {
484 if (end_attr_offset_ptr)
485 *end_attr_offset_ptr = offset;
486 return attr_offset;
487 }
488 }
489 }
490
491 if (check_specification_or_abstract_origin) {
492 if (GetAttributeValue(cu, DW_AT_specification, form_value)) {
493 DWARFDIE die = form_value.Reference();
494 if (die) {
495 dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
496 die.GetCU(), attr, form_value, end_attr_offset_ptr, false);
497 if (die_offset)
498 return die_offset;
499 }
500 }
501
502 if (GetAttributeValue(cu, DW_AT_abstract_origin, form_value)) {
503 DWARFDIE die = form_value.Reference();
504 if (die) {
505 dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
506 die.GetCU(), attr, form_value, end_attr_offset_ptr, false);
507 if (die_offset)
508 return die_offset;
509 }
510 }
511 }
512 return 0;
513 }
514
515 // GetAttributeValueAsString
516 //
517 // Get the value of an attribute as a string return it. The resulting pointer
518 // to the string data exists within the supplied SymbolFileDWARF and will only
519 // be available as long as the SymbolFileDWARF is still around and it's content
520 // doesn't change.
GetAttributeValueAsString(const DWARFUnit * cu,const dw_attr_t attr,const char * fail_value,bool check_specification_or_abstract_origin) const521 const char *DWARFDebugInfoEntry::GetAttributeValueAsString(
522 const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value,
523 bool check_specification_or_abstract_origin) const {
524 DWARFFormValue form_value;
525 if (GetAttributeValue(cu, attr, form_value, nullptr,
526 check_specification_or_abstract_origin))
527 return form_value.AsCString();
528 return fail_value;
529 }
530
531 // GetAttributeValueAsUnsigned
532 //
533 // 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_specification_or_abstract_origin) const534 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned(
535 const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
536 bool check_specification_or_abstract_origin) const {
537 DWARFFormValue form_value;
538 if (GetAttributeValue(cu, attr, form_value, nullptr,
539 check_specification_or_abstract_origin))
540 return form_value.Unsigned();
541 return fail_value;
542 }
543
544 // GetAttributeValueAsReference
545 //
546 // Get the value of an attribute as reference and fix up and compile unit
547 // relative offsets as needed.
GetAttributeValueAsReference(const DWARFUnit * cu,const dw_attr_t attr,bool check_specification_or_abstract_origin) const548 DWARFDIE DWARFDebugInfoEntry::GetAttributeValueAsReference(
549 const DWARFUnit *cu, const dw_attr_t attr,
550 bool check_specification_or_abstract_origin) const {
551 DWARFFormValue form_value;
552 if (GetAttributeValue(cu, attr, form_value, nullptr,
553 check_specification_or_abstract_origin))
554 return form_value.Reference();
555 return {};
556 }
557
GetAttributeValueAsAddress(const DWARFUnit * cu,const dw_attr_t attr,uint64_t fail_value,bool check_specification_or_abstract_origin) const558 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress(
559 const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
560 bool check_specification_or_abstract_origin) const {
561 DWARFFormValue form_value;
562 if (GetAttributeValue(cu, attr, form_value, nullptr,
563 check_specification_or_abstract_origin))
564 return form_value.Address();
565 return fail_value;
566 }
567
568 // GetAttributeHighPC
569 //
570 // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low-
571 // pc>.
572 //
573 // Returns the hi_pc or fail_value.
GetAttributeHighPC(const DWARFUnit * cu,dw_addr_t lo_pc,uint64_t fail_value,bool check_specification_or_abstract_origin) const574 dw_addr_t DWARFDebugInfoEntry::GetAttributeHighPC(
575 const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value,
576 bool check_specification_or_abstract_origin) const {
577 DWARFFormValue form_value;
578 if (GetAttributeValue(cu, DW_AT_high_pc, form_value, nullptr,
579 check_specification_or_abstract_origin)) {
580 dw_form_t form = form_value.Form();
581 if (form == DW_FORM_addr || form == DW_FORM_addrx ||
582 form == DW_FORM_GNU_addr_index)
583 return form_value.Address();
584
585 // DWARF4 can specify the hi_pc as an <offset-from-lowpc>
586 return lo_pc + form_value.Unsigned();
587 }
588 return fail_value;
589 }
590
591 // GetAttributeAddressRange
592 //
593 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset-
594 // from-low-pc>.
595 //
596 // 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_specification_or_abstract_origin) const597 bool DWARFDebugInfoEntry::GetAttributeAddressRange(
598 const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc,
599 uint64_t fail_value, bool check_specification_or_abstract_origin) const {
600 lo_pc = GetAttributeValueAsAddress(cu, DW_AT_low_pc, fail_value,
601 check_specification_or_abstract_origin);
602 if (lo_pc != fail_value) {
603 hi_pc = GetAttributeHighPC(cu, lo_pc, fail_value,
604 check_specification_or_abstract_origin);
605 if (hi_pc != fail_value)
606 return true;
607 }
608 lo_pc = fail_value;
609 hi_pc = fail_value;
610 return false;
611 }
612
GetAttributeAddressRanges(DWARFUnit * cu,DWARFRangeList & ranges,bool check_hi_lo_pc,bool check_specification_or_abstract_origin) const613 size_t DWARFDebugInfoEntry::GetAttributeAddressRanges(
614 DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc,
615 bool check_specification_or_abstract_origin) const {
616 ranges.Clear();
617
618 DWARFFormValue form_value;
619 if (GetAttributeValue(cu, DW_AT_ranges, form_value)) {
620 ranges = GetRangesOrReportError(*cu, *this, form_value);
621 } else if (check_hi_lo_pc) {
622 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
623 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
624 if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS,
625 check_specification_or_abstract_origin)) {
626 if (lo_pc < hi_pc)
627 ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
628 }
629 }
630 return ranges.GetSize();
631 }
632
633 // GetName
634 //
635 // Get value of the DW_AT_name attribute and return it if one exists, else
636 // return NULL.
GetName(const DWARFUnit * cu) const637 const char *DWARFDebugInfoEntry::GetName(const DWARFUnit *cu) const {
638 return GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
639 }
640
641 // GetMangledName
642 //
643 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one
644 // exists, else return the value of the DW_AT_name attribute
645 const char *
GetMangledName(const DWARFUnit * cu,bool substitute_name_allowed) const646 DWARFDebugInfoEntry::GetMangledName(const DWARFUnit *cu,
647 bool substitute_name_allowed) const {
648 const char *name = nullptr;
649
650 name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true);
651 if (name)
652 return name;
653
654 name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true);
655 if (name)
656 return name;
657
658 if (!substitute_name_allowed)
659 return nullptr;
660
661 name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
662 return name;
663 }
664
665 // GetPubname
666 //
667 // Get value the name for a DIE as it should appear for a .debug_pubnames or
668 // .debug_pubtypes section.
GetPubname(const DWARFUnit * cu) const669 const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const {
670 const char *name = nullptr;
671 if (!cu)
672 return name;
673
674 name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true);
675 if (name)
676 return name;
677
678 name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true);
679 if (name)
680 return name;
681
682 name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true);
683 return name;
684 }
685
686 /// This function is builds a table very similar to the standard .debug_aranges
687 /// table, except that the actual DIE offset for the function is placed in the
688 /// table instead of the compile unit offset.
BuildFunctionAddressRangeTable(const DWARFUnit * cu,DWARFDebugAranges * debug_aranges) const689 void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable(
690 const DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const {
691 if (m_tag) {
692 if (m_tag == DW_TAG_subprogram) {
693 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
694 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
695 if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS)) {
696 debug_aranges->AppendRange(GetOffset(), lo_pc, hi_pc);
697 }
698 }
699
700 const DWARFDebugInfoEntry *child = GetFirstChild();
701 while (child) {
702 child->BuildFunctionAddressRangeTable(cu, debug_aranges);
703 child = child->GetSibling();
704 }
705 }
706 }
707
708 DWARFDeclContext
GetDWARFDeclContextStatic(const DWARFDebugInfoEntry * die,DWARFUnit * cu)709 DWARFDebugInfoEntry::GetDWARFDeclContextStatic(const DWARFDebugInfoEntry *die,
710 DWARFUnit *cu) {
711 DWARFDeclContext dwarf_decl_ctx;
712 for (;;) {
713 const dw_tag_t tag = die->Tag();
714 if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
715 return dwarf_decl_ctx;
716 dwarf_decl_ctx.AppendDeclContext(tag, die->GetName(cu));
717 DWARFDIE parent_decl_ctx_die = die->GetParentDeclContextDIE(cu);
718 if (!parent_decl_ctx_die || parent_decl_ctx_die.GetDIE() == die)
719 return dwarf_decl_ctx;
720 if (parent_decl_ctx_die.Tag() == DW_TAG_compile_unit ||
721 parent_decl_ctx_die.Tag() == DW_TAG_partial_unit)
722 return dwarf_decl_ctx;
723 die = parent_decl_ctx_die.GetDIE();
724 cu = parent_decl_ctx_die.GetCU();
725 }
726 }
727
GetDWARFDeclContext(DWARFUnit * cu) const728 DWARFDeclContext DWARFDebugInfoEntry::GetDWARFDeclContext(DWARFUnit *cu) const {
729 return GetDWARFDeclContextStatic(this, cu);
730 }
731
732 DWARFDIE
GetParentDeclContextDIE(DWARFUnit * cu) const733 DWARFDebugInfoEntry::GetParentDeclContextDIE(DWARFUnit *cu) const {
734 DWARFAttributes attributes;
735 GetAttributes(cu, attributes, Recurse::yes);
736 return GetParentDeclContextDIE(cu, attributes);
737 }
738
739 DWARFDIE
GetParentDeclContextDIE(DWARFUnit * cu,const DWARFAttributes & attributes) const740 DWARFDebugInfoEntry::GetParentDeclContextDIE(
741 DWARFUnit *cu, const DWARFAttributes &attributes) const {
742 DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this));
743
744 while (die) {
745 // If this is the original DIE that we are searching for a declaration for,
746 // then don't look in the cache as we don't want our own decl context to be
747 // our decl context...
748 if (die.GetDIE() != this) {
749 switch (die.Tag()) {
750 case DW_TAG_compile_unit:
751 case DW_TAG_partial_unit:
752 case DW_TAG_namespace:
753 case DW_TAG_structure_type:
754 case DW_TAG_union_type:
755 case DW_TAG_class_type:
756 return die;
757
758 default:
759 break;
760 }
761 }
762
763 DWARFDIE spec_die = attributes.FormValueAsReference(DW_AT_specification);
764 if (spec_die) {
765 DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE();
766 if (decl_ctx_die)
767 return decl_ctx_die;
768 }
769
770 DWARFDIE abs_die = attributes.FormValueAsReference(DW_AT_abstract_origin);
771 if (abs_die) {
772 DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE();
773 if (decl_ctx_die)
774 return decl_ctx_die;
775 }
776
777 die = die.GetParent();
778 }
779 return DWARFDIE();
780 }
781
GetQualifiedName(DWARFUnit * cu,std::string & storage) const782 const char *DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu,
783 std::string &storage) const {
784 DWARFAttributes attributes;
785 GetAttributes(cu, attributes, Recurse::yes);
786 return GetQualifiedName(cu, attributes, storage);
787 }
788
789 const char *
GetQualifiedName(DWARFUnit * cu,const DWARFAttributes & attributes,std::string & storage) const790 DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu,
791 const DWARFAttributes &attributes,
792 std::string &storage) const {
793
794 const char *name = GetName(cu);
795
796 if (name) {
797 DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu);
798 storage.clear();
799 // TODO: change this to get the correct decl context parent....
800 while (parent_decl_ctx_die) {
801 const dw_tag_t parent_tag = parent_decl_ctx_die.Tag();
802 switch (parent_tag) {
803 case DW_TAG_namespace: {
804 const char *namespace_name = parent_decl_ctx_die.GetName();
805 if (namespace_name) {
806 storage.insert(0, "::");
807 storage.insert(0, namespace_name);
808 } else {
809 storage.insert(0, "(anonymous namespace)::");
810 }
811 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
812 } break;
813
814 case DW_TAG_class_type:
815 case DW_TAG_structure_type:
816 case DW_TAG_union_type: {
817 const char *class_union_struct_name = parent_decl_ctx_die.GetName();
818
819 if (class_union_struct_name) {
820 storage.insert(0, "::");
821 storage.insert(0, class_union_struct_name);
822 }
823 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
824 } break;
825
826 default:
827 parent_decl_ctx_die.Clear();
828 break;
829 }
830 }
831
832 if (storage.empty())
833 storage.append("::");
834
835 storage.append(name);
836 }
837 if (storage.empty())
838 return nullptr;
839 return storage.c_str();
840 }
841
GetFirstAttributeOffset() const842 lldb::offset_t DWARFDebugInfoEntry::GetFirstAttributeOffset() const {
843 return GetOffset() + llvm::getULEB128Size(m_abbr_idx);
844 }
845
846 const DWARFAbbreviationDeclaration *
GetAbbreviationDeclarationPtr(const DWARFUnit * cu) const847 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const {
848 if (cu) {
849 const DWARFAbbreviationDeclarationSet *abbrev_set = cu->GetAbbreviations();
850 if (abbrev_set)
851 return abbrev_set->GetAbbreviationDeclaration(m_abbr_idx);
852 }
853 return nullptr;
854 }
855
IsGlobalOrStaticScopeVariable() const856 bool DWARFDebugInfoEntry::IsGlobalOrStaticScopeVariable() const {
857 if (Tag() != DW_TAG_variable)
858 return false;
859 const DWARFDebugInfoEntry *parent_die = GetParent();
860 while (parent_die != nullptr) {
861 switch (parent_die->Tag()) {
862 case DW_TAG_subprogram:
863 case DW_TAG_lexical_block:
864 case DW_TAG_inlined_subroutine:
865 return false;
866
867 case DW_TAG_compile_unit:
868 case DW_TAG_partial_unit:
869 return true;
870
871 default:
872 break;
873 }
874 parent_die = parent_die->GetParent();
875 }
876 return false;
877 }
878
operator ==(const DWARFDebugInfoEntry & rhs) const879 bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const {
880 return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx &&
881 m_sibling_idx == rhs.m_sibling_idx &&
882 m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children &&
883 m_tag == rhs.m_tag;
884 }
885
operator !=(const DWARFDebugInfoEntry & rhs) const886 bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const {
887 return !(*this == rhs);
888 }
889