1 //===-- ObjectFileMachO.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 #include "llvm/ADT/StringRef.h" 11 #include "llvm/Support/MachO.h" 12 13 #include "ObjectFileMachO.h" 14 15 #include "lldb/lldb-private-log.h" 16 #include "lldb/Core/ArchSpec.h" 17 #include "lldb/Core/DataBuffer.h" 18 #include "lldb/Core/Debugger.h" 19 #include "lldb/Core/FileSpecList.h" 20 #include "lldb/Core/Log.h" 21 #include "lldb/Core/Module.h" 22 #include "lldb/Core/ModuleSpec.h" 23 #include "lldb/Core/PluginManager.h" 24 #include "lldb/Core/RangeMap.h" 25 #include "lldb/Core/Section.h" 26 #include "lldb/Core/StreamFile.h" 27 #include "lldb/Core/StreamString.h" 28 #include "lldb/Core/Timer.h" 29 #include "lldb/Core/UUID.h" 30 #include "lldb/Host/Host.h" 31 #include "lldb/Host/FileSpec.h" 32 #include "lldb/Symbol/ClangNamespaceDecl.h" 33 #include "lldb/Symbol/DWARFCallFrameInfo.h" 34 #include "lldb/Symbol/ObjectFile.h" 35 #include "lldb/Target/Platform.h" 36 #include "lldb/Target/Process.h" 37 #include "lldb/Target/Target.h" 38 #include "Plugins/Process/Utility/RegisterContextDarwin_arm.h" 39 #include "Plugins/Process/Utility/RegisterContextDarwin_i386.h" 40 #include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h" 41 42 #if defined (__APPLE__) && defined (__arm__) 43 // GetLLDBSharedCacheUUID() needs to call dlsym() 44 #include <dlfcn.h> 45 #endif 46 47 #ifndef __APPLE__ 48 #include "Utility/UuidCompatibility.h" 49 #endif 50 51 using namespace lldb; 52 using namespace lldb_private; 53 using namespace llvm::MachO; 54 55 class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64 56 { 57 public: 58 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) : 59 RegisterContextDarwin_x86_64 (thread, 0) 60 { 61 SetRegisterDataFrom_LC_THREAD (data); 62 } 63 64 virtual void 65 InvalidateAllRegisters () 66 { 67 // Do nothing... registers are always valid... 68 } 69 70 void 71 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data) 72 { 73 lldb::offset_t offset = 0; 74 SetError (GPRRegSet, Read, -1); 75 SetError (FPURegSet, Read, -1); 76 SetError (EXCRegSet, Read, -1); 77 bool done = false; 78 79 while (!done) 80 { 81 int flavor = data.GetU32 (&offset); 82 if (flavor == 0) 83 done = true; 84 else 85 { 86 uint32_t i; 87 uint32_t count = data.GetU32 (&offset); 88 switch (flavor) 89 { 90 case GPRRegSet: 91 for (i=0; i<count; ++i) 92 (&gpr.rax)[i] = data.GetU64(&offset); 93 SetError (GPRRegSet, Read, 0); 94 done = true; 95 96 break; 97 case FPURegSet: 98 // TODO: fill in FPU regs.... 99 //SetError (FPURegSet, Read, -1); 100 done = true; 101 102 break; 103 case EXCRegSet: 104 exc.trapno = data.GetU32(&offset); 105 exc.err = data.GetU32(&offset); 106 exc.faultvaddr = data.GetU64(&offset); 107 SetError (EXCRegSet, Read, 0); 108 done = true; 109 break; 110 case 7: 111 case 8: 112 case 9: 113 // fancy flavors that encapsulate of the the above 114 // falvors... 115 break; 116 117 default: 118 done = true; 119 break; 120 } 121 } 122 } 123 } 124 protected: 125 virtual int 126 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr) 127 { 128 return 0; 129 } 130 131 virtual int 132 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu) 133 { 134 return 0; 135 } 136 137 virtual int 138 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc) 139 { 140 return 0; 141 } 142 143 virtual int 144 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr) 145 { 146 return 0; 147 } 148 149 virtual int 150 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu) 151 { 152 return 0; 153 } 154 155 virtual int 156 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc) 157 { 158 return 0; 159 } 160 }; 161 162 163 class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386 164 { 165 public: 166 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) : 167 RegisterContextDarwin_i386 (thread, 0) 168 { 169 SetRegisterDataFrom_LC_THREAD (data); 170 } 171 172 virtual void 173 InvalidateAllRegisters () 174 { 175 // Do nothing... registers are always valid... 176 } 177 178 void 179 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data) 180 { 181 lldb::offset_t offset = 0; 182 SetError (GPRRegSet, Read, -1); 183 SetError (FPURegSet, Read, -1); 184 SetError (EXCRegSet, Read, -1); 185 bool done = false; 186 187 while (!done) 188 { 189 int flavor = data.GetU32 (&offset); 190 if (flavor == 0) 191 done = true; 192 else 193 { 194 uint32_t i; 195 uint32_t count = data.GetU32 (&offset); 196 switch (flavor) 197 { 198 case GPRRegSet: 199 for (i=0; i<count; ++i) 200 (&gpr.eax)[i] = data.GetU32(&offset); 201 SetError (GPRRegSet, Read, 0); 202 done = true; 203 204 break; 205 case FPURegSet: 206 // TODO: fill in FPU regs.... 207 //SetError (FPURegSet, Read, -1); 208 done = true; 209 210 break; 211 case EXCRegSet: 212 exc.trapno = data.GetU32(&offset); 213 exc.err = data.GetU32(&offset); 214 exc.faultvaddr = data.GetU32(&offset); 215 SetError (EXCRegSet, Read, 0); 216 done = true; 217 break; 218 case 7: 219 case 8: 220 case 9: 221 // fancy flavors that encapsulate of the the above 222 // falvors... 223 break; 224 225 default: 226 done = true; 227 break; 228 } 229 } 230 } 231 } 232 protected: 233 virtual int 234 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr) 235 { 236 return 0; 237 } 238 239 virtual int 240 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu) 241 { 242 return 0; 243 } 244 245 virtual int 246 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc) 247 { 248 return 0; 249 } 250 251 virtual int 252 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr) 253 { 254 return 0; 255 } 256 257 virtual int 258 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu) 259 { 260 return 0; 261 } 262 263 virtual int 264 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc) 265 { 266 return 0; 267 } 268 }; 269 270 class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm 271 { 272 public: 273 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) : 274 RegisterContextDarwin_arm (thread, 0) 275 { 276 SetRegisterDataFrom_LC_THREAD (data); 277 } 278 279 virtual void 280 InvalidateAllRegisters () 281 { 282 // Do nothing... registers are always valid... 283 } 284 285 void 286 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data) 287 { 288 lldb::offset_t offset = 0; 289 SetError (GPRRegSet, Read, -1); 290 SetError (FPURegSet, Read, -1); 291 SetError (EXCRegSet, Read, -1); 292 bool done = false; 293 294 while (!done) 295 { 296 int flavor = data.GetU32 (&offset); 297 uint32_t count = data.GetU32 (&offset); 298 lldb::offset_t next_thread_state = offset + (count * 4); 299 switch (flavor) 300 { 301 case GPRRegSet: 302 for (uint32_t i=0; i<count; ++i) 303 { 304 gpr.r[i] = data.GetU32(&offset); 305 } 306 307 // Note that gpr.cpsr is also copied by the above loop; this loop technically extends 308 // one element past the end of the gpr.r[] array. 309 310 SetError (GPRRegSet, Read, 0); 311 offset = next_thread_state; 312 break; 313 314 case FPURegSet: 315 { 316 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0]; 317 const int fpu_reg_buf_size = sizeof (fpu.floats); 318 if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size) 319 { 320 offset += fpu_reg_buf_size; 321 fpu.fpscr = data.GetU32(&offset); 322 SetError (FPURegSet, Read, 0); 323 } 324 else 325 { 326 done = true; 327 } 328 } 329 offset = next_thread_state; 330 break; 331 332 case EXCRegSet: 333 if (count == 3) 334 { 335 exc.exception = data.GetU32(&offset); 336 exc.fsr = data.GetU32(&offset); 337 exc.far = data.GetU32(&offset); 338 SetError (EXCRegSet, Read, 0); 339 } 340 done = true; 341 offset = next_thread_state; 342 break; 343 344 // Unknown register set flavor, stop trying to parse. 345 default: 346 done = true; 347 } 348 } 349 } 350 protected: 351 virtual int 352 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr) 353 { 354 return -1; 355 } 356 357 virtual int 358 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu) 359 { 360 return -1; 361 } 362 363 virtual int 364 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc) 365 { 366 return -1; 367 } 368 369 virtual int 370 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg) 371 { 372 return -1; 373 } 374 375 virtual int 376 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr) 377 { 378 return 0; 379 } 380 381 virtual int 382 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu) 383 { 384 return 0; 385 } 386 387 virtual int 388 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc) 389 { 390 return 0; 391 } 392 393 virtual int 394 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg) 395 { 396 return -1; 397 } 398 }; 399 400 static uint32_t 401 MachHeaderSizeFromMagic(uint32_t magic) 402 { 403 switch (magic) 404 { 405 case HeaderMagic32: 406 case HeaderMagic32Swapped: 407 return sizeof(struct mach_header); 408 409 case HeaderMagic64: 410 case HeaderMagic64Swapped: 411 return sizeof(struct mach_header_64); 412 break; 413 414 default: 415 break; 416 } 417 return 0; 418 } 419 420 #define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008 421 422 void 423 ObjectFileMachO::Initialize() 424 { 425 PluginManager::RegisterPlugin (GetPluginNameStatic(), 426 GetPluginDescriptionStatic(), 427 CreateInstance, 428 CreateMemoryInstance, 429 GetModuleSpecifications); 430 } 431 432 void 433 ObjectFileMachO::Terminate() 434 { 435 PluginManager::UnregisterPlugin (CreateInstance); 436 } 437 438 439 lldb_private::ConstString 440 ObjectFileMachO::GetPluginNameStatic() 441 { 442 static ConstString g_name("mach-o"); 443 return g_name; 444 } 445 446 const char * 447 ObjectFileMachO::GetPluginDescriptionStatic() 448 { 449 return "Mach-o object file reader (32 and 64 bit)"; 450 } 451 452 ObjectFile * 453 ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp, 454 DataBufferSP& data_sp, 455 lldb::offset_t data_offset, 456 const FileSpec* file, 457 lldb::offset_t file_offset, 458 lldb::offset_t length) 459 { 460 if (!data_sp) 461 { 462 data_sp = file->MemoryMapFileContents(file_offset, length); 463 data_offset = 0; 464 } 465 466 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length)) 467 { 468 // Update the data to contain the entire file if it doesn't already 469 if (data_sp->GetByteSize() < length) 470 { 471 data_sp = file->MemoryMapFileContents(file_offset, length); 472 data_offset = 0; 473 } 474 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, data_offset, file, file_offset, length)); 475 if (objfile_ap.get() && objfile_ap->ParseHeader()) 476 return objfile_ap.release(); 477 } 478 return NULL; 479 } 480 481 ObjectFile * 482 ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp, 483 DataBufferSP& data_sp, 484 const ProcessSP &process_sp, 485 lldb::addr_t header_addr) 486 { 487 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) 488 { 489 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr)); 490 if (objfile_ap.get() && objfile_ap->ParseHeader()) 491 return objfile_ap.release(); 492 } 493 return NULL; 494 } 495 496 size_t 497 ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file, 498 lldb::DataBufferSP& data_sp, 499 lldb::offset_t data_offset, 500 lldb::offset_t file_offset, 501 lldb::offset_t length, 502 lldb_private::ModuleSpecList &specs) 503 { 504 const size_t initial_count = specs.GetSize(); 505 506 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) 507 { 508 DataExtractor data; 509 data.SetData(data_sp); 510 llvm::MachO::mach_header header; 511 if (ParseHeader (data, &data_offset, header)) 512 { 513 if (header.sizeofcmds >= data_sp->GetByteSize()) 514 { 515 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds); 516 data.SetData(data_sp); 517 data_offset = MachHeaderSizeFromMagic(header.magic); 518 } 519 if (data_sp) 520 { 521 ModuleSpec spec; 522 spec.GetFileSpec() = file; 523 spec.GetArchitecture().SetArchitecture(eArchTypeMachO, 524 header.cputype, 525 header.cpusubtype); 526 if (spec.GetArchitecture().IsValid()) 527 { 528 GetUUID (header, data, data_offset, spec.GetUUID()); 529 specs.Append(spec); 530 } 531 } 532 } 533 } 534 return specs.GetSize() - initial_count; 535 } 536 537 538 539 const ConstString & 540 ObjectFileMachO::GetSegmentNameTEXT() 541 { 542 static ConstString g_segment_name_TEXT ("__TEXT"); 543 return g_segment_name_TEXT; 544 } 545 546 const ConstString & 547 ObjectFileMachO::GetSegmentNameDATA() 548 { 549 static ConstString g_segment_name_DATA ("__DATA"); 550 return g_segment_name_DATA; 551 } 552 553 const ConstString & 554 ObjectFileMachO::GetSegmentNameOBJC() 555 { 556 static ConstString g_segment_name_OBJC ("__OBJC"); 557 return g_segment_name_OBJC; 558 } 559 560 const ConstString & 561 ObjectFileMachO::GetSegmentNameLINKEDIT() 562 { 563 static ConstString g_section_name_LINKEDIT ("__LINKEDIT"); 564 return g_section_name_LINKEDIT; 565 } 566 567 const ConstString & 568 ObjectFileMachO::GetSectionNameEHFrame() 569 { 570 static ConstString g_section_name_eh_frame ("__eh_frame"); 571 return g_section_name_eh_frame; 572 } 573 574 bool 575 ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp, 576 lldb::addr_t data_offset, 577 lldb::addr_t data_length) 578 { 579 DataExtractor data; 580 data.SetData (data_sp, data_offset, data_length); 581 lldb::offset_t offset = 0; 582 uint32_t magic = data.GetU32(&offset); 583 return MachHeaderSizeFromMagic(magic) != 0; 584 } 585 586 587 ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp, 588 DataBufferSP& data_sp, 589 lldb::offset_t data_offset, 590 const FileSpec* file, 591 lldb::offset_t file_offset, 592 lldb::offset_t length) : 593 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 594 m_mach_segments(), 595 m_mach_sections(), 596 m_entry_point_address(), 597 m_thread_context_offsets(), 598 m_thread_context_offsets_valid(false) 599 { 600 ::memset (&m_header, 0, sizeof(m_header)); 601 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab)); 602 } 603 604 ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp, 605 lldb::DataBufferSP& header_data_sp, 606 const lldb::ProcessSP &process_sp, 607 lldb::addr_t header_addr) : 608 ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 609 m_mach_segments(), 610 m_mach_sections(), 611 m_entry_point_address(), 612 m_thread_context_offsets(), 613 m_thread_context_offsets_valid(false) 614 { 615 ::memset (&m_header, 0, sizeof(m_header)); 616 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab)); 617 } 618 619 ObjectFileMachO::~ObjectFileMachO() 620 { 621 } 622 623 bool 624 ObjectFileMachO::ParseHeader (DataExtractor &data, 625 lldb::offset_t *data_offset_ptr, 626 llvm::MachO::mach_header &header) 627 { 628 data.SetByteOrder (lldb::endian::InlHostByteOrder()); 629 // Leave magic in the original byte order 630 header.magic = data.GetU32(data_offset_ptr); 631 bool can_parse = false; 632 bool is_64_bit = false; 633 switch (header.magic) 634 { 635 case HeaderMagic32: 636 data.SetByteOrder (lldb::endian::InlHostByteOrder()); 637 data.SetAddressByteSize(4); 638 can_parse = true; 639 break; 640 641 case HeaderMagic64: 642 data.SetByteOrder (lldb::endian::InlHostByteOrder()); 643 data.SetAddressByteSize(8); 644 can_parse = true; 645 is_64_bit = true; 646 break; 647 648 case HeaderMagic32Swapped: 649 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); 650 data.SetAddressByteSize(4); 651 can_parse = true; 652 break; 653 654 case HeaderMagic64Swapped: 655 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); 656 data.SetAddressByteSize(8); 657 is_64_bit = true; 658 can_parse = true; 659 break; 660 661 default: 662 break; 663 } 664 665 if (can_parse) 666 { 667 data.GetU32(data_offset_ptr, &header.cputype, 6); 668 if (is_64_bit) 669 *data_offset_ptr += 4; 670 return true; 671 } 672 else 673 { 674 memset(&header, 0, sizeof(header)); 675 } 676 return false; 677 } 678 679 bool 680 ObjectFileMachO::ParseHeader () 681 { 682 ModuleSP module_sp(GetModule()); 683 if (module_sp) 684 { 685 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 686 bool can_parse = false; 687 lldb::offset_t offset = 0; 688 m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); 689 // Leave magic in the original byte order 690 m_header.magic = m_data.GetU32(&offset); 691 switch (m_header.magic) 692 { 693 case HeaderMagic32: 694 m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); 695 m_data.SetAddressByteSize(4); 696 can_parse = true; 697 break; 698 699 case HeaderMagic64: 700 m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); 701 m_data.SetAddressByteSize(8); 702 can_parse = true; 703 break; 704 705 case HeaderMagic32Swapped: 706 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); 707 m_data.SetAddressByteSize(4); 708 can_parse = true; 709 break; 710 711 case HeaderMagic64Swapped: 712 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); 713 m_data.SetAddressByteSize(8); 714 can_parse = true; 715 break; 716 717 default: 718 break; 719 } 720 721 if (can_parse) 722 { 723 m_data.GetU32(&offset, &m_header.cputype, 6); 724 725 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); 726 727 // Check if the module has a required architecture 728 const ArchSpec &module_arch = module_sp->GetArchitecture(); 729 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch)) 730 return false; 731 732 if (SetModulesArchitecture (mach_arch)) 733 { 734 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic); 735 if (m_data.GetByteSize() < header_and_lc_size) 736 { 737 DataBufferSP data_sp; 738 ProcessSP process_sp (m_process_wp.lock()); 739 if (process_sp) 740 { 741 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size); 742 } 743 else 744 { 745 // Read in all only the load command data from the file on disk 746 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size); 747 if (data_sp->GetByteSize() != header_and_lc_size) 748 return false; 749 } 750 if (data_sp) 751 m_data.SetData (data_sp); 752 } 753 } 754 return true; 755 } 756 else 757 { 758 memset(&m_header, 0, sizeof(struct mach_header)); 759 } 760 } 761 return false; 762 } 763 764 765 ByteOrder 766 ObjectFileMachO::GetByteOrder () const 767 { 768 return m_data.GetByteOrder (); 769 } 770 771 bool 772 ObjectFileMachO::IsExecutable() const 773 { 774 return m_header.filetype == HeaderFileTypeExecutable; 775 } 776 777 uint32_t 778 ObjectFileMachO::GetAddressByteSize () const 779 { 780 return m_data.GetAddressByteSize (); 781 } 782 783 AddressClass 784 ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr) 785 { 786 Symtab *symtab = GetSymtab(); 787 if (symtab) 788 { 789 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); 790 if (symbol) 791 { 792 if (symbol->ValueIsAddress()) 793 { 794 SectionSP section_sp (symbol->GetAddress().GetSection()); 795 if (section_sp) 796 { 797 const SectionType section_type = section_sp->GetType(); 798 switch (section_type) 799 { 800 case eSectionTypeInvalid: return eAddressClassUnknown; 801 case eSectionTypeCode: 802 if (m_header.cputype == llvm::MachO::CPUTypeARM) 803 { 804 // For ARM we have a bit in the n_desc field of the symbol 805 // that tells us ARM/Thumb which is bit 0x0008. 806 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB) 807 return eAddressClassCodeAlternateISA; 808 } 809 return eAddressClassCode; 810 811 case eSectionTypeContainer: return eAddressClassUnknown; 812 case eSectionTypeData: 813 case eSectionTypeDataCString: 814 case eSectionTypeDataCStringPointers: 815 case eSectionTypeDataSymbolAddress: 816 case eSectionTypeData4: 817 case eSectionTypeData8: 818 case eSectionTypeData16: 819 case eSectionTypeDataPointers: 820 case eSectionTypeZeroFill: 821 case eSectionTypeDataObjCMessageRefs: 822 case eSectionTypeDataObjCCFStrings: 823 return eAddressClassData; 824 case eSectionTypeDebug: 825 case eSectionTypeDWARFDebugAbbrev: 826 case eSectionTypeDWARFDebugAranges: 827 case eSectionTypeDWARFDebugFrame: 828 case eSectionTypeDWARFDebugInfo: 829 case eSectionTypeDWARFDebugLine: 830 case eSectionTypeDWARFDebugLoc: 831 case eSectionTypeDWARFDebugMacInfo: 832 case eSectionTypeDWARFDebugPubNames: 833 case eSectionTypeDWARFDebugPubTypes: 834 case eSectionTypeDWARFDebugRanges: 835 case eSectionTypeDWARFDebugStr: 836 case eSectionTypeDWARFAppleNames: 837 case eSectionTypeDWARFAppleTypes: 838 case eSectionTypeDWARFAppleNamespaces: 839 case eSectionTypeDWARFAppleObjC: 840 return eAddressClassDebug; 841 case eSectionTypeEHFrame: return eAddressClassRuntime; 842 case eSectionTypeELFSymbolTable: 843 case eSectionTypeELFDynamicSymbols: 844 case eSectionTypeELFRelocationEntries: 845 case eSectionTypeELFDynamicLinkInfo: 846 case eSectionTypeOther: return eAddressClassUnknown; 847 } 848 } 849 } 850 851 const SymbolType symbol_type = symbol->GetType(); 852 switch (symbol_type) 853 { 854 case eSymbolTypeAny: return eAddressClassUnknown; 855 case eSymbolTypeAbsolute: return eAddressClassUnknown; 856 857 case eSymbolTypeCode: 858 case eSymbolTypeTrampoline: 859 case eSymbolTypeResolver: 860 if (m_header.cputype == llvm::MachO::CPUTypeARM) 861 { 862 // For ARM we have a bit in the n_desc field of the symbol 863 // that tells us ARM/Thumb which is bit 0x0008. 864 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB) 865 return eAddressClassCodeAlternateISA; 866 } 867 return eAddressClassCode; 868 869 case eSymbolTypeData: return eAddressClassData; 870 case eSymbolTypeRuntime: return eAddressClassRuntime; 871 case eSymbolTypeException: return eAddressClassRuntime; 872 case eSymbolTypeSourceFile: return eAddressClassDebug; 873 case eSymbolTypeHeaderFile: return eAddressClassDebug; 874 case eSymbolTypeObjectFile: return eAddressClassDebug; 875 case eSymbolTypeCommonBlock: return eAddressClassDebug; 876 case eSymbolTypeBlock: return eAddressClassDebug; 877 case eSymbolTypeLocal: return eAddressClassData; 878 case eSymbolTypeParam: return eAddressClassData; 879 case eSymbolTypeVariable: return eAddressClassData; 880 case eSymbolTypeVariableType: return eAddressClassDebug; 881 case eSymbolTypeLineEntry: return eAddressClassDebug; 882 case eSymbolTypeLineHeader: return eAddressClassDebug; 883 case eSymbolTypeScopeBegin: return eAddressClassDebug; 884 case eSymbolTypeScopeEnd: return eAddressClassDebug; 885 case eSymbolTypeAdditional: return eAddressClassUnknown; 886 case eSymbolTypeCompiler: return eAddressClassDebug; 887 case eSymbolTypeInstrumentation:return eAddressClassDebug; 888 case eSymbolTypeUndefined: return eAddressClassUnknown; 889 case eSymbolTypeObjCClass: return eAddressClassRuntime; 890 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime; 891 case eSymbolTypeObjCIVar: return eAddressClassRuntime; 892 } 893 } 894 } 895 return eAddressClassUnknown; 896 } 897 898 Symtab * 899 ObjectFileMachO::GetSymtab() 900 { 901 ModuleSP module_sp(GetModule()); 902 if (module_sp) 903 { 904 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 905 if (m_symtab_ap.get() == NULL) 906 { 907 m_symtab_ap.reset(new Symtab(this)); 908 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex()); 909 ParseSymtab (); 910 m_symtab_ap->Finalize (); 911 } 912 } 913 return m_symtab_ap.get(); 914 } 915 916 bool 917 ObjectFileMachO::IsStripped () 918 { 919 if (m_dysymtab.cmd == 0) 920 { 921 ModuleSP module_sp(GetModule()); 922 if (module_sp) 923 { 924 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 925 for (uint32_t i=0; i<m_header.ncmds; ++i) 926 { 927 const lldb::offset_t load_cmd_offset = offset; 928 929 load_command lc; 930 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL) 931 break; 932 if (lc.cmd == LoadCommandDynamicSymtabInfo) 933 { 934 m_dysymtab.cmd = lc.cmd; 935 m_dysymtab.cmdsize = lc.cmdsize; 936 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL) 937 { 938 // Clear m_dysymtab if we were unable to read all items from the load command 939 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab)); 940 } 941 } 942 offset = load_cmd_offset + lc.cmdsize; 943 } 944 } 945 } 946 if (m_dysymtab.cmd) 947 return m_dysymtab.nlocalsym == 0; 948 return false; 949 } 950 951 void 952 ObjectFileMachO::CreateSections (SectionList &unified_section_list) 953 { 954 if (!m_sections_ap.get()) 955 { 956 m_sections_ap.reset(new SectionList()); 957 958 const bool is_dsym = (m_header.filetype == HeaderFileTypeDSYM); 959 lldb::user_id_t segID = 0; 960 lldb::user_id_t sectID = 0; 961 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 962 uint32_t i; 963 const bool is_core = GetType() == eTypeCoreFile; 964 //bool dump_sections = false; 965 ModuleSP module_sp (GetModule()); 966 // First look up any LC_ENCRYPTION_INFO load commands 967 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges; 968 EncryptedFileRanges encrypted_file_ranges; 969 encryption_info_command encryption_cmd; 970 for (i=0; i<m_header.ncmds; ++i) 971 { 972 const lldb::offset_t load_cmd_offset = offset; 973 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL) 974 break; 975 976 if (encryption_cmd.cmd == LoadCommandEncryptionInfo) 977 { 978 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3)) 979 { 980 if (encryption_cmd.cryptid != 0) 981 { 982 EncryptedFileRanges::Entry entry; 983 entry.SetRangeBase(encryption_cmd.cryptoff); 984 entry.SetByteSize(encryption_cmd.cryptsize); 985 encrypted_file_ranges.Append(entry); 986 } 987 } 988 } 989 offset = load_cmd_offset + encryption_cmd.cmdsize; 990 } 991 992 offset = MachHeaderSizeFromMagic(m_header.magic); 993 994 struct segment_command_64 load_cmd; 995 for (i=0; i<m_header.ncmds; ++i) 996 { 997 const lldb::offset_t load_cmd_offset = offset; 998 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 999 break; 1000 1001 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64) 1002 { 1003 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16)) 1004 { 1005 bool add_section = true; 1006 bool add_to_unified = true; 1007 ConstString const_segname (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname))); 1008 1009 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname)); 1010 if (is_dsym && unified_section_sp) 1011 { 1012 if (const_segname == GetSegmentNameLINKEDIT()) 1013 { 1014 // We need to keep the __LINKEDIT segment private to this object file only 1015 add_to_unified = false; 1016 } 1017 else 1018 { 1019 // This is the dSYM file and this section has already been created by 1020 // the object file, no need to create it. 1021 add_section = false; 1022 } 1023 } 1024 load_cmd.vmaddr = m_data.GetAddress(&offset); 1025 load_cmd.vmsize = m_data.GetAddress(&offset); 1026 load_cmd.fileoff = m_data.GetAddress(&offset); 1027 load_cmd.filesize = m_data.GetAddress(&offset); 1028 if (m_length != 0 && load_cmd.filesize != 0) 1029 { 1030 if (load_cmd.fileoff > m_length) 1031 { 1032 // We have a load command that says it extends past the end of hte file. This is likely 1033 // a corrupt file. We don't have any way to return an error condition here (this method 1034 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do 1035 // is null out the SectionList vector and if a process has been set up, dump a message 1036 // to stdout. The most common case here is core file debugging with a truncated file. 1037 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT"; 1038 module_sp->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")", 1039 i, 1040 lc_segment_name, 1041 load_cmd.fileoff, 1042 m_length); 1043 1044 load_cmd.fileoff = 0; 1045 load_cmd.filesize = 0; 1046 } 1047 1048 if (load_cmd.fileoff + load_cmd.filesize > m_length) 1049 { 1050 // We have a load command that says it extends past the end of hte file. This is likely 1051 // a corrupt file. We don't have any way to return an error condition here (this method 1052 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do 1053 // is null out the SectionList vector and if a process has been set up, dump a message 1054 // to stdout. The most common case here is core file debugging with a truncated file. 1055 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT"; 1056 module_sp->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff + filesize (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 "), the segment will be truncated", 1057 i, 1058 lc_segment_name, 1059 load_cmd.fileoff + load_cmd.filesize, 1060 m_length); 1061 1062 // Tuncase the length 1063 load_cmd.filesize = m_length - load_cmd.fileoff; 1064 } 1065 } 1066 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4)) 1067 { 1068 1069 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0; 1070 1071 // Keep a list of mach segments around in case we need to 1072 // get at data that isn't stored in the abstracted Sections. 1073 m_mach_segments.push_back (load_cmd); 1074 1075 // Use a segment ID of the segment index shifted left by 8 so they 1076 // never conflict with any of the sections. 1077 SectionSP segment_sp; 1078 if (add_section && (const_segname || is_core)) 1079 { 1080 segment_sp.reset(new Section (module_sp, // Module to which this section belongs 1081 this, // Object file to which this sections belongs 1082 ++segID << 8, // Section ID is the 1 based segment index shifted right by 8 bits as not to collide with any of the 256 section IDs that are possible 1083 const_segname, // Name of this section 1084 eSectionTypeContainer, // This section is a container of other sections. 1085 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file 1086 load_cmd.vmsize, // VM size in bytes of this section 1087 load_cmd.fileoff, // Offset to the data for this section in the file 1088 load_cmd.filesize, // Size in bytes of this section as found in the the file 1089 load_cmd.flags)); // Flags for this section 1090 1091 segment_sp->SetIsEncrypted (segment_is_encrypted); 1092 m_sections_ap->AddSection(segment_sp); 1093 if (add_to_unified) 1094 unified_section_list.AddSection(segment_sp); 1095 } 1096 else if (unified_section_sp) 1097 { 1098 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr) 1099 { 1100 // Check to see if the module was read from memory? 1101 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid()) 1102 { 1103 // We have a module that is in memory and needs to have its 1104 // file address adjusted. We need to do this because when we 1105 // load a file from memory, its addresses will be slid already, 1106 // yet the addresses in the new symbol file will still be unslid. 1107 // Since everything is stored as section offset, this shouldn't 1108 // cause any problems. 1109 unified_section_sp->SetFileAddress(load_cmd.vmaddr); 1110 } 1111 } 1112 m_sections_ap->AddSection(unified_section_sp); 1113 } 1114 1115 struct section_64 sect64; 1116 ::memset (§64, 0, sizeof(sect64)); 1117 // Push a section into our mach sections for the section at 1118 // index zero (NListSectionNoSection) if we don't have any 1119 // mach sections yet... 1120 if (m_mach_sections.empty()) 1121 m_mach_sections.push_back(sect64); 1122 uint32_t segment_sect_idx; 1123 const lldb::user_id_t first_segment_sectID = sectID + 1; 1124 1125 1126 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8; 1127 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx) 1128 { 1129 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL) 1130 break; 1131 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL) 1132 break; 1133 sect64.addr = m_data.GetAddress(&offset); 1134 sect64.size = m_data.GetAddress(&offset); 1135 1136 if (m_data.GetU32(&offset, §64.offset, num_u32s) == NULL) 1137 break; 1138 1139 // Keep a list of mach sections around in case we need to 1140 // get at data that isn't stored in the abstracted Sections. 1141 m_mach_sections.push_back (sect64); 1142 1143 if (add_section) 1144 { 1145 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname))); 1146 if (!const_segname) 1147 { 1148 // We have a segment with no name so we need to conjure up 1149 // segments that correspond to the section's segname if there 1150 // isn't already such a section. If there is such a section, 1151 // we resize the section so that it spans all sections. 1152 // We also mark these sections as fake so address matches don't 1153 // hit if they land in the gaps between the child sections. 1154 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname)); 1155 segment_sp = unified_section_list.FindSectionByName (const_segname); 1156 if (segment_sp.get()) 1157 { 1158 Section *segment = segment_sp.get(); 1159 // Grow the section size as needed. 1160 const lldb::addr_t sect64_min_addr = sect64.addr; 1161 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size; 1162 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize(); 1163 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress(); 1164 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size; 1165 if (sect64_min_addr >= curr_seg_min_addr) 1166 { 1167 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr; 1168 // Only grow the section size if needed 1169 if (new_seg_byte_size > curr_seg_byte_size) 1170 segment->SetByteSize (new_seg_byte_size); 1171 } 1172 else 1173 { 1174 // We need to change the base address of the segment and 1175 // adjust the child section offsets for all existing children. 1176 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr; 1177 segment->Slide(slide_amount, false); 1178 segment->GetChildren().Slide(-slide_amount, false); 1179 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr); 1180 } 1181 1182 // Grow the section size as needed. 1183 if (sect64.offset) 1184 { 1185 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset(); 1186 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize(); 1187 1188 const lldb::addr_t section_min_file_offset = sect64.offset; 1189 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size; 1190 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset); 1191 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset; 1192 segment->SetFileOffset (new_file_offset); 1193 segment->SetFileSize (new_file_size); 1194 } 1195 } 1196 else 1197 { 1198 // Create a fake section for the section's named segment 1199 segment_sp.reset(new Section (segment_sp, // Parent section 1200 module_sp, // Module to which this section belongs 1201 this, // Object file to which this section belongs 1202 ++segID << 8, // Section ID is the 1 based segment index shifted right by 8 bits as not to collide with any of the 256 section IDs that are possible 1203 const_segname, // Name of this section 1204 eSectionTypeContainer, // This section is a container of other sections. 1205 sect64.addr, // File VM address == addresses as they are found in the object file 1206 sect64.size, // VM size in bytes of this section 1207 sect64.offset, // Offset to the data for this section in the file 1208 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file 1209 load_cmd.flags)); // Flags for this section 1210 segment_sp->SetIsFake(true); 1211 1212 m_sections_ap->AddSection(segment_sp); 1213 if (add_to_unified) 1214 unified_section_list.AddSection(segment_sp); 1215 segment_sp->SetIsEncrypted (segment_is_encrypted); 1216 } 1217 } 1218 assert (segment_sp.get()); 1219 1220 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType; 1221 static ConstString g_sect_name_objc_data ("__objc_data"); 1222 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs"); 1223 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs"); 1224 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs"); 1225 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs"); 1226 static ConstString g_sect_name_objc_const ("__objc_const"); 1227 static ConstString g_sect_name_objc_classlist ("__objc_classlist"); 1228 static ConstString g_sect_name_cfstring ("__cfstring"); 1229 1230 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev"); 1231 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges"); 1232 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame"); 1233 static ConstString g_sect_name_dwarf_debug_info ("__debug_info"); 1234 static ConstString g_sect_name_dwarf_debug_line ("__debug_line"); 1235 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc"); 1236 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo"); 1237 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames"); 1238 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes"); 1239 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges"); 1240 static ConstString g_sect_name_dwarf_debug_str ("__debug_str"); 1241 static ConstString g_sect_name_dwarf_apple_names ("__apple_names"); 1242 static ConstString g_sect_name_dwarf_apple_types ("__apple_types"); 1243 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac"); 1244 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc"); 1245 static ConstString g_sect_name_eh_frame ("__eh_frame"); 1246 static ConstString g_sect_name_DATA ("__DATA"); 1247 static ConstString g_sect_name_TEXT ("__TEXT"); 1248 1249 SectionType sect_type = eSectionTypeOther; 1250 1251 if (section_name == g_sect_name_dwarf_debug_abbrev) 1252 sect_type = eSectionTypeDWARFDebugAbbrev; 1253 else if (section_name == g_sect_name_dwarf_debug_aranges) 1254 sect_type = eSectionTypeDWARFDebugAranges; 1255 else if (section_name == g_sect_name_dwarf_debug_frame) 1256 sect_type = eSectionTypeDWARFDebugFrame; 1257 else if (section_name == g_sect_name_dwarf_debug_info) 1258 sect_type = eSectionTypeDWARFDebugInfo; 1259 else if (section_name == g_sect_name_dwarf_debug_line) 1260 sect_type = eSectionTypeDWARFDebugLine; 1261 else if (section_name == g_sect_name_dwarf_debug_loc) 1262 sect_type = eSectionTypeDWARFDebugLoc; 1263 else if (section_name == g_sect_name_dwarf_debug_macinfo) 1264 sect_type = eSectionTypeDWARFDebugMacInfo; 1265 else if (section_name == g_sect_name_dwarf_debug_pubnames) 1266 sect_type = eSectionTypeDWARFDebugPubNames; 1267 else if (section_name == g_sect_name_dwarf_debug_pubtypes) 1268 sect_type = eSectionTypeDWARFDebugPubTypes; 1269 else if (section_name == g_sect_name_dwarf_debug_ranges) 1270 sect_type = eSectionTypeDWARFDebugRanges; 1271 else if (section_name == g_sect_name_dwarf_debug_str) 1272 sect_type = eSectionTypeDWARFDebugStr; 1273 else if (section_name == g_sect_name_dwarf_apple_names) 1274 sect_type = eSectionTypeDWARFAppleNames; 1275 else if (section_name == g_sect_name_dwarf_apple_types) 1276 sect_type = eSectionTypeDWARFAppleTypes; 1277 else if (section_name == g_sect_name_dwarf_apple_namespaces) 1278 sect_type = eSectionTypeDWARFAppleNamespaces; 1279 else if (section_name == g_sect_name_dwarf_apple_objc) 1280 sect_type = eSectionTypeDWARFAppleObjC; 1281 else if (section_name == g_sect_name_objc_selrefs) 1282 sect_type = eSectionTypeDataCStringPointers; 1283 else if (section_name == g_sect_name_objc_msgrefs) 1284 sect_type = eSectionTypeDataObjCMessageRefs; 1285 else if (section_name == g_sect_name_eh_frame) 1286 sect_type = eSectionTypeEHFrame; 1287 else if (section_name == g_sect_name_cfstring) 1288 sect_type = eSectionTypeDataObjCCFStrings; 1289 else if (section_name == g_sect_name_objc_data || 1290 section_name == g_sect_name_objc_classrefs || 1291 section_name == g_sect_name_objc_superrefs || 1292 section_name == g_sect_name_objc_const || 1293 section_name == g_sect_name_objc_classlist) 1294 { 1295 sect_type = eSectionTypeDataPointers; 1296 } 1297 1298 if (sect_type == eSectionTypeOther) 1299 { 1300 switch (mach_sect_type) 1301 { 1302 // TODO: categorize sections by other flags for regular sections 1303 case SectionTypeRegular: 1304 if (segment_sp->GetName() == g_sect_name_TEXT) 1305 sect_type = eSectionTypeCode; 1306 else if (segment_sp->GetName() == g_sect_name_DATA) 1307 sect_type = eSectionTypeData; 1308 else 1309 sect_type = eSectionTypeOther; 1310 break; 1311 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break; 1312 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings 1313 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals 1314 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals 1315 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals 1316 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers 1317 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers 1318 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field 1319 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization 1320 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination 1321 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break; 1322 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break; 1323 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing 1324 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals 1325 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break; 1326 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break; 1327 default: break; 1328 } 1329 } 1330 1331 SectionSP section_sp(new Section (segment_sp, 1332 module_sp, 1333 this, 1334 ++sectID, 1335 section_name, 1336 sect_type, 1337 sect64.addr - segment_sp->GetFileAddress(), 1338 sect64.size, 1339 sect64.offset, 1340 sect64.offset == 0 ? 0 : sect64.size, 1341 sect64.flags)); 1342 // Set the section to be encrypted to match the segment 1343 1344 bool section_is_encrypted = false; 1345 if (!segment_is_encrypted && load_cmd.filesize != 0) 1346 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL; 1347 1348 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted); 1349 segment_sp->GetChildren().AddSection(section_sp); 1350 1351 if (segment_sp->IsFake()) 1352 { 1353 segment_sp.reset(); 1354 const_segname.Clear(); 1355 } 1356 } 1357 } 1358 if (segment_sp && is_dsym) 1359 { 1360 if (first_segment_sectID <= sectID) 1361 { 1362 lldb::user_id_t sect_uid; 1363 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid) 1364 { 1365 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid)); 1366 SectionSP next_section_sp; 1367 if (sect_uid + 1 <= sectID) 1368 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1); 1369 1370 if (curr_section_sp.get()) 1371 { 1372 if (curr_section_sp->GetByteSize() == 0) 1373 { 1374 if (next_section_sp.get() != NULL) 1375 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() ); 1376 else 1377 curr_section_sp->SetByteSize ( load_cmd.vmsize ); 1378 } 1379 } 1380 } 1381 } 1382 } 1383 } 1384 } 1385 } 1386 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo) 1387 { 1388 m_dysymtab.cmd = load_cmd.cmd; 1389 m_dysymtab.cmdsize = load_cmd.cmdsize; 1390 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2); 1391 } 1392 1393 offset = load_cmd_offset + load_cmd.cmdsize; 1394 } 1395 1396 // StreamFile s(stdout, false); // REMOVE THIS LINE 1397 // s.Printf ("Sections for %s:\n", m_file.GetPath().c_str());// REMOVE THIS LINE 1398 // m_sections_ap->Dump(&s, NULL, true, UINT32_MAX);// REMOVE THIS LINE 1399 } 1400 } 1401 1402 class MachSymtabSectionInfo 1403 { 1404 public: 1405 1406 MachSymtabSectionInfo (SectionList *section_list) : 1407 m_section_list (section_list), 1408 m_section_infos() 1409 { 1410 // Get the number of sections down to a depth of 1 to include 1411 // all segments and their sections, but no other sections that 1412 // may be added for debug map or 1413 m_section_infos.resize(section_list->GetNumSections(1)); 1414 } 1415 1416 1417 SectionSP 1418 GetSection (uint8_t n_sect, addr_t file_addr) 1419 { 1420 if (n_sect == 0) 1421 return SectionSP(); 1422 if (n_sect < m_section_infos.size()) 1423 { 1424 if (!m_section_infos[n_sect].section_sp) 1425 { 1426 SectionSP section_sp (m_section_list->FindSectionByID (n_sect)); 1427 m_section_infos[n_sect].section_sp = section_sp; 1428 if (section_sp) 1429 { 1430 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress()); 1431 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize()); 1432 } 1433 else 1434 { 1435 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect); 1436 } 1437 } 1438 if (m_section_infos[n_sect].vm_range.Contains(file_addr)) 1439 { 1440 // Symbol is in section. 1441 return m_section_infos[n_sect].section_sp; 1442 } 1443 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 && 1444 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr) 1445 { 1446 // Symbol is in section with zero size, but has the same start 1447 // address as the section. This can happen with linker symbols 1448 // (symbols that start with the letter 'l' or 'L'. 1449 return m_section_infos[n_sect].section_sp; 1450 } 1451 } 1452 return m_section_list->FindSectionContainingFileAddress(file_addr); 1453 } 1454 1455 protected: 1456 struct SectionInfo 1457 { 1458 SectionInfo () : 1459 vm_range(), 1460 section_sp () 1461 { 1462 } 1463 1464 VMRange vm_range; 1465 SectionSP section_sp; 1466 }; 1467 SectionList *m_section_list; 1468 std::vector<SectionInfo> m_section_infos; 1469 }; 1470 1471 size_t 1472 ObjectFileMachO::ParseSymtab () 1473 { 1474 Timer scoped_timer(__PRETTY_FUNCTION__, 1475 "ObjectFileMachO::ParseSymtab () module = %s", 1476 m_file.GetFilename().AsCString("")); 1477 ModuleSP module_sp (GetModule()); 1478 if (!module_sp) 1479 return 0; 1480 1481 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 }; 1482 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 }; 1483 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts; 1484 FunctionStarts function_starts; 1485 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 1486 uint32_t i; 1487 1488 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS)); 1489 1490 for (i=0; i<m_header.ncmds; ++i) 1491 { 1492 const lldb::offset_t cmd_offset = offset; 1493 // Read in the load command and load command size 1494 struct load_command lc; 1495 if (m_data.GetU32(&offset, &lc, 2) == NULL) 1496 break; 1497 // Watch for the symbol table load command 1498 switch (lc.cmd) 1499 { 1500 case LoadCommandSymtab: 1501 symtab_load_command.cmd = lc.cmd; 1502 symtab_load_command.cmdsize = lc.cmdsize; 1503 // Read in the rest of the symtab load command 1504 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields 1505 return 0; 1506 if (symtab_load_command.symoff == 0) 1507 { 1508 if (log) 1509 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0"); 1510 return 0; 1511 } 1512 1513 if (symtab_load_command.stroff == 0) 1514 { 1515 if (log) 1516 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0"); 1517 return 0; 1518 } 1519 1520 if (symtab_load_command.nsyms == 0) 1521 { 1522 if (log) 1523 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0"); 1524 return 0; 1525 } 1526 1527 if (symtab_load_command.strsize == 0) 1528 { 1529 if (log) 1530 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0"); 1531 return 0; 1532 } 1533 break; 1534 1535 case LoadCommandFunctionStarts: 1536 function_starts_load_command.cmd = lc.cmd; 1537 function_starts_load_command.cmdsize = lc.cmdsize; 1538 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields 1539 bzero (&function_starts_load_command, sizeof(function_starts_load_command)); 1540 break; 1541 1542 default: 1543 break; 1544 } 1545 offset = cmd_offset + lc.cmdsize; 1546 } 1547 1548 if (symtab_load_command.cmd) 1549 { 1550 Symtab *symtab = m_symtab_ap.get(); 1551 SectionList *section_list = GetSectionList(); 1552 if (section_list == NULL) 1553 return 0; 1554 1555 const uint32_t addr_byte_size = m_data.GetAddressByteSize(); 1556 const ByteOrder byte_order = m_data.GetByteOrder(); 1557 bool bit_width_32 = addr_byte_size == 4; 1558 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64); 1559 1560 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size); 1561 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size); 1562 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size); 1563 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size); 1564 1565 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size; 1566 const addr_t strtab_data_byte_size = symtab_load_command.strsize; 1567 addr_t strtab_addr = LLDB_INVALID_ADDRESS; 1568 1569 ProcessSP process_sp (m_process_wp.lock()); 1570 Process *process = process_sp.get(); 1571 1572 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete; 1573 1574 if (process) 1575 { 1576 Target &target = process->GetTarget(); 1577 1578 memory_module_load_level = target.GetMemoryModuleLoadLevel(); 1579 1580 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT())); 1581 // Reading mach file from memory in a process or core file... 1582 1583 if (linkedit_section_sp) 1584 { 1585 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target); 1586 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset(); 1587 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset; 1588 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset; 1589 1590 bool data_was_read = false; 1591 1592 #if defined (__APPLE__) && defined (__arm__) 1593 if (m_header.flags & 0x80000000u) 1594 { 1595 // This mach-o memory file is in the dyld shared cache. If this 1596 // program is not remote and this is iOS, then this process will 1597 // share the same shared cache as the process we are debugging and 1598 // we can read the entire __LINKEDIT from the address space in this 1599 // process. This is a needed optimization that is used for local iOS 1600 // debugging only since all shared libraries in the shared cache do 1601 // not have corresponding files that exist in the file system of the 1602 // device. They have been combined into a single file. This means we 1603 // always have to load these files from memory. All of the symbol and 1604 // string tables from all of the __LINKEDIT sections from the shared 1605 // libraries in the shared cache have been merged into a single large 1606 // symbol and string table. Reading all of this symbol and string table 1607 // data across can slow down debug launch times, so we optimize this by 1608 // reading the memory for the __LINKEDIT section from this process. 1609 1610 UUID lldb_shared_cache(GetLLDBSharedCacheUUID()); 1611 UUID process_shared_cache(GetProcessSharedCacheUUID(process)); 1612 bool use_lldb_cache = true; 1613 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache) 1614 { 1615 use_lldb_cache = false; 1616 ModuleSP module_sp (GetModule()); 1617 if (module_sp) 1618 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow."); 1619 1620 } 1621 1622 PlatformSP platform_sp (target.GetPlatform()); 1623 if (platform_sp && platform_sp->IsHost() && use_lldb_cache) 1624 { 1625 data_was_read = true; 1626 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle); 1627 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle); 1628 if (function_starts_load_command.cmd) 1629 { 1630 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset; 1631 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle); 1632 } 1633 } 1634 } 1635 #endif 1636 1637 if (!data_was_read) 1638 { 1639 if (memory_module_load_level == eMemoryModuleLoadLevelComplete) 1640 { 1641 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size)); 1642 if (nlist_data_sp) 1643 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize()); 1644 // Load strings individually from memory when loading from memory since shared cache 1645 // string tables contain strings for all symbols from all shared cached libraries 1646 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size)); 1647 //if (strtab_data_sp) 1648 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize()); 1649 if (m_dysymtab.nindirectsyms != 0) 1650 { 1651 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset; 1652 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4)); 1653 if (indirect_syms_data_sp) 1654 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize()); 1655 } 1656 } 1657 1658 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial) 1659 { 1660 if (function_starts_load_command.cmd) 1661 { 1662 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset; 1663 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize)); 1664 if (func_start_data_sp) 1665 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize()); 1666 } 1667 } 1668 } 1669 } 1670 } 1671 else 1672 { 1673 nlist_data.SetData (m_data, 1674 symtab_load_command.symoff, 1675 nlist_data_byte_size); 1676 strtab_data.SetData (m_data, 1677 symtab_load_command.stroff, 1678 strtab_data_byte_size); 1679 if (m_dysymtab.nindirectsyms != 0) 1680 { 1681 indirect_symbol_index_data.SetData (m_data, 1682 m_dysymtab.indirectsymoff, 1683 m_dysymtab.nindirectsyms * 4); 1684 } 1685 if (function_starts_load_command.cmd) 1686 { 1687 function_starts_data.SetData (m_data, 1688 function_starts_load_command.dataoff, 1689 function_starts_load_command.datasize); 1690 } 1691 } 1692 1693 if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete) 1694 { 1695 if (log) 1696 module_sp->LogMessage(log, "failed to read nlist data"); 1697 return 0; 1698 } 1699 1700 1701 const bool have_strtab_data = strtab_data.GetByteSize() > 0; 1702 if (!have_strtab_data) 1703 { 1704 if (process) 1705 { 1706 if (strtab_addr == LLDB_INVALID_ADDRESS) 1707 { 1708 if (log) 1709 module_sp->LogMessage(log, "failed to locate the strtab in memory"); 1710 return 0; 1711 } 1712 } 1713 else 1714 { 1715 if (log) 1716 module_sp->LogMessage(log, "failed to read strtab data"); 1717 return 0; 1718 } 1719 } 1720 1721 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT(); 1722 const ConstString &g_segment_name_DATA = GetSegmentNameDATA(); 1723 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC(); 1724 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame(); 1725 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT)); 1726 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA)); 1727 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC)); 1728 SectionSP eh_frame_section_sp; 1729 if (text_section_sp.get()) 1730 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame); 1731 else 1732 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame); 1733 1734 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM); 1735 1736 // lldb works best if it knows the start addresss of all functions in a module. 1737 // Linker symbols or debug info are normally the best source of information for start addr / size but 1738 // they may be stripped in a released binary. 1739 // Two additional sources of information exist in Mach-O binaries: 1740 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the 1741 // binary, relative to the text section. 1742 // eh_frame - the eh_frame FDEs have the start addr & size of each function 1743 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries. 1744 // Binaries built to run on older releases may need to use eh_frame information. 1745 1746 if (text_section_sp && function_starts_data.GetByteSize()) 1747 { 1748 FunctionStarts::Entry function_start_entry; 1749 function_start_entry.data = false; 1750 lldb::offset_t function_start_offset = 0; 1751 function_start_entry.addr = text_section_sp->GetFileAddress(); 1752 uint64_t delta; 1753 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0) 1754 { 1755 // Now append the current entry 1756 function_start_entry.addr += delta; 1757 function_starts.Append(function_start_entry); 1758 } 1759 } 1760 else 1761 { 1762 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame 1763 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any 1764 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in 1765 // the module. 1766 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo) 1767 { 1768 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true); 1769 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions; 1770 eh_frame.GetFunctionAddressAndSizeVector (functions); 1771 addr_t text_base_addr = text_section_sp->GetFileAddress(); 1772 size_t count = functions.GetSize(); 1773 for (size_t i = 0; i < count; ++i) 1774 { 1775 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i); 1776 if (func) 1777 { 1778 FunctionStarts::Entry function_start_entry; 1779 function_start_entry.addr = func->base - text_base_addr; 1780 function_starts.Append(function_start_entry); 1781 } 1782 } 1783 } 1784 } 1785 1786 const size_t function_starts_count = function_starts.GetSize(); 1787 1788 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection; 1789 1790 lldb::offset_t nlist_data_offset = 0; 1791 1792 uint32_t N_SO_index = UINT32_MAX; 1793 1794 MachSymtabSectionInfo section_info (section_list); 1795 std::vector<uint32_t> N_FUN_indexes; 1796 std::vector<uint32_t> N_NSYM_indexes; 1797 std::vector<uint32_t> N_INCL_indexes; 1798 std::vector<uint32_t> N_BRAC_indexes; 1799 std::vector<uint32_t> N_COMM_indexes; 1800 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap; 1801 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap; 1802 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap; 1803 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx; 1804 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx; 1805 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx; 1806 // Any symbols that get merged into another will get an entry 1807 // in this map so we know 1808 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx; 1809 uint32_t nlist_idx = 0; 1810 Symbol *symbol_ptr = NULL; 1811 1812 uint32_t sym_idx = 0; 1813 Symbol *sym = NULL; 1814 size_t num_syms = 0; 1815 std::string memory_symbol_name; 1816 uint32_t unmapped_local_symbols_found = 0; 1817 1818 #if defined (__APPLE__) && defined (__arm__) 1819 1820 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL 1821 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained, 1822 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some* 1823 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt 1824 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal 1825 // nlist parser to ignore all LOCAL symbols. 1826 1827 if (m_header.flags & 0x80000000u) 1828 { 1829 // Before we can start mapping the DSC, we need to make certain the target process is actually 1830 // using the cache we can find. 1831 1832 // Next we need to determine the correct path for the dyld shared cache. 1833 1834 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); 1835 char dsc_path[PATH_MAX]; 1836 1837 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s", 1838 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */ 1839 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */ 1840 header_arch.GetArchitectureName()); 1841 1842 FileSpec dsc_filespec(dsc_path, false); 1843 1844 // We need definitions of two structures in the on-disk DSC, copy them here manually 1845 struct lldb_copy_dyld_cache_header_v0 1846 { 1847 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc. 1848 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info 1849 uint32_t mappingCount; // number of dyld_cache_mapping_info entries 1850 uint32_t imagesOffset; 1851 uint32_t imagesCount; 1852 uint64_t dyldBaseAddress; 1853 uint64_t codeSignatureOffset; 1854 uint64_t codeSignatureSize; 1855 uint64_t slideInfoOffset; 1856 uint64_t slideInfoSize; 1857 uint64_t localSymbolsOffset; // file offset of where local symbols are stored 1858 uint64_t localSymbolsSize; // size of local symbols information 1859 }; 1860 struct lldb_copy_dyld_cache_header_v1 1861 { 1862 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc. 1863 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info 1864 uint32_t mappingCount; // number of dyld_cache_mapping_info entries 1865 uint32_t imagesOffset; 1866 uint32_t imagesCount; 1867 uint64_t dyldBaseAddress; 1868 uint64_t codeSignatureOffset; 1869 uint64_t codeSignatureSize; 1870 uint64_t slideInfoOffset; 1871 uint64_t slideInfoSize; 1872 uint64_t localSymbolsOffset; 1873 uint64_t localSymbolsSize; 1874 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later 1875 }; 1876 1877 struct lldb_copy_dyld_cache_mapping_info 1878 { 1879 uint64_t address; 1880 uint64_t size; 1881 uint64_t fileOffset; 1882 uint32_t maxProt; 1883 uint32_t initProt; 1884 }; 1885 1886 struct lldb_copy_dyld_cache_local_symbols_info 1887 { 1888 uint32_t nlistOffset; 1889 uint32_t nlistCount; 1890 uint32_t stringsOffset; 1891 uint32_t stringsSize; 1892 uint32_t entriesOffset; 1893 uint32_t entriesCount; 1894 }; 1895 struct lldb_copy_dyld_cache_local_symbols_entry 1896 { 1897 uint32_t dylibOffset; 1898 uint32_t nlistStartIndex; 1899 uint32_t nlistCount; 1900 }; 1901 1902 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset). 1903 The dyld_cache_local_symbols_info structure gives us three things: 1904 1. The start and count of the nlist records in the dyld_shared_cache file 1905 2. The start and size of the strings for these nlist records 1906 3. The start and count of dyld_cache_local_symbols_entry entries 1907 1908 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache. 1909 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache. 1910 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records 1911 and the count of how many nlist records there are for this dylib/framework. 1912 */ 1913 1914 // Process the dsc header to find the unmapped symbols 1915 // 1916 // Save some VM space, do not map the entire cache in one shot. 1917 1918 DataBufferSP dsc_data_sp; 1919 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1)); 1920 1921 if (dsc_data_sp) 1922 { 1923 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size); 1924 1925 char version_str[17]; 1926 int version = -1; 1927 lldb::offset_t offset = 0; 1928 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16); 1929 version_str[16] = '\0'; 1930 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6])) 1931 { 1932 int v; 1933 if (::sscanf (version_str + 6, "%d", &v) == 1) 1934 { 1935 version = v; 1936 } 1937 } 1938 1939 UUID dsc_uuid; 1940 if (version >= 1) 1941 { 1942 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid); 1943 uint8_t uuid_bytes[sizeof (uuid_t)]; 1944 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t)); 1945 dsc_uuid.SetBytes (uuid_bytes); 1946 } 1947 1948 bool uuid_match = true; 1949 if (dsc_uuid.IsValid() && process) 1950 { 1951 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process)); 1952 1953 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid) 1954 { 1955 // The on-disk dyld_shared_cache file is not the same as the one in this 1956 // process' memory, don't use it. 1957 uuid_match = false; 1958 ModuleSP module_sp (GetModule()); 1959 if (module_sp) 1960 module_sp->ReportWarning ("process shared cache does not match on-disk dyld_shared_cache file, some symbol names will be missing."); 1961 } 1962 } 1963 1964 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset); 1965 1966 uint32_t mappingOffset = dsc_header_data.GetU32(&offset); 1967 1968 // If the mappingOffset points to a location inside the header, we've 1969 // opened an old dyld shared cache, and should not proceed further. 1970 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0)) 1971 { 1972 1973 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info)); 1974 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size); 1975 offset = 0; 1976 1977 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries 1978 // in the shared library cache need to be adjusted by an offset to match up with the 1979 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is 1980 // recorded in mapping_offset_value. 1981 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset); 1982 1983 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset); 1984 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset); 1985 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset); 1986 1987 if (localSymbolsOffset && localSymbolsSize) 1988 { 1989 // Map the local symbols 1990 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize)) 1991 { 1992 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size); 1993 1994 offset = 0; 1995 1996 // Read the local_symbols_infos struct in one shot 1997 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info; 1998 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6); 1999 2000 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT())); 2001 2002 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value); 2003 2004 offset = local_symbols_info.entriesOffset; 2005 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++) 2006 { 2007 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry; 2008 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset); 2009 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset); 2010 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset); 2011 2012 if (header_file_offset == local_symbols_entry.dylibOffset) 2013 { 2014 unmapped_local_symbols_found = local_symbols_entry.nlistCount; 2015 2016 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here. 2017 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym); 2018 num_syms = symtab->GetNumSymbols(); 2019 2020 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex); 2021 uint32_t string_table_offset = local_symbols_info.stringsOffset; 2022 2023 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++) 2024 { 2025 ///////////////////////////// 2026 { 2027 struct nlist_64 nlist; 2028 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size)) 2029 break; 2030 2031 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset); 2032 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset); 2033 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset); 2034 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset); 2035 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset); 2036 2037 SymbolType type = eSymbolTypeInvalid; 2038 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx); 2039 2040 if (symbol_name == NULL) 2041 { 2042 // No symbol should be NULL, even the symbols with no 2043 // string values should have an offset zero which points 2044 // to an empty C-string 2045 Host::SystemLog (Host::eSystemLogError, 2046 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n", 2047 entry_index, 2048 nlist.n_strx, 2049 module_sp->GetFileSpec().GetPath().c_str()); 2050 continue; 2051 } 2052 if (symbol_name[0] == '\0') 2053 symbol_name = NULL; 2054 2055 const char *symbol_name_non_abi_mangled = NULL; 2056 2057 SectionSP symbol_section; 2058 uint32_t symbol_byte_size = 0; 2059 bool add_nlist = true; 2060 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0); 2061 bool demangled_is_synthesized = false; 2062 bool is_gsym = false; 2063 2064 assert (sym_idx < num_syms); 2065 2066 sym[sym_idx].SetDebug (is_debug); 2067 2068 if (is_debug) 2069 { 2070 switch (nlist.n_type) 2071 { 2072 case StabGlobalSymbol: 2073 // N_GSYM -- global symbol: name,,NO_SECT,type,0 2074 // Sometimes the N_GSYM value contains the address. 2075 2076 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They 2077 // have the same address, but we want to ensure that we always find only the real symbol, 2078 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass 2079 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated 2080 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the 2081 // same address. 2082 2083 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O' 2084 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0 2085 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0 2086 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0)) 2087 add_nlist = false; 2088 else 2089 { 2090 is_gsym = true; 2091 sym[sym_idx].SetExternal(true); 2092 if (nlist.n_value != 0) 2093 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2094 type = eSymbolTypeData; 2095 } 2096 break; 2097 2098 case StabFunctionName: 2099 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0 2100 type = eSymbolTypeCompiler; 2101 break; 2102 2103 case StabFunction: 2104 // N_FUN -- procedure: name,,n_sect,linenumber,address 2105 if (symbol_name) 2106 { 2107 type = eSymbolTypeCode; 2108 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2109 2110 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx; 2111 // We use the current number of symbols in the symbol table in lieu of 2112 // using nlist_idx in case we ever start trimming entries out 2113 N_FUN_indexes.push_back(sym_idx); 2114 } 2115 else 2116 { 2117 type = eSymbolTypeCompiler; 2118 2119 if ( !N_FUN_indexes.empty() ) 2120 { 2121 // Copy the size of the function into the original STAB entry so we don't have 2122 // to hunt for it later 2123 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value); 2124 N_FUN_indexes.pop_back(); 2125 // We don't really need the end function STAB as it contains the size which 2126 // we already placed with the original symbol, so don't add it if we want a 2127 // minimal symbol table 2128 add_nlist = false; 2129 } 2130 } 2131 break; 2132 2133 case StabStaticSymbol: 2134 // N_STSYM -- static symbol: name,,n_sect,type,address 2135 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx; 2136 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2137 type = eSymbolTypeData; 2138 break; 2139 2140 case StabLocalCommon: 2141 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address 2142 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2143 type = eSymbolTypeCommonBlock; 2144 break; 2145 2146 case StabBeginSymbol: 2147 // N_BNSYM 2148 // We use the current number of symbols in the symbol table in lieu of 2149 // using nlist_idx in case we ever start trimming entries out 2150 // Skip these if we want minimal symbol tables 2151 add_nlist = false; 2152 break; 2153 2154 case StabEndSymbol: 2155 // N_ENSYM 2156 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM 2157 // so that we can always skip the entire symbol if we need to navigate 2158 // more quickly at the source level when parsing STABS 2159 // Skip these if we want minimal symbol tables 2160 add_nlist = false; 2161 break; 2162 2163 2164 case StabSourceFileOptions: 2165 // N_OPT - emitted with gcc2_compiled and in gcc source 2166 type = eSymbolTypeCompiler; 2167 break; 2168 2169 case StabRegisterSymbol: 2170 // N_RSYM - register sym: name,,NO_SECT,type,register 2171 type = eSymbolTypeVariable; 2172 break; 2173 2174 case StabSourceLine: 2175 // N_SLINE - src line: 0,,n_sect,linenumber,address 2176 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2177 type = eSymbolTypeLineEntry; 2178 break; 2179 2180 case StabStructureType: 2181 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset 2182 type = eSymbolTypeVariableType; 2183 break; 2184 2185 case StabSourceFileName: 2186 // N_SO - source file name 2187 type = eSymbolTypeSourceFile; 2188 if (symbol_name == NULL) 2189 { 2190 add_nlist = false; 2191 if (N_SO_index != UINT32_MAX) 2192 { 2193 // Set the size of the N_SO to the terminating index of this N_SO 2194 // so that we can always skip the entire N_SO if we need to navigate 2195 // more quickly at the source level when parsing STABS 2196 symbol_ptr = symtab->SymbolAtIndex(N_SO_index); 2197 symbol_ptr->SetByteSize(sym_idx); 2198 symbol_ptr->SetSizeIsSibling(true); 2199 } 2200 N_NSYM_indexes.clear(); 2201 N_INCL_indexes.clear(); 2202 N_BRAC_indexes.clear(); 2203 N_COMM_indexes.clear(); 2204 N_FUN_indexes.clear(); 2205 N_SO_index = UINT32_MAX; 2206 } 2207 else 2208 { 2209 // We use the current number of symbols in the symbol table in lieu of 2210 // using nlist_idx in case we ever start trimming entries out 2211 const bool N_SO_has_full_path = symbol_name[0] == '/'; 2212 if (N_SO_has_full_path) 2213 { 2214 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) 2215 { 2216 // We have two consecutive N_SO entries where the first contains a directory 2217 // and the second contains a full path. 2218 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false); 2219 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; 2220 add_nlist = false; 2221 } 2222 else 2223 { 2224 // This is the first entry in a N_SO that contains a directory or 2225 // a full path to the source file 2226 N_SO_index = sym_idx; 2227 } 2228 } 2229 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) 2230 { 2231 // This is usually the second N_SO entry that contains just the filename, 2232 // so here we combine it with the first one if we are minimizing the symbol table 2233 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString(); 2234 if (so_path && so_path[0]) 2235 { 2236 std::string full_so_path (so_path); 2237 const size_t double_slash_pos = full_so_path.find("//"); 2238 if (double_slash_pos != std::string::npos) 2239 { 2240 // The linker has been generating bad N_SO entries with doubled up paths 2241 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir, 2242 // and the second is the directory for the source file so you end up with 2243 // a path that looks like "/tmp/src//tmp/src/" 2244 FileSpec so_dir(so_path, false); 2245 if (!so_dir.Exists()) 2246 { 2247 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false); 2248 if (so_dir.Exists()) 2249 { 2250 // Trim off the incorrect path 2251 full_so_path.erase(0, double_slash_pos + 1); 2252 } 2253 } 2254 } 2255 if (*full_so_path.rbegin() != '/') 2256 full_so_path += '/'; 2257 full_so_path += symbol_name; 2258 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false); 2259 add_nlist = false; 2260 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; 2261 } 2262 } 2263 else 2264 { 2265 // This could be a relative path to a N_SO 2266 N_SO_index = sym_idx; 2267 } 2268 } 2269 break; 2270 2271 case StabObjectFileName: 2272 // N_OSO - object file name: name,,0,0,st_mtime 2273 type = eSymbolTypeObjectFile; 2274 break; 2275 2276 case StabLocalSymbol: 2277 // N_LSYM - local sym: name,,NO_SECT,type,offset 2278 type = eSymbolTypeLocal; 2279 break; 2280 2281 //---------------------------------------------------------------------- 2282 // INCL scopes 2283 //---------------------------------------------------------------------- 2284 case StabBeginIncludeFileName: 2285 // N_BINCL - include file beginning: name,,NO_SECT,0,sum 2286 // We use the current number of symbols in the symbol table in lieu of 2287 // using nlist_idx in case we ever start trimming entries out 2288 N_INCL_indexes.push_back(sym_idx); 2289 type = eSymbolTypeScopeBegin; 2290 break; 2291 2292 case StabEndIncludeFile: 2293 // N_EINCL - include file end: name,,NO_SECT,0,0 2294 // Set the size of the N_BINCL to the terminating index of this N_EINCL 2295 // so that we can always skip the entire symbol if we need to navigate 2296 // more quickly at the source level when parsing STABS 2297 if ( !N_INCL_indexes.empty() ) 2298 { 2299 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back()); 2300 symbol_ptr->SetByteSize(sym_idx + 1); 2301 symbol_ptr->SetSizeIsSibling(true); 2302 N_INCL_indexes.pop_back(); 2303 } 2304 type = eSymbolTypeScopeEnd; 2305 break; 2306 2307 case StabIncludeFileName: 2308 // N_SOL - #included file name: name,,n_sect,0,address 2309 type = eSymbolTypeHeaderFile; 2310 2311 // We currently don't use the header files on darwin 2312 add_nlist = false; 2313 break; 2314 2315 case StabCompilerParameters: 2316 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0 2317 type = eSymbolTypeCompiler; 2318 break; 2319 2320 case StabCompilerVersion: 2321 // N_VERSION - compiler version: name,,NO_SECT,0,0 2322 type = eSymbolTypeCompiler; 2323 break; 2324 2325 case StabCompilerOptLevel: 2326 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0 2327 type = eSymbolTypeCompiler; 2328 break; 2329 2330 case StabParameter: 2331 // N_PSYM - parameter: name,,NO_SECT,type,offset 2332 type = eSymbolTypeVariable; 2333 break; 2334 2335 case StabAlternateEntry: 2336 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address 2337 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2338 type = eSymbolTypeLineEntry; 2339 break; 2340 2341 //---------------------------------------------------------------------- 2342 // Left and Right Braces 2343 //---------------------------------------------------------------------- 2344 case StabLeftBracket: 2345 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address 2346 // We use the current number of symbols in the symbol table in lieu of 2347 // using nlist_idx in case we ever start trimming entries out 2348 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2349 N_BRAC_indexes.push_back(sym_idx); 2350 type = eSymbolTypeScopeBegin; 2351 break; 2352 2353 case StabRightBracket: 2354 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address 2355 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC 2356 // so that we can always skip the entire symbol if we need to navigate 2357 // more quickly at the source level when parsing STABS 2358 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2359 if ( !N_BRAC_indexes.empty() ) 2360 { 2361 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back()); 2362 symbol_ptr->SetByteSize(sym_idx + 1); 2363 symbol_ptr->SetSizeIsSibling(true); 2364 N_BRAC_indexes.pop_back(); 2365 } 2366 type = eSymbolTypeScopeEnd; 2367 break; 2368 2369 case StabDeletedIncludeFile: 2370 // N_EXCL - deleted include file: name,,NO_SECT,0,sum 2371 type = eSymbolTypeHeaderFile; 2372 break; 2373 2374 //---------------------------------------------------------------------- 2375 // COMM scopes 2376 //---------------------------------------------------------------------- 2377 case StabBeginCommon: 2378 // N_BCOMM - begin common: name,,NO_SECT,0,0 2379 // We use the current number of symbols in the symbol table in lieu of 2380 // using nlist_idx in case we ever start trimming entries out 2381 type = eSymbolTypeScopeBegin; 2382 N_COMM_indexes.push_back(sym_idx); 2383 break; 2384 2385 case StabEndCommonLocal: 2386 // N_ECOML - end common (local name): 0,,n_sect,0,address 2387 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2388 // Fall through 2389 2390 case StabEndCommon: 2391 // N_ECOMM - end common: name,,n_sect,0,0 2392 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML 2393 // so that we can always skip the entire symbol if we need to navigate 2394 // more quickly at the source level when parsing STABS 2395 if ( !N_COMM_indexes.empty() ) 2396 { 2397 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back()); 2398 symbol_ptr->SetByteSize(sym_idx + 1); 2399 symbol_ptr->SetSizeIsSibling(true); 2400 N_COMM_indexes.pop_back(); 2401 } 2402 type = eSymbolTypeScopeEnd; 2403 break; 2404 2405 case StabLength: 2406 // N_LENG - second stab entry with length information 2407 type = eSymbolTypeAdditional; 2408 break; 2409 2410 default: break; 2411 } 2412 } 2413 else 2414 { 2415 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type; 2416 uint8_t n_type = NlistMaskType & nlist.n_type; 2417 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0); 2418 2419 switch (n_type) 2420 { 2421 case NListTypeIndirect: // N_INDR - Fall through 2422 case NListTypePreboundUndefined:// N_PBUD - Fall through 2423 case NListTypeUndefined: // N_UNDF 2424 type = eSymbolTypeUndefined; 2425 break; 2426 2427 case NListTypeAbsolute: // N_ABS 2428 type = eSymbolTypeAbsolute; 2429 break; 2430 2431 case NListTypeSection: // N_SECT 2432 { 2433 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2434 2435 if (symbol_section == NULL) 2436 { 2437 // TODO: warn about this? 2438 add_nlist = false; 2439 break; 2440 } 2441 2442 if (TEXT_eh_frame_sectID == nlist.n_sect) 2443 { 2444 type = eSymbolTypeException; 2445 } 2446 else 2447 { 2448 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType; 2449 2450 switch (section_type) 2451 { 2452 case SectionTypeRegular: break; // regular section 2453 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section 2454 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings 2455 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals 2456 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals 2457 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals 2458 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers 2459 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers 2460 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field 2461 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization 2462 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination 2463 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced 2464 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes) 2465 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing 2466 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals 2467 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break; 2468 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break; 2469 default: break; 2470 } 2471 2472 if (type == eSymbolTypeInvalid) 2473 { 2474 const char *symbol_sect_name = symbol_section->GetName().AsCString(); 2475 if (symbol_section->IsDescendant (text_section_sp.get())) 2476 { 2477 if (symbol_section->IsClear(SectionAttrUserPureInstructions | 2478 SectionAttrUserSelfModifyingCode | 2479 SectionAttrSytemSomeInstructions)) 2480 type = eSymbolTypeData; 2481 else 2482 type = eSymbolTypeCode; 2483 } 2484 else if (symbol_section->IsDescendant(data_section_sp.get())) 2485 { 2486 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name) 2487 { 2488 type = eSymbolTypeRuntime; 2489 2490 if (symbol_name && 2491 symbol_name[0] == '_' && 2492 symbol_name[1] == 'O' && 2493 symbol_name[2] == 'B') 2494 { 2495 llvm::StringRef symbol_name_ref(symbol_name); 2496 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_"); 2497 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_"); 2498 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_"); 2499 if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) 2500 { 2501 symbol_name_non_abi_mangled = symbol_name + 1; 2502 symbol_name = symbol_name + g_objc_v2_prefix_class.size(); 2503 type = eSymbolTypeObjCClass; 2504 demangled_is_synthesized = true; 2505 } 2506 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass)) 2507 { 2508 symbol_name_non_abi_mangled = symbol_name + 1; 2509 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size(); 2510 type = eSymbolTypeObjCMetaClass; 2511 demangled_is_synthesized = true; 2512 } 2513 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar)) 2514 { 2515 symbol_name_non_abi_mangled = symbol_name + 1; 2516 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size(); 2517 type = eSymbolTypeObjCIVar; 2518 demangled_is_synthesized = true; 2519 } 2520 } 2521 } 2522 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name) 2523 { 2524 type = eSymbolTypeException; 2525 } 2526 else 2527 { 2528 type = eSymbolTypeData; 2529 } 2530 } 2531 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name) 2532 { 2533 type = eSymbolTypeTrampoline; 2534 } 2535 else if (symbol_section->IsDescendant(objc_section_sp.get())) 2536 { 2537 type = eSymbolTypeRuntime; 2538 if (symbol_name && symbol_name[0] == '.') 2539 { 2540 llvm::StringRef symbol_name_ref(symbol_name); 2541 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_"); 2542 if (symbol_name_ref.startswith(g_objc_v1_prefix_class)) 2543 { 2544 symbol_name_non_abi_mangled = symbol_name; 2545 symbol_name = symbol_name + g_objc_v1_prefix_class.size(); 2546 type = eSymbolTypeObjCClass; 2547 demangled_is_synthesized = true; 2548 } 2549 } 2550 } 2551 } 2552 } 2553 } 2554 break; 2555 } 2556 } 2557 2558 if (add_nlist) 2559 { 2560 uint64_t symbol_value = nlist.n_value; 2561 if (symbol_name_non_abi_mangled) 2562 { 2563 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled)); 2564 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name)); 2565 } 2566 else 2567 { 2568 bool symbol_name_is_mangled = false; 2569 2570 if (symbol_name && symbol_name[0] == '_') 2571 { 2572 symbol_name_is_mangled = symbol_name[1] == '_'; 2573 symbol_name++; // Skip the leading underscore 2574 } 2575 2576 if (symbol_name) 2577 { 2578 ConstString const_symbol_name(symbol_name); 2579 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled); 2580 if (is_gsym && is_debug) 2581 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx; 2582 } 2583 } 2584 if (symbol_section) 2585 { 2586 const addr_t section_file_addr = symbol_section->GetFileAddress(); 2587 if (symbol_byte_size == 0 && function_starts_count > 0) 2588 { 2589 addr_t symbol_lookup_file_addr = nlist.n_value; 2590 // Do an exact address match for non-ARM addresses, else get the closest since 2591 // the symbol might be a thumb symbol which has an address with bit zero set 2592 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm); 2593 if (is_arm && func_start_entry) 2594 { 2595 // Verify that the function start address is the symbol address (ARM) 2596 // or the symbol address + 1 (thumb) 2597 if (func_start_entry->addr != symbol_lookup_file_addr && 2598 func_start_entry->addr != (symbol_lookup_file_addr + 1)) 2599 { 2600 // Not the right entry, NULL it out... 2601 func_start_entry = NULL; 2602 } 2603 } 2604 if (func_start_entry) 2605 { 2606 func_start_entry->data = true; 2607 2608 addr_t symbol_file_addr = func_start_entry->addr; 2609 uint32_t symbol_flags = 0; 2610 if (is_arm) 2611 { 2612 if (symbol_file_addr & 1) 2613 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB; 2614 symbol_file_addr &= 0xfffffffffffffffeull; 2615 } 2616 2617 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry); 2618 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize(); 2619 if (next_func_start_entry) 2620 { 2621 addr_t next_symbol_file_addr = next_func_start_entry->addr; 2622 // Be sure the clear the Thumb address bit when we calculate the size 2623 // from the current and next address 2624 if (is_arm) 2625 next_symbol_file_addr &= 0xfffffffffffffffeull; 2626 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr); 2627 } 2628 else 2629 { 2630 symbol_byte_size = section_end_file_addr - symbol_file_addr; 2631 } 2632 } 2633 } 2634 symbol_value -= section_file_addr; 2635 } 2636 2637 if (is_debug == false) 2638 { 2639 if (type == eSymbolTypeCode) 2640 { 2641 // See if we can find a N_FUN entry for any code symbols. 2642 // If we do find a match, and the name matches, then we 2643 // can merge the two into just the function symbol to avoid 2644 // duplicate entries in the symbol table 2645 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value); 2646 if (pos != N_FUN_addr_to_sym_idx.end()) 2647 { 2648 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled)) 2649 { 2650 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 2651 // We just need the flags from the linker symbol, so put these flags 2652 // into the N_FUN flags to avoid duplicate symbols in the symbol table 2653 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); 2654 sym[sym_idx].Clear(); 2655 continue; 2656 } 2657 } 2658 } 2659 else if (type == eSymbolTypeData) 2660 { 2661 // See if we can find a N_STSYM entry for any data symbols. 2662 // If we do find a match, and the name matches, then we 2663 // can merge the two into just the Static symbol to avoid 2664 // duplicate entries in the symbol table 2665 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value); 2666 if (pos != N_STSYM_addr_to_sym_idx.end()) 2667 { 2668 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled)) 2669 { 2670 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 2671 // We just need the flags from the linker symbol, so put these flags 2672 // into the N_STSYM flags to avoid duplicate symbols in the symbol table 2673 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); 2674 sym[sym_idx].Clear(); 2675 continue; 2676 } 2677 } 2678 else 2679 { 2680 // Combine N_GSYM stab entries with the non stab symbol 2681 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()); 2682 if (pos != N_GSYM_name_to_sym_idx.end()) 2683 { 2684 const uint32_t GSYM_sym_idx = pos->second; 2685 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx; 2686 // Copy the address, because often the N_GSYM address has an invalid address of zero 2687 // when the global is a common symbol 2688 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section); 2689 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value); 2690 // We just need the flags from the linker symbol, so put these flags 2691 // into the N_STSYM flags to avoid duplicate symbols in the symbol table 2692 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); 2693 sym[sym_idx].Clear(); 2694 continue; 2695 } 2696 } 2697 } 2698 } 2699 2700 sym[sym_idx].SetID (nlist_idx); 2701 sym[sym_idx].SetType (type); 2702 sym[sym_idx].GetAddress().SetSection (symbol_section); 2703 sym[sym_idx].GetAddress().SetOffset (symbol_value); 2704 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); 2705 2706 if (symbol_byte_size > 0) 2707 sym[sym_idx].SetByteSize(symbol_byte_size); 2708 2709 if (demangled_is_synthesized) 2710 sym[sym_idx].SetDemangledNameIsSynthesized(true); 2711 ++sym_idx; 2712 } 2713 else 2714 { 2715 sym[sym_idx].Clear(); 2716 } 2717 2718 } 2719 ///////////////////////////// 2720 } 2721 break; // No more entries to consider 2722 } 2723 } 2724 } 2725 } 2726 } 2727 } 2728 } 2729 2730 // Must reset this in case it was mutated above! 2731 nlist_data_offset = 0; 2732 #endif 2733 2734 if (nlist_data.GetByteSize() > 0) 2735 { 2736 2737 // If the sym array was not created while parsing the DSC unmapped 2738 // symbols, create it now. 2739 if (sym == NULL) 2740 { 2741 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); 2742 num_syms = symtab->GetNumSymbols(); 2743 } 2744 2745 if (unmapped_local_symbols_found) 2746 { 2747 assert(m_dysymtab.ilocalsym == 0); 2748 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size); 2749 nlist_idx = m_dysymtab.nlocalsym; 2750 } 2751 else 2752 { 2753 nlist_idx = 0; 2754 } 2755 2756 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) 2757 { 2758 struct nlist_64 nlist; 2759 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size)) 2760 break; 2761 2762 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset); 2763 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset); 2764 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset); 2765 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset); 2766 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset); 2767 2768 SymbolType type = eSymbolTypeInvalid; 2769 const char *symbol_name = NULL; 2770 2771 if (have_strtab_data) 2772 { 2773 symbol_name = strtab_data.PeekCStr(nlist.n_strx); 2774 2775 if (symbol_name == NULL) 2776 { 2777 // No symbol should be NULL, even the symbols with no 2778 // string values should have an offset zero which points 2779 // to an empty C-string 2780 Host::SystemLog (Host::eSystemLogError, 2781 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n", 2782 nlist_idx, 2783 nlist.n_strx, 2784 module_sp->GetFileSpec().GetPath().c_str()); 2785 continue; 2786 } 2787 if (symbol_name[0] == '\0') 2788 symbol_name = NULL; 2789 } 2790 else 2791 { 2792 const addr_t str_addr = strtab_addr + nlist.n_strx; 2793 Error str_error; 2794 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error)) 2795 symbol_name = memory_symbol_name.c_str(); 2796 } 2797 const char *symbol_name_non_abi_mangled = NULL; 2798 2799 SectionSP symbol_section; 2800 lldb::addr_t symbol_byte_size = 0; 2801 bool add_nlist = true; 2802 bool is_gsym = false; 2803 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0); 2804 bool demangled_is_synthesized = false; 2805 2806 assert (sym_idx < num_syms); 2807 2808 sym[sym_idx].SetDebug (is_debug); 2809 2810 if (is_debug) 2811 { 2812 switch (nlist.n_type) 2813 { 2814 case StabGlobalSymbol: 2815 // N_GSYM -- global symbol: name,,NO_SECT,type,0 2816 // Sometimes the N_GSYM value contains the address. 2817 2818 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They 2819 // have the same address, but we want to ensure that we always find only the real symbol, 2820 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass 2821 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated 2822 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the 2823 // same address. 2824 2825 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O' 2826 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0 2827 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0 2828 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0)) 2829 add_nlist = false; 2830 else 2831 { 2832 is_gsym = true; 2833 sym[sym_idx].SetExternal(true); 2834 if (nlist.n_value != 0) 2835 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2836 type = eSymbolTypeData; 2837 } 2838 break; 2839 2840 case StabFunctionName: 2841 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0 2842 type = eSymbolTypeCompiler; 2843 break; 2844 2845 case StabFunction: 2846 // N_FUN -- procedure: name,,n_sect,linenumber,address 2847 if (symbol_name) 2848 { 2849 type = eSymbolTypeCode; 2850 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2851 2852 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx; 2853 // We use the current number of symbols in the symbol table in lieu of 2854 // using nlist_idx in case we ever start trimming entries out 2855 N_FUN_indexes.push_back(sym_idx); 2856 } 2857 else 2858 { 2859 type = eSymbolTypeCompiler; 2860 2861 if ( !N_FUN_indexes.empty() ) 2862 { 2863 // Copy the size of the function into the original STAB entry so we don't have 2864 // to hunt for it later 2865 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value); 2866 N_FUN_indexes.pop_back(); 2867 // We don't really need the end function STAB as it contains the size which 2868 // we already placed with the original symbol, so don't add it if we want a 2869 // minimal symbol table 2870 add_nlist = false; 2871 } 2872 } 2873 break; 2874 2875 case StabStaticSymbol: 2876 // N_STSYM -- static symbol: name,,n_sect,type,address 2877 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx; 2878 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2879 type = eSymbolTypeData; 2880 break; 2881 2882 case StabLocalCommon: 2883 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address 2884 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2885 type = eSymbolTypeCommonBlock; 2886 break; 2887 2888 case StabBeginSymbol: 2889 // N_BNSYM 2890 // We use the current number of symbols in the symbol table in lieu of 2891 // using nlist_idx in case we ever start trimming entries out 2892 // Skip these if we want minimal symbol tables 2893 add_nlist = false; 2894 break; 2895 2896 case StabEndSymbol: 2897 // N_ENSYM 2898 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM 2899 // so that we can always skip the entire symbol if we need to navigate 2900 // more quickly at the source level when parsing STABS 2901 // Skip these if we want minimal symbol tables 2902 add_nlist = false; 2903 break; 2904 2905 2906 case StabSourceFileOptions: 2907 // N_OPT - emitted with gcc2_compiled and in gcc source 2908 type = eSymbolTypeCompiler; 2909 break; 2910 2911 case StabRegisterSymbol: 2912 // N_RSYM - register sym: name,,NO_SECT,type,register 2913 type = eSymbolTypeVariable; 2914 break; 2915 2916 case StabSourceLine: 2917 // N_SLINE - src line: 0,,n_sect,linenumber,address 2918 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 2919 type = eSymbolTypeLineEntry; 2920 break; 2921 2922 case StabStructureType: 2923 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset 2924 type = eSymbolTypeVariableType; 2925 break; 2926 2927 case StabSourceFileName: 2928 // N_SO - source file name 2929 type = eSymbolTypeSourceFile; 2930 if (symbol_name == NULL) 2931 { 2932 add_nlist = false; 2933 if (N_SO_index != UINT32_MAX) 2934 { 2935 // Set the size of the N_SO to the terminating index of this N_SO 2936 // so that we can always skip the entire N_SO if we need to navigate 2937 // more quickly at the source level when parsing STABS 2938 symbol_ptr = symtab->SymbolAtIndex(N_SO_index); 2939 symbol_ptr->SetByteSize(sym_idx); 2940 symbol_ptr->SetSizeIsSibling(true); 2941 } 2942 N_NSYM_indexes.clear(); 2943 N_INCL_indexes.clear(); 2944 N_BRAC_indexes.clear(); 2945 N_COMM_indexes.clear(); 2946 N_FUN_indexes.clear(); 2947 N_SO_index = UINT32_MAX; 2948 } 2949 else 2950 { 2951 // We use the current number of symbols in the symbol table in lieu of 2952 // using nlist_idx in case we ever start trimming entries out 2953 const bool N_SO_has_full_path = symbol_name[0] == '/'; 2954 if (N_SO_has_full_path) 2955 { 2956 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) 2957 { 2958 // We have two consecutive N_SO entries where the first contains a directory 2959 // and the second contains a full path. 2960 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false); 2961 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; 2962 add_nlist = false; 2963 } 2964 else 2965 { 2966 // This is the first entry in a N_SO that contains a directory or 2967 // a full path to the source file 2968 N_SO_index = sym_idx; 2969 } 2970 } 2971 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) 2972 { 2973 // This is usually the second N_SO entry that contains just the filename, 2974 // so here we combine it with the first one if we are minimizing the symbol table 2975 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString(); 2976 if (so_path && so_path[0]) 2977 { 2978 std::string full_so_path (so_path); 2979 const size_t double_slash_pos = full_so_path.find("//"); 2980 if (double_slash_pos != std::string::npos) 2981 { 2982 // The linker has been generating bad N_SO entries with doubled up paths 2983 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir, 2984 // and the second is the directory for the source file so you end up with 2985 // a path that looks like "/tmp/src//tmp/src/" 2986 FileSpec so_dir(so_path, false); 2987 if (!so_dir.Exists()) 2988 { 2989 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false); 2990 if (so_dir.Exists()) 2991 { 2992 // Trim off the incorrect path 2993 full_so_path.erase(0, double_slash_pos + 1); 2994 } 2995 } 2996 } 2997 if (*full_so_path.rbegin() != '/') 2998 full_so_path += '/'; 2999 full_so_path += symbol_name; 3000 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false); 3001 add_nlist = false; 3002 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; 3003 } 3004 } 3005 else 3006 { 3007 // This could be a relative path to a N_SO 3008 N_SO_index = sym_idx; 3009 } 3010 } 3011 3012 break; 3013 3014 case StabObjectFileName: 3015 // N_OSO - object file name: name,,0,0,st_mtime 3016 type = eSymbolTypeObjectFile; 3017 break; 3018 3019 case StabLocalSymbol: 3020 // N_LSYM - local sym: name,,NO_SECT,type,offset 3021 type = eSymbolTypeLocal; 3022 break; 3023 3024 //---------------------------------------------------------------------- 3025 // INCL scopes 3026 //---------------------------------------------------------------------- 3027 case StabBeginIncludeFileName: 3028 // N_BINCL - include file beginning: name,,NO_SECT,0,sum 3029 // We use the current number of symbols in the symbol table in lieu of 3030 // using nlist_idx in case we ever start trimming entries out 3031 N_INCL_indexes.push_back(sym_idx); 3032 type = eSymbolTypeScopeBegin; 3033 break; 3034 3035 case StabEndIncludeFile: 3036 // N_EINCL - include file end: name,,NO_SECT,0,0 3037 // Set the size of the N_BINCL to the terminating index of this N_EINCL 3038 // so that we can always skip the entire symbol if we need to navigate 3039 // more quickly at the source level when parsing STABS 3040 if ( !N_INCL_indexes.empty() ) 3041 { 3042 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back()); 3043 symbol_ptr->SetByteSize(sym_idx + 1); 3044 symbol_ptr->SetSizeIsSibling(true); 3045 N_INCL_indexes.pop_back(); 3046 } 3047 type = eSymbolTypeScopeEnd; 3048 break; 3049 3050 case StabIncludeFileName: 3051 // N_SOL - #included file name: name,,n_sect,0,address 3052 type = eSymbolTypeHeaderFile; 3053 3054 // We currently don't use the header files on darwin 3055 add_nlist = false; 3056 break; 3057 3058 case StabCompilerParameters: 3059 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0 3060 type = eSymbolTypeCompiler; 3061 break; 3062 3063 case StabCompilerVersion: 3064 // N_VERSION - compiler version: name,,NO_SECT,0,0 3065 type = eSymbolTypeCompiler; 3066 break; 3067 3068 case StabCompilerOptLevel: 3069 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0 3070 type = eSymbolTypeCompiler; 3071 break; 3072 3073 case StabParameter: 3074 // N_PSYM - parameter: name,,NO_SECT,type,offset 3075 type = eSymbolTypeVariable; 3076 break; 3077 3078 case StabAlternateEntry: 3079 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address 3080 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 3081 type = eSymbolTypeLineEntry; 3082 break; 3083 3084 //---------------------------------------------------------------------- 3085 // Left and Right Braces 3086 //---------------------------------------------------------------------- 3087 case StabLeftBracket: 3088 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address 3089 // We use the current number of symbols in the symbol table in lieu of 3090 // using nlist_idx in case we ever start trimming entries out 3091 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 3092 N_BRAC_indexes.push_back(sym_idx); 3093 type = eSymbolTypeScopeBegin; 3094 break; 3095 3096 case StabRightBracket: 3097 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address 3098 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC 3099 // so that we can always skip the entire symbol if we need to navigate 3100 // more quickly at the source level when parsing STABS 3101 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 3102 if ( !N_BRAC_indexes.empty() ) 3103 { 3104 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back()); 3105 symbol_ptr->SetByteSize(sym_idx + 1); 3106 symbol_ptr->SetSizeIsSibling(true); 3107 N_BRAC_indexes.pop_back(); 3108 } 3109 type = eSymbolTypeScopeEnd; 3110 break; 3111 3112 case StabDeletedIncludeFile: 3113 // N_EXCL - deleted include file: name,,NO_SECT,0,sum 3114 type = eSymbolTypeHeaderFile; 3115 break; 3116 3117 //---------------------------------------------------------------------- 3118 // COMM scopes 3119 //---------------------------------------------------------------------- 3120 case StabBeginCommon: 3121 // N_BCOMM - begin common: name,,NO_SECT,0,0 3122 // We use the current number of symbols in the symbol table in lieu of 3123 // using nlist_idx in case we ever start trimming entries out 3124 type = eSymbolTypeScopeBegin; 3125 N_COMM_indexes.push_back(sym_idx); 3126 break; 3127 3128 case StabEndCommonLocal: 3129 // N_ECOML - end common (local name): 0,,n_sect,0,address 3130 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 3131 // Fall through 3132 3133 case StabEndCommon: 3134 // N_ECOMM - end common: name,,n_sect,0,0 3135 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML 3136 // so that we can always skip the entire symbol if we need to navigate 3137 // more quickly at the source level when parsing STABS 3138 if ( !N_COMM_indexes.empty() ) 3139 { 3140 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back()); 3141 symbol_ptr->SetByteSize(sym_idx + 1); 3142 symbol_ptr->SetSizeIsSibling(true); 3143 N_COMM_indexes.pop_back(); 3144 } 3145 type = eSymbolTypeScopeEnd; 3146 break; 3147 3148 case StabLength: 3149 // N_LENG - second stab entry with length information 3150 type = eSymbolTypeAdditional; 3151 break; 3152 3153 default: break; 3154 } 3155 } 3156 else 3157 { 3158 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type; 3159 uint8_t n_type = NlistMaskType & nlist.n_type; 3160 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0); 3161 3162 switch (n_type) 3163 { 3164 case NListTypeIndirect: // N_INDR - Fall through 3165 case NListTypePreboundUndefined:// N_PBUD - Fall through 3166 case NListTypeUndefined: // N_UNDF 3167 type = eSymbolTypeUndefined; 3168 break; 3169 3170 case NListTypeAbsolute: // N_ABS 3171 type = eSymbolTypeAbsolute; 3172 break; 3173 3174 case NListTypeSection: // N_SECT 3175 { 3176 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 3177 3178 if (!symbol_section) 3179 { 3180 // TODO: warn about this? 3181 add_nlist = false; 3182 break; 3183 } 3184 3185 if (TEXT_eh_frame_sectID == nlist.n_sect) 3186 { 3187 type = eSymbolTypeException; 3188 } 3189 else 3190 { 3191 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType; 3192 3193 switch (section_type) 3194 { 3195 case SectionTypeRegular: break; // regular section 3196 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section 3197 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings 3198 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals 3199 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals 3200 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals 3201 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers 3202 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers 3203 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field 3204 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization 3205 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination 3206 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced 3207 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes) 3208 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing 3209 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals 3210 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break; 3211 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break; 3212 default: break; 3213 } 3214 3215 if (type == eSymbolTypeInvalid) 3216 { 3217 const char *symbol_sect_name = symbol_section->GetName().AsCString(); 3218 if (symbol_section->IsDescendant (text_section_sp.get())) 3219 { 3220 if (symbol_section->IsClear(SectionAttrUserPureInstructions | 3221 SectionAttrUserSelfModifyingCode | 3222 SectionAttrSytemSomeInstructions)) 3223 type = eSymbolTypeData; 3224 else 3225 type = eSymbolTypeCode; 3226 } 3227 else 3228 if (symbol_section->IsDescendant(data_section_sp.get())) 3229 { 3230 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name) 3231 { 3232 type = eSymbolTypeRuntime; 3233 3234 if (symbol_name && 3235 symbol_name[0] == '_' && 3236 symbol_name[1] == 'O' && 3237 symbol_name[2] == 'B') 3238 { 3239 llvm::StringRef symbol_name_ref(symbol_name); 3240 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_"); 3241 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_"); 3242 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_"); 3243 if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) 3244 { 3245 symbol_name_non_abi_mangled = symbol_name + 1; 3246 symbol_name = symbol_name + g_objc_v2_prefix_class.size(); 3247 type = eSymbolTypeObjCClass; 3248 demangled_is_synthesized = true; 3249 } 3250 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass)) 3251 { 3252 symbol_name_non_abi_mangled = symbol_name + 1; 3253 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size(); 3254 type = eSymbolTypeObjCMetaClass; 3255 demangled_is_synthesized = true; 3256 } 3257 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar)) 3258 { 3259 symbol_name_non_abi_mangled = symbol_name + 1; 3260 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size(); 3261 type = eSymbolTypeObjCIVar; 3262 demangled_is_synthesized = true; 3263 } 3264 } 3265 } 3266 else 3267 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name) 3268 { 3269 type = eSymbolTypeException; 3270 } 3271 else 3272 { 3273 type = eSymbolTypeData; 3274 } 3275 } 3276 else 3277 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name) 3278 { 3279 type = eSymbolTypeTrampoline; 3280 } 3281 else 3282 if (symbol_section->IsDescendant(objc_section_sp.get())) 3283 { 3284 type = eSymbolTypeRuntime; 3285 if (symbol_name && symbol_name[0] == '.') 3286 { 3287 llvm::StringRef symbol_name_ref(symbol_name); 3288 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_"); 3289 if (symbol_name_ref.startswith(g_objc_v1_prefix_class)) 3290 { 3291 symbol_name_non_abi_mangled = symbol_name; 3292 symbol_name = symbol_name + g_objc_v1_prefix_class.size(); 3293 type = eSymbolTypeObjCClass; 3294 demangled_is_synthesized = true; 3295 } 3296 } 3297 } 3298 } 3299 } 3300 } 3301 break; 3302 } 3303 } 3304 3305 if (add_nlist) 3306 { 3307 uint64_t symbol_value = nlist.n_value; 3308 3309 if (symbol_name_non_abi_mangled) 3310 { 3311 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled)); 3312 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name)); 3313 } 3314 else 3315 { 3316 bool symbol_name_is_mangled = false; 3317 3318 if (symbol_name && symbol_name[0] == '_') 3319 { 3320 symbol_name_is_mangled = symbol_name[1] == '_'; 3321 symbol_name++; // Skip the leading underscore 3322 } 3323 3324 if (symbol_name) 3325 { 3326 ConstString const_symbol_name(symbol_name); 3327 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled); 3328 if (is_gsym && is_debug) 3329 { 3330 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx; 3331 } 3332 } 3333 } 3334 if (symbol_section) 3335 { 3336 const addr_t section_file_addr = symbol_section->GetFileAddress(); 3337 if (symbol_byte_size == 0 && function_starts_count > 0) 3338 { 3339 addr_t symbol_lookup_file_addr = nlist.n_value; 3340 // Do an exact address match for non-ARM addresses, else get the closest since 3341 // the symbol might be a thumb symbol which has an address with bit zero set 3342 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm); 3343 if (is_arm && func_start_entry) 3344 { 3345 // Verify that the function start address is the symbol address (ARM) 3346 // or the symbol address + 1 (thumb) 3347 if (func_start_entry->addr != symbol_lookup_file_addr && 3348 func_start_entry->addr != (symbol_lookup_file_addr + 1)) 3349 { 3350 // Not the right entry, NULL it out... 3351 func_start_entry = NULL; 3352 } 3353 } 3354 if (func_start_entry) 3355 { 3356 func_start_entry->data = true; 3357 3358 addr_t symbol_file_addr = func_start_entry->addr; 3359 if (is_arm) 3360 symbol_file_addr &= 0xfffffffffffffffeull; 3361 3362 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry); 3363 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize(); 3364 if (next_func_start_entry) 3365 { 3366 addr_t next_symbol_file_addr = next_func_start_entry->addr; 3367 // Be sure the clear the Thumb address bit when we calculate the size 3368 // from the current and next address 3369 if (is_arm) 3370 next_symbol_file_addr &= 0xfffffffffffffffeull; 3371 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr); 3372 } 3373 else 3374 { 3375 symbol_byte_size = section_end_file_addr - symbol_file_addr; 3376 } 3377 } 3378 } 3379 symbol_value -= section_file_addr; 3380 } 3381 3382 if (is_debug == false) 3383 { 3384 if (type == eSymbolTypeCode) 3385 { 3386 // See if we can find a N_FUN entry for any code symbols. 3387 // If we do find a match, and the name matches, then we 3388 // can merge the two into just the function symbol to avoid 3389 // duplicate entries in the symbol table 3390 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value); 3391 if (pos != N_FUN_addr_to_sym_idx.end()) 3392 { 3393 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled)) 3394 { 3395 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 3396 // We just need the flags from the linker symbol, so put these flags 3397 // into the N_FUN flags to avoid duplicate symbols in the symbol table 3398 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); 3399 sym[sym_idx].Clear(); 3400 continue; 3401 } 3402 } 3403 } 3404 else if (type == eSymbolTypeData) 3405 { 3406 // See if we can find a N_STSYM entry for any data symbols. 3407 // If we do find a match, and the name matches, then we 3408 // can merge the two into just the Static symbol to avoid 3409 // duplicate entries in the symbol table 3410 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value); 3411 if (pos != N_STSYM_addr_to_sym_idx.end()) 3412 { 3413 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled)) 3414 { 3415 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 3416 // We just need the flags from the linker symbol, so put these flags 3417 // into the N_STSYM flags to avoid duplicate symbols in the symbol table 3418 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); 3419 sym[sym_idx].Clear(); 3420 continue; 3421 } 3422 } 3423 else 3424 { 3425 // Combine N_GSYM stab entries with the non stab symbol 3426 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()); 3427 if (pos != N_GSYM_name_to_sym_idx.end()) 3428 { 3429 const uint32_t GSYM_sym_idx = pos->second; 3430 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx; 3431 // Copy the address, because often the N_GSYM address has an invalid address of zero 3432 // when the global is a common symbol 3433 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section); 3434 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value); 3435 // We just need the flags from the linker symbol, so put these flags 3436 // into the N_STSYM flags to avoid duplicate symbols in the symbol table 3437 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); 3438 sym[sym_idx].Clear(); 3439 continue; 3440 } 3441 } 3442 } 3443 } 3444 3445 sym[sym_idx].SetID (nlist_idx); 3446 sym[sym_idx].SetType (type); 3447 sym[sym_idx].GetAddress().SetSection (symbol_section); 3448 sym[sym_idx].GetAddress().SetOffset (symbol_value); 3449 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); 3450 3451 if (symbol_byte_size > 0) 3452 sym[sym_idx].SetByteSize(symbol_byte_size); 3453 3454 if (demangled_is_synthesized) 3455 sym[sym_idx].SetDemangledNameIsSynthesized(true); 3456 3457 ++sym_idx; 3458 } 3459 else 3460 { 3461 sym[sym_idx].Clear(); 3462 } 3463 3464 } 3465 3466 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value 3467 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all 3468 // such entries by figuring out what the address for the global is by looking up this non-STAB 3469 // entry and copying the value into the debug symbol's value to save us the hassle in the 3470 // debug symbol parser. 3471 3472 Symbol *global_symbol = NULL; 3473 for (nlist_idx = 0; 3474 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL; 3475 nlist_idx++) 3476 { 3477 if (global_symbol->GetAddress().GetFileAddress() == 0) 3478 { 3479 std::vector<uint32_t> indexes; 3480 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0) 3481 { 3482 std::vector<uint32_t>::const_iterator pos; 3483 std::vector<uint32_t>::const_iterator end = indexes.end(); 3484 for (pos = indexes.begin(); pos != end; ++pos) 3485 { 3486 symbol_ptr = symtab->SymbolAtIndex(*pos); 3487 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false) 3488 { 3489 global_symbol->GetAddress() = symbol_ptr->GetAddress(); 3490 break; 3491 } 3492 } 3493 } 3494 } 3495 } 3496 } 3497 3498 uint32_t synthetic_sym_id = symtab_load_command.nsyms; 3499 3500 if (function_starts_count > 0) 3501 { 3502 char synthetic_function_symbol[PATH_MAX]; 3503 uint32_t num_synthetic_function_symbols = 0; 3504 for (i=0; i<function_starts_count; ++i) 3505 { 3506 if (function_starts.GetEntryRef (i).data == false) 3507 ++num_synthetic_function_symbols; 3508 } 3509 3510 if (num_synthetic_function_symbols > 0) 3511 { 3512 if (num_syms < sym_idx + num_synthetic_function_symbols) 3513 { 3514 num_syms = sym_idx + num_synthetic_function_symbols; 3515 sym = symtab->Resize (num_syms); 3516 } 3517 uint32_t synthetic_function_symbol_idx = 0; 3518 for (i=0; i<function_starts_count; ++i) 3519 { 3520 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i); 3521 if (func_start_entry->data == false) 3522 { 3523 addr_t symbol_file_addr = func_start_entry->addr; 3524 uint32_t symbol_flags = 0; 3525 if (is_arm) 3526 { 3527 if (symbol_file_addr & 1) 3528 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB; 3529 symbol_file_addr &= 0xfffffffffffffffeull; 3530 } 3531 Address symbol_addr; 3532 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr)) 3533 { 3534 SectionSP symbol_section (symbol_addr.GetSection()); 3535 uint32_t symbol_byte_size = 0; 3536 if (symbol_section) 3537 { 3538 const addr_t section_file_addr = symbol_section->GetFileAddress(); 3539 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry); 3540 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize(); 3541 if (next_func_start_entry) 3542 { 3543 addr_t next_symbol_file_addr = next_func_start_entry->addr; 3544 if (is_arm) 3545 next_symbol_file_addr &= 0xfffffffffffffffeull; 3546 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr); 3547 } 3548 else 3549 { 3550 symbol_byte_size = section_end_file_addr - symbol_file_addr; 3551 } 3552 snprintf (synthetic_function_symbol, 3553 sizeof(synthetic_function_symbol), 3554 "___lldb_unnamed_function%u$$%s", 3555 ++synthetic_function_symbol_idx, 3556 module_sp->GetFileSpec().GetFilename().GetCString()); 3557 sym[sym_idx].SetID (synthetic_sym_id++); 3558 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol)); 3559 sym[sym_idx].SetType (eSymbolTypeCode); 3560 sym[sym_idx].SetIsSynthetic (true); 3561 sym[sym_idx].GetAddress() = symbol_addr; 3562 if (symbol_flags) 3563 sym[sym_idx].SetFlags (symbol_flags); 3564 if (symbol_byte_size) 3565 sym[sym_idx].SetByteSize (symbol_byte_size); 3566 ++sym_idx; 3567 } 3568 } 3569 } 3570 } 3571 } 3572 } 3573 3574 // Trim our symbols down to just what we ended up with after 3575 // removing any symbols. 3576 if (sym_idx < num_syms) 3577 { 3578 num_syms = sym_idx; 3579 sym = symtab->Resize (num_syms); 3580 } 3581 3582 // Now synthesize indirect symbols 3583 if (m_dysymtab.nindirectsyms != 0) 3584 { 3585 if (indirect_symbol_index_data.GetByteSize()) 3586 { 3587 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end(); 3588 3589 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx) 3590 { 3591 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs) 3592 { 3593 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2; 3594 if (symbol_stub_byte_size == 0) 3595 continue; 3596 3597 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size; 3598 3599 if (num_symbol_stubs == 0) 3600 continue; 3601 3602 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1; 3603 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) 3604 { 3605 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx; 3606 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size); 3607 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4; 3608 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4)) 3609 { 3610 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset); 3611 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal)) 3612 continue; 3613 3614 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id); 3615 Symbol *stub_symbol = NULL; 3616 if (index_pos != end_index_pos) 3617 { 3618 // We have a remapping from the original nlist index to 3619 // a current symbol index, so just look this up by index 3620 stub_symbol = symtab->SymbolAtIndex (index_pos->second); 3621 } 3622 else 3623 { 3624 // We need to lookup a symbol using the original nlist 3625 // symbol index since this index is coming from the 3626 // S_SYMBOL_STUBS 3627 stub_symbol = symtab->FindSymbolByID (stub_sym_id); 3628 } 3629 3630 if (stub_symbol) 3631 { 3632 Address so_addr(symbol_stub_addr, section_list); 3633 3634 if (stub_symbol->GetType() == eSymbolTypeUndefined) 3635 { 3636 // Change the external symbol into a trampoline that makes sense 3637 // These symbols were N_UNDF N_EXT, and are useless to us, so we 3638 // can re-use them so we don't have to make up a synthetic symbol 3639 // for no good reason. 3640 stub_symbol->SetType (eSymbolTypeTrampoline); 3641 stub_symbol->SetExternal (false); 3642 stub_symbol->GetAddress() = so_addr; 3643 stub_symbol->SetByteSize (symbol_stub_byte_size); 3644 } 3645 else 3646 { 3647 // Make a synthetic symbol to describe the trampoline stub 3648 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled()); 3649 if (sym_idx >= num_syms) 3650 { 3651 sym = symtab->Resize (++num_syms); 3652 stub_symbol = NULL; // this pointer no longer valid 3653 } 3654 sym[sym_idx].SetID (synthetic_sym_id++); 3655 sym[sym_idx].GetMangled() = stub_symbol_mangled_name; 3656 sym[sym_idx].SetType (eSymbolTypeTrampoline); 3657 sym[sym_idx].SetIsSynthetic (true); 3658 sym[sym_idx].GetAddress() = so_addr; 3659 sym[sym_idx].SetByteSize (symbol_stub_byte_size); 3660 ++sym_idx; 3661 } 3662 } 3663 else 3664 { 3665 if (log) 3666 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id); 3667 } 3668 } 3669 } 3670 } 3671 } 3672 } 3673 } 3674 3675 // StreamFile s(stdout, false); 3676 // s.Printf ("Symbol table before CalculateSymbolSizes():\n"); 3677 // symtab->Dump(&s, NULL, eSortOrderNone); 3678 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes 3679 symtab->CalculateSymbolSizes(); 3680 3681 // s.Printf ("Symbol table after CalculateSymbolSizes():\n"); 3682 // symtab->Dump(&s, NULL, eSortOrderNone); 3683 3684 return symtab->GetNumSymbols(); 3685 } 3686 return 0; 3687 } 3688 3689 3690 void 3691 ObjectFileMachO::Dump (Stream *s) 3692 { 3693 ModuleSP module_sp(GetModule()); 3694 if (module_sp) 3695 { 3696 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 3697 s->Printf("%p: ", this); 3698 s->Indent(); 3699 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped) 3700 s->PutCString("ObjectFileMachO64"); 3701 else 3702 s->PutCString("ObjectFileMachO32"); 3703 3704 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); 3705 3706 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n"; 3707 3708 SectionList *sections = GetSectionList(); 3709 if (sections) 3710 sections->Dump(s, NULL, true, UINT32_MAX); 3711 3712 if (m_symtab_ap.get()) 3713 m_symtab_ap->Dump(s, NULL, eSortOrderNone); 3714 } 3715 } 3716 3717 bool 3718 ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header, 3719 const lldb_private::DataExtractor &data, 3720 lldb::offset_t lc_offset, 3721 lldb_private::UUID& uuid) 3722 { 3723 uint32_t i; 3724 struct uuid_command load_cmd; 3725 3726 lldb::offset_t offset = lc_offset; 3727 for (i=0; i<header.ncmds; ++i) 3728 { 3729 const lldb::offset_t cmd_offset = offset; 3730 if (data.GetU32(&offset, &load_cmd, 2) == NULL) 3731 break; 3732 3733 if (load_cmd.cmd == LoadCommandUUID) 3734 { 3735 const uint8_t *uuid_bytes = data.PeekData(offset, 16); 3736 3737 if (uuid_bytes) 3738 { 3739 // OpenCL on Mac OS X uses the same UUID for each of its object files. 3740 // We pretend these object files have no UUID to prevent crashing. 3741 3742 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b, 3743 0x3b, 0xa8, 3744 0x4b, 0x16, 3745 0xb6, 0xa4, 3746 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d }; 3747 3748 if (!memcmp(uuid_bytes, opencl_uuid, 16)) 3749 return false; 3750 3751 uuid.SetBytes (uuid_bytes); 3752 return true; 3753 } 3754 return false; 3755 } 3756 offset = cmd_offset + load_cmd.cmdsize; 3757 } 3758 return false; 3759 } 3760 3761 bool 3762 ObjectFileMachO::GetUUID (lldb_private::UUID* uuid) 3763 { 3764 ModuleSP module_sp(GetModule()); 3765 if (module_sp) 3766 { 3767 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 3768 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 3769 return GetUUID (m_header, m_data, offset, *uuid); 3770 } 3771 return false; 3772 } 3773 3774 3775 uint32_t 3776 ObjectFileMachO::GetDependentModules (FileSpecList& files) 3777 { 3778 uint32_t count = 0; 3779 ModuleSP module_sp(GetModule()); 3780 if (module_sp) 3781 { 3782 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 3783 struct load_command load_cmd; 3784 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 3785 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system 3786 uint32_t i; 3787 for (i=0; i<m_header.ncmds; ++i) 3788 { 3789 const uint32_t cmd_offset = offset; 3790 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 3791 break; 3792 3793 switch (load_cmd.cmd) 3794 { 3795 case LoadCommandDylibLoad: 3796 case LoadCommandDylibLoadWeak: 3797 case LoadCommandDylibReexport: 3798 case LoadCommandDynamicLinkerLoad: 3799 case LoadCommandFixedVMShlibLoad: 3800 case LoadCommandDylibLoadUpward: 3801 { 3802 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset); 3803 const char *path = m_data.PeekCStr(name_offset); 3804 // Skip any path that starts with '@' since these are usually: 3805 // @executable_path/.../file 3806 // @rpath/.../file 3807 if (path && path[0] != '@') 3808 { 3809 FileSpec file_spec(path, resolve_path); 3810 if (files.AppendIfUnique(file_spec)) 3811 count++; 3812 } 3813 } 3814 break; 3815 3816 default: 3817 break; 3818 } 3819 offset = cmd_offset + load_cmd.cmdsize; 3820 } 3821 } 3822 return count; 3823 } 3824 3825 lldb_private::Address 3826 ObjectFileMachO::GetEntryPointAddress () 3827 { 3828 // If the object file is not an executable it can't hold the entry point. m_entry_point_address 3829 // is initialized to an invalid address, so we can just return that. 3830 // If m_entry_point_address is valid it means we've found it already, so return the cached value. 3831 3832 if (!IsExecutable() || m_entry_point_address.IsValid()) 3833 return m_entry_point_address; 3834 3835 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in 3836 // /usr/include/mach-o.h, but it is basically: 3837 // 3838 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state 3839 // uint32_t count - this is the count of longs in the thread state data 3840 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor. 3841 // <repeat this trio> 3842 // 3843 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there. 3844 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers 3845 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin, 3846 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here. 3847 // 3848 // For now we hard-code the offsets and flavors we need: 3849 // 3850 // 3851 3852 ModuleSP module_sp(GetModule()); 3853 if (module_sp) 3854 { 3855 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 3856 struct load_command load_cmd; 3857 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 3858 uint32_t i; 3859 lldb::addr_t start_address = LLDB_INVALID_ADDRESS; 3860 bool done = false; 3861 3862 for (i=0; i<m_header.ncmds; ++i) 3863 { 3864 const lldb::offset_t cmd_offset = offset; 3865 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 3866 break; 3867 3868 switch (load_cmd.cmd) 3869 { 3870 case LoadCommandUnixThread: 3871 case LoadCommandThread: 3872 { 3873 while (offset < cmd_offset + load_cmd.cmdsize) 3874 { 3875 uint32_t flavor = m_data.GetU32(&offset); 3876 uint32_t count = m_data.GetU32(&offset); 3877 if (count == 0) 3878 { 3879 // We've gotten off somehow, log and exit; 3880 return m_entry_point_address; 3881 } 3882 3883 switch (m_header.cputype) 3884 { 3885 case llvm::MachO::CPUTypeARM: 3886 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h 3887 { 3888 offset += 60; // This is the offset of pc in the GPR thread state data structure. 3889 start_address = m_data.GetU32(&offset); 3890 done = true; 3891 } 3892 break; 3893 case llvm::MachO::CPUTypeI386: 3894 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h 3895 { 3896 offset += 40; // This is the offset of eip in the GPR thread state data structure. 3897 start_address = m_data.GetU32(&offset); 3898 done = true; 3899 } 3900 break; 3901 case llvm::MachO::CPUTypeX86_64: 3902 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h 3903 { 3904 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure. 3905 start_address = m_data.GetU64(&offset); 3906 done = true; 3907 } 3908 break; 3909 default: 3910 return m_entry_point_address; 3911 } 3912 // Haven't found the GPR flavor yet, skip over the data for this flavor: 3913 if (done) 3914 break; 3915 offset += count * 4; 3916 } 3917 } 3918 break; 3919 case LoadCommandMain: 3920 { 3921 ConstString text_segment_name ("__TEXT"); 3922 uint64_t entryoffset = m_data.GetU64(&offset); 3923 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name); 3924 if (text_segment_sp) 3925 { 3926 done = true; 3927 start_address = text_segment_sp->GetFileAddress() + entryoffset; 3928 } 3929 } 3930 3931 default: 3932 break; 3933 } 3934 if (done) 3935 break; 3936 3937 // Go to the next load command: 3938 offset = cmd_offset + load_cmd.cmdsize; 3939 } 3940 3941 if (start_address != LLDB_INVALID_ADDRESS) 3942 { 3943 // We got the start address from the load commands, so now resolve that address in the sections 3944 // of this ObjectFile: 3945 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList())) 3946 { 3947 m_entry_point_address.Clear(); 3948 } 3949 } 3950 else 3951 { 3952 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the 3953 // "start" symbol in the main executable. 3954 3955 ModuleSP module_sp (GetModule()); 3956 3957 if (module_sp) 3958 { 3959 SymbolContextList contexts; 3960 SymbolContext context; 3961 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts)) 3962 { 3963 if (contexts.GetContextAtIndex(0, context)) 3964 m_entry_point_address = context.symbol->GetAddress(); 3965 } 3966 } 3967 } 3968 } 3969 3970 return m_entry_point_address; 3971 3972 } 3973 3974 lldb_private::Address 3975 ObjectFileMachO::GetHeaderAddress () 3976 { 3977 lldb_private::Address header_addr; 3978 SectionList *section_list = GetSectionList(); 3979 if (section_list) 3980 { 3981 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT())); 3982 if (text_segment_sp) 3983 { 3984 header_addr.SetSection (text_segment_sp); 3985 header_addr.SetOffset (0); 3986 } 3987 } 3988 return header_addr; 3989 } 3990 3991 uint32_t 3992 ObjectFileMachO::GetNumThreadContexts () 3993 { 3994 ModuleSP module_sp(GetModule()); 3995 if (module_sp) 3996 { 3997 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 3998 if (!m_thread_context_offsets_valid) 3999 { 4000 m_thread_context_offsets_valid = true; 4001 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 4002 FileRangeArray::Entry file_range; 4003 thread_command thread_cmd; 4004 for (uint32_t i=0; i<m_header.ncmds; ++i) 4005 { 4006 const uint32_t cmd_offset = offset; 4007 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL) 4008 break; 4009 4010 if (thread_cmd.cmd == LoadCommandThread) 4011 { 4012 file_range.SetRangeBase (offset); 4013 file_range.SetByteSize (thread_cmd.cmdsize - 8); 4014 m_thread_context_offsets.Append (file_range); 4015 } 4016 offset = cmd_offset + thread_cmd.cmdsize; 4017 } 4018 } 4019 } 4020 return m_thread_context_offsets.GetSize(); 4021 } 4022 4023 lldb::RegisterContextSP 4024 ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread) 4025 { 4026 lldb::RegisterContextSP reg_ctx_sp; 4027 4028 ModuleSP module_sp(GetModule()); 4029 if (module_sp) 4030 { 4031 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 4032 if (!m_thread_context_offsets_valid) 4033 GetNumThreadContexts (); 4034 4035 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx); 4036 if (thread_context_file_range) 4037 { 4038 4039 DataExtractor data (m_data, 4040 thread_context_file_range->GetRangeBase(), 4041 thread_context_file_range->GetByteSize()); 4042 4043 switch (m_header.cputype) 4044 { 4045 case llvm::MachO::CPUTypeARM: 4046 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data)); 4047 break; 4048 4049 case llvm::MachO::CPUTypeI386: 4050 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data)); 4051 break; 4052 4053 case llvm::MachO::CPUTypeX86_64: 4054 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data)); 4055 break; 4056 } 4057 } 4058 } 4059 return reg_ctx_sp; 4060 } 4061 4062 4063 ObjectFile::Type 4064 ObjectFileMachO::CalculateType() 4065 { 4066 switch (m_header.filetype) 4067 { 4068 case HeaderFileTypeObject: // 0x1u MH_OBJECT 4069 if (GetAddressByteSize () == 4) 4070 { 4071 // 32 bit kexts are just object files, but they do have a valid 4072 // UUID load command. 4073 UUID uuid; 4074 if (GetUUID(&uuid)) 4075 { 4076 // this checking for the UUID load command is not enough 4077 // we could eventually look for the symbol named 4078 // "OSKextGetCurrentIdentifier" as this is required of kexts 4079 if (m_strata == eStrataInvalid) 4080 m_strata = eStrataKernel; 4081 return eTypeSharedLibrary; 4082 } 4083 } 4084 return eTypeObjectFile; 4085 4086 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE 4087 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB 4088 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE 4089 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD 4090 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB 4091 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER 4092 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE 4093 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB 4094 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM 4095 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE 4096 default: 4097 break; 4098 } 4099 return eTypeUnknown; 4100 } 4101 4102 ObjectFile::Strata 4103 ObjectFileMachO::CalculateStrata() 4104 { 4105 switch (m_header.filetype) 4106 { 4107 case HeaderFileTypeObject: // 0x1u MH_OBJECT 4108 { 4109 // 32 bit kexts are just object files, but they do have a valid 4110 // UUID load command. 4111 UUID uuid; 4112 if (GetUUID(&uuid)) 4113 { 4114 // this checking for the UUID load command is not enough 4115 // we could eventually look for the symbol named 4116 // "OSKextGetCurrentIdentifier" as this is required of kexts 4117 if (m_type == eTypeInvalid) 4118 m_type = eTypeSharedLibrary; 4119 4120 return eStrataKernel; 4121 } 4122 } 4123 return eStrataUnknown; 4124 4125 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE 4126 // Check for the MH_DYLDLINK bit in the flags 4127 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject) 4128 { 4129 return eStrataUser; 4130 } 4131 else 4132 { 4133 SectionList *section_list = GetSectionList(); 4134 if (section_list) 4135 { 4136 static ConstString g_kld_section_name ("__KLD"); 4137 if (section_list->FindSectionByName(g_kld_section_name)) 4138 return eStrataKernel; 4139 } 4140 } 4141 return eStrataRawImage; 4142 4143 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB 4144 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE 4145 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD 4146 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB 4147 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER 4148 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE 4149 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB 4150 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM 4151 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE 4152 default: 4153 break; 4154 } 4155 return eStrataUnknown; 4156 } 4157 4158 4159 uint32_t 4160 ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions) 4161 { 4162 ModuleSP module_sp(GetModule()); 4163 if (module_sp) 4164 { 4165 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 4166 struct dylib_command load_cmd; 4167 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 4168 uint32_t version_cmd = 0; 4169 uint64_t version = 0; 4170 uint32_t i; 4171 for (i=0; i<m_header.ncmds; ++i) 4172 { 4173 const lldb::offset_t cmd_offset = offset; 4174 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 4175 break; 4176 4177 if (load_cmd.cmd == LoadCommandDylibIdent) 4178 { 4179 if (version_cmd == 0) 4180 { 4181 version_cmd = load_cmd.cmd; 4182 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL) 4183 break; 4184 version = load_cmd.dylib.current_version; 4185 } 4186 break; // Break for now unless there is another more complete version 4187 // number load command in the future. 4188 } 4189 offset = cmd_offset + load_cmd.cmdsize; 4190 } 4191 4192 if (version_cmd == LoadCommandDylibIdent) 4193 { 4194 if (versions != NULL && num_versions > 0) 4195 { 4196 if (num_versions > 0) 4197 versions[0] = (version & 0xFFFF0000ull) >> 16; 4198 if (num_versions > 1) 4199 versions[1] = (version & 0x0000FF00ull) >> 8; 4200 if (num_versions > 2) 4201 versions[2] = (version & 0x000000FFull); 4202 // Fill in an remaining version numbers with invalid values 4203 for (i=3; i<num_versions; ++i) 4204 versions[i] = UINT32_MAX; 4205 } 4206 // The LC_ID_DYLIB load command has a version with 3 version numbers 4207 // in it, so always return 3 4208 return 3; 4209 } 4210 } 4211 return false; 4212 } 4213 4214 bool 4215 ObjectFileMachO::GetArchitecture (ArchSpec &arch) 4216 { 4217 ModuleSP module_sp(GetModule()); 4218 if (module_sp) 4219 { 4220 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 4221 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype); 4222 4223 // Files with type MH_PRELOAD are currently used in cases where the image 4224 // debugs at the addresses in the file itself. Below we set the OS to 4225 // unknown to make sure we use the DynamicLoaderStatic()... 4226 if (m_header.filetype == HeaderFileTypePreloadedExecutable) 4227 { 4228 arch.GetTriple().setOS (llvm::Triple::UnknownOS); 4229 } 4230 return true; 4231 } 4232 return false; 4233 } 4234 4235 4236 UUID 4237 ObjectFileMachO::GetProcessSharedCacheUUID (Process *process) 4238 { 4239 UUID uuid; 4240 if (process) 4241 { 4242 addr_t all_image_infos = process->GetImageInfoAddress(); 4243 4244 // The address returned by GetImageInfoAddress may be the address of dyld (don't want) 4245 // or it may be the address of the dyld_all_image_infos structure (want). The first four 4246 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant. 4247 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field. 4248 4249 Error err; 4250 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err); 4251 if (version_or_magic != -1 4252 && version_or_magic != HeaderMagic32 4253 && version_or_magic != HeaderMagic32Swapped 4254 && version_or_magic != HeaderMagic64 4255 && version_or_magic != HeaderMagic64Swapped 4256 && version_or_magic >= 13) 4257 { 4258 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS; 4259 int wordsize = process->GetAddressByteSize(); 4260 if (wordsize == 8) 4261 { 4262 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h> 4263 } 4264 if (wordsize == 4) 4265 { 4266 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h> 4267 } 4268 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS) 4269 { 4270 uuid_t shared_cache_uuid; 4271 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t)) 4272 { 4273 uuid.SetBytes (shared_cache_uuid); 4274 } 4275 } 4276 } 4277 } 4278 return uuid; 4279 } 4280 4281 UUID 4282 ObjectFileMachO::GetLLDBSharedCacheUUID () 4283 { 4284 UUID uuid; 4285 #if defined (__APPLE__) && defined (__arm__) 4286 uint8_t *(*dyld_get_all_image_infos)(void); 4287 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos"); 4288 if (dyld_get_all_image_infos) 4289 { 4290 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos(); 4291 if (dyld_all_image_infos_address) 4292 { 4293 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h> 4294 if (*version >= 13) 4295 { 4296 uuid_t *sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h> 4297 uuid.SetBytes (sharedCacheUUID_address); 4298 } 4299 } 4300 } 4301 #endif 4302 return uuid; 4303 } 4304 4305 4306 //------------------------------------------------------------------ 4307 // PluginInterface protocol 4308 //------------------------------------------------------------------ 4309 lldb_private::ConstString 4310 ObjectFileMachO::GetPluginName() 4311 { 4312 return GetPluginNameStatic(); 4313 } 4314 4315 uint32_t 4316 ObjectFileMachO::GetPluginVersion() 4317 { 4318 return 1; 4319 } 4320 4321