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