1cfca06d7SDimitry Andric //===-- DynamicLoaderStatic.cpp -------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Core/Module.h"
10f034231aSEd Maste #include "lldb/Core/PluginManager.h"
11f034231aSEd Maste #include "lldb/Core/Section.h"
12f034231aSEd Maste #include "lldb/Symbol/ObjectFile.h"
13344a3780SDimitry Andric #include "lldb/Target/SectionLoadList.h"
14f034231aSEd Maste #include "lldb/Target/Target.h"
15f034231aSEd Maste
16f034231aSEd Maste #include "DynamicLoaderStatic.h"
17f034231aSEd Maste
18f034231aSEd Maste using namespace lldb;
19f034231aSEd Maste using namespace lldb_private;
20f034231aSEd Maste
LLDB_PLUGIN_DEFINE(DynamicLoaderStatic)21cfca06d7SDimitry Andric LLDB_PLUGIN_DEFINE(DynamicLoaderStatic)
22cfca06d7SDimitry Andric
23f73363f1SDimitry Andric // Create an instance of this class. This function is filled into the plugin
24f73363f1SDimitry Andric // info class that gets handed out by the plugin factory and allows the lldb to
25f73363f1SDimitry Andric // instantiate an instance of this class.
2614f1b3e8SDimitry Andric DynamicLoader *DynamicLoaderStatic::CreateInstance(Process *process,
2714f1b3e8SDimitry Andric bool force) {
28f034231aSEd Maste bool create = force;
2914f1b3e8SDimitry Andric if (!create) {
3014f1b3e8SDimitry Andric const llvm::Triple &triple_ref =
3114f1b3e8SDimitry Andric process->GetTarget().GetArchitecture().GetTriple();
32f034231aSEd Maste const llvm::Triple::OSType os_type = triple_ref.getOS();
33cfca06d7SDimitry Andric const llvm::Triple::ArchType arch_type = triple_ref.getArch();
34cfca06d7SDimitry Andric if (os_type == llvm::Triple::UnknownOS) {
35cfca06d7SDimitry Andric // The WASM and Hexagon plugin check the ArchType rather than the OSType,
36cfca06d7SDimitry Andric // so explicitly reject those here.
37cfca06d7SDimitry Andric switch(arch_type) {
38cfca06d7SDimitry Andric case llvm::Triple::hexagon:
39cfca06d7SDimitry Andric case llvm::Triple::wasm32:
40cfca06d7SDimitry Andric case llvm::Triple::wasm64:
41cfca06d7SDimitry Andric break;
42cfca06d7SDimitry Andric default:
43f034231aSEd Maste create = true;
44f034231aSEd Maste }
45cfca06d7SDimitry Andric }
46cfca06d7SDimitry Andric }
47f034231aSEd Maste
4814f1b3e8SDimitry Andric if (!create) {
49f034231aSEd Maste Module *exe_module = process->GetTarget().GetExecutableModulePointer();
5014f1b3e8SDimitry Andric if (exe_module) {
51f034231aSEd Maste ObjectFile *object_file = exe_module->GetObjectFile();
5214f1b3e8SDimitry Andric if (object_file) {
53f034231aSEd Maste create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);
54f034231aSEd Maste }
55f034231aSEd Maste }
56f034231aSEd Maste }
57f034231aSEd Maste
58f034231aSEd Maste if (create)
59f034231aSEd Maste return new DynamicLoaderStatic(process);
605f29bb8aSDimitry Andric return nullptr;
61f034231aSEd Maste }
62f034231aSEd Maste
63f034231aSEd Maste // Constructor
DynamicLoaderStatic(Process * process)6414f1b3e8SDimitry Andric DynamicLoaderStatic::DynamicLoaderStatic(Process *process)
6514f1b3e8SDimitry Andric : DynamicLoader(process) {}
66f034231aSEd Maste
67f034231aSEd Maste /// Called after attaching a process.
68f034231aSEd Maste ///
69f034231aSEd Maste /// Allow DynamicLoader plug-ins to execute some code after
70f034231aSEd Maste /// attaching to a process.
DidAttach()7114f1b3e8SDimitry Andric void DynamicLoaderStatic::DidAttach() { LoadAllImagesAtFileAddresses(); }
72f034231aSEd Maste
73f034231aSEd Maste /// Called after attaching a process.
74f034231aSEd Maste ///
75f034231aSEd Maste /// Allow DynamicLoader plug-ins to execute some code after
76f034231aSEd Maste /// attaching to a process.
DidLaunch()7714f1b3e8SDimitry Andric void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); }
78f034231aSEd Maste
LoadAllImagesAtFileAddresses()7914f1b3e8SDimitry Andric void DynamicLoaderStatic::LoadAllImagesAtFileAddresses() {
80f034231aSEd Maste const ModuleList &module_list = m_process->GetTarget().GetImages();
81f034231aSEd Maste
82f034231aSEd Maste ModuleList loaded_module_list;
83f034231aSEd Maste
84f034231aSEd Maste // Disable JIT for static dynamic loader targets
85f034231aSEd Maste m_process->SetCanJIT(false);
86f034231aSEd Maste
87ac9a064cSDimitry Andric Target &target = m_process->GetTarget();
88b60736ecSDimitry Andric for (ModuleSP module_sp : module_list.Modules()) {
8914f1b3e8SDimitry Andric if (module_sp) {
90f034231aSEd Maste bool changed = false;
91ac9a064cSDimitry Andric bool no_load_addresses = true;
92ac9a064cSDimitry Andric // If this module has a section with a load address set in
93ac9a064cSDimitry Andric // the target, assume all necessary work is already done. There
94ac9a064cSDimitry Andric // may be sections without a load address set intentionally
95ac9a064cSDimitry Andric // and we don't want to mutate that.
96ac9a064cSDimitry Andric // For a module with no load addresses set, set the load addresses
97ac9a064cSDimitry Andric // to slide == 0, the same as the file addresses, in the target.
98f034231aSEd Maste ObjectFile *image_object_file = module_sp->GetObjectFile();
9914f1b3e8SDimitry Andric if (image_object_file) {
100f034231aSEd Maste SectionList *section_list = image_object_file->GetSectionList();
10114f1b3e8SDimitry Andric if (section_list) {
102f034231aSEd Maste const size_t num_sections = section_list->GetSize();
103ac9a064cSDimitry Andric for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
104f034231aSEd Maste SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
10514f1b3e8SDimitry Andric if (section_sp) {
106ac9a064cSDimitry Andric if (target.GetSectionLoadList().GetSectionLoadAddress(
107ac9a064cSDimitry Andric section_sp) != LLDB_INVALID_ADDRESS) {
108ac9a064cSDimitry Andric no_load_addresses = false;
109ac9a064cSDimitry Andric break;
110f034231aSEd Maste }
111f034231aSEd Maste }
112f034231aSEd Maste }
113f034231aSEd Maste }
114ac9a064cSDimitry Andric }
115ac9a064cSDimitry Andric if (no_load_addresses)
116ac9a064cSDimitry Andric module_sp->SetLoadAddress(target, 0, true /*value_is_offset*/, changed);
117f034231aSEd Maste
118f034231aSEd Maste if (changed)
119f034231aSEd Maste loaded_module_list.AppendIfNeeded(module_sp);
120f034231aSEd Maste }
121f034231aSEd Maste }
122f034231aSEd Maste
123ac9a064cSDimitry Andric target.ModulesDidLoad(loaded_module_list);
124f034231aSEd Maste }
125f034231aSEd Maste
126f034231aSEd Maste ThreadPlanSP
GetStepThroughTrampolinePlan(Thread & thread,bool stop_others)12714f1b3e8SDimitry Andric DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread,
12814f1b3e8SDimitry Andric bool stop_others) {
129f034231aSEd Maste return ThreadPlanSP();
130f034231aSEd Maste }
131f034231aSEd Maste
CanLoadImage()132b76161e4SDimitry Andric Status DynamicLoaderStatic::CanLoadImage() {
133b76161e4SDimitry Andric Status error;
134f034231aSEd Maste error.SetErrorString("can't load images on with a static debug session");
135f034231aSEd Maste return error;
136f034231aSEd Maste }
137f034231aSEd Maste
Initialize()13814f1b3e8SDimitry Andric void DynamicLoaderStatic::Initialize() {
139f034231aSEd Maste PluginManager::RegisterPlugin(GetPluginNameStatic(),
14014f1b3e8SDimitry Andric GetPluginDescriptionStatic(), CreateInstance);
141f034231aSEd Maste }
142f034231aSEd Maste
Terminate()14314f1b3e8SDimitry Andric void DynamicLoaderStatic::Terminate() {
144f034231aSEd Maste PluginManager::UnregisterPlugin(CreateInstance);
145f034231aSEd Maste }
146f034231aSEd Maste
GetPluginDescriptionStatic()147c0981da4SDimitry Andric llvm::StringRef DynamicLoaderStatic::GetPluginDescriptionStatic() {
14814f1b3e8SDimitry Andric return "Dynamic loader plug-in that will load any images at the static "
14914f1b3e8SDimitry Andric "addresses contained in each image.";
150f034231aSEd Maste }
151