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