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 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 #include "llvm/ADT/StringRef.h"
14 
15 // Project includes
16 #include "lldb/Core/ArchSpec.h"
17 #include "lldb/Core/DataBuffer.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/Error.h"
20 #include "lldb/Core/FileSpecList.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/ModuleSpec.h"
24 #include "lldb/Core/PluginManager.h"
25 #include "lldb/Core/RangeMap.h"
26 #include "lldb/Core/Section.h"
27 #include "lldb/Core/StreamFile.h"
28 #include "lldb/Core/StreamString.h"
29 #include "lldb/Core/Timer.h"
30 #include "lldb/Core/UUID.h"
31 #include "lldb/Host/Host.h"
32 #include "lldb/Host/FileSpec.h"
33 #include "lldb/Symbol/DWARFCallFrameInfo.h"
34 #include "lldb/Symbol/ObjectFile.h"
35 #include "lldb/Target/MemoryRegionInfo.h"
36 #include "lldb/Target/Platform.h"
37 #include "lldb/Target/Process.h"
38 #include "lldb/Target/SectionLoadList.h"
39 #include "lldb/Target/Target.h"
40 #include "lldb/Target/Thread.h"
41 #include "lldb/Target/ThreadList.h"
42 #include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
43 #include "Plugins/Process/Utility/RegisterContextDarwin_arm64.h"
44 #include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
45 #include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
46 
47 #include "lldb/Utility/SafeMachO.h"
48 
49 #include "ObjectFileMachO.h"
50 
51 #if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
52 // GetLLDBSharedCacheUUID() needs to call dlsym()
53 #include <dlfcn.h>
54 #endif
55 
56 #ifndef __APPLE__
57 #include "Utility/UuidCompatibility.h"
58 #endif
59 
60 #define THUMB_ADDRESS_BIT_MASK 0xfffffffffffffffeull
61 using namespace lldb;
62 using namespace lldb_private;
63 using namespace llvm::MachO;
64 
65 // Some structure definitions needed for parsing the dyld shared cache files
66 // found on iOS devices.
67 
68 struct lldb_copy_dyld_cache_header_v1
69 {
70     char        magic[16];            // e.g. "dyld_v0    i386", "dyld_v1   armv7", etc.
71     uint32_t    mappingOffset;        // file offset to first dyld_cache_mapping_info
72     uint32_t    mappingCount;         // number of dyld_cache_mapping_info entries
73     uint32_t    imagesOffset;
74     uint32_t    imagesCount;
75     uint64_t    dyldBaseAddress;
76     uint64_t    codeSignatureOffset;
77     uint64_t    codeSignatureSize;
78     uint64_t    slideInfoOffset;
79     uint64_t    slideInfoSize;
80     uint64_t    localSymbolsOffset;
81     uint64_t    localSymbolsSize;
82     uint8_t     uuid[16];             // v1 and above, also recorded in dyld_all_image_infos v13 and later
83 };
84 
85 struct lldb_copy_dyld_cache_mapping_info
86 {
87     uint64_t        address;
88     uint64_t        size;
89     uint64_t        fileOffset;
90     uint32_t        maxProt;
91     uint32_t        initProt;
92 };
93 
94 struct lldb_copy_dyld_cache_local_symbols_info
95 {
96     uint32_t        nlistOffset;
97     uint32_t        nlistCount;
98     uint32_t        stringsOffset;
99     uint32_t        stringsSize;
100     uint32_t        entriesOffset;
101     uint32_t        entriesCount;
102 };
103 struct lldb_copy_dyld_cache_local_symbols_entry
104 {
105     uint32_t        dylibOffset;
106     uint32_t        nlistStartIndex;
107     uint32_t        nlistCount;
108 };
109 
110 
111 class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
112 {
113 public:
114     RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
115         RegisterContextDarwin_x86_64 (thread, 0)
116     {
117         SetRegisterDataFrom_LC_THREAD (data);
118     }
119 
120     void
121     InvalidateAllRegisters() override
122     {
123         // Do nothing... registers are always valid...
124     }
125 
126     void
127     SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
128     {
129         lldb::offset_t offset = 0;
130         SetError (GPRRegSet, Read, -1);
131         SetError (FPURegSet, Read, -1);
132         SetError (EXCRegSet, Read, -1);
133         bool done = false;
134 
135         while (!done)
136         {
137             int flavor = data.GetU32 (&offset);
138             if (flavor == 0)
139                 done = true;
140             else
141             {
142                 uint32_t i;
143                 uint32_t count = data.GetU32 (&offset);
144                 switch (flavor)
145                 {
146                     case GPRRegSet:
147                         for (i=0; i<count; ++i)
148                             (&gpr.rax)[i] = data.GetU64(&offset);
149                         SetError (GPRRegSet, Read, 0);
150                         done = true;
151 
152                         break;
153                     case FPURegSet:
154                         // TODO: fill in FPU regs....
155                         //SetError (FPURegSet, Read, -1);
156                         done = true;
157 
158                         break;
159                     case EXCRegSet:
160                         exc.trapno = data.GetU32(&offset);
161                         exc.err = data.GetU32(&offset);
162                         exc.faultvaddr = data.GetU64(&offset);
163                         SetError (EXCRegSet, Read, 0);
164                         done = true;
165                         break;
166                     case 7:
167                     case 8:
168                     case 9:
169                         // fancy flavors that encapsulate of the above
170                         // flavors...
171                         break;
172 
173                     default:
174                         done = true;
175                         break;
176                 }
177             }
178         }
179     }
180 
181     static size_t
182     WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
183     {
184         const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
185         if (reg_info == NULL)
186             reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
187         if (reg_info)
188         {
189             lldb_private::RegisterValue reg_value;
190             if (reg_ctx->ReadRegister(reg_info, reg_value))
191             {
192                 if (reg_info->byte_size >= reg_byte_size)
193                     data.Write(reg_value.GetBytes(), reg_byte_size);
194                 else
195                 {
196                     data.Write(reg_value.GetBytes(), reg_info->byte_size);
197                     for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
198                         data.PutChar(0);
199                 }
200                 return reg_byte_size;
201             }
202         }
203         // Just write zeros if all else fails
204         for (size_t i=0; i<reg_byte_size; ++ i)
205             data.PutChar(0);
206         return reg_byte_size;
207     }
208 
209     static bool
210     Create_LC_THREAD (Thread *thread, Stream &data)
211     {
212         RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
213         if (reg_ctx_sp)
214         {
215             RegisterContext *reg_ctx = reg_ctx_sp.get();
216 
217             data.PutHex32 (GPRRegSet);  // Flavor
218             data.PutHex32 (GPRWordCount);
219             WriteRegister (reg_ctx, "rax", NULL, 8, data);
220             WriteRegister (reg_ctx, "rbx", NULL, 8, data);
221             WriteRegister (reg_ctx, "rcx", NULL, 8, data);
222             WriteRegister (reg_ctx, "rdx", NULL, 8, data);
223             WriteRegister (reg_ctx, "rdi", NULL, 8, data);
224             WriteRegister (reg_ctx, "rsi", NULL, 8, data);
225             WriteRegister (reg_ctx, "rbp", NULL, 8, data);
226             WriteRegister (reg_ctx, "rsp", NULL, 8, data);
227             WriteRegister (reg_ctx, "r8", NULL, 8, data);
228             WriteRegister (reg_ctx, "r9", NULL, 8, data);
229             WriteRegister (reg_ctx, "r10", NULL, 8, data);
230             WriteRegister (reg_ctx, "r11", NULL, 8, data);
231             WriteRegister (reg_ctx, "r12", NULL, 8, data);
232             WriteRegister (reg_ctx, "r13", NULL, 8, data);
233             WriteRegister (reg_ctx, "r14", NULL, 8, data);
234             WriteRegister (reg_ctx, "r15", NULL, 8, data);
235             WriteRegister (reg_ctx, "rip", NULL, 8, data);
236             WriteRegister (reg_ctx, "rflags", NULL, 8, data);
237             WriteRegister (reg_ctx, "cs", NULL, 8, data);
238             WriteRegister (reg_ctx, "fs", NULL, 8, data);
239             WriteRegister (reg_ctx, "gs", NULL, 8, data);
240 
241 //            // Write out the FPU registers
242 //            const size_t fpu_byte_size = sizeof(FPU);
243 //            size_t bytes_written = 0;
244 //            data.PutHex32 (FPURegSet);
245 //            data.PutHex32 (fpu_byte_size/sizeof(uint64_t));
246 //            bytes_written += data.PutHex32(0);                                   // uint32_t pad[0]
247 //            bytes_written += data.PutHex32(0);                                   // uint32_t pad[1]
248 //            bytes_written += WriteRegister (reg_ctx, "fcw", "fctrl", 2, data);   // uint16_t    fcw;    // "fctrl"
249 //            bytes_written += WriteRegister (reg_ctx, "fsw" , "fstat", 2, data);  // uint16_t    fsw;    // "fstat"
250 //            bytes_written += WriteRegister (reg_ctx, "ftw" , "ftag", 1, data);   // uint8_t     ftw;    // "ftag"
251 //            bytes_written += data.PutHex8  (0);                                  // uint8_t pad1;
252 //            bytes_written += WriteRegister (reg_ctx, "fop" , NULL, 2, data);     // uint16_t    fop;    // "fop"
253 //            bytes_written += WriteRegister (reg_ctx, "fioff", "ip", 4, data);    // uint32_t    ip;     // "fioff"
254 //            bytes_written += WriteRegister (reg_ctx, "fiseg", NULL, 2, data);    // uint16_t    cs;     // "fiseg"
255 //            bytes_written += data.PutHex16 (0);                                  // uint16_t    pad2;
256 //            bytes_written += WriteRegister (reg_ctx, "dp", "fooff" , 4, data);   // uint32_t    dp;     // "fooff"
257 //            bytes_written += WriteRegister (reg_ctx, "foseg", NULL, 2, data);    // uint16_t    ds;     // "foseg"
258 //            bytes_written += data.PutHex16 (0);                                  // uint16_t    pad3;
259 //            bytes_written += WriteRegister (reg_ctx, "mxcsr", NULL, 4, data);    // uint32_t    mxcsr;
260 //            bytes_written += WriteRegister (reg_ctx, "mxcsrmask", NULL, 4, data);// uint32_t    mxcsrmask;
261 //            bytes_written += WriteRegister (reg_ctx, "stmm0", NULL, sizeof(MMSReg), data);
262 //            bytes_written += WriteRegister (reg_ctx, "stmm1", NULL, sizeof(MMSReg), data);
263 //            bytes_written += WriteRegister (reg_ctx, "stmm2", NULL, sizeof(MMSReg), data);
264 //            bytes_written += WriteRegister (reg_ctx, "stmm3", NULL, sizeof(MMSReg), data);
265 //            bytes_written += WriteRegister (reg_ctx, "stmm4", NULL, sizeof(MMSReg), data);
266 //            bytes_written += WriteRegister (reg_ctx, "stmm5", NULL, sizeof(MMSReg), data);
267 //            bytes_written += WriteRegister (reg_ctx, "stmm6", NULL, sizeof(MMSReg), data);
268 //            bytes_written += WriteRegister (reg_ctx, "stmm7", NULL, sizeof(MMSReg), data);
269 //            bytes_written += WriteRegister (reg_ctx, "xmm0" , NULL, sizeof(XMMReg), data);
270 //            bytes_written += WriteRegister (reg_ctx, "xmm1" , NULL, sizeof(XMMReg), data);
271 //            bytes_written += WriteRegister (reg_ctx, "xmm2" , NULL, sizeof(XMMReg), data);
272 //            bytes_written += WriteRegister (reg_ctx, "xmm3" , NULL, sizeof(XMMReg), data);
273 //            bytes_written += WriteRegister (reg_ctx, "xmm4" , NULL, sizeof(XMMReg), data);
274 //            bytes_written += WriteRegister (reg_ctx, "xmm5" , NULL, sizeof(XMMReg), data);
275 //            bytes_written += WriteRegister (reg_ctx, "xmm6" , NULL, sizeof(XMMReg), data);
276 //            bytes_written += WriteRegister (reg_ctx, "xmm7" , NULL, sizeof(XMMReg), data);
277 //            bytes_written += WriteRegister (reg_ctx, "xmm8" , NULL, sizeof(XMMReg), data);
278 //            bytes_written += WriteRegister (reg_ctx, "xmm9" , NULL, sizeof(XMMReg), data);
279 //            bytes_written += WriteRegister (reg_ctx, "xmm10", NULL, sizeof(XMMReg), data);
280 //            bytes_written += WriteRegister (reg_ctx, "xmm11", NULL, sizeof(XMMReg), data);
281 //            bytes_written += WriteRegister (reg_ctx, "xmm12", NULL, sizeof(XMMReg), data);
282 //            bytes_written += WriteRegister (reg_ctx, "xmm13", NULL, sizeof(XMMReg), data);
283 //            bytes_written += WriteRegister (reg_ctx, "xmm14", NULL, sizeof(XMMReg), data);
284 //            bytes_written += WriteRegister (reg_ctx, "xmm15", NULL, sizeof(XMMReg), data);
285 //
286 //            // Fill rest with zeros
287 //            for (size_t i=0, n = fpu_byte_size - bytes_written; i<n; ++ i)
288 //                data.PutChar(0);
289 
290             // Write out the EXC registers
291             data.PutHex32 (EXCRegSet);
292             data.PutHex32 (EXCWordCount);
293             WriteRegister (reg_ctx, "trapno", NULL, 4, data);
294             WriteRegister (reg_ctx, "err", NULL, 4, data);
295             WriteRegister (reg_ctx, "faultvaddr", NULL, 8, data);
296             return true;
297         }
298         return false;
299     }
300 
301 protected:
302     int
303     DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override
304     {
305         return 0;
306     }
307 
308     int
309     DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override
310     {
311         return 0;
312     }
313 
314     int
315     DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override
316     {
317         return 0;
318     }
319 
320     int
321     DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override
322     {
323         return 0;
324     }
325 
326     int
327     DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override
328     {
329         return 0;
330     }
331 
332     int
333     DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override
334     {
335         return 0;
336     }
337 };
338 
339 class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
340 {
341 public:
342     RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
343     RegisterContextDarwin_i386 (thread, 0)
344     {
345         SetRegisterDataFrom_LC_THREAD (data);
346     }
347 
348     void
349     InvalidateAllRegisters() override
350     {
351         // Do nothing... registers are always valid...
352     }
353 
354     void
355     SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
356     {
357         lldb::offset_t offset = 0;
358         SetError (GPRRegSet, Read, -1);
359         SetError (FPURegSet, Read, -1);
360         SetError (EXCRegSet, Read, -1);
361         bool done = false;
362 
363         while (!done)
364         {
365             int flavor = data.GetU32 (&offset);
366             if (flavor == 0)
367                 done = true;
368             else
369             {
370                 uint32_t i;
371                 uint32_t count = data.GetU32 (&offset);
372                 switch (flavor)
373                 {
374                     case GPRRegSet:
375                         for (i=0; i<count; ++i)
376                             (&gpr.eax)[i] = data.GetU32(&offset);
377                         SetError (GPRRegSet, Read, 0);
378                         done = true;
379 
380                         break;
381                     case FPURegSet:
382                         // TODO: fill in FPU regs....
383                         //SetError (FPURegSet, Read, -1);
384                         done = true;
385 
386                         break;
387                     case EXCRegSet:
388                         exc.trapno = data.GetU32(&offset);
389                         exc.err = data.GetU32(&offset);
390                         exc.faultvaddr = data.GetU32(&offset);
391                         SetError (EXCRegSet, Read, 0);
392                         done = true;
393                         break;
394                     case 7:
395                     case 8:
396                     case 9:
397                         // fancy flavors that encapsulate of the above
398                         // flavors...
399                         break;
400 
401                     default:
402                         done = true;
403                         break;
404                 }
405             }
406         }
407     }
408 
409     static size_t
410     WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
411     {
412         const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
413         if (reg_info == NULL)
414             reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
415         if (reg_info)
416         {
417             lldb_private::RegisterValue reg_value;
418             if (reg_ctx->ReadRegister(reg_info, reg_value))
419             {
420                 if (reg_info->byte_size >= reg_byte_size)
421                     data.Write(reg_value.GetBytes(), reg_byte_size);
422                 else
423                 {
424                     data.Write(reg_value.GetBytes(), reg_info->byte_size);
425                     for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
426                         data.PutChar(0);
427                 }
428                 return reg_byte_size;
429             }
430         }
431         // Just write zeros if all else fails
432         for (size_t i=0; i<reg_byte_size; ++ i)
433             data.PutChar(0);
434         return reg_byte_size;
435     }
436 
437     static bool
438     Create_LC_THREAD (Thread *thread, Stream &data)
439     {
440         RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
441         if (reg_ctx_sp)
442         {
443             RegisterContext *reg_ctx = reg_ctx_sp.get();
444 
445             data.PutHex32 (GPRRegSet);  // Flavor
446             data.PutHex32 (GPRWordCount);
447             WriteRegister (reg_ctx, "eax", NULL, 4, data);
448             WriteRegister (reg_ctx, "ebx", NULL, 4, data);
449             WriteRegister (reg_ctx, "ecx", NULL, 4, data);
450             WriteRegister (reg_ctx, "edx", NULL, 4, data);
451             WriteRegister (reg_ctx, "edi", NULL, 4, data);
452             WriteRegister (reg_ctx, "esi", NULL, 4, data);
453             WriteRegister (reg_ctx, "ebp", NULL, 4, data);
454             WriteRegister (reg_ctx, "esp", NULL, 4, data);
455             WriteRegister (reg_ctx, "ss", NULL, 4, data);
456             WriteRegister (reg_ctx, "eflags", NULL, 4, data);
457             WriteRegister (reg_ctx, "eip", NULL, 4, data);
458             WriteRegister (reg_ctx, "cs", NULL, 4, data);
459             WriteRegister (reg_ctx, "ds", NULL, 4, data);
460             WriteRegister (reg_ctx, "es", NULL, 4, data);
461             WriteRegister (reg_ctx, "fs", NULL, 4, data);
462             WriteRegister (reg_ctx, "gs", NULL, 4, data);
463 
464             // Write out the EXC registers
465             data.PutHex32 (EXCRegSet);
466             data.PutHex32 (EXCWordCount);
467             WriteRegister (reg_ctx, "trapno", NULL, 4, data);
468             WriteRegister (reg_ctx, "err", NULL, 4, data);
469             WriteRegister (reg_ctx, "faultvaddr", NULL, 4, data);
470             return true;
471         }
472         return false;
473     }
474 
475 protected:
476     int
477     DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override
478     {
479         return 0;
480     }
481 
482     int
483     DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override
484     {
485         return 0;
486     }
487 
488     int
489     DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override
490     {
491         return 0;
492     }
493 
494     int
495     DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override
496     {
497         return 0;
498     }
499 
500     int
501     DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override
502     {
503         return 0;
504     }
505 
506     int
507     DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override
508     {
509         return 0;
510     }
511 };
512 
513 class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
514 {
515 public:
516     RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
517         RegisterContextDarwin_arm (thread, 0)
518     {
519         SetRegisterDataFrom_LC_THREAD (data);
520     }
521 
522     void
523     InvalidateAllRegisters() override
524     {
525         // Do nothing... registers are always valid...
526     }
527 
528     void
529     SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
530     {
531         lldb::offset_t offset = 0;
532         SetError (GPRRegSet, Read, -1);
533         SetError (FPURegSet, Read, -1);
534         SetError (EXCRegSet, Read, -1);
535         bool done = false;
536 
537         while (!done)
538         {
539             int flavor = data.GetU32 (&offset);
540             uint32_t count = data.GetU32 (&offset);
541             lldb::offset_t next_thread_state = offset + (count * 4);
542             switch (flavor)
543             {
544                 case GPRRegSet:
545                     for (uint32_t i=0; i<count; ++i)
546                     {
547                         gpr.r[i] = data.GetU32(&offset);
548                     }
549 
550                     // Note that gpr.cpsr is also copied by the above loop; this loop technically extends
551                     // one element past the end of the gpr.r[] array.
552 
553                     SetError (GPRRegSet, Read, 0);
554                     offset = next_thread_state;
555                     break;
556 
557                 case FPURegSet:
558                     {
559                         uint8_t  *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0];
560                         const int fpu_reg_buf_size = sizeof (fpu.floats);
561                         if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
562                         {
563                             offset += fpu_reg_buf_size;
564                             fpu.fpscr = data.GetU32(&offset);
565                             SetError (FPURegSet, Read, 0);
566                         }
567                         else
568                         {
569                             done = true;
570                         }
571                     }
572                     offset = next_thread_state;
573                     break;
574 
575                 case EXCRegSet:
576                     if (count == 3)
577                     {
578                         exc.exception = data.GetU32(&offset);
579                         exc.fsr = data.GetU32(&offset);
580                         exc.far = data.GetU32(&offset);
581                         SetError (EXCRegSet, Read, 0);
582                     }
583                     done = true;
584                     offset = next_thread_state;
585                     break;
586 
587                 // Unknown register set flavor, stop trying to parse.
588                 default:
589                     done = true;
590             }
591         }
592     }
593 
594     static size_t
595     WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
596     {
597         const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
598         if (reg_info == NULL)
599             reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
600         if (reg_info)
601         {
602             lldb_private::RegisterValue reg_value;
603             if (reg_ctx->ReadRegister(reg_info, reg_value))
604             {
605                 if (reg_info->byte_size >= reg_byte_size)
606                     data.Write(reg_value.GetBytes(), reg_byte_size);
607                 else
608                 {
609                     data.Write(reg_value.GetBytes(), reg_info->byte_size);
610                     for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
611                         data.PutChar(0);
612                 }
613                 return reg_byte_size;
614             }
615         }
616         // Just write zeros if all else fails
617         for (size_t i=0; i<reg_byte_size; ++ i)
618             data.PutChar(0);
619         return reg_byte_size;
620     }
621 
622     static bool
623     Create_LC_THREAD (Thread *thread, Stream &data)
624     {
625         RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
626         if (reg_ctx_sp)
627         {
628             RegisterContext *reg_ctx = reg_ctx_sp.get();
629 
630             data.PutHex32 (GPRRegSet);  // Flavor
631             data.PutHex32 (GPRWordCount);
632             WriteRegister (reg_ctx, "r0", NULL, 4, data);
633             WriteRegister (reg_ctx, "r1", NULL, 4, data);
634             WriteRegister (reg_ctx, "r2", NULL, 4, data);
635             WriteRegister (reg_ctx, "r3", NULL, 4, data);
636             WriteRegister (reg_ctx, "r4", NULL, 4, data);
637             WriteRegister (reg_ctx, "r5", NULL, 4, data);
638             WriteRegister (reg_ctx, "r6", NULL, 4, data);
639             WriteRegister (reg_ctx, "r7", NULL, 4, data);
640             WriteRegister (reg_ctx, "r8", NULL, 4, data);
641             WriteRegister (reg_ctx, "r9", NULL, 4, data);
642             WriteRegister (reg_ctx, "r10", NULL, 4, data);
643             WriteRegister (reg_ctx, "r11", NULL, 4, data);
644             WriteRegister (reg_ctx, "r12", NULL, 4, data);
645             WriteRegister (reg_ctx, "sp", NULL, 4, data);
646             WriteRegister (reg_ctx, "lr", NULL, 4, data);
647             WriteRegister (reg_ctx, "pc", NULL, 4, data);
648             WriteRegister (reg_ctx, "cpsr", NULL, 4, data);
649 
650             // Write out the EXC registers
651 //            data.PutHex32 (EXCRegSet);
652 //            data.PutHex32 (EXCWordCount);
653 //            WriteRegister (reg_ctx, "exception", NULL, 4, data);
654 //            WriteRegister (reg_ctx, "fsr", NULL, 4, data);
655 //            WriteRegister (reg_ctx, "far", NULL, 4, data);
656             return true;
657         }
658         return false;
659     }
660 
661 protected:
662     int
663     DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override
664     {
665         return -1;
666     }
667 
668     int
669     DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override
670     {
671         return -1;
672     }
673 
674     int
675     DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override
676     {
677         return -1;
678     }
679 
680     int
681     DoReadDBG(lldb::tid_t tid, int flavor, DBG &dbg) override
682     {
683         return -1;
684     }
685 
686     int
687     DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override
688     {
689         return 0;
690     }
691 
692     int
693     DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override
694     {
695         return 0;
696     }
697 
698     int
699     DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override
700     {
701         return 0;
702     }
703 
704     int
705     DoWriteDBG(lldb::tid_t tid, int flavor, const DBG &dbg) override
706     {
707         return -1;
708     }
709 };
710 
711 class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64
712 {
713 public:
714     RegisterContextDarwin_arm64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
715         RegisterContextDarwin_arm64 (thread, 0)
716     {
717         SetRegisterDataFrom_LC_THREAD (data);
718     }
719 
720     void
721     InvalidateAllRegisters() override
722     {
723         // Do nothing... registers are always valid...
724     }
725 
726     void
727     SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
728     {
729         lldb::offset_t offset = 0;
730         SetError (GPRRegSet, Read, -1);
731         SetError (FPURegSet, Read, -1);
732         SetError (EXCRegSet, Read, -1);
733         bool done = false;
734         while (!done)
735         {
736             int flavor = data.GetU32 (&offset);
737             uint32_t count = data.GetU32 (&offset);
738             lldb::offset_t next_thread_state = offset + (count * 4);
739             switch (flavor)
740             {
741                 case GPRRegSet:
742                     // x0-x29 + fp + lr + sp + pc (== 33 64-bit registers) plus cpsr (1 32-bit register)
743                     if (count >= (33 * 2) + 1)
744                     {
745                         for (uint32_t i=0; i<33; ++i)
746                             gpr.x[i] = data.GetU64(&offset);
747                         gpr.cpsr = data.GetU32(&offset);
748                         SetError (GPRRegSet, Read, 0);
749                     }
750                     offset = next_thread_state;
751                     break;
752                 case FPURegSet:
753                     {
754                         uint8_t *fpu_reg_buf = (uint8_t*) &fpu.v[0];
755                         const int fpu_reg_buf_size = sizeof (fpu);
756                         if (fpu_reg_buf_size == count
757                             && data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
758                         {
759                             SetError (FPURegSet, Read, 0);
760                         }
761                         else
762                         {
763                             done = true;
764                         }
765                     }
766                     offset = next_thread_state;
767                     break;
768                 case EXCRegSet:
769                     if (count == 4)
770                     {
771                         exc.far = data.GetU64(&offset);
772                         exc.esr = data.GetU32(&offset);
773                         exc.exception = data.GetU32(&offset);
774                         SetError (EXCRegSet, Read, 0);
775                     }
776                     offset = next_thread_state;
777                     break;
778                 default:
779                     done = true;
780                     break;
781             }
782         }
783     }
784 
785     static size_t
786     WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
787     {
788         const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
789         if (reg_info == NULL)
790             reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
791         if (reg_info)
792         {
793             lldb_private::RegisterValue reg_value;
794             if (reg_ctx->ReadRegister(reg_info, reg_value))
795             {
796                 if (reg_info->byte_size >= reg_byte_size)
797                     data.Write(reg_value.GetBytes(), reg_byte_size);
798                 else
799                 {
800                     data.Write(reg_value.GetBytes(), reg_info->byte_size);
801                     for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
802                         data.PutChar(0);
803                 }
804                 return reg_byte_size;
805             }
806         }
807         // Just write zeros if all else fails
808         for (size_t i=0; i<reg_byte_size; ++ i)
809             data.PutChar(0);
810         return reg_byte_size;
811     }
812 
813     static bool
814     Create_LC_THREAD (Thread *thread, Stream &data)
815     {
816         RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
817         if (reg_ctx_sp)
818         {
819             RegisterContext *reg_ctx = reg_ctx_sp.get();
820 
821             data.PutHex32 (GPRRegSet);  // Flavor
822             data.PutHex32 (GPRWordCount);
823             WriteRegister (reg_ctx, "x0", NULL, 8, data);
824             WriteRegister (reg_ctx, "x1", NULL, 8, data);
825             WriteRegister (reg_ctx, "x2", NULL, 8, data);
826             WriteRegister (reg_ctx, "x3", NULL, 8, data);
827             WriteRegister (reg_ctx, "x4", NULL, 8, data);
828             WriteRegister (reg_ctx, "x5", NULL, 8, data);
829             WriteRegister (reg_ctx, "x6", NULL, 8, data);
830             WriteRegister (reg_ctx, "x7", NULL, 8, data);
831             WriteRegister (reg_ctx, "x8", NULL, 8, data);
832             WriteRegister (reg_ctx, "x9", NULL, 8, data);
833             WriteRegister (reg_ctx, "x10", NULL, 8, data);
834             WriteRegister (reg_ctx, "x11", NULL, 8, data);
835             WriteRegister (reg_ctx, "x12", NULL, 8, data);
836             WriteRegister (reg_ctx, "x13", NULL, 8, data);
837             WriteRegister (reg_ctx, "x14", NULL, 8, data);
838             WriteRegister (reg_ctx, "x15", NULL, 8, data);
839             WriteRegister (reg_ctx, "x16", NULL, 8, data);
840             WriteRegister (reg_ctx, "x17", NULL, 8, data);
841             WriteRegister (reg_ctx, "x18", NULL, 8, data);
842             WriteRegister (reg_ctx, "x19", NULL, 8, data);
843             WriteRegister (reg_ctx, "x20", NULL, 8, data);
844             WriteRegister (reg_ctx, "x21", NULL, 8, data);
845             WriteRegister (reg_ctx, "x22", NULL, 8, data);
846             WriteRegister (reg_ctx, "x23", NULL, 8, data);
847             WriteRegister (reg_ctx, "x24", NULL, 8, data);
848             WriteRegister (reg_ctx, "x25", NULL, 8, data);
849             WriteRegister (reg_ctx, "x26", NULL, 8, data);
850             WriteRegister (reg_ctx, "x27", NULL, 8, data);
851             WriteRegister (reg_ctx, "x28", NULL, 8, data);
852             WriteRegister (reg_ctx, "fp", NULL, 8, data);
853             WriteRegister (reg_ctx, "lr", NULL, 8, data);
854             WriteRegister (reg_ctx, "sp", NULL, 8, data);
855             WriteRegister (reg_ctx, "pc", NULL, 8, data);
856             WriteRegister (reg_ctx, "cpsr", NULL, 4, data);
857 
858             // Write out the EXC registers
859 //            data.PutHex32 (EXCRegSet);
860 //            data.PutHex32 (EXCWordCount);
861 //            WriteRegister (reg_ctx, "far", NULL, 8, data);
862 //            WriteRegister (reg_ctx, "esr", NULL, 4, data);
863 //            WriteRegister (reg_ctx, "exception", NULL, 4, data);
864             return true;
865         }
866         return false;
867     }
868 
869 protected:
870     int
871     DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override
872     {
873         return -1;
874     }
875 
876     int
877     DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override
878     {
879         return -1;
880     }
881 
882     int
883     DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override
884     {
885         return -1;
886     }
887 
888     int
889     DoReadDBG(lldb::tid_t tid, int flavor, DBG &dbg) override
890     {
891         return -1;
892     }
893 
894     int
895     DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override
896     {
897         return 0;
898     }
899 
900     int
901     DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override
902     {
903         return 0;
904     }
905 
906     int
907     DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override
908     {
909         return 0;
910     }
911 
912     int
913     DoWriteDBG(lldb::tid_t tid, int flavor, const DBG &dbg) override
914     {
915         return -1;
916     }
917 };
918 
919 static uint32_t
920 MachHeaderSizeFromMagic(uint32_t magic)
921 {
922     switch (magic)
923     {
924         case MH_MAGIC:
925         case MH_CIGAM:
926             return sizeof(struct mach_header);
927 
928         case MH_MAGIC_64:
929         case MH_CIGAM_64:
930             return sizeof(struct mach_header_64);
931             break;
932 
933         default:
934             break;
935     }
936     return 0;
937 }
938 
939 #define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
940 
941 void
942 ObjectFileMachO::Initialize()
943 {
944     PluginManager::RegisterPlugin (GetPluginNameStatic(),
945                                    GetPluginDescriptionStatic(),
946                                    CreateInstance,
947                                    CreateMemoryInstance,
948                                    GetModuleSpecifications,
949                                    SaveCore);
950 }
951 
952 void
953 ObjectFileMachO::Terminate()
954 {
955     PluginManager::UnregisterPlugin (CreateInstance);
956 }
957 
958 lldb_private::ConstString
959 ObjectFileMachO::GetPluginNameStatic()
960 {
961     static ConstString g_name("mach-o");
962     return g_name;
963 }
964 
965 const char *
966 ObjectFileMachO::GetPluginDescriptionStatic()
967 {
968     return "Mach-o object file reader (32 and 64 bit)";
969 }
970 
971 ObjectFile *
972 ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
973                                  DataBufferSP& data_sp,
974                                  lldb::offset_t data_offset,
975                                  const FileSpec* file,
976                                  lldb::offset_t file_offset,
977                                  lldb::offset_t length)
978 {
979     if (!data_sp)
980     {
981         data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
982         data_offset = 0;
983     }
984 
985     if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
986     {
987         // Update the data to contain the entire file if it doesn't already
988         if (data_sp->GetByteSize() < length)
989         {
990             data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
991             data_offset = 0;
992         }
993         std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, data_offset, file, file_offset, length));
994         if (objfile_ap.get() && objfile_ap->ParseHeader())
995             return objfile_ap.release();
996     }
997     return NULL;
998 }
999 
1000 ObjectFile *
1001 ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
1002                                        DataBufferSP& data_sp,
1003                                        const ProcessSP &process_sp,
1004                                        lldb::addr_t header_addr)
1005 {
1006     if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
1007     {
1008         std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
1009         if (objfile_ap.get() && objfile_ap->ParseHeader())
1010             return objfile_ap.release();
1011     }
1012     return NULL;
1013 }
1014 
1015 size_t
1016 ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
1017                                           lldb::DataBufferSP& data_sp,
1018                                           lldb::offset_t data_offset,
1019                                           lldb::offset_t file_offset,
1020                                           lldb::offset_t length,
1021                                           lldb_private::ModuleSpecList &specs)
1022 {
1023     const size_t initial_count = specs.GetSize();
1024 
1025     if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
1026     {
1027         DataExtractor data;
1028         data.SetData(data_sp);
1029         llvm::MachO::mach_header header;
1030         if (ParseHeader (data, &data_offset, header))
1031         {
1032             size_t header_and_load_cmds = header.sizeofcmds + MachHeaderSizeFromMagic(header.magic);
1033             if (header_and_load_cmds >= data_sp->GetByteSize())
1034             {
1035                 data_sp = file.ReadFileContents(file_offset, header_and_load_cmds);
1036                 data.SetData(data_sp);
1037                 data_offset = MachHeaderSizeFromMagic(header.magic);
1038             }
1039             if (data_sp)
1040             {
1041                 ModuleSpec spec;
1042                 spec.GetFileSpec() = file;
1043                 spec.SetObjectOffset(file_offset);
1044                 spec.SetObjectSize(length);
1045 
1046                 if (GetArchitecture (header, data, data_offset, spec.GetArchitecture()))
1047                 {
1048                     if (spec.GetArchitecture().IsValid())
1049                     {
1050                         GetUUID (header, data, data_offset, spec.GetUUID());
1051                         specs.Append(spec);
1052                     }
1053                 }
1054             }
1055         }
1056     }
1057     return specs.GetSize() - initial_count;
1058 }
1059 
1060 const ConstString &
1061 ObjectFileMachO::GetSegmentNameTEXT()
1062 {
1063     static ConstString g_segment_name_TEXT ("__TEXT");
1064     return g_segment_name_TEXT;
1065 }
1066 
1067 const ConstString &
1068 ObjectFileMachO::GetSegmentNameDATA()
1069 {
1070     static ConstString g_segment_name_DATA ("__DATA");
1071     return g_segment_name_DATA;
1072 }
1073 
1074 const ConstString &
1075 ObjectFileMachO::GetSegmentNameDATA_DIRTY()
1076 {
1077     static ConstString g_segment_name ("__DATA_DIRTY");
1078     return g_segment_name;
1079 }
1080 
1081 const ConstString &
1082 ObjectFileMachO::GetSegmentNameDATA_CONST()
1083 {
1084     static ConstString g_segment_name ("__DATA_CONST");
1085     return g_segment_name;
1086 }
1087 
1088 const ConstString &
1089 ObjectFileMachO::GetSegmentNameOBJC()
1090 {
1091     static ConstString g_segment_name_OBJC ("__OBJC");
1092     return g_segment_name_OBJC;
1093 }
1094 
1095 const ConstString &
1096 ObjectFileMachO::GetSegmentNameLINKEDIT()
1097 {
1098     static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
1099     return g_section_name_LINKEDIT;
1100 }
1101 
1102 const ConstString &
1103 ObjectFileMachO::GetSectionNameEHFrame()
1104 {
1105     static ConstString g_section_name_eh_frame ("__eh_frame");
1106     return g_section_name_eh_frame;
1107 }
1108 
1109 bool
1110 ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
1111                                   lldb::addr_t data_offset,
1112                                   lldb::addr_t data_length)
1113 {
1114     DataExtractor data;
1115     data.SetData (data_sp, data_offset, data_length);
1116     lldb::offset_t offset = 0;
1117     uint32_t magic = data.GetU32(&offset);
1118     return MachHeaderSizeFromMagic(magic) != 0;
1119 }
1120 
1121 ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
1122                                  DataBufferSP& data_sp,
1123                                  lldb::offset_t data_offset,
1124                                  const FileSpec* file,
1125                                  lldb::offset_t file_offset,
1126                                  lldb::offset_t length) :
1127     ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
1128     m_mach_segments(),
1129     m_mach_sections(),
1130     m_entry_point_address(),
1131     m_thread_context_offsets(),
1132     m_thread_context_offsets_valid(false)
1133 {
1134     ::memset (&m_header, 0, sizeof(m_header));
1135     ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1136 }
1137 
1138 ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
1139                                   lldb::DataBufferSP& header_data_sp,
1140                                   const lldb::ProcessSP &process_sp,
1141                                   lldb::addr_t header_addr) :
1142     ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
1143     m_mach_segments(),
1144     m_mach_sections(),
1145     m_entry_point_address(),
1146     m_thread_context_offsets(),
1147     m_thread_context_offsets_valid(false)
1148 {
1149     ::memset (&m_header, 0, sizeof(m_header));
1150     ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1151 }
1152 
1153 bool
1154 ObjectFileMachO::ParseHeader (DataExtractor &data,
1155                               lldb::offset_t *data_offset_ptr,
1156                               llvm::MachO::mach_header &header)
1157 {
1158     data.SetByteOrder (endian::InlHostByteOrder());
1159     // Leave magic in the original byte order
1160     header.magic = data.GetU32(data_offset_ptr);
1161     bool can_parse = false;
1162     bool is_64_bit = false;
1163     switch (header.magic)
1164     {
1165         case MH_MAGIC:
1166             data.SetByteOrder (endian::InlHostByteOrder());
1167             data.SetAddressByteSize(4);
1168             can_parse = true;
1169             break;
1170 
1171         case MH_MAGIC_64:
1172             data.SetByteOrder (endian::InlHostByteOrder());
1173             data.SetAddressByteSize(8);
1174             can_parse = true;
1175             is_64_bit = true;
1176             break;
1177 
1178         case MH_CIGAM:
1179             data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1180             data.SetAddressByteSize(4);
1181             can_parse = true;
1182             break;
1183 
1184         case MH_CIGAM_64:
1185             data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1186             data.SetAddressByteSize(8);
1187             is_64_bit = true;
1188             can_parse = true;
1189             break;
1190 
1191         default:
1192             break;
1193     }
1194 
1195     if (can_parse)
1196     {
1197         data.GetU32(data_offset_ptr, &header.cputype, 6);
1198         if (is_64_bit)
1199             *data_offset_ptr += 4;
1200         return true;
1201     }
1202     else
1203     {
1204         memset(&header, 0, sizeof(header));
1205     }
1206     return false;
1207 }
1208 
1209 bool
1210 ObjectFileMachO::ParseHeader ()
1211 {
1212     ModuleSP module_sp(GetModule());
1213     if (module_sp)
1214     {
1215         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
1216         bool can_parse = false;
1217         lldb::offset_t offset = 0;
1218         m_data.SetByteOrder (endian::InlHostByteOrder());
1219         // Leave magic in the original byte order
1220         m_header.magic = m_data.GetU32(&offset);
1221         switch (m_header.magic)
1222         {
1223         case MH_MAGIC:
1224             m_data.SetByteOrder (endian::InlHostByteOrder());
1225             m_data.SetAddressByteSize(4);
1226             can_parse = true;
1227             break;
1228 
1229         case MH_MAGIC_64:
1230             m_data.SetByteOrder (endian::InlHostByteOrder());
1231             m_data.SetAddressByteSize(8);
1232             can_parse = true;
1233             break;
1234 
1235         case MH_CIGAM:
1236             m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1237             m_data.SetAddressByteSize(4);
1238             can_parse = true;
1239             break;
1240 
1241         case MH_CIGAM_64:
1242             m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1243             m_data.SetAddressByteSize(8);
1244             can_parse = true;
1245             break;
1246 
1247         default:
1248             break;
1249         }
1250 
1251         if (can_parse)
1252         {
1253             m_data.GetU32(&offset, &m_header.cputype, 6);
1254 
1255 
1256             ArchSpec mach_arch;
1257 
1258             if (GetArchitecture (mach_arch))
1259             {
1260                 // Check if the module has a required architecture
1261                 const ArchSpec &module_arch = module_sp->GetArchitecture();
1262                 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
1263                     return false;
1264 
1265                 if (SetModulesArchitecture (mach_arch))
1266                 {
1267                     const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
1268                     if (m_data.GetByteSize() < header_and_lc_size)
1269                     {
1270                         DataBufferSP data_sp;
1271                         ProcessSP process_sp (m_process_wp.lock());
1272                         if (process_sp)
1273                         {
1274                             data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
1275                         }
1276                         else
1277                         {
1278                             // Read in all only the load command data from the file on disk
1279                             data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
1280                             if (data_sp->GetByteSize() != header_and_lc_size)
1281                                 return false;
1282                         }
1283                         if (data_sp)
1284                             m_data.SetData (data_sp);
1285                     }
1286                 }
1287                 return true;
1288             }
1289         }
1290         else
1291         {
1292             memset(&m_header, 0, sizeof(struct mach_header));
1293         }
1294     }
1295     return false;
1296 }
1297 
1298 ByteOrder
1299 ObjectFileMachO::GetByteOrder () const
1300 {
1301     return m_data.GetByteOrder ();
1302 }
1303 
1304 bool
1305 ObjectFileMachO::IsExecutable() const
1306 {
1307     return m_header.filetype == MH_EXECUTE;
1308 }
1309 
1310 uint32_t
1311 ObjectFileMachO::GetAddressByteSize () const
1312 {
1313     return m_data.GetAddressByteSize ();
1314 }
1315 
1316 AddressClass
1317 ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
1318 {
1319     Symtab *symtab = GetSymtab();
1320     if (symtab)
1321     {
1322         Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
1323         if (symbol)
1324         {
1325             if (symbol->ValueIsAddress())
1326             {
1327                 SectionSP section_sp (symbol->GetAddressRef().GetSection());
1328                 if (section_sp)
1329                 {
1330                     const lldb::SectionType section_type = section_sp->GetType();
1331                     switch (section_type)
1332                     {
1333                     case eSectionTypeInvalid:
1334                         return eAddressClassUnknown;
1335 
1336                     case eSectionTypeCode:
1337                         if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
1338                         {
1339                             // For ARM we have a bit in the n_desc field of the symbol
1340                             // that tells us ARM/Thumb which is bit 0x0008.
1341                             if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1342                                 return eAddressClassCodeAlternateISA;
1343                         }
1344                         return eAddressClassCode;
1345 
1346                     case eSectionTypeContainer:
1347                         return eAddressClassUnknown;
1348 
1349                     case eSectionTypeData:
1350                     case eSectionTypeDataCString:
1351                     case eSectionTypeDataCStringPointers:
1352                     case eSectionTypeDataSymbolAddress:
1353                     case eSectionTypeData4:
1354                     case eSectionTypeData8:
1355                     case eSectionTypeData16:
1356                     case eSectionTypeDataPointers:
1357                     case eSectionTypeZeroFill:
1358                     case eSectionTypeDataObjCMessageRefs:
1359                     case eSectionTypeDataObjCCFStrings:
1360                     case eSectionTypeGoSymtab:
1361                         return eAddressClassData;
1362 
1363                     case eSectionTypeDebug:
1364                     case eSectionTypeDWARFDebugAbbrev:
1365                     case eSectionTypeDWARFDebugAddr:
1366                     case eSectionTypeDWARFDebugAranges:
1367                     case eSectionTypeDWARFDebugFrame:
1368                     case eSectionTypeDWARFDebugInfo:
1369                     case eSectionTypeDWARFDebugLine:
1370                     case eSectionTypeDWARFDebugLoc:
1371                     case eSectionTypeDWARFDebugMacInfo:
1372                     case eSectionTypeDWARFDebugPubNames:
1373                     case eSectionTypeDWARFDebugPubTypes:
1374                     case eSectionTypeDWARFDebugRanges:
1375                     case eSectionTypeDWARFDebugStr:
1376                     case eSectionTypeDWARFDebugStrOffsets:
1377                     case eSectionTypeDWARFAppleNames:
1378                     case eSectionTypeDWARFAppleTypes:
1379                     case eSectionTypeDWARFAppleNamespaces:
1380                     case eSectionTypeDWARFAppleObjC:
1381                         return eAddressClassDebug;
1382 
1383                     case eSectionTypeEHFrame:
1384                     case eSectionTypeARMexidx:
1385                     case eSectionTypeARMextab:
1386                     case eSectionTypeCompactUnwind:
1387                         return eAddressClassRuntime;
1388 
1389                     case eSectionTypeELFSymbolTable:
1390                     case eSectionTypeELFDynamicSymbols:
1391                     case eSectionTypeELFRelocationEntries:
1392                     case eSectionTypeELFDynamicLinkInfo:
1393                     case eSectionTypeOther:
1394                         return eAddressClassUnknown;
1395                     }
1396                 }
1397             }
1398 
1399             const SymbolType symbol_type = symbol->GetType();
1400             switch (symbol_type)
1401             {
1402             case eSymbolTypeAny:            return eAddressClassUnknown;
1403             case eSymbolTypeAbsolute:       return eAddressClassUnknown;
1404 
1405             case eSymbolTypeCode:
1406             case eSymbolTypeTrampoline:
1407             case eSymbolTypeResolver:
1408                 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
1409                 {
1410                     // For ARM we have a bit in the n_desc field of the symbol
1411                     // that tells us ARM/Thumb which is bit 0x0008.
1412                     if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1413                         return eAddressClassCodeAlternateISA;
1414                 }
1415                 return eAddressClassCode;
1416 
1417             case eSymbolTypeData:           return eAddressClassData;
1418             case eSymbolTypeRuntime:        return eAddressClassRuntime;
1419             case eSymbolTypeException:      return eAddressClassRuntime;
1420             case eSymbolTypeSourceFile:     return eAddressClassDebug;
1421             case eSymbolTypeHeaderFile:     return eAddressClassDebug;
1422             case eSymbolTypeObjectFile:     return eAddressClassDebug;
1423             case eSymbolTypeCommonBlock:    return eAddressClassDebug;
1424             case eSymbolTypeBlock:          return eAddressClassDebug;
1425             case eSymbolTypeLocal:          return eAddressClassData;
1426             case eSymbolTypeParam:          return eAddressClassData;
1427             case eSymbolTypeVariable:       return eAddressClassData;
1428             case eSymbolTypeVariableType:   return eAddressClassDebug;
1429             case eSymbolTypeLineEntry:      return eAddressClassDebug;
1430             case eSymbolTypeLineHeader:     return eAddressClassDebug;
1431             case eSymbolTypeScopeBegin:     return eAddressClassDebug;
1432             case eSymbolTypeScopeEnd:       return eAddressClassDebug;
1433             case eSymbolTypeAdditional:     return eAddressClassUnknown;
1434             case eSymbolTypeCompiler:       return eAddressClassDebug;
1435             case eSymbolTypeInstrumentation:return eAddressClassDebug;
1436             case eSymbolTypeUndefined:      return eAddressClassUnknown;
1437             case eSymbolTypeObjCClass:      return eAddressClassRuntime;
1438             case eSymbolTypeObjCMetaClass:  return eAddressClassRuntime;
1439             case eSymbolTypeObjCIVar:       return eAddressClassRuntime;
1440             case eSymbolTypeReExported:     return eAddressClassRuntime;
1441             }
1442         }
1443     }
1444     return eAddressClassUnknown;
1445 }
1446 
1447 Symtab *
1448 ObjectFileMachO::GetSymtab()
1449 {
1450     ModuleSP module_sp(GetModule());
1451     if (module_sp)
1452     {
1453         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
1454         if (m_symtab_ap.get() == NULL)
1455         {
1456             m_symtab_ap.reset(new Symtab(this));
1457             Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
1458             ParseSymtab ();
1459             m_symtab_ap->Finalize ();
1460         }
1461     }
1462     return m_symtab_ap.get();
1463 }
1464 
1465 bool
1466 ObjectFileMachO::IsStripped ()
1467 {
1468     if (m_dysymtab.cmd == 0)
1469     {
1470         ModuleSP module_sp(GetModule());
1471         if (module_sp)
1472         {
1473             lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1474             for (uint32_t i=0; i<m_header.ncmds; ++i)
1475             {
1476                 const lldb::offset_t load_cmd_offset = offset;
1477 
1478                 load_command lc;
1479                 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
1480                     break;
1481                 if (lc.cmd == LC_DYSYMTAB)
1482                 {
1483                     m_dysymtab.cmd = lc.cmd;
1484                     m_dysymtab.cmdsize = lc.cmdsize;
1485                     if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
1486                     {
1487                         // Clear m_dysymtab if we were unable to read all items from the load command
1488                         ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1489                     }
1490                 }
1491                 offset = load_cmd_offset + lc.cmdsize;
1492             }
1493         }
1494     }
1495     if (m_dysymtab.cmd)
1496         return m_dysymtab.nlocalsym <= 1;
1497     return false;
1498 }
1499 
1500 void
1501 ObjectFileMachO::CreateSections (SectionList &unified_section_list)
1502 {
1503     if (!m_sections_ap.get())
1504     {
1505         m_sections_ap.reset(new SectionList());
1506 
1507         const bool is_dsym = (m_header.filetype == MH_DSYM);
1508         lldb::user_id_t segID = 0;
1509         lldb::user_id_t sectID = 0;
1510         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1511         uint32_t i;
1512         const bool is_core = GetType() == eTypeCoreFile;
1513         //bool dump_sections = false;
1514         ModuleSP module_sp (GetModule());
1515         // First look up any LC_ENCRYPTION_INFO load commands
1516         typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
1517         EncryptedFileRanges encrypted_file_ranges;
1518         encryption_info_command encryption_cmd;
1519         for (i=0; i<m_header.ncmds; ++i)
1520         {
1521             const lldb::offset_t load_cmd_offset = offset;
1522             if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
1523                 break;
1524 
1525             // LC_ENCRYPTION_INFO and LC_ENCRYPTION_INFO_64 have the same sizes for
1526             // the 3 fields we care about, so treat them the same.
1527             if (encryption_cmd.cmd == LC_ENCRYPTION_INFO || encryption_cmd.cmd == LC_ENCRYPTION_INFO_64)
1528             {
1529                 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
1530                 {
1531                     if (encryption_cmd.cryptid != 0)
1532                     {
1533                         EncryptedFileRanges::Entry entry;
1534                         entry.SetRangeBase(encryption_cmd.cryptoff);
1535                         entry.SetByteSize(encryption_cmd.cryptsize);
1536                         encrypted_file_ranges.Append(entry);
1537                     }
1538                 }
1539             }
1540             offset = load_cmd_offset + encryption_cmd.cmdsize;
1541         }
1542 
1543         bool section_file_addresses_changed = false;
1544 
1545         offset = MachHeaderSizeFromMagic(m_header.magic);
1546 
1547         struct segment_command_64 load_cmd;
1548         for (i=0; i<m_header.ncmds; ++i)
1549         {
1550             const lldb::offset_t load_cmd_offset = offset;
1551             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1552                 break;
1553 
1554             if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64)
1555             {
1556                 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
1557                 {
1558                     bool add_section = true;
1559                     bool add_to_unified = true;
1560                     ConstString const_segname (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
1561 
1562                     SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1563                     if (is_dsym && unified_section_sp)
1564                     {
1565                         if (const_segname == GetSegmentNameLINKEDIT())
1566                         {
1567                             // We need to keep the __LINKEDIT segment private to this object file only
1568                             add_to_unified = false;
1569                         }
1570                         else
1571                         {
1572                             // This is the dSYM file and this section has already been created by
1573                             // the object file, no need to create it.
1574                             add_section = false;
1575                         }
1576                     }
1577                     load_cmd.vmaddr = m_data.GetAddress(&offset);
1578                     load_cmd.vmsize = m_data.GetAddress(&offset);
1579                     load_cmd.fileoff = m_data.GetAddress(&offset);
1580                     load_cmd.filesize = m_data.GetAddress(&offset);
1581                     if (m_length != 0 && load_cmd.filesize != 0)
1582                     {
1583                         if (load_cmd.fileoff > m_length)
1584                         {
1585                             // We have a load command that says it extends past the end of the file.  This is likely
1586                             // a corrupt file.  We don't have any way to return an error condition here (this method
1587                             // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
1588                             // is null out the SectionList vector and if a process has been set up, dump a message
1589                             // to stdout.  The most common case here is core file debugging with a truncated file.
1590                             const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
1591                             module_sp->ReportWarning("load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 "), ignoring this section",
1592                                                    i,
1593                                                    lc_segment_name,
1594                                                    load_cmd.fileoff,
1595                                                    m_length);
1596 
1597                             load_cmd.fileoff = 0;
1598                             load_cmd.filesize = 0;
1599                         }
1600 
1601                         if (load_cmd.fileoff + load_cmd.filesize > m_length)
1602                         {
1603                             // We have a load command that says it extends past the end of the file.  This is likely
1604                             // a corrupt file.  We don't have any way to return an error condition here (this method
1605                             // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
1606                             // is null out the SectionList vector and if a process has been set up, dump a message
1607                             // to stdout.  The most common case here is core file debugging with a truncated file.
1608                             const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
1609                             GetModule()->ReportWarning("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 to match",
1610                                                      i,
1611                                                      lc_segment_name,
1612                                                      load_cmd.fileoff + load_cmd.filesize,
1613                                                      m_length);
1614 
1615                             // Tuncase the length
1616                             load_cmd.filesize = m_length - load_cmd.fileoff;
1617                         }
1618                     }
1619                     if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1620                     {
1621 
1622                         const bool segment_is_encrypted = (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
1623 
1624                         // Keep a list of mach segments around in case we need to
1625                         // get at data that isn't stored in the abstracted Sections.
1626                         m_mach_segments.push_back (load_cmd);
1627 
1628                         // Use a segment ID of the segment index shifted left by 8 so they
1629                         // never conflict with any of the sections.
1630                         SectionSP segment_sp;
1631                         if (add_section && (const_segname || is_core))
1632                         {
1633                             segment_sp.reset(new Section (module_sp,              // Module to which this section belongs
1634                                                           this,                   // Object file to which this sections belongs
1635                                                           ++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
1636                                                           const_segname,          // Name of this section
1637                                                           eSectionTypeContainer,  // This section is a container of other sections.
1638                                                           load_cmd.vmaddr,        // File VM address == addresses as they are found in the object file
1639                                                           load_cmd.vmsize,        // VM size in bytes of this section
1640                                                           load_cmd.fileoff,       // Offset to the data for this section in the file
1641                                                           load_cmd.filesize,      // Size in bytes of this section as found in the file
1642                                                           0,                      // Segments have no alignment information
1643                                                           load_cmd.flags));       // Flags for this section
1644 
1645                             segment_sp->SetIsEncrypted (segment_is_encrypted);
1646                             m_sections_ap->AddSection(segment_sp);
1647                             if (add_to_unified)
1648                                 unified_section_list.AddSection(segment_sp);
1649                         }
1650                         else if (unified_section_sp)
1651                         {
1652                             if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr)
1653                             {
1654                                 // Check to see if the module was read from memory?
1655                                 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid())
1656                                 {
1657                                     // We have a module that is in memory and needs to have its
1658                                     // file address adjusted. We need to do this because when we
1659                                     // load a file from memory, its addresses will be slid already,
1660                                     // yet the addresses in the new symbol file will still be unslid.
1661                                     // Since everything is stored as section offset, this shouldn't
1662                                     // cause any problems.
1663 
1664                                     // Make sure we've parsed the symbol table from the
1665                                     // ObjectFile before we go around changing its Sections.
1666                                     module_sp->GetObjectFile()->GetSymtab();
1667                                     // eh_frame would present the same problems but we parse that on
1668                                     // a per-function basis as-needed so it's more difficult to
1669                                     // remove its use of the Sections.  Realistically, the environments
1670                                     // where this code path will be taken will not have eh_frame sections.
1671 
1672                                     unified_section_sp->SetFileAddress(load_cmd.vmaddr);
1673 
1674                                     // Notify the module that the section addresses have been changed once
1675                                     // we're done so any file-address caches can be updated.
1676                                     section_file_addresses_changed = true;
1677                                 }
1678                             }
1679                             m_sections_ap->AddSection(unified_section_sp);
1680                         }
1681 
1682                         struct section_64 sect64;
1683                         ::memset (&sect64, 0, sizeof(sect64));
1684                         // Push a section into our mach sections for the section at
1685                         // index zero (NO_SECT) if we don't have any mach sections yet...
1686                         if (m_mach_sections.empty())
1687                             m_mach_sections.push_back(sect64);
1688                         uint32_t segment_sect_idx;
1689                         const lldb::user_id_t first_segment_sectID = sectID + 1;
1690 
1691 
1692                         const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8;
1693                         for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
1694                         {
1695                             if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
1696                                 break;
1697                             if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1698                                 break;
1699                             sect64.addr = m_data.GetAddress(&offset);
1700                             sect64.size = m_data.GetAddress(&offset);
1701 
1702                             if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1703                                 break;
1704 
1705                             // Keep a list of mach sections around in case we need to
1706                             // get at data that isn't stored in the abstracted Sections.
1707                             m_mach_sections.push_back (sect64);
1708 
1709                             if (add_section)
1710                             {
1711                                 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1712                                 if (!const_segname)
1713                                 {
1714                                     // We have a segment with no name so we need to conjure up
1715                                     // segments that correspond to the section's segname if there
1716                                     // isn't already such a section. If there is such a section,
1717                                     // we resize the section so that it spans all sections.
1718                                     // We also mark these sections as fake so address matches don't
1719                                     // hit if they land in the gaps between the child sections.
1720                                     const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1721                                     segment_sp = unified_section_list.FindSectionByName (const_segname);
1722                                     if (segment_sp.get())
1723                                     {
1724                                         Section *segment = segment_sp.get();
1725                                         // Grow the section size as needed.
1726                                         const lldb::addr_t sect64_min_addr = sect64.addr;
1727                                         const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1728                                         const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1729                                         const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1730                                         const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1731                                         if (sect64_min_addr >= curr_seg_min_addr)
1732                                         {
1733                                             const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1734                                             // Only grow the section size if needed
1735                                             if (new_seg_byte_size > curr_seg_byte_size)
1736                                                 segment->SetByteSize (new_seg_byte_size);
1737                                         }
1738                                         else
1739                                         {
1740                                             // We need to change the base address of the segment and
1741                                             // adjust the child section offsets for all existing children.
1742                                             const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1743                                             segment->Slide(slide_amount, false);
1744                                             segment->GetChildren().Slide(-slide_amount, false);
1745                                             segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1746                                         }
1747 
1748                                         // Grow the section size as needed.
1749                                         if (sect64.offset)
1750                                         {
1751                                             const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1752                                             const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1753 
1754                                             const lldb::addr_t section_min_file_offset = sect64.offset;
1755                                             const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1756                                             const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1757                                             const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1758                                             segment->SetFileOffset (new_file_offset);
1759                                             segment->SetFileSize (new_file_size);
1760                                         }
1761                                     }
1762                                     else
1763                                     {
1764                                         // Create a fake section for the section's named segment
1765                                         segment_sp.reset(new Section (segment_sp,            // Parent section
1766                                                                       module_sp,             // Module to which this section belongs
1767                                                                       this,                  // Object file to which this section belongs
1768                                                                       ++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
1769                                                                       const_segname,         // Name of this section
1770                                                                       eSectionTypeContainer, // This section is a container of other sections.
1771                                                                       sect64.addr,           // File VM address == addresses as they are found in the object file
1772                                                                       sect64.size,           // VM size in bytes of this section
1773                                                                       sect64.offset,         // Offset to the data for this section in the file
1774                                                                       sect64.offset ? sect64.size : 0,        // Size in bytes of this section as found in the file
1775                                                                       sect64.align,
1776                                                                       load_cmd.flags));      // Flags for this section
1777                                         segment_sp->SetIsFake(true);
1778 
1779                                         m_sections_ap->AddSection(segment_sp);
1780                                         if (add_to_unified)
1781                                             unified_section_list.AddSection(segment_sp);
1782                                         segment_sp->SetIsEncrypted (segment_is_encrypted);
1783                                     }
1784                                 }
1785                                 assert (segment_sp.get());
1786 
1787                                 lldb::SectionType sect_type = eSectionTypeOther;
1788 
1789                                 if (sect64.flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS))
1790                                     sect_type = eSectionTypeCode;
1791                                 else
1792                                 {
1793                                     uint32_t mach_sect_type = sect64.flags & SECTION_TYPE;
1794                                     static ConstString g_sect_name_objc_data ("__objc_data");
1795                                     static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1796                                     static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1797                                     static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1798                                     static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1799                                     static ConstString g_sect_name_objc_const ("__objc_const");
1800                                     static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1801                                     static ConstString g_sect_name_cfstring ("__cfstring");
1802 
1803                                     static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1804                                     static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1805                                     static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1806                                     static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1807                                     static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1808                                     static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1809                                     static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1810                                     static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1811                                     static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1812                                     static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1813                                     static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1814                                     static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1815                                     static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1816                                     static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1817                                     static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1818                                     static ConstString g_sect_name_eh_frame ("__eh_frame");
1819                                     static ConstString g_sect_name_compact_unwind ("__unwind_info");
1820                                     static ConstString g_sect_name_text ("__text");
1821                                     static ConstString g_sect_name_data ("__data");
1822                                     static ConstString g_sect_name_go_symtab ("__gosymtab");
1823 
1824                                     if (section_name == g_sect_name_dwarf_debug_abbrev)
1825                                         sect_type = eSectionTypeDWARFDebugAbbrev;
1826                                     else if (section_name == g_sect_name_dwarf_debug_aranges)
1827                                         sect_type = eSectionTypeDWARFDebugAranges;
1828                                     else if (section_name == g_sect_name_dwarf_debug_frame)
1829                                         sect_type = eSectionTypeDWARFDebugFrame;
1830                                     else if (section_name == g_sect_name_dwarf_debug_info)
1831                                         sect_type = eSectionTypeDWARFDebugInfo;
1832                                     else if (section_name == g_sect_name_dwarf_debug_line)
1833                                         sect_type = eSectionTypeDWARFDebugLine;
1834                                     else if (section_name == g_sect_name_dwarf_debug_loc)
1835                                         sect_type = eSectionTypeDWARFDebugLoc;
1836                                     else if (section_name == g_sect_name_dwarf_debug_macinfo)
1837                                         sect_type = eSectionTypeDWARFDebugMacInfo;
1838                                     else if (section_name == g_sect_name_dwarf_debug_pubnames)
1839                                         sect_type = eSectionTypeDWARFDebugPubNames;
1840                                     else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1841                                         sect_type = eSectionTypeDWARFDebugPubTypes;
1842                                     else if (section_name == g_sect_name_dwarf_debug_ranges)
1843                                         sect_type = eSectionTypeDWARFDebugRanges;
1844                                     else if (section_name == g_sect_name_dwarf_debug_str)
1845                                         sect_type = eSectionTypeDWARFDebugStr;
1846                                     else if (section_name == g_sect_name_dwarf_apple_names)
1847                                         sect_type = eSectionTypeDWARFAppleNames;
1848                                     else if (section_name == g_sect_name_dwarf_apple_types)
1849                                         sect_type = eSectionTypeDWARFAppleTypes;
1850                                     else if (section_name == g_sect_name_dwarf_apple_namespaces)
1851                                         sect_type = eSectionTypeDWARFAppleNamespaces;
1852                                     else if (section_name == g_sect_name_dwarf_apple_objc)
1853                                         sect_type = eSectionTypeDWARFAppleObjC;
1854                                     else if (section_name == g_sect_name_objc_selrefs)
1855                                         sect_type = eSectionTypeDataCStringPointers;
1856                                     else if (section_name == g_sect_name_objc_msgrefs)
1857                                         sect_type = eSectionTypeDataObjCMessageRefs;
1858                                     else if (section_name == g_sect_name_eh_frame)
1859                                         sect_type = eSectionTypeEHFrame;
1860                                     else if (section_name == g_sect_name_compact_unwind)
1861                                         sect_type = eSectionTypeCompactUnwind;
1862                                     else if (section_name == g_sect_name_cfstring)
1863                                         sect_type = eSectionTypeDataObjCCFStrings;
1864                                     else if (section_name == g_sect_name_go_symtab)
1865                                         sect_type = eSectionTypeGoSymtab;
1866                                     else if (section_name == g_sect_name_objc_data ||
1867                                              section_name == g_sect_name_objc_classrefs ||
1868                                              section_name == g_sect_name_objc_superrefs ||
1869                                              section_name == g_sect_name_objc_const ||
1870                                              section_name == g_sect_name_objc_classlist)
1871                                     {
1872                                         sect_type = eSectionTypeDataPointers;
1873                                     }
1874 
1875                                     if (sect_type == eSectionTypeOther)
1876                                     {
1877                                         switch (mach_sect_type)
1878                                         {
1879                                         // TODO: categorize sections by other flags for regular sections
1880                                         case S_REGULAR:
1881                                             if (section_name == g_sect_name_text)
1882                                                 sect_type = eSectionTypeCode;
1883                                             else if (section_name == g_sect_name_data)
1884                                                 sect_type = eSectionTypeData;
1885                                             else
1886                                                 sect_type = eSectionTypeOther;
1887                                             break;
1888                                         case S_ZEROFILL:                   sect_type = eSectionTypeZeroFill; break;
1889                                         case S_CSTRING_LITERALS:           sect_type = eSectionTypeDataCString;    break; // section with only literal C strings
1890                                         case S_4BYTE_LITERALS:             sect_type = eSectionTypeData4;    break; // section with only 4 byte literals
1891                                         case S_8BYTE_LITERALS:             sect_type = eSectionTypeData8;    break; // section with only 8 byte literals
1892                                         case S_LITERAL_POINTERS:           sect_type = eSectionTypeDataPointers;  break; // section with only pointers to literals
1893                                         case S_NON_LAZY_SYMBOL_POINTERS:   sect_type = eSectionTypeDataPointers;  break; // section with only non-lazy symbol pointers
1894                                         case S_LAZY_SYMBOL_POINTERS:       sect_type = eSectionTypeDataPointers;  break; // section with only lazy symbol pointers
1895                                         case S_SYMBOL_STUBS:               sect_type = eSectionTypeCode;  break; // section with only symbol stubs, byte size of stub in the reserved2 field
1896                                         case S_MOD_INIT_FUNC_POINTERS:     sect_type = eSectionTypeDataPointers;    break; // section with only function pointers for initialization
1897                                         case S_MOD_TERM_FUNC_POINTERS:     sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1898                                         case S_COALESCED:                  sect_type = eSectionTypeOther; break;
1899                                         case S_GB_ZEROFILL:                sect_type = eSectionTypeZeroFill; break;
1900                                         case S_INTERPOSING:                sect_type = eSectionTypeCode;  break; // section with only pairs of function pointers for interposing
1901                                         case S_16BYTE_LITERALS:            sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1902                                         case S_DTRACE_DOF:                 sect_type = eSectionTypeDebug; break;
1903                                         case S_LAZY_DYLIB_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers;  break;
1904                                         default: break;
1905                                         }
1906                                     }
1907                                 }
1908 
1909                                 SectionSP section_sp(new Section (segment_sp,
1910                                                                   module_sp,
1911                                                                   this,
1912                                                                   ++sectID,
1913                                                                   section_name,
1914                                                                   sect_type,
1915                                                                   sect64.addr - segment_sp->GetFileAddress(),
1916                                                                   sect64.size,
1917                                                                   sect64.offset,
1918                                                                   sect64.offset == 0 ? 0 : sect64.size,
1919                                                                   sect64.align,
1920                                                                   sect64.flags));
1921                                 // Set the section to be encrypted to match the segment
1922 
1923                                 bool section_is_encrypted = false;
1924                                 if (!segment_is_encrypted && load_cmd.filesize != 0)
1925                                     section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1926 
1927                                 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1928                                 segment_sp->GetChildren().AddSection(section_sp);
1929 
1930                                 if (segment_sp->IsFake())
1931                                 {
1932                                     segment_sp.reset();
1933                                     const_segname.Clear();
1934                                 }
1935                             }
1936                         }
1937                         if (segment_sp && is_dsym)
1938                         {
1939                             if (first_segment_sectID <= sectID)
1940                             {
1941                                 lldb::user_id_t sect_uid;
1942                                 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1943                                 {
1944                                     SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1945                                     SectionSP next_section_sp;
1946                                     if (sect_uid + 1 <= sectID)
1947                                         next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1948 
1949                                     if (curr_section_sp.get())
1950                                     {
1951                                         if (curr_section_sp->GetByteSize() == 0)
1952                                         {
1953                                             if (next_section_sp.get() != NULL)
1954                                                 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1955                                             else
1956                                                 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1957                                         }
1958                                     }
1959                                 }
1960                             }
1961                         }
1962                     }
1963                 }
1964             }
1965             else if (load_cmd.cmd == LC_DYSYMTAB)
1966             {
1967                 m_dysymtab.cmd = load_cmd.cmd;
1968                 m_dysymtab.cmdsize = load_cmd.cmdsize;
1969                 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1970             }
1971 
1972             offset = load_cmd_offset + load_cmd.cmdsize;
1973         }
1974 
1975 
1976         if (section_file_addresses_changed && module_sp.get())
1977         {
1978             module_sp->SectionFileAddressesChanged();
1979         }
1980     }
1981 }
1982 
1983 class MachSymtabSectionInfo
1984 {
1985 public:
1986     MachSymtabSectionInfo (SectionList *section_list) :
1987         m_section_list (section_list),
1988         m_section_infos()
1989     {
1990         // Get the number of sections down to a depth of 1 to include
1991         // all segments and their sections, but no other sections that
1992         // may be added for debug map or
1993         m_section_infos.resize(section_list->GetNumSections(1));
1994     }
1995 
1996     SectionSP
1997     GetSection (uint8_t n_sect, addr_t file_addr)
1998     {
1999         if (n_sect == 0)
2000             return SectionSP();
2001         if (n_sect < m_section_infos.size())
2002         {
2003             if (!m_section_infos[n_sect].section_sp)
2004             {
2005                 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
2006                 m_section_infos[n_sect].section_sp = section_sp;
2007                 if (section_sp)
2008                 {
2009                     m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
2010                     m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
2011                 }
2012                 else
2013                 {
2014                     Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
2015                 }
2016             }
2017             if (m_section_infos[n_sect].vm_range.Contains(file_addr))
2018             {
2019                 // Symbol is in section.
2020                 return m_section_infos[n_sect].section_sp;
2021             }
2022             else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
2023                      m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
2024             {
2025                 // Symbol is in section with zero size, but has the same start
2026                 // address as the section. This can happen with linker symbols
2027                 // (symbols that start with the letter 'l' or 'L'.
2028                 return m_section_infos[n_sect].section_sp;
2029             }
2030         }
2031         return m_section_list->FindSectionContainingFileAddress(file_addr);
2032     }
2033 
2034 protected:
2035     struct SectionInfo
2036     {
2037         SectionInfo () :
2038             vm_range(),
2039             section_sp ()
2040         {
2041         }
2042 
2043         VMRange vm_range;
2044         SectionSP section_sp;
2045     };
2046     SectionList *m_section_list;
2047     std::vector<SectionInfo> m_section_infos;
2048 };
2049 
2050 struct TrieEntry
2051 {
2052     TrieEntry () :
2053         name(),
2054         address(LLDB_INVALID_ADDRESS),
2055         flags (0),
2056         other(0),
2057         import_name()
2058     {
2059     }
2060 
2061     void
2062     Clear ()
2063     {
2064         name.Clear();
2065         address = LLDB_INVALID_ADDRESS;
2066         flags = 0;
2067         other = 0;
2068         import_name.Clear();
2069     }
2070 
2071     void
2072     Dump () const
2073     {
2074         printf ("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"",
2075                 static_cast<unsigned long long>(address),
2076                 static_cast<unsigned long long>(flags),
2077                 static_cast<unsigned long long>(other), name.GetCString());
2078         if (import_name)
2079             printf (" -> \"%s\"\n", import_name.GetCString());
2080         else
2081             printf ("\n");
2082     }
2083     ConstString		name;
2084     uint64_t		address;
2085     uint64_t		flags;
2086     uint64_t		other;
2087     ConstString		import_name;
2088 };
2089 
2090 struct TrieEntryWithOffset
2091 {
2092 	lldb::offset_t nodeOffset;
2093 	TrieEntry entry;
2094 
2095     TrieEntryWithOffset (lldb::offset_t offset) :
2096         nodeOffset (offset),
2097         entry()
2098     {
2099     }
2100 
2101     void
2102     Dump (uint32_t idx) const
2103     {
2104         printf ("[%3u] 0x%16.16llx: ", idx,
2105                 static_cast<unsigned long long>(nodeOffset));
2106         entry.Dump();
2107     }
2108 
2109 	bool
2110     operator<(const TrieEntryWithOffset& other) const
2111     {
2112         return ( nodeOffset < other.nodeOffset );
2113     }
2114 };
2115 
2116 static bool
2117 ParseTrieEntries (DataExtractor &data,
2118                   lldb::offset_t offset,
2119                   const bool is_arm,
2120                   std::vector<llvm::StringRef> &nameSlices,
2121                   std::set<lldb::addr_t> &resolver_addresses,
2122                   std::vector<TrieEntryWithOffset>& output)
2123 {
2124 	if (!data.ValidOffset(offset))
2125         return true;
2126 
2127 	const uint64_t terminalSize = data.GetULEB128(&offset);
2128 	lldb::offset_t children_offset = offset + terminalSize;
2129 	if ( terminalSize != 0 ) {
2130 		TrieEntryWithOffset e (offset);
2131 		e.entry.flags = data.GetULEB128(&offset);
2132         const char *import_name = NULL;
2133 		if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
2134 			e.entry.address = 0;
2135 			e.entry.other = data.GetULEB128(&offset); // dylib ordinal
2136             import_name = data.GetCStr(&offset);
2137 		}
2138 		else {
2139 			e.entry.address = data.GetULEB128(&offset);
2140 			if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
2141             {
2142 				e.entry.other = data.GetULEB128(&offset);
2143                 uint64_t resolver_addr = e.entry.other;
2144                 if (is_arm)
2145                     resolver_addr &= THUMB_ADDRESS_BIT_MASK;
2146                 resolver_addresses.insert(resolver_addr);
2147             }
2148 			else
2149 				e.entry.other = 0;
2150 		}
2151         // Only add symbols that are reexport symbols with a valid import name
2152         if (EXPORT_SYMBOL_FLAGS_REEXPORT & e.entry.flags && import_name && import_name[0])
2153         {
2154             std::string name;
2155             if (!nameSlices.empty())
2156             {
2157                 for (auto name_slice: nameSlices)
2158                     name.append(name_slice.data(), name_slice.size());
2159             }
2160             if (name.size() > 1)
2161             {
2162                 // Skip the leading '_'
2163                 e.entry.name.SetCStringWithLength(name.c_str() + 1,name.size() - 1);
2164             }
2165             if (import_name)
2166             {
2167                 // Skip the leading '_'
2168                 e.entry.import_name.SetCString(import_name+1);
2169             }
2170             output.push_back(e);
2171         }
2172 	}
2173 
2174 	const uint8_t childrenCount = data.GetU8(&children_offset);
2175 	for (uint8_t i=0; i < childrenCount; ++i) {
2176         const char *cstr = data.GetCStr(&children_offset);
2177         if (cstr)
2178             nameSlices.push_back(llvm::StringRef(cstr));
2179         else
2180             return false; // Corrupt data
2181         lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset);
2182 		if (childNodeOffset)
2183         {
2184             if (!ParseTrieEntries(data,
2185                                  childNodeOffset,
2186                                  is_arm,
2187                                  nameSlices,
2188                                  resolver_addresses,
2189                                  output))
2190             {
2191                 return false;
2192             }
2193         }
2194         nameSlices.pop_back();
2195 	}
2196     return true;
2197 }
2198 
2199 // Read the UUID out of a dyld_shared_cache file on-disk.
2200 UUID
2201 ObjectFileMachO::GetSharedCacheUUID (FileSpec dyld_shared_cache, const ByteOrder byte_order, const uint32_t addr_byte_size)
2202 {
2203     UUID dsc_uuid;
2204     DataBufferSP dsc_data_sp = dyld_shared_cache.MemoryMapFileContentsIfLocal(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
2205     if (dsc_data_sp)
2206     {
2207         DataExtractor dsc_header_data (dsc_data_sp, byte_order, addr_byte_size);
2208 
2209         char version_str[7];
2210         lldb::offset_t offset = 0;
2211         memcpy (version_str, dsc_header_data.GetData (&offset, 6), 6);
2212         version_str[6] = '\0';
2213         if (strcmp (version_str, "dyld_v") == 0)
2214         {
2215             offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
2216             uint8_t uuid_bytes[sizeof (uuid_t)];
2217             memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
2218             dsc_uuid.SetBytes (uuid_bytes);
2219         }
2220     }
2221     return dsc_uuid;
2222 }
2223 
2224 size_t
2225 ObjectFileMachO::ParseSymtab ()
2226 {
2227     Timer scoped_timer(__PRETTY_FUNCTION__,
2228                        "ObjectFileMachO::ParseSymtab () module = %s",
2229                        m_file.GetFilename().AsCString(""));
2230     ModuleSP module_sp (GetModule());
2231     if (!module_sp)
2232         return 0;
2233 
2234     struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
2235     struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
2236     struct dyld_info_command dyld_info = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2237     typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
2238     FunctionStarts function_starts;
2239     lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
2240     uint32_t i;
2241     FileSpecList dylib_files;
2242     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
2243     static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2244     static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2245     static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2246 
2247     for (i=0; i<m_header.ncmds; ++i)
2248     {
2249         const lldb::offset_t cmd_offset = offset;
2250         // Read in the load command and load command size
2251         struct load_command lc;
2252         if (m_data.GetU32(&offset, &lc, 2) == NULL)
2253             break;
2254         // Watch for the symbol table load command
2255         switch (lc.cmd)
2256         {
2257         case LC_SYMTAB:
2258             symtab_load_command.cmd = lc.cmd;
2259             symtab_load_command.cmdsize = lc.cmdsize;
2260             // Read in the rest of the symtab load command
2261             if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
2262                 return 0;
2263             if (symtab_load_command.symoff == 0)
2264             {
2265                 if (log)
2266                     module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
2267                 return 0;
2268             }
2269 
2270             if (symtab_load_command.stroff == 0)
2271             {
2272                 if (log)
2273                     module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
2274                 return 0;
2275             }
2276 
2277             if (symtab_load_command.nsyms == 0)
2278             {
2279                 if (log)
2280                     module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
2281                 return 0;
2282             }
2283 
2284             if (symtab_load_command.strsize == 0)
2285             {
2286                 if (log)
2287                     module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
2288                 return 0;
2289             }
2290             break;
2291 
2292         case LC_DYLD_INFO:
2293         case LC_DYLD_INFO_ONLY:
2294             if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10))
2295             {
2296                 dyld_info.cmd = lc.cmd;
2297                 dyld_info.cmdsize = lc.cmdsize;
2298             }
2299             else
2300             {
2301                 memset (&dyld_info, 0, sizeof(dyld_info));
2302             }
2303             break;
2304 
2305         case LC_LOAD_DYLIB:
2306         case LC_LOAD_WEAK_DYLIB:
2307         case LC_REEXPORT_DYLIB:
2308         case LC_LOADFVMLIB:
2309         case LC_LOAD_UPWARD_DYLIB:
2310             {
2311                 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
2312                 const char *path = m_data.PeekCStr(name_offset);
2313                 if (path)
2314                 {
2315                     FileSpec file_spec(path, false);
2316                     // Strip the path if there is @rpath, @executable, etc so we just use the basename
2317                     if (path[0] == '@')
2318                         file_spec.GetDirectory().Clear();
2319 
2320                     if (lc.cmd == LC_REEXPORT_DYLIB)
2321                     {
2322                         m_reexported_dylibs.AppendIfUnique(file_spec);
2323                     }
2324 
2325                     dylib_files.Append(file_spec);
2326                 }
2327             }
2328             break;
2329 
2330         case LC_FUNCTION_STARTS:
2331             function_starts_load_command.cmd = lc.cmd;
2332             function_starts_load_command.cmdsize = lc.cmdsize;
2333             if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
2334                 memset (&function_starts_load_command, 0, sizeof(function_starts_load_command));
2335             break;
2336 
2337         default:
2338             break;
2339         }
2340         offset = cmd_offset + lc.cmdsize;
2341     }
2342 
2343     if (symtab_load_command.cmd)
2344     {
2345         Symtab *symtab = m_symtab_ap.get();
2346         SectionList *section_list = GetSectionList();
2347         if (section_list == NULL)
2348             return 0;
2349 
2350         const uint32_t addr_byte_size = m_data.GetAddressByteSize();
2351         const ByteOrder byte_order = m_data.GetByteOrder();
2352         bool bit_width_32 = addr_byte_size == 4;
2353         const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
2354 
2355         DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
2356         DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
2357         DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
2358         DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
2359         DataExtractor dyld_trie_data (NULL, 0, byte_order, addr_byte_size);
2360 
2361         const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
2362         const addr_t strtab_data_byte_size = symtab_load_command.strsize;
2363         addr_t strtab_addr = LLDB_INVALID_ADDRESS;
2364 
2365         ProcessSP process_sp (m_process_wp.lock());
2366         Process *process = process_sp.get();
2367 
2368         uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
2369 
2370         if (process && m_header.filetype != llvm::MachO::MH_OBJECT)
2371         {
2372             Target &target = process->GetTarget();
2373 
2374             memory_module_load_level = target.GetMemoryModuleLoadLevel();
2375 
2376             SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2377             // Reading mach file from memory in a process or core file...
2378 
2379             if (linkedit_section_sp)
2380             {
2381                 addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
2382                 if (linkedit_load_addr == LLDB_INVALID_ADDRESS)
2383                 {
2384                     // We might be trying to access the symbol table before the __LINKEDIT's load
2385                     // address has been set in the target. We can't fail to read the symbol table,
2386                     // so calculate the right address manually
2387                     linkedit_load_addr = CalculateSectionLoadAddressForMemoryImage(m_memory_addr, GetMachHeaderSection(), linkedit_section_sp.get());
2388                 }
2389 
2390                 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2391                 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
2392                 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
2393 
2394                 bool data_was_read = false;
2395 
2396 #if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
2397                 if (m_header.flags & 0x80000000u && process->GetAddressByteSize() == sizeof (void*))
2398                 {
2399                     // This mach-o memory file is in the dyld shared cache. If this
2400                     // program is not remote and this is iOS, then this process will
2401                     // share the same shared cache as the process we are debugging and
2402                     // we can read the entire __LINKEDIT from the address space in this
2403                     // process. This is a needed optimization that is used for local iOS
2404                     // debugging only since all shared libraries in the shared cache do
2405                     // not have corresponding files that exist in the file system of the
2406                     // device. They have been combined into a single file. This means we
2407                     // always have to load these files from memory. All of the symbol and
2408                     // string tables from all of the __LINKEDIT sections from the shared
2409                     // libraries in the shared cache have been merged into a single large
2410                     // symbol and string table. Reading all of this symbol and string table
2411                     // data across can slow down debug launch times, so we optimize this by
2412                     // reading the memory for the __LINKEDIT section from this process.
2413 
2414                     UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
2415                     UUID process_shared_cache(GetProcessSharedCacheUUID(process));
2416                     bool use_lldb_cache = true;
2417                     if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
2418                     {
2419                             use_lldb_cache = false;
2420                             ModuleSP module_sp (GetModule());
2421                             if (module_sp)
2422                                 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
2423 
2424                     }
2425 
2426                     PlatformSP platform_sp (target.GetPlatform());
2427                     if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
2428                     {
2429                         data_was_read = true;
2430                         nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
2431                         strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
2432                         if (function_starts_load_command.cmd)
2433                         {
2434                             const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2435                             function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
2436                         }
2437                     }
2438                 }
2439 #endif
2440 
2441                 if (!data_was_read)
2442                 {
2443                     if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
2444                     {
2445                         DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
2446                         if (nlist_data_sp)
2447                             nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2448                         // Load strings individually from memory when loading from memory since shared cache
2449                         // string tables contain strings for all symbols from all shared cached libraries
2450                         //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
2451                         //if (strtab_data_sp)
2452                         //    strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
2453                         if (m_dysymtab.nindirectsyms != 0)
2454                         {
2455                             const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
2456                             DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
2457                             if (indirect_syms_data_sp)
2458                                 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
2459                         }
2460                     }
2461 
2462                     if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
2463                     {
2464                         if (function_starts_load_command.cmd)
2465                         {
2466                             const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2467                             DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
2468                             if (func_start_data_sp)
2469                                 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
2470                         }
2471                     }
2472                 }
2473             }
2474         }
2475         else
2476         {
2477             nlist_data.SetData (m_data,
2478                                 symtab_load_command.symoff,
2479                                 nlist_data_byte_size);
2480             strtab_data.SetData (m_data,
2481                                  symtab_load_command.stroff,
2482                                  strtab_data_byte_size);
2483 
2484             if (dyld_info.export_size > 0)
2485             {
2486                 dyld_trie_data.SetData (m_data,
2487                                         dyld_info.export_off,
2488                                         dyld_info.export_size);
2489             }
2490 
2491             if (m_dysymtab.nindirectsyms != 0)
2492             {
2493                 indirect_symbol_index_data.SetData (m_data,
2494                                                     m_dysymtab.indirectsymoff,
2495                                                     m_dysymtab.nindirectsyms * 4);
2496             }
2497             if (function_starts_load_command.cmd)
2498             {
2499                 function_starts_data.SetData (m_data,
2500                                               function_starts_load_command.dataoff,
2501                                               function_starts_load_command.datasize);
2502             }
2503         }
2504 
2505         if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete)
2506         {
2507             if (log)
2508                 module_sp->LogMessage(log, "failed to read nlist data");
2509             return 0;
2510         }
2511 
2512         const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2513         if (!have_strtab_data)
2514         {
2515             if (process)
2516             {
2517                 if (strtab_addr == LLDB_INVALID_ADDRESS)
2518                 {
2519                     if (log)
2520                         module_sp->LogMessage(log, "failed to locate the strtab in memory");
2521                     return 0;
2522                 }
2523             }
2524             else
2525             {
2526                 if (log)
2527                     module_sp->LogMessage(log, "failed to read strtab data");
2528                 return 0;
2529             }
2530         }
2531 
2532         const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
2533         const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
2534         const ConstString &g_segment_name_DATA_DIRTY = GetSegmentNameDATA_DIRTY();
2535         const ConstString &g_segment_name_DATA_CONST = GetSegmentNameDATA_CONST();
2536         const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
2537         const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
2538         SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
2539         SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
2540         SectionSP data_dirty_section_sp(section_list->FindSectionByName(g_segment_name_DATA_DIRTY));
2541         SectionSP data_const_section_sp(section_list->FindSectionByName(g_segment_name_DATA_CONST));
2542         SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
2543         SectionSP eh_frame_section_sp;
2544         if (text_section_sp.get())
2545             eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
2546         else
2547             eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
2548 
2549         const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
2550 
2551         // lldb works best if it knows the start address of all functions in a module.
2552         // Linker symbols or debug info are normally the best source of information for start addr / size but
2553         // they may be stripped in a released binary.
2554         // Two additional sources of information exist in Mach-O binaries:
2555         //    LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
2556         //                         binary, relative to the text section.
2557         //    eh_frame           - the eh_frame FDEs have the start addr & size of each function
2558         //  LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
2559         //  Binaries built to run on older releases may need to use eh_frame information.
2560 
2561         if (text_section_sp && function_starts_data.GetByteSize())
2562         {
2563             FunctionStarts::Entry function_start_entry;
2564             function_start_entry.data = false;
2565             lldb::offset_t function_start_offset = 0;
2566             function_start_entry.addr = text_section_sp->GetFileAddress();
2567             uint64_t delta;
2568             while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
2569             {
2570                 // Now append the current entry
2571                 function_start_entry.addr += delta;
2572                 function_starts.Append(function_start_entry);
2573             }
2574         }
2575         else
2576         {
2577             // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
2578             // but it doesn't actually have the eh_frame content.  And if we have a dSYM, we don't need to do any
2579             // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
2580             // the module.
2581             if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
2582             {
2583                 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindEHFrame, true);
2584                 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
2585                 eh_frame.GetFunctionAddressAndSizeVector (functions);
2586                 addr_t text_base_addr = text_section_sp->GetFileAddress();
2587                 size_t count = functions.GetSize();
2588                 for (size_t i = 0; i < count; ++i)
2589                 {
2590                     const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
2591                     if (func)
2592                     {
2593                         FunctionStarts::Entry function_start_entry;
2594                         function_start_entry.addr = func->base - text_base_addr;
2595                         function_starts.Append(function_start_entry);
2596                     }
2597                 }
2598             }
2599         }
2600 
2601         const size_t function_starts_count = function_starts.GetSize();
2602 
2603         const user_id_t TEXT_eh_frame_sectID =
2604             eh_frame_section_sp.get() ? eh_frame_section_sp->GetID()
2605                                       : static_cast<user_id_t>(NO_SECT);
2606 
2607         lldb::offset_t nlist_data_offset = 0;
2608 
2609         uint32_t N_SO_index = UINT32_MAX;
2610 
2611         MachSymtabSectionInfo section_info (section_list);
2612         std::vector<uint32_t> N_FUN_indexes;
2613         std::vector<uint32_t> N_NSYM_indexes;
2614         std::vector<uint32_t> N_INCL_indexes;
2615         std::vector<uint32_t> N_BRAC_indexes;
2616         std::vector<uint32_t> N_COMM_indexes;
2617         typedef std::multimap <uint64_t, uint32_t> ValueToSymbolIndexMap;
2618         typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
2619         typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
2620         ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2621         ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
2622         ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
2623         // Any symbols that get merged into another will get an entry
2624         // in this map so we know
2625         NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2626         uint32_t nlist_idx = 0;
2627         Symbol *symbol_ptr = NULL;
2628 
2629         uint32_t sym_idx = 0;
2630         Symbol *sym = NULL;
2631         size_t num_syms = 0;
2632         std::string memory_symbol_name;
2633         uint32_t unmapped_local_symbols_found = 0;
2634 
2635         std::vector<TrieEntryWithOffset> trie_entries;
2636         std::set<lldb::addr_t> resolver_addresses;
2637 
2638         if (dyld_trie_data.GetByteSize() > 0)
2639         {
2640             std::vector<llvm::StringRef> nameSlices;
2641             ParseTrieEntries (dyld_trie_data,
2642                               0,
2643                               is_arm,
2644                               nameSlices,
2645                               resolver_addresses,
2646                               trie_entries);
2647 
2648             ConstString text_segment_name ("__TEXT");
2649             SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2650             if (text_segment_sp)
2651             {
2652                 const lldb::addr_t text_segment_file_addr = text_segment_sp->GetFileAddress();
2653                 if (text_segment_file_addr != LLDB_INVALID_ADDRESS)
2654                 {
2655                     for (auto &e : trie_entries)
2656                         e.entry.address += text_segment_file_addr;
2657                 }
2658             }
2659         }
2660 
2661         typedef std::set<ConstString> IndirectSymbols;
2662         IndirectSymbols indirect_symbol_names;
2663 
2664 #if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
2665 
2666         // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
2667         // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
2668         // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
2669         // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
2670         // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
2671         // nlist parser to ignore all LOCAL symbols.
2672 
2673         if (m_header.flags & 0x80000000u)
2674         {
2675             // Before we can start mapping the DSC, we need to make certain the target process is actually
2676             // using the cache we can find.
2677 
2678             // Next we need to determine the correct path for the dyld shared cache.
2679 
2680             ArchSpec header_arch;
2681             GetArchitecture(header_arch);
2682             char dsc_path[PATH_MAX];
2683             char dsc_path_development[PATH_MAX];
2684 
2685             snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
2686                      "/System/Library/Caches/com.apple.dyld/",  /* IPHONE_DYLD_SHARED_CACHE_DIR */
2687                      "dyld_shared_cache_",          /* DYLD_SHARED_CACHE_BASE_NAME */
2688                      header_arch.GetArchitectureName());
2689 
2690             snprintf(dsc_path_development, sizeof(dsc_path), "%s%s%s%s",
2691                      "/System/Library/Caches/com.apple.dyld/",  /* IPHONE_DYLD_SHARED_CACHE_DIR */
2692                      "dyld_shared_cache_",          /* DYLD_SHARED_CACHE_BASE_NAME */
2693                      header_arch.GetArchitectureName(),
2694                      ".development");
2695 
2696             FileSpec dsc_nondevelopment_filespec(dsc_path, false);
2697             FileSpec dsc_development_filespec(dsc_path_development, false);
2698             FileSpec dsc_filespec;
2699 
2700             UUID dsc_uuid;
2701             UUID process_shared_cache_uuid;
2702 
2703             if (process)
2704             {
2705                 process_shared_cache_uuid = GetProcessSharedCacheUUID(process);
2706             }
2707 
2708             // First see if we can find an exact match for the inferior process shared cache UUID in
2709             // the development or non-development shared caches on disk.
2710             if (process_shared_cache_uuid.IsValid())
2711             {
2712                 if (dsc_development_filespec.Exists())
2713                 {
2714                     UUID dsc_development_uuid = GetSharedCacheUUID (dsc_development_filespec, byte_order, addr_byte_size);
2715                     if (dsc_development_uuid.IsValid() && dsc_development_uuid == process_shared_cache_uuid)
2716                     {
2717                         dsc_filespec = dsc_development_filespec;
2718                         dsc_uuid = dsc_development_uuid;
2719                     }
2720                 }
2721                 if (!dsc_uuid.IsValid() && dsc_nondevelopment_filespec.Exists())
2722                 {
2723                     UUID dsc_nondevelopment_uuid = GetSharedCacheUUID (dsc_nondevelopment_filespec, byte_order, addr_byte_size);
2724                     if (dsc_nondevelopment_uuid.IsValid() && dsc_nondevelopment_uuid == process_shared_cache_uuid)
2725                     {
2726                         dsc_filespec = dsc_nondevelopment_filespec;
2727                         dsc_uuid = dsc_nondevelopment_uuid;
2728                     }
2729                 }
2730             }
2731 
2732             // Failing a UUID match, prefer the development dyld_shared cache if both are present.
2733             if (!dsc_filespec.Exists())
2734             {
2735                 if (dsc_development_filespec.Exists())
2736                 {
2737                     dsc_filespec = dsc_development_filespec;
2738                 }
2739                 else
2740                 {
2741                     dsc_filespec = dsc_nondevelopment_filespec;
2742                 }
2743             }
2744 
2745             /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
2746                The dyld_cache_local_symbols_info structure gives us three things:
2747                  1. The start and count of the nlist records in the dyld_shared_cache file
2748                  2. The start and size of the strings for these nlist records
2749                  3. The start and count of dyld_cache_local_symbols_entry entries
2750 
2751                There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
2752                The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
2753                The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
2754                and the count of how many nlist records there are for this dylib/framework.
2755             */
2756 
2757             // Process the dyld shared cache header to find the unmapped symbols
2758 
2759             DataBufferSP dsc_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
2760             if (!dsc_uuid.IsValid())
2761             {
2762                 dsc_uuid = GetSharedCacheUUID (dsc_filespec, byte_order, addr_byte_size);
2763             }
2764             if (dsc_data_sp)
2765             {
2766                 DataExtractor dsc_header_data (dsc_data_sp, byte_order, addr_byte_size);
2767 
2768                 bool uuid_match = true;
2769                 if (dsc_uuid.IsValid() && process)
2770                 {
2771                     if (process_shared_cache_uuid.IsValid() && dsc_uuid != process_shared_cache_uuid)
2772                     {
2773                         // The on-disk dyld_shared_cache file is not the same as the one in this
2774                         // process' memory, don't use it.
2775                         uuid_match = false;
2776                         ModuleSP module_sp (GetModule());
2777                         if (module_sp)
2778                             module_sp->ReportWarning ("process shared cache does not match on-disk dyld_shared_cache file, some symbol names will be missing.");
2779                     }
2780                 }
2781 
2782                 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
2783 
2784                 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
2785 
2786                 // If the mappingOffset points to a location inside the header, we've
2787                 // opened an old dyld shared cache, and should not proceed further.
2788                 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v1))
2789                 {
2790 
2791                     DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
2792                     DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
2793                     offset = 0;
2794 
2795                     // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
2796                     // in the shared library cache need to be adjusted by an offset to match up with the
2797                     // dylibOffset identifying field in the dyld_cache_local_symbol_entry's.  This offset is
2798                     // recorded in mapping_offset_value.
2799                     const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
2800 
2801                     offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
2802                     uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
2803                     uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
2804 
2805                     if (localSymbolsOffset && localSymbolsSize)
2806                     {
2807                         // Map the local symbols
2808                         if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(localSymbolsOffset, localSymbolsSize))
2809                         {
2810                             DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
2811 
2812                             offset = 0;
2813 
2814                             typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap;
2815                             typedef std::map<uint32_t, ConstString> SymbolIndexToName;
2816                             UndefinedNameToDescMap undefined_name_to_desc;
2817                             SymbolIndexToName reexport_shlib_needs_fixup;
2818 
2819 
2820                             // Read the local_symbols_infos struct in one shot
2821                             struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2822                             dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
2823 
2824                             SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
2825 
2826                             uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
2827 
2828                             offset = local_symbols_info.entriesOffset;
2829                             for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
2830                             {
2831                                 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
2832                                 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
2833                                 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
2834                                 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
2835 
2836                                 if (header_file_offset == local_symbols_entry.dylibOffset)
2837                                 {
2838                                     unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2839 
2840                                     // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
2841                                     sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2842                                     num_syms = symtab->GetNumSymbols();
2843 
2844                                     nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2845                                     uint32_t string_table_offset = local_symbols_info.stringsOffset;
2846 
2847                                     for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
2848                                     {
2849                                         /////////////////////////////
2850                                         {
2851                                             struct nlist_64 nlist;
2852                                             if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2853                                                 break;
2854 
2855                                             nlist.n_strx  = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2856                                             nlist.n_type  = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2857                                             nlist.n_sect  = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2858                                             nlist.n_desc  = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2859                                             nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2860 
2861                                             SymbolType type = eSymbolTypeInvalid;
2862                                             const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2863 
2864                                             if (symbol_name == NULL)
2865                                             {
2866                                                 // No symbol should be NULL, even the symbols with no
2867                                                 // string values should have an offset zero which points
2868                                                 // to an empty C-string
2869                                                 Host::SystemLog (Host::eSystemLogError,
2870                                                                  "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
2871                                                                  entry_index,
2872                                                                  nlist.n_strx,
2873                                                                  module_sp->GetFileSpec().GetPath().c_str());
2874                                                 continue;
2875                                             }
2876                                             if (symbol_name[0] == '\0')
2877                                                 symbol_name = NULL;
2878 
2879                                             const char *symbol_name_non_abi_mangled = NULL;
2880 
2881                                             SectionSP symbol_section;
2882                                             uint32_t symbol_byte_size = 0;
2883                                             bool add_nlist = true;
2884                                             bool is_debug = ((nlist.n_type & N_STAB) != 0);
2885                                             bool demangled_is_synthesized = false;
2886                                             bool is_gsym = false;
2887                                             bool set_value = true;
2888 
2889                                             assert (sym_idx < num_syms);
2890 
2891                                             sym[sym_idx].SetDebug (is_debug);
2892 
2893                                             if (is_debug)
2894                                             {
2895                                                 switch (nlist.n_type)
2896                                                 {
2897                                                     case N_GSYM:
2898                                                         // global symbol: name,,NO_SECT,type,0
2899                                                         // Sometimes the N_GSYM value contains the address.
2900 
2901                                                         // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data.  They
2902                                                         // have the same address, but we want to ensure that we always find only the real symbol,
2903                                                         // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2904                                                         // symbol type.  This is a temporary hack to make sure the ObjectiveC symbols get treated
2905                                                         // correctly.  To do this right, we should coalesce all the GSYM & global symbols that have the
2906                                                         // same address.
2907 
2908                                                         is_gsym = true;
2909                                                         sym[sym_idx].SetExternal(true);
2910 
2911                                                         if (symbol_name && symbol_name[0] == '_' && symbol_name[1] ==  'O')
2912                                                         {
2913                                                             llvm::StringRef symbol_name_ref(symbol_name);
2914                                                             if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2915                                                             {
2916                                                                 symbol_name_non_abi_mangled = symbol_name + 1;
2917                                                                 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2918                                                                 type = eSymbolTypeObjCClass;
2919                                                                 demangled_is_synthesized = true;
2920 
2921                                                             }
2922                                                             else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2923                                                             {
2924                                                                 symbol_name_non_abi_mangled = symbol_name + 1;
2925                                                                 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2926                                                                 type = eSymbolTypeObjCMetaClass;
2927                                                                 demangled_is_synthesized = true;
2928                                                             }
2929                                                             else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2930                                                             {
2931                                                                 symbol_name_non_abi_mangled = symbol_name + 1;
2932                                                                 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2933                                                                 type = eSymbolTypeObjCIVar;
2934                                                                 demangled_is_synthesized = true;
2935                                                             }
2936                                                         }
2937                                                         else
2938                                                         {
2939                                                             if (nlist.n_value != 0)
2940                                                                 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2941                                                             type = eSymbolTypeData;
2942                                                         }
2943                                                         break;
2944 
2945                                                     case N_FNAME:
2946                                                         // procedure name (f77 kludge): name,,NO_SECT,0,0
2947                                                         type = eSymbolTypeCompiler;
2948                                                         break;
2949 
2950                                                     case N_FUN:
2951                                                         // procedure: name,,n_sect,linenumber,address
2952                                                         if (symbol_name)
2953                                                         {
2954                                                             type = eSymbolTypeCode;
2955                                                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2956 
2957                                                             N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
2958                                                             // We use the current number of symbols in the symbol table in lieu of
2959                                                             // using nlist_idx in case we ever start trimming entries out
2960                                                             N_FUN_indexes.push_back(sym_idx);
2961                                                         }
2962                                                         else
2963                                                         {
2964                                                             type = eSymbolTypeCompiler;
2965 
2966                                                             if ( !N_FUN_indexes.empty() )
2967                                                             {
2968                                                                 // Copy the size of the function into the original STAB entry so we don't have
2969                                                                 // to hunt for it later
2970                                                                 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2971                                                                 N_FUN_indexes.pop_back();
2972                                                                 // We don't really need the end function STAB as it contains the size which
2973                                                                 // we already placed with the original symbol, so don't add it if we want a
2974                                                                 // minimal symbol table
2975                                                                 add_nlist = false;
2976                                                             }
2977                                                         }
2978                                                         break;
2979 
2980                                                     case N_STSYM:
2981                                                         // static symbol: name,,n_sect,type,address
2982                                                         N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
2983                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2984                                                         if (symbol_name && symbol_name[0])
2985                                                         {
2986                                                             type = ObjectFile::GetSymbolTypeFromName(symbol_name+1, eSymbolTypeData);
2987                                                         }
2988                                                         break;
2989 
2990                                                     case N_LCSYM:
2991                                                         // .lcomm symbol: name,,n_sect,type,address
2992                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2993                                                         type = eSymbolTypeCommonBlock;
2994                                                         break;
2995 
2996                                                     case N_BNSYM:
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                                                         // Skip these if we want minimal symbol tables
3000                                                         add_nlist = false;
3001                                                         break;
3002 
3003                                                     case N_ENSYM:
3004                                                         // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3005                                                         // so that we can always skip the entire symbol if we need to navigate
3006                                                         // more quickly at the source level when parsing STABS
3007                                                         // Skip these if we want minimal symbol tables
3008                                                         add_nlist = false;
3009                                                         break;
3010 
3011                                                     case N_OPT:
3012                                                         // emitted with gcc2_compiled and in gcc source
3013                                                         type = eSymbolTypeCompiler;
3014                                                         break;
3015 
3016                                                     case N_RSYM:
3017                                                         // register sym: name,,NO_SECT,type,register
3018                                                         type = eSymbolTypeVariable;
3019                                                         break;
3020 
3021                                                     case N_SLINE:
3022                                                         // src line: 0,,n_sect,linenumber,address
3023                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3024                                                         type = eSymbolTypeLineEntry;
3025                                                         break;
3026 
3027                                                     case N_SSYM:
3028                                                         // structure elt: name,,NO_SECT,type,struct_offset
3029                                                         type = eSymbolTypeVariableType;
3030                                                         break;
3031 
3032                                                     case N_SO:
3033                                                         // source file name
3034                                                         type = eSymbolTypeSourceFile;
3035                                                         if (symbol_name == NULL)
3036                                                         {
3037                                                             add_nlist = false;
3038                                                             if (N_SO_index != UINT32_MAX)
3039                                                             {
3040                                                                 // Set the size of the N_SO to the terminating index of this N_SO
3041                                                                 // so that we can always skip the entire N_SO if we need to navigate
3042                                                                 // more quickly at the source level when parsing STABS
3043                                                                 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
3044                                                                 symbol_ptr->SetByteSize(sym_idx);
3045                                                                 symbol_ptr->SetSizeIsSibling(true);
3046                                                             }
3047                                                             N_NSYM_indexes.clear();
3048                                                             N_INCL_indexes.clear();
3049                                                             N_BRAC_indexes.clear();
3050                                                             N_COMM_indexes.clear();
3051                                                             N_FUN_indexes.clear();
3052                                                             N_SO_index = UINT32_MAX;
3053                                                         }
3054                                                         else
3055                                                         {
3056                                                             // We use the current number of symbols in the symbol table in lieu of
3057                                                             // using nlist_idx in case we ever start trimming entries out
3058                                                             const bool N_SO_has_full_path = symbol_name[0] == '/';
3059                                                             if (N_SO_has_full_path)
3060                                                             {
3061                                                                 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3062                                                                 {
3063                                                                     // We have two consecutive N_SO entries where the first contains a directory
3064                                                                     // and the second contains a full path.
3065                                                                     sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
3066                                                                     m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3067                                                                     add_nlist = false;
3068                                                                 }
3069                                                                 else
3070                                                                 {
3071                                                                     // This is the first entry in a N_SO that contains a directory or
3072                                                                     // a full path to the source file
3073                                                                     N_SO_index = sym_idx;
3074                                                                 }
3075                                                             }
3076                                                             else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3077                                                             {
3078                                                                 // This is usually the second N_SO entry that contains just the filename,
3079                                                                 // so here we combine it with the first one if we are minimizing the symbol table
3080                                                                 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
3081                                                                 if (so_path && so_path[0])
3082                                                                 {
3083                                                                     std::string full_so_path (so_path);
3084                                                                     const size_t double_slash_pos = full_so_path.find("//");
3085                                                                     if (double_slash_pos != std::string::npos)
3086                                                                     {
3087                                                                         // The linker has been generating bad N_SO entries with doubled up paths
3088                                                                         // in the format "%s%s" where the first string in the DW_AT_comp_dir,
3089                                                                         // and the second is the directory for the source file so you end up with
3090                                                                         // a path that looks like "/tmp/src//tmp/src/"
3091                                                                         FileSpec so_dir(so_path, false);
3092                                                                         if (!so_dir.Exists())
3093                                                                         {
3094                                                                             so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3095                                                                             if (so_dir.Exists())
3096                                                                             {
3097                                                                                 // Trim off the incorrect path
3098                                                                                 full_so_path.erase(0, double_slash_pos + 1);
3099                                                                             }
3100                                                                         }
3101                                                                     }
3102                                                                     if (*full_so_path.rbegin() != '/')
3103                                                                         full_so_path += '/';
3104                                                                     full_so_path += symbol_name;
3105                                                                     sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3106                                                                     add_nlist = false;
3107                                                                     m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3108                                                                 }
3109                                                             }
3110                                                             else
3111                                                             {
3112                                                                 // This could be a relative path to a N_SO
3113                                                                 N_SO_index = sym_idx;
3114                                                             }
3115                                                         }
3116                                                         break;
3117 
3118                                                     case N_OSO:
3119                                                         // object file name: name,,0,0,st_mtime
3120                                                         type = eSymbolTypeObjectFile;
3121                                                         break;
3122 
3123                                                     case N_LSYM:
3124                                                         // local sym: name,,NO_SECT,type,offset
3125                                                         type = eSymbolTypeLocal;
3126                                                         break;
3127 
3128                                                         //----------------------------------------------------------------------
3129                                                         // INCL scopes
3130                                                         //----------------------------------------------------------------------
3131                                                     case N_BINCL:
3132                                                         // include file beginning: name,,NO_SECT,0,sum
3133                                                         // We use the current number of symbols in the symbol table in lieu of
3134                                                         // using nlist_idx in case we ever start trimming entries out
3135                                                         N_INCL_indexes.push_back(sym_idx);
3136                                                         type = eSymbolTypeScopeBegin;
3137                                                         break;
3138 
3139                                                     case N_EINCL:
3140                                                         // include file end: name,,NO_SECT,0,0
3141                                                         // Set the size of the N_BINCL to the terminating index of this N_EINCL
3142                                                         // so that we can always skip the entire symbol if we need to navigate
3143                                                         // more quickly at the source level when parsing STABS
3144                                                         if ( !N_INCL_indexes.empty() )
3145                                                         {
3146                                                             symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3147                                                             symbol_ptr->SetByteSize(sym_idx + 1);
3148                                                             symbol_ptr->SetSizeIsSibling(true);
3149                                                             N_INCL_indexes.pop_back();
3150                                                         }
3151                                                         type = eSymbolTypeScopeEnd;
3152                                                         break;
3153 
3154                                                     case N_SOL:
3155                                                         // #included file name: name,,n_sect,0,address
3156                                                         type = eSymbolTypeHeaderFile;
3157 
3158                                                         // We currently don't use the header files on darwin
3159                                                         add_nlist = false;
3160                                                         break;
3161 
3162                                                     case N_PARAMS:
3163                                                         // compiler parameters: name,,NO_SECT,0,0
3164                                                         type = eSymbolTypeCompiler;
3165                                                         break;
3166 
3167                                                     case N_VERSION:
3168                                                         // compiler version: name,,NO_SECT,0,0
3169                                                         type = eSymbolTypeCompiler;
3170                                                         break;
3171 
3172                                                     case N_OLEVEL:
3173                                                         // compiler -O level: name,,NO_SECT,0,0
3174                                                         type = eSymbolTypeCompiler;
3175                                                         break;
3176 
3177                                                     case N_PSYM:
3178                                                         // parameter: name,,NO_SECT,type,offset
3179                                                         type = eSymbolTypeVariable;
3180                                                         break;
3181 
3182                                                     case N_ENTRY:
3183                                                         // alternate entry: name,,n_sect,linenumber,address
3184                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3185                                                         type = eSymbolTypeLineEntry;
3186                                                         break;
3187 
3188                                                         //----------------------------------------------------------------------
3189                                                         // Left and Right Braces
3190                                                         //----------------------------------------------------------------------
3191                                                     case N_LBRAC:
3192                                                         // left bracket: 0,,NO_SECT,nesting level,address
3193                                                         // We use the current number of symbols in the symbol table in lieu of
3194                                                         // using nlist_idx in case we ever start trimming entries out
3195                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3196                                                         N_BRAC_indexes.push_back(sym_idx);
3197                                                         type = eSymbolTypeScopeBegin;
3198                                                         break;
3199 
3200                                                     case N_RBRAC:
3201                                                         // right bracket: 0,,NO_SECT,nesting level,address
3202                                                         // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3203                                                         // so that we can always skip the entire symbol if we need to navigate
3204                                                         // more quickly at the source level when parsing STABS
3205                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3206                                                         if ( !N_BRAC_indexes.empty() )
3207                                                         {
3208                                                             symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3209                                                             symbol_ptr->SetByteSize(sym_idx + 1);
3210                                                             symbol_ptr->SetSizeIsSibling(true);
3211                                                             N_BRAC_indexes.pop_back();
3212                                                         }
3213                                                         type = eSymbolTypeScopeEnd;
3214                                                         break;
3215 
3216                                                     case N_EXCL:
3217                                                         // deleted include file: name,,NO_SECT,0,sum
3218                                                         type = eSymbolTypeHeaderFile;
3219                                                         break;
3220 
3221                                                         //----------------------------------------------------------------------
3222                                                         // COMM scopes
3223                                                         //----------------------------------------------------------------------
3224                                                     case N_BCOMM:
3225                                                         // begin common: name,,NO_SECT,0,0
3226                                                         // We use the current number of symbols in the symbol table in lieu of
3227                                                         // using nlist_idx in case we ever start trimming entries out
3228                                                         type = eSymbolTypeScopeBegin;
3229                                                         N_COMM_indexes.push_back(sym_idx);
3230                                                         break;
3231 
3232                                                     case N_ECOML:
3233                                                         // end common (local name): 0,,n_sect,0,address
3234                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3235                                                         // Fall through
3236 
3237                                                     case N_ECOMM:
3238                                                         // end common: name,,n_sect,0,0
3239                                                         // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3240                                                         // so that we can always skip the entire symbol if we need to navigate
3241                                                         // more quickly at the source level when parsing STABS
3242                                                         if ( !N_COMM_indexes.empty() )
3243                                                         {
3244                                                             symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3245                                                             symbol_ptr->SetByteSize(sym_idx + 1);
3246                                                             symbol_ptr->SetSizeIsSibling(true);
3247                                                             N_COMM_indexes.pop_back();
3248                                                         }
3249                                                         type = eSymbolTypeScopeEnd;
3250                                                         break;
3251 
3252                                                     case N_LENG:
3253                                                         // second stab entry with length information
3254                                                         type = eSymbolTypeAdditional;
3255                                                         break;
3256 
3257                                                     default: break;
3258                                                 }
3259                                             }
3260                                             else
3261                                             {
3262                                                 //uint8_t n_pext    = N_PEXT & nlist.n_type;
3263                                                 uint8_t n_type  = N_TYPE & nlist.n_type;
3264                                                 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
3265 
3266                                                 switch (n_type)
3267                                                 {
3268                                                     case N_INDR:
3269                                                         {
3270                                                             const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
3271                                                             if (reexport_name_cstr && reexport_name_cstr[0])
3272                                                             {
3273                                                                 type = eSymbolTypeReExported;
3274                                                                 ConstString reexport_name(reexport_name_cstr + ((reexport_name_cstr[0] == '_') ? 1 : 0));
3275                                                                 sym[sym_idx].SetReExportedSymbolName(reexport_name);
3276                                                                 set_value = false;
3277                                                                 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3278                                                                 indirect_symbol_names.insert(ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
3279                                                             }
3280                                                             else
3281                                                                 type = eSymbolTypeUndefined;
3282                                                         }
3283                                                         break;
3284 
3285                                                     case N_UNDF:
3286                                                         if (symbol_name && symbol_name[0])
3287                                                         {
3288                                                             ConstString undefined_name(symbol_name + ((symbol_name[0] == '_') ? 1 : 0));
3289                                                             undefined_name_to_desc[undefined_name] = nlist.n_desc;
3290                                                         }
3291                                                         // Fall through
3292                                                     case N_PBUD:
3293                                                         type = eSymbolTypeUndefined;
3294                                                         break;
3295 
3296                                                     case N_ABS:
3297                                                         type = eSymbolTypeAbsolute;
3298                                                         break;
3299 
3300                                                     case N_SECT:
3301                                                         {
3302                                                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3303 
3304                                                             if (symbol_section == NULL)
3305                                                             {
3306                                                                 // TODO: warn about this?
3307                                                                 add_nlist = false;
3308                                                                 break;
3309                                                             }
3310 
3311                                                             if (TEXT_eh_frame_sectID == nlist.n_sect)
3312                                                             {
3313                                                                 type = eSymbolTypeException;
3314                                                             }
3315                                                             else
3316                                                             {
3317                                                                 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
3318 
3319                                                                 switch (section_type)
3320                                                                 {
3321                                                                     case S_CSTRING_LITERALS:           type = eSymbolTypeData;    break; // section with only literal C strings
3322                                                                     case S_4BYTE_LITERALS:             type = eSymbolTypeData;    break; // section with only 4 byte literals
3323                                                                     case S_8BYTE_LITERALS:             type = eSymbolTypeData;    break; // section with only 8 byte literals
3324                                                                     case S_LITERAL_POINTERS:           type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3325                                                                     case S_NON_LAZY_SYMBOL_POINTERS:   type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3326                                                                     case S_LAZY_SYMBOL_POINTERS:       type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3327                                                                     case S_SYMBOL_STUBS:               type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3328                                                                     case S_MOD_INIT_FUNC_POINTERS:     type = eSymbolTypeCode;    break; // section with only function pointers for initialization
3329                                                                     case S_MOD_TERM_FUNC_POINTERS:     type = eSymbolTypeCode;    break; // section with only function pointers for termination
3330                                                                     case S_INTERPOSING:                type = eSymbolTypeTrampoline;  break; // section with only pairs of function pointers for interposing
3331                                                                     case S_16BYTE_LITERALS:            type = eSymbolTypeData;    break; // section with only 16 byte literals
3332                                                                     case S_DTRACE_DOF:                 type = eSymbolTypeInstrumentation; break;
3333                                                                     case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
3334                                                                     default:
3335                                                                         switch (symbol_section->GetType())
3336                                                                         {
3337                                                                             case lldb::eSectionTypeCode:
3338                                                                                 type = eSymbolTypeCode;
3339                                                                                 break;
3340                                                                             case eSectionTypeData:
3341                                                                             case eSectionTypeDataCString:            // Inlined C string data
3342                                                                             case eSectionTypeDataCStringPointers:    // Pointers to C string data
3343                                                                             case eSectionTypeDataSymbolAddress:      // Address of a symbol in the symbol table
3344                                                                             case eSectionTypeData4:
3345                                                                             case eSectionTypeData8:
3346                                                                             case eSectionTypeData16:
3347                                                                                 type = eSymbolTypeData;
3348                                                                                 break;
3349                                                                             default:
3350                                                                                 break;
3351                                                                         }
3352                                                                         break;
3353                                                                 }
3354 
3355                                                                 if (type == eSymbolTypeInvalid)
3356                                                                 {
3357                                                                     const char *symbol_sect_name = symbol_section->GetName().AsCString();
3358                                                                     if (symbol_section->IsDescendant (text_section_sp.get()))
3359                                                                     {
3360                                                                         if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
3361                                                                                                     S_ATTR_SELF_MODIFYING_CODE |
3362                                                                                                     S_ATTR_SOME_INSTRUCTIONS))
3363                                                                             type = eSymbolTypeData;
3364                                                                         else
3365                                                                             type = eSymbolTypeCode;
3366                                                                     }
3367                                                                     else if (symbol_section->IsDescendant(data_section_sp.get()) ||
3368                                                                              symbol_section->IsDescendant(data_dirty_section_sp.get()) ||
3369                                                                              symbol_section->IsDescendant(data_const_section_sp.get()))
3370                                                                     {
3371                                                                         if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3372                                                                         {
3373                                                                             type = eSymbolTypeRuntime;
3374 
3375                                                                             if (symbol_name)
3376                                                                             {
3377                                                                                 llvm::StringRef symbol_name_ref(symbol_name);
3378                                                                                 if (symbol_name_ref.startswith("_OBJC_"))
3379                                                                                 {
3380                                                                                     static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3381                                                                                     static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3382                                                                                     static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3383                                                                                     if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3384                                                                                     {
3385                                                                                         symbol_name_non_abi_mangled = symbol_name + 1;
3386                                                                                         symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3387                                                                                         type = eSymbolTypeObjCClass;
3388                                                                                         demangled_is_synthesized = true;
3389                                                                                     }
3390                                                                                     else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3391                                                                                     {
3392                                                                                         symbol_name_non_abi_mangled = symbol_name + 1;
3393                                                                                         symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3394                                                                                         type = eSymbolTypeObjCMetaClass;
3395                                                                                         demangled_is_synthesized = true;
3396                                                                                     }
3397                                                                                     else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3398                                                                                     {
3399                                                                                         symbol_name_non_abi_mangled = symbol_name + 1;
3400                                                                                         symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3401                                                                                         type = eSymbolTypeObjCIVar;
3402                                                                                         demangled_is_synthesized = true;
3403                                                                                     }
3404                                                                                 }
3405                                                                             }
3406                                                                         }
3407                                                                         else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3408                                                                         {
3409                                                                             type = eSymbolTypeException;
3410                                                                         }
3411                                                                         else
3412                                                                         {
3413                                                                             type = eSymbolTypeData;
3414                                                                         }
3415                                                                     }
3416                                                                     else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3417                                                                     {
3418                                                                         type = eSymbolTypeTrampoline;
3419                                                                     }
3420                                                                     else if (symbol_section->IsDescendant(objc_section_sp.get()))
3421                                                                     {
3422                                                                         type = eSymbolTypeRuntime;
3423                                                                         if (symbol_name && symbol_name[0] == '.')
3424                                                                         {
3425                                                                             llvm::StringRef symbol_name_ref(symbol_name);
3426                                                                             static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3427                                                                             if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3428                                                                             {
3429                                                                                 symbol_name_non_abi_mangled = symbol_name;
3430                                                                                 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3431                                                                                 type = eSymbolTypeObjCClass;
3432                                                                                 demangled_is_synthesized = true;
3433                                                                             }
3434                                                                         }
3435                                                                     }
3436                                                                 }
3437                                                             }
3438                                                         }
3439                                                         break;
3440                                                 }
3441                                             }
3442 
3443                                             if (add_nlist)
3444                                             {
3445                                                 uint64_t symbol_value = nlist.n_value;
3446                                                 if (symbol_name_non_abi_mangled)
3447                                                 {
3448                                                     sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3449                                                     sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
3450                                                 }
3451                                                 else
3452                                                 {
3453                                                     bool symbol_name_is_mangled = false;
3454 
3455                                                     if (symbol_name && symbol_name[0] == '_')
3456                                                     {
3457                                                         symbol_name_is_mangled = symbol_name[1] == '_';
3458                                                         symbol_name++;  // Skip the leading underscore
3459                                                     }
3460 
3461                                                     if (symbol_name)
3462                                                     {
3463                                                         ConstString const_symbol_name(symbol_name);
3464                                                         sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
3465                                                         if (is_gsym && is_debug)
3466                                                         {
3467                                                             const char *gsym_name = sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString();
3468                                                             if (gsym_name)
3469                                                                 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
3470                                                         }
3471                                                     }
3472                                                 }
3473                                                 if (symbol_section)
3474                                                 {
3475                                                     const addr_t section_file_addr = symbol_section->GetFileAddress();
3476                                                     if (symbol_byte_size == 0 && function_starts_count > 0)
3477                                                     {
3478                                                         addr_t symbol_lookup_file_addr = nlist.n_value;
3479                                                         // Do an exact address match for non-ARM addresses, else get the closest since
3480                                                         // the symbol might be a thumb symbol which has an address with bit zero set
3481                                                         FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3482                                                         if (is_arm && func_start_entry)
3483                                                         {
3484                                                             // Verify that the function start address is the symbol address (ARM)
3485                                                             // or the symbol address + 1 (thumb)
3486                                                             if (func_start_entry->addr != symbol_lookup_file_addr &&
3487                                                                 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3488                                                             {
3489                                                                 // Not the right entry, NULL it out...
3490                                                                 func_start_entry = NULL;
3491                                                             }
3492                                                         }
3493                                                         if (func_start_entry)
3494                                                         {
3495                                                             func_start_entry->data = true;
3496 
3497                                                             addr_t symbol_file_addr = func_start_entry->addr;
3498                                                             uint32_t symbol_flags = 0;
3499                                                             if (is_arm)
3500                                                             {
3501                                                                 if (symbol_file_addr & 1)
3502                                                                     symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3503                                                                 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
3504                                                             }
3505 
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                                                                 // Be sure the clear the Thumb address bit when we calculate the size
3512                                                                 // from the current and next address
3513                                                                 if (is_arm)
3514                                                                     next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
3515                                                                 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3516                                                             }
3517                                                             else
3518                                                             {
3519                                                                 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3520                                                             }
3521                                                         }
3522                                                     }
3523                                                     symbol_value -= section_file_addr;
3524                                                 }
3525 
3526                                                 if (is_debug == false)
3527                                                 {
3528                                                     if (type == eSymbolTypeCode)
3529                                                     {
3530                                                         // See if we can find a N_FUN entry for any code symbols.
3531                                                         // If we do find a match, and the name matches, then we
3532                                                         // can merge the two into just the function symbol to avoid
3533                                                         // duplicate entries in the symbol table
3534                                                         std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3535                                                         range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3536                                                         if (range.first != range.second)
3537                                                         {
3538                                                             bool found_it = false;
3539                                                             for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
3540                                                             {
3541                                                                 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3542                                                                 {
3543                                                                     m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3544                                                                     // We just need the flags from the linker symbol, so put these flags
3545                                                                     // into the N_FUN flags to avoid duplicate symbols in the symbol table
3546                                                                     sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3547                                                                     sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3548                                                                     if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3549                                                                         sym[pos->second].SetType (eSymbolTypeResolver);
3550                                                                     sym[sym_idx].Clear();
3551                                                                     found_it = true;
3552                                                                     break;
3553                                                                 }
3554                                                             }
3555                                                             if (found_it)
3556                                                                 continue;
3557                                                         }
3558                                                         else
3559                                                         {
3560                                                             if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3561                                                                 type = eSymbolTypeResolver;
3562                                                         }
3563                                                     }
3564                                                     else if (type == eSymbolTypeData          ||
3565                                                              type == eSymbolTypeObjCClass     ||
3566                                                              type == eSymbolTypeObjCMetaClass ||
3567                                                              type == eSymbolTypeObjCIVar      )
3568                                                     {
3569                                                         // See if we can find a N_STSYM entry for any data symbols.
3570                                                         // If we do find a match, and the name matches, then we
3571                                                         // can merge the two into just the Static symbol to avoid
3572                                                         // duplicate entries in the symbol table
3573                                                         std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3574                                                         range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
3575                                                         if (range.first != range.second)
3576                                                         {
3577                                                             bool found_it = false;
3578                                                             for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
3579                                                             {
3580                                                                 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3581                                                                 {
3582                                                                     m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3583                                                                     // We just need the flags from the linker symbol, so put these flags
3584                                                                     // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3585                                                                     sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3586                                                                     sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3587                                                                     sym[sym_idx].Clear();
3588                                                                     found_it = true;
3589                                                                     break;
3590                                                                 }
3591                                                             }
3592                                                             if (found_it)
3593                                                                 continue;
3594                                                         }
3595                                                         else
3596                                                         {
3597                                                             const char *gsym_name = sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString();
3598                                                             if (gsym_name)
3599                                                             {
3600                                                                 // Combine N_GSYM stab entries with the non stab symbol
3601                                                                 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(gsym_name);
3602                                                                 if (pos != N_GSYM_name_to_sym_idx.end())
3603                                                                 {
3604                                                                     const uint32_t GSYM_sym_idx = pos->second;
3605                                                                     m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3606                                                                     // Copy the address, because often the N_GSYM address has an invalid address of zero
3607                                                                     // when the global is a common symbol
3608                                                                     sym[GSYM_sym_idx].GetAddressRef().SetSection (symbol_section);
3609                                                                     sym[GSYM_sym_idx].GetAddressRef().SetOffset (symbol_value);
3610                                                                     // We just need the flags from the linker symbol, so put these flags
3611                                                                     // into the N_GSYM flags to avoid duplicate symbols in the symbol table
3612                                                                     sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3613                                                                     sym[sym_idx].Clear();
3614                                                                     continue;
3615                                                                 }
3616                                                             }
3617                                                         }
3618                                                     }
3619                                                 }
3620 
3621                                                 sym[sym_idx].SetID (nlist_idx);
3622                                                 sym[sym_idx].SetType (type);
3623                                                 if (set_value)
3624                                                 {
3625                                                     sym[sym_idx].GetAddressRef().SetSection (symbol_section);
3626                                                     sym[sym_idx].GetAddressRef().SetOffset (symbol_value);
3627                                                 }
3628                                                 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3629 
3630                                                 if (symbol_byte_size > 0)
3631                                                     sym[sym_idx].SetByteSize(symbol_byte_size);
3632 
3633                                                 if (demangled_is_synthesized)
3634                                                     sym[sym_idx].SetDemangledNameIsSynthesized(true);
3635                                                 ++sym_idx;
3636                                             }
3637                                             else
3638                                             {
3639                                                 sym[sym_idx].Clear();
3640                                             }
3641 
3642                                         }
3643                                         /////////////////////////////
3644                                     }
3645                                     break; // No more entries to consider
3646                                 }
3647                             }
3648 
3649                             for (const auto &pos :reexport_shlib_needs_fixup)
3650                             {
3651                                 const auto undef_pos = undefined_name_to_desc.find(pos.second);
3652                                 if (undef_pos != undefined_name_to_desc.end())
3653                                 {
3654                                     const uint8_t dylib_ordinal = llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
3655                                     if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
3656                                         sym[pos.first].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(dylib_ordinal-1));
3657                                 }
3658                             }
3659                         }
3660                     }
3661                 }
3662             }
3663         }
3664 
3665         // Must reset this in case it was mutated above!
3666         nlist_data_offset = 0;
3667 #endif
3668 
3669         if (nlist_data.GetByteSize() > 0)
3670         {
3671 
3672             // If the sym array was not created while parsing the DSC unmapped
3673             // symbols, create it now.
3674             if (sym == NULL)
3675             {
3676                 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
3677                 num_syms = symtab->GetNumSymbols();
3678             }
3679 
3680             if (unmapped_local_symbols_found)
3681             {
3682                 assert(m_dysymtab.ilocalsym == 0);
3683                 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
3684                 nlist_idx = m_dysymtab.nlocalsym;
3685             }
3686             else
3687             {
3688                 nlist_idx = 0;
3689             }
3690 
3691             typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap;
3692             typedef std::map<uint32_t, ConstString> SymbolIndexToName;
3693             UndefinedNameToDescMap undefined_name_to_desc;
3694             SymbolIndexToName reexport_shlib_needs_fixup;
3695             for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
3696             {
3697                 struct nlist_64 nlist;
3698                 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
3699                     break;
3700 
3701                 nlist.n_strx  = nlist_data.GetU32_unchecked(&nlist_data_offset);
3702                 nlist.n_type  = nlist_data.GetU8_unchecked (&nlist_data_offset);
3703                 nlist.n_sect  = nlist_data.GetU8_unchecked (&nlist_data_offset);
3704                 nlist.n_desc  = nlist_data.GetU16_unchecked (&nlist_data_offset);
3705                 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
3706 
3707                 SymbolType type = eSymbolTypeInvalid;
3708                 const char *symbol_name = NULL;
3709 
3710                 if (have_strtab_data)
3711                 {
3712                     symbol_name = strtab_data.PeekCStr(nlist.n_strx);
3713 
3714                     if (symbol_name == NULL)
3715                     {
3716                         // No symbol should be NULL, even the symbols with no
3717                         // string values should have an offset zero which points
3718                         // to an empty C-string
3719                         Host::SystemLog (Host::eSystemLogError,
3720                                          "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
3721                                          nlist_idx,
3722                                          nlist.n_strx,
3723                                          module_sp->GetFileSpec().GetPath().c_str());
3724                         continue;
3725                     }
3726                     if (symbol_name[0] == '\0')
3727                         symbol_name = NULL;
3728                 }
3729                 else
3730                 {
3731                     const addr_t str_addr = strtab_addr + nlist.n_strx;
3732                     Error str_error;
3733                     if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
3734                         symbol_name = memory_symbol_name.c_str();
3735                 }
3736                 const char *symbol_name_non_abi_mangled = NULL;
3737 
3738                 SectionSP symbol_section;
3739                 lldb::addr_t symbol_byte_size = 0;
3740                 bool add_nlist = true;
3741                 bool is_gsym = false;
3742                 bool is_debug = ((nlist.n_type & N_STAB) != 0);
3743                 bool demangled_is_synthesized = false;
3744                 bool set_value = true;
3745                 assert (sym_idx < num_syms);
3746 
3747                 sym[sym_idx].SetDebug (is_debug);
3748 
3749                 if (is_debug)
3750                 {
3751                     switch (nlist.n_type)
3752                     {
3753                     case N_GSYM:
3754                         // global symbol: name,,NO_SECT,type,0
3755                         // Sometimes the N_GSYM value contains the address.
3756 
3757                         // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data.  They
3758                         // have the same address, but we want to ensure that we always find only the real symbol,
3759                         // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
3760                         // symbol type.  This is a temporary hack to make sure the ObjectiveC symbols get treated
3761                         // correctly.  To do this right, we should coalesce all the GSYM & global symbols that have the
3762                         // same address.
3763                         is_gsym = true;
3764                         sym[sym_idx].SetExternal(true);
3765 
3766                         if (symbol_name && symbol_name[0] == '_' && symbol_name[1] ==  'O')
3767                         {
3768                             llvm::StringRef symbol_name_ref(symbol_name);
3769                             if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3770                             {
3771                                 symbol_name_non_abi_mangled = symbol_name + 1;
3772                                 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3773                                 type = eSymbolTypeObjCClass;
3774                                 demangled_is_synthesized = true;
3775 
3776                             }
3777                             else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3778                             {
3779                                 symbol_name_non_abi_mangled = symbol_name + 1;
3780                                 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3781                                 type = eSymbolTypeObjCMetaClass;
3782                                 demangled_is_synthesized = true;
3783                             }
3784                             else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3785                             {
3786                                 symbol_name_non_abi_mangled = symbol_name + 1;
3787                                 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3788                                 type = eSymbolTypeObjCIVar;
3789                                 demangled_is_synthesized = true;
3790                             }
3791                         }
3792                         else
3793                         {
3794                             if (nlist.n_value != 0)
3795                                 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3796                             type = eSymbolTypeData;
3797                         }
3798                         break;
3799 
3800                     case N_FNAME:
3801                         // procedure name (f77 kludge): name,,NO_SECT,0,0
3802                         type = eSymbolTypeCompiler;
3803                         break;
3804 
3805                     case N_FUN:
3806                         // procedure: name,,n_sect,linenumber,address
3807                         if (symbol_name)
3808                         {
3809                             type = eSymbolTypeCode;
3810                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3811 
3812                             N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
3813                             // We use the current number of symbols in the symbol table in lieu of
3814                             // using nlist_idx in case we ever start trimming entries out
3815                             N_FUN_indexes.push_back(sym_idx);
3816                         }
3817                         else
3818                         {
3819                             type = eSymbolTypeCompiler;
3820 
3821                             if ( !N_FUN_indexes.empty() )
3822                             {
3823                                 // Copy the size of the function into the original STAB entry so we don't have
3824                                 // to hunt for it later
3825                                 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
3826                                 N_FUN_indexes.pop_back();
3827                                 // We don't really need the end function STAB as it contains the size which
3828                                 // we already placed with the original symbol, so don't add it if we want a
3829                                 // minimal symbol table
3830                                 add_nlist = false;
3831                             }
3832                         }
3833                         break;
3834 
3835                     case N_STSYM:
3836                         // static symbol: name,,n_sect,type,address
3837                         N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
3838                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3839                         if (symbol_name && symbol_name[0])
3840                         {
3841                             type = ObjectFile::GetSymbolTypeFromName(symbol_name+1, eSymbolTypeData);
3842                         }
3843                         break;
3844 
3845                     case N_LCSYM:
3846                         // .lcomm symbol: name,,n_sect,type,address
3847                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3848                         type = eSymbolTypeCommonBlock;
3849                         break;
3850 
3851                     case N_BNSYM:
3852                         // We use the current number of symbols in the symbol table in lieu of
3853                         // using nlist_idx in case we ever start trimming entries out
3854                         // Skip these if we want minimal symbol tables
3855                         add_nlist = false;
3856                         break;
3857 
3858                     case N_ENSYM:
3859                         // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3860                         // so that we can always skip the entire symbol if we need to navigate
3861                         // more quickly at the source level when parsing STABS
3862                         // Skip these if we want minimal symbol tables
3863                         add_nlist = false;
3864                         break;
3865 
3866 
3867                     case N_OPT:
3868                         // emitted with gcc2_compiled and in gcc source
3869                         type = eSymbolTypeCompiler;
3870                         break;
3871 
3872                     case N_RSYM:
3873                         // register sym: name,,NO_SECT,type,register
3874                         type = eSymbolTypeVariable;
3875                         break;
3876 
3877                     case N_SLINE:
3878                         // src line: 0,,n_sect,linenumber,address
3879                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3880                         type = eSymbolTypeLineEntry;
3881                         break;
3882 
3883                     case N_SSYM:
3884                         // structure elt: name,,NO_SECT,type,struct_offset
3885                         type = eSymbolTypeVariableType;
3886                         break;
3887 
3888                     case N_SO:
3889                         // source file name
3890                         type = eSymbolTypeSourceFile;
3891                         if (symbol_name == NULL)
3892                         {
3893                             add_nlist = false;
3894                             if (N_SO_index != UINT32_MAX)
3895                             {
3896                                 // Set the size of the N_SO to the terminating index of this N_SO
3897                                 // so that we can always skip the entire N_SO if we need to navigate
3898                                 // more quickly at the source level when parsing STABS
3899                                 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
3900                                 symbol_ptr->SetByteSize(sym_idx);
3901                                 symbol_ptr->SetSizeIsSibling(true);
3902                             }
3903                             N_NSYM_indexes.clear();
3904                             N_INCL_indexes.clear();
3905                             N_BRAC_indexes.clear();
3906                             N_COMM_indexes.clear();
3907                             N_FUN_indexes.clear();
3908                             N_SO_index = UINT32_MAX;
3909                         }
3910                         else
3911                         {
3912                             // We use the current number of symbols in the symbol table in lieu of
3913                             // using nlist_idx in case we ever start trimming entries out
3914                             const bool N_SO_has_full_path = symbol_name[0] == '/';
3915                             if (N_SO_has_full_path)
3916                             {
3917                                 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3918                                 {
3919                                     // We have two consecutive N_SO entries where the first contains a directory
3920                                     // and the second contains a full path.
3921                                     sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
3922                                     m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3923                                     add_nlist = false;
3924                                 }
3925                                 else
3926                                 {
3927                                     // This is the first entry in a N_SO that contains a directory or
3928                                     // a full path to the source file
3929                                     N_SO_index = sym_idx;
3930                                 }
3931                             }
3932                             else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3933                             {
3934                                 // This is usually the second N_SO entry that contains just the filename,
3935                                 // so here we combine it with the first one if we are minimizing the symbol table
3936                                 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName(lldb::eLanguageTypeUnknown).AsCString();
3937                                 if (so_path && so_path[0])
3938                                 {
3939                                     std::string full_so_path (so_path);
3940                                     const size_t double_slash_pos = full_so_path.find("//");
3941                                     if (double_slash_pos != std::string::npos)
3942                                     {
3943                                         // The linker has been generating bad N_SO entries with doubled up paths
3944                                         // in the format "%s%s" where the first string in the DW_AT_comp_dir,
3945                                         // and the second is the directory for the source file so you end up with
3946                                         // a path that looks like "/tmp/src//tmp/src/"
3947                                         FileSpec so_dir(so_path, false);
3948                                         if (!so_dir.Exists())
3949                                         {
3950                                             so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3951                                             if (so_dir.Exists())
3952                                             {
3953                                                 // Trim off the incorrect path
3954                                                 full_so_path.erase(0, double_slash_pos + 1);
3955                                             }
3956                                         }
3957                                     }
3958                                     if (*full_so_path.rbegin() != '/')
3959                                         full_so_path += '/';
3960                                     full_so_path += symbol_name;
3961                                     sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3962                                     add_nlist = false;
3963                                     m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3964                                 }
3965                             }
3966                             else
3967                             {
3968                                 // This could be a relative path to a N_SO
3969                                 N_SO_index = sym_idx;
3970                             }
3971                         }
3972                         break;
3973 
3974                     case N_OSO:
3975                         // object file name: name,,0,0,st_mtime
3976                         type = eSymbolTypeObjectFile;
3977                         break;
3978 
3979                     case N_LSYM:
3980                         // local sym: name,,NO_SECT,type,offset
3981                         type = eSymbolTypeLocal;
3982                         break;
3983 
3984                     //----------------------------------------------------------------------
3985                     // INCL scopes
3986                     //----------------------------------------------------------------------
3987                     case N_BINCL:
3988                         // include file beginning: name,,NO_SECT,0,sum
3989                         // We use the current number of symbols in the symbol table in lieu of
3990                         // using nlist_idx in case we ever start trimming entries out
3991                         N_INCL_indexes.push_back(sym_idx);
3992                         type = eSymbolTypeScopeBegin;
3993                         break;
3994 
3995                     case N_EINCL:
3996                         // include file end: name,,NO_SECT,0,0
3997                         // Set the size of the N_BINCL to the terminating index of this N_EINCL
3998                         // so that we can always skip the entire symbol if we need to navigate
3999                         // more quickly at the source level when parsing STABS
4000                         if ( !N_INCL_indexes.empty() )
4001                         {
4002                             symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
4003                             symbol_ptr->SetByteSize(sym_idx + 1);
4004                             symbol_ptr->SetSizeIsSibling(true);
4005                             N_INCL_indexes.pop_back();
4006                         }
4007                         type = eSymbolTypeScopeEnd;
4008                         break;
4009 
4010                     case N_SOL:
4011                         // #included file name: name,,n_sect,0,address
4012                         type = eSymbolTypeHeaderFile;
4013 
4014                         // We currently don't use the header files on darwin
4015                         add_nlist = false;
4016                         break;
4017 
4018                     case N_PARAMS:
4019                         // compiler parameters: name,,NO_SECT,0,0
4020                         type = eSymbolTypeCompiler;
4021                         break;
4022 
4023                     case N_VERSION:
4024                         // compiler version: name,,NO_SECT,0,0
4025                         type = eSymbolTypeCompiler;
4026                         break;
4027 
4028                     case N_OLEVEL:
4029                         // compiler -O level: name,,NO_SECT,0,0
4030                         type = eSymbolTypeCompiler;
4031                         break;
4032 
4033                     case N_PSYM:
4034                         // parameter: name,,NO_SECT,type,offset
4035                         type = eSymbolTypeVariable;
4036                         break;
4037 
4038                     case N_ENTRY:
4039                         // alternate entry: name,,n_sect,linenumber,address
4040                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4041                         type = eSymbolTypeLineEntry;
4042                         break;
4043 
4044                     //----------------------------------------------------------------------
4045                     // Left and Right Braces
4046                     //----------------------------------------------------------------------
4047                     case N_LBRAC:
4048                         // left bracket: 0,,NO_SECT,nesting level,address
4049                         // We use the current number of symbols in the symbol table in lieu of
4050                         // using nlist_idx in case we ever start trimming entries out
4051                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4052                         N_BRAC_indexes.push_back(sym_idx);
4053                         type = eSymbolTypeScopeBegin;
4054                         break;
4055 
4056                     case N_RBRAC:
4057                         // right bracket: 0,,NO_SECT,nesting level,address
4058                         // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
4059                         // so that we can always skip the entire symbol if we need to navigate
4060                         // more quickly at the source level when parsing STABS
4061                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4062                         if ( !N_BRAC_indexes.empty() )
4063                         {
4064                             symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
4065                             symbol_ptr->SetByteSize(sym_idx + 1);
4066                             symbol_ptr->SetSizeIsSibling(true);
4067                             N_BRAC_indexes.pop_back();
4068                         }
4069                         type = eSymbolTypeScopeEnd;
4070                         break;
4071 
4072                     case N_EXCL:
4073                         // deleted include file: name,,NO_SECT,0,sum
4074                         type = eSymbolTypeHeaderFile;
4075                         break;
4076 
4077                     //----------------------------------------------------------------------
4078                     // COMM scopes
4079                     //----------------------------------------------------------------------
4080                     case N_BCOMM:
4081                         // begin common: name,,NO_SECT,0,0
4082                         // We use the current number of symbols in the symbol table in lieu of
4083                         // using nlist_idx in case we ever start trimming entries out
4084                         type = eSymbolTypeScopeBegin;
4085                         N_COMM_indexes.push_back(sym_idx);
4086                         break;
4087 
4088                     case N_ECOML:
4089                         // end common (local name): 0,,n_sect,0,address
4090                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4091                         // Fall through
4092 
4093                     case N_ECOMM:
4094                         // end common: name,,n_sect,0,0
4095                         // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
4096                         // so that we can always skip the entire symbol if we need to navigate
4097                         // more quickly at the source level when parsing STABS
4098                         if ( !N_COMM_indexes.empty() )
4099                         {
4100                             symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
4101                             symbol_ptr->SetByteSize(sym_idx + 1);
4102                             symbol_ptr->SetSizeIsSibling(true);
4103                             N_COMM_indexes.pop_back();
4104                         }
4105                         type = eSymbolTypeScopeEnd;
4106                         break;
4107 
4108                     case N_LENG:
4109                         // second stab entry with length information
4110                         type = eSymbolTypeAdditional;
4111                         break;
4112 
4113                     default: break;
4114                     }
4115                 }
4116                 else
4117                 {
4118                     //uint8_t n_pext    = N_PEXT & nlist.n_type;
4119                     uint8_t n_type  = N_TYPE & nlist.n_type;
4120                     sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
4121 
4122                     switch (n_type)
4123                     {
4124                     case N_INDR:
4125                         {
4126                             const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
4127                             if (reexport_name_cstr && reexport_name_cstr[0])
4128                             {
4129                                 type = eSymbolTypeReExported;
4130                                 ConstString reexport_name(reexport_name_cstr + ((reexport_name_cstr[0] == '_') ? 1 : 0));
4131                                 sym[sym_idx].SetReExportedSymbolName(reexport_name);
4132                                 set_value = false;
4133                                 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
4134                                 indirect_symbol_names.insert(ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
4135                             }
4136                             else
4137                                 type = eSymbolTypeUndefined;
4138                         }
4139                         break;
4140 
4141                     case N_UNDF:
4142                         if (symbol_name && symbol_name[0])
4143                         {
4144                             ConstString undefined_name(symbol_name + ((symbol_name[0] == '_') ? 1 : 0));
4145                             undefined_name_to_desc[undefined_name] = nlist.n_desc;
4146                         }
4147                         // Fall through
4148                     case N_PBUD:
4149                         type = eSymbolTypeUndefined;
4150                         break;
4151 
4152                     case N_ABS:
4153                         type = eSymbolTypeAbsolute;
4154                         break;
4155 
4156                     case N_SECT:
4157                         {
4158                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4159 
4160                             if (!symbol_section)
4161                             {
4162                                 // TODO: warn about this?
4163                                 add_nlist = false;
4164                                 break;
4165                             }
4166 
4167                             if (TEXT_eh_frame_sectID == nlist.n_sect)
4168                             {
4169                                 type = eSymbolTypeException;
4170                             }
4171                             else
4172                             {
4173                                 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
4174 
4175                                 switch (section_type)
4176                                 {
4177                                 case S_CSTRING_LITERALS:           type = eSymbolTypeData;    break; // section with only literal C strings
4178                                 case S_4BYTE_LITERALS:             type = eSymbolTypeData;    break; // section with only 4 byte literals
4179                                 case S_8BYTE_LITERALS:             type = eSymbolTypeData;    break; // section with only 8 byte literals
4180                                 case S_LITERAL_POINTERS:           type = eSymbolTypeTrampoline; break; // section with only pointers to literals
4181                                 case S_NON_LAZY_SYMBOL_POINTERS:   type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
4182                                 case S_LAZY_SYMBOL_POINTERS:       type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
4183                                 case S_SYMBOL_STUBS:               type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
4184                                 case S_MOD_INIT_FUNC_POINTERS:     type = eSymbolTypeCode;    break; // section with only function pointers for initialization
4185                                 case S_MOD_TERM_FUNC_POINTERS:     type = eSymbolTypeCode;    break; // section with only function pointers for termination
4186                                 case S_INTERPOSING:                type = eSymbolTypeTrampoline;  break; // section with only pairs of function pointers for interposing
4187                                 case S_16BYTE_LITERALS:            type = eSymbolTypeData;    break; // section with only 16 byte literals
4188                                 case S_DTRACE_DOF:                 type = eSymbolTypeInstrumentation; break;
4189                                 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
4190                                 default:
4191                                     switch (symbol_section->GetType())
4192                                     {
4193                                         case lldb::eSectionTypeCode:
4194                                             type = eSymbolTypeCode;
4195                                             break;
4196                                         case eSectionTypeData:
4197                                         case eSectionTypeDataCString:            // Inlined C string data
4198                                         case eSectionTypeDataCStringPointers:    // Pointers to C string data
4199                                         case eSectionTypeDataSymbolAddress:      // Address of a symbol in the symbol table
4200                                         case eSectionTypeData4:
4201                                         case eSectionTypeData8:
4202                                         case eSectionTypeData16:
4203                                             type = eSymbolTypeData;
4204                                             break;
4205                                         default:
4206                                             break;
4207                                     }
4208                                     break;
4209                                 }
4210 
4211                                 if (type == eSymbolTypeInvalid)
4212                                 {
4213                                     const char *symbol_sect_name = symbol_section->GetName().AsCString();
4214                                     if (symbol_section->IsDescendant (text_section_sp.get()))
4215                                     {
4216                                         if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
4217                                                                     S_ATTR_SELF_MODIFYING_CODE |
4218                                                                     S_ATTR_SOME_INSTRUCTIONS))
4219                                             type = eSymbolTypeData;
4220                                         else
4221                                             type = eSymbolTypeCode;
4222                                     }
4223                                     else
4224                                     if (symbol_section->IsDescendant(data_section_sp.get()) ||
4225                                         symbol_section->IsDescendant(data_dirty_section_sp.get()) ||
4226                                         symbol_section->IsDescendant(data_const_section_sp.get()))
4227                                     {
4228                                         if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
4229                                         {
4230                                             type = eSymbolTypeRuntime;
4231 
4232                                             if (symbol_name)
4233                                             {
4234                                                 llvm::StringRef symbol_name_ref(symbol_name);
4235                                                 if (symbol_name_ref.startswith("_OBJC_"))
4236                                                 {
4237                                                     static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
4238                                                     static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
4239                                                     static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
4240                                                     if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
4241                                                     {
4242                                                         symbol_name_non_abi_mangled = symbol_name + 1;
4243                                                         symbol_name = symbol_name + g_objc_v2_prefix_class.size();
4244                                                         type = eSymbolTypeObjCClass;
4245                                                         demangled_is_synthesized = true;
4246                                                     }
4247                                                     else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
4248                                                     {
4249                                                         symbol_name_non_abi_mangled = symbol_name + 1;
4250                                                         symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
4251                                                         type = eSymbolTypeObjCMetaClass;
4252                                                         demangled_is_synthesized = true;
4253                                                     }
4254                                                     else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
4255                                                     {
4256                                                         symbol_name_non_abi_mangled = symbol_name + 1;
4257                                                         symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
4258                                                         type = eSymbolTypeObjCIVar;
4259                                                         demangled_is_synthesized = true;
4260                                                     }
4261                                                 }
4262                                             }
4263                                         }
4264                                         else
4265                                         if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
4266                                         {
4267                                             type = eSymbolTypeException;
4268                                         }
4269                                         else
4270                                         {
4271                                             type = eSymbolTypeData;
4272                                         }
4273                                     }
4274                                     else
4275                                     if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
4276                                     {
4277                                         type = eSymbolTypeTrampoline;
4278                                     }
4279                                     else
4280                                     if (symbol_section->IsDescendant(objc_section_sp.get()))
4281                                     {
4282                                         type = eSymbolTypeRuntime;
4283                                         if (symbol_name && symbol_name[0] == '.')
4284                                         {
4285                                             llvm::StringRef symbol_name_ref(symbol_name);
4286                                             static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
4287                                             if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
4288                                             {
4289                                                 symbol_name_non_abi_mangled = symbol_name;
4290                                                 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
4291                                                 type = eSymbolTypeObjCClass;
4292                                                 demangled_is_synthesized = true;
4293                                             }
4294                                         }
4295                                     }
4296                                 }
4297                             }
4298                         }
4299                         break;
4300                     }
4301                 }
4302 
4303                 if (add_nlist)
4304                 {
4305                     uint64_t symbol_value = nlist.n_value;
4306 
4307                     if (symbol_name_non_abi_mangled)
4308                     {
4309                         sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
4310                         sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
4311                     }
4312                     else
4313                     {
4314                         bool symbol_name_is_mangled = false;
4315 
4316                         if (symbol_name && symbol_name[0] == '_')
4317                         {
4318                             symbol_name_is_mangled = symbol_name[1] == '_';
4319                             symbol_name++;  // Skip the leading underscore
4320                         }
4321 
4322                         if (symbol_name)
4323                         {
4324                             ConstString const_symbol_name(symbol_name);
4325                             sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
4326                         }
4327                     }
4328 
4329                     if (is_gsym)
4330                     {
4331                         const char *gsym_name = sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled).GetCString();
4332                         if (gsym_name)
4333                             N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
4334                     }
4335 
4336                     if (symbol_section)
4337                     {
4338                         const addr_t section_file_addr = symbol_section->GetFileAddress();
4339                         if (symbol_byte_size == 0 && function_starts_count > 0)
4340                         {
4341                             addr_t symbol_lookup_file_addr = nlist.n_value;
4342                             // Do an exact address match for non-ARM addresses, else get the closest since
4343                             // the symbol might be a thumb symbol which has an address with bit zero set
4344                             FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
4345                             if (is_arm && func_start_entry)
4346                             {
4347                                 // Verify that the function start address is the symbol address (ARM)
4348                                 // or the symbol address + 1 (thumb)
4349                                 if (func_start_entry->addr != symbol_lookup_file_addr &&
4350                                     func_start_entry->addr != (symbol_lookup_file_addr + 1))
4351                                 {
4352                                     // Not the right entry, NULL it out...
4353                                     func_start_entry = NULL;
4354                                 }
4355                             }
4356                             if (func_start_entry)
4357                             {
4358                                 func_start_entry->data = true;
4359 
4360                                 addr_t symbol_file_addr = func_start_entry->addr;
4361                                 if (is_arm)
4362                                     symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
4363 
4364                                 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4365                                 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4366                                 if (next_func_start_entry)
4367                                 {
4368                                     addr_t next_symbol_file_addr = next_func_start_entry->addr;
4369                                     // Be sure the clear the Thumb address bit when we calculate the size
4370                                     // from the current and next address
4371                                     if (is_arm)
4372                                         next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
4373                                     symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
4374                                 }
4375                                 else
4376                                 {
4377                                     symbol_byte_size = section_end_file_addr - symbol_file_addr;
4378                                 }
4379                             }
4380                         }
4381                         symbol_value -= section_file_addr;
4382                     }
4383 
4384                     if (is_debug == false)
4385                     {
4386                         if (type == eSymbolTypeCode)
4387                         {
4388                             // See if we can find a N_FUN entry for any code symbols.
4389                             // If we do find a match, and the name matches, then we
4390                             // can merge the two into just the function symbol to avoid
4391                             // duplicate entries in the symbol table
4392                             std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
4393                             range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
4394                             if (range.first != range.second)
4395                             {
4396                                 bool found_it = false;
4397                                 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
4398                                 {
4399                                     if (sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled))
4400                                     {
4401                                         m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4402                                         // We just need the flags from the linker symbol, so put these flags
4403                                         // into the N_FUN flags to avoid duplicate symbols in the symbol table
4404                                         sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4405                                         sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4406                                         if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
4407                                             sym[pos->second].SetType (eSymbolTypeResolver);
4408                                         sym[sym_idx].Clear();
4409                                         found_it = true;
4410                                         break;
4411                                     }
4412                                 }
4413                                 if (found_it)
4414                                     continue;
4415                             }
4416                             else
4417                             {
4418                                 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
4419                                     type = eSymbolTypeResolver;
4420                             }
4421                         }
4422                         else if (type == eSymbolTypeData          ||
4423                                  type == eSymbolTypeObjCClass     ||
4424                                  type == eSymbolTypeObjCMetaClass ||
4425                                  type == eSymbolTypeObjCIVar      )
4426                         {
4427                             // See if we can find a N_STSYM entry for any data symbols.
4428                             // If we do find a match, and the name matches, then we
4429                             // can merge the two into just the Static symbol to avoid
4430                             // duplicate entries in the symbol table
4431                             std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
4432                             range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
4433                             if (range.first != range.second)
4434                             {
4435                                 bool found_it = false;
4436                                 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
4437                                 {
4438                                     if (sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled))
4439                                     {
4440                                         m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4441                                         // We just need the flags from the linker symbol, so put these flags
4442                                         // into the N_STSYM flags to avoid duplicate symbols in the symbol table
4443                                         sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4444                                         sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4445                                         sym[sym_idx].Clear();
4446                                         found_it = true;
4447                                         break;
4448                                     }
4449                                 }
4450                                 if (found_it)
4451                                     continue;
4452                             }
4453                             else
4454                             {
4455                                 // Combine N_GSYM stab entries with the non stab symbol
4456                                 const char *gsym_name = sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled).GetCString();
4457                                 if (gsym_name)
4458                                 {
4459                                     ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(gsym_name);
4460                                     if (pos != N_GSYM_name_to_sym_idx.end())
4461                                     {
4462                                         const uint32_t GSYM_sym_idx = pos->second;
4463                                         m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4464                                         // Copy the address, because often the N_GSYM address has an invalid address of zero
4465                                         // when the global is a common symbol
4466                                         sym[GSYM_sym_idx].GetAddressRef().SetSection (symbol_section);
4467                                         sym[GSYM_sym_idx].GetAddressRef().SetOffset (symbol_value);
4468                                         // We just need the flags from the linker symbol, so put these flags
4469                                         // into the N_GSYM flags to avoid duplicate symbols in the symbol table
4470                                         sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4471                                         sym[sym_idx].Clear();
4472                                         continue;
4473                                     }
4474                                 }
4475                             }
4476                         }
4477                     }
4478 
4479                     sym[sym_idx].SetID (nlist_idx);
4480                     sym[sym_idx].SetType (type);
4481                     if (set_value)
4482                     {
4483                         sym[sym_idx].GetAddressRef().SetSection (symbol_section);
4484                         sym[sym_idx].GetAddressRef().SetOffset (symbol_value);
4485                     }
4486                     sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4487 
4488                     if (symbol_byte_size > 0)
4489                         sym[sym_idx].SetByteSize(symbol_byte_size);
4490 
4491                     if (demangled_is_synthesized)
4492                         sym[sym_idx].SetDemangledNameIsSynthesized(true);
4493 
4494                     ++sym_idx;
4495                 }
4496                 else
4497                 {
4498                     sym[sym_idx].Clear();
4499                 }
4500             }
4501 
4502             for (const auto &pos :reexport_shlib_needs_fixup)
4503             {
4504                 const auto undef_pos = undefined_name_to_desc.find(pos.second);
4505                 if (undef_pos != undefined_name_to_desc.end())
4506                 {
4507                     const uint8_t dylib_ordinal = llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
4508                     if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
4509                         sym[pos.first].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(dylib_ordinal-1));
4510                 }
4511             }
4512         }
4513 
4514         uint32_t synthetic_sym_id = symtab_load_command.nsyms;
4515 
4516         if (function_starts_count > 0)
4517         {
4518             char synthetic_function_symbol[PATH_MAX];
4519             uint32_t num_synthetic_function_symbols = 0;
4520             for (i=0; i<function_starts_count; ++i)
4521             {
4522                 if (function_starts.GetEntryRef (i).data == false)
4523                     ++num_synthetic_function_symbols;
4524             }
4525 
4526             if (num_synthetic_function_symbols > 0)
4527             {
4528                 if (num_syms < sym_idx + num_synthetic_function_symbols)
4529                 {
4530                     num_syms = sym_idx + num_synthetic_function_symbols;
4531                     sym = symtab->Resize (num_syms);
4532                 }
4533                 uint32_t synthetic_function_symbol_idx = 0;
4534                 for (i=0; i<function_starts_count; ++i)
4535                 {
4536                     const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
4537                     if (func_start_entry->data == false)
4538                     {
4539                         addr_t symbol_file_addr = func_start_entry->addr;
4540                         uint32_t symbol_flags = 0;
4541                         if (is_arm)
4542                         {
4543                             if (symbol_file_addr & 1)
4544                                 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
4545                             symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
4546                         }
4547                         Address symbol_addr;
4548                         if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
4549                         {
4550                             SectionSP symbol_section (symbol_addr.GetSection());
4551                             uint32_t symbol_byte_size = 0;
4552                             if (symbol_section)
4553                             {
4554                                 const addr_t section_file_addr = symbol_section->GetFileAddress();
4555                                 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4556                                 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4557                                 if (next_func_start_entry)
4558                                 {
4559                                     addr_t next_symbol_file_addr = next_func_start_entry->addr;
4560                                     if (is_arm)
4561                                         next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
4562                                     symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
4563                                 }
4564                                 else
4565                                 {
4566                                     symbol_byte_size = section_end_file_addr - symbol_file_addr;
4567                                 }
4568                                 snprintf (synthetic_function_symbol,
4569                                           sizeof(synthetic_function_symbol),
4570                                           "___lldb_unnamed_function%u$$%s",
4571                                           ++synthetic_function_symbol_idx,
4572                                           module_sp->GetFileSpec().GetFilename().GetCString());
4573                                 sym[sym_idx].SetID (synthetic_sym_id++);
4574                                 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
4575                                 sym[sym_idx].SetType (eSymbolTypeCode);
4576                                 sym[sym_idx].SetIsSynthetic (true);
4577                                 sym[sym_idx].GetAddressRef() = symbol_addr;
4578                                 if (symbol_flags)
4579                                     sym[sym_idx].SetFlags (symbol_flags);
4580                                 if (symbol_byte_size)
4581                                     sym[sym_idx].SetByteSize (symbol_byte_size);
4582                                 ++sym_idx;
4583                             }
4584                         }
4585                     }
4586                 }
4587             }
4588         }
4589 
4590         // Trim our symbols down to just what we ended up with after
4591         // removing any symbols.
4592         if (sym_idx < num_syms)
4593         {
4594             num_syms = sym_idx;
4595             sym = symtab->Resize (num_syms);
4596         }
4597 
4598         // Now synthesize indirect symbols
4599         if (m_dysymtab.nindirectsyms != 0)
4600         {
4601             if (indirect_symbol_index_data.GetByteSize())
4602             {
4603                 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
4604 
4605                 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
4606                 {
4607                     if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) == S_SYMBOL_STUBS)
4608                     {
4609                         uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
4610                         if (symbol_stub_byte_size == 0)
4611                             continue;
4612 
4613                         const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
4614 
4615                         if (num_symbol_stubs == 0)
4616                             continue;
4617 
4618                         const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
4619                         for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
4620                         {
4621                             const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
4622                             const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
4623                             lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
4624                             if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
4625                             {
4626                                 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
4627                                 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
4628                                     continue;
4629 
4630                                 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
4631                                 Symbol *stub_symbol = NULL;
4632                                 if (index_pos != end_index_pos)
4633                                 {
4634                                     // We have a remapping from the original nlist index to
4635                                     // a current symbol index, so just look this up by index
4636                                     stub_symbol = symtab->SymbolAtIndex (index_pos->second);
4637                                 }
4638                                 else
4639                                 {
4640                                     // We need to lookup a symbol using the original nlist
4641                                     // symbol index since this index is coming from the
4642                                     // S_SYMBOL_STUBS
4643                                     stub_symbol = symtab->FindSymbolByID (stub_sym_id);
4644                                 }
4645 
4646                                 if (stub_symbol)
4647                                 {
4648                                     Address so_addr(symbol_stub_addr, section_list);
4649 
4650                                     if (stub_symbol->GetType() == eSymbolTypeUndefined)
4651                                     {
4652                                         // Change the external symbol into a trampoline that makes sense
4653                                         // These symbols were N_UNDF N_EXT, and are useless to us, so we
4654                                         // can re-use them so we don't have to make up a synthetic symbol
4655                                         // for no good reason.
4656                                         if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4657                                             stub_symbol->SetType (eSymbolTypeTrampoline);
4658                                         else
4659                                             stub_symbol->SetType (eSymbolTypeResolver);
4660                                         stub_symbol->SetExternal (false);
4661                                         stub_symbol->GetAddressRef() = so_addr;
4662                                         stub_symbol->SetByteSize (symbol_stub_byte_size);
4663                                     }
4664                                     else
4665                                     {
4666                                         // Make a synthetic symbol to describe the trampoline stub
4667                                         Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
4668                                         if (sym_idx >= num_syms)
4669                                         {
4670                                             sym = symtab->Resize (++num_syms);
4671                                             stub_symbol = NULL;  // this pointer no longer valid
4672                                         }
4673                                         sym[sym_idx].SetID (synthetic_sym_id++);
4674                                         sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
4675                                         if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4676                                             sym[sym_idx].SetType (eSymbolTypeTrampoline);
4677                                         else
4678                                             sym[sym_idx].SetType (eSymbolTypeResolver);
4679                                         sym[sym_idx].SetIsSynthetic (true);
4680                                         sym[sym_idx].GetAddressRef() = so_addr;
4681                                         sym[sym_idx].SetByteSize (symbol_stub_byte_size);
4682                                         ++sym_idx;
4683                                     }
4684                                 }
4685                                 else
4686                                 {
4687                                     if (log)
4688                                         log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
4689                                 }
4690                             }
4691                         }
4692                     }
4693                 }
4694             }
4695         }
4696 
4697         if (!trie_entries.empty())
4698         {
4699             for (const auto &e : trie_entries)
4700             {
4701                 if (e.entry.import_name)
4702                 {
4703                     // Only add indirect symbols from the Trie entries if we
4704                     // didn't have a N_INDR nlist entry for this already
4705                     if (indirect_symbol_names.find(e.entry.name) == indirect_symbol_names.end())
4706                     {
4707                         // Make a synthetic symbol to describe re-exported symbol.
4708                         if (sym_idx >= num_syms)
4709                             sym = symtab->Resize (++num_syms);
4710                         sym[sym_idx].SetID (synthetic_sym_id++);
4711                         sym[sym_idx].GetMangled() = Mangled(e.entry.name);
4712                         sym[sym_idx].SetType (eSymbolTypeReExported);
4713                         sym[sym_idx].SetIsSynthetic (true);
4714                         sym[sym_idx].SetReExportedSymbolName(e.entry.import_name);
4715                         if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize())
4716                         {
4717                             sym[sym_idx].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(e.entry.other-1));
4718                         }
4719                         ++sym_idx;
4720                     }
4721                 }
4722             }
4723         }
4724 
4725 //        StreamFile s(stdout, false);
4726 //        s.Printf ("Symbol table before CalculateSymbolSizes():\n");
4727 //        symtab->Dump(&s, NULL, eSortOrderNone);
4728         // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
4729         symtab->CalculateSymbolSizes();
4730 
4731 //        s.Printf ("Symbol table after CalculateSymbolSizes():\n");
4732 //        symtab->Dump(&s, NULL, eSortOrderNone);
4733 
4734         return symtab->GetNumSymbols();
4735     }
4736     return 0;
4737 }
4738 
4739 void
4740 ObjectFileMachO::Dump (Stream *s)
4741 {
4742     ModuleSP module_sp(GetModule());
4743     if (module_sp)
4744     {
4745         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4746         s->Printf("%p: ", static_cast<void*>(this));
4747         s->Indent();
4748         if (m_header.magic == MH_MAGIC_64 || m_header.magic == MH_CIGAM_64)
4749             s->PutCString("ObjectFileMachO64");
4750         else
4751             s->PutCString("ObjectFileMachO32");
4752 
4753         ArchSpec header_arch;
4754         GetArchitecture(header_arch);
4755 
4756         *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
4757 
4758         SectionList *sections = GetSectionList();
4759         if (sections)
4760             sections->Dump(s, NULL, true, UINT32_MAX);
4761 
4762         if (m_symtab_ap.get())
4763             m_symtab_ap->Dump(s, NULL, eSortOrderNone);
4764     }
4765 }
4766 
4767 bool
4768 ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
4769                           const lldb_private::DataExtractor &data,
4770                           lldb::offset_t lc_offset,
4771                           lldb_private::UUID& uuid)
4772 {
4773     uint32_t i;
4774     struct uuid_command load_cmd;
4775 
4776     lldb::offset_t offset = lc_offset;
4777     for (i=0; i<header.ncmds; ++i)
4778     {
4779         const lldb::offset_t cmd_offset = offset;
4780         if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4781             break;
4782 
4783         if (load_cmd.cmd == LC_UUID)
4784         {
4785             const uint8_t *uuid_bytes = data.PeekData(offset, 16);
4786 
4787             if (uuid_bytes)
4788             {
4789                 // OpenCL on Mac OS X uses the same UUID for each of its object files.
4790                 // We pretend these object files have no UUID to prevent crashing.
4791 
4792                 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
4793                     0x3b, 0xa8,
4794                     0x4b, 0x16,
4795                     0xb6, 0xa4,
4796                     0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
4797 
4798                 if (!memcmp(uuid_bytes, opencl_uuid, 16))
4799                     return false;
4800 
4801                 uuid.SetBytes (uuid_bytes);
4802                 return true;
4803             }
4804             return false;
4805         }
4806         offset = cmd_offset + load_cmd.cmdsize;
4807     }
4808     return false;
4809 }
4810 
4811 bool
4812 ObjectFileMachO::GetArchitecture (const llvm::MachO::mach_header &header,
4813                                   const lldb_private::DataExtractor &data,
4814                                   lldb::offset_t lc_offset,
4815                                   ArchSpec &arch)
4816 {
4817     arch.SetArchitecture (eArchTypeMachO, header.cputype, header.cpusubtype);
4818 
4819     if (arch.IsValid())
4820     {
4821         llvm::Triple &triple = arch.GetTriple();
4822 
4823         // Set OS to an unspecified unknown or a "*" so it can match any OS
4824         triple.setOS(llvm::Triple::UnknownOS);
4825         triple.setOSName(llvm::StringRef());
4826 
4827         if (header.filetype == MH_PRELOAD)
4828         {
4829             // Set vendor to an unspecified unknown or a "*" so it can match any vendor
4830             triple.setVendor(llvm::Triple::UnknownVendor);
4831             triple.setVendorName(llvm::StringRef());
4832             return true;
4833         }
4834         else
4835         {
4836             struct load_command load_cmd;
4837 
4838             lldb::offset_t offset = lc_offset;
4839             for (uint32_t i=0; i<header.ncmds; ++i)
4840             {
4841                 const lldb::offset_t cmd_offset = offset;
4842                 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4843                     break;
4844 
4845                 switch (load_cmd.cmd)
4846                 {
4847                     case llvm::MachO::LC_VERSION_MIN_IPHONEOS:
4848                         triple.setOS (llvm::Triple::IOS);
4849                         return true;
4850 
4851                     case llvm::MachO::LC_VERSION_MIN_MACOSX:
4852                         triple.setOS (llvm::Triple::MacOSX);
4853                         return true;
4854 
4855                     case llvm::MachO::LC_VERSION_MIN_TVOS:
4856                         triple.setOS (llvm::Triple::TvOS);
4857                         return true;
4858 
4859                     case llvm::MachO::LC_VERSION_MIN_WATCHOS:
4860                         triple.setOS (llvm::Triple::WatchOS);
4861                         return true;
4862 
4863                     default:
4864                         break;
4865                 }
4866 
4867                 offset = cmd_offset + load_cmd.cmdsize;
4868             }
4869 
4870             if (header.filetype != MH_KEXT_BUNDLE)
4871             {
4872                 // We didn't find a LC_VERSION_MIN load command and this isn't a KEXT
4873                 // so lets not say our Vendor is Apple, leave it as an unspecified unknown
4874                 triple.setVendor(llvm::Triple::UnknownVendor);
4875                 triple.setVendorName(llvm::StringRef());
4876             }
4877         }
4878     }
4879     return arch.IsValid();
4880 }
4881 
4882 bool
4883 ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
4884 {
4885     ModuleSP module_sp(GetModule());
4886     if (module_sp)
4887     {
4888         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4889         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
4890         return GetUUID (m_header, m_data, offset, *uuid);
4891     }
4892     return false;
4893 }
4894 
4895 uint32_t
4896 ObjectFileMachO::GetDependentModules (FileSpecList& files)
4897 {
4898     uint32_t count = 0;
4899     ModuleSP module_sp(GetModule());
4900     if (module_sp)
4901     {
4902         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4903         struct load_command load_cmd;
4904         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
4905         std::vector<std::string> rpath_paths;
4906         std::vector<std::string> rpath_relative_paths;
4907         const bool resolve_path = false; // Don't resolve the dependent file paths since they may not reside on this system
4908         uint32_t i;
4909         for (i=0; i<m_header.ncmds; ++i)
4910         {
4911             const uint32_t cmd_offset = offset;
4912             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4913                 break;
4914 
4915             switch (load_cmd.cmd)
4916             {
4917             case LC_RPATH:
4918             case LC_LOAD_DYLIB:
4919             case LC_LOAD_WEAK_DYLIB:
4920             case LC_REEXPORT_DYLIB:
4921             case LC_LOAD_DYLINKER:
4922             case LC_LOADFVMLIB:
4923             case LC_LOAD_UPWARD_DYLIB:
4924                 {
4925                     uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
4926                     const char *path = m_data.PeekCStr(name_offset);
4927                     if (path)
4928                     {
4929                         if (load_cmd.cmd == LC_RPATH)
4930                             rpath_paths.push_back(path);
4931                         else
4932                         {
4933                             if (path[0] == '@')
4934                             {
4935                                 if (strncmp(path, "@rpath", strlen("@rpath")) == 0)
4936                                     rpath_relative_paths.push_back(path + strlen("@rpath"));
4937                             }
4938                             else
4939                             {
4940                                 FileSpec file_spec(path, resolve_path);
4941                                 if (files.AppendIfUnique(file_spec))
4942                                     count++;
4943                             }
4944                         }
4945                     }
4946                 }
4947                 break;
4948 
4949             default:
4950                 break;
4951             }
4952             offset = cmd_offset + load_cmd.cmdsize;
4953         }
4954 
4955         if (!rpath_paths.empty())
4956         {
4957             // Fixup all LC_RPATH values to be absolute paths
4958             FileSpec this_file_spec(m_file);
4959             this_file_spec.ResolvePath();
4960             std::string loader_path("@loader_path");
4961             std::string executable_path("@executable_path");
4962             for (auto &rpath : rpath_paths)
4963             {
4964                 if (rpath.find(loader_path) == 0)
4965                 {
4966                     rpath.erase(0, loader_path.size());
4967                     rpath.insert(0, this_file_spec.GetDirectory().GetCString());
4968                 }
4969                 else if (rpath.find(executable_path) == 0)
4970                 {
4971                     rpath.erase(0, executable_path.size());
4972                     rpath.insert(0, this_file_spec.GetDirectory().GetCString());
4973                 }
4974             }
4975 
4976             for (const auto &rpath_relative_path : rpath_relative_paths)
4977             {
4978                 for (const auto &rpath : rpath_paths)
4979                 {
4980                     std::string path = rpath;
4981                     path += rpath_relative_path;
4982                     // It is OK to resolve this path because we must find a file on
4983                     // disk for us to accept it anyway if it is rpath relative.
4984                     FileSpec file_spec(path, true);
4985                     // Remove any redundant parts of the path (like "../foo") since
4986                     // LC_RPATH values often contain "..".
4987                     file_spec.NormalizePath ();
4988                     if (file_spec.Exists() && files.AppendIfUnique(file_spec))
4989                     {
4990                         count++;
4991                         break;
4992                     }
4993                 }
4994             }
4995         }
4996     }
4997     return count;
4998 }
4999 
5000 lldb_private::Address
5001 ObjectFileMachO::GetEntryPointAddress ()
5002 {
5003     // If the object file is not an executable it can't hold the entry point.  m_entry_point_address
5004     // is initialized to an invalid address, so we can just return that.
5005     // If m_entry_point_address is valid it means we've found it already, so return the cached value.
5006 
5007     if (!IsExecutable() || m_entry_point_address.IsValid())
5008         return m_entry_point_address;
5009 
5010     // Otherwise, look for the UnixThread or Thread command.  The data for the Thread command is given in
5011     // /usr/include/mach-o.h, but it is basically:
5012     //
5013     //  uint32_t flavor  - this is the flavor argument you would pass to thread_get_state
5014     //  uint32_t count   - this is the count of longs in the thread state data
5015     //  struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
5016     //  <repeat this trio>
5017     //
5018     // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
5019     // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
5020     // out of data in this form & attach them to a given thread.  That should underlie the MacOS X User process plugin,
5021     // and we'll also need it for the MacOS X Core File process plugin.  When we have that we can also use it here.
5022     //
5023     // For now we hard-code the offsets and flavors we need:
5024     //
5025     //
5026 
5027     ModuleSP module_sp(GetModule());
5028     if (module_sp)
5029     {
5030         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5031         struct load_command load_cmd;
5032         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5033         uint32_t i;
5034         lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
5035         bool done = false;
5036 
5037         for (i=0; i<m_header.ncmds; ++i)
5038         {
5039             const lldb::offset_t cmd_offset = offset;
5040             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
5041                 break;
5042 
5043             switch (load_cmd.cmd)
5044             {
5045             case LC_UNIXTHREAD:
5046             case LC_THREAD:
5047                 {
5048                     while (offset < cmd_offset + load_cmd.cmdsize)
5049                     {
5050                         uint32_t flavor = m_data.GetU32(&offset);
5051                         uint32_t count = m_data.GetU32(&offset);
5052                         if (count == 0)
5053                         {
5054                             // We've gotten off somehow, log and exit;
5055                             return m_entry_point_address;
5056                         }
5057 
5058                         switch (m_header.cputype)
5059                         {
5060                         case llvm::MachO::CPU_TYPE_ARM:
5061                            if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
5062                            {
5063                                offset += 60;  // This is the offset of pc in the GPR thread state data structure.
5064                                start_address = m_data.GetU32(&offset);
5065                                done = true;
5066                             }
5067                         break;
5068                         case llvm::MachO::CPU_TYPE_ARM64:
5069                            if (flavor == 6) // ARM_THREAD_STATE64 from mach/arm/thread_status.h
5070                            {
5071                                offset += 256;  // This is the offset of pc in the GPR thread state data structure.
5072                                start_address = m_data.GetU64(&offset);
5073                                done = true;
5074                             }
5075                         break;
5076                         case llvm::MachO::CPU_TYPE_I386:
5077                            if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
5078                            {
5079                                offset += 40;  // This is the offset of eip in the GPR thread state data structure.
5080                                start_address = m_data.GetU32(&offset);
5081                                done = true;
5082                             }
5083                         break;
5084                         case llvm::MachO::CPU_TYPE_X86_64:
5085                            if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
5086                            {
5087                                offset += 16 * 8;  // This is the offset of rip in the GPR thread state data structure.
5088                                start_address = m_data.GetU64(&offset);
5089                                done = true;
5090                             }
5091                         break;
5092                         default:
5093                             return m_entry_point_address;
5094                         }
5095                         // Haven't found the GPR flavor yet, skip over the data for this flavor:
5096                         if (done)
5097                             break;
5098                         offset += count * 4;
5099                     }
5100                 }
5101                 break;
5102             case LC_MAIN:
5103                 {
5104                     ConstString text_segment_name ("__TEXT");
5105                     uint64_t entryoffset = m_data.GetU64(&offset);
5106                     SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
5107                     if (text_segment_sp)
5108                     {
5109                         done = true;
5110                         start_address = text_segment_sp->GetFileAddress() + entryoffset;
5111                     }
5112                 }
5113 
5114             default:
5115                 break;
5116             }
5117             if (done)
5118                 break;
5119 
5120             // Go to the next load command:
5121             offset = cmd_offset + load_cmd.cmdsize;
5122         }
5123 
5124         if (start_address != LLDB_INVALID_ADDRESS)
5125         {
5126             // We got the start address from the load commands, so now resolve that address in the sections
5127             // of this ObjectFile:
5128             if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
5129             {
5130                 m_entry_point_address.Clear();
5131             }
5132         }
5133         else
5134         {
5135             // We couldn't read the UnixThread load command - maybe it wasn't there.  As a fallback look for the
5136             // "start" symbol in the main executable.
5137 
5138             ModuleSP module_sp (GetModule());
5139 
5140             if (module_sp)
5141             {
5142                 SymbolContextList contexts;
5143                 SymbolContext context;
5144                 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
5145                 {
5146                     if (contexts.GetContextAtIndex(0, context))
5147                         m_entry_point_address = context.symbol->GetAddress();
5148                 }
5149             }
5150         }
5151     }
5152 
5153     return m_entry_point_address;
5154 }
5155 
5156 lldb_private::Address
5157 ObjectFileMachO::GetHeaderAddress ()
5158 {
5159     lldb_private::Address header_addr;
5160     SectionList *section_list = GetSectionList();
5161     if (section_list)
5162     {
5163         SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
5164         if (text_segment_sp)
5165         {
5166             header_addr.SetSection (text_segment_sp);
5167             header_addr.SetOffset (0);
5168         }
5169     }
5170     return header_addr;
5171 }
5172 
5173 uint32_t
5174 ObjectFileMachO::GetNumThreadContexts ()
5175 {
5176     ModuleSP module_sp(GetModule());
5177     if (module_sp)
5178     {
5179         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5180         if (!m_thread_context_offsets_valid)
5181         {
5182             m_thread_context_offsets_valid = true;
5183             lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5184             FileRangeArray::Entry file_range;
5185             thread_command thread_cmd;
5186             for (uint32_t i=0; i<m_header.ncmds; ++i)
5187             {
5188                 const uint32_t cmd_offset = offset;
5189                 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
5190                     break;
5191 
5192                 if (thread_cmd.cmd == LC_THREAD)
5193                 {
5194                     file_range.SetRangeBase (offset);
5195                     file_range.SetByteSize (thread_cmd.cmdsize - 8);
5196                     m_thread_context_offsets.Append (file_range);
5197                 }
5198                 offset = cmd_offset + thread_cmd.cmdsize;
5199             }
5200         }
5201     }
5202     return m_thread_context_offsets.GetSize();
5203 }
5204 
5205 lldb::RegisterContextSP
5206 ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
5207 {
5208     lldb::RegisterContextSP reg_ctx_sp;
5209 
5210     ModuleSP module_sp(GetModule());
5211     if (module_sp)
5212     {
5213         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5214         if (!m_thread_context_offsets_valid)
5215             GetNumThreadContexts ();
5216 
5217         const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
5218         if (thread_context_file_range)
5219         {
5220 
5221             DataExtractor data (m_data,
5222                                 thread_context_file_range->GetRangeBase(),
5223                                 thread_context_file_range->GetByteSize());
5224 
5225             switch (m_header.cputype)
5226             {
5227                 case llvm::MachO::CPU_TYPE_ARM64:
5228                     reg_ctx_sp.reset (new RegisterContextDarwin_arm64_Mach (thread, data));
5229                     break;
5230 
5231                 case llvm::MachO::CPU_TYPE_ARM:
5232                     reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
5233                     break;
5234 
5235                 case llvm::MachO::CPU_TYPE_I386:
5236                     reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
5237                     break;
5238 
5239                 case llvm::MachO::CPU_TYPE_X86_64:
5240                     reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
5241                     break;
5242             }
5243         }
5244     }
5245     return reg_ctx_sp;
5246 }
5247 
5248 ObjectFile::Type
5249 ObjectFileMachO::CalculateType()
5250 {
5251     switch (m_header.filetype)
5252     {
5253         case MH_OBJECT:                                         // 0x1u
5254             if (GetAddressByteSize () == 4)
5255             {
5256                 // 32 bit kexts are just object files, but they do have a valid
5257                 // UUID load command.
5258                 UUID uuid;
5259                 if (GetUUID(&uuid))
5260                 {
5261                     // this checking for the UUID load command is not enough
5262                     // we could eventually look for the symbol named
5263                     // "OSKextGetCurrentIdentifier" as this is required of kexts
5264                     if (m_strata == eStrataInvalid)
5265                         m_strata = eStrataKernel;
5266                     return eTypeSharedLibrary;
5267                 }
5268             }
5269             return eTypeObjectFile;
5270 
5271         case MH_EXECUTE:            return eTypeExecutable;     // 0x2u
5272         case MH_FVMLIB:             return eTypeSharedLibrary;  // 0x3u
5273         case MH_CORE:               return eTypeCoreFile;       // 0x4u
5274         case MH_PRELOAD:            return eTypeSharedLibrary;  // 0x5u
5275         case MH_DYLIB:              return eTypeSharedLibrary;  // 0x6u
5276         case MH_DYLINKER:           return eTypeDynamicLinker;  // 0x7u
5277         case MH_BUNDLE:             return eTypeSharedLibrary;  // 0x8u
5278         case MH_DYLIB_STUB:         return eTypeStubLibrary;    // 0x9u
5279         case MH_DSYM:               return eTypeDebugInfo;      // 0xAu
5280         case MH_KEXT_BUNDLE:        return eTypeSharedLibrary;  // 0xBu
5281         default:
5282             break;
5283     }
5284     return eTypeUnknown;
5285 }
5286 
5287 ObjectFile::Strata
5288 ObjectFileMachO::CalculateStrata()
5289 {
5290     switch (m_header.filetype)
5291     {
5292         case MH_OBJECT:                                  // 0x1u
5293             {
5294                 // 32 bit kexts are just object files, but they do have a valid
5295                 // UUID load command.
5296                 UUID uuid;
5297                 if (GetUUID(&uuid))
5298                 {
5299                     // this checking for the UUID load command is not enough
5300                     // we could eventually look for the symbol named
5301                     // "OSKextGetCurrentIdentifier" as this is required of kexts
5302                     if (m_type == eTypeInvalid)
5303                         m_type = eTypeSharedLibrary;
5304 
5305                     return eStrataKernel;
5306                 }
5307             }
5308             return eStrataUnknown;
5309 
5310         case MH_EXECUTE:                                 // 0x2u
5311             // Check for the MH_DYLDLINK bit in the flags
5312             if (m_header.flags & MH_DYLDLINK)
5313             {
5314                 return eStrataUser;
5315             }
5316             else
5317             {
5318                 SectionList *section_list = GetSectionList();
5319                 if (section_list)
5320                 {
5321                     static ConstString g_kld_section_name ("__KLD");
5322                     if (section_list->FindSectionByName(g_kld_section_name))
5323                         return eStrataKernel;
5324                 }
5325             }
5326             return eStrataRawImage;
5327 
5328         case MH_FVMLIB:      return eStrataUser;         // 0x3u
5329         case MH_CORE:        return eStrataUnknown;      // 0x4u
5330         case MH_PRELOAD:     return eStrataRawImage;     // 0x5u
5331         case MH_DYLIB:       return eStrataUser;         // 0x6u
5332         case MH_DYLINKER:    return eStrataUser;         // 0x7u
5333         case MH_BUNDLE:      return eStrataUser;         // 0x8u
5334         case MH_DYLIB_STUB:  return eStrataUser;         // 0x9u
5335         case MH_DSYM:        return eStrataUnknown;      // 0xAu
5336         case MH_KEXT_BUNDLE: return eStrataKernel;       // 0xBu
5337         default:
5338             break;
5339     }
5340     return eStrataUnknown;
5341 }
5342 
5343 uint32_t
5344 ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
5345 {
5346     ModuleSP module_sp(GetModule());
5347     if (module_sp)
5348     {
5349         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5350         struct dylib_command load_cmd;
5351         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5352         uint32_t version_cmd = 0;
5353         uint64_t version = 0;
5354         uint32_t i;
5355         for (i=0; i<m_header.ncmds; ++i)
5356         {
5357             const lldb::offset_t cmd_offset = offset;
5358             if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
5359                 break;
5360 
5361             if (load_cmd.cmd == LC_ID_DYLIB)
5362             {
5363                 if (version_cmd == 0)
5364                 {
5365                     version_cmd = load_cmd.cmd;
5366                     if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
5367                         break;
5368                     version = load_cmd.dylib.current_version;
5369                 }
5370                 break; // Break for now unless there is another more complete version
5371                        // number load command in the future.
5372             }
5373             offset = cmd_offset + load_cmd.cmdsize;
5374         }
5375 
5376         if (version_cmd == LC_ID_DYLIB)
5377         {
5378             if (versions != NULL && num_versions > 0)
5379             {
5380                 if (num_versions > 0)
5381                     versions[0] = (version & 0xFFFF0000ull) >> 16;
5382                 if (num_versions > 1)
5383                     versions[1] = (version & 0x0000FF00ull) >> 8;
5384                 if (num_versions > 2)
5385                     versions[2] = (version & 0x000000FFull);
5386                 // Fill in an remaining version numbers with invalid values
5387                 for (i=3; i<num_versions; ++i)
5388                     versions[i] = UINT32_MAX;
5389             }
5390             // The LC_ID_DYLIB load command has a version with 3 version numbers
5391             // in it, so always return 3
5392             return 3;
5393         }
5394     }
5395     return false;
5396 }
5397 
5398 bool
5399 ObjectFileMachO::GetArchitecture (ArchSpec &arch)
5400 {
5401     ModuleSP module_sp(GetModule());
5402     if (module_sp)
5403     {
5404         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5405         return GetArchitecture (m_header, m_data, MachHeaderSizeFromMagic(m_header.magic), arch);
5406     }
5407     return false;
5408 }
5409 
5410 UUID
5411 ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
5412 {
5413     UUID uuid;
5414     if (process)
5415     {
5416         addr_t all_image_infos = process->GetImageInfoAddress();
5417 
5418         // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
5419         // or it may be the address of the dyld_all_image_infos structure (want).  The first four
5420         // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
5421         // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
5422 
5423         Error err;
5424         uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
5425         if (version_or_magic != static_cast<uint32_t>(-1)
5426             && version_or_magic != MH_MAGIC
5427             && version_or_magic != MH_CIGAM
5428             && version_or_magic != MH_MAGIC_64
5429             && version_or_magic != MH_CIGAM_64
5430             && version_or_magic >= 13)
5431         {
5432             addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
5433             int wordsize = process->GetAddressByteSize();
5434             if (wordsize == 8)
5435             {
5436                 sharedCacheUUID_address = all_image_infos + 160;  // sharedCacheUUID <mach-o/dyld_images.h>
5437             }
5438             if (wordsize == 4)
5439             {
5440                 sharedCacheUUID_address = all_image_infos + 84;   // sharedCacheUUID <mach-o/dyld_images.h>
5441             }
5442             if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
5443             {
5444                 uuid_t shared_cache_uuid;
5445                 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
5446                 {
5447                     uuid.SetBytes (shared_cache_uuid);
5448                 }
5449             }
5450         }
5451     }
5452     return uuid;
5453 }
5454 
5455 UUID
5456 ObjectFileMachO::GetLLDBSharedCacheUUID ()
5457 {
5458     UUID uuid;
5459 #if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
5460     uint8_t *(*dyld_get_all_image_infos)(void);
5461     dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
5462     if (dyld_get_all_image_infos)
5463     {
5464         uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
5465         if (dyld_all_image_infos_address)
5466         {
5467             uint32_t *version = (uint32_t*) dyld_all_image_infos_address;              // version <mach-o/dyld_images.h>
5468             if (*version >= 13)
5469             {
5470                 uuid_t *sharedCacheUUID_address = 0;
5471                 int wordsize = sizeof (uint8_t *);
5472                 if (wordsize == 8)
5473                 {
5474                     sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 160); // sharedCacheUUID <mach-o/dyld_images.h>
5475                 }
5476                 else
5477                 {
5478                     sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84);  // sharedCacheUUID <mach-o/dyld_images.h>
5479                 }
5480                 uuid.SetBytes (sharedCacheUUID_address);
5481             }
5482         }
5483     }
5484 #endif
5485     return uuid;
5486 }
5487 
5488 uint32_t
5489 ObjectFileMachO::GetMinimumOSVersion (uint32_t *versions, uint32_t num_versions)
5490 {
5491     if (m_min_os_versions.empty())
5492     {
5493         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5494         bool success = false;
5495         for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
5496         {
5497             const lldb::offset_t load_cmd_offset = offset;
5498 
5499             version_min_command lc;
5500             if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
5501                 break;
5502             if (lc.cmd == llvm::MachO::LC_VERSION_MIN_MACOSX
5503                  || lc.cmd == llvm::MachO::LC_VERSION_MIN_IPHONEOS
5504                  || lc.cmd == llvm::MachO::LC_VERSION_MIN_TVOS
5505                  || lc.cmd == llvm::MachO::LC_VERSION_MIN_WATCHOS)
5506             {
5507                 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
5508                 {
5509                     const uint32_t xxxx = lc.version >> 16;
5510                     const uint32_t yy = (lc.version >> 8) & 0xffu;
5511                     const uint32_t zz = lc.version  & 0xffu;
5512                     if (xxxx)
5513                     {
5514                         m_min_os_versions.push_back(xxxx);
5515                         m_min_os_versions.push_back(yy);
5516                         m_min_os_versions.push_back(zz);
5517                     }
5518                     success = true;
5519                 }
5520             }
5521             offset = load_cmd_offset + lc.cmdsize;
5522         }
5523 
5524         if (success == false)
5525         {
5526             // Push an invalid value so we don't keep trying to
5527             m_min_os_versions.push_back(UINT32_MAX);
5528         }
5529     }
5530 
5531     if (m_min_os_versions.size() > 1 || m_min_os_versions[0] != UINT32_MAX)
5532     {
5533         if (versions != NULL && num_versions > 0)
5534         {
5535             for (size_t i=0; i<num_versions; ++i)
5536             {
5537                 if (i < m_min_os_versions.size())
5538                     versions[i] = m_min_os_versions[i];
5539                 else
5540                     versions[i] = 0;
5541             }
5542         }
5543         return m_min_os_versions.size();
5544     }
5545     // Call the superclasses version that will empty out the data
5546     return ObjectFile::GetMinimumOSVersion (versions, num_versions);
5547 }
5548 
5549 uint32_t
5550 ObjectFileMachO::GetSDKVersion(uint32_t *versions, uint32_t num_versions)
5551 {
5552     if (m_sdk_versions.empty())
5553     {
5554         lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5555         bool success = false;
5556         for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
5557         {
5558             const lldb::offset_t load_cmd_offset = offset;
5559 
5560             version_min_command lc;
5561             if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
5562                 break;
5563             if (lc.cmd == llvm::MachO::LC_VERSION_MIN_MACOSX
5564                 || lc.cmd == llvm::MachO::LC_VERSION_MIN_IPHONEOS
5565                 || lc.cmd == llvm::MachO::LC_VERSION_MIN_TVOS
5566                 || lc.cmd == llvm::MachO::LC_VERSION_MIN_WATCHOS)
5567             {
5568                 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
5569                 {
5570                     const uint32_t xxxx = lc.sdk >> 16;
5571                     const uint32_t yy = (lc.sdk >> 8) & 0xffu;
5572                     const uint32_t zz = lc.sdk & 0xffu;
5573                     if (xxxx)
5574                     {
5575                         m_sdk_versions.push_back(xxxx);
5576                         m_sdk_versions.push_back(yy);
5577                         m_sdk_versions.push_back(zz);
5578                     }
5579                     success = true;
5580                 }
5581             }
5582             offset = load_cmd_offset + lc.cmdsize;
5583         }
5584 
5585         if (success == false)
5586         {
5587             // Push an invalid value so we don't keep trying to
5588             m_sdk_versions.push_back(UINT32_MAX);
5589         }
5590     }
5591 
5592     if (m_sdk_versions.size() > 1 || m_sdk_versions[0] != UINT32_MAX)
5593     {
5594         if (versions != NULL && num_versions > 0)
5595         {
5596             for (size_t i=0; i<num_versions; ++i)
5597             {
5598                 if (i < m_sdk_versions.size())
5599                     versions[i] = m_sdk_versions[i];
5600                 else
5601                     versions[i] = 0;
5602             }
5603         }
5604         return m_sdk_versions.size();
5605     }
5606     // Call the superclasses version that will empty out the data
5607     return ObjectFile::GetSDKVersion (versions, num_versions);
5608 }
5609 
5610 bool
5611 ObjectFileMachO::GetIsDynamicLinkEditor()
5612 {
5613     return m_header.filetype == llvm::MachO::MH_DYLINKER;
5614 }
5615 
5616 //------------------------------------------------------------------
5617 // PluginInterface protocol
5618 //------------------------------------------------------------------
5619 lldb_private::ConstString
5620 ObjectFileMachO::GetPluginName()
5621 {
5622     return GetPluginNameStatic();
5623 }
5624 
5625 uint32_t
5626 ObjectFileMachO::GetPluginVersion()
5627 {
5628     return 1;
5629 }
5630 
5631 Section *
5632 ObjectFileMachO::GetMachHeaderSection()
5633 {
5634     // Find the first address of the mach header which is the first non-zero
5635     // file sized section whose file offset is zero. This is the base file address
5636     // of the mach-o file which can be subtracted from the vmaddr of the other
5637     // segments found in memory and added to the load address
5638     ModuleSP module_sp = GetModule();
5639     if (module_sp)
5640     {
5641         SectionList *section_list = GetSectionList ();
5642         if (section_list)
5643         {
5644             lldb::addr_t mach_base_file_addr = LLDB_INVALID_ADDRESS;
5645             const size_t num_sections = section_list->GetSize();
5646 
5647             for (size_t sect_idx = 0;
5648                  sect_idx < num_sections && mach_base_file_addr == LLDB_INVALID_ADDRESS;
5649                  ++sect_idx)
5650             {
5651                 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
5652                 if (section &&
5653                     section->GetFileSize() > 0 &&
5654                     section->GetFileOffset() == 0 &&
5655                     section->IsThreadSpecific() == false &&
5656                     module_sp.get() == section->GetModule().get())
5657                 {
5658                     return section;
5659                 }
5660             }
5661         }
5662     }
5663     return nullptr;
5664 }
5665 
5666 lldb::addr_t
5667 ObjectFileMachO::CalculateSectionLoadAddressForMemoryImage(lldb::addr_t mach_header_load_address, const Section *mach_header_section, const Section *section)
5668 {
5669     ModuleSP module_sp = GetModule();
5670     if (module_sp && mach_header_section && section && mach_header_load_address != LLDB_INVALID_ADDRESS)
5671     {
5672         lldb::addr_t mach_header_file_addr = mach_header_section->GetFileAddress();
5673         if (mach_header_file_addr != LLDB_INVALID_ADDRESS)
5674         {
5675             if (section &&
5676                 section->GetFileSize() > 0 &&
5677                 section->IsThreadSpecific() == false &&
5678                 module_sp.get() == section->GetModule().get())
5679             {
5680                 // Ignore __LINKEDIT and __DWARF segments
5681                 if (section->GetName() == GetSegmentNameLINKEDIT())
5682                 {
5683                     // Only map __LINKEDIT if we have an in memory image and this isn't
5684                     // a kernel binary like a kext or mach_kernel.
5685                     const bool is_memory_image = (bool)m_process_wp.lock();
5686                     const Strata strata = GetStrata();
5687                     if (is_memory_image == false || strata == eStrataKernel)
5688                         return LLDB_INVALID_ADDRESS;
5689                 }
5690                 return section->GetFileAddress() - mach_header_file_addr + mach_header_load_address;
5691             }
5692         }
5693     }
5694     return LLDB_INVALID_ADDRESS;
5695 }
5696 
5697 bool
5698 ObjectFileMachO::SetLoadAddress (Target &target,
5699                                  lldb::addr_t value,
5700                                  bool value_is_offset)
5701 {
5702     ModuleSP module_sp = GetModule();
5703     if (module_sp)
5704     {
5705         size_t num_loaded_sections = 0;
5706         SectionList *section_list = GetSectionList ();
5707         if (section_list)
5708         {
5709             const size_t num_sections = section_list->GetSize();
5710 
5711             if (value_is_offset)
5712             {
5713                 // "value" is an offset to apply to each top level segment
5714                 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5715                 {
5716                     // Iterate through the object file sections to find all
5717                     // of the sections that size on disk (to avoid __PAGEZERO)
5718                     // and load them
5719                     SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
5720                     if (section_sp &&
5721                         section_sp->GetFileSize() > 0 &&
5722                         section_sp->IsThreadSpecific() == false &&
5723                         module_sp.get() == section_sp->GetModule().get())
5724                     {
5725                         // Ignore __LINKEDIT and __DWARF segments
5726                         if (section_sp->GetName() == GetSegmentNameLINKEDIT())
5727                         {
5728                             // Only map __LINKEDIT if we have an in memory image and this isn't
5729                             // a kernel binary like a kext or mach_kernel.
5730                             const bool is_memory_image = (bool)m_process_wp.lock();
5731                             const Strata strata = GetStrata();
5732                             if (is_memory_image == false || strata == eStrataKernel)
5733                                 continue;
5734                         }
5735                         if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
5736                             ++num_loaded_sections;
5737                     }
5738                 }
5739             }
5740             else
5741             {
5742                 // "value" is the new base address of the mach_header, adjust each
5743                 // section accordingly
5744 
5745                 Section *mach_header_section = GetMachHeaderSection();
5746                 if (mach_header_section)
5747                 {
5748                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5749                     {
5750                         SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
5751 
5752                         lldb::addr_t section_load_addr = CalculateSectionLoadAddressForMemoryImage(value, mach_header_section, section_sp.get());
5753                         if (section_load_addr != LLDB_INVALID_ADDRESS)
5754                         {
5755                             if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_load_addr))
5756                                 ++num_loaded_sections;
5757                         }
5758                     }
5759                 }
5760             }
5761         }
5762         return num_loaded_sections > 0;
5763     }
5764     return false;
5765 }
5766 
5767 bool
5768 ObjectFileMachO::SaveCore (const lldb::ProcessSP &process_sp,
5769                            const FileSpec &outfile,
5770                            Error &error)
5771 {
5772     if (process_sp)
5773     {
5774         Target &target = process_sp->GetTarget();
5775         const ArchSpec target_arch = target.GetArchitecture();
5776         const llvm::Triple &target_triple = target_arch.GetTriple();
5777         if (target_triple.getVendor() == llvm::Triple::Apple &&
5778             (target_triple.getOS() == llvm::Triple::MacOSX
5779              || target_triple.getOS() == llvm::Triple::IOS
5780              || target_triple.getOS() == llvm::Triple::WatchOS
5781              || target_triple.getOS() == llvm::Triple::TvOS))
5782         {
5783             bool make_core = false;
5784             switch (target_arch.GetMachine())
5785             {
5786                 case llvm::Triple::aarch64:
5787                 case llvm::Triple::arm:
5788                 case llvm::Triple::thumb:
5789                 case llvm::Triple::x86:
5790                 case llvm::Triple::x86_64:
5791                     make_core = true;
5792                     break;
5793                 default:
5794                     error.SetErrorStringWithFormat ("unsupported core architecture: %s", target_triple.str().c_str());
5795                     break;
5796             }
5797 
5798             if (make_core)
5799             {
5800                 std::vector<segment_command_64> segment_load_commands;
5801 //                uint32_t range_info_idx = 0;
5802                 MemoryRegionInfo range_info;
5803                 Error range_error = process_sp->GetMemoryRegionInfo(0, range_info);
5804                 const uint32_t addr_byte_size = target_arch.GetAddressByteSize();
5805                 const ByteOrder byte_order = target_arch.GetByteOrder();
5806                 if (range_error.Success())
5807                 {
5808                     while (range_info.GetRange().GetRangeBase() != LLDB_INVALID_ADDRESS)
5809                     {
5810                         const addr_t addr = range_info.GetRange().GetRangeBase();
5811                         const addr_t size = range_info.GetRange().GetByteSize();
5812 
5813                         if (size == 0)
5814                             break;
5815 
5816                         // Calculate correct protections
5817                         uint32_t prot = 0;
5818                         if (range_info.GetReadable() == MemoryRegionInfo::eYes)
5819                             prot |= VM_PROT_READ;
5820                         if (range_info.GetWritable() == MemoryRegionInfo::eYes)
5821                             prot |= VM_PROT_WRITE;
5822                         if (range_info.GetExecutable() == MemoryRegionInfo::eYes)
5823                             prot |= VM_PROT_EXECUTE;
5824 
5825 //                        printf ("[%3u] [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") %c%c%c\n",
5826 //                                range_info_idx,
5827 //                                addr,
5828 //                                size,
5829 //                                (prot & VM_PROT_READ   ) ? 'r' : '-',
5830 //                                (prot & VM_PROT_WRITE  ) ? 'w' : '-',
5831 //                                (prot & VM_PROT_EXECUTE) ? 'x' : '-');
5832 
5833                         if (prot != 0)
5834                         {
5835                             uint32_t cmd_type = LC_SEGMENT_64;
5836                             uint32_t segment_size = sizeof (segment_command_64);
5837                             if (addr_byte_size == 4)
5838                             {
5839                                 cmd_type = LC_SEGMENT;
5840                                 segment_size = sizeof (segment_command);
5841                             }
5842                             segment_command_64 segment = {
5843                                 cmd_type,           // uint32_t cmd;
5844                                 segment_size,       // uint32_t cmdsize;
5845                                 {0},                // char segname[16];
5846                                 addr,               // uint64_t vmaddr;    // uint32_t for 32-bit Mach-O
5847                                 size,               // uint64_t vmsize;    // uint32_t for 32-bit Mach-O
5848                                 0,                  // uint64_t fileoff;   // uint32_t for 32-bit Mach-O
5849                                 size,               // uint64_t filesize;  // uint32_t for 32-bit Mach-O
5850                                 prot,               // uint32_t maxprot;
5851                                 prot,               // uint32_t initprot;
5852                                 0,                  // uint32_t nsects;
5853                                 0 };                // uint32_t flags;
5854                             segment_load_commands.push_back(segment);
5855                         }
5856                         else
5857                         {
5858                             // No protections and a size of 1 used to be returned from old
5859                             // debugservers when we asked about a region that was past the
5860                             // last memory region and it indicates the end...
5861                             if (size == 1)
5862                                 break;
5863                         }
5864 
5865                         range_error = process_sp->GetMemoryRegionInfo(range_info.GetRange().GetRangeEnd(), range_info);
5866                         if (range_error.Fail())
5867                             break;
5868                     }
5869 
5870                     StreamString buffer (Stream::eBinary,
5871                                          addr_byte_size,
5872                                          byte_order);
5873 
5874                     mach_header_64 mach_header;
5875                     if (addr_byte_size == 8)
5876                     {
5877                         mach_header.magic = MH_MAGIC_64;
5878                     }
5879                     else
5880                     {
5881                         mach_header.magic = MH_MAGIC;
5882                     }
5883                     mach_header.cputype = target_arch.GetMachOCPUType();
5884                     mach_header.cpusubtype = target_arch.GetMachOCPUSubType();
5885                     mach_header.filetype = MH_CORE;
5886                     mach_header.ncmds = segment_load_commands.size();
5887                     mach_header.flags = 0;
5888                     mach_header.reserved = 0;
5889                     ThreadList &thread_list = process_sp->GetThreadList();
5890                     const uint32_t num_threads = thread_list.GetSize();
5891 
5892                     // Make an array of LC_THREAD data items. Each one contains
5893                     // the contents of the LC_THREAD load command. The data doesn't
5894                     // contain the load command + load command size, we will
5895                     // add the load command and load command size as we emit the data.
5896                     std::vector<StreamString> LC_THREAD_datas(num_threads);
5897                     for (auto &LC_THREAD_data : LC_THREAD_datas)
5898                     {
5899                         LC_THREAD_data.GetFlags().Set(Stream::eBinary);
5900                         LC_THREAD_data.SetAddressByteSize(addr_byte_size);
5901                         LC_THREAD_data.SetByteOrder(byte_order);
5902                     }
5903                     for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx)
5904                     {
5905                         ThreadSP thread_sp (thread_list.GetThreadAtIndex(thread_idx));
5906                         if (thread_sp)
5907                         {
5908                             switch (mach_header.cputype)
5909                             {
5910                                 case llvm::MachO::CPU_TYPE_ARM64:
5911                                     RegisterContextDarwin_arm64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5912                                     break;
5913 
5914                                 case llvm::MachO::CPU_TYPE_ARM:
5915                                     RegisterContextDarwin_arm_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5916                                     break;
5917 
5918                                 case llvm::MachO::CPU_TYPE_I386:
5919                                     RegisterContextDarwin_i386_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5920                                     break;
5921 
5922                                 case llvm::MachO::CPU_TYPE_X86_64:
5923                                     RegisterContextDarwin_x86_64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5924                                     break;
5925                             }
5926 
5927                         }
5928                     }
5929 
5930                     // The size of the load command is the size of the segments...
5931                     if (addr_byte_size == 8)
5932                     {
5933                         mach_header.sizeofcmds = segment_load_commands.size() * sizeof (struct segment_command_64);
5934                     }
5935                     else
5936                     {
5937                         mach_header.sizeofcmds = segment_load_commands.size() * sizeof (struct segment_command);
5938                     }
5939 
5940                     // and the size of all LC_THREAD load command
5941                     for (const auto &LC_THREAD_data : LC_THREAD_datas)
5942                     {
5943                         ++mach_header.ncmds;
5944                         mach_header.sizeofcmds += 8 + LC_THREAD_data.GetSize();
5945                     }
5946 
5947                     printf ("mach_header: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x\n",
5948                             mach_header.magic,
5949                             mach_header.cputype,
5950                             mach_header.cpusubtype,
5951                             mach_header.filetype,
5952                             mach_header.ncmds,
5953                             mach_header.sizeofcmds,
5954                             mach_header.flags,
5955                             mach_header.reserved);
5956 
5957                     // Write the mach header
5958                     buffer.PutHex32(mach_header.magic);
5959                     buffer.PutHex32(mach_header.cputype);
5960                     buffer.PutHex32(mach_header.cpusubtype);
5961                     buffer.PutHex32(mach_header.filetype);
5962                     buffer.PutHex32(mach_header.ncmds);
5963                     buffer.PutHex32(mach_header.sizeofcmds);
5964                     buffer.PutHex32(mach_header.flags);
5965                     if (addr_byte_size == 8)
5966                     {
5967                         buffer.PutHex32(mach_header.reserved);
5968                     }
5969 
5970                     // Skip the mach header and all load commands and align to the next
5971                     // 0x1000 byte boundary
5972                     addr_t file_offset = buffer.GetSize() + mach_header.sizeofcmds;
5973                     if (file_offset & 0x00000fff)
5974                     {
5975                         file_offset += 0x00001000ull;
5976                         file_offset &= (~0x00001000ull + 1);
5977                     }
5978 
5979                     for (auto &segment : segment_load_commands)
5980                     {
5981                         segment.fileoff = file_offset;
5982                         file_offset += segment.filesize;
5983                     }
5984 
5985                     // Write out all of the LC_THREAD load commands
5986                     for (const auto &LC_THREAD_data : LC_THREAD_datas)
5987                     {
5988                         const size_t LC_THREAD_data_size = LC_THREAD_data.GetSize();
5989                         buffer.PutHex32(LC_THREAD);
5990                         buffer.PutHex32(8 + LC_THREAD_data_size); // cmd + cmdsize + data
5991                         buffer.Write(LC_THREAD_data.GetData(), LC_THREAD_data_size);
5992                     }
5993 
5994                     // Write out all of the segment load commands
5995                     for (const auto &segment : segment_load_commands)
5996                     {
5997                         printf ("0x%8.8x 0x%8.8x [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") [0x%16.16" PRIx64 " 0x%16.16" PRIx64 ") 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x]\n",
5998                                 segment.cmd,
5999                                 segment.cmdsize,
6000                                 segment.vmaddr,
6001                                 segment.vmaddr + segment.vmsize,
6002                                 segment.fileoff,
6003                                 segment.filesize,
6004                                 segment.maxprot,
6005                                 segment.initprot,
6006                                 segment.nsects,
6007                                 segment.flags);
6008 
6009                         buffer.PutHex32(segment.cmd);
6010                         buffer.PutHex32(segment.cmdsize);
6011                         buffer.PutRawBytes(segment.segname, sizeof(segment.segname));
6012                         if (addr_byte_size == 8)
6013                         {
6014                             buffer.PutHex64(segment.vmaddr);
6015                             buffer.PutHex64(segment.vmsize);
6016                             buffer.PutHex64(segment.fileoff);
6017                             buffer.PutHex64(segment.filesize);
6018                         }
6019                         else
6020                         {
6021                             buffer.PutHex32(static_cast<uint32_t>(segment.vmaddr));
6022                             buffer.PutHex32(static_cast<uint32_t>(segment.vmsize));
6023                             buffer.PutHex32(static_cast<uint32_t>(segment.fileoff));
6024                             buffer.PutHex32(static_cast<uint32_t>(segment.filesize));
6025                         }
6026                         buffer.PutHex32(segment.maxprot);
6027                         buffer.PutHex32(segment.initprot);
6028                         buffer.PutHex32(segment.nsects);
6029                         buffer.PutHex32(segment.flags);
6030                     }
6031 
6032                     File core_file;
6033                     std::string core_file_path(outfile.GetPath());
6034                     error = core_file.Open(core_file_path.c_str(),
6035                                            File::eOpenOptionWrite    |
6036                                            File::eOpenOptionTruncate |
6037                                            File::eOpenOptionCanCreate);
6038                     if (error.Success())
6039                     {
6040                         // Read 1 page at a time
6041                         uint8_t bytes[0x1000];
6042                         // Write the mach header and load commands out to the core file
6043                         size_t bytes_written = buffer.GetString().size();
6044                         error = core_file.Write(buffer.GetString().data(), bytes_written);
6045                         if (error.Success())
6046                         {
6047                             // Now write the file data for all memory segments in the process
6048                             for (const auto &segment : segment_load_commands)
6049                             {
6050                                 if (core_file.SeekFromStart(segment.fileoff) == -1)
6051                                 {
6052                                     error.SetErrorStringWithFormat("unable to seek to offset 0x%" PRIx64 " in '%s'", segment.fileoff, core_file_path.c_str());
6053                                     break;
6054                                 }
6055 
6056                                 printf ("Saving %" PRId64 " bytes of data for memory region at 0x%" PRIx64 "\n", segment.vmsize, segment.vmaddr);
6057                                 addr_t bytes_left = segment.vmsize;
6058                                 addr_t addr = segment.vmaddr;
6059                                 Error memory_read_error;
6060                                 while (bytes_left > 0 && error.Success())
6061                                 {
6062                                     const size_t bytes_to_read = bytes_left > sizeof(bytes) ? sizeof(bytes) : bytes_left;
6063                                     const size_t bytes_read = process_sp->ReadMemory(addr, bytes, bytes_to_read, memory_read_error);
6064                                     if (bytes_read == bytes_to_read)
6065                                     {
6066                                         size_t bytes_written = bytes_read;
6067                                         error = core_file.Write(bytes, bytes_written);
6068                                         bytes_left -= bytes_read;
6069                                         addr += bytes_read;
6070                                     }
6071                                     else
6072                                     {
6073                                         // Some pages within regions are not readable, those
6074                                         // should be zero filled
6075                                         memset (bytes, 0, bytes_to_read);
6076                                         size_t bytes_written = bytes_to_read;
6077                                         error = core_file.Write(bytes, bytes_written);
6078                                         bytes_left -= bytes_to_read;
6079                                         addr += bytes_to_read;
6080                                     }
6081                                 }
6082                             }
6083                         }
6084                     }
6085                 }
6086                 else
6087                 {
6088                     error.SetErrorString("process doesn't support getting memory region info");
6089                 }
6090             }
6091             return true; // This is the right plug to handle saving core files for this process
6092         }
6093     }
6094     return false;
6095 }
6096