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