130fdc8d8SChris Lattner //===-- DWARFCallFrameInfo.cpp ----------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner // C Includes
1130fdc8d8SChris Lattner // C++ Includes
1230fdc8d8SChris Lattner #include <list>
1330fdc8d8SChris Lattner 
1430fdc8d8SChris Lattner #include "lldb/Core/ArchSpec.h"
1530fdc8d8SChris Lattner #include "lldb/Core/Module.h"
1630fdc8d8SChris Lattner #include "lldb/Core/Section.h"
1774303829SJason Molenda #include "lldb/Core/Timer.h"
18*29cb868aSZachary Turner #include "lldb/Core/dwarf.h"
19e38a5eddSGreg Clayton #include "lldb/Host/Host.h"
20e38a5eddSGreg Clayton #include "lldb/Symbol/DWARFCallFrameInfo.h"
21e38a5eddSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
22fbcb7f2cSJason Molenda #include "lldb/Symbol/UnwindPlan.h"
23e38a5eddSGreg Clayton #include "lldb/Target/RegisterContext.h"
24e38a5eddSGreg Clayton #include "lldb/Target/Thread.h"
256f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2630fdc8d8SChris Lattner 
2730fdc8d8SChris Lattner using namespace lldb;
2830fdc8d8SChris Lattner using namespace lldb_private;
2930fdc8d8SChris Lattner 
30*29cb868aSZachary Turner //----------------------------------------------------------------------
31*29cb868aSZachary Turner // GetDwarfEHPtr
32*29cb868aSZachary Turner //
33*29cb868aSZachary Turner // Used for calls when the value type is specified by a DWARF EH Frame
34*29cb868aSZachary Turner // pointer encoding.
35*29cb868aSZachary Turner //----------------------------------------------------------------------
36*29cb868aSZachary Turner static uint64_t
37*29cb868aSZachary Turner GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr,
38*29cb868aSZachary Turner                 uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr,
39*29cb868aSZachary Turner                 addr_t data_addr) //, BSDRelocs *data_relocs) const
40*29cb868aSZachary Turner {
41*29cb868aSZachary Turner   if (eh_ptr_enc == DW_EH_PE_omit)
42*29cb868aSZachary Turner     return ULLONG_MAX; // Value isn't in the buffer...
43*29cb868aSZachary Turner 
44*29cb868aSZachary Turner   uint64_t baseAddress = 0;
45*29cb868aSZachary Turner   uint64_t addressValue = 0;
46*29cb868aSZachary Turner   const uint32_t addr_size = DE.GetAddressByteSize();
47*29cb868aSZachary Turner #ifdef LLDB_CONFIGURATION_DEBUG
48*29cb868aSZachary Turner   assert(addr_size == 4 || addr_size == 8);
49*29cb868aSZachary Turner #endif
50*29cb868aSZachary Turner 
51*29cb868aSZachary Turner   bool signExtendValue = false;
52*29cb868aSZachary Turner   // Decode the base part or adjust our offset
53*29cb868aSZachary Turner   switch (eh_ptr_enc & 0x70) {
54*29cb868aSZachary Turner   case DW_EH_PE_pcrel:
55*29cb868aSZachary Turner     signExtendValue = true;
56*29cb868aSZachary Turner     baseAddress = *offset_ptr;
57*29cb868aSZachary Turner     if (pc_rel_addr != LLDB_INVALID_ADDRESS)
58*29cb868aSZachary Turner       baseAddress += pc_rel_addr;
59*29cb868aSZachary Turner     //      else
60*29cb868aSZachary Turner     //          Log::GlobalWarning ("PC relative pointer encoding found with
61*29cb868aSZachary Turner     //          invalid pc relative address.");
62*29cb868aSZachary Turner     break;
63*29cb868aSZachary Turner 
64*29cb868aSZachary Turner   case DW_EH_PE_textrel:
65*29cb868aSZachary Turner     signExtendValue = true;
66*29cb868aSZachary Turner     if (text_addr != LLDB_INVALID_ADDRESS)
67*29cb868aSZachary Turner       baseAddress = text_addr;
68*29cb868aSZachary Turner     //      else
69*29cb868aSZachary Turner     //          Log::GlobalWarning ("text relative pointer encoding being
70*29cb868aSZachary Turner     //          decoded with invalid text section address, setting base address
71*29cb868aSZachary Turner     //          to zero.");
72*29cb868aSZachary Turner     break;
73*29cb868aSZachary Turner 
74*29cb868aSZachary Turner   case DW_EH_PE_datarel:
75*29cb868aSZachary Turner     signExtendValue = true;
76*29cb868aSZachary Turner     if (data_addr != LLDB_INVALID_ADDRESS)
77*29cb868aSZachary Turner       baseAddress = data_addr;
78*29cb868aSZachary Turner     //      else
79*29cb868aSZachary Turner     //          Log::GlobalWarning ("data relative pointer encoding being
80*29cb868aSZachary Turner     //          decoded with invalid data section address, setting base address
81*29cb868aSZachary Turner     //          to zero.");
82*29cb868aSZachary Turner     break;
83*29cb868aSZachary Turner 
84*29cb868aSZachary Turner   case DW_EH_PE_funcrel:
85*29cb868aSZachary Turner     signExtendValue = true;
86*29cb868aSZachary Turner     break;
87*29cb868aSZachary Turner 
88*29cb868aSZachary Turner   case DW_EH_PE_aligned: {
89*29cb868aSZachary Turner     // SetPointerSize should be called prior to extracting these so the
90*29cb868aSZachary Turner     // pointer size is cached
91*29cb868aSZachary Turner     assert(addr_size != 0);
92*29cb868aSZachary Turner     if (addr_size) {
93*29cb868aSZachary Turner       // Align to a address size boundary first
94*29cb868aSZachary Turner       uint32_t alignOffset = *offset_ptr % addr_size;
95*29cb868aSZachary Turner       if (alignOffset)
96*29cb868aSZachary Turner         offset_ptr += addr_size - alignOffset;
97*29cb868aSZachary Turner     }
98*29cb868aSZachary Turner   } break;
99*29cb868aSZachary Turner 
100*29cb868aSZachary Turner   default:
101*29cb868aSZachary Turner     break;
102*29cb868aSZachary Turner   }
103*29cb868aSZachary Turner 
104*29cb868aSZachary Turner   // Decode the value part
105*29cb868aSZachary Turner   switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) {
106*29cb868aSZachary Turner   case DW_EH_PE_absptr: {
107*29cb868aSZachary Turner     addressValue = DE.GetAddress(offset_ptr);
108*29cb868aSZachary Turner     //          if (data_relocs)
109*29cb868aSZachary Turner     //              addressValue = data_relocs->Relocate(*offset_ptr -
110*29cb868aSZachary Turner     //              addr_size, *this, addressValue);
111*29cb868aSZachary Turner   } break;
112*29cb868aSZachary Turner   case DW_EH_PE_uleb128:
113*29cb868aSZachary Turner     addressValue = DE.GetULEB128(offset_ptr);
114*29cb868aSZachary Turner     break;
115*29cb868aSZachary Turner   case DW_EH_PE_udata2:
116*29cb868aSZachary Turner     addressValue = DE.GetU16(offset_ptr);
117*29cb868aSZachary Turner     break;
118*29cb868aSZachary Turner   case DW_EH_PE_udata4:
119*29cb868aSZachary Turner     addressValue = DE.GetU32(offset_ptr);
120*29cb868aSZachary Turner     break;
121*29cb868aSZachary Turner   case DW_EH_PE_udata8:
122*29cb868aSZachary Turner     addressValue = DE.GetU64(offset_ptr);
123*29cb868aSZachary Turner     break;
124*29cb868aSZachary Turner   case DW_EH_PE_sleb128:
125*29cb868aSZachary Turner     addressValue = DE.GetSLEB128(offset_ptr);
126*29cb868aSZachary Turner     break;
127*29cb868aSZachary Turner   case DW_EH_PE_sdata2:
128*29cb868aSZachary Turner     addressValue = (int16_t)DE.GetU16(offset_ptr);
129*29cb868aSZachary Turner     break;
130*29cb868aSZachary Turner   case DW_EH_PE_sdata4:
131*29cb868aSZachary Turner     addressValue = (int32_t)DE.GetU32(offset_ptr);
132*29cb868aSZachary Turner     break;
133*29cb868aSZachary Turner   case DW_EH_PE_sdata8:
134*29cb868aSZachary Turner     addressValue = (int64_t)DE.GetU64(offset_ptr);
135*29cb868aSZachary Turner     break;
136*29cb868aSZachary Turner   default:
137*29cb868aSZachary Turner     // Unhandled encoding type
138*29cb868aSZachary Turner     assert(eh_ptr_enc);
139*29cb868aSZachary Turner     break;
140*29cb868aSZachary Turner   }
141*29cb868aSZachary Turner 
142*29cb868aSZachary Turner   // Since we promote everything to 64 bit, we may need to sign extend
143*29cb868aSZachary Turner   if (signExtendValue && addr_size < sizeof(baseAddress)) {
144*29cb868aSZachary Turner     uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
145*29cb868aSZachary Turner     if (sign_bit & addressValue) {
146*29cb868aSZachary Turner       uint64_t mask = ~sign_bit + 1;
147*29cb868aSZachary Turner       addressValue |= mask;
148*29cb868aSZachary Turner     }
149*29cb868aSZachary Turner   }
150*29cb868aSZachary Turner   return baseAddress + addressValue;
151*29cb868aSZachary Turner }
152*29cb868aSZachary Turner 
153b9c1b51eSKate Stone DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile &objfile,
154b9c1b51eSKate Stone                                        SectionSP &section_sp,
155b9c1b51eSKate Stone                                        lldb::RegisterKind reg_kind,
156b9c1b51eSKate Stone                                        bool is_eh_frame)
157b9c1b51eSKate Stone     : m_objfile(objfile), m_section_sp(section_sp),
158b9c1b51eSKate Stone       m_reg_kind(reg_kind), // The flavor of registers that the CFI data uses
159b9c1b51eSKate Stone                             // (enum RegisterKind)
160b9c1b51eSKate Stone       m_flags(), m_cie_map(), m_cfi_data(), m_cfi_data_initialized(false),
161b9c1b51eSKate Stone       m_fde_index(), m_fde_index_initialized(false),
162b9c1b51eSKate Stone       m_is_eh_frame(is_eh_frame) {}
16330fdc8d8SChris Lattner 
164b9c1b51eSKate Stone DWARFCallFrameInfo::~DWARFCallFrameInfo() {}
16530fdc8d8SChris Lattner 
166b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetUnwindPlan(Address addr, UnwindPlan &unwind_plan) {
16774303829SJason Molenda   FDEEntryMap::Entry fde_entry;
16874303829SJason Molenda 
16974303829SJason Molenda   // Make sure that the Address we're searching for is the same object file
17074303829SJason Molenda   // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
17174303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
172b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
173b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
174fbcb7f2cSJason Molenda     return false;
17574303829SJason Molenda 
17674303829SJason Molenda   if (GetFDEEntryByFileAddress(addr.GetFileAddress(), fde_entry) == false)
17774303829SJason Molenda     return false;
17874303829SJason Molenda   return FDEToUnwindPlan(fde_entry.data, addr, unwind_plan);
17930fdc8d8SChris Lattner }
18030fdc8d8SChris Lattner 
181b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) {
18274303829SJason Molenda 
18374303829SJason Molenda   // Make sure that the Address we're searching for is the same object file
18474303829SJason Molenda   // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
18574303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
186b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
187b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
18874303829SJason Molenda     return false;
18974303829SJason Molenda 
190d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
191fbcb7f2cSJason Molenda     return false;
192fbcb7f2cSJason Molenda   GetFDEIndex();
193b9c1b51eSKate Stone   FDEEntryMap::Entry *fde_entry =
194b9c1b51eSKate Stone       m_fde_index.FindEntryThatContains(addr.GetFileAddress());
19574303829SJason Molenda   if (!fde_entry)
196fbcb7f2cSJason Molenda     return false;
197fbcb7f2cSJason Molenda 
198b9c1b51eSKate Stone   range = AddressRange(fde_entry->base, fde_entry->size,
199b9c1b51eSKate Stone                        m_objfile.GetSectionList());
200fbcb7f2cSJason Molenda   return true;
20130fdc8d8SChris Lattner }
20230fdc8d8SChris Lattner 
203b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetFDEEntryByFileAddress(
204b9c1b51eSKate Stone     addr_t file_addr, FDEEntryMap::Entry &fde_entry) {
205d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
206fbcb7f2cSJason Molenda     return false;
20774303829SJason Molenda 
20874303829SJason Molenda   GetFDEIndex();
20974303829SJason Molenda 
21074303829SJason Molenda   if (m_fde_index.IsEmpty())
21174303829SJason Molenda     return false;
21274303829SJason Molenda 
21374303829SJason Molenda   FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains(file_addr);
21474303829SJason Molenda 
215d4612ad0SEd Maste   if (fde == nullptr)
21674303829SJason Molenda     return false;
21774303829SJason Molenda 
21874303829SJason Molenda   fde_entry = *fde;
21974303829SJason Molenda   return true;
22030fdc8d8SChris Lattner }
22130fdc8d8SChris Lattner 
222b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFunctionAddressAndSizeVector(
223b9c1b51eSKate Stone     FunctionAddressAndSizeVector &function_info) {
2245635f77aSJason Molenda   GetFDEIndex();
2255635f77aSJason Molenda   const size_t count = m_fde_index.GetSize();
226ee7593fbSJason Molenda   function_info.Clear();
227c1946cd8SJason Molenda   if (count > 0)
228ee7593fbSJason Molenda     function_info.Reserve(count);
229b9c1b51eSKate Stone   for (size_t i = 0; i < count; ++i) {
230b9c1b51eSKate Stone     const FDEEntryMap::Entry *func_offset_data_entry =
231b9c1b51eSKate Stone         m_fde_index.GetEntryAtIndex(i);
232b9c1b51eSKate Stone     if (func_offset_data_entry) {
233b9c1b51eSKate Stone       FunctionAddressAndSizeVector::Entry function_offset_entry(
234b9c1b51eSKate Stone           func_offset_data_entry->base, func_offset_data_entry->size);
2355635f77aSJason Molenda       function_info.Append(function_offset_entry);
2365635f77aSJason Molenda     }
2375635f77aSJason Molenda   }
2385635f77aSJason Molenda }
2395635f77aSJason Molenda 
24030fdc8d8SChris Lattner const DWARFCallFrameInfo::CIE *
241b9c1b51eSKate Stone DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) {
24230fdc8d8SChris Lattner   cie_map_t::iterator pos = m_cie_map.find(cie_offset);
24330fdc8d8SChris Lattner 
244b9c1b51eSKate Stone   if (pos != m_cie_map.end()) {
24530fdc8d8SChris Lattner     // Parse and cache the CIE
246d4612ad0SEd Maste     if (pos->second.get() == nullptr)
24730fdc8d8SChris Lattner       pos->second = ParseCIE(cie_offset);
24830fdc8d8SChris Lattner 
24930fdc8d8SChris Lattner     return pos->second.get();
25030fdc8d8SChris Lattner   }
251d4612ad0SEd Maste   return nullptr;
25230fdc8d8SChris Lattner }
25330fdc8d8SChris Lattner 
254fbcb7f2cSJason Molenda DWARFCallFrameInfo::CIESP
255b9c1b51eSKate Stone DWARFCallFrameInfo::ParseCIE(const dw_offset_t cie_offset) {
256fbcb7f2cSJason Molenda   CIESP cie_sp(new CIE(cie_offset));
257c7bece56SGreg Clayton   lldb::offset_t offset = cie_offset;
258fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
259f97c5213SGreg Clayton     GetCFIData();
2601767e115STodd Fiala   uint32_t length = m_cfi_data.GetU32(&offset);
2611767e115STodd Fiala   dw_offset_t cie_id, end_offset;
2621767e115STodd Fiala   bool is_64bit = (length == UINT32_MAX);
2631767e115STodd Fiala   if (is_64bit) {
2641767e115STodd Fiala     length = m_cfi_data.GetU64(&offset);
2651767e115STodd Fiala     cie_id = m_cfi_data.GetU64(&offset);
2661767e115STodd Fiala     end_offset = cie_offset + length + 12;
2671767e115STodd Fiala   } else {
2681767e115STodd Fiala     cie_id = m_cfi_data.GetU32(&offset);
2691767e115STodd Fiala     end_offset = cie_offset + length + 4;
2701767e115STodd Fiala   }
271b9c1b51eSKate Stone   if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) ||
272b9c1b51eSKate Stone                      (m_is_eh_frame && cie_id == 0ul))) {
27330fdc8d8SChris Lattner     size_t i;
27430fdc8d8SChris Lattner     //    cie.offset = cie_offset;
27530fdc8d8SChris Lattner     //    cie.length = length;
27630fdc8d8SChris Lattner     //    cie.cieID = cieID;
2776e264d39SAshok Thirumurthi     cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
27830fdc8d8SChris Lattner     cie_sp->version = m_cfi_data.GetU8(&offset);
27930fdc8d8SChris Lattner 
280b9c1b51eSKate Stone     for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) {
28130fdc8d8SChris Lattner       cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
282b9c1b51eSKate Stone       if (cie_sp->augmentation[i] == '\0') {
28330fdc8d8SChris Lattner         // Zero out remaining bytes in augmentation string
28430fdc8d8SChris Lattner         for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j)
28530fdc8d8SChris Lattner           cie_sp->augmentation[j] = '\0';
28630fdc8d8SChris Lattner 
28730fdc8d8SChris Lattner         break;
28830fdc8d8SChris Lattner       }
28930fdc8d8SChris Lattner     }
29030fdc8d8SChris Lattner 
291b9c1b51eSKate Stone     if (i == CFI_AUG_MAX_SIZE &&
292b9c1b51eSKate Stone         cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') {
293b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError,
294b9c1b51eSKate Stone                       "CIE parse error: CIE augmentation string was too large "
295b9c1b51eSKate Stone                       "for the fixed sized buffer of %d bytes.\n",
296b9c1b51eSKate Stone                       CFI_AUG_MAX_SIZE);
29730fdc8d8SChris Lattner       return cie_sp;
29830fdc8d8SChris Lattner     }
29930fdc8d8SChris Lattner     cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
30030fdc8d8SChris Lattner     cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
30130fdc8d8SChris Lattner     cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset);
30230fdc8d8SChris Lattner 
303b9c1b51eSKate Stone     if (cie_sp->augmentation[0]) {
30430fdc8d8SChris Lattner       // Get the length of the eh_frame augmentation data
30530fdc8d8SChris Lattner       // which starts with a ULEB128 length in bytes
30630fdc8d8SChris Lattner       const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
30730fdc8d8SChris Lattner       const size_t aug_data_end = offset + aug_data_len;
30830fdc8d8SChris Lattner       const size_t aug_str_len = strlen(cie_sp->augmentation);
30930fdc8d8SChris Lattner       // A 'z' may be present as the first character of the string.
31030fdc8d8SChris Lattner       // If present, the Augmentation Data field shall be present.
31158ef391fSBruce Mitchener       // The contents of the Augmentation Data shall be interpreted
31230fdc8d8SChris Lattner       // according to other characters in the Augmentation String.
313b9c1b51eSKate Stone       if (cie_sp->augmentation[0] == 'z') {
31430fdc8d8SChris Lattner         // Extract the Augmentation Data
31530fdc8d8SChris Lattner         size_t aug_str_idx = 0;
316b9c1b51eSKate Stone         for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) {
31730fdc8d8SChris Lattner           char aug = cie_sp->augmentation[aug_str_idx];
318b9c1b51eSKate Stone           switch (aug) {
31930fdc8d8SChris Lattner           case 'L':
32030fdc8d8SChris Lattner             // Indicates the presence of one argument in the
32130fdc8d8SChris Lattner             // Augmentation Data of the CIE, and a corresponding
32230fdc8d8SChris Lattner             // argument in the Augmentation Data of the FDE. The
32330fdc8d8SChris Lattner             // argument in the Augmentation Data of the CIE is
32430fdc8d8SChris Lattner             // 1-byte and represents the pointer encoding used
32530fdc8d8SChris Lattner             // for the argument in the Augmentation Data of the
32630fdc8d8SChris Lattner             // FDE, which is the address of a language-specific
32730fdc8d8SChris Lattner             // data area (LSDA). The size of the LSDA pointer is
32830fdc8d8SChris Lattner             // specified by the pointer encoding used.
329e9c7ecf6SJason Molenda             cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
33030fdc8d8SChris Lattner             break;
33130fdc8d8SChris Lattner 
33230fdc8d8SChris Lattner           case 'P':
33330fdc8d8SChris Lattner             // Indicates the presence of two arguments in the
334e9c7ecf6SJason Molenda             // Augmentation Data of the CIE. The first argument
33530fdc8d8SChris Lattner             // is 1-byte and represents the pointer encoding
33630fdc8d8SChris Lattner             // used for the second argument, which is the
33730fdc8d8SChris Lattner             // address of a personality routine handler. The
33830fdc8d8SChris Lattner             // size of the personality routine pointer is
33930fdc8d8SChris Lattner             // specified by the pointer encoding used.
340e9c7ecf6SJason Molenda             //
341e9c7ecf6SJason Molenda             // The address of the personality function will
342e9c7ecf6SJason Molenda             // be stored at this location.  Pre-execution, it
343e9c7ecf6SJason Molenda             // will be all zero's so don't read it until we're
344e9c7ecf6SJason Molenda             // trying to do an unwind & the reloc has been
345e9c7ecf6SJason Molenda             // resolved.
34630fdc8d8SChris Lattner             {
34730fdc8d8SChris Lattner               uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
348e9c7ecf6SJason Molenda               const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
349*29cb868aSZachary Turner               cie_sp->personality_loc = GetGNUEHPointer(
350*29cb868aSZachary Turner                   m_cfi_data, &offset, arg_ptr_encoding, pc_rel_addr,
351*29cb868aSZachary Turner                   LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
35230fdc8d8SChris Lattner             }
35330fdc8d8SChris Lattner             break;
35430fdc8d8SChris Lattner 
35530fdc8d8SChris Lattner           case 'R':
35630fdc8d8SChris Lattner             // A 'R' may be present at any position after the
35730fdc8d8SChris Lattner             // first character of the string. The Augmentation
35830fdc8d8SChris Lattner             // Data shall include a 1 byte argument that
35930fdc8d8SChris Lattner             // represents the pointer encoding for the address
36030fdc8d8SChris Lattner             // pointers used in the FDE.
3616e264d39SAshok Thirumurthi             // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4
36230fdc8d8SChris Lattner             cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
36330fdc8d8SChris Lattner             break;
36430fdc8d8SChris Lattner           }
36530fdc8d8SChris Lattner         }
366b9c1b51eSKate Stone       } else if (strcmp(cie_sp->augmentation, "eh") == 0) {
36730fdc8d8SChris Lattner         // If the Augmentation string has the value "eh", then
36830fdc8d8SChris Lattner         // the EH Data field shall be present
36930fdc8d8SChris Lattner       }
37030fdc8d8SChris Lattner 
37130fdc8d8SChris Lattner       // Set the offset to be the end of the augmentation data just in case
37230fdc8d8SChris Lattner       // we didn't understand any of the data.
37330fdc8d8SChris Lattner       offset = (uint32_t)aug_data_end;
37430fdc8d8SChris Lattner     }
37530fdc8d8SChris Lattner 
376b9c1b51eSKate Stone     if (end_offset > offset) {
37730fdc8d8SChris Lattner       cie_sp->inst_offset = offset;
37830fdc8d8SChris Lattner       cie_sp->inst_length = end_offset - offset;
37930fdc8d8SChris Lattner     }
380b9c1b51eSKate Stone     while (offset < end_offset) {
381fbcb7f2cSJason Molenda       uint8_t inst = m_cfi_data.GetU8(&offset);
382fbcb7f2cSJason Molenda       uint8_t primary_opcode = inst & 0xC0;
383fbcb7f2cSJason Molenda       uint8_t extended_opcode = inst & 0x3F;
384fbcb7f2cSJason Molenda 
385b9c1b51eSKate Stone       if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode,
386b9c1b51eSKate Stone                                    cie_sp->data_align, offset,
387b9c1b51eSKate Stone                                    cie_sp->initial_row))
388fbcb7f2cSJason Molenda         break; // Stop if we hit an unrecognized opcode
389fbcb7f2cSJason Molenda     }
39030fdc8d8SChris Lattner   }
39130fdc8d8SChris Lattner 
39230fdc8d8SChris Lattner   return cie_sp;
39330fdc8d8SChris Lattner }
39430fdc8d8SChris Lattner 
395b9c1b51eSKate Stone void DWARFCallFrameInfo::GetCFIData() {
396b9c1b51eSKate Stone   if (m_cfi_data_initialized == false) {
3975160ce5cSGreg Clayton     Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
398f97c5213SGreg Clayton     if (log)
3995160ce5cSGreg Clayton       m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
400c9660546SGreg Clayton     m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data);
401f97c5213SGreg Clayton     m_cfi_data_initialized = true;
402f97c5213SGreg Clayton   }
403f97c5213SGreg Clayton }
404b9c1b51eSKate Stone // Scan through the eh_frame or debug_frame section looking for FDEs and noting
405b9c1b51eSKate Stone // the start/end addresses
406b9c1b51eSKate Stone // of the functions and a pointer back to the function's FDE for later
407b9c1b51eSKate Stone // expansion.
408fbcb7f2cSJason Molenda // Internalize CIEs as we come across them.
40930fdc8d8SChris Lattner 
410b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFDEIndex() {
411d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
412fbcb7f2cSJason Molenda     return;
4137d69f12eSSean Callanan 
414fbcb7f2cSJason Molenda   if (m_fde_index_initialized)
415fbcb7f2cSJason Molenda     return;
416fbcb7f2cSJason Molenda 
417bb19a13cSSaleem Abdulrasool   std::lock_guard<std::mutex> guard(m_fde_index_mutex);
4187d69f12eSSean Callanan 
4197d69f12eSSean Callanan   if (m_fde_index_initialized) // if two threads hit the locker
4207d69f12eSSean Callanan     return;
421e6194f17SJason Molenda 
422b9c1b51eSKate Stone   Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s - %s", LLVM_PRETTY_FUNCTION,
423b9c1b51eSKate Stone                      m_objfile.GetFileSpec().GetFilename().AsCString(""));
42474303829SJason Molenda 
425c7afda5aSJason Molenda   bool clear_address_zeroth_bit = false;
426c7afda5aSJason Molenda   ArchSpec arch;
427b9c1b51eSKate Stone   if (m_objfile.GetArchitecture(arch)) {
428b9c1b51eSKate Stone     if (arch.GetTriple().getArch() == llvm::Triple::arm ||
429b9c1b51eSKate Stone         arch.GetTriple().getArch() == llvm::Triple::thumb)
430c7afda5aSJason Molenda       clear_address_zeroth_bit = true;
431c7afda5aSJason Molenda   }
432c7afda5aSJason Molenda 
433c7bece56SGreg Clayton   lldb::offset_t offset = 0;
434fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
435f97c5213SGreg Clayton     GetCFIData();
436b9c1b51eSKate Stone   while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) {
43773bf5dbdSGreg Clayton     const dw_offset_t current_entry = offset;
4381767e115STodd Fiala     dw_offset_t cie_id, next_entry, cie_offset;
439fbcb7f2cSJason Molenda     uint32_t len = m_cfi_data.GetU32(&offset);
4401767e115STodd Fiala     bool is_64bit = (len == UINT32_MAX);
4411767e115STodd Fiala     if (is_64bit) {
4421767e115STodd Fiala       len = m_cfi_data.GetU64(&offset);
4431767e115STodd Fiala       cie_id = m_cfi_data.GetU64(&offset);
4441767e115STodd Fiala       next_entry = current_entry + len + 12;
4451767e115STodd Fiala       cie_offset = current_entry + 12 - cie_id;
4461767e115STodd Fiala     } else {
4471767e115STodd Fiala       cie_id = m_cfi_data.GetU32(&offset);
4481767e115STodd Fiala       next_entry = current_entry + len + 4;
4491767e115STodd Fiala       cie_offset = current_entry + 4 - cie_id;
4501767e115STodd Fiala     }
451fbcb7f2cSJason Molenda 
452b9c1b51eSKate Stone     if (next_entry > m_cfi_data.GetByteSize() + 1) {
453b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError, "error: Invalid fde/cie next "
454b9c1b51eSKate Stone                                              "entry offset of 0x%x found in "
455b9c1b51eSKate Stone                                              "cie/fde at 0x%x\n",
456b9c1b51eSKate Stone                       next_entry, current_entry);
457e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
458c48ef341SJason Molenda       // invalid data.
459c48ef341SJason Molenda       m_fde_index.Clear();
460c48ef341SJason Molenda       m_fde_index_initialized = true;
461c48ef341SJason Molenda       return;
462c48ef341SJason Molenda     }
463b9c1b51eSKate Stone     if (cie_offset > m_cfi_data.GetByteSize()) {
464b9c1b51eSKate Stone       Host::SystemLog(
465b9c1b51eSKate Stone           Host::eSystemLogError,
466c48ef341SJason Molenda           "error: Invalid cie offset of 0x%x found in cie/fde at 0x%x\n",
467b9c1b51eSKate Stone           cie_offset, current_entry);
468e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
469c48ef341SJason Molenda       // invalid data.
470c48ef341SJason Molenda       m_fde_index.Clear();
471c48ef341SJason Molenda       m_fde_index_initialized = true;
472c48ef341SJason Molenda       return;
473c48ef341SJason Molenda     }
474c48ef341SJason Molenda 
475b9c1b51eSKate Stone     if (cie_id == 0 || cie_id == UINT32_MAX || len == 0) {
476fbcb7f2cSJason Molenda       m_cie_map[current_entry] = ParseCIE(current_entry);
477fbcb7f2cSJason Molenda       offset = next_entry;
478fbcb7f2cSJason Molenda       continue;
479fbcb7f2cSJason Molenda     }
480fbcb7f2cSJason Molenda 
48173bf5dbdSGreg Clayton     const CIE *cie = GetCIE(cie_offset);
482b9c1b51eSKate Stone     if (cie) {
483c9660546SGreg Clayton       const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
484fbcb7f2cSJason Molenda       const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
485fbcb7f2cSJason Molenda       const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
486fbcb7f2cSJason Molenda 
487*29cb868aSZachary Turner       lldb::addr_t addr =
488*29cb868aSZachary Turner           GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
489*29cb868aSZachary Turner                           text_addr, data_addr);
490c7afda5aSJason Molenda       if (clear_address_zeroth_bit)
491c7afda5aSJason Molenda         addr &= ~1ull;
492c7afda5aSJason Molenda 
493*29cb868aSZachary Turner       lldb::addr_t length = GetGNUEHPointer(
494*29cb868aSZachary Turner           m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
495*29cb868aSZachary Turner           pc_rel_addr, text_addr, data_addr);
49674303829SJason Molenda       FDEEntryMap::Entry fde(addr, length, current_entry);
49774303829SJason Molenda       m_fde_index.Append(fde);
498b9c1b51eSKate Stone     } else {
499b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError, "error: unable to find CIE at "
500b9c1b51eSKate Stone                                              "0x%8.8x for cie_id = 0x%8.8x for "
501b9c1b51eSKate Stone                                              "entry at 0x%8.8x.\n",
502b9c1b51eSKate Stone                       cie_offset, cie_id, current_entry);
50373bf5dbdSGreg Clayton     }
504fbcb7f2cSJason Molenda     offset = next_entry;
505fbcb7f2cSJason Molenda   }
50674303829SJason Molenda   m_fde_index.Sort();
507fbcb7f2cSJason Molenda   m_fde_index_initialized = true;
508fbcb7f2cSJason Molenda }
509fbcb7f2cSJason Molenda 
510b9c1b51eSKate Stone bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset,
511b9c1b51eSKate Stone                                          Address startaddr,
512b9c1b51eSKate Stone                                          UnwindPlan &unwind_plan) {
5131ee89eb8SPavel Labath   Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND);
514c7bece56SGreg Clayton   lldb::offset_t offset = dwarf_offset;
515c7bece56SGreg Clayton   lldb::offset_t current_entry = offset;
516fbcb7f2cSJason Molenda 
517d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
518fbcb7f2cSJason Molenda     return false;
519fbcb7f2cSJason Molenda 
520fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
521f97c5213SGreg Clayton     GetCFIData();
522fbcb7f2cSJason Molenda 
523fbcb7f2cSJason Molenda   uint32_t length = m_cfi_data.GetU32(&offset);
5241767e115STodd Fiala   dw_offset_t cie_offset;
5251767e115STodd Fiala   bool is_64bit = (length == UINT32_MAX);
5261767e115STodd Fiala   if (is_64bit) {
5271767e115STodd Fiala     length = m_cfi_data.GetU64(&offset);
5281767e115STodd Fiala     cie_offset = m_cfi_data.GetU64(&offset);
5291767e115STodd Fiala   } else {
5301767e115STodd Fiala     cie_offset = m_cfi_data.GetU32(&offset);
5311767e115STodd Fiala   }
532fbcb7f2cSJason Molenda 
533fbcb7f2cSJason Molenda   assert(cie_offset != 0 && cie_offset != UINT32_MAX);
53430fdc8d8SChris Lattner 
53530fdc8d8SChris Lattner   // Translate the CIE_id from the eh_frame format, which
53630fdc8d8SChris Lattner   // is relative to the FDE offset, into a __eh_frame section
53730fdc8d8SChris Lattner   // offset
538b9c1b51eSKate Stone   if (m_is_eh_frame) {
539ab4f1924SJason Molenda     unwind_plan.SetSourceName("eh_frame CFI");
5401767e115STodd Fiala     cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
54160f0bd49SJason Molenda     unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
542b9c1b51eSKate Stone   } else {
543ab4f1924SJason Molenda     unwind_plan.SetSourceName("DWARF CFI");
54460f0bd49SJason Molenda     // In theory the debug_frame info should be valid at all call sites
54560f0bd49SJason Molenda     // ("asynchronous unwind info" as it is sometimes called) but in practice
54660f0bd49SJason Molenda     // gcc et al all emit call frame info for the prologue and call sites, but
547b9c1b51eSKate Stone     // not for the epilogue or all the other locations during the function
548b9c1b51eSKate Stone     // reliably.
54960f0bd49SJason Molenda     unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
550ab4f1924SJason Molenda   }
55160f0bd49SJason Molenda   unwind_plan.SetSourcedFromCompiler(eLazyBoolYes);
55230fdc8d8SChris Lattner 
55330fdc8d8SChris Lattner   const CIE *cie = GetCIE(cie_offset);
554d4612ad0SEd Maste   assert(cie != nullptr);
555fbcb7f2cSJason Molenda 
5561767e115STodd Fiala   const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
557fbcb7f2cSJason Molenda 
558c9660546SGreg Clayton   const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
55930fdc8d8SChris Lattner   const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
56030fdc8d8SChris Lattner   const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
561*29cb868aSZachary Turner   lldb::addr_t range_base =
562*29cb868aSZachary Turner       GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
563b9c1b51eSKate Stone                       text_addr, data_addr);
564*29cb868aSZachary Turner   lldb::addr_t range_len = GetGNUEHPointer(
565*29cb868aSZachary Turner       m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
566*29cb868aSZachary Turner       pc_rel_addr, text_addr, data_addr);
567b9c1b51eSKate Stone   AddressRange range(range_base, m_objfile.GetAddressByteSize(),
568b9c1b51eSKate Stone                      m_objfile.GetSectionList());
569fbcb7f2cSJason Molenda   range.SetByteSize(range_len);
57030fdc8d8SChris Lattner 
571e9c7ecf6SJason Molenda   addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
572e9c7ecf6SJason Molenda 
573b9c1b51eSKate Stone   if (cie->augmentation[0] == 'z') {
57430fdc8d8SChris Lattner     uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
575b9c1b51eSKate Stone     if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) {
576e9c7ecf6SJason Molenda       offset_t saved_offset = offset;
577*29cb868aSZachary Turner       lsda_data_file_address =
578*29cb868aSZachary Turner           GetGNUEHPointer(m_cfi_data, &offset, cie->lsda_addr_encoding,
579*29cb868aSZachary Turner                           pc_rel_addr, text_addr, data_addr);
580b9c1b51eSKate Stone       if (offset - saved_offset != aug_data_len) {
581e9c7ecf6SJason Molenda         // There is more in the augmentation region than we know how to process;
582e9c7ecf6SJason Molenda         // don't read anything.
583e9c7ecf6SJason Molenda         lsda_data_file_address = LLDB_INVALID_ADDRESS;
584e9c7ecf6SJason Molenda       }
585e9c7ecf6SJason Molenda       offset = saved_offset;
586e9c7ecf6SJason Molenda     }
58730fdc8d8SChris Lattner     offset += aug_data_len;
58830fdc8d8SChris Lattner   }
589e9c7ecf6SJason Molenda   Address lsda_data;
590e9c7ecf6SJason Molenda   Address personality_function_ptr;
591e9c7ecf6SJason Molenda 
592b9c1b51eSKate Stone   if (lsda_data_file_address != LLDB_INVALID_ADDRESS &&
593b9c1b51eSKate Stone       cie->personality_loc != LLDB_INVALID_ADDRESS) {
594b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address,
595b9c1b51eSKate Stone                                               lsda_data);
596b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc,
597b9c1b51eSKate Stone                                               personality_function_ptr);
598e9c7ecf6SJason Molenda   }
599e9c7ecf6SJason Molenda 
600b9c1b51eSKate Stone   if (lsda_data.IsValid() && personality_function_ptr.IsValid()) {
601e9c7ecf6SJason Molenda     unwind_plan.SetLSDAAddress(lsda_data);
602e9c7ecf6SJason Molenda     unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr);
603e9c7ecf6SJason Molenda   }
60430fdc8d8SChris Lattner 
60530fdc8d8SChris Lattner   uint32_t code_align = cie->code_align;
60630fdc8d8SChris Lattner   int32_t data_align = cie->data_align;
60730fdc8d8SChris Lattner 
608fbcb7f2cSJason Molenda   unwind_plan.SetPlanValidAddressRange(range);
6091d42c7bcSJason Molenda   UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
6101d42c7bcSJason Molenda   *cie_initial_row = cie->initial_row;
6111d42c7bcSJason Molenda   UnwindPlan::RowSP row(cie_initial_row);
61230fdc8d8SChris Lattner 
613fbcb7f2cSJason Molenda   unwind_plan.SetRegisterKind(m_reg_kind);
6148eba46c6SJason Molenda   unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num);
615fbcb7f2cSJason Molenda 
616f70d6040SRichard Mitton   std::vector<UnwindPlan::RowSP> stack;
617f70d6040SRichard Mitton 
618fbcb7f2cSJason Molenda   UnwindPlan::Row::RegisterLocation reg_location;
619b9c1b51eSKate Stone   while (m_cfi_data.ValidOffset(offset) && offset < end_offset) {
62030fdc8d8SChris Lattner     uint8_t inst = m_cfi_data.GetU8(&offset);
62130fdc8d8SChris Lattner     uint8_t primary_opcode = inst & 0xC0;
62230fdc8d8SChris Lattner     uint8_t extended_opcode = inst & 0x3F;
62330fdc8d8SChris Lattner 
624b9c1b51eSKate Stone     if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align,
625b9c1b51eSKate Stone                                  offset, *row)) {
626b9c1b51eSKate Stone       if (primary_opcode) {
627b9c1b51eSKate Stone         switch (primary_opcode) {
62830fdc8d8SChris Lattner         case DW_CFA_advance_loc: // (Row Creation Instruction)
62930fdc8d8SChris Lattner         { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
63030fdc8d8SChris Lattner           // takes a single argument that represents a constant delta. The
63130fdc8d8SChris Lattner           // required action is to create a new table row with a location
63230fdc8d8SChris Lattner           // value that is computed by taking the current entry's location
63330fdc8d8SChris Lattner           // value and adding (delta * code_align). All other
63430fdc8d8SChris Lattner           // values in the new row are initially identical to the current row.
635fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
636fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
637fa67e879SJason Molenda           *newrow = *row.get();
638fa67e879SJason Molenda           row.reset(newrow);
6391d42c7bcSJason Molenda           row->SlideOffset(extended_opcode * code_align);
64030fdc8d8SChris Lattner           break;
64130fdc8d8SChris Lattner         }
64230fdc8d8SChris Lattner 
643b9c1b51eSKate Stone         case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are
644b9c1b51eSKate Stone                                // register
64530fdc8d8SChris Lattner           // takes a single argument that represents a register number. The
64630fdc8d8SChris Lattner           // required action is to change the rule for the indicated register
64730fdc8d8SChris Lattner           // to the rule assigned it by the initial_instructions in the CIE.
648623652a0STamas Berghammer           uint32_t reg_num = extended_opcode;
64930fdc8d8SChris Lattner           // We only keep enough register locations around to
65030fdc8d8SChris Lattner           // unwind what is in our thread, and these are organized
65130fdc8d8SChris Lattner           // by the register index in that state, so we need to convert our
652b9c1b51eSKate Stone           // eh_frame register number from the EH frame info, to a register
653b9c1b51eSKate Stone           // index
65430fdc8d8SChris Lattner 
655b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
656b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
657b9c1b51eSKate Stone                                                             reg_location))
6581d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
65930fdc8d8SChris Lattner           break;
66030fdc8d8SChris Lattner         }
66130fdc8d8SChris Lattner         }
662b9c1b51eSKate Stone       } else {
663b9c1b51eSKate Stone         switch (extended_opcode) {
66430fdc8d8SChris Lattner         case DW_CFA_set_loc: // 0x1 (Row Creation Instruction)
66530fdc8d8SChris Lattner         {
66630fdc8d8SChris Lattner           // DW_CFA_set_loc takes a single argument that represents an address.
66730fdc8d8SChris Lattner           // The required action is to create a new table row using the
66830fdc8d8SChris Lattner           // specified address as the location. All other values in the new row
66930fdc8d8SChris Lattner           // are initially identical to the current row. The new location value
67030fdc8d8SChris Lattner           // should always be greater than the current one.
671fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
672fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
673fa67e879SJason Molenda           *newrow = *row.get();
674fa67e879SJason Molenda           row.reset(newrow);
675b9c1b51eSKate Stone           row->SetOffset(m_cfi_data.GetPointer(&offset) -
676b9c1b51eSKate Stone                          startaddr.GetFileAddress());
67730fdc8d8SChris Lattner           break;
678623652a0STamas Berghammer         }
67930fdc8d8SChris Lattner 
68030fdc8d8SChris Lattner         case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction)
68130fdc8d8SChris Lattner         {
68230fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
68330fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
68430fdc8d8SChris Lattner           // encoding and size of the delta argument.
685fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
686fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
687fa67e879SJason Molenda           *newrow = *row.get();
688fa67e879SJason Molenda           row.reset(newrow);
6891d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align);
69030fdc8d8SChris Lattner           break;
691623652a0STamas Berghammer         }
69230fdc8d8SChris Lattner 
69330fdc8d8SChris Lattner         case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction)
69430fdc8d8SChris Lattner         {
69530fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
69630fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
69730fdc8d8SChris Lattner           // encoding and size of the delta argument.
698fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
699fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
700fa67e879SJason Molenda           *newrow = *row.get();
701fa67e879SJason Molenda           row.reset(newrow);
7021d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align);
70330fdc8d8SChris Lattner           break;
704623652a0STamas Berghammer         }
70530fdc8d8SChris Lattner 
70630fdc8d8SChris Lattner         case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction)
70730fdc8d8SChris Lattner         {
70830fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
70930fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
71030fdc8d8SChris Lattner           // encoding and size of the delta argument.
711fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
712fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
713fa67e879SJason Molenda           *newrow = *row.get();
714fa67e879SJason Molenda           row.reset(newrow);
7151d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align);
71630fdc8d8SChris Lattner           break;
71730fdc8d8SChris Lattner         }
71830fdc8d8SChris Lattner 
71930fdc8d8SChris Lattner         case DW_CFA_restore_extended: // 0x6
72030fdc8d8SChris Lattner         {
72130fdc8d8SChris Lattner           // takes a single unsigned LEB128 argument that represents a register
72230fdc8d8SChris Lattner           // number. This instruction is identical to DW_CFA_restore except for
72330fdc8d8SChris Lattner           // the encoding and size of the register argument.
724623652a0STamas Berghammer           uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
725b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
726b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
727b9c1b51eSKate Stone                                                             reg_location))
7281d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
72930fdc8d8SChris Lattner           break;
73030fdc8d8SChris Lattner         }
73130fdc8d8SChris Lattner 
73230fdc8d8SChris Lattner         case DW_CFA_remember_state: // 0xA
733fa67e879SJason Molenda         {
73430fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
73530fdc8d8SChris Lattner           // DW_CFA_remember_state instruction means to save the rules for every
73630fdc8d8SChris Lattner           // register on the current row on the stack. Encountering the
73730fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
73830fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
73930fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
74030fdc8d8SChris Lattner           // function.)
741f70d6040SRichard Mitton           stack.push_back(row);
742fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
743fa67e879SJason Molenda           *newrow = *row.get();
744fa67e879SJason Molenda           row.reset(newrow);
74530fdc8d8SChris Lattner           break;
746623652a0STamas Berghammer         }
74730fdc8d8SChris Lattner 
74830fdc8d8SChris Lattner         case DW_CFA_restore_state: // 0xB
749623652a0STamas Berghammer         {
75030fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
75130fdc8d8SChris Lattner           // DW_CFA_remember_state instruction means to save the rules for every
75230fdc8d8SChris Lattner           // register on the current row on the stack. Encountering the
75330fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
75430fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
75530fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
75630fdc8d8SChris Lattner           // function.)
757b9c1b51eSKate Stone           if (stack.empty()) {
7581ee89eb8SPavel Labath             if (log)
759b9c1b51eSKate Stone               log->Printf("DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32
760b9c1b51eSKate Stone                           ", startaddr: %" PRIx64
761b9c1b51eSKate Stone                           " encountered DW_CFA_restore_state but state stack "
762b9c1b51eSKate Stone                           "is empty. Corrupt unwind info?",
763b9c1b51eSKate Stone                           __FUNCTION__, dwarf_offset,
764b9c1b51eSKate Stone                           startaddr.GetFileAddress());
7651ee89eb8SPavel Labath             break;
7661ee89eb8SPavel Labath           }
76749dd6d15STamas Berghammer           lldb::addr_t offset = row->GetOffset();
768f70d6040SRichard Mitton           row = stack.back();
769f70d6040SRichard Mitton           stack.pop_back();
77049dd6d15STamas Berghammer           row->SetOffset(offset);
77130fdc8d8SChris Lattner           break;
772623652a0STamas Berghammer         }
773623652a0STamas Berghammer 
7741ee89eb8SPavel Labath         case DW_CFA_GNU_args_size: // 0x2e
7751ee89eb8SPavel Labath         {
776b9c1b51eSKate Stone           // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128
777b9c1b51eSKate Stone           // operand
778b9c1b51eSKate Stone           // representing an argument size. This instruction specifies the total
779b9c1b51eSKate Stone           // of
7801ee89eb8SPavel Labath           // the size of the arguments which have been pushed onto the stack.
7811ee89eb8SPavel Labath 
7821ee89eb8SPavel Labath           // TODO: Figure out how we should handle this.
7831ee89eb8SPavel Labath           m_cfi_data.GetULEB128(&offset);
784a41deb1cSJason Molenda           break;
7851ee89eb8SPavel Labath         }
7861ee89eb8SPavel Labath 
787623652a0STamas Berghammer         case DW_CFA_val_offset:    // 0x14
788623652a0STamas Berghammer         case DW_CFA_val_offset_sf: // 0x15
789623652a0STamas Berghammer         default:
790623652a0STamas Berghammer           break;
791623652a0STamas Berghammer         }
792623652a0STamas Berghammer       }
793623652a0STamas Berghammer     }
794623652a0STamas Berghammer   }
795623652a0STamas Berghammer   unwind_plan.AppendRow(row);
796623652a0STamas Berghammer 
797623652a0STamas Berghammer   return true;
798623652a0STamas Berghammer }
799623652a0STamas Berghammer 
800b9c1b51eSKate Stone bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
801623652a0STamas Berghammer                                                  uint8_t extended_opcode,
802623652a0STamas Berghammer                                                  int32_t data_align,
803623652a0STamas Berghammer                                                  lldb::offset_t &offset,
804b9c1b51eSKate Stone                                                  UnwindPlan::Row &row) {
805623652a0STamas Berghammer   UnwindPlan::Row::RegisterLocation reg_location;
806623652a0STamas Berghammer 
807b9c1b51eSKate Stone   if (primary_opcode) {
808b9c1b51eSKate Stone     switch (primary_opcode) {
809b9c1b51eSKate Stone     case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are
810b9c1b51eSKate Stone                           // register
811623652a0STamas Berghammer       // takes two arguments: an unsigned LEB128 constant representing a
812623652a0STamas Berghammer       // factored offset and a register number. The required action is to
813623652a0STamas Berghammer       // change the rule for the register indicated by the register number
814623652a0STamas Berghammer       // to be an offset(N) rule with a value of
815623652a0STamas Berghammer       // (N = factored offset * data_align).
816623652a0STamas Berghammer       uint8_t reg_num = extended_opcode;
817623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
818623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
819623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
820623652a0STamas Berghammer       return true;
821623652a0STamas Berghammer     }
822623652a0STamas Berghammer     }
823b9c1b51eSKate Stone   } else {
824b9c1b51eSKate Stone     switch (extended_opcode) {
825623652a0STamas Berghammer     case DW_CFA_nop: // 0x0
826623652a0STamas Berghammer       return true;
827623652a0STamas Berghammer 
828623652a0STamas Berghammer     case DW_CFA_offset_extended: // 0x5
829623652a0STamas Berghammer     {
830623652a0STamas Berghammer       // takes two unsigned LEB128 arguments representing a register number
831623652a0STamas Berghammer       // and a factored offset. This instruction is identical to DW_CFA_offset
832623652a0STamas Berghammer       // except for the encoding and size of the register argument.
833623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
834623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
835623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
836623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
837623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
838623652a0STamas Berghammer       return true;
839623652a0STamas Berghammer     }
840623652a0STamas Berghammer 
841623652a0STamas Berghammer     case DW_CFA_undefined: // 0x7
842623652a0STamas Berghammer     {
843623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
844623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
845623652a0STamas Berghammer       // register to undefined.
846623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
847623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
848623652a0STamas Berghammer       reg_location.SetUndefined();
849623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
850623652a0STamas Berghammer       return true;
851623652a0STamas Berghammer     }
852623652a0STamas Berghammer 
853623652a0STamas Berghammer     case DW_CFA_same_value: // 0x8
854623652a0STamas Berghammer     {
855623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
856623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
857623652a0STamas Berghammer       // register to same value.
858623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
859623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
860623652a0STamas Berghammer       reg_location.SetSame();
861623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
862623652a0STamas Berghammer       return true;
863623652a0STamas Berghammer     }
864623652a0STamas Berghammer 
865623652a0STamas Berghammer     case DW_CFA_register: // 0x9
866623652a0STamas Berghammer     {
867623652a0STamas Berghammer       // takes two unsigned LEB128 arguments representing register numbers.
868623652a0STamas Berghammer       // The required action is to set the rule for the first register to be
869623652a0STamas Berghammer       // the second register.
870623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
871623652a0STamas Berghammer       uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
872623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
873623652a0STamas Berghammer       reg_location.SetInRegister(other_reg_num);
874623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
875623652a0STamas Berghammer       return true;
876623652a0STamas Berghammer     }
87730fdc8d8SChris Lattner 
87830fdc8d8SChris Lattner     case DW_CFA_def_cfa: // 0xC    (CFA Definition Instruction)
87930fdc8d8SChris Lattner     {
88030fdc8d8SChris Lattner       // Takes two unsigned LEB128 operands representing a register
88130fdc8d8SChris Lattner       // number and a (non-factored) offset. The required action
88230fdc8d8SChris Lattner       // is to define the current CFA rule to use the provided
88330fdc8d8SChris Lattner       // register and offset.
884623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
885623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
886623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
887623652a0STamas Berghammer       return true;
88830fdc8d8SChris Lattner     }
88930fdc8d8SChris Lattner 
89030fdc8d8SChris Lattner     case DW_CFA_def_cfa_register: // 0xD    (CFA Definition Instruction)
89130fdc8d8SChris Lattner     {
89230fdc8d8SChris Lattner       // takes a single unsigned LEB128 argument representing a register
89330fdc8d8SChris Lattner       // number. The required action is to define the current CFA rule to
89430fdc8d8SChris Lattner       // use the provided register (but to keep the old offset).
895623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
896b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num,
897b9c1b51eSKate Stone                                                 row.GetCFAValue().GetOffset());
898623652a0STamas Berghammer       return true;
89930fdc8d8SChris Lattner     }
90030fdc8d8SChris Lattner 
90130fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset: // 0xE    (CFA Definition Instruction)
90230fdc8d8SChris Lattner     {
90330fdc8d8SChris Lattner       // Takes a single unsigned LEB128 operand representing a
90430fdc8d8SChris Lattner       // (non-factored) offset. The required action is to define
90530fdc8d8SChris Lattner       // the current CFA rule to use the provided offset (but
90630fdc8d8SChris Lattner       // to keep the old register).
907623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
908b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(
909b9c1b51eSKate Stone           row.GetCFAValue().GetRegisterNumber(), op_offset);
910623652a0STamas Berghammer       return true;
91130fdc8d8SChris Lattner     }
91230fdc8d8SChris Lattner 
91330fdc8d8SChris Lattner     case DW_CFA_def_cfa_expression: // 0xF    (CFA Definition Instruction)
91430fdc8d8SChris Lattner     {
91530fdc8d8SChris Lattner       size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
916b9c1b51eSKate Stone       const uint8_t *block_data =
917b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
918623652a0STamas Berghammer       row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
919623652a0STamas Berghammer       return true;
92030fdc8d8SChris Lattner     }
92130fdc8d8SChris Lattner 
92230fdc8d8SChris Lattner     case DW_CFA_expression: // 0x10
92330fdc8d8SChris Lattner     {
92430fdc8d8SChris Lattner       // Takes two operands: an unsigned LEB128 value representing
92530fdc8d8SChris Lattner       // a register number, and a DW_FORM_block value representing a DWARF
92630fdc8d8SChris Lattner       // expression. The required action is to change the rule for the
92730fdc8d8SChris Lattner       // register indicated by the register number to be an expression(E)
92830fdc8d8SChris Lattner       // rule where E is the DWARF expression. That is, the DWARF
92930fdc8d8SChris Lattner       // expression computes the address. The value of the CFA is
93030fdc8d8SChris Lattner       // pushed on the DWARF evaluation stack prior to execution of
93130fdc8d8SChris Lattner       // the DWARF expression.
932623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
93330fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
934b9c1b51eSKate Stone       const uint8_t *block_data =
935b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
936623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
93730fdc8d8SChris Lattner       reg_location.SetAtDWARFExpression(block_data, block_len);
938623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
939623652a0STamas Berghammer       return true;
94030fdc8d8SChris Lattner     }
94130fdc8d8SChris Lattner 
94230fdc8d8SChris Lattner     case DW_CFA_offset_extended_sf: // 0x11
94330fdc8d8SChris Lattner     {
94430fdc8d8SChris Lattner       // takes two operands: an unsigned LEB128 value representing a
94530fdc8d8SChris Lattner       // register number and a signed LEB128 factored offset. This
94630fdc8d8SChris Lattner       // instruction is identical to DW_CFA_offset_extended except
94730fdc8d8SChris Lattner       // that the second operand is signed and factored.
948623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
949623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
950623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
95130fdc8d8SChris Lattner       reg_location.SetAtCFAPlusOffset(op_offset);
952623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
953623652a0STamas Berghammer       return true;
95430fdc8d8SChris Lattner     }
95530fdc8d8SChris Lattner 
95630fdc8d8SChris Lattner     case DW_CFA_def_cfa_sf: // 0x12   (CFA Definition Instruction)
95730fdc8d8SChris Lattner     {
95830fdc8d8SChris Lattner       // Takes two operands: an unsigned LEB128 value representing
95930fdc8d8SChris Lattner       // a register number and a signed LEB128 factored offset.
96030fdc8d8SChris Lattner       // This instruction is identical to DW_CFA_def_cfa except
96130fdc8d8SChris Lattner       // that the second operand is signed and factored.
962623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
963623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
964623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
965623652a0STamas Berghammer       return true;
96630fdc8d8SChris Lattner     }
96730fdc8d8SChris Lattner 
96830fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset_sf: // 0x13   (CFA Definition Instruction)
96930fdc8d8SChris Lattner     {
97030fdc8d8SChris Lattner       // takes a signed LEB128 operand representing a factored
97130fdc8d8SChris Lattner       // offset. This instruction is identical to  DW_CFA_def_cfa_offset
97230fdc8d8SChris Lattner       // except that the operand is signed and factored.
973623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
974623652a0STamas Berghammer       uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
975623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
976623652a0STamas Berghammer       return true;
97730fdc8d8SChris Lattner     }
97830fdc8d8SChris Lattner 
97930fdc8d8SChris Lattner     case DW_CFA_val_expression: // 0x16
98030fdc8d8SChris Lattner     {
98130fdc8d8SChris Lattner       // takes two operands: an unsigned LEB128 value representing a register
98230fdc8d8SChris Lattner       // number, and a DW_FORM_block value representing a DWARF expression.
98330fdc8d8SChris Lattner       // The required action is to change the rule for the register indicated
98430fdc8d8SChris Lattner       // by the register number to be a val_expression(E) rule where E is the
98530fdc8d8SChris Lattner       // DWARF expression. That is, the DWARF expression computes the value of
98630fdc8d8SChris Lattner       // the given register. The value of the CFA is pushed on the DWARF
98730fdc8d8SChris Lattner       // evaluation stack prior to execution of the DWARF expression.
988623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
98930fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
990b9c1b51eSKate Stone       const uint8_t *block_data =
991b9c1b51eSKate Stone           (const uint8_t *)m_cfi_data.GetData(&offset, block_len);
99230fdc8d8SChris Lattner       //#if defined(__i386__) || defined(__x86_64__)
993b9c1b51eSKate Stone       //              // The EH frame info for EIP and RIP contains code that
994b9c1b51eSKate Stone       //              looks for traps to
99530fdc8d8SChris Lattner       //              // be a specific type and increments the PC.
99630fdc8d8SChris Lattner       //              // For i386:
99730fdc8d8SChris Lattner       //              // DW_CFA_val_expression where:
998b9c1b51eSKate Stone       //              // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup,
999b9c1b51eSKate Stone       //              DW_OP_plus_uconst(0x34),
1000b9c1b51eSKate Stone       //              //       DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0),
1001b9c1b51eSKate Stone       //              DW_OP_deref,
1002b9c1b51eSKate Stone       //              //       DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap,
1003b9c1b51eSKate Stone       //              DW_OP_lit4, DW_OP_ne,
100430fdc8d8SChris Lattner       //              //       DW_OP_and, DW_OP_plus
100530fdc8d8SChris Lattner       //              // This basically does a:
100630fdc8d8SChris Lattner       //              // eip = ucontenxt.mcontext32->gpr.eip;
1007b9c1b51eSKate Stone       //              // if (ucontenxt.mcontext32->exc.trapno != 3 &&
1008b9c1b51eSKate Stone       //              ucontenxt.mcontext32->exc.trapno != 4)
100930fdc8d8SChris Lattner       //              //   eip++;
101030fdc8d8SChris Lattner       //              //
101130fdc8d8SChris Lattner       //              // For x86_64:
101230fdc8d8SChris Lattner       //              // DW_CFA_val_expression where:
1013b9c1b51eSKate Stone       //              // rip =  DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup,
1014b9c1b51eSKate Stone       //              DW_OP_plus_uconst(0x90), DW_OP_deref,
1015b9c1b51eSKate Stone       //              //          DW_OP_swap, DW_OP_plus_uconst(0),
1016b9c1b51eSKate Stone       //              DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
1017b9c1b51eSKate Stone       //              //          DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
1018b9c1b51eSKate Stone       //              DW_OP_and, DW_OP_plus
101930fdc8d8SChris Lattner       //              // This basically does a:
102030fdc8d8SChris Lattner       //              // rip = ucontenxt.mcontext64->gpr.rip;
1021b9c1b51eSKate Stone       //              // if (ucontenxt.mcontext64->exc.trapno != 3 &&
1022b9c1b51eSKate Stone       //              ucontenxt.mcontext64->exc.trapno != 4)
102330fdc8d8SChris Lattner       //              //   rip++;
1024b9c1b51eSKate Stone       //              // The trap comparisons and increments are not needed as
1025b9c1b51eSKate Stone       //              it hoses up the unwound PC which
1026b9c1b51eSKate Stone       //              // is expected to point at least past the instruction that
1027b9c1b51eSKate Stone       //              causes the fault/trap. So we
1028b9c1b51eSKate Stone       //              // take it out by trimming the expression right at the
1029b9c1b51eSKate Stone       //              first "DW_OP_swap" opcodes
1030b9c1b51eSKate Stone       //              if (block_data != NULL && thread->GetPCRegNum(Thread::GCC)
1031b9c1b51eSKate Stone       //              == reg_num)
103230fdc8d8SChris Lattner       //              {
103330fdc8d8SChris Lattner       //                  if (thread->Is64Bit())
103430fdc8d8SChris Lattner       //                  {
1035b9c1b51eSKate Stone       //                      if (block_len > 9 && block_data[8] == DW_OP_swap
1036b9c1b51eSKate Stone       //                      && block_data[9] == DW_OP_plus_uconst)
103730fdc8d8SChris Lattner       //                          block_len = 8;
103830fdc8d8SChris Lattner       //                  }
103930fdc8d8SChris Lattner       //                  else
104030fdc8d8SChris Lattner       //                  {
1041b9c1b51eSKate Stone       //                      if (block_len > 8 && block_data[7] == DW_OP_swap
1042b9c1b51eSKate Stone       //                      && block_data[8] == DW_OP_plus_uconst)
104330fdc8d8SChris Lattner       //                          block_len = 7;
104430fdc8d8SChris Lattner       //                  }
104530fdc8d8SChris Lattner       //              }
104630fdc8d8SChris Lattner       //#endif
104730fdc8d8SChris Lattner       reg_location.SetIsDWARFExpression(block_data, block_len);
1048623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
1049fbcb7f2cSJason Molenda       return true;
105030fdc8d8SChris Lattner     }
1051623652a0STamas Berghammer     }
1052623652a0STamas Berghammer   }
1053623652a0STamas Berghammer   return false;
1054623652a0STamas Berghammer }
10556b63b148STamas Berghammer 
1056b9c1b51eSKate Stone void DWARFCallFrameInfo::ForEachFDEEntries(
1057b9c1b51eSKate Stone     const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) {
10586b63b148STamas Berghammer   GetFDEIndex();
10596b63b148STamas Berghammer 
1060b9c1b51eSKate Stone   for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) {
10616b63b148STamas Berghammer     const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i);
10626b63b148STamas Berghammer     if (!callback(entry.base, entry.size, entry.data))
10636b63b148STamas Berghammer       break;
10646b63b148STamas Berghammer   }
10656b63b148STamas Berghammer }
1066