xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/DynamicLoader/Static/DynamicLoaderStatic.cpp (revision 2eb4d8dc723da3cf7d735a3226ae49da4c8c5dbc)
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 /// Called after attaching a process.
67 ///
68 /// Allow DynamicLoader plug-ins to execute some code after
69 /// attaching to a process.
70 void DynamicLoaderStatic::DidAttach() { LoadAllImagesAtFileAddresses(); }
71 
72 /// Called after attaching a process.
73 ///
74 /// Allow DynamicLoader plug-ins to execute some code after
75 /// attaching to a process.
76 void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); }
77 
78 void DynamicLoaderStatic::LoadAllImagesAtFileAddresses() {
79   const ModuleList &module_list = m_process->GetTarget().GetImages();
80 
81   ModuleList loaded_module_list;
82 
83   // Disable JIT for static dynamic loader targets
84   m_process->SetCanJIT(false);
85 
86   for (ModuleSP module_sp : module_list.Modules()) {
87     if (module_sp) {
88       bool changed = false;
89       ObjectFile *image_object_file = module_sp->GetObjectFile();
90       if (image_object_file) {
91         SectionList *section_list = image_object_file->GetSectionList();
92         if (section_list) {
93           // All sections listed in the dyld image info structure will all
94           // either be fixed up already, or they will all be off by a single
95           // slide amount that is determined by finding the first segment that
96           // is at file offset zero which also has bytes (a file size that is
97           // greater than zero) in the object file.
98 
99           // Determine the slide amount (if any)
100           const size_t num_sections = section_list->GetSize();
101           size_t sect_idx = 0;
102           for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
103             // Iterate through the object file sections to find the first
104             // section that starts of file offset zero and that has bytes in
105             // the file...
106             SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
107             if (section_sp) {
108               if (m_process->GetTarget().SetSectionLoadAddress(
109                       section_sp, section_sp->GetFileAddress()))
110                 changed = true;
111             }
112           }
113         }
114       }
115 
116       if (changed)
117         loaded_module_list.AppendIfNeeded(module_sp);
118     }
119   }
120 
121   m_process->GetTarget().ModulesDidLoad(loaded_module_list);
122 }
123 
124 ThreadPlanSP
125 DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread,
126                                                   bool stop_others) {
127   return ThreadPlanSP();
128 }
129 
130 Status DynamicLoaderStatic::CanLoadImage() {
131   Status error;
132   error.SetErrorString("can't load images on with a static debug session");
133   return error;
134 }
135 
136 void DynamicLoaderStatic::Initialize() {
137   PluginManager::RegisterPlugin(GetPluginNameStatic(),
138                                 GetPluginDescriptionStatic(), CreateInstance);
139 }
140 
141 void DynamicLoaderStatic::Terminate() {
142   PluginManager::UnregisterPlugin(CreateInstance);
143 }
144 
145 lldb_private::ConstString DynamicLoaderStatic::GetPluginNameStatic() {
146   static ConstString g_name("static");
147   return g_name;
148 }
149 
150 const char *DynamicLoaderStatic::GetPluginDescriptionStatic() {
151   return "Dynamic loader plug-in that will load any images at the static "
152          "addresses contained in each image.";
153 }
154 
155 // PluginInterface protocol
156 lldb_private::ConstString DynamicLoaderStatic::GetPluginName() {
157   return GetPluginNameStatic();
158 }
159 
160 uint32_t DynamicLoaderStatic::GetPluginVersion() { return 1; }
161