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