1944b828aSGreg Clayton //===-- DynamicLoaderDarwinKernel.cpp -----------------------------*- C++ -*-===//
2d4bfbc9aSGreg Clayton //
3d4bfbc9aSGreg Clayton //                     The LLVM Compiler Infrastructure
4d4bfbc9aSGreg Clayton //
5d4bfbc9aSGreg Clayton // This file is distributed under the University of Illinois Open Source
6d4bfbc9aSGreg Clayton // License. See LICENSE.TXT for details.
7d4bfbc9aSGreg Clayton //
8d4bfbc9aSGreg Clayton //===----------------------------------------------------------------------===//
9d4bfbc9aSGreg Clayton 
1046d005dbSJim Ingham #include "lldb/Utility/SafeMachO.h"
11eadcca97SGreg Clayton 
12d4bfbc9aSGreg Clayton #include "lldb/Breakpoint/StoppointCallbackContext.h"
13d4bfbc9aSGreg Clayton #include "lldb/Core/DataBuffer.h"
14d4bfbc9aSGreg Clayton #include "lldb/Core/DataBufferHeap.h"
15d4bfbc9aSGreg Clayton #include "lldb/Core/Debugger.h"
16d4bfbc9aSGreg Clayton #include "lldb/Core/Log.h"
17d4bfbc9aSGreg Clayton #include "lldb/Core/Module.h"
181f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
19d4bfbc9aSGreg Clayton #include "lldb/Core/PluginManager.h"
201f746071SGreg Clayton #include "lldb/Core/Section.h"
21d4bfbc9aSGreg Clayton #include "lldb/Core/State.h"
2244d93782SGreg Clayton #include "lldb/Core/StreamFile.h"
2368b3607fSJason Molenda #include "lldb/Host/Symbols.h"
2441204d09SIlia K #include "lldb/Interpreter/OptionValueProperties.h"
25d4bfbc9aSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
26d4bfbc9aSGreg Clayton #include "lldb/Target/RegisterContext.h"
27b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h"
28d4bfbc9aSGreg Clayton #include "lldb/Target/Target.h"
29d4bfbc9aSGreg Clayton #include "lldb/Target/Thread.h"
30d4bfbc9aSGreg Clayton #include "lldb/Target/ThreadPlanRunToAddress.h"
3157abc5d6SGreg Clayton #include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
32d4bfbc9aSGreg Clayton 
33944b828aSGreg Clayton #include "DynamicLoaderDarwinKernel.h"
34d4bfbc9aSGreg Clayton 
35d4bfbc9aSGreg Clayton //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
36d4bfbc9aSGreg Clayton #ifdef ENABLE_DEBUG_PRINTF
37d4bfbc9aSGreg Clayton #include <stdio.h>
38d4bfbc9aSGreg Clayton #define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
39d4bfbc9aSGreg Clayton #else
40d4bfbc9aSGreg Clayton #define DEBUG_PRINTF(fmt, ...)
41d4bfbc9aSGreg Clayton #endif
42d4bfbc9aSGreg Clayton 
43d4bfbc9aSGreg Clayton using namespace lldb;
44d4bfbc9aSGreg Clayton using namespace lldb_private;
45d4bfbc9aSGreg Clayton 
466ba6d3d1SJason Molenda // Progressively greater amounts of scanning we will allow
476ba6d3d1SJason Molenda // For some targets very early in startup, we can't do any random reads of memory or we can crash the device
486ba6d3d1SJason Molenda // so a setting is needed that can completely disable the KASLR scans.
496ba6d3d1SJason Molenda 
506ba6d3d1SJason Molenda enum KASLRScanType
516ba6d3d1SJason Molenda {
526ba6d3d1SJason Molenda     eKASLRScanNone = 0,         // No reading into the inferior at all
536ba6d3d1SJason Molenda     eKASLRScanLowgloAddresses,  // Check one word of memory for a possible kernel addr, then see if a kernel is there
5463969221SJason Molenda     eKASLRScanNearPC,           // Scan backwards from the current $pc looking for kernel; checking at 96 locations total
556ba6d3d1SJason Molenda     eKASLRScanExhaustiveScan    // Scan through the entire possible kernel address range looking for a kernel
566ba6d3d1SJason Molenda };
576ba6d3d1SJason Molenda 
586ba6d3d1SJason Molenda OptionEnumValueElement
596ba6d3d1SJason Molenda g_kaslr_kernel_scan_enum_values[] =
606ba6d3d1SJason Molenda {
616ba6d3d1SJason Molenda     { eKASLRScanNone,            "none",            "Do not read memory looking for a Darwin kernel when attaching." },
626ba6d3d1SJason Molenda     { eKASLRScanLowgloAddresses, "basic",           "Check for the Darwin kernel's load addr in the lowglo page (boot-args=debug) only." },
636ba6d3d1SJason Molenda     { eKASLRScanNearPC,          "fast-scan",       "Scan near the pc value on attach to find the Darwin kernel's load address."},
646ba6d3d1SJason Molenda     { eKASLRScanExhaustiveScan,  "exhaustive-scan", "Scan through the entire potential address range of Darwin kernel (only on 32-bit targets)."},
656ba6d3d1SJason Molenda     { 0, NULL, NULL }
666ba6d3d1SJason Molenda };
676ba6d3d1SJason Molenda 
68e8cd0c98SGreg Clayton static PropertyDefinition
69e8cd0c98SGreg Clayton g_properties[] =
70e8cd0c98SGreg Clayton {
7166763eedSGreg Clayton     { "load-kexts" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Automatically loads kext images when attaching to a kernel." },
726ba6d3d1SJason Molenda     { "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." },
73e8cd0c98SGreg Clayton     {  NULL        , OptionValue::eTypeInvalid, false, 0  , NULL, NULL, NULL  }
74e8cd0c98SGreg Clayton };
75e8cd0c98SGreg Clayton 
76e8cd0c98SGreg Clayton enum {
776ba6d3d1SJason Molenda     ePropertyLoadKexts,
786ba6d3d1SJason Molenda     ePropertyScanType
79e8cd0c98SGreg Clayton };
80e8cd0c98SGreg Clayton 
81e8cd0c98SGreg Clayton class DynamicLoaderDarwinKernelProperties : public Properties
82e8cd0c98SGreg Clayton {
83e8cd0c98SGreg Clayton public:
84e8cd0c98SGreg Clayton 
85e8cd0c98SGreg Clayton     static ConstString &
86e8cd0c98SGreg Clayton     GetSettingName ()
87e8cd0c98SGreg Clayton     {
88468ea4ebSGreg Clayton         static ConstString g_setting_name("darwin-kernel");
89e8cd0c98SGreg Clayton         return g_setting_name;
90e8cd0c98SGreg Clayton     }
91e8cd0c98SGreg Clayton 
92e8cd0c98SGreg Clayton     DynamicLoaderDarwinKernelProperties() :
93e8cd0c98SGreg Clayton         Properties ()
94e8cd0c98SGreg Clayton     {
95e8cd0c98SGreg Clayton         m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
96e8cd0c98SGreg Clayton         m_collection_sp->Initialize(g_properties);
97e8cd0c98SGreg Clayton     }
98e8cd0c98SGreg Clayton 
99e8cd0c98SGreg Clayton     virtual
100e8cd0c98SGreg Clayton     ~DynamicLoaderDarwinKernelProperties()
101e8cd0c98SGreg Clayton     {
102e8cd0c98SGreg Clayton     }
103e8cd0c98SGreg Clayton 
104e8cd0c98SGreg Clayton     bool
10566763eedSGreg Clayton     GetLoadKexts() const
106e8cd0c98SGreg Clayton     {
10766763eedSGreg Clayton         const uint32_t idx = ePropertyLoadKexts;
108e8cd0c98SGreg Clayton         return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
109e8cd0c98SGreg Clayton     }
110e8cd0c98SGreg Clayton 
1116ba6d3d1SJason Molenda     KASLRScanType
1126ba6d3d1SJason Molenda     GetScanType() const
1136ba6d3d1SJason Molenda     {
1146ba6d3d1SJason Molenda         const uint32_t idx = ePropertyScanType;
11563969221SJason Molenda         return (KASLRScanType) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
1166ba6d3d1SJason Molenda     }
1176ba6d3d1SJason Molenda 
1186ba6d3d1SJason Molenda 
119e8cd0c98SGreg Clayton };
120e8cd0c98SGreg Clayton 
1217b0992d9SGreg Clayton typedef std::shared_ptr<DynamicLoaderDarwinKernelProperties> DynamicLoaderDarwinKernelPropertiesSP;
122e8cd0c98SGreg Clayton 
123e8cd0c98SGreg Clayton static const DynamicLoaderDarwinKernelPropertiesSP &
124e8cd0c98SGreg Clayton GetGlobalProperties()
125e8cd0c98SGreg Clayton {
126e8cd0c98SGreg Clayton     static DynamicLoaderDarwinKernelPropertiesSP g_settings_sp;
127e8cd0c98SGreg Clayton     if (!g_settings_sp)
128e8cd0c98SGreg Clayton         g_settings_sp.reset (new DynamicLoaderDarwinKernelProperties ());
129e8cd0c98SGreg Clayton     return g_settings_sp;
130e8cd0c98SGreg Clayton }
131e8cd0c98SGreg Clayton 
132d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
133d4bfbc9aSGreg Clayton // Create an instance of this class. This function is filled into
134d4bfbc9aSGreg Clayton // the plugin info class that gets handed out by the plugin factory and
135d4bfbc9aSGreg Clayton // allows the lldb to instantiate an instance of this class.
136d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
137d4bfbc9aSGreg Clayton DynamicLoader *
138944b828aSGreg Clayton DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force)
139d4bfbc9aSGreg Clayton {
1406ba6d3d1SJason Molenda     if (!force)
141d4bfbc9aSGreg Clayton     {
1426ba6d3d1SJason Molenda         // If the user provided an executable binary and it is not a kernel,
1436ba6d3d1SJason Molenda         // this plugin should not create an instance.
144d4bfbc9aSGreg Clayton         Module* exe_module = process->GetTarget().GetExecutableModulePointer();
145d4bfbc9aSGreg Clayton         if (exe_module)
146d4bfbc9aSGreg Clayton         {
147d4bfbc9aSGreg Clayton             ObjectFile *object_file = exe_module->GetObjectFile();
148d4bfbc9aSGreg Clayton             if (object_file)
149d4bfbc9aSGreg Clayton             {
1506ba6d3d1SJason Molenda                 if (object_file->GetStrata() != ObjectFile::eStrataKernel)
1516ba6d3d1SJason Molenda                 {
1526ba6d3d1SJason Molenda                     return NULL;
1536ba6d3d1SJason Molenda                 }
154d4bfbc9aSGreg Clayton             }
155d4bfbc9aSGreg Clayton         }
156d4bfbc9aSGreg Clayton 
1576ba6d3d1SJason Molenda         // If the target's architecture does not look like an Apple environment,
1586ba6d3d1SJason Molenda         // this plugin should not create an instance.
159d4bfbc9aSGreg Clayton         const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
16070512317SGreg Clayton         switch (triple_ref.getOS())
16170512317SGreg Clayton         {
16270512317SGreg Clayton             case llvm::Triple::Darwin:
16370512317SGreg Clayton             case llvm::Triple::MacOSX:
16470512317SGreg Clayton             case llvm::Triple::IOS:
165a814f704SJason Molenda             case llvm::Triple::TvOS:
166a814f704SJason Molenda             case llvm::Triple::WatchOS:
1676ba6d3d1SJason Molenda                 if (triple_ref.getVendor() != llvm::Triple::Apple)
1686ba6d3d1SJason Molenda                 {
1696ba6d3d1SJason Molenda                    return NULL;
1706ba6d3d1SJason Molenda                 }
1716ba6d3d1SJason Molenda                 break;
1726ba6d3d1SJason Molenda             // If we have triple like armv7-unknown-unknown, we should try looking for a Darwin kernel.
1736ba6d3d1SJason Molenda             case llvm::Triple::UnknownOS:
17470512317SGreg Clayton                 break;
17570512317SGreg Clayton             default:
1766ba6d3d1SJason Molenda                 return NULL;
17770512317SGreg Clayton                 break;
17870512317SGreg Clayton         }
179d4bfbc9aSGreg Clayton     }
1806ba6d3d1SJason Molenda 
1816ba6d3d1SJason Molenda     // At this point if there is an ExecutableModule, it is a kernel and the Target is some variant of an Apple system.
1826ba6d3d1SJason Molenda     // If the Process hasn't provided the kernel load address, we need to look around in memory to find it.
1836ba6d3d1SJason Molenda 
184503d0181SJason Molenda     addr_t kernel_load_address = SearchForDarwinKernel (process);
185503d0181SJason Molenda     if (kernel_load_address != LLDB_INVALID_ADDRESS)
186503d0181SJason Molenda     {
18790ff7911SEwan Crawford         process->SetCanRunCode(false);
188503d0181SJason Molenda         return new DynamicLoaderDarwinKernel (process, kernel_load_address);
189503d0181SJason Molenda     }
190503d0181SJason Molenda     return NULL;
191503d0181SJason Molenda }
192503d0181SJason Molenda 
193503d0181SJason Molenda lldb::addr_t
194503d0181SJason Molenda DynamicLoaderDarwinKernel::SearchForDarwinKernel (Process *process)
195503d0181SJason Molenda {
1966ba6d3d1SJason Molenda     addr_t kernel_load_address = process->GetImageInfoAddress();
1976ba6d3d1SJason Molenda     if (kernel_load_address == LLDB_INVALID_ADDRESS)
1986ba6d3d1SJason Molenda     {
1996ba6d3d1SJason Molenda         kernel_load_address = SearchForKernelAtSameLoadAddr (process);
2006ba6d3d1SJason Molenda         if (kernel_load_address == LLDB_INVALID_ADDRESS)
2016ba6d3d1SJason Molenda         {
2026ba6d3d1SJason Molenda             kernel_load_address = SearchForKernelWithDebugHints (process);
2036ba6d3d1SJason Molenda             if (kernel_load_address == LLDB_INVALID_ADDRESS)
2046ba6d3d1SJason Molenda             {
2056ba6d3d1SJason Molenda                 kernel_load_address = SearchForKernelNearPC (process);
2066ba6d3d1SJason Molenda                 if (kernel_load_address == LLDB_INVALID_ADDRESS)
2076ba6d3d1SJason Molenda                 {
2086ba6d3d1SJason Molenda                     kernel_load_address = SearchForKernelViaExhaustiveSearch (process);
2096ba6d3d1SJason Molenda                 }
2106ba6d3d1SJason Molenda             }
2116ba6d3d1SJason Molenda         }
212d4bfbc9aSGreg Clayton     }
213503d0181SJason Molenda     return kernel_load_address;
214d4bfbc9aSGreg Clayton }
215d4bfbc9aSGreg Clayton 
216d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
2176ba6d3d1SJason Molenda // Check if the kernel binary is loaded in memory without a slide.
2186ba6d3d1SJason Molenda // First verify that the ExecutableModule is a kernel before we proceed.
2196ba6d3d1SJason Molenda // Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
2206ba6d3d1SJason Molenda //----------------------------------------------------------------------
2216ba6d3d1SJason Molenda lldb::addr_t
2226ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::SearchForKernelAtSameLoadAddr (Process *process)
2236ba6d3d1SJason Molenda {
2246ba6d3d1SJason Molenda     Module *exe_module = process->GetTarget().GetExecutableModulePointer();
2256ba6d3d1SJason Molenda     if (exe_module == NULL)
2266ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2276ba6d3d1SJason Molenda 
2286ba6d3d1SJason Molenda     ObjectFile *exe_objfile = exe_module->GetObjectFile();
2296ba6d3d1SJason Molenda     if (exe_objfile == NULL)
2306ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2316ba6d3d1SJason Molenda 
2326ba6d3d1SJason Molenda     if (exe_objfile->GetType() != ObjectFile::eTypeExecutable || exe_objfile->GetStrata() != ObjectFile::eStrataKernel)
2336ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2346ba6d3d1SJason Molenda 
2356ba6d3d1SJason Molenda     if (!exe_objfile->GetHeaderAddress().IsValid())
2366ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2376ba6d3d1SJason Molenda 
2386ba6d3d1SJason Molenda     if (CheckForKernelImageAtAddress (exe_objfile->GetHeaderAddress().GetFileAddress(), process) == exe_module->GetUUID())
2396ba6d3d1SJason Molenda         return exe_objfile->GetHeaderAddress().GetFileAddress();
2406ba6d3d1SJason Molenda 
2416ba6d3d1SJason Molenda     return LLDB_INVALID_ADDRESS;
2426ba6d3d1SJason Molenda }
2436ba6d3d1SJason Molenda 
2446ba6d3d1SJason Molenda //----------------------------------------------------------------------
2456ba6d3d1SJason Molenda // If the debug flag is included in the boot-args nvram setting, the kernel's load address
2466ba6d3d1SJason Molenda // will be noted in the lowglo page at a fixed address
2476ba6d3d1SJason Molenda // Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
2486ba6d3d1SJason Molenda //----------------------------------------------------------------------
2496ba6d3d1SJason Molenda lldb::addr_t
2506ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::SearchForKernelWithDebugHints (Process *process)
2516ba6d3d1SJason Molenda {
2526ba6d3d1SJason Molenda     if (GetGlobalProperties()->GetScanType() == eKASLRScanNone)
2536ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2546ba6d3d1SJason Molenda 
2556ba6d3d1SJason Molenda     Error read_err;
2566ba6d3d1SJason Molenda     addr_t addr = LLDB_INVALID_ADDRESS;
2576ba6d3d1SJason Molenda     if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
2586ba6d3d1SJason Molenda     {
2596ba6d3d1SJason Molenda         addr = process->ReadUnsignedIntegerFromMemory (0xffffff8000002010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
2607dd29390SJason Molenda         if (CheckForKernelImageAtAddress (addr, process).IsValid())
2617dd29390SJason Molenda         {
2627dd29390SJason Molenda             return addr;
2637dd29390SJason Molenda         }
2647dd29390SJason Molenda         addr = process->ReadUnsignedIntegerFromMemory (0xffffff8000004010ULL, 8, LLDB_INVALID_ADDRESS, read_err);
2657dd29390SJason Molenda         if (CheckForKernelImageAtAddress (addr, process).IsValid())
2667dd29390SJason Molenda         {
2677dd29390SJason Molenda             return addr;
2687dd29390SJason Molenda         }
2696ba6d3d1SJason Molenda     }
2706ba6d3d1SJason Molenda     else
2716ba6d3d1SJason Molenda     {
2726ba6d3d1SJason Molenda         addr = process->ReadUnsignedIntegerFromMemory (0xffff0110, 4, LLDB_INVALID_ADDRESS, read_err);
2736ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr, process).IsValid())
2747dd29390SJason Molenda         {
2756ba6d3d1SJason Molenda             return addr;
2766ba6d3d1SJason Molenda         }
2777dd29390SJason Molenda     }
2786ba6d3d1SJason Molenda     return LLDB_INVALID_ADDRESS;
2796ba6d3d1SJason Molenda }
2806ba6d3d1SJason Molenda 
2816ba6d3d1SJason Molenda //----------------------------------------------------------------------
2826ba6d3d1SJason Molenda // If the kernel is currently executing when lldb attaches, and we don't have
2836ba6d3d1SJason Molenda // a better way of finding the kernel's load address, try searching backwards
2846ba6d3d1SJason Molenda // from the current pc value looking for the kernel's Mach header in memory.
2856ba6d3d1SJason Molenda // Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
2866ba6d3d1SJason Molenda //----------------------------------------------------------------------
2876ba6d3d1SJason Molenda lldb::addr_t
2886ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::SearchForKernelNearPC (Process *process)
2896ba6d3d1SJason Molenda {
2906ba6d3d1SJason Molenda     if (GetGlobalProperties()->GetScanType() == eKASLRScanNone
2916ba6d3d1SJason Molenda         || GetGlobalProperties()->GetScanType() == eKASLRScanLowgloAddresses)
2926ba6d3d1SJason Molenda     {
2936ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2946ba6d3d1SJason Molenda     }
2956ba6d3d1SJason Molenda 
2966ba6d3d1SJason Molenda     ThreadSP thread = process->GetThreadList().GetSelectedThread ();
2976ba6d3d1SJason Molenda     if (thread.get() == NULL)
2986ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
2996ba6d3d1SJason Molenda     addr_t pc = thread->GetRegisterContext ()->GetPC(LLDB_INVALID_ADDRESS);
3006ba6d3d1SJason Molenda 
3016ba6d3d1SJason Molenda     if (pc == LLDB_INVALID_ADDRESS)
3026ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
3036ba6d3d1SJason Molenda 
30444edbf13SJason Molenda     addr_t kernel_range_low;
3056ba6d3d1SJason Molenda     if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
3066ba6d3d1SJason Molenda     {
3076ba6d3d1SJason Molenda         kernel_range_low = 1ULL << 63;
3086ba6d3d1SJason Molenda     }
3096ba6d3d1SJason Molenda     else
3106ba6d3d1SJason Molenda     {
3116ba6d3d1SJason Molenda         kernel_range_low = 1ULL << 31;
3126ba6d3d1SJason Molenda     }
3136ba6d3d1SJason Molenda 
3146ba6d3d1SJason Molenda     // Outside the normal kernel address range, this is probably userland code running right now
3156ba6d3d1SJason Molenda     if (pc < kernel_range_low)
31644edbf13SJason Molenda         return LLDB_INVALID_ADDRESS;
3176ba6d3d1SJason Molenda 
3186ba6d3d1SJason Molenda     // The kernel will load at at one megabyte boundary (0x100000), or at that boundary plus
3196ba6d3d1SJason Molenda     // an offset of one page (0x1000) or two, depending on the device.
3206ba6d3d1SJason Molenda 
3216ba6d3d1SJason Molenda     // Round the current pc down to the nearest one megabyte boundary - the place where we will start searching.
3226ba6d3d1SJason Molenda     addr_t addr = pc & ~0xfffff;
3236ba6d3d1SJason Molenda 
3246ba6d3d1SJason Molenda     int i = 0;
3256ba6d3d1SJason Molenda     while (i < 32 && pc >= kernel_range_low)
3266ba6d3d1SJason Molenda     {
3276ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr, process).IsValid())
3286ba6d3d1SJason Molenda             return addr;
3296ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
3306ba6d3d1SJason Molenda             return addr + 0x1000;
3316ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
3326ba6d3d1SJason Molenda             return addr + 0x2000;
333a02869deSJason Molenda         if (CheckForKernelImageAtAddress (addr + 0x4000, process).IsValid())
334a02869deSJason Molenda             return addr + 0x4000;
3356ba6d3d1SJason Molenda         i++;
3366ba6d3d1SJason Molenda         addr -= 0x100000;
3376ba6d3d1SJason Molenda     }
3386ba6d3d1SJason Molenda 
3396ba6d3d1SJason Molenda     return LLDB_INVALID_ADDRESS;
3406ba6d3d1SJason Molenda }
3416ba6d3d1SJason Molenda 
3426ba6d3d1SJason Molenda //----------------------------------------------------------------------
3436ba6d3d1SJason Molenda // Scan through the valid address range for a kernel binary.
3446ba6d3d1SJason Molenda // This is uselessly slow in 64-bit environments so we don't even try it.
3456ba6d3d1SJason Molenda // This scan is not enabled by default even for 32-bit targets.
3466ba6d3d1SJason Molenda // Returns the address of the kernel if one was found, else LLDB_INVALID_ADDRESS.
3476ba6d3d1SJason Molenda //----------------------------------------------------------------------
3486ba6d3d1SJason Molenda lldb::addr_t
3496ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::SearchForKernelViaExhaustiveSearch (Process *process)
3506ba6d3d1SJason Molenda {
3516ba6d3d1SJason Molenda     if (GetGlobalProperties()->GetScanType() != eKASLRScanExhaustiveScan)
3526ba6d3d1SJason Molenda     {
3536ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
3546ba6d3d1SJason Molenda     }
3556ba6d3d1SJason Molenda 
3566ba6d3d1SJason Molenda     addr_t kernel_range_low, kernel_range_high;
3576ba6d3d1SJason Molenda     if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
3586ba6d3d1SJason Molenda     {
3596ba6d3d1SJason Molenda         kernel_range_low = 1ULL << 63;
3606ba6d3d1SJason Molenda         kernel_range_high = UINT64_MAX;
3616ba6d3d1SJason Molenda     }
3626ba6d3d1SJason Molenda     else
3636ba6d3d1SJason Molenda     {
3646ba6d3d1SJason Molenda         kernel_range_low = 1ULL << 31;
3656ba6d3d1SJason Molenda         kernel_range_high = UINT32_MAX;
3666ba6d3d1SJason Molenda     }
3676ba6d3d1SJason Molenda 
3686ba6d3d1SJason Molenda     // Stepping through memory at one-megabyte resolution looking for a kernel
3696ba6d3d1SJason Molenda     // rarely works (fast enough) with a 64-bit address space -- for now, let's
3706ba6d3d1SJason Molenda     // not even bother.  We may be attaching to something which *isn't* a kernel
3716ba6d3d1SJason Molenda     // and we don't want to spin for minutes on-end looking for a kernel.
3726ba6d3d1SJason Molenda     if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
3736ba6d3d1SJason Molenda         return LLDB_INVALID_ADDRESS;
3746ba6d3d1SJason Molenda 
3756ba6d3d1SJason Molenda     addr_t addr = kernel_range_low;
3766ba6d3d1SJason Molenda 
3776ba6d3d1SJason Molenda     while (addr >= kernel_range_low && addr < kernel_range_high)
3786ba6d3d1SJason Molenda     {
3796ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr, process).IsValid())
3806ba6d3d1SJason Molenda             return addr;
3816ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr + 0x1000, process).IsValid())
3826ba6d3d1SJason Molenda             return addr + 0x1000;
3836ba6d3d1SJason Molenda         if (CheckForKernelImageAtAddress (addr + 0x2000, process).IsValid())
3846ba6d3d1SJason Molenda             return addr + 0x2000;
385a02869deSJason Molenda         if (CheckForKernelImageAtAddress (addr + 0x4000, process).IsValid())
386a02869deSJason Molenda             return addr + 0x4000;
3876ba6d3d1SJason Molenda         addr += 0x100000;
3886ba6d3d1SJason Molenda     }
3896ba6d3d1SJason Molenda     return LLDB_INVALID_ADDRESS;
3906ba6d3d1SJason Molenda }
3916ba6d3d1SJason Molenda 
3926ba6d3d1SJason Molenda //----------------------------------------------------------------------
3936ba6d3d1SJason Molenda // Given an address in memory, look to see if there is a kernel image at that
3946ba6d3d1SJason Molenda // address.
3956ba6d3d1SJason Molenda // Returns a UUID; if a kernel was not found at that address, UUID.IsValid() will be false.
3966ba6d3d1SJason Molenda //----------------------------------------------------------------------
3976ba6d3d1SJason Molenda lldb_private::UUID
3986ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress (lldb::addr_t addr, Process *process)
3996ba6d3d1SJason Molenda {
4006ba6d3d1SJason Molenda     if (addr == LLDB_INVALID_ADDRESS)
4016ba6d3d1SJason Molenda         return UUID();
4026ba6d3d1SJason Molenda 
4036ba6d3d1SJason Molenda     // First try a quick test -- read the first 4 bytes and see if there is a valid Mach-O magic field there
4046ba6d3d1SJason Molenda     // (the first field of the mach_header/mach_header_64 struct).
4056ba6d3d1SJason Molenda 
4066ba6d3d1SJason Molenda     Error read_error;
4076ba6d3d1SJason Molenda     uint64_t result = process->ReadUnsignedIntegerFromMemory (addr, 4, LLDB_INVALID_ADDRESS, read_error);
408510938e5SCharles Davis     if (result != llvm::MachO::MH_MAGIC_64
409510938e5SCharles Davis         && result != llvm::MachO::MH_MAGIC
410510938e5SCharles Davis         && result != llvm::MachO::MH_CIGAM
411510938e5SCharles Davis         && result != llvm::MachO::MH_CIGAM_64)
4126ba6d3d1SJason Molenda     {
4136ba6d3d1SJason Molenda         return UUID();
4146ba6d3d1SJason Molenda     }
4156ba6d3d1SJason Molenda 
4166ba6d3d1SJason Molenda     // Read the mach header and see whether it looks like a kernel
4176ba6d3d1SJason Molenda     llvm::MachO::mach_header header;
4186ba6d3d1SJason Molenda     if (process->DoReadMemory (addr, &header, sizeof(header), read_error) != sizeof(header))
4196ba6d3d1SJason Molenda         return UUID();
4206ba6d3d1SJason Molenda 
421510938e5SCharles Davis     if (header.magic == llvm::MachO::MH_CIGAM ||
422510938e5SCharles Davis         header.magic == llvm::MachO::MH_CIGAM_64)
4236ba6d3d1SJason Molenda     {
4246ba6d3d1SJason Molenda         header.magic        = llvm::ByteSwap_32(header.magic);
4256ba6d3d1SJason Molenda         header.cputype      = llvm::ByteSwap_32(header.cputype);
4266ba6d3d1SJason Molenda         header.cpusubtype   = llvm::ByteSwap_32(header.cpusubtype);
4276ba6d3d1SJason Molenda         header.filetype     = llvm::ByteSwap_32(header.filetype);
4286ba6d3d1SJason Molenda         header.ncmds        = llvm::ByteSwap_32(header.ncmds);
4296ba6d3d1SJason Molenda         header.sizeofcmds   = llvm::ByteSwap_32(header.sizeofcmds);
4306ba6d3d1SJason Molenda         header.flags        = llvm::ByteSwap_32(header.flags);
4316ba6d3d1SJason Molenda     }
4326ba6d3d1SJason Molenda 
4336ba6d3d1SJason Molenda     // A kernel is an executable which does not have the dynamic link object flag set.
434510938e5SCharles Davis     if (header.filetype == llvm::MachO::MH_EXECUTE
435510938e5SCharles Davis         && (header.flags & llvm::MachO::MH_DYLDLINK) == 0)
4366ba6d3d1SJason Molenda     {
4376ba6d3d1SJason Molenda         // Create a full module to get the UUID
43839f7ee86SGreg Clayton         ModuleSP memory_module_sp = process->ReadModuleFromMemory (FileSpec ("temp_mach_kernel", false), addr);
4396ba6d3d1SJason Molenda         if (!memory_module_sp.get())
4406ba6d3d1SJason Molenda             return UUID();
4416ba6d3d1SJason Molenda 
4426ba6d3d1SJason Molenda         ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
4436ba6d3d1SJason Molenda         if (exe_objfile == NULL)
4446ba6d3d1SJason Molenda             return UUID();
4456ba6d3d1SJason Molenda 
4466ba6d3d1SJason Molenda         if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
4476ba6d3d1SJason Molenda         {
448a4ce2538SJason Molenda             ArchSpec kernel_arch (eArchTypeMachO, header.cputype, header.cpusubtype);
449a4ce2538SJason Molenda             if (!process->GetTarget().GetArchitecture().IsCompatibleMatch(kernel_arch))
450a4ce2538SJason Molenda             {
451a4ce2538SJason Molenda                 process->GetTarget().SetArchitecture (kernel_arch);
452a4ce2538SJason Molenda             }
4536ba6d3d1SJason Molenda             return memory_module_sp->GetUUID();
4546ba6d3d1SJason Molenda         }
4556ba6d3d1SJason Molenda     }
4566ba6d3d1SJason Molenda 
4576ba6d3d1SJason Molenda     return UUID();
4586ba6d3d1SJason Molenda }
4596ba6d3d1SJason Molenda 
4606ba6d3d1SJason Molenda //----------------------------------------------------------------------
461d4bfbc9aSGreg Clayton // Constructor
462d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
4636ba6d3d1SJason Molenda DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process, lldb::addr_t kernel_addr) :
464d4bfbc9aSGreg Clayton     DynamicLoader(process),
4656ba6d3d1SJason Molenda     m_kernel_load_address (kernel_addr),
466d4bfbc9aSGreg Clayton     m_kernel(),
467d4bfbc9aSGreg Clayton     m_kext_summary_header_ptr_addr (),
468d4bfbc9aSGreg Clayton     m_kext_summary_header_addr (),
469d4bfbc9aSGreg Clayton     m_kext_summary_header (),
470306bd0aaSJason Molenda     m_known_kexts (),
471a08823fdSDaniel Dunbar     m_mutex(Mutex::eMutexTypeRecursive),
472a08823fdSDaniel Dunbar     m_break_id (LLDB_INVALID_BREAK_ID)
473d4bfbc9aSGreg Clayton {
474615eb7e6SGreg Clayton     Error error;
475615eb7e6SGreg Clayton     PlatformSP platform_sp(Platform::Create(PlatformDarwinKernel::GetPluginNameStatic(), error));
4761c627543SJason Molenda     // Only select the darwin-kernel Platform if we've been asked to load kexts.
4771c627543SJason Molenda     // It can take some time to scan over all of the kext info.plists and that
4781c627543SJason Molenda     // shouldn't be done if kext loading is explicitly disabled.
4791c627543SJason Molenda     if (platform_sp.get() && GetGlobalProperties()->GetLoadKexts())
4801c627543SJason Molenda     {
4811c627543SJason Molenda         process->GetTarget().SetPlatform (platform_sp);
4821c627543SJason Molenda     }
483d4bfbc9aSGreg Clayton }
484d4bfbc9aSGreg Clayton 
485d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
486d4bfbc9aSGreg Clayton // Destructor
487d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
488944b828aSGreg Clayton DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
489d4bfbc9aSGreg Clayton {
490d4bfbc9aSGreg Clayton     Clear(true);
491d4bfbc9aSGreg Clayton }
492d4bfbc9aSGreg Clayton 
493d4bfbc9aSGreg Clayton void
494944b828aSGreg Clayton DynamicLoaderDarwinKernel::UpdateIfNeeded()
495d4bfbc9aSGreg Clayton {
496d4bfbc9aSGreg Clayton     LoadKernelModuleIfNeeded();
497d4bfbc9aSGreg Clayton     SetNotificationBreakpointIfNeeded ();
498d4bfbc9aSGreg Clayton }
499d4bfbc9aSGreg Clayton //------------------------------------------------------------------
500d4bfbc9aSGreg Clayton /// Called after attaching a process.
501d4bfbc9aSGreg Clayton ///
502d4bfbc9aSGreg Clayton /// Allow DynamicLoader plug-ins to execute some code after
503d4bfbc9aSGreg Clayton /// attaching to a process.
504d4bfbc9aSGreg Clayton //------------------------------------------------------------------
505d4bfbc9aSGreg Clayton void
506944b828aSGreg Clayton DynamicLoaderDarwinKernel::DidAttach ()
507d4bfbc9aSGreg Clayton {
508d4bfbc9aSGreg Clayton     PrivateInitialize(m_process);
509d4bfbc9aSGreg Clayton     UpdateIfNeeded();
510d4bfbc9aSGreg Clayton }
511d4bfbc9aSGreg Clayton 
512d4bfbc9aSGreg Clayton //------------------------------------------------------------------
513d4bfbc9aSGreg Clayton /// Called after attaching a process.
514d4bfbc9aSGreg Clayton ///
515d4bfbc9aSGreg Clayton /// Allow DynamicLoader plug-ins to execute some code after
516d4bfbc9aSGreg Clayton /// attaching to a process.
517d4bfbc9aSGreg Clayton //------------------------------------------------------------------
518d4bfbc9aSGreg Clayton void
519944b828aSGreg Clayton DynamicLoaderDarwinKernel::DidLaunch ()
520d4bfbc9aSGreg Clayton {
521d4bfbc9aSGreg Clayton     PrivateInitialize(m_process);
522d4bfbc9aSGreg Clayton     UpdateIfNeeded();
523d4bfbc9aSGreg Clayton }
524d4bfbc9aSGreg Clayton 
525d4bfbc9aSGreg Clayton 
526d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
527d4bfbc9aSGreg Clayton // Clear out the state of this class.
528d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
529d4bfbc9aSGreg Clayton void
530944b828aSGreg Clayton DynamicLoaderDarwinKernel::Clear (bool clear_process)
531d4bfbc9aSGreg Clayton {
532d4bfbc9aSGreg Clayton     Mutex::Locker locker(m_mutex);
533d4bfbc9aSGreg Clayton 
534d4bfbc9aSGreg Clayton     if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
535d4bfbc9aSGreg Clayton         m_process->ClearBreakpointSiteByID(m_break_id);
536d4bfbc9aSGreg Clayton 
537d4bfbc9aSGreg Clayton     if (clear_process)
538d4bfbc9aSGreg Clayton         m_process = NULL;
539306bd0aaSJason Molenda     m_kernel.Clear();
540306bd0aaSJason Molenda     m_known_kexts.clear();
541d4bfbc9aSGreg Clayton     m_kext_summary_header_ptr_addr.Clear();
542d4bfbc9aSGreg Clayton     m_kext_summary_header_addr.Clear();
543d4bfbc9aSGreg Clayton     m_break_id = LLDB_INVALID_BREAK_ID;
544d4bfbc9aSGreg Clayton }
545d4bfbc9aSGreg Clayton 
546d4bfbc9aSGreg Clayton 
547c859e2d5SGreg Clayton bool
548306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::LoadImageAtFileAddress (Process *process)
5492af282a1SGreg Clayton {
5502af282a1SGreg Clayton     if (IsLoaded())
5512af282a1SGreg Clayton         return true;
5522af282a1SGreg Clayton 
553306bd0aaSJason Molenda     if (m_module_sp)
5542af282a1SGreg Clayton     {
5552af282a1SGreg Clayton         bool changed = false;
556751caf65SGreg Clayton         if (m_module_sp->SetLoadAddress (process->GetTarget(), 0, true, changed))
557306bd0aaSJason Molenda             m_load_process_stop_id = process->GetStopID();
5582af282a1SGreg Clayton     }
5592af282a1SGreg Clayton     return false;
5602af282a1SGreg Clayton }
5612af282a1SGreg Clayton 
562306bd0aaSJason Molenda void
563306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetModule (ModuleSP module_sp)
564306bd0aaSJason Molenda {
565306bd0aaSJason Molenda     m_module_sp = module_sp;
566306bd0aaSJason Molenda     if (module_sp.get() && module_sp->GetObjectFile())
567306bd0aaSJason Molenda     {
568306bd0aaSJason Molenda         if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
569306bd0aaSJason Molenda             && module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
570306bd0aaSJason Molenda         {
571306bd0aaSJason Molenda             m_kernel_image = true;
572306bd0aaSJason Molenda         }
573306bd0aaSJason Molenda         else
574306bd0aaSJason Molenda         {
575306bd0aaSJason Molenda             m_kernel_image = false;
576306bd0aaSJason Molenda         }
577306bd0aaSJason Molenda     }
578306bd0aaSJason Molenda }
579306bd0aaSJason Molenda 
580306bd0aaSJason Molenda ModuleSP
581306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetModule ()
582306bd0aaSJason Molenda {
583306bd0aaSJason Molenda     return m_module_sp;
584306bd0aaSJason Molenda }
585306bd0aaSJason Molenda 
586306bd0aaSJason Molenda void
587306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetLoadAddress (addr_t load_addr)
588306bd0aaSJason Molenda {
589306bd0aaSJason Molenda     m_load_address = load_addr;
590306bd0aaSJason Molenda }
591306bd0aaSJason Molenda 
592306bd0aaSJason Molenda addr_t
593306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetLoadAddress () const
594306bd0aaSJason Molenda {
595306bd0aaSJason Molenda     return m_load_address;
596306bd0aaSJason Molenda }
597306bd0aaSJason Molenda 
598306bd0aaSJason Molenda uint64_t
599306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetSize () const
600306bd0aaSJason Molenda {
601306bd0aaSJason Molenda     return m_size;
602306bd0aaSJason Molenda }
603306bd0aaSJason Molenda 
604306bd0aaSJason Molenda void
605306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetSize (uint64_t size)
606306bd0aaSJason Molenda {
607306bd0aaSJason Molenda     m_size = size;
608306bd0aaSJason Molenda }
609306bd0aaSJason Molenda 
610306bd0aaSJason Molenda uint32_t
611306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetProcessStopId () const
612306bd0aaSJason Molenda {
613306bd0aaSJason Molenda     return m_load_process_stop_id;
614306bd0aaSJason Molenda }
615306bd0aaSJason Molenda 
616306bd0aaSJason Molenda void
617306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetProcessStopId (uint32_t stop_id)
618306bd0aaSJason Molenda {
619306bd0aaSJason Molenda     m_load_process_stop_id = stop_id;
620306bd0aaSJason Molenda }
621306bd0aaSJason Molenda 
6222af282a1SGreg Clayton bool
623306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::operator== (const KextImageInfo &rhs)
624306bd0aaSJason Molenda {
625306bd0aaSJason Molenda     if (m_uuid.IsValid() || rhs.GetUUID().IsValid())
626306bd0aaSJason Molenda     {
627306bd0aaSJason Molenda         if (m_uuid == rhs.GetUUID())
628306bd0aaSJason Molenda         {
629306bd0aaSJason Molenda             return true;
630306bd0aaSJason Molenda         }
631306bd0aaSJason Molenda         return false;
632306bd0aaSJason Molenda     }
633306bd0aaSJason Molenda 
634306bd0aaSJason Molenda     if (m_name == rhs.GetName() && m_load_address == rhs.GetLoadAddress())
635306bd0aaSJason Molenda         return true;
636306bd0aaSJason Molenda 
637306bd0aaSJason Molenda     return false;
638306bd0aaSJason Molenda }
639306bd0aaSJason Molenda 
640306bd0aaSJason Molenda void
641306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetName (const char *name)
642306bd0aaSJason Molenda {
643306bd0aaSJason Molenda     m_name = name;
644306bd0aaSJason Molenda }
645306bd0aaSJason Molenda 
646306bd0aaSJason Molenda std::string
647306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetName () const
648306bd0aaSJason Molenda {
649306bd0aaSJason Molenda     return m_name;
650306bd0aaSJason Molenda }
651306bd0aaSJason Molenda 
652306bd0aaSJason Molenda void
653306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetUUID (const UUID &uuid)
654306bd0aaSJason Molenda {
655306bd0aaSJason Molenda     m_uuid = uuid;
656306bd0aaSJason Molenda }
657306bd0aaSJason Molenda 
658306bd0aaSJason Molenda UUID
659306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetUUID () const
660306bd0aaSJason Molenda {
661306bd0aaSJason Molenda     return m_uuid;
662306bd0aaSJason Molenda }
663306bd0aaSJason Molenda 
664306bd0aaSJason Molenda // Given the m_load_address from the kext summaries, and a UUID, try to create an in-memory
665306bd0aaSJason Molenda // Module at that address.  Require that the MemoryModule have a matching UUID and detect
666306bd0aaSJason Molenda // if this MemoryModule is a kernel or a kext.
667306bd0aaSJason Molenda //
668306bd0aaSJason Molenda // Returns true if m_memory_module_sp is now set to a valid Module.
669306bd0aaSJason Molenda 
670306bd0aaSJason Molenda bool
671306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule (Process *process)
672306bd0aaSJason Molenda {
67308a3258bSJason Molenda     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
674306bd0aaSJason Molenda     if (m_memory_module_sp.get() != NULL)
675306bd0aaSJason Molenda         return true;
676306bd0aaSJason Molenda     if (m_load_address == LLDB_INVALID_ADDRESS)
677306bd0aaSJason Molenda         return false;
678306bd0aaSJason Molenda 
679306bd0aaSJason Molenda     FileSpec file_spec;
680306bd0aaSJason Molenda     file_spec.SetFile (m_name.c_str(), false);
681306bd0aaSJason Molenda 
682306bd0aaSJason Molenda     ModuleSP memory_module_sp = process->ReadModuleFromMemory (file_spec, m_load_address);
683306bd0aaSJason Molenda 
684306bd0aaSJason Molenda     if (memory_module_sp.get() == NULL)
685306bd0aaSJason Molenda         return false;
686306bd0aaSJason Molenda 
687306bd0aaSJason Molenda     bool is_kernel = false;
688306bd0aaSJason Molenda     if (memory_module_sp->GetObjectFile())
689306bd0aaSJason Molenda     {
690306bd0aaSJason Molenda         if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable
691306bd0aaSJason Molenda             && memory_module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel)
692306bd0aaSJason Molenda         {
693306bd0aaSJason Molenda             is_kernel = true;
694306bd0aaSJason Molenda         }
695306bd0aaSJason Molenda         else if (memory_module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeSharedLibrary)
696306bd0aaSJason Molenda         {
697306bd0aaSJason Molenda             is_kernel = false;
698306bd0aaSJason Molenda         }
699306bd0aaSJason Molenda     }
700306bd0aaSJason Molenda 
70138e70d11SJason Molenda     // If this is a kext, and the kernel specified what UUID we should find at this
70238e70d11SJason Molenda     // load address, require that the memory module have a matching UUID or something
70338e70d11SJason Molenda     // has gone wrong and we should discard it.
704306bd0aaSJason Molenda     if (m_uuid.IsValid())
705306bd0aaSJason Molenda     {
706306bd0aaSJason Molenda         if (m_uuid != memory_module_sp->GetUUID())
707306bd0aaSJason Molenda         {
70808a3258bSJason Molenda             if (log)
70908a3258bSJason Molenda             {
71008a3258bSJason Molenda                 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());
71108a3258bSJason Molenda             }
712306bd0aaSJason Molenda             return false;
713306bd0aaSJason Molenda         }
714306bd0aaSJason Molenda     }
715306bd0aaSJason Molenda 
716306bd0aaSJason Molenda     // If the in-memory Module has a UUID, let's use that.
717306bd0aaSJason Molenda     if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid())
718306bd0aaSJason Molenda     {
719306bd0aaSJason Molenda         m_uuid = memory_module_sp->GetUUID();
720306bd0aaSJason Molenda     }
721306bd0aaSJason Molenda 
722306bd0aaSJason Molenda     m_memory_module_sp = memory_module_sp;
723306bd0aaSJason Molenda     m_kernel_image = is_kernel;
724306bd0aaSJason Molenda     if (is_kernel)
725306bd0aaSJason Molenda     {
72608a3258bSJason Molenda         if (log)
72708a3258bSJason Molenda         {
72808a3258bSJason Molenda             // This is unusual and probably not intended
72908a3258bSJason Molenda             log->Printf ("KextImageInfo::ReadMemoryModule read the kernel binary out of memory");
73008a3258bSJason Molenda         }
731306bd0aaSJason Molenda         if (memory_module_sp->GetArchitecture().IsValid())
732306bd0aaSJason Molenda         {
733306bd0aaSJason Molenda             process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
734306bd0aaSJason Molenda         }
73538e70d11SJason Molenda         if (m_uuid.IsValid())
73638e70d11SJason Molenda         {
73748975833SJason Molenda             ModuleSP exe_module_sp = process->GetTarget().GetExecutableModule();
73848975833SJason Molenda             if (exe_module_sp.get() && exe_module_sp->GetUUID().IsValid())
73938e70d11SJason Molenda             {
74048975833SJason Molenda                 if (m_uuid != exe_module_sp->GetUUID())
74138e70d11SJason Molenda                 {
74248975833SJason Molenda                     // The user specified a kernel binary that has a different UUID than
74348975833SJason Molenda                     // the kernel actually running in memory.  This never ends well;
74448975833SJason Molenda                     // clear the user specified kernel binary from the Target.
74548975833SJason Molenda 
74648975833SJason Molenda                     m_module_sp.reset();
74748975833SJason Molenda 
74848975833SJason Molenda                     ModuleList user_specified_kernel_list;
74948975833SJason Molenda                     user_specified_kernel_list.Append (exe_module_sp);
75048975833SJason Molenda                     process->GetTarget().GetImages().Remove (user_specified_kernel_list);
75138e70d11SJason Molenda                 }
75238e70d11SJason Molenda             }
75338e70d11SJason Molenda         }
754306bd0aaSJason Molenda     }
755306bd0aaSJason Molenda 
756306bd0aaSJason Molenda     return true;
757306bd0aaSJason Molenda }
758306bd0aaSJason Molenda 
759306bd0aaSJason Molenda bool
760306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::IsKernel () const
761306bd0aaSJason Molenda {
762306bd0aaSJason Molenda     return m_kernel_image == true;
763306bd0aaSJason Molenda }
764306bd0aaSJason Molenda 
765306bd0aaSJason Molenda void
766306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel (bool is_kernel)
767306bd0aaSJason Molenda {
768306bd0aaSJason Molenda     m_kernel_image = is_kernel;
769306bd0aaSJason Molenda }
770306bd0aaSJason Molenda 
771306bd0aaSJason Molenda bool
772306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule (Process *process)
773c859e2d5SGreg Clayton {
774c859e2d5SGreg Clayton     if (IsLoaded())
775c859e2d5SGreg Clayton         return true;
776c859e2d5SGreg Clayton 
777c859e2d5SGreg Clayton 
778c859e2d5SGreg Clayton     Target &target = process->GetTarget();
77987a04b24SJason Molenda 
780306bd0aaSJason Molenda     // If we don't have / can't create a memory module for this kext, don't try to load it - we won't
781306bd0aaSJason Molenda     // have the correct segment load addresses.
782306bd0aaSJason Molenda     if (!ReadMemoryModule (process))
78387a04b24SJason Molenda     {
78487a04b24SJason Molenda         return false;
78587a04b24SJason Molenda     }
78687a04b24SJason Molenda 
787306bd0aaSJason Molenda     bool uuid_is_valid = m_uuid.IsValid();
788306bd0aaSJason Molenda 
789e575e7bcSJason Molenda     if (IsKernel() && uuid_is_valid && m_memory_module_sp.get())
790e575e7bcSJason Molenda     {
79144d93782SGreg Clayton         Stream *s = target.GetDebugger().GetOutputFile().get();
792e575e7bcSJason Molenda         if (s)
793e575e7bcSJason Molenda         {
794c16b4af0SJason Molenda             s->Printf ("Kernel UUID: %s\n", m_memory_module_sp->GetUUID().GetAsString().c_str());
795e575e7bcSJason Molenda             s->Printf ("Load Address: 0x%" PRIx64 "\n", m_load_address);
796e575e7bcSJason Molenda         }
797e575e7bcSJason Molenda     }
798e575e7bcSJason Molenda 
799306bd0aaSJason Molenda     if (!m_module_sp)
800c859e2d5SGreg Clayton     {
801306bd0aaSJason Molenda         // See if the kext has already been loaded into the target, probably by the user doing target modules add.
8021759848bSEnrico Granata         const ModuleList &target_images = target.GetImages();
803306bd0aaSJason Molenda         m_module_sp = target_images.FindModule(m_uuid);
804c859e2d5SGreg Clayton 
805306bd0aaSJason Molenda         // Search for the kext on the local filesystem via the UUID
806306bd0aaSJason Molenda         if (!m_module_sp && uuid_is_valid)
807b9a01b39SGreg Clayton         {
8082af282a1SGreg Clayton             ModuleSpec module_spec;
809306bd0aaSJason Molenda             module_spec.GetUUID() = m_uuid;
81053667f5dSJason Molenda             module_spec.GetArchitecture() = target.GetArchitecture();
811bb860bd2SJason Molenda 
812d76fb6eaSJason Molenda             // For the kernel, we really do need an on-disk file copy of the binary to do anything useful.
813d76fb6eaSJason Molenda             // This will force a clal to
814306bd0aaSJason Molenda             if (IsKernel())
815bb860bd2SJason Molenda             {
816d76fb6eaSJason Molenda                 if (Symbols::DownloadObjectAndSymbolFile (module_spec, true))
817bb860bd2SJason Molenda                 {
818bb860bd2SJason Molenda                     if (module_spec.GetFileSpec().Exists())
819bb860bd2SJason Molenda                     {
820306bd0aaSJason Molenda                         m_module_sp.reset(new Module (module_spec.GetFileSpec(), target.GetArchitecture()));
821306bd0aaSJason Molenda                         if (m_module_sp.get() && m_module_sp->MatchesModuleSpec (module_spec))
822bb860bd2SJason Molenda                         {
823bb860bd2SJason Molenda                             ModuleList loaded_module_list;
824306bd0aaSJason Molenda                             loaded_module_list.Append (m_module_sp);
825bb860bd2SJason Molenda                             target.ModulesDidLoad (loaded_module_list);
826bb860bd2SJason Molenda                         }
827bb860bd2SJason Molenda                     }
828bb860bd2SJason Molenda                 }
829d76fb6eaSJason Molenda             }
830bb860bd2SJason Molenda 
8311c627543SJason Molenda             // If the current platform is PlatformDarwinKernel, create a ModuleSpec with the filename set
8321c627543SJason Molenda             // to be the bundle ID for this kext, e.g. "com.apple.filesystems.msdosfs", and ask the platform
8331c627543SJason Molenda             // to find it.
8341c627543SJason Molenda             PlatformSP platform_sp (target.GetPlatform());
8350ddfe7f8SJason Molenda             if (!m_module_sp && platform_sp)
8361c627543SJason Molenda             {
83757abc5d6SGreg Clayton                 ConstString platform_name (platform_sp->GetPluginName());
83857abc5d6SGreg Clayton                 static ConstString g_platform_name (PlatformDarwinKernel::GetPluginNameStatic());
83957abc5d6SGreg Clayton                 if (platform_name == g_platform_name)
8401c627543SJason Molenda                 {
8411c627543SJason Molenda                     ModuleSpec kext_bundle_module_spec(module_spec);
8421c627543SJason Molenda                     FileSpec kext_filespec(m_name.c_str(), false);
8431c627543SJason Molenda                     kext_bundle_module_spec.GetFileSpec() = kext_filespec;
844667ef229SIlia K                     platform_sp->GetSharedModule (kext_bundle_module_spec, process, m_module_sp, &target.GetExecutableSearchPaths(), NULL, NULL);
8451c627543SJason Molenda                 }
8461c627543SJason Molenda             }
8471c627543SJason Molenda 
848d76fb6eaSJason Molenda             // Ask the Target to find this file on the local system, if possible.
849bb860bd2SJason Molenda             // This will search in the list of currently-loaded files, look in the
850bb860bd2SJason Molenda             // standard search paths on the system, and on a Mac it will try calling
851bb860bd2SJason Molenda             // the DebugSymbols framework with the UUID to find the binary via its
852bb860bd2SJason Molenda             // search methods.
853306bd0aaSJason Molenda             if (!m_module_sp)
854bb860bd2SJason Molenda             {
855306bd0aaSJason Molenda                 m_module_sp = target.GetSharedModule (module_spec);
856306bd0aaSJason Molenda             }
857e575e7bcSJason Molenda 
858d76fb6eaSJason Molenda             if (IsKernel() && !m_module_sp)
859e575e7bcSJason Molenda             {
86044d93782SGreg Clayton                 Stream *s = target.GetDebugger().GetOutputFile().get();
861e575e7bcSJason Molenda                 if (s)
862e575e7bcSJason Molenda                 {
863cc6dc780SJason Molenda                     s->Printf ("WARNING: Unable to locate kernel binary on the debugger system.\n");
864e575e7bcSJason Molenda                 }
865e575e7bcSJason Molenda             }
866b9a01b39SGreg Clayton         }
86765d57a3dSJason Molenda 
868306bd0aaSJason Molenda         // If we managed to find a module, append it to the target's list of images.
869306bd0aaSJason Molenda         // If we also have a memory module, require that they have matching UUIDs
870306bd0aaSJason Molenda         if (m_module_sp)
87165d57a3dSJason Molenda         {
872306bd0aaSJason Molenda             bool uuid_match_ok = true;
873306bd0aaSJason Molenda             if (m_memory_module_sp)
87465d57a3dSJason Molenda             {
875306bd0aaSJason Molenda                 if (m_module_sp->GetUUID() != m_memory_module_sp->GetUUID())
876306bd0aaSJason Molenda                 {
877306bd0aaSJason Molenda                     uuid_match_ok = false;
87865d57a3dSJason Molenda                 }
87965d57a3dSJason Molenda             }
880306bd0aaSJason Molenda             if (uuid_match_ok)
881306bd0aaSJason Molenda             {
882a4d3e1d2SJason Molenda                 target.GetImages().AppendIfNeeded(m_module_sp);
883306bd0aaSJason Molenda                 if (IsKernel() && target.GetExecutableModulePointer() != m_module_sp.get())
884306bd0aaSJason Molenda                 {
885306bd0aaSJason Molenda                     target.SetExecutableModule (m_module_sp, false);
886306bd0aaSJason Molenda                 }
887c859e2d5SGreg Clayton             }
888c859e2d5SGreg Clayton         }
889bb860bd2SJason Molenda     }
890c859e2d5SGreg Clayton 
89156c23285SJason Molenda     if (!m_module_sp && !IsKernel() && m_uuid.IsValid() && !m_name.empty())
89256c23285SJason Molenda     {
89344d93782SGreg Clayton         Stream *s = target.GetDebugger().GetOutputFile().get();
89456c23285SJason Molenda         if (s)
89556c23285SJason Molenda         {
89656c23285SJason Molenda             s->Printf ("warning: Can't find binary/dSYM for %s (%s)\n",
897c16b4af0SJason Molenda                        m_name.c_str(), m_uuid.GetAsString().c_str());
89856c23285SJason Molenda         }
89956c23285SJason Molenda     }
900c859e2d5SGreg Clayton 
90167408537SGreg Clayton     static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
90267408537SGreg Clayton 
903306bd0aaSJason Molenda     if (m_memory_module_sp && m_module_sp)
904c859e2d5SGreg Clayton     {
905306bd0aaSJason Molenda         if (m_module_sp->GetUUID() == m_memory_module_sp->GetUUID())
906c859e2d5SGreg Clayton         {
907306bd0aaSJason Molenda             ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
908306bd0aaSJason Molenda             ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
90967408537SGreg Clayton 
910c859e2d5SGreg Clayton             if (memory_object_file && ondisk_object_file)
911c859e2d5SGreg Clayton             {
912306bd0aaSJason Molenda                 // The memory_module for kexts may have an invalid __LINKEDIT seg; skip it.
913306bd0aaSJason Molenda                 const bool ignore_linkedit = !IsKernel ();
91467408537SGreg Clayton 
915c859e2d5SGreg Clayton                 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
916c859e2d5SGreg Clayton                 SectionList *memory_section_list = memory_object_file->GetSectionList ();
917c859e2d5SGreg Clayton                 if (memory_section_list && ondisk_section_list)
918c859e2d5SGreg Clayton                 {
919bae2f2f8SGreg Clayton                     const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
920c859e2d5SGreg Clayton                     // There may be CTF sections in the memory image so we can't
921c859e2d5SGreg Clayton                     // always just compare the number of sections (which are actually
922c859e2d5SGreg Clayton                     // segments in mach-o parlance)
923c859e2d5SGreg Clayton                     uint32_t sect_idx = 0;
924bae2f2f8SGreg Clayton 
92553667f5dSJason Molenda                     // Use the memory_module's addresses for each section to set the
92653667f5dSJason Molenda                     // file module's load address as appropriate.  We don't want to use
92753667f5dSJason Molenda                     // a single slide value for the entire kext - different segments may
92853667f5dSJason Molenda                     // be slid different amounts by the kext loader.
929bae2f2f8SGreg Clayton 
930bae2f2f8SGreg Clayton                     uint32_t num_sections_loaded = 0;
931bae2f2f8SGreg Clayton                     for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
932c859e2d5SGreg Clayton                     {
9337820bd1eSGreg Clayton                         SectionSP ondisk_section_sp(ondisk_section_list->GetSectionAtIndex(sect_idx));
9347820bd1eSGreg Clayton                         if (ondisk_section_sp)
935c859e2d5SGreg Clayton                         {
93667408537SGreg Clayton                             // Don't ever load __LINKEDIT as it may or may not be actually
93767408537SGreg Clayton                             // mapped into memory and there is no current way to tell.
93867408537SGreg Clayton                             // I filed rdar://problem/12851706 to track being able to tell
93967408537SGreg Clayton                             // if the __LINKEDIT is actually mapped, but until then, we need
94067408537SGreg Clayton                             // to not load the __LINKEDIT
94167408537SGreg Clayton                             if (ignore_linkedit && ondisk_section_sp->GetName() == g_section_name_LINKEDIT)
94267408537SGreg Clayton                                 continue;
94367408537SGreg Clayton 
9447820bd1eSGreg Clayton                             const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section_sp->GetName()).get();
945bae2f2f8SGreg Clayton                             if (memory_section)
946c859e2d5SGreg Clayton                             {
947d5944cd1SGreg Clayton                                 target.SetSectionLoadAddress (ondisk_section_sp, memory_section->GetFileAddress());
948bae2f2f8SGreg Clayton                                 ++num_sections_loaded;
949c859e2d5SGreg Clayton                             }
950bae2f2f8SGreg Clayton                         }
951bae2f2f8SGreg Clayton                     }
952bae2f2f8SGreg Clayton                     if (num_sections_loaded > 0)
953306bd0aaSJason Molenda                         m_load_process_stop_id = process->GetStopID();
954bae2f2f8SGreg Clayton                     else
955306bd0aaSJason Molenda                         m_module_sp.reset(); // No sections were loaded
956c859e2d5SGreg Clayton                 }
957c859e2d5SGreg Clayton                 else
958306bd0aaSJason Molenda                     m_module_sp.reset(); // One or both section lists
959c859e2d5SGreg Clayton             }
960c859e2d5SGreg Clayton             else
961306bd0aaSJason Molenda                 m_module_sp.reset(); // One or both object files missing
962c859e2d5SGreg Clayton         }
963c859e2d5SGreg Clayton         else
964306bd0aaSJason Molenda             m_module_sp.reset(); // UUID mismatch
965c859e2d5SGreg Clayton     }
966c859e2d5SGreg Clayton 
967c859e2d5SGreg Clayton     bool is_loaded = IsLoaded();
968c859e2d5SGreg Clayton 
969306bd0aaSJason Molenda     if (is_loaded && m_module_sp && IsKernel())
97068b3607fSJason Molenda     {
97144d93782SGreg Clayton         Stream *s = target.GetDebugger().GetOutputFile().get();
97268b3607fSJason Molenda         if (s)
97368b3607fSJason Molenda         {
974732238cdSJason Molenda             ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
975732238cdSJason Molenda             if (kernel_object_file)
976732238cdSJason Molenda             {
977732238cdSJason Molenda                 addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
978732238cdSJason Molenda                 if (m_load_address != LLDB_INVALID_ADDRESS && file_address != LLDB_INVALID_ADDRESS)
979732238cdSJason Molenda                 {
980787d1623SMatt Kopec                     s->Printf ("Kernel slid 0x%" PRIx64 " in memory.\n", m_load_address - file_address);
981732238cdSJason Molenda                 }
982732238cdSJason Molenda             }
983743e4396SJason Molenda             {
984b5ad4ec7SGreg Clayton                 s->Printf ("Loaded kernel file %s\n",
985b5ad4ec7SGreg Clayton                            m_module_sp->GetFileSpec().GetPath().c_str());
986743e4396SJason Molenda             }
98768b3607fSJason Molenda             s->Flush ();
98868b3607fSJason Molenda         }
98968b3607fSJason Molenda     }
990c859e2d5SGreg Clayton     return is_loaded;
991c859e2d5SGreg Clayton }
992c859e2d5SGreg Clayton 
9931f746071SGreg Clayton uint32_t
994306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetAddressByteSize ()
9951f746071SGreg Clayton {
996306bd0aaSJason Molenda     if (m_memory_module_sp)
997306bd0aaSJason Molenda         return m_memory_module_sp->GetArchitecture().GetAddressByteSize();
998306bd0aaSJason Molenda     if (m_module_sp)
999306bd0aaSJason Molenda         return m_module_sp->GetArchitecture().GetAddressByteSize();
10001f746071SGreg Clayton     return 0;
10011f746071SGreg Clayton }
10021f746071SGreg Clayton 
10031f746071SGreg Clayton lldb::ByteOrder
1004306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetByteOrder()
10051f746071SGreg Clayton {
1006306bd0aaSJason Molenda     if (m_memory_module_sp)
1007306bd0aaSJason Molenda         return m_memory_module_sp->GetArchitecture().GetByteOrder();
1008306bd0aaSJason Molenda     if (m_module_sp)
1009306bd0aaSJason Molenda         return m_module_sp->GetArchitecture().GetByteOrder();
1010*9ccb970fSBruce Mitchener     return endian::InlHostByteOrder();
10111f746071SGreg Clayton }
10121f746071SGreg Clayton 
10131f746071SGreg Clayton lldb_private::ArchSpec
1014306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::GetArchitecture () const
10151f746071SGreg Clayton {
1016306bd0aaSJason Molenda     if (m_memory_module_sp)
1017306bd0aaSJason Molenda         return m_memory_module_sp->GetArchitecture();
1018306bd0aaSJason Molenda     if (m_module_sp)
1019306bd0aaSJason Molenda         return m_module_sp->GetArchitecture();
10201f746071SGreg Clayton     return lldb_private::ArchSpec ();
10211f746071SGreg Clayton }
10221f746071SGreg Clayton 
10231f746071SGreg Clayton 
1024d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1025d4bfbc9aSGreg Clayton // Load the kernel module and initialize the "m_kernel" member. Return
1026d4bfbc9aSGreg Clayton // true _only_ if the kernel is loaded the first time through (subsequent
1027d4bfbc9aSGreg Clayton // calls to this function should return false after the kernel has been
1028d4bfbc9aSGreg Clayton // already loaded).
1029d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1030d4bfbc9aSGreg Clayton void
1031944b828aSGreg Clayton DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
1032d4bfbc9aSGreg Clayton {
1033d4bfbc9aSGreg Clayton     if (!m_kext_summary_header_ptr_addr.IsValid())
1034d4bfbc9aSGreg Clayton     {
1035306bd0aaSJason Molenda         m_kernel.Clear();
1036306bd0aaSJason Molenda         m_kernel.SetModule (m_process->GetTarget().GetExecutableModule());
1037306bd0aaSJason Molenda         m_kernel.SetIsKernel(true);
10384bd4e7e3SJason Molenda 
10394bd4e7e3SJason Molenda         ConstString kernel_name("mach_kernel");
1040306bd0aaSJason Molenda         if (m_kernel.GetModule().get()
1041306bd0aaSJason Molenda             && m_kernel.GetModule()->GetObjectFile()
1042306bd0aaSJason Molenda             && !m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
10434bd4e7e3SJason Molenda         {
1044306bd0aaSJason Molenda             kernel_name = m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename();
10454bd4e7e3SJason Molenda         }
1046306bd0aaSJason Molenda         m_kernel.SetName (kernel_name.AsCString());
10474bd4e7e3SJason Molenda 
1048306bd0aaSJason Molenda         if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS)
1049d4bfbc9aSGreg Clayton         {
1050306bd0aaSJason Molenda             m_kernel.SetLoadAddress(m_kernel_load_address);
1051306bd0aaSJason Molenda             if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS && m_kernel.GetModule())
1052c859e2d5SGreg Clayton             {
1053c859e2d5SGreg Clayton                 // We didn't get a hint from the process, so we will
1054c859e2d5SGreg Clayton                 // try the kernel at the address that it exists at in
1055c859e2d5SGreg Clayton                 // the file if we have one
1056306bd0aaSJason Molenda                 ObjectFile *kernel_object_file = m_kernel.GetModule()->GetObjectFile();
1057c859e2d5SGreg Clayton                 if (kernel_object_file)
10584bd4e7e3SJason Molenda                 {
10594bd4e7e3SJason Molenda                     addr_t load_address = kernel_object_file->GetHeaderAddress().GetLoadAddress(&m_process->GetTarget());
10604bd4e7e3SJason Molenda                     addr_t file_address = kernel_object_file->GetHeaderAddress().GetFileAddress();
10614bd4e7e3SJason Molenda                     if (load_address != LLDB_INVALID_ADDRESS && load_address != 0)
10624bd4e7e3SJason Molenda                     {
1063306bd0aaSJason Molenda                         m_kernel.SetLoadAddress (load_address);
10644bd4e7e3SJason Molenda                         if (load_address != file_address)
10654bd4e7e3SJason Molenda                         {
10664bd4e7e3SJason Molenda                             // Don't accidentally relocate the kernel to the File address --
10674bd4e7e3SJason Molenda                             // the Load address has already been set to its actual in-memory address.
10684bd4e7e3SJason Molenda                             // Mark it as IsLoaded.
1069306bd0aaSJason Molenda                             m_kernel.SetProcessStopId (m_process->GetStopID());
10704bd4e7e3SJason Molenda                         }
10714bd4e7e3SJason Molenda                     }
10724bd4e7e3SJason Molenda                     else
10734bd4e7e3SJason Molenda                     {
1074306bd0aaSJason Molenda                         m_kernel.SetLoadAddress(file_address);
10754bd4e7e3SJason Molenda                     }
10764bd4e7e3SJason Molenda                 }
1077c859e2d5SGreg Clayton             }
1078c859e2d5SGreg Clayton         }
1079c859e2d5SGreg Clayton 
1080306bd0aaSJason Molenda         if (m_kernel.GetLoadAddress() != LLDB_INVALID_ADDRESS)
10812af282a1SGreg Clayton         {
10822af282a1SGreg Clayton             if (!m_kernel.LoadImageUsingMemoryModule (m_process))
10832af282a1SGreg Clayton             {
10842af282a1SGreg Clayton                 m_kernel.LoadImageAtFileAddress (m_process);
10852af282a1SGreg Clayton             }
10862af282a1SGreg Clayton         }
1087c859e2d5SGreg Clayton 
1088306bd0aaSJason Molenda         if (m_kernel.IsLoaded() && m_kernel.GetModule())
1089c859e2d5SGreg Clayton         {
1090d4bfbc9aSGreg Clayton             static ConstString kext_summary_symbol ("gLoadedKextSummaries");
1091306bd0aaSJason Molenda             const Symbol *symbol = m_kernel.GetModule()->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
1092d4bfbc9aSGreg Clayton             if (symbol)
1093c859e2d5SGreg Clayton             {
1094e7612134SGreg Clayton                 m_kext_summary_header_ptr_addr = symbol->GetAddress();
1095d4bfbc9aSGreg Clayton                 // Update all image infos
1096d4bfbc9aSGreg Clayton                 ReadAllKextSummaries ();
1097d4bfbc9aSGreg Clayton             }
1098d4bfbc9aSGreg Clayton         }
1099d4bfbc9aSGreg Clayton         else
1100d4bfbc9aSGreg Clayton         {
1101306bd0aaSJason Molenda             m_kernel.Clear();
1102d4bfbc9aSGreg Clayton         }
1103d4bfbc9aSGreg Clayton     }
1104d4bfbc9aSGreg Clayton }
1105d4bfbc9aSGreg Clayton 
1106d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1107d4bfbc9aSGreg Clayton // Static callback function that gets called when our DYLD notification
1108d4bfbc9aSGreg Clayton // breakpoint gets hit. We update all of our image infos and then
1109d4bfbc9aSGreg Clayton // let our super class DynamicLoader class decide if we should stop
1110d4bfbc9aSGreg Clayton // or not (based on global preference).
1111d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1112d4bfbc9aSGreg Clayton bool
1113944b828aSGreg Clayton DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
1114d4bfbc9aSGreg Clayton                                                   StoppointCallbackContext *context,
1115d4bfbc9aSGreg Clayton                                                   user_id_t break_id,
1116d4bfbc9aSGreg Clayton                                                   user_id_t break_loc_id)
1117d4bfbc9aSGreg Clayton {
1118944b828aSGreg Clayton     return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
1119d4bfbc9aSGreg Clayton }
1120d4bfbc9aSGreg Clayton 
1121d4bfbc9aSGreg Clayton bool
1122944b828aSGreg Clayton DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
1123d4bfbc9aSGreg Clayton                                           user_id_t break_id,
1124d4bfbc9aSGreg Clayton                                           user_id_t break_loc_id)
1125d4bfbc9aSGreg Clayton {
11265160ce5cSGreg Clayton     Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
1127d4bfbc9aSGreg Clayton     if (log)
1128944b828aSGreg Clayton         log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
1129d4bfbc9aSGreg Clayton 
1130d4bfbc9aSGreg Clayton     ReadAllKextSummaries ();
1131d4bfbc9aSGreg Clayton 
1132d4bfbc9aSGreg Clayton     if (log)
11335160ce5cSGreg Clayton         PutToLog(log);
1134d4bfbc9aSGreg Clayton 
1135d4bfbc9aSGreg Clayton     return GetStopWhenImagesChange();
1136d4bfbc9aSGreg Clayton }
1137d4bfbc9aSGreg Clayton 
1138d4bfbc9aSGreg Clayton 
1139d4bfbc9aSGreg Clayton bool
1140944b828aSGreg Clayton DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
1141d4bfbc9aSGreg Clayton {
1142d4bfbc9aSGreg Clayton     Mutex::Locker locker(m_mutex);
1143d4bfbc9aSGreg Clayton 
1144d4bfbc9aSGreg Clayton     // the all image infos is already valid for this process stop ID
1145d4bfbc9aSGreg Clayton 
1146d4bfbc9aSGreg Clayton     if (m_kext_summary_header_ptr_addr.IsValid())
1147d4bfbc9aSGreg Clayton     {
1148d4bfbc9aSGreg Clayton         const uint32_t addr_size = m_kernel.GetAddressByteSize ();
1149d4bfbc9aSGreg Clayton         const ByteOrder byte_order = m_kernel.GetByteOrder();
1150d4bfbc9aSGreg Clayton         Error error;
1151d4bfbc9aSGreg Clayton         // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
1152aaa0ba31SBruce Mitchener         // which is currently 4 uint32_t and a pointer.
1153d4bfbc9aSGreg Clayton         uint8_t buf[24];
1154d4bfbc9aSGreg Clayton         DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
1155d4bfbc9aSGreg Clayton         const size_t count = 4 * sizeof(uint32_t) + addr_size;
1156d4bfbc9aSGreg Clayton         const bool prefer_file_cache = false;
1157d4bfbc9aSGreg Clayton         if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
1158d4bfbc9aSGreg Clayton                                                           prefer_file_cache,
1159d4bfbc9aSGreg Clayton                                                           error,
1160d4bfbc9aSGreg Clayton                                                           m_kext_summary_header_addr))
1161d4bfbc9aSGreg Clayton         {
1162d4bfbc9aSGreg Clayton             // We got a valid address for our kext summary header and make sure it isn't NULL
1163d4bfbc9aSGreg Clayton             if (m_kext_summary_header_addr.IsValid() &&
1164d4bfbc9aSGreg Clayton                 m_kext_summary_header_addr.GetFileAddress() != 0)
1165d4bfbc9aSGreg Clayton             {
1166d4bfbc9aSGreg Clayton                 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
1167d4bfbc9aSGreg Clayton                 if (bytes_read == count)
1168d4bfbc9aSGreg Clayton                 {
1169c7bece56SGreg Clayton                     lldb::offset_t offset = 0;
1170d4bfbc9aSGreg Clayton                     m_kext_summary_header.version = data.GetU32(&offset);
1171c6fa5db7SJason Molenda                     if (m_kext_summary_header.version > 128)
1172c6fa5db7SJason Molenda                     {
1173c6fa5db7SJason Molenda                         Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1174c6fa5db7SJason Molenda                         s->Printf ("WARNING: Unable to read kext summary header, got improbable version number %u\n", m_kext_summary_header.version);
1175aaa0ba31SBruce Mitchener                         // If we get an improbably large version number, we're probably getting bad memory.
1176c6fa5db7SJason Molenda                         m_kext_summary_header_addr.Clear();
1177c6fa5db7SJason Molenda                         return false;
1178c6fa5db7SJason Molenda                     }
1179d4bfbc9aSGreg Clayton                     if (m_kext_summary_header.version >= 2)
1180d4bfbc9aSGreg Clayton                     {
1181d4bfbc9aSGreg Clayton                         m_kext_summary_header.entry_size = data.GetU32(&offset);
1182c6fa5db7SJason Molenda                         if (m_kext_summary_header.entry_size > 4096)
1183c6fa5db7SJason Molenda                         {
1184c6fa5db7SJason Molenda                             // If we get an improbably large entry_size, we're probably getting bad memory.
1185c6fa5db7SJason Molenda                             Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1186c6fa5db7SJason Molenda                             s->Printf ("WARNING: Unable to read kext summary header, got improbable entry_size %u\n", m_kext_summary_header.entry_size);
1187c6fa5db7SJason Molenda                             m_kext_summary_header_addr.Clear();
1188c6fa5db7SJason Molenda                             return false;
1189c6fa5db7SJason Molenda                         }
1190d4bfbc9aSGreg Clayton                     }
1191d4bfbc9aSGreg Clayton                     else
1192d4bfbc9aSGreg Clayton                     {
1193d4bfbc9aSGreg Clayton                         // Versions less than 2 didn't have an entry size, it was hard coded
1194d4bfbc9aSGreg Clayton                         m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
1195d4bfbc9aSGreg Clayton                     }
1196d4bfbc9aSGreg Clayton                     m_kext_summary_header.entry_count = data.GetU32(&offset);
1197c6fa5db7SJason Molenda                     if (m_kext_summary_header.entry_count > 10000)
1198c6fa5db7SJason Molenda                     {
1199c6fa5db7SJason Molenda                         // If we get an improbably large number of kexts, we're probably getting bad memory.
1200c6fa5db7SJason Molenda                         Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
1201c6fa5db7SJason Molenda                         s->Printf ("WARNING: Unable to read kext summary header, got improbable number of kexts %u\n", m_kext_summary_header.entry_count);
1202c6fa5db7SJason Molenda                         m_kext_summary_header_addr.Clear();
1203c6fa5db7SJason Molenda                         return false;
1204c6fa5db7SJason Molenda                     }
1205d4bfbc9aSGreg Clayton                     return true;
1206d4bfbc9aSGreg Clayton                 }
1207d4bfbc9aSGreg Clayton             }
1208d4bfbc9aSGreg Clayton         }
1209d4bfbc9aSGreg Clayton     }
1210d4bfbc9aSGreg Clayton     m_kext_summary_header_addr.Clear();
1211d4bfbc9aSGreg Clayton     return false;
1212d4bfbc9aSGreg Clayton }
1213d4bfbc9aSGreg Clayton 
1214306bd0aaSJason Molenda // We've either (a) just attached to a new kernel, or (b) the kexts-changed breakpoint was hit
1215306bd0aaSJason Molenda // and we need to figure out what kexts have been added or removed.
1216306bd0aaSJason Molenda // Read the kext summaries from the inferior kernel memory, compare them against the
1217306bd0aaSJason Molenda // m_known_kexts vector and update the m_known_kexts vector as needed to keep in sync with the
1218306bd0aaSJason Molenda // inferior.
1219d4bfbc9aSGreg Clayton 
1220d4bfbc9aSGreg Clayton bool
1221306bd0aaSJason Molenda DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr, uint32_t count)
1222d4bfbc9aSGreg Clayton {
1223306bd0aaSJason Molenda     KextImageInfo::collection kext_summaries;
12245160ce5cSGreg Clayton     Log *log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
1225d4bfbc9aSGreg Clayton     if (log)
1226306bd0aaSJason Molenda         log->Printf ("Kexts-changed breakpoint hit, there are %d kexts currently.\n", count);
1227d4bfbc9aSGreg Clayton 
1228d4bfbc9aSGreg Clayton     Mutex::Locker locker(m_mutex);
1229d4bfbc9aSGreg Clayton 
1230d4bfbc9aSGreg Clayton     if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
1231d4bfbc9aSGreg Clayton         return false;
1232d4bfbc9aSGreg Clayton 
12334da2e32fSJason Molenda     // read the plugin.dynamic-loader.darwin-kernel.load-kexts setting -- if the user requested no
12344da2e32fSJason Molenda     // kext loading, don't print any messages about kexts & don't try to read them.
12354da2e32fSJason Molenda     const bool load_kexts = GetGlobalProperties()->GetLoadKexts();
12364da2e32fSJason Molenda 
1237306bd0aaSJason Molenda     // By default, all kexts we've loaded in the past are marked as "remove" and all of the kexts
1238306bd0aaSJason Molenda     // we just found out about from ReadKextSummaries are marked as "add".
1239306bd0aaSJason Molenda     std::vector<bool> to_be_removed(m_known_kexts.size(), true);
1240306bd0aaSJason Molenda     std::vector<bool> to_be_added(count, true);
1241306bd0aaSJason Molenda 
1242306bd0aaSJason Molenda     int number_of_new_kexts_being_added = 0;
1243306bd0aaSJason Molenda     int number_of_old_kexts_being_removed = m_known_kexts.size();
1244306bd0aaSJason Molenda 
1245306bd0aaSJason Molenda     const uint32_t new_kexts_size = kext_summaries.size();
1246306bd0aaSJason Molenda     const uint32_t old_kexts_size = m_known_kexts.size();
1247306bd0aaSJason Molenda 
1248306bd0aaSJason Molenda     // The m_known_kexts vector may have entries that have been Cleared,
1249306bd0aaSJason Molenda     // or are a kernel.
1250306bd0aaSJason Molenda     for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1251306bd0aaSJason Molenda     {
1252306bd0aaSJason Molenda         bool ignore = false;
1253306bd0aaSJason Molenda         KextImageInfo &image_info = m_known_kexts[old_kext];
1254306bd0aaSJason Molenda         if (image_info.IsKernel())
1255306bd0aaSJason Molenda         {
1256306bd0aaSJason Molenda             ignore = true;
1257306bd0aaSJason Molenda         }
1258306bd0aaSJason Molenda         else if (image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS && !image_info.GetModule())
1259306bd0aaSJason Molenda         {
1260306bd0aaSJason Molenda             ignore = true;
1261306bd0aaSJason Molenda         }
1262306bd0aaSJason Molenda 
1263306bd0aaSJason Molenda         if (ignore)
1264306bd0aaSJason Molenda         {
1265306bd0aaSJason Molenda             number_of_old_kexts_being_removed--;
1266306bd0aaSJason Molenda             to_be_removed[old_kext] = false;
1267306bd0aaSJason Molenda         }
1268306bd0aaSJason Molenda     }
1269306bd0aaSJason Molenda 
1270306bd0aaSJason Molenda     // Scan over the list of kexts we just read from the kernel, note those that
1271306bd0aaSJason Molenda     // need to be added and those already loaded.
1272306bd0aaSJason Molenda     for (uint32_t new_kext = 0; new_kext < new_kexts_size; new_kext++)
1273306bd0aaSJason Molenda     {
1274306bd0aaSJason Molenda         bool add_this_one = true;
1275306bd0aaSJason Molenda         for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++)
1276306bd0aaSJason Molenda         {
1277306bd0aaSJason Molenda             if (m_known_kexts[old_kext] == kext_summaries[new_kext])
1278306bd0aaSJason Molenda             {
1279306bd0aaSJason Molenda                 // We already have this kext, don't re-load it.
1280306bd0aaSJason Molenda                 to_be_added[new_kext] = false;
1281306bd0aaSJason Molenda                 // This kext is still present, do not remove it.
1282306bd0aaSJason Molenda                 to_be_removed[old_kext] = false;
1283306bd0aaSJason Molenda 
1284306bd0aaSJason Molenda                 number_of_old_kexts_being_removed--;
1285306bd0aaSJason Molenda                 add_this_one = false;
1286306bd0aaSJason Molenda                 break;
1287306bd0aaSJason Molenda             }
1288306bd0aaSJason Molenda         }
1289306bd0aaSJason Molenda         if (add_this_one)
1290306bd0aaSJason Molenda         {
1291306bd0aaSJason Molenda             number_of_new_kexts_being_added++;
1292306bd0aaSJason Molenda         }
1293306bd0aaSJason Molenda     }
1294306bd0aaSJason Molenda 
1295306bd0aaSJason Molenda     if (number_of_new_kexts_being_added == 0 && number_of_old_kexts_being_removed == 0)
1296306bd0aaSJason Molenda         return true;
1297306bd0aaSJason Molenda 
129844d93782SGreg Clayton     Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get();
12994da2e32fSJason Molenda     if (s && load_kexts)
1300d4bfbc9aSGreg Clayton     {
1301306bd0aaSJason Molenda         if (number_of_new_kexts_being_added > 0 && number_of_old_kexts_being_removed > 0)
1302306bd0aaSJason Molenda         {
1303306bd0aaSJason Molenda             s->Printf ("Loading %d kext modules and unloading %d kext modules ", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
1304306bd0aaSJason Molenda         }
1305306bd0aaSJason Molenda         else if (number_of_new_kexts_being_added > 0)
1306306bd0aaSJason Molenda         {
1307306bd0aaSJason Molenda             s->Printf ("Loading %d kext modules ", number_of_new_kexts_being_added);
1308306bd0aaSJason Molenda         }
1309306bd0aaSJason Molenda         else if (number_of_old_kexts_being_removed > 0)
1310306bd0aaSJason Molenda         {
1311306bd0aaSJason Molenda             s->Printf ("Unloading %d kext modules ", number_of_old_kexts_being_removed);
1312306bd0aaSJason Molenda         }
1313306bd0aaSJason Molenda     }
1314306bd0aaSJason Molenda 
1315306bd0aaSJason Molenda     if (log)
13164da2e32fSJason Molenda     {
13174da2e32fSJason Molenda         if (load_kexts)
13184da2e32fSJason Molenda         {
1319306bd0aaSJason Molenda             log->Printf ("DynamicLoaderDarwinKernel::ParseKextSummaries: %d kexts added, %d kexts removed", number_of_new_kexts_being_added, number_of_old_kexts_being_removed);
13204da2e32fSJason Molenda         }
13214da2e32fSJason Molenda         else
13224da2e32fSJason Molenda         {
13234da2e32fSJason Molenda             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);
13244da2e32fSJason Molenda         }
13254da2e32fSJason Molenda     }
13264da2e32fSJason Molenda 
1327306bd0aaSJason Molenda 
1328306bd0aaSJason Molenda     if (number_of_new_kexts_being_added > 0)
1329306bd0aaSJason Molenda     {
1330306bd0aaSJason Molenda         ModuleList loaded_module_list;
1331306bd0aaSJason Molenda 
1332306bd0aaSJason Molenda         const uint32_t num_of_new_kexts = kext_summaries.size();
1333306bd0aaSJason Molenda         for (uint32_t new_kext = 0; new_kext < num_of_new_kexts; new_kext++)
1334306bd0aaSJason Molenda         {
1335306bd0aaSJason Molenda             if (to_be_added[new_kext] == true)
1336306bd0aaSJason Molenda             {
1337306bd0aaSJason Molenda                 KextImageInfo &image_info = kext_summaries[new_kext];
13384da2e32fSJason Molenda                 if (load_kexts)
13394da2e32fSJason Molenda                 {
1340306bd0aaSJason Molenda                     if (!image_info.LoadImageUsingMemoryModule (m_process))
13414da2e32fSJason Molenda                     {
1342306bd0aaSJason Molenda                         image_info.LoadImageAtFileAddress (m_process);
13434da2e32fSJason Molenda                     }
13444da2e32fSJason Molenda                 }
1345306bd0aaSJason Molenda 
1346306bd0aaSJason Molenda                 m_known_kexts.push_back(image_info);
1347306bd0aaSJason Molenda 
1348306bd0aaSJason Molenda                 if (image_info.GetModule() && m_process->GetStopID() == image_info.GetProcessStopId())
1349306bd0aaSJason Molenda                     loaded_module_list.AppendIfNeeded (image_info.GetModule());
1350d4bfbc9aSGreg Clayton 
13514da2e32fSJason Molenda                 if (s && load_kexts)
135268b3607fSJason Molenda                     s->Printf (".");
1353d4bfbc9aSGreg Clayton 
1354d4bfbc9aSGreg Clayton                 if (log)
13555160ce5cSGreg Clayton                     kext_summaries[new_kext].PutToLog (log);
1356d4bfbc9aSGreg Clayton             }
1357306bd0aaSJason Molenda         }
1358306bd0aaSJason Molenda         m_process->GetTarget().ModulesDidLoad (loaded_module_list);
1359306bd0aaSJason Molenda     }
1360306bd0aaSJason Molenda 
1361306bd0aaSJason Molenda     if (number_of_old_kexts_being_removed > 0)
1362306bd0aaSJason Molenda     {
1363306bd0aaSJason Molenda         ModuleList loaded_module_list;
1364306bd0aaSJason Molenda         const uint32_t num_of_old_kexts = m_known_kexts.size();
1365306bd0aaSJason Molenda         for (uint32_t old_kext = 0; old_kext < num_of_old_kexts; old_kext++)
1366306bd0aaSJason Molenda         {
1367306bd0aaSJason Molenda             ModuleList unloaded_module_list;
1368306bd0aaSJason Molenda             if (to_be_removed[old_kext])
1369306bd0aaSJason Molenda             {
1370306bd0aaSJason Molenda                 KextImageInfo &image_info = m_known_kexts[old_kext];
1371306bd0aaSJason Molenda                 // You can't unload the kernel.
1372306bd0aaSJason Molenda                 if (!image_info.IsKernel())
1373306bd0aaSJason Molenda                 {
1374306bd0aaSJason Molenda                     if (image_info.GetModule())
1375306bd0aaSJason Molenda                     {
1376306bd0aaSJason Molenda                         unloaded_module_list.AppendIfNeeded (image_info.GetModule());
1377306bd0aaSJason Molenda                     }
1378306bd0aaSJason Molenda                     if (s)
1379306bd0aaSJason Molenda                         s->Printf (".");
1380306bd0aaSJason Molenda                     image_info.Clear();
1381306bd0aaSJason Molenda                     // should pull it out of the KextImageInfos vector but that would mutate the list and invalidate
1382306bd0aaSJason Molenda                     // the to_be_removed bool vector; leaving it in place once Cleared() is relatively harmless.
1383306bd0aaSJason Molenda                 }
1384306bd0aaSJason Molenda             }
1385095eeaa0SGreg Clayton             m_process->GetTarget().ModulesDidUnload (unloaded_module_list, false);
1386306bd0aaSJason Molenda         }
1387306bd0aaSJason Molenda     }
1388306bd0aaSJason Molenda 
13894da2e32fSJason Molenda     if (s && load_kexts)
139068b3607fSJason Molenda     {
139168b3607fSJason Molenda         s->Printf (" done.\n");
139268b3607fSJason Molenda         s->Flush ();
139368b3607fSJason Molenda     }
139468b3607fSJason Molenda 
1395306bd0aaSJason Molenda     return true;
1396d4bfbc9aSGreg Clayton }
1397d4bfbc9aSGreg Clayton 
1398d4bfbc9aSGreg Clayton uint32_t
1399944b828aSGreg Clayton DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
1400d4bfbc9aSGreg Clayton                                               uint32_t image_infos_count,
1401306bd0aaSJason Molenda                                               KextImageInfo::collection &image_infos)
1402d4bfbc9aSGreg Clayton {
1403d4bfbc9aSGreg Clayton     const ByteOrder endian = m_kernel.GetByteOrder();
1404d4bfbc9aSGreg Clayton     const uint32_t addr_size = m_kernel.GetAddressByteSize();
1405d4bfbc9aSGreg Clayton 
1406d4bfbc9aSGreg Clayton     image_infos.resize(image_infos_count);
1407d4bfbc9aSGreg Clayton     const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
1408d4bfbc9aSGreg Clayton     DataBufferHeap data(count, 0);
1409d4bfbc9aSGreg Clayton     Error error;
1410d4bfbc9aSGreg Clayton 
1411d4bfbc9aSGreg Clayton     const bool prefer_file_cache = false;
1412d4bfbc9aSGreg Clayton     const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
1413d4bfbc9aSGreg Clayton                                                                  prefer_file_cache,
1414d4bfbc9aSGreg Clayton                                                                  data.GetBytes(),
1415d4bfbc9aSGreg Clayton                                                                  data.GetByteSize(),
1416d4bfbc9aSGreg Clayton                                                                  error);
1417d4bfbc9aSGreg Clayton     if (bytes_read == count)
1418d4bfbc9aSGreg Clayton     {
1419d4bfbc9aSGreg Clayton 
1420d4bfbc9aSGreg Clayton         DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
1421d4bfbc9aSGreg Clayton         uint32_t i=0;
1422d4bfbc9aSGreg Clayton         for (uint32_t kext_summary_offset = 0;
1423d4bfbc9aSGreg Clayton              i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
1424d4bfbc9aSGreg Clayton              ++i, kext_summary_offset += m_kext_summary_header.entry_size)
1425d4bfbc9aSGreg Clayton         {
1426c7bece56SGreg Clayton             lldb::offset_t offset = kext_summary_offset;
1427d4bfbc9aSGreg Clayton             const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
1428d4bfbc9aSGreg Clayton             if (name_data == NULL)
1429d4bfbc9aSGreg Clayton                 break;
1430306bd0aaSJason Molenda             image_infos[i].SetName ((const char *) name_data);
1431306bd0aaSJason Molenda             UUID uuid (extractor.GetData (&offset, 16), 16);
1432306bd0aaSJason Molenda             image_infos[i].SetUUID (uuid);
1433306bd0aaSJason Molenda             image_infos[i].SetLoadAddress (extractor.GetU64(&offset));
1434306bd0aaSJason Molenda             image_infos[i].SetSize (extractor.GetU64(&offset));
1435d4bfbc9aSGreg Clayton         }
1436d4bfbc9aSGreg Clayton         if (i < image_infos.size())
1437d4bfbc9aSGreg Clayton             image_infos.resize(i);
1438d4bfbc9aSGreg Clayton     }
1439d4bfbc9aSGreg Clayton     else
1440d4bfbc9aSGreg Clayton     {
1441d4bfbc9aSGreg Clayton         image_infos.clear();
1442d4bfbc9aSGreg Clayton     }
1443d4bfbc9aSGreg Clayton     return image_infos.size();
1444d4bfbc9aSGreg Clayton }
1445d4bfbc9aSGreg Clayton 
1446d4bfbc9aSGreg Clayton bool
1447944b828aSGreg Clayton DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
1448d4bfbc9aSGreg Clayton {
1449d4bfbc9aSGreg Clayton     Mutex::Locker locker(m_mutex);
1450d4bfbc9aSGreg Clayton 
1451d4bfbc9aSGreg Clayton     if (ReadKextSummaryHeader ())
1452d4bfbc9aSGreg Clayton     {
1453d4bfbc9aSGreg Clayton         if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
1454d4bfbc9aSGreg Clayton         {
1455d4bfbc9aSGreg Clayton             Address summary_addr (m_kext_summary_header_addr);
1456d4bfbc9aSGreg Clayton             summary_addr.Slide(m_kext_summary_header.GetSize());
1457d4bfbc9aSGreg Clayton             if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
1458d4bfbc9aSGreg Clayton             {
1459306bd0aaSJason Molenda                 m_known_kexts.clear();
1460d4bfbc9aSGreg Clayton             }
1461d4bfbc9aSGreg Clayton             return true;
1462d4bfbc9aSGreg Clayton         }
1463d4bfbc9aSGreg Clayton     }
1464d4bfbc9aSGreg Clayton     return false;
1465d4bfbc9aSGreg Clayton }
1466d4bfbc9aSGreg Clayton 
1467d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1468d4bfbc9aSGreg Clayton // Dump an image info structure to the file handle provided.
1469d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1470d4bfbc9aSGreg Clayton void
1471306bd0aaSJason Molenda DynamicLoaderDarwinKernel::KextImageInfo::PutToLog (Log *log) const
1472d4bfbc9aSGreg Clayton {
1473d4bfbc9aSGreg Clayton     if (log == NULL)
1474d4bfbc9aSGreg Clayton         return;
1475306bd0aaSJason Molenda     const uint8_t *u = (uint8_t *) m_uuid.GetBytes();
1476d4bfbc9aSGreg Clayton 
1477306bd0aaSJason Molenda     if (m_load_address == LLDB_INVALID_ADDRESS)
1478d4bfbc9aSGreg Clayton     {
1479d4bfbc9aSGreg Clayton         if (u)
1480d4bfbc9aSGreg Clayton         {
1481d4bfbc9aSGreg Clayton             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)",
1482d4bfbc9aSGreg Clayton                         u[ 0], u[ 1], u[ 2], u[ 3],
1483d4bfbc9aSGreg Clayton                         u[ 4], u[ 5], u[ 6], u[ 7],
1484d4bfbc9aSGreg Clayton                         u[ 8], u[ 9], u[10], u[11],
1485d4bfbc9aSGreg Clayton                         u[12], u[13], u[14], u[15],
1486306bd0aaSJason Molenda                         m_name.c_str());
1487d4bfbc9aSGreg Clayton         }
1488d4bfbc9aSGreg Clayton         else
1489306bd0aaSJason Molenda             log->Printf("\tname=\"%s\" (UNLOADED)", m_name.c_str());
1490d4bfbc9aSGreg Clayton     }
1491d4bfbc9aSGreg Clayton     else
1492d4bfbc9aSGreg Clayton     {
1493d4bfbc9aSGreg Clayton         if (u)
1494d4bfbc9aSGreg Clayton         {
1495306bd0aaSJason Molenda             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\"",
1496306bd0aaSJason Molenda                         m_load_address, m_size,
1497d4bfbc9aSGreg Clayton                         u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
1498d4bfbc9aSGreg Clayton                         u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
1499306bd0aaSJason Molenda                         m_name.c_str());
1500d4bfbc9aSGreg Clayton         }
1501d4bfbc9aSGreg Clayton         else
1502d4bfbc9aSGreg Clayton         {
1503306bd0aaSJason Molenda             log->Printf("\t[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") name=\"%s\"",
1504306bd0aaSJason Molenda                         m_load_address, m_load_address+m_size, m_name.c_str());
1505d4bfbc9aSGreg Clayton         }
1506d4bfbc9aSGreg Clayton     }
1507d4bfbc9aSGreg Clayton }
1508d4bfbc9aSGreg Clayton 
1509d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1510d4bfbc9aSGreg Clayton // Dump the _dyld_all_image_infos members and all current image infos
1511d4bfbc9aSGreg Clayton // that we have parsed to the file handle provided.
1512d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1513d4bfbc9aSGreg Clayton void
1514944b828aSGreg Clayton DynamicLoaderDarwinKernel::PutToLog(Log *log) const
1515d4bfbc9aSGreg Clayton {
1516d4bfbc9aSGreg Clayton     if (log == NULL)
1517d4bfbc9aSGreg Clayton         return;
1518d4bfbc9aSGreg Clayton 
1519d4bfbc9aSGreg Clayton     Mutex::Locker locker(m_mutex);
1520d01b2953SDaniel Malea     log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 " { version=%u, entry_size=%u, entry_count=%u }",
1521d4bfbc9aSGreg Clayton                 m_kext_summary_header_addr.GetFileAddress(),
1522d4bfbc9aSGreg Clayton                 m_kext_summary_header.version,
1523d4bfbc9aSGreg Clayton                 m_kext_summary_header.entry_size,
1524d4bfbc9aSGreg Clayton                 m_kext_summary_header.entry_count);
1525d4bfbc9aSGreg Clayton 
1526d4bfbc9aSGreg Clayton     size_t i;
1527306bd0aaSJason Molenda     const size_t count = m_known_kexts.size();
1528d4bfbc9aSGreg Clayton     if (count > 0)
1529d4bfbc9aSGreg Clayton     {
1530d4bfbc9aSGreg Clayton         log->PutCString("Loaded:");
1531d4bfbc9aSGreg Clayton         for (i = 0; i<count; i++)
1532306bd0aaSJason Molenda             m_known_kexts[i].PutToLog(log);
1533d4bfbc9aSGreg Clayton     }
1534d4bfbc9aSGreg Clayton }
1535d4bfbc9aSGreg Clayton 
1536d4bfbc9aSGreg Clayton void
1537944b828aSGreg Clayton DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
1538d4bfbc9aSGreg Clayton {
1539944b828aSGreg Clayton     DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
1540d4bfbc9aSGreg Clayton     Clear(true);
1541d4bfbc9aSGreg Clayton     m_process = process;
1542d4bfbc9aSGreg Clayton }
1543d4bfbc9aSGreg Clayton 
1544d4bfbc9aSGreg Clayton void
1545944b828aSGreg Clayton DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
1546d4bfbc9aSGreg Clayton {
1547306bd0aaSJason Molenda     if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.GetModule())
1548d4bfbc9aSGreg Clayton     {
1549944b828aSGreg Clayton         DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
1550d4bfbc9aSGreg Clayton 
1551d4bfbc9aSGreg Clayton 
1552a8558b62SJim Ingham         const bool internal_bp = true;
1553eb023e75SGreg Clayton         const bool hardware = false;
1554d4bfbc9aSGreg Clayton         const LazyBool skip_prologue = eLazyBoolNo;
1555969795f1SJim Ingham         FileSpecList module_spec_list;
1556306bd0aaSJason Molenda         module_spec_list.Append (m_kernel.GetModule()->GetFileSpec());
1557969795f1SJim Ingham         Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
155887df91b8SJim Ingham                                                                   NULL,
1559d4bfbc9aSGreg Clayton                                                                   "OSKextLoadedKextSummariesUpdated",
1560d4bfbc9aSGreg Clayton                                                                   eFunctionNameTypeFull,
156123b1decbSDawn Perchik                                                                   eLanguageTypeUnknown,
1562a8558b62SJim Ingham                                                                   skip_prologue,
1563eb023e75SGreg Clayton                                                                   internal_bp,
1564eb023e75SGreg Clayton                                                                   hardware).get();
1565d4bfbc9aSGreg Clayton 
1566944b828aSGreg Clayton         bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
1567d4bfbc9aSGreg Clayton         m_break_id = bp->GetID();
1568d4bfbc9aSGreg Clayton     }
1569d4bfbc9aSGreg Clayton }
1570d4bfbc9aSGreg Clayton 
1571d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1572d4bfbc9aSGreg Clayton // Member function that gets called when the process state changes.
1573d4bfbc9aSGreg Clayton //----------------------------------------------------------------------
1574d4bfbc9aSGreg Clayton void
1575944b828aSGreg Clayton DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
1576d4bfbc9aSGreg Clayton {
1577944b828aSGreg Clayton     DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
1578d4bfbc9aSGreg Clayton     switch (state)
1579d4bfbc9aSGreg Clayton     {
1580d4bfbc9aSGreg Clayton     case eStateConnected:
1581d4bfbc9aSGreg Clayton     case eStateAttaching:
1582d4bfbc9aSGreg Clayton     case eStateLaunching:
1583d4bfbc9aSGreg Clayton     case eStateInvalid:
1584d4bfbc9aSGreg Clayton     case eStateUnloaded:
1585d4bfbc9aSGreg Clayton     case eStateExited:
1586d4bfbc9aSGreg Clayton     case eStateDetached:
1587d4bfbc9aSGreg Clayton         Clear(false);
1588d4bfbc9aSGreg Clayton         break;
1589d4bfbc9aSGreg Clayton 
1590d4bfbc9aSGreg Clayton     case eStateStopped:
1591d4bfbc9aSGreg Clayton         UpdateIfNeeded();
1592d4bfbc9aSGreg Clayton         break;
1593d4bfbc9aSGreg Clayton 
1594d4bfbc9aSGreg Clayton     case eStateRunning:
1595d4bfbc9aSGreg Clayton     case eStateStepping:
1596d4bfbc9aSGreg Clayton     case eStateCrashed:
1597d4bfbc9aSGreg Clayton     case eStateSuspended:
1598d4bfbc9aSGreg Clayton         break;
1599d4bfbc9aSGreg Clayton     }
1600d4bfbc9aSGreg Clayton }
1601d4bfbc9aSGreg Clayton 
1602d4bfbc9aSGreg Clayton ThreadPlanSP
1603944b828aSGreg Clayton DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
1604d4bfbc9aSGreg Clayton {
1605d4bfbc9aSGreg Clayton     ThreadPlanSP thread_plan_sp;
16065160ce5cSGreg Clayton     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1607d4bfbc9aSGreg Clayton     if (log)
1608d4bfbc9aSGreg Clayton         log->Printf ("Could not find symbol for step through.");
1609d4bfbc9aSGreg Clayton     return thread_plan_sp;
1610d4bfbc9aSGreg Clayton }
1611d4bfbc9aSGreg Clayton 
1612d4bfbc9aSGreg Clayton Error
1613944b828aSGreg Clayton DynamicLoaderDarwinKernel::CanLoadImage ()
1614d4bfbc9aSGreg Clayton {
1615d4bfbc9aSGreg Clayton     Error error;
1616d4bfbc9aSGreg Clayton     error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
1617d4bfbc9aSGreg Clayton     return error;
1618d4bfbc9aSGreg Clayton }
1619d4bfbc9aSGreg Clayton 
1620d4bfbc9aSGreg Clayton void
1621944b828aSGreg Clayton DynamicLoaderDarwinKernel::Initialize()
1622d4bfbc9aSGreg Clayton {
1623d4bfbc9aSGreg Clayton     PluginManager::RegisterPlugin (GetPluginNameStatic(),
1624d4bfbc9aSGreg Clayton                                    GetPluginDescriptionStatic(),
1625e8cd0c98SGreg Clayton                                    CreateInstance,
1626e8cd0c98SGreg Clayton                                    DebuggerInitialize);
1627d4bfbc9aSGreg Clayton }
1628d4bfbc9aSGreg Clayton 
1629d4bfbc9aSGreg Clayton void
1630944b828aSGreg Clayton DynamicLoaderDarwinKernel::Terminate()
1631d4bfbc9aSGreg Clayton {
1632d4bfbc9aSGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
1633d4bfbc9aSGreg Clayton }
1634d4bfbc9aSGreg Clayton 
1635e8cd0c98SGreg Clayton void
1636e8cd0c98SGreg Clayton DynamicLoaderDarwinKernel::DebuggerInitialize (lldb_private::Debugger &debugger)
1637e8cd0c98SGreg Clayton {
1638e8cd0c98SGreg Clayton     if (!PluginManager::GetSettingForDynamicLoaderPlugin (debugger, DynamicLoaderDarwinKernelProperties::GetSettingName()))
1639e8cd0c98SGreg Clayton     {
1640e8cd0c98SGreg Clayton         const bool is_global_setting = true;
1641e8cd0c98SGreg Clayton         PluginManager::CreateSettingForDynamicLoaderPlugin (debugger,
1642e8cd0c98SGreg Clayton                                                             GetGlobalProperties()->GetValueProperties(),
1643e8cd0c98SGreg Clayton                                                             ConstString ("Properties for the DynamicLoaderDarwinKernel plug-in."),
1644e8cd0c98SGreg Clayton                                                             is_global_setting);
1645e8cd0c98SGreg Clayton     }
1646e8cd0c98SGreg Clayton }
1647d4bfbc9aSGreg Clayton 
164857abc5d6SGreg Clayton lldb_private::ConstString
1649944b828aSGreg Clayton DynamicLoaderDarwinKernel::GetPluginNameStatic()
1650d4bfbc9aSGreg Clayton {
165157abc5d6SGreg Clayton     static ConstString g_name("darwin-kernel");
165257abc5d6SGreg Clayton     return g_name;
1653d4bfbc9aSGreg Clayton }
1654d4bfbc9aSGreg Clayton 
1655d4bfbc9aSGreg Clayton const char *
1656944b828aSGreg Clayton DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
1657d4bfbc9aSGreg Clayton {
1658d4bfbc9aSGreg Clayton     return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
1659d4bfbc9aSGreg Clayton }
1660d4bfbc9aSGreg Clayton 
1661d4bfbc9aSGreg Clayton 
1662d4bfbc9aSGreg Clayton //------------------------------------------------------------------
1663d4bfbc9aSGreg Clayton // PluginInterface protocol
1664d4bfbc9aSGreg Clayton //------------------------------------------------------------------
166557abc5d6SGreg Clayton lldb_private::ConstString
1666944b828aSGreg Clayton DynamicLoaderDarwinKernel::GetPluginName()
1667d4bfbc9aSGreg Clayton {
1668d4bfbc9aSGreg Clayton     return GetPluginNameStatic();
1669d4bfbc9aSGreg Clayton }
1670d4bfbc9aSGreg Clayton 
1671d4bfbc9aSGreg Clayton uint32_t
1672944b828aSGreg Clayton DynamicLoaderDarwinKernel::GetPluginVersion()
1673d4bfbc9aSGreg Clayton {
1674d4bfbc9aSGreg Clayton     return 1;
1675d4bfbc9aSGreg Clayton }
1676d4bfbc9aSGreg Clayton 
16771f746071SGreg Clayton lldb::ByteOrder
16781f746071SGreg Clayton DynamicLoaderDarwinKernel::GetByteOrderFromMagic (uint32_t magic)
16791f746071SGreg Clayton {
16801f746071SGreg Clayton     switch (magic)
16811f746071SGreg Clayton     {
1682510938e5SCharles Davis         case llvm::MachO::MH_MAGIC:
1683510938e5SCharles Davis         case llvm::MachO::MH_MAGIC_64:
1684*9ccb970fSBruce Mitchener             return endian::InlHostByteOrder();
16851f746071SGreg Clayton 
1686510938e5SCharles Davis         case llvm::MachO::MH_CIGAM:
1687510938e5SCharles Davis         case llvm::MachO::MH_CIGAM_64:
1688*9ccb970fSBruce Mitchener             if (endian::InlHostByteOrder() == lldb::eByteOrderBig)
16891f746071SGreg Clayton                 return lldb::eByteOrderLittle;
16901f746071SGreg Clayton             else
16911f746071SGreg Clayton                 return lldb::eByteOrderBig;
16921f746071SGreg Clayton 
16931f746071SGreg Clayton         default:
16941f746071SGreg Clayton             break;
16951f746071SGreg Clayton     }
16961f746071SGreg Clayton     return lldb::eByteOrderInvalid;
16971f746071SGreg Clayton }
16981f746071SGreg Clayton 
1699