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