blob: 16f1ffca1a20d7941d8088c9e58e1701052bb649 [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
Kate Stoneb9c1b512016-09-06 20:57:50 +000010#include "lldb/Target/DynamicLoader.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000011
Steve Pucci9e02dac2014-02-06 19:02:19 +000012#include "lldb/Core/Module.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000013#include "lldb/Core/ModuleList.h" // for ModuleList
Steve Pucci9e02dac2014-02-06 19:02:19 +000014#include "lldb/Core/ModuleSpec.h"
Tamas Berghammerd7d69f82016-07-22 12:55:35 +000015#include "lldb/Core/PluginManager.h"
Steve Pucci9e02dac2014-02-06 19:02:19 +000016#include "lldb/Core/Section.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000017#include "lldb/Symbol/ObjectFile.h" // for ObjectFile
Tamas Berghammerd7d69f82016-07-22 12:55:35 +000018#include "lldb/Target/MemoryRegionInfo.h"
19#include "lldb/Target/Process.h"
20#include "lldb/Target/Target.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000021#include "lldb/Utility/ConstString.h" // for ConstString
22#include "lldb/lldb-private-interfaces.h" // for DynamicLoaderCreateInstance
23
24#include "llvm/ADT/StringRef.h" // for StringRef
25
26#include <memory> // for shared_ptr, unique_ptr
27
28#include <assert.h> // for assert
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
Kate Stoneb9c1b512016-09-06 20:57:50 +000033DynamicLoader *DynamicLoader::FindPlugin(Process *process,
34 const char *plugin_name) {
35 DynamicLoaderCreateInstance create_callback = nullptr;
36 if (plugin_name) {
37 ConstString const_plugin_name(plugin_name);
38 create_callback =
39 PluginManager::GetDynamicLoaderCreateCallbackForPluginName(
40 const_plugin_name);
41 if (create_callback) {
42 std::unique_ptr<DynamicLoader> instance_ap(
43 create_callback(process, true));
44 if (instance_ap)
45 return instance_ap.release();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 } else {
48 for (uint32_t idx = 0;
49 (create_callback =
50 PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) !=
51 nullptr;
52 ++idx) {
53 std::unique_ptr<DynamicLoader> instance_ap(
54 create_callback(process, false));
55 if (instance_ap)
56 return instance_ap.release();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 }
59 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060}
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062DynamicLoader::DynamicLoader(Process *process) : m_process(process) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Eugene Zelenko34ede342016-03-03 00:51:40 +000064DynamicLoader::~DynamicLoader() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065
66//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +000067// Accessosors to the global setting as to whether to stop at image (shared
68// library) loading/unloading.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069//----------------------------------------------------------------------
Eugene Zelenko34ede342016-03-03 00:51:40 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071bool DynamicLoader::GetStopWhenImagesChange() const {
72 return m_process->GetStopOnSharedLibraryEvents();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075void DynamicLoader::SetStopWhenImagesChange(bool stop) {
76 m_process->SetStopOnSharedLibraryEvents(stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077}
78
Kate Stoneb9c1b512016-09-06 20:57:50 +000079ModuleSP DynamicLoader::GetTargetExecutable() {
80 Target &target = m_process->GetTarget();
81 ModuleSP executable = target.GetExecutableModule();
Steve Pucci9e02dac2014-02-06 19:02:19 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 if (executable) {
84 if (executable->GetFileSpec().Exists()) {
85 ModuleSpec module_spec(executable->GetFileSpec(),
86 executable->GetArchitecture());
Zachary Turner2f3df612017-04-06 21:28:29 +000087 auto module_sp = std::make_shared<Module>(module_spec);
Steve Pucci9e02dac2014-02-06 19:02:19 +000088
Adrian Prantl05097242018-04-30 16:49:04 +000089 // Check if the executable has changed and set it to the target
90 // executable if they differ.
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 if (module_sp && module_sp->GetUUID().IsValid() &&
92 executable->GetUUID().IsValid()) {
93 if (module_sp->GetUUID() != executable->GetUUID())
94 executable.reset();
95 } else if (executable->FileHasChanged()) {
96 executable.reset();
97 }
Steve Pucci9e02dac2014-02-06 19:02:19 +000098
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 if (!executable) {
100 executable = target.GetSharedModule(module_spec);
101 if (executable.get() != target.GetExecutableModulePointer()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000102 // Don't load dependent images since we are in dyld where we will
103 // know and find out about all images that are loaded
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 const bool get_dependent_images = false;
105 target.SetExecutableModule(executable, get_dependent_images);
Steve Pucci9e02dac2014-02-06 19:02:19 +0000106 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 }
Steve Pucci9e02dac2014-02-06 19:02:19 +0000108 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 }
110 return executable;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000111}
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr,
114 addr_t base_addr,
115 bool base_addr_is_offset) {
116 UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset);
Steve Pucci9e02dac2014-02-06 19:02:19 +0000117}
118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119void DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module,
120 addr_t base_addr,
121 bool base_addr_is_offset) {
122 bool changed;
123 module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset,
124 changed);
Steve Pucci9e02dac2014-02-06 19:02:19 +0000125}
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127void DynamicLoader::UnloadSections(const ModuleSP module) {
128 UnloadSectionsCommon(module);
Steve Pucci9e02dac2014-02-06 19:02:19 +0000129}
130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) {
132 Target &target = m_process->GetTarget();
133 const SectionList *sections = GetSectionListFromModule(module);
Steve Pucci9e02dac2014-02-06 19:02:19 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 assert(sections && "SectionList missing from unloaded module.");
Steve Pucci9e02dac2014-02-06 19:02:19 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 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);
141 }
Steve Pucci9e02dac2014-02-06 19:02:19 +0000142}
143
Steve Pucci9e02dac2014-02-06 19:02:19 +0000144const SectionList *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145DynamicLoader::GetSectionListFromModule(const ModuleSP module) const {
146 SectionList *sections = nullptr;
147 if (module) {
148 ObjectFile *obj_file = module->GetObjectFile();
149 if (obj_file != nullptr) {
150 sections = obj_file->GetSectionList();
Steve Pucci9e02dac2014-02-06 19:02:19 +0000151 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 }
153 return sections;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file,
157 addr_t link_map_addr,
158 addr_t base_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());
163 ModuleSP module_sp;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 if ((module_sp = modules.FindFirstModule(module_spec))) {
166 UpdateLoadedSections(module_sp, link_map_addr, base_addr,
167 base_addr_is_offset);
Steve Pucci9e02dac2014-02-06 19:02:19 +0000168 return module_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 }
170
171 if ((module_sp = target.GetSharedModule(module_spec))) {
172 UpdateLoadedSections(module_sp, link_map_addr, base_addr,
173 base_addr_is_offset);
174 return module_sp;
175 }
176
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
Adrian Prantl05097242018-04-30 16:49:04 +0000180 // absolute load address to read the file out of the memory instead of a
181 // load bias.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 bool is_loaded = false;
183 lldb::addr_t load_addr;
Zachary Turner97206d52017-05-12 04:51:55 +0000184 Status error = m_process->GetFileLoadAddress(file, is_loaded, load_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 if (error.Success() && is_loaded) {
186 check_alternative_file_name = false;
187 base_addr = load_addr;
188 }
189 }
190
Adrian Prantl05097242018-04-30 16:49:04 +0000191 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 if (check_alternative_file_name) {
194 MemoryRegionInfo memory_info;
Zachary Turner97206d52017-05-12 04:51:55 +0000195 Status error = m_process->GetMemoryRegionInfo(base_addr, memory_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 if (error.Success() && memory_info.GetMapped() &&
Nitesh Jain5ba3d852017-03-31 10:55:55 +0000197 memory_info.GetRange().GetRangeBase() == base_addr &&
198 !(memory_info.GetName().IsEmpty())) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 ModuleSpec new_module_spec(
200 FileSpec(memory_info.GetName().AsCString(), false),
201 target.GetArchitecture());
202
203 if ((module_sp = modules.FindFirstModule(new_module_spec))) {
204 UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
205 return module_sp;
206 }
207
208 if ((module_sp = target.GetSharedModule(new_module_spec))) {
209 UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
210 return module_sp;
211 }
212 }
213 }
214
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);
218 }
219
220 return module_sp;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000221}
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr,
224 int size_in_bytes) {
Zachary Turner97206d52017-05-12 04:51:55 +0000225 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 uint64_t value =
227 m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error);
228 if (error.Fail())
229 return -1;
230 else
231 return (int64_t)value;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000232}
233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234addr_t DynamicLoader::ReadPointer(addr_t addr) {
Zachary Turner97206d52017-05-12 04:51:55 +0000235 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 addr_t value = m_process->ReadPointerFromMemory(addr, error);
237 if (error.Fail())
238 return LLDB_INVALID_ADDRESS;
239 else
240 return value;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000241}
Jim Ingham519b0812017-02-28 18:57:54 +0000242
243void DynamicLoader::LoadOperatingSystemPlugin(bool flush)
244{
245 if (m_process)
246 m_process->LoadOperatingSystemPlugin(flush);
247}
248