xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/DynamicLoader/Static/DynamicLoaderStatic.cpp (revision 824adb5411e4389b29bae28eba5c2c2bbd147f34)
1 //===-- DynamicLoaderStatic.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Core/Module.h"
10 #include "lldb/Core/PluginManager.h"
11 #include "lldb/Core/Section.h"
12 #include "lldb/Symbol/ObjectFile.h"
13 #include "lldb/Target/Target.h"
14 
15 #include "DynamicLoaderStatic.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 LLDB_PLUGIN_DEFINE(DynamicLoaderStatic)
21 
22 // Create an instance of this class. This function is filled into the plugin
23 // info class that gets handed out by the plugin factory and allows the lldb to
24 // instantiate an instance of this class.
25 DynamicLoader *DynamicLoaderStatic::CreateInstance(Process *process,
26                                                    bool force) {
27   bool create = force;
28   if (!create) {
29     const llvm::Triple &triple_ref =
30         process->GetTarget().GetArchitecture().GetTriple();
31     const llvm::Triple::OSType os_type = triple_ref.getOS();
32     const llvm::Triple::ArchType arch_type = triple_ref.getArch();
33     if (os_type == llvm::Triple::UnknownOS) {
34       // The WASM and Hexagon plugin check the ArchType rather than the OSType,
35       // so explicitly reject those here.
36       switch(arch_type) {
37         case llvm::Triple::hexagon:
38         case llvm::Triple::wasm32:
39         case llvm::Triple::wasm64:
40           break;
41         default:
42           create = true;
43       }
44     }
45   }
46 
47   if (!create) {
48     Module *exe_module = process->GetTarget().GetExecutableModulePointer();
49     if (exe_module) {
50       ObjectFile *object_file = exe_module->GetObjectFile();
51       if (object_file) {
52         create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);
53       }
54     }
55   }
56 
57   if (create)
58     return new DynamicLoaderStatic(process);
59   return nullptr;
60 }
61 
62 // Constructor
63 DynamicLoaderStatic::DynamicLoaderStatic(Process *process)
64     : DynamicLoader(process) {}
65 
66 // Destructor
67 DynamicLoaderStatic::~DynamicLoaderStatic() {}
68 
69 /// Called after attaching a process.
70 ///
71 /// Allow DynamicLoader plug-ins to execute some code after
72 /// attaching to a process.
73 void DynamicLoaderStatic::DidAttach() { LoadAllImagesAtFileAddresses(); }
74 
75 /// Called after attaching a process.
76 ///
77 /// Allow DynamicLoader plug-ins to execute some code after
78 /// attaching to a process.
79 void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); }
80 
81 void DynamicLoaderStatic::LoadAllImagesAtFileAddresses() {
82   const ModuleList &module_list = m_process->GetTarget().GetImages();
83 
84   ModuleList loaded_module_list;
85 
86   // Disable JIT for static dynamic loader targets
87   m_process->SetCanJIT(false);
88 
89   std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
90 
91   const size_t num_modules = module_list.GetSize();
92   for (uint32_t idx = 0; idx < num_modules; ++idx) {
93     ModuleSP module_sp(module_list.GetModuleAtIndexUnlocked(idx));
94     if (module_sp) {
95       bool changed = false;
96       ObjectFile *image_object_file = module_sp->GetObjectFile();
97       if (image_object_file) {
98         SectionList *section_list = image_object_file->GetSectionList();
99         if (section_list) {
100           // All sections listed in the dyld image info structure will all
101           // either be fixed up already, or they will all be off by a single
102           // slide amount that is determined by finding the first segment that
103           // is at file offset zero which also has bytes (a file size that is
104           // greater than zero) in the object file.
105 
106           // Determine the slide amount (if any)
107           const size_t num_sections = section_list->GetSize();
108           size_t sect_idx = 0;
109           for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
110             // Iterate through the object file sections to find the first
111             // section that starts of file offset zero and that has bytes in
112             // the file...
113             SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
114             if (section_sp) {
115               if (m_process->GetTarget().SetSectionLoadAddress(
116                       section_sp, section_sp->GetFileAddress()))
117                 changed = true;
118             }
119           }
120         }
121       }
122 
123       if (changed)
124         loaded_module_list.AppendIfNeeded(module_sp);
125     }
126   }
127 
128   m_process->GetTarget().ModulesDidLoad(loaded_module_list);
129 }
130 
131 ThreadPlanSP
132 DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread,
133                                                   bool stop_others) {
134   return ThreadPlanSP();
135 }
136 
137 Status DynamicLoaderStatic::CanLoadImage() {
138   Status error;
139   error.SetErrorString("can't load images on with a static debug session");
140   return error;
141 }
142 
143 void DynamicLoaderStatic::Initialize() {
144   PluginManager::RegisterPlugin(GetPluginNameStatic(),
145                                 GetPluginDescriptionStatic(), CreateInstance);
146 }
147 
148 void DynamicLoaderStatic::Terminate() {
149   PluginManager::UnregisterPlugin(CreateInstance);
150 }
151 
152 lldb_private::ConstString DynamicLoaderStatic::GetPluginNameStatic() {
153   static ConstString g_name("static");
154   return g_name;
155 }
156 
157 const char *DynamicLoaderStatic::GetPluginDescriptionStatic() {
158   return "Dynamic loader plug-in that will load any images at the static "
159          "addresses contained in each image.";
160 }
161 
162 // PluginInterface protocol
163 lldb_private::ConstString DynamicLoaderStatic::GetPluginName() {
164   return GetPluginNameStatic();
165 }
166 
167 uint32_t DynamicLoaderStatic::GetPluginVersion() { return 1; }
168