1 //===-- CompactUnwindInfo.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 //===----------------------------------------------------------------------===//
15 #include "lldb/Core/ArchSpec.h"
16 #include "lldb/Core/DataBufferHeap.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/Section.h"
20 #include "lldb/Core/Section.h"
21 #include "lldb/Core/StreamString.h"
22 #include "lldb/Symbol/CompactUnwindInfo.h"
23 #include "lldb/Symbol/ObjectFile.h"
24 #include "lldb/Symbol/UnwindPlan.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Target/Target.h"
28 #include "llvm/Support/MathExtras.h"
31 using namespace lldb_private;
34 namespace lldb_private {
36 // Constants from <mach-o/compact_unwind_encoding.h>
38 FLAGS_ANONYMOUS_ENUM()
40 UNWIND_IS_NOT_FUNCTION_START = 0x80000000,
41 UNWIND_HAS_LSDA = 0x40000000,
42 UNWIND_PERSONALITY_MASK = 0x30000000,
45 FLAGS_ANONYMOUS_ENUM()
47 UNWIND_X86_MODE_MASK = 0x0F000000,
48 UNWIND_X86_MODE_EBP_FRAME = 0x01000000,
49 UNWIND_X86_MODE_STACK_IMMD = 0x02000000,
50 UNWIND_X86_MODE_STACK_IND = 0x03000000,
51 UNWIND_X86_MODE_DWARF = 0x04000000,
53 UNWIND_X86_EBP_FRAME_REGISTERS = 0x00007FFF,
54 UNWIND_X86_EBP_FRAME_OFFSET = 0x00FF0000,
56 UNWIND_X86_FRAMELESS_STACK_SIZE = 0x00FF0000,
57 UNWIND_X86_FRAMELESS_STACK_ADJUST = 0x0000E000,
58 UNWIND_X86_FRAMELESS_STACK_REG_COUNT = 0x00001C00,
59 UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION = 0x000003FF,
61 UNWIND_X86_DWARF_SECTION_OFFSET = 0x00FFFFFF,
66 UNWIND_X86_REG_NONE = 0,
67 UNWIND_X86_REG_EBX = 1,
68 UNWIND_X86_REG_ECX = 2,
69 UNWIND_X86_REG_EDX = 3,
70 UNWIND_X86_REG_EDI = 4,
71 UNWIND_X86_REG_ESI = 5,
72 UNWIND_X86_REG_EBP = 6,
75 FLAGS_ANONYMOUS_ENUM()
77 UNWIND_X86_64_MODE_MASK = 0x0F000000,
78 UNWIND_X86_64_MODE_RBP_FRAME = 0x01000000,
79 UNWIND_X86_64_MODE_STACK_IMMD = 0x02000000,
80 UNWIND_X86_64_MODE_STACK_IND = 0x03000000,
81 UNWIND_X86_64_MODE_DWARF = 0x04000000,
83 UNWIND_X86_64_RBP_FRAME_REGISTERS = 0x00007FFF,
84 UNWIND_X86_64_RBP_FRAME_OFFSET = 0x00FF0000,
86 UNWIND_X86_64_FRAMELESS_STACK_SIZE = 0x00FF0000,
87 UNWIND_X86_64_FRAMELESS_STACK_ADJUST = 0x0000E000,
88 UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT = 0x00001C00,
89 UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION = 0x000003FF,
91 UNWIND_X86_64_DWARF_SECTION_OFFSET = 0x00FFFFFF,
96 UNWIND_X86_64_REG_NONE = 0,
97 UNWIND_X86_64_REG_RBX = 1,
98 UNWIND_X86_64_REG_R12 = 2,
99 UNWIND_X86_64_REG_R13 = 3,
100 UNWIND_X86_64_REG_R14 = 4,
101 UNWIND_X86_64_REG_R15 = 5,
102 UNWIND_X86_64_REG_RBP = 6,
107 #ifndef UNWIND_SECOND_LEVEL_REGULAR
108 #define UNWIND_SECOND_LEVEL_REGULAR 2
111 #ifndef UNWIND_SECOND_LEVEL_COMPRESSED
112 #define UNWIND_SECOND_LEVEL_COMPRESSED 3
115 #ifndef UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET
116 #define UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(entry) (entry & 0x00FFFFFF)
119 #ifndef UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX
120 #define UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(entry) ((entry >> 24) & 0xFF)
123 #define EXTRACT_BITS(value, mask) \
124 ( (value >> llvm::countTrailingZeros(static_cast<uint32_t>(mask), llvm::ZB_Width)) & \
125 (((1 << llvm::countPopulation(static_cast<uint32_t>(mask))))-1) )
129 //----------------------
131 //----------------------
134 CompactUnwindInfo::CompactUnwindInfo(ObjectFile& objfile, SectionSP& section_sp) :
136 m_section_sp (section_sp),
137 m_section_contents_if_encrypted (),
140 m_indexes_computed (eLazyBoolCalculate),
141 m_unwindinfo_data (),
142 m_unwindinfo_data_computed (false),
148 //----------------------
150 //----------------------
152 CompactUnwindInfo::~CompactUnwindInfo()
157 CompactUnwindInfo::GetUnwindPlan (Target &target, Address addr, UnwindPlan& unwind_plan)
159 if (!IsValid (target.GetProcessSP()))
163 FunctionInfo function_info;
164 if (GetCompactUnwindInfoForFunction (target, addr, function_info))
166 // shortcut return for functions that have no compact unwind
167 if (function_info.encoding == 0)
171 if (m_objfile.GetArchitecture (arch))
174 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
175 if (log && log->GetVerbose())
178 addr.Dump (&strm, NULL, Address::DumpStyle::DumpStyleResolvedDescriptionNoFunctionArguments, Address::DumpStyle::DumpStyleFileAddress, arch.GetAddressByteSize());
179 log->Printf ("Got compact unwind encoding 0x%x for function %s", function_info.encoding, strm.GetData());
182 if (function_info.valid_range_offset_start != 0 && function_info.valid_range_offset_end != 0)
184 SectionList *sl = m_objfile.GetSectionList ();
187 addr_t func_range_start_file_addr =
188 function_info.valid_range_offset_start + m_objfile.GetHeaderAddress().GetFileAddress();
189 AddressRange func_range (func_range_start_file_addr,
190 function_info.valid_range_offset_end - function_info.valid_range_offset_start,
192 unwind_plan.SetPlanValidAddressRange (func_range);
196 if (arch.GetTriple().getArch() == llvm::Triple::x86_64)
198 return CreateUnwindPlan_x86_64 (target, function_info, unwind_plan, addr);
200 if (arch.GetTriple().getArch() == llvm::Triple::x86)
202 return CreateUnwindPlan_i386 (target, function_info, unwind_plan, addr);
210 CompactUnwindInfo::IsValid (const ProcessSP &process_sp)
212 if (m_section_sp.get() == nullptr)
215 if (m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed)
218 ScanIndex (process_sp);
220 return m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed;
224 CompactUnwindInfo::ScanIndex (const ProcessSP &process_sp)
226 Mutex::Locker locker(m_mutex);
227 if (m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed)
230 // We can't read the index for some reason.
231 if (m_indexes_computed == eLazyBoolNo)
236 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
238 m_objfile.GetModule()->LogMessage(log, "Reading compact unwind first-level indexes");
240 if (m_unwindinfo_data_computed == false)
242 if (m_section_sp->IsEncrypted())
244 // Can't get section contents of a protected/encrypted section until we have a live
245 // process and can read them out of memory.
246 if (process_sp.get() == nullptr)
248 m_section_contents_if_encrypted.reset (new DataBufferHeap (m_section_sp->GetByteSize(), 0));
250 if (process_sp->ReadMemory (
251 m_section_sp->GetLoadBaseAddress (&process_sp->GetTarget()),
252 m_section_contents_if_encrypted->GetBytes(),
253 m_section_sp->GetByteSize(), error) == m_section_sp->GetByteSize() && error.Success())
255 m_unwindinfo_data.SetAddressByteSize (process_sp->GetTarget().GetArchitecture().GetAddressByteSize());
256 m_unwindinfo_data.SetByteOrder (process_sp->GetTarget().GetArchitecture().GetByteOrder());
257 m_unwindinfo_data.SetData (m_section_contents_if_encrypted, 0);
262 m_objfile.ReadSectionData (m_section_sp.get(), m_unwindinfo_data);
264 if (m_unwindinfo_data.GetByteSize() != m_section_sp->GetByteSize())
266 m_unwindinfo_data_computed = true;
269 if (m_unwindinfo_data.GetByteSize() > 0)
273 // struct unwind_info_section_header
275 // uint32_t version; // UNWIND_SECTION_VERSION
276 // uint32_t commonEncodingsArraySectionOffset;
277 // uint32_t commonEncodingsArrayCount;
278 // uint32_t personalityArraySectionOffset;
279 // uint32_t personalityArrayCount;
280 // uint32_t indexSectionOffset;
281 // uint32_t indexCount;
283 m_unwind_header.version = m_unwindinfo_data.GetU32(&offset);
284 m_unwind_header.common_encodings_array_offset = m_unwindinfo_data.GetU32(&offset);
285 m_unwind_header.common_encodings_array_count = m_unwindinfo_data.GetU32(&offset);
286 m_unwind_header.personality_array_offset = m_unwindinfo_data.GetU32(&offset);
287 m_unwind_header.personality_array_count = m_unwindinfo_data.GetU32(&offset);
288 uint32_t indexSectionOffset = m_unwindinfo_data.GetU32(&offset);
290 uint32_t indexCount = m_unwindinfo_data.GetU32(&offset);
292 if (m_unwind_header.common_encodings_array_offset > m_unwindinfo_data.GetByteSize()
293 || m_unwind_header.personality_array_offset > m_unwindinfo_data.GetByteSize()
294 || indexSectionOffset > m_unwindinfo_data.GetByteSize()
295 || offset > m_unwindinfo_data.GetByteSize())
297 Host::SystemLog (Host::eSystemLogError,
298 "error: Invalid offset encountered in compact unwind info, skipping\n");
299 // don't trust anything from this compact_unwind section if it looks
300 // blatently invalid data in the header.
301 m_indexes_computed = eLazyBoolNo;
305 // Parse the basic information from the indexes
306 // We wait to scan the second level page info until it's needed
308 // struct unwind_info_section_header_index_entry
310 // uint32_t functionOffset;
311 // uint32_t secondLevelPagesSectionOffset;
312 // uint32_t lsdaIndexArraySectionOffset;
315 offset = indexSectionOffset;
316 for (uint32_t idx = 0; idx < indexCount; idx++)
318 uint32_t function_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
319 uint32_t second_level_offset = m_unwindinfo_data.GetU32(&offset); // secondLevelPagesSectionOffset
320 uint32_t lsda_offset = m_unwindinfo_data.GetU32(&offset); // lsdaIndexArraySectionOffset
322 if (second_level_offset > m_section_sp->GetByteSize() || lsda_offset > m_section_sp->GetByteSize())
324 m_indexes_computed = eLazyBoolNo;
327 UnwindIndex this_index;
328 this_index.function_offset = function_offset; //
329 this_index.second_level = second_level_offset;
330 this_index.lsda_array_start = lsda_offset;
332 if (m_indexes.size() > 0)
334 m_indexes[m_indexes.size() - 1].lsda_array_end = lsda_offset;
337 if (second_level_offset == 0)
339 this_index.sentinal_entry = true;
342 m_indexes.push_back (this_index);
344 m_indexes_computed = eLazyBoolYes;
348 m_indexes_computed = eLazyBoolNo;
353 CompactUnwindInfo::GetLSDAForFunctionOffset (uint32_t lsda_offset, uint32_t lsda_count, uint32_t function_offset)
355 // struct unwind_info_section_header_lsda_index_entry
357 // uint32_t functionOffset;
358 // uint32_t lsdaOffset;
361 offset_t first_entry = lsda_offset;
363 uint32_t high = lsda_count;
366 uint32_t mid = (low + high) / 2;
367 offset_t offset = first_entry + (mid * 8);
368 uint32_t mid_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
369 uint32_t mid_lsda_offset = m_unwindinfo_data.GetU32(&offset); // lsdaOffset
370 if (mid_func_offset == function_offset)
372 return mid_lsda_offset;
374 if (mid_func_offset < function_offset)
387 CompactUnwindInfo::BinarySearchRegularSecondPage (uint32_t entry_page_offset, uint32_t entry_count, uint32_t function_offset, uint32_t *entry_func_start_offset, uint32_t *entry_func_end_offset)
389 // typedef uint32_t compact_unwind_encoding_t;
390 // struct unwind_info_regular_second_level_entry
392 // uint32_t functionOffset;
393 // compact_unwind_encoding_t encoding;
395 offset_t first_entry = entry_page_offset;
398 uint32_t high = entry_count;
399 uint32_t last = high - 1;
402 uint32_t mid = (low + high) / 2;
403 offset_t offset = first_entry + (mid * 8);
404 uint32_t mid_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
405 uint32_t next_func_offset = 0;
408 offset = first_entry + ((mid + 1) * 8);
409 next_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
411 if (mid_func_offset <= function_offset)
413 if (mid == last || (next_func_offset > function_offset))
415 if (entry_func_start_offset)
416 *entry_func_start_offset = mid_func_offset;
417 if (mid != last && entry_func_end_offset)
418 *entry_func_end_offset = next_func_offset;
419 return first_entry + (mid * 8);
431 return LLDB_INVALID_OFFSET;
435 CompactUnwindInfo::BinarySearchCompressedSecondPage (uint32_t entry_page_offset, uint32_t entry_count, uint32_t function_offset_to_find, uint32_t function_offset_base, uint32_t *entry_func_start_offset, uint32_t *entry_func_end_offset)
437 offset_t first_entry = entry_page_offset;
440 uint32_t high = entry_count;
441 uint32_t last = high - 1;
444 uint32_t mid = (low + high) / 2;
445 offset_t offset = first_entry + (mid * 4);
446 uint32_t entry = m_unwindinfo_data.GetU32(&offset); // entry
447 uint32_t mid_func_offset = UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET (entry);
448 mid_func_offset += function_offset_base;
449 uint32_t next_func_offset = 0;
452 offset = first_entry + ((mid + 1) * 4);
453 uint32_t next_entry = m_unwindinfo_data.GetU32(&offset); // entry
454 next_func_offset = UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET (next_entry);
455 next_func_offset += function_offset_base;
457 if (mid_func_offset <= function_offset_to_find)
459 if (mid == last || (next_func_offset > function_offset_to_find))
461 if (entry_func_start_offset)
462 *entry_func_start_offset = mid_func_offset;
463 if (mid != last && entry_func_end_offset)
464 *entry_func_end_offset = next_func_offset;
465 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX (entry);
482 CompactUnwindInfo::GetCompactUnwindInfoForFunction (Target &target, Address address, FunctionInfo &unwind_info)
484 unwind_info.encoding = 0;
485 unwind_info.lsda_address.Clear();
486 unwind_info.personality_ptr_address.Clear();
488 if (!IsValid (target.GetProcessSP()))
491 addr_t text_section_file_address = LLDB_INVALID_ADDRESS;
492 SectionList *sl = m_objfile.GetSectionList ();
495 SectionSP text_sect = sl->FindSectionByType (eSectionTypeCode, true);
498 text_section_file_address = text_sect->GetFileAddress();
501 if (text_section_file_address == LLDB_INVALID_ADDRESS)
504 addr_t function_offset = address.GetFileAddress() - m_objfile.GetHeaderAddress().GetFileAddress();
507 key.function_offset = function_offset;
509 std::vector<UnwindIndex>::const_iterator it;
510 it = std::lower_bound (m_indexes.begin(), m_indexes.end(), key);
511 if (it == m_indexes.end())
516 if (it->function_offset != key.function_offset)
518 if (it != m_indexes.begin())
522 if (it->sentinal_entry == true)
527 auto next_it = it + 1;
528 if (next_it != m_indexes.end())
530 // initialize the function offset end range to be the start of the
531 // next index offset. If we find an entry which is at the end of
532 // the index table, this will establish the range end.
533 unwind_info.valid_range_offset_end = next_it->function_offset;
536 offset_t second_page_offset = it->second_level;
537 offset_t lsda_array_start = it->lsda_array_start;
538 offset_t lsda_array_count = (it->lsda_array_end - it->lsda_array_start) / 8;
540 offset_t offset = second_page_offset;
541 uint32_t kind = m_unwindinfo_data.GetU32(&offset); // UNWIND_SECOND_LEVEL_REGULAR or UNWIND_SECOND_LEVEL_COMPRESSED
543 if (kind == UNWIND_SECOND_LEVEL_REGULAR)
545 // struct unwind_info_regular_second_level_page_header
547 // uint32_t kind; // UNWIND_SECOND_LEVEL_REGULAR
548 // uint16_t entryPageOffset;
549 // uint16_t entryCount;
551 // typedef uint32_t compact_unwind_encoding_t;
552 // struct unwind_info_regular_second_level_entry
554 // uint32_t functionOffset;
555 // compact_unwind_encoding_t encoding;
557 uint16_t entry_page_offset = m_unwindinfo_data.GetU16(&offset); // entryPageOffset
558 uint16_t entry_count = m_unwindinfo_data.GetU16(&offset); // entryCount
560 offset_t entry_offset = BinarySearchRegularSecondPage (second_page_offset + entry_page_offset, entry_count, function_offset, &unwind_info.valid_range_offset_start, &unwind_info.valid_range_offset_end);
561 if (entry_offset == LLDB_INVALID_OFFSET)
565 entry_offset += 4; // skip over functionOffset
566 unwind_info.encoding = m_unwindinfo_data.GetU32(&entry_offset); // encoding
567 if (unwind_info.encoding & UNWIND_HAS_LSDA)
569 SectionList *sl = m_objfile.GetSectionList ();
572 uint32_t lsda_offset = GetLSDAForFunctionOffset (lsda_array_start, lsda_array_count, function_offset);
573 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
574 unwind_info.lsda_address.ResolveAddressUsingFileSections (objfile_header_file_address + lsda_offset, sl);
577 if (unwind_info.encoding & UNWIND_PERSONALITY_MASK)
579 uint32_t personality_index = EXTRACT_BITS (unwind_info.encoding, UNWIND_PERSONALITY_MASK);
581 if (personality_index > 0)
584 if (personality_index < m_unwind_header.personality_array_count)
586 offset_t offset = m_unwind_header.personality_array_offset;
587 offset += 4 * personality_index;
588 SectionList *sl = m_objfile.GetSectionList ();
591 uint32_t personality_offset = m_unwindinfo_data.GetU32(&offset);
592 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
593 unwind_info.personality_ptr_address.ResolveAddressUsingFileSections (objfile_header_file_address + personality_offset, sl);
600 else if (kind == UNWIND_SECOND_LEVEL_COMPRESSED)
602 // struct unwind_info_compressed_second_level_page_header
604 // uint32_t kind; // UNWIND_SECOND_LEVEL_COMPRESSED
605 // uint16_t entryPageOffset; // offset from this 2nd lvl page idx to array of entries
606 // // (an entry has a function offset and index into the encodings)
607 // // NB function offset from the entry in the compressed page
608 // // must be added to the index's functionOffset value.
609 // uint16_t entryCount;
610 // uint16_t encodingsPageOffset; // offset from this 2nd lvl page idx to array of encodings
611 // uint16_t encodingsCount;
613 uint16_t entry_page_offset = m_unwindinfo_data.GetU16(&offset); // entryPageOffset
614 uint16_t entry_count = m_unwindinfo_data.GetU16(&offset); // entryCount
615 uint16_t encodings_page_offset = m_unwindinfo_data.GetU16(&offset); // encodingsPageOffset
616 uint16_t encodings_count = m_unwindinfo_data.GetU16(&offset); // encodingsCount
618 uint32_t encoding_index = BinarySearchCompressedSecondPage (second_page_offset + entry_page_offset, entry_count, function_offset, it->function_offset, &unwind_info.valid_range_offset_start, &unwind_info.valid_range_offset_end);
619 if (encoding_index == UINT32_MAX || encoding_index >= encodings_count + m_unwind_header.common_encodings_array_count)
623 uint32_t encoding = 0;
624 if (encoding_index < m_unwind_header.common_encodings_array_count)
626 offset = m_unwind_header.common_encodings_array_offset + (encoding_index * sizeof (uint32_t));
627 encoding = m_unwindinfo_data.GetU32(&offset); // encoding entry from the commonEncodingsArray
631 uint32_t page_specific_entry_index = encoding_index - m_unwind_header.common_encodings_array_count;
632 offset = second_page_offset + encodings_page_offset + (page_specific_entry_index * sizeof (uint32_t));
633 encoding = m_unwindinfo_data.GetU32(&offset); // encoding entry from the page-specific encoding array
638 unwind_info.encoding = encoding;
639 if (unwind_info.encoding & UNWIND_HAS_LSDA)
641 SectionList *sl = m_objfile.GetSectionList ();
644 uint32_t lsda_offset = GetLSDAForFunctionOffset (lsda_array_start, lsda_array_count, function_offset);
645 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
646 unwind_info.lsda_address.ResolveAddressUsingFileSections (objfile_header_file_address + lsda_offset, sl);
649 if (unwind_info.encoding & UNWIND_PERSONALITY_MASK)
651 uint32_t personality_index = EXTRACT_BITS (unwind_info.encoding, UNWIND_PERSONALITY_MASK);
653 if (personality_index > 0)
656 if (personality_index < m_unwind_header.personality_array_count)
658 offset_t offset = m_unwind_header.personality_array_offset;
659 offset += 4 * personality_index;
660 SectionList *sl = m_objfile.GetSectionList ();
663 uint32_t personality_offset = m_unwindinfo_data.GetU32(&offset);
664 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
665 unwind_info.personality_ptr_address.ResolveAddressUsingFileSections (objfile_header_file_address + personality_offset, sl);
675 enum x86_64_eh_regnum {
692 rip = 16 // this is officially the Return Address register number, but close enough
695 // Convert the compact_unwind_info.h register numbering scheme
696 // to eRegisterKindGCC (eh_frame) register numbering scheme.
698 translate_to_eh_frame_regnum_x86_64 (uint32_t unwind_regno)
700 switch (unwind_regno)
702 case UNWIND_X86_64_REG_RBX:
703 return x86_64_eh_regnum::rbx;
704 case UNWIND_X86_64_REG_R12:
705 return x86_64_eh_regnum::r12;
706 case UNWIND_X86_64_REG_R13:
707 return x86_64_eh_regnum::r13;
708 case UNWIND_X86_64_REG_R14:
709 return x86_64_eh_regnum::r14;
710 case UNWIND_X86_64_REG_R15:
711 return x86_64_eh_regnum::r15;
712 case UNWIND_X86_64_REG_RBP:
713 return x86_64_eh_regnum::rbp;
715 return LLDB_INVALID_REGNUM;
720 CompactUnwindInfo::CreateUnwindPlan_x86_64 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start)
722 unwind_plan.SetSourceName ("compact unwind info");
723 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
724 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
725 unwind_plan.SetRegisterKind (eRegisterKindGCC);
727 unwind_plan.SetLSDAAddress (function_info.lsda_address);
728 unwind_plan.SetPersonalityFunctionPtr (function_info.personality_ptr_address);
730 UnwindPlan::RowSP row (new UnwindPlan::Row);
732 const int wordsize = 8;
733 int mode = function_info.encoding & UNWIND_X86_64_MODE_MASK;
736 case UNWIND_X86_64_MODE_RBP_FRAME:
738 row->GetCFAValue().SetIsRegisterPlusOffset (
739 translate_to_eh_frame_regnum_x86_64 (UNWIND_X86_64_REG_RBP),
742 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rbp, wordsize * -2, true);
743 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rip, wordsize * -1, true);
744 row->SetRegisterLocationToIsCFAPlusOffset (x86_64_eh_regnum::rsp, 0, true);
746 uint32_t saved_registers_offset = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_RBP_FRAME_OFFSET);
748 uint32_t saved_registers_locations = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_RBP_FRAME_REGISTERS);
750 saved_registers_offset += 2;
752 for (int i = 0; i < 5; i++)
754 uint32_t regnum = saved_registers_locations & 0x7;
757 case UNWIND_X86_64_REG_NONE:
759 case UNWIND_X86_64_REG_RBX:
760 case UNWIND_X86_64_REG_R12:
761 case UNWIND_X86_64_REG_R13:
762 case UNWIND_X86_64_REG_R14:
763 case UNWIND_X86_64_REG_R15:
764 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_x86_64 (regnum), wordsize * -saved_registers_offset, true);
767 saved_registers_offset--;
768 saved_registers_locations >>= 3;
770 unwind_plan.AppendRow (row);
775 case UNWIND_X86_64_MODE_STACK_IND:
777 // The clang in Xcode 6 is emitting incorrect compact unwind encodings for this
778 // style of unwind. It was fixed in llvm r217020.
779 // The clang in Xcode 7 has this fixed.
784 case UNWIND_X86_64_MODE_STACK_IMMD:
786 uint32_t stack_size = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_FRAMELESS_STACK_SIZE);
787 uint32_t register_count = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT);
788 uint32_t permutation = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION);
790 if (mode == UNWIND_X86_64_MODE_STACK_IND && function_info.valid_range_offset_start != 0)
792 uint32_t stack_adjust = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_FRAMELESS_STACK_ADJUST);
794 // offset into the function instructions; 0 == beginning of first instruction
795 uint32_t offset_to_subl_insn = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_FRAMELESS_STACK_SIZE);
797 SectionList *sl = m_objfile.GetSectionList ();
800 ProcessSP process_sp = target.GetProcessSP();
803 Address subl_payload_addr (function_info.valid_range_offset_start, sl);
804 subl_payload_addr.Slide (offset_to_subl_insn);
806 uint64_t large_stack_size = process_sp->ReadUnsignedIntegerFromMemory (subl_payload_addr.GetLoadAddress (&target),
808 if (large_stack_size != 0 && error.Success ())
810 // Got the large stack frame size correctly - use it
811 stack_size = large_stack_size + (stack_adjust * wordsize);
829 int32_t offset = mode == UNWIND_X86_64_MODE_STACK_IND ? stack_size : stack_size * wordsize;
830 row->GetCFAValue().SetIsRegisterPlusOffset (x86_64_eh_regnum::rsp, offset);
833 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rip, wordsize * -1, true);
834 row->SetRegisterLocationToIsCFAPlusOffset (x86_64_eh_regnum::rsp, 0, true);
836 if (register_count > 0)
839 // We need to include (up to) 6 registers in 10 bits.
840 // That would be 18 bits if we just used 3 bits per reg to indicate
841 // the order they're saved on the stack.
843 // This is done with Lehmer code permutation, e.g. see
844 // http://stackoverflow.com/questions/1506078/fast-permutation-number-permutation-mapping-algorithms
845 int permunreg[6] = {0, 0, 0, 0, 0, 0};
847 // This decodes the variable-base number in the 10 bits
848 // and gives us the Lehmer code sequence which can then
851 switch (register_count)
854 permunreg[0] = permutation/120; // 120 == 5!
855 permutation -= (permunreg[0]*120);
856 permunreg[1] = permutation/24; // 24 == 4!
857 permutation -= (permunreg[1]*24);
858 permunreg[2] = permutation/6; // 6 == 3!
859 permutation -= (permunreg[2]*6);
860 permunreg[3] = permutation/2; // 2 == 2!
861 permutation -= (permunreg[3]*2);
862 permunreg[4] = permutation; // 1 == 1!
866 permunreg[0] = permutation/120;
867 permutation -= (permunreg[0]*120);
868 permunreg[1] = permutation/24;
869 permutation -= (permunreg[1]*24);
870 permunreg[2] = permutation/6;
871 permutation -= (permunreg[2]*6);
872 permunreg[3] = permutation/2;
873 permutation -= (permunreg[3]*2);
874 permunreg[4] = permutation;
877 permunreg[0] = permutation/60;
878 permutation -= (permunreg[0]*60);
879 permunreg[1] = permutation/12;
880 permutation -= (permunreg[1]*12);
881 permunreg[2] = permutation/3;
882 permutation -= (permunreg[2]*3);
883 permunreg[3] = permutation;
886 permunreg[0] = permutation/20;
887 permutation -= (permunreg[0]*20);
888 permunreg[1] = permutation/4;
889 permutation -= (permunreg[1]*4);
890 permunreg[2] = permutation;
893 permunreg[0] = permutation/5;
894 permutation -= (permunreg[0]*5);
895 permunreg[1] = permutation;
898 permunreg[0] = permutation;
902 // Decode the Lehmer code for this permutation of
903 // the registers v. http://en.wikipedia.org/wiki/Lehmer_code
905 int registers[6] = { UNWIND_X86_64_REG_NONE, UNWIND_X86_64_REG_NONE, UNWIND_X86_64_REG_NONE, UNWIND_X86_64_REG_NONE, UNWIND_X86_64_REG_NONE, UNWIND_X86_64_REG_NONE };
906 bool used[7] = { false, false, false, false, false, false, false };
907 for (uint32_t i = 0; i < register_count; i++)
910 for (int j = 1; j < 7; j++)
912 if (used[j] == false)
914 if (renum == permunreg[i])
925 uint32_t saved_registers_offset = 1;
926 saved_registers_offset++;
928 for (int i = (sizeof (registers) / sizeof (int)) - 1; i >= 0; i--)
930 switch (registers[i])
932 case UNWIND_X86_64_REG_NONE:
934 case UNWIND_X86_64_REG_RBX:
935 case UNWIND_X86_64_REG_R12:
936 case UNWIND_X86_64_REG_R13:
937 case UNWIND_X86_64_REG_R14:
938 case UNWIND_X86_64_REG_R15:
939 case UNWIND_X86_64_REG_RBP:
940 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_x86_64 (registers[i]), wordsize * -saved_registers_offset, true);
941 saved_registers_offset++;
946 unwind_plan.AppendRow (row);
951 case UNWIND_X86_64_MODE_DWARF:
966 enum i386_eh_regnum {
975 eip = 8 // this is officially the Return Address register number, but close enough
978 // Convert the compact_unwind_info.h register numbering scheme
979 // to eRegisterKindGCC (eh_frame) register numbering scheme.
981 translate_to_eh_frame_regnum_i386 (uint32_t unwind_regno)
983 switch (unwind_regno)
985 case UNWIND_X86_REG_EBX:
986 return i386_eh_regnum::ebx;
987 case UNWIND_X86_REG_ECX:
988 return i386_eh_regnum::ecx;
989 case UNWIND_X86_REG_EDX:
990 return i386_eh_regnum::edx;
991 case UNWIND_X86_REG_EDI:
992 return i386_eh_regnum::edi;
993 case UNWIND_X86_REG_ESI:
994 return i386_eh_regnum::esi;
995 case UNWIND_X86_REG_EBP:
996 return i386_eh_regnum::ebp;
998 return LLDB_INVALID_REGNUM;
1004 CompactUnwindInfo::CreateUnwindPlan_i386 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start)
1006 unwind_plan.SetSourceName ("compact unwind info");
1007 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
1008 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
1009 unwind_plan.SetRegisterKind (eRegisterKindGCC);
1011 unwind_plan.SetLSDAAddress (function_info.lsda_address);
1012 unwind_plan.SetPersonalityFunctionPtr (function_info.personality_ptr_address);
1014 UnwindPlan::RowSP row (new UnwindPlan::Row);
1016 const int wordsize = 4;
1017 int mode = function_info.encoding & UNWIND_X86_MODE_MASK;
1020 case UNWIND_X86_MODE_EBP_FRAME:
1022 row->GetCFAValue().SetIsRegisterPlusOffset (
1023 translate_to_eh_frame_regnum_i386 (UNWIND_X86_REG_EBP), 2 * wordsize);
1025 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::ebp, wordsize * -2, true);
1026 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::eip, wordsize * -1, true);
1027 row->SetRegisterLocationToIsCFAPlusOffset (i386_eh_regnum::esp, 0, true);
1029 uint32_t saved_registers_offset = EXTRACT_BITS (function_info.encoding, UNWIND_X86_EBP_FRAME_OFFSET);
1031 uint32_t saved_registers_locations = EXTRACT_BITS (function_info.encoding, UNWIND_X86_EBP_FRAME_REGISTERS);
1033 saved_registers_offset += 2;
1035 for (int i = 0; i < 5; i++)
1037 uint32_t regnum = saved_registers_locations & 0x7;
1040 case UNWIND_X86_REG_NONE:
1042 case UNWIND_X86_REG_EBX:
1043 case UNWIND_X86_REG_ECX:
1044 case UNWIND_X86_REG_EDX:
1045 case UNWIND_X86_REG_EDI:
1046 case UNWIND_X86_REG_ESI:
1047 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_i386 (regnum), wordsize * -saved_registers_offset, true);
1050 saved_registers_offset--;
1051 saved_registers_locations >>= 3;
1053 unwind_plan.AppendRow (row);
1058 case UNWIND_X86_MODE_STACK_IND:
1059 case UNWIND_X86_MODE_STACK_IMMD:
1061 uint32_t stack_size = EXTRACT_BITS (function_info.encoding, UNWIND_X86_FRAMELESS_STACK_SIZE);
1062 uint32_t register_count = EXTRACT_BITS (function_info.encoding, UNWIND_X86_FRAMELESS_STACK_REG_COUNT);
1063 uint32_t permutation = EXTRACT_BITS (function_info.encoding, UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION);
1065 if (mode == UNWIND_X86_MODE_STACK_IND && function_info.valid_range_offset_start != 0)
1067 uint32_t stack_adjust = EXTRACT_BITS (function_info.encoding, UNWIND_X86_FRAMELESS_STACK_ADJUST);
1069 // offset into the function instructions; 0 == beginning of first instruction
1070 uint32_t offset_to_subl_insn = EXTRACT_BITS (function_info.encoding, UNWIND_X86_FRAMELESS_STACK_SIZE);
1072 SectionList *sl = m_objfile.GetSectionList ();
1075 ProcessSP process_sp = target.GetProcessSP();
1078 Address subl_payload_addr (function_info.valid_range_offset_start, sl);
1079 subl_payload_addr.Slide (offset_to_subl_insn);
1081 uint64_t large_stack_size = process_sp->ReadUnsignedIntegerFromMemory (subl_payload_addr.GetLoadAddress (&target),
1083 if (large_stack_size != 0 && error.Success ())
1085 // Got the large stack frame size correctly - use it
1086 stack_size = large_stack_size + (stack_adjust * wordsize);
1104 int32_t offset = mode == UNWIND_X86_MODE_STACK_IND ? stack_size : stack_size * wordsize;
1105 row->GetCFAValue().SetIsRegisterPlusOffset (i386_eh_regnum::esp, offset);
1107 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::eip, wordsize * -1, true);
1108 row->SetRegisterLocationToIsCFAPlusOffset (i386_eh_regnum::esp, 0, true);
1110 if (register_count > 0)
1113 // We need to include (up to) 6 registers in 10 bits.
1114 // That would be 18 bits if we just used 3 bits per reg to indicate
1115 // the order they're saved on the stack.
1117 // This is done with Lehmer code permutation, e.g. see
1118 // http://stackoverflow.com/questions/1506078/fast-permutation-number-permutation-mapping-algorithms
1119 int permunreg[6] = {0, 0, 0, 0, 0, 0};
1121 // This decodes the variable-base number in the 10 bits
1122 // and gives us the Lehmer code sequence which can then
1125 switch (register_count)
1128 permunreg[0] = permutation/120; // 120 == 5!
1129 permutation -= (permunreg[0]*120);
1130 permunreg[1] = permutation/24; // 24 == 4!
1131 permutation -= (permunreg[1]*24);
1132 permunreg[2] = permutation/6; // 6 == 3!
1133 permutation -= (permunreg[2]*6);
1134 permunreg[3] = permutation/2; // 2 == 2!
1135 permutation -= (permunreg[3]*2);
1136 permunreg[4] = permutation; // 1 == 1!
1140 permunreg[0] = permutation/120;
1141 permutation -= (permunreg[0]*120);
1142 permunreg[1] = permutation/24;
1143 permutation -= (permunreg[1]*24);
1144 permunreg[2] = permutation/6;
1145 permutation -= (permunreg[2]*6);
1146 permunreg[3] = permutation/2;
1147 permutation -= (permunreg[3]*2);
1148 permunreg[4] = permutation;
1151 permunreg[0] = permutation/60;
1152 permutation -= (permunreg[0]*60);
1153 permunreg[1] = permutation/12;
1154 permutation -= (permunreg[1]*12);
1155 permunreg[2] = permutation/3;
1156 permutation -= (permunreg[2]*3);
1157 permunreg[3] = permutation;
1160 permunreg[0] = permutation/20;
1161 permutation -= (permunreg[0]*20);
1162 permunreg[1] = permutation/4;
1163 permutation -= (permunreg[1]*4);
1164 permunreg[2] = permutation;
1167 permunreg[0] = permutation/5;
1168 permutation -= (permunreg[0]*5);
1169 permunreg[1] = permutation;
1172 permunreg[0] = permutation;
1176 // Decode the Lehmer code for this permutation of
1177 // the registers v. http://en.wikipedia.org/wiki/Lehmer_code
1179 int registers[6] = { UNWIND_X86_REG_NONE, UNWIND_X86_REG_NONE, UNWIND_X86_REG_NONE, UNWIND_X86_REG_NONE, UNWIND_X86_REG_NONE, UNWIND_X86_REG_NONE };
1180 bool used[7] = { false, false, false, false, false, false, false };
1181 for (uint32_t i = 0; i < register_count; i++)
1184 for (int j = 1; j < 7; j++)
1186 if (used[j] == false)
1188 if (renum == permunreg[i])
1199 uint32_t saved_registers_offset = 1;
1200 saved_registers_offset++;
1202 for (int i = (sizeof (registers) / sizeof (int)) - 1; i >= 0; i--)
1204 switch (registers[i])
1206 case UNWIND_X86_REG_NONE:
1208 case UNWIND_X86_REG_EBX:
1209 case UNWIND_X86_REG_ECX:
1210 case UNWIND_X86_REG_EDX:
1211 case UNWIND_X86_REG_EDI:
1212 case UNWIND_X86_REG_ESI:
1213 case UNWIND_X86_REG_EBP:
1214 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_i386 (registers[i]), wordsize * -saved_registers_offset, true);
1215 saved_registers_offset++;
1221 unwind_plan.AppendRow (row);
1226 case UNWIND_X86_MODE_DWARF: