ObjectFileMachO.cpp revision db2dc2b824b61ef7578dc8cdad7b338aa82b1f17
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
30
31using namespace lldb;
32using namespace lldb_private;
33using namespace llvm::MachO;
34
35#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
36
37void
38ObjectFileMachO::Initialize()
39{
40    PluginManager::RegisterPlugin (GetPluginNameStatic(),
41                                   GetPluginDescriptionStatic(),
42                                   CreateInstance);
43}
44
45void
46ObjectFileMachO::Terminate()
47{
48    PluginManager::UnregisterPlugin (CreateInstance);
49}
50
51
52const char *
53ObjectFileMachO::GetPluginNameStatic()
54{
55    return "object-file.mach-o";
56}
57
58const char *
59ObjectFileMachO::GetPluginDescriptionStatic()
60{
61    return "Mach-o object file reader (32 and 64 bit)";
62}
63
64
65ObjectFile *
66ObjectFileMachO::CreateInstance (Module* module, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length)
67{
68    if (ObjectFileMachO::MagicBytesMatch(data_sp, offset, length))
69    {
70        std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, data_sp, file, offset, length));
71        if (objfile_ap.get() && objfile_ap->ParseHeader())
72            return objfile_ap.release();
73    }
74    return NULL;
75}
76
77
78static uint32_t
79MachHeaderSizeFromMagic(uint32_t magic)
80{
81    switch (magic)
82    {
83    case HeaderMagic32:
84    case HeaderMagic32Swapped:
85        return sizeof(struct mach_header);
86
87    case HeaderMagic64:
88    case HeaderMagic64Swapped:
89        return sizeof(struct mach_header_64);
90        break;
91
92    default:
93        break;
94    }
95    return 0;
96}
97
98
99bool
100ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
101                                  lldb::addr_t data_offset,
102                                  lldb::addr_t data_length)
103{
104    DataExtractor data;
105    data.SetData (data_sp, data_offset, data_length);
106    uint32_t offset = 0;
107    uint32_t magic = data.GetU32(&offset);
108    return MachHeaderSizeFromMagic(magic) != 0;
109}
110
111
112ObjectFileMachO::ObjectFileMachO(Module* module, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length) :
113    ObjectFile(module, file, offset, length, data_sp),
114    m_mutex (Mutex::eMutexTypeRecursive),
115    m_header(),
116    m_sections_ap(),
117    m_symtab_ap(),
118    m_entry_point_address ()
119{
120    ::memset (&m_header, 0, sizeof(m_header));
121    ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
122}
123
124
125ObjectFileMachO::~ObjectFileMachO()
126{
127}
128
129
130bool
131ObjectFileMachO::ParseHeader ()
132{
133    lldb_private::Mutex::Locker locker(m_mutex);
134    bool can_parse = false;
135    uint32_t offset = 0;
136    m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
137    // Leave magic in the original byte order
138    m_header.magic = m_data.GetU32(&offset);
139    switch (m_header.magic)
140    {
141    case HeaderMagic32:
142        m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
143        m_data.SetAddressByteSize(4);
144        can_parse = true;
145        break;
146
147    case HeaderMagic64:
148        m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
149        m_data.SetAddressByteSize(8);
150        can_parse = true;
151        break;
152
153    case HeaderMagic32Swapped:
154        m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
155        m_data.SetAddressByteSize(4);
156        can_parse = true;
157        break;
158
159    case HeaderMagic64Swapped:
160        m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
161        m_data.SetAddressByteSize(8);
162        can_parse = true;
163        break;
164
165    default:
166        break;
167    }
168
169    if (can_parse)
170    {
171        m_data.GetU32(&offset, &m_header.cputype, 6);
172
173        ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
174
175        if (SetModulesArchitecture (mach_arch))
176            return true;
177    }
178    else
179    {
180        memset(&m_header, 0, sizeof(struct mach_header));
181    }
182    return false;
183}
184
185
186ByteOrder
187ObjectFileMachO::GetByteOrder () const
188{
189    lldb_private::Mutex::Locker locker(m_mutex);
190    return m_data.GetByteOrder ();
191}
192
193bool
194ObjectFileMachO::IsExecutable() const
195{
196    return m_header.filetype == HeaderFileTypeExecutable;
197}
198
199size_t
200ObjectFileMachO::GetAddressByteSize () const
201{
202    lldb_private::Mutex::Locker locker(m_mutex);
203    return m_data.GetAddressByteSize ();
204}
205
206AddressClass
207ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
208{
209    Symtab *symtab = GetSymtab();
210    if (symtab)
211    {
212        Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
213        if (symbol)
214        {
215            const AddressRange *range_ptr = symbol->GetAddressRangePtr();
216            if (range_ptr)
217            {
218                const Section *section = range_ptr->GetBaseAddress().GetSection();
219                if (section)
220                {
221                    const SectionType section_type = section->GetType();
222                    switch (section_type)
223                    {
224                    case eSectionTypeInvalid:               return eAddressClassUnknown;
225                    case eSectionTypeCode:
226                        if (m_header.cputype == llvm::MachO::CPUTypeARM)
227                        {
228                            // For ARM we have a bit in the n_desc field of the symbol
229                            // that tells us ARM/Thumb which is bit 0x0008.
230                            if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
231                                return eAddressClassCodeAlternateISA;
232                        }
233                        return eAddressClassCode;
234
235                    case eSectionTypeContainer:             return eAddressClassUnknown;
236                    case eSectionTypeData:
237                    case eSectionTypeDataCString:
238                    case eSectionTypeDataCStringPointers:
239                    case eSectionTypeDataSymbolAddress:
240                    case eSectionTypeData4:
241                    case eSectionTypeData8:
242                    case eSectionTypeData16:
243                    case eSectionTypeDataPointers:
244                    case eSectionTypeZeroFill:
245                    case eSectionTypeDataObjCMessageRefs:
246                    case eSectionTypeDataObjCCFStrings:
247                        return eAddressClassData;
248                    case eSectionTypeDebug:
249                    case eSectionTypeDWARFDebugAbbrev:
250                    case eSectionTypeDWARFDebugAranges:
251                    case eSectionTypeDWARFDebugFrame:
252                    case eSectionTypeDWARFDebugInfo:
253                    case eSectionTypeDWARFDebugLine:
254                    case eSectionTypeDWARFDebugLoc:
255                    case eSectionTypeDWARFDebugMacInfo:
256                    case eSectionTypeDWARFDebugPubNames:
257                    case eSectionTypeDWARFDebugPubTypes:
258                    case eSectionTypeDWARFDebugRanges:
259                    case eSectionTypeDWARFDebugStr:
260                    case eSectionTypeDWARFAppleNames:
261                    case eSectionTypeDWARFAppleTypes:
262                    case eSectionTypeDWARFAppleNamespaces:
263                    case eSectionTypeDWARFAppleObjC:
264                        return eAddressClassDebug;
265                    case eSectionTypeEHFrame:               return eAddressClassRuntime;
266                    case eSectionTypeOther:                 return eAddressClassUnknown;
267                    }
268                }
269            }
270
271            const SymbolType symbol_type = symbol->GetType();
272            switch (symbol_type)
273            {
274            case eSymbolTypeAny:            return eAddressClassUnknown;
275            case eSymbolTypeAbsolute:       return eAddressClassUnknown;
276
277            case eSymbolTypeCode:
278            case eSymbolTypeTrampoline:
279                if (m_header.cputype == llvm::MachO::CPUTypeARM)
280                {
281                    // For ARM we have a bit in the n_desc field of the symbol
282                    // that tells us ARM/Thumb which is bit 0x0008.
283                    if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
284                        return eAddressClassCodeAlternateISA;
285                }
286                return eAddressClassCode;
287
288            case eSymbolTypeData:           return eAddressClassData;
289            case eSymbolTypeRuntime:        return eAddressClassRuntime;
290            case eSymbolTypeException:      return eAddressClassRuntime;
291            case eSymbolTypeSourceFile:     return eAddressClassDebug;
292            case eSymbolTypeHeaderFile:     return eAddressClassDebug;
293            case eSymbolTypeObjectFile:     return eAddressClassDebug;
294            case eSymbolTypeCommonBlock:    return eAddressClassDebug;
295            case eSymbolTypeBlock:          return eAddressClassDebug;
296            case eSymbolTypeLocal:          return eAddressClassData;
297            case eSymbolTypeParam:          return eAddressClassData;
298            case eSymbolTypeVariable:       return eAddressClassData;
299            case eSymbolTypeVariableType:   return eAddressClassDebug;
300            case eSymbolTypeLineEntry:      return eAddressClassDebug;
301            case eSymbolTypeLineHeader:     return eAddressClassDebug;
302            case eSymbolTypeScopeBegin:     return eAddressClassDebug;
303            case eSymbolTypeScopeEnd:       return eAddressClassDebug;
304            case eSymbolTypeAdditional:     return eAddressClassUnknown;
305            case eSymbolTypeCompiler:       return eAddressClassDebug;
306            case eSymbolTypeInstrumentation:return eAddressClassDebug;
307            case eSymbolTypeUndefined:      return eAddressClassUnknown;
308            case eSymbolTypeObjCClass:      return eAddressClassRuntime;
309            case eSymbolTypeObjCMetaClass:  return eAddressClassRuntime;
310            case eSymbolTypeObjCIVar:       return eAddressClassRuntime;
311            }
312        }
313    }
314    return eAddressClassUnknown;
315}
316
317Symtab *
318ObjectFileMachO::GetSymtab()
319{
320    lldb_private::Mutex::Locker symfile_locker(m_mutex);
321    if (m_symtab_ap.get() == NULL)
322    {
323        m_symtab_ap.reset(new Symtab(this));
324        Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
325        ParseSymtab (true);
326        m_symtab_ap->Finalize ();
327    }
328    return m_symtab_ap.get();
329}
330
331
332SectionList *
333ObjectFileMachO::GetSectionList()
334{
335    lldb_private::Mutex::Locker locker(m_mutex);
336    if (m_sections_ap.get() == NULL)
337    {
338        m_sections_ap.reset(new SectionList());
339        ParseSections();
340    }
341    return m_sections_ap.get();
342}
343
344
345size_t
346ObjectFileMachO::ParseSections ()
347{
348    lldb::user_id_t segID = 0;
349    lldb::user_id_t sectID = 0;
350    struct segment_command_64 load_cmd;
351    uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
352    uint32_t i;
353    //bool dump_sections = false;
354    for (i=0; i<m_header.ncmds; ++i)
355    {
356        const uint32_t load_cmd_offset = offset;
357        if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
358            break;
359
360        if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
361        {
362            if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
363            {
364                load_cmd.vmaddr = m_data.GetAddress(&offset);
365                load_cmd.vmsize = m_data.GetAddress(&offset);
366                load_cmd.fileoff = m_data.GetAddress(&offset);
367                load_cmd.filesize = m_data.GetAddress(&offset);
368                if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
369                {
370
371                    const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
372
373                    // Keep a list of mach segments around in case we need to
374                    // get at data that isn't stored in the abstracted Sections.
375                    m_mach_segments.push_back (load_cmd);
376
377                    ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
378                    // Use a segment ID of the segment index shifted left by 8 so they
379                    // never conflict with any of the sections.
380                    SectionSP segment_sp;
381                    if (segment_name)
382                    {
383                        segment_sp.reset(new Section (NULL,
384                                                      GetModule(),            // Module to which this section belongs
385                                                      ++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
386                                                      segment_name,           // Name of this section
387                                                      eSectionTypeContainer,  // This section is a container of other sections.
388                                                      load_cmd.vmaddr,        // File VM address == addresses as they are found in the object file
389                                                      load_cmd.vmsize,        // VM size in bytes of this section
390                                                      load_cmd.fileoff,       // Offset to the data for this section in the file
391                                                      load_cmd.filesize,      // Size in bytes of this section as found in the the file
392                                                      load_cmd.flags));       // Flags for this section
393
394                        segment_sp->SetIsEncrypted (segment_is_encrypted);
395                        m_sections_ap->AddSection(segment_sp);
396                    }
397
398                    struct section_64 sect64;
399                    ::memset (&sect64, 0, sizeof(sect64));
400                    // Push a section into our mach sections for the section at
401                    // index zero (NListSectionNoSection) if we don't have any
402                    // mach sections yet...
403                    if (m_mach_sections.empty())
404                        m_mach_sections.push_back(sect64);
405                    uint32_t segment_sect_idx;
406                    const lldb::user_id_t first_segment_sectID = sectID + 1;
407
408
409                    const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
410                    for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
411                    {
412                        if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
413                            break;
414                        if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
415                            break;
416                        sect64.addr = m_data.GetAddress(&offset);
417                        sect64.size = m_data.GetAddress(&offset);
418
419                        if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
420                            break;
421
422                        // Keep a list of mach sections around in case we need to
423                        // get at data that isn't stored in the abstracted Sections.
424                        m_mach_sections.push_back (sect64);
425
426                        ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
427                        if (!segment_name)
428                        {
429                            // We have a segment with no name so we need to conjure up
430                            // segments that correspond to the section's segname if there
431                            // isn't already such a section. If there is such a section,
432                            // we resize the section so that it spans all sections.
433                            // We also mark these sections as fake so address matches don't
434                            // hit if they land in the gaps between the child sections.
435                            segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
436                            segment_sp = m_sections_ap->FindSectionByName (segment_name);
437                            if (segment_sp.get())
438                            {
439                                Section *segment = segment_sp.get();
440                                // Grow the section size as needed.
441                                const lldb::addr_t sect64_min_addr = sect64.addr;
442                                const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
443                                const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
444                                const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
445                                const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
446                                if (sect64_min_addr >= curr_seg_min_addr)
447                                {
448                                    const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
449                                    // Only grow the section size if needed
450                                    if (new_seg_byte_size > curr_seg_byte_size)
451                                        segment->SetByteSize (new_seg_byte_size);
452                                }
453                                else
454                                {
455                                    // We need to change the base address of the segment and
456                                    // adjust the child section offsets for all existing children.
457                                    const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
458                                    segment->Slide(slide_amount, false);
459                                    segment->GetChildren().Slide (-slide_amount, false);
460                                    segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
461                                }
462
463                                // Grow the section size as needed.
464                                if (sect64.offset)
465                                {
466                                    const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
467                                    const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
468
469                                    const lldb::addr_t section_min_file_offset = sect64.offset;
470                                    const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
471                                    const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
472                                    const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
473                                    segment->SetFileOffset (new_file_offset);
474                                    segment->SetFileSize (new_file_size);
475                                }
476                            }
477                            else
478                            {
479                                // Create a fake section for the section's named segment
480                                segment_sp.reset(new Section(segment_sp.get(),       // Parent section
481                                                             GetModule(),            // Module to which this section belongs
482                                                             ++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
483                                                             segment_name,           // Name of this section
484                                                             eSectionTypeContainer,  // This section is a container of other sections.
485                                                             sect64.addr,            // File VM address == addresses as they are found in the object file
486                                                             sect64.size,            // VM size in bytes of this section
487                                                             sect64.offset,          // Offset to the data for this section in the file
488                                                             sect64.offset ? sect64.size : 0,        // Size in bytes of this section as found in the the file
489                                                             load_cmd.flags));       // Flags for this section
490                                segment_sp->SetIsFake(true);
491                                m_sections_ap->AddSection(segment_sp);
492                                segment_sp->SetIsEncrypted (segment_is_encrypted);
493                            }
494                        }
495                        assert (segment_sp.get());
496
497                        uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
498                        static ConstString g_sect_name_objc_data ("__objc_data");
499                        static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
500                        static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
501                        static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
502                        static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
503                        static ConstString g_sect_name_objc_const ("__objc_const");
504                        static ConstString g_sect_name_objc_classlist ("__objc_classlist");
505                        static ConstString g_sect_name_cfstring ("__cfstring");
506
507                        static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
508                        static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
509                        static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
510                        static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
511                        static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
512                        static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
513                        static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
514                        static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
515                        static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
516                        static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
517                        static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
518                        static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
519                        static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
520                        static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
521                        static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
522                        static ConstString g_sect_name_eh_frame ("__eh_frame");
523                        static ConstString g_sect_name_DATA ("__DATA");
524                        static ConstString g_sect_name_TEXT ("__TEXT");
525
526                        SectionType sect_type = eSectionTypeOther;
527
528                        if (section_name == g_sect_name_dwarf_debug_abbrev)
529                            sect_type = eSectionTypeDWARFDebugAbbrev;
530                        else if (section_name == g_sect_name_dwarf_debug_aranges)
531                            sect_type = eSectionTypeDWARFDebugAranges;
532                        else if (section_name == g_sect_name_dwarf_debug_frame)
533                            sect_type = eSectionTypeDWARFDebugFrame;
534                        else if (section_name == g_sect_name_dwarf_debug_info)
535                            sect_type = eSectionTypeDWARFDebugInfo;
536                        else if (section_name == g_sect_name_dwarf_debug_line)
537                            sect_type = eSectionTypeDWARFDebugLine;
538                        else if (section_name == g_sect_name_dwarf_debug_loc)
539                            sect_type = eSectionTypeDWARFDebugLoc;
540                        else if (section_name == g_sect_name_dwarf_debug_macinfo)
541                            sect_type = eSectionTypeDWARFDebugMacInfo;
542                        else if (section_name == g_sect_name_dwarf_debug_pubnames)
543                            sect_type = eSectionTypeDWARFDebugPubNames;
544                        else if (section_name == g_sect_name_dwarf_debug_pubtypes)
545                            sect_type = eSectionTypeDWARFDebugPubTypes;
546                        else if (section_name == g_sect_name_dwarf_debug_ranges)
547                            sect_type = eSectionTypeDWARFDebugRanges;
548                        else if (section_name == g_sect_name_dwarf_debug_str)
549                            sect_type = eSectionTypeDWARFDebugStr;
550                        else if (section_name == g_sect_name_dwarf_apple_names)
551                            sect_type = eSectionTypeDWARFAppleNames;
552                        else if (section_name == g_sect_name_dwarf_apple_types)
553                            sect_type = eSectionTypeDWARFAppleTypes;
554                        else if (section_name == g_sect_name_dwarf_apple_namespaces)
555                            sect_type = eSectionTypeDWARFAppleNamespaces;
556                        else if (section_name == g_sect_name_dwarf_apple_objc)
557                            sect_type = eSectionTypeDWARFAppleObjC;
558                        else if (section_name == g_sect_name_objc_selrefs)
559                            sect_type = eSectionTypeDataCStringPointers;
560                        else if (section_name == g_sect_name_objc_msgrefs)
561                            sect_type = eSectionTypeDataObjCMessageRefs;
562                        else if (section_name == g_sect_name_eh_frame)
563                            sect_type = eSectionTypeEHFrame;
564                        else if (section_name == g_sect_name_cfstring)
565                            sect_type = eSectionTypeDataObjCCFStrings;
566                        else if (section_name == g_sect_name_objc_data ||
567                                 section_name == g_sect_name_objc_classrefs ||
568                                 section_name == g_sect_name_objc_superrefs ||
569                                 section_name == g_sect_name_objc_const ||
570                                 section_name == g_sect_name_objc_classlist)
571                        {
572                            sect_type = eSectionTypeDataPointers;
573                        }
574
575                        if (sect_type == eSectionTypeOther)
576                        {
577                            switch (mach_sect_type)
578                            {
579                            // TODO: categorize sections by other flags for regular sections
580                            case SectionTypeRegular:
581                                if (segment_sp->GetName() == g_sect_name_TEXT)
582                                    sect_type = eSectionTypeCode;
583                                else if (segment_sp->GetName() == g_sect_name_DATA)
584                                    sect_type = eSectionTypeData;
585                                else
586                                    sect_type = eSectionTypeOther;
587                                break;
588                            case SectionTypeZeroFill:                   sect_type = eSectionTypeZeroFill; break;
589                            case SectionTypeCStringLiterals:            sect_type = eSectionTypeDataCString;    break; // section with only literal C strings
590                            case SectionType4ByteLiterals:              sect_type = eSectionTypeData4;    break; // section with only 4 byte literals
591                            case SectionType8ByteLiterals:              sect_type = eSectionTypeData8;    break; // section with only 8 byte literals
592                            case SectionTypeLiteralPointers:            sect_type = eSectionTypeDataPointers;  break; // section with only pointers to literals
593                            case SectionTypeNonLazySymbolPointers:      sect_type = eSectionTypeDataPointers;  break; // section with only non-lazy symbol pointers
594                            case SectionTypeLazySymbolPointers:         sect_type = eSectionTypeDataPointers;  break; // section with only lazy symbol pointers
595                            case SectionTypeSymbolStubs:                sect_type = eSectionTypeCode;  break; // section with only symbol stubs, byte size of stub in the reserved2 field
596                            case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers;    break; // section with only function pointers for initialization
597                            case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
598                            case SectionTypeCoalesced:                  sect_type = eSectionTypeOther; break;
599                            case SectionTypeZeroFillLarge:              sect_type = eSectionTypeZeroFill; break;
600                            case SectionTypeInterposing:                sect_type = eSectionTypeCode;  break; // section with only pairs of function pointers for interposing
601                            case SectionType16ByteLiterals:             sect_type = eSectionTypeData16; break; // section with only 16 byte literals
602                            case SectionTypeDTraceObjectFormat:         sect_type = eSectionTypeDebug; break;
603                            case SectionTypeLazyDylibSymbolPointers:    sect_type = eSectionTypeDataPointers;  break;
604                            default: break;
605                            }
606                        }
607
608                        SectionSP section_sp(new Section(segment_sp.get(),
609                                                         GetModule(),
610                                                         ++sectID,
611                                                         section_name,
612                                                         sect_type,
613                                                         sect64.addr - segment_sp->GetFileAddress(),
614                                                         sect64.size,
615                                                         sect64.offset,
616                                                         sect64.offset == 0 ? 0 : sect64.size,
617                                                         sect64.flags));
618                        // Set the section to be encrypted to match the segment
619                        section_sp->SetIsEncrypted (segment_is_encrypted);
620
621                        segment_sp->GetChildren().AddSection(section_sp);
622
623                        if (segment_sp->IsFake())
624                        {
625                            segment_sp.reset();
626                            segment_name.Clear();
627                        }
628                    }
629                    if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
630                    {
631                        if (first_segment_sectID <= sectID)
632                        {
633                            lldb::user_id_t sect_uid;
634                            for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
635                            {
636                                SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
637                                SectionSP next_section_sp;
638                                if (sect_uid + 1 <= sectID)
639                                    next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
640
641                                if (curr_section_sp.get())
642                                {
643                                    if (curr_section_sp->GetByteSize() == 0)
644                                    {
645                                        if (next_section_sp.get() != NULL)
646                                            curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
647                                        else
648                                            curr_section_sp->SetByteSize ( load_cmd.vmsize );
649                                    }
650                                }
651                            }
652                        }
653                    }
654                }
655            }
656        }
657        else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
658        {
659            m_dysymtab.cmd = load_cmd.cmd;
660            m_dysymtab.cmdsize = load_cmd.cmdsize;
661            m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
662        }
663
664        offset = load_cmd_offset + load_cmd.cmdsize;
665    }
666//    if (dump_sections)
667//    {
668//        StreamFile s(stdout);
669//        m_sections_ap->Dump(&s, true);
670//    }
671    return sectID;  // Return the number of sections we registered with the module
672}
673
674class MachSymtabSectionInfo
675{
676public:
677
678    MachSymtabSectionInfo (SectionList *section_list) :
679        m_section_list (section_list),
680        m_section_infos()
681    {
682        // Get the number of sections down to a depth of 1 to include
683        // all segments and their sections, but no other sections that
684        // may be added for debug map or
685        m_section_infos.resize(section_list->GetNumSections(1));
686    }
687
688
689    Section *
690    GetSection (uint8_t n_sect, addr_t file_addr)
691    {
692        if (n_sect == 0)
693            return NULL;
694        if (n_sect < m_section_infos.size())
695        {
696            if (m_section_infos[n_sect].section == NULL)
697            {
698                Section *section = m_section_list->FindSectionByID (n_sect).get();
699                m_section_infos[n_sect].section = section;
700                if (section != NULL)
701                {
702                    m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
703                    m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
704                }
705                else
706                {
707                    Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
708                }
709            }
710            if (m_section_infos[n_sect].vm_range.Contains(file_addr))
711            {
712                // Symbol is in section.
713                return m_section_infos[n_sect].section;
714            }
715            else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
716                     m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
717            {
718                // Symbol is in section with zero size, but has the same start
719                // address as the section. This can happen with linker symbols
720                // (symbols that start with the letter 'l' or 'L'.
721                return m_section_infos[n_sect].section;
722            }
723        }
724        return m_section_list->FindSectionContainingFileAddress(file_addr).get();
725    }
726
727protected:
728    struct SectionInfo
729    {
730        SectionInfo () :
731            vm_range(),
732            section (NULL)
733        {
734        }
735
736        VMRange vm_range;
737        Section *section;
738    };
739    SectionList *m_section_list;
740    std::vector<SectionInfo> m_section_infos;
741};
742
743
744
745size_t
746ObjectFileMachO::ParseSymtab (bool minimize)
747{
748    Timer scoped_timer(__PRETTY_FUNCTION__,
749                       "ObjectFileMachO::ParseSymtab () module = %s",
750                       m_file.GetFilename().AsCString(""));
751    struct symtab_command symtab_load_command;
752    uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
753    uint32_t i;
754
755    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
756
757    for (i=0; i<m_header.ncmds; ++i)
758    {
759        const uint32_t cmd_offset = offset;
760        // Read in the load command and load command size
761        if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
762            break;
763        // Watch for the symbol table load command
764        if (symtab_load_command.cmd == LoadCommandSymtab)
765        {
766            // Read in the rest of the symtab load command
767            if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
768            {
769                if (symtab_load_command.symoff == 0)
770                {
771                    if (log)
772                        GetModule()->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
773                    return 0;
774                }
775
776                if (symtab_load_command.stroff == 0)
777                {
778                    if (log)
779                        GetModule()->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
780                    return 0;
781                }
782
783                if (symtab_load_command.nsyms == 0)
784                {
785                    if (log)
786                        GetModule()->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
787                    return 0;
788                }
789
790                if (symtab_load_command.strsize == 0)
791                {
792                    if (log)
793                        GetModule()->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
794                    return 0;
795                }
796
797                Symtab *symtab = m_symtab_ap.get();
798                SectionList *section_list = GetSectionList();
799                if (section_list == NULL)
800                    return 0;
801
802                const size_t addr_byte_size = m_data.GetAddressByteSize();
803                bool bit_width_32 = addr_byte_size == 4;
804                const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
805
806                DataExtractor nlist_data (m_data, symtab_load_command.symoff, symtab_load_command.nsyms * nlist_byte_size);
807
808                if (nlist_data.GetByteSize() == 0)
809                {
810                    if (log)
811                        GetModule()->LogMessage(log.get(), "failed to read nlist data");
812                    return 0;
813                }
814
815                DataExtractor strtab_data (m_data, symtab_load_command.stroff, symtab_load_command.strsize);
816
817                if (strtab_data.GetByteSize() == 0)
818                {
819                    if (log)
820                        GetModule()->LogMessage(log.get(), "failed to read strtab data");
821                    return 0;
822                }
823
824                static ConstString g_segment_name_TEXT ("__TEXT");
825                static ConstString g_segment_name_DATA ("__DATA");
826                static ConstString g_segment_name_OBJC ("__OBJC");
827                static ConstString g_section_name_eh_frame ("__eh_frame");
828                SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
829                SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
830                SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
831                SectionSP eh_frame_section_sp;
832                if (text_section_sp.get())
833                    eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
834                else
835                    eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
836
837                uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
838
839                uint32_t nlist_data_offset = 0;
840
841                uint32_t N_SO_index = UINT32_MAX;
842
843                MachSymtabSectionInfo section_info (section_list);
844                std::vector<uint32_t> N_FUN_indexes;
845                std::vector<uint32_t> N_NSYM_indexes;
846                std::vector<uint32_t> N_INCL_indexes;
847                std::vector<uint32_t> N_BRAC_indexes;
848                std::vector<uint32_t> N_COMM_indexes;
849                typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
850                typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
851                ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
852                ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
853                // Any symbols that get merged into another will get an entry
854                // in this map so we know
855                NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
856                uint32_t nlist_idx = 0;
857                Symbol *symbol_ptr = NULL;
858
859                uint32_t sym_idx = 0;
860                Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
861                uint32_t num_syms = symtab->GetNumSymbols();
862
863                //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
864                for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
865                {
866                    struct nlist_64 nlist;
867                    if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
868                        break;
869
870                    nlist.n_strx  = nlist_data.GetU32_unchecked(&nlist_data_offset);
871                    nlist.n_type  = nlist_data.GetU8_unchecked (&nlist_data_offset);
872                    nlist.n_sect  = nlist_data.GetU8_unchecked (&nlist_data_offset);
873                    nlist.n_desc  = nlist_data.GetU16_unchecked (&nlist_data_offset);
874                    nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
875
876                    SymbolType type = eSymbolTypeInvalid;
877                    const char *symbol_name = strtab_data.PeekCStr(nlist.n_strx);
878                    if (symbol_name == NULL)
879                    {
880                        // No symbol should be NULL, even the symbols with no
881                        // string values should have an offset zero which points
882                        // to an empty C-string
883                        Host::SystemLog (Host::eSystemLogError,
884                                         "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
885                                         nlist_idx,
886                                         nlist.n_strx,
887                                         m_module->GetFileSpec().GetDirectory().GetCString(),
888                                         m_module->GetFileSpec().GetFilename().GetCString());
889                        continue;
890                    }
891                    const char *symbol_name_non_abi_mangled = NULL;
892
893                    if (symbol_name[0] == '\0')
894                        symbol_name = NULL;
895                    Section* symbol_section = NULL;
896                    bool add_nlist = true;
897                    bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
898
899                    assert (sym_idx < num_syms);
900
901                    sym[sym_idx].SetDebug (is_debug);
902
903                    if (is_debug)
904                    {
905                        switch (nlist.n_type)
906                        {
907                        case StabGlobalSymbol:
908                            // N_GSYM -- global symbol: name,,NO_SECT,type,0
909                            // Sometimes the N_GSYM value contains the address.
910
911                            // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data.  They
912                            // have the same address, but we want to ensure that we always find only the real symbol,
913                            // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
914                            // symbol type.  This is a temporary hack to make sure the ObjectiveC symbols get treated
915                            // correctly.  To do this right, we should coalesce all the GSYM & global symbols that have the
916                            // same address.
917
918                            if (symbol_name && symbol_name[0] == '_' && symbol_name[1] ==  'O'
919                                && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
920                                    || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
921                                    || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
922                                add_nlist = false;
923                            else
924                            {
925                                sym[sym_idx].SetExternal(true);
926                                if (nlist.n_value != 0)
927                                    symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
928                                type = eSymbolTypeData;
929                            }
930                            break;
931
932                        case StabFunctionName:
933                            // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
934                            type = eSymbolTypeCompiler;
935                            break;
936
937                        case StabFunction:
938                            // N_FUN -- procedure: name,,n_sect,linenumber,address
939                            if (symbol_name)
940                            {
941                                type = eSymbolTypeCode;
942                                symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
943
944                                N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
945                                // We use the current number of symbols in the symbol table in lieu of
946                                // using nlist_idx in case we ever start trimming entries out
947                                N_FUN_indexes.push_back(sym_idx);
948                            }
949                            else
950                            {
951                                type = eSymbolTypeCompiler;
952
953                                if ( !N_FUN_indexes.empty() )
954                                {
955                                    // Copy the size of the function into the original STAB entry so we don't have
956                                    // to hunt for it later
957                                    symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
958                                    N_FUN_indexes.pop_back();
959                                    // We don't really need the end function STAB as it contains the size which
960                                    // we already placed with the original symbol, so don't add it if we want a
961                                    // minimal symbol table
962                                    if (minimize)
963                                        add_nlist = false;
964                                }
965                            }
966                            break;
967
968                        case StabStaticSymbol:
969                            // N_STSYM -- static symbol: name,,n_sect,type,address
970                            N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
971                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
972                            type = eSymbolTypeData;
973                            break;
974
975                        case StabLocalCommon:
976                            // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
977                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
978                            type = eSymbolTypeCommonBlock;
979                            break;
980
981                        case StabBeginSymbol:
982                            // N_BNSYM
983                            // We use the current number of symbols in the symbol table in lieu of
984                            // using nlist_idx in case we ever start trimming entries out
985                            if (minimize)
986                            {
987                                // Skip these if we want minimal symbol tables
988                                add_nlist = false;
989                            }
990                            else
991                            {
992                                symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
993                                N_NSYM_indexes.push_back(sym_idx);
994                                type = eSymbolTypeScopeBegin;
995                            }
996                            break;
997
998                        case StabEndSymbol:
999                            // N_ENSYM
1000                            // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1001                            // so that we can always skip the entire symbol if we need to navigate
1002                            // more quickly at the source level when parsing STABS
1003                            if (minimize)
1004                            {
1005                                // Skip these if we want minimal symbol tables
1006                                add_nlist = false;
1007                            }
1008                            else
1009                            {
1010                                if ( !N_NSYM_indexes.empty() )
1011                                {
1012                                    symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1013                                    symbol_ptr->SetByteSize(sym_idx + 1);
1014                                    symbol_ptr->SetSizeIsSibling(true);
1015                                    N_NSYM_indexes.pop_back();
1016                                }
1017                                type = eSymbolTypeScopeEnd;
1018                            }
1019                            break;
1020
1021
1022                        case StabSourceFileOptions:
1023                            // N_OPT - emitted with gcc2_compiled and in gcc source
1024                            type = eSymbolTypeCompiler;
1025                            break;
1026
1027                        case StabRegisterSymbol:
1028                            // N_RSYM - register sym: name,,NO_SECT,type,register
1029                            type = eSymbolTypeVariable;
1030                            break;
1031
1032                        case StabSourceLine:
1033                            // N_SLINE - src line: 0,,n_sect,linenumber,address
1034                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1035                            type = eSymbolTypeLineEntry;
1036                            break;
1037
1038                        case StabStructureType:
1039                            // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1040                            type = eSymbolTypeVariableType;
1041                            break;
1042
1043                        case StabSourceFileName:
1044                            // N_SO - source file name
1045                            type = eSymbolTypeSourceFile;
1046                            if (symbol_name == NULL)
1047                            {
1048                                if (minimize)
1049                                    add_nlist = false;
1050                                if (N_SO_index != UINT32_MAX)
1051                                {
1052                                    // Set the size of the N_SO to the terminating index of this N_SO
1053                                    // so that we can always skip the entire N_SO if we need to navigate
1054                                    // more quickly at the source level when parsing STABS
1055                                    symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1056                                    symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1057                                    symbol_ptr->SetSizeIsSibling(true);
1058                                }
1059                                N_NSYM_indexes.clear();
1060                                N_INCL_indexes.clear();
1061                                N_BRAC_indexes.clear();
1062                                N_COMM_indexes.clear();
1063                                N_FUN_indexes.clear();
1064                                N_SO_index = UINT32_MAX;
1065                            }
1066                            else
1067                            {
1068                                // We use the current number of symbols in the symbol table in lieu of
1069                                // using nlist_idx in case we ever start trimming entries out
1070                                if (symbol_name[0] == '/')
1071                                    N_SO_index = sym_idx;
1072                                else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1073                                {
1074                                    const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1075                                    if (so_path && so_path[0])
1076                                    {
1077                                        std::string full_so_path (so_path);
1078                                        if (*full_so_path.rbegin() != '/')
1079                                            full_so_path += '/';
1080                                        full_so_path += symbol_name;
1081                                        sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
1082                                        add_nlist = false;
1083                                        m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1084                                    }
1085                                }
1086                            }
1087
1088                            break;
1089
1090                        case StabObjectFileName:
1091                            // N_OSO - object file name: name,,0,0,st_mtime
1092                            type = eSymbolTypeObjectFile;
1093                            break;
1094
1095                        case StabLocalSymbol:
1096                            // N_LSYM - local sym: name,,NO_SECT,type,offset
1097                            type = eSymbolTypeLocal;
1098                            break;
1099
1100                        //----------------------------------------------------------------------
1101                        // INCL scopes
1102                        //----------------------------------------------------------------------
1103                        case StabBeginIncludeFileName:
1104                            // N_BINCL - include file beginning: name,,NO_SECT,0,sum
1105                            // We use the current number of symbols in the symbol table in lieu of
1106                            // using nlist_idx in case we ever start trimming entries out
1107                            N_INCL_indexes.push_back(sym_idx);
1108                            type = eSymbolTypeScopeBegin;
1109                            break;
1110
1111                        case StabEndIncludeFile:
1112                            // N_EINCL - include file end: name,,NO_SECT,0,0
1113                            // Set the size of the N_BINCL to the terminating index of this N_EINCL
1114                            // so that we can always skip the entire symbol if we need to navigate
1115                            // more quickly at the source level when parsing STABS
1116                            if ( !N_INCL_indexes.empty() )
1117                            {
1118                                symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1119                                symbol_ptr->SetByteSize(sym_idx + 1);
1120                                symbol_ptr->SetSizeIsSibling(true);
1121                                N_INCL_indexes.pop_back();
1122                            }
1123                            type = eSymbolTypeScopeEnd;
1124                            break;
1125
1126                        case StabIncludeFileName:
1127                            // N_SOL - #included file name: name,,n_sect,0,address
1128                            type = eSymbolTypeHeaderFile;
1129
1130                            // We currently don't use the header files on darwin
1131                            if (minimize)
1132                                add_nlist = false;
1133                            break;
1134
1135                        case StabCompilerParameters:
1136                            // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
1137                            type = eSymbolTypeCompiler;
1138                            break;
1139
1140                        case StabCompilerVersion:
1141                            // N_VERSION - compiler version: name,,NO_SECT,0,0
1142                            type = eSymbolTypeCompiler;
1143                            break;
1144
1145                        case StabCompilerOptLevel:
1146                            // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
1147                            type = eSymbolTypeCompiler;
1148                            break;
1149
1150                        case StabParameter:
1151                            // N_PSYM - parameter: name,,NO_SECT,type,offset
1152                            type = eSymbolTypeVariable;
1153                            break;
1154
1155                        case StabAlternateEntry:
1156                            // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
1157                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1158                            type = eSymbolTypeLineEntry;
1159                            break;
1160
1161                        //----------------------------------------------------------------------
1162                        // Left and Right Braces
1163                        //----------------------------------------------------------------------
1164                        case StabLeftBracket:
1165                            // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
1166                            // We use the current number of symbols in the symbol table in lieu of
1167                            // using nlist_idx in case we ever start trimming entries out
1168                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1169                            N_BRAC_indexes.push_back(sym_idx);
1170                            type = eSymbolTypeScopeBegin;
1171                            break;
1172
1173                        case StabRightBracket:
1174                            // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
1175                            // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1176                            // so that we can always skip the entire symbol if we need to navigate
1177                            // more quickly at the source level when parsing STABS
1178                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1179                            if ( !N_BRAC_indexes.empty() )
1180                            {
1181                                symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1182                                symbol_ptr->SetByteSize(sym_idx + 1);
1183                                symbol_ptr->SetSizeIsSibling(true);
1184                                N_BRAC_indexes.pop_back();
1185                            }
1186                            type = eSymbolTypeScopeEnd;
1187                            break;
1188
1189                        case StabDeletedIncludeFile:
1190                            // N_EXCL - deleted include file: name,,NO_SECT,0,sum
1191                            type = eSymbolTypeHeaderFile;
1192                            break;
1193
1194                        //----------------------------------------------------------------------
1195                        // COMM scopes
1196                        //----------------------------------------------------------------------
1197                        case StabBeginCommon:
1198                            // N_BCOMM - begin common: name,,NO_SECT,0,0
1199                            // We use the current number of symbols in the symbol table in lieu of
1200                            // using nlist_idx in case we ever start trimming entries out
1201                            type = eSymbolTypeScopeBegin;
1202                            N_COMM_indexes.push_back(sym_idx);
1203                            break;
1204
1205                        case StabEndCommonLocal:
1206                            // N_ECOML - end common (local name): 0,,n_sect,0,address
1207                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1208                            // Fall through
1209
1210                        case StabEndCommon:
1211                            // N_ECOMM - end common: name,,n_sect,0,0
1212                            // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1213                            // so that we can always skip the entire symbol if we need to navigate
1214                            // more quickly at the source level when parsing STABS
1215                            if ( !N_COMM_indexes.empty() )
1216                            {
1217                                symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1218                                symbol_ptr->SetByteSize(sym_idx + 1);
1219                                symbol_ptr->SetSizeIsSibling(true);
1220                                N_COMM_indexes.pop_back();
1221                            }
1222                            type = eSymbolTypeScopeEnd;
1223                            break;
1224
1225                        case StabLength:
1226                            // N_LENG - second stab entry with length information
1227                            type = eSymbolTypeAdditional;
1228                            break;
1229
1230                        default: break;
1231                        }
1232                    }
1233                    else
1234                    {
1235                        //uint8_t n_pext    = NlistMaskPrivateExternal & nlist.n_type;
1236                        uint8_t n_type  = NlistMaskType & nlist.n_type;
1237                        sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
1238
1239                        switch (n_type)
1240                        {
1241                        case NListTypeIndirect:         // N_INDR - Fall through
1242                        case NListTypePreboundUndefined:// N_PBUD - Fall through
1243                        case NListTypeUndefined:        // N_UNDF
1244                            type = eSymbolTypeUndefined;
1245                            break;
1246
1247                        case NListTypeAbsolute:         // N_ABS
1248                            type = eSymbolTypeAbsolute;
1249                            break;
1250
1251                        case NListTypeSection:          // N_SECT
1252                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1253
1254                            if (symbol_section == NULL)
1255                            {
1256                                // TODO: warn about this?
1257                                add_nlist = false;
1258                                break;
1259                            }
1260
1261                            if (TEXT_eh_frame_sectID == nlist.n_sect)
1262                            {
1263                                type = eSymbolTypeException;
1264                            }
1265                            else
1266                            {
1267                                uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
1268
1269                                switch (section_type)
1270                                {
1271                                case SectionTypeRegular:                     break; // regular section
1272                                //case SectionTypeZeroFill:                 type = eSymbolTypeData;    break; // zero fill on demand section
1273                                case SectionTypeCStringLiterals:            type = eSymbolTypeData;    break; // section with only literal C strings
1274                                case SectionType4ByteLiterals:              type = eSymbolTypeData;    break; // section with only 4 byte literals
1275                                case SectionType8ByteLiterals:              type = eSymbolTypeData;    break; // section with only 8 byte literals
1276                                case SectionTypeLiteralPointers:            type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1277                                case SectionTypeNonLazySymbolPointers:      type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1278                                case SectionTypeLazySymbolPointers:         type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1279                                case SectionTypeSymbolStubs:                type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1280                                case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for initialization
1281                                case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for termination
1282                                //case SectionTypeCoalesced:                type = eSymbolType;    break; // section contains symbols that are to be coalesced
1283                                //case SectionTypeZeroFillLarge:            type = eSymbolTypeData;    break; // zero fill on demand section (that can be larger than 4 gigabytes)
1284                                case SectionTypeInterposing:                type = eSymbolTypeTrampoline;  break; // section with only pairs of function pointers for interposing
1285                                case SectionType16ByteLiterals:             type = eSymbolTypeData;    break; // section with only 16 byte literals
1286                                case SectionTypeDTraceObjectFormat:         type = eSymbolTypeInstrumentation; break;
1287                                case SectionTypeLazyDylibSymbolPointers:    type = eSymbolTypeTrampoline; break;
1288                                default: break;
1289                                }
1290
1291                                if (type == eSymbolTypeInvalid)
1292                                {
1293                                    const char *symbol_sect_name = symbol_section->GetName().AsCString();
1294                                    if (symbol_section->IsDescendant (text_section_sp.get()))
1295                                    {
1296                                        if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1297                                                                    SectionAttrUserSelfModifyingCode |
1298                                                                    SectionAttrSytemSomeInstructions))
1299                                            type = eSymbolTypeData;
1300                                        else
1301                                            type = eSymbolTypeCode;
1302                                    }
1303                                    else
1304                                    if (symbol_section->IsDescendant(data_section_sp.get()))
1305                                    {
1306                                        if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1307                                        {
1308                                            type = eSymbolTypeRuntime;
1309
1310                                            if (symbol_name &&
1311                                                symbol_name[0] == '_' &&
1312                                                symbol_name[1] == 'O' &&
1313                                                symbol_name[2] == 'B')
1314                                            {
1315                                                llvm::StringRef symbol_name_ref(symbol_name);
1316                                                static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
1317                                                static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
1318                                                static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
1319                                                if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
1320                                                {
1321                                                    symbol_name_non_abi_mangled = symbol_name + 1;
1322                                                    symbol_name = symbol_name + g_objc_v2_prefix_class.size();
1323                                                    type = eSymbolTypeObjCClass;
1324                                                }
1325                                                else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
1326                                                {
1327                                                    symbol_name_non_abi_mangled = symbol_name + 1;
1328                                                    symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
1329                                                    type = eSymbolTypeObjCMetaClass;
1330                                                }
1331                                                else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
1332                                                {
1333                                                    symbol_name_non_abi_mangled = symbol_name + 1;
1334                                                    symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
1335                                                    type = eSymbolTypeObjCIVar;
1336                                                }
1337                                            }
1338                                        }
1339                                        else
1340                                        if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1341                                        {
1342                                            type = eSymbolTypeException;
1343                                        }
1344                                        else
1345                                        {
1346                                            type = eSymbolTypeData;
1347                                        }
1348                                    }
1349                                    else
1350                                    if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1351                                    {
1352                                        type = eSymbolTypeTrampoline;
1353                                    }
1354                                    else
1355                                    if (symbol_section->IsDescendant(objc_section_sp.get()))
1356                                    {
1357                                        type = eSymbolTypeRuntime;
1358                                        if (symbol_name && symbol_name[0] == '.')
1359                                        {
1360                                            llvm::StringRef symbol_name_ref(symbol_name);
1361                                            static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
1362                                            if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
1363                                            {
1364                                                symbol_name_non_abi_mangled = symbol_name;
1365                                                symbol_name = symbol_name + g_objc_v1_prefix_class.size();
1366                                                type = eSymbolTypeObjCClass;
1367                                            }
1368                                        }
1369                                    }
1370                                }
1371                            }
1372                            break;
1373                        }
1374                    }
1375
1376                    if (add_nlist)
1377                    {
1378                        uint64_t symbol_value = nlist.n_value;
1379                        bool symbol_name_is_mangled = false;
1380
1381                        if (symbol_name_non_abi_mangled)
1382                        {
1383                            sym[sym_idx].GetMangled().SetMangledName (symbol_name_non_abi_mangled);
1384                            sym[sym_idx].GetMangled().SetDemangledName (symbol_name);
1385                        }
1386                        else
1387                        {
1388                            if (symbol_name && symbol_name[0] == '_')
1389                            {
1390                                symbol_name_is_mangled = symbol_name[1] == '_';
1391                                symbol_name++;  // Skip the leading underscore
1392                            }
1393
1394                            if (symbol_name)
1395                            {
1396                                sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
1397                            }
1398                        }
1399
1400                        if (is_debug == false)
1401                        {
1402                            if (type == eSymbolTypeCode)
1403                            {
1404                                // See if we can find a N_FUN entry for any code symbols.
1405                                // If we do find a match, and the name matches, then we
1406                                // can merge the two into just the function symbol to avoid
1407                                // duplicate entries in the symbol table
1408                                ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1409                                if (pos != N_FUN_addr_to_sym_idx.end())
1410                                {
1411                                    if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1412                                        (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1413                                    {
1414                                        m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
1415                                        // We just need the flags from the linker symbol, so put these flags
1416                                        // into the N_FUN flags to avoid duplicate symbols in the symbol table
1417                                        sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1418                                        sym[sym_idx].Clear();
1419                                        continue;
1420                                    }
1421                                }
1422                            }
1423                            else if (type == eSymbolTypeData)
1424                            {
1425                                // See if we can find a N_STSYM entry for any data symbols.
1426                                // If we do find a match, and the name matches, then we
1427                                // can merge the two into just the Static symbol to avoid
1428                                // duplicate entries in the symbol table
1429                                ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1430                                if (pos != N_STSYM_addr_to_sym_idx.end())
1431                                {
1432                                    if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1433                                        (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1434                                    {
1435                                        m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
1436                                        // We just need the flags from the linker symbol, so put these flags
1437                                        // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1438                                        sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1439                                        sym[sym_idx].Clear();
1440                                        continue;
1441                                    }
1442                                }
1443                            }
1444                        }
1445                        if (symbol_section != NULL)
1446                            symbol_value -= symbol_section->GetFileAddress();
1447
1448                        sym[sym_idx].SetID (nlist_idx);
1449                        sym[sym_idx].SetType (type);
1450                        sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1451                        sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1452                        sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1453
1454                        ++sym_idx;
1455                    }
1456                    else
1457                    {
1458                        sym[sym_idx].Clear();
1459                    }
1460
1461                }
1462
1463                // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1464                // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1465                // such entries by figuring out what the address for the global is by looking up this non-STAB
1466                // entry and copying the value into the debug symbol's value to save us the hassle in the
1467                // debug symbol parser.
1468
1469                Symbol *global_symbol = NULL;
1470                for (nlist_idx = 0;
1471                     nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
1472                     nlist_idx++)
1473                {
1474                    if (global_symbol->GetValue().GetFileAddress() == 0)
1475                    {
1476                        std::vector<uint32_t> indexes;
1477                        if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
1478                        {
1479                            std::vector<uint32_t>::const_iterator pos;
1480                            std::vector<uint32_t>::const_iterator end = indexes.end();
1481                            for (pos = indexes.begin(); pos != end; ++pos)
1482                            {
1483                                symbol_ptr = symtab->SymbolAtIndex(*pos);
1484                                if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1485                                {
1486                                    global_symbol->SetValue(symbol_ptr->GetValue());
1487                                    break;
1488                                }
1489                            }
1490                        }
1491                    }
1492                }
1493
1494                // Trim our symbols down to just what we ended up with after
1495                // removing any symbols.
1496                if (sym_idx < num_syms)
1497                {
1498                    num_syms = sym_idx;
1499                    sym = symtab->Resize (num_syms);
1500                }
1501
1502                // Now synthesize indirect symbols
1503                if (m_dysymtab.nindirectsyms != 0)
1504                {
1505                    DataExtractor indirect_symbol_index_data (m_data, m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4);
1506
1507                    if (indirect_symbol_index_data.GetByteSize())
1508                    {
1509                        NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
1510
1511                        for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1512                        {
1513                            if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
1514                            {
1515                                uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1516                                if (symbol_stub_byte_size == 0)
1517                                    continue;
1518
1519                                const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1520
1521                                if (num_symbol_stubs == 0)
1522                                    continue;
1523
1524                                const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
1525                                uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms;
1526                                for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1527                                {
1528                                    const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1529                                    const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1530                                    uint32_t symbol_stub_offset = symbol_stub_index * 4;
1531                                    if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1532                                    {
1533                                        const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
1534                                        if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
1535                                            continue;
1536
1537                                        NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
1538                                        Symbol *stub_symbol = NULL;
1539                                        if (index_pos != end_index_pos)
1540                                        {
1541                                            // We have a remapping from the original nlist index to
1542                                            // a current symbol index, so just look this up by index
1543                                            stub_symbol = symtab->SymbolAtIndex (index_pos->second);
1544                                        }
1545                                        else
1546                                        {
1547                                            // We need to lookup a symbol using the original nlist
1548                                            // symbol index since this index is coming from the
1549                                            // S_SYMBOL_STUBS
1550                                            stub_symbol = symtab->FindSymbolByID (stub_sym_id);
1551                                        }
1552
1553                                        assert (stub_symbol);
1554                                        if (stub_symbol)
1555                                        {
1556                                            Address so_addr(symbol_stub_addr, section_list);
1557
1558                                            if (stub_symbol->GetType() == eSymbolTypeUndefined)
1559                                            {
1560                                                // Change the external symbol into a trampoline that makes sense
1561                                                // These symbols were N_UNDF N_EXT, and are useless to us, so we
1562                                                // can re-use them so we don't have to make up a synthetic symbol
1563                                                // for no good reason.
1564                                                stub_symbol->SetType (eSymbolTypeTrampoline);
1565                                                stub_symbol->SetExternal (false);
1566                                                stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1567                                                stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1568                                            }
1569                                            else
1570                                            {
1571                                                // Make a synthetic symbol to describe the trampoline stub
1572                                                if (sym_idx >= num_syms)
1573                                                    sym = symtab->Resize (++num_syms);
1574                                                sym[sym_idx].SetID (synthetic_stub_sym_id++);
1575                                                sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1576                                                sym[sym_idx].SetType (eSymbolTypeTrampoline);
1577                                                sym[sym_idx].SetIsSynthetic (true);
1578                                                sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1579                                                sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1580                                                ++sym_idx;
1581                                            }
1582                                        }
1583                                    }
1584                                }
1585                            }
1586                        }
1587                    }
1588                }
1589                return symtab->GetNumSymbols();
1590            }
1591        }
1592        offset = cmd_offset + symtab_load_command.cmdsize;
1593    }
1594    return 0;
1595}
1596
1597
1598void
1599ObjectFileMachO::Dump (Stream *s)
1600{
1601    lldb_private::Mutex::Locker locker(m_mutex);
1602    s->Printf("%p: ", this);
1603    s->Indent();
1604    if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
1605        s->PutCString("ObjectFileMachO64");
1606    else
1607        s->PutCString("ObjectFileMachO32");
1608
1609    ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1610
1611    *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
1612
1613    if (m_sections_ap.get())
1614        m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
1615
1616    if (m_symtab_ap.get())
1617        m_symtab_ap->Dump(s, NULL, eSortOrderNone);
1618}
1619
1620
1621bool
1622ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
1623{
1624    lldb_private::Mutex::Locker locker(m_mutex);
1625    struct uuid_command load_cmd;
1626    uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1627    uint32_t i;
1628    for (i=0; i<m_header.ncmds; ++i)
1629    {
1630        const uint32_t cmd_offset = offset;
1631        if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1632            break;
1633
1634        if (load_cmd.cmd == LoadCommandUUID)
1635        {
1636            const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1637            if (uuid_bytes)
1638            {
1639                uuid->SetBytes (uuid_bytes);
1640                return true;
1641            }
1642            return false;
1643        }
1644        offset = cmd_offset + load_cmd.cmdsize;
1645    }
1646    return false;
1647}
1648
1649
1650uint32_t
1651ObjectFileMachO::GetDependentModules (FileSpecList& files)
1652{
1653    lldb_private::Mutex::Locker locker(m_mutex);
1654    struct load_command load_cmd;
1655    uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1656    uint32_t count = 0;
1657    const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
1658    uint32_t i;
1659    for (i=0; i<m_header.ncmds; ++i)
1660    {
1661        const uint32_t cmd_offset = offset;
1662        if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1663            break;
1664
1665        switch (load_cmd.cmd)
1666        {
1667        case LoadCommandDylibLoad:
1668        case LoadCommandDylibLoadWeak:
1669        case LoadCommandDylibReexport:
1670        case LoadCommandDynamicLinkerLoad:
1671        case LoadCommandFixedVMShlibLoad:
1672        case LoadCommandDylibLoadUpward:
1673            {
1674                uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1675                const char *path = m_data.PeekCStr(name_offset);
1676                // Skip any path that starts with '@' since these are usually:
1677                // @executable_path/.../file
1678                // @rpath/.../file
1679                if (path && path[0] != '@')
1680                {
1681                    FileSpec file_spec(path, resolve_path);
1682                    if (files.AppendIfUnique(file_spec))
1683                        count++;
1684                }
1685            }
1686            break;
1687
1688        default:
1689            break;
1690        }
1691        offset = cmd_offset + load_cmd.cmdsize;
1692    }
1693    return count;
1694}
1695
1696lldb_private::Address
1697ObjectFileMachO::GetEntryPointAddress ()
1698{
1699    // If the object file is not an executable it can't hold the entry point.  m_entry_point_address
1700    // is initialized to an invalid address, so we can just return that.
1701    // If m_entry_point_address is valid it means we've found it already, so return the cached value.
1702
1703    if (!IsExecutable() || m_entry_point_address.IsValid())
1704        return m_entry_point_address;
1705
1706    // Otherwise, look for the UnixThread or Thread command.  The data for the Thread command is given in
1707    // /usr/include/mach-o.h, but it is basically:
1708    //
1709    //  uint32_t flavor  - this is the flavor argument you would pass to thread_get_state
1710    //  uint32_t count   - this is the count of longs in the thread state data
1711    //  struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
1712    //  <repeat this trio>
1713    //
1714    // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
1715    // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
1716    // out of data in this form & attach them to a given thread.  That should underlie the MacOS X User process plugin,
1717    // and we'll also need it for the MacOS X Core File process plugin.  When we have that we can also use it here.
1718    //
1719    // For now we hard-code the offsets and flavors we need:
1720    //
1721    //
1722
1723    lldb_private::Mutex::Locker locker(m_mutex);
1724    struct load_command load_cmd;
1725    uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1726    uint32_t i;
1727    lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
1728    bool done = false;
1729
1730    for (i=0; i<m_header.ncmds; ++i)
1731    {
1732        const uint32_t cmd_offset = offset;
1733        if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1734            break;
1735
1736        switch (load_cmd.cmd)
1737        {
1738        case LoadCommandUnixThread:
1739        case LoadCommandThread:
1740            {
1741                while (offset < cmd_offset + load_cmd.cmdsize)
1742                {
1743                    uint32_t flavor = m_data.GetU32(&offset);
1744                    uint32_t count = m_data.GetU32(&offset);
1745                    if (count == 0)
1746                    {
1747                        // We've gotten off somehow, log and exit;
1748                        return m_entry_point_address;
1749                    }
1750
1751                    switch (m_header.cputype)
1752                    {
1753                    case llvm::MachO::CPUTypeARM:
1754                       if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
1755                       {
1756                           offset += 60;  // This is the offset of pc in the GPR thread state data structure.
1757                           start_address = m_data.GetU32(&offset);
1758                           done = true;
1759                        }
1760                    break;
1761                    case llvm::MachO::CPUTypeI386:
1762                       if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
1763                       {
1764                           offset += 40;  // This is the offset of eip in the GPR thread state data structure.
1765                           start_address = m_data.GetU32(&offset);
1766                           done = true;
1767                        }
1768                    break;
1769                    case llvm::MachO::CPUTypeX86_64:
1770                       if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
1771                       {
1772                           offset += 16 * 8;  // This is the offset of rip in the GPR thread state data structure.
1773                           start_address = m_data.GetU64(&offset);
1774                           done = true;
1775                        }
1776                    break;
1777                    default:
1778                        return m_entry_point_address;
1779                    }
1780                    // Haven't found the GPR flavor yet, skip over the data for this flavor:
1781                    if (done)
1782                        break;
1783                    offset += count * 4;
1784                }
1785            }
1786            break;
1787
1788        default:
1789            break;
1790        }
1791        if (done)
1792            break;
1793
1794        // Go to the next load command:
1795        offset = cmd_offset + load_cmd.cmdsize;
1796    }
1797
1798    if (start_address != LLDB_INVALID_ADDRESS)
1799    {
1800        // We got the start address from the load commands, so now resolve that address in the sections
1801        // of this ObjectFile:
1802        if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
1803        {
1804            m_entry_point_address.Clear();
1805        }
1806    }
1807    else
1808    {
1809        // We couldn't read the UnixThread load command - maybe it wasn't there.  As a fallback look for the
1810        // "start" symbol in the main executable.
1811
1812        SymbolContextList contexts;
1813        SymbolContext context;
1814        if (!m_module->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
1815            return m_entry_point_address;
1816
1817        contexts.GetContextAtIndex(0, context);
1818
1819        m_entry_point_address = context.symbol->GetValue();
1820    }
1821
1822    return m_entry_point_address;
1823
1824}
1825
1826ObjectFile::Type
1827ObjectFileMachO::CalculateType()
1828{
1829    switch (m_header.filetype)
1830    {
1831        case HeaderFileTypeObject:                                          // 0x1u MH_OBJECT
1832            if (GetAddressByteSize () == 4)
1833            {
1834                // 32 bit kexts are just object files, but they do have a valid
1835                // UUID load command.
1836                UUID uuid;
1837                if (GetUUID(&uuid))
1838                {
1839                    // this checking for the UUID load command is not enough
1840                    // we could eventually look for the symbol named
1841                    // "OSKextGetCurrentIdentifier" as this is required of kexts
1842                    if (m_strata == eStrataInvalid)
1843                        m_strata = eStrataKernel;
1844                    return eTypeSharedLibrary;
1845                }
1846            }
1847            return eTypeObjectFile;
1848
1849        case HeaderFileTypeExecutable:          return eTypeExecutable;     // 0x2u MH_EXECUTE
1850        case HeaderFileTypeFixedVMShlib:        return eTypeSharedLibrary;  // 0x3u MH_FVMLIB
1851        case HeaderFileTypeCore:                return eTypeCoreFile;       // 0x4u MH_CORE
1852        case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary;  // 0x5u MH_PRELOAD
1853        case HeaderFileTypeDynamicShlib:        return eTypeSharedLibrary;  // 0x6u MH_DYLIB
1854        case HeaderFileTypeDynamicLinkEditor:   return eTypeDynamicLinker;  // 0x7u MH_DYLINKER
1855        case HeaderFileTypeBundle:              return eTypeSharedLibrary;  // 0x8u MH_BUNDLE
1856        case HeaderFileTypeDynamicShlibStub:    return eTypeStubLibrary;    // 0x9u MH_DYLIB_STUB
1857        case HeaderFileTypeDSYM:                return eTypeDebugInfo;      // 0xAu MH_DSYM
1858        case HeaderFileTypeKextBundle:          return eTypeSharedLibrary;  // 0xBu MH_KEXT_BUNDLE
1859        default:
1860            break;
1861    }
1862    return eTypeUnknown;
1863}
1864
1865ObjectFile::Strata
1866ObjectFileMachO::CalculateStrata()
1867{
1868    switch (m_header.filetype)
1869    {
1870        case HeaderFileTypeObject:      // 0x1u MH_OBJECT
1871            {
1872                // 32 bit kexts are just object files, but they do have a valid
1873                // UUID load command.
1874                UUID uuid;
1875                if (GetUUID(&uuid))
1876                {
1877                    // this checking for the UUID load command is not enough
1878                    // we could eventually look for the symbol named
1879                    // "OSKextGetCurrentIdentifier" as this is required of kexts
1880                    if (m_type == eTypeInvalid)
1881                        m_type = eTypeSharedLibrary;
1882
1883                    return eStrataKernel;
1884                }
1885            }
1886            return eStrataUnknown;
1887
1888        case HeaderFileTypeExecutable:                                     // 0x2u MH_EXECUTE
1889            // Check for the MH_DYLDLINK bit in the flags
1890            if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
1891                return eStrataUser;
1892            return eStrataKernel;
1893
1894        case HeaderFileTypeFixedVMShlib:        return eStrataUser;         // 0x3u MH_FVMLIB
1895        case HeaderFileTypeCore:                return eStrataUnknown;      // 0x4u MH_CORE
1896        case HeaderFileTypePreloadedExecutable: return eStrataUser;         // 0x5u MH_PRELOAD
1897        case HeaderFileTypeDynamicShlib:        return eStrataUser;         // 0x6u MH_DYLIB
1898        case HeaderFileTypeDynamicLinkEditor:   return eStrataUser;         // 0x7u MH_DYLINKER
1899        case HeaderFileTypeBundle:              return eStrataUser;         // 0x8u MH_BUNDLE
1900        case HeaderFileTypeDynamicShlibStub:    return eStrataUser;         // 0x9u MH_DYLIB_STUB
1901        case HeaderFileTypeDSYM:                return eStrataUnknown;      // 0xAu MH_DSYM
1902        case HeaderFileTypeKextBundle:          return eStrataKernel;       // 0xBu MH_KEXT_BUNDLE
1903        default:
1904            break;
1905    }
1906    return eStrataUnknown;
1907}
1908
1909
1910bool
1911ObjectFileMachO::GetArchitecture (ArchSpec &arch)
1912{
1913    lldb_private::Mutex::Locker locker(m_mutex);
1914    arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1915
1916    // Files with type MH_PRELOAD are currently used in cases where the image
1917    // debugs at the addresses in the file itself. Below we set the OS to
1918    // unknown to make sure we use the DynamicLoaderStatic()...
1919    if (m_header.filetype == HeaderFileTypePreloadedExecutable)
1920    {
1921        arch.GetTriple().setOS (llvm::Triple::UnknownOS);
1922    }
1923
1924    return true;
1925}
1926
1927
1928//------------------------------------------------------------------
1929// PluginInterface protocol
1930//------------------------------------------------------------------
1931const char *
1932ObjectFileMachO::GetPluginName()
1933{
1934    return "ObjectFileMachO";
1935}
1936
1937const char *
1938ObjectFileMachO::GetShortPluginName()
1939{
1940    return GetPluginNameStatic();
1941}
1942
1943uint32_t
1944ObjectFileMachO::GetPluginVersion()
1945{
1946    return 1;
1947}
1948
1949