ObjectFileMachO.cpp revision 29021d341d8985ff180a0f1cdb0ce7a8504a83f6
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/Core/ArchSpec.h"
16#include "lldb/Core/DataBuffer.h"
17#include "lldb/Core/FileSpecList.h"
18#include "lldb/Core/Module.h"
19#include "lldb/Core/PluginManager.h"
20#include "lldb/Core/Section.h"
21#include "lldb/Core/StreamFile.h"
22#include "lldb/Core/StreamString.h"
23#include "lldb/Core/Timer.h"
24#include "lldb/Core/UUID.h"
25#include "lldb/Host/Host.h"
26#include "lldb/Host/FileSpec.h"
27#include "lldb/Symbol/ClangNamespaceDecl.h"
28#include "lldb/Symbol/ObjectFile.h"
29#include "lldb/Target/Platform.h"
30#include "lldb/Target/Process.h"
31#include "lldb/Target/Target.h"
32#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
33#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
34#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
35
36
37using namespace lldb;
38using namespace lldb_private;
39using namespace llvm::MachO;
40
41class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
42{
43public:
44    RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
45        RegisterContextDarwin_x86_64 (thread, 0)
46    {
47        SetRegisterDataFrom_LC_THREAD (data);
48    }
49
50    virtual void
51    InvalidateAllRegisters ()
52    {
53        // Do nothing... registers are always valid...
54    }
55
56    void
57    SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
58    {
59        uint32_t offset = 0;
60        SetError (GPRRegSet, Read, -1);
61        SetError (FPURegSet, Read, -1);
62        SetError (EXCRegSet, Read, -1);
63        bool done = false;
64
65        while (!done)
66        {
67            int flavor = data.GetU32 (&offset);
68            if (flavor == 0)
69                done = true;
70            else
71            {
72                uint32_t i;
73                uint32_t count = data.GetU32 (&offset);
74                switch (flavor)
75                {
76                    case GPRRegSet:
77                        for (i=0; i<count; ++i)
78                            (&gpr.rax)[i] = data.GetU64(&offset);
79                        SetError (GPRRegSet, Read, 0);
80                        done = true;
81
82                        break;
83                    case FPURegSet:
84                        // TODO: fill in FPU regs....
85                        //SetError (FPURegSet, Read, -1);
86                        done = true;
87
88                        break;
89                    case EXCRegSet:
90                        exc.trapno = data.GetU32(&offset);
91                        exc.err = data.GetU32(&offset);
92                        exc.faultvaddr = data.GetU64(&offset);
93                        SetError (EXCRegSet, Read, 0);
94                        done = true;
95                        break;
96                    case 7:
97                    case 8:
98                    case 9:
99                        // fancy flavors that encapsulate of the the above
100                        // falvors...
101                        break;
102
103                    default:
104                        done = true;
105                        break;
106                }
107            }
108        }
109    }
110protected:
111    virtual int
112    DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
113    {
114        return 0;
115    }
116
117    virtual int
118    DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
119    {
120        return 0;
121    }
122
123    virtual int
124    DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
125    {
126        return 0;
127    }
128
129    virtual int
130    DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
131    {
132        return 0;
133    }
134
135    virtual int
136    DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
137    {
138        return 0;
139    }
140
141    virtual int
142    DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
143    {
144        return 0;
145    }
146};
147
148
149class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
150{
151public:
152    RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
153    RegisterContextDarwin_i386 (thread, 0)
154    {
155        SetRegisterDataFrom_LC_THREAD (data);
156    }
157
158    virtual void
159    InvalidateAllRegisters ()
160    {
161        // Do nothing... registers are always valid...
162    }
163
164    void
165    SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
166    {
167        uint32_t offset = 0;
168        SetError (GPRRegSet, Read, -1);
169        SetError (FPURegSet, Read, -1);
170        SetError (EXCRegSet, Read, -1);
171        bool done = false;
172
173        while (!done)
174        {
175            int flavor = data.GetU32 (&offset);
176            if (flavor == 0)
177                done = true;
178            else
179            {
180                uint32_t i;
181                uint32_t count = data.GetU32 (&offset);
182                switch (flavor)
183                {
184                    case GPRRegSet:
185                        for (i=0; i<count; ++i)
186                            (&gpr.eax)[i] = data.GetU32(&offset);
187                        SetError (GPRRegSet, Read, 0);
188                        done = true;
189
190                        break;
191                    case FPURegSet:
192                        // TODO: fill in FPU regs....
193                        //SetError (FPURegSet, Read, -1);
194                        done = true;
195
196                        break;
197                    case EXCRegSet:
198                        exc.trapno = data.GetU32(&offset);
199                        exc.err = data.GetU32(&offset);
200                        exc.faultvaddr = data.GetU32(&offset);
201                        SetError (EXCRegSet, Read, 0);
202                        done = true;
203                        break;
204                    case 7:
205                    case 8:
206                    case 9:
207                        // fancy flavors that encapsulate of the the above
208                        // falvors...
209                        break;
210
211                    default:
212                        done = true;
213                        break;
214                }
215            }
216        }
217    }
218protected:
219    virtual int
220    DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
221    {
222        return 0;
223    }
224
225    virtual int
226    DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
227    {
228        return 0;
229    }
230
231    virtual int
232    DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
233    {
234        return 0;
235    }
236
237    virtual int
238    DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
239    {
240        return 0;
241    }
242
243    virtual int
244    DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
245    {
246        return 0;
247    }
248
249    virtual int
250    DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
251    {
252        return 0;
253    }
254};
255
256class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
257{
258public:
259    RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
260    RegisterContextDarwin_arm (thread, 0)
261    {
262        SetRegisterDataFrom_LC_THREAD (data);
263    }
264
265    virtual void
266    InvalidateAllRegisters ()
267    {
268        // Do nothing... registers are always valid...
269    }
270
271    void
272    SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
273    {
274        uint32_t offset = 0;
275        SetError (GPRRegSet, Read, -1);
276        SetError (FPURegSet, Read, -1);
277        SetError (EXCRegSet, Read, -1);
278        int flavor = data.GetU32 (&offset);
279        uint32_t count = data.GetU32 (&offset);
280        switch (flavor)
281        {
282            case GPRRegSet:
283                for (uint32_t i=0; i<count; ++i)
284                    gpr.r[i] = data.GetU32(&offset);
285                SetError (GPRRegSet, Read, 0);
286                break;
287            case FPURegSet:
288                // TODO: fill in FPU regs....
289                //SetError (FPURegSet, Read, -1);
290                break;
291            case EXCRegSet:
292                exc.exception = data.GetU32(&offset);
293                exc.fsr = data.GetU32(&offset);
294                exc.far = data.GetU32(&offset);
295                SetError (EXCRegSet, Read, 0);
296                break;
297        }
298    }
299protected:
300    virtual int
301    DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
302    {
303        return 0;
304    }
305
306    virtual int
307    DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
308    {
309        return 0;
310    }
311
312    virtual int
313    DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
314    {
315        return 0;
316    }
317
318    virtual int
319    DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
320    {
321        return 0;
322    }
323
324    virtual int
325    DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
326    {
327        return 0;
328    }
329
330    virtual int
331    DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
332    {
333        return 0;
334    }
335};
336
337#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
338
339void
340ObjectFileMachO::Initialize()
341{
342    PluginManager::RegisterPlugin (GetPluginNameStatic(),
343                                   GetPluginDescriptionStatic(),
344                                   CreateInstance,
345                                   CreateMemoryInstance);
346}
347
348void
349ObjectFileMachO::Terminate()
350{
351    PluginManager::UnregisterPlugin (CreateInstance);
352}
353
354
355const char *
356ObjectFileMachO::GetPluginNameStatic()
357{
358    return "object-file.mach-o";
359}
360
361const char *
362ObjectFileMachO::GetPluginDescriptionStatic()
363{
364    return "Mach-o object file reader (32 and 64 bit)";
365}
366
367
368ObjectFile *
369ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length)
370{
371    if (ObjectFileMachO::MagicBytesMatch(data_sp, offset, length))
372    {
373        std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, file, offset, length));
374        if (objfile_ap.get() && objfile_ap->ParseHeader())
375            return objfile_ap.release();
376    }
377    return NULL;
378}
379
380ObjectFile *
381ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
382                                       DataBufferSP& data_sp,
383                                       const ProcessSP &process_sp,
384                                       lldb::addr_t header_addr)
385{
386    if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
387    {
388        std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
389        if (objfile_ap.get() && objfile_ap->ParseHeader())
390            return objfile_ap.release();
391    }
392    return NULL;
393}
394
395
396const ConstString &
397ObjectFileMachO::GetSegmentNameTEXT()
398{
399    static ConstString g_segment_name_TEXT ("__TEXT");
400    return g_segment_name_TEXT;
401}
402
403const ConstString &
404ObjectFileMachO::GetSegmentNameDATA()
405{
406    static ConstString g_segment_name_DATA ("__DATA");
407    return g_segment_name_DATA;
408}
409
410const ConstString &
411ObjectFileMachO::GetSegmentNameOBJC()
412{
413    static ConstString g_segment_name_OBJC ("__OBJC");
414    return g_segment_name_OBJC;
415}
416
417const ConstString &
418ObjectFileMachO::GetSegmentNameLINKEDIT()
419{
420    static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
421    return g_section_name_LINKEDIT;
422}
423
424const ConstString &
425ObjectFileMachO::GetSectionNameEHFrame()
426{
427    static ConstString g_section_name_eh_frame ("__eh_frame");
428    return g_section_name_eh_frame;
429}
430
431
432
433static uint32_t
434MachHeaderSizeFromMagic(uint32_t magic)
435{
436    switch (magic)
437    {
438    case HeaderMagic32:
439    case HeaderMagic32Swapped:
440        return sizeof(struct mach_header);
441
442    case HeaderMagic64:
443    case HeaderMagic64Swapped:
444        return sizeof(struct mach_header_64);
445        break;
446
447    default:
448        break;
449    }
450    return 0;
451}
452
453
454bool
455ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
456                                  lldb::addr_t data_offset,
457                                  lldb::addr_t data_length)
458{
459    DataExtractor data;
460    data.SetData (data_sp, data_offset, data_length);
461    uint32_t offset = 0;
462    uint32_t magic = data.GetU32(&offset);
463    return MachHeaderSizeFromMagic(magic) != 0;
464}
465
466
467ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length) :
468    ObjectFile(module_sp, file, offset, length, data_sp),
469    m_sections_ap(),
470    m_symtab_ap(),
471    m_mach_segments(),
472    m_mach_sections(),
473    m_entry_point_address(),
474    m_thread_context_offsets(),
475    m_thread_context_offsets_valid(false)
476{
477    ::memset (&m_header, 0, sizeof(m_header));
478    ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
479}
480
481ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
482                                  lldb::DataBufferSP& header_data_sp,
483                                  const lldb::ProcessSP &process_sp,
484                                  lldb::addr_t header_addr) :
485    ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
486    m_sections_ap(),
487    m_symtab_ap(),
488    m_mach_segments(),
489    m_mach_sections(),
490    m_entry_point_address(),
491    m_thread_context_offsets(),
492    m_thread_context_offsets_valid(false)
493{
494    ::memset (&m_header, 0, sizeof(m_header));
495    ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
496}
497
498ObjectFileMachO::~ObjectFileMachO()
499{
500}
501
502
503bool
504ObjectFileMachO::ParseHeader ()
505{
506    ModuleSP module_sp(GetModule());
507    if (module_sp)
508    {
509        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
510        bool can_parse = false;
511        uint32_t offset = 0;
512        m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
513        // Leave magic in the original byte order
514        m_header.magic = m_data.GetU32(&offset);
515        switch (m_header.magic)
516        {
517        case HeaderMagic32:
518            m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
519            m_data.SetAddressByteSize(4);
520            can_parse = true;
521            break;
522
523        case HeaderMagic64:
524            m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
525            m_data.SetAddressByteSize(8);
526            can_parse = true;
527            break;
528
529        case HeaderMagic32Swapped:
530            m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
531            m_data.SetAddressByteSize(4);
532            can_parse = true;
533            break;
534
535        case HeaderMagic64Swapped:
536            m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
537            m_data.SetAddressByteSize(8);
538            can_parse = true;
539            break;
540
541        default:
542            break;
543        }
544
545        if (can_parse)
546        {
547            m_data.GetU32(&offset, &m_header.cputype, 6);
548
549            ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
550
551            if (SetModulesArchitecture (mach_arch))
552            {
553                const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
554                if (m_data.GetByteSize() < header_and_lc_size)
555                {
556                    DataBufferSP data_sp;
557                    ProcessSP process_sp (m_process_wp.lock());
558                    if (process_sp)
559                    {
560                        data_sp = ReadMemory (process_sp, m_offset, header_and_lc_size);
561                    }
562                    else
563                    {
564                        // Read in all only the load command data from the file on disk
565                        data_sp = m_file.ReadFileContents(m_offset, header_and_lc_size);
566                        if (data_sp->GetByteSize() != header_and_lc_size)
567                            return false;
568                    }
569                    if (data_sp)
570                        m_data.SetData (data_sp);
571                }
572            }
573            return true;
574        }
575        else
576        {
577            memset(&m_header, 0, sizeof(struct mach_header));
578        }
579    }
580    return false;
581}
582
583
584ByteOrder
585ObjectFileMachO::GetByteOrder () const
586{
587    return m_data.GetByteOrder ();
588}
589
590bool
591ObjectFileMachO::IsExecutable() const
592{
593    return m_header.filetype == HeaderFileTypeExecutable;
594}
595
596size_t
597ObjectFileMachO::GetAddressByteSize () const
598{
599    return m_data.GetAddressByteSize ();
600}
601
602AddressClass
603ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
604{
605    Symtab *symtab = GetSymtab();
606    if (symtab)
607    {
608        Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
609        if (symbol)
610        {
611            if (symbol->ValueIsAddress())
612            {
613                SectionSP section_sp (symbol->GetAddress().GetSection());
614                if (section_sp)
615                {
616                    const SectionType section_type = section_sp->GetType();
617                    switch (section_type)
618                    {
619                    case eSectionTypeInvalid:               return eAddressClassUnknown;
620                    case eSectionTypeCode:
621                        if (m_header.cputype == llvm::MachO::CPUTypeARM)
622                        {
623                            // For ARM we have a bit in the n_desc field of the symbol
624                            // that tells us ARM/Thumb which is bit 0x0008.
625                            if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
626                                return eAddressClassCodeAlternateISA;
627                        }
628                        return eAddressClassCode;
629
630                    case eSectionTypeContainer:             return eAddressClassUnknown;
631                    case eSectionTypeData:
632                    case eSectionTypeDataCString:
633                    case eSectionTypeDataCStringPointers:
634                    case eSectionTypeDataSymbolAddress:
635                    case eSectionTypeData4:
636                    case eSectionTypeData8:
637                    case eSectionTypeData16:
638                    case eSectionTypeDataPointers:
639                    case eSectionTypeZeroFill:
640                    case eSectionTypeDataObjCMessageRefs:
641                    case eSectionTypeDataObjCCFStrings:
642                        return eAddressClassData;
643                    case eSectionTypeDebug:
644                    case eSectionTypeDWARFDebugAbbrev:
645                    case eSectionTypeDWARFDebugAranges:
646                    case eSectionTypeDWARFDebugFrame:
647                    case eSectionTypeDWARFDebugInfo:
648                    case eSectionTypeDWARFDebugLine:
649                    case eSectionTypeDWARFDebugLoc:
650                    case eSectionTypeDWARFDebugMacInfo:
651                    case eSectionTypeDWARFDebugPubNames:
652                    case eSectionTypeDWARFDebugPubTypes:
653                    case eSectionTypeDWARFDebugRanges:
654                    case eSectionTypeDWARFDebugStr:
655                    case eSectionTypeDWARFAppleNames:
656                    case eSectionTypeDWARFAppleTypes:
657                    case eSectionTypeDWARFAppleNamespaces:
658                    case eSectionTypeDWARFAppleObjC:
659                        return eAddressClassDebug;
660                    case eSectionTypeEHFrame:               return eAddressClassRuntime;
661                    case eSectionTypeOther:                 return eAddressClassUnknown;
662                    }
663                }
664            }
665
666            const SymbolType symbol_type = symbol->GetType();
667            switch (symbol_type)
668            {
669            case eSymbolTypeAny:            return eAddressClassUnknown;
670            case eSymbolTypeAbsolute:       return eAddressClassUnknown;
671
672            case eSymbolTypeCode:
673            case eSymbolTypeTrampoline:
674                if (m_header.cputype == llvm::MachO::CPUTypeARM)
675                {
676                    // For ARM we have a bit in the n_desc field of the symbol
677                    // that tells us ARM/Thumb which is bit 0x0008.
678                    if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
679                        return eAddressClassCodeAlternateISA;
680                }
681                return eAddressClassCode;
682
683            case eSymbolTypeData:           return eAddressClassData;
684            case eSymbolTypeRuntime:        return eAddressClassRuntime;
685            case eSymbolTypeException:      return eAddressClassRuntime;
686            case eSymbolTypeSourceFile:     return eAddressClassDebug;
687            case eSymbolTypeHeaderFile:     return eAddressClassDebug;
688            case eSymbolTypeObjectFile:     return eAddressClassDebug;
689            case eSymbolTypeCommonBlock:    return eAddressClassDebug;
690            case eSymbolTypeBlock:          return eAddressClassDebug;
691            case eSymbolTypeLocal:          return eAddressClassData;
692            case eSymbolTypeParam:          return eAddressClassData;
693            case eSymbolTypeVariable:       return eAddressClassData;
694            case eSymbolTypeVariableType:   return eAddressClassDebug;
695            case eSymbolTypeLineEntry:      return eAddressClassDebug;
696            case eSymbolTypeLineHeader:     return eAddressClassDebug;
697            case eSymbolTypeScopeBegin:     return eAddressClassDebug;
698            case eSymbolTypeScopeEnd:       return eAddressClassDebug;
699            case eSymbolTypeAdditional:     return eAddressClassUnknown;
700            case eSymbolTypeCompiler:       return eAddressClassDebug;
701            case eSymbolTypeInstrumentation:return eAddressClassDebug;
702            case eSymbolTypeUndefined:      return eAddressClassUnknown;
703            case eSymbolTypeObjCClass:      return eAddressClassRuntime;
704            case eSymbolTypeObjCMetaClass:  return eAddressClassRuntime;
705            case eSymbolTypeObjCIVar:       return eAddressClassRuntime;
706            }
707        }
708    }
709    return eAddressClassUnknown;
710}
711
712Symtab *
713ObjectFileMachO::GetSymtab()
714{
715    ModuleSP module_sp(GetModule());
716    if (module_sp)
717    {
718        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
719        if (m_symtab_ap.get() == NULL)
720        {
721            m_symtab_ap.reset(new Symtab(this));
722            Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
723            ParseSymtab (true);
724            m_symtab_ap->Finalize ();
725        }
726    }
727    return m_symtab_ap.get();
728}
729
730
731SectionList *
732ObjectFileMachO::GetSectionList()
733{
734    ModuleSP module_sp(GetModule());
735    if (module_sp)
736    {
737        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
738        if (m_sections_ap.get() == NULL)
739        {
740            m_sections_ap.reset(new SectionList());
741            ParseSections();
742        }
743    }
744    return m_sections_ap.get();
745}
746
747
748size_t
749ObjectFileMachO::ParseSections ()
750{
751    lldb::user_id_t segID = 0;
752    lldb::user_id_t sectID = 0;
753    struct segment_command_64 load_cmd;
754    uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
755    uint32_t i;
756    const bool is_core = GetType() == eTypeCoreFile;
757    //bool dump_sections = false;
758    ModuleSP module_sp (GetModule());
759    for (i=0; i<m_header.ncmds; ++i)
760    {
761        const uint32_t load_cmd_offset = offset;
762        if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
763            break;
764
765        if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
766        {
767            if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
768            {
769                load_cmd.vmaddr = m_data.GetAddress(&offset);
770                load_cmd.vmsize = m_data.GetAddress(&offset);
771                load_cmd.fileoff = m_data.GetAddress(&offset);
772                load_cmd.filesize = m_data.GetAddress(&offset);
773                if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
774                {
775
776                    const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
777
778                    // Keep a list of mach segments around in case we need to
779                    // get at data that isn't stored in the abstracted Sections.
780                    m_mach_segments.push_back (load_cmd);
781
782                    ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
783                    // Use a segment ID of the segment index shifted left by 8 so they
784                    // never conflict with any of the sections.
785                    SectionSP segment_sp;
786                    if (segment_name || is_core)
787                    {
788                        segment_sp.reset(new Section (module_sp,            // Module to which this section belongs
789                                                      ++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
790                                                      segment_name,           // Name of this section
791                                                      eSectionTypeContainer,  // This section is a container of other sections.
792                                                      load_cmd.vmaddr,        // File VM address == addresses as they are found in the object file
793                                                      load_cmd.vmsize,        // VM size in bytes of this section
794                                                      load_cmd.fileoff,       // Offset to the data for this section in the file
795                                                      load_cmd.filesize,      // Size in bytes of this section as found in the the file
796                                                      load_cmd.flags));       // Flags for this section
797
798                        segment_sp->SetIsEncrypted (segment_is_encrypted);
799                        m_sections_ap->AddSection(segment_sp);
800                    }
801
802                    struct section_64 sect64;
803                    ::memset (&sect64, 0, sizeof(sect64));
804                    // Push a section into our mach sections for the section at
805                    // index zero (NListSectionNoSection) if we don't have any
806                    // mach sections yet...
807                    if (m_mach_sections.empty())
808                        m_mach_sections.push_back(sect64);
809                    uint32_t segment_sect_idx;
810                    const lldb::user_id_t first_segment_sectID = sectID + 1;
811
812
813                    const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
814                    for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
815                    {
816                        if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
817                            break;
818                        if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
819                            break;
820                        sect64.addr = m_data.GetAddress(&offset);
821                        sect64.size = m_data.GetAddress(&offset);
822
823                        if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
824                            break;
825
826                        // Keep a list of mach sections around in case we need to
827                        // get at data that isn't stored in the abstracted Sections.
828                        m_mach_sections.push_back (sect64);
829
830                        ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
831                        if (!segment_name)
832                        {
833                            // We have a segment with no name so we need to conjure up
834                            // segments that correspond to the section's segname if there
835                            // isn't already such a section. If there is such a section,
836                            // we resize the section so that it spans all sections.
837                            // We also mark these sections as fake so address matches don't
838                            // hit if they land in the gaps between the child sections.
839                            segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
840                            segment_sp = m_sections_ap->FindSectionByName (segment_name);
841                            if (segment_sp.get())
842                            {
843                                Section *segment = segment_sp.get();
844                                // Grow the section size as needed.
845                                const lldb::addr_t sect64_min_addr = sect64.addr;
846                                const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
847                                const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
848                                const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
849                                const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
850                                if (sect64_min_addr >= curr_seg_min_addr)
851                                {
852                                    const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
853                                    // Only grow the section size if needed
854                                    if (new_seg_byte_size > curr_seg_byte_size)
855                                        segment->SetByteSize (new_seg_byte_size);
856                                }
857                                else
858                                {
859                                    // We need to change the base address of the segment and
860                                    // adjust the child section offsets for all existing children.
861                                    const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
862                                    segment->Slide(slide_amount, false);
863                                    segment->GetChildren().Slide (-slide_amount, false);
864                                    segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
865                                }
866
867                                // Grow the section size as needed.
868                                if (sect64.offset)
869                                {
870                                    const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
871                                    const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
872
873                                    const lldb::addr_t section_min_file_offset = sect64.offset;
874                                    const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
875                                    const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
876                                    const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
877                                    segment->SetFileOffset (new_file_offset);
878                                    segment->SetFileSize (new_file_size);
879                                }
880                            }
881                            else
882                            {
883                                // Create a fake section for the section's named segment
884                                segment_sp.reset(new Section (segment_sp,            // Parent section
885                                                              module_sp,           // Module to which this section belongs
886                                                              ++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
887                                                              segment_name,          // Name of this section
888                                                              eSectionTypeContainer, // This section is a container of other sections.
889                                                              sect64.addr,           // File VM address == addresses as they are found in the object file
890                                                              sect64.size,           // VM size in bytes of this section
891                                                              sect64.offset,         // Offset to the data for this section in the file
892                                                              sect64.offset ? sect64.size : 0,        // Size in bytes of this section as found in the the file
893                                                              load_cmd.flags));      // Flags for this section
894                                segment_sp->SetIsFake(true);
895                                m_sections_ap->AddSection(segment_sp);
896                                segment_sp->SetIsEncrypted (segment_is_encrypted);
897                            }
898                        }
899                        assert (segment_sp.get());
900
901                        uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
902                        static ConstString g_sect_name_objc_data ("__objc_data");
903                        static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
904                        static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
905                        static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
906                        static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
907                        static ConstString g_sect_name_objc_const ("__objc_const");
908                        static ConstString g_sect_name_objc_classlist ("__objc_classlist");
909                        static ConstString g_sect_name_cfstring ("__cfstring");
910
911                        static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
912                        static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
913                        static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
914                        static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
915                        static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
916                        static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
917                        static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
918                        static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
919                        static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
920                        static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
921                        static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
922                        static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
923                        static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
924                        static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
925                        static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
926                        static ConstString g_sect_name_eh_frame ("__eh_frame");
927                        static ConstString g_sect_name_DATA ("__DATA");
928                        static ConstString g_sect_name_TEXT ("__TEXT");
929
930                        SectionType sect_type = eSectionTypeOther;
931
932                        if (section_name == g_sect_name_dwarf_debug_abbrev)
933                            sect_type = eSectionTypeDWARFDebugAbbrev;
934                        else if (section_name == g_sect_name_dwarf_debug_aranges)
935                            sect_type = eSectionTypeDWARFDebugAranges;
936                        else if (section_name == g_sect_name_dwarf_debug_frame)
937                            sect_type = eSectionTypeDWARFDebugFrame;
938                        else if (section_name == g_sect_name_dwarf_debug_info)
939                            sect_type = eSectionTypeDWARFDebugInfo;
940                        else if (section_name == g_sect_name_dwarf_debug_line)
941                            sect_type = eSectionTypeDWARFDebugLine;
942                        else if (section_name == g_sect_name_dwarf_debug_loc)
943                            sect_type = eSectionTypeDWARFDebugLoc;
944                        else if (section_name == g_sect_name_dwarf_debug_macinfo)
945                            sect_type = eSectionTypeDWARFDebugMacInfo;
946                        else if (section_name == g_sect_name_dwarf_debug_pubnames)
947                            sect_type = eSectionTypeDWARFDebugPubNames;
948                        else if (section_name == g_sect_name_dwarf_debug_pubtypes)
949                            sect_type = eSectionTypeDWARFDebugPubTypes;
950                        else if (section_name == g_sect_name_dwarf_debug_ranges)
951                            sect_type = eSectionTypeDWARFDebugRanges;
952                        else if (section_name == g_sect_name_dwarf_debug_str)
953                            sect_type = eSectionTypeDWARFDebugStr;
954                        else if (section_name == g_sect_name_dwarf_apple_names)
955                            sect_type = eSectionTypeDWARFAppleNames;
956                        else if (section_name == g_sect_name_dwarf_apple_types)
957                            sect_type = eSectionTypeDWARFAppleTypes;
958                        else if (section_name == g_sect_name_dwarf_apple_namespaces)
959                            sect_type = eSectionTypeDWARFAppleNamespaces;
960                        else if (section_name == g_sect_name_dwarf_apple_objc)
961                            sect_type = eSectionTypeDWARFAppleObjC;
962                        else if (section_name == g_sect_name_objc_selrefs)
963                            sect_type = eSectionTypeDataCStringPointers;
964                        else if (section_name == g_sect_name_objc_msgrefs)
965                            sect_type = eSectionTypeDataObjCMessageRefs;
966                        else if (section_name == g_sect_name_eh_frame)
967                            sect_type = eSectionTypeEHFrame;
968                        else if (section_name == g_sect_name_cfstring)
969                            sect_type = eSectionTypeDataObjCCFStrings;
970                        else if (section_name == g_sect_name_objc_data ||
971                                 section_name == g_sect_name_objc_classrefs ||
972                                 section_name == g_sect_name_objc_superrefs ||
973                                 section_name == g_sect_name_objc_const ||
974                                 section_name == g_sect_name_objc_classlist)
975                        {
976                            sect_type = eSectionTypeDataPointers;
977                        }
978
979                        if (sect_type == eSectionTypeOther)
980                        {
981                            switch (mach_sect_type)
982                            {
983                            // TODO: categorize sections by other flags for regular sections
984                            case SectionTypeRegular:
985                                if (segment_sp->GetName() == g_sect_name_TEXT)
986                                    sect_type = eSectionTypeCode;
987                                else if (segment_sp->GetName() == g_sect_name_DATA)
988                                    sect_type = eSectionTypeData;
989                                else
990                                    sect_type = eSectionTypeOther;
991                                break;
992                            case SectionTypeZeroFill:                   sect_type = eSectionTypeZeroFill; break;
993                            case SectionTypeCStringLiterals:            sect_type = eSectionTypeDataCString;    break; // section with only literal C strings
994                            case SectionType4ByteLiterals:              sect_type = eSectionTypeData4;    break; // section with only 4 byte literals
995                            case SectionType8ByteLiterals:              sect_type = eSectionTypeData8;    break; // section with only 8 byte literals
996                            case SectionTypeLiteralPointers:            sect_type = eSectionTypeDataPointers;  break; // section with only pointers to literals
997                            case SectionTypeNonLazySymbolPointers:      sect_type = eSectionTypeDataPointers;  break; // section with only non-lazy symbol pointers
998                            case SectionTypeLazySymbolPointers:         sect_type = eSectionTypeDataPointers;  break; // section with only lazy symbol pointers
999                            case SectionTypeSymbolStubs:                sect_type = eSectionTypeCode;  break; // section with only symbol stubs, byte size of stub in the reserved2 field
1000                            case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers;    break; // section with only function pointers for initialization
1001                            case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1002                            case SectionTypeCoalesced:                  sect_type = eSectionTypeOther; break;
1003                            case SectionTypeZeroFillLarge:              sect_type = eSectionTypeZeroFill; break;
1004                            case SectionTypeInterposing:                sect_type = eSectionTypeCode;  break; // section with only pairs of function pointers for interposing
1005                            case SectionType16ByteLiterals:             sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1006                            case SectionTypeDTraceObjectFormat:         sect_type = eSectionTypeDebug; break;
1007                            case SectionTypeLazyDylibSymbolPointers:    sect_type = eSectionTypeDataPointers;  break;
1008                            default: break;
1009                            }
1010                        }
1011
1012                        SectionSP section_sp(new Section (segment_sp,
1013                                                          module_sp,
1014                                                          ++sectID,
1015                                                          section_name,
1016                                                          sect_type,
1017                                                          sect64.addr - segment_sp->GetFileAddress(),
1018                                                          sect64.size,
1019                                                          sect64.offset,
1020                                                          sect64.offset == 0 ? 0 : sect64.size,
1021                                                          sect64.flags));
1022                        // Set the section to be encrypted to match the segment
1023                        section_sp->SetIsEncrypted (segment_is_encrypted);
1024
1025                        segment_sp->GetChildren().AddSection(section_sp);
1026
1027                        if (segment_sp->IsFake())
1028                        {
1029                            segment_sp.reset();
1030                            segment_name.Clear();
1031                        }
1032                    }
1033                    if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
1034                    {
1035                        if (first_segment_sectID <= sectID)
1036                        {
1037                            lldb::user_id_t sect_uid;
1038                            for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1039                            {
1040                                SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1041                                SectionSP next_section_sp;
1042                                if (sect_uid + 1 <= sectID)
1043                                    next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1044
1045                                if (curr_section_sp.get())
1046                                {
1047                                    if (curr_section_sp->GetByteSize() == 0)
1048                                    {
1049                                        if (next_section_sp.get() != NULL)
1050                                            curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1051                                        else
1052                                            curr_section_sp->SetByteSize ( load_cmd.vmsize );
1053                                    }
1054                                }
1055                            }
1056                        }
1057                    }
1058                }
1059            }
1060        }
1061        else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
1062        {
1063            m_dysymtab.cmd = load_cmd.cmd;
1064            m_dysymtab.cmdsize = load_cmd.cmdsize;
1065            m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1066        }
1067
1068        offset = load_cmd_offset + load_cmd.cmdsize;
1069    }
1070//    if (dump_sections)
1071//    {
1072//        StreamFile s(stdout);
1073//        m_sections_ap->Dump(&s, true);
1074//    }
1075    return sectID;  // Return the number of sections we registered with the module
1076}
1077
1078class MachSymtabSectionInfo
1079{
1080public:
1081
1082    MachSymtabSectionInfo (SectionList *section_list) :
1083        m_section_list (section_list),
1084        m_section_infos()
1085    {
1086        // Get the number of sections down to a depth of 1 to include
1087        // all segments and their sections, but no other sections that
1088        // may be added for debug map or
1089        m_section_infos.resize(section_list->GetNumSections(1));
1090    }
1091
1092
1093    SectionSP
1094    GetSection (uint8_t n_sect, addr_t file_addr)
1095    {
1096        if (n_sect == 0)
1097            return SectionSP();
1098        if (n_sect < m_section_infos.size())
1099        {
1100            if (!m_section_infos[n_sect].section_sp)
1101            {
1102                SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1103                m_section_infos[n_sect].section_sp = section_sp;
1104                if (section_sp != NULL)
1105                {
1106                    m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1107                    m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
1108                }
1109                else
1110                {
1111                    Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
1112                }
1113            }
1114            if (m_section_infos[n_sect].vm_range.Contains(file_addr))
1115            {
1116                // Symbol is in section.
1117                return m_section_infos[n_sect].section_sp;
1118            }
1119            else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1120                     m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1121            {
1122                // Symbol is in section with zero size, but has the same start
1123                // address as the section. This can happen with linker symbols
1124                // (symbols that start with the letter 'l' or 'L'.
1125                return m_section_infos[n_sect].section_sp;
1126            }
1127        }
1128        return m_section_list->FindSectionContainingFileAddress(file_addr);
1129    }
1130
1131protected:
1132    struct SectionInfo
1133    {
1134        SectionInfo () :
1135            vm_range(),
1136            section_sp ()
1137        {
1138        }
1139
1140        VMRange vm_range;
1141        SectionSP section_sp;
1142    };
1143    SectionList *m_section_list;
1144    std::vector<SectionInfo> m_section_infos;
1145};
1146
1147
1148
1149size_t
1150ObjectFileMachO::ParseSymtab (bool minimize)
1151{
1152    Timer scoped_timer(__PRETTY_FUNCTION__,
1153                       "ObjectFileMachO::ParseSymtab () module = %s",
1154                       m_file.GetFilename().AsCString(""));
1155    ModuleSP module_sp (GetModule());
1156    if (!module_sp)
1157        return 0;
1158
1159    struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1160    struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1161    typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1162    FunctionStarts function_starts;
1163    uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1164    uint32_t i;
1165
1166    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
1167
1168    for (i=0; i<m_header.ncmds; ++i)
1169    {
1170        const uint32_t cmd_offset = offset;
1171        // Read in the load command and load command size
1172        struct load_command lc;
1173        if (m_data.GetU32(&offset, &lc, 2) == NULL)
1174            break;
1175        // Watch for the symbol table load command
1176        switch (lc.cmd)
1177        {
1178        case LoadCommandSymtab:
1179            symtab_load_command.cmd = lc.cmd;
1180            symtab_load_command.cmdsize = lc.cmdsize;
1181            // Read in the rest of the symtab load command
1182            if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1183                return 0;
1184            if (symtab_load_command.symoff == 0)
1185            {
1186                if (log)
1187                    module_sp->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
1188                return 0;
1189            }
1190
1191            if (symtab_load_command.stroff == 0)
1192            {
1193                if (log)
1194                    module_sp->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
1195                return 0;
1196            }
1197
1198            if (symtab_load_command.nsyms == 0)
1199            {
1200                if (log)
1201                    module_sp->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
1202                return 0;
1203            }
1204
1205            if (symtab_load_command.strsize == 0)
1206            {
1207                if (log)
1208                    module_sp->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
1209                return 0;
1210            }
1211            break;
1212
1213        case LoadCommandFunctionStarts:
1214            function_starts_load_command.cmd = lc.cmd;
1215            function_starts_load_command.cmdsize = lc.cmdsize;
1216            if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1217                bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1218            break;
1219
1220        default:
1221            break;
1222        }
1223        offset = cmd_offset + lc.cmdsize;
1224    }
1225
1226    if (symtab_load_command.cmd)
1227    {
1228        Symtab *symtab = m_symtab_ap.get();
1229        SectionList *section_list = GetSectionList();
1230        if (section_list == NULL)
1231            return 0;
1232
1233        ProcessSP process_sp (m_process_wp.lock());
1234
1235        const size_t addr_byte_size = m_data.GetAddressByteSize();
1236        bool bit_width_32 = addr_byte_size == 4;
1237        const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1238
1239        DataExtractor nlist_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1240        DataExtractor strtab_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1241        DataExtractor function_starts_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1242
1243        const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1244        const addr_t strtab_data_byte_size = symtab_load_command.strsize;
1245        if (process_sp)
1246        {
1247            Target &target = process_sp->GetTarget();
1248            SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1249            // Reading mach file from memory in a process or core file...
1250
1251            if (linkedit_section_sp)
1252            {
1253                const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1254                const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1255                const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
1256                const addr_t stroff_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
1257
1258                bool data_was_read = false;
1259
1260#if defined (__APPLE__) && defined (__arm__)
1261                if (m_header.flags & 0x80000000u)
1262                {
1263                    // This mach-o memory file is in the dyld shared cache. If this
1264                    // program is not remote and this is iOS, then this process will
1265                    // share the same shared cache as the process we are debugging and
1266                    // we can read the entire __LINKEDIT from the address space in this
1267                    // process. This is a needed optimization that is used for local iOS
1268                    // debugging only since all shared libraries in the shared cache do
1269                    // not have corresponding files that exist in the file system of the
1270                    // device. They have been combined into a single file. This means we
1271                    // always have to load these files from memory. All of the symbol and
1272                    // string tables from all of the __LINKEDIT sections from the shared
1273                    // libraries in the shared cache have been merged into a single large
1274                    // symbol and string table. Reading all of this symbol and string table
1275                    // data across can slow down debug launch times, so we optimize this by
1276                    // reading the memory for the __LINKEDIT section from this process.
1277                    PlatformSP platform_sp (target.GetPlatform());
1278                    if (platform_sp && platform_sp->IsHost())
1279                    {
1280                        data_was_read = true;
1281                        nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
1282                        strtab_data.SetData((void *)stroff_addr, strtab_data_byte_size, eByteOrderLittle);
1283                        if (function_starts_load_command.cmd)
1284                        {
1285                            const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1286                            function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1287                        }
1288                    }
1289                }
1290#endif
1291
1292                if (!data_was_read)
1293                {
1294                    DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1295                    if (nlist_data_sp)
1296                        nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
1297                    DataBufferSP strtab_data_sp (ReadMemory (process_sp, stroff_addr, strtab_data_byte_size));
1298                    if (strtab_data_sp)
1299                        strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
1300                    if (function_starts_load_command.cmd)
1301                    {
1302                        const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1303                        DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1304                        if (func_start_data_sp)
1305                            function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1306                    }
1307                }
1308            }
1309        }
1310        else
1311        {
1312            nlist_data.SetData (m_data,
1313                                symtab_load_command.symoff,
1314                                nlist_data_byte_size);
1315            strtab_data.SetData (m_data,
1316                                 symtab_load_command.stroff,
1317                                 strtab_data_byte_size);
1318            if (function_starts_load_command.cmd)
1319            {
1320                function_starts_data.SetData (m_data,
1321                                              function_starts_load_command.dataoff,
1322                                              function_starts_load_command.datasize);
1323            }
1324        }
1325
1326        if (nlist_data.GetByteSize() == 0)
1327        {
1328            if (log)
1329                module_sp->LogMessage(log.get(), "failed to read nlist data");
1330            return 0;
1331        }
1332
1333
1334        if (strtab_data.GetByteSize() == 0)
1335        {
1336            if (log)
1337                module_sp->LogMessage(log.get(), "failed to read strtab data");
1338            return 0;
1339        }
1340
1341        const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1342        const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1343        const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1344        const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1345        SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1346        SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1347        SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1348        SectionSP eh_frame_section_sp;
1349        if (text_section_sp.get())
1350            eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1351        else
1352            eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1353
1354        const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
1355        if (text_section_sp && function_starts_data.GetByteSize())
1356        {
1357            FunctionStarts::Entry function_start_entry;
1358            function_start_entry.data = false;
1359            uint32_t function_start_offset = 0;
1360            function_start_entry.addr = text_section_sp->GetFileAddress();
1361            uint64_t delta;
1362            while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1363            {
1364                // Now append the current entry
1365                function_start_entry.addr += delta;
1366                function_starts.Append(function_start_entry);
1367            }
1368        }
1369
1370        const uint32_t function_starts_count = function_starts.GetSize();
1371
1372        uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
1373
1374        uint32_t nlist_data_offset = 0;
1375
1376        uint32_t N_SO_index = UINT32_MAX;
1377
1378        MachSymtabSectionInfo section_info (section_list);
1379        std::vector<uint32_t> N_FUN_indexes;
1380        std::vector<uint32_t> N_NSYM_indexes;
1381        std::vector<uint32_t> N_INCL_indexes;
1382        std::vector<uint32_t> N_BRAC_indexes;
1383        std::vector<uint32_t> N_COMM_indexes;
1384        typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1385        typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1386        ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1387        ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1388        // Any symbols that get merged into another will get an entry
1389        // in this map so we know
1390        NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1391        uint32_t nlist_idx = 0;
1392        Symbol *symbol_ptr = NULL;
1393
1394        uint32_t sym_idx = 0;
1395        Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
1396        uint32_t num_syms = symtab->GetNumSymbols();
1397
1398        //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
1399        for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
1400        {
1401            struct nlist_64 nlist;
1402            if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1403                break;
1404
1405            nlist.n_strx  = nlist_data.GetU32_unchecked(&nlist_data_offset);
1406            nlist.n_type  = nlist_data.GetU8_unchecked (&nlist_data_offset);
1407            nlist.n_sect  = nlist_data.GetU8_unchecked (&nlist_data_offset);
1408            nlist.n_desc  = nlist_data.GetU16_unchecked (&nlist_data_offset);
1409            nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
1410
1411            SymbolType type = eSymbolTypeInvalid;
1412            const char *symbol_name = strtab_data.PeekCStr(nlist.n_strx);
1413            if (symbol_name == NULL)
1414            {
1415                // No symbol should be NULL, even the symbols with no
1416                // string values should have an offset zero which points
1417                // to an empty C-string
1418                Host::SystemLog (Host::eSystemLogError,
1419                                 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1420                                 nlist_idx,
1421                                 nlist.n_strx,
1422                                 module_sp->GetFileSpec().GetDirectory().GetCString(),
1423                                 module_sp->GetFileSpec().GetFilename().GetCString());
1424                continue;
1425            }
1426            const char *symbol_name_non_abi_mangled = NULL;
1427
1428            if (symbol_name[0] == '\0')
1429                symbol_name = NULL;
1430            SectionSP symbol_section;
1431            uint32_t symbol_byte_size = 0;
1432            bool add_nlist = true;
1433            bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
1434
1435            assert (sym_idx < num_syms);
1436
1437            sym[sym_idx].SetDebug (is_debug);
1438
1439            if (is_debug)
1440            {
1441                switch (nlist.n_type)
1442                {
1443                case StabGlobalSymbol:
1444                    // N_GSYM -- global symbol: name,,NO_SECT,type,0
1445                    // Sometimes the N_GSYM value contains the address.
1446
1447                    // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data.  They
1448                    // have the same address, but we want to ensure that we always find only the real symbol,
1449                    // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1450                    // symbol type.  This is a temporary hack to make sure the ObjectiveC symbols get treated
1451                    // correctly.  To do this right, we should coalesce all the GSYM & global symbols that have the
1452                    // same address.
1453
1454                    if (symbol_name && symbol_name[0] == '_' && symbol_name[1] ==  'O'
1455                        && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1456                            || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1457                            || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1458                        add_nlist = false;
1459                    else
1460                    {
1461                        sym[sym_idx].SetExternal(true);
1462                        if (nlist.n_value != 0)
1463                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1464                        type = eSymbolTypeData;
1465                    }
1466                    break;
1467
1468                case StabFunctionName:
1469                    // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1470                    type = eSymbolTypeCompiler;
1471                    break;
1472
1473                case StabFunction:
1474                    // N_FUN -- procedure: name,,n_sect,linenumber,address
1475                    if (symbol_name)
1476                    {
1477                        type = eSymbolTypeCode;
1478                        symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1479
1480                        N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1481                        // We use the current number of symbols in the symbol table in lieu of
1482                        // using nlist_idx in case we ever start trimming entries out
1483                        N_FUN_indexes.push_back(sym_idx);
1484                    }
1485                    else
1486                    {
1487                        type = eSymbolTypeCompiler;
1488
1489                        if ( !N_FUN_indexes.empty() )
1490                        {
1491                            // Copy the size of the function into the original STAB entry so we don't have
1492                            // to hunt for it later
1493                            symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1494                            N_FUN_indexes.pop_back();
1495                            // We don't really need the end function STAB as it contains the size which
1496                            // we already placed with the original symbol, so don't add it if we want a
1497                            // minimal symbol table
1498                            if (minimize)
1499                                add_nlist = false;
1500                        }
1501                    }
1502                    break;
1503
1504                case StabStaticSymbol:
1505                    // N_STSYM -- static symbol: name,,n_sect,type,address
1506                    N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1507                    symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1508                    type = eSymbolTypeData;
1509                    break;
1510
1511                case StabLocalCommon:
1512                    // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1513                    symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1514                    type = eSymbolTypeCommonBlock;
1515                    break;
1516
1517                case StabBeginSymbol:
1518                    // N_BNSYM
1519                    // We use the current number of symbols in the symbol table in lieu of
1520                    // using nlist_idx in case we ever start trimming entries out
1521                    if (minimize)
1522                    {
1523                        // Skip these if we want minimal symbol tables
1524                        add_nlist = false;
1525                    }
1526                    else
1527                    {
1528                        symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1529                        N_NSYM_indexes.push_back(sym_idx);
1530                        type = eSymbolTypeScopeBegin;
1531                    }
1532                    break;
1533
1534                case StabEndSymbol:
1535                    // N_ENSYM
1536                    // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1537                    // so that we can always skip the entire symbol if we need to navigate
1538                    // more quickly at the source level when parsing STABS
1539                    if (minimize)
1540                    {
1541                        // Skip these if we want minimal symbol tables
1542                        add_nlist = false;
1543                    }
1544                    else
1545                    {
1546                        if ( !N_NSYM_indexes.empty() )
1547                        {
1548                            symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1549                            symbol_ptr->SetByteSize(sym_idx + 1);
1550                            symbol_ptr->SetSizeIsSibling(true);
1551                            N_NSYM_indexes.pop_back();
1552                        }
1553                        type = eSymbolTypeScopeEnd;
1554                    }
1555                    break;
1556
1557
1558                case StabSourceFileOptions:
1559                    // N_OPT - emitted with gcc2_compiled and in gcc source
1560                    type = eSymbolTypeCompiler;
1561                    break;
1562
1563                case StabRegisterSymbol:
1564                    // N_RSYM - register sym: name,,NO_SECT,type,register
1565                    type = eSymbolTypeVariable;
1566                    break;
1567
1568                case StabSourceLine:
1569                    // N_SLINE - src line: 0,,n_sect,linenumber,address
1570                    symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1571                    type = eSymbolTypeLineEntry;
1572                    break;
1573
1574                case StabStructureType:
1575                    // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1576                    type = eSymbolTypeVariableType;
1577                    break;
1578
1579                case StabSourceFileName:
1580                    // N_SO - source file name
1581                    type = eSymbolTypeSourceFile;
1582                    if (symbol_name == NULL)
1583                    {
1584                        if (minimize)
1585                            add_nlist = false;
1586                        if (N_SO_index != UINT32_MAX)
1587                        {
1588                            // Set the size of the N_SO to the terminating index of this N_SO
1589                            // so that we can always skip the entire N_SO if we need to navigate
1590                            // more quickly at the source level when parsing STABS
1591                            symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1592                            symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1593                            symbol_ptr->SetSizeIsSibling(true);
1594                        }
1595                        N_NSYM_indexes.clear();
1596                        N_INCL_indexes.clear();
1597                        N_BRAC_indexes.clear();
1598                        N_COMM_indexes.clear();
1599                        N_FUN_indexes.clear();
1600                        N_SO_index = UINT32_MAX;
1601                    }
1602                    else
1603                    {
1604                        // We use the current number of symbols in the symbol table in lieu of
1605                        // using nlist_idx in case we ever start trimming entries out
1606                        if (symbol_name[0] == '/')
1607                            N_SO_index = sym_idx;
1608                        else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1609                        {
1610                            const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1611                            if (so_path && so_path[0])
1612                            {
1613                                std::string full_so_path (so_path);
1614                                if (*full_so_path.rbegin() != '/')
1615                                    full_so_path += '/';
1616                                full_so_path += symbol_name;
1617                                sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
1618                                add_nlist = false;
1619                                m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1620                            }
1621                        }
1622                    }
1623
1624                    break;
1625
1626                case StabObjectFileName:
1627                    // N_OSO - object file name: name,,0,0,st_mtime
1628                    type = eSymbolTypeObjectFile;
1629                    break;
1630
1631                case StabLocalSymbol:
1632                    // N_LSYM - local sym: name,,NO_SECT,type,offset
1633                    type = eSymbolTypeLocal;
1634                    break;
1635
1636                //----------------------------------------------------------------------
1637                // INCL scopes
1638                //----------------------------------------------------------------------
1639                case StabBeginIncludeFileName:
1640                    // N_BINCL - include file beginning: name,,NO_SECT,0,sum
1641                    // We use the current number of symbols in the symbol table in lieu of
1642                    // using nlist_idx in case we ever start trimming entries out
1643                    N_INCL_indexes.push_back(sym_idx);
1644                    type = eSymbolTypeScopeBegin;
1645                    break;
1646
1647                case StabEndIncludeFile:
1648                    // N_EINCL - include file end: name,,NO_SECT,0,0
1649                    // Set the size of the N_BINCL to the terminating index of this N_EINCL
1650                    // so that we can always skip the entire symbol if we need to navigate
1651                    // more quickly at the source level when parsing STABS
1652                    if ( !N_INCL_indexes.empty() )
1653                    {
1654                        symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1655                        symbol_ptr->SetByteSize(sym_idx + 1);
1656                        symbol_ptr->SetSizeIsSibling(true);
1657                        N_INCL_indexes.pop_back();
1658                    }
1659                    type = eSymbolTypeScopeEnd;
1660                    break;
1661
1662                case StabIncludeFileName:
1663                    // N_SOL - #included file name: name,,n_sect,0,address
1664                    type = eSymbolTypeHeaderFile;
1665
1666                    // We currently don't use the header files on darwin
1667                    if (minimize)
1668                        add_nlist = false;
1669                    break;
1670
1671                case StabCompilerParameters:
1672                    // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
1673                    type = eSymbolTypeCompiler;
1674                    break;
1675
1676                case StabCompilerVersion:
1677                    // N_VERSION - compiler version: name,,NO_SECT,0,0
1678                    type = eSymbolTypeCompiler;
1679                    break;
1680
1681                case StabCompilerOptLevel:
1682                    // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
1683                    type = eSymbolTypeCompiler;
1684                    break;
1685
1686                case StabParameter:
1687                    // N_PSYM - parameter: name,,NO_SECT,type,offset
1688                    type = eSymbolTypeVariable;
1689                    break;
1690
1691                case StabAlternateEntry:
1692                    // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
1693                    symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1694                    type = eSymbolTypeLineEntry;
1695                    break;
1696
1697                //----------------------------------------------------------------------
1698                // Left and Right Braces
1699                //----------------------------------------------------------------------
1700                case StabLeftBracket:
1701                    // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
1702                    // We use the current number of symbols in the symbol table in lieu of
1703                    // using nlist_idx in case we ever start trimming entries out
1704                    symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1705                    N_BRAC_indexes.push_back(sym_idx);
1706                    type = eSymbolTypeScopeBegin;
1707                    break;
1708
1709                case StabRightBracket:
1710                    // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
1711                    // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1712                    // so that we can always skip the entire symbol if we need to navigate
1713                    // more quickly at the source level when parsing STABS
1714                    symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1715                    if ( !N_BRAC_indexes.empty() )
1716                    {
1717                        symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1718                        symbol_ptr->SetByteSize(sym_idx + 1);
1719                        symbol_ptr->SetSizeIsSibling(true);
1720                        N_BRAC_indexes.pop_back();
1721                    }
1722                    type = eSymbolTypeScopeEnd;
1723                    break;
1724
1725                case StabDeletedIncludeFile:
1726                    // N_EXCL - deleted include file: name,,NO_SECT,0,sum
1727                    type = eSymbolTypeHeaderFile;
1728                    break;
1729
1730                //----------------------------------------------------------------------
1731                // COMM scopes
1732                //----------------------------------------------------------------------
1733                case StabBeginCommon:
1734                    // N_BCOMM - begin common: name,,NO_SECT,0,0
1735                    // We use the current number of symbols in the symbol table in lieu of
1736                    // using nlist_idx in case we ever start trimming entries out
1737                    type = eSymbolTypeScopeBegin;
1738                    N_COMM_indexes.push_back(sym_idx);
1739                    break;
1740
1741                case StabEndCommonLocal:
1742                    // N_ECOML - end common (local name): 0,,n_sect,0,address
1743                    symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1744                    // Fall through
1745
1746                case StabEndCommon:
1747                    // N_ECOMM - end common: name,,n_sect,0,0
1748                    // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1749                    // so that we can always skip the entire symbol if we need to navigate
1750                    // more quickly at the source level when parsing STABS
1751                    if ( !N_COMM_indexes.empty() )
1752                    {
1753                        symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1754                        symbol_ptr->SetByteSize(sym_idx + 1);
1755                        symbol_ptr->SetSizeIsSibling(true);
1756                        N_COMM_indexes.pop_back();
1757                    }
1758                    type = eSymbolTypeScopeEnd;
1759                    break;
1760
1761                case StabLength:
1762                    // N_LENG - second stab entry with length information
1763                    type = eSymbolTypeAdditional;
1764                    break;
1765
1766                default: break;
1767                }
1768            }
1769            else
1770            {
1771                //uint8_t n_pext    = NlistMaskPrivateExternal & nlist.n_type;
1772                uint8_t n_type  = NlistMaskType & nlist.n_type;
1773                sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
1774
1775                switch (n_type)
1776                {
1777                case NListTypeIndirect:         // N_INDR - Fall through
1778                case NListTypePreboundUndefined:// N_PBUD - Fall through
1779                case NListTypeUndefined:        // N_UNDF
1780                    type = eSymbolTypeUndefined;
1781                    break;
1782
1783                case NListTypeAbsolute:         // N_ABS
1784                    type = eSymbolTypeAbsolute;
1785                    break;
1786
1787                case NListTypeSection:          // N_SECT
1788                    {
1789                        symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1790
1791                        if (symbol_section == NULL)
1792                        {
1793                            // TODO: warn about this?
1794                            add_nlist = false;
1795                            break;
1796                        }
1797
1798                        if (TEXT_eh_frame_sectID == nlist.n_sect)
1799                        {
1800                            type = eSymbolTypeException;
1801                        }
1802                        else
1803                        {
1804                            uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
1805
1806                            switch (section_type)
1807                            {
1808                            case SectionTypeRegular:                     break; // regular section
1809                            //case SectionTypeZeroFill:                 type = eSymbolTypeData;    break; // zero fill on demand section
1810                            case SectionTypeCStringLiterals:            type = eSymbolTypeData;    break; // section with only literal C strings
1811                            case SectionType4ByteLiterals:              type = eSymbolTypeData;    break; // section with only 4 byte literals
1812                            case SectionType8ByteLiterals:              type = eSymbolTypeData;    break; // section with only 8 byte literals
1813                            case SectionTypeLiteralPointers:            type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1814                            case SectionTypeNonLazySymbolPointers:      type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1815                            case SectionTypeLazySymbolPointers:         type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1816                            case SectionTypeSymbolStubs:                type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1817                            case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for initialization
1818                            case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for termination
1819                            //case SectionTypeCoalesced:                type = eSymbolType;    break; // section contains symbols that are to be coalesced
1820                            //case SectionTypeZeroFillLarge:            type = eSymbolTypeData;    break; // zero fill on demand section (that can be larger than 4 gigabytes)
1821                            case SectionTypeInterposing:                type = eSymbolTypeTrampoline;  break; // section with only pairs of function pointers for interposing
1822                            case SectionType16ByteLiterals:             type = eSymbolTypeData;    break; // section with only 16 byte literals
1823                            case SectionTypeDTraceObjectFormat:         type = eSymbolTypeInstrumentation; break;
1824                            case SectionTypeLazyDylibSymbolPointers:    type = eSymbolTypeTrampoline; break;
1825                            default: break;
1826                            }
1827
1828                            if (type == eSymbolTypeInvalid)
1829                            {
1830                                const char *symbol_sect_name = symbol_section->GetName().AsCString();
1831                                if (symbol_section->IsDescendant (text_section_sp.get()))
1832                                {
1833                                    if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1834                                                                SectionAttrUserSelfModifyingCode |
1835                                                                SectionAttrSytemSomeInstructions))
1836                                        type = eSymbolTypeData;
1837                                    else
1838                                        type = eSymbolTypeCode;
1839                                }
1840                                else
1841                                if (symbol_section->IsDescendant(data_section_sp.get()))
1842                                {
1843                                    if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1844                                    {
1845                                        type = eSymbolTypeRuntime;
1846
1847                                        if (symbol_name &&
1848                                            symbol_name[0] == '_' &&
1849                                            symbol_name[1] == 'O' &&
1850                                            symbol_name[2] == 'B')
1851                                        {
1852                                            llvm::StringRef symbol_name_ref(symbol_name);
1853                                            static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
1854                                            static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
1855                                            static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
1856                                            if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
1857                                            {
1858                                                symbol_name_non_abi_mangled = symbol_name + 1;
1859                                                symbol_name = symbol_name + g_objc_v2_prefix_class.size();
1860                                                type = eSymbolTypeObjCClass;
1861                                            }
1862                                            else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
1863                                            {
1864                                                symbol_name_non_abi_mangled = symbol_name + 1;
1865                                                symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
1866                                                type = eSymbolTypeObjCMetaClass;
1867                                            }
1868                                            else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
1869                                            {
1870                                                symbol_name_non_abi_mangled = symbol_name + 1;
1871                                                symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
1872                                                type = eSymbolTypeObjCIVar;
1873                                            }
1874                                        }
1875                                    }
1876                                    else
1877                                    if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1878                                    {
1879                                        type = eSymbolTypeException;
1880                                    }
1881                                    else
1882                                    {
1883                                        type = eSymbolTypeData;
1884                                    }
1885                                }
1886                                else
1887                                if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1888                                {
1889                                    type = eSymbolTypeTrampoline;
1890                                }
1891                                else
1892                                if (symbol_section->IsDescendant(objc_section_sp.get()))
1893                                {
1894                                    type = eSymbolTypeRuntime;
1895                                    if (symbol_name && symbol_name[0] == '.')
1896                                    {
1897                                        llvm::StringRef symbol_name_ref(symbol_name);
1898                                        static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
1899                                        if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
1900                                        {
1901                                            symbol_name_non_abi_mangled = symbol_name;
1902                                            symbol_name = symbol_name + g_objc_v1_prefix_class.size();
1903                                            type = eSymbolTypeObjCClass;
1904                                        }
1905                                    }
1906                                }
1907                            }
1908                        }
1909                    }
1910                    break;
1911                }
1912            }
1913
1914            if (add_nlist)
1915            {
1916                uint64_t symbol_value = nlist.n_value;
1917                bool symbol_name_is_mangled = false;
1918
1919                if (symbol_name_non_abi_mangled)
1920                {
1921                    sym[sym_idx].GetMangled().SetMangledName (symbol_name_non_abi_mangled);
1922                    sym[sym_idx].GetMangled().SetDemangledName (symbol_name);
1923                }
1924                else
1925                {
1926                    if (symbol_name && symbol_name[0] == '_')
1927                    {
1928                        symbol_name_is_mangled = symbol_name[1] == '_';
1929                        symbol_name++;  // Skip the leading underscore
1930                    }
1931
1932                    if (symbol_name)
1933                    {
1934                        sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
1935                    }
1936                }
1937
1938                if (is_debug == false)
1939                {
1940                    if (type == eSymbolTypeCode)
1941                    {
1942                        // See if we can find a N_FUN entry for any code symbols.
1943                        // If we do find a match, and the name matches, then we
1944                        // can merge the two into just the function symbol to avoid
1945                        // duplicate entries in the symbol table
1946                        ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1947                        if (pos != N_FUN_addr_to_sym_idx.end())
1948                        {
1949                            if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1950                                (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1951                            {
1952                                m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
1953                                // We just need the flags from the linker symbol, so put these flags
1954                                // into the N_FUN flags to avoid duplicate symbols in the symbol table
1955                                sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1956                                sym[sym_idx].Clear();
1957                                continue;
1958                            }
1959                        }
1960                    }
1961                    else if (type == eSymbolTypeData)
1962                    {
1963                        // See if we can find a N_STSYM entry for any data symbols.
1964                        // If we do find a match, and the name matches, then we
1965                        // can merge the two into just the Static symbol to avoid
1966                        // duplicate entries in the symbol table
1967                        ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1968                        if (pos != N_STSYM_addr_to_sym_idx.end())
1969                        {
1970                            if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1971                                (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1972                            {
1973                                m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
1974                                // We just need the flags from the linker symbol, so put these flags
1975                                // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1976                                sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1977                                sym[sym_idx].Clear();
1978                                continue;
1979                            }
1980                        }
1981                    }
1982                }
1983                if (symbol_section)
1984                {
1985                    const addr_t section_file_addr = symbol_section->GetFileAddress();
1986                    if (symbol_byte_size == 0 && function_starts_count > 0)
1987                    {
1988                        addr_t symbol_lookup_file_addr = nlist.n_value;
1989                        // Do an exact address match for non-ARM addresses, else get the closest since
1990                        // the symbol might be a thumb symbol which has an address with bit zero set
1991                        FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
1992                        if (is_arm && func_start_entry)
1993                        {
1994                            // Verify that the function start address is the symbol address (ARM)
1995                            // or the symbol address + 1 (thumb)
1996                            if (func_start_entry->addr != symbol_lookup_file_addr &&
1997                                func_start_entry->addr != (symbol_lookup_file_addr + 1))
1998                            {
1999                                // Not the right entry, NULL it out...
2000                                func_start_entry = NULL;
2001                            }
2002                        }
2003                        if (func_start_entry)
2004                        {
2005                            func_start_entry->data = true;
2006
2007                            addr_t symbol_file_addr = func_start_entry->addr;
2008                            uint32_t symbol_flags = 0;
2009                            if (is_arm)
2010                            {
2011                                if (symbol_file_addr & 1)
2012                                    symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2013                                symbol_file_addr &= 0xfffffffffffffffeull;
2014                            }
2015
2016                            const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2017                            const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2018                            if (next_func_start_entry)
2019                            {
2020                                addr_t next_symbol_file_addr = next_func_start_entry->addr;
2021                                // Be sure the clear the Thumb address bit when we calculate the size
2022                                // from the current and next address
2023                                if (is_arm)
2024                                    next_symbol_file_addr &= 0xfffffffffffffffeull;
2025                                symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2026                            }
2027                            else
2028                            {
2029                                symbol_byte_size = section_end_file_addr - symbol_file_addr;
2030                            }
2031                        }
2032                    }
2033                    symbol_value -= section_file_addr;
2034                }
2035
2036                sym[sym_idx].SetID (nlist_idx);
2037                sym[sym_idx].SetType (type);
2038                sym[sym_idx].GetAddress().SetSection (symbol_section);
2039                sym[sym_idx].GetAddress().SetOffset (symbol_value);
2040                sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2041
2042                if (symbol_byte_size > 0)
2043                    sym[sym_idx].SetByteSize(symbol_byte_size);
2044
2045                ++sym_idx;
2046            }
2047            else
2048            {
2049                sym[sym_idx].Clear();
2050            }
2051
2052        }
2053
2054        // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
2055        // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
2056        // such entries by figuring out what the address for the global is by looking up this non-STAB
2057        // entry and copying the value into the debug symbol's value to save us the hassle in the
2058        // debug symbol parser.
2059
2060        Symbol *global_symbol = NULL;
2061        for (nlist_idx = 0;
2062             nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
2063             nlist_idx++)
2064        {
2065            if (global_symbol->GetAddress().GetFileAddress() == 0)
2066            {
2067                std::vector<uint32_t> indexes;
2068                if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
2069                {
2070                    std::vector<uint32_t>::const_iterator pos;
2071                    std::vector<uint32_t>::const_iterator end = indexes.end();
2072                    for (pos = indexes.begin(); pos != end; ++pos)
2073                    {
2074                        symbol_ptr = symtab->SymbolAtIndex(*pos);
2075                        if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
2076                        {
2077                            global_symbol->GetAddress() = symbol_ptr->GetAddress();
2078                            break;
2079                        }
2080                    }
2081                }
2082            }
2083        }
2084
2085        uint32_t synthetic_sym_id = symtab_load_command.nsyms;
2086
2087
2088        if (function_starts_count > 0)
2089        {
2090            char synthetic_function_symbol[PATH_MAX];
2091            uint32_t num_synthetic_function_symbols = 0;
2092            for (i=0; i<function_starts_count; ++i)
2093            {
2094                if (function_starts.GetEntryRef (i).data == false)
2095                    ++num_synthetic_function_symbols;
2096            }
2097
2098            if (num_synthetic_function_symbols > 0)
2099            {
2100                if (num_syms < sym_idx + num_synthetic_function_symbols)
2101                {
2102                    num_syms = sym_idx + num_synthetic_function_symbols;
2103                    sym = symtab->Resize (num_syms);
2104                }
2105                uint32_t synthetic_function_symbol_idx = 0;
2106                for (i=0; i<function_starts_count; ++i)
2107                {
2108                    const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
2109                    if (func_start_entry->data == false)
2110                    {
2111                        addr_t symbol_file_addr = func_start_entry->addr;
2112                        uint32_t symbol_flags = 0;
2113                        if (is_arm)
2114                        {
2115                            if (symbol_file_addr & 1)
2116                                symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2117                            symbol_file_addr &= 0xfffffffffffffffeull;
2118                        }
2119                        Address symbol_addr;
2120                        if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
2121                        {
2122                            SectionSP symbol_section (symbol_addr.GetSection());
2123                            uint32_t symbol_byte_size = 0;
2124                            if (symbol_section)
2125                            {
2126                                const addr_t section_file_addr = symbol_section->GetFileAddress();
2127                                const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2128                                const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2129                                if (next_func_start_entry)
2130                                {
2131                                    addr_t next_symbol_file_addr = next_func_start_entry->addr;
2132                                    if (is_arm)
2133                                        next_symbol_file_addr &= 0xfffffffffffffffeull;
2134                                    symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2135                                }
2136                                else
2137                                {
2138                                    symbol_byte_size = section_end_file_addr - symbol_file_addr;
2139                                }
2140                                snprintf (synthetic_function_symbol,
2141                                          sizeof(synthetic_function_symbol),
2142                                          "___lldb_unnamed_function%u$$%s",
2143                                          ++synthetic_function_symbol_idx,
2144                                          module_sp->GetFileSpec().GetFilename().GetCString());
2145                                sym[sym_idx].SetID (synthetic_sym_id++);
2146                                sym[sym_idx].GetMangled().SetDemangledName(synthetic_function_symbol);
2147                                sym[sym_idx].SetType (eSymbolTypeCode);
2148                                sym[sym_idx].SetIsSynthetic (true);
2149                                sym[sym_idx].GetAddress() = symbol_addr;
2150                                if (symbol_flags)
2151                                    sym[sym_idx].SetFlags (symbol_flags);
2152                                if (symbol_byte_size)
2153                                    sym[sym_idx].SetByteSize (symbol_byte_size);
2154                                ++sym_idx;
2155                            }
2156                        }
2157                    }
2158                }
2159            }
2160        }
2161
2162        // Trim our symbols down to just what we ended up with after
2163        // removing any symbols.
2164        if (sym_idx < num_syms)
2165        {
2166            num_syms = sym_idx;
2167            sym = symtab->Resize (num_syms);
2168        }
2169
2170        // Now synthesize indirect symbols
2171        if (m_dysymtab.nindirectsyms != 0)
2172        {
2173            DataExtractor indirect_symbol_index_data (m_data, m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4);
2174
2175            if (indirect_symbol_index_data.GetByteSize())
2176            {
2177                NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
2178
2179                for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
2180                {
2181                    if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
2182                    {
2183                        uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
2184                        if (symbol_stub_byte_size == 0)
2185                            continue;
2186
2187                        const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
2188
2189                        if (num_symbol_stubs == 0)
2190                            continue;
2191
2192                        const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
2193                        for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
2194                        {
2195                            const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
2196                            const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
2197                            uint32_t symbol_stub_offset = symbol_stub_index * 4;
2198                            if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
2199                            {
2200                                const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
2201                                if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
2202                                    continue;
2203
2204                                NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
2205                                Symbol *stub_symbol = NULL;
2206                                if (index_pos != end_index_pos)
2207                                {
2208                                    // We have a remapping from the original nlist index to
2209                                    // a current symbol index, so just look this up by index
2210                                    stub_symbol = symtab->SymbolAtIndex (index_pos->second);
2211                                }
2212                                else
2213                                {
2214                                    // We need to lookup a symbol using the original nlist
2215                                    // symbol index since this index is coming from the
2216                                    // S_SYMBOL_STUBS
2217                                    stub_symbol = symtab->FindSymbolByID (stub_sym_id);
2218                                }
2219
2220                                assert (stub_symbol);
2221                                if (stub_symbol)
2222                                {
2223                                    Address so_addr(symbol_stub_addr, section_list);
2224
2225                                    if (stub_symbol->GetType() == eSymbolTypeUndefined)
2226                                    {
2227                                        // Change the external symbol into a trampoline that makes sense
2228                                        // These symbols were N_UNDF N_EXT, and are useless to us, so we
2229                                        // can re-use them so we don't have to make up a synthetic symbol
2230                                        // for no good reason.
2231                                        stub_symbol->SetType (eSymbolTypeTrampoline);
2232                                        stub_symbol->SetExternal (false);
2233                                        stub_symbol->GetAddress() = so_addr;
2234                                        stub_symbol->SetByteSize (symbol_stub_byte_size);
2235                                    }
2236                                    else
2237                                    {
2238                                        // Make a synthetic symbol to describe the trampoline stub
2239                                        if (sym_idx >= num_syms)
2240                                            sym = symtab->Resize (++num_syms);
2241                                        sym[sym_idx].SetID (synthetic_sym_id++);
2242                                        sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
2243                                        sym[sym_idx].SetType (eSymbolTypeTrampoline);
2244                                        sym[sym_idx].SetIsSynthetic (true);
2245                                        sym[sym_idx].GetAddress() = so_addr;
2246                                        sym[sym_idx].SetByteSize (symbol_stub_byte_size);
2247                                        ++sym_idx;
2248                                    }
2249                                }
2250                            }
2251                        }
2252                    }
2253                }
2254            }
2255        }
2256        return symtab->GetNumSymbols();
2257    }
2258    return 0;
2259}
2260
2261
2262void
2263ObjectFileMachO::Dump (Stream *s)
2264{
2265    ModuleSP module_sp(GetModule());
2266    if (module_sp)
2267    {
2268        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2269        s->Printf("%p: ", this);
2270        s->Indent();
2271        if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
2272            s->PutCString("ObjectFileMachO64");
2273        else
2274            s->PutCString("ObjectFileMachO32");
2275
2276        ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
2277
2278        *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
2279
2280        if (m_sections_ap.get())
2281            m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
2282
2283        if (m_symtab_ap.get())
2284            m_symtab_ap->Dump(s, NULL, eSortOrderNone);
2285    }
2286}
2287
2288
2289bool
2290ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
2291{
2292    ModuleSP module_sp(GetModule());
2293    if (module_sp)
2294    {
2295        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2296        struct uuid_command load_cmd;
2297        uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2298        uint32_t i;
2299        for (i=0; i<m_header.ncmds; ++i)
2300        {
2301            const uint32_t cmd_offset = offset;
2302            if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2303                break;
2304
2305            if (load_cmd.cmd == LoadCommandUUID)
2306            {
2307                const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
2308                if (uuid_bytes)
2309                {
2310                    uuid->SetBytes (uuid_bytes);
2311                    return true;
2312                }
2313                return false;
2314            }
2315            offset = cmd_offset + load_cmd.cmdsize;
2316        }
2317    }
2318    return false;
2319}
2320
2321
2322uint32_t
2323ObjectFileMachO::GetDependentModules (FileSpecList& files)
2324{
2325    uint32_t count = 0;
2326    ModuleSP module_sp(GetModule());
2327    if (module_sp)
2328    {
2329        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2330        struct load_command load_cmd;
2331        uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2332        const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
2333        uint32_t i;
2334        for (i=0; i<m_header.ncmds; ++i)
2335        {
2336            const uint32_t cmd_offset = offset;
2337            if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2338                break;
2339
2340            switch (load_cmd.cmd)
2341            {
2342            case LoadCommandDylibLoad:
2343            case LoadCommandDylibLoadWeak:
2344            case LoadCommandDylibReexport:
2345            case LoadCommandDynamicLinkerLoad:
2346            case LoadCommandFixedVMShlibLoad:
2347            case LoadCommandDylibLoadUpward:
2348                {
2349                    uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
2350                    const char *path = m_data.PeekCStr(name_offset);
2351                    // Skip any path that starts with '@' since these are usually:
2352                    // @executable_path/.../file
2353                    // @rpath/.../file
2354                    if (path && path[0] != '@')
2355                    {
2356                        FileSpec file_spec(path, resolve_path);
2357                        if (files.AppendIfUnique(file_spec))
2358                            count++;
2359                    }
2360                }
2361                break;
2362
2363            default:
2364                break;
2365            }
2366            offset = cmd_offset + load_cmd.cmdsize;
2367        }
2368    }
2369    return count;
2370}
2371
2372lldb_private::Address
2373ObjectFileMachO::GetEntryPointAddress ()
2374{
2375    // If the object file is not an executable it can't hold the entry point.  m_entry_point_address
2376    // is initialized to an invalid address, so we can just return that.
2377    // If m_entry_point_address is valid it means we've found it already, so return the cached value.
2378
2379    if (!IsExecutable() || m_entry_point_address.IsValid())
2380        return m_entry_point_address;
2381
2382    // Otherwise, look for the UnixThread or Thread command.  The data for the Thread command is given in
2383    // /usr/include/mach-o.h, but it is basically:
2384    //
2385    //  uint32_t flavor  - this is the flavor argument you would pass to thread_get_state
2386    //  uint32_t count   - this is the count of longs in the thread state data
2387    //  struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
2388    //  <repeat this trio>
2389    //
2390    // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
2391    // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
2392    // out of data in this form & attach them to a given thread.  That should underlie the MacOS X User process plugin,
2393    // and we'll also need it for the MacOS X Core File process plugin.  When we have that we can also use it here.
2394    //
2395    // For now we hard-code the offsets and flavors we need:
2396    //
2397    //
2398
2399    ModuleSP module_sp(GetModule());
2400    if (module_sp)
2401    {
2402        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2403        struct load_command load_cmd;
2404        uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2405        uint32_t i;
2406        lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
2407        bool done = false;
2408
2409        for (i=0; i<m_header.ncmds; ++i)
2410        {
2411            const uint32_t cmd_offset = offset;
2412            if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2413                break;
2414
2415            switch (load_cmd.cmd)
2416            {
2417            case LoadCommandUnixThread:
2418            case LoadCommandThread:
2419                {
2420                    while (offset < cmd_offset + load_cmd.cmdsize)
2421                    {
2422                        uint32_t flavor = m_data.GetU32(&offset);
2423                        uint32_t count = m_data.GetU32(&offset);
2424                        if (count == 0)
2425                        {
2426                            // We've gotten off somehow, log and exit;
2427                            return m_entry_point_address;
2428                        }
2429
2430                        switch (m_header.cputype)
2431                        {
2432                        case llvm::MachO::CPUTypeARM:
2433                           if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
2434                           {
2435                               offset += 60;  // This is the offset of pc in the GPR thread state data structure.
2436                               start_address = m_data.GetU32(&offset);
2437                               done = true;
2438                            }
2439                        break;
2440                        case llvm::MachO::CPUTypeI386:
2441                           if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
2442                           {
2443                               offset += 40;  // This is the offset of eip in the GPR thread state data structure.
2444                               start_address = m_data.GetU32(&offset);
2445                               done = true;
2446                            }
2447                        break;
2448                        case llvm::MachO::CPUTypeX86_64:
2449                           if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
2450                           {
2451                               offset += 16 * 8;  // This is the offset of rip in the GPR thread state data structure.
2452                               start_address = m_data.GetU64(&offset);
2453                               done = true;
2454                            }
2455                        break;
2456                        default:
2457                            return m_entry_point_address;
2458                        }
2459                        // Haven't found the GPR flavor yet, skip over the data for this flavor:
2460                        if (done)
2461                            break;
2462                        offset += count * 4;
2463                    }
2464                }
2465                break;
2466            case LoadCommandMain:
2467                {
2468                    ConstString text_segment_name ("__TEXT");
2469                    uint64_t entryoffset = m_data.GetU64(&offset);
2470                    SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2471                    if (text_segment_sp)
2472                    {
2473                        done = true;
2474                        start_address = text_segment_sp->GetFileAddress() + entryoffset;
2475                    }
2476                }
2477
2478            default:
2479                break;
2480            }
2481            if (done)
2482                break;
2483
2484            // Go to the next load command:
2485            offset = cmd_offset + load_cmd.cmdsize;
2486        }
2487
2488        if (start_address != LLDB_INVALID_ADDRESS)
2489        {
2490            // We got the start address from the load commands, so now resolve that address in the sections
2491            // of this ObjectFile:
2492            if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
2493            {
2494                m_entry_point_address.Clear();
2495            }
2496        }
2497        else
2498        {
2499            // We couldn't read the UnixThread load command - maybe it wasn't there.  As a fallback look for the
2500            // "start" symbol in the main executable.
2501
2502            ModuleSP module_sp (GetModule());
2503
2504            if (module_sp)
2505            {
2506                SymbolContextList contexts;
2507                SymbolContext context;
2508                if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
2509                {
2510                    if (contexts.GetContextAtIndex(0, context))
2511                        m_entry_point_address = context.symbol->GetAddress();
2512                }
2513            }
2514        }
2515    }
2516
2517    return m_entry_point_address;
2518
2519}
2520
2521lldb_private::Address
2522ObjectFileMachO::GetHeaderAddress ()
2523{
2524    lldb_private::Address header_addr;
2525    SectionList *section_list = GetSectionList();
2526    if (section_list)
2527    {
2528        SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
2529        if (text_segment_sp)
2530        {
2531            header_addr.SetSection (text_segment_sp);
2532            header_addr.SetOffset (0);
2533        }
2534    }
2535    return header_addr;
2536}
2537
2538uint32_t
2539ObjectFileMachO::GetNumThreadContexts ()
2540{
2541    ModuleSP module_sp(GetModule());
2542    if (module_sp)
2543    {
2544        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2545        if (!m_thread_context_offsets_valid)
2546        {
2547            m_thread_context_offsets_valid = true;
2548            uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2549            FileRangeArray::Entry file_range;
2550            thread_command thread_cmd;
2551            for (uint32_t i=0; i<m_header.ncmds; ++i)
2552            {
2553                const uint32_t cmd_offset = offset;
2554                if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
2555                    break;
2556
2557                if (thread_cmd.cmd == LoadCommandThread)
2558                {
2559                    file_range.SetRangeBase (offset);
2560                    file_range.SetByteSize (thread_cmd.cmdsize - 8);
2561                    m_thread_context_offsets.Append (file_range);
2562                }
2563                offset = cmd_offset + thread_cmd.cmdsize;
2564            }
2565        }
2566    }
2567    return m_thread_context_offsets.GetSize();
2568}
2569
2570lldb::RegisterContextSP
2571ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
2572{
2573    lldb::RegisterContextSP reg_ctx_sp;
2574
2575    ModuleSP module_sp(GetModule());
2576    if (module_sp)
2577    {
2578        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2579        if (!m_thread_context_offsets_valid)
2580            GetNumThreadContexts ();
2581
2582        const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
2583
2584        DataExtractor data (m_data,
2585                            thread_context_file_range->GetRangeBase(),
2586                            thread_context_file_range->GetByteSize());
2587
2588        switch (m_header.cputype)
2589        {
2590            case llvm::MachO::CPUTypeARM:
2591                reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
2592                break;
2593
2594            case llvm::MachO::CPUTypeI386:
2595                reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
2596                break;
2597
2598            case llvm::MachO::CPUTypeX86_64:
2599                reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
2600                break;
2601        }
2602    }
2603    return reg_ctx_sp;
2604}
2605
2606
2607ObjectFile::Type
2608ObjectFileMachO::CalculateType()
2609{
2610    switch (m_header.filetype)
2611    {
2612        case HeaderFileTypeObject:                                          // 0x1u MH_OBJECT
2613            if (GetAddressByteSize () == 4)
2614            {
2615                // 32 bit kexts are just object files, but they do have a valid
2616                // UUID load command.
2617                UUID uuid;
2618                if (GetUUID(&uuid))
2619                {
2620                    // this checking for the UUID load command is not enough
2621                    // we could eventually look for the symbol named
2622                    // "OSKextGetCurrentIdentifier" as this is required of kexts
2623                    if (m_strata == eStrataInvalid)
2624                        m_strata = eStrataKernel;
2625                    return eTypeSharedLibrary;
2626                }
2627            }
2628            return eTypeObjectFile;
2629
2630        case HeaderFileTypeExecutable:          return eTypeExecutable;     // 0x2u MH_EXECUTE
2631        case HeaderFileTypeFixedVMShlib:        return eTypeSharedLibrary;  // 0x3u MH_FVMLIB
2632        case HeaderFileTypeCore:                return eTypeCoreFile;       // 0x4u MH_CORE
2633        case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary;  // 0x5u MH_PRELOAD
2634        case HeaderFileTypeDynamicShlib:        return eTypeSharedLibrary;  // 0x6u MH_DYLIB
2635        case HeaderFileTypeDynamicLinkEditor:   return eTypeDynamicLinker;  // 0x7u MH_DYLINKER
2636        case HeaderFileTypeBundle:              return eTypeSharedLibrary;  // 0x8u MH_BUNDLE
2637        case HeaderFileTypeDynamicShlibStub:    return eTypeStubLibrary;    // 0x9u MH_DYLIB_STUB
2638        case HeaderFileTypeDSYM:                return eTypeDebugInfo;      // 0xAu MH_DSYM
2639        case HeaderFileTypeKextBundle:          return eTypeSharedLibrary;  // 0xBu MH_KEXT_BUNDLE
2640        default:
2641            break;
2642    }
2643    return eTypeUnknown;
2644}
2645
2646ObjectFile::Strata
2647ObjectFileMachO::CalculateStrata()
2648{
2649    switch (m_header.filetype)
2650    {
2651        case HeaderFileTypeObject:      // 0x1u MH_OBJECT
2652            {
2653                // 32 bit kexts are just object files, but they do have a valid
2654                // UUID load command.
2655                UUID uuid;
2656                if (GetUUID(&uuid))
2657                {
2658                    // this checking for the UUID load command is not enough
2659                    // we could eventually look for the symbol named
2660                    // "OSKextGetCurrentIdentifier" as this is required of kexts
2661                    if (m_type == eTypeInvalid)
2662                        m_type = eTypeSharedLibrary;
2663
2664                    return eStrataKernel;
2665                }
2666            }
2667            return eStrataUnknown;
2668
2669        case HeaderFileTypeExecutable:                                     // 0x2u MH_EXECUTE
2670            // Check for the MH_DYLDLINK bit in the flags
2671            if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
2672            {
2673                return eStrataUser;
2674            }
2675            else
2676            {
2677                SectionList *section_list = GetSectionList();
2678                if (section_list)
2679                {
2680                    static ConstString g_kld_section_name ("__KLD");
2681                    if (section_list->FindSectionByName(g_kld_section_name))
2682                        return eStrataKernel;
2683                }
2684            }
2685            return eStrataRawImage;
2686
2687        case HeaderFileTypeFixedVMShlib:        return eStrataUser;         // 0x3u MH_FVMLIB
2688        case HeaderFileTypeCore:                return eStrataUnknown;      // 0x4u MH_CORE
2689        case HeaderFileTypePreloadedExecutable: return eStrataRawImage;     // 0x5u MH_PRELOAD
2690        case HeaderFileTypeDynamicShlib:        return eStrataUser;         // 0x6u MH_DYLIB
2691        case HeaderFileTypeDynamicLinkEditor:   return eStrataUser;         // 0x7u MH_DYLINKER
2692        case HeaderFileTypeBundle:              return eStrataUser;         // 0x8u MH_BUNDLE
2693        case HeaderFileTypeDynamicShlibStub:    return eStrataUser;         // 0x9u MH_DYLIB_STUB
2694        case HeaderFileTypeDSYM:                return eStrataUnknown;      // 0xAu MH_DSYM
2695        case HeaderFileTypeKextBundle:          return eStrataKernel;       // 0xBu MH_KEXT_BUNDLE
2696        default:
2697            break;
2698    }
2699    return eStrataUnknown;
2700}
2701
2702
2703uint32_t
2704ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
2705{
2706    ModuleSP module_sp(GetModule());
2707    if (module_sp)
2708    {
2709        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2710        struct dylib_command load_cmd;
2711        uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2712        uint32_t version_cmd = 0;
2713        uint64_t version = 0;
2714        uint32_t i;
2715        for (i=0; i<m_header.ncmds; ++i)
2716        {
2717            const uint32_t cmd_offset = offset;
2718            if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2719                break;
2720
2721            if (load_cmd.cmd == LoadCommandDylibIdent)
2722            {
2723                if (version_cmd == 0)
2724                {
2725                    version_cmd = load_cmd.cmd;
2726                    if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
2727                        break;
2728                    version = load_cmd.dylib.current_version;
2729                }
2730                break; // Break for now unless there is another more complete version
2731                       // number load command in the future.
2732            }
2733            offset = cmd_offset + load_cmd.cmdsize;
2734        }
2735
2736        if (version_cmd == LoadCommandDylibIdent)
2737        {
2738            if (versions != NULL && num_versions > 0)
2739            {
2740                if (num_versions > 0)
2741                    versions[0] = (version & 0xFFFF0000ull) >> 16;
2742                if (num_versions > 1)
2743                    versions[1] = (version & 0x0000FF00ull) >> 8;
2744                if (num_versions > 2)
2745                    versions[2] = (version & 0x000000FFull);
2746                // Fill in an remaining version numbers with invalid values
2747                for (i=3; i<num_versions; ++i)
2748                    versions[i] = UINT32_MAX;
2749            }
2750            // The LC_ID_DYLIB load command has a version with 3 version numbers
2751            // in it, so always return 3
2752            return 3;
2753        }
2754    }
2755    return false;
2756}
2757
2758bool
2759ObjectFileMachO::GetArchitecture (ArchSpec &arch)
2760{
2761    ModuleSP module_sp(GetModule());
2762    if (module_sp)
2763    {
2764        lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2765        arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
2766
2767        // Files with type MH_PRELOAD are currently used in cases where the image
2768        // debugs at the addresses in the file itself. Below we set the OS to
2769        // unknown to make sure we use the DynamicLoaderStatic()...
2770        if (m_header.filetype == HeaderFileTypePreloadedExecutable)
2771        {
2772            arch.GetTriple().setOS (llvm::Triple::UnknownOS);
2773        }
2774        return true;
2775    }
2776    return false;
2777}
2778
2779
2780//------------------------------------------------------------------
2781// PluginInterface protocol
2782//------------------------------------------------------------------
2783const char *
2784ObjectFileMachO::GetPluginName()
2785{
2786    return "ObjectFileMachO";
2787}
2788
2789const char *
2790ObjectFileMachO::GetShortPluginName()
2791{
2792    return GetPluginNameStatic();
2793}
2794
2795uint32_t
2796ObjectFileMachO::GetPluginVersion()
2797{
2798    return 1;
2799}
2800
2801