ObjectFileMachO.cpp revision 7508e732818c32e1cfeaaf7d1d507fe3834ce9d2
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 "ObjectFileMachO.h" 11 12#include "lldb/Core/ArchSpec.h" 13#include "lldb/Core/DataBuffer.h" 14#include "lldb/Core/FileSpec.h" 15#include "lldb/Core/FileSpecList.h" 16#include "lldb/Core/Module.h" 17#include "lldb/Core/PluginManager.h" 18#include "lldb/Core/Section.h" 19#include "lldb/Core/StreamFile.h" 20#include "lldb/Core/StreamString.h" 21#include "lldb/Core/Timer.h" 22#include "lldb/Core/UUID.h" 23#include "lldb/Symbol/ObjectFile.h" 24 25 26using namespace lldb; 27using namespace lldb_private; 28using namespace llvm::MachO; 29 30 31void 32ObjectFileMachO::Initialize() 33{ 34 PluginManager::RegisterPlugin (GetPluginNameStatic(), 35 GetPluginDescriptionStatic(), 36 CreateInstance); 37} 38 39void 40ObjectFileMachO::Terminate() 41{ 42 PluginManager::UnregisterPlugin (CreateInstance); 43} 44 45 46const char * 47ObjectFileMachO::GetPluginNameStatic() 48{ 49 return "object-file.mach-o"; 50} 51 52const char * 53ObjectFileMachO::GetPluginDescriptionStatic() 54{ 55 return "Mach-o object file reader (32 and 64 bit)"; 56} 57 58 59ObjectFile * 60ObjectFileMachO::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) 61{ 62 if (ObjectFileMachO::MagicBytesMatch(dataSP)) 63 { 64 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, dataSP, file, offset, length)); 65 if (objfile_ap.get() && objfile_ap->ParseHeader()) 66 return objfile_ap.release(); 67 } 68 return NULL; 69} 70 71 72static uint32_t 73MachHeaderSizeFromMagic(uint32_t magic) 74{ 75 switch (magic) 76 { 77 case HeaderMagic32: 78 case HeaderMagic32Swapped: 79 return sizeof(struct mach_header); 80 81 case HeaderMagic64: 82 case HeaderMagic64Swapped: 83 return sizeof(struct mach_header_64); 84 break; 85 86 default: 87 break; 88 } 89 return 0; 90} 91 92 93bool 94ObjectFileMachO::MagicBytesMatch (DataBufferSP& dataSP) 95{ 96 DataExtractor data(dataSP, eByteOrderHost, 4); 97 uint32_t offset = 0; 98 uint32_t magic = data.GetU32(&offset); 99 return MachHeaderSizeFromMagic(magic) != 0; 100} 101 102 103ObjectFileMachO::ObjectFileMachO(Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) : 104 ObjectFile(module, file, offset, length, dataSP), 105 m_mutex (Mutex::eMutexTypeRecursive), 106 m_header(), 107 m_sections_ap(), 108 m_symtab_ap() 109{ 110 ::bzero (&m_header, sizeof(m_header)); 111 ::bzero (&m_dysymtab, sizeof(m_dysymtab)); 112} 113 114 115ObjectFileMachO::~ObjectFileMachO() 116{ 117} 118 119 120bool 121ObjectFileMachO::ParseHeader () 122{ 123 lldb_private::Mutex::Locker locker(m_mutex); 124 bool can_parse = false; 125 uint32_t offset = 0; 126 m_data.SetByteOrder (eByteOrderHost); 127 // Leave magic in the original byte order 128 m_header.magic = m_data.GetU32(&offset); 129 switch (m_header.magic) 130 { 131 case HeaderMagic32: 132 m_data.SetByteOrder (eByteOrderHost); 133 m_data.SetAddressByteSize(4); 134 can_parse = true; 135 break; 136 137 case HeaderMagic64: 138 m_data.SetByteOrder (eByteOrderHost); 139 m_data.SetAddressByteSize(8); 140 can_parse = true; 141 break; 142 143 case HeaderMagic32Swapped: 144 m_data.SetByteOrder(eByteOrderHost == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); 145 m_data.SetAddressByteSize(4); 146 can_parse = true; 147 break; 148 149 case HeaderMagic64Swapped: 150 m_data.SetByteOrder(eByteOrderHost == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); 151 m_data.SetAddressByteSize(8); 152 can_parse = true; 153 break; 154 155 default: 156 break; 157 } 158 159 if (can_parse) 160 { 161 m_data.GetU32(&offset, &m_header.cputype, 6); 162 163 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); 164 165 if (SetModulesArchitecture (mach_arch)) 166 { 167 // Read in all only the load command data 168 DataBufferSP data_sp(m_file.ReadFileContents(m_offset, m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic))); 169 m_data.SetData (data_sp); 170 return true; 171 } 172 } 173 else 174 { 175 memset(&m_header, 0, sizeof(struct mach_header)); 176 } 177 return false; 178} 179 180 181ByteOrder 182ObjectFileMachO::GetByteOrder () const 183{ 184 lldb_private::Mutex::Locker locker(m_mutex); 185 return m_data.GetByteOrder (); 186} 187 188bool 189ObjectFileMachO::IsExecutable() const 190{ 191 return m_header.filetype == HeaderFileTypeExecutable; 192} 193 194size_t 195ObjectFileMachO::GetAddressByteSize () const 196{ 197 lldb_private::Mutex::Locker locker(m_mutex); 198 return m_data.GetAddressByteSize (); 199} 200 201 202Symtab * 203ObjectFileMachO::GetSymtab() 204{ 205 lldb_private::Mutex::Locker locker(m_mutex); 206 if (m_symtab_ap.get() == NULL) 207 { 208 m_symtab_ap.reset(new Symtab(this)); 209 ParseSymtab(false); 210 } 211 return m_symtab_ap.get(); 212} 213 214 215SectionList * 216ObjectFileMachO::GetSectionList() 217{ 218 lldb_private::Mutex::Locker locker(m_mutex); 219 if (m_sections_ap.get() == NULL) 220 { 221 m_sections_ap.reset(new SectionList()); 222 ParseSections(); 223 } 224 return m_sections_ap.get(); 225} 226 227 228size_t 229ObjectFileMachO::ParseSections () 230{ 231 lldb::user_id_t segID = 0; 232 lldb::user_id_t sectID = 0; 233 struct segment_command_64 load_cmd; 234 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 235 uint32_t i; 236 //bool dump_sections = false; 237 for (i=0; i<m_header.ncmds; ++i) 238 { 239 const uint32_t load_cmd_offset = offset; 240 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 241 break; 242 243 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64) 244 { 245 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16)) 246 { 247 load_cmd.vmaddr = m_data.GetAddress(&offset); 248 load_cmd.vmsize = m_data.GetAddress(&offset); 249 load_cmd.fileoff = m_data.GetAddress(&offset); 250 load_cmd.filesize = m_data.GetAddress(&offset); 251 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4)) 252 { 253 // Keep a list of mach segments around in case we need to 254 // get at data that isn't stored in the abstracted Sections. 255 m_mach_segments.push_back (load_cmd); 256 257 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname))); 258 // Use a segment ID of the segment index shifted left by 8 so they 259 // never conflict with any of the sections. 260 SectionSP segment_sp; 261 if (segment_name) 262 { 263 segment_sp.reset(new Section (NULL, 264 GetModule(), // Module to which this section belongs 265 ++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 266 segment_name, // Name of this section 267 eSectionTypeContainer, // This section is a container of other sections. 268 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file 269 load_cmd.vmsize, // VM size in bytes of this section 270 load_cmd.fileoff, // Offset to the data for this section in the file 271 load_cmd.filesize, // Size in bytes of this section as found in the the file 272 load_cmd.flags)); // Flags for this section 273 274 m_sections_ap->AddSection(segment_sp); 275 } 276 277 struct section_64 sect64; 278 ::bzero (§64, sizeof(sect64)); 279 // Push a section into our mach sections for the section at 280 // index zero (NListSectionNoSection) 281 m_mach_sections.push_back(sect64); 282 uint32_t segment_sect_idx; 283 const lldb::user_id_t first_segment_sectID = sectID + 1; 284 285 286 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8; 287 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx) 288 { 289 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL) 290 break; 291 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL) 292 break; 293 sect64.addr = m_data.GetAddress(&offset); 294 sect64.size = m_data.GetAddress(&offset); 295 296 if (m_data.GetU32(&offset, §64.offset, num_u32s) == NULL) 297 break; 298 299 // Keep a list of mach sections around in case we need to 300 // get at data that isn't stored in the abstracted Sections. 301 m_mach_sections.push_back (sect64); 302 303 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname))); 304 if (!segment_name) 305 { 306 // We have a segment with no name so we need to conjure up 307 // segments that correspond to the section's segname if there 308 // isn't already such a section. If there is such a section, 309 // we resize the section so that it spans all sections. 310 // We also mark these sections as fake so address matches don't 311 // hit if they land in the gaps between the child sections. 312 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname)); 313 segment_sp = m_sections_ap->FindSectionByName (segment_name); 314 if (segment_sp.get()) 315 { 316 Section *segment = segment_sp.get(); 317 // Grow the section size as needed. 318 const lldb::addr_t sect64_min_addr = sect64.addr; 319 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size; 320 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize(); 321 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress(); 322 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size; 323 if (sect64_min_addr >= curr_seg_min_addr) 324 { 325 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr; 326 // Only grow the section size if needed 327 if (new_seg_byte_size > curr_seg_byte_size) 328 segment->SetByteSize (new_seg_byte_size); 329 } 330 else 331 { 332 // We need to change the base address of the segment and 333 // adjust the child section offsets for all existing children. 334 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr; 335 segment->Slide(slide_amount, false); 336 segment->GetChildren().Slide (-slide_amount, false); 337 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr); 338 } 339 340 // Grow the section size as needed. 341 if (sect64.offset) 342 { 343 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset(); 344 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize(); 345 346 const lldb::addr_t section_min_file_offset = sect64.offset; 347 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size; 348 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset); 349 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset; 350 segment->SetFileOffset (new_file_offset); 351 segment->SetFileSize (new_file_size); 352 } 353 } 354 else 355 { 356 // Create a fake section for the section's named segment 357 segment_sp.reset(new Section(segment_sp.get(), // Parent section 358 GetModule(), // Module to which this section belongs 359 ++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 360 segment_name, // Name of this section 361 eSectionTypeContainer, // This section is a container of other sections. 362 sect64.addr, // File VM address == addresses as they are found in the object file 363 sect64.size, // VM size in bytes of this section 364 sect64.offset, // Offset to the data for this section in the file 365 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file 366 load_cmd.flags)); // Flags for this section 367 segment_sp->SetIsFake(true); 368 m_sections_ap->AddSection(segment_sp); 369 } 370 } 371 assert (segment_sp.get()); 372 373 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType; 374 static ConstString g_sect_name_objc_data ("__objc_data"); 375 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs"); 376 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs"); 377 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs"); 378 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs"); 379 static ConstString g_sect_name_objc_const ("__objc_const"); 380 static ConstString g_sect_name_objc_classlist ("__objc_classlist"); 381 static ConstString g_sect_name_cfstring ("__cfstring"); 382 383 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev"); 384 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges"); 385 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame"); 386 static ConstString g_sect_name_dwarf_debug_info ("__debug_info"); 387 static ConstString g_sect_name_dwarf_debug_line ("__debug_line"); 388 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc"); 389 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo"); 390 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames"); 391 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes"); 392 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges"); 393 static ConstString g_sect_name_dwarf_debug_str ("__debug_str"); 394 static ConstString g_sect_name_eh_frame ("__eh_frame"); 395 396 SectionType sect_type = eSectionTypeOther; 397 398 399 if (section_name == g_sect_name_dwarf_debug_abbrev) 400 sect_type = eSectionTypeDWARFDebugAbbrev; 401 else if (section_name == g_sect_name_dwarf_debug_aranges) 402 sect_type = eSectionTypeDWARFDebugAranges; 403 else if (section_name == g_sect_name_dwarf_debug_frame) 404 sect_type = eSectionTypeDWARFDebugFrame; 405 else if (section_name == g_sect_name_dwarf_debug_info) 406 sect_type = eSectionTypeDWARFDebugInfo; 407 else if (section_name == g_sect_name_dwarf_debug_line) 408 sect_type = eSectionTypeDWARFDebugLine; 409 else if (section_name == g_sect_name_dwarf_debug_loc) 410 sect_type = eSectionTypeDWARFDebugLoc; 411 else if (section_name == g_sect_name_dwarf_debug_macinfo) 412 sect_type = eSectionTypeDWARFDebugMacInfo; 413 else if (section_name == g_sect_name_dwarf_debug_pubnames) 414 sect_type = eSectionTypeDWARFDebugPubNames; 415 else if (section_name == g_sect_name_dwarf_debug_pubtypes) 416 sect_type = eSectionTypeDWARFDebugPubTypes; 417 else if (section_name == g_sect_name_dwarf_debug_ranges) 418 sect_type = eSectionTypeDWARFDebugRanges; 419 else if (section_name == g_sect_name_dwarf_debug_str) 420 sect_type = eSectionTypeDWARFDebugStr; 421 else if (section_name == g_sect_name_objc_selrefs) 422 sect_type = eSectionTypeDataCStringPointers; 423 else if (section_name == g_sect_name_objc_msgrefs) 424 sect_type = eSectionTypeDataObjCMessageRefs; 425 else if (section_name == g_sect_name_eh_frame) 426 sect_type = eSectionTypeEHFrame; 427 else if (section_name == g_sect_name_cfstring) 428 sect_type = eSectionTypeDataObjCCFStrings; 429 else if (section_name == g_sect_name_objc_data || 430 section_name == g_sect_name_objc_classrefs || 431 section_name == g_sect_name_objc_superrefs || 432 section_name == g_sect_name_objc_const || 433 section_name == g_sect_name_objc_classlist) 434 { 435 sect_type = eSectionTypeDataPointers; 436 } 437 438 if (sect_type == eSectionTypeOther) 439 { 440 switch (mach_sect_type) 441 { 442 // TODO: categorize sections by other flags for regular sections 443 case SectionTypeRegular: sect_type = eSectionTypeOther; break; 444 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break; 445 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings 446 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals 447 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals 448 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals 449 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers 450 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers 451 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field 452 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization 453 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination 454 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break; 455 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break; 456 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing 457 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals 458 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break; 459 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break; 460 default: break; 461 } 462 } 463 464 SectionSP section_sp(new Section(segment_sp.get(), 465 GetModule(), 466 ++sectID, 467 section_name, 468 sect_type, 469 sect64.addr - segment_sp->GetFileAddress(), 470 sect64.size, 471 sect64.offset, 472 sect64.offset == 0 ? 0 : sect64.size, 473 sect64.flags)); 474 segment_sp->GetChildren().AddSection(section_sp); 475 476 if (segment_sp->IsFake()) 477 { 478 segment_sp.reset(); 479 segment_name.Clear(); 480 } 481 } 482 if (m_header.filetype == HeaderFileTypeDSYM) 483 { 484 if (first_segment_sectID <= sectID) 485 { 486 lldb::user_id_t sect_uid; 487 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid) 488 { 489 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid)); 490 SectionSP next_section_sp; 491 if (sect_uid + 1 <= sectID) 492 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1); 493 494 if (curr_section_sp.get()) 495 { 496 if (curr_section_sp->GetByteSize() == 0) 497 { 498 if (next_section_sp.get() != NULL) 499 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() ); 500 else 501 curr_section_sp->SetByteSize ( load_cmd.vmsize ); 502 } 503 } 504 } 505 } 506 } 507 } 508 } 509 } 510 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo) 511 { 512 m_dysymtab.cmd = load_cmd.cmd; 513 m_dysymtab.cmdsize = load_cmd.cmdsize; 514 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2); 515 } 516 517 offset = load_cmd_offset + load_cmd.cmdsize; 518 } 519// if (dump_sections) 520// { 521// StreamFile s(stdout); 522// m_sections_ap->Dump(&s, true); 523// } 524 return sectID; // Return the number of sections we registered with the module 525} 526 527class MachSymtabSectionInfo 528{ 529public: 530 531 MachSymtabSectionInfo (SectionList *section_list) : 532 m_section_list (section_list), 533 m_section_infos() 534 { 535 // Get the number of sections down to a depth of 1 to include 536 // all segments and their sections, but no other sections that 537 // may be added for debug map or 538 m_section_infos.resize(section_list->GetNumSections(1)); 539 } 540 541 542 Section * 543 GetSection (uint8_t n_sect, addr_t file_addr) 544 { 545 if (n_sect == 0) 546 return NULL; 547 if (n_sect < m_section_infos.size()) 548 { 549 if (m_section_infos[n_sect].section == NULL) 550 { 551 Section *section = m_section_list->FindSectionByID (n_sect).get(); 552 m_section_infos[n_sect].section = section; 553 assert (section != NULL); 554 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress()); 555 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize()); 556 } 557 if (m_section_infos[n_sect].vm_range.Contains(file_addr)) 558 return m_section_infos[n_sect].section; 559 } 560 return m_section_list->FindSectionContainingFileAddress(file_addr).get(); 561 } 562 563protected: 564 struct SectionInfo 565 { 566 SectionInfo () : 567 vm_range(), 568 section (NULL) 569 { 570 } 571 572 VMRange vm_range; 573 Section *section; 574 }; 575 SectionList *m_section_list; 576 std::vector<SectionInfo> m_section_infos; 577}; 578 579 580 581size_t 582ObjectFileMachO::ParseSymtab (bool minimize) 583{ 584 Timer scoped_timer(__PRETTY_FUNCTION__, 585 "ObjectFileMachO::ParseSymtab () module = %s", 586 m_file.GetFilename().AsCString("")); 587 struct symtab_command symtab_load_command; 588 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 589 uint32_t i; 590 for (i=0; i<m_header.ncmds; ++i) 591 { 592 const uint32_t cmd_offset = offset; 593 // Read in the load command and load command size 594 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL) 595 break; 596 // Watch for the symbol table load command 597 if (symtab_load_command.cmd == LoadCommandSymtab) 598 { 599 // Read in the rest of the symtab load command 600 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields 601 { 602 Symtab *symtab = m_symtab_ap.get(); 603 SectionList *section_list = GetSectionList(); 604 assert(section_list); 605 const size_t addr_size = m_data.GetAddressByteSize(); 606 const ByteOrder endian = m_data.GetByteOrder(); 607 bool bit_width_32 = addr_size == 4; 608 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64); 609 610 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size)); 611 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize)); 612 613 const char *strtab_data = (const char *)strtab_data_sp->GetBytes(); 614// DataExtractor symtab_data(symtab_data_sp, endian, addr_size); 615// DataExtractor strtab_data(strtab_data_sp, endian, addr_size); 616 617 static ConstString g_segment_name_TEXT ("__TEXT"); 618 static ConstString g_segment_name_DATA ("__DATA"); 619 static ConstString g_segment_name_OBJC ("__OBJC"); 620 static ConstString g_section_name_eh_frame ("__eh_frame"); 621 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT)); 622 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA)); 623 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC)); 624 SectionSP eh_frame_section_sp; 625 if (text_section_sp.get()) 626 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame); 627 else 628 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame); 629 630 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection; 631 //uint32_t symtab_offset = 0; 632 const uint8_t* nlist_data = symtab_data_sp->GetBytes(); 633 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms); 634 635 636 if (endian != eByteOrderHost) 637 { 638 // ... 639 assert (!"UNIMPLEMENTED: Swap all nlist entries"); 640 } 641 uint32_t N_SO_index = UINT_MAX; 642 643 MachSymtabSectionInfo section_info (section_list); 644 std::vector<uint32_t> N_FUN_indexes; 645 std::vector<uint32_t> N_NSYM_indexes; 646 std::vector<uint32_t> N_INCL_indexes; 647 std::vector<uint32_t> N_BRAC_indexes; 648 std::vector<uint32_t> N_COMM_indexes; 649 uint32_t nlist_idx = 0; 650 Symbol *symbol_ptr = NULL; 651 652 uint32_t sym_idx = 0; 653 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); 654 uint32_t num_syms = symtab->GetNumSymbols(); 655 656 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); 657 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) 658 { 659 struct nlist_64 nlist; 660 if (bit_width_32) 661 { 662 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size)); 663 nlist.n_strx = nlist32_ptr->n_strx; 664 nlist.n_type = nlist32_ptr->n_type; 665 nlist.n_sect = nlist32_ptr->n_sect; 666 nlist.n_desc = nlist32_ptr->n_desc; 667 nlist.n_value = nlist32_ptr->n_value; 668 } 669 else 670 { 671 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size))); 672 } 673 674 SymbolType type = eSymbolTypeInvalid; 675 const char* symbol_name = &strtab_data[nlist.n_strx]; 676 if (symbol_name[0] == '\0') 677 symbol_name = NULL; 678 Section* symbol_section = NULL; 679 bool add_nlist = true; 680 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0); 681 682 assert (sym_idx < num_syms); 683 684 sym[sym_idx].SetDebug (is_debug); 685 686 if (is_debug) 687 { 688 switch (nlist.n_type) 689 { 690 case StabGlobalSymbol: 691 // N_GSYM -- global symbol: name,,NO_SECT,type,0 692 // Sometimes the N_GSYM value contains the address. 693 if (nlist.n_value != 0) 694 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 695 type = eSymbolTypeGlobal; 696 break; 697 698 case StabFunctionName: 699 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0 700 type = eSymbolTypeFunction; 701 break; 702 703 case StabFunction: 704 // N_FUN -- procedure: name,,n_sect,linenumber,address 705 if (symbol_name) 706 { 707 type = eSymbolTypeFunction; 708 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 709 // We use the current number of symbols in the symbol table in lieu of 710 // using nlist_idx in case we ever start trimming entries out 711 N_FUN_indexes.push_back(sym_idx); 712 } 713 else 714 { 715 type = eSymbolTypeFunctionEnd; 716 717 if ( !N_FUN_indexes.empty() ) 718 { 719 // Copy the size of the function into the original STAB entry so we don't have 720 // to hunt for it later 721 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value); 722 N_FUN_indexes.pop_back(); 723 // We don't really need the end function STAB as it contains the size which 724 // we already placed with the original symbol, so don't add it if we want a 725 // minimal symbol table 726 if (minimize) 727 add_nlist = false; 728 } 729 } 730 break; 731 732 case StabStaticSymbol: 733 // N_STSYM -- static symbol: name,,n_sect,type,address 734 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 735 type = eSymbolTypeStatic; 736 break; 737 738 case StabLocalCommon: 739 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address 740 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 741 type = eSymbolTypeCommonBlock; 742 break; 743 744 case StabBeginSymbol: 745 // N_BNSYM 746 // We use the current number of symbols in the symbol table in lieu of 747 // using nlist_idx in case we ever start trimming entries out 748 if (minimize) 749 { 750 // Skip these if we want minimal symbol tables 751 add_nlist = false; 752 } 753 else 754 { 755 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 756 N_NSYM_indexes.push_back(sym_idx); 757 type = eSymbolTypeScopeBegin; 758 } 759 break; 760 761 case StabEndSymbol: 762 // N_ENSYM 763 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM 764 // so that we can always skip the entire symbol if we need to navigate 765 // more quickly at the source level when parsing STABS 766 if (minimize) 767 { 768 // Skip these if we want minimal symbol tables 769 add_nlist = false; 770 } 771 else 772 { 773 if ( !N_NSYM_indexes.empty() ) 774 { 775 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back()); 776 symbol_ptr->SetByteSize(sym_idx + 1); 777 symbol_ptr->SetSizeIsSibling(true); 778 N_NSYM_indexes.pop_back(); 779 } 780 type = eSymbolTypeScopeEnd; 781 } 782 break; 783 784 785 case StabSourceFileOptions: 786 // N_OPT - emitted with gcc2_compiled and in gcc source 787 type = eSymbolTypeCompiler; 788 break; 789 790 case StabRegisterSymbol: 791 // N_RSYM - register sym: name,,NO_SECT,type,register 792 type = eSymbolTypeVariable; 793 break; 794 795 case StabSourceLine: 796 // N_SLINE - src line: 0,,n_sect,linenumber,address 797 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 798 type = eSymbolTypeLineEntry; 799 break; 800 801 case StabStructureType: 802 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset 803 type = eSymbolTypeVariableType; 804 break; 805 806 case StabSourceFileName: 807 // N_SO - source file name 808 type = eSymbolTypeSourceFile; 809 if (symbol_name == NULL) 810 { 811 if (N_SO_index == UINT_MAX) 812 { 813 // Skip the extra blank N_SO entries that happen when the entire 814 // path is contained in the second consecutive N_SO STAB. 815 if (minimize) 816 add_nlist = false; 817 } 818 else 819 { 820 // Set the size of the N_SO to the terminating index of this N_SO 821 // so that we can always skip the entire N_SO if we need to navigate 822 // more quickly at the source level when parsing STABS 823 symbol_ptr = symtab->SymbolAtIndex(N_SO_index); 824 symbol_ptr->SetByteSize(sym_idx + 1); 825 symbol_ptr->SetSizeIsSibling(true); 826 } 827 N_NSYM_indexes.clear(); 828 N_INCL_indexes.clear(); 829 N_BRAC_indexes.clear(); 830 N_COMM_indexes.clear(); 831 N_FUN_indexes.clear(); 832 N_SO_index = UINT_MAX; 833 } 834 else if (symbol_name[0] == '/') 835 { 836 // We use the current number of symbols in the symbol table in lieu of 837 // using nlist_idx in case we ever start trimming entries out 838 N_SO_index = sym_idx; 839 } 840 break; 841 842 case StabObjectFileName: 843 // N_OSO - object file name: name,,0,0,st_mtime 844 type = eSymbolTypeObjectFile; 845 break; 846 847 case StabLocalSymbol: 848 // N_LSYM - local sym: name,,NO_SECT,type,offset 849 type = eSymbolTypeLocal; 850 break; 851 852 //---------------------------------------------------------------------- 853 // INCL scopes 854 //---------------------------------------------------------------------- 855 case StabBeginIncludeFileName: 856 // N_BINCL - include file beginning: name,,NO_SECT,0,sum 857 // We use the current number of symbols in the symbol table in lieu of 858 // using nlist_idx in case we ever start trimming entries out 859 N_INCL_indexes.push_back(sym_idx); 860 type = eSymbolTypeScopeBegin; 861 break; 862 863 case StabEndIncludeFile: 864 // N_EINCL - include file end: name,,NO_SECT,0,0 865 // Set the size of the N_BINCL to the terminating index of this N_EINCL 866 // so that we can always skip the entire symbol if we need to navigate 867 // more quickly at the source level when parsing STABS 868 if ( !N_INCL_indexes.empty() ) 869 { 870 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back()); 871 symbol_ptr->SetByteSize(sym_idx + 1); 872 symbol_ptr->SetSizeIsSibling(true); 873 N_INCL_indexes.pop_back(); 874 } 875 type = eSymbolTypeScopeEnd; 876 break; 877 878 case StabIncludeFileName: 879 // N_SOL - #included file name: name,,n_sect,0,address 880 type = eSymbolTypeHeaderFile; 881 break; 882 883 case StabCompilerParameters: 884 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0 885 type = eSymbolTypeCompiler; 886 break; 887 888 case StabCompilerVersion: 889 // N_VERSION - compiler version: name,,NO_SECT,0,0 890 type = eSymbolTypeCompiler; 891 break; 892 893 case StabCompilerOptLevel: 894 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0 895 type = eSymbolTypeCompiler; 896 break; 897 898 case StabParameter: 899 // N_PSYM - parameter: name,,NO_SECT,type,offset 900 type = eSymbolTypeVariable; 901 break; 902 903 case StabAlternateEntry: 904 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address 905 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 906 type = eSymbolTypeLineEntry; 907 break; 908 909 //---------------------------------------------------------------------- 910 // Left and Right Braces 911 //---------------------------------------------------------------------- 912 case StabLeftBracket: 913 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address 914 // We use the current number of symbols in the symbol table in lieu of 915 // using nlist_idx in case we ever start trimming entries out 916 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 917 N_BRAC_indexes.push_back(sym_idx); 918 type = eSymbolTypeScopeBegin; 919 break; 920 921 case StabRightBracket: 922 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address 923 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC 924 // so that we can always skip the entire symbol if we need to navigate 925 // more quickly at the source level when parsing STABS 926 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 927 if ( !N_BRAC_indexes.empty() ) 928 { 929 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back()); 930 symbol_ptr->SetByteSize(sym_idx + 1); 931 symbol_ptr->SetSizeIsSibling(true); 932 N_BRAC_indexes.pop_back(); 933 } 934 type = eSymbolTypeScopeEnd; 935 break; 936 937 case StabDeletedIncludeFile: 938 // N_EXCL - deleted include file: name,,NO_SECT,0,sum 939 type = eSymbolTypeHeaderFile; 940 break; 941 942 //---------------------------------------------------------------------- 943 // COMM scopes 944 //---------------------------------------------------------------------- 945 case StabBeginCommon: 946 // N_BCOMM - begin common: name,,NO_SECT,0,0 947 // We use the current number of symbols in the symbol table in lieu of 948 // using nlist_idx in case we ever start trimming entries out 949 type = eSymbolTypeScopeBegin; 950 N_COMM_indexes.push_back(sym_idx); 951 break; 952 953 case StabEndCommonLocal: 954 // N_ECOML - end common (local name): 0,,n_sect,0,address 955 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 956 // Fall through 957 958 case StabEndCommon: 959 // N_ECOMM - end common: name,,n_sect,0,0 960 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML 961 // so that we can always skip the entire symbol if we need to navigate 962 // more quickly at the source level when parsing STABS 963 if ( !N_COMM_indexes.empty() ) 964 { 965 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back()); 966 symbol_ptr->SetByteSize(sym_idx + 1); 967 symbol_ptr->SetSizeIsSibling(true); 968 N_COMM_indexes.pop_back(); 969 } 970 type = eSymbolTypeScopeEnd; 971 break; 972 973 case StabLength: 974 // N_LENG - second stab entry with length information 975 type = eSymbolTypeAdditional; 976 break; 977 978 default: break; 979 } 980 } 981 else 982 { 983 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type; 984 uint8_t n_type = NlistMaskType & nlist.n_type; 985 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0); 986 987 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name) 988 { 989 type = eSymbolTypeRuntime; 990 } 991 else 992 { 993 switch (n_type) 994 { 995 case NListTypeIndirect: // N_INDR - Fall through 996 case NListTypePreboundUndefined:// N_PBUD - Fall through 997 case NListTypeUndefined: // N_UNDF 998 type = eSymbolTypeExtern; 999 break; 1000 1001 case NListTypeAbsolute: // N_ABS 1002 type = eSymbolTypeAbsolute; 1003 break; 1004 1005 case NListTypeSection: // N_SECT 1006 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 1007 1008 assert(symbol_section != NULL); 1009 if (TEXT_eh_frame_sectID == nlist.n_sect) 1010 { 1011 type = eSymbolTypeException; 1012 } 1013 else 1014 { 1015 uint32_t section_type = symbol_section->GetAllFlagBits() & SectionFlagMaskSectionType; 1016 1017 switch (section_type) 1018 { 1019 case SectionTypeRegular: break; // regular section 1020 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section 1021 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings 1022 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals 1023 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals 1024 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals 1025 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers 1026 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers 1027 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field 1028 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization 1029 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination 1030 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced 1031 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes) 1032 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing 1033 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals 1034 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break; 1035 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break; 1036 default: break; 1037 } 1038 1039 if (type == eSymbolTypeInvalid) 1040 { 1041 const char *symbol_sect_name = symbol_section->GetName().AsCString(); 1042 if (symbol_section->IsDescendant (text_section_sp.get())) 1043 { 1044 if (symbol_section->IsClear(SectionAttrUserPureInstructions | 1045 SectionAttrUserSelfModifyingCode | 1046 SectionAttrSytemSomeInstructions)) 1047 type = eSymbolTypeData; 1048 else 1049 type = eSymbolTypeCode; 1050 } 1051 else 1052 if (symbol_section->IsDescendant(data_section_sp.get())) 1053 { 1054 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name) 1055 { 1056 type = eSymbolTypeRuntime; 1057 } 1058 else 1059 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name) 1060 { 1061 type = eSymbolTypeException; 1062 } 1063 else 1064 { 1065 type = eSymbolTypeData; 1066 } 1067 } 1068 else 1069 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name) 1070 { 1071 type = eSymbolTypeTrampoline; 1072 } 1073 else 1074 if (symbol_section->IsDescendant(objc_section_sp.get())) 1075 { 1076 type = eSymbolTypeRuntime; 1077 } 1078 } 1079 } 1080 break; 1081 } 1082 } 1083 } 1084 1085 if (add_nlist) 1086 { 1087 bool symbol_name_is_mangled = false; 1088 if (symbol_name && symbol_name[0] == '_') 1089 { 1090 symbol_name_is_mangled = symbol_name[1] == '_'; 1091 symbol_name++; // Skip the leading underscore 1092 } 1093 uint64_t symbol_value = nlist.n_value; 1094 if (symbol_section != NULL) 1095 symbol_value -= symbol_section->GetFileAddress(); 1096 1097 sym[sym_idx].SetID (nlist_idx); 1098 sym[sym_idx].SetType (type); 1099 if (symbol_name) 1100 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled); 1101 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section); 1102 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value); 1103 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); 1104 1105 ++sym_idx; 1106 } 1107 else 1108 { 1109 sym[sym_idx].Clear(); 1110 } 1111 1112 } 1113 1114 1115 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value 1116 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all 1117 // such entries by figuring out what the address for the global is by looking up this non-STAB 1118 // entry and copying the value into the debug symbol's value to save us the hassle in the 1119 // debug symbol parser. 1120 1121 Symbol *global_symbol = NULL; 1122 for (nlist_idx = 0; 1123 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType(eSymbolTypeGlobal, nlist_idx)) != NULL; 1124 nlist_idx++) 1125 { 1126 if (global_symbol->GetValue().GetFileAddress() == 0) 1127 { 1128 std::vector<uint32_t> indexes; 1129 if (symtab->AppendSymbolIndexesWithName(global_symbol->GetMangled().GetName(), indexes) > 0) 1130 { 1131 std::vector<uint32_t>::const_iterator pos; 1132 std::vector<uint32_t>::const_iterator end = indexes.end(); 1133 for (pos = indexes.begin(); pos != end; ++pos) 1134 { 1135 symbol_ptr = symtab->SymbolAtIndex(*pos); 1136 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false) 1137 { 1138 global_symbol->SetValue(symbol_ptr->GetValue()); 1139 break; 1140 } 1141 } 1142 } 1143 } 1144 } 1145 // Now synthesize indirect symbols 1146 if (m_dysymtab.nindirectsyms != 0) 1147 { 1148 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4)); 1149 1150 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize()) 1151 { 1152 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize()); 1153 1154 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx) 1155 { 1156 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs) 1157 { 1158 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2; 1159 if (symbol_stub_byte_size == 0) 1160 continue; 1161 1162 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size; 1163 1164 if (num_symbol_stubs == 0) 1165 continue; 1166 1167 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1; 1168 uint32_t stub_sym_id = symtab_load_command.nsyms; 1169 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) 1170 { 1171 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx; 1172 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size); 1173 uint32_t symbol_stub_offset = symbol_stub_index * 4; 1174 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4)) 1175 { 1176 const uint32_t symbol_index = indirect_symbol_index_data.GetU32 (&symbol_stub_offset); 1177 1178 Symbol *stub_symbol = symtab->SymbolAtIndex(symbol_index); 1179 if (stub_symbol) 1180 { 1181 Address so_addr(symbol_stub_addr, section_list); 1182 1183 if (stub_symbol->GetType() == eSymbolTypeExtern) 1184 { 1185 // Change the external symbol into a trampoline that makes sense 1186 // These symbols were N_UNDF N_EXT, and are useless to us, so we 1187 // can re-use them so we don't have to make up a synthetic symbol 1188 // for no good reason. 1189 stub_symbol->SetType (eSymbolTypeTrampoline); 1190 stub_symbol->SetExternal (false); 1191 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr; 1192 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size); 1193 } 1194 else 1195 { 1196 // Make a synthetic symbol to describe the trampoline stub 1197 if (sym_idx >= num_syms) 1198 { 1199 sym = symtab->Resize (num_syms + 16); 1200 num_syms = symtab->GetNumSymbols(); 1201 } 1202 sym[sym_idx].SetID (stub_sym_id++); 1203 sym[sym_idx].GetMangled() = stub_symbol->GetMangled(); 1204 sym[sym_idx].SetType (eSymbolTypeTrampoline); 1205 sym[sym_idx].SetIsSynthetic (true); 1206 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr; 1207 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size); 1208 ++sym_idx; 1209 } 1210 } 1211 } 1212 } 1213 } 1214 } 1215 } 1216 } 1217 1218 if (sym_idx != symtab->GetNumSymbols()) 1219 symtab->Resize (sym_idx); 1220 1221 return symtab->GetNumSymbols(); 1222 } 1223 } 1224 offset = cmd_offset + symtab_load_command.cmdsize; 1225 } 1226 return 0; 1227} 1228 1229 1230void 1231ObjectFileMachO::Dump (Stream *s) 1232{ 1233 lldb_private::Mutex::Locker locker(m_mutex); 1234 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 1235 s->Indent(); 1236 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped) 1237 s->PutCString("ObjectFileMachO64"); 1238 else 1239 s->PutCString("ObjectFileMachO32"); 1240 1241 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); 1242 1243 *s << ", file = '" << m_file << "', arch = " << header_arch.AsCString() << "\n"; 1244 1245 if (m_sections_ap.get()) 1246 m_sections_ap->Dump(s, NULL, true); 1247 1248 if (m_symtab_ap.get()) 1249 m_symtab_ap->Dump(s, NULL); 1250} 1251 1252 1253bool 1254ObjectFileMachO::GetUUID (UUID* uuid) 1255{ 1256 lldb_private::Mutex::Locker locker(m_mutex); 1257 struct uuid_command load_cmd; 1258 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 1259 uint32_t i; 1260 for (i=0; i<m_header.ncmds; ++i) 1261 { 1262 const uint32_t cmd_offset = offset; 1263 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 1264 break; 1265 1266 if (load_cmd.cmd == LoadCommandUUID) 1267 { 1268 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16); 1269 if (uuid_bytes) 1270 { 1271 uuid->SetBytes (uuid_bytes); 1272 return true; 1273 } 1274 return false; 1275 } 1276 offset = cmd_offset + load_cmd.cmdsize; 1277 } 1278 return false; 1279} 1280 1281 1282uint32_t 1283ObjectFileMachO::GetDependentModules (FileSpecList& files) 1284{ 1285 lldb_private::Mutex::Locker locker(m_mutex); 1286 struct load_command load_cmd; 1287 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 1288 uint32_t count = 0; 1289 uint32_t i; 1290 for (i=0; i<m_header.ncmds; ++i) 1291 { 1292 const uint32_t cmd_offset = offset; 1293 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 1294 break; 1295 1296 switch (load_cmd.cmd) 1297 { 1298 case LoadCommandDylibLoad: 1299 case LoadCommandDylibLoadWeak: 1300 case LoadCommandDylibReexport: 1301 case LoadCommandDynamicLinkerLoad: 1302 case LoadCommandFixedVMShlibLoad: 1303 { 1304 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset); 1305 const char *path = m_data.PeekCStr(name_offset); 1306 // Skip any path that starts with '@' since these are usually: 1307 // @executable_path/.../file 1308 // @rpath/.../file 1309 if (path && path[0] != '@') 1310 { 1311 FileSpec file_spec(path); 1312 if (files.AppendIfUnique(file_spec)) 1313 count++; 1314 } 1315 } 1316 break; 1317 1318 default: 1319 break; 1320 } 1321 offset = cmd_offset + load_cmd.cmdsize; 1322 } 1323 return count; 1324} 1325 1326bool 1327ObjectFileMachO::GetTargetTriple (ConstString &target_triple) 1328{ 1329 lldb_private::Mutex::Locker locker(m_mutex); 1330 std::string triple(GetModule()->GetArchitecture().AsCString()); 1331 triple += "-apple-darwin"; 1332 target_triple.SetCString(triple.c_str()); 1333 if (target_triple) 1334 return true; 1335 return false; 1336} 1337 1338 1339//------------------------------------------------------------------ 1340// PluginInterface protocol 1341//------------------------------------------------------------------ 1342const char * 1343ObjectFileMachO::GetPluginName() 1344{ 1345 return "ObjectFileMachO"; 1346} 1347 1348const char * 1349ObjectFileMachO::GetShortPluginName() 1350{ 1351 return GetPluginNameStatic(); 1352} 1353 1354uint32_t 1355ObjectFileMachO::GetPluginVersion() 1356{ 1357 return 1; 1358} 1359 1360void 1361ObjectFileMachO::GetPluginCommandHelp (const char *command, Stream *strm) 1362{ 1363} 1364 1365Error 1366ObjectFileMachO::ExecutePluginCommand (Args &command, Stream *strm) 1367{ 1368 Error error; 1369 error.SetErrorString("No plug-in command are currently supported."); 1370 return error; 1371} 1372 1373Log * 1374ObjectFileMachO::EnablePluginLogging (Stream *strm, Args &command) 1375{ 1376 return NULL; 1377} 1378 1379 1380 1381 1382