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 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Target/DynamicLoader.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Core/Section.h"
19 #include "lldb/Target/MemoryRegionInfo.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/lldb-private.h"
25 using namespace lldb_private;
27 DynamicLoader *DynamicLoader::FindPlugin(Process *process,
28 const char *plugin_name) {
29 DynamicLoaderCreateInstance create_callback = nullptr;
31 ConstString const_plugin_name(plugin_name);
33 PluginManager::GetDynamicLoaderCreateCallbackForPluginName(
35 if (create_callback) {
36 std::unique_ptr<DynamicLoader> instance_ap(
37 create_callback(process, true));
39 return instance_ap.release();
42 for (uint32_t idx = 0;
44 PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) !=
47 std::unique_ptr<DynamicLoader> instance_ap(
48 create_callback(process, false));
50 return instance_ap.release();
56 DynamicLoader::DynamicLoader(Process *process) : m_process(process) {}
58 DynamicLoader::~DynamicLoader() = default;
60 //----------------------------------------------------------------------
61 // Accessosors to the global setting as to whether to stop at image
62 // (shared library) loading/unloading.
63 //----------------------------------------------------------------------
65 bool DynamicLoader::GetStopWhenImagesChange() const {
66 return m_process->GetStopOnSharedLibraryEvents();
69 void DynamicLoader::SetStopWhenImagesChange(bool stop) {
70 m_process->SetStopOnSharedLibraryEvents(stop);
73 ModuleSP DynamicLoader::GetTargetExecutable() {
74 Target &target = m_process->GetTarget();
75 ModuleSP executable = target.GetExecutableModule();
78 if (executable->GetFileSpec().Exists()) {
79 ModuleSpec module_spec(executable->GetFileSpec(),
80 executable->GetArchitecture());
81 ModuleSP module_sp(new Module(module_spec));
83 // Check if the executable has changed and set it to the target executable
85 if (module_sp && module_sp->GetUUID().IsValid() &&
86 executable->GetUUID().IsValid()) {
87 if (module_sp->GetUUID() != executable->GetUUID())
89 } else if (executable->FileHasChanged()) {
94 executable = target.GetSharedModule(module_spec);
95 if (executable.get() != target.GetExecutableModulePointer()) {
96 // Don't load dependent images since we are in dyld where we will know
97 // and find out about all images that are loaded
98 const bool get_dependent_images = false;
99 target.SetExecutableModule(executable, get_dependent_images);
107 void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr,
109 bool base_addr_is_offset) {
110 UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset);
113 void DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module,
115 bool base_addr_is_offset) {
117 module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset,
121 void DynamicLoader::UnloadSections(const ModuleSP module) {
122 UnloadSectionsCommon(module);
125 void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) {
126 Target &target = m_process->GetTarget();
127 const SectionList *sections = GetSectionListFromModule(module);
129 assert(sections && "SectionList missing from unloaded module.");
131 const size_t num_sections = sections->GetSize();
132 for (size_t i = 0; i < num_sections; ++i) {
133 SectionSP section_sp(sections->GetSectionAtIndex(i));
134 target.SetSectionUnloaded(section_sp);
139 DynamicLoader::GetSectionListFromModule(const ModuleSP module) const {
140 SectionList *sections = nullptr;
142 ObjectFile *obj_file = module->GetObjectFile();
143 if (obj_file != nullptr) {
144 sections = obj_file->GetSectionList();
150 ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file,
151 addr_t link_map_addr,
153 bool base_addr_is_offset) {
154 Target &target = m_process->GetTarget();
155 ModuleList &modules = target.GetImages();
156 ModuleSpec module_spec(file, target.GetArchitecture());
159 if ((module_sp = modules.FindFirstModule(module_spec))) {
160 UpdateLoadedSections(module_sp, link_map_addr, base_addr,
161 base_addr_is_offset);
165 if ((module_sp = target.GetSharedModule(module_spec))) {
166 UpdateLoadedSections(module_sp, link_map_addr, base_addr,
167 base_addr_is_offset);
171 bool check_alternative_file_name = true;
172 if (base_addr_is_offset) {
173 // Try to fetch the load address of the file from the process as we need
175 // address to read the file out of the memory instead of a load bias.
176 bool is_loaded = false;
177 lldb::addr_t load_addr;
178 Error error = m_process->GetFileLoadAddress(file, is_loaded, load_addr);
179 if (error.Success() && is_loaded) {
180 check_alternative_file_name = false;
181 base_addr = load_addr;
185 // We failed to find the module based on its name. Lets try to check if we can
187 // different name based on the memory region info.
188 if (check_alternative_file_name) {
189 MemoryRegionInfo memory_info;
190 Error error = m_process->GetMemoryRegionInfo(base_addr, memory_info);
191 if (error.Success() && memory_info.GetMapped() &&
192 memory_info.GetRange().GetRangeBase() == base_addr) {
193 ModuleSpec new_module_spec(
194 FileSpec(memory_info.GetName().AsCString(), false),
195 target.GetArchitecture());
197 if ((module_sp = modules.FindFirstModule(new_module_spec))) {
198 UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
202 if ((module_sp = target.GetSharedModule(new_module_spec))) {
203 UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
209 if ((module_sp = m_process->ReadModuleFromMemory(file, base_addr))) {
210 UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
211 target.GetImages().AppendIfNeeded(module_sp);
217 int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr,
221 m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error);
225 return (int64_t)value;
228 addr_t DynamicLoader::ReadPointer(addr_t addr) {
230 addr_t value = m_process->ReadPointerFromMemory(addr, error);
232 return LLDB_INVALID_ADDRESS;