Home | History | Annotate | Download | only in Mach-O
      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/lldb-private-log.h"
     16 #include "lldb/Core/ArchSpec.h"
     17 #include "lldb/Core/DataBuffer.h"
     18 #include "lldb/Core/Debugger.h"
     19 #include "lldb/Core/FileSpecList.h"
     20 #include "lldb/Core/Log.h"
     21 #include "lldb/Core/Module.h"
     22 #include "lldb/Core/ModuleSpec.h"
     23 #include "lldb/Core/PluginManager.h"
     24 #include "lldb/Core/RangeMap.h"
     25 #include "lldb/Core/Section.h"
     26 #include "lldb/Core/StreamFile.h"
     27 #include "lldb/Core/StreamString.h"
     28 #include "lldb/Core/Timer.h"
     29 #include "lldb/Core/UUID.h"
     30 #include "lldb/Host/Host.h"
     31 #include "lldb/Host/FileSpec.h"
     32 #include "lldb/Symbol/ClangNamespaceDecl.h"
     33 #include "lldb/Symbol/DWARFCallFrameInfo.h"
     34 #include "lldb/Symbol/ObjectFile.h"
     35 #include "lldb/Target/Platform.h"
     36 #include "lldb/Target/Process.h"
     37 #include "lldb/Target/Target.h"
     38 #include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
     39 #include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
     40 #include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
     41 
     42 #if defined (__APPLE__) && defined (__arm__)
     43 // GetLLDBSharedCacheUUID() needs to call dlsym()
     44 #include <dlfcn.h>
     45 #endif
     46 
     47 #ifndef __APPLE__
     48 #include "Utility/UuidCompatibility.h"
     49 #endif
     50 
     51 using namespace lldb;
     52 using namespace lldb_private;
     53 using namespace llvm::MachO;
     54 
     55 class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
     56 {
     57 public:
     58     RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
     59         RegisterContextDarwin_x86_64 (thread, 0)
     60     {
     61         SetRegisterDataFrom_LC_THREAD (data);
     62     }
     63 
     64     virtual void
     65     InvalidateAllRegisters ()
     66     {
     67         // Do nothing... registers are always valid...
     68     }
     69 
     70     void
     71     SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
     72     {
     73         lldb::offset_t offset = 0;
     74         SetError (GPRRegSet, Read, -1);
     75         SetError (FPURegSet, Read, -1);
     76         SetError (EXCRegSet, Read, -1);
     77         bool done = false;
     78 
     79         while (!done)
     80         {
     81             int flavor = data.GetU32 (&offset);
     82             if (flavor == 0)
     83                 done = true;
     84             else
     85             {
     86                 uint32_t i;
     87                 uint32_t count = data.GetU32 (&offset);
     88                 switch (flavor)
     89                 {
     90                     case GPRRegSet:
     91                         for (i=0; i<count; ++i)
     92                             (&gpr.rax)[i] = data.GetU64(&offset);
     93                         SetError (GPRRegSet, Read, 0);
     94                         done = true;
     95 
     96                         break;
     97                     case FPURegSet:
     98                         // TODO: fill in FPU regs....
     99                         //SetError (FPURegSet, Read, -1);
    100                         done = true;
    101 
    102                         break;
    103                     case EXCRegSet:
    104                         exc.trapno = data.GetU32(&offset);
    105                         exc.err = data.GetU32(&offset);
    106                         exc.faultvaddr = data.GetU64(&offset);
    107                         SetError (EXCRegSet, Read, 0);
    108                         done = true;
    109                         break;
    110                     case 7:
    111                     case 8:
    112                     case 9:
    113                         // fancy flavors that encapsulate of the the above
    114                         // falvors...
    115                         break;
    116 
    117                     default:
    118                         done = true;
    119                         break;
    120                 }
    121             }
    122         }
    123     }
    124 protected:
    125     virtual int
    126     DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
    127     {
    128         return 0;
    129     }
    130 
    131     virtual int
    132     DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
    133     {
    134         return 0;
    135     }
    136 
    137     virtual int
    138     DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
    139     {
    140         return 0;
    141     }
    142 
    143     virtual int
    144     DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
    145     {
    146         return 0;
    147     }
    148 
    149     virtual int
    150     DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
    151     {
    152         return 0;
    153     }
    154 
    155     virtual int
    156     DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
    157     {
    158         return 0;
    159     }
    160 };
    161 
    162 
    163 class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
    164 {
    165 public:
    166     RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
    167     RegisterContextDarwin_i386 (thread, 0)
    168     {
    169         SetRegisterDataFrom_LC_THREAD (data);
    170     }
    171 
    172     virtual void
    173     InvalidateAllRegisters ()
    174     {
    175         // Do nothing... registers are always valid...
    176     }
    177 
    178     void
    179     SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
    180     {
    181         lldb::offset_t offset = 0;
    182         SetError (GPRRegSet, Read, -1);
    183         SetError (FPURegSet, Read, -1);
    184         SetError (EXCRegSet, Read, -1);
    185         bool done = false;
    186 
    187         while (!done)
    188         {
    189             int flavor = data.GetU32 (&offset);
    190             if (flavor == 0)
    191                 done = true;
    192             else
    193             {
    194                 uint32_t i;
    195                 uint32_t count = data.GetU32 (&offset);
    196                 switch (flavor)
    197                 {
    198                     case GPRRegSet:
    199                         for (i=0; i<count; ++i)
    200                             (&gpr.eax)[i] = data.GetU32(&offset);
    201                         SetError (GPRRegSet, Read, 0);
    202                         done = true;
    203 
    204                         break;
    205                     case FPURegSet:
    206                         // TODO: fill in FPU regs....
    207                         //SetError (FPURegSet, Read, -1);
    208                         done = true;
    209 
    210                         break;
    211                     case EXCRegSet:
    212                         exc.trapno = data.GetU32(&offset);
    213                         exc.err = data.GetU32(&offset);
    214                         exc.faultvaddr = data.GetU32(&offset);
    215                         SetError (EXCRegSet, Read, 0);
    216                         done = true;
    217                         break;
    218                     case 7:
    219                     case 8:
    220                     case 9:
    221                         // fancy flavors that encapsulate of the the above
    222                         // falvors...
    223                         break;
    224 
    225                     default:
    226                         done = true;
    227                         break;
    228                 }
    229             }
    230         }
    231     }
    232 protected:
    233     virtual int
    234     DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
    235     {
    236         return 0;
    237     }
    238 
    239     virtual int
    240     DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
    241     {
    242         return 0;
    243     }
    244 
    245     virtual int
    246     DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
    247     {
    248         return 0;
    249     }
    250 
    251     virtual int
    252     DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
    253     {
    254         return 0;
    255     }
    256 
    257     virtual int
    258     DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
    259     {
    260         return 0;
    261     }
    262 
    263     virtual int
    264     DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
    265     {
    266         return 0;
    267     }
    268 };
    269 
    270 class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
    271 {
    272 public:
    273     RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
    274         RegisterContextDarwin_arm (thread, 0)
    275     {
    276         SetRegisterDataFrom_LC_THREAD (data);
    277     }
    278 
    279     virtual void
    280     InvalidateAllRegisters ()
    281     {
    282         // Do nothing... registers are always valid...
    283     }
    284 
    285     void
    286     SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
    287     {
    288         lldb::offset_t offset = 0;
    289         SetError (GPRRegSet, Read, -1);
    290         SetError (FPURegSet, Read, -1);
    291         SetError (EXCRegSet, Read, -1);
    292         bool done = false;
    293 
    294         while (!done)
    295         {
    296             int flavor = data.GetU32 (&offset);
    297             uint32_t count = data.GetU32 (&offset);
    298             lldb::offset_t next_thread_state = offset + (count * 4);
    299             switch (flavor)
    300             {
    301                 case GPRRegSet:
    302                     for (uint32_t i=0; i<count; ++i)
    303                     {
    304                         gpr.r[i] = data.GetU32(&offset);
    305                     }
    306 
    307                     // Note that gpr.cpsr is also copied by the above loop; this loop technically extends
    308                     // one element past the end of the gpr.r[] array.
    309 
    310                     SetError (GPRRegSet, Read, 0);
    311                     offset = next_thread_state;
    312                     break;
    313 
    314                 case FPURegSet:
    315                     {
    316                         uint8_t  *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0];
    317                         const int fpu_reg_buf_size = sizeof (fpu.floats);
    318                         if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
    319                         {
    320                             offset += fpu_reg_buf_size;
    321                             fpu.fpscr = data.GetU32(&offset);
    322                             SetError (FPURegSet, Read, 0);
    323                         }
    324                         else
    325                         {
    326                             done = true;
    327                         }
    328                     }
    329                     offset = next_thread_state;
    330                     break;
    331 
    332                 case EXCRegSet:
    333                     if (count == 3)
    334                     {
    335                         exc.exception = data.GetU32(&offset);
    336                         exc.fsr = data.GetU32(&offset);
    337                         exc.far = data.GetU32(&offset);
    338                         SetError (EXCRegSet, Read, 0);
    339                     }
    340                     done = true;
    341                     offset = next_thread_state;
    342                     break;
    343 
    344                 // Unknown register set flavor, stop trying to parse.
    345                 default:
    346                     done = true;
    347             }
    348         }
    349     }
    350 protected:
    351     virtual int
    352     DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
    353     {
    354         return -1;
    355     }
    356 
    357     virtual int
    358     DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
    359     {
    360         return -1;
    361     }
    362 
    363     virtual int
    364     DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
    365     {
    366         return -1;
    367     }
    368 
    369     virtual int
    370     DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
    371     {
    372         return -1;
    373     }
    374 
    375     virtual int
    376     DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
    377     {
    378         return 0;
    379     }
    380 
    381     virtual int
    382     DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
    383     {
    384         return 0;
    385     }
    386 
    387     virtual int
    388     DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
    389     {
    390         return 0;
    391     }
    392 
    393     virtual int
    394     DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
    395     {
    396         return -1;
    397     }
    398 };
    399 
    400 static uint32_t
    401 MachHeaderSizeFromMagic(uint32_t magic)
    402 {
    403     switch (magic)
    404     {
    405         case HeaderMagic32:
    406         case HeaderMagic32Swapped:
    407             return sizeof(struct mach_header);
    408 
    409         case HeaderMagic64:
    410         case HeaderMagic64Swapped:
    411             return sizeof(struct mach_header_64);
    412             break;
    413 
    414         default:
    415             break;
    416     }
    417     return 0;
    418 }
    419 
    420 #define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
    421 
    422 void
    423 ObjectFileMachO::Initialize()
    424 {
    425     PluginManager::RegisterPlugin (GetPluginNameStatic(),
    426                                    GetPluginDescriptionStatic(),
    427                                    CreateInstance,
    428                                    CreateMemoryInstance,
    429                                    GetModuleSpecifications);
    430 }
    431 
    432 void
    433 ObjectFileMachO::Terminate()
    434 {
    435     PluginManager::UnregisterPlugin (CreateInstance);
    436 }
    437 
    438 
    439 lldb_private::ConstString
    440 ObjectFileMachO::GetPluginNameStatic()
    441 {
    442     static ConstString g_name("mach-o");
    443     return g_name;
    444 }
    445 
    446 const char *
    447 ObjectFileMachO::GetPluginDescriptionStatic()
    448 {
    449     return "Mach-o object file reader (32 and 64 bit)";
    450 }
    451 
    452 ObjectFile *
    453 ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
    454                                  DataBufferSP& data_sp,
    455                                  lldb::offset_t data_offset,
    456                                  const FileSpec* file,
    457                                  lldb::offset_t file_offset,
    458                                  lldb::offset_t length)
    459 {
    460     if (!data_sp)
    461     {
    462         data_sp = file->MemoryMapFileContents(file_offset, length);
    463         data_offset = 0;
    464     }
    465 
    466     if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
    467     {
    468         // Update the data to contain the entire file if it doesn't already
    469         if (data_sp->GetByteSize() < length)
    470         {
    471             data_sp = file->MemoryMapFileContents(file_offset, length);
    472             data_offset = 0;
    473         }
    474         std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, data_offset, file, file_offset, length));
    475         if (objfile_ap.get() && objfile_ap->ParseHeader())
    476             return objfile_ap.release();
    477     }
    478     return NULL;
    479 }
    480 
    481 ObjectFile *
    482 ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
    483                                        DataBufferSP& data_sp,
    484                                        const ProcessSP &process_sp,
    485                                        lldb::addr_t header_addr)
    486 {
    487     if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
    488     {
    489         std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
    490         if (objfile_ap.get() && objfile_ap->ParseHeader())
    491             return objfile_ap.release();
    492     }
    493     return NULL;
    494 }
    495 
    496 size_t
    497 ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
    498                                           lldb::DataBufferSP& data_sp,
    499                                           lldb::offset_t data_offset,
    500                                           lldb::offset_t file_offset,
    501                                           lldb::offset_t length,
    502                                           lldb_private::ModuleSpecList &specs)
    503 {
    504     const size_t initial_count = specs.GetSize();
    505 
    506     if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
    507     {
    508         DataExtractor data;
    509         data.SetData(data_sp);
    510         llvm::MachO::mach_header header;
    511         if (ParseHeader (data, &data_offset, header))
    512         {
    513             if (header.sizeofcmds >= data_sp->GetByteSize())
    514             {
    515                 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds);
    516                 data.SetData(data_sp);
    517                 data_offset = MachHeaderSizeFromMagic(header.magic);
    518             }
    519             if (data_sp)
    520             {
    521                 ModuleSpec spec;
    522                 spec.GetFileSpec() = file;
    523                 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
    524                                                        header.cputype,
    525                                                        header.cpusubtype);
    526                 if (spec.GetArchitecture().IsValid())
    527                 {
    528                     GetUUID (header, data, data_offset, spec.GetUUID());
    529                     specs.Append(spec);
    530                 }
    531             }
    532         }
    533     }
    534     return specs.GetSize() - initial_count;
    535 }
    536 
    537 
    538 
    539 const ConstString &
    540 ObjectFileMachO::GetSegmentNameTEXT()
    541 {
    542     static ConstString g_segment_name_TEXT ("__TEXT");
    543     return g_segment_name_TEXT;
    544 }
    545 
    546 const ConstString &
    547 ObjectFileMachO::GetSegmentNameDATA()
    548 {
    549     static ConstString g_segment_name_DATA ("__DATA");
    550     return g_segment_name_DATA;
    551 }
    552 
    553 const ConstString &
    554 ObjectFileMachO::GetSegmentNameOBJC()
    555 {
    556     static ConstString g_segment_name_OBJC ("__OBJC");
    557     return g_segment_name_OBJC;
    558 }
    559 
    560 const ConstString &
    561 ObjectFileMachO::GetSegmentNameLINKEDIT()
    562 {
    563     static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
    564     return g_section_name_LINKEDIT;
    565 }
    566 
    567 const ConstString &
    568 ObjectFileMachO::GetSectionNameEHFrame()
    569 {
    570     static ConstString g_section_name_eh_frame ("__eh_frame");
    571     return g_section_name_eh_frame;
    572 }
    573 
    574 bool
    575 ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
    576                                   lldb::addr_t data_offset,
    577                                   lldb::addr_t data_length)
    578 {
    579     DataExtractor data;
    580     data.SetData (data_sp, data_offset, data_length);
    581     lldb::offset_t offset = 0;
    582     uint32_t magic = data.GetU32(&offset);
    583     return MachHeaderSizeFromMagic(magic) != 0;
    584 }
    585 
    586 
    587 ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
    588                                  DataBufferSP& data_sp,
    589                                  lldb::offset_t data_offset,
    590                                  const FileSpec* file,
    591                                  lldb::offset_t file_offset,
    592                                  lldb::offset_t length) :
    593     ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
    594     m_mach_segments(),
    595     m_mach_sections(),
    596     m_entry_point_address(),
    597     m_thread_context_offsets(),
    598     m_thread_context_offsets_valid(false)
    599 {
    600     ::memset (&m_header, 0, sizeof(m_header));
    601     ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
    602 }
    603 
    604 ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
    605                                   lldb::DataBufferSP& header_data_sp,
    606                                   const lldb::ProcessSP &process_sp,
    607                                   lldb::addr_t header_addr) :
    608     ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
    609     m_mach_segments(),
    610     m_mach_sections(),
    611     m_entry_point_address(),
    612     m_thread_context_offsets(),
    613     m_thread_context_offsets_valid(false)
    614 {
    615     ::memset (&m_header, 0, sizeof(m_header));
    616     ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
    617 }
    618 
    619 ObjectFileMachO::~ObjectFileMachO()
    620 {
    621 }
    622 
    623 bool
    624 ObjectFileMachO::ParseHeader (DataExtractor &data,
    625                               lldb::offset_t *data_offset_ptr,
    626                               llvm::MachO::mach_header &header)
    627 {
    628     data.SetByteOrder (lldb::endian::InlHostByteOrder());
    629     // Leave magic in the original byte order
    630     header.magic = data.GetU32(data_offset_ptr);
    631     bool can_parse = false;
    632     bool is_64_bit = false;
    633     switch (header.magic)
    634     {
    635         case HeaderMagic32:
    636             data.SetByteOrder (lldb::endian::InlHostByteOrder());
    637             data.SetAddressByteSize(4);
    638             can_parse = true;
    639             break;
    640 
    641         case HeaderMagic64:
    642             data.SetByteOrder (lldb::endian::InlHostByteOrder());
    643             data.SetAddressByteSize(8);
    644             can_parse = true;
    645             is_64_bit = true;
    646             break;
    647 
    648         case HeaderMagic32Swapped:
    649             data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
    650             data.SetAddressByteSize(4);
    651             can_parse = true;
    652             break;
    653 
    654         case HeaderMagic64Swapped:
    655             data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
    656             data.SetAddressByteSize(8);
    657             is_64_bit = true;
    658             can_parse = true;
    659             break;
    660 
    661         default:
    662             break;
    663     }
    664 
    665     if (can_parse)
    666     {
    667         data.GetU32(data_offset_ptr, &header.cputype, 6);
    668         if (is_64_bit)
    669             *data_offset_ptr += 4;
    670         return true;
    671     }
    672     else
    673     {
    674         memset(&header, 0, sizeof(header));
    675     }
    676     return false;
    677 }
    678 
    679 bool
    680 ObjectFileMachO::ParseHeader ()
    681 {
    682     ModuleSP module_sp(GetModule());
    683     if (module_sp)
    684     {
    685         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
    686         bool can_parse = false;
    687         lldb::offset_t offset = 0;
    688         m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
    689         // Leave magic in the original byte order
    690         m_header.magic = m_data.GetU32(&offset);
    691         switch (m_header.magic)
    692         {
    693         case HeaderMagic32:
    694             m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
    695             m_data.SetAddressByteSize(4);
    696             can_parse = true;
    697             break;
    698 
    699         case HeaderMagic64:
    700             m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
    701             m_data.SetAddressByteSize(8);
    702             can_parse = true;
    703             break;
    704 
    705         case HeaderMagic32Swapped:
    706             m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
    707             m_data.SetAddressByteSize(4);
    708             can_parse = true;
    709             break;
    710 
    711         case HeaderMagic64Swapped:
    712             m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
    713             m_data.SetAddressByteSize(8);
    714             can_parse = true;
    715             break;
    716 
    717         default:
    718             break;
    719         }
    720 
    721         if (can_parse)
    722         {
    723             m_data.GetU32(&offset, &m_header.cputype, 6);
    724 
    725             ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
    726 
    727             // Check if the module has a required architecture
    728             const ArchSpec &module_arch = module_sp->GetArchitecture();
    729             if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
    730                 return false;
    731 
    732             if (SetModulesArchitecture (mach_arch))
    733             {
    734                 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
    735                 if (m_data.GetByteSize() < header_and_lc_size)
    736                 {
    737                     DataBufferSP data_sp;
    738                     ProcessSP process_sp (m_process_wp.lock());
    739                     if (process_sp)
    740                     {
    741                         data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
    742                     }
    743                     else
    744                     {
    745                         // Read in all only the load command data from the file on disk
    746                         data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
    747                         if (data_sp->GetByteSize() != header_and_lc_size)
    748                             return false;
    749                     }
    750                     if (data_sp)
    751                         m_data.SetData (data_sp);
    752                 }
    753             }
    754             return true;
    755         }
    756         else
    757         {
    758             memset(&m_header, 0, sizeof(struct mach_header));
    759         }
    760     }
    761     return false;
    762 }
    763 
    764 
    765 ByteOrder
    766 ObjectFileMachO::GetByteOrder () const
    767 {
    768     return m_data.GetByteOrder ();
    769 }
    770 
    771 bool
    772 ObjectFileMachO::IsExecutable() const
    773 {
    774     return m_header.filetype == HeaderFileTypeExecutable;
    775 }
    776 
    777 uint32_t
    778 ObjectFileMachO::GetAddressByteSize () const
    779 {
    780     return m_data.GetAddressByteSize ();
    781 }
    782 
    783 AddressClass
    784 ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
    785 {
    786     Symtab *symtab = GetSymtab();
    787     if (symtab)
    788     {
    789         Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
    790         if (symbol)
    791         {
    792             if (symbol->ValueIsAddress())
    793             {
    794                 SectionSP section_sp (symbol->GetAddress().GetSection());
    795                 if (section_sp)
    796                 {
    797                     const SectionType section_type = section_sp->GetType();
    798                     switch (section_type)
    799                     {
    800                     case eSectionTypeInvalid:               return eAddressClassUnknown;
    801                     case eSectionTypeCode:
    802                         if (m_header.cputype == llvm::MachO::CPUTypeARM)
    803                         {
    804                             // For ARM we have a bit in the n_desc field of the symbol
    805                             // that tells us ARM/Thumb which is bit 0x0008.
    806                             if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
    807                                 return eAddressClassCodeAlternateISA;
    808                         }
    809                         return eAddressClassCode;
    810 
    811                     case eSectionTypeContainer:             return eAddressClassUnknown;
    812                     case eSectionTypeData:
    813                     case eSectionTypeDataCString:
    814                     case eSectionTypeDataCStringPointers:
    815                     case eSectionTypeDataSymbolAddress:
    816                     case eSectionTypeData4:
    817                     case eSectionTypeData8:
    818                     case eSectionTypeData16:
    819                     case eSectionTypeDataPointers:
    820                     case eSectionTypeZeroFill:
    821                     case eSectionTypeDataObjCMessageRefs:
    822                     case eSectionTypeDataObjCCFStrings:
    823                         return eAddressClassData;
    824                     case eSectionTypeDebug:
    825                     case eSectionTypeDWARFDebugAbbrev:
    826                     case eSectionTypeDWARFDebugAranges:
    827                     case eSectionTypeDWARFDebugFrame:
    828                     case eSectionTypeDWARFDebugInfo:
    829                     case eSectionTypeDWARFDebugLine:
    830                     case eSectionTypeDWARFDebugLoc:
    831                     case eSectionTypeDWARFDebugMacInfo:
    832                     case eSectionTypeDWARFDebugPubNames:
    833                     case eSectionTypeDWARFDebugPubTypes:
    834                     case eSectionTypeDWARFDebugRanges:
    835                     case eSectionTypeDWARFDebugStr:
    836                     case eSectionTypeDWARFAppleNames:
    837                     case eSectionTypeDWARFAppleTypes:
    838                     case eSectionTypeDWARFAppleNamespaces:
    839                     case eSectionTypeDWARFAppleObjC:
    840                         return eAddressClassDebug;
    841                     case eSectionTypeEHFrame:               return eAddressClassRuntime;
    842                     case eSectionTypeELFSymbolTable:
    843                     case eSectionTypeELFDynamicSymbols:
    844                     case eSectionTypeELFRelocationEntries:
    845                     case eSectionTypeELFDynamicLinkInfo:
    846                     case eSectionTypeOther:                 return eAddressClassUnknown;
    847                     }
    848                 }
    849             }
    850 
    851             const SymbolType symbol_type = symbol->GetType();
    852             switch (symbol_type)
    853             {
    854             case eSymbolTypeAny:            return eAddressClassUnknown;
    855             case eSymbolTypeAbsolute:       return eAddressClassUnknown;
    856 
    857             case eSymbolTypeCode:
    858             case eSymbolTypeTrampoline:
    859             case eSymbolTypeResolver:
    860                 if (m_header.cputype == llvm::MachO::CPUTypeARM)
    861                 {
    862                     // For ARM we have a bit in the n_desc field of the symbol
    863                     // that tells us ARM/Thumb which is bit 0x0008.
    864                     if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
    865                         return eAddressClassCodeAlternateISA;
    866                 }
    867                 return eAddressClassCode;
    868 
    869             case eSymbolTypeData:           return eAddressClassData;
    870             case eSymbolTypeRuntime:        return eAddressClassRuntime;
    871             case eSymbolTypeException:      return eAddressClassRuntime;
    872             case eSymbolTypeSourceFile:     return eAddressClassDebug;
    873             case eSymbolTypeHeaderFile:     return eAddressClassDebug;
    874             case eSymbolTypeObjectFile:     return eAddressClassDebug;
    875             case eSymbolTypeCommonBlock:    return eAddressClassDebug;
    876             case eSymbolTypeBlock:          return eAddressClassDebug;
    877             case eSymbolTypeLocal:          return eAddressClassData;
    878             case eSymbolTypeParam:          return eAddressClassData;
    879             case eSymbolTypeVariable:       return eAddressClassData;
    880             case eSymbolTypeVariableType:   return eAddressClassDebug;
    881             case eSymbolTypeLineEntry:      return eAddressClassDebug;
    882             case eSymbolTypeLineHeader:     return eAddressClassDebug;
    883             case eSymbolTypeScopeBegin:     return eAddressClassDebug;
    884             case eSymbolTypeScopeEnd:       return eAddressClassDebug;
    885             case eSymbolTypeAdditional:     return eAddressClassUnknown;
    886             case eSymbolTypeCompiler:       return eAddressClassDebug;
    887             case eSymbolTypeInstrumentation:return eAddressClassDebug;
    888             case eSymbolTypeUndefined:      return eAddressClassUnknown;
    889             case eSymbolTypeObjCClass:      return eAddressClassRuntime;
    890             case eSymbolTypeObjCMetaClass:  return eAddressClassRuntime;
    891             case eSymbolTypeObjCIVar:       return eAddressClassRuntime;
    892             }
    893         }
    894     }
    895     return eAddressClassUnknown;
    896 }
    897 
    898 Symtab *
    899 ObjectFileMachO::GetSymtab()
    900 {
    901     ModuleSP module_sp(GetModule());
    902     if (module_sp)
    903     {
    904         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
    905         if (m_symtab_ap.get() == NULL)
    906         {
    907             m_symtab_ap.reset(new Symtab(this));
    908             Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
    909             ParseSymtab ();
    910             m_symtab_ap->Finalize ();
    911         }
    912     }
    913     return m_symtab_ap.get();
    914 }
    915 
    916 bool
    917 ObjectFileMachO::IsStripped ()
    918 {
    919     if (m_dysymtab.cmd == 0)
    920     {
    921         ModuleSP module_sp(GetModule());
    922         if (module_sp)
    923         {
    924             lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
    925             for (uint32_t i=0; i<m_header.ncmds; ++i)
    926             {
    927                 const lldb::offset_t load_cmd_offset = offset;
    928 
    929                 load_command lc;
    930                 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
    931                     break;
    932                 if (lc.cmd == LoadCommandDynamicSymtabInfo)
    933                 {
    934                     m_dysymtab.cmd = lc.cmd;
    935                     m_dysymtab.cmdsize = lc.cmdsize;
    936                     if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
    937                     {
    938                         // Clear m_dysymtab if we were unable to read all items from the load command
    939                         ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
    940                     }
    941                 }
    942                 offset = load_cmd_offset + lc.cmdsize;
    943             }
    944         }
    945     }
    946     if (m_dysymtab.cmd)
    947         return m_dysymtab.nlocalsym == 0;
    948     return false;
    949 }
    950 
    951 void
    952 ObjectFileMachO::CreateSections (SectionList &unified_section_list)
    953 {
    954     if (!m_sections_ap.get())
    955     {
    956         m_sections_ap.reset(new SectionList());
    957 
    958         const bool is_dsym = (m_header.filetype == HeaderFileTypeDSYM);
    959         lldb::user_id_t segID = 0;
    960         lldb::user_id_t sectID = 0;
    961         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
    962         uint32_t i;
    963         const bool is_core = GetType() == eTypeCoreFile;
    964         //bool dump_sections = false;
    965         ModuleSP module_sp (GetModule());
    966         // First look up any LC_ENCRYPTION_INFO load commands
    967         typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
    968         EncryptedFileRanges encrypted_file_ranges;
    969         encryption_info_command encryption_cmd;
    970         for (i=0; i<m_header.ncmds; ++i)
    971         {
    972             const lldb::offset_t load_cmd_offset = offset;
    973             if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
    974                 break;
    975 
    976             if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
    977             {
    978                 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
    979                 {
    980                     if (encryption_cmd.cryptid != 0)
    981                     {
    982                         EncryptedFileRanges::Entry entry;
    983                         entry.SetRangeBase(encryption_cmd.cryptoff);
    984                         entry.SetByteSize(encryption_cmd.cryptsize);
    985                         encrypted_file_ranges.Append(entry);
    986                     }
    987                 }
    988             }
    989             offset = load_cmd_offset + encryption_cmd.cmdsize;
    990         }
    991 
    992         offset = MachHeaderSizeFromMagic(m_header.magic);
    993 
    994         struct segment_command_64 load_cmd;
    995         for (i=0; i<m_header.ncmds; ++i)
    996         {
    997             const lldb::offset_t load_cmd_offset = offset;
    998             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
    999                 break;
   1000 
   1001             if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
   1002             {
   1003                 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
   1004                 {
   1005                     bool add_section = true;
   1006                     bool add_to_unified = true;
   1007                     ConstString const_segname (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
   1008 
   1009                     SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
   1010                     if (is_dsym && unified_section_sp)
   1011                     {
   1012                         if (const_segname == GetSegmentNameLINKEDIT())
   1013                         {
   1014                             // We need to keep the __LINKEDIT segment private to this object file only
   1015                             add_to_unified = false;
   1016                         }
   1017                         else
   1018                         {
   1019                             // This is the dSYM file and this section has already been created by
   1020                             // the object file, no need to create it.
   1021                             add_section = false;
   1022                         }
   1023                     }
   1024                     load_cmd.vmaddr = m_data.GetAddress(&offset);
   1025                     load_cmd.vmsize = m_data.GetAddress(&offset);
   1026                     load_cmd.fileoff = m_data.GetAddress(&offset);
   1027                     load_cmd.filesize = m_data.GetAddress(&offset);
   1028                     if (m_length != 0 && load_cmd.filesize != 0)
   1029                     {
   1030                         if (load_cmd.fileoff > m_length)
   1031                         {
   1032                             // We have a load command that says it extends past the end of hte file.  This is likely
   1033                             // a corrupt file.  We don't have any way to return an error condition here (this method
   1034                             // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
   1035                             // is null out the SectionList vector and if a process has been set up, dump a message
   1036                             // to stdout.  The most common case here is core file debugging with a truncated file.
   1037                             const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
   1038                             GetModule()->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")",
   1039                                                      i,
   1040                                                      lc_segment_name,
   1041                                                      load_cmd.fileoff,
   1042                                                      m_length);
   1043 
   1044                             load_cmd.fileoff = 0;
   1045                             load_cmd.filesize = 0;
   1046                         }
   1047 
   1048                         if (load_cmd.fileoff + load_cmd.filesize > m_length)
   1049                         {
   1050                             // We have a load command that says it extends past the end of hte file.  This is likely
   1051                             // a corrupt file.  We don't have any way to return an error condition here (this method
   1052                             // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
   1053                             // is null out the SectionList vector and if a process has been set up, dump a message
   1054                             // to stdout.  The most common case here is core file debugging with a truncated file.
   1055                             const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
   1056                             GetModule()->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff + filesize (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 "), the segment will be truncated",
   1057                                                      i,
   1058                                                      lc_segment_name,
   1059                                                      load_cmd.fileoff + load_cmd.filesize,
   1060                                                      m_length);
   1061 
   1062                             // Tuncase the length
   1063                             load_cmd.filesize = m_length - load_cmd.fileoff;
   1064                         }
   1065                     }
   1066                     if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
   1067                     {
   1068 
   1069                         const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
   1070 
   1071                         // Keep a list of mach segments around in case we need to
   1072                         // get at data that isn't stored in the abstracted Sections.
   1073                         m_mach_segments.push_back (load_cmd);
   1074 
   1075                         // Use a segment ID of the segment index shifted left by 8 so they
   1076                         // never conflict with any of the sections.
   1077                         SectionSP segment_sp;
   1078                         if (add_section && (const_segname || is_core))
   1079                         {
   1080                             segment_sp.reset(new Section (module_sp,              // Module to which this section belongs
   1081                                                           this,                   // Object file to which this sections belongs
   1082                                                           ++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
   1083                                                           const_segname,          // Name of this section
   1084                                                           eSectionTypeContainer,  // This section is a container of other sections.
   1085                                                           load_cmd.vmaddr,        // File VM address == addresses as they are found in the object file
   1086                                                           load_cmd.vmsize,        // VM size in bytes of this section
   1087                                                           load_cmd.fileoff,       // Offset to the data for this section in the file
   1088                                                           load_cmd.filesize,      // Size in bytes of this section as found in the the file
   1089                                                           load_cmd.flags));       // Flags for this section
   1090 
   1091                             segment_sp->SetIsEncrypted (segment_is_encrypted);
   1092                             m_sections_ap->AddSection(segment_sp);
   1093                             if (add_to_unified)
   1094                                 unified_section_list.AddSection(segment_sp);
   1095                         }
   1096                         else if (unified_section_sp)
   1097                         {
   1098                             m_sections_ap->AddSection(unified_section_sp);
   1099                         }
   1100 
   1101                         struct section_64 sect64;
   1102                         ::memset (&sect64, 0, sizeof(sect64));
   1103                         // Push a section into our mach sections for the section at
   1104                         // index zero (NListSectionNoSection) if we don't have any
   1105                         // mach sections yet...
   1106                         if (m_mach_sections.empty())
   1107                             m_mach_sections.push_back(sect64);
   1108                         uint32_t segment_sect_idx;
   1109                         const lldb::user_id_t first_segment_sectID = sectID + 1;
   1110 
   1111 
   1112                         const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
   1113                         for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
   1114                         {
   1115                             if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
   1116                                 break;
   1117                             if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
   1118                                 break;
   1119                             sect64.addr = m_data.GetAddress(&offset);
   1120                             sect64.size = m_data.GetAddress(&offset);
   1121 
   1122                             if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
   1123                                 break;
   1124 
   1125                             // Keep a list of mach sections around in case we need to
   1126                             // get at data that isn't stored in the abstracted Sections.
   1127                             m_mach_sections.push_back (sect64);
   1128 
   1129                             if (add_section)
   1130                             {
   1131                                 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
   1132                                 if (!const_segname)
   1133                                 {
   1134                                     // We have a segment with no name so we need to conjure up
   1135                                     // segments that correspond to the section's segname if there
   1136                                     // isn't already such a section. If there is such a section,
   1137                                     // we resize the section so that it spans all sections.
   1138                                     // We also mark these sections as fake so address matches don't
   1139                                     // hit if they land in the gaps between the child sections.
   1140                                     const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
   1141                                     segment_sp = unified_section_list.FindSectionByName (const_segname);
   1142                                     if (segment_sp.get())
   1143                                     {
   1144                                         Section *segment = segment_sp.get();
   1145                                         // Grow the section size as needed.
   1146                                         const lldb::addr_t sect64_min_addr = sect64.addr;
   1147                                         const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
   1148                                         const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
   1149                                         const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
   1150                                         const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
   1151                                         if (sect64_min_addr >= curr_seg_min_addr)
   1152                                         {
   1153                                             const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
   1154                                             // Only grow the section size if needed
   1155                                             if (new_seg_byte_size > curr_seg_byte_size)
   1156                                                 segment->SetByteSize (new_seg_byte_size);
   1157                                         }
   1158                                         else
   1159                                         {
   1160                                             // We need to change the base address of the segment and
   1161                                             // adjust the child section offsets for all existing children.
   1162                                             const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
   1163                                             segment->Slide(slide_amount, false);
   1164                                             segment->GetChildren().Slide(-slide_amount, false);
   1165                                             segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
   1166                                         }
   1167 
   1168                                         // Grow the section size as needed.
   1169                                         if (sect64.offset)
   1170                                         {
   1171                                             const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
   1172                                             const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
   1173 
   1174                                             const lldb::addr_t section_min_file_offset = sect64.offset;
   1175                                             const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
   1176                                             const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
   1177                                             const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
   1178                                             segment->SetFileOffset (new_file_offset);
   1179                                             segment->SetFileSize (new_file_size);
   1180                                         }
   1181                                     }
   1182                                     else
   1183                                     {
   1184                                         // Create a fake section for the section's named segment
   1185                                         segment_sp.reset(new Section (segment_sp,            // Parent section
   1186                                                                       module_sp,             // Module to which this section belongs
   1187                                                                       this,                  // Object file to which this section belongs
   1188                                                                       ++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
   1189                                                                       const_segname,         // Name of this section
   1190                                                                       eSectionTypeContainer, // This section is a container of other sections.
   1191                                                                       sect64.addr,           // File VM address == addresses as they are found in the object file
   1192                                                                       sect64.size,           // VM size in bytes of this section
   1193                                                                       sect64.offset,         // Offset to the data for this section in the file
   1194                                                                       sect64.offset ? sect64.size : 0,        // Size in bytes of this section as found in the the file
   1195                                                                       load_cmd.flags));      // Flags for this section
   1196                                         segment_sp->SetIsFake(true);
   1197 
   1198                                         m_sections_ap->AddSection(segment_sp);
   1199                                         if (add_to_unified)
   1200                                             unified_section_list.AddSection(segment_sp);
   1201                                         segment_sp->SetIsEncrypted (segment_is_encrypted);
   1202                                     }
   1203                                 }
   1204                                 assert (segment_sp.get());
   1205 
   1206                                 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
   1207                                 static ConstString g_sect_name_objc_data ("__objc_data");
   1208                                 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
   1209                                 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
   1210                                 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
   1211                                 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
   1212                                 static ConstString g_sect_name_objc_const ("__objc_const");
   1213                                 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
   1214                                 static ConstString g_sect_name_cfstring ("__cfstring");
   1215 
   1216                                 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
   1217                                 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
   1218                                 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
   1219                                 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
   1220                                 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
   1221                                 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
   1222                                 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
   1223                                 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
   1224                                 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
   1225                                 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
   1226                                 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
   1227                                 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
   1228                                 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
   1229                                 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
   1230                                 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
   1231                                 static ConstString g_sect_name_eh_frame ("__eh_frame");
   1232                                 static ConstString g_sect_name_DATA ("__DATA");
   1233                                 static ConstString g_sect_name_TEXT ("__TEXT");
   1234 
   1235                                 SectionType sect_type = eSectionTypeOther;
   1236 
   1237                                 if (section_name == g_sect_name_dwarf_debug_abbrev)
   1238                                     sect_type = eSectionTypeDWARFDebugAbbrev;
   1239                                 else if (section_name == g_sect_name_dwarf_debug_aranges)
   1240                                     sect_type = eSectionTypeDWARFDebugAranges;
   1241                                 else if (section_name == g_sect_name_dwarf_debug_frame)
   1242                                     sect_type = eSectionTypeDWARFDebugFrame;
   1243                                 else if (section_name == g_sect_name_dwarf_debug_info)
   1244                                     sect_type = eSectionTypeDWARFDebugInfo;
   1245                                 else if (section_name == g_sect_name_dwarf_debug_line)
   1246                                     sect_type = eSectionTypeDWARFDebugLine;
   1247                                 else if (section_name == g_sect_name_dwarf_debug_loc)
   1248                                     sect_type = eSectionTypeDWARFDebugLoc;
   1249                                 else if (section_name == g_sect_name_dwarf_debug_macinfo)
   1250                                     sect_type = eSectionTypeDWARFDebugMacInfo;
   1251                                 else if (section_name == g_sect_name_dwarf_debug_pubnames)
   1252                                     sect_type = eSectionTypeDWARFDebugPubNames;
   1253                                 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
   1254                                     sect_type = eSectionTypeDWARFDebugPubTypes;
   1255                                 else if (section_name == g_sect_name_dwarf_debug_ranges)
   1256                                     sect_type = eSectionTypeDWARFDebugRanges;
   1257                                 else if (section_name == g_sect_name_dwarf_debug_str)
   1258                                     sect_type = eSectionTypeDWARFDebugStr;
   1259                                 else if (section_name == g_sect_name_dwarf_apple_names)
   1260                                     sect_type = eSectionTypeDWARFAppleNames;
   1261                                 else if (section_name == g_sect_name_dwarf_apple_types)
   1262                                     sect_type = eSectionTypeDWARFAppleTypes;
   1263                                 else if (section_name == g_sect_name_dwarf_apple_namespaces)
   1264                                     sect_type = eSectionTypeDWARFAppleNamespaces;
   1265                                 else if (section_name == g_sect_name_dwarf_apple_objc)
   1266                                     sect_type = eSectionTypeDWARFAppleObjC;
   1267                                 else if (section_name == g_sect_name_objc_selrefs)
   1268                                     sect_type = eSectionTypeDataCStringPointers;
   1269                                 else if (section_name == g_sect_name_objc_msgrefs)
   1270                                     sect_type = eSectionTypeDataObjCMessageRefs;
   1271                                 else if (section_name == g_sect_name_eh_frame)
   1272                                     sect_type = eSectionTypeEHFrame;
   1273                                 else if (section_name == g_sect_name_cfstring)
   1274                                     sect_type = eSectionTypeDataObjCCFStrings;
   1275                                 else if (section_name == g_sect_name_objc_data ||
   1276                                          section_name == g_sect_name_objc_classrefs ||
   1277                                          section_name == g_sect_name_objc_superrefs ||
   1278                                          section_name == g_sect_name_objc_const ||
   1279                                          section_name == g_sect_name_objc_classlist)
   1280                                 {
   1281                                     sect_type = eSectionTypeDataPointers;
   1282                                 }
   1283 
   1284                                 if (sect_type == eSectionTypeOther)
   1285                                 {
   1286                                     switch (mach_sect_type)
   1287                                     {
   1288                                     // TODO: categorize sections by other flags for regular sections
   1289                                     case SectionTypeRegular:
   1290                                         if (segment_sp->GetName() == g_sect_name_TEXT)
   1291                                             sect_type = eSectionTypeCode;
   1292                                         else if (segment_sp->GetName() == g_sect_name_DATA)
   1293                                             sect_type = eSectionTypeData;
   1294                                         else
   1295                                             sect_type = eSectionTypeOther;
   1296                                         break;
   1297                                     case SectionTypeZeroFill:                   sect_type = eSectionTypeZeroFill; break;
   1298                                     case SectionTypeCStringLiterals:            sect_type = eSectionTypeDataCString;    break; // section with only literal C strings
   1299                                     case SectionType4ByteLiterals:              sect_type = eSectionTypeData4;    break; // section with only 4 byte literals
   1300                                     case SectionType8ByteLiterals:              sect_type = eSectionTypeData8;    break; // section with only 8 byte literals
   1301                                     case SectionTypeLiteralPointers:            sect_type = eSectionTypeDataPointers;  break; // section with only pointers to literals
   1302                                     case SectionTypeNonLazySymbolPointers:      sect_type = eSectionTypeDataPointers;  break; // section with only non-lazy symbol pointers
   1303                                     case SectionTypeLazySymbolPointers:         sect_type = eSectionTypeDataPointers;  break; // section with only lazy symbol pointers
   1304                                     case SectionTypeSymbolStubs:                sect_type = eSectionTypeCode;  break; // section with only symbol stubs, byte size of stub in the reserved2 field
   1305                                     case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers;    break; // section with only function pointers for initialization
   1306                                     case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
   1307                                     case SectionTypeCoalesced:                  sect_type = eSectionTypeOther; break;
   1308                                     case SectionTypeZeroFillLarge:              sect_type = eSectionTypeZeroFill; break;
   1309                                     case SectionTypeInterposing:                sect_type = eSectionTypeCode;  break; // section with only pairs of function pointers for interposing
   1310                                     case SectionType16ByteLiterals:             sect_type = eSectionTypeData16; break; // section with only 16 byte literals
   1311                                     case SectionTypeDTraceObjectFormat:         sect_type = eSectionTypeDebug; break;
   1312                                     case SectionTypeLazyDylibSymbolPointers:    sect_type = eSectionTypeDataPointers;  break;
   1313                                     default: break;
   1314                                     }
   1315                                 }
   1316 
   1317                                 SectionSP section_sp(new Section (segment_sp,
   1318                                                                   module_sp,
   1319                                                                   this,
   1320                                                                   ++sectID,
   1321                                                                   section_name,
   1322                                                                   sect_type,
   1323                                                                   sect64.addr - segment_sp->GetFileAddress(),
   1324                                                                   sect64.size,
   1325                                                                   sect64.offset,
   1326                                                                   sect64.offset == 0 ? 0 : sect64.size,
   1327                                                                   sect64.flags));
   1328                                 // Set the section to be encrypted to match the segment
   1329 
   1330                                 bool section_is_encrypted = false;
   1331                                 if (!segment_is_encrypted && load_cmd.filesize != 0)
   1332                                     section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
   1333 
   1334                                 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
   1335                                 segment_sp->GetChildren().AddSection(section_sp);
   1336 
   1337                                 if (segment_sp->IsFake())
   1338                                 {
   1339                                     segment_sp.reset();
   1340                                     const_segname.Clear();
   1341                                 }
   1342                             }
   1343                         }
   1344                         if (segment_sp && is_dsym)
   1345                         {
   1346                             if (first_segment_sectID <= sectID)
   1347                             {
   1348                                 lldb::user_id_t sect_uid;
   1349                                 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
   1350                                 {
   1351                                     SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
   1352                                     SectionSP next_section_sp;
   1353                                     if (sect_uid + 1 <= sectID)
   1354                                         next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
   1355 
   1356                                     if (curr_section_sp.get())
   1357                                     {
   1358                                         if (curr_section_sp->GetByteSize() == 0)
   1359                                         {
   1360                                             if (next_section_sp.get() != NULL)
   1361                                                 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
   1362                                             else
   1363                                                 curr_section_sp->SetByteSize ( load_cmd.vmsize );
   1364                                         }
   1365                                     }
   1366                                 }
   1367                             }
   1368                         }
   1369                     }
   1370                 }
   1371             }
   1372             else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
   1373             {
   1374                 m_dysymtab.cmd = load_cmd.cmd;
   1375                 m_dysymtab.cmdsize = load_cmd.cmdsize;
   1376                 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
   1377             }
   1378 
   1379             offset = load_cmd_offset + load_cmd.cmdsize;
   1380         }
   1381 
   1382 //        StreamFile s(stdout, false);                    // REMOVE THIS LINE
   1383 //        s.Printf ("Sections for %s:\n", m_file.GetPath().c_str());// REMOVE THIS LINE
   1384 //        m_sections_ap->Dump(&s, NULL, true, UINT32_MAX);// REMOVE THIS LINE
   1385     }
   1386 }
   1387 
   1388 class MachSymtabSectionInfo
   1389 {
   1390 public:
   1391 
   1392     MachSymtabSectionInfo (SectionList *section_list) :
   1393         m_section_list (section_list),
   1394         m_section_infos()
   1395     {
   1396         // Get the number of sections down to a depth of 1 to include
   1397         // all segments and their sections, but no other sections that
   1398         // may be added for debug map or
   1399         m_section_infos.resize(section_list->GetNumSections(1));
   1400     }
   1401 
   1402 
   1403     SectionSP
   1404     GetSection (uint8_t n_sect, addr_t file_addr)
   1405     {
   1406         if (n_sect == 0)
   1407             return SectionSP();
   1408         if (n_sect < m_section_infos.size())
   1409         {
   1410             if (!m_section_infos[n_sect].section_sp)
   1411             {
   1412                 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
   1413                 m_section_infos[n_sect].section_sp = section_sp;
   1414                 if (section_sp)
   1415                 {
   1416                     m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
   1417                     m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
   1418                 }
   1419                 else
   1420                 {
   1421                     Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
   1422                 }
   1423             }
   1424             if (m_section_infos[n_sect].vm_range.Contains(file_addr))
   1425             {
   1426                 // Symbol is in section.
   1427                 return m_section_infos[n_sect].section_sp;
   1428             }
   1429             else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
   1430                      m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
   1431             {
   1432                 // Symbol is in section with zero size, but has the same start
   1433                 // address as the section. This can happen with linker symbols
   1434                 // (symbols that start with the letter 'l' or 'L'.
   1435                 return m_section_infos[n_sect].section_sp;
   1436             }
   1437         }
   1438         return m_section_list->FindSectionContainingFileAddress(file_addr);
   1439     }
   1440 
   1441 protected:
   1442     struct SectionInfo
   1443     {
   1444         SectionInfo () :
   1445             vm_range(),
   1446             section_sp ()
   1447         {
   1448         }
   1449 
   1450         VMRange vm_range;
   1451         SectionSP section_sp;
   1452     };
   1453     SectionList *m_section_list;
   1454     std::vector<SectionInfo> m_section_infos;
   1455 };
   1456 
   1457 size_t
   1458 ObjectFileMachO::ParseSymtab ()
   1459 {
   1460     Timer scoped_timer(__PRETTY_FUNCTION__,
   1461                        "ObjectFileMachO::ParseSymtab () module = %s",
   1462                        m_file.GetFilename().AsCString(""));
   1463     ModuleSP module_sp (GetModule());
   1464     if (!module_sp)
   1465         return 0;
   1466 
   1467     struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
   1468     struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
   1469     typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
   1470     FunctionStarts function_starts;
   1471     lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
   1472     uint32_t i;
   1473 
   1474     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
   1475 
   1476     for (i=0; i<m_header.ncmds; ++i)
   1477     {
   1478         const lldb::offset_t cmd_offset = offset;
   1479         // Read in the load command and load command size
   1480         struct load_command lc;
   1481         if (m_data.GetU32(&offset, &lc, 2) == NULL)
   1482             break;
   1483         // Watch for the symbol table load command
   1484         switch (lc.cmd)
   1485         {
   1486         case LoadCommandSymtab:
   1487             symtab_load_command.cmd = lc.cmd;
   1488             symtab_load_command.cmdsize = lc.cmdsize;
   1489             // Read in the rest of the symtab load command
   1490             if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
   1491                 return 0;
   1492             if (symtab_load_command.symoff == 0)
   1493             {
   1494                 if (log)
   1495                     module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
   1496                 return 0;
   1497             }
   1498 
   1499             if (symtab_load_command.stroff == 0)
   1500             {
   1501                 if (log)
   1502                     module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
   1503                 return 0;
   1504             }
   1505 
   1506             if (symtab_load_command.nsyms == 0)
   1507             {
   1508                 if (log)
   1509                     module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
   1510                 return 0;
   1511             }
   1512 
   1513             if (symtab_load_command.strsize == 0)
   1514             {
   1515                 if (log)
   1516                     module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
   1517                 return 0;
   1518             }
   1519             break;
   1520 
   1521         case LoadCommandFunctionStarts:
   1522             function_starts_load_command.cmd = lc.cmd;
   1523             function_starts_load_command.cmdsize = lc.cmdsize;
   1524             if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
   1525                 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
   1526             break;
   1527 
   1528         default:
   1529             break;
   1530         }
   1531         offset = cmd_offset + lc.cmdsize;
   1532     }
   1533 
   1534     if (symtab_load_command.cmd)
   1535     {
   1536         Symtab *symtab = m_symtab_ap.get();
   1537         SectionList *section_list = GetSectionList();
   1538         if (section_list == NULL)
   1539             return 0;
   1540 
   1541         ProcessSP process_sp (m_process_wp.lock());
   1542         Process *process = process_sp.get();
   1543 
   1544         const uint32_t addr_byte_size = m_data.GetAddressByteSize();
   1545         const ByteOrder byte_order = m_data.GetByteOrder();
   1546         bool bit_width_32 = addr_byte_size == 4;
   1547         const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
   1548 
   1549         DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
   1550         DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
   1551         DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
   1552         DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
   1553 
   1554         const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
   1555         const addr_t strtab_data_byte_size = symtab_load_command.strsize;
   1556         addr_t strtab_addr = LLDB_INVALID_ADDRESS;
   1557         if (process)
   1558         {
   1559             Target &target = process->GetTarget();
   1560             SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
   1561             // Reading mach file from memory in a process or core file...
   1562 
   1563             if (linkedit_section_sp)
   1564             {
   1565                 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
   1566                 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
   1567                 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
   1568                 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
   1569 
   1570                 bool data_was_read = false;
   1571 
   1572 #if defined (__APPLE__) && defined (__arm__)
   1573                 if (m_header.flags & 0x80000000u)
   1574                 {
   1575                     // This mach-o memory file is in the dyld shared cache. If this
   1576                     // program is not remote and this is iOS, then this process will
   1577                     // share the same shared cache as the process we are debugging and
   1578                     // we can read the entire __LINKEDIT from the address space in this
   1579                     // process. This is a needed optimization that is used for local iOS
   1580                     // debugging only since all shared libraries in the shared cache do
   1581                     // not have corresponding files that exist in the file system of the
   1582                     // device. They have been combined into a single file. This means we
   1583                     // always have to load these files from memory. All of the symbol and
   1584                     // string tables from all of the __LINKEDIT sections from the shared
   1585                     // libraries in the shared cache have been merged into a single large
   1586                     // symbol and string table. Reading all of this symbol and string table
   1587                     // data across can slow down debug launch times, so we optimize this by
   1588                     // reading the memory for the __LINKEDIT section from this process.
   1589 
   1590                     UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
   1591                     UUID process_shared_cache(GetProcessSharedCacheUUID(process));
   1592                     bool use_lldb_cache = true;
   1593                     if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
   1594                     {
   1595                             use_lldb_cache = false;
   1596                             ModuleSP module_sp (GetModule());
   1597                             if (module_sp)
   1598                                 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
   1599 
   1600                     }
   1601 
   1602                     PlatformSP platform_sp (target.GetPlatform());
   1603                     if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
   1604                     {
   1605                         data_was_read = true;
   1606                         nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
   1607                         strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
   1608                         if (function_starts_load_command.cmd)
   1609                         {
   1610                             const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
   1611                             function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
   1612                         }
   1613                     }
   1614                 }
   1615 #endif
   1616 
   1617                 if (!data_was_read)
   1618                 {
   1619                     DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
   1620                     if (nlist_data_sp)
   1621                         nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
   1622                     //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
   1623                     //if (strtab_data_sp)
   1624                     //    strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
   1625                     if (m_dysymtab.nindirectsyms != 0)
   1626                     {
   1627                         const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
   1628                         DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
   1629                         if (indirect_syms_data_sp)
   1630                             indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
   1631                     }
   1632                     if (function_starts_load_command.cmd)
   1633                     {
   1634                         const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
   1635                         DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
   1636                         if (func_start_data_sp)
   1637                             function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
   1638                     }
   1639                 }
   1640             }
   1641         }
   1642         else
   1643         {
   1644             nlist_data.SetData (m_data,
   1645                                 symtab_load_command.symoff,
   1646                                 nlist_data_byte_size);
   1647             strtab_data.SetData (m_data,
   1648                                  symtab_load_command.stroff,
   1649                                  strtab_data_byte_size);
   1650             if (m_dysymtab.nindirectsyms != 0)
   1651             {
   1652                 indirect_symbol_index_data.SetData (m_data,
   1653                                                     m_dysymtab.indirectsymoff,
   1654                                                     m_dysymtab.nindirectsyms * 4);
   1655             }
   1656             if (function_starts_load_command.cmd)
   1657             {
   1658                 function_starts_data.SetData (m_data,
   1659                                               function_starts_load_command.dataoff,
   1660                                               function_starts_load_command.datasize);
   1661             }
   1662         }
   1663 
   1664         if (nlist_data.GetByteSize() == 0)
   1665         {
   1666             if (log)
   1667                 module_sp->LogMessage(log, "failed to read nlist data");
   1668             return 0;
   1669         }
   1670 
   1671 
   1672         const bool have_strtab_data = strtab_data.GetByteSize() > 0;
   1673         if (!have_strtab_data)
   1674         {
   1675             if (process)
   1676             {
   1677                 if (strtab_addr == LLDB_INVALID_ADDRESS)
   1678                 {
   1679                     if (log)
   1680                         module_sp->LogMessage(log, "failed to locate the strtab in memory");
   1681                     return 0;
   1682                 }
   1683             }
   1684             else
   1685             {
   1686                 if (log)
   1687                     module_sp->LogMessage(log, "failed to read strtab data");
   1688                 return 0;
   1689             }
   1690         }
   1691 
   1692         const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
   1693         const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
   1694         const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
   1695         const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
   1696         SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
   1697         SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
   1698         SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
   1699         SectionSP eh_frame_section_sp;
   1700         if (text_section_sp.get())
   1701             eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
   1702         else
   1703             eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
   1704 
   1705         const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
   1706 
   1707         // lldb works best if it knows the start addresss of all functions in a module.
   1708         // Linker symbols or debug info are normally the best source of information for start addr / size but
   1709         // they may be stripped in a released binary.
   1710         // Two additional sources of information exist in Mach-O binaries:
   1711         //    LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
   1712         //                         binary, relative to the text section.
   1713         //    eh_frame           - the eh_frame FDEs have the start addr & size of each function
   1714         //  LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
   1715         //  Binaries built to run on older releases may need to use eh_frame information.
   1716 
   1717         if (text_section_sp && function_starts_data.GetByteSize())
   1718         {
   1719             FunctionStarts::Entry function_start_entry;
   1720             function_start_entry.data = false;
   1721             lldb::offset_t function_start_offset = 0;
   1722             function_start_entry.addr = text_section_sp->GetFileAddress();
   1723             uint64_t delta;
   1724             while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
   1725             {
   1726                 // Now append the current entry
   1727                 function_start_entry.addr += delta;
   1728                 function_starts.Append(function_start_entry);
   1729             }
   1730         }
   1731         else
   1732         {
   1733             // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
   1734             // but it doesn't actually have the eh_frame content.  And if we have a dSYM, we don't need to do any
   1735             // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
   1736             // the module.
   1737             if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
   1738             {
   1739                 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
   1740                 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
   1741                 eh_frame.GetFunctionAddressAndSizeVector (functions);
   1742                 addr_t text_base_addr = text_section_sp->GetFileAddress();
   1743                 size_t count = functions.GetSize();
   1744                 for (size_t i = 0; i < count; ++i)
   1745                 {
   1746                     const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
   1747                     if (func)
   1748                     {
   1749                         FunctionStarts::Entry function_start_entry;
   1750                         function_start_entry.addr = func->base - text_base_addr;
   1751                         function_starts.Append(function_start_entry);
   1752                     }
   1753                 }
   1754             }
   1755         }
   1756 
   1757         const size_t function_starts_count = function_starts.GetSize();
   1758 
   1759         const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
   1760 
   1761         lldb::offset_t nlist_data_offset = 0;
   1762 
   1763         uint32_t N_SO_index = UINT32_MAX;
   1764 
   1765         MachSymtabSectionInfo section_info (section_list);
   1766         std::vector<uint32_t> N_FUN_indexes;
   1767         std::vector<uint32_t> N_NSYM_indexes;
   1768         std::vector<uint32_t> N_INCL_indexes;
   1769         std::vector<uint32_t> N_BRAC_indexes;
   1770         std::vector<uint32_t> N_COMM_indexes;
   1771         typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
   1772         typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
   1773         typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
   1774         ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
   1775         ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
   1776         ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
   1777         // Any symbols that get merged into another will get an entry
   1778         // in this map so we know
   1779         NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
   1780         uint32_t nlist_idx = 0;
   1781         Symbol *symbol_ptr = NULL;
   1782 
   1783         uint32_t sym_idx = 0;
   1784         Symbol *sym = NULL;
   1785         size_t num_syms = 0;
   1786         std::string memory_symbol_name;
   1787         uint32_t unmapped_local_symbols_found = 0;
   1788 
   1789 #if defined (__APPLE__) && defined (__arm__)
   1790 
   1791         // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
   1792         // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
   1793         // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
   1794         // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
   1795         // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
   1796         // nlist parser to ignore all LOCAL symbols.
   1797 
   1798         if (m_header.flags & 0x80000000u)
   1799         {
   1800             // Before we can start mapping the DSC, we need to make certain the target process is actually
   1801             // using the cache we can find.
   1802 
   1803             // Next we need to determine the correct path for the dyld shared cache.
   1804 
   1805             ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
   1806             char dsc_path[PATH_MAX];
   1807 
   1808             snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
   1809                      "/System/Library/Caches/com.apple.dyld/",  /* IPHONE_DYLD_SHARED_CACHE_DIR */
   1810                      "dyld_shared_cache_",          /* DYLD_SHARED_CACHE_BASE_NAME */
   1811                      header_arch.GetArchitectureName());
   1812 
   1813             FileSpec dsc_filespec(dsc_path, false);
   1814 
   1815             // We need definitions of two structures in the on-disk DSC, copy them here manually
   1816             struct lldb_copy_dyld_cache_header_v0
   1817             {
   1818                 char        magic[16];            // e.g. "dyld_v0    i386", "dyld_v1   armv7", etc.
   1819                 uint32_t    mappingOffset;        // file offset to first dyld_cache_mapping_info
   1820                 uint32_t    mappingCount;         // number of dyld_cache_mapping_info entries
   1821                 uint32_t    imagesOffset;
   1822                 uint32_t    imagesCount;
   1823                 uint64_t    dyldBaseAddress;
   1824                 uint64_t    codeSignatureOffset;
   1825                 uint64_t    codeSignatureSize;
   1826                 uint64_t    slideInfoOffset;
   1827                 uint64_t    slideInfoSize;
   1828                 uint64_t    localSymbolsOffset;   // file offset of where local symbols are stored
   1829                 uint64_t    localSymbolsSize;     // size of local symbols information
   1830             };
   1831             struct lldb_copy_dyld_cache_header_v1
   1832             {
   1833                 char        magic[16];            // e.g. "dyld_v0    i386", "dyld_v1   armv7", etc.
   1834                 uint32_t    mappingOffset;        // file offset to first dyld_cache_mapping_info
   1835                 uint32_t    mappingCount;         // number of dyld_cache_mapping_info entries
   1836                 uint32_t    imagesOffset;
   1837                 uint32_t    imagesCount;
   1838                 uint64_t    dyldBaseAddress;
   1839                 uint64_t    codeSignatureOffset;
   1840                 uint64_t    codeSignatureSize;
   1841                 uint64_t    slideInfoOffset;
   1842                 uint64_t    slideInfoSize;
   1843                 uint64_t    localSymbolsOffset;
   1844                 uint64_t    localSymbolsSize;
   1845                 uint8_t     uuid[16];             // v1 and above, also recorded in dyld_all_image_infos v13 and later
   1846             };
   1847 
   1848             struct lldb_copy_dyld_cache_mapping_info
   1849             {
   1850                 uint64_t        address;
   1851                 uint64_t        size;
   1852                 uint64_t        fileOffset;
   1853                 uint32_t        maxProt;
   1854                 uint32_t        initProt;
   1855             };
   1856 
   1857             struct lldb_copy_dyld_cache_local_symbols_info
   1858             {
   1859                 uint32_t        nlistOffset;
   1860                 uint32_t        nlistCount;
   1861                 uint32_t        stringsOffset;
   1862                 uint32_t        stringsSize;
   1863                 uint32_t        entriesOffset;
   1864                 uint32_t        entriesCount;
   1865             };
   1866             struct lldb_copy_dyld_cache_local_symbols_entry
   1867             {
   1868                 uint32_t        dylibOffset;
   1869                 uint32_t        nlistStartIndex;
   1870                 uint32_t        nlistCount;
   1871             };
   1872 
   1873             /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
   1874                The dyld_cache_local_symbols_info structure gives us three things:
   1875                  1. The start and count of the nlist records in the dyld_shared_cache file
   1876                  2. The start and size of the strings for these nlist records
   1877                  3. The start and count of dyld_cache_local_symbols_entry entries
   1878 
   1879                There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
   1880                The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
   1881                The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
   1882                and the count of how many nlist records there are for this dylib/framework.
   1883             */
   1884 
   1885             // Process the dsc header to find the unmapped symbols
   1886             //
   1887             // Save some VM space, do not map the entire cache in one shot.
   1888 
   1889             DataBufferSP dsc_data_sp;
   1890             dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
   1891 
   1892             if (dsc_data_sp)
   1893             {
   1894                 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
   1895 
   1896                 char version_str[17];
   1897                 int version = -1;
   1898                 lldb::offset_t offset = 0;
   1899                 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
   1900                 version_str[16] = '\0';
   1901                 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
   1902                 {
   1903                     int v;
   1904                     if (::sscanf (version_str + 6, "%d", &v) == 1)
   1905                     {
   1906                         version = v;
   1907                     }
   1908                 }
   1909 
   1910                 UUID dsc_uuid;
   1911                 if (version >= 1)
   1912                 {
   1913                     offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
   1914                     uint8_t uuid_bytes[sizeof (uuid_t)];
   1915                     memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
   1916                     dsc_uuid.SetBytes (uuid_bytes);
   1917                 }
   1918 
   1919                 bool uuid_match = true;
   1920                 if (dsc_uuid.IsValid() && process)
   1921                 {
   1922                     UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
   1923 
   1924                     if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
   1925                     {
   1926                         // The on-disk dyld_shared_cache file is not the same as the one in this
   1927                         // process' memory, don't use it.
   1928                         uuid_match = false;
   1929                         ModuleSP module_sp (GetModule());
   1930                         if (module_sp)
   1931                             module_sp->ReportWarning ("process shared cache does not match on-disk dyld_shared_cache file, some symbol names will be missing.");
   1932                     }
   1933                 }
   1934 
   1935                 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
   1936 
   1937                 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
   1938 
   1939                 // If the mappingOffset points to a location inside the header, we've
   1940                 // opened an old dyld shared cache, and should not proceed further.
   1941                 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
   1942                 {
   1943 
   1944                     DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
   1945                     DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
   1946                     offset = 0;
   1947 
   1948                     // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
   1949                     // in the shared library cache need to be adjusted by an offset to match up with the
   1950                     // dylibOffset identifying field in the dyld_cache_local_symbol_entry's.  This offset is
   1951                     // recorded in mapping_offset_value.
   1952                     const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
   1953 
   1954                     offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
   1955                     uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
   1956                     uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
   1957 
   1958                     if (localSymbolsOffset && localSymbolsSize)
   1959                     {
   1960                         // Map the local symbols
   1961                         if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
   1962                         {
   1963                             DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
   1964 
   1965                             offset = 0;
   1966 
   1967                             // Read the local_symbols_infos struct in one shot
   1968                             struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
   1969                             dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
   1970 
   1971                             SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
   1972 
   1973                             uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
   1974 
   1975                             offset = local_symbols_info.entriesOffset;
   1976                             for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
   1977                             {
   1978                                 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
   1979                                 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
   1980                                 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
   1981                                 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
   1982 
   1983                                 if (header_file_offset == local_symbols_entry.dylibOffset)
   1984                                 {
   1985                                     unmapped_local_symbols_found = local_symbols_entry.nlistCount;
   1986 
   1987                                     // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
   1988                                     sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
   1989                                     num_syms = symtab->GetNumSymbols();
   1990 
   1991                                     nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
   1992                                     uint32_t string_table_offset = local_symbols_info.stringsOffset;
   1993 
   1994                                     for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
   1995                                     {
   1996                                         /////////////////////////////
   1997                                         {
   1998                                             struct nlist_64 nlist;
   1999                                             if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
   2000                                                 break;
   2001 
   2002                                             nlist.n_strx  = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
   2003                                             nlist.n_type  = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
   2004                                             nlist.n_sect  = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
   2005                                             nlist.n_desc  = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
   2006                                             nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
   2007 
   2008                                             SymbolType type = eSymbolTypeInvalid;
   2009                                             const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
   2010 
   2011                                             if (symbol_name == NULL)
   2012                                             {
   2013                                                 // No symbol should be NULL, even the symbols with no
   2014                                                 // string values should have an offset zero which points
   2015                                                 // to an empty C-string
   2016                                                 Host::SystemLog (Host::eSystemLogError,
   2017                                                                  "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
   2018                                                                  entry_index,
   2019                                                                  nlist.n_strx,
   2020                                                                  module_sp->GetFileSpec().GetPath().c_str());
   2021                                                 continue;
   2022                                             }
   2023                                             if (symbol_name[0] == '\0')
   2024                                                 symbol_name = NULL;
   2025 
   2026                                             const char *symbol_name_non_abi_mangled = NULL;
   2027 
   2028                                             SectionSP symbol_section;
   2029                                             uint32_t symbol_byte_size = 0;
   2030                                             bool add_nlist = true;
   2031                                             bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
   2032                                             bool demangled_is_synthesized = false;
   2033                                             bool is_gsym = false;
   2034 
   2035                                             assert (sym_idx < num_syms);
   2036 
   2037                                             sym[sym_idx].SetDebug (is_debug);
   2038 
   2039                                             if (is_debug)
   2040                                             {
   2041                                                 switch (nlist.n_type)
   2042                                                 {
   2043                                                     case StabGlobalSymbol:
   2044                                                         // N_GSYM -- global symbol: name,,NO_SECT,type,0
   2045                                                         // Sometimes the N_GSYM value contains the address.
   2046 
   2047                                                         // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data.  They
   2048                                                         // have the same address, but we want to ensure that we always find only the real symbol,
   2049                                                         // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
   2050                                                         // symbol type.  This is a temporary hack to make sure the ObjectiveC symbols get treated
   2051                                                         // correctly.  To do this right, we should coalesce all the GSYM & global symbols that have the
   2052                                                         // same address.
   2053 
   2054                                                         if (symbol_name && symbol_name[0] == '_' && symbol_name[1] ==  'O'
   2055                                                             && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
   2056                                                                 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
   2057                                                                 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
   2058                                                             add_nlist = false;
   2059                                                         else
   2060                                                         {
   2061                                                             is_gsym = true;
   2062                                                             sym[sym_idx].SetExternal(true);
   2063                                                             if (nlist.n_value != 0)
   2064                                                                 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2065                                                             type = eSymbolTypeData;
   2066                                                         }
   2067                                                         break;
   2068 
   2069                                                     case StabFunctionName:
   2070                                                         // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
   2071                                                         type = eSymbolTypeCompiler;
   2072                                                         break;
   2073 
   2074                                                     case StabFunction:
   2075                                                         // N_FUN -- procedure: name,,n_sect,linenumber,address
   2076                                                         if (symbol_name)
   2077                                                         {
   2078                                                             type = eSymbolTypeCode;
   2079                                                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2080 
   2081                                                             N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
   2082                                                             // We use the current number of symbols in the symbol table in lieu of
   2083                                                             // using nlist_idx in case we ever start trimming entries out
   2084                                                             N_FUN_indexes.push_back(sym_idx);
   2085                                                         }
   2086                                                         else
   2087                                                         {
   2088                                                             type = eSymbolTypeCompiler;
   2089 
   2090                                                             if ( !N_FUN_indexes.empty() )
   2091                                                             {
   2092                                                                 // Copy the size of the function into the original STAB entry so we don't have
   2093                                                                 // to hunt for it later
   2094                                                                 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
   2095                                                                 N_FUN_indexes.pop_back();
   2096                                                                 // We don't really need the end function STAB as it contains the size which
   2097                                                                 // we already placed with the original symbol, so don't add it if we want a
   2098                                                                 // minimal symbol table
   2099                                                                 add_nlist = false;
   2100                                                             }
   2101                                                         }
   2102                                                         break;
   2103 
   2104                                                     case StabStaticSymbol:
   2105                                                         // N_STSYM -- static symbol: name,,n_sect,type,address
   2106                                                         N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
   2107                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2108                                                         type = eSymbolTypeData;
   2109                                                         break;
   2110 
   2111                                                     case StabLocalCommon:
   2112                                                         // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
   2113                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2114                                                         type = eSymbolTypeCommonBlock;
   2115                                                         break;
   2116 
   2117                                                     case StabBeginSymbol:
   2118                                                         // N_BNSYM
   2119                                                         // We use the current number of symbols in the symbol table in lieu of
   2120                                                         // using nlist_idx in case we ever start trimming entries out
   2121                                                         // Skip these if we want minimal symbol tables
   2122                                                         add_nlist = false;
   2123                                                         break;
   2124 
   2125                                                     case StabEndSymbol:
   2126                                                         // N_ENSYM
   2127                                                         // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
   2128                                                         // so that we can always skip the entire symbol if we need to navigate
   2129                                                         // more quickly at the source level when parsing STABS
   2130                                                         // Skip these if we want minimal symbol tables
   2131                                                         add_nlist = false;
   2132                                                         break;
   2133 
   2134 
   2135                                                     case StabSourceFileOptions:
   2136                                                         // N_OPT - emitted with gcc2_compiled and in gcc source
   2137                                                         type = eSymbolTypeCompiler;
   2138                                                         break;
   2139 
   2140                                                     case StabRegisterSymbol:
   2141                                                         // N_RSYM - register sym: name,,NO_SECT,type,register
   2142                                                         type = eSymbolTypeVariable;
   2143                                                         break;
   2144 
   2145                                                     case StabSourceLine:
   2146                                                         // N_SLINE - src line: 0,,n_sect,linenumber,address
   2147                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2148                                                         type = eSymbolTypeLineEntry;
   2149                                                         break;
   2150 
   2151                                                     case StabStructureType:
   2152                                                         // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
   2153                                                         type = eSymbolTypeVariableType;
   2154                                                         break;
   2155 
   2156                                                     case StabSourceFileName:
   2157                                                         // N_SO - source file name
   2158                                                         type = eSymbolTypeSourceFile;
   2159                                                         if (symbol_name == NULL)
   2160                                                         {
   2161                                                             add_nlist = false;
   2162                                                             if (N_SO_index != UINT32_MAX)
   2163                                                             {
   2164                                                                 // Set the size of the N_SO to the terminating index of this N_SO
   2165                                                                 // so that we can always skip the entire N_SO if we need to navigate
   2166                                                                 // more quickly at the source level when parsing STABS
   2167                                                                 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
   2168                                                                 symbol_ptr->SetByteSize(sym_idx);
   2169                                                                 symbol_ptr->SetSizeIsSibling(true);
   2170                                                             }
   2171                                                             N_NSYM_indexes.clear();
   2172                                                             N_INCL_indexes.clear();
   2173                                                             N_BRAC_indexes.clear();
   2174                                                             N_COMM_indexes.clear();
   2175                                                             N_FUN_indexes.clear();
   2176                                                             N_SO_index = UINT32_MAX;
   2177                                                         }
   2178                                                         else
   2179                                                         {
   2180                                                             // We use the current number of symbols in the symbol table in lieu of
   2181                                                             // using nlist_idx in case we ever start trimming entries out
   2182                                                             const bool N_SO_has_full_path = symbol_name[0] == '/';
   2183                                                             if (N_SO_has_full_path)
   2184                                                             {
   2185                                                                 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
   2186                                                                 {
   2187                                                                     // We have two consecutive N_SO entries where the first contains a directory
   2188                                                                     // and the second contains a full path.
   2189                                                                     sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
   2190                                                                     m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
   2191                                                                     add_nlist = false;
   2192                                                                 }
   2193                                                                 else
   2194                                                                 {
   2195                                                                     // This is the first entry in a N_SO that contains a directory or
   2196                                                                     // a full path to the source file
   2197                                                                     N_SO_index = sym_idx;
   2198                                                                 }
   2199                                                             }
   2200                                                             else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
   2201                                                             {
   2202                                                                 // This is usually the second N_SO entry that contains just the filename,
   2203                                                                 // so here we combine it with the first one if we are minimizing the symbol table
   2204                                                                 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
   2205                                                                 if (so_path && so_path[0])
   2206                                                                 {
   2207                                                                     std::string full_so_path (so_path);
   2208                                                                     const size_t double_slash_pos = full_so_path.find("//");
   2209                                                                     if (double_slash_pos != std::string::npos)
   2210                                                                     {
   2211                                                                         // The linker has been generating bad N_SO entries with doubled up paths
   2212                                                                         // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
   2213                                                                         // and the second is the directory for the source file so you end up with
   2214                                                                         // a path that looks like "/tmp/src//tmp/src/"
   2215                                                                         FileSpec so_dir(so_path, false);
   2216                                                                         if (!so_dir.Exists())
   2217                                                                         {
   2218                                                                             so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
   2219                                                                             if (so_dir.Exists())
   2220                                                                             {
   2221                                                                                 // Trim off the incorrect path
   2222                                                                                 full_so_path.erase(0, double_slash_pos + 1);
   2223                                                                             }
   2224                                                                         }
   2225                                                                     }
   2226                                                                     if (*full_so_path.rbegin() != '/')
   2227                                                                         full_so_path += '/';
   2228                                                                     full_so_path += symbol_name;
   2229                                                                     sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
   2230                                                                     add_nlist = false;
   2231                                                                     m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
   2232                                                                 }
   2233                                                             }
   2234                                                             else
   2235                                                             {
   2236                                                                 // This could be a relative path to a N_SO
   2237                                                                 N_SO_index = sym_idx;
   2238                                                             }
   2239                                                         }
   2240                                                         break;
   2241 
   2242                                                     case StabObjectFileName:
   2243                                                         // N_OSO - object file name: name,,0,0,st_mtime
   2244                                                         type = eSymbolTypeObjectFile;
   2245                                                         break;
   2246 
   2247                                                     case StabLocalSymbol:
   2248                                                         // N_LSYM - local sym: name,,NO_SECT,type,offset
   2249                                                         type = eSymbolTypeLocal;
   2250                                                         break;
   2251 
   2252                                                         //----------------------------------------------------------------------
   2253                                                         // INCL scopes
   2254                                                         //----------------------------------------------------------------------
   2255                                                     case StabBeginIncludeFileName:
   2256                                                         // N_BINCL - include file beginning: name,,NO_SECT,0,sum
   2257                                                         // We use the current number of symbols in the symbol table in lieu of
   2258                                                         // using nlist_idx in case we ever start trimming entries out
   2259                                                         N_INCL_indexes.push_back(sym_idx);
   2260                                                         type = eSymbolTypeScopeBegin;
   2261                                                         break;
   2262 
   2263                                                     case StabEndIncludeFile:
   2264                                                         // N_EINCL - include file end: name,,NO_SECT,0,0
   2265                                                         // Set the size of the N_BINCL to the terminating index of this N_EINCL
   2266                                                         // so that we can always skip the entire symbol if we need to navigate
   2267                                                         // more quickly at the source level when parsing STABS
   2268                                                         if ( !N_INCL_indexes.empty() )
   2269                                                         {
   2270                                                             symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
   2271                                                             symbol_ptr->SetByteSize(sym_idx + 1);
   2272                                                             symbol_ptr->SetSizeIsSibling(true);
   2273                                                             N_INCL_indexes.pop_back();
   2274                                                         }
   2275                                                         type = eSymbolTypeScopeEnd;
   2276                                                         break;
   2277 
   2278                                                     case StabIncludeFileName:
   2279                                                         // N_SOL - #included file name: name,,n_sect,0,address
   2280                                                         type = eSymbolTypeHeaderFile;
   2281 
   2282                                                         // We currently don't use the header files on darwin
   2283                                                         add_nlist = false;
   2284                                                         break;
   2285 
   2286                                                     case StabCompilerParameters:
   2287                                                         // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
   2288                                                         type = eSymbolTypeCompiler;
   2289                                                         break;
   2290 
   2291                                                     case StabCompilerVersion:
   2292                                                         // N_VERSION - compiler version: name,,NO_SECT,0,0
   2293                                                         type = eSymbolTypeCompiler;
   2294                                                         break;
   2295 
   2296                                                     case StabCompilerOptLevel:
   2297                                                         // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
   2298                                                         type = eSymbolTypeCompiler;
   2299                                                         break;
   2300 
   2301                                                     case StabParameter:
   2302                                                         // N_PSYM - parameter: name,,NO_SECT,type,offset
   2303                                                         type = eSymbolTypeVariable;
   2304                                                         break;
   2305 
   2306                                                     case StabAlternateEntry:
   2307                                                         // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
   2308                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2309                                                         type = eSymbolTypeLineEntry;
   2310                                                         break;
   2311 
   2312                                                         //----------------------------------------------------------------------
   2313                                                         // Left and Right Braces
   2314                                                         //----------------------------------------------------------------------
   2315                                                     case StabLeftBracket:
   2316                                                         // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
   2317                                                         // We use the current number of symbols in the symbol table in lieu of
   2318                                                         // using nlist_idx in case we ever start trimming entries out
   2319                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2320                                                         N_BRAC_indexes.push_back(sym_idx);
   2321                                                         type = eSymbolTypeScopeBegin;
   2322                                                         break;
   2323 
   2324                                                     case StabRightBracket:
   2325                                                         // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
   2326                                                         // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
   2327                                                         // so that we can always skip the entire symbol if we need to navigate
   2328                                                         // more quickly at the source level when parsing STABS
   2329                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2330                                                         if ( !N_BRAC_indexes.empty() )
   2331                                                         {
   2332                                                             symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
   2333                                                             symbol_ptr->SetByteSize(sym_idx + 1);
   2334                                                             symbol_ptr->SetSizeIsSibling(true);
   2335                                                             N_BRAC_indexes.pop_back();
   2336                                                         }
   2337                                                         type = eSymbolTypeScopeEnd;
   2338                                                         break;
   2339 
   2340                                                     case StabDeletedIncludeFile:
   2341                                                         // N_EXCL - deleted include file: name,,NO_SECT,0,sum
   2342                                                         type = eSymbolTypeHeaderFile;
   2343                                                         break;
   2344 
   2345                                                         //----------------------------------------------------------------------
   2346                                                         // COMM scopes
   2347                                                         //----------------------------------------------------------------------
   2348                                                     case StabBeginCommon:
   2349                                                         // N_BCOMM - begin common: name,,NO_SECT,0,0
   2350                                                         // We use the current number of symbols in the symbol table in lieu of
   2351                                                         // using nlist_idx in case we ever start trimming entries out
   2352                                                         type = eSymbolTypeScopeBegin;
   2353                                                         N_COMM_indexes.push_back(sym_idx);
   2354                                                         break;
   2355 
   2356                                                     case StabEndCommonLocal:
   2357                                                         // N_ECOML - end common (local name): 0,,n_sect,0,address
   2358                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2359                                                         // Fall through
   2360 
   2361                                                     case StabEndCommon:
   2362                                                         // N_ECOMM - end common: name,,n_sect,0,0
   2363                                                         // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
   2364                                                         // so that we can always skip the entire symbol if we need to navigate
   2365                                                         // more quickly at the source level when parsing STABS
   2366                                                         if ( !N_COMM_indexes.empty() )
   2367                                                         {
   2368                                                             symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
   2369                                                             symbol_ptr->SetByteSize(sym_idx + 1);
   2370                                                             symbol_ptr->SetSizeIsSibling(true);
   2371                                                             N_COMM_indexes.pop_back();
   2372                                                         }
   2373                                                         type = eSymbolTypeScopeEnd;
   2374                                                         break;
   2375 
   2376                                                     case StabLength:
   2377                                                         // N_LENG - second stab entry with length information
   2378                                                         type = eSymbolTypeAdditional;
   2379                                                         break;
   2380 
   2381                                                     default: break;
   2382                                                 }
   2383                                             }
   2384                                             else
   2385                                             {
   2386                                                 //uint8_t n_pext    = NlistMaskPrivateExternal & nlist.n_type;
   2387                                                 uint8_t n_type  = NlistMaskType & nlist.n_type;
   2388                                                 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
   2389 
   2390                                                 switch (n_type)
   2391                                                 {
   2392                                                     case NListTypeIndirect:         // N_INDR - Fall through
   2393                                                     case NListTypePreboundUndefined:// N_PBUD - Fall through
   2394                                                     case NListTypeUndefined:        // N_UNDF
   2395                                                         type = eSymbolTypeUndefined;
   2396                                                         break;
   2397 
   2398                                                     case NListTypeAbsolute:         // N_ABS
   2399                                                         type = eSymbolTypeAbsolute;
   2400                                                         break;
   2401 
   2402                                                     case NListTypeSection:          // N_SECT
   2403                                                         {
   2404                                                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2405 
   2406                                                             if (symbol_section == NULL)
   2407                                                             {
   2408                                                                 // TODO: warn about this?
   2409                                                                 add_nlist = false;
   2410                                                                 break;
   2411                                                             }
   2412 
   2413                                                             if (TEXT_eh_frame_sectID == nlist.n_sect)
   2414                                                             {
   2415                                                                 type = eSymbolTypeException;
   2416                                                             }
   2417                                                             else
   2418                                                             {
   2419                                                                 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
   2420 
   2421                                                                 switch (section_type)
   2422                                                                 {
   2423                                                                     case SectionTypeRegular:                     break; // regular section
   2424                                                                                                                         //case SectionTypeZeroFill:                 type = eSymbolTypeData;    break; // zero fill on demand section
   2425                                                                     case SectionTypeCStringLiterals:            type = eSymbolTypeData;    break; // section with only literal C strings
   2426                                                                     case SectionType4ByteLiterals:              type = eSymbolTypeData;    break; // section with only 4 byte literals
   2427                                                                     case SectionType8ByteLiterals:              type = eSymbolTypeData;    break; // section with only 8 byte literals
   2428                                                                     case SectionTypeLiteralPointers:            type = eSymbolTypeTrampoline; break; // section with only pointers to literals
   2429                                                                     case SectionTypeNonLazySymbolPointers:      type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
   2430                                                                     case SectionTypeLazySymbolPointers:         type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
   2431                                                                     case SectionTypeSymbolStubs:                type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
   2432                                                                     case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for initialization
   2433                                                                     case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for termination
   2434                                                                                                                                                   //case SectionTypeCoalesced:                type = eSymbolType;    break; // section contains symbols that are to be coalesced
   2435                                                                                                                                                   //case SectionTypeZeroFillLarge:            type = eSymbolTypeData;    break; // zero fill on demand section (that can be larger than 4 gigabytes)
   2436                                                                     case SectionTypeInterposing:                type = eSymbolTypeTrampoline;  break; // section with only pairs of function pointers for interposing
   2437                                                                     case SectionType16ByteLiterals:             type = eSymbolTypeData;    break; // section with only 16 byte literals
   2438                                                                     case SectionTypeDTraceObjectFormat:         type = eSymbolTypeInstrumentation; break;
   2439                                                                     case SectionTypeLazyDylibSymbolPointers:    type = eSymbolTypeTrampoline; break;
   2440                                                                     default: break;
   2441                                                                 }
   2442 
   2443                                                                 if (type == eSymbolTypeInvalid)
   2444                                                                 {
   2445                                                                     const char *symbol_sect_name = symbol_section->GetName().AsCString();
   2446                                                                     if (symbol_section->IsDescendant (text_section_sp.get()))
   2447                                                                     {
   2448                                                                         if (symbol_section->IsClear(SectionAttrUserPureInstructions |
   2449                                                                                                     SectionAttrUserSelfModifyingCode |
   2450                                                                                                     SectionAttrSytemSomeInstructions))
   2451                                                                             type = eSymbolTypeData;
   2452                                                                         else
   2453                                                                             type = eSymbolTypeCode;
   2454                                                                     }
   2455                                                                     else if (symbol_section->IsDescendant(data_section_sp.get()))
   2456                                                                     {
   2457                                                                         if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
   2458                                                                         {
   2459                                                                             type = eSymbolTypeRuntime;
   2460 
   2461                                                                             if (symbol_name &&
   2462                                                                                 symbol_name[0] == '_' &&
   2463                                                                                 symbol_name[1] == 'O' &&
   2464                                                                                 symbol_name[2] == 'B')
   2465                                                                             {
   2466                                                                                 llvm::StringRef symbol_name_ref(symbol_name);
   2467                                                                                 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
   2468                                                                                 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
   2469                                                                                 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
   2470                                                                                 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
   2471                                                                                 {
   2472                                                                                     symbol_name_non_abi_mangled = symbol_name + 1;
   2473                                                                                     symbol_name = symbol_name + g_objc_v2_prefix_class.size();
   2474                                                                                     type = eSymbolTypeObjCClass;
   2475                                                                                     demangled_is_synthesized = true;
   2476                                                                                 }
   2477                                                                                 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
   2478                                                                                 {
   2479                                                                                     symbol_name_non_abi_mangled = symbol_name + 1;
   2480                                                                                     symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
   2481                                                                                     type = eSymbolTypeObjCMetaClass;
   2482                                                                                     demangled_is_synthesized = true;
   2483                                                                                 }
   2484                                                                                 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
   2485                                                                                 {
   2486                                                                                     symbol_name_non_abi_mangled = symbol_name + 1;
   2487                                                                                     symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
   2488                                                                                     type = eSymbolTypeObjCIVar;
   2489                                                                                     demangled_is_synthesized = true;
   2490                                                                                 }
   2491                                                                             }
   2492                                                                         }
   2493                                                                         else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
   2494                                                                         {
   2495                                                                             type = eSymbolTypeException;
   2496                                                                         }
   2497                                                                         else
   2498                                                                         {
   2499                                                                             type = eSymbolTypeData;
   2500                                                                         }
   2501                                                                     }
   2502                                                                     else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
   2503                                                                     {
   2504                                                                         type = eSymbolTypeTrampoline;
   2505                                                                     }
   2506                                                                     else if (symbol_section->IsDescendant(objc_section_sp.get()))
   2507                                                                     {
   2508                                                                         type = eSymbolTypeRuntime;
   2509                                                                         if (symbol_name && symbol_name[0] == '.')
   2510                                                                         {
   2511                                                                             llvm::StringRef symbol_name_ref(symbol_name);
   2512                                                                             static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
   2513                                                                             if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
   2514                                                                             {
   2515                                                                                 symbol_name_non_abi_mangled = symbol_name;
   2516                                                                                 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
   2517                                                                                 type = eSymbolTypeObjCClass;
   2518                                                                                 demangled_is_synthesized = true;
   2519                                                                             }
   2520                                                                         }
   2521                                                                     }
   2522                                                                 }
   2523                                                             }
   2524                                                         }
   2525                                                         break;
   2526                                                 }
   2527                                             }
   2528 
   2529                                             if (add_nlist)
   2530                                             {
   2531                                                 uint64_t symbol_value = nlist.n_value;
   2532                                                 if (symbol_name_non_abi_mangled)
   2533                                                 {
   2534                                                     sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
   2535                                                     sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
   2536                                                 }
   2537                                                 else
   2538                                                 {
   2539                                                     bool symbol_name_is_mangled = false;
   2540 
   2541                                                     if (symbol_name && symbol_name[0] == '_')
   2542                                                     {
   2543                                                         symbol_name_is_mangled = symbol_name[1] == '_';
   2544                                                         symbol_name++;  // Skip the leading underscore
   2545                                                     }
   2546 
   2547                                                     if (symbol_name)
   2548                                                     {
   2549                                                         ConstString const_symbol_name(symbol_name);
   2550                                                         sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
   2551                                                         if (is_gsym && is_debug)
   2552                                                             N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
   2553                                                     }
   2554                                                 }
   2555                                                 if (symbol_section)
   2556                                                 {
   2557                                                     const addr_t section_file_addr = symbol_section->GetFileAddress();
   2558                                                     if (symbol_byte_size == 0 && function_starts_count > 0)
   2559                                                     {
   2560                                                         addr_t symbol_lookup_file_addr = nlist.n_value;
   2561                                                         // Do an exact address match for non-ARM addresses, else get the closest since
   2562                                                         // the symbol might be a thumb symbol which has an address with bit zero set
   2563                                                         FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
   2564                                                         if (is_arm && func_start_entry)
   2565                                                         {
   2566                                                             // Verify that the function start address is the symbol address (ARM)
   2567                                                             // or the symbol address + 1 (thumb)
   2568                                                             if (func_start_entry->addr != symbol_lookup_file_addr &&
   2569                                                                 func_start_entry->addr != (symbol_lookup_file_addr + 1))
   2570                                                             {
   2571                                                                 // Not the right entry, NULL it out...
   2572                                                                 func_start_entry = NULL;
   2573                                                             }
   2574                                                         }
   2575                                                         if (func_start_entry)
   2576                                                         {
   2577                                                             func_start_entry->data = true;
   2578 
   2579                                                             addr_t symbol_file_addr = func_start_entry->addr;
   2580                                                             uint32_t symbol_flags = 0;
   2581                                                             if (is_arm)
   2582                                                             {
   2583                                                                 if (symbol_file_addr & 1)
   2584                                                                     symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
   2585                                                                 symbol_file_addr &= 0xfffffffffffffffeull;
   2586                                                             }
   2587 
   2588                                                             const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
   2589                                                             const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
   2590                                                             if (next_func_start_entry)
   2591                                                             {
   2592                                                                 addr_t next_symbol_file_addr = next_func_start_entry->addr;
   2593                                                                 // Be sure the clear the Thumb address bit when we calculate the size
   2594                                                                 // from the current and next address
   2595                                                                 if (is_arm)
   2596                                                                     next_symbol_file_addr &= 0xfffffffffffffffeull;
   2597                                                                 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
   2598                                                             }
   2599                                                             else
   2600                                                             {
   2601                                                                 symbol_byte_size = section_end_file_addr - symbol_file_addr;
   2602                                                             }
   2603                                                         }
   2604                                                     }
   2605                                                     symbol_value -= section_file_addr;
   2606                                                 }
   2607 
   2608                                                 if (is_debug == false)
   2609                                                 {
   2610                                                     if (type == eSymbolTypeCode)
   2611                                                     {
   2612                                                         // See if we can find a N_FUN entry for any code symbols.
   2613                                                         // If we do find a match, and the name matches, then we
   2614                                                         // can merge the two into just the function symbol to avoid
   2615                                                         // duplicate entries in the symbol table
   2616                                                         ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
   2617                                                         if (pos != N_FUN_addr_to_sym_idx.end())
   2618                                                         {
   2619                                                             if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
   2620                                                             {
   2621                                                                 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
   2622                                                                 // We just need the flags from the linker symbol, so put these flags
   2623                                                                 // into the N_FUN flags to avoid duplicate symbols in the symbol table
   2624                                                                 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
   2625                                                                 sym[sym_idx].Clear();
   2626                                                                 continue;
   2627                                                             }
   2628                                                         }
   2629                                                     }
   2630                                                     else if (type == eSymbolTypeData)
   2631                                                     {
   2632                                                         // See if we can find a N_STSYM entry for any data symbols.
   2633                                                         // If we do find a match, and the name matches, then we
   2634                                                         // can merge the two into just the Static symbol to avoid
   2635                                                         // duplicate entries in the symbol table
   2636                                                         ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
   2637                                                         if (pos != N_STSYM_addr_to_sym_idx.end())
   2638                                                         {
   2639                                                             if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
   2640                                                             {
   2641                                                                 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
   2642                                                                 // We just need the flags from the linker symbol, so put these flags
   2643                                                                 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
   2644                                                                 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
   2645                                                                 sym[sym_idx].Clear();
   2646                                                                 continue;
   2647                                                             }
   2648                                                         }
   2649                                                         else
   2650                                                         {
   2651                                                             // Combine N_GSYM stab entries with the non stab symbol
   2652                                                             ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
   2653                                                             if (pos != N_GSYM_name_to_sym_idx.end())
   2654                                                             {
   2655                                                                 const uint32_t GSYM_sym_idx = pos->second;
   2656                                                                 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
   2657                                                                 // Copy the address, because often the N_GSYM address has an invalid address of zero
   2658                                                                 // when the global is a common symbol
   2659                                                                 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
   2660                                                                 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
   2661                                                                 // We just need the flags from the linker symbol, so put these flags
   2662                                                                 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
   2663                                                                 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
   2664                                                                 sym[sym_idx].Clear();
   2665                                                                 continue;
   2666                                                             }
   2667                                                         }
   2668                                                     }
   2669                                                 }
   2670 
   2671                                                 sym[sym_idx].SetID (nlist_idx);
   2672                                                 sym[sym_idx].SetType (type);
   2673                                                 sym[sym_idx].GetAddress().SetSection (symbol_section);
   2674                                                 sym[sym_idx].GetAddress().SetOffset (symbol_value);
   2675                                                 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
   2676 
   2677                                                 if (symbol_byte_size > 0)
   2678                                                     sym[sym_idx].SetByteSize(symbol_byte_size);
   2679 
   2680                                                 if (demangled_is_synthesized)
   2681                                                     sym[sym_idx].SetDemangledNameIsSynthesized(true);
   2682                                                 ++sym_idx;
   2683                                             }
   2684                                             else
   2685                                             {
   2686                                                 sym[sym_idx].Clear();
   2687                                             }
   2688 
   2689                                         }
   2690                                         /////////////////////////////
   2691                                     }
   2692                                     break; // No more entries to consider
   2693                                 }
   2694                             }
   2695                         }
   2696                     }
   2697                 }
   2698             }
   2699         }
   2700 
   2701         // Must reset this in case it was mutated above!
   2702         nlist_data_offset = 0;
   2703 #endif
   2704 
   2705         // If the sym array was not created while parsing the DSC unmapped
   2706         // symbols, create it now.
   2707         if (sym == NULL)
   2708         {
   2709             sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
   2710             num_syms = symtab->GetNumSymbols();
   2711         }
   2712 
   2713         if (unmapped_local_symbols_found)
   2714         {
   2715             assert(m_dysymtab.ilocalsym == 0);
   2716             nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
   2717             nlist_idx = m_dysymtab.nlocalsym;
   2718         }
   2719         else
   2720         {
   2721             nlist_idx = 0;
   2722         }
   2723 
   2724         for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
   2725         {
   2726             struct nlist_64 nlist;
   2727             if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
   2728                 break;
   2729 
   2730             nlist.n_strx  = nlist_data.GetU32_unchecked(&nlist_data_offset);
   2731             nlist.n_type  = nlist_data.GetU8_unchecked (&nlist_data_offset);
   2732             nlist.n_sect  = nlist_data.GetU8_unchecked (&nlist_data_offset);
   2733             nlist.n_desc  = nlist_data.GetU16_unchecked (&nlist_data_offset);
   2734             nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
   2735 
   2736             SymbolType type = eSymbolTypeInvalid;
   2737             const char *symbol_name = NULL;
   2738 
   2739             if (have_strtab_data)
   2740             {
   2741                 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
   2742 
   2743                 if (symbol_name == NULL)
   2744                 {
   2745                     // No symbol should be NULL, even the symbols with no
   2746                     // string values should have an offset zero which points
   2747                     // to an empty C-string
   2748                     Host::SystemLog (Host::eSystemLogError,
   2749                                      "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
   2750                                      nlist_idx,
   2751                                      nlist.n_strx,
   2752                                      module_sp->GetFileSpec().GetPath().c_str());
   2753                     continue;
   2754                 }
   2755                 if (symbol_name[0] == '\0')
   2756                     symbol_name = NULL;
   2757             }
   2758             else
   2759             {
   2760                 const addr_t str_addr = strtab_addr + nlist.n_strx;
   2761                 Error str_error;
   2762                 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
   2763                     symbol_name = memory_symbol_name.c_str();
   2764             }
   2765             const char *symbol_name_non_abi_mangled = NULL;
   2766 
   2767             SectionSP symbol_section;
   2768             lldb::addr_t symbol_byte_size = 0;
   2769             bool add_nlist = true;
   2770             bool is_gsym = false;
   2771             bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
   2772             bool demangled_is_synthesized = false;
   2773 
   2774             assert (sym_idx < num_syms);
   2775 
   2776             sym[sym_idx].SetDebug (is_debug);
   2777 
   2778             if (is_debug)
   2779             {
   2780                 switch (nlist.n_type)
   2781                 {
   2782                 case StabGlobalSymbol:
   2783                     // N_GSYM -- global symbol: name,,NO_SECT,type,0
   2784                     // Sometimes the N_GSYM value contains the address.
   2785 
   2786                     // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data.  They
   2787                     // have the same address, but we want to ensure that we always find only the real symbol,
   2788                     // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
   2789                     // symbol type.  This is a temporary hack to make sure the ObjectiveC symbols get treated
   2790                     // correctly.  To do this right, we should coalesce all the GSYM & global symbols that have the
   2791                     // same address.
   2792 
   2793                     if (symbol_name && symbol_name[0] == '_' && symbol_name[1] ==  'O'
   2794                         && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
   2795                             || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
   2796                             || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
   2797                         add_nlist = false;
   2798                     else
   2799                     {
   2800                         is_gsym = true;
   2801                         sym[sym_idx].SetExternal(true);
   2802                         if (nlist.n_value != 0)
   2803                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2804                         type = eSymbolTypeData;
   2805                     }
   2806                     break;
   2807 
   2808                 case StabFunctionName:
   2809                     // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
   2810                     type = eSymbolTypeCompiler;
   2811                     break;
   2812 
   2813                 case StabFunction:
   2814                     // N_FUN -- procedure: name,,n_sect,linenumber,address
   2815                     if (symbol_name)
   2816                     {
   2817                         type = eSymbolTypeCode;
   2818                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2819 
   2820                         N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
   2821                         // We use the current number of symbols in the symbol table in lieu of
   2822                         // using nlist_idx in case we ever start trimming entries out
   2823                         N_FUN_indexes.push_back(sym_idx);
   2824                     }
   2825                     else
   2826                     {
   2827                         type = eSymbolTypeCompiler;
   2828 
   2829                         if ( !N_FUN_indexes.empty() )
   2830                         {
   2831                             // Copy the size of the function into the original STAB entry so we don't have
   2832                             // to hunt for it later
   2833                             symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
   2834                             N_FUN_indexes.pop_back();
   2835                             // We don't really need the end function STAB as it contains the size which
   2836                             // we already placed with the original symbol, so don't add it if we want a
   2837                             // minimal symbol table
   2838                             add_nlist = false;
   2839                         }
   2840                     }
   2841                     break;
   2842 
   2843                 case StabStaticSymbol:
   2844                     // N_STSYM -- static symbol: name,,n_sect,type,address
   2845                     N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
   2846                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2847                     type = eSymbolTypeData;
   2848                     break;
   2849 
   2850                 case StabLocalCommon:
   2851                     // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
   2852                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2853                     type = eSymbolTypeCommonBlock;
   2854                     break;
   2855 
   2856                 case StabBeginSymbol:
   2857                     // N_BNSYM
   2858                     // We use the current number of symbols in the symbol table in lieu of
   2859                     // using nlist_idx in case we ever start trimming entries out
   2860                     // Skip these if we want minimal symbol tables
   2861                     add_nlist = false;
   2862                     break;
   2863 
   2864                 case StabEndSymbol:
   2865                     // N_ENSYM
   2866                     // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
   2867                     // so that we can always skip the entire symbol if we need to navigate
   2868                     // more quickly at the source level when parsing STABS
   2869                     // Skip these if we want minimal symbol tables
   2870                     add_nlist = false;
   2871                     break;
   2872 
   2873 
   2874                 case StabSourceFileOptions:
   2875                     // N_OPT - emitted with gcc2_compiled and in gcc source
   2876                     type = eSymbolTypeCompiler;
   2877                     break;
   2878 
   2879                 case StabRegisterSymbol:
   2880                     // N_RSYM - register sym: name,,NO_SECT,type,register
   2881                     type = eSymbolTypeVariable;
   2882                     break;
   2883 
   2884                 case StabSourceLine:
   2885                     // N_SLINE - src line: 0,,n_sect,linenumber,address
   2886                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   2887                     type = eSymbolTypeLineEntry;
   2888                     break;
   2889 
   2890                 case StabStructureType:
   2891                     // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
   2892                     type = eSymbolTypeVariableType;
   2893                     break;
   2894 
   2895                 case StabSourceFileName:
   2896                     // N_SO - source file name
   2897                     type = eSymbolTypeSourceFile;
   2898                     if (symbol_name == NULL)
   2899                     {
   2900                         add_nlist = false;
   2901                         if (N_SO_index != UINT32_MAX)
   2902                         {
   2903                             // Set the size of the N_SO to the terminating index of this N_SO
   2904                             // so that we can always skip the entire N_SO if we need to navigate
   2905                             // more quickly at the source level when parsing STABS
   2906                             symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
   2907                             symbol_ptr->SetByteSize(sym_idx);
   2908                             symbol_ptr->SetSizeIsSibling(true);
   2909                         }
   2910                         N_NSYM_indexes.clear();
   2911                         N_INCL_indexes.clear();
   2912                         N_BRAC_indexes.clear();
   2913                         N_COMM_indexes.clear();
   2914                         N_FUN_indexes.clear();
   2915                         N_SO_index = UINT32_MAX;
   2916                     }
   2917                     else
   2918                     {
   2919                         // We use the current number of symbols in the symbol table in lieu of
   2920                         // using nlist_idx in case we ever start trimming entries out
   2921                         const bool N_SO_has_full_path = symbol_name[0] == '/';
   2922                         if (N_SO_has_full_path)
   2923                         {
   2924                             if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
   2925                             {
   2926                                 // We have two consecutive N_SO entries where the first contains a directory
   2927                                 // and the second contains a full path.
   2928                                 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
   2929                                 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
   2930                                 add_nlist = false;
   2931                             }
   2932                             else
   2933                             {
   2934                                 // This is the first entry in a N_SO that contains a directory or
   2935                                 // a full path to the source file
   2936                                 N_SO_index = sym_idx;
   2937                             }
   2938                         }
   2939                         else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
   2940                         {
   2941                             // This is usually the second N_SO entry that contains just the filename,
   2942                             // so here we combine it with the first one if we are minimizing the symbol table
   2943                             const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
   2944                             if (so_path && so_path[0])
   2945                             {
   2946                                 std::string full_so_path (so_path);
   2947                                 const size_t double_slash_pos = full_so_path.find("//");
   2948                                 if (double_slash_pos != std::string::npos)
   2949                                 {
   2950                                     // The linker has been generating bad N_SO entries with doubled up paths
   2951                                     // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
   2952                                     // and the second is the directory for the source file so you end up with
   2953                                     // a path that looks like "/tmp/src//tmp/src/"
   2954                                     FileSpec so_dir(so_path, false);
   2955                                     if (!so_dir.Exists())
   2956                                     {
   2957                                         so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
   2958                                         if (so_dir.Exists())
   2959                                         {
   2960                                             // Trim off the incorrect path
   2961                                             full_so_path.erase(0, double_slash_pos + 1);
   2962                                         }
   2963                                     }
   2964                                 }
   2965                                 if (*full_so_path.rbegin() != '/')
   2966                                     full_so_path += '/';
   2967                                 full_so_path += symbol_name;
   2968                                 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
   2969                                 add_nlist = false;
   2970                                 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
   2971                             }
   2972                         }
   2973                         else
   2974                         {
   2975                             // This could be a relative path to a N_SO
   2976                             N_SO_index = sym_idx;
   2977                         }
   2978                     }
   2979 
   2980                     break;
   2981 
   2982                 case StabObjectFileName:
   2983                     // N_OSO - object file name: name,,0,0,st_mtime
   2984                     type = eSymbolTypeObjectFile;
   2985                     break;
   2986 
   2987                 case StabLocalSymbol:
   2988                     // N_LSYM - local sym: name,,NO_SECT,type,offset
   2989                     type = eSymbolTypeLocal;
   2990                     break;
   2991 
   2992                 //----------------------------------------------------------------------
   2993                 // INCL scopes
   2994                 //----------------------------------------------------------------------
   2995                 case StabBeginIncludeFileName:
   2996                     // N_BINCL - include file beginning: name,,NO_SECT,0,sum
   2997                     // We use the current number of symbols in the symbol table in lieu of
   2998                     // using nlist_idx in case we ever start trimming entries out
   2999                     N_INCL_indexes.push_back(sym_idx);
   3000                     type = eSymbolTypeScopeBegin;
   3001                     break;
   3002 
   3003                 case StabEndIncludeFile:
   3004                     // N_EINCL - include file end: name,,NO_SECT,0,0
   3005                     // Set the size of the N_BINCL to the terminating index of this N_EINCL
   3006                     // so that we can always skip the entire symbol if we need to navigate
   3007                     // more quickly at the source level when parsing STABS
   3008                     if ( !N_INCL_indexes.empty() )
   3009                     {
   3010                         symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
   3011                         symbol_ptr->SetByteSize(sym_idx + 1);
   3012                         symbol_ptr->SetSizeIsSibling(true);
   3013                         N_INCL_indexes.pop_back();
   3014                     }
   3015                     type = eSymbolTypeScopeEnd;
   3016                     break;
   3017 
   3018                 case StabIncludeFileName:
   3019                     // N_SOL - #included file name: name,,n_sect,0,address
   3020                     type = eSymbolTypeHeaderFile;
   3021 
   3022                     // We currently don't use the header files on darwin
   3023                     add_nlist = false;
   3024                     break;
   3025 
   3026                 case StabCompilerParameters:
   3027                     // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
   3028                     type = eSymbolTypeCompiler;
   3029                     break;
   3030 
   3031                 case StabCompilerVersion:
   3032                     // N_VERSION - compiler version: name,,NO_SECT,0,0
   3033                     type = eSymbolTypeCompiler;
   3034                     break;
   3035 
   3036                 case StabCompilerOptLevel:
   3037                     // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
   3038                     type = eSymbolTypeCompiler;
   3039                     break;
   3040 
   3041                 case StabParameter:
   3042                     // N_PSYM - parameter: name,,NO_SECT,type,offset
   3043                     type = eSymbolTypeVariable;
   3044                     break;
   3045 
   3046                 case StabAlternateEntry:
   3047                     // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
   3048                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   3049                     type = eSymbolTypeLineEntry;
   3050                     break;
   3051 
   3052                 //----------------------------------------------------------------------
   3053                 // Left and Right Braces
   3054                 //----------------------------------------------------------------------
   3055                 case StabLeftBracket:
   3056                     // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
   3057                     // We use the current number of symbols in the symbol table in lieu of
   3058                     // using nlist_idx in case we ever start trimming entries out
   3059                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   3060                     N_BRAC_indexes.push_back(sym_idx);
   3061                     type = eSymbolTypeScopeBegin;
   3062                     break;
   3063 
   3064                 case StabRightBracket:
   3065                     // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
   3066                     // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
   3067                     // so that we can always skip the entire symbol if we need to navigate
   3068                     // more quickly at the source level when parsing STABS
   3069                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   3070                     if ( !N_BRAC_indexes.empty() )
   3071                     {
   3072                         symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
   3073                         symbol_ptr->SetByteSize(sym_idx + 1);
   3074                         symbol_ptr->SetSizeIsSibling(true);
   3075                         N_BRAC_indexes.pop_back();
   3076                     }
   3077                     type = eSymbolTypeScopeEnd;
   3078                     break;
   3079 
   3080                 case StabDeletedIncludeFile:
   3081                     // N_EXCL - deleted include file: name,,NO_SECT,0,sum
   3082                     type = eSymbolTypeHeaderFile;
   3083                     break;
   3084 
   3085                 //----------------------------------------------------------------------
   3086                 // COMM scopes
   3087                 //----------------------------------------------------------------------
   3088                 case StabBeginCommon:
   3089                     // N_BCOMM - begin common: name,,NO_SECT,0,0
   3090                     // We use the current number of symbols in the symbol table in lieu of
   3091                     // using nlist_idx in case we ever start trimming entries out
   3092                     type = eSymbolTypeScopeBegin;
   3093                     N_COMM_indexes.push_back(sym_idx);
   3094                     break;
   3095 
   3096                 case StabEndCommonLocal:
   3097                     // N_ECOML - end common (local name): 0,,n_sect,0,address
   3098                     symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   3099                     // Fall through
   3100 
   3101                 case StabEndCommon:
   3102                     // N_ECOMM - end common: name,,n_sect,0,0
   3103                     // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
   3104                     // so that we can always skip the entire symbol if we need to navigate
   3105                     // more quickly at the source level when parsing STABS
   3106                     if ( !N_COMM_indexes.empty() )
   3107                     {
   3108                         symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
   3109                         symbol_ptr->SetByteSize(sym_idx + 1);
   3110                         symbol_ptr->SetSizeIsSibling(true);
   3111                         N_COMM_indexes.pop_back();
   3112                     }
   3113                     type = eSymbolTypeScopeEnd;
   3114                     break;
   3115 
   3116                 case StabLength:
   3117                     // N_LENG - second stab entry with length information
   3118                     type = eSymbolTypeAdditional;
   3119                     break;
   3120 
   3121                 default: break;
   3122                 }
   3123             }
   3124             else
   3125             {
   3126                 //uint8_t n_pext    = NlistMaskPrivateExternal & nlist.n_type;
   3127                 uint8_t n_type  = NlistMaskType & nlist.n_type;
   3128                 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
   3129 
   3130                 switch (n_type)
   3131                 {
   3132                 case NListTypeIndirect:         // N_INDR - Fall through
   3133                 case NListTypePreboundUndefined:// N_PBUD - Fall through
   3134                 case NListTypeUndefined:        // N_UNDF
   3135                     type = eSymbolTypeUndefined;
   3136                     break;
   3137 
   3138                 case NListTypeAbsolute:         // N_ABS
   3139                     type = eSymbolTypeAbsolute;
   3140                     break;
   3141 
   3142                 case NListTypeSection:          // N_SECT
   3143                     {
   3144                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
   3145 
   3146                         if (!symbol_section)
   3147                         {
   3148                             // TODO: warn about this?
   3149                             add_nlist = false;
   3150                             break;
   3151                         }
   3152 
   3153                         if (TEXT_eh_frame_sectID == nlist.n_sect)
   3154                         {
   3155                             type = eSymbolTypeException;
   3156                         }
   3157                         else
   3158                         {
   3159                             uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
   3160 
   3161                             switch (section_type)
   3162                             {
   3163                             case SectionTypeRegular:                     break; // regular section
   3164                             //case SectionTypeZeroFill:                 type = eSymbolTypeData;    break; // zero fill on demand section
   3165                             case SectionTypeCStringLiterals:            type = eSymbolTypeData;    break; // section with only literal C strings
   3166                             case SectionType4ByteLiterals:              type = eSymbolTypeData;    break; // section with only 4 byte literals
   3167                             case SectionType8ByteLiterals:              type = eSymbolTypeData;    break; // section with only 8 byte literals
   3168                             case SectionTypeLiteralPointers:            type = eSymbolTypeTrampoline; break; // section with only pointers to literals
   3169                             case SectionTypeNonLazySymbolPointers:      type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
   3170                             case SectionTypeLazySymbolPointers:         type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
   3171                             case SectionTypeSymbolStubs:                type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
   3172                             case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for initialization
   3173                             case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode;    break; // section with only function pointers for termination
   3174                             //case SectionTypeCoalesced:                type = eSymbolType;    break; // section contains symbols that are to be coalesced
   3175                             //case SectionTypeZeroFillLarge:            type = eSymbolTypeData;    break; // zero fill on demand section (that can be larger than 4 gigabytes)
   3176                             case SectionTypeInterposing:                type = eSymbolTypeTrampoline;  break; // section with only pairs of function pointers for interposing
   3177                             case SectionType16ByteLiterals:             type = eSymbolTypeData;    break; // section with only 16 byte literals
   3178                             case SectionTypeDTraceObjectFormat:         type = eSymbolTypeInstrumentation; break;
   3179                             case SectionTypeLazyDylibSymbolPointers:    type = eSymbolTypeTrampoline; break;
   3180                             default: break;
   3181                             }
   3182 
   3183                             if (type == eSymbolTypeInvalid)
   3184                             {
   3185                                 const char *symbol_sect_name = symbol_section->GetName().AsCString();
   3186                                 if (symbol_section->IsDescendant (text_section_sp.get()))
   3187                                 {
   3188                                     if (symbol_section->IsClear(SectionAttrUserPureInstructions |
   3189                                                                 SectionAttrUserSelfModifyingCode |
   3190                                                                 SectionAttrSytemSomeInstructions))
   3191                                         type = eSymbolTypeData;
   3192                                     else
   3193                                         type = eSymbolTypeCode;
   3194                                 }
   3195                                 else
   3196                                 if (symbol_section->IsDescendant(data_section_sp.get()))
   3197                                 {
   3198                                     if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
   3199                                     {
   3200                                         type = eSymbolTypeRuntime;
   3201 
   3202                                         if (symbol_name &&
   3203                                             symbol_name[0] == '_' &&
   3204                                             symbol_name[1] == 'O' &&
   3205                                             symbol_name[2] == 'B')
   3206                                         {
   3207                                             llvm::StringRef symbol_name_ref(symbol_name);
   3208                                             static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
   3209                                             static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
   3210                                             static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
   3211                                             if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
   3212                                             {
   3213                                                 symbol_name_non_abi_mangled = symbol_name + 1;
   3214                                                 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
   3215                                                 type = eSymbolTypeObjCClass;
   3216                                                 demangled_is_synthesized = true;
   3217                                             }
   3218                                             else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
   3219                                             {
   3220                                                 symbol_name_non_abi_mangled = symbol_name + 1;
   3221                                                 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
   3222                                                 type = eSymbolTypeObjCMetaClass;
   3223                                                 demangled_is_synthesized = true;
   3224                                             }
   3225                                             else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
   3226                                             {
   3227                                                 symbol_name_non_abi_mangled = symbol_name + 1;
   3228                                                 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
   3229                                                 type = eSymbolTypeObjCIVar;
   3230                                                 demangled_is_synthesized = true;
   3231                                             }
   3232                                         }
   3233                                     }
   3234                                     else
   3235                                     if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
   3236                                     {
   3237                                         type = eSymbolTypeException;
   3238                                     }
   3239                                     else
   3240                                     {
   3241                                         type = eSymbolTypeData;
   3242                                     }
   3243                                 }
   3244                                 else
   3245                                 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
   3246                                 {
   3247                                     type = eSymbolTypeTrampoline;
   3248                                 }
   3249                                 else
   3250                                 if (symbol_section->IsDescendant(objc_section_sp.get()))
   3251                                 {
   3252                                     type = eSymbolTypeRuntime;
   3253                                     if (symbol_name && symbol_name[0] == '.')
   3254                                     {
   3255                                         llvm::StringRef symbol_name_ref(symbol_name);
   3256                                         static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
   3257                                         if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
   3258                                         {
   3259                                             symbol_name_non_abi_mangled = symbol_name;
   3260                                             symbol_name = symbol_name + g_objc_v1_prefix_class.size();
   3261                                             type = eSymbolTypeObjCClass;
   3262                                             demangled_is_synthesized = true;
   3263                                         }
   3264                                     }
   3265                                 }
   3266                             }
   3267                         }
   3268                     }
   3269                     break;
   3270                 }
   3271             }
   3272 
   3273             if (add_nlist)
   3274             {
   3275                 uint64_t symbol_value = nlist.n_value;
   3276 
   3277                 if (symbol_name_non_abi_mangled)
   3278                 {
   3279                     sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
   3280                     sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
   3281                 }
   3282                 else
   3283                 {
   3284                     bool symbol_name_is_mangled = false;
   3285 
   3286                     if (symbol_name && symbol_name[0] == '_')
   3287                     {
   3288                         symbol_name_is_mangled = symbol_name[1] == '_';
   3289                         symbol_name++;  // Skip the leading underscore
   3290                     }
   3291 
   3292                     if (symbol_name)
   3293                     {
   3294                         ConstString const_symbol_name(symbol_name);
   3295                         sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
   3296                         if (is_gsym && is_debug)
   3297                         {
   3298                             N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
   3299                         }
   3300                     }
   3301                 }
   3302                 if (symbol_section)
   3303                 {
   3304                     const addr_t section_file_addr = symbol_section->GetFileAddress();
   3305                     if (symbol_byte_size == 0 && function_starts_count > 0)
   3306                     {
   3307                         addr_t symbol_lookup_file_addr = nlist.n_value;
   3308                         // Do an exact address match for non-ARM addresses, else get the closest since
   3309                         // the symbol might be a thumb symbol which has an address with bit zero set
   3310                         FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
   3311                         if (is_arm && func_start_entry)
   3312                         {
   3313                             // Verify that the function start address is the symbol address (ARM)
   3314                             // or the symbol address + 1 (thumb)
   3315                             if (func_start_entry->addr != symbol_lookup_file_addr &&
   3316                                 func_start_entry->addr != (symbol_lookup_file_addr + 1))
   3317                             {
   3318                                 // Not the right entry, NULL it out...
   3319                                 func_start_entry = NULL;
   3320                             }
   3321                         }
   3322                         if (func_start_entry)
   3323                         {
   3324                             func_start_entry->data = true;
   3325 
   3326                             addr_t symbol_file_addr = func_start_entry->addr;
   3327                             if (is_arm)
   3328                                 symbol_file_addr &= 0xfffffffffffffffeull;
   3329 
   3330                             const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
   3331                             const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
   3332                             if (next_func_start_entry)
   3333                             {
   3334                                 addr_t next_symbol_file_addr = next_func_start_entry->addr;
   3335                                 // Be sure the clear the Thumb address bit when we calculate the size
   3336                                 // from the current and next address
   3337                                 if (is_arm)
   3338                                     next_symbol_file_addr &= 0xfffffffffffffffeull;
   3339                                 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
   3340                             }
   3341                             else
   3342                             {
   3343                                 symbol_byte_size = section_end_file_addr - symbol_file_addr;
   3344                             }
   3345                         }
   3346                     }
   3347                     symbol_value -= section_file_addr;
   3348                 }
   3349 
   3350                 if (is_debug == false)
   3351                 {
   3352                     if (type == eSymbolTypeCode)
   3353                     {
   3354                         // See if we can find a N_FUN entry for any code symbols.
   3355                         // If we do find a match, and the name matches, then we
   3356                         // can merge the two into just the function symbol to avoid
   3357                         // duplicate entries in the symbol table
   3358                         ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
   3359                         if (pos != N_FUN_addr_to_sym_idx.end())
   3360                         {
   3361                             if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
   3362                             {
   3363                                 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
   3364                                 // We just need the flags from the linker symbol, so put these flags
   3365                                 // into the N_FUN flags to avoid duplicate symbols in the symbol table
   3366                                 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
   3367                                 sym[sym_idx].Clear();
   3368                                 continue;
   3369                             }
   3370                         }
   3371                     }
   3372                     else if (type == eSymbolTypeData)
   3373                     {
   3374                         // See if we can find a N_STSYM entry for any data symbols.
   3375                         // If we do find a match, and the name matches, then we
   3376                         // can merge the two into just the Static symbol to avoid
   3377                         // duplicate entries in the symbol table
   3378                         ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
   3379                         if (pos != N_STSYM_addr_to_sym_idx.end())
   3380                         {
   3381                             if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
   3382                             {
   3383                                 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
   3384                                 // We just need the flags from the linker symbol, so put these flags
   3385                                 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
   3386                                 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
   3387                                 sym[sym_idx].Clear();
   3388                                 continue;
   3389                             }
   3390                         }
   3391                         else
   3392                         {
   3393                             // Combine N_GSYM stab entries with the non stab symbol
   3394                             ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
   3395                             if (pos != N_GSYM_name_to_sym_idx.end())
   3396                             {
   3397                                 const uint32_t GSYM_sym_idx = pos->second;
   3398                                 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
   3399                                 // Copy the address, because often the N_GSYM address has an invalid address of zero
   3400                                 // when the global is a common symbol
   3401                                 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
   3402                                 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
   3403                                 // We just need the flags from the linker symbol, so put these flags
   3404                                 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
   3405                                 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
   3406                                 sym[sym_idx].Clear();
   3407                                 continue;
   3408                             }
   3409                         }
   3410                     }
   3411                 }
   3412 
   3413                 sym[sym_idx].SetID (nlist_idx);
   3414                 sym[sym_idx].SetType (type);
   3415                 sym[sym_idx].GetAddress().SetSection (symbol_section);
   3416                 sym[sym_idx].GetAddress().SetOffset (symbol_value);
   3417                 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
   3418 
   3419                 if (symbol_byte_size > 0)
   3420                     sym[sym_idx].SetByteSize(symbol_byte_size);
   3421 
   3422                 if (demangled_is_synthesized)
   3423                     sym[sym_idx].SetDemangledNameIsSynthesized(true);
   3424 
   3425                 ++sym_idx;
   3426             }
   3427             else
   3428             {
   3429                 sym[sym_idx].Clear();
   3430             }
   3431 
   3432         }
   3433 
   3434         // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
   3435         // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
   3436         // such entries by figuring out what the address for the global is by looking up this non-STAB
   3437         // entry and copying the value into the debug symbol's value to save us the hassle in the
   3438         // debug symbol parser.
   3439 
   3440         Symbol *global_symbol = NULL;
   3441         for (nlist_idx = 0;
   3442              nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
   3443              nlist_idx++)
   3444         {
   3445             if (global_symbol->GetAddress().GetFileAddress() == 0)
   3446             {
   3447                 std::vector<uint32_t> indexes;
   3448                 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
   3449                 {
   3450                     std::vector<uint32_t>::const_iterator pos;
   3451                     std::vector<uint32_t>::const_iterator end = indexes.end();
   3452                     for (pos = indexes.begin(); pos != end; ++pos)
   3453                     {
   3454                         symbol_ptr = symtab->SymbolAtIndex(*pos);
   3455                         if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
   3456                         {
   3457                             global_symbol->GetAddress() = symbol_ptr->GetAddress();
   3458                             break;
   3459                         }
   3460                     }
   3461                 }
   3462             }
   3463         }
   3464 
   3465         uint32_t synthetic_sym_id = symtab_load_command.nsyms;
   3466 
   3467         if (function_starts_count > 0)
   3468         {
   3469             char synthetic_function_symbol[PATH_MAX];
   3470             uint32_t num_synthetic_function_symbols = 0;
   3471             for (i=0; i<function_starts_count; ++i)
   3472             {
   3473                 if (function_starts.GetEntryRef (i).data == false)
   3474                     ++num_synthetic_function_symbols;
   3475             }
   3476 
   3477             if (num_synthetic_function_symbols > 0)
   3478             {
   3479                 if (num_syms < sym_idx + num_synthetic_function_symbols)
   3480                 {
   3481                     num_syms = sym_idx + num_synthetic_function_symbols;
   3482                     sym = symtab->Resize (num_syms);
   3483                 }
   3484                 uint32_t synthetic_function_symbol_idx = 0;
   3485                 for (i=0; i<function_starts_count; ++i)
   3486                 {
   3487                     const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
   3488                     if (func_start_entry->data == false)
   3489                     {
   3490                         addr_t symbol_file_addr = func_start_entry->addr;
   3491                         uint32_t symbol_flags = 0;
   3492                         if (is_arm)
   3493                         {
   3494                             if (symbol_file_addr & 1)
   3495                                 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
   3496                             symbol_file_addr &= 0xfffffffffffffffeull;
   3497                         }
   3498                         Address symbol_addr;
   3499                         if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
   3500                         {
   3501                             SectionSP symbol_section (symbol_addr.GetSection());
   3502                             uint32_t symbol_byte_size = 0;
   3503                             if (symbol_section)
   3504                             {
   3505                                 const addr_t section_file_addr = symbol_section->GetFileAddress();
   3506                                 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
   3507                                 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
   3508                                 if (next_func_start_entry)
   3509                                 {
   3510                                     addr_t next_symbol_file_addr = next_func_start_entry->addr;
   3511                                     if (is_arm)
   3512                                         next_symbol_file_addr &= 0xfffffffffffffffeull;
   3513                                     symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
   3514                                 }
   3515                                 else
   3516                                 {
   3517                                     symbol_byte_size = section_end_file_addr - symbol_file_addr;
   3518                                 }
   3519                                 snprintf (synthetic_function_symbol,
   3520                                           sizeof(synthetic_function_symbol),
   3521                                           "___lldb_unnamed_function%u$$%s",
   3522                                           ++synthetic_function_symbol_idx,
   3523                                           module_sp->GetFileSpec().GetFilename().GetCString());
   3524                                 sym[sym_idx].SetID (synthetic_sym_id++);
   3525                                 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
   3526                                 sym[sym_idx].SetType (eSymbolTypeCode);
   3527                                 sym[sym_idx].SetIsSynthetic (true);
   3528                                 sym[sym_idx].GetAddress() = symbol_addr;
   3529                                 if (symbol_flags)
   3530                                     sym[sym_idx].SetFlags (symbol_flags);
   3531                                 if (symbol_byte_size)
   3532                                     sym[sym_idx].SetByteSize (symbol_byte_size);
   3533                                 ++sym_idx;
   3534                             }
   3535                         }
   3536                     }
   3537                 }
   3538             }
   3539         }
   3540 
   3541         // Trim our symbols down to just what we ended up with after
   3542         // removing any symbols.
   3543         if (sym_idx < num_syms)
   3544         {
   3545             num_syms = sym_idx;
   3546             sym = symtab->Resize (num_syms);
   3547         }
   3548 
   3549         // Now synthesize indirect symbols
   3550         if (m_dysymtab.nindirectsyms != 0)
   3551         {
   3552             if (indirect_symbol_index_data.GetByteSize())
   3553             {
   3554                 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
   3555 
   3556                 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
   3557                 {
   3558                     if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
   3559                     {
   3560                         uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
   3561                         if (symbol_stub_byte_size == 0)
   3562                             continue;
   3563 
   3564                         const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
   3565 
   3566                         if (num_symbol_stubs == 0)
   3567                             continue;
   3568 
   3569                         const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
   3570                         for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
   3571                         {
   3572                             const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
   3573                             const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
   3574                             lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
   3575                             if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
   3576                             {
   3577                                 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
   3578                                 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
   3579                                     continue;
   3580 
   3581                                 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
   3582                                 Symbol *stub_symbol = NULL;
   3583                                 if (index_pos != end_index_pos)
   3584                                 {
   3585                                     // We have a remapping from the original nlist index to
   3586                                     // a current symbol index, so just look this up by index
   3587                                     stub_symbol = symtab->SymbolAtIndex (index_pos->second);
   3588                                 }
   3589                                 else
   3590                                 {
   3591                                     // We need to lookup a symbol using the original nlist
   3592                                     // symbol index since this index is coming from the
   3593                                     // S_SYMBOL_STUBS
   3594                                     stub_symbol = symtab->FindSymbolByID (stub_sym_id);
   3595                                 }
   3596 
   3597                                 if (stub_symbol)
   3598                                 {
   3599                                     Address so_addr(symbol_stub_addr, section_list);
   3600 
   3601                                     if (stub_symbol->GetType() == eSymbolTypeUndefined)
   3602                                     {
   3603                                         // Change the external symbol into a trampoline that makes sense
   3604                                         // These symbols were N_UNDF N_EXT, and are useless to us, so we
   3605                                         // can re-use them so we don't have to make up a synthetic symbol
   3606                                         // for no good reason.
   3607                                         stub_symbol->SetType (eSymbolTypeTrampoline);
   3608                                         stub_symbol->SetExternal (false);
   3609                                         stub_symbol->GetAddress() = so_addr;
   3610                                         stub_symbol->SetByteSize (symbol_stub_byte_size);
   3611                                     }
   3612                                     else
   3613                                     {
   3614                                         // Make a synthetic symbol to describe the trampoline stub
   3615                                         Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
   3616                                         if (sym_idx >= num_syms)
   3617                                         {
   3618                                             sym = symtab->Resize (++num_syms);
   3619                                             stub_symbol = NULL;  // this pointer no longer valid
   3620                                         }
   3621                                         sym[sym_idx].SetID (synthetic_sym_id++);
   3622                                         sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
   3623                                         sym[sym_idx].SetType (eSymbolTypeTrampoline);
   3624                                         sym[sym_idx].SetIsSynthetic (true);
   3625                                         sym[sym_idx].GetAddress() = so_addr;
   3626                                         sym[sym_idx].SetByteSize (symbol_stub_byte_size);
   3627                                         ++sym_idx;
   3628                                     }
   3629                                 }
   3630                                 else
   3631                                 {
   3632                                     if (log)
   3633                                         log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
   3634                                 }
   3635                             }
   3636                         }
   3637                     }
   3638                 }
   3639             }
   3640         }
   3641 
   3642 //        StreamFile s(stdout, false);
   3643 //        s.Printf ("Symbol table before CalculateSymbolSizes():\n");
   3644 //        symtab->Dump(&s, NULL, eSortOrderNone);
   3645         // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
   3646         symtab->CalculateSymbolSizes();
   3647 
   3648 //        s.Printf ("Symbol table after CalculateSymbolSizes():\n");
   3649 //        symtab->Dump(&s, NULL, eSortOrderNone);
   3650 
   3651         return symtab->GetNumSymbols();
   3652     }
   3653     return 0;
   3654 }
   3655 
   3656 
   3657 void
   3658 ObjectFileMachO::Dump (Stream *s)
   3659 {
   3660     ModuleSP module_sp(GetModule());
   3661     if (module_sp)
   3662     {
   3663         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
   3664         s->Printf("%p: ", this);
   3665         s->Indent();
   3666         if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
   3667             s->PutCString("ObjectFileMachO64");
   3668         else
   3669             s->PutCString("ObjectFileMachO32");
   3670 
   3671         ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
   3672 
   3673         *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
   3674 
   3675         SectionList *sections = GetSectionList();
   3676         if (sections)
   3677             sections->Dump(s, NULL, true, UINT32_MAX);
   3678 
   3679         if (m_symtab_ap.get())
   3680             m_symtab_ap->Dump(s, NULL, eSortOrderNone);
   3681     }
   3682 }
   3683 
   3684 bool
   3685 ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
   3686                           const lldb_private::DataExtractor &data,
   3687                           lldb::offset_t lc_offset,
   3688                           lldb_private::UUID& uuid)
   3689 {
   3690     uint32_t i;
   3691     struct uuid_command load_cmd;
   3692 
   3693     lldb::offset_t offset = lc_offset;
   3694     for (i=0; i<header.ncmds; ++i)
   3695     {
   3696         const lldb::offset_t cmd_offset = offset;
   3697         if (data.GetU32(&offset, &load_cmd, 2) == NULL)
   3698             break;
   3699 
   3700         if (load_cmd.cmd == LoadCommandUUID)
   3701         {
   3702             const uint8_t *uuid_bytes = data.PeekData(offset, 16);
   3703 
   3704             if (uuid_bytes)
   3705             {
   3706                 // OpenCL on Mac OS X uses the same UUID for each of its object files.
   3707                 // We pretend these object files have no UUID to prevent crashing.
   3708 
   3709                 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
   3710                     0x3b, 0xa8,
   3711                     0x4b, 0x16,
   3712                     0xb6, 0xa4,
   3713                     0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
   3714 
   3715                 if (!memcmp(uuid_bytes, opencl_uuid, 16))
   3716                     return false;
   3717 
   3718                 uuid.SetBytes (uuid_bytes);
   3719                 return true;
   3720             }
   3721             return false;
   3722         }
   3723         offset = cmd_offset + load_cmd.cmdsize;
   3724     }
   3725     return false;
   3726 }
   3727 
   3728 bool
   3729 ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
   3730 {
   3731     ModuleSP module_sp(GetModule());
   3732     if (module_sp)
   3733     {
   3734         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
   3735         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
   3736         return GetUUID (m_header, m_data, offset, *uuid);
   3737     }
   3738     return false;
   3739 }
   3740 
   3741 
   3742 uint32_t
   3743 ObjectFileMachO::GetDependentModules (FileSpecList& files)
   3744 {
   3745     uint32_t count = 0;
   3746     ModuleSP module_sp(GetModule());
   3747     if (module_sp)
   3748     {
   3749         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
   3750         struct load_command load_cmd;
   3751         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
   3752         const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
   3753         uint32_t i;
   3754         for (i=0; i<m_header.ncmds; ++i)
   3755         {
   3756             const uint32_t cmd_offset = offset;
   3757             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
   3758                 break;
   3759 
   3760             switch (load_cmd.cmd)
   3761             {
   3762             case LoadCommandDylibLoad:
   3763             case LoadCommandDylibLoadWeak:
   3764             case LoadCommandDylibReexport:
   3765             case LoadCommandDynamicLinkerLoad:
   3766             case LoadCommandFixedVMShlibLoad:
   3767             case LoadCommandDylibLoadUpward:
   3768                 {
   3769                     uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
   3770                     const char *path = m_data.PeekCStr(name_offset);
   3771                     // Skip any path that starts with '@' since these are usually:
   3772                     // @executable_path/.../file
   3773                     // @rpath/.../file
   3774                     if (path && path[0] != '@')
   3775                     {
   3776                         FileSpec file_spec(path, resolve_path);
   3777                         if (files.AppendIfUnique(file_spec))
   3778                             count++;
   3779                     }
   3780                 }
   3781                 break;
   3782 
   3783             default:
   3784                 break;
   3785             }
   3786             offset = cmd_offset + load_cmd.cmdsize;
   3787         }
   3788     }
   3789     return count;
   3790 }
   3791 
   3792 lldb_private::Address
   3793 ObjectFileMachO::GetEntryPointAddress ()
   3794 {
   3795     // If the object file is not an executable it can't hold the entry point.  m_entry_point_address
   3796     // is initialized to an invalid address, so we can just return that.
   3797     // If m_entry_point_address is valid it means we've found it already, so return the cached value.
   3798 
   3799     if (!IsExecutable() || m_entry_point_address.IsValid())
   3800         return m_entry_point_address;
   3801 
   3802     // Otherwise, look for the UnixThread or Thread command.  The data for the Thread command is given in
   3803     // /usr/include/mach-o.h, but it is basically:
   3804     //
   3805     //  uint32_t flavor  - this is the flavor argument you would pass to thread_get_state
   3806     //  uint32_t count   - this is the count of longs in the thread state data
   3807     //  struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
   3808     //  <repeat this trio>
   3809     //
   3810     // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
   3811     // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
   3812     // out of data in this form & attach them to a given thread.  That should underlie the MacOS X User process plugin,
   3813     // and we'll also need it for the MacOS X Core File process plugin.  When we have that we can also use it here.
   3814     //
   3815     // For now we hard-code the offsets and flavors we need:
   3816     //
   3817     //
   3818 
   3819     ModuleSP module_sp(GetModule());
   3820     if (module_sp)
   3821     {
   3822         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
   3823         struct load_command load_cmd;
   3824         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
   3825         uint32_t i;
   3826         lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
   3827         bool done = false;
   3828 
   3829         for (i=0; i<m_header.ncmds; ++i)
   3830         {
   3831             const lldb::offset_t cmd_offset = offset;
   3832             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
   3833                 break;
   3834 
   3835             switch (load_cmd.cmd)
   3836             {
   3837             case LoadCommandUnixThread:
   3838             case LoadCommandThread:
   3839                 {
   3840                     while (offset < cmd_offset + load_cmd.cmdsize)
   3841                     {
   3842                         uint32_t flavor = m_data.GetU32(&offset);
   3843                         uint32_t count = m_data.GetU32(&offset);
   3844                         if (count == 0)
   3845                         {
   3846                             // We've gotten off somehow, log and exit;
   3847                             return m_entry_point_address;
   3848                         }
   3849 
   3850                         switch (m_header.cputype)
   3851                         {
   3852                         case llvm::MachO::CPUTypeARM:
   3853                            if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
   3854                            {
   3855                                offset += 60;  // This is the offset of pc in the GPR thread state data structure.
   3856                                start_address = m_data.GetU32(&offset);
   3857                                done = true;
   3858                             }
   3859                         break;
   3860                         case llvm::MachO::CPUTypeI386:
   3861                            if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
   3862                            {
   3863                                offset += 40;  // This is the offset of eip in the GPR thread state data structure.
   3864                                start_address = m_data.GetU32(&offset);
   3865                                done = true;
   3866                             }
   3867                         break;
   3868                         case llvm::MachO::CPUTypeX86_64:
   3869                            if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
   3870                            {
   3871                                offset += 16 * 8;  // This is the offset of rip in the GPR thread state data structure.
   3872                                start_address = m_data.GetU64(&offset);
   3873                                done = true;
   3874                             }
   3875                         break;
   3876                         default:
   3877                             return m_entry_point_address;
   3878                         }
   3879                         // Haven't found the GPR flavor yet, skip over the data for this flavor:
   3880                         if (done)
   3881                             break;
   3882                         offset += count * 4;
   3883                     }
   3884                 }
   3885                 break;
   3886             case LoadCommandMain:
   3887                 {
   3888                     ConstString text_segment_name ("__TEXT");
   3889                     uint64_t entryoffset = m_data.GetU64(&offset);
   3890                     SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
   3891                     if (text_segment_sp)
   3892                     {
   3893                         done = true;
   3894                         start_address = text_segment_sp->GetFileAddress() + entryoffset;
   3895                     }
   3896                 }
   3897 
   3898             default:
   3899                 break;
   3900             }
   3901             if (done)
   3902                 break;
   3903 
   3904             // Go to the next load command:
   3905             offset = cmd_offset + load_cmd.cmdsize;
   3906         }
   3907 
   3908         if (start_address != LLDB_INVALID_ADDRESS)
   3909         {
   3910             // We got the start address from the load commands, so now resolve that address in the sections
   3911             // of this ObjectFile:
   3912             if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
   3913             {
   3914                 m_entry_point_address.Clear();
   3915             }
   3916         }
   3917         else
   3918         {
   3919             // We couldn't read the UnixThread load command - maybe it wasn't there.  As a fallback look for the
   3920             // "start" symbol in the main executable.
   3921 
   3922             ModuleSP module_sp (GetModule());
   3923 
   3924             if (module_sp)
   3925             {
   3926                 SymbolContextList contexts;
   3927                 SymbolContext context;
   3928                 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
   3929                 {
   3930                     if (contexts.GetContextAtIndex(0, context))
   3931                         m_entry_point_address = context.symbol->GetAddress();
   3932                 }
   3933             }
   3934         }
   3935     }
   3936 
   3937     return m_entry_point_address;
   3938 
   3939 }
   3940 
   3941 lldb_private::Address
   3942 ObjectFileMachO::GetHeaderAddress ()
   3943 {
   3944     lldb_private::Address header_addr;
   3945     SectionList *section_list = GetSectionList();
   3946     if (section_list)
   3947     {
   3948         SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
   3949         if (text_segment_sp)
   3950         {
   3951             header_addr.SetSection (text_segment_sp);
   3952             header_addr.SetOffset (0);
   3953         }
   3954     }
   3955     return header_addr;
   3956 }
   3957 
   3958 uint32_t
   3959 ObjectFileMachO::GetNumThreadContexts ()
   3960 {
   3961     ModuleSP module_sp(GetModule());
   3962     if (module_sp)
   3963     {
   3964         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
   3965         if (!m_thread_context_offsets_valid)
   3966         {
   3967             m_thread_context_offsets_valid = true;
   3968             lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
   3969             FileRangeArray::Entry file_range;
   3970             thread_command thread_cmd;
   3971             for (uint32_t i=0; i<m_header.ncmds; ++i)
   3972             {
   3973                 const uint32_t cmd_offset = offset;
   3974                 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
   3975                     break;
   3976 
   3977                 if (thread_cmd.cmd == LoadCommandThread)
   3978                 {
   3979                     file_range.SetRangeBase (offset);
   3980                     file_range.SetByteSize (thread_cmd.cmdsize - 8);
   3981                     m_thread_context_offsets.Append (file_range);
   3982                 }
   3983                 offset = cmd_offset + thread_cmd.cmdsize;
   3984             }
   3985         }
   3986     }
   3987     return m_thread_context_offsets.GetSize();
   3988 }
   3989 
   3990 lldb::RegisterContextSP
   3991 ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
   3992 {
   3993     lldb::RegisterContextSP reg_ctx_sp;
   3994 
   3995     ModuleSP module_sp(GetModule());
   3996     if (module_sp)
   3997     {
   3998         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
   3999         if (!m_thread_context_offsets_valid)
   4000             GetNumThreadContexts ();
   4001 
   4002         const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
   4003         if (thread_context_file_range)
   4004         {
   4005 
   4006             DataExtractor data (m_data,
   4007                                 thread_context_file_range->GetRangeBase(),
   4008                                 thread_context_file_range->GetByteSize());
   4009 
   4010             switch (m_header.cputype)
   4011             {
   4012                 case llvm::MachO::CPUTypeARM:
   4013                     reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
   4014                     break;
   4015 
   4016                 case llvm::MachO::CPUTypeI386:
   4017                     reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
   4018                     break;
   4019 
   4020                 case llvm::MachO::CPUTypeX86_64:
   4021                     reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
   4022                     break;
   4023             }
   4024         }
   4025     }
   4026     return reg_ctx_sp;
   4027 }
   4028 
   4029 
   4030 ObjectFile::Type
   4031 ObjectFileMachO::CalculateType()
   4032 {
   4033     switch (m_header.filetype)
   4034     {
   4035         case HeaderFileTypeObject:                                          // 0x1u MH_OBJECT
   4036             if (GetAddressByteSize () == 4)
   4037             {
   4038                 // 32 bit kexts are just object files, but they do have a valid
   4039                 // UUID load command.
   4040                 UUID uuid;
   4041                 if (GetUUID(&uuid))
   4042                 {
   4043                     // this checking for the UUID load command is not enough
   4044                     // we could eventually look for the symbol named
   4045                     // "OSKextGetCurrentIdentifier" as this is required of kexts
   4046                     if (m_strata == eStrataInvalid)
   4047                         m_strata = eStrataKernel;
   4048                     return eTypeSharedLibrary;
   4049                 }
   4050             }
   4051             return eTypeObjectFile;
   4052 
   4053         case HeaderFileTypeExecutable:          return eTypeExecutable;     // 0x2u MH_EXECUTE
   4054         case HeaderFileTypeFixedVMShlib:        return eTypeSharedLibrary;  // 0x3u MH_FVMLIB
   4055         case HeaderFileTypeCore:                return eTypeCoreFile;       // 0x4u MH_CORE
   4056         case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary;  // 0x5u MH_PRELOAD
   4057         case HeaderFileTypeDynamicShlib:        return eTypeSharedLibrary;  // 0x6u MH_DYLIB
   4058         case HeaderFileTypeDynamicLinkEditor:   return eTypeDynamicLinker;  // 0x7u MH_DYLINKER
   4059         case HeaderFileTypeBundle:              return eTypeSharedLibrary;  // 0x8u MH_BUNDLE
   4060         case HeaderFileTypeDynamicShlibStub:    return eTypeStubLibrary;    // 0x9u MH_DYLIB_STUB
   4061         case HeaderFileTypeDSYM:                return eTypeDebugInfo;      // 0xAu MH_DSYM
   4062         case HeaderFileTypeKextBundle:          return eTypeSharedLibrary;  // 0xBu MH_KEXT_BUNDLE
   4063         default:
   4064             break;
   4065     }
   4066     return eTypeUnknown;
   4067 }
   4068 
   4069 ObjectFile::Strata
   4070 ObjectFileMachO::CalculateStrata()
   4071 {
   4072     switch (m_header.filetype)
   4073     {
   4074         case HeaderFileTypeObject:      // 0x1u MH_OBJECT
   4075             {
   4076                 // 32 bit kexts are just object files, but they do have a valid
   4077                 // UUID load command.
   4078                 UUID uuid;
   4079                 if (GetUUID(&uuid))
   4080                 {
   4081                     // this checking for the UUID load command is not enough
   4082                     // we could eventually look for the symbol named
   4083                     // "OSKextGetCurrentIdentifier" as this is required of kexts
   4084                     if (m_type == eTypeInvalid)
   4085                         m_type = eTypeSharedLibrary;
   4086 
   4087                     return eStrataKernel;
   4088                 }
   4089             }
   4090             return eStrataUnknown;
   4091 
   4092         case HeaderFileTypeExecutable:                                     // 0x2u MH_EXECUTE
   4093             // Check for the MH_DYLDLINK bit in the flags
   4094             if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
   4095             {
   4096                 return eStrataUser;
   4097             }
   4098             else
   4099             {
   4100                 SectionList *section_list = GetSectionList();
   4101                 if (section_list)
   4102                 {
   4103                     static ConstString g_kld_section_name ("__KLD");
   4104                     if (section_list->FindSectionByName(g_kld_section_name))
   4105                         return eStrataKernel;
   4106                 }
   4107             }
   4108             return eStrataRawImage;
   4109 
   4110         case HeaderFileTypeFixedVMShlib:        return eStrataUser;         // 0x3u MH_FVMLIB
   4111         case HeaderFileTypeCore:                return eStrataUnknown;      // 0x4u MH_CORE
   4112         case HeaderFileTypePreloadedExecutable: return eStrataRawImage;     // 0x5u MH_PRELOAD
   4113         case HeaderFileTypeDynamicShlib:        return eStrataUser;         // 0x6u MH_DYLIB
   4114         case HeaderFileTypeDynamicLinkEditor:   return eStrataUser;         // 0x7u MH_DYLINKER
   4115         case HeaderFileTypeBundle:              return eStrataUser;         // 0x8u MH_BUNDLE
   4116         case HeaderFileTypeDynamicShlibStub:    return eStrataUser;         // 0x9u MH_DYLIB_STUB
   4117         case HeaderFileTypeDSYM:                return eStrataUnknown;      // 0xAu MH_DSYM
   4118         case HeaderFileTypeKextBundle:          return eStrataKernel;       // 0xBu MH_KEXT_BUNDLE
   4119         default:
   4120             break;
   4121     }
   4122     return eStrataUnknown;
   4123 }
   4124 
   4125 
   4126 uint32_t
   4127 ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
   4128 {
   4129     ModuleSP module_sp(GetModule());
   4130     if (module_sp)
   4131     {
   4132         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
   4133         struct dylib_command load_cmd;
   4134         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
   4135         uint32_t version_cmd = 0;
   4136         uint64_t version = 0;
   4137         uint32_t i;
   4138         for (i=0; i<m_header.ncmds; ++i)
   4139         {
   4140             const lldb::offset_t cmd_offset = offset;
   4141             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
   4142                 break;
   4143 
   4144             if (load_cmd.cmd == LoadCommandDylibIdent)
   4145             {
   4146                 if (version_cmd == 0)
   4147                 {
   4148                     version_cmd = load_cmd.cmd;
   4149                     if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
   4150                         break;
   4151                     version = load_cmd.dylib.current_version;
   4152                 }
   4153                 break; // Break for now unless there is another more complete version
   4154                        // number load command in the future.
   4155             }
   4156             offset = cmd_offset + load_cmd.cmdsize;
   4157         }
   4158 
   4159         if (version_cmd == LoadCommandDylibIdent)
   4160         {
   4161             if (versions != NULL && num_versions > 0)
   4162             {
   4163                 if (num_versions > 0)
   4164                     versions[0] = (version & 0xFFFF0000ull) >> 16;
   4165                 if (num_versions > 1)
   4166                     versions[1] = (version & 0x0000FF00ull) >> 8;
   4167                 if (num_versions > 2)
   4168                     versions[2] = (version & 0x000000FFull);
   4169                 // Fill in an remaining version numbers with invalid values
   4170                 for (i=3; i<num_versions; ++i)
   4171                     versions[i] = UINT32_MAX;
   4172             }
   4173             // The LC_ID_DYLIB load command has a version with 3 version numbers
   4174             // in it, so always return 3
   4175             return 3;
   4176         }
   4177     }
   4178     return false;
   4179 }
   4180 
   4181 bool
   4182 ObjectFileMachO::GetArchitecture (ArchSpec &arch)
   4183 {
   4184     ModuleSP module_sp(GetModule());
   4185     if (module_sp)
   4186     {
   4187         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
   4188         arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
   4189 
   4190         // Files with type MH_PRELOAD are currently used in cases where the image
   4191         // debugs at the addresses in the file itself. Below we set the OS to
   4192         // unknown to make sure we use the DynamicLoaderStatic()...
   4193         if (m_header.filetype == HeaderFileTypePreloadedExecutable)
   4194         {
   4195             arch.GetTriple().setOS (llvm::Triple::UnknownOS);
   4196         }
   4197         return true;
   4198     }
   4199     return false;
   4200 }
   4201 
   4202 
   4203 UUID
   4204 ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
   4205 {
   4206     UUID uuid;
   4207     if (process)
   4208     {
   4209         addr_t all_image_infos = process->GetImageInfoAddress();
   4210 
   4211         // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
   4212         // or it may be the address of the dyld_all_image_infos structure (want).  The first four
   4213         // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
   4214         // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
   4215 
   4216         Error err;
   4217         uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
   4218         if (version_or_magic != -1
   4219             && version_or_magic != HeaderMagic32
   4220             && version_or_magic != HeaderMagic32Swapped
   4221             && version_or_magic != HeaderMagic64
   4222             && version_or_magic != HeaderMagic64Swapped
   4223             && version_or_magic >= 13)
   4224         {
   4225             addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
   4226             int wordsize = process->GetAddressByteSize();
   4227             if (wordsize == 8)
   4228             {
   4229                 sharedCacheUUID_address = all_image_infos + 160;  // sharedCacheUUID <mach-o/dyld_images.h>
   4230             }
   4231             if (wordsize == 4)
   4232             {
   4233                 sharedCacheUUID_address = all_image_infos + 84;   // sharedCacheUUID <mach-o/dyld_images.h>
   4234             }
   4235             if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
   4236             {
   4237                 uuid_t shared_cache_uuid;
   4238                 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
   4239                 {
   4240                     uuid.SetBytes (shared_cache_uuid);
   4241                 }
   4242             }
   4243         }
   4244     }
   4245     return uuid;
   4246 }
   4247 
   4248 UUID
   4249 ObjectFileMachO::GetLLDBSharedCacheUUID ()
   4250 {
   4251     UUID uuid;
   4252 #if defined (__APPLE__) && defined (__arm__)
   4253     uint8_t *(*dyld_get_all_image_infos)(void);
   4254     dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
   4255     if (dyld_get_all_image_infos)
   4256     {
   4257         uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
   4258         if (dyld_all_image_infos_address)
   4259         {
   4260             uint32_t *version = (uint32_t*) dyld_all_image_infos_address;              // version <mach-o/dyld_images.h>
   4261             if (*version >= 13)
   4262             {
   4263                 uuid_t *sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84);  // sharedCacheUUID <mach-o/dyld_images.h>
   4264                 uuid.SetBytes (sharedCacheUUID_address);
   4265             }
   4266         }
   4267     }
   4268 #endif
   4269     return uuid;
   4270 }
   4271 
   4272 
   4273 //------------------------------------------------------------------
   4274 // PluginInterface protocol
   4275 //------------------------------------------------------------------
   4276 lldb_private::ConstString
   4277 ObjectFileMachO::GetPluginName()
   4278 {
   4279     return GetPluginNameStatic();
   4280 }
   4281 
   4282 uint32_t
   4283 ObjectFileMachO::GetPluginVersion()
   4284 {
   4285     return 1;
   4286 }
   4287 
   4288