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