blob: a9e674e7ac2455e1983b13cd03faa8090a167b2d [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- DynamicLoaderMacOS.cpp --------------------------------------------===//
Jason Molenda9ab5dc22016-07-21 08:30:55 +00002//
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 Molenda9ab5dc22016-07-21 08:30:55 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Breakpoint/StoppointCallbackContext.h"
10#include "lldb/Core/Debugger.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000011#include "lldb/Core/Module.h"
12#include "lldb/Core/PluginManager.h"
13#include "lldb/Core/Section.h"
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +010014#include "lldb/Symbol/TypeSystemClang.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000015#include "lldb/Symbol/ObjectFile.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000016#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000017#include "lldb/Target/ABI.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include "lldb/Target/StackFrame.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000019#include "lldb/Target/Target.h"
20#include "lldb/Target/Thread.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000021#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000022#include "lldb/Utility/State.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000023
Jason Molenda9ab5dc22016-07-21 08:30:55 +000024#include "DynamicLoaderDarwin.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000025#include "DynamicLoaderMacOS.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000026
27using namespace lldb;
28using namespace lldb_private;
29
Adrian Prantl05097242018-04-30 16:49:04 +000030// Create an instance of this class. This function is filled into the plugin
31// info class that gets handed out by the plugin factory and allows the lldb to
32// instantiate an instance of this class.
Kate Stoneb9c1b512016-09-06 20:57:50 +000033DynamicLoader *DynamicLoaderMacOS::CreateInstance(Process *process,
34 bool force) {
35 bool create = force;
36 if (!create) {
37 create = true;
38 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
39 if (exe_module) {
40 ObjectFile *object_file = exe_module->GetObjectFile();
41 if (object_file) {
42 create = (object_file->GetStrata() == ObjectFile::eStrataUser);
43 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +000044 }
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046 if (create) {
47 const llvm::Triple &triple_ref =
48 process->GetTarget().GetArchitecture().GetTriple();
49 switch (triple_ref.getOS()) {
50 case llvm::Triple::Darwin:
51 case llvm::Triple::MacOSX:
52 case llvm::Triple::IOS:
53 case llvm::Triple::TvOS:
54 case llvm::Triple::WatchOS:
Jason Molenda32762fd2018-10-11 00:28:35 +000055 // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 create = triple_ref.getVendor() == llvm::Triple::Apple;
57 break;
58 default:
Jason Molenda716814a2016-07-22 22:26:26 +000059 create = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 break;
61 }
Jason Molenda716814a2016-07-22 22:26:26 +000062 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 }
64
Jonas Devliegherea6682a42018-12-15 00:15:33 +000065 if (!UseDYLDSPI(process)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 create = false;
67 }
68
69 if (create)
70 return new DynamicLoaderMacOS(process);
Konrad Kleine248a1302019-05-23 11:14:47 +000071 return nullptr;
Jason Molenda9ab5dc22016-07-21 08:30:55 +000072}
73
Jason Molenda9ab5dc22016-07-21 08:30:55 +000074// Constructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000075DynamicLoaderMacOS::DynamicLoaderMacOS(Process *process)
76 : DynamicLoaderDarwin(process), m_image_infos_stop_id(UINT32_MAX),
Jim Ingham29ae6592018-12-07 01:18:40 +000077 m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
78 m_maybe_image_infos_address(LLDB_INVALID_ADDRESS) {}
Jason Molenda9ab5dc22016-07-21 08:30:55 +000079
Jason Molenda9ab5dc22016-07-21 08:30:55 +000080// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000081DynamicLoaderMacOS::~DynamicLoaderMacOS() {
82 if (LLDB_BREAK_ID_IS_VALID(m_break_id))
83 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086bool DynamicLoaderMacOS::ProcessDidExec() {
87 std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
88 bool did_exec = false;
89 if (m_process) {
90 // If we are stopped after an exec, we will have only one thread...
91 if (m_process->GetThreadList().GetSize() == 1) {
Jim Ingham29ae6592018-12-07 01:18:40 +000092 // Maybe we still have an image infos address around? If so see
93 // if that has changed, and if so we have exec'ed.
94 if (m_maybe_image_infos_address != LLDB_INVALID_ADDRESS) {
95 lldb::addr_t image_infos_address = m_process->GetImageInfoAddress();
96 if (image_infos_address != m_maybe_image_infos_address) {
97 // We don't really have to reset this here, since we are going to
98 // call DoInitialImageFetch right away to handle the exec. But in
99 // case anybody looks at it in the meantime, it can't hurt.
100 m_maybe_image_infos_address = image_infos_address;
101 did_exec = true;
102 }
103 }
104
105 if (!did_exec) {
106 // See if we are stopped at '_dyld_start'
107 ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0));
108 if (thread_sp) {
109 lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
110 if (frame_sp) {
111 const Symbol *symbol =
112 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
113 if (symbol) {
Raphael Isemann05cfdb02019-04-26 07:21:36 +0000114 if (symbol->GetName() == "_dyld_start")
Jim Ingham29ae6592018-12-07 01:18:40 +0000115 did_exec = true;
116 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000118 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000120 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 if (did_exec) {
124 m_libpthread_module_wp.reset();
125 m_pthread_getspecific_addr.Clear();
126 }
127 return did_exec;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000128}
129
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000130// Clear out the state of this class.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131void DynamicLoaderMacOS::DoClear() {
132 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 if (LLDB_BREAK_ID_IS_VALID(m_break_id))
135 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 m_break_id = LLDB_INVALID_BREAK_ID;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000138}
139
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000140// Check if we have found DYLD yet
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141bool DynamicLoaderMacOS::DidSetNotificationBreakpoint() {
142 return LLDB_BREAK_ID_IS_VALID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145void DynamicLoaderMacOS::ClearNotificationBreakpoint() {
146 if (LLDB_BREAK_ID_IS_VALID(m_break_id)) {
147 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda777fcec2017-01-21 01:17:36 +0000148 m_break_id = LLDB_INVALID_BREAK_ID;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000150}
151
Adrian Prantl05097242018-04-30 16:49:04 +0000152// Try and figure out where dyld is by first asking the Process if it knows
153// (which currently calls down in the lldb::Process to get the DYLD info
154// (available on SnowLeopard only). If that fails, then check in the default
155// addresses.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156void DynamicLoaderMacOS::DoInitialImageFetch() {
157 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000158
Adrian Prantl05097242018-04-30 16:49:04 +0000159 // Remove any binaries we pre-loaded in the Target before
160 // launching/attaching. If the same binaries are present in the process,
161 // we'll get them from the shared module cache, we won't need to re-load them
162 // from disk.
Jason Molenda848c7be2017-01-19 00:20:29 +0000163 UnloadAllImages();
164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 StructuredData::ObjectSP all_image_info_json_sp(
166 m_process->GetLoadedDynamicLibrariesInfos());
167 ImageInfo::collection image_infos;
168 if (all_image_info_json_sp.get() &&
169 all_image_info_json_sp->GetAsDictionary() &&
170 all_image_info_json_sp->GetAsDictionary()->HasKey("images") &&
171 all_image_info_json_sp->GetAsDictionary()
172 ->GetValueForKey("images")
173 ->GetAsArray()) {
174 if (JSONImageInformationIntoImageInfo(all_image_info_json_sp,
175 image_infos)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000176 LLDB_LOGF(log, "Initial module fetch: Adding %" PRId64 " modules.\n",
177 (uint64_t)image_infos.size());
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 UpdateSpecialBinariesFromNewImageInfos(image_infos);
180 AddModulesUsingImageInfos(image_infos);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000181 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 }
183
184 m_dyld_image_infos_stop_id = m_process->GetStopID();
Jim Ingham29ae6592018-12-07 01:18:40 +0000185 m_maybe_image_infos_address = m_process->GetImageInfoAddress();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000186}
187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188bool DynamicLoaderMacOS::NeedToDoInitialImageFetch() { return true; }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000189
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000190// Static callback function that gets called when our DYLD notification
Adrian Prantl05097242018-04-30 16:49:04 +0000191// breakpoint gets hit. We update all of our image infos and then let our super
192// class DynamicLoader class decide if we should stop or not (based on global
193// preference).
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194bool DynamicLoaderMacOS::NotifyBreakpointHit(void *baton,
195 StoppointCallbackContext *context,
196 lldb::user_id_t break_id,
197 lldb::user_id_t break_loc_id) {
198 // Let the event know that the images have changed
199 // DYLD passes three arguments to the notification breakpoint.
Adrian Prantl05097242018-04-30 16:49:04 +0000200 // Arg1: enum dyld_notify_mode mode - 0 = adding, 1 = removing, 2 = remove
201 // all Arg2: unsigned long icount - Number of shared libraries
202 // added/removed Arg3: uint64_t mach_headers[] - Array of load addresses
203 // of binaries added/removed
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 DynamicLoaderMacOS *dyld_instance = (DynamicLoaderMacOS *)baton;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 ExecutionContext exe_ctx(context->exe_ctx_ref);
208 Process *process = exe_ctx.GetProcessPtr();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 // This is a sanity check just in case this dyld_instance is an old dyld
211 // plugin's breakpoint still lying around.
212 if (process != dyld_instance->m_process)
213 return false;
214
215 if (dyld_instance->m_image_infos_stop_id != UINT32_MAX &&
216 process->GetStopID() < dyld_instance->m_image_infos_stop_id) {
217 return false;
218 }
219
220 const lldb::ABISP &abi = process->GetABI();
221 if (abi) {
222 // Build up the value array to store the three arguments given above, then
223 // get the values from the ABI:
224
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100225 TypeSystemClang *clang_ast_context =
226 TypeSystemClang::GetScratch(process->GetTarget());
Alex Langford30318182019-11-14 13:41:52 -0800227 if (!clang_ast_context)
228 return false;
229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 ValueList argument_values;
231
232 Value mode_value; // enum dyld_notify_mode { dyld_notify_adding=0,
233 // dyld_notify_removing=1, dyld_notify_remove_all=2 };
234 Value count_value; // unsigned long count
235 Value headers_value; // uint64_t machHeaders[] (aka void*)
236
237 CompilerType clang_void_ptr_type =
238 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
239 CompilerType clang_uint32_type =
240 clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
241 lldb::eEncodingUint, 32);
242 CompilerType clang_uint64_type =
243 clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
244 lldb::eEncodingUint, 32);
245
246 mode_value.SetValueType(Value::eValueTypeScalar);
247 mode_value.SetCompilerType(clang_uint32_type);
248
249 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 4) {
250 count_value.SetValueType(Value::eValueTypeScalar);
251 count_value.SetCompilerType(clang_uint32_type);
252 } else {
253 count_value.SetValueType(Value::eValueTypeScalar);
254 count_value.SetCompilerType(clang_uint64_type);
Jason Molenda716814a2016-07-22 22:26:26 +0000255 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 headers_value.SetValueType(Value::eValueTypeScalar);
258 headers_value.SetCompilerType(clang_void_ptr_type);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 argument_values.PushValue(mode_value);
261 argument_values.PushValue(count_value);
262 argument_values.PushValue(headers_value);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) {
265 uint32_t dyld_mode =
266 argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1);
267 if (dyld_mode != static_cast<uint32_t>(-1)) {
268 // Okay the mode was right, now get the number of elements, and the
269 // array of new elements...
270 uint32_t image_infos_count =
271 argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1);
272 if (image_infos_count != static_cast<uint32_t>(-1)) {
273 addr_t header_array =
274 argument_values.GetValueAtIndex(2)->GetScalar().ULongLong(-1);
275 if (header_array != static_cast<uint64_t>(-1)) {
276 std::vector<addr_t> image_load_addresses;
277 for (uint64_t i = 0; i < image_infos_count; i++) {
Zachary Turner97206d52017-05-12 04:51:55 +0000278 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 addr_t addr = process->ReadUnsignedIntegerFromMemory(
280 header_array + (8 * i), 8, LLDB_INVALID_ADDRESS, error);
281 if (addr != LLDB_INVALID_ADDRESS) {
282 image_load_addresses.push_back(addr);
283 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000284 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 if (dyld_mode == 0) {
286 // dyld_notify_adding
287 dyld_instance->AddBinaries(image_load_addresses);
288 } else if (dyld_mode == 1) {
289 // dyld_notify_removing
290 dyld_instance->UnloadImages(image_load_addresses);
291 } else if (dyld_mode == 2) {
292 // dyld_notify_remove_all
293 dyld_instance->UnloadAllImages();
294 }
295 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000296 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000298 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 } else {
300 process->GetTarget().GetDebugger().GetAsyncErrorStream()->Printf(
301 "No ABI plugin located for triple %s -- shared libraries will not be "
302 "registered!\n",
303 process->GetTarget().GetArchitecture().GetTriple().getTriple().c_str());
304 }
305
306 // Return true to stop the target, false to just let the target run
307 return dyld_instance->GetStopWhenImagesChange();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000308}
309
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310void DynamicLoaderMacOS::AddBinaries(
311 const std::vector<lldb::addr_t> &load_addresses) {
312 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
313 ImageInfo::collection image_infos;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000314
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000315 LLDB_LOGF(log, "Adding %" PRId64 " modules.",
316 (uint64_t)load_addresses.size());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 StructuredData::ObjectSP binaries_info_sp =
318 m_process->GetLoadedDynamicLibrariesInfos(load_addresses);
319 if (binaries_info_sp.get() && binaries_info_sp->GetAsDictionary() &&
320 binaries_info_sp->GetAsDictionary()->HasKey("images") &&
321 binaries_info_sp->GetAsDictionary()
322 ->GetValueForKey("images")
323 ->GetAsArray() &&
324 binaries_info_sp->GetAsDictionary()
325 ->GetValueForKey("images")
326 ->GetAsArray()
327 ->GetSize() == load_addresses.size()) {
328 if (JSONImageInformationIntoImageInfo(binaries_info_sp, image_infos)) {
329 UpdateSpecialBinariesFromNewImageInfos(image_infos);
330 AddModulesUsingImageInfos(image_infos);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000331 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 m_dyld_image_infos_stop_id = m_process->GetStopID();
333 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000334}
335
Adrian Prantl05097242018-04-30 16:49:04 +0000336// Dump the _dyld_all_image_infos members and all current image infos that we
337// have parsed to the file handle provided.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338void DynamicLoaderMacOS::PutToLog(Log *log) const {
Konrad Kleine248a1302019-05-23 11:14:47 +0000339 if (log == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340 return;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000341}
342
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343bool DynamicLoaderMacOS::SetNotificationBreakpoint() {
344 if (m_break_id == LLDB_INVALID_BREAK_ID) {
345 ConstString g_symbol_name("_dyld_debugger_notification");
346 const Symbol *symbol = nullptr;
347 ModuleSP dyld_sp(GetDYLDModule());
348 if (dyld_sp) {
349 symbol = dyld_sp->FindFirstSymbolWithNameAndType(g_symbol_name,
350 eSymbolTypeCode);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000351 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 if (symbol &&
353 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
354 addr_t symbol_address =
355 symbol->GetAddressRef().GetOpcodeLoadAddress(&m_process->GetTarget());
356 if (symbol_address != LLDB_INVALID_ADDRESS) {
357 bool internal = true;
358 bool hardware = false;
359 Breakpoint *breakpoint =
360 m_process->GetTarget()
361 .CreateBreakpoint(symbol_address, internal, hardware)
362 .get();
363 breakpoint->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
364 true);
365 breakpoint->SetBreakpointKind("shared-library-event");
366 m_break_id = breakpoint->GetID();
367 }
368 }
369 }
370 return m_break_id != LLDB_INVALID_BREAK_ID;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000371}
372
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000373addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374DynamicLoaderMacOS::GetDyldLockVariableAddressFromModule(Module *module) {
375 SymbolContext sc;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 Target &target = m_process->GetTarget();
Pavel Labathd5d47a32019-08-05 09:21:47 +0000377 if (Symtab *symtab = module->GetSymtab()) {
378 std::vector<uint32_t> match_indexes;
379 ConstString g_symbol_name("_dyld_global_lock_held");
380 uint32_t num_matches = 0;
381 num_matches =
382 symtab->AppendSymbolIndexesWithName(g_symbol_name, match_indexes);
383 if (num_matches == 1) {
384 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[0]);
385 if (symbol &&
386 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
387 return symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000389 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 }
391 return LLDB_INVALID_ADDRESS;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000392}
393
394// Look for this symbol:
395//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396// int __attribute__((visibility("hidden"))) _dyld_global_lock_held =
397// 0;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000398//
399// in libdyld.dylib.
Zachary Turner97206d52017-05-12 04:51:55 +0000400Status DynamicLoaderMacOS::CanLoadImage() {
401 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 addr_t symbol_address = LLDB_INVALID_ADDRESS;
403 Target &target = m_process->GetTarget();
404 const ModuleList &target_modules = target.GetImages();
405 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
406 const size_t num_modules = target_modules.GetSize();
407 ConstString g_libdyld_name("libdyld.dylib");
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 // Find any modules named "libdyld.dylib" and look for the symbol there first
410 for (size_t i = 0; i < num_modules; i++) {
411 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
412 if (module_pointer) {
413 if (module_pointer->GetFileSpec().GetFilename() == g_libdyld_name) {
414 symbol_address = GetDyldLockVariableAddressFromModule(module_pointer);
415 if (symbol_address != LLDB_INVALID_ADDRESS)
416 break;
417 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000418 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 // Search through all modules looking for the symbol in them
422 if (symbol_address == LLDB_INVALID_ADDRESS) {
423 for (size_t i = 0; i < num_modules; i++) {
424 Module *module_pointer =
425 target_modules.GetModulePointerAtIndexUnlocked(i);
426 if (module_pointer) {
427 addr_t symbol_address =
428 GetDyldLockVariableAddressFromModule(module_pointer);
429 if (symbol_address != LLDB_INVALID_ADDRESS)
430 break;
431 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000432 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000434
Adrian Prantl05097242018-04-30 16:49:04 +0000435 // Default assumption is that it is OK to load images. Only say that we
436 // cannot load images if we find the symbol in libdyld and it indicates that
437 // we cannot.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 if (symbol_address != LLDB_INVALID_ADDRESS) {
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000440 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 int lock_held =
442 m_process->ReadUnsignedIntegerFromMemory(symbol_address, 4, 0, error);
443 if (lock_held != 0) {
Jim Inghamabc5d722017-05-06 01:15:47 +0000444 error.SetErrorString("dyld lock held - unsafe to load images.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 }
Jason Molenda37397352016-07-22 00:17:55 +0000446 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000448 // If we were unable to find _dyld_global_lock_held in any modules, or it
449 // is not loaded into memory yet, we may be at process startup (sitting at
450 // _dyld_start) - so we should not allow dlopen calls. But if we found more
451 // than one module then we are clearly past _dyld_start so in that case
452 // we'll default to "it's safe".
Jim Inghamabc5d722017-05-06 01:15:47 +0000453 if (num_modules <= 1)
454 error.SetErrorString("could not find the dyld library or "
455 "the dyld lock symbol");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 }
457 return error;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000458}
459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460bool DynamicLoaderMacOS::GetSharedCacheInformation(
461 lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache,
462 LazyBool &private_shared_cache) {
463 base_address = LLDB_INVALID_ADDRESS;
464 uuid.Clear();
465 using_shared_cache = eLazyBoolCalculate;
466 private_shared_cache = eLazyBoolCalculate;
Jason Molenda13becd42016-07-29 00:18:39 +0000467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 if (m_process) {
469 StructuredData::ObjectSP info = m_process->GetSharedCacheInfo();
470 StructuredData::Dictionary *info_dict = nullptr;
471 if (info.get() && info->GetAsDictionary()) {
472 info_dict = info->GetAsDictionary();
Jason Molenda13becd42016-07-29 00:18:39 +0000473 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474
Adrian Prantl05097242018-04-30 16:49:04 +0000475 // {"shared_cache_base_address":140735683125248,"shared_cache_uuid
476 // ":"DDB8D70C-
477 // C9A2-3561-B2C8-BE48A4F33F96","no_shared_cache":false,"shared_cache_private_cache":false}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478
479 if (info_dict && info_dict->HasKey("shared_cache_uuid") &&
480 info_dict->HasKey("no_shared_cache") &&
481 info_dict->HasKey("shared_cache_base_address")) {
482 base_address = info_dict->GetValueForKey("shared_cache_base_address")
483 ->GetIntegerValue(LLDB_INVALID_ADDRESS);
Benjamin Krameradcd0262020-01-28 20:23:46 +0100484 std::string uuid_str = std::string(
485 info_dict->GetValueForKey("shared_cache_uuid")->GetStringValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 if (!uuid_str.empty())
Pavel Labatha174bcb2018-06-21 15:24:39 +0000487 uuid.SetFromStringRef(uuid_str);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000488 if (!info_dict->GetValueForKey("no_shared_cache")->GetBooleanValue())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 using_shared_cache = eLazyBoolYes;
490 else
491 using_shared_cache = eLazyBoolNo;
492 if (info_dict->GetValueForKey("shared_cache_private_cache")
493 ->GetBooleanValue())
494 private_shared_cache = eLazyBoolYes;
495 else
496 private_shared_cache = eLazyBoolNo;
497
498 return true;
499 }
500 }
501 return false;
Jason Molenda13becd42016-07-29 00:18:39 +0000502}
503
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504void DynamicLoaderMacOS::Initialize() {
505 PluginManager::RegisterPlugin(GetPluginNameStatic(),
506 GetPluginDescriptionStatic(), CreateInstance);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000507}
508
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509void DynamicLoaderMacOS::Terminate() {
510 PluginManager::UnregisterPlugin(CreateInstance);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000511}
512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513lldb_private::ConstString DynamicLoaderMacOS::GetPluginNameStatic() {
514 static ConstString g_name("macos-dyld");
515 return g_name;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000516}
517
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518const char *DynamicLoaderMacOS::GetPluginDescriptionStatic() {
519 return "Dynamic loader plug-in that watches for shared library loads/unloads "
520 "in MacOSX user processes.";
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000521}
522
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000523// PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524lldb_private::ConstString DynamicLoaderMacOS::GetPluginName() {
525 return GetPluginNameStatic();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000526}
527
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528uint32_t DynamicLoaderMacOS::GetPluginVersion() { return 1; }