1254721Semaste//===-- DWARFCallFrameInfo.cpp ----------------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste 11254721Semaste// C Includes 12254721Semaste// C++ Includes 13254721Semaste#include <list> 14254721Semaste 15254721Semaste#include "lldb/Core/Log.h" 16254721Semaste#include "lldb/Core/Section.h" 17254721Semaste#include "lldb/Core/ArchSpec.h" 18254721Semaste#include "lldb/Core/Module.h" 19254721Semaste#include "lldb/Core/Section.h" 20254721Semaste#include "lldb/Core/Timer.h" 21254721Semaste#include "lldb/Host/Host.h" 22254721Semaste#include "lldb/Symbol/DWARFCallFrameInfo.h" 23254721Semaste#include "lldb/Symbol/ObjectFile.h" 24254721Semaste#include "lldb/Symbol/UnwindPlan.h" 25254721Semaste#include "lldb/Target/RegisterContext.h" 26254721Semaste#include "lldb/Target/Thread.h" 27254721Semaste 28254721Semasteusing namespace lldb; 29254721Semasteusing namespace lldb_private; 30254721Semaste 31254721SemasteDWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile& objfile, SectionSP& section_sp, lldb::RegisterKind reg_kind, bool is_eh_frame) : 32254721Semaste m_objfile (objfile), 33254721Semaste m_section_sp (section_sp), 34254721Semaste m_reg_kind (reg_kind), // The flavor of registers that the CFI data uses (enum RegisterKind) 35254721Semaste m_flags (), 36254721Semaste m_cie_map (), 37254721Semaste m_cfi_data (), 38254721Semaste m_cfi_data_initialized (false), 39254721Semaste m_fde_index (), 40254721Semaste m_fde_index_initialized (false), 41254721Semaste m_is_eh_frame (is_eh_frame) 42254721Semaste{ 43254721Semaste} 44254721Semaste 45254721SemasteDWARFCallFrameInfo::~DWARFCallFrameInfo() 46254721Semaste{ 47254721Semaste} 48254721Semaste 49254721Semaste 50254721Semastebool 51254721SemasteDWARFCallFrameInfo::GetUnwindPlan (Address addr, UnwindPlan& unwind_plan) 52254721Semaste{ 53254721Semaste FDEEntryMap::Entry fde_entry; 54254721Semaste 55254721Semaste // Make sure that the Address we're searching for is the same object file 56254721Semaste // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index. 57254721Semaste ModuleSP module_sp = addr.GetModule(); 58254721Semaste if (module_sp.get() == NULL || module_sp->GetObjectFile() == NULL || module_sp->GetObjectFile() != &m_objfile) 59254721Semaste return false; 60254721Semaste 61254721Semaste if (GetFDEEntryByFileAddress (addr.GetFileAddress(), fde_entry) == false) 62254721Semaste return false; 63254721Semaste return FDEToUnwindPlan (fde_entry.data, addr, unwind_plan); 64254721Semaste} 65254721Semaste 66254721Semastebool 67254721SemasteDWARFCallFrameInfo::GetAddressRange (Address addr, AddressRange &range) 68254721Semaste{ 69254721Semaste 70254721Semaste // Make sure that the Address we're searching for is the same object file 71254721Semaste // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index. 72254721Semaste ModuleSP module_sp = addr.GetModule(); 73254721Semaste if (module_sp.get() == NULL || module_sp->GetObjectFile() == NULL || module_sp->GetObjectFile() != &m_objfile) 74254721Semaste return false; 75254721Semaste 76254721Semaste if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted()) 77254721Semaste return false; 78254721Semaste GetFDEIndex(); 79254721Semaste FDEEntryMap::Entry *fde_entry = m_fde_index.FindEntryThatContains (addr.GetFileAddress()); 80254721Semaste if (!fde_entry) 81254721Semaste return false; 82254721Semaste 83254721Semaste range = AddressRange(fde_entry->base, fde_entry->size, m_objfile.GetSectionList()); 84254721Semaste return true; 85254721Semaste} 86254721Semaste 87254721Semastebool 88254721SemasteDWARFCallFrameInfo::GetFDEEntryByFileAddress (addr_t file_addr, FDEEntryMap::Entry &fde_entry) 89254721Semaste{ 90254721Semaste if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted()) 91254721Semaste return false; 92254721Semaste 93254721Semaste GetFDEIndex(); 94254721Semaste 95254721Semaste if (m_fde_index.IsEmpty()) 96254721Semaste return false; 97254721Semaste 98254721Semaste FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains (file_addr); 99254721Semaste 100254721Semaste if (fde == NULL) 101254721Semaste return false; 102254721Semaste 103254721Semaste fde_entry = *fde; 104254721Semaste return true; 105254721Semaste} 106254721Semaste 107254721Semastevoid 108254721SemasteDWARFCallFrameInfo::GetFunctionAddressAndSizeVector (FunctionAddressAndSizeVector &function_info) 109254721Semaste{ 110254721Semaste GetFDEIndex(); 111254721Semaste const size_t count = m_fde_index.GetSize(); 112254721Semaste function_info.Clear(); 113254721Semaste if (count > 0) 114254721Semaste function_info.Reserve(count); 115254721Semaste for (size_t i = 0; i < count; ++i) 116254721Semaste { 117254721Semaste const FDEEntryMap::Entry *func_offset_data_entry = m_fde_index.GetEntryAtIndex (i); 118254721Semaste if (func_offset_data_entry) 119254721Semaste { 120254721Semaste FunctionAddressAndSizeVector::Entry function_offset_entry (func_offset_data_entry->base, func_offset_data_entry->size); 121254721Semaste function_info.Append (function_offset_entry); 122254721Semaste } 123254721Semaste } 124254721Semaste} 125254721Semaste 126254721Semasteconst DWARFCallFrameInfo::CIE* 127254721SemasteDWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) 128254721Semaste{ 129254721Semaste cie_map_t::iterator pos = m_cie_map.find(cie_offset); 130254721Semaste 131254721Semaste if (pos != m_cie_map.end()) 132254721Semaste { 133254721Semaste // Parse and cache the CIE 134254721Semaste if (pos->second.get() == NULL) 135254721Semaste pos->second = ParseCIE (cie_offset); 136254721Semaste 137254721Semaste return pos->second.get(); 138254721Semaste } 139254721Semaste return NULL; 140254721Semaste} 141254721Semaste 142254721SemasteDWARFCallFrameInfo::CIESP 143254721SemasteDWARFCallFrameInfo::ParseCIE (const dw_offset_t cie_offset) 144254721Semaste{ 145254721Semaste CIESP cie_sp(new CIE(cie_offset)); 146254721Semaste lldb::offset_t offset = cie_offset; 147254721Semaste if (m_cfi_data_initialized == false) 148254721Semaste GetCFIData(); 149254721Semaste const uint32_t length = m_cfi_data.GetU32(&offset); 150254721Semaste const dw_offset_t cie_id = m_cfi_data.GetU32(&offset); 151254721Semaste const dw_offset_t end_offset = cie_offset + length + 4; 152254721Semaste if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) || (m_is_eh_frame && cie_id == 0ul))) 153254721Semaste { 154254721Semaste size_t i; 155254721Semaste // cie.offset = cie_offset; 156254721Semaste // cie.length = length; 157254721Semaste // cie.cieID = cieID; 158254721Semaste cie_sp->ptr_encoding = DW_EH_PE_absptr; // default 159254721Semaste cie_sp->version = m_cfi_data.GetU8(&offset); 160254721Semaste 161254721Semaste for (i=0; i<CFI_AUG_MAX_SIZE; ++i) 162254721Semaste { 163254721Semaste cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset); 164254721Semaste if (cie_sp->augmentation[i] == '\0') 165254721Semaste { 166254721Semaste // Zero out remaining bytes in augmentation string 167254721Semaste for (size_t j = i+1; j<CFI_AUG_MAX_SIZE; ++j) 168254721Semaste cie_sp->augmentation[j] = '\0'; 169254721Semaste 170254721Semaste break; 171254721Semaste } 172254721Semaste } 173254721Semaste 174254721Semaste if (i == CFI_AUG_MAX_SIZE && cie_sp->augmentation[CFI_AUG_MAX_SIZE-1] != '\0') 175254721Semaste { 176254721Semaste Host::SystemLog (Host::eSystemLogError, "CIE parse error: CIE augmentation string was too large for the fixed sized buffer of %d bytes.\n", CFI_AUG_MAX_SIZE); 177254721Semaste return cie_sp; 178254721Semaste } 179254721Semaste cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset); 180254721Semaste cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset); 181254721Semaste cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset); 182254721Semaste 183254721Semaste if (cie_sp->augmentation[0]) 184254721Semaste { 185254721Semaste // Get the length of the eh_frame augmentation data 186254721Semaste // which starts with a ULEB128 length in bytes 187254721Semaste const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset); 188254721Semaste const size_t aug_data_end = offset + aug_data_len; 189254721Semaste const size_t aug_str_len = strlen(cie_sp->augmentation); 190254721Semaste // A 'z' may be present as the first character of the string. 191254721Semaste // If present, the Augmentation Data field shall be present. 192254721Semaste // The contents of the Augmentation Data shall be intepreted 193254721Semaste // according to other characters in the Augmentation String. 194254721Semaste if (cie_sp->augmentation[0] == 'z') 195254721Semaste { 196254721Semaste // Extract the Augmentation Data 197254721Semaste size_t aug_str_idx = 0; 198254721Semaste for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) 199254721Semaste { 200254721Semaste char aug = cie_sp->augmentation[aug_str_idx]; 201254721Semaste switch (aug) 202254721Semaste { 203254721Semaste case 'L': 204254721Semaste // Indicates the presence of one argument in the 205254721Semaste // Augmentation Data of the CIE, and a corresponding 206254721Semaste // argument in the Augmentation Data of the FDE. The 207254721Semaste // argument in the Augmentation Data of the CIE is 208254721Semaste // 1-byte and represents the pointer encoding used 209254721Semaste // for the argument in the Augmentation Data of the 210254721Semaste // FDE, which is the address of a language-specific 211254721Semaste // data area (LSDA). The size of the LSDA pointer is 212254721Semaste // specified by the pointer encoding used. 213254721Semaste m_cfi_data.GetU8(&offset); 214254721Semaste break; 215254721Semaste 216254721Semaste case 'P': 217254721Semaste // Indicates the presence of two arguments in the 218254721Semaste // Augmentation Data of the cie_sp-> The first argument 219254721Semaste // is 1-byte and represents the pointer encoding 220254721Semaste // used for the second argument, which is the 221254721Semaste // address of a personality routine handler. The 222254721Semaste // size of the personality routine pointer is 223254721Semaste // specified by the pointer encoding used. 224254721Semaste { 225254721Semaste uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset); 226254721Semaste m_cfi_data.GetGNUEHPointer(&offset, arg_ptr_encoding, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS); 227254721Semaste } 228254721Semaste break; 229254721Semaste 230254721Semaste case 'R': 231254721Semaste // A 'R' may be present at any position after the 232254721Semaste // first character of the string. The Augmentation 233254721Semaste // Data shall include a 1 byte argument that 234254721Semaste // represents the pointer encoding for the address 235254721Semaste // pointers used in the FDE. 236254721Semaste // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4 237254721Semaste cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset); 238254721Semaste break; 239254721Semaste } 240254721Semaste } 241254721Semaste } 242254721Semaste else if (strcmp(cie_sp->augmentation, "eh") == 0) 243254721Semaste { 244254721Semaste // If the Augmentation string has the value "eh", then 245254721Semaste // the EH Data field shall be present 246254721Semaste } 247254721Semaste 248254721Semaste // Set the offset to be the end of the augmentation data just in case 249254721Semaste // we didn't understand any of the data. 250254721Semaste offset = (uint32_t)aug_data_end; 251254721Semaste } 252254721Semaste 253254721Semaste if (end_offset > offset) 254254721Semaste { 255254721Semaste cie_sp->inst_offset = offset; 256254721Semaste cie_sp->inst_length = end_offset - offset; 257254721Semaste } 258254721Semaste while (offset < end_offset) 259254721Semaste { 260254721Semaste uint8_t inst = m_cfi_data.GetU8(&offset); 261254721Semaste uint8_t primary_opcode = inst & 0xC0; 262254721Semaste uint8_t extended_opcode = inst & 0x3F; 263254721Semaste 264254721Semaste if (extended_opcode == DW_CFA_def_cfa) 265254721Semaste { 266254721Semaste // Takes two unsigned LEB128 operands representing a register 267254721Semaste // number and a (non-factored) offset. The required action 268254721Semaste // is to define the current CFA rule to use the provided 269254721Semaste // register and offset. 270254721Semaste uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 271254721Semaste int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 272254721Semaste cie_sp->initial_row.SetCFARegister (reg_num); 273254721Semaste cie_sp->initial_row.SetCFAOffset (op_offset); 274254721Semaste continue; 275254721Semaste } 276254721Semaste if (primary_opcode == DW_CFA_offset) 277254721Semaste { 278254721Semaste // 0x80 - high 2 bits are 0x2, lower 6 bits are register. 279254721Semaste // Takes two arguments: an unsigned LEB128 constant representing a 280254721Semaste // factored offset and a register number. The required action is to 281254721Semaste // change the rule for the register indicated by the register number 282254721Semaste // to be an offset(N) rule with a value of 283254721Semaste // (N = factored offset * data_align). 284254721Semaste uint32_t reg_num = extended_opcode; 285254721Semaste int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * cie_sp->data_align; 286254721Semaste UnwindPlan::Row::RegisterLocation reg_location; 287254721Semaste reg_location.SetAtCFAPlusOffset(op_offset); 288254721Semaste cie_sp->initial_row.SetRegisterInfo (reg_num, reg_location); 289254721Semaste continue; 290254721Semaste } 291254721Semaste if (extended_opcode == DW_CFA_nop) 292254721Semaste { 293254721Semaste continue; 294254721Semaste } 295254721Semaste break; // Stop if we hit an unrecognized opcode 296254721Semaste } 297254721Semaste } 298254721Semaste 299254721Semaste return cie_sp; 300254721Semaste} 301254721Semaste 302254721Semastevoid 303254721SemasteDWARFCallFrameInfo::GetCFIData() 304254721Semaste{ 305254721Semaste if (m_cfi_data_initialized == false) 306254721Semaste { 307254721Semaste Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 308254721Semaste if (log) 309254721Semaste m_objfile.GetModule()->LogMessage(log, "Reading EH frame info"); 310254721Semaste m_objfile.ReadSectionData (m_section_sp.get(), m_cfi_data); 311254721Semaste m_cfi_data_initialized = true; 312254721Semaste } 313254721Semaste} 314254721Semaste// Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses 315254721Semaste// of the functions and a pointer back to the function's FDE for later expansion. 316254721Semaste// Internalize CIEs as we come across them. 317254721Semaste 318254721Semastevoid 319254721SemasteDWARFCallFrameInfo::GetFDEIndex () 320254721Semaste{ 321254721Semaste if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted()) 322254721Semaste return; 323254721Semaste 324254721Semaste if (m_fde_index_initialized) 325254721Semaste return; 326254721Semaste 327254721Semaste Mutex::Locker locker(m_fde_index_mutex); 328254721Semaste 329254721Semaste if (m_fde_index_initialized) // if two threads hit the locker 330254721Semaste return; 331254721Semaste 332254721Semaste Timer scoped_timer (__PRETTY_FUNCTION__, "%s - %s", __PRETTY_FUNCTION__, m_objfile.GetFileSpec().GetFilename().AsCString("")); 333254721Semaste 334254721Semaste lldb::offset_t offset = 0; 335254721Semaste if (m_cfi_data_initialized == false) 336254721Semaste GetCFIData(); 337254721Semaste while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8)) 338254721Semaste { 339254721Semaste const dw_offset_t current_entry = offset; 340254721Semaste uint32_t len = m_cfi_data.GetU32 (&offset); 341254721Semaste dw_offset_t next_entry = current_entry + len + 4; 342254721Semaste dw_offset_t cie_id = m_cfi_data.GetU32 (&offset); 343254721Semaste 344263363Semaste if (cie_id == 0 || cie_id == UINT32_MAX || len == 0) 345254721Semaste { 346254721Semaste m_cie_map[current_entry] = ParseCIE (current_entry); 347254721Semaste offset = next_entry; 348254721Semaste continue; 349254721Semaste } 350254721Semaste 351254721Semaste const dw_offset_t cie_offset = current_entry + 4 - cie_id; 352254721Semaste const CIE *cie = GetCIE (cie_offset); 353254721Semaste if (cie) 354254721Semaste { 355254721Semaste const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 356254721Semaste const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; 357254721Semaste const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; 358254721Semaste 359254721Semaste lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr); 360254721Semaste lldb::addr_t length = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr); 361254721Semaste FDEEntryMap::Entry fde (addr, length, current_entry); 362254721Semaste m_fde_index.Append(fde); 363254721Semaste } 364254721Semaste else 365254721Semaste { 366254721Semaste Host::SystemLog (Host::eSystemLogError, 367254721Semaste "error: unable to find CIE at 0x%8.8x for cie_id = 0x%8.8x for entry at 0x%8.8x.\n", 368254721Semaste cie_offset, 369254721Semaste cie_id, 370254721Semaste current_entry); 371254721Semaste } 372254721Semaste offset = next_entry; 373254721Semaste } 374254721Semaste m_fde_index.Sort(); 375254721Semaste m_fde_index_initialized = true; 376254721Semaste} 377254721Semaste 378254721Semastebool 379254721SemasteDWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t dwarf_offset, Address startaddr, UnwindPlan& unwind_plan) 380254721Semaste{ 381254721Semaste lldb::offset_t offset = dwarf_offset; 382254721Semaste lldb::offset_t current_entry = offset; 383254721Semaste 384254721Semaste if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted()) 385254721Semaste return false; 386254721Semaste 387254721Semaste if (m_cfi_data_initialized == false) 388254721Semaste GetCFIData(); 389254721Semaste 390254721Semaste uint32_t length = m_cfi_data.GetU32 (&offset); 391254721Semaste dw_offset_t cie_offset = m_cfi_data.GetU32 (&offset); 392254721Semaste 393254721Semaste assert (cie_offset != 0 && cie_offset != UINT32_MAX); 394254721Semaste 395254721Semaste // Translate the CIE_id from the eh_frame format, which 396254721Semaste // is relative to the FDE offset, into a __eh_frame section 397254721Semaste // offset 398254721Semaste if (m_is_eh_frame) 399254721Semaste { 400254721Semaste unwind_plan.SetSourceName ("eh_frame CFI"); 401254721Semaste cie_offset = current_entry + 4 - cie_offset; 402254721Semaste unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); 403254721Semaste } 404254721Semaste else 405254721Semaste { 406254721Semaste unwind_plan.SetSourceName ("DWARF CFI"); 407254721Semaste // In theory the debug_frame info should be valid at all call sites 408254721Semaste // ("asynchronous unwind info" as it is sometimes called) but in practice 409254721Semaste // gcc et al all emit call frame info for the prologue and call sites, but 410254721Semaste // not for the epilogue or all the other locations during the function reliably. 411254721Semaste unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); 412254721Semaste } 413254721Semaste unwind_plan.SetSourcedFromCompiler (eLazyBoolYes); 414254721Semaste 415254721Semaste const CIE *cie = GetCIE (cie_offset); 416254721Semaste assert (cie != NULL); 417254721Semaste 418254721Semaste const dw_offset_t end_offset = current_entry + length + 4; 419254721Semaste 420254721Semaste const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 421254721Semaste const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; 422254721Semaste const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; 423254721Semaste lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr); 424254721Semaste lldb::addr_t range_len = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr); 425254721Semaste AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList()); 426254721Semaste range.SetByteSize (range_len); 427254721Semaste 428254721Semaste if (cie->augmentation[0] == 'z') 429254721Semaste { 430254721Semaste uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 431254721Semaste offset += aug_data_len; 432254721Semaste } 433254721Semaste 434254721Semaste uint32_t reg_num = 0; 435254721Semaste int32_t op_offset = 0; 436254721Semaste uint32_t code_align = cie->code_align; 437254721Semaste int32_t data_align = cie->data_align; 438254721Semaste 439254721Semaste unwind_plan.SetPlanValidAddressRange (range); 440254721Semaste UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row; 441254721Semaste *cie_initial_row = cie->initial_row; 442254721Semaste UnwindPlan::RowSP row(cie_initial_row); 443254721Semaste 444254721Semaste unwind_plan.SetRegisterKind (m_reg_kind); 445254721Semaste unwind_plan.SetReturnAddressRegister (cie->return_addr_reg_num); 446254721Semaste 447263363Semaste std::vector<UnwindPlan::RowSP> stack; 448263363Semaste 449254721Semaste UnwindPlan::Row::RegisterLocation reg_location; 450254721Semaste while (m_cfi_data.ValidOffset(offset) && offset < end_offset) 451254721Semaste { 452254721Semaste uint8_t inst = m_cfi_data.GetU8(&offset); 453254721Semaste uint8_t primary_opcode = inst & 0xC0; 454254721Semaste uint8_t extended_opcode = inst & 0x3F; 455254721Semaste 456254721Semaste if (primary_opcode) 457254721Semaste { 458254721Semaste switch (primary_opcode) 459254721Semaste { 460254721Semaste case DW_CFA_advance_loc : // (Row Creation Instruction) 461254721Semaste { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta 462254721Semaste // takes a single argument that represents a constant delta. The 463254721Semaste // required action is to create a new table row with a location 464254721Semaste // value that is computed by taking the current entry's location 465254721Semaste // value and adding (delta * code_align). All other 466254721Semaste // values in the new row are initially identical to the current row. 467254721Semaste unwind_plan.AppendRow(row); 468254721Semaste UnwindPlan::Row *newrow = new UnwindPlan::Row; 469254721Semaste *newrow = *row.get(); 470254721Semaste row.reset (newrow); 471254721Semaste row->SlideOffset(extended_opcode * code_align); 472254721Semaste } 473254721Semaste break; 474254721Semaste 475254721Semaste case DW_CFA_offset : 476254721Semaste { // 0x80 - high 2 bits are 0x2, lower 6 bits are register 477254721Semaste // takes two arguments: an unsigned LEB128 constant representing a 478254721Semaste // factored offset and a register number. The required action is to 479254721Semaste // change the rule for the register indicated by the register number 480254721Semaste // to be an offset(N) rule with a value of 481254721Semaste // (N = factored offset * data_align). 482254721Semaste reg_num = extended_opcode; 483254721Semaste op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 484254721Semaste reg_location.SetAtCFAPlusOffset(op_offset); 485254721Semaste row->SetRegisterInfo (reg_num, reg_location); 486254721Semaste } 487254721Semaste break; 488254721Semaste 489254721Semaste case DW_CFA_restore : 490254721Semaste { // 0xC0 - high 2 bits are 0x3, lower 6 bits are register 491254721Semaste // takes a single argument that represents a register number. The 492254721Semaste // required action is to change the rule for the indicated register 493254721Semaste // to the rule assigned it by the initial_instructions in the CIE. 494254721Semaste reg_num = extended_opcode; 495254721Semaste // We only keep enough register locations around to 496254721Semaste // unwind what is in our thread, and these are organized 497254721Semaste // by the register index in that state, so we need to convert our 498254721Semaste // GCC register number from the EH frame info, to a register index 499254721Semaste 500254721Semaste if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location)) 501254721Semaste row->SetRegisterInfo (reg_num, reg_location); 502254721Semaste } 503254721Semaste break; 504254721Semaste } 505254721Semaste } 506254721Semaste else 507254721Semaste { 508254721Semaste switch (extended_opcode) 509254721Semaste { 510254721Semaste case DW_CFA_nop : // 0x0 511254721Semaste break; 512254721Semaste 513254721Semaste case DW_CFA_set_loc : // 0x1 (Row Creation Instruction) 514254721Semaste { 515254721Semaste // DW_CFA_set_loc takes a single argument that represents an address. 516254721Semaste // The required action is to create a new table row using the 517254721Semaste // specified address as the location. All other values in the new row 518254721Semaste // are initially identical to the current row. The new location value 519254721Semaste // should always be greater than the current one. 520254721Semaste unwind_plan.AppendRow(row); 521254721Semaste UnwindPlan::Row *newrow = new UnwindPlan::Row; 522254721Semaste *newrow = *row.get(); 523254721Semaste row.reset (newrow); 524254721Semaste row->SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress()); 525254721Semaste } 526254721Semaste break; 527254721Semaste 528254721Semaste case DW_CFA_advance_loc1 : // 0x2 (Row Creation Instruction) 529254721Semaste { 530254721Semaste // takes a single uword argument that represents a constant delta. 531254721Semaste // This instruction is identical to DW_CFA_advance_loc except for the 532254721Semaste // encoding and size of the delta argument. 533254721Semaste unwind_plan.AppendRow(row); 534254721Semaste UnwindPlan::Row *newrow = new UnwindPlan::Row; 535254721Semaste *newrow = *row.get(); 536254721Semaste row.reset (newrow); 537254721Semaste row->SlideOffset (m_cfi_data.GetU8(&offset) * code_align); 538254721Semaste } 539254721Semaste break; 540254721Semaste 541254721Semaste case DW_CFA_advance_loc2 : // 0x3 (Row Creation Instruction) 542254721Semaste { 543254721Semaste // takes a single uword argument that represents a constant delta. 544254721Semaste // This instruction is identical to DW_CFA_advance_loc except for the 545254721Semaste // encoding and size of the delta argument. 546254721Semaste unwind_plan.AppendRow(row); 547254721Semaste UnwindPlan::Row *newrow = new UnwindPlan::Row; 548254721Semaste *newrow = *row.get(); 549254721Semaste row.reset (newrow); 550254721Semaste row->SlideOffset (m_cfi_data.GetU16(&offset) * code_align); 551254721Semaste } 552254721Semaste break; 553254721Semaste 554254721Semaste case DW_CFA_advance_loc4 : // 0x4 (Row Creation Instruction) 555254721Semaste { 556254721Semaste // takes a single uword argument that represents a constant delta. 557254721Semaste // This instruction is identical to DW_CFA_advance_loc except for the 558254721Semaste // encoding and size of the delta argument. 559254721Semaste unwind_plan.AppendRow(row); 560254721Semaste UnwindPlan::Row *newrow = new UnwindPlan::Row; 561254721Semaste *newrow = *row.get(); 562254721Semaste row.reset (newrow); 563254721Semaste row->SlideOffset (m_cfi_data.GetU32(&offset) * code_align); 564254721Semaste } 565254721Semaste break; 566254721Semaste 567254721Semaste case DW_CFA_offset_extended : // 0x5 568254721Semaste { 569254721Semaste // takes two unsigned LEB128 arguments representing a register number 570254721Semaste // and a factored offset. This instruction is identical to DW_CFA_offset 571254721Semaste // except for the encoding and size of the register argument. 572254721Semaste reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 573254721Semaste op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 574254721Semaste reg_location.SetAtCFAPlusOffset(op_offset); 575254721Semaste row->SetRegisterInfo (reg_num, reg_location); 576254721Semaste } 577254721Semaste break; 578254721Semaste 579254721Semaste case DW_CFA_restore_extended : // 0x6 580254721Semaste { 581254721Semaste // takes a single unsigned LEB128 argument that represents a register 582254721Semaste // number. This instruction is identical to DW_CFA_restore except for 583254721Semaste // the encoding and size of the register argument. 584254721Semaste reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 585254721Semaste if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location)) 586254721Semaste row->SetRegisterInfo (reg_num, reg_location); 587254721Semaste } 588254721Semaste break; 589254721Semaste 590254721Semaste case DW_CFA_undefined : // 0x7 591254721Semaste { 592254721Semaste // takes a single unsigned LEB128 argument that represents a register 593254721Semaste // number. The required action is to set the rule for the specified 594254721Semaste // register to undefined. 595254721Semaste reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 596254721Semaste reg_location.SetUndefined(); 597254721Semaste row->SetRegisterInfo (reg_num, reg_location); 598254721Semaste } 599254721Semaste break; 600254721Semaste 601254721Semaste case DW_CFA_same_value : // 0x8 602254721Semaste { 603254721Semaste // takes a single unsigned LEB128 argument that represents a register 604254721Semaste // number. The required action is to set the rule for the specified 605254721Semaste // register to same value. 606254721Semaste reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 607254721Semaste reg_location.SetSame(); 608254721Semaste row->SetRegisterInfo (reg_num, reg_location); 609254721Semaste } 610254721Semaste break; 611254721Semaste 612254721Semaste case DW_CFA_register : // 0x9 613254721Semaste { 614254721Semaste // takes two unsigned LEB128 arguments representing register numbers. 615254721Semaste // The required action is to set the rule for the first register to be 616254721Semaste // the second register. 617254721Semaste 618254721Semaste reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 619254721Semaste uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 620254721Semaste reg_location.SetInRegister(other_reg_num); 621254721Semaste row->SetRegisterInfo (reg_num, reg_location); 622254721Semaste } 623254721Semaste break; 624254721Semaste 625254721Semaste case DW_CFA_remember_state : // 0xA 626254721Semaste { 627254721Semaste // These instructions define a stack of information. Encountering the 628254721Semaste // DW_CFA_remember_state instruction means to save the rules for every 629254721Semaste // register on the current row on the stack. Encountering the 630254721Semaste // DW_CFA_restore_state instruction means to pop the set of rules off 631254721Semaste // the stack and place them in the current row. (This operation is 632254721Semaste // useful for compilers that move epilogue code into the body of a 633254721Semaste // function.) 634263363Semaste stack.push_back (row); 635254721Semaste UnwindPlan::Row *newrow = new UnwindPlan::Row; 636254721Semaste *newrow = *row.get(); 637254721Semaste row.reset (newrow); 638254721Semaste } 639254721Semaste break; 640254721Semaste 641254721Semaste case DW_CFA_restore_state : // 0xB 642254721Semaste // These instructions define a stack of information. Encountering the 643254721Semaste // DW_CFA_remember_state instruction means to save the rules for every 644254721Semaste // register on the current row on the stack. Encountering the 645254721Semaste // DW_CFA_restore_state instruction means to pop the set of rules off 646254721Semaste // the stack and place them in the current row. (This operation is 647254721Semaste // useful for compilers that move epilogue code into the body of a 648254721Semaste // function.) 649254721Semaste { 650263363Semaste row = stack.back (); 651263363Semaste stack.pop_back (); 652254721Semaste } 653254721Semaste break; 654254721Semaste 655254721Semaste case DW_CFA_def_cfa : // 0xC (CFA Definition Instruction) 656254721Semaste { 657254721Semaste // Takes two unsigned LEB128 operands representing a register 658254721Semaste // number and a (non-factored) offset. The required action 659254721Semaste // is to define the current CFA rule to use the provided 660254721Semaste // register and offset. 661254721Semaste reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 662254721Semaste op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 663254721Semaste row->SetCFARegister (reg_num); 664254721Semaste row->SetCFAOffset (op_offset); 665254721Semaste } 666254721Semaste break; 667254721Semaste 668254721Semaste case DW_CFA_def_cfa_register : // 0xD (CFA Definition Instruction) 669254721Semaste { 670254721Semaste // takes a single unsigned LEB128 argument representing a register 671254721Semaste // number. The required action is to define the current CFA rule to 672254721Semaste // use the provided register (but to keep the old offset). 673254721Semaste reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 674254721Semaste row->SetCFARegister (reg_num); 675254721Semaste } 676254721Semaste break; 677254721Semaste 678254721Semaste case DW_CFA_def_cfa_offset : // 0xE (CFA Definition Instruction) 679254721Semaste { 680254721Semaste // Takes a single unsigned LEB128 operand representing a 681254721Semaste // (non-factored) offset. The required action is to define 682254721Semaste // the current CFA rule to use the provided offset (but 683254721Semaste // to keep the old register). 684254721Semaste op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 685254721Semaste row->SetCFAOffset (op_offset); 686254721Semaste } 687254721Semaste break; 688254721Semaste 689254721Semaste case DW_CFA_def_cfa_expression : // 0xF (CFA Definition Instruction) 690254721Semaste { 691254721Semaste size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset); 692254721Semaste offset += (uint32_t)block_len; 693254721Semaste } 694254721Semaste break; 695254721Semaste 696254721Semaste case DW_CFA_expression : // 0x10 697254721Semaste { 698254721Semaste // Takes two operands: an unsigned LEB128 value representing 699254721Semaste // a register number, and a DW_FORM_block value representing a DWARF 700254721Semaste // expression. The required action is to change the rule for the 701254721Semaste // register indicated by the register number to be an expression(E) 702254721Semaste // rule where E is the DWARF expression. That is, the DWARF 703254721Semaste // expression computes the address. The value of the CFA is 704254721Semaste // pushed on the DWARF evaluation stack prior to execution of 705254721Semaste // the DWARF expression. 706254721Semaste reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 707254721Semaste uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 708254721Semaste const uint8_t *block_data = (uint8_t *)m_cfi_data.GetData(&offset, block_len); 709254721Semaste 710254721Semaste reg_location.SetAtDWARFExpression(block_data, block_len); 711254721Semaste row->SetRegisterInfo (reg_num, reg_location); 712254721Semaste } 713254721Semaste break; 714254721Semaste 715254721Semaste case DW_CFA_offset_extended_sf : // 0x11 716254721Semaste { 717254721Semaste // takes two operands: an unsigned LEB128 value representing a 718254721Semaste // register number and a signed LEB128 factored offset. This 719254721Semaste // instruction is identical to DW_CFA_offset_extended except 720254721Semaste //that the second operand is signed and factored. 721254721Semaste reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 722254721Semaste op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 723254721Semaste reg_location.SetAtCFAPlusOffset(op_offset); 724254721Semaste row->SetRegisterInfo (reg_num, reg_location); 725254721Semaste } 726254721Semaste break; 727254721Semaste 728254721Semaste case DW_CFA_def_cfa_sf : // 0x12 (CFA Definition Instruction) 729254721Semaste { 730254721Semaste // Takes two operands: an unsigned LEB128 value representing 731254721Semaste // a register number and a signed LEB128 factored offset. 732254721Semaste // This instruction is identical to DW_CFA_def_cfa except 733254721Semaste // that the second operand is signed and factored. 734254721Semaste reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 735254721Semaste op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 736254721Semaste row->SetCFARegister (reg_num); 737254721Semaste row->SetCFAOffset (op_offset); 738254721Semaste } 739254721Semaste break; 740254721Semaste 741254721Semaste case DW_CFA_def_cfa_offset_sf : // 0x13 (CFA Definition Instruction) 742254721Semaste { 743254721Semaste // takes a signed LEB128 operand representing a factored 744254721Semaste // offset. This instruction is identical to DW_CFA_def_cfa_offset 745254721Semaste // except that the operand is signed and factored. 746254721Semaste op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 747254721Semaste row->SetCFAOffset (op_offset); 748254721Semaste } 749254721Semaste break; 750254721Semaste 751254721Semaste case DW_CFA_val_expression : // 0x16 752254721Semaste { 753254721Semaste // takes two operands: an unsigned LEB128 value representing a register 754254721Semaste // number, and a DW_FORM_block value representing a DWARF expression. 755254721Semaste // The required action is to change the rule for the register indicated 756254721Semaste // by the register number to be a val_expression(E) rule where E is the 757254721Semaste // DWARF expression. That is, the DWARF expression computes the value of 758254721Semaste // the given register. The value of the CFA is pushed on the DWARF 759254721Semaste // evaluation stack prior to execution of the DWARF expression. 760254721Semaste reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 761254721Semaste uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 762254721Semaste const uint8_t* block_data = (uint8_t*)m_cfi_data.GetData(&offset, block_len); 763254721Semaste//#if defined(__i386__) || defined(__x86_64__) 764254721Semaste// // The EH frame info for EIP and RIP contains code that looks for traps to 765254721Semaste// // be a specific type and increments the PC. 766254721Semaste// // For i386: 767254721Semaste// // DW_CFA_val_expression where: 768254721Semaste// // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34), 769254721Semaste// // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref, 770254721Semaste// // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, 771254721Semaste// // DW_OP_and, DW_OP_plus 772254721Semaste// // This basically does a: 773254721Semaste// // eip = ucontenxt.mcontext32->gpr.eip; 774254721Semaste// // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4) 775254721Semaste// // eip++; 776254721Semaste// // 777254721Semaste// // For x86_64: 778254721Semaste// // DW_CFA_val_expression where: 779254721Semaste// // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref, 780254721Semaste// // DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3, 781254721Semaste// // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus 782254721Semaste// // This basically does a: 783254721Semaste// // rip = ucontenxt.mcontext64->gpr.rip; 784254721Semaste// // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4) 785254721Semaste// // rip++; 786254721Semaste// // The trap comparisons and increments are not needed as it hoses up the unwound PC which 787254721Semaste// // is expected to point at least past the instruction that causes the fault/trap. So we 788254721Semaste// // take it out by trimming the expression right at the first "DW_OP_swap" opcodes 789254721Semaste// if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num) 790254721Semaste// { 791254721Semaste// if (thread->Is64Bit()) 792254721Semaste// { 793254721Semaste// if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst) 794254721Semaste// block_len = 8; 795254721Semaste// } 796254721Semaste// else 797254721Semaste// { 798254721Semaste// if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst) 799254721Semaste// block_len = 7; 800254721Semaste// } 801254721Semaste// } 802254721Semaste//#endif 803254721Semaste reg_location.SetIsDWARFExpression(block_data, block_len); 804254721Semaste row->SetRegisterInfo (reg_num, reg_location); 805254721Semaste } 806254721Semaste break; 807254721Semaste 808254721Semaste case DW_CFA_val_offset : // 0x14 809254721Semaste case DW_CFA_val_offset_sf : // 0x15 810254721Semaste default: 811254721Semaste break; 812254721Semaste } 813254721Semaste } 814254721Semaste } 815254721Semaste unwind_plan.AppendRow(row); 816254721Semaste 817254721Semaste return true; 818254721Semaste} 819