blob: 7bc14061ffe05d07c3fc2e503cdb75e95f19404b [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"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000014#include "lldb/Symbol/ObjectFile.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000015#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000016#include "lldb/Target/ABI.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include "lldb/Target/StackFrame.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000018#include "lldb/Target/Target.h"
19#include "lldb/Target/Thread.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000020#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000021#include "lldb/Utility/State.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000022
Jason Molenda9ab5dc22016-07-21 08:30:55 +000023#include "DynamicLoaderDarwin.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "DynamicLoaderMacOS.h"
Jason Molenda9ab5dc22016-07-21 08:30:55 +000025
Alex Langford8be30212020-01-29 11:59:28 -080026#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
27
Jason Molenda9ab5dc22016-07-21 08:30:55 +000028using namespace lldb;
29using namespace lldb_private;
30
Adrian Prantl05097242018-04-30 16:49:04 +000031// Create an instance of this class. This function is filled into the plugin
32// info class that gets handed out by the plugin factory and allows the lldb to
33// instantiate an instance of this class.
Kate Stoneb9c1b512016-09-06 20:57:50 +000034DynamicLoader *DynamicLoaderMacOS::CreateInstance(Process *process,
35 bool force) {
36 bool create = force;
37 if (!create) {
38 create = true;
39 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
40 if (exe_module) {
41 ObjectFile *object_file = exe_module->GetObjectFile();
42 if (object_file) {
43 create = (object_file->GetStrata() == ObjectFile::eStrataUser);
44 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +000045 }
46
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 if (create) {
48 const llvm::Triple &triple_ref =
49 process->GetTarget().GetArchitecture().GetTriple();
50 switch (triple_ref.getOS()) {
51 case llvm::Triple::Darwin:
52 case llvm::Triple::MacOSX:
53 case llvm::Triple::IOS:
54 case llvm::Triple::TvOS:
55 case llvm::Triple::WatchOS:
Jason Molenda32762fd2018-10-11 00:28:35 +000056 // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 create = triple_ref.getVendor() == llvm::Triple::Apple;
58 break;
59 default:
Jason Molenda716814a2016-07-22 22:26:26 +000060 create = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 break;
62 }
Jason Molenda716814a2016-07-22 22:26:26 +000063 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 }
65
Jonas Devliegherea6682a42018-12-15 00:15:33 +000066 if (!UseDYLDSPI(process)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 create = false;
68 }
69
70 if (create)
71 return new DynamicLoaderMacOS(process);
Konrad Kleine248a1302019-05-23 11:14:47 +000072 return nullptr;
Jason Molenda9ab5dc22016-07-21 08:30:55 +000073}
74
Jason Molenda9ab5dc22016-07-21 08:30:55 +000075// Constructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000076DynamicLoaderMacOS::DynamicLoaderMacOS(Process *process)
77 : DynamicLoaderDarwin(process), m_image_infos_stop_id(UINT32_MAX),
Jim Ingham29ae6592018-12-07 01:18:40 +000078 m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
79 m_maybe_image_infos_address(LLDB_INVALID_ADDRESS) {}
Jason Molenda9ab5dc22016-07-21 08:30:55 +000080
Jason Molenda9ab5dc22016-07-21 08:30:55 +000081// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000082DynamicLoaderMacOS::~DynamicLoaderMacOS() {
83 if (LLDB_BREAK_ID_IS_VALID(m_break_id))
84 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +000085}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087bool DynamicLoaderMacOS::ProcessDidExec() {
88 std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
89 bool did_exec = false;
90 if (m_process) {
91 // If we are stopped after an exec, we will have only one thread...
92 if (m_process->GetThreadList().GetSize() == 1) {
Jim Ingham29ae6592018-12-07 01:18:40 +000093 // Maybe we still have an image infos address around? If so see
94 // if that has changed, and if so we have exec'ed.
95 if (m_maybe_image_infos_address != LLDB_INVALID_ADDRESS) {
96 lldb::addr_t image_infos_address = m_process->GetImageInfoAddress();
97 if (image_infos_address != m_maybe_image_infos_address) {
98 // We don't really have to reset this here, since we are going to
99 // call DoInitialImageFetch right away to handle the exec. But in
100 // case anybody looks at it in the meantime, it can't hurt.
101 m_maybe_image_infos_address = image_infos_address;
102 did_exec = true;
103 }
104 }
105
106 if (!did_exec) {
107 // See if we are stopped at '_dyld_start'
108 ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0));
109 if (thread_sp) {
110 lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
111 if (frame_sp) {
112 const Symbol *symbol =
113 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
114 if (symbol) {
Raphael Isemann05cfdb02019-04-26 07:21:36 +0000115 if (symbol->GetName() == "_dyld_start")
Jim Ingham29ae6592018-12-07 01:18:40 +0000116 did_exec = true;
117 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000119 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000121 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 if (did_exec) {
125 m_libpthread_module_wp.reset();
126 m_pthread_getspecific_addr.Clear();
127 }
128 return did_exec;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000129}
130
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000131// Clear out the state of this class.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132void DynamicLoaderMacOS::DoClear() {
133 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 if (LLDB_BREAK_ID_IS_VALID(m_break_id))
136 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 m_break_id = LLDB_INVALID_BREAK_ID;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000139}
140
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000141// Check if we have found DYLD yet
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142bool DynamicLoaderMacOS::DidSetNotificationBreakpoint() {
143 return LLDB_BREAK_ID_IS_VALID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146void DynamicLoaderMacOS::ClearNotificationBreakpoint() {
147 if (LLDB_BREAK_ID_IS_VALID(m_break_id)) {
148 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda777fcec2017-01-21 01:17:36 +0000149 m_break_id = LLDB_INVALID_BREAK_ID;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000151}
152
Adrian Prantl05097242018-04-30 16:49:04 +0000153// Try and figure out where dyld is by first asking the Process if it knows
154// (which currently calls down in the lldb::Process to get the DYLD info
155// (available on SnowLeopard only). If that fails, then check in the default
156// addresses.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157void DynamicLoaderMacOS::DoInitialImageFetch() {
158 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000159
Adrian Prantl05097242018-04-30 16:49:04 +0000160 // Remove any binaries we pre-loaded in the Target before
161 // launching/attaching. If the same binaries are present in the process,
162 // we'll get them from the shared module cache, we won't need to re-load them
163 // from disk.
Jason Molenda848c7be2017-01-19 00:20:29 +0000164 UnloadAllImages();
165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 StructuredData::ObjectSP all_image_info_json_sp(
167 m_process->GetLoadedDynamicLibrariesInfos());
168 ImageInfo::collection image_infos;
169 if (all_image_info_json_sp.get() &&
170 all_image_info_json_sp->GetAsDictionary() &&
171 all_image_info_json_sp->GetAsDictionary()->HasKey("images") &&
172 all_image_info_json_sp->GetAsDictionary()
173 ->GetValueForKey("images")
174 ->GetAsArray()) {
175 if (JSONImageInformationIntoImageInfo(all_image_info_json_sp,
176 image_infos)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000177 LLDB_LOGF(log, "Initial module fetch: Adding %" PRId64 " modules.\n",
178 (uint64_t)image_infos.size());
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 UpdateSpecialBinariesFromNewImageInfos(image_infos);
181 AddModulesUsingImageInfos(image_infos);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000182 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 }
184
185 m_dyld_image_infos_stop_id = m_process->GetStopID();
Jim Ingham29ae6592018-12-07 01:18:40 +0000186 m_maybe_image_infos_address = m_process->GetImageInfoAddress();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000187}
188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189bool DynamicLoaderMacOS::NeedToDoInitialImageFetch() { return true; }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000190
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000191// Static callback function that gets called when our DYLD notification
Adrian Prantl05097242018-04-30 16:49:04 +0000192// breakpoint gets hit. We update all of our image infos and then let our super
193// class DynamicLoader class decide if we should stop or not (based on global
194// preference).
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195bool DynamicLoaderMacOS::NotifyBreakpointHit(void *baton,
196 StoppointCallbackContext *context,
197 lldb::user_id_t break_id,
198 lldb::user_id_t break_loc_id) {
199 // Let the event know that the images have changed
200 // DYLD passes three arguments to the notification breakpoint.
Adrian Prantl05097242018-04-30 16:49:04 +0000201 // Arg1: enum dyld_notify_mode mode - 0 = adding, 1 = removing, 2 = remove
202 // all Arg2: unsigned long icount - Number of shared libraries
203 // added/removed Arg3: uint64_t mach_headers[] - Array of load addresses
204 // of binaries added/removed
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 DynamicLoaderMacOS *dyld_instance = (DynamicLoaderMacOS *)baton;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 ExecutionContext exe_ctx(context->exe_ctx_ref);
209 Process *process = exe_ctx.GetProcessPtr();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 // This is a sanity check just in case this dyld_instance is an old dyld
212 // plugin's breakpoint still lying around.
213 if (process != dyld_instance->m_process)
214 return false;
215
216 if (dyld_instance->m_image_infos_stop_id != UINT32_MAX &&
217 process->GetStopID() < dyld_instance->m_image_infos_stop_id) {
218 return false;
219 }
220
221 const lldb::ABISP &abi = process->GetABI();
222 if (abi) {
223 // Build up the value array to store the three arguments given above, then
224 // get the values from the ABI:
225
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100226 TypeSystemClang *clang_ast_context =
227 TypeSystemClang::GetScratch(process->GetTarget());
Alex Langford30318182019-11-14 13:41:52 -0800228 if (!clang_ast_context)
229 return false;
230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 ValueList argument_values;
232
233 Value mode_value; // enum dyld_notify_mode { dyld_notify_adding=0,
234 // dyld_notify_removing=1, dyld_notify_remove_all=2 };
235 Value count_value; // unsigned long count
236 Value headers_value; // uint64_t machHeaders[] (aka void*)
237
238 CompilerType clang_void_ptr_type =
239 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
240 CompilerType clang_uint32_type =
241 clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
242 lldb::eEncodingUint, 32);
243 CompilerType clang_uint64_type =
244 clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
245 lldb::eEncodingUint, 32);
246
247 mode_value.SetValueType(Value::eValueTypeScalar);
248 mode_value.SetCompilerType(clang_uint32_type);
249
250 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 4) {
251 count_value.SetValueType(Value::eValueTypeScalar);
252 count_value.SetCompilerType(clang_uint32_type);
253 } else {
254 count_value.SetValueType(Value::eValueTypeScalar);
255 count_value.SetCompilerType(clang_uint64_type);
Jason Molenda716814a2016-07-22 22:26:26 +0000256 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 headers_value.SetValueType(Value::eValueTypeScalar);
259 headers_value.SetCompilerType(clang_void_ptr_type);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 argument_values.PushValue(mode_value);
262 argument_values.PushValue(count_value);
263 argument_values.PushValue(headers_value);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) {
266 uint32_t dyld_mode =
267 argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1);
268 if (dyld_mode != static_cast<uint32_t>(-1)) {
269 // Okay the mode was right, now get the number of elements, and the
270 // array of new elements...
271 uint32_t image_infos_count =
272 argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1);
273 if (image_infos_count != static_cast<uint32_t>(-1)) {
274 addr_t header_array =
275 argument_values.GetValueAtIndex(2)->GetScalar().ULongLong(-1);
276 if (header_array != static_cast<uint64_t>(-1)) {
277 std::vector<addr_t> image_load_addresses;
278 for (uint64_t i = 0; i < image_infos_count; i++) {
Zachary Turner97206d52017-05-12 04:51:55 +0000279 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 addr_t addr = process->ReadUnsignedIntegerFromMemory(
281 header_array + (8 * i), 8, LLDB_INVALID_ADDRESS, error);
282 if (addr != LLDB_INVALID_ADDRESS) {
283 image_load_addresses.push_back(addr);
284 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000285 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 if (dyld_mode == 0) {
287 // dyld_notify_adding
288 dyld_instance->AddBinaries(image_load_addresses);
289 } else if (dyld_mode == 1) {
290 // dyld_notify_removing
291 dyld_instance->UnloadImages(image_load_addresses);
292 } else if (dyld_mode == 2) {
293 // dyld_notify_remove_all
294 dyld_instance->UnloadAllImages();
295 }
296 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000297 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000299 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 } else {
301 process->GetTarget().GetDebugger().GetAsyncErrorStream()->Printf(
302 "No ABI plugin located for triple %s -- shared libraries will not be "
303 "registered!\n",
304 process->GetTarget().GetArchitecture().GetTriple().getTriple().c_str());
305 }
306
307 // Return true to stop the target, false to just let the target run
308 return dyld_instance->GetStopWhenImagesChange();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000309}
310
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311void DynamicLoaderMacOS::AddBinaries(
312 const std::vector<lldb::addr_t> &load_addresses) {
313 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
314 ImageInfo::collection image_infos;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000315
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000316 LLDB_LOGF(log, "Adding %" PRId64 " modules.",
317 (uint64_t)load_addresses.size());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 StructuredData::ObjectSP binaries_info_sp =
319 m_process->GetLoadedDynamicLibrariesInfos(load_addresses);
320 if (binaries_info_sp.get() && binaries_info_sp->GetAsDictionary() &&
321 binaries_info_sp->GetAsDictionary()->HasKey("images") &&
322 binaries_info_sp->GetAsDictionary()
323 ->GetValueForKey("images")
324 ->GetAsArray() &&
325 binaries_info_sp->GetAsDictionary()
326 ->GetValueForKey("images")
327 ->GetAsArray()
328 ->GetSize() == load_addresses.size()) {
329 if (JSONImageInformationIntoImageInfo(binaries_info_sp, image_infos)) {
330 UpdateSpecialBinariesFromNewImageInfos(image_infos);
331 AddModulesUsingImageInfos(image_infos);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000332 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 m_dyld_image_infos_stop_id = m_process->GetStopID();
334 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000335}
336
Adrian Prantl05097242018-04-30 16:49:04 +0000337// Dump the _dyld_all_image_infos members and all current image infos that we
338// have parsed to the file handle provided.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339void DynamicLoaderMacOS::PutToLog(Log *log) const {
Konrad Kleine248a1302019-05-23 11:14:47 +0000340 if (log == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 return;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000342}
343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344bool DynamicLoaderMacOS::SetNotificationBreakpoint() {
345 if (m_break_id == LLDB_INVALID_BREAK_ID) {
346 ConstString g_symbol_name("_dyld_debugger_notification");
347 const Symbol *symbol = nullptr;
348 ModuleSP dyld_sp(GetDYLDModule());
349 if (dyld_sp) {
350 symbol = dyld_sp->FindFirstSymbolWithNameAndType(g_symbol_name,
351 eSymbolTypeCode);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000352 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 if (symbol &&
354 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
355 addr_t symbol_address =
356 symbol->GetAddressRef().GetOpcodeLoadAddress(&m_process->GetTarget());
357 if (symbol_address != LLDB_INVALID_ADDRESS) {
358 bool internal = true;
359 bool hardware = false;
360 Breakpoint *breakpoint =
361 m_process->GetTarget()
362 .CreateBreakpoint(symbol_address, internal, hardware)
363 .get();
364 breakpoint->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
365 true);
366 breakpoint->SetBreakpointKind("shared-library-event");
367 m_break_id = breakpoint->GetID();
368 }
369 }
370 }
371 return m_break_id != LLDB_INVALID_BREAK_ID;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000372}
373
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000374addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375DynamicLoaderMacOS::GetDyldLockVariableAddressFromModule(Module *module) {
376 SymbolContext sc;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 Target &target = m_process->GetTarget();
Pavel Labathd5d47a32019-08-05 09:21:47 +0000378 if (Symtab *symtab = module->GetSymtab()) {
379 std::vector<uint32_t> match_indexes;
380 ConstString g_symbol_name("_dyld_global_lock_held");
381 uint32_t num_matches = 0;
382 num_matches =
383 symtab->AppendSymbolIndexesWithName(g_symbol_name, match_indexes);
384 if (num_matches == 1) {
385 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[0]);
386 if (symbol &&
387 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
388 return symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000390 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 }
392 return LLDB_INVALID_ADDRESS;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000393}
394
395// Look for this symbol:
396//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397// int __attribute__((visibility("hidden"))) _dyld_global_lock_held =
398// 0;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000399//
400// in libdyld.dylib.
Zachary Turner97206d52017-05-12 04:51:55 +0000401Status DynamicLoaderMacOS::CanLoadImage() {
402 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 addr_t symbol_address = LLDB_INVALID_ADDRESS;
404 Target &target = m_process->GetTarget();
405 const ModuleList &target_modules = target.GetImages();
406 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
407 const size_t num_modules = target_modules.GetSize();
408 ConstString g_libdyld_name("libdyld.dylib");
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000409
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 // Find any modules named "libdyld.dylib" and look for the symbol there first
411 for (size_t i = 0; i < num_modules; i++) {
412 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
413 if (module_pointer) {
414 if (module_pointer->GetFileSpec().GetFilename() == g_libdyld_name) {
415 symbol_address = GetDyldLockVariableAddressFromModule(module_pointer);
416 if (symbol_address != LLDB_INVALID_ADDRESS)
417 break;
418 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000419 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000421
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 // Search through all modules looking for the symbol in them
423 if (symbol_address == LLDB_INVALID_ADDRESS) {
424 for (size_t i = 0; i < num_modules; i++) {
425 Module *module_pointer =
426 target_modules.GetModulePointerAtIndexUnlocked(i);
427 if (module_pointer) {
428 addr_t symbol_address =
429 GetDyldLockVariableAddressFromModule(module_pointer);
430 if (symbol_address != LLDB_INVALID_ADDRESS)
431 break;
432 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000433 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000435
Adrian Prantl05097242018-04-30 16:49:04 +0000436 // Default assumption is that it is OK to load images. Only say that we
437 // cannot load images if we find the symbol in libdyld and it indicates that
438 // we cannot.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440 if (symbol_address != LLDB_INVALID_ADDRESS) {
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000441 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 int lock_held =
443 m_process->ReadUnsignedIntegerFromMemory(symbol_address, 4, 0, error);
444 if (lock_held != 0) {
Jim Inghamabc5d722017-05-06 01:15:47 +0000445 error.SetErrorString("dyld lock held - unsafe to load images.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 }
Jason Molenda37397352016-07-22 00:17:55 +0000447 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000449 // If we were unable to find _dyld_global_lock_held in any modules, or it
450 // is not loaded into memory yet, we may be at process startup (sitting at
451 // _dyld_start) - so we should not allow dlopen calls. But if we found more
452 // than one module then we are clearly past _dyld_start so in that case
453 // we'll default to "it's safe".
Jim Inghamabc5d722017-05-06 01:15:47 +0000454 if (num_modules <= 1)
455 error.SetErrorString("could not find the dyld library or "
456 "the dyld lock symbol");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 }
458 return error;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000459}
460
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461bool DynamicLoaderMacOS::GetSharedCacheInformation(
462 lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache,
463 LazyBool &private_shared_cache) {
464 base_address = LLDB_INVALID_ADDRESS;
465 uuid.Clear();
466 using_shared_cache = eLazyBoolCalculate;
467 private_shared_cache = eLazyBoolCalculate;
Jason Molenda13becd42016-07-29 00:18:39 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 if (m_process) {
470 StructuredData::ObjectSP info = m_process->GetSharedCacheInfo();
471 StructuredData::Dictionary *info_dict = nullptr;
472 if (info.get() && info->GetAsDictionary()) {
473 info_dict = info->GetAsDictionary();
Jason Molenda13becd42016-07-29 00:18:39 +0000474 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475
Adrian Prantl05097242018-04-30 16:49:04 +0000476 // {"shared_cache_base_address":140735683125248,"shared_cache_uuid
477 // ":"DDB8D70C-
478 // C9A2-3561-B2C8-BE48A4F33F96","no_shared_cache":false,"shared_cache_private_cache":false}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479
480 if (info_dict && info_dict->HasKey("shared_cache_uuid") &&
481 info_dict->HasKey("no_shared_cache") &&
482 info_dict->HasKey("shared_cache_base_address")) {
483 base_address = info_dict->GetValueForKey("shared_cache_base_address")
484 ->GetIntegerValue(LLDB_INVALID_ADDRESS);
Benjamin Krameradcd0262020-01-28 20:23:46 +0100485 std::string uuid_str = std::string(
486 info_dict->GetValueForKey("shared_cache_uuid")->GetStringValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487 if (!uuid_str.empty())
Pavel Labatha174bcb2018-06-21 15:24:39 +0000488 uuid.SetFromStringRef(uuid_str);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000489 if (!info_dict->GetValueForKey("no_shared_cache")->GetBooleanValue())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 using_shared_cache = eLazyBoolYes;
491 else
492 using_shared_cache = eLazyBoolNo;
493 if (info_dict->GetValueForKey("shared_cache_private_cache")
494 ->GetBooleanValue())
495 private_shared_cache = eLazyBoolYes;
496 else
497 private_shared_cache = eLazyBoolNo;
498
499 return true;
500 }
501 }
502 return false;
Jason Molenda13becd42016-07-29 00:18:39 +0000503}
504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505void DynamicLoaderMacOS::Initialize() {
506 PluginManager::RegisterPlugin(GetPluginNameStatic(),
507 GetPluginDescriptionStatic(), CreateInstance);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000508}
509
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510void DynamicLoaderMacOS::Terminate() {
511 PluginManager::UnregisterPlugin(CreateInstance);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000512}
513
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514lldb_private::ConstString DynamicLoaderMacOS::GetPluginNameStatic() {
515 static ConstString g_name("macos-dyld");
516 return g_name;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000517}
518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519const char *DynamicLoaderMacOS::GetPluginDescriptionStatic() {
520 return "Dynamic loader plug-in that watches for shared library loads/unloads "
521 "in MacOSX user processes.";
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000522}
523
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000524// PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525lldb_private::ConstString DynamicLoaderMacOS::GetPluginName() {
526 return GetPluginNameStatic();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000527}
528
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529uint32_t DynamicLoaderMacOS::GetPluginVersion() { return 1; }