1 //===-- DynamicLoader.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/DynamicLoader.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleList.h" // for ModuleList
14 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/PluginManager.h"
16 #include "lldb/Core/Section.h"
17 #include "lldb/Symbol/ObjectFile.h" // for ObjectFile
18 #include "lldb/Target/MemoryRegionInfo.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/ConstString.h" // for ConstString
22 #include "lldb/lldb-private-interfaces.h" // for DynamicLoaderCreateInstance
24 #include "llvm/ADT/StringRef.h" // for StringRef
26 #include <memory> // for shared_ptr, unique_ptr
28 #include <assert.h> // for assert
31 using namespace lldb_private;
33 DynamicLoader *DynamicLoader::FindPlugin(Process *process,
34 const char *plugin_name) {
35 DynamicLoaderCreateInstance create_callback = nullptr;
37 ConstString const_plugin_name(plugin_name);
39 PluginManager::GetDynamicLoaderCreateCallbackForPluginName(
41 if (create_callback) {
42 std::unique_ptr<DynamicLoader> instance_ap(
43 create_callback(process, true));
45 return instance_ap.release();
48 for (uint32_t idx = 0;
50 PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) !=
53 std::unique_ptr<DynamicLoader> instance_ap(
54 create_callback(process, false));
56 return instance_ap.release();
62 DynamicLoader::DynamicLoader(Process *process) : m_process(process) {}
64 DynamicLoader::~DynamicLoader() = default;
66 //----------------------------------------------------------------------
67 // Accessosors to the global setting as to whether to stop at image (shared
68 // library) loading/unloading.
69 //----------------------------------------------------------------------
71 bool DynamicLoader::GetStopWhenImagesChange() const {
72 return m_process->GetStopOnSharedLibraryEvents();
75 void DynamicLoader::SetStopWhenImagesChange(bool stop) {
76 m_process->SetStopOnSharedLibraryEvents(stop);
79 ModuleSP DynamicLoader::GetTargetExecutable() {
80 Target &target = m_process->GetTarget();
81 ModuleSP executable = target.GetExecutableModule();
84 if (executable->GetFileSpec().Exists()) {
85 ModuleSpec module_spec(executable->GetFileSpec(),
86 executable->GetArchitecture());
87 auto module_sp = std::make_shared<Module>(module_spec);
89 // Check if the executable has changed and set it to the target
90 // executable if they differ.
91 if (module_sp && module_sp->GetUUID().IsValid() &&
92 executable->GetUUID().IsValid()) {
93 if (module_sp->GetUUID() != executable->GetUUID())
95 } else if (executable->FileHasChanged()) {
100 executable = target.GetSharedModule(module_spec);
101 if (executable.get() != target.GetExecutableModulePointer()) {
102 // Don't load dependent images since we are in dyld where we will
103 // know and find out about all images that are loaded
104 const bool get_dependent_images = false;
105 target.SetExecutableModule(executable, get_dependent_images);
113 void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr,
115 bool base_addr_is_offset) {
116 UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset);
119 void DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module,
121 bool base_addr_is_offset) {
123 module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset,
127 void DynamicLoader::UnloadSections(const ModuleSP module) {
128 UnloadSectionsCommon(module);
131 void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) {
132 Target &target = m_process->GetTarget();
133 const SectionList *sections = GetSectionListFromModule(module);
135 assert(sections && "SectionList missing from unloaded module.");
137 const size_t num_sections = sections->GetSize();
138 for (size_t i = 0; i < num_sections; ++i) {
139 SectionSP section_sp(sections->GetSectionAtIndex(i));
140 target.SetSectionUnloaded(section_sp);
145 DynamicLoader::GetSectionListFromModule(const ModuleSP module) const {
146 SectionList *sections = nullptr;
148 ObjectFile *obj_file = module->GetObjectFile();
149 if (obj_file != nullptr) {
150 sections = obj_file->GetSectionList();
156 ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file,
157 addr_t link_map_addr,
159 bool base_addr_is_offset) {
160 Target &target = m_process->GetTarget();
161 ModuleList &modules = target.GetImages();
162 ModuleSpec module_spec(file, target.GetArchitecture());
165 if ((module_sp = modules.FindFirstModule(module_spec))) {
166 UpdateLoadedSections(module_sp, link_map_addr, base_addr,
167 base_addr_is_offset);
171 if ((module_sp = target.GetSharedModule(module_spec))) {
172 UpdateLoadedSections(module_sp, link_map_addr, base_addr,
173 base_addr_is_offset);
177 bool check_alternative_file_name = true;
178 if (base_addr_is_offset) {
179 // Try to fetch the load address of the file from the process as we need
180 // absolute load address to read the file out of the memory instead of a
182 bool is_loaded = false;
183 lldb::addr_t load_addr;
184 Status error = m_process->GetFileLoadAddress(file, is_loaded, load_addr);
185 if (error.Success() && is_loaded) {
186 check_alternative_file_name = false;
187 base_addr = load_addr;
191 // We failed to find the module based on its name. Lets try to check if we
192 // can find a different name based on the memory region info.
193 if (check_alternative_file_name) {
194 MemoryRegionInfo memory_info;
195 Status error = m_process->GetMemoryRegionInfo(base_addr, memory_info);
196 if (error.Success() && memory_info.GetMapped() &&
197 memory_info.GetRange().GetRangeBase() == base_addr &&
198 !(memory_info.GetName().IsEmpty())) {
199 ModuleSpec new_module_spec(
200 FileSpec(memory_info.GetName().AsCString(), false),
201 target.GetArchitecture());
203 if ((module_sp = modules.FindFirstModule(new_module_spec))) {
204 UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
208 if ((module_sp = target.GetSharedModule(new_module_spec))) {
209 UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
215 if ((module_sp = m_process->ReadModuleFromMemory(file, base_addr))) {
216 UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
217 target.GetImages().AppendIfNeeded(module_sp);
223 int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr,
227 m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error);
231 return (int64_t)value;
234 addr_t DynamicLoader::ReadPointer(addr_t addr) {
236 addr_t value = m_process->ReadPointerFromMemory(addr, error);
238 return LLDB_INVALID_ADDRESS;
243 void DynamicLoader::LoadOperatingSystemPlugin(bool flush)
246 m_process->LoadOperatingSystemPlugin(flush);