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"
1829cb868aSZachary 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 
3029cb868aSZachary Turner //----------------------------------------------------------------------
3129cb868aSZachary Turner // GetDwarfEHPtr
3229cb868aSZachary Turner //
3329cb868aSZachary Turner // Used for calls when the value type is specified by a DWARF EH Frame
3429cb868aSZachary Turner // pointer encoding.
3529cb868aSZachary Turner //----------------------------------------------------------------------
3629cb868aSZachary Turner static uint64_t
3729cb868aSZachary Turner GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr,
3829cb868aSZachary Turner                 uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr,
3929cb868aSZachary Turner                 addr_t data_addr) //, BSDRelocs *data_relocs) const
4029cb868aSZachary Turner {
4129cb868aSZachary Turner   if (eh_ptr_enc == DW_EH_PE_omit)
4229cb868aSZachary Turner     return ULLONG_MAX; // Value isn't in the buffer...
4329cb868aSZachary Turner 
4429cb868aSZachary Turner   uint64_t baseAddress = 0;
4529cb868aSZachary Turner   uint64_t addressValue = 0;
4629cb868aSZachary Turner   const uint32_t addr_size = DE.GetAddressByteSize();
4729cb868aSZachary Turner #ifdef LLDB_CONFIGURATION_DEBUG
4829cb868aSZachary Turner   assert(addr_size == 4 || addr_size == 8);
4929cb868aSZachary Turner #endif
5029cb868aSZachary Turner 
5129cb868aSZachary Turner   bool signExtendValue = false;
5229cb868aSZachary Turner   // Decode the base part or adjust our offset
5329cb868aSZachary Turner   switch (eh_ptr_enc & 0x70) {
5429cb868aSZachary Turner   case DW_EH_PE_pcrel:
5529cb868aSZachary Turner     signExtendValue = true;
5629cb868aSZachary Turner     baseAddress = *offset_ptr;
5729cb868aSZachary Turner     if (pc_rel_addr != LLDB_INVALID_ADDRESS)
5829cb868aSZachary Turner       baseAddress += pc_rel_addr;
5929cb868aSZachary Turner     //      else
6029cb868aSZachary Turner     //          Log::GlobalWarning ("PC relative pointer encoding found with
6129cb868aSZachary Turner     //          invalid pc relative address.");
6229cb868aSZachary Turner     break;
6329cb868aSZachary Turner 
6429cb868aSZachary Turner   case DW_EH_PE_textrel:
6529cb868aSZachary Turner     signExtendValue = true;
6629cb868aSZachary Turner     if (text_addr != LLDB_INVALID_ADDRESS)
6729cb868aSZachary Turner       baseAddress = text_addr;
6829cb868aSZachary Turner     //      else
6929cb868aSZachary Turner     //          Log::GlobalWarning ("text relative pointer encoding being
7029cb868aSZachary Turner     //          decoded with invalid text section address, setting base address
7129cb868aSZachary Turner     //          to zero.");
7229cb868aSZachary Turner     break;
7329cb868aSZachary Turner 
7429cb868aSZachary Turner   case DW_EH_PE_datarel:
7529cb868aSZachary Turner     signExtendValue = true;
7629cb868aSZachary Turner     if (data_addr != LLDB_INVALID_ADDRESS)
7729cb868aSZachary Turner       baseAddress = data_addr;
7829cb868aSZachary Turner     //      else
7929cb868aSZachary Turner     //          Log::GlobalWarning ("data relative pointer encoding being
8029cb868aSZachary Turner     //          decoded with invalid data section address, setting base address
8129cb868aSZachary Turner     //          to zero.");
8229cb868aSZachary Turner     break;
8329cb868aSZachary Turner 
8429cb868aSZachary Turner   case DW_EH_PE_funcrel:
8529cb868aSZachary Turner     signExtendValue = true;
8629cb868aSZachary Turner     break;
8729cb868aSZachary Turner 
8829cb868aSZachary Turner   case DW_EH_PE_aligned: {
8929cb868aSZachary Turner     // SetPointerSize should be called prior to extracting these so the
9029cb868aSZachary Turner     // pointer size is cached
9129cb868aSZachary Turner     assert(addr_size != 0);
9229cb868aSZachary Turner     if (addr_size) {
9329cb868aSZachary Turner       // Align to a address size boundary first
9429cb868aSZachary Turner       uint32_t alignOffset = *offset_ptr % addr_size;
9529cb868aSZachary Turner       if (alignOffset)
9629cb868aSZachary Turner         offset_ptr += addr_size - alignOffset;
9729cb868aSZachary Turner     }
9829cb868aSZachary Turner   } break;
9929cb868aSZachary Turner 
10029cb868aSZachary Turner   default:
10129cb868aSZachary Turner     break;
10229cb868aSZachary Turner   }
10329cb868aSZachary Turner 
10429cb868aSZachary Turner   // Decode the value part
10529cb868aSZachary Turner   switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) {
10629cb868aSZachary Turner   case DW_EH_PE_absptr: {
10729cb868aSZachary Turner     addressValue = DE.GetAddress(offset_ptr);
10829cb868aSZachary Turner     //          if (data_relocs)
10929cb868aSZachary Turner     //              addressValue = data_relocs->Relocate(*offset_ptr -
11029cb868aSZachary Turner     //              addr_size, *this, addressValue);
11129cb868aSZachary Turner   } break;
11229cb868aSZachary Turner   case DW_EH_PE_uleb128:
11329cb868aSZachary Turner     addressValue = DE.GetULEB128(offset_ptr);
11429cb868aSZachary Turner     break;
11529cb868aSZachary Turner   case DW_EH_PE_udata2:
11629cb868aSZachary Turner     addressValue = DE.GetU16(offset_ptr);
11729cb868aSZachary Turner     break;
11829cb868aSZachary Turner   case DW_EH_PE_udata4:
11929cb868aSZachary Turner     addressValue = DE.GetU32(offset_ptr);
12029cb868aSZachary Turner     break;
12129cb868aSZachary Turner   case DW_EH_PE_udata8:
12229cb868aSZachary Turner     addressValue = DE.GetU64(offset_ptr);
12329cb868aSZachary Turner     break;
12429cb868aSZachary Turner   case DW_EH_PE_sleb128:
12529cb868aSZachary Turner     addressValue = DE.GetSLEB128(offset_ptr);
12629cb868aSZachary Turner     break;
12729cb868aSZachary Turner   case DW_EH_PE_sdata2:
12829cb868aSZachary Turner     addressValue = (int16_t)DE.GetU16(offset_ptr);
12929cb868aSZachary Turner     break;
13029cb868aSZachary Turner   case DW_EH_PE_sdata4:
13129cb868aSZachary Turner     addressValue = (int32_t)DE.GetU32(offset_ptr);
13229cb868aSZachary Turner     break;
13329cb868aSZachary Turner   case DW_EH_PE_sdata8:
13429cb868aSZachary Turner     addressValue = (int64_t)DE.GetU64(offset_ptr);
13529cb868aSZachary Turner     break;
13629cb868aSZachary Turner   default:
13729cb868aSZachary Turner     // Unhandled encoding type
13829cb868aSZachary Turner     assert(eh_ptr_enc);
13929cb868aSZachary Turner     break;
14029cb868aSZachary Turner   }
14129cb868aSZachary Turner 
14229cb868aSZachary Turner   // Since we promote everything to 64 bit, we may need to sign extend
14329cb868aSZachary Turner   if (signExtendValue && addr_size < sizeof(baseAddress)) {
14429cb868aSZachary Turner     uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
14529cb868aSZachary Turner     if (sign_bit & addressValue) {
14629cb868aSZachary Turner       uint64_t mask = ~sign_bit + 1;
14729cb868aSZachary Turner       addressValue |= mask;
14829cb868aSZachary Turner     }
14929cb868aSZachary Turner   }
15029cb868aSZachary Turner   return baseAddress + addressValue;
15129cb868aSZachary Turner }
15229cb868aSZachary 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 bool DWARFCallFrameInfo::GetUnwindPlan(Address addr, UnwindPlan &unwind_plan) {
16574303829SJason Molenda   FDEEntryMap::Entry fde_entry;
16674303829SJason Molenda 
16774303829SJason Molenda   // Make sure that the Address we're searching for is the same object file
16874303829SJason Molenda   // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
16974303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
170b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
171b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
172fbcb7f2cSJason Molenda     return false;
17374303829SJason Molenda 
17474303829SJason Molenda   if (GetFDEEntryByFileAddress(addr.GetFileAddress(), fde_entry) == false)
17574303829SJason Molenda     return false;
17674303829SJason Molenda   return FDEToUnwindPlan(fde_entry.data, addr, unwind_plan);
17730fdc8d8SChris Lattner }
17830fdc8d8SChris Lattner 
179b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) {
18074303829SJason Molenda 
18174303829SJason Molenda   // Make sure that the Address we're searching for is the same object file
18274303829SJason Molenda   // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
18374303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
184b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
185b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
18674303829SJason Molenda     return false;
18774303829SJason Molenda 
188d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
189fbcb7f2cSJason Molenda     return false;
190fbcb7f2cSJason Molenda   GetFDEIndex();
191b9c1b51eSKate Stone   FDEEntryMap::Entry *fde_entry =
192b9c1b51eSKate Stone       m_fde_index.FindEntryThatContains(addr.GetFileAddress());
19374303829SJason Molenda   if (!fde_entry)
194fbcb7f2cSJason Molenda     return false;
195fbcb7f2cSJason Molenda 
196b9c1b51eSKate Stone   range = AddressRange(fde_entry->base, fde_entry->size,
197b9c1b51eSKate Stone                        m_objfile.GetSectionList());
198fbcb7f2cSJason Molenda   return true;
19930fdc8d8SChris Lattner }
20030fdc8d8SChris Lattner 
201b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetFDEEntryByFileAddress(
202b9c1b51eSKate Stone     addr_t file_addr, FDEEntryMap::Entry &fde_entry) {
203d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
204fbcb7f2cSJason Molenda     return false;
20574303829SJason Molenda 
20674303829SJason Molenda   GetFDEIndex();
20774303829SJason Molenda 
20874303829SJason Molenda   if (m_fde_index.IsEmpty())
20974303829SJason Molenda     return false;
21074303829SJason Molenda 
21174303829SJason Molenda   FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains(file_addr);
21274303829SJason Molenda 
213d4612ad0SEd Maste   if (fde == nullptr)
21474303829SJason Molenda     return false;
21574303829SJason Molenda 
21674303829SJason Molenda   fde_entry = *fde;
21774303829SJason Molenda   return true;
21830fdc8d8SChris Lattner }
21930fdc8d8SChris Lattner 
220b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFunctionAddressAndSizeVector(
221b9c1b51eSKate Stone     FunctionAddressAndSizeVector &function_info) {
2225635f77aSJason Molenda   GetFDEIndex();
2235635f77aSJason Molenda   const size_t count = m_fde_index.GetSize();
224ee7593fbSJason Molenda   function_info.Clear();
225c1946cd8SJason Molenda   if (count > 0)
226ee7593fbSJason Molenda     function_info.Reserve(count);
227b9c1b51eSKate Stone   for (size_t i = 0; i < count; ++i) {
228b9c1b51eSKate Stone     const FDEEntryMap::Entry *func_offset_data_entry =
229b9c1b51eSKate Stone         m_fde_index.GetEntryAtIndex(i);
230b9c1b51eSKate Stone     if (func_offset_data_entry) {
231b9c1b51eSKate Stone       FunctionAddressAndSizeVector::Entry function_offset_entry(
232b9c1b51eSKate Stone           func_offset_data_entry->base, func_offset_data_entry->size);
2335635f77aSJason Molenda       function_info.Append(function_offset_entry);
2345635f77aSJason Molenda     }
2355635f77aSJason Molenda   }
2365635f77aSJason Molenda }
2375635f77aSJason Molenda 
23830fdc8d8SChris Lattner const DWARFCallFrameInfo::CIE *
239b9c1b51eSKate Stone DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) {
24030fdc8d8SChris Lattner   cie_map_t::iterator pos = m_cie_map.find(cie_offset);
24130fdc8d8SChris Lattner 
242b9c1b51eSKate Stone   if (pos != m_cie_map.end()) {
24330fdc8d8SChris Lattner     // Parse and cache the CIE
244d4612ad0SEd Maste     if (pos->second.get() == nullptr)
24530fdc8d8SChris Lattner       pos->second = ParseCIE(cie_offset);
24630fdc8d8SChris Lattner 
24730fdc8d8SChris Lattner     return pos->second.get();
24830fdc8d8SChris Lattner   }
249d4612ad0SEd Maste   return nullptr;
25030fdc8d8SChris Lattner }
25130fdc8d8SChris Lattner 
252fbcb7f2cSJason Molenda DWARFCallFrameInfo::CIESP
253b9c1b51eSKate Stone DWARFCallFrameInfo::ParseCIE(const dw_offset_t cie_offset) {
254fbcb7f2cSJason Molenda   CIESP cie_sp(new CIE(cie_offset));
255c7bece56SGreg Clayton   lldb::offset_t offset = cie_offset;
256fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
257f97c5213SGreg Clayton     GetCFIData();
2581767e115STodd Fiala   uint32_t length = m_cfi_data.GetU32(&offset);
2591767e115STodd Fiala   dw_offset_t cie_id, end_offset;
2601767e115STodd Fiala   bool is_64bit = (length == UINT32_MAX);
2611767e115STodd Fiala   if (is_64bit) {
2621767e115STodd Fiala     length = m_cfi_data.GetU64(&offset);
2631767e115STodd Fiala     cie_id = m_cfi_data.GetU64(&offset);
2641767e115STodd Fiala     end_offset = cie_offset + length + 12;
2651767e115STodd Fiala   } else {
2661767e115STodd Fiala     cie_id = m_cfi_data.GetU32(&offset);
2671767e115STodd Fiala     end_offset = cie_offset + length + 4;
2681767e115STodd Fiala   }
269b9c1b51eSKate Stone   if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) ||
270b9c1b51eSKate Stone                      (m_is_eh_frame && cie_id == 0ul))) {
27130fdc8d8SChris Lattner     size_t i;
27230fdc8d8SChris Lattner     //    cie.offset = cie_offset;
27330fdc8d8SChris Lattner     //    cie.length = length;
27430fdc8d8SChris Lattner     //    cie.cieID = cieID;
2756e264d39SAshok Thirumurthi     cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
27630fdc8d8SChris Lattner     cie_sp->version = m_cfi_data.GetU8(&offset);
277*cdda23ebSPavel Labath     if (cie_sp->version > CFI_VERSION4) {
278*cdda23ebSPavel Labath       Host::SystemLog(Host::eSystemLogError,
279*cdda23ebSPavel Labath                       "CIE parse error: CFI version %d is not supported\n",
280*cdda23ebSPavel Labath                       cie_sp->version);
281*cdda23ebSPavel Labath       return nullptr;
282*cdda23ebSPavel Labath     }
28330fdc8d8SChris Lattner 
284b9c1b51eSKate Stone     for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) {
28530fdc8d8SChris Lattner       cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
286b9c1b51eSKate Stone       if (cie_sp->augmentation[i] == '\0') {
28730fdc8d8SChris Lattner         // Zero out remaining bytes in augmentation string
28830fdc8d8SChris Lattner         for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j)
28930fdc8d8SChris Lattner           cie_sp->augmentation[j] = '\0';
29030fdc8d8SChris Lattner 
29130fdc8d8SChris Lattner         break;
29230fdc8d8SChris Lattner       }
29330fdc8d8SChris Lattner     }
29430fdc8d8SChris Lattner 
295b9c1b51eSKate Stone     if (i == CFI_AUG_MAX_SIZE &&
296b9c1b51eSKate Stone         cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') {
297b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError,
298b9c1b51eSKate Stone                       "CIE parse error: CIE augmentation string was too large "
299b9c1b51eSKate Stone                       "for the fixed sized buffer of %d bytes.\n",
300b9c1b51eSKate Stone                       CFI_AUG_MAX_SIZE);
301*cdda23ebSPavel Labath       return nullptr;
30230fdc8d8SChris Lattner     }
303*cdda23ebSPavel Labath 
304*cdda23ebSPavel Labath     // m_cfi_data uses address size from target architecture of the process
305*cdda23ebSPavel Labath     // may ignore these fields?
306*cdda23ebSPavel Labath     if (!m_is_eh_frame && cie_sp->version >= CFI_VERSION4) {
307*cdda23ebSPavel Labath       cie_sp->address_size = m_cfi_data.GetU8(&offset);
308*cdda23ebSPavel Labath       cie_sp->segment_size = m_cfi_data.GetU8(&offset);
309*cdda23ebSPavel Labath     }
310*cdda23ebSPavel Labath 
31130fdc8d8SChris Lattner     cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
31230fdc8d8SChris Lattner     cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
313*cdda23ebSPavel Labath 
314*cdda23ebSPavel Labath     cie_sp->return_addr_reg_num =
315*cdda23ebSPavel Labath         !m_is_eh_frame && cie_sp->version >= CFI_VERSION3
316*cdda23ebSPavel Labath             ? static_cast<uint32_t>(m_cfi_data.GetULEB128(&offset))
317*cdda23ebSPavel Labath             : m_cfi_data.GetU8(&offset);
31830fdc8d8SChris Lattner 
319b9c1b51eSKate Stone     if (cie_sp->augmentation[0]) {
32030fdc8d8SChris Lattner       // Get the length of the eh_frame augmentation data
32130fdc8d8SChris Lattner       // which starts with a ULEB128 length in bytes
32230fdc8d8SChris Lattner       const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
32330fdc8d8SChris Lattner       const size_t aug_data_end = offset + aug_data_len;
32430fdc8d8SChris Lattner       const size_t aug_str_len = strlen(cie_sp->augmentation);
32530fdc8d8SChris Lattner       // A 'z' may be present as the first character of the string.
32630fdc8d8SChris Lattner       // If present, the Augmentation Data field shall be present.
32758ef391fSBruce Mitchener       // The contents of the Augmentation Data shall be interpreted
32830fdc8d8SChris Lattner       // according to other characters in the Augmentation String.
329b9c1b51eSKate Stone       if (cie_sp->augmentation[0] == 'z') {
33030fdc8d8SChris Lattner         // Extract the Augmentation Data
33130fdc8d8SChris Lattner         size_t aug_str_idx = 0;
332b9c1b51eSKate Stone         for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) {
33330fdc8d8SChris Lattner           char aug = cie_sp->augmentation[aug_str_idx];
334b9c1b51eSKate Stone           switch (aug) {
33530fdc8d8SChris Lattner           case 'L':
33630fdc8d8SChris Lattner             // Indicates the presence of one argument in the
33730fdc8d8SChris Lattner             // Augmentation Data of the CIE, and a corresponding
33830fdc8d8SChris Lattner             // argument in the Augmentation Data of the FDE. The
33930fdc8d8SChris Lattner             // argument in the Augmentation Data of the CIE is
34030fdc8d8SChris Lattner             // 1-byte and represents the pointer encoding used
34130fdc8d8SChris Lattner             // for the argument in the Augmentation Data of the
34230fdc8d8SChris Lattner             // FDE, which is the address of a language-specific
34330fdc8d8SChris Lattner             // data area (LSDA). The size of the LSDA pointer is
34430fdc8d8SChris Lattner             // specified by the pointer encoding used.
345e9c7ecf6SJason Molenda             cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
34630fdc8d8SChris Lattner             break;
34730fdc8d8SChris Lattner 
34830fdc8d8SChris Lattner           case 'P':
34930fdc8d8SChris Lattner             // Indicates the presence of two arguments in the
350e9c7ecf6SJason Molenda             // Augmentation Data of the CIE. The first argument
35130fdc8d8SChris Lattner             // is 1-byte and represents the pointer encoding
35230fdc8d8SChris Lattner             // used for the second argument, which is the
35330fdc8d8SChris Lattner             // address of a personality routine handler. The
35430fdc8d8SChris Lattner             // size of the personality routine pointer is
35530fdc8d8SChris Lattner             // specified by the pointer encoding used.
356e9c7ecf6SJason Molenda             //
357e9c7ecf6SJason Molenda             // The address of the personality function will
358e9c7ecf6SJason Molenda             // be stored at this location.  Pre-execution, it
359e9c7ecf6SJason Molenda             // will be all zero's so don't read it until we're
360e9c7ecf6SJason Molenda             // trying to do an unwind & the reloc has been
361e9c7ecf6SJason Molenda             // resolved.
36230fdc8d8SChris Lattner             {
36330fdc8d8SChris Lattner               uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
364e9c7ecf6SJason Molenda               const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
36529cb868aSZachary Turner               cie_sp->personality_loc = GetGNUEHPointer(
36629cb868aSZachary Turner                   m_cfi_data, &offset, arg_ptr_encoding, pc_rel_addr,
36729cb868aSZachary Turner                   LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
36830fdc8d8SChris Lattner             }
36930fdc8d8SChris Lattner             break;
37030fdc8d8SChris Lattner 
37130fdc8d8SChris Lattner           case 'R':
37230fdc8d8SChris Lattner             // A 'R' may be present at any position after the
37330fdc8d8SChris Lattner             // first character of the string. The Augmentation
37430fdc8d8SChris Lattner             // Data shall include a 1 byte argument that
37530fdc8d8SChris Lattner             // represents the pointer encoding for the address
37630fdc8d8SChris Lattner             // pointers used in the FDE.
3776e264d39SAshok Thirumurthi             // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4
37830fdc8d8SChris Lattner             cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
37930fdc8d8SChris Lattner             break;
38030fdc8d8SChris Lattner           }
38130fdc8d8SChris Lattner         }
382b9c1b51eSKate Stone       } else if (strcmp(cie_sp->augmentation, "eh") == 0) {
38330fdc8d8SChris Lattner         // If the Augmentation string has the value "eh", then
38430fdc8d8SChris Lattner         // the EH Data field shall be present
38530fdc8d8SChris Lattner       }
38630fdc8d8SChris Lattner 
38730fdc8d8SChris Lattner       // Set the offset to be the end of the augmentation data just in case
38830fdc8d8SChris Lattner       // we didn't understand any of the data.
38930fdc8d8SChris Lattner       offset = (uint32_t)aug_data_end;
39030fdc8d8SChris Lattner     }
39130fdc8d8SChris Lattner 
392b9c1b51eSKate Stone     if (end_offset > offset) {
39330fdc8d8SChris Lattner       cie_sp->inst_offset = offset;
39430fdc8d8SChris Lattner       cie_sp->inst_length = end_offset - offset;
39530fdc8d8SChris Lattner     }
396b9c1b51eSKate Stone     while (offset < end_offset) {
397fbcb7f2cSJason Molenda       uint8_t inst = m_cfi_data.GetU8(&offset);
398fbcb7f2cSJason Molenda       uint8_t primary_opcode = inst & 0xC0;
399fbcb7f2cSJason Molenda       uint8_t extended_opcode = inst & 0x3F;
400fbcb7f2cSJason Molenda 
401b9c1b51eSKate Stone       if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode,
402b9c1b51eSKate Stone                                    cie_sp->data_align, offset,
403b9c1b51eSKate Stone                                    cie_sp->initial_row))
404fbcb7f2cSJason Molenda         break; // Stop if we hit an unrecognized opcode
405fbcb7f2cSJason Molenda     }
40630fdc8d8SChris Lattner   }
40730fdc8d8SChris Lattner 
40830fdc8d8SChris Lattner   return cie_sp;
40930fdc8d8SChris Lattner }
41030fdc8d8SChris Lattner 
411b9c1b51eSKate Stone void DWARFCallFrameInfo::GetCFIData() {
412b9c1b51eSKate Stone   if (m_cfi_data_initialized == false) {
4135160ce5cSGreg Clayton     Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
414f97c5213SGreg Clayton     if (log)
4155160ce5cSGreg Clayton       m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
416c9660546SGreg Clayton     m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data);
417f97c5213SGreg Clayton     m_cfi_data_initialized = true;
418f97c5213SGreg Clayton   }
419f97c5213SGreg Clayton }
420b9c1b51eSKate Stone // Scan through the eh_frame or debug_frame section looking for FDEs and noting
421b9c1b51eSKate Stone // the start/end addresses
422b9c1b51eSKate Stone // of the functions and a pointer back to the function's FDE for later
423b9c1b51eSKate Stone // expansion.
424fbcb7f2cSJason Molenda // Internalize CIEs as we come across them.
42530fdc8d8SChris Lattner 
426b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFDEIndex() {
427d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
428fbcb7f2cSJason Molenda     return;
4297d69f12eSSean Callanan 
430fbcb7f2cSJason Molenda   if (m_fde_index_initialized)
431fbcb7f2cSJason Molenda     return;
432fbcb7f2cSJason Molenda 
433bb19a13cSSaleem Abdulrasool   std::lock_guard<std::mutex> guard(m_fde_index_mutex);
4347d69f12eSSean Callanan 
4357d69f12eSSean Callanan   if (m_fde_index_initialized) // if two threads hit the locker
4367d69f12eSSean Callanan     return;
437e6194f17SJason Molenda 
438f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
439f9d16476SPavel Labath   Timer scoped_timer(func_cat, "%s - %s", LLVM_PRETTY_FUNCTION,
440b9c1b51eSKate Stone                      m_objfile.GetFileSpec().GetFilename().AsCString(""));
44174303829SJason Molenda 
442c7afda5aSJason Molenda   bool clear_address_zeroth_bit = false;
443c7afda5aSJason Molenda   ArchSpec arch;
444b9c1b51eSKate Stone   if (m_objfile.GetArchitecture(arch)) {
445b9c1b51eSKate Stone     if (arch.GetTriple().getArch() == llvm::Triple::arm ||
446b9c1b51eSKate Stone         arch.GetTriple().getArch() == llvm::Triple::thumb)
447c7afda5aSJason Molenda       clear_address_zeroth_bit = true;
448c7afda5aSJason Molenda   }
449c7afda5aSJason Molenda 
450c7bece56SGreg Clayton   lldb::offset_t offset = 0;
451fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
452f97c5213SGreg Clayton     GetCFIData();
453b9c1b51eSKate Stone   while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) {
45473bf5dbdSGreg Clayton     const dw_offset_t current_entry = offset;
4551767e115STodd Fiala     dw_offset_t cie_id, next_entry, cie_offset;
456fbcb7f2cSJason Molenda     uint32_t len = m_cfi_data.GetU32(&offset);
4571767e115STodd Fiala     bool is_64bit = (len == UINT32_MAX);
4581767e115STodd Fiala     if (is_64bit) {
4591767e115STodd Fiala       len = m_cfi_data.GetU64(&offset);
4601767e115STodd Fiala       cie_id = m_cfi_data.GetU64(&offset);
4611767e115STodd Fiala       next_entry = current_entry + len + 12;
4621767e115STodd Fiala       cie_offset = current_entry + 12 - cie_id;
4631767e115STodd Fiala     } else {
4641767e115STodd Fiala       cie_id = m_cfi_data.GetU32(&offset);
4651767e115STodd Fiala       next_entry = current_entry + len + 4;
4661767e115STodd Fiala       cie_offset = current_entry + 4 - cie_id;
4671767e115STodd Fiala     }
468fbcb7f2cSJason Molenda 
469b9c1b51eSKate Stone     if (next_entry > m_cfi_data.GetByteSize() + 1) {
470b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError, "error: Invalid fde/cie next "
471b9c1b51eSKate Stone                                              "entry offset of 0x%x found in "
472b9c1b51eSKate Stone                                              "cie/fde at 0x%x\n",
473b9c1b51eSKate Stone                       next_entry, current_entry);
474e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
475c48ef341SJason Molenda       // invalid data.
476c48ef341SJason Molenda       m_fde_index.Clear();
477c48ef341SJason Molenda       m_fde_index_initialized = true;
478c48ef341SJason Molenda       return;
479c48ef341SJason Molenda     }
480*cdda23ebSPavel Labath 
481*cdda23ebSPavel Labath     // An FDE entry contains CIE_pointer in debug_frame in same place as cie_id
482*cdda23ebSPavel Labath     // in eh_frame. CIE_pointer is an offset into the .debug_frame section.
483*cdda23ebSPavel Labath     // So, variable cie_offset should be equal to cie_id for debug_frame.
484*cdda23ebSPavel Labath     // FDE entries with cie_id == 0 shouldn't be ignored for it.
485*cdda23ebSPavel Labath     if ((cie_id == 0 && m_is_eh_frame) || cie_id == UINT32_MAX || len == 0) {
486*cdda23ebSPavel Labath       auto cie_sp = ParseCIE(current_entry);
487*cdda23ebSPavel Labath       if (!cie_sp) {
488*cdda23ebSPavel Labath         // Cannot parse, the reason is already logged
489*cdda23ebSPavel Labath         m_fde_index.Clear();
490*cdda23ebSPavel Labath         m_fde_index_initialized = true;
491*cdda23ebSPavel Labath         return;
492*cdda23ebSPavel Labath       }
493*cdda23ebSPavel Labath 
494*cdda23ebSPavel Labath       m_cie_map[current_entry] = std::move(cie_sp);
495*cdda23ebSPavel Labath       offset = next_entry;
496*cdda23ebSPavel Labath       continue;
497*cdda23ebSPavel Labath     }
498*cdda23ebSPavel Labath 
499*cdda23ebSPavel Labath     if (!m_is_eh_frame)
500*cdda23ebSPavel Labath       cie_offset = cie_id;
501*cdda23ebSPavel Labath 
502b9c1b51eSKate Stone     if (cie_offset > m_cfi_data.GetByteSize()) {
503*cdda23ebSPavel Labath       Host::SystemLog(Host::eSystemLogError,
504*cdda23ebSPavel Labath                       "error: Invalid cie offset of 0x%x "
505*cdda23ebSPavel Labath                       "found in cie/fde at 0x%x\n",
506b9c1b51eSKate Stone                       cie_offset, current_entry);
507e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
508c48ef341SJason Molenda       // invalid data.
509c48ef341SJason Molenda       m_fde_index.Clear();
510c48ef341SJason Molenda       m_fde_index_initialized = true;
511c48ef341SJason Molenda       return;
512c48ef341SJason Molenda     }
513c48ef341SJason Molenda 
51473bf5dbdSGreg Clayton     const CIE *cie = GetCIE(cie_offset);
515b9c1b51eSKate Stone     if (cie) {
516c9660546SGreg Clayton       const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
517fbcb7f2cSJason Molenda       const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
518fbcb7f2cSJason Molenda       const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
519fbcb7f2cSJason Molenda 
52029cb868aSZachary Turner       lldb::addr_t addr =
52129cb868aSZachary Turner           GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
52229cb868aSZachary Turner                           text_addr, data_addr);
523c7afda5aSJason Molenda       if (clear_address_zeroth_bit)
524c7afda5aSJason Molenda         addr &= ~1ull;
525c7afda5aSJason Molenda 
52629cb868aSZachary Turner       lldb::addr_t length = GetGNUEHPointer(
52729cb868aSZachary Turner           m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
52829cb868aSZachary Turner           pc_rel_addr, text_addr, data_addr);
52974303829SJason Molenda       FDEEntryMap::Entry fde(addr, length, current_entry);
53074303829SJason Molenda       m_fde_index.Append(fde);
531b9c1b51eSKate Stone     } else {
532b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError, "error: unable to find CIE at "
533b9c1b51eSKate Stone                                              "0x%8.8x for cie_id = 0x%8.8x for "
534b9c1b51eSKate Stone                                              "entry at 0x%8.8x.\n",
535b9c1b51eSKate Stone                       cie_offset, cie_id, current_entry);
53673bf5dbdSGreg Clayton     }
537fbcb7f2cSJason Molenda     offset = next_entry;
538fbcb7f2cSJason Molenda   }
53974303829SJason Molenda   m_fde_index.Sort();
540fbcb7f2cSJason Molenda   m_fde_index_initialized = true;
541fbcb7f2cSJason Molenda }
542fbcb7f2cSJason Molenda 
543b9c1b51eSKate Stone bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset,
544b9c1b51eSKate Stone                                          Address startaddr,
545b9c1b51eSKate Stone                                          UnwindPlan &unwind_plan) {
5461ee89eb8SPavel Labath   Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND);
547c7bece56SGreg Clayton   lldb::offset_t offset = dwarf_offset;
548c7bece56SGreg Clayton   lldb::offset_t current_entry = offset;
549fbcb7f2cSJason Molenda 
550d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
551fbcb7f2cSJason Molenda     return false;
552fbcb7f2cSJason Molenda 
553fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
554f97c5213SGreg Clayton     GetCFIData();
555fbcb7f2cSJason Molenda 
556fbcb7f2cSJason Molenda   uint32_t length = m_cfi_data.GetU32(&offset);
5571767e115STodd Fiala   dw_offset_t cie_offset;
5581767e115STodd Fiala   bool is_64bit = (length == UINT32_MAX);
5591767e115STodd Fiala   if (is_64bit) {
5601767e115STodd Fiala     length = m_cfi_data.GetU64(&offset);
5611767e115STodd Fiala     cie_offset = m_cfi_data.GetU64(&offset);
5621767e115STodd Fiala   } else {
5631767e115STodd Fiala     cie_offset = m_cfi_data.GetU32(&offset);
5641767e115STodd Fiala   }
565fbcb7f2cSJason Molenda 
566*cdda23ebSPavel Labath   // FDE entries with zeroth cie_offset may occur for debug_frame.
567*cdda23ebSPavel Labath   assert(!(m_is_eh_frame && 0 == cie_offset) && cie_offset != UINT32_MAX);
56830fdc8d8SChris Lattner 
56930fdc8d8SChris Lattner   // Translate the CIE_id from the eh_frame format, which
57030fdc8d8SChris Lattner   // is relative to the FDE offset, into a __eh_frame section
57130fdc8d8SChris Lattner   // offset
572b9c1b51eSKate Stone   if (m_is_eh_frame) {
573ab4f1924SJason Molenda     unwind_plan.SetSourceName("eh_frame CFI");
5741767e115STodd Fiala     cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
57560f0bd49SJason Molenda     unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
576b9c1b51eSKate Stone   } else {
577ab4f1924SJason Molenda     unwind_plan.SetSourceName("DWARF CFI");
57860f0bd49SJason Molenda     // In theory the debug_frame info should be valid at all call sites
57960f0bd49SJason Molenda     // ("asynchronous unwind info" as it is sometimes called) but in practice
58060f0bd49SJason Molenda     // gcc et al all emit call frame info for the prologue and call sites, but
581b9c1b51eSKate Stone     // not for the epilogue or all the other locations during the function
582b9c1b51eSKate Stone     // reliably.
58360f0bd49SJason Molenda     unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
584ab4f1924SJason Molenda   }
58560f0bd49SJason Molenda   unwind_plan.SetSourcedFromCompiler(eLazyBoolYes);
58630fdc8d8SChris Lattner 
58730fdc8d8SChris Lattner   const CIE *cie = GetCIE(cie_offset);
588d4612ad0SEd Maste   assert(cie != nullptr);
589fbcb7f2cSJason Molenda 
5901767e115STodd Fiala   const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
591fbcb7f2cSJason Molenda 
592c9660546SGreg Clayton   const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
59330fdc8d8SChris Lattner   const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
59430fdc8d8SChris Lattner   const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
59529cb868aSZachary Turner   lldb::addr_t range_base =
59629cb868aSZachary Turner       GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
597b9c1b51eSKate Stone                       text_addr, data_addr);
59829cb868aSZachary Turner   lldb::addr_t range_len = GetGNUEHPointer(
59929cb868aSZachary Turner       m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
60029cb868aSZachary Turner       pc_rel_addr, text_addr, data_addr);
601b9c1b51eSKate Stone   AddressRange range(range_base, m_objfile.GetAddressByteSize(),
602b9c1b51eSKate Stone                      m_objfile.GetSectionList());
603fbcb7f2cSJason Molenda   range.SetByteSize(range_len);
60430fdc8d8SChris Lattner 
605e9c7ecf6SJason Molenda   addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
606e9c7ecf6SJason Molenda 
607b9c1b51eSKate Stone   if (cie->augmentation[0] == 'z') {
60830fdc8d8SChris Lattner     uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
609b9c1b51eSKate Stone     if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) {
610e9c7ecf6SJason Molenda       offset_t saved_offset = offset;
61129cb868aSZachary Turner       lsda_data_file_address =
61229cb868aSZachary Turner           GetGNUEHPointer(m_cfi_data, &offset, cie->lsda_addr_encoding,
61329cb868aSZachary Turner                           pc_rel_addr, text_addr, data_addr);
614b9c1b51eSKate Stone       if (offset - saved_offset != aug_data_len) {
615e9c7ecf6SJason Molenda         // There is more in the augmentation region than we know how to process;
616e9c7ecf6SJason Molenda         // don't read anything.
617e9c7ecf6SJason Molenda         lsda_data_file_address = LLDB_INVALID_ADDRESS;
618e9c7ecf6SJason Molenda       }
619e9c7ecf6SJason Molenda       offset = saved_offset;
620e9c7ecf6SJason Molenda     }
62130fdc8d8SChris Lattner     offset += aug_data_len;
62230fdc8d8SChris Lattner   }
623e9c7ecf6SJason Molenda   Address lsda_data;
624e9c7ecf6SJason Molenda   Address personality_function_ptr;
625e9c7ecf6SJason Molenda 
626b9c1b51eSKate Stone   if (lsda_data_file_address != LLDB_INVALID_ADDRESS &&
627b9c1b51eSKate Stone       cie->personality_loc != LLDB_INVALID_ADDRESS) {
628b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address,
629b9c1b51eSKate Stone                                               lsda_data);
630b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc,
631b9c1b51eSKate Stone                                               personality_function_ptr);
632e9c7ecf6SJason Molenda   }
633e9c7ecf6SJason Molenda 
634b9c1b51eSKate Stone   if (lsda_data.IsValid() && personality_function_ptr.IsValid()) {
635e9c7ecf6SJason Molenda     unwind_plan.SetLSDAAddress(lsda_data);
636e9c7ecf6SJason Molenda     unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr);
637e9c7ecf6SJason Molenda   }
63830fdc8d8SChris Lattner 
63930fdc8d8SChris Lattner   uint32_t code_align = cie->code_align;
64030fdc8d8SChris Lattner   int32_t data_align = cie->data_align;
64130fdc8d8SChris Lattner 
642fbcb7f2cSJason Molenda   unwind_plan.SetPlanValidAddressRange(range);
6431d42c7bcSJason Molenda   UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
6441d42c7bcSJason Molenda   *cie_initial_row = cie->initial_row;
6451d42c7bcSJason Molenda   UnwindPlan::RowSP row(cie_initial_row);
64630fdc8d8SChris Lattner 
647fbcb7f2cSJason Molenda   unwind_plan.SetRegisterKind(m_reg_kind);
6488eba46c6SJason Molenda   unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num);
649fbcb7f2cSJason Molenda 
650f70d6040SRichard Mitton   std::vector<UnwindPlan::RowSP> stack;
651f70d6040SRichard Mitton 
652fbcb7f2cSJason Molenda   UnwindPlan::Row::RegisterLocation reg_location;
653b9c1b51eSKate Stone   while (m_cfi_data.ValidOffset(offset) && offset < end_offset) {
65430fdc8d8SChris Lattner     uint8_t inst = m_cfi_data.GetU8(&offset);
65530fdc8d8SChris Lattner     uint8_t primary_opcode = inst & 0xC0;
65630fdc8d8SChris Lattner     uint8_t extended_opcode = inst & 0x3F;
65730fdc8d8SChris Lattner 
658b9c1b51eSKate Stone     if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align,
659b9c1b51eSKate Stone                                  offset, *row)) {
660b9c1b51eSKate Stone       if (primary_opcode) {
661b9c1b51eSKate Stone         switch (primary_opcode) {
66230fdc8d8SChris Lattner         case DW_CFA_advance_loc: // (Row Creation Instruction)
66330fdc8d8SChris Lattner         { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
66430fdc8d8SChris Lattner           // takes a single argument that represents a constant delta. The
66530fdc8d8SChris Lattner           // required action is to create a new table row with a location
66630fdc8d8SChris Lattner           // value that is computed by taking the current entry's location
66730fdc8d8SChris Lattner           // value and adding (delta * code_align). All other
66830fdc8d8SChris Lattner           // values in the new row are initially identical to the current row.
669fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
670fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
671fa67e879SJason Molenda           *newrow = *row.get();
672fa67e879SJason Molenda           row.reset(newrow);
6731d42c7bcSJason Molenda           row->SlideOffset(extended_opcode * code_align);
67430fdc8d8SChris Lattner           break;
67530fdc8d8SChris Lattner         }
67630fdc8d8SChris Lattner 
677b9c1b51eSKate Stone         case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are
678b9c1b51eSKate Stone                                // register
67930fdc8d8SChris Lattner           // takes a single argument that represents a register number. The
68030fdc8d8SChris Lattner           // required action is to change the rule for the indicated register
68130fdc8d8SChris Lattner           // to the rule assigned it by the initial_instructions in the CIE.
682623652a0STamas Berghammer           uint32_t reg_num = extended_opcode;
68330fdc8d8SChris Lattner           // We only keep enough register locations around to
68430fdc8d8SChris Lattner           // unwind what is in our thread, and these are organized
68530fdc8d8SChris Lattner           // by the register index in that state, so we need to convert our
686b9c1b51eSKate Stone           // eh_frame register number from the EH frame info, to a register
687b9c1b51eSKate Stone           // index
68830fdc8d8SChris Lattner 
689b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
690b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
691b9c1b51eSKate Stone                                                             reg_location))
6921d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
69330fdc8d8SChris Lattner           break;
69430fdc8d8SChris Lattner         }
69530fdc8d8SChris Lattner         }
696b9c1b51eSKate Stone       } else {
697b9c1b51eSKate Stone         switch (extended_opcode) {
69830fdc8d8SChris Lattner         case DW_CFA_set_loc: // 0x1 (Row Creation Instruction)
69930fdc8d8SChris Lattner         {
70030fdc8d8SChris Lattner           // DW_CFA_set_loc takes a single argument that represents an address.
70130fdc8d8SChris Lattner           // The required action is to create a new table row using the
70230fdc8d8SChris Lattner           // specified address as the location. All other values in the new row
70330fdc8d8SChris Lattner           // are initially identical to the current row. The new location value
70430fdc8d8SChris Lattner           // should always be greater than the current one.
705fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
706fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
707fa67e879SJason Molenda           *newrow = *row.get();
708fa67e879SJason Molenda           row.reset(newrow);
709b9c1b51eSKate Stone           row->SetOffset(m_cfi_data.GetPointer(&offset) -
710b9c1b51eSKate Stone                          startaddr.GetFileAddress());
71130fdc8d8SChris Lattner           break;
712623652a0STamas Berghammer         }
71330fdc8d8SChris Lattner 
71430fdc8d8SChris Lattner         case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction)
71530fdc8d8SChris Lattner         {
71630fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
71730fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
71830fdc8d8SChris Lattner           // encoding and size of the delta argument.
719fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
720fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
721fa67e879SJason Molenda           *newrow = *row.get();
722fa67e879SJason Molenda           row.reset(newrow);
7231d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align);
72430fdc8d8SChris Lattner           break;
725623652a0STamas Berghammer         }
72630fdc8d8SChris Lattner 
72730fdc8d8SChris Lattner         case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction)
72830fdc8d8SChris Lattner         {
72930fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
73030fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
73130fdc8d8SChris Lattner           // encoding and size of the delta argument.
732fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
733fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
734fa67e879SJason Molenda           *newrow = *row.get();
735fa67e879SJason Molenda           row.reset(newrow);
7361d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align);
73730fdc8d8SChris Lattner           break;
738623652a0STamas Berghammer         }
73930fdc8d8SChris Lattner 
74030fdc8d8SChris Lattner         case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction)
74130fdc8d8SChris Lattner         {
74230fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
74330fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
74430fdc8d8SChris Lattner           // encoding and size of the delta argument.
745fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
746fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
747fa67e879SJason Molenda           *newrow = *row.get();
748fa67e879SJason Molenda           row.reset(newrow);
7491d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align);
75030fdc8d8SChris Lattner           break;
75130fdc8d8SChris Lattner         }
75230fdc8d8SChris Lattner 
75330fdc8d8SChris Lattner         case DW_CFA_restore_extended: // 0x6
75430fdc8d8SChris Lattner         {
75530fdc8d8SChris Lattner           // takes a single unsigned LEB128 argument that represents a register
75630fdc8d8SChris Lattner           // number. This instruction is identical to DW_CFA_restore except for
75730fdc8d8SChris Lattner           // the encoding and size of the register argument.
758623652a0STamas Berghammer           uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
759b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
760b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
761b9c1b51eSKate Stone                                                             reg_location))
7621d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
76330fdc8d8SChris Lattner           break;
76430fdc8d8SChris Lattner         }
76530fdc8d8SChris Lattner 
76630fdc8d8SChris Lattner         case DW_CFA_remember_state: // 0xA
767fa67e879SJason Molenda         {
76830fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
76930fdc8d8SChris Lattner           // DW_CFA_remember_state instruction means to save the rules for every
77030fdc8d8SChris Lattner           // register on the current row on the stack. Encountering the
77130fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
77230fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
77330fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
77430fdc8d8SChris Lattner           // function.)
775f70d6040SRichard Mitton           stack.push_back(row);
776fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
777fa67e879SJason Molenda           *newrow = *row.get();
778fa67e879SJason Molenda           row.reset(newrow);
77930fdc8d8SChris Lattner           break;
780623652a0STamas Berghammer         }
78130fdc8d8SChris Lattner 
78230fdc8d8SChris Lattner         case DW_CFA_restore_state: // 0xB
783623652a0STamas Berghammer         {
78430fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
78530fdc8d8SChris Lattner           // DW_CFA_remember_state instruction means to save the rules for every
78630fdc8d8SChris Lattner           // register on the current row on the stack. Encountering the
78730fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
78830fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
78930fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
79030fdc8d8SChris Lattner           // function.)
791b9c1b51eSKate Stone           if (stack.empty()) {
7921ee89eb8SPavel Labath             if (log)
793b9c1b51eSKate Stone               log->Printf("DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32
794b9c1b51eSKate Stone                           ", startaddr: %" PRIx64
795b9c1b51eSKate Stone                           " encountered DW_CFA_restore_state but state stack "
796b9c1b51eSKate Stone                           "is empty. Corrupt unwind info?",
797b9c1b51eSKate Stone                           __FUNCTION__, dwarf_offset,
798b9c1b51eSKate Stone                           startaddr.GetFileAddress());
7991ee89eb8SPavel Labath             break;
8001ee89eb8SPavel Labath           }
80149dd6d15STamas Berghammer           lldb::addr_t offset = row->GetOffset();
802f70d6040SRichard Mitton           row = stack.back();
803f70d6040SRichard Mitton           stack.pop_back();
80449dd6d15STamas Berghammer           row->SetOffset(offset);
80530fdc8d8SChris Lattner           break;
806623652a0STamas Berghammer         }
807623652a0STamas Berghammer 
8081ee89eb8SPavel Labath         case DW_CFA_GNU_args_size: // 0x2e
8091ee89eb8SPavel Labath         {
810b9c1b51eSKate Stone           // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128
811b9c1b51eSKate Stone           // operand
812b9c1b51eSKate Stone           // representing an argument size. This instruction specifies the total
813b9c1b51eSKate Stone           // of
8141ee89eb8SPavel Labath           // the size of the arguments which have been pushed onto the stack.
8151ee89eb8SPavel Labath 
8161ee89eb8SPavel Labath           // TODO: Figure out how we should handle this.
8171ee89eb8SPavel Labath           m_cfi_data.GetULEB128(&offset);
818a41deb1cSJason Molenda           break;
8191ee89eb8SPavel Labath         }
8201ee89eb8SPavel Labath 
821623652a0STamas Berghammer         case DW_CFA_val_offset:    // 0x14
822623652a0STamas Berghammer         case DW_CFA_val_offset_sf: // 0x15
823623652a0STamas Berghammer         default:
824623652a0STamas Berghammer           break;
825623652a0STamas Berghammer         }
826623652a0STamas Berghammer       }
827623652a0STamas Berghammer     }
828623652a0STamas Berghammer   }
829623652a0STamas Berghammer   unwind_plan.AppendRow(row);
830623652a0STamas Berghammer 
831623652a0STamas Berghammer   return true;
832623652a0STamas Berghammer }
833623652a0STamas Berghammer 
834b9c1b51eSKate Stone bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
835623652a0STamas Berghammer                                                  uint8_t extended_opcode,
836623652a0STamas Berghammer                                                  int32_t data_align,
837623652a0STamas Berghammer                                                  lldb::offset_t &offset,
838b9c1b51eSKate Stone                                                  UnwindPlan::Row &row) {
839623652a0STamas Berghammer   UnwindPlan::Row::RegisterLocation reg_location;
840623652a0STamas Berghammer 
841b9c1b51eSKate Stone   if (primary_opcode) {
842b9c1b51eSKate Stone     switch (primary_opcode) {
843b9c1b51eSKate Stone     case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are
844b9c1b51eSKate Stone                           // register
845623652a0STamas Berghammer       // takes two arguments: an unsigned LEB128 constant representing a
846623652a0STamas Berghammer       // factored offset and a register number. The required action is to
847623652a0STamas Berghammer       // change the rule for the register indicated by the register number
848623652a0STamas Berghammer       // to be an offset(N) rule with a value of
849623652a0STamas Berghammer       // (N = factored offset * data_align).
850623652a0STamas Berghammer       uint8_t reg_num = extended_opcode;
851623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
852623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
853623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
854623652a0STamas Berghammer       return true;
855623652a0STamas Berghammer     }
856623652a0STamas Berghammer     }
857b9c1b51eSKate Stone   } else {
858b9c1b51eSKate Stone     switch (extended_opcode) {
859623652a0STamas Berghammer     case DW_CFA_nop: // 0x0
860623652a0STamas Berghammer       return true;
861623652a0STamas Berghammer 
862623652a0STamas Berghammer     case DW_CFA_offset_extended: // 0x5
863623652a0STamas Berghammer     {
864623652a0STamas Berghammer       // takes two unsigned LEB128 arguments representing a register number
865623652a0STamas Berghammer       // and a factored offset. This instruction is identical to DW_CFA_offset
866623652a0STamas Berghammer       // except for the encoding and size of the register argument.
867623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
868623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
869623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
870623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
871623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
872623652a0STamas Berghammer       return true;
873623652a0STamas Berghammer     }
874623652a0STamas Berghammer 
875623652a0STamas Berghammer     case DW_CFA_undefined: // 0x7
876623652a0STamas Berghammer     {
877623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
878623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
879623652a0STamas Berghammer       // register to undefined.
880623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
881623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
882623652a0STamas Berghammer       reg_location.SetUndefined();
883623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
884623652a0STamas Berghammer       return true;
885623652a0STamas Berghammer     }
886623652a0STamas Berghammer 
887623652a0STamas Berghammer     case DW_CFA_same_value: // 0x8
888623652a0STamas Berghammer     {
889623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
890623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
891623652a0STamas Berghammer       // register to same value.
892623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
893623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
894623652a0STamas Berghammer       reg_location.SetSame();
895623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
896623652a0STamas Berghammer       return true;
897623652a0STamas Berghammer     }
898623652a0STamas Berghammer 
899623652a0STamas Berghammer     case DW_CFA_register: // 0x9
900623652a0STamas Berghammer     {
901623652a0STamas Berghammer       // takes two unsigned LEB128 arguments representing register numbers.
902623652a0STamas Berghammer       // The required action is to set the rule for the first register to be
903623652a0STamas Berghammer       // the second register.
904623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
905623652a0STamas Berghammer       uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
906623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
907623652a0STamas Berghammer       reg_location.SetInRegister(other_reg_num);
908623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
909623652a0STamas Berghammer       return true;
910623652a0STamas Berghammer     }
91130fdc8d8SChris Lattner 
91230fdc8d8SChris Lattner     case DW_CFA_def_cfa: // 0xC    (CFA Definition Instruction)
91330fdc8d8SChris Lattner     {
91430fdc8d8SChris Lattner       // Takes two unsigned LEB128 operands representing a register
91530fdc8d8SChris Lattner       // number and a (non-factored) offset. The required action
91630fdc8d8SChris Lattner       // is to define the current CFA rule to use the provided
91730fdc8d8SChris Lattner       // register and offset.
918623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
919623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
920623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
921623652a0STamas Berghammer       return true;
92230fdc8d8SChris Lattner     }
92330fdc8d8SChris Lattner 
92430fdc8d8SChris Lattner     case DW_CFA_def_cfa_register: // 0xD    (CFA Definition Instruction)
92530fdc8d8SChris Lattner     {
92630fdc8d8SChris Lattner       // takes a single unsigned LEB128 argument representing a register
92730fdc8d8SChris Lattner       // number. The required action is to define the current CFA rule to
92830fdc8d8SChris Lattner       // use the provided register (but to keep the old offset).
929623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
930b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num,
931b9c1b51eSKate Stone                                                 row.GetCFAValue().GetOffset());
932623652a0STamas Berghammer       return true;
93330fdc8d8SChris Lattner     }
93430fdc8d8SChris Lattner 
93530fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset: // 0xE    (CFA Definition Instruction)
93630fdc8d8SChris Lattner     {
93730fdc8d8SChris Lattner       // Takes a single unsigned LEB128 operand representing a
93830fdc8d8SChris Lattner       // (non-factored) offset. The required action is to define
93930fdc8d8SChris Lattner       // the current CFA rule to use the provided offset (but
94030fdc8d8SChris Lattner       // to keep the old register).
941623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
942b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(
943b9c1b51eSKate Stone           row.GetCFAValue().GetRegisterNumber(), op_offset);
944623652a0STamas Berghammer       return true;
94530fdc8d8SChris Lattner     }
94630fdc8d8SChris Lattner 
94730fdc8d8SChris Lattner     case DW_CFA_def_cfa_expression: // 0xF    (CFA Definition Instruction)
94830fdc8d8SChris Lattner     {
94930fdc8d8SChris Lattner       size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
950b9c1b51eSKate Stone       const uint8_t *block_data =
951b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
952623652a0STamas Berghammer       row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
953623652a0STamas Berghammer       return true;
95430fdc8d8SChris Lattner     }
95530fdc8d8SChris Lattner 
95630fdc8d8SChris Lattner     case DW_CFA_expression: // 0x10
95730fdc8d8SChris Lattner     {
95830fdc8d8SChris Lattner       // Takes two operands: an unsigned LEB128 value representing
95930fdc8d8SChris Lattner       // a register number, and a DW_FORM_block value representing a DWARF
96030fdc8d8SChris Lattner       // expression. The required action is to change the rule for the
96130fdc8d8SChris Lattner       // register indicated by the register number to be an expression(E)
96230fdc8d8SChris Lattner       // rule where E is the DWARF expression. That is, the DWARF
96330fdc8d8SChris Lattner       // expression computes the address. The value of the CFA is
96430fdc8d8SChris Lattner       // pushed on the DWARF evaluation stack prior to execution of
96530fdc8d8SChris Lattner       // the DWARF expression.
966623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
96730fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
968b9c1b51eSKate Stone       const uint8_t *block_data =
969b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
970623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
97130fdc8d8SChris Lattner       reg_location.SetAtDWARFExpression(block_data, block_len);
972623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
973623652a0STamas Berghammer       return true;
97430fdc8d8SChris Lattner     }
97530fdc8d8SChris Lattner 
97630fdc8d8SChris Lattner     case DW_CFA_offset_extended_sf: // 0x11
97730fdc8d8SChris Lattner     {
97830fdc8d8SChris Lattner       // takes two operands: an unsigned LEB128 value representing a
97930fdc8d8SChris Lattner       // register number and a signed LEB128 factored offset. This
98030fdc8d8SChris Lattner       // instruction is identical to DW_CFA_offset_extended except
98130fdc8d8SChris Lattner       // that the second operand is signed and factored.
982623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
983623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
984623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
98530fdc8d8SChris Lattner       reg_location.SetAtCFAPlusOffset(op_offset);
986623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
987623652a0STamas Berghammer       return true;
98830fdc8d8SChris Lattner     }
98930fdc8d8SChris Lattner 
99030fdc8d8SChris Lattner     case DW_CFA_def_cfa_sf: // 0x12   (CFA Definition Instruction)
99130fdc8d8SChris Lattner     {
99230fdc8d8SChris Lattner       // Takes two operands: an unsigned LEB128 value representing
99330fdc8d8SChris Lattner       // a register number and a signed LEB128 factored offset.
99430fdc8d8SChris Lattner       // This instruction is identical to DW_CFA_def_cfa except
99530fdc8d8SChris Lattner       // that the second operand is signed and factored.
996623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
997623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
998623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
999623652a0STamas Berghammer       return true;
100030fdc8d8SChris Lattner     }
100130fdc8d8SChris Lattner 
100230fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset_sf: // 0x13   (CFA Definition Instruction)
100330fdc8d8SChris Lattner     {
100430fdc8d8SChris Lattner       // takes a signed LEB128 operand representing a factored
100530fdc8d8SChris Lattner       // offset. This instruction is identical to  DW_CFA_def_cfa_offset
100630fdc8d8SChris Lattner       // except that the operand is signed and factored.
1007623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
1008623652a0STamas Berghammer       uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
1009623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
1010623652a0STamas Berghammer       return true;
101130fdc8d8SChris Lattner     }
101230fdc8d8SChris Lattner 
101330fdc8d8SChris Lattner     case DW_CFA_val_expression: // 0x16
101430fdc8d8SChris Lattner     {
101530fdc8d8SChris Lattner       // takes two operands: an unsigned LEB128 value representing a register
101630fdc8d8SChris Lattner       // number, and a DW_FORM_block value representing a DWARF expression.
101730fdc8d8SChris Lattner       // The required action is to change the rule for the register indicated
101830fdc8d8SChris Lattner       // by the register number to be a val_expression(E) rule where E is the
101930fdc8d8SChris Lattner       // DWARF expression. That is, the DWARF expression computes the value of
102030fdc8d8SChris Lattner       // the given register. The value of the CFA is pushed on the DWARF
102130fdc8d8SChris Lattner       // evaluation stack prior to execution of the DWARF expression.
1022623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
102330fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
1024b9c1b51eSKate Stone       const uint8_t *block_data =
1025b9c1b51eSKate Stone           (const uint8_t *)m_cfi_data.GetData(&offset, block_len);
102630fdc8d8SChris Lattner       //#if defined(__i386__) || defined(__x86_64__)
1027b9c1b51eSKate Stone       //              // The EH frame info for EIP and RIP contains code that
1028b9c1b51eSKate Stone       //              looks for traps to
102930fdc8d8SChris Lattner       //              // be a specific type and increments the PC.
103030fdc8d8SChris Lattner       //              // For i386:
103130fdc8d8SChris Lattner       //              // DW_CFA_val_expression where:
1032b9c1b51eSKate Stone       //              // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup,
1033b9c1b51eSKate Stone       //              DW_OP_plus_uconst(0x34),
1034b9c1b51eSKate Stone       //              //       DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0),
1035b9c1b51eSKate Stone       //              DW_OP_deref,
1036b9c1b51eSKate Stone       //              //       DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap,
1037b9c1b51eSKate Stone       //              DW_OP_lit4, DW_OP_ne,
103830fdc8d8SChris Lattner       //              //       DW_OP_and, DW_OP_plus
103930fdc8d8SChris Lattner       //              // This basically does a:
104030fdc8d8SChris Lattner       //              // eip = ucontenxt.mcontext32->gpr.eip;
1041b9c1b51eSKate Stone       //              // if (ucontenxt.mcontext32->exc.trapno != 3 &&
1042b9c1b51eSKate Stone       //              ucontenxt.mcontext32->exc.trapno != 4)
104330fdc8d8SChris Lattner       //              //   eip++;
104430fdc8d8SChris Lattner       //              //
104530fdc8d8SChris Lattner       //              // For x86_64:
104630fdc8d8SChris Lattner       //              // DW_CFA_val_expression where:
1047b9c1b51eSKate Stone       //              // rip =  DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup,
1048b9c1b51eSKate Stone       //              DW_OP_plus_uconst(0x90), DW_OP_deref,
1049b9c1b51eSKate Stone       //              //          DW_OP_swap, DW_OP_plus_uconst(0),
1050b9c1b51eSKate Stone       //              DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
1051b9c1b51eSKate Stone       //              //          DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
1052b9c1b51eSKate Stone       //              DW_OP_and, DW_OP_plus
105330fdc8d8SChris Lattner       //              // This basically does a:
105430fdc8d8SChris Lattner       //              // rip = ucontenxt.mcontext64->gpr.rip;
1055b9c1b51eSKate Stone       //              // if (ucontenxt.mcontext64->exc.trapno != 3 &&
1056b9c1b51eSKate Stone       //              ucontenxt.mcontext64->exc.trapno != 4)
105730fdc8d8SChris Lattner       //              //   rip++;
1058b9c1b51eSKate Stone       //              // The trap comparisons and increments are not needed as
1059b9c1b51eSKate Stone       //              it hoses up the unwound PC which
1060b9c1b51eSKate Stone       //              // is expected to point at least past the instruction that
1061b9c1b51eSKate Stone       //              causes the fault/trap. So we
1062b9c1b51eSKate Stone       //              // take it out by trimming the expression right at the
1063b9c1b51eSKate Stone       //              first "DW_OP_swap" opcodes
1064b9c1b51eSKate Stone       //              if (block_data != NULL && thread->GetPCRegNum(Thread::GCC)
1065b9c1b51eSKate Stone       //              == reg_num)
106630fdc8d8SChris Lattner       //              {
106730fdc8d8SChris Lattner       //                  if (thread->Is64Bit())
106830fdc8d8SChris Lattner       //                  {
1069b9c1b51eSKate Stone       //                      if (block_len > 9 && block_data[8] == DW_OP_swap
1070b9c1b51eSKate Stone       //                      && block_data[9] == DW_OP_plus_uconst)
107130fdc8d8SChris Lattner       //                          block_len = 8;
107230fdc8d8SChris Lattner       //                  }
107330fdc8d8SChris Lattner       //                  else
107430fdc8d8SChris Lattner       //                  {
1075b9c1b51eSKate Stone       //                      if (block_len > 8 && block_data[7] == DW_OP_swap
1076b9c1b51eSKate Stone       //                      && block_data[8] == DW_OP_plus_uconst)
107730fdc8d8SChris Lattner       //                          block_len = 7;
107830fdc8d8SChris Lattner       //                  }
107930fdc8d8SChris Lattner       //              }
108030fdc8d8SChris Lattner       //#endif
108130fdc8d8SChris Lattner       reg_location.SetIsDWARFExpression(block_data, block_len);
1082623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
1083fbcb7f2cSJason Molenda       return true;
108430fdc8d8SChris Lattner     }
1085623652a0STamas Berghammer     }
1086623652a0STamas Berghammer   }
1087623652a0STamas Berghammer   return false;
1088623652a0STamas Berghammer }
10896b63b148STamas Berghammer 
1090b9c1b51eSKate Stone void DWARFCallFrameInfo::ForEachFDEEntries(
1091b9c1b51eSKate Stone     const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) {
10926b63b148STamas Berghammer   GetFDEIndex();
10936b63b148STamas Berghammer 
1094b9c1b51eSKate Stone   for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) {
10956b63b148STamas Berghammer     const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i);
10966b63b148STamas Berghammer     if (!callback(entry.base, entry.size, entry.data))
10976b63b148STamas Berghammer       break;
10986b63b148STamas Berghammer   }
10996b63b148STamas Berghammer }
1100