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 
10*5f19b907SPavel Labath #include "lldb/Symbol/DWARFCallFrameInfo.h"
1130fdc8d8SChris Lattner #include "lldb/Core/Module.h"
1230fdc8d8SChris Lattner #include "lldb/Core/Section.h"
1329cb868aSZachary Turner #include "lldb/Core/dwarf.h"
14e38a5eddSGreg Clayton #include "lldb/Host/Host.h"
15e38a5eddSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
16fbcb7f2cSJason Molenda #include "lldb/Symbol/UnwindPlan.h"
17e38a5eddSGreg Clayton #include "lldb/Target/RegisterContext.h"
18e38a5eddSGreg Clayton #include "lldb/Target/Thread.h"
19*5f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
206f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2138d0632eSPavel Labath #include "lldb/Utility/Timer.h"
22*5f19b907SPavel Labath #include <list>
2330fdc8d8SChris Lattner 
2430fdc8d8SChris Lattner using namespace lldb;
2530fdc8d8SChris Lattner using namespace lldb_private;
2630fdc8d8SChris Lattner 
2729cb868aSZachary Turner //----------------------------------------------------------------------
2829cb868aSZachary Turner // GetDwarfEHPtr
2929cb868aSZachary Turner //
3029cb868aSZachary Turner // Used for calls when the value type is specified by a DWARF EH Frame
3129cb868aSZachary Turner // pointer encoding.
3229cb868aSZachary Turner //----------------------------------------------------------------------
3329cb868aSZachary Turner static uint64_t
3429cb868aSZachary Turner GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr,
3529cb868aSZachary Turner                 uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr,
3629cb868aSZachary Turner                 addr_t data_addr) //, BSDRelocs *data_relocs) const
3729cb868aSZachary Turner {
3829cb868aSZachary Turner   if (eh_ptr_enc == DW_EH_PE_omit)
3929cb868aSZachary Turner     return ULLONG_MAX; // Value isn't in the buffer...
4029cb868aSZachary Turner 
4129cb868aSZachary Turner   uint64_t baseAddress = 0;
4229cb868aSZachary Turner   uint64_t addressValue = 0;
4329cb868aSZachary Turner   const uint32_t addr_size = DE.GetAddressByteSize();
4429cb868aSZachary Turner #ifdef LLDB_CONFIGURATION_DEBUG
4529cb868aSZachary Turner   assert(addr_size == 4 || addr_size == 8);
4629cb868aSZachary Turner #endif
4729cb868aSZachary Turner 
4829cb868aSZachary Turner   bool signExtendValue = false;
4929cb868aSZachary Turner   // Decode the base part or adjust our offset
5029cb868aSZachary Turner   switch (eh_ptr_enc & 0x70) {
5129cb868aSZachary Turner   case DW_EH_PE_pcrel:
5229cb868aSZachary Turner     signExtendValue = true;
5329cb868aSZachary Turner     baseAddress = *offset_ptr;
5429cb868aSZachary Turner     if (pc_rel_addr != LLDB_INVALID_ADDRESS)
5529cb868aSZachary Turner       baseAddress += pc_rel_addr;
5629cb868aSZachary Turner     //      else
5729cb868aSZachary Turner     //          Log::GlobalWarning ("PC relative pointer encoding found with
5829cb868aSZachary Turner     //          invalid pc relative address.");
5929cb868aSZachary Turner     break;
6029cb868aSZachary Turner 
6129cb868aSZachary Turner   case DW_EH_PE_textrel:
6229cb868aSZachary Turner     signExtendValue = true;
6329cb868aSZachary Turner     if (text_addr != LLDB_INVALID_ADDRESS)
6429cb868aSZachary Turner       baseAddress = text_addr;
6529cb868aSZachary Turner     //      else
6629cb868aSZachary Turner     //          Log::GlobalWarning ("text relative pointer encoding being
6729cb868aSZachary Turner     //          decoded with invalid text section address, setting base address
6829cb868aSZachary Turner     //          to zero.");
6929cb868aSZachary Turner     break;
7029cb868aSZachary Turner 
7129cb868aSZachary Turner   case DW_EH_PE_datarel:
7229cb868aSZachary Turner     signExtendValue = true;
7329cb868aSZachary Turner     if (data_addr != LLDB_INVALID_ADDRESS)
7429cb868aSZachary Turner       baseAddress = data_addr;
7529cb868aSZachary Turner     //      else
7629cb868aSZachary Turner     //          Log::GlobalWarning ("data relative pointer encoding being
7729cb868aSZachary Turner     //          decoded with invalid data section address, setting base address
7829cb868aSZachary Turner     //          to zero.");
7929cb868aSZachary Turner     break;
8029cb868aSZachary Turner 
8129cb868aSZachary Turner   case DW_EH_PE_funcrel:
8229cb868aSZachary Turner     signExtendValue = true;
8329cb868aSZachary Turner     break;
8429cb868aSZachary Turner 
8529cb868aSZachary Turner   case DW_EH_PE_aligned: {
8629cb868aSZachary Turner     // SetPointerSize should be called prior to extracting these so the
8729cb868aSZachary Turner     // pointer size is cached
8829cb868aSZachary Turner     assert(addr_size != 0);
8929cb868aSZachary Turner     if (addr_size) {
9029cb868aSZachary Turner       // Align to a address size boundary first
9129cb868aSZachary Turner       uint32_t alignOffset = *offset_ptr % addr_size;
9229cb868aSZachary Turner       if (alignOffset)
9329cb868aSZachary Turner         offset_ptr += addr_size - alignOffset;
9429cb868aSZachary Turner     }
9529cb868aSZachary Turner   } break;
9629cb868aSZachary Turner 
9729cb868aSZachary Turner   default:
9829cb868aSZachary Turner     break;
9929cb868aSZachary Turner   }
10029cb868aSZachary Turner 
10129cb868aSZachary Turner   // Decode the value part
10229cb868aSZachary Turner   switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) {
10329cb868aSZachary Turner   case DW_EH_PE_absptr: {
10429cb868aSZachary Turner     addressValue = DE.GetAddress(offset_ptr);
10529cb868aSZachary Turner     //          if (data_relocs)
10629cb868aSZachary Turner     //              addressValue = data_relocs->Relocate(*offset_ptr -
10729cb868aSZachary Turner     //              addr_size, *this, addressValue);
10829cb868aSZachary Turner   } break;
10929cb868aSZachary Turner   case DW_EH_PE_uleb128:
11029cb868aSZachary Turner     addressValue = DE.GetULEB128(offset_ptr);
11129cb868aSZachary Turner     break;
11229cb868aSZachary Turner   case DW_EH_PE_udata2:
11329cb868aSZachary Turner     addressValue = DE.GetU16(offset_ptr);
11429cb868aSZachary Turner     break;
11529cb868aSZachary Turner   case DW_EH_PE_udata4:
11629cb868aSZachary Turner     addressValue = DE.GetU32(offset_ptr);
11729cb868aSZachary Turner     break;
11829cb868aSZachary Turner   case DW_EH_PE_udata8:
11929cb868aSZachary Turner     addressValue = DE.GetU64(offset_ptr);
12029cb868aSZachary Turner     break;
12129cb868aSZachary Turner   case DW_EH_PE_sleb128:
12229cb868aSZachary Turner     addressValue = DE.GetSLEB128(offset_ptr);
12329cb868aSZachary Turner     break;
12429cb868aSZachary Turner   case DW_EH_PE_sdata2:
12529cb868aSZachary Turner     addressValue = (int16_t)DE.GetU16(offset_ptr);
12629cb868aSZachary Turner     break;
12729cb868aSZachary Turner   case DW_EH_PE_sdata4:
12829cb868aSZachary Turner     addressValue = (int32_t)DE.GetU32(offset_ptr);
12929cb868aSZachary Turner     break;
13029cb868aSZachary Turner   case DW_EH_PE_sdata8:
13129cb868aSZachary Turner     addressValue = (int64_t)DE.GetU64(offset_ptr);
13229cb868aSZachary Turner     break;
13329cb868aSZachary Turner   default:
13429cb868aSZachary Turner     // Unhandled encoding type
13529cb868aSZachary Turner     assert(eh_ptr_enc);
13629cb868aSZachary Turner     break;
13729cb868aSZachary Turner   }
13829cb868aSZachary Turner 
13929cb868aSZachary Turner   // Since we promote everything to 64 bit, we may need to sign extend
14029cb868aSZachary Turner   if (signExtendValue && addr_size < sizeof(baseAddress)) {
14129cb868aSZachary Turner     uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
14229cb868aSZachary Turner     if (sign_bit & addressValue) {
14329cb868aSZachary Turner       uint64_t mask = ~sign_bit + 1;
14429cb868aSZachary Turner       addressValue |= mask;
14529cb868aSZachary Turner     }
14629cb868aSZachary Turner   }
14729cb868aSZachary Turner   return baseAddress + addressValue;
14829cb868aSZachary Turner }
14929cb868aSZachary Turner 
150b9c1b51eSKate Stone DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile &objfile,
1513f2a0818SPavel Labath                                        SectionSP &section_sp, Type type)
1523f2a0818SPavel Labath     : m_objfile(objfile), m_section_sp(section_sp), m_type(type) {}
15330fdc8d8SChris Lattner 
154b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetUnwindPlan(Address addr, UnwindPlan &unwind_plan) {
15574303829SJason Molenda   FDEEntryMap::Entry fde_entry;
15674303829SJason Molenda 
15774303829SJason Molenda   // Make sure that the Address we're searching for is the same object file
15874303829SJason Molenda   // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
15974303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
160b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
161b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
162fbcb7f2cSJason Molenda     return false;
16374303829SJason Molenda 
16474303829SJason Molenda   if (GetFDEEntryByFileAddress(addr.GetFileAddress(), fde_entry) == false)
16574303829SJason Molenda     return false;
16674303829SJason Molenda   return FDEToUnwindPlan(fde_entry.data, addr, unwind_plan);
16730fdc8d8SChris Lattner }
16830fdc8d8SChris Lattner 
169b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) {
17074303829SJason Molenda 
17174303829SJason Molenda   // Make sure that the Address we're searching for is the same object file
17274303829SJason Molenda   // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
17374303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
174b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
175b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
17674303829SJason Molenda     return false;
17774303829SJason Molenda 
178d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
179fbcb7f2cSJason Molenda     return false;
180fbcb7f2cSJason Molenda   GetFDEIndex();
181b9c1b51eSKate Stone   FDEEntryMap::Entry *fde_entry =
182b9c1b51eSKate Stone       m_fde_index.FindEntryThatContains(addr.GetFileAddress());
18374303829SJason Molenda   if (!fde_entry)
184fbcb7f2cSJason Molenda     return false;
185fbcb7f2cSJason Molenda 
186b9c1b51eSKate Stone   range = AddressRange(fde_entry->base, fde_entry->size,
187b9c1b51eSKate Stone                        m_objfile.GetSectionList());
188fbcb7f2cSJason Molenda   return true;
18930fdc8d8SChris Lattner }
19030fdc8d8SChris Lattner 
191b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetFDEEntryByFileAddress(
192b9c1b51eSKate Stone     addr_t file_addr, FDEEntryMap::Entry &fde_entry) {
193d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
194fbcb7f2cSJason Molenda     return false;
19574303829SJason Molenda 
19674303829SJason Molenda   GetFDEIndex();
19774303829SJason Molenda 
19874303829SJason Molenda   if (m_fde_index.IsEmpty())
19974303829SJason Molenda     return false;
20074303829SJason Molenda 
20174303829SJason Molenda   FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains(file_addr);
20274303829SJason Molenda 
203d4612ad0SEd Maste   if (fde == nullptr)
20474303829SJason Molenda     return false;
20574303829SJason Molenda 
20674303829SJason Molenda   fde_entry = *fde;
20774303829SJason Molenda   return true;
20830fdc8d8SChris Lattner }
20930fdc8d8SChris Lattner 
210b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFunctionAddressAndSizeVector(
211b9c1b51eSKate Stone     FunctionAddressAndSizeVector &function_info) {
2125635f77aSJason Molenda   GetFDEIndex();
2135635f77aSJason Molenda   const size_t count = m_fde_index.GetSize();
214ee7593fbSJason Molenda   function_info.Clear();
215c1946cd8SJason Molenda   if (count > 0)
216ee7593fbSJason Molenda     function_info.Reserve(count);
217b9c1b51eSKate Stone   for (size_t i = 0; i < count; ++i) {
218b9c1b51eSKate Stone     const FDEEntryMap::Entry *func_offset_data_entry =
219b9c1b51eSKate Stone         m_fde_index.GetEntryAtIndex(i);
220b9c1b51eSKate Stone     if (func_offset_data_entry) {
221b9c1b51eSKate Stone       FunctionAddressAndSizeVector::Entry function_offset_entry(
222b9c1b51eSKate Stone           func_offset_data_entry->base, func_offset_data_entry->size);
2235635f77aSJason Molenda       function_info.Append(function_offset_entry);
2245635f77aSJason Molenda     }
2255635f77aSJason Molenda   }
2265635f77aSJason Molenda }
2275635f77aSJason Molenda 
22830fdc8d8SChris Lattner const DWARFCallFrameInfo::CIE *
229b9c1b51eSKate Stone DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) {
23030fdc8d8SChris Lattner   cie_map_t::iterator pos = m_cie_map.find(cie_offset);
23130fdc8d8SChris Lattner 
232b9c1b51eSKate Stone   if (pos != m_cie_map.end()) {
23330fdc8d8SChris Lattner     // Parse and cache the CIE
234d4612ad0SEd Maste     if (pos->second.get() == nullptr)
23530fdc8d8SChris Lattner       pos->second = ParseCIE(cie_offset);
23630fdc8d8SChris Lattner 
23730fdc8d8SChris Lattner     return pos->second.get();
23830fdc8d8SChris Lattner   }
239d4612ad0SEd Maste   return nullptr;
24030fdc8d8SChris Lattner }
24130fdc8d8SChris Lattner 
242fbcb7f2cSJason Molenda DWARFCallFrameInfo::CIESP
243b9c1b51eSKate Stone DWARFCallFrameInfo::ParseCIE(const dw_offset_t cie_offset) {
244fbcb7f2cSJason Molenda   CIESP cie_sp(new CIE(cie_offset));
245c7bece56SGreg Clayton   lldb::offset_t offset = cie_offset;
246fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
247f97c5213SGreg Clayton     GetCFIData();
2481767e115STodd Fiala   uint32_t length = m_cfi_data.GetU32(&offset);
2491767e115STodd Fiala   dw_offset_t cie_id, end_offset;
2501767e115STodd Fiala   bool is_64bit = (length == UINT32_MAX);
2511767e115STodd Fiala   if (is_64bit) {
2521767e115STodd Fiala     length = m_cfi_data.GetU64(&offset);
2531767e115STodd Fiala     cie_id = m_cfi_data.GetU64(&offset);
2541767e115STodd Fiala     end_offset = cie_offset + length + 12;
2551767e115STodd Fiala   } else {
2561767e115STodd Fiala     cie_id = m_cfi_data.GetU32(&offset);
2571767e115STodd Fiala     end_offset = cie_offset + length + 4;
2581767e115STodd Fiala   }
2593f2a0818SPavel Labath   if (length > 0 && ((m_type == DWARF && cie_id == UINT32_MAX) ||
2603f2a0818SPavel Labath                      (m_type == EH && cie_id == 0ul))) {
26130fdc8d8SChris Lattner     size_t i;
26230fdc8d8SChris Lattner     //    cie.offset = cie_offset;
26330fdc8d8SChris Lattner     //    cie.length = length;
26430fdc8d8SChris Lattner     //    cie.cieID = cieID;
2656e264d39SAshok Thirumurthi     cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
26630fdc8d8SChris Lattner     cie_sp->version = m_cfi_data.GetU8(&offset);
267cdda23ebSPavel Labath     if (cie_sp->version > CFI_VERSION4) {
268cdda23ebSPavel Labath       Host::SystemLog(Host::eSystemLogError,
269cdda23ebSPavel Labath                       "CIE parse error: CFI version %d is not supported\n",
270cdda23ebSPavel Labath                       cie_sp->version);
271cdda23ebSPavel Labath       return nullptr;
272cdda23ebSPavel Labath     }
27330fdc8d8SChris Lattner 
274b9c1b51eSKate Stone     for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) {
27530fdc8d8SChris Lattner       cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
276b9c1b51eSKate Stone       if (cie_sp->augmentation[i] == '\0') {
27730fdc8d8SChris Lattner         // Zero out remaining bytes in augmentation string
27830fdc8d8SChris Lattner         for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j)
27930fdc8d8SChris Lattner           cie_sp->augmentation[j] = '\0';
28030fdc8d8SChris Lattner 
28130fdc8d8SChris Lattner         break;
28230fdc8d8SChris Lattner       }
28330fdc8d8SChris Lattner     }
28430fdc8d8SChris Lattner 
285b9c1b51eSKate Stone     if (i == CFI_AUG_MAX_SIZE &&
286b9c1b51eSKate Stone         cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') {
287b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError,
288b9c1b51eSKate Stone                       "CIE parse error: CIE augmentation string was too large "
289b9c1b51eSKate Stone                       "for the fixed sized buffer of %d bytes.\n",
290b9c1b51eSKate Stone                       CFI_AUG_MAX_SIZE);
291cdda23ebSPavel Labath       return nullptr;
29230fdc8d8SChris Lattner     }
293cdda23ebSPavel Labath 
294cdda23ebSPavel Labath     // m_cfi_data uses address size from target architecture of the process
295cdda23ebSPavel Labath     // may ignore these fields?
2963f2a0818SPavel Labath     if (m_type == DWARF && cie_sp->version >= CFI_VERSION4) {
297cdda23ebSPavel Labath       cie_sp->address_size = m_cfi_data.GetU8(&offset);
298cdda23ebSPavel Labath       cie_sp->segment_size = m_cfi_data.GetU8(&offset);
299cdda23ebSPavel Labath     }
300cdda23ebSPavel Labath 
30130fdc8d8SChris Lattner     cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
30230fdc8d8SChris Lattner     cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
303cdda23ebSPavel Labath 
304cdda23ebSPavel Labath     cie_sp->return_addr_reg_num =
3053f2a0818SPavel Labath         m_type == DWARF && cie_sp->version >= CFI_VERSION3
306cdda23ebSPavel Labath             ? static_cast<uint32_t>(m_cfi_data.GetULEB128(&offset))
307cdda23ebSPavel Labath             : m_cfi_data.GetU8(&offset);
30830fdc8d8SChris Lattner 
309b9c1b51eSKate Stone     if (cie_sp->augmentation[0]) {
31030fdc8d8SChris Lattner       // Get the length of the eh_frame augmentation data
31130fdc8d8SChris Lattner       // which starts with a ULEB128 length in bytes
31230fdc8d8SChris Lattner       const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
31330fdc8d8SChris Lattner       const size_t aug_data_end = offset + aug_data_len;
31430fdc8d8SChris Lattner       const size_t aug_str_len = strlen(cie_sp->augmentation);
31530fdc8d8SChris Lattner       // A 'z' may be present as the first character of the string.
31630fdc8d8SChris Lattner       // If present, the Augmentation Data field shall be present.
31758ef391fSBruce Mitchener       // The contents of the Augmentation Data shall be interpreted
31830fdc8d8SChris Lattner       // according to other characters in the Augmentation String.
319b9c1b51eSKate Stone       if (cie_sp->augmentation[0] == 'z') {
32030fdc8d8SChris Lattner         // Extract the Augmentation Data
32130fdc8d8SChris Lattner         size_t aug_str_idx = 0;
322b9c1b51eSKate Stone         for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) {
32330fdc8d8SChris Lattner           char aug = cie_sp->augmentation[aug_str_idx];
324b9c1b51eSKate Stone           switch (aug) {
32530fdc8d8SChris Lattner           case 'L':
32630fdc8d8SChris Lattner             // Indicates the presence of one argument in the
32730fdc8d8SChris Lattner             // Augmentation Data of the CIE, and a corresponding
32830fdc8d8SChris Lattner             // argument in the Augmentation Data of the FDE. The
32930fdc8d8SChris Lattner             // argument in the Augmentation Data of the CIE is
33030fdc8d8SChris Lattner             // 1-byte and represents the pointer encoding used
33130fdc8d8SChris Lattner             // for the argument in the Augmentation Data of the
33230fdc8d8SChris Lattner             // FDE, which is the address of a language-specific
33330fdc8d8SChris Lattner             // data area (LSDA). The size of the LSDA pointer is
33430fdc8d8SChris Lattner             // specified by the pointer encoding used.
335e9c7ecf6SJason Molenda             cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
33630fdc8d8SChris Lattner             break;
33730fdc8d8SChris Lattner 
33830fdc8d8SChris Lattner           case 'P':
33930fdc8d8SChris Lattner             // Indicates the presence of two arguments in the
340e9c7ecf6SJason Molenda             // Augmentation Data of the CIE. The first argument
34130fdc8d8SChris Lattner             // is 1-byte and represents the pointer encoding
34230fdc8d8SChris Lattner             // used for the second argument, which is the
34330fdc8d8SChris Lattner             // address of a personality routine handler. The
34430fdc8d8SChris Lattner             // size of the personality routine pointer is
34530fdc8d8SChris Lattner             // specified by the pointer encoding used.
346e9c7ecf6SJason Molenda             //
347e9c7ecf6SJason Molenda             // The address of the personality function will
348e9c7ecf6SJason Molenda             // be stored at this location.  Pre-execution, it
349e9c7ecf6SJason Molenda             // will be all zero's so don't read it until we're
350e9c7ecf6SJason Molenda             // trying to do an unwind & the reloc has been
351e9c7ecf6SJason Molenda             // resolved.
35230fdc8d8SChris Lattner             {
35330fdc8d8SChris Lattner               uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
354e9c7ecf6SJason Molenda               const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
35529cb868aSZachary Turner               cie_sp->personality_loc = GetGNUEHPointer(
35629cb868aSZachary Turner                   m_cfi_data, &offset, arg_ptr_encoding, pc_rel_addr,
35729cb868aSZachary Turner                   LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
35830fdc8d8SChris Lattner             }
35930fdc8d8SChris Lattner             break;
36030fdc8d8SChris Lattner 
36130fdc8d8SChris Lattner           case 'R':
36230fdc8d8SChris Lattner             // A 'R' may be present at any position after the
36330fdc8d8SChris Lattner             // first character of the string. The Augmentation
36430fdc8d8SChris Lattner             // Data shall include a 1 byte argument that
36530fdc8d8SChris Lattner             // represents the pointer encoding for the address
36630fdc8d8SChris Lattner             // pointers used in the FDE.
3676e264d39SAshok Thirumurthi             // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4
36830fdc8d8SChris Lattner             cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
36930fdc8d8SChris Lattner             break;
37030fdc8d8SChris Lattner           }
37130fdc8d8SChris Lattner         }
372b9c1b51eSKate Stone       } else if (strcmp(cie_sp->augmentation, "eh") == 0) {
37330fdc8d8SChris Lattner         // If the Augmentation string has the value "eh", then
37430fdc8d8SChris Lattner         // the EH Data field shall be present
37530fdc8d8SChris Lattner       }
37630fdc8d8SChris Lattner 
37730fdc8d8SChris Lattner       // Set the offset to be the end of the augmentation data just in case
37830fdc8d8SChris Lattner       // we didn't understand any of the data.
37930fdc8d8SChris Lattner       offset = (uint32_t)aug_data_end;
38030fdc8d8SChris Lattner     }
38130fdc8d8SChris Lattner 
382b9c1b51eSKate Stone     if (end_offset > offset) {
38330fdc8d8SChris Lattner       cie_sp->inst_offset = offset;
38430fdc8d8SChris Lattner       cie_sp->inst_length = end_offset - offset;
38530fdc8d8SChris Lattner     }
386b9c1b51eSKate Stone     while (offset < end_offset) {
387fbcb7f2cSJason Molenda       uint8_t inst = m_cfi_data.GetU8(&offset);
388fbcb7f2cSJason Molenda       uint8_t primary_opcode = inst & 0xC0;
389fbcb7f2cSJason Molenda       uint8_t extended_opcode = inst & 0x3F;
390fbcb7f2cSJason Molenda 
391b9c1b51eSKate Stone       if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode,
392b9c1b51eSKate Stone                                    cie_sp->data_align, offset,
393b9c1b51eSKate Stone                                    cie_sp->initial_row))
394fbcb7f2cSJason Molenda         break; // Stop if we hit an unrecognized opcode
395fbcb7f2cSJason Molenda     }
39630fdc8d8SChris Lattner   }
39730fdc8d8SChris Lattner 
39830fdc8d8SChris Lattner   return cie_sp;
39930fdc8d8SChris Lattner }
40030fdc8d8SChris Lattner 
401b9c1b51eSKate Stone void DWARFCallFrameInfo::GetCFIData() {
402b9c1b51eSKate Stone   if (m_cfi_data_initialized == false) {
4035160ce5cSGreg Clayton     Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
404f97c5213SGreg Clayton     if (log)
4055160ce5cSGreg Clayton       m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
406c9660546SGreg Clayton     m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data);
407f97c5213SGreg Clayton     m_cfi_data_initialized = true;
408f97c5213SGreg Clayton   }
409f97c5213SGreg Clayton }
410b9c1b51eSKate Stone // Scan through the eh_frame or debug_frame section looking for FDEs and noting
411b9c1b51eSKate Stone // the start/end addresses
412b9c1b51eSKate Stone // of the functions and a pointer back to the function's FDE for later
413b9c1b51eSKate Stone // expansion.
414fbcb7f2cSJason Molenda // Internalize CIEs as we come across them.
41530fdc8d8SChris Lattner 
416b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFDEIndex() {
417d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
418fbcb7f2cSJason Molenda     return;
4197d69f12eSSean Callanan 
420fbcb7f2cSJason Molenda   if (m_fde_index_initialized)
421fbcb7f2cSJason Molenda     return;
422fbcb7f2cSJason Molenda 
423bb19a13cSSaleem Abdulrasool   std::lock_guard<std::mutex> guard(m_fde_index_mutex);
4247d69f12eSSean Callanan 
4257d69f12eSSean Callanan   if (m_fde_index_initialized) // if two threads hit the locker
4267d69f12eSSean Callanan     return;
427e6194f17SJason Molenda 
428f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
429f9d16476SPavel Labath   Timer scoped_timer(func_cat, "%s - %s", LLVM_PRETTY_FUNCTION,
430b9c1b51eSKate Stone                      m_objfile.GetFileSpec().GetFilename().AsCString(""));
43174303829SJason Molenda 
432c7afda5aSJason Molenda   bool clear_address_zeroth_bit = false;
433c7afda5aSJason Molenda   ArchSpec arch;
434b9c1b51eSKate Stone   if (m_objfile.GetArchitecture(arch)) {
435b9c1b51eSKate Stone     if (arch.GetTriple().getArch() == llvm::Triple::arm ||
436b9c1b51eSKate Stone         arch.GetTriple().getArch() == llvm::Triple::thumb)
437c7afda5aSJason Molenda       clear_address_zeroth_bit = true;
438c7afda5aSJason Molenda   }
439c7afda5aSJason Molenda 
440c7bece56SGreg Clayton   lldb::offset_t offset = 0;
441fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
442f97c5213SGreg Clayton     GetCFIData();
443b9c1b51eSKate Stone   while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) {
44473bf5dbdSGreg Clayton     const dw_offset_t current_entry = offset;
4451767e115STodd Fiala     dw_offset_t cie_id, next_entry, cie_offset;
446fbcb7f2cSJason Molenda     uint32_t len = m_cfi_data.GetU32(&offset);
4471767e115STodd Fiala     bool is_64bit = (len == UINT32_MAX);
4481767e115STodd Fiala     if (is_64bit) {
4491767e115STodd Fiala       len = m_cfi_data.GetU64(&offset);
4501767e115STodd Fiala       cie_id = m_cfi_data.GetU64(&offset);
4511767e115STodd Fiala       next_entry = current_entry + len + 12;
4521767e115STodd Fiala       cie_offset = current_entry + 12 - cie_id;
4531767e115STodd Fiala     } else {
4541767e115STodd Fiala       cie_id = m_cfi_data.GetU32(&offset);
4551767e115STodd Fiala       next_entry = current_entry + len + 4;
4561767e115STodd Fiala       cie_offset = current_entry + 4 - cie_id;
4571767e115STodd Fiala     }
458fbcb7f2cSJason Molenda 
459b9c1b51eSKate Stone     if (next_entry > m_cfi_data.GetByteSize() + 1) {
460b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError, "error: Invalid fde/cie next "
461b9c1b51eSKate Stone                                              "entry offset of 0x%x found in "
462b9c1b51eSKate Stone                                              "cie/fde at 0x%x\n",
463b9c1b51eSKate Stone                       next_entry, current_entry);
464e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
465c48ef341SJason Molenda       // invalid data.
466c48ef341SJason Molenda       m_fde_index.Clear();
467c48ef341SJason Molenda       m_fde_index_initialized = true;
468c48ef341SJason Molenda       return;
469c48ef341SJason Molenda     }
470cdda23ebSPavel Labath 
471cdda23ebSPavel Labath     // An FDE entry contains CIE_pointer in debug_frame in same place as cie_id
472cdda23ebSPavel Labath     // in eh_frame. CIE_pointer is an offset into the .debug_frame section.
473cdda23ebSPavel Labath     // So, variable cie_offset should be equal to cie_id for debug_frame.
474cdda23ebSPavel Labath     // FDE entries with cie_id == 0 shouldn't be ignored for it.
4753f2a0818SPavel Labath     if ((cie_id == 0 && m_type == EH) || cie_id == UINT32_MAX || len == 0) {
476cdda23ebSPavel Labath       auto cie_sp = ParseCIE(current_entry);
477cdda23ebSPavel Labath       if (!cie_sp) {
478cdda23ebSPavel Labath         // Cannot parse, the reason is already logged
479cdda23ebSPavel Labath         m_fde_index.Clear();
480cdda23ebSPavel Labath         m_fde_index_initialized = true;
481cdda23ebSPavel Labath         return;
482cdda23ebSPavel Labath       }
483cdda23ebSPavel Labath 
484cdda23ebSPavel Labath       m_cie_map[current_entry] = std::move(cie_sp);
485cdda23ebSPavel Labath       offset = next_entry;
486cdda23ebSPavel Labath       continue;
487cdda23ebSPavel Labath     }
488cdda23ebSPavel Labath 
4893f2a0818SPavel Labath     if (m_type == DWARF)
490cdda23ebSPavel Labath       cie_offset = cie_id;
491cdda23ebSPavel Labath 
492b9c1b51eSKate Stone     if (cie_offset > m_cfi_data.GetByteSize()) {
493cdda23ebSPavel Labath       Host::SystemLog(Host::eSystemLogError,
494cdda23ebSPavel Labath                       "error: Invalid cie offset of 0x%x "
495cdda23ebSPavel Labath                       "found in cie/fde at 0x%x\n",
496b9c1b51eSKate Stone                       cie_offset, current_entry);
497e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
498c48ef341SJason Molenda       // invalid data.
499c48ef341SJason Molenda       m_fde_index.Clear();
500c48ef341SJason Molenda       m_fde_index_initialized = true;
501c48ef341SJason Molenda       return;
502c48ef341SJason Molenda     }
503c48ef341SJason Molenda 
50473bf5dbdSGreg Clayton     const CIE *cie = GetCIE(cie_offset);
505b9c1b51eSKate Stone     if (cie) {
506c9660546SGreg Clayton       const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
507fbcb7f2cSJason Molenda       const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
508fbcb7f2cSJason Molenda       const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
509fbcb7f2cSJason Molenda 
51029cb868aSZachary Turner       lldb::addr_t addr =
51129cb868aSZachary Turner           GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
51229cb868aSZachary Turner                           text_addr, data_addr);
513c7afda5aSJason Molenda       if (clear_address_zeroth_bit)
514c7afda5aSJason Molenda         addr &= ~1ull;
515c7afda5aSJason Molenda 
51629cb868aSZachary Turner       lldb::addr_t length = GetGNUEHPointer(
51729cb868aSZachary Turner           m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
51829cb868aSZachary Turner           pc_rel_addr, text_addr, data_addr);
51974303829SJason Molenda       FDEEntryMap::Entry fde(addr, length, current_entry);
52074303829SJason Molenda       m_fde_index.Append(fde);
521b9c1b51eSKate Stone     } else {
522b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError, "error: unable to find CIE at "
523b9c1b51eSKate Stone                                              "0x%8.8x for cie_id = 0x%8.8x for "
524b9c1b51eSKate Stone                                              "entry at 0x%8.8x.\n",
525b9c1b51eSKate Stone                       cie_offset, cie_id, current_entry);
52673bf5dbdSGreg Clayton     }
527fbcb7f2cSJason Molenda     offset = next_entry;
528fbcb7f2cSJason Molenda   }
52974303829SJason Molenda   m_fde_index.Sort();
530fbcb7f2cSJason Molenda   m_fde_index_initialized = true;
531fbcb7f2cSJason Molenda }
532fbcb7f2cSJason Molenda 
533b9c1b51eSKate Stone bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset,
534b9c1b51eSKate Stone                                          Address startaddr,
535b9c1b51eSKate Stone                                          UnwindPlan &unwind_plan) {
5361ee89eb8SPavel Labath   Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND);
537c7bece56SGreg Clayton   lldb::offset_t offset = dwarf_offset;
538c7bece56SGreg Clayton   lldb::offset_t current_entry = offset;
539fbcb7f2cSJason Molenda 
540d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
541fbcb7f2cSJason Molenda     return false;
542fbcb7f2cSJason Molenda 
543fbcb7f2cSJason Molenda   if (m_cfi_data_initialized == false)
544f97c5213SGreg Clayton     GetCFIData();
545fbcb7f2cSJason Molenda 
546fbcb7f2cSJason Molenda   uint32_t length = m_cfi_data.GetU32(&offset);
5471767e115STodd Fiala   dw_offset_t cie_offset;
5481767e115STodd Fiala   bool is_64bit = (length == UINT32_MAX);
5491767e115STodd Fiala   if (is_64bit) {
5501767e115STodd Fiala     length = m_cfi_data.GetU64(&offset);
5511767e115STodd Fiala     cie_offset = m_cfi_data.GetU64(&offset);
5521767e115STodd Fiala   } else {
5531767e115STodd Fiala     cie_offset = m_cfi_data.GetU32(&offset);
5541767e115STodd Fiala   }
555fbcb7f2cSJason Molenda 
556cdda23ebSPavel Labath   // FDE entries with zeroth cie_offset may occur for debug_frame.
5573f2a0818SPavel Labath   assert(!(m_type == EH && 0 == cie_offset) && cie_offset != UINT32_MAX);
55830fdc8d8SChris Lattner 
55930fdc8d8SChris Lattner   // Translate the CIE_id from the eh_frame format, which
56030fdc8d8SChris Lattner   // is relative to the FDE offset, into a __eh_frame section
56130fdc8d8SChris Lattner   // offset
5623f2a0818SPavel Labath   if (m_type == EH) {
563ab4f1924SJason Molenda     unwind_plan.SetSourceName("eh_frame CFI");
5641767e115STodd Fiala     cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
56560f0bd49SJason Molenda     unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
566b9c1b51eSKate Stone   } else {
567ab4f1924SJason Molenda     unwind_plan.SetSourceName("DWARF CFI");
56860f0bd49SJason Molenda     // In theory the debug_frame info should be valid at all call sites
56960f0bd49SJason Molenda     // ("asynchronous unwind info" as it is sometimes called) but in practice
57060f0bd49SJason Molenda     // gcc et al all emit call frame info for the prologue and call sites, but
571b9c1b51eSKate Stone     // not for the epilogue or all the other locations during the function
572b9c1b51eSKate Stone     // reliably.
57360f0bd49SJason Molenda     unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
574ab4f1924SJason Molenda   }
57560f0bd49SJason Molenda   unwind_plan.SetSourcedFromCompiler(eLazyBoolYes);
57630fdc8d8SChris Lattner 
57730fdc8d8SChris Lattner   const CIE *cie = GetCIE(cie_offset);
578d4612ad0SEd Maste   assert(cie != nullptr);
579fbcb7f2cSJason Molenda 
5801767e115STodd Fiala   const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
581fbcb7f2cSJason Molenda 
582c9660546SGreg Clayton   const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
58330fdc8d8SChris Lattner   const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
58430fdc8d8SChris Lattner   const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
58529cb868aSZachary Turner   lldb::addr_t range_base =
58629cb868aSZachary Turner       GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
587b9c1b51eSKate Stone                       text_addr, data_addr);
58829cb868aSZachary Turner   lldb::addr_t range_len = GetGNUEHPointer(
58929cb868aSZachary Turner       m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
59029cb868aSZachary Turner       pc_rel_addr, text_addr, data_addr);
591b9c1b51eSKate Stone   AddressRange range(range_base, m_objfile.GetAddressByteSize(),
592b9c1b51eSKate Stone                      m_objfile.GetSectionList());
593fbcb7f2cSJason Molenda   range.SetByteSize(range_len);
59430fdc8d8SChris Lattner 
595e9c7ecf6SJason Molenda   addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
596e9c7ecf6SJason Molenda 
597b9c1b51eSKate Stone   if (cie->augmentation[0] == 'z') {
59830fdc8d8SChris Lattner     uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
599b9c1b51eSKate Stone     if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) {
600e9c7ecf6SJason Molenda       offset_t saved_offset = offset;
60129cb868aSZachary Turner       lsda_data_file_address =
60229cb868aSZachary Turner           GetGNUEHPointer(m_cfi_data, &offset, cie->lsda_addr_encoding,
60329cb868aSZachary Turner                           pc_rel_addr, text_addr, data_addr);
604b9c1b51eSKate Stone       if (offset - saved_offset != aug_data_len) {
605e9c7ecf6SJason Molenda         // There is more in the augmentation region than we know how to process;
606e9c7ecf6SJason Molenda         // don't read anything.
607e9c7ecf6SJason Molenda         lsda_data_file_address = LLDB_INVALID_ADDRESS;
608e9c7ecf6SJason Molenda       }
609e9c7ecf6SJason Molenda       offset = saved_offset;
610e9c7ecf6SJason Molenda     }
61130fdc8d8SChris Lattner     offset += aug_data_len;
61230fdc8d8SChris Lattner   }
613e9c7ecf6SJason Molenda   Address lsda_data;
614e9c7ecf6SJason Molenda   Address personality_function_ptr;
615e9c7ecf6SJason Molenda 
616b9c1b51eSKate Stone   if (lsda_data_file_address != LLDB_INVALID_ADDRESS &&
617b9c1b51eSKate Stone       cie->personality_loc != LLDB_INVALID_ADDRESS) {
618b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address,
619b9c1b51eSKate Stone                                               lsda_data);
620b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc,
621b9c1b51eSKate Stone                                               personality_function_ptr);
622e9c7ecf6SJason Molenda   }
623e9c7ecf6SJason Molenda 
624b9c1b51eSKate Stone   if (lsda_data.IsValid() && personality_function_ptr.IsValid()) {
625e9c7ecf6SJason Molenda     unwind_plan.SetLSDAAddress(lsda_data);
626e9c7ecf6SJason Molenda     unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr);
627e9c7ecf6SJason Molenda   }
62830fdc8d8SChris Lattner 
62930fdc8d8SChris Lattner   uint32_t code_align = cie->code_align;
63030fdc8d8SChris Lattner   int32_t data_align = cie->data_align;
63130fdc8d8SChris Lattner 
632fbcb7f2cSJason Molenda   unwind_plan.SetPlanValidAddressRange(range);
6331d42c7bcSJason Molenda   UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
6341d42c7bcSJason Molenda   *cie_initial_row = cie->initial_row;
6351d42c7bcSJason Molenda   UnwindPlan::RowSP row(cie_initial_row);
63630fdc8d8SChris Lattner 
6373f2a0818SPavel Labath   unwind_plan.SetRegisterKind(GetRegisterKind());
6388eba46c6SJason Molenda   unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num);
639fbcb7f2cSJason Molenda 
640f70d6040SRichard Mitton   std::vector<UnwindPlan::RowSP> stack;
641f70d6040SRichard Mitton 
642fbcb7f2cSJason Molenda   UnwindPlan::Row::RegisterLocation reg_location;
643b9c1b51eSKate Stone   while (m_cfi_data.ValidOffset(offset) && offset < end_offset) {
64430fdc8d8SChris Lattner     uint8_t inst = m_cfi_data.GetU8(&offset);
64530fdc8d8SChris Lattner     uint8_t primary_opcode = inst & 0xC0;
64630fdc8d8SChris Lattner     uint8_t extended_opcode = inst & 0x3F;
64730fdc8d8SChris Lattner 
648b9c1b51eSKate Stone     if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align,
649b9c1b51eSKate Stone                                  offset, *row)) {
650b9c1b51eSKate Stone       if (primary_opcode) {
651b9c1b51eSKate Stone         switch (primary_opcode) {
65230fdc8d8SChris Lattner         case DW_CFA_advance_loc: // (Row Creation Instruction)
65330fdc8d8SChris Lattner         { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
65430fdc8d8SChris Lattner           // takes a single argument that represents a constant delta. The
65530fdc8d8SChris Lattner           // required action is to create a new table row with a location
65630fdc8d8SChris Lattner           // value that is computed by taking the current entry's location
65730fdc8d8SChris Lattner           // value and adding (delta * code_align). All other
65830fdc8d8SChris Lattner           // values in the new row are initially identical to the current row.
659fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
660fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
661fa67e879SJason Molenda           *newrow = *row.get();
662fa67e879SJason Molenda           row.reset(newrow);
6631d42c7bcSJason Molenda           row->SlideOffset(extended_opcode * code_align);
66430fdc8d8SChris Lattner           break;
66530fdc8d8SChris Lattner         }
66630fdc8d8SChris Lattner 
667b9c1b51eSKate Stone         case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are
668b9c1b51eSKate Stone                                // register
66930fdc8d8SChris Lattner           // takes a single argument that represents a register number. The
67030fdc8d8SChris Lattner           // required action is to change the rule for the indicated register
67130fdc8d8SChris Lattner           // to the rule assigned it by the initial_instructions in the CIE.
672623652a0STamas Berghammer           uint32_t reg_num = extended_opcode;
67330fdc8d8SChris Lattner           // We only keep enough register locations around to
67430fdc8d8SChris Lattner           // unwind what is in our thread, and these are organized
67530fdc8d8SChris Lattner           // by the register index in that state, so we need to convert our
676b9c1b51eSKate Stone           // eh_frame register number from the EH frame info, to a register
677b9c1b51eSKate Stone           // index
67830fdc8d8SChris Lattner 
679b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
680b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
681b9c1b51eSKate Stone                                                             reg_location))
6821d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
68330fdc8d8SChris Lattner           break;
68430fdc8d8SChris Lattner         }
68530fdc8d8SChris Lattner         }
686b9c1b51eSKate Stone       } else {
687b9c1b51eSKate Stone         switch (extended_opcode) {
68830fdc8d8SChris Lattner         case DW_CFA_set_loc: // 0x1 (Row Creation Instruction)
68930fdc8d8SChris Lattner         {
69030fdc8d8SChris Lattner           // DW_CFA_set_loc takes a single argument that represents an address.
69130fdc8d8SChris Lattner           // The required action is to create a new table row using the
69230fdc8d8SChris Lattner           // specified address as the location. All other values in the new row
69330fdc8d8SChris Lattner           // are initially identical to the current row. The new location value
69430fdc8d8SChris Lattner           // should always be greater than the current one.
695fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
696fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
697fa67e879SJason Molenda           *newrow = *row.get();
698fa67e879SJason Molenda           row.reset(newrow);
699b9c1b51eSKate Stone           row->SetOffset(m_cfi_data.GetPointer(&offset) -
700b9c1b51eSKate Stone                          startaddr.GetFileAddress());
70130fdc8d8SChris Lattner           break;
702623652a0STamas Berghammer         }
70330fdc8d8SChris Lattner 
70430fdc8d8SChris Lattner         case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction)
70530fdc8d8SChris Lattner         {
70630fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
70730fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
70830fdc8d8SChris Lattner           // encoding and size of the delta argument.
709fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
710fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
711fa67e879SJason Molenda           *newrow = *row.get();
712fa67e879SJason Molenda           row.reset(newrow);
7131d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align);
71430fdc8d8SChris Lattner           break;
715623652a0STamas Berghammer         }
71630fdc8d8SChris Lattner 
71730fdc8d8SChris Lattner         case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction)
71830fdc8d8SChris Lattner         {
71930fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
72030fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
72130fdc8d8SChris Lattner           // encoding and size of the delta argument.
722fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
723fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
724fa67e879SJason Molenda           *newrow = *row.get();
725fa67e879SJason Molenda           row.reset(newrow);
7261d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align);
72730fdc8d8SChris Lattner           break;
728623652a0STamas Berghammer         }
72930fdc8d8SChris Lattner 
73030fdc8d8SChris Lattner         case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction)
73130fdc8d8SChris Lattner         {
73230fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
73330fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
73430fdc8d8SChris Lattner           // encoding and size of the delta argument.
735fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
736fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
737fa67e879SJason Molenda           *newrow = *row.get();
738fa67e879SJason Molenda           row.reset(newrow);
7391d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align);
74030fdc8d8SChris Lattner           break;
74130fdc8d8SChris Lattner         }
74230fdc8d8SChris Lattner 
74330fdc8d8SChris Lattner         case DW_CFA_restore_extended: // 0x6
74430fdc8d8SChris Lattner         {
74530fdc8d8SChris Lattner           // takes a single unsigned LEB128 argument that represents a register
74630fdc8d8SChris Lattner           // number. This instruction is identical to DW_CFA_restore except for
74730fdc8d8SChris Lattner           // the encoding and size of the register argument.
748623652a0STamas Berghammer           uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
749b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
750b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
751b9c1b51eSKate Stone                                                             reg_location))
7521d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
75330fdc8d8SChris Lattner           break;
75430fdc8d8SChris Lattner         }
75530fdc8d8SChris Lattner 
75630fdc8d8SChris Lattner         case DW_CFA_remember_state: // 0xA
757fa67e879SJason Molenda         {
75830fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
75930fdc8d8SChris Lattner           // DW_CFA_remember_state instruction means to save the rules for every
76030fdc8d8SChris Lattner           // register on the current row on the stack. Encountering the
76130fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
76230fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
76330fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
76430fdc8d8SChris Lattner           // function.)
765f70d6040SRichard Mitton           stack.push_back(row);
766fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
767fa67e879SJason Molenda           *newrow = *row.get();
768fa67e879SJason Molenda           row.reset(newrow);
76930fdc8d8SChris Lattner           break;
770623652a0STamas Berghammer         }
77130fdc8d8SChris Lattner 
77230fdc8d8SChris Lattner         case DW_CFA_restore_state: // 0xB
773623652a0STamas Berghammer         {
77430fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
77530fdc8d8SChris Lattner           // DW_CFA_remember_state instruction means to save the rules for every
77630fdc8d8SChris Lattner           // register on the current row on the stack. Encountering the
77730fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
77830fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
77930fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
78030fdc8d8SChris Lattner           // function.)
781b9c1b51eSKate Stone           if (stack.empty()) {
7821ee89eb8SPavel Labath             if (log)
783b9c1b51eSKate Stone               log->Printf("DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32
784b9c1b51eSKate Stone                           ", startaddr: %" PRIx64
785b9c1b51eSKate Stone                           " encountered DW_CFA_restore_state but state stack "
786b9c1b51eSKate Stone                           "is empty. Corrupt unwind info?",
787b9c1b51eSKate Stone                           __FUNCTION__, dwarf_offset,
788b9c1b51eSKate Stone                           startaddr.GetFileAddress());
7891ee89eb8SPavel Labath             break;
7901ee89eb8SPavel Labath           }
79149dd6d15STamas Berghammer           lldb::addr_t offset = row->GetOffset();
792f70d6040SRichard Mitton           row = stack.back();
793f70d6040SRichard Mitton           stack.pop_back();
79449dd6d15STamas Berghammer           row->SetOffset(offset);
79530fdc8d8SChris Lattner           break;
796623652a0STamas Berghammer         }
797623652a0STamas Berghammer 
7981ee89eb8SPavel Labath         case DW_CFA_GNU_args_size: // 0x2e
7991ee89eb8SPavel Labath         {
800b9c1b51eSKate Stone           // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128
801b9c1b51eSKate Stone           // operand
802b9c1b51eSKate Stone           // representing an argument size. This instruction specifies the total
803b9c1b51eSKate Stone           // of
8041ee89eb8SPavel Labath           // the size of the arguments which have been pushed onto the stack.
8051ee89eb8SPavel Labath 
8061ee89eb8SPavel Labath           // TODO: Figure out how we should handle this.
8071ee89eb8SPavel Labath           m_cfi_data.GetULEB128(&offset);
808a41deb1cSJason Molenda           break;
8091ee89eb8SPavel Labath         }
8101ee89eb8SPavel Labath 
811623652a0STamas Berghammer         case DW_CFA_val_offset:    // 0x14
812623652a0STamas Berghammer         case DW_CFA_val_offset_sf: // 0x15
813623652a0STamas Berghammer         default:
814623652a0STamas Berghammer           break;
815623652a0STamas Berghammer         }
816623652a0STamas Berghammer       }
817623652a0STamas Berghammer     }
818623652a0STamas Berghammer   }
819623652a0STamas Berghammer   unwind_plan.AppendRow(row);
820623652a0STamas Berghammer 
821623652a0STamas Berghammer   return true;
822623652a0STamas Berghammer }
823623652a0STamas Berghammer 
824b9c1b51eSKate Stone bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
825623652a0STamas Berghammer                                                  uint8_t extended_opcode,
826623652a0STamas Berghammer                                                  int32_t data_align,
827623652a0STamas Berghammer                                                  lldb::offset_t &offset,
828b9c1b51eSKate Stone                                                  UnwindPlan::Row &row) {
829623652a0STamas Berghammer   UnwindPlan::Row::RegisterLocation reg_location;
830623652a0STamas Berghammer 
831b9c1b51eSKate Stone   if (primary_opcode) {
832b9c1b51eSKate Stone     switch (primary_opcode) {
833b9c1b51eSKate Stone     case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are
834b9c1b51eSKate Stone                           // register
835623652a0STamas Berghammer       // takes two arguments: an unsigned LEB128 constant representing a
836623652a0STamas Berghammer       // factored offset and a register number. The required action is to
837623652a0STamas Berghammer       // change the rule for the register indicated by the register number
838623652a0STamas Berghammer       // to be an offset(N) rule with a value of
839623652a0STamas Berghammer       // (N = factored offset * data_align).
840623652a0STamas Berghammer       uint8_t reg_num = extended_opcode;
841623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
842623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
843623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
844623652a0STamas Berghammer       return true;
845623652a0STamas Berghammer     }
846623652a0STamas Berghammer     }
847b9c1b51eSKate Stone   } else {
848b9c1b51eSKate Stone     switch (extended_opcode) {
849623652a0STamas Berghammer     case DW_CFA_nop: // 0x0
850623652a0STamas Berghammer       return true;
851623652a0STamas Berghammer 
852623652a0STamas Berghammer     case DW_CFA_offset_extended: // 0x5
853623652a0STamas Berghammer     {
854623652a0STamas Berghammer       // takes two unsigned LEB128 arguments representing a register number
855623652a0STamas Berghammer       // and a factored offset. This instruction is identical to DW_CFA_offset
856623652a0STamas Berghammer       // except for the encoding and size of the register argument.
857623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
858623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
859623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
860623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
861623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
862623652a0STamas Berghammer       return true;
863623652a0STamas Berghammer     }
864623652a0STamas Berghammer 
865623652a0STamas Berghammer     case DW_CFA_undefined: // 0x7
866623652a0STamas Berghammer     {
867623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
868623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
869623652a0STamas Berghammer       // register to undefined.
870623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
871623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
872623652a0STamas Berghammer       reg_location.SetUndefined();
873623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
874623652a0STamas Berghammer       return true;
875623652a0STamas Berghammer     }
876623652a0STamas Berghammer 
877623652a0STamas Berghammer     case DW_CFA_same_value: // 0x8
878623652a0STamas Berghammer     {
879623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
880623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
881623652a0STamas Berghammer       // register to same value.
882623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
883623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
884623652a0STamas Berghammer       reg_location.SetSame();
885623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
886623652a0STamas Berghammer       return true;
887623652a0STamas Berghammer     }
888623652a0STamas Berghammer 
889623652a0STamas Berghammer     case DW_CFA_register: // 0x9
890623652a0STamas Berghammer     {
891623652a0STamas Berghammer       // takes two unsigned LEB128 arguments representing register numbers.
892623652a0STamas Berghammer       // The required action is to set the rule for the first register to be
893623652a0STamas Berghammer       // the second register.
894623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
895623652a0STamas Berghammer       uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
896623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
897623652a0STamas Berghammer       reg_location.SetInRegister(other_reg_num);
898623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
899623652a0STamas Berghammer       return true;
900623652a0STamas Berghammer     }
90130fdc8d8SChris Lattner 
90230fdc8d8SChris Lattner     case DW_CFA_def_cfa: // 0xC    (CFA Definition Instruction)
90330fdc8d8SChris Lattner     {
90430fdc8d8SChris Lattner       // Takes two unsigned LEB128 operands representing a register
90530fdc8d8SChris Lattner       // number and a (non-factored) offset. The required action
90630fdc8d8SChris Lattner       // is to define the current CFA rule to use the provided
90730fdc8d8SChris Lattner       // register and offset.
908623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
909623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
910623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
911623652a0STamas Berghammer       return true;
91230fdc8d8SChris Lattner     }
91330fdc8d8SChris Lattner 
91430fdc8d8SChris Lattner     case DW_CFA_def_cfa_register: // 0xD    (CFA Definition Instruction)
91530fdc8d8SChris Lattner     {
91630fdc8d8SChris Lattner       // takes a single unsigned LEB128 argument representing a register
91730fdc8d8SChris Lattner       // number. The required action is to define the current CFA rule to
91830fdc8d8SChris Lattner       // use the provided register (but to keep the old offset).
919623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
920b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num,
921b9c1b51eSKate Stone                                                 row.GetCFAValue().GetOffset());
922623652a0STamas Berghammer       return true;
92330fdc8d8SChris Lattner     }
92430fdc8d8SChris Lattner 
92530fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset: // 0xE    (CFA Definition Instruction)
92630fdc8d8SChris Lattner     {
92730fdc8d8SChris Lattner       // Takes a single unsigned LEB128 operand representing a
92830fdc8d8SChris Lattner       // (non-factored) offset. The required action is to define
92930fdc8d8SChris Lattner       // the current CFA rule to use the provided offset (but
93030fdc8d8SChris Lattner       // to keep the old register).
931623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
932b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(
933b9c1b51eSKate Stone           row.GetCFAValue().GetRegisterNumber(), op_offset);
934623652a0STamas Berghammer       return true;
93530fdc8d8SChris Lattner     }
93630fdc8d8SChris Lattner 
93730fdc8d8SChris Lattner     case DW_CFA_def_cfa_expression: // 0xF    (CFA Definition Instruction)
93830fdc8d8SChris Lattner     {
93930fdc8d8SChris Lattner       size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
940b9c1b51eSKate Stone       const uint8_t *block_data =
941b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
942623652a0STamas Berghammer       row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
943623652a0STamas Berghammer       return true;
94430fdc8d8SChris Lattner     }
94530fdc8d8SChris Lattner 
94630fdc8d8SChris Lattner     case DW_CFA_expression: // 0x10
94730fdc8d8SChris Lattner     {
94830fdc8d8SChris Lattner       // Takes two operands: an unsigned LEB128 value representing
94930fdc8d8SChris Lattner       // a register number, and a DW_FORM_block value representing a DWARF
95030fdc8d8SChris Lattner       // expression. The required action is to change the rule for the
95130fdc8d8SChris Lattner       // register indicated by the register number to be an expression(E)
95230fdc8d8SChris Lattner       // rule where E is the DWARF expression. That is, the DWARF
95330fdc8d8SChris Lattner       // expression computes the address. The value of the CFA is
95430fdc8d8SChris Lattner       // pushed on the DWARF evaluation stack prior to execution of
95530fdc8d8SChris Lattner       // the DWARF expression.
956623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
95730fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
958b9c1b51eSKate Stone       const uint8_t *block_data =
959b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
960623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
96130fdc8d8SChris Lattner       reg_location.SetAtDWARFExpression(block_data, block_len);
962623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
963623652a0STamas Berghammer       return true;
96430fdc8d8SChris Lattner     }
96530fdc8d8SChris Lattner 
96630fdc8d8SChris Lattner     case DW_CFA_offset_extended_sf: // 0x11
96730fdc8d8SChris Lattner     {
96830fdc8d8SChris Lattner       // takes two operands: an unsigned LEB128 value representing a
96930fdc8d8SChris Lattner       // register number and a signed LEB128 factored offset. This
97030fdc8d8SChris Lattner       // instruction is identical to DW_CFA_offset_extended except
97130fdc8d8SChris Lattner       // that the second operand is signed and factored.
972623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
973623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
974623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
97530fdc8d8SChris Lattner       reg_location.SetAtCFAPlusOffset(op_offset);
976623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
977623652a0STamas Berghammer       return true;
97830fdc8d8SChris Lattner     }
97930fdc8d8SChris Lattner 
98030fdc8d8SChris Lattner     case DW_CFA_def_cfa_sf: // 0x12   (CFA Definition Instruction)
98130fdc8d8SChris Lattner     {
98230fdc8d8SChris Lattner       // Takes two operands: an unsigned LEB128 value representing
98330fdc8d8SChris Lattner       // a register number and a signed LEB128 factored offset.
98430fdc8d8SChris Lattner       // This instruction is identical to DW_CFA_def_cfa except
98530fdc8d8SChris Lattner       // that the second operand is signed and factored.
986623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
987623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
988623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
989623652a0STamas Berghammer       return true;
99030fdc8d8SChris Lattner     }
99130fdc8d8SChris Lattner 
99230fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset_sf: // 0x13   (CFA Definition Instruction)
99330fdc8d8SChris Lattner     {
99430fdc8d8SChris Lattner       // takes a signed LEB128 operand representing a factored
99530fdc8d8SChris Lattner       // offset. This instruction is identical to  DW_CFA_def_cfa_offset
99630fdc8d8SChris Lattner       // except that the operand is signed and factored.
997623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
998623652a0STamas Berghammer       uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
999623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
1000623652a0STamas Berghammer       return true;
100130fdc8d8SChris Lattner     }
100230fdc8d8SChris Lattner 
100330fdc8d8SChris Lattner     case DW_CFA_val_expression: // 0x16
100430fdc8d8SChris Lattner     {
100530fdc8d8SChris Lattner       // takes two operands: an unsigned LEB128 value representing a register
100630fdc8d8SChris Lattner       // number, and a DW_FORM_block value representing a DWARF expression.
100730fdc8d8SChris Lattner       // The required action is to change the rule for the register indicated
100830fdc8d8SChris Lattner       // by the register number to be a val_expression(E) rule where E is the
100930fdc8d8SChris Lattner       // DWARF expression. That is, the DWARF expression computes the value of
101030fdc8d8SChris Lattner       // the given register. The value of the CFA is pushed on the DWARF
101130fdc8d8SChris Lattner       // evaluation stack prior to execution of the DWARF expression.
1012623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
101330fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
1014b9c1b51eSKate Stone       const uint8_t *block_data =
1015b9c1b51eSKate Stone           (const uint8_t *)m_cfi_data.GetData(&offset, block_len);
101630fdc8d8SChris Lattner       //#if defined(__i386__) || defined(__x86_64__)
1017b9c1b51eSKate Stone       //              // The EH frame info for EIP and RIP contains code that
1018b9c1b51eSKate Stone       //              looks for traps to
101930fdc8d8SChris Lattner       //              // be a specific type and increments the PC.
102030fdc8d8SChris Lattner       //              // For i386:
102130fdc8d8SChris Lattner       //              // DW_CFA_val_expression where:
1022b9c1b51eSKate Stone       //              // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup,
1023b9c1b51eSKate Stone       //              DW_OP_plus_uconst(0x34),
1024b9c1b51eSKate Stone       //              //       DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0),
1025b9c1b51eSKate Stone       //              DW_OP_deref,
1026b9c1b51eSKate Stone       //              //       DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap,
1027b9c1b51eSKate Stone       //              DW_OP_lit4, DW_OP_ne,
102830fdc8d8SChris Lattner       //              //       DW_OP_and, DW_OP_plus
102930fdc8d8SChris Lattner       //              // This basically does a:
103030fdc8d8SChris Lattner       //              // eip = ucontenxt.mcontext32->gpr.eip;
1031b9c1b51eSKate Stone       //              // if (ucontenxt.mcontext32->exc.trapno != 3 &&
1032b9c1b51eSKate Stone       //              ucontenxt.mcontext32->exc.trapno != 4)
103330fdc8d8SChris Lattner       //              //   eip++;
103430fdc8d8SChris Lattner       //              //
103530fdc8d8SChris Lattner       //              // For x86_64:
103630fdc8d8SChris Lattner       //              // DW_CFA_val_expression where:
1037b9c1b51eSKate Stone       //              // rip =  DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup,
1038b9c1b51eSKate Stone       //              DW_OP_plus_uconst(0x90), DW_OP_deref,
1039b9c1b51eSKate Stone       //              //          DW_OP_swap, DW_OP_plus_uconst(0),
1040b9c1b51eSKate Stone       //              DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
1041b9c1b51eSKate Stone       //              //          DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
1042b9c1b51eSKate Stone       //              DW_OP_and, DW_OP_plus
104330fdc8d8SChris Lattner       //              // This basically does a:
104430fdc8d8SChris Lattner       //              // rip = ucontenxt.mcontext64->gpr.rip;
1045b9c1b51eSKate Stone       //              // if (ucontenxt.mcontext64->exc.trapno != 3 &&
1046b9c1b51eSKate Stone       //              ucontenxt.mcontext64->exc.trapno != 4)
104730fdc8d8SChris Lattner       //              //   rip++;
1048b9c1b51eSKate Stone       //              // The trap comparisons and increments are not needed as
1049b9c1b51eSKate Stone       //              it hoses up the unwound PC which
1050b9c1b51eSKate Stone       //              // is expected to point at least past the instruction that
1051b9c1b51eSKate Stone       //              causes the fault/trap. So we
1052b9c1b51eSKate Stone       //              // take it out by trimming the expression right at the
1053b9c1b51eSKate Stone       //              first "DW_OP_swap" opcodes
1054b9c1b51eSKate Stone       //              if (block_data != NULL && thread->GetPCRegNum(Thread::GCC)
1055b9c1b51eSKate Stone       //              == reg_num)
105630fdc8d8SChris Lattner       //              {
105730fdc8d8SChris Lattner       //                  if (thread->Is64Bit())
105830fdc8d8SChris Lattner       //                  {
1059b9c1b51eSKate Stone       //                      if (block_len > 9 && block_data[8] == DW_OP_swap
1060b9c1b51eSKate Stone       //                      && block_data[9] == DW_OP_plus_uconst)
106130fdc8d8SChris Lattner       //                          block_len = 8;
106230fdc8d8SChris Lattner       //                  }
106330fdc8d8SChris Lattner       //                  else
106430fdc8d8SChris Lattner       //                  {
1065b9c1b51eSKate Stone       //                      if (block_len > 8 && block_data[7] == DW_OP_swap
1066b9c1b51eSKate Stone       //                      && block_data[8] == DW_OP_plus_uconst)
106730fdc8d8SChris Lattner       //                          block_len = 7;
106830fdc8d8SChris Lattner       //                  }
106930fdc8d8SChris Lattner       //              }
107030fdc8d8SChris Lattner       //#endif
107130fdc8d8SChris Lattner       reg_location.SetIsDWARFExpression(block_data, block_len);
1072623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
1073fbcb7f2cSJason Molenda       return true;
107430fdc8d8SChris Lattner     }
1075623652a0STamas Berghammer     }
1076623652a0STamas Berghammer   }
1077623652a0STamas Berghammer   return false;
1078623652a0STamas Berghammer }
10796b63b148STamas Berghammer 
1080b9c1b51eSKate Stone void DWARFCallFrameInfo::ForEachFDEEntries(
1081b9c1b51eSKate Stone     const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) {
10826b63b148STamas Berghammer   GetFDEIndex();
10836b63b148STamas Berghammer 
1084b9c1b51eSKate Stone   for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) {
10856b63b148STamas Berghammer     const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i);
10866b63b148STamas Berghammer     if (!callback(entry.base, entry.size, entry.data))
10876b63b148STamas Berghammer       break;
10886b63b148STamas Berghammer   }
10896b63b148STamas Berghammer }
1090