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/lldb-private-log.h"
16 #include "lldb/Core/ArchSpec.h"
17 #include "lldb/Core/DataBuffer.h"
18 #include "lldb/Core/FileSpecList.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/PluginManager.h"
22 #include "lldb/Core/RangeMap.h"
23 #include "lldb/Core/Section.h"
24 #include "lldb/Core/StreamFile.h"
25 #include "lldb/Core/StreamString.h"
26 #include "lldb/Core/Timer.h"
27 #include "lldb/Core/UUID.h"
28 #include "lldb/Host/Host.h"
29 #include "lldb/Host/FileSpec.h"
30 #include "lldb/Symbol/ClangNamespaceDecl.h"
31 #include "lldb/Symbol/ObjectFile.h"
32 #include "lldb/Target/Platform.h"
33 #include "lldb/Target/Process.h"
34 #include "lldb/Target/Target.h"
35 #include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
36 #include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
37 #include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
38 
39 using namespace lldb;
40 using namespace lldb_private;
41 using namespace llvm::MachO;
42 
43 class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
44 {
45 public:
46     RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
47         RegisterContextDarwin_x86_64 (thread, 0)
48     {
49         SetRegisterDataFrom_LC_THREAD (data);
50     }
51 
52     virtual void
53     InvalidateAllRegisters ()
54     {
55         // Do nothing... registers are always valid...
56     }
57 
58     void
59     SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
60     {
61         lldb::offset_t offset = 0;
62         SetError (GPRRegSet, Read, -1);
63         SetError (FPURegSet, Read, -1);
64         SetError (EXCRegSet, Read, -1);
65         bool done = false;
66 
67         while (!done)
68         {
69             int flavor = data.GetU32 (&offset);
70             if (flavor == 0)
71                 done = true;
72             else
73             {
74                 uint32_t i;
75                 uint32_t count = data.GetU32 (&offset);
76                 switch (flavor)
77                 {
78                     case GPRRegSet:
79                         for (i=0; i<count; ++i)
80                             (&gpr.rax)[i] = data.GetU64(&offset);
81                         SetError (GPRRegSet, Read, 0);
82                         done = true;
83 
84                         break;
85                     case FPURegSet:
86                         // TODO: fill in FPU regs....
87                         //SetError (FPURegSet, Read, -1);
88                         done = true;
89 
90                         break;
91                     case EXCRegSet:
92                         exc.trapno = data.GetU32(&offset);
93                         exc.err = data.GetU32(&offset);
94                         exc.faultvaddr = data.GetU64(&offset);
95                         SetError (EXCRegSet, Read, 0);
96                         done = true;
97                         break;
98                     case 7:
99                     case 8:
100                     case 9:
101                         // fancy flavors that encapsulate of the the above
102                         // falvors...
103                         break;
104 
105                     default:
106                         done = true;
107                         break;
108                 }
109             }
110         }
111     }
112 protected:
113     virtual int
114     DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
115     {
116         return 0;
117     }
118 
119     virtual int
120     DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
121     {
122         return 0;
123     }
124 
125     virtual int
126     DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
127     {
128         return 0;
129     }
130 
131     virtual int
132     DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
133     {
134         return 0;
135     }
136 
137     virtual int
138     DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
139     {
140         return 0;
141     }
142 
143     virtual int
144     DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
145     {
146         return 0;
147     }
148 };
149 
150 
151 class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
152 {
153 public:
154     RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
155     RegisterContextDarwin_i386 (thread, 0)
156     {
157         SetRegisterDataFrom_LC_THREAD (data);
158     }
159 
160     virtual void
161     InvalidateAllRegisters ()
162     {
163         // Do nothing... registers are always valid...
164     }
165 
166     void
167     SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
168     {
169         lldb::offset_t offset = 0;
170         SetError (GPRRegSet, Read, -1);
171         SetError (FPURegSet, Read, -1);
172         SetError (EXCRegSet, Read, -1);
173         bool done = false;
174 
175         while (!done)
176         {
177             int flavor = data.GetU32 (&offset);
178             if (flavor == 0)
179                 done = true;
180             else
181             {
182                 uint32_t i;
183                 uint32_t count = data.GetU32 (&offset);
184                 switch (flavor)
185                 {
186                     case GPRRegSet:
187                         for (i=0; i<count; ++i)
188                             (&gpr.eax)[i] = data.GetU32(&offset);
189                         SetError (GPRRegSet, Read, 0);
190                         done = true;
191 
192                         break;
193                     case FPURegSet:
194                         // TODO: fill in FPU regs....
195                         //SetError (FPURegSet, Read, -1);
196                         done = true;
197 
198                         break;
199                     case EXCRegSet:
200                         exc.trapno = data.GetU32(&offset);
201                         exc.err = data.GetU32(&offset);
202                         exc.faultvaddr = data.GetU32(&offset);
203                         SetError (EXCRegSet, Read, 0);
204                         done = true;
205                         break;
206                     case 7:
207                     case 8:
208                     case 9:
209                         // fancy flavors that encapsulate of the the above
210                         // falvors...
211                         break;
212 
213                     default:
214                         done = true;
215                         break;
216                 }
217             }
218         }
219     }
220 protected:
221     virtual int
222     DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
223     {
224         return 0;
225     }
226 
227     virtual int
228     DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
229     {
230         return 0;
231     }
232 
233     virtual int
234     DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
235     {
236         return 0;
237     }
238 
239     virtual int
240     DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
241     {
242         return 0;
243     }
244 
245     virtual int
246     DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
247     {
248         return 0;
249     }
250 
251     virtual int
252     DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
253     {
254         return 0;
255     }
256 };
257 
258 class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
259 {
260 public:
261     RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
262         RegisterContextDarwin_arm (thread, 0)
263     {
264         SetRegisterDataFrom_LC_THREAD (data);
265     }
266 
267     virtual void
268     InvalidateAllRegisters ()
269     {
270         // Do nothing... registers are always valid...
271     }
272 
273     void
274     SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
275     {
276         lldb::offset_t offset = 0;
277         SetError (GPRRegSet, Read, -1);
278         SetError (FPURegSet, Read, -1);
279         SetError (EXCRegSet, Read, -1);
280         int flavor = data.GetU32 (&offset);
281         uint32_t count = data.GetU32 (&offset);
282         switch (flavor)
283         {
284             case GPRRegSet:
285                 for (uint32_t i=0; i<count; ++i)
286                     gpr.r[i] = data.GetU32(&offset);
287                 SetError (GPRRegSet, Read, 0);
288                 break;
289             case FPURegSet:
290                 // TODO: fill in FPU regs....
291                 //SetError (FPURegSet, Read, -1);
292                 break;
293             case EXCRegSet:
294                 exc.exception = data.GetU32(&offset);
295                 exc.fsr = data.GetU32(&offset);
296                 exc.far = data.GetU32(&offset);
297                 SetError (EXCRegSet, Read, 0);
298                 break;
299         }
300     }
301 protected:
302     virtual int
303     DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
304     {
305         return 0;
306     }
307 
308     virtual int
309     DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
310     {
311         return 0;
312     }
313 
314     virtual int
315     DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
316     {
317         return 0;
318     }
319 
320     virtual int
321     DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
322     {
323         return -1;
324     }
325 
326     virtual int
327     DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
328     {
329         return 0;
330     }
331 
332     virtual int
333     DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
334     {
335         return 0;
336     }
337 
338     virtual int
339     DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
340     {
341         return 0;
342     }
343 
344     virtual int
345     DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
346     {
347         return -1;
348     }
349 };
350 
351 #define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
352 
353 void
354 ObjectFileMachO::Initialize()
355 {
356     PluginManager::RegisterPlugin (GetPluginNameStatic(),
357                                    GetPluginDescriptionStatic(),
358                                    CreateInstance,
359                                    CreateMemoryInstance);
360 }
361 
362 void
363 ObjectFileMachO::Terminate()
364 {
365     PluginManager::UnregisterPlugin (CreateInstance);
366 }
367 
368 
369 const char *
370 ObjectFileMachO::GetPluginNameStatic()
371 {
372     return "object-file.mach-o";
373 }
374 
375 const char *
376 ObjectFileMachO::GetPluginDescriptionStatic()
377 {
378     return "Mach-o object file reader (32 and 64 bit)";
379 }
380 
381 
382 ObjectFile *
383 ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length)
384 {
385     if (ObjectFileMachO::MagicBytesMatch(data_sp, offset, length))
386     {
387         std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, file, offset, length));
388         if (objfile_ap.get() && objfile_ap->ParseHeader())
389             return objfile_ap.release();
390     }
391     return NULL;
392 }
393 
394 ObjectFile *
395 ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
396                                        DataBufferSP& data_sp,
397                                        const ProcessSP &process_sp,
398                                        lldb::addr_t header_addr)
399 {
400     if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
401     {
402         std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
403         if (objfile_ap.get() && objfile_ap->ParseHeader())
404             return objfile_ap.release();
405     }
406     return NULL;
407 }
408 
409 
410 const ConstString &
411 ObjectFileMachO::GetSegmentNameTEXT()
412 {
413     static ConstString g_segment_name_TEXT ("__TEXT");
414     return g_segment_name_TEXT;
415 }
416 
417 const ConstString &
418 ObjectFileMachO::GetSegmentNameDATA()
419 {
420     static ConstString g_segment_name_DATA ("__DATA");
421     return g_segment_name_DATA;
422 }
423 
424 const ConstString &
425 ObjectFileMachO::GetSegmentNameOBJC()
426 {
427     static ConstString g_segment_name_OBJC ("__OBJC");
428     return g_segment_name_OBJC;
429 }
430 
431 const ConstString &
432 ObjectFileMachO::GetSegmentNameLINKEDIT()
433 {
434     static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
435     return g_section_name_LINKEDIT;
436 }
437 
438 const ConstString &
439 ObjectFileMachO::GetSectionNameEHFrame()
440 {
441     static ConstString g_section_name_eh_frame ("__eh_frame");
442     return g_section_name_eh_frame;
443 }
444 
445 
446 
447 static uint32_t
448 MachHeaderSizeFromMagic(uint32_t magic)
449 {
450     switch (magic)
451     {
452     case HeaderMagic32:
453     case HeaderMagic32Swapped:
454         return sizeof(struct mach_header);
455 
456     case HeaderMagic64:
457     case HeaderMagic64Swapped:
458         return sizeof(struct mach_header_64);
459         break;
460 
461     default:
462         break;
463     }
464     return 0;
465 }
466 
467 
468 bool
469 ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
470                                   lldb::addr_t data_offset,
471                                   lldb::addr_t data_length)
472 {
473     DataExtractor data;
474     data.SetData (data_sp, data_offset, data_length);
475     lldb::offset_t offset = 0;
476     uint32_t magic = data.GetU32(&offset);
477     return MachHeaderSizeFromMagic(magic) != 0;
478 }
479 
480 
481 ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length) :
482     ObjectFile(module_sp, file, offset, length, data_sp),
483     m_sections_ap(),
484     m_symtab_ap(),
485     m_mach_segments(),
486     m_mach_sections(),
487     m_entry_point_address(),
488     m_thread_context_offsets(),
489     m_thread_context_offsets_valid(false)
490 {
491     ::memset (&m_header, 0, sizeof(m_header));
492     ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
493 }
494 
495 ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
496                                   lldb::DataBufferSP& header_data_sp,
497                                   const lldb::ProcessSP &process_sp,
498                                   lldb::addr_t header_addr) :
499     ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
500     m_sections_ap(),
501     m_symtab_ap(),
502     m_mach_segments(),
503     m_mach_sections(),
504     m_entry_point_address(),
505     m_thread_context_offsets(),
506     m_thread_context_offsets_valid(false)
507 {
508     ::memset (&m_header, 0, sizeof(m_header));
509     ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
510 }
511 
512 ObjectFileMachO::~ObjectFileMachO()
513 {
514 }
515 
516 
517 bool
518 ObjectFileMachO::ParseHeader ()
519 {
520     ModuleSP module_sp(GetModule());
521     if (module_sp)
522     {
523         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
524         bool can_parse = false;
525         lldb::offset_t offset = 0;
526         m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
527         // Leave magic in the original byte order
528         m_header.magic = m_data.GetU32(&offset);
529         switch (m_header.magic)
530         {
531         case HeaderMagic32:
532             m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
533             m_data.SetAddressByteSize(4);
534             can_parse = true;
535             break;
536 
537         case HeaderMagic64:
538             m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
539             m_data.SetAddressByteSize(8);
540             can_parse = true;
541             break;
542 
543         case HeaderMagic32Swapped:
544             m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
545             m_data.SetAddressByteSize(4);
546             can_parse = true;
547             break;
548 
549         case HeaderMagic64Swapped:
550             m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
551             m_data.SetAddressByteSize(8);
552             can_parse = true;
553             break;
554 
555         default:
556             break;
557         }
558 
559         if (can_parse)
560         {
561             m_data.GetU32(&offset, &m_header.cputype, 6);
562 
563             ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
564 
565             // Check if the module has a required architecture
566             const ArchSpec &module_arch = module_sp->GetArchitecture();
567             if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
568                 return false;
569 
570             if (SetModulesArchitecture (mach_arch))
571             {
572                 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
573                 if (m_data.GetByteSize() < header_and_lc_size)
574                 {
575                     DataBufferSP data_sp;
576                     ProcessSP process_sp (m_process_wp.lock());
577                     if (process_sp)
578                     {
579                         data_sp = ReadMemory (process_sp, m_offset, header_and_lc_size);
580                     }
581                     else
582                     {
583                         // Read in all only the load command data from the file on disk
584                         data_sp = m_file.ReadFileContents(m_offset, header_and_lc_size);
585                         if (data_sp->GetByteSize() != header_and_lc_size)
586                             return false;
587                     }
588                     if (data_sp)
589                         m_data.SetData (data_sp);
590                 }
591             }
592             return true;
593         }
594         else
595         {
596             memset(&m_header, 0, sizeof(struct mach_header));
597         }
598     }
599     return false;
600 }
601 
602 
603 ByteOrder
604 ObjectFileMachO::GetByteOrder () const
605 {
606     return m_data.GetByteOrder ();
607 }
608 
609 bool
610 ObjectFileMachO::IsExecutable() const
611 {
612     return m_header.filetype == HeaderFileTypeExecutable;
613 }
614 
615 uint32_t
616 ObjectFileMachO::GetAddressByteSize () const
617 {
618     return m_data.GetAddressByteSize ();
619 }
620 
621 AddressClass
622 ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
623 {
624     Symtab *symtab = GetSymtab();
625     if (symtab)
626     {
627         Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
628         if (symbol)
629         {
630             if (symbol->ValueIsAddress())
631             {
632                 SectionSP section_sp (symbol->GetAddress().GetSection());
633                 if (section_sp)
634                 {
635                     const SectionType section_type = section_sp->GetType();
636                     switch (section_type)
637                     {
638                     case eSectionTypeInvalid:               return eAddressClassUnknown;
639                     case eSectionTypeCode:
640                         if (m_header.cputype == llvm::MachO::CPUTypeARM)
641                         {
642                             // For ARM we have a bit in the n_desc field of the symbol
643                             // that tells us ARM/Thumb which is bit 0x0008.
644                             if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
645                                 return eAddressClassCodeAlternateISA;
646                         }
647                         return eAddressClassCode;
648 
649                     case eSectionTypeContainer:             return eAddressClassUnknown;
650                     case eSectionTypeData:
651                     case eSectionTypeDataCString:
652                     case eSectionTypeDataCStringPointers:
653                     case eSectionTypeDataSymbolAddress:
654                     case eSectionTypeData4:
655                     case eSectionTypeData8:
656                     case eSectionTypeData16:
657                     case eSectionTypeDataPointers:
658                     case eSectionTypeZeroFill:
659                     case eSectionTypeDataObjCMessageRefs:
660                     case eSectionTypeDataObjCCFStrings:
661                         return eAddressClassData;
662                     case eSectionTypeDebug:
663                     case eSectionTypeDWARFDebugAbbrev:
664                     case eSectionTypeDWARFDebugAranges:
665                     case eSectionTypeDWARFDebugFrame:
666                     case eSectionTypeDWARFDebugInfo:
667                     case eSectionTypeDWARFDebugLine:
668                     case eSectionTypeDWARFDebugLoc:
669                     case eSectionTypeDWARFDebugMacInfo:
670                     case eSectionTypeDWARFDebugPubNames:
671                     case eSectionTypeDWARFDebugPubTypes:
672                     case eSectionTypeDWARFDebugRanges:
673                     case eSectionTypeDWARFDebugStr:
674                     case eSectionTypeDWARFAppleNames:
675                     case eSectionTypeDWARFAppleTypes:
676                     case eSectionTypeDWARFAppleNamespaces:
677                     case eSectionTypeDWARFAppleObjC:
678                         return eAddressClassDebug;
679                     case eSectionTypeEHFrame:               return eAddressClassRuntime;
680                     case eSectionTypeOther:                 return eAddressClassUnknown;
681                     }
682                 }
683             }
684 
685             const SymbolType symbol_type = symbol->GetType();
686             switch (symbol_type)
687             {
688             case eSymbolTypeAny:            return eAddressClassUnknown;
689             case eSymbolTypeAbsolute:       return eAddressClassUnknown;
690 
691             case eSymbolTypeCode:
692             case eSymbolTypeTrampoline:
693                 if (m_header.cputype == llvm::MachO::CPUTypeARM)
694                 {
695                     // For ARM we have a bit in the n_desc field of the symbol
696                     // that tells us ARM/Thumb which is bit 0x0008.
697                     if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
698                         return eAddressClassCodeAlternateISA;
699                 }
700                 return eAddressClassCode;
701 
702             case eSymbolTypeData:           return eAddressClassData;
703             case eSymbolTypeRuntime:        return eAddressClassRuntime;
704             case eSymbolTypeException:      return eAddressClassRuntime;
705             case eSymbolTypeSourceFile:     return eAddressClassDebug;
706             case eSymbolTypeHeaderFile:     return eAddressClassDebug;
707             case eSymbolTypeObjectFile:     return eAddressClassDebug;
708             case eSymbolTypeCommonBlock:    return eAddressClassDebug;
709             case eSymbolTypeBlock:          return eAddressClassDebug;
710             case eSymbolTypeLocal:          return eAddressClassData;
711             case eSymbolTypeParam:          return eAddressClassData;
712             case eSymbolTypeVariable:       return eAddressClassData;
713             case eSymbolTypeVariableType:   return eAddressClassDebug;
714             case eSymbolTypeLineEntry:      return eAddressClassDebug;
715             case eSymbolTypeLineHeader:     return eAddressClassDebug;
716             case eSymbolTypeScopeBegin:     return eAddressClassDebug;
717             case eSymbolTypeScopeEnd:       return eAddressClassDebug;
718             case eSymbolTypeAdditional:     return eAddressClassUnknown;
719             case eSymbolTypeCompiler:       return eAddressClassDebug;
720             case eSymbolTypeInstrumentation:return eAddressClassDebug;
721             case eSymbolTypeUndefined:      return eAddressClassUnknown;
722             case eSymbolTypeObjCClass:      return eAddressClassRuntime;
723             case eSymbolTypeObjCMetaClass:  return eAddressClassRuntime;
724             case eSymbolTypeObjCIVar:       return eAddressClassRuntime;
725             }
726         }
727     }
728     return eAddressClassUnknown;
729 }
730 
731 Symtab *
732 ObjectFileMachO::GetSymtab()
733 {
734     ModuleSP module_sp(GetModule());
735     if (module_sp)
736     {
737         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
738         if (m_symtab_ap.get() == NULL)
739         {
740             m_symtab_ap.reset(new Symtab(this));
741             Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
742             ParseSymtab (true);
743             m_symtab_ap->Finalize ();
744         }
745     }
746     return m_symtab_ap.get();
747 }
748 
749 
750 SectionList *
751 ObjectFileMachO::GetSectionList()
752 {
753     ModuleSP module_sp(GetModule());
754     if (module_sp)
755     {
756         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
757         if (m_sections_ap.get() == NULL)
758         {
759             m_sections_ap.reset(new SectionList());
760             ParseSections();
761         }
762     }
763     return m_sections_ap.get();
764 }
765 
766 
767 size_t
768 ObjectFileMachO::ParseSections ()
769 {
770     lldb::user_id_t segID = 0;
771     lldb::user_id_t sectID = 0;
772     lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
773     uint32_t i;
774     const bool is_core = GetType() == eTypeCoreFile;
775     //bool dump_sections = false;
776     ModuleSP module_sp (GetModule());
777     // First look up any LC_ENCRYPTION_INFO load commands
778     typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
779     EncryptedFileRanges encrypted_file_ranges;
780     encryption_info_command encryption_cmd;
781     for (i=0; i<m_header.ncmds; ++i)
782     {
783         const lldb::offset_t load_cmd_offset = offset;
784         if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
785             break;
786 
787         if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
788         {
789             if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
790             {
791                 if (encryption_cmd.cryptid != 0)
792                 {
793                     EncryptedFileRanges::Entry entry;
794                     entry.SetRangeBase(encryption_cmd.cryptoff);
795                     entry.SetByteSize(encryption_cmd.cryptsize);
796                     encrypted_file_ranges.Append(entry);
797                 }
798             }
799         }
800         offset = load_cmd_offset + encryption_cmd.cmdsize;
801     }
802 
803     offset = MachHeaderSizeFromMagic(m_header.magic);
804 
805     struct segment_command_64 load_cmd;
806     for (i=0; i<m_header.ncmds; ++i)
807     {
808         const lldb::offset_t load_cmd_offset = offset;
809         if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
810             break;
811 
812         if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
813         {
814             if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
815             {
816                 load_cmd.vmaddr = m_data.GetAddress(&offset);
817                 load_cmd.vmsize = m_data.GetAddress(&offset);
818                 load_cmd.fileoff = m_data.GetAddress(&offset);
819                 load_cmd.filesize = m_data.GetAddress(&offset);
820                 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
821                 {
822 
823                     const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
824 
825                     // Keep a list of mach segments around in case we need to
826                     // get at data that isn't stored in the abstracted Sections.
827                     m_mach_segments.push_back (load_cmd);
828 
829                     ConstString segment_name (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
830                     // Use a segment ID of the segment index shifted left by 8 so they
831                     // never conflict with any of the sections.
832                     SectionSP segment_sp;
833                     if (segment_name || is_core)
834                     {
835                         segment_sp.reset(new Section (module_sp,              // Module to which this section belongs
836                                                       ++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
837                                                       segment_name,           // Name of this section
838                                                       eSectionTypeContainer,  // This section is a container of other sections.
839                                                       load_cmd.vmaddr,        // File VM address == addresses as they are found in the object file
840                                                       load_cmd.vmsize,        // VM size in bytes of this section
841                                                       load_cmd.fileoff,       // Offset to the data for this section in the file
842                                                       load_cmd.filesize,      // Size in bytes of this section as found in the the file
843                                                       load_cmd.flags));       // Flags for this section
844 
845                         segment_sp->SetIsEncrypted (segment_is_encrypted);
846                         m_sections_ap->AddSection(segment_sp);
847                     }
848 
849                     struct section_64 sect64;
850                     ::memset (&sect64, 0, sizeof(sect64));
851                     // Push a section into our mach sections for the section at
852                     // index zero (NListSectionNoSection) if we don't have any
853                     // mach sections yet...
854                     if (m_mach_sections.empty())
855                         m_mach_sections.push_back(sect64);
856                     uint32_t segment_sect_idx;
857                     const lldb::user_id_t first_segment_sectID = sectID + 1;
858 
859 
860                     const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
861                     for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
862                     {
863                         if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
864                             break;
865                         if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
866                             break;
867                         sect64.addr = m_data.GetAddress(&offset);
868                         sect64.size = m_data.GetAddress(&offset);
869 
870                         if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
871                             break;
872 
873                         // Keep a list of mach sections around in case we need to
874                         // get at data that isn't stored in the abstracted Sections.
875                         m_mach_sections.push_back (sect64);
876 
877                         ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
878                         if (!segment_name)
879                         {
880                             // We have a segment with no name so we need to conjure up
881                             // segments that correspond to the section's segname if there
882                             // isn't already such a section. If there is such a section,
883                             // we resize the section so that it spans all sections.
884                             // We also mark these sections as fake so address matches don't
885                             // hit if they land in the gaps between the child sections.
886                             segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
887                             segment_sp = m_sections_ap->FindSectionByName (segment_name);
888                             if (segment_sp.get())
889                             {
890                                 Section *segment = segment_sp.get();
891                                 // Grow the section size as needed.
892                                 const lldb::addr_t sect64_min_addr = sect64.addr;
893                                 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
894                                 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
895                                 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
896                                 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
897                                 if (sect64_min_addr >= curr_seg_min_addr)
898                                 {
899                                     const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
900                                     // Only grow the section size if needed
901                                     if (new_seg_byte_size > curr_seg_byte_size)
902                                         segment->SetByteSize (new_seg_byte_size);
903                                 }
904                                 else
905                                 {
906                                     // We need to change the base address of the segment and
907                                     // adjust the child section offsets for all existing children.
908                                     const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
909                                     segment->Slide(slide_amount, false);
910                                     segment->GetChildren().Slide(-slide_amount, false);
911                                     segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
912                                 }
913 
914                                 // Grow the section size as needed.
915                                 if (sect64.offset)
916                                 {
917                                     const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
918                                     const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
919 
920                                     const lldb::addr_t section_min_file_offset = sect64.offset;
921                                     const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
922                                     const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
923                                     const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
924                                     segment->SetFileOffset (new_file_offset);
925                                     segment->SetFileSize (new_file_size);
926                                 }
927                             }
928                             else
929                             {
930                                 // Create a fake section for the section's named segment
931                                 segment_sp.reset(new Section (segment_sp,            // Parent section
932                                                               module_sp,           // Module to which this section belongs
933                                                               ++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
934                                                               segment_name,          // Name of this section
935                                                               eSectionTypeContainer, // This section is a container of other sections.
936                                                               sect64.addr,           // File VM address == addresses as they are found in the object file
937                                                               sect64.size,           // VM size in bytes of this section
938                                                               sect64.offset,         // Offset to the data for this section in the file
939                                                               sect64.offset ? sect64.size : 0,        // Size in bytes of this section as found in the the file
940                                                               load_cmd.flags));      // Flags for this section
941                                 segment_sp->SetIsFake(true);
942                                 m_sections_ap->AddSection(segment_sp);
943                                 segment_sp->SetIsEncrypted (segment_is_encrypted);
944                             }
945                         }
946                         assert (segment_sp.get());
947 
948                         uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
949                         static ConstString g_sect_name_objc_data ("__objc_data");
950                         static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
951                         static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
952                         static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
953                         static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
954                         static ConstString g_sect_name_objc_const ("__objc_const");
955                         static ConstString g_sect_name_objc_classlist ("__objc_classlist");
956                         static ConstString g_sect_name_cfstring ("__cfstring");
957 
958                         static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
959                         static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
960                         static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
961                         static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
962                         static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
963                         static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
964                         static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
965                         static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
966                         static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
967                         static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
968                         static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
969                         static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
970                         static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
971                         static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
972                         static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
973                         static ConstString g_sect_name_eh_frame ("__eh_frame");
974                         static ConstString g_sect_name_DATA ("__DATA");
975                         static ConstString g_sect_name_TEXT ("__TEXT");
976 
977                         SectionType sect_type = eSectionTypeOther;
978 
979                         if (section_name == g_sect_name_dwarf_debug_abbrev)
980                             sect_type = eSectionTypeDWARFDebugAbbrev;
981                         else if (section_name == g_sect_name_dwarf_debug_aranges)
982                             sect_type = eSectionTypeDWARFDebugAranges;
983                         else if (section_name == g_sect_name_dwarf_debug_frame)
984                             sect_type = eSectionTypeDWARFDebugFrame;
985                         else if (section_name == g_sect_name_dwarf_debug_info)
986                             sect_type = eSectionTypeDWARFDebugInfo;
987                         else if (section_name == g_sect_name_dwarf_debug_line)
988                             sect_type = eSectionTypeDWARFDebugLine;
989                         else if (section_name == g_sect_name_dwarf_debug_loc)
990                             sect_type = eSectionTypeDWARFDebugLoc;
991                         else if (section_name == g_sect_name_dwarf_debug_macinfo)
992                             sect_type = eSectionTypeDWARFDebugMacInfo;
993                         else if (section_name == g_sect_name_dwarf_debug_pubnames)
994                             sect_type = eSectionTypeDWARFDebugPubNames;
995                         else if (section_name == g_sect_name_dwarf_debug_pubtypes)
996                             sect_type = eSectionTypeDWARFDebugPubTypes;
997                         else if (section_name == g_sect_name_dwarf_debug_ranges)
998                             sect_type = eSectionTypeDWARFDebugRanges;
999                         else if (section_name == g_sect_name_dwarf_debug_str)
1000                             sect_type = eSectionTypeDWARFDebugStr;
1001                         else if (section_name == g_sect_name_dwarf_apple_names)
1002                             sect_type = eSectionTypeDWARFAppleNames;
1003                         else if (section_name == g_sect_name_dwarf_apple_types)
1004                             sect_type = eSectionTypeDWARFAppleTypes;
1005                         else if (section_name == g_sect_name_dwarf_apple_namespaces)
1006                             sect_type = eSectionTypeDWARFAppleNamespaces;
1007                         else if (section_name == g_sect_name_dwarf_apple_objc)
1008                             sect_type = eSectionTypeDWARFAppleObjC;
1009                         else if (section_name == g_sect_name_objc_selrefs)
1010                             sect_type = eSectionTypeDataCStringPointers;
1011                         else if (section_name == g_sect_name_objc_msgrefs)
1012                             sect_type = eSectionTypeDataObjCMessageRefs;
1013                         else if (section_name == g_sect_name_eh_frame)
1014                             sect_type = eSectionTypeEHFrame;
1015                         else if (section_name == g_sect_name_cfstring)
1016                             sect_type = eSectionTypeDataObjCCFStrings;
1017                         else if (section_name == g_sect_name_objc_data ||
1018                                  section_name == g_sect_name_objc_classrefs ||
1019                                  section_name == g_sect_name_objc_superrefs ||
1020                                  section_name == g_sect_name_objc_const ||
1021                                  section_name == g_sect_name_objc_classlist)
1022                         {
1023                             sect_type = eSectionTypeDataPointers;
1024                         }
1025 
1026                         if (sect_type == eSectionTypeOther)
1027                         {
1028                             switch (mach_sect_type)
1029                             {
1030                             // TODO: categorize sections by other flags for regular sections
1031                             case SectionTypeRegular:
1032                                 if (segment_sp->GetName() == g_sect_name_TEXT)
1033                                     sect_type = eSectionTypeCode;
1034                                 else if (segment_sp->GetName() == g_sect_name_DATA)
1035                                     sect_type = eSectionTypeData;
1036                                 else
1037                                     sect_type = eSectionTypeOther;
1038                                 break;
1039                             case SectionTypeZeroFill:                   sect_type = eSectionTypeZeroFill; break;
1040                             case SectionTypeCStringLiterals:            sect_type = eSectionTypeDataCString;    break; // section with only literal C strings
1041                             case SectionType4ByteLiterals:              sect_type = eSectionTypeData4;    break; // section with only 4 byte literals
1042                             case SectionType8ByteLiterals:              sect_type = eSectionTypeData8;    break; // section with only 8 byte literals
1043                             case SectionTypeLiteralPointers:            sect_type = eSectionTypeDataPointers;  break; // section with only pointers to literals
1044                             case SectionTypeNonLazySymbolPointers:      sect_type = eSectionTypeDataPointers;  break; // section with only non-lazy symbol pointers
1045                             case SectionTypeLazySymbolPointers:         sect_type = eSectionTypeDataPointers;  break; // section with only lazy symbol pointers
1046                             case SectionTypeSymbolStubs:                sect_type = eSectionTypeCode;  break; // section with only symbol stubs, byte size of stub in the reserved2 field
1047                             case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers;    break; // section with only function pointers for initialization
1048                             case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1049                             case SectionTypeCoalesced:                  sect_type = eSectionTypeOther; break;
1050                             case SectionTypeZeroFillLarge:              sect_type = eSectionTypeZeroFill; break;
1051                             case SectionTypeInterposing:                sect_type = eSectionTypeCode;  break; // section with only pairs of function pointers for interposing
1052                             case SectionType16ByteLiterals:             sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1053                             case SectionTypeDTraceObjectFormat:         sect_type = eSectionTypeDebug; break;
1054                             case SectionTypeLazyDylibSymbolPointers:    sect_type = eSectionTypeDataPointers;  break;
1055                             default: break;
1056                             }
1057                         }
1058 
1059                         SectionSP section_sp(new Section (segment_sp,
1060                                                           module_sp,
1061                                                           ++sectID,
1062                                                           section_name,
1063                                                           sect_type,
1064                                                           sect64.addr - segment_sp->GetFileAddress(),
1065                                                           sect64.size,
1066                                                           sect64.offset,
1067                                                           sect64.offset == 0 ? 0 : sect64.size,
1068                                                           sect64.flags));
1069                         // Set the section to be encrypted to match the segment
1070 
1071                         bool section_is_encrypted = false;
1072                         if (!segment_is_encrypted && load_cmd.filesize != 0)
1073                             section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1074 
1075                         section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1076                         segment_sp->GetChildren().AddSection(section_sp);
1077 
1078                         if (segment_sp->IsFake())
1079                         {
1080                             segment_sp.reset();
1081                             segment_name.Clear();
1082                         }
1083                     }
1084                     if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
1085                     {
1086                         if (first_segment_sectID <= sectID)
1087                         {
1088                             lldb::user_id_t sect_uid;
1089                             for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1090                             {
1091                                 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1092                                 SectionSP next_section_sp;
1093                                 if (sect_uid + 1 <= sectID)
1094                                     next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1095 
1096                                 if (curr_section_sp.get())
1097                                 {
1098                                     if (curr_section_sp->GetByteSize() == 0)
1099                                     {
1100                                         if (next_section_sp.get() != NULL)
1101                                             curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1102                                         else
1103                                             curr_section_sp->SetByteSize ( load_cmd.vmsize );
1104                                     }
1105                                 }
1106                             }
1107                         }
1108                     }
1109                 }
1110             }
1111         }
1112         else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
1113         {
1114             m_dysymtab.cmd = load_cmd.cmd;
1115             m_dysymtab.cmdsize = load_cmd.cmdsize;
1116             m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1117         }
1118 
1119         offset = load_cmd_offset + load_cmd.cmdsize;
1120     }
1121 //    if (dump_sections)
1122 //    {
1123 //        StreamFile s(stdout);
1124 //        m_sections_ap->Dump(&s, true);
1125 //    }
1126     return sectID;  // Return the number of sections we registered with the module
1127 }
1128 
1129 class MachSymtabSectionInfo
1130 {
1131 public:
1132 
1133     MachSymtabSectionInfo (SectionList *section_list) :
1134         m_section_list (section_list),
1135         m_section_infos()
1136     {
1137         // Get the number of sections down to a depth of 1 to include
1138         // all segments and their sections, but no other sections that
1139         // may be added for debug map or
1140         m_section_infos.resize(section_list->GetNumSections(1));
1141     }
1142 
1143 
1144     SectionSP
1145     GetSection (uint8_t n_sect, addr_t file_addr)
1146     {
1147         if (n_sect == 0)
1148             return SectionSP();
1149         if (n_sect < m_section_infos.size())
1150         {
1151             if (!m_section_infos[n_sect].section_sp)
1152             {
1153                 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1154                 m_section_infos[n_sect].section_sp = section_sp;
1155                 if (section_sp)
1156                 {
1157                     m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1158                     m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
1159                 }
1160                 else
1161                 {
1162                     Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
1163                 }
1164             }
1165             if (m_section_infos[n_sect].vm_range.Contains(file_addr))
1166             {
1167                 // Symbol is in section.
1168                 return m_section_infos[n_sect].section_sp;
1169             }
1170             else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1171                      m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1172             {
1173                 // Symbol is in section with zero size, but has the same start
1174                 // address as the section. This can happen with linker symbols
1175                 // (symbols that start with the letter 'l' or 'L'.
1176                 return m_section_infos[n_sect].section_sp;
1177             }
1178         }
1179         return m_section_list->FindSectionContainingFileAddress(file_addr);
1180     }
1181 
1182 protected:
1183     struct SectionInfo
1184     {
1185         SectionInfo () :
1186             vm_range(),
1187             section_sp ()
1188         {
1189         }
1190 
1191         VMRange vm_range;
1192         SectionSP section_sp;
1193     };
1194     SectionList *m_section_list;
1195     std::vector<SectionInfo> m_section_infos;
1196 };
1197 
1198 size_t
1199 ObjectFileMachO::ParseSymtab (bool minimize)
1200 {
1201     Timer scoped_timer(__PRETTY_FUNCTION__,
1202                        "ObjectFileMachO::ParseSymtab () module = %s",
1203                        m_file.GetFilename().AsCString(""));
1204     ModuleSP module_sp (GetModule());
1205     if (!module_sp)
1206         return 0;
1207 
1208     struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1209     struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1210     typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1211     FunctionStarts function_starts;
1212     lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1213     uint32_t i;
1214 
1215     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
1216 
1217     for (i=0; i<m_header.ncmds; ++i)
1218     {
1219         const lldb::offset_t cmd_offset = offset;
1220         // Read in the load command and load command size
1221         struct load_command lc;
1222         if (m_data.GetU32(&offset, &lc, 2) == NULL)
1223             break;
1224         // Watch for the symbol table load command
1225         switch (lc.cmd)
1226         {
1227         case LoadCommandSymtab:
1228             symtab_load_command.cmd = lc.cmd;
1229             symtab_load_command.cmdsize = lc.cmdsize;
1230             // Read in the rest of the symtab load command
1231             if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1232                 return 0;
1233             if (symtab_load_command.symoff == 0)
1234             {
1235                 if (log)
1236                     module_sp->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
1237                 return 0;
1238             }
1239 
1240             if (symtab_load_command.stroff == 0)
1241             {
1242                 if (log)
1243                     module_sp->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
1244                 return 0;
1245             }
1246 
1247             if (symtab_load_command.nsyms == 0)
1248             {
1249                 if (log)
1250                     module_sp->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
1251                 return 0;
1252             }
1253 
1254             if (symtab_load_command.strsize == 0)
1255             {
1256                 if (log)
1257                     module_sp->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
1258                 return 0;
1259             }
1260             break;
1261 
1262         case LoadCommandFunctionStarts:
1263             function_starts_load_command.cmd = lc.cmd;
1264             function_starts_load_command.cmdsize = lc.cmdsize;
1265             if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1266                 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1267             break;
1268 
1269         default:
1270             break;
1271         }
1272         offset = cmd_offset + lc.cmdsize;
1273     }
1274 
1275     if (symtab_load_command.cmd)
1276     {
1277         Symtab *symtab = m_symtab_ap.get();
1278         SectionList *section_list = GetSectionList();
1279         if (section_list == NULL)
1280             return 0;
1281 
1282         ProcessSP process_sp (m_process_wp.lock());
1283         Process *process = process_sp.get();
1284 
1285         const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1286         const ByteOrder byte_order = m_data.GetByteOrder();
1287         bool bit_width_32 = addr_byte_size == 4;
1288         const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1289 
1290         DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1291         DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1292         DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
1293 
1294         const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1295         const addr_t strtab_data_byte_size = symtab_load_command.strsize;
1296         addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1297         if (process)
1298         {
1299             Target &target = process->GetTarget();
1300             SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1301             // Reading mach file from memory in a process or core file...
1302 
1303             if (linkedit_section_sp)
1304             {
1305                 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1306                 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1307                 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
1308                 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
1309 
1310                 bool data_was_read = false;
1311 
1312 #if defined (__APPLE__) && defined (__arm__)
1313                 if (m_header.flags & 0x80000000u)
1314                 {
1315                     // This mach-o memory file is in the dyld shared cache. If this
1316                     // program is not remote and this is iOS, then this process will
1317                     // share the same shared cache as the process we are debugging and
1318                     // we can read the entire __LINKEDIT from the address space in this
1319                     // process. This is a needed optimization that is used for local iOS
1320                     // debugging only since all shared libraries in the shared cache do
1321                     // not have corresponding files that exist in the file system of the
1322                     // device. They have been combined into a single file. This means we
1323                     // always have to load these files from memory. All of the symbol and
1324                     // string tables from all of the __LINKEDIT sections from the shared
1325                     // libraries in the shared cache have been merged into a single large
1326                     // symbol and string table. Reading all of this symbol and string table
1327                     // data across can slow down debug launch times, so we optimize this by
1328                     // reading the memory for the __LINKEDIT section from this process.
1329                     PlatformSP platform_sp (target.GetPlatform());
1330                     if (platform_sp && platform_sp->IsHost())
1331                     {
1332                         data_was_read = true;
1333                         nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
1334                         strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
1335                         if (function_starts_load_command.cmd)
1336                         {
1337                             const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1338                             function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1339                         }
1340                     }
1341                 }
1342 #endif
1343 
1344                 if (!data_was_read)
1345                 {
1346                     DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1347                     if (nlist_data_sp)
1348                         nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
1349                     //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1350                     //if (strtab_data_sp)
1351                     //    strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
1352                     if (function_starts_load_command.cmd)
1353                     {
1354                         const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1355                         DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1356                         if (func_start_data_sp)
1357                             function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1358                     }
1359                 }
1360             }
1361         }
1362         else
1363         {
1364             nlist_data.SetData (m_data,
1365                                 symtab_load_command.symoff,
1366                                 nlist_data_byte_size);
1367             strtab_data.SetData (m_data,
1368                                  symtab_load_command.stroff,
1369                                  strtab_data_byte_size);
1370             if (function_starts_load_command.cmd)
1371             {
1372                 function_starts_data.SetData (m_data,
1373                                               function_starts_load_command.dataoff,
1374                                               function_starts_load_command.datasize);
1375             }
1376         }
1377 
1378         if (nlist_data.GetByteSize() == 0)
1379         {
1380             if (log)
1381                 module_sp->LogMessage(log.get(), "failed to read nlist data");
1382             return 0;
1383         }
1384 
1385 
1386         const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1387         if (!have_strtab_data)
1388         {
1389             if (process)
1390             {
1391                 if (strtab_addr == LLDB_INVALID_ADDRESS)
1392                 {
1393                     if (log)
1394                         module_sp->LogMessage(log.get(), "failed to locate the strtab in memory");
1395                     return 0;
1396                 }
1397             }
1398             else
1399             {
1400                 if (log)
1401                     module_sp->LogMessage(log.get(), "failed to read strtab data");
1402                 return 0;
1403             }
1404         }
1405 
1406         const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1407         const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1408         const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1409         const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1410         SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1411         SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1412         SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1413         SectionSP eh_frame_section_sp;
1414         if (text_section_sp.get())
1415             eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1416         else
1417             eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1418 
1419         const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
1420         if (text_section_sp && function_starts_data.GetByteSize())
1421         {
1422             FunctionStarts::Entry function_start_entry;
1423             function_start_entry.data = false;
1424             lldb::offset_t function_start_offset = 0;
1425             function_start_entry.addr = text_section_sp->GetFileAddress();
1426             uint64_t delta;
1427             while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1428             {
1429                 // Now append the current entry
1430                 function_start_entry.addr += delta;
1431                 function_starts.Append(function_start_entry);
1432             }
1433         }
1434 
1435         const size_t function_starts_count = function_starts.GetSize();
1436 
1437         const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
1438 
1439         lldb::offset_t nlist_data_offset = 0;
1440 
1441         uint32_t N_SO_index = UINT32_MAX;
1442 
1443         MachSymtabSectionInfo section_info (section_list);
1444         std::vector<uint32_t> N_FUN_indexes;
1445         std::vector<uint32_t> N_NSYM_indexes;
1446         std::vector<uint32_t> N_INCL_indexes;
1447         std::vector<uint32_t> N_BRAC_indexes;
1448         std::vector<uint32_t> N_COMM_indexes;
1449         typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1450         typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1451         ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1452         ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1453         // Any symbols that get merged into another will get an entry
1454         // in this map so we know
1455         NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1456         uint32_t nlist_idx = 0;
1457         Symbol *symbol_ptr = NULL;
1458 
1459         uint32_t sym_idx = 0;
1460         Symbol *sym = NULL;
1461         size_t num_syms = 0;
1462         std::string memory_symbol_name;
1463         uint32_t unmapped_local_symbols_found = 0;
1464 
1465 #if defined (__APPLE__) && defined (__arm__)
1466 
1467         // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1468         // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1469         // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1470         // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1471         // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1472         // nlist parser to ignore all LOCAL symbols.
1473 
1474         if (m_header.flags & 0x80000000u)
1475         {
1476             // Before we can start mapping the DSC, we need to make certain the target process is actually
1477             // using the cache we can find.
1478 
1479             /*
1480              * TODO (FIXME!)
1481              *
1482              * Consider the case of testing with a separate DSC file.
1483              * If we go through the normal code paths, we will give symbols for the wrong DSC, and
1484              * that is bad.  We need to read the target process' all_image_infos struct, and look
1485              * at the values of the processDetachedFromSharedRegion field. If that is set, we should skip
1486              * this code section.
1487              */
1488 
1489             // Next we need to determine the correct path for the dyld shared cache.
1490 
1491             ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1492             char dsc_path[PATH_MAX];
1493 
1494             snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
1495                      "/System/Library/Caches/com.apple.dyld/",	/* IPHONE_DYLD_SHARED_CACHE_DIR */
1496                      "dyld_shared_cache_",			/* DYLD_SHARED_CACHE_BASE_NAME */
1497                      header_arch.GetArchitectureName());
1498 
1499             FileSpec dsc_filespec(dsc_path, false);
1500 
1501             // We need definitions of two structures in the on-disk DSC, copy them here manually
1502             struct lldb_copy_dyld_cache_header
1503             {
1504                 char		magic[16];
1505                 uint32_t	mappingOffset;
1506                 uint32_t	mappingCount;
1507                 uint32_t	imagesOffset;
1508                 uint32_t	imagesCount;
1509                 uint64_t	dyldBaseAddress;
1510                 uint64_t	codeSignatureOffset;
1511                 uint64_t	codeSignatureSize;
1512                 uint64_t	slideInfoOffset;
1513                 uint64_t	slideInfoSize;
1514                 uint64_t	localSymbolsOffset;
1515                 uint64_t	localSymbolsSize;
1516             };
1517             struct lldb_copy_dyld_cache_local_symbols_info
1518             {
1519                     uint32_t        nlistOffset;
1520                     uint32_t        nlistCount;
1521                     uint32_t        stringsOffset;
1522                     uint32_t        stringsSize;
1523                     uint32_t        entriesOffset;
1524                     uint32_t        entriesCount;
1525             };
1526             struct lldb_copy_dyld_cache_local_symbols_entry
1527             {
1528                     uint32_t        dylibOffset;
1529                     uint32_t        nlistStartIndex;
1530                     uint32_t        nlistCount;
1531             };
1532 
1533             /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1534                The dyld_cache_local_symbols_info structure gives us three things:
1535                  1. The start and count of the nlist records in the dyld_shared_cache file
1536                  2. The start and size of the strings for these nlist records
1537                  3. The start and count of dyld_cache_local_symbols_entry entries
1538 
1539                There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1540                The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
1541                The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
1542                and the count of how many nlist records there are for this dylib/framework.
1543             */
1544 
1545             // Process the dsc header to find the unmapped symbols
1546             //
1547             // Save some VM space, do not map the entire cache in one shot.
1548 
1549             if (DataBufferSP dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header)))
1550             {
1551                 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
1552 
1553                 lldb::offset_t offset = offsetof (struct lldb_copy_dyld_cache_header, mappingOffset);
1554                 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1555 
1556                 // If the mappingOffset points to a location inside the header, we've
1557                 // opened an old dyld shared cache, and should not proceed further.
1558                 if (mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header))
1559                 {
1560 
1561                     offset = offsetof (struct lldb_copy_dyld_cache_header, localSymbolsOffset);
1562                     uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1563                     uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1564 
1565                     if (localSymbolsOffset && localSymbolsSize)
1566                     {
1567                         // Map the local symbols
1568                         if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
1569                         {
1570                             DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
1571 
1572                             offset = 0;
1573 
1574                             // Read the local_symbols_infos struct in one shot
1575                             struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1576                             dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1577 
1578                             // The local_symbols_infos offsets are offsets into local symbols memory, NOT file offsets!
1579                             // We first need to identify the local "entry" that matches the current header.
1580                             // The "entry" is stored as a file offset in the dyld_shared_cache, so we need to
1581                             // adjust the raw m_header value by slide and 0x30000000.
1582 
1583                             SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1584 
1585                             uint32_t header_file_offset = (text_section_sp->GetFileAddress() - 0x30000000);
1586 
1587                             offset = local_symbols_info.entriesOffset;
1588                             for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1589                             {
1590                                 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1591                                 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1592                                 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1593                                 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1594 
1595                                 if (header_file_offset == local_symbols_entry.dylibOffset)
1596                                 {
1597                                     unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1598 
1599                                     // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1600                                     sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1601                                     num_syms = symtab->GetNumSymbols();
1602 
1603                                     nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1604                                     uint32_t string_table_offset = local_symbols_info.stringsOffset;
1605 
1606                                     for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
1607                                     {
1608                                         /////////////////////////////
1609                                         {
1610                                             struct nlist_64 nlist;
1611                                             if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1612                                                 break;
1613 
1614                                             nlist.n_strx  = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1615                                             nlist.n_type  = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1616                                             nlist.n_sect  = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1617                                             nlist.n_desc  = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1618                                             nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1619 
1620                                             SymbolType type = eSymbolTypeInvalid;
1621                                             const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1622 
1623                                             if (symbol_name == NULL)
1624                                             {
1625                                                 // No symbol should be NULL, even the symbols with no
1626                                                 // string values should have an offset zero which points
1627                                                 // to an empty C-string
1628                                                 Host::SystemLog (Host::eSystemLogError,
1629                                                                  "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1630                                                                  entry_index,
1631                                                                  nlist.n_strx,
1632                                                                  module_sp->GetFileSpec().GetDirectory().GetCString(),
1633                                                                  module_sp->GetFileSpec().GetFilename().GetCString());
1634                                                 continue;
1635                                             }
1636                                             if (symbol_name[0] == '\0')
1637                                                 symbol_name = NULL;
1638 
1639                                             const char *symbol_name_non_abi_mangled = NULL;
1640 
1641                                             SectionSP symbol_section;
1642                                             uint32_t symbol_byte_size = 0;
1643                                             bool add_nlist = true;
1644                                             bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
1645                                             bool demangled_is_synthesized = false;
1646 
1647                                             assert (sym_idx < num_syms);
1648 
1649                                             sym[sym_idx].SetDebug (is_debug);
1650 
1651                                             if (is_debug)
1652                                             {
1653                                                 switch (nlist.n_type)
1654                                                 {
1655                                                     case StabGlobalSymbol:
1656                                                         // N_GSYM -- global symbol: name,,NO_SECT,type,0
1657                                                         // Sometimes the N_GSYM value contains the address.
1658 
1659                                                         // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data.  They
1660                                                         // have the same address, but we want to ensure that we always find only the real symbol,
1661                                                         // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1662                                                         // symbol type.  This is a temporary hack to make sure the ObjectiveC symbols get treated
1663                                                         // correctly.  To do this right, we should coalesce all the GSYM & global symbols that have the
1664                                                         // same address.
1665 
1666                                                         if (symbol_name && symbol_name[0] == '_' && symbol_name[1] ==  'O'
1667                                                             && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1668                                                                 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1669                                                                 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1670                                                             add_nlist = false;
1671                                                         else
1672                                                         {
1673                                                             sym[sym_idx].SetExternal(true);
1674                                                             if (nlist.n_value != 0)
1675                                                                 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1676                                                             type = eSymbolTypeData;
1677                                                         }
1678                                                         break;
1679 
1680                                                     case StabFunctionName:
1681                                                         // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1682                                                         type = eSymbolTypeCompiler;
1683                                                         break;
1684 
1685                                                     case StabFunction:
1686                                                         // N_FUN -- procedure: name,,n_sect,linenumber,address
1687                                                         if (symbol_name)
1688                                                         {
1689                                                             type = eSymbolTypeCode;
1690                                                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1691 
1692                                                             N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1693                                                             // We use the current number of symbols in the symbol table in lieu of
1694                                                             // using nlist_idx in case we ever start trimming entries out
1695                                                             N_FUN_indexes.push_back(sym_idx);
1696                                                         }
1697                                                         else
1698                                                         {
1699                                                             type = eSymbolTypeCompiler;
1700 
1701                                                             if ( !N_FUN_indexes.empty() )
1702                                                             {
1703                                                                 // Copy the size of the function into the original STAB entry so we don't have
1704                                                                 // to hunt for it later
1705                                                                 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1706                                                                 N_FUN_indexes.pop_back();
1707                                                                 // We don't really need the end function STAB as it contains the size which
1708                                                                 // we already placed with the original symbol, so don't add it if we want a
1709                                                                 // minimal symbol table
1710                                                                 if (minimize)
1711                                                                     add_nlist = false;
1712                                                             }
1713                                                         }
1714                                                         break;
1715 
1716                                                     case StabStaticSymbol:
1717                                                         // N_STSYM -- static symbol: name,,n_sect,type,address
1718                                                         N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1719                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1720                                                         type = eSymbolTypeData;
1721                                                         break;
1722 
1723                                                     case StabLocalCommon:
1724                                                         // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1725                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1726                                                         type = eSymbolTypeCommonBlock;
1727                                                         break;
1728 
1729                                                     case StabBeginSymbol:
1730                                                         // N_BNSYM
1731                                                         // We use the current number of symbols in the symbol table in lieu of
1732                                                         // using nlist_idx in case we ever start trimming entries out
1733                                                         if (minimize)
1734                                                         {
1735                                                             // Skip these if we want minimal symbol tables
1736                                                             add_nlist = false;
1737                                                         }
1738                                                         else
1739                                                         {
1740                                                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1741                                                             N_NSYM_indexes.push_back(sym_idx);
1742                                                             type = eSymbolTypeScopeBegin;
1743                                                         }
1744                                                         break;
1745 
1746                                                     case StabEndSymbol:
1747                                                         // N_ENSYM
1748                                                         // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1749                                                         // so that we can always skip the entire symbol if we need to navigate
1750                                                         // more quickly at the source level when parsing STABS
1751                                                         if (minimize)
1752                                                         {
1753                                                             // Skip these if we want minimal symbol tables
1754                                                             add_nlist = false;
1755                                                         }
1756                                                         else
1757                                                         {
1758                                                             if ( !N_NSYM_indexes.empty() )
1759                                                             {
1760                                                                 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1761                                                                 symbol_ptr->SetByteSize(sym_idx + 1);
1762                                                                 symbol_ptr->SetSizeIsSibling(true);
1763                                                                 N_NSYM_indexes.pop_back();
1764                                                             }
1765                                                             type = eSymbolTypeScopeEnd;
1766                                                         }
1767                                                         break;
1768 
1769 
1770                                                     case StabSourceFileOptions:
1771                                                         // N_OPT - emitted with gcc2_compiled and in gcc source
1772                                                         type = eSymbolTypeCompiler;
1773                                                         break;
1774 
1775                                                     case StabRegisterSymbol:
1776                                                         // N_RSYM - register sym: name,,NO_SECT,type,register
1777                                                         type = eSymbolTypeVariable;
1778                                                         break;
1779 
1780                                                     case StabSourceLine:
1781                                                         // N_SLINE - src line: 0,,n_sect,linenumber,address
1782                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1783                                                         type = eSymbolTypeLineEntry;
1784                                                         break;
1785 
1786                                                     case StabStructureType:
1787                                                         // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1788                                                         type = eSymbolTypeVariableType;
1789                                                         break;
1790 
1791                                                     case StabSourceFileName:
1792                                                         // N_SO - source file name
1793                                                         type = eSymbolTypeSourceFile;
1794                                                         if (symbol_name == NULL)
1795                                                         {
1796                                                             if (minimize)
1797                                                                 add_nlist = false;
1798                                                             if (N_SO_index != UINT32_MAX)
1799                                                             {
1800                                                                 // Set the size of the N_SO to the terminating index of this N_SO
1801                                                                 // so that we can always skip the entire N_SO if we need to navigate
1802                                                                 // more quickly at the source level when parsing STABS
1803                                                                 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1804                                                                 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1805                                                                 symbol_ptr->SetSizeIsSibling(true);
1806                                                             }
1807                                                             N_NSYM_indexes.clear();
1808                                                             N_INCL_indexes.clear();
1809                                                             N_BRAC_indexes.clear();
1810                                                             N_COMM_indexes.clear();
1811                                                             N_FUN_indexes.clear();
1812                                                             N_SO_index = UINT32_MAX;
1813                                                         }
1814                                                         else
1815                                                         {
1816                                                             // We use the current number of symbols in the symbol table in lieu of
1817                                                             // using nlist_idx in case we ever start trimming entries out
1818                                                             const bool N_SO_has_full_path = symbol_name[0] == '/';
1819                                                             if (N_SO_has_full_path)
1820                                                             {
1821                                                                 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1822                                                                 {
1823                                                                     // We have two consecutive N_SO entries where the first contains a directory
1824                                                                     // and the second contains a full path.
1825                                                                     sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
1826                                                                     m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1827                                                                     add_nlist = false;
1828                                                                 }
1829                                                                 else
1830                                                                 {
1831                                                                     // This is the first entry in a N_SO that contains a directory or
1832                                                                     // a full path to the source file
1833                                                                     N_SO_index = sym_idx;
1834                                                                 }
1835                                                             }
1836                                                             else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1837                                                             {
1838                                                                 // This is usually the second N_SO entry that contains just the filename,
1839                                                                 // so here we combine it with the first one if we are minimizing the symbol table
1840                                                                 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1841                                                                 if (so_path && so_path[0])
1842                                                                 {
1843                                                                     std::string full_so_path (so_path);
1844                                                                     const size_t double_slash_pos = full_so_path.find("//");
1845                                                                     if (double_slash_pos != std::string::npos)
1846                                                                     {
1847                                                                         // The linker has been generating bad N_SO entries with doubled up paths
1848                                                                         // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
1849                                                                         // and the second is the directory for the source file so you end up with
1850                                                                         // a path that looks like "/tmp/src//tmp/src/"
1851                                                                         FileSpec so_dir(so_path, false);
1852                                                                         if (!so_dir.Exists())
1853                                                                         {
1854                                                                             so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
1855                                                                             if (so_dir.Exists())
1856                                                                             {
1857                                                                                 // Trim off the incorrect path
1858                                                                                 full_so_path.erase(0, double_slash_pos + 1);
1859                                                                             }
1860                                                                         }
1861                                                                     }
1862                                                                     if (*full_so_path.rbegin() != '/')
1863                                                                         full_so_path += '/';
1864                                                                     full_so_path += symbol_name;
1865                                                                     sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
1866                                                                     add_nlist = false;
1867                                                                     m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1868                                                                 }
1869                                                             }
1870                                                             else
1871                                                             {
1872                                                                 // This could be a relative path to a N_SO
1873                                                                 N_SO_index = sym_idx;
1874                                                             }
1875                                                         }
1876                                                         break;
1877 
1878                                                     case StabObjectFileName:
1879                                                         // N_OSO - object file name: name,,0,0,st_mtime
1880                                                         type = eSymbolTypeObjectFile;
1881                                                         break;
1882 
1883                                                     case StabLocalSymbol:
1884                                                         // N_LSYM - local sym: name,,NO_SECT,type,offset
1885                                                         type = eSymbolTypeLocal;
1886                                                         break;
1887 
1888                                                         //----------------------------------------------------------------------
1889                                                         // INCL scopes
1890                                                         //----------------------------------------------------------------------
1891                                                     case StabBeginIncludeFileName:
1892                                                         // N_BINCL - include file beginning: name,,NO_SECT,0,sum
1893                                                         // We use the current number of symbols in the symbol table in lieu of
1894                                                         // using nlist_idx in case we ever start trimming entries out
1895                                                         N_INCL_indexes.push_back(sym_idx);
1896                                                         type = eSymbolTypeScopeBegin;
1897                                                         break;
1898 
1899                                                     case StabEndIncludeFile:
1900                                                         // N_EINCL - include file end: name,,NO_SECT,0,0
1901                                                         // Set the size of the N_BINCL to the terminating index of this N_EINCL
1902                                                         // so that we can always skip the entire symbol if we need to navigate
1903                                                         // more quickly at the source level when parsing STABS
1904                                                         if ( !N_INCL_indexes.empty() )
1905                                                         {
1906                                                             symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1907                                                             symbol_ptr->SetByteSize(sym_idx + 1);
1908                                                             symbol_ptr->SetSizeIsSibling(true);
1909                                                             N_INCL_indexes.pop_back();
1910                                                         }
1911                                                         type = eSymbolTypeScopeEnd;
1912                                                         break;
1913 
1914                                                     case StabIncludeFileName:
1915                                                         // N_SOL - #included file name: name,,n_sect,0,address
1916                                                         type = eSymbolTypeHeaderFile;
1917 
1918                                                         // We currently don't use the header files on darwin
1919                                                         if (minimize)
1920                                                             add_nlist = false;
1921                                                         break;
1922 
1923                                                     case StabCompilerParameters:
1924                                                         // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
1925                                                         type = eSymbolTypeCompiler;
1926                                                         break;
1927 
1928                                                     case StabCompilerVersion:
1929                                                         // N_VERSION - compiler version: name,,NO_SECT,0,0
1930                                                         type = eSymbolTypeCompiler;
1931                                                         break;
1932 
1933                                                     case StabCompilerOptLevel:
1934                                                         // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
1935                                                         type = eSymbolTypeCompiler;
1936                                                         break;
1937 
1938                                                     case StabParameter:
1939                                                         // N_PSYM - parameter: name,,NO_SECT,type,offset
1940                                                         type = eSymbolTypeVariable;
1941                                                         break;
1942 
1943                                                     case StabAlternateEntry:
1944                                                         // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
1945                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1946                                                         type = eSymbolTypeLineEntry;
1947                                                         break;
1948 
1949                                                         //----------------------------------------------------------------------
1950                                                         // Left and Right Braces
1951                                                         //----------------------------------------------------------------------
1952                                                     case StabLeftBracket:
1953                                                         // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
1954                                                         // We use the current number of symbols in the symbol table in lieu of
1955                                                         // using nlist_idx in case we ever start trimming entries out
1956                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1957                                                         N_BRAC_indexes.push_back(sym_idx);
1958                                                         type = eSymbolTypeScopeBegin;
1959                                                         break;
1960 
1961                                                     case StabRightBracket:
1962                                                         // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
1963                                                         // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1964                                                         // so that we can always skip the entire symbol if we need to navigate
1965                                                         // more quickly at the source level when parsing STABS
1966                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1967                                                         if ( !N_BRAC_indexes.empty() )
1968                                                         {
1969                                                             symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1970                                                             symbol_ptr->SetByteSize(sym_idx + 1);
1971                                                             symbol_ptr->SetSizeIsSibling(true);
1972                                                             N_BRAC_indexes.pop_back();
1973                                                         }
1974                                                         type = eSymbolTypeScopeEnd;
1975                                                         break;
1976 
1977                                                     case StabDeletedIncludeFile:
1978                                                         // N_EXCL - deleted include file: name,,NO_SECT,0,sum
1979                                                         type = eSymbolTypeHeaderFile;
1980                                                         break;
1981 
1982                                                         //----------------------------------------------------------------------
1983                                                         // COMM scopes
1984                                                         //----------------------------------------------------------------------
1985                                                     case StabBeginCommon:
1986                                                         // N_BCOMM - begin common: name,,NO_SECT,0,0
1987                                                         // We use the current number of symbols in the symbol table in lieu of
1988                                                         // using nlist_idx in case we ever start trimming entries out
1989                                                         type = eSymbolTypeScopeBegin;
1990                                                         N_COMM_indexes.push_back(sym_idx);
1991                                                         break;
1992 
1993                                                     case StabEndCommonLocal:
1994                                                         // N_ECOML - end common (local name): 0,,n_sect,0,address
1995                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1996                                                         // Fall through
1997 
1998                                                     case StabEndCommon:
1999                                                         // N_ECOMM - end common: name,,n_sect,0,0
2000                                                         // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2001                                                         // so that we can always skip the entire symbol if we need to navigate
2002                                                         // more quickly at the source level when parsing STABS
2003                                                         if ( !N_COMM_indexes.empty() )
2004                                                         {
2005                                                             symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2006                                                             symbol_ptr->SetByteSize(sym_idx + 1);
2007                                                             symbol_ptr->SetSizeIsSibling(true);
2008                                                             N_COMM_indexes.pop_back();
2009                                                         }
2010                                                         type = eSymbolTypeScopeEnd;
2011                                                         break;
2012 
2013                                                     case StabLength:
2014                                                         // N_LENG - second stab entry with length information
2015                                                         type = eSymbolTypeAdditional;
2016                                                         break;
2017 
2018                                                     default: break;
2019                                                 }
2020                                             }
2021                                             else
2022                                             {
2023                                                 //uint8_t n_pext    = NlistMaskPrivateExternal & nlist.n_type;
2024                                                 uint8_t n_type  = NlistMaskType & nlist.n_type;
2025                                                 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2026 
2027                                                 switch (n_type)
2028                                                 {
2029                                                     case NListTypeIndirect:         // N_INDR - Fall through
2030                                                     case NListTypePreboundUndefined:// N_PBUD - Fall through
2031                                                     case NListTypeUndefined:        // N_UNDF
2032                                                         type = eSymbolTypeUndefined;
2033                                                         break;
2034 
2035                                                     case NListTypeAbsolute:         // N_ABS
2036                                                         type = eSymbolTypeAbsolute;
2037                                                         break;
2038 
2039                                                     case NListTypeSection:          // N_SECT
2040                                                         {
2041                                                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2042 
2043                                                             if (symbol_section == NULL)
2044                                                             {
2045                                                                 // TODO: warn about this?
2046                                                                 add_nlist = false;
2047                                                                 break;
2048                                                             }
2049 
2050                                                             if (TEXT_eh_frame_sectID == nlist.n_sect)
2051                                                             {
2052                                                                 type = eSymbolTypeException;
2053                                                             }
2054                                                             else
2055                                                             {
2056                                                                 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2057 
2058                                                                 switch (section_type)
2059                                                                 {
2060                                                                     case SectionTypeRegular:                     break; // regular section
2061                                                                                                                         //case SectionTypeZeroFill:                 type = eSymbolTypeData;    break; // zero fill on demand section
2062                                                                     case SectionTypeCStringLiterals:            type = eSymbolTypeData;    break; // section with only literal C strings
2063                                                                     case SectionType4ByteLiterals:              type = eSymbolTypeData;    break; // section with only 4 byte literals
2064                                                                     case SectionType8ByteLiterals:              type = eSymbolTypeData;    break; // section with only 8 byte literals
2065                                                                     case SectionTypeLiteralPointers:            type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2066                                                                     case SectionTypeNonLazySymbolPointers:      type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2067                                                                     case SectionTypeLazySymbolPointers:         type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2068                                                                     case SectionTypeSymbolStubs:                type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2069                                                                     case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for initialization
2070                                                                     case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for termination
2071                                                                                                                                                   //case SectionTypeCoalesced:                type = eSymbolType;    break; // section contains symbols that are to be coalesced
2072                                                                                                                                                   //case SectionTypeZeroFillLarge:            type = eSymbolTypeData;    break; // zero fill on demand section (that can be larger than 4 gigabytes)
2073                                                                     case SectionTypeInterposing:                type = eSymbolTypeTrampoline;  break; // section with only pairs of function pointers for interposing
2074                                                                     case SectionType16ByteLiterals:             type = eSymbolTypeData;    break; // section with only 16 byte literals
2075                                                                     case SectionTypeDTraceObjectFormat:         type = eSymbolTypeInstrumentation; break;
2076                                                                     case SectionTypeLazyDylibSymbolPointers:    type = eSymbolTypeTrampoline; break;
2077                                                                     default: break;
2078                                                                 }
2079 
2080                                                                 if (type == eSymbolTypeInvalid)
2081                                                                 {
2082                                                                     const char *symbol_sect_name = symbol_section->GetName().AsCString();
2083                                                                     if (symbol_section->IsDescendant (text_section_sp.get()))
2084                                                                     {
2085                                                                         if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2086                                                                                                     SectionAttrUserSelfModifyingCode |
2087                                                                                                     SectionAttrSytemSomeInstructions))
2088                                                                             type = eSymbolTypeData;
2089                                                                         else
2090                                                                             type = eSymbolTypeCode;
2091                                                                     }
2092                                                                     else if (symbol_section->IsDescendant(data_section_sp.get()))
2093                                                                     {
2094                                                                         if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2095                                                                         {
2096                                                                             type = eSymbolTypeRuntime;
2097 
2098                                                                             if (symbol_name &&
2099                                                                                 symbol_name[0] == '_' &&
2100                                                                                 symbol_name[1] == 'O' &&
2101                                                                                 symbol_name[2] == 'B')
2102                                                                             {
2103                                                                                 llvm::StringRef symbol_name_ref(symbol_name);
2104                                                                                 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2105                                                                                 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2106                                                                                 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2107                                                                                 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2108                                                                                 {
2109                                                                                     symbol_name_non_abi_mangled = symbol_name + 1;
2110                                                                                     symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2111                                                                                     type = eSymbolTypeObjCClass;
2112                                                                                     demangled_is_synthesized = true;
2113                                                                                 }
2114                                                                                 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2115                                                                                 {
2116                                                                                     symbol_name_non_abi_mangled = symbol_name + 1;
2117                                                                                     symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2118                                                                                     type = eSymbolTypeObjCMetaClass;
2119                                                                                     demangled_is_synthesized = true;
2120                                                                                 }
2121                                                                                 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2122                                                                                 {
2123                                                                                     symbol_name_non_abi_mangled = symbol_name + 1;
2124                                                                                     symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2125                                                                                     type = eSymbolTypeObjCIVar;
2126                                                                                     demangled_is_synthesized = true;
2127                                                                                 }
2128                                                                             }
2129                                                                         }
2130                                                                         else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2131                                                                         {
2132                                                                             type = eSymbolTypeException;
2133                                                                         }
2134                                                                         else
2135                                                                         {
2136                                                                             type = eSymbolTypeData;
2137                                                                         }
2138                                                                     }
2139                                                                     else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2140                                                                     {
2141                                                                         type = eSymbolTypeTrampoline;
2142                                                                     }
2143                                                                     else if (symbol_section->IsDescendant(objc_section_sp.get()))
2144                                                                     {
2145                                                                         type = eSymbolTypeRuntime;
2146                                                                         if (symbol_name && symbol_name[0] == '.')
2147                                                                         {
2148                                                                             llvm::StringRef symbol_name_ref(symbol_name);
2149                                                                             static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2150                                                                             if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
2151                                                                             {
2152                                                                                 symbol_name_non_abi_mangled = symbol_name;
2153                                                                                 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2154                                                                                 type = eSymbolTypeObjCClass;
2155                                                                                 demangled_is_synthesized = true;
2156                                                                             }
2157                                                                         }
2158                                                                     }
2159                                                                 }
2160                                                             }
2161                                                         }
2162                                                         break;
2163                                                 }
2164                                             }
2165 
2166                                             if (add_nlist)
2167                                             {
2168                                                 uint64_t symbol_value = nlist.n_value;
2169                                                 bool symbol_name_is_mangled = false;
2170 
2171                                                 if (symbol_name_non_abi_mangled)
2172                                                 {
2173                                                     sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2174                                                     sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
2175                                                 }
2176                                                 else
2177                                                 {
2178                                                     if (symbol_name && symbol_name[0] == '_')
2179                                                     {
2180                                                         symbol_name_is_mangled = symbol_name[1] == '_';
2181                                                         symbol_name++;  // Skip the leading underscore
2182                                                     }
2183 
2184                                                     if (symbol_name)
2185                                                     {
2186                                                         sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
2187                                                     }
2188                                                 }
2189 
2190                                                 if (is_debug == false)
2191                                                 {
2192                                                     if (type == eSymbolTypeCode)
2193                                                     {
2194                                                         // See if we can find a N_FUN entry for any code symbols.
2195                                                         // If we do find a match, and the name matches, then we
2196                                                         // can merge the two into just the function symbol to avoid
2197                                                         // duplicate entries in the symbol table
2198                                                         ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2199                                                         if (pos != N_FUN_addr_to_sym_idx.end())
2200                                                         {
2201                                                             if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2202                                                                 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2203                                                             {
2204                                                                 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2205                                                                 // We just need the flags from the linker symbol, so put these flags
2206                                                                 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2207                                                                 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2208                                                                 sym[sym_idx].Clear();
2209                                                                 continue;
2210                                                             }
2211                                                         }
2212                                                     }
2213                                                     else if (type == eSymbolTypeData)
2214                                                     {
2215                                                         // See if we can find a N_STSYM entry for any data symbols.
2216                                                         // If we do find a match, and the name matches, then we
2217                                                         // can merge the two into just the Static symbol to avoid
2218                                                         // duplicate entries in the symbol table
2219                                                         ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2220                                                         if (pos != N_STSYM_addr_to_sym_idx.end())
2221                                                         {
2222                                                             if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2223                                                                 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2224                                                             {
2225                                                                 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2226                                                                 // We just need the flags from the linker symbol, so put these flags
2227                                                                 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2228                                                                 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2229                                                                 sym[sym_idx].Clear();
2230                                                                 continue;
2231                                                             }
2232                                                         }
2233                                                     }
2234                                                 }
2235                                                 if (symbol_section)
2236                                                 {
2237                                                     const addr_t section_file_addr = symbol_section->GetFileAddress();
2238                                                     if (symbol_byte_size == 0 && function_starts_count > 0)
2239                                                     {
2240                                                         addr_t symbol_lookup_file_addr = nlist.n_value;
2241                                                         // Do an exact address match for non-ARM addresses, else get the closest since
2242                                                         // the symbol might be a thumb symbol which has an address with bit zero set
2243                                                         FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2244                                                         if (is_arm && func_start_entry)
2245                                                         {
2246                                                             // Verify that the function start address is the symbol address (ARM)
2247                                                             // or the symbol address + 1 (thumb)
2248                                                             if (func_start_entry->addr != symbol_lookup_file_addr &&
2249                                                                 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2250                                                             {
2251                                                                 // Not the right entry, NULL it out...
2252                                                                 func_start_entry = NULL;
2253                                                             }
2254                                                         }
2255                                                         if (func_start_entry)
2256                                                         {
2257                                                             func_start_entry->data = true;
2258 
2259                                                             addr_t symbol_file_addr = func_start_entry->addr;
2260                                                             uint32_t symbol_flags = 0;
2261                                                             if (is_arm)
2262                                                             {
2263                                                                 if (symbol_file_addr & 1)
2264                                                                     symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2265                                                                 symbol_file_addr &= 0xfffffffffffffffeull;
2266                                                             }
2267 
2268                                                             const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2269                                                             const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2270                                                             if (next_func_start_entry)
2271                                                             {
2272                                                                 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2273                                                                 // Be sure the clear the Thumb address bit when we calculate the size
2274                                                                 // from the current and next address
2275                                                                 if (is_arm)
2276                                                                     next_symbol_file_addr &= 0xfffffffffffffffeull;
2277                                                                 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2278                                                             }
2279                                                             else
2280                                                             {
2281                                                                 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2282                                                             }
2283                                                         }
2284                                                     }
2285                                                     symbol_value -= section_file_addr;
2286                                                 }
2287 
2288                                                 sym[sym_idx].SetID (nlist_idx);
2289                                                 sym[sym_idx].SetType (type);
2290                                                 sym[sym_idx].GetAddress().SetSection (symbol_section);
2291                                                 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2292                                                 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2293 
2294                                                 if (symbol_byte_size > 0)
2295                                                     sym[sym_idx].SetByteSize(symbol_byte_size);
2296 
2297                                                 if (demangled_is_synthesized)
2298                                                     sym[sym_idx].SetDemangledNameIsSynthesized(true);
2299                                                 ++sym_idx;
2300                                             }
2301                                             else
2302                                             {
2303                                                 sym[sym_idx].Clear();
2304                                             }
2305 
2306                                         }
2307                                         /////////////////////////////
2308                                     }
2309                                     break; // No more entries to consider
2310                                 }
2311                             }
2312                         }
2313                     }
2314                 }
2315             }
2316         }
2317 
2318         // Must reset this in case it was mutated above!
2319         nlist_data_offset = 0;
2320 #endif
2321 
2322         // If the sym array was not created while parsing the DSC unmapped
2323         // symbols, create it now.
2324         if (sym == NULL)
2325         {
2326             sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2327             num_syms = symtab->GetNumSymbols();
2328         }
2329 
2330         if (unmapped_local_symbols_found)
2331         {
2332             assert(m_dysymtab.ilocalsym == 0);
2333             nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2334             nlist_idx = m_dysymtab.nlocalsym;
2335         }
2336         else
2337         {
2338             nlist_idx = 0;
2339         }
2340 
2341         for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
2342         {
2343             struct nlist_64 nlist;
2344             if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2345                 break;
2346 
2347             nlist.n_strx  = nlist_data.GetU32_unchecked(&nlist_data_offset);
2348             nlist.n_type  = nlist_data.GetU8_unchecked (&nlist_data_offset);
2349             nlist.n_sect  = nlist_data.GetU8_unchecked (&nlist_data_offset);
2350             nlist.n_desc  = nlist_data.GetU16_unchecked (&nlist_data_offset);
2351             nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2352 
2353             SymbolType type = eSymbolTypeInvalid;
2354             const char *symbol_name = NULL;
2355 
2356             if (have_strtab_data)
2357             {
2358                 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
2359 
2360                 if (symbol_name == NULL)
2361                 {
2362                     // No symbol should be NULL, even the symbols with no
2363                     // string values should have an offset zero which points
2364                     // to an empty C-string
2365                     Host::SystemLog (Host::eSystemLogError,
2366                                      "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
2367                                      nlist_idx,
2368                                      nlist.n_strx,
2369                                      module_sp->GetFileSpec().GetDirectory().GetCString(),
2370                                      module_sp->GetFileSpec().GetFilename().GetCString());
2371                     continue;
2372                 }
2373                 if (symbol_name[0] == '\0')
2374                     symbol_name = NULL;
2375             }
2376             else
2377             {
2378                 const addr_t str_addr = strtab_addr + nlist.n_strx;
2379                 Error str_error;
2380                 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2381                     symbol_name = memory_symbol_name.c_str();
2382             }
2383             const char *symbol_name_non_abi_mangled = NULL;
2384 
2385             SectionSP symbol_section;
2386             lldb::addr_t symbol_byte_size = 0;
2387             bool add_nlist = true;
2388             bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
2389             bool demangled_is_synthesized = false;
2390 
2391             assert (sym_idx < num_syms);
2392 
2393             sym[sym_idx].SetDebug (is_debug);
2394 
2395             if (is_debug)
2396             {
2397                 switch (nlist.n_type)
2398                 {
2399                 case StabGlobalSymbol:
2400                     // N_GSYM -- global symbol: name,,NO_SECT,type,0
2401                     // Sometimes the N_GSYM value contains the address.
2402 
2403                     // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data.  They
2404                     // have the same address, but we want to ensure that we always find only the real symbol,
2405                     // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2406                     // symbol type.  This is a temporary hack to make sure the ObjectiveC symbols get treated
2407                     // correctly.  To do this right, we should coalesce all the GSYM & global symbols that have the
2408                     // same address.
2409 
2410                     if (symbol_name && symbol_name[0] == '_' && symbol_name[1] ==  'O'
2411                         && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2412                             || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2413                             || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2414                         add_nlist = false;
2415                     else
2416                     {
2417                         sym[sym_idx].SetExternal(true);
2418                         if (nlist.n_value != 0)
2419                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2420                         type = eSymbolTypeData;
2421                     }
2422                     break;
2423 
2424                 case StabFunctionName:
2425                     // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2426                     type = eSymbolTypeCompiler;
2427                     break;
2428 
2429                 case StabFunction:
2430                     // N_FUN -- procedure: name,,n_sect,linenumber,address
2431                     if (symbol_name)
2432                     {
2433                         type = eSymbolTypeCode;
2434                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2435 
2436                         N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2437                         // We use the current number of symbols in the symbol table in lieu of
2438                         // using nlist_idx in case we ever start trimming entries out
2439                         N_FUN_indexes.push_back(sym_idx);
2440                     }
2441                     else
2442                     {
2443                         type = eSymbolTypeCompiler;
2444 
2445                         if ( !N_FUN_indexes.empty() )
2446                         {
2447                             // Copy the size of the function into the original STAB entry so we don't have
2448                             // to hunt for it later
2449                             symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2450                             N_FUN_indexes.pop_back();
2451                             // We don't really need the end function STAB as it contains the size which
2452                             // we already placed with the original symbol, so don't add it if we want a
2453                             // minimal symbol table
2454                             if (minimize)
2455                                 add_nlist = false;
2456                         }
2457                     }
2458                     break;
2459 
2460                 case StabStaticSymbol:
2461                     // N_STSYM -- static symbol: name,,n_sect,type,address
2462                     N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2463                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2464                     type = eSymbolTypeData;
2465                     break;
2466 
2467                 case StabLocalCommon:
2468                     // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2469                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2470                     type = eSymbolTypeCommonBlock;
2471                     break;
2472 
2473                 case StabBeginSymbol:
2474                     // N_BNSYM
2475                     // We use the current number of symbols in the symbol table in lieu of
2476                     // using nlist_idx in case we ever start trimming entries out
2477                     if (minimize)
2478                     {
2479                         // Skip these if we want minimal symbol tables
2480                         add_nlist = false;
2481                     }
2482                     else
2483                     {
2484                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2485                         N_NSYM_indexes.push_back(sym_idx);
2486                         type = eSymbolTypeScopeBegin;
2487                     }
2488                     break;
2489 
2490                 case StabEndSymbol:
2491                     // N_ENSYM
2492                     // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2493                     // so that we can always skip the entire symbol if we need to navigate
2494                     // more quickly at the source level when parsing STABS
2495                     if (minimize)
2496                     {
2497                         // Skip these if we want minimal symbol tables
2498                         add_nlist = false;
2499                     }
2500                     else
2501                     {
2502                         if ( !N_NSYM_indexes.empty() )
2503                         {
2504                             symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2505                             symbol_ptr->SetByteSize(sym_idx + 1);
2506                             symbol_ptr->SetSizeIsSibling(true);
2507                             N_NSYM_indexes.pop_back();
2508                         }
2509                         type = eSymbolTypeScopeEnd;
2510                     }
2511                     break;
2512 
2513 
2514                 case StabSourceFileOptions:
2515                     // N_OPT - emitted with gcc2_compiled and in gcc source
2516                     type = eSymbolTypeCompiler;
2517                     break;
2518 
2519                 case StabRegisterSymbol:
2520                     // N_RSYM - register sym: name,,NO_SECT,type,register
2521                     type = eSymbolTypeVariable;
2522                     break;
2523 
2524                 case StabSourceLine:
2525                     // N_SLINE - src line: 0,,n_sect,linenumber,address
2526                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2527                     type = eSymbolTypeLineEntry;
2528                     break;
2529 
2530                 case StabStructureType:
2531                     // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2532                     type = eSymbolTypeVariableType;
2533                     break;
2534 
2535                 case StabSourceFileName:
2536                     // N_SO - source file name
2537                     type = eSymbolTypeSourceFile;
2538                     if (symbol_name == NULL)
2539                     {
2540                         if (minimize)
2541                             add_nlist = false;
2542                         if (N_SO_index != UINT32_MAX)
2543                         {
2544                             // Set the size of the N_SO to the terminating index of this N_SO
2545                             // so that we can always skip the entire N_SO if we need to navigate
2546                             // more quickly at the source level when parsing STABS
2547                             symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2548                             symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2549                             symbol_ptr->SetSizeIsSibling(true);
2550                         }
2551                         N_NSYM_indexes.clear();
2552                         N_INCL_indexes.clear();
2553                         N_BRAC_indexes.clear();
2554                         N_COMM_indexes.clear();
2555                         N_FUN_indexes.clear();
2556                         N_SO_index = UINT32_MAX;
2557                     }
2558                     else
2559                     {
2560                         // We use the current number of symbols in the symbol table in lieu of
2561                         // using nlist_idx in case we ever start trimming entries out
2562                         const bool N_SO_has_full_path = symbol_name[0] == '/';
2563                         if (N_SO_has_full_path)
2564                         {
2565                             if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2566                             {
2567                                 // We have two consecutive N_SO entries where the first contains a directory
2568                                 // and the second contains a full path.
2569                                 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
2570                                 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2571                                 add_nlist = false;
2572                             }
2573                             else
2574                             {
2575                                 // This is the first entry in a N_SO that contains a directory or
2576                                 // a full path to the source file
2577                                 N_SO_index = sym_idx;
2578                             }
2579                         }
2580                         else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2581                         {
2582                             // This is usually the second N_SO entry that contains just the filename,
2583                             // so here we combine it with the first one if we are minimizing the symbol table
2584                             const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2585                             if (so_path && so_path[0])
2586                             {
2587                                 std::string full_so_path (so_path);
2588                                 const size_t double_slash_pos = full_so_path.find("//");
2589                                 if (double_slash_pos != std::string::npos)
2590                                 {
2591                                     // The linker has been generating bad N_SO entries with doubled up paths
2592                                     // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2593                                     // and the second is the directory for the source file so you end up with
2594                                     // a path that looks like "/tmp/src//tmp/src/"
2595                                     FileSpec so_dir(so_path, false);
2596                                     if (!so_dir.Exists())
2597                                     {
2598                                         so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2599                                         if (so_dir.Exists())
2600                                         {
2601                                             // Trim off the incorrect path
2602                                             full_so_path.erase(0, double_slash_pos + 1);
2603                                         }
2604                                     }
2605                                 }
2606                                 if (*full_so_path.rbegin() != '/')
2607                                     full_so_path += '/';
2608                                 full_so_path += symbol_name;
2609                                 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
2610                                 add_nlist = false;
2611                                 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2612                             }
2613                         }
2614                         else
2615                         {
2616                             // This could be a relative path to a N_SO
2617                             N_SO_index = sym_idx;
2618                         }
2619                     }
2620 
2621                     break;
2622 
2623                 case StabObjectFileName:
2624                     // N_OSO - object file name: name,,0,0,st_mtime
2625                     type = eSymbolTypeObjectFile;
2626                     break;
2627 
2628                 case StabLocalSymbol:
2629                     // N_LSYM - local sym: name,,NO_SECT,type,offset
2630                     type = eSymbolTypeLocal;
2631                     break;
2632 
2633                 //----------------------------------------------------------------------
2634                 // INCL scopes
2635                 //----------------------------------------------------------------------
2636                 case StabBeginIncludeFileName:
2637                     // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2638                     // We use the current number of symbols in the symbol table in lieu of
2639                     // using nlist_idx in case we ever start trimming entries out
2640                     N_INCL_indexes.push_back(sym_idx);
2641                     type = eSymbolTypeScopeBegin;
2642                     break;
2643 
2644                 case StabEndIncludeFile:
2645                     // N_EINCL - include file end: name,,NO_SECT,0,0
2646                     // Set the size of the N_BINCL to the terminating index of this N_EINCL
2647                     // so that we can always skip the entire symbol if we need to navigate
2648                     // more quickly at the source level when parsing STABS
2649                     if ( !N_INCL_indexes.empty() )
2650                     {
2651                         symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2652                         symbol_ptr->SetByteSize(sym_idx + 1);
2653                         symbol_ptr->SetSizeIsSibling(true);
2654                         N_INCL_indexes.pop_back();
2655                     }
2656                     type = eSymbolTypeScopeEnd;
2657                     break;
2658 
2659                 case StabIncludeFileName:
2660                     // N_SOL - #included file name: name,,n_sect,0,address
2661                     type = eSymbolTypeHeaderFile;
2662 
2663                     // We currently don't use the header files on darwin
2664                     if (minimize)
2665                         add_nlist = false;
2666                     break;
2667 
2668                 case StabCompilerParameters:
2669                     // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2670                     type = eSymbolTypeCompiler;
2671                     break;
2672 
2673                 case StabCompilerVersion:
2674                     // N_VERSION - compiler version: name,,NO_SECT,0,0
2675                     type = eSymbolTypeCompiler;
2676                     break;
2677 
2678                 case StabCompilerOptLevel:
2679                     // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2680                     type = eSymbolTypeCompiler;
2681                     break;
2682 
2683                 case StabParameter:
2684                     // N_PSYM - parameter: name,,NO_SECT,type,offset
2685                     type = eSymbolTypeVariable;
2686                     break;
2687 
2688                 case StabAlternateEntry:
2689                     // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2690                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2691                     type = eSymbolTypeLineEntry;
2692                     break;
2693 
2694                 //----------------------------------------------------------------------
2695                 // Left and Right Braces
2696                 //----------------------------------------------------------------------
2697                 case StabLeftBracket:
2698                     // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2699                     // We use the current number of symbols in the symbol table in lieu of
2700                     // using nlist_idx in case we ever start trimming entries out
2701                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2702                     N_BRAC_indexes.push_back(sym_idx);
2703                     type = eSymbolTypeScopeBegin;
2704                     break;
2705 
2706                 case StabRightBracket:
2707                     // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2708                     // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2709                     // so that we can always skip the entire symbol if we need to navigate
2710                     // more quickly at the source level when parsing STABS
2711                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2712                     if ( !N_BRAC_indexes.empty() )
2713                     {
2714                         symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2715                         symbol_ptr->SetByteSize(sym_idx + 1);
2716                         symbol_ptr->SetSizeIsSibling(true);
2717                         N_BRAC_indexes.pop_back();
2718                     }
2719                     type = eSymbolTypeScopeEnd;
2720                     break;
2721 
2722                 case StabDeletedIncludeFile:
2723                     // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2724                     type = eSymbolTypeHeaderFile;
2725                     break;
2726 
2727                 //----------------------------------------------------------------------
2728                 // COMM scopes
2729                 //----------------------------------------------------------------------
2730                 case StabBeginCommon:
2731                     // N_BCOMM - begin common: name,,NO_SECT,0,0
2732                     // We use the current number of symbols in the symbol table in lieu of
2733                     // using nlist_idx in case we ever start trimming entries out
2734                     type = eSymbolTypeScopeBegin;
2735                     N_COMM_indexes.push_back(sym_idx);
2736                     break;
2737 
2738                 case StabEndCommonLocal:
2739                     // N_ECOML - end common (local name): 0,,n_sect,0,address
2740                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2741                     // Fall through
2742 
2743                 case StabEndCommon:
2744                     // N_ECOMM - end common: name,,n_sect,0,0
2745                     // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2746                     // so that we can always skip the entire symbol if we need to navigate
2747                     // more quickly at the source level when parsing STABS
2748                     if ( !N_COMM_indexes.empty() )
2749                     {
2750                         symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2751                         symbol_ptr->SetByteSize(sym_idx + 1);
2752                         symbol_ptr->SetSizeIsSibling(true);
2753                         N_COMM_indexes.pop_back();
2754                     }
2755                     type = eSymbolTypeScopeEnd;
2756                     break;
2757 
2758                 case StabLength:
2759                     // N_LENG - second stab entry with length information
2760                     type = eSymbolTypeAdditional;
2761                     break;
2762 
2763                 default: break;
2764                 }
2765             }
2766             else
2767             {
2768                 //uint8_t n_pext    = NlistMaskPrivateExternal & nlist.n_type;
2769                 uint8_t n_type  = NlistMaskType & nlist.n_type;
2770                 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2771 
2772                 switch (n_type)
2773                 {
2774                 case NListTypeIndirect:         // N_INDR - Fall through
2775                 case NListTypePreboundUndefined:// N_PBUD - Fall through
2776                 case NListTypeUndefined:        // N_UNDF
2777                     type = eSymbolTypeUndefined;
2778                     break;
2779 
2780                 case NListTypeAbsolute:         // N_ABS
2781                     type = eSymbolTypeAbsolute;
2782                     break;
2783 
2784                 case NListTypeSection:          // N_SECT
2785                     {
2786                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2787 
2788                         if (!symbol_section)
2789                         {
2790                             // TODO: warn about this?
2791                             add_nlist = false;
2792                             break;
2793                         }
2794 
2795                         if (TEXT_eh_frame_sectID == nlist.n_sect)
2796                         {
2797                             type = eSymbolTypeException;
2798                         }
2799                         else
2800                         {
2801                             uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2802 
2803                             switch (section_type)
2804                             {
2805                             case SectionTypeRegular:                     break; // regular section
2806                             //case SectionTypeZeroFill:                 type = eSymbolTypeData;    break; // zero fill on demand section
2807                             case SectionTypeCStringLiterals:            type = eSymbolTypeData;    break; // section with only literal C strings
2808                             case SectionType4ByteLiterals:              type = eSymbolTypeData;    break; // section with only 4 byte literals
2809                             case SectionType8ByteLiterals:              type = eSymbolTypeData;    break; // section with only 8 byte literals
2810                             case SectionTypeLiteralPointers:            type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2811                             case SectionTypeNonLazySymbolPointers:      type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2812                             case SectionTypeLazySymbolPointers:         type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2813                             case SectionTypeSymbolStubs:                type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2814                             case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for initialization
2815                             case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for termination
2816                             //case SectionTypeCoalesced:                type = eSymbolType;    break; // section contains symbols that are to be coalesced
2817                             //case SectionTypeZeroFillLarge:            type = eSymbolTypeData;    break; // zero fill on demand section (that can be larger than 4 gigabytes)
2818                             case SectionTypeInterposing:                type = eSymbolTypeTrampoline;  break; // section with only pairs of function pointers for interposing
2819                             case SectionType16ByteLiterals:             type = eSymbolTypeData;    break; // section with only 16 byte literals
2820                             case SectionTypeDTraceObjectFormat:         type = eSymbolTypeInstrumentation; break;
2821                             case SectionTypeLazyDylibSymbolPointers:    type = eSymbolTypeTrampoline; break;
2822                             default: break;
2823                             }
2824 
2825                             if (type == eSymbolTypeInvalid)
2826                             {
2827                                 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2828                                 if (symbol_section->IsDescendant (text_section_sp.get()))
2829                                 {
2830                                     if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2831                                                                 SectionAttrUserSelfModifyingCode |
2832                                                                 SectionAttrSytemSomeInstructions))
2833                                         type = eSymbolTypeData;
2834                                     else
2835                                         type = eSymbolTypeCode;
2836                                 }
2837                                 else
2838                                 if (symbol_section->IsDescendant(data_section_sp.get()))
2839                                 {
2840                                     if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2841                                     {
2842                                         type = eSymbolTypeRuntime;
2843 
2844                                         if (symbol_name &&
2845                                             symbol_name[0] == '_' &&
2846                                             symbol_name[1] == 'O' &&
2847                                             symbol_name[2] == 'B')
2848                                         {
2849                                             llvm::StringRef symbol_name_ref(symbol_name);
2850                                             static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2851                                             static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2852                                             static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2853                                             if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2854                                             {
2855                                                 symbol_name_non_abi_mangled = symbol_name + 1;
2856                                                 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2857                                                 type = eSymbolTypeObjCClass;
2858                                                 demangled_is_synthesized = true;
2859                                             }
2860                                             else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2861                                             {
2862                                                 symbol_name_non_abi_mangled = symbol_name + 1;
2863                                                 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2864                                                 type = eSymbolTypeObjCMetaClass;
2865                                                 demangled_is_synthesized = true;
2866                                             }
2867                                             else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2868                                             {
2869                                                 symbol_name_non_abi_mangled = symbol_name + 1;
2870                                                 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2871                                                 type = eSymbolTypeObjCIVar;
2872                                                 demangled_is_synthesized = true;
2873                                             }
2874                                         }
2875                                     }
2876                                     else
2877                                     if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2878                                     {
2879                                         type = eSymbolTypeException;
2880                                     }
2881                                     else
2882                                     {
2883                                         type = eSymbolTypeData;
2884                                     }
2885                                 }
2886                                 else
2887                                 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2888                                 {
2889                                     type = eSymbolTypeTrampoline;
2890                                 }
2891                                 else
2892                                 if (symbol_section->IsDescendant(objc_section_sp.get()))
2893                                 {
2894                                     type = eSymbolTypeRuntime;
2895                                     if (symbol_name && symbol_name[0] == '.')
2896                                     {
2897                                         llvm::StringRef symbol_name_ref(symbol_name);
2898                                         static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2899                                         if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
2900                                         {
2901                                             symbol_name_non_abi_mangled = symbol_name;
2902                                             symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2903                                             type = eSymbolTypeObjCClass;
2904                                             demangled_is_synthesized = true;
2905                                         }
2906                                     }
2907                                 }
2908                             }
2909                         }
2910                     }
2911                     break;
2912                 }
2913             }
2914 
2915             if (add_nlist)
2916             {
2917                 uint64_t symbol_value = nlist.n_value;
2918                 bool symbol_name_is_mangled = false;
2919 
2920                 if (symbol_name_non_abi_mangled)
2921                 {
2922                     sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2923                     sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
2924                 }
2925                 else
2926                 {
2927                     if (symbol_name && symbol_name[0] == '_')
2928                     {
2929                         symbol_name_is_mangled = symbol_name[1] == '_';
2930                         symbol_name++;  // Skip the leading underscore
2931                     }
2932 
2933                     if (symbol_name)
2934                     {
2935                         sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
2936                     }
2937                 }
2938 
2939                 if (is_debug == false)
2940                 {
2941                     if (type == eSymbolTypeCode)
2942                     {
2943                         // See if we can find a N_FUN entry for any code symbols.
2944                         // If we do find a match, and the name matches, then we
2945                         // can merge the two into just the function symbol to avoid
2946                         // duplicate entries in the symbol table
2947                         ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2948                         if (pos != N_FUN_addr_to_sym_idx.end())
2949                         {
2950                             if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2951                                 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2952                             {
2953                                 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2954                                 // We just need the flags from the linker symbol, so put these flags
2955                                 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2956                                 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2957                                 sym[sym_idx].Clear();
2958                                 continue;
2959                             }
2960                         }
2961                     }
2962                     else if (type == eSymbolTypeData)
2963                     {
2964                         // See if we can find a N_STSYM entry for any data symbols.
2965                         // If we do find a match, and the name matches, then we
2966                         // can merge the two into just the Static symbol to avoid
2967                         // duplicate entries in the symbol table
2968                         ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2969                         if (pos != N_STSYM_addr_to_sym_idx.end())
2970                         {
2971                             if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2972                                 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2973                             {
2974                                 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2975                                 // We just need the flags from the linker symbol, so put these flags
2976                                 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2977                                 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2978                                 sym[sym_idx].Clear();
2979                                 continue;
2980                             }
2981                         }
2982                     }
2983                 }
2984                 if (symbol_section)
2985                 {
2986                     const addr_t section_file_addr = symbol_section->GetFileAddress();
2987                     if (symbol_byte_size == 0 && function_starts_count > 0)
2988                     {
2989                         addr_t symbol_lookup_file_addr = nlist.n_value;
2990                         // Do an exact address match for non-ARM addresses, else get the closest since
2991                         // the symbol might be a thumb symbol which has an address with bit zero set
2992                         FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2993                         if (is_arm && func_start_entry)
2994                         {
2995                             // Verify that the function start address is the symbol address (ARM)
2996                             // or the symbol address + 1 (thumb)
2997                             if (func_start_entry->addr != symbol_lookup_file_addr &&
2998                                 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2999                             {
3000                                 // Not the right entry, NULL it out...
3001                                 func_start_entry = NULL;
3002                             }
3003                         }
3004                         if (func_start_entry)
3005                         {
3006                             func_start_entry->data = true;
3007 
3008                             addr_t symbol_file_addr = func_start_entry->addr;
3009                             if (is_arm)
3010                                 symbol_file_addr &= 0xfffffffffffffffeull;
3011 
3012                             const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3013                             const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3014                             if (next_func_start_entry)
3015                             {
3016                                 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3017                                 // Be sure the clear the Thumb address bit when we calculate the size
3018                                 // from the current and next address
3019                                 if (is_arm)
3020                                     next_symbol_file_addr &= 0xfffffffffffffffeull;
3021                                 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3022                             }
3023                             else
3024                             {
3025                                 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3026                             }
3027                         }
3028                     }
3029                     symbol_value -= section_file_addr;
3030                 }
3031 
3032                 sym[sym_idx].SetID (nlist_idx);
3033                 sym[sym_idx].SetType (type);
3034                 sym[sym_idx].GetAddress().SetSection (symbol_section);
3035                 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3036                 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3037 
3038                 if (symbol_byte_size > 0)
3039                     sym[sym_idx].SetByteSize(symbol_byte_size);
3040 
3041                 if (demangled_is_synthesized)
3042                     sym[sym_idx].SetDemangledNameIsSynthesized(true);
3043 
3044                 ++sym_idx;
3045             }
3046             else
3047             {
3048                 sym[sym_idx].Clear();
3049             }
3050 
3051         }
3052 
3053         // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3054         // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3055         // such entries by figuring out what the address for the global is by looking up this non-STAB
3056         // entry and copying the value into the debug symbol's value to save us the hassle in the
3057         // debug symbol parser.
3058 
3059         Symbol *global_symbol = NULL;
3060         for (nlist_idx = 0;
3061              nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3062              nlist_idx++)
3063         {
3064             if (global_symbol->GetAddress().GetFileAddress() == 0)
3065             {
3066                 std::vector<uint32_t> indexes;
3067                 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3068                 {
3069                     std::vector<uint32_t>::const_iterator pos;
3070                     std::vector<uint32_t>::const_iterator end = indexes.end();
3071                     for (pos = indexes.begin(); pos != end; ++pos)
3072                     {
3073                         symbol_ptr = symtab->SymbolAtIndex(*pos);
3074                         if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3075                         {
3076                             global_symbol->GetAddress() = symbol_ptr->GetAddress();
3077                             break;
3078                         }
3079                     }
3080                 }
3081             }
3082         }
3083 
3084         uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3085 
3086         if (function_starts_count > 0)
3087         {
3088             char synthetic_function_symbol[PATH_MAX];
3089             uint32_t num_synthetic_function_symbols = 0;
3090             for (i=0; i<function_starts_count; ++i)
3091             {
3092                 if (function_starts.GetEntryRef (i).data == false)
3093                     ++num_synthetic_function_symbols;
3094             }
3095 
3096             if (num_synthetic_function_symbols > 0)
3097             {
3098                 if (num_syms < sym_idx + num_synthetic_function_symbols)
3099                 {
3100                     num_syms = sym_idx + num_synthetic_function_symbols;
3101                     sym = symtab->Resize (num_syms);
3102                 }
3103                 uint32_t synthetic_function_symbol_idx = 0;
3104                 for (i=0; i<function_starts_count; ++i)
3105                 {
3106                     const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3107                     if (func_start_entry->data == false)
3108                     {
3109                         addr_t symbol_file_addr = func_start_entry->addr;
3110                         uint32_t symbol_flags = 0;
3111                         if (is_arm)
3112                         {
3113                             if (symbol_file_addr & 1)
3114                                 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3115                             symbol_file_addr &= 0xfffffffffffffffeull;
3116                         }
3117                         Address symbol_addr;
3118                         if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
3119                         {
3120                             SectionSP symbol_section (symbol_addr.GetSection());
3121                             uint32_t symbol_byte_size = 0;
3122                             if (symbol_section)
3123                             {
3124                                 const addr_t section_file_addr = symbol_section->GetFileAddress();
3125                                 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3126                                 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3127                                 if (next_func_start_entry)
3128                                 {
3129                                     addr_t next_symbol_file_addr = next_func_start_entry->addr;
3130                                     if (is_arm)
3131                                         next_symbol_file_addr &= 0xfffffffffffffffeull;
3132                                     symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3133                                 }
3134                                 else
3135                                 {
3136                                     symbol_byte_size = section_end_file_addr - symbol_file_addr;
3137                                 }
3138                                 snprintf (synthetic_function_symbol,
3139                                           sizeof(synthetic_function_symbol),
3140                                           "___lldb_unnamed_function%u$$%s",
3141                                           ++synthetic_function_symbol_idx,
3142                                           module_sp->GetFileSpec().GetFilename().GetCString());
3143                                 sym[sym_idx].SetID (synthetic_sym_id++);
3144                                 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
3145                                 sym[sym_idx].SetType (eSymbolTypeCode);
3146                                 sym[sym_idx].SetIsSynthetic (true);
3147                                 sym[sym_idx].GetAddress() = symbol_addr;
3148                                 if (symbol_flags)
3149                                     sym[sym_idx].SetFlags (symbol_flags);
3150                                 if (symbol_byte_size)
3151                                     sym[sym_idx].SetByteSize (symbol_byte_size);
3152                                 ++sym_idx;
3153                             }
3154                         }
3155                     }
3156                 }
3157             }
3158         }
3159 
3160         // Trim our symbols down to just what we ended up with after
3161         // removing any symbols.
3162         if (sym_idx < num_syms)
3163         {
3164             num_syms = sym_idx;
3165             sym = symtab->Resize (num_syms);
3166         }
3167 
3168         // Now synthesize indirect symbols
3169         if (m_dysymtab.nindirectsyms != 0)
3170         {
3171             DataExtractor indirect_symbol_index_data (m_data, m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4);
3172 
3173             if (indirect_symbol_index_data.GetByteSize())
3174             {
3175                 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3176 
3177                 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3178                 {
3179                     if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3180                     {
3181                         uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3182                         if (symbol_stub_byte_size == 0)
3183                             continue;
3184 
3185                         const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3186 
3187                         if (num_symbol_stubs == 0)
3188                             continue;
3189 
3190                         const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3191                         for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3192                         {
3193                             const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3194                             const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
3195                             lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
3196                             if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3197                             {
3198                                 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3199                                 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3200                                     continue;
3201 
3202                                 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3203                                 Symbol *stub_symbol = NULL;
3204                                 if (index_pos != end_index_pos)
3205                                 {
3206                                     // We have a remapping from the original nlist index to
3207                                     // a current symbol index, so just look this up by index
3208                                     stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3209                                 }
3210                                 else
3211                                 {
3212                                     // We need to lookup a symbol using the original nlist
3213                                     // symbol index since this index is coming from the
3214                                     // S_SYMBOL_STUBS
3215                                     stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3216                                 }
3217 
3218                                 if (stub_symbol)
3219                                 {
3220                                     Address so_addr(symbol_stub_addr, section_list);
3221 
3222                                     if (stub_symbol->GetType() == eSymbolTypeUndefined)
3223                                     {
3224                                         // Change the external symbol into a trampoline that makes sense
3225                                         // These symbols were N_UNDF N_EXT, and are useless to us, so we
3226                                         // can re-use them so we don't have to make up a synthetic symbol
3227                                         // for no good reason.
3228                                         stub_symbol->SetType (eSymbolTypeTrampoline);
3229                                         stub_symbol->SetExternal (false);
3230                                         stub_symbol->GetAddress() = so_addr;
3231                                         stub_symbol->SetByteSize (symbol_stub_byte_size);
3232                                     }
3233                                     else
3234                                     {
3235                                         // Make a synthetic symbol to describe the trampoline stub
3236                                         Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
3237                                         if (sym_idx >= num_syms)
3238                                         {
3239                                             sym = symtab->Resize (++num_syms);
3240                                             stub_symbol = NULL;  // this pointer no longer valid
3241                                         }
3242                                         sym[sym_idx].SetID (synthetic_sym_id++);
3243                                         sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
3244                                         sym[sym_idx].SetType (eSymbolTypeTrampoline);
3245                                         sym[sym_idx].SetIsSynthetic (true);
3246                                         sym[sym_idx].GetAddress() = so_addr;
3247                                         sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3248                                         ++sym_idx;
3249                                     }
3250                                 }
3251                                 else
3252                                 {
3253                                     if (log)
3254                                         log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3255                                 }
3256                             }
3257                         }
3258                     }
3259                 }
3260             }
3261         }
3262         return symtab->GetNumSymbols();
3263     }
3264     return 0;
3265 }
3266 
3267 
3268 void
3269 ObjectFileMachO::Dump (Stream *s)
3270 {
3271     ModuleSP module_sp(GetModule());
3272     if (module_sp)
3273     {
3274         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3275         s->Printf("%p: ", this);
3276         s->Indent();
3277         if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3278             s->PutCString("ObjectFileMachO64");
3279         else
3280             s->PutCString("ObjectFileMachO32");
3281 
3282         ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
3283 
3284         *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
3285 
3286         if (m_sections_ap.get())
3287             m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
3288 
3289         if (m_symtab_ap.get())
3290             m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3291     }
3292 }
3293 
3294 
3295 bool
3296 ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
3297 {
3298     ModuleSP module_sp(GetModule());
3299     if (module_sp)
3300     {
3301         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3302         struct uuid_command load_cmd;
3303         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
3304         uint32_t i;
3305         for (i=0; i<m_header.ncmds; ++i)
3306         {
3307             const lldb::offset_t cmd_offset = offset;
3308             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3309                 break;
3310 
3311             if (load_cmd.cmd == LoadCommandUUID)
3312             {
3313                 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
3314 
3315                 if (uuid_bytes)
3316                 {
3317                     // OpenCL on Mac OS X uses the same UUID for each of its object files.
3318                     // We pretend these object files have no UUID to prevent crashing.
3319 
3320                     const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3321                                                     0x3b, 0xa8,
3322                                                     0x4b, 0x16,
3323                                                     0xb6, 0xa4,
3324                                                     0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3325 
3326                     if (!memcmp(uuid_bytes, opencl_uuid, 16))
3327                         return false;
3328 
3329                     uuid->SetBytes (uuid_bytes);
3330                     return true;
3331                 }
3332                 return false;
3333             }
3334             offset = cmd_offset + load_cmd.cmdsize;
3335         }
3336     }
3337     return false;
3338 }
3339 
3340 
3341 uint32_t
3342 ObjectFileMachO::GetDependentModules (FileSpecList& files)
3343 {
3344     uint32_t count = 0;
3345     ModuleSP module_sp(GetModule());
3346     if (module_sp)
3347     {
3348         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3349         struct load_command load_cmd;
3350         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
3351         const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3352         uint32_t i;
3353         for (i=0; i<m_header.ncmds; ++i)
3354         {
3355             const uint32_t cmd_offset = offset;
3356             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3357                 break;
3358 
3359             switch (load_cmd.cmd)
3360             {
3361             case LoadCommandDylibLoad:
3362             case LoadCommandDylibLoadWeak:
3363             case LoadCommandDylibReexport:
3364             case LoadCommandDynamicLinkerLoad:
3365             case LoadCommandFixedVMShlibLoad:
3366             case LoadCommandDylibLoadUpward:
3367                 {
3368                     uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3369                     const char *path = m_data.PeekCStr(name_offset);
3370                     // Skip any path that starts with '@' since these are usually:
3371                     // @executable_path/.../file
3372                     // @rpath/.../file
3373                     if (path && path[0] != '@')
3374                     {
3375                         FileSpec file_spec(path, resolve_path);
3376                         if (files.AppendIfUnique(file_spec))
3377                             count++;
3378                     }
3379                 }
3380                 break;
3381 
3382             default:
3383                 break;
3384             }
3385             offset = cmd_offset + load_cmd.cmdsize;
3386         }
3387     }
3388     return count;
3389 }
3390 
3391 lldb_private::Address
3392 ObjectFileMachO::GetEntryPointAddress ()
3393 {
3394     // If the object file is not an executable it can't hold the entry point.  m_entry_point_address
3395     // is initialized to an invalid address, so we can just return that.
3396     // If m_entry_point_address is valid it means we've found it already, so return the cached value.
3397 
3398     if (!IsExecutable() || m_entry_point_address.IsValid())
3399         return m_entry_point_address;
3400 
3401     // Otherwise, look for the UnixThread or Thread command.  The data for the Thread command is given in
3402     // /usr/include/mach-o.h, but it is basically:
3403     //
3404     //  uint32_t flavor  - this is the flavor argument you would pass to thread_get_state
3405     //  uint32_t count   - this is the count of longs in the thread state data
3406     //  struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3407     //  <repeat this trio>
3408     //
3409     // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3410     // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3411     // out of data in this form & attach them to a given thread.  That should underlie the MacOS X User process plugin,
3412     // and we'll also need it for the MacOS X Core File process plugin.  When we have that we can also use it here.
3413     //
3414     // For now we hard-code the offsets and flavors we need:
3415     //
3416     //
3417 
3418     ModuleSP module_sp(GetModule());
3419     if (module_sp)
3420     {
3421         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3422         struct load_command load_cmd;
3423         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
3424         uint32_t i;
3425         lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3426         bool done = false;
3427 
3428         for (i=0; i<m_header.ncmds; ++i)
3429         {
3430             const lldb::offset_t cmd_offset = offset;
3431             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3432                 break;
3433 
3434             switch (load_cmd.cmd)
3435             {
3436             case LoadCommandUnixThread:
3437             case LoadCommandThread:
3438                 {
3439                     while (offset < cmd_offset + load_cmd.cmdsize)
3440                     {
3441                         uint32_t flavor = m_data.GetU32(&offset);
3442                         uint32_t count = m_data.GetU32(&offset);
3443                         if (count == 0)
3444                         {
3445                             // We've gotten off somehow, log and exit;
3446                             return m_entry_point_address;
3447                         }
3448 
3449                         switch (m_header.cputype)
3450                         {
3451                         case llvm::MachO::CPUTypeARM:
3452                            if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3453                            {
3454                                offset += 60;  // This is the offset of pc in the GPR thread state data structure.
3455                                start_address = m_data.GetU32(&offset);
3456                                done = true;
3457                             }
3458                         break;
3459                         case llvm::MachO::CPUTypeI386:
3460                            if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3461                            {
3462                                offset += 40;  // This is the offset of eip in the GPR thread state data structure.
3463                                start_address = m_data.GetU32(&offset);
3464                                done = true;
3465                             }
3466                         break;
3467                         case llvm::MachO::CPUTypeX86_64:
3468                            if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3469                            {
3470                                offset += 16 * 8;  // This is the offset of rip in the GPR thread state data structure.
3471                                start_address = m_data.GetU64(&offset);
3472                                done = true;
3473                             }
3474                         break;
3475                         default:
3476                             return m_entry_point_address;
3477                         }
3478                         // Haven't found the GPR flavor yet, skip over the data for this flavor:
3479                         if (done)
3480                             break;
3481                         offset += count * 4;
3482                     }
3483                 }
3484                 break;
3485             case LoadCommandMain:
3486                 {
3487                     ConstString text_segment_name ("__TEXT");
3488                     uint64_t entryoffset = m_data.GetU64(&offset);
3489                     SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3490                     if (text_segment_sp)
3491                     {
3492                         done = true;
3493                         start_address = text_segment_sp->GetFileAddress() + entryoffset;
3494                     }
3495                 }
3496 
3497             default:
3498                 break;
3499             }
3500             if (done)
3501                 break;
3502 
3503             // Go to the next load command:
3504             offset = cmd_offset + load_cmd.cmdsize;
3505         }
3506 
3507         if (start_address != LLDB_INVALID_ADDRESS)
3508         {
3509             // We got the start address from the load commands, so now resolve that address in the sections
3510             // of this ObjectFile:
3511             if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
3512             {
3513                 m_entry_point_address.Clear();
3514             }
3515         }
3516         else
3517         {
3518             // We couldn't read the UnixThread load command - maybe it wasn't there.  As a fallback look for the
3519             // "start" symbol in the main executable.
3520 
3521             ModuleSP module_sp (GetModule());
3522 
3523             if (module_sp)
3524             {
3525                 SymbolContextList contexts;
3526                 SymbolContext context;
3527                 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3528                 {
3529                     if (contexts.GetContextAtIndex(0, context))
3530                         m_entry_point_address = context.symbol->GetAddress();
3531                 }
3532             }
3533         }
3534     }
3535 
3536     return m_entry_point_address;
3537 
3538 }
3539 
3540 lldb_private::Address
3541 ObjectFileMachO::GetHeaderAddress ()
3542 {
3543     lldb_private::Address header_addr;
3544     SectionList *section_list = GetSectionList();
3545     if (section_list)
3546     {
3547         SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3548         if (text_segment_sp)
3549         {
3550             header_addr.SetSection (text_segment_sp);
3551             header_addr.SetOffset (0);
3552         }
3553     }
3554     return header_addr;
3555 }
3556 
3557 uint32_t
3558 ObjectFileMachO::GetNumThreadContexts ()
3559 {
3560     ModuleSP module_sp(GetModule());
3561     if (module_sp)
3562     {
3563         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3564         if (!m_thread_context_offsets_valid)
3565         {
3566             m_thread_context_offsets_valid = true;
3567             lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
3568             FileRangeArray::Entry file_range;
3569             thread_command thread_cmd;
3570             for (uint32_t i=0; i<m_header.ncmds; ++i)
3571             {
3572                 const uint32_t cmd_offset = offset;
3573                 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3574                     break;
3575 
3576                 if (thread_cmd.cmd == LoadCommandThread)
3577                 {
3578                     file_range.SetRangeBase (offset);
3579                     file_range.SetByteSize (thread_cmd.cmdsize - 8);
3580                     m_thread_context_offsets.Append (file_range);
3581                 }
3582                 offset = cmd_offset + thread_cmd.cmdsize;
3583             }
3584         }
3585     }
3586     return m_thread_context_offsets.GetSize();
3587 }
3588 
3589 lldb::RegisterContextSP
3590 ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3591 {
3592     lldb::RegisterContextSP reg_ctx_sp;
3593 
3594     ModuleSP module_sp(GetModule());
3595     if (module_sp)
3596     {
3597         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3598         if (!m_thread_context_offsets_valid)
3599             GetNumThreadContexts ();
3600 
3601         const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
3602         if (thread_context_file_range)
3603         {
3604 
3605             DataExtractor data (m_data,
3606                                 thread_context_file_range->GetRangeBase(),
3607                                 thread_context_file_range->GetByteSize());
3608 
3609             switch (m_header.cputype)
3610             {
3611                 case llvm::MachO::CPUTypeARM:
3612                     reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3613                     break;
3614 
3615                 case llvm::MachO::CPUTypeI386:
3616                     reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3617                     break;
3618 
3619                 case llvm::MachO::CPUTypeX86_64:
3620                     reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3621                     break;
3622             }
3623         }
3624     }
3625     return reg_ctx_sp;
3626 }
3627 
3628 
3629 ObjectFile::Type
3630 ObjectFileMachO::CalculateType()
3631 {
3632     switch (m_header.filetype)
3633     {
3634         case HeaderFileTypeObject:                                          // 0x1u MH_OBJECT
3635             if (GetAddressByteSize () == 4)
3636             {
3637                 // 32 bit kexts are just object files, but they do have a valid
3638                 // UUID load command.
3639                 UUID uuid;
3640                 if (GetUUID(&uuid))
3641                 {
3642                     // this checking for the UUID load command is not enough
3643                     // we could eventually look for the symbol named
3644                     // "OSKextGetCurrentIdentifier" as this is required of kexts
3645                     if (m_strata == eStrataInvalid)
3646                         m_strata = eStrataKernel;
3647                     return eTypeSharedLibrary;
3648                 }
3649             }
3650             return eTypeObjectFile;
3651 
3652         case HeaderFileTypeExecutable:          return eTypeExecutable;     // 0x2u MH_EXECUTE
3653         case HeaderFileTypeFixedVMShlib:        return eTypeSharedLibrary;  // 0x3u MH_FVMLIB
3654         case HeaderFileTypeCore:                return eTypeCoreFile;       // 0x4u MH_CORE
3655         case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary;  // 0x5u MH_PRELOAD
3656         case HeaderFileTypeDynamicShlib:        return eTypeSharedLibrary;  // 0x6u MH_DYLIB
3657         case HeaderFileTypeDynamicLinkEditor:   return eTypeDynamicLinker;  // 0x7u MH_DYLINKER
3658         case HeaderFileTypeBundle:              return eTypeSharedLibrary;  // 0x8u MH_BUNDLE
3659         case HeaderFileTypeDynamicShlibStub:    return eTypeStubLibrary;    // 0x9u MH_DYLIB_STUB
3660         case HeaderFileTypeDSYM:                return eTypeDebugInfo;      // 0xAu MH_DSYM
3661         case HeaderFileTypeKextBundle:          return eTypeSharedLibrary;  // 0xBu MH_KEXT_BUNDLE
3662         default:
3663             break;
3664     }
3665     return eTypeUnknown;
3666 }
3667 
3668 ObjectFile::Strata
3669 ObjectFileMachO::CalculateStrata()
3670 {
3671     switch (m_header.filetype)
3672     {
3673         case HeaderFileTypeObject:      // 0x1u MH_OBJECT
3674             {
3675                 // 32 bit kexts are just object files, but they do have a valid
3676                 // UUID load command.
3677                 UUID uuid;
3678                 if (GetUUID(&uuid))
3679                 {
3680                     // this checking for the UUID load command is not enough
3681                     // we could eventually look for the symbol named
3682                     // "OSKextGetCurrentIdentifier" as this is required of kexts
3683                     if (m_type == eTypeInvalid)
3684                         m_type = eTypeSharedLibrary;
3685 
3686                     return eStrataKernel;
3687                 }
3688             }
3689             return eStrataUnknown;
3690 
3691         case HeaderFileTypeExecutable:                                     // 0x2u MH_EXECUTE
3692             // Check for the MH_DYLDLINK bit in the flags
3693             if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
3694             {
3695                 return eStrataUser;
3696             }
3697             else
3698             {
3699                 SectionList *section_list = GetSectionList();
3700                 if (section_list)
3701                 {
3702                     static ConstString g_kld_section_name ("__KLD");
3703                     if (section_list->FindSectionByName(g_kld_section_name))
3704                         return eStrataKernel;
3705                 }
3706             }
3707             return eStrataRawImage;
3708 
3709         case HeaderFileTypeFixedVMShlib:        return eStrataUser;         // 0x3u MH_FVMLIB
3710         case HeaderFileTypeCore:                return eStrataUnknown;      // 0x4u MH_CORE
3711         case HeaderFileTypePreloadedExecutable: return eStrataRawImage;     // 0x5u MH_PRELOAD
3712         case HeaderFileTypeDynamicShlib:        return eStrataUser;         // 0x6u MH_DYLIB
3713         case HeaderFileTypeDynamicLinkEditor:   return eStrataUser;         // 0x7u MH_DYLINKER
3714         case HeaderFileTypeBundle:              return eStrataUser;         // 0x8u MH_BUNDLE
3715         case HeaderFileTypeDynamicShlibStub:    return eStrataUser;         // 0x9u MH_DYLIB_STUB
3716         case HeaderFileTypeDSYM:                return eStrataUnknown;      // 0xAu MH_DSYM
3717         case HeaderFileTypeKextBundle:          return eStrataKernel;       // 0xBu MH_KEXT_BUNDLE
3718         default:
3719             break;
3720     }
3721     return eStrataUnknown;
3722 }
3723 
3724 
3725 uint32_t
3726 ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3727 {
3728     ModuleSP module_sp(GetModule());
3729     if (module_sp)
3730     {
3731         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3732         struct dylib_command load_cmd;
3733         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
3734         uint32_t version_cmd = 0;
3735         uint64_t version = 0;
3736         uint32_t i;
3737         for (i=0; i<m_header.ncmds; ++i)
3738         {
3739             const lldb::offset_t cmd_offset = offset;
3740             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3741                 break;
3742 
3743             if (load_cmd.cmd == LoadCommandDylibIdent)
3744             {
3745                 if (version_cmd == 0)
3746                 {
3747                     version_cmd = load_cmd.cmd;
3748                     if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3749                         break;
3750                     version = load_cmd.dylib.current_version;
3751                 }
3752                 break; // Break for now unless there is another more complete version
3753                        // number load command in the future.
3754             }
3755             offset = cmd_offset + load_cmd.cmdsize;
3756         }
3757 
3758         if (version_cmd == LoadCommandDylibIdent)
3759         {
3760             if (versions != NULL && num_versions > 0)
3761             {
3762                 if (num_versions > 0)
3763                     versions[0] = (version & 0xFFFF0000ull) >> 16;
3764                 if (num_versions > 1)
3765                     versions[1] = (version & 0x0000FF00ull) >> 8;
3766                 if (num_versions > 2)
3767                     versions[2] = (version & 0x000000FFull);
3768                 // Fill in an remaining version numbers with invalid values
3769                 for (i=3; i<num_versions; ++i)
3770                     versions[i] = UINT32_MAX;
3771             }
3772             // The LC_ID_DYLIB load command has a version with 3 version numbers
3773             // in it, so always return 3
3774             return 3;
3775         }
3776     }
3777     return false;
3778 }
3779 
3780 bool
3781 ObjectFileMachO::GetArchitecture (ArchSpec &arch)
3782 {
3783     ModuleSP module_sp(GetModule());
3784     if (module_sp)
3785     {
3786         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3787         arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
3788 
3789         // Files with type MH_PRELOAD are currently used in cases where the image
3790         // debugs at the addresses in the file itself. Below we set the OS to
3791         // unknown to make sure we use the DynamicLoaderStatic()...
3792         if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3793         {
3794             arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3795         }
3796         return true;
3797     }
3798     return false;
3799 }
3800 
3801 
3802 //------------------------------------------------------------------
3803 // PluginInterface protocol
3804 //------------------------------------------------------------------
3805 const char *
3806 ObjectFileMachO::GetPluginName()
3807 {
3808     return "ObjectFileMachO";
3809 }
3810 
3811 const char *
3812 ObjectFileMachO::GetShortPluginName()
3813 {
3814     return GetPluginNameStatic();
3815 }
3816 
3817 uint32_t
3818 ObjectFileMachO::GetPluginVersion()
3819 {
3820     return 1;
3821 }
3822 
3823