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