1 //===-- DynamicLoaderWindowsDYLD.cpp --------------------------------*- C++
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #include "DynamicLoaderWindowsDYLD.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/PluginManager.h"
15 #include "lldb/Target/ExecutionContext.h"
16 #include "lldb/Target/Process.h"
17 #include "lldb/Target/RegisterContext.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Target/ThreadPlanStepInstruction.h"
20 #include "lldb/Utility/Log.h"
22 #include "llvm/ADT/Triple.h"
25 using namespace lldb_private;
27 DynamicLoaderWindowsDYLD::DynamicLoaderWindowsDYLD(Process *process)
28 : DynamicLoader(process) {}
30 DynamicLoaderWindowsDYLD::~DynamicLoaderWindowsDYLD() {}
32 void DynamicLoaderWindowsDYLD::Initialize() {
33 PluginManager::RegisterPlugin(GetPluginNameStatic(),
34 GetPluginDescriptionStatic(), CreateInstance);
37 void DynamicLoaderWindowsDYLD::Terminate() {}
39 ConstString DynamicLoaderWindowsDYLD::GetPluginNameStatic() {
40 static ConstString g_plugin_name("windows-dyld");
44 const char *DynamicLoaderWindowsDYLD::GetPluginDescriptionStatic() {
45 return "Dynamic loader plug-in that watches for shared library "
46 "loads/unloads in Windows processes.";
49 DynamicLoader *DynamicLoaderWindowsDYLD::CreateInstance(Process *process,
51 bool should_create = force;
53 const llvm::Triple &triple_ref =
54 process->GetTarget().GetArchitecture().GetTriple();
55 if (triple_ref.getOS() == llvm::Triple::Win32)
60 return new DynamicLoaderWindowsDYLD(process);
65 void DynamicLoaderWindowsDYLD::DidAttach() {
66 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
68 log->Printf("DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
70 ModuleSP executable = GetTargetExecutable();
72 if (!executable.get())
75 // Try to fetch the load address of the file from the process, since there
76 // could be randomization of the load address.
78 // It might happen that the remote has a different dir for the file, so we
79 // only send the basename of the executable in the query. I think this is safe
80 // because I doubt that two executables with the same basenames are loaded in
83 executable->GetPlatformFileSpec().GetFilename().GetCString());
86 lldb::addr_t load_addr;
87 Status error = m_process->GetFileLoadAddress(file_spec, is_loaded, load_addr);
88 if (error.Success() && is_loaded) {
89 base_addr = load_addr;
90 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, base_addr, false);
93 ModuleList module_list;
94 module_list.Append(executable);
95 m_process->GetTarget().ModulesDidLoad(module_list);
96 m_process->LoadModules();
99 void DynamicLoaderWindowsDYLD::DidLaunch() {}
101 Status DynamicLoaderWindowsDYLD::CanLoadImage() { return Status(); }
103 ConstString DynamicLoaderWindowsDYLD::GetPluginName() {
104 return GetPluginNameStatic();
107 uint32_t DynamicLoaderWindowsDYLD::GetPluginVersion() { return 1; }
110 DynamicLoaderWindowsDYLD::GetStepThroughTrampolinePlan(Thread &thread,
112 auto arch = m_process->GetTarget().GetArchitecture();
113 if (arch.GetMachine() != llvm::Triple::x86) {
114 return ThreadPlanSP();
117 uint64_t pc = thread.GetRegisterContext()->GetPC();
118 // Max size of an instruction in x86 is 15 bytes.
119 AddressRange range(pc, 2 * 15);
121 ExecutionContext exe_ctx(m_process->GetTarget());
122 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
123 arch, nullptr, nullptr, exe_ctx, range, true);
124 if (!disassembler_sp) {
125 return ThreadPlanSP();
128 InstructionList *insn_list = &disassembler_sp->GetInstructionList();
129 if (insn_list == nullptr) {
130 return ThreadPlanSP();
133 // First instruction in a x86 Windows trampoline is going to be an indirect
134 // jump through the IAT and the next one will be a nop (usually there for
135 // alignment purposes). e.g.:
136 // 0x70ff4cfc <+956>: jmpl *0x7100c2a8
137 // 0x70ff4d02 <+962>: nop
139 auto first_insn = insn_list->GetInstructionAtIndex(0);
140 auto second_insn = insn_list->GetInstructionAtIndex(1);
142 if (first_insn == nullptr || second_insn == nullptr ||
143 strcmp(first_insn->GetMnemonic(&exe_ctx), "jmpl") != 0 ||
144 strcmp(second_insn->GetMnemonic(&exe_ctx), "nop") != 0) {
145 return ThreadPlanSP();
148 assert(first_insn->DoesBranch() && !second_insn->DoesBranch());
150 return ThreadPlanSP(new ThreadPlanStepInstruction(
151 thread, false, false, eVoteNoOpinion, eVoteNoOpinion));