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 = nullptr;
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() == nullptr)
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 == "__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(FindTargetModuleForImageInfo(image_infos[exe_idx],
537                                                         can_create, nullptr));
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 = FindTargetModuleForImageInfo(image_infos[dyld_idx],
553                                                     can_create, nullptr);
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, nullptr);
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, nullptr));
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() == nullptr) {
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() == nullptr && sym_ctx.function) {
690     module_sp =
691         sym_ctx.function->GetAddressRange().GetBaseAddress().GetModule();
692   }
693   if (module_sp.get() == nullptr)
694     return false;
695 
696   ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
697   return objc_runtime != nullptr &&
698          objc_runtime->IsModuleObjCLibrary(module_sp);
699 }
700 
701 // Dump a Segment to the file handle provided.
702 void DynamicLoaderDarwin::Segment::PutToLog(Log *log,
703                                             lldb::addr_t slide) const {
704   if (log) {
705     if (slide == 0)
706       log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")",
707                   name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize);
708     else
709       log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64
710                   ") slide = 0x%" PRIx64,
711                   name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize,
712                   slide);
713   }
714 }
715 
716 const DynamicLoaderDarwin::Segment *
717 DynamicLoaderDarwin::ImageInfo::FindSegment(ConstString name) const {
718   const size_t num_segments = segments.size();
719   for (size_t i = 0; i < num_segments; ++i) {
720     if (segments[i].name == name)
721       return &segments[i];
722   }
723   return nullptr;
724 }
725 
726 // Dump an image info structure to the file handle provided.
727 void DynamicLoaderDarwin::ImageInfo::PutToLog(Log *log) const {
728   if (!log)
729     return;
730   if (address == LLDB_INVALID_ADDRESS) {
731     LLDB_LOG(log, "modtime={0:x+8} uuid={1} path='{2}' (UNLOADED)", mod_date,
732              uuid.GetAsString(), file_spec.GetPath());
733   } else {
734     LLDB_LOG(log, "address={0:x+16} modtime={1:x+8} uuid={2} path='{3}'",
735              address, mod_date, uuid.GetAsString(), file_spec.GetPath());
736     for (uint32_t i = 0; i < segments.size(); ++i)
737       segments[i].PutToLog(log, slide);
738   }
739 }
740 
741 void DynamicLoaderDarwin::PrivateInitialize(Process *process) {
742   DEBUG_PRINTF("DynamicLoaderDarwin::%s() process state = %s\n", __FUNCTION__,
743                StateAsCString(m_process->GetState()));
744   Clear(true);
745   m_process = process;
746   m_process->GetTarget().ClearAllLoadedSections();
747 }
748 
749 // Member function that gets called when the process state changes.
750 void DynamicLoaderDarwin::PrivateProcessStateChanged(Process *process,
751                                                      StateType state) {
752   DEBUG_PRINTF("DynamicLoaderDarwin::%s(%s)\n", __FUNCTION__,
753                StateAsCString(state));
754   switch (state) {
755   case eStateConnected:
756   case eStateAttaching:
757   case eStateLaunching:
758   case eStateInvalid:
759   case eStateUnloaded:
760   case eStateExited:
761   case eStateDetached:
762     Clear(false);
763     break;
764 
765   case eStateStopped:
766     // Keep trying find dyld and set our notification breakpoint each time we
767     // stop until we succeed
768     if (!DidSetNotificationBreakpoint() && m_process->IsAlive()) {
769       if (NeedToDoInitialImageFetch())
770         DoInitialImageFetch();
771 
772       SetNotificationBreakpoint();
773     }
774     break;
775 
776   case eStateRunning:
777   case eStateStepping:
778   case eStateCrashed:
779   case eStateSuspended:
780     break;
781   }
782 }
783 
784 ThreadPlanSP
785 DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread,
786                                                   bool stop_others) {
787   ThreadPlanSP thread_plan_sp;
788   StackFrame *current_frame = thread.GetStackFrameAtIndex(0).get();
789   const SymbolContext &current_context =
790       current_frame->GetSymbolContext(eSymbolContextSymbol);
791   Symbol *current_symbol = current_context.symbol;
792   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
793   TargetSP target_sp(thread.CalculateTarget());
794 
795   if (current_symbol != nullptr) {
796     std::vector<Address> addresses;
797 
798     if (current_symbol->IsTrampoline()) {
799       ConstString trampoline_name = current_symbol->GetMangled().GetName(
800           current_symbol->GetLanguage(), Mangled::ePreferMangled);
801 
802       if (trampoline_name) {
803         const ModuleList &images = target_sp->GetImages();
804 
805         SymbolContextList code_symbols;
806         images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeCode,
807                                           code_symbols);
808         size_t num_code_symbols = code_symbols.GetSize();
809 
810         if (num_code_symbols > 0) {
811           for (uint32_t i = 0; i < num_code_symbols; i++) {
812             SymbolContext context;
813             AddressRange addr_range;
814             if (code_symbols.GetContextAtIndex(i, context)) {
815               context.GetAddressRange(eSymbolContextEverything, 0, false,
816                                       addr_range);
817               addresses.push_back(addr_range.GetBaseAddress());
818               if (log) {
819                 addr_t load_addr =
820                     addr_range.GetBaseAddress().GetLoadAddress(target_sp.get());
821 
822                 log->Printf("Found a trampoline target symbol at 0x%" PRIx64
823                             ".",
824                             load_addr);
825               }
826             }
827           }
828         }
829 
830         SymbolContextList reexported_symbols;
831         images.FindSymbolsWithNameAndType(
832             trampoline_name, eSymbolTypeReExported, reexported_symbols);
833         size_t num_reexported_symbols = reexported_symbols.GetSize();
834         if (num_reexported_symbols > 0) {
835           for (uint32_t i = 0; i < num_reexported_symbols; i++) {
836             SymbolContext context;
837             if (reexported_symbols.GetContextAtIndex(i, context)) {
838               if (context.symbol) {
839                 Symbol *actual_symbol =
840                     context.symbol->ResolveReExportedSymbol(*target_sp.get());
841                 if (actual_symbol) {
842                   const Address actual_symbol_addr =
843                       actual_symbol->GetAddress();
844                   if (actual_symbol_addr.IsValid()) {
845                     addresses.push_back(actual_symbol_addr);
846                     if (log) {
847                       lldb::addr_t load_addr =
848                           actual_symbol_addr.GetLoadAddress(target_sp.get());
849                       log->Printf(
850                           "Found a re-exported symbol: %s at 0x%" PRIx64 ".",
851                           actual_symbol->GetName().GetCString(), load_addr);
852                     }
853                   }
854                 }
855               }
856             }
857           }
858         }
859 
860         SymbolContextList indirect_symbols;
861         images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeResolver,
862                                           indirect_symbols);
863         size_t num_indirect_symbols = indirect_symbols.GetSize();
864         if (num_indirect_symbols > 0) {
865           for (uint32_t i = 0; i < num_indirect_symbols; i++) {
866             SymbolContext context;
867             AddressRange addr_range;
868             if (indirect_symbols.GetContextAtIndex(i, context)) {
869               context.GetAddressRange(eSymbolContextEverything, 0, false,
870                                       addr_range);
871               addresses.push_back(addr_range.GetBaseAddress());
872               if (log) {
873                 addr_t load_addr =
874                     addr_range.GetBaseAddress().GetLoadAddress(target_sp.get());
875 
876                 log->Printf("Found an indirect target symbol at 0x%" PRIx64 ".",
877                             load_addr);
878               }
879             }
880           }
881         }
882       }
883     } else if (current_symbol->GetType() == eSymbolTypeReExported) {
884       // I am not sure we could ever end up stopped AT a re-exported symbol.
885       // But just in case:
886 
887       const Symbol *actual_symbol =
888           current_symbol->ResolveReExportedSymbol(*(target_sp.get()));
889       if (actual_symbol) {
890         Address target_addr(actual_symbol->GetAddress());
891         if (target_addr.IsValid()) {
892           if (log)
893             log->Printf(
894                 "Found a re-exported symbol: %s pointing to: %s at 0x%" PRIx64
895                 ".",
896                 current_symbol->GetName().GetCString(),
897                 actual_symbol->GetName().GetCString(),
898                 target_addr.GetLoadAddress(target_sp.get()));
899           addresses.push_back(target_addr.GetLoadAddress(target_sp.get()));
900         }
901       }
902     }
903 
904     if (addresses.size() > 0) {
905       // First check whether any of the addresses point to Indirect symbols,
906       // and if they do, resolve them:
907       std::vector<lldb::addr_t> load_addrs;
908       for (Address address : addresses) {
909         Symbol *symbol = address.CalculateSymbolContextSymbol();
910         if (symbol && symbol->IsIndirect()) {
911           Status error;
912           Address symbol_address = symbol->GetAddress();
913           addr_t resolved_addr = thread.GetProcess()->ResolveIndirectFunction(
914               &symbol_address, error);
915           if (error.Success()) {
916             load_addrs.push_back(resolved_addr);
917             if (log)
918               log->Printf("ResolveIndirectFunction found resolved target for "
919                           "%s at 0x%" PRIx64 ".",
920                           symbol->GetName().GetCString(), resolved_addr);
921           }
922         } else {
923           load_addrs.push_back(address.GetLoadAddress(target_sp.get()));
924         }
925       }
926       thread_plan_sp = std::make_shared<ThreadPlanRunToAddress>(
927           thread, load_addrs, stop_others);
928     }
929   } else {
930     if (log)
931       log->Printf("Could not find symbol for step through.");
932   }
933 
934   return thread_plan_sp;
935 }
936 
937 size_t DynamicLoaderDarwin::FindEquivalentSymbols(
938     lldb_private::Symbol *original_symbol, lldb_private::ModuleList &images,
939     lldb_private::SymbolContextList &equivalent_symbols) {
940   ConstString trampoline_name = original_symbol->GetMangled().GetName(
941       original_symbol->GetLanguage(), Mangled::ePreferMangled);
942   if (!trampoline_name)
943     return 0;
944 
945   size_t initial_size = equivalent_symbols.GetSize();
946 
947   static const char *resolver_name_regex = "(_gc|_non_gc|\\$[A-Za-z0-9\\$]+)$";
948   std::string equivalent_regex_buf("^");
949   equivalent_regex_buf.append(trampoline_name.GetCString());
950   equivalent_regex_buf.append(resolver_name_regex);
951 
952   RegularExpression equivalent_name_regex(equivalent_regex_buf);
953   const bool append = true;
954   images.FindSymbolsMatchingRegExAndType(equivalent_name_regex, eSymbolTypeCode,
955                                          equivalent_symbols, append);
956 
957   return equivalent_symbols.GetSize() - initial_size;
958 }
959 
960 lldb::ModuleSP DynamicLoaderDarwin::GetPThreadLibraryModule() {
961   ModuleSP module_sp = m_libpthread_module_wp.lock();
962   if (!module_sp) {
963     SymbolContextList sc_list;
964     ModuleSpec module_spec;
965     module_spec.GetFileSpec().GetFilename().SetCString(
966         "libsystem_pthread.dylib");
967     ModuleList module_list;
968     if (m_process->GetTarget().GetImages().FindModules(module_spec,
969                                                        module_list)) {
970       if (module_list.GetSize() == 1) {
971         module_sp = module_list.GetModuleAtIndex(0);
972         if (module_sp)
973           m_libpthread_module_wp = module_sp;
974       }
975     }
976   }
977   return module_sp;
978 }
979 
980 Address DynamicLoaderDarwin::GetPthreadSetSpecificAddress() {
981   if (!m_pthread_getspecific_addr.IsValid()) {
982     ModuleSP module_sp = GetPThreadLibraryModule();
983     if (module_sp) {
984       lldb_private::SymbolContextList sc_list;
985       module_sp->FindSymbolsWithNameAndType(ConstString("pthread_getspecific"),
986                                             eSymbolTypeCode, sc_list);
987       SymbolContext sc;
988       if (sc_list.GetContextAtIndex(0, sc)) {
989         if (sc.symbol)
990           m_pthread_getspecific_addr = sc.symbol->GetAddress();
991       }
992     }
993   }
994   return m_pthread_getspecific_addr;
995 }
996 
997 lldb::addr_t
998 DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp,
999                                         const lldb::ThreadSP thread_sp,
1000                                         lldb::addr_t tls_file_addr) {
1001   if (!thread_sp || !module_sp)
1002     return LLDB_INVALID_ADDRESS;
1003 
1004   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1005 
1006   const uint32_t addr_size = m_process->GetAddressByteSize();
1007   uint8_t buf[sizeof(lldb::addr_t) * 3];
1008 
1009   lldb_private::Address tls_addr;
1010   if (module_sp->ResolveFileAddress(tls_file_addr, tls_addr)) {
1011     Status error;
1012     const size_t tsl_data_size = addr_size * 3;
1013     Target &target = m_process->GetTarget();
1014     if (target.ReadMemory(tls_addr, false, buf, tsl_data_size, error) ==
1015         tsl_data_size) {
1016       const ByteOrder byte_order = m_process->GetByteOrder();
1017       DataExtractor data(buf, sizeof(buf), byte_order, addr_size);
1018       lldb::offset_t offset = addr_size; // Skip the first pointer
1019       const lldb::addr_t pthread_key = data.GetAddress(&offset);
1020       const lldb::addr_t tls_offset = data.GetAddress(&offset);
1021       if (pthread_key != 0) {
1022         // First check to see if we have already figured out the location of
1023         // TLS data for the pthread_key on a specific thread yet. If we have we
1024         // can re-use it since its location will not change unless the process
1025         // execs.
1026         const tid_t tid = thread_sp->GetID();
1027         auto tid_pos = m_tid_to_tls_map.find(tid);
1028         if (tid_pos != m_tid_to_tls_map.end()) {
1029           auto tls_pos = tid_pos->second.find(pthread_key);
1030           if (tls_pos != tid_pos->second.end()) {
1031             return tls_pos->second + tls_offset;
1032           }
1033         }
1034         StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(0);
1035         if (frame_sp) {
1036           ClangASTContext *clang_ast_context =
1037               target.GetScratchClangASTContext();
1038 
1039           if (!clang_ast_context)
1040             return LLDB_INVALID_ADDRESS;
1041 
1042           CompilerType clang_void_ptr_type =
1043               clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
1044           Address pthread_getspecific_addr = GetPthreadSetSpecificAddress();
1045           if (pthread_getspecific_addr.IsValid()) {
1046             EvaluateExpressionOptions options;
1047 
1048             lldb::ThreadPlanSP thread_plan_sp(new ThreadPlanCallFunction(
1049                 *thread_sp, pthread_getspecific_addr, clang_void_ptr_type,
1050                 llvm::ArrayRef<lldb::addr_t>(pthread_key), options));
1051 
1052             DiagnosticManager execution_errors;
1053             ExecutionContext exe_ctx(thread_sp);
1054             lldb::ExpressionResults results = m_process->RunThreadPlan(
1055                 exe_ctx, thread_plan_sp, options, execution_errors);
1056 
1057             if (results == lldb::eExpressionCompleted) {
1058               lldb::ValueObjectSP result_valobj_sp =
1059                   thread_plan_sp->GetReturnValueObject();
1060               if (result_valobj_sp) {
1061                 const lldb::addr_t pthread_key_data =
1062                     result_valobj_sp->GetValueAsUnsigned(0);
1063                 if (pthread_key_data) {
1064                   m_tid_to_tls_map[tid].insert(
1065                       std::make_pair(pthread_key, pthread_key_data));
1066                   return pthread_key_data + tls_offset;
1067                 }
1068               }
1069             }
1070           }
1071         }
1072       }
1073     }
1074   }
1075   return LLDB_INVALID_ADDRESS;
1076 }
1077 
1078 bool DynamicLoaderDarwin::UseDYLDSPI(Process *process) {
1079   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
1080   bool use_new_spi_interface = false;
1081 
1082   llvm::VersionTuple version = process->GetHostOSVersion();
1083   if (!version.empty()) {
1084     const llvm::Triple::OSType os_type =
1085         process->GetTarget().GetArchitecture().GetTriple().getOS();
1086 
1087     // macOS 10.12 and newer
1088     if (os_type == llvm::Triple::MacOSX &&
1089         version >= llvm::VersionTuple(10, 12))
1090       use_new_spi_interface = true;
1091 
1092     // iOS 10 and newer
1093     if (os_type == llvm::Triple::IOS && version >= llvm::VersionTuple(10))
1094       use_new_spi_interface = true;
1095 
1096     // tvOS 10 and newer
1097     if (os_type == llvm::Triple::TvOS && version >= llvm::VersionTuple(10))
1098       use_new_spi_interface = true;
1099 
1100     // watchOS 3 and newer
1101     if (os_type == llvm::Triple::WatchOS && version >= llvm::VersionTuple(3))
1102       use_new_spi_interface = true;
1103 
1104     // NEED_BRIDGEOS_TRIPLE // Any BridgeOS
1105     // NEED_BRIDGEOS_TRIPLE if (os_type == llvm::Triple::BridgeOS)
1106     // NEED_BRIDGEOS_TRIPLE   use_new_spi_interface = true;
1107   }
1108 
1109   if (log) {
1110     if (use_new_spi_interface)
1111       log->Printf(
1112           "DynamicLoaderDarwin::UseDYLDSPI: Use new DynamicLoader plugin");
1113     else
1114       log->Printf(
1115           "DynamicLoaderDarwin::UseDYLDSPI: Use old DynamicLoader plugin");
1116   }
1117   return use_new_spi_interface;
1118 }
1119