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"
14 #include "lldb/Target/Platform.h"
15 #include "lldb/Target/Process.h"
17 #include "lldb/Target/Target.h"
19 #include "lldb/Utility/LLDBLog.h"
20 #include "lldb/Utility/Log.h"
21 
22 #include "llvm/ADT/Triple.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
28 
30  : DynamicLoader(process) {}
31 
32 DynamicLoaderWindowsDYLD::~DynamicLoaderWindowsDYLD() = default;
33 
34 void DynamicLoaderWindowsDYLD::Initialize() {
35  PluginManager::RegisterPlugin(GetPluginNameStatic(),
36  GetPluginDescriptionStatic(), CreateInstance);
37 }
38 
39 void DynamicLoaderWindowsDYLD::Terminate() {}
40 
41 llvm::StringRef DynamicLoaderWindowsDYLD::GetPluginDescriptionStatic() {
42  return "Dynamic loader plug-in that watches for shared library "
43  "loads/unloads in Windows processes.";
44 }
45 
46 DynamicLoader *DynamicLoaderWindowsDYLD::CreateInstance(Process *process,
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 
62 void DynamicLoaderWindowsDYLD::OnLoadModule(lldb::ModuleSP module_sp,
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) {
68  Status error;
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 
82 void DynamicLoaderWindowsDYLD::OnUnloadModule(lldb::addr_t module_addr) {
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 
97 lldb::addr_t DynamicLoaderWindowsDYLD::GetLoadAddress(ModuleSP executable) {
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 
117  return LLDB_INVALID_ADDRESS;
118 }
119 
120 void DynamicLoaderWindowsDYLD::DidAttach() {
121  Log *log = GetLog(LLDBLog::DynamicLoader);
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.
136  lldb::addr_t image_base = m_process->GetImageInfoAddress();
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 
150 void DynamicLoaderWindowsDYLD::DidLaunch() {
151  Log *log = GetLog(LLDBLog::DynamicLoader);
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 
171 Status DynamicLoaderWindowsDYLD::CanLoadImage() { return Status(); }
172 
173 ThreadPlanSP
174 DynamicLoaderWindowsDYLD::GetStepThroughTrampolinePlan(Thread &thread,
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 
185  DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
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 
205  ExecutionContext exe_ctx(m_process->GetTarget());
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 
214  return ThreadPlanSP(new ThreadPlanStepInstruction(
215  thread, false, false, eVoteNoOpinion, eVoteNoOpinion));
216 }
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
DynamicLoaderWindowsDYLD.h
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:343
lldb_private::Process
Definition: Process.h:338
Module.h
lldb_private::Process::GetTarget
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1206
lldb_private::eVoteNoOpinion
@ eVoteNoOpinion
Definition: lldb-private-enumerations.h:59
lldb_private::InstructionList::GetInstructionAtIndex
lldb::InstructionSP GetInstructionAtIndex(size_t idx) const
Definition: Disassembler.cpp:945
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:455
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
pc
@ pc
Definition: CompactUnwindInfo.cpp:1251
lldb_private::ThreadPlanStepInstruction
Definition: ThreadPlanStepInstruction.h:18
Process.h
Target.h
Platform.h
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::Status::Success
bool Success() const
Test for success condition.
Definition: Status.cpp:287
lldb_private::InstructionList
Definition: Disassembler.h:267
lldb_private::ModuleList
Definition: ModuleList.h:81
Log.h
lldb_private::Thread
Definition: Thread.h:61
lldb_private::AddressRange
Definition: AddressRange.h:25
lldb_private::DynamicLoader
Definition: DynamicLoader.h:52
lldb_private::ModuleSpec
Definition: ModuleSpec.h:27
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:989
lldb_private::Status
Definition: Status.h:44
lldb_private::Address
Definition: Address.h:59
ThreadPlanStepInstruction.h
PluginManager.h
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Address::GetModule
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:283
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Log
Definition: Log.h:115
lldb_private::DynamicLoaderWindowsDYLD
Definition: DynamicLoaderWindowsDYLD.h:19
lldb_private::Thread::GetRegisterContext
virtual lldb::RegisterContextSP GetRegisterContext()=0
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:308
lldb
Definition: SBAddress.h:15
RegisterContext.h
LLDB_LOG_ERROR
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:359
LLDBLog.h
ExecutionContext.h
lldb_private::ModuleList::Append
void Append(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list.