• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
LLDB_PLUGIN_DEFINE(DynamicLoaderStatic)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
DynamicLoaderStatic(Process * process)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.
DidAttach()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.
DidLaunch()76 void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); }
77 
LoadAllImagesAtFileAddresses()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   std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
87 
88   const size_t num_modules = module_list.GetSize();
89   for (uint32_t idx = 0; idx < num_modules; ++idx) {
90     ModuleSP module_sp(module_list.GetModuleAtIndexUnlocked(idx));
91     if (module_sp) {
92       bool changed = false;
93       ObjectFile *image_object_file = module_sp->GetObjectFile();
94       if (image_object_file) {
95         SectionList *section_list = image_object_file->GetSectionList();
96         if (section_list) {
97           // All sections listed in the dyld image info structure will all
98           // either be fixed up already, or they will all be off by a single
99           // slide amount that is determined by finding the first segment that
100           // is at file offset zero which also has bytes (a file size that is
101           // greater than zero) in the object file.
102 
103           // Determine the slide amount (if any)
104           const size_t num_sections = section_list->GetSize();
105           size_t sect_idx = 0;
106           for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
107             // Iterate through the object file sections to find the first
108             // section that starts of file offset zero and that has bytes in
109             // the file...
110             SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
111             if (section_sp) {
112               if (m_process->GetTarget().SetSectionLoadAddress(
113                       section_sp, section_sp->GetFileAddress()))
114                 changed = true;
115             }
116           }
117         }
118       }
119 
120       if (changed)
121         loaded_module_list.AppendIfNeeded(module_sp);
122     }
123   }
124 
125   m_process->GetTarget().ModulesDidLoad(loaded_module_list);
126 }
127 
128 ThreadPlanSP
GetStepThroughTrampolinePlan(Thread & thread,bool stop_others)129 DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread,
130                                                   bool stop_others) {
131   return ThreadPlanSP();
132 }
133 
CanLoadImage()134 Status DynamicLoaderStatic::CanLoadImage() {
135   Status error;
136   error.SetErrorString("can't load images on with a static debug session");
137   return error;
138 }
139 
Initialize()140 void DynamicLoaderStatic::Initialize() {
141   PluginManager::RegisterPlugin(GetPluginNameStatic(),
142                                 GetPluginDescriptionStatic(), CreateInstance);
143 }
144 
Terminate()145 void DynamicLoaderStatic::Terminate() {
146   PluginManager::UnregisterPlugin(CreateInstance);
147 }
148 
GetPluginNameStatic()149 lldb_private::ConstString DynamicLoaderStatic::GetPluginNameStatic() {
150   static ConstString g_name("static");
151   return g_name;
152 }
153 
GetPluginDescriptionStatic()154 const char *DynamicLoaderStatic::GetPluginDescriptionStatic() {
155   return "Dynamic loader plug-in that will load any images at the static "
156          "addresses contained in each image.";
157 }
158 
159 // PluginInterface protocol
GetPluginName()160 lldb_private::ConstString DynamicLoaderStatic::GetPluginName() {
161   return GetPluginNameStatic();
162 }
163 
GetPluginVersion()164 uint32_t DynamicLoaderStatic::GetPluginVersion() { return 1; }
165