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/lldb-private.h"
11 #include "lldb/Target/DynamicLoader.h"
12 #include "lldb/Target/Process.h"
13 #include "lldb/Target/Target.h"
14 #include "lldb/Core/PluginManager.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/Section.h"
20 using namespace lldb_private;
23 DynamicLoader::FindPlugin (Process *process, const char *plugin_name)
25 DynamicLoaderCreateInstance create_callback = NULL;
28 ConstString const_plugin_name(plugin_name);
29 create_callback = PluginManager::GetDynamicLoaderCreateCallbackForPluginName (const_plugin_name);
32 std::unique_ptr<DynamicLoader> instance_ap(create_callback(process, true));
33 if (instance_ap.get())
34 return instance_ap.release();
39 for (uint32_t idx = 0; (create_callback = PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) != NULL; ++idx)
41 std::unique_ptr<DynamicLoader> instance_ap(create_callback(process, false));
42 if (instance_ap.get())
43 return instance_ap.release();
50 //----------------------------------------------------------------------
51 // DynamicLoader constructor
52 //----------------------------------------------------------------------
53 DynamicLoader::DynamicLoader(Process *process) :
58 //----------------------------------------------------------------------
60 //----------------------------------------------------------------------
61 DynamicLoader::~DynamicLoader()
65 //----------------------------------------------------------------------
66 // Accessosors to the global setting as to whether to stop at image
67 // (shared library) loading/unloading.
68 //----------------------------------------------------------------------
70 DynamicLoader::GetStopWhenImagesChange () const
72 return m_process->GetStopOnSharedLibraryEvents();
76 DynamicLoader::SetStopWhenImagesChange (bool stop)
78 m_process->SetStopOnSharedLibraryEvents (stop);
82 DynamicLoader::GetTargetExecutable()
84 Target &target = m_process->GetTarget();
85 ModuleSP executable = target.GetExecutableModule();
89 if (executable->GetFileSpec().Exists())
91 ModuleSpec module_spec (executable->GetFileSpec(), executable->GetArchitecture());
92 ModuleSP module_sp (new Module (module_spec));
94 // Check if the executable has changed and set it to the target executable if they differ.
95 if (module_sp.get() && module_sp->GetUUID().IsValid() && executable->GetUUID().IsValid())
97 if (module_sp->GetUUID() != executable->GetUUID())
100 else if (executable->FileHasChanged())
105 if (!executable.get())
107 executable = target.GetSharedModule(module_spec);
108 if (executable.get() != target.GetExecutableModulePointer())
110 // Don't load dependent images since we are in dyld where we will know
111 // and find out about all images that are loaded
112 const bool get_dependent_images = false;
113 target.SetExecutableModule(executable, get_dependent_images);
122 DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, addr_t base_addr)
124 UpdateLoadedSectionsCommon(module, base_addr);
128 DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module, addr_t base_addr)
131 const bool base_addr_is_offset = true;
132 module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset, changed);
136 DynamicLoader::UnloadSections(const ModuleSP module)
138 UnloadSectionsCommon(module);
142 DynamicLoader::UnloadSectionsCommon(const ModuleSP module)
144 Target &target = m_process->GetTarget();
145 const SectionList *sections = GetSectionListFromModule(module);
147 assert(sections && "SectionList missing from unloaded module.");
149 const size_t num_sections = sections->GetSize();
150 for (size_t i = 0; i < num_sections; ++i)
152 SectionSP section_sp (sections->GetSectionAtIndex(i));
153 target.SetSectionUnloaded(section_sp);
159 DynamicLoader::GetSectionListFromModule(const ModuleSP module) const
161 SectionList *sections = nullptr;
164 ObjectFile *obj_file = module->GetObjectFile();
167 sections = obj_file->GetSectionList();
174 DynamicLoader::LoadModuleAtAddress(const FileSpec &file, addr_t link_map_addr, addr_t base_addr)
176 Target &target = m_process->GetTarget();
177 ModuleList &modules = target.GetImages();
180 ModuleSpec module_spec (file, target.GetArchitecture());
181 if ((module_sp = modules.FindFirstModule (module_spec)))
183 UpdateLoadedSections(module_sp, link_map_addr, base_addr);
185 else if ((module_sp = target.GetSharedModule(module_spec)))
187 UpdateLoadedSections(module_sp, link_map_addr, base_addr);
194 DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr, int size_in_bytes)
198 uint64_t value = m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error);
202 return (int64_t)value;
206 DynamicLoader::ReadPointer(addr_t addr)
209 addr_t value = m_process->ReadPointerFromMemory(addr, error);
211 return LLDB_INVALID_ADDRESS;