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/Support/MachO.h" 11 12 #include "ObjectFileMachO.h" 13 14 #include "lldb/Core/ArchSpec.h" 15 #include "lldb/Core/DataBuffer.h" 16 #include "lldb/Host/FileSpec.h" 17 #include "lldb/Core/FileSpecList.h" 18 #include "lldb/Core/Module.h" 19 #include "lldb/Core/PluginManager.h" 20 #include "lldb/Core/Section.h" 21 #include "lldb/Core/StreamFile.h" 22 #include "lldb/Core/StreamString.h" 23 #include "lldb/Core/Timer.h" 24 #include "lldb/Core/UUID.h" 25 #include "lldb/Symbol/ObjectFile.h" 26 27 28 using namespace lldb; 29 using namespace lldb_private; 30 using namespace llvm::MachO; 31 32 #define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008 33 34 void 35 ObjectFileMachO::Initialize() 36 { 37 PluginManager::RegisterPlugin (GetPluginNameStatic(), 38 GetPluginDescriptionStatic(), 39 CreateInstance); 40 } 41 42 void 43 ObjectFileMachO::Terminate() 44 { 45 PluginManager::UnregisterPlugin (CreateInstance); 46 } 47 48 49 const char * 50 ObjectFileMachO::GetPluginNameStatic() 51 { 52 return "object-file.mach-o"; 53 } 54 55 const char * 56 ObjectFileMachO::GetPluginDescriptionStatic() 57 { 58 return "Mach-o object file reader (32 and 64 bit)"; 59 } 60 61 62 ObjectFile * 63 ObjectFileMachO::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) 64 { 65 if (ObjectFileMachO::MagicBytesMatch(dataSP)) 66 { 67 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, dataSP, file, offset, length)); 68 if (objfile_ap.get() && objfile_ap->ParseHeader()) 69 return objfile_ap.release(); 70 } 71 return NULL; 72 } 73 74 75 static uint32_t 76 MachHeaderSizeFromMagic(uint32_t magic) 77 { 78 switch (magic) 79 { 80 case HeaderMagic32: 81 case HeaderMagic32Swapped: 82 return sizeof(struct mach_header); 83 84 case HeaderMagic64: 85 case HeaderMagic64Swapped: 86 return sizeof(struct mach_header_64); 87 break; 88 89 default: 90 break; 91 } 92 return 0; 93 } 94 95 96 bool 97 ObjectFileMachO::MagicBytesMatch (DataBufferSP& dataSP) 98 { 99 DataExtractor data(dataSP, lldb::endian::InlHostByteOrder(), 4); 100 uint32_t offset = 0; 101 uint32_t magic = data.GetU32(&offset); 102 return MachHeaderSizeFromMagic(magic) != 0; 103 } 104 105 106 ObjectFileMachO::ObjectFileMachO(Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) : 107 ObjectFile(module, file, offset, length, dataSP), 108 m_mutex (Mutex::eMutexTypeRecursive), 109 m_header(), 110 m_sections_ap(), 111 m_symtab_ap(), 112 m_entry_point_address () 113 { 114 ::memset (&m_header, 0, sizeof(m_header)); 115 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab)); 116 } 117 118 119 ObjectFileMachO::~ObjectFileMachO() 120 { 121 } 122 123 124 bool 125 ObjectFileMachO::ParseHeader () 126 { 127 lldb_private::Mutex::Locker locker(m_mutex); 128 bool can_parse = false; 129 uint32_t offset = 0; 130 m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); 131 // Leave magic in the original byte order 132 m_header.magic = m_data.GetU32(&offset); 133 switch (m_header.magic) 134 { 135 case HeaderMagic32: 136 m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); 137 m_data.SetAddressByteSize(4); 138 can_parse = true; 139 break; 140 141 case HeaderMagic64: 142 m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); 143 m_data.SetAddressByteSize(8); 144 can_parse = true; 145 break; 146 147 case HeaderMagic32Swapped: 148 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); 149 m_data.SetAddressByteSize(4); 150 can_parse = true; 151 break; 152 153 case HeaderMagic64Swapped: 154 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); 155 m_data.SetAddressByteSize(8); 156 can_parse = true; 157 break; 158 159 default: 160 break; 161 } 162 163 if (can_parse) 164 { 165 m_data.GetU32(&offset, &m_header.cputype, 6); 166 167 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); 168 169 if (SetModulesArchitecture (mach_arch)) 170 { 171 // Read in all only the load command data 172 DataBufferSP data_sp(m_file.ReadFileContents(m_offset, m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic))); 173 m_data.SetData (data_sp); 174 return true; 175 } 176 } 177 else 178 { 179 memset(&m_header, 0, sizeof(struct mach_header)); 180 } 181 return false; 182 } 183 184 185 ByteOrder 186 ObjectFileMachO::GetByteOrder () const 187 { 188 lldb_private::Mutex::Locker locker(m_mutex); 189 return m_data.GetByteOrder (); 190 } 191 192 bool 193 ObjectFileMachO::IsExecutable() const 194 { 195 return m_header.filetype == HeaderFileTypeExecutable; 196 } 197 198 size_t 199 ObjectFileMachO::GetAddressByteSize () const 200 { 201 lldb_private::Mutex::Locker locker(m_mutex); 202 return m_data.GetAddressByteSize (); 203 } 204 205 AddressClass 206 ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr) 207 { 208 Symtab *symtab = GetSymtab(); 209 if (symtab) 210 { 211 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); 212 if (symbol) 213 { 214 const AddressRange *range_ptr = symbol->GetAddressRangePtr(); 215 if (range_ptr) 216 { 217 const Section *section = range_ptr->GetBaseAddress().GetSection(); 218 if (section) 219 { 220 const SectionType section_type = section->GetType(); 221 switch (section_type) 222 { 223 case eSectionTypeInvalid: return eAddressClassUnknown; 224 case eSectionTypeCode: 225 if (m_header.cputype == llvm::MachO::CPUTypeARM) 226 { 227 // For ARM we have a bit in the n_desc field of the symbol 228 // that tells us ARM/Thumb which is bit 0x0008. 229 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB) 230 return eAddressClassCodeAlternateISA; 231 } 232 return eAddressClassCode; 233 234 case eSectionTypeContainer: return eAddressClassUnknown; 235 case eSectionTypeData: return eAddressClassData; 236 case eSectionTypeDataCString: return eAddressClassData; 237 case eSectionTypeDataCStringPointers: return eAddressClassData; 238 case eSectionTypeDataSymbolAddress: return eAddressClassData; 239 case eSectionTypeData4: return eAddressClassData; 240 case eSectionTypeData8: return eAddressClassData; 241 case eSectionTypeData16: return eAddressClassData; 242 case eSectionTypeDataPointers: return eAddressClassData; 243 case eSectionTypeZeroFill: return eAddressClassData; 244 case eSectionTypeDataObjCMessageRefs: return eAddressClassData; 245 case eSectionTypeDataObjCCFStrings: return eAddressClassData; 246 case eSectionTypeDebug: return eAddressClassDebug; 247 case eSectionTypeDWARFDebugAbbrev: return eAddressClassDebug; 248 case eSectionTypeDWARFDebugAranges: return eAddressClassDebug; 249 case eSectionTypeDWARFDebugFrame: return eAddressClassDebug; 250 case eSectionTypeDWARFDebugInfo: return eAddressClassDebug; 251 case eSectionTypeDWARFDebugLine: return eAddressClassDebug; 252 case eSectionTypeDWARFDebugLoc: return eAddressClassDebug; 253 case eSectionTypeDWARFDebugMacInfo: return eAddressClassDebug; 254 case eSectionTypeDWARFDebugPubNames: return eAddressClassDebug; 255 case eSectionTypeDWARFDebugPubTypes: return eAddressClassDebug; 256 case eSectionTypeDWARFDebugRanges: return eAddressClassDebug; 257 case eSectionTypeDWARFDebugStr: return eAddressClassDebug; 258 case eSectionTypeDWARFDebugNames: return eAddressClassDebug; 259 case eSectionTypeDWARFDebugTypes: return eAddressClassDebug; 260 case eSectionTypeEHFrame: return eAddressClassRuntime; 261 case eSectionTypeOther: return eAddressClassUnknown; 262 } 263 } 264 } 265 266 const SymbolType symbol_type = symbol->GetType(); 267 switch (symbol_type) 268 { 269 case eSymbolTypeAny: return eAddressClassUnknown; 270 case eSymbolTypeAbsolute: return eAddressClassUnknown; 271 case eSymbolTypeExtern: return eAddressClassUnknown; 272 273 case eSymbolTypeCode: 274 case eSymbolTypeTrampoline: 275 if (m_header.cputype == llvm::MachO::CPUTypeARM) 276 { 277 // For ARM we have a bit in the n_desc field of the symbol 278 // that tells us ARM/Thumb which is bit 0x0008. 279 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB) 280 return eAddressClassCodeAlternateISA; 281 } 282 return eAddressClassCode; 283 284 case eSymbolTypeData: return eAddressClassData; 285 case eSymbolTypeRuntime: return eAddressClassRuntime; 286 case eSymbolTypeException: return eAddressClassRuntime; 287 case eSymbolTypeSourceFile: return eAddressClassDebug; 288 case eSymbolTypeHeaderFile: return eAddressClassDebug; 289 case eSymbolTypeObjectFile: return eAddressClassDebug; 290 case eSymbolTypeCommonBlock: return eAddressClassDebug; 291 case eSymbolTypeBlock: return eAddressClassDebug; 292 case eSymbolTypeLocal: return eAddressClassData; 293 case eSymbolTypeParam: return eAddressClassData; 294 case eSymbolTypeVariable: return eAddressClassData; 295 case eSymbolTypeVariableType: return eAddressClassDebug; 296 case eSymbolTypeLineEntry: return eAddressClassDebug; 297 case eSymbolTypeLineHeader: return eAddressClassDebug; 298 case eSymbolTypeScopeBegin: return eAddressClassDebug; 299 case eSymbolTypeScopeEnd: return eAddressClassDebug; 300 case eSymbolTypeAdditional: return eAddressClassUnknown; 301 case eSymbolTypeCompiler: return eAddressClassDebug; 302 case eSymbolTypeInstrumentation:return eAddressClassDebug; 303 case eSymbolTypeUndefined: return eAddressClassUnknown; 304 } 305 } 306 } 307 return eAddressClassUnknown; 308 } 309 310 Symtab * 311 ObjectFileMachO::GetSymtab() 312 { 313 lldb_private::Mutex::Locker symfile_locker(m_mutex); 314 if (m_symtab_ap.get() == NULL) 315 { 316 m_symtab_ap.reset(new Symtab(this)); 317 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex()); 318 ParseSymtab (true); 319 } 320 return m_symtab_ap.get(); 321 } 322 323 324 SectionList * 325 ObjectFileMachO::GetSectionList() 326 { 327 lldb_private::Mutex::Locker locker(m_mutex); 328 if (m_sections_ap.get() == NULL) 329 { 330 m_sections_ap.reset(new SectionList()); 331 ParseSections(); 332 } 333 return m_sections_ap.get(); 334 } 335 336 337 size_t 338 ObjectFileMachO::ParseSections () 339 { 340 lldb::user_id_t segID = 0; 341 lldb::user_id_t sectID = 0; 342 struct segment_command_64 load_cmd; 343 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 344 uint32_t i; 345 //bool dump_sections = false; 346 for (i=0; i<m_header.ncmds; ++i) 347 { 348 const uint32_t load_cmd_offset = offset; 349 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 350 break; 351 352 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64) 353 { 354 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16)) 355 { 356 load_cmd.vmaddr = m_data.GetAddress(&offset); 357 load_cmd.vmsize = m_data.GetAddress(&offset); 358 load_cmd.fileoff = m_data.GetAddress(&offset); 359 load_cmd.filesize = m_data.GetAddress(&offset); 360 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4)) 361 { 362 363 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0; 364 365 // Keep a list of mach segments around in case we need to 366 // get at data that isn't stored in the abstracted Sections. 367 m_mach_segments.push_back (load_cmd); 368 369 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname))); 370 // Use a segment ID of the segment index shifted left by 8 so they 371 // never conflict with any of the sections. 372 SectionSP segment_sp; 373 if (segment_name) 374 { 375 segment_sp.reset(new Section (NULL, 376 GetModule(), // Module to which this section belongs 377 ++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 378 segment_name, // Name of this section 379 eSectionTypeContainer, // This section is a container of other sections. 380 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file 381 load_cmd.vmsize, // VM size in bytes of this section 382 load_cmd.fileoff, // Offset to the data for this section in the file 383 load_cmd.filesize, // Size in bytes of this section as found in the the file 384 load_cmd.flags)); // Flags for this section 385 386 segment_sp->SetIsEncrypted (segment_is_encrypted); 387 m_sections_ap->AddSection(segment_sp); 388 } 389 390 struct section_64 sect64; 391 ::memset (§64, 0, sizeof(sect64)); 392 // Push a section into our mach sections for the section at 393 // index zero (NListSectionNoSection) if we don't have any 394 // mach sections yet... 395 if (m_mach_sections.empty()) 396 m_mach_sections.push_back(sect64); 397 uint32_t segment_sect_idx; 398 const lldb::user_id_t first_segment_sectID = sectID + 1; 399 400 401 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8; 402 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx) 403 { 404 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL) 405 break; 406 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL) 407 break; 408 sect64.addr = m_data.GetAddress(&offset); 409 sect64.size = m_data.GetAddress(&offset); 410 411 if (m_data.GetU32(&offset, §64.offset, num_u32s) == NULL) 412 break; 413 414 // Keep a list of mach sections around in case we need to 415 // get at data that isn't stored in the abstracted Sections. 416 m_mach_sections.push_back (sect64); 417 418 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname))); 419 if (!segment_name) 420 { 421 // We have a segment with no name so we need to conjure up 422 // segments that correspond to the section's segname if there 423 // isn't already such a section. If there is such a section, 424 // we resize the section so that it spans all sections. 425 // We also mark these sections as fake so address matches don't 426 // hit if they land in the gaps between the child sections. 427 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname)); 428 segment_sp = m_sections_ap->FindSectionByName (segment_name); 429 if (segment_sp.get()) 430 { 431 Section *segment = segment_sp.get(); 432 // Grow the section size as needed. 433 const lldb::addr_t sect64_min_addr = sect64.addr; 434 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size; 435 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize(); 436 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress(); 437 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size; 438 if (sect64_min_addr >= curr_seg_min_addr) 439 { 440 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr; 441 // Only grow the section size if needed 442 if (new_seg_byte_size > curr_seg_byte_size) 443 segment->SetByteSize (new_seg_byte_size); 444 } 445 else 446 { 447 // We need to change the base address of the segment and 448 // adjust the child section offsets for all existing children. 449 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr; 450 segment->Slide(slide_amount, false); 451 segment->GetChildren().Slide (-slide_amount, false); 452 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr); 453 } 454 455 // Grow the section size as needed. 456 if (sect64.offset) 457 { 458 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset(); 459 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize(); 460 461 const lldb::addr_t section_min_file_offset = sect64.offset; 462 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size; 463 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset); 464 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset; 465 segment->SetFileOffset (new_file_offset); 466 segment->SetFileSize (new_file_size); 467 } 468 } 469 else 470 { 471 // Create a fake section for the section's named segment 472 segment_sp.reset(new Section(segment_sp.get(), // Parent section 473 GetModule(), // Module to which this section belongs 474 ++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 475 segment_name, // Name of this section 476 eSectionTypeContainer, // This section is a container of other sections. 477 sect64.addr, // File VM address == addresses as they are found in the object file 478 sect64.size, // VM size in bytes of this section 479 sect64.offset, // Offset to the data for this section in the file 480 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file 481 load_cmd.flags)); // Flags for this section 482 segment_sp->SetIsFake(true); 483 m_sections_ap->AddSection(segment_sp); 484 segment_sp->SetIsEncrypted (segment_is_encrypted); 485 } 486 } 487 assert (segment_sp.get()); 488 489 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType; 490 static ConstString g_sect_name_objc_data ("__objc_data"); 491 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs"); 492 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs"); 493 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs"); 494 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs"); 495 static ConstString g_sect_name_objc_const ("__objc_const"); 496 static ConstString g_sect_name_objc_classlist ("__objc_classlist"); 497 static ConstString g_sect_name_cfstring ("__cfstring"); 498 499 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev"); 500 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges"); 501 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame"); 502 static ConstString g_sect_name_dwarf_debug_info ("__debug_info"); 503 static ConstString g_sect_name_dwarf_debug_line ("__debug_line"); 504 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc"); 505 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo"); 506 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames"); 507 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes"); 508 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges"); 509 static ConstString g_sect_name_dwarf_debug_str ("__debug_str"); 510 static ConstString g_sect_name_dwarf_debug_names ("__debug_names"); 511 static ConstString g_sect_name_dwarf_debug_types ("__debug_types"); 512 static ConstString g_sect_name_eh_frame ("__eh_frame"); 513 static ConstString g_sect_name_DATA ("__DATA"); 514 static ConstString g_sect_name_TEXT ("__TEXT"); 515 516 SectionType sect_type = eSectionTypeOther; 517 518 if (section_name == g_sect_name_dwarf_debug_abbrev) 519 sect_type = eSectionTypeDWARFDebugAbbrev; 520 else if (section_name == g_sect_name_dwarf_debug_aranges) 521 sect_type = eSectionTypeDWARFDebugAranges; 522 else if (section_name == g_sect_name_dwarf_debug_frame) 523 sect_type = eSectionTypeDWARFDebugFrame; 524 else if (section_name == g_sect_name_dwarf_debug_info) 525 sect_type = eSectionTypeDWARFDebugInfo; 526 else if (section_name == g_sect_name_dwarf_debug_line) 527 sect_type = eSectionTypeDWARFDebugLine; 528 else if (section_name == g_sect_name_dwarf_debug_loc) 529 sect_type = eSectionTypeDWARFDebugLoc; 530 else if (section_name == g_sect_name_dwarf_debug_macinfo) 531 sect_type = eSectionTypeDWARFDebugMacInfo; 532 else if (section_name == g_sect_name_dwarf_debug_pubnames) 533 sect_type = eSectionTypeDWARFDebugPubNames; 534 else if (section_name == g_sect_name_dwarf_debug_pubtypes) 535 sect_type = eSectionTypeDWARFDebugPubTypes; 536 else if (section_name == g_sect_name_dwarf_debug_ranges) 537 sect_type = eSectionTypeDWARFDebugRanges; 538 else if (section_name == g_sect_name_dwarf_debug_str) 539 sect_type = eSectionTypeDWARFDebugStr; 540 else if (section_name == g_sect_name_dwarf_debug_names) 541 sect_type = eSectionTypeDWARFDebugNames; 542 else if (section_name == g_sect_name_dwarf_debug_types) 543 sect_type = eSectionTypeDWARFDebugTypes; 544 else if (section_name == g_sect_name_objc_selrefs) 545 sect_type = eSectionTypeDataCStringPointers; 546 else if (section_name == g_sect_name_objc_msgrefs) 547 sect_type = eSectionTypeDataObjCMessageRefs; 548 else if (section_name == g_sect_name_eh_frame) 549 sect_type = eSectionTypeEHFrame; 550 else if (section_name == g_sect_name_cfstring) 551 sect_type = eSectionTypeDataObjCCFStrings; 552 else if (section_name == g_sect_name_objc_data || 553 section_name == g_sect_name_objc_classrefs || 554 section_name == g_sect_name_objc_superrefs || 555 section_name == g_sect_name_objc_const || 556 section_name == g_sect_name_objc_classlist) 557 { 558 sect_type = eSectionTypeDataPointers; 559 } 560 561 if (sect_type == eSectionTypeOther) 562 { 563 switch (mach_sect_type) 564 { 565 // TODO: categorize sections by other flags for regular sections 566 case SectionTypeRegular: 567 if (segment_sp->GetName() == g_sect_name_TEXT) 568 sect_type = eSectionTypeCode; 569 else if (segment_sp->GetName() == g_sect_name_DATA) 570 sect_type = eSectionTypeData; 571 else 572 sect_type = eSectionTypeOther; 573 break; 574 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break; 575 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings 576 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals 577 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals 578 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals 579 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers 580 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers 581 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field 582 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization 583 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination 584 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break; 585 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break; 586 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing 587 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals 588 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break; 589 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break; 590 default: break; 591 } 592 } 593 594 SectionSP section_sp(new Section(segment_sp.get(), 595 GetModule(), 596 ++sectID, 597 section_name, 598 sect_type, 599 sect64.addr - segment_sp->GetFileAddress(), 600 sect64.size, 601 sect64.offset, 602 sect64.offset == 0 ? 0 : sect64.size, 603 sect64.flags)); 604 // Set the section to be encrypted to match the segment 605 section_sp->SetIsEncrypted (segment_is_encrypted); 606 607 segment_sp->GetChildren().AddSection(section_sp); 608 609 if (segment_sp->IsFake()) 610 { 611 segment_sp.reset(); 612 segment_name.Clear(); 613 } 614 } 615 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM) 616 { 617 if (first_segment_sectID <= sectID) 618 { 619 lldb::user_id_t sect_uid; 620 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid) 621 { 622 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid)); 623 SectionSP next_section_sp; 624 if (sect_uid + 1 <= sectID) 625 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1); 626 627 if (curr_section_sp.get()) 628 { 629 if (curr_section_sp->GetByteSize() == 0) 630 { 631 if (next_section_sp.get() != NULL) 632 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() ); 633 else 634 curr_section_sp->SetByteSize ( load_cmd.vmsize ); 635 } 636 } 637 } 638 } 639 } 640 } 641 } 642 } 643 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo) 644 { 645 m_dysymtab.cmd = load_cmd.cmd; 646 m_dysymtab.cmdsize = load_cmd.cmdsize; 647 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2); 648 } 649 650 offset = load_cmd_offset + load_cmd.cmdsize; 651 } 652 // if (dump_sections) 653 // { 654 // StreamFile s(stdout); 655 // m_sections_ap->Dump(&s, true); 656 // } 657 return sectID; // Return the number of sections we registered with the module 658 } 659 660 class MachSymtabSectionInfo 661 { 662 public: 663 664 MachSymtabSectionInfo (SectionList *section_list) : 665 m_section_list (section_list), 666 m_section_infos() 667 { 668 // Get the number of sections down to a depth of 1 to include 669 // all segments and their sections, but no other sections that 670 // may be added for debug map or 671 m_section_infos.resize(section_list->GetNumSections(1)); 672 } 673 674 675 Section * 676 GetSection (uint8_t n_sect, addr_t file_addr) 677 { 678 if (n_sect == 0) 679 return NULL; 680 if (n_sect < m_section_infos.size()) 681 { 682 if (m_section_infos[n_sect].section == NULL) 683 { 684 Section *section = m_section_list->FindSectionByID (n_sect).get(); 685 m_section_infos[n_sect].section = section; 686 if (section != NULL) 687 { 688 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress()); 689 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize()); 690 } 691 else 692 { 693 fprintf (stderr, "error: unable to find section for section %u\n", n_sect); 694 } 695 } 696 if (m_section_infos[n_sect].vm_range.Contains(file_addr)) 697 { 698 // Symbol is in section. 699 return m_section_infos[n_sect].section; 700 } 701 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 && 702 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr) 703 { 704 // Symbol is in section with zero size, but has the same start 705 // address as the section. This can happen with linker symbols 706 // (symbols that start with the letter 'l' or 'L'. 707 return m_section_infos[n_sect].section; 708 } 709 } 710 return m_section_list->FindSectionContainingFileAddress(file_addr).get(); 711 } 712 713 protected: 714 struct SectionInfo 715 { 716 SectionInfo () : 717 vm_range(), 718 section (NULL) 719 { 720 } 721 722 VMRange vm_range; 723 Section *section; 724 }; 725 SectionList *m_section_list; 726 std::vector<SectionInfo> m_section_infos; 727 }; 728 729 730 731 size_t 732 ObjectFileMachO::ParseSymtab (bool minimize) 733 { 734 Timer scoped_timer(__PRETTY_FUNCTION__, 735 "ObjectFileMachO::ParseSymtab () module = %s", 736 m_file.GetFilename().AsCString("")); 737 struct symtab_command symtab_load_command; 738 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 739 uint32_t i; 740 for (i=0; i<m_header.ncmds; ++i) 741 { 742 const uint32_t cmd_offset = offset; 743 // Read in the load command and load command size 744 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL) 745 break; 746 // Watch for the symbol table load command 747 if (symtab_load_command.cmd == LoadCommandSymtab) 748 { 749 // Read in the rest of the symtab load command 750 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields 751 { 752 Symtab *symtab = m_symtab_ap.get(); 753 SectionList *section_list = GetSectionList(); 754 assert(section_list); 755 const size_t addr_size = m_data.GetAddressByteSize(); 756 const ByteOrder endian = m_data.GetByteOrder(); 757 bool bit_width_32 = addr_size == 4; 758 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64); 759 760 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size)); 761 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize)); 762 763 const char *strtab_data = (const char *)strtab_data_sp->GetBytes(); 764 // DataExtractor symtab_data(symtab_data_sp, endian, addr_size); 765 // DataExtractor strtab_data(strtab_data_sp, endian, addr_size); 766 767 static ConstString g_segment_name_TEXT ("__TEXT"); 768 static ConstString g_segment_name_DATA ("__DATA"); 769 static ConstString g_segment_name_OBJC ("__OBJC"); 770 static ConstString g_section_name_eh_frame ("__eh_frame"); 771 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT)); 772 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA)); 773 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC)); 774 SectionSP eh_frame_section_sp; 775 if (text_section_sp.get()) 776 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame); 777 else 778 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame); 779 780 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection; 781 //uint32_t symtab_offset = 0; 782 const uint8_t* nlist_data = symtab_data_sp->GetBytes(); 783 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms); 784 785 786 if (endian != lldb::endian::InlHostByteOrder()) 787 { 788 // ... 789 assert (!"UNIMPLEMENTED: Swap all nlist entries"); 790 } 791 uint32_t N_SO_index = UINT32_MAX; 792 793 MachSymtabSectionInfo section_info (section_list); 794 std::vector<uint32_t> N_FUN_indexes; 795 std::vector<uint32_t> N_NSYM_indexes; 796 std::vector<uint32_t> N_INCL_indexes; 797 std::vector<uint32_t> N_BRAC_indexes; 798 std::vector<uint32_t> N_COMM_indexes; 799 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap; 800 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap; 801 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx; 802 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx; 803 // Any symbols that get merged into another will get an entry 804 // in this map so we know 805 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx; 806 uint32_t nlist_idx = 0; 807 Symbol *symbol_ptr = NULL; 808 809 uint32_t sym_idx = 0; 810 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); 811 uint32_t num_syms = symtab->GetNumSymbols(); 812 813 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); 814 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) 815 { 816 struct nlist_64 nlist; 817 if (bit_width_32) 818 { 819 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size)); 820 nlist.n_strx = nlist32_ptr->n_strx; 821 nlist.n_type = nlist32_ptr->n_type; 822 nlist.n_sect = nlist32_ptr->n_sect; 823 nlist.n_desc = nlist32_ptr->n_desc; 824 nlist.n_value = nlist32_ptr->n_value; 825 } 826 else 827 { 828 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size))); 829 } 830 831 SymbolType type = eSymbolTypeInvalid; 832 const char* symbol_name = &strtab_data[nlist.n_strx]; 833 if (symbol_name[0] == '\0') 834 symbol_name = NULL; 835 Section* symbol_section = NULL; 836 bool add_nlist = true; 837 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0); 838 839 assert (sym_idx < num_syms); 840 841 sym[sym_idx].SetDebug (is_debug); 842 843 if (is_debug) 844 { 845 switch (nlist.n_type) 846 { 847 case StabGlobalSymbol: 848 // N_GSYM -- global symbol: name,,NO_SECT,type,0 849 // Sometimes the N_GSYM value contains the address. 850 sym[sym_idx].SetExternal(true); 851 if (nlist.n_value != 0) 852 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 853 type = eSymbolTypeData; 854 break; 855 856 case StabFunctionName: 857 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0 858 type = eSymbolTypeCompiler; 859 break; 860 861 case StabFunction: 862 // N_FUN -- procedure: name,,n_sect,linenumber,address 863 if (symbol_name) 864 { 865 type = eSymbolTypeCode; 866 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 867 868 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx; 869 // We use the current number of symbols in the symbol table in lieu of 870 // using nlist_idx in case we ever start trimming entries out 871 N_FUN_indexes.push_back(sym_idx); 872 } 873 else 874 { 875 type = eSymbolTypeCompiler; 876 877 if ( !N_FUN_indexes.empty() ) 878 { 879 // Copy the size of the function into the original STAB entry so we don't have 880 // to hunt for it later 881 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value); 882 N_FUN_indexes.pop_back(); 883 // We don't really need the end function STAB as it contains the size which 884 // we already placed with the original symbol, so don't add it if we want a 885 // minimal symbol table 886 if (minimize) 887 add_nlist = false; 888 } 889 } 890 break; 891 892 case StabStaticSymbol: 893 // N_STSYM -- static symbol: name,,n_sect,type,address 894 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx; 895 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 896 type = eSymbolTypeData; 897 break; 898 899 case StabLocalCommon: 900 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address 901 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 902 type = eSymbolTypeCommonBlock; 903 break; 904 905 case StabBeginSymbol: 906 // N_BNSYM 907 // We use the current number of symbols in the symbol table in lieu of 908 // using nlist_idx in case we ever start trimming entries out 909 if (minimize) 910 { 911 // Skip these if we want minimal symbol tables 912 add_nlist = false; 913 } 914 else 915 { 916 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 917 N_NSYM_indexes.push_back(sym_idx); 918 type = eSymbolTypeScopeBegin; 919 } 920 break; 921 922 case StabEndSymbol: 923 // N_ENSYM 924 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM 925 // so that we can always skip the entire symbol if we need to navigate 926 // more quickly at the source level when parsing STABS 927 if (minimize) 928 { 929 // Skip these if we want minimal symbol tables 930 add_nlist = false; 931 } 932 else 933 { 934 if ( !N_NSYM_indexes.empty() ) 935 { 936 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back()); 937 symbol_ptr->SetByteSize(sym_idx + 1); 938 symbol_ptr->SetSizeIsSibling(true); 939 N_NSYM_indexes.pop_back(); 940 } 941 type = eSymbolTypeScopeEnd; 942 } 943 break; 944 945 946 case StabSourceFileOptions: 947 // N_OPT - emitted with gcc2_compiled and in gcc source 948 type = eSymbolTypeCompiler; 949 break; 950 951 case StabRegisterSymbol: 952 // N_RSYM - register sym: name,,NO_SECT,type,register 953 type = eSymbolTypeVariable; 954 break; 955 956 case StabSourceLine: 957 // N_SLINE - src line: 0,,n_sect,linenumber,address 958 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 959 type = eSymbolTypeLineEntry; 960 break; 961 962 case StabStructureType: 963 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset 964 type = eSymbolTypeVariableType; 965 break; 966 967 case StabSourceFileName: 968 // N_SO - source file name 969 type = eSymbolTypeSourceFile; 970 if (symbol_name == NULL) 971 { 972 if (minimize) 973 add_nlist = false; 974 if (N_SO_index != UINT32_MAX) 975 { 976 // Set the size of the N_SO to the terminating index of this N_SO 977 // so that we can always skip the entire N_SO if we need to navigate 978 // more quickly at the source level when parsing STABS 979 symbol_ptr = symtab->SymbolAtIndex(N_SO_index); 980 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1)); 981 symbol_ptr->SetSizeIsSibling(true); 982 } 983 N_NSYM_indexes.clear(); 984 N_INCL_indexes.clear(); 985 N_BRAC_indexes.clear(); 986 N_COMM_indexes.clear(); 987 N_FUN_indexes.clear(); 988 N_SO_index = UINT32_MAX; 989 } 990 else 991 { 992 // We use the current number of symbols in the symbol table in lieu of 993 // using nlist_idx in case we ever start trimming entries out 994 if (symbol_name[0] == '/') 995 N_SO_index = sym_idx; 996 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) 997 { 998 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString(); 999 if (so_path && so_path[0]) 1000 { 1001 std::string full_so_path (so_path); 1002 if (*full_so_path.rbegin() != '/') 1003 full_so_path += '/'; 1004 full_so_path += symbol_name; 1005 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false); 1006 add_nlist = false; 1007 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; 1008 } 1009 } 1010 } 1011 1012 break; 1013 1014 case StabObjectFileName: 1015 // N_OSO - object file name: name,,0,0,st_mtime 1016 type = eSymbolTypeObjectFile; 1017 break; 1018 1019 case StabLocalSymbol: 1020 // N_LSYM - local sym: name,,NO_SECT,type,offset 1021 type = eSymbolTypeLocal; 1022 break; 1023 1024 //---------------------------------------------------------------------- 1025 // INCL scopes 1026 //---------------------------------------------------------------------- 1027 case StabBeginIncludeFileName: 1028 // N_BINCL - include file beginning: name,,NO_SECT,0,sum 1029 // We use the current number of symbols in the symbol table in lieu of 1030 // using nlist_idx in case we ever start trimming entries out 1031 N_INCL_indexes.push_back(sym_idx); 1032 type = eSymbolTypeScopeBegin; 1033 break; 1034 1035 case StabEndIncludeFile: 1036 // N_EINCL - include file end: name,,NO_SECT,0,0 1037 // Set the size of the N_BINCL to the terminating index of this N_EINCL 1038 // so that we can always skip the entire symbol if we need to navigate 1039 // more quickly at the source level when parsing STABS 1040 if ( !N_INCL_indexes.empty() ) 1041 { 1042 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back()); 1043 symbol_ptr->SetByteSize(sym_idx + 1); 1044 symbol_ptr->SetSizeIsSibling(true); 1045 N_INCL_indexes.pop_back(); 1046 } 1047 type = eSymbolTypeScopeEnd; 1048 break; 1049 1050 case StabIncludeFileName: 1051 // N_SOL - #included file name: name,,n_sect,0,address 1052 type = eSymbolTypeHeaderFile; 1053 1054 // We currently don't use the header files on darwin 1055 if (minimize) 1056 add_nlist = false; 1057 break; 1058 1059 case StabCompilerParameters: 1060 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0 1061 type = eSymbolTypeCompiler; 1062 break; 1063 1064 case StabCompilerVersion: 1065 // N_VERSION - compiler version: name,,NO_SECT,0,0 1066 type = eSymbolTypeCompiler; 1067 break; 1068 1069 case StabCompilerOptLevel: 1070 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0 1071 type = eSymbolTypeCompiler; 1072 break; 1073 1074 case StabParameter: 1075 // N_PSYM - parameter: name,,NO_SECT,type,offset 1076 type = eSymbolTypeVariable; 1077 break; 1078 1079 case StabAlternateEntry: 1080 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address 1081 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 1082 type = eSymbolTypeLineEntry; 1083 break; 1084 1085 //---------------------------------------------------------------------- 1086 // Left and Right Braces 1087 //---------------------------------------------------------------------- 1088 case StabLeftBracket: 1089 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address 1090 // We use the current number of symbols in the symbol table in lieu of 1091 // using nlist_idx in case we ever start trimming entries out 1092 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 1093 N_BRAC_indexes.push_back(sym_idx); 1094 type = eSymbolTypeScopeBegin; 1095 break; 1096 1097 case StabRightBracket: 1098 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address 1099 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC 1100 // so that we can always skip the entire symbol if we need to navigate 1101 // more quickly at the source level when parsing STABS 1102 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 1103 if ( !N_BRAC_indexes.empty() ) 1104 { 1105 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back()); 1106 symbol_ptr->SetByteSize(sym_idx + 1); 1107 symbol_ptr->SetSizeIsSibling(true); 1108 N_BRAC_indexes.pop_back(); 1109 } 1110 type = eSymbolTypeScopeEnd; 1111 break; 1112 1113 case StabDeletedIncludeFile: 1114 // N_EXCL - deleted include file: name,,NO_SECT,0,sum 1115 type = eSymbolTypeHeaderFile; 1116 break; 1117 1118 //---------------------------------------------------------------------- 1119 // COMM scopes 1120 //---------------------------------------------------------------------- 1121 case StabBeginCommon: 1122 // N_BCOMM - begin common: name,,NO_SECT,0,0 1123 // We use the current number of symbols in the symbol table in lieu of 1124 // using nlist_idx in case we ever start trimming entries out 1125 type = eSymbolTypeScopeBegin; 1126 N_COMM_indexes.push_back(sym_idx); 1127 break; 1128 1129 case StabEndCommonLocal: 1130 // N_ECOML - end common (local name): 0,,n_sect,0,address 1131 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 1132 // Fall through 1133 1134 case StabEndCommon: 1135 // N_ECOMM - end common: name,,n_sect,0,0 1136 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML 1137 // so that we can always skip the entire symbol if we need to navigate 1138 // more quickly at the source level when parsing STABS 1139 if ( !N_COMM_indexes.empty() ) 1140 { 1141 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back()); 1142 symbol_ptr->SetByteSize(sym_idx + 1); 1143 symbol_ptr->SetSizeIsSibling(true); 1144 N_COMM_indexes.pop_back(); 1145 } 1146 type = eSymbolTypeScopeEnd; 1147 break; 1148 1149 case StabLength: 1150 // N_LENG - second stab entry with length information 1151 type = eSymbolTypeAdditional; 1152 break; 1153 1154 default: break; 1155 } 1156 } 1157 else 1158 { 1159 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type; 1160 uint8_t n_type = NlistMaskType & nlist.n_type; 1161 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0); 1162 1163 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name) 1164 { 1165 type = eSymbolTypeRuntime; 1166 } 1167 else 1168 { 1169 switch (n_type) 1170 { 1171 case NListTypeIndirect: // N_INDR - Fall through 1172 case NListTypePreboundUndefined:// N_PBUD - Fall through 1173 case NListTypeUndefined: // N_UNDF 1174 type = eSymbolTypeExtern; 1175 break; 1176 1177 case NListTypeAbsolute: // N_ABS 1178 type = eSymbolTypeAbsolute; 1179 break; 1180 1181 case NListTypeSection: // N_SECT 1182 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 1183 1184 if (symbol_section == NULL) 1185 { 1186 // TODO: warn about this? 1187 add_nlist = false; 1188 break; 1189 } 1190 1191 if (TEXT_eh_frame_sectID == nlist.n_sect) 1192 { 1193 type = eSymbolTypeException; 1194 } 1195 else 1196 { 1197 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType; 1198 1199 switch (section_type) 1200 { 1201 case SectionTypeRegular: break; // regular section 1202 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section 1203 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings 1204 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals 1205 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals 1206 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals 1207 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers 1208 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers 1209 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field 1210 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization 1211 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination 1212 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced 1213 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes) 1214 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing 1215 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals 1216 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break; 1217 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break; 1218 default: break; 1219 } 1220 1221 if (type == eSymbolTypeInvalid) 1222 { 1223 const char *symbol_sect_name = symbol_section->GetName().AsCString(); 1224 if (symbol_section->IsDescendant (text_section_sp.get())) 1225 { 1226 if (symbol_section->IsClear(SectionAttrUserPureInstructions | 1227 SectionAttrUserSelfModifyingCode | 1228 SectionAttrSytemSomeInstructions)) 1229 type = eSymbolTypeData; 1230 else 1231 type = eSymbolTypeCode; 1232 } 1233 else 1234 if (symbol_section->IsDescendant(data_section_sp.get())) 1235 { 1236 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name) 1237 { 1238 type = eSymbolTypeRuntime; 1239 } 1240 else 1241 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name) 1242 { 1243 type = eSymbolTypeException; 1244 } 1245 else 1246 { 1247 type = eSymbolTypeData; 1248 } 1249 } 1250 else 1251 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name) 1252 { 1253 type = eSymbolTypeTrampoline; 1254 } 1255 else 1256 if (symbol_section->IsDescendant(objc_section_sp.get())) 1257 { 1258 type = eSymbolTypeRuntime; 1259 } 1260 } 1261 } 1262 break; 1263 } 1264 } 1265 } 1266 if (add_nlist) 1267 { 1268 bool symbol_name_is_mangled = false; 1269 if (symbol_name && symbol_name[0] == '_') 1270 { 1271 symbol_name_is_mangled = symbol_name[1] == '_'; 1272 symbol_name++; // Skip the leading underscore 1273 } 1274 uint64_t symbol_value = nlist.n_value; 1275 1276 if (symbol_name) 1277 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled); 1278 if (is_debug == false) 1279 { 1280 if (type == eSymbolTypeCode) 1281 { 1282 // See if we can find a N_FUN entry for any code symbols. 1283 // If we do find a match, and the name matches, then we 1284 // can merge the two into just the function symbol to avoid 1285 // duplicate entries in the symbol table 1286 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value); 1287 if (pos != N_FUN_addr_to_sym_idx.end()) 1288 { 1289 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || 1290 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) 1291 { 1292 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 1293 // We just need the flags from the linker symbol, so put these flags 1294 // into the N_FUN flags to avoid duplicate symbols in the symbol table 1295 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); 1296 sym[sym_idx].Clear(); 1297 continue; 1298 } 1299 } 1300 } 1301 else if (type == eSymbolTypeData) 1302 { 1303 // See if we can find a N_STSYM entry for any data symbols. 1304 // If we do find a match, and the name matches, then we 1305 // can merge the two into just the Static symbol to avoid 1306 // duplicate entries in the symbol table 1307 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value); 1308 if (pos != N_STSYM_addr_to_sym_idx.end()) 1309 { 1310 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || 1311 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) 1312 { 1313 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 1314 // We just need the flags from the linker symbol, so put these flags 1315 // into the N_STSYM flags to avoid duplicate symbols in the symbol table 1316 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); 1317 sym[sym_idx].Clear(); 1318 continue; 1319 } 1320 } 1321 } 1322 } 1323 if (symbol_section != NULL) 1324 symbol_value -= symbol_section->GetFileAddress(); 1325 1326 sym[sym_idx].SetID (nlist_idx); 1327 sym[sym_idx].SetType (type); 1328 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section); 1329 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value); 1330 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); 1331 1332 ++sym_idx; 1333 } 1334 else 1335 { 1336 sym[sym_idx].Clear(); 1337 } 1338 1339 } 1340 1341 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value 1342 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all 1343 // such entries by figuring out what the address for the global is by looking up this non-STAB 1344 // entry and copying the value into the debug symbol's value to save us the hassle in the 1345 // debug symbol parser. 1346 1347 Symbol *global_symbol = NULL; 1348 for (nlist_idx = 0; 1349 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL; 1350 nlist_idx++) 1351 { 1352 if (global_symbol->GetValue().GetFileAddress() == 0) 1353 { 1354 std::vector<uint32_t> indexes; 1355 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0) 1356 { 1357 std::vector<uint32_t>::const_iterator pos; 1358 std::vector<uint32_t>::const_iterator end = indexes.end(); 1359 for (pos = indexes.begin(); pos != end; ++pos) 1360 { 1361 symbol_ptr = symtab->SymbolAtIndex(*pos); 1362 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false) 1363 { 1364 global_symbol->SetValue(symbol_ptr->GetValue()); 1365 break; 1366 } 1367 } 1368 } 1369 } 1370 } 1371 1372 // Trim our symbols down to just what we ended up with after 1373 // removing any symbols. 1374 if (sym_idx < num_syms) 1375 { 1376 num_syms = sym_idx; 1377 sym = symtab->Resize (num_syms); 1378 } 1379 1380 // Now synthesize indirect symbols 1381 if (m_dysymtab.nindirectsyms != 0) 1382 { 1383 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4)); 1384 1385 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize()) 1386 { 1387 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end(); 1388 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize()); 1389 1390 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx) 1391 { 1392 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs) 1393 { 1394 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2; 1395 if (symbol_stub_byte_size == 0) 1396 continue; 1397 1398 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size; 1399 1400 if (num_symbol_stubs == 0) 1401 continue; 1402 1403 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1; 1404 uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms; 1405 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) 1406 { 1407 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx; 1408 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size); 1409 uint32_t symbol_stub_offset = symbol_stub_index * 4; 1410 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4)) 1411 { 1412 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset); 1413 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal)) 1414 continue; 1415 1416 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id); 1417 Symbol *stub_symbol = NULL; 1418 if (index_pos != end_index_pos) 1419 { 1420 // We have a remapping from the original nlist index to 1421 // a current symbol index, so just look this up by index 1422 stub_symbol = symtab->SymbolAtIndex (index_pos->second); 1423 } 1424 else 1425 { 1426 // We need to lookup a symbol using the original nlist 1427 // symbol index since this index is coming from the 1428 // S_SYMBOL_STUBS 1429 stub_symbol = symtab->FindSymbolByID (stub_sym_id); 1430 } 1431 1432 assert (stub_symbol); 1433 if (stub_symbol) 1434 { 1435 Address so_addr(symbol_stub_addr, section_list); 1436 1437 if (stub_symbol->GetType() == eSymbolTypeExtern) 1438 { 1439 // Change the external symbol into a trampoline that makes sense 1440 // These symbols were N_UNDF N_EXT, and are useless to us, so we 1441 // can re-use them so we don't have to make up a synthetic symbol 1442 // for no good reason. 1443 stub_symbol->SetType (eSymbolTypeTrampoline); 1444 stub_symbol->SetExternal (false); 1445 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr; 1446 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size); 1447 } 1448 else 1449 { 1450 // Make a synthetic symbol to describe the trampoline stub 1451 if (sym_idx >= num_syms) 1452 sym = symtab->Resize (++num_syms); 1453 sym[sym_idx].SetID (synthetic_stub_sym_id++); 1454 sym[sym_idx].GetMangled() = stub_symbol->GetMangled(); 1455 sym[sym_idx].SetType (eSymbolTypeTrampoline); 1456 sym[sym_idx].SetIsSynthetic (true); 1457 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr; 1458 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size); 1459 ++sym_idx; 1460 } 1461 } 1462 } 1463 } 1464 } 1465 } 1466 } 1467 } 1468 1469 return symtab->GetNumSymbols(); 1470 } 1471 } 1472 offset = cmd_offset + symtab_load_command.cmdsize; 1473 } 1474 return 0; 1475 } 1476 1477 1478 void 1479 ObjectFileMachO::Dump (Stream *s) 1480 { 1481 lldb_private::Mutex::Locker locker(m_mutex); 1482 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 1483 s->Indent(); 1484 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped) 1485 s->PutCString("ObjectFileMachO64"); 1486 else 1487 s->PutCString("ObjectFileMachO32"); 1488 1489 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); 1490 1491 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n"; 1492 1493 if (m_sections_ap.get()) 1494 m_sections_ap->Dump(s, NULL, true, UINT32_MAX); 1495 1496 if (m_symtab_ap.get()) 1497 m_symtab_ap->Dump(s, NULL, eSortOrderNone); 1498 } 1499 1500 1501 bool 1502 ObjectFileMachO::GetUUID (lldb_private::UUID* uuid) 1503 { 1504 lldb_private::Mutex::Locker locker(m_mutex); 1505 struct uuid_command load_cmd; 1506 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 1507 uint32_t i; 1508 for (i=0; i<m_header.ncmds; ++i) 1509 { 1510 const uint32_t cmd_offset = offset; 1511 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 1512 break; 1513 1514 if (load_cmd.cmd == LoadCommandUUID) 1515 { 1516 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16); 1517 if (uuid_bytes) 1518 { 1519 uuid->SetBytes (uuid_bytes); 1520 return true; 1521 } 1522 return false; 1523 } 1524 offset = cmd_offset + load_cmd.cmdsize; 1525 } 1526 return false; 1527 } 1528 1529 1530 uint32_t 1531 ObjectFileMachO::GetDependentModules (FileSpecList& files) 1532 { 1533 lldb_private::Mutex::Locker locker(m_mutex); 1534 struct load_command load_cmd; 1535 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 1536 uint32_t count = 0; 1537 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system 1538 uint32_t i; 1539 for (i=0; i<m_header.ncmds; ++i) 1540 { 1541 const uint32_t cmd_offset = offset; 1542 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 1543 break; 1544 1545 switch (load_cmd.cmd) 1546 { 1547 case LoadCommandDylibLoad: 1548 case LoadCommandDylibLoadWeak: 1549 case LoadCommandDylibReexport: 1550 case LoadCommandDynamicLinkerLoad: 1551 case LoadCommandFixedVMShlibLoad: 1552 case LoadCommandDylibLoadUpward: 1553 { 1554 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset); 1555 const char *path = m_data.PeekCStr(name_offset); 1556 // Skip any path that starts with '@' since these are usually: 1557 // @executable_path/.../file 1558 // @rpath/.../file 1559 if (path && path[0] != '@') 1560 { 1561 FileSpec file_spec(path, resolve_path); 1562 if (files.AppendIfUnique(file_spec)) 1563 count++; 1564 } 1565 } 1566 break; 1567 1568 default: 1569 break; 1570 } 1571 offset = cmd_offset + load_cmd.cmdsize; 1572 } 1573 return count; 1574 } 1575 1576 lldb_private::Address 1577 ObjectFileMachO::GetEntryPointAddress () 1578 { 1579 // If the object file is not an executable it can't hold the entry point. m_entry_point_address 1580 // is initialized to an invalid address, so we can just return that. 1581 // If m_entry_point_address is valid it means we've found it already, so return the cached value. 1582 1583 if (!IsExecutable() || m_entry_point_address.IsValid()) 1584 return m_entry_point_address; 1585 1586 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in 1587 // /usr/include/mach-o.h, but it is basically: 1588 // 1589 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state 1590 // uint32_t count - this is the count of longs in the thread state data 1591 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor. 1592 // <repeat this trio> 1593 // 1594 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there. 1595 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers 1596 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin, 1597 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here. 1598 // 1599 // For now we hard-code the offsets and flavors we need: 1600 // 1601 // 1602 1603 lldb_private::Mutex::Locker locker(m_mutex); 1604 struct load_command load_cmd; 1605 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 1606 uint32_t i; 1607 lldb::addr_t start_address = LLDB_INVALID_ADDRESS; 1608 bool done = false; 1609 1610 for (i=0; i<m_header.ncmds; ++i) 1611 { 1612 const uint32_t cmd_offset = offset; 1613 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 1614 break; 1615 1616 switch (load_cmd.cmd) 1617 { 1618 case LoadCommandUnixThread: 1619 case LoadCommandThread: 1620 { 1621 while (offset < cmd_offset + load_cmd.cmdsize) 1622 { 1623 uint32_t flavor = m_data.GetU32(&offset); 1624 uint32_t count = m_data.GetU32(&offset); 1625 if (count == 0) 1626 { 1627 // We've gotten off somehow, log and exit; 1628 return m_entry_point_address; 1629 } 1630 1631 switch (m_header.cputype) 1632 { 1633 case llvm::MachO::CPUTypeARM: 1634 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h 1635 { 1636 offset += 60; // This is the offset of pc in the GPR thread state data structure. 1637 start_address = m_data.GetU32(&offset); 1638 done = true; 1639 } 1640 break; 1641 case llvm::MachO::CPUTypeI386: 1642 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h 1643 { 1644 offset += 40; // This is the offset of eip in the GPR thread state data structure. 1645 start_address = m_data.GetU32(&offset); 1646 done = true; 1647 } 1648 break; 1649 case llvm::MachO::CPUTypeX86_64: 1650 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h 1651 { 1652 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure. 1653 start_address = m_data.GetU64(&offset); 1654 done = true; 1655 } 1656 break; 1657 default: 1658 return m_entry_point_address; 1659 } 1660 // Haven't found the GPR flavor yet, skip over the data for this flavor: 1661 if (done) 1662 break; 1663 offset += count * 4; 1664 } 1665 } 1666 break; 1667 1668 default: 1669 break; 1670 } 1671 if (done) 1672 break; 1673 1674 // Go to the next load command: 1675 offset = cmd_offset + load_cmd.cmdsize; 1676 } 1677 1678 if (start_address != LLDB_INVALID_ADDRESS) 1679 { 1680 // We got the start address from the load commands, so now resolve that address in the sections 1681 // of this ObjectFile: 1682 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList())) 1683 { 1684 m_entry_point_address.Clear(); 1685 } 1686 } 1687 else 1688 { 1689 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the 1690 // "start" symbol in the main executable. 1691 1692 SymbolContextList contexts; 1693 SymbolContext context; 1694 if (!m_module->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts)) 1695 return m_entry_point_address; 1696 1697 contexts.GetContextAtIndex(0, context); 1698 1699 m_entry_point_address = context.symbol->GetValue(); 1700 } 1701 1702 return m_entry_point_address; 1703 1704 } 1705 1706 ObjectFile::Type 1707 ObjectFileMachO::CalculateType() 1708 { 1709 switch (m_header.filetype) 1710 { 1711 case HeaderFileTypeObject: // 0x1u MH_OBJECT 1712 if (GetAddressByteSize () == 4) 1713 { 1714 // 32 bit kexts are just object files, but they do have a valid 1715 // UUID load command. 1716 UUID uuid; 1717 if (GetUUID(&uuid)) 1718 { 1719 // this checking for the UUID load command is not enough 1720 // we could eventually look for the symbol named 1721 // "OSKextGetCurrentIdentifier" as this is required of kexts 1722 if (m_strata == eStrataInvalid) 1723 m_strata = eStrataKernel; 1724 return eTypeSharedLibrary; 1725 } 1726 } 1727 return eTypeObjectFile; 1728 1729 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE 1730 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB 1731 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE 1732 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD 1733 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB 1734 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER 1735 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE 1736 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB 1737 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM 1738 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE 1739 default: 1740 break; 1741 } 1742 return eTypeUnknown; 1743 } 1744 1745 ObjectFile::Strata 1746 ObjectFileMachO::CalculateStrata() 1747 { 1748 switch (m_header.filetype) 1749 { 1750 case HeaderFileTypeObject: // 0x1u MH_OBJECT 1751 { 1752 // 32 bit kexts are just object files, but they do have a valid 1753 // UUID load command. 1754 UUID uuid; 1755 if (GetUUID(&uuid)) 1756 { 1757 // this checking for the UUID load command is not enough 1758 // we could eventually look for the symbol named 1759 // "OSKextGetCurrentIdentifier" as this is required of kexts 1760 if (m_type == eTypeInvalid) 1761 m_type = eTypeSharedLibrary; 1762 1763 return eStrataKernel; 1764 } 1765 } 1766 return eStrataUnknown; 1767 1768 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE 1769 // Check for the MH_DYLDLINK bit in the flags 1770 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject) 1771 return eStrataUser; 1772 return eStrataKernel; 1773 1774 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB 1775 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE 1776 case HeaderFileTypePreloadedExecutable: return eStrataUser; // 0x5u MH_PRELOAD 1777 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB 1778 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER 1779 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE 1780 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB 1781 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM 1782 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE 1783 default: 1784 break; 1785 } 1786 return eStrataUnknown; 1787 } 1788 1789 1790 bool 1791 ObjectFileMachO::GetArchitecture (ArchSpec &arch) 1792 { 1793 lldb_private::Mutex::Locker locker(m_mutex); 1794 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype); 1795 return true; 1796 } 1797 1798 1799 //------------------------------------------------------------------ 1800 // PluginInterface protocol 1801 //------------------------------------------------------------------ 1802 const char * 1803 ObjectFileMachO::GetPluginName() 1804 { 1805 return "ObjectFileMachO"; 1806 } 1807 1808 const char * 1809 ObjectFileMachO::GetShortPluginName() 1810 { 1811 return GetPluginNameStatic(); 1812 } 1813 1814 uint32_t 1815 ObjectFileMachO::GetPluginVersion() 1816 { 1817 return 1; 1818 } 1819 1820