1 //===-- ProcessMachCore.cpp ------------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 // C Includes
12 #include <errno.h>
13 #include <stdlib.h>
14 
15 // C++ Includes
16 #include "llvm/Support/MathExtras.h"
17 #include "llvm/Support/Threading.h"
18 #include <mutex>
19 
20 // Other libraries and framework includes
21 #include "lldb/Core/Debugger.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/ModuleSpec.h"
24 #include "lldb/Core/PluginManager.h"
25 #include "lldb/Core/Section.h"
26 #include "lldb/Core/State.h"
27 #include "lldb/Host/Host.h"
28 #include "lldb/Symbol/ObjectFile.h"
29 #include "lldb/Target/MemoryRegionInfo.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
32 #include "lldb/Utility/DataBuffer.h"
33 #include "lldb/Utility/DataBufferLLVM.h"
34 #include "lldb/Utility/Log.h"
35 
36 // Project includes
37 #include "ProcessMachCore.h"
38 #include "StopInfoMachException.h"
39 #include "ThreadMachCore.h"
40 
41 // Needed for the plug-in names for the dynamic loaders.
42 #include "lldb/Utility/SafeMachO.h"
43 
44 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
45 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
46 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
47 
48 using namespace lldb;
49 using namespace lldb_private;
50 
51 ConstString ProcessMachCore::GetPluginNameStatic() {
52   static ConstString g_name("mach-o-core");
53   return g_name;
54 }
55 
56 const char *ProcessMachCore::GetPluginDescriptionStatic() {
57   return "Mach-O core file debugging plug-in.";
58 }
59 
60 void ProcessMachCore::Terminate() {
61   PluginManager::UnregisterPlugin(ProcessMachCore::CreateInstance);
62 }
63 
64 lldb::ProcessSP ProcessMachCore::CreateInstance(lldb::TargetSP target_sp,
65                                                 ListenerSP listener_sp,
66                                                 const FileSpec *crash_file) {
67   lldb::ProcessSP process_sp;
68   if (crash_file) {
69     const size_t header_size = sizeof(llvm::MachO::mach_header);
70     auto data_sp =
71         DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), header_size, 0);
72     if (data_sp && data_sp->GetByteSize() == header_size) {
73       DataExtractor data(data_sp, lldb::eByteOrderLittle, 4);
74 
75       lldb::offset_t data_offset = 0;
76       llvm::MachO::mach_header mach_header;
77       if (ObjectFileMachO::ParseHeader(data, &data_offset, mach_header)) {
78         if (mach_header.filetype == llvm::MachO::MH_CORE)
79           process_sp.reset(
80               new ProcessMachCore(target_sp, listener_sp, *crash_file));
81       }
82     }
83   }
84   return process_sp;
85 }
86 
87 bool ProcessMachCore::CanDebug(lldb::TargetSP target_sp,
88                                bool plugin_specified_by_name) {
89   if (plugin_specified_by_name)
90     return true;
91 
92   // For now we are just making sure the file exists for a given module
93   if (!m_core_module_sp && m_core_file.Exists()) {
94     // Don't add the Target's architecture to the ModuleSpec - we may be working
95     // with a core file that doesn't have the correct cpusubtype in the header
96     // but we should still try to use it -
97     // ModuleSpecList::FindMatchingModuleSpec
98     // enforces a strict arch mach.
99     ModuleSpec core_module_spec(m_core_file);
100     Error error(ModuleList::GetSharedModule(core_module_spec, m_core_module_sp,
101                                             NULL, NULL, NULL));
102 
103     if (m_core_module_sp) {
104       ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
105       if (core_objfile && core_objfile->GetType() == ObjectFile::eTypeCoreFile)
106         return true;
107     }
108   }
109   return false;
110 }
111 
112 //----------------------------------------------------------------------
113 // ProcessMachCore constructor
114 //----------------------------------------------------------------------
115 ProcessMachCore::ProcessMachCore(lldb::TargetSP target_sp,
116                                  ListenerSP listener_sp,
117                                  const FileSpec &core_file)
118     : Process(target_sp, listener_sp), m_core_aranges(), m_core_range_infos(),
119       m_core_module_sp(), m_core_file(core_file),
120       m_dyld_addr(LLDB_INVALID_ADDRESS),
121       m_mach_kernel_addr(LLDB_INVALID_ADDRESS), m_dyld_plugin_name() {}
122 
123 //----------------------------------------------------------------------
124 // Destructor
125 //----------------------------------------------------------------------
126 ProcessMachCore::~ProcessMachCore() {
127   Clear();
128   // We need to call finalize on the process before destroying ourselves
129   // to make sure all of the broadcaster cleanup goes as planned. If we
130   // destruct this class, then Process::~Process() might have problems
131   // trying to fully destroy the broadcaster.
132   Finalize();
133 }
134 
135 //----------------------------------------------------------------------
136 // PluginInterface
137 //----------------------------------------------------------------------
138 ConstString ProcessMachCore::GetPluginName() { return GetPluginNameStatic(); }
139 
140 uint32_t ProcessMachCore::GetPluginVersion() { return 1; }
141 
142 bool ProcessMachCore::GetDynamicLoaderAddress(lldb::addr_t addr) {
143   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER |
144                                                   LIBLLDB_LOG_PROCESS));
145   llvm::MachO::mach_header header;
146   Error error;
147   if (DoReadMemory(addr, &header, sizeof(header), error) != sizeof(header))
148     return false;
149   if (header.magic == llvm::MachO::MH_CIGAM ||
150       header.magic == llvm::MachO::MH_CIGAM_64) {
151     header.magic = llvm::ByteSwap_32(header.magic);
152     header.cputype = llvm::ByteSwap_32(header.cputype);
153     header.cpusubtype = llvm::ByteSwap_32(header.cpusubtype);
154     header.filetype = llvm::ByteSwap_32(header.filetype);
155     header.ncmds = llvm::ByteSwap_32(header.ncmds);
156     header.sizeofcmds = llvm::ByteSwap_32(header.sizeofcmds);
157     header.flags = llvm::ByteSwap_32(header.flags);
158   }
159 
160   // TODO: swap header if needed...
161   // printf("0x%16.16" PRIx64 ": magic = 0x%8.8x, file_type= %u\n", vaddr,
162   // header.magic, header.filetype);
163   if (header.magic == llvm::MachO::MH_MAGIC ||
164       header.magic == llvm::MachO::MH_MAGIC_64) {
165     // Check MH_EXECUTABLE to see if we can find the mach image
166     // that contains the shared library list. The dynamic loader
167     // (dyld) is what contains the list for user applications,
168     // and the mach kernel contains a global that has the list
169     // of kexts to load
170     switch (header.filetype) {
171     case llvm::MachO::MH_DYLINKER:
172       // printf("0x%16.16" PRIx64 ": file_type = MH_DYLINKER\n", vaddr);
173       // Address of dyld "struct mach_header" in the core file
174       if (log)
175         log->Printf("ProcessMachCore::GetDynamicLoaderAddress found a user "
176                     "process dyld binary image at 0x%" PRIx64,
177                     addr);
178       m_dyld_addr = addr;
179       return true;
180 
181     case llvm::MachO::MH_EXECUTE:
182       // printf("0x%16.16" PRIx64 ": file_type = MH_EXECUTE\n", vaddr);
183       // Check MH_EXECUTABLE file types to see if the dynamic link object flag
184       // is NOT set. If it isn't, then we have a mach_kernel.
185       if ((header.flags & llvm::MachO::MH_DYLDLINK) == 0) {
186         if (log)
187           log->Printf("ProcessMachCore::GetDynamicLoaderAddress found a mach "
188                       "kernel binary image at 0x%" PRIx64,
189                       addr);
190         // Address of the mach kernel "struct mach_header" in the core file.
191         m_mach_kernel_addr = addr;
192         return true;
193       }
194       break;
195     }
196   }
197   return false;
198 }
199 
200 //----------------------------------------------------------------------
201 // Process Control
202 //----------------------------------------------------------------------
203 Error ProcessMachCore::DoLoadCore() {
204   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER |
205                                                   LIBLLDB_LOG_PROCESS));
206   Error error;
207   if (!m_core_module_sp) {
208     error.SetErrorString("invalid core module");
209     return error;
210   }
211 
212   ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
213   if (core_objfile == NULL) {
214     error.SetErrorString("invalid core object file");
215     return error;
216   }
217 
218   if (core_objfile->GetNumThreadContexts() == 0) {
219     error.SetErrorString("core file doesn't contain any LC_THREAD load "
220                          "commands, or the LC_THREAD architecture is not "
221                          "supported in this lldb");
222     return error;
223   }
224 
225   SectionList *section_list = core_objfile->GetSectionList();
226   if (section_list == NULL) {
227     error.SetErrorString("core file has no sections");
228     return error;
229   }
230 
231   const uint32_t num_sections = section_list->GetNumSections(0);
232   if (num_sections == 0) {
233     error.SetErrorString("core file has no sections");
234     return error;
235   }
236 
237   SetCanJIT(false);
238 
239   llvm::MachO::mach_header header;
240   DataExtractor data(&header, sizeof(header),
241                      m_core_module_sp->GetArchitecture().GetByteOrder(),
242                      m_core_module_sp->GetArchitecture().GetAddressByteSize());
243 
244   bool ranges_are_sorted = true;
245   addr_t vm_addr = 0;
246   for (uint32_t i = 0; i < num_sections; ++i) {
247     Section *section = section_list->GetSectionAtIndex(i).get();
248     if (section) {
249       lldb::addr_t section_vm_addr = section->GetFileAddress();
250       FileRange file_range(section->GetFileOffset(), section->GetFileSize());
251       VMRangeToFileOffset::Entry range_entry(
252           section_vm_addr, section->GetByteSize(), file_range);
253 
254       if (vm_addr > section_vm_addr)
255         ranges_are_sorted = false;
256       vm_addr = section->GetFileAddress();
257       VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back();
258       //            printf ("LC_SEGMENT[%u] arange=[0x%16.16" PRIx64 " -
259       //            0x%16.16" PRIx64 "), frange=[0x%8.8x - 0x%8.8x)\n",
260       //                    i,
261       //                    range_entry.GetRangeBase(),
262       //                    range_entry.GetRangeEnd(),
263       //                    range_entry.data.GetRangeBase(),
264       //                    range_entry.data.GetRangeEnd());
265 
266       if (last_entry &&
267           last_entry->GetRangeEnd() == range_entry.GetRangeBase() &&
268           last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase()) {
269         last_entry->SetRangeEnd(range_entry.GetRangeEnd());
270         last_entry->data.SetRangeEnd(range_entry.data.GetRangeEnd());
271         // puts("combine");
272       } else {
273         m_core_aranges.Append(range_entry);
274       }
275       // Some core files don't fill in the permissions correctly. If that is the
276       // case
277       // assume read + execute so clients don't think the memory is not
278       // readable,
279       // or executable. The memory isn't writable since this plug-in doesn't
280       // implement
281       // DoWriteMemory.
282       uint32_t permissions = section->GetPermissions();
283       if (permissions == 0)
284         permissions = lldb::ePermissionsReadable | lldb::ePermissionsExecutable;
285       m_core_range_infos.Append(VMRangeToPermissions::Entry(
286           section_vm_addr, section->GetByteSize(), permissions));
287     }
288   }
289   if (!ranges_are_sorted) {
290     m_core_aranges.Sort();
291     m_core_range_infos.Sort();
292   }
293 
294   if (m_dyld_addr == LLDB_INVALID_ADDRESS ||
295       m_mach_kernel_addr == LLDB_INVALID_ADDRESS) {
296     // We need to locate the main executable in the memory ranges
297     // we have in the core file.  We need to search for both a user-process dyld
298     // binary
299     // and a kernel binary in memory; we must look at all the pages in the
300     // binary so
301     // we don't miss one or the other.  Step through all memory segments
302     // searching for
303     // a kernel binary and for a user process dyld -- we'll decide which to
304     // prefer
305     // later if both are present.
306 
307     const size_t num_core_aranges = m_core_aranges.GetSize();
308     for (size_t i = 0; i < num_core_aranges; ++i) {
309       const VMRangeToFileOffset::Entry *entry =
310           m_core_aranges.GetEntryAtIndex(i);
311       lldb::addr_t section_vm_addr_start = entry->GetRangeBase();
312       lldb::addr_t section_vm_addr_end = entry->GetRangeEnd();
313       for (lldb::addr_t section_vm_addr = section_vm_addr_start;
314            section_vm_addr < section_vm_addr_end; section_vm_addr += 0x1000) {
315         GetDynamicLoaderAddress(section_vm_addr);
316       }
317     }
318   }
319 
320   if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) {
321     // In the case of multiple kernel images found in the core file via
322     // exhaustive
323     // search, we may not pick the correct one.  See if the
324     // DynamicLoaderDarwinKernel's
325     // search heuristics might identify the correct one.
326     // Most of the time, I expect the address from SearchForDarwinKernel() will
327     // be the
328     // same as the address we found via exhaustive search.
329 
330     if (GetTarget().GetArchitecture().IsValid() == false &&
331         m_core_module_sp.get()) {
332       GetTarget().SetArchitecture(m_core_module_sp->GetArchitecture());
333     }
334 
335     // SearchForDarwinKernel will end up calling back into this this class in
336     // the GetImageInfoAddress
337     // method which will give it the m_mach_kernel_addr/m_dyld_addr it already
338     // has.  Save that aside
339     // and set m_mach_kernel_addr/m_dyld_addr to an invalid address temporarily
340     // so
341     // DynamicLoaderDarwinKernel does a real search for the kernel using its own
342     // heuristics.
343 
344     addr_t saved_mach_kernel_addr = m_mach_kernel_addr;
345     addr_t saved_user_dyld_addr = m_dyld_addr;
346     m_mach_kernel_addr = LLDB_INVALID_ADDRESS;
347     m_dyld_addr = LLDB_INVALID_ADDRESS;
348 
349     addr_t better_kernel_address =
350         DynamicLoaderDarwinKernel::SearchForDarwinKernel(this);
351 
352     m_mach_kernel_addr = saved_mach_kernel_addr;
353     m_dyld_addr = saved_user_dyld_addr;
354 
355     if (better_kernel_address != LLDB_INVALID_ADDRESS) {
356       if (log)
357         log->Printf("ProcessMachCore::DoLoadCore: Using the kernel address "
358                     "from DynamicLoaderDarwinKernel");
359       m_mach_kernel_addr = better_kernel_address;
360     }
361   }
362 
363   // If we found both a user-process dyld and a kernel binary, we need to decide
364   // which to prefer.
365   if (GetCorefilePreference() == eKernelCorefile) {
366     if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) {
367       if (log)
368         log->Printf("ProcessMachCore::DoLoadCore: Using kernel corefile image "
369                     "at 0x%" PRIx64,
370                     m_mach_kernel_addr);
371       m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
372     } else if (m_dyld_addr != LLDB_INVALID_ADDRESS) {
373       if (log)
374         log->Printf("ProcessMachCore::DoLoadCore: Using user process dyld "
375                     "image at 0x%" PRIx64,
376                     m_dyld_addr);
377       m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic();
378     }
379   } else {
380     if (m_dyld_addr != LLDB_INVALID_ADDRESS) {
381       if (log)
382         log->Printf("ProcessMachCore::DoLoadCore: Using user process dyld "
383                     "image at 0x%" PRIx64,
384                     m_dyld_addr);
385       m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic();
386     } else if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) {
387       if (log)
388         log->Printf("ProcessMachCore::DoLoadCore: Using kernel corefile image "
389                     "at 0x%" PRIx64,
390                     m_mach_kernel_addr);
391       m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic();
392     }
393   }
394 
395   if (m_dyld_plugin_name != DynamicLoaderMacOSXDYLD::GetPluginNameStatic()) {
396     // For non-user process core files, the permissions on the core file
397     // segments are usually
398     // meaningless, they may be just "read", because we're dealing with kernel
399     // coredumps or
400     // early startup coredumps and the dumper is grabbing pages of memory
401     // without knowing
402     // what they are.  If they aren't marked as "exeuctable", that can break the
403     // unwinder
404     // which will check a pc value to see if it is in an executable segment and
405     // stop the
406     // backtrace early if it is not ("executable" and "unknown" would both be
407     // fine, but
408     // "not executable" will break the unwinder).
409     size_t core_range_infos_size = m_core_range_infos.GetSize();
410     for (size_t i = 0; i < core_range_infos_size; i++) {
411       VMRangeToPermissions::Entry *ent =
412           m_core_range_infos.GetMutableEntryAtIndex(i);
413       ent->data = lldb::ePermissionsReadable | lldb::ePermissionsExecutable;
414     }
415   }
416 
417   // Even if the architecture is set in the target, we need to override
418   // it to match the core file which is always single arch.
419   ArchSpec arch(m_core_module_sp->GetArchitecture());
420   if (arch.GetCore() == ArchSpec::eCore_x86_32_i486) {
421     arch.SetTriple("i386", GetTarget().GetPlatform().get());
422   }
423   if (arch.IsValid())
424     GetTarget().SetArchitecture(arch);
425 
426   return error;
427 }
428 
429 lldb_private::DynamicLoader *ProcessMachCore::GetDynamicLoader() {
430   if (m_dyld_ap.get() == NULL)
431     m_dyld_ap.reset(DynamicLoader::FindPlugin(
432         this,
433         m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString()));
434   return m_dyld_ap.get();
435 }
436 
437 bool ProcessMachCore::UpdateThreadList(ThreadList &old_thread_list,
438                                        ThreadList &new_thread_list) {
439   if (old_thread_list.GetSize(false) == 0) {
440     // Make up the thread the first time this is called so we can setup our one
441     // and only
442     // core thread state.
443     ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
444 
445     if (core_objfile) {
446       const uint32_t num_threads = core_objfile->GetNumThreadContexts();
447       for (lldb::tid_t tid = 0; tid < num_threads; ++tid) {
448         ThreadSP thread_sp(new ThreadMachCore(*this, tid));
449         new_thread_list.AddThread(thread_sp);
450       }
451     }
452   } else {
453     const uint32_t num_threads = old_thread_list.GetSize(false);
454     for (uint32_t i = 0; i < num_threads; ++i)
455       new_thread_list.AddThread(old_thread_list.GetThreadAtIndex(i, false));
456   }
457   return new_thread_list.GetSize(false) > 0;
458 }
459 
460 void ProcessMachCore::RefreshStateAfterStop() {
461   // Let all threads recover from stopping and do any clean up based
462   // on the previous thread state (if any).
463   m_thread_list.RefreshStateAfterStop();
464   // SetThreadStopInfo (m_last_stop_packet);
465 }
466 
467 Error ProcessMachCore::DoDestroy() { return Error(); }
468 
469 //------------------------------------------------------------------
470 // Process Queries
471 //------------------------------------------------------------------
472 
473 bool ProcessMachCore::IsAlive() { return true; }
474 
475 bool ProcessMachCore::WarnBeforeDetach() const { return false; }
476 
477 //------------------------------------------------------------------
478 // Process Memory
479 //------------------------------------------------------------------
480 size_t ProcessMachCore::ReadMemory(addr_t addr, void *buf, size_t size,
481                                    Error &error) {
482   // Don't allow the caching that lldb_private::Process::ReadMemory does
483   // since in core files we have it all cached our our core file anyway.
484   return DoReadMemory(addr, buf, size, error);
485 }
486 
487 size_t ProcessMachCore::DoReadMemory(addr_t addr, void *buf, size_t size,
488                                      Error &error) {
489   ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
490   size_t bytes_read = 0;
491 
492   if (core_objfile) {
493     //----------------------------------------------------------------------
494     // Segments are not always contiguous in mach-o core files. We have core
495     // files that have segments like:
496     //            Address    Size       File off   File size
497     //            ---------- ---------- ---------- ----------
498     // LC_SEGMENT 0x000f6000 0x00001000 0x1d509ee8 0x00001000 --- ---   0
499     // 0x00000000 __TEXT
500     // LC_SEGMENT 0x0f600000 0x00100000 0x1d50aee8 0x00100000 --- ---   0
501     // 0x00000000 __TEXT
502     // LC_SEGMENT 0x000f7000 0x00001000 0x1d60aee8 0x00001000 --- ---   0
503     // 0x00000000 __TEXT
504     //
505     // Any if the user executes the following command:
506     //
507     // (lldb) mem read 0xf6ff0
508     //
509     // We would attempt to read 32 bytes from 0xf6ff0 but would only
510     // get 16 unless we loop through consecutive memory ranges that are
511     // contiguous in the address space, but not in the file data.
512     //----------------------------------------------------------------------
513     while (bytes_read < size) {
514       const addr_t curr_addr = addr + bytes_read;
515       const VMRangeToFileOffset::Entry *core_memory_entry =
516           m_core_aranges.FindEntryThatContains(curr_addr);
517 
518       if (core_memory_entry) {
519         const addr_t offset = curr_addr - core_memory_entry->GetRangeBase();
520         const addr_t bytes_left = core_memory_entry->GetRangeEnd() - curr_addr;
521         const size_t bytes_to_read =
522             std::min(size - bytes_read, (size_t)bytes_left);
523         const size_t curr_bytes_read = core_objfile->CopyData(
524             core_memory_entry->data.GetRangeBase() + offset, bytes_to_read,
525             (char *)buf + bytes_read);
526         if (curr_bytes_read == 0)
527           break;
528         bytes_read += curr_bytes_read;
529       } else {
530         // Only set the error if we didn't read any bytes
531         if (bytes_read == 0)
532           error.SetErrorStringWithFormat(
533               "core file does not contain 0x%" PRIx64, curr_addr);
534         break;
535       }
536     }
537   }
538 
539   return bytes_read;
540 }
541 
542 Error ProcessMachCore::GetMemoryRegionInfo(addr_t load_addr,
543                                            MemoryRegionInfo &region_info) {
544   region_info.Clear();
545   const VMRangeToPermissions::Entry *permission_entry =
546       m_core_range_infos.FindEntryThatContainsOrFollows(load_addr);
547   if (permission_entry) {
548     if (permission_entry->Contains(load_addr)) {
549       region_info.GetRange().SetRangeBase(permission_entry->GetRangeBase());
550       region_info.GetRange().SetRangeEnd(permission_entry->GetRangeEnd());
551       const Flags permissions(permission_entry->data);
552       region_info.SetReadable(permissions.Test(ePermissionsReadable)
553                                   ? MemoryRegionInfo::eYes
554                                   : MemoryRegionInfo::eNo);
555       region_info.SetWritable(permissions.Test(ePermissionsWritable)
556                                   ? MemoryRegionInfo::eYes
557                                   : MemoryRegionInfo::eNo);
558       region_info.SetExecutable(permissions.Test(ePermissionsExecutable)
559                                     ? MemoryRegionInfo::eYes
560                                     : MemoryRegionInfo::eNo);
561       region_info.SetMapped(MemoryRegionInfo::eYes);
562     } else if (load_addr < permission_entry->GetRangeBase()) {
563       region_info.GetRange().SetRangeBase(load_addr);
564       region_info.GetRange().SetRangeEnd(permission_entry->GetRangeBase());
565       region_info.SetReadable(MemoryRegionInfo::eNo);
566       region_info.SetWritable(MemoryRegionInfo::eNo);
567       region_info.SetExecutable(MemoryRegionInfo::eNo);
568       region_info.SetMapped(MemoryRegionInfo::eNo);
569     }
570     return Error();
571   }
572 
573   region_info.GetRange().SetRangeBase(load_addr);
574   region_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS);
575   region_info.SetReadable(MemoryRegionInfo::eNo);
576   region_info.SetWritable(MemoryRegionInfo::eNo);
577   region_info.SetExecutable(MemoryRegionInfo::eNo);
578   region_info.SetMapped(MemoryRegionInfo::eNo);
579   return Error();
580 }
581 
582 void ProcessMachCore::Clear() { m_thread_list.Clear(); }
583 
584 void ProcessMachCore::Initialize() {
585   static llvm::once_flag g_once_flag;
586 
587   llvm::call_once(g_once_flag, []() {
588     PluginManager::RegisterPlugin(GetPluginNameStatic(),
589                                   GetPluginDescriptionStatic(), CreateInstance);
590   });
591 }
592 
593 addr_t ProcessMachCore::GetImageInfoAddress() {
594   // If we found both a user-process dyld and a kernel binary, we need to decide
595   // which to prefer.
596   if (GetCorefilePreference() == eKernelCorefile) {
597     if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) {
598       return m_mach_kernel_addr;
599     }
600     return m_dyld_addr;
601   } else {
602     if (m_dyld_addr != LLDB_INVALID_ADDRESS) {
603       return m_dyld_addr;
604     }
605     return m_mach_kernel_addr;
606   }
607 }
608 
609 lldb_private::ObjectFile *ProcessMachCore::GetCoreObjectFile() {
610   return m_core_module_sp->GetObjectFile();
611 }
612