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