LLDB  mainline
DynamicLoaderStatic.cpp
Go to the documentation of this file.
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"
11 #include "lldb/Core/Section.h"
12 #include "lldb/Symbol/ObjectFile.h"
14 #include "lldb/Target/Target.h"
15 
16 #include "DynamicLoaderStatic.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
22 
23 // Create an instance of this class. This function is filled into the plugin
24 // info class that gets handed out by the plugin factory and allows the lldb to
25 // instantiate an instance of this class.
26 DynamicLoader *DynamicLoaderStatic::CreateInstance(Process *process,
27  bool force) {
28  bool create = force;
29  if (!create) {
30  const llvm::Triple &triple_ref =
31  process->GetTarget().GetArchitecture().GetTriple();
32  const llvm::Triple::OSType os_type = triple_ref.getOS();
33  const llvm::Triple::ArchType arch_type = triple_ref.getArch();
34  if (os_type == llvm::Triple::UnknownOS) {
35  // The WASM and Hexagon plugin check the ArchType rather than the OSType,
36  // so explicitly reject those here.
37  switch(arch_type) {
38  case llvm::Triple::hexagon:
39  case llvm::Triple::wasm32:
40  case llvm::Triple::wasm64:
41  break;
42  default:
43  create = true;
44  }
45  }
46  }
47 
48  if (!create) {
49  Module *exe_module = process->GetTarget().GetExecutableModulePointer();
50  if (exe_module) {
51  ObjectFile *object_file = exe_module->GetObjectFile();
52  if (object_file) {
53  create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);
54  }
55  }
56  }
57 
58  if (create)
59  return new DynamicLoaderStatic(process);
60  return nullptr;
61 }
62 
63 // Constructor
65  : DynamicLoader(process) {}
66 
67 /// Called after attaching a process.
68 ///
69 /// Allow DynamicLoader plug-ins to execute some code after
70 /// attaching to a process.
72 
73 /// Called after attaching a process.
74 ///
75 /// Allow DynamicLoader plug-ins to execute some code after
76 /// attaching to a process.
78 
80  const ModuleList &module_list = m_process->GetTarget().GetImages();
81 
82  ModuleList loaded_module_list;
83 
84  // Disable JIT for static dynamic loader targets
85  m_process->SetCanJIT(false);
86 
87  for (ModuleSP module_sp : module_list.Modules()) {
88  if (module_sp) {
89  bool changed = false;
90  ObjectFile *image_object_file = module_sp->GetObjectFile();
91  if (image_object_file) {
92  SectionList *section_list = image_object_file->GetSectionList();
93  if (section_list) {
94  // All sections listed in the dyld image info structure will all
95  // either be fixed up already, or they will all be off by a single
96  // slide amount that is determined by finding the first segment that
97  // is at file offset zero which also has bytes (a file size that is
98  // greater than zero) in the object file.
99 
100  // Determine the slide amount (if any)
101  const size_t num_sections = section_list->GetSize();
102  size_t sect_idx = 0;
103  for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
104  // Iterate through the object file sections to find the first
105  // section that starts of file offset zero and that has bytes in
106  // the file...
107  SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
108  if (section_sp) {
109  // If this section already has a load address set in the target,
110  // don't re-set it to the file address. Something may have
111  // set it to a more correct value already.
112  if (m_process->GetTarget()
114  .GetSectionLoadAddress(section_sp) !=
116  continue;
117  }
119  section_sp, section_sp->GetFileAddress()))
120  changed = true;
121  }
122  }
123  }
124  }
125 
126  if (changed)
127  loaded_module_list.AppendIfNeeded(module_sp);
128  }
129  }
130 
131  m_process->GetTarget().ModulesDidLoad(loaded_module_list);
132 }
133 
134 ThreadPlanSP
136  bool stop_others) {
137  return ThreadPlanSP();
138 }
139 
141  Status error;
142  error.SetErrorString("can't load images on with a static debug session");
143  return error;
144 }
145 
147  PluginManager::RegisterPlugin(GetPluginNameStatic(),
149 }
150 
152  PluginManager::UnregisterPlugin(CreateInstance);
153 }
154 
156  static ConstString g_name("static");
157  return g_name;
158 }
159 
161  return "Dynamic loader plug-in that will load any images at the static "
162  "addresses contained in each image.";
163 }
164 
165 // PluginInterface protocol
167  return GetPluginNameStatic();
168 }
169 
lldb_private::Module::GetObjectFile
virtual ObjectFile * GetObjectFile()
Get the object file representation for the current architecture.
Definition: Module.cpp:1238
lldb_private::ModuleList::Modules
ModuleIterable Modules() const
Definition: ModuleList.h:480
DynamicLoaderStatic::LoadAllImagesAtFileAddresses
void LoadAllImagesAtFileAddresses()
Definition: DynamicLoaderStatic.cpp:79
lldb_private::SectionList::GetSize
size_t GetSize() const
Definition: Section.h:74
lldb_private::Process
Definition: Process.h:341
Module.h
lldb_private::Process::GetTarget
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1194
lldb_private::Module
Definition: Module.h:75
SectionLoadList.h
lldb_private::SectionList
Definition: Section.h:34
DynamicLoaderStatic.h
DynamicLoaderStatic::CanLoadImage
lldb_private::Status CanLoadImage() override
Ask if it is ok to try and load or unload an shared library (image).
Definition: DynamicLoaderStatic.cpp:140
Section.h
lldb_private::Target::GetImages
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:919
Target.h
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::ObjectFile::GetSectionList
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
Definition: ObjectFile.cpp:583
lldb_private::SectionLoadList::GetSectionLoadAddress
lldb::addr_t GetSectionLoadAddress(const lldb::SectionSP &section_sp) const
Definition: SectionLoadList.cpp:49
DynamicLoaderStatic::DidLaunch
void DidLaunch() override
Called after attaching a process.
Definition: DynamicLoaderStatic.cpp:77
lldb_private::ModuleList
Definition: ModuleList.h:71
lldb_private::ModuleList::AppendIfNeeded
bool AppendIfNeeded(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list, if it is not already there.
lldb_private::Target::SetSectionLoadAddress
bool SetSectionLoadAddress(const lldb::SectionSP &section, lldb::addr_t load_addr, bool warn_multiple=false)
Definition: Target.cpp:2837
lldb_private::Thread
Definition: Thread.h:62
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::SectionList::GetSectionAtIndex
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition: Section.cpp:476
lldb_private::DynamicLoader
Definition: DynamicLoader.h:52
lldb_private::DynamicLoader::m_process
Process * m_process
The process that this dynamic loader plug-in is tracking.
Definition: DynamicLoader.h:312
DynamicLoaderStatic::DidAttach
void DidAttach() override
Called after attaching a process.
Definition: DynamicLoaderStatic.cpp:71
DynamicLoaderStatic::GetStepThroughTrampolinePlan
lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread, bool stop_others) override
Provides a plan to step through the dynamic loader trampoline for the current state of thread.
Definition: DynamicLoaderStatic.cpp:135
ObjectFile.h
DynamicLoaderStatic::GetPluginNameStatic
static lldb_private::ConstString GetPluginNameStatic()
Definition: DynamicLoaderStatic.cpp:155
lldb_private::Status
Definition: Status.h:44
DynamicLoaderStatic::CreateInstance
static lldb_private::DynamicLoader * CreateInstance(lldb_private::Process *process, bool force)
Definition: DynamicLoaderStatic.cpp:26
uint32_t
lldb_private::Target::ModulesDidLoad
void ModulesDidLoad(ModuleList &module_list)
Definition: Target.cpp:1613
lldb_private::ObjectFile::GetStrata
Strata GetStrata()
Definition: ObjectFile.h:621
PluginManager.h
lldb_private::Target::GetSectionLoadList
SectionLoadList & GetSectionLoadList()
Definition: Target.h:1034
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
DynamicLoaderStatic
Definition: DynamicLoaderStatic.h:17
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
DynamicLoaderStatic::GetPluginName
lldb_private::ConstString GetPluginName() override
Definition: DynamicLoaderStatic.cpp:166
DynamicLoaderStatic::Initialize
static void Initialize()
Definition: DynamicLoaderStatic.cpp:146
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
DynamicLoaderStatic::DynamicLoaderStatic
DynamicLoaderStatic(lldb_private::Process *process)
Definition: DynamicLoaderStatic.cpp:64
lldb_private::Process::SetCanJIT
void SetCanJIT(bool can_jit)
Sets whether executing JIT-compiled code in this process is possible.
Definition: Process.cpp:2340
DynamicLoaderStatic::GetPluginDescriptionStatic
static const char * GetPluginDescriptionStatic()
Definition: DynamicLoaderStatic.cpp:160
DynamicLoaderStatic::Terminate
static void Terminate()
Definition: DynamicLoaderStatic.cpp:151
lldb
Definition: SBAddress.h:15
DynamicLoaderStatic::GetPluginVersion
uint32_t GetPluginVersion() override
Definition: DynamicLoaderStatic.cpp:170
lldb_private::ObjectFile
Definition: ObjectFile.h:58