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