1*80814287SRaphael 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" 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> 223fd917d8SJoseph Tremoulet #include <cstring> 2330fdc8d8SChris Lattner 2430fdc8d8SChris Lattner using namespace lldb; 2530fdc8d8SChris Lattner using namespace lldb_private; 2630fdc8d8SChris Lattner 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 static uint64_t 3229cb868aSZachary Turner GetGNUEHPointer(const DataExtractor &DE, offset_t *offset_ptr, 3329cb868aSZachary Turner uint32_t eh_ptr_enc, addr_t pc_rel_addr, addr_t text_addr, 3429cb868aSZachary Turner addr_t data_addr) //, BSDRelocs *data_relocs) const 3529cb868aSZachary Turner { 3629cb868aSZachary Turner if (eh_ptr_enc == DW_EH_PE_omit) 3729cb868aSZachary Turner return ULLONG_MAX; // Value isn't in the buffer... 3829cb868aSZachary Turner 3929cb868aSZachary Turner uint64_t baseAddress = 0; 4029cb868aSZachary Turner uint64_t addressValue = 0; 4129cb868aSZachary Turner const uint32_t addr_size = DE.GetAddressByteSize(); 4229cb868aSZachary Turner assert(addr_size == 4 || addr_size == 8); 4329cb868aSZachary Turner 4429cb868aSZachary Turner bool signExtendValue = false; 4529cb868aSZachary Turner // Decode the base part or adjust our offset 4629cb868aSZachary Turner switch (eh_ptr_enc & 0x70) { 4729cb868aSZachary Turner case DW_EH_PE_pcrel: 4829cb868aSZachary Turner signExtendValue = true; 4929cb868aSZachary Turner baseAddress = *offset_ptr; 5029cb868aSZachary Turner if (pc_rel_addr != LLDB_INVALID_ADDRESS) 5129cb868aSZachary Turner baseAddress += pc_rel_addr; 5229cb868aSZachary Turner // else 5329cb868aSZachary Turner // Log::GlobalWarning ("PC relative pointer encoding found with 5429cb868aSZachary Turner // invalid pc relative address."); 5529cb868aSZachary Turner break; 5629cb868aSZachary Turner 5729cb868aSZachary Turner case DW_EH_PE_textrel: 5829cb868aSZachary Turner signExtendValue = true; 5929cb868aSZachary Turner if (text_addr != LLDB_INVALID_ADDRESS) 6029cb868aSZachary Turner baseAddress = text_addr; 6129cb868aSZachary Turner // else 6229cb868aSZachary Turner // Log::GlobalWarning ("text relative pointer encoding being 6329cb868aSZachary Turner // decoded with invalid text section address, setting base address 6429cb868aSZachary Turner // to zero."); 6529cb868aSZachary Turner break; 6629cb868aSZachary Turner 6729cb868aSZachary Turner case DW_EH_PE_datarel: 6829cb868aSZachary Turner signExtendValue = true; 6929cb868aSZachary Turner if (data_addr != LLDB_INVALID_ADDRESS) 7029cb868aSZachary Turner baseAddress = data_addr; 7129cb868aSZachary Turner // else 7229cb868aSZachary Turner // Log::GlobalWarning ("data relative pointer encoding being 7329cb868aSZachary Turner // decoded with invalid data section address, setting base address 7429cb868aSZachary Turner // to zero."); 7529cb868aSZachary Turner break; 7629cb868aSZachary Turner 7729cb868aSZachary Turner case DW_EH_PE_funcrel: 7829cb868aSZachary Turner signExtendValue = true; 7929cb868aSZachary Turner break; 8029cb868aSZachary Turner 8129cb868aSZachary Turner case DW_EH_PE_aligned: { 8205097246SAdrian Prantl // SetPointerSize should be called prior to extracting these so the pointer 8305097246SAdrian Prantl // size is cached 8429cb868aSZachary Turner assert(addr_size != 0); 8529cb868aSZachary Turner if (addr_size) { 8629cb868aSZachary Turner // Align to a address size boundary first 8729cb868aSZachary Turner uint32_t alignOffset = *offset_ptr % addr_size; 8829cb868aSZachary Turner if (alignOffset) 8929cb868aSZachary Turner offset_ptr += addr_size - alignOffset; 9029cb868aSZachary Turner } 9129cb868aSZachary Turner } break; 9229cb868aSZachary Turner 9329cb868aSZachary Turner default: 9429cb868aSZachary Turner break; 9529cb868aSZachary Turner } 9629cb868aSZachary Turner 9729cb868aSZachary Turner // Decode the value part 9829cb868aSZachary Turner switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) { 9929cb868aSZachary Turner case DW_EH_PE_absptr: { 10029cb868aSZachary Turner addressValue = DE.GetAddress(offset_ptr); 10129cb868aSZachary Turner // if (data_relocs) 10229cb868aSZachary Turner // addressValue = data_relocs->Relocate(*offset_ptr - 10329cb868aSZachary Turner // addr_size, *this, addressValue); 10429cb868aSZachary Turner } break; 10529cb868aSZachary Turner case DW_EH_PE_uleb128: 10629cb868aSZachary Turner addressValue = DE.GetULEB128(offset_ptr); 10729cb868aSZachary Turner break; 10829cb868aSZachary Turner case DW_EH_PE_udata2: 10929cb868aSZachary Turner addressValue = DE.GetU16(offset_ptr); 11029cb868aSZachary Turner break; 11129cb868aSZachary Turner case DW_EH_PE_udata4: 11229cb868aSZachary Turner addressValue = DE.GetU32(offset_ptr); 11329cb868aSZachary Turner break; 11429cb868aSZachary Turner case DW_EH_PE_udata8: 11529cb868aSZachary Turner addressValue = DE.GetU64(offset_ptr); 11629cb868aSZachary Turner break; 11729cb868aSZachary Turner case DW_EH_PE_sleb128: 11829cb868aSZachary Turner addressValue = DE.GetSLEB128(offset_ptr); 11929cb868aSZachary Turner break; 12029cb868aSZachary Turner case DW_EH_PE_sdata2: 12129cb868aSZachary Turner addressValue = (int16_t)DE.GetU16(offset_ptr); 12229cb868aSZachary Turner break; 12329cb868aSZachary Turner case DW_EH_PE_sdata4: 12429cb868aSZachary Turner addressValue = (int32_t)DE.GetU32(offset_ptr); 12529cb868aSZachary Turner break; 12629cb868aSZachary Turner case DW_EH_PE_sdata8: 12729cb868aSZachary Turner addressValue = (int64_t)DE.GetU64(offset_ptr); 12829cb868aSZachary Turner break; 12929cb868aSZachary Turner default: 13029cb868aSZachary Turner // Unhandled encoding type 13129cb868aSZachary Turner assert(eh_ptr_enc); 13229cb868aSZachary Turner break; 13329cb868aSZachary Turner } 13429cb868aSZachary Turner 13529cb868aSZachary Turner // Since we promote everything to 64 bit, we may need to sign extend 13629cb868aSZachary Turner if (signExtendValue && addr_size < sizeof(baseAddress)) { 13729cb868aSZachary Turner uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull); 13829cb868aSZachary Turner if (sign_bit & addressValue) { 13929cb868aSZachary Turner uint64_t mask = ~sign_bit + 1; 14029cb868aSZachary Turner addressValue |= mask; 14129cb868aSZachary Turner } 14229cb868aSZachary Turner } 14329cb868aSZachary Turner return baseAddress + addressValue; 14429cb868aSZachary Turner } 14529cb868aSZachary Turner 146b9c1b51eSKate Stone DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile &objfile, 1473f2a0818SPavel Labath SectionSP §ion_sp, Type type) 1483f2a0818SPavel Labath : m_objfile(objfile), m_section_sp(section_sp), m_type(type) {} 14930fdc8d8SChris Lattner 15023594291SPavel Labath bool DWARFCallFrameInfo::GetUnwindPlan(const Address &addr, 15123594291SPavel Labath UnwindPlan &unwind_plan) { 15223594291SPavel Labath return GetUnwindPlan(AddressRange(addr, 1), unwind_plan); 15323594291SPavel Labath } 15423594291SPavel Labath 15523594291SPavel Labath bool DWARFCallFrameInfo::GetUnwindPlan(const AddressRange &range, 15623594291SPavel Labath UnwindPlan &unwind_plan) { 15774303829SJason Molenda FDEEntryMap::Entry fde_entry; 15823594291SPavel Labath Address addr = range.GetBaseAddress(); 15974303829SJason Molenda 16005097246SAdrian Prantl // Make sure that the Address we're searching for is the same object file as 16105097246SAdrian Prantl // this DWARFCallFrameInfo, we only store File offsets in m_fde_index. 16274303829SJason Molenda ModuleSP module_sp = addr.GetModule(); 163b9c1b51eSKate Stone if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || 164b9c1b51eSKate Stone module_sp->GetObjectFile() != &m_objfile) 165fbcb7f2cSJason Molenda return false; 16674303829SJason Molenda 16723594291SPavel Labath if (llvm::Optional<FDEEntryMap::Entry> entry = GetFirstFDEEntryInRange(range)) 16823594291SPavel Labath return FDEToUnwindPlan(entry->data, addr, unwind_plan); 16974303829SJason Molenda return false; 17030fdc8d8SChris Lattner } 17130fdc8d8SChris Lattner 172b9c1b51eSKate Stone bool DWARFCallFrameInfo::GetAddressRange(Address addr, AddressRange &range) { 17374303829SJason Molenda 17405097246SAdrian Prantl // Make sure that the Address we're searching for is the same object file as 17505097246SAdrian Prantl // this DWARFCallFrameInfo, we only store File offsets in m_fde_index. 17674303829SJason Molenda ModuleSP module_sp = addr.GetModule(); 177b9c1b51eSKate Stone if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || 178b9c1b51eSKate Stone module_sp->GetObjectFile() != &m_objfile) 17974303829SJason Molenda return false; 18074303829SJason Molenda 181d4612ad0SEd Maste if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 182fbcb7f2cSJason Molenda return false; 183fbcb7f2cSJason Molenda GetFDEIndex(); 184b9c1b51eSKate Stone FDEEntryMap::Entry *fde_entry = 185b9c1b51eSKate Stone m_fde_index.FindEntryThatContains(addr.GetFileAddress()); 18674303829SJason Molenda if (!fde_entry) 187fbcb7f2cSJason Molenda return false; 188fbcb7f2cSJason Molenda 189b9c1b51eSKate Stone range = AddressRange(fde_entry->base, fde_entry->size, 190b9c1b51eSKate Stone m_objfile.GetSectionList()); 191fbcb7f2cSJason Molenda return true; 19230fdc8d8SChris Lattner } 19330fdc8d8SChris Lattner 19423594291SPavel Labath llvm::Optional<DWARFCallFrameInfo::FDEEntryMap::Entry> 19523594291SPavel Labath DWARFCallFrameInfo::GetFirstFDEEntryInRange(const AddressRange &range) { 19623594291SPavel Labath if (!m_section_sp || m_section_sp->IsEncrypted()) 19723594291SPavel Labath return llvm::None; 19874303829SJason Molenda 19974303829SJason Molenda GetFDEIndex(); 20074303829SJason Molenda 20123594291SPavel Labath addr_t start_file_addr = range.GetBaseAddress().GetFileAddress(); 20223594291SPavel Labath const FDEEntryMap::Entry *fde = 20323594291SPavel Labath m_fde_index.FindEntryThatContainsOrFollows(start_file_addr); 20423594291SPavel Labath if (fde && fde->DoesIntersect( 20523594291SPavel Labath FDEEntryMap::Range(start_file_addr, range.GetByteSize()))) 20623594291SPavel Labath return *fde; 20774303829SJason Molenda 20823594291SPavel Labath return llvm::None; 20930fdc8d8SChris Lattner } 21030fdc8d8SChris Lattner 211b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFunctionAddressAndSizeVector( 212b9c1b51eSKate Stone FunctionAddressAndSizeVector &function_info) { 2135635f77aSJason Molenda GetFDEIndex(); 2145635f77aSJason Molenda const size_t count = m_fde_index.GetSize(); 215ee7593fbSJason Molenda function_info.Clear(); 216c1946cd8SJason Molenda if (count > 0) 217ee7593fbSJason Molenda function_info.Reserve(count); 218b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 219b9c1b51eSKate Stone const FDEEntryMap::Entry *func_offset_data_entry = 220b9c1b51eSKate Stone m_fde_index.GetEntryAtIndex(i); 221b9c1b51eSKate Stone if (func_offset_data_entry) { 222b9c1b51eSKate Stone FunctionAddressAndSizeVector::Entry function_offset_entry( 223b9c1b51eSKate Stone func_offset_data_entry->base, func_offset_data_entry->size); 2245635f77aSJason Molenda function_info.Append(function_offset_entry); 2255635f77aSJason Molenda } 2265635f77aSJason Molenda } 2275635f77aSJason Molenda } 2285635f77aSJason Molenda 22930fdc8d8SChris Lattner const DWARFCallFrameInfo::CIE * 230b9c1b51eSKate Stone DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) { 23130fdc8d8SChris Lattner cie_map_t::iterator pos = m_cie_map.find(cie_offset); 23230fdc8d8SChris Lattner 233b9c1b51eSKate Stone if (pos != m_cie_map.end()) { 23430fdc8d8SChris Lattner // Parse and cache the CIE 23570355aceSJonas Devlieghere if (pos->second == nullptr) 23630fdc8d8SChris Lattner pos->second = ParseCIE(cie_offset); 23730fdc8d8SChris Lattner 23830fdc8d8SChris Lattner return pos->second.get(); 23930fdc8d8SChris Lattner } 240d4612ad0SEd Maste return nullptr; 24130fdc8d8SChris Lattner } 24230fdc8d8SChris Lattner 243fbcb7f2cSJason Molenda DWARFCallFrameInfo::CIESP 244b9c1b51eSKate Stone DWARFCallFrameInfo::ParseCIE(const dw_offset_t cie_offset) { 245fbcb7f2cSJason Molenda CIESP cie_sp(new CIE(cie_offset)); 246c7bece56SGreg Clayton lldb::offset_t offset = cie_offset; 247a6682a41SJonas Devlieghere if (!m_cfi_data_initialized) 248f97c5213SGreg Clayton GetCFIData(); 2491767e115STodd Fiala uint32_t length = m_cfi_data.GetU32(&offset); 2501767e115STodd Fiala dw_offset_t cie_id, end_offset; 2511767e115STodd Fiala bool is_64bit = (length == UINT32_MAX); 2521767e115STodd Fiala if (is_64bit) { 2531767e115STodd Fiala length = m_cfi_data.GetU64(&offset); 2541767e115STodd Fiala cie_id = m_cfi_data.GetU64(&offset); 2551767e115STodd Fiala end_offset = cie_offset + length + 12; 2561767e115STodd Fiala } else { 2571767e115STodd Fiala cie_id = m_cfi_data.GetU32(&offset); 2581767e115STodd Fiala end_offset = cie_offset + length + 4; 2591767e115STodd Fiala } 2603f2a0818SPavel Labath if (length > 0 && ((m_type == DWARF && cie_id == UINT32_MAX) || 2613f2a0818SPavel Labath (m_type == EH && cie_id == 0ul))) { 26230fdc8d8SChris Lattner size_t i; 26330fdc8d8SChris Lattner // cie.offset = cie_offset; 26430fdc8d8SChris Lattner // cie.length = length; 26530fdc8d8SChris Lattner // cie.cieID = cieID; 2666e264d39SAshok Thirumurthi cie_sp->ptr_encoding = DW_EH_PE_absptr; // default 26730fdc8d8SChris Lattner cie_sp->version = m_cfi_data.GetU8(&offset); 268cdda23ebSPavel Labath if (cie_sp->version > CFI_VERSION4) { 269cdda23ebSPavel Labath Host::SystemLog(Host::eSystemLogError, 270cdda23ebSPavel Labath "CIE parse error: CFI version %d is not supported\n", 271cdda23ebSPavel Labath cie_sp->version); 272cdda23ebSPavel Labath return nullptr; 273cdda23ebSPavel Labath } 27430fdc8d8SChris Lattner 275b9c1b51eSKate Stone for (i = 0; i < CFI_AUG_MAX_SIZE; ++i) { 27630fdc8d8SChris Lattner cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset); 277b9c1b51eSKate Stone if (cie_sp->augmentation[i] == '\0') { 27830fdc8d8SChris Lattner // Zero out remaining bytes in augmentation string 27930fdc8d8SChris Lattner for (size_t j = i + 1; j < CFI_AUG_MAX_SIZE; ++j) 28030fdc8d8SChris Lattner cie_sp->augmentation[j] = '\0'; 28130fdc8d8SChris Lattner 28230fdc8d8SChris Lattner break; 28330fdc8d8SChris Lattner } 28430fdc8d8SChris Lattner } 28530fdc8d8SChris Lattner 286b9c1b51eSKate Stone if (i == CFI_AUG_MAX_SIZE && 287b9c1b51eSKate Stone cie_sp->augmentation[CFI_AUG_MAX_SIZE - 1] != '\0') { 288b9c1b51eSKate Stone Host::SystemLog(Host::eSystemLogError, 289b9c1b51eSKate Stone "CIE parse error: CIE augmentation string was too large " 290b9c1b51eSKate Stone "for the fixed sized buffer of %d bytes.\n", 291b9c1b51eSKate Stone CFI_AUG_MAX_SIZE); 292cdda23ebSPavel Labath return nullptr; 29330fdc8d8SChris Lattner } 294cdda23ebSPavel Labath 29505097246SAdrian Prantl // m_cfi_data uses address size from target architecture of the process may 29605097246SAdrian Prantl // ignore these fields? 2973f2a0818SPavel Labath if (m_type == DWARF && cie_sp->version >= CFI_VERSION4) { 298cdda23ebSPavel Labath cie_sp->address_size = m_cfi_data.GetU8(&offset); 299cdda23ebSPavel Labath cie_sp->segment_size = m_cfi_data.GetU8(&offset); 300cdda23ebSPavel Labath } 301cdda23ebSPavel Labath 30230fdc8d8SChris Lattner cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset); 30330fdc8d8SChris Lattner cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset); 304cdda23ebSPavel Labath 305cdda23ebSPavel Labath cie_sp->return_addr_reg_num = 3063f2a0818SPavel Labath m_type == DWARF && cie_sp->version >= CFI_VERSION3 307cdda23ebSPavel Labath ? static_cast<uint32_t>(m_cfi_data.GetULEB128(&offset)) 308cdda23ebSPavel Labath : m_cfi_data.GetU8(&offset); 30930fdc8d8SChris Lattner 310b9c1b51eSKate Stone if (cie_sp->augmentation[0]) { 31105097246SAdrian Prantl // Get the length of the eh_frame augmentation data which starts with a 31205097246SAdrian Prantl // ULEB128 length in bytes 31330fdc8d8SChris Lattner const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset); 31430fdc8d8SChris Lattner const size_t aug_data_end = offset + aug_data_len; 31530fdc8d8SChris Lattner const size_t aug_str_len = strlen(cie_sp->augmentation); 31630fdc8d8SChris Lattner // A 'z' may be present as the first character of the string. 31705097246SAdrian Prantl // If present, the Augmentation Data field shall be present. The contents 31805097246SAdrian Prantl // of the Augmentation Data shall be interpreted according to other 31905097246SAdrian Prantl // characters in the Augmentation String. 320b9c1b51eSKate Stone if (cie_sp->augmentation[0] == 'z') { 32130fdc8d8SChris Lattner // Extract the Augmentation Data 32230fdc8d8SChris Lattner size_t aug_str_idx = 0; 323b9c1b51eSKate Stone for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) { 32430fdc8d8SChris Lattner char aug = cie_sp->augmentation[aug_str_idx]; 325b9c1b51eSKate Stone switch (aug) { 32630fdc8d8SChris Lattner case 'L': 32705097246SAdrian Prantl // Indicates the presence of one argument in the Augmentation Data 32805097246SAdrian Prantl // of the CIE, and a corresponding argument in the Augmentation 32905097246SAdrian Prantl // Data of the FDE. The argument in the Augmentation Data of the 33005097246SAdrian Prantl // CIE is 1-byte and represents the pointer encoding used for the 33105097246SAdrian Prantl // argument in the Augmentation Data of the FDE, which is the 33205097246SAdrian Prantl // address of a language-specific data area (LSDA). The size of the 33305097246SAdrian Prantl // LSDA pointer is specified by the pointer encoding used. 334e9c7ecf6SJason Molenda cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset); 33530fdc8d8SChris Lattner break; 33630fdc8d8SChris Lattner 33730fdc8d8SChris Lattner case 'P': 33805097246SAdrian Prantl // Indicates the presence of two arguments in the Augmentation Data 33905097246SAdrian Prantl // of the CIE. The first argument is 1-byte and represents the 34005097246SAdrian Prantl // pointer encoding used for the second argument, which is the 34105097246SAdrian Prantl // address of a personality routine handler. The size of the 34205097246SAdrian Prantl // personality routine pointer is specified by the pointer encoding 34305097246SAdrian Prantl // used. 344e9c7ecf6SJason Molenda // 34505097246SAdrian Prantl // The address of the personality function will be stored at this 34605097246SAdrian Prantl // location. Pre-execution, it will be all zero's so don't read it 34705097246SAdrian Prantl // until we're trying to do an unwind & the reloc has been 348e9c7ecf6SJason Molenda // resolved. 34930fdc8d8SChris Lattner { 35030fdc8d8SChris Lattner uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset); 351e9c7ecf6SJason Molenda const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 35229cb868aSZachary Turner cie_sp->personality_loc = GetGNUEHPointer( 35329cb868aSZachary Turner m_cfi_data, &offset, arg_ptr_encoding, pc_rel_addr, 35429cb868aSZachary Turner LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS); 35530fdc8d8SChris Lattner } 35630fdc8d8SChris Lattner break; 35730fdc8d8SChris Lattner 35830fdc8d8SChris Lattner case 'R': 35930fdc8d8SChris Lattner // A 'R' may be present at any position after the 36005097246SAdrian Prantl // first character of the string. The Augmentation Data shall 36105097246SAdrian Prantl // include a 1 byte argument that represents the pointer encoding 36205097246SAdrian Prantl // for the address pointers used in the FDE. Example: 0x1B == 36305097246SAdrian Prantl // DW_EH_PE_pcrel | DW_EH_PE_sdata4 36430fdc8d8SChris Lattner cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset); 36530fdc8d8SChris Lattner break; 36630fdc8d8SChris Lattner } 36730fdc8d8SChris Lattner } 368b9c1b51eSKate Stone } else if (strcmp(cie_sp->augmentation, "eh") == 0) { 36905097246SAdrian Prantl // If the Augmentation string has the value "eh", then the EH Data 37005097246SAdrian Prantl // field shall be present 37130fdc8d8SChris Lattner } 37230fdc8d8SChris Lattner 37305097246SAdrian Prantl // Set the offset to be the end of the augmentation data just in case we 37405097246SAdrian Prantl // didn't understand any of the data. 37530fdc8d8SChris Lattner offset = (uint32_t)aug_data_end; 37630fdc8d8SChris Lattner } 37730fdc8d8SChris Lattner 378b9c1b51eSKate Stone if (end_offset > offset) { 37930fdc8d8SChris Lattner cie_sp->inst_offset = offset; 38030fdc8d8SChris Lattner cie_sp->inst_length = end_offset - offset; 38130fdc8d8SChris Lattner } 382b9c1b51eSKate Stone while (offset < end_offset) { 383fbcb7f2cSJason Molenda uint8_t inst = m_cfi_data.GetU8(&offset); 384fbcb7f2cSJason Molenda uint8_t primary_opcode = inst & 0xC0; 385fbcb7f2cSJason Molenda uint8_t extended_opcode = inst & 0x3F; 386fbcb7f2cSJason Molenda 387b9c1b51eSKate Stone if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, 388b9c1b51eSKate Stone cie_sp->data_align, offset, 389b9c1b51eSKate Stone cie_sp->initial_row)) 390fbcb7f2cSJason Molenda break; // Stop if we hit an unrecognized opcode 391fbcb7f2cSJason Molenda } 39230fdc8d8SChris Lattner } 39330fdc8d8SChris Lattner 39430fdc8d8SChris Lattner return cie_sp; 39530fdc8d8SChris Lattner } 39630fdc8d8SChris Lattner 397b9c1b51eSKate Stone void DWARFCallFrameInfo::GetCFIData() { 398a6682a41SJonas Devlieghere if (!m_cfi_data_initialized) { 3995160ce5cSGreg Clayton Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND)); 400f97c5213SGreg Clayton if (log) 4015160ce5cSGreg Clayton m_objfile.GetModule()->LogMessage(log, "Reading EH frame info"); 402c9660546SGreg Clayton m_objfile.ReadSectionData(m_section_sp.get(), m_cfi_data); 403f97c5213SGreg Clayton m_cfi_data_initialized = true; 404f97c5213SGreg Clayton } 405f97c5213SGreg Clayton } 406b9c1b51eSKate Stone // Scan through the eh_frame or debug_frame section looking for FDEs and noting 40705097246SAdrian Prantl // the start/end addresses of the functions and a pointer back to the 40805097246SAdrian Prantl // function's FDE for later expansion. Internalize CIEs as we come across them. 40930fdc8d8SChris Lattner 410b9c1b51eSKate Stone void DWARFCallFrameInfo::GetFDEIndex() { 411d4612ad0SEd Maste if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 412fbcb7f2cSJason Molenda return; 4137d69f12eSSean Callanan 414fbcb7f2cSJason Molenda if (m_fde_index_initialized) 415fbcb7f2cSJason Molenda return; 416fbcb7f2cSJason Molenda 417bb19a13cSSaleem Abdulrasool std::lock_guard<std::mutex> guard(m_fde_index_mutex); 4187d69f12eSSean Callanan 4197d69f12eSSean Callanan if (m_fde_index_initialized) // if two threads hit the locker 4207d69f12eSSean Callanan return; 421e6194f17SJason Molenda 422f9d16476SPavel Labath static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 423f9d16476SPavel Labath Timer scoped_timer(func_cat, "%s - %s", LLVM_PRETTY_FUNCTION, 424b9c1b51eSKate Stone m_objfile.GetFileSpec().GetFilename().AsCString("")); 42574303829SJason Molenda 426c7afda5aSJason Molenda bool clear_address_zeroth_bit = false; 427f760f5aeSPavel Labath if (ArchSpec arch = m_objfile.GetArchitecture()) { 428b9c1b51eSKate Stone if (arch.GetTriple().getArch() == llvm::Triple::arm || 429b9c1b51eSKate Stone arch.GetTriple().getArch() == llvm::Triple::thumb) 430c7afda5aSJason Molenda clear_address_zeroth_bit = true; 431c7afda5aSJason Molenda } 432c7afda5aSJason Molenda 433c7bece56SGreg Clayton lldb::offset_t offset = 0; 434a6682a41SJonas Devlieghere if (!m_cfi_data_initialized) 435f97c5213SGreg Clayton GetCFIData(); 436b9c1b51eSKate Stone while (m_cfi_data.ValidOffsetForDataOfSize(offset, 8)) { 43773bf5dbdSGreg Clayton const dw_offset_t current_entry = offset; 4381767e115STodd Fiala dw_offset_t cie_id, next_entry, cie_offset; 439fbcb7f2cSJason Molenda uint32_t len = m_cfi_data.GetU32(&offset); 4401767e115STodd Fiala bool is_64bit = (len == UINT32_MAX); 4411767e115STodd Fiala if (is_64bit) { 4421767e115STodd Fiala len = m_cfi_data.GetU64(&offset); 4431767e115STodd Fiala cie_id = m_cfi_data.GetU64(&offset); 4441767e115STodd Fiala next_entry = current_entry + len + 12; 4451767e115STodd Fiala cie_offset = current_entry + 12 - cie_id; 4461767e115STodd Fiala } else { 4471767e115STodd Fiala cie_id = m_cfi_data.GetU32(&offset); 4481767e115STodd Fiala next_entry = current_entry + len + 4; 4491767e115STodd Fiala cie_offset = current_entry + 4 - cie_id; 4501767e115STodd Fiala } 451fbcb7f2cSJason Molenda 452b9c1b51eSKate Stone if (next_entry > m_cfi_data.GetByteSize() + 1) { 453b9c1b51eSKate Stone Host::SystemLog(Host::eSystemLogError, "error: Invalid fde/cie next " 454b9c1b51eSKate Stone "entry offset of 0x%x found in " 455b9c1b51eSKate Stone "cie/fde at 0x%x\n", 456b9c1b51eSKate Stone next_entry, current_entry); 457e171da5cSBruce Mitchener // Don't trust anything in this eh_frame section if we find blatantly 458c48ef341SJason Molenda // invalid data. 459c48ef341SJason Molenda m_fde_index.Clear(); 460c48ef341SJason Molenda m_fde_index_initialized = true; 461c48ef341SJason Molenda return; 462c48ef341SJason Molenda } 463cdda23ebSPavel Labath 464cdda23ebSPavel Labath // An FDE entry contains CIE_pointer in debug_frame in same place as cie_id 46505097246SAdrian Prantl // in eh_frame. CIE_pointer is an offset into the .debug_frame section. So, 46605097246SAdrian Prantl // variable cie_offset should be equal to cie_id for debug_frame. 467cdda23ebSPavel Labath // FDE entries with cie_id == 0 shouldn't be ignored for it. 4683f2a0818SPavel Labath if ((cie_id == 0 && m_type == EH) || cie_id == UINT32_MAX || len == 0) { 469cdda23ebSPavel Labath auto cie_sp = ParseCIE(current_entry); 470cdda23ebSPavel Labath if (!cie_sp) { 471cdda23ebSPavel Labath // Cannot parse, the reason is already logged 472cdda23ebSPavel Labath m_fde_index.Clear(); 473cdda23ebSPavel Labath m_fde_index_initialized = true; 474cdda23ebSPavel Labath return; 475cdda23ebSPavel Labath } 476cdda23ebSPavel Labath 477cdda23ebSPavel Labath m_cie_map[current_entry] = std::move(cie_sp); 478cdda23ebSPavel Labath offset = next_entry; 479cdda23ebSPavel Labath continue; 480cdda23ebSPavel Labath } 481cdda23ebSPavel Labath 4823f2a0818SPavel Labath if (m_type == DWARF) 483cdda23ebSPavel Labath cie_offset = cie_id; 484cdda23ebSPavel Labath 485b9c1b51eSKate Stone if (cie_offset > m_cfi_data.GetByteSize()) { 486cdda23ebSPavel Labath Host::SystemLog(Host::eSystemLogError, 487cdda23ebSPavel Labath "error: Invalid cie offset of 0x%x " 488cdda23ebSPavel Labath "found in cie/fde at 0x%x\n", 489b9c1b51eSKate Stone 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 { 515b9c1b51eSKate Stone Host::SystemLog(Host::eSystemLogError, "error: unable to find CIE at " 516b9c1b51eSKate Stone "0x%8.8x for cie_id = 0x%8.8x for " 517b9c1b51eSKate Stone "entry at 0x%8.8x.\n", 518b9c1b51eSKate Stone cie_offset, cie_id, current_entry); 51973bf5dbdSGreg Clayton } 520fbcb7f2cSJason Molenda offset = next_entry; 521fbcb7f2cSJason Molenda } 52274303829SJason Molenda m_fde_index.Sort(); 523fbcb7f2cSJason Molenda m_fde_index_initialized = true; 524fbcb7f2cSJason Molenda } 525fbcb7f2cSJason Molenda 526b9c1b51eSKate Stone bool DWARFCallFrameInfo::FDEToUnwindPlan(dw_offset_t dwarf_offset, 527b9c1b51eSKate Stone Address startaddr, 528b9c1b51eSKate Stone UnwindPlan &unwind_plan) { 5291ee89eb8SPavel Labath Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND); 530c7bece56SGreg Clayton lldb::offset_t offset = dwarf_offset; 531c7bece56SGreg Clayton lldb::offset_t current_entry = offset; 532fbcb7f2cSJason Molenda 533d4612ad0SEd Maste if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 534fbcb7f2cSJason Molenda return false; 535fbcb7f2cSJason Molenda 536a6682a41SJonas Devlieghere if (!m_cfi_data_initialized) 537f97c5213SGreg Clayton GetCFIData(); 538fbcb7f2cSJason Molenda 539fbcb7f2cSJason Molenda uint32_t length = m_cfi_data.GetU32(&offset); 5401767e115STodd Fiala dw_offset_t cie_offset; 5411767e115STodd Fiala bool is_64bit = (length == UINT32_MAX); 5421767e115STodd Fiala if (is_64bit) { 5431767e115STodd Fiala length = m_cfi_data.GetU64(&offset); 5441767e115STodd Fiala cie_offset = m_cfi_data.GetU64(&offset); 5451767e115STodd Fiala } else { 5461767e115STodd Fiala cie_offset = m_cfi_data.GetU32(&offset); 5471767e115STodd Fiala } 548fbcb7f2cSJason Molenda 549cdda23ebSPavel Labath // FDE entries with zeroth cie_offset may occur for debug_frame. 5503f2a0818SPavel Labath assert(!(m_type == EH && 0 == cie_offset) && cie_offset != UINT32_MAX); 55130fdc8d8SChris Lattner 55205097246SAdrian Prantl // Translate the CIE_id from the eh_frame format, which is relative to the 55305097246SAdrian Prantl // FDE offset, into a __eh_frame section offset 5543f2a0818SPavel Labath if (m_type == EH) { 555ab4f1924SJason Molenda unwind_plan.SetSourceName("eh_frame CFI"); 5561767e115STodd Fiala cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset; 55760f0bd49SJason Molenda unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 558b9c1b51eSKate Stone } else { 559ab4f1924SJason Molenda unwind_plan.SetSourceName("DWARF CFI"); 56060f0bd49SJason Molenda // In theory the debug_frame info should be valid at all call sites 56160f0bd49SJason Molenda // ("asynchronous unwind info" as it is sometimes called) but in practice 56260f0bd49SJason Molenda // gcc et al all emit call frame info for the prologue and call sites, but 563b9c1b51eSKate Stone // not for the epilogue or all the other locations during the function 564b9c1b51eSKate Stone // reliably. 56560f0bd49SJason Molenda unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 566ab4f1924SJason Molenda } 56760f0bd49SJason Molenda unwind_plan.SetSourcedFromCompiler(eLazyBoolYes); 56830fdc8d8SChris Lattner 56930fdc8d8SChris Lattner const CIE *cie = GetCIE(cie_offset); 570d4612ad0SEd Maste assert(cie != nullptr); 571fbcb7f2cSJason Molenda 5721767e115STodd Fiala const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4); 573fbcb7f2cSJason Molenda 574c9660546SGreg Clayton const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 57530fdc8d8SChris Lattner const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; 57630fdc8d8SChris Lattner const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; 57729cb868aSZachary Turner lldb::addr_t range_base = 57829cb868aSZachary Turner GetGNUEHPointer(m_cfi_data, &offset, cie->ptr_encoding, pc_rel_addr, 579b9c1b51eSKate Stone text_addr, data_addr); 58029cb868aSZachary Turner lldb::addr_t range_len = GetGNUEHPointer( 58129cb868aSZachary Turner m_cfi_data, &offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, 58229cb868aSZachary Turner pc_rel_addr, text_addr, data_addr); 583b9c1b51eSKate Stone AddressRange range(range_base, m_objfile.GetAddressByteSize(), 584b9c1b51eSKate Stone m_objfile.GetSectionList()); 585fbcb7f2cSJason Molenda range.SetByteSize(range_len); 58630fdc8d8SChris Lattner 587e9c7ecf6SJason Molenda addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS; 588e9c7ecf6SJason Molenda 589b9c1b51eSKate Stone if (cie->augmentation[0] == 'z') { 59030fdc8d8SChris Lattner uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 591b9c1b51eSKate Stone if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) { 592e9c7ecf6SJason Molenda offset_t saved_offset = offset; 59329cb868aSZachary Turner lsda_data_file_address = 59429cb868aSZachary Turner GetGNUEHPointer(m_cfi_data, &offset, cie->lsda_addr_encoding, 59529cb868aSZachary Turner pc_rel_addr, text_addr, data_addr); 596b9c1b51eSKate Stone if (offset - saved_offset != aug_data_len) { 597e9c7ecf6SJason Molenda // There is more in the augmentation region than we know how to process; 598e9c7ecf6SJason Molenda // don't read anything. 599e9c7ecf6SJason Molenda lsda_data_file_address = LLDB_INVALID_ADDRESS; 600e9c7ecf6SJason Molenda } 601e9c7ecf6SJason Molenda offset = saved_offset; 602e9c7ecf6SJason Molenda } 60330fdc8d8SChris Lattner offset += aug_data_len; 60430fdc8d8SChris Lattner } 6053fd917d8SJoseph Tremoulet unwind_plan.SetUnwindPlanForSignalTrap( 6063fd917d8SJoseph Tremoulet strchr(cie->augmentation, 'S') ? eLazyBoolYes : eLazyBoolNo); 6073fd917d8SJoseph Tremoulet 608e9c7ecf6SJason Molenda Address lsda_data; 609e9c7ecf6SJason Molenda Address personality_function_ptr; 610e9c7ecf6SJason Molenda 611b9c1b51eSKate Stone if (lsda_data_file_address != LLDB_INVALID_ADDRESS && 612b9c1b51eSKate Stone cie->personality_loc != LLDB_INVALID_ADDRESS) { 613b9c1b51eSKate Stone m_objfile.GetModule()->ResolveFileAddress(lsda_data_file_address, 614b9c1b51eSKate Stone lsda_data); 615b9c1b51eSKate Stone m_objfile.GetModule()->ResolveFileAddress(cie->personality_loc, 616b9c1b51eSKate Stone personality_function_ptr); 617e9c7ecf6SJason Molenda } 618e9c7ecf6SJason Molenda 619b9c1b51eSKate Stone if (lsda_data.IsValid() && personality_function_ptr.IsValid()) { 620e9c7ecf6SJason Molenda unwind_plan.SetLSDAAddress(lsda_data); 621e9c7ecf6SJason Molenda unwind_plan.SetPersonalityFunctionPtr(personality_function_ptr); 622e9c7ecf6SJason Molenda } 62330fdc8d8SChris Lattner 62430fdc8d8SChris Lattner uint32_t code_align = cie->code_align; 62530fdc8d8SChris Lattner int32_t data_align = cie->data_align; 62630fdc8d8SChris Lattner 627fbcb7f2cSJason Molenda unwind_plan.SetPlanValidAddressRange(range); 6281d42c7bcSJason Molenda UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row; 6291d42c7bcSJason Molenda *cie_initial_row = cie->initial_row; 6301d42c7bcSJason Molenda UnwindPlan::RowSP row(cie_initial_row); 63130fdc8d8SChris Lattner 6323f2a0818SPavel Labath unwind_plan.SetRegisterKind(GetRegisterKind()); 6338eba46c6SJason Molenda unwind_plan.SetReturnAddressRegister(cie->return_addr_reg_num); 634fbcb7f2cSJason Molenda 635f70d6040SRichard Mitton std::vector<UnwindPlan::RowSP> stack; 636f70d6040SRichard Mitton 637fbcb7f2cSJason Molenda UnwindPlan::Row::RegisterLocation reg_location; 638b9c1b51eSKate Stone while (m_cfi_data.ValidOffset(offset) && offset < end_offset) { 63930fdc8d8SChris Lattner uint8_t inst = m_cfi_data.GetU8(&offset); 64030fdc8d8SChris Lattner uint8_t primary_opcode = inst & 0xC0; 64130fdc8d8SChris Lattner uint8_t extended_opcode = inst & 0x3F; 64230fdc8d8SChris Lattner 643b9c1b51eSKate Stone if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align, 644b9c1b51eSKate Stone offset, *row)) { 645b9c1b51eSKate Stone if (primary_opcode) { 646b9c1b51eSKate Stone switch (primary_opcode) { 64730fdc8d8SChris Lattner case DW_CFA_advance_loc: // (Row Creation Instruction) 64830fdc8d8SChris Lattner { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta 64930fdc8d8SChris Lattner // takes a single argument that represents a constant delta. The 65005097246SAdrian Prantl // required action is to create a new table row with a location value 65105097246SAdrian Prantl // that is computed by taking the current entry's location value and 65205097246SAdrian Prantl // adding (delta * code_align). All other values in the new row are 65305097246SAdrian Prantl // initially identical to the current row. 654fbcb7f2cSJason Molenda unwind_plan.AppendRow(row); 655fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 656fa67e879SJason Molenda *newrow = *row.get(); 657fa67e879SJason Molenda row.reset(newrow); 6581d42c7bcSJason Molenda row->SlideOffset(extended_opcode * code_align); 65930fdc8d8SChris Lattner break; 66030fdc8d8SChris Lattner } 66130fdc8d8SChris Lattner 662b9c1b51eSKate Stone case DW_CFA_restore: { // 0xC0 - high 2 bits are 0x3, lower 6 bits are 663b9c1b51eSKate Stone // register 66430fdc8d8SChris Lattner // takes a single argument that represents a register number. The 66530fdc8d8SChris Lattner // required action is to change the rule for the indicated register 66630fdc8d8SChris Lattner // to the rule assigned it by the initial_instructions in the CIE. 667623652a0STamas Berghammer uint32_t reg_num = extended_opcode; 66805097246SAdrian Prantl // We only keep enough register locations around to unwind what is in 66905097246SAdrian Prantl // our thread, and these are organized by the register index in that 67005097246SAdrian Prantl // state, so we need to convert our eh_frame register number from the 67105097246SAdrian Prantl // EH frame info, to a register index 67230fdc8d8SChris Lattner 673b9c1b51eSKate Stone if (unwind_plan.IsValidRowIndex(0) && 674b9c1b51eSKate Stone unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, 675b9c1b51eSKate Stone reg_location)) 6761d42c7bcSJason Molenda row->SetRegisterInfo(reg_num, reg_location); 67730fdc8d8SChris Lattner break; 67830fdc8d8SChris Lattner } 67930fdc8d8SChris Lattner } 680b9c1b51eSKate Stone } else { 681b9c1b51eSKate Stone switch (extended_opcode) { 68230fdc8d8SChris Lattner case DW_CFA_set_loc: // 0x1 (Row Creation Instruction) 68330fdc8d8SChris Lattner { 68430fdc8d8SChris Lattner // DW_CFA_set_loc takes a single argument that represents an address. 68530fdc8d8SChris Lattner // The required action is to create a new table row using the 68630fdc8d8SChris Lattner // specified address as the location. All other values in the new row 68730fdc8d8SChris Lattner // are initially identical to the current row. The new location value 68830fdc8d8SChris Lattner // should always be greater than the current one. 689fbcb7f2cSJason Molenda unwind_plan.AppendRow(row); 690fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 691fa67e879SJason Molenda *newrow = *row.get(); 692fa67e879SJason Molenda row.reset(newrow); 693b9c1b51eSKate Stone row->SetOffset(m_cfi_data.GetPointer(&offset) - 694b9c1b51eSKate Stone startaddr.GetFileAddress()); 69530fdc8d8SChris Lattner break; 696623652a0STamas Berghammer } 69730fdc8d8SChris Lattner 69830fdc8d8SChris Lattner case DW_CFA_advance_loc1: // 0x2 (Row Creation Instruction) 69930fdc8d8SChris Lattner { 70030fdc8d8SChris Lattner // takes a single uword argument that represents a constant delta. 70130fdc8d8SChris Lattner // This instruction is identical to DW_CFA_advance_loc except for the 70230fdc8d8SChris Lattner // encoding and size of the delta argument. 703fbcb7f2cSJason Molenda unwind_plan.AppendRow(row); 704fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 705fa67e879SJason Molenda *newrow = *row.get(); 706fa67e879SJason Molenda row.reset(newrow); 7071d42c7bcSJason Molenda row->SlideOffset(m_cfi_data.GetU8(&offset) * code_align); 70830fdc8d8SChris Lattner break; 709623652a0STamas Berghammer } 71030fdc8d8SChris Lattner 71130fdc8d8SChris Lattner case DW_CFA_advance_loc2: // 0x3 (Row Creation Instruction) 71230fdc8d8SChris Lattner { 71330fdc8d8SChris Lattner // takes a single uword argument that represents a constant delta. 71430fdc8d8SChris Lattner // This instruction is identical to DW_CFA_advance_loc except for the 71530fdc8d8SChris Lattner // encoding and size of the delta argument. 716fbcb7f2cSJason Molenda unwind_plan.AppendRow(row); 717fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 718fa67e879SJason Molenda *newrow = *row.get(); 719fa67e879SJason Molenda row.reset(newrow); 7201d42c7bcSJason Molenda row->SlideOffset(m_cfi_data.GetU16(&offset) * code_align); 72130fdc8d8SChris Lattner break; 722623652a0STamas Berghammer } 72330fdc8d8SChris Lattner 72430fdc8d8SChris Lattner case DW_CFA_advance_loc4: // 0x4 (Row Creation Instruction) 72530fdc8d8SChris Lattner { 72630fdc8d8SChris Lattner // takes a single uword argument that represents a constant delta. 72730fdc8d8SChris Lattner // This instruction is identical to DW_CFA_advance_loc except for the 72830fdc8d8SChris Lattner // encoding and size of the delta argument. 729fbcb7f2cSJason Molenda unwind_plan.AppendRow(row); 730fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 731fa67e879SJason Molenda *newrow = *row.get(); 732fa67e879SJason Molenda row.reset(newrow); 7331d42c7bcSJason Molenda row->SlideOffset(m_cfi_data.GetU32(&offset) * code_align); 73430fdc8d8SChris Lattner break; 73530fdc8d8SChris Lattner } 73630fdc8d8SChris Lattner 73730fdc8d8SChris Lattner case DW_CFA_restore_extended: // 0x6 73830fdc8d8SChris Lattner { 73930fdc8d8SChris Lattner // takes a single unsigned LEB128 argument that represents a register 74030fdc8d8SChris Lattner // number. This instruction is identical to DW_CFA_restore except for 74130fdc8d8SChris Lattner // the encoding and size of the register argument. 742623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 743b9c1b51eSKate Stone if (unwind_plan.IsValidRowIndex(0) && 744b9c1b51eSKate Stone unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, 745b9c1b51eSKate Stone reg_location)) 7461d42c7bcSJason Molenda row->SetRegisterInfo(reg_num, reg_location); 74730fdc8d8SChris Lattner break; 74830fdc8d8SChris Lattner } 74930fdc8d8SChris Lattner 75030fdc8d8SChris Lattner case DW_CFA_remember_state: // 0xA 751fa67e879SJason Molenda { 75230fdc8d8SChris Lattner // These instructions define a stack of information. Encountering the 75305097246SAdrian Prantl // DW_CFA_remember_state instruction means to save the rules for 75405097246SAdrian Prantl // every register on the current row on the stack. Encountering the 75530fdc8d8SChris Lattner // DW_CFA_restore_state instruction means to pop the set of rules off 75630fdc8d8SChris Lattner // the stack and place them in the current row. (This operation is 75730fdc8d8SChris Lattner // useful for compilers that move epilogue code into the body of a 75830fdc8d8SChris Lattner // function.) 759f70d6040SRichard Mitton stack.push_back(row); 760fa67e879SJason Molenda UnwindPlan::Row *newrow = new UnwindPlan::Row; 761fa67e879SJason Molenda *newrow = *row.get(); 762fa67e879SJason Molenda row.reset(newrow); 76330fdc8d8SChris Lattner break; 764623652a0STamas Berghammer } 76530fdc8d8SChris Lattner 76630fdc8d8SChris Lattner case DW_CFA_restore_state: // 0xB 767623652a0STamas Berghammer { 76830fdc8d8SChris Lattner // These instructions define a stack of information. Encountering the 76905097246SAdrian Prantl // DW_CFA_remember_state instruction means to save the rules for 77005097246SAdrian Prantl // every register on the current row on the stack. Encountering the 77130fdc8d8SChris Lattner // DW_CFA_restore_state instruction means to pop the set of rules off 77230fdc8d8SChris Lattner // the stack and place them in the current row. (This operation is 77330fdc8d8SChris Lattner // useful for compilers that move epilogue code into the body of a 77430fdc8d8SChris Lattner // function.) 775b9c1b51eSKate Stone if (stack.empty()) { 77663e5fb76SJonas Devlieghere LLDB_LOGF(log, 77763e5fb76SJonas Devlieghere "DWARFCallFrameInfo::%s(dwarf_offset: %" PRIx32 778b9c1b51eSKate Stone ", startaddr: %" PRIx64 779b9c1b51eSKate Stone " encountered DW_CFA_restore_state but state stack " 780b9c1b51eSKate Stone "is empty. Corrupt unwind info?", 78163e5fb76SJonas Devlieghere __FUNCTION__, dwarf_offset, startaddr.GetFileAddress()); 7821ee89eb8SPavel Labath break; 7831ee89eb8SPavel Labath } 78449dd6d15STamas Berghammer lldb::addr_t offset = row->GetOffset(); 785f70d6040SRichard Mitton row = stack.back(); 786f70d6040SRichard Mitton stack.pop_back(); 78749dd6d15STamas Berghammer row->SetOffset(offset); 78830fdc8d8SChris Lattner break; 789623652a0STamas Berghammer } 790623652a0STamas Berghammer 7911ee89eb8SPavel Labath case DW_CFA_GNU_args_size: // 0x2e 7921ee89eb8SPavel Labath { 793b9c1b51eSKate Stone // The DW_CFA_GNU_args_size instruction takes an unsigned LEB128 79405097246SAdrian Prantl // operand representing an argument size. This instruction specifies 79505097246SAdrian Prantl // the total of the size of the arguments which have been pushed onto 79605097246SAdrian Prantl // the stack. 7971ee89eb8SPavel Labath 7981ee89eb8SPavel Labath // TODO: Figure out how we should handle this. 7991ee89eb8SPavel Labath m_cfi_data.GetULEB128(&offset); 800a41deb1cSJason Molenda break; 8011ee89eb8SPavel Labath } 8021ee89eb8SPavel Labath 803623652a0STamas Berghammer case DW_CFA_val_offset: // 0x14 804623652a0STamas Berghammer case DW_CFA_val_offset_sf: // 0x15 805623652a0STamas Berghammer default: 806623652a0STamas Berghammer break; 807623652a0STamas Berghammer } 808623652a0STamas Berghammer } 809623652a0STamas Berghammer } 810623652a0STamas Berghammer } 811623652a0STamas Berghammer unwind_plan.AppendRow(row); 812623652a0STamas Berghammer 813623652a0STamas Berghammer return true; 814623652a0STamas Berghammer } 815623652a0STamas Berghammer 816b9c1b51eSKate Stone bool DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode, 817623652a0STamas Berghammer uint8_t extended_opcode, 818623652a0STamas Berghammer int32_t data_align, 819623652a0STamas Berghammer lldb::offset_t &offset, 820b9c1b51eSKate Stone UnwindPlan::Row &row) { 821623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 822623652a0STamas Berghammer 823b9c1b51eSKate Stone if (primary_opcode) { 824b9c1b51eSKate Stone switch (primary_opcode) { 825b9c1b51eSKate Stone case DW_CFA_offset: { // 0x80 - high 2 bits are 0x2, lower 6 bits are 826b9c1b51eSKate Stone // register 827623652a0STamas Berghammer // takes two arguments: an unsigned LEB128 constant representing a 828623652a0STamas Berghammer // factored offset and a register number. The required action is to 82905097246SAdrian Prantl // change the rule for the register indicated by the register number to 83005097246SAdrian Prantl // be an offset(N) rule with a value of (N = factored offset * 83105097246SAdrian Prantl // data_align). 832623652a0STamas Berghammer uint8_t reg_num = extended_opcode; 833623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 834623652a0STamas Berghammer reg_location.SetAtCFAPlusOffset(op_offset); 835623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 836623652a0STamas Berghammer return true; 837623652a0STamas Berghammer } 838623652a0STamas Berghammer } 839b9c1b51eSKate Stone } else { 840b9c1b51eSKate Stone switch (extended_opcode) { 841623652a0STamas Berghammer case DW_CFA_nop: // 0x0 842623652a0STamas Berghammer return true; 843623652a0STamas Berghammer 844623652a0STamas Berghammer case DW_CFA_offset_extended: // 0x5 845623652a0STamas Berghammer { 84605097246SAdrian Prantl // takes two unsigned LEB128 arguments representing a register number and 84705097246SAdrian Prantl // a factored offset. This instruction is identical to DW_CFA_offset 848623652a0STamas Berghammer // except for the encoding and size of the register argument. 849623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 850623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 851623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 852623652a0STamas Berghammer reg_location.SetAtCFAPlusOffset(op_offset); 853623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 854623652a0STamas Berghammer return true; 855623652a0STamas Berghammer } 856623652a0STamas Berghammer 857623652a0STamas Berghammer case DW_CFA_undefined: // 0x7 858623652a0STamas Berghammer { 859623652a0STamas Berghammer // takes a single unsigned LEB128 argument that represents a register 860623652a0STamas Berghammer // number. The required action is to set the rule for the specified 861623652a0STamas Berghammer // register to undefined. 862623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 863623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 864623652a0STamas Berghammer reg_location.SetUndefined(); 865623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 866623652a0STamas Berghammer return true; 867623652a0STamas Berghammer } 868623652a0STamas Berghammer 869623652a0STamas Berghammer case DW_CFA_same_value: // 0x8 870623652a0STamas Berghammer { 871623652a0STamas Berghammer // takes a single unsigned LEB128 argument that represents a register 872623652a0STamas Berghammer // number. The required action is to set the rule for the specified 873623652a0STamas Berghammer // register to same value. 874623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 875623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 876623652a0STamas Berghammer reg_location.SetSame(); 877623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 878623652a0STamas Berghammer return true; 879623652a0STamas Berghammer } 880623652a0STamas Berghammer 881623652a0STamas Berghammer case DW_CFA_register: // 0x9 882623652a0STamas Berghammer { 88305097246SAdrian Prantl // takes two unsigned LEB128 arguments representing register numbers. The 88405097246SAdrian Prantl // required action is to set the rule for the first register to be the 88505097246SAdrian Prantl // second register. 886623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 887623652a0STamas Berghammer uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 888623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 889623652a0STamas Berghammer reg_location.SetInRegister(other_reg_num); 890623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 891623652a0STamas Berghammer return true; 892623652a0STamas Berghammer } 89330fdc8d8SChris Lattner 89430fdc8d8SChris Lattner case DW_CFA_def_cfa: // 0xC (CFA Definition Instruction) 89530fdc8d8SChris Lattner { 89605097246SAdrian Prantl // Takes two unsigned LEB128 operands representing a register number and 89705097246SAdrian Prantl // a (non-factored) offset. The required action is to define the current 89805097246SAdrian Prantl // CFA rule to use the provided register and offset. 899623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 900623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 901623652a0STamas Berghammer row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset); 902623652a0STamas Berghammer return true; 90330fdc8d8SChris Lattner } 90430fdc8d8SChris Lattner 90530fdc8d8SChris Lattner case DW_CFA_def_cfa_register: // 0xD (CFA Definition Instruction) 90630fdc8d8SChris Lattner { 90730fdc8d8SChris Lattner // takes a single unsigned LEB128 argument representing a register 90805097246SAdrian Prantl // number. The required action is to define the current CFA rule to use 90905097246SAdrian Prantl // the provided register (but to keep the old offset). 910623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 911b9c1b51eSKate Stone row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, 912b9c1b51eSKate Stone row.GetCFAValue().GetOffset()); 913623652a0STamas Berghammer return true; 91430fdc8d8SChris Lattner } 91530fdc8d8SChris Lattner 91630fdc8d8SChris Lattner case DW_CFA_def_cfa_offset: // 0xE (CFA Definition Instruction) 91730fdc8d8SChris Lattner { 91805097246SAdrian Prantl // Takes a single unsigned LEB128 operand representing a (non-factored) 91905097246SAdrian Prantl // offset. The required action is to define the current CFA rule to use 92005097246SAdrian Prantl // the provided offset (but to keep the old register). 921623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 922b9c1b51eSKate Stone row.GetCFAValue().SetIsRegisterPlusOffset( 923b9c1b51eSKate Stone row.GetCFAValue().GetRegisterNumber(), op_offset); 924623652a0STamas Berghammer return true; 92530fdc8d8SChris Lattner } 92630fdc8d8SChris Lattner 92730fdc8d8SChris Lattner case DW_CFA_def_cfa_expression: // 0xF (CFA Definition Instruction) 92830fdc8d8SChris Lattner { 92930fdc8d8SChris Lattner size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset); 930b9c1b51eSKate Stone const uint8_t *block_data = 931b9c1b51eSKate Stone static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len)); 932623652a0STamas Berghammer row.GetCFAValue().SetIsDWARFExpression(block_data, block_len); 933623652a0STamas Berghammer return true; 93430fdc8d8SChris Lattner } 93530fdc8d8SChris Lattner 93630fdc8d8SChris Lattner case DW_CFA_expression: // 0x10 93730fdc8d8SChris Lattner { 93805097246SAdrian Prantl // Takes two operands: an unsigned LEB128 value representing a register 93905097246SAdrian Prantl // number, and a DW_FORM_block value representing a DWARF expression. The 94005097246SAdrian Prantl // required action is to change the rule for the register indicated by 94105097246SAdrian Prantl // the register number to be an expression(E) rule where E is the DWARF 94205097246SAdrian Prantl // expression. That is, the DWARF expression computes the address. The 94305097246SAdrian Prantl // value of the CFA is pushed on the DWARF evaluation stack prior to 94405097246SAdrian Prantl // execution of the DWARF expression. 945623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 94630fdc8d8SChris Lattner uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 947b9c1b51eSKate Stone const uint8_t *block_data = 948b9c1b51eSKate Stone static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len)); 949623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 95030fdc8d8SChris Lattner reg_location.SetAtDWARFExpression(block_data, block_len); 951623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 952623652a0STamas Berghammer return true; 95330fdc8d8SChris Lattner } 95430fdc8d8SChris Lattner 95530fdc8d8SChris Lattner case DW_CFA_offset_extended_sf: // 0x11 95630fdc8d8SChris Lattner { 95705097246SAdrian Prantl // takes two operands: an unsigned LEB128 value representing a register 95805097246SAdrian Prantl // number and a signed LEB128 factored offset. This instruction is 95905097246SAdrian Prantl // identical to DW_CFA_offset_extended except that the second operand is 96005097246SAdrian Prantl // signed and factored. 961623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 962623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 963623652a0STamas Berghammer UnwindPlan::Row::RegisterLocation reg_location; 96430fdc8d8SChris Lattner reg_location.SetAtCFAPlusOffset(op_offset); 965623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 966623652a0STamas Berghammer return true; 96730fdc8d8SChris Lattner } 96830fdc8d8SChris Lattner 96930fdc8d8SChris Lattner case DW_CFA_def_cfa_sf: // 0x12 (CFA Definition Instruction) 97030fdc8d8SChris Lattner { 97105097246SAdrian Prantl // Takes two operands: an unsigned LEB128 value representing a register 97205097246SAdrian Prantl // number and a signed LEB128 factored offset. This instruction is 97305097246SAdrian Prantl // identical to DW_CFA_def_cfa except that the second operand is signed 97405097246SAdrian Prantl // and factored. 975623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 976623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 977623652a0STamas Berghammer row.GetCFAValue().SetIsRegisterPlusOffset(reg_num, op_offset); 978623652a0STamas Berghammer return true; 97930fdc8d8SChris Lattner } 98030fdc8d8SChris Lattner 98130fdc8d8SChris Lattner case DW_CFA_def_cfa_offset_sf: // 0x13 (CFA Definition Instruction) 98230fdc8d8SChris Lattner { 98305097246SAdrian Prantl // takes a signed LEB128 operand representing a factored offset. This 98405097246SAdrian Prantl // instruction is identical to DW_CFA_def_cfa_offset except that the 98505097246SAdrian Prantl // operand is signed and factored. 986623652a0STamas Berghammer int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 987623652a0STamas Berghammer uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber(); 988623652a0STamas Berghammer row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset); 989623652a0STamas Berghammer return true; 99030fdc8d8SChris Lattner } 99130fdc8d8SChris Lattner 99230fdc8d8SChris Lattner case DW_CFA_val_expression: // 0x16 99330fdc8d8SChris Lattner { 99430fdc8d8SChris Lattner // takes two operands: an unsigned LEB128 value representing a register 99505097246SAdrian Prantl // number, and a DW_FORM_block value representing a DWARF expression. The 99605097246SAdrian Prantl // required action is to change the rule for the register indicated by 99705097246SAdrian Prantl // the register number to be a val_expression(E) rule where E is the 99830fdc8d8SChris Lattner // DWARF expression. That is, the DWARF expression computes the value of 99930fdc8d8SChris Lattner // the given register. The value of the CFA is pushed on the DWARF 100030fdc8d8SChris Lattner // evaluation stack prior to execution of the DWARF expression. 1001623652a0STamas Berghammer uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 100230fdc8d8SChris Lattner uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 1003b9c1b51eSKate Stone const uint8_t *block_data = 1004b9c1b51eSKate Stone (const uint8_t *)m_cfi_data.GetData(&offset, block_len); 100530fdc8d8SChris Lattner reg_location.SetIsDWARFExpression(block_data, block_len); 1006623652a0STamas Berghammer row.SetRegisterInfo(reg_num, reg_location); 1007fbcb7f2cSJason Molenda return true; 100830fdc8d8SChris Lattner } 1009623652a0STamas Berghammer } 1010623652a0STamas Berghammer } 1011623652a0STamas Berghammer return false; 1012623652a0STamas Berghammer } 10136b63b148STamas Berghammer 1014b9c1b51eSKate Stone void DWARFCallFrameInfo::ForEachFDEEntries( 1015b9c1b51eSKate Stone const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback) { 10166b63b148STamas Berghammer GetFDEIndex(); 10176b63b148STamas Berghammer 1018b9c1b51eSKate Stone for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i) { 10196b63b148STamas Berghammer const FDEEntryMap::Entry &entry = m_fde_index.GetEntryRef(i); 10206b63b148STamas Berghammer if (!callback(entry.base, entry.size, entry.data)) 10216b63b148STamas Berghammer break; 10226b63b148STamas Berghammer } 10236b63b148STamas Berghammer } 1024