1//===-- DynamicLoaderDarwin.cpp -------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "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/Host/HostInfo.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/Target/ThreadPlanStepInstruction.h"
30#include "lldb/Utility/DataBuffer.h"
31#include "lldb/Utility/DataBufferHeap.h"
32#include "lldb/Utility/LLDBLog.h"
33#include "lldb/Utility/Log.h"
34#include "lldb/Utility/State.h"
35#include "llvm/Support/ThreadPool.h"
36
37#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
38#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
39
40//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
41#ifdef ENABLE_DEBUG_PRINTF
42#include <cstdio>
43#define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
44#else
45#define DEBUG_PRINTF(fmt, ...)
46#endif
47
48#include <memory>
49
50using namespace lldb;
51using namespace lldb_private;
52
53// Constructor
54DynamicLoaderDarwin::DynamicLoaderDarwin(Process *process)
55 : DynamicLoader(process), m_dyld_module_wp(), m_libpthread_module_wp(),
56 m_pthread_getspecific_addr(), m_tid_to_tls_map(), m_dyld_image_infos(),
57 m_dyld_image_infos_stop_id(UINT32_MAX), m_dyld(), m_mutex() {}
58
59// Destructor
60DynamicLoaderDarwin::~DynamicLoaderDarwin() = default;
61
62/// Called after attaching a process.
63///
64/// Allow DynamicLoader plug-ins to execute some code after
65/// attaching to a process.
66void DynamicLoaderDarwin::DidAttach() {
67 PrivateInitialize(process: m_process);
68 DoInitialImageFetch();
69 SetNotificationBreakpoint();
70}
71
72/// Called after attaching a process.
73///
74/// Allow DynamicLoader plug-ins to execute some code after
75/// attaching to a process.
76void DynamicLoaderDarwin::DidLaunch() {
77 PrivateInitialize(process: m_process);
78 DoInitialImageFetch();
79 SetNotificationBreakpoint();
80}
81
82// Clear out the state of this class.
83void DynamicLoaderDarwin::Clear(bool clear_process) {
84 std::lock_guard<std::recursive_mutex> guard(m_mutex);
85 if (clear_process)
86 m_process = nullptr;
87 m_dyld_image_infos.clear();
88 m_dyld_image_infos_stop_id = UINT32_MAX;
89 m_dyld.Clear(load_cmd_data_only: false);
90}
91
92ModuleSP DynamicLoaderDarwin::FindTargetModuleForImageInfo(
93 const ImageInfo &image_info, bool can_create, bool *did_create_ptr) {
94 if (did_create_ptr)
95 *did_create_ptr = false;
96
97 Target &target = m_process->GetTarget();
98 const ModuleList &target_images = target.GetImages();
99 ModuleSpec module_spec(image_info.file_spec);
100 module_spec.GetUUID() = image_info.uuid;
101
102 // macCatalyst support: Request matching os/environment.
103 {
104 auto &target_triple = target.GetArchitecture().GetTriple();
105 if (target_triple.getOS() == llvm::Triple::IOS &&
106 target_triple.getEnvironment() == llvm::Triple::MacABI) {
107 // Request the macCatalyst variant of frameworks that have both
108 // a PLATFORM_MACOS and a PLATFORM_MACCATALYST load command.
109 module_spec.GetArchitecture() = ArchSpec(target_triple);
110 }
111 }
112
113 ModuleSP module_sp(target_images.FindFirstModule(module_spec));
114
115 if (module_sp && !module_spec.GetUUID().IsValid() &&
116 !module_sp->GetUUID().IsValid()) {
117 // No UUID, we must rely upon the cached module modification time and the
118 // modification time of the file on disk
119 if (module_sp->GetModificationTime() !=
120 FileSystem::Instance().GetModificationTime(file_spec: module_sp->GetFileSpec()))
121 module_sp.reset();
122 }
123
124 if (module_sp || !can_create)
125 return module_sp;
126
127 if (HostInfo::GetArchitecture().IsCompatibleMatch(rhs: target.GetArchitecture())) {
128 // When debugging on the host, we are most likely using the same shared
129 // cache as our inferior. The dylibs from the shared cache might not
130 // exist on the filesystem, so let's use the images in our own memory
131 // to create the modules.
132 // Check if the requested image is in our shared cache.
133 SharedCacheImageInfo image_info =
134 HostInfo::GetSharedCacheImageInfo(image_name: module_spec.GetFileSpec().GetPath());
135
136 // If we found it and it has the correct UUID, let's proceed with
137 // creating a module from the memory contents.
138 if (image_info.uuid &&
139 (!module_spec.GetUUID() || module_spec.GetUUID() == image_info.uuid)) {
140 ModuleSpec shared_cache_spec(module_spec.GetFileSpec(), image_info.uuid,
141 image_info.data_sp);
142 module_sp =
143 target.GetOrCreateModule(module_spec: shared_cache_spec, notify: false /* notify */);
144 }
145 }
146 // We'll call Target::ModulesDidLoad after all the modules have been
147 // added to the target, don't let it be called for every one.
148 if (!module_sp)
149 module_sp = target.GetOrCreateModule(module_spec, notify: false /* notify */);
150 if (!module_sp || module_sp->GetObjectFile() == nullptr)
151 module_sp = m_process->ReadModuleFromMemory(file_spec: image_info.file_spec,
152 header_addr: image_info.address);
153
154 if (did_create_ptr)
155 *did_create_ptr = (bool)module_sp;
156
157 return module_sp;
158}
159
160void DynamicLoaderDarwin::UnloadImages(
161 const std::vector<lldb::addr_t> &solib_addresses) {
162 std::lock_guard<std::recursive_mutex> guard(m_mutex);
163 if (m_process->GetStopID() == m_dyld_image_infos_stop_id)
164 return;
165
166 Log *log = GetLog(mask: LLDBLog::DynamicLoader);
167 Target &target = m_process->GetTarget();
168 LLDB_LOGF(log, "Removing %" PRId64 " modules.",
169 (uint64_t)solib_addresses.size());
170
171 ModuleList unloaded_module_list;
172
173 for (addr_t solib_addr : solib_addresses) {
174 Address header;
175 if (header.SetLoadAddress(load_addr: solib_addr, target: &target)) {
176 if (header.GetOffset() == 0) {
177 ModuleSP module_to_remove(header.GetModule());
178 if (module_to_remove.get()) {
179 LLDB_LOGF(log, "Removing module at address 0x%" PRIx64, solib_addr);
180 // remove the sections from the Target
181 UnloadSections(module: module_to_remove);
182 // add this to the list of modules to remove
183 unloaded_module_list.AppendIfNeeded(new_module: module_to_remove);
184 // remove the entry from the m_dyld_image_infos
185 ImageInfo::collection::iterator pos, end = m_dyld_image_infos.end();
186 for (pos = m_dyld_image_infos.begin(); pos != end; pos++) {
187 if (solib_addr == (*pos).address) {
188 m_dyld_image_infos.erase(position: pos);
189 break;
190 }
191 }
192 }
193 }
194 }
195 }
196
197 if (unloaded_module_list.GetSize() > 0) {
198 if (log) {
199 log->PutCString(cstr: "Unloaded:");
200 unloaded_module_list.LogUUIDAndPaths(
201 log, prefix_cstr: "DynamicLoaderDarwin::UnloadModules");
202 }
203 m_process->GetTarget().GetImages().Remove(module_list&: unloaded_module_list);
204 m_dyld_image_infos_stop_id = m_process->GetStopID();
205 }
206}
207
208void DynamicLoaderDarwin::UnloadAllImages() {
209 Log *log = GetLog(mask: LLDBLog::DynamicLoader);
210 ModuleList unloaded_modules_list;
211
212 Target &target = m_process->GetTarget();
213 const ModuleList &target_modules = target.GetImages();
214 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
215
216 ModuleSP dyld_sp(GetDYLDModule());
217 for (ModuleSP module_sp : target_modules.Modules()) {
218 // Don't remove dyld - else we'll lose our breakpoint notifying us about
219 // libraries being re-loaded...
220 if (module_sp && module_sp != dyld_sp) {
221 UnloadSections(module: module_sp);
222 unloaded_modules_list.Append(module_sp);
223 }
224 }
225
226 if (unloaded_modules_list.GetSize() != 0) {
227 if (log) {
228 log->PutCString(cstr: "Unloaded:");
229 unloaded_modules_list.LogUUIDAndPaths(
230 log, prefix_cstr: "DynamicLoaderDarwin::UnloadAllImages");
231 }
232 target.GetImages().Remove(module_list&: unloaded_modules_list);
233 m_dyld_image_infos.clear();
234 m_dyld_image_infos_stop_id = m_process->GetStopID();
235 }
236}
237
238// Update the load addresses for all segments in MODULE using the updated INFO
239// that is passed in.
240bool DynamicLoaderDarwin::UpdateImageLoadAddress(Module *module,
241 ImageInfo &info) {
242 bool changed = false;
243 if (module) {
244 ObjectFile *image_object_file = module->GetObjectFile();
245 if (image_object_file) {
246 SectionList *section_list = image_object_file->GetSectionList();
247 if (section_list) {
248 std::vector<uint32_t> inaccessible_segment_indexes;
249 // We now know the slide amount, so go through all sections and update
250 // the load addresses with the correct values.
251 const size_t num_segments = info.segments.size();
252 for (size_t i = 0; i < num_segments; ++i) {
253 // Only load a segment if it has protections. Things like __PAGEZERO
254 // don't have any protections, and they shouldn't be slid
255 SectionSP section_sp(
256 section_list->FindSectionByName(section_dstr: info.segments[i].name));
257
258 if (info.segments[i].maxprot == 0) {
259 inaccessible_segment_indexes.push_back(x: i);
260 } else {
261 const addr_t new_section_load_addr =
262 info.segments[i].vmaddr + info.slide;
263 static ConstString g_section_name_LINKEDIT("__LINKEDIT");
264
265 if (section_sp) {
266 // __LINKEDIT sections from files in the shared cache can overlap
267 // so check to see what the segment name is and pass "false" so
268 // we don't warn of overlapping "Section" objects, and "true" for
269 // all other sections.
270 const bool warn_multiple =
271 section_sp->GetName() != g_section_name_LINKEDIT;
272
273 changed = m_process->GetTarget().SetSectionLoadAddress(
274 section: section_sp, load_addr: new_section_load_addr, warn_multiple);
275 }
276 }
277 }
278
279 // If the loaded the file (it changed) and we have segments that are
280 // not readable or writeable, add them to the invalid memory region
281 // cache for the process. This will typically only be the __PAGEZERO
282 // segment in the main executable. We might be able to apply this more
283 // generally to more sections that have no protections in the future,
284 // but for now we are going to just do __PAGEZERO.
285 if (changed && !inaccessible_segment_indexes.empty()) {
286 for (uint32_t i = 0; i < inaccessible_segment_indexes.size(); ++i) {
287 const uint32_t seg_idx = inaccessible_segment_indexes[i];
288 SectionSP section_sp(
289 section_list->FindSectionByName(section_dstr: info.segments[seg_idx].name));
290
291 if (section_sp) {
292 static ConstString g_pagezero_section_name("__PAGEZERO");
293 if (g_pagezero_section_name == section_sp->GetName()) {
294 // __PAGEZERO never slides...
295 const lldb::addr_t vmaddr = info.segments[seg_idx].vmaddr;
296 const lldb::addr_t vmsize = info.segments[seg_idx].vmsize;
297 Process::LoadRange pagezero_range(vmaddr, vmsize);
298 m_process->AddInvalidMemoryRegion(region: pagezero_range);
299 }
300 }
301 }
302 }
303 }
304 }
305 }
306 // We might have an in memory image that was loaded as soon as it was created
307 if (info.load_stop_id == m_process->GetStopID())
308 changed = true;
309 else if (changed) {
310 // Update the stop ID when this library was updated
311 info.load_stop_id = m_process->GetStopID();
312 }
313 return changed;
314}
315
316// Unload the segments in MODULE using the INFO that is passed in.
317bool DynamicLoaderDarwin::UnloadModuleSections(Module *module,
318 ImageInfo &info) {
319 bool changed = false;
320 if (module) {
321 ObjectFile *image_object_file = module->GetObjectFile();
322 if (image_object_file) {
323 SectionList *section_list = image_object_file->GetSectionList();
324 if (section_list) {
325 const size_t num_segments = info.segments.size();
326 for (size_t i = 0; i < num_segments; ++i) {
327 SectionSP section_sp(
328 section_list->FindSectionByName(section_dstr: info.segments[i].name));
329 if (section_sp) {
330 const addr_t old_section_load_addr =
331 info.segments[i].vmaddr + info.slide;
332 if (m_process->GetTarget().SetSectionUnloaded(
333 section_sp, load_addr: old_section_load_addr))
334 changed = true;
335 } else {
336 Debugger::ReportWarning(
337 message: llvm::formatv(Fmt: "unable to find and unload segment named "
338 "'{0}' in '{1}' in macosx dynamic loader plug-in",
339 Vals: info.segments[i].name.AsCString(value_if_empty: "<invalid>"),
340 Vals: image_object_file->GetFileSpec().GetPath()));
341 }
342 }
343 }
344 }
345 }
346 return changed;
347}
348
349// Given a JSON dictionary (from debugserver, most likely) of binary images
350// loaded in the inferior process, add the images to the ImageInfo collection.
351
352bool DynamicLoaderDarwin::JSONImageInformationIntoImageInfo(
353 StructuredData::ObjectSP image_details,
354 ImageInfo::collection &image_infos) {
355 StructuredData::ObjectSP images_sp =
356 image_details->GetAsDictionary()->GetValueForKey(key: "images");
357 if (images_sp.get() == nullptr)
358 return false;
359
360 image_infos.resize(new_size: images_sp->GetAsArray()->GetSize());
361
362 for (size_t i = 0; i < image_infos.size(); i++) {
363 StructuredData::ObjectSP image_sp =
364 images_sp->GetAsArray()->GetItemAtIndex(idx: i);
365 if (image_sp.get() == nullptr || image_sp->GetAsDictionary() == nullptr)
366 return false;
367 StructuredData::Dictionary *image = image_sp->GetAsDictionary();
368 // clang-format off
369 if (!image->HasKey(key: "load_address") ||
370 !image->HasKey(key: "pathname") ||
371 !image->HasKey(key: "mach_header") ||
372 image->GetValueForKey(key: "mach_header")->GetAsDictionary() == nullptr ||
373 !image->HasKey(key: "segments") ||
374 image->GetValueForKey(key: "segments")->GetAsArray() == nullptr ||
375 !image->HasKey(key: "uuid")) {
376 return false;
377 }
378 // clang-format on
379 image_infos[i].address =
380 image->GetValueForKey(key: "load_address")->GetUnsignedIntegerValue();
381 image_infos[i].file_spec.SetFile(
382 path: image->GetValueForKey(key: "pathname")->GetAsString()->GetValue(),
383 style: FileSpec::Style::native);
384
385 StructuredData::Dictionary *mh =
386 image->GetValueForKey(key: "mach_header")->GetAsDictionary();
387 image_infos[i].header.magic =
388 mh->GetValueForKey(key: "magic")->GetUnsignedIntegerValue();
389 image_infos[i].header.cputype =
390 mh->GetValueForKey(key: "cputype")->GetUnsignedIntegerValue();
391 image_infos[i].header.cpusubtype =
392 mh->GetValueForKey(key: "cpusubtype")->GetUnsignedIntegerValue();
393 image_infos[i].header.filetype =
394 mh->GetValueForKey(key: "filetype")->GetUnsignedIntegerValue();
395
396 if (image->HasKey(key: "min_version_os_name")) {
397 std::string os_name =
398 std::string(image->GetValueForKey(key: "min_version_os_name")
399 ->GetAsString()
400 ->GetValue());
401 if (os_name == "macosx")
402 image_infos[i].os_type = llvm::Triple::MacOSX;
403 else if (os_name == "ios" || os_name == "iphoneos")
404 image_infos[i].os_type = llvm::Triple::IOS;
405 else if (os_name == "tvos")
406 image_infos[i].os_type = llvm::Triple::TvOS;
407 else if (os_name == "watchos")
408 image_infos[i].os_type = llvm::Triple::WatchOS;
409 else if (os_name == "bridgeos")
410 image_infos[i].os_type = llvm::Triple::BridgeOS;
411 else if (os_name == "driverkit")
412 image_infos[i].os_type = llvm::Triple::DriverKit;
413 else if (os_name == "xros")
414 image_infos[i].os_type = llvm::Triple::XROS;
415 else if (os_name == "maccatalyst") {
416 image_infos[i].os_type = llvm::Triple::IOS;
417 image_infos[i].os_env = llvm::Triple::MacABI;
418 } else if (os_name == "iossimulator") {
419 image_infos[i].os_type = llvm::Triple::IOS;
420 image_infos[i].os_env = llvm::Triple::Simulator;
421 } else if (os_name == "tvossimulator") {
422 image_infos[i].os_type = llvm::Triple::TvOS;
423 image_infos[i].os_env = llvm::Triple::Simulator;
424 } else if (os_name == "watchossimulator") {
425 image_infos[i].os_type = llvm::Triple::WatchOS;
426 image_infos[i].os_env = llvm::Triple::Simulator;
427 } else if (os_name == "xrsimulator") {
428 image_infos[i].os_type = llvm::Triple::XROS;
429 image_infos[i].os_env = llvm::Triple::Simulator;
430 }
431 }
432 if (image->HasKey(key: "min_version_os_sdk")) {
433 image_infos[i].min_version_os_sdk =
434 std::string(image->GetValueForKey(key: "min_version_os_sdk")
435 ->GetAsString()
436 ->GetValue());
437 }
438
439 // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't
440 // currently send them in the reply.
441
442 if (mh->HasKey(key: "flags"))
443 image_infos[i].header.flags =
444 mh->GetValueForKey(key: "flags")->GetUnsignedIntegerValue();
445 else
446 image_infos[i].header.flags = 0;
447
448 if (mh->HasKey(key: "ncmds"))
449 image_infos[i].header.ncmds =
450 mh->GetValueForKey(key: "ncmds")->GetUnsignedIntegerValue();
451 else
452 image_infos[i].header.ncmds = 0;
453
454 if (mh->HasKey(key: "sizeofcmds"))
455 image_infos[i].header.sizeofcmds =
456 mh->GetValueForKey(key: "sizeofcmds")->GetUnsignedIntegerValue();
457 else
458 image_infos[i].header.sizeofcmds = 0;
459
460 StructuredData::Array *segments =
461 image->GetValueForKey(key: "segments")->GetAsArray();
462 uint32_t segcount = segments->GetSize();
463 for (size_t j = 0; j < segcount; j++) {
464 Segment segment;
465 StructuredData::Dictionary *seg =
466 segments->GetItemAtIndex(idx: j)->GetAsDictionary();
467 segment.name =
468 ConstString(seg->GetValueForKey(key: "name")->GetAsString()->GetValue());
469 segment.vmaddr = seg->GetValueForKey(key: "vmaddr")->GetUnsignedIntegerValue();
470 segment.vmsize = seg->GetValueForKey(key: "vmsize")->GetUnsignedIntegerValue();
471 segment.fileoff =
472 seg->GetValueForKey(key: "fileoff")->GetUnsignedIntegerValue();
473 segment.filesize =
474 seg->GetValueForKey(key: "filesize")->GetUnsignedIntegerValue();
475 segment.maxprot =
476 seg->GetValueForKey(key: "maxprot")->GetUnsignedIntegerValue();
477
478 // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't
479 // currently send them in the reply.
480
481 if (seg->HasKey(key: "initprot"))
482 segment.initprot =
483 seg->GetValueForKey(key: "initprot")->GetUnsignedIntegerValue();
484 else
485 segment.initprot = 0;
486
487 if (seg->HasKey(key: "flags"))
488 segment.flags = seg->GetValueForKey(key: "flags")->GetUnsignedIntegerValue();
489 else
490 segment.flags = 0;
491
492 if (seg->HasKey(key: "nsects"))
493 segment.nsects =
494 seg->GetValueForKey(key: "nsects")->GetUnsignedIntegerValue();
495 else
496 segment.nsects = 0;
497
498 image_infos[i].segments.push_back(x: segment);
499 }
500
501 image_infos[i].uuid.SetFromStringRef(
502 image->GetValueForKey(key: "uuid")->GetAsString()->GetValue());
503
504 // All sections listed in the dyld image info structure will all either be
505 // fixed up already, or they will all be off by a single slide amount that
506 // is determined by finding the first segment that is at file offset zero
507 // which also has bytes (a file size that is greater than zero) in the
508 // object file.
509
510 // Determine the slide amount (if any)
511 const size_t num_sections = image_infos[i].segments.size();
512 for (size_t k = 0; k < num_sections; ++k) {
513 // Iterate through the object file sections to find the first section
514 // that starts of file offset zero and that has bytes in the file...
515 if ((image_infos[i].segments[k].fileoff == 0 &&
516 image_infos[i].segments[k].filesize > 0) ||
517 (image_infos[i].segments[k].name == "__TEXT")) {
518 image_infos[i].slide =
519 image_infos[i].address - image_infos[i].segments[k].vmaddr;
520 // We have found the slide amount, so we can exit this for loop.
521 break;
522 }
523 }
524 }
525
526 return true;
527}
528
529void DynamicLoaderDarwin::UpdateSpecialBinariesFromPreloadedModules(
530 std::vector<std::pair<ImageInfo, ModuleSP>> &images) {
531 uint32_t exe_idx = UINT32_MAX;
532 uint32_t dyld_idx = UINT32_MAX;
533 Target &target = m_process->GetTarget();
534 Log *log = GetLog(mask: LLDBLog::DynamicLoader);
535 ConstString g_dyld_sim_filename("dyld_sim");
536
537 ArchSpec target_arch = target.GetArchitecture();
538 const size_t images_size = images.size();
539 for (size_t i = 0; i < images_size; i++) {
540 const auto &image_info = images[i].first;
541 if (image_info.header.filetype == llvm::MachO::MH_DYLINKER) {
542 // In a "simulator" process we will have two dyld modules --
543 // a "dyld" that we want to keep track of, and a "dyld_sim" which
544 // we don't need to keep track of here. dyld_sim will have a non-macosx
545 // OS.
546 if (target_arch.GetTriple().getEnvironment() == llvm::Triple::Simulator &&
547 image_info.os_type != llvm::Triple::OSType::MacOSX) {
548 continue;
549 }
550
551 dyld_idx = i;
552 }
553 if (image_info.header.filetype == llvm::MachO::MH_EXECUTE) {
554 exe_idx = i;
555 }
556 }
557
558 // Set the target executable if we haven't found one so far.
559 if (exe_idx != UINT32_MAX && !target.GetExecutableModule()) {
560 ModuleSP exe_module_sp = images[exe_idx].second;
561 if (exe_module_sp) {
562 LLDB_LOGF(log, "Found executable module: %s",
563 exe_module_sp->GetFileSpec().GetPath().c_str());
564 target.GetImages().AppendIfNeeded(new_module: exe_module_sp);
565 UpdateImageLoadAddress(module: exe_module_sp.get(), info&: images[exe_idx].first);
566 if (exe_module_sp.get() != target.GetExecutableModulePointer())
567 target.SetExecutableModule(module_sp&: exe_module_sp, load_dependent_files: eLoadDependentsNo);
568
569 // Update the target executable's arch if necessary.
570 auto exe_triple = exe_module_sp->GetArchitecture().GetTriple();
571 if (target_arch.GetTriple().isArm64e() &&
572 exe_triple.getArch() == llvm::Triple::aarch64 &&
573 !exe_triple.isArm64e()) {
574 // On arm64e-capable Apple platforms, the system libraries are
575 // always arm64e, but applications often are arm64. When a
576 // target is created from a file, LLDB recognizes it as an
577 // arm64 target, but debugserver will still (technically
578 // correct) report the process as being arm64e. For
579 // consistency, set the target to arm64 here, so attaching to
580 // a live process behaves the same as creating a process from
581 // file.
582 auto triple = target_arch.GetTriple();
583 triple.setArchName(exe_triple.getArchName());
584 target_arch.SetTriple(triple);
585 target.SetArchitecture(arch_spec: target_arch, /*set_platform=*/false,
586 /*merge=*/false);
587 }
588 }
589 }
590
591 if (dyld_idx != UINT32_MAX) {
592 ModuleSP dyld_sp = images[dyld_idx].second;
593 if (dyld_sp.get()) {
594 LLDB_LOGF(log, "Found dyld module: %s",
595 dyld_sp->GetFileSpec().GetPath().c_str());
596 target.GetImages().AppendIfNeeded(new_module: dyld_sp);
597 UpdateImageLoadAddress(module: dyld_sp.get(), info&: images[dyld_idx].first);
598 SetDYLDModule(dyld_sp);
599 }
600 }
601}
602
603bool DynamicLoaderDarwin::UpdateDYLDImageInfoFromNewImageInfo(
604 ImageInfo &image_info) {
605 if (image_info.header.filetype == llvm::MachO::MH_DYLINKER) {
606 const bool can_create = true;
607 ModuleSP dyld_sp =
608 FindTargetModuleForImageInfo(image_info, can_create, did_create_ptr: nullptr);
609 if (dyld_sp.get()) {
610 Target &target = m_process->GetTarget();
611 target.GetImages().AppendIfNeeded(new_module: dyld_sp);
612 UpdateImageLoadAddress(module: dyld_sp.get(), info&: image_info);
613 SetDYLDModule(dyld_sp);
614 return true;
615 }
616 }
617 return false;
618}
619
620std::optional<lldb_private::Address> DynamicLoaderDarwin::GetStartAddress() {
621 Log *log = GetLog(mask: LLDBLog::DynamicLoader);
622
623 auto log_err = [log](llvm::StringLiteral err_msg) -> std::nullopt_t {
624 LLDB_LOGV(log, "{}", err_msg);
625 return std::nullopt;
626 };
627
628 ModuleSP dyld_sp = GetDYLDModule();
629 if (!dyld_sp)
630 return log_err("Couldn't retrieve DYLD module. Cannot get `start` symbol.");
631
632 const Symbol *symbol =
633 dyld_sp->FindFirstSymbolWithNameAndType(name: ConstString("_dyld_start"));
634 if (!symbol)
635 return log_err("Cannot find `start` symbol in DYLD module.");
636
637 return symbol->GetAddress();
638}
639
640void DynamicLoaderDarwin::SetDYLDModule(lldb::ModuleSP &dyld_module_sp) {
641 m_dyld_module_wp = dyld_module_sp;
642}
643
644ModuleSP DynamicLoaderDarwin::GetDYLDModule() {
645 ModuleSP dyld_sp(m_dyld_module_wp.lock());
646 return dyld_sp;
647}
648
649void DynamicLoaderDarwin::ClearDYLDModule() { m_dyld_module_wp.reset(); }
650
651std::vector<std::pair<DynamicLoaderDarwin::ImageInfo, ModuleSP>>
652DynamicLoaderDarwin::PreloadModulesFromImageInfos(
653 const ImageInfo::collection &image_infos) {
654 const auto size = image_infos.size();
655 std::vector<std::pair<DynamicLoaderDarwin::ImageInfo, ModuleSP>> images(size);
656 auto LoadImage = [&](size_t i, ImageInfo::collection::const_iterator it) {
657 const auto &image_info = *it;
658 images[i] = std::make_pair(
659 x: image_info, y: FindTargetModuleForImageInfo(image_info, can_create: true, did_create_ptr: nullptr));
660 };
661 auto it = image_infos.begin();
662 bool is_parallel_load = m_process->GetTarget().GetParallelModuleLoad();
663 if (is_parallel_load) {
664 llvm::ThreadPoolTaskGroup taskGroup(Debugger::GetThreadPool());
665 for (size_t i = 0; i < size; ++i, ++it) {
666 taskGroup.async(F&: LoadImage, ArgList&: i, ArgList&: it);
667 }
668 taskGroup.wait();
669 } else {
670 for (size_t i = 0; i < size; ++i, ++it) {
671 LoadImage(i, it);
672 }
673 }
674 return images;
675}
676
677bool DynamicLoaderDarwin::AddModulesUsingImageInfos(
678 ImageInfo::collection &image_infos) {
679 std::lock_guard<std::recursive_mutex> guard(m_mutex);
680 auto images = PreloadModulesFromImageInfos(image_infos);
681 return AddModulesUsingPreloadedModules(images);
682}
683
684bool DynamicLoaderDarwin::AddModulesUsingPreloadedModules(
685 std::vector<std::pair<ImageInfo, ModuleSP>> &images) {
686 std::lock_guard<std::recursive_mutex> guard(m_mutex);
687 // Now add these images to the main list.
688 ModuleList loaded_module_list;
689 Log *log = GetLog(mask: LLDBLog::DynamicLoader);
690 Target &target = m_process->GetTarget();
691 ModuleList &target_images = target.GetImages();
692
693 for (uint32_t idx = 0; idx < images.size(); ++idx) {
694 auto &image_info = images[idx].first;
695 const auto &image_module_sp = images[idx].second;
696 if (log) {
697 LLDB_LOGF(log, "Adding new image at address=0x%16.16" PRIx64 ".",
698 image_info.address);
699 image_info.PutToLog(log);
700 }
701 m_dyld_image_infos.push_back(x: image_info);
702
703 if (image_module_sp) {
704 ObjectFile *objfile = image_module_sp->GetObjectFile();
705 if (objfile) {
706 SectionList *sections = objfile->GetSectionList();
707 if (sections) {
708 ConstString commpage_dbstr("__commpage");
709 Section *commpage_section =
710 sections->FindSectionByName(section_dstr: commpage_dbstr).get();
711 if (commpage_section) {
712 ModuleSpec module_spec(objfile->GetFileSpec(),
713 image_info.GetArchitecture());
714 module_spec.GetObjectName() = commpage_dbstr;
715 ModuleSP commpage_image_module_sp(
716 target_images.FindFirstModule(module_spec));
717 if (!commpage_image_module_sp) {
718 module_spec.SetObjectOffset(objfile->GetFileOffset() +
719 commpage_section->GetFileOffset());
720 module_spec.SetObjectSize(objfile->GetByteSize());
721 commpage_image_module_sp = target.GetOrCreateModule(module_spec,
722 notify: true /* notify */);
723 if (!commpage_image_module_sp ||
724 commpage_image_module_sp->GetObjectFile() == nullptr) {
725 commpage_image_module_sp = m_process->ReadModuleFromMemory(
726 file_spec: image_info.file_spec, header_addr: image_info.address);
727 // Always load a memory image right away in the target in case
728 // we end up trying to read the symbol table from memory... The
729 // __LINKEDIT will need to be mapped so we can figure out where
730 // the symbol table bits are...
731 bool changed = false;
732 UpdateImageLoadAddress(module: commpage_image_module_sp.get(),
733 info&: image_info);
734 target.GetImages().Append(module_sp: commpage_image_module_sp);
735 if (changed) {
736 image_info.load_stop_id = m_process->GetStopID();
737 loaded_module_list.AppendIfNeeded(new_module: commpage_image_module_sp);
738 }
739 }
740 }
741 }
742 }
743 }
744
745 // UpdateImageLoadAddress will return true if any segments change load
746 // address. We need to check this so we don't mention that all loaded
747 // shared libraries are newly loaded each time we hit out dyld breakpoint
748 // since dyld will list all shared libraries each time.
749 if (UpdateImageLoadAddress(module: image_module_sp.get(), info&: image_info)) {
750 target_images.AppendIfNeeded(new_module: image_module_sp);
751 loaded_module_list.AppendIfNeeded(new_module: image_module_sp);
752 }
753
754 // To support macCatalyst and legacy iOS simulator,
755 // update the module's platform with the DYLD info.
756 ArchSpec dyld_spec = image_info.GetArchitecture();
757 auto &dyld_triple = dyld_spec.GetTriple();
758 if ((dyld_triple.getEnvironment() == llvm::Triple::MacABI &&
759 dyld_triple.getOS() == llvm::Triple::IOS) ||
760 (dyld_triple.getEnvironment() == llvm::Triple::Simulator &&
761 (dyld_triple.getOS() == llvm::Triple::IOS ||
762 dyld_triple.getOS() == llvm::Triple::TvOS ||
763 dyld_triple.getOS() == llvm::Triple::WatchOS ||
764 dyld_triple.getOS() == llvm::Triple::XROS)))
765 image_module_sp->MergeArchitecture(arch_spec: dyld_spec);
766 }
767 }
768
769 if (loaded_module_list.GetSize() > 0) {
770 if (log)
771 loaded_module_list.LogUUIDAndPaths(log,
772 prefix_cstr: "DynamicLoaderDarwin::ModulesDidLoad");
773 m_process->GetTarget().ModulesDidLoad(module_list&: loaded_module_list);
774 }
775 return true;
776}
777
778// On Mac OS X libobjc (the Objective-C runtime) has several critical dispatch
779// functions written in hand-written assembly, and also have hand-written
780// unwind information in the eh_frame section. Normally we prefer analyzing
781// the assembly instructions of a currently executing frame to unwind from that
782// frame -- but on hand-written functions this profiling can fail. We should
783// use the eh_frame instructions for these functions all the time.
784//
785// As an aside, it would be better if the eh_frame entries had a flag (or were
786// extensible so they could have an Apple-specific flag) which indicates that
787// the instructions are asynchronous -- accurate at every instruction, instead
788// of our normal default assumption that they are not.
789
790bool DynamicLoaderDarwin::AlwaysRelyOnEHUnwindInfo(SymbolContext &sym_ctx) {
791 ModuleSP module_sp;
792 if (sym_ctx.symbol) {
793 module_sp = sym_ctx.symbol->GetAddressRef().GetModule();
794 }
795 if (module_sp.get() == nullptr && sym_ctx.function)
796 module_sp = sym_ctx.function->GetAddress().GetModule();
797 if (module_sp.get() == nullptr)
798 return false;
799
800 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(process&: *m_process);
801 return objc_runtime != nullptr &&
802 objc_runtime->IsModuleObjCLibrary(module_sp);
803}
804
805// Dump a Segment to the file handle provided.
806void DynamicLoaderDarwin::Segment::PutToLog(Log *log,
807 lldb::addr_t slide) const {
808 if (log) {
809 if (slide == 0)
810 LLDB_LOGF(log, "\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")",
811 name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize);
812 else
813 LLDB_LOGF(log,
814 "\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64
815 ") slide = 0x%" PRIx64,
816 name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize,
817 slide);
818 }
819}
820
821lldb_private::ArchSpec DynamicLoaderDarwin::ImageInfo::GetArchitecture() const {
822 // Update the module's platform with the DYLD info.
823 lldb_private::ArchSpec arch_spec(lldb_private::eArchTypeMachO, header.cputype,
824 header.cpusubtype);
825 if (os_env == llvm::Triple::MacABI && os_type == llvm::Triple::IOS) {
826 llvm::Triple triple(llvm::Twine(arch_spec.GetArchitectureName()) +
827 "-apple-ios" + min_version_os_sdk + "-macabi");
828 ArchSpec maccatalyst_spec(triple);
829 if (arch_spec.IsCompatibleMatch(rhs: maccatalyst_spec))
830 arch_spec.MergeFrom(other: maccatalyst_spec);
831 }
832 if (os_env == llvm::Triple::Simulator &&
833 (os_type == llvm::Triple::IOS || os_type == llvm::Triple::TvOS ||
834 os_type == llvm::Triple::WatchOS || os_type == llvm::Triple::XROS)) {
835 llvm::Triple triple(llvm::Twine(arch_spec.GetArchitectureName()) +
836 "-apple-" + llvm::Triple::getOSTypeName(Kind: os_type) +
837 min_version_os_sdk + "-simulator");
838 ArchSpec sim_spec(triple);
839 if (arch_spec.IsCompatibleMatch(rhs: sim_spec))
840 arch_spec.MergeFrom(other: sim_spec);
841 }
842 return arch_spec;
843}
844
845const DynamicLoaderDarwin::Segment *
846DynamicLoaderDarwin::ImageInfo::FindSegment(ConstString name) const {
847 const size_t num_segments = segments.size();
848 for (size_t i = 0; i < num_segments; ++i) {
849 if (segments[i].name == name)
850 return &segments[i];
851 }
852 return nullptr;
853}
854
855// Dump an image info structure to the file handle provided.
856void DynamicLoaderDarwin::ImageInfo::PutToLog(Log *log) const {
857 if (!log)
858 return;
859 if (address == LLDB_INVALID_ADDRESS) {
860 LLDB_LOG(log, "uuid={1} path='{2}' (UNLOADED)", uuid.GetAsString(),
861 file_spec.GetPath());
862 } else {
863 LLDB_LOG(log, "address={0:x+16} uuid={1} path='{2}'", address,
864 uuid.GetAsString(), file_spec.GetPath());
865 for (uint32_t i = 0; i < segments.size(); ++i)
866 segments[i].PutToLog(log, slide);
867 }
868}
869
870void DynamicLoaderDarwin::PrivateInitialize(Process *process) {
871 DEBUG_PRINTF("DynamicLoaderDarwin::%s() process state = %s\n", __FUNCTION__,
872 StateAsCString(m_process->GetState()));
873 Clear(clear_process: true);
874 m_process = process;
875}
876
877// Member function that gets called when the process state changes.
878void DynamicLoaderDarwin::PrivateProcessStateChanged(Process *process,
879 StateType state) {
880 DEBUG_PRINTF("DynamicLoaderDarwin::%s(%s)\n", __FUNCTION__,
881 StateAsCString(state));
882 switch (state) {
883 case eStateConnected:
884 case eStateAttaching:
885 case eStateLaunching:
886 case eStateInvalid:
887 case eStateUnloaded:
888 case eStateExited:
889 case eStateDetached:
890 Clear(clear_process: false);
891 break;
892
893 case eStateStopped:
894 // Keep trying find dyld and set our notification breakpoint each time we
895 // stop until we succeed
896 if (!DidSetNotificationBreakpoint() && m_process->IsAlive()) {
897 if (NeedToDoInitialImageFetch())
898 DoInitialImageFetch();
899
900 SetNotificationBreakpoint();
901 }
902 break;
903
904 case eStateRunning:
905 case eStateStepping:
906 case eStateCrashed:
907 case eStateSuspended:
908 break;
909 }
910}
911
912ThreadPlanSP
913DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread,
914 bool stop_others) {
915 ThreadPlanSP thread_plan_sp;
916 StackFrame *current_frame = thread.GetStackFrameAtIndex(idx: 0).get();
917 const SymbolContext &current_context =
918 current_frame->GetSymbolContext(resolve_scope: eSymbolContextSymbol);
919 Symbol *current_symbol = current_context.symbol;
920 Log *log = GetLog(mask: LLDBLog::Step);
921 TargetSP target_sp(thread.CalculateTarget());
922
923 if (current_symbol != nullptr) {
924 std::vector<Address> addresses;
925
926 ConstString current_name =
927 current_symbol->GetMangled().GetName(preference: Mangled::ePreferMangled);
928 if (current_symbol->IsTrampoline()) {
929
930 if (current_name) {
931 const ModuleList &images = target_sp->GetImages();
932
933 SymbolContextList code_symbols;
934 images.FindSymbolsWithNameAndType(name: current_name, symbol_type: eSymbolTypeCode,
935 sc_list&: code_symbols);
936 for (const SymbolContext &context : code_symbols) {
937 Address addr = context.GetFunctionOrSymbolAddress();
938 addresses.push_back(x: addr);
939 if (log) {
940 addr_t load_addr = addr.GetLoadAddress(target: target_sp.get());
941
942 LLDB_LOGF(log, "Found a trampoline target symbol at 0x%" PRIx64 ".",
943 load_addr);
944 }
945 }
946
947 SymbolContextList reexported_symbols;
948 images.FindSymbolsWithNameAndType(name: current_name, symbol_type: eSymbolTypeReExported,
949 sc_list&: reexported_symbols);
950 for (const SymbolContext &context : reexported_symbols) {
951 if (context.symbol) {
952 Symbol *actual_symbol =
953 context.symbol->ResolveReExportedSymbol(target&: *target_sp.get());
954 if (actual_symbol) {
955 const Address actual_symbol_addr = actual_symbol->GetAddress();
956 if (actual_symbol_addr.IsValid()) {
957 addresses.push_back(x: actual_symbol_addr);
958 if (log) {
959 lldb::addr_t load_addr =
960 actual_symbol_addr.GetLoadAddress(target: target_sp.get());
961 LLDB_LOGF(log,
962 "Found a re-exported symbol: %s at 0x%" PRIx64 ".",
963 actual_symbol->GetName().GetCString(), load_addr);
964 }
965 }
966 }
967 }
968 }
969
970 SymbolContextList indirect_symbols;
971 images.FindSymbolsWithNameAndType(name: current_name, symbol_type: eSymbolTypeResolver,
972 sc_list&: indirect_symbols);
973
974 for (const SymbolContext &context : indirect_symbols) {
975 Address addr = context.GetFunctionOrSymbolAddress();
976 addresses.push_back(x: addr);
977 if (log) {
978 addr_t load_addr = addr.GetLoadAddress(target: target_sp.get());
979
980 LLDB_LOGF(log, "Found an indirect target symbol at 0x%" PRIx64 ".",
981 load_addr);
982 }
983 }
984 }
985 } else if (current_symbol->GetType() == eSymbolTypeReExported) {
986 // I am not sure we could ever end up stopped AT a re-exported symbol.
987 // But just in case:
988
989 const Symbol *actual_symbol =
990 current_symbol->ResolveReExportedSymbol(target&: *(target_sp.get()));
991 if (actual_symbol) {
992 Address target_addr(actual_symbol->GetAddress());
993 if (target_addr.IsValid()) {
994 LLDB_LOGF(
995 log,
996 "Found a re-exported symbol: %s pointing to: %s at 0x%" PRIx64
997 ".",
998 current_symbol->GetName().GetCString(),
999 actual_symbol->GetName().GetCString(),
1000 target_addr.GetLoadAddress(target_sp.get()));
1001 addresses.push_back(x: target_addr.GetLoadAddress(target: target_sp.get()));
1002 }
1003 }
1004 }
1005
1006 if (addresses.size() > 0) {
1007 // First check whether any of the addresses point to Indirect symbols,
1008 // and if they do, resolve them:
1009 std::vector<lldb::addr_t> load_addrs;
1010 for (Address address : addresses) {
1011 Symbol *symbol = address.CalculateSymbolContextSymbol();
1012 if (symbol && symbol->IsIndirect()) {
1013 Status error;
1014 Address symbol_address = symbol->GetAddress();
1015 addr_t resolved_addr = thread.GetProcess()->ResolveIndirectFunction(
1016 address: &symbol_address, error);
1017 if (error.Success()) {
1018 load_addrs.push_back(x: resolved_addr);
1019 LLDB_LOGF(log,
1020 "ResolveIndirectFunction found resolved target for "
1021 "%s at 0x%" PRIx64 ".",
1022 symbol->GetName().GetCString(), resolved_addr);
1023 }
1024 } else {
1025 load_addrs.push_back(x: address.GetLoadAddress(target: target_sp.get()));
1026 }
1027 }
1028 thread_plan_sp = std::make_shared<ThreadPlanRunToAddress>(
1029 args&: thread, args&: load_addrs, args&: stop_others);
1030 }
1031 // One more case we have to consider is "branch islands". These are regular
1032 // TEXT symbols but their names end in .island plus maybe a .digit suffix.
1033 // They are to allow arm64 code to branch further than the size of the
1034 // address slot allows. We just need to single-instruction step in that
1035 // case.
1036 static const char *g_branch_island_pattern = "\\.island\\.?[0-9]*$";
1037 static RegularExpression g_branch_island_regex(g_branch_island_pattern);
1038
1039 bool is_branch_island = g_branch_island_regex.Execute(string: current_name);
1040 if (!thread_plan_sp && is_branch_island) {
1041 thread_plan_sp = std::make_shared<ThreadPlanStepInstruction>(
1042 args&: thread,
1043 /* step_over= */ args: false, /* stop_others */ args: false, args: eVoteNoOpinion,
1044 args: eVoteNoOpinion);
1045 LLDB_LOG(log, "Stepping one instruction over branch island: '{0}'.",
1046 current_name);
1047 }
1048 } else {
1049 LLDB_LOGF(log, "Could not find symbol for step through.");
1050 }
1051
1052 return thread_plan_sp;
1053}
1054
1055void DynamicLoaderDarwin::FindEquivalentSymbols(
1056 lldb_private::Symbol *original_symbol, lldb_private::ModuleList &images,
1057 lldb_private::SymbolContextList &equivalent_symbols) {
1058 ConstString trampoline_name =
1059 original_symbol->GetMangled().GetName(preference: Mangled::ePreferMangled);
1060 if (!trampoline_name)
1061 return;
1062
1063 static const char *resolver_name_regex = "(_gc|_non_gc|\\$[A-Za-z0-9\\$]+)$";
1064 std::string equivalent_regex_buf("^");
1065 equivalent_regex_buf.append(s: trampoline_name.GetCString());
1066 equivalent_regex_buf.append(s: resolver_name_regex);
1067
1068 RegularExpression equivalent_name_regex(equivalent_regex_buf);
1069 images.FindSymbolsMatchingRegExAndType(regex: equivalent_name_regex, symbol_type: eSymbolTypeCode,
1070 sc_list&: equivalent_symbols);
1071
1072}
1073
1074lldb::ModuleSP DynamicLoaderDarwin::GetPThreadLibraryModule() {
1075 ModuleSP module_sp = m_libpthread_module_wp.lock();
1076 if (!module_sp) {
1077 SymbolContextList sc_list;
1078 ModuleSpec module_spec;
1079 module_spec.GetFileSpec().SetFilename("libsystem_pthread.dylib");
1080 ModuleList module_list;
1081 m_process->GetTarget().GetImages().FindModules(module_spec, matching_module_list&: module_list);
1082 if (!module_list.IsEmpty()) {
1083 if (module_list.GetSize() == 1) {
1084 module_sp = module_list.GetModuleAtIndex(idx: 0);
1085 if (module_sp)
1086 m_libpthread_module_wp = module_sp;
1087 }
1088 }
1089 }
1090 return module_sp;
1091}
1092
1093Address DynamicLoaderDarwin::GetPthreadSetSpecificAddress() {
1094 if (!m_pthread_getspecific_addr.IsValid()) {
1095 ModuleSP module_sp = GetPThreadLibraryModule();
1096 if (module_sp) {
1097 lldb_private::SymbolContextList sc_list;
1098 module_sp->FindSymbolsWithNameAndType(name: ConstString("pthread_getspecific"),
1099 symbol_type: eSymbolTypeCode, sc_list);
1100 SymbolContext sc;
1101 if (sc_list.GetContextAtIndex(idx: 0, sc)) {
1102 if (sc.symbol)
1103 m_pthread_getspecific_addr = sc.symbol->GetAddress();
1104 }
1105 }
1106 }
1107 return m_pthread_getspecific_addr;
1108}
1109
1110lldb::addr_t
1111DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp,
1112 const lldb::ThreadSP thread_sp,
1113 lldb::addr_t tls_file_addr) {
1114 if (!thread_sp || !module_sp)
1115 return LLDB_INVALID_ADDRESS;
1116
1117 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1118
1119 lldb_private::Address tls_addr;
1120 if (!module_sp->ResolveFileAddress(vm_addr: tls_file_addr, so_addr&: tls_addr))
1121 return LLDB_INVALID_ADDRESS;
1122
1123 Target &target = m_process->GetTarget();
1124 TypeSystemClangSP scratch_ts_sp =
1125 ScratchTypeSystemClang::GetForTarget(target);
1126 if (!scratch_ts_sp)
1127 return LLDB_INVALID_ADDRESS;
1128
1129 CompilerType clang_void_ptr_type =
1130 scratch_ts_sp->GetBasicType(type: eBasicTypeVoid).GetPointerType();
1131
1132 auto evaluate_tls_address = [this, &thread_sp, &clang_void_ptr_type](
1133 Address func_ptr,
1134 llvm::ArrayRef<addr_t> args) -> addr_t {
1135 EvaluateExpressionOptions options;
1136
1137 lldb::ThreadPlanSP thread_plan_sp(new ThreadPlanCallFunction(
1138 *thread_sp, func_ptr, clang_void_ptr_type, args, options));
1139
1140 DiagnosticManager execution_errors;
1141 ExecutionContext exe_ctx(thread_sp);
1142 lldb::ExpressionResults results = m_process->RunThreadPlan(
1143 exe_ctx, thread_plan_sp, options, diagnostic_manager&: execution_errors);
1144
1145 if (results == lldb::eExpressionCompleted) {
1146 if (lldb::ValueObjectSP result_valobj_sp =
1147 thread_plan_sp->GetReturnValueObject()) {
1148 return result_valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
1149 }
1150 }
1151 return LLDB_INVALID_ADDRESS;
1152 };
1153
1154 // On modern apple platforms, there is a small data structure that looks
1155 // approximately like this:
1156 // struct TLS_Thunk {
1157 // void *(*get_addr)(struct TLS_Thunk *);
1158 // size_t key;
1159 // size_t offset;
1160 // }
1161 //
1162 // The strategy is to take get_addr, call it with the address of the
1163 // containing TLS_Thunk structure, and add the offset to the resulting
1164 // pointer to get the data block.
1165 //
1166 // On older apple platforms, the key is treated as a pthread_key_t and passed
1167 // to pthread_getspecific. The pointer returned from that call is added to
1168 // offset to get the relevant data block.
1169
1170 const uint32_t addr_size = m_process->GetAddressByteSize();
1171 uint8_t buf[sizeof(addr_t) * 3];
1172 Status error;
1173 const size_t tls_data_size = addr_size * 3;
1174 const size_t bytes_read = target.ReadMemory(
1175 addr: tls_addr, dst: buf, dst_len: tls_data_size, error, /*force_live_memory = */ true);
1176 if (bytes_read != tls_data_size || error.Fail())
1177 return LLDB_INVALID_ADDRESS;
1178
1179 DataExtractor data(buf, sizeof(buf), m_process->GetByteOrder(), addr_size);
1180 lldb::offset_t offset = 0;
1181 const addr_t tls_thunk = data.GetAddress(offset_ptr: &offset);
1182 const addr_t key = data.GetAddress(offset_ptr: &offset);
1183 const addr_t tls_offset = data.GetAddress(offset_ptr: &offset);
1184
1185 if (tls_thunk != 0) {
1186 const addr_t fixed_tls_thunk = m_process->FixCodeAddress(pc: tls_thunk);
1187 Address thunk_load_addr;
1188 if (target.ResolveLoadAddress(load_addr: fixed_tls_thunk, so_addr&: thunk_load_addr)) {
1189 const addr_t tls_load_addr = tls_addr.GetLoadAddress(target: &target);
1190 const addr_t tls_data = evaluate_tls_address(
1191 thunk_load_addr, llvm::ArrayRef<addr_t>(tls_load_addr));
1192 if (tls_data != LLDB_INVALID_ADDRESS)
1193 return tls_data + tls_offset;
1194 }
1195 }
1196
1197 if (key != 0) {
1198 // First check to see if we have already figured out the location of
1199 // TLS data for the pthread_key on a specific thread yet. If we have we
1200 // can re-use it since its location will not change unless the process
1201 // execs.
1202 const lldb::tid_t tid = thread_sp->GetID();
1203 auto tid_pos = m_tid_to_tls_map.find(x: tid);
1204 if (tid_pos != m_tid_to_tls_map.end()) {
1205 auto tls_pos = tid_pos->second.find(x: key);
1206 if (tls_pos != tid_pos->second.end()) {
1207 return tls_pos->second + tls_offset;
1208 }
1209 }
1210 Address pthread_getspecific_addr = GetPthreadSetSpecificAddress();
1211 if (pthread_getspecific_addr.IsValid()) {
1212 const addr_t tls_data = evaluate_tls_address(pthread_getspecific_addr,
1213 llvm::ArrayRef<addr_t>(key));
1214 if (tls_data != LLDB_INVALID_ADDRESS)
1215 return tls_data + tls_offset;
1216 }
1217 }
1218 return LLDB_INVALID_ADDRESS;
1219}
1220
1221bool DynamicLoaderDarwin::UseDYLDSPI(Process *process) {
1222 Log *log = GetLog(mask: LLDBLog::DynamicLoader);
1223 bool use_new_spi_interface = true;
1224
1225 llvm::VersionTuple version = process->GetHostOSVersion();
1226 if (!version.empty()) {
1227 using namespace llvm;
1228 const Triple::OSType os_type =
1229 process->GetTarget().GetArchitecture().GetTriple().getOS();
1230
1231 auto OlderThan = [os_type, version](llvm::Triple::OSType o,
1232 llvm::VersionTuple v) -> bool {
1233 return os_type == o && version < v;
1234 };
1235
1236 if (OlderThan(Triple::MacOSX, VersionTuple(10, 12)))
1237 use_new_spi_interface = false;
1238
1239 if (OlderThan(Triple::IOS, VersionTuple(10)))
1240 use_new_spi_interface = false;
1241
1242 if (OlderThan(Triple::TvOS, VersionTuple(10)))
1243 use_new_spi_interface = false;
1244
1245 if (OlderThan(Triple::WatchOS, VersionTuple(3)))
1246 use_new_spi_interface = false;
1247
1248 // llvm::Triple::BridgeOS and llvm::Triple::XROS always use the new
1249 // libdyld SPI interface.
1250 } else {
1251 // We could not get an OS version string, we are likely not
1252 // connected to debugserver and the packets to call the libdyld SPI
1253 // will not exist.
1254 use_new_spi_interface = false;
1255 }
1256
1257 // Corefiles cannot use the libdyld SPI to get the inferior's
1258 // binaries, we must find it through metadata or a scan
1259 // of the corefile memory.
1260 if (!process->IsLiveDebugSession())
1261 use_new_spi_interface = false;
1262
1263 if (log) {
1264 if (use_new_spi_interface)
1265 LLDB_LOGF(
1266 log, "DynamicLoaderDarwin::UseDYLDSPI: Use new DynamicLoader plugin");
1267 else
1268 LLDB_LOGF(
1269 log, "DynamicLoaderDarwin::UseDYLDSPI: Use old DynamicLoader plugin");
1270 }
1271 return use_new_spi_interface;
1272}
1273

source code of lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp