blob: 4c2b02f376e331491e04e96cb06e75af5b8db20f [file] [log] [blame]
Greg Clayton944b8282011-08-22 22:30:57 +00001//===-- DynamicLoaderDarwinKernel.cpp -----------------------------*- C++ -*-===//
Greg Clayton7b242382011-07-08 00:48:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Jim Ingham46d005d2014-04-02 22:53:21 +000010#include "lldb/Utility/SafeMachO.h"
Greg Claytoneadcca92014-04-02 20:36:22 +000011
Greg Clayton7b242382011-07-08 00:48:09 +000012#include "lldb/Breakpoint/StoppointCallbackContext.h"
13#include "lldb/Core/DataBuffer.h"
14#include "lldb/Core/DataBufferHeap.h"
Greg Clayton07e66e32011-07-20 03:41:06 +000015#include "lldb/Core/Debugger.h"
Greg Clayton7b242382011-07-08 00:48:09 +000016#include "lldb/Core/Log.h"
17#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/ModuleSpec.h"
Greg Clayton7b242382011-07-08 00:48:09 +000019#include "lldb/Core/PluginManager.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Core/Section.h"
Greg Clayton7b242382011-07-08 00:48:09 +000021#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000022#include "lldb/Core/StreamFile.h"
Jason Molenda68b36072012-10-02 03:49:41 +000023#include "lldb/Host/Symbols.h"
Ilia K41204d02015-03-04 12:05:24 +000024#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton7b242382011-07-08 00:48:09 +000025#include "lldb/Symbol/ObjectFile.h"
Greg Clayton7b242382011-07-08 00:48:09 +000026#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000027#include "lldb/Target/StackFrame.h"
Greg Clayton7b242382011-07-08 00:48:09 +000028#include "lldb/Target/Target.h"
29#include "lldb/Target/Thread.h"
30#include "lldb/Target/ThreadPlanRunToAddress.h"
Greg Clayton57abc5d2013-05-10 21:47:16 +000031#include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000032
Greg Clayton944b8282011-08-22 22:30:57 +000033#include "DynamicLoaderDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000034
35//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
36#ifdef ENABLE_DEBUG_PRINTF
37#include <stdio.h>
38#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
39#else
40#define DEBUG_PRINTF(fmt, ...)
41#endif
42
43using namespace lldb;
44using namespace lldb_private;
45
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000046// Progressively greater amounts of scanning we will allow
47// For some targets very early in startup, we can't do any random reads of memory or we can crash the device
48// so a setting is needed that can completely disable the KASLR scans.
49
50enum KASLRScanType
51{
52 eKASLRScanNone = 0, // No reading into the inferior at all
53 eKASLRScanLowgloAddresses, // Check one word of memory for a possible kernel addr, then see if a kernel is there
Jason Molenda63969222013-01-30 04:48:16 +000054 eKASLRScanNearPC, // Scan backwards from the current $pc looking for kernel; checking at 96 locations total
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000055 eKASLRScanExhaustiveScan // Scan through the entire possible kernel address range looking for a kernel
56};
57
58OptionEnumValueElement
59g_kaslr_kernel_scan_enum_values[] =
60{
61 { eKASLRScanNone, "none", "Do not read memory looking for a Darwin kernel when attaching." },
62 { eKASLRScanLowgloAddresses, "basic", "Check for the Darwin kernel's load addr in the lowglo page (boot-args=debug) only." },
63 { eKASLRScanNearPC, "fast-scan", "Scan near the pc value on attach to find the Darwin kernel's load address."},
64 { eKASLRScanExhaustiveScan, "exhaustive-scan", "Scan through the entire potential address range of Darwin kernel (only on 32-bit targets)."},
65 { 0, NULL, NULL }
66};
67
Greg Claytone8cd0c92012-10-19 18:02:49 +000068static PropertyDefinition
69g_properties[] =
70{
Greg Clayton66763ee2012-10-19 20:53:18 +000071 { "load-kexts" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Automatically loads kext images when attaching to a kernel." },
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000072 { "scan-type", OptionValue::eTypeEnum, true, eKASLRScanNearPC, NULL, g_kaslr_kernel_scan_enum_values, "Control how many reads lldb will make while searching for a Darwin kernel on attach." },
Greg Clayton66763ee2012-10-19 20:53:18 +000073 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
Greg Claytone8cd0c92012-10-19 18:02:49 +000074};
75
76enum {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000077 ePropertyLoadKexts,
78 ePropertyScanType
Greg Claytone8cd0c92012-10-19 18:02:49 +000079};
80
81class DynamicLoaderDarwinKernelProperties : public Properties
82{
83public:
84
85 static ConstString &
86 GetSettingName ()
87 {
Greg Clayton468ea4e2012-10-19 18:14:47 +000088 static ConstString g_setting_name("darwin-kernel");
Greg Claytone8cd0c92012-10-19 18:02:49 +000089 return g_setting_name;
90 }
91
92 DynamicLoaderDarwinKernelProperties() :
93 Properties ()
94 {
95 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
96 m_collection_sp->Initialize(g_properties);
97 }
98
99 virtual
100 ~DynamicLoaderDarwinKernelProperties()
101 {
102 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000103
Greg Claytone8cd0c92012-10-19 18:02:49 +0000104 bool
Greg Clayton66763ee2012-10-19 20:53:18 +0000105 GetLoadKexts() const
Greg Claytone8cd0c92012-10-19 18:02:49 +0000106 {
Greg Clayton66763ee2012-10-19 20:53:18 +0000107 const uint32_t idx = ePropertyLoadKexts;
Greg Claytone8cd0c92012-10-19 18:02:49 +0000108 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
109 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000110
111 KASLRScanType
112 GetScanType() const
113 {
114 const uint32_t idx = ePropertyScanType;
Jason Molenda63969222013-01-30 04:48:16 +0000115 return (KASLRScanType) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000116 }
117
118
Greg Claytone8cd0c92012-10-19 18:02:49 +0000119};
120
Greg Clayton7b0992d2013-04-18 22:45:39 +0000121typedef std::shared_ptr<DynamicLoaderDarwinKernelProperties> DynamicLoaderDarwinKernelPropertiesSP;
Greg Claytone8cd0c92012-10-19 18:02:49 +0000122
123static const DynamicLoaderDarwinKernelPropertiesSP &
124GetGlobalProperties()
125{
126 static DynamicLoaderDarwinKernelPropertiesSP g_settings_sp;
127 if (!g_settings_sp)
128 g_settings_sp.reset (new DynamicLoaderDarwinKernelProperties ());
129 return g_settings_sp;
130}
131
Greg Clayton7b242382011-07-08 00:48:09 +0000132//----------------------------------------------------------------------
133// Create an instance of this class. This function is filled into
134// the plugin info class that gets handed out by the plugin factory and
135// allows the lldb to instantiate an instance of this class.
136//----------------------------------------------------------------------
137DynamicLoader *
Greg Clayton944b8282011-08-22 22:30:57 +0000138DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force)
Greg Clayton7b242382011-07-08 00:48:09 +0000139{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000140 if (!force)
Greg Clayton7b242382011-07-08 00:48:09 +0000141 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000142 // If the user provided an executable binary and it is not a kernel,
143 // this plugin should not create an instance.
Greg Claytonaa149cb2011-08-11 02:48:45 +0000144 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
Greg Claytondf0b7d52011-07-08 04:11:42 +0000145 if (exe_module)
146 {
147 ObjectFile *object_file = exe_module->GetObjectFile();
148 if (object_file)
149 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000150 if (object_file->GetStrata() != ObjectFile::eStrataKernel)
151 {
152 return NULL;
153 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000154 }
155 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000156
157 // If the target's architecture does not look like an Apple environment,
158 // this plugin should not create an instance.
159 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
160 switch (triple_ref.getOS())
Greg Claytondf0b7d52011-07-08 04:11:42 +0000161 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000162 case llvm::Triple::Darwin:
163 case llvm::Triple::MacOSX:
164 case llvm::Triple::IOS:
Jason Molendaa814f702015-11-05 23:03:44 +0000165 case llvm::Triple::TvOS:
166 case llvm::Triple::WatchOS:
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000167 if (triple_ref.getVendor() != llvm::Triple::Apple)
168 {
169 return NULL;
170 }
171 break;
172 // If we have triple like armv7-unknown-unknown, we should try looking for a Darwin kernel.
173 case llvm::Triple::UnknownOS:
174 break;
175 default:
176 return NULL;
177 break;
178 }
179 }
180
181 // At this point if there is an ExecutableModule, it is a kernel and the Target is some variant of an Apple system.
182 // If the Process hasn't provided the kernel load address, we need to look around in memory to find it.
183
Jason Molenda503d0182013-03-02 07:19:32 +0000184 addr_t kernel_load_address = SearchForDarwinKernel (process);
185 if (kernel_load_address != LLDB_INVALID_ADDRESS)
186 {
Ewan Crawford90ff7912015-07-14 10:56:58 +0000187 process->SetCanRunCode(false);
Jason Molenda503d0182013-03-02 07:19:32 +0000188 return new DynamicLoaderDarwinKernel (process, kernel_load_address);
189 }
190 return NULL;
191}
192
193lldb::addr_t
194DynamicLoaderDarwinKernel::SearchForDarwinKernel (Process *process)
195{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000196 addr_t kernel_load_address = process->GetImageInfoAddress();
197 if (kernel_load_address == LLDB_INVALID_ADDRESS)
198 {
199 kernel_load_address = SearchForKernelAtSameLoadAddr (process);
200 if (kernel_load_address == LLDB_INVALID_ADDRESS)
201 {
202 kernel_load_address = SearchForKernelWithDebugHints (process);
203 if (kernel_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton70512312012-05-08 01:45:38 +0000204 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000205 kernel_load_address = SearchForKernelNearPC (process);
206 if (kernel_load_address == LLDB_INVALID_ADDRESS)
207 {
208 kernel_load_address = SearchForKernelViaExhaustiveSearch (process);
209 }
Greg Clayton70512312012-05-08 01:45:38 +0000210 }
Greg Claytondf0b7d52011-07-08 04:11:42 +0000211 }
Greg Clayton7b242382011-07-08 00:48:09 +0000212 }
Jason Molenda503d0182013-03-02 07:19:32 +0000213 return kernel_load_address;
Greg Clayton7b242382011-07-08 00:48:09 +0000214}
215
216//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000217// Check if the kernel binary is loaded in memory without a slide.
218// First verify that the ExecutableModule is a kernel before we proceed.
219// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
220//----------------------------------------------------------------------
221lldb::addr_t
222DynamicLoaderDarwinKernel::SearchForKernelAtSameLoadAddr (Process *process)
223{
224 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
225 if (exe_module == NULL)
226 return LLDB_INVALID_ADDRESS;
227
228 ObjectFile *exe_objfile = exe_module->GetObjectFile();
229 if (exe_objfile == NULL)
230 return LLDB_INVALID_ADDRESS;
231
232 if (exe_objfile->GetType() != ObjectFile::eTypeExecutable || exe_objfile->GetStrata() != ObjectFile::eStrataKernel)
233 return LLDB_INVALID_ADDRESS;
234
235 if (!exe_objfile->GetHeaderAddress().IsValid())
236 return LLDB_INVALID_ADDRESS;
237
238 if (CheckForKernelImageAtAddress (exe_objfile->GetHeaderAddress().GetFileAddress(), process) == exe_module->GetUUID())
239 return exe_objfile->GetHeaderAddress().GetFileAddress();
240
241 return LLDB_INVALID_ADDRESS;
242}
243
244//----------------------------------------------------------------------
245// If the debug flag is included in the boot-args nvram setting, the kernel's load address
246// will be noted in the lowglo page at a fixed address
247// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
248//----------------------------------------------------------------------
249lldb::addr_t
250DynamicLoaderDarwinKernel::SearchForKernelWithDebugHints (Process *process)
251{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000252 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone)
253 return LLDB_INVALID_ADDRESS;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000254
255 Error read_err;
256 addr_t addr = LLDB_INVALID_ADDRESS;
Jason Molenda880988a2016-02-06 04:55:26 +0000257 addr_t kernel_addresses_64[] = { 0xffffff8000002010ULL, 0xffffff8000004010ULL,
258 0xfffffff000002010ULL, 0xfffffff000004010ULL,
259 LLDB_INVALID_ADDRESS };
260 addr_t kernel_addresses_32[] = { 0xffff0110,
261 LLDB_INVALID_ADDRESS };
262 for (size_t i = 0; kernel_addresses_64[i] != LLDB_INVALID_ADDRESS; i++)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000263 {
Jason Molenda880988a2016-02-06 04:55:26 +0000264 addr = process->ReadUnsignedIntegerFromMemory (kernel_addresses_64[i], 8, LLDB_INVALID_ADDRESS, read_err);
Jason Molendaec504232016-02-05 01:38:56 +0000265 if (CheckForKernelImageAtAddress (addr, process).IsValid())
266 {
267 return addr;
268 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000269 }
Jason Molenda880988a2016-02-06 04:55:26 +0000270
271 for (size_t i = 0; kernel_addresses_32[i] != LLDB_INVALID_ADDRESS; i++)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000272 {
Jason Molenda880988a2016-02-06 04:55:26 +0000273 addr = process->ReadUnsignedIntegerFromMemory (kernel_addresses_32[i], 4, LLDB_INVALID_ADDRESS, read_err);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000274 if (CheckForKernelImageAtAddress (addr, process).IsValid())
Jason Molenda7dd29392014-10-06 22:23:30 +0000275 {
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000276 return addr;
Jason Molenda7dd29392014-10-06 22:23:30 +0000277 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000278 }
Jason Molenda880988a2016-02-06 04:55:26 +0000279
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000280 return LLDB_INVALID_ADDRESS;
281}
282
283//----------------------------------------------------------------------
284// If the kernel is currently executing when lldb attaches, and we don't have
285// a better way of finding the kernel's load address, try searching backwards
286// from the current pc value looking for the kernel's Mach header in memory.
287// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
288//----------------------------------------------------------------------
289lldb::addr_t
290DynamicLoaderDarwinKernel::SearchForKernelNearPC (Process *process)
291{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000292 if (GetGlobalProperties()->GetScanType() == eKASLRScanNone
293 || GetGlobalProperties()->GetScanType() == eKASLRScanLowgloAddresses)
294 {
295 return LLDB_INVALID_ADDRESS;
296 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000297
298 ThreadSP thread = process->GetThreadList().GetSelectedThread ();
299 if (thread.get() == NULL)
300 return LLDB_INVALID_ADDRESS;
301 addr_t pc = thread->GetRegisterContext ()->GetPC(LLDB_INVALID_ADDRESS);
302
303 if (pc == LLDB_INVALID_ADDRESS)
304 return LLDB_INVALID_ADDRESS;
305
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000306 // The kernel will load at at one megabyte boundary (0x100000), or at that boundary plus
Jason Molenda880988a2016-02-06 04:55:26 +0000307 // an offset of one page (0x1000) or two, or four (0x4000), depending on the device.
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000308
309 // Round the current pc down to the nearest one megabyte boundary - the place where we will start searching.
310 addr_t addr = pc & ~0xfffff;
311
Jason Molenda880988a2016-02-06 04:55:26 +0000312 // Search backwards 32 megabytes, looking for the start of the kernel at each one-megabyte boundary.
313 for (int i = 0; i < 32; i++, addr -= 0x100000)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000314 {
315 if (CheckForKernelImageAtAddress (addr, process).IsValid())
316 return addr;
317 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
318 return addr + 0x1000;
319 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
320 return addr + 0x2000;
Jason Molendaa02869d2014-07-31 06:07:04 +0000321 if (CheckForKernelImageAtAddress (addr + 0x4000, process).IsValid())
322 return addr + 0x4000;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000323 }
324
325 return LLDB_INVALID_ADDRESS;
326}
327
328//----------------------------------------------------------------------
329// Scan through the valid address range for a kernel binary.
330// This is uselessly slow in 64-bit environments so we don't even try it.
331// This scan is not enabled by default even for 32-bit targets.
332// Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
333//----------------------------------------------------------------------
334lldb::addr_t
335DynamicLoaderDarwinKernel::SearchForKernelViaExhaustiveSearch (Process *process)
336{
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000337 if (GetGlobalProperties()->GetScanType() != eKASLRScanExhaustiveScan)
338 {
339 return LLDB_INVALID_ADDRESS;
340 }
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000341
342 addr_t kernel_range_low, kernel_range_high;
343 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
344 {
345 kernel_range_low = 1ULL << 63;
346 kernel_range_high = UINT64_MAX;
347 }
348 else
349 {
350 kernel_range_low = 1ULL << 31;
351 kernel_range_high = UINT32_MAX;
352 }
353
354 // Stepping through memory at one-megabyte resolution looking for a kernel
355 // rarely works (fast enough) with a 64-bit address space -- for now, let's
356 // not even bother. We may be attaching to something which *isn't* a kernel
357 // and we don't want to spin for minutes on-end looking for a kernel.
358 if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
359 return LLDB_INVALID_ADDRESS;
360
361 addr_t addr = kernel_range_low;
362
363 while (addr >= kernel_range_low && addr < kernel_range_high)
364 {
365 if (CheckForKernelImageAtAddress (addr, process).IsValid())
366 return addr;
367 if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
368 return addr + 0x1000;
369 if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
370 return addr + 0x2000;
Jason Molendaa02869d2014-07-31 06:07:04 +0000371 if (CheckForKernelImageAtAddress (addr + 0x4000, process).IsValid())
372 return addr + 0x4000;
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000373 addr += 0x100000;
374 }
375 return LLDB_INVALID_ADDRESS;
376}
377
378//----------------------------------------------------------------------
379// Given an address in memory, look to see if there is a kernel image at that
380// address.
381// Returns a UUID; if a kernel was not found at that address, UUID.IsValid() will be false.
382//----------------------------------------------------------------------
383lldb_private::UUID
384DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress (lldb::addr_t addr, Process *process)
385{
Jason Molendaec504232016-02-05 01:38:56 +0000386 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000387 if (addr == LLDB_INVALID_ADDRESS)
388 return UUID();
389
Jason Molendaec504232016-02-05 01:38:56 +0000390 if (log)
391 log->Printf ("DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress: looking for kernel binary at 0x%" PRIx64, addr);
392
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000393 // First try a quick test -- read the first 4 bytes and see if there is a valid Mach-O magic field there
394 // (the first field of the mach_header/mach_header_64 struct).
395
396 Error read_error;
397 uint64_t result = process->ReadUnsignedIntegerFromMemory (addr, 4, LLDB_INVALID_ADDRESS, read_error);
Charles Davis510938e2013-08-27 05:04:57 +0000398 if (result != llvm::MachO::MH_MAGIC_64
399 && result != llvm::MachO::MH_MAGIC
400 && result != llvm::MachO::MH_CIGAM
401 && result != llvm::MachO::MH_CIGAM_64)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000402 {
403 return UUID();
404 }
405
406 // Read the mach header and see whether it looks like a kernel
407 llvm::MachO::mach_header header;
Jason Molenda880988a2016-02-06 04:55:26 +0000408 if (process->DoReadMemory (addr, &header, sizeof (header), read_error) != sizeof (header))
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000409 return UUID();
410
Charles Davis510938e2013-08-27 05:04:57 +0000411 if (header.magic == llvm::MachO::MH_CIGAM ||
412 header.magic == llvm::MachO::MH_CIGAM_64)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000413 {
Jason Molenda880988a2016-02-06 04:55:26 +0000414 header.magic = llvm::ByteSwap_32 (header.magic);
415 header.cputype = llvm::ByteSwap_32 (header.cputype);
416 header.cpusubtype = llvm::ByteSwap_32 (header.cpusubtype);
417 header.filetype = llvm::ByteSwap_32 (header.filetype);
418 header.ncmds = llvm::ByteSwap_32 (header.ncmds);
419 header.sizeofcmds = llvm::ByteSwap_32 (header.sizeofcmds);
420 header.flags = llvm::ByteSwap_32 (header.flags);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000421 }
422
423 // A kernel is an executable which does not have the dynamic link object flag set.
Charles Davis510938e2013-08-27 05:04:57 +0000424 if (header.filetype == llvm::MachO::MH_EXECUTE
425 && (header.flags & llvm::MachO::MH_DYLDLINK) == 0)
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000426 {
427 // Create a full module to get the UUID
Greg Clayton39f7ee82013-02-01 21:38:35 +0000428 ModuleSP memory_module_sp = process->ReadModuleFromMemory (FileSpec ("temp_mach_kernel", false), addr);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000429 if (!memory_module_sp.get())
430 return UUID();
431
432 ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
433 if (exe_objfile == NULL)
434 return UUID();
435
436 if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
437 {
Jason Molendaa4ce2532013-05-02 22:02:57 +0000438 ArchSpec kernel_arch (eArchTypeMachO, header.cputype, header.cpusubtype);
439 if (!process->GetTarget().GetArchitecture().IsCompatibleMatch(kernel_arch))
440 {
441 process->GetTarget().SetArchitecture (kernel_arch);
442 }
Jason Molendaec504232016-02-05 01:38:56 +0000443 if (log)
444 log->Printf ("DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress: kernel binary image found at 0x%" PRIx64, addr);
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000445 return memory_module_sp->GetUUID();
446 }
447 }
448
449 return UUID();
450}
451
452//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000453// Constructor
454//----------------------------------------------------------------------
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000455DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process, lldb::addr_t kernel_addr) :
Greg Clayton7b242382011-07-08 00:48:09 +0000456 DynamicLoader(process),
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000457 m_kernel_load_address (kernel_addr),
Greg Clayton7b242382011-07-08 00:48:09 +0000458 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +0000459 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +0000460 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +0000461 m_kext_summary_header (),
Jason Molenda306bd0a2013-02-19 05:42:46 +0000462 m_known_kexts (),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000463 m_mutex(Mutex::eMutexTypeRecursive),
464 m_break_id (LLDB_INVALID_BREAK_ID)
Greg Clayton7b242382011-07-08 00:48:09 +0000465{
Greg Clayton615eb7e2014-09-19 20:11:50 +0000466 Error error;
467 PlatformSP platform_sp(Platform::Create(PlatformDarwinKernel::GetPluginNameStatic(), error));
Jason Molenda1c627542013-04-05 01:03:25 +0000468 // Only select the darwin-kernel Platform if we've been asked to load kexts.
469 // It can take some time to scan over all of the kext info.plists and that
470 // shouldn't be done if kext loading is explicitly disabled.
471 if (platform_sp.get() && GetGlobalProperties()->GetLoadKexts())
472 {
473 process->GetTarget().SetPlatform (platform_sp);
Jason Molenda1c627542013-04-05 01:03:25 +0000474 }
Greg Clayton7b242382011-07-08 00:48:09 +0000475}
476
477//----------------------------------------------------------------------
478// Destructor
479//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +0000480DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +0000481{
482 Clear(true);
483}
484
Greg Clayton374972e2011-07-09 17:15:55 +0000485void
Greg Clayton944b8282011-08-22 22:30:57 +0000486DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000487{
488 LoadKernelModuleIfNeeded();
489 SetNotificationBreakpointIfNeeded ();
490}
Greg Clayton7b242382011-07-08 00:48:09 +0000491//------------------------------------------------------------------
492/// Called after attaching a process.
493///
494/// Allow DynamicLoader plug-ins to execute some code after
495/// attaching to a process.
496//------------------------------------------------------------------
497void
Greg Clayton944b8282011-08-22 22:30:57 +0000498DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000499{
500 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000501 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000502}
503
504//------------------------------------------------------------------
505/// Called after attaching a process.
506///
507/// Allow DynamicLoader plug-ins to execute some code after
508/// attaching to a process.
509//------------------------------------------------------------------
510void
Greg Clayton944b8282011-08-22 22:30:57 +0000511DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000512{
513 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000514 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000515}
516
517
518//----------------------------------------------------------------------
519// Clear out the state of this class.
520//----------------------------------------------------------------------
521void
Greg Clayton944b8282011-08-22 22:30:57 +0000522DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000523{
524 Mutex::Locker locker(m_mutex);
525
526 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
527 m_process->ClearBreakpointSiteByID(m_break_id);
528
529 if (clear_process)
530 m_process = NULL;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000531 m_kernel.Clear();
532 m_known_kexts.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000533 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000534 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000535 m_break_id = LLDB_INVALID_BREAK_ID;
536}
537
Greg Clayton7b242382011-07-08 00:48:09 +0000538
Greg Claytonc859e2d2012-02-13 23:10:39 +0000539bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000540DynamicLoaderDarwinKernel::KextImageInfo::LoadImageAtFileAddress (Process *process)
Greg Clayton2af282a2012-03-21 04:25:00 +0000541{
542 if (IsLoaded())
543 return true;
544
Jason Molenda306bd0a2013-02-19 05:42:46 +0000545 if (m_module_sp)
Greg Clayton2af282a2012-03-21 04:25:00 +0000546 {
547 bool changed = false;
Greg Clayton751caf62014-02-07 22:54:47 +0000548 if (m_module_sp->SetLoadAddress (process->GetTarget(), 0, true, changed))
Jason Molenda306bd0a2013-02-19 05:42:46 +0000549 m_load_process_stop_id = process->GetStopID();
Greg Clayton2af282a2012-03-21 04:25:00 +0000550 }
551 return false;
552}
553
Jason Molenda306bd0a2013-02-19 05:42:46 +0000554void
555DynamicLoaderDarwinKernel::KextImageInfo::SetModule (ModuleSP module_sp)
556{
557 m_module_sp = module_sp;
558 if (module_sp.get() && module_sp->GetObjectFile())
559 {
560 if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
561 && module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
562 {
563 m_kernel_image = true;
564 }
565 else
566 {
567 m_kernel_image = false;
568 }
569 }
570}
571
572ModuleSP
573DynamicLoaderDarwinKernel::KextImageInfo::GetModule ()
574{
575 return m_module_sp;
576}
577
578void
579DynamicLoaderDarwinKernel::KextImageInfo::SetLoadAddress (addr_t load_addr)
580{
581 m_load_address = load_addr;
582}
583
584addr_t
585DynamicLoaderDarwinKernel::KextImageInfo::GetLoadAddress () const
586{
587 return m_load_address;
588}
589
590uint64_t
591DynamicLoaderDarwinKernel::KextImageInfo::GetSize () const
592{
593 return m_size;
594}
595
596void
597DynamicLoaderDarwinKernel::KextImageInfo::SetSize (uint64_t size)
598{
599 m_size = size;
600}
601
602uint32_t
603DynamicLoaderDarwinKernel::KextImageInfo::GetProcessStopId () const
604{
605 return m_load_process_stop_id;
606}
607
608void
609DynamicLoaderDarwinKernel::KextImageInfo::SetProcessStopId (uint32_t stop_id)
610{
611 m_load_process_stop_id = stop_id;
612}
613
Greg Clayton2af282a2012-03-21 04:25:00 +0000614bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000615DynamicLoaderDarwinKernel::KextImageInfo::operator== (const KextImageInfo &rhs)
616{
617 if (m_uuid.IsValid() || rhs.GetUUID().IsValid())
618 {
619 if (m_uuid == rhs.GetUUID())
620 {
621 return true;
622 }
623 return false;
624 }
625
626 if (m_name == rhs.GetName() && m_load_address == rhs.GetLoadAddress())
627 return true;
628
629 return false;
630}
631
632void
633DynamicLoaderDarwinKernel::KextImageInfo::SetName (const char *name)
634{
635 m_name = name;
636}
637
638std::string
639DynamicLoaderDarwinKernel::KextImageInfo::GetName () const
640{
641 return m_name;
642}
643
644void
645DynamicLoaderDarwinKernel::KextImageInfo::SetUUID (const UUID &uuid)
646{
647 m_uuid = uuid;
648}
649
650UUID
651DynamicLoaderDarwinKernel::KextImageInfo::GetUUID () const
652{
653 return m_uuid;
654}
655
656// Given the m_load_address from the kext summaries, and a UUID, try to create an in-memory
657// Module at that address. Require that the MemoryModule have a matching UUID and detect
658// if this MemoryModule is a kernel or a kext.
659//
660// Returns true if m_memory_module_sp is now set to a valid Module.
661
662bool
663DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule (Process *process)
664{
Jason Molenda08a32582015-07-25 02:39:42 +0000665 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000666 if (m_memory_module_sp.get() != NULL)
667 return true;
668 if (m_load_address == LLDB_INVALID_ADDRESS)
669 return false;
670
671 FileSpec file_spec;
672 file_spec.SetFile (m_name.c_str(), false);
673
674 ModuleSP memory_module_sp = process->ReadModuleFromMemory (file_spec, m_load_address);
675
676 if (memory_module_sp.get() == NULL)
677 return false;
678
679 bool is_kernel = false;
680 if (memory_module_sp->GetObjectFile())
681 {
682 if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
683 && memory_module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
684 {
685 is_kernel = true;
686 }
687 else if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeSharedLibrary)
688 {
689 is_kernel = false;
690 }
691 }
692
Jason Molenda38e70d12013-02-27 03:07:49 +0000693 // If this is a kext, and the kernel specified what UUID we should find at this
694 // load address, require that the memory module have a matching UUID or something
695 // has gone wrong and we should discard it.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000696 if (m_uuid.IsValid())
697 {
698 if (m_uuid != memory_module_sp->GetUUID())
699 {
Jason Molenda08a32582015-07-25 02:39:42 +0000700 if (log)
701 {
702 log->Printf ("KextImageInfo::ReadMemoryModule the kernel said to find uuid %s at 0x%" PRIx64 " but instead we found uuid %s, throwing it away", m_uuid.GetAsString().c_str(), m_load_address, memory_module_sp->GetUUID().GetAsString().c_str());
703 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000704 return false;
705 }
706 }
707
708 // If the in-memory Module has a UUID, let's use that.
709 if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid())
710 {
711 m_uuid = memory_module_sp->GetUUID();
712 }
713
714 m_memory_module_sp = memory_module_sp;
715 m_kernel_image = is_kernel;
716 if (is_kernel)
717 {
Jason Molenda08a32582015-07-25 02:39:42 +0000718 if (log)
719 {
720 // This is unusual and probably not intended
721 log->Printf ("KextImageInfo::ReadMemoryModule read the kernel binary out of memory");
722 }
Jason Molenda38e70d12013-02-27 03:07:49 +0000723 if (memory_module_sp->GetArchitecture().IsValid())
Jason Molenda306bd0a2013-02-19 05:42:46 +0000724 {
725 process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
726 }
Jason Molenda38e70d12013-02-27 03:07:49 +0000727 if (m_uuid.IsValid())
728 {
Jason Molenda48975832015-03-10 23:34:52 +0000729 ModuleSP exe_module_sp = process->GetTarget().GetExecutableModule();
730 if (exe_module_sp.get() && exe_module_sp->GetUUID().IsValid())
Jason Molenda38e70d12013-02-27 03:07:49 +0000731 {
Jason Molenda48975832015-03-10 23:34:52 +0000732 if (m_uuid != exe_module_sp->GetUUID())
Jason Molenda38e70d12013-02-27 03:07:49 +0000733 {
Jason Molenda48975832015-03-10 23:34:52 +0000734 // The user specified a kernel binary that has a different UUID than
735 // the kernel actually running in memory. This never ends well;
736 // clear the user specified kernel binary from the Target.
737
738 m_module_sp.reset();
739
740 ModuleList user_specified_kernel_list;
741 user_specified_kernel_list.Append (exe_module_sp);
742 process->GetTarget().GetImages().Remove (user_specified_kernel_list);
Jason Molenda38e70d12013-02-27 03:07:49 +0000743 }
744 }
745 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000746 }
747
748 return true;
749}
750
751bool
752DynamicLoaderDarwinKernel::KextImageInfo::IsKernel () const
753{
754 return m_kernel_image == true;
755}
756
757void
758DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel (bool is_kernel)
759{
760 m_kernel_image = is_kernel;
761}
762
763bool
764DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule (Process *process)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000765{
766 if (IsLoaded())
767 return true;
768
Greg Claytonc859e2d2012-02-13 23:10:39 +0000769
770 Target &target = process->GetTarget();
Jason Molenda87a04b22012-10-19 03:40:45 +0000771
Jason Molenda306bd0a2013-02-19 05:42:46 +0000772 // If we don't have / can't create a memory module for this kext, don't try to load it - we won't
773 // have the correct segment load addresses.
774 if (!ReadMemoryModule (process))
Jason Molenda87a04b22012-10-19 03:40:45 +0000775 {
776 return false;
777 }
778
Jason Molenda306bd0a2013-02-19 05:42:46 +0000779 bool uuid_is_valid = m_uuid.IsValid();
780
Jason Molendae575e7b2013-02-19 06:11:13 +0000781 if (IsKernel() && uuid_is_valid && m_memory_module_sp.get())
782 {
Greg Clayton44d93782014-01-27 23:43:24 +0000783 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molendae575e7b2013-02-19 06:11:13 +0000784 if (s)
785 {
Jason Molendac16b4af2013-05-03 23:56:12 +0000786 s->Printf ("Kernel UUID: %s\n", m_memory_module_sp->GetUUID().GetAsString().c_str());
Jason Molendae575e7b2013-02-19 06:11:13 +0000787 s->Printf ("Load Address: 0x%" PRIx64 "\n", m_load_address);
788 }
789 }
790
Jason Molenda306bd0a2013-02-19 05:42:46 +0000791 if (!m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000792 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000793 // See if the kext has already been loaded into the target, probably by the user doing target modules add.
794 const ModuleList &target_images = target.GetImages();
795 m_module_sp = target_images.FindModule(m_uuid);
796
797 // Search for the kext on the local filesystem via the UUID
798 if (!m_module_sp && uuid_is_valid)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000799 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000800 ModuleSpec module_spec;
801 module_spec.GetUUID() = m_uuid;
802 module_spec.GetArchitecture() = target.GetArchitecture();
Jason Molenda53667f52012-10-06 02:02:26 +0000803
Jason Molendad76fb6e2013-02-19 07:16:22 +0000804 // For the kernel, we really do need an on-disk file copy of the binary to do anything useful.
805 // This will force a clal to
Jason Molenda306bd0a2013-02-19 05:42:46 +0000806 if (IsKernel())
Greg Claytonb9a01b32012-02-26 05:51:37 +0000807 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000808 if (Symbols::DownloadObjectAndSymbolFile (module_spec, true))
Jason Molendabb860bd2012-10-09 01:17:11 +0000809 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000810 if (module_spec.GetFileSpec().Exists())
Jason Molendabb860bd2012-10-09 01:17:11 +0000811 {
Jason Molendad76fb6e2013-02-19 07:16:22 +0000812 m_module_sp.reset(new Module (module_spec.GetFileSpec(), target.GetArchitecture()));
813 if (m_module_sp.get() && m_module_sp->MatchesModuleSpec (module_spec))
814 {
815 ModuleList loaded_module_list;
816 loaded_module_list.Append (m_module_sp);
817 target.ModulesDidLoad (loaded_module_list);
818 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000819 }
820 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000821 }
Jason Molendad76fb6e2013-02-19 07:16:22 +0000822
Jason Molenda1c627542013-04-05 01:03:25 +0000823 // If the current platform is PlatformDarwinKernel, create a ModuleSpec with the filename set
824 // to be the bundle ID for this kext, e.g. "com.apple.filesystems.msdosfs", and ask the platform
825 // to find it.
826 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0ddfe7f2014-03-05 03:33:01 +0000827 if (!m_module_sp && platform_sp)
Jason Molenda1c627542013-04-05 01:03:25 +0000828 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000829 ConstString platform_name (platform_sp->GetPluginName());
830 static ConstString g_platform_name (PlatformDarwinKernel::GetPluginNameStatic());
831 if (platform_name == g_platform_name)
Jason Molenda1c627542013-04-05 01:03:25 +0000832 {
833 ModuleSpec kext_bundle_module_spec(module_spec);
834 FileSpec kext_filespec(m_name.c_str(), false);
835 kext_bundle_module_spec.GetFileSpec() = kext_filespec;
Ilia K667ef222015-03-24 14:30:28 +0000836 platform_sp->GetSharedModule (kext_bundle_module_spec, process, m_module_sp, &target.GetExecutableSearchPaths(), NULL, NULL);
Jason Molenda1c627542013-04-05 01:03:25 +0000837 }
838 }
839
Jason Molendad76fb6e2013-02-19 07:16:22 +0000840 // Ask the Target to find this file on the local system, if possible.
Jason Molenda306bd0a2013-02-19 05:42:46 +0000841 // This will search in the list of currently-loaded files, look in the
842 // standard search paths on the system, and on a Mac it will try calling
843 // the DebugSymbols framework with the UUID to find the binary via its
844 // search methods.
845 if (!m_module_sp)
846 {
847 m_module_sp = target.GetSharedModule (module_spec);
848 }
Jason Molendae575e7b2013-02-19 06:11:13 +0000849
Jason Molendad76fb6e2013-02-19 07:16:22 +0000850 if (IsKernel() && !m_module_sp)
Jason Molendae575e7b2013-02-19 06:11:13 +0000851 {
Greg Clayton44d93782014-01-27 23:43:24 +0000852 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molendae575e7b2013-02-19 06:11:13 +0000853 if (s)
854 {
Jason Molendacc6dc782013-04-30 22:38:28 +0000855 s->Printf ("WARNING: Unable to locate kernel binary on the debugger system.\n");
Jason Molendae575e7b2013-02-19 06:11:13 +0000856 }
857 }
Jason Molenda306bd0a2013-02-19 05:42:46 +0000858 }
Jason Molenda65d57a32012-12-01 06:13:29 +0000859
Jason Molenda306bd0a2013-02-19 05:42:46 +0000860 // If we managed to find a module, append it to the target's list of images.
861 // If we also have a memory module, require that they have matching UUIDs
862 if (m_module_sp)
863 {
864 bool uuid_match_ok = true;
865 if (m_memory_module_sp)
866 {
867 if (m_module_sp->GetUUID() != m_memory_module_sp->GetUUID())
Jason Molenda65d57a32012-12-01 06:13:29 +0000868 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000869 uuid_match_ok = false;
870 }
871 }
872 if (uuid_match_ok)
873 {
Jason Molendaa4d3e1d2013-02-19 07:41:13 +0000874 target.GetImages().AppendIfNeeded(m_module_sp);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000875 if (IsKernel() && target.GetExecutableModulePointer() != m_module_sp.get())
876 {
877 target.SetExecutableModule (m_module_sp, false);
Jason Molenda65d57a32012-12-01 06:13:29 +0000878 }
Greg Claytonb9a01b32012-02-26 05:51:37 +0000879 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000880 }
881 }
882
Jason Molenda56c23282013-02-19 06:39:56 +0000883 if (!m_module_sp && !IsKernel() && m_uuid.IsValid() && !m_name.empty())
884 {
Greg Clayton44d93782014-01-27 23:43:24 +0000885 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molenda56c23282013-02-19 06:39:56 +0000886 if (s)
887 {
Jason Molenda56c23282013-02-19 06:39:56 +0000888 s->Printf ("warning: Can't find binary/dSYM for %s (%s)\n",
Jason Molendac16b4af2013-05-03 23:56:12 +0000889 m_name.c_str(), m_uuid.GetAsString().c_str());
Jason Molenda56c23282013-02-19 06:39:56 +0000890 }
891 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000892
Greg Clayton67408532012-12-11 01:20:51 +0000893 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
894
Jason Molenda306bd0a2013-02-19 05:42:46 +0000895 if (m_memory_module_sp && m_module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000896 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000897 if (m_module_sp->GetUUID() == m_memory_module_sp->GetUUID())
Greg Claytonc859e2d2012-02-13 23:10:39 +0000898 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000899 ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
900 ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
Greg Clayton67408532012-12-11 01:20:51 +0000901
Jason Molendabb860bd2012-10-09 01:17:11 +0000902 if (memory_object_file && ondisk_object_file)
903 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000904 // The memory_module for kexts may have an invalid __LINKEDIT seg; skip it.
905 const bool ignore_linkedit = !IsKernel ();
Greg Clayton67408532012-12-11 01:20:51 +0000906
Jason Molendabb860bd2012-10-09 01:17:11 +0000907 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
908 SectionList *memory_section_list = memory_object_file->GetSectionList ();
909 if (memory_section_list && ondisk_section_list)
910 {
911 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
912 // There may be CTF sections in the memory image so we can't
913 // always just compare the number of sections (which are actually
914 // segments in mach-o parlance)
915 uint32_t sect_idx = 0;
916
917 // Use the memory_module's addresses for each section to set the
918 // file module's load address as appropriate. We don't want to use
919 // a single slide value for the entire kext - different segments may
920 // be slid different amounts by the kext loader.
921
922 uint32_t num_sections_loaded = 0;
923 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
924 {
925 SectionSP ondisk_section_sp(ondisk_section_list->GetSectionAtIndex(sect_idx));
926 if (ondisk_section_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000927 {
Greg Clayton67408532012-12-11 01:20:51 +0000928 // Don't ever load __LINKEDIT as it may or may not be actually
929 // mapped into memory and there is no current way to tell.
930 // I filed rdar://problem/12851706 to track being able to tell
931 // if the __LINKEDIT is actually mapped, but until then, we need
932 // to not load the __LINKEDIT
933 if (ignore_linkedit && ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
934 continue;
935
Jason Molendabb860bd2012-10-09 01:17:11 +0000936 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section_sp->GetName()).get();
937 if (memory_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000938 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000939 target.SetSectionLoadAddress (ondisk_section_sp, memory_section->GetFileAddress());
Jason Molendabb860bd2012-10-09 01:17:11 +0000940 ++num_sections_loaded;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000941 }
942 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000943 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000944 if (num_sections_loaded > 0)
Jason Molenda306bd0a2013-02-19 05:42:46 +0000945 m_load_process_stop_id = process->GetStopID();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000946 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000947 m_module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000948 }
949 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000950 m_module_sp.reset(); // One or both section lists
Greg Claytonc859e2d2012-02-13 23:10:39 +0000951 }
952 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000953 m_module_sp.reset(); // One or both object files missing
Greg Claytonc859e2d2012-02-13 23:10:39 +0000954 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000955 else
Jason Molenda306bd0a2013-02-19 05:42:46 +0000956 m_module_sp.reset(); // UUID mismatch
Greg Claytonc859e2d2012-02-13 23:10:39 +0000957 }
Jason Molendabb860bd2012-10-09 01:17:11 +0000958
Greg Claytonc859e2d2012-02-13 23:10:39 +0000959 bool is_loaded = IsLoaded();
960
Jason Molenda306bd0a2013-02-19 05:42:46 +0000961 if (is_loaded && m_module_sp && IsKernel())
Jason Molenda68b36072012-10-02 03:49:41 +0000962 {
Greg Clayton44d93782014-01-27 23:43:24 +0000963 Stream *s = target.GetDebugger().GetOutputFile().get();
Jason Molenda68b36072012-10-02 03:49:41 +0000964 if (s)
965 {
Jason Molenda732238c2013-03-01 00:06:37 +0000966 ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
967 if (kernel_object_file)
968 {
969 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
970 if (m_load_address != LLDB_INVALID_ADDRESS && file_address != LLDB_INVALID_ADDRESS)
971 {
Matt Kopec787d1622013-03-12 17:45:38 +0000972 s->Printf ("Kernel slid 0x%" PRIx64 " in memory.\n", m_load_address - file_address);
Jason Molenda732238c2013-03-01 00:06:37 +0000973 }
974 }
Jason Molenda743e43962012-10-02 22:23:42 +0000975 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000976 s->Printf ("Loaded kernel file %s\n",
977 m_module_sp->GetFileSpec().GetPath().c_str());
Jason Molenda743e43962012-10-02 22:23:42 +0000978 }
Jason Molenda68b36072012-10-02 03:49:41 +0000979 s->Flush ();
980 }
981 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000982 return is_loaded;
983}
984
Greg Clayton1f746072012-08-29 21:13:06 +0000985uint32_t
Jason Molenda306bd0a2013-02-19 05:42:46 +0000986DynamicLoaderDarwinKernel::KextImageInfo::GetAddressByteSize ()
Greg Clayton1f746072012-08-29 21:13:06 +0000987{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000988 if (m_memory_module_sp)
989 return m_memory_module_sp->GetArchitecture().GetAddressByteSize();
990 if (m_module_sp)
991 return m_module_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton1f746072012-08-29 21:13:06 +0000992 return 0;
993}
994
995lldb::ByteOrder
Jason Molenda306bd0a2013-02-19 05:42:46 +0000996DynamicLoaderDarwinKernel::KextImageInfo::GetByteOrder()
Greg Clayton1f746072012-08-29 21:13:06 +0000997{
Jason Molenda306bd0a2013-02-19 05:42:46 +0000998 if (m_memory_module_sp)
999 return m_memory_module_sp->GetArchitecture().GetByteOrder();
1000 if (m_module_sp)
1001 return m_module_sp->GetArchitecture().GetByteOrder();
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001002 return endian::InlHostByteOrder();
Greg Clayton1f746072012-08-29 21:13:06 +00001003}
1004
1005lldb_private::ArchSpec
Jason Molenda306bd0a2013-02-19 05:42:46 +00001006DynamicLoaderDarwinKernel::KextImageInfo::GetArchitecture () const
Greg Clayton1f746072012-08-29 21:13:06 +00001007{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001008 if (m_memory_module_sp)
1009 return m_memory_module_sp->GetArchitecture();
1010 if (m_module_sp)
1011 return m_module_sp->GetArchitecture();
Greg Clayton1f746072012-08-29 21:13:06 +00001012 return lldb_private::ArchSpec ();
1013}
1014
1015
Greg Clayton7b242382011-07-08 00:48:09 +00001016//----------------------------------------------------------------------
1017// Load the kernel module and initialize the "m_kernel" member. Return
1018// true _only_ if the kernel is loaded the first time through (subsequent
1019// calls to this function should return false after the kernel has been
1020// already loaded).
1021//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +00001022void
Greg Clayton944b8282011-08-22 22:30:57 +00001023DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +00001024{
Greg Claytond16e1e52011-07-12 17:06:17 +00001025 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001026 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001027 m_kernel.Clear();
1028 m_kernel.SetModule (m_process->GetTarget().GetExecutableModule());
1029 m_kernel.SetIsKernel(true);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001030
1031 ConstString kernel_name("mach_kernel");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001032 if (m_kernel.GetModule().get()
1033 && m_kernel.GetModule()->GetObjectFile()
1034 && !m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001035 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001036 kernel_name = m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename();
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001037 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001038 m_kernel.SetName (kernel_name.AsCString());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001039
Jason Molenda306bd0a2013-02-19 05:42:46 +00001040 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001041 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001042 m_kernel.SetLoadAddress(m_kernel_load_address);
1043 if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001044 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001045 // We didn't get a hint from the process, so we will
1046 // try the kernel at the address that it exists at in
1047 // the file if we have one
Jason Molenda306bd0a2013-02-19 05:42:46 +00001048 ObjectFile *kernel_object_file = m_kernel.GetModule()->GetObjectFile();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001049 if (kernel_object_file)
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001050 {
1051 addr_t load_address = kernel_object_file->GetHeaderAddress().GetLoadAddress(&m_process->GetTarget());
1052 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
1053 if (load_address != LLDB_INVALID_ADDRESS && load_address != 0)
1054 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001055 m_kernel.SetLoadAddress (load_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001056 if (load_address != file_address)
1057 {
1058 // Don't accidentally relocate the kernel to the File address --
1059 // the Load address has already been set to its actual in-memory address.
1060 // Mark it as IsLoaded.
Jason Molenda306bd0a2013-02-19 05:42:46 +00001061 m_kernel.SetProcessStopId (m_process->GetStopID());
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001062 }
1063 }
1064 else
1065 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001066 m_kernel.SetLoadAddress(file_address);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001067 }
1068 }
Greg Clayton7b242382011-07-08 00:48:09 +00001069 }
1070 }
Greg Claytonc859e2d2012-02-13 23:10:39 +00001071
Jason Molenda306bd0a2013-02-19 05:42:46 +00001072 if (m_kernel.GetLoadAddress() != LLDB_INVALID_ADDRESS)
Greg Clayton2af282a2012-03-21 04:25:00 +00001073 {
1074 if (!m_kernel.LoadImageUsingMemoryModule (m_process))
1075 {
1076 m_kernel.LoadImageAtFileAddress (m_process);
1077 }
1078 }
Greg Clayton7b242382011-07-08 00:48:09 +00001079
Jason Molenda306bd0a2013-02-19 05:42:46 +00001080 if (m_kernel.IsLoaded() && m_kernel.GetModule())
Greg Clayton7b242382011-07-08 00:48:09 +00001081 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00001082 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
Jason Molenda306bd0a2013-02-19 05:42:46 +00001083 const Symbol *symbol = m_kernel.GetModule()->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
Greg Claytonc859e2d2012-02-13 23:10:39 +00001084 if (symbol)
1085 {
Greg Claytone7612132012-03-07 21:03:09 +00001086 m_kext_summary_header_ptr_addr = symbol->GetAddress();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001087 // Update all image infos
1088 ReadAllKextSummaries ();
1089 }
Greg Clayton7b242382011-07-08 00:48:09 +00001090 }
1091 else
Greg Clayton7b242382011-07-08 00:48:09 +00001092 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001093 m_kernel.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001094 }
1095 }
Greg Clayton7b242382011-07-08 00:48:09 +00001096}
1097
Greg Clayton7b242382011-07-08 00:48:09 +00001098//----------------------------------------------------------------------
1099// Static callback function that gets called when our DYLD notification
1100// breakpoint gets hit. We update all of our image infos and then
1101// let our super class DynamicLoader class decide if we should stop
1102// or not (based on global preference).
1103//----------------------------------------------------------------------
1104bool
Greg Clayton944b8282011-08-22 22:30:57 +00001105DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +00001106 StoppointCallbackContext *context,
1107 user_id_t break_id,
1108 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +00001109{
Greg Clayton944b8282011-08-22 22:30:57 +00001110 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +00001111}
1112
1113bool
Greg Clayton944b8282011-08-22 22:30:57 +00001114DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +00001115 user_id_t break_id,
1116 user_id_t break_loc_id)
1117{
Greg Clayton5160ce52013-03-27 23:08:40 +00001118 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Claytond16e1e52011-07-12 17:06:17 +00001119 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +00001120 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +00001121
Greg Clayton374972e2011-07-09 17:15:55 +00001122 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +00001123
1124 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001125 PutToLog(log);
Greg Claytond16e1e52011-07-12 17:06:17 +00001126
Greg Clayton374972e2011-07-09 17:15:55 +00001127 return GetStopWhenImagesChange();
1128}
1129
1130
1131bool
Greg Clayton944b8282011-08-22 22:30:57 +00001132DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +00001133{
1134 Mutex::Locker locker(m_mutex);
1135
1136 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +00001137
Greg Claytond16e1e52011-07-12 17:06:17 +00001138 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001139 {
1140 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
1141 const ByteOrder byte_order = m_kernel.GetByteOrder();
1142 Error error;
1143 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00001144 // which is currently 4 uint32_t and a pointer.
Greg Clayton7b242382011-07-08 00:48:09 +00001145 uint8_t buf[24];
1146 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
1147 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +00001148 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001149 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
1150 prefer_file_cache,
1151 error,
1152 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +00001153 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001154 // We got a valid address for our kext summary header and make sure it isn't NULL
1155 if (m_kext_summary_header_addr.IsValid() &&
1156 m_kext_summary_header_addr.GetFileAddress() != 0)
1157 {
1158 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
1159 if (bytes_read == count)
1160 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001161 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001162 m_kext_summary_header.version = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001163 if (m_kext_summary_header.version > 128)
1164 {
1165 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1166 s->Printf ("WARNING: Unable to read kext summary header, got improbable version number %u\n", m_kext_summary_header.version);
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00001167 // If we get an improbably large version number, we're probably getting bad memory.
Jason Molendac6fa5db2014-04-15 01:04:00 +00001168 m_kext_summary_header_addr.Clear();
1169 return false;
1170 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001171 if (m_kext_summary_header.version >= 2)
1172 {
1173 m_kext_summary_header.entry_size = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001174 if (m_kext_summary_header.entry_size > 4096)
1175 {
1176 // If we get an improbably large entry_size, we're probably getting bad memory.
1177 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1178 s->Printf ("WARNING: Unable to read kext summary header, got improbable entry_size %u\n", m_kext_summary_header.entry_size);
1179 m_kext_summary_header_addr.Clear();
1180 return false;
1181 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001182 }
1183 else
1184 {
1185 // Versions less than 2 didn't have an entry size, it was hard coded
1186 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
1187 }
1188 m_kext_summary_header.entry_count = data.GetU32(&offset);
Jason Molendac6fa5db2014-04-15 01:04:00 +00001189 if (m_kext_summary_header.entry_count > 10000)
1190 {
1191 // If we get an improbably large number of kexts, we're probably getting bad memory.
1192 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1193 s->Printf ("WARNING: Unable to read kext summary header, got improbable number of kexts %u\n", m_kext_summary_header.entry_count);
1194 m_kext_summary_header_addr.Clear();
1195 return false;
1196 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001197 return true;
1198 }
1199 }
Greg Clayton7b242382011-07-08 00:48:09 +00001200 }
1201 }
Greg Claytond16e1e52011-07-12 17:06:17 +00001202 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001203 return false;
1204}
1205
Jason Molenda306bd0a2013-02-19 05:42:46 +00001206// We've either (a) just attached to a new kernel, or (b) the kexts-changed breakpoint was hit
1207// and we need to figure out what kexts have been added or removed.
1208// Read the kext summaries from the inferior kernel memory, compare them against the
1209// m_known_kexts vector and update the m_known_kexts vector as needed to keep in sync with the
1210// inferior.
Greg Clayton7b242382011-07-08 00:48:09 +00001211
1212bool
Jason Molenda306bd0a2013-02-19 05:42:46 +00001213DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr, uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +00001214{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001215 KextImageInfo::collection kext_summaries;
Greg Clayton5160ce52013-03-27 23:08:40 +00001216 Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +00001217 if (log)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001218 log->Printf ("Kexts-changed breakpoint hit, there are %d kexts currently.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +00001219
1220 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +00001221
1222 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
1223 return false;
1224
Jason Molenda4da2e322013-02-26 00:26:47 +00001225 // read the plugin.dynamic-loader.darwin-kernel.load-kexts setting -- if the user requested no
1226 // kext loading, don't print any messages about kexts & don't try to read them.
1227 const bool load_kexts = GetGlobalProperties()->GetLoadKexts();
1228
Jason Molenda306bd0a2013-02-19 05:42:46 +00001229 // By default, all kexts we've loaded in the past are marked as "remove" and all of the kexts
1230 // we just found out about from ReadKextSummaries are marked as "add".
1231 std::vector<bool> to_be_removed(m_known_kexts.size(), true);
1232 std::vector<bool> to_be_added(count, true);
1233
1234 int number_of_new_kexts_being_added = 0;
1235 int number_of_old_kexts_being_removed = m_known_kexts.size();
1236
1237 const uint32_t new_kexts_size = kext_summaries.size();
1238 const uint32_t old_kexts_size = m_known_kexts.size();
1239
1240 // The m_known_kexts vector may have entries that have been Cleared,
1241 // or are a kernel.
1242 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1243 {
1244 bool ignore = false;
1245 KextImageInfo &image_info = m_known_kexts[old_kext];
1246 if (image_info.IsKernel())
1247 {
1248 ignore = true;
1249 }
1250 else if (image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS && !image_info.GetModule())
1251 {
1252 ignore = true;
1253 }
1254
1255 if (ignore)
1256 {
1257 number_of_old_kexts_being_removed--;
1258 to_be_removed[old_kext] = false;
1259 }
1260 }
1261
1262 // Scan over the list of kexts we just read from the kernel, note those that
1263 // need to be added and those already loaded.
1264 for (uint32_t new_kext = 0; new_kext < new_kexts_size; new_kext++)
1265 {
1266 bool add_this_one = true;
1267 for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1268 {
1269 if (m_known_kexts[old_kext] == kext_summaries[new_kext])
1270 {
1271 // We already have this kext, don't re-load it.
1272 to_be_added[new_kext] = false;
1273 // This kext is still present, do not remove it.
1274 to_be_removed[old_kext] = false;
1275
1276 number_of_old_kexts_being_removed--;
1277 add_this_one = false;
1278 break;
1279 }
1280 }
1281 if (add_this_one)
1282 {
1283 number_of_new_kexts_being_added++;
1284 }
1285 }
1286
1287 if (number_of_new_kexts_being_added == 0 && number_of_old_kexts_being_removed == 0)
1288 return true;
1289
Greg Clayton44d93782014-01-27 23:43:24 +00001290 Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
Jason Molenda4da2e322013-02-26 00:26:47 +00001291 if (s && load_kexts)
Greg Clayton7b242382011-07-08 00:48:09 +00001292 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001293 if (number_of_new_kexts_being_added > 0 && number_of_old_kexts_being_removed > 0)
1294 {
1295 s->Printf ("Loading %d kext modules and unloading %d kext modules ", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1296 }
1297 else if (number_of_new_kexts_being_added > 0)
1298 {
1299 s->Printf ("Loading %d kext modules ", number_of_new_kexts_being_added);
1300 }
1301 else if (number_of_old_kexts_being_removed > 0)
1302 {
1303 s->Printf ("Unloading %d kext modules ", number_of_old_kexts_being_removed);
1304 }
Greg Clayton7b242382011-07-08 00:48:09 +00001305 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001306
1307 if (log)
Jason Molenda4da2e322013-02-26 00:26:47 +00001308 {
1309 if (load_kexts)
1310 {
1311 log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries: %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1312 }
1313 else
1314 {
1315 log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries kext loading is disabled, else would have %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1316 }
1317 }
1318
Jason Molenda306bd0a2013-02-19 05:42:46 +00001319
1320 if (number_of_new_kexts_being_added > 0)
1321 {
1322 ModuleList loaded_module_list;
1323
1324 const uint32_t num_of_new_kexts = kext_summaries.size();
1325 for (uint32_t new_kext = 0; new_kext < num_of_new_kexts; new_kext++)
1326 {
1327 if (to_be_added[new_kext] == true)
1328 {
1329 KextImageInfo &image_info = kext_summaries[new_kext];
Jason Molenda4da2e322013-02-26 00:26:47 +00001330 if (load_kexts)
1331 {
1332 if (!image_info.LoadImageUsingMemoryModule (m_process))
1333 {
1334 image_info.LoadImageAtFileAddress (m_process);
1335 }
1336 }
Jason Molenda306bd0a2013-02-19 05:42:46 +00001337
1338 m_known_kexts.push_back(image_info);
1339
1340 if (image_info.GetModule() && m_process->GetStopID() == image_info.GetProcessStopId())
1341 loaded_module_list.AppendIfNeeded (image_info.GetModule());
1342
Jason Molenda4da2e322013-02-26 00:26:47 +00001343 if (s && load_kexts)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001344 s->Printf (".");
1345
1346 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001347 kext_summaries[new_kext].PutToLog (log);
Jason Molenda306bd0a2013-02-19 05:42:46 +00001348 }
1349 }
1350 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
1351 }
1352
1353 if (number_of_old_kexts_being_removed > 0)
1354 {
1355 ModuleList loaded_module_list;
1356 const uint32_t num_of_old_kexts = m_known_kexts.size();
1357 for (uint32_t old_kext = 0; old_kext < num_of_old_kexts; old_kext++)
1358 {
1359 ModuleList unloaded_module_list;
1360 if (to_be_removed[old_kext])
1361 {
1362 KextImageInfo &image_info = m_known_kexts[old_kext];
1363 // You can't unload the kernel.
1364 if (!image_info.IsKernel())
1365 {
1366 if (image_info.GetModule())
1367 {
1368 unloaded_module_list.AppendIfNeeded (image_info.GetModule());
1369 }
1370 if (s)
1371 s->Printf (".");
1372 image_info.Clear();
1373 // should pull it out of the KextImageInfos vector but that would mutate the list and invalidate
1374 // the to_be_removed bool vector; leaving it in place once Cleared() is relatively harmless.
1375 }
1376 }
Greg Clayton095eeaa2013-11-05 23:28:00 +00001377 m_process->GetTarget().ModulesDidUnload (unloaded_module_list, false);
Jason Molenda306bd0a2013-02-19 05:42:46 +00001378 }
1379 }
1380
Jason Molenda4da2e322013-02-26 00:26:47 +00001381 if (s && load_kexts)
Jason Molenda68b36072012-10-02 03:49:41 +00001382 {
1383 s->Printf (" done.\n");
1384 s->Flush ();
1385 }
1386
Jason Molenda306bd0a2013-02-19 05:42:46 +00001387 return true;
Greg Clayton7b242382011-07-08 00:48:09 +00001388}
1389
Greg Clayton7b242382011-07-08 00:48:09 +00001390uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001391DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +00001392 uint32_t image_infos_count,
Jason Molenda306bd0a2013-02-19 05:42:46 +00001393 KextImageInfo::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +00001394{
1395 const ByteOrder endian = m_kernel.GetByteOrder();
1396 const uint32_t addr_size = m_kernel.GetAddressByteSize();
1397
1398 image_infos.resize(image_infos_count);
1399 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
1400 DataBufferHeap data(count, 0);
1401 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +00001402
Greg Clayton0d9fc762011-07-08 03:21:57 +00001403 const bool prefer_file_cache = false;
1404 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
1405 prefer_file_cache,
1406 data.GetBytes(),
1407 data.GetByteSize(),
1408 error);
Greg Clayton7b242382011-07-08 00:48:09 +00001409 if (bytes_read == count)
1410 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001411
Greg Clayton7b242382011-07-08 00:48:09 +00001412 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
1413 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +00001414 for (uint32_t kext_summary_offset = 0;
1415 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
1416 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +00001417 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001418 lldb::offset_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +00001419 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
1420 if (name_data == NULL)
1421 break;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001422 image_infos[i].SetName ((const char *) name_data);
1423 UUID uuid (extractor.GetData (&offset, 16), 16);
1424 image_infos[i].SetUUID (uuid);
1425 image_infos[i].SetLoadAddress (extractor.GetU64(&offset));
1426 image_infos[i].SetSize (extractor.GetU64(&offset));
Greg Clayton7b242382011-07-08 00:48:09 +00001427 }
1428 if (i < image_infos.size())
1429 image_infos.resize(i);
1430 }
1431 else
1432 {
1433 image_infos.clear();
1434 }
1435 return image_infos.size();
1436}
1437
1438bool
Greg Clayton944b8282011-08-22 22:30:57 +00001439DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +00001440{
Greg Clayton7b242382011-07-08 00:48:09 +00001441 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +00001442
Greg Clayton7b242382011-07-08 00:48:09 +00001443 if (ReadKextSummaryHeader ())
1444 {
Greg Clayton374972e2011-07-09 17:15:55 +00001445 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +00001446 {
Greg Clayton0d9fc762011-07-08 03:21:57 +00001447 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +00001448 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +00001449 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +00001450 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001451 m_known_kexts.clear();
Greg Clayton7b242382011-07-08 00:48:09 +00001452 }
1453 return true;
1454 }
1455 }
1456 return false;
1457}
1458
1459//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +00001460// Dump an image info structure to the file handle provided.
1461//----------------------------------------------------------------------
1462void
Jason Molenda306bd0a2013-02-19 05:42:46 +00001463DynamicLoaderDarwinKernel::KextImageInfo::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001464{
1465 if (log == NULL)
1466 return;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001467 const uint8_t *u = (uint8_t *) m_uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +00001468
Jason Molenda306bd0a2013-02-19 05:42:46 +00001469 if (m_load_address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +00001470 {
1471 if (u)
1472 {
Greg Claytona63d08c2011-07-19 03:57:15 +00001473 log->Printf("\tuuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X name=\"%s\" (UNLOADED)",
Greg Clayton7b242382011-07-08 00:48:09 +00001474 u[ 0], u[ 1], u[ 2], u[ 3],
1475 u[ 4], u[ 5], u[ 6], u[ 7],
1476 u[ 8], u[ 9], u[10], u[11],
1477 u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001478 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001479 }
1480 else
Jason Molenda306bd0a2013-02-19 05:42:46 +00001481 log->Printf("\tname=\"%s\" (UNLOADED)", m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001482 }
1483 else
1484 {
1485 if (u)
1486 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001487 log->Printf("\taddr=0x%16.16" PRIx64 " size=0x%16.16" PRIx64 " uuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X name=\"%s\"",
1488 m_load_address, m_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001489 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
1490 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Jason Molenda306bd0a2013-02-19 05:42:46 +00001491 m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001492 }
1493 else
1494 {
Jason Molenda306bd0a2013-02-19 05:42:46 +00001495 log->Printf("\t[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") name=\"%s\"",
1496 m_load_address, m_load_address+m_size, m_name.c_str());
Greg Clayton7b242382011-07-08 00:48:09 +00001497 }
Greg Clayton7b242382011-07-08 00:48:09 +00001498 }
1499}
1500
1501//----------------------------------------------------------------------
1502// Dump the _dyld_all_image_infos members and all current image infos
1503// that we have parsed to the file handle provided.
1504//----------------------------------------------------------------------
1505void
Greg Clayton944b8282011-08-22 22:30:57 +00001506DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +00001507{
1508 if (log == NULL)
1509 return;
1510
1511 Mutex::Locker locker(m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +00001512 log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 " { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +00001513 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +00001514 m_kext_summary_header.version,
1515 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +00001516 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +00001517
1518 size_t i;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001519 const size_t count = m_known_kexts.size();
Greg Clayton7b242382011-07-08 00:48:09 +00001520 if (count > 0)
1521 {
1522 log->PutCString("Loaded:");
1523 for (i = 0; i<count; i++)
Jason Molenda306bd0a2013-02-19 05:42:46 +00001524 m_known_kexts[i].PutToLog(log);
Greg Clayton7b242382011-07-08 00:48:09 +00001525 }
1526}
1527
1528void
Greg Clayton944b8282011-08-22 22:30:57 +00001529DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +00001530{
Greg Clayton944b8282011-08-22 22:30:57 +00001531 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +00001532 Clear(true);
1533 m_process = process;
Greg Clayton7b242382011-07-08 00:48:09 +00001534}
1535
Greg Clayton374972e2011-07-09 17:15:55 +00001536void
Greg Clayton944b8282011-08-22 22:30:57 +00001537DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +00001538{
Jason Molenda306bd0a2013-02-19 05:42:46 +00001539 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.GetModule())
Greg Clayton374972e2011-07-09 17:15:55 +00001540 {
Greg Clayton944b8282011-08-22 22:30:57 +00001541 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +00001542
Greg Claytond16e1e52011-07-12 17:06:17 +00001543
Jim Inghama8558b62012-05-22 00:12:20 +00001544 const bool internal_bp = true;
Greg Claytoneb023e72013-10-11 19:48:25 +00001545 const bool hardware = false;
Greg Claytond16e1e52011-07-12 17:06:17 +00001546 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +00001547 FileSpecList module_spec_list;
Jason Molenda306bd0a2013-02-19 05:42:46 +00001548 module_spec_list.Append (m_kernel.GetModule()->GetFileSpec());
Jim Ingham969795f2011-09-21 01:17:13 +00001549 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +00001550 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +00001551 "OSKextLoadedKextSummariesUpdated",
1552 eFunctionNameTypeFull,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00001553 eLanguageTypeUnknown,
Jim Inghama8558b62012-05-22 00:12:20 +00001554 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +00001555 internal_bp,
1556 hardware).get();
Greg Clayton374972e2011-07-09 17:15:55 +00001557
Greg Clayton944b8282011-08-22 22:30:57 +00001558 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +00001559 m_break_id = bp->GetID();
1560 }
Greg Clayton7b242382011-07-08 00:48:09 +00001561}
1562
1563//----------------------------------------------------------------------
1564// Member function that gets called when the process state changes.
1565//----------------------------------------------------------------------
1566void
Greg Clayton944b8282011-08-22 22:30:57 +00001567DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +00001568{
Greg Clayton944b8282011-08-22 22:30:57 +00001569 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +00001570 switch (state)
1571 {
1572 case eStateConnected:
1573 case eStateAttaching:
1574 case eStateLaunching:
1575 case eStateInvalid:
1576 case eStateUnloaded:
1577 case eStateExited:
1578 case eStateDetached:
1579 Clear(false);
1580 break;
1581
1582 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +00001583 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +00001584 break;
1585
1586 case eStateRunning:
1587 case eStateStepping:
1588 case eStateCrashed:
1589 case eStateSuspended:
1590 break;
Greg Clayton7b242382011-07-08 00:48:09 +00001591 }
1592}
1593
1594ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +00001595DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +00001596{
1597 ThreadPlanSP thread_plan_sp;
Greg Clayton5160ce52013-03-27 23:08:40 +00001598 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton374972e2011-07-09 17:15:55 +00001599 if (log)
1600 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +00001601 return thread_plan_sp;
1602}
1603
1604Error
Greg Clayton944b8282011-08-22 22:30:57 +00001605DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +00001606{
1607 Error error;
1608 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1609 return error;
1610}
1611
1612void
Greg Clayton944b8282011-08-22 22:30:57 +00001613DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +00001614{
1615 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1616 GetPluginDescriptionStatic(),
Greg Claytone8cd0c92012-10-19 18:02:49 +00001617 CreateInstance,
1618 DebuggerInitialize);
Greg Clayton7b242382011-07-08 00:48:09 +00001619}
1620
1621void
Greg Clayton944b8282011-08-22 22:30:57 +00001622DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +00001623{
1624 PluginManager::UnregisterPlugin (CreateInstance);
1625}
1626
Greg Claytone8cd0c92012-10-19 18:02:49 +00001627void
1628DynamicLoaderDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
1629{
1630 if (!PluginManager::GetSettingForDynamicLoaderPlugin (debugger, DynamicLoaderDarwinKernelProperties::GetSettingName()))
1631 {
1632 const bool is_global_setting = true;
1633 PluginManager::CreateSettingForDynamicLoaderPlugin (debugger,
1634 GetGlobalProperties()->GetValueProperties(),
1635 ConstString ("Properties for the DynamicLoaderDarwinKernel plug-in."),
1636 is_global_setting);
1637 }
1638}
Greg Clayton7b242382011-07-08 00:48:09 +00001639
Greg Clayton57abc5d2013-05-10 21:47:16 +00001640lldb_private::ConstString
Greg Clayton944b8282011-08-22 22:30:57 +00001641DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001642{
Greg Clayton57abc5d2013-05-10 21:47:16 +00001643 static ConstString g_name("darwin-kernel");
1644 return g_name;
Greg Clayton7b242382011-07-08 00:48:09 +00001645}
1646
1647const char *
Greg Clayton944b8282011-08-22 22:30:57 +00001648DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +00001649{
1650 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1651}
1652
1653
1654//------------------------------------------------------------------
1655// PluginInterface protocol
1656//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00001657lldb_private::ConstString
Greg Clayton944b8282011-08-22 22:30:57 +00001658DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +00001659{
Greg Clayton7b242382011-07-08 00:48:09 +00001660 return GetPluginNameStatic();
1661}
1662
1663uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +00001664DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +00001665{
1666 return 1;
1667}
1668
Greg Clayton1f746072012-08-29 21:13:06 +00001669lldb::ByteOrder
1670DynamicLoaderDarwinKernel::GetByteOrderFromMagic (uint32_t magic)
1671{
1672 switch (magic)
1673 {
Charles Davis510938e2013-08-27 05:04:57 +00001674 case llvm::MachO::MH_MAGIC:
1675 case llvm::MachO::MH_MAGIC_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001676 return endian::InlHostByteOrder();
Greg Clayton1f746072012-08-29 21:13:06 +00001677
Charles Davis510938e2013-08-27 05:04:57 +00001678 case llvm::MachO::MH_CIGAM:
1679 case llvm::MachO::MH_CIGAM_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001680 if (endian::InlHostByteOrder() == lldb::eByteOrderBig)
Greg Clayton1f746072012-08-29 21:13:06 +00001681 return lldb::eByteOrderLittle;
1682 else
1683 return lldb::eByteOrderBig;
1684
1685 default:
1686 break;
1687 }
1688 return lldb::eByteOrderInvalid;
1689}
1690