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/RegisterContext.h"
24 #include "lldb/Target/StackFrame.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Target/Thread.h"
27 #include "lldb/Target/ThreadPlanCallFunction.h"
28 #include "lldb/Target/ThreadPlanRunToAddress.h"
29 #include "lldb/Utility/DataBuffer.h"
30 #include "lldb/Utility/DataBufferHeap.h"
31 #include "lldb/Utility/Log.h"
32 #include "lldb/Utility/State.h"
33 
34 #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
35 
36 //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
37 #ifdef ENABLE_DEBUG_PRINTF
38 #include <stdio.h>
39 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
40 #else
41 #define DEBUG_PRINTF(fmt, ...)
42 #endif
43 
44 #ifndef __APPLE__
45 #include "Utility/UuidCompatibility.h"
46 #else
47 #include <uuid/uuid.h>
48 #endif
49 
50 #include <memory>
51 
52 using namespace lldb;
53 using namespace lldb_private;
54 
55 // Constructor
56 DynamicLoaderDarwin::DynamicLoaderDarwin(Process *process)
57     : DynamicLoader(process), m_dyld_module_wp(), m_libpthread_module_wp(),
58       m_pthread_getspecific_addr(), m_tid_to_tls_map(), m_dyld_image_infos(),
59       m_dyld_image_infos_stop_id(UINT32_MAX), m_dyld(), m_mutex() {}
60 
61 // Destructor
62 DynamicLoaderDarwin::~DynamicLoaderDarwin() {}
63 
64 /// Called after attaching a process.
65 ///
66 /// Allow DynamicLoader plug-ins to execute some code after
67 /// attaching to a process.
68 void DynamicLoaderDarwin::DidAttach() {
69   PrivateInitialize(m_process);
70   DoInitialImageFetch();
71   SetNotificationBreakpoint();
72 }
73 
74 /// Called after attaching a process.
75 ///
76 /// Allow DynamicLoader plug-ins to execute some code after
77 /// attaching to a process.
78 void DynamicLoaderDarwin::DidLaunch() {
79   PrivateInitialize(m_process);
80   DoInitialImageFetch();
81   SetNotificationBreakpoint();
82 }
83 
84 // Clear out the state of this class.
85 void DynamicLoaderDarwin::Clear(bool clear_process) {
86   std::lock_guard<std::recursive_mutex> guard(m_mutex);
87   if (clear_process)
88     m_process = nullptr;
89   m_dyld_image_infos.clear();
90   m_dyld_image_infos_stop_id = UINT32_MAX;
91   m_dyld.Clear(false);
92 }
93 
94 ModuleSP DynamicLoaderDarwin::FindTargetModuleForImageInfo(
95     ImageInfo &image_info, bool can_create, bool *did_create_ptr) {
96   if (did_create_ptr)
97     *did_create_ptr = false;
98 
99   Target &target = m_process->GetTarget();
100   const ModuleList &target_images = target.GetImages();
101   ModuleSpec module_spec(image_info.file_spec);
102   module_spec.GetUUID() = image_info.uuid;
103   ModuleSP module_sp(target_images.FindFirstModule(module_spec));
104 
105   if (module_sp && !module_spec.GetUUID().IsValid() &&
106       !module_sp->GetUUID().IsValid()) {
107     // No UUID, we must rely upon the cached module modification time and the
108     // modification time of the file on disk
109     if (module_sp->GetModificationTime() !=
110         FileSystem::Instance().GetModificationTime(module_sp->GetFileSpec()))
111       module_sp.reset();
112   }
113 
114   if (!module_sp) {
115     if (can_create) {
116       // We'll call Target::ModulesDidLoad after all the modules have been
117       // added to the target, don't let it be called for every one.
118       module_sp = target.GetOrCreateModule(module_spec, false /* notify */);
119       if (!module_sp || module_sp->GetObjectFile() == nullptr)
120         module_sp = m_process->ReadModuleFromMemory(image_info.file_spec,
121                                                     image_info.address);
122 
123       if (did_create_ptr)
124         *did_create_ptr = (bool)module_sp;
125     }
126   }
127   return module_sp;
128 }
129 
130 void DynamicLoaderDarwin::UnloadImages(
131     const std::vector<lldb::addr_t> &solib_addresses) {
132   std::lock_guard<std::recursive_mutex> guard(m_mutex);
133   if (m_process->GetStopID() == m_dyld_image_infos_stop_id)
134     return;
135 
136   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
137   Target &target = m_process->GetTarget();
138   LLDB_LOGF(log, "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           LLDB_LOGF(log, "Removing module at address 0x%" PRIx64, solib_addr);
150           // remove the sections from the Target
151           UnloadSections(module_to_remove);
152           // add this to the list of modules to remove
153           unloaded_module_list.AppendIfNeeded(module_to_remove);
154           // remove the entry from the m_dyld_image_infos
155           ImageInfo::collection::iterator pos, end = m_dyld_image_infos.end();
156           for (pos = m_dyld_image_infos.begin(); pos != end; pos++) {
157             if (solib_addr == (*pos).address) {
158               m_dyld_image_infos.erase(pos);
159               break;
160             }
161           }
162         }
163       }
164     }
165   }
166 
167   if (unloaded_module_list.GetSize() > 0) {
168     if (log) {
169       log->PutCString("Unloaded:");
170       unloaded_module_list.LogUUIDAndPaths(
171           log, "DynamicLoaderDarwin::UnloadModules");
172     }
173     m_process->GetTarget().GetImages().Remove(unloaded_module_list);
174     m_dyld_image_infos_stop_id = m_process->GetStopID();
175   }
176 }
177 
178 void DynamicLoaderDarwin::UnloadAllImages() {
179   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
180   ModuleList unloaded_modules_list;
181 
182   Target &target = m_process->GetTarget();
183   const ModuleList &target_modules = target.GetImages();
184   std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
185 
186   size_t num_modules = target_modules.GetSize();
187   ModuleSP dyld_sp(GetDYLDModule());
188 
189   for (size_t i = 0; i < num_modules; i++) {
190     ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i);
191 
192     // Don't remove dyld - else we'll lose our breakpoint notifying us about
193     // libraries being re-loaded...
194     if (module_sp.get() != nullptr && module_sp.get() != dyld_sp.get()) {
195       UnloadSections(module_sp);
196       unloaded_modules_list.Append(module_sp);
197     }
198   }
199 
200   if (unloaded_modules_list.GetSize() != 0) {
201     if (log) {
202       log->PutCString("Unloaded:");
203       unloaded_modules_list.LogUUIDAndPaths(
204           log, "DynamicLoaderDarwin::UnloadAllImages");
205     }
206     target.GetImages().Remove(unloaded_modules_list);
207     m_dyld_image_infos.clear();
208     m_dyld_image_infos_stop_id = m_process->GetStopID();
209   }
210 }
211 
212 // Update the load addresses for all segments in MODULE using the updated INFO
213 // that is passed in.
214 bool DynamicLoaderDarwin::UpdateImageLoadAddress(Module *module,
215                                                  ImageInfo &info) {
216   bool changed = false;
217   if (module) {
218     ObjectFile *image_object_file = module->GetObjectFile();
219     if (image_object_file) {
220       SectionList *section_list = image_object_file->GetSectionList();
221       if (section_list) {
222         std::vector<uint32_t> inaccessible_segment_indexes;
223         // We now know the slide amount, so go through all sections and update
224         // the load addresses with the correct values.
225         const size_t num_segments = info.segments.size();
226         for (size_t i = 0; i < num_segments; ++i) {
227           // Only load a segment if it has protections. Things like __PAGEZERO
228           // don't have any protections, and they shouldn't be slid
229           SectionSP section_sp(
230               section_list->FindSectionByName(info.segments[i].name));
231 
232           if (info.segments[i].maxprot == 0) {
233             inaccessible_segment_indexes.push_back(i);
234           } else {
235             const addr_t new_section_load_addr =
236                 info.segments[i].vmaddr + info.slide;
237             static ConstString g_section_name_LINKEDIT("__LINKEDIT");
238 
239             if (section_sp) {
240               // __LINKEDIT sections from files in the shared cache can overlap
241               // so check to see what the segment name is and pass "false" so
242               // we don't warn of overlapping "Section" objects, and "true" for
243               // all other sections.
244               const bool warn_multiple =
245                   section_sp->GetName() != g_section_name_LINKEDIT;
246 
247               changed = m_process->GetTarget().SetSectionLoadAddress(
248                   section_sp, new_section_load_addr, warn_multiple);
249             }
250           }
251         }
252 
253         // If the loaded the file (it changed) and we have segments that are
254         // not readable or writeable, add them to the invalid memory region
255         // cache for the process. This will typically only be the __PAGEZERO
256         // segment in the main executable. We might be able to apply this more
257         // generally to more sections that have no protections in the future,
258         // but for now we are going to just do __PAGEZERO.
259         if (changed && !inaccessible_segment_indexes.empty()) {
260           for (uint32_t i = 0; i < inaccessible_segment_indexes.size(); ++i) {
261             const uint32_t seg_idx = inaccessible_segment_indexes[i];
262             SectionSP section_sp(
263                 section_list->FindSectionByName(info.segments[seg_idx].name));
264 
265             if (section_sp) {
266               static ConstString g_pagezero_section_name("__PAGEZERO");
267               if (g_pagezero_section_name == section_sp->GetName()) {
268                 // __PAGEZERO never slides...
269                 const lldb::addr_t vmaddr = info.segments[seg_idx].vmaddr;
270                 const lldb::addr_t vmsize = info.segments[seg_idx].vmsize;
271                 Process::LoadRange pagezero_range(vmaddr, vmsize);
272                 m_process->AddInvalidMemoryRegion(pagezero_range);
273               }
274             }
275           }
276         }
277       }
278     }
279   }
280   // We might have an in memory image that was loaded as soon as it was created
281   if (info.load_stop_id == m_process->GetStopID())
282     changed = true;
283   else if (changed) {
284     // Update the stop ID when this library was updated
285     info.load_stop_id = m_process->GetStopID();
286   }
287   return changed;
288 }
289 
290 // Unload the segments in MODULE using the INFO that is passed in.
291 bool DynamicLoaderDarwin::UnloadModuleSections(Module *module,
292                                                ImageInfo &info) {
293   bool changed = false;
294   if (module) {
295     ObjectFile *image_object_file = module->GetObjectFile();
296     if (image_object_file) {
297       SectionList *section_list = image_object_file->GetSectionList();
298       if (section_list) {
299         const size_t num_segments = info.segments.size();
300         for (size_t i = 0; i < num_segments; ++i) {
301           SectionSP section_sp(
302               section_list->FindSectionByName(info.segments[i].name));
303           if (section_sp) {
304             const addr_t old_section_load_addr =
305                 info.segments[i].vmaddr + info.slide;
306             if (m_process->GetTarget().SetSectionUnloaded(
307                     section_sp, old_section_load_addr))
308               changed = true;
309           } else {
310             Host::SystemLog(Host::eSystemLogWarning,
311                             "warning: unable to find and unload segment named "
312                             "'%s' in '%s' in macosx dynamic loader plug-in.\n",
313                             info.segments[i].name.AsCString("<invalid>"),
314                             image_object_file->GetFileSpec().GetPath().c_str());
315           }
316         }
317       }
318     }
319   }
320   return changed;
321 }
322 
323 // Given a JSON dictionary (from debugserver, most likely) of binary images
324 // loaded in the inferior process, add the images to the ImageInfo collection.
325 
326 bool DynamicLoaderDarwin::JSONImageInformationIntoImageInfo(
327     StructuredData::ObjectSP image_details,
328     ImageInfo::collection &image_infos) {
329   StructuredData::ObjectSP images_sp =
330       image_details->GetAsDictionary()->GetValueForKey("images");
331   if (images_sp.get() == nullptr)
332     return false;
333 
334   image_infos.resize(images_sp->GetAsArray()->GetSize());
335 
336   for (size_t i = 0; i < image_infos.size(); i++) {
337     StructuredData::ObjectSP image_sp =
338         images_sp->GetAsArray()->GetItemAtIndex(i);
339     if (image_sp.get() == nullptr || image_sp->GetAsDictionary() == nullptr)
340       return false;
341     StructuredData::Dictionary *image = image_sp->GetAsDictionary();
342     // clang-format off
343     if (!image->HasKey("load_address") ||
344         !image->HasKey("pathname") ||
345         !image->HasKey("mod_date") ||
346         !image->HasKey("mach_header") ||
347         image->GetValueForKey("mach_header")->GetAsDictionary() == nullptr ||
348         !image->HasKey("segments") ||
349         image->GetValueForKey("segments")->GetAsArray() == nullptr ||
350         !image->HasKey("uuid")) {
351       return false;
352     }
353     // clang-format on
354     image_infos[i].address =
355         image->GetValueForKey("load_address")->GetAsInteger()->GetValue();
356     image_infos[i].mod_date =
357         image->GetValueForKey("mod_date")->GetAsInteger()->GetValue();
358     image_infos[i].file_spec.SetFile(
359         image->GetValueForKey("pathname")->GetAsString()->GetValue(),
360         FileSpec::Style::native);
361 
362     StructuredData::Dictionary *mh =
363         image->GetValueForKey("mach_header")->GetAsDictionary();
364     image_infos[i].header.magic =
365         mh->GetValueForKey("magic")->GetAsInteger()->GetValue();
366     image_infos[i].header.cputype =
367         mh->GetValueForKey("cputype")->GetAsInteger()->GetValue();
368     image_infos[i].header.cpusubtype =
369         mh->GetValueForKey("cpusubtype")->GetAsInteger()->GetValue();
370     image_infos[i].header.filetype =
371         mh->GetValueForKey("filetype")->GetAsInteger()->GetValue();
372 
373     if (image->HasKey("min_version_os_name")) {
374       std::string os_name = image->GetValueForKey("min_version_os_name")
375                                 ->GetAsString()
376                                 ->GetValue();
377       if (os_name == "macosx")
378         image_infos[i].os_type = llvm::Triple::MacOSX;
379       else if (os_name == "ios" || os_name == "iphoneos")
380         image_infos[i].os_type = llvm::Triple::IOS;
381       else if (os_name == "tvos")
382         image_infos[i].os_type = llvm::Triple::TvOS;
383       else if (os_name == "watchos")
384         image_infos[i].os_type = llvm::Triple::WatchOS;
385       // NEED_BRIDGEOS_TRIPLE else if (os_name == "bridgeos")
386       // NEED_BRIDGEOS_TRIPLE   image_infos[i].os_type = llvm::Triple::BridgeOS;
387     }
388     if (image->HasKey("min_version_os_sdk")) {
389       image_infos[i].min_version_os_sdk =
390           image->GetValueForKey("min_version_os_sdk")
391               ->GetAsString()
392               ->GetValue();
393     }
394 
395     // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't
396     // currently send them in the reply.
397 
398     if (mh->HasKey("flags"))
399       image_infos[i].header.flags =
400           mh->GetValueForKey("flags")->GetAsInteger()->GetValue();
401     else
402       image_infos[i].header.flags = 0;
403 
404     if (mh->HasKey("ncmds"))
405       image_infos[i].header.ncmds =
406           mh->GetValueForKey("ncmds")->GetAsInteger()->GetValue();
407     else
408       image_infos[i].header.ncmds = 0;
409 
410     if (mh->HasKey("sizeofcmds"))
411       image_infos[i].header.sizeofcmds =
412           mh->GetValueForKey("sizeofcmds")->GetAsInteger()->GetValue();
413     else
414       image_infos[i].header.sizeofcmds = 0;
415 
416     StructuredData::Array *segments =
417         image->GetValueForKey("segments")->GetAsArray();
418     uint32_t segcount = segments->GetSize();
419     for (size_t j = 0; j < segcount; j++) {
420       Segment segment;
421       StructuredData::Dictionary *seg =
422           segments->GetItemAtIndex(j)->GetAsDictionary();
423       segment.name =
424           ConstString(seg->GetValueForKey("name")->GetAsString()->GetValue());
425       segment.vmaddr =
426           seg->GetValueForKey("vmaddr")->GetAsInteger()->GetValue();
427       segment.vmsize =
428           seg->GetValueForKey("vmsize")->GetAsInteger()->GetValue();
429       segment.fileoff =
430           seg->GetValueForKey("fileoff")->GetAsInteger()->GetValue();
431       segment.filesize =
432           seg->GetValueForKey("filesize")->GetAsInteger()->GetValue();
433       segment.maxprot =
434           seg->GetValueForKey("maxprot")->GetAsInteger()->GetValue();
435 
436       // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't
437       // currently send them in the reply.
438 
439       if (seg->HasKey("initprot"))
440         segment.initprot =
441             seg->GetValueForKey("initprot")->GetAsInteger()->GetValue();
442       else
443         segment.initprot = 0;
444 
445       if (seg->HasKey("flags"))
446         segment.flags =
447             seg->GetValueForKey("flags")->GetAsInteger()->GetValue();
448       else
449         segment.flags = 0;
450 
451       if (seg->HasKey("nsects"))
452         segment.nsects =
453             seg->GetValueForKey("nsects")->GetAsInteger()->GetValue();
454       else
455         segment.nsects = 0;
456 
457       image_infos[i].segments.push_back(segment);
458     }
459 
460     image_infos[i].uuid.SetFromOptionalStringRef(
461         image->GetValueForKey("uuid")->GetAsString()->GetValue());
462 
463     // All sections listed in the dyld image info structure will all either be
464     // fixed up already, or they will all be off by a single slide amount that
465     // is determined by finding the first segment that is at file offset zero
466     // which also has bytes (a file size that is greater than zero) in the
467     // object file.
468 
469     // Determine the slide amount (if any)
470     const size_t num_sections = image_infos[i].segments.size();
471     for (size_t k = 0; k < num_sections; ++k) {
472       // Iterate through the object file sections to find the first section
473       // that starts of file offset zero and that has bytes in the file...
474       if ((image_infos[i].segments[k].fileoff == 0 &&
475            image_infos[i].segments[k].filesize > 0) ||
476           (image_infos[i].segments[k].name == "__TEXT")) {
477         image_infos[i].slide =
478             image_infos[i].address - image_infos[i].segments[k].vmaddr;
479         // We have found the slide amount, so we can exit this for loop.
480         break;
481       }
482     }
483   }
484 
485   return true;
486 }
487 
488 void DynamicLoaderDarwin::UpdateSpecialBinariesFromNewImageInfos(
489     ImageInfo::collection &image_infos) {
490   uint32_t exe_idx = UINT32_MAX;
491   uint32_t dyld_idx = UINT32_MAX;
492   Target &target = m_process->GetTarget();
493   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
494   ConstString g_dyld_sim_filename("dyld_sim");
495 
496   ArchSpec target_arch = target.GetArchitecture();
497   const size_t image_infos_size = image_infos.size();
498   for (size_t i = 0; i < image_infos_size; i++) {
499     if (image_infos[i].header.filetype == llvm::MachO::MH_DYLINKER) {
500       // In a "simulator" process (an x86 process that is
501       // ios/tvos/watchos/bridgeos) we will have two dyld modules --
502       // a "dyld" that we want to keep track of, and a "dyld_sim" which
503       // we don't need to keep track of here. If the target is an x86
504       // system and the OS of the dyld binary is ios/tvos/watchos/bridgeos,
505       // then we are looking at dyld_sym.
506 
507       // debugserver has only recently (late 2016) started sending up the os
508       // type for each binary it sees -- so if we don't have an os type, use a
509       // filename check as our next best guess.
510       if (image_infos[i].os_type == llvm::Triple::OSType::UnknownOS) {
511         if (image_infos[i].file_spec.GetFilename() != g_dyld_sim_filename) {
512           dyld_idx = i;
513         }
514       } else if (target_arch.GetTriple().getArch() == llvm::Triple::x86 ||
515                  target_arch.GetTriple().getArch() == llvm::Triple::x86_64) {
516         if (image_infos[i].os_type != llvm::Triple::OSType::IOS &&
517             image_infos[i].os_type != llvm::Triple::TvOS &&
518             image_infos[i].os_type != llvm::Triple::WatchOS) {
519             // NEED_BRIDGEOS_TRIPLE image_infos[i].os_type != llvm::Triple::BridgeOS) {
520           dyld_idx = i;
521         }
522       }
523       else {
524         // catch-all for any other environment -- trust that dyld is actually
525         // dyld
526         dyld_idx = i;
527       }
528     } else if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE) {
529       exe_idx = i;
530     }
531   }
532 
533   if (exe_idx != UINT32_MAX) {
534     const bool can_create = true;
535     ModuleSP exe_module_sp(FindTargetModuleForImageInfo(image_infos[exe_idx],
536                                                         can_create, nullptr));
537     if (exe_module_sp) {
538       LLDB_LOGF(log, "Found executable module: %s",
539                 exe_module_sp->GetFileSpec().GetPath().c_str());
540       target.GetImages().AppendIfNeeded(exe_module_sp);
541       UpdateImageLoadAddress(exe_module_sp.get(), image_infos[exe_idx]);
542       if (exe_module_sp.get() != target.GetExecutableModulePointer()) {
543         target.SetExecutableModule(exe_module_sp, eLoadDependentsNo);
544       }
545     }
546   }
547 
548   if (dyld_idx != UINT32_MAX) {
549     const bool can_create = true;
550     ModuleSP dyld_sp = FindTargetModuleForImageInfo(image_infos[dyld_idx],
551                                                     can_create, nullptr);
552     if (dyld_sp.get()) {
553       LLDB_LOGF(log, "Found dyld module: %s",
554                 dyld_sp->GetFileSpec().GetPath().c_str());
555       target.GetImages().AppendIfNeeded(dyld_sp);
556       UpdateImageLoadAddress(dyld_sp.get(), image_infos[dyld_idx]);
557       SetDYLDModule(dyld_sp);
558     }
559   }
560 }
561 
562 void DynamicLoaderDarwin::UpdateDYLDImageInfoFromNewImageInfo(
563     ImageInfo &image_info) {
564   if (image_info.header.filetype == llvm::MachO::MH_DYLINKER) {
565     const bool can_create = true;
566     ModuleSP dyld_sp =
567         FindTargetModuleForImageInfo(image_info, can_create, nullptr);
568     if (dyld_sp.get()) {
569       Target &target = m_process->GetTarget();
570       target.GetImages().AppendIfNeeded(dyld_sp);
571       UpdateImageLoadAddress(dyld_sp.get(), image_info);
572       SetDYLDModule(dyld_sp);
573     }
574   }
575 }
576 
577 void DynamicLoaderDarwin::SetDYLDModule(lldb::ModuleSP &dyld_module_sp) {
578   m_dyld_module_wp = dyld_module_sp;
579 }
580 
581 ModuleSP DynamicLoaderDarwin::GetDYLDModule() {
582   ModuleSP dyld_sp(m_dyld_module_wp.lock());
583   return dyld_sp;
584 }
585 
586 bool DynamicLoaderDarwin::AddModulesUsingImageInfos(
587     ImageInfo::collection &image_infos) {
588   std::lock_guard<std::recursive_mutex> guard(m_mutex);
589   // Now add these images to the main list.
590   ModuleList loaded_module_list;
591   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
592   Target &target = m_process->GetTarget();
593   ModuleList &target_images = target.GetImages();
594 
595   for (uint32_t idx = 0; idx < image_infos.size(); ++idx) {
596     if (log) {
597       LLDB_LOGF(log, "Adding new image at address=0x%16.16" PRIx64 ".",
598                 image_infos[idx].address);
599       image_infos[idx].PutToLog(log);
600     }
601 
602     m_dyld_image_infos.push_back(image_infos[idx]);
603 
604     ModuleSP image_module_sp(
605         FindTargetModuleForImageInfo(image_infos[idx], true, nullptr));
606 
607     if (image_module_sp) {
608       ObjectFile *objfile = image_module_sp->GetObjectFile();
609       if (objfile) {
610         SectionList *sections = objfile->GetSectionList();
611         if (sections) {
612           ConstString commpage_dbstr("__commpage");
613           Section *commpage_section =
614               sections->FindSectionByName(commpage_dbstr).get();
615           if (commpage_section) {
616             ModuleSpec module_spec(objfile->GetFileSpec(),
617                                    image_infos[idx].GetArchitecture());
618             module_spec.GetObjectName() = commpage_dbstr;
619             ModuleSP commpage_image_module_sp(
620                 target_images.FindFirstModule(module_spec));
621             if (!commpage_image_module_sp) {
622               module_spec.SetObjectOffset(objfile->GetFileOffset() +
623                                           commpage_section->GetFileOffset());
624               module_spec.SetObjectSize(objfile->GetByteSize());
625               commpage_image_module_sp = target.GetOrCreateModule(module_spec,
626                                                                true /* notify */);
627               if (!commpage_image_module_sp ||
628                   commpage_image_module_sp->GetObjectFile() == nullptr) {
629                 commpage_image_module_sp = m_process->ReadModuleFromMemory(
630                     image_infos[idx].file_spec, image_infos[idx].address);
631                 // Always load a memory image right away in the target in case
632                 // we end up trying to read the symbol table from memory... The
633                 // __LINKEDIT will need to be mapped so we can figure out where
634                 // the symbol table bits are...
635                 bool changed = false;
636                 UpdateImageLoadAddress(commpage_image_module_sp.get(),
637                                        image_infos[idx]);
638                 target.GetImages().Append(commpage_image_module_sp);
639                 if (changed) {
640                   image_infos[idx].load_stop_id = m_process->GetStopID();
641                   loaded_module_list.AppendIfNeeded(commpage_image_module_sp);
642                 }
643               }
644             }
645           }
646         }
647       }
648 
649       // UpdateImageLoadAddress will return true if any segments change load
650       // address. We need to check this so we don't mention that all loaded
651       // shared libraries are newly loaded each time we hit out dyld breakpoint
652       // since dyld will list all shared libraries each time.
653       if (UpdateImageLoadAddress(image_module_sp.get(), image_infos[idx])) {
654         target_images.AppendIfNeeded(image_module_sp);
655         loaded_module_list.AppendIfNeeded(image_module_sp);
656       }
657     }
658   }
659 
660   if (loaded_module_list.GetSize() > 0) {
661     if (log)
662       loaded_module_list.LogUUIDAndPaths(log,
663                                          "DynamicLoaderDarwin::ModulesDidLoad");
664     m_process->GetTarget().ModulesDidLoad(loaded_module_list);
665   }
666   return true;
667 }
668 
669 // On Mac OS X libobjc (the Objective-C runtime) has several critical dispatch
670 // functions written in hand-written assembly, and also have hand-written
671 // unwind information in the eh_frame section.  Normally we prefer analyzing
672 // the assembly instructions of a currently executing frame to unwind from that
673 // frame -- but on hand-written functions this profiling can fail.  We should
674 // use the eh_frame instructions for these functions all the time.
675 //
676 // As an aside, it would be better if the eh_frame entries had a flag (or were
677 // extensible so they could have an Apple-specific flag) which indicates that
678 // the instructions are asynchronous -- accurate at every instruction, instead
679 // of our normal default assumption that they are not.
680 
681 bool DynamicLoaderDarwin::AlwaysRelyOnEHUnwindInfo(SymbolContext &sym_ctx) {
682   ModuleSP module_sp;
683   if (sym_ctx.symbol) {
684     module_sp = sym_ctx.symbol->GetAddressRef().GetModule();
685   }
686   if (module_sp.get() == nullptr && sym_ctx.function) {
687     module_sp =
688         sym_ctx.function->GetAddressRange().GetBaseAddress().GetModule();
689   }
690   if (module_sp.get() == nullptr)
691     return false;
692 
693   ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*m_process);
694   return objc_runtime != nullptr &&
695          objc_runtime->IsModuleObjCLibrary(module_sp);
696 }
697 
698 // Dump a Segment to the file handle provided.
699 void DynamicLoaderDarwin::Segment::PutToLog(Log *log,
700                                             lldb::addr_t slide) const {
701   if (log) {
702     if (slide == 0)
703       LLDB_LOGF(log, "\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")",
704                 name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize);
705     else
706       LLDB_LOGF(log,
707                 "\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64
708                 ") slide = 0x%" PRIx64,
709                 name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize,
710                 slide);
711   }
712 }
713 
714 const DynamicLoaderDarwin::Segment *
715 DynamicLoaderDarwin::ImageInfo::FindSegment(ConstString name) const {
716   const size_t num_segments = segments.size();
717   for (size_t i = 0; i < num_segments; ++i) {
718     if (segments[i].name == name)
719       return &segments[i];
720   }
721   return nullptr;
722 }
723 
724 // Dump an image info structure to the file handle provided.
725 void DynamicLoaderDarwin::ImageInfo::PutToLog(Log *log) const {
726   if (!log)
727     return;
728   if (address == LLDB_INVALID_ADDRESS) {
729     LLDB_LOG(log, "modtime={0:x+8} uuid={1} path='{2}' (UNLOADED)", mod_date,
730              uuid.GetAsString(), file_spec.GetPath());
731   } else {
732     LLDB_LOG(log, "address={0:x+16} modtime={1:x+8} uuid={2} path='{3}'",
733              address, mod_date, uuid.GetAsString(), file_spec.GetPath());
734     for (uint32_t i = 0; i < segments.size(); ++i)
735       segments[i].PutToLog(log, slide);
736   }
737 }
738 
739 void DynamicLoaderDarwin::PrivateInitialize(Process *process) {
740   DEBUG_PRINTF("DynamicLoaderDarwin::%s() process state = %s\n", __FUNCTION__,
741                StateAsCString(m_process->GetState()));
742   Clear(true);
743   m_process = process;
744   m_process->GetTarget().ClearAllLoadedSections();
745 }
746 
747 // Member function that gets called when the process state changes.
748 void DynamicLoaderDarwin::PrivateProcessStateChanged(Process *process,
749                                                      StateType state) {
750   DEBUG_PRINTF("DynamicLoaderDarwin::%s(%s)\n", __FUNCTION__,
751                StateAsCString(state));
752   switch (state) {
753   case eStateConnected:
754   case eStateAttaching:
755   case eStateLaunching:
756   case eStateInvalid:
757   case eStateUnloaded:
758   case eStateExited:
759   case eStateDetached:
760     Clear(false);
761     break;
762 
763   case eStateStopped:
764     // Keep trying find dyld and set our notification breakpoint each time we
765     // stop until we succeed
766     if (!DidSetNotificationBreakpoint() && m_process->IsAlive()) {
767       if (NeedToDoInitialImageFetch())
768         DoInitialImageFetch();
769 
770       SetNotificationBreakpoint();
771     }
772     break;
773 
774   case eStateRunning:
775   case eStateStepping:
776   case eStateCrashed:
777   case eStateSuspended:
778     break;
779   }
780 }
781 
782 ThreadPlanSP
783 DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread,
784                                                   bool stop_others) {
785   ThreadPlanSP thread_plan_sp;
786   StackFrame *current_frame = thread.GetStackFrameAtIndex(0).get();
787   const SymbolContext &current_context =
788       current_frame->GetSymbolContext(eSymbolContextSymbol);
789   Symbol *current_symbol = current_context.symbol;
790   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
791   TargetSP target_sp(thread.CalculateTarget());
792 
793   if (current_symbol != nullptr) {
794     std::vector<Address> addresses;
795 
796     if (current_symbol->IsTrampoline()) {
797       ConstString trampoline_name = current_symbol->GetMangled().GetName(
798           current_symbol->GetLanguage(), Mangled::ePreferMangled);
799 
800       if (trampoline_name) {
801         const ModuleList &images = target_sp->GetImages();
802 
803         SymbolContextList code_symbols;
804         images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeCode,
805                                           code_symbols);
806         size_t num_code_symbols = code_symbols.GetSize();
807 
808         if (num_code_symbols > 0) {
809           for (uint32_t i = 0; i < num_code_symbols; i++) {
810             SymbolContext context;
811             AddressRange addr_range;
812             if (code_symbols.GetContextAtIndex(i, context)) {
813               context.GetAddressRange(eSymbolContextEverything, 0, false,
814                                       addr_range);
815               addresses.push_back(addr_range.GetBaseAddress());
816               if (log) {
817                 addr_t load_addr =
818                     addr_range.GetBaseAddress().GetLoadAddress(target_sp.get());
819 
820                 LLDB_LOGF(log,
821                           "Found a trampoline target symbol at 0x%" PRIx64 ".",
822                           load_addr);
823               }
824             }
825           }
826         }
827 
828         SymbolContextList reexported_symbols;
829         images.FindSymbolsWithNameAndType(
830             trampoline_name, eSymbolTypeReExported, reexported_symbols);
831         size_t num_reexported_symbols = reexported_symbols.GetSize();
832         if (num_reexported_symbols > 0) {
833           for (uint32_t i = 0; i < num_reexported_symbols; i++) {
834             SymbolContext context;
835             if (reexported_symbols.GetContextAtIndex(i, context)) {
836               if (context.symbol) {
837                 Symbol *actual_symbol =
838                     context.symbol->ResolveReExportedSymbol(*target_sp.get());
839                 if (actual_symbol) {
840                   const Address actual_symbol_addr =
841                       actual_symbol->GetAddress();
842                   if (actual_symbol_addr.IsValid()) {
843                     addresses.push_back(actual_symbol_addr);
844                     if (log) {
845                       lldb::addr_t load_addr =
846                           actual_symbol_addr.GetLoadAddress(target_sp.get());
847                       LLDB_LOGF(
848                           log,
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                 LLDB_LOGF(log,
876                           "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           LLDB_LOGF(
893               log,
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             LLDB_LOGF(log,
918                       "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     LLDB_LOGF(log, "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       LLDB_LOGF(
1111           log, "DynamicLoaderDarwin::UseDYLDSPI: Use new DynamicLoader plugin");
1112     else
1113       LLDB_LOGF(
1114           log, "DynamicLoaderDarwin::UseDYLDSPI: Use old DynamicLoader plugin");
1115   }
1116   return use_new_spi_interface;
1117 }
1118