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