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