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