ObjectFileMachO.cpp revision 3fed8b9b2696fc2ea78005c8f9b1c621d5748042
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 (true); 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) if we don't have any 281 // mach sections yet... 282 if (m_mach_sections.empty()) 283 m_mach_sections.push_back(sect64); 284 uint32_t segment_sect_idx; 285 const lldb::user_id_t first_segment_sectID = sectID + 1; 286 287 288 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8; 289 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx) 290 { 291 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL) 292 break; 293 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL) 294 break; 295 sect64.addr = m_data.GetAddress(&offset); 296 sect64.size = m_data.GetAddress(&offset); 297 298 if (m_data.GetU32(&offset, §64.offset, num_u32s) == NULL) 299 break; 300 301 // Keep a list of mach sections around in case we need to 302 // get at data that isn't stored in the abstracted Sections. 303 m_mach_sections.push_back (sect64); 304 305 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname))); 306 if (!segment_name) 307 { 308 // We have a segment with no name so we need to conjure up 309 // segments that correspond to the section's segname if there 310 // isn't already such a section. If there is such a section, 311 // we resize the section so that it spans all sections. 312 // We also mark these sections as fake so address matches don't 313 // hit if they land in the gaps between the child sections. 314 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname)); 315 segment_sp = m_sections_ap->FindSectionByName (segment_name); 316 if (segment_sp.get()) 317 { 318 Section *segment = segment_sp.get(); 319 // Grow the section size as needed. 320 const lldb::addr_t sect64_min_addr = sect64.addr; 321 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size; 322 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize(); 323 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress(); 324 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size; 325 if (sect64_min_addr >= curr_seg_min_addr) 326 { 327 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr; 328 // Only grow the section size if needed 329 if (new_seg_byte_size > curr_seg_byte_size) 330 segment->SetByteSize (new_seg_byte_size); 331 } 332 else 333 { 334 // We need to change the base address of the segment and 335 // adjust the child section offsets for all existing children. 336 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr; 337 segment->Slide(slide_amount, false); 338 segment->GetChildren().Slide (-slide_amount, false); 339 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr); 340 } 341 342 // Grow the section size as needed. 343 if (sect64.offset) 344 { 345 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset(); 346 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize(); 347 348 const lldb::addr_t section_min_file_offset = sect64.offset; 349 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size; 350 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset); 351 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset; 352 segment->SetFileOffset (new_file_offset); 353 segment->SetFileSize (new_file_size); 354 } 355 } 356 else 357 { 358 // Create a fake section for the section's named segment 359 segment_sp.reset(new Section(segment_sp.get(), // Parent section 360 GetModule(), // Module to which this section belongs 361 ++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 362 segment_name, // Name of this section 363 eSectionTypeContainer, // This section is a container of other sections. 364 sect64.addr, // File VM address == addresses as they are found in the object file 365 sect64.size, // VM size in bytes of this section 366 sect64.offset, // Offset to the data for this section in the file 367 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file 368 load_cmd.flags)); // Flags for this section 369 segment_sp->SetIsFake(true); 370 m_sections_ap->AddSection(segment_sp); 371 } 372 } 373 assert (segment_sp.get()); 374 375 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType; 376 static ConstString g_sect_name_objc_data ("__objc_data"); 377 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs"); 378 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs"); 379 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs"); 380 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs"); 381 static ConstString g_sect_name_objc_const ("__objc_const"); 382 static ConstString g_sect_name_objc_classlist ("__objc_classlist"); 383 static ConstString g_sect_name_cfstring ("__cfstring"); 384 385 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev"); 386 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges"); 387 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame"); 388 static ConstString g_sect_name_dwarf_debug_info ("__debug_info"); 389 static ConstString g_sect_name_dwarf_debug_line ("__debug_line"); 390 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc"); 391 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo"); 392 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames"); 393 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes"); 394 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges"); 395 static ConstString g_sect_name_dwarf_debug_str ("__debug_str"); 396 static ConstString g_sect_name_eh_frame ("__eh_frame"); 397 static ConstString g_sect_name_DATA ("__DATA"); 398 static ConstString g_sect_name_TEXT ("__TEXT"); 399 400 SectionType sect_type = eSectionTypeOther; 401 402 if (section_name == g_sect_name_dwarf_debug_abbrev) 403 sect_type = eSectionTypeDWARFDebugAbbrev; 404 else if (section_name == g_sect_name_dwarf_debug_aranges) 405 sect_type = eSectionTypeDWARFDebugAranges; 406 else if (section_name == g_sect_name_dwarf_debug_frame) 407 sect_type = eSectionTypeDWARFDebugFrame; 408 else if (section_name == g_sect_name_dwarf_debug_info) 409 sect_type = eSectionTypeDWARFDebugInfo; 410 else if (section_name == g_sect_name_dwarf_debug_line) 411 sect_type = eSectionTypeDWARFDebugLine; 412 else if (section_name == g_sect_name_dwarf_debug_loc) 413 sect_type = eSectionTypeDWARFDebugLoc; 414 else if (section_name == g_sect_name_dwarf_debug_macinfo) 415 sect_type = eSectionTypeDWARFDebugMacInfo; 416 else if (section_name == g_sect_name_dwarf_debug_pubnames) 417 sect_type = eSectionTypeDWARFDebugPubNames; 418 else if (section_name == g_sect_name_dwarf_debug_pubtypes) 419 sect_type = eSectionTypeDWARFDebugPubTypes; 420 else if (section_name == g_sect_name_dwarf_debug_ranges) 421 sect_type = eSectionTypeDWARFDebugRanges; 422 else if (section_name == g_sect_name_dwarf_debug_str) 423 sect_type = eSectionTypeDWARFDebugStr; 424 else if (section_name == g_sect_name_objc_selrefs) 425 sect_type = eSectionTypeDataCStringPointers; 426 else if (section_name == g_sect_name_objc_msgrefs) 427 sect_type = eSectionTypeDataObjCMessageRefs; 428 else if (section_name == g_sect_name_eh_frame) 429 sect_type = eSectionTypeEHFrame; 430 else if (section_name == g_sect_name_cfstring) 431 sect_type = eSectionTypeDataObjCCFStrings; 432 else if (section_name == g_sect_name_objc_data || 433 section_name == g_sect_name_objc_classrefs || 434 section_name == g_sect_name_objc_superrefs || 435 section_name == g_sect_name_objc_const || 436 section_name == g_sect_name_objc_classlist) 437 { 438 sect_type = eSectionTypeDataPointers; 439 } 440 441 if (sect_type == eSectionTypeOther) 442 { 443 switch (mach_sect_type) 444 { 445 // TODO: categorize sections by other flags for regular sections 446 case SectionTypeRegular: 447 if (segment_sp->GetName() == g_sect_name_TEXT) 448 sect_type = eSectionTypeCode; 449 else if (segment_sp->GetName() == g_sect_name_DATA) 450 sect_type = eSectionTypeData; 451 else 452 sect_type = eSectionTypeOther; 453 break; 454 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break; 455 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings 456 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals 457 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals 458 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals 459 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers 460 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers 461 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field 462 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization 463 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination 464 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break; 465 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break; 466 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing 467 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals 468 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break; 469 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break; 470 default: break; 471 } 472 } 473 474 SectionSP section_sp(new Section(segment_sp.get(), 475 GetModule(), 476 ++sectID, 477 section_name, 478 sect_type, 479 sect64.addr - segment_sp->GetFileAddress(), 480 sect64.size, 481 sect64.offset, 482 sect64.offset == 0 ? 0 : sect64.size, 483 sect64.flags)); 484 segment_sp->GetChildren().AddSection(section_sp); 485 486 if (segment_sp->IsFake()) 487 { 488 segment_sp.reset(); 489 segment_name.Clear(); 490 } 491 } 492 if (m_header.filetype == HeaderFileTypeDSYM) 493 { 494 if (first_segment_sectID <= sectID) 495 { 496 lldb::user_id_t sect_uid; 497 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid) 498 { 499 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid)); 500 SectionSP next_section_sp; 501 if (sect_uid + 1 <= sectID) 502 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1); 503 504 if (curr_section_sp.get()) 505 { 506 if (curr_section_sp->GetByteSize() == 0) 507 { 508 if (next_section_sp.get() != NULL) 509 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() ); 510 else 511 curr_section_sp->SetByteSize ( load_cmd.vmsize ); 512 } 513 } 514 } 515 } 516 } 517 } 518 } 519 } 520 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo) 521 { 522 m_dysymtab.cmd = load_cmd.cmd; 523 m_dysymtab.cmdsize = load_cmd.cmdsize; 524 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2); 525 } 526 527 offset = load_cmd_offset + load_cmd.cmdsize; 528 } 529// if (dump_sections) 530// { 531// StreamFile s(stdout); 532// m_sections_ap->Dump(&s, true); 533// } 534 return sectID; // Return the number of sections we registered with the module 535} 536 537class MachSymtabSectionInfo 538{ 539public: 540 541 MachSymtabSectionInfo (SectionList *section_list) : 542 m_section_list (section_list), 543 m_section_infos() 544 { 545 // Get the number of sections down to a depth of 1 to include 546 // all segments and their sections, but no other sections that 547 // may be added for debug map or 548 m_section_infos.resize(section_list->GetNumSections(1)); 549 } 550 551 552 Section * 553 GetSection (uint8_t n_sect, addr_t file_addr) 554 { 555 if (n_sect == 0) 556 return NULL; 557 if (n_sect < m_section_infos.size()) 558 { 559 if (m_section_infos[n_sect].section == NULL) 560 { 561 Section *section = m_section_list->FindSectionByID (n_sect).get(); 562 m_section_infos[n_sect].section = section; 563 assert (section != NULL); 564 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress()); 565 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize()); 566 } 567 if (m_section_infos[n_sect].vm_range.Contains(file_addr)) 568 return m_section_infos[n_sect].section; 569 } 570 return m_section_list->FindSectionContainingFileAddress(file_addr).get(); 571 } 572 573protected: 574 struct SectionInfo 575 { 576 SectionInfo () : 577 vm_range(), 578 section (NULL) 579 { 580 } 581 582 VMRange vm_range; 583 Section *section; 584 }; 585 SectionList *m_section_list; 586 std::vector<SectionInfo> m_section_infos; 587}; 588 589 590 591size_t 592ObjectFileMachO::ParseSymtab (bool minimize) 593{ 594 Timer scoped_timer(__PRETTY_FUNCTION__, 595 "ObjectFileMachO::ParseSymtab () module = %s", 596 m_file.GetFilename().AsCString("")); 597 struct symtab_command symtab_load_command; 598 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 599 uint32_t i; 600 for (i=0; i<m_header.ncmds; ++i) 601 { 602 const uint32_t cmd_offset = offset; 603 // Read in the load command and load command size 604 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL) 605 break; 606 // Watch for the symbol table load command 607 if (symtab_load_command.cmd == LoadCommandSymtab) 608 { 609 // Read in the rest of the symtab load command 610 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields 611 { 612 Symtab *symtab = m_symtab_ap.get(); 613 SectionList *section_list = GetSectionList(); 614 assert(section_list); 615 const size_t addr_size = m_data.GetAddressByteSize(); 616 const ByteOrder endian = m_data.GetByteOrder(); 617 bool bit_width_32 = addr_size == 4; 618 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64); 619 620 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size)); 621 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize)); 622 623 const char *strtab_data = (const char *)strtab_data_sp->GetBytes(); 624// DataExtractor symtab_data(symtab_data_sp, endian, addr_size); 625// DataExtractor strtab_data(strtab_data_sp, endian, addr_size); 626 627 static ConstString g_segment_name_TEXT ("__TEXT"); 628 static ConstString g_segment_name_DATA ("__DATA"); 629 static ConstString g_segment_name_OBJC ("__OBJC"); 630 static ConstString g_section_name_eh_frame ("__eh_frame"); 631 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT)); 632 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA)); 633 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC)); 634 SectionSP eh_frame_section_sp; 635 if (text_section_sp.get()) 636 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame); 637 else 638 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame); 639 640 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection; 641 //uint32_t symtab_offset = 0; 642 const uint8_t* nlist_data = symtab_data_sp->GetBytes(); 643 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms); 644 645 646 if (endian != eByteOrderHost) 647 { 648 // ... 649 assert (!"UNIMPLEMENTED: Swap all nlist entries"); 650 } 651 uint32_t N_SO_index = UINT32_MAX; 652 653 MachSymtabSectionInfo section_info (section_list); 654 std::vector<uint32_t> N_FUN_indexes; 655 std::vector<uint32_t> N_NSYM_indexes; 656 std::vector<uint32_t> N_INCL_indexes; 657 std::vector<uint32_t> N_BRAC_indexes; 658 std::vector<uint32_t> N_COMM_indexes; 659 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap; 660 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap; 661 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx; 662 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx; 663 // Any symbols that get merged into another will get an entry 664 // in this map so we know 665 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx; 666 uint32_t nlist_idx = 0; 667 Symbol *symbol_ptr = NULL; 668 669 uint32_t sym_idx = 0; 670 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); 671 uint32_t num_syms = symtab->GetNumSymbols(); 672 673 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); 674 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) 675 { 676 struct nlist_64 nlist; 677 if (bit_width_32) 678 { 679 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size)); 680 nlist.n_strx = nlist32_ptr->n_strx; 681 nlist.n_type = nlist32_ptr->n_type; 682 nlist.n_sect = nlist32_ptr->n_sect; 683 nlist.n_desc = nlist32_ptr->n_desc; 684 nlist.n_value = nlist32_ptr->n_value; 685 } 686 else 687 { 688 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size))); 689 } 690 691 SymbolType type = eSymbolTypeInvalid; 692 const char* symbol_name = &strtab_data[nlist.n_strx]; 693 if (symbol_name[0] == '\0') 694 symbol_name = NULL; 695 Section* symbol_section = NULL; 696 bool add_nlist = true; 697 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0); 698 699 assert (sym_idx < num_syms); 700 701 sym[sym_idx].SetDebug (is_debug); 702 703 if (is_debug) 704 { 705 switch (nlist.n_type) 706 { 707 case StabGlobalSymbol: 708 // N_GSYM -- global symbol: name,,NO_SECT,type,0 709 // Sometimes the N_GSYM value contains the address. 710 sym[sym_idx].SetExternal(true); 711 if (nlist.n_value != 0) 712 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 713 type = eSymbolTypeData; 714 break; 715 716 case StabFunctionName: 717 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0 718 type = eSymbolTypeCompiler; 719 break; 720 721 case StabFunction: 722 // N_FUN -- procedure: name,,n_sect,linenumber,address 723 if (symbol_name) 724 { 725 type = eSymbolTypeCode; 726 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 727 728 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx; 729 // We use the current number of symbols in the symbol table in lieu of 730 // using nlist_idx in case we ever start trimming entries out 731 N_FUN_indexes.push_back(sym_idx); 732 } 733 else 734 { 735 type = eSymbolTypeCompiler; 736 737 if ( !N_FUN_indexes.empty() ) 738 { 739 // Copy the size of the function into the original STAB entry so we don't have 740 // to hunt for it later 741 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value); 742 N_FUN_indexes.pop_back(); 743 // We don't really need the end function STAB as it contains the size which 744 // we already placed with the original symbol, so don't add it if we want a 745 // minimal symbol table 746 if (minimize) 747 add_nlist = false; 748 } 749 } 750 break; 751 752 case StabStaticSymbol: 753 // N_STSYM -- static symbol: name,,n_sect,type,address 754 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx; 755 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 756 type = eSymbolTypeData; 757 break; 758 759 case StabLocalCommon: 760 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address 761 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 762 type = eSymbolTypeCommonBlock; 763 break; 764 765 case StabBeginSymbol: 766 // N_BNSYM 767 // We use the current number of symbols in the symbol table in lieu of 768 // using nlist_idx in case we ever start trimming entries out 769 if (minimize) 770 { 771 // Skip these if we want minimal symbol tables 772 add_nlist = false; 773 } 774 else 775 { 776 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 777 N_NSYM_indexes.push_back(sym_idx); 778 type = eSymbolTypeScopeBegin; 779 } 780 break; 781 782 case StabEndSymbol: 783 // N_ENSYM 784 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM 785 // so that we can always skip the entire symbol if we need to navigate 786 // more quickly at the source level when parsing STABS 787 if (minimize) 788 { 789 // Skip these if we want minimal symbol tables 790 add_nlist = false; 791 } 792 else 793 { 794 if ( !N_NSYM_indexes.empty() ) 795 { 796 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back()); 797 symbol_ptr->SetByteSize(sym_idx + 1); 798 symbol_ptr->SetSizeIsSibling(true); 799 N_NSYM_indexes.pop_back(); 800 } 801 type = eSymbolTypeScopeEnd; 802 } 803 break; 804 805 806 case StabSourceFileOptions: 807 // N_OPT - emitted with gcc2_compiled and in gcc source 808 type = eSymbolTypeCompiler; 809 break; 810 811 case StabRegisterSymbol: 812 // N_RSYM - register sym: name,,NO_SECT,type,register 813 type = eSymbolTypeVariable; 814 break; 815 816 case StabSourceLine: 817 // N_SLINE - src line: 0,,n_sect,linenumber,address 818 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 819 type = eSymbolTypeLineEntry; 820 break; 821 822 case StabStructureType: 823 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset 824 type = eSymbolTypeVariableType; 825 break; 826 827 case StabSourceFileName: 828 // N_SO - source file name 829 type = eSymbolTypeSourceFile; 830 if (symbol_name == NULL) 831 { 832 if (minimize) 833 add_nlist = false; 834 if (N_SO_index != UINT32_MAX) 835 { 836 // Set the size of the N_SO to the terminating index of this N_SO 837 // so that we can always skip the entire N_SO if we need to navigate 838 // more quickly at the source level when parsing STABS 839 symbol_ptr = symtab->SymbolAtIndex(N_SO_index); 840 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1)); 841 symbol_ptr->SetSizeIsSibling(true); 842 } 843 N_NSYM_indexes.clear(); 844 N_INCL_indexes.clear(); 845 N_BRAC_indexes.clear(); 846 N_COMM_indexes.clear(); 847 N_FUN_indexes.clear(); 848 N_SO_index = UINT32_MAX; 849 } 850 else 851 { 852 // We use the current number of symbols in the symbol table in lieu of 853 // using nlist_idx in case we ever start trimming entries out 854 if (symbol_name[0] == '/') 855 N_SO_index = sym_idx; 856 else if (minimize && (N_SO_index == sym_idx - 1)) 857 { 858 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString(); 859 if (so_path && so_path[0]) 860 { 861 std::string full_so_path (so_path); 862 if (*full_so_path.rbegin() != '/') 863 full_so_path += '/'; 864 full_so_path += symbol_name; 865 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false); 866 add_nlist = false; 867 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; 868 } 869 } 870 } 871 872 break; 873 874 case StabObjectFileName: 875 // N_OSO - object file name: name,,0,0,st_mtime 876 type = eSymbolTypeObjectFile; 877 break; 878 879 case StabLocalSymbol: 880 // N_LSYM - local sym: name,,NO_SECT,type,offset 881 type = eSymbolTypeLocal; 882 break; 883 884 //---------------------------------------------------------------------- 885 // INCL scopes 886 //---------------------------------------------------------------------- 887 case StabBeginIncludeFileName: 888 // N_BINCL - include file beginning: name,,NO_SECT,0,sum 889 // We use the current number of symbols in the symbol table in lieu of 890 // using nlist_idx in case we ever start trimming entries out 891 N_INCL_indexes.push_back(sym_idx); 892 type = eSymbolTypeScopeBegin; 893 break; 894 895 case StabEndIncludeFile: 896 // N_EINCL - include file end: name,,NO_SECT,0,0 897 // Set the size of the N_BINCL to the terminating index of this N_EINCL 898 // so that we can always skip the entire symbol if we need to navigate 899 // more quickly at the source level when parsing STABS 900 if ( !N_INCL_indexes.empty() ) 901 { 902 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back()); 903 symbol_ptr->SetByteSize(sym_idx + 1); 904 symbol_ptr->SetSizeIsSibling(true); 905 N_INCL_indexes.pop_back(); 906 } 907 type = eSymbolTypeScopeEnd; 908 break; 909 910 case StabIncludeFileName: 911 // N_SOL - #included file name: name,,n_sect,0,address 912 type = eSymbolTypeHeaderFile; 913 914 // We currently don't use the header files on darwin 915 if (minimize) 916 add_nlist = false; 917 break; 918 919 case StabCompilerParameters: 920 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0 921 type = eSymbolTypeCompiler; 922 break; 923 924 case StabCompilerVersion: 925 // N_VERSION - compiler version: name,,NO_SECT,0,0 926 type = eSymbolTypeCompiler; 927 break; 928 929 case StabCompilerOptLevel: 930 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0 931 type = eSymbolTypeCompiler; 932 break; 933 934 case StabParameter: 935 // N_PSYM - parameter: name,,NO_SECT,type,offset 936 type = eSymbolTypeVariable; 937 break; 938 939 case StabAlternateEntry: 940 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address 941 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 942 type = eSymbolTypeLineEntry; 943 break; 944 945 //---------------------------------------------------------------------- 946 // Left and Right Braces 947 //---------------------------------------------------------------------- 948 case StabLeftBracket: 949 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address 950 // We use the current number of symbols in the symbol table in lieu of 951 // using nlist_idx in case we ever start trimming entries out 952 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 953 N_BRAC_indexes.push_back(sym_idx); 954 type = eSymbolTypeScopeBegin; 955 break; 956 957 case StabRightBracket: 958 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address 959 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC 960 // so that we can always skip the entire symbol if we need to navigate 961 // more quickly at the source level when parsing STABS 962 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 963 if ( !N_BRAC_indexes.empty() ) 964 { 965 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back()); 966 symbol_ptr->SetByteSize(sym_idx + 1); 967 symbol_ptr->SetSizeIsSibling(true); 968 N_BRAC_indexes.pop_back(); 969 } 970 type = eSymbolTypeScopeEnd; 971 break; 972 973 case StabDeletedIncludeFile: 974 // N_EXCL - deleted include file: name,,NO_SECT,0,sum 975 type = eSymbolTypeHeaderFile; 976 break; 977 978 //---------------------------------------------------------------------- 979 // COMM scopes 980 //---------------------------------------------------------------------- 981 case StabBeginCommon: 982 // N_BCOMM - begin common: name,,NO_SECT,0,0 983 // We use the current number of symbols in the symbol table in lieu of 984 // using nlist_idx in case we ever start trimming entries out 985 type = eSymbolTypeScopeBegin; 986 N_COMM_indexes.push_back(sym_idx); 987 break; 988 989 case StabEndCommonLocal: 990 // N_ECOML - end common (local name): 0,,n_sect,0,address 991 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 992 // Fall through 993 994 case StabEndCommon: 995 // N_ECOMM - end common: name,,n_sect,0,0 996 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML 997 // so that we can always skip the entire symbol if we need to navigate 998 // more quickly at the source level when parsing STABS 999 if ( !N_COMM_indexes.empty() ) 1000 { 1001 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back()); 1002 symbol_ptr->SetByteSize(sym_idx + 1); 1003 symbol_ptr->SetSizeIsSibling(true); 1004 N_COMM_indexes.pop_back(); 1005 } 1006 type = eSymbolTypeScopeEnd; 1007 break; 1008 1009 case StabLength: 1010 // N_LENG - second stab entry with length information 1011 type = eSymbolTypeAdditional; 1012 break; 1013 1014 default: break; 1015 } 1016 } 1017 else 1018 { 1019 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type; 1020 uint8_t n_type = NlistMaskType & nlist.n_type; 1021 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0); 1022 1023 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name) 1024 { 1025 type = eSymbolTypeRuntime; 1026 } 1027 else 1028 { 1029 switch (n_type) 1030 { 1031 case NListTypeIndirect: // N_INDR - Fall through 1032 case NListTypePreboundUndefined:// N_PBUD - Fall through 1033 case NListTypeUndefined: // N_UNDF 1034 type = eSymbolTypeExtern; 1035 break; 1036 1037 case NListTypeAbsolute: // N_ABS 1038 type = eSymbolTypeAbsolute; 1039 break; 1040 1041 case NListTypeSection: // N_SECT 1042 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 1043 1044 assert(symbol_section != NULL); 1045 if (TEXT_eh_frame_sectID == nlist.n_sect) 1046 { 1047 type = eSymbolTypeException; 1048 } 1049 else 1050 { 1051 uint32_t section_type = symbol_section->GetAllFlagBits() & SectionFlagMaskSectionType; 1052 1053 switch (section_type) 1054 { 1055 case SectionTypeRegular: break; // regular section 1056 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section 1057 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings 1058 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals 1059 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals 1060 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals 1061 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers 1062 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers 1063 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field 1064 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization 1065 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination 1066 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced 1067 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes) 1068 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing 1069 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals 1070 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break; 1071 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break; 1072 default: break; 1073 } 1074 1075 if (type == eSymbolTypeInvalid) 1076 { 1077 const char *symbol_sect_name = symbol_section->GetName().AsCString(); 1078 if (symbol_section->IsDescendant (text_section_sp.get())) 1079 { 1080 if (symbol_section->IsClear(SectionAttrUserPureInstructions | 1081 SectionAttrUserSelfModifyingCode | 1082 SectionAttrSytemSomeInstructions)) 1083 type = eSymbolTypeData; 1084 else 1085 type = eSymbolTypeCode; 1086 } 1087 else 1088 if (symbol_section->IsDescendant(data_section_sp.get())) 1089 { 1090 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name) 1091 { 1092 type = eSymbolTypeRuntime; 1093 } 1094 else 1095 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name) 1096 { 1097 type = eSymbolTypeException; 1098 } 1099 else 1100 { 1101 type = eSymbolTypeData; 1102 } 1103 } 1104 else 1105 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name) 1106 { 1107 type = eSymbolTypeTrampoline; 1108 } 1109 else 1110 if (symbol_section->IsDescendant(objc_section_sp.get())) 1111 { 1112 type = eSymbolTypeRuntime; 1113 } 1114 } 1115 } 1116 break; 1117 } 1118 } 1119 } 1120 if (add_nlist) 1121 { 1122 bool symbol_name_is_mangled = false; 1123 if (symbol_name && symbol_name[0] == '_') 1124 { 1125 symbol_name_is_mangled = symbol_name[1] == '_'; 1126 symbol_name++; // Skip the leading underscore 1127 } 1128 uint64_t symbol_value = nlist.n_value; 1129 1130 if (symbol_name) 1131 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled); 1132 if (is_debug == false) 1133 { 1134 if (type == eSymbolTypeCode) 1135 { 1136 // See if we can find a N_FUN entry for any code symbols. 1137 // If we do find a match, and the name matches, then we 1138 // can merge the two into just the function symbol to avoid 1139 // duplicate entries in the symbol table 1140 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value); 1141 if (pos != N_FUN_addr_to_sym_idx.end()) 1142 { 1143 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || 1144 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) 1145 { 1146 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 1147 // We just need the flags from the linker symbol, so put these flags 1148 // into the N_FUN flags to avoid duplicate symbols in the symbol table 1149 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); 1150 sym[sym_idx].Clear(); 1151 continue; 1152 } 1153 } 1154 } 1155 else if (type == eSymbolTypeData) 1156 { 1157 // See if we can find a N_STSYM entry for any data symbols. 1158 // If we do find a match, and the name matches, then we 1159 // can merge the two into just the Static symbol to avoid 1160 // duplicate entries in the symbol table 1161 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value); 1162 if (pos != N_STSYM_addr_to_sym_idx.end()) 1163 { 1164 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || 1165 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) 1166 { 1167 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 1168 // We just need the flags from the linker symbol, so put these flags 1169 // into the N_STSYM flags to avoid duplicate symbols in the symbol table 1170 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); 1171 sym[sym_idx].Clear(); 1172 continue; 1173 } 1174 } 1175 } 1176 } 1177 if (symbol_section != NULL) 1178 symbol_value -= symbol_section->GetFileAddress(); 1179 1180 sym[sym_idx].SetID (nlist_idx); 1181 sym[sym_idx].SetType (type); 1182 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section); 1183 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value); 1184 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); 1185 1186 ++sym_idx; 1187 } 1188 else 1189 { 1190 sym[sym_idx].Clear(); 1191 } 1192 1193 } 1194 1195 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value 1196 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all 1197 // such entries by figuring out what the address for the global is by looking up this non-STAB 1198 // entry and copying the value into the debug symbol's value to save us the hassle in the 1199 // debug symbol parser. 1200 1201 Symbol *global_symbol = NULL; 1202 for (nlist_idx = 0; 1203 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL; 1204 nlist_idx++) 1205 { 1206 if (global_symbol->GetValue().GetFileAddress() == 0) 1207 { 1208 std::vector<uint32_t> indexes; 1209 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0) 1210 { 1211 std::vector<uint32_t>::const_iterator pos; 1212 std::vector<uint32_t>::const_iterator end = indexes.end(); 1213 for (pos = indexes.begin(); pos != end; ++pos) 1214 { 1215 symbol_ptr = symtab->SymbolAtIndex(*pos); 1216 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false) 1217 { 1218 global_symbol->SetValue(symbol_ptr->GetValue()); 1219 break; 1220 } 1221 } 1222 } 1223 } 1224 } 1225 1226 // Trim our symbols down to just what we ended up with after 1227 // removing any symbols. 1228 if (sym_idx < num_syms) 1229 { 1230 num_syms = sym_idx; 1231 sym = symtab->Resize (num_syms); 1232 } 1233 1234 // Now synthesize indirect symbols 1235 if (m_dysymtab.nindirectsyms != 0) 1236 { 1237 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4)); 1238 1239 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize()) 1240 { 1241 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end(); 1242 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize()); 1243 1244 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx) 1245 { 1246 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs) 1247 { 1248 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2; 1249 if (symbol_stub_byte_size == 0) 1250 continue; 1251 1252 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size; 1253 1254 if (num_symbol_stubs == 0) 1255 continue; 1256 1257 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1; 1258 uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms; 1259 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) 1260 { 1261 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx; 1262 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size); 1263 uint32_t symbol_stub_offset = symbol_stub_index * 4; 1264 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4)) 1265 { 1266 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset); 1267 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal)) 1268 continue; 1269 1270 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id); 1271 Symbol *stub_symbol = NULL; 1272 if (index_pos != end_index_pos) 1273 { 1274 // We have a remapping from the original nlist index to 1275 // a current symbol index, so just look this up by index 1276 stub_symbol = symtab->SymbolAtIndex (index_pos->second); 1277 } 1278 else 1279 { 1280 // We need to lookup a symbol using the original nlist 1281 // symbol index since this index is coming from the 1282 // S_SYMBOL_STUBS 1283 stub_symbol = symtab->FindSymbolByID (stub_sym_id); 1284 } 1285 1286 assert (stub_symbol); 1287 if (stub_symbol) 1288 { 1289 Address so_addr(symbol_stub_addr, section_list); 1290 1291 if (stub_symbol->GetType() == eSymbolTypeExtern) 1292 { 1293 // Change the external symbol into a trampoline that makes sense 1294 // These symbols were N_UNDF N_EXT, and are useless to us, so we 1295 // can re-use them so we don't have to make up a synthetic symbol 1296 // for no good reason. 1297 stub_symbol->SetType (eSymbolTypeTrampoline); 1298 stub_symbol->SetExternal (false); 1299 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr; 1300 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size); 1301 } 1302 else 1303 { 1304 // Make a synthetic symbol to describe the trampoline stub 1305 if (sym_idx >= num_syms) 1306 sym = symtab->Resize (++num_syms); 1307 sym[sym_idx].SetID (synthetic_stub_sym_id++); 1308 sym[sym_idx].GetMangled() = stub_symbol->GetMangled(); 1309 sym[sym_idx].SetType (eSymbolTypeTrampoline); 1310 sym[sym_idx].SetIsSynthetic (true); 1311 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr; 1312 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size); 1313 ++sym_idx; 1314 } 1315 } 1316 } 1317 } 1318 } 1319 } 1320 } 1321 } 1322 1323 return symtab->GetNumSymbols(); 1324 } 1325 } 1326 offset = cmd_offset + symtab_load_command.cmdsize; 1327 } 1328 return 0; 1329} 1330 1331 1332void 1333ObjectFileMachO::Dump (Stream *s) 1334{ 1335 lldb_private::Mutex::Locker locker(m_mutex); 1336 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 1337 s->Indent(); 1338 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped) 1339 s->PutCString("ObjectFileMachO64"); 1340 else 1341 s->PutCString("ObjectFileMachO32"); 1342 1343 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); 1344 1345 *s << ", file = '" << m_file << "', arch = " << header_arch.AsCString() << "\n"; 1346 1347 if (m_sections_ap.get()) 1348 m_sections_ap->Dump(s, NULL, true); 1349 1350 if (m_symtab_ap.get()) 1351 m_symtab_ap->Dump(s, NULL); 1352} 1353 1354 1355bool 1356ObjectFileMachO::GetUUID (UUID* uuid) 1357{ 1358 lldb_private::Mutex::Locker locker(m_mutex); 1359 struct uuid_command load_cmd; 1360 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 1361 uint32_t i; 1362 for (i=0; i<m_header.ncmds; ++i) 1363 { 1364 const uint32_t cmd_offset = offset; 1365 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 1366 break; 1367 1368 if (load_cmd.cmd == LoadCommandUUID) 1369 { 1370 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16); 1371 if (uuid_bytes) 1372 { 1373 uuid->SetBytes (uuid_bytes); 1374 return true; 1375 } 1376 return false; 1377 } 1378 offset = cmd_offset + load_cmd.cmdsize; 1379 } 1380 return false; 1381} 1382 1383 1384uint32_t 1385ObjectFileMachO::GetDependentModules (FileSpecList& files) 1386{ 1387 lldb_private::Mutex::Locker locker(m_mutex); 1388 struct load_command load_cmd; 1389 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 1390 uint32_t count = 0; 1391 uint32_t i; 1392 for (i=0; i<m_header.ncmds; ++i) 1393 { 1394 const uint32_t cmd_offset = offset; 1395 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 1396 break; 1397 1398 switch (load_cmd.cmd) 1399 { 1400 case LoadCommandDylibLoad: 1401 case LoadCommandDylibLoadWeak: 1402 case LoadCommandDylibReexport: 1403 case LoadCommandDynamicLinkerLoad: 1404 case LoadCommandFixedVMShlibLoad: 1405 { 1406 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset); 1407 const char *path = m_data.PeekCStr(name_offset); 1408 // Skip any path that starts with '@' since these are usually: 1409 // @executable_path/.../file 1410 // @rpath/.../file 1411 if (path && path[0] != '@') 1412 { 1413 FileSpec file_spec(path); 1414 if (files.AppendIfUnique(file_spec)) 1415 count++; 1416 } 1417 } 1418 break; 1419 1420 default: 1421 break; 1422 } 1423 offset = cmd_offset + load_cmd.cmdsize; 1424 } 1425 return count; 1426} 1427 1428bool 1429ObjectFileMachO::GetTargetTriple (ConstString &target_triple) 1430{ 1431 lldb_private::Mutex::Locker locker(m_mutex); 1432 std::string triple(GetModule()->GetArchitecture().AsCString()); 1433 triple += "-apple-darwin"; 1434 target_triple.SetCString(triple.c_str()); 1435 if (target_triple) 1436 return true; 1437 return false; 1438} 1439 1440 1441//------------------------------------------------------------------ 1442// PluginInterface protocol 1443//------------------------------------------------------------------ 1444const char * 1445ObjectFileMachO::GetPluginName() 1446{ 1447 return "ObjectFileMachO"; 1448} 1449 1450const char * 1451ObjectFileMachO::GetShortPluginName() 1452{ 1453 return GetPluginNameStatic(); 1454} 1455 1456uint32_t 1457ObjectFileMachO::GetPluginVersion() 1458{ 1459 return 1; 1460} 1461 1462void 1463ObjectFileMachO::GetPluginCommandHelp (const char *command, Stream *strm) 1464{ 1465} 1466 1467Error 1468ObjectFileMachO::ExecutePluginCommand (Args &command, Stream *strm) 1469{ 1470 Error error; 1471 error.SetErrorString("No plug-in command are currently supported."); 1472 return error; 1473} 1474 1475Log * 1476ObjectFileMachO::EnablePluginLogging (Stream *strm, Args &command) 1477{ 1478 return NULL; 1479} 1480 1481 1482 1483 1484