1c1121908SPaolo Severini //===-- DynamicLoaderWasmDYLD.cpp -----------------------------------------===//
2c1121908SPaolo Severini //
3c1121908SPaolo Severini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c1121908SPaolo Severini // See https://llvm.org/LICENSE.txt for license information.
5c1121908SPaolo Severini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c1121908SPaolo Severini //
7c1121908SPaolo Severini //===----------------------------------------------------------------------===//
8c1121908SPaolo Severini 
9c1121908SPaolo Severini #include "DynamicLoaderWasmDYLD.h"
10c1121908SPaolo Severini 
11c1121908SPaolo Severini #include "Plugins/ObjectFile/wasm/ObjectFileWasm.h"
12c1121908SPaolo Severini #include "lldb/Core/Module.h"
13c1121908SPaolo Severini #include "lldb/Core/PluginManager.h"
14c1121908SPaolo Severini #include "lldb/Core/Section.h"
15c1121908SPaolo Severini #include "lldb/Target/Process.h"
16c1121908SPaolo Severini #include "lldb/Target/Target.h"
17c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
18c1121908SPaolo Severini #include "lldb/Utility/Log.h"
19c1121908SPaolo Severini 
20c1121908SPaolo Severini using namespace lldb;
21c1121908SPaolo Severini using namespace lldb_private;
22c1121908SPaolo Severini using namespace lldb_private::wasm;
23c1121908SPaolo Severini 
LLDB_PLUGIN_DEFINE(DynamicLoaderWasmDYLD)24c1121908SPaolo Severini LLDB_PLUGIN_DEFINE(DynamicLoaderWasmDYLD)
25c1121908SPaolo Severini 
26c1121908SPaolo Severini DynamicLoaderWasmDYLD::DynamicLoaderWasmDYLD(Process *process)
27c1121908SPaolo Severini     : DynamicLoader(process) {}
28c1121908SPaolo Severini 
Initialize()29c1121908SPaolo Severini void DynamicLoaderWasmDYLD::Initialize() {
30c1121908SPaolo Severini   PluginManager::RegisterPlugin(GetPluginNameStatic(),
31c1121908SPaolo Severini                                 GetPluginDescriptionStatic(), CreateInstance);
32c1121908SPaolo Severini }
33c1121908SPaolo Severini 
GetPluginDescriptionStatic()346fa1b4ffSPavel Labath llvm::StringRef DynamicLoaderWasmDYLD::GetPluginDescriptionStatic() {
35c1121908SPaolo Severini   return "Dynamic loader plug-in that watches for shared library "
36c1121908SPaolo Severini          "loads/unloads in WebAssembly engines.";
37c1121908SPaolo Severini }
38c1121908SPaolo Severini 
CreateInstance(Process * process,bool force)39c1121908SPaolo Severini DynamicLoader *DynamicLoaderWasmDYLD::CreateInstance(Process *process,
40c1121908SPaolo Severini                                                      bool force) {
41c1121908SPaolo Severini   bool should_create = force;
42c1121908SPaolo Severini   if (!should_create) {
43c1121908SPaolo Severini     should_create =
44c1121908SPaolo Severini         (process->GetTarget().GetArchitecture().GetTriple().getArch() ==
45c1121908SPaolo Severini          llvm::Triple::wasm32);
46c1121908SPaolo Severini   }
47c1121908SPaolo Severini 
48c1121908SPaolo Severini   if (should_create)
49c1121908SPaolo Severini     return new DynamicLoaderWasmDYLD(process);
50c1121908SPaolo Severini 
51c1121908SPaolo Severini   return nullptr;
52c1121908SPaolo Severini }
53c1121908SPaolo Severini 
DidAttach()54c1121908SPaolo Severini void DynamicLoaderWasmDYLD::DidAttach() {
55a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::DynamicLoader);
56c1121908SPaolo Severini   LLDB_LOGF(log, "DynamicLoaderWasmDYLD::%s()", __FUNCTION__);
57c1121908SPaolo Severini 
58c1121908SPaolo Severini   // Ask the process for the list of loaded WebAssembly modules.
59c1121908SPaolo Severini   auto error = m_process->LoadModules();
60c1121908SPaolo Severini   LLDB_LOG_ERROR(log, std::move(error), "Couldn't load modules: {0}");
61c1121908SPaolo Severini }
62c1121908SPaolo Severini 
GetStepThroughTrampolinePlan(Thread & thread,bool stop)63c1121908SPaolo Severini ThreadPlanSP DynamicLoaderWasmDYLD::GetStepThroughTrampolinePlan(Thread &thread,
64c1121908SPaolo Severini                                                                  bool stop) {
65c1121908SPaolo Severini   return ThreadPlanSP();
66c1121908SPaolo Severini }
67*e67cee09SPavel Labath 
LoadModuleAtAddress(const lldb_private::FileSpec & file,lldb::addr_t link_map_addr,lldb::addr_t base_addr,bool base_addr_is_offset)68*e67cee09SPavel Labath lldb::ModuleSP DynamicLoaderWasmDYLD::LoadModuleAtAddress(
69*e67cee09SPavel Labath     const lldb_private::FileSpec &file, lldb::addr_t link_map_addr,
70*e67cee09SPavel Labath     lldb::addr_t base_addr, bool base_addr_is_offset) {
71*e67cee09SPavel Labath   if (ModuleSP module_sp = DynamicLoader::LoadModuleAtAddress(
72*e67cee09SPavel Labath           file, link_map_addr, base_addr, base_addr_is_offset))
73*e67cee09SPavel Labath     return module_sp;
74*e67cee09SPavel Labath 
75*e67cee09SPavel Labath   if (ModuleSP module_sp = m_process->ReadModuleFromMemory(file, base_addr)) {
76*e67cee09SPavel Labath     UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
77*e67cee09SPavel Labath     m_process->GetTarget().GetImages().AppendIfNeeded(module_sp);
78*e67cee09SPavel Labath     return module_sp;
79*e67cee09SPavel Labath   }
80*e67cee09SPavel Labath 
81*e67cee09SPavel Labath   return nullptr;
82*e67cee09SPavel Labath }
83