1 //===-- DynamicLoaderDarwin.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 "DynamicLoaderDarwin.h"
10 
11 #include "lldb/Breakpoint/StoppointCallbackContext.h"
12 #include "lldb/Core/Debugger.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/PluginManager.h"
16 #include "lldb/Core/Section.h"
17 #include "lldb/Expression/DiagnosticManager.h"
18 #include "lldb/Host/FileSystem.h"
19 #include "lldb/Symbol/ClangASTContext.h"
20 #include "lldb/Symbol/Function.h"
21 #include "lldb/Symbol/ObjectFile.h"
22 #include "lldb/Target/ABI.h"
23 #include "lldb/Target/ObjCLanguageRuntime.h"
24 #include "lldb/Target/RegisterContext.h"
25 #include "lldb/Target/StackFrame.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Target/Thread.h"
28 #include "lldb/Target/ThreadPlanCallFunction.h"
29 #include "lldb/Target/ThreadPlanRunToAddress.h"
30 #include "lldb/Utility/DataBuffer.h"
31 #include "lldb/Utility/DataBufferHeap.h"
32 #include "lldb/Utility/Log.h"
33 #include "lldb/Utility/State.h"
34 
35 //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
36 #ifdef ENABLE_DEBUG_PRINTF
37 #include <stdio.h>
38 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
39 #else
40 #define DEBUG_PRINTF(fmt, ...)
41 #endif
42 
43 #ifndef __APPLE__
44 #include "Utility/UuidCompatibility.h"
45 #else
46 #include <uuid/uuid.h>
47 #endif
48 
49 #include <memory>
50 
51 using namespace lldb;
52 using namespace lldb_private;
53 
54 // Constructor
55 DynamicLoaderDarwin::DynamicLoaderDarwin(Process *process)
56     : DynamicLoader(process), m_dyld_module_wp(), m_libpthread_module_wp(),
57       m_pthread_getspecific_addr(), m_tid_to_tls_map(), m_dyld_image_infos(),
58       m_dyld_image_infos_stop_id(UINT32_MAX), m_dyld(), m_mutex() {}
59 
60 // Destructor
61 DynamicLoaderDarwin::~DynamicLoaderDarwin() {}
62 
63 /// Called after attaching a process.
64 ///
65 /// Allow DynamicLoader plug-ins to execute some code after
66 /// attaching to a process.
67 void DynamicLoaderDarwin::DidAttach() {
68   PrivateInitialize(m_process);
69   DoInitialImageFetch();
70   SetNotificationBreakpoint();
71 }
72 
73 /// Called after attaching a process.
74 ///
75 /// Allow DynamicLoader plug-ins to execute some code after
76 /// attaching to a process.
77 void DynamicLoaderDarwin::DidLaunch() {
78   PrivateInitialize(m_process);
79   DoInitialImageFetch();
80   SetNotificationBreakpoint();
81 }
82 
83 // Clear out the state of this class.
84 void DynamicLoaderDarwin::Clear(bool clear_process) {
85   std::lock_guard<std::recursive_mutex> guard(m_mutex);
86   if (clear_process)
87     m_process = NULL;
88   m_dyld_image_infos.clear();
89   m_dyld_image_infos_stop_id = UINT32_MAX;
90   m_dyld.Clear(false);
91 }
92 
93 ModuleSP DynamicLoaderDarwin::FindTargetModuleForImageInfo(
94     ImageInfo &image_info, bool can_create, bool *did_create_ptr) {
95   if (did_create_ptr)
96     *did_create_ptr = false;
97 
98   Target &target = m_process->GetTarget();
99   const ModuleList &target_images = target.GetImages();
100   ModuleSpec module_spec(image_info.file_spec);
101   module_spec.GetUUID() = image_info.uuid;
102   ModuleSP module_sp(target_images.FindFirstModule(module_spec));
103 
104   if (module_sp && !module_spec.GetUUID().IsValid() &&
105       !module_sp->GetUUID().IsValid()) {
106     // No UUID, we must rely upon the cached module modification time and the
107     // modification time of the file on disk
108     if (module_sp->GetModificationTime() !=
109         FileSystem::Instance().GetModificationTime(module_sp->GetFileSpec()))
110       module_sp.reset();
111   }
112 
113   if (!module_sp) {
114     if (can_create) {
115       // We'll call Target::ModulesDidLoad after all the modules have been
116       // added to the target, don't let it be called for every one.
117       module_sp = target.GetOrCreateModule(module_spec, false /* notify */);
118       if (!module_sp || module_sp->GetObjectFile() == NULL)
119         module_sp = m_process->ReadModuleFromMemory(image_info.file_spec,
120                                                     image_info.address);
121 
122       if (did_create_ptr)
123         *did_create_ptr = (bool)module_sp;
124     }
125   }
126   return module_sp;
127 }
128 
129 void DynamicLoaderDarwin::UnloadImages(
130     const std::vector<lldb::addr_t> &solib_addresses) {
131   std::lock_guard<std::recursive_mutex> guard(m_mutex);
132   if (m_process->GetStopID() == m_dyld_image_infos_stop_id)
133     return;
134 
135   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
136   Target &target = m_process->GetTarget();
137   if (log)
138     log->Printf("Removing %" PRId64 " modules.",
139                 (uint64_t)solib_addresses.size());
140 
141   ModuleList unloaded_module_list;
142 
143   for (addr_t solib_addr : solib_addresses) {
144     Address header;
145     if (header.SetLoadAddress(solib_addr, &target)) {
146       if (header.GetOffset() == 0) {
147         ModuleSP module_to_remove(header.GetModule());
148         if (module_to_remove.get()) {
149           if (log)
150             log->Printf("Removing module at address 0x%" PRIx64, solib_addr);
151           // remove the sections from the Target
152           UnloadSections(module_to_remove);
153           // add this to the list of modules to remove
154           unloaded_module_list.AppendIfNeeded(module_to_remove);
155           // remove the entry from the m_dyld_image_infos
156           ImageInfo::collection::iterator pos, end = m_dyld_image_infos.end();
157           for (pos = m_dyld_image_infos.begin(); pos != end; pos++) {
158             if (solib_addr == (*pos).address) {
159               m_dyld_image_infos.erase(pos);
160               break;
161             }
162           }
163         }
164       }
165     }
166   }
167 
168   if (unloaded_module_list.GetSize() > 0) {
169     if (log) {
170       log->PutCString("Unloaded:");
171       unloaded_module_list.LogUUIDAndPaths(
172           log, "DynamicLoaderDarwin::UnloadModules");
173     }
174     m_process->GetTarget().GetImages().Remove(unloaded_module_list);
175     m_dyld_image_infos_stop_id = m_process->GetStopID();
176   }
177 }
178 
179 void DynamicLoaderDarwin::UnloadAllImages() {
180   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
181   ModuleList unloaded_modules_list;
182 
183   Target &target = m_process->GetTarget();
184   const ModuleList &target_modules = target.GetImages();
185   std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
186 
187   size_t num_modules = target_modules.GetSize();
188   ModuleSP dyld_sp(GetDYLDModule());
189 
190   for (size_t i = 0; i < num_modules; i++) {
191     ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i);
192 
193     // Don't remove dyld - else we'll lose our breakpoint notifying us about
194     // libraries being re-loaded...
195     if (module_sp.get() != nullptr && module_sp.get() != dyld_sp.get()) {
196       UnloadSections(module_sp);
197       unloaded_modules_list.Append(module_sp);
198     }
199   }
200 
201   if (unloaded_modules_list.GetSize() != 0) {
202     if (log) {
203       log->PutCString("Unloaded:");
204       unloaded_modules_list.LogUUIDAndPaths(
205           log, "DynamicLoaderDarwin::UnloadAllImages");
206     }
207     target.GetImages().Remove(unloaded_modules_list);
208     m_dyld_image_infos.clear();
209     m_dyld_image_infos_stop_id = m_process->GetStopID();
210   }
211 }
212 
213 // Update the load addresses for all segments in MODULE using the updated INFO
214 // that is passed in.
215 bool DynamicLoaderDarwin::UpdateImageLoadAddress(Module *module,
216                                                  ImageInfo &info) {
217   bool changed = false;
218   if (module) {
219     ObjectFile *image_object_file = module->GetObjectFile();
220     if (image_object_file) {
221       SectionList *section_list = image_object_file->GetSectionList();
222       if (section_list) {
223         std::vector<uint32_t> inaccessible_segment_indexes;
224         // We now know the slide amount, so go through all sections and update
225         // the load addresses with the correct values.
226         const size_t num_segments = info.segments.size();
227         for (size_t i = 0; i < num_segments; ++i) {
228           // Only load a segment if it has protections. Things like __PAGEZERO
229           // don't have any protections, and they shouldn't be slid
230           SectionSP section_sp(
231               section_list->FindSectionByName(info.segments[i].name));
232 
233           if (info.segments[i].maxprot == 0) {
234             inaccessible_segment_indexes.push_back(i);
235           } else {
236             const addr_t new_section_load_addr =
237                 info.segments[i].vmaddr + info.slide;
238             static ConstString g_section_name_LINKEDIT("__LINKEDIT");
239 
240             if (section_sp) {
241               // __LINKEDIT sections from files in the shared cache can overlap
242               // so check to see what the segment name is and pass "false" so
243               // we don't warn of overlapping "Section" objects, and "true" for
244               // all other sections.
245               const bool warn_multiple =
246                   section_sp->GetName() != g_section_name_LINKEDIT;
247 
248               changed = m_process->GetTarget().SetSectionLoadAddress(
249                   section_sp, new_section_load_addr, warn_multiple);
250             }
251           }
252         }
253 
254         // If the loaded the file (it changed) and we have segments that are
255         // not readable or writeable, add them to the invalid memory region
256         // cache for the process. This will typically only be the __PAGEZERO
257         // segment in the main executable. We might be able to apply this more
258         // generally to more sections that have no protections in the future,
259         // but for now we are going to just do __PAGEZERO.
260         if (changed && !inaccessible_segment_indexes.empty()) {
261           for (uint32_t i = 0; i < inaccessible_segment_indexes.size(); ++i) {
262             const uint32_t seg_idx = inaccessible_segment_indexes[i];
263             SectionSP section_sp(
264                 section_list->FindSectionByName(info.segments[seg_idx].name));
265 
266             if (section_sp) {
267               static ConstString g_pagezero_section_name("__PAGEZERO");
268               if (g_pagezero_section_name == section_sp->GetName()) {
269                 // __PAGEZERO never slides...
270                 const lldb::addr_t vmaddr = info.segments[seg_idx].vmaddr;
271                 const lldb::addr_t vmsize = info.segments[seg_idx].vmsize;
272                 Process::LoadRange pagezero_range(vmaddr, vmsize);
273                 m_process->AddInvalidMemoryRegion(pagezero_range);
274               }
275             }
276           }
277         }
278       }
279     }
280   }
281   // We might have an in memory image that was loaded as soon as it was created
282   if (info.load_stop_id == m_process->GetStopID())
283     changed = true;
284   else if (changed) {
285     // Update the stop ID when this library was updated
286     info.load_stop_id = m_process->GetStopID();
287   }
288   return changed;
289 }
290 
291 // Unload the segments in MODULE using the INFO that is passed in.
292 bool DynamicLoaderDarwin::UnloadModuleSections(Module *module,
293                                                ImageInfo &info) {
294   bool changed = false;
295   if (module) {
296     ObjectFile *image_object_file = module->GetObjectFile();
297     if (image_object_file) {
298       SectionList *section_list = image_object_file->GetSectionList();
299       if (section_list) {
300         const size_t num_segments = info.segments.size();
301         for (size_t i = 0; i < num_segments; ++i) {
302           SectionSP section_sp(
303               section_list->FindSectionByName(info.segments[i].name));
304           if (section_sp) {
305             const addr_t old_section_load_addr =
306                 info.segments[i].vmaddr + info.slide;
307             if (m_process->GetTarget().SetSectionUnloaded(
308                     section_sp, old_section_load_addr))
309               changed = true;
310           } else {
311             Host::SystemLog(Host::eSystemLogWarning,
312                             "warning: unable to find and unload segment named "
313                             "'%s' in '%s' in macosx dynamic loader plug-in.\n",
314                             info.segments[i].name.AsCString("<invalid>"),
315                             image_object_file->GetFileSpec().GetPath().c_str());
316           }
317         }
318       }
319     }
320   }
321   return changed;
322 }
323 
324 // Given a JSON dictionary (from debugserver, most likely) of binary images
325 // loaded in the inferior process, add the images to the ImageInfo collection.
326 
327 bool DynamicLoaderDarwin::JSONImageInformationIntoImageInfo(
328     StructuredData::ObjectSP image_details,
329     ImageInfo::collection &image_infos) {
330   StructuredData::ObjectSP images_sp =
331       image_details->GetAsDictionary()->GetValueForKey("images");
332   if (images_sp.get() == nullptr)
333     return false;
334 
335   image_infos.resize(images_sp->GetAsArray()->GetSize());
336 
337   for (size_t i = 0; i < image_infos.size(); i++) {
338     StructuredData::ObjectSP image_sp =
339         images_sp->GetAsArray()->GetItemAtIndex(i);
340     if (image_sp.get() == nullptr || image_sp->GetAsDictionary() == nullptr)
341       return false;
342     StructuredData::Dictionary *image = image_sp->GetAsDictionary();
343     // clang-format off
344     if (!image->HasKey("load_address") ||
345         !image->HasKey("pathname") ||
346         !image->HasKey("mod_date") ||
347         !image->HasKey("mach_header") ||
348         image->GetValueForKey("mach_header")->GetAsDictionary() == nullptr ||
349         !image->HasKey("segments") ||
350         image->GetValueForKey("segments")->GetAsArray() == nullptr ||
351         !image->HasKey("uuid")) {
352       return false;
353     }
354     // clang-format on
355     image_infos[i].address =
356         image->GetValueForKey("load_address")->GetAsInteger()->GetValue();
357     image_infos[i].mod_date =
358         image->GetValueForKey("mod_date")->GetAsInteger()->GetValue();
359     image_infos[i].file_spec.SetFile(
360         image->GetValueForKey("pathname")->GetAsString()->GetValue(),
361         FileSpec::Style::native);
362 
363     StructuredData::Dictionary *mh =
364         image->GetValueForKey("mach_header")->GetAsDictionary();
365     image_infos[i].header.magic =
366         mh->GetValueForKey("magic")->GetAsInteger()->GetValue();
367     image_infos[i].header.cputype =
368         mh->GetValueForKey("cputype")->GetAsInteger()->GetValue();
369     image_infos[i].header.cpusubtype =
370         mh->GetValueForKey("cpusubtype")->GetAsInteger()->GetValue();
371     image_infos[i].header.filetype =
372         mh->GetValueForKey("filetype")->GetAsInteger()->GetValue();
373 
374     if (image->HasKey("min_version_os_name")) {
375       std::string os_name = image->GetValueForKey("min_version_os_name")
376                                 ->GetAsString()
377                                 ->GetValue();
378       if (os_name == "macosx")
379         image_infos[i].os_type = llvm::Triple::MacOSX;
380       else if (os_name == "ios" || os_name == "iphoneos")
381         image_infos[i].os_type = llvm::Triple::IOS;
382       else if (os_name == "tvos")
383         image_infos[i].os_type = llvm::Triple::TvOS;
384       else if (os_name == "watchos")
385         image_infos[i].os_type = llvm::Triple::WatchOS;
386       // NEED_BRIDGEOS_TRIPLE else if (os_name == "bridgeos")
387       // NEED_BRIDGEOS_TRIPLE   image_infos[i].os_type = llvm::Triple::BridgeOS;
388     }
389     if (image->HasKey("min_version_os_sdk")) {
390       image_infos[i].min_version_os_sdk =
391           image->GetValueForKey("min_version_os_sdk")
392               ->GetAsString()
393               ->GetValue();
394     }
395 
396     // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't
397     // currently send them in the reply.
398 
399     if (mh->HasKey("flags"))
400       image_infos[i].header.flags =
401           mh->GetValueForKey("flags")->GetAsInteger()->GetValue();
402     else
403       image_infos[i].header.flags = 0;
404 
405     if (mh->HasKey("ncmds"))
406       image_infos[i].header.ncmds =
407           mh->GetValueForKey("ncmds")->GetAsInteger()->GetValue();
408     else
409       image_infos[i].header.ncmds = 0;
410 
411     if (mh->HasKey("sizeofcmds"))
412       image_infos[i].header.sizeofcmds =
413           mh->GetValueForKey("sizeofcmds")->GetAsInteger()->GetValue();
414     else
415       image_infos[i].header.sizeofcmds = 0;
416 
417     StructuredData::Array *segments =
418         image->GetValueForKey("segments")->GetAsArray();
419     uint32_t segcount = segments->GetSize();
420     for (size_t j = 0; j < segcount; j++) {
421       Segment segment;
422       StructuredData::Dictionary *seg =
423           segments->GetItemAtIndex(j)->GetAsDictionary();
424       segment.name =
425           ConstString(seg->GetValueForKey("name")->GetAsString()->GetValue());
426       segment.vmaddr =
427           seg->GetValueForKey("vmaddr")->GetAsInteger()->GetValue();
428       segment.vmsize =
429           seg->GetValueForKey("vmsize")->GetAsInteger()->GetValue();
430       segment.fileoff =
431           seg->GetValueForKey("fileoff")->GetAsInteger()->GetValue();
432       segment.filesize =
433           seg->GetValueForKey("filesize")->GetAsInteger()->GetValue();
434       segment.maxprot =
435           seg->GetValueForKey("maxprot")->GetAsInteger()->GetValue();
436 
437       // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't
438       // currently send them in the reply.
439 
440       if (seg->HasKey("initprot"))
441         segment.initprot =
442             seg->GetValueForKey("initprot")->GetAsInteger()->GetValue();
443       else
444         segment.initprot = 0;
445 
446       if (seg->HasKey("flags"))
447         segment.flags =
448             seg->GetValueForKey("flags")->GetAsInteger()->GetValue();
449       else
450         segment.flags = 0;
451 
452       if (seg->HasKey("nsects"))
453         segment.nsects =
454             seg->GetValueForKey("nsects")->GetAsInteger()->GetValue();
455       else
456         segment.nsects = 0;
457 
458       image_infos[i].segments.push_back(segment);
459     }
460 
461     image_infos[i].uuid.SetFromOptionalStringRef(
462         image->GetValueForKey("uuid")->GetAsString()->GetValue());
463 
464     // All sections listed in the dyld image info structure will all either be
465     // fixed up already, or they will all be off by a single slide amount that
466     // is determined by finding the first segment that is at file offset zero
467     // which also has bytes (a file size that is greater than zero) in the
468     // object file.
469 
470     // Determine the slide amount (if any)
471     const size_t num_sections = image_infos[i].segments.size();
472     for (size_t k = 0; k < num_sections; ++k) {
473       // Iterate through the object file sections to find the first section
474       // that starts of file offset zero and that has bytes in the file...
475       if ((image_infos[i].segments[k].fileoff == 0 &&
476            image_infos[i].segments[k].filesize > 0) ||
477           (image_infos[i].segments[k].name == ConstString("__TEXT"))) {
478         image_infos[i].slide =
479             image_infos[i].address - image_infos[i].segments[k].vmaddr;
480         // We have found the slide amount, so we can exit this for loop.
481         break;
482       }
483     }
484   }
485 
486   return true;
487 }
488 
489 void DynamicLoaderDarwin::UpdateSpecialBinariesFromNewImageInfos(
490     ImageInfo::collection &image_infos) {
491   uint32_t exe_idx = UINT32_MAX;
492   uint32_t dyld_idx = UINT32_MAX;
493   Target &target = m_process->GetTarget();
494   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
495   ConstString g_dyld_sim_filename("dyld_sim");
496 
497   ArchSpec target_arch = target.GetArchitecture();
498   const size_t image_infos_size = image_infos.size();
499   for (size_t i = 0; i < image_infos_size; i++) {
500     if (image_infos[i].header.filetype == llvm::MachO::MH_DYLINKER) {
501       // In a "simulator" process (an x86 process that is
502       // ios/tvos/watchos/bridgeos) we will have two dyld modules --
503       // a "dyld" that we want to keep track of, and a "dyld_sim" which
504       // we don't need to keep track of here. If the target is an x86
505       // system and the OS of the dyld binary is ios/tvos/watchos/bridgeos,
506       // then we are looking at dyld_sym.
507 
508       // debugserver has only recently (late 2016) started sending up the os
509       // type for each binary it sees -- so if we don't have an os type, use a
510       // filename check as our next best guess.
511       if (image_infos[i].os_type == llvm::Triple::OSType::UnknownOS) {
512         if (image_infos[i].file_spec.GetFilename() != g_dyld_sim_filename) {
513           dyld_idx = i;
514         }
515       } else if (target_arch.GetTriple().getArch() == llvm::Triple::x86 ||
516                  target_arch.GetTriple().getArch() == llvm::Triple::x86_64) {
517         if (image_infos[i].os_type != llvm::Triple::OSType::IOS &&
518             image_infos[i].os_type != llvm::Triple::TvOS &&
519             image_infos[i].os_type != llvm::Triple::WatchOS) {
520             // NEED_BRIDGEOS_TRIPLE image_infos[i].os_type != llvm::Triple::BridgeOS) {
521           dyld_idx = i;
522         }
523       }
524       else {
525         // catch-all for any other environment -- trust that dyld is actually
526         // dyld
527         dyld_idx = i;
528       }
529     } else if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE) {
530       exe_idx = i;
531     }
532   }
533 
534   if (exe_idx != UINT32_MAX) {
535     const bool can_create = true;
536     ModuleSP exe_module_sp(
537         FindTargetModuleForImageInfo(image_infos[exe_idx], can_create, NULL));
538     if (exe_module_sp) {
539       if (log)
540         log->Printf("Found executable module: %s",
541                     exe_module_sp->GetFileSpec().GetPath().c_str());
542       target.GetImages().AppendIfNeeded(exe_module_sp);
543       UpdateImageLoadAddress(exe_module_sp.get(), image_infos[exe_idx]);
544       if (exe_module_sp.get() != target.GetExecutableModulePointer()) {
545         target.SetExecutableModule(exe_module_sp, eLoadDependentsNo);
546       }
547     }
548   }
549 
550   if (dyld_idx != UINT32_MAX) {
551     const bool can_create = true;
552     ModuleSP dyld_sp =
553         FindTargetModuleForImageInfo(image_infos[dyld_idx], can_create, NULL);
554     if (dyld_sp.get()) {
555       if (log)
556         log->Printf("Found dyld module: %s",
557                     dyld_sp->GetFileSpec().GetPath().c_str());
558       target.GetImages().AppendIfNeeded(dyld_sp);
559       UpdateImageLoadAddress(dyld_sp.get(), image_infos[dyld_idx]);
560       SetDYLDModule(dyld_sp);
561     }
562   }
563 }
564 
565 void DynamicLoaderDarwin::UpdateDYLDImageInfoFromNewImageInfo(
566     ImageInfo &image_info) {
567   if (image_info.header.filetype == llvm::MachO::MH_DYLINKER) {
568     const bool can_create = true;
569     ModuleSP dyld_sp =
570         FindTargetModuleForImageInfo(image_info, can_create, NULL);
571     if (dyld_sp.get()) {
572       Target &target = m_process->GetTarget();
573       target.GetImages().AppendIfNeeded(dyld_sp);
574       UpdateImageLoadAddress(dyld_sp.get(), image_info);
575       SetDYLDModule(dyld_sp);
576     }
577   }
578 }
579 
580 void DynamicLoaderDarwin::SetDYLDModule(lldb::ModuleSP &dyld_module_sp) {
581   m_dyld_module_wp = dyld_module_sp;
582 }
583 
584 ModuleSP DynamicLoaderDarwin::GetDYLDModule() {
585   ModuleSP dyld_sp(m_dyld_module_wp.lock());
586   return dyld_sp;
587 }
588 
589 bool DynamicLoaderDarwin::AddModulesUsingImageInfos(
590     ImageInfo::collection &image_infos) {
591   std::lock_guard<std::recursive_mutex> guard(m_mutex);
592   // Now add these images to the main list.
593   ModuleList loaded_module_list;
594   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
595   Target &target = m_process->GetTarget();
596   ModuleList &target_images = target.GetImages();
597 
598   for (uint32_t idx = 0; idx < image_infos.size(); ++idx) {
599     if (log) {
600       log->Printf("Adding new image at address=0x%16.16" PRIx64 ".",
601                   image_infos[idx].address);
602       image_infos[idx].PutToLog(log);
603     }
604 
605     m_dyld_image_infos.push_back(image_infos[idx]);
606 
607     ModuleSP image_module_sp(
608         FindTargetModuleForImageInfo(image_infos[idx], true, NULL));
609 
610     if (image_module_sp) {
611       ObjectFile *objfile = image_module_sp->GetObjectFile();
612       if (objfile) {
613         SectionList *sections = objfile->GetSectionList();
614         if (sections) {
615           ConstString commpage_dbstr("__commpage");
616           Section *commpage_section =
617               sections->FindSectionByName(commpage_dbstr).get();
618           if (commpage_section) {
619             ModuleSpec module_spec(objfile->GetFileSpec(),
620                                    image_infos[idx].GetArchitecture());
621             module_spec.GetObjectName() = commpage_dbstr;
622             ModuleSP commpage_image_module_sp(
623                 target_images.FindFirstModule(module_spec));
624             if (!commpage_image_module_sp) {
625               module_spec.SetObjectOffset(objfile->GetFileOffset() +
626                                           commpage_section->GetFileOffset());
627               module_spec.SetObjectSize(objfile->GetByteSize());
628               commpage_image_module_sp = target.GetOrCreateModule(module_spec,
629                                                                true /* notify */);
630               if (!commpage_image_module_sp ||
631                   commpage_image_module_sp->GetObjectFile() == NULL) {
632                 commpage_image_module_sp = m_process->ReadModuleFromMemory(
633                     image_infos[idx].file_spec, image_infos[idx].address);
634                 // Always load a memory image right away in the target in case
635                 // we end up trying to read the symbol table from memory... The
636                 // __LINKEDIT will need to be mapped so we can figure out where
637                 // the symbol table bits are...
638                 bool changed = false;
639                 UpdateImageLoadAddress(commpage_image_module_sp.get(),
640                                        image_infos[idx]);
641                 target.GetImages().Append(commpage_image_module_sp);
642                 if (changed) {
643                   image_infos[idx].load_stop_id = m_process->GetStopID();
644                   loaded_module_list.AppendIfNeeded(commpage_image_module_sp);
645                 }
646               }
647             }
648           }
649         }
650       }
651 
652       // UpdateImageLoadAddress will return true if any segments change load
653       // address. We need to check this so we don't mention that all loaded
654       // shared libraries are newly loaded each time we hit out dyld breakpoint
655       // since dyld will list all shared libraries each time.
656       if (UpdateImageLoadAddress(image_module_sp.get(), image_infos[idx])) {
657         target_images.AppendIfNeeded(image_module_sp);
658         loaded_module_list.AppendIfNeeded(image_module_sp);
659       }
660     }
661   }
662 
663   if (loaded_module_list.GetSize() > 0) {
664     if (log)
665       loaded_module_list.LogUUIDAndPaths(log,
666                                          "DynamicLoaderDarwin::ModulesDidLoad");
667     m_process->GetTarget().ModulesDidLoad(loaded_module_list);
668   }
669   return true;
670 }
671 
672 // On Mac OS X libobjc (the Objective-C runtime) has several critical dispatch
673 // functions written in hand-written assembly, and also have hand-written
674 // unwind information in the eh_frame section.  Normally we prefer analyzing
675 // the assembly instructions of a currently executing frame to unwind from that
676 // frame -- but on hand-written functions this profiling can fail.  We should
677 // use the eh_frame instructions for these functions all the time.
678 //
679 // As an aside, it would be better if the eh_frame entries had a flag (or were
680 // extensible so they could have an Apple-specific flag) which indicates that
681 // the instructions are asynchronous -- accurate at every instruction, instead
682 // of our normal default assumption that they are not.
683 
684 bool DynamicLoaderDarwin::AlwaysRelyOnEHUnwindInfo(SymbolContext &sym_ctx) {
685   ModuleSP module_sp;
686   if (sym_ctx.symbol) {
687     module_sp = sym_ctx.symbol->GetAddressRef().GetModule();
688   }
689   if (module_sp.get() == NULL && sym_ctx.function) {
690     module_sp =
691         sym_ctx.function->GetAddressRange().GetBaseAddress().GetModule();
692   }
693   if (module_sp.get() == NULL)
694     return false;
695 
696   ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
697   return objc_runtime != NULL && objc_runtime->IsModuleObjCLibrary(module_sp);
698 }
699 
700 // Dump a Segment to the file handle provided.
701 void DynamicLoaderDarwin::Segment::PutToLog(Log *log,
702                                             lldb::addr_t slide) const {
703   if (log) {
704     if (slide == 0)
705       log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")",
706                   name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize);
707     else
708       log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64
709                   ") slide = 0x%" PRIx64,
710                   name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize,
711                   slide);
712   }
713 }
714 
715 const DynamicLoaderDarwin::Segment *
716 DynamicLoaderDarwin::ImageInfo::FindSegment(ConstString name) const {
717   const size_t num_segments = segments.size();
718   for (size_t i = 0; i < num_segments; ++i) {
719     if (segments[i].name == name)
720       return &segments[i];
721   }
722   return NULL;
723 }
724 
725 // Dump an image info structure to the file handle provided.
726 void DynamicLoaderDarwin::ImageInfo::PutToLog(Log *log) const {
727   if (!log)
728     return;
729   if (address == LLDB_INVALID_ADDRESS) {
730     LLDB_LOG(log, "modtime={0:x+8} uuid={1} path='{2}' (UNLOADED)", mod_date,
731              uuid.GetAsString(), file_spec.GetPath());
732   } else {
733     LLDB_LOG(log, "address={0:x+16} modtime={1:x+8} uuid={2} path='{3}'",
734              address, mod_date, uuid.GetAsString(), file_spec.GetPath());
735     for (uint32_t i = 0; i < segments.size(); ++i)
736       segments[i].PutToLog(log, slide);
737   }
738 }
739 
740 void DynamicLoaderDarwin::PrivateInitialize(Process *process) {
741   DEBUG_PRINTF("DynamicLoaderDarwin::%s() process state = %s\n", __FUNCTION__,
742                StateAsCString(m_process->GetState()));
743   Clear(true);
744   m_process = process;
745   m_process->GetTarget().ClearAllLoadedSections();
746 }
747 
748 // Member function that gets called when the process state changes.
749 void DynamicLoaderDarwin::PrivateProcessStateChanged(Process *process,
750                                                      StateType state) {
751   DEBUG_PRINTF("DynamicLoaderDarwin::%s(%s)\n", __FUNCTION__,
752                StateAsCString(state));
753   switch (state) {
754   case eStateConnected:
755   case eStateAttaching:
756   case eStateLaunching:
757   case eStateInvalid:
758   case eStateUnloaded:
759   case eStateExited:
760   case eStateDetached:
761     Clear(false);
762     break;
763 
764   case eStateStopped:
765     // Keep trying find dyld and set our notification breakpoint each time we
766     // stop until we succeed
767     if (!DidSetNotificationBreakpoint() && m_process->IsAlive()) {
768       if (NeedToDoInitialImageFetch())
769         DoInitialImageFetch();
770 
771       SetNotificationBreakpoint();
772     }
773     break;
774 
775   case eStateRunning:
776   case eStateStepping:
777   case eStateCrashed:
778   case eStateSuspended:
779     break;
780   }
781 }
782 
783 ThreadPlanSP
784 DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread,
785                                                   bool stop_others) {
786   ThreadPlanSP thread_plan_sp;
787   StackFrame *current_frame = thread.GetStackFrameAtIndex(0).get();
788   const SymbolContext &current_context =
789       current_frame->GetSymbolContext(eSymbolContextSymbol);
790   Symbol *current_symbol = current_context.symbol;
791   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
792   TargetSP target_sp(thread.CalculateTarget());
793 
794   if (current_symbol != NULL) {
795     std::vector<Address> addresses;
796 
797     if (current_symbol->IsTrampoline()) {
798       ConstString trampoline_name = current_symbol->GetMangled().GetName(
799           current_symbol->GetLanguage(), Mangled::ePreferMangled);
800 
801       if (trampoline_name) {
802         const ModuleList &images = target_sp->GetImages();
803 
804         SymbolContextList code_symbols;
805         images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeCode,
806                                           code_symbols);
807         size_t num_code_symbols = code_symbols.GetSize();
808 
809         if (num_code_symbols > 0) {
810           for (uint32_t i = 0; i < num_code_symbols; i++) {
811             SymbolContext context;
812             AddressRange addr_range;
813             if (code_symbols.GetContextAtIndex(i, context)) {
814               context.GetAddressRange(eSymbolContextEverything, 0, false,
815                                       addr_range);
816               addresses.push_back(addr_range.GetBaseAddress());
817               if (log) {
818                 addr_t load_addr =
819                     addr_range.GetBaseAddress().GetLoadAddress(target_sp.get());
820 
821                 log->Printf("Found a trampoline target symbol at 0x%" PRIx64
822                             ".",
823                             load_addr);
824               }
825             }
826           }
827         }
828 
829         SymbolContextList reexported_symbols;
830         images.FindSymbolsWithNameAndType(
831             trampoline_name, eSymbolTypeReExported, reexported_symbols);
832         size_t num_reexported_symbols = reexported_symbols.GetSize();
833         if (num_reexported_symbols > 0) {
834           for (uint32_t i = 0; i < num_reexported_symbols; i++) {
835             SymbolContext context;
836             if (reexported_symbols.GetContextAtIndex(i, context)) {
837               if (context.symbol) {
838                 Symbol *actual_symbol =
839                     context.symbol->ResolveReExportedSymbol(*target_sp.get());
840                 if (actual_symbol) {
841                   const Address actual_symbol_addr =
842                       actual_symbol->GetAddress();
843                   if (actual_symbol_addr.IsValid()) {
844                     addresses.push_back(actual_symbol_addr);
845                     if (log) {
846                       lldb::addr_t load_addr =
847                           actual_symbol_addr.GetLoadAddress(target_sp.get());
848                       log->Printf(
849                           "Found a re-exported symbol: %s at 0x%" PRIx64 ".",
850                           actual_symbol->GetName().GetCString(), load_addr);
851                     }
852                   }
853                 }
854               }
855             }
856           }
857         }
858 
859         SymbolContextList indirect_symbols;
860         images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeResolver,
861                                           indirect_symbols);
862         size_t num_indirect_symbols = indirect_symbols.GetSize();
863         if (num_indirect_symbols > 0) {
864           for (uint32_t i = 0; i < num_indirect_symbols; i++) {
865             SymbolContext context;
866             AddressRange addr_range;
867             if (indirect_symbols.GetContextAtIndex(i, context)) {
868               context.GetAddressRange(eSymbolContextEverything, 0, false,
869                                       addr_range);
870               addresses.push_back(addr_range.GetBaseAddress());
871               if (log) {
872                 addr_t load_addr =
873                     addr_range.GetBaseAddress().GetLoadAddress(target_sp.get());
874 
875                 log->Printf("Found an indirect target symbol at 0x%" PRIx64 ".",
876                             load_addr);
877               }
878             }
879           }
880         }
881       }
882     } else if (current_symbol->GetType() == eSymbolTypeReExported) {
883       // I am not sure we could ever end up stopped AT a re-exported symbol.
884       // But just in case:
885 
886       const Symbol *actual_symbol =
887           current_symbol->ResolveReExportedSymbol(*(target_sp.get()));
888       if (actual_symbol) {
889         Address target_addr(actual_symbol->GetAddress());
890         if (target_addr.IsValid()) {
891           if (log)
892             log->Printf(
893                 "Found a re-exported symbol: %s pointing to: %s at 0x%" PRIx64
894                 ".",
895                 current_symbol->GetName().GetCString(),
896                 actual_symbol->GetName().GetCString(),
897                 target_addr.GetLoadAddress(target_sp.get()));
898           addresses.push_back(target_addr.GetLoadAddress(target_sp.get()));
899         }
900       }
901     }
902 
903     if (addresses.size() > 0) {
904       // First check whether any of the addresses point to Indirect symbols,
905       // and if they do, resolve them:
906       std::vector<lldb::addr_t> load_addrs;
907       for (Address address : addresses) {
908         Symbol *symbol = address.CalculateSymbolContextSymbol();
909         if (symbol && symbol->IsIndirect()) {
910           Status error;
911           Address symbol_address = symbol->GetAddress();
912           addr_t resolved_addr = thread.GetProcess()->ResolveIndirectFunction(
913               &symbol_address, error);
914           if (error.Success()) {
915             load_addrs.push_back(resolved_addr);
916             if (log)
917               log->Printf("ResolveIndirectFunction found resolved target for "
918                           "%s at 0x%" PRIx64 ".",
919                           symbol->GetName().GetCString(), resolved_addr);
920           }
921         } else {
922           load_addrs.push_back(address.GetLoadAddress(target_sp.get()));
923         }
924       }
925       thread_plan_sp = std::make_shared<ThreadPlanRunToAddress>(
926           thread, load_addrs, stop_others);
927     }
928   } else {
929     if (log)
930       log->Printf("Could not find symbol for step through.");
931   }
932 
933   return thread_plan_sp;
934 }
935 
936 size_t DynamicLoaderDarwin::FindEquivalentSymbols(
937     lldb_private::Symbol *original_symbol, lldb_private::ModuleList &images,
938     lldb_private::SymbolContextList &equivalent_symbols) {
939   ConstString trampoline_name = original_symbol->GetMangled().GetName(
940       original_symbol->GetLanguage(), Mangled::ePreferMangled);
941   if (!trampoline_name)
942     return 0;
943 
944   size_t initial_size = equivalent_symbols.GetSize();
945 
946   static const char *resolver_name_regex = "(_gc|_non_gc|\\$[A-Za-z0-9\\$]+)$";
947   std::string equivalent_regex_buf("^");
948   equivalent_regex_buf.append(trampoline_name.GetCString());
949   equivalent_regex_buf.append(resolver_name_regex);
950 
951   RegularExpression equivalent_name_regex(equivalent_regex_buf);
952   const bool append = true;
953   images.FindSymbolsMatchingRegExAndType(equivalent_name_regex, eSymbolTypeCode,
954                                          equivalent_symbols, append);
955 
956   return equivalent_symbols.GetSize() - initial_size;
957 }
958 
959 lldb::ModuleSP DynamicLoaderDarwin::GetPThreadLibraryModule() {
960   ModuleSP module_sp = m_libpthread_module_wp.lock();
961   if (!module_sp) {
962     SymbolContextList sc_list;
963     ModuleSpec module_spec;
964     module_spec.GetFileSpec().GetFilename().SetCString(
965         "libsystem_pthread.dylib");
966     ModuleList module_list;
967     if (m_process->GetTarget().GetImages().FindModules(module_spec,
968                                                        module_list)) {
969       if (module_list.GetSize() == 1) {
970         module_sp = module_list.GetModuleAtIndex(0);
971         if (module_sp)
972           m_libpthread_module_wp = module_sp;
973       }
974     }
975   }
976   return module_sp;
977 }
978 
979 Address DynamicLoaderDarwin::GetPthreadSetSpecificAddress() {
980   if (!m_pthread_getspecific_addr.IsValid()) {
981     ModuleSP module_sp = GetPThreadLibraryModule();
982     if (module_sp) {
983       lldb_private::SymbolContextList sc_list;
984       module_sp->FindSymbolsWithNameAndType(ConstString("pthread_getspecific"),
985                                             eSymbolTypeCode, sc_list);
986       SymbolContext sc;
987       if (sc_list.GetContextAtIndex(0, sc)) {
988         if (sc.symbol)
989           m_pthread_getspecific_addr = sc.symbol->GetAddress();
990       }
991     }
992   }
993   return m_pthread_getspecific_addr;
994 }
995 
996 lldb::addr_t
997 DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp,
998                                         const lldb::ThreadSP thread_sp,
999                                         lldb::addr_t tls_file_addr) {
1000   if (!thread_sp || !module_sp)
1001     return LLDB_INVALID_ADDRESS;
1002 
1003   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1004 
1005   const uint32_t addr_size = m_process->GetAddressByteSize();
1006   uint8_t buf[sizeof(lldb::addr_t) * 3];
1007 
1008   lldb_private::Address tls_addr;
1009   if (module_sp->ResolveFileAddress(tls_file_addr, tls_addr)) {
1010     Status error;
1011     const size_t tsl_data_size = addr_size * 3;
1012     Target &target = m_process->GetTarget();
1013     if (target.ReadMemory(tls_addr, false, buf, tsl_data_size, error) ==
1014         tsl_data_size) {
1015       const ByteOrder byte_order = m_process->GetByteOrder();
1016       DataExtractor data(buf, sizeof(buf), byte_order, addr_size);
1017       lldb::offset_t offset = addr_size; // Skip the first pointer
1018       const lldb::addr_t pthread_key = data.GetAddress(&offset);
1019       const lldb::addr_t tls_offset = data.GetAddress(&offset);
1020       if (pthread_key != 0) {
1021         // First check to see if we have already figured out the location of
1022         // TLS data for the pthread_key on a specific thread yet. If we have we
1023         // can re-use it since its location will not change unless the process
1024         // execs.
1025         const tid_t tid = thread_sp->GetID();
1026         auto tid_pos = m_tid_to_tls_map.find(tid);
1027         if (tid_pos != m_tid_to_tls_map.end()) {
1028           auto tls_pos = tid_pos->second.find(pthread_key);
1029           if (tls_pos != tid_pos->second.end()) {
1030             return tls_pos->second + tls_offset;
1031           }
1032         }
1033         StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(0);
1034         if (frame_sp) {
1035           ClangASTContext *clang_ast_context =
1036               target.GetScratchClangASTContext();
1037 
1038           if (!clang_ast_context)
1039             return LLDB_INVALID_ADDRESS;
1040 
1041           CompilerType clang_void_ptr_type =
1042               clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
1043           Address pthread_getspecific_addr = GetPthreadSetSpecificAddress();
1044           if (pthread_getspecific_addr.IsValid()) {
1045             EvaluateExpressionOptions options;
1046 
1047             lldb::ThreadPlanSP thread_plan_sp(new ThreadPlanCallFunction(
1048                 *thread_sp, pthread_getspecific_addr, clang_void_ptr_type,
1049                 llvm::ArrayRef<lldb::addr_t>(pthread_key), options));
1050 
1051             DiagnosticManager execution_errors;
1052             ExecutionContext exe_ctx(thread_sp);
1053             lldb::ExpressionResults results = m_process->RunThreadPlan(
1054                 exe_ctx, thread_plan_sp, options, execution_errors);
1055 
1056             if (results == lldb::eExpressionCompleted) {
1057               lldb::ValueObjectSP result_valobj_sp =
1058                   thread_plan_sp->GetReturnValueObject();
1059               if (result_valobj_sp) {
1060                 const lldb::addr_t pthread_key_data =
1061                     result_valobj_sp->GetValueAsUnsigned(0);
1062                 if (pthread_key_data) {
1063                   m_tid_to_tls_map[tid].insert(
1064                       std::make_pair(pthread_key, pthread_key_data));
1065                   return pthread_key_data + tls_offset;
1066                 }
1067               }
1068             }
1069           }
1070         }
1071       }
1072     }
1073   }
1074   return LLDB_INVALID_ADDRESS;
1075 }
1076 
1077 bool DynamicLoaderDarwin::UseDYLDSPI(Process *process) {
1078   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
1079   bool use_new_spi_interface = false;
1080 
1081   llvm::VersionTuple version = process->GetHostOSVersion();
1082   if (!version.empty()) {
1083     const llvm::Triple::OSType os_type =
1084         process->GetTarget().GetArchitecture().GetTriple().getOS();
1085 
1086     // macOS 10.12 and newer
1087     if (os_type == llvm::Triple::MacOSX &&
1088         version >= llvm::VersionTuple(10, 12))
1089       use_new_spi_interface = true;
1090 
1091     // iOS 10 and newer
1092     if (os_type == llvm::Triple::IOS && version >= llvm::VersionTuple(10))
1093       use_new_spi_interface = true;
1094 
1095     // tvOS 10 and newer
1096     if (os_type == llvm::Triple::TvOS && version >= llvm::VersionTuple(10))
1097       use_new_spi_interface = true;
1098 
1099     // watchOS 3 and newer
1100     if (os_type == llvm::Triple::WatchOS && version >= llvm::VersionTuple(3))
1101       use_new_spi_interface = true;
1102 
1103     // NEED_BRIDGEOS_TRIPLE // Any BridgeOS
1104     // NEED_BRIDGEOS_TRIPLE if (os_type == llvm::Triple::BridgeOS)
1105     // NEED_BRIDGEOS_TRIPLE   use_new_spi_interface = true;
1106   }
1107 
1108   if (log) {
1109     if (use_new_spi_interface)
1110       log->Printf(
1111           "DynamicLoaderDarwin::UseDYLDSPI: Use new DynamicLoader plugin");
1112     else
1113       log->Printf(
1114           "DynamicLoaderDarwin::UseDYLDSPI: Use old DynamicLoader plugin");
1115   }
1116   return use_new_spi_interface;
1117 }
1118