1 //===-- DWARFCallFrameInfo.cpp ----------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 11 // C Includes 12 // C++ Includes 13 #include <list> 14 15 #include "lldb/Core/Log.h" 16 #include "lldb/Core/Section.h" 17 #include "lldb/Core/ArchSpec.h" 18 #include "lldb/Core/Module.h" 19 #include "lldb/Core/Section.h" 20 #include "lldb/Core/Timer.h" 21 #include "lldb/Host/Host.h" 22 #include "lldb/Symbol/DWARFCallFrameInfo.h" 23 #include "lldb/Symbol/ObjectFile.h" 24 #include "lldb/Symbol/UnwindPlan.h" 25 #include "lldb/Target/RegisterContext.h" 26 #include "lldb/Target/Thread.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile& objfile, SectionSP& section_sp, lldb::RegisterKind reg_kind, bool is_eh_frame) : 32 m_objfile (objfile), 33 m_section_sp (section_sp), 34 m_reg_kind (reg_kind), // The flavor of registers that the CFI data uses (enum RegisterKind) 35 m_flags (), 36 m_cie_map (), 37 m_cfi_data (), 38 m_cfi_data_initialized (false), 39 m_fde_index (), 40 m_fde_index_initialized (false), 41 m_is_eh_frame (is_eh_frame) 42 { 43 } 44 45 DWARFCallFrameInfo::~DWARFCallFrameInfo() 46 { 47 } 48 49 50 bool 51 DWARFCallFrameInfo::GetUnwindPlan (Address addr, UnwindPlan& unwind_plan) 52 { 53 FDEEntryMap::Entry fde_entry; 54 55 // Make sure that the Address we're searching for is the same object file 56 // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index. 57 ModuleSP module_sp = addr.GetModule(); 58 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || module_sp->GetObjectFile() != &m_objfile) 59 return false; 60 61 if (GetFDEEntryByFileAddress (addr.GetFileAddress(), fde_entry) == false) 62 return false; 63 return FDEToUnwindPlan (fde_entry.data, addr, unwind_plan); 64 } 65 66 bool 67 DWARFCallFrameInfo::GetAddressRange (Address addr, AddressRange &range) 68 { 69 70 // Make sure that the Address we're searching for is the same object file 71 // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index. 72 ModuleSP module_sp = addr.GetModule(); 73 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || module_sp->GetObjectFile() != &m_objfile) 74 return false; 75 76 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 77 return false; 78 GetFDEIndex(); 79 FDEEntryMap::Entry *fde_entry = m_fde_index.FindEntryThatContains (addr.GetFileAddress()); 80 if (!fde_entry) 81 return false; 82 83 range = AddressRange(fde_entry->base, fde_entry->size, m_objfile.GetSectionList()); 84 return true; 85 } 86 87 bool 88 DWARFCallFrameInfo::GetFDEEntryByFileAddress (addr_t file_addr, FDEEntryMap::Entry &fde_entry) 89 { 90 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 91 return false; 92 93 GetFDEIndex(); 94 95 if (m_fde_index.IsEmpty()) 96 return false; 97 98 FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains (file_addr); 99 100 if (fde == nullptr) 101 return false; 102 103 fde_entry = *fde; 104 return true; 105 } 106 107 void 108 DWARFCallFrameInfo::GetFunctionAddressAndSizeVector (FunctionAddressAndSizeVector &function_info) 109 { 110 GetFDEIndex(); 111 const size_t count = m_fde_index.GetSize(); 112 function_info.Clear(); 113 if (count > 0) 114 function_info.Reserve(count); 115 for (size_t i = 0; i < count; ++i) 116 { 117 const FDEEntryMap::Entry *func_offset_data_entry = m_fde_index.GetEntryAtIndex (i); 118 if (func_offset_data_entry) 119 { 120 FunctionAddressAndSizeVector::Entry function_offset_entry (func_offset_data_entry->base, func_offset_data_entry->size); 121 function_info.Append (function_offset_entry); 122 } 123 } 124 } 125 126 const DWARFCallFrameInfo::CIE* 127 DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) 128 { 129 cie_map_t::iterator pos = m_cie_map.find(cie_offset); 130 131 if (pos != m_cie_map.end()) 132 { 133 // Parse and cache the CIE 134 if (pos->second.get() == nullptr) 135 pos->second = ParseCIE (cie_offset); 136 137 return pos->second.get(); 138 } 139 return nullptr; 140 } 141 142 DWARFCallFrameInfo::CIESP 143 DWARFCallFrameInfo::ParseCIE (const dw_offset_t cie_offset) 144 { 145 CIESP cie_sp(new CIE(cie_offset)); 146 lldb::offset_t offset = cie_offset; 147 if (m_cfi_data_initialized == false) 148 GetCFIData(); 149 uint32_t length = m_cfi_data.GetU32(&offset); 150 dw_offset_t cie_id, end_offset; 151 bool is_64bit = (length == UINT32_MAX); 152 if (is_64bit) { 153 length = m_cfi_data.GetU64(&offset); 154 cie_id = m_cfi_data.GetU64(&offset); 155 end_offset = cie_offset + length + 12; 156 } else { 157 cie_id = m_cfi_data.GetU32(&offset); 158 end_offset = cie_offset + length + 4; 159 } 160 if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) || (m_is_eh_frame && cie_id == 0ul))) 161 { 162 size_t i; 163 // cie.offset = cie_offset; 164 // cie.length = length; 165 // cie.cieID = cieID; 166 cie_sp->ptr_encoding = DW_EH_PE_absptr; // default 167 cie_sp->version = m_cfi_data.GetU8(&offset); 168 169 for (i=0; i<CFI_AUG_MAX_SIZE; ++i) 170 { 171 cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset); 172 if (cie_sp->augmentation[i] == '\0') 173 { 174 // Zero out remaining bytes in augmentation string 175 for (size_t j = i+1; j<CFI_AUG_MAX_SIZE; ++j) 176 cie_sp->augmentation[j] = '\0'; 177 178 break; 179 } 180 } 181 182 if (i == CFI_AUG_MAX_SIZE && cie_sp->augmentation[CFI_AUG_MAX_SIZE-1] != '\0') 183 { 184 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); 185 return cie_sp; 186 } 187 cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset); 188 cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset); 189 cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset); 190 191 if (cie_sp->augmentation[0]) 192 { 193 // Get the length of the eh_frame augmentation data 194 // which starts with a ULEB128 length in bytes 195 const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset); 196 const size_t aug_data_end = offset + aug_data_len; 197 const size_t aug_str_len = strlen(cie_sp->augmentation); 198 // A 'z' may be present as the first character of the string. 199 // If present, the Augmentation Data field shall be present. 200 // The contents of the Augmentation Data shall be intepreted 201 // according to other characters in the Augmentation String. 202 if (cie_sp->augmentation[0] == 'z') 203 { 204 // Extract the Augmentation Data 205 size_t aug_str_idx = 0; 206 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) 207 { 208 char aug = cie_sp->augmentation[aug_str_idx]; 209 switch (aug) 210 { 211 case 'L': 212 // Indicates the presence of one argument in the 213 // Augmentation Data of the CIE, and a corresponding 214 // argument in the Augmentation Data of the FDE. The 215 // argument in the Augmentation Data of the CIE is 216 // 1-byte and represents the pointer encoding used 217 // for the argument in the Augmentation Data of the 218 // FDE, which is the address of a language-specific 219 // data area (LSDA). The size of the LSDA pointer is 220 // specified by the pointer encoding used. 221 cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset); 222 break; 223 224 case 'P': 225 // Indicates the presence of two arguments in the 226 // Augmentation Data of the CIE. The first argument 227 // is 1-byte and represents the pointer encoding 228 // used for the second argument, which is the 229 // address of a personality routine handler. The 230 // size of the personality routine pointer is 231 // specified by the pointer encoding used. 232 // 233 // The address of the personality function will 234 // be stored at this location. Pre-execution, it 235 // will be all zero's so don't read it until we're 236 // trying to do an unwind & the reloc has been 237 // resolved. 238 { 239 uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset); 240 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 241 cie_sp->personality_loc = m_cfi_data.GetGNUEHPointer(&offset, arg_ptr_encoding, pc_rel_addr, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS); 242 } 243 break; 244 245 case 'R': 246 // A 'R' may be present at any position after the 247 // first character of the string. The Augmentation 248 // Data shall include a 1 byte argument that 249 // represents the pointer encoding for the address 250 // pointers used in the FDE. 251 // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4 252 cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset); 253 break; 254 } 255 } 256 } 257 else if (strcmp(cie_sp->augmentation, "eh") == 0) 258 { 259 // If the Augmentation string has the value "eh", then 260 // the EH Data field shall be present 261 } 262 263 // Set the offset to be the end of the augmentation data just in case 264 // we didn't understand any of the data. 265 offset = (uint32_t)aug_data_end; 266 } 267 268 if (end_offset > offset) 269 { 270 cie_sp->inst_offset = offset; 271 cie_sp->inst_length = end_offset - offset; 272 } 273 while (offset < end_offset) 274 { 275 uint8_t inst = m_cfi_data.GetU8(&offset); 276 uint8_t primary_opcode = inst & 0xC0; 277 uint8_t extended_opcode = inst & 0x3F; 278 279 if (extended_opcode == DW_CFA_def_cfa) 280 { 281 // Takes two unsigned LEB128 operands representing a register 282 // number and a (non-factored) offset. The required action 283 // is to define the current CFA rule to use the provided 284 // register and offset. 285 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 286 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 287 cie_sp->initial_row.GetCFAValue().SetIsRegisterPlusOffset (reg_num, op_offset); 288 continue; 289 } 290 if (extended_opcode == DW_CFA_def_cfa_sf) 291 { 292 // The DW_CFA_def_cfa_sf instruction takes two operands: an unsigned LEB128 value 293 // representing a register number and a signed LEB128 factored offset. This 294 // instruction is identical to DW_CFA_def_cfa except that the second operand is 295 // signed and factored. 296 // The resulting offset is factored_offset * data_alignment_factor. 297 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 298 int op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset); 299 cie_sp->initial_row.GetCFAValue().SetIsRegisterPlusOffset ( 300 reg_num, op_offset * cie_sp->data_align); 301 continue; 302 } 303 if (primary_opcode == DW_CFA_offset) 304 { 305 // 0x80 - high 2 bits are 0x2, lower 6 bits are register. 306 // Takes two arguments: an unsigned LEB128 constant representing a 307 // factored offset and a register number. The required action is to 308 // change the rule for the register indicated by the register number 309 // to be an offset(N) rule with a value of 310 // (N = factored offset * data_align). 311 uint32_t reg_num = extended_opcode; 312 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * cie_sp->data_align; 313 UnwindPlan::Row::RegisterLocation reg_location; 314 reg_location.SetAtCFAPlusOffset(op_offset); 315 cie_sp->initial_row.SetRegisterInfo (reg_num, reg_location); 316 continue; 317 } 318 if (extended_opcode == DW_CFA_nop) 319 { 320 continue; 321 } 322 break; // Stop if we hit an unrecognized opcode 323 } 324 } 325 326 return cie_sp; 327 } 328 329 void 330 DWARFCallFrameInfo::GetCFIData() 331 { 332 if (m_cfi_data_initialized == false) 333 { 334 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 335 if (log) 336 m_objfile.GetModule()->LogMessage(log, "Reading EH frame info"); 337 m_objfile.ReadSectionData (m_section_sp.get(), m_cfi_data); 338 m_cfi_data_initialized = true; 339 } 340 } 341 // Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses 342 // of the functions and a pointer back to the function's FDE for later expansion. 343 // Internalize CIEs as we come across them. 344 345 void 346 DWARFCallFrameInfo::GetFDEIndex () 347 { 348 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 349 return; 350 351 if (m_fde_index_initialized) 352 return; 353 354 Mutex::Locker locker(m_fde_index_mutex); 355 356 if (m_fde_index_initialized) // if two threads hit the locker 357 return; 358 359 Timer scoped_timer (__PRETTY_FUNCTION__, "%s - %s", __PRETTY_FUNCTION__, m_objfile.GetFileSpec().GetFilename().AsCString("")); 360 361 lldb::offset_t offset = 0; 362 if (m_cfi_data_initialized == false) 363 GetCFIData(); 364 while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8)) 365 { 366 const dw_offset_t current_entry = offset; 367 dw_offset_t cie_id, next_entry, cie_offset; 368 uint32_t len = m_cfi_data.GetU32 (&offset); 369 bool is_64bit = (len == UINT32_MAX); 370 if (is_64bit) { 371 len = m_cfi_data.GetU64 (&offset); 372 cie_id = m_cfi_data.GetU64 (&offset); 373 next_entry = current_entry + len + 12; 374 cie_offset = current_entry + 12 - cie_id; 375 } else { 376 cie_id = m_cfi_data.GetU32 (&offset); 377 next_entry = current_entry + len + 4; 378 cie_offset = current_entry + 4 - cie_id; 379 } 380 381 if (next_entry > m_cfi_data.GetByteSize() + 1) 382 { 383 Host::SystemLog (Host::eSystemLogError, 384 "error: Invalid fde/cie next entry offset of 0x%x found in cie/fde at 0x%x\n", 385 next_entry, 386 current_entry); 387 // Don't trust anything in this eh_frame section if we find blatently 388 // invalid data. 389 m_fde_index.Clear(); 390 m_fde_index_initialized = true; 391 return; 392 } 393 if (cie_offset > m_cfi_data.GetByteSize()) 394 { 395 Host::SystemLog (Host::eSystemLogError, 396 "error: Invalid cie offset of 0x%x found in cie/fde at 0x%x\n", 397 cie_offset, 398 current_entry); 399 // Don't trust anything in this eh_frame section if we find blatently 400 // invalid data. 401 m_fde_index.Clear(); 402 m_fde_index_initialized = true; 403 return; 404 } 405 406 if (cie_id == 0 || cie_id == UINT32_MAX || len == 0) 407 { 408 m_cie_map[current_entry] = ParseCIE (current_entry); 409 offset = next_entry; 410 continue; 411 } 412 413 const CIE *cie = GetCIE (cie_offset); 414 if (cie) 415 { 416 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 417 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; 418 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; 419 420 lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr); 421 lldb::addr_t length = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr); 422 FDEEntryMap::Entry fde (addr, length, current_entry); 423 m_fde_index.Append(fde); 424 } 425 else 426 { 427 Host::SystemLog (Host::eSystemLogError, 428 "error: unable to find CIE at 0x%8.8x for cie_id = 0x%8.8x for entry at 0x%8.8x.\n", 429 cie_offset, 430 cie_id, 431 current_entry); 432 } 433 offset = next_entry; 434 } 435 m_fde_index.Sort(); 436 m_fde_index_initialized = true; 437 } 438 439 bool 440 DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t dwarf_offset, Address startaddr, UnwindPlan& unwind_plan) 441 { 442 lldb::offset_t offset = dwarf_offset; 443 lldb::offset_t current_entry = offset; 444 445 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) 446 return false; 447 448 if (m_cfi_data_initialized == false) 449 GetCFIData(); 450 451 uint32_t length = m_cfi_data.GetU32 (&offset); 452 dw_offset_t cie_offset; 453 bool is_64bit = (length == UINT32_MAX); 454 if (is_64bit) { 455 length = m_cfi_data.GetU64 (&offset); 456 cie_offset = m_cfi_data.GetU64 (&offset); 457 } else { 458 cie_offset = m_cfi_data.GetU32 (&offset); 459 } 460 461 assert (cie_offset != 0 && cie_offset != UINT32_MAX); 462 463 // Translate the CIE_id from the eh_frame format, which 464 // is relative to the FDE offset, into a __eh_frame section 465 // offset 466 if (m_is_eh_frame) 467 { 468 unwind_plan.SetSourceName ("eh_frame CFI"); 469 cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset; 470 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); 471 } 472 else 473 { 474 unwind_plan.SetSourceName ("DWARF CFI"); 475 // In theory the debug_frame info should be valid at all call sites 476 // ("asynchronous unwind info" as it is sometimes called) but in practice 477 // gcc et al all emit call frame info for the prologue and call sites, but 478 // not for the epilogue or all the other locations during the function reliably. 479 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); 480 } 481 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes); 482 483 const CIE *cie = GetCIE (cie_offset); 484 assert (cie != nullptr); 485 486 const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4); 487 488 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); 489 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; 490 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; 491 lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr); 492 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); 493 AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList()); 494 range.SetByteSize (range_len); 495 496 addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS; 497 498 if (cie->augmentation[0] == 'z') 499 { 500 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 501 if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit) 502 { 503 offset_t saved_offset = offset; 504 lsda_data_file_address = m_cfi_data.GetGNUEHPointer(&offset, cie->lsda_addr_encoding, pc_rel_addr, text_addr, data_addr); 505 if (offset - saved_offset != aug_data_len) 506 { 507 // There is more in the augmentation region than we know how to process; 508 // don't read anything. 509 lsda_data_file_address = LLDB_INVALID_ADDRESS; 510 } 511 offset = saved_offset; 512 } 513 offset += aug_data_len; 514 } 515 Address lsda_data; 516 Address personality_function_ptr; 517 518 if (lsda_data_file_address != LLDB_INVALID_ADDRESS && cie->personality_loc != LLDB_INVALID_ADDRESS) 519 { 520 m_objfile.GetModule()->ResolveFileAddress (lsda_data_file_address, lsda_data); 521 m_objfile.GetModule()->ResolveFileAddress (cie->personality_loc, personality_function_ptr); 522 } 523 524 if (lsda_data.IsValid() && personality_function_ptr.IsValid()) 525 { 526 unwind_plan.SetLSDAAddress (lsda_data); 527 unwind_plan.SetPersonalityFunctionPtr (personality_function_ptr); 528 } 529 530 uint32_t reg_num = 0; 531 int32_t op_offset = 0; 532 uint32_t code_align = cie->code_align; 533 int32_t data_align = cie->data_align; 534 535 unwind_plan.SetPlanValidAddressRange (range); 536 UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row; 537 *cie_initial_row = cie->initial_row; 538 UnwindPlan::RowSP row(cie_initial_row); 539 540 unwind_plan.SetRegisterKind (m_reg_kind); 541 unwind_plan.SetReturnAddressRegister (cie->return_addr_reg_num); 542 543 std::vector<UnwindPlan::RowSP> stack; 544 545 UnwindPlan::Row::RegisterLocation reg_location; 546 while (m_cfi_data.ValidOffset(offset) && offset < end_offset) 547 { 548 uint8_t inst = m_cfi_data.GetU8(&offset); 549 uint8_t primary_opcode = inst & 0xC0; 550 uint8_t extended_opcode = inst & 0x3F; 551 552 if (primary_opcode) 553 { 554 switch (primary_opcode) 555 { 556 case DW_CFA_advance_loc : // (Row Creation Instruction) 557 { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta 558 // takes a single argument that represents a constant delta. The 559 // required action is to create a new table row with a location 560 // value that is computed by taking the current entry's location 561 // value and adding (delta * code_align). All other 562 // values in the new row are initially identical to the current row. 563 unwind_plan.AppendRow(row); 564 UnwindPlan::Row *newrow = new UnwindPlan::Row; 565 *newrow = *row.get(); 566 row.reset (newrow); 567 row->SlideOffset(extended_opcode * code_align); 568 } 569 break; 570 571 case DW_CFA_offset : 572 { // 0x80 - high 2 bits are 0x2, lower 6 bits are register 573 // takes two arguments: an unsigned LEB128 constant representing a 574 // factored offset and a register number. The required action is to 575 // change the rule for the register indicated by the register number 576 // to be an offset(N) rule with a value of 577 // (N = factored offset * data_align). 578 reg_num = extended_opcode; 579 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 580 reg_location.SetAtCFAPlusOffset(op_offset); 581 row->SetRegisterInfo (reg_num, reg_location); 582 } 583 break; 584 585 case DW_CFA_restore : 586 { // 0xC0 - high 2 bits are 0x3, lower 6 bits are register 587 // takes a single argument that represents a register number. The 588 // required action is to change the rule for the indicated register 589 // to the rule assigned it by the initial_instructions in the CIE. 590 reg_num = extended_opcode; 591 // We only keep enough register locations around to 592 // unwind what is in our thread, and these are organized 593 // by the register index in that state, so we need to convert our 594 // GCC register number from the EH frame info, to a register index 595 596 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location)) 597 row->SetRegisterInfo (reg_num, reg_location); 598 } 599 break; 600 } 601 } 602 else 603 { 604 switch (extended_opcode) 605 { 606 case DW_CFA_nop : // 0x0 607 break; 608 609 case DW_CFA_set_loc : // 0x1 (Row Creation Instruction) 610 { 611 // DW_CFA_set_loc takes a single argument that represents an address. 612 // The required action is to create a new table row using the 613 // specified address as the location. All other values in the new row 614 // are initially identical to the current row. The new location value 615 // should always be greater than the current one. 616 unwind_plan.AppendRow(row); 617 UnwindPlan::Row *newrow = new UnwindPlan::Row; 618 *newrow = *row.get(); 619 row.reset (newrow); 620 row->SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress()); 621 } 622 break; 623 624 case DW_CFA_advance_loc1 : // 0x2 (Row Creation Instruction) 625 { 626 // takes a single uword argument that represents a constant delta. 627 // This instruction is identical to DW_CFA_advance_loc except for the 628 // encoding and size of the delta argument. 629 unwind_plan.AppendRow(row); 630 UnwindPlan::Row *newrow = new UnwindPlan::Row; 631 *newrow = *row.get(); 632 row.reset (newrow); 633 row->SlideOffset (m_cfi_data.GetU8(&offset) * code_align); 634 } 635 break; 636 637 case DW_CFA_advance_loc2 : // 0x3 (Row Creation Instruction) 638 { 639 // takes a single uword argument that represents a constant delta. 640 // This instruction is identical to DW_CFA_advance_loc except for the 641 // encoding and size of the delta argument. 642 unwind_plan.AppendRow(row); 643 UnwindPlan::Row *newrow = new UnwindPlan::Row; 644 *newrow = *row.get(); 645 row.reset (newrow); 646 row->SlideOffset (m_cfi_data.GetU16(&offset) * code_align); 647 } 648 break; 649 650 case DW_CFA_advance_loc4 : // 0x4 (Row Creation Instruction) 651 { 652 // takes a single uword argument that represents a constant delta. 653 // This instruction is identical to DW_CFA_advance_loc except for the 654 // encoding and size of the delta argument. 655 unwind_plan.AppendRow(row); 656 UnwindPlan::Row *newrow = new UnwindPlan::Row; 657 *newrow = *row.get(); 658 row.reset (newrow); 659 row->SlideOffset (m_cfi_data.GetU32(&offset) * code_align); 660 } 661 break; 662 663 case DW_CFA_offset_extended : // 0x5 664 { 665 // takes two unsigned LEB128 arguments representing a register number 666 // and a factored offset. This instruction is identical to DW_CFA_offset 667 // except for the encoding and size of the register argument. 668 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 669 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; 670 reg_location.SetAtCFAPlusOffset(op_offset); 671 row->SetRegisterInfo (reg_num, reg_location); 672 } 673 break; 674 675 case DW_CFA_restore_extended : // 0x6 676 { 677 // takes a single unsigned LEB128 argument that represents a register 678 // number. This instruction is identical to DW_CFA_restore except for 679 // the encoding and size of the register argument. 680 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 681 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location)) 682 row->SetRegisterInfo (reg_num, reg_location); 683 } 684 break; 685 686 case DW_CFA_undefined : // 0x7 687 { 688 // takes a single unsigned LEB128 argument that represents a register 689 // number. The required action is to set the rule for the specified 690 // register to undefined. 691 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 692 reg_location.SetUndefined(); 693 row->SetRegisterInfo (reg_num, reg_location); 694 } 695 break; 696 697 case DW_CFA_same_value : // 0x8 698 { 699 // takes a single unsigned LEB128 argument that represents a register 700 // number. The required action is to set the rule for the specified 701 // register to same value. 702 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 703 reg_location.SetSame(); 704 row->SetRegisterInfo (reg_num, reg_location); 705 } 706 break; 707 708 case DW_CFA_register : // 0x9 709 { 710 // takes two unsigned LEB128 arguments representing register numbers. 711 // The required action is to set the rule for the first register to be 712 // the second register. 713 714 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 715 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 716 reg_location.SetInRegister(other_reg_num); 717 row->SetRegisterInfo (reg_num, reg_location); 718 } 719 break; 720 721 case DW_CFA_remember_state : // 0xA 722 { 723 // These instructions define a stack of information. Encountering the 724 // DW_CFA_remember_state instruction means to save the rules for every 725 // register on the current row on the stack. Encountering the 726 // DW_CFA_restore_state instruction means to pop the set of rules off 727 // the stack and place them in the current row. (This operation is 728 // useful for compilers that move epilogue code into the body of a 729 // function.) 730 stack.push_back (row); 731 UnwindPlan::Row *newrow = new UnwindPlan::Row; 732 *newrow = *row.get(); 733 row.reset (newrow); 734 } 735 break; 736 737 case DW_CFA_restore_state : // 0xB 738 // These instructions define a stack of information. Encountering the 739 // DW_CFA_remember_state instruction means to save the rules for every 740 // register on the current row on the stack. Encountering the 741 // DW_CFA_restore_state instruction means to pop the set of rules off 742 // the stack and place them in the current row. (This operation is 743 // useful for compilers that move epilogue code into the body of a 744 // function.) 745 { 746 row = stack.back (); 747 stack.pop_back (); 748 } 749 break; 750 751 case DW_CFA_def_cfa : // 0xC (CFA Definition Instruction) 752 { 753 // Takes two unsigned LEB128 operands representing a register 754 // number and a (non-factored) offset. The required action 755 // is to define the current CFA rule to use the provided 756 // register and offset. 757 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 758 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 759 row->GetCFAValue().SetIsRegisterPlusOffset (reg_num, op_offset); 760 } 761 break; 762 763 case DW_CFA_def_cfa_register : // 0xD (CFA Definition Instruction) 764 { 765 // takes a single unsigned LEB128 argument representing a register 766 // number. The required action is to define the current CFA rule to 767 // use the provided register (but to keep the old offset). 768 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 769 row->GetCFAValue().SetIsRegisterPlusOffset (reg_num, 770 row->GetCFAValue().GetOffset()); 771 } 772 break; 773 774 case DW_CFA_def_cfa_offset : // 0xE (CFA Definition Instruction) 775 { 776 // Takes a single unsigned LEB128 operand representing a 777 // (non-factored) offset. The required action is to define 778 // the current CFA rule to use the provided offset (but 779 // to keep the old register). 780 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); 781 row->GetCFAValue().SetIsRegisterPlusOffset ( 782 row->GetCFAValue().GetRegisterNumber(), op_offset); 783 } 784 break; 785 786 case DW_CFA_def_cfa_expression : // 0xF (CFA Definition Instruction) 787 { 788 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset); 789 const uint8_t *block_data = 790 static_cast<const uint8_t *>(m_cfi_data.GetData(&offset, block_len)); 791 row->GetCFAValue().SetIsDWARFExpression(block_data, block_len); 792 } 793 break; 794 795 case DW_CFA_expression : // 0x10 796 { 797 // Takes two operands: an unsigned LEB128 value representing 798 // a register number, and a DW_FORM_block value representing a DWARF 799 // expression. The required action is to change the rule for the 800 // register indicated by the register number to be an expression(E) 801 // rule where E is the DWARF expression. That is, the DWARF 802 // expression computes the address. The value of the CFA is 803 // pushed on the DWARF evaluation stack prior to execution of 804 // the DWARF expression. 805 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 806 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 807 const uint8_t *block_data = (uint8_t *)m_cfi_data.GetData(&offset, block_len); 808 809 reg_location.SetAtDWARFExpression(block_data, block_len); 810 row->SetRegisterInfo (reg_num, reg_location); 811 } 812 break; 813 814 case DW_CFA_offset_extended_sf : // 0x11 815 { 816 // takes two operands: an unsigned LEB128 value representing a 817 // register number and a signed LEB128 factored offset. This 818 // instruction is identical to DW_CFA_offset_extended except 819 //that the second operand is signed and factored. 820 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 821 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 822 reg_location.SetAtCFAPlusOffset(op_offset); 823 row->SetRegisterInfo (reg_num, reg_location); 824 } 825 break; 826 827 case DW_CFA_def_cfa_sf : // 0x12 (CFA Definition Instruction) 828 { 829 // Takes two operands: an unsigned LEB128 value representing 830 // a register number and a signed LEB128 factored offset. 831 // This instruction is identical to DW_CFA_def_cfa except 832 // that the second operand is signed and factored. 833 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 834 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 835 row->GetCFAValue().SetIsRegisterPlusOffset (reg_num, op_offset); 836 } 837 break; 838 839 case DW_CFA_def_cfa_offset_sf : // 0x13 (CFA Definition Instruction) 840 { 841 // takes a signed LEB128 operand representing a factored 842 // offset. This instruction is identical to DW_CFA_def_cfa_offset 843 // except that the operand is signed and factored. 844 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; 845 row->GetCFAValue().SetIsRegisterPlusOffset ( 846 row->GetCFAValue().GetRegisterNumber(), op_offset); 847 } 848 break; 849 850 case DW_CFA_val_expression : // 0x16 851 { 852 // takes two operands: an unsigned LEB128 value representing a register 853 // number, and a DW_FORM_block value representing a DWARF expression. 854 // The required action is to change the rule for the register indicated 855 // by the register number to be a val_expression(E) rule where E is the 856 // DWARF expression. That is, the DWARF expression computes the value of 857 // the given register. The value of the CFA is pushed on the DWARF 858 // evaluation stack prior to execution of the DWARF expression. 859 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); 860 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); 861 const uint8_t* block_data = (uint8_t*)m_cfi_data.GetData(&offset, block_len); 862 //#if defined(__i386__) || defined(__x86_64__) 863 // // The EH frame info for EIP and RIP contains code that looks for traps to 864 // // be a specific type and increments the PC. 865 // // For i386: 866 // // DW_CFA_val_expression where: 867 // // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34), 868 // // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref, 869 // // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, 870 // // DW_OP_and, DW_OP_plus 871 // // This basically does a: 872 // // eip = ucontenxt.mcontext32->gpr.eip; 873 // // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4) 874 // // eip++; 875 // // 876 // // For x86_64: 877 // // DW_CFA_val_expression where: 878 // // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref, 879 // // DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3, 880 // // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus 881 // // This basically does a: 882 // // rip = ucontenxt.mcontext64->gpr.rip; 883 // // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4) 884 // // rip++; 885 // // The trap comparisons and increments are not needed as it hoses up the unwound PC which 886 // // is expected to point at least past the instruction that causes the fault/trap. So we 887 // // take it out by trimming the expression right at the first "DW_OP_swap" opcodes 888 // if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num) 889 // { 890 // if (thread->Is64Bit()) 891 // { 892 // if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst) 893 // block_len = 8; 894 // } 895 // else 896 // { 897 // if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst) 898 // block_len = 7; 899 // } 900 // } 901 //#endif 902 reg_location.SetIsDWARFExpression(block_data, block_len); 903 row->SetRegisterInfo (reg_num, reg_location); 904 } 905 break; 906 907 case DW_CFA_val_offset : // 0x14 908 case DW_CFA_val_offset_sf : // 0x15 909 default: 910 break; 911 } 912 } 913 } 914 unwind_plan.AppendRow(row); 915 916 return true; 917 } 918