1 //===-- DynamicLoader.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Target/DynamicLoader.h" 10 11 #include "lldb/Core/Module.h" 12 #include "lldb/Core/ModuleList.h" 13 #include "lldb/Core/ModuleSpec.h" 14 #include "lldb/Core/PluginManager.h" 15 #include "lldb/Core/Section.h" 16 #include "lldb/Symbol/LocateSymbolFile.h" 17 #include "lldb/Symbol/ObjectFile.h" 18 #include "lldb/Target/MemoryRegionInfo.h" 19 #include "lldb/Target/Platform.h" 20 #include "lldb/Target/Process.h" 21 #include "lldb/Target/Target.h" 22 #include "lldb/Utility/ConstString.h" 23 #include "lldb/Utility/LLDBLog.h" 24 #include "lldb/Utility/Log.h" 25 #include "lldb/lldb-private-interfaces.h" 26 27 #include "llvm/ADT/StringRef.h" 28 29 #include <memory> 30 31 #include <cassert> 32 33 using namespace lldb; 34 using namespace lldb_private; 35 36 DynamicLoader *DynamicLoader::FindPlugin(Process *process, 37 llvm::StringRef plugin_name) { 38 DynamicLoaderCreateInstance create_callback = nullptr; 39 if (!plugin_name.empty()) { 40 create_callback = 41 PluginManager::GetDynamicLoaderCreateCallbackForPluginName(plugin_name); 42 if (create_callback) { 43 std::unique_ptr<DynamicLoader> instance_up( 44 create_callback(process, true)); 45 if (instance_up) 46 return instance_up.release(); 47 } 48 } else { 49 for (uint32_t idx = 0; 50 (create_callback = 51 PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) != 52 nullptr; 53 ++idx) { 54 std::unique_ptr<DynamicLoader> instance_up( 55 create_callback(process, false)); 56 if (instance_up) 57 return instance_up.release(); 58 } 59 } 60 return nullptr; 61 } 62 63 DynamicLoader::DynamicLoader(Process *process) : m_process(process) {} 64 65 // Accessors to the global setting as to whether to stop at image (shared 66 // library) loading/unloading. 67 68 bool DynamicLoader::GetStopWhenImagesChange() const { 69 return m_process->GetStopOnSharedLibraryEvents(); 70 } 71 72 void DynamicLoader::SetStopWhenImagesChange(bool stop) { 73 m_process->SetStopOnSharedLibraryEvents(stop); 74 } 75 76 ModuleSP DynamicLoader::GetTargetExecutable() { 77 Target &target = m_process->GetTarget(); 78 ModuleSP executable = target.GetExecutableModule(); 79 80 if (executable) { 81 if (FileSystem::Instance().Exists(executable->GetFileSpec())) { 82 ModuleSpec module_spec(executable->GetFileSpec(), 83 executable->GetArchitecture()); 84 auto module_sp = std::make_shared<Module>(module_spec); 85 86 // Check if the executable has changed and set it to the target 87 // executable if they differ. 88 if (module_sp && module_sp->GetUUID().IsValid() && 89 executable->GetUUID().IsValid()) { 90 if (module_sp->GetUUID() != executable->GetUUID()) 91 executable.reset(); 92 } else if (executable->FileHasChanged()) { 93 executable.reset(); 94 } 95 96 if (!executable) { 97 executable = target.GetOrCreateModule(module_spec, true /* notify */); 98 if (executable.get() != target.GetExecutableModulePointer()) { 99 // Don't load dependent images since we are in dyld where we will 100 // know and find out about all images that are loaded 101 target.SetExecutableModule(executable, eLoadDependentsNo); 102 } 103 } 104 } 105 } 106 return executable; 107 } 108 109 void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr, 110 addr_t base_addr, 111 bool base_addr_is_offset) { 112 UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset); 113 } 114 115 void DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module, 116 addr_t base_addr, 117 bool base_addr_is_offset) { 118 bool changed; 119 module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset, 120 changed); 121 } 122 123 void DynamicLoader::UnloadSections(const ModuleSP module) { 124 UnloadSectionsCommon(module); 125 } 126 127 void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) { 128 Target &target = m_process->GetTarget(); 129 const SectionList *sections = GetSectionListFromModule(module); 130 131 assert(sections && "SectionList missing from unloaded module."); 132 133 const size_t num_sections = sections->GetSize(); 134 for (size_t i = 0; i < num_sections; ++i) { 135 SectionSP section_sp(sections->GetSectionAtIndex(i)); 136 target.SetSectionUnloaded(section_sp); 137 } 138 } 139 140 const SectionList * 141 DynamicLoader::GetSectionListFromModule(const ModuleSP module) const { 142 SectionList *sections = nullptr; 143 if (module) { 144 ObjectFile *obj_file = module->GetObjectFile(); 145 if (obj_file != nullptr) { 146 sections = obj_file->GetSectionList(); 147 } 148 } 149 return sections; 150 } 151 152 ModuleSP DynamicLoader::FindModuleViaTarget(const FileSpec &file) { 153 Target &target = m_process->GetTarget(); 154 ModuleSpec module_spec(file, target.GetArchitecture()); 155 156 if (ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec)) 157 return module_sp; 158 159 if (ModuleSP module_sp = target.GetOrCreateModule(module_spec, false)) 160 return module_sp; 161 162 return nullptr; 163 } 164 165 ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file, 166 addr_t link_map_addr, 167 addr_t base_addr, 168 bool base_addr_is_offset) { 169 if (ModuleSP module_sp = FindModuleViaTarget(file)) { 170 UpdateLoadedSections(module_sp, link_map_addr, base_addr, 171 base_addr_is_offset); 172 return module_sp; 173 } 174 175 return nullptr; 176 } 177 178 static ModuleSP ReadUnnamedMemoryModule(Process *process, addr_t addr, 179 llvm::StringRef name) { 180 char namebuf[80]; 181 if (name.empty()) { 182 snprintf(namebuf, sizeof(namebuf), "memory-image-0x%" PRIx64, addr); 183 name = namebuf; 184 } 185 return process->ReadModuleFromMemory(FileSpec(name), addr); 186 } 187 188 ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress( 189 Process *process, llvm::StringRef name, UUID uuid, addr_t value, 190 bool value_is_offset, bool force_symbol_search, bool notify, 191 bool set_address_in_target) { 192 ModuleSP memory_module_sp; 193 ModuleSP module_sp; 194 PlatformSP platform_sp = process->GetTarget().GetPlatform(); 195 Target &target = process->GetTarget(); 196 Status error; 197 198 if (!uuid.IsValid() && !value_is_offset) { 199 memory_module_sp = ReadUnnamedMemoryModule(process, value, name); 200 201 if (memory_module_sp) 202 uuid = memory_module_sp->GetUUID(); 203 } 204 ModuleSpec module_spec; 205 module_spec.GetUUID() = uuid; 206 FileSpec name_filespec(name); 207 if (FileSystem::Instance().Exists(name_filespec)) 208 module_spec.GetFileSpec() = name_filespec; 209 210 if (uuid.IsValid()) { 211 // Has lldb already seen a module with this UUID? 212 if (!module_sp) 213 error = ModuleList::GetSharedModule(module_spec, module_sp, nullptr, 214 nullptr, nullptr); 215 216 // Can lldb's symbol/executable location schemes 217 // find an executable and symbol file. 218 if (!module_sp) { 219 FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths(); 220 module_spec.GetSymbolFileSpec() = 221 Symbols::LocateExecutableSymbolFile(module_spec, search_paths); 222 ModuleSpec objfile_module_spec = 223 Symbols::LocateExecutableObjectFile(module_spec); 224 module_spec.GetFileSpec() = objfile_module_spec.GetFileSpec(); 225 if (FileSystem::Instance().Exists(module_spec.GetFileSpec()) && 226 FileSystem::Instance().Exists(module_spec.GetSymbolFileSpec())) { 227 module_sp = std::make_shared<Module>(module_spec); 228 } 229 } 230 231 // If we haven't found a binary, or we don't have a SymbolFile, see 232 // if there is an external search tool that can find it. 233 if (!module_sp || !module_sp->GetSymbolFileFileSpec()) { 234 Symbols::DownloadObjectAndSymbolFile(module_spec, error, 235 force_symbol_search); 236 if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) { 237 module_sp = std::make_shared<Module>(module_spec); 238 } 239 } 240 241 // If we only found the executable, create a Module based on that. 242 if (!module_sp && FileSystem::Instance().Exists(module_spec.GetFileSpec())) 243 module_sp = std::make_shared<Module>(module_spec); 244 } 245 246 // If we couldn't find the binary anywhere else, as a last resort, 247 // read it out of memory. 248 if (!module_sp.get() && value != LLDB_INVALID_ADDRESS && !value_is_offset) { 249 if (!memory_module_sp) 250 memory_module_sp = ReadUnnamedMemoryModule(process, value, name); 251 if (memory_module_sp) 252 module_sp = memory_module_sp; 253 } 254 255 Log *log = GetLog(LLDBLog::DynamicLoader); 256 if (module_sp.get()) { 257 // Ensure the Target has an architecture set in case 258 // we need it while processing this binary/eh_frame/debug info. 259 if (!target.GetArchitecture().IsValid()) 260 target.SetArchitecture(module_sp->GetArchitecture()); 261 target.GetImages().AppendIfNeeded(module_sp, false); 262 263 bool changed = false; 264 if (set_address_in_target) { 265 if (module_sp->GetObjectFile()) { 266 if (value != LLDB_INVALID_ADDRESS) { 267 LLDB_LOGF(log, 268 "DynamicLoader::LoadBinaryWithUUIDAndAddress Loading " 269 "binary UUID %s at %s 0x%" PRIx64, 270 uuid.GetAsString().c_str(), 271 value_is_offset ? "offset" : "address", value); 272 module_sp->SetLoadAddress(target, value, value_is_offset, changed); 273 } else { 274 // No address/offset/slide, load the binary at file address, 275 // offset 0. 276 LLDB_LOGF(log, 277 "DynamicLoader::LoadBinaryWithUUIDAndAddress Loading " 278 "binary UUID %s at file address", 279 uuid.GetAsString().c_str()); 280 module_sp->SetLoadAddress(target, 0, true /* value_is_slide */, 281 changed); 282 } 283 } else { 284 // In-memory image, load at its true address, offset 0. 285 LLDB_LOGF(log, 286 "DynamicLoader::LoadBinaryWithUUIDAndAddress Loading binary " 287 "UUID %s from memory at address 0x%" PRIx64, 288 uuid.GetAsString().c_str(), value); 289 module_sp->SetLoadAddress(target, 0, true /* value_is_slide */, 290 changed); 291 } 292 } 293 294 if (notify) { 295 ModuleList added_module; 296 added_module.Append(module_sp, false); 297 target.ModulesDidLoad(added_module); 298 } 299 } else { 300 LLDB_LOGF(log, "Unable to find binary with UUID %s and load it at " 301 "%s 0x%" PRIx64, 302 uuid.GetAsString().c_str(), 303 value_is_offset ? "offset" : "address", value); 304 } 305 306 return module_sp; 307 } 308 309 int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr, 310 int size_in_bytes) { 311 Status error; 312 uint64_t value = 313 m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error); 314 if (error.Fail()) 315 return -1; 316 else 317 return (int64_t)value; 318 } 319 320 addr_t DynamicLoader::ReadPointer(addr_t addr) { 321 Status error; 322 addr_t value = m_process->ReadPointerFromMemory(addr, error); 323 if (error.Fail()) 324 return LLDB_INVALID_ADDRESS; 325 else 326 return value; 327 } 328 329 void DynamicLoader::LoadOperatingSystemPlugin(bool flush) 330 { 331 if (m_process) 332 m_process->LoadOperatingSystemPlugin(flush); 333 } 334 335