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