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     if ((os_type == llvm::Triple::UnknownOS))
33       create = true;
34   }
35 
36   if (!create) {
37     Module *exe_module = process->GetTarget().GetExecutableModulePointer();
38     if (exe_module) {
39       ObjectFile *object_file = exe_module->GetObjectFile();
40       if (object_file) {
41         create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);
42       }
43     }
44   }
45 
46   if (create)
47     return new DynamicLoaderStatic(process);
48   return nullptr;
49 }
50 
51 // Constructor
52 DynamicLoaderStatic::DynamicLoaderStatic(Process *process)
53     : DynamicLoader(process) {}
54 
55 // Destructor
56 DynamicLoaderStatic::~DynamicLoaderStatic() {}
57 
58 /// Called after attaching a process.
59 ///
60 /// Allow DynamicLoader plug-ins to execute some code after
61 /// attaching to a process.
62 void DynamicLoaderStatic::DidAttach() { LoadAllImagesAtFileAddresses(); }
63 
64 /// Called after attaching a process.
65 ///
66 /// Allow DynamicLoader plug-ins to execute some code after
67 /// attaching to a process.
68 void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); }
69 
70 void DynamicLoaderStatic::LoadAllImagesAtFileAddresses() {
71   const ModuleList &module_list = m_process->GetTarget().GetImages();
72 
73   ModuleList loaded_module_list;
74 
75   // Disable JIT for static dynamic loader targets
76   m_process->SetCanJIT(false);
77 
78   std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
79 
80   const size_t num_modules = module_list.GetSize();
81   for (uint32_t idx = 0; idx < num_modules; ++idx) {
82     ModuleSP module_sp(module_list.GetModuleAtIndexUnlocked(idx));
83     if (module_sp) {
84       bool changed = false;
85       ObjectFile *image_object_file = module_sp->GetObjectFile();
86       if (image_object_file) {
87         SectionList *section_list = image_object_file->GetSectionList();
88         if (section_list) {
89           // All sections listed in the dyld image info structure will all
90           // either be fixed up already, or they will all be off by a single
91           // slide amount that is determined by finding the first segment that
92           // is at file offset zero which also has bytes (a file size that is
93           // greater than zero) in the object file.
94 
95           // Determine the slide amount (if any)
96           const size_t num_sections = section_list->GetSize();
97           size_t sect_idx = 0;
98           for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
99             // Iterate through the object file sections to find the first
100             // section that starts of file offset zero and that has bytes in
101             // the file...
102             SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
103             if (section_sp) {
104               if (m_process->GetTarget().SetSectionLoadAddress(
105                       section_sp, section_sp->GetFileAddress()))
106                 changed = true;
107             }
108           }
109         }
110       }
111 
112       if (changed)
113         loaded_module_list.AppendIfNeeded(module_sp);
114     }
115   }
116 
117   m_process->GetTarget().ModulesDidLoad(loaded_module_list);
118 }
119 
120 ThreadPlanSP
121 DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread,
122                                                   bool stop_others) {
123   return ThreadPlanSP();
124 }
125 
126 Status DynamicLoaderStatic::CanLoadImage() {
127   Status error;
128   error.SetErrorString("can't load images on with a static debug session");
129   return error;
130 }
131 
132 void DynamicLoaderStatic::Initialize() {
133   PluginManager::RegisterPlugin(GetPluginNameStatic(),
134                                 GetPluginDescriptionStatic(), CreateInstance);
135 }
136 
137 void DynamicLoaderStatic::Terminate() {
138   PluginManager::UnregisterPlugin(CreateInstance);
139 }
140 
141 lldb_private::ConstString DynamicLoaderStatic::GetPluginNameStatic() {
142   static ConstString g_name("static");
143   return g_name;
144 }
145 
146 const char *DynamicLoaderStatic::GetPluginDescriptionStatic() {
147   return "Dynamic loader plug-in that will load any images at the static "
148          "addresses contained in each image.";
149 }
150 
151 // PluginInterface protocol
152 lldb_private::ConstString DynamicLoaderStatic::GetPluginName() {
153   return GetPluginNameStatic();
154 }
155 
156 uint32_t DynamicLoaderStatic::GetPluginVersion() { return 1; }
157