LLDB mainline
DynamicLoaderWindowsDYLD.cpp
Go to the documentation of this file.
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
10
12#include "lldb/Core/Module.h"
16#include "lldb/Target/Process.h"
18#include "lldb/Target/Target.h"
21#include "lldb/Utility/Log.h"
22
23#include "llvm/TargetParser/Triple.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
29
32
34
39
43
45 return "Dynamic loader plug-in that watches for shared library "
46 "loads/unloads in Windows processes.";
47}
48
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
66 const ModuleSpec module_spec,
67 lldb::addr_t module_addr) {
68 // Resolve the module unless we already have one.
69 if (!module_sp) {
71 module_sp = m_process->GetTarget().GetOrCreateModule(module_spec,
72 true /* notify */, &error);
73 if (error.Fail())
74 return;
75 }
76
77 m_loaded_modules.insert({module_addr, lldb::ModuleWP(module_sp)});
78 UpdateLoadedSectionsCommon(module_sp, module_addr, false);
79 ModuleList module_list;
80 module_list.Append(module_sp);
81 m_process->GetTarget().ModulesDidLoad(module_list);
82}
83
85 auto it = m_loaded_modules.find(module_addr);
86 if (it == m_loaded_modules.end())
87 return;
88
89 ModuleSP module_sp = it->second.lock();
90 m_loaded_modules.erase(it);
91
92 if (!module_sp)
93 return;
94
95 bool full_unload = true;
96 for (const auto &entry : m_loaded_modules) {
97 ModuleSP other_sp = entry.second.lock();
98 if (other_sp == module_sp) {
99 UpdateLoadedSectionsCommon(module_sp, entry.first, false);
100 full_unload = false;
101 }
102 }
103
104 if (full_unload) {
105 UnloadSectionsCommon(module_sp);
106 ModuleList module_list;
107 module_list.Append(module_sp);
108 m_process->GetTarget().ModulesDidUnload(module_list, false);
109 }
110}
111
113 // First, see if the load address is already cached.
114 for (const auto &entry : m_loaded_modules) {
115 ModuleSP mod = entry.second.lock();
116 if (mod == executable && entry.first != LLDB_INVALID_ADDRESS)
117 return entry.first;
118 }
119
121
122 // Second, try to get it through the process plugins. For a remote process,
123 // the remote platform will be responsible for providing it.
124 FileSpec file_spec(executable->GetPlatformFileSpec());
125 bool is_loaded = false;
126 Status status =
127 m_process->GetFileLoadAddress(file_spec, is_loaded, load_addr);
128 // Servers other than lldb server could respond with a bogus address.
129 if (status.Success() && is_loaded && load_addr != LLDB_INVALID_ADDRESS) {
130 m_loaded_modules.insert({load_addr, lldb::ModuleWP(executable)});
131 return load_addr;
132 }
133
135}
136
139 LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
140
142
143 ModuleSP executable = GetTargetExecutable();
144
145 if (!executable)
146 return;
147
148 // Try to fetch the load address of the file from the process, since there
149 // could be randomization of the load address.
150 lldb::addr_t load_addr = GetLoadAddress(executable);
151 if (load_addr == LLDB_INVALID_ADDRESS)
152 return;
153
154 // Request the process base address.
155 lldb::addr_t image_base = m_process->GetImageInfoAddress();
156 if (image_base == load_addr)
157 return;
158
159 // Rebase the process's modules if there is a mismatch.
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
171 LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
172
174
175 ModuleSP executable = GetTargetExecutable();
176 if (!executable)
177 return;
178
179 lldb::addr_t load_addr = GetLoadAddress(executable);
180 if (load_addr != LLDB_INVALID_ADDRESS) {
181 // Update the loaded sections so that the breakpoints can be resolved.
182 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false);
183
184 ModuleList module_list;
185 module_list.Append(executable);
186 m_process->GetTarget().ModulesDidLoad(module_list);
187 auto error = m_process->LoadModules();
188 LLDB_LOG_ERROR(log, std::move(error), "failed to load modules: {0}");
189 }
190}
191
193
196 bool stop) {
197 auto arch = m_process->GetTarget().GetArchitecture();
198 if (arch.GetMachine() != llvm::Triple::x86) {
199 return ThreadPlanSP();
200 }
201
202 RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
203 if (!reg_ctx_sp)
204 return ThreadPlanSP();
205
206 uint64_t pc = reg_ctx_sp->GetPC();
207 // Max size of an instruction in x86 is 15 bytes.
208 AddressRange range(pc, 2 * 15);
209
211 arch, nullptr, nullptr, nullptr, nullptr, m_process->GetTarget(), range);
212 if (!disassembler_sp) {
213 return ThreadPlanSP();
214 }
215
216 InstructionList &insn_list = disassembler_sp->GetInstructionList();
217
218 // First instruction in a x86 Windows trampoline is going to be an indirect
219 // jump through the IAT and the next one will be a nop (usually there for
220 // alignment purposes). e.g.:
221 // 0x70ff4cfc <+956>: jmpl *0x7100c2a8
222 // 0x70ff4d02 <+962>: nop
223
224 auto first_insn = insn_list.GetInstructionAtIndex(0);
225 auto second_insn = insn_list.GetInstructionAtIndex(1);
226
227 ExecutionContext exe_ctx(m_process->GetTarget());
228 if (first_insn == nullptr || second_insn == nullptr)
229 return ThreadPlanSP();
230
231 const char *first_mnemonic = first_insn->GetMnemonic(&exe_ctx);
232 const char *second_mnemonic = second_insn->GetMnemonic(&exe_ctx);
233 if (!first_mnemonic || !second_mnemonic ||
234 strcmp(first_mnemonic, "jmpl") != 0 ||
235 strcmp(second_mnemonic, "nop") != 0)
236 return ThreadPlanSP();
237
238 assert(first_insn->DoesBranch() && !second_insn->DoesBranch());
239
241 thread, false, false, eVoteNoOpinion, eVoteNoOpinion));
242}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:378
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:394
#define LLDB_PLUGIN_DEFINE(PluginName)
A section + offset based address range class.
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:460
static lldb::DisassemblerSP DisassembleRange(const ArchSpec &arch, const char *plugin_name, const char *flavor, const char *cpu, const char *features, Target &target, llvm::ArrayRef< AddressRange > disasm_ranges, bool force_live_memory=false)
void DidAttach() override
Called after attaching a process.
void DidLaunch() override
Called after launching a process.
llvm::DenseMap< lldb::addr_t, lldb::ModuleWP > m_loaded_modules
Maps load addresses to their corresponding modules.
Status CanLoadImage() override
Ask if it is ok to try and load or unload an shared library (image).
void OnLoadModule(lldb::ModuleSP module_sp, const ModuleSpec module_spec, lldb::addr_t module_addr)
lldb::addr_t GetLoadAddress(lldb::ModuleSP executable)
Returns the load address for the given executable module.
static DynamicLoader * CreateInstance(Process *process, bool force)
lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread, bool stop) override
Provides a plan to step through the dynamic loader trampoline for the current state of thread.
Process * m_process
The process that this dynamic loader plug-in is tracking.
void UpdateLoadedSectionsCommon(lldb::ModuleSP module, lldb::addr_t base_addr, bool base_addr_is_offset)
lldb::ModuleSP GetTargetExecutable()
Checks to see if the target module has changed, updates the target accordingly and returns the target...
virtual void UpdateLoadedSections(lldb::ModuleSP module, lldb::addr_t link_map_addr, lldb::addr_t base_addr, bool base_addr_is_offset)
Updates the load address of every allocatable section in module.
DynamicLoader(Process *process)
Construct with a process.
void UnloadSectionsCommon(const lldb::ModuleSP module)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:57
lldb::InstructionSP GetInstructionAtIndex(size_t idx) const
A collection class for Module objects.
Definition ModuleList.h:125
void Append(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list.
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A plug-in interface definition class for debugging a process.
Definition Process.h:357
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1255
An error handling class.
Definition Status.h:118
bool Success() const
Test for success condition.
Definition Status.cpp:303
const ArchSpec & GetArchitecture() const
Definition Target.h:1283
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
void RegisterMSVCRTCFrameRecognizer(Target &target)
Registers the MSVC run-time check failure frame recognizer with the target.
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
std::weak_ptr< lldb_private::Module > ModuleWP
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
std::shared_ptr< lldb_private::Module > ModuleSP