blob: 0f53da8b97f9599bb0e7682efc6a482da444067d [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- DynamicLoader.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-private.h"
11#include "lldb/Target/DynamicLoader.h"
Jim Ingham29950772013-01-26 02:19:28 +000012#include "lldb/Target/Process.h"
Steve Pucci9e02dac2014-02-06 19:02:19 +000013#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/PluginManager.h"
Steve Pucci9e02dac2014-02-06 19:02:19 +000015#include "lldb/Core/Module.h"
16#include "lldb/Core/ModuleSpec.h"
17#include "lldb/Core/Section.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
19using namespace lldb;
20using namespace lldb_private;
21
22DynamicLoader*
23DynamicLoader::FindPlugin (Process *process, const char *plugin_name)
24{
25 DynamicLoaderCreateInstance create_callback = NULL;
26 if (plugin_name)
27 {
Greg Clayton57abc5d2013-05-10 21:47:16 +000028 ConstString const_plugin_name(plugin_name);
29 create_callback = PluginManager::GetDynamicLoaderCreateCallbackForPluginName (const_plugin_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030 if (create_callback)
31 {
Greg Clayton7b0992d2013-04-18 22:45:39 +000032 std::unique_ptr<DynamicLoader> instance_ap(create_callback(process, true));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033 if (instance_ap.get())
34 return instance_ap.release();
35 }
36 }
37 else
38 {
39 for (uint32_t idx = 0; (create_callback = PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) != NULL; ++idx)
40 {
Greg Clayton7b0992d2013-04-18 22:45:39 +000041 std::unique_ptr<DynamicLoader> instance_ap(create_callback(process, false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042 if (instance_ap.get())
43 return instance_ap.release();
44 }
45 }
46 return NULL;
47}
48
49
50//----------------------------------------------------------------------
51// DynamicLoader constructor
52//----------------------------------------------------------------------
53DynamicLoader::DynamicLoader(Process *process) :
Jim Ingham29950772013-01-26 02:19:28 +000054 m_process (process)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055{
56}
57
58//----------------------------------------------------------------------
59// Destructor
60//----------------------------------------------------------------------
61DynamicLoader::~DynamicLoader()
62{
63}
64
65//----------------------------------------------------------------------
Greg Claytoned8a7052010-09-18 03:37:20 +000066// Accessosors to the global setting as to whether to stop at image
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067// (shared library) loading/unloading.
68//----------------------------------------------------------------------
69bool
70DynamicLoader::GetStopWhenImagesChange () const
71{
Jim Ingham29950772013-01-26 02:19:28 +000072 return m_process->GetStopOnSharedLibraryEvents();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
75void
76DynamicLoader::SetStopWhenImagesChange (bool stop)
77{
Jim Ingham29950772013-01-26 02:19:28 +000078 m_process->SetStopOnSharedLibraryEvents (stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079}
80
Steve Pucci9e02dac2014-02-06 19:02:19 +000081ModuleSP
82DynamicLoader::GetTargetExecutable()
83{
84 Target &target = m_process->GetTarget();
85 ModuleSP executable = target.GetExecutableModule();
86
87 if (executable.get())
88 {
89 if (executable->GetFileSpec().Exists())
90 {
91 ModuleSpec module_spec (executable->GetFileSpec(), executable->GetArchitecture());
92 ModuleSP module_sp (new Module (module_spec));
93
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())
96 {
97 if (module_sp->GetUUID() != executable->GetUUID())
98 executable.reset();
99 }
100 else if (executable->FileHasChanged())
101 {
102 executable.reset();
103 }
104
105 if (!executable.get())
106 {
107 executable = target.GetSharedModule(module_spec);
108 if (executable.get() != target.GetExecutableModulePointer())
109 {
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);
114 }
115 }
116 }
117 }
118 return executable;
119}
120
121void
122DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, addr_t base_addr)
123{
124 UpdateLoadedSectionsCommon(module, base_addr);
125}
126
127void
128DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module, addr_t base_addr)
129{
130 bool changed;
131 module->SetLoadAddress(m_process->GetTarget(), base_addr, changed);
132}
133
134void
135DynamicLoader::UnloadSections(const ModuleSP module)
136{
137 UnloadSectionsCommon(module);
138}
139
140void
141DynamicLoader::UnloadSectionsCommon(const ModuleSP module)
142{
143 Target &target = m_process->GetTarget();
144 const SectionList *sections = GetSectionListFromModule(module);
145
146 assert(sections && "SectionList missing from unloaded module.");
147
148 const size_t num_sections = sections->GetSize();
149 for (size_t i = 0; i < num_sections; ++i)
150 {
151 SectionSP section_sp (sections->GetSectionAtIndex(i));
152 target.SetSectionUnloaded(section_sp);
153 }
154}
155
156
157const SectionList *
158DynamicLoader::GetSectionListFromModule(const ModuleSP module) const
159{
160 SectionList *sections = nullptr;
161 if (module.get())
162 {
163 ObjectFile *obj_file = module->GetObjectFile();
164 if (obj_file)
165 {
166 sections = obj_file->GetSectionList();
167 }
168 }
169 return sections;
170}
171
172ModuleSP
173DynamicLoader::LoadModuleAtAddress(const FileSpec &file, addr_t link_map_addr, addr_t base_addr)
174{
175 Target &target = m_process->GetTarget();
176 ModuleList &modules = target.GetImages();
177 ModuleSP module_sp;
178
179 ModuleSpec module_spec (file, target.GetArchitecture());
180 if ((module_sp = modules.FindFirstModule (module_spec)))
181 {
182 UpdateLoadedSections(module_sp, link_map_addr, base_addr);
183 }
184 else if ((module_sp = target.GetSharedModule(module_spec)))
185 {
186 UpdateLoadedSections(module_sp, link_map_addr, base_addr);
187 }
188
189 return module_sp;
190}
191
192int64_t
193DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr, int size_in_bytes)
194{
195 Error error;
196
197 uint64_t value = m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error);
198 if (error.Fail())
199 return -1;
200 else
201 return (int64_t)value;
202}
203
204addr_t
205DynamicLoader::ReadPointer(addr_t addr)
206{
207 Error error;
208 addr_t value = m_process->ReadPointerFromMemory(addr, error);
209 if (error.Fail())
210 return LLDB_INVALID_ADDRESS;
211 else
212 return value;
213}