180814287SRaphael Isemann //===-- DynamicLoaderWindowsDYLD.cpp --------------------------------------===//
238a1b7eaSStephane Sezer //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
638a1b7eaSStephane Sezer //
738a1b7eaSStephane Sezer //===----------------------------------------------------------------------===//
838a1b7eaSStephane Sezer
938a1b7eaSStephane Sezer #include "DynamicLoaderWindowsDYLD.h"
1038a1b7eaSStephane Sezer
11488214feSNathan Lanza #include "lldb/Core/Module.h"
1238a1b7eaSStephane Sezer #include "lldb/Core/PluginManager.h"
1385317f23SStephane Sezer #include "lldb/Target/ExecutionContext.h"
14eb6671e7SAaron Smith #include "lldb/Target/Platform.h"
1538a1b7eaSStephane Sezer #include "lldb/Target/Process.h"
1685317f23SStephane Sezer #include "lldb/Target/RegisterContext.h"
1738a1b7eaSStephane Sezer #include "lldb/Target/Target.h"
1885317f23SStephane Sezer #include "lldb/Target/ThreadPlanStepInstruction.h"
19*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
20488214feSNathan Lanza #include "lldb/Utility/Log.h"
2138a1b7eaSStephane Sezer
2238a1b7eaSStephane Sezer #include "llvm/ADT/Triple.h"
2338a1b7eaSStephane Sezer
2438a1b7eaSStephane Sezer using namespace lldb;
2538a1b7eaSStephane Sezer using namespace lldb_private;
2638a1b7eaSStephane Sezer
LLDB_PLUGIN_DEFINE(DynamicLoaderWindowsDYLD)27bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(DynamicLoaderWindowsDYLD)
28fbb4d1e4SJonas Devlieghere
2938a1b7eaSStephane Sezer DynamicLoaderWindowsDYLD::DynamicLoaderWindowsDYLD(Process *process)
30b9c1b51eSKate Stone : DynamicLoader(process) {}
31b9c1b51eSKate Stone
32fd2433e1SJonas Devlieghere DynamicLoaderWindowsDYLD::~DynamicLoaderWindowsDYLD() = default;
33b9c1b51eSKate Stone
Initialize()34b9c1b51eSKate Stone void DynamicLoaderWindowsDYLD::Initialize() {
35b9c1b51eSKate Stone PluginManager::RegisterPlugin(GetPluginNameStatic(),
36b9c1b51eSKate Stone GetPluginDescriptionStatic(), CreateInstance);
3738a1b7eaSStephane Sezer }
3838a1b7eaSStephane Sezer
Terminate()39b9c1b51eSKate Stone void DynamicLoaderWindowsDYLD::Terminate() {}
4038a1b7eaSStephane Sezer
GetPluginDescriptionStatic()416fa1b4ffSPavel Labath llvm::StringRef DynamicLoaderWindowsDYLD::GetPluginDescriptionStatic() {
4238a1b7eaSStephane Sezer return "Dynamic loader plug-in that watches for shared library "
4338a1b7eaSStephane Sezer "loads/unloads in Windows processes.";
4438a1b7eaSStephane Sezer }
4538a1b7eaSStephane Sezer
CreateInstance(Process * process,bool force)46b9c1b51eSKate Stone DynamicLoader *DynamicLoaderWindowsDYLD::CreateInstance(Process *process,
47b9c1b51eSKate Stone bool force) {
4838a1b7eaSStephane Sezer bool should_create = force;
49b9c1b51eSKate Stone if (!should_create) {
50b9c1b51eSKate Stone const llvm::Triple &triple_ref =
51b9c1b51eSKate Stone process->GetTarget().GetArchitecture().GetTriple();
5238a1b7eaSStephane Sezer if (triple_ref.getOS() == llvm::Triple::Win32)
5338a1b7eaSStephane Sezer should_create = true;
5438a1b7eaSStephane Sezer }
5538a1b7eaSStephane Sezer
5638a1b7eaSStephane Sezer if (should_create)
5738a1b7eaSStephane Sezer return new DynamicLoaderWindowsDYLD(process);
5838a1b7eaSStephane Sezer
5938a1b7eaSStephane Sezer return nullptr;
6038a1b7eaSStephane Sezer }
6138a1b7eaSStephane Sezer
OnLoadModule(lldb::ModuleSP module_sp,const ModuleSpec module_spec,lldb::addr_t module_addr)62a2d9fdf5SStella Stamenova void DynamicLoaderWindowsDYLD::OnLoadModule(lldb::ModuleSP module_sp,
63a2d9fdf5SStella Stamenova const ModuleSpec module_spec,
64eb6671e7SAaron Smith lldb::addr_t module_addr) {
65a2d9fdf5SStella Stamenova
66a2d9fdf5SStella Stamenova // Resolve the module unless we already have one.
67a2d9fdf5SStella Stamenova if (!module_sp) {
68eb6671e7SAaron Smith Status error;
691724a179SJason Molenda module_sp = m_process->GetTarget().GetOrCreateModule(module_spec,
701724a179SJason Molenda true /* notify */, &error);
71eb6671e7SAaron Smith if (error.Fail())
72eb6671e7SAaron Smith return;
73a2d9fdf5SStella Stamenova }
74eb6671e7SAaron Smith
75eb6671e7SAaron Smith m_loaded_modules[module_sp] = module_addr;
76eb6671e7SAaron Smith UpdateLoadedSectionsCommon(module_sp, module_addr, false);
77a2d9fdf5SStella Stamenova ModuleList module_list;
78a2d9fdf5SStella Stamenova module_list.Append(module_sp);
79a2d9fdf5SStella Stamenova m_process->GetTarget().ModulesDidLoad(module_list);
80eb6671e7SAaron Smith }
81eb6671e7SAaron Smith
OnUnloadModule(lldb::addr_t module_addr)82eb6671e7SAaron Smith void DynamicLoaderWindowsDYLD::OnUnloadModule(lldb::addr_t module_addr) {
83eb6671e7SAaron Smith Address resolved_addr;
84eb6671e7SAaron Smith if (!m_process->GetTarget().ResolveLoadAddress(module_addr, resolved_addr))
85eb6671e7SAaron Smith return;
86eb6671e7SAaron Smith
87eb6671e7SAaron Smith ModuleSP module_sp = resolved_addr.GetModule();
88eb6671e7SAaron Smith if (module_sp) {
89eb6671e7SAaron Smith m_loaded_modules.erase(module_sp);
90eb6671e7SAaron Smith UnloadSectionsCommon(module_sp);
91a2d9fdf5SStella Stamenova ModuleList module_list;
92a2d9fdf5SStella Stamenova module_list.Append(module_sp);
93a2d9fdf5SStella Stamenova m_process->GetTarget().ModulesDidUnload(module_list, false);
94eb6671e7SAaron Smith }
95eb6671e7SAaron Smith }
96eb6671e7SAaron Smith
GetLoadAddress(ModuleSP executable)97eb6671e7SAaron Smith lldb::addr_t DynamicLoaderWindowsDYLD::GetLoadAddress(ModuleSP executable) {
98eb6671e7SAaron Smith // First, see if the load address is already cached.
99eb6671e7SAaron Smith auto it = m_loaded_modules.find(executable);
100eb6671e7SAaron Smith if (it != m_loaded_modules.end() && it->second != LLDB_INVALID_ADDRESS)
101eb6671e7SAaron Smith return it->second;
102eb6671e7SAaron Smith
103eb6671e7SAaron Smith lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
104eb6671e7SAaron Smith
105eb6671e7SAaron Smith // Second, try to get it through the process plugins. For a remote process,
106eb6671e7SAaron Smith // the remote platform will be responsible for providing it.
107eb6671e7SAaron Smith FileSpec file_spec(executable->GetPlatformFileSpec());
108eb6671e7SAaron Smith bool is_loaded = false;
109eb6671e7SAaron Smith Status status =
110eb6671e7SAaron Smith m_process->GetFileLoadAddress(file_spec, is_loaded, load_addr);
111eb6671e7SAaron Smith // Servers other than lldb server could respond with a bogus address.
112eb6671e7SAaron Smith if (status.Success() && is_loaded && load_addr != LLDB_INVALID_ADDRESS) {
113eb6671e7SAaron Smith m_loaded_modules[executable] = load_addr;
114eb6671e7SAaron Smith return load_addr;
115eb6671e7SAaron Smith }
116eb6671e7SAaron Smith
117eb6671e7SAaron Smith return LLDB_INVALID_ADDRESS;
118eb6671e7SAaron Smith }
119eb6671e7SAaron Smith
DidAttach()120488214feSNathan Lanza void DynamicLoaderWindowsDYLD::DidAttach() {
121a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::DynamicLoader);
12263e5fb76SJonas Devlieghere LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
123488214feSNathan Lanza
124488214feSNathan Lanza ModuleSP executable = GetTargetExecutable();
125488214feSNathan Lanza
126488214feSNathan Lanza if (!executable.get())
127488214feSNathan Lanza return;
128488214feSNathan Lanza
129488214feSNathan Lanza // Try to fetch the load address of the file from the process, since there
130488214feSNathan Lanza // could be randomization of the load address.
131eb6671e7SAaron Smith lldb::addr_t load_addr = GetLoadAddress(executable);
132eb6671e7SAaron Smith if (load_addr == LLDB_INVALID_ADDRESS)
133eb6671e7SAaron Smith return;
134488214feSNathan Lanza
135eb6671e7SAaron Smith // Request the process base address.
136eb6671e7SAaron Smith lldb::addr_t image_base = m_process->GetImageInfoAddress();
137eb6671e7SAaron Smith if (image_base == load_addr)
138eb6671e7SAaron Smith return;
139eb6671e7SAaron Smith
140eb6671e7SAaron Smith // Rebase the process's modules if there is a mismatch.
141eb6671e7SAaron Smith UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false);
142488214feSNathan Lanza
143488214feSNathan Lanza ModuleList module_list;
144488214feSNathan Lanza module_list.Append(executable);
145488214feSNathan Lanza m_process->GetTarget().ModulesDidLoad(module_list);
146d668260fSAntonio Afonso auto error = m_process->LoadModules();
147d668260fSAntonio Afonso LLDB_LOG_ERROR(log, std::move(error), "failed to load modules: {0}");
148488214feSNathan Lanza }
14938a1b7eaSStephane Sezer
DidLaunch()150eb6671e7SAaron Smith void DynamicLoaderWindowsDYLD::DidLaunch() {
151a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::DynamicLoader);
15263e5fb76SJonas Devlieghere LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
153eb6671e7SAaron Smith
154eb6671e7SAaron Smith ModuleSP executable = GetTargetExecutable();
155eb6671e7SAaron Smith if (!executable.get())
156eb6671e7SAaron Smith return;
157eb6671e7SAaron Smith
158eb6671e7SAaron Smith lldb::addr_t load_addr = GetLoadAddress(executable);
159eb6671e7SAaron Smith if (load_addr != LLDB_INVALID_ADDRESS) {
160eb6671e7SAaron Smith // Update the loaded sections so that the breakpoints can be resolved.
161eb6671e7SAaron Smith UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false);
162eb6671e7SAaron Smith
163eb6671e7SAaron Smith ModuleList module_list;
164eb6671e7SAaron Smith module_list.Append(executable);
165eb6671e7SAaron Smith m_process->GetTarget().ModulesDidLoad(module_list);
166d668260fSAntonio Afonso auto error = m_process->LoadModules();
167d668260fSAntonio Afonso LLDB_LOG_ERROR(log, std::move(error), "failed to load modules: {0}");
168eb6671e7SAaron Smith }
169eb6671e7SAaron Smith }
17038a1b7eaSStephane Sezer
CanLoadImage()17197206d57SZachary Turner Status DynamicLoaderWindowsDYLD::CanLoadImage() { return Status(); }
17238a1b7eaSStephane Sezer
17338a1b7eaSStephane Sezer ThreadPlanSP
GetStepThroughTrampolinePlan(Thread & thread,bool stop)174b9c1b51eSKate Stone DynamicLoaderWindowsDYLD::GetStepThroughTrampolinePlan(Thread &thread,
175b9c1b51eSKate Stone bool stop) {
17685317f23SStephane Sezer auto arch = m_process->GetTarget().GetArchitecture();
17785317f23SStephane Sezer if (arch.GetMachine() != llvm::Triple::x86) {
17838a1b7eaSStephane Sezer return ThreadPlanSP();
17938a1b7eaSStephane Sezer }
18085317f23SStephane Sezer
18185317f23SStephane Sezer uint64_t pc = thread.GetRegisterContext()->GetPC();
18285317f23SStephane Sezer // Max size of an instruction in x86 is 15 bytes.
18385317f23SStephane Sezer AddressRange range(pc, 2 * 15);
18485317f23SStephane Sezer
18585317f23SStephane Sezer DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
186e9fe788dSJason Molenda arch, nullptr, nullptr, m_process->GetTarget(), range);
18785317f23SStephane Sezer if (!disassembler_sp) {
18885317f23SStephane Sezer return ThreadPlanSP();
18985317f23SStephane Sezer }
19085317f23SStephane Sezer
19185317f23SStephane Sezer InstructionList *insn_list = &disassembler_sp->GetInstructionList();
19285317f23SStephane Sezer if (insn_list == nullptr) {
19385317f23SStephane Sezer return ThreadPlanSP();
19485317f23SStephane Sezer }
19585317f23SStephane Sezer
19685317f23SStephane Sezer // First instruction in a x86 Windows trampoline is going to be an indirect
19785317f23SStephane Sezer // jump through the IAT and the next one will be a nop (usually there for
19885317f23SStephane Sezer // alignment purposes). e.g.:
19985317f23SStephane Sezer // 0x70ff4cfc <+956>: jmpl *0x7100c2a8
20085317f23SStephane Sezer // 0x70ff4d02 <+962>: nop
20185317f23SStephane Sezer
20285317f23SStephane Sezer auto first_insn = insn_list->GetInstructionAtIndex(0);
20385317f23SStephane Sezer auto second_insn = insn_list->GetInstructionAtIndex(1);
20485317f23SStephane Sezer
20504592d5bSPavel Labath ExecutionContext exe_ctx(m_process->GetTarget());
20685317f23SStephane Sezer if (first_insn == nullptr || second_insn == nullptr ||
20785317f23SStephane Sezer strcmp(first_insn->GetMnemonic(&exe_ctx), "jmpl") != 0 ||
20885317f23SStephane Sezer strcmp(second_insn->GetMnemonic(&exe_ctx), "nop") != 0) {
20985317f23SStephane Sezer return ThreadPlanSP();
21085317f23SStephane Sezer }
21185317f23SStephane Sezer
21285317f23SStephane Sezer assert(first_insn->DoesBranch() && !second_insn->DoesBranch());
21385317f23SStephane Sezer
21485317f23SStephane Sezer return ThreadPlanSP(new ThreadPlanStepInstruction(
21585317f23SStephane Sezer thread, false, false, eVoteNoOpinion, eVoteNoOpinion));
21685317f23SStephane Sezer }
217