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