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