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
11#include "lldb/Core/Module.h"
15#include "lldb/Target/Process.h"
17#include "lldb/Target/Target.h"
20#include "lldb/Utility/Log.h"
21
22#include "llvm/TargetParser/Triple.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
28
31
33
38
42
44 return "Dynamic loader plug-in that watches for shared library "
45 "loads/unloads in Windows processes.";
46}
47
49 bool force) {
50 bool should_create = force;
51 if (!should_create) {
52 const llvm::Triple &triple_ref =
53 process->GetTarget().GetArchitecture().GetTriple();
54 if (triple_ref.getOS() == llvm::Triple::Win32)
55 should_create = true;
56 }
57
58 if (should_create)
59 return new DynamicLoaderWindowsDYLD(process);
60
61 return nullptr;
62}
63
65 const ModuleSpec module_spec,
66 lldb::addr_t module_addr) {
67 // Resolve the module unless we already have one.
68 if (!module_sp) {
70 module_sp = m_process->GetTarget().GetOrCreateModule(module_spec,
71 true /* notify */, &error);
72 if (error.Fail())
73 return;
74 }
75
76 m_loaded_modules.insert({module_addr, lldb::ModuleWP(module_sp)});
77 UpdateLoadedSectionsCommon(module_sp, module_addr, false);
78 ModuleList module_list;
79 module_list.Append(module_sp);
80 m_process->GetTarget().ModulesDidLoad(module_list);
81}
82
84 auto it = m_loaded_modules.find(module_addr);
85 if (it == m_loaded_modules.end())
86 return;
87
88 ModuleSP module_sp = it->second.lock();
89 m_loaded_modules.erase(it);
90
91 if (!module_sp)
92 return;
93
94 bool full_unload = true;
95 for (const auto &entry : m_loaded_modules) {
96 ModuleSP other_sp = entry.second.lock();
97 if (other_sp == module_sp) {
98 UpdateLoadedSectionsCommon(module_sp, entry.first, false);
99 full_unload = false;
100 }
101 }
102
103 if (full_unload) {
104 UnloadSectionsCommon(module_sp);
105 ModuleList module_list;
106 module_list.Append(module_sp);
107 m_process->GetTarget().ModulesDidUnload(module_list, false);
108 }
109}
110
112 // First, see if the load address is already cached.
113 for (const auto &entry : m_loaded_modules) {
114 ModuleSP mod = entry.second.lock();
115 if (mod == executable && entry.first != LLDB_INVALID_ADDRESS)
116 return entry.first;
117 }
118
120
121 // Second, try to get it through the process plugins. For a remote process,
122 // the remote platform will be responsible for providing it.
123 FileSpec file_spec(executable->GetPlatformFileSpec());
124 bool is_loaded = false;
125 Status status =
126 m_process->GetFileLoadAddress(file_spec, is_loaded, load_addr);
127 // Servers other than lldb server could respond with a bogus address.
128 if (status.Success() && is_loaded && load_addr != LLDB_INVALID_ADDRESS) {
129 m_loaded_modules.insert({load_addr, lldb::ModuleWP(executable)});
130 return load_addr;
131 }
132
134}
135
138 LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
139
140 ModuleSP executable = GetTargetExecutable();
141
142 if (!executable.get())
143 return;
144
145 // Try to fetch the load address of the file from the process, since there
146 // could be randomization of the load address.
147 lldb::addr_t load_addr = GetLoadAddress(executable);
148 if (load_addr == LLDB_INVALID_ADDRESS)
149 return;
150
151 // Request the process base address.
152 lldb::addr_t image_base = m_process->GetImageInfoAddress();
153 if (image_base == load_addr)
154 return;
155
156 // Rebase the process's modules if there is a mismatch.
157 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false);
158
159 ModuleList module_list;
160 module_list.Append(executable);
161 m_process->GetTarget().ModulesDidLoad(module_list);
162 auto error = m_process->LoadModules();
163 LLDB_LOG_ERROR(log, std::move(error), "failed to load modules: {0}");
164}
165
168 LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
169
170 ModuleSP executable = GetTargetExecutable();
171 if (!executable.get())
172 return;
173
174 lldb::addr_t load_addr = GetLoadAddress(executable);
175 if (load_addr != LLDB_INVALID_ADDRESS) {
176 // Update the loaded sections so that the breakpoints can be resolved.
177 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false);
178
179 ModuleList module_list;
180 module_list.Append(executable);
181 m_process->GetTarget().ModulesDidLoad(module_list);
182 auto error = m_process->LoadModules();
183 LLDB_LOG_ERROR(log, std::move(error), "failed to load modules: {0}");
184 }
185}
186
188
191 bool stop) {
192 auto arch = m_process->GetTarget().GetArchitecture();
193 if (arch.GetMachine() != llvm::Triple::x86) {
194 return ThreadPlanSP();
195 }
196
197 uint64_t pc = thread.GetRegisterContext()->GetPC();
198 // Max size of an instruction in x86 is 15 bytes.
199 AddressRange range(pc, 2 * 15);
200
202 arch, nullptr, nullptr, nullptr, nullptr, m_process->GetTarget(), range);
203 if (!disassembler_sp) {
204 return ThreadPlanSP();
205 }
206
207 InstructionList *insn_list = &disassembler_sp->GetInstructionList();
208 if (insn_list == nullptr) {
209 return ThreadPlanSP();
210 }
211
212 // First instruction in a x86 Windows trampoline is going to be an indirect
213 // jump through the IAT and the next one will be a nop (usually there for
214 // alignment purposes). e.g.:
215 // 0x70ff4cfc <+956>: jmpl *0x7100c2a8
216 // 0x70ff4d02 <+962>: nop
217
218 auto first_insn = insn_list->GetInstructionAtIndex(0);
219 auto second_insn = insn_list->GetInstructionAtIndex(1);
220
221 ExecutionContext exe_ctx(m_process->GetTarget());
222 if (first_insn == nullptr || second_insn == nullptr ||
223 strcmp(first_insn->GetMnemonic(&exe_ctx), "jmpl") != 0 ||
224 strcmp(second_insn->GetMnemonic(&exe_ctx), "nop") != 0) {
225 return ThreadPlanSP();
226 }
227
228 assert(first_insn->DoesBranch() && !second_insn->DoesBranch());
229
231 thread, false, false, eVoteNoOpinion, eVoteNoOpinion));
232}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:383
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:399
#define LLDB_PLUGIN_DEFINE(PluginName)
A section + offset based address range class.
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:457
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:354
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1250
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:1182
#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:332
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::Module > ModuleSP