1 | //===-- DynamicLoaderMacOSXDYLD.cpp ---------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "DynamicLoaderMacOSXDYLD.h" |
10 | #include "DynamicLoaderDarwin.h" |
11 | #include "DynamicLoaderMacOS.h" |
12 | #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" |
13 | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
14 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
15 | #include "lldb/Core/Debugger.h" |
16 | #include "lldb/Core/Module.h" |
17 | #include "lldb/Core/ModuleSpec.h" |
18 | #include "lldb/Core/PluginManager.h" |
19 | #include "lldb/Core/Section.h" |
20 | #include "lldb/Symbol/Function.h" |
21 | #include "lldb/Symbol/ObjectFile.h" |
22 | #include "lldb/Target/ABI.h" |
23 | #include "lldb/Target/RegisterContext.h" |
24 | #include "lldb/Target/StackFrame.h" |
25 | #include "lldb/Target/Target.h" |
26 | #include "lldb/Target/Thread.h" |
27 | #include "lldb/Target/ThreadPlanRunToAddress.h" |
28 | #include "lldb/Utility/DataBuffer.h" |
29 | #include "lldb/Utility/DataBufferHeap.h" |
30 | #include "lldb/Utility/LLDBLog.h" |
31 | #include "lldb/Utility/Log.h" |
32 | #include "lldb/Utility/State.h" |
33 | |
34 | //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN |
35 | #ifdef ENABLE_DEBUG_PRINTF |
36 | #include <cstdio> |
37 | #define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__) |
38 | #else |
39 | #define DEBUG_PRINTF(fmt, ...) |
40 | #endif |
41 | |
42 | #ifndef __APPLE__ |
43 | #include "lldb/Utility/AppleUuidCompatibility.h" |
44 | #else |
45 | #include <uuid/uuid.h> |
46 | #endif |
47 | |
48 | using namespace lldb; |
49 | using namespace lldb_private; |
50 | |
51 | LLDB_PLUGIN_DEFINE(DynamicLoaderMacOSXDYLD) |
52 | |
53 | // Create an instance of this class. This function is filled into the plugin |
54 | // info class that gets handed out by the plugin factory and allows the lldb to |
55 | // instantiate an instance of this class. |
56 | DynamicLoader *DynamicLoaderMacOSXDYLD::CreateInstance(Process *process, |
57 | bool force) { |
58 | bool create = force; |
59 | if (!create) { |
60 | create = true; |
61 | Module *exe_module = process->GetTarget().GetExecutableModulePointer(); |
62 | if (exe_module) { |
63 | ObjectFile *object_file = exe_module->GetObjectFile(); |
64 | if (object_file) { |
65 | create = (object_file->GetStrata() == ObjectFile::eStrataUser); |
66 | } |
67 | } |
68 | |
69 | if (create) { |
70 | const llvm::Triple &triple_ref = |
71 | process->GetTarget().GetArchitecture().GetTriple(); |
72 | switch (triple_ref.getOS()) { |
73 | case llvm::Triple::Darwin: |
74 | case llvm::Triple::MacOSX: |
75 | case llvm::Triple::IOS: |
76 | case llvm::Triple::TvOS: |
77 | case llvm::Triple::WatchOS: |
78 | case llvm::Triple::XROS: |
79 | case llvm::Triple::BridgeOS: |
80 | create = triple_ref.getVendor() == llvm::Triple::Apple; |
81 | break; |
82 | default: |
83 | create = false; |
84 | break; |
85 | } |
86 | } |
87 | } |
88 | |
89 | if (UseDYLDSPI(process)) { |
90 | create = false; |
91 | } |
92 | |
93 | if (create) |
94 | return new DynamicLoaderMacOSXDYLD(process); |
95 | return nullptr; |
96 | } |
97 | |
98 | // Constructor |
99 | DynamicLoaderMacOSXDYLD::DynamicLoaderMacOSXDYLD(Process *process) |
100 | : DynamicLoaderDarwin(process), |
101 | m_dyld_all_image_infos_addr(LLDB_INVALID_ADDRESS), |
102 | m_dyld_all_image_infos(), m_dyld_all_image_infos_stop_id(UINT32_MAX), |
103 | m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(), |
104 | m_process_image_addr_is_all_images_infos(false) {} |
105 | |
106 | // Destructor |
107 | DynamicLoaderMacOSXDYLD::~DynamicLoaderMacOSXDYLD() { |
108 | if (LLDB_BREAK_ID_IS_VALID(m_break_id)) |
109 | m_process->GetTarget().RemoveBreakpointByID(break_id: m_break_id); |
110 | } |
111 | |
112 | bool DynamicLoaderMacOSXDYLD::ProcessDidExec() { |
113 | std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); |
114 | bool did_exec = false; |
115 | if (m_process) { |
116 | // If we are stopped after an exec, we will have only one thread... |
117 | if (m_process->GetThreadList().GetSize() == 1) { |
118 | // We know if a process has exec'ed if our "m_dyld_all_image_infos_addr" |
119 | // value differs from the Process' image info address. When a process |
120 | // execs itself it might cause a change if ASLR is enabled. |
121 | const addr_t shlib_addr = m_process->GetImageInfoAddress(); |
122 | if (m_process_image_addr_is_all_images_infos && |
123 | shlib_addr != m_dyld_all_image_infos_addr) { |
124 | // The image info address from the process is the |
125 | // 'dyld_all_image_infos' address and it has changed. |
126 | did_exec = true; |
127 | } else if (!m_process_image_addr_is_all_images_infos && |
128 | shlib_addr == m_dyld.address) { |
129 | // The image info address from the process is the mach_header address |
130 | // for dyld and it has changed. |
131 | did_exec = true; |
132 | } else { |
133 | // ASLR might be disabled and dyld could have ended up in the same |
134 | // location. We should try and detect if we are stopped at |
135 | // '_dyld_start' |
136 | ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(idx: 0)); |
137 | if (thread_sp) { |
138 | lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(idx: 0)); |
139 | if (frame_sp) { |
140 | const Symbol *symbol = |
141 | frame_sp->GetSymbolContext(resolve_scope: eSymbolContextSymbol).symbol; |
142 | if (symbol) { |
143 | if (symbol->GetName() == "_dyld_start" ) |
144 | did_exec = true; |
145 | } |
146 | } |
147 | } |
148 | } |
149 | |
150 | if (did_exec) { |
151 | m_libpthread_module_wp.reset(); |
152 | m_pthread_getspecific_addr.Clear(); |
153 | } |
154 | } |
155 | } |
156 | return did_exec; |
157 | } |
158 | |
159 | // Clear out the state of this class. |
160 | void DynamicLoaderMacOSXDYLD::DoClear() { |
161 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
162 | |
163 | if (LLDB_BREAK_ID_IS_VALID(m_break_id)) |
164 | m_process->GetTarget().RemoveBreakpointByID(break_id: m_break_id); |
165 | |
166 | m_dyld_all_image_infos_addr = LLDB_INVALID_ADDRESS; |
167 | m_dyld_all_image_infos.Clear(); |
168 | m_break_id = LLDB_INVALID_BREAK_ID; |
169 | } |
170 | |
171 | // Check if we have found DYLD yet |
172 | bool DynamicLoaderMacOSXDYLD::DidSetNotificationBreakpoint() { |
173 | return LLDB_BREAK_ID_IS_VALID(m_break_id); |
174 | } |
175 | |
176 | void DynamicLoaderMacOSXDYLD::ClearNotificationBreakpoint() { |
177 | if (LLDB_BREAK_ID_IS_VALID(m_break_id)) { |
178 | m_process->GetTarget().RemoveBreakpointByID(break_id: m_break_id); |
179 | } |
180 | } |
181 | |
182 | // Try and figure out where dyld is by first asking the Process if it knows |
183 | // (which currently calls down in the lldb::Process to get the DYLD info |
184 | // (available on SnowLeopard only). If that fails, then check in the default |
185 | // addresses. |
186 | void DynamicLoaderMacOSXDYLD::DoInitialImageFetch() { |
187 | if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS) { |
188 | // Check the image info addr as it might point to the mach header for dyld, |
189 | // or it might point to the dyld_all_image_infos struct |
190 | const addr_t shlib_addr = m_process->GetImageInfoAddress(); |
191 | if (shlib_addr != LLDB_INVALID_ADDRESS) { |
192 | ByteOrder byte_order = |
193 | m_process->GetTarget().GetArchitecture().GetByteOrder(); |
194 | uint8_t buf[4]; |
195 | DataExtractor data(buf, sizeof(buf), byte_order, 4); |
196 | Status error; |
197 | if (m_process->ReadMemory(vm_addr: shlib_addr, buf, size: 4, error) == 4) { |
198 | lldb::offset_t offset = 0; |
199 | uint32_t magic = data.GetU32(offset_ptr: &offset); |
200 | switch (magic) { |
201 | case llvm::MachO::MH_MAGIC: |
202 | case llvm::MachO::MH_MAGIC_64: |
203 | case llvm::MachO::MH_CIGAM: |
204 | case llvm::MachO::MH_CIGAM_64: |
205 | m_process_image_addr_is_all_images_infos = false; |
206 | ReadDYLDInfoFromMemoryAndSetNotificationCallback(addr: shlib_addr); |
207 | return; |
208 | |
209 | default: |
210 | break; |
211 | } |
212 | } |
213 | // Maybe it points to the all image infos? |
214 | m_dyld_all_image_infos_addr = shlib_addr; |
215 | m_process_image_addr_is_all_images_infos = true; |
216 | } |
217 | } |
218 | |
219 | if (m_dyld_all_image_infos_addr != LLDB_INVALID_ADDRESS) { |
220 | if (ReadAllImageInfosStructure()) { |
221 | if (m_dyld_all_image_infos.dyldImageLoadAddress != LLDB_INVALID_ADDRESS) |
222 | ReadDYLDInfoFromMemoryAndSetNotificationCallback( |
223 | addr: m_dyld_all_image_infos.dyldImageLoadAddress); |
224 | else |
225 | ReadDYLDInfoFromMemoryAndSetNotificationCallback( |
226 | addr: m_dyld_all_image_infos_addr & 0xfffffffffff00000ull); |
227 | return; |
228 | } |
229 | } |
230 | |
231 | // Check some default values |
232 | Module *executable = m_process->GetTarget().GetExecutableModulePointer(); |
233 | |
234 | if (executable) { |
235 | const ArchSpec &exe_arch = executable->GetArchitecture(); |
236 | if (exe_arch.GetAddressByteSize() == 8) { |
237 | ReadDYLDInfoFromMemoryAndSetNotificationCallback(addr: 0x7fff5fc00000ull); |
238 | } else if (exe_arch.GetMachine() == llvm::Triple::arm || |
239 | exe_arch.GetMachine() == llvm::Triple::thumb || |
240 | exe_arch.GetMachine() == llvm::Triple::aarch64 || |
241 | exe_arch.GetMachine() == llvm::Triple::aarch64_32) { |
242 | ReadDYLDInfoFromMemoryAndSetNotificationCallback(addr: 0x2fe00000); |
243 | } else { |
244 | ReadDYLDInfoFromMemoryAndSetNotificationCallback(addr: 0x8fe00000); |
245 | } |
246 | } |
247 | } |
248 | |
249 | // Assume that dyld is in memory at ADDR and try to parse it's load commands |
250 | bool DynamicLoaderMacOSXDYLD::ReadDYLDInfoFromMemoryAndSetNotificationCallback( |
251 | lldb::addr_t addr) { |
252 | std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); |
253 | DataExtractor data; // Load command data |
254 | static ConstString g_dyld_all_image_infos("dyld_all_image_infos" ); |
255 | static ConstString g_new_dyld_all_image_infos("dyld4::dyld_all_image_infos" ); |
256 | if (ReadMachHeader(addr, header: &m_dyld.header, load_command_data: &data)) { |
257 | if (m_dyld.header.filetype == llvm::MachO::MH_DYLINKER) { |
258 | m_dyld.address = addr; |
259 | ModuleSP dyld_module_sp; |
260 | if (ParseLoadCommands(data, dylib_info&: m_dyld, lc_id_dylinker: &m_dyld.file_spec)) { |
261 | if (m_dyld.file_spec) { |
262 | UpdateDYLDImageInfoFromNewImageInfo(image_info&: m_dyld); |
263 | } |
264 | } |
265 | dyld_module_sp = GetDYLDModule(); |
266 | |
267 | Target &target = m_process->GetTarget(); |
268 | |
269 | if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS && |
270 | dyld_module_sp.get()) { |
271 | const Symbol *symbol = dyld_module_sp->FindFirstSymbolWithNameAndType( |
272 | name: g_dyld_all_image_infos, symbol_type: eSymbolTypeData); |
273 | if (!symbol) { |
274 | symbol = dyld_module_sp->FindFirstSymbolWithNameAndType( |
275 | name: g_new_dyld_all_image_infos, symbol_type: eSymbolTypeData); |
276 | } |
277 | if (symbol) |
278 | m_dyld_all_image_infos_addr = symbol->GetLoadAddress(target: &target); |
279 | } |
280 | |
281 | if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS) { |
282 | ConstString g_sect_name("__all_image_info" ); |
283 | SectionSP dyld_aii_section_sp = |
284 | dyld_module_sp->GetSectionList()->FindSectionByName(section_dstr: g_sect_name); |
285 | if (dyld_aii_section_sp) { |
286 | Address dyld_aii_addr(dyld_aii_section_sp, 0); |
287 | m_dyld_all_image_infos_addr = dyld_aii_addr.GetLoadAddress(target: &target); |
288 | } |
289 | } |
290 | |
291 | // Update all image infos |
292 | InitializeFromAllImageInfos(); |
293 | |
294 | // If we didn't have an executable before, but now we do, then the dyld |
295 | // module shared pointer might be unique and we may need to add it again |
296 | // (since Target::SetExecutableModule() will clear the images). So append |
297 | // the dyld module back to the list if it is |
298 | /// unique! |
299 | if (dyld_module_sp) { |
300 | target.GetImages().AppendIfNeeded(new_module: dyld_module_sp); |
301 | |
302 | // At this point we should have read in dyld's module, and so we should |
303 | // set breakpoints in it: |
304 | ModuleList modules; |
305 | modules.Append(module_sp: dyld_module_sp); |
306 | target.ModulesDidLoad(module_list&: modules); |
307 | SetDYLDModule(dyld_module_sp); |
308 | } |
309 | |
310 | return true; |
311 | } |
312 | } |
313 | return false; |
314 | } |
315 | |
316 | bool DynamicLoaderMacOSXDYLD::NeedToDoInitialImageFetch() { |
317 | return m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS; |
318 | } |
319 | |
320 | // Static callback function that gets called when our DYLD notification |
321 | // breakpoint gets hit. We update all of our image infos and then let our super |
322 | // class DynamicLoader class decide if we should stop or not (based on global |
323 | // preference). |
324 | bool DynamicLoaderMacOSXDYLD::NotifyBreakpointHit( |
325 | void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, |
326 | lldb::user_id_t break_loc_id) { |
327 | // Let the event know that the images have changed |
328 | // DYLD passes three arguments to the notification breakpoint. |
329 | // Arg1: enum dyld_image_mode mode - 0 = adding, 1 = removing Arg2: uint32_t |
330 | // infoCount - Number of shared libraries added Arg3: dyld_image_info |
331 | // info[] - Array of structs of the form: |
332 | // const struct mach_header |
333 | // *imageLoadAddress |
334 | // const char *imageFilePath |
335 | // uintptr_t imageFileModDate (a time_t) |
336 | |
337 | DynamicLoaderMacOSXDYLD *dyld_instance = (DynamicLoaderMacOSXDYLD *)baton; |
338 | |
339 | // First step is to see if we've already initialized the all image infos. If |
340 | // we haven't then this function will do so and return true. In the course |
341 | // of initializing the all_image_infos it will read the complete current |
342 | // state, so we don't need to figure out what has changed from the data |
343 | // passed in to us. |
344 | |
345 | ExecutionContext exe_ctx(context->exe_ctx_ref); |
346 | Process *process = exe_ctx.GetProcessPtr(); |
347 | |
348 | // This is a sanity check just in case this dyld_instance is an old dyld |
349 | // plugin's breakpoint still lying around. |
350 | if (process != dyld_instance->m_process) |
351 | return false; |
352 | |
353 | if (dyld_instance->InitializeFromAllImageInfos()) |
354 | return dyld_instance->GetStopWhenImagesChange(); |
355 | |
356 | const lldb::ABISP &abi = process->GetABI(); |
357 | if (abi) { |
358 | // Build up the value array to store the three arguments given above, then |
359 | // get the values from the ABI: |
360 | |
361 | TypeSystemClangSP scratch_ts_sp = |
362 | ScratchTypeSystemClang::GetForTarget(target&: process->GetTarget()); |
363 | if (!scratch_ts_sp) |
364 | return false; |
365 | |
366 | ValueList argument_values; |
367 | Value input_value; |
368 | |
369 | CompilerType clang_void_ptr_type = |
370 | scratch_ts_sp->GetBasicType(type: eBasicTypeVoid).GetPointerType(); |
371 | CompilerType clang_uint32_type = |
372 | scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(encoding: lldb::eEncodingUint, |
373 | bit_size: 32); |
374 | input_value.SetValueType(Value::ValueType::Scalar); |
375 | input_value.SetCompilerType(clang_uint32_type); |
376 | // input_value.SetContext (Value::eContextTypeClangType, |
377 | // clang_uint32_type); |
378 | argument_values.PushValue(value: input_value); |
379 | argument_values.PushValue(value: input_value); |
380 | input_value.SetCompilerType(clang_void_ptr_type); |
381 | // input_value.SetContext (Value::eContextTypeClangType, |
382 | // clang_void_ptr_type); |
383 | argument_values.PushValue(value: input_value); |
384 | |
385 | if (abi->GetArgumentValues(thread&: exe_ctx.GetThreadRef(), values&: argument_values)) { |
386 | uint32_t dyld_mode = |
387 | argument_values.GetValueAtIndex(idx: 0)->GetScalar().UInt(fail_value: -1); |
388 | if (dyld_mode != static_cast<uint32_t>(-1)) { |
389 | // Okay the mode was right, now get the number of elements, and the |
390 | // array of new elements... |
391 | uint32_t image_infos_count = |
392 | argument_values.GetValueAtIndex(idx: 1)->GetScalar().UInt(fail_value: -1); |
393 | if (image_infos_count != static_cast<uint32_t>(-1)) { |
394 | // Got the number added, now go through the array of added elements, |
395 | // putting out the mach header address, and adding the image. Note, |
396 | // I'm not putting in logging here, since the AddModules & |
397 | // RemoveModules functions do all the logging internally. |
398 | |
399 | lldb::addr_t image_infos_addr = |
400 | argument_values.GetValueAtIndex(idx: 2)->GetScalar().ULongLong(); |
401 | if (dyld_mode == 0) { |
402 | // This is add: |
403 | dyld_instance->AddModulesUsingImageInfosAddress(image_infos_addr, |
404 | image_infos_count); |
405 | } else { |
406 | // This is remove: |
407 | dyld_instance->RemoveModulesUsingImageInfosAddress( |
408 | image_infos_addr, image_infos_count); |
409 | } |
410 | } |
411 | } |
412 | } |
413 | } else { |
414 | Target &target = process->GetTarget(); |
415 | Debugger::ReportWarning( |
416 | message: "no ABI plugin located for triple " + |
417 | target.GetArchitecture().GetTriple().getTriple() + |
418 | ": shared libraries will not be registered" , |
419 | debugger_id: target.GetDebugger().GetID()); |
420 | } |
421 | |
422 | // Return true to stop the target, false to just let the target run |
423 | return dyld_instance->GetStopWhenImagesChange(); |
424 | } |
425 | |
426 | bool DynamicLoaderMacOSXDYLD::ReadAllImageInfosStructure() { |
427 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
428 | |
429 | // the all image infos is already valid for this process stop ID |
430 | if (m_process->GetStopID() == m_dyld_all_image_infos_stop_id) |
431 | return true; |
432 | |
433 | m_dyld_all_image_infos.Clear(); |
434 | if (m_dyld_all_image_infos_addr != LLDB_INVALID_ADDRESS) { |
435 | ByteOrder byte_order = |
436 | m_process->GetTarget().GetArchitecture().GetByteOrder(); |
437 | uint32_t addr_size = |
438 | m_process->GetTarget().GetArchitecture().GetAddressByteSize(); |
439 | |
440 | uint8_t buf[256]; |
441 | DataExtractor data(buf, sizeof(buf), byte_order, addr_size); |
442 | lldb::offset_t offset = 0; |
443 | |
444 | const size_t count_v2 = sizeof(uint32_t) + // version |
445 | sizeof(uint32_t) + // infoArrayCount |
446 | addr_size + // infoArray |
447 | addr_size + // notification |
448 | addr_size + // processDetachedFromSharedRegion + |
449 | // libSystemInitialized + pad |
450 | addr_size; // dyldImageLoadAddress |
451 | const size_t count_v11 = count_v2 + addr_size + // jitInfo |
452 | addr_size + // dyldVersion |
453 | addr_size + // errorMessage |
454 | addr_size + // terminationFlags |
455 | addr_size + // coreSymbolicationShmPage |
456 | addr_size + // systemOrderFlag |
457 | addr_size + // uuidArrayCount |
458 | addr_size + // uuidArray |
459 | addr_size + // dyldAllImageInfosAddress |
460 | addr_size + // initialImageCount |
461 | addr_size + // errorKind |
462 | addr_size + // errorClientOfDylibPath |
463 | addr_size + // errorTargetDylibPath |
464 | addr_size; // errorSymbol |
465 | const size_t count_v13 = count_v11 + addr_size + // sharedCacheSlide |
466 | sizeof(uuid_t); // sharedCacheUUID |
467 | UNUSED_IF_ASSERT_DISABLED(count_v13); |
468 | assert(sizeof(buf) >= count_v13); |
469 | |
470 | Status error; |
471 | if (m_process->ReadMemory(vm_addr: m_dyld_all_image_infos_addr, buf, size: 4, error) == |
472 | 4) { |
473 | m_dyld_all_image_infos.version = data.GetU32(offset_ptr: &offset); |
474 | // If anything in the high byte is set, we probably got the byte order |
475 | // incorrect (the process might not have it set correctly yet due to |
476 | // attaching to a program without a specified file). |
477 | if (m_dyld_all_image_infos.version & 0xff000000) { |
478 | // We have guessed the wrong byte order. Swap it and try reading the |
479 | // version again. |
480 | if (byte_order == eByteOrderLittle) |
481 | byte_order = eByteOrderBig; |
482 | else |
483 | byte_order = eByteOrderLittle; |
484 | |
485 | data.SetByteOrder(byte_order); |
486 | offset = 0; |
487 | m_dyld_all_image_infos.version = data.GetU32(offset_ptr: &offset); |
488 | } |
489 | } else { |
490 | return false; |
491 | } |
492 | |
493 | const size_t count = |
494 | (m_dyld_all_image_infos.version >= 11) ? count_v11 : count_v2; |
495 | |
496 | const size_t bytes_read = |
497 | m_process->ReadMemory(vm_addr: m_dyld_all_image_infos_addr, buf, size: count, error); |
498 | if (bytes_read == count) { |
499 | offset = 0; |
500 | m_dyld_all_image_infos.version = data.GetU32(offset_ptr: &offset); |
501 | m_dyld_all_image_infos.dylib_info_count = data.GetU32(offset_ptr: &offset); |
502 | m_dyld_all_image_infos.dylib_info_addr = data.GetAddress(offset_ptr: &offset); |
503 | m_dyld_all_image_infos.notification = data.GetAddress(offset_ptr: &offset); |
504 | m_dyld_all_image_infos.processDetachedFromSharedRegion = |
505 | data.GetU8(offset_ptr: &offset); |
506 | m_dyld_all_image_infos.libSystemInitialized = data.GetU8(offset_ptr: &offset); |
507 | // Adjust for padding. |
508 | offset += addr_size - 2; |
509 | m_dyld_all_image_infos.dyldImageLoadAddress = data.GetAddress(offset_ptr: &offset); |
510 | if (m_dyld_all_image_infos.version >= 11) { |
511 | offset += addr_size * 8; |
512 | uint64_t dyld_all_image_infos_addr = data.GetAddress(offset_ptr: &offset); |
513 | |
514 | // When we started, we were given the actual address of the |
515 | // all_image_infos struct (probably via TASK_DYLD_INFO) in memory - |
516 | // this address is stored in m_dyld_all_image_infos_addr and is the |
517 | // most accurate address we have. |
518 | |
519 | // We read the dyld_all_image_infos struct from memory; it contains its |
520 | // own address. If the address in the struct does not match the actual |
521 | // address, the dyld we're looking at has been loaded at a different |
522 | // location (slid) from where it intended to load. The addresses in |
523 | // the dyld_all_image_infos struct are the original, non-slid |
524 | // addresses, and need to be adjusted. Most importantly the address of |
525 | // dyld and the notification address need to be adjusted. |
526 | |
527 | if (dyld_all_image_infos_addr != m_dyld_all_image_infos_addr) { |
528 | uint64_t image_infos_offset = |
529 | dyld_all_image_infos_addr - |
530 | m_dyld_all_image_infos.dyldImageLoadAddress; |
531 | uint64_t notification_offset = |
532 | m_dyld_all_image_infos.notification - |
533 | m_dyld_all_image_infos.dyldImageLoadAddress; |
534 | m_dyld_all_image_infos.dyldImageLoadAddress = |
535 | m_dyld_all_image_infos_addr - image_infos_offset; |
536 | m_dyld_all_image_infos.notification = |
537 | m_dyld_all_image_infos.dyldImageLoadAddress + notification_offset; |
538 | } |
539 | } |
540 | m_dyld_all_image_infos_stop_id = m_process->GetStopID(); |
541 | return true; |
542 | } |
543 | } |
544 | return false; |
545 | } |
546 | |
547 | bool DynamicLoaderMacOSXDYLD::AddModulesUsingImageInfosAddress( |
548 | lldb::addr_t image_infos_addr, uint32_t image_infos_count) { |
549 | ImageInfo::collection image_infos; |
550 | Log *log = GetLog(mask: LLDBLog::DynamicLoader); |
551 | LLDB_LOGF(log, "Adding %d modules.\n" , image_infos_count); |
552 | |
553 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
554 | std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); |
555 | if (m_process->GetStopID() == m_dyld_image_infos_stop_id) |
556 | return true; |
557 | |
558 | StructuredData::ObjectSP image_infos_json_sp = |
559 | m_process->GetLoadedDynamicLibrariesInfos(image_list_address: image_infos_addr, |
560 | image_count: image_infos_count); |
561 | if (image_infos_json_sp.get() && image_infos_json_sp->GetAsDictionary() && |
562 | image_infos_json_sp->GetAsDictionary()->HasKey(key: "images" ) && |
563 | image_infos_json_sp->GetAsDictionary() |
564 | ->GetValueForKey(key: "images" ) |
565 | ->GetAsArray() && |
566 | image_infos_json_sp->GetAsDictionary() |
567 | ->GetValueForKey(key: "images" ) |
568 | ->GetAsArray() |
569 | ->GetSize() == image_infos_count) { |
570 | bool return_value = false; |
571 | if (JSONImageInformationIntoImageInfo(image_details: image_infos_json_sp, image_infos)) { |
572 | UpdateSpecialBinariesFromNewImageInfos(image_infos); |
573 | return_value = AddModulesUsingImageInfos(image_infos); |
574 | } |
575 | m_dyld_image_infos_stop_id = m_process->GetStopID(); |
576 | return return_value; |
577 | } |
578 | |
579 | if (!ReadImageInfos(image_infos_addr, image_infos_count, image_infos)) |
580 | return false; |
581 | |
582 | UpdateImageInfosHeaderAndLoadCommands(image_infos, infos_count: image_infos_count, update_executable: false); |
583 | bool return_value = AddModulesUsingImageInfos(image_infos); |
584 | m_dyld_image_infos_stop_id = m_process->GetStopID(); |
585 | return return_value; |
586 | } |
587 | |
588 | bool DynamicLoaderMacOSXDYLD::RemoveModulesUsingImageInfosAddress( |
589 | lldb::addr_t image_infos_addr, uint32_t image_infos_count) { |
590 | ImageInfo::collection image_infos; |
591 | Log *log = GetLog(mask: LLDBLog::DynamicLoader); |
592 | |
593 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
594 | std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); |
595 | if (m_process->GetStopID() == m_dyld_image_infos_stop_id) |
596 | return true; |
597 | |
598 | // First read in the image_infos for the removed modules, and their headers & |
599 | // load commands. |
600 | if (!ReadImageInfos(image_infos_addr, image_infos_count, image_infos)) { |
601 | if (log) |
602 | log->PutCString(cstr: "Failed reading image infos array." ); |
603 | return false; |
604 | } |
605 | |
606 | LLDB_LOGF(log, "Removing %d modules." , image_infos_count); |
607 | |
608 | ModuleList unloaded_module_list; |
609 | for (uint32_t idx = 0; idx < image_infos.size(); ++idx) { |
610 | if (log) { |
611 | LLDB_LOGF(log, "Removing module at address=0x%16.16" PRIx64 "." , |
612 | image_infos[idx].address); |
613 | image_infos[idx].PutToLog(log); |
614 | } |
615 | |
616 | // Remove this image_infos from the m_all_image_infos. We do the |
617 | // comparison by address rather than by file spec because we can have many |
618 | // modules with the same "file spec" in the case that they are modules |
619 | // loaded from memory. |
620 | // |
621 | // Also copy over the uuid from the old entry to the removed entry so we |
622 | // can use it to lookup the module in the module list. |
623 | |
624 | bool found = false; |
625 | |
626 | for (ImageInfo::collection::iterator pos = m_dyld_image_infos.begin(); |
627 | pos != m_dyld_image_infos.end(); pos++) { |
628 | if (image_infos[idx].address == (*pos).address) { |
629 | image_infos[idx].uuid = (*pos).uuid; |
630 | |
631 | // Add the module from this image_info to the "unloaded_module_list". |
632 | // We'll remove them all at one go later on. |
633 | |
634 | ModuleSP unload_image_module_sp( |
635 | FindTargetModuleForImageInfo(image_info&: image_infos[idx], can_create: false, did_create_ptr: nullptr)); |
636 | if (unload_image_module_sp.get()) { |
637 | // When we unload, be sure to use the image info from the old list, |
638 | // since that has sections correctly filled in. |
639 | UnloadModuleSections(module: unload_image_module_sp.get(), info&: *pos); |
640 | unloaded_module_list.AppendIfNeeded(new_module: unload_image_module_sp); |
641 | } else { |
642 | if (log) { |
643 | LLDB_LOGF(log, "Could not find module for unloading info entry:" ); |
644 | image_infos[idx].PutToLog(log); |
645 | } |
646 | } |
647 | |
648 | // Then remove it from the m_dyld_image_infos: |
649 | |
650 | m_dyld_image_infos.erase(position: pos); |
651 | found = true; |
652 | break; |
653 | } |
654 | } |
655 | |
656 | if (!found) { |
657 | if (log) { |
658 | LLDB_LOGF(log, "Could not find image_info entry for unloading image:" ); |
659 | image_infos[idx].PutToLog(log); |
660 | } |
661 | } |
662 | } |
663 | if (unloaded_module_list.GetSize() > 0) { |
664 | if (log) { |
665 | log->PutCString(cstr: "Unloaded:" ); |
666 | unloaded_module_list.LogUUIDAndPaths( |
667 | log, prefix_cstr: "DynamicLoaderMacOSXDYLD::ModulesDidUnload" ); |
668 | } |
669 | m_process->GetTarget().GetImages().Remove(module_list&: unloaded_module_list); |
670 | } |
671 | m_dyld_image_infos_stop_id = m_process->GetStopID(); |
672 | return true; |
673 | } |
674 | |
675 | bool DynamicLoaderMacOSXDYLD::ReadImageInfos( |
676 | lldb::addr_t image_infos_addr, uint32_t image_infos_count, |
677 | ImageInfo::collection &image_infos) { |
678 | std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); |
679 | const ByteOrder endian = GetByteOrderFromMagic(magic: m_dyld.header.magic); |
680 | const uint32_t addr_size = m_dyld.GetAddressByteSize(); |
681 | |
682 | image_infos.resize(new_size: image_infos_count); |
683 | const size_t count = image_infos.size() * 3 * addr_size; |
684 | DataBufferHeap info_data(count, 0); |
685 | Status error; |
686 | const size_t bytes_read = m_process->ReadMemory( |
687 | vm_addr: image_infos_addr, buf: info_data.GetBytes(), size: info_data.GetByteSize(), error); |
688 | if (bytes_read == count) { |
689 | lldb::offset_t info_data_offset = 0; |
690 | DataExtractor info_data_ref(info_data.GetBytes(), info_data.GetByteSize(), |
691 | endian, addr_size); |
692 | for (size_t i = 0; |
693 | i < image_infos.size() && info_data_ref.ValidOffset(offset: info_data_offset); |
694 | i++) { |
695 | image_infos[i].address = info_data_ref.GetAddress(offset_ptr: &info_data_offset); |
696 | lldb::addr_t path_addr = info_data_ref.GetAddress(offset_ptr: &info_data_offset); |
697 | info_data_ref.GetAddress(offset_ptr: &info_data_offset); // mod_date, unused */ |
698 | |
699 | char raw_path[PATH_MAX]; |
700 | m_process->ReadCStringFromMemory(vm_addr: path_addr, cstr: raw_path, cstr_max_len: sizeof(raw_path), |
701 | error); |
702 | // don't resolve the path |
703 | if (error.Success()) { |
704 | image_infos[i].file_spec.SetFile(path: raw_path, style: FileSpec::Style::native); |
705 | } |
706 | } |
707 | return true; |
708 | } else { |
709 | return false; |
710 | } |
711 | } |
712 | |
713 | // If we have found where the "_dyld_all_image_infos" lives in memory, read the |
714 | // current info from it, and then update all image load addresses (or lack |
715 | // thereof). Only do this if this is the first time we're reading the dyld |
716 | // infos. Return true if we actually read anything, and false otherwise. |
717 | bool DynamicLoaderMacOSXDYLD::InitializeFromAllImageInfos() { |
718 | Log *log = GetLog(mask: LLDBLog::DynamicLoader); |
719 | |
720 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
721 | std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); |
722 | if (m_process->GetStopID() == m_dyld_image_infos_stop_id || |
723 | m_dyld_image_infos.size() != 0) |
724 | return false; |
725 | |
726 | if (ReadAllImageInfosStructure()) { |
727 | // Nothing to load or unload? |
728 | if (m_dyld_all_image_infos.dylib_info_count == 0) |
729 | return true; |
730 | |
731 | if (m_dyld_all_image_infos.dylib_info_addr == 0) { |
732 | // DYLD is updating the images now. So we should say we have no images, |
733 | // and then we'll |
734 | // figure it out when we hit the added breakpoint. |
735 | return false; |
736 | } else { |
737 | if (!AddModulesUsingImageInfosAddress( |
738 | image_infos_addr: m_dyld_all_image_infos.dylib_info_addr, |
739 | image_infos_count: m_dyld_all_image_infos.dylib_info_count)) { |
740 | DEBUG_PRINTF("%s" , "unable to read all data for all_dylib_infos." ); |
741 | m_dyld_image_infos.clear(); |
742 | } |
743 | } |
744 | |
745 | // Now we have one more bit of business. If there is a library left in the |
746 | // images for our target that doesn't have a load address, then it must be |
747 | // something that we were expecting to load (for instance we read a load |
748 | // command for it) but it didn't in fact load - probably because |
749 | // DYLD_*_PATH pointed to an equivalent version. We don't want it to stay |
750 | // in the target's module list or it will confuse us, so unload it here. |
751 | Target &target = m_process->GetTarget(); |
752 | ModuleList not_loaded_modules; |
753 | for (ModuleSP module_sp : target.GetImages().Modules()) { |
754 | if (!module_sp->IsLoadedInTarget(target: &target)) { |
755 | if (log) { |
756 | StreamString s; |
757 | module_sp->GetDescription(s&: s.AsRawOstream()); |
758 | LLDB_LOGF(log, "Unloading pre-run module: %s." , s.GetData()); |
759 | } |
760 | not_loaded_modules.Append(module_sp); |
761 | } |
762 | } |
763 | |
764 | if (not_loaded_modules.GetSize() != 0) { |
765 | target.GetImages().Remove(module_list&: not_loaded_modules); |
766 | } |
767 | |
768 | return true; |
769 | } else |
770 | return false; |
771 | } |
772 | |
773 | // Read a mach_header at ADDR into HEADER, and also fill in the load command |
774 | // data into LOAD_COMMAND_DATA if it is non-NULL. |
775 | // |
776 | // Returns true if we succeed, false if we fail for any reason. |
777 | bool DynamicLoaderMacOSXDYLD::(lldb::addr_t addr, |
778 | llvm::MachO::mach_header *, |
779 | DataExtractor *load_command_data) { |
780 | DataBufferHeap (sizeof(llvm::MachO::mach_header), 0); |
781 | Status error; |
782 | size_t bytes_read = m_process->ReadMemory(vm_addr: addr, buf: header_bytes.GetBytes(), |
783 | size: header_bytes.GetByteSize(), error); |
784 | if (bytes_read == sizeof(llvm::MachO::mach_header)) { |
785 | lldb::offset_t offset = 0; |
786 | ::memset(s: header, c: 0, n: sizeof(llvm::MachO::mach_header)); |
787 | |
788 | // Get the magic byte unswapped so we can figure out what we are dealing |
789 | // with |
790 | DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(), |
791 | endian::InlHostByteOrder(), 4); |
792 | header->magic = data.GetU32(offset_ptr: &offset); |
793 | lldb::addr_t load_cmd_addr = addr; |
794 | data.SetByteOrder( |
795 | DynamicLoaderMacOSXDYLD::GetByteOrderFromMagic(magic: header->magic)); |
796 | switch (header->magic) { |
797 | case llvm::MachO::MH_MAGIC: |
798 | case llvm::MachO::MH_CIGAM: |
799 | data.SetAddressByteSize(4); |
800 | load_cmd_addr += sizeof(llvm::MachO::mach_header); |
801 | break; |
802 | |
803 | case llvm::MachO::MH_MAGIC_64: |
804 | case llvm::MachO::MH_CIGAM_64: |
805 | data.SetAddressByteSize(8); |
806 | load_cmd_addr += sizeof(llvm::MachO::mach_header_64); |
807 | break; |
808 | |
809 | default: |
810 | return false; |
811 | } |
812 | |
813 | // Read the rest of dyld's mach header |
814 | if (data.GetU32(offset_ptr: &offset, dst: &header->cputype, |
815 | count: (sizeof(llvm::MachO::mach_header) / sizeof(uint32_t)) - |
816 | 1)) { |
817 | if (load_command_data == nullptr) |
818 | return true; // We were able to read the mach_header and weren't asked |
819 | // to read the load command bytes |
820 | |
821 | WritableDataBufferSP load_cmd_data_sp( |
822 | new DataBufferHeap(header->sizeofcmds, 0)); |
823 | |
824 | size_t load_cmd_bytes_read = |
825 | m_process->ReadMemory(vm_addr: load_cmd_addr, buf: load_cmd_data_sp->GetBytes(), |
826 | size: load_cmd_data_sp->GetByteSize(), error); |
827 | |
828 | if (load_cmd_bytes_read == header->sizeofcmds) { |
829 | // Set the load command data and also set the correct endian swap |
830 | // settings and the correct address size |
831 | load_command_data->SetData(data_sp: load_cmd_data_sp, offset: 0, length: header->sizeofcmds); |
832 | load_command_data->SetByteOrder(data.GetByteOrder()); |
833 | load_command_data->SetAddressByteSize(data.GetAddressByteSize()); |
834 | return true; // We successfully read the mach_header and the load |
835 | // command data |
836 | } |
837 | |
838 | return false; // We weren't able to read the load command data |
839 | } |
840 | } |
841 | return false; // We failed the read the mach_header |
842 | } |
843 | |
844 | // Parse the load commands for an image |
845 | uint32_t DynamicLoaderMacOSXDYLD::ParseLoadCommands(const DataExtractor &data, |
846 | ImageInfo &dylib_info, |
847 | FileSpec *lc_id_dylinker) { |
848 | lldb::offset_t offset = 0; |
849 | uint32_t cmd_idx; |
850 | Segment segment; |
851 | dylib_info.Clear(load_cmd_data_only: true); |
852 | |
853 | for (cmd_idx = 0; cmd_idx < dylib_info.header.ncmds; cmd_idx++) { |
854 | // Clear out any load command specific data from DYLIB_INFO since we are |
855 | // about to read it. |
856 | |
857 | if (data.ValidOffsetForDataOfSize(offset, |
858 | length: sizeof(llvm::MachO::load_command))) { |
859 | llvm::MachO::load_command load_cmd; |
860 | lldb::offset_t load_cmd_offset = offset; |
861 | load_cmd.cmd = data.GetU32(offset_ptr: &offset); |
862 | load_cmd.cmdsize = data.GetU32(offset_ptr: &offset); |
863 | switch (load_cmd.cmd) { |
864 | case llvm::MachO::LC_SEGMENT: { |
865 | segment.name.SetTrimmedCStringWithLength( |
866 | cstr: (const char *)data.GetData(offset_ptr: &offset, length: 16), fixed_cstr_len: 16); |
867 | // We are putting 4 uint32_t values 4 uint64_t values so we have to use |
868 | // multiple 32 bit gets below. |
869 | segment.vmaddr = data.GetU32(offset_ptr: &offset); |
870 | segment.vmsize = data.GetU32(offset_ptr: &offset); |
871 | segment.fileoff = data.GetU32(offset_ptr: &offset); |
872 | segment.filesize = data.GetU32(offset_ptr: &offset); |
873 | // Extract maxprot, initprot, nsects and flags all at once |
874 | data.GetU32(offset_ptr: &offset, dst: &segment.maxprot, count: 4); |
875 | dylib_info.segments.push_back(x: segment); |
876 | } break; |
877 | |
878 | case llvm::MachO::LC_SEGMENT_64: { |
879 | segment.name.SetTrimmedCStringWithLength( |
880 | cstr: (const char *)data.GetData(offset_ptr: &offset, length: 16), fixed_cstr_len: 16); |
881 | // Extract vmaddr, vmsize, fileoff, and filesize all at once |
882 | data.GetU64(offset_ptr: &offset, dst: &segment.vmaddr, count: 4); |
883 | // Extract maxprot, initprot, nsects and flags all at once |
884 | data.GetU32(offset_ptr: &offset, dst: &segment.maxprot, count: 4); |
885 | dylib_info.segments.push_back(x: segment); |
886 | } break; |
887 | |
888 | case llvm::MachO::LC_ID_DYLINKER: |
889 | if (lc_id_dylinker) { |
890 | const lldb::offset_t name_offset = |
891 | load_cmd_offset + data.GetU32(offset_ptr: &offset); |
892 | const char *path = data.PeekCStr(offset: name_offset); |
893 | lc_id_dylinker->SetFile(path, style: FileSpec::Style::native); |
894 | FileSystem::Instance().Resolve(file_spec&: *lc_id_dylinker); |
895 | } |
896 | break; |
897 | |
898 | case llvm::MachO::LC_UUID: |
899 | dylib_info.uuid = UUID(data.GetData(offset_ptr: &offset, length: 16), 16); |
900 | break; |
901 | |
902 | default: |
903 | break; |
904 | } |
905 | // Set offset to be the beginning of the next load command. |
906 | offset = load_cmd_offset + load_cmd.cmdsize; |
907 | } |
908 | } |
909 | |
910 | // All sections listed in the dyld image info structure will all either be |
911 | // fixed up already, or they will all be off by a single slide amount that is |
912 | // determined by finding the first segment that is at file offset zero which |
913 | // also has bytes (a file size that is greater than zero) in the object file. |
914 | |
915 | // Determine the slide amount (if any) |
916 | const size_t num_sections = dylib_info.segments.size(); |
917 | for (size_t i = 0; i < num_sections; ++i) { |
918 | // Iterate through the object file sections to find the first section that |
919 | // starts of file offset zero and that has bytes in the file... |
920 | if ((dylib_info.segments[i].fileoff == 0 && |
921 | dylib_info.segments[i].filesize > 0) || |
922 | (dylib_info.segments[i].name == "__TEXT" )) { |
923 | dylib_info.slide = dylib_info.address - dylib_info.segments[i].vmaddr; |
924 | // We have found the slide amount, so we can exit this for loop. |
925 | break; |
926 | } |
927 | } |
928 | return cmd_idx; |
929 | } |
930 | |
931 | // Read the mach_header and load commands for each image that the |
932 | // _dyld_all_image_infos structure points to and cache the results. |
933 | |
934 | void DynamicLoaderMacOSXDYLD::UpdateImageInfosHeaderAndLoadCommands( |
935 | ImageInfo::collection &image_infos, uint32_t infos_count, |
936 | bool update_executable) { |
937 | uint32_t exe_idx = UINT32_MAX; |
938 | // Read any UUID values that we can get |
939 | for (uint32_t i = 0; i < infos_count; i++) { |
940 | if (!image_infos[i].UUIDValid()) { |
941 | DataExtractor data; // Load command data |
942 | if (!ReadMachHeader(addr: image_infos[i].address, header: &image_infos[i].header, |
943 | load_command_data: &data)) |
944 | continue; |
945 | |
946 | ParseLoadCommands(data, dylib_info&: image_infos[i], lc_id_dylinker: nullptr); |
947 | |
948 | if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE) |
949 | exe_idx = i; |
950 | } |
951 | } |
952 | |
953 | Target &target = m_process->GetTarget(); |
954 | |
955 | if (exe_idx < image_infos.size()) { |
956 | const bool can_create = true; |
957 | ModuleSP exe_module_sp(FindTargetModuleForImageInfo(image_info&: image_infos[exe_idx], |
958 | can_create, did_create_ptr: nullptr)); |
959 | |
960 | if (exe_module_sp) { |
961 | UpdateImageLoadAddress(module: exe_module_sp.get(), info&: image_infos[exe_idx]); |
962 | |
963 | if (exe_module_sp.get() != target.GetExecutableModulePointer()) { |
964 | // Don't load dependent images since we are in dyld where we will know |
965 | // and find out about all images that are loaded. Also when setting the |
966 | // executable module, it will clear the targets module list, and if we |
967 | // have an in memory dyld module, it will get removed from the list so |
968 | // we will need to add it back after setting the executable module, so |
969 | // we first try and see if we already have a weak pointer to the dyld |
970 | // module, make it into a shared pointer, then add the executable, then |
971 | // re-add it back to make sure it is always in the list. |
972 | ModuleSP dyld_module_sp(GetDYLDModule()); |
973 | |
974 | m_process->GetTarget().SetExecutableModule(module_sp&: exe_module_sp, |
975 | load_dependent_files: eLoadDependentsNo); |
976 | |
977 | if (dyld_module_sp) { |
978 | if (target.GetImages().AppendIfNeeded(new_module: dyld_module_sp)) { |
979 | std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); |
980 | |
981 | // Also add it to the section list. |
982 | UpdateImageLoadAddress(module: dyld_module_sp.get(), info&: m_dyld); |
983 | } |
984 | } |
985 | } |
986 | } |
987 | } |
988 | } |
989 | |
990 | // Dump the _dyld_all_image_infos members and all current image infos that we |
991 | // have parsed to the file handle provided. |
992 | void DynamicLoaderMacOSXDYLD::PutToLog(Log *log) const { |
993 | if (log == nullptr) |
994 | return; |
995 | |
996 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
997 | std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); |
998 | LLDB_LOGF(log, |
999 | "dyld_all_image_infos = { version=%d, count=%d, addr=0x%8.8" PRIx64 |
1000 | ", notify=0x%8.8" PRIx64 " }" , |
1001 | m_dyld_all_image_infos.version, |
1002 | m_dyld_all_image_infos.dylib_info_count, |
1003 | (uint64_t)m_dyld_all_image_infos.dylib_info_addr, |
1004 | (uint64_t)m_dyld_all_image_infos.notification); |
1005 | size_t i; |
1006 | const size_t count = m_dyld_image_infos.size(); |
1007 | if (count > 0) { |
1008 | log->PutCString(cstr: "Loaded:" ); |
1009 | for (i = 0; i < count; i++) |
1010 | m_dyld_image_infos[i].PutToLog(log); |
1011 | } |
1012 | } |
1013 | |
1014 | bool DynamicLoaderMacOSXDYLD::SetNotificationBreakpoint() { |
1015 | DEBUG_PRINTF("DynamicLoaderMacOSXDYLD::%s() process state = %s\n" , |
1016 | __FUNCTION__, StateAsCString(m_process->GetState())); |
1017 | if (m_break_id == LLDB_INVALID_BREAK_ID) { |
1018 | if (m_dyld_all_image_infos.notification != LLDB_INVALID_ADDRESS) { |
1019 | Address so_addr; |
1020 | // Set the notification breakpoint and install a breakpoint callback |
1021 | // function that will get called each time the breakpoint gets hit. We |
1022 | // will use this to track when shared libraries get loaded/unloaded. |
1023 | bool resolved = m_process->GetTarget().ResolveLoadAddress( |
1024 | load_addr: m_dyld_all_image_infos.notification, so_addr); |
1025 | if (!resolved) { |
1026 | ModuleSP dyld_module_sp = GetDYLDModule(); |
1027 | if (dyld_module_sp) { |
1028 | std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); |
1029 | |
1030 | UpdateImageLoadAddress(module: dyld_module_sp.get(), info&: m_dyld); |
1031 | resolved = m_process->GetTarget().ResolveLoadAddress( |
1032 | load_addr: m_dyld_all_image_infos.notification, so_addr); |
1033 | } |
1034 | } |
1035 | |
1036 | if (resolved) { |
1037 | Breakpoint *dyld_break = |
1038 | m_process->GetTarget().CreateBreakpoint(addr: so_addr, internal: true, request_hardware: false).get(); |
1039 | dyld_break->SetCallback(callback: DynamicLoaderMacOSXDYLD::NotifyBreakpointHit, |
1040 | baton: this, is_synchronous: true); |
1041 | dyld_break->SetBreakpointKind("shared-library-event" ); |
1042 | m_break_id = dyld_break->GetID(); |
1043 | } |
1044 | } |
1045 | } |
1046 | return m_break_id != LLDB_INVALID_BREAK_ID; |
1047 | } |
1048 | |
1049 | Status DynamicLoaderMacOSXDYLD::CanLoadImage() { |
1050 | Status error; |
1051 | // In order for us to tell if we can load a shared library we verify that the |
1052 | // dylib_info_addr isn't zero (which means no shared libraries have been set |
1053 | // yet, or dyld is currently mucking with the shared library list). |
1054 | if (ReadAllImageInfosStructure()) { |
1055 | // TODO: also check the _dyld_global_lock_held variable in |
1056 | // libSystem.B.dylib? |
1057 | // TODO: check the malloc lock? |
1058 | // TODO: check the objective C lock? |
1059 | if (m_dyld_all_image_infos.dylib_info_addr != 0) |
1060 | return error; // Success |
1061 | } |
1062 | |
1063 | error.SetErrorString("unsafe to load or unload shared libraries" ); |
1064 | return error; |
1065 | } |
1066 | |
1067 | bool DynamicLoaderMacOSXDYLD::GetSharedCacheInformation( |
1068 | lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache, |
1069 | LazyBool &private_shared_cache) { |
1070 | base_address = LLDB_INVALID_ADDRESS; |
1071 | uuid.Clear(); |
1072 | using_shared_cache = eLazyBoolCalculate; |
1073 | private_shared_cache = eLazyBoolCalculate; |
1074 | |
1075 | if (m_process) { |
1076 | addr_t all_image_infos = m_process->GetImageInfoAddress(); |
1077 | |
1078 | // The address returned by GetImageInfoAddress may be the address of dyld |
1079 | // (don't want) or it may be the address of the dyld_all_image_infos |
1080 | // structure (want). The first four bytes will be either the version field |
1081 | // (all_image_infos) or a Mach-O file magic constant. Version 13 and higher |
1082 | // of dyld_all_image_infos is required to get the sharedCacheUUID field. |
1083 | |
1084 | Status err; |
1085 | uint32_t version_or_magic = |
1086 | m_process->ReadUnsignedIntegerFromMemory(load_addr: all_image_infos, byte_size: 4, fail_value: -1, error&: err); |
1087 | if (version_or_magic != static_cast<uint32_t>(-1) && |
1088 | version_or_magic != llvm::MachO::MH_MAGIC && |
1089 | version_or_magic != llvm::MachO::MH_CIGAM && |
1090 | version_or_magic != llvm::MachO::MH_MAGIC_64 && |
1091 | version_or_magic != llvm::MachO::MH_CIGAM_64 && |
1092 | version_or_magic >= 13) { |
1093 | addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS; |
1094 | int wordsize = m_process->GetAddressByteSize(); |
1095 | if (wordsize == 8) { |
1096 | sharedCacheUUID_address = |
1097 | all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h> |
1098 | } |
1099 | if (wordsize == 4) { |
1100 | sharedCacheUUID_address = |
1101 | all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h> |
1102 | } |
1103 | if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS) { |
1104 | uuid_t shared_cache_uuid; |
1105 | if (m_process->ReadMemory(vm_addr: sharedCacheUUID_address, buf: shared_cache_uuid, |
1106 | size: sizeof(uuid_t), error&: err) == sizeof(uuid_t)) { |
1107 | uuid = UUID(shared_cache_uuid, 16); |
1108 | if (uuid.IsValid()) { |
1109 | using_shared_cache = eLazyBoolYes; |
1110 | } |
1111 | } |
1112 | |
1113 | if (version_or_magic >= 15) { |
1114 | // The sharedCacheBaseAddress field is the next one in the |
1115 | // dyld_all_image_infos struct. |
1116 | addr_t sharedCacheBaseAddr_address = sharedCacheUUID_address + 16; |
1117 | Status error; |
1118 | base_address = m_process->ReadUnsignedIntegerFromMemory( |
1119 | load_addr: sharedCacheBaseAddr_address, byte_size: wordsize, LLDB_INVALID_ADDRESS, |
1120 | error); |
1121 | if (error.Fail()) |
1122 | base_address = LLDB_INVALID_ADDRESS; |
1123 | } |
1124 | |
1125 | return true; |
1126 | } |
1127 | |
1128 | // |
1129 | // add |
1130 | // NB: sharedCacheBaseAddress is the next field in dyld_all_image_infos |
1131 | // after |
1132 | // sharedCacheUUID -- that is, 16 bytes after it, if we wanted to fetch |
1133 | // it. |
1134 | } |
1135 | } |
1136 | return false; |
1137 | } |
1138 | |
1139 | bool DynamicLoaderMacOSXDYLD::IsFullyInitialized() { |
1140 | if (ReadAllImageInfosStructure()) |
1141 | return m_dyld_all_image_infos.libSystemInitialized; |
1142 | return false; |
1143 | } |
1144 | |
1145 | void DynamicLoaderMacOSXDYLD::Initialize() { |
1146 | PluginManager::RegisterPlugin(name: GetPluginNameStatic(), |
1147 | description: GetPluginDescriptionStatic(), create_callback: CreateInstance); |
1148 | DynamicLoaderMacOS::Initialize(); |
1149 | } |
1150 | |
1151 | void DynamicLoaderMacOSXDYLD::Terminate() { |
1152 | DynamicLoaderMacOS::Terminate(); |
1153 | PluginManager::UnregisterPlugin(create_callback: CreateInstance); |
1154 | } |
1155 | |
1156 | llvm::StringRef DynamicLoaderMacOSXDYLD::GetPluginDescriptionStatic() { |
1157 | return "Dynamic loader plug-in that watches for shared library loads/unloads " |
1158 | "in MacOSX user processes." ; |
1159 | } |
1160 | |
1161 | uint32_t DynamicLoaderMacOSXDYLD::AddrByteSize() { |
1162 | std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); |
1163 | |
1164 | switch (m_dyld.header.magic) { |
1165 | case llvm::MachO::MH_MAGIC: |
1166 | case llvm::MachO::MH_CIGAM: |
1167 | return 4; |
1168 | |
1169 | case llvm::MachO::MH_MAGIC_64: |
1170 | case llvm::MachO::MH_CIGAM_64: |
1171 | return 8; |
1172 | |
1173 | default: |
1174 | break; |
1175 | } |
1176 | return 0; |
1177 | } |
1178 | |
1179 | lldb::ByteOrder DynamicLoaderMacOSXDYLD::GetByteOrderFromMagic(uint32_t magic) { |
1180 | switch (magic) { |
1181 | case llvm::MachO::MH_MAGIC: |
1182 | case llvm::MachO::MH_MAGIC_64: |
1183 | return endian::InlHostByteOrder(); |
1184 | |
1185 | case llvm::MachO::MH_CIGAM: |
1186 | case llvm::MachO::MH_CIGAM_64: |
1187 | if (endian::InlHostByteOrder() == lldb::eByteOrderBig) |
1188 | return lldb::eByteOrderLittle; |
1189 | else |
1190 | return lldb::eByteOrderBig; |
1191 | |
1192 | default: |
1193 | break; |
1194 | } |
1195 | return lldb::eByteOrderInvalid; |
1196 | } |
1197 | |