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