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
30 : DynamicLoader(process) {}
31
33
37}
38
40
42 return "Dynamic loader plug-in that watches for shared library "
43 "loads/unloads in Windows processes.";
44}
45
47 bool force) {
48 bool should_create = force;
49 if (!should_create) {
50 const llvm::Triple &triple_ref =
51 process->GetTarget().GetArchitecture().GetTriple();
52 if (triple_ref.getOS() == llvm::Triple::Win32)
53 should_create = true;
54 }
55
56 if (should_create)
57 return new DynamicLoaderWindowsDYLD(process);
58
59 return nullptr;
60}
61
63 const ModuleSpec module_spec,
64 lldb::addr_t module_addr) {
65
66 // Resolve the module unless we already have one.
67 if (!module_sp) {
69 module_sp = m_process->GetTarget().GetOrCreateModule(module_spec,
70 true /* notify */, &error);
71 if (error.Fail())
72 return;
73 }
74
75 m_loaded_modules[module_sp] = module_addr;
76 UpdateLoadedSectionsCommon(module_sp, module_addr, false);
77 ModuleList module_list;
78 module_list.Append(module_sp);
79 m_process->GetTarget().ModulesDidLoad(module_list);
80}
81
83 Address resolved_addr;
84 if (!m_process->GetTarget().ResolveLoadAddress(module_addr, resolved_addr))
85 return;
86
87 ModuleSP module_sp = resolved_addr.GetModule();
88 if (module_sp) {
89 m_loaded_modules.erase(module_sp);
90 UnloadSectionsCommon(module_sp);
91 ModuleList module_list;
92 module_list.Append(module_sp);
93 m_process->GetTarget().ModulesDidUnload(module_list, false);
94 }
95}
96
98 // First, see if the load address is already cached.
99 auto it = m_loaded_modules.find(executable);
100 if (it != m_loaded_modules.end() && it->second != LLDB_INVALID_ADDRESS)
101 return it->second;
102
104
105 // Second, try to get it through the process plugins. For a remote process,
106 // the remote platform will be responsible for providing it.
107 FileSpec file_spec(executable->GetPlatformFileSpec());
108 bool is_loaded = false;
109 Status status =
110 m_process->GetFileLoadAddress(file_spec, is_loaded, load_addr);
111 // Servers other than lldb server could respond with a bogus address.
112 if (status.Success() && is_loaded && load_addr != LLDB_INVALID_ADDRESS) {
113 m_loaded_modules[executable] = load_addr;
114 return load_addr;
115 }
116
118}
119
122 LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
123
124 ModuleSP executable = GetTargetExecutable();
125
126 if (!executable.get())
127 return;
128
129 // Try to fetch the load address of the file from the process, since there
130 // could be randomization of the load address.
131 lldb::addr_t load_addr = GetLoadAddress(executable);
132 if (load_addr == LLDB_INVALID_ADDRESS)
133 return;
134
135 // Request the process base address.
137 if (image_base == load_addr)
138 return;
139
140 // Rebase the process's modules if there is a mismatch.
141 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false);
142
143 ModuleList module_list;
144 module_list.Append(executable);
145 m_process->GetTarget().ModulesDidLoad(module_list);
146 auto error = m_process->LoadModules();
147 LLDB_LOG_ERROR(log, std::move(error), "failed to load modules: {0}");
148}
149
152 LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
153
154 ModuleSP executable = GetTargetExecutable();
155 if (!executable.get())
156 return;
157
158 lldb::addr_t load_addr = GetLoadAddress(executable);
159 if (load_addr != LLDB_INVALID_ADDRESS) {
160 // Update the loaded sections so that the breakpoints can be resolved.
161 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false);
162
163 ModuleList module_list;
164 module_list.Append(executable);
165 m_process->GetTarget().ModulesDidLoad(module_list);
166 auto error = m_process->LoadModules();
167 LLDB_LOG_ERROR(log, std::move(error), "failed to load modules: {0}");
168 }
169}
170
172
175 bool stop) {
176 auto arch = m_process->GetTarget().GetArchitecture();
177 if (arch.GetMachine() != llvm::Triple::x86) {
178 return ThreadPlanSP();
179 }
180
181 uint64_t pc = thread.GetRegisterContext()->GetPC();
182 // Max size of an instruction in x86 is 15 bytes.
183 AddressRange range(pc, 2 * 15);
184
186 arch, nullptr, nullptr, m_process->GetTarget(), range);
187 if (!disassembler_sp) {
188 return ThreadPlanSP();
189 }
190
191 InstructionList *insn_list = &disassembler_sp->GetInstructionList();
192 if (insn_list == nullptr) {
193 return ThreadPlanSP();
194 }
195
196 // First instruction in a x86 Windows trampoline is going to be an indirect
197 // jump through the IAT and the next one will be a nop (usually there for
198 // alignment purposes). e.g.:
199 // 0x70ff4cfc <+956>: jmpl *0x7100c2a8
200 // 0x70ff4d02 <+962>: nop
201
202 auto first_insn = insn_list->GetInstructionAtIndex(0);
203 auto second_insn = insn_list->GetInstructionAtIndex(1);
204
206 if (first_insn == nullptr || second_insn == nullptr ||
207 strcmp(first_insn->GetMnemonic(&exe_ctx), "jmpl") != 0 ||
208 strcmp(second_insn->GetMnemonic(&exe_ctx), "nop") != 0) {
209 return ThreadPlanSP();
210 }
211
212 assert(first_insn->DoesBranch() && !second_insn->DoesBranch());
213
215 thread, false, false, eVoteNoOpinion, eVoteNoOpinion));
216}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:366
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:382
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:32
A section + offset based address range class.
Definition: AddressRange.h:25
A section + offset based address class.
Definition: Address.h:62
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:285
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:450
static lldb::DisassemblerSP DisassembleRange(const ArchSpec &arch, const char *plugin_name, const char *flavor, Target &target, const AddressRange &disasm_range, bool force_live_memory=false)
void DidAttach() override
Called after attaching a process.
void DidLaunch() override
Called after launching a process.
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)
std::map< lldb::ModuleSP, lldb::addr_t > m_loaded_modules
void OnUnloadModule(lldb::addr_t module_addr)
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.
A plug-in interface definition class for dynamic loaders.
Definition: DynamicLoader.h:53
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.
void UnloadSectionsCommon(const lldb::ModuleSP module)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition: FileSpec.h:56
lldb::InstructionSP GetInstructionAtIndex(size_t idx) const
A collection class for Module objects.
Definition: ModuleList.h:103
void Append(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list.
Definition: ModuleList.cpp:247
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
A plug-in interface definition class for debugging a process.
Definition: Process.h:341
virtual lldb::addr_t GetImageInfoAddress()
Get the image information address for the current process.
Definition: Process.cpp:1529
virtual Status GetFileLoadAddress(const FileSpec &file, bool &is_loaded, lldb::addr_t &load_addr)
Try to find the load address of a file.
Definition: Process.h:2573
virtual llvm::Error LoadModules()
Sometimes processes know how to retrieve and load shared libraries.
Definition: Process.h:692
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1285
An error handling class.
Definition: Status.h:44
bool Success() const
Test for success condition.
Definition: Status.cpp:278
void ModulesDidLoad(ModuleList &module_list)
Definition: Target.cpp:1691
lldb::ModuleSP GetOrCreateModule(const ModuleSpec &module_spec, bool notify, Status *error_ptr=nullptr)
Find a binary on the system and return its Module, or return an existing Module that is already in th...
Definition: Target.cpp:2159
void ModulesDidUnload(ModuleList &module_list, bool delete_locations)
Definition: Target.cpp:1725
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow)
Definition: Target.cpp:3114
const ArchSpec & GetArchitecture() const
Definition: Target.h:1028
virtual lldb::RegisterContextSP GetRegisterContext()=0
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
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:331
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
Definition: lldb-forward.h:449
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
Definition: lldb-forward.h:339
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:371