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