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