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 //---------------------------------------------------------------------- 2729cb868aSZachary Turner // GetDwarfEHPtr 2829cb868aSZachary Turner // 2905097246SAdrian Prantl // Used for calls when the value type is specified by a DWARF EH Frame pointer 3005097246SAdrian Prantl // encoding. 3129cb868aSZachary Turner //---------------------------------------------------------------------- 3229cb868aSZachary Turner static uint64_t 3329cb868aSZachary Turner GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr, 3429cb868aSZachary Turner uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr, 3529cb868aSZachary Turner addr_t data_addr) //, BSDRelocs *data_relocs) const 3629cb868aSZachary Turner { 3729cb868aSZachary Turner if (eh_ptr_enc == DW_EH_PE_omit) 3829cb868aSZachary Turner return ULLONG_MAX; // Value isn't in the buffer... 3929cb868aSZachary Turner 4029cb868aSZachary Turner uint64_t baseAddress = 0; 4129cb868aSZachary Turner uint64_t addressValue = 0; 4229cb868aSZachary Turner const uint32_t addr_size = DE.GetAddressByteSize(); 4329cb868aSZachary Turner #ifdef LLDB_CONFIGURATION_DEBUG 4429cb868aSZachary Turner assert(addr_size == 4 || addr_size == 8); 4529cb868aSZachary Turner #endif 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 149b9c1b51eSKate Stone DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile &objfile, 1503f2a0818SPavel Labath SectionSP §ion_sp, Type type) 1513f2a0818SPavel Labath : m_objfile(objfile), m_section_sp(section_sp), m_type(type) {} 15230fdc8d8SChris Lattner 153b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetUnwindPlan(Address addr, UnwindPlan &unwind_plan) { 15474303829SJason Molenda FDEEntryMap::Entry fde_entry; 15574303829SJason Molenda 15605097246SAdrian Prantl // Make sure that the Address we're searching for is the same object file as 15705097246SAdrian Prantl // this DWARFCallFrameInfo, we only store File offsets in m_fde_index. 15874303829SJason Molenda ModuleSP module_sp = addr.GetModule(); 159b9c1b51eSKate Stone if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || 160b9c1b51eSKate Stone module_sp->GetObjectFile() != &m_objfile) 161fbcb7f2cSJason Molenda return false; 16274303829SJason Molenda 163a6682a41SJonas Devlieghere if (!GetFDEEntryByFileAddress(addr.GetFileAddress(), fde_entry)) 16474303829SJason Molenda return false; 16574303829SJason Molenda return FDEToUnwindPlan(fde_entry.data, addr, unwind_plan); 16630fdc8d8SChris Lattner } 16730fdc8d8SChris Lattner 168b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) { 16974303829SJason Molenda 17005097246SAdrian Prantl // Make sure that the Address we're searching for is the same object file as 17105097246SAdrian Prantl // this DWARFCallFrameInfo, we only store File offsets in m_fde_index. 17274303829SJason Molenda ModuleSP module_sp = addr.GetModule(); 173b9c1b51eSKate Stone if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || 174b9c1b51eSKate Stone module_sp->GetObjectFile() != &m_objfile) 17574303829SJason Molenda return false; 17674303829SJason Molenda 177d4612ad0SEd Maste if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 178fbcb7f2cSJason Molenda return false; 179fbcb7f2cSJason Molenda GetFDEIndex(); 180b9c1b51eSKate Stone FDEEntryMap::Entry *fde_entry = 181b9c1b51eSKate Stone m_fde_index.FindEntryThatContains(addr.GetFileAddress()); 18274303829SJason Molenda if (!fde_entry) 183fbcb7f2cSJason Molenda return false; 184fbcb7f2cSJason Molenda 185b9c1b51eSKate Stone range = AddressRange(fde_entry->base, fde_entry->size, 186b9c1b51eSKate Stone m_objfile.GetSectionList()); 187fbcb7f2cSJason Molenda return true; 18830fdc8d8SChris Lattner } 18930fdc8d8SChris Lattner 190b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetFDEEntryByFileAddress( 191b9c1b51eSKate Stone addr_t file_addr, FDEEntryMap::Entry &fde_entry) { 192d4612ad0SEd Maste if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 193fbcb7f2cSJason Molenda return false; 19474303829SJason Molenda 19574303829SJason Molenda GetFDEIndex(); 19674303829SJason Molenda 19774303829SJason Molenda if (m_fde_index.IsEmpty()) 19874303829SJason Molenda return false; 19974303829SJason Molenda 20074303829SJason Molenda FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains(file_addr); 20174303829SJason Molenda 202d4612ad0SEd Maste if (fde == nullptr) 20374303829SJason Molenda return false; 20474303829SJason Molenda 20574303829SJason Molenda fde_entry = *fde; 20674303829SJason Molenda return true; 20730fdc8d8SChris Lattner } 20830fdc8d8SChris Lattner 209b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFunctionAddressAndSizeVector( 210b9c1b51eSKate Stone FunctionAddressAndSizeVector &function_info) { 2115635f77aSJason Molenda GetFDEIndex(); 2125635f77aSJason Molenda const size_t count = m_fde_index.GetSize(); 213ee7593fbSJason Molenda function_info.Clear(); 214c1946cd8SJason Molenda if (count > 0) 215ee7593fbSJason Molenda function_info.Reserve(count); 216b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 217b9c1b51eSKate Stone const FDEEntryMap::Entry *func_offset_data_entry = 218b9c1b51eSKate Stone m_fde_index.GetEntryAtIndex(i); 219b9c1b51eSKate Stone if (func_offset_data_entry) { 220b9c1b51eSKate Stone FunctionAddressAndSizeVector::Entry function_offset_entry( 221b9c1b51eSKate Stone func_offset_data_entry->base, func_offset_data_entry->size); 2225635f77aSJason Molenda function_info.Append(function_offset_entry); 2235635f77aSJason Molenda } 2245635f77aSJason Molenda } 2255635f77aSJason Molenda } 2265635f77aSJason Molenda 22730fdc8d8SChris Lattner const DWARFCallFrameInfo::CIE * 228b9c1b51eSKate Stone DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) { 22930fdc8d8SChris Lattner cie_map_t::iterator pos = m_cie_map.find(cie_offset); 23030fdc8d8SChris Lattner 231b9c1b51eSKate Stone if (pos != m_cie_map.end()) { 23230fdc8d8SChris Lattner // Parse and cache the CIE 233*70355aceSJonas Devlieghere if (pos->second == nullptr) 23430fdc8d8SChris Lattner pos->second = ParseCIE(cie_offset); 23530fdc8d8SChris Lattner 23630fdc8d8SChris Lattner return pos->second.get(); 23730fdc8d8SChris Lattner } 238d4612ad0SEd Maste return nullptr; 23930fdc8d8SChris Lattner } 24030fdc8d8SChris Lattner 241fbcb7f2cSJason Molenda DWARFCallFrameInfo::CIESP 242b9c1b51eSKate Stone DWARFCallFrameInfo::ParseCIE(const dw_offset_t cie_offset) { 243fbcb7f2cSJason Molenda CIESP cie_sp(new CIE(cie_offset)); 244c7bece56SGreg Clayton lldb::offset_t offset = cie_offset; 245a6682a41SJonas Devlieghere if (!m_cfi_data_initialized) 246f97c5213SGreg Clayton GetCFIData(); 2471767e115STodd Fiala uint32_t length = m_cfi_data.GetU32(&offset); 2481767e115STodd Fiala dw_offset_t cie_id, end_offset; 2491767e115STodd Fiala bool is_64bit = (length == UINT32_MAX); 2501767e115STodd Fiala if (is_64bit) { 2511767e115STodd Fiala length = m_cfi_data.GetU64(&offset); 2521767e115STodd Fiala cie_id = m_cfi_data.GetU64(&offset); 2531767e115STodd Fiala end_offset = cie_offset + length + 12; 2541767e115STodd Fiala } else { 2551767e115STodd Fiala cie_id = m_cfi_data.GetU32(&offset); 2561767e115STodd Fiala end_offset = cie_offset + length + 4; 2571767e115STodd Fiala } 2583f2a0818SPavel Labath if (length > 0 && ((m_type == DWARF && cie_id == UINT32_MAX) || 2593f2a0818SPavel Labath (m_type == EH && cie_id == 0ul))) { 26030fdc8d8SChris Lattner size_t i; 26130fdc8d8SChris Lattner // cie.offset = cie_offset; 26230fdc8d8SChris Lattner // cie.length = length; 26330fdc8d8SChris Lattner // cie.cieID = cieID; 2646e264d39SAshok Thirumurthi cie_sp->ptr_encoding = DW_EH_PE_absptr; // default 26530fdc8d8SChris Lattner cie_sp->version = m_cfi_data.GetU8(&offset); 266cdda23ebSPavel Labath if (cie_sp->version > CFI_VERSION4) { 267cdda23ebSPavel Labath Host::SystemLog(Host::eSystemLogError, 268cdda23ebSPavel Labath "CIE parse error: CFI version %d is not supported\n", 269cdda23ebSPavel Labath cie_sp->version); 270cdda23ebSPavel Labath return nullptr; 271cdda23ebSPavel Labath } 27230fdc8d8SChris Lattner 273b9c1b51eSKate Stone for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) { 27430fdc8d8SChris Lattner cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset); 275b9c1b51eSKate Stone if (cie_sp->augmentation[i] == '\0') { 27630fdc8d8SChris Lattner // Zero out remaining bytes in augmentation string 27730fdc8d8SChris Lattner for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j) 27830fdc8d8SChris Lattner cie_sp->augmentation[j] = '\0'; 27930fdc8d8SChris Lattner 28030fdc8d8SChris Lattner break; 28130fdc8d8SChris Lattner } 28230fdc8d8SChris Lattner } 28330fdc8d8SChris Lattner 284b9c1b51eSKate Stone if (i == CFI_AUG_MAX_SIZE && 285b9c1b51eSKate Stone cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') { 286b9c1b51eSKate Stone Host::SystemLog(Host::eSystemLogError, 287b9c1b51eSKate Stone "CIE parse error: CIE augmentation string was too large " 288b9c1b51eSKate Stone "for the fixed sized buffer of %d bytes.\n", 289b9c1b51eSKate Stone CFI_AUG_MAX_SIZE); 290cdda23ebSPavel Labath return nullptr; 29130fdc8d8SChris Lattner } 292cdda23ebSPavel Labath 29305097246SAdrian Prantl // m_cfi_data uses address size from target architecture of the process may 29405097246SAdrian Prantl // ignore these fields? 2953f2a0818SPavel Labath if (m_type == DWARF && cie_sp->version >= CFI_VERSION4) { 296cdda23ebSPavel Labath cie_sp->address_size = m_cfi_data.GetU8(&offset); 297cdda23ebSPavel Labath cie_sp->segment_size = m_cfi_data.GetU8(&offset); 298cdda23ebSPavel Labath } 299cdda23ebSPavel Labath 30030fdc8d8SChris Lattner cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset); 30130fdc8d8SChris Lattner cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset); 302cdda23ebSPavel Labath 303cdda23ebSPavel Labath cie_sp->return_addr_reg_num = 3043f2a0818SPavel Labath m_type == DWARF && cie_sp->version >= CFI_VERSION3 305cdda23ebSPavel Labath ? static_cast<uint32_t>(m_cfi_data.GetULEB128(&offset)) 306cdda23ebSPavel Labath : m_cfi_data.GetU8(&offset); 30730fdc8d8SChris Lattner 308b9c1b51eSKate Stone if (cie_sp->augmentation[0]) { 30905097246SAdrian Prantl // Get the length of the eh_frame augmentation data which starts with a 31005097246SAdrian Prantl // ULEB128 length in bytes 31130fdc8d8SChris Lattner const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset); 31230fdc8d8SChris Lattner const size_t aug_data_end = offset + aug_data_len; 31330fdc8d8SChris Lattner const size_t aug_str_len = strlen(cie_sp->augmentation); 31430fdc8d8SChris Lattner // A 'z' may be present as the first character of the string. 31505097246SAdrian Prantl // If present, the Augmentation Data field shall be present. The contents 31605097246SAdrian Prantl // of the Augmentation Data shall be interpreted according to other 31705097246SAdrian Prantl // characters in the Augmentation String. 318b9c1b51eSKate Stone if (cie_sp->augmentation[0] == 'z') { 31930fdc8d8SChris Lattner // Extract the Augmentation Data 32030fdc8d8SChris Lattner size_t aug_str_idx = 0; 321b9c1b51eSKate Stone for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) { 32230fdc8d8SChris Lattner char aug = cie_sp->augmentation[aug_str_idx]; 323b9c1b51eSKate Stone switch (aug) { 32430fdc8d8SChris Lattner case 'L': 32505097246SAdrian Prantl // Indicates the presence of one argument in the Augmentation Data 32605097246SAdrian Prantl // of the CIE, and a corresponding argument in the Augmentation 32705097246SAdrian Prantl // Data of the FDE. The argument in the Augmentation Data of the 32805097246SAdrian Prantl // CIE is 1-byte and represents the pointer encoding used for the 32905097246SAdrian Prantl // argument in the Augmentation Data of the FDE, which is the 33005097246SAdrian Prantl // address of a language-specific data area (LSDA). The size of the 33105097246SAdrian Prantl // LSDA pointer is specified by the pointer encoding used. 332e9c7ecf6SJason Molenda cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset); 33330fdc8d8SChris Lattner break; 33430fdc8d8SChris Lattner 33530fdc8d8SChris Lattner case 'P': 33605097246SAdrian Prantl // Indicates the presence of two arguments in the Augmentation Data 33705097246SAdrian Prantl // of the CIE. The first argument is 1-byte and represents the 33805097246SAdrian Prantl // pointer encoding used for the second argument, which is the 33905097246SAdrian Prantl // address of a personality routine handler. The size of the 34005097246SAdrian Prantl // personality routine pointer is specified by the pointer encoding 34105097246SAdrian Prantl // used. 342e9c7ecf6SJason Molenda // 34305097246SAdrian Prantl // The address of the personality function will be stored at this 34405097246SAdrian Prantl // location. Pre-execution, it will be all zero's so don't read it 34505097246SAdrian Prantl // until we're trying to do an unwind & the reloc has been 346e9c7ecf6SJason Molenda // resolved. 34730fdc8d8SChris Lattner { 34830fdc8d8SChris Lattner uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset); 349e9c7ecf6SJason Molenda const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 35029cb868aSZachary Turner cie_sp->personality_loc = GetGNUEHPointer( 35129cb868aSZachary Turner m_cfi_data, &offset, arg_ptr_encoding, pc_rel_addr, 35229cb868aSZachary Turner LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS); 35330fdc8d8SChris Lattner } 35430fdc8d8SChris Lattner break; 35530fdc8d8SChris Lattner 35630fdc8d8SChris Lattner case 'R': 35730fdc8d8SChris Lattner // A 'R' may be present at any position after the 35805097246SAdrian Prantl // first character of the string. The Augmentation Data shall 35905097246SAdrian Prantl // include a 1 byte argument that represents the pointer encoding 36005097246SAdrian Prantl // for the address pointers used in the FDE. Example: 0x1B == 36105097246SAdrian Prantl // DW_EH_PE_pcrel | DW_EH_PE_sdata4 36230fdc8d8SChris Lattner cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset); 36330fdc8d8SChris Lattner break; 36430fdc8d8SChris Lattner } 36530fdc8d8SChris Lattner } 366b9c1b51eSKate Stone } else if (strcmp(cie_sp->augmentation, "eh") == 0) { 36705097246SAdrian Prantl // If the Augmentation string has the value "eh", then the EH Data 36805097246SAdrian Prantl // field shall be present 36930fdc8d8SChris Lattner } 37030fdc8d8SChris Lattner 37105097246SAdrian Prantl // Set the offset to be the end of the augmentation data just in case we 37205097246SAdrian Prantl // didn't understand any of the data. 37330fdc8d8SChris Lattner offset = (uint32_t)aug_data_end; 37430fdc8d8SChris Lattner } 37530fdc8d8SChris Lattner 376b9c1b51eSKate Stone if (end_offset > offset) { 37730fdc8d8SChris Lattner cie_sp->inst_offset = offset; 37830fdc8d8SChris Lattner cie_sp->inst_length = end_offset - offset; 37930fdc8d8SChris Lattner } 380b9c1b51eSKate Stone while (offset < end_offset) { 381fbcb7f2cSJason Molenda uint8_t inst = m_cfi_data.GetU8(&offset); 382fbcb7f2cSJason Molenda uint8_t primary_opcode = inst & 0xC0; 383fbcb7f2cSJason Molenda uint8_t extended_opcode = inst & 0x3F; 384fbcb7f2cSJason Molenda 385b9c1b51eSKate Stone if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, 386b9c1b51eSKate Stone cie_sp->data_align, offset, 387b9c1b51eSKate Stone cie_sp->initial_row)) 388fbcb7f2cSJason Molenda break; // Stop if we hit an unrecognized opcode 389fbcb7f2cSJason Molenda } 39030fdc8d8SChris Lattner } 39130fdc8d8SChris Lattner 39230fdc8d8SChris Lattner return cie_sp; 39330fdc8d8SChris Lattner } 39430fdc8d8SChris Lattner 395b9c1b51eSKate Stone void DWARFCallFrameInfo::GetCFIData() { 396a6682a41SJonas Devlieghere if (!m_cfi_data_initialized) { 3975160ce5cSGreg Clayton Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND)); 398f97c5213SGreg Clayton if (log) 3995160ce5cSGreg Clayton m_objfile.GetModule()->LogMessage(log, "Reading EH frame info"); 400c9660546SGreg Clayton m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data); 401f97c5213SGreg Clayton m_cfi_data_initialized = true; 402f97c5213SGreg Clayton } 403f97c5213SGreg Clayton } 404b9c1b51eSKate Stone // Scan through the eh_frame or debug_frame section looking for FDEs and noting 40505097246SAdrian Prantl // the start/end addresses of the functions and a pointer back to the 40605097246SAdrian Prantl // function's FDE for later expansion. Internalize CIEs as we come across them. 40730fdc8d8SChris Lattner 408b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFDEIndex() { 409d4612ad0SEd Maste if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 410fbcb7f2cSJason Molenda return; 4117d69f12eSSean Callanan 412fbcb7f2cSJason Molenda if (m_fde_index_initialized) 413fbcb7f2cSJason Molenda return; 414fbcb7f2cSJason Molenda 415bb19a13cSSaleem Abdulrasool std::lock_guard<std::mutex> guard(m_fde_index_mutex); 4167d69f12eSSean Callanan 4177d69f12eSSean Callanan if (m_fde_index_initialized) // if two threads hit the locker 4187d69f12eSSean Callanan return; 419e6194f17SJason Molenda 420f9d16476SPavel Labath static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 421f9d16476SPavel Labath Timer scoped_timer(func_cat, "%s - %s", LLVM_PRETTY_FUNCTION, 422b9c1b51eSKate Stone m_objfile.GetFileSpec().GetFilename().AsCString("")); 42374303829SJason Molenda 424c7afda5aSJason Molenda bool clear_address_zeroth_bit = false; 425f760f5aeSPavel Labath if (ArchSpec arch = m_objfile.GetArchitecture()) { 426b9c1b51eSKate Stone if (arch.GetTriple().getArch() == llvm::Triple::arm || 427b9c1b51eSKate Stone arch.GetTriple().getArch() == llvm::Triple::thumb) 428c7afda5aSJason Molenda clear_address_zeroth_bit = true; 429c7afda5aSJason Molenda } 430c7afda5aSJason Molenda 431c7bece56SGreg Clayton lldb::offset_t offset = 0; 432a6682a41SJonas Devlieghere if (!m_cfi_data_initialized) 433f97c5213SGreg Clayton GetCFIData(); 434b9c1b51eSKate Stone while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) { 43573bf5dbdSGreg Clayton const dw_offset_t current_entry = offset; 4361767e115STodd Fiala dw_offset_t cie_id, next_entry, cie_offset; 437fbcb7f2cSJason Molenda uint32_t len = m_cfi_data.GetU32(&offset); 4381767e115STodd Fiala bool is_64bit = (len == UINT32_MAX); 4391767e115STodd Fiala if (is_64bit) { 4401767e115STodd Fiala len = m_cfi_data.GetU64(&offset); 4411767e115STodd Fiala cie_id = m_cfi_data.GetU64(&offset); 4421767e115STodd Fiala next_entry = current_entry + len + 12; 4431767e115STodd Fiala cie_offset = current_entry + 12 - cie_id; 4441767e115STodd Fiala } else { 4451767e115STodd Fiala cie_id = m_cfi_data.GetU32(&offset); 4461767e115STodd Fiala next_entry = current_entry + len + 4; 4471767e115STodd Fiala cie_offset = current_entry + 4 - cie_id; 4481767e115STodd Fiala } 449fbcb7f2cSJason Molenda 450b9c1b51eSKate Stone if (next_entry > m_cfi_data.GetByteSize() + 1) { 451b9c1b51eSKate Stone Host::SystemLog(Host::eSystemLogError, "error: Invalid fde/cie next " 452b9c1b51eSKate Stone "entry offset of 0x%x found in " 453b9c1b51eSKate Stone "cie/fde at 0x%x\n", 454b9c1b51eSKate Stone next_entry, current_entry); 455e171da5cSBruce Mitchener // Don't trust anything in this eh_frame section if we find blatantly 456c48ef341SJason Molenda // invalid data. 457c48ef341SJason Molenda m_fde_index.Clear(); 458c48ef341SJason Molenda m_fde_index_initialized = true; 459c48ef341SJason Molenda return; 460c48ef341SJason Molenda } 461cdda23ebSPavel Labath 462cdda23ebSPavel Labath // An FDE entry contains CIE_pointer in debug_frame in same place as cie_id 46305097246SAdrian Prantl // in eh_frame. CIE_pointer is an offset into the .debug_frame section. So, 46405097246SAdrian Prantl // variable cie_offset should be equal to cie_id for debug_frame. 465cdda23ebSPavel Labath // FDE entries with cie_id == 0 shouldn't be ignored for it. 4663f2a0818SPavel Labath if ((cie_id == 0 && m_type == EH) || cie_id == UINT32_MAX || len == 0) { 467cdda23ebSPavel Labath auto cie_sp = ParseCIE(current_entry); 468cdda23ebSPavel Labath if (!cie_sp) { 469cdda23ebSPavel Labath // Cannot parse, the reason is already logged 470cdda23ebSPavel Labath m_fde_index.Clear(); 471cdda23ebSPavel Labath m_fde_index_initialized = true; 472cdda23ebSPavel Labath return; 473cdda23ebSPavel Labath } 474cdda23ebSPavel Labath 475cdda23ebSPavel Labath m_cie_map[current_entry] = std::move(cie_sp); 476cdda23ebSPavel Labath offset = next_entry; 477cdda23ebSPavel Labath continue; 478cdda23ebSPavel Labath } 479cdda23ebSPavel Labath 4803f2a0818SPavel Labath if (m_type == DWARF) 481cdda23ebSPavel Labath cie_offset = cie_id; 482cdda23ebSPavel Labath 483b9c1b51eSKate Stone if (cie_offset > m_cfi_data.GetByteSize()) { 484cdda23ebSPavel Labath Host::SystemLog(Host::eSystemLogError, 485cdda23ebSPavel Labath "error: Invalid cie offset of 0x%x " 486cdda23ebSPavel Labath "found in cie/fde at 0x%x\n", 487b9c1b51eSKate Stone cie_offset, current_entry); 488e171da5cSBruce Mitchener // Don't trust anything in this eh_frame section if we find blatantly 489c48ef341SJason Molenda // invalid data. 490c48ef341SJason Molenda m_fde_index.Clear(); 491c48ef341SJason Molenda m_fde_index_initialized = true; 492c48ef341SJason Molenda return; 493c48ef341SJason Molenda } 494c48ef341SJason Molenda 49573bf5dbdSGreg Clayton const CIE *cie = GetCIE(cie_offset); 496b9c1b51eSKate Stone if (cie) { 497c9660546SGreg Clayton const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 498fbcb7f2cSJason Molenda const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; 499fbcb7f2cSJason Molenda const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; 500fbcb7f2cSJason Molenda 50129cb868aSZachary Turner lldb::addr_t addr = 50229cb868aSZachary Turner GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr, 50329cb868aSZachary Turner text_addr, data_addr); 504c7afda5aSJason Molenda if (clear_address_zeroth_bit) 505c7afda5aSJason Molenda addr &= ~1ull; 506c7afda5aSJason Molenda 50729cb868aSZachary Turner lldb::addr_t length = GetGNUEHPointer( 50829cb868aSZachary Turner m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, 50929cb868aSZachary Turner pc_rel_addr, text_addr, data_addr); 51074303829SJason Molenda FDEEntryMap::Entry fde(addr, length, current_entry); 51174303829SJason Molenda m_fde_index.Append(fde); 512b9c1b51eSKate Stone } else { 513b9c1b51eSKate Stone Host::SystemLog(Host::eSystemLogError, "error: unable to find CIE at " 514b9c1b51eSKate Stone "0x%8.8x for cie_id = 0x%8.8x for " 515b9c1b51eSKate Stone "entry at 0x%8.8x.\n", 516b9c1b51eSKate Stone cie_offset, cie_id, current_entry); 51773bf5dbdSGreg Clayton } 518fbcb7f2cSJason Molenda offset = next_entry; 519fbcb7f2cSJason Molenda } 52074303829SJason Molenda m_fde_index.Sort(); 521fbcb7f2cSJason Molenda m_fde_index_initialized = true; 522fbcb7f2cSJason Molenda } 523fbcb7f2cSJason Molenda 524b9c1b51eSKate Stone bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset, 525b9c1b51eSKate Stone Address startaddr, 526b9c1b51eSKate Stone UnwindPlan &unwind_plan) { 5271ee89eb8SPavel Labath Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND); 528c7bece56SGreg Clayton lldb::offset_t offset = dwarf_offset; 529c7bece56SGreg Clayton lldb::offset_t current_entry = offset; 530fbcb7f2cSJason Molenda 531d4612ad0SEd Maste if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 532fbcb7f2cSJason Molenda return false; 533fbcb7f2cSJason Molenda 534a6682a41SJonas Devlieghere if (!m_cfi_data_initialized) 535f97c5213SGreg Clayton GetCFIData(); 536fbcb7f2cSJason Molenda 537fbcb7f2cSJason Molenda uint32_t length = m_cfi_data.GetU32(&offset); 5381767e115STodd Fiala dw_offset_t cie_offset; 5391767e115STodd Fiala bool is_64bit = (length == UINT32_MAX); 5401767e115STodd Fiala if (is_64bit) { 5411767e115STodd Fiala length = m_cfi_data.GetU64(&offset); 5421767e115STodd Fiala cie_offset = m_cfi_data.GetU64(&offset); 5431767e115STodd Fiala } else { 5441767e115STodd Fiala cie_offset = m_cfi_data.GetU32(&offset); 5451767e115STodd Fiala } 546fbcb7f2cSJason Molenda 547cdda23ebSPavel Labath // FDE entries with zeroth cie_offset may occur for debug_frame. 5483f2a0818SPavel Labath assert(!(m_type == EH && 0 == cie_offset) && cie_offset != UINT32_MAX); 54930fdc8d8SChris Lattner 55005097246SAdrian Prantl // Translate the CIE_id from the eh_frame format, which is relative to the 55105097246SAdrian Prantl // FDE offset, into a __eh_frame section offset 5523f2a0818SPavel Labath if (m_type == EH) { 553ab4f1924SJason Molenda unwind_plan.SetSourceName("eh_frame CFI"); 5541767e115STodd Fiala cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset; 55560f0bd49SJason Molenda unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 556b9c1b51eSKate Stone } else { 557ab4f1924SJason Molenda unwind_plan.SetSourceName("DWARF CFI"); 55860f0bd49SJason Molenda // In theory the debug_frame info should be valid at all call sites 55960f0bd49SJason Molenda // ("asynchronous unwind info" as it is sometimes called) but in practice 56060f0bd49SJason Molenda // gcc et al all emit call frame info for the prologue and call sites, but 561b9c1b51eSKate Stone // not for the epilogue or all the other locations during the function 562b9c1b51eSKate Stone // reliably. 56360f0bd49SJason Molenda unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 564ab4f1924SJason Molenda } 56560f0bd49SJason Molenda unwind_plan.SetSourcedFromCompiler(eLazyBoolYes); 56630fdc8d8SChris Lattner 56730fdc8d8SChris Lattner const CIE *cie = GetCIE(cie_offset); 568d4612ad0SEd Maste assert(cie != nullptr); 569fbcb7f2cSJason Molenda 5701767e115STodd Fiala const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4); 571fbcb7f2cSJason Molenda 572c9660546SGreg Clayton const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 57330fdc8d8SChris Lattner const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; 57430fdc8d8SChris Lattner const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; 57529cb868aSZachary Turner lldb::addr_t range_base = 57629cb868aSZachary Turner GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr, 577b9c1b51eSKate Stone text_addr, data_addr); 57829cb868aSZachary Turner lldb::addr_t range_len = GetGNUEHPointer( 57929cb868aSZachary Turner m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, 58029cb868aSZachary Turner pc_rel_addr, text_addr, data_addr); 581b9c1b51eSKate Stone AddressRange range(range_base, m_objfile.GetAddressByteSize(), 582b9c1b51eSKate Stone m_objfile.GetSectionList()); 583fbcb7f2cSJason Molenda range.SetByteSize(range_len); 58430fdc8d8SChris Lattner 585e9c7ecf6SJason Molenda addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS; 586e9c7ecf6SJason Molenda 587b9c1b51eSKate Stone if (cie->augmentation[0] == 'z') { 58830fdc8d8SChris Lattner uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 589b9c1b51eSKate Stone if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) { 590e9c7ecf6SJason Molenda offset_t saved_offset = offset; 59129cb868aSZachary Turner lsda_data_file_address = 59229cb868aSZachary Turner GetGNUEHPointer(m_cfi_data, &offset, cie->lsda_addr_encoding, 59329cb868aSZachary Turner pc_rel_addr, text_addr, data_addr); 594b9c1b51eSKate Stone if (offset - saved_offset != aug_data_len) { 595e9c7ecf6SJason Molenda // There is more in the augmentation region than we know how to process; 596e9c7ecf6SJason Molenda // don't read anything. 597e9c7ecf6SJason Molenda lsda_data_file_address = LLDB_INVALID_ADDRESS; 598e9c7ecf6SJason Molenda } 599e9c7ecf6SJason Molenda offset = saved_offset; 600e9c7ecf6SJason Molenda } 60130fdc8d8SChris Lattner offset += aug_data_len; 60230fdc8d8SChris Lattner } 603e9c7ecf6SJason Molenda Address lsda_data; 604e9c7ecf6SJason Molenda Address personality_function_ptr; 605e9c7ecf6SJason Molenda 606b9c1b51eSKate Stone if (lsda_data_file_address != LLDB_INVALID_ADDRESS && 607b9c1b51eSKate Stone cie->personality_loc != LLDB_INVALID_ADDRESS) { 608b9c1b51eSKate Stone m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address, 609b9c1b51eSKate Stone lsda_data); 610b9c1b51eSKate Stone m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc, 611b9c1b51eSKate Stone personality_function_ptr); 612e9c7ecf6SJason Molenda } 613e9c7ecf6SJason Molenda 614b9c1b51eSKate Stone if (lsda_data.IsValid() && personality_function_ptr.IsValid()) { 615e9c7ecf6SJason Molenda unwind_plan.SetLSDAAddress(lsda_data); 616e9c7ecf6SJason Molenda unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr); 617e9c7ecf6SJason Molenda } 61830fdc8d8SChris Lattner 61930fdc8d8SChris Lattner uint32_t code_align = cie->code_align; 62030fdc8d8SChris Lattner int32_t data_align = cie->data_align; 62130fdc8d8SChris Lattner 622fbcb7f2cSJason Molenda unwind_plan.SetPlanValidAddressRange(range); 6231d42c7bcSJason Molenda UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row; 6241d42c7bcSJason Molenda *cie_initial_row = cie->initial_row; 6251d42c7bcSJason Molenda UnwindPlan::RowSP row(cie_initial_row); 62630fdc8d8SChris Lattner 6273f2a0818SPavel Labath unwind_plan.SetRegisterKind(GetRegisterKind()); 6288eba46c6SJason Molenda unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num); 629fbcb7f2cSJason Molenda 630f70d6040SRichard Mitton std::vector<UnwindPlan::RowSP> stack; 631f70d6040SRichard Mitton 632fbcb7f2cSJason Molenda UnwindPlan::Row::RegisterLocation reg_location; 633b9c1b51eSKate Stone while (m_cfi_data.ValidOffset(offset) && offset < end_offset) { 63430fdc8d8SChris Lattner uint8_t inst = m_cfi_data.GetU8(&offset); 63530fdc8d8SChris Lattner uint8_t primary_opcode = inst & 0xC0; 63630fdc8d8SChris Lattner uint8_t extended_opcode = inst & 0x3F; 63730fdc8d8SChris Lattner 638b9c1b51eSKate Stone if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align, 639b9c1b51eSKate Stone offset, *row)) { 640b9c1b51eSKate Stone if (primary_opcode) { 641b9c1b51eSKate Stone switch (primary_opcode) { 64230fdc8d8SChris Lattner case DW_CFA_advance_loc: // (Row Creation Instruction) 64330fdc8d8SChris Lattner { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta 64430fdc8d8SChris Lattner // takes a single argument that represents a constant delta. The 64505097246SAdrian Prantl // required action is to create a new table row with a location value 64605097246SAdrian Prantl // that is computed by taking the current entry's location value and 64705097246SAdrian Prantl // adding (delta * code_align). All other values in the new row are 64805097246SAdrian Prantl // initially identical to the current row. 649fbcb7f2cSJason Molenda unwind_plan.AppendRow(row); 650fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 651fa67e879SJason Molenda *newrow = *row.get(); 652fa67e879SJason Molenda row.reset(newrow); 6531d42c7bcSJason Molenda row->SlideOffset(extended_opcode * code_align); 65430fdc8d8SChris Lattner break; 65530fdc8d8SChris Lattner } 65630fdc8d8SChris Lattner 657b9c1b51eSKate Stone case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are 658b9c1b51eSKate Stone // register 65930fdc8d8SChris Lattner // takes a single argument that represents a register number. The 66030fdc8d8SChris Lattner // required action is to change the rule for the indicated register 66130fdc8d8SChris Lattner // to the rule assigned it by the initial_instructions in the CIE. 662623652a0STamas Berghammer uint32_t reg_num = extended_opcode; 66305097246SAdrian Prantl // We only keep enough register locations around to unwind what is in 66405097246SAdrian Prantl // our thread, and these are organized by the register index in that 66505097246SAdrian Prantl // state, so we need to convert our eh_frame register number from the 66605097246SAdrian Prantl // EH frame info, to a register index 66730fdc8d8SChris Lattner 668b9c1b51eSKate Stone if (unwind_plan.IsValidRowIndex(0) && 669b9c1b51eSKate Stone unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, 670b9c1b51eSKate Stone reg_location)) 6711d42c7bcSJason Molenda row->SetRegisterInfo(reg_num, reg_location); 67230fdc8d8SChris Lattner break; 67330fdc8d8SChris Lattner } 67430fdc8d8SChris Lattner } 675b9c1b51eSKate Stone } else { 676b9c1b51eSKate Stone switch (extended_opcode) { 67730fdc8d8SChris Lattner case DW_CFA_set_loc: // 0x1 (Row Creation Instruction) 67830fdc8d8SChris Lattner { 67930fdc8d8SChris Lattner // DW_CFA_set_loc takes a single argument that represents an address. 68030fdc8d8SChris Lattner // The required action is to create a new table row using the 68130fdc8d8SChris Lattner // specified address as the location. All other values in the new row 68230fdc8d8SChris Lattner // are initially identical to the current row. The new location value 68330fdc8d8SChris Lattner // should always be greater than the current one. 684fbcb7f2cSJason Molenda unwind_plan.AppendRow(row); 685fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 686fa67e879SJason Molenda *newrow = *row.get(); 687fa67e879SJason Molenda row.reset(newrow); 688b9c1b51eSKate Stone row->SetOffset(m_cfi_data.GetPointer(&offset) - 689b9c1b51eSKate Stone startaddr.GetFileAddress()); 69030fdc8d8SChris Lattner break; 691623652a0STamas Berghammer } 69230fdc8d8SChris Lattner 69330fdc8d8SChris Lattner case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction) 69430fdc8d8SChris Lattner { 69530fdc8d8SChris Lattner // takes a single uword argument that represents a constant delta. 69630fdc8d8SChris Lattner // This instruction is identical to DW_CFA_advance_loc except for the 69730fdc8d8SChris Lattner // encoding and size of the delta argument. 698fbcb7f2cSJason Molenda unwind_plan.AppendRow(row); 699fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 700fa67e879SJason Molenda *newrow = *row.get(); 701fa67e879SJason Molenda row.reset(newrow); 7021d42c7bcSJason Molenda row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align); 70330fdc8d8SChris Lattner break; 704623652a0STamas Berghammer } 70530fdc8d8SChris Lattner 70630fdc8d8SChris Lattner case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction) 70730fdc8d8SChris Lattner { 70830fdc8d8SChris Lattner // takes a single uword argument that represents a constant delta. 70930fdc8d8SChris Lattner // This instruction is identical to DW_CFA_advance_loc except for the 71030fdc8d8SChris Lattner // encoding and size of the delta argument. 711fbcb7f2cSJason Molenda unwind_plan.AppendRow(row); 712fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 713fa67e879SJason Molenda *newrow = *row.get(); 714fa67e879SJason Molenda row.reset(newrow); 7151d42c7bcSJason Molenda row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align); 71630fdc8d8SChris Lattner break; 717623652a0STamas Berghammer } 71830fdc8d8SChris Lattner 71930fdc8d8SChris Lattner case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction) 72030fdc8d8SChris Lattner { 72130fdc8d8SChris Lattner // takes a single uword argument that represents a constant delta. 72230fdc8d8SChris Lattner // This instruction is identical to DW_CFA_advance_loc except for the 72330fdc8d8SChris Lattner // encoding and size of the delta argument. 724fbcb7f2cSJason Molenda unwind_plan.AppendRow(row); 725fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 726fa67e879SJason Molenda *newrow = *row.get(); 727fa67e879SJason Molenda row.reset(newrow); 7281d42c7bcSJason Molenda row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align); 72930fdc8d8SChris Lattner break; 73030fdc8d8SChris Lattner } 73130fdc8d8SChris Lattner 73230fdc8d8SChris Lattner case DW_CFA_restore_extended: // 0x6 73330fdc8d8SChris Lattner { 73430fdc8d8SChris Lattner // takes a single unsigned LEB128 argument that represents a register 73530fdc8d8SChris Lattner // number. This instruction is identical to DW_CFA_restore except for 73630fdc8d8SChris Lattner // the encoding and size of the register argument. 737623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 738b9c1b51eSKate Stone if (unwind_plan.IsValidRowIndex(0) && 739b9c1b51eSKate Stone unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, 740b9c1b51eSKate Stone reg_location)) 7411d42c7bcSJason Molenda row->SetRegisterInfo(reg_num, reg_location); 74230fdc8d8SChris Lattner break; 74330fdc8d8SChris Lattner } 74430fdc8d8SChris Lattner 74530fdc8d8SChris Lattner case DW_CFA_remember_state: // 0xA 746fa67e879SJason Molenda { 74730fdc8d8SChris Lattner // These instructions define a stack of information. Encountering the 74805097246SAdrian Prantl // DW_CFA_remember_state instruction means to save the rules for 74905097246SAdrian Prantl // every register on the current row on the stack. Encountering the 75030fdc8d8SChris Lattner // DW_CFA_restore_state instruction means to pop the set of rules off 75130fdc8d8SChris Lattner // the stack and place them in the current row. (This operation is 75230fdc8d8SChris Lattner // useful for compilers that move epilogue code into the body of a 75330fdc8d8SChris Lattner // function.) 754f70d6040SRichard Mitton stack.push_back(row); 755fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 756fa67e879SJason Molenda *newrow = *row.get(); 757fa67e879SJason Molenda row.reset(newrow); 75830fdc8d8SChris Lattner break; 759623652a0STamas Berghammer } 76030fdc8d8SChris Lattner 76130fdc8d8SChris Lattner case DW_CFA_restore_state: // 0xB 762623652a0STamas Berghammer { 76330fdc8d8SChris Lattner // These instructions define a stack of information. Encountering the 76405097246SAdrian Prantl // DW_CFA_remember_state instruction means to save the rules for 76505097246SAdrian Prantl // every register on the current row on the stack. Encountering the 76630fdc8d8SChris Lattner // DW_CFA_restore_state instruction means to pop the set of rules off 76730fdc8d8SChris Lattner // the stack and place them in the current row. (This operation is 76830fdc8d8SChris Lattner // useful for compilers that move epilogue code into the body of a 76930fdc8d8SChris Lattner // function.) 770b9c1b51eSKate Stone if (stack.empty()) { 7711ee89eb8SPavel Labath if (log) 772b9c1b51eSKate Stone log->Printf("DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32 773b9c1b51eSKate Stone ", startaddr: %" PRIx64 774b9c1b51eSKate Stone " encountered DW_CFA_restore_state but state stack " 775b9c1b51eSKate Stone "is empty. Corrupt unwind info?", 776b9c1b51eSKate Stone __FUNCTION__, dwarf_offset, 777b9c1b51eSKate Stone startaddr.GetFileAddress()); 7781ee89eb8SPavel Labath break; 7791ee89eb8SPavel Labath } 78049dd6d15STamas Berghammer lldb::addr_t offset = row->GetOffset(); 781f70d6040SRichard Mitton row = stack.back(); 782f70d6040SRichard Mitton stack.pop_back(); 78349dd6d15STamas Berghammer row->SetOffset(offset); 78430fdc8d8SChris Lattner break; 785623652a0STamas Berghammer } 786623652a0STamas Berghammer 7871ee89eb8SPavel Labath case DW_CFA_GNU_args_size: // 0x2e 7881ee89eb8SPavel Labath { 789b9c1b51eSKate Stone // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128 79005097246SAdrian Prantl // operand representing an argument size. This instruction specifies 79105097246SAdrian Prantl // the total of the size of the arguments which have been pushed onto 79205097246SAdrian Prantl // the stack. 7931ee89eb8SPavel Labath 7941ee89eb8SPavel Labath // TODO: Figure out how we should handle this. 7951ee89eb8SPavel Labath m_cfi_data.GetULEB128(&offset); 796a41deb1cSJason Molenda break; 7971ee89eb8SPavel Labath } 7981ee89eb8SPavel Labath 799623652a0STamas Berghammer case DW_CFA_val_offset: // 0x14 800623652a0STamas Berghammer case DW_CFA_val_offset_sf: // 0x15 801623652a0STamas Berghammer default: 802623652a0STamas Berghammer break; 803623652a0STamas Berghammer } 804623652a0STamas Berghammer } 805623652a0STamas Berghammer } 806623652a0STamas Berghammer } 807623652a0STamas Berghammer unwind_plan.AppendRow(row); 808623652a0STamas Berghammer 809623652a0STamas Berghammer return true; 810623652a0STamas Berghammer } 811623652a0STamas Berghammer 812b9c1b51eSKate Stone bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode, 813623652a0STamas Berghammer uint8_t extended_opcode, 814623652a0STamas Berghammer int32_t data_align, 815623652a0STamas Berghammer lldb::offset_t &offset, 816b9c1b51eSKate Stone UnwindPlan::Row &row) { 817623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 818623652a0STamas Berghammer 819b9c1b51eSKate Stone if (primary_opcode) { 820b9c1b51eSKate Stone switch (primary_opcode) { 821b9c1b51eSKate Stone case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are 822b9c1b51eSKate Stone // register 823623652a0STamas Berghammer // takes two arguments: an unsigned LEB128 constant representing a 824623652a0STamas Berghammer // factored offset and a register number. The required action is to 82505097246SAdrian Prantl // change the rule for the register indicated by the register number to 82605097246SAdrian Prantl // be an offset(N) rule with a value of (N = factored offset * 82705097246SAdrian Prantl // data_align). 828623652a0STamas Berghammer uint8_t reg_num = extended_opcode; 829623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 830623652a0STamas Berghammer reg_location.SetAtCFAPlusOffset(op_offset); 831623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 832623652a0STamas Berghammer return true; 833623652a0STamas Berghammer } 834623652a0STamas Berghammer } 835b9c1b51eSKate Stone } else { 836b9c1b51eSKate Stone switch (extended_opcode) { 837623652a0STamas Berghammer case DW_CFA_nop: // 0x0 838623652a0STamas Berghammer return true; 839623652a0STamas Berghammer 840623652a0STamas Berghammer case DW_CFA_offset_extended: // 0x5 841623652a0STamas Berghammer { 84205097246SAdrian Prantl // takes two unsigned LEB128 arguments representing a register number and 84305097246SAdrian Prantl // a factored offset. This instruction is identical to DW_CFA_offset 844623652a0STamas Berghammer // except for the encoding and size of the register argument. 845623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 846623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 847623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 848623652a0STamas Berghammer reg_location.SetAtCFAPlusOffset(op_offset); 849623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 850623652a0STamas Berghammer return true; 851623652a0STamas Berghammer } 852623652a0STamas Berghammer 853623652a0STamas Berghammer case DW_CFA_undefined: // 0x7 854623652a0STamas Berghammer { 855623652a0STamas Berghammer // takes a single unsigned LEB128 argument that represents a register 856623652a0STamas Berghammer // number. The required action is to set the rule for the specified 857623652a0STamas Berghammer // register to undefined. 858623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 859623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 860623652a0STamas Berghammer reg_location.SetUndefined(); 861623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 862623652a0STamas Berghammer return true; 863623652a0STamas Berghammer } 864623652a0STamas Berghammer 865623652a0STamas Berghammer case DW_CFA_same_value: // 0x8 866623652a0STamas Berghammer { 867623652a0STamas Berghammer // takes a single unsigned LEB128 argument that represents a register 868623652a0STamas Berghammer // number. The required action is to set the rule for the specified 869623652a0STamas Berghammer // register to same value. 870623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 871623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 872623652a0STamas Berghammer reg_location.SetSame(); 873623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 874623652a0STamas Berghammer return true; 875623652a0STamas Berghammer } 876623652a0STamas Berghammer 877623652a0STamas Berghammer case DW_CFA_register: // 0x9 878623652a0STamas Berghammer { 87905097246SAdrian Prantl // takes two unsigned LEB128 arguments representing register numbers. The 88005097246SAdrian Prantl // required action is to set the rule for the first register to be the 88105097246SAdrian Prantl // second register. 882623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 883623652a0STamas Berghammer uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 884623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 885623652a0STamas Berghammer reg_location.SetInRegister(other_reg_num); 886623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 887623652a0STamas Berghammer return true; 888623652a0STamas Berghammer } 88930fdc8d8SChris Lattner 89030fdc8d8SChris Lattner case DW_CFA_def_cfa: // 0xC (CFA Definition Instruction) 89130fdc8d8SChris Lattner { 89205097246SAdrian Prantl // Takes two unsigned LEB128 operands representing a register number and 89305097246SAdrian Prantl // a (non-factored) offset. The required action is to define the current 89405097246SAdrian Prantl // CFA rule to use the provided register and offset. 895623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 896623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 897623652a0STamas Berghammer row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset); 898623652a0STamas Berghammer return true; 89930fdc8d8SChris Lattner } 90030fdc8d8SChris Lattner 90130fdc8d8SChris Lattner case DW_CFA_def_cfa_register: // 0xD (CFA Definition Instruction) 90230fdc8d8SChris Lattner { 90330fdc8d8SChris Lattner // takes a single unsigned LEB128 argument representing a register 90405097246SAdrian Prantl // number. The required action is to define the current CFA rule to use 90505097246SAdrian Prantl // the provided register (but to keep the old offset). 906623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 907b9c1b51eSKate Stone row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, 908b9c1b51eSKate Stone row.GetCFAValue().GetOffset()); 909623652a0STamas Berghammer return true; 91030fdc8d8SChris Lattner } 91130fdc8d8SChris Lattner 91230fdc8d8SChris Lattner case DW_CFA_def_cfa_offset: // 0xE (CFA Definition Instruction) 91330fdc8d8SChris Lattner { 91405097246SAdrian Prantl // Takes a single unsigned LEB128 operand representing a (non-factored) 91505097246SAdrian Prantl // offset. The required action is to define the current CFA rule to use 91605097246SAdrian Prantl // the provided offset (but to keep the old register). 917623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 918b9c1b51eSKate Stone row.GetCFAValue().SetIsRegisterPlusOffset( 919b9c1b51eSKate Stone row.GetCFAValue().GetRegisterNumber(), op_offset); 920623652a0STamas Berghammer return true; 92130fdc8d8SChris Lattner } 92230fdc8d8SChris Lattner 92330fdc8d8SChris Lattner case DW_CFA_def_cfa_expression: // 0xF (CFA Definition Instruction) 92430fdc8d8SChris Lattner { 92530fdc8d8SChris Lattner size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset); 926b9c1b51eSKate Stone const uint8_t *block_data = 927b9c1b51eSKate Stone static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len)); 928623652a0STamas Berghammer row.GetCFAValue().SetIsDWARFExpression(block_data, block_len); 929623652a0STamas Berghammer return true; 93030fdc8d8SChris Lattner } 93130fdc8d8SChris Lattner 93230fdc8d8SChris Lattner case DW_CFA_expression: // 0x10 93330fdc8d8SChris Lattner { 93405097246SAdrian Prantl // Takes two operands: an unsigned LEB128 value representing a register 93505097246SAdrian Prantl // number, and a DW_FORM_block value representing a DWARF expression. The 93605097246SAdrian Prantl // required action is to change the rule for the register indicated by 93705097246SAdrian Prantl // the register number to be an expression(E) rule where E is the DWARF 93805097246SAdrian Prantl // expression. That is, the DWARF expression computes the address. The 93905097246SAdrian Prantl // value of the CFA is pushed on the DWARF evaluation stack prior to 94005097246SAdrian Prantl // execution of the DWARF expression. 941623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 94230fdc8d8SChris Lattner uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 943b9c1b51eSKate Stone const uint8_t *block_data = 944b9c1b51eSKate Stone static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len)); 945623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 94630fdc8d8SChris Lattner reg_location.SetAtDWARFExpression(block_data, block_len); 947623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 948623652a0STamas Berghammer return true; 94930fdc8d8SChris Lattner } 95030fdc8d8SChris Lattner 95130fdc8d8SChris Lattner case DW_CFA_offset_extended_sf: // 0x11 95230fdc8d8SChris Lattner { 95305097246SAdrian Prantl // takes two operands: an unsigned LEB128 value representing a register 95405097246SAdrian Prantl // number and a signed LEB128 factored offset. This instruction is 95505097246SAdrian Prantl // identical to DW_CFA_offset_extended except that the second operand is 95605097246SAdrian Prantl // signed and factored. 957623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 958623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 959623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 96030fdc8d8SChris Lattner reg_location.SetAtCFAPlusOffset(op_offset); 961623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 962623652a0STamas Berghammer return true; 96330fdc8d8SChris Lattner } 96430fdc8d8SChris Lattner 96530fdc8d8SChris Lattner case DW_CFA_def_cfa_sf: // 0x12 (CFA Definition Instruction) 96630fdc8d8SChris Lattner { 96705097246SAdrian Prantl // Takes two operands: an unsigned LEB128 value representing a register 96805097246SAdrian Prantl // number and a signed LEB128 factored offset. This instruction is 96905097246SAdrian Prantl // identical to DW_CFA_def_cfa except that the second operand is signed 97005097246SAdrian Prantl // and factored. 971623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 972623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 973623652a0STamas Berghammer row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset); 974623652a0STamas Berghammer return true; 97530fdc8d8SChris Lattner } 97630fdc8d8SChris Lattner 97730fdc8d8SChris Lattner case DW_CFA_def_cfa_offset_sf: // 0x13 (CFA Definition Instruction) 97830fdc8d8SChris Lattner { 97905097246SAdrian Prantl // takes a signed LEB128 operand representing a factored offset. This 98005097246SAdrian Prantl // instruction is identical to DW_CFA_def_cfa_offset except that the 98105097246SAdrian Prantl // operand is signed and factored. 982623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 983623652a0STamas Berghammer uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber(); 984623652a0STamas Berghammer row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset); 985623652a0STamas Berghammer return true; 98630fdc8d8SChris Lattner } 98730fdc8d8SChris Lattner 98830fdc8d8SChris Lattner case DW_CFA_val_expression: // 0x16 98930fdc8d8SChris Lattner { 99030fdc8d8SChris Lattner // takes two operands: an unsigned LEB128 value representing a register 99105097246SAdrian Prantl // number, and a DW_FORM_block value representing a DWARF expression. The 99205097246SAdrian Prantl // required action is to change the rule for the register indicated by 99305097246SAdrian Prantl // the register number to be a val_expression(E) rule where E is the 99430fdc8d8SChris Lattner // DWARF expression. That is, the DWARF expression computes the value of 99530fdc8d8SChris Lattner // the given register. The value of the CFA is pushed on the DWARF 99630fdc8d8SChris Lattner // evaluation stack prior to execution of the DWARF expression. 997623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 99830fdc8d8SChris Lattner uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 999b9c1b51eSKate Stone const uint8_t *block_data = 1000b9c1b51eSKate Stone (const uint8_t *)m_cfi_data.GetData(&offset, block_len); 100130fdc8d8SChris Lattner //#if defined(__i386__) || defined(__x86_64__) 1002b9c1b51eSKate Stone // // The EH frame info for EIP and RIP contains code that 1003b9c1b51eSKate Stone // looks for traps to 100430fdc8d8SChris Lattner // // be a specific type and increments the PC. 100530fdc8d8SChris Lattner // // For i386: 100630fdc8d8SChris Lattner // // DW_CFA_val_expression where: 1007b9c1b51eSKate Stone // // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, 1008b9c1b51eSKate Stone // DW_OP_plus_uconst(0x34), 1009b9c1b51eSKate Stone // // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), 1010b9c1b51eSKate Stone // DW_OP_deref, 1011b9c1b51eSKate Stone // // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, 1012b9c1b51eSKate Stone // DW_OP_lit4, DW_OP_ne, 101330fdc8d8SChris Lattner // // DW_OP_and, DW_OP_plus 101430fdc8d8SChris Lattner // // This basically does a: 101530fdc8d8SChris Lattner // // eip = ucontenxt.mcontext32->gpr.eip; 1016b9c1b51eSKate Stone // // if (ucontenxt.mcontext32->exc.trapno != 3 && 1017b9c1b51eSKate Stone // ucontenxt.mcontext32->exc.trapno != 4) 101830fdc8d8SChris Lattner // // eip++; 101930fdc8d8SChris Lattner // // 102030fdc8d8SChris Lattner // // For x86_64: 102130fdc8d8SChris Lattner // // DW_CFA_val_expression where: 1022b9c1b51eSKate Stone // // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, 1023b9c1b51eSKate Stone // DW_OP_plus_uconst(0x90), DW_OP_deref, 1024b9c1b51eSKate Stone // // DW_OP_swap, DW_OP_plus_uconst(0), 1025b9c1b51eSKate Stone // DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3, 1026b9c1b51eSKate Stone // // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, 1027b9c1b51eSKate Stone // DW_OP_and, DW_OP_plus 102830fdc8d8SChris Lattner // // This basically does a: 102930fdc8d8SChris Lattner // // rip = ucontenxt.mcontext64->gpr.rip; 1030b9c1b51eSKate Stone // // if (ucontenxt.mcontext64->exc.trapno != 3 && 1031b9c1b51eSKate Stone // ucontenxt.mcontext64->exc.trapno != 4) 103230fdc8d8SChris Lattner // // rip++; 1033b9c1b51eSKate Stone // // The trap comparisons and increments are not needed as 1034b9c1b51eSKate Stone // it hoses up the unwound PC which 1035b9c1b51eSKate Stone // // is expected to point at least past the instruction that 1036b9c1b51eSKate Stone // causes the fault/trap. So we 1037b9c1b51eSKate Stone // // take it out by trimming the expression right at the 1038b9c1b51eSKate Stone // first "DW_OP_swap" opcodes 1039b9c1b51eSKate Stone // if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) 1040b9c1b51eSKate Stone // == reg_num) 104130fdc8d8SChris Lattner // { 104230fdc8d8SChris Lattner // if (thread->Is64Bit()) 104330fdc8d8SChris Lattner // { 1044b9c1b51eSKate Stone // if (block_len > 9 && block_data[8] == DW_OP_swap 1045b9c1b51eSKate Stone // && block_data[9] == DW_OP_plus_uconst) 104630fdc8d8SChris Lattner // block_len = 8; 104730fdc8d8SChris Lattner // } 104830fdc8d8SChris Lattner // else 104930fdc8d8SChris Lattner // { 1050b9c1b51eSKate Stone // if (block_len > 8 && block_data[7] == DW_OP_swap 1051b9c1b51eSKate Stone // && block_data[8] == DW_OP_plus_uconst) 105230fdc8d8SChris Lattner // block_len = 7; 105330fdc8d8SChris Lattner // } 105430fdc8d8SChris Lattner // } 105530fdc8d8SChris Lattner //#endif 105630fdc8d8SChris Lattner reg_location.SetIsDWARFExpression(block_data, block_len); 1057623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 1058fbcb7f2cSJason Molenda return true; 105930fdc8d8SChris Lattner } 1060623652a0STamas Berghammer } 1061623652a0STamas Berghammer } 1062623652a0STamas Berghammer return false; 1063623652a0STamas Berghammer } 10646b63b148STamas Berghammer 1065b9c1b51eSKate Stone void DWARFCallFrameInfo::ForEachFDEEntries( 1066b9c1b51eSKate Stone const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) { 10676b63b148STamas Berghammer GetFDEIndex(); 10686b63b148STamas Berghammer 1069b9c1b51eSKate Stone for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) { 10706b63b148STamas Berghammer const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i); 10716b63b148STamas Berghammer if (!callback(entry.base, entry.size, entry.data)) 10726b63b148STamas Berghammer break; 10736b63b148STamas Berghammer } 10746b63b148STamas Berghammer } 1075