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