blob: 27ccb89f4bea9c85a6783a81d7b16d9376e0735a [file] [log] [blame]
Jason Molenda9ab5dc22016-07-21 08:30:55 +00001//===-- DynamicLoaderMacOS.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 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/ClangASTContext.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
Jason Molenda9ab5dc22016-07-21 08:30:55 +000030//----------------------------------------------------------------------
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.
Jason Molenda9ab5dc22016-07-21 08:30:55 +000034//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000035DynamicLoader *DynamicLoaderMacOS::CreateInstance(Process *process,
36 bool force) {
37 bool create = force;
38 if (!create) {
39 create = true;
40 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
41 if (exe_module) {
42 ObjectFile *object_file = exe_module->GetObjectFile();
43 if (object_file) {
44 create = (object_file->GetStrata() == ObjectFile::eStrataUser);
45 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +000046 }
47
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 if (create) {
49 const llvm::Triple &triple_ref =
50 process->GetTarget().GetArchitecture().GetTriple();
51 switch (triple_ref.getOS()) {
52 case llvm::Triple::Darwin:
53 case llvm::Triple::MacOSX:
54 case llvm::Triple::IOS:
55 case llvm::Triple::TvOS:
56 case llvm::Triple::WatchOS:
Jason Molenda32762fd2018-10-11 00:28:35 +000057 // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 create = triple_ref.getVendor() == llvm::Triple::Apple;
59 break;
60 default:
Jason Molenda716814a2016-07-22 22:26:26 +000061 create = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 break;
63 }
Jason Molenda716814a2016-07-22 22:26:26 +000064 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 }
66
Jonas Devliegherea6682a42018-12-15 00:15:33 +000067 if (!UseDYLDSPI(process)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 create = false;
69 }
70
71 if (create)
72 return new DynamicLoaderMacOS(process);
73 return NULL;
Jason Molenda9ab5dc22016-07-21 08:30:55 +000074}
75
76//----------------------------------------------------------------------
77// Constructor
78//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000079DynamicLoaderMacOS::DynamicLoaderMacOS(Process *process)
80 : DynamicLoaderDarwin(process), m_image_infos_stop_id(UINT32_MAX),
Jim Ingham29ae6592018-12-07 01:18:40 +000081 m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
82 m_maybe_image_infos_address(LLDB_INVALID_ADDRESS) {}
Jason Molenda9ab5dc22016-07-21 08:30:55 +000083
84//----------------------------------------------------------------------
85// Destructor
86//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000087DynamicLoaderMacOS::~DynamicLoaderMacOS() {
88 if (LLDB_BREAK_ID_IS_VALID(m_break_id))
89 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +000090}
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092bool DynamicLoaderMacOS::ProcessDidExec() {
93 std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
94 bool did_exec = false;
95 if (m_process) {
96 // If we are stopped after an exec, we will have only one thread...
97 if (m_process->GetThreadList().GetSize() == 1) {
Jim Ingham29ae6592018-12-07 01:18:40 +000098 // Maybe we still have an image infos address around? If so see
99 // if that has changed, and if so we have exec'ed.
100 if (m_maybe_image_infos_address != LLDB_INVALID_ADDRESS) {
101 lldb::addr_t image_infos_address = m_process->GetImageInfoAddress();
102 if (image_infos_address != m_maybe_image_infos_address) {
103 // We don't really have to reset this here, since we are going to
104 // call DoInitialImageFetch right away to handle the exec. But in
105 // case anybody looks at it in the meantime, it can't hurt.
106 m_maybe_image_infos_address = image_infos_address;
107 did_exec = true;
108 }
109 }
110
111 if (!did_exec) {
112 // See if we are stopped at '_dyld_start'
113 ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0));
114 if (thread_sp) {
115 lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
116 if (frame_sp) {
117 const Symbol *symbol =
118 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
119 if (symbol) {
120 if (symbol->GetName() == ConstString("_dyld_start"))
121 did_exec = true;
122 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000124 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000126 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 if (did_exec) {
130 m_libpthread_module_wp.reset();
131 m_pthread_getspecific_addr.Clear();
132 }
133 return did_exec;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000134}
135
136//----------------------------------------------------------------------
137// Clear out the state of this class.
138//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139void DynamicLoaderMacOS::DoClear() {
140 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 if (LLDB_BREAK_ID_IS_VALID(m_break_id))
143 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 m_break_id = LLDB_INVALID_BREAK_ID;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000146}
147
148//----------------------------------------------------------------------
149// Check if we have found DYLD yet
150//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151bool DynamicLoaderMacOS::DidSetNotificationBreakpoint() {
152 return LLDB_BREAK_ID_IS_VALID(m_break_id);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000153}
154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155void DynamicLoaderMacOS::ClearNotificationBreakpoint() {
156 if (LLDB_BREAK_ID_IS_VALID(m_break_id)) {
157 m_process->GetTarget().RemoveBreakpointByID(m_break_id);
Jason Molenda777fcec2017-01-21 01:17:36 +0000158 m_break_id = LLDB_INVALID_BREAK_ID;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000160}
161
162//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000163// Try and figure out where dyld is by first asking the Process if it knows
164// (which currently calls down in the lldb::Process to get the DYLD info
165// (available on SnowLeopard only). If that fails, then check in the default
166// addresses.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000167//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168void DynamicLoaderMacOS::DoInitialImageFetch() {
169 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000170
Adrian Prantl05097242018-04-30 16:49:04 +0000171 // Remove any binaries we pre-loaded in the Target before
172 // launching/attaching. If the same binaries are present in the process,
173 // we'll get them from the shared module cache, we won't need to re-load them
174 // from disk.
Jason Molenda848c7be2017-01-19 00:20:29 +0000175 UnloadAllImages();
176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 StructuredData::ObjectSP all_image_info_json_sp(
178 m_process->GetLoadedDynamicLibrariesInfos());
179 ImageInfo::collection image_infos;
180 if (all_image_info_json_sp.get() &&
181 all_image_info_json_sp->GetAsDictionary() &&
182 all_image_info_json_sp->GetAsDictionary()->HasKey("images") &&
183 all_image_info_json_sp->GetAsDictionary()
184 ->GetValueForKey("images")
185 ->GetAsArray()) {
186 if (JSONImageInformationIntoImageInfo(all_image_info_json_sp,
187 image_infos)) {
188 if (log)
189 log->Printf("Initial module fetch: Adding %" PRId64 " modules.\n",
190 (uint64_t)image_infos.size());
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 UpdateSpecialBinariesFromNewImageInfos(image_infos);
193 AddModulesUsingImageInfos(image_infos);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000194 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 }
196
197 m_dyld_image_infos_stop_id = m_process->GetStopID();
Jim Ingham29ae6592018-12-07 01:18:40 +0000198 m_maybe_image_infos_address = m_process->GetImageInfoAddress();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000199}
200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201bool DynamicLoaderMacOS::NeedToDoInitialImageFetch() { return true; }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000202
203//----------------------------------------------------------------------
204// Static callback function that gets called when our DYLD notification
Adrian Prantl05097242018-04-30 16:49:04 +0000205// breakpoint gets hit. We update all of our image infos and then let our super
206// class DynamicLoader class decide if we should stop or not (based on global
207// preference).
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000208//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209bool DynamicLoaderMacOS::NotifyBreakpointHit(void *baton,
210 StoppointCallbackContext *context,
211 lldb::user_id_t break_id,
212 lldb::user_id_t break_loc_id) {
213 // Let the event know that the images have changed
214 // DYLD passes three arguments to the notification breakpoint.
Adrian Prantl05097242018-04-30 16:49:04 +0000215 // Arg1: enum dyld_notify_mode mode - 0 = adding, 1 = removing, 2 = remove
216 // all Arg2: unsigned long icount - Number of shared libraries
217 // added/removed Arg3: uint64_t mach_headers[] - Array of load addresses
218 // of binaries added/removed
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 DynamicLoaderMacOS *dyld_instance = (DynamicLoaderMacOS *)baton;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 ExecutionContext exe_ctx(context->exe_ctx_ref);
223 Process *process = exe_ctx.GetProcessPtr();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 // This is a sanity check just in case this dyld_instance is an old dyld
226 // plugin's breakpoint still lying around.
227 if (process != dyld_instance->m_process)
228 return false;
229
230 if (dyld_instance->m_image_infos_stop_id != UINT32_MAX &&
231 process->GetStopID() < dyld_instance->m_image_infos_stop_id) {
232 return false;
233 }
234
235 const lldb::ABISP &abi = process->GetABI();
236 if (abi) {
237 // Build up the value array to store the three arguments given above, then
238 // get the values from the ABI:
239
240 ClangASTContext *clang_ast_context =
241 process->GetTarget().GetScratchClangASTContext();
242 ValueList argument_values;
243
244 Value mode_value; // enum dyld_notify_mode { dyld_notify_adding=0,
245 // dyld_notify_removing=1, dyld_notify_remove_all=2 };
246 Value count_value; // unsigned long count
247 Value headers_value; // uint64_t machHeaders[] (aka void*)
248
249 CompilerType clang_void_ptr_type =
250 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
251 CompilerType clang_uint32_type =
252 clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
253 lldb::eEncodingUint, 32);
254 CompilerType clang_uint64_type =
255 clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
256 lldb::eEncodingUint, 32);
257
258 mode_value.SetValueType(Value::eValueTypeScalar);
259 mode_value.SetCompilerType(clang_uint32_type);
260
261 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 4) {
262 count_value.SetValueType(Value::eValueTypeScalar);
263 count_value.SetCompilerType(clang_uint32_type);
264 } else {
265 count_value.SetValueType(Value::eValueTypeScalar);
266 count_value.SetCompilerType(clang_uint64_type);
Jason Molenda716814a2016-07-22 22:26:26 +0000267 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 headers_value.SetValueType(Value::eValueTypeScalar);
270 headers_value.SetCompilerType(clang_void_ptr_type);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 argument_values.PushValue(mode_value);
273 argument_values.PushValue(count_value);
274 argument_values.PushValue(headers_value);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) {
277 uint32_t dyld_mode =
278 argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1);
279 if (dyld_mode != static_cast<uint32_t>(-1)) {
280 // Okay the mode was right, now get the number of elements, and the
281 // array of new elements...
282 uint32_t image_infos_count =
283 argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1);
284 if (image_infos_count != static_cast<uint32_t>(-1)) {
285 addr_t header_array =
286 argument_values.GetValueAtIndex(2)->GetScalar().ULongLong(-1);
287 if (header_array != static_cast<uint64_t>(-1)) {
288 std::vector<addr_t> image_load_addresses;
289 for (uint64_t i = 0; i < image_infos_count; i++) {
Zachary Turner97206d52017-05-12 04:51:55 +0000290 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 addr_t addr = process->ReadUnsignedIntegerFromMemory(
292 header_array + (8 * i), 8, LLDB_INVALID_ADDRESS, error);
293 if (addr != LLDB_INVALID_ADDRESS) {
294 image_load_addresses.push_back(addr);
295 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000296 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 if (dyld_mode == 0) {
298 // dyld_notify_adding
299 dyld_instance->AddBinaries(image_load_addresses);
300 } else if (dyld_mode == 1) {
301 // dyld_notify_removing
302 dyld_instance->UnloadImages(image_load_addresses);
303 } else if (dyld_mode == 2) {
304 // dyld_notify_remove_all
305 dyld_instance->UnloadAllImages();
306 }
307 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000308 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000310 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311 } else {
312 process->GetTarget().GetDebugger().GetAsyncErrorStream()->Printf(
313 "No ABI plugin located for triple %s -- shared libraries will not be "
314 "registered!\n",
315 process->GetTarget().GetArchitecture().GetTriple().getTriple().c_str());
316 }
317
318 // Return true to stop the target, false to just let the target run
319 return dyld_instance->GetStopWhenImagesChange();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000320}
321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322void DynamicLoaderMacOS::AddBinaries(
323 const std::vector<lldb::addr_t> &load_addresses) {
324 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
325 ImageInfo::collection image_infos;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000326
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 if (log)
328 log->Printf("Adding %" PRId64 " modules.", (uint64_t)load_addresses.size());
329 StructuredData::ObjectSP binaries_info_sp =
330 m_process->GetLoadedDynamicLibrariesInfos(load_addresses);
331 if (binaries_info_sp.get() && binaries_info_sp->GetAsDictionary() &&
332 binaries_info_sp->GetAsDictionary()->HasKey("images") &&
333 binaries_info_sp->GetAsDictionary()
334 ->GetValueForKey("images")
335 ->GetAsArray() &&
336 binaries_info_sp->GetAsDictionary()
337 ->GetValueForKey("images")
338 ->GetAsArray()
339 ->GetSize() == load_addresses.size()) {
340 if (JSONImageInformationIntoImageInfo(binaries_info_sp, image_infos)) {
341 UpdateSpecialBinariesFromNewImageInfos(image_infos);
342 AddModulesUsingImageInfos(image_infos);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000343 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 m_dyld_image_infos_stop_id = m_process->GetStopID();
345 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000346}
347
Adrian Prantl05097242018-04-30 16:49:04 +0000348// Dump the _dyld_all_image_infos members and all current image infos that we
349// have parsed to the file handle provided.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000350//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351void DynamicLoaderMacOS::PutToLog(Log *log) const {
352 if (log == NULL)
353 return;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000354}
355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356bool DynamicLoaderMacOS::SetNotificationBreakpoint() {
357 if (m_break_id == LLDB_INVALID_BREAK_ID) {
358 ConstString g_symbol_name("_dyld_debugger_notification");
359 const Symbol *symbol = nullptr;
360 ModuleSP dyld_sp(GetDYLDModule());
361 if (dyld_sp) {
362 symbol = dyld_sp->FindFirstSymbolWithNameAndType(g_symbol_name,
363 eSymbolTypeCode);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000364 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 if (symbol &&
366 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
367 addr_t symbol_address =
368 symbol->GetAddressRef().GetOpcodeLoadAddress(&m_process->GetTarget());
369 if (symbol_address != LLDB_INVALID_ADDRESS) {
370 bool internal = true;
371 bool hardware = false;
372 Breakpoint *breakpoint =
373 m_process->GetTarget()
374 .CreateBreakpoint(symbol_address, internal, hardware)
375 .get();
376 breakpoint->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
377 true);
378 breakpoint->SetBreakpointKind("shared-library-event");
379 m_break_id = breakpoint->GetID();
380 }
381 }
382 }
383 return m_break_id != LLDB_INVALID_BREAK_ID;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000384}
385
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000386addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387DynamicLoaderMacOS::GetDyldLockVariableAddressFromModule(Module *module) {
388 SymbolContext sc;
389 SymbolVendor *sym_vendor = module->GetSymbolVendor();
390 Target &target = m_process->GetTarget();
391 if (sym_vendor) {
392 Symtab *symtab = sym_vendor->GetSymtab();
393 if (symtab) {
394 std::vector<uint32_t> match_indexes;
395 ConstString g_symbol_name("_dyld_global_lock_held");
396 uint32_t num_matches = 0;
397 num_matches =
398 symtab->AppendSymbolIndexesWithName(g_symbol_name, match_indexes);
399 if (num_matches == 1) {
400 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[0]);
401 if (symbol &&
402 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
403 return symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000404 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000406 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 }
408 return LLDB_INVALID_ADDRESS;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000409}
410
411// Look for this symbol:
412//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413// int __attribute__((visibility("hidden"))) _dyld_global_lock_held =
414// 0;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000415//
416// in libdyld.dylib.
Zachary Turner97206d52017-05-12 04:51:55 +0000417Status DynamicLoaderMacOS::CanLoadImage() {
418 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 addr_t symbol_address = LLDB_INVALID_ADDRESS;
420 Target &target = m_process->GetTarget();
421 const ModuleList &target_modules = target.GetImages();
422 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
423 const size_t num_modules = target_modules.GetSize();
424 ConstString g_libdyld_name("libdyld.dylib");
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 // Find any modules named "libdyld.dylib" and look for the symbol there first
427 for (size_t i = 0; i < num_modules; i++) {
428 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
429 if (module_pointer) {
430 if (module_pointer->GetFileSpec().GetFilename() == g_libdyld_name) {
431 symbol_address = GetDyldLockVariableAddressFromModule(module_pointer);
432 if (symbol_address != LLDB_INVALID_ADDRESS)
433 break;
434 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000435 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 // Search through all modules looking for the symbol in them
439 if (symbol_address == LLDB_INVALID_ADDRESS) {
440 for (size_t i = 0; i < num_modules; i++) {
441 Module *module_pointer =
442 target_modules.GetModulePointerAtIndexUnlocked(i);
443 if (module_pointer) {
444 addr_t symbol_address =
445 GetDyldLockVariableAddressFromModule(module_pointer);
446 if (symbol_address != LLDB_INVALID_ADDRESS)
447 break;
448 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000449 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 }
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000451
Adrian Prantl05097242018-04-30 16:49:04 +0000452 // Default assumption is that it is OK to load images. Only say that we
453 // cannot load images if we find the symbol in libdyld and it indicates that
454 // we cannot.
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 if (symbol_address != LLDB_INVALID_ADDRESS) {
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000457 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 int lock_held =
459 m_process->ReadUnsignedIntegerFromMemory(symbol_address, 4, 0, error);
460 if (lock_held != 0) {
Jim Inghamabc5d722017-05-06 01:15:47 +0000461 error.SetErrorString("dyld lock held - unsafe to load images.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 }
Jason Molenda37397352016-07-22 00:17:55 +0000463 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000465 // If we were unable to find _dyld_global_lock_held in any modules, or it
466 // is not loaded into memory yet, we may be at process startup (sitting at
467 // _dyld_start) - so we should not allow dlopen calls. But if we found more
468 // than one module then we are clearly past _dyld_start so in that case
469 // we'll default to "it's safe".
Jim Inghamabc5d722017-05-06 01:15:47 +0000470 if (num_modules <= 1)
471 error.SetErrorString("could not find the dyld library or "
472 "the dyld lock symbol");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 }
474 return error;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000475}
476
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477bool DynamicLoaderMacOS::GetSharedCacheInformation(
478 lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache,
479 LazyBool &private_shared_cache) {
480 base_address = LLDB_INVALID_ADDRESS;
481 uuid.Clear();
482 using_shared_cache = eLazyBoolCalculate;
483 private_shared_cache = eLazyBoolCalculate;
Jason Molenda13becd42016-07-29 00:18:39 +0000484
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 if (m_process) {
486 StructuredData::ObjectSP info = m_process->GetSharedCacheInfo();
487 StructuredData::Dictionary *info_dict = nullptr;
488 if (info.get() && info->GetAsDictionary()) {
489 info_dict = info->GetAsDictionary();
Jason Molenda13becd42016-07-29 00:18:39 +0000490 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491
Adrian Prantl05097242018-04-30 16:49:04 +0000492 // {"shared_cache_base_address":140735683125248,"shared_cache_uuid
493 // ":"DDB8D70C-
494 // C9A2-3561-B2C8-BE48A4F33F96","no_shared_cache":false,"shared_cache_private_cache":false}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495
496 if (info_dict && info_dict->HasKey("shared_cache_uuid") &&
497 info_dict->HasKey("no_shared_cache") &&
498 info_dict->HasKey("shared_cache_base_address")) {
499 base_address = info_dict->GetValueForKey("shared_cache_base_address")
500 ->GetIntegerValue(LLDB_INVALID_ADDRESS);
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000501 std::string uuid_str =
502 info_dict->GetValueForKey("shared_cache_uuid")->GetStringValue();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 if (!uuid_str.empty())
Pavel Labatha174bcb2018-06-21 15:24:39 +0000504 uuid.SetFromStringRef(uuid_str);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000505 if (!info_dict->GetValueForKey("no_shared_cache")->GetBooleanValue())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 using_shared_cache = eLazyBoolYes;
507 else
508 using_shared_cache = eLazyBoolNo;
509 if (info_dict->GetValueForKey("shared_cache_private_cache")
510 ->GetBooleanValue())
511 private_shared_cache = eLazyBoolYes;
512 else
513 private_shared_cache = eLazyBoolNo;
514
515 return true;
516 }
517 }
518 return false;
Jason Molenda13becd42016-07-29 00:18:39 +0000519}
520
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521void DynamicLoaderMacOS::Initialize() {
522 PluginManager::RegisterPlugin(GetPluginNameStatic(),
523 GetPluginDescriptionStatic(), CreateInstance);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000524}
525
Kate Stoneb9c1b512016-09-06 20:57:50 +0000526void DynamicLoaderMacOS::Terminate() {
527 PluginManager::UnregisterPlugin(CreateInstance);
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000528}
529
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530lldb_private::ConstString DynamicLoaderMacOS::GetPluginNameStatic() {
531 static ConstString g_name("macos-dyld");
532 return g_name;
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000533}
534
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535const char *DynamicLoaderMacOS::GetPluginDescriptionStatic() {
536 return "Dynamic loader plug-in that watches for shared library loads/unloads "
537 "in MacOSX user processes.";
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000538}
539
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000540//------------------------------------------------------------------
541// PluginInterface protocol
542//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543lldb_private::ConstString DynamicLoaderMacOS::GetPluginName() {
544 return GetPluginNameStatic();
Jason Molenda9ab5dc22016-07-21 08:30:55 +0000545}
546
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547uint32_t DynamicLoaderMacOS::GetPluginVersion() { return 1; }