blob: 339aeaec50f7842ffd5a827eb4f6648f0d15ae78 [file] [log] [blame]
Jason Molenda5fe4d142016-07-17 21:27:32 +00001//===-- DynamicLoaderDarwin.cpp -----------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jason Molenda5fe4d142016-07-17 21:27:32 +00006//
7//===----------------------------------------------------------------------===//
8
Pavel Labath1408bf72016-11-01 16:11:14 +00009#include "DynamicLoaderDarwin.h"
10
Jason Molenda5fe4d142016-07-17 21:27:32 +000011#include "lldb/Breakpoint/StoppointCallbackContext.h"
Jason Molenda5fe4d142016-07-17 21:27:32 +000012#include "lldb/Core/Debugger.h"
Jason Molenda5fe4d142016-07-17 21:27:32 +000013#include "lldb/Core/Module.h"
14#include "lldb/Core/ModuleSpec.h"
15#include "lldb/Core/PluginManager.h"
16#include "lldb/Core/Section.h"
Jason Molenda5fe4d142016-07-17 21:27:32 +000017#include "lldb/Expression/DiagnosticManager.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000018#include "lldb/Host/FileSystem.h"
Jason Molenda5fe4d142016-07-17 21:27:32 +000019#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"
Zachary Turner666cc0b2017-03-04 01:30:05 +000030#include "lldb/Utility/DataBuffer.h"
31#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000032#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000033#include "lldb/Utility/State.h"
Jason Molenda5fe4d142016-07-17 21:27:32 +000034
Jason Molenda5fe4d142016-07-17 21:27:32 +000035//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
36#ifdef ENABLE_DEBUG_PRINTF
37#include <stdio.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000038#define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
Jason Molenda5fe4d142016-07-17 21:27:32 +000039#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
Jonas Devlieghere796ac802019-02-11 23:13:08 +000049#include <memory>
50
Jason Molenda5fe4d142016-07-17 21:27:32 +000051using namespace lldb;
52using namespace lldb_private;
53
Jason Molenda5fe4d142016-07-17 21:27:32 +000054// Constructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000055DynamicLoaderDarwin::DynamicLoaderDarwin(Process *process)
56 : DynamicLoader(process), m_dyld_module_wp(), m_libpthread_module_wp(),
57 m_pthread_getspecific_addr(), m_tid_to_tls_map(), m_dyld_image_infos(),
58 m_dyld_image_infos_stop_id(UINT32_MAX), m_dyld(), m_mutex() {}
Jason Molenda5fe4d142016-07-17 21:27:32 +000059
Jason Molenda5fe4d142016-07-17 21:27:32 +000060// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000061DynamicLoaderDarwin::~DynamicLoaderDarwin() {}
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063/// Called after attaching a process.
64///
65/// Allow DynamicLoader plug-ins to execute some code after
66/// attaching to a process.
Kate Stoneb9c1b512016-09-06 20:57:50 +000067void DynamicLoaderDarwin::DidAttach() {
68 PrivateInitialize(m_process);
69 DoInitialImageFetch();
70 SetNotificationBreakpoint();
Jason Molenda5fe4d142016-07-17 21:27:32 +000071}
72
Jason Molenda5fe4d142016-07-17 21:27:32 +000073/// Called after attaching a process.
74///
75/// Allow DynamicLoader plug-ins to execute some code after
76/// attaching to a process.
Kate Stoneb9c1b512016-09-06 20:57:50 +000077void DynamicLoaderDarwin::DidLaunch() {
78 PrivateInitialize(m_process);
79 DoInitialImageFetch();
80 SetNotificationBreakpoint();
Jason Molenda5fe4d142016-07-17 21:27:32 +000081}
82
Jason Molenda5fe4d142016-07-17 21:27:32 +000083// Clear out the state of this class.
Kate Stoneb9c1b512016-09-06 20:57:50 +000084void DynamicLoaderDarwin::Clear(bool clear_process) {
85 std::lock_guard<std::recursive_mutex> guard(m_mutex);
86 if (clear_process)
Konrad Kleine248a1302019-05-23 11:14:47 +000087 m_process = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 m_dyld_image_infos.clear();
89 m_dyld_image_infos_stop_id = UINT32_MAX;
90 m_dyld.Clear(false);
Jason Molenda5fe4d142016-07-17 21:27:32 +000091}
92
Kate Stoneb9c1b512016-09-06 20:57:50 +000093ModuleSP DynamicLoaderDarwin::FindTargetModuleForImageInfo(
94 ImageInfo &image_info, bool can_create, bool *did_create_ptr) {
95 if (did_create_ptr)
96 *did_create_ptr = false;
Jason Molenda5fe4d142016-07-17 21:27:32 +000097
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 Target &target = m_process->GetTarget();
99 const ModuleList &target_images = target.GetImages();
100 ModuleSpec module_spec(image_info.file_spec);
101 module_spec.GetUUID() = image_info.uuid;
102 ModuleSP module_sp(target_images.FindFirstModule(module_spec));
Jason Molenda5fe4d142016-07-17 21:27:32 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 if (module_sp && !module_spec.GetUUID().IsValid() &&
105 !module_sp->GetUUID().IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000106 // No UUID, we must rely upon the cached module modification time and the
107 // modification time of the file on disk
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 if (module_sp->GetModificationTime() !=
Jonas Devlieghere46376962018-10-31 21:49:27 +0000109 FileSystem::Instance().GetModificationTime(module_sp->GetFileSpec()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 module_sp.reset();
111 }
112
113 if (!module_sp) {
114 if (can_create) {
Jason Molenda1724a172019-04-08 23:03:02 +0000115 // We'll call Target::ModulesDidLoad after all the modules have been
116 // added to the target, don't let it be called for every one.
117 module_sp = target.GetOrCreateModule(module_spec, false /* notify */);
Konrad Kleine248a1302019-05-23 11:14:47 +0000118 if (!module_sp || module_sp->GetObjectFile() == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 module_sp = m_process->ReadModuleFromMemory(image_info.file_spec,
120 image_info.address);
121
122 if (did_create_ptr)
123 *did_create_ptr = (bool)module_sp;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000124 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 }
126 return module_sp;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000127}
128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129void DynamicLoaderDarwin::UnloadImages(
130 const std::vector<lldb::addr_t> &solib_addresses) {
131 std::lock_guard<std::recursive_mutex> guard(m_mutex);
132 if (m_process->GetStopID() == m_dyld_image_infos_stop_id)
133 return;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
136 Target &target = m_process->GetTarget();
137 if (log)
138 log->Printf("Removing %" PRId64 " modules.",
139 (uint64_t)solib_addresses.size());
Jason Molenda5fe4d142016-07-17 21:27:32 +0000140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 ModuleList unloaded_module_list;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 for (addr_t solib_addr : solib_addresses) {
144 Address header;
145 if (header.SetLoadAddress(solib_addr, &target)) {
146 if (header.GetOffset() == 0) {
147 ModuleSP module_to_remove(header.GetModule());
148 if (module_to_remove.get()) {
149 if (log)
150 log->Printf("Removing module at address 0x%" PRIx64, solib_addr);
151 // remove the sections from the Target
152 UnloadSections(module_to_remove);
153 // add this to the list of modules to remove
154 unloaded_module_list.AppendIfNeeded(module_to_remove);
155 // remove the entry from the m_dyld_image_infos
156 ImageInfo::collection::iterator pos, end = m_dyld_image_infos.end();
157 for (pos = m_dyld_image_infos.begin(); pos != end; pos++) {
158 if (solib_addr == (*pos).address) {
159 m_dyld_image_infos.erase(pos);
160 break;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000161 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000163 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000165 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 if (unloaded_module_list.GetSize() > 0) {
169 if (log) {
170 log->PutCString("Unloaded:");
171 unloaded_module_list.LogUUIDAndPaths(
172 log, "DynamicLoaderDarwin::UnloadModules");
Jason Molenda5fe4d142016-07-17 21:27:32 +0000173 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 m_process->GetTarget().GetImages().Remove(unloaded_module_list);
175 m_dyld_image_infos_stop_id = m_process->GetStopID();
176 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000177}
178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179void DynamicLoaderDarwin::UnloadAllImages() {
180 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
181 ModuleList unloaded_modules_list;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 Target &target = m_process->GetTarget();
184 const ModuleList &target_modules = target.GetImages();
185 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 size_t num_modules = target_modules.GetSize();
188 ModuleSP dyld_sp(GetDYLDModule());
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 for (size_t i = 0; i < num_modules; i++) {
191 ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 // Don't remove dyld - else we'll lose our breakpoint notifying us about
Adrian Prantl05097242018-04-30 16:49:04 +0000194 // libraries being re-loaded...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 if (module_sp.get() != nullptr && module_sp.get() != dyld_sp.get()) {
196 UnloadSections(module_sp);
197 unloaded_modules_list.Append(module_sp);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000198 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 if (unloaded_modules_list.GetSize() != 0) {
202 if (log) {
203 log->PutCString("Unloaded:");
204 unloaded_modules_list.LogUUIDAndPaths(
205 log, "DynamicLoaderDarwin::UnloadAllImages");
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000206 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 target.GetImages().Remove(unloaded_modules_list);
208 m_dyld_image_infos.clear();
209 m_dyld_image_infos_stop_id = m_process->GetStopID();
210 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000211}
212
Adrian Prantl05097242018-04-30 16:49:04 +0000213// Update the load addresses for all segments in MODULE using the updated INFO
214// that is passed in.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215bool DynamicLoaderDarwin::UpdateImageLoadAddress(Module *module,
216 ImageInfo &info) {
217 bool changed = false;
218 if (module) {
219 ObjectFile *image_object_file = module->GetObjectFile();
220 if (image_object_file) {
221 SectionList *section_list = image_object_file->GetSectionList();
222 if (section_list) {
223 std::vector<uint32_t> inaccessible_segment_indexes;
Adrian Prantl05097242018-04-30 16:49:04 +0000224 // We now know the slide amount, so go through all sections and update
225 // the load addresses with the correct values.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 const size_t num_segments = info.segments.size();
227 for (size_t i = 0; i < num_segments; ++i) {
Adrian Prantl05097242018-04-30 16:49:04 +0000228 // Only load a segment if it has protections. Things like __PAGEZERO
229 // don't have any protections, and they shouldn't be slid
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 SectionSP section_sp(
231 section_list->FindSectionByName(info.segments[i].name));
Jason Molenda5fe4d142016-07-17 21:27:32 +0000232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 if (info.segments[i].maxprot == 0) {
234 inaccessible_segment_indexes.push_back(i);
235 } else {
236 const addr_t new_section_load_addr =
237 info.segments[i].vmaddr + info.slide;
238 static ConstString g_section_name_LINKEDIT("__LINKEDIT");
Jason Molenda5fe4d142016-07-17 21:27:32 +0000239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 if (section_sp) {
Adrian Prantl05097242018-04-30 16:49:04 +0000241 // __LINKEDIT sections from files in the shared cache can overlap
242 // so check to see what the segment name is and pass "false" so
243 // we don't warn of overlapping "Section" objects, and "true" for
244 // all other sections.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 const bool warn_multiple =
246 section_sp->GetName() != g_section_name_LINKEDIT;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 changed = m_process->GetTarget().SetSectionLoadAddress(
249 section_sp, new_section_load_addr, warn_multiple);
Jason Molenda9073eb42019-01-25 03:01:48 +0000250 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000252 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253
Adrian Prantl05097242018-04-30 16:49:04 +0000254 // If the loaded the file (it changed) and we have segments that are
255 // not readable or writeable, add them to the invalid memory region
256 // cache for the process. This will typically only be the __PAGEZERO
257 // segment in the main executable. We might be able to apply this more
258 // generally to more sections that have no protections in the future,
259 // but for now we are going to just do __PAGEZERO.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 if (changed && !inaccessible_segment_indexes.empty()) {
261 for (uint32_t i = 0; i < inaccessible_segment_indexes.size(); ++i) {
262 const uint32_t seg_idx = inaccessible_segment_indexes[i];
263 SectionSP section_sp(
264 section_list->FindSectionByName(info.segments[seg_idx].name));
265
266 if (section_sp) {
267 static ConstString g_pagezero_section_name("__PAGEZERO");
268 if (g_pagezero_section_name == section_sp->GetName()) {
269 // __PAGEZERO never slides...
270 const lldb::addr_t vmaddr = info.segments[seg_idx].vmaddr;
271 const lldb::addr_t vmsize = info.segments[seg_idx].vmsize;
272 Process::LoadRange pagezero_range(vmaddr, vmsize);
273 m_process->AddInvalidMemoryRegion(pagezero_range);
274 }
275 }
276 }
277 }
278 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000279 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 }
281 // We might have an in memory image that was loaded as soon as it was created
282 if (info.load_stop_id == m_process->GetStopID())
283 changed = true;
284 else if (changed) {
285 // Update the stop ID when this library was updated
286 info.load_stop_id = m_process->GetStopID();
287 }
288 return changed;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000289}
290
Jason Molenda5fe4d142016-07-17 21:27:32 +0000291// Unload the segments in MODULE using the INFO that is passed in.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292bool DynamicLoaderDarwin::UnloadModuleSections(Module *module,
293 ImageInfo &info) {
294 bool changed = false;
295 if (module) {
296 ObjectFile *image_object_file = module->GetObjectFile();
297 if (image_object_file) {
298 SectionList *section_list = image_object_file->GetSectionList();
299 if (section_list) {
300 const size_t num_segments = info.segments.size();
301 for (size_t i = 0; i < num_segments; ++i) {
302 SectionSP section_sp(
303 section_list->FindSectionByName(info.segments[i].name));
304 if (section_sp) {
305 const addr_t old_section_load_addr =
306 info.segments[i].vmaddr + info.slide;
307 if (m_process->GetTarget().SetSectionUnloaded(
308 section_sp, old_section_load_addr))
309 changed = true;
310 } else {
311 Host::SystemLog(Host::eSystemLogWarning,
312 "warning: unable to find and unload segment named "
313 "'%s' in '%s' in macosx dynamic loader plug-in.\n",
314 info.segments[i].name.AsCString("<invalid>"),
315 image_object_file->GetFileSpec().GetPath().c_str());
316 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000317 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000319 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 }
321 return changed;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000322}
323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324// Given a JSON dictionary (from debugserver, most likely) of binary images
Adrian Prantl05097242018-04-30 16:49:04 +0000325// loaded in the inferior process, add the images to the ImageInfo collection.
Jason Molenda5fe4d142016-07-17 21:27:32 +0000326
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327bool DynamicLoaderDarwin::JSONImageInformationIntoImageInfo(
328 StructuredData::ObjectSP image_details,
329 ImageInfo::collection &image_infos) {
330 StructuredData::ObjectSP images_sp =
331 image_details->GetAsDictionary()->GetValueForKey("images");
332 if (images_sp.get() == nullptr)
333 return false;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 image_infos.resize(images_sp->GetAsArray()->GetSize());
Jason Molenda5fe4d142016-07-17 21:27:32 +0000336
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337 for (size_t i = 0; i < image_infos.size(); i++) {
338 StructuredData::ObjectSP image_sp =
339 images_sp->GetAsArray()->GetItemAtIndex(i);
340 if (image_sp.get() == nullptr || image_sp->GetAsDictionary() == nullptr)
341 return false;
342 StructuredData::Dictionary *image = image_sp->GetAsDictionary();
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000343 // clang-format off
344 if (!image->HasKey("load_address") ||
345 !image->HasKey("pathname") ||
346 !image->HasKey("mod_date") ||
347 !image->HasKey("mach_header") ||
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 image->GetValueForKey("mach_header")->GetAsDictionary() == nullptr ||
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000349 !image->HasKey("segments") ||
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 image->GetValueForKey("segments")->GetAsArray() == nullptr ||
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000351 !image->HasKey("uuid")) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 return false;
353 }
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000354 // clang-format on
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 image_infos[i].address =
356 image->GetValueForKey("load_address")->GetAsInteger()->GetValue();
357 image_infos[i].mod_date =
358 image->GetValueForKey("mod_date")->GetAsInteger()->GetValue();
359 image_infos[i].file_spec.SetFile(
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000360 image->GetValueForKey("pathname")->GetAsString()->GetValue(),
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000361 FileSpec::Style::native);
Jason Molenda5fe4d142016-07-17 21:27:32 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 StructuredData::Dictionary *mh =
364 image->GetValueForKey("mach_header")->GetAsDictionary();
365 image_infos[i].header.magic =
366 mh->GetValueForKey("magic")->GetAsInteger()->GetValue();
367 image_infos[i].header.cputype =
368 mh->GetValueForKey("cputype")->GetAsInteger()->GetValue();
369 image_infos[i].header.cpusubtype =
370 mh->GetValueForKey("cpusubtype")->GetAsInteger()->GetValue();
371 image_infos[i].header.filetype =
372 mh->GetValueForKey("filetype")->GetAsInteger()->GetValue();
Jason Molenda5fe4d142016-07-17 21:27:32 +0000373
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374 if (image->HasKey("min_version_os_name")) {
375 std::string os_name = image->GetValueForKey("min_version_os_name")
376 ->GetAsString()
377 ->GetValue();
378 if (os_name == "macosx")
379 image_infos[i].os_type = llvm::Triple::MacOSX;
380 else if (os_name == "ios" || os_name == "iphoneos")
381 image_infos[i].os_type = llvm::Triple::IOS;
382 else if (os_name == "tvos")
383 image_infos[i].os_type = llvm::Triple::TvOS;
384 else if (os_name == "watchos")
385 image_infos[i].os_type = llvm::Triple::WatchOS;
Jason Molenda32762fd2018-10-11 00:28:35 +0000386 // NEED_BRIDGEOS_TRIPLE else if (os_name == "bridgeos")
387 // NEED_BRIDGEOS_TRIPLE image_infos[i].os_type = llvm::Triple::BridgeOS;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 }
389 if (image->HasKey("min_version_os_sdk")) {
390 image_infos[i].min_version_os_sdk =
391 image->GetValueForKey("min_version_os_sdk")
392 ->GetAsString()
393 ->GetValue();
Jason Molenda5fe4d142016-07-17 21:27:32 +0000394 }
395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't
Adrian Prantl05097242018-04-30 16:49:04 +0000397 // currently send them in the reply.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398
399 if (mh->HasKey("flags"))
400 image_infos[i].header.flags =
401 mh->GetValueForKey("flags")->GetAsInteger()->GetValue();
402 else
403 image_infos[i].header.flags = 0;
404
405 if (mh->HasKey("ncmds"))
406 image_infos[i].header.ncmds =
407 mh->GetValueForKey("ncmds")->GetAsInteger()->GetValue();
408 else
409 image_infos[i].header.ncmds = 0;
410
411 if (mh->HasKey("sizeofcmds"))
412 image_infos[i].header.sizeofcmds =
413 mh->GetValueForKey("sizeofcmds")->GetAsInteger()->GetValue();
414 else
415 image_infos[i].header.sizeofcmds = 0;
416
417 StructuredData::Array *segments =
418 image->GetValueForKey("segments")->GetAsArray();
419 uint32_t segcount = segments->GetSize();
420 for (size_t j = 0; j < segcount; j++) {
421 Segment segment;
422 StructuredData::Dictionary *seg =
423 segments->GetItemAtIndex(j)->GetAsDictionary();
Zachary Turner28333212017-05-12 05:49:54 +0000424 segment.name =
425 ConstString(seg->GetValueForKey("name")->GetAsString()->GetValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 segment.vmaddr =
427 seg->GetValueForKey("vmaddr")->GetAsInteger()->GetValue();
428 segment.vmsize =
429 seg->GetValueForKey("vmsize")->GetAsInteger()->GetValue();
430 segment.fileoff =
431 seg->GetValueForKey("fileoff")->GetAsInteger()->GetValue();
432 segment.filesize =
433 seg->GetValueForKey("filesize")->GetAsInteger()->GetValue();
434 segment.maxprot =
435 seg->GetValueForKey("maxprot")->GetAsInteger()->GetValue();
436
437 // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't
Adrian Prantl05097242018-04-30 16:49:04 +0000438 // currently send them in the reply.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439
440 if (seg->HasKey("initprot"))
441 segment.initprot =
442 seg->GetValueForKey("initprot")->GetAsInteger()->GetValue();
443 else
444 segment.initprot = 0;
445
446 if (seg->HasKey("flags"))
447 segment.flags =
448 seg->GetValueForKey("flags")->GetAsInteger()->GetValue();
449 else
450 segment.flags = 0;
451
452 if (seg->HasKey("nsects"))
453 segment.nsects =
454 seg->GetValueForKey("nsects")->GetAsInteger()->GetValue();
455 else
456 segment.nsects = 0;
457
458 image_infos[i].segments.push_back(segment);
459 }
460
Jim Inghamf3ecbfc2019-01-24 22:43:44 +0000461 image_infos[i].uuid.SetFromOptionalStringRef(
Zachary Turner28333212017-05-12 05:49:54 +0000462 image->GetValueForKey("uuid")->GetAsString()->GetValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463
Adrian Prantl05097242018-04-30 16:49:04 +0000464 // All sections listed in the dyld image info structure will all either be
465 // fixed up already, or they will all be off by a single slide amount that
466 // is determined by finding the first segment that is at file offset zero
467 // which also has bytes (a file size that is greater than zero) in the
468 // object file.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469
470 // Determine the slide amount (if any)
471 const size_t num_sections = image_infos[i].segments.size();
472 for (size_t k = 0; k < num_sections; ++k) {
Adrian Prantl05097242018-04-30 16:49:04 +0000473 // Iterate through the object file sections to find the first section
474 // that starts of file offset zero and that has bytes in the file...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475 if ((image_infos[i].segments[k].fileoff == 0 &&
476 image_infos[i].segments[k].filesize > 0) ||
Raphael Isemann05cfdb02019-04-26 07:21:36 +0000477 (image_infos[i].segments[k].name == "__TEXT")) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 image_infos[i].slide =
479 image_infos[i].address - image_infos[i].segments[k].vmaddr;
Adrian Prantl05097242018-04-30 16:49:04 +0000480 // We have found the slide amount, so we can exit this for loop.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481 break;
482 }
483 }
484 }
485
486 return true;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000487}
488
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489void DynamicLoaderDarwin::UpdateSpecialBinariesFromNewImageInfos(
490 ImageInfo::collection &image_infos) {
491 uint32_t exe_idx = UINT32_MAX;
492 uint32_t dyld_idx = UINT32_MAX;
493 Target &target = m_process->GetTarget();
494 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
495 ConstString g_dyld_sim_filename("dyld_sim");
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000496
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 ArchSpec target_arch = target.GetArchitecture();
498 const size_t image_infos_size = image_infos.size();
499 for (size_t i = 0; i < image_infos_size; i++) {
500 if (image_infos[i].header.filetype == llvm::MachO::MH_DYLINKER) {
Jason Molenda32762fd2018-10-11 00:28:35 +0000501 // In a "simulator" process (an x86 process that is
502 // ios/tvos/watchos/bridgeos) we will have two dyld modules --
503 // a "dyld" that we want to keep track of, and a "dyld_sim" which
504 // we don't need to keep track of here. If the target is an x86
505 // system and the OS of the dyld binary is ios/tvos/watchos/bridgeos,
506 // then we are looking at dyld_sym.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000507
Adrian Prantl05097242018-04-30 16:49:04 +0000508 // debugserver has only recently (late 2016) started sending up the os
509 // type for each binary it sees -- so if we don't have an os type, use a
510 // filename check as our next best guess.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 if (image_infos[i].os_type == llvm::Triple::OSType::UnknownOS) {
512 if (image_infos[i].file_spec.GetFilename() != g_dyld_sim_filename) {
513 dyld_idx = i;
514 }
515 } else if (target_arch.GetTriple().getArch() == llvm::Triple::x86 ||
516 target_arch.GetTriple().getArch() == llvm::Triple::x86_64) {
517 if (image_infos[i].os_type != llvm::Triple::OSType::IOS &&
518 image_infos[i].os_type != llvm::Triple::TvOS &&
519 image_infos[i].os_type != llvm::Triple::WatchOS) {
Jason Molenda32762fd2018-10-11 00:28:35 +0000520 // NEED_BRIDGEOS_TRIPLE image_infos[i].os_type != llvm::Triple::BridgeOS) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 dyld_idx = i;
522 }
523 }
Jason Molenda777fcec2017-01-21 01:17:36 +0000524 else {
Adrian Prantl05097242018-04-30 16:49:04 +0000525 // catch-all for any other environment -- trust that dyld is actually
526 // dyld
Jason Molenda777fcec2017-01-21 01:17:36 +0000527 dyld_idx = i;
528 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 } else if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE) {
530 exe_idx = i;
531 }
532 }
533
534 if (exe_idx != UINT32_MAX) {
535 const bool can_create = true;
Konrad Kleine248a1302019-05-23 11:14:47 +0000536 ModuleSP exe_module_sp(FindTargetModuleForImageInfo(image_infos[exe_idx],
537 can_create, nullptr));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538 if (exe_module_sp) {
539 if (log)
540 log->Printf("Found executable module: %s",
541 exe_module_sp->GetFileSpec().GetPath().c_str());
542 target.GetImages().AppendIfNeeded(exe_module_sp);
543 UpdateImageLoadAddress(exe_module_sp.get(), image_infos[exe_idx]);
544 if (exe_module_sp.get() != target.GetExecutableModulePointer()) {
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000545 target.SetExecutableModule(exe_module_sp, eLoadDependentsNo);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000546 }
547 }
548 }
549
550 if (dyld_idx != UINT32_MAX) {
551 const bool can_create = true;
Konrad Kleine248a1302019-05-23 11:14:47 +0000552 ModuleSP dyld_sp = FindTargetModuleForImageInfo(image_infos[dyld_idx],
553 can_create, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 if (dyld_sp.get()) {
555 if (log)
556 log->Printf("Found dyld module: %s",
557 dyld_sp->GetFileSpec().GetPath().c_str());
558 target.GetImages().AppendIfNeeded(dyld_sp);
559 UpdateImageLoadAddress(dyld_sp.get(), image_infos[dyld_idx]);
560 SetDYLDModule(dyld_sp);
561 }
562 }
563}
564
565void DynamicLoaderDarwin::UpdateDYLDImageInfoFromNewImageInfo(
566 ImageInfo &image_info) {
567 if (image_info.header.filetype == llvm::MachO::MH_DYLINKER) {
568 const bool can_create = true;
569 ModuleSP dyld_sp =
Konrad Kleine248a1302019-05-23 11:14:47 +0000570 FindTargetModuleForImageInfo(image_info, can_create, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 if (dyld_sp.get()) {
572 Target &target = m_process->GetTarget();
573 target.GetImages().AppendIfNeeded(dyld_sp);
574 UpdateImageLoadAddress(dyld_sp.get(), image_info);
575 SetDYLDModule(dyld_sp);
576 }
577 }
578}
579
580void DynamicLoaderDarwin::SetDYLDModule(lldb::ModuleSP &dyld_module_sp) {
581 m_dyld_module_wp = dyld_module_sp;
582}
583
584ModuleSP DynamicLoaderDarwin::GetDYLDModule() {
585 ModuleSP dyld_sp(m_dyld_module_wp.lock());
586 return dyld_sp;
587}
588
589bool DynamicLoaderDarwin::AddModulesUsingImageInfos(
590 ImageInfo::collection &image_infos) {
591 std::lock_guard<std::recursive_mutex> guard(m_mutex);
592 // Now add these images to the main list.
593 ModuleList loaded_module_list;
594 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
595 Target &target = m_process->GetTarget();
596 ModuleList &target_images = target.GetImages();
597
598 for (uint32_t idx = 0; idx < image_infos.size(); ++idx) {
599 if (log) {
600 log->Printf("Adding new image at address=0x%16.16" PRIx64 ".",
601 image_infos[idx].address);
602 image_infos[idx].PutToLog(log);
603 }
604
605 m_dyld_image_infos.push_back(image_infos[idx]);
606
607 ModuleSP image_module_sp(
Konrad Kleine248a1302019-05-23 11:14:47 +0000608 FindTargetModuleForImageInfo(image_infos[idx], true, nullptr));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609
610 if (image_module_sp) {
611 ObjectFile *objfile = image_module_sp->GetObjectFile();
612 if (objfile) {
613 SectionList *sections = objfile->GetSectionList();
614 if (sections) {
615 ConstString commpage_dbstr("__commpage");
616 Section *commpage_section =
617 sections->FindSectionByName(commpage_dbstr).get();
618 if (commpage_section) {
619 ModuleSpec module_spec(objfile->GetFileSpec(),
620 image_infos[idx].GetArchitecture());
621 module_spec.GetObjectName() = commpage_dbstr;
622 ModuleSP commpage_image_module_sp(
623 target_images.FindFirstModule(module_spec));
624 if (!commpage_image_module_sp) {
625 module_spec.SetObjectOffset(objfile->GetFileOffset() +
626 commpage_section->GetFileOffset());
627 module_spec.SetObjectSize(objfile->GetByteSize());
Jason Molenda1724a172019-04-08 23:03:02 +0000628 commpage_image_module_sp = target.GetOrCreateModule(module_spec,
629 true /* notify */);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630 if (!commpage_image_module_sp ||
Konrad Kleine248a1302019-05-23 11:14:47 +0000631 commpage_image_module_sp->GetObjectFile() == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632 commpage_image_module_sp = m_process->ReadModuleFromMemory(
633 image_infos[idx].file_spec, image_infos[idx].address);
634 // Always load a memory image right away in the target in case
635 // we end up trying to read the symbol table from memory... The
636 // __LINKEDIT will need to be mapped so we can figure out where
637 // the symbol table bits are...
638 bool changed = false;
639 UpdateImageLoadAddress(commpage_image_module_sp.get(),
640 image_infos[idx]);
641 target.GetImages().Append(commpage_image_module_sp);
642 if (changed) {
643 image_infos[idx].load_stop_id = m_process->GetStopID();
644 loaded_module_list.AppendIfNeeded(commpage_image_module_sp);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000645 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000647 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000649 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000650 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000651
Adrian Prantl05097242018-04-30 16:49:04 +0000652 // UpdateImageLoadAddress will return true if any segments change load
653 // address. We need to check this so we don't mention that all loaded
654 // shared libraries are newly loaded each time we hit out dyld breakpoint
655 // since dyld will list all shared libraries each time.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656 if (UpdateImageLoadAddress(image_module_sp.get(), image_infos[idx])) {
657 target_images.AppendIfNeeded(image_module_sp);
658 loaded_module_list.AppendIfNeeded(image_module_sp);
659 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000660 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000662
Kate Stoneb9c1b512016-09-06 20:57:50 +0000663 if (loaded_module_list.GetSize() > 0) {
664 if (log)
665 loaded_module_list.LogUUIDAndPaths(log,
666 "DynamicLoaderDarwin::ModulesDidLoad");
667 m_process->GetTarget().ModulesDidLoad(loaded_module_list);
668 }
669 return true;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000670}
671
Jason Molenda5fe4d142016-07-17 21:27:32 +0000672// On Mac OS X libobjc (the Objective-C runtime) has several critical dispatch
Adrian Prantl05097242018-04-30 16:49:04 +0000673// functions written in hand-written assembly, and also have hand-written
674// unwind information in the eh_frame section. Normally we prefer analyzing
675// the assembly instructions of a currently executing frame to unwind from that
676// frame -- but on hand-written functions this profiling can fail. We should
677// use the eh_frame instructions for these functions all the time.
Jason Molenda5fe4d142016-07-17 21:27:32 +0000678//
679// As an aside, it would be better if the eh_frame entries had a flag (or were
680// extensible so they could have an Apple-specific flag) which indicates that
681// the instructions are asynchronous -- accurate at every instruction, instead
682// of our normal default assumption that they are not.
Jason Molenda5fe4d142016-07-17 21:27:32 +0000683
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684bool DynamicLoaderDarwin::AlwaysRelyOnEHUnwindInfo(SymbolContext &sym_ctx) {
685 ModuleSP module_sp;
686 if (sym_ctx.symbol) {
687 module_sp = sym_ctx.symbol->GetAddressRef().GetModule();
688 }
Konrad Kleine248a1302019-05-23 11:14:47 +0000689 if (module_sp.get() == nullptr && sym_ctx.function) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 module_sp =
691 sym_ctx.function->GetAddressRange().GetBaseAddress().GetModule();
692 }
Konrad Kleine248a1302019-05-23 11:14:47 +0000693 if (module_sp.get() == nullptr)
Jason Molenda5fe4d142016-07-17 21:27:32 +0000694 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695
696 ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
Konrad Kleine248a1302019-05-23 11:14:47 +0000697 return objc_runtime != nullptr &&
698 objc_runtime->IsModuleObjCLibrary(module_sp);
Jason Molenda5fe4d142016-07-17 21:27:32 +0000699}
700
Jason Molenda5fe4d142016-07-17 21:27:32 +0000701// Dump a Segment to the file handle provided.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702void DynamicLoaderDarwin::Segment::PutToLog(Log *log,
703 lldb::addr_t slide) const {
704 if (log) {
705 if (slide == 0)
706 log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")",
707 name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize);
708 else
709 log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64
710 ") slide = 0x%" PRIx64,
711 name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize,
712 slide);
713 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000714}
715
716const DynamicLoaderDarwin::Segment *
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000717DynamicLoaderDarwin::ImageInfo::FindSegment(ConstString name) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718 const size_t num_segments = segments.size();
719 for (size_t i = 0; i < num_segments; ++i) {
720 if (segments[i].name == name)
721 return &segments[i];
722 }
Konrad Kleine248a1302019-05-23 11:14:47 +0000723 return nullptr;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000724}
725
Jason Molenda5fe4d142016-07-17 21:27:32 +0000726// Dump an image info structure to the file handle provided.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727void DynamicLoaderDarwin::ImageInfo::PutToLog(Log *log) const {
Pavel Labathfbb14282018-06-20 20:13:04 +0000728 if (!log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730 if (address == LLDB_INVALID_ADDRESS) {
Pavel Labathfbb14282018-06-20 20:13:04 +0000731 LLDB_LOG(log, "modtime={0:x+8} uuid={1} path='{2}' (UNLOADED)", mod_date,
732 uuid.GetAsString(), file_spec.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 } else {
Pavel Labathfbb14282018-06-20 20:13:04 +0000734 LLDB_LOG(log, "address={0:x+16} modtime={1:x+8} uuid={2} path='{3}'",
735 address, mod_date, uuid.GetAsString(), file_spec.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 for (uint32_t i = 0; i < segments.size(); ++i)
737 segments[i].PutToLog(log, slide);
738 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000739}
740
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741void DynamicLoaderDarwin::PrivateInitialize(Process *process) {
742 DEBUG_PRINTF("DynamicLoaderDarwin::%s() process state = %s\n", __FUNCTION__,
743 StateAsCString(m_process->GetState()));
744 Clear(true);
745 m_process = process;
746 m_process->GetTarget().ClearAllLoadedSections();
Jason Molenda5fe4d142016-07-17 21:27:32 +0000747}
748
Jason Molenda5fe4d142016-07-17 21:27:32 +0000749// Member function that gets called when the process state changes.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750void DynamicLoaderDarwin::PrivateProcessStateChanged(Process *process,
751 StateType state) {
752 DEBUG_PRINTF("DynamicLoaderDarwin::%s(%s)\n", __FUNCTION__,
753 StateAsCString(state));
754 switch (state) {
755 case eStateConnected:
756 case eStateAttaching:
757 case eStateLaunching:
758 case eStateInvalid:
759 case eStateUnloaded:
760 case eStateExited:
761 case eStateDetached:
762 Clear(false);
763 break;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000764
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765 case eStateStopped:
Adrian Prantl05097242018-04-30 16:49:04 +0000766 // Keep trying find dyld and set our notification breakpoint each time we
767 // stop until we succeed
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 if (!DidSetNotificationBreakpoint() && m_process->IsAlive()) {
769 if (NeedToDoInitialImageFetch())
770 DoInitialImageFetch();
Jason Molenda5fe4d142016-07-17 21:27:32 +0000771
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 SetNotificationBreakpoint();
Jason Molenda5fe4d142016-07-17 21:27:32 +0000773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 break;
775
776 case eStateRunning:
777 case eStateStepping:
778 case eStateCrashed:
779 case eStateSuspended:
780 break;
781 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000782}
783
784ThreadPlanSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread,
786 bool stop_others) {
787 ThreadPlanSP thread_plan_sp;
788 StackFrame *current_frame = thread.GetStackFrameAtIndex(0).get();
789 const SymbolContext &current_context =
790 current_frame->GetSymbolContext(eSymbolContextSymbol);
791 Symbol *current_symbol = current_context.symbol;
792 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
793 TargetSP target_sp(thread.CalculateTarget());
Jason Molenda5fe4d142016-07-17 21:27:32 +0000794
Konrad Kleine248a1302019-05-23 11:14:47 +0000795 if (current_symbol != nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000796 std::vector<Address> addresses;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000797
Kate Stoneb9c1b512016-09-06 20:57:50 +0000798 if (current_symbol->IsTrampoline()) {
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000799 ConstString trampoline_name = current_symbol->GetMangled().GetName(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000800 current_symbol->GetLanguage(), Mangled::ePreferMangled);
Jason Molenda5fe4d142016-07-17 21:27:32 +0000801
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802 if (trampoline_name) {
803 const ModuleList &images = target_sp->GetImages();
804
805 SymbolContextList code_symbols;
806 images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeCode,
807 code_symbols);
808 size_t num_code_symbols = code_symbols.GetSize();
809
810 if (num_code_symbols > 0) {
811 for (uint32_t i = 0; i < num_code_symbols; i++) {
812 SymbolContext context;
813 AddressRange addr_range;
814 if (code_symbols.GetContextAtIndex(i, context)) {
815 context.GetAddressRange(eSymbolContextEverything, 0, false,
816 addr_range);
817 addresses.push_back(addr_range.GetBaseAddress());
818 if (log) {
819 addr_t load_addr =
820 addr_range.GetBaseAddress().GetLoadAddress(target_sp.get());
821
822 log->Printf("Found a trampoline target symbol at 0x%" PRIx64
823 ".",
824 load_addr);
825 }
826 }
827 }
828 }
829
830 SymbolContextList reexported_symbols;
831 images.FindSymbolsWithNameAndType(
832 trampoline_name, eSymbolTypeReExported, reexported_symbols);
833 size_t num_reexported_symbols = reexported_symbols.GetSize();
834 if (num_reexported_symbols > 0) {
835 for (uint32_t i = 0; i < num_reexported_symbols; i++) {
836 SymbolContext context;
837 if (reexported_symbols.GetContextAtIndex(i, context)) {
838 if (context.symbol) {
839 Symbol *actual_symbol =
840 context.symbol->ResolveReExportedSymbol(*target_sp.get());
841 if (actual_symbol) {
842 const Address actual_symbol_addr =
843 actual_symbol->GetAddress();
844 if (actual_symbol_addr.IsValid()) {
845 addresses.push_back(actual_symbol_addr);
846 if (log) {
847 lldb::addr_t load_addr =
848 actual_symbol_addr.GetLoadAddress(target_sp.get());
849 log->Printf(
850 "Found a re-exported symbol: %s at 0x%" PRIx64 ".",
851 actual_symbol->GetName().GetCString(), load_addr);
Jason Molenda5fe4d142016-07-17 21:27:32 +0000852 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000854 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000856 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000857 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000858 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859
860 SymbolContextList indirect_symbols;
861 images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeResolver,
862 indirect_symbols);
863 size_t num_indirect_symbols = indirect_symbols.GetSize();
864 if (num_indirect_symbols > 0) {
865 for (uint32_t i = 0; i < num_indirect_symbols; i++) {
866 SymbolContext context;
867 AddressRange addr_range;
868 if (indirect_symbols.GetContextAtIndex(i, context)) {
869 context.GetAddressRange(eSymbolContextEverything, 0, false,
870 addr_range);
871 addresses.push_back(addr_range.GetBaseAddress());
872 if (log) {
873 addr_t load_addr =
874 addr_range.GetBaseAddress().GetLoadAddress(target_sp.get());
875
876 log->Printf("Found an indirect target symbol at 0x%" PRIx64 ".",
877 load_addr);
878 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000879 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000881 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882 }
883 } else if (current_symbol->GetType() == eSymbolTypeReExported) {
884 // I am not sure we could ever end up stopped AT a re-exported symbol.
885 // But just in case:
886
887 const Symbol *actual_symbol =
888 current_symbol->ResolveReExportedSymbol(*(target_sp.get()));
889 if (actual_symbol) {
890 Address target_addr(actual_symbol->GetAddress());
891 if (target_addr.IsValid()) {
892 if (log)
893 log->Printf(
894 "Found a re-exported symbol: %s pointing to: %s at 0x%" PRIx64
895 ".",
896 current_symbol->GetName().GetCString(),
897 actual_symbol->GetName().GetCString(),
898 target_addr.GetLoadAddress(target_sp.get()));
899 addresses.push_back(target_addr.GetLoadAddress(target_sp.get()));
Jason Molenda5fe4d142016-07-17 21:27:32 +0000900 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000902 }
903
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904 if (addresses.size() > 0) {
Adrian Prantl05097242018-04-30 16:49:04 +0000905 // First check whether any of the addresses point to Indirect symbols,
906 // and if they do, resolve them:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907 std::vector<lldb::addr_t> load_addrs;
908 for (Address address : addresses) {
909 Symbol *symbol = address.CalculateSymbolContextSymbol();
910 if (symbol && symbol->IsIndirect()) {
Zachary Turner97206d52017-05-12 04:51:55 +0000911 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912 Address symbol_address = symbol->GetAddress();
913 addr_t resolved_addr = thread.GetProcess()->ResolveIndirectFunction(
914 &symbol_address, error);
915 if (error.Success()) {
916 load_addrs.push_back(resolved_addr);
917 if (log)
918 log->Printf("ResolveIndirectFunction found resolved target for "
919 "%s at 0x%" PRIx64 ".",
920 symbol->GetName().GetCString(), resolved_addr);
921 }
922 } else {
923 load_addrs.push_back(address.GetLoadAddress(target_sp.get()));
Jason Molenda5fe4d142016-07-17 21:27:32 +0000924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 }
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000926 thread_plan_sp = std::make_shared<ThreadPlanRunToAddress>(
927 thread, load_addrs, stop_others);
Jason Molenda5fe4d142016-07-17 21:27:32 +0000928 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929 } else {
930 if (log)
931 log->Printf("Could not find symbol for step through.");
932 }
933
934 return thread_plan_sp;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000935}
936
Kate Stoneb9c1b512016-09-06 20:57:50 +0000937size_t DynamicLoaderDarwin::FindEquivalentSymbols(
938 lldb_private::Symbol *original_symbol, lldb_private::ModuleList &images,
939 lldb_private::SymbolContextList &equivalent_symbols) {
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000940 ConstString trampoline_name = original_symbol->GetMangled().GetName(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 original_symbol->GetLanguage(), Mangled::ePreferMangled);
942 if (!trampoline_name)
943 return 0;
944
945 size_t initial_size = equivalent_symbols.GetSize();
946
947 static const char *resolver_name_regex = "(_gc|_non_gc|\\$[A-Za-z0-9\\$]+)$";
948 std::string equivalent_regex_buf("^");
949 equivalent_regex_buf.append(trampoline_name.GetCString());
950 equivalent_regex_buf.append(resolver_name_regex);
951
Zachary Turner95eae422016-09-21 16:01:28 +0000952 RegularExpression equivalent_name_regex(equivalent_regex_buf);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 const bool append = true;
954 images.FindSymbolsMatchingRegExAndType(equivalent_name_regex, eSymbolTypeCode,
955 equivalent_symbols, append);
956
957 return equivalent_symbols.GetSize() - initial_size;
958}
959
960lldb::ModuleSP DynamicLoaderDarwin::GetPThreadLibraryModule() {
961 ModuleSP module_sp = m_libpthread_module_wp.lock();
962 if (!module_sp) {
963 SymbolContextList sc_list;
964 ModuleSpec module_spec;
965 module_spec.GetFileSpec().GetFilename().SetCString(
966 "libsystem_pthread.dylib");
967 ModuleList module_list;
968 if (m_process->GetTarget().GetImages().FindModules(module_spec,
969 module_list)) {
970 if (module_list.GetSize() == 1) {
971 module_sp = module_list.GetModuleAtIndex(0);
Jason Molenda5fe4d142016-07-17 21:27:32 +0000972 if (module_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973 m_libpthread_module_wp = module_sp;
974 }
Jason Molenda5fe4d142016-07-17 21:27:32 +0000975 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 }
977 return module_sp;
978}
979
980Address DynamicLoaderDarwin::GetPthreadSetSpecificAddress() {
981 if (!m_pthread_getspecific_addr.IsValid()) {
982 ModuleSP module_sp = GetPThreadLibraryModule();
983 if (module_sp) {
984 lldb_private::SymbolContextList sc_list;
985 module_sp->FindSymbolsWithNameAndType(ConstString("pthread_getspecific"),
986 eSymbolTypeCode, sc_list);
987 SymbolContext sc;
988 if (sc_list.GetContextAtIndex(0, sc)) {
989 if (sc.symbol)
990 m_pthread_getspecific_addr = sc.symbol->GetAddress();
991 }
992 }
993 }
994 return m_pthread_getspecific_addr;
Jason Molenda5fe4d142016-07-17 21:27:32 +0000995}
996
997lldb::addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp,
999 const lldb::ThreadSP thread_sp,
1000 lldb::addr_t tls_file_addr) {
1001 if (!thread_sp || !module_sp)
Jason Molenda5fe4d142016-07-17 21:27:32 +00001002 return LLDB_INVALID_ADDRESS;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001003
1004 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1005
1006 const uint32_t addr_size = m_process->GetAddressByteSize();
1007 uint8_t buf[sizeof(lldb::addr_t) * 3];
1008
1009 lldb_private::Address tls_addr;
1010 if (module_sp->ResolveFileAddress(tls_file_addr, tls_addr)) {
Zachary Turner97206d52017-05-12 04:51:55 +00001011 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 const size_t tsl_data_size = addr_size * 3;
1013 Target &target = m_process->GetTarget();
1014 if (target.ReadMemory(tls_addr, false, buf, tsl_data_size, error) ==
1015 tsl_data_size) {
1016 const ByteOrder byte_order = m_process->GetByteOrder();
1017 DataExtractor data(buf, sizeof(buf), byte_order, addr_size);
1018 lldb::offset_t offset = addr_size; // Skip the first pointer
1019 const lldb::addr_t pthread_key = data.GetAddress(&offset);
1020 const lldb::addr_t tls_offset = data.GetAddress(&offset);
1021 if (pthread_key != 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00001022 // First check to see if we have already figured out the location of
1023 // TLS data for the pthread_key on a specific thread yet. If we have we
1024 // can re-use it since its location will not change unless the process
1025 // execs.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026 const tid_t tid = thread_sp->GetID();
1027 auto tid_pos = m_tid_to_tls_map.find(tid);
1028 if (tid_pos != m_tid_to_tls_map.end()) {
1029 auto tls_pos = tid_pos->second.find(pthread_key);
1030 if (tls_pos != tid_pos->second.end()) {
1031 return tls_pos->second + tls_offset;
1032 }
1033 }
1034 StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(0);
1035 if (frame_sp) {
1036 ClangASTContext *clang_ast_context =
1037 target.GetScratchClangASTContext();
1038
1039 if (!clang_ast_context)
1040 return LLDB_INVALID_ADDRESS;
1041
1042 CompilerType clang_void_ptr_type =
1043 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
1044 Address pthread_getspecific_addr = GetPthreadSetSpecificAddress();
1045 if (pthread_getspecific_addr.IsValid()) {
1046 EvaluateExpressionOptions options;
1047
1048 lldb::ThreadPlanSP thread_plan_sp(new ThreadPlanCallFunction(
1049 *thread_sp, pthread_getspecific_addr, clang_void_ptr_type,
1050 llvm::ArrayRef<lldb::addr_t>(pthread_key), options));
1051
1052 DiagnosticManager execution_errors;
1053 ExecutionContext exe_ctx(thread_sp);
1054 lldb::ExpressionResults results = m_process->RunThreadPlan(
1055 exe_ctx, thread_plan_sp, options, execution_errors);
1056
1057 if (results == lldb::eExpressionCompleted) {
1058 lldb::ValueObjectSP result_valobj_sp =
1059 thread_plan_sp->GetReturnValueObject();
1060 if (result_valobj_sp) {
1061 const lldb::addr_t pthread_key_data =
1062 result_valobj_sp->GetValueAsUnsigned(0);
1063 if (pthread_key_data) {
1064 m_tid_to_tls_map[tid].insert(
1065 std::make_pair(pthread_key, pthread_key_data));
1066 return pthread_key_data + tls_offset;
1067 }
1068 }
1069 }
1070 }
1071 }
1072 }
1073 }
1074 }
1075 return LLDB_INVALID_ADDRESS;
Jason Molenda5fe4d142016-07-17 21:27:32 +00001076}
1077
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078bool DynamicLoaderDarwin::UseDYLDSPI(Process *process) {
1079 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080 bool use_new_spi_interface = false;
Jason Molenda9ab5dc22016-07-21 08:30:55 +00001081
Pavel Labath2272c482018-06-18 15:02:23 +00001082 llvm::VersionTuple version = process->GetHostOSVersion();
1083 if (!version.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 const llvm::Triple::OSType os_type =
1085 process->GetTarget().GetArchitecture().GetTriple().getOS();
Jason Molenda9ab5dc22016-07-21 08:30:55 +00001086
Kate Stoneb9c1b512016-09-06 20:57:50 +00001087 // macOS 10.12 and newer
1088 if (os_type == llvm::Triple::MacOSX &&
Pavel Labath2272c482018-06-18 15:02:23 +00001089 version >= llvm::VersionTuple(10, 12))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 use_new_spi_interface = true;
Jason Molenda9ab5dc22016-07-21 08:30:55 +00001091
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 // iOS 10 and newer
Pavel Labath2272c482018-06-18 15:02:23 +00001093 if (os_type == llvm::Triple::IOS && version >= llvm::VersionTuple(10))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 use_new_spi_interface = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095
1096 // tvOS 10 and newer
Pavel Labath2272c482018-06-18 15:02:23 +00001097 if (os_type == llvm::Triple::TvOS && version >= llvm::VersionTuple(10))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 use_new_spi_interface = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099
1100 // watchOS 3 and newer
Pavel Labath2272c482018-06-18 15:02:23 +00001101 if (os_type == llvm::Triple::WatchOS && version >= llvm::VersionTuple(3))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 use_new_spi_interface = true;
Jason Molenda32762fd2018-10-11 00:28:35 +00001103
1104 // NEED_BRIDGEOS_TRIPLE // Any BridgeOS
1105 // NEED_BRIDGEOS_TRIPLE if (os_type == llvm::Triple::BridgeOS)
1106 // NEED_BRIDGEOS_TRIPLE use_new_spi_interface = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 }
1108
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 if (log) {
1110 if (use_new_spi_interface)
1111 log->Printf(
1112 "DynamicLoaderDarwin::UseDYLDSPI: Use new DynamicLoader plugin");
1113 else
1114 log->Printf(
1115 "DynamicLoaderDarwin::UseDYLDSPI: Use old DynamicLoader plugin");
1116 }
1117 return use_new_spi_interface;
Jason Molenda9ab5dc22016-07-21 08:30:55 +00001118}