Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10 | #include "lldb/Target/DynamicLoader.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 11 | |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Module.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 13 | #include "lldb/Core/ModuleList.h" // for ModuleList |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 14 | #include "lldb/Core/ModuleSpec.h" |
Tamas Berghammer | d7d69f8 | 2016-07-22 12:55:35 +0000 | [diff] [blame] | 15 | #include "lldb/Core/PluginManager.h" |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Section.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/ObjectFile.h" // for ObjectFile |
Tamas Berghammer | d7d69f8 | 2016-07-22 12:55:35 +0000 | [diff] [blame] | 18 | #include "lldb/Target/MemoryRegionInfo.h" |
| 19 | #include "lldb/Target/Process.h" |
| 20 | #include "lldb/Target/Target.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 21 | #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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | DynamicLoader *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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | } 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | } |
| 59 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | DynamicLoader::DynamicLoader(Process *process) : m_process(process) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | |
Eugene Zelenko | 34ede34 | 2016-03-03 00:51:40 +0000 | [diff] [blame] | 64 | DynamicLoader::~DynamicLoader() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | |
| 66 | //---------------------------------------------------------------------- |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 67 | // Accessosors to the global setting as to whether to stop at image |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | // (shared library) loading/unloading. |
| 69 | //---------------------------------------------------------------------- |
Eugene Zelenko | 34ede34 | 2016-03-03 00:51:40 +0000 | [diff] [blame] | 70 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | bool DynamicLoader::GetStopWhenImagesChange() const { |
| 72 | return m_process->GetStopOnSharedLibraryEvents(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | void DynamicLoader::SetStopWhenImagesChange(bool stop) { |
| 76 | m_process->SetStopOnSharedLibraryEvents(stop); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | ModuleSP DynamicLoader::GetTargetExecutable() { |
| 80 | Target &target = m_process->GetTarget(); |
| 81 | ModuleSP executable = target.GetExecutableModule(); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | if (executable) { |
| 84 | if (executable->GetFileSpec().Exists()) { |
| 85 | ModuleSpec module_spec(executable->GetFileSpec(), |
| 86 | executable->GetArchitecture()); |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 87 | auto module_sp = std::make_shared<Module>(module_spec); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 88 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | // Check if the executable has changed and set it to the target executable |
| 90 | // if they differ. |
| 91 | 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 Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | if (!executable) { |
| 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 know |
| 103 | // and find out about all images that are loaded |
| 104 | const bool get_dependent_images = false; |
| 105 | target.SetExecutableModule(executable, get_dependent_images); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 106 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | } |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 108 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | } |
| 110 | return executable; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | void 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 Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | void 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 Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | void DynamicLoader::UnloadSections(const ModuleSP module) { |
| 128 | UnloadSectionsCommon(module); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) { |
| 132 | Target &target = m_process->GetTarget(); |
| 133 | const SectionList *sections = GetSectionListFromModule(module); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | assert(sections && "SectionList missing from unloaded module."); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
| 141 | } |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 144 | const SectionList * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | DynamicLoader::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 Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 151 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | } |
| 153 | return sections; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | ModuleSP 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 Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | if ((module_sp = modules.FindFirstModule(module_spec))) { |
| 166 | UpdateLoadedSections(module_sp, link_map_addr, base_addr, |
| 167 | base_addr_is_offset); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 168 | return module_sp; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 169 | } |
| 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 |
| 180 | // absolute load |
| 181 | // address to read the file out of the memory instead of a load bias. |
| 182 | bool is_loaded = false; |
| 183 | lldb::addr_t load_addr; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 184 | Status error = m_process->GetFileLoadAddress(file, is_loaded, load_addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | if (error.Success() && is_loaded) { |
| 186 | check_alternative_file_name = false; |
| 187 | base_addr = load_addr; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // We failed to find the module based on its name. Lets try to check if we can |
| 192 | // find a |
| 193 | // different name based on the memory region info. |
| 194 | if (check_alternative_file_name) { |
| 195 | MemoryRegionInfo memory_info; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 196 | Status error = m_process->GetMemoryRegionInfo(base_addr, memory_info); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | if (error.Success() && memory_info.GetMapped() && |
Nitesh Jain | 5ba3d85 | 2017-03-31 10:55:55 +0000 | [diff] [blame] | 198 | memory_info.GetRange().GetRangeBase() == base_addr && |
| 199 | !(memory_info.GetName().IsEmpty())) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | ModuleSpec new_module_spec( |
| 201 | FileSpec(memory_info.GetName().AsCString(), false), |
| 202 | target.GetArchitecture()); |
| 203 | |
| 204 | if ((module_sp = modules.FindFirstModule(new_module_spec))) { |
| 205 | UpdateLoadedSections(module_sp, link_map_addr, base_addr, false); |
| 206 | return module_sp; |
| 207 | } |
| 208 | |
| 209 | if ((module_sp = target.GetSharedModule(new_module_spec))) { |
| 210 | UpdateLoadedSections(module_sp, link_map_addr, base_addr, false); |
| 211 | return module_sp; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if ((module_sp = m_process->ReadModuleFromMemory(file, base_addr))) { |
| 217 | UpdateLoadedSections(module_sp, link_map_addr, base_addr, false); |
| 218 | target.GetImages().AppendIfNeeded(module_sp); |
| 219 | } |
| 220 | |
| 221 | return module_sp; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr, |
| 225 | int size_in_bytes) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 226 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | uint64_t value = |
| 228 | m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error); |
| 229 | if (error.Fail()) |
| 230 | return -1; |
| 231 | else |
| 232 | return (int64_t)value; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | addr_t DynamicLoader::ReadPointer(addr_t addr) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 236 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 237 | addr_t value = m_process->ReadPointerFromMemory(addr, error); |
| 238 | if (error.Fail()) |
| 239 | return LLDB_INVALID_ADDRESS; |
| 240 | else |
| 241 | return value; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 242 | } |
Jim Ingham | 519b081 | 2017-02-28 18:57:54 +0000 | [diff] [blame] | 243 | |
| 244 | void DynamicLoader::LoadOperatingSystemPlugin(bool flush) |
| 245 | { |
| 246 | if (m_process) |
| 247 | m_process->LoadOperatingSystemPlugin(flush); |
| 248 | } |
| 249 | |