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