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