1 //===-- DynamicLoaderWindowsDYLD.cpp --------------------------------*- C++ 2 //-*-===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "DynamicLoaderWindowsDYLD.h" 11 12 #include "lldb/Core/Module.h" 13 #include "lldb/Core/PluginManager.h" 14 #include "lldb/Target/ExecutionContext.h" 15 #include "lldb/Target/Platform.h" 16 #include "lldb/Target/Process.h" 17 #include "lldb/Target/RegisterContext.h" 18 #include "lldb/Target/Target.h" 19 #include "lldb/Target/ThreadPlanStepInstruction.h" 20 #include "lldb/Utility/Log.h" 21 22 #include "llvm/ADT/Triple.h" 23 24 using namespace lldb; 25 using namespace lldb_private; 26 27 DynamicLoaderWindowsDYLD::DynamicLoaderWindowsDYLD(Process *process) 28 : DynamicLoader(process) {} 29 30 DynamicLoaderWindowsDYLD::~DynamicLoaderWindowsDYLD() {} 31 32 void DynamicLoaderWindowsDYLD::Initialize() { 33 PluginManager::RegisterPlugin(GetPluginNameStatic(), 34 GetPluginDescriptionStatic(), CreateInstance); 35 } 36 37 void DynamicLoaderWindowsDYLD::Terminate() {} 38 39 ConstString DynamicLoaderWindowsDYLD::GetPluginNameStatic() { 40 static ConstString g_plugin_name("windows-dyld"); 41 return g_plugin_name; 42 } 43 44 const char *DynamicLoaderWindowsDYLD::GetPluginDescriptionStatic() { 45 return "Dynamic loader plug-in that watches for shared library " 46 "loads/unloads in Windows processes."; 47 } 48 49 DynamicLoader *DynamicLoaderWindowsDYLD::CreateInstance(Process *process, 50 bool force) { 51 bool should_create = force; 52 if (!should_create) { 53 const llvm::Triple &triple_ref = 54 process->GetTarget().GetArchitecture().GetTriple(); 55 if (triple_ref.getOS() == llvm::Triple::Win32) 56 should_create = true; 57 } 58 59 if (should_create) 60 return new DynamicLoaderWindowsDYLD(process); 61 62 return nullptr; 63 } 64 65 void DynamicLoaderWindowsDYLD::OnLoadModule(lldb::ModuleSP module_sp, 66 const ModuleSpec module_spec, 67 lldb::addr_t module_addr) { 68 69 // Resolve the module unless we already have one. 70 if (!module_sp) { 71 // Confusingly, there is no Target::AddSharedModule. Instead, calling 72 // GetSharedModule() with a new module will add it to the module list and 73 // return a corresponding ModuleSP. 74 Status error; 75 module_sp = m_process->GetTarget().GetSharedModule(module_spec, &error); 76 if (error.Fail()) 77 return; 78 } 79 80 m_loaded_modules[module_sp] = module_addr; 81 UpdateLoadedSectionsCommon(module_sp, module_addr, false); 82 ModuleList module_list; 83 module_list.Append(module_sp); 84 m_process->GetTarget().ModulesDidLoad(module_list); 85 } 86 87 void DynamicLoaderWindowsDYLD::OnUnloadModule(lldb::addr_t module_addr) { 88 Address resolved_addr; 89 if (!m_process->GetTarget().ResolveLoadAddress(module_addr, resolved_addr)) 90 return; 91 92 ModuleSP module_sp = resolved_addr.GetModule(); 93 if (module_sp) { 94 m_loaded_modules.erase(module_sp); 95 UnloadSectionsCommon(module_sp); 96 ModuleList module_list; 97 module_list.Append(module_sp); 98 m_process->GetTarget().ModulesDidUnload(module_list, false); 99 } 100 } 101 102 lldb::addr_t DynamicLoaderWindowsDYLD::GetLoadAddress(ModuleSP executable) { 103 // First, see if the load address is already cached. 104 auto it = m_loaded_modules.find(executable); 105 if (it != m_loaded_modules.end() && it->second != LLDB_INVALID_ADDRESS) 106 return it->second; 107 108 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS; 109 110 // Second, try to get it through the process plugins. For a remote process, 111 // the remote platform will be responsible for providing it. 112 FileSpec file_spec(executable->GetPlatformFileSpec()); 113 bool is_loaded = false; 114 Status status = 115 m_process->GetFileLoadAddress(file_spec, is_loaded, load_addr); 116 // Servers other than lldb server could respond with a bogus address. 117 if (status.Success() && is_loaded && load_addr != LLDB_INVALID_ADDRESS) { 118 m_loaded_modules[executable] = load_addr; 119 return load_addr; 120 } 121 122 return LLDB_INVALID_ADDRESS; 123 } 124 125 void DynamicLoaderWindowsDYLD::DidAttach() { 126 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 127 if (log) 128 log->Printf("DynamicLoaderWindowsDYLD::%s()", __FUNCTION__); 129 130 ModuleSP executable = GetTargetExecutable(); 131 132 if (!executable.get()) 133 return; 134 135 // Try to fetch the load address of the file from the process, since there 136 // could be randomization of the load address. 137 lldb::addr_t load_addr = GetLoadAddress(executable); 138 if (load_addr == LLDB_INVALID_ADDRESS) 139 return; 140 141 // Request the process base address. 142 lldb::addr_t image_base = m_process->GetImageInfoAddress(); 143 if (image_base == load_addr) 144 return; 145 146 // Rebase the process's modules if there is a mismatch. 147 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false); 148 149 ModuleList module_list; 150 module_list.Append(executable); 151 m_process->GetTarget().ModulesDidLoad(module_list); 152 m_process->LoadModules(); 153 } 154 155 void DynamicLoaderWindowsDYLD::DidLaunch() { 156 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); 157 if (log) 158 log->Printf("DynamicLoaderWindowsDYLD::%s()", __FUNCTION__); 159 160 ModuleSP executable = GetTargetExecutable(); 161 if (!executable.get()) 162 return; 163 164 lldb::addr_t load_addr = GetLoadAddress(executable); 165 if (load_addr != LLDB_INVALID_ADDRESS) { 166 // Update the loaded sections so that the breakpoints can be resolved. 167 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false); 168 169 ModuleList module_list; 170 module_list.Append(executable); 171 m_process->GetTarget().ModulesDidLoad(module_list); 172 m_process->LoadModules(); 173 } 174 } 175 176 Status DynamicLoaderWindowsDYLD::CanLoadImage() { return Status(); } 177 178 ConstString DynamicLoaderWindowsDYLD::GetPluginName() { 179 return GetPluginNameStatic(); 180 } 181 182 uint32_t DynamicLoaderWindowsDYLD::GetPluginVersion() { return 1; } 183 184 ThreadPlanSP 185 DynamicLoaderWindowsDYLD::GetStepThroughTrampolinePlan(Thread &thread, 186 bool stop) { 187 auto arch = m_process->GetTarget().GetArchitecture(); 188 if (arch.GetMachine() != llvm::Triple::x86) { 189 return ThreadPlanSP(); 190 } 191 192 uint64_t pc = thread.GetRegisterContext()->GetPC(); 193 // Max size of an instruction in x86 is 15 bytes. 194 AddressRange range(pc, 2 * 15); 195 196 ExecutionContext exe_ctx(m_process->GetTarget()); 197 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange( 198 arch, nullptr, nullptr, exe_ctx, range, true); 199 if (!disassembler_sp) { 200 return ThreadPlanSP(); 201 } 202 203 InstructionList *insn_list = &disassembler_sp->GetInstructionList(); 204 if (insn_list == nullptr) { 205 return ThreadPlanSP(); 206 } 207 208 // First instruction in a x86 Windows trampoline is going to be an indirect 209 // jump through the IAT and the next one will be a nop (usually there for 210 // alignment purposes). e.g.: 211 // 0x70ff4cfc <+956>: jmpl *0x7100c2a8 212 // 0x70ff4d02 <+962>: nop 213 214 auto first_insn = insn_list->GetInstructionAtIndex(0); 215 auto second_insn = insn_list->GetInstructionAtIndex(1); 216 217 if (first_insn == nullptr || second_insn == nullptr || 218 strcmp(first_insn->GetMnemonic(&exe_ctx), "jmpl") != 0 || 219 strcmp(second_insn->GetMnemonic(&exe_ctx), "nop") != 0) { 220 return ThreadPlanSP(); 221 } 222 223 assert(first_insn->DoesBranch() && !second_insn->DoesBranch()); 224 225 return ThreadPlanSP(new ThreadPlanStepInstruction( 226 thread, false, false, eVoteNoOpinion, eVoteNoOpinion)); 227 } 228