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