LLDB  mainline
DynamicLoader.cpp
Go to the documentation of this file.
1 //===-- DynamicLoader.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 
10 
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/ModuleList.h"
13 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/Target.h"
22 
23 #include "llvm/ADT/StringRef.h"
24 
25 #include <memory>
26 
27 #include <assert.h>
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 DynamicLoader *DynamicLoader::FindPlugin(Process *process,
33  const char *plugin_name) {
34  DynamicLoaderCreateInstance create_callback = nullptr;
35  if (plugin_name) {
36  ConstString const_plugin_name(plugin_name);
37  create_callback =
38  PluginManager::GetDynamicLoaderCreateCallbackForPluginName(
39  const_plugin_name);
40  if (create_callback) {
41  std::unique_ptr<DynamicLoader> instance_up(
42  create_callback(process, true));
43  if (instance_up)
44  return instance_up.release();
45  }
46  } else {
47  for (uint32_t idx = 0;
48  (create_callback =
49  PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) !=
50  nullptr;
51  ++idx) {
52  std::unique_ptr<DynamicLoader> instance_up(
53  create_callback(process, false));
54  if (instance_up)
55  return instance_up.release();
56  }
57  }
58  return nullptr;
59 }
60 
61 DynamicLoader::DynamicLoader(Process *process) : m_process(process) {}
62 
64 
65 // Accessosors to the global setting as to whether to stop at image (shared
66 // library) loading/unloading.
67 
70 }
71 
74 }
75 
77  Target &target = m_process->GetTarget();
78  ModuleSP executable = target.GetExecutableModule();
79 
80  if (executable) {
81  if (FileSystem::Instance().Exists(executable->GetFileSpec())) {
82  ModuleSpec module_spec(executable->GetFileSpec(),
83  executable->GetArchitecture());
84  auto module_sp = std::make_shared<Module>(module_spec);
85 
86  // Check if the executable has changed and set it to the target
87  // executable if they differ.
88  if (module_sp && module_sp->GetUUID().IsValid() &&
89  executable->GetUUID().IsValid()) {
90  if (module_sp->GetUUID() != executable->GetUUID())
91  executable.reset();
92  } else if (executable->FileHasChanged()) {
93  executable.reset();
94  }
95 
96  if (!executable) {
97  executable = target.GetOrCreateModule(module_spec, true /* notify */);
98  if (executable.get() != target.GetExecutableModulePointer()) {
99  // Don't load dependent images since we are in dyld where we will
100  // know and find out about all images that are loaded
101  target.SetExecutableModule(executable, eLoadDependentsNo);
102  }
103  }
104  }
105  }
106  return executable;
107 }
108 
109 void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr,
110  addr_t base_addr,
111  bool base_addr_is_offset) {
112  UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset);
113 }
114 
116  addr_t base_addr,
117  bool base_addr_is_offset) {
118  bool changed;
119  module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset,
120  changed);
121 }
122 
123 void DynamicLoader::UnloadSections(const ModuleSP module) {
124  UnloadSectionsCommon(module);
125 }
126 
127 void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) {
128  Target &target = m_process->GetTarget();
129  const SectionList *sections = GetSectionListFromModule(module);
130 
131  assert(sections && "SectionList missing from unloaded module.");
132 
133  const size_t num_sections = sections->GetSize();
134  for (size_t i = 0; i < num_sections; ++i) {
135  SectionSP section_sp(sections->GetSectionAtIndex(i));
136  target.SetSectionUnloaded(section_sp);
137  }
138 }
139 
140 const SectionList *
141 DynamicLoader::GetSectionListFromModule(const ModuleSP module) const {
142  SectionList *sections = nullptr;
143  if (module) {
144  ObjectFile *obj_file = module->GetObjectFile();
145  if (obj_file != nullptr) {
146  sections = obj_file->GetSectionList();
147  }
148  }
149  return sections;
150 }
151 
153  addr_t link_map_addr,
154  addr_t base_addr,
155  bool base_addr_is_offset) {
156  Target &target = m_process->GetTarget();
157  ModuleList &modules = target.GetImages();
158  ModuleSpec module_spec(file, target.GetArchitecture());
159  ModuleSP module_sp;
160 
161  if ((module_sp = modules.FindFirstModule(module_spec))) {
162  UpdateLoadedSections(module_sp, link_map_addr, base_addr,
163  base_addr_is_offset);
164  return module_sp;
165  }
166 
167  if ((module_sp = target.GetOrCreateModule(module_spec,
168  true /* notify */))) {
169  UpdateLoadedSections(module_sp, link_map_addr, base_addr,
170  base_addr_is_offset);
171  return module_sp;
172  }
173 
174  bool check_alternative_file_name = true;
175  if (base_addr_is_offset) {
176  // Try to fetch the load address of the file from the process as we need
177  // absolute load address to read the file out of the memory instead of a
178  // load bias.
179  bool is_loaded = false;
180  lldb::addr_t load_addr;
181  Status error = m_process->GetFileLoadAddress(file, is_loaded, load_addr);
182  if (error.Success() && is_loaded) {
183  check_alternative_file_name = false;
184  base_addr = load_addr;
185  }
186  }
187 
188  // We failed to find the module based on its name. Lets try to check if we
189  // can find a different name based on the memory region info.
190  if (check_alternative_file_name) {
191  MemoryRegionInfo memory_info;
192  Status error = m_process->GetMemoryRegionInfo(base_addr, memory_info);
193  if (error.Success() && memory_info.GetMapped() &&
194  memory_info.GetRange().GetRangeBase() == base_addr &&
195  !(memory_info.GetName().IsEmpty())) {
196  ModuleSpec new_module_spec(FileSpec(memory_info.GetName().AsCString()),
197  target.GetArchitecture());
198 
199  if ((module_sp = modules.FindFirstModule(new_module_spec))) {
200  UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
201  return module_sp;
202  }
203 
204  if ((module_sp = target.GetOrCreateModule(new_module_spec,
205  true /* notify */))) {
206  UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
207  return module_sp;
208  }
209  }
210  }
211 
212  if ((module_sp = m_process->ReadModuleFromMemory(file, base_addr))) {
213  UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
214  target.GetImages().AppendIfNeeded(module_sp);
215  }
216 
217  return module_sp;
218 }
219 
221  int size_in_bytes) {
222  Status error;
223  uint64_t value =
224  m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error);
225  if (error.Fail())
226  return -1;
227  else
228  return (int64_t)value;
229 }
230 
232  Status error;
233  addr_t value = m_process->ReadPointerFromMemory(addr, error);
234  if (error.Fail())
235  return LLDB_INVALID_ADDRESS;
236  else
237  return value;
238 }
239 
241 {
242  if (m_process)
244 }
245 
OptionalBool GetMapped() const
void LoadOperatingSystemPlugin(bool flush)
Definition: Process.cpp:2513
virtual Status GetFileLoadAddress(const FileSpec &file, bool &is_loaded, lldb::addr_t &load_addr)
Try to find the load address of a file.
Definition: Process.h:2313
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
const ArchSpec & GetArchitecture() const
Definition: Target.h:941
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:224
virtual lldb::ModuleSP LoadModuleAtAddress(const lldb_private::FileSpec &file, lldb::addr_t link_map_addr, lldb::addr_t base_addr, bool base_addr_is_offset)
Locates or creates a module given by file and updates/loads the resulting module at the virtual base ...
~DynamicLoader() override
Destructor.
A file utility class.
Definition: FileSpec.h:55
bool GetStopWhenImagesChange() const
Get whether the process should stop when images change.
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:58
virtual void UnloadSections(const lldb::ModuleSP module)
Removes the loaded sections from the target in module.
Module * GetExecutableModulePointer()
Definition: Target.cpp:1394
static FileSystem & Instance()
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
lldb::addr_t ReadPointer(lldb::addr_t addr)
lldb::ModuleSP ReadModuleFromMemory(const FileSpec &file_spec, lldb::addr_t header_addr, size_t size_to_read=512)
Definition: Process.cpp:2435
A collection class for Module objects.
Definition: ModuleList.h:91
Process * m_process
The process that this dynamic loader plug-in is tracking.
const lldb_private::SectionList * GetSectionListFromModule(const lldb::ModuleSP module) const
int64_t ReadUnsignedIntWithSizeInBytes(lldb::addr_t addr, int size_in_bytes)
lldb::ModuleSP FindFirstModule(const ModuleSpec &module_spec) const
Definition: ModuleList.cpp:599
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:605
lldb::ModuleSP GetOrCreateModule(const ModuleSpec &module_spec, bool notify, Status *error_ptr=nullptr)
Find a binary on the system and return its Module, or return an existing Module that is already in th...
Definition: Target.cpp:1999
bool GetStopOnSharedLibraryEvents() const
Definition: Process.cpp:253
lldb::ModuleSP GetTargetExecutable()
Checks to see if the target module has changed, updates the target accordingly and returns the target...
A plug-in interface definition class for debugging a process.
Definition: Process.h:353
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition: Section.cpp:497
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1380
bool Success() const
Test for success condition.
Definition: Status.cpp:287
void SetStopOnSharedLibraryEvents(bool stop)
Definition: Process.cpp:259
A plug-in interface definition class for dynamic loaders.
Definition: DynamicLoader.h:64
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:899
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition: Process.cpp:2172
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1194
uint64_t addr_t
Definition: lldb-types.h:83
bool SetSectionUnloaded(const lldb::SectionSP &section_sp)
Definition: Target.cpp:2805
A uniqued constant string class.
Definition: ConstString.h:38
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
virtual Status GetMemoryRegionInfo(lldb::addr_t load_addr, MemoryRegionInfo &range_info)
Locate the memory region that contains load_addr.
Definition: Process.h:1759
void UnloadSectionsCommon(const lldb::ModuleSP module)
void LoadOperatingSystemPlugin(bool flush)
Definition: SBAddress.h:15
size_t GetSize() const
Definition: Section.h:83
void SetStopWhenImagesChange(bool stop)
Set whether the process should stop when images change.
void UpdateLoadedSectionsCommon(lldb::ModuleSP module, lldb::addr_t base_addr, bool base_addr_is_offset)
uint64_t ReadUnsignedIntegerFromMemory(lldb::addr_t load_addr, size_t byte_size, uint64_t fail_value, Status &error)
Reads an unsigned integer of the specified byte size from process memory.
Definition: Process.cpp:2150
bool AppendIfNeeded(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list, if it is not already there.
void SetExecutableModule(lldb::ModuleSP &module_sp, LoadDependentFiles load_dependent_files=eLoadDependentsDefault)
Set the main executable module.
Definition: Target.cpp:1431
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:340
An error handling class.
Definition: Status.h:44
BaseType GetRangeBase() const
Definition: RangeMap.h:46
virtual void UpdateLoadedSections(lldb::ModuleSP module, lldb::addr_t link_map_addr, lldb::addr_t base_addr, bool base_addr_is_offset)
Updates the load address of every allocatable section in module.