1 //===-- DynamicLoaderMacOSXDYLD.cpp ---------------------------------------===//
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 "DynamicLoaderMacOSXDYLD.h"
10 #include "DynamicLoaderDarwin.h"
11 #include "DynamicLoaderMacOS.h"
12 #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
13 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
14 #include "lldb/Breakpoint/StoppointCallbackContext.h"
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/ModuleSpec.h"
18 #include "lldb/Core/PluginManager.h"
19 #include "lldb/Core/Section.h"
20 #include "lldb/Symbol/Function.h"
21 #include "lldb/Symbol/ObjectFile.h"
22 #include "lldb/Target/ABI.h"
23 #include "lldb/Target/RegisterContext.h"
24 #include "lldb/Target/StackFrame.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Target/Thread.h"
27 #include "lldb/Target/ThreadPlanRunToAddress.h"
28 #include "lldb/Utility/DataBuffer.h"
29 #include "lldb/Utility/DataBufferHeap.h"
30 #include "lldb/Utility/Log.h"
31 #include "lldb/Utility/State.h"
32 
33 //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
34 #ifdef ENABLE_DEBUG_PRINTF
35 #include <cstdio>
36 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
37 #else
38 #define DEBUG_PRINTF(fmt, ...)
39 #endif
40 
41 #ifndef __APPLE__
42 #include "Utility/UuidCompatibility.h"
43 #else
44 #include <uuid/uuid.h>
45 #endif
46 
47 using namespace lldb;
48 using namespace lldb_private;
49 
50 LLDB_PLUGIN_DEFINE(DynamicLoaderMacOSXDYLD)
51 
52 // Create an instance of this class. This function is filled into the plugin
53 // info class that gets handed out by the plugin factory and allows the lldb to
54 // instantiate an instance of this class.
55 DynamicLoader *DynamicLoaderMacOSXDYLD::CreateInstance(Process *process,
56                                                        bool force) {
57   bool create = force;
58   if (!create) {
59     create = true;
60     Module *exe_module = process->GetTarget().GetExecutableModulePointer();
61     if (exe_module) {
62       ObjectFile *object_file = exe_module->GetObjectFile();
63       if (object_file) {
64         create = (object_file->GetStrata() == ObjectFile::eStrataUser);
65       }
66     }
67 
68     if (create) {
69       const llvm::Triple &triple_ref =
70           process->GetTarget().GetArchitecture().GetTriple();
71       switch (triple_ref.getOS()) {
72       case llvm::Triple::Darwin:
73       case llvm::Triple::MacOSX:
74       case llvm::Triple::IOS:
75       case llvm::Triple::TvOS:
76       case llvm::Triple::WatchOS:
77       // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
78         create = triple_ref.getVendor() == llvm::Triple::Apple;
79         break;
80       default:
81         create = false;
82         break;
83       }
84     }
85   }
86 
87   if (UseDYLDSPI(process)) {
88     create = false;
89   }
90 
91   if (create)
92     return new DynamicLoaderMacOSXDYLD(process);
93   return nullptr;
94 }
95 
96 // Constructor
97 DynamicLoaderMacOSXDYLD::DynamicLoaderMacOSXDYLD(Process *process)
98     : DynamicLoaderDarwin(process),
99       m_dyld_all_image_infos_addr(LLDB_INVALID_ADDRESS),
100       m_dyld_all_image_infos(), m_dyld_all_image_infos_stop_id(UINT32_MAX),
101       m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
102       m_process_image_addr_is_all_images_infos(false) {}
103 
104 // Destructor
105 DynamicLoaderMacOSXDYLD::~DynamicLoaderMacOSXDYLD() {
106   if (LLDB_BREAK_ID_IS_VALID(m_break_id))
107     m_process->GetTarget().RemoveBreakpointByID(m_break_id);
108 }
109 
110 bool DynamicLoaderMacOSXDYLD::ProcessDidExec() {
111   std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
112   bool did_exec = false;
113   if (m_process) {
114     // If we are stopped after an exec, we will have only one thread...
115     if (m_process->GetThreadList().GetSize() == 1) {
116       // We know if a process has exec'ed if our "m_dyld_all_image_infos_addr"
117       // value differs from the Process' image info address. When a process
118       // execs itself it might cause a change if ASLR is enabled.
119       const addr_t shlib_addr = m_process->GetImageInfoAddress();
120       if (m_process_image_addr_is_all_images_infos &&
121           shlib_addr != m_dyld_all_image_infos_addr) {
122         // The image info address from the process is the
123         // 'dyld_all_image_infos' address and it has changed.
124         did_exec = true;
125       } else if (!m_process_image_addr_is_all_images_infos &&
126                  shlib_addr == m_dyld.address) {
127         // The image info address from the process is the mach_header address
128         // for dyld and it has changed.
129         did_exec = true;
130       } else {
131         // ASLR might be disabled and dyld could have ended up in the same
132         // location. We should try and detect if we are stopped at
133         // '_dyld_start'
134         ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0));
135         if (thread_sp) {
136           lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
137           if (frame_sp) {
138             const Symbol *symbol =
139                 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
140             if (symbol) {
141               if (symbol->GetName() == "_dyld_start")
142                 did_exec = true;
143             }
144           }
145         }
146       }
147 
148       if (did_exec) {
149         m_libpthread_module_wp.reset();
150         m_pthread_getspecific_addr.Clear();
151       }
152     }
153   }
154   return did_exec;
155 }
156 
157 // Clear out the state of this class.
158 void DynamicLoaderMacOSXDYLD::DoClear() {
159   std::lock_guard<std::recursive_mutex> guard(m_mutex);
160 
161   if (LLDB_BREAK_ID_IS_VALID(m_break_id))
162     m_process->GetTarget().RemoveBreakpointByID(m_break_id);
163 
164   m_dyld_all_image_infos_addr = LLDB_INVALID_ADDRESS;
165   m_dyld_all_image_infos.Clear();
166   m_break_id = LLDB_INVALID_BREAK_ID;
167 }
168 
169 // Check if we have found DYLD yet
170 bool DynamicLoaderMacOSXDYLD::DidSetNotificationBreakpoint() {
171   return LLDB_BREAK_ID_IS_VALID(m_break_id);
172 }
173 
174 void DynamicLoaderMacOSXDYLD::ClearNotificationBreakpoint() {
175   if (LLDB_BREAK_ID_IS_VALID(m_break_id)) {
176     m_process->GetTarget().RemoveBreakpointByID(m_break_id);
177   }
178 }
179 
180 // Try and figure out where dyld is by first asking the Process if it knows
181 // (which currently calls down in the lldb::Process to get the DYLD info
182 // (available on SnowLeopard only). If that fails, then check in the default
183 // addresses.
184 void DynamicLoaderMacOSXDYLD::DoInitialImageFetch() {
185   if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS) {
186     // Check the image info addr as it might point to the mach header for dyld,
187     // or it might point to the dyld_all_image_infos struct
188     const addr_t shlib_addr = m_process->GetImageInfoAddress();
189     if (shlib_addr != LLDB_INVALID_ADDRESS) {
190       ByteOrder byte_order =
191           m_process->GetTarget().GetArchitecture().GetByteOrder();
192       uint8_t buf[4];
193       DataExtractor data(buf, sizeof(buf), byte_order, 4);
194       Status error;
195       if (m_process->ReadMemory(shlib_addr, buf, 4, error) == 4) {
196         lldb::offset_t offset = 0;
197         uint32_t magic = data.GetU32(&offset);
198         switch (magic) {
199         case llvm::MachO::MH_MAGIC:
200         case llvm::MachO::MH_MAGIC_64:
201         case llvm::MachO::MH_CIGAM:
202         case llvm::MachO::MH_CIGAM_64:
203           m_process_image_addr_is_all_images_infos = false;
204           ReadDYLDInfoFromMemoryAndSetNotificationCallback(shlib_addr);
205           return;
206 
207         default:
208           break;
209         }
210       }
211       // Maybe it points to the all image infos?
212       m_dyld_all_image_infos_addr = shlib_addr;
213       m_process_image_addr_is_all_images_infos = true;
214     }
215   }
216 
217   if (m_dyld_all_image_infos_addr != LLDB_INVALID_ADDRESS) {
218     if (ReadAllImageInfosStructure()) {
219       if (m_dyld_all_image_infos.dyldImageLoadAddress != LLDB_INVALID_ADDRESS)
220         ReadDYLDInfoFromMemoryAndSetNotificationCallback(
221             m_dyld_all_image_infos.dyldImageLoadAddress);
222       else
223         ReadDYLDInfoFromMemoryAndSetNotificationCallback(
224             m_dyld_all_image_infos_addr & 0xfffffffffff00000ull);
225       return;
226     }
227   }
228 
229   // Check some default values
230   Module *executable = m_process->GetTarget().GetExecutableModulePointer();
231 
232   if (executable) {
233     const ArchSpec &exe_arch = executable->GetArchitecture();
234     if (exe_arch.GetAddressByteSize() == 8) {
235       ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x7fff5fc00000ull);
236     } else if (exe_arch.GetMachine() == llvm::Triple::arm ||
237                exe_arch.GetMachine() == llvm::Triple::thumb ||
238                exe_arch.GetMachine() == llvm::Triple::aarch64 ||
239                exe_arch.GetMachine() == llvm::Triple::aarch64_32) {
240       ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x2fe00000);
241     } else {
242       ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x8fe00000);
243     }
244   }
245 }
246 
247 // Assume that dyld is in memory at ADDR and try to parse it's load commands
248 bool DynamicLoaderMacOSXDYLD::ReadDYLDInfoFromMemoryAndSetNotificationCallback(
249     lldb::addr_t addr) {
250   std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
251   DataExtractor data; // Load command data
252   static ConstString g_dyld_all_image_infos("dyld_all_image_infos");
253   static ConstString g_new_dyld_all_image_infos("dyld4::dyld_all_image_infos");
254   if (ReadMachHeader(addr, &m_dyld.header, &data)) {
255     if (m_dyld.header.filetype == llvm::MachO::MH_DYLINKER) {
256       m_dyld.address = addr;
257       ModuleSP dyld_module_sp;
258       if (ParseLoadCommands(data, m_dyld, &m_dyld.file_spec)) {
259         if (m_dyld.file_spec) {
260           UpdateDYLDImageInfoFromNewImageInfo(m_dyld);
261         }
262       }
263       dyld_module_sp = GetDYLDModule();
264 
265       Target &target = m_process->GetTarget();
266 
267       if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS &&
268           dyld_module_sp.get()) {
269         const Symbol *symbol = dyld_module_sp->FindFirstSymbolWithNameAndType(
270             g_dyld_all_image_infos, eSymbolTypeData);
271         if (!symbol) {
272           symbol = dyld_module_sp->FindFirstSymbolWithNameAndType(
273               g_new_dyld_all_image_infos, eSymbolTypeData);
274         }
275         if (symbol)
276           m_dyld_all_image_infos_addr = symbol->GetLoadAddress(&target);
277       }
278 
279       // Update all image infos
280       InitializeFromAllImageInfos();
281 
282       // If we didn't have an executable before, but now we do, then the dyld
283       // module shared pointer might be unique and we may need to add it again
284       // (since Target::SetExecutableModule() will clear the images). So append
285       // the dyld module back to the list if it is
286       /// unique!
287       if (dyld_module_sp) {
288         target.GetImages().AppendIfNeeded(dyld_module_sp);
289 
290         // At this point we should have read in dyld's module, and so we should
291         // set breakpoints in it:
292         ModuleList modules;
293         modules.Append(dyld_module_sp);
294         target.ModulesDidLoad(modules);
295         SetDYLDModule(dyld_module_sp);
296       }
297 
298       return true;
299     }
300   }
301   return false;
302 }
303 
304 bool DynamicLoaderMacOSXDYLD::NeedToDoInitialImageFetch() {
305   return m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS;
306 }
307 
308 // Static callback function that gets called when our DYLD notification
309 // breakpoint gets hit. We update all of our image infos and then let our super
310 // class DynamicLoader class decide if we should stop or not (based on global
311 // preference).
312 bool DynamicLoaderMacOSXDYLD::NotifyBreakpointHit(
313     void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id,
314     lldb::user_id_t break_loc_id) {
315   // Let the event know that the images have changed
316   // DYLD passes three arguments to the notification breakpoint.
317   // Arg1: enum dyld_image_mode mode - 0 = adding, 1 = removing Arg2: uint32_t
318   // infoCount        - Number of shared libraries added Arg3: dyld_image_info
319   // info[]    - Array of structs of the form:
320   //                                     const struct mach_header
321   //                                     *imageLoadAddress
322   //                                     const char               *imageFilePath
323   //                                     uintptr_t imageFileModDate (a time_t)
324 
325   DynamicLoaderMacOSXDYLD *dyld_instance = (DynamicLoaderMacOSXDYLD *)baton;
326 
327   // First step is to see if we've already initialized the all image infos.  If
328   // we haven't then this function will do so and return true.  In the course
329   // of initializing the all_image_infos it will read the complete current
330   // state, so we don't need to figure out what has changed from the data
331   // passed in to us.
332 
333   ExecutionContext exe_ctx(context->exe_ctx_ref);
334   Process *process = exe_ctx.GetProcessPtr();
335 
336   // This is a sanity check just in case this dyld_instance is an old dyld
337   // plugin's breakpoint still lying around.
338   if (process != dyld_instance->m_process)
339     return false;
340 
341   if (dyld_instance->InitializeFromAllImageInfos())
342     return dyld_instance->GetStopWhenImagesChange();
343 
344   const lldb::ABISP &abi = process->GetABI();
345   if (abi) {
346     // Build up the value array to store the three arguments given above, then
347     // get the values from the ABI:
348 
349     TypeSystemClang *clang_ast_context =
350         ScratchTypeSystemClang::GetForTarget(process->GetTarget());
351     if (!clang_ast_context)
352       return false;
353 
354     ValueList argument_values;
355     Value input_value;
356 
357     CompilerType clang_void_ptr_type =
358         clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
359     CompilerType clang_uint32_type =
360         clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
361             lldb::eEncodingUint, 32);
362     input_value.SetValueType(Value::ValueType::Scalar);
363     input_value.SetCompilerType(clang_uint32_type);
364     //        input_value.SetContext (Value::eContextTypeClangType,
365     //        clang_uint32_type);
366     argument_values.PushValue(input_value);
367     argument_values.PushValue(input_value);
368     input_value.SetCompilerType(clang_void_ptr_type);
369     //        input_value.SetContext (Value::eContextTypeClangType,
370     //        clang_void_ptr_type);
371     argument_values.PushValue(input_value);
372 
373     if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) {
374       uint32_t dyld_mode =
375           argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1);
376       if (dyld_mode != static_cast<uint32_t>(-1)) {
377         // Okay the mode was right, now get the number of elements, and the
378         // array of new elements...
379         uint32_t image_infos_count =
380             argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1);
381         if (image_infos_count != static_cast<uint32_t>(-1)) {
382           // Got the number added, now go through the array of added elements,
383           // putting out the mach header address, and adding the image. Note,
384           // I'm not putting in logging here, since the AddModules &
385           // RemoveModules functions do all the logging internally.
386 
387           lldb::addr_t image_infos_addr =
388               argument_values.GetValueAtIndex(2)->GetScalar().ULongLong();
389           if (dyld_mode == 0) {
390             // This is add:
391             dyld_instance->AddModulesUsingImageInfosAddress(image_infos_addr,
392                                                             image_infos_count);
393           } else {
394             // This is remove:
395             dyld_instance->RemoveModulesUsingImageInfosAddress(
396                 image_infos_addr, image_infos_count);
397           }
398         }
399       }
400     }
401   } else {
402     process->GetTarget().GetDebugger().GetAsyncErrorStream()->Printf(
403         "No ABI plugin located for triple %s -- shared libraries will not be "
404         "registered!\n",
405         process->GetTarget().GetArchitecture().GetTriple().getTriple().c_str());
406   }
407 
408   // Return true to stop the target, false to just let the target run
409   return dyld_instance->GetStopWhenImagesChange();
410 }
411 
412 bool DynamicLoaderMacOSXDYLD::ReadAllImageInfosStructure() {
413   std::lock_guard<std::recursive_mutex> guard(m_mutex);
414 
415   // the all image infos is already valid for this process stop ID
416   if (m_process->GetStopID() == m_dyld_all_image_infos_stop_id)
417     return true;
418 
419   m_dyld_all_image_infos.Clear();
420   if (m_dyld_all_image_infos_addr != LLDB_INVALID_ADDRESS) {
421     ByteOrder byte_order =
422         m_process->GetTarget().GetArchitecture().GetByteOrder();
423     uint32_t addr_size =
424         m_process->GetTarget().GetArchitecture().GetAddressByteSize();
425 
426     uint8_t buf[256];
427     DataExtractor data(buf, sizeof(buf), byte_order, addr_size);
428     lldb::offset_t offset = 0;
429 
430     const size_t count_v2 = sizeof(uint32_t) + // version
431                             sizeof(uint32_t) + // infoArrayCount
432                             addr_size +        // infoArray
433                             addr_size +        // notification
434                             addr_size + // processDetachedFromSharedRegion +
435                                         // libSystemInitialized + pad
436                             addr_size;  // dyldImageLoadAddress
437     const size_t count_v11 = count_v2 + addr_size +  // jitInfo
438                              addr_size +             // dyldVersion
439                              addr_size +             // errorMessage
440                              addr_size +             // terminationFlags
441                              addr_size +             // coreSymbolicationShmPage
442                              addr_size +             // systemOrderFlag
443                              addr_size +             // uuidArrayCount
444                              addr_size +             // uuidArray
445                              addr_size +             // dyldAllImageInfosAddress
446                              addr_size +             // initialImageCount
447                              addr_size +             // errorKind
448                              addr_size +             // errorClientOfDylibPath
449                              addr_size +             // errorTargetDylibPath
450                              addr_size;              // errorSymbol
451     const size_t count_v13 = count_v11 + addr_size + // sharedCacheSlide
452                              sizeof(uuid_t);         // sharedCacheUUID
453     UNUSED_IF_ASSERT_DISABLED(count_v13);
454     assert(sizeof(buf) >= count_v13);
455 
456     Status error;
457     if (m_process->ReadMemory(m_dyld_all_image_infos_addr, buf, 4, error) ==
458         4) {
459       m_dyld_all_image_infos.version = data.GetU32(&offset);
460       // If anything in the high byte is set, we probably got the byte order
461       // incorrect (the process might not have it set correctly yet due to
462       // attaching to a program without a specified file).
463       if (m_dyld_all_image_infos.version & 0xff000000) {
464         // We have guessed the wrong byte order. Swap it and try reading the
465         // version again.
466         if (byte_order == eByteOrderLittle)
467           byte_order = eByteOrderBig;
468         else
469           byte_order = eByteOrderLittle;
470 
471         data.SetByteOrder(byte_order);
472         offset = 0;
473         m_dyld_all_image_infos.version = data.GetU32(&offset);
474       }
475     } else {
476       return false;
477     }
478 
479     const size_t count =
480         (m_dyld_all_image_infos.version >= 11) ? count_v11 : count_v2;
481 
482     const size_t bytes_read =
483         m_process->ReadMemory(m_dyld_all_image_infos_addr, buf, count, error);
484     if (bytes_read == count) {
485       offset = 0;
486       m_dyld_all_image_infos.version = data.GetU32(&offset);
487       m_dyld_all_image_infos.dylib_info_count = data.GetU32(&offset);
488       m_dyld_all_image_infos.dylib_info_addr = data.GetAddress(&offset);
489       m_dyld_all_image_infos.notification = data.GetAddress(&offset);
490       m_dyld_all_image_infos.processDetachedFromSharedRegion =
491           data.GetU8(&offset);
492       m_dyld_all_image_infos.libSystemInitialized = data.GetU8(&offset);
493       // Adjust for padding.
494       offset += addr_size - 2;
495       m_dyld_all_image_infos.dyldImageLoadAddress = data.GetAddress(&offset);
496       if (m_dyld_all_image_infos.version >= 11) {
497         offset += addr_size * 8;
498         uint64_t dyld_all_image_infos_addr = data.GetAddress(&offset);
499 
500         // When we started, we were given the actual address of the
501         // all_image_infos struct (probably via TASK_DYLD_INFO) in memory -
502         // this address is stored in m_dyld_all_image_infos_addr and is the
503         // most accurate address we have.
504 
505         // We read the dyld_all_image_infos struct from memory; it contains its
506         // own address. If the address in the struct does not match the actual
507         // address, the dyld we're looking at has been loaded at a different
508         // location (slid) from where it intended to load.  The addresses in
509         // the dyld_all_image_infos struct are the original, non-slid
510         // addresses, and need to be adjusted.  Most importantly the address of
511         // dyld and the notification address need to be adjusted.
512 
513         if (dyld_all_image_infos_addr != m_dyld_all_image_infos_addr) {
514           uint64_t image_infos_offset =
515               dyld_all_image_infos_addr -
516               m_dyld_all_image_infos.dyldImageLoadAddress;
517           uint64_t notification_offset =
518               m_dyld_all_image_infos.notification -
519               m_dyld_all_image_infos.dyldImageLoadAddress;
520           m_dyld_all_image_infos.dyldImageLoadAddress =
521               m_dyld_all_image_infos_addr - image_infos_offset;
522           m_dyld_all_image_infos.notification =
523               m_dyld_all_image_infos.dyldImageLoadAddress + notification_offset;
524         }
525       }
526       m_dyld_all_image_infos_stop_id = m_process->GetStopID();
527       return true;
528     }
529   }
530   return false;
531 }
532 
533 bool DynamicLoaderMacOSXDYLD::AddModulesUsingImageInfosAddress(
534     lldb::addr_t image_infos_addr, uint32_t image_infos_count) {
535   ImageInfo::collection image_infos;
536   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
537   LLDB_LOGF(log, "Adding %d modules.\n", image_infos_count);
538 
539   std::lock_guard<std::recursive_mutex> guard(m_mutex);
540   std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
541   if (m_process->GetStopID() == m_dyld_image_infos_stop_id)
542     return true;
543 
544   StructuredData::ObjectSP image_infos_json_sp =
545       m_process->GetLoadedDynamicLibrariesInfos(image_infos_addr,
546                                                 image_infos_count);
547   if (image_infos_json_sp.get() && image_infos_json_sp->GetAsDictionary() &&
548       image_infos_json_sp->GetAsDictionary()->HasKey("images") &&
549       image_infos_json_sp->GetAsDictionary()
550           ->GetValueForKey("images")
551           ->GetAsArray() &&
552       image_infos_json_sp->GetAsDictionary()
553               ->GetValueForKey("images")
554               ->GetAsArray()
555               ->GetSize() == image_infos_count) {
556     bool return_value = false;
557     if (JSONImageInformationIntoImageInfo(image_infos_json_sp, image_infos)) {
558       UpdateSpecialBinariesFromNewImageInfos(image_infos);
559       return_value = AddModulesUsingImageInfos(image_infos);
560     }
561     m_dyld_image_infos_stop_id = m_process->GetStopID();
562     return return_value;
563   }
564 
565   if (!ReadImageInfos(image_infos_addr, image_infos_count, image_infos))
566     return false;
567 
568   UpdateImageInfosHeaderAndLoadCommands(image_infos, image_infos_count, false);
569   bool return_value = AddModulesUsingImageInfos(image_infos);
570   m_dyld_image_infos_stop_id = m_process->GetStopID();
571   return return_value;
572 }
573 
574 bool DynamicLoaderMacOSXDYLD::RemoveModulesUsingImageInfosAddress(
575     lldb::addr_t image_infos_addr, uint32_t image_infos_count) {
576   ImageInfo::collection image_infos;
577   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
578 
579   std::lock_guard<std::recursive_mutex> guard(m_mutex);
580   std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
581   if (m_process->GetStopID() == m_dyld_image_infos_stop_id)
582     return true;
583 
584   // First read in the image_infos for the removed modules, and their headers &
585   // load commands.
586   if (!ReadImageInfos(image_infos_addr, image_infos_count, image_infos)) {
587     if (log)
588       log->PutCString("Failed reading image infos array.");
589     return false;
590   }
591 
592   LLDB_LOGF(log, "Removing %d modules.", image_infos_count);
593 
594   ModuleList unloaded_module_list;
595   for (uint32_t idx = 0; idx < image_infos.size(); ++idx) {
596     if (log) {
597       LLDB_LOGF(log, "Removing module at address=0x%16.16" PRIx64 ".",
598                 image_infos[idx].address);
599       image_infos[idx].PutToLog(log);
600     }
601 
602     // Remove this image_infos from the m_all_image_infos.  We do the
603     // comparison by address rather than by file spec because we can have many
604     // modules with the same "file spec" in the case that they are modules
605     // loaded from memory.
606     //
607     // Also copy over the uuid from the old entry to the removed entry so we
608     // can use it to lookup the module in the module list.
609 
610     ImageInfo::collection::iterator pos, end = m_dyld_image_infos.end();
611     for (pos = m_dyld_image_infos.begin(); pos != end; pos++) {
612       if (image_infos[idx].address == (*pos).address) {
613         image_infos[idx].uuid = (*pos).uuid;
614 
615         // Add the module from this image_info to the "unloaded_module_list".
616         // We'll remove them all at one go later on.
617 
618         ModuleSP unload_image_module_sp(
619             FindTargetModuleForImageInfo(image_infos[idx], false, nullptr));
620         if (unload_image_module_sp.get()) {
621           // When we unload, be sure to use the image info from the old list,
622           // since that has sections correctly filled in.
623           UnloadModuleSections(unload_image_module_sp.get(), *pos);
624           unloaded_module_list.AppendIfNeeded(unload_image_module_sp);
625         } else {
626           if (log) {
627             LLDB_LOGF(log, "Could not find module for unloading info entry:");
628             image_infos[idx].PutToLog(log);
629           }
630         }
631 
632         // Then remove it from the m_dyld_image_infos:
633 
634         m_dyld_image_infos.erase(pos);
635         break;
636       }
637     }
638 
639     if (pos == end) {
640       if (log) {
641         LLDB_LOGF(log, "Could not find image_info entry for unloading image:");
642         image_infos[idx].PutToLog(log);
643       }
644     }
645   }
646   if (unloaded_module_list.GetSize() > 0) {
647     if (log) {
648       log->PutCString("Unloaded:");
649       unloaded_module_list.LogUUIDAndPaths(
650           log, "DynamicLoaderMacOSXDYLD::ModulesDidUnload");
651     }
652     m_process->GetTarget().GetImages().Remove(unloaded_module_list);
653   }
654   m_dyld_image_infos_stop_id = m_process->GetStopID();
655   return true;
656 }
657 
658 bool DynamicLoaderMacOSXDYLD::ReadImageInfos(
659     lldb::addr_t image_infos_addr, uint32_t image_infos_count,
660     ImageInfo::collection &image_infos) {
661   std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
662   const ByteOrder endian = GetByteOrderFromMagic(m_dyld.header.magic);
663   const uint32_t addr_size = m_dyld.GetAddressByteSize();
664 
665   image_infos.resize(image_infos_count);
666   const size_t count = image_infos.size() * 3 * addr_size;
667   DataBufferHeap info_data(count, 0);
668   Status error;
669   const size_t bytes_read = m_process->ReadMemory(
670       image_infos_addr, info_data.GetBytes(), info_data.GetByteSize(), error);
671   if (bytes_read == count) {
672     lldb::offset_t info_data_offset = 0;
673     DataExtractor info_data_ref(info_data.GetBytes(), info_data.GetByteSize(),
674                                 endian, addr_size);
675     for (size_t i = 0;
676          i < image_infos.size() && info_data_ref.ValidOffset(info_data_offset);
677          i++) {
678       image_infos[i].address = info_data_ref.GetAddress(&info_data_offset);
679       lldb::addr_t path_addr = info_data_ref.GetAddress(&info_data_offset);
680       image_infos[i].mod_date = info_data_ref.GetAddress(&info_data_offset);
681 
682       char raw_path[PATH_MAX];
683       m_process->ReadCStringFromMemory(path_addr, raw_path, sizeof(raw_path),
684                                        error);
685       // don't resolve the path
686       if (error.Success()) {
687         image_infos[i].file_spec.SetFile(raw_path, FileSpec::Style::native);
688       }
689     }
690     return true;
691   } else {
692     return false;
693   }
694 }
695 
696 // If we have found where the "_dyld_all_image_infos" lives in memory, read the
697 // current info from it, and then update all image load addresses (or lack
698 // thereof).  Only do this if this is the first time we're reading the dyld
699 // infos.  Return true if we actually read anything, and false otherwise.
700 bool DynamicLoaderMacOSXDYLD::InitializeFromAllImageInfos() {
701   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
702 
703   std::lock_guard<std::recursive_mutex> guard(m_mutex);
704   std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
705   if (m_process->GetStopID() == m_dyld_image_infos_stop_id ||
706       m_dyld_image_infos.size() != 0)
707     return false;
708 
709   if (ReadAllImageInfosStructure()) {
710     // Nothing to load or unload?
711     if (m_dyld_all_image_infos.dylib_info_count == 0)
712       return true;
713 
714     if (m_dyld_all_image_infos.dylib_info_addr == 0) {
715       // DYLD is updating the images now.  So we should say we have no images,
716       // and then we'll
717       // figure it out when we hit the added breakpoint.
718       return false;
719     } else {
720       if (!AddModulesUsingImageInfosAddress(
721               m_dyld_all_image_infos.dylib_info_addr,
722               m_dyld_all_image_infos.dylib_info_count)) {
723         DEBUG_PRINTF("%s", "unable to read all data for all_dylib_infos.");
724         m_dyld_image_infos.clear();
725       }
726     }
727 
728     // Now we have one more bit of business.  If there is a library left in the
729     // images for our target that doesn't have a load address, then it must be
730     // something that we were expecting to load (for instance we read a load
731     // command for it) but it didn't in fact load - probably because
732     // DYLD_*_PATH pointed to an equivalent version.  We don't want it to stay
733     // in the target's module list or it will confuse us, so unload it here.
734     Target &target = m_process->GetTarget();
735     ModuleList not_loaded_modules;
736     for (ModuleSP module_sp : target.GetImages().Modules()) {
737       if (!module_sp->IsLoadedInTarget(&target)) {
738         if (log) {
739           StreamString s;
740           module_sp->GetDescription(s.AsRawOstream());
741           LLDB_LOGF(log, "Unloading pre-run module: %s.", s.GetData());
742         }
743         not_loaded_modules.Append(module_sp);
744       }
745     }
746 
747     if (not_loaded_modules.GetSize() != 0) {
748       target.GetImages().Remove(not_loaded_modules);
749     }
750 
751     return true;
752   } else
753     return false;
754 }
755 
756 // Read a mach_header at ADDR into HEADER, and also fill in the load command
757 // data into LOAD_COMMAND_DATA if it is non-NULL.
758 //
759 // Returns true if we succeed, false if we fail for any reason.
760 bool DynamicLoaderMacOSXDYLD::ReadMachHeader(lldb::addr_t addr,
761                                              llvm::MachO::mach_header *header,
762                                              DataExtractor *load_command_data) {
763   DataBufferHeap header_bytes(sizeof(llvm::MachO::mach_header), 0);
764   Status error;
765   size_t bytes_read = m_process->ReadMemory(addr, header_bytes.GetBytes(),
766                                             header_bytes.GetByteSize(), error);
767   if (bytes_read == sizeof(llvm::MachO::mach_header)) {
768     lldb::offset_t offset = 0;
769     ::memset(header, 0, sizeof(llvm::MachO::mach_header));
770 
771     // Get the magic byte unswapped so we can figure out what we are dealing
772     // with
773     DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(),
774                        endian::InlHostByteOrder(), 4);
775     header->magic = data.GetU32(&offset);
776     lldb::addr_t load_cmd_addr = addr;
777     data.SetByteOrder(
778         DynamicLoaderMacOSXDYLD::GetByteOrderFromMagic(header->magic));
779     switch (header->magic) {
780     case llvm::MachO::MH_MAGIC:
781     case llvm::MachO::MH_CIGAM:
782       data.SetAddressByteSize(4);
783       load_cmd_addr += sizeof(llvm::MachO::mach_header);
784       break;
785 
786     case llvm::MachO::MH_MAGIC_64:
787     case llvm::MachO::MH_CIGAM_64:
788       data.SetAddressByteSize(8);
789       load_cmd_addr += sizeof(llvm::MachO::mach_header_64);
790       break;
791 
792     default:
793       return false;
794     }
795 
796     // Read the rest of dyld's mach header
797     if (data.GetU32(&offset, &header->cputype,
798                     (sizeof(llvm::MachO::mach_header) / sizeof(uint32_t)) -
799                         1)) {
800       if (load_command_data == nullptr)
801         return true; // We were able to read the mach_header and weren't asked
802                      // to read the load command bytes
803 
804       DataBufferSP load_cmd_data_sp(new DataBufferHeap(header->sizeofcmds, 0));
805 
806       size_t load_cmd_bytes_read =
807           m_process->ReadMemory(load_cmd_addr, load_cmd_data_sp->GetBytes(),
808                                 load_cmd_data_sp->GetByteSize(), error);
809 
810       if (load_cmd_bytes_read == header->sizeofcmds) {
811         // Set the load command data and also set the correct endian swap
812         // settings and the correct address size
813         load_command_data->SetData(load_cmd_data_sp, 0, header->sizeofcmds);
814         load_command_data->SetByteOrder(data.GetByteOrder());
815         load_command_data->SetAddressByteSize(data.GetAddressByteSize());
816         return true; // We successfully read the mach_header and the load
817                      // command data
818       }
819 
820       return false; // We weren't able to read the load command data
821     }
822   }
823   return false; // We failed the read the mach_header
824 }
825 
826 // Parse the load commands for an image
827 uint32_t DynamicLoaderMacOSXDYLD::ParseLoadCommands(const DataExtractor &data,
828                                                     ImageInfo &dylib_info,
829                                                     FileSpec *lc_id_dylinker) {
830   lldb::offset_t offset = 0;
831   uint32_t cmd_idx;
832   Segment segment;
833   dylib_info.Clear(true);
834 
835   for (cmd_idx = 0; cmd_idx < dylib_info.header.ncmds; cmd_idx++) {
836     // Clear out any load command specific data from DYLIB_INFO since we are
837     // about to read it.
838 
839     if (data.ValidOffsetForDataOfSize(offset,
840                                       sizeof(llvm::MachO::load_command))) {
841       llvm::MachO::load_command load_cmd;
842       lldb::offset_t load_cmd_offset = offset;
843       load_cmd.cmd = data.GetU32(&offset);
844       load_cmd.cmdsize = data.GetU32(&offset);
845       switch (load_cmd.cmd) {
846       case llvm::MachO::LC_SEGMENT: {
847         segment.name.SetTrimmedCStringWithLength(
848             (const char *)data.GetData(&offset, 16), 16);
849         // We are putting 4 uint32_t values 4 uint64_t values so we have to use
850         // multiple 32 bit gets below.
851         segment.vmaddr = data.GetU32(&offset);
852         segment.vmsize = data.GetU32(&offset);
853         segment.fileoff = data.GetU32(&offset);
854         segment.filesize = data.GetU32(&offset);
855         // Extract maxprot, initprot, nsects and flags all at once
856         data.GetU32(&offset, &segment.maxprot, 4);
857         dylib_info.segments.push_back(segment);
858       } break;
859 
860       case llvm::MachO::LC_SEGMENT_64: {
861         segment.name.SetTrimmedCStringWithLength(
862             (const char *)data.GetData(&offset, 16), 16);
863         // Extract vmaddr, vmsize, fileoff, and filesize all at once
864         data.GetU64(&offset, &segment.vmaddr, 4);
865         // Extract maxprot, initprot, nsects and flags all at once
866         data.GetU32(&offset, &segment.maxprot, 4);
867         dylib_info.segments.push_back(segment);
868       } break;
869 
870       case llvm::MachO::LC_ID_DYLINKER:
871         if (lc_id_dylinker) {
872           const lldb::offset_t name_offset =
873               load_cmd_offset + data.GetU32(&offset);
874           const char *path = data.PeekCStr(name_offset);
875           lc_id_dylinker->SetFile(path, FileSpec::Style::native);
876           FileSystem::Instance().Resolve(*lc_id_dylinker);
877         }
878         break;
879 
880       case llvm::MachO::LC_UUID:
881         dylib_info.uuid = UUID::fromOptionalData(data.GetData(&offset, 16), 16);
882         break;
883 
884       default:
885         break;
886       }
887       // Set offset to be the beginning of the next load command.
888       offset = load_cmd_offset + load_cmd.cmdsize;
889     }
890   }
891 
892   // All sections listed in the dyld image info structure will all either be
893   // fixed up already, or they will all be off by a single slide amount that is
894   // determined by finding the first segment that is at file offset zero which
895   // also has bytes (a file size that is greater than zero) in the object file.
896 
897   // Determine the slide amount (if any)
898   const size_t num_sections = dylib_info.segments.size();
899   for (size_t i = 0; i < num_sections; ++i) {
900     // Iterate through the object file sections to find the first section that
901     // starts of file offset zero and that has bytes in the file...
902     if ((dylib_info.segments[i].fileoff == 0 &&
903          dylib_info.segments[i].filesize > 0) ||
904         (dylib_info.segments[i].name == "__TEXT")) {
905       dylib_info.slide = dylib_info.address - dylib_info.segments[i].vmaddr;
906       // We have found the slide amount, so we can exit this for loop.
907       break;
908     }
909   }
910   return cmd_idx;
911 }
912 
913 // Read the mach_header and load commands for each image that the
914 // _dyld_all_image_infos structure points to and cache the results.
915 
916 void DynamicLoaderMacOSXDYLD::UpdateImageInfosHeaderAndLoadCommands(
917     ImageInfo::collection &image_infos, uint32_t infos_count,
918     bool update_executable) {
919   uint32_t exe_idx = UINT32_MAX;
920   // Read any UUID values that we can get
921   for (uint32_t i = 0; i < infos_count; i++) {
922     if (!image_infos[i].UUIDValid()) {
923       DataExtractor data; // Load command data
924       if (!ReadMachHeader(image_infos[i].address, &image_infos[i].header,
925                           &data))
926         continue;
927 
928       ParseLoadCommands(data, image_infos[i], nullptr);
929 
930       if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE)
931         exe_idx = i;
932     }
933   }
934 
935   Target &target = m_process->GetTarget();
936 
937   if (exe_idx < image_infos.size()) {
938     const bool can_create = true;
939     ModuleSP exe_module_sp(FindTargetModuleForImageInfo(image_infos[exe_idx],
940                                                         can_create, nullptr));
941 
942     if (exe_module_sp) {
943       UpdateImageLoadAddress(exe_module_sp.get(), image_infos[exe_idx]);
944 
945       if (exe_module_sp.get() != target.GetExecutableModulePointer()) {
946         // Don't load dependent images since we are in dyld where we will know
947         // and find out about all images that are loaded. Also when setting the
948         // executable module, it will clear the targets module list, and if we
949         // have an in memory dyld module, it will get removed from the list so
950         // we will need to add it back after setting the executable module, so
951         // we first try and see if we already have a weak pointer to the dyld
952         // module, make it into a shared pointer, then add the executable, then
953         // re-add it back to make sure it is always in the list.
954         ModuleSP dyld_module_sp(GetDYLDModule());
955 
956         m_process->GetTarget().SetExecutableModule(exe_module_sp,
957                                                    eLoadDependentsNo);
958 
959         if (dyld_module_sp) {
960           if (target.GetImages().AppendIfNeeded(dyld_module_sp)) {
961             std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
962 
963             // Also add it to the section list.
964             UpdateImageLoadAddress(dyld_module_sp.get(), m_dyld);
965           }
966         }
967       }
968     }
969   }
970 }
971 
972 // Dump the _dyld_all_image_infos members and all current image infos that we
973 // have parsed to the file handle provided.
974 void DynamicLoaderMacOSXDYLD::PutToLog(Log *log) const {
975   if (log == nullptr)
976     return;
977 
978   std::lock_guard<std::recursive_mutex> guard(m_mutex);
979   std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
980   LLDB_LOGF(log,
981             "dyld_all_image_infos = { version=%d, count=%d, addr=0x%8.8" PRIx64
982             ", notify=0x%8.8" PRIx64 " }",
983             m_dyld_all_image_infos.version,
984             m_dyld_all_image_infos.dylib_info_count,
985             (uint64_t)m_dyld_all_image_infos.dylib_info_addr,
986             (uint64_t)m_dyld_all_image_infos.notification);
987   size_t i;
988   const size_t count = m_dyld_image_infos.size();
989   if (count > 0) {
990     log->PutCString("Loaded:");
991     for (i = 0; i < count; i++)
992       m_dyld_image_infos[i].PutToLog(log);
993   }
994 }
995 
996 bool DynamicLoaderMacOSXDYLD::SetNotificationBreakpoint() {
997   DEBUG_PRINTF("DynamicLoaderMacOSXDYLD::%s() process state = %s\n",
998                __FUNCTION__, StateAsCString(m_process->GetState()));
999   if (m_break_id == LLDB_INVALID_BREAK_ID) {
1000     if (m_dyld_all_image_infos.notification != LLDB_INVALID_ADDRESS) {
1001       Address so_addr;
1002       // Set the notification breakpoint and install a breakpoint callback
1003       // function that will get called each time the breakpoint gets hit. We
1004       // will use this to track when shared libraries get loaded/unloaded.
1005       bool resolved = m_process->GetTarget().ResolveLoadAddress(
1006           m_dyld_all_image_infos.notification, so_addr);
1007       if (!resolved) {
1008         ModuleSP dyld_module_sp = GetDYLDModule();
1009         if (dyld_module_sp) {
1010           std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
1011 
1012           UpdateImageLoadAddress(dyld_module_sp.get(), m_dyld);
1013           resolved = m_process->GetTarget().ResolveLoadAddress(
1014               m_dyld_all_image_infos.notification, so_addr);
1015         }
1016       }
1017 
1018       if (resolved) {
1019         Breakpoint *dyld_break =
1020             m_process->GetTarget().CreateBreakpoint(so_addr, true, false).get();
1021         dyld_break->SetCallback(DynamicLoaderMacOSXDYLD::NotifyBreakpointHit,
1022                                 this, true);
1023         dyld_break->SetBreakpointKind("shared-library-event");
1024         m_break_id = dyld_break->GetID();
1025       }
1026     }
1027   }
1028   return m_break_id != LLDB_INVALID_BREAK_ID;
1029 }
1030 
1031 Status DynamicLoaderMacOSXDYLD::CanLoadImage() {
1032   Status error;
1033   // In order for us to tell if we can load a shared library we verify that the
1034   // dylib_info_addr isn't zero (which means no shared libraries have been set
1035   // yet, or dyld is currently mucking with the shared library list).
1036   if (ReadAllImageInfosStructure()) {
1037     // TODO: also check the _dyld_global_lock_held variable in
1038     // libSystem.B.dylib?
1039     // TODO: check the malloc lock?
1040     // TODO: check the objective C lock?
1041     if (m_dyld_all_image_infos.dylib_info_addr != 0)
1042       return error; // Success
1043   }
1044 
1045   error.SetErrorString("unsafe to load or unload shared libraries");
1046   return error;
1047 }
1048 
1049 bool DynamicLoaderMacOSXDYLD::GetSharedCacheInformation(
1050     lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache,
1051     LazyBool &private_shared_cache) {
1052   base_address = LLDB_INVALID_ADDRESS;
1053   uuid.Clear();
1054   using_shared_cache = eLazyBoolCalculate;
1055   private_shared_cache = eLazyBoolCalculate;
1056 
1057   if (m_process) {
1058     addr_t all_image_infos = m_process->GetImageInfoAddress();
1059 
1060     // The address returned by GetImageInfoAddress may be the address of dyld
1061     // (don't want) or it may be the address of the dyld_all_image_infos
1062     // structure (want). The first four bytes will be either the version field
1063     // (all_image_infos) or a Mach-O file magic constant. Version 13 and higher
1064     // of dyld_all_image_infos is required to get the sharedCacheUUID field.
1065 
1066     Status err;
1067     uint32_t version_or_magic =
1068         m_process->ReadUnsignedIntegerFromMemory(all_image_infos, 4, -1, err);
1069     if (version_or_magic != static_cast<uint32_t>(-1) &&
1070         version_or_magic != llvm::MachO::MH_MAGIC &&
1071         version_or_magic != llvm::MachO::MH_CIGAM &&
1072         version_or_magic != llvm::MachO::MH_MAGIC_64 &&
1073         version_or_magic != llvm::MachO::MH_CIGAM_64 &&
1074         version_or_magic >= 13) {
1075       addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
1076       int wordsize = m_process->GetAddressByteSize();
1077       if (wordsize == 8) {
1078         sharedCacheUUID_address =
1079             all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
1080       }
1081       if (wordsize == 4) {
1082         sharedCacheUUID_address =
1083             all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
1084       }
1085       if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS) {
1086         uuid_t shared_cache_uuid;
1087         if (m_process->ReadMemory(sharedCacheUUID_address, shared_cache_uuid,
1088                                   sizeof(uuid_t), err) == sizeof(uuid_t)) {
1089           uuid = UUID::fromOptionalData(shared_cache_uuid, 16);
1090           if (uuid.IsValid()) {
1091             using_shared_cache = eLazyBoolYes;
1092           }
1093         }
1094 
1095         if (version_or_magic >= 15) {
1096           // The sharedCacheBaseAddress field is the next one in the
1097           // dyld_all_image_infos struct.
1098           addr_t sharedCacheBaseAddr_address = sharedCacheUUID_address + 16;
1099           Status error;
1100           base_address = m_process->ReadUnsignedIntegerFromMemory(
1101               sharedCacheBaseAddr_address, wordsize, LLDB_INVALID_ADDRESS,
1102               error);
1103           if (error.Fail())
1104             base_address = LLDB_INVALID_ADDRESS;
1105         }
1106 
1107         return true;
1108       }
1109 
1110       //
1111       // add
1112       // NB: sharedCacheBaseAddress is the next field in dyld_all_image_infos
1113       // after
1114       // sharedCacheUUID -- that is, 16 bytes after it, if we wanted to fetch
1115       // it.
1116     }
1117   }
1118   return false;
1119 }
1120 
1121 bool DynamicLoaderMacOSXDYLD::IsFullyInitialized() {
1122   if (ReadAllImageInfosStructure())
1123     return m_dyld_all_image_infos.libSystemInitialized;
1124   return false;
1125 }
1126 
1127 void DynamicLoaderMacOSXDYLD::Initialize() {
1128   PluginManager::RegisterPlugin(GetPluginNameStatic(),
1129                                 GetPluginDescriptionStatic(), CreateInstance);
1130   DynamicLoaderMacOS::Initialize();
1131 }
1132 
1133 void DynamicLoaderMacOSXDYLD::Terminate() {
1134   DynamicLoaderMacOS::Terminate();
1135   PluginManager::UnregisterPlugin(CreateInstance);
1136 }
1137 
1138 llvm::StringRef DynamicLoaderMacOSXDYLD::GetPluginDescriptionStatic() {
1139   return "Dynamic loader plug-in that watches for shared library loads/unloads "
1140          "in MacOSX user processes.";
1141 }
1142 
1143 uint32_t DynamicLoaderMacOSXDYLD::AddrByteSize() {
1144   std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
1145 
1146   switch (m_dyld.header.magic) {
1147   case llvm::MachO::MH_MAGIC:
1148   case llvm::MachO::MH_CIGAM:
1149     return 4;
1150 
1151   case llvm::MachO::MH_MAGIC_64:
1152   case llvm::MachO::MH_CIGAM_64:
1153     return 8;
1154 
1155   default:
1156     break;
1157   }
1158   return 0;
1159 }
1160 
1161 lldb::ByteOrder DynamicLoaderMacOSXDYLD::GetByteOrderFromMagic(uint32_t magic) {
1162   switch (magic) {
1163   case llvm::MachO::MH_MAGIC:
1164   case llvm::MachO::MH_MAGIC_64:
1165     return endian::InlHostByteOrder();
1166 
1167   case llvm::MachO::MH_CIGAM:
1168   case llvm::MachO::MH_CIGAM_64:
1169     if (endian::InlHostByteOrder() == lldb::eByteOrderBig)
1170       return lldb::eByteOrderLittle;
1171     else
1172       return lldb::eByteOrderBig;
1173 
1174   default:
1175     break;
1176   }
1177   return lldb::eByteOrderInvalid;
1178 }
1179