LLDB mainline
DWARFCallFrameInfo.cpp
Go to the documentation of this file.
1//===-- DWARFCallFrameInfo.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
10#include "lldb/Core/Debugger.h"
11#include "lldb/Core/Module.h"
12#include "lldb/Core/Section.h"
13#include "lldb/Core/dwarf.h"
14#include "lldb/Host/Host.h"
18#include "lldb/Target/Thread.h"
21#include "lldb/Utility/Log.h"
22#include "lldb/Utility/Timer.h"
23#include <cstring>
24#include <list>
25#include <optional>
26
27using namespace lldb;
28using namespace lldb_private;
29using namespace lldb_private::dwarf;
30
31// GetDwarfEHPtr
32//
33// Used for calls when the value type is specified by a DWARF EH Frame pointer
34// encoding.
35static uint64_t
36GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr,
37 uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr,
38 addr_t data_addr) //, BSDRelocs *data_relocs) const
39{
40 if (eh_ptr_enc == DW_EH_PE_omit)
41 return ULLONG_MAX; // Value isn't in the buffer...
42
43 uint64_t baseAddress = 0;
44 uint64_t addressValue = 0;
45 const uint32_t addr_size = DE.GetAddressByteSize();
46 assert(addr_size == 4 || addr_size == 8);
47
48 bool signExtendValue = false;
49 // Decode the base part or adjust our offset
50 switch (eh_ptr_enc & 0x70) {
51 case DW_EH_PE_pcrel:
52 signExtendValue = true;
53 baseAddress = *offset_ptr;
54 if (pc_rel_addr != LLDB_INVALID_ADDRESS)
55 baseAddress += pc_rel_addr;
56 // else
57 // Log::GlobalWarning ("PC relative pointer encoding found with
58 // invalid pc relative address.");
59 break;
60
61 case DW_EH_PE_textrel:
62 signExtendValue = true;
63 if (text_addr != LLDB_INVALID_ADDRESS)
64 baseAddress = text_addr;
65 // else
66 // Log::GlobalWarning ("text relative pointer encoding being
67 // decoded with invalid text section address, setting base address
68 // to zero.");
69 break;
70
71 case DW_EH_PE_datarel:
72 signExtendValue = true;
73 if (data_addr != LLDB_INVALID_ADDRESS)
74 baseAddress = data_addr;
75 // else
76 // Log::GlobalWarning ("data relative pointer encoding being
77 // decoded with invalid data section address, setting base address
78 // to zero.");
79 break;
80
81 case DW_EH_PE_funcrel:
82 signExtendValue = true;
83 break;
84
85 case DW_EH_PE_aligned: {
86 // SetPointerSize should be called prior to extracting these so the pointer
87 // size is cached
88 assert(addr_size != 0);
89 if (addr_size) {
90 // Align to a address size boundary first
91 uint32_t alignOffset = *offset_ptr % addr_size;
92 if (alignOffset)
93 offset_ptr += addr_size - alignOffset;
94 }
95 } break;
96
97 default:
98 break;
99 }
100
101 // Decode the value part
102 switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) {
103 case DW_EH_PE_absptr: {
104 addressValue = DE.GetAddress(offset_ptr);
105 // if (data_relocs)
106 // addressValue = data_relocs->Relocate(*offset_ptr -
107 // addr_size, *this, addressValue);
108 } break;
109 case DW_EH_PE_uleb128:
110 addressValue = DE.GetULEB128(offset_ptr);
111 break;
112 case DW_EH_PE_udata2:
113 addressValue = DE.GetU16(offset_ptr);
114 break;
115 case DW_EH_PE_udata4:
116 addressValue = DE.GetU32(offset_ptr);
117 break;
118 case DW_EH_PE_udata8:
119 addressValue = DE.GetU64(offset_ptr);
120 break;
121 case DW_EH_PE_sleb128:
122 addressValue = DE.GetSLEB128(offset_ptr);
123 break;
124 case DW_EH_PE_sdata2:
125 addressValue = (int16_t)DE.GetU16(offset_ptr);
126 break;
127 case DW_EH_PE_sdata4:
128 addressValue = (int32_t)DE.GetU32(offset_ptr);
129 break;
130 case DW_EH_PE_sdata8:
131 addressValue = (int64_t)DE.GetU64(offset_ptr);
132 break;
133 default:
134 // Unhandled encoding type
135 assert(eh_ptr_enc);
136 break;
137 }
138
139 // Since we promote everything to 64 bit, we may need to sign extend
140 if (signExtendValue && addr_size < sizeof(baseAddress)) {
141 uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
142 if (sign_bit & addressValue) {
143 uint64_t mask = ~sign_bit + 1;
144 addressValue |= mask;
145 }
146 }
147 return baseAddress + addressValue;
148}
149
151 SectionSP &section_sp, Type type)
152 : m_objfile(objfile), m_section_sp(section_sp), m_type(type) {}
153
155 UnwindPlan &unwind_plan) {
156 return GetUnwindPlan(AddressRange(addr, 1), unwind_plan);
157}
158
160 UnwindPlan &unwind_plan) {
161 FDEEntryMap::Entry fde_entry;
162 Address addr = range.GetBaseAddress();
163
164 // Make sure that the Address we're searching for is the same object file as
165 // this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
166 ModuleSP module_sp = addr.GetModule();
167 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
168 module_sp->GetObjectFile() != &m_objfile)
169 return false;
170
171 if (std::optional<FDEEntryMap::Entry> entry = GetFirstFDEEntryInRange(range))
172 return FDEToUnwindPlan(entry->data, addr, unwind_plan);
173 return false;
174}
175
177
178 // Make sure that the Address we're searching for is the same object file as
179 // this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
180 ModuleSP module_sp = addr.GetModule();
181 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
182 module_sp->GetObjectFile() != &m_objfile)
183 return false;
184
185 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
186 return false;
187 GetFDEIndex();
188 FDEEntryMap::Entry *fde_entry =
190 if (!fde_entry)
191 return false;
192
193 range = AddressRange(fde_entry->base, fde_entry->size,
195 return true;
196}
197
198std::optional<DWARFCallFrameInfo::FDEEntryMap::Entry>
200 if (!m_section_sp || m_section_sp->IsEncrypted())
201 return std::nullopt;
202
203 GetFDEIndex();
204
205 addr_t start_file_addr = range.GetBaseAddress().GetFileAddress();
206 const FDEEntryMap::Entry *fde =
208 if (fde && fde->DoesIntersect(
209 FDEEntryMap::Range(start_file_addr, range.GetByteSize())))
210 return *fde;
211
212 return std::nullopt;
213}
214
216 FunctionAddressAndSizeVector &function_info) {
217 GetFDEIndex();
218 const size_t count = m_fde_index.GetSize();
219 function_info.Clear();
220 if (count > 0)
221 function_info.Reserve(count);
222 for (size_t i = 0; i < count; ++i) {
223 const FDEEntryMap::Entry *func_offset_data_entry =
225 if (func_offset_data_entry) {
226 FunctionAddressAndSizeVector::Entry function_offset_entry(
227 func_offset_data_entry->base, func_offset_data_entry->size);
228 function_info.Append(function_offset_entry);
229 }
230 }
231}
232
235 cie_map_t::iterator pos = m_cie_map.find(cie_offset);
236
237 if (pos != m_cie_map.end()) {
238 // Parse and cache the CIE
239 if (pos->second == nullptr)
240 pos->second = ParseCIE(cie_offset);
241
242 return pos->second.get();
243 }
244 return nullptr;
245}
246
249 CIESP cie_sp(new CIE(cie_offset));
250 lldb::offset_t offset = cie_offset;
252 GetCFIData();
253 uint32_t length = m_cfi_data.GetU32(&offset);
254 dw_offset_t cie_id, end_offset;
255 bool is_64bit = (length == UINT32_MAX);
256 if (is_64bit) {
257 length = m_cfi_data.GetU64(&offset);
258 cie_id = m_cfi_data.GetU64(&offset);
259 end_offset = cie_offset + length + 12;
260 } else {
261 cie_id = m_cfi_data.GetU32(&offset);
262 end_offset = cie_offset + length + 4;
263 }
264 if (length > 0 && ((m_type == DWARF && cie_id == UINT32_MAX) ||
265 (m_type == EH && cie_id == 0ul))) {
266 size_t i;
267 // cie.offset = cie_offset;
268 // cie.length = length;
269 // cie.cieID = cieID;
270 cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
271 cie_sp->version = m_cfi_data.GetU8(&offset);
272 if (cie_sp->version > CFI_VERSION4) {
274 llvm::formatv("CIE parse error: CFI version {0} is not supported",
275 cie_sp->version));
276 return nullptr;
277 }
278
279 for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) {
280 cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
281 if (cie_sp->augmentation[i] == '\0') {
282 // Zero out remaining bytes in augmentation string
283 for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j)
284 cie_sp->augmentation[j] = '\0';
285
286 break;
287 }
288 }
289
290 if (i == CFI_AUG_MAX_SIZE &&
291 cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') {
292 Debugger::ReportError(llvm::formatv(
293 "CIE parse error: CIE augmentation string was too large "
294 "for the fixed sized buffer of {0} bytes.",
296 return nullptr;
297 }
298
299 // m_cfi_data uses address size from target architecture of the process may
300 // ignore these fields?
301 if (m_type == DWARF && cie_sp->version >= CFI_VERSION4) {
302 cie_sp->address_size = m_cfi_data.GetU8(&offset);
303 cie_sp->segment_size = m_cfi_data.GetU8(&offset);
304 }
305
306 cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
307 cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
308
309 cie_sp->return_addr_reg_num =
310 m_type == DWARF && cie_sp->version >= CFI_VERSION3
311 ? static_cast<uint32_t>(m_cfi_data.GetULEB128(&offset))
312 : m_cfi_data.GetU8(&offset);
313
314 if (cie_sp->augmentation[0]) {
315 // Get the length of the eh_frame augmentation data which starts with a
316 // ULEB128 length in bytes
317 const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
318 const size_t aug_data_end = offset + aug_data_len;
319 const size_t aug_str_len = strlen(cie_sp->augmentation);
320 // A 'z' may be present as the first character of the string.
321 // If present, the Augmentation Data field shall be present. The contents
322 // of the Augmentation Data shall be interpreted according to other
323 // characters in the Augmentation String.
324 if (cie_sp->augmentation[0] == 'z') {
325 // Extract the Augmentation Data
326 size_t aug_str_idx = 0;
327 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) {
328 char aug = cie_sp->augmentation[aug_str_idx];
329 switch (aug) {
330 case 'L':
331 // Indicates the presence of one argument in the Augmentation Data
332 // of the CIE, and a corresponding argument in the Augmentation
333 // Data of the FDE. The argument in the Augmentation Data of the
334 // CIE is 1-byte and represents the pointer encoding used for the
335 // argument in the Augmentation Data of the FDE, which is the
336 // address of a language-specific data area (LSDA). The size of the
337 // LSDA pointer is specified by the pointer encoding used.
338 cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
339 break;
340
341 case 'P':
342 // Indicates the presence of two arguments in the Augmentation Data
343 // of the CIE. The first argument is 1-byte and represents the
344 // pointer encoding used for the second argument, which is the
345 // address of a personality routine handler. The size of the
346 // personality routine pointer is specified by the pointer encoding
347 // used.
348 //
349 // The address of the personality function will be stored at this
350 // location. Pre-execution, it will be all zero's so don't read it
351 // until we're trying to do an unwind & the reloc has been
352 // resolved.
353 {
354 uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
355 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
356 cie_sp->personality_loc = GetGNUEHPointer(
357 m_cfi_data, &offset, arg_ptr_encoding, pc_rel_addr,
359 }
360 break;
361
362 case 'R':
363 // A 'R' may be present at any position after the
364 // first character of the string. The Augmentation Data shall
365 // include a 1 byte argument that represents the pointer encoding
366 // for the address pointers used in the FDE. Example: 0x1B ==
367 // DW_EH_PE_pcrel | DW_EH_PE_sdata4
368 cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
369 break;
370 }
371 }
372 } else if (strcmp(cie_sp->augmentation, "eh") == 0) {
373 // If the Augmentation string has the value "eh", then the EH Data
374 // field shall be present
375 }
376
377 // Set the offset to be the end of the augmentation data just in case we
378 // didn't understand any of the data.
379 offset = (uint32_t)aug_data_end;
380 }
381
382 if (end_offset > offset) {
383 cie_sp->inst_offset = offset;
384 cie_sp->inst_length = end_offset - offset;
385 }
386 while (offset < end_offset) {
387 uint8_t inst = m_cfi_data.GetU8(&offset);
388 uint8_t primary_opcode = inst & 0xC0;
389 uint8_t extended_opcode = inst & 0x3F;
390
391 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode,
392 cie_sp->data_align, offset,
393 cie_sp->initial_row))
394 break; // Stop if we hit an unrecognized opcode
395 }
396 }
397
398 return cie_sp;
399}
400
403 Log *log = GetLog(LLDBLog::Unwind);
404 if (log)
405 m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
408 }
409}
410// Scan through the eh_frame or debug_frame section looking for FDEs and noting
411// the start/end addresses of the functions and a pointer back to the
412// function's FDE for later expansion. Internalize CIEs as we come across them.
413
415 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
416 return;
417
419 return;
420
421 std::lock_guard<std::mutex> guard(m_fde_index_mutex);
422
423 if (m_fde_index_initialized) // if two threads hit the locker
424 return;
425
427
428 bool clear_address_zeroth_bit = false;
429 if (ArchSpec arch = m_objfile.GetArchitecture()) {
430 if (arch.GetTriple().getArch() == llvm::Triple::arm ||
431 arch.GetTriple().getArch() == llvm::Triple::thumb)
432 clear_address_zeroth_bit = true;
433 }
434
435 lldb::offset_t offset = 0;
437 GetCFIData();
438 while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) {
439 const dw_offset_t current_entry = offset;
440 dw_offset_t cie_id, next_entry, cie_offset;
441 uint32_t len = m_cfi_data.GetU32(&offset);
442 bool is_64bit = (len == UINT32_MAX);
443 if (is_64bit) {
444 len = m_cfi_data.GetU64(&offset);
445 cie_id = m_cfi_data.GetU64(&offset);
446 next_entry = current_entry + len + 12;
447 cie_offset = current_entry + 12 - cie_id;
448 } else {
449 cie_id = m_cfi_data.GetU32(&offset);
450 next_entry = current_entry + len + 4;
451 cie_offset = current_entry + 4 - cie_id;
452 }
453
454 if (next_entry > m_cfi_data.GetByteSize() + 1) {
455 Debugger::ReportError(llvm::formatv("Invalid fde/cie next entry offset "
456 "of {0:x} found in cie/fde at {1:x}",
457 next_entry, current_entry));
458 // Don't trust anything in this eh_frame section if we find blatantly
459 // invalid data.
462 return;
463 }
464
465 // An FDE entry contains CIE_pointer in debug_frame in same place as cie_id
466 // in eh_frame. CIE_pointer is an offset into the .debug_frame section. So,
467 // variable cie_offset should be equal to cie_id for debug_frame.
468 // FDE entries with cie_id == 0 shouldn't be ignored for it.
469 if ((cie_id == 0 && m_type == EH) || cie_id == UINT32_MAX || len == 0) {
470 auto cie_sp = ParseCIE(current_entry);
471 if (!cie_sp) {
472 // Cannot parse, the reason is already logged
475 return;
476 }
477
478 m_cie_map[current_entry] = std::move(cie_sp);
479 offset = next_entry;
480 continue;
481 }
482
483 if (m_type == DWARF)
484 cie_offset = cie_id;
485
486 if (cie_offset > m_cfi_data.GetByteSize()) {
487 Debugger::ReportError(llvm::formatv("Invalid cie offset of {0:x} "
488 "found in cie/fde at {1:x}",
489 cie_offset, current_entry));
490 // Don't trust anything in this eh_frame section if we find blatantly
491 // invalid data.
494 return;
495 }
496
497 const CIE *cie = GetCIE(cie_offset);
498 if (cie) {
499 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
500 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
501 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
502
503 lldb::addr_t addr =
504 GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
505 text_addr, data_addr);
506 if (clear_address_zeroth_bit)
507 addr &= ~1ull;
508
511 pc_rel_addr, text_addr, data_addr);
512 FDEEntryMap::Entry fde(addr, length, current_entry);
513 m_fde_index.Append(fde);
514 } else {
515 Debugger::ReportError(llvm::formatv(
516 "unable to find CIE at {0:x} for cie_id = {1:x} for entry at {2:x}.",
517 cie_offset, cie_id, current_entry));
518 }
519 offset = next_entry;
520 }
523}
524
526 Address startaddr,
527 UnwindPlan &unwind_plan) {
528 Log *log = GetLog(LLDBLog::Unwind);
529 lldb::offset_t offset = dwarf_offset;
530 lldb::offset_t current_entry = offset;
531
532 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
533 return false;
534
536 GetCFIData();
537
538 uint32_t length = m_cfi_data.GetU32(&offset);
539 dw_offset_t cie_offset;
540 bool is_64bit = (length == UINT32_MAX);
541 if (is_64bit) {
542 length = m_cfi_data.GetU64(&offset);
543 cie_offset = m_cfi_data.GetU64(&offset);
544 } else {
545 cie_offset = m_cfi_data.GetU32(&offset);
546 }
547
548 // FDE entries with zeroth cie_offset may occur for debug_frame.
549 assert(!(m_type == EH && 0 == cie_offset) && cie_offset != UINT32_MAX);
550
551 // Translate the CIE_id from the eh_frame format, which is relative to the
552 // FDE offset, into a __eh_frame section offset
553 if (m_type == EH) {
554 unwind_plan.SetSourceName("eh_frame CFI");
555 cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
557 } else {
558 unwind_plan.SetSourceName("DWARF CFI");
559 // In theory the debug_frame info should be valid at all call sites
560 // ("asynchronous unwind info" as it is sometimes called) but in practice
561 // gcc et al all emit call frame info for the prologue and call sites, but
562 // not for the epilogue or all the other locations during the function
563 // reliably.
565 }
567
568 const CIE *cie = GetCIE(cie_offset);
569 assert(cie != nullptr);
570
571 const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
572
573 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
574 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
575 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
576 lldb::addr_t range_base =
577 GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
578 text_addr, data_addr);
579 lldb::addr_t range_len = GetGNUEHPointer(
581 pc_rel_addr, text_addr, data_addr);
582 AddressRange range(range_base, m_objfile.GetAddressByteSize(),
584 range.SetByteSize(range_len);
585
586 addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
587
588 if (cie->augmentation[0] == 'z') {
589 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
590 if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) {
591 offset_t saved_offset = offset;
592 lsda_data_file_address =
594 pc_rel_addr, text_addr, data_addr);
595 if (offset - saved_offset != aug_data_len) {
596 // There is more in the augmentation region than we know how to process;
597 // don't read anything.
598 lsda_data_file_address = LLDB_INVALID_ADDRESS;
599 }
600 offset = saved_offset;
601 }
602 offset += aug_data_len;
603 }
604 unwind_plan.SetUnwindPlanForSignalTrap(
605 strchr(cie->augmentation, 'S') ? eLazyBoolYes : eLazyBoolNo);
606
607 Address lsda_data;
608 Address personality_function_ptr;
609
610 if (lsda_data_file_address != LLDB_INVALID_ADDRESS &&
612 m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address,
613 lsda_data);
614 m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc,
615 personality_function_ptr);
616 }
617
618 if (lsda_data.IsValid() && personality_function_ptr.IsValid()) {
619 unwind_plan.SetLSDAAddress(lsda_data);
620 unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr);
621 }
622
623 uint32_t code_align = cie->code_align;
624 int32_t data_align = cie->data_align;
625
626 unwind_plan.SetPlanValidAddressRange(range);
627 UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
628 *cie_initial_row = cie->initial_row;
629 UnwindPlan::RowSP row(cie_initial_row);
630
631 unwind_plan.SetRegisterKind(GetRegisterKind());
633
634 std::vector<UnwindPlan::RowSP> stack;
635
637 while (m_cfi_data.ValidOffset(offset) && offset < end_offset) {
638 uint8_t inst = m_cfi_data.GetU8(&offset);
639 uint8_t primary_opcode = inst & 0xC0;
640 uint8_t extended_opcode = inst & 0x3F;
641
642 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align,
643 offset, *row)) {
644 if (primary_opcode) {
645 switch (primary_opcode) {
646 case DW_CFA_advance_loc: // (Row Creation Instruction)
647 { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
648 // takes a single argument that represents a constant delta. The
649 // required action is to create a new table row with a location value
650 // that is computed by taking the current entry's location value and
651 // adding (delta * code_align). All other values in the new row are
652 // initially identical to the current row.
653 unwind_plan.AppendRow(row);
654 UnwindPlan::Row *newrow = new UnwindPlan::Row;
655 *newrow = *row.get();
656 row.reset(newrow);
657 row->SlideOffset(extended_opcode * code_align);
658 break;
659 }
660
661 case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are
662 // register
663 // takes a single argument that represents a register number. The
664 // required action is to change the rule for the indicated register
665 // to the rule assigned it by the initial_instructions in the CIE.
666 uint32_t reg_num = extended_opcode;
667 // We only keep enough register locations around to unwind what is in
668 // our thread, and these are organized by the register index in that
669 // state, so we need to convert our eh_frame register number from the
670 // EH frame info, to a register index
671
672 if (unwind_plan.IsValidRowIndex(0) &&
673 unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
674 reg_location))
675 row->SetRegisterInfo(reg_num, reg_location);
676 else {
677 // If the register was not set in the first row, remove the
678 // register info to keep the unmodified value from the caller.
679 row->RemoveRegisterInfo(reg_num);
680 }
681 break;
682 }
683 }
684 } else {
685 switch (extended_opcode) {
686 case DW_CFA_set_loc: // 0x1 (Row Creation Instruction)
687 {
688 // DW_CFA_set_loc takes a single argument that represents an address.
689 // The required action is to create a new table row using the
690 // specified address as the location. All other values in the new row
691 // are initially identical to the current row. The new location value
692 // should always be greater than the current one.
693 unwind_plan.AppendRow(row);
694 UnwindPlan::Row *newrow = new UnwindPlan::Row;
695 *newrow = *row.get();
696 row.reset(newrow);
697 row->SetOffset(m_cfi_data.GetAddress(&offset) -
698 startaddr.GetFileAddress());
699 break;
700 }
701
702 case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction)
703 {
704 // takes a single uword argument that represents a constant delta.
705 // This instruction is identical to DW_CFA_advance_loc except for the
706 // encoding and size of the delta argument.
707 unwind_plan.AppendRow(row);
708 UnwindPlan::Row *newrow = new UnwindPlan::Row;
709 *newrow = *row.get();
710 row.reset(newrow);
711 row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align);
712 break;
713 }
714
715 case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction)
716 {
717 // takes a single uword argument that represents a constant delta.
718 // This instruction is identical to DW_CFA_advance_loc except for the
719 // encoding and size of the delta argument.
720 unwind_plan.AppendRow(row);
721 UnwindPlan::Row *newrow = new UnwindPlan::Row;
722 *newrow = *row.get();
723 row.reset(newrow);
724 row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align);
725 break;
726 }
727
728 case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction)
729 {
730 // takes a single uword argument that represents a constant delta.
731 // This instruction is identical to DW_CFA_advance_loc except for the
732 // encoding and size of the delta argument.
733 unwind_plan.AppendRow(row);
734 UnwindPlan::Row *newrow = new UnwindPlan::Row;
735 *newrow = *row.get();
736 row.reset(newrow);
737 row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align);
738 break;
739 }
740
741 case DW_CFA_restore_extended: // 0x6
742 {
743 // takes a single unsigned LEB128 argument that represents a register
744 // number. This instruction is identical to DW_CFA_restore except for
745 // the encoding and size of the register argument.
746 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
747 if (unwind_plan.IsValidRowIndex(0) &&
748 unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
749 reg_location))
750 row->SetRegisterInfo(reg_num, reg_location);
751 break;
752 }
753
754 case DW_CFA_remember_state: // 0xA
755 {
756 // These instructions define a stack of information. Encountering the
757 // DW_CFA_remember_state instruction means to save the rules for
758 // every register on the current row on the stack. Encountering the
759 // DW_CFA_restore_state instruction means to pop the set of rules off
760 // the stack and place them in the current row. (This operation is
761 // useful for compilers that move epilogue code into the body of a
762 // function.)
763 stack.push_back(row);
764 UnwindPlan::Row *newrow = new UnwindPlan::Row;
765 *newrow = *row.get();
766 row.reset(newrow);
767 break;
768 }
769
770 case DW_CFA_restore_state: // 0xB
771 {
772 // These instructions define a stack of information. Encountering the
773 // DW_CFA_remember_state instruction means to save the rules for
774 // every register on the current row on the stack. Encountering the
775 // DW_CFA_restore_state instruction means to pop the set of rules off
776 // the stack and place them in the current row. (This operation is
777 // useful for compilers that move epilogue code into the body of a
778 // function.)
779 if (stack.empty()) {
780 LLDB_LOG(log,
781 "DWARFCallFrameInfo::{0}(dwarf_offset: "
782 "{1:x16}, startaddr: [{2:x16}] encountered "
783 "DW_CFA_restore_state but state stack "
784 "is empty. Corrupt unwind info?",
785 __FUNCTION__, dwarf_offset, startaddr.GetFileAddress());
786 break;
787 }
788 lldb::addr_t offset = row->GetOffset();
789 row = stack.back();
790 stack.pop_back();
791 row->SetOffset(offset);
792 break;
793 }
794
795 case DW_CFA_GNU_args_size: // 0x2e
796 {
797 // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128
798 // operand representing an argument size. This instruction specifies
799 // the total of the size of the arguments which have been pushed onto
800 // the stack.
801
802 // TODO: Figure out how we should handle this.
803 m_cfi_data.GetULEB128(&offset);
804 break;
805 }
806
807 case DW_CFA_val_offset: // 0x14
808 case DW_CFA_val_offset_sf: // 0x15
809 default:
810 break;
811 }
812 }
813 }
814 }
815 unwind_plan.AppendRow(row);
816
817 return true;
818}
819
821 uint8_t extended_opcode,
822 int32_t data_align,
823 lldb::offset_t &offset,
824 UnwindPlan::Row &row) {
826
827 if (primary_opcode) {
828 switch (primary_opcode) {
829 case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are
830 // register
831 // takes two arguments: an unsigned LEB128 constant representing a
832 // factored offset and a register number. The required action is to
833 // change the rule for the register indicated by the register number to
834 // be an offset(N) rule with a value of (N = factored offset *
835 // data_align).
836 uint8_t reg_num = extended_opcode;
837 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
838 reg_location.SetAtCFAPlusOffset(op_offset);
839 row.SetRegisterInfo(reg_num, reg_location);
840 return true;
841 }
842 }
843 } else {
844 switch (extended_opcode) {
845 case DW_CFA_nop: // 0x0
846 return true;
847
848 case DW_CFA_offset_extended: // 0x5
849 {
850 // takes two unsigned LEB128 arguments representing a register number and
851 // a factored offset. This instruction is identical to DW_CFA_offset
852 // except for the encoding and size of the register argument.
853 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
854 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
856 reg_location.SetAtCFAPlusOffset(op_offset);
857 row.SetRegisterInfo(reg_num, reg_location);
858 return true;
859 }
860
861 case DW_CFA_undefined: // 0x7
862 {
863 // takes a single unsigned LEB128 argument that represents a register
864 // number. The required action is to set the rule for the specified
865 // register to undefined.
866 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
868 reg_location.SetUndefined();
869 row.SetRegisterInfo(reg_num, reg_location);
870 return true;
871 }
872
873 case DW_CFA_same_value: // 0x8
874 {
875 // takes a single unsigned LEB128 argument that represents a register
876 // number. The required action is to set the rule for the specified
877 // register to same value.
878 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
880 reg_location.SetSame();
881 row.SetRegisterInfo(reg_num, reg_location);
882 return true;
883 }
884
885 case DW_CFA_register: // 0x9
886 {
887 // takes two unsigned LEB128 arguments representing register numbers. The
888 // required action is to set the rule for the first register to be the
889 // second register.
890 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
891 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
893 reg_location.SetInRegister(other_reg_num);
894 row.SetRegisterInfo(reg_num, reg_location);
895 return true;
896 }
897
898 case DW_CFA_def_cfa: // 0xC (CFA Definition Instruction)
899 {
900 // Takes two unsigned LEB128 operands representing a register number and
901 // a (non-factored) offset. The required action is to define the current
902 // CFA rule to use the provided register and offset.
903 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
904 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
905 row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
906 return true;
907 }
908
909 case DW_CFA_def_cfa_register: // 0xD (CFA Definition Instruction)
910 {
911 // takes a single unsigned LEB128 argument representing a register
912 // number. The required action is to define the current CFA rule to use
913 // the provided register (but to keep the old offset).
914 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
916 row.GetCFAValue().GetOffset());
917 return true;
918 }
919
920 case DW_CFA_def_cfa_offset: // 0xE (CFA Definition Instruction)
921 {
922 // Takes a single unsigned LEB128 operand representing a (non-factored)
923 // offset. The required action is to define the current CFA rule to use
924 // the provided offset (but to keep the old register).
925 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
927 row.GetCFAValue().GetRegisterNumber(), op_offset);
928 return true;
929 }
930
931 case DW_CFA_def_cfa_expression: // 0xF (CFA Definition Instruction)
932 {
933 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
934 const uint8_t *block_data =
935 static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
936 row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
937 return true;
938 }
939
940 case DW_CFA_expression: // 0x10
941 {
942 // Takes two operands: an unsigned LEB128 value representing a register
943 // number, and a DW_FORM_block value representing a DWARF expression. The
944 // required action is to change the rule for the register indicated by
945 // the register number to be an expression(E) rule where E is the DWARF
946 // expression. That is, the DWARF expression computes the address. The
947 // value of the CFA is pushed on the DWARF evaluation stack prior to
948 // execution of the DWARF expression.
949 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
950 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
951 const uint8_t *block_data =
952 static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
954 reg_location.SetAtDWARFExpression(block_data, block_len);
955 row.SetRegisterInfo(reg_num, reg_location);
956 return true;
957 }
958
959 case DW_CFA_offset_extended_sf: // 0x11
960 {
961 // takes two operands: an unsigned LEB128 value representing a register
962 // number and a signed LEB128 factored offset. This instruction is
963 // identical to DW_CFA_offset_extended except that the second operand is
964 // signed and factored.
965 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
966 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
968 reg_location.SetAtCFAPlusOffset(op_offset);
969 row.SetRegisterInfo(reg_num, reg_location);
970 return true;
971 }
972
973 case DW_CFA_def_cfa_sf: // 0x12 (CFA Definition Instruction)
974 {
975 // Takes two operands: an unsigned LEB128 value representing a register
976 // number and a signed LEB128 factored offset. This instruction is
977 // identical to DW_CFA_def_cfa except that the second operand is signed
978 // and factored.
979 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
980 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
981 row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
982 return true;
983 }
984
985 case DW_CFA_def_cfa_offset_sf: // 0x13 (CFA Definition Instruction)
986 {
987 // takes a signed LEB128 operand representing a factored offset. This
988 // instruction is identical to DW_CFA_def_cfa_offset except that the
989 // operand is signed and factored.
990 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
991 uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
992 row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
993 return true;
994 }
995
996 case DW_CFA_val_expression: // 0x16
997 {
998 // takes two operands: an unsigned LEB128 value representing a register
999 // number, and a DW_FORM_block value representing a DWARF expression. The
1000 // required action is to change the rule for the register indicated by
1001 // the register number to be a val_expression(E) rule where E is the
1002 // DWARF expression. That is, the DWARF expression computes the value of
1003 // the given register. The value of the CFA is pushed on the DWARF
1004 // evaluation stack prior to execution of the DWARF expression.
1005 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
1006 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
1007 const uint8_t *block_data =
1008 (const uint8_t *)m_cfi_data.GetData(&offset, block_len);
1009 reg_location.SetIsDWARFExpression(block_data, block_len);
1010 row.SetRegisterInfo(reg_num, reg_location);
1011 return true;
1012 }
1013 }
1014 }
1015 return false;
1016}
1017
1019 const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) {
1020 GetFDEIndex();
1021
1022 for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) {
1023 const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i);
1024 if (!callback(entry.base, entry.size, entry.data))
1025 break;
1026 }
1027}
static uint64_t GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr, uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr, addr_t data_addr)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:359
#define LLDB_SCOPED_TIMERF(...)
Definition: Timer.h:86
A section + offset based address range class.
Definition: AddressRange.h:25
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:211
void SetByteSize(lldb::addr_t byte_size)
Set accessor for the byte size of this range.
Definition: AddressRange.h:239
lldb::addr_t GetByteSize() const
Get accessor for the byte size of this range.
Definition: AddressRange.h:223
A section + offset based address class.
Definition: Address.h:62
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:285
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:293
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:355
An architecture specification class.
Definition: ArchSpec.h:31
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
lldb::RegisterKind GetRegisterKind() const
CIESP ParseCIE(const dw_offset_t cie_offset)
void GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector &function_info)
std::optional< FDEEntryMap::Entry > GetFirstFDEEntryInRange(const AddressRange &range)
void ForEachFDEEntries(const std::function< bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback)
const CIE * GetCIE(dw_offset_t cie_offset)
bool GetAddressRange(Address addr, AddressRange &range)
bool FDEToUnwindPlan(dw_offset_t offset, Address startaddr, UnwindPlan &unwind_plan)
DWARFCallFrameInfo(ObjectFile &objfile, lldb::SectionSP &section, Type type)
bool GetUnwindPlan(const Address &addr, UnwindPlan &unwind_plan)
Return an UnwindPlan based on the call frame information encoded in the FDE of this DWARFCallFrameInf...
bool HandleCommonDwarfOpcode(uint8_t primary_opcode, uint8_t extended_opcode, int32_t data_align, lldb::offset_t &offset, UnwindPlan::Row &row)
An data extractor class.
Definition: DataExtractor.h:48
uint64_t GetULEB128(lldb::offset_t *offset_ptr) const
Extract a unsigned LEB128 value from *offset_ptr.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
bool ValidOffsetForDataOfSize(lldb::offset_t offset, lldb::offset_t length) const
Test the availability of length bytes of data from offset.
const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
uint64_t GetAddress(lldb::offset_t *offset_ptr) const
Extract an address from *offset_ptr.
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
bool ValidOffset(lldb::offset_t offset) const
Test the validity of offset.
uint32_t GetAddressByteSize() const
Get the current address size.
int64_t GetSLEB128(lldb::offset_t *offset_ptr) const
Extract a signed LEB128 value from *offset_ptr.
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
static void ReportError(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report error events.
Definition: Debugger.cpp:1593
const ConstString & GetFilename() const
Filename string const get accessor.
Definition: FileSpec.h:240
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:24
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:44
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition: ObjectFile.h:275
virtual uint32_t GetAddressByteSize() const =0
Gets the address size in bytes for the current object file.
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
Definition: ObjectFile.cpp:599
virtual size_t ReadSectionData(Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len)
Definition: ObjectFile.cpp:485
virtual ArchSpec GetArchitecture()=0
Get the ArchSpec for this object file.
Entry & GetEntryRef(size_t i)
Definition: RangeMap.h:538
const Entry * GetEntryAtIndex(size_t i) const
Definition: RangeMap.h:528
const Entry * FindEntryThatContainsOrFollows(B addr) const
Definition: RangeMap.h:614
void Append(const Entry &entry)
Definition: RangeMap.h:451
Entry * FindEntryThatContains(B addr)
Definition: RangeMap.h:563
void Append(const Entry &entry)
Definition: RangeMap.h:179
void Reserve(typename Collection::size_type size)
Definition: RangeMap.h:291
void SetIsDWARFExpression(const uint8_t *opcodes, uint32_t len)
Definition: UnwindPlan.h:244
void SetIsRegisterPlusOffset(uint32_t reg_num, int32_t offset)
Definition: UnwindPlan.h:227
void SetAtDWARFExpression(const uint8_t *opcodes, uint32_t len)
Definition: UnwindPlan.cpp:56
void SetIsDWARFExpression(const uint8_t *opcodes, uint32_t len)
Definition: UnwindPlan.cpp:65
void SlideOffset(lldb::addr_t offset)
Definition: UnwindPlan.h:339
void SetOffset(lldb::addr_t offset)
Definition: UnwindPlan.h:337
void SetRegisterInfo(uint32_t reg_num, const RegisterLocation register_location)
Definition: UnwindPlan.cpp:273
void SetUnwindPlanForSignalTrap(lldb_private::LazyBool is_for_signal_trap)
Definition: UnwindPlan.h:502
const UnwindPlan::RowSP GetRowAtIndex(uint32_t idx) const
Definition: UnwindPlan.cpp:407
void SetRegisterKind(lldb::RegisterKind kind)
Definition: UnwindPlan.h:437
void SetPlanValidAddressRange(const AddressRange &range)
Definition: UnwindPlan.cpp:431
void SetReturnAddressRegister(uint32_t regnum)
Definition: UnwindPlan.h:439
bool IsValidRowIndex(uint32_t idx) const
Definition: UnwindPlan.cpp:403
void SetPersonalityFunctionPtr(Address presonality_func_ptr)
Definition: UnwindPlan.h:528
void AppendRow(const RowSP &row_sp)
Definition: UnwindPlan.cpp:362
std::shared_ptr< Row > RowSP
Definition: UnwindPlan.h:395
void SetLSDAAddress(Address lsda_addr)
Definition: UnwindPlan.h:524
void SetSourcedFromCompiler(lldb_private::LazyBool from_compiler)
Definition: UnwindPlan.h:478
void SetSourceName(const char *)
Definition: UnwindPlan.cpp:564
void SetUnwindPlanValidAtAllInstructions(lldb_private::LazyBool valid_at_all_insn)
Definition: UnwindPlan.h:490
uint64_t dw_offset_t
Definition: dwarf.h:31
#define DW_EH_PE_MASK_ENCODING
Definition: dwarf.h:41
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:331
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::Section > SectionSP
Definition: lldb-forward.h:413
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:370
lldb_private::UnwindPlan::Row initial_row
bool DoesIntersect(const Range &rhs) const
Definition: RangeMap.h:117