ObjectFileMachO.cpp revision 576a68b11040d567a45dd14bc63590f1b5e0f099
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) 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 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap; 650 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx; 651 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx; 652 uint32_t nlist_idx = 0; 653 Symbol *symbol_ptr = NULL; 654 655 uint32_t sym_idx = 0; 656 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); 657 uint32_t num_syms = symtab->GetNumSymbols(); 658 659 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); 660 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) 661 { 662 struct nlist_64 nlist; 663 if (bit_width_32) 664 { 665 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size)); 666 nlist.n_strx = nlist32_ptr->n_strx; 667 nlist.n_type = nlist32_ptr->n_type; 668 nlist.n_sect = nlist32_ptr->n_sect; 669 nlist.n_desc = nlist32_ptr->n_desc; 670 nlist.n_value = nlist32_ptr->n_value; 671 } 672 else 673 { 674 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size))); 675 } 676 677 SymbolType type = eSymbolTypeInvalid; 678 const char* symbol_name = &strtab_data[nlist.n_strx]; 679 if (symbol_name[0] == '\0') 680 symbol_name = NULL; 681 Section* symbol_section = NULL; 682 bool add_nlist = true; 683 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0); 684 685 assert (sym_idx < num_syms); 686 687 sym[sym_idx].SetDebug (is_debug); 688 689 if (is_debug) 690 { 691 switch (nlist.n_type) 692 { 693 case StabGlobalSymbol: 694 // N_GSYM -- global symbol: name,,NO_SECT,type,0 695 // Sometimes the N_GSYM value contains the address. 696 if (nlist.n_value != 0) 697 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 698 type = eSymbolTypeGlobal; 699 break; 700 701 case StabFunctionName: 702 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0 703 type = eSymbolTypeFunction; 704 break; 705 706 case StabFunction: 707 // N_FUN -- procedure: name,,n_sect,linenumber,address 708 if (symbol_name) 709 { 710 type = eSymbolTypeFunction; 711 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 712 713 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx; 714 // We use the current number of symbols in the symbol table in lieu of 715 // using nlist_idx in case we ever start trimming entries out 716 N_FUN_indexes.push_back(sym_idx); 717 } 718 else 719 { 720 type = eSymbolTypeFunctionEnd; 721 722 if ( !N_FUN_indexes.empty() ) 723 { 724 // Copy the size of the function into the original STAB entry so we don't have 725 // to hunt for it later 726 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value); 727 N_FUN_indexes.pop_back(); 728 // We don't really need the end function STAB as it contains the size which 729 // we already placed with the original symbol, so don't add it if we want a 730 // minimal symbol table 731 if (minimize) 732 add_nlist = false; 733 } 734 } 735 break; 736 737 case StabStaticSymbol: 738 // N_STSYM -- static symbol: name,,n_sect,type,address 739 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx; 740 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 741 type = eSymbolTypeStatic; 742 break; 743 744 case StabLocalCommon: 745 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address 746 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 747 type = eSymbolTypeCommonBlock; 748 break; 749 750 case StabBeginSymbol: 751 // N_BNSYM 752 // We use the current number of symbols in the symbol table in lieu of 753 // using nlist_idx in case we ever start trimming entries out 754 if (minimize) 755 { 756 // Skip these if we want minimal symbol tables 757 add_nlist = false; 758 } 759 else 760 { 761 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 762 N_NSYM_indexes.push_back(sym_idx); 763 type = eSymbolTypeScopeBegin; 764 } 765 break; 766 767 case StabEndSymbol: 768 // N_ENSYM 769 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM 770 // so that we can always skip the entire symbol if we need to navigate 771 // more quickly at the source level when parsing STABS 772 if (minimize) 773 { 774 // Skip these if we want minimal symbol tables 775 add_nlist = false; 776 } 777 else 778 { 779 if ( !N_NSYM_indexes.empty() ) 780 { 781 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back()); 782 symbol_ptr->SetByteSize(sym_idx + 1); 783 symbol_ptr->SetSizeIsSibling(true); 784 N_NSYM_indexes.pop_back(); 785 } 786 type = eSymbolTypeScopeEnd; 787 } 788 break; 789 790 791 case StabSourceFileOptions: 792 // N_OPT - emitted with gcc2_compiled and in gcc source 793 type = eSymbolTypeCompiler; 794 break; 795 796 case StabRegisterSymbol: 797 // N_RSYM - register sym: name,,NO_SECT,type,register 798 type = eSymbolTypeVariable; 799 break; 800 801 case StabSourceLine: 802 // N_SLINE - src line: 0,,n_sect,linenumber,address 803 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 804 type = eSymbolTypeLineEntry; 805 break; 806 807 case StabStructureType: 808 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset 809 type = eSymbolTypeVariableType; 810 break; 811 812 case StabSourceFileName: 813 // N_SO - source file name 814 type = eSymbolTypeSourceFile; 815 if (symbol_name == NULL) 816 { 817 if (N_SO_index == UINT_MAX) 818 { 819 // Skip the extra blank N_SO entries that happen when the entire 820 // path is contained in the second consecutive N_SO STAB. 821 if (minimize) 822 add_nlist = false; 823 } 824 else 825 { 826 // Set the size of the N_SO to the terminating index of this N_SO 827 // so that we can always skip the entire N_SO if we need to navigate 828 // more quickly at the source level when parsing STABS 829 symbol_ptr = symtab->SymbolAtIndex(N_SO_index); 830 symbol_ptr->SetByteSize(sym_idx + 1); 831 symbol_ptr->SetSizeIsSibling(true); 832 } 833 N_NSYM_indexes.clear(); 834 N_INCL_indexes.clear(); 835 N_BRAC_indexes.clear(); 836 N_COMM_indexes.clear(); 837 N_FUN_indexes.clear(); 838 N_SO_index = UINT_MAX; 839 } 840 else if (symbol_name[0] == '/') 841 { 842 // We use the current number of symbols in the symbol table in lieu of 843 // using nlist_idx in case we ever start trimming entries out 844 N_SO_index = sym_idx; 845 } 846 break; 847 848 case StabObjectFileName: 849 // N_OSO - object file name: name,,0,0,st_mtime 850 type = eSymbolTypeObjectFile; 851 break; 852 853 case StabLocalSymbol: 854 // N_LSYM - local sym: name,,NO_SECT,type,offset 855 type = eSymbolTypeLocal; 856 break; 857 858 //---------------------------------------------------------------------- 859 // INCL scopes 860 //---------------------------------------------------------------------- 861 case StabBeginIncludeFileName: 862 // N_BINCL - include file beginning: name,,NO_SECT,0,sum 863 // We use the current number of symbols in the symbol table in lieu of 864 // using nlist_idx in case we ever start trimming entries out 865 N_INCL_indexes.push_back(sym_idx); 866 type = eSymbolTypeScopeBegin; 867 break; 868 869 case StabEndIncludeFile: 870 // N_EINCL - include file end: name,,NO_SECT,0,0 871 // Set the size of the N_BINCL to the terminating index of this N_EINCL 872 // so that we can always skip the entire symbol if we need to navigate 873 // more quickly at the source level when parsing STABS 874 if ( !N_INCL_indexes.empty() ) 875 { 876 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back()); 877 symbol_ptr->SetByteSize(sym_idx + 1); 878 symbol_ptr->SetSizeIsSibling(true); 879 N_INCL_indexes.pop_back(); 880 } 881 type = eSymbolTypeScopeEnd; 882 break; 883 884 case StabIncludeFileName: 885 // N_SOL - #included file name: name,,n_sect,0,address 886 type = eSymbolTypeHeaderFile; 887 888 // We currently don't use the header files on darwin 889 if (minimize) 890 add_nlist = false; 891 break; 892 893 case StabCompilerParameters: 894 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0 895 type = eSymbolTypeCompiler; 896 break; 897 898 case StabCompilerVersion: 899 // N_VERSION - compiler version: name,,NO_SECT,0,0 900 type = eSymbolTypeCompiler; 901 break; 902 903 case StabCompilerOptLevel: 904 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0 905 type = eSymbolTypeCompiler; 906 break; 907 908 case StabParameter: 909 // N_PSYM - parameter: name,,NO_SECT,type,offset 910 type = eSymbolTypeVariable; 911 break; 912 913 case StabAlternateEntry: 914 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address 915 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 916 type = eSymbolTypeLineEntry; 917 break; 918 919 //---------------------------------------------------------------------- 920 // Left and Right Braces 921 //---------------------------------------------------------------------- 922 case StabLeftBracket: 923 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address 924 // We use the current number of symbols in the symbol table in lieu of 925 // using nlist_idx in case we ever start trimming entries out 926 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 927 N_BRAC_indexes.push_back(sym_idx); 928 type = eSymbolTypeScopeBegin; 929 break; 930 931 case StabRightBracket: 932 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address 933 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC 934 // so that we can always skip the entire symbol if we need to navigate 935 // more quickly at the source level when parsing STABS 936 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 937 if ( !N_BRAC_indexes.empty() ) 938 { 939 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back()); 940 symbol_ptr->SetByteSize(sym_idx + 1); 941 symbol_ptr->SetSizeIsSibling(true); 942 N_BRAC_indexes.pop_back(); 943 } 944 type = eSymbolTypeScopeEnd; 945 break; 946 947 case StabDeletedIncludeFile: 948 // N_EXCL - deleted include file: name,,NO_SECT,0,sum 949 type = eSymbolTypeHeaderFile; 950 break; 951 952 //---------------------------------------------------------------------- 953 // COMM scopes 954 //---------------------------------------------------------------------- 955 case StabBeginCommon: 956 // N_BCOMM - begin common: name,,NO_SECT,0,0 957 // We use the current number of symbols in the symbol table in lieu of 958 // using nlist_idx in case we ever start trimming entries out 959 type = eSymbolTypeScopeBegin; 960 N_COMM_indexes.push_back(sym_idx); 961 break; 962 963 case StabEndCommonLocal: 964 // N_ECOML - end common (local name): 0,,n_sect,0,address 965 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 966 // Fall through 967 968 case StabEndCommon: 969 // N_ECOMM - end common: name,,n_sect,0,0 970 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML 971 // so that we can always skip the entire symbol if we need to navigate 972 // more quickly at the source level when parsing STABS 973 if ( !N_COMM_indexes.empty() ) 974 { 975 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back()); 976 symbol_ptr->SetByteSize(sym_idx + 1); 977 symbol_ptr->SetSizeIsSibling(true); 978 N_COMM_indexes.pop_back(); 979 } 980 type = eSymbolTypeScopeEnd; 981 break; 982 983 case StabLength: 984 // N_LENG - second stab entry with length information 985 type = eSymbolTypeAdditional; 986 break; 987 988 default: break; 989 } 990 } 991 else 992 { 993 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type; 994 uint8_t n_type = NlistMaskType & nlist.n_type; 995 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0); 996 997 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name) 998 { 999 type = eSymbolTypeRuntime; 1000 } 1001 else 1002 { 1003 switch (n_type) 1004 { 1005 case NListTypeIndirect: // N_INDR - Fall through 1006 case NListTypePreboundUndefined:// N_PBUD - Fall through 1007 case NListTypeUndefined: // N_UNDF 1008 type = eSymbolTypeExtern; 1009 break; 1010 1011 case NListTypeAbsolute: // N_ABS 1012 type = eSymbolTypeAbsolute; 1013 break; 1014 1015 case NListTypeSection: // N_SECT 1016 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); 1017 1018 assert(symbol_section != NULL); 1019 if (TEXT_eh_frame_sectID == nlist.n_sect) 1020 { 1021 type = eSymbolTypeException; 1022 } 1023 else 1024 { 1025 uint32_t section_type = symbol_section->GetAllFlagBits() & SectionFlagMaskSectionType; 1026 1027 switch (section_type) 1028 { 1029 case SectionTypeRegular: break; // regular section 1030 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section 1031 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings 1032 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals 1033 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals 1034 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals 1035 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers 1036 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers 1037 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field 1038 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization 1039 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination 1040 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced 1041 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes) 1042 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing 1043 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals 1044 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break; 1045 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break; 1046 default: break; 1047 } 1048 1049 if (type == eSymbolTypeInvalid) 1050 { 1051 const char *symbol_sect_name = symbol_section->GetName().AsCString(); 1052 if (symbol_section->IsDescendant (text_section_sp.get())) 1053 { 1054 if (symbol_section->IsClear(SectionAttrUserPureInstructions | 1055 SectionAttrUserSelfModifyingCode | 1056 SectionAttrSytemSomeInstructions)) 1057 type = eSymbolTypeData; 1058 else 1059 type = eSymbolTypeCode; 1060 } 1061 else 1062 if (symbol_section->IsDescendant(data_section_sp.get())) 1063 { 1064 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name) 1065 { 1066 type = eSymbolTypeRuntime; 1067 } 1068 else 1069 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name) 1070 { 1071 type = eSymbolTypeException; 1072 } 1073 else 1074 { 1075 type = eSymbolTypeData; 1076 } 1077 } 1078 else 1079 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name) 1080 { 1081 type = eSymbolTypeTrampoline; 1082 } 1083 else 1084 if (symbol_section->IsDescendant(objc_section_sp.get())) 1085 { 1086 type = eSymbolTypeRuntime; 1087 } 1088 } 1089 } 1090 break; 1091 } 1092 } 1093 } 1094 1095 if (add_nlist) 1096 { 1097 bool symbol_name_is_mangled = false; 1098 if (symbol_name && symbol_name[0] == '_') 1099 { 1100 symbol_name_is_mangled = symbol_name[1] == '_'; 1101 symbol_name++; // Skip the leading underscore 1102 } 1103 uint64_t symbol_value = nlist.n_value; 1104 1105 if (symbol_name) 1106 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled); 1107 1108 if (type == eSymbolTypeCode) 1109 { 1110 // See if we can find a N_FUN entry for any code symbols. 1111 // If we do find a match, and the name matches, then we 1112 // can merge the two into just the function symbol to avoid 1113 // duplicate entries in the symbol table 1114 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value); 1115 if (pos != N_FUN_addr_to_sym_idx.end()) 1116 { 1117 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || 1118 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) 1119 { 1120 1121 // We just need the flags from the linker symbol, so put these flags 1122 // into the N_FUN flags to avoid duplicate symbols in the symbol table 1123 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); 1124 sym[sym_idx].GetMangled().Clear(); 1125 continue; 1126 } 1127 } 1128 } 1129 else if (type == eSymbolTypeData) 1130 { 1131 // See if we can find a N_STSYM entry for any data symbols. 1132 // If we do find a match, and the name matches, then we 1133 // can merge the two into just the Static symbol to avoid 1134 // duplicate entries in the symbol table 1135 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value); 1136 if (pos != N_STSYM_addr_to_sym_idx.end()) 1137 { 1138 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || 1139 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) 1140 { 1141 1142 // We just need the flags from the linker symbol, so put these flags 1143 // into the N_STSYM flags to avoid duplicate symbols in the symbol table 1144 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); 1145 sym[sym_idx].GetMangled().Clear(); 1146 continue; 1147 } 1148 } 1149 } 1150 1151 if (symbol_section != NULL) 1152 symbol_value -= symbol_section->GetFileAddress(); 1153 1154 sym[sym_idx].SetID (nlist_idx); 1155 sym[sym_idx].SetType (type); 1156 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section); 1157 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value); 1158 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); 1159 1160 ++sym_idx; 1161 } 1162 else 1163 { 1164 sym[sym_idx].Clear(); 1165 } 1166 1167 } 1168 1169 1170 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value 1171 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all 1172 // such entries by figuring out what the address for the global is by looking up this non-STAB 1173 // entry and copying the value into the debug symbol's value to save us the hassle in the 1174 // debug symbol parser. 1175 1176 Symbol *global_symbol = NULL; 1177 for (nlist_idx = 0; 1178 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType(eSymbolTypeGlobal, nlist_idx)) != NULL; 1179 nlist_idx++) 1180 { 1181 if (global_symbol->GetValue().GetFileAddress() == 0) 1182 { 1183 std::vector<uint32_t> indexes; 1184 if (symtab->AppendSymbolIndexesWithName(global_symbol->GetMangled().GetName(), indexes) > 0) 1185 { 1186 std::vector<uint32_t>::const_iterator pos; 1187 std::vector<uint32_t>::const_iterator end = indexes.end(); 1188 for (pos = indexes.begin(); pos != end; ++pos) 1189 { 1190 symbol_ptr = symtab->SymbolAtIndex(*pos); 1191 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false) 1192 { 1193 global_symbol->SetValue(symbol_ptr->GetValue()); 1194 break; 1195 } 1196 } 1197 } 1198 } 1199 } 1200 // Now synthesize indirect symbols 1201 if (m_dysymtab.nindirectsyms != 0) 1202 { 1203 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4)); 1204 1205 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize()) 1206 { 1207 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize()); 1208 1209 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx) 1210 { 1211 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs) 1212 { 1213 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2; 1214 if (symbol_stub_byte_size == 0) 1215 continue; 1216 1217 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size; 1218 1219 if (num_symbol_stubs == 0) 1220 continue; 1221 1222 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1; 1223 uint32_t stub_sym_id = symtab_load_command.nsyms; 1224 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) 1225 { 1226 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx; 1227 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size); 1228 uint32_t symbol_stub_offset = symbol_stub_index * 4; 1229 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4)) 1230 { 1231 const uint32_t symbol_index = indirect_symbol_index_data.GetU32 (&symbol_stub_offset); 1232 1233 Symbol *stub_symbol; 1234 if (minimize) 1235 stub_symbol = symtab->FindSymbolByID (symbol_index); 1236 else 1237 stub_symbol = symtab->SymbolAtIndex (symbol_index); 1238 1239 assert (stub_symbol); 1240 if (stub_symbol) 1241 { 1242 Address so_addr(symbol_stub_addr, section_list); 1243 1244 if (stub_symbol->GetType() == eSymbolTypeExtern) 1245 { 1246 // Change the external symbol into a trampoline that makes sense 1247 // These symbols were N_UNDF N_EXT, and are useless to us, so we 1248 // can re-use them so we don't have to make up a synthetic symbol 1249 // for no good reason. 1250 stub_symbol->SetType (eSymbolTypeTrampoline); 1251 stub_symbol->SetExternal (false); 1252 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr; 1253 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size); 1254 } 1255 else 1256 { 1257 // Make a synthetic symbol to describe the trampoline stub 1258 if (sym_idx >= num_syms) 1259 { 1260 sym = symtab->Resize (num_syms + 16); 1261 num_syms = symtab->GetNumSymbols(); 1262 } 1263 sym[sym_idx].SetID (stub_sym_id++); 1264 sym[sym_idx].GetMangled() = stub_symbol->GetMangled(); 1265 sym[sym_idx].SetType (eSymbolTypeTrampoline); 1266 sym[sym_idx].SetIsSynthetic (true); 1267 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr; 1268 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size); 1269 ++sym_idx; 1270 } 1271 } 1272 } 1273 } 1274 } 1275 } 1276 } 1277 } 1278 1279 if (sym_idx != symtab->GetNumSymbols()) 1280 symtab->Resize (sym_idx); 1281 1282 return symtab->GetNumSymbols(); 1283 } 1284 } 1285 offset = cmd_offset + symtab_load_command.cmdsize; 1286 } 1287 return 0; 1288} 1289 1290 1291void 1292ObjectFileMachO::Dump (Stream *s) 1293{ 1294 lldb_private::Mutex::Locker locker(m_mutex); 1295 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 1296 s->Indent(); 1297 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped) 1298 s->PutCString("ObjectFileMachO64"); 1299 else 1300 s->PutCString("ObjectFileMachO32"); 1301 1302 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); 1303 1304 *s << ", file = '" << m_file << "', arch = " << header_arch.AsCString() << "\n"; 1305 1306 if (m_sections_ap.get()) 1307 m_sections_ap->Dump(s, NULL, true); 1308 1309 if (m_symtab_ap.get()) 1310 m_symtab_ap->Dump(s, NULL); 1311} 1312 1313 1314bool 1315ObjectFileMachO::GetUUID (UUID* uuid) 1316{ 1317 lldb_private::Mutex::Locker locker(m_mutex); 1318 struct uuid_command load_cmd; 1319 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 1320 uint32_t i; 1321 for (i=0; i<m_header.ncmds; ++i) 1322 { 1323 const uint32_t cmd_offset = offset; 1324 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 1325 break; 1326 1327 if (load_cmd.cmd == LoadCommandUUID) 1328 { 1329 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16); 1330 if (uuid_bytes) 1331 { 1332 uuid->SetBytes (uuid_bytes); 1333 return true; 1334 } 1335 return false; 1336 } 1337 offset = cmd_offset + load_cmd.cmdsize; 1338 } 1339 return false; 1340} 1341 1342 1343uint32_t 1344ObjectFileMachO::GetDependentModules (FileSpecList& files) 1345{ 1346 lldb_private::Mutex::Locker locker(m_mutex); 1347 struct load_command load_cmd; 1348 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); 1349 uint32_t count = 0; 1350 uint32_t i; 1351 for (i=0; i<m_header.ncmds; ++i) 1352 { 1353 const uint32_t cmd_offset = offset; 1354 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) 1355 break; 1356 1357 switch (load_cmd.cmd) 1358 { 1359 case LoadCommandDylibLoad: 1360 case LoadCommandDylibLoadWeak: 1361 case LoadCommandDylibReexport: 1362 case LoadCommandDynamicLinkerLoad: 1363 case LoadCommandFixedVMShlibLoad: 1364 { 1365 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset); 1366 const char *path = m_data.PeekCStr(name_offset); 1367 // Skip any path that starts with '@' since these are usually: 1368 // @executable_path/.../file 1369 // @rpath/.../file 1370 if (path && path[0] != '@') 1371 { 1372 FileSpec file_spec(path); 1373 if (files.AppendIfUnique(file_spec)) 1374 count++; 1375 } 1376 } 1377 break; 1378 1379 default: 1380 break; 1381 } 1382 offset = cmd_offset + load_cmd.cmdsize; 1383 } 1384 return count; 1385} 1386 1387bool 1388ObjectFileMachO::GetTargetTriple (ConstString &target_triple) 1389{ 1390 lldb_private::Mutex::Locker locker(m_mutex); 1391 std::string triple(GetModule()->GetArchitecture().AsCString()); 1392 triple += "-apple-darwin"; 1393 target_triple.SetCString(triple.c_str()); 1394 if (target_triple) 1395 return true; 1396 return false; 1397} 1398 1399 1400//------------------------------------------------------------------ 1401// PluginInterface protocol 1402//------------------------------------------------------------------ 1403const char * 1404ObjectFileMachO::GetPluginName() 1405{ 1406 return "ObjectFileMachO"; 1407} 1408 1409const char * 1410ObjectFileMachO::GetShortPluginName() 1411{ 1412 return GetPluginNameStatic(); 1413} 1414 1415uint32_t 1416ObjectFileMachO::GetPluginVersion() 1417{ 1418 return 1; 1419} 1420 1421void 1422ObjectFileMachO::GetPluginCommandHelp (const char *command, Stream *strm) 1423{ 1424} 1425 1426Error 1427ObjectFileMachO::ExecutePluginCommand (Args &command, Stream *strm) 1428{ 1429 Error error; 1430 error.SetErrorString("No plug-in command are currently supported."); 1431 return error; 1432} 1433 1434Log * 1435ObjectFileMachO::EnablePluginLogging (Stream *strm, Args &command) 1436{ 1437 return NULL; 1438} 1439 1440 1441 1442 1443