130fdc8d8SChris Lattner //===-- DWARFCallFrameInfo.cpp ----------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
95f19b907SPavel Labath #include "lldb/Symbol/DWARFCallFrameInfo.h"
1030fdc8d8SChris Lattner #include "lldb/Core/Module.h"
1130fdc8d8SChris Lattner #include "lldb/Core/Section.h"
1229cb868aSZachary Turner #include "lldb/Core/dwarf.h"
13e38a5eddSGreg Clayton #include "lldb/Host/Host.h"
14e38a5eddSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
15fbcb7f2cSJason Molenda #include "lldb/Symbol/UnwindPlan.h"
16e38a5eddSGreg Clayton #include "lldb/Target/RegisterContext.h"
17e38a5eddSGreg Clayton #include "lldb/Target/Thread.h"
185f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
196f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2038d0632eSPavel Labath #include "lldb/Utility/Timer.h"
215f19b907SPavel Labath #include <list>
2230fdc8d8SChris Lattner 
2330fdc8d8SChris Lattner using namespace lldb;
2430fdc8d8SChris Lattner using namespace lldb_private;
2530fdc8d8SChris Lattner 
2629cb868aSZachary Turner // GetDwarfEHPtr
2729cb868aSZachary Turner //
2805097246SAdrian Prantl // Used for calls when the value type is specified by a DWARF EH Frame pointer
2905097246SAdrian Prantl // encoding.
3029cb868aSZachary Turner static uint64_t
3129cb868aSZachary Turner GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr,
3229cb868aSZachary Turner                 uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr,
3329cb868aSZachary Turner                 addr_t data_addr) //, BSDRelocs *data_relocs) const
3429cb868aSZachary Turner {
3529cb868aSZachary Turner   if (eh_ptr_enc == DW_EH_PE_omit)
3629cb868aSZachary Turner     return ULLONG_MAX; // Value isn't in the buffer...
3729cb868aSZachary Turner 
3829cb868aSZachary Turner   uint64_t baseAddress = 0;
3929cb868aSZachary Turner   uint64_t addressValue = 0;
4029cb868aSZachary Turner   const uint32_t addr_size = DE.GetAddressByteSize();
4129cb868aSZachary Turner   assert(addr_size == 4 || addr_size == 8);
4229cb868aSZachary Turner 
4329cb868aSZachary Turner   bool signExtendValue = false;
4429cb868aSZachary Turner   // Decode the base part or adjust our offset
4529cb868aSZachary Turner   switch (eh_ptr_enc & 0x70) {
4629cb868aSZachary Turner   case DW_EH_PE_pcrel:
4729cb868aSZachary Turner     signExtendValue = true;
4829cb868aSZachary Turner     baseAddress = *offset_ptr;
4929cb868aSZachary Turner     if (pc_rel_addr != LLDB_INVALID_ADDRESS)
5029cb868aSZachary Turner       baseAddress += pc_rel_addr;
5129cb868aSZachary Turner     //      else
5229cb868aSZachary Turner     //          Log::GlobalWarning ("PC relative pointer encoding found with
5329cb868aSZachary Turner     //          invalid pc relative address.");
5429cb868aSZachary Turner     break;
5529cb868aSZachary Turner 
5629cb868aSZachary Turner   case DW_EH_PE_textrel:
5729cb868aSZachary Turner     signExtendValue = true;
5829cb868aSZachary Turner     if (text_addr != LLDB_INVALID_ADDRESS)
5929cb868aSZachary Turner       baseAddress = text_addr;
6029cb868aSZachary Turner     //      else
6129cb868aSZachary Turner     //          Log::GlobalWarning ("text relative pointer encoding being
6229cb868aSZachary Turner     //          decoded with invalid text section address, setting base address
6329cb868aSZachary Turner     //          to zero.");
6429cb868aSZachary Turner     break;
6529cb868aSZachary Turner 
6629cb868aSZachary Turner   case DW_EH_PE_datarel:
6729cb868aSZachary Turner     signExtendValue = true;
6829cb868aSZachary Turner     if (data_addr != LLDB_INVALID_ADDRESS)
6929cb868aSZachary Turner       baseAddress = data_addr;
7029cb868aSZachary Turner     //      else
7129cb868aSZachary Turner     //          Log::GlobalWarning ("data relative pointer encoding being
7229cb868aSZachary Turner     //          decoded with invalid data section address, setting base address
7329cb868aSZachary Turner     //          to zero.");
7429cb868aSZachary Turner     break;
7529cb868aSZachary Turner 
7629cb868aSZachary Turner   case DW_EH_PE_funcrel:
7729cb868aSZachary Turner     signExtendValue = true;
7829cb868aSZachary Turner     break;
7929cb868aSZachary Turner 
8029cb868aSZachary Turner   case DW_EH_PE_aligned: {
8105097246SAdrian Prantl     // SetPointerSize should be called prior to extracting these so the pointer
8205097246SAdrian Prantl     // size is cached
8329cb868aSZachary Turner     assert(addr_size != 0);
8429cb868aSZachary Turner     if (addr_size) {
8529cb868aSZachary Turner       // Align to a address size boundary first
8629cb868aSZachary Turner       uint32_t alignOffset = *offset_ptr % addr_size;
8729cb868aSZachary Turner       if (alignOffset)
8829cb868aSZachary Turner         offset_ptr += addr_size - alignOffset;
8929cb868aSZachary Turner     }
9029cb868aSZachary Turner   } break;
9129cb868aSZachary Turner 
9229cb868aSZachary Turner   default:
9329cb868aSZachary Turner     break;
9429cb868aSZachary Turner   }
9529cb868aSZachary Turner 
9629cb868aSZachary Turner   // Decode the value part
9729cb868aSZachary Turner   switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) {
9829cb868aSZachary Turner   case DW_EH_PE_absptr: {
9929cb868aSZachary Turner     addressValue = DE.GetAddress(offset_ptr);
10029cb868aSZachary Turner     //          if (data_relocs)
10129cb868aSZachary Turner     //              addressValue = data_relocs->Relocate(*offset_ptr -
10229cb868aSZachary Turner     //              addr_size, *this, addressValue);
10329cb868aSZachary Turner   } break;
10429cb868aSZachary Turner   case DW_EH_PE_uleb128:
10529cb868aSZachary Turner     addressValue = DE.GetULEB128(offset_ptr);
10629cb868aSZachary Turner     break;
10729cb868aSZachary Turner   case DW_EH_PE_udata2:
10829cb868aSZachary Turner     addressValue = DE.GetU16(offset_ptr);
10929cb868aSZachary Turner     break;
11029cb868aSZachary Turner   case DW_EH_PE_udata4:
11129cb868aSZachary Turner     addressValue = DE.GetU32(offset_ptr);
11229cb868aSZachary Turner     break;
11329cb868aSZachary Turner   case DW_EH_PE_udata8:
11429cb868aSZachary Turner     addressValue = DE.GetU64(offset_ptr);
11529cb868aSZachary Turner     break;
11629cb868aSZachary Turner   case DW_EH_PE_sleb128:
11729cb868aSZachary Turner     addressValue = DE.GetSLEB128(offset_ptr);
11829cb868aSZachary Turner     break;
11929cb868aSZachary Turner   case DW_EH_PE_sdata2:
12029cb868aSZachary Turner     addressValue = (int16_t)DE.GetU16(offset_ptr);
12129cb868aSZachary Turner     break;
12229cb868aSZachary Turner   case DW_EH_PE_sdata4:
12329cb868aSZachary Turner     addressValue = (int32_t)DE.GetU32(offset_ptr);
12429cb868aSZachary Turner     break;
12529cb868aSZachary Turner   case DW_EH_PE_sdata8:
12629cb868aSZachary Turner     addressValue = (int64_t)DE.GetU64(offset_ptr);
12729cb868aSZachary Turner     break;
12829cb868aSZachary Turner   default:
12929cb868aSZachary Turner     // Unhandled encoding type
13029cb868aSZachary Turner     assert(eh_ptr_enc);
13129cb868aSZachary Turner     break;
13229cb868aSZachary Turner   }
13329cb868aSZachary Turner 
13429cb868aSZachary Turner   // Since we promote everything to 64 bit, we may need to sign extend
13529cb868aSZachary Turner   if (signExtendValue && addr_size < sizeof(baseAddress)) {
13629cb868aSZachary Turner     uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
13729cb868aSZachary Turner     if (sign_bit & addressValue) {
13829cb868aSZachary Turner       uint64_t mask = ~sign_bit + 1;
13929cb868aSZachary Turner       addressValue |= mask;
14029cb868aSZachary Turner     }
14129cb868aSZachary Turner   }
14229cb868aSZachary Turner   return baseAddress + addressValue;
14329cb868aSZachary Turner }
14429cb868aSZachary Turner 
145b9c1b51eSKate Stone DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile &objfile,
1463f2a0818SPavel Labath                                        SectionSP &section_sp, Type type)
1473f2a0818SPavel Labath     : m_objfile(objfile), m_section_sp(section_sp), m_type(type) {}
14830fdc8d8SChris Lattner 
149*23594291SPavel Labath bool DWARFCallFrameInfo::GetUnwindPlan(const Address &addr,
150*23594291SPavel Labath                                        UnwindPlan &unwind_plan) {
151*23594291SPavel Labath   return GetUnwindPlan(AddressRange(addr, 1), unwind_plan);
152*23594291SPavel Labath }
153*23594291SPavel Labath 
154*23594291SPavel Labath bool DWARFCallFrameInfo::GetUnwindPlan(const AddressRange &range,
155*23594291SPavel Labath                                        UnwindPlan &unwind_plan) {
15674303829SJason Molenda   FDEEntryMap::Entry fde_entry;
157*23594291SPavel Labath   Address addr = range.GetBaseAddress();
15874303829SJason Molenda 
15905097246SAdrian Prantl   // Make sure that the Address we're searching for is the same object file as
16005097246SAdrian Prantl   // this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
16174303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
162b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
163b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
164fbcb7f2cSJason Molenda     return false;
16574303829SJason Molenda 
166*23594291SPavel Labath   if (llvm::Optional<FDEEntryMap::Entry> entry = GetFirstFDEEntryInRange(range))
167*23594291SPavel Labath     return FDEToUnwindPlan(entry->data, addr, unwind_plan);
16874303829SJason Molenda   return false;
16930fdc8d8SChris Lattner }
17030fdc8d8SChris Lattner 
171b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) {
17274303829SJason Molenda 
17305097246SAdrian Prantl   // Make sure that the Address we're searching for is the same object file as
17405097246SAdrian Prantl   // this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
17574303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
176b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
177b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
17874303829SJason Molenda     return false;
17974303829SJason Molenda 
180d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
181fbcb7f2cSJason Molenda     return false;
182fbcb7f2cSJason Molenda   GetFDEIndex();
183b9c1b51eSKate Stone   FDEEntryMap::Entry *fde_entry =
184b9c1b51eSKate Stone       m_fde_index.FindEntryThatContains(addr.GetFileAddress());
18574303829SJason Molenda   if (!fde_entry)
186fbcb7f2cSJason Molenda     return false;
187fbcb7f2cSJason Molenda 
188b9c1b51eSKate Stone   range = AddressRange(fde_entry->base, fde_entry->size,
189b9c1b51eSKate Stone                        m_objfile.GetSectionList());
190fbcb7f2cSJason Molenda   return true;
19130fdc8d8SChris Lattner }
19230fdc8d8SChris Lattner 
193*23594291SPavel Labath llvm::Optional<DWARFCallFrameInfo::FDEEntryMap::Entry>
194*23594291SPavel Labath DWARFCallFrameInfo::GetFirstFDEEntryInRange(const AddressRange &range) {
195*23594291SPavel Labath   if (!m_section_sp || m_section_sp->IsEncrypted())
196*23594291SPavel Labath     return llvm::None;
19774303829SJason Molenda 
19874303829SJason Molenda   GetFDEIndex();
19974303829SJason Molenda 
200*23594291SPavel Labath   addr_t start_file_addr = range.GetBaseAddress().GetFileAddress();
201*23594291SPavel Labath   const FDEEntryMap::Entry *fde =
202*23594291SPavel Labath       m_fde_index.FindEntryThatContainsOrFollows(start_file_addr);
203*23594291SPavel Labath   if (fde && fde->DoesIntersect(
204*23594291SPavel Labath                  FDEEntryMap::Range(start_file_addr, range.GetByteSize())))
205*23594291SPavel Labath     return *fde;
20674303829SJason Molenda 
207*23594291SPavel Labath   return llvm::None;
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
23470355aceSJonas Devlieghere     if (pos->second == 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;
246a6682a41SJonas Devlieghere   if (!m_cfi_data_initialized)
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 
29405097246SAdrian Prantl     // m_cfi_data uses address size from target architecture of the process may
29505097246SAdrian Prantl     // 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]) {
31005097246SAdrian Prantl       // Get the length of the eh_frame augmentation data which starts with a
31105097246SAdrian Prantl       // 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.
31605097246SAdrian Prantl       // If present, the Augmentation Data field shall be present. The contents
31705097246SAdrian Prantl       // of the Augmentation Data shall be interpreted according to other
31805097246SAdrian Prantl       // 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':
32605097246SAdrian Prantl             // Indicates the presence of one argument in the Augmentation Data
32705097246SAdrian Prantl             // of the CIE, and a corresponding argument in the Augmentation
32805097246SAdrian Prantl             // Data of the FDE. The argument in the Augmentation Data of the
32905097246SAdrian Prantl             // CIE is 1-byte and represents the pointer encoding used for the
33005097246SAdrian Prantl             // argument in the Augmentation Data of the FDE, which is the
33105097246SAdrian Prantl             // address of a language-specific data area (LSDA). The size of the
33205097246SAdrian Prantl             // LSDA pointer is specified by the pointer encoding used.
333e9c7ecf6SJason Molenda             cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
33430fdc8d8SChris Lattner             break;
33530fdc8d8SChris Lattner 
33630fdc8d8SChris Lattner           case 'P':
33705097246SAdrian Prantl             // Indicates the presence of two arguments in the Augmentation Data
33805097246SAdrian Prantl             // of the CIE. The first argument is 1-byte and represents the
33905097246SAdrian Prantl             // pointer encoding used for the second argument, which is the
34005097246SAdrian Prantl             // address of a personality routine handler. The size of the
34105097246SAdrian Prantl             // personality routine pointer is specified by the pointer encoding
34205097246SAdrian Prantl             // used.
343e9c7ecf6SJason Molenda             //
34405097246SAdrian Prantl             // The address of the personality function will be stored at this
34505097246SAdrian Prantl             // location.  Pre-execution, it will be all zero's so don't read it
34605097246SAdrian Prantl             // until we're trying to do an unwind & the reloc has been
347e9c7ecf6SJason Molenda             // resolved.
34830fdc8d8SChris Lattner             {
34930fdc8d8SChris Lattner               uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
350e9c7ecf6SJason Molenda               const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
35129cb868aSZachary Turner               cie_sp->personality_loc = GetGNUEHPointer(
35229cb868aSZachary Turner                   m_cfi_data, &offset, arg_ptr_encoding, pc_rel_addr,
35329cb868aSZachary Turner                   LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
35430fdc8d8SChris Lattner             }
35530fdc8d8SChris Lattner             break;
35630fdc8d8SChris Lattner 
35730fdc8d8SChris Lattner           case 'R':
35830fdc8d8SChris Lattner             // A 'R' may be present at any position after the
35905097246SAdrian Prantl             // first character of the string. The Augmentation Data shall
36005097246SAdrian Prantl             // include a 1 byte argument that represents the pointer encoding
36105097246SAdrian Prantl             // for the address pointers used in the FDE. Example: 0x1B ==
36205097246SAdrian Prantl             // DW_EH_PE_pcrel | DW_EH_PE_sdata4
36330fdc8d8SChris Lattner             cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
36430fdc8d8SChris Lattner             break;
36530fdc8d8SChris Lattner           }
36630fdc8d8SChris Lattner         }
367b9c1b51eSKate Stone       } else if (strcmp(cie_sp->augmentation, "eh") == 0) {
36805097246SAdrian Prantl         // If the Augmentation string has the value "eh", then the EH Data
36905097246SAdrian Prantl         // field shall be present
37030fdc8d8SChris Lattner       }
37130fdc8d8SChris Lattner 
37205097246SAdrian Prantl       // Set the offset to be the end of the augmentation data just in case we
37305097246SAdrian Prantl       // didn't understand any of the data.
37430fdc8d8SChris Lattner       offset = (uint32_t)aug_data_end;
37530fdc8d8SChris Lattner     }
37630fdc8d8SChris Lattner 
377b9c1b51eSKate Stone     if (end_offset > offset) {
37830fdc8d8SChris Lattner       cie_sp->inst_offset = offset;
37930fdc8d8SChris Lattner       cie_sp->inst_length = end_offset - offset;
38030fdc8d8SChris Lattner     }
381b9c1b51eSKate Stone     while (offset < end_offset) {
382fbcb7f2cSJason Molenda       uint8_t inst = m_cfi_data.GetU8(&offset);
383fbcb7f2cSJason Molenda       uint8_t primary_opcode = inst & 0xC0;
384fbcb7f2cSJason Molenda       uint8_t extended_opcode = inst & 0x3F;
385fbcb7f2cSJason Molenda 
386b9c1b51eSKate Stone       if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode,
387b9c1b51eSKate Stone                                    cie_sp->data_align, offset,
388b9c1b51eSKate Stone                                    cie_sp->initial_row))
389fbcb7f2cSJason Molenda         break; // Stop if we hit an unrecognized opcode
390fbcb7f2cSJason Molenda     }
39130fdc8d8SChris Lattner   }
39230fdc8d8SChris Lattner 
39330fdc8d8SChris Lattner   return cie_sp;
39430fdc8d8SChris Lattner }
39530fdc8d8SChris Lattner 
396b9c1b51eSKate Stone void DWARFCallFrameInfo::GetCFIData() {
397a6682a41SJonas Devlieghere   if (!m_cfi_data_initialized) {
3985160ce5cSGreg Clayton     Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
399f97c5213SGreg Clayton     if (log)
4005160ce5cSGreg Clayton       m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
401c9660546SGreg Clayton     m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data);
402f97c5213SGreg Clayton     m_cfi_data_initialized = true;
403f97c5213SGreg Clayton   }
404f97c5213SGreg Clayton }
405b9c1b51eSKate Stone // Scan through the eh_frame or debug_frame section looking for FDEs and noting
40605097246SAdrian Prantl // the start/end addresses of the functions and a pointer back to the
40705097246SAdrian Prantl // function's FDE for later expansion. Internalize CIEs as we come across them.
40830fdc8d8SChris Lattner 
409b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFDEIndex() {
410d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
411fbcb7f2cSJason Molenda     return;
4127d69f12eSSean Callanan 
413fbcb7f2cSJason Molenda   if (m_fde_index_initialized)
414fbcb7f2cSJason Molenda     return;
415fbcb7f2cSJason Molenda 
416bb19a13cSSaleem Abdulrasool   std::lock_guard<std::mutex> guard(m_fde_index_mutex);
4177d69f12eSSean Callanan 
4187d69f12eSSean Callanan   if (m_fde_index_initialized) // if two threads hit the locker
4197d69f12eSSean Callanan     return;
420e6194f17SJason Molenda 
421f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
422f9d16476SPavel Labath   Timer scoped_timer(func_cat, "%s - %s", LLVM_PRETTY_FUNCTION,
423b9c1b51eSKate Stone                      m_objfile.GetFileSpec().GetFilename().AsCString(""));
42474303829SJason Molenda 
425c7afda5aSJason Molenda   bool clear_address_zeroth_bit = false;
426f760f5aeSPavel Labath   if (ArchSpec arch = m_objfile.GetArchitecture()) {
427b9c1b51eSKate Stone     if (arch.GetTriple().getArch() == llvm::Triple::arm ||
428b9c1b51eSKate Stone         arch.GetTriple().getArch() == llvm::Triple::thumb)
429c7afda5aSJason Molenda       clear_address_zeroth_bit = true;
430c7afda5aSJason Molenda   }
431c7afda5aSJason Molenda 
432c7bece56SGreg Clayton   lldb::offset_t offset = 0;
433a6682a41SJonas Devlieghere   if (!m_cfi_data_initialized)
434f97c5213SGreg Clayton     GetCFIData();
435b9c1b51eSKate Stone   while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) {
43673bf5dbdSGreg Clayton     const dw_offset_t current_entry = offset;
4371767e115STodd Fiala     dw_offset_t cie_id, next_entry, cie_offset;
438fbcb7f2cSJason Molenda     uint32_t len = m_cfi_data.GetU32(&offset);
4391767e115STodd Fiala     bool is_64bit = (len == UINT32_MAX);
4401767e115STodd Fiala     if (is_64bit) {
4411767e115STodd Fiala       len = m_cfi_data.GetU64(&offset);
4421767e115STodd Fiala       cie_id = m_cfi_data.GetU64(&offset);
4431767e115STodd Fiala       next_entry = current_entry + len + 12;
4441767e115STodd Fiala       cie_offset = current_entry + 12 - cie_id;
4451767e115STodd Fiala     } else {
4461767e115STodd Fiala       cie_id = m_cfi_data.GetU32(&offset);
4471767e115STodd Fiala       next_entry = current_entry + len + 4;
4481767e115STodd Fiala       cie_offset = current_entry + 4 - cie_id;
4491767e115STodd Fiala     }
450fbcb7f2cSJason Molenda 
451b9c1b51eSKate Stone     if (next_entry > m_cfi_data.GetByteSize() + 1) {
452b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError, "error: Invalid fde/cie next "
453b9c1b51eSKate Stone                                              "entry offset of 0x%x found in "
454b9c1b51eSKate Stone                                              "cie/fde at 0x%x\n",
455b9c1b51eSKate Stone                       next_entry, current_entry);
456e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
457c48ef341SJason Molenda       // invalid data.
458c48ef341SJason Molenda       m_fde_index.Clear();
459c48ef341SJason Molenda       m_fde_index_initialized = true;
460c48ef341SJason Molenda       return;
461c48ef341SJason Molenda     }
462cdda23ebSPavel Labath 
463cdda23ebSPavel Labath     // An FDE entry contains CIE_pointer in debug_frame in same place as cie_id
46405097246SAdrian Prantl     // in eh_frame. CIE_pointer is an offset into the .debug_frame section. So,
46505097246SAdrian Prantl     // variable cie_offset should be equal to cie_id for debug_frame.
466cdda23ebSPavel Labath     // FDE entries with cie_id == 0 shouldn't be ignored for it.
4673f2a0818SPavel Labath     if ((cie_id == 0 && m_type == EH) || cie_id == UINT32_MAX || len == 0) {
468cdda23ebSPavel Labath       auto cie_sp = ParseCIE(current_entry);
469cdda23ebSPavel Labath       if (!cie_sp) {
470cdda23ebSPavel Labath         // Cannot parse, the reason is already logged
471cdda23ebSPavel Labath         m_fde_index.Clear();
472cdda23ebSPavel Labath         m_fde_index_initialized = true;
473cdda23ebSPavel Labath         return;
474cdda23ebSPavel Labath       }
475cdda23ebSPavel Labath 
476cdda23ebSPavel Labath       m_cie_map[current_entry] = std::move(cie_sp);
477cdda23ebSPavel Labath       offset = next_entry;
478cdda23ebSPavel Labath       continue;
479cdda23ebSPavel Labath     }
480cdda23ebSPavel Labath 
4813f2a0818SPavel Labath     if (m_type == DWARF)
482cdda23ebSPavel Labath       cie_offset = cie_id;
483cdda23ebSPavel Labath 
484b9c1b51eSKate Stone     if (cie_offset > m_cfi_data.GetByteSize()) {
485cdda23ebSPavel Labath       Host::SystemLog(Host::eSystemLogError,
486cdda23ebSPavel Labath                       "error: Invalid cie offset of 0x%x "
487cdda23ebSPavel Labath                       "found in cie/fde at 0x%x\n",
488b9c1b51eSKate Stone                       cie_offset, current_entry);
489e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
490c48ef341SJason Molenda       // invalid data.
491c48ef341SJason Molenda       m_fde_index.Clear();
492c48ef341SJason Molenda       m_fde_index_initialized = true;
493c48ef341SJason Molenda       return;
494c48ef341SJason Molenda     }
495c48ef341SJason Molenda 
49673bf5dbdSGreg Clayton     const CIE *cie = GetCIE(cie_offset);
497b9c1b51eSKate Stone     if (cie) {
498c9660546SGreg Clayton       const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
499fbcb7f2cSJason Molenda       const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
500fbcb7f2cSJason Molenda       const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
501fbcb7f2cSJason Molenda 
50229cb868aSZachary Turner       lldb::addr_t addr =
50329cb868aSZachary Turner           GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
50429cb868aSZachary Turner                           text_addr, data_addr);
505c7afda5aSJason Molenda       if (clear_address_zeroth_bit)
506c7afda5aSJason Molenda         addr &= ~1ull;
507c7afda5aSJason Molenda 
50829cb868aSZachary Turner       lldb::addr_t length = GetGNUEHPointer(
50929cb868aSZachary Turner           m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
51029cb868aSZachary Turner           pc_rel_addr, text_addr, data_addr);
51174303829SJason Molenda       FDEEntryMap::Entry fde(addr, length, current_entry);
51274303829SJason Molenda       m_fde_index.Append(fde);
513b9c1b51eSKate Stone     } else {
514b9c1b51eSKate Stone       Host::SystemLog(Host::eSystemLogError, "error: unable to find CIE at "
515b9c1b51eSKate Stone                                              "0x%8.8x for cie_id = 0x%8.8x for "
516b9c1b51eSKate Stone                                              "entry at 0x%8.8x.\n",
517b9c1b51eSKate Stone                       cie_offset, cie_id, current_entry);
51873bf5dbdSGreg Clayton     }
519fbcb7f2cSJason Molenda     offset = next_entry;
520fbcb7f2cSJason Molenda   }
52174303829SJason Molenda   m_fde_index.Sort();
522fbcb7f2cSJason Molenda   m_fde_index_initialized = true;
523fbcb7f2cSJason Molenda }
524fbcb7f2cSJason Molenda 
525b9c1b51eSKate Stone bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset,
526b9c1b51eSKate Stone                                          Address startaddr,
527b9c1b51eSKate Stone                                          UnwindPlan &unwind_plan) {
5281ee89eb8SPavel Labath   Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND);
529c7bece56SGreg Clayton   lldb::offset_t offset = dwarf_offset;
530c7bece56SGreg Clayton   lldb::offset_t current_entry = offset;
531fbcb7f2cSJason Molenda 
532d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
533fbcb7f2cSJason Molenda     return false;
534fbcb7f2cSJason Molenda 
535a6682a41SJonas Devlieghere   if (!m_cfi_data_initialized)
536f97c5213SGreg Clayton     GetCFIData();
537fbcb7f2cSJason Molenda 
538fbcb7f2cSJason Molenda   uint32_t length = m_cfi_data.GetU32(&offset);
5391767e115STodd Fiala   dw_offset_t cie_offset;
5401767e115STodd Fiala   bool is_64bit = (length == UINT32_MAX);
5411767e115STodd Fiala   if (is_64bit) {
5421767e115STodd Fiala     length = m_cfi_data.GetU64(&offset);
5431767e115STodd Fiala     cie_offset = m_cfi_data.GetU64(&offset);
5441767e115STodd Fiala   } else {
5451767e115STodd Fiala     cie_offset = m_cfi_data.GetU32(&offset);
5461767e115STodd Fiala   }
547fbcb7f2cSJason Molenda 
548cdda23ebSPavel Labath   // FDE entries with zeroth cie_offset may occur for debug_frame.
5493f2a0818SPavel Labath   assert(!(m_type == EH && 0 == cie_offset) && cie_offset != UINT32_MAX);
55030fdc8d8SChris Lattner 
55105097246SAdrian Prantl   // Translate the CIE_id from the eh_frame format, which is relative to the
55205097246SAdrian Prantl   // FDE offset, into a __eh_frame section offset
5533f2a0818SPavel Labath   if (m_type == EH) {
554ab4f1924SJason Molenda     unwind_plan.SetSourceName("eh_frame CFI");
5551767e115STodd Fiala     cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
55660f0bd49SJason Molenda     unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
557b9c1b51eSKate Stone   } else {
558ab4f1924SJason Molenda     unwind_plan.SetSourceName("DWARF CFI");
55960f0bd49SJason Molenda     // In theory the debug_frame info should be valid at all call sites
56060f0bd49SJason Molenda     // ("asynchronous unwind info" as it is sometimes called) but in practice
56160f0bd49SJason Molenda     // gcc et al all emit call frame info for the prologue and call sites, but
562b9c1b51eSKate Stone     // not for the epilogue or all the other locations during the function
563b9c1b51eSKate Stone     // reliably.
56460f0bd49SJason Molenda     unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
565ab4f1924SJason Molenda   }
56660f0bd49SJason Molenda   unwind_plan.SetSourcedFromCompiler(eLazyBoolYes);
56730fdc8d8SChris Lattner 
56830fdc8d8SChris Lattner   const CIE *cie = GetCIE(cie_offset);
569d4612ad0SEd Maste   assert(cie != nullptr);
570fbcb7f2cSJason Molenda 
5711767e115STodd Fiala   const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
572fbcb7f2cSJason Molenda 
573c9660546SGreg Clayton   const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
57430fdc8d8SChris Lattner   const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
57530fdc8d8SChris Lattner   const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
57629cb868aSZachary Turner   lldb::addr_t range_base =
57729cb868aSZachary Turner       GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
578b9c1b51eSKate Stone                       text_addr, data_addr);
57929cb868aSZachary Turner   lldb::addr_t range_len = GetGNUEHPointer(
58029cb868aSZachary Turner       m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
58129cb868aSZachary Turner       pc_rel_addr, text_addr, data_addr);
582b9c1b51eSKate Stone   AddressRange range(range_base, m_objfile.GetAddressByteSize(),
583b9c1b51eSKate Stone                      m_objfile.GetSectionList());
584fbcb7f2cSJason Molenda   range.SetByteSize(range_len);
58530fdc8d8SChris Lattner 
586e9c7ecf6SJason Molenda   addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
587e9c7ecf6SJason Molenda 
588b9c1b51eSKate Stone   if (cie->augmentation[0] == 'z') {
58930fdc8d8SChris Lattner     uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
590b9c1b51eSKate Stone     if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) {
591e9c7ecf6SJason Molenda       offset_t saved_offset = offset;
59229cb868aSZachary Turner       lsda_data_file_address =
59329cb868aSZachary Turner           GetGNUEHPointer(m_cfi_data, &offset, cie->lsda_addr_encoding,
59429cb868aSZachary Turner                           pc_rel_addr, text_addr, data_addr);
595b9c1b51eSKate Stone       if (offset - saved_offset != aug_data_len) {
596e9c7ecf6SJason Molenda         // There is more in the augmentation region than we know how to process;
597e9c7ecf6SJason Molenda         // don't read anything.
598e9c7ecf6SJason Molenda         lsda_data_file_address = LLDB_INVALID_ADDRESS;
599e9c7ecf6SJason Molenda       }
600e9c7ecf6SJason Molenda       offset = saved_offset;
601e9c7ecf6SJason Molenda     }
60230fdc8d8SChris Lattner     offset += aug_data_len;
60330fdc8d8SChris Lattner   }
604e9c7ecf6SJason Molenda   Address lsda_data;
605e9c7ecf6SJason Molenda   Address personality_function_ptr;
606e9c7ecf6SJason Molenda 
607b9c1b51eSKate Stone   if (lsda_data_file_address != LLDB_INVALID_ADDRESS &&
608b9c1b51eSKate Stone       cie->personality_loc != LLDB_INVALID_ADDRESS) {
609b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address,
610b9c1b51eSKate Stone                                               lsda_data);
611b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc,
612b9c1b51eSKate Stone                                               personality_function_ptr);
613e9c7ecf6SJason Molenda   }
614e9c7ecf6SJason Molenda 
615b9c1b51eSKate Stone   if (lsda_data.IsValid() && personality_function_ptr.IsValid()) {
616e9c7ecf6SJason Molenda     unwind_plan.SetLSDAAddress(lsda_data);
617e9c7ecf6SJason Molenda     unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr);
618e9c7ecf6SJason Molenda   }
61930fdc8d8SChris Lattner 
62030fdc8d8SChris Lattner   uint32_t code_align = cie->code_align;
62130fdc8d8SChris Lattner   int32_t data_align = cie->data_align;
62230fdc8d8SChris Lattner 
623fbcb7f2cSJason Molenda   unwind_plan.SetPlanValidAddressRange(range);
6241d42c7bcSJason Molenda   UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
6251d42c7bcSJason Molenda   *cie_initial_row = cie->initial_row;
6261d42c7bcSJason Molenda   UnwindPlan::RowSP row(cie_initial_row);
62730fdc8d8SChris Lattner 
6283f2a0818SPavel Labath   unwind_plan.SetRegisterKind(GetRegisterKind());
6298eba46c6SJason Molenda   unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num);
630fbcb7f2cSJason Molenda 
631f70d6040SRichard Mitton   std::vector<UnwindPlan::RowSP> stack;
632f70d6040SRichard Mitton 
633fbcb7f2cSJason Molenda   UnwindPlan::Row::RegisterLocation reg_location;
634b9c1b51eSKate Stone   while (m_cfi_data.ValidOffset(offset) && offset < end_offset) {
63530fdc8d8SChris Lattner     uint8_t inst = m_cfi_data.GetU8(&offset);
63630fdc8d8SChris Lattner     uint8_t primary_opcode = inst & 0xC0;
63730fdc8d8SChris Lattner     uint8_t extended_opcode = inst & 0x3F;
63830fdc8d8SChris Lattner 
639b9c1b51eSKate Stone     if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align,
640b9c1b51eSKate Stone                                  offset, *row)) {
641b9c1b51eSKate Stone       if (primary_opcode) {
642b9c1b51eSKate Stone         switch (primary_opcode) {
64330fdc8d8SChris Lattner         case DW_CFA_advance_loc: // (Row Creation Instruction)
64430fdc8d8SChris Lattner         { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
64530fdc8d8SChris Lattner           // takes a single argument that represents a constant delta. The
64605097246SAdrian Prantl           // required action is to create a new table row with a location value
64705097246SAdrian Prantl           // that is computed by taking the current entry's location value and
64805097246SAdrian Prantl           // adding (delta * code_align). All other values in the new row are
64905097246SAdrian Prantl           // initially identical to the current row.
650fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
651fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
652fa67e879SJason Molenda           *newrow = *row.get();
653fa67e879SJason Molenda           row.reset(newrow);
6541d42c7bcSJason Molenda           row->SlideOffset(extended_opcode * code_align);
65530fdc8d8SChris Lattner           break;
65630fdc8d8SChris Lattner         }
65730fdc8d8SChris Lattner 
658b9c1b51eSKate Stone         case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are
659b9c1b51eSKate Stone                                // register
66030fdc8d8SChris Lattner           // takes a single argument that represents a register number. The
66130fdc8d8SChris Lattner           // required action is to change the rule for the indicated register
66230fdc8d8SChris Lattner           // to the rule assigned it by the initial_instructions in the CIE.
663623652a0STamas Berghammer           uint32_t reg_num = extended_opcode;
66405097246SAdrian Prantl           // We only keep enough register locations around to unwind what is in
66505097246SAdrian Prantl           // our thread, and these are organized by the register index in that
66605097246SAdrian Prantl           // state, so we need to convert our eh_frame register number from the
66705097246SAdrian Prantl           // EH frame info, to a register index
66830fdc8d8SChris Lattner 
669b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
670b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
671b9c1b51eSKate Stone                                                             reg_location))
6721d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
67330fdc8d8SChris Lattner           break;
67430fdc8d8SChris Lattner         }
67530fdc8d8SChris Lattner         }
676b9c1b51eSKate Stone       } else {
677b9c1b51eSKate Stone         switch (extended_opcode) {
67830fdc8d8SChris Lattner         case DW_CFA_set_loc: // 0x1 (Row Creation Instruction)
67930fdc8d8SChris Lattner         {
68030fdc8d8SChris Lattner           // DW_CFA_set_loc takes a single argument that represents an address.
68130fdc8d8SChris Lattner           // The required action is to create a new table row using the
68230fdc8d8SChris Lattner           // specified address as the location. All other values in the new row
68330fdc8d8SChris Lattner           // are initially identical to the current row. The new location value
68430fdc8d8SChris Lattner           // should always be greater than the current one.
685fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
686fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
687fa67e879SJason Molenda           *newrow = *row.get();
688fa67e879SJason Molenda           row.reset(newrow);
689b9c1b51eSKate Stone           row->SetOffset(m_cfi_data.GetPointer(&offset) -
690b9c1b51eSKate Stone                          startaddr.GetFileAddress());
69130fdc8d8SChris Lattner           break;
692623652a0STamas Berghammer         }
69330fdc8d8SChris Lattner 
69430fdc8d8SChris Lattner         case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction)
69530fdc8d8SChris Lattner         {
69630fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
69730fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
69830fdc8d8SChris Lattner           // encoding and size of the delta argument.
699fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
700fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
701fa67e879SJason Molenda           *newrow = *row.get();
702fa67e879SJason Molenda           row.reset(newrow);
7031d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align);
70430fdc8d8SChris Lattner           break;
705623652a0STamas Berghammer         }
70630fdc8d8SChris Lattner 
70730fdc8d8SChris Lattner         case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction)
70830fdc8d8SChris Lattner         {
70930fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
71030fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
71130fdc8d8SChris Lattner           // encoding and size of the delta argument.
712fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
713fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
714fa67e879SJason Molenda           *newrow = *row.get();
715fa67e879SJason Molenda           row.reset(newrow);
7161d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align);
71730fdc8d8SChris Lattner           break;
718623652a0STamas Berghammer         }
71930fdc8d8SChris Lattner 
72030fdc8d8SChris Lattner         case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction)
72130fdc8d8SChris Lattner         {
72230fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
72330fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
72430fdc8d8SChris Lattner           // encoding and size of the delta argument.
725fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
726fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
727fa67e879SJason Molenda           *newrow = *row.get();
728fa67e879SJason Molenda           row.reset(newrow);
7291d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align);
73030fdc8d8SChris Lattner           break;
73130fdc8d8SChris Lattner         }
73230fdc8d8SChris Lattner 
73330fdc8d8SChris Lattner         case DW_CFA_restore_extended: // 0x6
73430fdc8d8SChris Lattner         {
73530fdc8d8SChris Lattner           // takes a single unsigned LEB128 argument that represents a register
73630fdc8d8SChris Lattner           // number. This instruction is identical to DW_CFA_restore except for
73730fdc8d8SChris Lattner           // the encoding and size of the register argument.
738623652a0STamas Berghammer           uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
739b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
740b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
741b9c1b51eSKate Stone                                                             reg_location))
7421d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
74330fdc8d8SChris Lattner           break;
74430fdc8d8SChris Lattner         }
74530fdc8d8SChris Lattner 
74630fdc8d8SChris Lattner         case DW_CFA_remember_state: // 0xA
747fa67e879SJason Molenda         {
74830fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
74905097246SAdrian Prantl           // DW_CFA_remember_state instruction means to save the rules for
75005097246SAdrian Prantl           // every register on the current row on the stack. Encountering the
75130fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
75230fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
75330fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
75430fdc8d8SChris Lattner           // function.)
755f70d6040SRichard Mitton           stack.push_back(row);
756fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
757fa67e879SJason Molenda           *newrow = *row.get();
758fa67e879SJason Molenda           row.reset(newrow);
75930fdc8d8SChris Lattner           break;
760623652a0STamas Berghammer         }
76130fdc8d8SChris Lattner 
76230fdc8d8SChris Lattner         case DW_CFA_restore_state: // 0xB
763623652a0STamas Berghammer         {
76430fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
76505097246SAdrian Prantl           // DW_CFA_remember_state instruction means to save the rules for
76605097246SAdrian Prantl           // every register on the current row on the stack. Encountering the
76730fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
76830fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
76930fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
77030fdc8d8SChris Lattner           // function.)
771b9c1b51eSKate Stone           if (stack.empty()) {
7721ee89eb8SPavel Labath             if (log)
773b9c1b51eSKate Stone               log->Printf("DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32
774b9c1b51eSKate Stone                           ", startaddr: %" PRIx64
775b9c1b51eSKate Stone                           " encountered DW_CFA_restore_state but state stack "
776b9c1b51eSKate Stone                           "is empty. Corrupt unwind info?",
777b9c1b51eSKate Stone                           __FUNCTION__, dwarf_offset,
778b9c1b51eSKate Stone                           startaddr.GetFileAddress());
7791ee89eb8SPavel Labath             break;
7801ee89eb8SPavel Labath           }
78149dd6d15STamas Berghammer           lldb::addr_t offset = row->GetOffset();
782f70d6040SRichard Mitton           row = stack.back();
783f70d6040SRichard Mitton           stack.pop_back();
78449dd6d15STamas Berghammer           row->SetOffset(offset);
78530fdc8d8SChris Lattner           break;
786623652a0STamas Berghammer         }
787623652a0STamas Berghammer 
7881ee89eb8SPavel Labath         case DW_CFA_GNU_args_size: // 0x2e
7891ee89eb8SPavel Labath         {
790b9c1b51eSKate Stone           // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128
79105097246SAdrian Prantl           // operand representing an argument size. This instruction specifies
79205097246SAdrian Prantl           // the total of the size of the arguments which have been pushed onto
79305097246SAdrian Prantl           // the stack.
7941ee89eb8SPavel Labath 
7951ee89eb8SPavel Labath           // TODO: Figure out how we should handle this.
7961ee89eb8SPavel Labath           m_cfi_data.GetULEB128(&offset);
797a41deb1cSJason Molenda           break;
7981ee89eb8SPavel Labath         }
7991ee89eb8SPavel Labath 
800623652a0STamas Berghammer         case DW_CFA_val_offset:    // 0x14
801623652a0STamas Berghammer         case DW_CFA_val_offset_sf: // 0x15
802623652a0STamas Berghammer         default:
803623652a0STamas Berghammer           break;
804623652a0STamas Berghammer         }
805623652a0STamas Berghammer       }
806623652a0STamas Berghammer     }
807623652a0STamas Berghammer   }
808623652a0STamas Berghammer   unwind_plan.AppendRow(row);
809623652a0STamas Berghammer 
810623652a0STamas Berghammer   return true;
811623652a0STamas Berghammer }
812623652a0STamas Berghammer 
813b9c1b51eSKate Stone bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
814623652a0STamas Berghammer                                                  uint8_t extended_opcode,
815623652a0STamas Berghammer                                                  int32_t data_align,
816623652a0STamas Berghammer                                                  lldb::offset_t &offset,
817b9c1b51eSKate Stone                                                  UnwindPlan::Row &row) {
818623652a0STamas Berghammer   UnwindPlan::Row::RegisterLocation reg_location;
819623652a0STamas Berghammer 
820b9c1b51eSKate Stone   if (primary_opcode) {
821b9c1b51eSKate Stone     switch (primary_opcode) {
822b9c1b51eSKate Stone     case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are
823b9c1b51eSKate Stone                           // register
824623652a0STamas Berghammer       // takes two arguments: an unsigned LEB128 constant representing a
825623652a0STamas Berghammer       // factored offset and a register number. The required action is to
82605097246SAdrian Prantl       // change the rule for the register indicated by the register number to
82705097246SAdrian Prantl       // be an offset(N) rule with a value of (N = factored offset *
82805097246SAdrian Prantl       // data_align).
829623652a0STamas Berghammer       uint8_t reg_num = extended_opcode;
830623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
831623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
832623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
833623652a0STamas Berghammer       return true;
834623652a0STamas Berghammer     }
835623652a0STamas Berghammer     }
836b9c1b51eSKate Stone   } else {
837b9c1b51eSKate Stone     switch (extended_opcode) {
838623652a0STamas Berghammer     case DW_CFA_nop: // 0x0
839623652a0STamas Berghammer       return true;
840623652a0STamas Berghammer 
841623652a0STamas Berghammer     case DW_CFA_offset_extended: // 0x5
842623652a0STamas Berghammer     {
84305097246SAdrian Prantl       // takes two unsigned LEB128 arguments representing a register number and
84405097246SAdrian Prantl       // a factored offset. This instruction is identical to DW_CFA_offset
845623652a0STamas Berghammer       // except for the encoding and size of the register argument.
846623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
847623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
848623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
849623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
850623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
851623652a0STamas Berghammer       return true;
852623652a0STamas Berghammer     }
853623652a0STamas Berghammer 
854623652a0STamas Berghammer     case DW_CFA_undefined: // 0x7
855623652a0STamas Berghammer     {
856623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
857623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
858623652a0STamas Berghammer       // register to undefined.
859623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
860623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
861623652a0STamas Berghammer       reg_location.SetUndefined();
862623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
863623652a0STamas Berghammer       return true;
864623652a0STamas Berghammer     }
865623652a0STamas Berghammer 
866623652a0STamas Berghammer     case DW_CFA_same_value: // 0x8
867623652a0STamas Berghammer     {
868623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
869623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
870623652a0STamas Berghammer       // register to same value.
871623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
872623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
873623652a0STamas Berghammer       reg_location.SetSame();
874623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
875623652a0STamas Berghammer       return true;
876623652a0STamas Berghammer     }
877623652a0STamas Berghammer 
878623652a0STamas Berghammer     case DW_CFA_register: // 0x9
879623652a0STamas Berghammer     {
88005097246SAdrian Prantl       // takes two unsigned LEB128 arguments representing register numbers. The
88105097246SAdrian Prantl       // required action is to set the rule for the first register to be the
88205097246SAdrian Prantl       // second register.
883623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
884623652a0STamas Berghammer       uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
885623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
886623652a0STamas Berghammer       reg_location.SetInRegister(other_reg_num);
887623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
888623652a0STamas Berghammer       return true;
889623652a0STamas Berghammer     }
89030fdc8d8SChris Lattner 
89130fdc8d8SChris Lattner     case DW_CFA_def_cfa: // 0xC    (CFA Definition Instruction)
89230fdc8d8SChris Lattner     {
89305097246SAdrian Prantl       // Takes two unsigned LEB128 operands representing a register number and
89405097246SAdrian Prantl       // a (non-factored) offset. The required action is to define the current
89505097246SAdrian Prantl       // CFA rule to use the provided register and offset.
896623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
897623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
898623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
899623652a0STamas Berghammer       return true;
90030fdc8d8SChris Lattner     }
90130fdc8d8SChris Lattner 
90230fdc8d8SChris Lattner     case DW_CFA_def_cfa_register: // 0xD    (CFA Definition Instruction)
90330fdc8d8SChris Lattner     {
90430fdc8d8SChris Lattner       // takes a single unsigned LEB128 argument representing a register
90505097246SAdrian Prantl       // number. The required action is to define the current CFA rule to use
90605097246SAdrian Prantl       // the provided register (but to keep the old offset).
907623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
908b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num,
909b9c1b51eSKate Stone                                                 row.GetCFAValue().GetOffset());
910623652a0STamas Berghammer       return true;
91130fdc8d8SChris Lattner     }
91230fdc8d8SChris Lattner 
91330fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset: // 0xE    (CFA Definition Instruction)
91430fdc8d8SChris Lattner     {
91505097246SAdrian Prantl       // Takes a single unsigned LEB128 operand representing a (non-factored)
91605097246SAdrian Prantl       // offset. The required action is to define the current CFA rule to use
91705097246SAdrian Prantl       // the provided offset (but to keep the old register).
918623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
919b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(
920b9c1b51eSKate Stone           row.GetCFAValue().GetRegisterNumber(), op_offset);
921623652a0STamas Berghammer       return true;
92230fdc8d8SChris Lattner     }
92330fdc8d8SChris Lattner 
92430fdc8d8SChris Lattner     case DW_CFA_def_cfa_expression: // 0xF    (CFA Definition Instruction)
92530fdc8d8SChris Lattner     {
92630fdc8d8SChris Lattner       size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
927b9c1b51eSKate Stone       const uint8_t *block_data =
928b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
929623652a0STamas Berghammer       row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
930623652a0STamas Berghammer       return true;
93130fdc8d8SChris Lattner     }
93230fdc8d8SChris Lattner 
93330fdc8d8SChris Lattner     case DW_CFA_expression: // 0x10
93430fdc8d8SChris Lattner     {
93505097246SAdrian Prantl       // Takes two operands: an unsigned LEB128 value representing a register
93605097246SAdrian Prantl       // number, and a DW_FORM_block value representing a DWARF expression. The
93705097246SAdrian Prantl       // required action is to change the rule for the register indicated by
93805097246SAdrian Prantl       // the register number to be an expression(E) rule where E is the DWARF
93905097246SAdrian Prantl       // expression. That is, the DWARF expression computes the address. The
94005097246SAdrian Prantl       // value of the CFA is pushed on the DWARF evaluation stack prior to
94105097246SAdrian Prantl       // execution of the DWARF expression.
942623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
94330fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
944b9c1b51eSKate Stone       const uint8_t *block_data =
945b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
946623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
94730fdc8d8SChris Lattner       reg_location.SetAtDWARFExpression(block_data, block_len);
948623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
949623652a0STamas Berghammer       return true;
95030fdc8d8SChris Lattner     }
95130fdc8d8SChris Lattner 
95230fdc8d8SChris Lattner     case DW_CFA_offset_extended_sf: // 0x11
95330fdc8d8SChris Lattner     {
95405097246SAdrian Prantl       // takes two operands: an unsigned LEB128 value representing a register
95505097246SAdrian Prantl       // number and a signed LEB128 factored offset. This instruction is
95605097246SAdrian Prantl       // identical to DW_CFA_offset_extended except that the second operand is
95705097246SAdrian Prantl       // signed and factored.
958623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
959623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
960623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
96130fdc8d8SChris Lattner       reg_location.SetAtCFAPlusOffset(op_offset);
962623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
963623652a0STamas Berghammer       return true;
96430fdc8d8SChris Lattner     }
96530fdc8d8SChris Lattner 
96630fdc8d8SChris Lattner     case DW_CFA_def_cfa_sf: // 0x12   (CFA Definition Instruction)
96730fdc8d8SChris Lattner     {
96805097246SAdrian Prantl       // Takes two operands: an unsigned LEB128 value representing a register
96905097246SAdrian Prantl       // number and a signed LEB128 factored offset. This instruction is
97005097246SAdrian Prantl       // identical to DW_CFA_def_cfa except that the second operand is signed
97105097246SAdrian Prantl       // 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       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
975623652a0STamas Berghammer       return true;
97630fdc8d8SChris Lattner     }
97730fdc8d8SChris Lattner 
97830fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset_sf: // 0x13   (CFA Definition Instruction)
97930fdc8d8SChris Lattner     {
98005097246SAdrian Prantl       // takes a signed LEB128 operand representing a factored offset. This
98105097246SAdrian Prantl       // instruction is identical to  DW_CFA_def_cfa_offset except that the
98205097246SAdrian Prantl       // operand is signed and factored.
983623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
984623652a0STamas Berghammer       uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
985623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
986623652a0STamas Berghammer       return true;
98730fdc8d8SChris Lattner     }
98830fdc8d8SChris Lattner 
98930fdc8d8SChris Lattner     case DW_CFA_val_expression: // 0x16
99030fdc8d8SChris Lattner     {
99130fdc8d8SChris Lattner       // takes two operands: an unsigned LEB128 value representing a register
99205097246SAdrian Prantl       // number, and a DW_FORM_block value representing a DWARF expression. The
99305097246SAdrian Prantl       // required action is to change the rule for the register indicated by
99405097246SAdrian Prantl       // the register number to be a val_expression(E) rule where E is the
99530fdc8d8SChris Lattner       // DWARF expression. That is, the DWARF expression computes the value of
99630fdc8d8SChris Lattner       // the given register. The value of the CFA is pushed on the DWARF
99730fdc8d8SChris Lattner       // evaluation stack prior to execution of the DWARF expression.
998623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
99930fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
1000b9c1b51eSKate Stone       const uint8_t *block_data =
1001b9c1b51eSKate Stone           (const uint8_t *)m_cfi_data.GetData(&offset, block_len);
100230fdc8d8SChris Lattner       //#if defined(__i386__) || defined(__x86_64__)
1003b9c1b51eSKate Stone       //              // The EH frame info for EIP and RIP contains code that
1004b9c1b51eSKate Stone       //              looks for traps to
100530fdc8d8SChris Lattner       //              // be a specific type and increments the PC.
100630fdc8d8SChris Lattner       //              // For i386:
100730fdc8d8SChris Lattner       //              // DW_CFA_val_expression where:
1008b9c1b51eSKate Stone       //              // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup,
1009b9c1b51eSKate Stone       //              DW_OP_plus_uconst(0x34),
1010b9c1b51eSKate Stone       //              //       DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0),
1011b9c1b51eSKate Stone       //              DW_OP_deref,
1012b9c1b51eSKate Stone       //              //       DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap,
1013b9c1b51eSKate Stone       //              DW_OP_lit4, DW_OP_ne,
101430fdc8d8SChris Lattner       //              //       DW_OP_and, DW_OP_plus
101530fdc8d8SChris Lattner       //              // This basically does a:
101630fdc8d8SChris Lattner       //              // eip = ucontenxt.mcontext32->gpr.eip;
1017b9c1b51eSKate Stone       //              // if (ucontenxt.mcontext32->exc.trapno != 3 &&
1018b9c1b51eSKate Stone       //              ucontenxt.mcontext32->exc.trapno != 4)
101930fdc8d8SChris Lattner       //              //   eip++;
102030fdc8d8SChris Lattner       //              //
102130fdc8d8SChris Lattner       //              // For x86_64:
102230fdc8d8SChris Lattner       //              // DW_CFA_val_expression where:
1023b9c1b51eSKate Stone       //              // rip =  DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup,
1024b9c1b51eSKate Stone       //              DW_OP_plus_uconst(0x90), DW_OP_deref,
1025b9c1b51eSKate Stone       //              //          DW_OP_swap, DW_OP_plus_uconst(0),
1026b9c1b51eSKate Stone       //              DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
1027b9c1b51eSKate Stone       //              //          DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
1028b9c1b51eSKate Stone       //              DW_OP_and, DW_OP_plus
102930fdc8d8SChris Lattner       //              // This basically does a:
103030fdc8d8SChris Lattner       //              // rip = ucontenxt.mcontext64->gpr.rip;
1031b9c1b51eSKate Stone       //              // if (ucontenxt.mcontext64->exc.trapno != 3 &&
1032b9c1b51eSKate Stone       //              ucontenxt.mcontext64->exc.trapno != 4)
103330fdc8d8SChris Lattner       //              //   rip++;
1034b9c1b51eSKate Stone       //              // The trap comparisons and increments are not needed as
1035b9c1b51eSKate Stone       //              it hoses up the unwound PC which
1036b9c1b51eSKate Stone       //              // is expected to point at least past the instruction that
1037b9c1b51eSKate Stone       //              causes the fault/trap. So we
1038b9c1b51eSKate Stone       //              // take it out by trimming the expression right at the
1039b9c1b51eSKate Stone       //              first "DW_OP_swap" opcodes
1040b9c1b51eSKate Stone       //              if (block_data != NULL && thread->GetPCRegNum(Thread::GCC)
1041b9c1b51eSKate Stone       //              == reg_num)
104230fdc8d8SChris Lattner       //              {
104330fdc8d8SChris Lattner       //                  if (thread->Is64Bit())
104430fdc8d8SChris Lattner       //                  {
1045b9c1b51eSKate Stone       //                      if (block_len > 9 && block_data[8] == DW_OP_swap
1046b9c1b51eSKate Stone       //                      && block_data[9] == DW_OP_plus_uconst)
104730fdc8d8SChris Lattner       //                          block_len = 8;
104830fdc8d8SChris Lattner       //                  }
104930fdc8d8SChris Lattner       //                  else
105030fdc8d8SChris Lattner       //                  {
1051b9c1b51eSKate Stone       //                      if (block_len > 8 && block_data[7] == DW_OP_swap
1052b9c1b51eSKate Stone       //                      && block_data[8] == DW_OP_plus_uconst)
105330fdc8d8SChris Lattner       //                          block_len = 7;
105430fdc8d8SChris Lattner       //                  }
105530fdc8d8SChris Lattner       //              }
105630fdc8d8SChris Lattner       //#endif
105730fdc8d8SChris Lattner       reg_location.SetIsDWARFExpression(block_data, block_len);
1058623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
1059fbcb7f2cSJason Molenda       return true;
106030fdc8d8SChris Lattner     }
1061623652a0STamas Berghammer     }
1062623652a0STamas Berghammer   }
1063623652a0STamas Berghammer   return false;
1064623652a0STamas Berghammer }
10656b63b148STamas Berghammer 
1066b9c1b51eSKate Stone void DWARFCallFrameInfo::ForEachFDEEntries(
1067b9c1b51eSKate Stone     const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) {
10686b63b148STamas Berghammer   GetFDEIndex();
10696b63b148STamas Berghammer 
1070b9c1b51eSKate Stone   for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) {
10716b63b148STamas Berghammer     const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i);
10726b63b148STamas Berghammer     if (!callback(entry.base, entry.size, entry.data))
10736b63b148STamas Berghammer       break;
10746b63b148STamas Berghammer   }
10756b63b148STamas Berghammer }
1076