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