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 |
43 | bool DWARFDebugInfoEntry::(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(cu: &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(form: attribute.Form, debug_info_data: data, offset_ptr, unit: &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> |
86 | GetRanges(DWARFUnit &unit, const DWARFFormValue &value) { |
87 | return (value.Form() == DW_FORM_rnglistx) |
88 | ? unit.FindRnglistFromIndex(index: value.Unsigned()) |
89 | : unit.FindRnglistFromOffset(offset: value.Unsigned()); |
90 | } |
91 | |
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. |
105 | 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(attr_spec: attribute, attr, form_value); |
132 | |
133 | if (form_value.ExtractValue(data, offset_ptr: &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(unit&: *cu, value: form_value)) { |
165 | ranges = std::move(*r); |
166 | } else { |
167 | module->ReportError( |
168 | format: "[{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 | args: GetOffset(), args: llvm::dwarf::FormEncodingString(Encoding: form_value.Form()), |
172 | args: form_value.Unsigned(), args: fmt_consume(Item: 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(x: form_value.Reference()); |
189 | break; |
190 | |
191 | case DW_AT_specification: |
192 | dies.push_back(x: 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, loc_list&: *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(args&: lo_pc, args&: 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(cu: 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. |
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(cu: 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(attr_spec: 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, attr_die_offset: offset, attr); |
332 | break; |
333 | } |
334 | |
335 | if (attr == DW_AT_specification || attr == DW_AT_abstract_origin) { |
336 | if (form_value.ExtractValue(data, offset_ptr: &offset)) { |
337 | if (DWARFDIE spec_die = form_value.Reference()) { |
338 | if (seen.insert(Ptr: spec_die.GetDIE()).second) |
339 | worklist.push_back(Elt: 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, u: cu); |
346 | if (fixed_skip_size) |
347 | offset += *fixed_skip_size; |
348 | else |
349 | DWARFFormValue::SkipValue(form, debug_info_data: data, offset_ptr: &offset, unit: cu); |
350 | } |
351 | } |
352 | |
353 | return true; |
354 | } |
355 | |
356 | 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(Args&: cu, Args: 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(Ptr: 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). |
388 | 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(form: abbrevDecl->getFormByIndex(idx: idx++), debug_info_data: data, |
401 | offset_ptr: &offset, unit: cu); |
402 | |
403 | const dw_offset_t attr_offset = offset; |
404 | form_value.SetUnit(cu); |
405 | form_value.SetForm(abbrevDecl->getFormByIndex(idx)); |
406 | if (form_value.ExtractValue(data, offset_ptr: &offset)) { |
407 | if (end_attr_offset_ptr) |
408 | *end_attr_offset_ptr = offset; |
409 | return attr_offset; |
410 | } |
411 | } |
412 | } |
413 | |
414 | if (check_elaborating_dies) { |
415 | for (dw_attr_t elaborating_attr : |
416 | {DW_AT_specification, DW_AT_abstract_origin, DW_AT_signature}) { |
417 | if (!GetAttributeValue(cu, attr: elaborating_attr, form_value)) |
418 | continue; |
419 | DWARFDIE die = form_value.Reference(); |
420 | if (!die) |
421 | continue; |
422 | dw_offset_t die_offset = die.GetDIE()->GetAttributeValue( |
423 | cu: die.GetCU(), attr, form_value, end_attr_offset_ptr, check_elaborating_dies: false); |
424 | if (die_offset) |
425 | return die_offset; |
426 | } |
427 | } |
428 | return 0; |
429 | } |
430 | |
431 | // GetAttributeValueAsString |
432 | // |
433 | // Get the value of an attribute as a string return it. The resulting pointer |
434 | // to the string data exists within the supplied SymbolFileDWARF and will only |
435 | // be available as long as the SymbolFileDWARF is still around and it's content |
436 | // doesn't change. |
437 | const char *DWARFDebugInfoEntry::GetAttributeValueAsString( |
438 | const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value, |
439 | bool check_elaborating_dies) const { |
440 | DWARFFormValue form_value; |
441 | if (GetAttributeValue(cu, attr, form_value, end_attr_offset_ptr: nullptr, check_elaborating_dies)) |
442 | return form_value.AsCString(); |
443 | return fail_value; |
444 | } |
445 | |
446 | // GetAttributeValueAsUnsigned |
447 | // |
448 | // Get the value of an attribute as unsigned and return it. |
449 | uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned( |
450 | const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, |
451 | bool check_elaborating_dies) const { |
452 | DWARFFormValue form_value; |
453 | if (GetAttributeValue(cu, attr, form_value, end_attr_offset_ptr: nullptr, check_elaborating_dies)) |
454 | return form_value.Unsigned(); |
455 | return fail_value; |
456 | } |
457 | |
458 | std::optional<uint64_t> |
459 | DWARFDebugInfoEntry::GetAttributeValueAsOptionalUnsigned( |
460 | const DWARFUnit *cu, const dw_attr_t attr, |
461 | bool check_elaborating_dies) const { |
462 | DWARFFormValue form_value; |
463 | if (GetAttributeValue(cu, attr, form_value, end_attr_offset_ptr: nullptr, check_elaborating_dies)) |
464 | return form_value.Unsigned(); |
465 | return std::nullopt; |
466 | } |
467 | |
468 | // GetAttributeValueAsReference |
469 | // |
470 | // Get the value of an attribute as reference and fix up and compile unit |
471 | // relative offsets as needed. |
472 | DWARFDIE DWARFDebugInfoEntry::GetAttributeValueAsReference( |
473 | const DWARFUnit *cu, const dw_attr_t attr, |
474 | bool check_elaborating_dies) const { |
475 | DWARFFormValue form_value; |
476 | if (GetAttributeValue(cu, attr, form_value, end_attr_offset_ptr: nullptr, check_elaborating_dies)) |
477 | return form_value.Reference(); |
478 | return {}; |
479 | } |
480 | |
481 | uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress( |
482 | const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, |
483 | bool check_elaborating_dies) const { |
484 | DWARFFormValue form_value; |
485 | if (GetAttributeValue(cu, attr, form_value, end_attr_offset_ptr: nullptr, check_elaborating_dies)) |
486 | return form_value.Address(); |
487 | return fail_value; |
488 | } |
489 | |
490 | // GetAttributeHighPC |
491 | // |
492 | // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low- |
493 | // pc>. |
494 | // |
495 | // Returns the hi_pc or fail_value. |
496 | dw_addr_t |
497 | DWARFDebugInfoEntry::GetAttributeHighPC(const DWARFUnit *cu, dw_addr_t lo_pc, |
498 | uint64_t fail_value, |
499 | bool check_elaborating_dies) const { |
500 | DWARFFormValue form_value; |
501 | if (GetAttributeValue(cu, attr: DW_AT_high_pc, form_value, end_attr_offset_ptr: nullptr, |
502 | check_elaborating_dies)) { |
503 | dw_form_t form = form_value.Form(); |
504 | if (form == DW_FORM_addr || form == DW_FORM_addrx || |
505 | form == DW_FORM_GNU_addr_index) |
506 | return form_value.Address(); |
507 | |
508 | // DWARF4 can specify the hi_pc as an <offset-from-lowpc> |
509 | return lo_pc + form_value.Unsigned(); |
510 | } |
511 | return fail_value; |
512 | } |
513 | |
514 | // GetAttributeAddressRange |
515 | // |
516 | // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset- |
517 | // from-low-pc>. |
518 | // |
519 | // Returns true or sets lo_pc and hi_pc to fail_value. |
520 | bool DWARFDebugInfoEntry::GetAttributeAddressRange( |
521 | const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc, |
522 | uint64_t fail_value, bool check_elaborating_dies) const { |
523 | lo_pc = GetAttributeValueAsAddress(cu, attr: DW_AT_low_pc, fail_value, |
524 | check_elaborating_dies); |
525 | if (lo_pc != fail_value) { |
526 | hi_pc = GetAttributeHighPC(cu, lo_pc, fail_value, check_elaborating_dies); |
527 | if (hi_pc != fail_value) |
528 | return true; |
529 | } |
530 | lo_pc = fail_value; |
531 | hi_pc = fail_value; |
532 | return false; |
533 | } |
534 | |
535 | llvm::Expected<llvm::DWARFAddressRangesVector> |
536 | DWARFDebugInfoEntry::GetAttributeAddressRanges( |
537 | DWARFUnit *cu, bool check_hi_lo_pc, bool check_elaborating_dies) const { |
538 | |
539 | DWARFFormValue form_value; |
540 | if (GetAttributeValue(cu, attr: DW_AT_ranges, form_value)) |
541 | return GetRanges(unit&: *cu, value: form_value); |
542 | |
543 | if (check_hi_lo_pc) { |
544 | dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; |
545 | dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; |
546 | if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS, |
547 | check_elaborating_dies) && |
548 | lo_pc < hi_pc) |
549 | return llvm::DWARFAddressRangesVector{{lo_pc, hi_pc}}; |
550 | } |
551 | return llvm::createStringError(Fmt: "DIE has no address range information" ); |
552 | } |
553 | |
554 | // GetName |
555 | // |
556 | // Get value of the DW_AT_name attribute and return it if one exists, else |
557 | // return NULL. |
558 | const char *DWARFDebugInfoEntry::GetName(const DWARFUnit *cu) const { |
559 | return GetAttributeValueAsString(cu, attr: DW_AT_name, fail_value: nullptr, check_elaborating_dies: true); |
560 | } |
561 | |
562 | // GetMangledName |
563 | // |
564 | // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one |
565 | // exists, else return the value of the DW_AT_name attribute |
566 | const char * |
567 | DWARFDebugInfoEntry::GetMangledName(const DWARFUnit *cu, |
568 | bool substitute_name_allowed) const { |
569 | const char *name = nullptr; |
570 | |
571 | name = GetAttributeValueAsString(cu, attr: DW_AT_MIPS_linkage_name, fail_value: nullptr, check_elaborating_dies: true); |
572 | if (name) |
573 | return name; |
574 | |
575 | name = GetAttributeValueAsString(cu, attr: DW_AT_linkage_name, fail_value: nullptr, check_elaborating_dies: true); |
576 | if (name) |
577 | return name; |
578 | |
579 | if (!substitute_name_allowed) |
580 | return nullptr; |
581 | |
582 | name = GetAttributeValueAsString(cu, attr: DW_AT_name, fail_value: nullptr, check_elaborating_dies: true); |
583 | return name; |
584 | } |
585 | |
586 | // GetPubname |
587 | // |
588 | // Get value the name for a DIE as it should appear for a .debug_pubnames or |
589 | // .debug_pubtypes section. |
590 | const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const { |
591 | const char *name = nullptr; |
592 | if (!cu) |
593 | return name; |
594 | |
595 | name = GetAttributeValueAsString(cu, attr: DW_AT_MIPS_linkage_name, fail_value: nullptr, check_elaborating_dies: true); |
596 | if (name) |
597 | return name; |
598 | |
599 | name = GetAttributeValueAsString(cu, attr: DW_AT_linkage_name, fail_value: nullptr, check_elaborating_dies: true); |
600 | if (name) |
601 | return name; |
602 | |
603 | name = GetAttributeValueAsString(cu, attr: DW_AT_name, fail_value: nullptr, check_elaborating_dies: true); |
604 | return name; |
605 | } |
606 | |
607 | /// This function is builds a table very similar to the standard .debug_aranges |
608 | /// table, except that the actual DIE offset for the function is placed in the |
609 | /// table instead of the compile unit offset. |
610 | void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable( |
611 | DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const { |
612 | Log *log = GetLog(mask: DWARFLog::DebugInfo); |
613 | if (m_tag) { |
614 | if (m_tag == DW_TAG_subprogram) { |
615 | if (llvm::Expected<llvm::DWARFAddressRangesVector> ranges = |
616 | GetAttributeAddressRanges(cu, /*check_hi_lo_pc=*/true)) { |
617 | for (const auto &r : *ranges) |
618 | debug_aranges->AppendRange(cu_offset: GetOffset(), low_pc: r.LowPC, high_pc: r.HighPC); |
619 | } else { |
620 | LLDB_LOG_ERROR(log, ranges.takeError(), "DIE({1:x}): {0}" , GetOffset()); |
621 | } |
622 | } |
623 | |
624 | const DWARFDebugInfoEntry *child = GetFirstChild(); |
625 | while (child) { |
626 | child->BuildFunctionAddressRangeTable(cu, debug_aranges); |
627 | child = child->GetSibling(); |
628 | } |
629 | } |
630 | } |
631 | |
632 | lldb::offset_t DWARFDebugInfoEntry::GetFirstAttributeOffset() const { |
633 | return GetOffset() + llvm::getULEB128Size(Value: m_abbr_idx); |
634 | } |
635 | |
636 | const llvm::DWARFAbbreviationDeclaration * |
637 | DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const { |
638 | if (!cu) |
639 | return nullptr; |
640 | |
641 | const llvm::DWARFAbbreviationDeclarationSet *abbrev_set = |
642 | cu->GetAbbreviations(); |
643 | if (!abbrev_set) |
644 | return nullptr; |
645 | |
646 | return abbrev_set->getAbbreviationDeclaration(AbbrCode: m_abbr_idx); |
647 | } |
648 | |
649 | bool DWARFDebugInfoEntry::IsGlobalOrStaticScopeVariable() const { |
650 | if (Tag() != DW_TAG_variable && Tag() != DW_TAG_member) |
651 | return false; |
652 | const DWARFDebugInfoEntry *parent_die = GetParent(); |
653 | while (parent_die != nullptr) { |
654 | switch (parent_die->Tag()) { |
655 | case DW_TAG_subprogram: |
656 | case DW_TAG_lexical_block: |
657 | case DW_TAG_inlined_subroutine: |
658 | return false; |
659 | |
660 | case DW_TAG_compile_unit: |
661 | case DW_TAG_partial_unit: |
662 | return true; |
663 | |
664 | default: |
665 | break; |
666 | } |
667 | parent_die = parent_die->GetParent(); |
668 | } |
669 | return false; |
670 | } |
671 | |
672 | bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const { |
673 | return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx && |
674 | m_sibling_idx == rhs.m_sibling_idx && |
675 | m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children && |
676 | m_tag == rhs.m_tag; |
677 | } |
678 | |
679 | bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const { |
680 | return !(*this == rhs); |
681 | } |
682 | |