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