ObjectFileMachO.cpp revision 0ad086f3ab18d6927b9df2f79169607d3a5d90e4
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 "ObjectFileMachO.h"
11
12#include "lldb/Core/ArchSpec.h"
13#include "lldb/Core/DataBuffer.h"
14#include "lldb/Core/FileSpec.h"
15#include "lldb/Core/FileSpecList.h"
16#include "lldb/Core/Module.h"
17#include "lldb/Core/PluginManager.h"
18#include "lldb/Core/Section.h"
19#include "lldb/Core/StreamFile.h"
20#include "lldb/Core/StreamString.h"
21#include "lldb/Core/Timer.h"
22#include "lldb/Core/UUID.h"
23#include "lldb/Symbol/ObjectFile.h"
24
25
26using namespace lldb;
27using namespace lldb_private;
28using namespace llvm::MachO;
29
30
31void
32ObjectFileMachO::Initialize()
33{
34    PluginManager::RegisterPlugin (GetPluginNameStatic(),
35                                   GetPluginDescriptionStatic(),
36                                   CreateInstance);
37}
38
39void
40ObjectFileMachO::Terminate()
41{
42    PluginManager::UnregisterPlugin (CreateInstance);
43}
44
45
46const char *
47ObjectFileMachO::GetPluginNameStatic()
48{
49    return "object-file.mach-o";
50}
51
52const char *
53ObjectFileMachO::GetPluginDescriptionStatic()
54{
55    return "Mach-o object file reader (32 and 64 bit)";
56}
57
58
59ObjectFile *
60ObjectFileMachO::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length)
61{
62    if (ObjectFileMachO::MagicBytesMatch(dataSP))
63    {
64        std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, dataSP, file, offset, length));
65        if (objfile_ap.get() && objfile_ap->ParseHeader())
66            return objfile_ap.release();
67    }
68    return NULL;
69}
70
71
72static uint32_t
73MachHeaderSizeFromMagic(uint32_t magic)
74{
75    switch (magic)
76    {
77    case HeaderMagic32:
78    case HeaderMagic32Swapped:
79        return sizeof(struct mach_header);
80
81    case HeaderMagic64:
82    case HeaderMagic64Swapped:
83        return sizeof(struct mach_header_64);
84        break;
85
86    default:
87        break;
88    }
89    return 0;
90}
91
92
93bool
94ObjectFileMachO::MagicBytesMatch (DataBufferSP& dataSP)
95{
96    DataExtractor data(dataSP, eByteOrderHost, 4);
97    uint32_t offset = 0;
98    uint32_t magic = data.GetU32(&offset);
99    return MachHeaderSizeFromMagic(magic) != 0;
100}
101
102
103ObjectFileMachO::ObjectFileMachO(Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) :
104    ObjectFile(module, file, offset, length, dataSP),
105    m_mutex (Mutex::eMutexTypeRecursive),
106    m_header(),
107    m_sections_ap(),
108    m_symtab_ap()
109{
110    ::bzero (&m_header, sizeof(m_header));
111    ::bzero (&m_dysymtab, sizeof(m_dysymtab));
112}
113
114
115ObjectFileMachO::~ObjectFileMachO()
116{
117}
118
119
120bool
121ObjectFileMachO::ParseHeader ()
122{
123    lldb_private::Mutex::Locker locker(m_mutex);
124    bool can_parse = false;
125    uint32_t offset = 0;
126    m_data.SetByteOrder (eByteOrderHost);
127    // Leave magic in the original byte order
128    m_header.magic = m_data.GetU32(&offset);
129    switch (m_header.magic)
130    {
131    case HeaderMagic32:
132        m_data.SetByteOrder (eByteOrderHost);
133        m_data.SetAddressByteSize(4);
134        can_parse = true;
135        break;
136
137    case HeaderMagic64:
138        m_data.SetByteOrder (eByteOrderHost);
139        m_data.SetAddressByteSize(8);
140        can_parse = true;
141        break;
142
143    case HeaderMagic32Swapped:
144        m_data.SetByteOrder(eByteOrderHost == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
145        m_data.SetAddressByteSize(4);
146        can_parse = true;
147        break;
148
149    case HeaderMagic64Swapped:
150        m_data.SetByteOrder(eByteOrderHost == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
151        m_data.SetAddressByteSize(8);
152        can_parse = true;
153        break;
154
155    default:
156        break;
157    }
158
159    if (can_parse)
160    {
161        m_data.GetU32(&offset, &m_header.cputype, 6);
162
163        ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
164
165        if (SetModulesArchitecture (mach_arch))
166        {
167            // Read in all only the load command data
168            DataBufferSP data_sp(m_file.ReadFileContents(m_offset, m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic)));
169            m_data.SetData (data_sp);
170            return true;
171        }
172    }
173    else
174    {
175        memset(&m_header, 0, sizeof(struct mach_header));
176    }
177    return false;
178}
179
180
181ByteOrder
182ObjectFileMachO::GetByteOrder () const
183{
184    lldb_private::Mutex::Locker locker(m_mutex);
185    return m_data.GetByteOrder ();
186}
187
188bool
189ObjectFileMachO::IsExecutable() const
190{
191    return m_header.filetype == HeaderFileTypeExecutable;
192}
193
194size_t
195ObjectFileMachO::GetAddressByteSize () const
196{
197    lldb_private::Mutex::Locker locker(m_mutex);
198    return m_data.GetAddressByteSize ();
199}
200
201
202Symtab *
203ObjectFileMachO::GetSymtab()
204{
205    lldb_private::Mutex::Locker locker(m_mutex);
206    if (m_symtab_ap.get() == NULL)
207    {
208        m_symtab_ap.reset(new Symtab(this));
209        ParseSymtab(false);
210    }
211    return m_symtab_ap.get();
212}
213
214
215SectionList *
216ObjectFileMachO::GetSectionList()
217{
218    lldb_private::Mutex::Locker locker(m_mutex);
219    if (m_sections_ap.get() == NULL)
220    {
221        m_sections_ap.reset(new SectionList());
222        ParseSections();
223    }
224    return m_sections_ap.get();
225}
226
227
228size_t
229ObjectFileMachO::ParseSections ()
230{
231    lldb::user_id_t segID = 0;
232    lldb::user_id_t sectID = 0;
233    struct segment_command_64 load_cmd;
234    uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
235    uint32_t i;
236    //bool dump_sections = false;
237    for (i=0; i<m_header.ncmds; ++i)
238    {
239        const uint32_t load_cmd_offset = offset;
240        if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
241            break;
242
243        if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
244        {
245            if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
246            {
247                load_cmd.vmaddr = m_data.GetAddress(&offset);
248                load_cmd.vmsize = m_data.GetAddress(&offset);
249                load_cmd.fileoff = m_data.GetAddress(&offset);
250                load_cmd.filesize = m_data.GetAddress(&offset);
251                if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
252                {
253                    // Keep a list of mach segments around in case we need to
254                    // get at data that isn't stored in the abstracted Sections.
255                    m_mach_segments.push_back (load_cmd);
256
257                    ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
258                    // Use a segment ID of the segment index shifted left by 8 so they
259                    // never conflict with any of the sections.
260                    SectionSP segment_sp;
261                    if (segment_name)
262                    {
263                        segment_sp.reset(new Section (NULL,
264                                                      GetModule(),            // Module to which this section belongs
265                                                      ++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
266                                                      segment_name,           // Name of this section
267                                                      eSectionTypeContainer,  // This section is a container of other sections.
268                                                      load_cmd.vmaddr,        // File VM address == addresses as they are found in the object file
269                                                      load_cmd.vmsize,        // VM size in bytes of this section
270                                                      load_cmd.fileoff,       // Offset to the data for this section in the file
271                                                      load_cmd.filesize,      // Size in bytes of this section as found in the the file
272                                                      load_cmd.flags));       // Flags for this section
273
274                        m_sections_ap->AddSection(segment_sp);
275                    }
276
277                    struct section_64 sect64;
278                    ::bzero (&sect64, sizeof(sect64));
279                    // Push a section into our mach sections for the section at
280                    // index zero (NListSectionNoSection)
281                    m_mach_sections.push_back(sect64);
282                    uint32_t segment_sect_idx;
283                    const lldb::user_id_t first_segment_sectID = sectID + 1;
284
285
286                    const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
287                    for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
288                    {
289                        if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
290                            break;
291                        if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
292                            break;
293                        sect64.addr = m_data.GetAddress(&offset);
294                        sect64.size = m_data.GetAddress(&offset);
295
296                        if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
297                            break;
298
299                        // Keep a list of mach sections around in case we need to
300                        // get at data that isn't stored in the abstracted Sections.
301                        m_mach_sections.push_back (sect64);
302
303                        ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
304                        if (!segment_name)
305                        {
306                            // We have a segment with no name so we need to conjure up
307                            // segments that correspond to the section's segname if there
308                            // isn't already such a section. If there is such a section,
309                            // we resize the section so that it spans all sections.
310                            // We also mark these sections as fake so address matches don't
311                            // hit if they land in the gaps between the child sections.
312                            segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
313                            segment_sp = m_sections_ap->FindSectionByName (segment_name);
314                            if (segment_sp.get())
315                            {
316                                Section *segment = segment_sp.get();
317                                // Grow the section size as needed.
318                                const lldb::addr_t sect64_min_addr = sect64.addr;
319                                const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
320                                const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
321                                const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
322                                const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
323                                if (sect64_min_addr >= curr_seg_min_addr)
324                                {
325                                    const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
326                                    // Only grow the section size if needed
327                                    if (new_seg_byte_size > curr_seg_byte_size)
328                                        segment->SetByteSize (new_seg_byte_size);
329                                }
330                                else
331                                {
332                                    // We need to change the base address of the segment and
333                                    // adjust the child section offsets for all existing children.
334                                    const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
335                                    segment->Slide(slide_amount, false);
336                                    segment->GetChildren().Slide (-slide_amount, false);
337                                    segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
338                                }
339
340                                // Grow the section size as needed.
341                                if (sect64.offset)
342                                {
343                                    const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
344                                    const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
345
346                                    const lldb::addr_t section_min_file_offset = sect64.offset;
347                                    const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
348                                    const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
349                                    const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
350                                    segment->SetFileOffset (new_file_offset);
351                                    segment->SetFileSize (new_file_size);
352                                }
353                            }
354                            else
355                            {
356                                // Create a fake section for the section's named segment
357                                segment_sp.reset(new Section(segment_sp.get(),       // Parent section
358                                                             GetModule(),            // Module to which this section belongs
359                                                             ++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
360                                                             segment_name,           // Name of this section
361                                                             eSectionTypeContainer,  // This section is a container of other sections.
362                                                             sect64.addr,            // File VM address == addresses as they are found in the object file
363                                                             sect64.size,            // VM size in bytes of this section
364                                                             sect64.offset,          // Offset to the data for this section in the file
365                                                             sect64.offset ? sect64.size : 0,        // Size in bytes of this section as found in the the file
366                                                             load_cmd.flags));       // Flags for this section
367                                segment_sp->SetIsFake(true);
368                                m_sections_ap->AddSection(segment_sp);
369                            }
370                        }
371                        assert (segment_sp.get());
372
373                        uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
374                        static ConstString g_sect_name_objc_data ("__objc_data");
375                        static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
376                        static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
377                        static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
378                        static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
379                        static ConstString g_sect_name_objc_const ("__objc_const");
380                        static ConstString g_sect_name_objc_classlist ("__objc_classlist");
381                        static ConstString g_sect_name_cfstring ("__cfstring");
382
383                        static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
384                        static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
385                        static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
386                        static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
387                        static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
388                        static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
389                        static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
390                        static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
391                        static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
392                        static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
393                        static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
394                        static ConstString g_sect_name_eh_frame ("__eh_frame");
395
396                        SectionType sect_type = eSectionTypeOther;
397
398
399                        if (section_name == g_sect_name_dwarf_debug_abbrev)
400                            sect_type = eSectionTypeDWARFDebugAbbrev;
401                        else if (section_name == g_sect_name_dwarf_debug_aranges)
402                            sect_type = eSectionTypeDWARFDebugAranges;
403                        else if (section_name == g_sect_name_dwarf_debug_frame)
404                            sect_type = eSectionTypeDWARFDebugFrame;
405                        else if (section_name == g_sect_name_dwarf_debug_info)
406                            sect_type = eSectionTypeDWARFDebugInfo;
407                        else if (section_name == g_sect_name_dwarf_debug_line)
408                            sect_type = eSectionTypeDWARFDebugLine;
409                        else if (section_name == g_sect_name_dwarf_debug_loc)
410                            sect_type = eSectionTypeDWARFDebugLoc;
411                        else if (section_name == g_sect_name_dwarf_debug_macinfo)
412                            sect_type = eSectionTypeDWARFDebugMacInfo;
413                        else if (section_name == g_sect_name_dwarf_debug_pubnames)
414                            sect_type = eSectionTypeDWARFDebugPubNames;
415                        else if (section_name == g_sect_name_dwarf_debug_pubtypes)
416                            sect_type = eSectionTypeDWARFDebugPubTypes;
417                        else if (section_name == g_sect_name_dwarf_debug_ranges)
418                            sect_type = eSectionTypeDWARFDebugRanges;
419                        else if (section_name == g_sect_name_dwarf_debug_str)
420                            sect_type = eSectionTypeDWARFDebugStr;
421                        else if (section_name == g_sect_name_objc_selrefs)
422                            sect_type = eSectionTypeDataCStringPointers;
423                        else if (section_name == g_sect_name_objc_msgrefs)
424                            sect_type = eSectionTypeDataObjCMessageRefs;
425                        else if (section_name == g_sect_name_eh_frame)
426                            sect_type = eSectionTypeEHFrame;
427                        else if (section_name == g_sect_name_cfstring)
428                            sect_type = eSectionTypeDataObjCCFStrings;
429                        else if (section_name == g_sect_name_objc_data ||
430                                 section_name == g_sect_name_objc_classrefs ||
431                                 section_name == g_sect_name_objc_superrefs ||
432                                 section_name == g_sect_name_objc_const ||
433                                 section_name == g_sect_name_objc_classlist)
434                        {
435                            sect_type = eSectionTypeDataPointers;
436                        }
437
438                        if (sect_type == eSectionTypeOther)
439                        {
440                            switch (mach_sect_type)
441                            {
442                            // TODO: categorize sections by other flags for regular sections
443                            case SectionTypeRegular:                    sect_type = eSectionTypeOther; break;
444                            case SectionTypeZeroFill:                   sect_type = eSectionTypeZeroFill; break;
445                            case SectionTypeCStringLiterals:            sect_type = eSectionTypeDataCString;    break; // section with only literal C strings
446                            case SectionType4ByteLiterals:              sect_type = eSectionTypeData4;    break; // section with only 4 byte literals
447                            case SectionType8ByteLiterals:              sect_type = eSectionTypeData8;    break; // section with only 8 byte literals
448                            case SectionTypeLiteralPointers:            sect_type = eSectionTypeDataPointers;  break; // section with only pointers to literals
449                            case SectionTypeNonLazySymbolPointers:      sect_type = eSectionTypeDataPointers;  break; // section with only non-lazy symbol pointers
450                            case SectionTypeLazySymbolPointers:         sect_type = eSectionTypeDataPointers;  break; // section with only lazy symbol pointers
451                            case SectionTypeSymbolStubs:                sect_type = eSectionTypeCode;  break; // section with only symbol stubs, byte size of stub in the reserved2 field
452                            case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers;    break; // section with only function pointers for initialization
453                            case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
454                            case SectionTypeCoalesced:                  sect_type = eSectionTypeOther; break;
455                            case SectionTypeZeroFillLarge:              sect_type = eSectionTypeZeroFill; break;
456                            case SectionTypeInterposing:                sect_type = eSectionTypeCode;  break; // section with only pairs of function pointers for interposing
457                            case SectionType16ByteLiterals:             sect_type = eSectionTypeData16; break; // section with only 16 byte literals
458                            case SectionTypeDTraceObjectFormat:         sect_type = eSectionTypeDebug; break;
459                            case SectionTypeLazyDylibSymbolPointers:    sect_type = eSectionTypeDataPointers;  break;
460                            default: break;
461                            }
462                        }
463
464                        SectionSP section_sp(new Section(segment_sp.get(),
465                                                         GetModule(),
466                                                         ++sectID,
467                                                         section_name,
468                                                         sect_type,
469                                                         sect64.addr - segment_sp->GetFileAddress(),
470                                                         sect64.size,
471                                                         sect64.offset,
472                                                         sect64.offset == 0 ? 0 : sect64.size,
473                                                         sect64.flags));
474                        segment_sp->GetChildren().AddSection(section_sp);
475
476                        if (segment_sp->IsFake())
477                        {
478                            segment_sp.reset();
479                            segment_name.Clear();
480                        }
481                    }
482                    if (m_header.filetype == HeaderFileTypeDSYM)
483                    {
484                        if (first_segment_sectID <= sectID)
485                        {
486                            lldb::user_id_t sect_uid;
487                            for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
488                            {
489                                SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
490                                SectionSP next_section_sp;
491                                if (sect_uid + 1 <= sectID)
492                                    next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
493
494                                if (curr_section_sp.get())
495                                {
496                                    if (curr_section_sp->GetByteSize() == 0)
497                                    {
498                                        if (next_section_sp.get() != NULL)
499                                            curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
500                                        else
501                                            curr_section_sp->SetByteSize ( load_cmd.vmsize );
502                                    }
503                                }
504                            }
505                        }
506                    }
507                }
508            }
509        }
510        else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
511        {
512            m_dysymtab.cmd = load_cmd.cmd;
513            m_dysymtab.cmdsize = load_cmd.cmdsize;
514            m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
515        }
516
517        offset = load_cmd_offset + load_cmd.cmdsize;
518    }
519//    if (dump_sections)
520//    {
521//        StreamFile s(stdout);
522//        m_sections_ap->Dump(&s, true);
523//    }
524    return sectID;  // Return the number of sections we registered with the module
525}
526
527class MachSymtabSectionInfo
528{
529public:
530
531    MachSymtabSectionInfo (SectionList *section_list) :
532        m_section_list (section_list),
533        m_section_infos()
534    {
535        // Get the number of sections down to a depth of 1 to include
536        // all segments and their sections, but no other sections that
537        // may be added for debug map or
538        m_section_infos.resize(section_list->GetNumSections(1));
539    }
540
541
542    Section *
543    GetSection (uint8_t n_sect, addr_t file_addr)
544    {
545        if (n_sect == 0)
546            return NULL;
547        if (n_sect < m_section_infos.size())
548        {
549            if (m_section_infos[n_sect].section == NULL)
550            {
551                Section *section = m_section_list->FindSectionByID (n_sect).get();
552                m_section_infos[n_sect].section = section;
553                assert (section != NULL);
554                m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
555                m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
556            }
557            if (m_section_infos[n_sect].vm_range.Contains(file_addr))
558                return m_section_infos[n_sect].section;
559        }
560        return m_section_list->FindSectionContainingFileAddress(file_addr).get();
561    }
562
563protected:
564    struct SectionInfo
565    {
566        SectionInfo () :
567            vm_range(),
568            section (NULL)
569        {
570        }
571
572        VMRange vm_range;
573        Section *section;
574    };
575    SectionList *m_section_list;
576    std::vector<SectionInfo> m_section_infos;
577};
578
579
580
581size_t
582ObjectFileMachO::ParseSymtab (bool minimize)
583{
584    Timer scoped_timer(__PRETTY_FUNCTION__,
585                       "ObjectFileMachO::ParseSymtab () module = %s",
586                       m_file.GetFilename().AsCString(""));
587    struct symtab_command symtab_load_command;
588    uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
589    uint32_t i;
590    for (i=0; i<m_header.ncmds; ++i)
591    {
592        const uint32_t cmd_offset = offset;
593        // Read in the load command and load command size
594        if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
595            break;
596        // Watch for the symbol table load command
597        if (symtab_load_command.cmd == LoadCommandSymtab)
598        {
599            // Read in the rest of the symtab load command
600            if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
601            {
602                Symtab *symtab = m_symtab_ap.get();
603                SectionList *section_list = GetSectionList();
604                assert(section_list);
605                const size_t addr_size = m_data.GetAddressByteSize();
606                const ByteOrder endian = m_data.GetByteOrder();
607                bool bit_width_32 = addr_size == 4;
608                const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
609
610                DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size));
611                DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize));
612
613                const char *strtab_data = (const char *)strtab_data_sp->GetBytes();
614//                DataExtractor symtab_data(symtab_data_sp, endian, addr_size);
615//                DataExtractor strtab_data(strtab_data_sp, endian, addr_size);
616
617                static ConstString g_segment_name_TEXT ("__TEXT");
618                static ConstString g_segment_name_DATA ("__DATA");
619                static ConstString g_segment_name_OBJC ("__OBJC");
620                static ConstString g_section_name_eh_frame ("__eh_frame");
621                SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
622                SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
623                SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
624                SectionSP eh_frame_section_sp;
625                if (text_section_sp.get())
626                    eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
627                else
628                    eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
629
630                uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
631                //uint32_t symtab_offset = 0;
632                const uint8_t* nlist_data = symtab_data_sp->GetBytes();
633                assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms);
634
635
636                if (endian != eByteOrderHost)
637                {
638                    // ...
639                    assert (!"UNIMPLEMENTED: Swap all nlist entries");
640                }
641                uint32_t N_SO_index = UINT_MAX;
642
643                MachSymtabSectionInfo section_info (section_list);
644                std::vector<uint32_t> N_FUN_indexes;
645                std::vector<uint32_t> N_NSYM_indexes;
646                std::vector<uint32_t> N_INCL_indexes;
647                std::vector<uint32_t> N_BRAC_indexes;
648                std::vector<uint32_t> N_COMM_indexes;
649                uint32_t nlist_idx = 0;
650                Symbol *symbol_ptr = NULL;
651
652                uint32_t sym_idx = 0;
653                Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
654                uint32_t num_syms = symtab->GetNumSymbols();
655
656                //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
657                for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
658                {
659                    struct nlist_64 nlist;
660                    if (bit_width_32)
661                    {
662                        struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size));
663                        nlist.n_strx = nlist32_ptr->n_strx;
664                        nlist.n_type = nlist32_ptr->n_type;
665                        nlist.n_sect = nlist32_ptr->n_sect;
666                        nlist.n_desc = nlist32_ptr->n_desc;
667                        nlist.n_value = nlist32_ptr->n_value;
668                    }
669                    else
670                    {
671                        nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size)));
672                    }
673
674                    SymbolType type = eSymbolTypeInvalid;
675                    const char* symbol_name = &strtab_data[nlist.n_strx];
676                    if (symbol_name[0] == '\0')
677                        symbol_name = NULL;
678                    Section* symbol_section = NULL;
679                    bool add_nlist = true;
680                    bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
681
682                    assert (sym_idx < num_syms);
683
684                    sym[sym_idx].SetDebug (is_debug);
685
686                    if (is_debug)
687                    {
688                        switch (nlist.n_type)
689                        {
690                        case StabGlobalSymbol:
691                            // N_GSYM -- global symbol: name,,NO_SECT,type,0
692                            // Sometimes the N_GSYM value contains the address.
693                            if (nlist.n_value != 0)
694                                symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
695                            type = eSymbolTypeGlobal;
696                            break;
697
698                        case StabFunctionName:
699                            // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
700                            type = eSymbolTypeFunction;
701                            break;
702
703                        case StabFunction:
704                            // N_FUN -- procedure: name,,n_sect,linenumber,address
705                            if (symbol_name)
706                            {
707                                type = eSymbolTypeFunction;
708                                symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
709                                // We use the current number of symbols in the symbol table in lieu of
710                                // using nlist_idx in case we ever start trimming entries out
711                                N_FUN_indexes.push_back(sym_idx);
712                            }
713                            else
714                            {
715                                type = eSymbolTypeFunctionEnd;
716
717                                if ( !N_FUN_indexes.empty() )
718                                {
719                                    // Copy the size of the function into the original STAB entry so we don't have
720                                    // to hunt for it later
721                                    symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
722                                    N_FUN_indexes.pop_back();
723                                    // We don't really need the end function STAB as it contains the size which
724                                    // we already placed with the original symbol, so don't add it if we want a
725                                    // minimal symbol table
726                                    if (minimize)
727                                        add_nlist = false;
728                                }
729                            }
730                            break;
731
732                        case StabStaticSymbol:
733                            // N_STSYM -- static symbol: name,,n_sect,type,address
734                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
735                            type = eSymbolTypeStatic;
736                            break;
737
738                        case StabLocalCommon:
739                            // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
740                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
741                            type = eSymbolTypeCommonBlock;
742                            break;
743
744                        case StabBeginSymbol:
745                            // N_BNSYM
746                            // We use the current number of symbols in the symbol table in lieu of
747                            // using nlist_idx in case we ever start trimming entries out
748                            if (minimize)
749                            {
750                                // Skip these if we want minimal symbol tables
751                                add_nlist = false;
752                            }
753                            else
754                            {
755                                symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
756                                N_NSYM_indexes.push_back(sym_idx);
757                                type = eSymbolTypeScopeBegin;
758                            }
759                            break;
760
761                        case StabEndSymbol:
762                            // N_ENSYM
763                            // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
764                            // so that we can always skip the entire symbol if we need to navigate
765                            // more quickly at the source level when parsing STABS
766                            if (minimize)
767                            {
768                                // Skip these if we want minimal symbol tables
769                                add_nlist = false;
770                            }
771                            else
772                            {
773                                if ( !N_NSYM_indexes.empty() )
774                                {
775                                    symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
776                                    symbol_ptr->SetByteSize(sym_idx + 1);
777                                    symbol_ptr->SetSizeIsSibling(true);
778                                    N_NSYM_indexes.pop_back();
779                                }
780                                type = eSymbolTypeScopeEnd;
781                            }
782                            break;
783
784
785                        case StabSourceFileOptions:
786                            // N_OPT - emitted with gcc2_compiled and in gcc source
787                            type = eSymbolTypeCompiler;
788                            break;
789
790                        case StabRegisterSymbol:
791                            // N_RSYM - register sym: name,,NO_SECT,type,register
792                            type = eSymbolTypeVariable;
793                            break;
794
795                        case StabSourceLine:
796                            // N_SLINE - src line: 0,,n_sect,linenumber,address
797                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
798                            type = eSymbolTypeLineEntry;
799                            break;
800
801                        case StabStructureType:
802                            // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
803                            type = eSymbolTypeVariableType;
804                            break;
805
806                        case StabSourceFileName:
807                            // N_SO - source file name
808                            type = eSymbolTypeSourceFile;
809                            if (symbol_name == NULL)
810                            {
811                                if (N_SO_index == UINT_MAX)
812                                {
813                                    // Skip the extra blank N_SO entries that happen when the entire
814                                    // path is contained in the second consecutive N_SO STAB.
815                                    if (minimize)
816                                        add_nlist = false;
817                                }
818                                else
819                                {
820                                    // Set the size of the N_SO to the terminating index of this N_SO
821                                    // so that we can always skip the entire N_SO if we need to navigate
822                                    // more quickly at the source level when parsing STABS
823                                    symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
824                                    symbol_ptr->SetByteSize(sym_idx + 1);
825                                    symbol_ptr->SetSizeIsSibling(true);
826                                }
827                                N_NSYM_indexes.clear();
828                                N_INCL_indexes.clear();
829                                N_BRAC_indexes.clear();
830                                N_COMM_indexes.clear();
831                                N_FUN_indexes.clear();
832                                N_SO_index = UINT_MAX;
833                            }
834                            else if (symbol_name[0] == '/')
835                            {
836                                // We use the current number of symbols in the symbol table in lieu of
837                                // using nlist_idx in case we ever start trimming entries out
838                                N_SO_index = sym_idx;
839                            }
840                            break;
841
842                        case StabObjectFileName:
843                            // N_OSO - object file name: name,,0,0,st_mtime
844                            type = eSymbolTypeObjectFile;
845                            break;
846
847                        case StabLocalSymbol:
848                            // N_LSYM - local sym: name,,NO_SECT,type,offset
849                            type = eSymbolTypeLocal;
850                            break;
851
852                        //----------------------------------------------------------------------
853                        // INCL scopes
854                        //----------------------------------------------------------------------
855                        case StabBeginIncludeFileName:
856                            // N_BINCL - include file beginning: name,,NO_SECT,0,sum
857                            // We use the current number of symbols in the symbol table in lieu of
858                            // using nlist_idx in case we ever start trimming entries out
859                            N_INCL_indexes.push_back(sym_idx);
860                            type = eSymbolTypeScopeBegin;
861                            break;
862
863                        case StabEndIncludeFile:
864                            // N_EINCL - include file end: name,,NO_SECT,0,0
865                            // Set the size of the N_BINCL to the terminating index of this N_EINCL
866                            // so that we can always skip the entire symbol if we need to navigate
867                            // more quickly at the source level when parsing STABS
868                            if ( !N_INCL_indexes.empty() )
869                            {
870                                symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
871                                symbol_ptr->SetByteSize(sym_idx + 1);
872                                symbol_ptr->SetSizeIsSibling(true);
873                                N_INCL_indexes.pop_back();
874                            }
875                            type = eSymbolTypeScopeEnd;
876                            break;
877
878                        case StabIncludeFileName:
879                            // N_SOL - #included file name: name,,n_sect,0,address
880                            type = eSymbolTypeHeaderFile;
881
882                            // We currently don't use the header files on darwin
883                            if (minimize)
884                                add_nlist = false;
885                            break;
886
887                        case StabCompilerParameters:
888                            // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
889                            type = eSymbolTypeCompiler;
890                            break;
891
892                        case StabCompilerVersion:
893                            // N_VERSION - compiler version: name,,NO_SECT,0,0
894                            type = eSymbolTypeCompiler;
895                            break;
896
897                        case StabCompilerOptLevel:
898                            // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
899                            type = eSymbolTypeCompiler;
900                            break;
901
902                        case StabParameter:
903                            // N_PSYM - parameter: name,,NO_SECT,type,offset
904                            type = eSymbolTypeVariable;
905                            break;
906
907                        case StabAlternateEntry:
908                            // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
909                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
910                            type = eSymbolTypeLineEntry;
911                            break;
912
913                        //----------------------------------------------------------------------
914                        // Left and Right Braces
915                        //----------------------------------------------------------------------
916                        case StabLeftBracket:
917                            // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
918                            // We use the current number of symbols in the symbol table in lieu of
919                            // using nlist_idx in case we ever start trimming entries out
920                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
921                            N_BRAC_indexes.push_back(sym_idx);
922                            type = eSymbolTypeScopeBegin;
923                            break;
924
925                        case StabRightBracket:
926                            // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
927                            // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
928                            // so that we can always skip the entire symbol if we need to navigate
929                            // more quickly at the source level when parsing STABS
930                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
931                            if ( !N_BRAC_indexes.empty() )
932                            {
933                                symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
934                                symbol_ptr->SetByteSize(sym_idx + 1);
935                                symbol_ptr->SetSizeIsSibling(true);
936                                N_BRAC_indexes.pop_back();
937                            }
938                            type = eSymbolTypeScopeEnd;
939                            break;
940
941                        case StabDeletedIncludeFile:
942                            // N_EXCL - deleted include file: name,,NO_SECT,0,sum
943                            type = eSymbolTypeHeaderFile;
944                            break;
945
946                        //----------------------------------------------------------------------
947                        // COMM scopes
948                        //----------------------------------------------------------------------
949                        case StabBeginCommon:
950                            // N_BCOMM - begin common: name,,NO_SECT,0,0
951                            // We use the current number of symbols in the symbol table in lieu of
952                            // using nlist_idx in case we ever start trimming entries out
953                            type = eSymbolTypeScopeBegin;
954                            N_COMM_indexes.push_back(sym_idx);
955                            break;
956
957                        case StabEndCommonLocal:
958                            // N_ECOML - end common (local name): 0,,n_sect,0,address
959                            symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
960                            // Fall through
961
962                        case StabEndCommon:
963                            // N_ECOMM - end common: name,,n_sect,0,0
964                            // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
965                            // so that we can always skip the entire symbol if we need to navigate
966                            // more quickly at the source level when parsing STABS
967                            if ( !N_COMM_indexes.empty() )
968                            {
969                                symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
970                                symbol_ptr->SetByteSize(sym_idx + 1);
971                                symbol_ptr->SetSizeIsSibling(true);
972                                N_COMM_indexes.pop_back();
973                            }
974                            type = eSymbolTypeScopeEnd;
975                            break;
976
977                        case StabLength:
978                            // N_LENG - second stab entry with length information
979                            type = eSymbolTypeAdditional;
980                            break;
981
982                        default: break;
983                        }
984                    }
985                    else
986                    {
987                        //uint8_t n_pext    = NlistMaskPrivateExternal & nlist.n_type;
988                        uint8_t n_type  = NlistMaskType & nlist.n_type;
989                        sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
990
991                        if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name)
992                        {
993                            type = eSymbolTypeRuntime;
994                        }
995                        else
996                        {
997                            switch (n_type)
998                            {
999                            case NListTypeIndirect:         // N_INDR - Fall through
1000                            case NListTypePreboundUndefined:// N_PBUD - Fall through
1001                            case NListTypeUndefined:        // N_UNDF
1002                                type = eSymbolTypeExtern;
1003                                break;
1004
1005                            case NListTypeAbsolute:         // N_ABS
1006                                type = eSymbolTypeAbsolute;
1007                                break;
1008
1009                            case NListTypeSection:          // N_SECT
1010                                symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1011
1012                                assert(symbol_section != NULL);
1013                                if (TEXT_eh_frame_sectID == nlist.n_sect)
1014                                {
1015                                    type = eSymbolTypeException;
1016                                }
1017                                else
1018                                {
1019                                    uint32_t section_type = symbol_section->GetAllFlagBits() & SectionFlagMaskSectionType;
1020
1021                                    switch (section_type)
1022                                    {
1023                                    case SectionTypeRegular:                     break; // regular section
1024                                    //case SectionTypeZeroFill:                 type = eSymbolTypeData;    break; // zero fill on demand section
1025                                    case SectionTypeCStringLiterals:            type = eSymbolTypeData;    break; // section with only literal C strings
1026                                    case SectionType4ByteLiterals:              type = eSymbolTypeData;    break; // section with only 4 byte literals
1027                                    case SectionType8ByteLiterals:              type = eSymbolTypeData;    break; // section with only 8 byte literals
1028                                    case SectionTypeLiteralPointers:            type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1029                                    case SectionTypeNonLazySymbolPointers:      type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1030                                    case SectionTypeLazySymbolPointers:         type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1031                                    case SectionTypeSymbolStubs:                type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1032                                    case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for initialization
1033                                    case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for termination
1034                                    //case SectionTypeCoalesced:                type = eSymbolType;    break; // section contains symbols that are to be coalesced
1035                                    //case SectionTypeZeroFillLarge:            type = eSymbolTypeData;    break; // zero fill on demand section (that can be larger than 4 gigabytes)
1036                                    case SectionTypeInterposing:                type = eSymbolTypeTrampoline;  break; // section with only pairs of function pointers for interposing
1037                                    case SectionType16ByteLiterals:             type = eSymbolTypeData;    break; // section with only 16 byte literals
1038                                    case SectionTypeDTraceObjectFormat:         type = eSymbolTypeInstrumentation; break;
1039                                    case SectionTypeLazyDylibSymbolPointers:    type = eSymbolTypeTrampoline; break;
1040                                    default: break;
1041                                    }
1042
1043                                    if (type == eSymbolTypeInvalid)
1044                                    {
1045                                        const char *symbol_sect_name = symbol_section->GetName().AsCString();
1046                                        if (symbol_section->IsDescendant (text_section_sp.get()))
1047                                        {
1048                                            if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1049                                                                        SectionAttrUserSelfModifyingCode |
1050                                                                        SectionAttrSytemSomeInstructions))
1051                                                type = eSymbolTypeData;
1052                                            else
1053                                                type = eSymbolTypeCode;
1054                                        }
1055                                        else
1056                                        if (symbol_section->IsDescendant(data_section_sp.get()))
1057                                        {
1058                                            if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1059                                            {
1060                                                type = eSymbolTypeRuntime;
1061                                            }
1062                                            else
1063                                            if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1064                                            {
1065                                                type = eSymbolTypeException;
1066                                            }
1067                                            else
1068                                            {
1069                                                type = eSymbolTypeData;
1070                                            }
1071                                        }
1072                                        else
1073                                        if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1074                                        {
1075                                            type = eSymbolTypeTrampoline;
1076                                        }
1077                                        else
1078                                        if (symbol_section->IsDescendant(objc_section_sp.get()))
1079                                        {
1080                                            type = eSymbolTypeRuntime;
1081                                        }
1082                                    }
1083                                }
1084                                break;
1085                            }
1086                        }
1087                    }
1088
1089                    if (add_nlist)
1090                    {
1091                        bool symbol_name_is_mangled = false;
1092                        if (symbol_name && symbol_name[0] == '_')
1093                        {
1094                            symbol_name_is_mangled = symbol_name[1] == '_';
1095                            symbol_name++;  // Skip the leading underscore
1096                        }
1097                        uint64_t symbol_value = nlist.n_value;
1098                        if (symbol_section != NULL)
1099                            symbol_value -= symbol_section->GetFileAddress();
1100
1101                        sym[sym_idx].SetID (nlist_idx);
1102                        sym[sym_idx].SetType (type);
1103                        if (symbol_name)
1104                            sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
1105                        sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1106                        sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1107                        sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1108
1109                        ++sym_idx;
1110                    }
1111                    else
1112                    {
1113                        sym[sym_idx].Clear();
1114                    }
1115
1116                }
1117
1118
1119                // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1120                // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1121                // such entries by figuring out what the address for the global is by looking up this non-STAB
1122                // entry and copying the value into the debug symbol's value to save us the hassle in the
1123                // debug symbol parser.
1124
1125                Symbol *global_symbol = NULL;
1126                for (nlist_idx = 0;
1127                     nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType(eSymbolTypeGlobal, nlist_idx)) != NULL;
1128                     nlist_idx++)
1129                {
1130                    if (global_symbol->GetValue().GetFileAddress() == 0)
1131                    {
1132                        std::vector<uint32_t> indexes;
1133                        if (symtab->AppendSymbolIndexesWithName(global_symbol->GetMangled().GetName(), indexes) > 0)
1134                        {
1135                            std::vector<uint32_t>::const_iterator pos;
1136                            std::vector<uint32_t>::const_iterator end = indexes.end();
1137                            for (pos = indexes.begin(); pos != end; ++pos)
1138                            {
1139                                symbol_ptr = symtab->SymbolAtIndex(*pos);
1140                                if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1141                                {
1142                                    global_symbol->SetValue(symbol_ptr->GetValue());
1143                                    break;
1144                                }
1145                            }
1146                        }
1147                    }
1148                }
1149                // Now synthesize indirect symbols
1150                if (m_dysymtab.nindirectsyms != 0)
1151                {
1152                    DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4));
1153
1154                    if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize())
1155                    {
1156                        DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1157
1158                        for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1159                        {
1160                            if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
1161                            {
1162                                uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1163                                if (symbol_stub_byte_size == 0)
1164                                    continue;
1165
1166                                const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1167
1168                                if (num_symbol_stubs == 0)
1169                                    continue;
1170
1171                                const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
1172                                uint32_t stub_sym_id = symtab_load_command.nsyms;
1173                                for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1174                                {
1175                                    const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1176                                    const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1177                                    uint32_t symbol_stub_offset = symbol_stub_index * 4;
1178                                    if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1179                                    {
1180                                        const uint32_t symbol_index = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
1181
1182                                        Symbol *stub_symbol;
1183                                        if (minimize)
1184                                            stub_symbol = symtab->FindSymbolByID (symbol_index);
1185                                        else
1186                                            stub_symbol = symtab->SymbolAtIndex (symbol_index);
1187
1188                                        assert (stub_symbol);
1189                                        if (stub_symbol)
1190                                        {
1191                                            Address so_addr(symbol_stub_addr, section_list);
1192
1193                                            if (stub_symbol->GetType() == eSymbolTypeExtern)
1194                                            {
1195                                                // Change the external symbol into a trampoline that makes sense
1196                                                // These symbols were N_UNDF N_EXT, and are useless to us, so we
1197                                                // can re-use them so we don't have to make up a synthetic symbol
1198                                                // for no good reason.
1199                                                stub_symbol->SetType (eSymbolTypeTrampoline);
1200                                                stub_symbol->SetExternal (false);
1201                                                stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1202                                                stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1203                                            }
1204                                            else
1205                                            {
1206                                                // Make a synthetic symbol to describe the trampoline stub
1207                                                if (sym_idx >= num_syms)
1208                                                {
1209                                                    sym = symtab->Resize (num_syms + 16);
1210                                                    num_syms = symtab->GetNumSymbols();
1211                                                }
1212                                                sym[sym_idx].SetID (stub_sym_id++);
1213                                                sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1214                                                sym[sym_idx].SetType (eSymbolTypeTrampoline);
1215                                                sym[sym_idx].SetIsSynthetic (true);
1216                                                sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1217                                                sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1218                                                ++sym_idx;
1219                                            }
1220                                        }
1221                                    }
1222                                }
1223                            }
1224                        }
1225                    }
1226                }
1227
1228                if (sym_idx != symtab->GetNumSymbols())
1229                    symtab->Resize (sym_idx);
1230
1231                return symtab->GetNumSymbols();
1232            }
1233        }
1234        offset = cmd_offset + symtab_load_command.cmdsize;
1235    }
1236    return 0;
1237}
1238
1239
1240void
1241ObjectFileMachO::Dump (Stream *s)
1242{
1243    lldb_private::Mutex::Locker locker(m_mutex);
1244    s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1245    s->Indent();
1246    if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
1247        s->PutCString("ObjectFileMachO64");
1248    else
1249        s->PutCString("ObjectFileMachO32");
1250
1251    ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1252
1253    *s << ", file = '" << m_file << "', arch = " << header_arch.AsCString() << "\n";
1254
1255    if (m_sections_ap.get())
1256        m_sections_ap->Dump(s, NULL, true);
1257
1258    if (m_symtab_ap.get())
1259        m_symtab_ap->Dump(s, NULL);
1260}
1261
1262
1263bool
1264ObjectFileMachO::GetUUID (UUID* uuid)
1265{
1266    lldb_private::Mutex::Locker locker(m_mutex);
1267    struct uuid_command load_cmd;
1268    uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1269    uint32_t i;
1270    for (i=0; i<m_header.ncmds; ++i)
1271    {
1272        const uint32_t cmd_offset = offset;
1273        if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1274            break;
1275
1276        if (load_cmd.cmd == LoadCommandUUID)
1277        {
1278            const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1279            if (uuid_bytes)
1280            {
1281                uuid->SetBytes (uuid_bytes);
1282                return true;
1283            }
1284            return false;
1285        }
1286        offset = cmd_offset + load_cmd.cmdsize;
1287    }
1288    return false;
1289}
1290
1291
1292uint32_t
1293ObjectFileMachO::GetDependentModules (FileSpecList& files)
1294{
1295    lldb_private::Mutex::Locker locker(m_mutex);
1296    struct load_command load_cmd;
1297    uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1298    uint32_t count = 0;
1299    uint32_t i;
1300    for (i=0; i<m_header.ncmds; ++i)
1301    {
1302        const uint32_t cmd_offset = offset;
1303        if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1304            break;
1305
1306        switch (load_cmd.cmd)
1307        {
1308        case LoadCommandDylibLoad:
1309        case LoadCommandDylibLoadWeak:
1310        case LoadCommandDylibReexport:
1311        case LoadCommandDynamicLinkerLoad:
1312        case LoadCommandFixedVMShlibLoad:
1313            {
1314                uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1315                const char *path = m_data.PeekCStr(name_offset);
1316                // Skip any path that starts with '@' since these are usually:
1317                // @executable_path/.../file
1318                // @rpath/.../file
1319                if (path && path[0] != '@')
1320                {
1321                    FileSpec file_spec(path);
1322                    if (files.AppendIfUnique(file_spec))
1323                        count++;
1324                }
1325            }
1326            break;
1327
1328        default:
1329            break;
1330        }
1331        offset = cmd_offset + load_cmd.cmdsize;
1332    }
1333    return count;
1334}
1335
1336bool
1337ObjectFileMachO::GetTargetTriple (ConstString &target_triple)
1338{
1339    lldb_private::Mutex::Locker locker(m_mutex);
1340    std::string triple(GetModule()->GetArchitecture().AsCString());
1341    triple += "-apple-darwin";
1342    target_triple.SetCString(triple.c_str());
1343    if (target_triple)
1344        return true;
1345    return false;
1346}
1347
1348
1349//------------------------------------------------------------------
1350// PluginInterface protocol
1351//------------------------------------------------------------------
1352const char *
1353ObjectFileMachO::GetPluginName()
1354{
1355    return "ObjectFileMachO";
1356}
1357
1358const char *
1359ObjectFileMachO::GetShortPluginName()
1360{
1361    return GetPluginNameStatic();
1362}
1363
1364uint32_t
1365ObjectFileMachO::GetPluginVersion()
1366{
1367    return 1;
1368}
1369
1370void
1371ObjectFileMachO::GetPluginCommandHelp (const char *command, Stream *strm)
1372{
1373}
1374
1375Error
1376ObjectFileMachO::ExecutePluginCommand (Args &command, Stream *strm)
1377{
1378    Error error;
1379    error.SetErrorString("No plug-in command are currently supported.");
1380    return error;
1381}
1382
1383Log *
1384ObjectFileMachO::EnablePluginLogging (Stream *strm, Args &command)
1385{
1386    return NULL;
1387}
1388
1389
1390
1391
1392