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