1 //===-- DWARFCallFrameInfo.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 #include "lldb/Core/ArchSpec.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/Section.h"
17 #include "lldb/Core/dwarf.h"
18 #include "lldb/Host/Host.h"
19 #include "lldb/Symbol/DWARFCallFrameInfo.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Symbol/UnwindPlan.h"
22 #include "lldb/Target/RegisterContext.h"
23 #include "lldb/Target/Thread.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/Timer.h"
28 using namespace lldb_private;
30 //----------------------------------------------------------------------
33 // Used for calls when the value type is specified by a DWARF EH Frame
35 //----------------------------------------------------------------------
37 GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr,
38 uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr,
39 addr_t data_addr) //, BSDRelocs *data_relocs) const
41 if (eh_ptr_enc == DW_EH_PE_omit)
42 return ULLONG_MAX; // Value isn't in the buffer...
44 uint64_t baseAddress = 0;
45 uint64_t addressValue = 0;
46 const uint32_t addr_size = DE.GetAddressByteSize();
47 #ifdef LLDB_CONFIGURATION_DEBUG
48 assert(addr_size == 4 || addr_size == 8);
51 bool signExtendValue = false;
52 // Decode the base part or adjust our offset
53 switch (eh_ptr_enc & 0x70) {
55 signExtendValue = true;
56 baseAddress = *offset_ptr;
57 if (pc_rel_addr != LLDB_INVALID_ADDRESS)
58 baseAddress += pc_rel_addr;
60 // Log::GlobalWarning ("PC relative pointer encoding found with
61 // invalid pc relative address.");
64 case DW_EH_PE_textrel:
65 signExtendValue = true;
66 if (text_addr != LLDB_INVALID_ADDRESS)
67 baseAddress = text_addr;
69 // Log::GlobalWarning ("text relative pointer encoding being
70 // decoded with invalid text section address, setting base address
74 case DW_EH_PE_datarel:
75 signExtendValue = true;
76 if (data_addr != LLDB_INVALID_ADDRESS)
77 baseAddress = data_addr;
79 // Log::GlobalWarning ("data relative pointer encoding being
80 // decoded with invalid data section address, setting base address
84 case DW_EH_PE_funcrel:
85 signExtendValue = true;
88 case DW_EH_PE_aligned: {
89 // SetPointerSize should be called prior to extracting these so the
90 // pointer size is cached
91 assert(addr_size != 0);
93 // Align to a address size boundary first
94 uint32_t alignOffset = *offset_ptr % addr_size;
96 offset_ptr += addr_size - alignOffset;
104 // Decode the value part
105 switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) {
106 case DW_EH_PE_absptr: {
107 addressValue = DE.GetAddress(offset_ptr);
109 // addressValue = data_relocs->Relocate(*offset_ptr -
110 // addr_size, *this, addressValue);
112 case DW_EH_PE_uleb128:
113 addressValue = DE.GetULEB128(offset_ptr);
115 case DW_EH_PE_udata2:
116 addressValue = DE.GetU16(offset_ptr);
118 case DW_EH_PE_udata4:
119 addressValue = DE.GetU32(offset_ptr);
121 case DW_EH_PE_udata8:
122 addressValue = DE.GetU64(offset_ptr);
124 case DW_EH_PE_sleb128:
125 addressValue = DE.GetSLEB128(offset_ptr);
127 case DW_EH_PE_sdata2:
128 addressValue = (int16_t)DE.GetU16(offset_ptr);
130 case DW_EH_PE_sdata4:
131 addressValue = (int32_t)DE.GetU32(offset_ptr);
133 case DW_EH_PE_sdata8:
134 addressValue = (int64_t)DE.GetU64(offset_ptr);
137 // Unhandled encoding type
142 // Since we promote everything to 64 bit, we may need to sign extend
143 if (signExtendValue && addr_size < sizeof(baseAddress)) {
144 uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
145 if (sign_bit & addressValue) {
146 uint64_t mask = ~sign_bit + 1;
147 addressValue |= mask;
150 return baseAddress + addressValue;
153 DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile &objfile,
154 SectionSP §ion_sp, Type type)
155 : m_objfile(objfile), m_section_sp(section_sp), m_type(type) {}
157 bool DWARFCallFrameInfo::GetUnwindPlan(Address addr, UnwindPlan &unwind_plan) {
158 FDEEntryMap::Entry fde_entry;
160 // Make sure that the Address we're searching for is the same object file
161 // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
162 ModuleSP module_sp = addr.GetModule();
163 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
164 module_sp->GetObjectFile() != &m_objfile)
167 if (GetFDEEntryByFileAddress(addr.GetFileAddress(), fde_entry) == false)
169 return FDEToUnwindPlan(fde_entry.data, addr, unwind_plan);
172 bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) {
174 // Make sure that the Address we're searching for is the same object file
175 // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
176 ModuleSP module_sp = addr.GetModule();
177 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
178 module_sp->GetObjectFile() != &m_objfile)
181 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
184 FDEEntryMap::Entry *fde_entry =
185 m_fde_index.FindEntryThatContains(addr.GetFileAddress());
189 range = AddressRange(fde_entry->base, fde_entry->size,
190 m_objfile.GetSectionList());
194 bool DWARFCallFrameInfo::GetFDEEntryByFileAddress(
195 addr_t file_addr, FDEEntryMap::Entry &fde_entry) {
196 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
201 if (m_fde_index.IsEmpty())
204 FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains(file_addr);
213 void DWARFCallFrameInfo::GetFunctionAddressAndSizeVector(
214 FunctionAddressAndSizeVector &function_info) {
216 const size_t count = m_fde_index.GetSize();
217 function_info.Clear();
219 function_info.Reserve(count);
220 for (size_t i = 0; i < count; ++i) {
221 const FDEEntryMap::Entry *func_offset_data_entry =
222 m_fde_index.GetEntryAtIndex(i);
223 if (func_offset_data_entry) {
224 FunctionAddressAndSizeVector::Entry function_offset_entry(
225 func_offset_data_entry->base, func_offset_data_entry->size);
226 function_info.Append(function_offset_entry);
231 const DWARFCallFrameInfo::CIE *
232 DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) {
233 cie_map_t::iterator pos = m_cie_map.find(cie_offset);
235 if (pos != m_cie_map.end()) {
236 // Parse and cache the CIE
237 if (pos->second.get() == nullptr)
238 pos->second = ParseCIE(cie_offset);
240 return pos->second.get();
245 DWARFCallFrameInfo::CIESP
246 DWARFCallFrameInfo::ParseCIE(const dw_offset_t cie_offset) {
247 CIESP cie_sp(new CIE(cie_offset));
248 lldb::offset_t offset = cie_offset;
249 if (m_cfi_data_initialized == false)
251 uint32_t length = m_cfi_data.GetU32(&offset);
252 dw_offset_t cie_id, end_offset;
253 bool is_64bit = (length == UINT32_MAX);
255 length = m_cfi_data.GetU64(&offset);
256 cie_id = m_cfi_data.GetU64(&offset);
257 end_offset = cie_offset + length + 12;
259 cie_id = m_cfi_data.GetU32(&offset);
260 end_offset = cie_offset + length + 4;
262 if (length > 0 && ((m_type == DWARF && cie_id == UINT32_MAX) ||
263 (m_type == EH && cie_id == 0ul))) {
265 // cie.offset = cie_offset;
266 // cie.length = length;
267 // cie.cieID = cieID;
268 cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
269 cie_sp->version = m_cfi_data.GetU8(&offset);
270 if (cie_sp->version > CFI_VERSION4) {
271 Host::SystemLog(Host::eSystemLogError,
272 "CIE parse error: CFI version %d is not supported\n",
277 for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) {
278 cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
279 if (cie_sp->augmentation[i] == '\0') {
280 // Zero out remaining bytes in augmentation string
281 for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j)
282 cie_sp->augmentation[j] = '\0';
288 if (i == CFI_AUG_MAX_SIZE &&
289 cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') {
290 Host::SystemLog(Host::eSystemLogError,
291 "CIE parse error: CIE augmentation string was too large "
292 "for the fixed sized buffer of %d bytes.\n",
297 // m_cfi_data uses address size from target architecture of the process
298 // may ignore these fields?
299 if (m_type == DWARF && cie_sp->version >= CFI_VERSION4) {
300 cie_sp->address_size = m_cfi_data.GetU8(&offset);
301 cie_sp->segment_size = m_cfi_data.GetU8(&offset);
304 cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
305 cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
307 cie_sp->return_addr_reg_num =
308 m_type == DWARF && cie_sp->version >= CFI_VERSION3
309 ? static_cast<uint32_t>(m_cfi_data.GetULEB128(&offset))
310 : m_cfi_data.GetU8(&offset);
312 if (cie_sp->augmentation[0]) {
313 // Get the length of the eh_frame augmentation data
314 // which starts with a ULEB128 length in bytes
315 const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
316 const size_t aug_data_end = offset + aug_data_len;
317 const size_t aug_str_len = strlen(cie_sp->augmentation);
318 // A 'z' may be present as the first character of the string.
319 // If present, the Augmentation Data field shall be present.
320 // The contents of the Augmentation Data shall be interpreted
321 // according to other characters in the Augmentation String.
322 if (cie_sp->augmentation[0] == 'z') {
323 // Extract the Augmentation Data
324 size_t aug_str_idx = 0;
325 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) {
326 char aug = cie_sp->augmentation[aug_str_idx];
329 // Indicates the presence of one argument in the
330 // Augmentation Data of the CIE, and a corresponding
331 // argument in the Augmentation Data of the FDE. The
332 // argument in the Augmentation Data of the CIE is
333 // 1-byte and represents the pointer encoding used
334 // for the argument in the Augmentation Data of the
335 // FDE, which is the address of a language-specific
336 // data area (LSDA). The size of the LSDA pointer is
337 // specified by the pointer encoding used.
338 cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
342 // Indicates the presence of two arguments in the
343 // Augmentation Data of the CIE. The first argument
344 // is 1-byte and represents the pointer encoding
345 // used for the second argument, which is the
346 // address of a personality routine handler. The
347 // size of the personality routine pointer is
348 // specified by the pointer encoding used.
350 // The address of the personality function will
351 // be stored at this location. Pre-execution, it
352 // will be all zero's so don't read it until we're
353 // trying to do an unwind & the reloc has been
356 uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
357 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
358 cie_sp->personality_loc = GetGNUEHPointer(
359 m_cfi_data, &offset, arg_ptr_encoding, pc_rel_addr,
360 LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
365 // A 'R' may be present at any position after the
366 // first character of the string. The Augmentation
367 // Data shall include a 1 byte argument that
368 // represents the pointer encoding for the address
369 // pointers used in the FDE.
370 // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4
371 cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
375 } else if (strcmp(cie_sp->augmentation, "eh") == 0) {
376 // If the Augmentation string has the value "eh", then
377 // the EH Data field shall be present
380 // Set the offset to be the end of the augmentation data just in case
381 // we didn't understand any of the data.
382 offset = (uint32_t)aug_data_end;
385 if (end_offset > offset) {
386 cie_sp->inst_offset = offset;
387 cie_sp->inst_length = end_offset - offset;
389 while (offset < end_offset) {
390 uint8_t inst = m_cfi_data.GetU8(&offset);
391 uint8_t primary_opcode = inst & 0xC0;
392 uint8_t extended_opcode = inst & 0x3F;
394 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode,
395 cie_sp->data_align, offset,
396 cie_sp->initial_row))
397 break; // Stop if we hit an unrecognized opcode
404 void DWARFCallFrameInfo::GetCFIData() {
405 if (m_cfi_data_initialized == false) {
406 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
408 m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
409 m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data);
410 m_cfi_data_initialized = true;
413 // Scan through the eh_frame or debug_frame section looking for FDEs and noting
414 // the start/end addresses
415 // of the functions and a pointer back to the function's FDE for later
417 // Internalize CIEs as we come across them.
419 void DWARFCallFrameInfo::GetFDEIndex() {
420 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
423 if (m_fde_index_initialized)
426 std::lock_guard<std::mutex> guard(m_fde_index_mutex);
428 if (m_fde_index_initialized) // if two threads hit the locker
431 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
432 Timer scoped_timer(func_cat, "%s - %s", LLVM_PRETTY_FUNCTION,
433 m_objfile.GetFileSpec().GetFilename().AsCString(""));
435 bool clear_address_zeroth_bit = false;
437 if (m_objfile.GetArchitecture(arch)) {
438 if (arch.GetTriple().getArch() == llvm::Triple::arm ||
439 arch.GetTriple().getArch() == llvm::Triple::thumb)
440 clear_address_zeroth_bit = true;
443 lldb::offset_t offset = 0;
444 if (m_cfi_data_initialized == false)
446 while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) {
447 const dw_offset_t current_entry = offset;
448 dw_offset_t cie_id, next_entry, cie_offset;
449 uint32_t len = m_cfi_data.GetU32(&offset);
450 bool is_64bit = (len == UINT32_MAX);
452 len = m_cfi_data.GetU64(&offset);
453 cie_id = m_cfi_data.GetU64(&offset);
454 next_entry = current_entry + len + 12;
455 cie_offset = current_entry + 12 - cie_id;
457 cie_id = m_cfi_data.GetU32(&offset);
458 next_entry = current_entry + len + 4;
459 cie_offset = current_entry + 4 - cie_id;
462 if (next_entry > m_cfi_data.GetByteSize() + 1) {
463 Host::SystemLog(Host::eSystemLogError, "error: Invalid fde/cie next "
464 "entry offset of 0x%x found in "
466 next_entry, current_entry);
467 // Don't trust anything in this eh_frame section if we find blatantly
470 m_fde_index_initialized = true;
474 // An FDE entry contains CIE_pointer in debug_frame in same place as cie_id
475 // in eh_frame. CIE_pointer is an offset into the .debug_frame section.
476 // So, variable cie_offset should be equal to cie_id for debug_frame.
477 // FDE entries with cie_id == 0 shouldn't be ignored for it.
478 if ((cie_id == 0 && m_type == EH) || cie_id == UINT32_MAX || len == 0) {
479 auto cie_sp = ParseCIE(current_entry);
481 // Cannot parse, the reason is already logged
483 m_fde_index_initialized = true;
487 m_cie_map[current_entry] = std::move(cie_sp);
495 if (cie_offset > m_cfi_data.GetByteSize()) {
496 Host::SystemLog(Host::eSystemLogError,
497 "error: Invalid cie offset of 0x%x "
498 "found in cie/fde at 0x%x\n",
499 cie_offset, current_entry);
500 // Don't trust anything in this eh_frame section if we find blatantly
503 m_fde_index_initialized = true;
507 const CIE *cie = GetCIE(cie_offset);
509 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
510 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
511 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
514 GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
515 text_addr, data_addr);
516 if (clear_address_zeroth_bit)
519 lldb::addr_t length = GetGNUEHPointer(
520 m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
521 pc_rel_addr, text_addr, data_addr);
522 FDEEntryMap::Entry fde(addr, length, current_entry);
523 m_fde_index.Append(fde);
525 Host::SystemLog(Host::eSystemLogError, "error: unable to find CIE at "
526 "0x%8.8x for cie_id = 0x%8.8x for "
527 "entry at 0x%8.8x.\n",
528 cie_offset, cie_id, current_entry);
533 m_fde_index_initialized = true;
536 bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset,
538 UnwindPlan &unwind_plan) {
539 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND);
540 lldb::offset_t offset = dwarf_offset;
541 lldb::offset_t current_entry = offset;
543 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
546 if (m_cfi_data_initialized == false)
549 uint32_t length = m_cfi_data.GetU32(&offset);
550 dw_offset_t cie_offset;
551 bool is_64bit = (length == UINT32_MAX);
553 length = m_cfi_data.GetU64(&offset);
554 cie_offset = m_cfi_data.GetU64(&offset);
556 cie_offset = m_cfi_data.GetU32(&offset);
559 // FDE entries with zeroth cie_offset may occur for debug_frame.
560 assert(!(m_type == EH && 0 == cie_offset) && cie_offset != UINT32_MAX);
562 // Translate the CIE_id from the eh_frame format, which
563 // is relative to the FDE offset, into a __eh_frame section
566 unwind_plan.SetSourceName("eh_frame CFI");
567 cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
568 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
570 unwind_plan.SetSourceName("DWARF CFI");
571 // In theory the debug_frame info should be valid at all call sites
572 // ("asynchronous unwind info" as it is sometimes called) but in practice
573 // gcc et al all emit call frame info for the prologue and call sites, but
574 // not for the epilogue or all the other locations during the function
576 unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
578 unwind_plan.SetSourcedFromCompiler(eLazyBoolYes);
580 const CIE *cie = GetCIE(cie_offset);
581 assert(cie != nullptr);
583 const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
585 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
586 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
587 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
588 lldb::addr_t range_base =
589 GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
590 text_addr, data_addr);
591 lldb::addr_t range_len = GetGNUEHPointer(
592 m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
593 pc_rel_addr, text_addr, data_addr);
594 AddressRange range(range_base, m_objfile.GetAddressByteSize(),
595 m_objfile.GetSectionList());
596 range.SetByteSize(range_len);
598 addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
600 if (cie->augmentation[0] == 'z') {
601 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
602 if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) {
603 offset_t saved_offset = offset;
604 lsda_data_file_address =
605 GetGNUEHPointer(m_cfi_data, &offset, cie->lsda_addr_encoding,
606 pc_rel_addr, text_addr, data_addr);
607 if (offset - saved_offset != aug_data_len) {
608 // There is more in the augmentation region than we know how to process;
609 // don't read anything.
610 lsda_data_file_address = LLDB_INVALID_ADDRESS;
612 offset = saved_offset;
614 offset += aug_data_len;
617 Address personality_function_ptr;
619 if (lsda_data_file_address != LLDB_INVALID_ADDRESS &&
620 cie->personality_loc != LLDB_INVALID_ADDRESS) {
621 m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address,
623 m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc,
624 personality_function_ptr);
627 if (lsda_data.IsValid() && personality_function_ptr.IsValid()) {
628 unwind_plan.SetLSDAAddress(lsda_data);
629 unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr);
632 uint32_t code_align = cie->code_align;
633 int32_t data_align = cie->data_align;
635 unwind_plan.SetPlanValidAddressRange(range);
636 UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
637 *cie_initial_row = cie->initial_row;
638 UnwindPlan::RowSP row(cie_initial_row);
640 unwind_plan.SetRegisterKind(GetRegisterKind());
641 unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num);
643 std::vector<UnwindPlan::RowSP> stack;
645 UnwindPlan::Row::RegisterLocation reg_location;
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;
651 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align,
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
659 // value that is computed by taking the current entry's location
660 // value and adding (delta * code_align). All other
661 // values in the new row are initially identical to the current row.
662 unwind_plan.AppendRow(row);
663 UnwindPlan::Row *newrow = new UnwindPlan::Row;
664 *newrow = *row.get();
666 row->SlideOffset(extended_opcode * code_align);
670 case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are
672 // takes a single argument that represents a register number. The
673 // required action is to change the rule for the indicated register
674 // to the rule assigned it by the initial_instructions in the CIE.
675 uint32_t reg_num = extended_opcode;
676 // We only keep enough register locations around to
677 // unwind what is in our thread, and these are organized
678 // by the register index in that state, so we need to convert our
679 // eh_frame register number from the EH frame info, to a register
682 if (unwind_plan.IsValidRowIndex(0) &&
683 unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
685 row->SetRegisterInfo(reg_num, reg_location);
690 switch (extended_opcode) {
691 case DW_CFA_set_loc: // 0x1 (Row Creation Instruction)
693 // DW_CFA_set_loc takes a single argument that represents an address.
694 // The required action is to create a new table row using the
695 // specified address as the location. All other values in the new row
696 // are initially identical to the current row. The new location value
697 // should always be greater than the current one.
698 unwind_plan.AppendRow(row);
699 UnwindPlan::Row *newrow = new UnwindPlan::Row;
700 *newrow = *row.get();
702 row->SetOffset(m_cfi_data.GetPointer(&offset) -
703 startaddr.GetFileAddress());
707 case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction)
709 // takes a single uword argument that represents a constant delta.
710 // This instruction is identical to DW_CFA_advance_loc except for the
711 // encoding and size of the delta argument.
712 unwind_plan.AppendRow(row);
713 UnwindPlan::Row *newrow = new UnwindPlan::Row;
714 *newrow = *row.get();
716 row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align);
720 case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction)
722 // takes a single uword argument that represents a constant delta.
723 // This instruction is identical to DW_CFA_advance_loc except for the
724 // encoding and size of the delta argument.
725 unwind_plan.AppendRow(row);
726 UnwindPlan::Row *newrow = new UnwindPlan::Row;
727 *newrow = *row.get();
729 row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align);
733 case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction)
735 // takes a single uword argument that represents a constant delta.
736 // This instruction is identical to DW_CFA_advance_loc except for the
737 // encoding and size of the delta argument.
738 unwind_plan.AppendRow(row);
739 UnwindPlan::Row *newrow = new UnwindPlan::Row;
740 *newrow = *row.get();
742 row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align);
746 case DW_CFA_restore_extended: // 0x6
748 // takes a single unsigned LEB128 argument that represents a register
749 // number. This instruction is identical to DW_CFA_restore except for
750 // the encoding and size of the register argument.
751 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
752 if (unwind_plan.IsValidRowIndex(0) &&
753 unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
755 row->SetRegisterInfo(reg_num, reg_location);
759 case DW_CFA_remember_state: // 0xA
761 // These instructions define a stack of information. Encountering the
762 // DW_CFA_remember_state instruction means to save the rules for every
763 // register on the current row on the stack. Encountering the
764 // DW_CFA_restore_state instruction means to pop the set of rules off
765 // the stack and place them in the current row. (This operation is
766 // useful for compilers that move epilogue code into the body of a
768 stack.push_back(row);
769 UnwindPlan::Row *newrow = new UnwindPlan::Row;
770 *newrow = *row.get();
775 case DW_CFA_restore_state: // 0xB
777 // These instructions define a stack of information. Encountering the
778 // DW_CFA_remember_state instruction means to save the rules for every
779 // register on the current row on the stack. Encountering the
780 // DW_CFA_restore_state instruction means to pop the set of rules off
781 // the stack and place them in the current row. (This operation is
782 // useful for compilers that move epilogue code into the body of a
786 log->Printf("DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32
787 ", startaddr: %" PRIx64
788 " encountered DW_CFA_restore_state but state stack "
789 "is empty. Corrupt unwind info?",
790 __FUNCTION__, dwarf_offset,
791 startaddr.GetFileAddress());
794 lldb::addr_t offset = row->GetOffset();
797 row->SetOffset(offset);
801 case DW_CFA_GNU_args_size: // 0x2e
803 // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128
805 // representing an argument size. This instruction specifies the total
807 // the size of the arguments which have been pushed onto the stack.
809 // TODO: Figure out how we should handle this.
810 m_cfi_data.GetULEB128(&offset);
814 case DW_CFA_val_offset: // 0x14
815 case DW_CFA_val_offset_sf: // 0x15
822 unwind_plan.AppendRow(row);
827 bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
828 uint8_t extended_opcode,
830 lldb::offset_t &offset,
831 UnwindPlan::Row &row) {
832 UnwindPlan::Row::RegisterLocation reg_location;
834 if (primary_opcode) {
835 switch (primary_opcode) {
836 case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are
838 // takes two arguments: an unsigned LEB128 constant representing a
839 // factored offset and a register number. The required action is to
840 // change the rule for the register indicated by the register number
841 // to be an offset(N) rule with a value of
842 // (N = factored offset * data_align).
843 uint8_t reg_num = extended_opcode;
844 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
845 reg_location.SetAtCFAPlusOffset(op_offset);
846 row.SetRegisterInfo(reg_num, reg_location);
851 switch (extended_opcode) {
852 case DW_CFA_nop: // 0x0
855 case DW_CFA_offset_extended: // 0x5
857 // takes two unsigned LEB128 arguments representing a register number
858 // and a factored offset. This instruction is identical to DW_CFA_offset
859 // except for the encoding and size of the register argument.
860 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
861 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
862 UnwindPlan::Row::RegisterLocation reg_location;
863 reg_location.SetAtCFAPlusOffset(op_offset);
864 row.SetRegisterInfo(reg_num, reg_location);
868 case DW_CFA_undefined: // 0x7
870 // takes a single unsigned LEB128 argument that represents a register
871 // number. The required action is to set the rule for the specified
872 // register to undefined.
873 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
874 UnwindPlan::Row::RegisterLocation reg_location;
875 reg_location.SetUndefined();
876 row.SetRegisterInfo(reg_num, reg_location);
880 case DW_CFA_same_value: // 0x8
882 // takes a single unsigned LEB128 argument that represents a register
883 // number. The required action is to set the rule for the specified
884 // register to same value.
885 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
886 UnwindPlan::Row::RegisterLocation reg_location;
887 reg_location.SetSame();
888 row.SetRegisterInfo(reg_num, reg_location);
892 case DW_CFA_register: // 0x9
894 // takes two unsigned LEB128 arguments representing register numbers.
895 // The required action is to set the rule for the first register to be
896 // the second register.
897 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
898 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
899 UnwindPlan::Row::RegisterLocation reg_location;
900 reg_location.SetInRegister(other_reg_num);
901 row.SetRegisterInfo(reg_num, reg_location);
905 case DW_CFA_def_cfa: // 0xC (CFA Definition Instruction)
907 // Takes two unsigned LEB128 operands representing a register
908 // number and a (non-factored) offset. The required action
909 // is to define the current CFA rule to use the provided
910 // register and offset.
911 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
912 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
913 row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
917 case DW_CFA_def_cfa_register: // 0xD (CFA Definition Instruction)
919 // takes a single unsigned LEB128 argument representing a register
920 // number. The required action is to define the current CFA rule to
921 // use the provided register (but to keep the old offset).
922 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
923 row.GetCFAValue().SetIsRegisterPlusOffset(reg_num,
924 row.GetCFAValue().GetOffset());
928 case DW_CFA_def_cfa_offset: // 0xE (CFA Definition Instruction)
930 // Takes a single unsigned LEB128 operand representing a
931 // (non-factored) offset. The required action is to define
932 // the current CFA rule to use the provided offset (but
933 // to keep the old register).
934 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
935 row.GetCFAValue().SetIsRegisterPlusOffset(
936 row.GetCFAValue().GetRegisterNumber(), op_offset);
940 case DW_CFA_def_cfa_expression: // 0xF (CFA Definition Instruction)
942 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
943 const uint8_t *block_data =
944 static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
945 row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
949 case DW_CFA_expression: // 0x10
951 // Takes two operands: an unsigned LEB128 value representing
952 // a register number, and a DW_FORM_block value representing a DWARF
953 // expression. The required action is to change the rule for the
954 // register indicated by the register number to be an expression(E)
955 // rule where E is the DWARF expression. That is, the DWARF
956 // expression computes the address. The value of the CFA is
957 // pushed on the DWARF evaluation stack prior to execution of
958 // 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));
963 UnwindPlan::Row::RegisterLocation reg_location;
964 reg_location.SetAtDWARFExpression(block_data, block_len);
965 row.SetRegisterInfo(reg_num, reg_location);
969 case DW_CFA_offset_extended_sf: // 0x11
971 // takes two operands: an unsigned LEB128 value representing a
972 // register number and a signed LEB128 factored offset. This
973 // instruction is identical to DW_CFA_offset_extended except
974 // that the second operand is 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;
977 UnwindPlan::Row::RegisterLocation reg_location;
978 reg_location.SetAtCFAPlusOffset(op_offset);
979 row.SetRegisterInfo(reg_num, reg_location);
983 case DW_CFA_def_cfa_sf: // 0x12 (CFA Definition Instruction)
985 // Takes two operands: an unsigned LEB128 value representing
986 // a register number and a signed LEB128 factored offset.
987 // This instruction is identical to DW_CFA_def_cfa except
988 // that the second operand is signed 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);
995 case DW_CFA_def_cfa_offset_sf: // 0x13 (CFA Definition Instruction)
997 // takes a signed LEB128 operand representing a factored
998 // offset. This instruction is identical to DW_CFA_def_cfa_offset
999 // except that the 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);
1006 case DW_CFA_val_expression: // 0x16
1008 // takes two operands: an unsigned LEB128 value representing a register
1009 // number, and a DW_FORM_block value representing a DWARF expression.
1010 // The required action is to change the rule for the register indicated
1011 // by 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 //#if defined(__i386__) || defined(__x86_64__)
1020 // // The EH frame info for EIP and RIP contains code that
1021 // looks for traps to
1022 // // be a specific type and increments the PC.
1024 // // DW_CFA_val_expression where:
1025 // // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup,
1026 // DW_OP_plus_uconst(0x34),
1027 // // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0),
1029 // // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap,
1030 // DW_OP_lit4, DW_OP_ne,
1031 // // DW_OP_and, DW_OP_plus
1032 // // This basically does a:
1033 // // eip = ucontenxt.mcontext32->gpr.eip;
1034 // // if (ucontenxt.mcontext32->exc.trapno != 3 &&
1035 // ucontenxt.mcontext32->exc.trapno != 4)
1039 // // DW_CFA_val_expression where:
1040 // // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup,
1041 // DW_OP_plus_uconst(0x90), DW_OP_deref,
1042 // // DW_OP_swap, DW_OP_plus_uconst(0),
1043 // DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
1044 // // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
1045 // DW_OP_and, DW_OP_plus
1046 // // This basically does a:
1047 // // rip = ucontenxt.mcontext64->gpr.rip;
1048 // // if (ucontenxt.mcontext64->exc.trapno != 3 &&
1049 // ucontenxt.mcontext64->exc.trapno != 4)
1051 // // The trap comparisons and increments are not needed as
1052 // it hoses up the unwound PC which
1053 // // is expected to point at least past the instruction that
1054 // causes the fault/trap. So we
1055 // // take it out by trimming the expression right at the
1056 // first "DW_OP_swap" opcodes
1057 // if (block_data != NULL && thread->GetPCRegNum(Thread::GCC)
1060 // if (thread->Is64Bit())
1062 // if (block_len > 9 && block_data[8] == DW_OP_swap
1063 // && block_data[9] == DW_OP_plus_uconst)
1068 // if (block_len > 8 && block_data[7] == DW_OP_swap
1069 // && block_data[8] == DW_OP_plus_uconst)
1074 reg_location.SetIsDWARFExpression(block_data, block_len);
1075 row.SetRegisterInfo(reg_num, reg_location);
1083 void DWARFCallFrameInfo::ForEachFDEEntries(
1084 const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) {
1087 for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) {
1088 const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i);
1089 if (!callback(entry.base, entry.size, entry.data))