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