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