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 | |
Eugene Zelenko | 34ede34 | 2016-03-03 00:51:40 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
| 13 | // Project includes |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 14 | #include "lldb/Target/DynamicLoader.h" |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Module.h" |
| 16 | #include "lldb/Core/ModuleSpec.h" |
Tamas Berghammer | d7d69f8 | 2016-07-22 12:55:35 +0000 | [diff] [blame] | 17 | #include "lldb/Core/PluginManager.h" |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Section.h" |
Tamas Berghammer | d7d69f8 | 2016-07-22 12:55:35 +0000 | [diff] [blame] | 19 | #include "lldb/Target/MemoryRegionInfo.h" |
| 20 | #include "lldb/Target/Process.h" |
| 21 | #include "lldb/Target/Target.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | #include "lldb/lldb-private.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | DynamicLoader *DynamicLoader::FindPlugin(Process *process, |
| 28 | const char *plugin_name) { |
| 29 | DynamicLoaderCreateInstance create_callback = nullptr; |
| 30 | if (plugin_name) { |
| 31 | ConstString const_plugin_name(plugin_name); |
| 32 | create_callback = |
| 33 | PluginManager::GetDynamicLoaderCreateCallbackForPluginName( |
| 34 | const_plugin_name); |
| 35 | if (create_callback) { |
| 36 | std::unique_ptr<DynamicLoader> instance_ap( |
| 37 | create_callback(process, true)); |
| 38 | if (instance_ap) |
| 39 | return instance_ap.release(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | } else { |
| 42 | for (uint32_t idx = 0; |
| 43 | (create_callback = |
| 44 | PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) != |
| 45 | nullptr; |
| 46 | ++idx) { |
| 47 | std::unique_ptr<DynamicLoader> instance_ap( |
| 48 | create_callback(process, false)); |
| 49 | if (instance_ap) |
| 50 | return instance_ap.release(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | } |
| 53 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | DynamicLoader::DynamicLoader(Process *process) : m_process(process) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | |
Eugene Zelenko | 34ede34 | 2016-03-03 00:51:40 +0000 | [diff] [blame] | 58 | DynamicLoader::~DynamicLoader() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | |
| 60 | //---------------------------------------------------------------------- |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 61 | // Accessosors to the global setting as to whether to stop at image |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | // (shared library) loading/unloading. |
| 63 | //---------------------------------------------------------------------- |
Eugene Zelenko | 34ede34 | 2016-03-03 00:51:40 +0000 | [diff] [blame] | 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | bool DynamicLoader::GetStopWhenImagesChange() const { |
| 66 | return m_process->GetStopOnSharedLibraryEvents(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | void DynamicLoader::SetStopWhenImagesChange(bool stop) { |
| 70 | m_process->SetStopOnSharedLibraryEvents(stop); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | ModuleSP DynamicLoader::GetTargetExecutable() { |
| 74 | Target &target = m_process->GetTarget(); |
| 75 | ModuleSP executable = target.GetExecutableModule(); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 76 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | if (executable) { |
| 78 | if (executable->GetFileSpec().Exists()) { |
| 79 | ModuleSpec module_spec(executable->GetFileSpec(), |
| 80 | executable->GetArchitecture()); |
| 81 | ModuleSP module_sp(new Module(module_spec)); |
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 | // Check if the executable has changed and set it to the target executable |
| 84 | // if they differ. |
| 85 | if (module_sp && module_sp->GetUUID().IsValid() && |
| 86 | executable->GetUUID().IsValid()) { |
| 87 | if (module_sp->GetUUID() != executable->GetUUID()) |
| 88 | executable.reset(); |
| 89 | } else if (executable->FileHasChanged()) { |
| 90 | executable.reset(); |
| 91 | } |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | if (!executable) { |
| 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); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 100 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | } |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 102 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | } |
| 104 | return executable; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, |
| 108 | addr_t base_addr, |
| 109 | bool base_addr_is_offset) { |
| 110 | UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset); |
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::UpdateLoadedSectionsCommon(ModuleSP module, |
| 114 | addr_t base_addr, |
| 115 | bool base_addr_is_offset) { |
| 116 | bool changed; |
| 117 | module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset, |
| 118 | changed); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | void DynamicLoader::UnloadSections(const ModuleSP module) { |
| 122 | UnloadSectionsCommon(module); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) { |
| 126 | Target &target = m_process->GetTarget(); |
| 127 | const SectionList *sections = GetSectionListFromModule(module); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | assert(sections && "SectionList missing from unloaded module."); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
| 135 | } |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 138 | const SectionList * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | DynamicLoader::GetSectionListFromModule(const ModuleSP module) const { |
| 140 | SectionList *sections = nullptr; |
| 141 | if (module) { |
| 142 | ObjectFile *obj_file = module->GetObjectFile(); |
| 143 | if (obj_file != nullptr) { |
| 144 | sections = obj_file->GetSectionList(); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 145 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 146 | } |
| 147 | return sections; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file, |
| 151 | addr_t link_map_addr, |
| 152 | addr_t base_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()); |
| 157 | ModuleSP module_sp; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 158 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 159 | if ((module_sp = modules.FindFirstModule(module_spec))) { |
| 160 | UpdateLoadedSections(module_sp, link_map_addr, base_addr, |
| 161 | base_addr_is_offset); |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 162 | return module_sp; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | if ((module_sp = target.GetSharedModule(module_spec))) { |
| 166 | UpdateLoadedSections(module_sp, link_map_addr, base_addr, |
| 167 | base_addr_is_offset); |
| 168 | return module_sp; |
| 169 | } |
| 170 | |
| 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 |
| 174 | // absolute load |
| 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; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // We failed to find the module based on its name. Lets try to check if we can |
| 186 | // find a |
| 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()); |
| 196 | |
| 197 | if ((module_sp = modules.FindFirstModule(new_module_spec))) { |
| 198 | UpdateLoadedSections(module_sp, link_map_addr, base_addr, false); |
| 199 | return module_sp; |
| 200 | } |
| 201 | |
| 202 | if ((module_sp = target.GetSharedModule(new_module_spec))) { |
| 203 | UpdateLoadedSections(module_sp, link_map_addr, base_addr, false); |
| 204 | return module_sp; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 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); |
| 212 | } |
| 213 | |
| 214 | return module_sp; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr, |
| 218 | int size_in_bytes) { |
| 219 | Error error; |
| 220 | uint64_t value = |
| 221 | m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error); |
| 222 | if (error.Fail()) |
| 223 | return -1; |
| 224 | else |
| 225 | return (int64_t)value; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 228 | addr_t DynamicLoader::ReadPointer(addr_t addr) { |
| 229 | Error error; |
| 230 | addr_t value = m_process->ReadPointerFromMemory(addr, error); |
| 231 | if (error.Fail()) |
| 232 | return LLDB_INVALID_ADDRESS; |
| 233 | else |
| 234 | return value; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 235 | } |
Jim Ingham | 519b081 | 2017-02-28 18:57:54 +0000 | [diff] [blame^] | 236 | |
| 237 | void DynamicLoader::LoadOperatingSystemPlugin(bool flush) |
| 238 | { |
| 239 | if (m_process) |
| 240 | m_process->LoadOperatingSystemPlugin(flush); |
| 241 | } |
| 242 | |