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