180814287SRaphael Isemann //===-- DynamicLoaderStatic.cpp -------------------------------------------===//
2fc7117aeSGreg Clayton //
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
6fc7117aeSGreg Clayton //
7fc7117aeSGreg Clayton //===----------------------------------------------------------------------===//
8fc7117aeSGreg Clayton
9fc7117aeSGreg Clayton #include "lldb/Core/Module.h"
10fc7117aeSGreg Clayton #include "lldb/Core/PluginManager.h"
111f746071SGreg Clayton #include "lldb/Core/Section.h"
121f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
1378a14128SJason Molenda #include "lldb/Target/SectionLoadList.h"
14fc7117aeSGreg Clayton #include "lldb/Target/Target.h"
15fc7117aeSGreg Clayton
16fc7117aeSGreg Clayton #include "DynamicLoaderStatic.h"
17fc7117aeSGreg Clayton
18fc7117aeSGreg Clayton using namespace lldb;
19fc7117aeSGreg Clayton using namespace lldb_private;
20fc7117aeSGreg Clayton
LLDB_PLUGIN_DEFINE(DynamicLoaderStatic)21bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(DynamicLoaderStatic)
22fbb4d1e4SJonas Devlieghere
2305097246SAdrian Prantl // Create an instance of this class. This function is filled into the plugin
2405097246SAdrian Prantl // info class that gets handed out by the plugin factory and allows the lldb to
2505097246SAdrian Prantl // instantiate an instance of this class.
26b9c1b51eSKate Stone DynamicLoader *DynamicLoaderStatic::CreateInstance(Process *process,
27b9c1b51eSKate Stone bool force) {
28fc7117aeSGreg Clayton bool create = force;
29b9c1b51eSKate Stone if (!create) {
30b9c1b51eSKate Stone const llvm::Triple &triple_ref =
31b9c1b51eSKate Stone process->GetTarget().GetArchitecture().GetTriple();
32fc7117aeSGreg Clayton const llvm::Triple::OSType os_type = triple_ref.getOS();
33ebf9a99bSJonas Devlieghere const llvm::Triple::ArchType arch_type = triple_ref.getArch();
34ebf9a99bSJonas Devlieghere if (os_type == llvm::Triple::UnknownOS) {
35ebf9a99bSJonas Devlieghere // The WASM and Hexagon plugin check the ArchType rather than the OSType,
36ebf9a99bSJonas Devlieghere // so explicitly reject those here.
37ebf9a99bSJonas Devlieghere switch(arch_type) {
38ebf9a99bSJonas Devlieghere case llvm::Triple::hexagon:
39ebf9a99bSJonas Devlieghere case llvm::Triple::wasm32:
40ebf9a99bSJonas Devlieghere case llvm::Triple::wasm64:
41ebf9a99bSJonas Devlieghere break;
42ebf9a99bSJonas Devlieghere default:
43fc7117aeSGreg Clayton create = true;
44fc7117aeSGreg Clayton }
45ebf9a99bSJonas Devlieghere }
46ebf9a99bSJonas Devlieghere }
47fc7117aeSGreg Clayton
48b9c1b51eSKate Stone if (!create) {
4949bce8ecSSean Callanan Module *exe_module = process->GetTarget().GetExecutableModulePointer();
50b9c1b51eSKate Stone if (exe_module) {
5149bce8ecSSean Callanan ObjectFile *object_file = exe_module->GetObjectFile();
52b9c1b51eSKate Stone if (object_file) {
5349bce8ecSSean Callanan create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);
5449bce8ecSSean Callanan }
5549bce8ecSSean Callanan }
5649bce8ecSSean Callanan }
5749bce8ecSSean Callanan
58fc7117aeSGreg Clayton if (create)
59fc7117aeSGreg Clayton return new DynamicLoaderStatic(process);
60248a1305SKonrad Kleine return nullptr;
61fc7117aeSGreg Clayton }
62fc7117aeSGreg Clayton
63fc7117aeSGreg Clayton // Constructor
DynamicLoaderStatic(Process * process)64b9c1b51eSKate Stone DynamicLoaderStatic::DynamicLoaderStatic(Process *process)
65b9c1b51eSKate Stone : DynamicLoader(process) {}
66fc7117aeSGreg Clayton
67fc7117aeSGreg Clayton /// Called after attaching a process.
68fc7117aeSGreg Clayton ///
69fc7117aeSGreg Clayton /// Allow DynamicLoader plug-ins to execute some code after
70fc7117aeSGreg Clayton /// attaching to a process.
DidAttach()71b9c1b51eSKate Stone void DynamicLoaderStatic::DidAttach() { LoadAllImagesAtFileAddresses(); }
72fc7117aeSGreg Clayton
73fc7117aeSGreg Clayton /// Called after attaching a process.
74fc7117aeSGreg Clayton ///
75fc7117aeSGreg Clayton /// Allow DynamicLoader plug-ins to execute some code after
76fc7117aeSGreg Clayton /// attaching to a process.
DidLaunch()77b9c1b51eSKate Stone void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); }
78fc7117aeSGreg Clayton
LoadAllImagesAtFileAddresses()79b9c1b51eSKate Stone void DynamicLoaderStatic::LoadAllImagesAtFileAddresses() {
801759848bSEnrico Granata const ModuleList &module_list = m_process->GetTarget().GetImages();
81fc7117aeSGreg Clayton
82fc7117aeSGreg Clayton ModuleList loaded_module_list;
83fc7117aeSGreg Clayton
8462243f84SGreg Clayton // Disable JIT for static dynamic loader targets
8562243f84SGreg Clayton m_process->SetCanJIT(false);
8662243f84SGreg Clayton
87f2e05855SJonas Devlieghere for (ModuleSP module_sp : module_list.Modules()) {
88b9c1b51eSKate Stone if (module_sp) {
89fc7117aeSGreg Clayton bool changed = false;
90fc7117aeSGreg Clayton ObjectFile *image_object_file = module_sp->GetObjectFile();
91b9c1b51eSKate Stone if (image_object_file) {
92fc7117aeSGreg Clayton SectionList *section_list = image_object_file->GetSectionList();
93b9c1b51eSKate Stone if (section_list) {
94fc7117aeSGreg Clayton // All sections listed in the dyld image info structure will all
95fc7117aeSGreg Clayton // either be fixed up already, or they will all be off by a single
9605097246SAdrian Prantl // slide amount that is determined by finding the first segment that
9705097246SAdrian Prantl // is at file offset zero which also has bytes (a file size that is
9805097246SAdrian Prantl // greater than zero) in the object file.
99fc7117aeSGreg Clayton
100fc7117aeSGreg Clayton // Determine the slide amount (if any)
101fc7117aeSGreg Clayton const size_t num_sections = section_list->GetSize();
102fc7117aeSGreg Clayton size_t sect_idx = 0;
103b9c1b51eSKate Stone for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
10405097246SAdrian Prantl // Iterate through the object file sections to find the first
10505097246SAdrian Prantl // section that starts of file offset zero and that has bytes in
10605097246SAdrian Prantl // the file...
1077820bd1eSGreg Clayton SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
108b9c1b51eSKate Stone if (section_sp) {
10978a14128SJason Molenda // If this section already has a load address set in the target,
11078a14128SJason Molenda // don't re-set it to the file address. Something may have
11178a14128SJason Molenda // set it to a more correct value already.
11278a14128SJason Molenda if (m_process->GetTarget()
11378a14128SJason Molenda .GetSectionLoadList()
11478a14128SJason Molenda .GetSectionLoadAddress(section_sp) !=
11578a14128SJason Molenda LLDB_INVALID_ADDRESS) {
11678a14128SJason Molenda continue;
11778a14128SJason Molenda }
118b9c1b51eSKate Stone if (m_process->GetTarget().SetSectionLoadAddress(
119b9c1b51eSKate Stone section_sp, section_sp->GetFileAddress()))
120fc7117aeSGreg Clayton changed = true;
121fc7117aeSGreg Clayton }
122fc7117aeSGreg Clayton }
123fc7117aeSGreg Clayton }
124fc7117aeSGreg Clayton }
125fc7117aeSGreg Clayton
126fc7117aeSGreg Clayton if (changed)
127fc7117aeSGreg Clayton loaded_module_list.AppendIfNeeded(module_sp);
128fc7117aeSGreg Clayton }
129fc7117aeSGreg Clayton }
130fc7117aeSGreg Clayton
131fc7117aeSGreg Clayton m_process->GetTarget().ModulesDidLoad(loaded_module_list);
132fc7117aeSGreg Clayton }
133fc7117aeSGreg Clayton
134fc7117aeSGreg Clayton ThreadPlanSP
GetStepThroughTrampolinePlan(Thread & thread,bool stop_others)135b9c1b51eSKate Stone DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread,
136b9c1b51eSKate Stone bool stop_others) {
137fc7117aeSGreg Clayton return ThreadPlanSP();
138fc7117aeSGreg Clayton }
139fc7117aeSGreg Clayton
CanLoadImage()14097206d57SZachary Turner Status DynamicLoaderStatic::CanLoadImage() {
14197206d57SZachary Turner Status error;
142fc7117aeSGreg Clayton error.SetErrorString("can't load images on with a static debug session");
143fc7117aeSGreg Clayton return error;
144fc7117aeSGreg Clayton }
145fc7117aeSGreg Clayton
Initialize()146b9c1b51eSKate Stone void DynamicLoaderStatic::Initialize() {
147fc7117aeSGreg Clayton PluginManager::RegisterPlugin(GetPluginNameStatic(),
148b9c1b51eSKate Stone GetPluginDescriptionStatic(), CreateInstance);
149fc7117aeSGreg Clayton }
150fc7117aeSGreg Clayton
Terminate()151b9c1b51eSKate Stone void DynamicLoaderStatic::Terminate() {
152fc7117aeSGreg Clayton PluginManager::UnregisterPlugin(CreateInstance);
153fc7117aeSGreg Clayton }
154fc7117aeSGreg Clayton
GetPluginDescriptionStatic()155*6fa1b4ffSPavel Labath llvm::StringRef DynamicLoaderStatic::GetPluginDescriptionStatic() {
156b9c1b51eSKate Stone return "Dynamic loader plug-in that will load any images at the static "
157b9c1b51eSKate Stone "addresses contained in each image.";
158fc7117aeSGreg Clayton }
159