180814287SRaphael Isemann //===-- DWARFCallFrameInfo.cpp --------------------------------------------===//
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"
10*68793919SJonas Devlieghere #include "lldb/Core/Debugger.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"
195f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
20c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
216f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2238d0632eSPavel Labath #include "lldb/Utility/Timer.h"
233fd917d8SJoseph Tremoulet #include <cstring>
24c34698a8SPavel Labath #include <list>
2530fdc8d8SChris Lattner 
2630fdc8d8SChris Lattner using namespace lldb;
2730fdc8d8SChris Lattner using namespace lldb_private;
28ae869d44SShafik Yaghmour using namespace lldb_private::dwarf;
2930fdc8d8SChris Lattner 
3029cb868aSZachary Turner // GetDwarfEHPtr
3129cb868aSZachary Turner //
3205097246SAdrian Prantl // Used for calls when the value type is specified by a DWARF EH Frame pointer
3305097246SAdrian Prantl // encoding.
3429cb868aSZachary Turner static uint64_t
GetGNUEHPointer(const DataExtractor & DE,offset_t * offset_ptr,uint32_t eh_ptr_enc,addr_t pc_rel_addr,addr_t text_addr,addr_t data_addr)3529cb868aSZachary Turner GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr,
3629cb868aSZachary Turner                 uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr,
3729cb868aSZachary Turner                 addr_t data_addr) //, BSDRelocs *data_relocs) const
3829cb868aSZachary Turner {
3929cb868aSZachary Turner   if (eh_ptr_enc == DW_EH_PE_omit)
4029cb868aSZachary Turner     return ULLONG_MAX; // Value isn't in the buffer...
4129cb868aSZachary Turner 
4229cb868aSZachary Turner   uint64_t baseAddress = 0;
4329cb868aSZachary Turner   uint64_t addressValue = 0;
4429cb868aSZachary Turner   const uint32_t addr_size = DE.GetAddressByteSize();
4529cb868aSZachary Turner   assert(addr_size == 4 || addr_size == 8);
4629cb868aSZachary Turner 
4729cb868aSZachary Turner   bool signExtendValue = false;
4829cb868aSZachary Turner   // Decode the base part or adjust our offset
4929cb868aSZachary Turner   switch (eh_ptr_enc & 0x70) {
5029cb868aSZachary Turner   case DW_EH_PE_pcrel:
5129cb868aSZachary Turner     signExtendValue = true;
5229cb868aSZachary Turner     baseAddress = *offset_ptr;
5329cb868aSZachary Turner     if (pc_rel_addr != LLDB_INVALID_ADDRESS)
5429cb868aSZachary Turner       baseAddress += pc_rel_addr;
5529cb868aSZachary Turner     //      else
5629cb868aSZachary Turner     //          Log::GlobalWarning ("PC relative pointer encoding found with
5729cb868aSZachary Turner     //          invalid pc relative address.");
5829cb868aSZachary Turner     break;
5929cb868aSZachary Turner 
6029cb868aSZachary Turner   case DW_EH_PE_textrel:
6129cb868aSZachary Turner     signExtendValue = true;
6229cb868aSZachary Turner     if (text_addr != LLDB_INVALID_ADDRESS)
6329cb868aSZachary Turner       baseAddress = text_addr;
6429cb868aSZachary Turner     //      else
6529cb868aSZachary Turner     //          Log::GlobalWarning ("text relative pointer encoding being
6629cb868aSZachary Turner     //          decoded with invalid text section address, setting base address
6729cb868aSZachary Turner     //          to zero.");
6829cb868aSZachary Turner     break;
6929cb868aSZachary Turner 
7029cb868aSZachary Turner   case DW_EH_PE_datarel:
7129cb868aSZachary Turner     signExtendValue = true;
7229cb868aSZachary Turner     if (data_addr != LLDB_INVALID_ADDRESS)
7329cb868aSZachary Turner       baseAddress = data_addr;
7429cb868aSZachary Turner     //      else
7529cb868aSZachary Turner     //          Log::GlobalWarning ("data relative pointer encoding being
7629cb868aSZachary Turner     //          decoded with invalid data section address, setting base address
7729cb868aSZachary Turner     //          to zero.");
7829cb868aSZachary Turner     break;
7929cb868aSZachary Turner 
8029cb868aSZachary Turner   case DW_EH_PE_funcrel:
8129cb868aSZachary Turner     signExtendValue = true;
8229cb868aSZachary Turner     break;
8329cb868aSZachary Turner 
8429cb868aSZachary Turner   case DW_EH_PE_aligned: {
8505097246SAdrian Prantl     // SetPointerSize should be called prior to extracting these so the pointer
8605097246SAdrian Prantl     // size is cached
8729cb868aSZachary Turner     assert(addr_size != 0);
8829cb868aSZachary Turner     if (addr_size) {
8929cb868aSZachary Turner       // Align to a address size boundary first
9029cb868aSZachary Turner       uint32_t alignOffset = *offset_ptr % addr_size;
9129cb868aSZachary Turner       if (alignOffset)
9229cb868aSZachary Turner         offset_ptr += addr_size - alignOffset;
9329cb868aSZachary Turner     }
9429cb868aSZachary Turner   } break;
9529cb868aSZachary Turner 
9629cb868aSZachary Turner   default:
9729cb868aSZachary Turner     break;
9829cb868aSZachary Turner   }
9929cb868aSZachary Turner 
10029cb868aSZachary Turner   // Decode the value part
10129cb868aSZachary Turner   switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) {
10229cb868aSZachary Turner   case DW_EH_PE_absptr: {
10329cb868aSZachary Turner     addressValue = DE.GetAddress(offset_ptr);
10429cb868aSZachary Turner     //          if (data_relocs)
10529cb868aSZachary Turner     //              addressValue = data_relocs->Relocate(*offset_ptr -
10629cb868aSZachary Turner     //              addr_size, *this, addressValue);
10729cb868aSZachary Turner   } break;
10829cb868aSZachary Turner   case DW_EH_PE_uleb128:
10929cb868aSZachary Turner     addressValue = DE.GetULEB128(offset_ptr);
11029cb868aSZachary Turner     break;
11129cb868aSZachary Turner   case DW_EH_PE_udata2:
11229cb868aSZachary Turner     addressValue = DE.GetU16(offset_ptr);
11329cb868aSZachary Turner     break;
11429cb868aSZachary Turner   case DW_EH_PE_udata4:
11529cb868aSZachary Turner     addressValue = DE.GetU32(offset_ptr);
11629cb868aSZachary Turner     break;
11729cb868aSZachary Turner   case DW_EH_PE_udata8:
11829cb868aSZachary Turner     addressValue = DE.GetU64(offset_ptr);
11929cb868aSZachary Turner     break;
12029cb868aSZachary Turner   case DW_EH_PE_sleb128:
12129cb868aSZachary Turner     addressValue = DE.GetSLEB128(offset_ptr);
12229cb868aSZachary Turner     break;
12329cb868aSZachary Turner   case DW_EH_PE_sdata2:
12429cb868aSZachary Turner     addressValue = (int16_t)DE.GetU16(offset_ptr);
12529cb868aSZachary Turner     break;
12629cb868aSZachary Turner   case DW_EH_PE_sdata4:
12729cb868aSZachary Turner     addressValue = (int32_t)DE.GetU32(offset_ptr);
12829cb868aSZachary Turner     break;
12929cb868aSZachary Turner   case DW_EH_PE_sdata8:
13029cb868aSZachary Turner     addressValue = (int64_t)DE.GetU64(offset_ptr);
13129cb868aSZachary Turner     break;
13229cb868aSZachary Turner   default:
13329cb868aSZachary Turner     // Unhandled encoding type
13429cb868aSZachary Turner     assert(eh_ptr_enc);
13529cb868aSZachary Turner     break;
13629cb868aSZachary Turner   }
13729cb868aSZachary Turner 
13829cb868aSZachary Turner   // Since we promote everything to 64 bit, we may need to sign extend
13929cb868aSZachary Turner   if (signExtendValue && addr_size < sizeof(baseAddress)) {
14029cb868aSZachary Turner     uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
14129cb868aSZachary Turner     if (sign_bit & addressValue) {
14229cb868aSZachary Turner       uint64_t mask = ~sign_bit + 1;
14329cb868aSZachary Turner       addressValue |= mask;
14429cb868aSZachary Turner     }
14529cb868aSZachary Turner   }
14629cb868aSZachary Turner   return baseAddress + addressValue;
14729cb868aSZachary Turner }
14829cb868aSZachary Turner 
DWARFCallFrameInfo(ObjectFile & objfile,SectionSP & section_sp,Type type)149b9c1b51eSKate Stone DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile &objfile,
1503f2a0818SPavel Labath                                        SectionSP &section_sp, Type type)
1513f2a0818SPavel Labath     : m_objfile(objfile), m_section_sp(section_sp), m_type(type) {}
15230fdc8d8SChris Lattner 
GetUnwindPlan(const Address & addr,UnwindPlan & unwind_plan)15323594291SPavel Labath bool DWARFCallFrameInfo::GetUnwindPlan(const Address &addr,
15423594291SPavel Labath                                        UnwindPlan &unwind_plan) {
15523594291SPavel Labath   return GetUnwindPlan(AddressRange(addr, 1), unwind_plan);
15623594291SPavel Labath }
15723594291SPavel Labath 
GetUnwindPlan(const AddressRange & range,UnwindPlan & unwind_plan)15823594291SPavel Labath bool DWARFCallFrameInfo::GetUnwindPlan(const AddressRange &range,
15923594291SPavel Labath                                        UnwindPlan &unwind_plan) {
16074303829SJason Molenda   FDEEntryMap::Entry fde_entry;
16123594291SPavel Labath   Address addr = range.GetBaseAddress();
16274303829SJason Molenda 
16305097246SAdrian Prantl   // Make sure that the Address we're searching for is the same object file as
16405097246SAdrian Prantl   // this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
16574303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
166b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
167b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
168fbcb7f2cSJason Molenda     return false;
16974303829SJason Molenda 
17023594291SPavel Labath   if (llvm::Optional<FDEEntryMap::Entry> entry = GetFirstFDEEntryInRange(range))
17123594291SPavel Labath     return FDEToUnwindPlan(entry->data, addr, unwind_plan);
17274303829SJason Molenda   return false;
17330fdc8d8SChris Lattner }
17430fdc8d8SChris Lattner 
GetAddressRange(Address addr,AddressRange & range)175b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) {
17674303829SJason Molenda 
17705097246SAdrian Prantl   // Make sure that the Address we're searching for is the same object file as
17805097246SAdrian Prantl   // this DWARFCallFrameInfo, we only store File offsets in m_fde_index.
17974303829SJason Molenda   ModuleSP module_sp = addr.GetModule();
180b9c1b51eSKate Stone   if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr ||
181b9c1b51eSKate Stone       module_sp->GetObjectFile() != &m_objfile)
18274303829SJason Molenda     return false;
18374303829SJason Molenda 
184d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
185fbcb7f2cSJason Molenda     return false;
186fbcb7f2cSJason Molenda   GetFDEIndex();
187b9c1b51eSKate Stone   FDEEntryMap::Entry *fde_entry =
188b9c1b51eSKate Stone       m_fde_index.FindEntryThatContains(addr.GetFileAddress());
18974303829SJason Molenda   if (!fde_entry)
190fbcb7f2cSJason Molenda     return false;
191fbcb7f2cSJason Molenda 
192b9c1b51eSKate Stone   range = AddressRange(fde_entry->base, fde_entry->size,
193b9c1b51eSKate Stone                        m_objfile.GetSectionList());
194fbcb7f2cSJason Molenda   return true;
19530fdc8d8SChris Lattner }
19630fdc8d8SChris Lattner 
19723594291SPavel Labath llvm::Optional<DWARFCallFrameInfo::FDEEntryMap::Entry>
GetFirstFDEEntryInRange(const AddressRange & range)19823594291SPavel Labath DWARFCallFrameInfo::GetFirstFDEEntryInRange(const AddressRange &range) {
19923594291SPavel Labath   if (!m_section_sp || m_section_sp->IsEncrypted())
20023594291SPavel Labath     return llvm::None;
20174303829SJason Molenda 
20274303829SJason Molenda   GetFDEIndex();
20374303829SJason Molenda 
20423594291SPavel Labath   addr_t start_file_addr = range.GetBaseAddress().GetFileAddress();
20523594291SPavel Labath   const FDEEntryMap::Entry *fde =
20623594291SPavel Labath       m_fde_index.FindEntryThatContainsOrFollows(start_file_addr);
20723594291SPavel Labath   if (fde && fde->DoesIntersect(
20823594291SPavel Labath                  FDEEntryMap::Range(start_file_addr, range.GetByteSize())))
20923594291SPavel Labath     return *fde;
21074303829SJason Molenda 
21123594291SPavel Labath   return llvm::None;
21230fdc8d8SChris Lattner }
21330fdc8d8SChris Lattner 
GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector & function_info)214b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFunctionAddressAndSizeVector(
215b9c1b51eSKate Stone     FunctionAddressAndSizeVector &function_info) {
2165635f77aSJason Molenda   GetFDEIndex();
2175635f77aSJason Molenda   const size_t count = m_fde_index.GetSize();
218ee7593fbSJason Molenda   function_info.Clear();
219c1946cd8SJason Molenda   if (count > 0)
220ee7593fbSJason Molenda     function_info.Reserve(count);
221b9c1b51eSKate Stone   for (size_t i = 0; i < count; ++i) {
222b9c1b51eSKate Stone     const FDEEntryMap::Entry *func_offset_data_entry =
223b9c1b51eSKate Stone         m_fde_index.GetEntryAtIndex(i);
224b9c1b51eSKate Stone     if (func_offset_data_entry) {
225b9c1b51eSKate Stone       FunctionAddressAndSizeVector::Entry function_offset_entry(
226b9c1b51eSKate Stone           func_offset_data_entry->base, func_offset_data_entry->size);
2275635f77aSJason Molenda       function_info.Append(function_offset_entry);
2285635f77aSJason Molenda     }
2295635f77aSJason Molenda   }
2305635f77aSJason Molenda }
2315635f77aSJason Molenda 
23230fdc8d8SChris Lattner const DWARFCallFrameInfo::CIE *
GetCIE(dw_offset_t cie_offset)233b9c1b51eSKate Stone DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) {
23430fdc8d8SChris Lattner   cie_map_t::iterator pos = m_cie_map.find(cie_offset);
23530fdc8d8SChris Lattner 
236b9c1b51eSKate Stone   if (pos != m_cie_map.end()) {
23730fdc8d8SChris Lattner     // Parse and cache the CIE
23870355aceSJonas Devlieghere     if (pos->second == nullptr)
23930fdc8d8SChris Lattner       pos->second = ParseCIE(cie_offset);
24030fdc8d8SChris Lattner 
24130fdc8d8SChris Lattner     return pos->second.get();
24230fdc8d8SChris Lattner   }
243d4612ad0SEd Maste   return nullptr;
24430fdc8d8SChris Lattner }
24530fdc8d8SChris Lattner 
246fbcb7f2cSJason Molenda DWARFCallFrameInfo::CIESP
ParseCIE(const dw_offset_t cie_offset)247b9c1b51eSKate Stone DWARFCallFrameInfo::ParseCIE(const dw_offset_t cie_offset) {
248fbcb7f2cSJason Molenda   CIESP cie_sp(new CIE(cie_offset));
249c7bece56SGreg Clayton   lldb::offset_t offset = cie_offset;
250a6682a41SJonas Devlieghere   if (!m_cfi_data_initialized)
251f97c5213SGreg Clayton     GetCFIData();
2521767e115STodd Fiala   uint32_t length = m_cfi_data.GetU32(&offset);
2531767e115STodd Fiala   dw_offset_t cie_id, end_offset;
2541767e115STodd Fiala   bool is_64bit = (length == UINT32_MAX);
2551767e115STodd Fiala   if (is_64bit) {
2561767e115STodd Fiala     length = m_cfi_data.GetU64(&offset);
2571767e115STodd Fiala     cie_id = m_cfi_data.GetU64(&offset);
2581767e115STodd Fiala     end_offset = cie_offset + length + 12;
2591767e115STodd Fiala   } else {
2601767e115STodd Fiala     cie_id = m_cfi_data.GetU32(&offset);
2611767e115STodd Fiala     end_offset = cie_offset + length + 4;
2621767e115STodd Fiala   }
2633f2a0818SPavel Labath   if (length > 0 && ((m_type == DWARF && cie_id == UINT32_MAX) ||
2643f2a0818SPavel Labath                      (m_type == EH && cie_id == 0ul))) {
26530fdc8d8SChris Lattner     size_t i;
26630fdc8d8SChris Lattner     //    cie.offset = cie_offset;
26730fdc8d8SChris Lattner     //    cie.length = length;
26830fdc8d8SChris Lattner     //    cie.cieID = cieID;
2696e264d39SAshok Thirumurthi     cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
27030fdc8d8SChris Lattner     cie_sp->version = m_cfi_data.GetU8(&offset);
271cdda23ebSPavel Labath     if (cie_sp->version > CFI_VERSION4) {
272*68793919SJonas Devlieghere       Debugger::ReportError(
273*68793919SJonas Devlieghere           llvm::formatv("CIE parse error: CFI version {0} is not supported",
274*68793919SJonas Devlieghere                         cie_sp->version));
275cdda23ebSPavel Labath       return nullptr;
276cdda23ebSPavel Labath     }
27730fdc8d8SChris Lattner 
278b9c1b51eSKate Stone     for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) {
27930fdc8d8SChris Lattner       cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
280b9c1b51eSKate Stone       if (cie_sp->augmentation[i] == '\0') {
28130fdc8d8SChris Lattner         // Zero out remaining bytes in augmentation string
28230fdc8d8SChris Lattner         for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j)
28330fdc8d8SChris Lattner           cie_sp->augmentation[j] = '\0';
28430fdc8d8SChris Lattner 
28530fdc8d8SChris Lattner         break;
28630fdc8d8SChris Lattner       }
28730fdc8d8SChris Lattner     }
28830fdc8d8SChris Lattner 
289b9c1b51eSKate Stone     if (i == CFI_AUG_MAX_SIZE &&
290b9c1b51eSKate Stone         cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') {
291*68793919SJonas Devlieghere       Debugger::ReportError(llvm::formatv(
292b9c1b51eSKate Stone           "CIE parse error: CIE augmentation string was too large "
293*68793919SJonas Devlieghere           "for the fixed sized buffer of {0} bytes.",
294*68793919SJonas Devlieghere           CFI_AUG_MAX_SIZE));
295cdda23ebSPavel Labath       return nullptr;
29630fdc8d8SChris Lattner     }
297cdda23ebSPavel Labath 
29805097246SAdrian Prantl     // m_cfi_data uses address size from target architecture of the process may
29905097246SAdrian Prantl     // ignore these fields?
3003f2a0818SPavel Labath     if (m_type == DWARF && cie_sp->version >= CFI_VERSION4) {
301cdda23ebSPavel Labath       cie_sp->address_size = m_cfi_data.GetU8(&offset);
302cdda23ebSPavel Labath       cie_sp->segment_size = m_cfi_data.GetU8(&offset);
303cdda23ebSPavel Labath     }
304cdda23ebSPavel Labath 
30530fdc8d8SChris Lattner     cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
30630fdc8d8SChris Lattner     cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
307cdda23ebSPavel Labath 
308cdda23ebSPavel Labath     cie_sp->return_addr_reg_num =
3093f2a0818SPavel Labath         m_type == DWARF && cie_sp->version >= CFI_VERSION3
310cdda23ebSPavel Labath             ? static_cast<uint32_t>(m_cfi_data.GetULEB128(&offset))
311cdda23ebSPavel Labath             : m_cfi_data.GetU8(&offset);
31230fdc8d8SChris Lattner 
313b9c1b51eSKate Stone     if (cie_sp->augmentation[0]) {
31405097246SAdrian Prantl       // Get the length of the eh_frame augmentation data which starts with a
31505097246SAdrian Prantl       // ULEB128 length in bytes
31630fdc8d8SChris Lattner       const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
31730fdc8d8SChris Lattner       const size_t aug_data_end = offset + aug_data_len;
31830fdc8d8SChris Lattner       const size_t aug_str_len = strlen(cie_sp->augmentation);
31930fdc8d8SChris Lattner       // A 'z' may be present as the first character of the string.
32005097246SAdrian Prantl       // If present, the Augmentation Data field shall be present. The contents
32105097246SAdrian Prantl       // of the Augmentation Data shall be interpreted according to other
32205097246SAdrian Prantl       // characters in the Augmentation String.
323b9c1b51eSKate Stone       if (cie_sp->augmentation[0] == 'z') {
32430fdc8d8SChris Lattner         // Extract the Augmentation Data
32530fdc8d8SChris Lattner         size_t aug_str_idx = 0;
326b9c1b51eSKate Stone         for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) {
32730fdc8d8SChris Lattner           char aug = cie_sp->augmentation[aug_str_idx];
328b9c1b51eSKate Stone           switch (aug) {
32930fdc8d8SChris Lattner           case 'L':
33005097246SAdrian Prantl             // Indicates the presence of one argument in the Augmentation Data
33105097246SAdrian Prantl             // of the CIE, and a corresponding argument in the Augmentation
33205097246SAdrian Prantl             // Data of the FDE. The argument in the Augmentation Data of the
33305097246SAdrian Prantl             // CIE is 1-byte and represents the pointer encoding used for the
33405097246SAdrian Prantl             // argument in the Augmentation Data of the FDE, which is the
33505097246SAdrian Prantl             // address of a language-specific data area (LSDA). The size of the
33605097246SAdrian Prantl             // LSDA pointer is specified by the pointer encoding used.
337e9c7ecf6SJason Molenda             cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
33830fdc8d8SChris Lattner             break;
33930fdc8d8SChris Lattner 
34030fdc8d8SChris Lattner           case 'P':
34105097246SAdrian Prantl             // Indicates the presence of two arguments in the Augmentation Data
34205097246SAdrian Prantl             // of the CIE. The first argument is 1-byte and represents the
34305097246SAdrian Prantl             // pointer encoding used for the second argument, which is the
34405097246SAdrian Prantl             // address of a personality routine handler. The size of the
34505097246SAdrian Prantl             // personality routine pointer is specified by the pointer encoding
34605097246SAdrian Prantl             // used.
347e9c7ecf6SJason Molenda             //
34805097246SAdrian Prantl             // The address of the personality function will be stored at this
34905097246SAdrian Prantl             // location.  Pre-execution, it will be all zero's so don't read it
35005097246SAdrian Prantl             // until we're 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
36305097246SAdrian Prantl             // first character of the string. The Augmentation Data shall
36405097246SAdrian Prantl             // include a 1 byte argument that represents the pointer encoding
36505097246SAdrian Prantl             // for the address pointers used in the FDE. Example: 0x1B ==
36605097246SAdrian Prantl             // DW_EH_PE_pcrel | DW_EH_PE_sdata4
36730fdc8d8SChris Lattner             cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
36830fdc8d8SChris Lattner             break;
36930fdc8d8SChris Lattner           }
37030fdc8d8SChris Lattner         }
371b9c1b51eSKate Stone       } else if (strcmp(cie_sp->augmentation, "eh") == 0) {
37205097246SAdrian Prantl         // If the Augmentation string has the value "eh", then the EH Data
37305097246SAdrian Prantl         // field shall be present
37430fdc8d8SChris Lattner       }
37530fdc8d8SChris Lattner 
37605097246SAdrian Prantl       // Set the offset to be the end of the augmentation data just in case we
37705097246SAdrian Prantl       // didn't understand any of the data.
37830fdc8d8SChris Lattner       offset = (uint32_t)aug_data_end;
37930fdc8d8SChris Lattner     }
38030fdc8d8SChris Lattner 
381b9c1b51eSKate Stone     if (end_offset > offset) {
38230fdc8d8SChris Lattner       cie_sp->inst_offset = offset;
38330fdc8d8SChris Lattner       cie_sp->inst_length = end_offset - offset;
38430fdc8d8SChris Lattner     }
385b9c1b51eSKate Stone     while (offset < end_offset) {
386fbcb7f2cSJason Molenda       uint8_t inst = m_cfi_data.GetU8(&offset);
387fbcb7f2cSJason Molenda       uint8_t primary_opcode = inst & 0xC0;
388fbcb7f2cSJason Molenda       uint8_t extended_opcode = inst & 0x3F;
389fbcb7f2cSJason Molenda 
390b9c1b51eSKate Stone       if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode,
391b9c1b51eSKate Stone                                    cie_sp->data_align, offset,
392b9c1b51eSKate Stone                                    cie_sp->initial_row))
393fbcb7f2cSJason Molenda         break; // Stop if we hit an unrecognized opcode
394fbcb7f2cSJason Molenda     }
39530fdc8d8SChris Lattner   }
39630fdc8d8SChris Lattner 
39730fdc8d8SChris Lattner   return cie_sp;
39830fdc8d8SChris Lattner }
39930fdc8d8SChris Lattner 
GetCFIData()400b9c1b51eSKate Stone void DWARFCallFrameInfo::GetCFIData() {
401a6682a41SJonas Devlieghere   if (!m_cfi_data_initialized) {
402a007a6d8SPavel Labath     Log *log = GetLog(LLDBLog::Unwind);
403f97c5213SGreg Clayton     if (log)
4045160ce5cSGreg Clayton       m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
405c9660546SGreg Clayton     m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data);
406f97c5213SGreg Clayton     m_cfi_data_initialized = true;
407f97c5213SGreg Clayton   }
408f97c5213SGreg Clayton }
409b9c1b51eSKate Stone // Scan through the eh_frame or debug_frame section looking for FDEs and noting
41005097246SAdrian Prantl // the start/end addresses of the functions and a pointer back to the
41105097246SAdrian Prantl // function's FDE for later expansion. Internalize CIEs as we come across them.
41230fdc8d8SChris Lattner 
GetFDEIndex()413b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFDEIndex() {
414d4612ad0SEd Maste   if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
415fbcb7f2cSJason Molenda     return;
4167d69f12eSSean Callanan 
417fbcb7f2cSJason Molenda   if (m_fde_index_initialized)
418fbcb7f2cSJason Molenda     return;
419fbcb7f2cSJason Molenda 
420bb19a13cSSaleem Abdulrasool   std::lock_guard<std::mutex> guard(m_fde_index_mutex);
4217d69f12eSSean Callanan 
4227d69f12eSSean Callanan   if (m_fde_index_initialized) // if two threads hit the locker
4237d69f12eSSean Callanan     return;
424e6194f17SJason Molenda 
4255c1c8443SJonas Devlieghere   LLDB_SCOPED_TIMERF("%s - %s", LLVM_PRETTY_FUNCTION,
426b9c1b51eSKate Stone                      m_objfile.GetFileSpec().GetFilename().AsCString(""));
42774303829SJason Molenda 
428c7afda5aSJason Molenda   bool clear_address_zeroth_bit = false;
429f760f5aeSPavel Labath   if (ArchSpec arch = m_objfile.GetArchitecture()) {
430b9c1b51eSKate Stone     if (arch.GetTriple().getArch() == llvm::Triple::arm ||
431b9c1b51eSKate Stone         arch.GetTriple().getArch() == llvm::Triple::thumb)
432c7afda5aSJason Molenda       clear_address_zeroth_bit = true;
433c7afda5aSJason Molenda   }
434c7afda5aSJason Molenda 
435c7bece56SGreg Clayton   lldb::offset_t offset = 0;
436a6682a41SJonas Devlieghere   if (!m_cfi_data_initialized)
437f97c5213SGreg Clayton     GetCFIData();
438b9c1b51eSKate Stone   while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) {
43973bf5dbdSGreg Clayton     const dw_offset_t current_entry = offset;
4401767e115STodd Fiala     dw_offset_t cie_id, next_entry, cie_offset;
441fbcb7f2cSJason Molenda     uint32_t len = m_cfi_data.GetU32(&offset);
4421767e115STodd Fiala     bool is_64bit = (len == UINT32_MAX);
4431767e115STodd Fiala     if (is_64bit) {
4441767e115STodd Fiala       len = m_cfi_data.GetU64(&offset);
4451767e115STodd Fiala       cie_id = m_cfi_data.GetU64(&offset);
4461767e115STodd Fiala       next_entry = current_entry + len + 12;
4471767e115STodd Fiala       cie_offset = current_entry + 12 - cie_id;
4481767e115STodd Fiala     } else {
4491767e115STodd Fiala       cie_id = m_cfi_data.GetU32(&offset);
4501767e115STodd Fiala       next_entry = current_entry + len + 4;
4511767e115STodd Fiala       cie_offset = current_entry + 4 - cie_id;
4521767e115STodd Fiala     }
453fbcb7f2cSJason Molenda 
454b9c1b51eSKate Stone     if (next_entry > m_cfi_data.GetByteSize() + 1) {
455*68793919SJonas Devlieghere       Debugger::ReportError(llvm::formatv("Invalid fde/cie next entry offset "
456*68793919SJonas Devlieghere                                           "of {0:x} found in cie/fde at {1:x}",
457*68793919SJonas Devlieghere                                           next_entry, current_entry));
458e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
459c48ef341SJason Molenda       // invalid data.
460c48ef341SJason Molenda       m_fde_index.Clear();
461c48ef341SJason Molenda       m_fde_index_initialized = true;
462c48ef341SJason Molenda       return;
463c48ef341SJason Molenda     }
464cdda23ebSPavel Labath 
465cdda23ebSPavel Labath     // An FDE entry contains CIE_pointer in debug_frame in same place as cie_id
46605097246SAdrian Prantl     // in eh_frame. CIE_pointer is an offset into the .debug_frame section. So,
46705097246SAdrian Prantl     // variable cie_offset should be equal to cie_id for debug_frame.
468cdda23ebSPavel Labath     // FDE entries with cie_id == 0 shouldn't be ignored for it.
4693f2a0818SPavel Labath     if ((cie_id == 0 && m_type == EH) || cie_id == UINT32_MAX || len == 0) {
470cdda23ebSPavel Labath       auto cie_sp = ParseCIE(current_entry);
471cdda23ebSPavel Labath       if (!cie_sp) {
472cdda23ebSPavel Labath         // Cannot parse, the reason is already logged
473cdda23ebSPavel Labath         m_fde_index.Clear();
474cdda23ebSPavel Labath         m_fde_index_initialized = true;
475cdda23ebSPavel Labath         return;
476cdda23ebSPavel Labath       }
477cdda23ebSPavel Labath 
478cdda23ebSPavel Labath       m_cie_map[current_entry] = std::move(cie_sp);
479cdda23ebSPavel Labath       offset = next_entry;
480cdda23ebSPavel Labath       continue;
481cdda23ebSPavel Labath     }
482cdda23ebSPavel Labath 
4833f2a0818SPavel Labath     if (m_type == DWARF)
484cdda23ebSPavel Labath       cie_offset = cie_id;
485cdda23ebSPavel Labath 
486b9c1b51eSKate Stone     if (cie_offset > m_cfi_data.GetByteSize()) {
487*68793919SJonas Devlieghere       Debugger::ReportError(llvm::formatv("Invalid cie offset of {0:x} "
488*68793919SJonas Devlieghere                                           "found in cie/fde at {1:x}",
489*68793919SJonas Devlieghere                                           cie_offset, current_entry));
490e171da5cSBruce Mitchener       // Don't trust anything in this eh_frame section if we find blatantly
491c48ef341SJason Molenda       // invalid data.
492c48ef341SJason Molenda       m_fde_index.Clear();
493c48ef341SJason Molenda       m_fde_index_initialized = true;
494c48ef341SJason Molenda       return;
495c48ef341SJason Molenda     }
496c48ef341SJason Molenda 
49773bf5dbdSGreg Clayton     const CIE *cie = GetCIE(cie_offset);
498b9c1b51eSKate Stone     if (cie) {
499c9660546SGreg Clayton       const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
500fbcb7f2cSJason Molenda       const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
501fbcb7f2cSJason Molenda       const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
502fbcb7f2cSJason Molenda 
50329cb868aSZachary Turner       lldb::addr_t addr =
50429cb868aSZachary Turner           GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr,
50529cb868aSZachary Turner                           text_addr, data_addr);
506c7afda5aSJason Molenda       if (clear_address_zeroth_bit)
507c7afda5aSJason Molenda         addr &= ~1ull;
508c7afda5aSJason Molenda 
50929cb868aSZachary Turner       lldb::addr_t length = GetGNUEHPointer(
51029cb868aSZachary Turner           m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING,
51129cb868aSZachary Turner           pc_rel_addr, text_addr, data_addr);
51274303829SJason Molenda       FDEEntryMap::Entry fde(addr, length, current_entry);
51374303829SJason Molenda       m_fde_index.Append(fde);
514b9c1b51eSKate Stone     } else {
515*68793919SJonas Devlieghere       Debugger::ReportError(llvm::formatv(
516*68793919SJonas Devlieghere           "unable to find CIE at {0:x} for cie_id = {1:x} for entry at {2:x}.",
517*68793919SJonas Devlieghere           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 
FDEToUnwindPlan(dw_offset_t dwarf_offset,Address startaddr,UnwindPlan & unwind_plan)525b9c1b51eSKate Stone bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset,
526b9c1b51eSKate Stone                                          Address startaddr,
527b9c1b51eSKate Stone                                          UnwindPlan &unwind_plan) {
528a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::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   }
6043fd917d8SJoseph Tremoulet   unwind_plan.SetUnwindPlanForSignalTrap(
6053fd917d8SJoseph Tremoulet     strchr(cie->augmentation, 'S') ? eLazyBoolYes : eLazyBoolNo);
6063fd917d8SJoseph Tremoulet 
607e9c7ecf6SJason Molenda   Address lsda_data;
608e9c7ecf6SJason Molenda   Address personality_function_ptr;
609e9c7ecf6SJason Molenda 
610b9c1b51eSKate Stone   if (lsda_data_file_address != LLDB_INVALID_ADDRESS &&
611b9c1b51eSKate Stone       cie->personality_loc != LLDB_INVALID_ADDRESS) {
612b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address,
613b9c1b51eSKate Stone                                               lsda_data);
614b9c1b51eSKate Stone     m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc,
615b9c1b51eSKate Stone                                               personality_function_ptr);
616e9c7ecf6SJason Molenda   }
617e9c7ecf6SJason Molenda 
618b9c1b51eSKate Stone   if (lsda_data.IsValid() && personality_function_ptr.IsValid()) {
619e9c7ecf6SJason Molenda     unwind_plan.SetLSDAAddress(lsda_data);
620e9c7ecf6SJason Molenda     unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr);
621e9c7ecf6SJason Molenda   }
62230fdc8d8SChris Lattner 
62330fdc8d8SChris Lattner   uint32_t code_align = cie->code_align;
62430fdc8d8SChris Lattner   int32_t data_align = cie->data_align;
62530fdc8d8SChris Lattner 
626fbcb7f2cSJason Molenda   unwind_plan.SetPlanValidAddressRange(range);
6271d42c7bcSJason Molenda   UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
6281d42c7bcSJason Molenda   *cie_initial_row = cie->initial_row;
6291d42c7bcSJason Molenda   UnwindPlan::RowSP row(cie_initial_row);
63030fdc8d8SChris Lattner 
6313f2a0818SPavel Labath   unwind_plan.SetRegisterKind(GetRegisterKind());
6328eba46c6SJason Molenda   unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num);
633fbcb7f2cSJason Molenda 
634f70d6040SRichard Mitton   std::vector<UnwindPlan::RowSP> stack;
635f70d6040SRichard Mitton 
636fbcb7f2cSJason Molenda   UnwindPlan::Row::RegisterLocation reg_location;
637b9c1b51eSKate Stone   while (m_cfi_data.ValidOffset(offset) && offset < end_offset) {
63830fdc8d8SChris Lattner     uint8_t inst = m_cfi_data.GetU8(&offset);
63930fdc8d8SChris Lattner     uint8_t primary_opcode = inst & 0xC0;
64030fdc8d8SChris Lattner     uint8_t extended_opcode = inst & 0x3F;
64130fdc8d8SChris Lattner 
642b9c1b51eSKate Stone     if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align,
643b9c1b51eSKate Stone                                  offset, *row)) {
644b9c1b51eSKate Stone       if (primary_opcode) {
645b9c1b51eSKate Stone         switch (primary_opcode) {
64630fdc8d8SChris Lattner         case DW_CFA_advance_loc: // (Row Creation Instruction)
64730fdc8d8SChris Lattner         { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
64830fdc8d8SChris Lattner           // takes a single argument that represents a constant delta. The
64905097246SAdrian Prantl           // required action is to create a new table row with a location value
65005097246SAdrian Prantl           // that is computed by taking the current entry's location value and
65105097246SAdrian Prantl           // adding (delta * code_align). All other values in the new row are
65205097246SAdrian Prantl           // initially identical to the current row.
653fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
654fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
655fa67e879SJason Molenda           *newrow = *row.get();
656fa67e879SJason Molenda           row.reset(newrow);
6571d42c7bcSJason Molenda           row->SlideOffset(extended_opcode * code_align);
65830fdc8d8SChris Lattner           break;
65930fdc8d8SChris Lattner         }
66030fdc8d8SChris Lattner 
661b9c1b51eSKate Stone         case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are
662b9c1b51eSKate Stone                                // register
66330fdc8d8SChris Lattner           // takes a single argument that represents a register number. The
66430fdc8d8SChris Lattner           // required action is to change the rule for the indicated register
66530fdc8d8SChris Lattner           // to the rule assigned it by the initial_instructions in the CIE.
666623652a0STamas Berghammer           uint32_t reg_num = extended_opcode;
66705097246SAdrian Prantl           // We only keep enough register locations around to unwind what is in
66805097246SAdrian Prantl           // our thread, and these are organized by the register index in that
66905097246SAdrian Prantl           // state, so we need to convert our eh_frame register number from the
67005097246SAdrian Prantl           // EH frame info, to a register index
67130fdc8d8SChris Lattner 
672b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
673b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
674b9c1b51eSKate Stone                                                             reg_location))
6751d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
67630fdc8d8SChris Lattner           break;
67730fdc8d8SChris Lattner         }
67830fdc8d8SChris Lattner         }
679b9c1b51eSKate Stone       } else {
680b9c1b51eSKate Stone         switch (extended_opcode) {
68130fdc8d8SChris Lattner         case DW_CFA_set_loc: // 0x1 (Row Creation Instruction)
68230fdc8d8SChris Lattner         {
68330fdc8d8SChris Lattner           // DW_CFA_set_loc takes a single argument that represents an address.
68430fdc8d8SChris Lattner           // The required action is to create a new table row using the
68530fdc8d8SChris Lattner           // specified address as the location. All other values in the new row
68630fdc8d8SChris Lattner           // are initially identical to the current row. The new location value
68730fdc8d8SChris Lattner           // should always be greater than the current one.
688fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
689fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
690fa67e879SJason Molenda           *newrow = *row.get();
691fa67e879SJason Molenda           row.reset(newrow);
6920863f675SPavel Labath           row->SetOffset(m_cfi_data.GetAddress(&offset) -
693b9c1b51eSKate Stone                          startaddr.GetFileAddress());
69430fdc8d8SChris Lattner           break;
695623652a0STamas Berghammer         }
69630fdc8d8SChris Lattner 
69730fdc8d8SChris Lattner         case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction)
69830fdc8d8SChris Lattner         {
69930fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
70030fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
70130fdc8d8SChris Lattner           // encoding and size of the delta argument.
702fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
703fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
704fa67e879SJason Molenda           *newrow = *row.get();
705fa67e879SJason Molenda           row.reset(newrow);
7061d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align);
70730fdc8d8SChris Lattner           break;
708623652a0STamas Berghammer         }
70930fdc8d8SChris Lattner 
71030fdc8d8SChris Lattner         case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction)
71130fdc8d8SChris Lattner         {
71230fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
71330fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
71430fdc8d8SChris Lattner           // encoding and size of the delta argument.
715fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
716fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
717fa67e879SJason Molenda           *newrow = *row.get();
718fa67e879SJason Molenda           row.reset(newrow);
7191d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align);
72030fdc8d8SChris Lattner           break;
721623652a0STamas Berghammer         }
72230fdc8d8SChris Lattner 
72330fdc8d8SChris Lattner         case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction)
72430fdc8d8SChris Lattner         {
72530fdc8d8SChris Lattner           // takes a single uword argument that represents a constant delta.
72630fdc8d8SChris Lattner           // This instruction is identical to DW_CFA_advance_loc except for the
72730fdc8d8SChris Lattner           // encoding and size of the delta argument.
728fbcb7f2cSJason Molenda           unwind_plan.AppendRow(row);
729fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
730fa67e879SJason Molenda           *newrow = *row.get();
731fa67e879SJason Molenda           row.reset(newrow);
7321d42c7bcSJason Molenda           row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align);
73330fdc8d8SChris Lattner           break;
73430fdc8d8SChris Lattner         }
73530fdc8d8SChris Lattner 
73630fdc8d8SChris Lattner         case DW_CFA_restore_extended: // 0x6
73730fdc8d8SChris Lattner         {
73830fdc8d8SChris Lattner           // takes a single unsigned LEB128 argument that represents a register
73930fdc8d8SChris Lattner           // number. This instruction is identical to DW_CFA_restore except for
74030fdc8d8SChris Lattner           // the encoding and size of the register argument.
741623652a0STamas Berghammer           uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
742b9c1b51eSKate Stone           if (unwind_plan.IsValidRowIndex(0) &&
743b9c1b51eSKate Stone               unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num,
744b9c1b51eSKate Stone                                                             reg_location))
7451d42c7bcSJason Molenda             row->SetRegisterInfo(reg_num, reg_location);
74630fdc8d8SChris Lattner           break;
74730fdc8d8SChris Lattner         }
74830fdc8d8SChris Lattner 
74930fdc8d8SChris Lattner         case DW_CFA_remember_state: // 0xA
750fa67e879SJason Molenda         {
75130fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
75205097246SAdrian Prantl           // DW_CFA_remember_state instruction means to save the rules for
75305097246SAdrian Prantl           // every register on the current row on the stack. Encountering the
75430fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
75530fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
75630fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
75730fdc8d8SChris Lattner           // function.)
758f70d6040SRichard Mitton           stack.push_back(row);
759fa67e879SJason Molenda           UnwindPlan::Row *newrow = new UnwindPlan::Row;
760fa67e879SJason Molenda           *newrow = *row.get();
761fa67e879SJason Molenda           row.reset(newrow);
76230fdc8d8SChris Lattner           break;
763623652a0STamas Berghammer         }
76430fdc8d8SChris Lattner 
76530fdc8d8SChris Lattner         case DW_CFA_restore_state: // 0xB
766623652a0STamas Berghammer         {
76730fdc8d8SChris Lattner           // These instructions define a stack of information. Encountering the
76805097246SAdrian Prantl           // DW_CFA_remember_state instruction means to save the rules for
76905097246SAdrian Prantl           // every register on the current row on the stack. Encountering the
77030fdc8d8SChris Lattner           // DW_CFA_restore_state instruction means to pop the set of rules off
77130fdc8d8SChris Lattner           // the stack and place them in the current row. (This operation is
77230fdc8d8SChris Lattner           // useful for compilers that move epilogue code into the body of a
77330fdc8d8SChris Lattner           // function.)
774b9c1b51eSKate Stone           if (stack.empty()) {
77563e5fb76SJonas Devlieghere             LLDB_LOGF(log,
77663e5fb76SJonas Devlieghere                       "DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32
777b9c1b51eSKate Stone                       ", startaddr: %" PRIx64
778b9c1b51eSKate Stone                       " encountered DW_CFA_restore_state but state stack "
779b9c1b51eSKate Stone                       "is empty. Corrupt unwind info?",
78063e5fb76SJonas Devlieghere                       __FUNCTION__, dwarf_offset, startaddr.GetFileAddress());
7811ee89eb8SPavel Labath             break;
7821ee89eb8SPavel Labath           }
78349dd6d15STamas Berghammer           lldb::addr_t offset = row->GetOffset();
784f70d6040SRichard Mitton           row = stack.back();
785f70d6040SRichard Mitton           stack.pop_back();
78649dd6d15STamas Berghammer           row->SetOffset(offset);
78730fdc8d8SChris Lattner           break;
788623652a0STamas Berghammer         }
789623652a0STamas Berghammer 
7901ee89eb8SPavel Labath         case DW_CFA_GNU_args_size: // 0x2e
7911ee89eb8SPavel Labath         {
792b9c1b51eSKate Stone           // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128
79305097246SAdrian Prantl           // operand representing an argument size. This instruction specifies
79405097246SAdrian Prantl           // the total of the size of the arguments which have been pushed onto
79505097246SAdrian Prantl           // the stack.
7961ee89eb8SPavel Labath 
7971ee89eb8SPavel Labath           // TODO: Figure out how we should handle this.
7981ee89eb8SPavel Labath           m_cfi_data.GetULEB128(&offset);
799a41deb1cSJason Molenda           break;
8001ee89eb8SPavel Labath         }
8011ee89eb8SPavel Labath 
802623652a0STamas Berghammer         case DW_CFA_val_offset:    // 0x14
803623652a0STamas Berghammer         case DW_CFA_val_offset_sf: // 0x15
804623652a0STamas Berghammer         default:
805623652a0STamas Berghammer           break;
806623652a0STamas Berghammer         }
807623652a0STamas Berghammer       }
808623652a0STamas Berghammer     }
809623652a0STamas Berghammer   }
810623652a0STamas Berghammer   unwind_plan.AppendRow(row);
811623652a0STamas Berghammer 
812623652a0STamas Berghammer   return true;
813623652a0STamas Berghammer }
814623652a0STamas Berghammer 
HandleCommonDwarfOpcode(uint8_t primary_opcode,uint8_t extended_opcode,int32_t data_align,lldb::offset_t & offset,UnwindPlan::Row & row)815b9c1b51eSKate Stone bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
816623652a0STamas Berghammer                                                  uint8_t extended_opcode,
817623652a0STamas Berghammer                                                  int32_t data_align,
818623652a0STamas Berghammer                                                  lldb::offset_t &offset,
819b9c1b51eSKate Stone                                                  UnwindPlan::Row &row) {
820623652a0STamas Berghammer   UnwindPlan::Row::RegisterLocation reg_location;
821623652a0STamas Berghammer 
822b9c1b51eSKate Stone   if (primary_opcode) {
823b9c1b51eSKate Stone     switch (primary_opcode) {
824b9c1b51eSKate Stone     case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are
825b9c1b51eSKate Stone                           // register
826623652a0STamas Berghammer       // takes two arguments: an unsigned LEB128 constant representing a
827623652a0STamas Berghammer       // factored offset and a register number. The required action is to
82805097246SAdrian Prantl       // change the rule for the register indicated by the register number to
82905097246SAdrian Prantl       // be an offset(N) rule with a value of (N = factored offset *
83005097246SAdrian Prantl       // data_align).
831623652a0STamas Berghammer       uint8_t reg_num = extended_opcode;
832623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
833623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
834623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
835623652a0STamas Berghammer       return true;
836623652a0STamas Berghammer     }
837623652a0STamas Berghammer     }
838b9c1b51eSKate Stone   } else {
839b9c1b51eSKate Stone     switch (extended_opcode) {
840623652a0STamas Berghammer     case DW_CFA_nop: // 0x0
841623652a0STamas Berghammer       return true;
842623652a0STamas Berghammer 
843623652a0STamas Berghammer     case DW_CFA_offset_extended: // 0x5
844623652a0STamas Berghammer     {
84505097246SAdrian Prantl       // takes two unsigned LEB128 arguments representing a register number and
84605097246SAdrian Prantl       // a factored offset. This instruction is identical to DW_CFA_offset
847623652a0STamas Berghammer       // except for the encoding and size of the register argument.
848623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
849623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
850623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
851623652a0STamas Berghammer       reg_location.SetAtCFAPlusOffset(op_offset);
852623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
853623652a0STamas Berghammer       return true;
854623652a0STamas Berghammer     }
855623652a0STamas Berghammer 
856623652a0STamas Berghammer     case DW_CFA_undefined: // 0x7
857623652a0STamas Berghammer     {
858623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
859623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
860623652a0STamas Berghammer       // register to undefined.
861623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
862623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
863623652a0STamas Berghammer       reg_location.SetUndefined();
864623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
865623652a0STamas Berghammer       return true;
866623652a0STamas Berghammer     }
867623652a0STamas Berghammer 
868623652a0STamas Berghammer     case DW_CFA_same_value: // 0x8
869623652a0STamas Berghammer     {
870623652a0STamas Berghammer       // takes a single unsigned LEB128 argument that represents a register
871623652a0STamas Berghammer       // number. The required action is to set the rule for the specified
872623652a0STamas Berghammer       // register to same value.
873623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
874623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
875623652a0STamas Berghammer       reg_location.SetSame();
876623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
877623652a0STamas Berghammer       return true;
878623652a0STamas Berghammer     }
879623652a0STamas Berghammer 
880623652a0STamas Berghammer     case DW_CFA_register: // 0x9
881623652a0STamas Berghammer     {
88205097246SAdrian Prantl       // takes two unsigned LEB128 arguments representing register numbers. The
88305097246SAdrian Prantl       // required action is to set the rule for the first register to be the
88405097246SAdrian Prantl       // second register.
885623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
886623652a0STamas Berghammer       uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
887623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
888623652a0STamas Berghammer       reg_location.SetInRegister(other_reg_num);
889623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
890623652a0STamas Berghammer       return true;
891623652a0STamas Berghammer     }
89230fdc8d8SChris Lattner 
89330fdc8d8SChris Lattner     case DW_CFA_def_cfa: // 0xC    (CFA Definition Instruction)
89430fdc8d8SChris Lattner     {
89505097246SAdrian Prantl       // Takes two unsigned LEB128 operands representing a register number and
89605097246SAdrian Prantl       // a (non-factored) offset. The required action is to define the current
89705097246SAdrian Prantl       // CFA rule to use the provided register and offset.
898623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
899623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
900623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
901623652a0STamas Berghammer       return true;
90230fdc8d8SChris Lattner     }
90330fdc8d8SChris Lattner 
90430fdc8d8SChris Lattner     case DW_CFA_def_cfa_register: // 0xD    (CFA Definition Instruction)
90530fdc8d8SChris Lattner     {
90630fdc8d8SChris Lattner       // takes a single unsigned LEB128 argument representing a register
90705097246SAdrian Prantl       // number. The required action is to define the current CFA rule to use
90805097246SAdrian Prantl       // the provided register (but to keep the old offset).
909623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
910b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num,
911b9c1b51eSKate Stone                                                 row.GetCFAValue().GetOffset());
912623652a0STamas Berghammer       return true;
91330fdc8d8SChris Lattner     }
91430fdc8d8SChris Lattner 
91530fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset: // 0xE    (CFA Definition Instruction)
91630fdc8d8SChris Lattner     {
91705097246SAdrian Prantl       // Takes a single unsigned LEB128 operand representing a (non-factored)
91805097246SAdrian Prantl       // offset. The required action is to define the current CFA rule to use
91905097246SAdrian Prantl       // the provided offset (but to keep the old register).
920623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
921b9c1b51eSKate Stone       row.GetCFAValue().SetIsRegisterPlusOffset(
922b9c1b51eSKate Stone           row.GetCFAValue().GetRegisterNumber(), op_offset);
923623652a0STamas Berghammer       return true;
92430fdc8d8SChris Lattner     }
92530fdc8d8SChris Lattner 
92630fdc8d8SChris Lattner     case DW_CFA_def_cfa_expression: // 0xF    (CFA Definition Instruction)
92730fdc8d8SChris Lattner     {
92830fdc8d8SChris Lattner       size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
929b9c1b51eSKate Stone       const uint8_t *block_data =
930b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
931623652a0STamas Berghammer       row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
932623652a0STamas Berghammer       return true;
93330fdc8d8SChris Lattner     }
93430fdc8d8SChris Lattner 
93530fdc8d8SChris Lattner     case DW_CFA_expression: // 0x10
93630fdc8d8SChris Lattner     {
93705097246SAdrian Prantl       // Takes two operands: an unsigned LEB128 value representing a register
93805097246SAdrian Prantl       // number, and a DW_FORM_block value representing a DWARF expression. The
93905097246SAdrian Prantl       // required action is to change the rule for the register indicated by
94005097246SAdrian Prantl       // the register number to be an expression(E) rule where E is the DWARF
94105097246SAdrian Prantl       // expression. That is, the DWARF expression computes the address. The
94205097246SAdrian Prantl       // value of the CFA is pushed on the DWARF evaluation stack prior to
94305097246SAdrian Prantl       // execution of the DWARF expression.
944623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
94530fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
946b9c1b51eSKate Stone       const uint8_t *block_data =
947b9c1b51eSKate Stone           static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len));
948623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
94930fdc8d8SChris Lattner       reg_location.SetAtDWARFExpression(block_data, block_len);
950623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
951623652a0STamas Berghammer       return true;
95230fdc8d8SChris Lattner     }
95330fdc8d8SChris Lattner 
95430fdc8d8SChris Lattner     case DW_CFA_offset_extended_sf: // 0x11
95530fdc8d8SChris Lattner     {
95605097246SAdrian Prantl       // takes two operands: an unsigned LEB128 value representing a register
95705097246SAdrian Prantl       // number and a signed LEB128 factored offset. This instruction is
95805097246SAdrian Prantl       // identical to DW_CFA_offset_extended except that the second operand is
95905097246SAdrian Prantl       // signed and factored.
960623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
961623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
962623652a0STamas Berghammer       UnwindPlan::Row::RegisterLocation reg_location;
96330fdc8d8SChris Lattner       reg_location.SetAtCFAPlusOffset(op_offset);
964623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
965623652a0STamas Berghammer       return true;
96630fdc8d8SChris Lattner     }
96730fdc8d8SChris Lattner 
96830fdc8d8SChris Lattner     case DW_CFA_def_cfa_sf: // 0x12   (CFA Definition Instruction)
96930fdc8d8SChris Lattner     {
97005097246SAdrian Prantl       // Takes two operands: an unsigned LEB128 value representing a register
97105097246SAdrian Prantl       // number and a signed LEB128 factored offset. This instruction is
97205097246SAdrian Prantl       // identical to DW_CFA_def_cfa except that the second operand is signed
97305097246SAdrian Prantl       // and factored.
974623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
975623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
976623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset);
977623652a0STamas Berghammer       return true;
97830fdc8d8SChris Lattner     }
97930fdc8d8SChris Lattner 
98030fdc8d8SChris Lattner     case DW_CFA_def_cfa_offset_sf: // 0x13   (CFA Definition Instruction)
98130fdc8d8SChris Lattner     {
98205097246SAdrian Prantl       // takes a signed LEB128 operand representing a factored offset. This
98305097246SAdrian Prantl       // instruction is identical to  DW_CFA_def_cfa_offset except that the
98405097246SAdrian Prantl       // operand is signed and factored.
985623652a0STamas Berghammer       int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
986623652a0STamas Berghammer       uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
987623652a0STamas Berghammer       row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
988623652a0STamas Berghammer       return true;
98930fdc8d8SChris Lattner     }
99030fdc8d8SChris Lattner 
99130fdc8d8SChris Lattner     case DW_CFA_val_expression: // 0x16
99230fdc8d8SChris Lattner     {
99330fdc8d8SChris Lattner       // takes two operands: an unsigned LEB128 value representing a register
99405097246SAdrian Prantl       // number, and a DW_FORM_block value representing a DWARF expression. The
99505097246SAdrian Prantl       // required action is to change the rule for the register indicated by
99605097246SAdrian Prantl       // the register number to be a val_expression(E) rule where E is the
99730fdc8d8SChris Lattner       // DWARF expression. That is, the DWARF expression computes the value of
99830fdc8d8SChris Lattner       // the given register. The value of the CFA is pushed on the DWARF
99930fdc8d8SChris Lattner       // evaluation stack prior to execution of the DWARF expression.
1000623652a0STamas Berghammer       uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
100130fdc8d8SChris Lattner       uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
1002b9c1b51eSKate Stone       const uint8_t *block_data =
1003b9c1b51eSKate Stone           (const uint8_t *)m_cfi_data.GetData(&offset, block_len);
100430fdc8d8SChris Lattner       reg_location.SetIsDWARFExpression(block_data, block_len);
1005623652a0STamas Berghammer       row.SetRegisterInfo(reg_num, reg_location);
1006fbcb7f2cSJason Molenda       return true;
100730fdc8d8SChris Lattner     }
1008623652a0STamas Berghammer     }
1009623652a0STamas Berghammer   }
1010623652a0STamas Berghammer   return false;
1011623652a0STamas Berghammer }
10126b63b148STamas Berghammer 
ForEachFDEEntries(const std::function<bool (lldb::addr_t,uint32_t,dw_offset_t)> & callback)1013b9c1b51eSKate Stone void DWARFCallFrameInfo::ForEachFDEEntries(
1014b9c1b51eSKate Stone     const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) {
10156b63b148STamas Berghammer   GetFDEIndex();
10166b63b148STamas Berghammer 
1017b9c1b51eSKate Stone   for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) {
10186b63b148STamas Berghammer     const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i);
10196b63b148STamas Berghammer     if (!callback(entry.base, entry.size, entry.data))
10206b63b148STamas Berghammer       break;
10216b63b148STamas Berghammer   }
10226b63b148STamas Berghammer }
1023